commit 6ebbddc790290d30c228c6ae10490e27d7d2de71
parent c5b775048746f647cabb399e8ff82661b8ec35d5
Author: William Djupström <william@deepztream.com>
Date: Sun, 27 Sep 2026 16:01:47 +0200
Add first draft of `load` support
Diffstat:
6 files changed, 146 insertions(+), 6 deletions(-)
diff --git a/src/backend.h b/src/backend.h
@@ -42,6 +42,7 @@ typedef enum Ir_arg_kind {
ARG_CSTRING_LITERAL,
ARG_TEMP_STORAGE,
ARG_ADDRESS,
+ ARG_STATIC_VARIABLE,
ARG_REGISTER,
} Ir_arg_kind;
@@ -75,6 +76,7 @@ typedef struct {
void *addr;
/* char *cstr; */
Sv sv;
+ Sv name;
};
} Ir_arg;
@@ -117,6 +119,7 @@ typedef struct {
Ir_arg right;
} binop;
struct {
+ Ir_type type;
Ir_arg loc;
} load;
struct {
diff --git a/src/ir_backend.c b/src/ir_backend.c
@@ -23,6 +23,11 @@ ir_arg_to_cstr(Ir_arg arg)
str[0] = '"';
str[arg.sv.count + 1] = '"';
str[arg.sv.count + 2] = '\0';
+ } else if (arg.kind == ARG_STATIC_VARIABLE) {
+ str = ta_alloc(&temp_allocator, arg.name.count + 2);
+ memcpy(str + 1, arg.name.items, arg.name.count);
+ str[0] = '?';
+ str[arg.sv.count + 1] = '\0';
} else {
fprintf(stderr, "Invalid arg %i\n", arg.kind);
abort();
@@ -84,7 +89,7 @@ ir_compile_block(Ir_block block, Sb *sb)
case IR_LOAD:
assert(ir.result.kind != ARG_NONE);
assert(ir.result.type != TYPE_VOID);
- sb_printf(sb, " %s =%s load(%s)", ir_arg_to_cstr(ir.result), ir_type_to_cstr(ir.result.type), ir_arg_to_cstr(ir.load.loc));
+ sb_printf(sb, " %s =%s load %s %s", ir_arg_to_cstr(ir.result), ir_type_to_cstr(ir.result.type), ir_type_to_cstr(ir.load.type), ir_arg_to_cstr(ir.load.loc));
break;
case IR_STORE:
assert(ir.result.kind == ARG_NONE);
diff --git a/src/lexer.c b/src/lexer.c
@@ -12,6 +12,8 @@ Sv keywords[] = {
[T_I64 - T_KEYWORDS_] = sv_literal("i64"),
[T_PTR - T_KEYWORDS_] = sv_literal("ptr"),
[T_VOID - T_KEYWORDS_] = sv_literal("void"),
+ [T_LOAD - T_KEYWORDS_] = sv_literal("load"),
+ [T_STORE - T_KEYWORDS_] = sv_literal("store"),
[T_CALL - T_KEYWORDS_] = sv_literal("call"),
[T_SYSCALL - T_KEYWORDS_] = sv_literal("syscall"),
[T_JUMP - T_KEYWORDS_] = sv_literal("jump"),
@@ -19,7 +21,7 @@ Sv keywords[] = {
[T_PHI - T_KEYWORDS_] = sv_literal("phi"),
};
-static_assert(T_COUNT_ - T_KEYWORDS_ == 13, "keywords[] wrong length");
+static_assert(T_COUNT_ - T_KEYWORDS_ == 15, "keywords[] wrong length");
int keywords_count = (sizeof(keywords) / sizeof(keywords[0]));
void
@@ -51,6 +53,8 @@ sb_append_token_kind(Sb *sb, Token_kind kind)
else if (kind == T_I64) sb_append_cstr(sb, "i64");
else if (kind == T_PTR) sb_append_cstr(sb, "ptr");
else if (kind == T_VOID) sb_append_cstr(sb, "void");
+ else if (kind == T_LOAD) sb_append_cstr(sb, "load");
+ else if (kind == T_STORE) sb_append_cstr(sb, "store");
else if (kind == T_CALL) sb_append_cstr(sb, "call");
else if (kind == T_SYSCALL) sb_append_cstr(sb, "syscall");
else if (kind == T_JUMP) sb_append_cstr(sb, "jump");
diff --git a/src/lexer.h b/src/lexer.h
@@ -35,6 +35,8 @@ typedef enum {
T_I64,
T_PTR,
T_VOID,
+ T_LOAD,
+ T_STORE,
T_CALL,
T_SYSCALL,
T_JUMP,
diff --git a/src/parse.c b/src/parse.c
@@ -116,6 +116,12 @@ parse_ir_value(Ir_arg *out_arg, Lexer *lexer)
.type = TYPE_PTR,
.sv = t.sv,
};
+ } else if (t.kind == T_VARIABLE_SYMBOL) {
+ *out_arg = (Ir_arg) {
+ .kind = ARG_STATIC_VARIABLE,
+ .type = TYPE_PTR,
+ .name = t.sv,
+ };
} else {
print_loc(t.loc);
fprintf(stderr, "Error: expected value\n");
@@ -251,7 +257,29 @@ parse_ir_assignment(Ir *out_ins, Lexer *lexer)
skip_whitespace(lexer);
Token t = peek_token(lexer);
- if (t.kind == T_CALL) {
+ if (t.kind == T_LOAD) {
+ next_token(lexer);
+ skip_whitespace(lexer);
+ Ir_type load_type = {0};
+ Ir_arg load_loc = {0};
+ if (!parse_ir_type(&load_type, lexer)) {
+ return false;
+ }
+ skip_whitespace(lexer);
+ if (!parse_ir_value(&load_loc, lexer)) {
+ return false;
+ }
+ *out_ins = (Ir) {
+ .kind = IR_LOAD,
+ .result = result,
+ .load = {
+ .type = load_type,
+ .loc = load_loc,
+ },
+ };
+ } else if (t.kind == T_STORE) {
+ todo("parse store");
+ } else if (t.kind == T_CALL) {
next_token(lexer);
skip_whitespace(lexer);
Ir_call_args call_args = {0};
diff --git a/src/x86_64-codegen.c b/src/x86_64-codegen.c
@@ -52,6 +52,7 @@ typedef enum {
x86_64_IMM_LITERAL,
x86_64_IMM_CODE_LABEL,
x86_64_IMM_DATA_LABEL,
+ x86_64_IMM_NAMED_DATA,
} x86_64_Immediate_kind;
typedef struct {
@@ -59,6 +60,7 @@ typedef struct {
union {
long int i64;
size_t label_id;
+ Sv name;
};
} x86_64_Immediate;
@@ -211,7 +213,12 @@ x86_64_operand_to_cstr(x86_64_Operand rand)
case x86_64_RAND_MEMORY:
switch (rand.mem.encoding) {
case x86_64_MEM_D:
- todo_p("MEM_D");
+ assert(rand.mem.offset.kind == x86_64_IMM_NAMED_DATA);
+ free(leak_buf);
+ len = rand.mem.offset.name.count + 5;
+ leak_buf = malloc(len);
+ len = snprintf(leak_buf, len, "[@%.*s]", (int) rand.mem.offset.name.count, rand.mem.offset.name.items);
+ break;
case x86_64_MEM_B:
todo_p("MEM_B");
case x86_64_MEM_BS:
@@ -423,6 +430,7 @@ x86_64_emit_program_header(x86_64_Program prog, Sb *sb)
{
(void) prog;
sb_printf(sb, "BITS 64\n");
+ sb_printf(sb, "DEFAULT REL\n");
sb_printf(sb, "SECTION .text\n");
}
@@ -584,7 +592,47 @@ x86_64_compile_instruction(Ir ir, x86_64_Instruction_list *list, x86_64_Data_lis
{
switch (ir.kind) {
case IR_LOAD:
- todo("load");
+ switch (ir.load.loc.kind) {
+ case ARG_INT_LITERAL:
+ todo("load-int");
+ case ARG_TEMP_STORAGE:
+ todo("load-temp");
+ case ARG_ADDRESS:
+ todo("load-address");
+ case ARG_STATIC_VARIABLE:
+ da_append(list, ((x86_64_Instruction) {
+ .opcode = x86_64_MOV,
+ .encoding = x86_64_RM,
+ .rand1 = {
+ .bit_width = 32,
+ .kind = x86_64_RAND_REGISTER,
+ .reg = {
+ .physical = -1,
+ .temp_id = ir.result.temp_id,
+ },
+ },
+ .rand2 = {
+ .bit_width = 32,
+ .kind = x86_64_RAND_MEMORY,
+ .mem = {
+ .encoding = x86_64_MEM_D,
+ .offset = {
+ .kind = x86_64_IMM_NAMED_DATA,
+ .name = ir.load.loc.name,
+ },
+ },
+ },
+ }));
+ break;
+ case ARG_CSTRING_LITERAL:
+ unreachable("load-cstring");
+ case ARG_NONE:
+ unreachable("load-none");
+ case ARG_REGISTER:
+ unreachable("assign-register");
+ default:
+ unreachable("assign");
+ }
break;
case IR_STORE:
todo("store");
@@ -662,6 +710,8 @@ x86_64_compile_instruction(Ir ir, x86_64_Instruction_list *list, x86_64_Data_lis
break;
case ARG_ADDRESS:
todo("assign-address");
+ case ARG_STATIC_VARIABLE:
+ todo("assign-static-variable");
case ARG_REGISTER:
unreachable("assign-register");
default:
@@ -854,6 +904,8 @@ x86_64_compile_instruction(Ir ir, x86_64_Instruction_list *list, x86_64_Data_lis
.temp_id = ir.call.args.items[i].temp_id,
};
break;
+ case ARG_STATIC_VARIABLE:
+ todo("static-variable operand");
case ARG_ADDRESS:
case ARG_REGISTER:
case ARG_NONE:
@@ -891,6 +943,8 @@ x86_64_compile_instruction(Ir ir, x86_64_Instruction_list *list, x86_64_Data_lis
},
};
break;
+ case ARG_STATIC_VARIABLE:
+ todo("syscall->static-variable");
case ARG_CSTRING_LITERAL:
unreachable("syscall->cstring");
case ARG_ADDRESS:
@@ -941,6 +995,8 @@ x86_64_compile_instruction(Ir ir, x86_64_Instruction_list *list, x86_64_Data_lis
.temp_id = ir.syscall.args.items[i].temp_id,
};
break;
+ case ARG_STATIC_VARIABLE:
+ todo("static-variable operand");
case ARG_ADDRESS:
case ARG_REGISTER:
case ARG_NONE:
@@ -1263,7 +1319,7 @@ x86_64_calculate_internal_liveness(x86_64_Basic_block *block)
block->live.items[entry_index].start = i;
}
}
- break;
+ break;
case x86_64_M:
todo("modifying M");
@@ -1305,6 +1361,48 @@ x86_64_calculate_internal_liveness(x86_64_Basic_block *block)
}
break;
case x86_64_RM:
+ if (ins.rand1.reg.physical != -1) {
+ /* fprintf(stderr, "INFO: Ignoring physical register in liveness check\n"); */
+ } else {
+ if ((entry_index = x86_64_get_liveness_entry(&block->live, ins.rand1.reg.temp_id)) == SIZE_T_INVALID) {
+ da_append(&block->live, ((x86_64_Liveness_entry) {
+ .start = i,
+ .end = i,
+ .temp_id = ins.rand1.reg.temp_id,
+ }));
+ } else {
+ assert(block->live.items[entry_index].start == -1);
+ block->live.items[entry_index].start = i;
+ }
+ }
+ switch (ins.rand2.mem.encoding) {
+ case x86_64_MEM_D:
+ break;
+ /*
+ switch (ins.rand2.mem.offset.kind) {
+ case x86_64_IMM_CODE_LABEL:
+ case x86_64_IMM_LITERAL:
+ case x86_64_IMM_DATA_LABEL:
+ case x86_64_IMM_NAMED_DATA:
+ default:
+ unreachabel("mem->imm kind");
+ }
+ break;
+ */
+ case x86_64_MEM_B:
+ todo("mem B");
+ case x86_64_MEM_BD:
+ todo("mem BD");
+ case x86_64_MEM_BS:
+ todo("mem BS");
+ case x86_64_MEM_SD:
+ todo("mem SD");
+ case x86_64_MEM_BSD:
+ todo("mem BSD");
+ default:
+ unreachable("mem encoding");
+ }
+ break;
todo("modifying RM");
case x86_64_MR:
todo("modifying MR");