-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsema.c
1576 lines (1280 loc) · 39.6 KB
/
sema.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
// SPDX-License-Identifier: GPL-2.0-only
#include "cc3.h"
// Primitive type table
ty_t ty_void = { TY_VOID };
ty_t ty_bool = { TY_BOOL, 1, 1, };
ty_t ty_char = { TY_CHAR, 1, 1, };
ty_t ty_schar = { TY_SCHAR, 1, 1, };
ty_t ty_uchar = { TY_UCHAR, 1, 1, };
ty_t ty_short = { TY_SHORT, 2, 2, };
ty_t ty_ushort = { TY_USHORT, 2, 2, };
ty_t ty_int = { TY_INT, 4, 4, };
ty_t ty_uint = { TY_UINT, 4, 4, };
ty_t ty_long = { TY_LONG, 8, 8, };
ty_t ty_ulong = { TY_ULONG, 8, 8, };
ty_t ty_llong = { TY_LLONG, 8, 8, };
ty_t ty_ullong = { TY_ULLONG, 8, 8, };
ty_t ty_float = { TY_FLOAT, 4, 4, };
ty_t ty_double = { TY_DOUBLE, 8, 8, };
ty_t ty_ldouble = { TY_LDOUBLE, 16, 16 };
// va_list type, as defined by the SysV AMD64 ABI
// typedef struct {
// unsigned int gp_offset;
// unsigned int fp_offset;
// void *overflow_arg_area;
// void *reg_save_area;
// } va_list[1];
ty_t ty_va_list = { TY_ARRAY, 8, 24, {{ &ty_void, 1 }} };
ty_t *make_ty(int kind)
{
ty_t *ty = calloc(1, sizeof *ty);
if (!ty) abort();
ty->kind = kind;
return ty;
}
ty_t *make_pointer(ty_t *base_ty)
{
ty_t *ty = make_ty(TY_POINTER);
ty->align = 8;
ty->size = 8;
ty->pointer.base_ty = base_ty;
return ty;
}
ty_t *make_array(ty_t *elem_ty, int cnt)
{
if (elem_ty->kind == TY_VOID)
err("Array of void");
if (elem_ty->kind == TY_ARRAY && elem_ty->array.cnt == -1)
err("Array of incomplete array %T", elem_ty);
if (elem_ty->kind & (TY_STRUCT | TY_UNION))
if (!elem_ty->as_aggregate.had_def)
err("Array of incomplete aggregate %T", elem_ty);
if (elem_ty->kind == TY_FUNCTION)
err("Array cannot contain function %T", elem_ty);
ty_t *ty = make_ty(TY_ARRAY);
if (cnt != -1) {
ty->align = elem_ty->align;
ty->size = elem_ty->size * cnt;
}
ty->array.elem_ty = elem_ty;
ty->array.cnt = cnt;
return ty;
}
ty_t *make_function(ty_t *ret_ty, scope_t *scope, param_vec_t *params, bool var)
{
if (ret_ty->kind == TY_ARRAY)
err("Function returning array %T", ret_ty);
if (ret_ty->kind & (TY_STRUCT | TY_UNION))
if (!ret_ty->as_aggregate.had_def)
err("Function returning incomplete aggregate %T", ret_ty);
if (ret_ty->kind == TY_FUNCTION)
err("Function returning function %T", ret_ty);
ty_t *ty = make_ty(TY_FUNCTION);
ty->function.ret_ty = ret_ty;
ty->function.scope = scope;
ty->function.params = *params;
ty->function.var = var;
return ty;
}
static void print_members(memb_vec_t *members)
{
printf(" { ");
VEC_FOREACH(members, memb) {
print_ty(memb->ty);
if (memb->name)
printf(" %s", memb->name);
printf("; ");
}
printf("}");
}
void print_ty(ty_t *ty)
{
switch (ty->kind) {
case TY_VOID: printf("void"); break;
case TY_CHAR: printf("char"); break;
case TY_SCHAR: printf("signed char"); break;
case TY_UCHAR: printf("unsigned char"); break;
case TY_SHORT: printf("short"); break;
case TY_USHORT: printf("unsigned short"); break;
case TY_INT: printf("int"); break;
case TY_UINT: printf("unsigned int"); break;
case TY_LONG: printf("long"); break;
case TY_ULONG: printf("unsigned long"); break;
case TY_LLONG: printf("long long"); break;
case TY_ULLONG: printf("unsigned long long"); break;
case TY_FLOAT: printf("float"); break;
case TY_DOUBLE: printf("double"); break;
case TY_LDOUBLE: printf("long double"); break;
case TY_BOOL: printf("_Bool"); break;
case TY_STRUCT:
if (ty->as_aggregate.name) {
printf("struct %s", ty->as_aggregate.name);
} else {
printf("struct");
assert(ty->as_aggregate.had_def);
print_members(&ty->as_aggregate.members);
}
break;
case TY_UNION:
if (ty->as_aggregate.name) {
printf("union %s", ty->as_aggregate.name);
} else {
printf("union");
assert(ty->as_aggregate.had_def);
print_members(&ty->as_aggregate.members);
}
break;
case TY_POINTER:
print_ty(ty->pointer.base_ty);
printf("*");
break;
case TY_ARRAY:
print_ty(ty->array.elem_ty);
if (ty->array.cnt < 0)
printf("[]");
else
printf("[%d]", ty->array.cnt);
break;
case TY_FUNCTION:
print_ty(ty->function.ret_ty);
printf("(");
VEC_FOREACH(&ty->function.params, param) {
print_ty(param->ty);
if (param->sym)
printf(" %s", param->sym->name);
printf(", ");
}
if (ty->function.var)
printf("...");
printf(")");
break;
default:
printf("%d\n", ty->kind);
ASSERT_NOT_REACHED();
}
}
static bool compare_ty(ty_t *ty1, ty_t *ty2)
{
switch (ty1->kind) {
case TY_VOID:
case TY_BOOL:
case TY_CHAR:
case TY_SCHAR:
case TY_UCHAR:
case TY_SHORT:
case TY_USHORT:
case TY_INT:
case TY_UINT:
case TY_LONG:
case TY_ULONG:
case TY_LLONG:
case TY_ULLONG:
case TY_FLOAT:
case TY_DOUBLE:
case TY_LDOUBLE:
return ty1->kind == ty2->kind;
case TY_UNION:
case TY_STRUCT:
return ty1 == ty2;
case TY_POINTER:
if (ty2->kind != TY_POINTER)
return false;
return compare_ty(ty1->pointer.base_ty, ty2->pointer.base_ty);
case TY_ARRAY:
if (ty2->kind != TY_ARRAY)
return false;
if (ty1->array.cnt != ty2->array.cnt)
return false;
return compare_ty(ty1->array.elem_ty, ty2->array.elem_ty);
case TY_FUNCTION:
if (ty2->kind != TY_FUNCTION)
return false;
if (ty1->function.var != ty2->function.var)
return false;
if (ty1->function.params.length != ty2->function.params.length)
return false;
if (!compare_ty(ty1->function.ret_ty, ty2->function.ret_ty))
return false;
for (int i = 0; i < ty1->function.params.length; ++i)
if (!compare_ty(VEC_AT(&ty1->function.params, i)->ty,
VEC_AT(&ty2->function.params, i)->ty))
return false;
return true;
default:
ASSERT_NOT_REACHED();
}
}
void sema_init(sema_t *self)
{
self->scope = NULL;
sema_enter(self);
self->block_static_cnt = 0;
}
void sema_free(sema_t *self)
{
sema_exit(self);
}
void sema_enter(sema_t *self)
{
scope_t *scope = calloc(1, sizeof *scope);
if (!scope) abort();
scope->parent = self->scope;
self->scope = scope;
sym_map_init(&scope->syms);
ty_map_init(&scope->tags);
}
void sema_exit(sema_t *self)
{
scope_t *scope = self->scope;
self->scope = scope->parent;
sym_map_free(&scope->syms);
ty_map_free(&scope->tags);
}
void sema_push(sema_t *self, scope_t *scope)
{
scope->parent = self->scope;
self->scope = scope;
}
scope_t *sema_pop(sema_t *self)
{
scope_t *scope = self->scope;
self->scope = scope->parent;
return scope;
}
static int sym_kind(sema_t *self, int sc)
{
switch (sc) {
case TK_TYPEDEF:
return SYM_TYPEDEF;
case TK_EXTERN:
return SYM_EXTERN;
case TK_STATIC:
return SYM_STATIC;
case TK_AUTO:
case TK_REGISTER:
return SYM_LOCAL;
case -1:
if (self->scope->parent)
return SYM_LOCAL;
else
return SYM_EXTERN;
default:
ASSERT_NOT_REACHED();
}
}
sym_t *sema_declare(sema_t *self, int sc, ty_t *ty, const char *name)
{
// Figure out the symbol kind
int kind = sym_kind(self, sc);
// Look for previous declaration in the current scope
bool found;
sym_entry_t *entry = sym_map_find_or_insert(&self->scope->syms, name, &found);
if (found) {
sym_t *sym = entry->value;
switch (sym->kind) {
case SYM_EXTERN:
case SYM_STATIC:
if (kind == SYM_EXTERN) { // OK
break;
}
if (kind == SYM_STATIC) { // OK
sym->kind = SYM_STATIC;
break;
}
FALLTHROUGH;
default:
err("Invalid re-declaration of %s", name);
}
// FIXME: check re-declaration type
// NOTE: This assignment is needed for function defs
sym->ty = ty;
return sym;
}
// Create symbol
sym_t *sym = calloc(1, sizeof *sym);
if (!sym) abort();
sym->kind = sym_kind(self, sc);
sym->ty = ty;
sym->name = name;
// Create unique assembler name for block scope static
if (sym->kind == SYM_STATIC && self->scope->parent) {
string_t s;
string_init(&s);
string_printf(&s, "%s$%d", sym->name, self->block_static_cnt++);
sym->asm_name = s.data;
} else {
sym->asm_name = sym->name;
}
// Add it to the symbol table
entry->value = sym;
return sym;
}
sym_t *sema_declare_enum_const(sema_t *self, const char *name, val_t val)
{
// Look for previous declaration in the current scope
bool found;
sym_entry_t *entry = sym_map_find_or_insert(&self->scope->syms, name, &found);
if (found)
err("Re-declaration of enumeration constant %s", name);
// Create symbol
sym_t *sym = calloc(1, sizeof *sym);
if (!sym) {
abort();
}
sym->kind = SYM_ENUM_CONST;
sym->name = name;
sym->val = val;
// Declare it in the current scope
entry->value = sym;
return sym;
}
sym_t *sema_lookup(sema_t *self, const char *name)
{
for (scope_t *scope = self->scope; scope; scope = scope->parent) {
sym_entry_t *entry = sym_map_find(&scope->syms, name);
if (entry) {
return entry->value;
}
}
return NULL;
}
ty_t *sema_findtypedef(sema_t *self, const char *name)
{
sym_t *sym = sema_lookup(self, name);
if (sym && sym->kind == SYM_TYPEDEF)
return sym->ty;
return NULL;
}
ty_t *sema_forward_declare_tag(sema_t *self, int kind, const char *name)
{
// Look for name in any scope
ty_entry_t *entry = NULL;
for (scope_t *scope = self->scope; scope; scope = scope->parent) {
if ((entry = ty_map_find(&scope->tags, name))) {
break;
}
}
if (entry) {
// Make sure the right keyword is used to refer to the tag
if (entry->value->kind != kind)
err("Tag referred to with the wrong keyword");
// Return the type associated with the tag we found
return entry->value;
}
bool found;
entry = ty_map_find_or_insert(&self->scope->tags, name, &found);
assert(!found);
// Create type
ty_t *ty = make_ty(kind);
ty->as_aggregate.name = name;
// Add it to the current scope
entry->key = ty->as_aggregate.name;
entry->value = ty;
return ty;
}
ty_t *sema_define_tag(sema_t *self, int kind, const char *name)
{
// Look for name just in the current scope
bool found;
ty_entry_t *entry = ty_map_find_or_insert(&self->scope->tags, name, &found);
if (found) {
// Make sure the right keyword is used to refer to the tag
if (entry->value->kind != kind)
err("Tag referred to with the wrong keyword");
// Return the type associated with the tag we found
return entry->value;
}
// Create new type
ty_t *ty = make_ty(kind);
ty->as_aggregate.name = name;
// Add it to the current scope
entry->key = ty->as_aggregate.name;
entry->value = ty;
return ty;
}
expr_t *alloc_expr(int kind)
{
expr_t *expr = calloc(1, sizeof *expr);
if (!expr) abort();
expr->kind = kind;
return expr;
}
//
// Primary expressions
//
expr_t *make_sym_expr(sema_t *self, const char *name)
{
// Special pre-declared identifier
if (self->func_name && !strcmp(name, "__func__"))
return make_str_expr(self->func_name);
// Find symbol
sym_t *sym = sema_lookup(self, name);
if (!sym)
err("Undeclared identifier %s", name);
// Create expression based on symbol type
expr_t *expr;
switch (sym->kind) {
case SYM_TYPEDEF:
err("Invalid use of typedef name %s", name);
case SYM_EXTERN:
case SYM_STATIC:
case SYM_LOCAL:
expr = alloc_expr(EXPR_SYM);
expr->ty = sym->ty;
expr->as_sym = sym;
break;
case SYM_ENUM_CONST:
expr = make_const_expr(&ty_int, sym->val);
break;
}
return expr;
}
expr_t *make_const_expr(ty_t *ty, val_t value)
{
expr_t *expr = alloc_expr(EXPR_CONST);
expr->ty = ty;
expr->as_const.value = value;
return expr;
}
expr_t *make_str_expr(const char *data)
{
expr_t *expr = alloc_expr(EXPR_STR);
expr->ty = make_array(&ty_char, strlen(data) + 1);
expr->as_str.data = data;
return expr;
}
// Is an expression a NULL pointer constant?
static bool is_null_const(expr_t *expr)
{
return expr->ty->kind & TY_INT_MASK
&& expr->kind == EXPR_CONST
&& expr->as_const.value == 0;
}
// Degrade array and function types to pointers per (6.3.2.1)
static ty_t *find_ptr_base(ty_t *ty)
{
if (ty->kind == TY_POINTER)
return ty->pointer.base_ty;
if (ty->kind == TY_ARRAY)
return ty->array.elem_ty;
if (ty->kind == TY_FUNCTION)
return ty;
return NULL;
}
// Compare two pointers, taking degradation into account
static ty_t *compare_ptrs(ty_t *ty1, ty_t *ty2, bool permit_void)
{
if (!(ty1 = find_ptr_base(ty1)))
return NULL;
if (!(ty2 = find_ptr_base(ty2)))
return NULL;
if (permit_void) {
if (ty1->kind == TY_VOID)
return ty2;
if (ty2->kind == TY_VOID)
return ty1;
}
if (!compare_ty(ty1, ty2))
return NULL;
return ty1;
}
//
// Postfix expressions
//
static int find_memb(ty_t *ty, const char *name, ty_t **out)
{
if (ty->kind != TY_STRUCT && ty->kind != TY_UNION)
return -1;
VEC_FOREACH(&ty->as_aggregate.members, memb) {
if (!memb->name) {
int result = find_memb(memb->ty, name, out);
if (result != -1)
return memb->offset + result;
} else if (!strcmp(memb->name, name)) {
*out = memb->ty;
return memb->offset;
}
}
return -1;
}
expr_t *make_memb_expr(expr_t *aggr, const char *name)
{
expr_t *expr = alloc_expr(EXPR_MEMB);
expr->as_memb.aggr = aggr;
expr->as_memb.name = name;
expr->as_memb.offset = find_memb(aggr->ty, name, &expr->ty);
if (expr->as_memb.offset == -1)
err("Non-existent member %s of type %T", name, aggr->ty);
return expr;
}
static expr_t *convert_by_assignment(ty_t *dest_ty, expr_t *src)
{
if (dest_ty->kind & TY_ARITH_MASK && src->ty->kind & TY_ARITH_MASK) {
if (dest_ty->kind != src->ty->kind)
return make_cast_expr(dest_ty, src);
else
return src;
}
if (dest_ty->kind & (TY_STRUCT | TY_UNION) && dest_ty == src->ty)
return src;
if (dest_ty->kind == TY_POINTER && compare_ptrs(dest_ty, src->ty, true))
return src;
if (dest_ty->kind == TY_POINTER && is_null_const(src))
return make_cast_expr(dest_ty, src);
if (dest_ty->kind == TY_BOOL && find_ptr_base(src->ty))
return make_cast_expr(dest_ty, src);
err("Cannot convert type %T to %T by assignment", src->ty, dest_ty);
}
expr_t *make_call_expr(expr_t *func, expr_vec_t *args)
{
// Called expression must be a function pointer after implicit conversions
ty_t *func_ty = find_ptr_base(func->ty);
if (!func_ty)
err("Cannot call %T object", func->ty);
// Convert the arguments to the type of the corresponding parameters
param_t *param = VEC_BEGIN(&func_ty->function.params),
*end_param = VEC_END(&func_ty->function.params);
VEC_FOREACH(args, arg)
if (param < end_param) {
// Function arguments get converted "as if by assignment"
*arg = convert_by_assignment(param++->ty, *arg);
} else if (func_ty->function.var) {
// Variable arguments get the "default argument promotions"
if ((*arg)->ty->kind & TY_PROMOTE_MASK)
*arg = make_cast_expr(&ty_int, *arg);
else if ((*arg)->ty->kind == TY_FLOAT)
*arg = make_cast_expr(&ty_double, *arg);
} else {
err("Too many arguments for %T", func_ty);
}
if (param < end_param)
err("Not enough arguments for %T", func_ty);
expr_t *expr = alloc_expr(EXPR_CALL);
expr->ty = func_ty->function.ret_ty;
expr->as_call.func = func;
expr->as_call.args = *args;
return expr;
}
//
// Prefix expressions
//
expr_t *make_ref_expr(expr_t *arg)
{
expr_t *expr = alloc_expr(EXPR_REF);
expr->ty = make_pointer(arg->ty);
expr->as_unary.arg = arg;
return expr;
}
expr_t *make_dref_expr(expr_t *arg)
{
ty_t *base_ty = find_ptr_base(arg->ty);
if (!base_ty)
err("Dereference of non-pointer type %T", arg->ty);
expr_t *expr = alloc_expr(EXPR_DREF);
expr->ty = base_ty;
expr->as_unary.arg = arg;
return expr;
}
expr_t *make_pos_expr(expr_t *arg)
{
if (!(arg->ty->kind & TY_ARITH_MASK))
err("Unary + applied to non-arithmetic type %T", arg->ty);
if (arg->ty->kind & TY_PROMOTE_MASK)
arg = make_cast_expr(&ty_int, arg);
return arg;
}
expr_t *make_neg_expr(expr_t *arg)
{
if (!(arg->ty->kind & TY_ARITH_MASK))
err("Unary - applied to non-arithmetic type %T", arg->ty);
if (arg->ty->kind & TY_PROMOTE_MASK)
arg = make_cast_expr(&ty_int, arg);
if (arg->kind == EXPR_CONST) {
arg->as_const.value = -arg->as_const.value;
return arg;
}
expr_t *expr = alloc_expr(EXPR_NEG);
expr->ty = arg->ty;
expr->as_unary.arg = arg;
return expr;
}
expr_t *make_not_expr(expr_t *arg)
{
if (!(arg->ty->kind & TY_INT_MASK))
err("Unary ~ applied to non-integer type %T", arg->ty);
if (arg->ty->kind & TY_PROMOTE_MASK)
arg = make_cast_expr(&ty_int, arg);
if (arg->kind == EXPR_CONST) {
arg->as_const.value = ~arg->as_const.value;
return arg;
}
expr_t *expr = alloc_expr(EXPR_NEG);
expr->ty = arg->ty;
expr->as_unary.arg = arg;
return expr;
}
expr_t *make_lnot_expr(expr_t *arg)
{
if (!(arg->ty->kind & TY_SCALAR_MASK))
err("Unary ! applied to non-scalar type %T", arg->ty);
if (arg->kind == EXPR_CONST) {
arg->ty = &ty_int;
arg->as_const.value = !arg->as_const.value;
return arg;
}
expr_t *expr = alloc_expr(EXPR_LNOT);
expr->ty = &ty_int;
expr->as_unary.arg = arg;
return expr;
}
expr_t *make_sizeof_expr(ty_t *ty)
{
if (!ty->size)
err("Requsted size of incomplete type %T", ty);
return make_const_expr(&ty_ulong, ty->size);
}
expr_t *make_cast_expr(ty_t *ty, expr_t *arg)
{
// Validate if the cast is allowed
switch (ty->kind) {
case TY_VOID: // From all types
break;
case TY_BOOL: // From any scalar type
case TY_CHAR:
case TY_SCHAR:
case TY_UCHAR:
case TY_SHORT:
case TY_USHORT:
case TY_INT:
case TY_UINT:
case TY_LONG:
case TY_ULONG:
case TY_LLONG:
case TY_ULLONG:
if (arg->ty->kind & TY_SCALAR_MASK)
break;
goto error;
case TY_FLOAT: // From arithmetic types
case TY_DOUBLE:
case TY_LDOUBLE:
if (arg->ty->kind & TY_ARITH_MASK)
break;
goto error;
case TY_POINTER: // From integer and pointer types
if (arg->ty->kind & TY_INT_MASK)
break;
if (find_ptr_base(arg->ty))
break;
FALLTHROUGH;
default:
error:
err("Cannot cast %T to %T", arg->ty, ty);
}
// Fold cast of constants into the operand
if (arg->kind == EXPR_CONST) {
arg->ty = ty;
return arg;
}
expr_t *expr = alloc_expr(EXPR_CAST);
expr->ty = ty;
expr->as_unary.arg = arg;
return expr;
}
//
// Infix expressions
//
void binary_promote(expr_t **lhs, expr_t **rhs)
{
ty_t *ty;
int kind1 = (*lhs)->ty->kind,
kind2 = (*rhs)->ty->kind;
// Find the common arithmetic type
if (kind1 == TY_LDOUBLE || kind2 == TY_LDOUBLE)
ty = &ty_ldouble;
else if (kind1 == TY_DOUBLE || kind2 == TY_DOUBLE)
ty = &ty_double;
else if (kind1 == TY_FLOAT || kind2 == TY_FLOAT)
ty = &ty_float;
else if (kind1 == TY_ULLONG || kind2 == TY_ULLONG)
ty = &ty_ullong;
else if (kind1 == TY_ULONG || kind2 == TY_ULONG)
ty = &ty_ulong;
else if (kind1 == TY_LLONG || kind2 == TY_LLONG)
ty = &ty_llong;
else if (kind1 == TY_LONG || kind2 == TY_LONG)
ty = &ty_long;
else if (kind1 == TY_UINT || kind2 == TY_UINT)
ty = &ty_uint;
else
ty = &ty_int;
// Then perform any promotions that might be required
if (kind1 != ty->kind)
*lhs = make_cast_expr(ty, *lhs);
if (kind2 != ty->kind)
*rhs = make_cast_expr(ty, *rhs);
}
expr_t *make_mul_expr(expr_t *lhs, expr_t *rhs)
{
if (lhs->ty->kind & TY_ARITH_MASK && rhs->ty->kind & TY_ARITH_MASK)
goto type_ok;
err("Cannot apply * to types %T and %T", lhs->ty, rhs->ty);
type_ok:
binary_promote(&lhs, &rhs);
if (lhs->kind == EXPR_CONST && rhs->kind == EXPR_CONST) {
lhs->as_const.value *= rhs->as_const.value;
return lhs;
}
expr_t *expr = alloc_expr(EXPR_MUL);
expr->ty = lhs->ty;
expr->as_binary.lhs = lhs;
expr->as_binary.rhs = rhs;
return expr;
}
expr_t *make_div_expr(expr_t *lhs, expr_t *rhs)
{
if (lhs->ty->kind & TY_ARITH_MASK && rhs->ty->kind & TY_ARITH_MASK)
goto type_ok;
err("Cannot apply / to types %T and %T", lhs->ty, rhs->ty);
type_ok:
binary_promote(&lhs, &rhs);
if (lhs->kind == EXPR_CONST && rhs->kind == EXPR_CONST) {
lhs->as_const.value /= rhs->as_const.value;
return lhs;
}
expr_t *expr = alloc_expr(EXPR_DIV);
expr->ty = lhs->ty;
expr->as_binary.lhs = lhs;
expr->as_binary.rhs = rhs;
return expr;
}
expr_t *make_mod_expr(expr_t *lhs, expr_t *rhs)
{
if (lhs->ty->kind & TY_INT_MASK && rhs->ty->kind & TY_INT_MASK)
goto type_ok;
err("Cannot apply %% to types %T and %T", lhs->ty, rhs->ty);
type_ok:
binary_promote(&lhs, &rhs);
if (lhs->kind == EXPR_CONST && rhs->kind == EXPR_CONST) {
lhs->as_const.value %= rhs->as_const.value;
return lhs;
}
expr_t *expr = alloc_expr(EXPR_MOD);
expr->ty = lhs->ty;
expr->as_binary.lhs = lhs;
expr->as_binary.rhs = rhs;
return expr;
}
static expr_t *make_ptraddsub(int kind, ty_t *base_ty, expr_t *ptr, expr_t *idx)
{
if (!(idx->ty->kind & TY_INT_MASK))
err("Non-integer type %T used as a pointer offset", idx->ty);
expr_t *expr = alloc_expr(kind);
expr->ty = make_pointer(base_ty);
expr->as_binary.lhs = ptr;
expr->as_binary.rhs = make_mul_expr(idx, make_sizeof_expr(base_ty));
return expr;
}
expr_t *make_add_expr(expr_t *lhs, expr_t *rhs)
{
ty_t *base_ty;
if ((base_ty = find_ptr_base(lhs->ty)))
return make_ptraddsub(EXPR_ADD, base_ty, lhs, rhs);
if ((base_ty = find_ptr_base(rhs->ty)))
return make_ptraddsub(EXPR_ADD, base_ty, rhs, lhs);
if (lhs->ty->kind & TY_ARITH_MASK && rhs->ty->kind & TY_ARITH_MASK)
goto type_ok;
err("Cannot apply + to types %T and %T", lhs->ty, rhs->ty);
type_ok:
binary_promote(&lhs, &rhs);
if (lhs->kind == EXPR_CONST && rhs->kind == EXPR_CONST) {
lhs->as_const.value += rhs->as_const.value;
return lhs;
}
expr_t *expr = alloc_expr(EXPR_ADD);
expr->ty = lhs->ty;
expr->as_binary.lhs = lhs;
expr->as_binary.rhs = rhs;
return expr;
}
expr_t *make_sub_expr(expr_t *lhs, expr_t *rhs)
{
ty_t *base_ty;
if ((base_ty = compare_ptrs(lhs->ty, rhs->ty, false))) {
expr_t *expr = alloc_expr(EXPR_SUB);
expr->ty = &ty_long;
expr->as_binary.lhs = lhs;
expr->as_binary.rhs = rhs;
return make_div_expr(expr, make_sizeof_expr(base_ty));
}
if ((base_ty = find_ptr_base(lhs->ty)))
return make_ptraddsub(EXPR_SUB, base_ty, lhs, rhs);
if (lhs->ty->kind & TY_ARITH_MASK && rhs->ty->kind & TY_ARITH_MASK)
goto type_ok;
err("Cannot apply - to types %T and %T", lhs->ty, rhs->ty);
type_ok:
binary_promote(&lhs, &rhs);
if (lhs->kind == EXPR_CONST && rhs->kind == EXPR_CONST) {
lhs->as_const.value -= rhs->as_const.value;
return lhs;
}
expr_t *expr = alloc_expr(EXPR_SUB);
expr->ty = lhs->ty;
expr->as_binary.lhs = lhs;
expr->as_binary.rhs = rhs;
return expr;
}
expr_t *make_lsh_expr(expr_t *lhs, expr_t *rhs)
{
if (lhs->ty->kind & TY_INT_MASK && rhs->ty->kind & TY_INT_MASK)
goto type_ok;
err("Cannot apply << to types %T and %T", lhs->ty, rhs->ty);
type_ok:
if (lhs->ty->kind & TY_PROMOTE_MASK)
lhs = make_cast_expr(&ty_int, lhs);