1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
#include "fn.h"
#include "div.h"
#include "errloc.h"
#include "gl.h"
#include "im.h"
#include "parse.h"
#include "prv.h"
#include "pr.h"
#include "stmt.h"
#include "storage.h"
#include "td.h"
#include "ws.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static int pubfn(const struct lex *l, struct prv *p);
static int prvfn(const struct lex *l, struct prv *p);
static int end(const struct lex *l, struct prv *p);
static const struct seq fnseq[] =
{
{(struct step[])
{{ID, "public"}, {ID, "function"}, {ID}, {0}}, .fn = pubfn, .end = end},
{(struct step[])
{{ID, "function"}, {ID}, {0}}, .fn = prvfn, .end = end},
{0}
};
static int end(const struct lex *l, struct prv *p)
{
const struct fn *fn = fn_cur(p);
if (!fn->pr)
{
const struct tk *tk = fn->tk, *stk = p->stk;
if (lex_eof(l, p->stk))
errloc(--stk, "function \"%s\" has no body", tk->s);
else
errloc(p->tk, "incomplete \"%s\"", p->tk->s);
return -1;
}
return 0;
}
static const struct tk *findfn(const char *s, const struct ast *ast)
{
for (size_t i = 0; i < ast->nfns; i++)
{
const struct tk *tk = ast->fns[i].tk;
if (!strcmp(tk->s, s))
return tk;
}
return NULL;
}
static int pushfn(const struct tk *tk, const struct lex *lex, struct prv *p,
enum linkage l)
{
const struct tk *def;
struct ast *ast = p->ast;
size_t n = ast->nfns + 1;
struct fn *fns;
if ((def = findfn(tk->s, ast)))
{
const struct loc *loc = &def->loc;
errloc(tk, "function \"%s\" already defined at %s:%d:%d", tk->s,
loc->f, loc->line, loc->col);
return -1;
}
else if (!(fns = realloc(ast->fns, n * sizeof *fns)))
{
perror("realloc(3)");
return -1;
}
fns[ast->nfns++] = (struct fn){.linkage = l, .tk = tk};
ast->fns = fns;
if (div_fn(lex, p))
return -1;
fprintf(stderr, "adding %s function %s\n",
l == STATIC ? "private" : "public", tk->s);
return 1;
}
static int pubfn(const struct lex *l, struct prv *p)
{
return pushfn(p->stk + 2, l, p, EXTERNAL);
}
static int prvfn(const struct lex *l, struct prv *p)
{
return pushfn(p->stk + 1, l, p, STATIC);
}
const struct stentry *fn_var(const struct fn *fn, const struct tk *tk)
{
const struct storage *lk = fn->lk, *ws = fn->ws, *gl = fn->gl;
const struct stentry *e;
if ((lk && (e = storage_find(tk->s, lk)))
|| (ws && (e = storage_find(tk->s, ws)))
|| (gl && (e = storage_find(tk->s, gl))))
return e;
for (size_t i = 0; i < fn->nimps; i++)
if ((e = fn_var(fn->imps[i].ast.fns, tk)))
return e;
return NULL;
}
int fn_cgen(const struct fn *fn, struct cgen *c)
{
const struct pr *pr = fn->pr;
const struct param *ret = pr->ret;
const struct type *rt = NULL;
const char *s = NULL;
if (fn->gl && gl_cgen(fn->gl, c))
return -1;
switch (fn->linkage)
{
case STATIC:
printf("function ");
break;
case EXTERNAL:
printf("export function ");
break;
}
if (ret)
{
rt = ret->entry->t;
s = ret->entry->tk->s;
printf("%s ", cgen_abity(ret->entry->t));
}
printf("$%s(", fn->tk->s);
for (size_t i = 0; i < pr->nparams; i++)
{
const struct param *p = &pr->params[i];
printf(" %s", cgen_abity(p->entry->t));
printf(" %%__param_%s ", p->tk->s);
if (i + 1 < pr->nparams)
fputs(", ", stdout);
}
puts(") {\n@start");
for (size_t i = 0; i < pr->nparams; i++)
{
const struct param *p = &pr->params[i];
const struct type *t = p->entry->t;
const char *s = p->tk->s;
printf("%%%s_ =l alloc%zu %zu\n", s, t->align, t->sz);
printf("store%s %%__param_%s, %%%s_\n", cgen_sz(t->sz), s, s);
}
if (ret)
printf("%%%s_ =l alloc%zu %zu\n", s, rt->align, rt->sz);
if (fn->ws && ws_cgen(fn->ws, c))
return -1;
for (size_t i = 0; i < fn->nstmts; i++)
if (stmt_cgen(&fn->stmts[i], c))
return -1;
if (ret)
{
const char *t = cgen_tmp(rt->sz);
printf("%%%s =%s load%s %%%s_\n", t, cgen_sz(rt->sz), cgen_load(rt), s);
printf("ret %%%s", t);
}
else
fputs("ret", stdout);
puts("\n}");
return 0;
}
void fn_cgen_free(struct fn_cgen *c)
{
free(c);
}
void fn_free(struct fn *fn)
{
for (size_t i = 0; i < fn->nimps; i++)
im_free(&fn->imps[i]);
for (size_t i = 0; i < fn->nstmts; i++)
stmt_free(&fn->stmts[i]);
storage_free(fn->lk);
storage_free(fn->ws);
storage_free(fn->gl);
pr_free(fn->pr);
td_free(fn->td);
free(fn->imps);
free(fn->stmts);
}
struct fn *fn_cur(const struct prv *p)
{
struct ast *ast = p->ast;
if (p->proto)
return p->proto;
return &ast->fns[p->ast->nfns - 1];
}
int fn(const struct lex *l, struct prv *p)
{
struct pos init =
{
.seq = fnseq,
.stseq = fnseq,
.step = fnseq->steps
};
if (lex_eof(l, p->stk))
return 0;
else if (lex_eof(l, p->stk + 1))
{
errloc(p->stk, "incomplete function statement");
return -1;
}
else if (push(&init, p))
return -1;
p->stk = p->tk;
return 1;
}
|