public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
* [PATCH olang] codegen: x86_64: add support to else statement
@ 2024-09-09 22:30 Carlos Maniero
  2024-09-10  0:33 ` Johnny Richard
  0 siblings, 1 reply; 2+ messages in thread
From: Carlos Maniero @ 2024-09-09 22:30 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/codegen_linux_x86_64.c                     | 11 +++++++++++
 tests/integration/tests/0023_else_statement.ol |  4 ++++
 2 files changed, 15 insertions(+)

diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 958c46e..8ed127c 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -324,8 +324,10 @@ codegen_linux_x86_64_emit_block(FILE *out, ast_block_t *block)
 
                 ast_node_t *cond = if_stmt.cond;
                 ast_node_t *then = if_stmt.then;
+                ast_node_t *_else = if_stmt._else;
 
                 size_t end_if_label = codegen_linux_x86_64_get_next_label();
+                size_t end_else_label = codegen_linux_x86_64_get_next_label();
 
                 codegen_linux_x86_64_emit_expression(out, cond);
                 fprintf(out, "    cmp $1, %%rax\n");
@@ -335,8 +337,17 @@ codegen_linux_x86_64_emit_block(FILE *out, ast_block_t *block)
                 ast_block_t then_block = then->as_block;
 
                 codegen_linux_x86_64_emit_block(out, &then_block);
+                fprintf(out, "    jmp .L%ld\n", end_else_label);
 
                 fprintf(out, ".L%ld:\n", end_if_label);
+
+                if (_else != NULL) {
+                    ast_block_t else_block = _else->as_block;
+                    codegen_linux_x86_64_emit_block(out, &else_block);
+                }
+
+                fprintf(out, ".L%ld:\n", end_else_label);
+
                 break;
             }
             default: {
diff --git a/tests/integration/tests/0023_else_statement.ol b/tests/integration/tests/0023_else_statement.ol
index a741537..fafb22e 100644
--- a/tests/integration/tests/0023_else_statement.ol
+++ b/tests/integration/tests/0023_else_statement.ol
@@ -21,6 +21,10 @@ fn main(): u32 {
   }
 }
 
+# TEST test_compile(exit_code=0)
+
+# TEST test_run_binary(exit_code=0)
+
 # TEST test_contains_tokens WITH
 # ./tests/0023_else_statement.ol:19:5: <else>
 # END

base-commit: 6d2288e9adf715b301eb62b9287b103a37353c9c
-- 
2.34.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-09-09 22:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-09 22:30 [PATCH olang] codegen: x86_64: add support to else statement Carlos Maniero
2024-09-10  0:33 ` Johnny Richard

Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox