-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompintern.h
More file actions
591 lines (552 loc) · 19 KB
/
compintern.h
File metadata and controls
591 lines (552 loc) · 19 KB
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
#ifndef COMPINTERN_H
#define COMPINTERN_H
#undef _GNU_SOURCE
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include "joecc_assert.h"
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include "bf.h"
#include "qhash.h"
#include "dynarr.h"
#include "dynstr.h"
#define PPDEBUG 0
extern DYNARR* includepath;
typedef struct {
int first_line;
int last_line;
int first_column;
int last_column;
char* filename;
} LOCTYPE;
typedef struct {
int line;
int column;
char* filename;
} HALFLOC;
static inline HALFLOC* halvestart(LOCTYPE* l) {
HALFLOC* h = malloc(sizeof(HALFLOC));
h->line = l->first_line;
h->column = l->first_column;
h->filename = l->filename;
return h;
}
static inline HALFLOC* halveend(LOCTYPE* l) {
HALFLOC* h = malloc(sizeof(HALFLOC));
h->line = l->last_line;
h->column = l->last_column;
h->filename = l->filename;
return h;
}
/**
* An enum describing a bitfield with a bunch of information about the type of an AST value.
* The lowest 4 bits in this enum are not listed here, they describe the number of bytes of the value.
**/
typedef enum {
FLOATNUM = 0x10,
UNSIGNEDNUM = 0x20,
CONSTNUM = 0x40,
VOLATILENUM = 0x80,
STATICNUM = 0x100,
EXTERNNUM = 0x200,
RESTRICTNUM = 0x400,
GLOBALFUNC = 0x800,
VOIDNUM = 0x1000,
ENUMVAL = 0x2000,
STRUCTVAL = 0x4000,
UNIONVAL = 0x8000,
ANONMEMB = 0x10000,
INLINED = 0x20000,
} TYPEBITS;
#define EXPRTYPELIST \
X(NOP), X(STRING), X(INT), X(UINT), X(FLOAT), X(MEMBER), X(IDENT), X(ARRAY_LIT), X(STRUCT_LIT), \
X(ADD), X(NEG), X(SUB), X(EQ), X(NEQ), X(GT), X(LT), X(GTE), X(LTE), X(MULT), X(DIVI), X(MOD), \
X(PREINC), X(POSTINC), X(PREDEC), X(POSTDEC), \
X(L_AND), X(L_OR), X(L_NOT), X(B_AND), X(B_OR), X(B_XOR), X(B_NOT), X(SHL), X(SHR), \
X(DOTOP), X(ARROW), \
X(SZOF), X(SZOFEXPR), \
X(ASSIGN), \
X(ADDASSIGN), X(SUBASSIGN), X(SHLASSIGN), X(SHRASSIGN), X(ANDASSIGN), \
X(XORASSIGN), X(ORASSIGN), X(DIVASSIGN), X(MULTASSIGN), X(MODASSIGN), \
X(CAST), \
X(COMMA), \
X(ADDR), X(DEREF), \
X(FCALL), \
X(TERNARY)
/**
* An enum describing the type of an expression in the AST.
**/
#define X(name) name
typedef enum {
EXPRTYPELIST
} EXPRTYPE;
#undef X
#define STMTTYPE_LIST \
X(FRET), X(LBREAK), X(JGOTO), X(LCONT), \
X(WHILEL), X(DOWHILEL), X(FORL), \
X(IFS), X(IFELSES), \
X(SWITCH), \
X(CASE), X(LABEL), \
X(CMPND), \
X(EXPR), X(NOPSTMT), \
X(ASMSTMT), \
X(DEFAULT)
/**
* An enum describing the type of a statement in the AST.
**/
#define X(name) name
enum stmttype {
STMTTYPE_LIST
};
#undef X
#define DECLPART_TYPE X(POINTERSPEC), X(ARRAYSPEC), X(VLASPEC), X(PARAMSSPEC), X(BITFIELDSPEC), X(NAMELESS_PARAMSSPEC)
/**
* An enum describing the type of an element of the pointerstack, whether it's a generic pointer, an array, a function pointer, a VLA, or something else
**/
#define X(name) name
enum declpart_info {
DECLPART_TYPE
};
#undef X
struct stmt;
/**
* A struct containing the information to describe a struct or union: most imporatntly the fields, their bit offsets from the base location of the struct
**/
typedef struct {
DYNARR* fields;//Each entry is a struct that contains a full identifier
char* name;
QHASHTABLE* offsets; //each entry is a struct that contains a full identifier and offset which is IN BITS rather than bytes in order to accommodate bitfields
int size; //size of the structure in bytes
} USTRUCT;
/**
* A struct containing the information necessary to describe an enum
**/
typedef struct {
DYNARR* fields;
char* name;
} ENUM;
/**
* A struct containing the information to describe a type, whether it contains a struct, has multiple pointer indirection, or not.
**/
typedef struct {
DYNARR* pointerstack;
TYPEBITS tb;
union {
USTRUCT* structtype;
USTRUCT* uniontype;
ENUM* enumtype;
};
} IDTYPE;
/**
* A struct containing the information to describe a single field of a struct
**/
typedef struct {
long offset;
IDTYPE* type;
} STRUCTFIELD;
/**
* A struct describing a single operand in the operands portion of an asm statement
**/
typedef struct {
char* constraint;
struct expr* varin;
} OPERAND;
/**
* A struct describing the information associated with a single identifier
* An index of -1 refers to a not yet specified index and an index of -2 refers to a function with global scope.
**/
typedef struct {
IDTYPE* type;
char* name;
long index;
} IDENTIFIERINFO;
/**
* A struct containing all the information necessary to process/describe a compilation unit (function), in AST form.
* The body field contains the root of the actual AST in a compound statement.
* The params, retrn, lbls, and numvars fields should be self-evident.
* The switchstack field is necessary during compilation, it contains a stack of the switch statements that enclose
* the currently compiling statement/expression. This is needed to convert switch cases into labels to clarify the
* AST representation of switches. The caseindex is used to generate unique label names for each case in a function.
* is currently
**/
typedef struct {
char* name;
struct stmt* body; //compound statement
DYNARR* params;
IDTYPE* retrn;
QHASHTABLE* lbls;
DYNARR* switchstack;
int caseindex;
int numvars;
} FUNC;
/**
* A struct containing all the information necessary to describe an expression in the AST representation.
* Type tags which anonymous union member is used, the id member is used only for sizeof.
**/
typedef struct expr {
EXPRTYPE type;
DYNARR* params;
IDTYPE* rettype;
union {
IDTYPE* vartype;
char* strconst;
char* member;
long intconst;
unsigned long uintconst;
double floatconst;
IDENTIFIERINFO* id;
};
int locstartind;
int locendind;
} EXPRESSION;
/**
* A struct storing state necessary for the processing of an array with designated initializers. Curpt
* is the last number designator that we have seen, and non designated elements increment that.
**/
typedef struct {
DYNARR* inits;
int curpt;
} DESIGNARR;
/**
* Contains the info for constructing a switch statement--the names of each case label, and the name of the default case
**/
typedef struct {
LVHASHTABLE* cases;
DYNARR* caseorder;
char* defaultval;
} SWITCHINFO;
/**
* Stores some "global" state information for the lexer, which we need in a struct for the reentrant parser.
* argpp is the array of arguments passed to the current macro that is being called
* locs is the stack of location information structs
* parg is the array of arguments that we have parsed so far to the current macro call
* stmtover, argeaten are simple status flags
* defname is the name of hte macro currently being defined
* defargs are the arguments of the macro currently being called, i.e. which things to replace
* paren_depth is the number of nested parens while lexing macro arguments, to tell when a comma ends an arg.
* md is the macro currently being defined
* dstrdly is the dynamic string currently being parsed, mdstrdly is the same but for a macro dynstring
* strcur is the same but for an actual string literal.
**/
struct lstate {
DYNARR* argpp;
DYNARR* locs;
DYNARR* parg;
char stmtover, argeaten;
char* defname;
QHASHTABLE* defargs;
int paren_depth;
struct macrodef* md;
DYNSTR* dstrdly, * mdstrdly, * strcur;
};
/**
* Stores some "global" state information for the parser and lexer, which we need to struct wrap for a reentrant parser.
* funcs stores the already declared functions.
* scopes is a stack of the scopes within which we are currently parsing, useful for variable definitions
* definestack is the stack of macros that we are currently in the process of parsing
* withindefines takes all the names of the macros from definestack into a hashmap, used for disallowing recursive macro execution
* enstruct2free allows STRUCT structs and UNION structs to be lazily freed rather than doing any complex reference counting
* enumerat2free is the same but for enums
* globals, externglobals, ls should be evident
* actualroot holds the actual file that we want to start parsing/lexing at. We start the lexer off looking at a file which
* we have constructed which contains the -D definitions passed on the command line, and switch to this new actualroot and
* reassign it to null when that is done.
**/
struct lexctx {
QHASHTABLE* funcs;
QHASHTABLE* defines;
QHASHTABLE* withindefines;
DYNARR* scopes;
DYNARR* definestack;
DYNARR* enstruct2free;
DYNARR* enumerat2free;
DYNARR* globals;
DYNARR* externglobals;
DYNARR* halflocs;
FUNC* func;
FILE* actualroot;
char* rootname;
struct lstate* ls;
char failurestate;
};
/**
* Stores an expression or initializer, tagged, used for stuff before the first semicolon in a for loop head.
**/
typedef struct {
char isE;
union {
DYNARR* I;
EXPRESSION* E;
};
} EOI;
/**
* Stores a (surprise, surprise) statement, type tags which element of the anonymous enum is used.
**/
typedef struct stmt {
enum stmttype type;
int locstartind;
int locendind;
union {
EXPRESSION* expression;
struct { //if or if/else
EXPRESSION* ifcond;
struct stmt* thencond;
struct stmt* elsecond;
};
struct { //while or dowhile, switch
EXPRESSION* cond;
struct stmt* body;
SWITCHINFO* switchinfo;
};
struct { //for
EOI* forinit;
EXPRESSION* forcond;
EXPRESSION* increment;
struct stmt* forbody;
};
struct { //case
EXPRESSION* casecond;
char* caselabel;
};
struct {
char* asmstmts;
DYNARR* outputs;
DYNARR* inputs;
DYNARR* clobbers;
};
char* glabel; //for label and goto
DYNARR* stmtsandinits; //compound
};
} STATEMENT;
/**
* Stores a statement or initializer, used as a generic list element for i.e. a compound statement.
**/
typedef struct {
char isstmt;
union {
struct stmt* state;
DYNARR* init;
};
} SOI;
/**
* Stores the name, and an expression giving a value, to an element, describes a value in an enum.
**/
typedef struct {
char* name;
EXPRESSION* value;
} ENUMFIELD;
/**
* Describes a single part of a pointer, type tags which field is used. These are put on the pointerstack of an IDTYPE.
* PARAMSSPEC describes a function (NOT a function pointer, any POINTERSPECs on top of it make it a function pointer)
* PARAMSSPEC has an array of its arguments. Anything above it on the pointerstack describes it (i.e. whether it's a
* function pointer, array of function pointers, etc), anything below it describes the return type.
* NAMELESS_PARAMSSPEC is the same but the parameter representation does not include the names of each parameter.
* ARRAYSPEC describes an array, it contains the maximum index, and the length of the array in bytes, but these aren't
* guaranteed to be populated right after its declaration is processed due to i.e. int foo[] syntax.
* VLASPEC describes a VLA, it has an expression describing its length, and addrun and addrty describing the
* location and type respectively of the place where the length of the VLA is stored in 3ac, only accessible/useful
* while the 3ac gets generated.
**/
struct declarator_part {
enum declpart_info type;
union {
DYNARR* params;
DYNARR* nameless_params;
struct {
int arrlen;
int arrmaxind;
};
struct {
EXPRESSION* vlaent;
void* addrun;
int addrty;
};
EXPRESSION* bfspec;
TYPEBITS ptrspec;
void* garbage;
};
};
/**
* Describes a declaration, including the name, type, and index
**/
typedef struct {
char* varname;
IDTYPE* type;
long varid;
int locstartind;
int locendind;
} DECLARATION;
/**
* It is very obvious what this is
**/
typedef struct {
DECLARATION* decl;
EXPRESSION* expr;
int locstartind;
int locendind;
} INITIALIZER;
/**
* Tags the type of a scope member
**/
#define MEMBERTYPELIST X(M_TYPEDEF), X(M_VARIABLE), X(M_GLOBAL), X(M_STRUCT), X(M_UNION), X(M_ENUM), X(M_ENUM_CONST)
#define X(name) name
enum membertype {
MEMBERTYPELIST
};
#undef X
/**
* Describes a member of a scope, tagged by mtype. Members of scopes are any scope-bound thing that is usable (i.e. variables, typedefs, structs, etc.)
**/
typedef struct {
enum membertype mtype;
union {
USTRUCT* structmemb;
ENUM* enummemb;
USTRUCT* unionmemb;
IDTYPE* typememb;
IDENTIFIERINFO* idi;
EXPRESSION* enumnum;
void* garbage;
};
} SCOPEMEMBER;
/**
* Describes a scope, as placed on the scopestack--truescope tags whether it is a scope with the normal members or a fakescope.
* Fakescopes are used within union/struct bodies in order to scope-bind the members declared there.
**/
typedef struct {
char truescope;
union {
struct {
QHASHTABLE* typesdef;//SCOPEMEMBER argument
QHASHTABLE* members;//SCOPEMEMBER argument
QHASHTABLE* structs;
QHASHTABLE* enums;
QHASHTABLE* unions;
QHASHTABLE* forwardstructs;
QHASHTABLE* forwardunions;
};
QHASHTABLE* fakescope;
};
} SCOPE;
/**
* Possible states for the lexer to be in, to be pushed onto a stack. These states are pushed to the stack/updated when we encounter a #if #ifdef #else, etc.
* IFDEFDUMMY, IFANDFALSE, ELSEANDTRUE tell the lexer that we have not been lexing actual code since the value was pushed to the stack.
**/
enum ifdefstate {
IFDEFDUMMY, IFANDTRUE, IFANDFALSE, ELSEANDTRUE, ELSEANDFALSE
};
/**
* Describes a macro, complete with the body of the macro in text, and the arguments of the macro, in string form, in args
* Used within the lexer to actually effect the macro replacements
**/
struct macrodef {
DYNSTR* text;
DYNARR* args;//NULL if macro is not function like
};
#define ispointer(x) ((x)->pointerstack && (x)->pointerstack->length)
#define ispointer2(x) ((x).pointerstack && (x).pointerstack->length)
//gets size of IDTYPE in bytes
static inline int lentype(IDTYPE* idt) {
if(ispointer(idt)) {
struct declarator_part* pointtop = dapeek(idt->pointerstack);
if(pointtop->type == VLASPEC) {
return -1;
} else if(pointtop->type != ARRAYSPEC) {
return 0x8;
} else {
if(pointtop->arrlen == -1) {
idt->pointerstack->length--;
pointtop->arrlen = lentype(idt) * pointtop->arrmaxind;
idt->pointerstack->length++;
assert(pointtop->arrlen > 0);
}
return pointtop->arrlen;
}
} else if(idt->tb & (STRUCTVAL | UNIONVAL)) {
return idt->structtype->size;
}
return idt->tb & 0xf;
}
USTRUCT* ustructor(char* name, DYNARR* fields, struct lexctx* lct);
ENUM* enumctor(char* name, DYNARR* fields, struct lexctx* lct);
IDTYPE* fcid2(IDTYPE* idt);
OPERAND* genoperand(char* constraint, EXPRESSION* varin);
EXPRESSION* cloneexpr(EXPRESSION* orig);
DYNARR* ptrdaclone(DYNARR* opointerstack);
EXPRESSION* ct_nop_expr(int locstartind, int locendind);
EXPRESSION* ct_unary_expr(EXPRTYPE t, EXPRESSION* param, int locstartind, int locendind);
EXPRESSION* ct_sztype(IDTYPE* whichtype, int locstartind, int locendind);
EXPRESSION* ct_binary_expr(EXPRTYPE t, EXPRESSION* param1, EXPRESSION* param2);
EXPRESSION* ct_cast_expr(IDTYPE* type, EXPRESSION* expr, int locstartind);
EXPRESSION* ct_ternary_expr(EXPRESSION* param1, EXPRESSION* param2, EXPRESSION* param3);
EXPRESSION* ct_fcall_expr(EXPRESSION* func, DYNARR* params, int locendind);
EXPRESSION* ct_strconst_expr(const char* str, int locstartind, int locendind);
EXPRESSION* ct_intconst_expr(long num, int locstartind, int locendind);
EXPRESSION* ct_uintconst_expr(unsigned long num, int locstartind, int locendind);
EXPRESSION* ct_floatconst_expr(double num, int locstartind, int locendind);
EXPRESSION* ct_array_lit(DYNARR* da, int locstartind, int locendind);
EXPRESSION* ct_member_expr(char* member, int locstartind, int locendind);
EXPRESSION* ct_ident_expr(struct lexctx* lct, char* ident, int locstartind, int locendind);
char typecompat(IDTYPE* t1, IDTYPE* t2);
int process_array_lit(IDTYPE* arr_memtype, EXPRESSION* arr_expr);
int process_struct_lit(IDTYPE* struct_memtype, EXPRESSION* struct_expr);
char type_compat(IDTYPE* id1, IDTYPE* id2);
void freedecl(DECLARATION* dcl);
void wipestruct(USTRUCT* strct);
void fef(ENUMFIELD* enf);
void freenum(ENUM* enm);
void freedeclpart(struct declarator_part* dclp);
void freetype(IDTYPE* id);
void freeinit(INITIALIZER* i);
void freesoi(SOI* soi);
void freesai(DYNARR* stmtsandinits);
void rfreexpr(EXPRESSION* e);
void rfreestate(STATEMENT* s);
void rfreefunc(FUNC* f);
void freemd(struct macrodef* mds);
void freemd2(struct macrodef* mds);
EXPRESSION* rclonexpr(EXPRESSION* e);
DECLARATION* mkdeclaration(char* name);
INITIALIZER* geninit(DECLARATION* decl, EXPRESSION* expr, int locstartind, int locendind);
SOI* sois(struct stmt* state);
SOI* soii(DYNARR* init);
STATEMENT* mkexprstmt(enum stmttype type, EXPRESSION* express, int locstartind, int locendind);
STATEMENT* mknopstmt(void);
STATEMENT* mkgotostmt(char* gotoloc, int locstartind, int locendind);
STATEMENT* mkforstmt(EOI* e1, EXPRESSION* e2, EXPRESSION* e3, STATEMENT* bdy, int locstartind);
STATEMENT* mklsstmt(enum stmttype type, EXPRESSION* condition, STATEMENT* bdy, int locstartind, int locendind);
STATEMENT* mkswitchstmt(EXPRESSION* contingent, STATEMENT* bdy, SWITCHINFO* swi, int locstartind);
STATEMENT* mkifstmt(EXPRESSION* condition, STATEMENT* ifbdy, STATEMENT* elsebdy, int locstartind);
STATEMENT* mkcmpndstmt(DYNARR* stmtsandinits, int locstartind, int locendind);
STATEMENT* mklblstmt(struct lexctx* lct, char* lblval, int locstartind, int locendind);
STATEMENT* mkcasestmt(struct lexctx* lct, EXPRESSION* casexpr, char* label, int locstartind, int locendind);
STATEMENT* mkdefaultstmt(struct lexctx* lct, char* label, int locstartind, int locendind);
STATEMENT* mkasmstmt(char* asmstmts, DYNARR* outputs, DYNARR* inputs, DYNARR* clobbers, int locstartind, int locendind);
ENUMFIELD* genenumfield(char* name, EXPRESSION* value);
struct declarator_part* mkdeclpart(enum declpart_info typ, void* d);
struct declarator_part* mkdeclpartarr(enum declpart_info typ, EXPRESSION* d);
struct declarator_part* mkdeclptr(TYPEBITS d);
FUNC* ct_function(char* name, STATEMENT* body, DYNARR* params, IDTYPE* retrn);
struct lexctx* ctxinit(FILE *, char*);
SCOPE* mkscope(void);
SCOPE* mkfakescope(void);
void scopepush(struct lexctx* lct);
void fakescopepush(struct lexctx* lct);
void scopepop(struct lexctx* lct);
SCOPE* fakescopepeek(struct lexctx* lct);
SCOPE* scopepeek(struct lexctx* lct);
void* scopesearch(struct lexctx* lct, enum membertype mt, char* key);
char scopequeryval(struct lexctx* lct, enum membertype mt, char* key);
void defbackward(struct lexctx* lct, enum membertype mt, char* defnd, USTRUCT* assignval);
INITIALIZER* decl2scope(DECLARATION* dec, EXPRESSION* ex, struct lexctx* lct);
void add2scope(struct lexctx* lct, char* memname, enum membertype mtype, void* memberval);
int feedstruct(USTRUCT* s);
int unionlen(USTRUCT* u);
#define locprint(lv) lv.filename, lv.first_line, lv.first_column, lv.last_line, lv.last_column
#define dlocprint(lv) lv->filename, lv->first_line, lv->first_column, lv->last_line, lv->last_column
#define locprint2(lv) yyget_lloc(yyscanner)->filename, lv->first_line, lv->first_column, lv->last_line, lv->last_column
#define ctx ((struct lexctx*) yyget_extra(scanner))
#endif