357 lines
10 KiB
C
357 lines
10 KiB
C
#include "ir.h"
|
|
#include "ast.h"
|
|
#include "collection.h"
|
|
#include "sema.h"
|
|
#include <assert.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static void keymap_irval_dump_fn(const void* entry, FILE* file)
|
|
{
|
|
irval_dump(*(IrVal**)entry, file, nullptr);
|
|
}
|
|
|
|
void astlowerer_init(AstLowerer* l, Sema* sema)
|
|
{
|
|
*l = (AstLowerer) {
|
|
.cx = {
|
|
.arena = { },
|
|
ARRAY_FIELD_INIT(fns),
|
|
ARRAY_FIELD_INIT(readonly_vals),
|
|
},
|
|
.a = nullptr,
|
|
.sema = sema,
|
|
.curr_fn = nullptr,
|
|
.curr_block = nullptr,
|
|
.sym_map = { },
|
|
};
|
|
|
|
l->a = &l->cx.arena;
|
|
|
|
arena_init(l->a, 8192);
|
|
keymap_init(&l->sym_map, sizeof(IrVal*));
|
|
l->sym_map.dump_entry = keymap_irval_dump_fn;
|
|
}
|
|
|
|
void astlowerer_deinit(AstLowerer* l)
|
|
{
|
|
arena_deinit(l->a);
|
|
keymap_deinit(&l->sym_map);
|
|
}
|
|
|
|
typedef AstLowerer L;
|
|
|
|
static void l_lower_top_declar(L* l, AstStmt* stmt, AstDeclar* declar);
|
|
static void l_lower_fn(L* l, AstStmt* stmt);
|
|
static void l_lower_stmtblock(L* l, AstStmtBlock* block);
|
|
static void l_lower_stmt(L* l, AstStmt* stmt);
|
|
static IrInst* l_lower_expr(L* l, AstExpr* expr);
|
|
|
|
void astlowerer_lower_top_stmt(AstLowerer* l, AstStmt* stmt)
|
|
{
|
|
switch (stmt->ty) {
|
|
case AstStmt_Decl:
|
|
if (stmt->decl->function_body) {
|
|
l_lower_fn(l, stmt);
|
|
break;
|
|
}
|
|
for (size_t i = 0; i < stmt->decl->declars_size; ++i)
|
|
l_lower_top_declar(l, stmt, stmt->decl->declars[i]);
|
|
break;
|
|
case AstStmt_Expr:
|
|
assert(false && "expressions not allowed here");
|
|
}
|
|
}
|
|
|
|
static IrInst* l_push_inst(AstLowerer* l, IrInst inst)
|
|
{
|
|
IrInst* allocated = arena_alloc(l->a, sizeof(*allocated));
|
|
*allocated = inst;
|
|
ARENA_ARRAY_FIELD_PUSH(l->a, l->curr_block, insts, &allocated);
|
|
return allocated;
|
|
}
|
|
|
|
static void l_lower_top_declar(AstLowerer* l, AstStmt* stmt, AstDeclar* declar)
|
|
{
|
|
(void)stmt;
|
|
if (declar->ty == AstDeclar_Function) {
|
|
assert(declar->function.declar->ty == AstDeclar_Ident);
|
|
|
|
IrFn* fn = arena_alloc(l->a, sizeof(*fn));
|
|
*fn = (IrFn) {
|
|
.ident = arena_strdup(l->a, declar->function.declar->ident),
|
|
ARRAY_FIELD_INIT(blocks),
|
|
.defined = false,
|
|
};
|
|
ARENA_ARRAY_FIELD_PUSH(l->a, &l->cx, fns, &fn);
|
|
|
|
Sym* sym = sema_get_ord_sym(l->sema, declar->function.declar->ident);
|
|
assert(sym);
|
|
|
|
IrVal* val = arena_alloc(l->a, sizeof(*val));
|
|
*val = (IrVal) { .ty = IrVal_Fn, .fn = fn };
|
|
keymap_set(&l->sym_map, (KeymapKey)sym, &val);
|
|
|
|
return;
|
|
}
|
|
|
|
assert(false);
|
|
}
|
|
|
|
static void l_lower_fn(L* l, AstStmt* stmt)
|
|
{
|
|
Sym* sym = sema_get_ord_sym(l->sema, stmt->decl->declars[0]->ident);
|
|
assert(sym);
|
|
|
|
IrFn* fn;
|
|
IrVal* val;
|
|
IrVal** found = keymap_get(&l->sym_map, (KeymapKey)sym);
|
|
|
|
if (found) {
|
|
val = *found;
|
|
assert(val->ty == IrVal_Fn);
|
|
fn = val->fn;
|
|
assert(!fn->defined);
|
|
} else {
|
|
fn = arena_alloc(l->a, sizeof(*fn));
|
|
*fn = (IrFn) {
|
|
.ident = arena_strdup(
|
|
l->a, stmt->decl->declars[0]->function.declar->ident),
|
|
ARRAY_FIELD_INIT(blocks),
|
|
};
|
|
ARENA_ARRAY_FIELD_PUSH(l->a, &l->cx, fns, &fn);
|
|
|
|
val = arena_alloc(l->a, sizeof(*val));
|
|
*val = (IrVal) { .ty = IrVal_Fn, .fn = fn };
|
|
keymap_set(&l->sym_map, (KeymapKey)sym, &val);
|
|
}
|
|
|
|
IrBlock* new_block = arena_alloc(l->a, sizeof(*new_block));
|
|
*new_block = (IrBlock) {
|
|
ARRAY_FIELD_INIT(insts),
|
|
};
|
|
ARENA_ARRAY_FIELD_PUSH(
|
|
l->a, l->cx.fns[l->cx.fns_size - 1], blocks, &new_block);
|
|
l->curr_fn = l->cx.fns[l->cx.fns_size - 1];
|
|
l->curr_block = l->curr_fn->blocks[l->curr_fn->blocks_size - 1];
|
|
|
|
fn->defined = true;
|
|
|
|
l_lower_stmtblock(l, stmt->decl->function_body);
|
|
|
|
if (strcmp(fn->ident, "main") == 0) {
|
|
IrVal* ret_val = arena_alloc(l->a, sizeof(*ret_val));
|
|
*ret_val = (IrVal) {
|
|
.ty = IrVal_I32,
|
|
.i32_val = 0,
|
|
};
|
|
IrInst inst = {
|
|
.ty = IrInst_Ret,
|
|
.val = ret_val,
|
|
};
|
|
l_push_inst(l, inst);
|
|
}
|
|
}
|
|
|
|
static void l_lower_stmtblock(L* l, AstStmtBlock* block)
|
|
{
|
|
for (size_t i = 0; i < block->stmts_size; ++i) {
|
|
l_lower_stmt(l, block->stmts[i]);
|
|
}
|
|
}
|
|
|
|
static void l_lower_stmt(L* l, AstStmt* stmt)
|
|
{
|
|
switch (stmt->ty) {
|
|
case AstStmt_Decl:
|
|
assert(!"not implemented");
|
|
break;
|
|
case AstStmt_Expr:
|
|
l_lower_expr(l, stmt->expr);
|
|
break;
|
|
}
|
|
}
|
|
|
|
static IrInst* l_lower_expr(L* l, AstExpr* expr)
|
|
{
|
|
switch (expr->ty) {
|
|
case AstExpr_Ident: {
|
|
Sym* sym = sema_get_ord_sym(l->sema, expr->ident);
|
|
assert(sym);
|
|
IrVal** val_ptr = keymap_get(&l->sym_map, (KeymapKey)sym);
|
|
assert(val_ptr);
|
|
IrInst inst = {
|
|
.ty = IrInst_Val,
|
|
.val = *val_ptr,
|
|
};
|
|
return l_push_inst(l, inst);
|
|
}
|
|
case AstExpr_Str: {
|
|
|
|
size_t elems_size = strlen(expr->str_val) + 1;
|
|
IrVal** elems = arena_alloc(l->a, sizeof(*elems) * elems_size);
|
|
|
|
for (size_t i = 0; i < elems_size; ++i) {
|
|
IrVal* val = arena_alloc(l->a, sizeof(*val));
|
|
*val = (IrVal) { .ty = IrVal_I8, .i8_val = expr->str_val[i] };
|
|
elems[i] = val;
|
|
}
|
|
|
|
IrVal* array_val = arena_alloc(l->a, sizeof(*array_val));
|
|
*array_val = (IrVal) {
|
|
.ty = IrVal_Array,
|
|
.array = {
|
|
.size_of_elem = sizeof(char),
|
|
.elems_size = elems_size,
|
|
.elems = elems,
|
|
},
|
|
};
|
|
ARENA_ARRAY_FIELD_PUSH(l->a, &l->cx, readonly_vals, &array_val);
|
|
|
|
IrVal* addrof_val = arena_alloc(l->a, sizeof(*array_val));
|
|
*addrof_val = (IrVal) {
|
|
.ty = IrVal_AddrOf,
|
|
.irval = array_val,
|
|
};
|
|
|
|
IrInst inst = {
|
|
.ty = IrInst_Val,
|
|
.val = addrof_val,
|
|
};
|
|
return l_push_inst(l, inst);
|
|
}
|
|
case AstExpr_Call: {
|
|
IrInst* inner = l_lower_expr(l, expr->call.expr);
|
|
size_t args_size = expr->call.args_size;
|
|
IrInst** args = arena_alloc(l->a, sizeof(**args) * args_size);
|
|
for (size_t i = 0; i < args_size; ++i)
|
|
args[i] = l_lower_expr(l, expr->call.args[i]);
|
|
IrInst inst = {
|
|
.ty = IrInst_Call,
|
|
.call = {
|
|
.expr = inner,
|
|
.args = args,
|
|
.args_size = args_size,
|
|
},
|
|
};
|
|
return l_push_inst(l, inst);
|
|
}
|
|
}
|
|
assert(false);
|
|
}
|
|
|
|
void irval_dump(const IrVal* val, FILE* file, Keymap* inst_ids)
|
|
{
|
|
switch (val->ty) {
|
|
case IrVal_I8:
|
|
fprintf(file, "I8 %d", val->i8_val);
|
|
return;
|
|
case IrVal_I32:
|
|
fprintf(file, "I32 %d", val->i32_val);
|
|
return;
|
|
case IrVal_Fn:
|
|
fprintf(file, "Fn %s", val->fn->ident);
|
|
return;
|
|
case IrVal_Array:
|
|
fprintf(file,
|
|
"Array { elems_size: %zu, size_of_elem: %zu, elems: [",
|
|
val->array.elems_size,
|
|
val->array.size_of_elem);
|
|
for (size_t i = 0; i < val->array.elems_size; ++i) {
|
|
if (i != 0)
|
|
fprintf(file, ", ");
|
|
irval_dump(val->array.elems[i], file, inst_ids);
|
|
}
|
|
fprintf(file, "] }");
|
|
return;
|
|
case IrVal_AddrOf:
|
|
fprintf(file, "AddrOf { ");
|
|
irval_dump(val->irval, file, inst_ids);
|
|
fprintf(file, " }");
|
|
return;
|
|
}
|
|
assert(false);
|
|
}
|
|
|
|
void irinst_dump(const IrInst* inst, FILE* file, Keymap* inst_ids)
|
|
{
|
|
int* inst_id = nullptr;
|
|
if (inst_ids)
|
|
inst_id = keymap_get(inst_ids, (KeymapKey)inst);
|
|
switch (inst->ty) {
|
|
case IrInst_Val:
|
|
if (inst_id)
|
|
fprintf(file, "%%%d = ", *inst_id);
|
|
irval_dump(inst->val, file, inst_ids);
|
|
return;
|
|
case IrInst_Call:
|
|
assert(inst_ids);
|
|
fprintf(file, "%%%d = ", *inst_id);
|
|
int* expr_id = keymap_get(inst_ids, (KeymapKey)inst->call.expr);
|
|
fprintf(file, "Call %%%d (", *expr_id);
|
|
for (size_t i = 0; i < inst->call.args_size; ++i) {
|
|
int* arg_id
|
|
= keymap_get(inst_ids, (KeymapKey)inst->call.args[i]);
|
|
fprintf(file, "%s%%%d", i ? ", " : "", *arg_id);
|
|
}
|
|
fprintf(file, ")");
|
|
return;
|
|
case IrInst_Ret:
|
|
fprintf(file, "Ret ");
|
|
irval_dump(inst->val, file, inst_ids);
|
|
return;
|
|
}
|
|
assert(false);
|
|
}
|
|
|
|
void int_dump_fn(const void* val, FILE* file)
|
|
{
|
|
fprintf(file, "%d", *(int*)val);
|
|
}
|
|
|
|
void irfn_dump(const IrFn* fn, FILE* file)
|
|
{
|
|
Keymap inst_ids;
|
|
keymap_init(&inst_ids, sizeof(int));
|
|
inst_ids.dump_entry = int_dump_fn;
|
|
|
|
int inst_id_counter = 0;
|
|
for (size_t block_i = 0; block_i < fn->blocks_size; ++block_i) {
|
|
IrBlock* block = fn->blocks[block_i];
|
|
|
|
for (size_t inst_i = 0; inst_i < block->insts_size; ++inst_i) {
|
|
IrInst* inst = block->insts[inst_i];
|
|
|
|
keymap_set(&inst_ids, (KeymapKey)inst, &inst_id_counter);
|
|
inst_id_counter += 1;
|
|
}
|
|
}
|
|
|
|
fprintf(file, "fn %s() -> _", fn->ident);
|
|
|
|
if (!fn->defined) {
|
|
fprintf(file, ";");
|
|
goto leave;
|
|
}
|
|
|
|
fprintf(file, " {\n");
|
|
for (size_t block_i = 0; block_i < fn->blocks_size; ++block_i) {
|
|
IrBlock* block = fn->blocks[block_i];
|
|
|
|
fprintf(file, "bb%zu:\n", block_i);
|
|
for (size_t inst_i = 0; inst_i < block->insts_size; ++inst_i) {
|
|
IrInst* inst = block->insts[inst_i];
|
|
|
|
fprintf(file, " ");
|
|
irinst_dump(inst, file, &inst_ids);
|
|
fprintf(file, "\n");
|
|
}
|
|
}
|
|
fprintf(file, "}");
|
|
|
|
leave:
|
|
keymap_deinit(&inst_ids);
|
|
}
|