public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v1 1/2] codegen: x86_64: extract function to emit expression
Date: Sat, 27 Apr 2024 14:14:11 +0200	[thread overview]
Message-ID: <20240427121832.203068-2-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240427121832.203068-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 d4f8222..72db1c6 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->data.as_literal;
+            assert(literal_u32.kind == AST_LITERAL_U32);
+            uint32_t exit_code = literal_u32.value.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->data.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->data.as_literal;
-
-    assert(literal_u32.kind == AST_LITERAL_U32);
-    uint32_t exit_code = literal_u32.value.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.44.0


  reply	other threads:[~2024-04-27 11:20 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-27 12:14 [PATCH olang v1 0/2] codegen: x86_64: implement binary operations Johnny Richard
2024-04-27 12:14 ` Johnny Richard [this message]
2024-04-27 12:14 ` [PATCH olang v1 2/2] " Johnny Richard
2024-04-27 11:21   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-05-03  2:04   ` [PATCH olang v1 2/2] codegen: x86_64: implement binary operations Carlos Maniero
2024-08-25 13:52 ` [PATCH olang v1 0/2] " 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=20240427121832.203068-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