-
Notifications
You must be signed in to change notification settings - Fork 3
/
oztree.c
executable file
·1160 lines (942 loc) · 33.7 KB
/
oztree.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
/* oztree.c */
/*-----------------------------------------------------------------------------
* Developed by: #undef TEAMNAME
* Provides full tree implementation for the Oz assembly language.
*---------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include "ast.h"
#include "symbol.h"
#include "oztree.h"
#include "helper.h"
#include "error_printer.h"
#include "pretty.h"
#include "array_access.h"
#include "std.h"
#define PROGENTRY "main"
const char *sectionnames[] = { COMMENTSTRS };
const char *builtinnames[] = { BUILTINNAMES };
#define BOUNDS_ERROR "[FATAL]: array element out of bounds!\\n"
#define DIV_ERROR "[FATAL]: division by zero!\\n"
typedef enum {
OUT_OF_BOUNDS_LABEL, DIV_BY_ZERO_LABEL, FIRST_AVAILABLE_LABEL
} ReservedLabel;
int next_label = FIRST_AVAILABLE_LABEL;
/*-----------------------------------------------------------------------------
* Function prototypes for internal functions
* Explanations are provided with the actual implementation of each
*---------------------------------------------------------------------------*/
void gen_oz_procs(OzProgram *p, Procs *procs, void *tables);
void gen_oz_prologue(OzProgram *p, Params *params, Decls *decls, void *table);
void gen_oz_epilogue(OzProgram *p, void *table);
void gen_oz_params(OzProgram *p, Params *params, void *table);
void gen_oz_decls(OzProgram *p, Decls *decls, void *table);
void gen_oz_init_array(OzProgram *p, int slot, int reg, Bounds *bounds);
void gen_oz_out_of_bounds(OzProgram *p);
void gen_oz_div_by_zero(OzProgram *p);
void gen_oz_stmts(OzProgram *p, Stmts *stmts, void *tables, void *table);
void gen_oz_write(OzProgram *p, Expr *write, void *table);
void gen_oz_read(OzProgram *p, Expr *read, void *table);
void gen_oz_assign(OzProgram *p, Assign *assign, void *table);
void gen_oz_call(OzProgram *p, Function *call, void *tables, void *table);
void gen_oz_cond(OzProgram *p, Cond *cond, void *tables, void *table);
void gen_oz_while(OzProgram *p, While *loop, void *tables, void *table);
void gen_oz_expr(OzProgram *p, int reg, Expr *expr, void *table);
void gen_oz_expr_id(OzProgram *p, int reg, char *id, void *table);
void gen_oz_expr_const(OzProgram *p, int reg, Constant *constant);
void gen_oz_expr_array_val(OzProgram *p, int reg, Expr *a, void *table);
void gen_oz_expr_array_addr(OzProgram *p, int reg, Expr *a, void *table);
void gen_oz_expr_binop(OzProgram *p, int reg, Expr *expr, void *table);
void gen_oz_expr_binop_bool(OzProgram *p, int r1, int r2, int r3, Expr *expr);
void gen_oz_expr_binop_int(OzProgram *p, int r1, int r2, int r3, Expr *expr);
void gen_oz_expr_binop_float(OzProgram *p, int r1, int r2, int r3, Expr *expr);
void gen_oz_expr_unop(OzProgram *p, int reg, Expr *expr, void *table);
int get_reg_usage(Expr *expr, void *table);
OzLine *new_line(OzProgram *p);
OzOp *new_op(OzProgram *p);
void gen_comment(OzProgram *p, OzCommentSection section);
void gen_call(OzProgram *p, char *id);
void gen_call_builtin(OzProgram *p, OzBuiltinId id);
void gen_halt(OzProgram *p);
void gen_return(OzProgram *p);
void gen_proc_label(OzProgram *p, char *id);
void gen_label(OzProgram *p, int id);
void gen_int_const(OzProgram *p, int reg, int val);
void gen_real_const(OzProgram *p, int reg, float val);
void gen_string_const(OzProgram *p, int reg, char *val);
void gen_triop(OzProgram *p, OpCode code, int arg1, int arg2, int arg3);
void gen_binop(OzProgram *p, OpCode code, int arg1, int arg2);
void gen_unop(OzProgram *p, OpCode code, int arg1);
/*-----------------------------------------------------------------------------
* Functions from header file
*---------------------------------------------------------------------------*/
OzProgram *
gen_oz_program(Program *p, void *tables) {
OzProgram *ozprog = checked_malloc(sizeof(OzProgram));
ozprog->start = NULL;
ozprog->end = NULL;
gen_call(ozprog, PROGENTRY);
gen_halt(ozprog);
gen_oz_out_of_bounds(ozprog);
gen_oz_div_by_zero(ozprog);
gen_oz_procs(ozprog, p->procedures, tables);
return ozprog;
}
/*-----------------------------------------------------------------------------
* Convert high level Wiz stuff into Oz structures
*---------------------------------------------------------------------------*/
// Recursively generate Oz code from a Wiz Procs struct
void
gen_oz_procs(OzProgram *p, Procs *procs, void *tables) {
if (procs == NULL) {
return; // no more procs
}
Proc *proc = procs->first;
void *table = find_scope(proc->header->id, tables);
gen_proc_label(p, proc->header->id);
gen_oz_prologue(p, proc->header->params, proc->body->decls, table);
gen_oz_stmts(p, proc->body->statements, tables, table);
gen_oz_epilogue(p, table);
gen_oz_procs(p, procs->rest, tables);
}
// Generate the prologue component of a Proc
// Includes pushing stack, and creating Wiz Params and Decls
void
gen_oz_prologue(OzProgram *p, Params *params, Decls *decls, void *table) {
gen_comment(p, SECTION_PROLOGUE);
gen_unop(p, OP_PUSH_STACK_FRAME, slots_needed_for_table(table));
gen_oz_params(p, params, table);
gen_oz_decls(p, decls, table);
}
// Generate the epilogue to for a Proc (pop stack and return)
void
gen_oz_epilogue(OzProgram *p, void *table) {
gen_comment(p, SECTION_EPILOGUE);
gen_unop(p, OP_POP_STACK_FRAME, slots_needed_for_table(table));
gen_return(p);
}
// Generate Oz code from Wiz Params
void
gen_oz_params(OzProgram *p, Params *params, void *table) {
Param *param;
symbol *sym;
int count = 0;
while (params != NULL) {
param = params->first;
sym = retrieve_symbol_in_scope(param->id, (scope *)table);
gen_binop(p, OP_STORE, sym->slot, count);
count++;
params = params->rest;
}
}
// Generate Oz code from Wiz Decls
void
gen_oz_decls(OzProgram *p, Decls *decls, void *table) {
Decls *ds;
Decl *decl;
symbol *sym;
int count = 0;
int reg;
BOOL ints, reals;
ints = reals = FALSE;
int int_reg, real_reg;
// figure out which registers we need to initialise
ds = decls;
while (ds != NULL) {
decl = ds->first;
sym = retrieve_symbol_in_scope(decl->id, (scope *)table);
if (!reals && sym->type == SYM_REAL) {
reals = TRUE;
real_reg = count++;
}
else if (!ints) {
ints = TRUE;
int_reg = count++;
}
ds = ds->rest;
}
// initialise the required registers
if (ints) {
gen_int_const(p, int_reg, 0);
}
if (reals) {
gen_real_const(p, real_reg, 0.0f);
}
// initialise the variables in the stack slots
ds = decls;
while (ds != NULL) {
decl = ds->first;
sym = retrieve_symbol_in_scope(decl->id, (scope *)table);
if (sym->type == SYM_REAL) {
reg = real_reg;
} else {
reg = int_reg;
}
// if not array, just do one, otherwise initalise all stack vars
if (sym->bounds == NULL) {
gen_binop(p, OP_STORE, sym->slot, reg);
} else {
gen_oz_init_array(p, sym->slot, reg, sym->bounds);
}
ds = ds->rest;
}
}
// Generate Oz code to initialise all the values in an array
void
gen_oz_init_array(OzProgram *p, int slot, int reg, Bounds *bounds) {
int size = bounds->first->upper - bounds->first->lower + 1;
int i;
// deepest down, initialse all the frames
if (bounds->rest == NULL) {
for (i = 0; i < size; i++) {
gen_binop(p, OP_STORE, slot + i, reg);
}
}
// recursively initialise all the subarrays
else {
int offset = bounds->first->offset_size;
int next_slot;
for (i = 0; i < size; i++) {
next_slot = slot + i * offset;
gen_oz_init_array(p, next_slot, reg, bounds->rest);
}
}
}
// Label to halt the program because someone attempted to access elements
// outside the bounds of an array!
void gen_oz_out_of_bounds(OzProgram *p) {
int size = strlen(BOUNDS_ERROR) + 1;
char *msg = checked_malloc(sizeof(char) * size);
strcpy(msg, BOUNDS_ERROR);
gen_label(p, OUT_OF_BOUNDS_LABEL);
gen_string_const(p, 0, msg);
gen_call_builtin(p, BUILTIN_PRINT_STRING);
gen_halt(p);
}
// Label to halt the program because someone attempted to divide by zero
void gen_oz_div_by_zero(OzProgram *p) {
int size = strlen(DIV_ERROR) + 1;
char *msg = checked_malloc(sizeof(char) * size);
strcpy(msg, DIV_ERROR);
gen_label(p, DIV_BY_ZERO_LABEL);
gen_string_const(p, 0, msg);
gen_call_builtin(p, BUILTIN_PRINT_STRING);
gen_halt(p);
}
/*-----------------------------------------------------------------------------
* Convert Wiz statements into Oz
*---------------------------------------------------------------------------*/
// Generate Oz code from Wiz Stmts
void
gen_oz_stmts(OzProgram *p, Stmts *stmts, void *tables, void *table) {
if (stmts == NULL) {
return; //no more statements
}
Stmt *stmt = stmts->first;
// call the appropriate code generator
switch (stmt->kind) {
case STMT_WRITE:
gen_oz_write(p, stmt->info.write, table);
break;
case STMT_READ:
gen_oz_read(p, stmt->info.read, table);
break;
case STMT_ASSIGN:
gen_oz_assign(p, &(stmt->info.assign), table);
break;
case STMT_FUNC:
gen_oz_call(p, stmt->info.func, tables, table);
break;
case STMT_COND:
gen_oz_cond(p, &(stmt->info.cond), tables, table);
break;
case STMT_WHILE:
gen_oz_while(p, &(stmt->info.loop), tables, table);
break;
default:
report_error_and_exit("cannot generate for statement!");
}
gen_oz_stmts(p, stmts->rest, tables, table);
}
// Generate Oz code from Wiz Write
void
gen_oz_write(OzProgram *p, Expr *write, void *table) {
gen_comment(p, SECTION_WRITE);
gen_oz_expr(p, 0, write, table);
switch (write->inferred_type) {
case BOOL_TYPE:
gen_call_builtin(p, BUILTIN_PRINT_BOOL);
break;
case INT_TYPE:
gen_call_builtin(p, BUILTIN_PRINT_INT);
break;
case FLOAT_TYPE:
gen_call_builtin(p, BUILTIN_PRINT_REAL);
break;
case STRING_CONST:
gen_call_builtin(p, BUILTIN_PRINT_STRING);
break;
default:
report_error_and_exit("cannot infer type for write!");
}
}
// Generate Oz code from Wiz Read
void
gen_oz_read(OzProgram *p, Expr *read, void *table) {
gen_comment(p, SECTION_READ);
symbol *sym = retrieve_symbol_in_scope(read->id, table);
// Read in the appropriate value type
switch (sym->type) {
case SYM_BOOL:
gen_call_builtin(p, BUILTIN_READ_BOOL);
break;
case SYM_INT:
gen_call_builtin(p, BUILTIN_READ_INT);
break;
case SYM_REAL:
gen_call_builtin(p, BUILTIN_READ_REAL);
break;
default:
report_error_and_exit("invalid type to read!");
}
// Store the value in the appropirate place
if (read->kind == EXPR_ARRAY) {
//if array access is entirely static, store directly
Bounds *bounds = sym->bounds;
ArrayAccess *array_access = get_array_access(read, bounds);
if (array_access->dynamic_bounds == NULL) {
gen_binop(p, OP_STORE, sym->slot + array_access->static_offset, 0);
} else {
gen_oz_expr_array_addr(p, 1, read, table);
gen_binop(p, OP_STORE_INDIRECT, 1, 0);
}
} else if (sym->kind == SYM_PARAM_REF) {
gen_binop(p, OP_LOAD, 1, sym->slot);
gen_binop(p, OP_STORE_INDIRECT, 1, 0);
} else {
gen_binop(p, OP_STORE, sym->slot, 0);
}
}
// Generate Oz code from Wiz Assign
void
gen_oz_assign(OzProgram *p, Assign *assign, void *table) {
gen_comment(p, SECTION_ASSIGN);
symbol *sym = retrieve_symbol_in_scope(assign->asg_ident->id, table);
Type etype = assign->asg_expr->inferred_type;
// Evaluate the expression
gen_oz_expr(p, 0, assign->asg_expr, table);
// convert to float if needed
if (sym->type == SYM_REAL && etype == INT_TYPE) {
gen_binop(p, OP_INT_TO_REAL, 0, 0);
}
// Store the value
if (assign->asg_ident->kind == EXPR_ARRAY) {
//if array access is entirely static, store directly
Bounds *bounds = sym->bounds;
ArrayAccess *array_access = get_array_access(assign->asg_ident, bounds);
if (array_access->dynamic_bounds == NULL) {
gen_binop(p, OP_STORE, sym->slot + array_access->static_offset, 0);
} else {
gen_oz_expr_array_addr(p, 1, assign->asg_ident, table);
gen_binop(p, OP_STORE_INDIRECT, 1, 0);
}
} else if (sym->kind == SYM_PARAM_REF) {
gen_binop(p, OP_LOAD, 1, sym->slot);
gen_binop(p, OP_STORE_INDIRECT, 1, 0);
} else {
gen_binop(p, OP_STORE, sym->slot, 0);
}
}
// Generate Oz code from Wiz Call
void
gen_oz_call(OzProgram *p, Function *call, void *tables, void *table) {
gen_comment(p, SECTION_CALL);
scope *call_table = find_scope(call->id, tables);
Params *params = call_table->params;
Param *param;
symbol *arg_sym;
int reg = 0;
Exprs *args = call->args;
Expr *arg;
// Store all the args in registers
while (args != NULL) {
arg = args->first;
param = params->first;
// see if we're passing by ref or val
if (param->ind == REF_IND) {
arg_sym = retrieve_symbol_in_scope(arg->id, table);
if (arg_sym->kind == SYM_PARAM_REF) {
gen_binop(p, OP_LOAD, reg, arg_sym->slot);
} else if (arg->kind == EXPR_ARRAY) {
gen_oz_expr_array_addr(p, reg, arg, table);
} else {
gen_binop(p, OP_LOAD_ADDRESS, reg, arg_sym->slot);
}
} else {
gen_oz_expr(p, reg, arg, table);
// are we passing an int value to a float param?
if (arg->inferred_type == INT_TYPE && param->type == FLOAT_TYPE) {
gen_binop(p, OP_INT_TO_REAL, reg, reg);
}
}
// other args
reg++;
params = params->rest;
args = args->rest;
}
// call the proc
gen_call(p, call->id);
}
// Generate Oz code from Wiz Cond
void
gen_oz_cond(OzProgram *p, Cond *cond, void *tables, void *table) {
gen_comment(p, SECTION_IF);
int else_label, after_label;
BOOL else_branch = (cond->else_branch != NULL);
// set up the labels we need
if (else_branch) {
else_label = next_label++;
}
after_label = next_label++;
// Evaluate the conditional
gen_oz_expr(p, 0, cond->cond, table);
gen_binop(p, OP_BRANCH_ON_FALSE, 0, else_branch ? else_label : after_label);
gen_oz_stmts(p, cond->then_branch, tables, table); // then body
// Code for else branch, if required
if (else_branch) {
gen_unop(p, OP_BRANCH_UNCOND, after_label);
gen_label(p, else_label);
gen_oz_stmts(p, cond->else_branch, tables, table);
}
// exit jump point
gen_label(p, after_label);
}
// Generate Oz code from Wiz While
void
gen_oz_while(OzProgram *p, While *loop, void *tables, void *table) {
gen_comment(p, SECTION_WHILE);
int begin_label = next_label++;
int after_label = next_label++;
gen_label(p, begin_label); // Where the loop begins
gen_oz_expr(p, 0, loop->cond, table); // the condition to match
gen_binop(p, OP_BRANCH_ON_FALSE, 0, after_label); // exit loop if false
gen_oz_stmts(p, loop->body, tables, table); // the loop body
gen_unop(p, OP_BRANCH_UNCOND, begin_label); // restart loop
gen_label(p, after_label); // exit jump point
}
/*-----------------------------------------------------------------------------
* Convert Wiz expressions into Oz structures
*---------------------------------------------------------------------------*/
// Generate Oz code from Wiz Expr
void
gen_oz_expr(OzProgram *p, int reg, Expr *expr, void *table) {
switch (expr->kind) {
case EXPR_ID:
gen_oz_expr_id(p, reg, expr->id, table);
break;
case EXPR_CONST:
gen_oz_expr_const(p, reg, &(expr->constant));
break;
case EXPR_BINOP:
gen_oz_expr_binop(p, reg, expr, table);
break;
case EXPR_UNOP:
gen_oz_expr_unop(p, reg, expr, table);
break;
case EXPR_ARRAY:
gen_oz_expr_array_val(p, reg, expr, table);
break;
default:
report_error_and_exit("unknown expr type!");
}
}
// Generate Oz code from Wiz EXPR_ID Expr
void
gen_oz_expr_id(OzProgram *p, int reg, char *id, void *table) {
symbol *sym = retrieve_symbol_in_scope(id, table);
if (sym->kind == SYM_PARAM_REF) {
//first load address of the variable to register reg
//use regular load as the value is already an address
gen_binop(p, OP_LOAD, reg, sym->slot);
//then load indirectly using this address
gen_binop(p, OP_LOAD_INDIRECT, reg, reg);
} else {
gen_binop(p, OP_LOAD, reg, sym->slot);
}
}
// Generate Oz code from Wiz EXPR_CONST Expr
void
gen_oz_expr_const(OzProgram *p, int reg, Constant *constant) {
Value *val = &(constant->val);
switch (constant->type) {
case BOOL_TYPE:
gen_int_const(p, reg, val->bool_val);
break;
case INT_TYPE:
gen_int_const(p, reg, val->int_val);
break;
case FLOAT_TYPE:
gen_real_const(p, reg, val->float_val);
break;
case STRING_CONST:
gen_string_const(p, reg, val->string);
break;
default:
report_error_and_exit("invalid expr const type!");
}
}
// evaluate an array expr, storing value in reg
void
gen_oz_expr_array_val(OzProgram *p, int reg, Expr *a, void *table) {
//if array access is static, load directly
symbol *sym = retrieve_symbol_in_scope(a->id, table);
Bounds *bounds = sym->bounds;
ArrayAccess *array_access = get_array_access(a, bounds);
if (array_access->dynamic_bounds == NULL) {
gen_binop(p, OP_LOAD, reg, sym->slot + array_access->static_offset);
} else {
gen_oz_expr_array_addr(p, reg, a, table);
gen_binop(p, OP_LOAD_INDIRECT, reg, reg);
}
}
// store the address of an array value in a register
void
gen_oz_expr_array_addr(OzProgram *p, int reg, Expr *a, void *table) {
symbol *sym = retrieve_symbol_in_scope(a->id, table);
Bounds *bounds = sym->bounds;
ArrayAccess *array_access = get_array_access(a, bounds);
Exprs *dynamic_offsets = array_access->dynamic_offsets;
Intervals *dynamic_bounds = array_access->dynamic_bounds;
// default to static offset
gen_int_const(p, reg, array_access->static_offset);
// check if we are in static bounds, if not create jump to out of bounds
// and do no more compilation for this access
if (!array_access->is_in_static_bounds) {
gen_unop(p, OP_BRANCH_UNCOND, OUT_OF_BOUNDS_LABEL);
return;
}
// calculate the dynamic offsets we want to apply, iteratively adding
// them to the total offset, and doing dynamic bounds checking for
// each dynamic offset
while (dynamic_offsets != NULL) {
Expr *dynamic_offset = dynamic_offsets->first;
Interval *bounds = dynamic_bounds->first;
// calculate the dynamic offset:
gen_oz_expr(p, reg + 1, dynamic_offset, table);
// check that it is in bounds
// offset < min_offset
gen_int_const(p, reg + 2, bounds->lower);
gen_triop(p, OP_CMP_LT_INT, reg + 2, reg + 1, reg + 2);
gen_binop(p, OP_BRANCH_ON_TRUE, reg + 2, OUT_OF_BOUNDS_LABEL);
// offset > max_offset
gen_int_const(p, reg + 2, bounds->upper);
gen_triop(p, OP_CMP_GT_INT, reg + 2, reg + 1, reg + 2);
gen_binop(p, OP_BRANCH_ON_TRUE, reg + 2, OUT_OF_BOUNDS_LABEL);
// add to the total offset so far
gen_triop(p, OP_ADD_INT, reg, reg, reg + 1);
//advance the lists we are iterating through
dynamic_offsets = dynamic_offsets->rest;
dynamic_bounds = dynamic_bounds->rest;
}
// access the array element
gen_binop(p, OP_LOAD_ADDRESS, reg + 1, sym->slot);
gen_triop(p, OP_SUB_OFFSET, reg, reg + 1, reg);
}
/*-----------------------------------------------------------------------------
* Convert Wiz binary/unary operations into Oz structures
*---------------------------------------------------------------------------*/
// Generate Oz code from Wiz EXPR_BINOP Expr
void
gen_oz_expr_binop(OzProgram *p, int reg, Expr *expr, void *table) {
int e1type = expr->e1->inferred_type;
int e2type = expr->e2->inferred_type;
// Eval sub expressions
// evaluate the more register intensive sub-expression in reg, and the
// lower in reg+1, in order to minimise total register usage
int reg_usage_1 = get_reg_usage(expr->e1, table);
int reg_usage_2 = get_reg_usage(expr->e2, table);
int expr1_reg, expr2_reg;
if (reg_usage_1 >= reg_usage_2) {
expr1_reg = reg;
expr2_reg = reg + 1;
gen_oz_expr(p, reg, expr->e1, table);
gen_oz_expr(p, reg + 1, expr->e2, table);
} else {
expr1_reg = reg + 1;
expr2_reg = reg;
gen_oz_expr(p, reg, expr->e2, table);
gen_oz_expr(p, reg + 1, expr->e1, table);
}
// check for div by 0
if (expr->binop == BINOP_DIV) {
if (e2type == FLOAT_TYPE) {
gen_real_const(p, reg + 2, 0.0f);
gen_triop(p, OP_CMP_EQ_REAL, reg + 2, reg + 2, expr2_reg);
} else {
gen_int_const(p, reg + 2, 0);
gen_triop(p, OP_CMP_EQ_INT, reg + 2, reg + 2, expr2_reg);
}
gen_binop(p, OP_BRANCH_ON_TRUE, reg + 2, DIV_BY_ZERO_LABEL);
}
// deal with operations with both int and float
if (e1type == INT_TYPE && e2type == FLOAT_TYPE) {
gen_binop(p, OP_INT_TO_REAL, expr1_reg, expr1_reg);
} else if (e1type == FLOAT_TYPE && e2type == INT_TYPE) {
gen_binop(p, OP_INT_TO_REAL, expr2_reg, expr2_reg);
}
// generate the code
if (e1type == BOOL_TYPE && e2type == BOOL_TYPE) {
gen_oz_expr_binop_bool(p, reg, expr1_reg, expr2_reg, expr);
} else if (e1type == FLOAT_TYPE || e2type == FLOAT_TYPE) {
gen_oz_expr_binop_float(p, reg, expr1_reg, expr2_reg, expr);
} else {
gen_oz_expr_binop_int(p, reg, expr1_reg, expr2_reg, expr);
}
}
// Generate Oz code from Wiz booean binop Expr
void
gen_oz_expr_binop_bool(OzProgram *p, int r1, int r2, int r3, Expr *expr) {
switch (expr->binop) {
case BINOP_OR:
gen_triop(p, OP_OR, r1, r2, r3);
break;
case BINOP_AND:
gen_triop(p, OP_AND, r1, r2, r3);
break;
case BINOP_EQ:
gen_triop(p, OP_CMP_EQ_INT, r1, r2, r3);
break;
case BINOP_NTEQ:
gen_triop(p, OP_CMP_NE_INT, r1, r2, r3);
break;
default:
report_error_and_exit("invalid op for bool binop expr!");
}
}
// Generate Oz code from Wiz integer binop Expr
void
gen_oz_expr_binop_int(OzProgram *p, int r1, int r2, int r3, Expr *expr) {
switch (expr->binop) {
case BINOP_ADD:
gen_triop(p, OP_ADD_INT, r1, r2, r3);
break;
case BINOP_SUB:
gen_triop(p, OP_SUB_INT, r1, r2, r3);
break;
case BINOP_MUL:
gen_triop(p, OP_MUL_INT, r1, r2, r3);
break;
case BINOP_DIV:
gen_triop(p, OP_DIV_INT, r1, r2, r3);
break;
case BINOP_EQ:
gen_triop(p, OP_CMP_EQ_INT, r1, r2, r3);
break;
case BINOP_NTEQ:
gen_triop(p, OP_CMP_NE_INT, r1, r2, r3);
break;
case BINOP_LT:
gen_triop(p, OP_CMP_LT_INT, r1, r2, r3);
break;
case BINOP_LTEQ:
gen_triop(p, OP_CMP_LE_INT, r1, r2, r3);
break;
case BINOP_GT:
gen_triop(p, OP_CMP_GT_INT, r1, r2, r3);
break;
case BINOP_GTEQ:
gen_triop(p, OP_CMP_GE_INT, r1, r2, r3);
break;
default:
report_error_and_exit("invalid op for int binop expr!");
}
}
// Generate Oz code from Wiz float binop Expr
void
gen_oz_expr_binop_float(OzProgram *p, int r1, int r2, int r3, Expr *expr) {
switch (expr->binop) {
case BINOP_ADD:
gen_triop(p, OP_ADD_REAL, r1, r2, r3);
break;
case BINOP_SUB:
gen_triop(p, OP_SUB_REAL, r1, r2, r3);
break;
case BINOP_MUL:
gen_triop(p, OP_MUL_REAL, r1, r2, r3);
break;
case BINOP_DIV:
gen_triop(p, OP_DIV_REAL, r1, r2, r3);
break;
case BINOP_EQ:
gen_triop(p, OP_CMP_EQ_REAL, r1, r2, r3);
break;
case BINOP_NTEQ:
gen_triop(p, OP_CMP_NE_REAL, r1, r2, r3);
break;
case BINOP_LT:
gen_triop(p, OP_CMP_LT_REAL, r1, r2, r3);
break;
case BINOP_LTEQ:
gen_triop(p, OP_CMP_LE_REAL, r1, r2, r3);
break;
case BINOP_GT:
gen_triop(p, OP_CMP_GT_REAL, r1, r2, r3);
break;
case BINOP_GTEQ:
gen_triop(p, OP_CMP_GE_REAL, r1, r2, r3);
break;
default:
report_error_and_exit("invalid op for float binop expr!");
}
}
// Generate Oz code from Wiz EXPR_UNOP Expr
void
gen_oz_expr_unop(OzProgram *p, int reg, Expr *expr, void *table) {
Type t = expr->inferred_type;
// Eval sub expression
gen_oz_expr(p, reg, expr->e1, table);
// Do we need to worry about converting float to int?
if (t == FLOAT_TYPE && expr->e1->inferred_type == INT_TYPE) {
gen_binop(p, OP_INT_TO_REAL, reg, reg);
}
// generate the op of this expr
if (t == BOOL_TYPE && expr->unop == UNOP_NOT) {
gen_binop(p, OP_NOT, reg, reg);
}
else if (t == INT_TYPE && expr->unop == UNOP_MINUS) {
gen_int_const(p, reg + 1, 0);
gen_triop(p, OP_SUB_INT, reg, reg + 1, reg);
}
else if (t == FLOAT_TYPE && expr->unop == UNOP_MINUS) {
gen_real_const(p, reg + 1, 0.0f);
gen_triop(p, OP_SUB_REAL, reg, reg + 1, reg);
}
else {
report_error_and_exit("invalid op for unop expr!");
}
}
/*-----------------------------------------------------------------------------
Helper function for generating expression code with minimal register
usage (calculates the number of extra registes (not including the
register where result will be saved), needed to evaluate a given
expression
-----------------------------------------------------------------------------*/
int
get_reg_usage(Expr *expr, void *table) {
int reg_usage_1, reg_usage_2, min_count, max_count, reg_usage_total;
symbol *sym;
Bounds *bounds;
ArrayAccess *array_access;
Exprs *exprs;
// Switch based on expression kind
switch (expr->kind) {
case EXPR_ID:
case EXPR_CONST:
// no exra registers needed in these cases
// (no intermediates involved)
return 0;
break;
case EXPR_BINOP:
// assuming our optimization to reduce unnecessary register usage,
// we store the sub-expression with greater register usage in
// reg, and the other in reg+1, so calculate accordingly
reg_usage_1 = get_reg_usage(expr->e1, table);
reg_usage_2 = get_reg_usage(expr->e2, table);
min_count = min(reg_usage_1, reg_usage_2);
max_count = max(reg_usage_1, reg_usage_2);
reg_usage_total = max(max_count, min_count + 1);
// if the binop expression is DIV, need at least one extra
// register for comparison of RHS to zero
if (expr->binop == BINOP_DIV) {
return max(reg_usage_total, 2);
} else {
return reg_usage_total;
}
break;
case EXPR_UNOP:
// for UNOP_MINUS case, use an additional register at least to
// store the 0 for subtration
reg_usage_1 = get_reg_usage(expr->e1, table);
if (expr->unop == UNOP_MINUS) {
return max(reg_usage_1, 1);
} else {
return reg_usage_1;
}
break;
case EXPR_ARRAY:
// if array access is static do not need any extra regs
sym = retrieve_symbol_in_scope(expr->id, table);
bounds = sym->bounds;
array_access = get_array_access(expr, bounds);
exprs = array_access->dynamic_offsets;
if (exprs == NULL) {
return 0;
} else {
// otherwise need to compare to register usage of
// each index expression
reg_usage_total = 0;
while (exprs != NULL) {
// each expr requires an extra register to save its
// result, and in addition uses at least one additional
// register for bounds checking
reg_usage_1 = max(get_reg_usage(exprs->first,
table) + 1, 2);
reg_usage_total = max(reg_usage_1, reg_usage_total);
exprs = exprs->rest;
}
return reg_usage_total;
}
break;
default:
report_error_and_exit("unknown expr type!");
return 0; // never hit, but gcc complains otherwise
}
}
/*-----------------------------------------------------------------------------
* Create new Oz structures to represent code
*---------------------------------------------------------------------------*/
// Add a new line to the end of the program, and return it
OzLine *
new_line(OzProgram *p) {
OzLine *new_line = checked_malloc(sizeof(OzLine));
OzLines *lines = checked_malloc(sizeof(OzLines));
lines->first = new_line;
lines->rest = NULL;
if (p->start == NULL) {
p->start = lines;
p->end = lines;
} else {
p->end->rest = lines;
p->end = lines;
}
return new_line;
}
// Add a new (blank) OzOp to the end of the program, and return it
OzOp *