-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr.c
1434 lines (1360 loc) · 39.5 KB
/
expr.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 "expr.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "scope.h"
#include "register.h"
#include "symbol.h"
extern int str_no;
extern struct symbol *cur_func;
extern int ctl_no;
struct expr *expr_create(expr_t kind, struct expr *left, struct expr *right, int line) {
struct expr *e = (struct expr *)malloc(sizeof(struct expr));
if(!e) {
fprintf(stdout, "malloc fails!\n");
exit(EXIT_FAILURE);
}
e->kind = kind;
e->symbol = 0;
e->left = left;
e->right = right;
e->line = line;
e->is_global = 0;
e->reg = -1;
return e;
}
struct expr *expr_create_name(const char *n, int line) {
struct expr *e = expr_create(EXPR_IDENT_NAME, 0, 0, line);
e->name = n;
return e;
}
struct expr *expr_create_boolean_literal(int c, int line) {
struct expr *e = expr_create(EXPR_BOOLEAN_LITERAL, 0, 0, line);
e->literal_value = c;
return e;
}
struct expr *expr_create_integer_literal(int c, int line) {
struct expr *e = expr_create(EXPR_INTEGER_LITERAL, 0, 0, line);
e->literal_value = c;
return e;
}
struct expr *expr_create_character_literal(const char *str, int line) {
struct expr *e = expr_create(EXPR_CHARACTER_LITERAL, 0, 0, line);
e->string_literal = str;
return e;
}
struct expr *expr_create_string_literal(const char *str, int line) {
struct expr *e = expr_create(EXPR_STRING_LITERAL, 0, 0, line);
e->string_literal = str;
return e;
}
void expr_print(struct expr *e) {
if(!e) return;
switch(e->kind) {
case EXPR_LEFTCURLY:
printf("{");
expr_print(e->right);
printf("}");
break;
case EXPR_LEFTPARENTHESS:
expr_print(e->left);
printf("(");
expr_print(e->right);
printf(")");
break;
case EXPR_LEFTBRACKET:
expr_print(e->left);
printf("[");
expr_print(e->right);
printf("]");
break;
case EXPR_INCREMENT:
printf("(");
expr_print(e->left);
printf("++");
printf(")");
break;
case EXPR_DECREMENT:
printf("(");
expr_print(e->left);
printf("--");
printf(")");
break;
case EXPR_UNARY_NEG:
printf("(");
printf("-");
expr_print(e->right);
printf(")");
break;
case EXPR_NOT:
printf("(");
printf("!");
expr_print(e->right);
printf(")");
break;
case EXPR_POWER:
printf("(");
expr_print(e->left);
printf("^");
expr_print(e->right);
printf(")");
break;
case EXPR_MUL:
printf("(");
expr_print(e->left);
printf("*");
expr_print(e->right);
printf(")");
break;
case EXPR_DIV:
printf("(");
expr_print(e->left);
printf("/");
expr_print(e->right);
printf(")");
break;
case EXPR_MOD:
printf("(");
expr_print(e->left);
printf("%%");
expr_print(e->right);
printf(")");
break;
case EXPR_ADD:
printf("(");
expr_print(e->left);
printf("+");
expr_print(e->right);
printf(")");
break;
case EXPR_SUB:
printf("(");
expr_print(e->left);
printf("-");
expr_print(e->right);
printf(")");
break;
case EXPR_LE:
printf("(");
expr_print(e->left);
printf("<=");
expr_print(e->right);
printf(")");
break;
case EXPR_LT:
printf("(");
expr_print(e->left);
printf("<");
expr_print(e->right);
printf(")");
break;
case EXPR_GE:
printf("(");
expr_print(e->left);
printf(">=");
expr_print(e->right);
printf(")");
break;
case EXPR_GT:
printf("(");
expr_print(e->left);
printf(">");
expr_print(e->right);
printf(")");
break;
case EXPR_EQ:
printf("(");
expr_print(e->left);
printf("==");
expr_print(e->right);
printf(")");
break;
case EXPR_UNEQ:
printf("(");
expr_print(e->left);
printf("!=");
expr_print(e->right);
printf(")");
break;
case EXPR_AND:
printf("(");
expr_print(e->left);
printf("&&");
expr_print(e->right);
printf(")");
break;
case EXPR_OR:
printf("(");
expr_print(e->left);
printf("||");
expr_print(e->right);
printf(")");
break;
case EXPR_ASSIGN:
printf("(");
expr_print(e->left);
printf("=");
expr_print(e->right);
printf(")");
break;
case EXPR_COMMA:
expr_print(e->left);
printf(",");
expr_print(e->right);
break;
case EXPR_IDENT_NAME:
printf("%s", e->name);
break;
case EXPR_BOOLEAN_LITERAL:
if(e->literal_value)
printf("true");
else
printf("false");
break;
case EXPR_INTEGER_LITERAL:
printf("%d", e->literal_value);
break;
case EXPR_CHARACTER_LITERAL: /*FIXME: need more work, print using the function in token.c */
printf("%s", e->string_literal);
break;
case EXPR_STRING_LITERAL: /*FIXME: need more work, print using the function in token.c */
printf("%s", e->string_literal);
break;
}
return;
}
void expr_resolve(struct expr *e) {
if(!e) return;
expr_resolve(e->left);
expr_resolve(e->right);
if(e->kind == EXPR_IDENT_NAME) {
e->symbol = scope_lookup(e->name, e->line, 1);
}
}
int expr_is_constant(struct expr *e) {
if(!e) return 1;
if(e->kind == EXPR_IDENT_NAME) {
return 0;
} else {
return expr_is_constant(e->left) && expr_is_constant(e->right);
}
}
struct type *expr_typecheck(struct expr *e, int is_array_initializer, int silent_mode) {
if(!e) return 0;
struct type *left = 0;
struct type *right = 0;
switch(e->kind) {
case EXPR_LEFTCURLY:
type_free(left);
type_free(right);
return type_create(TYPE_ARRAY, 0, expr_create_integer_literal(expr_count_item(e->right), e->line), expr_typecheck(e->right, is_array_initializer, silent_mode), e->line, 0);
break;
case EXPR_LEFTPARENTHESS:
if(e->left) {
//function call
//check function call arguments and function definition paramters
expr_func_typecheck(e, silent_mode);
type_free(left);
type_free(right);
return scope_lookup(e->left->name, e->left->line, 0)->type->subtype;
} else {
//grouping
type_free(left);
type_free(right);
return expr_typecheck(e->right, is_array_initializer, silent_mode);
}
break;
case EXPR_LEFTBRACKET:
left = expr_typecheck(e->left, is_array_initializer, silent_mode);
right = expr_typecheck(e->right, is_array_initializer, silent_mode);
//the left child should be type array
if(left->kind != TYPE_ARRAY) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): %s is not an array, and can not be indexed.\n", e->left->line, e->left->name);
type_error_count += 1;
}
type_free(right);
return left;
}
//the right child should be type integer
if(right->kind != TYPE_INTEGER) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): the index (", e->left->line);
expr_print(e->right);
printf(") of an array (%s) has wrong type (", e->left->name);
type_print(right);
printf("), and should be integer!\n");
type_error_count += 1;
}
}
type_free(right);
return left->subtype;
break;
case EXPR_INCREMENT:
case EXPR_DECREMENT:
left = expr_typecheck(e->left, is_array_initializer, silent_mode);
if(left->kind != TYPE_INTEGER) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): ++/-- expr only applys to integer types: the type of expr (", e->line);
expr_print(e->left);
printf(") is ");
type_print(left);
printf("!\n");
type_error_count += 1;
}
type_free(left);
type_free(right);
return type_create(TYPE_INTEGER, 0, 0, 0, e->line, 0);
}
if(expr_is_constant(e->left)) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): lvalue of ++/-- expr (", e->line);
expr_print(e->left);
fprintf(stdout, ") should not be constant!\n");
type_error_count += 1;
}
}
type_free(right);
return left;
break;
case EXPR_UNARY_NEG:
right = expr_typecheck(e->right, is_array_initializer, silent_mode);
if(right->kind != TYPE_INTEGER) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): unary neg operator expr only applys to integer types: the type of expr (", e->line);
expr_print(e->right);
printf(") is ");
type_print(right);
printf("!\n");
type_error_count += 1;
}
type_free(left);
type_free(right);
return type_create(TYPE_INTEGER, 0, 0, 0, e->line, 0);
}
type_free(left);
return right;
break;
case EXPR_NOT:
right = expr_typecheck(e->right, is_array_initializer, silent_mode);
if(right->kind != TYPE_BOOLEAN) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): not operator expr only applys to boolean types: the type of expr (", e->line);
expr_print(e->right);
printf(") is ");
type_print(right);
printf("!\n");
type_error_count += 1;
}
type_free(left);
type_free(right);
return type_create(TYPE_BOOLEAN, 0, 0, 0, e->line, 0);
}
type_free(left);
return right;
break;
case EXPR_POWER:
case EXPR_MUL:
case EXPR_DIV:
case EXPR_MOD:
case EXPR_ADD:
case EXPR_SUB:
left = expr_typecheck(e->left, is_array_initializer, silent_mode);
right = expr_typecheck(e->right, is_array_initializer, silent_mode);
if(!type_equals(left, right)) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): the operands of binary arithmetic operator expr (", e->line);
expr_print(e);
printf(")mismatch: left operand (");
expr_print(e->left);
printf(") type is ");
type_print(left);
printf(", right operand (");
expr_print(e->right);
printf(") type is ");
type_print(right);
printf("!\n");
type_error_count += 1;
}
type_free(left);
type_free(right);
return type_create(TYPE_INTEGER, 0, 0, 0, e->line, 0);
} else {
if(left->kind != TYPE_INTEGER) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): the operands of binary arithmetic operator expr (", e->line);
expr_print(e);
printf(")must be integers: left operand (");
expr_print(e->left);
printf(") type is ");
type_print(left);
printf(", right operand (");
expr_print(e->right);
printf(") type is ");
type_print(right);
printf("!\n");
type_error_count += 1;
}
type_free(left);
type_free(right);
return type_create(TYPE_INTEGER, 0, 0, 0, e->line, 0);
}
type_free(right);
return left;
}
break;
case EXPR_LE:
case EXPR_LT:
case EXPR_GE:
case EXPR_GT:
left = expr_typecheck(e->left, is_array_initializer, silent_mode);
right = expr_typecheck(e->right, is_array_initializer, silent_mode);
if(!type_equals(left, right)) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): the operands of a comparison operator expr (", e->line);
expr_print(e);
printf(") mismatch: left operand (");
expr_print(e->left);
printf(") type is ");
type_print(left);
printf(", right operand (");
expr_print(e->right);
printf(") type is ");
type_print(right);
printf("!\n");
type_error_count += 1;
}
type_free(left);
type_free(right);
return type_create(TYPE_BOOLEAN, 0, 0, 0, e->line, 0);
} else {
if(left->kind != TYPE_INTEGER) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): the operands of a comparison operator expr (", e->line);
expr_print(e);
printf(") must be integers: left operand (");
expr_print(e->left);
printf(") type is ");
type_print(left);
printf(", right operand (");
expr_print(e->right);
printf(") type is ");
type_print(right);
printf("!\n");
type_error_count += 1;
}
type_free(left);
type_free(right);
return type_create(TYPE_BOOLEAN, 0, 0, 0, e->line, 0);
}
type_free(left);
type_free(right);
return type_create(TYPE_BOOLEAN, 0, 0, 0, e->line, 0);
}
break;
case EXPR_EQ:
left = expr_typecheck(e->left, is_array_initializer, silent_mode);
right = expr_typecheck(e->right, is_array_initializer, silent_mode);
if(!type_equals(left, right)) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): the operands of the == operator expr (", e->line);
expr_print(e);
printf(") mismatch: left operand (");
expr_print(e->left);
printf(") type is ");
type_print(left);
printf(", right operand (");
expr_print(e->right);
printf(") type is ");
type_print(right);
printf("!\n");
type_error_count += 1;
}
type_free(left);
type_free(right);
return type_create(TYPE_BOOLEAN, 0, 0, 0, e->line, 0);
} else {
if(left->kind == TYPE_ARRAY || left->kind == TYPE_FUNCTION) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): the == operator does not apply to array and function types:", e->line);
printf(" left operand (");
expr_print(e->left);
printf(") type is ");
type_print(left);
printf(", right operand (");
expr_print(e->right);
printf(") type is ");
type_print(right);
printf("!\n");
type_error_count += 1;
}
type_free(left);
type_free(right);
return type_create(TYPE_BOOLEAN, 0, 0, 0, e->line, 0);
}
type_free(left);
type_free(right);
return type_create(TYPE_BOOLEAN, 0, 0, 0, e->line, 0);
}
break;
case EXPR_UNEQ:
left = expr_typecheck(e->left, is_array_initializer, silent_mode);
right = expr_typecheck(e->right, is_array_initializer, silent_mode);
if(!type_equals(left, right)) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): the operands of the != operator expr (", e->line);
expr_print(e);
printf(") mismatch: left operand (");
expr_print(e->left);
printf(") type is ");
type_print(left);
printf(", right operand (");
expr_print(e->right);
printf(") type is ");
type_print(right);
printf("!\n");
type_error_count += 1;
}
type_free(left);
type_free(right);
return type_create(TYPE_BOOLEAN, 0, 0, 0, e->line, 0);
} else {
if(left->kind == TYPE_ARRAY || left->kind == TYPE_FUNCTION) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): the != operator does not apply to array and function types:", e->line);
printf(" left operand (");
expr_print(e->left);
printf(") type is ");
type_print(left);
printf(", right operand (");
expr_print(e->right);
printf(") type is ");
type_print(right);
printf("!\n");
type_error_count += 1;
}
type_free(left);
type_free(right);
return type_create(TYPE_BOOLEAN, 0, 0, 0, e->line, 0);
}
type_free(left);
type_free(right);
return type_create(TYPE_BOOLEAN, 0, 0, 0, e->line, 0);
}
break;
case EXPR_AND:
left = expr_typecheck(e->left, is_array_initializer, silent_mode);
right = expr_typecheck(e->right, is_array_initializer, silent_mode);
if(!type_equals(left, right)) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): the operands of the && operator mismatch:", e->line);
printf(" left operand (");
expr_print(e->left);
printf(") type is ");
type_print(left);
printf(", right operand (");
expr_print(e->right);
printf(") type is ");
type_print(right);
printf("!\n");
type_error_count += 1;
}
type_free(left);
type_free(right);
return type_create(TYPE_BOOLEAN, 0, 0, 0, e->line, 0);
} else {
if(left->kind != TYPE_BOOLEAN) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): the && operator only applys to boolean types:", e->line);
printf(" left operand (");
expr_print(e->left);
printf(") type is ");
type_print(left);
printf(", right operand (");
expr_print(e->right);
printf(") type is ");
type_print(right);
printf("!\n");
type_error_count += 1;
}
type_free(left);
type_free(right);
return type_create(TYPE_BOOLEAN, 0, 0, 0, e->line, 0);
}
type_free(right);
return left;
}
break;
case EXPR_OR:
left = expr_typecheck(e->left, is_array_initializer, silent_mode);
right = expr_typecheck(e->right, is_array_initializer, silent_mode);
if(!type_equals(left, right)) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): the operands of the || operator mismatch:", e->line);
printf(" left operand (");
expr_print(e->left);
printf(") type is ");
type_print(left);
printf(", right operand (");
expr_print(e->right);
printf(") type is ");
type_print(right);
printf("!\n");
type_error_count += 1;
}
type_free(left);
type_free(right);
return type_create(TYPE_BOOLEAN, 0, 0, 0, e->line, 0);
} else {
if(left->kind != TYPE_BOOLEAN) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): the || operator only applys to boolean types:", e->line);
printf(" left operand (");
expr_print(e->left);
printf(") type is ");
type_print(left);
printf(", right operand (");
expr_print(e->right);
printf(") type is ");
type_print(right);
printf("!\n");
type_error_count += 1;
}
type_free(left);
type_free(right);
return type_create(TYPE_BOOLEAN, 0, 0, 0, e->line, 0);
}
type_free(right);
return left;
}
break;
case EXPR_ASSIGN:
left = expr_typecheck(e->left, is_array_initializer, silent_mode);
right = expr_typecheck(e->right, is_array_initializer, silent_mode);
if(!type_equals(left, right)) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): the operands of the = operator mismatch:", e->line);
printf(" left operand (");
expr_print(e->left);
printf(") type is ");
type_print(left);
printf(", right operand (");
expr_print(e->right);
printf(") type is ");
type_print(right);
printf("!\n");
type_error_count += 1;
}
type_free(left);
return right;
} else {
if(left->kind == TYPE_FUNCTION) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): the = operator does not apply to function types!\n", e->line);
type_error_count += 1;
}
type_free(left);
return right;
}
type_free(left);
return right;
}
break;
case EXPR_COMMA:
left = expr_typecheck(e->left, is_array_initializer, silent_mode);
right = expr_typecheck(e->right, is_array_initializer, silent_mode);
if(is_array_initializer) {
if(!type_equals(left, right)) {
if(!silent_mode) {
printf("type error (line %d): the elements of an array intializer ({", e->line);
expr_print(e);
printf("}) should have the same type:");
printf(" (");
expr_print(e->left);
printf(") type is ");
type_print(left);
printf(", (");
expr_print(e->right);
printf(") type is ");
type_print(right);
printf("!\n");
type_error_count += 1;
}
}
}
type_free(right);
return left;
break;
case EXPR_IDENT_NAME:
type_free(left);
type_free(right);
return e->symbol->type;
break;
case EXPR_BOOLEAN_LITERAL:
type_free(left);
type_free(right);
return type_create(TYPE_BOOLEAN, 0, 0, 0, e->line, 0);
break;
case EXPR_INTEGER_LITERAL:
type_free(left);
type_free(right);
return type_create(TYPE_INTEGER, 0, 0, 0, e->line, 0);
break;
case EXPR_CHARACTER_LITERAL:
type_free(left);
type_free(right);
return type_create(TYPE_CHARACTER, 0, 0, 0, e->line, 0);
break;
case EXPR_STRING_LITERAL:
type_free(left);
type_free(right);
return type_create(TYPE_STRING, 0, 0, 0, e->line, 0);
break;
}
return 0;
}
//check function call arguments and function definition paramters
void expr_func_typecheck(struct expr *e, int silent_mode) {
//search for the function in the global scope, get its type
struct symbol *s = scope_lookup(e->left->name, e->line, 0);
struct param_list *p = s->type->params;
//get the argument number of the function call
int n = expr_count_item(e->right);
int i = 0;
struct expr *arg;
while(p) {
i += 1;
if(i > n) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): function call (", e->line);
expr_print(e);
printf(") does not have enough arguments, the correct function type is: ");
type_print(s->type);
printf("!\n");
type_error_count += 1;
}
return;
}
arg = expr_get_item(e->right, n, i);
struct type *t = expr_typecheck(arg, 0, silent_mode);
if(!type_equals(p->type, t)) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): the types of function call arguments (", e->line);
expr_print(e);
printf(") do not match the types of function parameters: ");
type_print(s->type);
printf("!\n");
type_error_count += 1;
}
type_free(t);
return;
}
type_free(t);
p = p->next;
}
if(i < n) {
if(!silent_mode) {
fprintf(stdout, "type error (line %d): function call (", e->line);
expr_print(e);
printf(") has too much arguments, the correct function type is: ");
type_print(s->type);
printf("!\n");
type_error_count += 1;
}
return;
}
return;
}
struct expr *expr_get_item(struct expr *e, int n, int i) {
if(i == n) {
if(n == 1) {
return e;
} else {
return e->right;
}
}
int x = n - i;
while(x > 0) {
e = e->left;
x -= 1;
}
if(i == 1) {
return e;
} else {
return e->right;
}
}
int expr_count_item(struct expr *e) {
if(!e) return 0;
int n = 1;
while(e->kind == EXPR_COMMA) {
n += 1;
e = e->left;
}
return n;
}
void expr_print_typecheck(struct expr *e) {
int n = expr_count_item(e);
int i;
struct expr *arg;
for(i = 1; i <= n; i++) {
arg = expr_get_item(e, n, i);
struct type *t = expr_typecheck(arg, 0, 1);
if(t->kind == TYPE_FUNCTION) {
fprintf(stdout, "type error (line %d): print_stmt can not print function (", e->line);
expr_print(arg);
printf(")!\n");
type_error_count += 1;
continue;
}
if(t->kind == TYPE_VOID) {
fprintf(stdout, "type error (line %d): print_stmt can not print void type (", e->line);
expr_print(arg);
printf(")!\n");
type_error_count += 1;
continue;
}
type_free(t);
}
}
int expr_equals(struct expr *s, struct expr *t) {
if((!s) && (!t)) {
return 1;
} else if(s && t) {
if((s->kind == EXPR_INTEGER_LITERAL) && (t->kind == EXPR_INTEGER_LITERAL) && (s->literal_value == t->literal_value)) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
}
void expr_codegen(struct expr *e, FILE *f) {
if(!e) return;
if(e->is_global) {
if(e->left) e->left->is_global = 1;
if(e->right) e->right->is_global = 1;
}
struct type *t;
switch(e->kind) {
case EXPR_LEFTCURLY: //array initializer
fprintf(stderr, "line %d: cminor does not support array currently!\n", e->line);
exit(EXIT_FAILURE);
break;
case EXPR_LEFTPARENTHESS:
/* the left child of ( can only be: id; there is no need to do codegen on it */
if(e->left) { //function call
expr_funccall_codegen(e->right, f);
fprintf(f, "\tpushq\t%%r10\n");
fprintf(f, "\tpushq\t%%r11\n");
fprintf(f, "\tcall\t%s\n", e->left->name);
fprintf(f, "\tpopq\t%%r11\n");
fprintf(f, "\tpopq\t%%r10\n");
e->reg = register_alloc();
fprintf(f, "\tmovq\t%%rax, %%%s\n", register_name(e->reg));
}
break;
case EXPR_LEFTBRACKET: //array subscript
fprintf(stderr, "line %d: cminor does not support array currently!\n", e->line);
exit(EXIT_FAILURE);
break;
case EXPR_INCREMENT:
/* left child must be a EXPR_IDENT_NAME, right child is empty */
expr_codegen(e->left, f);
if(e->is_global) {
e->literal_value = e->left->literal_value + 1;
} else {
if(e->left->symbol->kind == SYMBOL_PARAM) {
fprintf(f, "\taddq\t$1, %d(%%rbp)\n", -8 * (e->left->symbol->which + 1));
} else if(e->left->symbol->kind == SYMBOL_LOCAL) {
fprintf(f, "\taddq\t$1, %d(%%rbp)\n", -8 * (cur_func->param_count + e->left->symbol->which + 1));
} else if(e->left->symbol->kind == FUNC_NOT) {
fprintf(f, "\taddq\t$1, %s(%%rip)\n", e->left->name);
}
e->reg = e->left->reg;
}
break;
case EXPR_DECREMENT:
/* left child must be a EXPR_IDENT_NAME, right child is empty */
expr_codegen(e->left, f);
if(e->is_global) {
e->literal_value = e->left->literal_value + 1;
} else {
if(e->left->symbol->kind == SYMBOL_PARAM) {
fprintf(f, "\tsubq\t$1, %d(%%rbp)\n", -8 * (e->left->symbol->which + 1));
} else if(e->left->symbol->kind == SYMBOL_LOCAL) {
fprintf(f, "\tsubq\t$1, %d(%%rbp)\n", -8 * (cur_func->param_count + e->left->symbol->which + 1));
} else if(e->left->symbol->kind == FUNC_NOT) {
fprintf(f, "\tsubq\t$1, %s(%%rip)\n", e->left->name);
}
e->reg = e->left->reg;
}
break;
case EXPR_UNARY_NEG:
/*left child is empty */
expr_codegen(e->right, f);
if(e->is_global) {
e->literal_value = -1 * e->right->literal_value;
} else {
fprintf(f, "\tmovq\t$0, %%rax\n");
fprintf(f, "\tsubq\t%%%s, %%rax\n", register_name(e->right->reg));
fprintf(f, "\tmovq\t%%rax, %%%s\n", register_name(e->right->reg));
e->reg = e->right->reg;
}
break;
case EXPR_NOT:
expr_codegen(e->right, f);
if(e->is_global) {
e->literal_value = 1 - e->right->literal_value;
} else {
fprintf(f, "\tcmpq\t$1, %%%s\n", register_name(e->right->reg));
fprintf(f, "\tje\t.L%d\n", ctl_no);
fprintf(f, "\tmovq\t$1, %%%s\n", register_name(e->right->reg));
fprintf(f, "\tjmp\t.L%d\n", ctl_no+1);
fprintf(f, ".L%d:\n", ctl_no);
fprintf(f, "\tmovq\t$0, %%%s\n", register_name(e->right->reg));
fprintf(f, ".L%d:\n", ctl_no+1);
ctl_no += 2;
e->reg = e->right->reg;
}
break;
case EXPR_POWER:
expr_codegen(e->left, f);
expr_codegen(e->right, f);
if(e->is_global) {
e->literal_value = (int)pow(e->left->literal_value, e->right->literal_value);
} else {
fprintf(f, "\tmovq\t%%%s, %%rdi\n", register_name(e->left->reg));
fprintf(f, "\tmovq\t%%%s, %%rsi\n", register_name(e->right->reg));
fprintf(f, "\tpushq\t%%r10\n");
fprintf(f, "\tpushq\t%%r11\n");
fprintf(f, "\tcall\tinteger_power\n");
fprintf(f, "\tpopq\t%%r11\n");
fprintf(f, "\tpopq\t%%r10\n");
fprintf(f, "\tmovq\t%%rax, %%%s\n", register_name(e->right->reg));
e->reg = e->right->reg;
register_free(e->left->reg);
e->left->reg = -1;
}
break;
case EXPR_MUL:
expr_codegen(e->left, f);
expr_codegen(e->right, f);
if(e->is_global) {