#ifndef ISEL_H #define ISEL_H #include "collection.h" #include "ir.h" #include #include #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