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
|
struct func;
enum tokenkind {
TNONE,
TEOF,
TNEWLINE,
TIDENT,
TNUMBER,
TCHARCONST,
TSTRINGLIT,
/* keyword */
TAUTO,
TBREAK,
TCASE,
TCHAR,
TCONST,
TCONTINUE,
TDEFAULT,
TDO,
TDOUBLE,
TELSE,
TENUM,
TEXTERN,
TFLOAT,
TFOR,
TGOTO,
TIF,
TINLINE,
TINT,
TLONG,
TREGISTER,
TRESTRICT,
TRETURN,
TSHORT,
TSIGNED,
TSIZEOF,
TSTATIC,
TSTRUCT,
TSWITCH,
TTYPEDEF,
TUNION,
TUNSIGNED,
TVOID,
TVOLATILE,
TWHILE,
T_ALIGNAS,
T_ALIGNOF,
T_ATOMIC,
T_BOOL,
T_COMPLEX,
T_GENERIC,
T_IMAGINARY,
T_NORETURN,
T_STATIC_ASSERT,
T_THREAD_LOCAL,
T__TYPEOF__,
/* punctuator */
TLBRACK,
TRBRACK,
TLPAREN,
TRPAREN,
TLBRACE,
TRBRACE,
TPERIOD,
TARROW,
TINC,
TDEC,
TBAND,
TMUL,
TADD,
TSUB,
TBNOT,
TLNOT,
TDIV,
TMOD,
TSHL,
TSHR,
TLESS,
TGREATER,
TLEQ,
TGEQ,
TEQL,
TNEQ,
TXOR,
TBOR,
TLAND,
TLOR,
TQUESTION,
TCOLON,
TSEMICOLON,
TELLIPSIS,
TASSIGN,
TMULASSIGN,
TDIVASSIGN,
TMODASSIGN,
TADDASSIGN,
TSUBASSIGN,
TSHLASSIGN,
TSHRASSIGN,
TBANDASSIGN,
TXORASSIGN,
TBORASSIGN,
TCOMMA,
THASH,
THASHHASH,
};
struct location {
const char *file;
size_t line, col;
};
struct token {
enum tokenkind kind;
struct location loc;
char *lit;
};
enum typequal {
QUALNONE,
QUALCONST = 1<<1,
QUALRESTRICT = 1<<2,
QUALVOLATILE = 1<<3,
QUALATOMIC = 1<<4,
};
enum typekind {
TYPENONE,
TYPEVOID,
TYPEBOOL,
TYPECHAR,
TYPESHORT,
TYPEINT,
TYPEENUM,
TYPELONG,
TYPELLONG,
TYPEFLOAT,
TYPEDOUBLE,
TYPELDOUBLE,
TYPEPOINTER,
TYPEARRAY,
TYPEFUNC,
TYPESTRUCT,
TYPEUNION,
};
enum typeprop {
PROPNONE,
PROPOBJECT = 1<<0,
PROPCHAR = 1<<1,
PROPINT = 1<<2,
PROPREAL = 1<<3,
PROPARITH = 1<<4,
PROPSCALAR = 1<<5,
PROPAGGR = 1<<6,
PROPDERIVED = 1<<7,
PROPFLOAT = 1<<8,
};
struct param {
char *name;
struct type *type;
enum typequal qual;
struct value *value;
struct param *next;
};
struct bitfield {
short before; /* number of bits in the storage unit before the bit-field */
short after; /* number of bits in the storage unit after the bit-field */
};
struct member {
char *name;
struct type *type;
enum typequal qual;
uint64_t offset;
struct bitfield bits;
struct member *next;
};
struct type {
enum typekind kind;
enum typeprop prop;
int align;
uint64_t size;
struct repr *repr;
union {
struct type *base;
struct list link; /* used only during construction of type */
};
/* qualifiers of the base type */
enum typequal qual;
_Bool incomplete;
union {
struct {
_Bool issigned, iscomplex;
} basic;
struct {
uint64_t length;
} array;
struct {
_Bool isprototype, isvararg, isnoreturn, paraminfo;
struct param *params;
} func;
struct {
char *tag;
struct member *members;
} structunion;
};
};
enum declkind {
DECLTYPE,
DECLOBJECT,
DECLFUNC,
DECLCONST,
DECLBUILTIN,
};
enum linkage {
LINKNONE,
LINKINTERN,
LINKEXTERN,
};
enum builtinkind {
BUILTINALLOCA,
BUILTINCONSTANTP,
BUILTININFF,
BUILTINNANF,
BUILTINOFFSETOF,
BUILTINTYPESCOMPATIBLEP,
BUILTINVAARG,
BUILTINVACOPY,
BUILTINVAEND,
BUILTINVALIST,
BUILTINVASTART,
};
struct decl {
enum declkind kind;
enum linkage linkage;
struct type *type;
enum typequal qual;
struct value *value;
/* objects and functions */
struct list link;
int align; /* may be more strict than type requires */
_Bool tentative, defined;
/* built-ins */
enum builtinkind builtin;
};
struct scope {
struct map *tags;
struct map *decls;
struct value *breaklabel;
struct value *continuelabel;
struct switchcases *switchcases;
struct scope *parent;
};
enum exprkind {
/* primary expression */
EXPRIDENT,
EXPRCONST,
EXPRSTRING,
/* postfix expression */
EXPRCALL,
/* member E.M gets transformed to *(typeof(E.M) *)((char *)E + offsetof(typeof(E), M)) */
EXPRBITFIELD,
EXPRINCDEC,
EXPRCOMPOUND,
/* subscript E1[E2] gets transformed to *((E1)+(E2)) */
EXPRUNARY,
EXPRCAST,
EXPRBINARY,
EXPRCOND,
EXPRASSIGN,
EXPRCOMMA,
EXPRBUILTIN,
EXPRTEMP,
};
struct expr {
enum exprkind kind;
/* whether this expression is an lvalue */
_Bool lvalue;
/* whether this expression is a pointer decayed from an array or function designator */
_Bool decayed;
/* the unqualified type of the expression */
struct type *type;
/* the type qualifiers of the object this expression refers to (ignored for non-lvalues) */
enum typequal qual;
struct expr *next;
union {
struct {
struct decl *decl;
} ident;
union {
uint64_t i;
double f;
} constant;
struct {
char *data;
size_t size;
} string;
struct {
struct expr *func, *args;
size_t nargs;
} call;
struct {
struct expr *base;
struct bitfield bits;
} bitfield;
struct {
struct init *init;
} compound;
struct {
enum tokenkind op;
_Bool post;
struct expr *base;
} incdec;
struct {
enum tokenkind op;
struct expr *base;
} unary;
struct {
struct expr *e;
} cast;
struct {
enum tokenkind op;
struct expr *l, *r;
} binary;
struct {
struct expr *e, *t, *f;
} cond;
struct {
struct expr *l, *r;
} assign;
struct {
struct expr *exprs;
} comma;
struct {
int kind;
struct expr *arg;
} builtin;
struct value *temp;
};
};
struct init {
uint64_t start, end;
struct expr *expr;
struct bitfield bits;
struct init *next;
};
/* token */
extern struct token tok;
void tokprint(const struct token *);
void tokdesc(char *, size_t, enum tokenkind, const char *);
_Noreturn void error(const struct location *, const char *, ...);
/* scan */
int scanfrom(const char *file);
void scan(struct token *);
void ppinit(const char *);
void next(void);
_Bool peek(int);
char *expect(int, const char *);
_Bool consume(int);
/* type */
struct type *mktype(enum typekind, enum typeprop);
struct type *mkpointertype(struct type *, enum typequal);
struct type *mkarraytype(struct type *, enum typequal, uint64_t);
_Bool typecompatible(struct type *, struct type *);
_Bool typesame(struct type *, struct type *);
struct type *typecomposite(struct type *, struct type *);
struct type *typeunqual(struct type *, enum typequal *);
struct type *typecommonreal(struct type *, struct type *);
struct type *typeargpromote(struct type *);
struct type *typeintpromote(struct type *);
enum typeprop typeprop(struct type *);
struct member *typemember(struct type *, const char *, uint64_t *);
struct param *mkparam(char *, struct type *, enum typequal);
extern struct type typevoid;
extern struct type typebool;
extern struct type typechar, typeschar, typeuchar;
extern struct type typeshort, typeushort;
extern struct type typeint, typeuint;
extern struct type typelong, typeulong;
extern struct type typellong, typeullong;
extern struct type typefloat, typedouble, typeldouble;
extern struct type typevalist, typevalistptr;
/* decl */
struct decl *mkdecl(enum declkind, struct type *, enum typequal, enum linkage);
_Bool decl(struct scope *, struct func *);
struct type *typename(struct scope *, enum typequal *);
struct decl *stringdecl(struct expr *);
void emittentativedefns(void);
/* scope */
void scopeinit(void);
struct scope *mkscope(struct scope *);
struct scope *delscope(struct scope *);
void scopeputdecl(struct scope *, const char *, struct decl *);
struct decl *scopegetdecl(struct scope *, const char *, _Bool);
void scopeputtag(struct scope *, const char *, struct type *);
struct type *scopegettag(struct scope *, const char *, _Bool);
extern struct scope filescope;
/* expr */
struct expr *expr(struct scope *);
struct expr *assignexpr(struct scope *);
struct expr *constexpr(struct scope *);
uint64_t intconstexpr(struct scope *, _Bool);
void delexpr(struct expr *);
struct expr *exprconvert(struct expr *, struct type *);
/* eval */
struct expr *eval(struct expr *);
/* init */
struct init *mkinit(uint64_t, uint64_t, struct bitfield, struct expr *);
struct init *parseinit(struct scope *, struct type *);
void stmt(struct func *, struct scope *);
/* backend */
struct gotolabel {
struct value *label;
_Bool defined;
};
struct switchcases {
void *root;
struct value *defaultlabel;
};
struct repr;
struct switchcases *mkswitch(void);
void switchcase(struct switchcases *, uint64_t, struct value *);
struct value *mkblock(char *);
struct value *mkglobal(char *, _Bool);
struct value *mkintconst(struct repr *, uint64_t);
uint64_t intconstvalue(struct value *);
struct func *mkfunc(char *, struct type *, struct scope *);
struct type *functype(struct func *);
void funclabel(struct func *, struct value *);
struct value *funcexpr(struct func *, struct expr *);
void funcjmp(struct func *, struct value *);
void funcjnz(struct func *, struct value *, struct value *, struct value *);
void funcret(struct func *, struct value *);
struct gotolabel *funcgoto(struct func *, char *);
void funcswitch(struct func *, struct value *, struct switchcases *, struct value *);
void funcinit(struct func *, struct decl *, struct init *);
void emitfunc(struct func *, _Bool);
void emitdata(struct decl *, struct init *);
extern struct repr i8, i16, i32, i64, f32, f64;
|