public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang] codegen: x86_64: add support to else statement
Date: Mon, 09 Sep 2024 22:30:40 +0000 (UTC)	[thread overview]
Message-ID: <20240909223020.226581-1-carlos@maniero.me> (raw)

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


             reply	other threads:[~2024-09-09 22:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-09 22:30 Carlos Maniero [this message]
2024-09-10  0:33 ` Johnny Richard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240909223020.226581-1-carlos@maniero.me \
    --to=carlos@maniero.me \
    --cc=~johnnyrichard/olang-devel@lists.sr.ht \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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