-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3ac.c
1790 lines (1731 loc) · 71.5 KB
/
3ac.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
#include "joecc_assert.h"
#include <sys/stat.h>
#include "3ac.h"
#define X(s) #s,
const char* opcode_3ac_names[] = {
OPS_3AC
"GENERIC_U", "GENERIC_I", "GENERIC_F"
};
extern const char* name_EXPRTYPE[];
extern const char* name_STMTTYPE[];
#undef X
//Construct an operation that takes 0 args
OPERATION* ct_3ac_op0(enum opcode_3ac opcode) {
OPERATION* retval = malloc(sizeof(OPERATION));
retval->opcode = opcode;
return retval;
}
//Construct an operation that takes 1 arg which is a source
OPERATION* ct_3ac_op1(enum opcode_3ac opcode, ADDRTYPE addr0_type, ADDRESS addr0) {
OPERATION* retval = malloc(sizeof(OPERATION));
retval->opcode = opcode;
retval->addr0_type = addr0_type;
retval->addr0 = addr0;
return retval;
}
//Construct an operation that takes 1 arg which is a destination
OPERATION* ct_3ac_op1_assign(enum opcode_3ac opcode, ADDRTYPE addr0_type, ADDRESS addr0) {
OPERATION* retval = malloc(sizeof(OPERATION));
retval->opcode = opcode;
retval->dest_type = addr0_type;
retval->dest = addr0;
return retval;
}
//Construct an operation that takes 2 args--one source and one destination
OPERATION* ct_3ac_op2(enum opcode_3ac opcode, ADDRTYPE addr0_type, ADDRESS addr0, ADDRTYPE dest_type, ADDRESS dest) {
OPERATION* retval = malloc(sizeof(OPERATION));
retval->opcode = opcode;
retval->addr0_type = addr0_type;
retval->addr0 = addr0;
retval->dest_type = dest_type;
retval->dest = dest;
return retval;
}
//Construct an operation that takes 3 args--two source and one destination
OPERATION* ct_3ac_op3(enum opcode_3ac opcode, ADDRTYPE addr0_type, ADDRESS addr0,
ADDRTYPE addr1_type, ADDRESS addr1, ADDRTYPE dest_type, ADDRESS dest) {
OPERATION* retval = malloc(sizeof(OPERATION));
retval->opcode = opcode;
retval->addr0_type = addr0_type;
retval->addr0 = addr0;
retval->addr1_type = addr1_type;
retval->addr1 = addr1;
retval->dest_type = dest_type;
retval->dest = dest;
return retval;
}
//Parses and creates operations from binary expression, doing implicit type casts (e.g. upgrading ints to floats
//when multiplying an int by a float) and using the proper opcode for the types (i.e. MUL_I or ADD_F)
OPERATION* implicit_binary_3(enum opcode_3ac op, EXPRESSION* cexpr, PROGRAM* prog) {
FULLADDR a1 = linearitree(daget(cexpr->params, 0), prog, 0);
FULLADDR a2 = linearitree(daget(cexpr->params, 1), prog, 0);
IDTYPE arg1id = typex(daget(cexpr->params, 0));
IDTYPE arg2id = typex(daget(cexpr->params, 1));
IDTYPE retid = typex(cexpr);
FULLADDR desta;
if(ispointer2(retid)) {
if(ispointer2(arg1id) && !ispointer2(arg2id)) {
a2 = ptarith(retid, a2, prog);
} else if(ispointer2(arg2id) && !ispointer2(arg1id)) {
a1 = ptarith(retid, a1, prog);
}
FILLREG(desta, ISPOINTER | 8);
} else if(retid.tb & FLOATNUM) {
op += 2;
if(!(arg1id.tb & FLOATNUM)) {
FULLADDR fad;
FILLREG(fad, ISFLOAT | (retid.tb & 0xf));
opn(prog, ct_3ac_op2(I2F, a1.addr_type, a1.addr, fad.addr_type, fad.addr));
a1 = fad;
} else if((arg1id.tb & 0xf) != (retid.tb & 0xf)) {
FULLADDR fad;
FILLREG(fad, ISFLOAT | (retid.tb & 0xf));
opn(prog, ct_3ac_op2(F2F, a1.addr_type, a1.addr, fad.addr_type, fad.addr));
a1 = fad;
}
if(!(arg2id.tb & FLOATNUM)) {
FULLADDR fad;
FILLREG(fad, ISFLOAT | (retid.tb & 0xf));
opn(prog, ct_3ac_op2(I2F, a2.addr_type, a2.addr, fad.addr_type, fad.addr));
a2 = fad;
} else if((arg2id.tb & 0xf) != (retid.tb & 0xf)) {
FULLADDR fad;
FILLREG(fad, ISFLOAT | (retid.tb & 0xf));
opn(prog, ct_3ac_op2(F2F, a2.addr_type, a2.addr, fad.addr_type, fad.addr));
a2 = fad;
}
FILLREG(desta, ISFLOAT | (retid.tb & 0xf));
} else if(retid.tb & UNSIGNEDNUM) {
FILLREG(desta, retid.tb & 0xf);
} else {
op += 1;
FILLREG(desta, (retid.tb & 0xf) | ISSIGNED);
}
return ct_3ac_op3(op, a1.addr_type, a1.addr, a2.addr_type, a2.addr, desta.addr_type, desta.addr);
}
//Parses and creates operations for binary bitwise operation
OPERATION* implicit_bitwise_3(enum opcode_3ac op, EXPRESSION* cexpr, PROGRAM* prog) {
FULLADDR a1 = linearitree(daget(cexpr->params, 0), prog, 0);
FULLADDR a2 = linearitree(daget(cexpr->params, 1), prog, 0);
FULLADDR desta;
IDTYPE retid = typex(cexpr);
assert(!(retid.tb & FLOATNUM));
FILLREG(desta, retid.tb & 0xf);
return ct_3ac_op3(op, a1.addr_type, a1.addr, a2.addr_type, a2.addr, desta.addr_type, desta.addr);
}
//Parses and creates operations for a compound assignment expression from the source code, performing implicit type upgrading
//And whatever other type fiddling is necessary
FULLADDR cmpnd_assign(enum opcode_3ac op, EXPRESSION* destexpr, EXPRESSION* srcexpr, PROGRAM* prog) {
IDTYPE destidt = typex(destexpr);
IDTYPE srcidt = typex(srcexpr);
FULLADDR srcaddr = linearitree(srcexpr, prog, 0);
FULLADDR destaddr = linearitree(destexpr, prog, 1);
//do some implicit binary stuff
if(ispointer2(destidt)) {
if(ispointer2(srcidt)) {
switch(op) {
case AND_U: case OR_U: case XOR_U:
break;
case SHL_U: case SHR_U: case MOD_U: case MULT_U: case DIV_U:
//pointer to integer without cast
default:
assert(0);
}
} else {
switch(op) {
case ADD_U: case SUB_U:
srcaddr = ptarith(destidt, srcaddr, prog);
break;
default:
//integer to pointer w/o cast
assert(0);
}
}
} else {
assert(!ispointer2(srcidt));
if(destidt.tb & FLOATNUM) {
op += 2;
if(!(srcidt.tb & FLOATNUM)) {
FULLADDR fad;
FILLREG(fad, ISFLOAT | (destidt.tb & 0xf));
opn(prog, ct_3ac_op2(I2F, srcaddr.addr_type, srcaddr.addr, fad.addr_type, fad.addr));
srcaddr = fad;
} else if((srcidt.tb & 0xf) != (destidt.tb & 0xf)) {
FULLADDR fad;
FILLREG(fad, ISFLOAT | (destidt.tb & 0xf));
opn(prog, ct_3ac_op2(F2F, srcaddr.addr_type, srcaddr.addr, fad.addr_type, fad.addr));
srcaddr = fad;
}
} else if(srcidt.tb & FLOATNUM) {
FULLADDR fad, fad2;
FILLREG(fad, ISFLOAT | (srcidt.tb & 0xf));
FILLREG(fad2, ISFLOAT | (srcidt.tb & 0xf));
opn(prog, ct_3ac_op2(I2F, destaddr.addr_type, destaddr.addr, fad.addr_type, fad.addr));
opn(prog, ct_3ac_op3(op, fad.addr_type, fad.addr, srcaddr.addr_type, srcaddr.addr, fad2.addr_type, fad2.addr));
opn(prog, ct_3ac_op2(F2I, fad2.addr_type, fad2.addr, destaddr.addr_type, destaddr.addr));
return destaddr;
} else {
if(!(destidt.tb & UNSIGNEDNUM && srcidt.tb & UNSIGNEDNUM))
op += 1;
}
}
opn(prog, ct_3ac_op3(op, destaddr.addr_type, destaddr.addr, srcaddr.addr_type, srcaddr.addr, destaddr.addr_type, destaddr.addr));
return destaddr;
}
//Parses and creates operations for a compound assignment expression from the source code, performing implicit type upgrading
//And whatever other type fiddling is necessary, this is the special case for add/subtract, which can accept pointer left hand sides
static FULLADDR cmpnd_assign_addsub(enum opcode_3ac op, EXPRESSION* destexpr, EXPRESSION* srcexpr, PROGRAM* prog) {
IDTYPE destidt = typex(destexpr);
IDTYPE srcidt = typex(srcexpr);
FULLADDR srcaddr = linearitree(srcexpr, prog, 0);
FULLADDR destaddr = linearitree(destexpr, prog, 1);
if(ispointer2(destidt)) {
if(!ispointer2(srcidt)) {
srcaddr = ptarith(destidt, srcaddr, prog);
}
} else {
assert(!ispointer2(srcidt));
if(destidt.tb & FLOATNUM) {
op += 1;
if(!(srcidt.tb & FLOATNUM)) {
FULLADDR fad;
FILLREG(fad, ISFLOAT | (destidt.tb & 0xf));
opn(prog, ct_3ac_op2(I2F, srcaddr.addr_type, srcaddr.addr, fad.addr_type, fad.addr));
srcaddr = fad;
} else if((srcidt.tb & 0xf) != (destidt.tb & 0xf)) {
FULLADDR fad;
FILLREG(fad, ISFLOAT | (destidt.tb & 0xf));
opn(prog, ct_3ac_op2(F2F, srcaddr.addr_type, srcaddr.addr, fad.addr_type, fad.addr));
srcaddr = fad;
}
} else if(srcidt.tb & FLOATNUM) {
FULLADDR fad, fad2;
FILLREG(fad, ISFLOAT | (srcidt.tb & 0xf));
FILLREG(fad2, ISFLOAT | (srcidt.tb & 0xf));
opn(prog, ct_3ac_op2(I2F, destaddr.addr_type, destaddr.addr, fad.addr_type, fad.addr));
opn(prog, ct_3ac_op3(op, fad.addr_type, fad.addr, srcaddr.addr_type, srcaddr.addr, fad2.addr_type, fad2.addr));
opn(prog, ct_3ac_op2(F2I, fad2.addr_type, fad2.addr, destaddr.addr_type, destaddr.addr));
return destaddr;
}
}
opn(prog, ct_3ac_op3(op, destaddr.addr_type, destaddr.addr, srcaddr.addr_type, srcaddr.addr, destaddr.addr_type, destaddr.addr));
return destaddr;
}
//shared code for both pre and post inc/dec
static inline ADDRESS stepty(EXPRESSION* cexpr) {
IDTYPE rid = typex(cexpr);
ADDRESS addr;
if(ispointer2(rid)) {
struct declarator_part* dclp = dapeek(rid.pointerstack);
assert(dclp->type != ARRAYSPEC && dclp->type != VLASPEC);
rid.pointerstack->length -= 1;
addr.uintconst_64 = lentype(&rid);
rid.pointerstack->length += 1;
} else {
addr.uintconst_64 = 1;
}
return addr;
}
//Parses and creates operations for a pre-increment expression
static FULLADDR prestep(char isinc, EXPRESSION* cexpr, PROGRAM* prog) {
FULLADDR destaddr = linearitree(daget(cexpr->params, 0), prog, 1);
char baseness = destaddr.addr_type & ISFLOAT ? 2 : 0;
opn(prog, ct_3ac_op3((isinc ? ADD_U : SUB_U) + baseness, destaddr.addr_type, destaddr.addr, ISCONST | 0x8, stepty(cexpr), destaddr.addr_type, destaddr.addr));
return destaddr;
}
//Parses and creates operations for a post-increment expression
static FULLADDR poststep(char isinc, EXPRESSION* cexpr, PROGRAM* prog) {
FULLADDR destaddr, actualaddr;
destaddr = linearitree(daget(cexpr->params, 0), prog, 1);
char baseness = destaddr.addr_type & ISFLOAT ? 2 : 0;
FILLREG(actualaddr, destaddr.addr_type & GENREGMASK);
opn(prog, ct_3ac_op2(MOV_3, destaddr.addr_type, destaddr.addr, actualaddr.addr_type, actualaddr.addr));
opn(prog, ct_3ac_op3((isinc ? ADD_U : SUB_U) + baseness, destaddr.addr_type, destaddr.addr, ISCONST | 0x8, stepty(cexpr), destaddr.addr_type, destaddr.addr));
return actualaddr;
}
//Parses and creates operations for an assignment to a pointer, including implicitly casting ints to floats and vice versa
OPERATION* implicit_mtp_2(EXPRESSION* destexpr, EXPRESSION* fromexpr, FULLADDR a1, FULLADDR a2, PROGRAM* prog) {
IDTYPE destidt = typex(destexpr);
IDTYPE srcidt = typex(fromexpr);
if(ispointer2(destidt)) {
if(!ispointer2(srcidt)) {
assert((fromexpr->type == INT || fromexpr->type == UINT) && fromexpr->intconst == 0);
}
} else if(destidt.tb & FLOATNUM) {
if(!(srcidt.tb & FLOATNUM) || (srcidt.tb & 0xf) != (destidt.tb & 0xf)) {
FULLADDR fad;
FILLREG(fad, ISFLOAT | (destidt.tb & 0xf));
opn(prog, ct_3ac_op2((srcidt.tb & FLOATNUM ? F2F : I2F), a2.addr_type, a2.addr, fad.addr_type, fad.addr));
a2 = fad;
}
} else {
if(srcidt.tb & FLOATNUM) {
FULLADDR fad;
FILLREG(fad, (destidt.tb & UNSIGNEDNUM) ? destidt.tb & 0xf : (destidt.tb & 0xf) | ISSIGNED);
opn(prog, ct_3ac_op2(F2I, a2.addr_type, a2.addr, fad.addr_type, fad.addr));
a2 = fad;
}
}
return ct_3ac_op2(MOV_3, a2.addr_type, a2.addr, a1.addr_type | ISDEREF, a1.addr);
}
//Parses and creates operations for a unary expression
OPERATION* implicit_unary_2(enum opcode_3ac op, EXPRESSION* cexpr, PROGRAM* prog) {
FULLADDR a1 = linearitree(daget(cexpr->params, 0), prog, 0);
IDTYPE arg1id = typex(daget(cexpr->params, 0));
IDTYPE retid = typex(cexpr);
FULLADDR desta;
if(ispointer2(retid)) {
FILLREG(desta, ISPOINTER | 8);
} else if(retid.tb & FLOATNUM) {
op += 2;
if(!(arg1id.tb & FLOATNUM) || (arg1id.tb & 0xf) != (retid.tb & 0xf)) {
FULLADDR fad;
FILLREG(fad, ISFLOAT | (retid.tb & 0xf));
opn(prog, ct_3ac_op2(arg1id.tb & FLOATNUM ? F2F : I2F, a1.addr_type, a1.addr, fad.addr_type, fad.addr));
a1 = fad;
}
FILLREG(desta, ISFLOAT | (retid.tb & 0xf));
} else if(retid.tb & UNSIGNEDNUM) {
FILLREG(desta, retid.tb & 0xf);
} else {
op += 1;
FILLREG(desta, (retid.tb & 0xf) | ISSIGNED);
}
return ct_3ac_op2(op, a1.addr_type, a1.addr, desta.addr_type, desta.addr);
}
//Parses and creates operations for a short circuiting operation, however one that need not return any value (as in the body of if statements)
void implicit_shortcircuit_noret(enum opcode_3ac op_to_cmp, EXPRESSION* cexpr, BBLOCK* branchto, PROGRAM* prog) {
FULLADDR addr2use;
for(int i = 0; i < cexpr->params->length; i++) {
addr2use = linearitree(daget(cexpr->params, i), prog, 0);
opn(prog, ct_3ac_op1(op_to_cmp, addr2use.addr_type, addr2use.addr));
prog->curblock->branchblock = branchto;
dapush(branchto->inedges, prog->curblock);
prog->curblock = NULL;
}
prog->curblock = dapeek(prog->allblocks);
}
//Parses and creates operations for a short circuiting operation, yielding a reg set to 1 if the condition is met, and 0 if it is not
FULLADDR implicit_shortcircuit_3(enum opcode_3ac op_to_cmp, EXPRESSION* cexpr, ADDRESS complete_val, ADDRESS shortcircuit_val, PROGRAM* prog) {
BBLOCK* failblock,* finalblock;
finalblock = mpblk();
failblock = mpblk();
FULLADDR addr2use;
for(int i = 0; i < cexpr->params->length; i++) {
addr2use = linearitree(daget(cexpr->params, i), prog, 0);
opn(prog, ct_3ac_op1(op_to_cmp, addr2use.addr_type, addr2use.addr));
prog->curblock->branchblock = failblock;
dapush(failblock->inedges, prog->curblock);
prog->curblock = NULL;
}
giveblock(prog, mpblk());
FILLREG(addr2use, 1);
opn(prog, ct_3ac_op2(MOV_3, ISCONST | 1, complete_val, 1, addr2use.addr));
prog->curblock->nextblock = finalblock;
dapushc(finalblock->inedges, prog->curblock);
giveblock(prog, failblock);
opn(prog, ct_3ac_op2(MOV_3, ISCONST | 1, shortcircuit_val, 1, addr2use.addr));
giveblock(prog, finalblock);
return addr2use;
}
//Parses and creates operations for a binary comparison expression, doing whatever casts are necessary
OPERATION* cmpret_binary_3(enum opcode_3ac op, EXPRESSION* cexpr, PROGRAM* prog) {
FULLADDR a1 = linearitree(daget(cexpr->params, 0), prog, 0);
FULLADDR a2 = linearitree(daget(cexpr->params, 1), prog, 0);
IDTYPE arg1id = typex(daget(cexpr->params, 0));
IDTYPE arg2id = typex(daget(cexpr->params, 1));
IDTYPE retid = typex(cexpr);
if(arg1id.tb & FLOATNUM) {
op += 2;
if(!(arg2id.tb & FLOATNUM)) {
FULLADDR fad;
FILLREG(fad, ISFLOAT | (arg1id.tb & 0xf));
opn(prog, ct_3ac_op2(I2F, a2.addr_type, a2.addr, fad.addr_type, fad.addr));
a2 = fad;
} else if((arg1id.tb & 0xf) > (arg2id.tb & 0xf)) {
FULLADDR fad;
FILLREG(fad, ISFLOAT | (arg1id.tb & 0xf));
opn(prog, ct_3ac_op2(F2F, a2.addr_type, a2.addr, fad.addr_type, fad.addr));
a2 = fad;
} else if((arg1id.tb & 0xf) < (arg2id.tb & 0xf)) {
FULLADDR fad;
FILLREG(fad, ISFLOAT | (arg2id.tb & 0xf));
opn(prog, ct_3ac_op2(F2F, a1.addr_type, a1.addr, fad.addr_type, fad.addr));
a1 = fad;
}
} else if(arg2id.tb & FLOATNUM) {
op += 2;
FULLADDR fad;
FILLREG(fad, ISFLOAT | (arg2id.tb & 0xf));
opn(prog, ct_3ac_op2(I2F, a1.addr_type, a1.addr, fad.addr_type, fad.addr));
a2 = fad;
} else if(!((arg1id.tb & UNSIGNEDNUM) || (arg2id.tb & UNSIGNEDNUM))) {
op += 1;
}
FULLADDR desta;
FILLREG(desta, (retid.tb & 0xf) | (retid.tb & UNSIGNEDNUM ? 0 : ISSIGNED));//unsigned
return ct_3ac_op3(op, a1.addr_type, a1.addr, a2.addr_type, a2.addr, desta.addr_type, desta.addr);
}
//Parses and creates operations for a binary bit shift operation
OPERATION* binshift_3(enum opcode_3ac opcode_unsigned, EXPRESSION* cexpr, PROGRAM* prog) {
FULLADDR a1 = linearitree(daget(cexpr->params, 0), prog, 0);
FULLADDR a2 = linearitree(daget(cexpr->params, 1), prog, 0);
assert(!(a1.addr_type & ISFLOAT) && !(a2.addr_type & ISFLOAT));
enum opcode_3ac shlop = opcode_unsigned + (a1.addr_type & ISSIGNED ? 1 : 0);
FULLADDR adr;
FILLREG(adr, a1.addr_type & GENREGMASK);
return ct_3ac_op3(shlop, a1.addr_type, a1.addr, a2.addr_type, a2.addr, adr.addr_type, adr.addr);
}
//Parses and creates operations for the accessing of a struct/union field, performing necessary casts
FULLADDR smemrec(EXPRESSION* cexpr, PROGRAM* prog, char islvalue) {
FULLADDR sead = linearitree(daget(cexpr->params, 0), prog, 0);
IDTYPE seaty = typex(daget(cexpr->params, 0));
IDTYPE retty = typex(cexpr);
assert(((EXPRESSION*) daget(cexpr->params, 1))->type == MEMBER);
char* memname = ((EXPRESSION*) daget(cexpr->params, 1))->member;
assert(!seaty.pointerstack || seaty.pointerstack->length <= 1);
assert(seaty.tb & (STRUCTVAL | UNIONVAL));
if(seaty.tb & STRUCTVAL) {
feedstruct(seaty.structtype);
} else {
unionlen(seaty.uniontype);
}
FULLADDR retaddr;
ADDRESS offaddr;
STRUCTFIELD* sf = qsearch(seaty.structtype->offsets, memname);
assert(!(sf->offset & 0x7));
char pointerqual = ispointer(sf->type);
offaddr.intconst_64 = sf->offset >> 3;
struct declarator_part* dclp = NULL;
if(sf->type->pointerstack && sf->type->pointerstack->length)
dclp = dapeek(sf->type->pointerstack);
if(!pointerqual && (sf->type->tb & (STRUCTVAL | UNIONVAL))) {
//handle here
if(dclp && dclp->type == BITFIELDSPEC) {
if(islvalue) {
//there is a big issue with lvalues, currently we don't treat rvalues and lvalues much differently until we actually assign
//however with bitfields, the bitwise ops are entirely different and we don't necessarily have that information
//when we enter into a function. This means that we will probably need to handle bitfield lvalues in a complex
//and unique way, unfortunately.
assert(0);
} else {
int bitlen = dclp->bfspec->intconst;
int bitoffset = sf->offset & 0x7;
int bitlencontainer = (((bitlen + (bitoffset)) & (-(bitlen + (bitoffset)) - 1))/8) << 1;
assert(bitlencontainer <= 64);
ADDRESS mask;
ADDRESS byteoffset;
mask.intconst_64 = ((0xffffffffffffffff << (bitlencontainer - bitlen)) >> (bitlencontainer - bitlen)) << (sf->offset & 7);
byteoffset.intconst_64 = sf->offset >> 3;
FULLADDR structoffaddr;
FULLADDR retval;
FILLREG(structoffaddr, ISPOINTER | 0x8);
FILLREG(retval, addrconv(&retty));
opn(prog, ct_3ac_op3(ADD_U, sead.addr_type, sead.addr, ISCONST | 0x8, byteoffset, structoffaddr.addr_type, structoffaddr.addr));
opn(prog, ct_3ac_op3(AND_U, structoffaddr.addr_type, structoffaddr.addr, ISCONST | 0x8, mask, retval.addr_type, retval.addr));
opn(prog, ct_3ac_op3(SHR_U, retval.addr_type, structoffaddr.addr, ISCONST | 0x8, mask, retval.addr_type, retval.addr));
return retval;
}
} else {
assert((sf->offset & 0x7) == 0);
if(offaddr.intconst_64) {
FILLREG(retaddr, ISPOINTER | 8);
opn(prog, ct_3ac_op3(ADD_U, sead.addr_type, sead.addr, ISCONST | 0x8, offaddr, retaddr.addr_type, retaddr.addr));
} else {
if(sead.addr_type & ISDEREF) {
FILLREG(retaddr, ISPOINTER | 8);
opn(prog, ct_3ac_op2(MOV_3, sead.addr_type, sead.addr, retaddr.addr_type, retaddr.addr));
} else {
return sead;
}
}
}
} else {
assert(!dclp || dclp->type != BITFIELDSPEC);
FULLADDR intermediate;
if(offaddr.intconst_64) {
FILLREG(intermediate, ISPOINTER | 8);
opn(prog, ct_3ac_op3(ADD_U, sead.addr_type, sead.addr, ISCONST | 0x8, offaddr, intermediate.addr_type, intermediate.addr));
} else {
if(sead.addr_type & ISDEREF) {
FILLREG(intermediate, ISPOINTER | 8);
opn(prog, ct_3ac_op2(MOV_3, sead.addr_type, sead.addr, intermediate.addr_type, intermediate.addr));
} else {
sead.addr_type |= ISDEREF;
return sead;
}
}
intermediate.addr_type = addrconv(&retty) | ISDEREF;
return intermediate; //probably nothing different needs to be done with pointer or anything
}
return retaddr;
}
//Returns an address containing a register which holds in it the size of the VLA of the given type
static FULLADDR execvla(IDTYPE* idt, PROGRAM* prog) {
FULLADDR curaddr, otheraddr, scratchaddr;
struct declarator_part* dclp = dapeek(idt->pointerstack);
curaddr = linearitree(dclp->vlaent, prog, 0);
struct declarator_part* subdclp;
int psentry;
for(psentry = idt->pointerstack->length - 1; psentry >= 0 && (subdclp = daget(idt->pointerstack, psentry))->type == VLASPEC; psentry--) {
FILLREG(otheraddr, curaddr.addr_type & GENREGMASK);
scratchaddr = linearitree(subdclp->vlaent, prog, 0);
opn(prog, ct_3ac_op3(MULT_U, curaddr.addr_type, curaddr.addr, scratchaddr.addr_type, scratchaddr.addr, otheraddr.addr_type, otheraddr.addr));
curaddr = otheraddr;
}
if(psentry < 0) psentry = 0;
int lenstore = idt->pointerstack->length;
idt->pointerstack->length = psentry;
scratchaddr.addr.uintconst_64 = lentype(idt);
idt->pointerstack->length = lenstore;
FILLREG(otheraddr, curaddr.addr_type & GENREGMASK);
opn(prog, ct_3ac_op3(MULT_U, curaddr.addr_type, curaddr.addr, ISCONST | 0x8, scratchaddr.addr, otheraddr.addr_type, otheraddr.addr));
dclp->addrun = otheraddr.addr.garbage; //may we need the non-top level pointerstack entries to be correct?
dclp->addrty = otheraddr.addr_type;
return otheraddr;
}
//Recursively parses and creates operations for an arbitrary expression
FULLADDR linearitree(EXPRESSION* cexpr, PROGRAM* prog, char islvalue) {
FULLADDR curaddr, otheraddr, destaddr;
IDTYPE varty;
OPERATION* genop;
switch(cexpr->type){
//for these literal cases we don't need to do much
case STRING:
curaddr.addr_type = ISCONST | ISSTRCONST | ISPOINTER | 0x8;
curaddr.addr.strconst = cexpr->strconst;
return curaddr;
case INT:
curaddr.addr_type = ISCONST | ISSIGNED | 0x8;
curaddr.addr.intconst_64 = cexpr->intconst;
return curaddr;
case UINT:
curaddr.addr_type = ISCONST | 0x8;
curaddr.addr.uintconst_64 = cexpr->uintconst;
return curaddr;
case FLOAT:
curaddr.addr_type = ISCONST | ISFLOAT | ISSIGNED | 0x8;
curaddr.addr.floatconst_64 = cexpr->floatconst;
return curaddr;
//in the case of an identifier, get some information about it based on scope
case IDENT:
if(cexpr->id->index < 0) {
//if it's a global variable
if(cexpr->id->index == -2) {
curaddr.addr_type = 8 | ISLABEL;
} else {
curaddr.addr_type = addrconv(cexpr->id->type) | ISLABEL;
}
curaddr.addr.labelname = cexpr->id->name;
return curaddr;
} else {
return *(FULLADDR*) daget(prog->dynvars, cexpr->id->index);
}
//Array literals can also serve to initialize VLAs (TODO: check), so more work must be done for them
case ARRAY_LIT:
FILLREG(destaddr, ISPOINTER | 0x8);
struct declarator_part* ptrtop = dapeek(cexpr->rettype->pointerstack);
--cexpr->rettype->pointerstack->length;
ADDRTYPE memtype = addrconv(cexpr->rettype);
++cexpr->rettype->pointerstack->length;
assert(ptrtop->type == ARRAYSPEC);
//vlas may not be initialized
curaddr.addr.uintconst_64 = ptrtop->arrlen;
opn(prog, ct_3ac_op2(ALOC_3, ISCONST | 0x8, curaddr.addr, destaddr.addr_type, destaddr.addr));
curaddr.addr.uintconst_64 = ptrtop->arrlen / ptrtop->arrmaxind;
if(curaddr.addr.uintconst_64 < 0xf) {
for(int i = 0; i < cexpr->params->length; i++) {
EXPRESSION* dyne = daget(cexpr->params, i);
otheraddr = linearitree(dyne, prog, 0);
curaddr.addr.uintconst_64 = i;
if(memtype & ISFLOAT && !(otheraddr.addr_type & ISFLOAT)) {
FULLADDR fad2;
FILLREG(fad2, memtype);
opn(prog, ct_3ac_op2(I2F, otheraddr.addr_type, otheraddr.addr, fad2.addr_type, fad2.addr));
otheraddr = fad2;
} else if(!(memtype & ISFLOAT) && otheraddr.addr_type & ISFLOAT) {
FULLADDR fad2;
FILLREG(fad2, memtype);
opn(prog, ct_3ac_op2(F2I, otheraddr.addr_type, otheraddr.addr, fad2.addr_type, fad2.addr));
otheraddr = fad2;
} else if(memtype & ISFLOAT && (otheraddr.addr_type & ISFLOAT) && (memtype & 0xf) != (otheraddr.addr_type & 0xf)) {
FULLADDR fad2;
FILLREG(fad2, memtype);
opn(prog, ct_3ac_op2(F2F, otheraddr.addr_type, otheraddr.addr, fad2.addr_type, fad2.addr));
otheraddr = fad2;
}
opn(prog, ct_3ac_op3(ARRMOV, otheraddr.addr_type, otheraddr.addr, ISCONST | 0x8, curaddr.addr, destaddr.addr_type | ISDEREF, destaddr.addr));
}
} else {
ADDRESS a;
a.uintconst_64 = curaddr.addr.uintconst_64;
for(int i = 0; i < cexpr->params->length; i++) {
EXPRESSION* dyne = daget(cexpr->params, i);
otheraddr = linearitree(dyne, prog, 0);
opn(prog, ct_3ac_op3(ADD_U, destaddr.addr_type, destaddr.addr, ISCONST | 0x8, a, destaddr.addr_type, destaddr.addr));
if(destaddr.addr_type & ISFLOAT && !(otheraddr.addr_type & ISFLOAT)) {
FULLADDR fad2;
FILLREG(fad2, memtype);
opn(prog, ct_3ac_op2(I2F, otheraddr.addr_type, otheraddr.addr, fad2.addr_type, fad2.addr));
otheraddr = fad2;
} else if(!(destaddr.addr_type & ISFLOAT) && otheraddr.addr_type & ISFLOAT) {
FULLADDR fad2;
FILLREG(fad2, memtype);
opn(prog, ct_3ac_op2(F2I, otheraddr.addr_type, otheraddr.addr, fad2.addr_type, fad2.addr));
otheraddr = fad2;
} else if(destaddr.addr_type & ISFLOAT && (otheraddr.addr_type & ISFLOAT) && (destaddr.addr_type & 0xf) != (otheraddr.addr_type & 0xf)) {
FULLADDR fad2;
FILLREG(fad2, memtype);
opn(prog, ct_3ac_op2(F2F, otheraddr.addr_type, otheraddr.addr, fad2.addr_type, fad2.addr));
otheraddr = fad2;
}
opn(prog, ct_3ac_op2(MOV_3, otheraddr.addr_type, otheraddr.addr, destaddr.addr_type | ISDEREF, destaddr.addr));
}
}
return destaddr;
//process a struct literal including its initializing members
case STRUCT_LIT:
FILLREG(destaddr, ISPOINTER | 0x8);
curaddr.addr.uintconst_64 = cexpr->rettype->structtype->size;
opn(prog, ct_3ac_op2(ALOC_3, ISCONST | 0x8, curaddr.addr, destaddr.addr_type, destaddr.addr));
for(int i = 0; i < cexpr->params->length; i++) {
EXPRESSION* member = daget(cexpr->params, i);
DECLARATION* decl = daget(cexpr->rettype->structtype->fields, i);
STRUCTFIELD* sf = qsearch(cexpr->rettype->structtype->offsets, decl->varname);
if(sf->offset & 0x7) {
assert(!(curaddr.addr_type & ISFLOAT));
otheraddr.addr.uintconst_64 = sf->offset >> 3;
struct declarator_part* bfspec = dapeek(sf->type->pointerstack);
int bitlen = bfspec->bfspec->intconst;
int bitlencontainer = (((bitlen + (sf->offset & 7)) & (-(bitlen + (sf->offset & 7)) - 1))/8) << 1;
if(bitlencontainer < 1) bitlencontainer = 1;
assert(bitlencontainer <= 64); //TODO: not do this
curaddr = linearitree(member, prog, 0);
otheraddr.addr.uintconst_64 = sf->offset >> 3;
ADDRTYPE sft = addrconv(sf->type);
if(sft & ISFLOAT && !(curaddr.addr_type & ISFLOAT)) {
FULLADDR fad2;
FILLREG(fad2, sft);
opn(prog, ct_3ac_op2(I2F, curaddr.addr_type, curaddr.addr, fad2.addr_type, fad2.addr));
curaddr = fad2;
}
FULLADDR fad2;
FILLREG(fad2, ISPOINTER | 0x8);
opn(prog, ct_3ac_op3(ADD_U, destaddr.addr_type, destaddr.addr, otheraddr.addr_type, otheraddr.addr, fad2.addr_type, fad2.addr));
FULLADDR bringdown;
FILLREG(bringdown, UNSIGNEDNUM | bitlencontainer);
opn(prog, ct_3ac_op2(MOV_3, fad2.addr_type | ISDEREF, fad2.addr, bringdown.addr_type, bringdown.addr));
int mask = ((0xffffffffffffffff << (bitlencontainer - bitlen)) >> (bitlencontainer - bitlen)) << (sf->offset & 7);
ADDRESS adr;
adr.intconst_64 = mask;
opn(prog, ct_3ac_op3(AND_U, curaddr.addr_type, curaddr.addr, ISCONST | bitlencontainer, adr, curaddr.addr_type, curaddr.addr));
opn(prog, ct_3ac_op3(OR_U, curaddr.addr_type, curaddr.addr, bringdown.addr_type, bringdown.addr, fad2.addr_type | ISDEREF, fad2.addr));
} else {
curaddr = linearitree(member, prog, 0);
otheraddr.addr.uintconst_64 = sf->offset >> 3;
ADDRTYPE sft = addrconv(sf->type);
if(sft & ISFLOAT && !(curaddr.addr_type & ISFLOAT)) {
FULLADDR fad2;
FILLREG(fad2, sft);
opn(prog, ct_3ac_op2(I2F, curaddr.addr_type, curaddr.addr, fad2.addr_type, fad2.addr));
curaddr = fad2;
} else if(!(sft & ISFLOAT) && curaddr.addr_type & ISFLOAT) {
FULLADDR fad2;
FILLREG(fad2, sft);
opn(prog, ct_3ac_op2(F2I, curaddr.addr_type, curaddr.addr, fad2.addr_type, fad2.addr));
curaddr = fad2;
} else if(sft & ISFLOAT && curaddr.addr_type & ISFLOAT &&
((sf->type->tb & 0xf) != (otheraddr.addr_type & 0xf))) {
FULLADDR fad2;
FILLREG(fad2, sft);
opn(prog, ct_3ac_op2(F2F, otheraddr.addr_type, otheraddr.addr, fad2.addr_type, fad2.addr));
otheraddr = fad2;
}
opn(prog, ct_3ac_op3(MTP_OFF, curaddr.addr_type, curaddr.addr, ISCONST | 0x8, otheraddr.addr, destaddr.addr_type | ISDEREF, destaddr.addr));
}
}
return destaddr;
//for simple unary operations we just need to use the output of the operand easily
case NEG:
curaddr = linearitree(daget(cexpr->params, 0), prog, 0);
FILLREG(destaddr, curaddr.addr_type & GENREGMASK);
opn(prog, ct_3ac_op2(destaddr.addr_type & ISFLOAT ? NEG_F : NEG_I, curaddr.addr_type, curaddr.addr, destaddr.addr_type, destaddr.addr));
return destaddr;
case L_NOT:
curaddr = linearitree(daget(cexpr->params, 0), prog, 0);
assert(!(curaddr.addr_type & ISFLOAT));
FILLREG(destaddr, (curaddr.addr_type & ISSIGNED) | 1);
otheraddr.addr.uintconst_64 = 0;
opn(prog, ct_3ac_op3(EQ_U, curaddr.addr_type, curaddr.addr, (curaddr.addr_type & 0xf) | ISCONST, otheraddr.addr,
destaddr.addr_type, destaddr.addr));
return destaddr;
case B_NOT:
curaddr = linearitree(daget(cexpr->params, 0), prog, 0);
FILLREG(destaddr, curaddr.addr_type & GENREGMASK);
assert(!(destaddr.addr_type & ISFLOAT));
opn(prog, ct_3ac_op2(NOT_U, curaddr.addr_type, curaddr.addr, destaddr.addr_type, destaddr.addr));
return destaddr;
case ADDR:
varty = typex(daget(cexpr->params, 0));
char hpoints = ispointer2(varty);
if(!(hpoints) && (varty.tb & STRUCTVAL)) {
return linearitree(daget(cexpr->params, 0), prog, 0);//addr should be a no-op for single pointers to structs
}
if(hpoints) {
struct declarator_part* dclp = dapeek(varty.pointerstack);
if(dclp->type == ARRAYSPEC || dclp->type == VLASPEC)
return linearitree(daget(cexpr->params, 0), prog, 0);//addr should be a no-op for single pointers to arrays
}
return op2ret(prog, implicit_unary_2(ADDR_3, cexpr, prog));
case DEREF:
varty = typex(daget(cexpr->params, 0));
assert(ispointer2(varty));
destaddr = linearitree(daget(cexpr->params, 0), prog, 0);
if(varty.pointerstack->length == 1 && (varty.tb & STRUCTVAL)) {
return destaddr; //dereferencing single pointer to struct should be a no-op
}
struct declarator_part* dclp = dapeek(varty.pointerstack);
if(dclp->type == ARRAYSPEC || dclp->type == VLASPEC) {
varty = typex(cexpr);
if(ispointer2(varty)) {
struct declarator_part* dclp2 = dapeek(varty.pointerstack);
if(dclp2->type != ARRAYSPEC || dclp2->type == VLASPEC) {
goto REALDEREF;
}
} else {
goto REALDEREF;
}
} else {
REALDEREF:
if(destaddr.addr_type & ISDEREF) {
FILLREG(otheraddr, destaddr.addr_type & GENREGMASK);
opn(prog, ct_3ac_op2(MOV_3, destaddr.addr_type, destaddr.addr, otheraddr.addr_type, otheraddr.addr));
destaddr = otheraddr;
}
destaddr.addr_type |= ISDEREF;
}
return destaddr;
case ADD:
genop = implicit_binary_3(GENERIC_U, cexpr, prog);
DEGENERIC(genop, ADD);
return op2ret(prog, genop);
case SUB:
genop = implicit_binary_3(GENERIC_U, cexpr, prog);
DEGENERIC(genop, SUB);
return op2ret(prog, genop);
case MULT:
return op2ret(prog, implicit_binary_3(MULT_U, cexpr, prog));
case DIVI:
return op2ret(prog, implicit_binary_3(DIV_U, cexpr, prog));
case EQ:
genop = cmpret_binary_3(GENERIC_U, cexpr, prog);
DEGENERIC(genop, EQ);
return op2ret(prog, genop);
case NEQ:
genop = cmpret_binary_3(GENERIC_U, cexpr, prog);
DEGENERIC(genop, NE);
return op2ret(prog, genop);
case GT:
return op2ret(prog, cmpret_binary_3(GT_U, cexpr, prog));
case LT:
return op2ret(prog, cmpret_binary_3(LT_U, cexpr, prog));
case GTE:
return op2ret(prog, cmpret_binary_3(GE_U, cexpr, prog));
case LTE:
return op2ret(prog, cmpret_binary_3(LE_U, cexpr, prog));
case MOD:
varty = typex(daget(cexpr->params, 0));
assert(!(varty.tb & FLOATNUM));
return op2ret(prog, cmpret_binary_3(MOD_U, cexpr, prog));
case L_AND:
return implicit_shortcircuit_3(BEZ_3, cexpr, (ADDRESS) 1ul, (ADDRESS) 0ul, prog);
case L_OR:
return implicit_shortcircuit_3(BNZ_3, cexpr, (ADDRESS) 0ul, (ADDRESS) 1ul, prog);
case B_AND:
return op2ret(prog, implicit_bitwise_3(AND_U, cexpr, prog));
case B_OR:
return op2ret(prog, implicit_bitwise_3(OR_U, cexpr, prog));
case B_XOR:
return op2ret(prog, implicit_bitwise_3(XOR_U, cexpr, prog));
case SHL:
return op2ret(prog, binshift_3(SHL_U, cexpr, prog));
case SHR:
return op2ret(prog, binshift_3(SHR_U, cexpr, prog));
case COMMA:
for(int i = 0; i < cexpr->params->length - 1; i++) {
linearitree(daget(cexpr->params, i), prog, 0);
}
return linearitree(daget(cexpr->params, cexpr->params->length - 1), prog, 0);
case DOTOP:
varty = typex(daget(cexpr->params, 0));
assert(!ispointer2(varty));
return smemrec(cexpr, prog, islvalue);
case ARROW:
varty = typex(daget(cexpr->params, 0));
assert(varty.pointerstack && (varty.pointerstack->length == 1));
return smemrec(cexpr, prog, islvalue);
case SZOFEXPR:
varty = typex(cexpr);
destaddr.addr.uintconst_64 = lentype(&varty);
if(destaddr.addr.uintconst_64 == (unsigned long) -1) {
struct declarator_part* dclp = dapeek(varty.pointerstack);
destaddr.addr.garbage = dclp->addrun;
destaddr.addr_type = dclp->addrty;
linearitree(daget(cexpr->params, 0), prog, 0);
} else {
destaddr.addr_type = ISCONST | 0x8;
}
return destaddr;
case CAST: //handle identity casts differently
curaddr = linearitree(daget(cexpr->params, 0), prog, islvalue);
if(ispointer(cexpr->vartype)) {
assert(!(curaddr.addr_type & ISFLOAT));
FILLREG(destaddr, 8 | ISPOINTER);
opn(prog, ct_3ac_op2(MOV_3, curaddr.addr_type, curaddr.addr, destaddr.addr_type, destaddr.addr));
} else if(cexpr->vartype->tb & FLOATNUM) {
FILLREG(destaddr, (cexpr->vartype->tb & 0xf) | ISSIGNED | ISFLOAT);
opn(prog, ct_3ac_op2(curaddr.addr_type & ISFLOAT ? MOV_3 : I2F, curaddr.addr_type, curaddr.addr, destaddr.addr_type, destaddr.addr));
} else if(cexpr->vartype->tb & UNSIGNEDNUM) {
FILLREG(destaddr, cexpr->vartype->tb & 0xf);
opn(prog, ct_3ac_op2(curaddr.addr_type & ISFLOAT ? F2I : MOV_3, curaddr.addr_type, curaddr.addr, destaddr.addr_type, destaddr.addr));
} else if(cexpr->vartype->tb & VOIDNUM) {
return curaddr; //not sure how this should be handled
} else if(cexpr->vartype->tb & UNIONVAL) {
USTRUCT* castdest = cexpr->vartype->uniontype;
FILLREG(destaddr, ISPOINTER | 0x8);
unionlen(castdest);
otheraddr.addr.uintconst_64 = castdest->size;
opn(prog, ct_3ac_op2(ALOC_3, ISCONST | 8, otheraddr.addr, destaddr.addr_type, destaddr.addr));
IDTYPE srctype = typex(daget(cexpr->params, 0));
for(int i = 0; i < castdest->fields->length; i++) {
DECLARATION* dcl = daget(castdest->fields, i);
if(!typecompat(dcl->type, &srctype)) continue;
opn(prog, ct_3ac_op2(MOV_3, curaddr.addr_type, curaddr.addr, destaddr.addr_type | ISDEREF, destaddr.addr));
return destaddr;
}
assert(0);
} else if(cexpr->vartype->tb & 0xf) {
FILLREG(destaddr, (cexpr->vartype->tb & 0xf) | ISSIGNED);
opn(prog, ct_3ac_op2(curaddr.addr_type & ISFLOAT ? F2I : MOV_3, curaddr.addr_type, curaddr.addr, destaddr.addr_type, destaddr.addr));
} else {
//don't support casting structs yet
assert(0);
}
return destaddr;
case TERNARY: ;
BBLOCK* joinblock = mpblk();
BBLOCK* succblock = mpblk();
BBLOCK* failblock = mpblk();
cmptype(daget(cexpr->params, 0), failblock, succblock, prog);
BBLOCK* topblock;
prog->curblock = NULL;
IDTYPE t0t = typex(daget(cexpr->params, 0));
IDTYPE t1t = typex(daget(cexpr->params, 1));
IDTYPE t2t = typex(daget(cexpr->params, 2));
IDTYPE t3t = typex(cexpr);
OPERATION* join = malloc(sizeof(OPERATION));
join->opcode = PHI;
join->addr0_type = ISCONST | GARBAGEVAL;//The GARBAGEVAL here singifies it's a ternary phi
join->addr0.joins = malloc(2 * sizeof(FULLADDR));
giveblock(prog, succblock);
FILLREG(destaddr, addrconv(&t3t));
join->dest = destaddr.addr;
join->dest_type = destaddr.addr_type;
curaddr = linearitree(daget(cexpr->params, 1), prog, 0);
if(!(t1t.tb & FLOATNUM) && (t2t.tb & FLOATNUM)) {
FULLADDR ad2;
FILLREG(ad2, (t0t.tb & 0xf) | ISFLOAT | ISSIGNED);
opn(prog, ct_3ac_op2(I2F, curaddr.addr_type, curaddr.addr, ad2.addr_type, ad2.addr));
curaddr = ad2;
}
if((t1t.tb & FLOATNUM) && ((destaddr.addr_type & 0xf) != (t1t.tb & 0xf))) {
FULLADDR fad2;
FILLREG(fad2, ISFLOAT | (destaddr.addr_type & 0xf));
opn(prog, ct_3ac_op2(F2F, curaddr.addr_type, curaddr.addr, fad2.addr_type, fad2.addr));
curaddr = fad2;
}
topblock = dapeek(prog->allblocks);
if(!topblock->nextblock) {
topblock->nextblock = joinblock;
dapushc(joinblock->inedges, topblock);
}
FILLREG(otheraddr, addrconv(&t3t));
join->addr0.joins[0] = otheraddr;
opn(prog, ct_3ac_op2(MOV_3, curaddr.addr_type, curaddr.addr, otheraddr.addr_type, otheraddr.addr));
giveblock(prog, failblock);
otheraddr = linearitree(daget(cexpr->params, 2), prog, 0);
if((t1t.tb & FLOATNUM) && !(t2t.tb & FLOATNUM)) {
FULLADDR ad2;
FILLREG(ad2, (t0t.tb & 0xf) | ISFLOAT | ISSIGNED);
opn(prog, ct_3ac_op2(I2F, otheraddr.addr_type, otheraddr.addr, ad2.addr_type, ad2.addr));
otheraddr = ad2;
}
if((t2t.tb & FLOATNUM) && ((destaddr.addr_type & 0xf) != (t2t.tb & 0xf))) {
FULLADDR fad2;
FILLREG(fad2, ISFLOAT | (destaddr.addr_type & 0xf));
opn(prog, ct_3ac_op2(F2F, otheraddr.addr_type, otheraddr.addr, fad2.addr_type, fad2.addr));
otheraddr = fad2;
}
assert((curaddr.addr_type & ISFLOAT) == (otheraddr.addr_type & ISFLOAT)); //confirm 2 addrs have same type or are coercible
FILLREG(curaddr, addrconv(&t3t));
join->addr0.joins[1] = curaddr;
opn(prog, ct_3ac_op2(MOV_3, otheraddr.addr_type, otheraddr.addr, curaddr.addr_type, curaddr.addr));
giveblock(prog, joinblock);
opn(prog, join);
return destaddr;
case ASSIGN:
varty = typex(cexpr);
curaddr = linearitree(daget(cexpr->params, 1), prog, 0);
destaddr = linearitree(daget(cexpr->params, 0), prog, 1);
if(!ispointer2(varty) && varty.tb & (STRUCTVAL | UNIONVAL)) {
feedstruct(varty.structtype);
otheraddr.addr.uintconst_64 = varty.structtype->size;
opn(prog, ct_3ac_op3(COPY_3, curaddr.addr_type | ISDEREF, curaddr.addr, ISCONST | 8, otheraddr.addr, destaddr.addr_type | ISDEREF, destaddr.addr));
} else {
if(destaddr.addr_type & ISDEREF) {
opn(prog, implicit_mtp_2(daget(cexpr->params, 0), daget(cexpr->params, 1), destaddr, curaddr, prog));
} else {
//implicit type coercion needed
opn(prog, ct_3ac_op2(MOV_3, curaddr.addr_type, curaddr.addr, destaddr.addr_type, destaddr.addr));
}
}
return destaddr;
case PREINC:
return prestep(1, cexpr, prog);
case PREDEC:
return prestep(0, cexpr, prog);
case POSTINC:
return poststep(1, cexpr, prog);
case POSTDEC:
return poststep(0, cexpr, prog);
case ADDASSIGN:
return cmpnd_assign_addsub(ADD_U, daget(cexpr->params, 0), daget(cexpr->params, 1), prog);
case SUBASSIGN:
return cmpnd_assign_addsub(SUB_U, daget(cexpr->params, 0), daget(cexpr->params, 1), prog);
case DIVASSIGN:
return cmpnd_assign(DIV_U, daget(cexpr->params, 0), daget(cexpr->params, 1), prog);
case SHLASSIGN:
return cmpnd_assign(SHL_U, daget(cexpr->params, 0), daget(cexpr->params, 1), prog);
case SHRASSIGN:
return cmpnd_assign(SHR_U, daget(cexpr->params, 0), daget(cexpr->params, 1), prog);
case ANDASSIGN:
return cmpnd_assign(AND_U, daget(cexpr->params, 0), daget(cexpr->params, 1), prog);
case XORASSIGN:
return cmpnd_assign(XOR_U, daget(cexpr->params, 0), daget(cexpr->params, 1), prog);
case ORASSIGN:
return cmpnd_assign(OR_U, daget(cexpr->params, 0), daget(cexpr->params, 1), prog);
case MULTASSIGN:
return cmpnd_assign(MULT_U, daget(cexpr->params, 0), daget(cexpr->params, 1), prog);
case MODASSIGN:
return cmpnd_assign(MOD_U, daget(cexpr->params, 0), daget(cexpr->params, 1), prog);
case NOP: case MEMBER:
assert(0);
case SZOF:
destaddr.addr_type = ISCONST | 8;
destaddr.addr.intconst_64 = lentype(cexpr->vartype);
if(-1 == destaddr.addr.intconst_64)
return execvla(cexpr->vartype, prog);
return destaddr;
case FCALL:
if(cexpr->params->length > 1) {
OPERATION* fparam;
curaddr = linearitree(daget(cexpr->params, 1), prog, 0);
fparam = ct_3ac_op1(ARG_3, curaddr.addr_type, curaddr.addr);
for(int i = 2; i < cexpr->params->length; ++i) {
curaddr = linearitree(daget(cexpr->params, i), prog, 0);
opn(prog, ct_3ac_op1(ARG_3, curaddr.addr_type, curaddr.addr));
}
opn(prog, fparam);
}
IDTYPE* frettype = cexpr->rettype;
if(ispointer(frettype)) {
FILLREG(destaddr, ISPOINTER | 8);
} else if(frettype->tb & (STRUCTVAL | UNIONVAL)) {
FILLREG(destaddr, ISPOINTER | 8);
FILLREG(otheraddr, ISPOINTER | 8);
curaddr.addr.uintconst_64 = lentype(frettype);
opn(prog, ct_3ac_op2(ALOC_3, ISCONST | 8, curaddr.addr, otheraddr.addr_type, otheraddr.addr));
} else if(frettype->tb & FLOATNUM) {
FILLREG(destaddr, ISFLOAT | (frettype->tb & 0xf));
} else if(frettype->tb & UNSIGNEDNUM) {
FILLREG(destaddr, frettype->tb & 0xf);
} else {
FILLREG(destaddr, ISSIGNED | (frettype->tb & 0xf));
}
EXPRESSION* fname = daget(cexpr->params, 0);
opn(prog, ct_3ac_op2(CALL_3, ISCONST | ISLABEL, (ADDRESS) fname->id->name, destaddr.addr_type, destaddr.addr));
if(!ispointer(frettype) && frettype->tb & (STRUCTVAL | UNIONVAL))
opn(prog, ct_3ac_op3(COPY_3, destaddr.addr_type | ISDEREF, destaddr.addr, ISCONST | 8, curaddr.addr, otheraddr.addr_type | ISDEREF, otheraddr.addr));
//Note, this is not entirely to spec as the program is only supposed to copy the struct as a transparent final argument within the scope of the called function, not within the caller. This should work for now however.