-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompintern.c
1530 lines (1441 loc) · 48 KB
/
compintern.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 "compintern.h"
#include "treeduce.h"
#include "joecc.tab.h"
USTRUCT* ustructor(char* name, DYNARR* fields, struct lexctx* lct) {
USTRUCT* retval = malloc(sizeof(USTRUCT));
retval->name = name;
retval->fields = fields;
retval->offsets = NULL;
retval->size = 0;
dapush(lct->enstruct2free, retval);
return retval;
}
ENUM* enumctor(char* name, DYNARR* fields, struct lexctx* lct) {
ENUM* retval = malloc(sizeof(ENUM));
retval->name = name;
retval->fields = fields;
dapush(lct->enumerat2free, retval);
return retval;
}
OPERAND* genoperand(char* constraint, EXPRESSION* varin) {
OPERAND* retval = malloc(sizeof(OPERAND));
retval->constraint = constraint;
retval->varin = varin;
return retval;
}
//fully clone idtype
static IDTYPE* fcid(IDTYPE* idt) {
IDTYPE* idr = malloc(sizeof(IDTYPE));
memcpy(idr, idt, sizeof(IDTYPE));
idr->pointerstack = daclone(idt->pointerstack);
return idr;
}
//shallow clone expression
EXPRESSION* cloneexpr(EXPRESSION* orig) {
EXPRESSION* clone = malloc(sizeof(EXPRESSION));
memcpy(clone, orig, sizeof(EXPRESSION));//includes the location
return clone;
}
//fully clone idtype, accepting null pointerstack
IDTYPE* fcid2(IDTYPE* idt) {
IDTYPE* idr = malloc(sizeof(IDTYPE));
memcpy(idr, idt, sizeof(IDTYPE));
if(idt->pointerstack) idr->pointerstack = ptrdaclone(idt->pointerstack);
else idr->pointerstack = NULL;
return idr;
}
static EXPRESSION* allocexpr(EXPRTYPE ext, IDTYPE* rettype, int locstartind, int locendind) {
EXPRESSION* retval = malloc(sizeof(EXPRESSION));
retval->type = ext;
retval->rettype = rettype;
retval->locstartind = locstartind;
retval->locendind = locendind;
return retval;
}
//fully clone pointerstack
DYNARR* ptrdaclone(DYNARR* opointerstack) {
DYNARR* npointerstack = dactor(opointerstack->maxlength);
for(int i = 0; i < opointerstack->length; i++) {
struct declarator_part* dclp = malloc(sizeof(struct declarator_part));
DYNARR* newp;
memcpy(dclp, opointerstack->arr[i], sizeof(struct declarator_part));
switch(dclp->type) {
case PARAMSSPEC:
newp = dactor(dclp->params->length);
for(int j = 0; j < dclp->params->length; j++) {
DECLARATION* parid = daget(dclp->params, j);
if(!parid) {
dapush(newp, NULL);
} else {
DECLARATION* newdecl = malloc(sizeof(DECLARATION));
newdecl->type = fcid2(parid->type);
newdecl->varname = strdup(parid->varname);
dapush(newp, newdecl);
}
}
dclp->params = newp;
break;
case NAMELESS_PARAMSSPEC:
if(dclp->nameless_params) {
dclp->nameless_params = daclone(dclp->nameless_params);
for(int j = 0; j < dclp->nameless_params->length; j++) {
if(dclp->nameless_params->arr[j]) {
dclp->nameless_params->arr[j] = fcid2(dclp->nameless_params->arr[j]);
}
}
}
break;
case POINTERSPEC: case ARRAYSPEC:
break;
case VLASPEC:
dclp->vlaent = rclonexpr(dclp->vlaent);
break;
case BITFIELDSPEC:
assert(0); //TODO: handle bitfields
}
npointerstack->arr[i] = dclp;
}
npointerstack->length = opointerstack->length;
return npointerstack;
}
EXPRESSION* ct_nop_expr(int locstartind, int locendind) {
EXPRESSION* retval = allocexpr(NOP, NULL, locstartind, locendind);
return retval;
}
EXPRESSION* ct_unary_expr(EXPRTYPE t, EXPRESSION* param, int locstartind, int locendind) {
EXPRESSION* retval = allocexpr(t, NULL, locstartind, locendind);
retval->params = dactor(1);
retval->params->arr[0] = param;
retval->params->length = 1;
return retval;
}
EXPRESSION* ct_sztype(IDTYPE* whichtype, int locstartind, int locendind) {
if(!(whichtype->tb & (STRUCTVAL | ENUMVAL | UNIONVAL))) {
EXPRESSION* ic = ct_intconst_expr(whichtype->tb & 0xf, locstartind, locendind);
free(whichtype);
return ic;
}
EXPRESSION* retval = allocexpr(SZOF, NULL, locstartind, locendind);
retval->vartype = whichtype;
return retval;
}
EXPRESSION* ct_binary_expr(EXPRTYPE t, EXPRESSION* param1, EXPRESSION* param2) {
EXPRESSION* retval = allocexpr(t, NULL, param1->locstartind, param2->locendind);
retval->params = dactor(2);
retval->params->arr[0] = param1;
retval->params->arr[1] = param2;
retval->params->length = 2;
return retval;
}
EXPRESSION* ct_cast_expr(IDTYPE* type, EXPRESSION* expr, int locstartind) {
EXPRESSION* retval = allocexpr(CAST, type, locstartind, expr->locendind);
retval->params = dactor(1);
retval->params->arr[0] = expr;
retval->params->length = 1;
retval->vartype = type;
return retval;
}
EXPRESSION* ct_ternary_expr(EXPRESSION* param1, EXPRESSION* param2, EXPRESSION* param3) {
EXPRESSION* retval = allocexpr(TERNARY, NULL, param1->locstartind, param3->locendind);
retval->params = dactor(3);
retval->params->arr[0] = param1;
retval->params->arr[1] = param2;
retval->params->arr[2] = param3;
retval->params->length = 3;
return retval;
}
EXPRESSION* ct_fcall_expr(EXPRESSION* func, DYNARR* params, int locendind) {
EXPRESSION* retval = malloc(sizeof(EXPRESSION));
retval->type = FCALL;
retval->locstartind = func->locstartind;
retval->locendind = locendind;
assert(func->type & IDENT);
DYNARR* ptrs = func->id->type->pointerstack;
assert(ptrs);
assert(ptrs->length);
IDTYPE* retid = fcid(func->id->type);
if(((struct declarator_part*) dapeek(ptrs))->type == POINTERSPEC) {
struct declarator_part* da2 = daget(ptrs, ptrs->length - 2);
assert(da2->type == PARAMSSPEC || da2->type == NAMELESS_PARAMSSPEC);
retid->pointerstack->length -= 2; //shorten but no pop needed
} else {
dapop(retid->pointerstack);
//clone pointer stack, remove function type from it
}
retval->rettype = retid;
DYNARR* dd = dactor(1 + params->length);
dd->arr[0] = func;
dd->length = 1;
retval->params = damerge(dd, params);
return retval;
}
EXPRESSION* ct_strconst_expr(const char* str, int locstartind, int locendind) {
EXPRESSION* retval = allocexpr(STRING, malloc(sizeof(IDTYPE)), locstartind, locendind);
retval->rettype->pointerstack = dactor(1);
dapushc(retval->rettype->pointerstack, mkdeclpart(POINTERSPEC, 0));
retval->rettype->tb = 1 | UNSIGNEDNUM;
retval->strconst = (char*)(unsigned long) str;
return retval;
}
EXPRESSION* ct_intconst_expr(long num, int locstartind, int locendind) {
EXPRESSION* retval = allocexpr(INT, NULL, locstartind, locendind);
retval->intconst = num;
return retval;
}
EXPRESSION* ct_uintconst_expr(unsigned long num, int locstartind, int locendind) {
EXPRESSION* retval = allocexpr(UINT, NULL, locstartind, locendind);
retval->uintconst = num;
return retval;
}
EXPRESSION* ct_floatconst_expr(double num, int locstartind, int locendind) {
EXPRESSION* retval = allocexpr(FLOAT, malloc(sizeof(IDTYPE)), locstartind, locendind);
retval->rettype->pointerstack = NULL;
retval->rettype->tb = 8 | FLOATNUM;
retval->floatconst = num;
return retval;
}
EXPRESSION* ct_array_lit(DYNARR* da, int locstartind, int locendind) {
EXPRESSION* retval = allocexpr(ARRAY_LIT, NULL, locstartind, locendind);
retval->params = da;
return retval;
}
EXPRESSION* ct_member_expr(char* member, int locstartind, int locendind) {
EXPRESSION* retval = allocexpr(MEMBER, NULL, locstartind, locendind);
retval->member = member;
return retval;
}
EXPRESSION* ct_ident_expr(struct lexctx* lct, char* ident, int locstartind, int locendind) {
EXPRESSION* retval = allocexpr(IDENT, NULL, locstartind, locendind);
IDENTIFIERINFO* ids = scopesearch(lct, M_VARIABLE, ident);
if(!ids) {
assert(lct->func); //error out, this may be not to spec
}
retval->id = malloc(sizeof(IDENTIFIERINFO));
retval->id->type = ids->type;
retval->id->name = ident;
if(ids->type->tb & GLOBALFUNC) {
retval->id->index = -2;
} else {
retval->id->index = ids->index;
}
assert(retval->id);
retval->rettype = retval->id->type;
return retval;
}
//returns whether two types are compatible: i.e. they can be added without an implicit or explicit cast
char typecompat(IDTYPE* t1, IDTYPE* t2) {
if(ispointer(t1))
return ispointer(t2); //array special case?
if(ispointer(t2))
return 0;
if(t1->tb & (STRUCTVAL | UNIONVAL)) {
if(!(t2->tb & (STRUCTVAL | UNIONVAL)))
return 0;
USTRUCT* st1 = t1->structtype;
USTRUCT* st2 = t2->structtype;
if(st1->fields->length != st2->fields->length)
return 0;
for(int i = 0; i < st1->fields->length; i++) {
STRUCTFIELD* sf1 = daget(st1->fields, i);
STRUCTFIELD* sf2 = daget(st2->fields, i);
if(sf1->offset != sf2->offset)
return 0;
if(!typecompat(sf1->type, sf2->type))
return 0;
}
return 1;
}
if(t2->tb & (STRUCTVAL | UNIONVAL))
return 0;
return 1;//!((t1->tb & FLOATNUM)^(t2->tb & FLOATNUM));
//floats are coerced into ints and vice versa
}
//TODO: handle NULLs default
//Process array initializer expression, figuring out length and populating types
int process_array_lit(IDTYPE* arr_memtype, EXPRESSION* arr_expr) {
struct declarator_part* tdclp = dapeek(arr_memtype->pointerstack);
tdclp->arrlen = 0;
arr_memtype->pointerstack->length -= 1;
if(!ispointer(arr_memtype) || ((struct declarator_part*) dapeek(arr_memtype->pointerstack))->type != ARRAYSPEC) {
int szstep;
if(ispointer(arr_memtype))
szstep = 0x8;//arr dim is one so must be real pointer
else if(arr_memtype->tb & (STRUCTVAL | UNIONVAL))
szstep = arr_memtype->structtype->size;
else szstep = arr_memtype->tb & 0xf;
if(0 == arr_memtype->pointerstack->length) {
if(arr_memtype->tb & (STRUCTVAL | UNIONVAL)) {
int i;
for(i = 0; i < arr_expr->params->length; i++) {
EXPRESSION* arrv = daget(arr_expr->params, i);
process_struct_lit(arr_memtype, arrv);
arrv->rettype = fcid2(arr_memtype);
tdclp->arrlen += szstep;
}
for(; i < tdclp->arrmaxind; i++) {
EXPRESSION* tofill = ct_array_lit(dactor(8), arr_expr->locstartind, arr_expr->locendind);
process_struct_lit(arr_memtype, tofill);
tofill->rettype = fcid2(arr_memtype);
dapush(arr_expr->params, tofill);
tdclp->arrlen += szstep;
}
} else {
int i;
for(i = 0; i < arr_expr->params->length; i++) {
EXPRESSION* arrv = daget(arr_expr->params, i);
if(!arrv) {
if(ispointer(arr_memtype)) {
if(((struct declarator_part*) dapeek(arr_memtype->pointerstack))->type == ARRAYSPEC) {
EXPRESSION* tofill = ct_array_lit(dactor(8), arr_expr->locstartind, arr_expr->locendind);
process_array_lit(arr_memtype, tofill);
tofill->rettype = fcid2(arr_memtype);
arr_expr->params->arr[i] = tofill;
} else {
arr_expr->params->arr[i] = ct_uintconst_expr(0, arr_expr->locstartind, arr_expr->locendind);
}
} else if(arr_memtype->tb & FLOATNUM) {
arr_expr->params->arr[i] = ct_floatconst_expr(0.0, arr_expr->locstartind, arr_expr->locendind);
} else if(arr_memtype->tb & UNSIGNEDNUM) {
arr_expr->params->arr[i] = ct_uintconst_expr(0, arr_expr->locstartind, arr_expr->locendind);
} else {
arr_expr->params->arr[i] = ct_intconst_expr(0, arr_expr->locstartind, arr_expr->locendind);
}
} else {
IDTYPE arrt = typex(arrv);
assert(typecompat(&arrt, arr_memtype));
}
tdclp->arrlen += szstep;
}
for(; i < tdclp->arrmaxind; i++) {
if(ispointer(arr_memtype)) {
if(((struct declarator_part*) dapeek(arr_memtype->pointerstack))->type == ARRAYSPEC) {
EXPRESSION* tofill = ct_array_lit(dactor(8), arr_expr->locstartind, arr_expr->locendind);
process_array_lit(arr_memtype, tofill);
tofill->rettype = fcid2(arr_memtype);
dapush(arr_expr->params, tofill);
} else {
dapush(arr_expr->params, ct_uintconst_expr(0, arr_expr->locstartind, arr_expr->locendind));
}
} else if(arr_memtype->tb & FLOATNUM) {
dapush(arr_expr->params, ct_floatconst_expr(0.0, arr_expr->locstartind, arr_expr->locendind));
} else if(arr_memtype->tb & UNSIGNEDNUM) {
dapush(arr_expr->params, ct_uintconst_expr(0, arr_expr->locstartind, arr_expr->locendind));
} else {
dapush(arr_expr->params, ct_intconst_expr(0, arr_expr->locstartind, arr_expr->locendind));
}
tdclp->arrlen += szstep;
}
//params are fine, no further processing necessary
}
} else {
int i;
for(i = 0; i < arr_expr->params->length; i++) {
EXPRESSION* arrv = daget(arr_expr->params, i);
IDTYPE arrt = typex(arrv);
assert(typecompat(&arrt, arr_memtype));
tdclp->arrlen += szstep;
}
for(; i < tdclp->arrmaxind; i++) {
dapush(arr_expr->params, ct_uintconst_expr(0, arr_expr->locstartind, arr_expr->locendind));
tdclp->arrlen += szstep;
}
//params are fine, no further processing necessary
}
} else {
int i;
for(i = 0; i < arr_expr->params->length; i++) {
EXPRESSION* arrv = daget(arr_expr->params, i);
tdclp->arrlen += process_array_lit(arr_memtype, arrv);
arrv->rettype = fcid2(arr_memtype);
assert(typecompat(arrv->rettype, arr_memtype));
}
for(; i < tdclp->arrmaxind; i++) {
struct declarator_part* ldclp = dapeek(arr_memtype->pointerstack);
EXPRESSION* arrv = ct_array_lit(dactor(ldclp->arrmaxind), arr_expr->locstartind, arr_expr->locendind);
tdclp->arrlen += process_array_lit(arr_memtype, arrv);
arrv->rettype = fcid2(arr_memtype);
assert(typecompat(arrv->rettype, arr_memtype));
}
}
if(!tdclp->arrmaxind) tdclp->arrmaxind = arr_expr->params->length;
arr_memtype->pointerstack->length += 1;
return tdclp->arrlen;
}
//Process struct initializer expression, figuring out length and populating types
int process_struct_lit(IDTYPE* struct_memtype, EXPRESSION* struct_expr) {
struct_expr->type = STRUCT_LIT;
USTRUCT* imptype = struct_memtype->structtype;
feedstruct(imptype);
if(struct_memtype->tb & UNIONVAL) {
//union initializers only do the first item in the union (without designators)
assert(struct_expr->params->length == 1);
} else {
assert(struct_expr->params->length == imptype->fields->length);
}
int i;
for(i = 0; i < struct_expr->params->length; i++) {
EXPRESSION* member = daget(struct_expr->params, i);
DECLARATION* decl = daget(imptype->fields, i);
if(member->type == ARRAY_LIT) {
if(decl->type->pointerstack && decl->type->pointerstack->length &&
((struct declarator_part*) dapeek(decl->type->pointerstack))->type == ARRAYSPEC) {
int arrdim = 0;
for(int j = decl->type->pointerstack->length - 1; j >= 0; j--, arrdim++) {
struct declarator_part* pointtop = daget(decl->type->pointerstack, j);
if(pointtop->type != ARRAYSPEC) break;
}
assert(arrdim);
process_array_lit(decl->type, member);
member->rettype = fcid2(decl->type);
} else if(decl->type->tb & (STRUCTVAL | UNIONVAL)) {
process_struct_lit(decl->type, member);
member->rettype = fcid2(decl->type);
} else {
assert(0);
}
} else {
IDTYPE memty = typex(member);
assert(typecompat(&memty, decl->type));
}
}
for(;i < struct_memtype->structtype->fields->length; i++) {
DECLARATION* decl = daget(imptype->fields, i);
if(ispointer(decl->type)) {
if(((struct declarator_part*) dapeek(decl->type->pointerstack))->type == ARRAYSPEC) {
EXPRESSION* tofill = ct_array_lit(dactor(8), struct_expr->locstartind, struct_expr->locendind);
process_array_lit(decl->type, tofill);
tofill->rettype = fcid2(decl->type);
dapush(struct_expr->params, tofill);
} else {
dapush(struct_expr->params, ct_uintconst_expr(0, struct_expr->locstartind, struct_expr->locendind));
}
} else if(decl->type->tb & (STRUCTVAL | UNIONVAL)) {
EXPRESSION* tofill = ct_array_lit(dactor(8), struct_expr->locstartind, struct_expr->locendind);
process_struct_lit(decl->type, tofill);
tofill->rettype = fcid2(decl->type);
dapush(struct_expr->params, tofill);
} else if(decl->type->tb & FLOATNUM) {
dapush(struct_expr->params, ct_floatconst_expr(0.0, struct_expr->locstartind, struct_expr->locendind));
} else if(decl->type->tb & UNSIGNEDNUM) {
dapush(struct_expr->params, ct_uintconst_expr(0, struct_expr->locstartind, struct_expr->locendind));
} else {
dapush(struct_expr->params, ct_intconst_expr(0, struct_expr->locstartind, struct_expr->locendind));
}
}
return imptype->size;
}
//completely frees declaration, including if the type has an anonymous struct member
void freedecl(DECLARATION* dcl) {
if(dcl->varname)
free(dcl->varname);
if(!(ispointer(dcl->type)) && dcl->type->tb & (ANONMEMB))
wipestruct(dcl->type->structtype);
freetype(dcl->type);
free(dcl);
}
//completely frees struct or union container
void wipestruct(USTRUCT* strct) {
if(strct->fields) {
for(int i = 0; i < strct->fields->length; ++i) {
DECLARATION* dcl = strct->fields->arr[i];
freedecl(dcl);
}
dadtor(strct->fields);
}
if(strct->offsets) qchtdtor(strct->offsets, free);
if(strct->name) free(strct->name);
free(strct);
}
//free enum field
void fef(ENUMFIELD* enf) {
free(enf->name);
free(enf);
}
//completely frees enum container
void freenum(ENUM* enm) {
if(enm->name) free(enm->name);
dadtorcfr(enm->fields, (void(*)(void*)) fef);
free(enm);
}
//frees a nameless declaration
static void fpdecl(DECLARATION* dc) {
if(!dc) return;
freetype(dc->type);
free(dc); //dc->varname should be freed in dadtor
}
//frees a variable declaration, named
static void fpdecl2(DECLARATION* dc) {
if(!dc) return;
freetype(dc->type);
free(dc->varname);
free(dc);
}
void freedeclpart(struct declarator_part* dclp) {
switch(dclp->type) {
case PARAMSSPEC:
dadtorcfr(dclp->params, (void (*)(void*)) fpdecl2);
break;
case NAMELESS_PARAMSSPEC:
if(dclp->nameless_params) dadtorcfr(dclp->nameless_params, (void (*)(void*)) freetype);
break;
case VLASPEC:
rfreexpr(dclp->vlaent);
break;
case POINTERSPEC: case ARRAYSPEC:
break;
case BITFIELDSPEC:
rfreexpr(dclp->bfspec);
break;
}
free(dclp);
}
//recursively frees idtype
void freetype(IDTYPE* id) {
if(!id)
return;
if(id->pointerstack) {
for(int i = 0; i < id->pointerstack->length; i++) {
struct declarator_part* dclp = id->pointerstack->arr[i];
freedeclpart(dclp);
}
dadtor(id->pointerstack);
id->pointerstack = NULL;
}
free(id);
}
//recursively frees expr
void rfreexpr(EXPRESSION* e) {
switch(e->type) {
case MEMBER:
free(e->member);
free(e);
return;
case NOP:
break;
case ARRAY_LIT: case STRUCT_LIT:
case L_AND: case L_OR: case L_NOT:
case EQ: case NEQ: case GT: case LT: case GTE: case LTE:
case SZOFEXPR:
case CAST://rettype and vartype are the same pointer
dadtorcfr(e->params, (void(*)(void*)) rfreexpr);
break;
case SZOF:
free(e->vartype);
break;
case UINT: case INT: case FLOAT:
break;
case STRING:
free(e->strconst);
if(e->rettype) freetype(e->rettype);
free(e);
return;
case NEG: case COMMA:
case B_NOT: case POSTINC: case POSTDEC:
case PREINC: case PREDEC:
case ASSIGN: case ADDASSIGN: case SUBASSIGN:
case SHLASSIGN: case SHRASSIGN: case ANDASSIGN:
case XORASSIGN: case ORASSIGN: case DIVASSIGN:
case MULTASSIGN: case MODASSIGN:
case B_AND: case B_OR: case B_XOR:
case ADD: case SUB: case SHR: case SHL:
case MULT: case DIVI: case MOD: case TERNARY:
case DOTOP: case ARROW:
dadtorcfr(e->params, (void(*)(void*)) rfreexpr);
if(e->rettype) free(e->rettype);
free(e);
return;
case IDENT:
free(e->id->name);
free(e->id);
free(e);
return;
case FCALL:
dadtorcfr(e->params, (void(*)(void*)) rfreexpr);
dadtor(e->rettype->pointerstack);//only free storage, declarator parts from global
free(e->rettype);
free(e);
return;
case ADDR:
if(e->rettype) free(dapop(e->rettype->pointerstack));
//fall through
case DEREF:
dadtorcfr(e->params, (void(*)(void*)) rfreexpr);
if(e->rettype) {
dadtor(e->rettype->pointerstack);
free(e->rettype);
}
free(e);
return;
}
if(e->rettype)
freetype(e->rettype);
free(e);
}
//frees initializer
void freeinit(INITIALIZER* i) {
if(i->expr) {
rfreexpr(i->expr);
}
free(i->decl->varname);
fpdecl(i->decl);
free(i);
}
void freesoi(SOI* soi) {
if(soi->isstmt) {
rfreestate(soi->state);
} else {
for(int j = 0; j < soi->init->length; j++) {
INITIALIZER* in = daget(soi->init, j);
if(in->expr) {
rfreexpr(in->expr);
}
freetype(in->decl->type);
free(in->decl->varname);
free(in->decl);
free(in);
}
dadtor(soi->init);
}
free(soi);
}
//frees list of statements and initializers such as that which composes a block statement
void freesai(DYNARR* stmtsandinits) {
for(int i = 0; i < stmtsandinits->length; i++) {
freesoi(daget(stmtsandinits, i));
}
dadtor(stmtsandinits);
}
//recursively frees statement
void rfreestate(STATEMENT* s) {
switch(s->type) {
case LBREAK: case LCONT: case DEFAULT: case NOPSTMT:
//We don't reduce case statement here
break;
case LABEL:
case JGOTO:
free(s->glabel);
break;
case CASE:
break;
case SWITCH:
lvhtdtor(s->switchinfo->cases);//labels already freed in 3ac
dadtor(s->switchinfo->caseorder);
free(s->switchinfo);
//fall through
case WHILEL: case DOWHILEL:
rfreestate(s->body);
rfreexpr(s->cond);
break;
case FORL:
rfreestate(s->forbody);
rfreexpr(s->forcond);
rfreexpr(s->increment);
if(s->forinit->isE) {
rfreexpr(s->forinit->E);
} else {
dadtorcfr(s->forinit->I, (void (*)(void*)) freeinit);
}
free(s->forinit);
break;
case CMPND:
if(s->stmtsandinits) {
freesai(s->stmtsandinits);
}
break;
case FRET:
if(!s->expression) break;
//fall through
case EXPR:
rfreexpr(s->expression);
break;
case IFELSES:
rfreestate(s->elsecond);
//fall through
case IFS:
rfreestate(s->thencond);
rfreexpr(s->ifcond);
break;
case ASMSTMT:
free(s->asmstmts);
if(s->inputs) {
for(int i = 0; i < s->inputs->length; i++) {
OPERAND* op = daget(s->inputs, i);
free(op->constraint);
rfreexpr(op->varin);
free(op);
}
dadtor(s->inputs);
}
if(s->outputs) {
for(int i = 0; i < s->outputs->length; i++) {
OPERAND* op = daget(s->outputs, i);
free(op->constraint);
rfreexpr(op->varin);
free(op);
}
dadtor(s->outputs);
}
if(s->clobbers) {
dadtorfr(s->clobbers);
}
break;
}
free(s);
}
//recursively frees function container
void rfreefunc(FUNC* f) {
if(!f) return;
free(f->name);
freetype(f->retrn);
rfreestate(f->body);
if(f->lbls) qchtdtor(f->lbls, free);
dadtor(f->switchstack);
free(f);
}
//deep clones an expression
EXPRESSION* rclonexpr(EXPRESSION* e) {
EXPRESSION* e2 = malloc(sizeof(EXPRESSION));
memcpy(e2, e, sizeof(EXPRESSION)); //includes location
switch(e->type) {
default:
e2->params = dactor(e->params->length);
for(int i = 0; i < e->params->length; i++)
dapushc(e2->params, rclonexpr(e->params->arr[i]));
case NOP:
break;
case SZOF:
e2->vartype = malloc(sizeof(IDTYPE));
memcpy(e2->vartype, e->vartype, sizeof(IDTYPE));
if(e->vartype->pointerstack) {
e2->vartype->pointerstack = dactor(e->vartype->pointerstack->length);
for(int i = 0; i < e->vartype->pointerstack->length; i++) {
struct declarator_part* dp = malloc(sizeof(struct declarator_part));
memcpy(dp, e->vartype->pointerstack->arr[i], sizeof(struct declarator_part));
dapushc(e2->vartype->pointerstack, dp);
}
}
break;
case CAST:
e2->vartype = malloc(sizeof(IDTYPE));
memcpy(e2->vartype, e->vartype, sizeof(IDTYPE));
if(e->vartype->pointerstack) {
e2->vartype->pointerstack = dactor(e->vartype->pointerstack->length);
for(int i = 0; i < e->vartype->pointerstack->length; i++) {
struct declarator_part* dp = malloc(sizeof(struct declarator_part));
memcpy(dp, e->vartype->pointerstack->arr[i], sizeof(struct declarator_part));
dapushc(e2->vartype->pointerstack, dp);
}
}
e2->params = dactor(e->params->length);
for(int i = 0; i < e->params->length; i++)
dapushc(e2->params, rclonexpr(e->params->arr[i]));
break;
case STRING:
e2->strconst = strdup(e->strconst);
break;
case MEMBER:
e2->member = strdup(e->member);
break;
case INT: case UINT: case FLOAT:
break;
case IDENT:
break;//do not free identifier info
}
return e2;
}
DECLARATION* mkdeclaration(char* name) {
DECLARATION* retval = calloc(1,sizeof(DECLARATION));
retval->varname = name;
IDTYPE* idt = calloc(1, sizeof(IDTYPE));
idt->pointerstack = dactor(2);
retval->type = idt;
return retval;
}
INITIALIZER* geninit(DECLARATION* decl, EXPRESSION* expr, int locstartind, int locendind) {
INITIALIZER* retval = malloc(sizeof(INITIALIZER));
retval->decl = decl;
retval->expr = expr;
retval->locstartind = locstartind;
retval->locendind = locendind;
return retval;
}
//statement or initializer: statement
SOI* sois(struct stmt* state) {
SOI* retval = malloc(sizeof(SOI));
retval->isstmt = 1;
retval->state = state;
return retval;
}
//statement or initializer: initializer
SOI* soii(DYNARR* init) {
SOI* retval = malloc(sizeof(SOI));
retval->isstmt = 0;
retval->init = init;
return retval;
}
STATEMENT* mkexprstmt(enum stmttype type, EXPRESSION* express, int locstartind, int locendind) {
STATEMENT* retval = malloc(sizeof(STATEMENT));
retval->type = type;
retval->expression = express;
retval->locstartind = locstartind;
retval->locendind = locendind;
return retval;
}
STATEMENT* mknopstmt(void) {
STATEMENT* retval = malloc(sizeof(STATEMENT));
retval->type = NOPSTMT;
retval->locstartind = -1;
retval->locendind = -1;
return retval;
}
STATEMENT* mkgotostmt(char* gotoloc, int locstartind, int locendind) {
STATEMENT* retval = malloc(sizeof(STATEMENT));
retval->type = JGOTO;
retval->glabel = gotoloc;
retval->locstartind = locstartind;
retval->locendind = locendind;
return retval;
}
STATEMENT* mkforstmt(EOI* e1, EXPRESSION* e2, EXPRESSION* e3, STATEMENT* bdy, int locstartind) {
STATEMENT* retval = malloc(sizeof(STATEMENT));
retval->type = FORL;
retval->forinit = e1;
retval->forcond = e2;
retval->increment = e3;
retval->forbody = bdy;
retval->locstartind = locstartind;
retval->locendind = bdy->locendind;
return retval;
}
STATEMENT* mklsstmt(enum stmttype type, EXPRESSION* condition, STATEMENT* bdy, int locstartind, int locendind) {
STATEMENT* retval = malloc(sizeof(STATEMENT));
retval->type = type;
retval->cond = condition;
retval->body = bdy;
retval->locstartind = locstartind;
retval->locendind = locendind;
return retval;
}
STATEMENT* mkswitchstmt(EXPRESSION* contingent, STATEMENT* bdy, SWITCHINFO* swi, int locstartind) {
STATEMENT* retval = malloc(sizeof(STATEMENT));
retval->type = SWITCH;
retval->cond = contingent;
retval->body = bdy;
retval->switchinfo = swi;
retval->locstartind = locstartind;
retval->locendind = bdy->locendind;
return retval;
}
STATEMENT* mkifstmt(EXPRESSION* condition, STATEMENT* ifbdy, STATEMENT* elsebdy, int locstartind) {
STATEMENT* retval = malloc(sizeof(STATEMENT));
retval->ifcond = condition;
retval->thencond = ifbdy;
if(elsebdy) {
retval->elsecond = elsebdy;
retval->type = IFELSES;
retval->locendind = elsebdy->locendind;
} else {
retval->type = IFS;
retval->locendind = ifbdy->locendind;
}
retval->locstartind = locstartind;
return retval;
}
STATEMENT* mkcmpndstmt(DYNARR* stmtsandinits, int locstartind, int locendind) {
STATEMENT* retval = malloc(sizeof(STATEMENT));
retval->type = CMPND;
retval->stmtsandinits = stmtsandinits;
retval->locstartind = locstartind;
retval->locendind = locendind;
return retval;
}
STATEMENT* mklblstmt(struct lexctx* lct, char* lblval, int locstartind, int locendind) {
STATEMENT* retval = malloc(sizeof(STATEMENT));
retval->type = LABEL;
retval->glabel = lblval;
retval->locstartind = locstartind;
retval->locendind = locendind;
if(!lct->func->lbls) lct->func->lbls = qhtctor();
qinsert(lct->func->lbls, lblval, NULL);
//confirm no collision
return retval;
}
STATEMENT* mkcasestmt(struct lexctx* lct, EXPRESSION* casexpr, char* label, int locstartind, int locendind) {
SWITCHINFO* swi = dapeek(lct->func->switchstack);
while(foldconst(casexpr)) ;
switch(casexpr->type) {
case INT: case UINT:
lvinsert(swi->cases, casexpr->uintconst, label);
dapush(swi->caseorder, (void*) casexpr->uintconst);
free(casexpr);
break;
default:
fprintf(stderr, "Error: case has nonrectifiable value\n");
assert(0);
}
return mklblstmt(lct, label, locstartind, locendind);
}
STATEMENT* mkdefaultstmt(struct lexctx* lct, char* label, int locstartind, int locendind) {
((SWITCHINFO*) dapeek(lct->func->switchstack))->defaultval = label;
return mklblstmt(lct, label, locstartind, locendind);
}
STATEMENT* mkasmstmt(char* asmstmts, DYNARR* outputs, DYNARR* inputs, DYNARR* clobbers, int locstartind, int locendind) {
STATEMENT* retval = malloc(sizeof(STATEMENT));
retval->asmstmts = asmstmts;
retval->outputs = outputs;
retval->inputs = inputs;
retval->clobbers = clobbers;
retval->locstartind = locstartind;
retval->locendind = locendind;
return retval;
}
ENUMFIELD* genenumfield(char* name, EXPRESSION* value) {
ENUMFIELD* retval = malloc(sizeof(ENUMFIELD));
retval->name = name;
while(foldconst(value)) ;
switch(value->type) {
case INT: case UINT:
break;
default:
fprintf(stderr,"Error: enum has nonrectifiable value\n");
assert(0);
}
retval->value = value;
return retval;
}
struct declarator_part* mkdeclpart(enum declpart_info typ, void* d) {
struct declarator_part* retval = malloc(sizeof(struct declarator_part));
retval->type = typ;
retval->garbage = d;
return retval;
}
struct declarator_part* mkdeclpartarr(enum declpart_info typ, EXPRESSION* d) {
struct declarator_part* retval = malloc(sizeof(struct declarator_part));
retval->type = typ;
foldconst(d);
switch(d->type) {
case INT: case UINT:
retval->arrmaxind = d->intconst;
retval->arrlen = -1;
break;
default:
retval->type = VLASPEC;
retval->vlaent = d;
return retval;
}
rfreexpr(d);
return retval;
}
struct declarator_part* mkdeclptr(TYPEBITS d) {
struct declarator_part* retval = malloc(sizeof(struct declarator_part));
retval->type = POINTERSPEC;
retval->ptrspec = d;
return retval;
}
FUNC* ct_function(char* name, STATEMENT* body, DYNARR* params, IDTYPE* retrn) {
FUNC* func = malloc(sizeof(FUNC));