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 v1 2/3] codegen: move label_index global variable to the codegen scope
Date: Mon, 23 Sep 2024 10:11:54 +0000 (UTC)	[thread overview]
Message-ID: <20240923101141.76783-3-carlos@maniero.me> (raw)
In-Reply-To: <20240923101141.76783-1-carlos@maniero.me>

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/codegen_linux_x86_64.c | 19 ++++++++-----------
 src/codegen_linux_x86_64.h |  1 +
 2 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index e604144..2e329ca 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -30,9 +30,6 @@
 // must be preserved else the ret instruction will jump to nowere.
 #define X86_CALL_EIP_STACK_OFFSET (8)
 
-// FIXME: move label_index to codegen_linux_x86_64_t structure
-size_t label_index;
-
 static void
 codegen_linux_x86_64_emit_start_entrypoint(codegen_x86_64_t *codegen);
 
@@ -60,7 +57,7 @@ codegen_linux_x86_64_init(codegen_x86_64_t *codegen, arena_t *arena, FILE *out)
 void
 codegen_linux_x86_64_emit_program(codegen_x86_64_t *codegen, ast_node_t *node)
 {
-    label_index = 0;
+    codegen->label_index = 0;
     codegen_linux_x86_64_emit_start_entrypoint(codegen);
 
     assert(node->kind == AST_NODE_PROGRAM);
@@ -86,9 +83,9 @@ codegen_linux_x86_64_emit_start_entrypoint(codegen_x86_64_t *codegen)
 }
 
 static size_t
-codegen_linux_x86_64_get_next_label(void)
+codegen_linux_x86_64_get_next_label(codegen_x86_64_t *codegen)
 {
-    return ++label_index;
+    return ++codegen->label_index;
 }
 
 static void
@@ -301,7 +298,7 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
                     return;
                 }
                 case AST_BINOP_LOGICAL_AND: {
-                    size_t label_exit = codegen_linux_x86_64_get_next_label();
+                    size_t label_exit = codegen_linux_x86_64_get_next_label(codegen);
 
                     codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                     fprintf(codegen->out, "    cmp $0, %%rax\n");
@@ -316,8 +313,8 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
                     return;
                 }
                 case AST_BINOP_LOGICAL_OR: {
-                    size_t label_t = codegen_linux_x86_64_get_next_label();
-                    size_t label_f = codegen_linux_x86_64_get_next_label();
+                    size_t label_t = codegen_linux_x86_64_get_next_label(codegen);
+                    size_t label_f = codegen_linux_x86_64_get_next_label(codegen);
 
                     codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                     fprintf(codegen->out, "    cmp $0, %%rax\n");
@@ -402,8 +399,8 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block)
                 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();
+                size_t end_if_label = codegen_linux_x86_64_get_next_label(codegen);
+                size_t end_else_label = codegen_linux_x86_64_get_next_label(codegen);
 
                 codegen_linux_x86_64_emit_expression(codegen, cond);
                 fprintf(codegen->out, "    cmp $1, %%rax\n");
diff --git a/src/codegen_linux_x86_64.h b/src/codegen_linux_x86_64.h
index cad530a..803c080 100644
--- a/src/codegen_linux_x86_64.h
+++ b/src/codegen_linux_x86_64.h
@@ -26,6 +26,7 @@ typedef struct codegen_x86_64
 {
     arena_t *arena;
     size_t base_offset;
+    size_t label_index;
     map_t *symbols_stack_offset;
     FILE *out;
 } codegen_x86_64_t;
-- 
2.34.1


  parent reply	other threads:[~2024-09-23 10:12 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-23 10:11 [PATCH olang v1 0/3] Housekeeping: resolve a few FIXMEs related to code style Carlos Maniero
2024-09-23 10:11 ` [PATCH olang v1 1/3] ast: return_stmt rename expr field and add it to the factory fn Carlos Maniero
2024-09-23 11:02   ` Johnny Richard
2024-09-23 11:47     ` Carlos Maniero
2024-09-23 10:11 ` Carlos Maniero [this message]
2024-09-23 10:11 ` [PATCH olang v1 3/3] naming: rename all identifier symbols to id Carlos Maniero
2024-09-23 10:12   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-23 11:08 ` [PATCH olang v1 0/3] Housekeeping: resolve a few FIXMEs related to code style 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=20240923101141.76783-3-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