generate x86

This commit is contained in:
sfja 2026-11-16 22:25:02 +01:00
parent 968bdec2eb
commit bd00be4ace
5 changed files with 751 additions and 13 deletions

38
ir.c
View File

@ -140,6 +140,19 @@ static void l_lower_fn(L* l, AstStmt* stmt)
fn->defined = true; fn->defined = true;
l_lower_stmtblock(l, stmt->decl->function_body); 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) static void l_lower_stmtblock(L* l, AstStmtBlock* block)
@ -186,8 +199,8 @@ static IrInst* l_lower_expr(L* l, AstExpr* expr)
elems[i] = val; elems[i] = val;
} }
IrVal* val = arena_alloc(l->a, sizeof(*val)); IrVal* array_val = arena_alloc(l->a, sizeof(*array_val));
*val = (IrVal) { *array_val = (IrVal) {
.ty = IrVal_Array, .ty = IrVal_Array,
.array = { .array = {
.size_of_elem = sizeof(char), .size_of_elem = sizeof(char),
@ -195,12 +208,17 @@ static IrInst* l_lower_expr(L* l, AstExpr* expr)
.elems = elems, .elems = elems,
}, },
}; };
ARENA_ARRAY_FIELD_PUSH(l->a, &l->cx, readonly_vals, &array_val);
ARENA_ARRAY_FIELD_PUSH(l->a, &l->cx, readonly_vals, val); IrVal* addrof_val = arena_alloc(l->a, sizeof(*array_val));
*addrof_val = (IrVal) {
.ty = IrVal_AddrOf,
.irval = array_val,
};
IrInst inst = { IrInst inst = {
.ty = IrInst_Val, .ty = IrInst_Val,
.val = val, .val = addrof_val,
}; };
return l_push_inst(l, inst); return l_push_inst(l, inst);
} }
@ -230,6 +248,9 @@ void irval_dump(const IrVal* val, FILE* file, Keymap* inst_ids)
case IrVal_I8: case IrVal_I8:
fprintf(file, "I8 %d", val->i8_val); fprintf(file, "I8 %d", val->i8_val);
return; return;
case IrVal_I32:
fprintf(file, "I32 %d", val->i32_val);
return;
case IrVal_Fn: case IrVal_Fn:
fprintf(file, "Fn %s", val->fn->ident); fprintf(file, "Fn %s", val->fn->ident);
return; return;
@ -245,6 +266,11 @@ void irval_dump(const IrVal* val, FILE* file, Keymap* inst_ids)
} }
fprintf(file, "] }"); fprintf(file, "] }");
return; return;
case IrVal_AddrOf:
fprintf(file, "AddrOf { ");
irval_dump(val->irval, file, inst_ids);
fprintf(file, " }");
return;
} }
assert(false); assert(false);
} }
@ -272,6 +298,10 @@ void irinst_dump(const IrInst* inst, FILE* file, Keymap* inst_ids)
} }
fprintf(file, ")"); fprintf(file, ")");
return; return;
case IrInst_Ret:
fprintf(file, "Ret ");
irval_dump(inst->val, file, inst_ids);
return;
} }
assert(false); assert(false);
} }

5
ir.h
View File

@ -10,13 +10,16 @@
typedef enum { typedef enum {
IrVal_I8, IrVal_I8,
IrVal_I32,
IrVal_Fn, IrVal_Fn,
IrVal_Array, IrVal_Array,
IrVal_AddrOf,
} IrValTy; } IrValTy;
typedef enum { typedef enum {
IrInst_Val, IrInst_Val,
IrInst_Call, IrInst_Call,
IrInst_Ret,
} IrInstTy; } IrInstTy;
typedef struct IrVal IrVal; typedef struct IrVal IrVal;
@ -27,7 +30,9 @@ struct IrVal {
IrValTy ty; IrValTy ty;
union { union {
int8_t i8_val; int8_t i8_val;
int32_t i32_val;
IrFn* fn; IrFn* fn;
IrVal* irval;
struct { struct {
IrVal** elems; IrVal** elems;
size_t elems_size; size_t elems_size;

534
isel.c Normal file
View File

@ -0,0 +1,534 @@
#include "isel.h"
#include "collection.h"
#include "ir.h"
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
[[maybe_unused]]
static bool is_scratch(X86Reg reg)
{
switch (reg) {
case X86Reg_AX:
case X86Reg_DI:
case X86Reg_SI:
case X86Reg_DX:
case X86Reg_CX:
case X86Reg_R8:
case X86Reg_R9:
case X86Reg_R10:
case X86Reg_R11:
return true;
case X86Reg_BX:
case X86Reg_SP:
case X86Reg_BP:
case X86Reg_R12:
case X86Reg_R13:
case X86Reg_R14:
case X86Reg_R15:
return false;
}
assert(false);
}
void x86cx_init(X86Cx* cx)
{
*cx = (X86Cx) {
.arena = { },
ARRAY_FIELD_INIT(fns),
ARRAY_FIELD_INIT(external_symbols),
};
arena_init(&cx->arena, 8192);
}
void x86cx_deinit(X86Cx* cx)
{
arena_deinit(&cx->arena);
}
void x86irlowerer_init(X86IrLowerer* l, X86Cx* cx)
{
*l = (X86IrLowerer) {
.cx = cx,
.a = &cx->arena,
.curr_fn = nullptr,
.curr_block = nullptr,
.vregs = 1000,
.inst_reg_map = { },
.readonly_val_map = { },
};
keymap_init(&l->inst_reg_map, sizeof(int64_t));
keymap_init(&l->readonly_val_map, sizeof(size_t));
}
void x86irlowerer_deinit(X86IrLowerer* l)
{
keymap_deinit(&l->inst_reg_map);
keymap_deinit(&l->readonly_val_map);
}
typedef X86IrLowerer L;
static void lower_fn(L* l, IrFn* fn);
void x86irlowerer_lower_ir(L* l, IrCx* ir_cx)
{
for (size_t i = 0; i < ir_cx->readonly_vals_size; ++i) {
IrVal* val = ir_cx->readonly_vals[i];
ARENA_ARRAY_FIELD_PUSH(l->a, l->cx, readonly_vals, &val);
keymap_set(&l->readonly_val_map, (KeymapKey)val, &i);
}
for (size_t i = 0; i < ir_cx->fns_size; ++i)
lower_fn(l, ir_cx->fns[i]);
}
static void push_inst(L* l, X86Inst inst)
{
X86Inst* p = arena_alloc(l->a, sizeof(*p));
*p = inst;
ARENA_ARRAY_FIELD_PUSH(l->a, l->curr_block, insts, &p);
}
static void lower_block(L* l, IrBlock* ir_block);
static void lower_inst(L* l, IrInst* inst);
static void lower_fn(L* l, IrFn* ir_fn)
{
if (!ir_fn->defined) {
char* ident = arena_strdup(l->a, ir_fn->ident);
ARENA_ARRAY_FIELD_PUSH(l->a, l->cx, external_symbols, &ident);
return;
}
X86Fn* fn = arena_alloc(l->a, sizeof(*fn));
*fn = (X86Fn) {
.ir_fn = ir_fn,
ARRAY_FIELD_INIT(blocks),
};
ARENA_ARRAY_FIELD_PUSH(l->a, l->cx, fns, &fn);
l->curr_fn = fn;
for (size_t i = 0; i < ir_fn->blocks_size; ++i) {
X86Block* block = arena_alloc(l->a, sizeof(*block));
*block = (X86Block) {
ARRAY_FIELD_INIT(insts),
};
ARENA_ARRAY_FIELD_PUSH(l->a, l->curr_fn, blocks, &block);
}
l->curr_block = l->curr_fn->blocks[0];
l->vregs = 1000;
push_inst(l,
(X86Inst) {
.ty = X86InstTy_PushR8,
.op1 = { .ty = X86Operand_Reg, .reg = X86Reg_BP },
});
push_inst(l,
(X86Inst) {
.ty = X86InstTy_MovRR8,
.op1 = { .ty = X86Operand_Reg, .reg = X86Reg_BP },
.op2 = { .ty = X86Operand_Reg, .reg = X86Reg_SP },
});
for (size_t i = 0; i < ir_fn->blocks_size; ++i) {
l->curr_block = l->curr_fn->blocks[i];
lower_block(l, ir_fn->blocks[i]);
}
push_inst(l,
(X86Inst) {
.ty = X86InstTy_MovRR8,
.op1 = { .ty = X86Operand_Reg, .reg = X86Reg_SP },
.op2 = { .ty = X86Operand_Reg, .reg = X86Reg_BP },
});
push_inst(l,
(X86Inst) {
.ty = X86InstTy_PopR8,
.op1 = { .ty = X86Operand_Reg, .reg = X86Reg_BP },
});
push_inst(l,
(X86Inst) {
.ty = X86InstTy_Ret,
});
}
static void lower_block(L* l, IrBlock* ir_block)
{
for (size_t i = 0; i < ir_block->insts_size; ++i) {
lower_inst(l, ir_block->insts[i]);
}
}
static void lower_inst(L* l, IrInst* inst)
{
switch (inst->ty) {
case IrInst_Val: {
switch (inst->val->ty) {
case IrVal_I8:
case IrVal_I32:
assert(!"not implemented");
case IrVal_Fn: {
int64_t vreg = l->vregs++;
push_inst(l,
(X86Inst) {
.ty = X86InstTy_Lea8,
.op1 = { .ty = X86Operand_VReg, .reg = vreg },
.op2 = {
.ty = X86Operand_IdentRelWrtPlt,
.ident = arena_strdup(l->a, inst->val->fn->ident),
},
});
keymap_set(&l->inst_reg_map, (KeymapKey)inst, &vreg);
break;
}
case IrVal_AddrOf:
int64_t vreg = l->vregs++;
size_t* ro_val_idx = keymap_get(
&l->readonly_val_map, (KeymapKey)inst->val->irval);
assert(ro_val_idx);
push_inst(l,
(X86Inst) {
.ty = X86InstTy_MovRI8,
.op1 = { .ty = X86Operand_VReg, .reg = vreg },
.op2 = {
.ty = X86Operand_IrVal,
.ro_val_idx = *ro_val_idx,
},
});
keymap_set(&l->inst_reg_map, (KeymapKey)inst, &vreg);
break;
case IrVal_Array:
assert(false);
}
break;
}
case IrInst_Call: {
X86Reg pass_regs[] = {
X86Reg_DI,
X86Reg_SI,
X86Reg_DX,
X86Reg_CX,
X86Reg_R8,
X86Reg_R9,
};
size_t pass_regs_size = sizeof(pass_regs) / sizeof(pass_regs[0]);
int64_t vreg = l->vregs++;
int64_t* expr_reg
= keymap_get(&l->inst_reg_map, (KeymapKey)inst->call.expr);
assert(expr_reg);
assert(inst->call.args_size <= pass_regs_size);
for (size_t i = 0; i < inst->call.args_size; ++i) {
int64_t* arg_reg = keymap_get(
&l->inst_reg_map, (KeymapKey)inst->call.args[i]);
assert(arg_reg);
push_inst(l,
(X86Inst) {
.ty = X86InstTy_MovRR8,
.op1 = { .ty = X86Operand_Reg, .reg = pass_regs[i] },
.op2 = { .ty = X86Operand_VReg, .reg = *arg_reg },
});
}
assert(inst->call.expr->ty == IrInst_Val
&& inst->call.expr->val->ty == IrVal_Fn);
push_inst(l,
(X86Inst) {
.ty = X86InstTy_CallWrtPlt,
.op1 = { .ty = X86Operand_IdentRelWrtPlt,
.ident = inst->call.expr->val->fn->ident },
});
push_inst(l,
(X86Inst) {
.ty = X86InstTy_MovRR4,
.op1 = { .ty = X86Operand_VReg, .reg = vreg },
.op2 = { .ty = X86Operand_Reg, .reg = X86Reg_AX },
});
break;
};
case IrInst_Ret:
assert(inst->val->ty == IrVal_I32);
push_inst(l,
(X86Inst) {
.ty = X86InstTy_MovRR4,
.op1 = { .ty = X86Operand_Reg, .reg = X86Reg_AX },
.op2
= { .ty = X86Operand_Imm4, .imm4 = inst->val->i32_val },
});
}
}
void x86regallocator_init(X86RegAllocator* r, X86Cx* cx)
{
*r = (X86RegAllocator) {
.cx = cx,
.a = &cx->arena,
};
}
void x86regallocator_deinit(X86RegAllocator* r)
{
(void)r;
}
static void fn_allocate_registers(X86RegAllocator* r, X86Fn* fn);
static void block_allocate_registers(X86RegAllocator* r, X86Block* block);
void x86regallocator_allocate_registers(X86RegAllocator* r)
{
for (size_t i = 0; i < r->cx->fns_size; ++i) {
X86Fn* fn = r->cx->fns[i];
fn_allocate_registers(r, fn);
}
}
static void fn_allocate_registers(X86RegAllocator* r, X86Fn* fn)
{
for (size_t i = 0; i < fn->blocks_size; ++i) {
X86Block* block = fn->blocks[i];
block_allocate_registers(r, block);
}
}
static void block_allocate_registers(X86RegAllocator* r, X86Block* block)
{
(void)r;
uint64_t regs_in_use = 0;
X86Reg regs_to_use[] = {
X86Reg_AX,
X86Reg_BX,
X86Reg_CX,
X86Reg_DX,
X86Reg_SI,
};
size_t regs_to_use_size = sizeof(regs_to_use) / sizeof(regs_to_use[0]);
Keymap vreg_reg_map;
keymap_init(&vreg_reg_map, sizeof(X86Reg));
for (size_t i = 0; i < block->insts_size; ++i) {
X86Inst* inst = block->insts[i];
if (inst->op1.ty != X86Operand_VReg)
continue;
X86Reg* found_reg = keymap_get(&vreg_reg_map, (KeymapKey)inst->op1.reg);
if (found_reg) {
inst->op1
= (X86Operand) { .ty = X86Operand_Reg, .reg = *found_reg };
continue;
}
X86Reg reg = -1;
for (size_t reg_i = 0; reg_i < regs_to_use_size; ++reg_i) {
if (regs_in_use & 1 << regs_to_use[reg_i])
continue;
reg = regs_to_use[reg_i];
}
assert(reg > -1);
regs_in_use |= 1 << reg;
keymap_set(&vreg_reg_map, (KeymapKey)inst->op1.reg, &reg);
inst->op1 = (X86Operand) { .ty = X86Operand_Reg, .reg = reg };
}
for (size_t i = 0; i < block->insts_size; ++i) {
X86Inst* inst = block->insts[i];
if (inst->op2.ty != X86Operand_VReg)
continue;
X86Reg* reg = keymap_get(&vreg_reg_map, (KeymapKey)inst->op2.reg);
assert(reg);
inst->op2 = (X86Operand) { .ty = X86Operand_Reg, .reg = *reg };
continue;
}
keymap_deinit(&vreg_reg_map);
}
static void fn_dump(X86Cx* cx, FILE* file, X86Fn* fn);
static void inst_dump(X86Cx* cx, FILE* file, X86Inst* inst);
static void operand_dump(X86Cx* cx, FILE* file, X86Operand* op, int size);
void x86cx_dump(X86Cx* cx, FILE* file)
{
fprintf(file, "bits 64\n");
for (size_t i = 0; i < cx->external_symbols_size; ++i) {
fprintf(file, "extern %s\n", cx->external_symbols[i]);
}
fprintf(file, "section .rodata\n");
for (size_t i = 0; i < cx->readonly_vals_size; ++i) {
IrVal* val = cx->readonly_vals[i];
fprintf(file, "data%zu:\n", i);
if (val->ty == IrVal_Array && val->array.size_of_elem == 1) {
fprintf(file, " db ");
for (size_t i = 0; i < val->array.elems_size; ++i) {
fprintf(
file, "%s%d", i ? ", " : "", val->array.elems[i]->i8_val);
}
} else {
irval_dump(val, file, nullptr);
}
fprintf(file, "\n");
}
fprintf(file, "section .text\n");
for (size_t i = 0; i < cx->fns_size; ++i) {
X86Fn* fn = cx->fns[i];
fn_dump(cx, file, fn);
}
}
static void fn_dump(X86Cx* cx, FILE* file, X86Fn* fn)
{
fprintf(file, "global %s\n", fn->ir_fn->ident);
fprintf(file, "%s:\n", fn->ir_fn->ident);
for (size_t block_i = 0; block_i < fn->blocks_size; ++block_i) {
X86Block* block = fn->blocks[block_i];
fprintf(file, ".l%zu:\n", block_i);
for (size_t inst_i = 0; inst_i < block->insts_size; ++inst_i) {
fprintf(file, " ");
inst_dump(cx, file, block->insts[inst_i]);
fprintf(file, "\n");
}
}
}
static void inst_dump(X86Cx* cx, FILE* file, X86Inst* inst)
{
switch (inst->ty) {
case X86InstTy_Ret:
fprintf(file, "ret ");
break;
case X86InstTy_PushR8:
fprintf(file, "push ");
operand_dump(cx, file, &inst->op1, 8);
break;
case X86InstTy_PopR8:
fprintf(file, "pop ");
operand_dump(cx, file, &inst->op1, 8);
break;
case X86InstTy_MovRR8:
fprintf(file, "mov ");
operand_dump(cx, file, &inst->op1, 8);
fprintf(file, ", ");
operand_dump(cx, file, &inst->op2, 8);
break;
case X86InstTy_MovRR4:
fprintf(file, "mov ");
operand_dump(cx, file, &inst->op1, 4);
fprintf(file, ", ");
operand_dump(cx, file, &inst->op2, 4);
break;
case X86InstTy_MovRI8:
fprintf(file, "mov ");
operand_dump(cx, file, &inst->op1, 8);
fprintf(file, ", ");
operand_dump(cx, file, &inst->op2, 8);
break;
case X86InstTy_SubRI8:
fprintf(file, "sub ");
operand_dump(cx, file, &inst->op1, 8);
fprintf(file, ", ");
operand_dump(cx, file, &inst->op2, 8);
break;
case X86InstTy_CallR8:
fprintf(file, "call ");
operand_dump(cx, file, &inst->op1, 8);
break;
case X86InstTy_CallWrtPlt:
fprintf(file, "call ");
operand_dump(cx, file, &inst->op1, 8);
break;
case X86InstTy_Lea8:
fprintf(file, "lea ");
operand_dump(cx, file, &inst->op1, 8);
fprintf(file, ", [rel ");
operand_dump(cx, file, &inst->op2, 8);
fprintf(file, "]");
break;
}
}
static char* reg8_names[] = {
#define X(CNAME, LNAME, NAME8, NAME4, NAME2, NAME1) [X86Reg_##CNAME] = #NAME8,
X86REG_LIST
#undef X
};
static char* reg4_names[] = {
#define X(CNAME, LNAME, NAME8, NAME4, NAME2, NAME1) [X86Reg_##CNAME] = #NAME4,
X86REG_LIST
#undef X
};
static char* reg2_names[] = {
#define X(CNAME, LNAME, NAME8, NAME4, NAME2, NAME1) [X86Reg_##CNAME] = #NAME2,
X86REG_LIST
#undef X
};
static char* reg1_names[] = {
#define X(CNAME, LNAME, NAME8, NAME4, NAME2, NAME1) [X86Reg_##CNAME] = #NAME1,
X86REG_LIST
#undef X
};
static void operand_dump(X86Cx* cx, FILE* file, X86Operand* op, int size)
{
(void)cx;
switch (op->ty) {
case X86Operand_Reg: {
char** reg_names = nullptr;
switch (size) {
case 8:
reg_names = reg8_names;
break;
case 4:
reg_names = reg4_names;
break;
case 2:
reg_names = reg2_names;
break;
case 1:
reg_names = reg1_names;
break;
default:
assert(false);
}
assert(reg_names);
assert(op->reg >= X86Reg_AX && op->reg <= X86Reg_R15);
fprintf(file, "%s", reg_names[op->reg]);
break;
}
case X86Operand_VReg:
fprintf(file, "%%%lu", op->reg - 1000);
break;
case X86Operand_Ident:
fprintf(file, "%s", op->ident);
break;
case X86Operand_IrVal:
fprintf(file, "data%zu", op->ro_val_idx);
break;
case X86Operand_Imm4:
fprintf(file, "%d", op->imm4);
break;
case X86Operand_IdentRelWrtPlt:
fprintf(file, "%s wrt ..plt", op->ident);
break;
}
}

117
isel.h Normal file
View File

@ -0,0 +1,117 @@
#ifndef ISEL_H
#define ISEL_H
#include "collection.h"
#include "ir.h"
#include <stdint.h>
#include <stdio.h>
#define X86REG_LIST \
X(AX, ax, rax, eax, ax, al) \
X(BX, bx, rbx, ebx, bx, bl) \
X(CX, cx, rcx, ecx, cx, cl) \
X(DX, dx, rdx, edx, dx, dl) \
X(SI, si, rsi, esi, si, sil) \
X(DI, di, rdi, edi, di, dil) \
X(SP, sp, rsp, esp, sp, spl) \
X(BP, bp, rbp, ebp, bp, bpl) \
X(R8, r8, r8, r8d, r8w, r8b) \
X(R9, r9, r9, r9d, r9w, r9b) \
X(R10, r10, r10, r10d, r10w, r10b) \
X(R11, r11, r11, r11d, r11w, r11b) \
X(R12, r12, r12, r12d, r12w, r12b) \
X(R13, r13, r13, r13d, r13w, r13b) \
X(R14, r14, r14, r14d, r14w, r14b) \
X(R15, r15, r15, r15d, r15w, r15b)
typedef enum : int64_t {
#define X(CNAME, LNAME, NAME8, NAME4, NAME2, NAME1) X86Reg_##CNAME,
X86REG_LIST
#undef X
} X86Reg;
typedef enum {
X86InstTy_Ret,
X86InstTy_PushR8,
X86InstTy_PopR8,
X86InstTy_MovRR8,
X86InstTy_MovRR4,
X86InstTy_MovRI8,
X86InstTy_SubRI8,
X86InstTy_CallR8,
X86InstTy_CallWrtPlt,
X86InstTy_Lea8,
} X86InstTy;
typedef enum {
X86Operand_Reg,
X86Operand_VReg,
X86Operand_Ident,
X86Operand_IrVal,
X86Operand_Imm4,
X86Operand_IdentRelWrtPlt,
} X86OperandTy;
typedef struct {
X86OperandTy ty;
union {
X86Reg reg;
char* ident;
size_t ro_val_idx;
int32_t imm4;
};
} X86Operand;
typedef struct {
X86InstTy ty;
X86Operand op1;
X86Operand op2;
} X86Inst;
typedef struct {
ARRAY_FIELD(X86Inst*, insts);
} X86Block;
typedef struct {
IrFn* ir_fn;
ARRAY_FIELD(X86Block*, blocks);
} X86Fn;
typedef struct {
Arena arena;
ARRAY_FIELD(X86Fn*, fns);
ARRAY_FIELD(char*, external_symbols);
ARRAY_FIELD(IrVal*, readonly_vals);
} X86Cx;
void x86cx_init(X86Cx* cx);
void x86cx_deinit(X86Cx* cx);
void x86cx_dump(X86Cx* cx, FILE* file);
typedef struct {
X86Cx* cx;
Arena* a;
X86Fn* curr_fn;
X86Block* curr_block;
int64_t vregs;
Keymap inst_reg_map;
Keymap readonly_val_map;
} X86IrLowerer;
void x86irlowerer_init(X86IrLowerer* l, X86Cx* cx);
void x86irlowerer_deinit(X86IrLowerer* l);
void x86irlowerer_lower_ir(X86IrLowerer* l, IrCx* ir_cx);
typedef struct {
X86Cx* cx;
Arena* a;
} X86RegAllocator;
void x86regallocator_init(X86RegAllocator* r, X86Cx* cx);
void x86regallocator_deinit(X86RegAllocator* r);
void x86regallocator_allocate_registers(X86RegAllocator* r);
#endif

70
main.c
View File

@ -1,6 +1,7 @@
#include "ast.h" #include "ast.h"
#include "collection.h" #include "collection.h"
#include "ir.h" #include "ir.h"
#include "isel.h"
#include "parse.h" #include "parse.h"
#include "preproc.h" #include "preproc.h"
#include "sema.h" #include "sema.h"
@ -17,6 +18,16 @@ int main(int argc, char* argv[])
return EXIT_FAILURE; return EXIT_FAILURE;
} }
char* filename = argv[1]; char* filename = argv[1];
char* out_filename = nullptr;
for (int i = 2; i < argc; ++i) {
if (strcmp(argv[i], "-o") == 0) {
i += 1;
assert(i < argc);
out_filename = argv[i];
}
}
char* text = read_textfile(filename); char* text = read_textfile(filename);
if (!text) { if (!text) {
return EXIT_FAILURE; return EXIT_FAILURE;
@ -41,32 +52,73 @@ int main(int argc, char* argv[])
Parser parser; Parser parser;
parser_init(&parser, &tokstream, sema, &ast_arena); parser_init(&parser, &tokstream, sema, &ast_arena);
AstLowerer astlowerer; AstLowerer astlow;
astlowerer_init(&astlowerer, sema); astlowerer_init(&astlow, sema);
AstStmt* stmt; AstStmt* stmt;
while (!parser_done(&parser)) { while (!parser_done(&parser)) {
stmt = parser_parse_stmt(&parser); stmt = parser_parse_stmt(&parser);
ast_stmt_print(stdout, stmt, 0); // ast_stmt_print(stdout, stmt, 0);
printf("\n"); // printf("\n");
astlowerer_lower_top_stmt(&astlowerer, stmt); astlowerer_lower_top_stmt(&astlow, stmt);
} }
IrCx* ir_cx = &astlowerer.cx; IrCx* ircx = &astlow.cx;
for (size_t i = 0; i < ir_cx->fns_size; ++i) { printf("--- ir ---\n");
irfn_dump(ir_cx->fns[i], stdout); for (size_t i = 0; i < ircx->fns_size; ++i) {
// irfn_dump(ircx->fns[i], stdout);
// printf("\n");
}
X86Cx x86cx;
x86cx_init(&x86cx);
X86IrLowerer irlow;
x86irlowerer_init(&irlow, &x86cx);
printf("--- instruction selection ---\n");
x86irlowerer_lower_ir(&irlow, ircx);
// x86cx_dump(&x86cx, stdout);
// printf("\n");
x86irlowerer_deinit(&irlow);
printf("--- register allocation ---\n");
X86RegAllocator reg_allocator;
x86regallocator_init(&reg_allocator, &x86cx);
x86regallocator_allocate_registers(&reg_allocator);
// x86cx_dump(&x86cx, stdout);
// printf("\n");
if (out_filename) {
FILE* outfile = fopen(out_filename, "w");
assert(outfile);
x86cx_dump(&x86cx, outfile);
fprintf(outfile, "\n");
fclose(outfile);
} else {
x86cx_dump(&x86cx, stdout);
printf("\n"); printf("\n");
} }
x86regallocator_deinit(&reg_allocator);
x86cx_deinit(&x86cx);
parser_deinit(&parser); parser_deinit(&parser);
preproc_free(preproc); preproc_free(preproc);
arena_deinit(&tok_arena); arena_deinit(&tok_arena);
arena_deinit(&ast_arena); arena_deinit(&ast_arena);
astlowerer_deinit(&astlowerer); astlowerer_deinit(&astlow);
sema_free(sema); sema_free(sema);
} }