public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
* [PATCH olang v1 0/3] codege: initial implementation of pointers
@ 2024-10-14 12:06 Carlos Maniero
  2024-10-14 12:06 ` [PATCH olang v1 1/3] fix: checker: populate vardef value scope Carlos Maniero
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Carlos Maniero @ 2024-10-14 12:06 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero

This patchset enables the following program in olang:

fn main(): u32 {
  var a: u32 = 1
  var b: u32* = &a
  *b = 0
  return a
}

It is still not possible to perform multiple dereference (**b).

There was an issue during the vardef scope population where the vardef
value was not been populated (aka no scope set).

Carlos Maniero (3):
  fix: checker: populate vardef value scope
  codegen: x64: generate address of (&)
  codegen: x64: generate dereference assignment

 src/checker.c              |  5 +++
 src/codegen_linux_x86_64.c | 89 ++++++++++++++++++++++++++++++--------
 tests/olc/0034_pointers.ol |  4 +-
 3 files changed, 79 insertions(+), 19 deletions(-)


base-commit: f1a40e971f5a8890acf2015632a39b64ca29e30c
-- 
2.46.1


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

* [PATCH olang v1 1/3] fix: checker: populate vardef value scope
  2024-10-14 12:06 [PATCH olang v1 0/3] codege: initial implementation of pointers Carlos Maniero
@ 2024-10-14 12:06 ` Carlos Maniero
  2024-10-14 12:06 ` [PATCH olang v1 2/3] codegen: x64: generate address of (&) Carlos Maniero
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Carlos Maniero @ 2024-10-14 12:06 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero

It also include asserts on function enter to ensure the pointers are
set.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/checker.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/checker.c b/src/checker.c
index 1230c3c..0acab13 100644
--- a/src/checker.c
+++ b/src/checker.c
@@ -105,6 +105,9 @@ checker_check(checker_t *checker, ast_node_t *ast)
 static void
 populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
 {
+    assert(checker);
+    assert(scope);
+
     switch (ast->kind) {
         case AST_NODE_TRANSLATION_UNIT: {
             list_item_t *item = list_head(ast->as_translation_unit.decls);
@@ -219,6 +222,8 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
 
             scope_insert(scope, symbol);
             ast->as_var_def.scope = scope;
+
+            populate_scope(checker, scope, ast->as_var_def.value);
             return;
         }
 
-- 
2.46.1


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

* [PATCH olang v1 2/3] codegen: x64: generate address of (&)
  2024-10-14 12:06 [PATCH olang v1 0/3] codege: initial implementation of pointers Carlos Maniero
  2024-10-14 12:06 ` [PATCH olang v1 1/3] fix: checker: populate vardef value scope Carlos Maniero
@ 2024-10-14 12:06 ` Carlos Maniero
  2024-10-14 12:06 ` [PATCH olang v1 3/3] codegen: x64: generate dereference assignment Carlos Maniero
  2024-10-14 14:35 ` [PATCH olang v1 0/3] codege: initial implementation of pointers Johnny Richard
  3 siblings, 0 replies; 6+ messages in thread
From: Carlos Maniero @ 2024-10-14 12:06 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero

At this point is not possible to reasign a pointer reference value.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/codegen_linux_x86_64.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 053963a..153c25d 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -699,6 +699,22 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen,
 
                     return expr_bytes;
                 }
+                case AST_UNARY_ADDRESSOF: {
+                    assert(unary_op.expr->kind == AST_NODE_REF &&
+                           "unsupported unary expression for addressof (&)");
+
+                    ast_ref_t ref = unary_op.expr->as_ref;
+
+                    symbol_t *symbol = scope_lookup(ref.scope, ref.id);
+                    assert(symbol);
+
+                    size_t offset =
+                        codegen_linux_x86_64_get_stack_offset(codegen, symbol);
+
+                    fprintf(
+                        codegen->out, "    lea -%ld(%%rbp), %%rax\n", offset);
+                    return 8;
+                }
                 default: {
                     assert(0 && "unsupported unary operation");
                     return 0;
-- 
2.46.1


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

* [PATCH olang v1 3/3] codegen: x64: generate dereference assignment
  2024-10-14 12:06 [PATCH olang v1 0/3] codege: initial implementation of pointers Carlos Maniero
  2024-10-14 12:06 ` [PATCH olang v1 1/3] fix: checker: populate vardef value scope Carlos Maniero
  2024-10-14 12:06 ` [PATCH olang v1 2/3] codegen: x64: generate address of (&) Carlos Maniero
@ 2024-10-14 12:06 ` Carlos Maniero
  2024-10-14 12:07   ` [olang/patches/.build.yml] build success builds.sr.ht
  2024-10-14 14:35 ` [PATCH olang v1 0/3] codege: initial implementation of pointers Johnny Richard
  3 siblings, 1 reply; 6+ messages in thread
From: Carlos Maniero @ 2024-10-14 12:06 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/codegen_linux_x86_64.c | 73 +++++++++++++++++++++++++++++---------
 tests/olc/0034_pointers.ol |  4 +--
 2 files changed, 58 insertions(+), 19 deletions(-)

diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 153c25d..fc8fcc4 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -657,23 +657,54 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen,
                     return 1;
                 }
                 case AST_BINOP_ASSIGN: {
-                    // FIXME: It may not be a ref
-                    ast_ref_t ref = bin_op.lhs->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, bin_op.rhs);
-
-                    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);
+                    switch (bin_op.lhs->kind) {
+                        case AST_NODE_REF: {
+                            ast_ref_t ref = bin_op.lhs->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,
+                                                                 bin_op.rhs);
+
+                            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_UNARY_OP: {
+                            assert(bin_op.lhs->as_unary_op.kind ==
+                                       AST_UNARY_DEREFERENCE &&
+                                   "unsupported assignment lhs");
+
+                            codegen_linux_x86_64_emit_expression(codegen,
+                                                                 bin_op.lhs);
+
+                            fprintf(codegen->out, "    push %%rax\n");
+
+                            size_t type_size =
+                                codegen_linux_x86_64_emit_expression(
+                                    codegen, bin_op.rhs);
+
+                            fprintf(codegen->out, "    pop %%rdx\n");
+
+                            fprintf(codegen->out,
+                                    "    mov %s, (%%rdx) \n",
+                                    get_reg_for(REG_ACCUMULATOR, type_size));
+
+                            break;
+                        }
+                        default: {
+                            assert(false && "unsupported assignment lhs");
+                        }
+                    }
 
                     // FIXME: we don't support a = b = c
                     return 0;
@@ -715,6 +746,14 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen,
                         codegen->out, "    lea -%ld(%%rbp), %%rax\n", offset);
                     return 8;
                 }
+                case AST_UNARY_DEREFERENCE: {
+                    // FIXME: support dereference of dereference (**)
+                    assert(unary_op.expr->kind == AST_NODE_REF &&
+                           "unsupported unary expression for dereference (*)");
+
+                    return codegen_linux_x86_64_emit_expression(codegen,
+                                                                unary_op.expr);
+                }
                 default: {
                     assert(0 && "unsupported unary operation");
                     return 0;
diff --git a/tests/olc/0034_pointers.ol b/tests/olc/0034_pointers.ol
index 0e95af4..b3a693b 100644
--- a/tests/olc/0034_pointers.ol
+++ b/tests/olc/0034_pointers.ol
@@ -20,9 +20,9 @@ fn main(): u32 {
   return a
 }
 
-# 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
-- 
2.46.1


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

* [olang/patches/.build.yml] build success
  2024-10-14 12:06 ` [PATCH olang v1 3/3] codegen: x64: generate dereference assignment Carlos Maniero
@ 2024-10-14 12:07   ` builds.sr.ht
  0 siblings, 0 replies; 6+ messages in thread
From: builds.sr.ht @ 2024-10-14 12:07 UTC (permalink / raw)
  To: Carlos Maniero; +Cc: ~johnnyrichard/olang-devel

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

[codege: initial implementation of pointers][0] from [Carlos Maniero][1]

[0]: https://lists.sr.ht/~johnnyrichard/olang-devel/patches/55469
[1]: mailto:carlos@maniero.me

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

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

* Re: [PATCH olang v1 0/3] codege: initial implementation of pointers
  2024-10-14 12:06 [PATCH olang v1 0/3] codege: initial implementation of pointers Carlos Maniero
                   ` (2 preceding siblings ...)
  2024-10-14 12:06 ` [PATCH olang v1 3/3] codegen: x64: generate dereference assignment Carlos Maniero
@ 2024-10-14 14:35 ` Johnny Richard
  3 siblings, 0 replies; 6+ messages in thread
From: Johnny Richard @ 2024-10-14 14:35 UTC (permalink / raw)
  To: Carlos Maniero; +Cc: ~johnnyrichard/olang-devel

Thanks, applied!

To git.sr.ht:~johnnyrichard/olang
   f1a40e9..cf5e4ab  main -> main


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

end of thread, other threads:[~2024-10-14 12:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-14 12:06 [PATCH olang v1 0/3] codege: initial implementation of pointers Carlos Maniero
2024-10-14 12:06 ` [PATCH olang v1 1/3] fix: checker: populate vardef value scope Carlos Maniero
2024-10-14 12:06 ` [PATCH olang v1 2/3] codegen: x64: generate address of (&) Carlos Maniero
2024-10-14 12:06 ` [PATCH olang v1 3/3] codegen: x64: generate dereference assignment Carlos Maniero
2024-10-14 12:07   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-10-14 14:35 ` [PATCH olang v1 0/3] codege: initial implementation of pointers Johnny Richard

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