-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenllvm.c
More file actions
1481 lines (1296 loc) · 45.3 KB
/
genllvm.c
File metadata and controls
1481 lines (1296 loc) · 45.3 KB
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
//
// Created by JakoError on 2021/12/15.
//
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "genllvm.h"
#include "list.h"
#include "ast.h"
String comm_space = " ";
int mark = 1;
int ti = 0;
int fi = 0;
//break in if-else
int ei = 0;
bool cond = false;
bool in_while = false;
//for break and continue
String start = NULL;
String end = NULL;
//for FuncRParams use pointer
int pointer = 0;
void genCompUnit(Bean CompUnit, String *buff) {
if (strcmp(CompUnit->value, "Decl") == 0)
genDecl(CompUnit->beans[0], buff);
else if (strcmp(CompUnit->value, "FuncDef") == 0)
genFuncDef(CompUnit->beans[0], buff);
else {
printf("Error-Type not match CompUnit: '%s'", toString(CompUnit));
exit(0);
}
}
void genDecl(Bean Decl, String *buff) {
if (strcmp(Decl->type, "VarDecl") == 0) {
genVarDecl(Decl->beans[0], buff, false);
return;
}
if (strcmp(Decl->type, "ConstDecl") == 0) {
genVarDecl(Decl->beans[0], buff, true);
return;
}
printf("Type not match VarDecl: '%s'", Decl->type);
exit(0);
}
void genVarDecl(Bean VarDecl, String *buff, bool is_const) {
genVarDefs(VarDecl, buff, is_const);
}
void genVarDefs(Bean VarDefs, String *buff, bool is_const) {
for (int i = 0; i < VarDefs->i; ++i)
genVarDef(VarDefs->beans[i], buff, is_const);
}
void genVarDef(Bean VarDef, String *buff, bool is_const) {
String s = newString(20 + strlen(comm_space));
String *buff1 = newStringP();//array (type)
String *buff2 = newStringP();//init store
if (range->next == NULL) {
genGlobal(VarDef, buff, is_const);
return;
}
//reserved id
int *id_mark = malloc(sizeof(int) * 2);
id_mark[0] = mark++;
id_mark[1] = is_const;
//check type
int align = 4;
if (VarDef->i > 0 && strcmp(VarDef->beans[0]->type, "ConstExps") == 0) {
// array type
Bean ConstExps = VarDef->beans[0];
int *array = genArrayExps(ConstExps, buff1);
if (array == NULL) {
printf("Error-VarDef '%s' array define should with const size value\n", VarDef->value);
exit(0);
}
//save ArrayExps and %address to range
//first line--address;second line--element
int **var_arr = malloc(sizeof(int *) * 2);
var_arr[0] = id_mark;
var_arr[1] = array;
putData(range->element, VarDef->value, var_arr);
align = 16;
//memset array
// %3 = bitcast [10 x [10 x i32]]* %2 to i8*
// call void @llvm.memset.p0i8.i64(i8* align 16 %3, i8 0, i64 400, i1 false)
String memset_buff = newString(1000);
sprintf(memset_buff + strlen(memset_buff),
" %%%d = bitcast %s* %%%d to i8*\n", mark++, *buff1, *id_mark);
int size = 0;//size for memset
for (int i = 1; i <= ConstExps->i; ++i) size += array[i] * 4;
sprintf(memset_buff + strlen(memset_buff),
" call void @llvm.memset.p0i8.i64(i8* align 16 %%%d, i8 0, i64 %d, i1 false)\n", mark - 1, size);
mystrcat(buff2, memset_buff);
//handle initval
if (VarDef->i == 2) {
Bean InitVal = VarDef->beans[1];
genArrayInits(InitVal, buff2, VarDef->value, ConstExps->i);
}
} else {
//regular i32
mystrcat(buff1, "i32");
int *arg = NULL;
if (VarDef->i != 0) {//has initval
//int init has to be exp
Bean InitVal = VarDef->beans[0];
if (InitVal->value != NULL) {
printf("Error-VarDef '%s' init value should be INT NUMBER", VarDef->value);
exit(0);
}
arg = genExps(InitVal->beans[0], buff2);
String init = newString(100);
if (arg != NULL) {
sprintf(init, " store i32 %d, i32* %%%d, align 4\n", *arg, *id_mark);
} else {
sprintf(init, " store i32 %%%d, i32* %%%d, align 4\n", mark - 1, *id_mark);
}
mystrcat(buff2, init);
}
//save %address and 0 to range
//first line--address;second line--element
int **var_arr = malloc(sizeof(int *) * 2);
var_arr[0] = id_mark;
var_arr[1] = malloc(sizeof(int));
var_arr[1][0] = 0;
if (is_const) {
if (arg == NULL) {
printf("Error-const '%s' init value has to be compile-time value", VarDef->value);
exit(0);
}
var_arr[0][0] = *arg;
}
// else {
// //nothing
// }
putData(range->element, VarDef->value, var_arr);
}
sprintf(s, "%%%d", *id_mark);
s = genAlloca(s, *buff1, align);
sprintf(s + strlen(s), "%s; define %s\n", comm_space, VarDef->value);
bool out = buff == NULL;
if (out) buff = newStringP();
mystrcat(buff, s);
mystrcat(buff, *buff2);
if (out)
fprintf(genOut, "%s", *buff);
freeBuff(buff1);
freeBuff(buff2);
}
void genGlobal(Bean VarDef, String *buff, bool is_const) {
String *s_buff = newStringP();
String *buff1 = newStringP();
//@gci = dso_local constant i32 1, align 4
//@gi = dso_local global i32 2, align 4
//@glarr = dso_local global [2 x [3 x i32]] zeroinitializer, align 16
//@cia = dso_local constant [33 x [44 x i32]] zeroinitializer, align 16
mystrcat(s_buff, "@");
mystrcat(s_buff, VarDef->value);
mystrcat(s_buff, " = dso_local ");
if (VarDef->i > 0 && strcmp(VarDef->beans[0]->type, "ConstExps") == 0) {
// array type
mystrcat(s_buff, "global ");
Bean ConstExps = VarDef->beans[0];
int *array = genArrayExps(ConstExps, buff1);
if (array == NULL) {
printf("Error-VarDef '%s' array define should with const size value\n", VarDef->value);
exit(0);
}
//save init wait to main
if (VarDef->i == 2) {
Bean InitVal = VarDef->beans[1];
if (glo_arr_i == 0)
glo_arr = malloc(sizeof(struct glo_init));
else
glo_arr = realloc(glo_arr, sizeof(struct glo_init) * (glo_arr_i + 1));
glo_arr[glo_arr_i].Init = InitVal;
glo_arr[glo_arr_i].var_name = VarDef->value;
glo_arr[glo_arr_i].layer = ConstExps->i;
glo_arr[glo_arr_i].is_const = is_const;
glo_arr_i++;
}
//save to map
int **var_arr = malloc(sizeof(int *) * 2);
var_arr[0] = malloc(sizeof(int) * 2);
var_arr[0][0] = -1;
var_arr[0][1] = VarDef->i == 2 ? 0 : is_const;//global const special store
var_arr[1] = array;
putData(range->element, VarDef->value, var_arr);
mystrcat(s_buff, *buff1);
mystrcat(s_buff, " zeroinitializer, align 16\n");
} else {
//difference only i32 type
if (is_const)
mystrcat(s_buff, "constant ");
else
mystrcat(s_buff, "global ");
//i32 type
int init_val = 0;
if (VarDef->i > 0) {
Bean InitVal = VarDef->beans[0];
if (InitVal->value != NULL) {
printf("Error-Global VarDef '%s' init value should be INT NUMBER", VarDef->value);
exit(0);
}
int *arg = genExps(InitVal->beans[0], NULL);
if (arg == NULL) {
printf("Error-const '%s' init value has to be compile-time value", VarDef->value);
exit(0);
} else
init_val = *arg;
}
mystrcat(s_buff, "i32 ");
mystrcat(s_buff, itos(init_val));
mystrcat(s_buff, ", align 4\n");
//save to map
int **var_arr = malloc(sizeof(int *) * 2);
var_arr[0] = malloc(sizeof(int) * 2);//global
var_arr[1] = malloc(sizeof(int));
var_arr[0][0] = -1;
var_arr[0][1] = is_const;
var_arr[1][0] = 0;
putData(range->element, VarDef->value, var_arr);
}
bool out = buff == NULL;
if (out) buff = newStringP();
mystrcat(buff, *s_buff);
if (out)
fprintf(genOut, "%s", *buff);
freeBuff(s_buff);
freeBuff(buff1);
}
String genAlloca(String lval, String type, int align) {
String s = newString(strlen(lval) + strlen(type) + 100);
sprintf(s, " %s = alloca %s, align %d", lval, type, align);
return s;
}
void genArrayInits(Bean InitVal, String *buff, String var_name, int layer) {
int *array = malloc(sizeof(int) * (layer + 1));
memset(array, 0, sizeof(int) * (layer + 1));
array[0] = layer;
genArrayInit(InitVal, buff, var_name, array, 0);
}
void genArrayInit(Bean InitVal, String *buff, String var_name, int *array, int stack) {
if (InitVal->i == 0) {//{}
array[stack]++;
return;
}
if (InitVal->value == NULL || strcmp(InitVal->value, "{}") != 0) {//Exp generate
Bean Stmt = beanInfo("Stmt", "assign");
Bean LVal = beanInfo("LVal", var_name);
Bean Exps = beanInfo("Exps", NULL);
for (int i = 1; i <= array[0]; ++i) {
Bean Number = beanInfo("Number", NULL);
Number->iValue = array[i];
addBean(Exps, Number);
}
addBean(LVal, Exps);
Bean Exp = InitVal->beans[0];
addBean(Stmt, LVal);
addBean(Stmt, Exp);
genStmt(Stmt, buff);
array[array[0]]++;//end ++
return;
}
stack++;
Bean InitVals = InitVal->beans[0];
for (int i = 0; i < InitVals->i; ++i) {
genArrayInit(InitVals->beans[i], buff, var_name, array, stack);
}
//exit layer
for (int i = stack; i <= array[0]; ++i)
array[i] = 0;
stack--;
//pre layer stack move next
if (stack != 0)
array[stack]++;
}
void genFuncDef(Bean FuncFuncDef, String *buff) {
String *buff_s = newStringP();
String *buff1 = newStringP();//para
String *buff2 = newStringP();//block
String func_end = "}\n";
//set mark 0
mark = 0;
int **type = (int **) malloc(sizeof(int *));
*type = (int *) malloc(sizeof(int));
**type = (strcmp(FuncFuncDef->type, "FuncDef-int") == 0);
if (!putData(funcType, FuncFuncDef->value, type)) {
printf("Error-redefine function: '%s'", FuncFuncDef->value);
exit(0);
}
mystrcat(buff_s, "\n; Function Attrs: noinline nounwind optnone uwtable\ndefine dso_local ");
if (**type)
mystrcat(buff_s, "i32 @");
else
mystrcat(buff_s, "void @");
mystrcat(buff_s, FuncFuncDef->value);
Bean Block;
if (FuncFuncDef->i == 1) {
int *para = (int *) malloc(sizeof(int));
*para = 0;
putData(funcPara, FuncFuncDef->value, ¶);
mystrcat(buff_s, "() #0 {\n");
Block = FuncFuncDef->beans[0];
} else {
putData(funcPara, FuncFuncDef->value, genFuncFParams(FuncFuncDef->beans[0], buff1));
mystrcat(buff_s, *buff1);
//genFuncFParams will generate #0 {... because para need to pre-'redefine'
// mystrcat(buff_s, " #0 {\n");
Block = FuncFuncDef->beans[1];
}
mark++;
//add return to function end if necessary
Bean BlockItems = Block->beans[0];
if (BlockItems->i == 0//empty block or last not return
|| strcmp(toString(BlockItems->beans[BlockItems->i - 1]->beans[0]), "Stmt(return)") != 0) {
if (**type) {
printf("warning: non-void function '%s' does not return a value\n", FuncFuncDef->value);
func_end = " ret i32 0\n}\n\n";
} else
func_end = " ret void\n}\n\n";
}
//generate global arr first
if (strcmp(FuncFuncDef->value, "main") == 0) {
for (int i = 0; i < glo_arr_i; ++i) {
genArrayInits(glo_arr[i].Init, buff2, glo_arr[i].var_name, glo_arr[i].layer);
}
}
genBlock(Block, buff2);
bool out = buff == NULL;
if (out) buff = newStringP();
mystrcat(buff, *buff_s);
mystrcat(buff, *buff2);
mystrcat(buff, func_end);
if (out) fprintf(genOut, "%s", *buff);
freeBuff(buff_s);
freeBuff(buff1);
freeBuff(buff2);
}
int **genFuncFParams(Bean FuncFParams, String *buff) {
String *buff_s = newStringP();
String *buff1 = newStringP();
String *buff2 = newStringP();//generate param in block
mystrcat(buff_s, "(");
int **paras = (int **) malloc(sizeof(int *) * (FuncFParams->i + 1));
paras[0] = &FuncFParams->i;
for (int i = 0; i < FuncFParams->i; ++i) {
paras[i + 1] = genFuncFParam(FuncFParams->beans[i], buff1);
mystrcat(buff_s, *buff1);
//store to range
int **var_arr = malloc(sizeof(int *) * 2);
var_arr[0] = malloc(sizeof(int) * 2);
var_arr[0][0] = mark + FuncFParams->i;
var_arr[0][1] = false;
var_arr[1] = paras[i + 1];
putData(range->element, FuncFParams->beans[i]->value, var_arr);
//generate func para in block read
//divide type
String *buff1_clone = newStringP();
mystrcat(buff1_clone, *buff1);
String type = *buff1_clone;
char *p = type + strlen(type);
while (*p != ' ') p--;
*p = '\0';
String lval = newString(20);
int align = *(p - 1) == '*' ? 8 : 4;
sprintf(lval, "%%%d", mark + FuncFParams->i);
mystrcat(buff2, genAlloca(lval, type, align));
//store [2 x i32]* %0, [2 x i32]** %6, align 8
mystrcat(buff2, "\n store ");
mystrcat(buff2, *buff1);
mystrcat(buff2, ", ");
mystrcat(buff2, type);
mystrcat(buff2, "* ");
mystrcat(buff2, lval);
mystrcat(buff2, ", align ");
mystrcat(buff2, itos(align));
mystrcat(buff2, "\n");
freeBuff(buff1);
if (i == FuncFParams->i - 1)
mystrcat(buff_s, ")");
else
mystrcat(buff_s, ", ");
}
mystrcat(buff_s, " #0 {\n");
mark += FuncFParams->i;
bool out = buff == NULL;
if (out) buff = newStringP();
mystrcat(buff, *buff_s);
mystrcat(buff, *buff2);
if (out) fprintf(genOut, "%s", *buff);
freeBuff(buff_s);
freeBuff(buff1);
freeBuff(buff2);
return paras;
}
int *genFuncFParam(Bean FuncFParam, String *buff) {
String *buff_s = newStringP();
String *buff1 = newStringP();
int *array = NULL;
if (FuncFParam->i == 0) {//i32
mystrcat(buff_s, "i32 %");
} else if (FuncFParam->beans[0]->i == 0) {//i32*
mystrcat(buff_s, "i32* %");
array = malloc(sizeof(int));
*array = 0;
} else {//array
array = genArrayExps(FuncFParam->beans[0], buff1);
if (array == NULL) {
printf("Error-FuncFParam '%s' define should with const value", FuncFParam->value);
exit(0);
}
mystrcat(buff_s, *buff1);
mystrcat(buff_s, "* %");
}
mystrcat(buff_s, itos(mark++));
bool out = buff == NULL;
if (out) buff = newStringP();
mystrcat(buff, *buff_s);
if (out) fprintf(genOut, "%s", *buff);
freeBuff(buff_s);
freeBuff(buff1);
return array;
}
void genBlock(Bean Block, String *buff) {
range = newList(range);
genBlockItems(Block->beans[0], buff);
range = exitRange(range);
}
void genBlockItems(Bean BlockItems, String *buff) {
for (int i = 0; i < BlockItems->i; ++i) {
genBlockItem(BlockItems->beans[i], buff);
}
}
void genBlockItem(Bean BlockItem, String *buff) {
if (strcmp(BlockItem->value, "Decl") == 0)
genDecl(BlockItem->beans[0], buff);
else if (strcmp(BlockItem->value, "Stmt") == 0)
genStmt(BlockItem->beans[0], buff);
else {
printf("Error-BlockItem dose not match value:%s", BlockItem->value);
exit(0);
}
}
void genIf(Bean If, String *buff) {
String s1 = newString(100 + strlen(comm_space));//if start
String s2 = newString(100 + strlen(comm_space));//if br end and end:
String *buff1 = newStringP();
String *buff2 = newStringP();
int *cond_result = genCond(If->beans[0], buff1);
if (cond_result == NULL) {
sprintf(s1 + strlen(s1), "t%d:%s;if-body\n", ti, comm_space);
sprintf(s2 + strlen(s2), " br label %%f%d\n\n", fi);
sprintf(s2 + strlen(s2), "f%d:%s;if-end\n", fi, comm_space);
ti++;
fi++;
} else if (*cond_result) {//always true
printf("warning:expression always true\n");
} else {//always false
printf("warning:expression always false can't reach the if-body sentence\n");
return;
}
genStmt(If->beans[1], buff2);
bool out = buff == NULL;
if (out) buff = newStringP();
mystrcat(buff, *buff1);
mystrcat(buff, s1);
mystrcat(buff, *buff2);
mystrcat(buff, s2);
if (out) fprintf(genOut, "%s", *buff);
freeBuff(buff1);
freeBuff(buff2);
}
void genIfElse(Bean IfElse, String *buff) {
String s1 = newString(100 + strlen(comm_space));//if start
String s2 = newString(100);//if br end
String s3 = newString(100 + strlen(comm_space));//else start
String s4 = newString(100 + strlen(comm_space));//else br end and end:
String *buff1 = newStringP();//cond
String *buff2 = newStringP();//if-body
String *buff3 = newStringP();//else-body
int *cond_result = genCond(IfElse->beans[0], buff1);
if (cond_result == NULL) {
sprintf(s1 + strlen(s1), "t%d:%s;if-body\n", ti, comm_space);
sprintf(s3 + strlen(s3), "f%d:%s;else-body\n", fi, comm_space);
ti++;
fi++;
genStmt(IfElse->beans[1], buff2);//if-body
genStmt(IfElse->beans[2], buff3);//else-body
sprintf(s2 + strlen(s2), " br label %%e%d\n\n", ei);//if br end
sprintf(s4 + strlen(s4), " br label %%e%d\n\n", ei);//else br end
sprintf(s4 + strlen(s4), "e%d:%s;if-else-end\n", ei, comm_space);
ei++;
ti++;
fi++;
} else if (*cond_result) {//always true
printf("warning:expression always true can't reach the else-body sentence\n");
genStmt(IfElse->beans[1], buff2);//if-body
} else {//always false
printf("warning:expression always false can't reach the if-body sentence\n");
genStmt(IfElse->beans[2], buff3);//else-body
}
bool out = buff == NULL;
if (out) buff = newStringP();
mystrcat(buff, *buff1);
mystrcat(buff, s1);
mystrcat(buff, *buff2);
mystrcat(buff, s2);
mystrcat(buff, s3);
mystrcat(buff, *buff3);
mystrcat(buff, s4);
if (out) fprintf(genOut, "%s", *buff);
freeBuff(buff1);
freeBuff(buff2);
freeBuff(buff3);
}
void genWhile(Bean While, String *buff) {
String s1 = newString(100);//while br cond
String s2 = newString(100 + strlen(comm_space));//while
String s3 = newString(100 + strlen(comm_space));//while br cond and end:
String *buff1 = newStringP();
String *buff2 = newStringP();
//while special br to set start(continue-ues)
sprintf(s1 + strlen(s1), " br label %%%d\n\n%d:%s; while start\n", mark, mark, comm_space);
int cond_start = mark++;
int *cond_result = genCond(While->beans[0], buff1);
if (cond_result == NULL) {
sprintf(s2 + strlen(s2), "t%d:%s; while-body\n", ti, comm_space);
ti++;
} else if (*cond_result) {
//no condition no label
sprintf(s2 + strlen(s2), "%s; while-body\n", comm_space);
} else {
printf("warning:expression always false can't reach the while-body sentence\n");
return;
}
sprintf(s3 + strlen(s3), " br label %%%d\n\n", cond_start);
sprintf(s3 + strlen(s3), "f%d:%s; while-end\n", fi, comm_space);
start = malloc(20);
end = malloc(20);
sprintf(start, "%%%d", cond_start);
sprintf(end, "%%f%d", fi);
fi++;
genStmt(While->beans[1], buff2);
bool out = buff == NULL;
if (out) buff = newStringP();
mystrcat(buff, s1);
mystrcat(buff, *buff1);
mystrcat(buff, s2);
mystrcat(buff, *buff2);
mystrcat(buff, s3);
if (out) fprintf(genOut, "%s", *buff);
freeBuff(buff1);
freeBuff(buff2);
}
int *genCond(Bean Cond, String *buff) {
String s = newString(1000);
String *buff1 = newStringP();
if (strcmp(Cond->beans[0]->type, "LVal") == 0) {
printf("Error-please set condition with \"'id' != 0\" or \"'id' == 0\" when call id:%s",
Cond->beans[0]->value);
exit(0);
}
cond = true;
int *arg = genExps(Cond->beans[0], buff1);
cond = false;
if (*Cond->beans[0]->value != '&' && *Cond->beans[0]->value != '|') {
//unit(add br)
sprintf(s + strlen(s), " br i1 %%%d, label %%t%d, label %%f%d\n\n", mark - 1, ti, fi);
}
if (arg != NULL) return arg;
bool out = buff == NULL;
if (out) buff = newStringP();
mystrcat(buff, *buff1);
mystrcat(buff, s);
if (out) fprintf(genOut, "%s", *buff);
freeBuff(buff1);
return NULL;
}
void genStmt(Bean Stmt, String *buff) {
String s = newString(1000 + strlen(comm_space));
if (strcmp(Stmt->type, "Stmt") != 0) {
printf("Stmt type not match: '%s'", toString(Stmt));
exit(0);
}
if (strcmp(Stmt->value, "assign") == 0) {
int *arg = genExps(Stmt->beans[1], buff);
int pre_mark = mark - 1;
String var_p = genLVal(Stmt->beans[0], buff, 0);
if (arg == NULL)
sprintf(s + strlen(s), " store i32 %%%d, ", pre_mark);
else
sprintf(s + strlen(s), " store i32 %d, ", *arg);
if (var_p == NULL)//pointer
sprintf(s + strlen(s), "i32* %%%d, align 4\n", mark - 1);
else//id
sprintf(s + strlen(s), "i32* %s, align 16\n", var_p);
} else if (strcmp(Stmt->value, "Exp") == 0) {
genExps(Stmt->beans[0], buff);
} else if (strcmp(Stmt->value, "block") == 0) {
genBlock(Stmt->beans[0], buff);
} else if (strcmp(Stmt->value, "if") == 0) {
genIf(Stmt, buff);
} else if (strcmp(Stmt->value, "if-else") == 0) {
genIfElse(Stmt, buff);
} else if (strcmp(Stmt->value, "while") == 0) {
in_while = true;
genWhile(Stmt, buff);
in_while = false;
} else if (strcmp(Stmt->value, "break") == 0) {
if (!in_while) errorPrint("'break' only use in while");
sprintf(s + strlen(s), " br label %s%s; break\n", end, comm_space);
mark++;
} else if (strcmp(Stmt->value, "continue") == 0) {
if (!in_while) errorPrint("'continue' only use in while");
sprintf(s + strlen(s), " br label %s%s; continue\n", start, comm_space);
mark++;
} else if (strcmp(Stmt->value, "return") == 0) {
if (Stmt->i == 0)
sprintf(s + strlen(s), " ret void\n");
else {
int *arg = genExps(Stmt->beans[0], buff);
if (arg != NULL)
sprintf(s + strlen(s), " ret i32 %d\n", *arg);
else
sprintf(s + strlen(s), " ret i32 %%%d\n", mark - 1);
}
}
bool out = buff == NULL;
if (out) buff = newStringP();
mystrcat(buff, s);
if (out) fprintf(genOut, "%s", *buff);
}
/**
* @return array of ArrayExps [0] length [...] element
*/
int *genArrayExps(Bean ArrayExps, String *buff) {
String array_str = newString(20 * ArrayExps->i);
int *array = malloc(sizeof(int) * (ArrayExps->i + 1));
array[0] = ArrayExps->i;
for (int j = 0; j < ArrayExps->i; ++j) {
int *arg = genExps(ArrayExps->beans[j], buff);
if (arg == NULL) {
// free(array_str);
return NULL;
}
array[j + 1] = *arg;
sprintf(array_str + strlen(array_str), "[%d x ", *arg);
}
sprintf(array_str + strlen(array_str), "i32");
for (int j = 0; j < ArrayExps->i; ++j)
sprintf(array_str + strlen(array_str), "]");
bool out = buff == NULL;
if (out) buff = newStringP();
mystrcat(buff, array_str);
if (out) fprintf(genOut, "%s", *buff);
return array;
}
/**
* LVal has two usage 1:store 2.read
* \n set 0 - store 1 - read
* @param LVal LVal Bean
* @param io 0 - store 1 - read
* @return String of \@i or %i(only store available) or const int String(only read available)
*/
String genLVal(Bean LVal, String *buff, bool io) {
String s = newString(1000);
String *buff1 = newStringP();//pointer read
if (strcmp(LVal->type, "LVal") != 0) errorPrint("LVal type not match");
//find var in range
int **value = getVarValue(LVal->value);
//array can not beyond initial size
int count = LVal->i == 0 ? 0 : LVal->beans[0]->i;
int original_count = value[1] == NULL ? 0 : value[1][0];
if (pointer == 0 && count != original_count) {
printf("Error-value of lval:%s should be int", LVal->value);
exit(0);
}
if (pointer != 0 && pointer != original_count - count) {
printf("Error-array type '%s' dose not match function type", LVal->value);
exit(0);
}
pointer = 0;//consume pointer
//generate code
//get var name(global) or address(local)
String var_p = malloc(20 + strlen(LVal->value));
if (value[0][0] == -1) {
//global
sprintf(var_p, "@%s", LVal->value);
} else {
//local
sprintf(var_p, "%%%d", **value);
}
bool add_load = true;
if (value[0][1]) {//const
if (value[1][0] == 0) {
//i32 const
if (!io) {
printf("Error-const '%s' is not assignable\n", LVal->value);
exit(0);
} else {
return itos(value[0][0]);
}
} else {
//array const
if (!io) {
printf("Error-const array '%s' is not assignable\n", LVal->value);
exit(0);
}
}
}
if (value[1][0] == 0) {//0 element when define
if (io) {//read
sprintf(s + strlen(s), " %%%d = load i32, i32* %s, align 4\n", mark++, var_p);
}
//store do nothing
} else {//array define
//%4 = getelementptr inbounds [22 x [33 x [44 x i32]]], [22 x [33 x [44 x i32]]]* %2, i64 0, i64 11
//%5 = getelementptr inbounds [33 x [44 x i32]], [33 x [44 x i32]]* %4, i64 0, i64 23
//%6 = getelementptr inbounds [44 x i32], [44 x i32]* %5, i64 0, i64 34
Bean Exps = LVal->beans[0];
int *pre_mark = malloc(sizeof(int) * Exps->i);
int **args = malloc(sizeof(int *) * (Exps->i + 2));//remain for addition
for (int i = 0; i < Exps->i; ++i) {
args[i] = genExps(Exps->beans[i], buff);
pre_mark[i] = mark - 1;
}
//if pointer not match add an 0
if (Exps->i < value[1][0]) {
args[count] = malloc(sizeof(int));
*args[count] = 0;
count++;
add_load = false;
}
for (int i = 0; i < count; ++i) {
String mark_str = newString(20);
sprintf(mark_str, " %%%d", mark++);
mystrcat(buff1, mark_str);
// free(mark_str);
mystrcat(buff1, " = getelementptr inbounds ");
String array_str = newString(20 * value[1][0]);
for (int j = i + 1; j <= value[1][0]; ++j)
sprintf(array_str + strlen(array_str), "[%d x ", value[1][j]);
sprintf(array_str + strlen(array_str), "i32");
for (int j = i + 1; j <= value[1][0]; ++j)
sprintf(array_str + strlen(array_str), "]");
mystrcat(buff1, array_str);
mystrcat(buff1, ", ");
mystrcat(buff1, array_str);
mystrcat(buff1, "* ");
// free(array_str);
String end_call = newString(100);
if (i == 0)//call the id of var
sprintf(end_call + strlen(end_call), "%s, ", var_p);
else//pre cal all arg so mark-2(include this line ++) work finely
sprintf(end_call + strlen(end_call), "%%%d, ", mark - 2);
if (args[i] != NULL)
sprintf(end_call + strlen(end_call), "i32 0, i32 %d\n", *args[i]);
else
sprintf(end_call + strlen(end_call), "i32 0, i32 %%%d\n", pre_mark[i]);
mystrcat(buff1, end_call);
// free(end_call);
}
if (io) {
//read
//load from pointer
if (add_load) {
sprintf(s + strlen(s), " %%%d = load i32, i32* %%%d, align 4\n", mark, mark - 1);
mark++;
}
} else {
//store update var_p to last pointer
//notify store to use mark-1 pointer
// free(var_p);
var_p = NULL;
}
}
bool out = buff == NULL;
if (out) buff = newStringP();
mystrcat(buff, *buff1);
mystrcat(buff, s);
if (out) fprintf(genOut, "%s", *buff);
freeBuff(buff1);
if (io)
return NULL;
else
return var_p;
}
/**
* generate code for every exp
* @param !Exp !Exps !PrimaryExp Number LVal UnaryExp MulExp AddExp RelExp EqExp !LAndExp !LOrExp !ConstExps !ConstExp
* @return result pointer
*/
int *genExps(Bean Exp, String *buff) {
//no output!!
if (Exp == NULL || Exp->type == NULL) return NULL;
if (strcmp(Exp->type, "Number") == 0) {
int *result = (int *) malloc(sizeof(int));
*result = Exp->iValue;
return result;
}
if (strcmp(Exp->type, "LVal") == 0) {//in exp LVal to read
String str = genLVal(Exp, buff, 1);
if (str != NULL) {//const LVal
int *const_result = malloc(sizeof(int));
*const_result = atoi(str);
return const_result;
}
return NULL;
}
if (strcmp(Exp->type, "UnaryExp") == 0)
return genUnaryExp(Exp, buff);
if (strcmp(Exp->type, "UnaryExp-function") == 0) {
genUnaryExpFunc(Exp, buff);
return NULL;
}
if (strcmp(Exp->type, "MulExp") == 0)
return genMulExp(Exp, buff);
if (strcmp(Exp->type, "AddExp") == 0)
return genAddExp(Exp, buff);
if (strcmp(Exp->type, "RelExp") == 0)
return genRelExp(Exp, buff);
if (strcmp(Exp->type, "EqExp") == 0)
return genEqExp(Exp, buff);
if (strcmp(Exp->type, "LAndExp") == 0)
return genLAndExp(Exp, buff);
if (strcmp(Exp->type, "LOrExp") == 0)
return genLOrExp(Exp, buff);
printf("Error-Exp type dose not match '%s'", Exp->type);
exit(0);
}
int *genUnaryExp(Bean UnaryExp, String *buff) {
String s = newString(100);
int *arg = genExps(UnaryExp->beans[0], buff);
if (arg != NULL) {
if (*UnaryExp->value == '-')
*arg = -*arg;
else if (*UnaryExp->value == '!')
*arg = (*arg == 0);
return arg;
}
//generate code
sprintf(s + strlen(s), " %%%d = ", mark);
mark++;
if (*UnaryExp->value == '-') {
sprintf(s + strlen(s), "sub nsw i32 0, %%%d\n", mark - 1);
} else if (*UnaryExp->value == '!') {
//only legal in condition
if (!cond)
errorPrint("'!' can only be used in Condition");
sprintf(s + strlen(s), "icmp ne i32 %%%d, 0\n", mark - 1);
}
bool out = buff == NULL;
if (out) buff = newStringP();
mystrcat(buff, s);
if (out) fprintf(genOut, "%s", *buff);
return arg;
}
void genUnaryExpFunc(Bean UnaryExp, String *buff) {
String *buff_s = newStringP();
String *buff1 = newStringP();//(...)
String *buff2 = newStringP();//exp
if (UnaryExp->value == NULL)
errorPrint("func name is null");
//check function name exist
int ***para = getValue(funcPara, UnaryExp->value);
int ***type = getValue(funcType, UnaryExp->value);
if (para == NULL || type == NULL) {//function not found
printf("Error-function:%s not define", UnaryExp->value);
exit(0);
}
//get the count of real para
int r_count = UnaryExp->i == 0 ? 0 : UnaryExp->beans[0]->i;
if (***para != r_count) {
printf("Error-function:%s param num not match:%d-%d", UnaryExp->value, ***para, r_count);