From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v2 1/2] codegen: x86_64: extract function to emit expression
Date: Sun, 25 Aug 2024 15:16:43 +0200 [thread overview]
Message-ID: <20240825132523.253490-2-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240825132523.253490-1-johnny@johnnyrichard.com>
We want to generalize how we emit expressions since we could have
different ast_node types. Today we are expecting integer literal but we
want to make room for emitting binary operations as well.
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
src/codegen_linux_x86_64.c | 29 ++++++++++++++++++++++-------
1 file changed, 22 insertions(+), 7 deletions(-)
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 7ccb252..b277014 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -56,6 +56,24 @@ codegen_linux_x86_64_emit_start_entrypoint(FILE *out)
fprintf(out, " syscall\n");
}
+static void
+codegen_linux_x86_64_emit_expression(FILE *out, ast_node_t *expr_node)
+{
+ switch (expr_node->kind) {
+ case AST_NODE_LITERAL: {
+ ast_literal_t literal_u32 = expr_node->as_literal;
+ assert(literal_u32.kind == AST_LITERAL_U32);
+ uint32_t exit_code = literal_u32.as_u32;
+
+ fprintf(out, " mov $%d, %%eax\n", exit_code);
+ return;
+ }
+ case AST_NODE_BINARY_OP:
+ default:
+ assert(0 && "NOT IMPLEMENTED");
+ }
+}
+
static void
codegen_linux_x86_64_emit_function(FILE *out, ast_fn_definition_t *fn)
{
@@ -70,14 +88,11 @@ codegen_linux_x86_64_emit_function(FILE *out, ast_fn_definition_t *fn)
assert(return_node->kind == AST_NODE_RETURN_STMT);
ast_return_stmt_t return_stmt = return_node->as_return_stmt;
- ast_node_t *literal_node = return_stmt.data;
- assert(literal_node->kind == AST_NODE_LITERAL);
- ast_literal_t literal_u32 = literal_node->as_literal;
-
- assert(literal_u32.kind == AST_LITERAL_U32);
- uint32_t exit_code = literal_u32.as_u32;
+ ast_node_t *expr = return_stmt.data;
fprintf(out, "" SV_FMT ":\n", SV_ARG(fn->identifier));
- fprintf(out, " mov $%d, %%eax\n", exit_code);
+
+ codegen_linux_x86_64_emit_expression(out, expr);
+
fprintf(out, " ret\n");
}
--
2.46.0
next prev parent reply other threads:[~2024-08-25 13:25 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-25 13:16 [PATCH olang v2 0/2] codegen: x86_64: implement binary operations Johnny Richard
2024-08-25 13:16 ` Johnny Richard [this message]
2024-08-25 13:16 ` [PATCH olang v2 2/2] " Johnny Richard
2024-08-25 13:26 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-05 22:49 ` [PATCH olang v2 0/2] codegen: x86_64: implement binary operations Carlos Maniero
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=20240825132523.253490-2-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