From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v2 1/3] be: create linux-x86_64 gas asm codegen
Date: Tue, 5 Mar 2024 09:44:44 +0100 [thread overview]
Message-ID: <20240305085101.968075-2-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240305085101.968075-1-johnny@johnnyrichard.com>
This patch creates basic functions to generate GAS assembly for
examples/main_exit.0.
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
src/codegen_linux_x86_64.c | 81 ++++++++++++++++++++++++++++++++++++++
src/codegen_linux_x86_64.h | 25 ++++++++++++
2 files changed, 106 insertions(+)
create mode 100644 src/codegen_linux_x86_64.c
create mode 100644 src/codegen_linux_x86_64.h
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
new file mode 100644
index 0000000..3d4b17e
--- /dev/null
+++ b/src/codegen_linux_x86_64.c
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2024 olang maintainers
+ *
+ * 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/>.
+ */
+#include <assert.h>
+#include <stdint.h>
+#include <stdio.h>
+
+#include "codegen_linux_x86_64.h"
+#include "list.h"
+
+#define SYS_exit (60)
+
+static void
+codegen_linux_x86_64_emit_start_entrypoint(FILE *out);
+
+static void
+codegen_linux_x86_64_emit_function(FILE *out, ast_fn_definition_t *fn);
+
+void
+codegen_linux_x86_64_emit_program(FILE *out, ast_node_t *prog)
+{
+ codegen_linux_x86_64_emit_start_entrypoint(out);
+
+ assert(prog->kind == AST_NODE_FN_DEF);
+ ast_fn_definition_t fn = prog->data.as_fn_def;
+
+ assert(string_view_eq_to_cstr(fn.identifier, "main"));
+ codegen_linux_x86_64_emit_function(out, &fn);
+}
+
+static void
+codegen_linux_x86_64_emit_start_entrypoint(FILE *out)
+{
+ fprintf(out, ".text\n");
+ fprintf(out, ".globl _start\n\n");
+
+ fprintf(out, "_start:\n");
+ fprintf(out, " call main\n");
+ fprintf(out, " mov %%eax, %%edi\n");
+ fprintf(out, " mov $%d, %%eax\n", SYS_exit);
+ fprintf(out, " syscall\n");
+}
+
+static void
+codegen_linux_x86_64_emit_function(FILE *out, ast_fn_definition_t *fn)
+{
+ ast_node_t *block_node = fn->block;
+ assert(block_node->kind == AST_NODE_BLOCK);
+ ast_block_t block = block_node->data.as_block;
+
+ assert(list_size(block.nodes) == 1);
+
+ list_item_t *nodes_item = list_get(block.nodes, 0);
+ ast_node_t *return_node = nodes_item->value;
+ assert(return_node->kind == AST_NODE_RETURN_STMT);
+ ast_return_stmt_t return_stmt = return_node->data.as_return_stmt;
+
+ ast_node_t *literal_node = return_stmt.data;
+ assert(literal_node->kind == AST_NODE_LITERAL);
+ ast_literal_t literal_u32 = literal_node->data.as_literal;
+
+ assert(literal_u32.kind == AST_LITERAL_U32);
+ uint32_t exit_code = literal_u32.value.as_u32;
+
+ fprintf(out, "" SV_FMT ":\n", SV_ARG(fn->identifier));
+ fprintf(out, " mov $%d, %%eax\n", exit_code);
+ fprintf(out, " ret\n");
+}
diff --git a/src/codegen_linux_x86_64.h b/src/codegen_linux_x86_64.h
new file mode 100644
index 0000000..2050c91
--- /dev/null
+++ b/src/codegen_linux_x86_64.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2024 olang maintainers
+ *
+ * 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/>.
+ */
+#ifndef CODEGEN_X86_64_H
+#define CODEGEN_X86_64_H
+
+#include "ast.h"
+
+void
+codegen_linux_x86_64_emit_program(FILE *out, ast_node_t *prog);
+
+#endif /* CODEGEN_X86_64_H */
--
2.44.0
next prev parent reply other threads:[~2024-03-05 7:51 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-05 8:44 [PATCH olang v2 0/3] implement assembly linux x86_64 compiler Johnny Richard
2024-03-05 8:44 ` Johnny Richard [this message]
2024-03-05 8:44 ` [PATCH olang v2 2/3] string_view: add function to create from cstr Johnny Richard
2024-03-05 8:44 ` [PATCH olang v2 3/3] cli: add compilation -o option with --save-temps Johnny Richard
2024-03-05 7:51 ` [olang/patches/.build.yml] build failed builds.sr.ht
2024-03-05 11:36 ` [PATCH olang v2 3/3] cli: add compilation -o option with --save-temps Carlos Maniero
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=20240305085101.968075-2-johnny@johnnyrichard.com \
--to=johnny@johnnyrichard.com \
--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