From 81799f3e41ad9cab305930b7743a752003ff16ec Mon Sep 17 00:00:00 2001 From: hachem Date: Sun, 30 Aug 2026 22:42:20 +0200 Subject: feat: sized memory stores bia ^ --- tests/codegen_test.c | 26 ++++++++++++++++++++++++++ tests/lexer_test.c | 10 ++++++++++ tests/parser_test.c | 21 +++++++++++++++++++++ 3 files changed, 57 insertions(+) (limited to 'tests') diff --git a/tests/codegen_test.c b/tests/codegen_test.c index a72638b..64a422d 100644 --- a/tests/codegen_test.c +++ b/tests/codegen_test.c @@ -203,6 +203,31 @@ static void test_generate_address_expr(struct TestContext* context) free_program(&program); } +static void test_generate_sized_store(struct TestContext* context) +{ + struct Lexer lexer = create_lexer( + "proc main\n{\n^byte rsi = rdx\n^dword rsi = rax\n^byte rsi = 5\n^rsi = rbx\n}\n"); + struct Program program; + check(context, parse_program(&lexer, &program)); + + FILE* out = tmpfile(); + generate_nasm(&program, out); + fflush(out); + rewind(out); + + char buffer[1024]; + size_t read = fread(buffer, 1, sizeof(buffer) - 1, out); + buffer[read] = '\0'; + fclose(out); + + check(context, strstr(buffer, "mov byte [rsi], dl") != NULL); + check(context, strstr(buffer, "mov dword [rsi], eax") != NULL); + check(context, strstr(buffer, "mov byte [rsi], 5") != NULL); + check(context, strstr(buffer, "mov [rsi], rbx") != NULL); + + free_program(&program); +} + void run_codegen_tests(struct TestContext* context) { test_generate_consts_and_data(context); @@ -213,4 +238,5 @@ void run_codegen_tests(struct TestContext* context) test_generate_divide(context); test_generate_stack_frame(context); test_generate_address_expr(context); + test_generate_sized_store(context); } diff --git a/tests/lexer_test.c b/tests/lexer_test.c index 11b15b9..f677793 100644 --- a/tests/lexer_test.c +++ b/tests/lexer_test.c @@ -45,6 +45,15 @@ static void test_keywords(struct TestContext* context) check(context, scan_token(&lexer).type == TOKEN_IDENTIFIER); } +static void test_size_keywords(struct TestContext* context) +{ + struct Lexer lexer = create_lexer("byte word dword qword"); + check(context, scan_token(&lexer).type == TOKEN_BYTE); + check(context, scan_token(&lexer).type == TOKEN_WORD); + check(context, scan_token(&lexer).type == TOKEN_DWORD); + check(context, scan_token(&lexer).type == TOKEN_QWORD); +} + static void test_line_counting(struct TestContext* context) { struct Lexer lexer = create_lexer("a\nb\nc"); @@ -70,6 +79,7 @@ void run_lexer_tests(struct TestContext* context) test_operators(context); test_literals(context); test_keywords(context); + test_size_keywords(context); test_line_counting(context); test_comments(context); } diff --git a/tests/parser_test.c b/tests/parser_test.c index bbbbaa2..61f8c37 100644 --- a/tests/parser_test.c +++ b/tests/parser_test.c @@ -188,6 +188,26 @@ static void test_parse_stack(struct TestContext* context) free_program(&program); } +static void test_parse_sized_deref(struct TestContext* context) +{ + struct Lexer lexer = create_lexer("proc main\n{\n^byte rsi = rdx\n^rdi = rax\n}\n"); + struct Program program; + + check(context, parse_program(&lexer, &program)); + check(context, program.procs[0].body_count == 2); + + struct AssignStatement sized = program.procs[0].body[0].assign; + check(context, sized.target_deref); + check(context, sized.store_size == STORE_SIZE_BYTE); + check(context, text_is(sized.target, "rsi")); + + struct AssignStatement plain = program.procs[0].body[1].assign; + check(context, plain.target_deref); + check(context, plain.store_size == STORE_SIZE_NONE); + + free_program(&program); +} + static void test_parse_errors(struct TestContext* context) { struct Program program; @@ -216,5 +236,6 @@ void run_parser_tests(struct TestContext* context) test_parse_if(context); test_parse_call(context); test_parse_stack(context); + test_parse_sized_deref(context); test_parse_errors(context); } -- cgit v1.3