From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang v1 3/3] codegen: x64: generate dereference assignment
Date: Mon, 14 Oct 2024 12:06:25 +0000 (UTC) [thread overview]
Message-ID: <20241014120607.90304-4-carlos@maniero.me> (raw)
In-Reply-To: <20241014120607.90304-1-carlos@maniero.me>
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
next prev parent reply other threads:[~2024-10-14 12:06 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
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=20241014120607.90304-4-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