-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.c
315 lines (285 loc) · 6.2 KB
/
util.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
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "nooc.h"
#include "stack.h"
#include "ir.h"
#include "array.h"
#include "util.h"
const char *const tokenstr[] = {
[TOK_NONE] = "TOK_NONE",
[TOK_NAME] = "TOK_NAME",
[TOK_LPAREN] = "TOK_LPAREN",
[TOK_RPAREN] = "TOK_RPAREN",
[TOK_LCURLY] = "TOK_LCURLY",
[TOK_RCURLY] = "TOK_RCURLY",
[TOK_PLUS] = "TOK_PLUS",
[TOK_MINUS] = "TOK_MINUS",
[TOK_GREATER] = "TOK_GREATER",
[TOK_COMMA] = "TOK_COMMA",
[TOK_EQUAL] = "TOK_EQUAL",
[TOK_NUM] = "TOK_NUM",
[TOK_STRING] = "TOK_STRING",
[TOK_LET] = "TOK_LET",
[TOK_IF] = "TOK_IF",
[TOK_ELSE] = "TOK_ELSE",
[TOK_LOOP] = "TOK_LOOP",
[TOK_RETURN] = "TOK_RETURN",
};
const char *const
exprkind_str(const enum exprkind kind)
{
switch (kind) {
case EXPR_LIT:
return "EXPR_LIT";
case EXPR_BINARY:
return "EXPR_BINARY";
case EXPR_IDENT:
return "EXPR_IDENT";
case EXPR_FCALL:
return "EXPR_FCALL";
case EXPR_COND:
return "EXPR_COND";
default:
die("invalid exprkind");
}
return NULL;
}
void
dumpval(const struct expr *const e)
{
switch (e->class) {
case C_INT:
fprintf(stderr, "%ld", e->d.v.v.i64);
break;
case C_STR:
fprintf(stderr, "\"%.*s\"", (int)e->d.v.v.s.len, e->d.v.v.s.data);
break;
case C_REF:
fprintf(stderr, "a reference");
break;
case C_PROC:
fprintf(stderr, "proc with %lu params", e->d.proc.in.len);
break;
}
}
void
dumpbinop(const struct binop *const op)
{
switch (op->kind) {
case BOP_PLUS:
fprintf(stderr, "BOP_PLUS");
break;
case BOP_MINUS:
fprintf(stderr, "BOP_MINUS");
break;
case BOP_GREATER:
fprintf(stderr, "BOP_GREATER");
break;
case BOP_EQUAL:
fprintf(stderr, "BOP_EQUAL");
break;
default:
die("invalid binop");
}
}
void
dumpexpr(const int indent, const struct expr *const expr)
{
for (int i = 0; i < indent; i++)
fputc(' ', stderr);
fprintf(stderr, "%s: ", exprkind_str(expr->kind));
switch (expr->kind) {
case EXPR_IDENT:
fprintf(stderr, "%.*s\n", (int)expr->d.s.len, expr->d.s.data);
break;
case EXPR_LIT:
dumpval(expr);
fputc('\n', stderr);
break;
case EXPR_BINARY:
dumpbinop(&expr->d.bop);
fputc('\n', stderr);
dumpexpr(indent + 8, &exprs.data[expr->d.bop.left]);
dumpexpr(indent + 8, &exprs.data[expr->d.bop.right]);
break;
case EXPR_COND:
dumpexpr(indent + 8, &exprs.data[expr->d.cond.cond]);
break;
case EXPR_FCALL:
fprintf(stderr, "%.*s\n", (int)expr->d.call.name.len, expr->d.call.name.data);
break;
default:
die("dumpexpr: bad expression");
}
}
static char
sigil(const int valtype)
{
switch (valtype) {
case VT_EMPTY:
return '!';
case VT_TEMP:
return '%';
case VT_IMM:
return ' ';
case VT_FUNC:
return '$';
case VT_LABEL:
return ':';
}
die("sigil: unknown valtype");
return 0; // suppress dumb error
}
void
dumpir(const struct iproc *const instrs)
{
bool callarg = false;
char sig;
for (int i = 0; i < instrs->len; i++) {
struct instr *instr = &instrs->data[i];
if (callarg && instr->op != IR_CALLARG) {
putc('\n', stderr);
callarg = false;
}
sig = sigil(instr->valtype);
switch (instr->op) {
case IR_ASSIGN:
fprintf(stderr, "%%%lu %hhu= ", instr->val, instrs->temps.data[instr->val].size);
break;
case IR_IN:
fprintf(stderr, "in %c%lu\n", sig, instr->val);
break;
case IR_IMM:
fprintf(stderr, "imm %c%lu\n", sig, instr->val);
break;
case IR_ALLOC:
fprintf(stderr, "alloc %c%lu\n", sig, instr->val);
break;
case IR_STORE:
fprintf(stderr, "store %c%lu", sig, instr->val);
break;
case IR_LOAD:
fprintf(stderr, "load %c%lu\n", sig, instr->val);
break;
case IR_ADD:
fprintf(stderr, "add %c%lu", sig, instr->val);
break;
case IR_CEQ:
fprintf(stderr, "ceq %c%lu", sig, instr->val);
break;
case IR_NOT:
fprintf(stderr, "not %c%lu\n", sig, instr->val);
break;
case IR_ZEXT:
fprintf(stderr, "zext %c%lu\n", sig, instr->val);
break;
case IR_EXTRA:
fprintf(stderr, ", %c%lu\n", sig, instr->val);
break;
case IR_CALLARG:
fprintf(stderr, ", %c%lu", sig, instr->val);
break;
case IR_CALL:
callarg = true;
fprintf(stderr, "call %c%lu", sig, instr->val);
break;
case IR_RETURN:
fputs("return\n", stderr);
break;
case IR_CONDJUMP:
fprintf(stderr, "condjump %c%lu", sig, instr->val);
break;
case IR_JUMP:
fprintf(stderr, "jump %c%lu\n", sig, instr->val);
break;
case IR_LABEL:
fprintf(stderr, "label %c%lu\n", sig, instr->val);
break;
default:
die("dumpir: unknown instruction");
}
}
if (callarg) putc('\n', stderr);
putc('\n', stderr);
for (size_t i = 0; i < instrs->temps.len; i++) {
fprintf(stderr, "%lu: using %d from %lu to %lu\n", i, instrs->temps.data[i].reg, instrs->temps.data[i].start, instrs->temps.data[i].end);
}
putc('\n', stderr);
for (size_t i = 0; i < instrs->labels.len; i++) {
fprintf(stderr, "%lu: %lu\n", i, instrs->labels.data[i]);
}
}
struct decl *
finddecl(const struct stack *const blocks, const struct slice s)
{
for (int j = blocks->idx - 1; j >= 0; j--) {
const struct block *block = blocks->data[j];
for (int i = 0; i < block->decls.len; i++) {
struct decl *decl = &block->decls.data[i];
if (slice_cmp(&s, &decl->s) == 0) {
return decl;
}
}
}
return NULL;
}
int
slice_cmp(const struct slice *const s1, const struct slice *const s2)
{
if (s1->len != s2->len)
return 1;
return memcmp(s1->data, s2->data, s1->len);
}
int
slice_cmplit(const struct slice *const s1, const char *const s2)
{
const size_t len = strlen(s2);
if (s1->len < len)
return 1;
return memcmp(s1->data, s2, len);
}
void
error(const size_t line, const size_t col, const char *error, ...)
{
va_list args;
fprintf(stderr, "%s:%lu:%lu: ", infile, line, col);
va_start(args, error);
vfprintf(stderr, error, args);
va_end(args);
fputc('\n', stderr);
exit(1);
}
void
die(const char *const error)
{
fprintf(stderr, "%s\n", error);
exit(1);
}
void *
xmalloc(const size_t size)
{
char *p = malloc(size);
if (!p)
die("malloc failed!");
return p;
}
void *
xrealloc(void *ptr, size_t size)
{
char *p = realloc(ptr, size);
if (!p)
die("realloc failed!");
return p;
}
void *
xcalloc(size_t nelem, size_t elsize)
{
char *p = calloc(nelem, elsize);
if (!p)
die("calloc failed!");
return p;
}