-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotaru.c
387 lines (339 loc) · 13.2 KB
/
hotaru.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
#include "hotaru.h"
#include "hvm.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
static const char *level_msg[COUNT_HLOG_LEVELS] = { "[FATAL]", "[ERROR]", "[WARN]", "[INFO]", };
static char format_buf[32*1024];
typedef struct hBinOpInfo {
hBinOpType type;
HVM_InstType inst;
} hBinOpInfo;
static const hBinOpInfo _binops_info[COUNT_HBINOP_TYPES] = {
[HBINOP_NONE] = { .type = HBINOP_NONE, .inst = HVM_INST_NONE, },
[HBINOP_ADD] = { .type = HBINOP_ADD, .inst = HVM_INST_ADD, },
[HBINOP_SUB] = { .type = HBINOP_SUB, .inst = HVM_INST_SUB, },
[HBINOP_MUL] = { .type = HBINOP_MUL, .inst = HVM_INST_MUL, },
[HBINOP_EQ] = { .type = HBINOP_EQ, .inst = HVM_INST_EQ, },
[HBINOP_NE] = { .type = HBINOP_NE, .inst = HVM_INST_NE, },
[HBINOP_LT] = { .type = HBINOP_LT, .inst = HVM_INST_LT, },
[HBINOP_LE] = { .type = HBINOP_LE, .inst = HVM_INST_LE, },
[HBINOP_GT] = { .type = HBINOP_GT, .inst = HVM_INST_GT, },
[HBINOP_GE] = { .type = HBINOP_GE, .inst = HVM_INST_GE, },
};
void hlog_message(hLogLevel level, const char *fmt, ...)
{
FILE *f[COUNT_HLOG_LEVELS] = { stderr, stderr, stdout, stdout, };
va_list ap;
va_start(ap, fmt);
vsnprintf(format_buf, sizeof(format_buf), fmt, ap);
fprintf(f[level], "%s %s\n", level_msg[level], format_buf);
va_end(ap);
if(level == HLOG_FATAL) {
exit(EXIT_FAILURE);
}
}
hVarBinding *hscope_append(hScope *scope, hVarBinding binding, Arena *arena)
{
UT_ASSERT(scope);
UT_ASSERT(arena);
if(scope->count + 1 > scope->capacity) {
ut_size new_capacity = scope->capacity * 2;
if(new_capacity == 0) new_capacity = 32;
hVarBinding *new_items = arena_malloc(arena, sizeof(hVarBinding)*new_capacity);
ut_memcpy(new_items, scope->items, scope->capacity * sizeof(binding));
scope->capacity = new_capacity;
scope->items = new_items; // No need to deallocate the old items since this is in arena
}
hVarBinding *res = &scope->items[scope->count];
*res = binding;
scope->count += 1;
return res;
}
hVarBinding *hscope_find(const hScope *scope, StringView name)
{
hVarBinding *res = UT_NULL;
while(scope != UT_NULL && res == UT_NULL) {
for(ut_size i = 0; i < scope->count; ++i) {
hVarBinding *cur = &scope->items[i];
if(sv_eq(cur->name, name))
res = cur;
}
scope = scope->prev;
}
return res;
}
void hstate_init(hState *state)
{
UT_ASSERT(state);
hvm_init(&state->vm);
hvm_module_init(&state->mod);
state->arena.begin = 0;
state->arena.end = 0;
state->global.prev = UT_NULL;
state->global.count = 0;
state->global.capacity = 0;
state->global.items = 0;
state->current = &state->global;
state->vsp = 0;
state->vss = 0;
}
void hstate_deinit(hState *state)
{
arena_free(&state->arena);
hvm_module_deinit(&state->mod);
}
hResult hstate_compile_expr(hState *state, const hExpr *expr)
{
UT_ASSERT(state);
UT_ASSERT(expr);
switch(expr->type) {
case HEXPR_INT_LITERAL:
{
hvm_module_append(&state->mod, HVM_MAKE_INST(
HVM_INST_PUSH,
HVM_WORD_I64(expr->as.int_literal)));
state->vsp += 1;
} break;
case HEXPR_BINOP:
{
hstate_compile_expr(state, expr->as.binop.left);
hstate_compile_expr(state, expr->as.binop.right);
hBinOpInfo info = _binops_info[expr->as.binop.type];
hvm_module_append(&state->mod, HVM_MAKE_INST(
info.inst,
HVM_NULL_WORD));
state->vsp -= 1;
} break;
case HEXPR_VAR_READ:
{
hVarBinding *var = hscope_find(state->current, expr->as.var_read.name);
if(!var) return HRES_INVALID_VARIABLE;
hvm_module_append(&state->mod, HVM_MAKE_INST(
HVM_INST_COPYABS,
HVM_WORD_U64(var->pos)));
state->vsp += 1;
} break;
default:
UT_ASSERT(0 && "Unreachable expr in hstate_compile_expr()");
break;
}
return 0;
}
hResult hstate_exec_expr(hState *state, const hExpr *expr)
{
UT_ASSERT(state);
UT_ASSERT(expr);
HVM_Inst inst;
switch(expr->type) {
case HEXPR_INT_LITERAL:
{
inst.type = HVM_INST_PUSH;
inst.op.as_i64 = expr->as.int_literal;
hvm_exec(&state->vm, inst);
} break;
case HEXPR_BINOP:
{
hstate_exec_expr(state, expr->as.binop.left);
hstate_exec_expr(state, expr->as.binop.right);
hBinOpInfo info = _binops_info[expr->as.binop.type];
inst.type = info.inst;
hvm_exec(&state->vm, inst);
} break;
case HEXPR_VAR_READ:
{
hVarBinding *var = hscope_find(state->current, expr->as.var_read.name);
if(!var) return HRES_INVALID_VARIABLE;
inst.type = HVM_INST_COPYABS;
inst.op.as_u64 = var->pos;
hvm_exec(&state->vm, inst);
} break;
default:
UT_ASSERT(0 && "Unreachable expr in hstate_exec_expr()");
break;
}
return HRES_OK;
}
hResult hstate_compile_block(hState *state, const hBlock block)
{
hvm_module_append(&state->mod, HVM_MAKE_INST(
HVM_INST_BEGIN_SCOPE,
HVM_NULL_WORD));
for(uint32_t i = 0; i < block.count; ++i) {
hstate_compile_stmt(state, &block.items[i]);
}
hvm_module_append(&state->mod, HVM_MAKE_INST(
HVM_INST_END_SCOPE,
HVM_NULL_WORD));
return HRES_OK;
}
hResult hstate_compile_stmt(hState *state, const hStmt *stmt)
{
UT_ASSERT(state);
UT_ASSERT(stmt);
switch(stmt->type) {
case HSTMT_VAR_INIT:
{
uint32_t last_sp = state->vsp;
hResult res = hstate_compile_expr(state, &stmt->as.var_init.value);
if(res != HRES_OK) return res;
hVarBinding var;
var.name = stmt->as.var_init.name;
var.pos = last_sp;
hscope_append(state->current, var, &state->arena);
} break;
case HSTMT_VAR_ASSIGN:
{
hVarBinding *var = hscope_find(state->current, stmt->as.var_assign.name);
if(!var) return HRES_INVALID_VARIABLE;
hResult res = hstate_compile_expr(state, &stmt->as.var_init.value);
if(res != HRES_OK) return res;
hvm_module_append(&state->mod, HVM_MAKE_INST(
HVM_INST_SWAPABS,
HVM_WORD_U64(var->pos)));
hvm_module_append(&state->mod, HVM_MAKE_INST(
HVM_INST_POP,
HVM_NULL_WORD));
} break;
case HSTMT_IF:
{
// HACK: (1) first we jump to the start of if's condition
hvm_module_append(&state->mod, HVM_MAKE_INST(
HVM_INST_JMP,
HVM_WORD_U64(state->mod.count + 2)));
// HACK: (2) we will jump here if body of a condition is done
uint32_t body_completed_jump_target = state->mod.count;
hvm_module_append(&state->mod, HVM_MAKE_INST(
HVM_INST_JMP,
HVM_NULL_WORD));
HVM_Inst *inst = &state->mod.items[body_completed_jump_target];
HVM_Module prev_mod = state->mod;
hvm_module_init(&state->mod);
hstate_compile_expr(state, &stmt->as._if.condition);
hstate_compile_block(state, stmt->as._if.body);
hvm_module_append(&state->mod, HVM_MAKE_INST(
HVM_INST_JMP,
HVM_WORD_U64(body_completed_jump_target)));
for(uint32_t i = 0; i < stmt->as._if._elif.count; ++i) {
hElifBlock elif = stmt->as._if._elif.items[i];
hstate_compile_expr(state, &elif.condition);
hstate_compile_block(state, elif.body);
hvm_module_append(&state->mod, HVM_MAKE_INST(
HVM_INST_JMP,
HVM_WORD_U64(body_completed_jump_target)));
}
hstate_compile_block(state, stmt->as._if._else);
for(uint32_t i = 0; i < state->mod.count; ++i)
hvm_module_append(&prev_mod, state->mod.items[i]);
inst->op = HVM_WORD_U64(prev_mod.count);
hvm_module_deinit(&state->mod);
state->mod = prev_mod;
} break;
case HSTMT_WHILE:
{
HVM_Module body_mod;
hvm_module_init(&body_mod);
HVM_Module prev_mod = state->mod;
state->mod = body_mod;
hBlock body = stmt->as._while.body;
for(uint32_t i = 0; i < body.count; ++i) {
hstate_compile_stmt(state, &body.items[i]);
}
body_mod = state->mod;
state->mod = prev_mod;
{
hvm_module_append(&state->mod, HVM_MAKE_INST(
HVM_INST_BEGIN_SCOPE,
HVM_NULL_WORD));
uint32_t while_loop_start_pc = state->mod.count;
hstate_compile_expr(state, &stmt->as._while.condition);
uint32_t count_insts_before_end_scope = body_mod.count + 2;
uint32_t while_loop_finish_pc = state->mod.count + count_insts_before_end_scope;
hvm_module_append(&state->mod, HVM_MAKE_INST(
HVM_INST_JZ,
HVM_WORD_U64(while_loop_finish_pc)));
for(uint32_t i = 0; i < body_mod.count; ++i)
hvm_module_append(&state->mod, body_mod.items[i]);
hvm_module_append(&state->mod, HVM_MAKE_INST(
HVM_INST_JMP,
HVM_WORD_U64(while_loop_start_pc)));
hvm_module_append(&state->mod, HVM_MAKE_INST(
HVM_INST_END_SCOPE,
HVM_NULL_WORD));
}
hvm_module_deinit(&body_mod);
} break;
case HSTMT_DUMP:
{
hResult res = hstate_compile_expr(state, &stmt->as.dump);
if(res != HRES_OK) return res;
hvm_module_append(&state->mod, HVM_MAKE_INST(
HVM_INST_DUMP,
HVM_NULL_WORD));
} break;
default:
{
UT_ASSERT(0 && "Unreachable stmt");
} break;
}
return 0;
}
hResult hstate_exec_stmt(hState *state, const hStmt *stmt)
{
UT_ASSERT(state);
UT_ASSERT(stmt);
switch(stmt->type) {
case HSTMT_VAR_INIT:
{
uint32_t last_sp = state->vm.sp;
hResult res = hstate_exec_expr(state, &stmt->as.var_init.value);
if(res != HRES_OK) return res;
hVarBinding var;
var.name = stmt->as.var_init.name;
var.pos = last_sp;
hscope_append(state->current, var, &state->arena);
} break;
case HSTMT_VAR_ASSIGN:
{
HVM_Inst inst;
hVarBinding *var = hscope_find(state->current, stmt->as.var_assign.name);
if(!var) return HRES_INVALID_VARIABLE;
hResult res = hstate_exec_expr(state, &stmt->as.var_assign.value);
if(res != HRES_OK) return res;
inst.type = HVM_INST_SWAPABS;
inst.op.as_u64 = var->pos;
hvm_exec(&state->vm, inst);
inst.type = HVM_INST_POP;
hvm_exec(&state->vm, inst);
} break;
case HSTMT_WHILE:
case HSTMT_IF:
{
hvm_module_init(&state->mod);
state->mod.count = 0;
hstate_compile_stmt(state, stmt);
hvm_module_append(&state->mod, HVM_MAKE_INST(
HVM_INST_HALT,
HVM_NULL_WORD));
uint32_t last_pc = state->vm.pc;
state->vm.pc = 0;
hvm_exec_module(&state->vm, state->mod);
state->vm.halt = ut_false;
state->vm.pc = last_pc;
hvm_module_deinit(&state->mod);
} break;
case HSTMT_DUMP:
{
hResult res = hstate_exec_expr(state, &stmt->as.dump);
if(res != HRES_OK) return res;
hvm_exec(&state->vm, HVM_MAKE_INST(
HVM_INST_DUMP,
HVM_NULL_WORD));
} break;
default:
{
UT_ASSERT(0 && "Unreachable expr in hstate_exec_stmt()");
} break;
}
return HRES_OK;
}