* [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 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