public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
* [olang/patches/.build.yml] build success
  2024-10-08 14:49 [PATCH olang v1] codegen: x86_64: emit assembly for var assignment stmts Johnny Richard
@ 2024-10-08 12:50 ` builds.sr.ht
  2024-10-08 13:54 ` [PATCH olang v1] codegen: x86_64: emit assembly for var assignment stmts Carlos Maniero
  1 sibling, 0 replies; 3+ messages in thread
From: builds.sr.ht @ 2024-10-08 12:50 UTC (permalink / raw)
  To: Johnny Richard; +Cc: ~johnnyrichard/olang-devel

olang/patches/.build.yml: SUCCESS in 22s

[codegen: x86_64: emit assembly for var assignment stmts][0] from [Johnny Richard][1]

[0]: https://lists.sr.ht/~johnnyrichard/olang-devel/patches/55381
[1]: mailto:johnny@johnnyrichard.com

✓ #1346372 SUCCESS olang/patches/.build.yml https://builds.sr.ht/~johnnyrichard/job/1346372

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH olang v1] codegen: x86_64: emit assembly for var assignment stmts
  2024-10-08 14:49 [PATCH olang v1] codegen: x86_64: emit assembly for var assignment stmts Johnny Richard
  2024-10-08 12:50 ` [olang/patches/.build.yml] build success builds.sr.ht
@ 2024-10-08 13:54 ` Carlos Maniero
  1 sibling, 0 replies; 3+ messages in thread
From: Carlos Maniero @ 2024-10-08 13:54 UTC (permalink / raw)
  To: Johnny Richard, ~johnnyrichard/olang-devel

Approved! Thanks.

To git.sr.ht:~johnnyrichard/olang
   dbbfdf2..3780214  main -> main

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH olang v1] codegen: x86_64: emit assembly for var assignment stmts
@ 2024-10-08 14:49 Johnny Richard
  2024-10-08 12:50 ` [olang/patches/.build.yml] build success builds.sr.ht
  2024-10-08 13:54 ` [PATCH olang v1] codegen: x86_64: emit assembly for var assignment stmts Carlos Maniero
  0 siblings, 2 replies; 3+ messages in thread
From: Johnny Richard @ 2024-10-08 14:49 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard

Since the codegen for var assignment also fetch offset on stack_offset
map and this logic is spread everywhere, I've introduced two new
functions to 'put' and 'get' stack_offsets to reduce code duplication.

I also reduced the key string key size for stack_offset by replacing
'%p' to '%lx' once the second option don't add an extra '0x' prefix.

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 src/codegen_linux_x86_64.c      | 74 +++++++++++++++++++++++----------
 tests/olc/0029_var_assigment.ol |  4 +-
 2 files changed, 55 insertions(+), 23 deletions(-)

diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index e501634..ebda8eb 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -24,7 +24,7 @@
 #include "scope.h"
 
 #define SYS_exit (60)
-#define PTR_HEX_CSTR_SIZE (18 + 1)
+#define PTR_HEX_CSTR_SIZE (16 + 1)
 
 // The call instruction pushes EIP into stack so the first 8 bytes from stack
 // must be preserved else the ret instruction will jump to nowere.
@@ -66,6 +66,12 @@ codegen_linux_x86_64_emit_start_entrypoint(codegen_x86_64_t *codegen);
 static void
 codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_t *fn);
 
+static void
+codegen_linux_x86_64_put_stack_offset(codegen_x86_64_t *codegen, symbol_t *symbol, size_t offset);
+
+static size_t
+codegen_linux_x86_64_get_stack_offset(codegen_x86_64_t *codegen, symbol_t *symbol);
+
 static size_t
 type_to_bytes(type_t *type);
 
@@ -154,15 +160,11 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
             symbol_t *symbol = scope_lookup(ref.scope, ref.id);
             assert(symbol);
 
-            char symbol_ptr[PTR_HEX_CSTR_SIZE];
-            sprintf(symbol_ptr, "%p", (void *)symbol);
-
-            size_t *offset = (size_t *)map_get(codegen->symbols_stack_offset, symbol_ptr);
-            assert(offset);
+            size_t offset = codegen_linux_x86_64_get_stack_offset(codegen, symbol);
 
             size_t bytes = type_to_bytes(&symbol->type);
 
-            fprintf(codegen->out, "    mov -%ld(%%rbp), %s\n", *offset, get_reg_for(REG_ACCUMULATOR, bytes));
+            fprintf(codegen->out, "    mov -%ld(%%rbp), %s\n", offset, get_reg_for(REG_ACCUMULATOR, bytes));
             return bytes;
         }
         case AST_NODE_FN_CALL: {
@@ -559,18 +561,12 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block)
                 symbol_t *symbol = scope_lookup(scope, var_def.id);
                 assert(symbol);
 
-                char symbol_ptr[PTR_HEX_CSTR_SIZE];
-                sprintf(symbol_ptr, "%p", (void *)symbol);
+                codegen_linux_x86_64_put_stack_offset(codegen, symbol, codegen->base_offset);
 
                 if (var_def.value) {
                     codegen_linux_x86_64_emit_expression(codegen, var_def.value);
                 }
 
-                size_t *offset = arena_alloc(codegen->arena, sizeof(size_t));
-                *offset = codegen->base_offset;
-
-                map_put(codegen->symbols_stack_offset, symbol_ptr, offset);
-
                 size_t type_size = type_to_bytes(&symbol->type);
 
                 fprintf(codegen->out,
@@ -582,6 +578,24 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block)
                 break;
             }
 
+            case AST_NODE_VAR_ASSIGN_STMT: {
+                ast_var_assign_stmt_t var_assign = node->as_var_assign_stmt;
+                ast_ref_t ref = var_assign.ref->as_ref;
+                scope_t *scope = ref.scope;
+
+                symbol_t *symbol = scope_lookup(scope, ref.id);
+                assert(symbol);
+
+                size_t offset = codegen_linux_x86_64_get_stack_offset(codegen, symbol);
+
+                codegen_linux_x86_64_emit_expression(codegen, var_assign.expr);
+
+                size_t type_size = type_to_bytes(&symbol->type);
+                fprintf(codegen->out, "    mov %s, -%ld(%%rbp)\n", get_reg_for(REG_ACCUMULATOR, type_size), offset);
+
+                break;
+            }
+
             case AST_NODE_IF_STMT: {
                 ast_if_stmt_t if_stmt = node->as_if_stmt;
 
@@ -689,21 +703,17 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_
 
         ast_fn_param_t *param = item->value;
 
-        size_t *offset = arena_alloc(codegen->arena, sizeof(size_t));
-        *offset = codegen->base_offset;
-
         symbol_t *symbol = scope_lookup(fn_def->scope, param->id);
         assert(symbol);
 
-        char symbol_ptr[PTR_HEX_CSTR_SIZE];
-        sprintf(symbol_ptr, "%p", (void *)symbol);
+        size_t offset = codegen->base_offset;
 
-        map_put(codegen->symbols_stack_offset, symbol_ptr, offset);
+        codegen_linux_x86_64_put_stack_offset(codegen, symbol, codegen->base_offset);
 
         fprintf(codegen->out,
                 "    mov %s, -%ld(%%rbp)\n",
                 get_reg_for(x86_call_args[i], symbol->type.as_primitive.size),
-                *offset);
+                offset);
 
         // FIXME: add offset according to the param size
         codegen->base_offset += 8;
@@ -722,6 +732,28 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_
     codegen_linux_x86_64_emit_block(codegen, &block);
 }
 
+static void
+codegen_linux_x86_64_put_stack_offset(codegen_x86_64_t *codegen, symbol_t *symbol, size_t offset)
+{
+
+    size_t *stack_offset = arena_alloc(codegen->arena, sizeof(size_t));
+    *stack_offset = offset;
+
+    char symbol_ptr[PTR_HEX_CSTR_SIZE];
+    sprintf(symbol_ptr, "%lx", (uintptr_t)symbol);
+
+    map_put(codegen->symbols_stack_offset, symbol_ptr, stack_offset);
+}
+
+static size_t
+codegen_linux_x86_64_get_stack_offset(codegen_x86_64_t *codegen, symbol_t *symbol)
+{
+    char symbol_ptr[PTR_HEX_CSTR_SIZE];
+    sprintf(symbol_ptr, "%lx", (uintptr_t)symbol);
+
+    return *(size_t *)map_get(codegen->symbols_stack_offset, symbol_ptr);
+}
+
 static char *
 get_reg_for(x86_64_register_type_t type, size_t bytes)
 {
diff --git a/tests/olc/0029_var_assigment.ol b/tests/olc/0029_var_assigment.ol
index 6c5c46f..0e04fcc 100644
--- a/tests/olc/0029_var_assigment.ol
+++ b/tests/olc/0029_var_assigment.ol
@@ -19,9 +19,9 @@ fn main(): u32 {
   return code
 }
 
-# XTEST test_compile(exit_code=0)
+# TEST test_compile(exit_code=0)
 
-# XTEST test_run_binary(exit_code=0)
+# TEST test_run_binary(exit_code=0)
 
 # TEST test_ast WITH
 # Translation_Unit

base-commit: dbbfdf2acab2b022cc9f0157d4341478d82b8838
-- 
2.46.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-10-08 13:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-08 14:49 [PATCH olang v1] codegen: x86_64: emit assembly for var assignment stmts Johnny Richard
2024-10-08 12:50 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-10-08 13:54 ` [PATCH olang v1] codegen: x86_64: emit assembly for var assignment stmts Carlos Maniero

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