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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
|
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include <ek/lower.h>
#include <ek/scope.h>
#include <ek/vec.h>
#define UNUSED(x) (void)x
enum retval_kind {
REG_I27,
REG_I9,
CONST_I9,
CONST_I27,
};
struct retval {
enum retval_kind kind;
char *s;
};
struct lower_state {
struct vec top;
struct vec bottom;
struct vec out;
int64_t uniq;
};
static struct lower_state create_state()
{
struct lower_state state;
state.top = vec_create(sizeof(char *));
state.bottom = vec_create(sizeof(char *));
state.out = vec_create(sizeof(char *));
state.uniq = 0;
return state;
}
static void destroy_state(struct lower_state *state)
{
assert(vec_len(&state->top) == 0);
assert(vec_len(&state->bottom) == 0);
assert(vec_len(&state->out) == 0);
vec_destroy(&state->top);
vec_destroy(&state->bottom);
vec_destroy(&state->out);
}
static void push_loop(struct lower_state *s, char *top, char *bottom, char *out)
{
vect_append(char *, s->top, &top);
vect_append(char *, s->bottom, &bottom);
vect_append(char *, s->out, &out);
}
static void pop_loop(struct lower_state *s)
{
char *top = vect_pop(char *, s->top);
char *bottom = vect_pop(char *, s->bottom);
char *out = vect_pop(char *, s->out);
free(top);
free(bottom);
free(out);
}
#define label_peek(v) \
vect_back(char *, v)
static int64_t retval_width(struct retval r)
{
switch (r.kind) {
case REG_I27: return 3;
case REG_I9: return 1;
case CONST_I9: return 1;
case CONST_I27: return 3;
default: abort();
}
return 0;
}
static const char *retval_kind_str(enum retval_kind kind)
{
switch (kind) {
case REG_I27: return "i27";
case REG_I9: return "i9";
case CONST_I9: return "i9";
case CONST_I27: return "i27";
default: abort();
}
return 0;
}
static const char *retval_type_str(struct retval r)
{
/* I guess it saves a bit on typing? */
return retval_kind_str(r.kind);
}
static bool retval_is_const(struct retval r)
{
return r.kind == CONST_I9 || r.kind == CONST_I27;
}
static bool is_i9(struct ast_node *n)
{
if (AST_TYPE(n->type).kind != AST_TYPE_PRIMITIVE)
return false;
return AST_PRIMITIVE_TYPE(n->type).type == AST_I9;
}
#define retval_create() \
vec_create(sizeof(struct retval))
#define foreach_retval(ri, retval) \
foreach_vec(ri, retval)
#define retval_at(rv, ri) \
vect_at(struct retval, rv, ri)
static void retval_destroy(struct vec *retval)
{
foreach_retval(ri, *retval) {
struct retval s = retval_at(*retval, ri);
free(s.s);
}
vec_destroy(retval);
}
static struct retval build_retval(enum retval_kind kind, char *s)
{
return (struct retval){.kind = kind, .s = s};
}
static __attribute__((format (printf, 1, 2)))
char *build_str(const char *fmt, ...) {
va_list args1, args2;
va_start(args1, fmt);
va_copy(args2, args1);
/* I don't expect this to fail, although I guess it could */
size_t size = (size_t)vsnprintf(NULL, 0, fmt, args1);
va_end(args1);
char *buf = malloc(size + 1);
vsnprintf(buf, size + 1, fmt, args2);
va_end(args2);
return buf;
}
static size_t get_scope_number(struct ast_node *id)
{
/** @todo this mirrors what's in actualize.c:actualize_id, same comments
* apply */
struct ast_node *def = file_scope_find_var(id->scope, id);
if (def)
return def->scope->number;
def = file_scope_find_proc(id->scope, id);
if (def)
return def->scope->number;
return 0;
}
static char *mangle_idx(struct ast_node *id, size_t idx)
{
assert(id->node_type == AST_ID);
assert(id->scope);
const char *name = AST_ID(id).id;
/* oh wait, I need to do a variable lookup on the ID, not use the ID's
* scope number, duh */
size_t number = get_scope_number(id);
if (ast_flags(id, AST_FLAG_NOMANGLE))
return strdup(name);
return build_str("%s_s%zif%zi", name, number, idx);
}
static char *mangle(struct ast_node *id)
{
return mangle_idx(id, 0);
}
static int lower_expr(struct lower_state *s, struct ast_node *e,
struct vec *retval);
static int lower_statement(struct lower_state *s, struct ast_node *n);
static void output_id(struct ast_node *id)
{
char *name = mangle(id);
printf("%s", name);
free(name);
}
static int lower_global_var(struct ast_node *n)
{
/* trivial types are reasonably easy, but stuff like compound types need
* a lot of work */
struct ast_node *type = AST_VAR(n).type;
if (AST_TYPE(type).kind != AST_TYPE_PRIMITIVE) {
semantic_error(n->scope->fctx, n,
"only primitive globals currently implemented");
return -1;
}
struct ast_node *id = AST_VAR(n).id;
struct ast_node *init = AST_VAR(n).init;
if (init->node_type != AST_CONST) {
semantic_error(n->scope->fctx, n,
"constant expressions currently not implemented");
return -1;
}
output_id(id);
printf(" = ");
/* hmm, this might be useful elsewhere as well */
switch (AST_PRIMITIVE_TYPE(type).type) {
case AST_I27: printf("i27 %lli", AST_CONST(init).integer); break;
case AST_I9: printf("i9 %lli", AST_CONST(init).integer); break;
default:
semantic_error(n->scope->fctx, n,
"unhandled primitive type");
return -1;
}
printf(";\n");
return 0;
}
static int lower_param(struct lower_state *s, struct ast_node *p)
{
UNUSED(s);
assert(p->node_type == AST_VAR);
struct ast_node *type = AST_VAR(p).type;
if (AST_TYPE(type).kind != AST_TYPE_PRIMITIVE) {
semantic_error(p->scope->fctx, p,
"only primitive params currently implemented");
return -1;
}
assert(AST_VAR(p).init == NULL);
switch (AST_PRIMITIVE_TYPE(type).type) {
case AST_I27: printf("i27 "); break;
case AST_I9: printf("i9 "); break;
default:
semantic_error(p->scope->fctx, p,
"unhandled primitive type");
return -1;
}
struct ast_node *id = AST_VAR(p).id;
output_id(id);
printf(",");
return 0;
}
static int lower_params(struct lower_state *s, struct ast_node *params)
{
for (struct ast_node *p = params; p; p = p->next) {
if (lower_param(s, p))
return -1;
}
return 0;
}
static int lower_var(struct lower_state *s, struct ast_node *v,
struct vec *retval)
{
assert(v->node_type == AST_VAR);
struct vec input = retval_create();
if (lower_expr(s, AST_VAR(v).init, &input))
return -1;
struct ast_node *id = AST_VAR(v).id;
/* if we have a struct, we should add the member name to the base name
* */
foreach_retval(ri, input) {
struct retval r = retval_at(input, ri);
char *name = mangle_idx(id, ri);
/* I assume we're always dealing with i27 for now */
/** @todo qbt could maybe skip the type stuff except for casts */
printf("i27 %s = %s;\n", name, r.s);
struct retval n = build_retval(REG_I27, name);
vec_append(retval, &n);
}
retval_destroy(&input);
return 0;
}
static void do_const_store(struct lower_state *s, struct vec *from,
struct retval t, struct retval o)
{
UNUSED(s);
/* I know, kind of silly to swap back and forth between string/int but
* good enough for now */
int64_t addr = strtoll(t.s, 0, 0) + strtoll(o.s, 0, 0);
foreach_retval(ri, *from) {
struct retval r = retval_at(*from, ri);
/* doesn't really take into account possible padding etc, should
* probably fix at some point */
printf("%s >> %s (%zi);\n",
r.s, retval_type_str(r), addr);
addr += retval_width(r);
}
}
static void do_store(struct lower_state *s, struct vec *from, struct vec *to,
struct vec *off)
{
assert(vec_len(to) == 1);
struct retval t = retval_at(*to, 0);
struct retval o = build_retval(CONST_I27, "0");
if (off)
o = retval_at(*off, 0);
if (retval_is_const(t) && retval_is_const(o)) {
do_const_store(s, from, t, o);
return;
}
if (retval_is_const(t)) {
/* o must be register, so swap around for the format to make
* sense */
struct retval tmp = t; t = o; o = tmp;
}
assert(retval_is_const(o));
int64_t addr = strtoll(o.s, 0, 0);
foreach_retval(ri, *from) {
struct retval r = retval_at(*from, ri);
/* doesn't really take into account possible padding etc, should
* probably fix at some point */
printf("%s >> %s %s %zi;\n",
r.s, retval_type_str(r), t.s, addr);
addr += retval_width(r);
}
}
static int lower_cast(struct lower_state *s, struct ast_node *e,
struct vec *retval)
{
assert(e->node_type == AST_CAST);
/** @todo make sure actualize removes casts that aren't of these types
* */
assert(AST_TYPE(e->type).kind == AST_TYPE_PRIMITIVE
|| AST_TYPE(e->type).kind == AST_TYPE_POINTER);
if (lower_expr(s, AST_CAST(e).expr, retval))
return -1;
enum retval_kind kind = REG_I27;
if (is_i9(e))
kind = REG_I9;
foreach_retval(ri, *retval) {
struct retval r = retval_at(*retval, ri);
/* build new temporary cast result and replace the previous
* retval */
char *s = build_str("%s%s", "cast_", r.s);
printf("%s %s = %s;\n", retval_kind_str(kind), s, r.s);
free(r.s);
r.s = s;
r.kind = kind;
retval_at(*retval, ri) = r;
}
return 0;
}
static int lower_const(struct lower_state *s, struct ast_node *c,
struct vec *retval)
{
UNUSED(s);
assert(c->node_type == AST_CONST);
if (AST_CONST(c).kind == AST_CONST_STRING) {
/* requires pushing strings as variables and replacing them with
* references */
semantic_error(c->scope->fctx, c,
"string constant lowering not yet implemented");
return -1;
}
enum retval_kind type = CONST_I27;
if (AST_PRIMITIVE_TYPE(c->type).type == AST_I9)
type = CONST_I9;
char *str = build_str("%lli", (long long int)AST_CONST(c).integer);
struct retval r = build_retval(type, str);
vec_append(retval, &r);
return 0;
}
static int lower_assign(struct lower_state *s, struct ast_node *a,
struct vec *retval)
{
#define IS_DEREF(t) (t->node_type == AST_UNOP && AST_UNOP(t).op == AST_DEREF)
#define IS_ARR(t) (t->node_type == AST_ARR_ACCESS)
assert(a->node_type == AST_ASSIGN);
if (lower_expr(s, AST_ASSIGN(a).from, retval))
return -1;
struct vec loc = retval_create();
struct vec off = retval_create();
struct ast_node *to = AST_ASSIGN(a).to;
struct ast_node *base = to;
if (IS_DEREF(to))
base = AST_UNOP(to).expr;
else if (IS_ARR(to)) {
base = AST_ARR_ACCESS(to).base;
if (lower_expr(s, AST_ARR_ACCESS(to).idx, &off)) {
retval_destroy(&loc);
retval_destroy(&off);
return -1;
}
}
if (lower_expr(s, base, &loc)) {
retval_destroy(&loc);
retval_destroy(&off);
return -1;
}
if (IS_DEREF(to)) {
do_store(s, retval, &loc, NULL);
}
else if (IS_ARR(to)) {
do_store(s, retval, &loc, &off);
} else {
assert(vec_len(retval) == vec_len(&loc));
foreach_retval(ri, *retval) {
struct retval to = retval_at(loc, ri);
struct retval from = retval_at(*retval, ri);
printf("i27 %s = %s;\n", to.s, from.s);
}
}
retval_destroy(&loc);
retval_destroy(&off);
return 0;
#undef IS_DEREF
#undef IS_ARR
}
static int lower_id(struct lower_state *s, struct ast_node *id,
struct vec *retval)
{
UNUSED(s);
assert(id->node_type == AST_ID);
char *m = mangle(id);
struct ast_node *type = id->type;
if (AST_TYPE(type).kind != AST_TYPE_PRIMITIVE
&& AST_TYPE(type).kind != AST_TYPE_POINTER
&& AST_TYPE(type).kind != AST_TYPE_SIGN) {
semantic_error(id->scope->fctx, id,
"only primitive ids currently implemented");
return -1;
}
enum retval_kind kind = REG_I27;
if (is_i9(id))
kind = REG_I9;
/* this likely isn't enough and we need to add the & to most things we
* want to take the address of */
if (AST_TYPE(type).kind == AST_TYPE_SIGN) {
char *o = m;
m = build_str("&%s", m);
free(o);
}
struct retval r = build_retval(kind, m);
vec_append(retval, &r);
return 0;
}
static int lower_return(struct lower_state *s, struct ast_node *r,
struct vec *retval)
{
assert(r->node_type == AST_RETURN);
if (lower_expr(s, AST_RETURN(r).expr, retval))
return -1;
printf("=> ( ");
foreach_retval(ri, *retval) {
struct retval r = retval_at(*retval, ri);
printf("%s, ", r.s);
}
printf(" );\n");
return 0;
}
static int lower_if(struct lower_state *s, struct ast_node *i,
struct vec *retval)
{
assert(i->node_type == AST_IF);
if (lower_expr(s, AST_IF(i).cond, retval))
return -1;
assert(vec_len(retval) == 1);
/* helps readability a little bit */
long long uniq = s->uniq++;
printf("if%lli:\n", uniq);
char *bottom = build_str("if_else%lli", uniq);
char *out = build_str("if_out%lli", uniq);
printf("! %s -> %s;\n", (retval_at(*retval, 0)).s, bottom);
/* a block counts as a statement in this case */
if (lower_statement(s, AST_IF(i).body)) {
free(bottom);
return -1;
}
printf("-> %s;\n", out);
printf("%s:\n", bottom);
free(bottom);
if (AST_IF(i).els && lower_statement(s, AST_IF(i).els)) {
free(out);
return -1;
}
printf("%s:\n", out);
free(out);
return 0;
}
static int lower_for(struct lower_state *s, struct ast_node *f,
struct vec *retval)
{
assert(f->node_type == AST_FOR);
if (lower_statement(s, AST_FOR(f).pre))
return -1;
long long uniq = s->uniq++;
char *top = build_str("for_top%lli", uniq);
char *bottom = build_str("for_bottom%lli", uniq);
char *out = build_str("for_out%lli", uniq);
push_loop(s, top, bottom, out);
printf("-> %s;\n", out);
printf("%s:\n", top);
if (lower_statement(s, AST_FOR(f).body)) {
pop_loop(s);
return -1;
}
printf("%s:\n", bottom);
if (lower_statement(s, AST_FOR(f).post)) {
pop_loop(s);
return -1;
}
printf("%s:\n", out);
if (lower_expr(s, AST_FOR(f).cond, retval)) {
pop_loop(s);
return -1;
}
assert(vec_len(retval) == 1);
printf("%s -> %s;\n", (retval_at(*retval, 0)).s, top);
pop_loop(s);
return 0;
}
static int lower_expr_if(struct lower_state *s, struct ast_node *i,
struct vec *retval)
{
semantic_error(i->scope->fctx, i,
"expr if unimplemented");
return 0;
}
static int lower_binop(struct lower_state *s, struct ast_node *i,
struct vec *retval)
{
struct vec l = retval_create();
struct vec r = retval_create();
if (lower_expr(s, AST_BINOP(i).left, &l)) {
retval_destroy(&l);
retval_destroy(&r);
return -1;
}
if (lower_expr(s, AST_BINOP(i).right, &r)) {
retval_destroy(&l);
retval_destroy(&r);
return -1;
}
assert(vec_len(&l) == 1);
assert(vec_len(&r) == 1);
char *name = build_str("tmp%lli", (long long)s->uniq++);
struct retval ret = build_retval(REG_I27, name);
vec_append(retval, &ret);
char *op = "";
switch (AST_BINOP(i).op) {
case AST_ADD: op = "+"; break;
case AST_SUB: op = "-"; break;
case AST_MUL: op = "*"; break;
case AST_DIV: op = "/"; break;
case AST_REM: op = "%"; break;
case AST_LSHIFT: op = "<<"; break;
case AST_RSHIFT: op = ">>"; break;
case AST_LT: op = "<"; break;
case AST_GT: op = ">"; break;
case AST_LE: op = "<="; break;
case AST_GE: op = ">="; break;
case AST_NE: op = "!="; break;
case AST_EQ: op = "=="; break;
default: semantic_error(i->scope->fctx, i,
"unimplemented binary operation");
retval_destroy(&l);
retval_destroy(&r);
return -1;
}
printf("i27 %s = %s %s %s;\n", name,
(retval_at(l, 0)).s,
op,
(retval_at(r, 0)).s);
retval_destroy(&l);
retval_destroy(&r);
return 0;
}
static int lower_call(struct lower_state *s, struct ast_node *c,
struct vec *retval)
{
assert(c->node_type == AST_CALL);
struct vec call = retval_create();
if (lower_expr(s, AST_CALL(c).expr, &call)) {
retval_destroy(&call);
return -1;
}
/* collect all args */
struct vec args = retval_create();
foreach_node(a, AST_CALL(c).args) {
struct vec arg = retval_create();
if (lower_expr(s, a, &arg)) {
retval_destroy(&arg);
retval_destroy(&args);
return -1;
}
foreach_retval(ri, arg) {
struct retval r = retval_at(arg, ri);
/* very important! */
r.s = strdup(r.s);
vec_append(&args, &r);
}
retval_destroy(&arg);
}
assert(vec_len(&call) == 1);
printf("%s (", (retval_at(call, 0)).s);
retval_destroy(&call);
foreach_retval(ri, args) {
struct retval r = retval_at(args, ri);
printf("%s, ", r.s);
}
#define IS_VOID(t) \
(t->node_type == AST_TYPE && AST_TYPE(t).kind == AST_TYPE_PRIMITIVE && \
AST_PRIMITIVE_TYPE(t).type == AST_VOID)
if (!IS_VOID(c->type)) {
semantic_error(c->scope->fctx, c,
"only void return type implemented");
retval_destroy(&args);
return -1;
}
printf(") => ();\n");
retval_destroy(&args);
return 0;
}
static int lower_expr(struct lower_state *s, struct ast_node *e,
struct vec *retval)
{
if (!e)
return 0;
switch (e->node_type) {
case AST_ID: return lower_id(s, e, retval);
/* var is considered an expression in this case */
case AST_VAR: return lower_var(s, e, retval);
case AST_CAST: return lower_cast(s, e, retval);
case AST_CONST: return lower_const(s, e, retval);
case AST_RETURN: return lower_return(s, e, retval);
case AST_ASSIGN: return lower_assign(s, e, retval);
case AST_BINOP: return lower_binop(s, e, retval);
case AST_CALL: return lower_call(s, e, retval);
case AST_IF: return lower_expr_if(s, e, retval);
default:
semantic_error(e->scope->fctx, e,
"unhandled expr in lowering");
return -1;
}
return 0;
}
static int lower_block(struct lower_state *s, struct ast_node *body)
{
assert(body->node_type == AST_BLOCK);
assert(!ast_flags(body, AST_FLAG_DOEXPR));
struct ast_node *stmt = AST_BLOCK(body).body;
for (; stmt; stmt = stmt->next) {
if (lower_statement(s, stmt))
return -1;
}
return 0;
}
static int lower_statement(struct lower_state *s, struct ast_node *n)
{
struct vec retval = retval_create();
int ret = 0;
switch (n->node_type) {
case AST_RETURN: ret = lower_return(s, n, &retval); break;
case AST_IF: ret = lower_if(s, n, &retval); break;
case AST_FOR: ret = lower_for(s, n, &retval); break;
case AST_BLOCK: ret = lower_block(s, n); break;
default: ret = lower_expr(s, n, &retval); break;
}
retval_destroy(&retval);
return ret;
}
static int lower_proc(struct ast_node *n)
{
assert(n->node_type == AST_PROC);
/* nobody uses the proc, so no need to do anything */
if (n->uses == 0 && !ast_flags(AST_PROC(n).id, AST_FLAG_NOMANGLE))
return 0;
struct lower_state state = create_state();
/* name */
struct ast_node *id = AST_PROC(n).id;
output_id(id);
/* args */
printf("(");
struct ast_node *sign = AST_PROC(n).sign;
if (lower_params(&state, AST_SIGN_TYPE(sign).params)) {
destroy_state(&state);
return -1;
}
/* no return type currently supported by qbt */
printf(")\n");
/* body */
printf("{\n");
if (lower_block(&state, AST_PROC(n).body)) {
destroy_state(&state);
return -1;
}
printf("}\n");
destroy_state(&state);
return 0;
}
static int lower_actual(struct ast_node *n)
{
assert(AST_TYPE(n).kind == AST_TYPE_CONSTRUCT);
return 0;
}
static int _lower_actuals(struct scope *root)
{
/* go through all child scopes but only do actual work on file-scope
* includes are allowed inside procs etc to make something only locally
* visible */
for (struct scope *c = root->children; c; c = c->next) {
if (_lower_actuals(c))
return -1;
}
if (!scope_flags(root, SCOPE_FILE))
return 0;
for (struct visible *v = root->vars; v; v = v->next) {
assert(v->node);
if (lower_global_var(v->node))
return -1;
}
for (struct visible *p = root->procs; p; p = p->next) {
assert(p->node);
if (lower_proc(p->node))
return -1;
}
return 0;
}
int lower_actuals(struct scope *root)
{
int ret = _lower_actuals(root);
/* actuals are currently global, would it make more sense for them to be
* scope-local? */
for (struct actual *a = root->actuals; a; a = a->next) {
assert(a->node);
if (lower_actual(a->node))
return -1;
}
return ret;
}
|