From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang v2 4/4] codegen: operate mov instructions based on the symbol's type
Date: Sun, 22 Sep 2024 00:46:43 +0000 (UTC) [thread overview]
Message-ID: <20240922004619.518310-5-carlos@maniero.me> (raw)
In-Reply-To: <20240922004619.518310-1-carlos@maniero.me>
Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
src/codegen_linux_x86_64.c | 60 ++++++++++++++++---
src/type.c | 18 ++++++
src/type.h | 5 +-
.../tests/0026_primitive_unsigneds.ol | 27 +++++++++
4 files changed, 100 insertions(+), 10 deletions(-)
create mode 100644 tests/integration/tests/0026_primitive_unsigneds.ol
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 1fa6c58..25cda2d 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -26,6 +26,10 @@
#define SYS_exit (60)
#define PTR_HEX_CSTR_SIZE (18 + 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.
+#define X86_CALL_EIP_STACK_OFFSET (8)
+
// FIXME: move label_index to codegen_linux_x86_64_t structure
size_t label_index;
@@ -35,6 +39,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 size_t
+type_to_bytes(type_t *type);
+
+static char *
+get_accumulator_reg_for(size_t bytes);
+
void
codegen_linux_x86_64_init(codegen_x86_64_t *codegen, arena_t *arena, FILE *out)
{
@@ -105,7 +115,10 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
size_t *offset = (size_t *)map_get(codegen->symbols_stack_offset, symbol_ptr);
assert(offset);
- fprintf(codegen->out, " mov -%ld(%%rbp), %%rax\n", *offset);
+ fprintf(codegen->out,
+ " mov -%ld(%%rbp), %s\n",
+ *offset,
+ get_accumulator_reg_for(type_to_bytes(&symbol->type)));
return;
}
case AST_NODE_BINARY_OP: {
@@ -366,12 +379,18 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block)
codegen_linux_x86_64_emit_expression(codegen, var_def.value);
}
- codegen->base_offset += 8;
size_t *offset = arena_alloc(codegen->arena, sizeof(size_t));
*offset = codegen->base_offset;
map_put(codegen->symbols_stack_offset, symbol_ptr, offset);
- fprintf(codegen->out, " mov %%rax, -%ld(%%rbp)\n", codegen->base_offset);
+
+ size_t type_size = type_to_bytes(&symbol->type);
+
+ fprintf(codegen->out,
+ " mov %s, -%ld(%%rbp)\n",
+ get_accumulator_reg_for(type_size),
+ codegen->base_offset);
+ codegen->base_offset += type_size;
break;
}
@@ -419,6 +438,18 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block)
codegen->base_offset = block_offset;
}
+static size_t
+type_to_bytes(type_t *type)
+{
+ switch (type->kind) {
+ case TYPE_PRIMITIVE: {
+ return type->as_primitive.size;
+ }
+ }
+
+ assert(0 && "unreachable");
+}
+
static size_t
calculate_fn_local_size(scope_t *scope)
{
@@ -431,9 +462,8 @@ calculate_fn_local_size(scope_t *scope)
map_get_kvs(scope->symbols, kvs);
for (size_t i = 0; i < scope->symbols->size; ++i) {
- // FIXME: symbols must have their types. Since we just have 8bytes
- // variables it is hard coded.
- local_size += 8;
+ symbol_t *symbol = (symbol_t *)kvs[i]->value;
+ local_size += type_to_bytes(&symbol->type);
}
size_t max_child_local_size = 0;
@@ -456,7 +486,8 @@ calculate_fn_local_size(scope_t *scope)
static void
codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_t *fn)
{
- codegen->base_offset = 0;
+ codegen->base_offset = X86_CALL_EIP_STACK_OFFSET;
+
ast_node_t *block_node = fn->block;
fprintf(codegen->out, "" SV_FMT ":\n", SV_ARG(fn->identifier));
@@ -464,8 +495,6 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_
size_t local_size = calculate_fn_local_size(fn->scope);
- // TODO: get the local_size from function scope
-
if (local_size != 0) {
fprintf(codegen->out, " sub $%ld, %%rsp\n", local_size);
}
@@ -475,3 +504,16 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_
codegen_linux_x86_64_emit_block(codegen, &block);
}
+
+static char *
+get_accumulator_reg_for(size_t bytes)
+{
+ if (bytes <= 1) {
+ return "%ah";
+ } else if (bytes <= 2) {
+ return "%ax";
+ } else if (bytes <= 4) {
+ return "%eax";
+ }
+ return "%rax";
+}
diff --git a/src/type.c b/src/type.c
index cbdfbde..64147a2 100644
--- a/src/type.c
+++ b/src/type.c
@@ -21,12 +21,30 @@ type_t
type_from_id(string_view_t id)
{
type_t type = { 0 };
+ if (string_view_eq_to_cstr(id, "u8")) {
+ type.kind = TYPE_PRIMITIVE;
+ type.as_primitive.size = 1;
+ type.as_primitive.kind = TYPE_U8;
+ return type;
+ }
+ if (string_view_eq_to_cstr(id, "u16")) {
+ type.kind = TYPE_PRIMITIVE;
+ type.as_primitive.size = 2;
+ type.as_primitive.kind = TYPE_U16;
+ return type;
+ }
if (string_view_eq_to_cstr(id, "u32")) {
type.kind = TYPE_PRIMITIVE;
type.as_primitive.size = 4;
type.as_primitive.kind = TYPE_U32;
return type;
}
+ if (string_view_eq_to_cstr(id, "u64")) {
+ type.kind = TYPE_PRIMITIVE;
+ type.as_primitive.size = 8;
+ type.as_primitive.kind = TYPE_U64;
+ return type;
+ }
// FIXME: handle user defined types
assert(0 && "unknown type");
diff --git a/src/type.h b/src/type.h
index b431171..1da3a11 100644
--- a/src/type.h
+++ b/src/type.h
@@ -24,7 +24,10 @@ typedef enum
typedef enum
{
- TYPE_U32
+ TYPE_U8,
+ TYPE_U16,
+ TYPE_U32,
+ TYPE_U64
} type_primitive_kind_t;
typedef struct type_primitive
diff --git a/tests/integration/tests/0026_primitive_unsigneds.ol b/tests/integration/tests/0026_primitive_unsigneds.ol
new file mode 100644
index 0000000..25f0f7e
--- /dev/null
+++ b/tests/integration/tests/0026_primitive_unsigneds.ol
@@ -0,0 +1,27 @@
+# Copyright (C) 2024 olang mantainers
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+fn main(): u64 {
+ var a: u8 = 255
+ var b: u16 = 65535
+ var c: u32 = 4294967295
+ var d: u64 = 4294967296
+
+ return a + b + c + d - a - b - c - d
+}
+
+# TEST test_compile(exit_code=0)
+
+# TEST test_run_binary(exit_code=0)
--
2.34.1
next prev parent reply other threads:[~2024-09-22 0:46 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-22 0:46 [PATCH olang v2 0/4] extend unsined integers types (u8, u16, u64) Carlos Maniero
2024-09-22 0:46 ` [PATCH olang v2 1/4] parser: replace type enum to a string view Carlos Maniero
2024-09-22 0:46 ` [PATCH olang v2 2/4] checker: scope: populate symbol's type Carlos Maniero
2024-09-22 0:46 ` [PATCH olang v2 3/4] codegen: fix map symbol list type Carlos Maniero
2024-09-22 0:46 ` Carlos Maniero [this message]
2024-09-22 0:47 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-22 14:15 ` [PATCH olang v2 4/4] codegen: operate mov instructions based on the symbol's type Johnny Richard
2024-09-22 14:19 ` [PATCH olang v2 0/4] extend unsined integers types (u8, u16, u64) 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=20240922004619.518310-5-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