-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolve.c
1972 lines (1762 loc) · 57.1 KB
/
resolve.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
#define MAX_LOCAL_SYMS 2048
typedef struct Package Package;
typedef union {
bool b;
char c;
int8_t sc;
uint8_t uc;
int16_t s;
uint16_t us;
int32_t i;
uint32_t ui;
int32_t l;
uint32_t ul;
int64_t ll;
uint64_t ull;
float f;
double d;
uintptr_t p;
} Val;
typedef struct {
Type *type;
bool is_const;
bool is_lvalue;
Val val;
} ResolvedExpr;
typedef enum {
SYM_NONE,
SYM_VAR,
SYM_CONST,
SYM_FUNC,
SYM_TYPE,
SYM_ENUM_CONST,
} SymKind;
typedef enum {
SYM_UNRESOLVED,
SYM_RESOLVING,
SYM_RESOLVED,
} SymState;
// struct Sym is typedefed to Sym in type.c
struct Sym {
char *name;
char *external_name;
SymKind kind;
SymState state;
Decl *decl;
Type *type;
Val val;
Package *package;
bool reachable;
};
struct Package {
char *external_name;
char *path;
char full_path[MAX_PATH];
Map syms_map;
BUF(Sym **syms);
BUF(Decl **decls);
BUF(Decl **directives);
bool always_reachable;
};
Map package_map;
BUF(Package **packages);
Package *current_package;
Package *builtin_package;
BUF(Sym **ordered_syms);
// reachable global symbols
BUF(Sym **reachable_syms);
Map reachable_syms_map;
Sym *local_syms[MAX_LOCAL_SYMS];
Sym **local_syms_end = local_syms;
// set upon entering a new local scope and reset upon exit
// used to issue errors for defer statements that follow a scope exit stmt
Stmt *scope_exit_stmt;
Arena resolve_arena;
ResolvedExpr resolved_null = {0};
Sym *get_package_sym(Package *package, char *name) {
return map_get(&package->syms_map, name);
}
Package *enter_package(Package *package) {
Package *prev_package = current_package;
current_package = package;
return prev_package;
}
void leave_package(Package *old_package) {
current_package = old_package;
}
bool is_null_ptr(ResolvedExpr operand) {
return operand.type->kind == TYPE_PTR && operand.is_const && operand.val.p == 0;
}
Sym *sym_alloc(char *name, SymKind kind) {
Sym *sym = arena_alloc_zeroed(&resolve_arena, sizeof(Sym));
sym->name = name;
sym->kind = kind;
sym->package = current_package;
return sym;
}
Sym *sym_decl(Decl *decl) {
SymKind kind = SYM_NONE;
switch (decl->kind) {
case DECL_TYPEDEF:
case DECL_STRUCT:
case DECL_UNION:
case DECL_ENUM:
kind = SYM_TYPE;
break;
case DECL_VAR:
kind = SYM_VAR;
break;
case DECL_CONST:
kind = SYM_CONST;
break;
case DECL_FUNC:
kind = SYM_FUNC;
break;
default:
assert(0);
break;
}
Sym *sym = sym_alloc(decl->name, kind);
sym->decl = decl;
sym->state = SYM_UNRESOLVED;
if (decl->kind == DECL_STRUCT || decl->kind == DECL_UNION) {
sym->state = SYM_RESOLVED;
sym->type = type_incomplete(sym);
}
return sym;
}
Sym *sym_type(char *name, Type *type) {
Sym *sym = sym_alloc(name, SYM_TYPE);
sym->type = type;
sym->state = SYM_RESOLVED;
return sym;
}
Sym *sym_init(Decl *decl, Type *type) {
assert(decl->kind == DECL_VAR);
Sym *sym = sym_decl(decl);
sym->type = type;
sym->state = SYM_RESOLVED;
return sym;
}
Sym *sym_enum_const(char *name, Decl *decl) {
Sym *sym = sym_alloc(name, SYM_ENUM_CONST);
sym->decl = decl;
return sym;
}
Sym *sym_get_local(char *name) {
for (Sym **it = local_syms_end; it > local_syms; --it) {
Sym *sym = it[-1];
if (sym->name == name) return sym;
}
return NULL;
}
Sym *sym_get(char *name) {
// first check symbols in local scopes
Sym *sym = sym_get_local(name);
if (sym) return sym;
// then check package symbols
return get_package_sym(current_package, name);
}
void sym_package_put(char *name, Sym *sym) {
assert(name);
Sym *old_sym = map_get(¤t_package->syms_map, name);
if (old_sym) {
if (sym == old_sym) return;
SourcePos pos = sym->decl ? sym->decl->pos : pos_builtin;
semantic_error(pos, "duplicate definition of global symbol '%s'.", name);
if (old_sym->decl) {
print_note(old_sym->decl->pos, "previous definition of '%s'", name);
}
}
map_put(¤t_package->syms_map, name, sym);
da_push(current_package->syms, sym);
}
bool is_local_sym(Sym *sym) {
return sym_get_local(sym->name) != NULL;
}
bool name_in_local_scope(char *name, Sym **scope_start) {
for (Sym **it = local_syms_end; it > scope_start; --it) {
Sym *sym = it[-1];
if (sym->name == name) return true;
}
return false;
}
void sym_put_decl(Decl *decl) {
Sym *sym = sym_decl(decl);
sym_package_put(sym->name, sym);
if (decl->kind == DECL_ENUM) {
EnumItem *items = decl->enum_decl.items;
for (int i = 0; i < decl->enum_decl.num_items; ++i) {
Sym *s = sym_enum_const(items[i].name, decl);
sym_package_put(s->name, s);
}
}
}
// currently used for primative types
void sym_put_type(char *name, Type *type) {
Sym *sym = sym_type(name, type);
type->sym = sym;
sym_package_put(sym->name, sym);
}
void sym_put_const(char *name, Type *type, Val val) {
Sym *sym = sym_alloc(name, SYM_CONST);
sym->state = SYM_RESOLVED;
sym->type = type;
sym->val = val;
sym_package_put(sym->name, sym);
}
Sym **sym_enter_scope(void) {
return local_syms_end;
}
void sym_leave_scope(Sym **scope_start) {
local_syms_end = scope_start;
}
void sym_push_scoped(Sym *sym) {
if (local_syms_end > local_syms + MAX_LOCAL_SYMS) {
fatal("Too many local symbols, max is %d", MAX_LOCAL_SYMS);
}
*local_syms_end++ = sym;
}
char *type_to_str(Type *type) {
if (type->sym)
return type->sym->name;
switch (type->kind) {
case TYPE_PTR:
return strf("%s*", type_to_str(type->ptr.base));
case TYPE_ARRAY:
return strf("%s[%d]", type_to_str(type->array.base), type->array.num_items);
// TODO:
case TYPE_STRUCT:
case TYPE_UNION:
case TYPE_ENUM:
case TYPE_FUNC:
default:
assert(0);
return NULL;
}
}
bool is_foreign_decl(Decl *decl) {
for (int i=0; i<decl->notes.num_notes; ++i) {
if (decl->notes.notes[i].name == name_foreign)
return true;
}
return false;
}
Note *get_note_foreign(Decl *decl) {
for (int i=0; i<decl->notes.num_notes; ++i) {
if (decl->notes.notes[i].name == name_foreign)
return &decl->notes.notes[i];
}
return NULL;
}
Sym *resolve_name(char *name);
ResolvedExpr resolve_expr_expected(Expr *expr, Type *expected_type);
ResolvedExpr resolve_expr(Expr *expr);
Type *resolve_typespec(Typespec *type);
void complete_type(Type *type) {
if (type->kind == TYPE_COMPLETING) {
assert(type->sym && type->sym->decl);
semantic_error(type->sym->decl->pos, "cyclic type dependency");
return;
} else if (type->kind != TYPE_INCOMPLETE) {
return;
}
type->kind = TYPE_COMPLETING;
assert(type->sym);
Decl *decl = type->sym->decl;
assert(decl);
if (decl->kind != DECL_STRUCT && decl->kind != DECL_UNION) {
// NOTE(shaw): not sure if this should be a semantic error (for the user)
// but regardless having the source position is useful
semantic_error(decl->pos, "cannot complete a type that is not a struct or union");
return;
}
Package *old_package = enter_package(type->sym->package);
BUF(TypeField *type_fields) = NULL; // @LEAK
AggregateField *decl_fields = decl->aggregate.fields;
int num_fields = decl->aggregate.num_fields;
size_t align = 1;
for (int i=0; i<num_fields; ++i) {
Type *field_type = resolve_typespec(decl_fields[i].typespec);
if (field_type->kind != TYPE_PTR)
complete_type(field_type);
align = MAX(align, field_type->align);
da_push(type_fields, (TypeField){
.name = decl_fields[i].name,
.type = field_type,
});
}
// accumulate fields until adding the next one would overflow the alignment
// add required padding
// for the last one, add padding if required (after loop)
size_t accum = 0;
size_t size = 0;
size_t max_size = 0;
size_t pad = 0;
for (int i = 0; i < num_fields; ++i) {
size_t field_size = type_fields[i].type->size;
max_size = MAX(max_size, field_size);
if (accum + field_size > align) {
// TODO(shaw): should this padding be stored in the type somewhere??
// or do we just recontruct the padding positions when actually laying
// variable of this type out in memory ?
pad = align - accum;
size += align;
accum = 0;
}
accum += field_size;
}
// get padding required for last field, and update size
pad = align - accum;
(void)pad;
size += align;
type->aggregate.fields = arena_memdup(&resolve_arena, type_fields, num_fields * sizeof(*type_fields));
type->aggregate.num_fields = num_fields;
type->align = align;
if (decl->kind == DECL_STRUCT) {
type->kind = TYPE_STRUCT;
type->size = size;
} else {
type->kind = TYPE_UNION;
type->size = max_size;
}
da_push(ordered_syms, type->sym);
leave_package(old_package);
}
Type *resolve_typespec(Typespec *typespec) {
switch (typespec->kind) {
case TYPESPEC_NAME: {
Sym *sym = resolve_name(typespec->name);
if (!sym) {
semantic_error(typespec->pos, "unknown type %s", typespec->name);
}
if (sym->kind != SYM_TYPE) {
semantic_error(typespec->pos, "%s must denote a type", typespec->name);
return NULL;
}
return sym->type;
}
case TYPESPEC_FUNC: {
BUF(TypeField *params) = NULL;
for (int i = 0; i < typespec->func.num_params; ++i) {
da_push(params, (TypeField){ .type = resolve_typespec(typespec->func.params[i]) });
}
Type *ret_type = resolve_typespec(typespec->func.ret);
return type_func(params, da_len(params), typespec->func.is_variadic, ret_type);
}
case TYPESPEC_ARRAY: {
Type *elem_type = resolve_typespec(typespec->array.base);
if (typespec->array.num_items) {
ResolvedExpr size = resolve_expr(typespec->array.num_items);
if (!size.is_const) {
semantic_error(typespec->pos, "array size must be a constant");
return NULL;
}
return type_array(elem_type, size.val.i);
} else {
// TODO(shaw): i don't like that this call caches an array type with size 0 that will ultimately
// not be used and will take up space in the cache. could think about some kind of incomplete
// type for arrays or maybe a separate type constructor that doesn't cache it and acts like a
// dummy type just to allow compound expressions to have an expected type to use, essentially
// just to get the array element type
return type_array(elem_type, 0);
}
}
case TYPESPEC_POINTER: {
return type_ptr(resolve_typespec(typespec->ptr.base));
}
default:
assert(0);
return NULL;
}
}
// based on the C standard, see https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf
bool is_convertible(Type *src, Type *dest) {
if (src == dest) {
return true;
} else if (src->kind == TYPE_PTR && dest->kind == TYPE_PTR) {
return (src->ptr.base->kind == TYPE_VOID || dest->ptr.base->kind == TYPE_VOID);
} else if (is_integer_type(src) && is_integer_type(dest)) {
return true;
} else if (is_floating_type(src) && is_floating_type(dest)) {
return true;
} else if (is_integer_type(src) && is_floating_type(dest)) {
return true;
}
return false;
}
bool is_castable(Type *src, Type *dest) {
if (is_convertible(src, dest)) {
return true;
} else if (is_integer_type(dest)) {
return is_ptr_like_type(src) || is_floating_type(src);
} else if (is_integer_type(src)) {
return is_ptr_like_type(dest);
} else if (is_ptr_like_type(dest) && is_ptr_like_type(src)) {
return true;
} else if (is_floating_type(src) && is_integer_type(dest)) {
return true;
} else {
return false;
}
}
#define CASE(_x, _type) \
switch (operand->type->kind) { \
case TYPE_CHAR: operand->val._x = (_type)operand->val.c; break; \
case TYPE_SCHAR: operand->val._x = (_type)operand->val.sc; break; \
case TYPE_UCHAR: operand->val._x = (_type)operand->val.uc; break; \
case TYPE_SHORT: operand->val._x = (_type)operand->val.s; break; \
case TYPE_USHORT: operand->val._x = (_type)operand->val.us; break; \
case TYPE_INT: operand->val._x = (_type)operand->val.i; break; \
case TYPE_ENUM: operand->val._x = (_type)operand->val.i; break; \
case TYPE_UINT: operand->val._x = (_type)operand->val.ui; break; \
case TYPE_LONG: operand->val._x = (_type)operand->val.l; break; \
case TYPE_ULONG: operand->val._x = (_type)operand->val.ul; break; \
case TYPE_LLONG: operand->val._x = (_type)operand->val.ll; break; \
case TYPE_ULLONG: operand->val._x = (_type)operand->val.ull; break; \
case TYPE_BOOL: operand->val._x = (_type)operand->val.b; break; \
case TYPE_PTR: operand->val._x = (_type)operand->val.p; break; \
default: assert(0); break; \
}
bool cast_operand(ResolvedExpr *operand, Type *type) {
if (!is_castable(operand->type, type)) {
return false;
}
if (operand->is_const) {
if (is_floating_type(operand->type)) {
operand->is_const = !is_integer_type(type);
} else {
switch (type->kind) {
case TYPE_CHAR: CASE(c, char); break;
case TYPE_SCHAR: CASE(sc, int8_t); break;
case TYPE_UCHAR: CASE(uc, uint8_t); break;
case TYPE_SHORT: CASE(s, int16_t); break;
case TYPE_USHORT: CASE(us, uint16_t); break;
case TYPE_INT: CASE(i, int32_t); break;
case TYPE_UINT: CASE(ui, uint32_t); break;
case TYPE_LONG: CASE(l, int32_t); break;
case TYPE_ULONG: CASE(ul, uint32_t); break;
case TYPE_LLONG: CASE(ll, int64_t); break;
case TYPE_ULLONG: CASE(ull, uint64_t); break;
case TYPE_FLOAT: break;
case TYPE_DOUBLE: break;
case TYPE_BOOL: CASE(b, bool); break;
case TYPE_PTR: CASE(p, uintptr_t); break;
default:
operand->is_const = false;
break;
}
}
}
operand->type = type;
return true;
}
#undef CASE
bool convert_operand(ResolvedExpr *operand, Type *type) {
if (is_convertible(operand->type, type)) {
cast_operand(operand, type);
return true;
}
return false;
}
// TODO(shaw): using c's macros for min and max values of integer types for
// now. furthermore it will be the values ONLY for the system the compiler is
// compiled on. this version for now is just to get things going.
int64_t integer_min_values[] = {
[TYPE_BOOL] = 0,
[TYPE_CHAR] = 0,
[TYPE_SCHAR] = SCHAR_MIN,
[TYPE_UCHAR] = 0,
[TYPE_SHORT] = SHRT_MIN,
[TYPE_USHORT] = 0,
[TYPE_INT] = INT_MIN,
[TYPE_UINT] = 0,
[TYPE_LONG] = LONG_MIN,
[TYPE_ULONG] = 0,
[TYPE_LLONG] = LLONG_MIN,
[TYPE_ULLONG] = 0,
};
uint64_t integer_max_values[] = {
[TYPE_BOOL] = 1,
[TYPE_CHAR] = UCHAR_MAX,
[TYPE_SCHAR] = SCHAR_MAX,
[TYPE_UCHAR] = UCHAR_MAX,
[TYPE_SHORT] = SHRT_MAX,
[TYPE_USHORT] = USHRT_MAX,
[TYPE_INT] = INT_MAX,
[TYPE_UINT] = UINT_MAX,
[TYPE_LONG] = LONG_MAX,
[TYPE_ULONG] = ULONG_MAX,
[TYPE_LLONG] = LLONG_MAX,
[TYPE_ULLONG] = ULLONG_MAX,
};
int integer_conversion_ranks[] = {
[TYPE_BOOL] = 1,
[TYPE_CHAR] = 2,
[TYPE_SCHAR] = 2,
[TYPE_UCHAR] = 2,
[TYPE_SHORT] = 3,
[TYPE_USHORT] = 3,
[TYPE_INT] = 4,
[TYPE_UINT] = 4,
[TYPE_LONG] = 5,
[TYPE_ULONG] = 5,
[TYPE_LLONG] = 6,
[TYPE_ULLONG] = 6,
};
void integer_promotion(ResolvedExpr *operand) {
if (!is_integer_type(operand->type))
return;
if (integer_conversion_ranks[operand->type->kind] > integer_conversion_ranks[TYPE_INT])
return;
if (integer_min_values[TYPE_INT] <= integer_min_values[operand->type->kind] &&
integer_max_values[TYPE_INT] >= integer_max_values[operand->type->kind])
{
cast_operand(operand, type_int);
} else {
cast_operand(operand, type_uint);
}
}
ResolvedExpr resolved_rvalue(Type *type) {
return (ResolvedExpr){ .type = type };
}
ResolvedExpr resolved_lvalue(Type *type) {
return (ResolvedExpr){
.type = type,
.is_lvalue = true,
};
}
ResolvedExpr resolved_const(Type *type, Val val) {
assert(type);
assert(is_scalar_type(type));
return (ResolvedExpr){
.type = type,
.is_const = true,
.val = val,
};
}
int64_t eval_unary_op_signed(TokenKind op, int64_t val) {
switch(op) {
case '+': return +val;
case '-': return -val;
case '~': return ~val;
default:
assert(0);
return 0;
}
}
uint64_t eval_unary_op_unsigned(TokenKind op, uint64_t val) {
switch(op) {
case '+': return +val;
case '-': return 0ull - val;
case '~': return ~val;
default:
assert(0);
return 0;
}
}
double eval_unary_op_floating(TokenKind op, double val) {
switch(op) {
case '+': return +val;
case '-': return -val;
default:
assert(0);
return 0;
}
}
Val eval_constant_unary_expr(TokenKind op, ResolvedExpr *operand) {
Type *type = operand->type;
ResolvedExpr dummy = *operand;
ResolvedExpr result = {0};
if (is_signed_integer_type(type)) {
cast_operand(&dummy, type_llong);
int64_t val = eval_unary_op_signed(op, dummy.val.ll);
result = resolved_const(type_llong, (Val){.ll=val});
} else if (is_unsigned_integer_type(type)) {
cast_operand(&dummy, type_ullong);
uint64_t val = eval_unary_op_unsigned(op, dummy.val.ull);
result = resolved_const(type_ullong, (Val){.ull=val});
} else if (is_floating_type(type)) {
cast_operand(&dummy, type_double);
double val = eval_unary_op_floating(op, dummy.val.d);
result = resolved_const(type_double, (Val){.d=val});
} else {
assert(0);
}
cast_operand(&result, type);
return result.val;
}
ResolvedExpr resolve_expr_unary(Expr *expr) {
assert(expr->kind == EXPR_UNARY);
ResolvedExpr operand = resolve_expr(expr->unary.expr);
ResolvedExpr result = resolved_null;
switch (expr->unary.op) {
case '*': {
if (operand.type->kind == TYPE_PTR) {
result = resolved_lvalue(operand.type->ptr.base);
} else {
semantic_error(expr->pos, "cannot dereference a non-pointer type");
}
break;
}
case '&': {
if (operand.is_lvalue) {
result = resolved_rvalue(type_ptr(operand.type));
} else {
semantic_error(expr->pos, "cannot take the address of a non-lvalue");
}
break;
}
case '!': {
if (is_scalar_type(operand.type)) {
cast_operand(&operand, type_bool);
result = operand.is_const
? resolved_const(operand.type, (Val){.b = !operand.val.b})
: resolved_rvalue(operand.type);
} else {
semantic_error(expr->pos, "operand of '!' operator must have integer, floating, or pointer type, got %s",
type_to_str(operand.type));
}
break;
}
case '~': {
if (is_integer_type(operand.type)) {
integer_promotion(&operand);
if (operand.is_const) {
Val val = eval_constant_unary_expr(expr->unary.op, &operand);
result = resolved_const(operand.type, val);
} else {
result = resolved_rvalue(operand.type);
}
} else {
semantic_error(expr->pos, "operand of '~' operator must have integer type, got %s", type_to_str(operand.type));
}
break;
}
case '+':
case '-': {
if (is_arithmetic_type(operand.type)) {
integer_promotion(&operand);
if (operand.is_const) {
Val val = eval_constant_unary_expr(expr->unary.op, &operand);
result = resolved_const(operand.type, val);
} else {
result = resolved_rvalue(operand.type);
}
} else {
semantic_error(expr->pos, "operand of '%s' operator must have integer or floating type, got %s",
expr->unary.op, type_to_str(operand.type));
}
break;
}
default:
assert(0);
break;
}
return result;
}
ResolvedExpr resolve_expr_name(Expr *expr) {
assert(expr->kind == EXPR_NAME);
Sym *sym = resolve_name(expr->name);
if (!sym) {
semantic_error(expr->pos, "unknown symbol %s", expr->name);
}
if (sym->kind == SYM_VAR)
return resolved_lvalue(sym->type);
else if (sym->kind == SYM_FUNC)
return resolved_rvalue(sym->type);
else if (sym->kind == SYM_CONST || sym->kind == SYM_ENUM_CONST)
return resolved_const(sym->type, sym->val);
else {
assert(sym->kind == SYM_TYPE);
semantic_error(expr->pos, "expected variable, constant, or function but got type (%s)", expr->name);
return resolved_null;
}
}
ResolvedExpr resolve_expr(Expr *expr);
ResolvedExpr resolve_expr_cond(Expr *cond) {
ResolvedExpr resolved = resolve_expr(cond);
if (!is_scalar_type(resolved.type)) {
semantic_error(cond->pos,
"condition expression must have integer, floating, or pointer type; got %s",
type_to_str(resolved.type));
}
return resolved;
}
void pointer_decay(ResolvedExpr *resolved) {
assert(resolved->type->kind == TYPE_ARRAY);
resolved->type = type_ptr(resolved->type->array.base);
resolved->is_lvalue = false;
}
bool floating_conversion(ResolvedExpr *operand_left, ResolvedExpr *operand_right) {
Type *left = operand_left->type;
Type *right = operand_right->type;
if (left == right) return true;
// conversion to double
if (left->kind == TYPE_DOUBLE && right->kind != TYPE_DOUBLE) {
if (is_convertible(right, type_double)) {
cast_operand(operand_right, type_double);
return true;
} else {
return false;
}
} else if (right->kind == TYPE_DOUBLE && left->kind != TYPE_DOUBLE) {
if (is_convertible(left, type_double)) {
cast_operand(operand_left, type_double);
return true;
} else {
return false;
}
// conversion to float
} else if (left->kind == TYPE_FLOAT && right->kind != TYPE_FLOAT) {
if (is_convertible(right, type_float)) {
cast_operand(operand_right, type_float);
return true;
} else {
return false;
}
} else if (right->kind == TYPE_FLOAT && left->kind != TYPE_FLOAT) {
if (is_convertible(left, type_float)) {
cast_operand(operand_left, type_float);
return true;
} else {
return false;
}
}
assert(0);
return false;
}
// returns true if types are compatible and conversion is successful, else false
// see usual arithmetic conversions in the C standard, section 6.3.1.8
bool arithmetic_conversion(ResolvedExpr *left, ResolvedExpr *right) {
assert(is_arithmetic_type(left->type) && is_arithmetic_type(right->type));
if (is_floating_type(left->type) || is_floating_type(right->type)) {
return floating_conversion(left, right);
}
integer_promotion(left);
integer_promotion(right);
if (left->type == right->type) {
return true;
} else if ((is_signed_integer_type(left->type) && is_signed_integer_type(right->type)) ||
(is_unsigned_integer_type(left->type) && is_unsigned_integer_type(right->type))) {
// the type with lower rank is converted to the type with higher rank
if (integer_conversion_ranks[left->type->kind] < integer_conversion_ranks[right->type->kind]) {
cast_operand(left, right->type);
} else {
cast_operand(right, left->type);
}
return true;
} else {
ResolvedExpr *signed_operand, *unsigned_operand;
if (is_signed_integer_type(left->type)) {
signed_operand = left;
unsigned_operand = right;
} else {
signed_operand = right;
unsigned_operand = left;
}
if (integer_conversion_ranks[unsigned_operand->type->kind] >= integer_conversion_ranks[signed_operand->type->kind]) {
cast_operand(signed_operand, unsigned_operand->type);
return true;
// if the operand with signed type can represent all of the values of the operand with unsigned type
} else if (integer_min_values[signed_operand->type->kind] <= integer_min_values[unsigned_operand->type->kind] &&
integer_max_values[signed_operand->type->kind] >= integer_max_values[unsigned_operand->type->kind]) {
cast_operand(unsigned_operand, signed_operand->type);
return true;
} else {
Type *corresponding_unsigned_type[] = {
[TYPE_SCHAR] = type_uchar,
[TYPE_SHORT] = type_ushort,
[TYPE_INT] = type_uint,
[TYPE_LONG] = type_ulong,
[TYPE_LLONG] = type_ullong,
};
Type *type = corresponding_unsigned_type[signed_operand->type->kind];
cast_operand(signed_operand, type);
cast_operand(unsigned_operand, type);
return true;
}
}
}
// see the table in c11 standard section 6.4.4.1
ResolvedExpr resolve_expr_int(Expr *expr) {
assert(expr->kind == EXPR_INT);
uint64_t int_val = expr->int_val;
ResolvedExpr result = resolved_const(type_ullong, (Val){.ull = int_val});
Type *type;
bool explicit_unsigned = IS_SET(expr->mod, TOKENMOD_UNSIGNED);
bool explicit_long = IS_SET(expr->mod, TOKENMOD_LONG);
bool explicit_llong = IS_SET(expr->mod, TOKENMOD_LLONG);
if (explicit_unsigned) {
type = type_uint;
if (int_val > integer_max_values[TYPE_UINT]) {
type = type_ulong;
if (int_val > integer_max_values[TYPE_ULONG]) {
type = type_ullong;
}
}
if (explicit_llong) {
type = type_ullong;
} else if (explicit_long && int_val <= integer_max_values[TYPE_ULONG]) {
type = type_ulong;
}
// hex, bin, or oct that is not explicitly unsigned
} else if (IS_SET(expr->mod, TOKENMOD_HEX) ||
IS_SET(expr->mod, TOKENMOD_BIN) ||
IS_SET(expr->mod, TOKENMOD_OCT))
{
type = type_int;
if (int_val > integer_max_values[TYPE_INT]) {
type = type_uint;
if (int_val > integer_max_values[TYPE_UINT]) {
type = type_long;
if (int_val > integer_max_values[TYPE_LONG]) {
type = type_ulong;
if (int_val > integer_max_values[TYPE_ULONG]) {
type = type_llong;
if (int_val > integer_max_values[TYPE_LLONG]) {
type = type_ullong;
}
}
}
}
}
if (explicit_llong && int_val <= integer_max_values[TYPE_LLONG]) {
type = type_llong;
} else if (explicit_long && int_val <= integer_max_values[TYPE_LONG]) {
type = type_long;
}
// decimal int that is not explicitly unsigned
} else {
type = type_int;
if (int_val > integer_max_values[TYPE_INT]) {
type = type_long;
if (int_val > integer_max_values[TYPE_LONG]) {
type = type_llong;
}
}
if (explicit_llong) {
type = type_llong;
} else if (explicit_long && int_val <= integer_max_values[TYPE_LONG]) {
type = type_long;
}
}
cast_operand(&result, type);
return result;
}
int64_t eval_binary_op_signed(TokenKind op, int64_t left, int64_t right) {
switch(op) {
case '+': return left + right;
case '-': return left - right;
case '*': return left * right;
case '&': return left & right;
case '|': return left | right;
case '^': return left ^ right;
case '<': return left < right;
case '>': return left > right;
case '/': return right == 0 ? 0 : left / right;
case '%': return right == 0 ? 0 : left % right;
case TOKEN_LSHIFT: return left << right;
case TOKEN_RSHIFT: return left >> right;
case TOKEN_EQ_EQ: return left == right;
case TOKEN_NOT_EQ: return left != right;
case TOKEN_LT_EQ: return left <= right;
case TOKEN_GT_EQ: return left >= right;
default:
assert(0);
return 0;
}
}
uint64_t eval_binary_op_unsigned(TokenKind op, uint64_t left, uint64_t right) {
switch(op) {
case '+': return left + right;
case '-': return left - right;
case '*': return left * right;
case '&': return left & right;
case '|': return left | right;
case '^': return left ^ right;
case '<': return left < right;
case '>': return left > right;
case '/': return right == 0 ? 0 : left / right;