/
m1_gencode.c
1241 lines (974 loc) · 32 KB
/
m1_gencode.c
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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Code generator.
Visit each node, and generate instructions as appropriate.
See m1_ast.h for an overview of the AST node types. For most
nodes/functions, a m1_reg structure is returned, that holds the
type and number of the register that will hold the result of
the expression for which code was generated.
Example: a node representing a floating point number will load
the number in an N register, and return that register. This happens
in gencode_number().
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include "m1_gencode.h"
#include "m1_ast.h"
#include "m1_compiler.h"
#include "m1_stack.h"
#include "m1_symtab.h"
#include "m1_ann.h"
#define OUT stdout
#define M1DEBUG 1
#ifdef M1DEBUG
#define debug(x) fprintf(stderr, x);
#else
#define debug(x)
#endif
static m1_reg gencode_expr(M1_compiler *comp, m1_expression *e);
static void gencode_block(M1_compiler *comp, m1_expression *block);
static m1_reg gencode_obj(M1_compiler *comp, m1_object *obj, m1_object **parent);
static const char type_chars[4] = {'i', 'n', 's', 'p'};
static const char reg_chars[4] = {'I', 'N', 'S', 'P'};
/*
Allocate new registers as needed.
*/
static m1_reg
gen_reg(M1_compiler *comp, m1_valuetype type) {
m1_reg reg;
assert(comp != NULL);
assert((type >=0) && (type < 4));
reg.type = type;
reg.no = comp->regs[type]++;
fprintf(stderr, "generating regi %d for type %d\n", reg.no, type);
return reg;
}
/*
Generate label identifiers.
*/
static int
gen_label(M1_compiler *comp) {
assert(comp != NULL);
return comp->label++;
}
static m1_reg
gencode_number(M1_compiler *comp, m1_literal *lit) {
/*
deref Nx, CONSTS, <const_id>
*/
m1_reg reg,
constindex;
assert(comp != NULL);
assert(lit != NULL);
assert(lit->type = VAL_FLOAT);
assert(lit->sym != NULL);
reg = gen_reg(comp, VAL_FLOAT);
constindex = gen_reg(comp, VAL_INT);
fprintf(OUT, "\tset_imm\tI%d, 0, %d\n", constindex.no, lit->sym->constindex);
fprintf(OUT, "\tderef\tN%d, CONSTS, %d\n", reg.no, constindex.no);
return reg;
}
static m1_reg
gencode_int(M1_compiler *comp, m1_literal *lit) {
/*
If the value is smaller than 256*255 and > 0,
then generate set_imm, otherwise, load the constant
from the constants table.
deref Ix, CONSTS, <const_id>
or:
set_imm Ix, y, z
*/
m1_reg reg;
assert(comp != NULL);
assert(lit != NULL);
assert(lit->type == VAL_INT);
assert(lit->sym != NULL);
reg = gen_reg(comp, VAL_INT);
if (lit->sym->value.ival < (256 * 255) && lit->sym->value.ival >= 0) { /* XXX check these numbers. operands are 8 bit? */
/* use set_imm X, N*256, remainder) */
int remainder = lit->sym->value.ival % 256;
int num256 = (lit->sym->value.ival - remainder) / 256;
fprintf(OUT, "\tset_imm\tI%d, %d, %d\n", reg.no, num256, remainder);
}
else {
m1_reg constindex = gen_reg(comp, VAL_INT);
fprintf(OUT, "\tset_imm\tI%d, 0, %d\n", constindex.no, lit->sym->constindex);
fprintf(OUT, "\tderef\tI%d, CONSTS, I%d\n", reg.no, constindex.no);
}
return reg;
}
static m1_reg
gencode_string(M1_compiler *comp, m1_literal *lit) {
m1_reg reg,
constidxreg;
assert(comp != NULL);
assert(lit != NULL);
assert(lit->sym != NULL);
assert(lit->type == VAL_STRING);
reg = gen_reg(comp, VAL_STRING);
constidxreg = gen_reg(comp, VAL_INT);
fprintf(OUT, "\tset_imm\tI%d, 0, %d\n", constidxreg.no, lit->sym->constindex);
fprintf(OUT, "\tderef\tS%d, CONSTS, I%d\n", reg.no, constidxreg.no);
return reg;
}
static m1_reg
gencode_assign(M1_compiler *comp, NOTNULL(m1_assignment *a)) {
m1_reg lhs, rhs;
m1_object *parent;
debug("gencode_assign start...\n");
assert(a != NULL);
rhs = gencode_expr(comp, a->rhs);
lhs = gencode_obj(comp, a->lhs, &parent);
/* copy the value held in register for rhs to the register of lhs */
assert((lhs.type >= 0) && (lhs.type < 4));
assert((rhs.type >= 0) && (rhs.type < 4));
fprintf(OUT, "\tset \t%c%d, %c%d, x\n", reg_chars[(int)lhs.type], lhs.no,
reg_chars[(int)rhs.type], rhs.no);
return lhs;
}
static m1_reg
gencode_null(M1_compiler *comp) {
m1_reg reg;
reg = gen_reg(comp, VAL_INT);
/* "null" is just 0, but then in a "pointer" context. */
fprintf(OUT, "\tset_imm\tI%d, 0, 0\n", reg.no);
return reg;
}
static m1_reg
gencode_obj(M1_compiler *comp, m1_object *obj, m1_object **parent) {
m1_reg reg;
assert(comp != NULL);
assert(comp->currentchunk != NULL);
assert(&comp->currentchunk->locals != NULL);
/* handle OBJECT_LINK nodes differently as they are the key to the recursion. */
if (obj->type == OBJECT_LINK) {
m1_reg field;
fprintf(stderr ,"object\n");
/* set the parent OUT parameter to the currently visited node (obj) */
*parent = obj;
/* visit this node's parent recursively, depth-first.
parent parameter will return a pointer to it so it can
be passed on when visiting the field. Note that this invocation
is recursive, so THIS function will be called again. Note also that
the tree was built upside down, so obj->parent is really its parent
in which the current node is a (link-node to a) field.
x.y.z looks like this:
x y z
\ / /
L1 /
\ /
L2
^
|
Node L2 is the root in this tree. Both L1 and L2 are of type OBJECT_LINK.
Node "x" is OBJECT_MAIN, whereas nodes "y" and "z" are OBJECT_FIELD.
First this function (gencode_obj) goes all the way down to x, sets the
OUT parameter "parent" to itself, then as the function returns, comes
back to L1, then visits y, passing on a pointer to node for "x" through
the parent parameter. Then, node y sets the parent OUT parameter to itself
(again, in this funciton gencode_obj), and then control goes up to L2,
visiting z, passing a pointer to node "y" through the parent parameter.
Neat huh?
*/
reg = gencode_obj(comp, obj->parent, parent);
assert(obj->obj.field != NULL);
/* pass the parent to the field node, so "c" knows who "b" is in b.c. */
field = gencode_obj(comp, obj->obj.field, parent);
}
switch (obj->type) {
case OBJECT_MAIN:
{
fprintf(stderr, "main\n");
assert(obj->obj.field != NULL);
assert(obj->sym != NULL);
/* if symbol has not register allocated yet, do it now. */
if (obj->sym->regno == NO_REG_ALLOCATED_YET) {
m1_reg r = gen_reg(comp, obj->sym->valtype);
obj->sym->regno = r.no;
}
reg.no = obj->sym->regno;
reg.type = obj->sym->valtype;
/*
fprintf(OUT, "\tset\t%c%d, ?? # load object %s's base address in reg\n",
reg_chars[(int)reg.type],
reg.no,
obj->obj.name);
*/
/* return a pointer to this node by OUT parameter. */
*parent = obj;
break;
}
case OBJECT_FIELD: /* example: b in a.b */
{
/* XXX need to get a pointer to the m1_struct object that represents
the parent (a in example) , so that we can look for the field (b in example).
The m1_structfield node for the field (b) has a field <offset> that must be
added to the base address, if it's > 0.
XXX2: study this in further detail. Need to use deref/set_ref.
*/
int offset = 0;
if (offset > 0) {
m1_reg offsetreg = gen_reg(comp, VAL_INT);
//fprintf(OUT, "\tset_imm\tI%d, 0, %d\n", offsetreg.no, offset);
//fprintf(OUT, "\tadd_i\tI%d, I%d, I%d\n", reg.no, reg.no, offsetreg.no);
}
/* set parent OUT parameter to the current node. */
*parent = obj;
fprintf(stderr, "parent: %s\n", (*parent)->obj.name);
break;
}
case OBJECT_DEREF: /* b in a->b */
reg = gencode_obj(comp, obj->obj.field, parent);
fprintf(OUT, "\tadd_i <struct>, I%d\n", reg.no);
break;
case OBJECT_INDEX: /* b in a[b] */
{
int offset = 0;
m1_reg offsetreg = gencode_expr(comp, obj->obj.index);
if (obj->is_target == 1) {
// fprintf(OUT, "\tderef\t%d, <array>, I%d\n", reg.no);
}
else {
}
break;
}
default:
break;
}
return reg;
}
static m1_reg
gencode_while(M1_compiler *comp, m1_whileexpr *w) {
/*
...
goto L2
L1
<block>
L2:
goto_if <cond>, L1
...
*/
m1_reg reg;
int startlabel = gen_label(comp),
endlabel = gen_label(comp);
/* push break label onto stack so break statement knows where to go. */
push(comp->breakstack, endlabel);
fprintf(OUT, "\tgoto L%d\n", endlabel);
fprintf(OUT, "L%d:\n", startlabel);
gencode_block(comp, w->block);
fprintf(OUT, "L%d:\n", endlabel);
reg = gencode_expr(comp, w->cond);
fprintf(OUT, "\tgoto_if\tL%d, %c%d\n", startlabel, reg_chars[(int)reg.type], reg.no);
/* remove break label from stack. */
(void)pop(comp->breakstack);
return reg;
}
static m1_reg
gencode_dowhile(M1_compiler *comp, m1_whileexpr *w) {
/*
START:
<code for block>
<code for cond>
goto_if START
*/
m1_reg reg;
int startlabel = gen_label(comp);
int endlabel = gen_label(comp);
push(comp->breakstack, endlabel);
fprintf(OUT, "L%d:\n", startlabel);
gencode_block(comp, w->block);
reg = gencode_expr(comp, w->cond);
fprintf(OUT, "\tgoto_if\tL%d, %c%d\n", startlabel, reg_chars[(int)reg.type], reg.no);
fprintf(OUT, "L%d:\n", endlabel);
(void)pop(comp->breakstack);
return reg;
}
static m1_reg
gencode_for(M1_compiler *comp, m1_forexpr *i) {
/*
<code for init>
START:
<code for cond>
goto_if cond, L1
goto END
L1:
<code for block>
<code for step>
goto START
END:
*/
int startlabel = gen_label(comp),
endlabel = gen_label(comp),
blocklabel = gen_label(comp);
m1_reg reg;
if (i->init)
gencode_expr(comp, i->init);
fprintf(OUT, "L%d:\n", startlabel);
if (i->cond)
reg = gencode_expr(comp, i->cond);
fprintf(OUT, "\tgoto_if L%d, %c%d\n", blocklabel, reg_chars[(int)reg.type], reg.no);
fprintf(OUT, "\tgoto L%d\n", endlabel);
fprintf(OUT, "L%d:\n", blocklabel);
if (i->block)
gencode_expr(comp, i->block);
if (i->step)
gencode_expr(comp, i->step);
fprintf(OUT, "\tgoto L%d\n", startlabel);
fprintf(OUT, "L%d:\n", endlabel);
return reg;
}
static m1_reg
gencode_if(M1_compiler *comp, m1_ifexpr *i) {
/*
result1 = <evaluate condition>
goto_if result1, L1
<code for elseblock>
goto L2
L1:
<code for ifblock>
L2:
*/
m1_reg condreg;
int endlabel = gen_label(comp),
iflabel = gen_label(comp) ;
condreg = gencode_expr(comp, i->cond);
fprintf(OUT, "\tgoto_if\tL%d, %c%d\n", iflabel, reg_chars[(int)condreg.type], condreg.no);
/* else block */
if (i->elseblock) {
gencode_block(comp, i->elseblock);
}
fprintf(OUT, "\tgoto L%d\n", endlabel);
/* if block */
fprintf(OUT, "L%d:\n", iflabel);
gencode_expr(comp, i->ifblock);
fprintf(OUT, "L%d:\n", endlabel);
return condreg;
}
static m1_reg
gencode_deref(M1_compiler *comp, m1_object *o) {
m1_reg reg;
reg = gencode_obj(comp, o, NULL);
return reg;
}
static m1_reg
gencode_address(M1_compiler *comp, m1_object *o) {
m1_reg reg;
reg = gencode_obj(comp, o, NULL);
return reg;
}
static m1_reg
gencode_return(M1_compiler *comp, m1_expression *e) {
m1_reg reg;
reg = gencode_expr(comp, e);
return reg;
}
static m1_reg
gencode_or(M1_compiler *comp, m1_binexpr *b) {
/*
left = <evaluate left>
goto_if left, END
right = <evaluate right>
left = right
END:
*/
m1_reg left, right;
int endlabel;
endlabel = gen_label(comp);
left = gencode_expr(comp, b->left);
/* if left was not true, then need to evaluate right, otherwise short-cut. */
fprintf(OUT, "\tgoto_if L%d, %c%d\n", endlabel, reg_chars[(int)left.type], left.no);
right = gencode_expr(comp, b->right);
fprintf(OUT, "\tset\t%c%d, %c%d, x\n", reg_chars[(int)left.type], left.no,
reg_chars[(int)right.type], right.no);
fprintf(OUT, "L%d:\n", endlabel);
return left;
}
static m1_reg
gencode_and(M1_compiler *comp, m1_binexpr *b) {
/*
left = <evaluate left>
goto_if left, evalright
goto END
evalright:
right = <evaluate right>
left = right
END:
*/
m1_reg left, right;
int endlabel = gen_label(comp);
int evalright = gen_label(comp);
left = gencode_expr(comp, b->left);
/* if left was false, no need to evaluate right, and go to end. */
fprintf(OUT, "\tgoto_if\tL%d, %c%d\n", evalright, reg_chars[(int)left.type], left.no);
fprintf(OUT, "\tgoto L%d\n", endlabel);
fprintf(OUT, "L%d:\n", evalright);
right = gencode_expr(comp, b->right);
/* copy result from right to left result reg, as that's the reg that will be returned. */
fprintf(OUT, "\tset\t%c%d, %c%d, x\n", reg_chars[(int)left.type], left.no, reg_chars[(int)right.type], right.no);
fprintf(OUT, "L%d:\n", endlabel);
return left;
}
/*
Helper function for != and == ops. The code generation template is the same,
except for one field. This is parameterized with the parameter is_eq_op, which
indicates whether it's the == op (is_eq_op = true) or the != op (is_eq_op = false).
*/
static m1_reg
ne_eq_common(M1_compiler *comp, m1_binexpr *b, int is_eq_op) {
/* code for EQ; NE swaps the result.
left = <code for left>
right = <code for right>
diff = left - right
goto_if NOTEQUAL, diff # not zero
result = 1
goto END
NOTEQUAL:
result = 0
END:
*/
m1_reg reg, left, right;
int endlabel, eq_ne_label;
left = gencode_expr(comp, b->left);
right = gencode_expr(comp, b->right);
endlabel = gen_label(comp);
eq_ne_label = gen_label(comp);
reg = gen_reg(comp, VAL_INT);
fprintf(OUT, "\tsub_i\tI%d, %c%d, %c%d\n", reg.no, reg_chars[(int)left.type], left.no,
reg_chars[(int)right.type], right.no);
fprintf(OUT, "\tgoto_if L%d, %c%d\n", eq_ne_label, reg_chars[(int)reg.type], reg.no);
fprintf(OUT, "\tset_imm\t%c%d, 0, %d\n", reg_chars[(int)reg.type], reg.no, is_eq_op);
fprintf(OUT, "\tgoto L%d\n", endlabel);
fprintf(OUT, "L%d:\n", eq_ne_label);
fprintf(OUT, "\tset_imm\t%c%d, 0, %d\n", reg_chars[(int)reg.type], reg.no, !is_eq_op);
fprintf(OUT, "L%d:\n", endlabel);
return reg;
}
static m1_reg
gencode_ne(M1_compiler *comp, m1_binexpr *b) {
return ne_eq_common(comp, b, 0);
}
static m1_reg
gencode_eq(M1_compiler *comp, m1_binexpr *b) {
return ne_eq_common(comp, b, 1);
}
static m1_reg
gencode_lt(M1_compiler *comp, m1_binexpr *b) {
/* for LT (<) operator, use the ISGE opcode, but swap its arguments. */
m1_reg result = gen_reg(comp, VAL_INT);
m1_reg left = gencode_expr(comp, b->left);
m1_reg right = gencode_expr(comp, b->right);
fprintf(OUT, "\tisge_%c I%d, %c%d, %c%d\n", type_chars[(int)left.type], result.no,
reg_chars[(int)right.type], right.no,
reg_chars[(int)left.type], left.no);
return result;
}
static m1_reg
gencode_le(M1_compiler *comp, m1_binexpr *b) {
/* for LE (<=) operator, use the ISGT opcode, but swap its arguments. */
m1_reg result = gen_reg(comp, VAL_INT);
m1_reg left = gencode_expr(comp, b->left);
m1_reg right = gencode_expr(comp, b->right);
fprintf(OUT, "\tisgt_%c I%d, %c%d, %c%d\n", type_chars[(int)left.type], result.no,
reg_chars[(int)right.type], right.no,
reg_chars[(int)left.type], left.no);
return result;
}
static m1_reg
gencode_binary(M1_compiler *comp, m1_binexpr *b) {
char *op = NULL;
m1_reg left,
right,
target;
left = gencode_expr(comp, b->left);
switch(b->op) {
case OP_ASSIGN:
op = "set"; /* in case of a = b = c; then b = c part is a binary expression */
break;
case OP_PLUS:
if (left.type == VAL_INT)
op = "add_i";
else if (left.type == VAL_FLOAT)
op = "add_n";
else { /* should not happen */
fprintf(stderr, "wrong type for add");
exit(EXIT_FAILURE);
}
break;
case OP_MINUS:
if (left.type == VAL_INT)
op = "sub_i";
else if (left.type == VAL_FLOAT)
op = "sub_n";
else { /* should not happen */
fprintf(stderr, "wrong type for sub");
exit(EXIT_FAILURE);
}
break;
case OP_MUL:
if (left.type == VAL_INT)
op = "mult_i";
else if (left.type == VAL_FLOAT)
op = "mult_n";
else { /* should not happen */
fprintf(stderr, "wrong type for mul");
exit(EXIT_FAILURE);
}
break;
case OP_DIV:
if (left.type == VAL_INT)
op = "div_i";
else if (left.type == VAL_FLOAT)
op = "div_n";
else { /* should not happen */
fprintf(stderr, "wrong type for div");
exit(EXIT_FAILURE);
}
break;
case OP_MOD:
if (left.type == VAL_INT)
op = "mod_i";
else if (left.type == VAL_FLOAT)
op = "mod_n";
else { /* should not happen */
fprintf(stderr, "wrong type for mod");
exit(EXIT_FAILURE);
}
break;
case OP_XOR:
op = "xor";
break;
case OP_GT:
if (left.type == VAL_INT)
op = "isgt_i";
else if (left.type == VAL_FLOAT)
op = "isgt_n";
else {
fprintf(stderr, "wrong type for isgt");
exit(EXIT_FAILURE);
}
break;
case OP_GE:
if (left.type == VAL_INT)
op = "isge_i";
else if (left.type == VAL_FLOAT)
op = "isge_n";
else {
fprintf(stderr, "wrong type for isge");
exit(EXIT_FAILURE);
}
break;
case OP_LT:
return gencode_lt(comp, b);
case OP_LE:
return gencode_le(comp, b);
case OP_EQ:
return gencode_eq(comp, b);
case OP_NE: /* a != b;*/
return gencode_ne(comp, b);
case OP_AND: /* a && b */
return gencode_and(comp, b);
case OP_OR: /* a || b */
return gencode_or(comp, b);
case OP_BAND:
op = "and";
break;
case OP_BOR:
op = "or";
break;
default:
op = "unknown op";
break;
}
right = gencode_expr(comp, b->right);
target = gen_reg(comp, left.type);
fprintf(OUT, "\t%s\t%c%d, %c%d, %c%d\n", op,
reg_chars[(int)target.type], target.no,
reg_chars[(int)left.type], left.no,
reg_chars[(int)right.type], right.no);
return target;
}
static m1_reg
gencode_not(M1_compiler *comp, m1_unexpr *u) {
m1_reg reg, temp;
int label1, label2;
reg = gencode_expr(comp, u->expr);
temp = gen_reg(comp, VAL_INT);
/* if reg is zero, make it nonzero (false->true).
If it's non-zero, make it zero. (true->false).
*/
/*
goto_if reg, L1 #non-zero, make it zero.
set_imm Ix, 0, 0
goto L2
L1: # nonzero, make it zero
set_imm Ix, 0, 1
L2:
set reg, Ix
#done
*/
label1 = gen_label(comp);
label2 = gen_label(comp);
fprintf(OUT, "\tgoto_if\tL%d, %c%d\n", label1, reg_chars[(int)reg.type], reg.no);
fprintf(OUT, "\tset_imm\tI%d, 0, 1\n", temp.no);
fprintf(OUT, "\tgoto L%d\n", label2);
fprintf(OUT, "L%d:\n", label1);
fprintf(OUT, "\tset_imm\tI%d, 0, 0\n", temp.no);
fprintf(OUT, "L%d:\n", label2);
fprintf(OUT, "\tset\t%c%d, I%d, x\n", reg_chars[(int)reg.type], reg.no, temp.no);
return temp;
}
static m1_reg
gencode_unary(M1_compiler *comp, NOTNULL(m1_unexpr *u)) {
char *op;
int postfix = 0;
m1_reg reg,
target,
one;
switch (u->op) {
case UNOP_POSTINC:
postfix = 1;
op = "add_i";
break;
case UNOP_POSTDEC:
op = "sub_i";
postfix = 1;
break;
case UNOP_PREINC:
postfix = 0;
op = "add_i";
break;
case UNOP_PREDEC:
op = "sub_i";
postfix = 0;
break;
case UNOP_NOT:
return gencode_not(comp, u);
default:
op = "unknown op";
break;
}
/* generate code for the pre/post ++ expression */
reg = gencode_expr(comp, u->expr);
one = gen_reg(comp, VAL_INT);
target = gen_reg(comp, VAL_INT);
fprintf(OUT, "\tset_imm\tI%d, 0, 1\n", one.no);
fprintf(OUT, "\t%s\tI%d, I%d, I%d\n", op, target.no, reg.no, one.no);
if (postfix == 0) {
/* prefix; return reg containing value before adding 1 */
reg.no = target.no;
fprintf(OUT, "\tset\tI%d, %Id, x\n", reg.no, target.no);
}
else {
fprintf(OUT, "\tset\tI%d, I%d, x\n", reg.no, target.no);
}
return reg;
}
static void
gencode_break(M1_compiler *comp) {
/* get label to jump to */
int breaklabel = top(comp->breakstack);
/* pop label from compiler
's label stack (todo!) and jump there. */
fprintf(OUT, "\tgoto\tL%d\n", breaklabel);
}
static m1_reg
gencode_funcall(M1_compiler *comp, m1_funcall *f) {
m1_reg reg;
m1_reg pmcreg;
m1_reg offsetreg;
m1_symbol *fun = sym_find_chunk(&comp->currentchunk->constants, f->name);
if (fun == NULL) {
fprintf(stderr, "Cant find function '%s'\n", f->name);
++comp->errors;
return reg;
}
reg = gen_reg(comp, VAL_INT);
fprintf(OUT, "\tset_imm\tI%d, 0, %d\n", reg.no, fun->constindex);
pmcreg = gen_reg(comp, VAL_CHUNK);
offsetreg = gen_reg(comp, VAL_INT);
fprintf(OUT, "\tset_imm\tI%d, 0, %d\n", offsetreg.no, 0);
fprintf(OUT, "\tderef\tP%d, CONSTS, I%d\n", pmcreg.no, reg.no);
fprintf(OUT, "\tgoto_chunk\tP%d, I%d, x\n", pmcreg.no, offsetreg.no);
return reg;
}
static m1_reg
gencode_print(M1_compiler *comp, m1_expression *expr) {
m1_reg reg;
m1_reg one;
reg = gencode_expr(comp, expr);
one = gen_reg(comp, VAL_INT);
fprintf(OUT, "\tset_imm\tI%d, 0, 1\n", one.no);
fprintf(OUT, "\tprint_%c\tI%d, %c%d, x\n", type_chars[(int)reg.type], one.no, reg_chars[(int)reg.type], reg.no);
return reg;
}
static m1_reg
gencode_new(M1_compiler *comp, m1_newexpr *expr) {
m1_reg reg = gen_reg(comp, VAL_INT);
m1_reg sizereg = gen_reg(comp, VAL_INT);
unsigned size = 128; /* fix; should be size of object requested */
fprintf(OUT, "\tset_imm I%d, 0, %d\n", sizereg.no, size);
fprintf(OUT, "\tgc_alloc\tI%d, I%d, 0\n", reg.no, sizereg.no);
return reg;
}
static m1_reg
gencode_switch(M1_compiler *comp, m1_switch *expr) {
/*
switch (selector) {
case val1:
stat1
case val2:
stat2
case val3:
stat3
default:
stat4
}
translates to:
sel = <evaluate selector>
TEST1:
sub_i result, selector, val1
goto_if TEST2, result # if result is non-zero, then it's not case 1
<code for stat1>
TEST2:
sub_i result, selector, val2
goto_if TEST3, result
<code for stat2>
TEST3:
sub_i result, selector, val3
goto_if TEST4, result
<code for stat3>
TEST4:
<code for default>
END: #break statements will go here.
*/
m1_reg reg;
int endlabel = gen_label(comp);
m1_case *caseiter;
m1_reg test;
test = gen_reg(comp, VAL_INT);
/* evaluate selector */
reg = gencode_expr(comp, expr->selector);
push(comp->breakstack, endlabel); /* for break statements to jump to. */
caseiter = expr->cases;
while (caseiter != NULL) {
int testlabel;
fprintf(OUT, "\tsub_i\tI%d, I%d, I%d\n", test.no, reg.no, caseiter->selector);
testlabel = gen_label(comp);
fprintf(OUT, "\tgoto_if L%d, I%d\n", testlabel, test.no);
gencode_block(comp, caseiter->block);
fprintf(OUT, "L%d:\n", testlabel);
caseiter = caseiter->next;
}
if (expr->defaultstat) {
gencode_block(comp, expr->defaultstat);
}
fprintf(OUT, "L%d:\n", endlabel);
(void)pop(comp->breakstack);
return reg;
}
static void
gencode_vardecl(M1_compiler *comp, m1_var *v) {
if (v->init) {
m1_reg reg = gencode_expr(comp, v->init);
m1_symbol *s = v->sym;
/* check for first usage of this variable; may not have a reg allocated yet. */
if (s->regno == NO_REG_ALLOCATED_YET) {
m1_reg r = gen_reg(comp, s->valtype);