From 21be45a904a0afc767214298721bd2bbce19a054 Mon Sep 17 00:00:00 2001 From: hachem Date: Tue, 8 Sep 2026 11:26:42 +0200 Subject: feat: add signed memory loads --- tests/codegen_test.c | 37 +++++++++++++++++++++++++++++++++++++ tests/parser_test.c | 40 ++++++++++++++++++++++++++++++++++++++++ tests/sema_test.c | 8 ++++++++ 3 files changed, 85 insertions(+) (limited to 'tests') diff --git a/tests/codegen_test.c b/tests/codegen_test.c index db23791..e2969db 100644 --- a/tests/codegen_test.c +++ b/tests/codegen_test.c @@ -100,6 +100,23 @@ static void test_generate_if_else(struct TestContext* context) free_program(&program); } +static void test_generate_negative(struct TestContext* context) +{ + struct Lexer lexer = create_lexer( + "const N = -8\nproc main\n{\nrax = -5\nrbx = rax + -3\n}\n"); + struct Program program; + check(context, parse_program(&lexer, &program)); + + char buffer[1024]; + generate_to_buffer(&program, buffer, sizeof(buffer)); + + check(context, strstr(buffer, "%define N (-8)") != NULL); + check(context, strstr(buffer, "mov rax, -5") != NULL); + check(context, strstr(buffer, "add rbx, -3") != NULL); + + free_program(&program); +} + static void test_generate_call(struct TestContext* context) { struct Lexer lexer = create_lexer( @@ -344,6 +361,24 @@ static void test_generate_load(struct TestContext* context) free_program(&program); } +static void test_generate_signed_load(struct TestContext* context) +{ + struct Lexer lexer = create_lexer( + "proc main\n{\nrax = ^signed byte rsi\nrbx = ^signed word rsi\nrcx = ^signed dword rsi\n}\n"); + struct Program program; + check(context, parse_program(&lexer, &program)); + + char buffer[1024]; + generate_to_buffer(&program, buffer, sizeof(buffer)); + + check(context, strstr(buffer, "movsx rax, byte [rsi]") != NULL); + check(context, strstr(buffer, "movsx rbx, word [rsi]") != NULL); + check(context, strstr(buffer, "movsxd rcx, dword [rsi]") != NULL); + check(context, strstr(buffer, "; TODO") == NULL); + + free_program(&program); +} + static void test_generate_enum_struct(struct TestContext* context) { struct Lexer lexer = create_lexer( @@ -441,6 +476,7 @@ void run_codegen_tests(struct TestContext* context) test_generate_add_zero_peephole(context); test_generate_enum_struct(context); test_generate_load(context); + test_generate_signed_load(context); test_generate_logical_registers(context); test_generate_logical_disabled(context); test_generate_entry_and_bits(context); @@ -449,6 +485,7 @@ void run_codegen_tests(struct TestContext* context) test_generate_text(context); test_generate_if(context); test_generate_if_else(context); + test_generate_negative(context); test_generate_call(context); test_generate_param_substitution(context); test_generate_divide(context); diff --git a/tests/parser_test.c b/tests/parser_test.c index 32ea422..e1199eb 100644 --- a/tests/parser_test.c +++ b/tests/parser_test.c @@ -46,6 +46,25 @@ static void test_parse_const_expr(struct TestContext* context) free_program(&program); } +static void test_parse_unary(struct TestContext* context) +{ + struct Lexer lexer = create_lexer("const N = -8\nproc main\n{\nrax = -5\n}\n"); + struct Program program; + + check(context, parse_program(&lexer, &program)); + + const struct Expr* konst = program.consts[0].value; + check(context, konst->kind == EXPR_UNARY); + check(context, text_is(konst->unary.op, "-")); + check(context, primary_is(konst->unary.operand, "8")); + + const struct Expr* value = program.procs[0].body[0].assign.value; + check(context, value->kind == EXPR_UNARY); + check(context, primary_is(value->unary.operand, "5")); + + free_program(&program); +} + static void test_parse_data(struct TestContext* context) { struct Lexer lexer = create_lexer("data msg = \"hi\"\n"); @@ -264,6 +283,25 @@ static void test_parse_sized_deref(struct TestContext* context) free_program(&program); } +static void test_parse_signed_deref(struct TestContext* context) +{ + struct Lexer lexer = create_lexer("proc main\n{\nrax = ^signed byte rsi\nrbx = ^byte rsi\n}\n"); + struct Program program; + + check(context, parse_program(&lexer, &program)); + + const struct Expr* signed_load = program.procs[0].body[0].assign.value; + check(context, signed_load->kind == EXPR_DEREF); + check(context, signed_load->deref.is_signed); + check(context, signed_load->deref.size == STORE_SIZE_BYTE); + + const struct Expr* plain_load = program.procs[0].body[1].assign.value; + check(context, plain_load->kind == EXPR_DEREF); + check(context, !plain_load->deref.is_signed); + + free_program(&program); +} + static void test_parse_directives(struct TestContext* context) { struct Lexer lexer = create_lexer("[bits: 32]\n[entry: kmain]\n[enable: logical_registers]\nproc kmain\n{\nsyscall\n}\n"); @@ -349,6 +387,7 @@ void run_parser_tests(struct TestContext* context) { test_parse_consts(context); test_parse_const_expr(context); + test_parse_unary(context); test_parse_data(context); test_parse_proc_params(context); test_parse_proc_body(context); @@ -360,6 +399,7 @@ void run_parser_tests(struct TestContext* context) test_parse_call(context); test_parse_stack(context); test_parse_sized_deref(context); + test_parse_signed_deref(context); test_parse_directives(context); test_parse_bad_directive(context); test_parse_register_size_suffix(context); diff --git a/tests/sema_test.c b/tests/sema_test.c index dc01d47..643ffdf 100644 --- a/tests/sema_test.c +++ b/tests/sema_test.c @@ -97,6 +97,13 @@ static void test_deref_needs_register(struct TestContext* context) check(context, analyze_source("proc main\n{\nrax = ^rsi\n}\n")); } +static void test_signed_load_needs_size(struct TestContext* context) +{ + check(context, analyze_source("proc main\n{\nrax = ^signed byte rsi\n}\n")); + check(context, !analyze_source("proc main\n{\nrax = ^signed rsi\n}\n")); + check(context, !analyze_source("proc main\n{\nrax = ^signed qword rsi\n}\n")); +} + static void test_stack_size_constant(struct TestContext* context) { check(context, analyze_source( @@ -152,6 +159,7 @@ void run_sema_tests(struct TestContext* context) test_const_expr_rejects_register(context); test_const_expr_rejects_data(context); test_deref_needs_register(context); + test_signed_load_needs_size(context); test_stack_size_constant(context); test_enum_struct_members(context); test_references_resolve(context); -- cgit v1.3