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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
#include "call.h"
#include "errloc.h"
#include "fn.h"
#include "lex.h"
#include "lit.h"
#include "parse.h"
#include "prv.h"
#include "stmt.h"
#include "type.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int callpr(const struct lex *l, struct prv *p);
static int callp(const struct lex *l, struct prv *p);
static int callr(const struct lex *l, struct prv *p);
static int callv(const struct lex *l, struct prv *p);
static const struct seq seqs[] =
{
{(struct step[]){
{ID},
{ID, "using"},
{ANY, NULL, 1},
{ID, "returning"},
{ID},
{0}}, .fn = callpr},
{(struct step[]){
{ID},
{ID, "using"},
{ANY, NULL, 1},
{0}}, .fn = callp},
{(struct step[]){
{ID},
{ID, "returning"},
{ID},
{0}}, .fn = callr},
{(struct step[]){
{ID},
{0}}, .fn = callv},
{0}
};
static int param(const struct lex *l, struct prv *p, const struct tk *tk,
struct call *c)
{
const struct fn *fn = fn_cur(p);
size_t n = c->nparams + 1;
struct callparam *params;
const struct lit *lit = NULL;
const struct stentry *e = NULL;
const struct type *t = NULL;
int sign = 0;
union callv u = {0};
if (tk->type == LIT)
{
if (!(lit = lit_push(tk, p)))
return -1;
}
else if (tk->type == NUM)
{
if ((sign = *tk->s == '-'))
u.v = strtoll(tk->s, NULL, 0);
else
u.uv = strtoull(tk->s, NULL, 0);
}
else if ((t = type_find(fn, tk->s)))
{
if (t->type != C)
{
errloc(tk, "type \"%s\" not a constant", tk->s);
return -1;
}
}
else if (!(e = fn_var(fn, tk)))
{
errloc(tk, "undefined reference to parameter \"%s\"", tk->s);
return -1;
}
if (!(params = realloc(c->params, n * sizeof *params)))
{
perror("realloc(3)");
return -1;
}
c->params = params;
c->params[c->nparams++] = (struct callparam)
{
.entry = e,
.lit = lit,
.t = t,
.u = u
};
return 0;
}
static int callpr(const struct lex *l, struct prv *p)
{
const struct tk *tk = p->stk + 2;
struct call *c = &stmt_cur(p)->u.call;
const struct pr *pr = c->pr;
const struct stentry *e;
size_t n = 0;
while (!lex_eof(l, tk) && !kw((tk++)->s))
n++;
if (pr->variadic)
{
if (n < pr->nparams)
{
errloc(tk, "function \"%s\" expects at least %zu parameters, "
"but %zu were given", c->tk->s, pr->nparams, n);
return -1;
}
}
else if (n != pr->nparams)
{
tk -= 2;
errloc(tk, "function \"%s\" expects %zu parameters, "
"but %zu were given", c->tk->s, pr->nparams, n);
return -1;
}
else if (!pr->ret)
{
errloc(tk, "function \"%s\" does not expect any return variable",
c->tk->s);
return -1;
}
tk = p->stk + 2;
for (size_t i = 0; i < n; i++)
if (param(l, p, tk++, c))
return -1;
if (!(e = fn_var(fn_cur(p), ++tk)))
{
errloc(tk, "undefined reference to return variable \"%s\"",
tk->s);
return -1;
}
else if (pop(l, p))
return -1;
fprintf(stderr, "\t\tadding call to %s using %zu params "
"and %s as return variable\n", c->tk->s, n, tk->s);
c->ret = e;
c->nparams = n;
p->stk = ++tk;
return 1;
}
static int callr(const struct lex *l, struct prv *p)
{
const struct tk *tk = p->stk + 2;
const struct fn *fn = fn_cur(p);
const struct stentry *e;
struct call *c = &stmt_cur(p)->u.call;
const struct pr *pr = c->pr;
if (!pr->ret)
{
errloc(tk, "function \"%s\" does not expect any return variable",
c->tk->s);
return -1;
}
else if (pr->nparams)
{
errloc(tk, "function \"%s\" expects %zu parameters, "
"but none were given", c->tk->s, pr->nparams);
return -1;
}
else if (!(e = fn_var(fn, tk)))
{
errloc(tk, "undefined reference to return variable \"%s\"",
tk->s);
return -1;
}
else if (pop(l, p))
return -1;
fprintf(stderr, "\t\tadding call to %s with %s as return variable\n",
c->tk->s, tk->s);
c->ret = e;
p->stk = ++tk;
return 1;
}
static int callp(const struct lex *l, struct prv *p)
{
const struct tk *tk = p->stk + 2;
struct call *c = &stmt_cur(p)->u.call;
const struct pr *pr = c->pr;
size_t n = 0;
while (!lex_eof(l, tk) && !kw((tk++)->s))
n++;
if (pr->variadic)
{
if (n < pr->nparams)
{
errloc(tk, "function \"%s\" expects at least %zu parameters, "
"but %zu were given", c->tk->s, pr->nparams, n);
return -1;
}
}
else if (pr->nparams != n)
{
errloc(tk, "function \"%s\" expects %zu parameters, "
"but %zu were given", c->tk->s, pr->nparams, n);
return -1;
}
tk = p->stk + 2;
for (size_t i = 0; i < n; i++)
if (param(l, p, tk++, c))
return -1;
if (pop(l, p))
return -1;
fprintf(stderr, "\t\tadding call to %s with 1 param\n", c->tk->s);
p->stk = tk;
return 1;
}
static int callv(const struct lex *l, struct prv *p)
{
const struct tk *tk = p->stk;
struct call *c = &stmt_cur(p)->u.call;
const struct pr *pr = c->pr;
if (pr->nparams)
{
errloc(tk, "function \"%s\" expects %zu parameters, "
"but none were given", c->tk->s, pr->nparams);
return -1;
}
else if (pop(l, p))
return -1;
fprintf(stderr, "\t\tadding call to %s with %s as return variable\n",
c->tk->s, tk->s);
p->stk = ++tk;
return 1;
}
static const struct pr *find_proto(const struct fn *fn, const char *s)
{
const struct type *t = type_find(fn, s);
if (t && t->type == P)
return t->u.p.fn->pr;
return NULL;
}
static const struct pr *find(const struct ast *ast, const char *s)
{
for (size_t i = 0; i < ast->nfns; i++)
{
const struct fn *fn = &ast->fns[i];
const struct pr *pr;
if ((pr = find_proto(fn, s)))
return pr;
else if (!strcmp(fn->tk->s, s))
return fn->pr;
}
return NULL;
}
static int gettmp(char *tmp, size_t n)
{
size_t j;
for (j = 0; j < n; j++)
{
char c = tmp[j];
if (!c)
return tmp[j] = 'a';
else if (c != 'z')
return ++(tmp[j]);
}
return EOF;
}
static int param_lit(const struct lit *lit, struct cgen *c)
{
printf("l $%s", lit->name);
return 0;
}
static int param_id(const struct stentry *e, struct cgen *c, char *tmp,
size_t n)
{
const char *name = e->tk->s;
const struct type *t = e->t;
printf("%s ", cgen_sz(t->sz));
if (cgen_global(c->fn, e))
printf("$%s", name);
else if (cgen_abity(t))
{
if (gettmp(tmp, n) == EOF)
return -1;
printf("%%%s", tmp);
}
else
printf("%%%s_", name);
return 0;
}
static int param_c(const struct type *t, struct cgen *cg)
{
const union c *c = &t->u.c;
/* TODO: adapt type to expected param size */
fputs("l ", stdout);
if (t->sign)
printf("%lld", c->v);
else
printf("%llu", c->uv);
return 0;
}
static int param_num(const struct callparam *cp, struct cgen *c)
{
const union callv *u = &cp->u;
/* TODO: adapt type to expected param size */
fputs("w ", stdout);
if (cp->sign)
printf("%lld", u->v);
else
printf("%llu", u->uv);
return 0;
}
static int param_load(const struct call *m, struct cgen *c)
{
char tmp[sizeof "abcdefghijklmnopqrstuvwxyz"] = {0};
for (size_t i = 0; i < m->nparams; i++)
{
const struct callparam *cp = &m->params[i];
const struct stentry *e = cp->entry;
if (!e || cgen_global(c->fn, e) || !cgen_abity(e->t))
continue;
else if (gettmp(tmp, sizeof tmp - 1) == EOF)
{
fprintf(stderr, "%s: exhausted temporaries\n", __func__);
return -1;
}
printf("%%%s =%s load%s %%%s_\n", tmp, cgen_sz(e->t->sz),
cgen_load(e->t), e->tk->s);
}
return 0;
}
int call_cgen(const struct call *m, struct cgen *c)
{
char tmps[sizeof "abcdefghijklmnopqrstuvwxyz"] = {0};
const struct stentry *ret = m->ret;
const char *tmp;
size_t sz;
if (param_load(m, c))
return -1;
else if (ret)
{
tmp = cgen_tmp((sz = ret->t->sz));
printf("%%%s =%s ", tmp, cgen_sz(sz));
}
printf("call $%s(", m->tk->s);
for (size_t i = 0; i < m->nparams; i++)
{
const struct callparam *cp = &m->params[i];
int var = m->pr->variadic;
if (cp->lit)
param_lit(cp->lit, c);
else if (cp->entry)
param_id(cp->entry, c, tmps, sizeof tmps - 1);
else if (cp->t)
param_c(cp->t, c);
else
param_num(cp, c);
if (var && i + 1 == var - 1)
fputs(", ...", stdout);
if (i + 1 < m->nparams)
fputs(", ", stdout);
}
puts(")");
if (ret)
{
const char *name = ret->tk->s;
printf("store%s %%%s, ", cgen_sz(sz), tmp);
if (cgen_global(c->fn, ret))
printf("%s", name);
else
printf("%%%s_", name);
putchar('\n');
}
return 0;
}
void call_free(struct call *c)
{
if (c)
free(c->params);
}
int call(const struct lex *l, struct prv *p)
{
const struct tk *tk = p->tk;
const struct pr *pr;
struct fn *fn = fn_cur(p);
size_t n = fn->nstmts +1;
struct stmt *stmts;
struct pos init =
{
.seq = seqs,
.stseq = seqs,
.step = seqs->steps
};
/* TODO: function pointers */
if (lex_eof(l, tk))
{
errloc(tk - 1, "incomplete call statement");
return -1;
}
else if (!(pr = find(p->ast, tk->s)))
{
errloc(tk, "undefined reference to function \"%s\"", tk->s);
return -1;
}
else if (!(stmts = realloc(fn->stmts, n * sizeof *stmts)))
{
perror("realloc(3)");
return -1;
}
fn->stmts = stmts;
fn->stmts[fn->nstmts++] = (struct stmt)
{
.type = CALL,
.u.call =
{
.tk = tk,
.pr = pr
}
};
if (push(&init, p))
return -1;
p->stk = p->tk;
return 1;
}
|