-
Notifications
You must be signed in to change notification settings - Fork 1
/
glang.h
359 lines (286 loc) · 13.2 KB
/
glang.h
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
#pragma once
#include "constants.h"
#include "commands.h"
#include "varpool.h"
#include "elf-gen.h"
#ifdef EXTRA_VERBOSE
#define GHT_CAPACITY 30
#endif
#include "ghashtable.h"
#define GARR_GENERIC(name) TEMPLATE(name, c)
#define T Command
#include "garray.h"
#undef T
#undef GARR_GENERIC
#define GARR_GENERIC(name) TEMPLATE(name, b)
#define T uint8_t
#include "garray.h"
#undef T
#undef GARR_GENERIC
#define STACK_TYPE size_t
#define ELEM_PRINTF_FORM "%lu"
#define CHEAP_DEBUG
#include "gstack.h"
#undef CHEAP_DEBUG
#undef ELEM_PRINTF_FORM
static const size_t GLANG_START_OFFSET = CODE_OFFSET_ + LOAD_VIRT_ADDR_;
enum gLang_Node_mode {
gLang_Node_mode_none,
gLang_Node_mode_add,
gLang_Node_mode_sub,
gLang_Node_mode_mul,
gLang_Node_mode_div,
gLang_Node_mode_exp,
gLang_Node_mode_opBrack,
gLang_Node_mode_clBrack,
gLang_Node_mode_opFigBrack,
gLang_Node_mode_clFigBrack,
gLang_Node_mode_semicolon,
gLang_Node_mode_comma,
gLang_Node_mode_less,
gLang_Node_mode_great,
gLang_Node_mode_assign,
gLang_Node_mode_var,
gLang_Node_mode_func,
gLang_Node_mode_num,
gLang_Node_mode_keyword,
gLang_Node_mode_func_args,
gLang_Node_mode_unknown,
gLang_Node_mode_CNT,
};
static const char gLang_Node_modeView[gLang_Node_mode_CNT][GLANG_MAX_LIT_LEN] = {
"NONE",
"+",
"-",
"*",
"/",
"^",
"(",
")",
"{",
"}",
";",
",",
"<",
">",
"=",
"VAR",
"FUNC",
"NUM",
"KEYWORD",
"FUNC_ARGS",
"UNKNOWN",
};
enum gLang_Node_keyword {
gLang_Node_keyword_if,
gLang_Node_keyword_else,
gLang_Node_keyword_return,
gLang_Node_keyword_print,
gLang_Node_keyword_while,
gLang_Node_keyword_sqrt,
gLang_Node_keyword_sin,
gLang_Node_keyword_cos,
gLang_Node_keyword_CNT,
};
static const char gLang_Node_keywordView[gLang_Node_keyword_CNT][GLANG_MAX_LIT_LEN] = {
"if",
"else",
"return",
"print",
"while",
"sqrt",
"sin",
"cos",
};
struct gLang_Node
{
gLang_Node_mode mode;
gLang_Node_keyword keyword;
char funcName[GLANG_MAX_LIT_LEN];
char varName[GLANG_MAX_LIT_LEN];
Var *var; /* To be filled by compiler; An intermediate structure that holds the position of var in register or memory */
double value;
size_t position;
} typedef gLang_Node;
typedef gLang_Node GTREE_TYPE;
#include "gtree.h"
enum gLang_status {
gLang_status_OK,
gLang_status_NothingToDo,
gLang_status_BadStructPtr,
gLang_status_TreeErr,
gLang_status_ObjPoolErr,
gLang_status_AllocErr,
gLang_status_FileErr,
gLang_status_BadPtr,
gLang_status_BadId,
gLang_status_BadInput,
gLang_status_EmptyLexer,
gLang_status_EmptyTree,
gLang_status_ParsingErr_UnknownLex,
gLang_status_ParsingErr_NoBrack,
gLang_status_ParsingErr_BadCur,
gLang_status_ParsingErr_BadRootId,
gLang_status_ParsingErr_EmptyOutp,
gLang_status_ParsingErr_NoSemicolon,
gLang_status_DecompositionErr,
gLang_status_CNT,
};
static const char gLang_statusMsg[gLang_status_CNT + 1][MAX_LINE_LEN] = {
"OK",
"Parsing block has not detected the pattern",
"Bad structure pointer provided",
"Error in gTree",
"Error in gObjPool",
"Error during allocation, ran out of memory?",
"Error in file IO",
"Bad pointer provided",
"Bad node id provided",
"WARNING: bad input provided",
"WARNING: lexemes stack is empty, have you run the lexer?",
"WARNING: expression tree is empty, have you run the parser?",
"Parsing error: unknown lexemes sequence",
"Parsing error: no closing bracket",
"Parsing error: bad cursor recieved by parser",
"Parsing error: bad rootId provided to parser subfunc",
"Parsing error: no semicolon at the end of a statement",
"Some error during tree decomposition",
"UNKNOWN ERROR CODE!",
};
/*
* WARNING: do not forget to undef new macro
*/
#define GLANG_THROW_ERR(errCode) ({ \
if (errCode >= gLang_status_ParsingErr_UnknownLex && errCode <= gLang_status_ParsingErr_NoSemicolon) \
fprintf(ctx->logStream, "Error in lexeme No. %lu:\n", GLANG_CUR_NODE_ID()); \
if (errCode >= gLang_status_CNT || errCode < 0) { \
ASSERT_LOG(false, gLang_status_CNT, \
gLang_statusMsg[gLang_status_CNT], ctx->logStream); \
} \
ASSERT_LOG(false, errCode, gLang_statusMsg[errCode], ctx->logStream); \
})
#define GLANG_ASSERT_LOG(expr, errCode) ({ \
if (!(expr)) \
GLANG_THROW_ERR(errCode); \
})
#define GLANG_CHECK_SELF_PTR(ptr) ASSERT_LOG(gPtrValid(ptr), gLang_status_BadStructPtr, \
gLang_statusMsg[gLang_status_BadStructPtr], \
stderr)
#define GLANG_IS_OK(expr) ({ \
gLang_status macroStatus = (expr); \
GLANG_ASSERT_LOG(macroStatus == gLang_status_OK, macroStatus); \
})
#define GLANG_NODE_BY_ID(macroId) ({ \
assert(macroId != -1); \
gTree_Node *macroNode = NULL; \
GLANG_ASSERT_LOG(gObjPool_get(&ctx->tree.pool, (macroId), ¯oNode) == gObjPool_status_OK, \
gLang_status_ObjPoolErr); \
assert(gPtrValid(macroNode)); \
macroNode; \
})
#define GLANG_POOL_ALLOC() ({ \
size_t macroId = -1; \
gTree_Node *macroNode = NULL; \
GLANG_ASSERT_LOG(gObjPool_alloc(&ctx->tree.pool, ¯oId) == gObjPool_status_OK, \
gLang_status_ObjPoolErr); \
GLANG_ASSERT_LOG(gObjPool_get(&ctx->tree.pool, macroId, ¯oNode) == gObjPool_status_OK, \
gLang_status_ObjPoolErr); \
macroNode->sibling = -1; \
macroNode->parent = -1; \
macroNode->child = -1; \
macroId; \
})
#ifdef GLANG_NO_FREE
#define GLANG_POOL_FREE(macroId)
#else
#define GLANG_POOL_FREE(macroId) ({ \
GLANG_ASSERT_LOG(gObjPool_free(&ctx->tree.pool, macroId) == gObjPool_status_OK, \
gLang_status_ObjPoolErr); \
})
#endif
#define GLANG_TREE_CHECK(expr) ({ \
GLANG_ASSERT_LOG((expr) == gTree_status_OK, gLang_status_TreeErr); \
})
#define GLANG_ID_CHECK(id) GLANG_ASSERT_LOG(gObjPool_idValid(&ctx->tree.pool, id), gLang_status_BadId)
#define GLANG_IS_DELIM(macroCur) (*macroCur == '\0' || isspace(*macroCur) || strnConsistsChrs(macroCur, GLANG_DELIMS_LIST, 1, strlen(GLANG_DELIMS_LIST)))
#define GLANG_CUR_NODE() ({ \
gLang_Node *macroNode = NULL; \
if (ctx->lexemeCur < ctx->LexemeIds.len) \
macroNode = &(GLANG_NODE_BY_ID(ctx->LexemeIds.data[ctx->lexemeCur])->data); \
macroNode; \
})
#define GLANG_CUR_NODE_ID() ({ \
size_t macroNodeId = -1; \
if (ctx->lexemeCur < ctx->LexemeIds.len) \
macroNodeId = ctx->LexemeIds.data[ctx->lexemeCur]; \
macroNodeId; \
})
#ifdef EXTRA_VERBOSE
#define GLANG_PARSER_CHECK() ({ \
fprintf(ctx->logStream, "%s curLit = %lu\n", __func__, GLANG_CUR_NODE_ID()); \
GLANG_CHECK_SELF_PTR(ctx); \
GLANG_ID_CHECK(rootId); \
GLANG_ASSERT_LOG(rootId != -1, gLang_status_ParsingErr_BadRootId); \
GLANG_ASSERT_LOG(ctx->lexemeCur < ctx->LexemeIds.len, gLang_status_ParsingErr_BadCur); \
})
#else
#define GLANG_PARSER_CHECK() ({ \
GLANG_CHECK_SELF_PTR(ctx); \
GLANG_ID_CHECK(rootId); \
GLANG_ASSERT_LOG(rootId != -1, gLang_status_ParsingErr_BadRootId); \
GLANG_ASSERT_LOG(ctx->lexemeCur < ctx->LexemeIds.len, gLang_status_ParsingErr_BadCur); \
})
#endif
#define PUSH_COMMAND(c) ({ \
c.line = __LINE__; \
gArr_push_c(ctx->commands, c); \
})
#define PUSH_BYTE(b) ({ \
gArr_push_b(ctx->bin, b); \
})
struct gLang {
gTree tree = {};
FILE *logStream = {};
FILE *asmOut = {};
const char *buffer = {};
size_t labelCnt = {};
GENERIC(stack) LexemeIds = {};
size_t lexemeCur = {};
varPool **varTables = {};
size_t varTablesCur = {};
size_t varTablesLen = {};
size_t *labelFixup = {};
gArr_c *commands = {};
gArr_b *bin = {};
gHT *funcFixup = {};
} typedef gLang;
gLang_status gLang_ctor(gLang *ctx, FILE *newLogStream);
gLang_status gLang_dtor(gLang *ctx);
gLang_status gLang_lexer(gLang *ctx, const char *buffer);
gLang_status gLang_parser(gLang *ctx);
static gLang_status gLang_parser_funcDef(gLang *ctx, size_t rootId);
static gLang_status gLang_parser_gram (gLang *ctx, size_t rootId);
static gLang_status gLang_parser_stmnt(gLang *ctx, size_t rootId);
static gLang_status gLang_parser_assig(gLang *ctx, size_t rootId);
static gLang_status gLang_parser_if (gLang *ctx, size_t rootId);
static gLang_status gLang_parser_expr (gLang *ctx, size_t rootId);
static gLang_status gLang_parser_cmp (gLang *ctx, size_t rootId);
static gLang_status gLang_parser_prior(gLang *ctx, size_t rootId);
static gLang_status gLang_parser_expn (gLang *ctx, size_t rootId);
static gLang_status gLang_parser_term (gLang *ctx, size_t rootId);
static gLang_status gLang_parser_func (gLang *ctx, size_t rootId);
static gLang_status gLang_parser_while(gLang *ctx, size_t rootId);
static gLang_status gLang_parser_blk (gLang *ctx, size_t rootId);
static gLang_status gLang_parser_retrn(gLang *ctx, size_t rootId);
static gLang_status gLang_parser_print(gLang *ctx, size_t rootId);
gLang_status gLang_optimize(gLang *ctx, const size_t rootId);
static gLang_status gLang_fillVarTable(gLang *ctx, size_t rootId);
static gLang_status gLang_compileExpr(gLang *ctx, size_t rootId);
static gLang_status gLang_compileStmnt(gLang *ctx, size_t rootId);
static gLang_status gLang_compileBlk(gLang *ctx, size_t siblingId);
static gLang_status gLang_getArgs(gLang *ctx, size_t siblingId);
gLang_status gLang_compile(gLang *ctx);
gLang_status gLang_translate(gLang *ctx, bool fixupRun);
gLang_status gLang_dumpBytes(gLang *ctx, FILE *out);
gLang_status gLang_commandsDump(gLang *ctx, FILE *out);
gLang_status gLang_writeBin(gLang *ctx, FILE *out);