-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCirParse.c
2444 lines (2257 loc) · 80.7 KB
/
CirParse.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 "cir_internal.h"
#include <assert.h>
#include <stdlib.h>
#include <stdalign.h>
static const CirMachine *CirParse__mach;
#define DECLARATOR_CONCRETE 0
#define DECLARATOR_ABSTRACT 1
#define DECLARATOR_MAYBEABSTRACT 2
typedef struct CirParse__TypeSpecItem {
enum {
CIRPARSE_TVOID = 1,
CIRPARSE_TCHAR,
CIRPARSE_TBOOL,
CIRPARSE_TSHORT,
CIRPARSE_TINT,
CIRPARSE_TLONG,
CIRPARSE_TFLOAT,
CIRPARSE_TDOUBLE,
CIRPARSE_TFLOAT128,
CIRPARSE_TSIGNED,
CIRPARSE_TUNSIGNED,
CIRPARSE_TNAMED,
CIRPARSE_TAUTOTYPE,
CIRPARSE_TCOMP,
CIRPARSE_TENUM,
CIRPARSE_TBUILTIN_VA_LIST
} type;
union {
CirName name;
CirTypedefId tid;
CirCompId cid;
CirEnumId enumId;
} data;
} CirParse__TypeSpecItem;
typedef CirArray(CirParse__TypeSpecItem) CirParse__TypeSpecArray;
typedef struct CirParse__ProcessedSpec {
const CirType *baseType; // NULL = autotype
uint8_t storage; // storage kind
bool isInline; // inline
bool isTypedef; // typedef
CirAttrArray attrArray;
} CirParse__ProcessedSpec;
static void
CirParse__ProcessedSpec_init(CirParse__ProcessedSpec *pspec)
{
CirArray_init(&pspec->attrArray);
}
static void
CirParse__ProcessedSpec_release(CirParse__ProcessedSpec *pspec)
{
CirArray_release(&pspec->attrArray);
}
// Declarator item
typedef CirArray(CirFunParam) CirFunParamArray;
typedef struct CirParse__DeclItem {
enum {
CIRPARSE_DTARRAY = 1,
CIRPARSE_DTPTR,
CIRPARSE_DTPROTO,
CIRPARSE_DTPAREN
} type;
CirAttrArray attrs;
CirAttrArray rattrs;
CirFunParamArray funParams;
bool isva;
bool hasLen; // Has array length
uint32_t arrayLen;
} CirParse__DeclItem;
typedef CirArray(CirParse__DeclItem) CirParse__DeclArray;
static bool decl_spec_list_FIRST(void);
static void declaration_or_function_definition(CirCodeId);
static void comp_field_declaration(CirCompId);
static int64_t enum_item(CirEnumId, int64_t);
static const CirType *type_name(int);
static CirCodeId comma_expression(void);
static CirCodeId expression(void);
static CirCodeId block(bool dropValue);
static void
CirParse__DeclItem__release(CirParse__DeclItem *item)
{
CirArray_release(&item->attrs);
CirArray_release(&item->rattrs);
CirArray_release(&item->funParams);
}
static void
CirParse__DeclArray_release(CirParse__DeclArray *arr)
{
for (size_t i = 0; i < arr->len; i++)
CirParse__DeclItem__release(&arr->items[i]);
CirArray_release(arr);
}
__attribute__((noreturn))
static void
unexpected_token(const char *ctx, const char *expectedWhat)
{
CirLog_begin(CIRLOG_FATAL);
CirLog_print(ctx);
CirLog_print(": unexpected token ");
CirLog_print(CirLex__str(cirtok.type));
CirLog_print(", expected ");
CirLog_print(expectedWhat);
CirLog_end();
exit(1);
}
static CirVarId
makeGlobalVar(CirVarId vid)
{
CirVarId old_vid;
CirTypedefId tid;
CirEnumItemId enumItemId;
CirName name = CirVar_getName(vid);
assert(name);
int result = CirEnv__findGlobalName(name, &old_vid, &tid, &enumItemId);
if (!result) {
return vid;
} else if (result == 2 || result == 3) {
cir_fatal("declared as a different type of symbol: %s", CirName_cstr(name));
} else {
assert(result == 1);
assert(old_vid);
const CirType *oldType = CirVar_getType(old_vid);
assert(oldType);
const CirType *newType = CirVar_getType(vid);
if (!newType)
cir_fatal("cannot use __auto_type in re-declaration of global: %s", CirName_cstr(name));
// It was already defined. We must re-use the varinfo. But clean up the storage
// TODO: set storage
const CirType *combinedType = CirType__combine(oldType, newType);
if (!combinedType)
cir_fatal("Declaration of %s does not match previous declaration", CirName_cstr(name));
CirVar_setType(old_vid, combinedType);
return old_vid;
}
}
// Returns NUL-terminated string literal
static char *
string_literal(size_t *outSize)
{
assert(cirtok.type == CIRTOK_STRINGLIT);
CirBBuf buf = CIRBBUF_INIT;
while (cirtok.type == CIRTOK_STRINGLIT) {
CirBBuf_grow(&buf, cirtok.data.stringlit.len);
memcpy(buf.items + buf.len, cirtok.data.stringlit.buf, cirtok.data.stringlit.len);
buf.len += cirtok.data.stringlit.len;
CirLex__next();
}
// Finally NUL-terminate
CirBBuf_grow(&buf, 1);
buf.items[buf.len++] = 0;
char *out = CirMem_balloc(buf.len, alignof(*out));
memcpy(out, buf.items, buf.len);
if (outSize)
*outSize = buf.len;
CirBBuf_release(&buf);
return out;
}
static const CirAttr *
attr(void)
{
switch (cirtok.type) {
case CIRTOK_ALIGNOF: { // __alignof__(type)
CirLex__next();
if (cirtok.type != CIRTOK_LPAREN)
unexpected_token("attr_alignof", "`(`");
CirLex__next();
if (!decl_spec_list_FIRST())
unexpected_token("attr_alignof", "type_name");
const CirType *t = type_name(CIRTOK_RPAREN);
if (cirtok.type != CIRTOK_RPAREN)
unexpected_token("attr_alignof", "`)`");
CirLex__next();
uint64_t result = CirType_alignof(t, CirParse__mach);
return CirAttr_int(result);
}
case CIRTOK_IDENT:
case CIRTOK_TYPENAME: {
CirName name = cirtok.data.name;
CirLex__next();
if (cirtok.type != CIRTOK_LPAREN)
return CirAttr_name(name);
// Is a Cons
CirLex__next(); // Consume Lparen
if (cirtok.type == CIRTOK_RPAREN) {
// Empty Cons
CirLex__next();
return CirAttr_cons(name, NULL, 0);
}
CirAttrArray args = CIRARRAY_INIT;
for (;;) {
const CirAttr *arg = attr();
CirArray_push(&args, &arg);
if (cirtok.type == CIRTOK_RPAREN) {
CirLex__next();
break;
} else if (cirtok.type == CIRTOK_COMMA) {
// OK, expecting next attr
CirLex__next();
} else {
unexpected_token("attr", "`,` or `)`");
}
}
const CirAttr *ret = CirAttr_cons(name, args.items, args.len);
CirArray_release(&args);
return ret;
}
case CIRTOK_LPAREN: {
CirLex__next();
const CirAttr *nestedAttr = attr();
if (cirtok.type != CIRTOK_RPAREN)
unexpected_token("attr", "`)`");
CirLex__next();
return nestedAttr;
}
case CIRTOK_INTLIT: {
int32_t value = cirtok.data.intlit.val.i64;
CirLex__next();
return CirAttr_int(value);
}
case CIRTOK_CHARLIT:
if (CirParse__mach->charIsUnsigned) {
uint8_t value = cirtok.data.charlit;
return CirAttr_int(value);
} else {
int8_t value = cirtok.data.charlit;
return CirAttr_int(value);
}
case CIRTOK_STRINGLIT: {
char *buf = string_literal(NULL);
return CirAttr_str(buf);
}
default:
unexpected_token("attr", "IDENT, TYPENAME, `(`, INTLIT, STRINGLIT");
}
}
static bool
attribute_list_FIRST(bool with_asm, bool with_cv)
{
return cirtok.type == CIRTOK_ATTRIBUTE ||
(with_cv && cirtok.type == CIRTOK_CONST) ||
(with_cv && cirtok.type == CIRTOK_RESTRICT) ||
(with_cv && cirtok.type == CIRTOK_VOLATILE) ||
(with_asm && cirtok.type == CIRTOK_ASM);
}
static void
attribute_list(CirAttrArray *out, bool with_asm, bool with_cv)
{
assert(attribute_list_FIRST(with_asm, with_cv));
loop:
if (cirtok.type == CIRTOK_ATTRIBUTE) {
CirLex__next();
if (cirtok.type != CIRTOK_LPAREN)
cir_fatal("expected `(`");
CirLex__next();
if (cirtok.type != CIRTOK_LPAREN)
cir_fatal("expected `(`");
CirLex__next();
if (cirtok.type == CIRTOK_RPAREN)
goto attribute_finish;
for (;;) {
if (cirtok.type != CIRTOK_IDENT && cirtok.type != CIRTOK_TYPENAME)
cir_fatal("expected ident or typename, got %s", CirLex__str(cirtok.type));
const CirAttr *a = attr();
assert(CirAttr_isName(a) || CirAttr_isCons(a));
CirAttrArray__add(out, a);
if (cirtok.type == CIRTOK_RPAREN) {
break;
} else if (cirtok.type == CIRTOK_COMMA) {
// Expecting another attribute
CirLex__next();
} else {
unexpected_token("__attribute__", "`,`, `)`");
}
}
attribute_finish:
CirLex__next();
if (cirtok.type != CIRTOK_RPAREN)
unexpected_token("__attribute__", "`)`");
CirLex__next();
goto loop;
} else if (with_cv && cirtok.type == CIRTOK_CONST) {
CirLex__next();
const CirAttr *attr = CirAttr_name(CirName_of("const"));
CirAttrArray__add(out, attr);
goto loop;
} else if (with_cv && cirtok.type == CIRTOK_RESTRICT) {
CirLex__next();
const CirAttr *attr = CirAttr_name(CirName_of("restrict"));
CirAttrArray__add(out, attr);
goto loop;
} else if (with_cv && cirtok.type == CIRTOK_VOLATILE) {
CirLex__next();
const CirAttr *attr = CirAttr_name(CirName_of("volatile"));
CirAttrArray__add(out, attr);
goto loop;
} else if (with_asm && cirtok.type == CIRTOK_ASM) {
// In some contexts we can have an inline assembly to specify the name
// to be used for a global.
// We treat this as a name attribute.
CirLex__next();
if (cirtok.type != CIRTOK_LPAREN)
unexpected_token("__asm__", "`(`");
CirLex__next();
if (cirtok.type != CIRTOK_STRINGLIT)
unexpected_token("__asm__", "STRINGLIT");
char *buf = string_literal(NULL);
if (cirtok.type != CIRTOK_RPAREN)
unexpected_token("__asm__", "`)`");
CirLex__next();
CirName name = CirName_of("__asm__");
const CirAttr *args[1] = { CirAttr_str(buf) };
const CirAttr *attr = CirAttr_cons(name, args, 1);
CirAttrArray__add(out, attr);
goto loop;
} else {
return;
}
}
static CirCodeId
comp_eval(void)
{
assert(cirtok.type == CIRTOK_AT);
CirLex__next(); // consume AT
if (cirtok.type != CIRTOK_IDENT)
unexpected_token("comp_eval", "IDENT");
CirVarId vid;
CirTypedefId tid;
CirEnumItemId enumItemId;
if (CirEnv__findLocalName(cirtok.data.name, &vid, &tid, &enumItemId) != 1)
cir_fatal("comp_eval: unknown ident: %s", CirName_cstr(cirtok.data.name));
CirLex__next(); // consume IDENT
if (cirtok.type != CIRTOK_LPAREN)
unexpected_token("comp_eval", "`(`");
CirLex__next(); // consume LPAREN
// Collect function arguments.
CirArray(CirCodeId) args = CIRARRAY_INIT;
// Are we calling with any arguments?
if (cirtok.type == CIRTOK_RPAREN) {
CirLex__next();
goto build_function_call;
}
for (;;) {
CirCodeId argCode = expression();
assert(CirCode_isExpr(argCode));
CirArray_push(&args, &argCode);
if (cirtok.type == CIRTOK_COMMA) {
CirLex__next();
} else if (cirtok.type == CIRTOK_RPAREN) {
CirLex__next();
break;
} else {
unexpected_token("comp_eval", "`,`, `)`");
}
}
build_function_call: ;
CirCodeId code_id = CirX64_call(vid, args.items, args.len);
CirArray_release(&args);
return code_id;
}
static CirCodeId
primary_expression(void)
{
switch (cirtok.type) {
case CIRTOK_INTLIT: {
const CirValue *val;
if (CirIkind_isSigned(cirtok.data.intlit.ikind, CirParse__mach)) {
val = CirValue_ofI64(cirtok.data.intlit.ikind, cirtok.data.intlit.val.i64);
} else {
val = CirValue_ofU64(cirtok.data.intlit.ikind, cirtok.data.intlit.val.u64);
}
CirLex__next(); // consume intlit
return CirCode_ofExpr(val);
}
case CIRTOK_CHARLIT: {
const CirValue *val;
if (CirParse__mach->charIsUnsigned) {
val = CirValue_ofU64(CIR_ICHAR, cirtok.data.charlit);
} else {
val = CirValue_ofI64(CIR_ICHAR, cirtok.data.charlit);
}
CirLex__next(); // consume charlit
return CirCode_ofExpr(val);
}
case CIRTOK_STRINGLIT: {
size_t len;
char *buf = string_literal(&len);
const CirValue *val = CirValue_ofString(buf, len);
return CirCode_ofExpr(val);
}
case CIRTOK_IDENT: {
CirVarId vid;
CirTypedefId tid;
CirEnumItemId enumItemId;
switch (CirEnv__findLocalName(cirtok.data.name, &vid, &tid, &enumItemId)) {
case 1: {
const CirValue *val = CirValue_ofVar(vid);
CirLex__next(); // consume ident
return CirCode_ofExpr(val);
}
case 3: {
int64_t value = CirEnumItem_getI64(enumItemId);
CirLex__next(); // consume ident
// The C spec says that enum constants always have type int
return CirCode_ofExpr(CirValue_ofI64(CIR_IINT, value));
}
default:
cir_fatal("unknown ident: %s", CirName_cstr(cirtok.data.name));
}
}
case CIRTOK_BUILTIN: {
assert(cirtok.data.builtinId);
const CirValue *val = CirValue_ofBuiltin(cirtok.data.builtinId);
CirLex__next(); // consume BUILTIN
return CirCode_ofExpr(val);
}
case CIRTOK_AT:
return comp_eval();
case CIRTOK_LPAREN: {
// comma expression or statement expression
CirLex__next(); // consume LPAREN
if (cirtok.type == CIRTOK_LBRACE) {
// statement expression
CirEnv__pushLocalScope();
CirCodeId code_id = block(false);
CirEnv__popScope();
if (cirtok.type != CIRTOK_RPAREN)
unexpected_token("primary_expression", "`)`");
CirLex__next(); // consume RPAREN
return code_id;
} else {
// comma expression
CirCodeId code_id = comma_expression();
if (cirtok.type != CIRTOK_RPAREN)
unexpected_token("primary_expression", "`)`");
CirLex__next(); // consume RPAREN
return code_id;
}
}
default:
unexpected_token("primary_expression", "INTLIT, STRINGLIT, IDENT, `(`");
}
}
static CirCodeId
postfix_expression(void)
{
CirCodeId lhs_id;
lhs_id = primary_expression();
loop:
switch (cirtok.type) {
case CIRTOK_DOT: {
CirArray(CirName) fields = CIRARRAY_INIT;
while (cirtok.type == CIRTOK_DOT) {
CirLex__next(); // consume dot
if (cirtok.type != CIRTOK_IDENT && cirtok.type != CIRTOK_TYPENAME)
unexpected_token("dot", "`IDENT`, `TYPENAME`");
CirArray_push(&fields, &cirtok.data.name);
CirLex__next();
}
assert(fields.len > 0);
const CirValue *value = CirCode_getValue(lhs_id);
if (!value)
cir_fatal("dot: operand has no value");
const CirType *castType = CirValue_getCastType(value);
if (castType) {
// Save value to temporary with the type of the cast
CirVarId tempVar = CirVar_new(lhs_id);
CirVar_setType(tempVar, castType);
CirStmtId assign_stmt_id = CirCode_appendNewStmt(lhs_id);
const CirValue *newValue = CirValue_ofVar(tempVar);
CirStmt_toUnOp(assign_stmt_id, newValue, CIR_UNOP_IDENTITY, value);
value = newValue;
}
CirCode_setValue(lhs_id, CirValue_withFields(value, fields.items, fields.len));
CirArray_release(&fields);
goto loop;
}
case CIRTOK_ARROW: {
CirLex__next(); // consume arrow
if (cirtok.type != CIRTOK_IDENT && cirtok.type != CIRTOK_TYPENAME)
unexpected_token("arrow", "`IDENT`, `TYPENAME`");
const CirValue *value = CirCode_getValue(lhs_id);
if (CirValue_isVar(value) && !CirValue_getNumFields(value) && !CirValue_getCastType(value)) {
// Convert to mem
value = CirValue_ofMem(CirValue_getVar(value));
value = CirValue_withFields(value, &cirtok.data.name, 1);
} else {
// Save pointer to temporary, then create a mem.
CirVarId tempVar = CirVar_new(lhs_id);
CirVar_setType(tempVar, CirValue_getType(value));
CirStmtId assign_stmt_id = CirCode_appendNewStmt(lhs_id);
CirStmt_toUnOp(assign_stmt_id, CirValue_ofVar(tempVar), CIR_UNOP_IDENTITY, value);
value = CirValue_ofMem(tempVar);
value = CirValue_withFields(value, &cirtok.data.name, 1);
}
CirCode_setValue(lhs_id, value);
CirLex__next();
goto loop;
}
case CIRTOK_LBRACKET: { // array subscript
CirLex__next(); // consume LBRACKET
CirCodeId rhs_id = comma_expression();
if (cirtok.type != CIRTOK_RBRACKET)
unexpected_token("array subscript", "`]`");
CirLex__next(); // consume RBRACKET
lhs_id = CirBuild__arraySubscript(lhs_id, rhs_id, CirParse__mach);
goto loop;
}
case CIRTOK_LPAREN: {
// function call
CirLex__next();
// Collect function arguments.
CirArray(CirCodeId) args = CIRARRAY_INIT;
// Are we calling with any arguments?
if (cirtok.type == CIRTOK_RPAREN) {
CirLex__next();
goto build_function_call;
}
for (;;) {
CirCodeId argCode = expression();
assert(CirCode_isExpr(argCode));
CirArray_push(&args, &argCode);
if (cirtok.type == CIRTOK_COMMA) {
CirLex__next();
} else if (cirtok.type == CIRTOK_RPAREN) {
CirLex__next();
break;
} else {
unexpected_token("function_call", "`,`, `)`");
}
}
build_function_call:
lhs_id = CirBuild__call(lhs_id, args.items, args.len, CirParse__mach);
CirArray_release(&args);
goto loop;
}
default:
return lhs_id;
}
}
static CirCodeId
unary_expression(void)
{
switch (cirtok.type) {
case CIRTOK_SIZEOF: {
CirLex__next();
const CirType *t;
if (cirtok.type == CIRTOK_LPAREN) {
// Might be type_name or comma_expression
CirLex__next();
if (decl_spec_list_FIRST()) {
// Is type_name
t = type_name(CIRTOK_RPAREN);
} else {
// Is comma_expression
CirCodeId code_id = comma_expression();
t = CirCode_getType(code_id);
CirCode_free(code_id);
}
if (cirtok.type != CIRTOK_RPAREN)
unexpected_token("sizeof", "`)`");
CirLex__next();
} else {
// Is unary_expression
CirCodeId code_id = unary_expression();
t = CirCode_getType(code_id);
CirCode_free(code_id);
}
uint64_t size = CirType_sizeof(t, CirParse__mach);
uint32_t ikind = CirIkind_fromSize(CirParse__mach->sizeofSizeT, true, CirParse__mach);
return CirCode_ofExpr(CirValue_ofU64(ikind, size));
}
case CIRTOK_ALIGNOF: {
CirLex__next();
if (cirtok.type != CIRTOK_LPAREN)
unexpected_token("alignof", "`(`");
CirLex__next();
if (!decl_spec_list_FIRST())
unexpected_token("alignof", "type_name");
const CirType *t = type_name(CIRTOK_RPAREN);
if (cirtok.type != CIRTOK_RPAREN)
unexpected_token("alignof", "`)`");
CirLex__next();
uint64_t result = CirType_alignof(t, CirParse__mach);
uint32_t ikind = CirIkind_fromSize(CirParse__mach->sizeofSizeT, true, CirParse__mach);
return CirCode_ofExpr(CirValue_ofU64(ikind, result));
}
case CIRTOK_TYPEVAL: {
CirLex__next();
if (cirtok.type != CIRTOK_LPAREN)
unexpected_token("typeval", "`(`");
CirLex__next();
if (!decl_spec_list_FIRST())
unexpected_token("typeval", "type_name");
const CirType *t = type_name(CIRTOK_RPAREN);
if (cirtok.type != CIRTOK_RPAREN)
unexpected_token("typeval", "`)`");
CirLex__next();
return CirCode_ofExpr(CirValue_ofType(t));
}
case CIRTOK_EXCLAM: { // NOT
CirLex__next();
CirCodeId code_id = unary_expression();
return CirBuild__lnot(code_id);
}
case CIRTOK_AND: { // addrof
CirLex__next();
CirCodeId code_id = unary_expression();
return CirBuild__addrof(code_id);
}
case CIRTOK_STAR: { // deref
CirLex__next();
CirCodeId code_id = unary_expression();
return CirBuild__deref(code_id);
}
default:
return postfix_expression();
}
}
static CirCodeId
cast_expression(void)
{
if (cirtok.type == CIRTOK_LPAREN) {
// cast or comma expression or statement expression
CirLex__next(); // consume LPAREN
if (decl_spec_list_FIRST()) {
// cast
const CirType *type = type_name(CIRTOK_RPAREN);
if (cirtok.type != CIRTOK_RPAREN)
unexpected_token("cast_expression", "`)`");
CirLex__next(); // consume RPAREN
CirCodeId code_id = cast_expression();
code_id = CirCode_toExpr(code_id, false);
const CirValue *value = CirCode_getValue(code_id);
if (!value)
cir_fatal("cast_expression: rhs has no value");
value = CirValue_withCastType(value, type);
CirCode_setValue(code_id, value);
return code_id;
} else if (cirtok.type == CIRTOK_LBRACE) {
// statement expression
CirEnv__pushLocalScope();
CirCodeId code_id = block(false);
CirEnv__popScope();
if (cirtok.type != CIRTOK_RPAREN)
unexpected_token("cast_expression", "`)`");
CirLex__next(); // consume RPAREN
return code_id;
} else {
// comma expression
CirCodeId code_id = comma_expression();
if (cirtok.type != CIRTOK_RPAREN)
unexpected_token("cast_expression", "`)`");
CirLex__next(); // consume RPAREN
return code_id;
}
} else {
return unary_expression();
}
}
static CirCodeId
multiplicative_expression(void)
{
CirCodeId lhs_id, rhs_id;
lhs_id = cast_expression();
loop:
switch (cirtok.type) {
case CIRTOK_STAR: // mul
CirLex__next();
rhs_id = cast_expression();
lhs_id = CirBuild__mul(lhs_id, rhs_id, CirParse__mach);
goto loop;
case CIRTOK_SLASH: // div
CirLex__next();
rhs_id = cast_expression();
lhs_id = CirBuild__div(lhs_id, rhs_id, CirParse__mach);
goto loop;
case CIRTOK_PERCENT: // mod
CirLex__next();
rhs_id = cast_expression();
lhs_id = CirBuild__mod(lhs_id, rhs_id, CirParse__mach);
goto loop;
default:
return lhs_id;
}
}
static CirCodeId
additive_expression(void)
{
CirCodeId lhs_id, rhs_id;
lhs_id = multiplicative_expression();
loop:
switch (cirtok.type) {
case CIRTOK_PLUS: // add
CirLex__next();
rhs_id = multiplicative_expression();
lhs_id = CirBuild__plus(lhs_id, rhs_id, CirParse__mach);
goto loop;
case CIRTOK_MINUS: // sub
CirLex__next();
rhs_id = multiplicative_expression();
lhs_id = CirBuild__minus(lhs_id, rhs_id, CirParse__mach);
goto loop;
default:
return lhs_id;
}
return multiplicative_expression();
}
static CirCodeId
shift_expression(void)
{
CirCodeId lhs_id, rhs_id;
lhs_id = additive_expression();
loop:
switch (cirtok.type) {
case CIRTOK_INF_INF: // <<
CirLex__next();
rhs_id = additive_expression();
lhs_id = CirBuild__lshift(lhs_id, rhs_id, CirParse__mach);
goto loop;
case CIRTOK_SUP_SUP: // >>
CirLex__next();
rhs_id = additive_expression();
lhs_id = CirBuild__rshift(lhs_id, rhs_id, CirParse__mach);
goto loop;
default:
return lhs_id;
}
}
static CirCodeId
relational_expression(void)
{
CirCodeId lhs_id, rhs_id;
lhs_id = shift_expression();
loop:
switch (cirtok.type) {
case CIRTOK_INF: // <
CirLex__next();
rhs_id = shift_expression();
lhs_id = CirBuild__lt(lhs_id, rhs_id, CirParse__mach);
goto loop;
case CIRTOK_SUP: // >
CirLex__next();
rhs_id = shift_expression();
lhs_id = CirBuild__gt(lhs_id, rhs_id, CirParse__mach);
goto loop;
case CIRTOK_INF_EQ: // <=
CirLex__next();
rhs_id = shift_expression();
lhs_id = CirBuild__le(lhs_id, rhs_id, CirParse__mach);
goto loop;
case CIRTOK_SUP_EQ: // >=
CirLex__next();
rhs_id = shift_expression();
lhs_id = CirBuild__gt(lhs_id, rhs_id, CirParse__mach);
goto loop;
case CIRTOK_EQ_EQ: // ==
CirLex__next();
rhs_id = shift_expression();
lhs_id = CirBuild__eq(lhs_id, rhs_id, CirParse__mach);
goto loop;
case CIRTOK_EXCLAM_EQ: // !=
CirLex__next();
rhs_id = shift_expression();
lhs_id = CirBuild__ne(lhs_id, rhs_id, CirParse__mach);
goto loop;
default:
return lhs_id;
}
}
static CirCodeId
equality_expression(void)
{
return relational_expression();
}
static CirCodeId
bitwise_and_expression(void)
{
return equality_expression();
}
static CirCodeId
bitwise_xor_expression(void)
{
return bitwise_and_expression();
}
static CirCodeId
bitwise_or_expression(void)
{
return bitwise_xor_expression();
}
static CirCodeId
logical_and_expression(void)
{
CirCodeId lhs_id, rhs_id;
lhs_id = bitwise_or_expression();
loop:
switch (cirtok.type) {
case CIRTOK_AND_AND: // &&
CirLex__next();
rhs_id = bitwise_or_expression();
lhs_id = CirBuild__land(lhs_id, rhs_id);
goto loop;
default:
return lhs_id;
}
}
static CirCodeId
logical_or_expression(void)
{
CirCodeId lhs_id, rhs_id;
lhs_id = logical_and_expression();
loop:
switch (cirtok.type) {
case CIRTOK_PIPE_PIPE: // ||
CirLex__next();
rhs_id = logical_and_expression();
lhs_id = CirBuild__lor(lhs_id, rhs_id);
goto loop;
default:
return lhs_id;
}
}
static CirCodeId
conditional_expression(void)
{
CirCodeId lhs_id = logical_or_expression();
if (cirtok.type == CIRTOK_QUEST) {
// Is ternary operator
CirLex__next();
CirCodeId condCodeId = lhs_id;
CirCodeId thenCodeId = comma_expression();
if (cirtok.type != CIRTOK_COLON)
unexpected_token("ternary", "`:`");
CirLex__next();
CirCodeId elseCodeId = conditional_expression();
lhs_id = CirBuild__ternary(condCodeId, thenCodeId, elseCodeId, CirParse__mach);
}
return lhs_id;
}
static CirCodeId
assignment_expression(void)
{
CirCodeId lhs_id, rhs_id;
lhs_id = conditional_expression();
switch (cirtok.type) {
case CIRTOK_EQ: // =
CirLex__next();
rhs_id = assignment_expression();
lhs_id = CirBuild__simpleAssign(lhs_id, rhs_id, CirParse__mach);
return lhs_id;
default:
return lhs_id;
}
}
static CirCodeId
expression(void)
{
return assignment_expression();
}
// Post-condition: returned CirCodeId may be a expr or cond
static CirCodeId
comma_expression(void)
{
CirCodeId code = expression();
while (cirtok.type == CIRTOK_COMMA) {
CirLex__next(); // consume COMMA
code = CirCode_toExpr(code, true);
CirCodeId code2 = expression();
CirCode_append(code, code2);
}
return code;
}
// Pre/Post-condition: !blockCode || CirCode_isExpr(blockCode)
static CirCodeId
statement(CirCodeId blockCode, bool dropValue)
{
assert(!blockCode || CirCode_isExpr(blockCode));
switch (cirtok.type) {
case CIRTOK_SEMICOLON:
// Empty statement
CirLex__next();
return blockCode;
case CIRTOK_LBRACE: {
// Nested block
CirEnv__pushLocalScope();
CirCodeId nestedBlock = block(dropValue);
assert(!nestedBlock || CirCode_isExpr(nestedBlock));
CirEnv__popScope();
if (!blockCode)
blockCode = nestedBlock;
else if (nestedBlock)
CirCode_append(blockCode, nestedBlock);
assert(!blockCode || CirCode_isExpr(blockCode));
return blockCode;
}
case CIRTOK_RETURN: {
// return
CirLex__next();
if (cirtok.type == CIRTOK_SEMICOLON) {
// No return value
if (!blockCode)
blockCode = CirCode_ofExpr(NULL);
CirStmtId return_stmt_id = CirCode_appendNewStmt(blockCode);
CirStmt_toReturn(return_stmt_id, NULL);
return blockCode;
}
CirCodeId exprCode = comma_expression();
exprCode = CirCode_toExpr(exprCode, false);
if (cirtok.type != CIRTOK_SEMICOLON)
unexpected_token("block_return_expression", "`;`");
CirLex__next();
const CirValue *returnValue = CirCode_getValue(exprCode);
if (!blockCode)
blockCode = exprCode;
else
CirCode_append(blockCode, exprCode);
CirStmtId return_stmt_id = CirCode_appendNewStmt(blockCode);
CirStmt_toReturn(return_stmt_id, returnValue);
assert(CirCode_isExpr(blockCode));
return blockCode;
}
case CIRTOK_IF: {
// If statement
CirLex__next();
if (cirtok.type != CIRTOK_LPAREN)
unexpected_token("if", "`(`");
CirLex__next(); // consume LPAREN
CirCodeId condCode = comma_expression();
if (cirtok.type != CIRTOK_RPAREN)
unexpected_token("if", "`)`");
CirLex__next(); // consume RPAREN
if (!blockCode)
blockCode = condCode;
else
CirCode_append(blockCode, condCode);
CirCodeId thenCode = statement(0, true);
CirCodeId elseCode = 0;
if (cirtok.type == CIRTOK_ELSE) {
CirLex__next(); // consume ELSE
elseCode = statement(0, true);
}
blockCode = CirBuild__if(blockCode, thenCode, elseCode);
assert(CirCode_isExpr(blockCode));
return blockCode;
}