From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v2 3/4] codegen: x86_64: add support to emit if statements
Date: Sun, 8 Sep 2024 03:10:57 +0200 [thread overview]
Message-ID: <20240908011512.152684-5-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240908011512.152684-2-johnny@johnnyrichard.com>
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
src/codegen_linux_x86_64.c | 63 ++++++++++++++-----
tests/integration/tests/0020_if_statement.ol | 3 +
.../tests/0021_if_statement_failed.ol | 24 +++++++
3 files changed, 76 insertions(+), 14 deletions(-)
create mode 100644 tests/integration/tests/0021_if_statement_failed.ol
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 64ec0e0..3b509ba 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -299,26 +299,61 @@ codegen_linux_x86_64_emit_expression(FILE *out, ast_node_t *expr_node)
assert(0 && "unsupported expression");
}
}
+static void
+codegen_linux_x86_64_emit_block(FILE *out, ast_block_t *block)
+{
+
+ size_t nodes_len = list_size(block->nodes);
+
+ for (size_t i = 0; i < nodes_len; ++i) {
+ ast_node_t *node = list_get(block->nodes, i)->value;
+ switch (node->kind) {
+ case AST_NODE_RETURN_STMT: {
+ ast_return_stmt_t return_stmt = node->as_return_stmt;
+
+ ast_node_t *expr = return_stmt.data;
+
+ codegen_linux_x86_64_emit_expression(out, expr);
+
+ fprintf(out, " ret\n");
+
+ break;
+ }
+ case AST_NODE_IF_STMT: {
+ ast_if_stmt_t if_stmt = node->as_if_stmt;
+
+ ast_node_t *cond = if_stmt.cond;
+ ast_node_t *then = if_stmt.then;
+
+ size_t end_if_label = codegen_linux_x86_64_get_next_label();
+
+ codegen_linux_x86_64_emit_expression(out, cond);
+ fprintf(out, " jnz .L%ld\n", end_if_label);
+
+ assert(then->kind == AST_NODE_BLOCK && "invalid if-then block");
+ ast_block_t then_block = then->as_block;
+
+ codegen_linux_x86_64_emit_block(out, &then_block);
+
+ fprintf(out, ".L%ld:\n", end_if_label);
+ break;
+ }
+ default: {
+ assert(0 && "unsupported block statement");
+ break;
+ }
+ }
+ }
+}
static void
codegen_linux_x86_64_emit_function(FILE *out, ast_fn_definition_t *fn)
{
ast_node_t *block_node = fn->block;
+ fprintf(out, "" SV_FMT ":\n", SV_ARG(fn->identifier));
+
assert(block_node->kind == AST_NODE_BLOCK);
ast_block_t block = block_node->as_block;
- assert(list_size(block.nodes) == 1);
-
- list_item_t *nodes_item = list_get(block.nodes, 0);
- ast_node_t *return_node = nodes_item->value;
- assert(return_node->kind == AST_NODE_RETURN_STMT);
- ast_return_stmt_t return_stmt = return_node->as_return_stmt;
-
- ast_node_t *expr = return_stmt.data;
-
- fprintf(out, "" SV_FMT ":\n", SV_ARG(fn->identifier));
-
- codegen_linux_x86_64_emit_expression(out, expr);
-
- fprintf(out, " ret\n");
+ codegen_linux_x86_64_emit_block(out, &block);
}
diff --git a/tests/integration/tests/0020_if_statement.ol b/tests/integration/tests/0020_if_statement.ol
index ef3cd36..d48122f 100644
--- a/tests/integration/tests/0020_if_statement.ol
+++ b/tests/integration/tests/0020_if_statement.ol
@@ -20,6 +20,9 @@ fn main(): u32 {
return 1
}
+# TEST test_compile(exit_code=0)
+# TEST test_run_binary(exit_code=0)
+#
# TEST test_contains_tokens WITH
# ./tests/0020_if_statement.ol:17:3: <if>
# END
diff --git a/tests/integration/tests/0021_if_statement_failed.ol b/tests/integration/tests/0021_if_statement_failed.ol
new file mode 100644
index 0000000..4bc2838
--- /dev/null
+++ b/tests/integration/tests/0021_if_statement_failed.ol
@@ -0,0 +1,24 @@
+# Copyright (C) 2024 olang mantainers
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+fn main(): u32 {
+ if 0 == 1 {
+ return 1
+ }
+ return 0
+}
+
+# TEST test_compile(exit_code=0)
+# TEST test_run_binary(exit_code=0)
--
2.46.0
next prev parent reply other threads:[~2024-09-07 23:16 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-08 1:10 [PATCH olang v2 0/4] add support to compile " Johnny Richard
2024-09-07 23:23 ` Carlos Maniero
2024-09-08 1:10 ` [PATCH olang v2 1/4] lexer: add support to if token Johnny Richard
2024-09-08 1:10 ` [PATCH olang v2 2/4] parser: parse a simple if statement Johnny Richard
2024-09-08 1:10 ` Johnny Richard [this message]
2024-09-08 1:10 ` [PATCH olang v2 4/4] codegen: x86_64: add if cond support for literals Johnny Richard
2024-09-08 1:36 ` [PATCH olang v2 0/4] add support to compile if statements 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=20240908011512.152684-5-johnny@johnnyrichard.com \
--to=johnny@johnnyrichard.com \
--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