#include "isel.h" #include "collection.h" #include "ir.h" #include #include #include #include [[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 }, }); } push_inst(l, (X86Inst) { .ty = X86InstTy_CallR8, .op1 = { .ty = X86Operand_VReg, .reg = *expr_reg, }, }); 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, ®); 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; } }