public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v1 3/3] codegen: add compiler support to linux aarch64 arch
Date: Tue, 19 Mar 2024 20:57:20 +0100	[thread overview]
Message-ID: <20240319195947.202414-4-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240319195947.202414-1-johnny@johnnyrichard.com>

This patch adds codegen for aarch64.  If you want compile to aarch64
from a x86_64 machine you have to set the --sysroot and --arch argument
correctly as well (you might want to install gcc for the target
architecture and qemu to run the program).

    $ ./olang examples/main_exit.ol -o main --arch aarch64 --sysroot /usr/aarch64-linux-gnu
    $ qemu-aarch64 ./main
    $ echo $? # should return 0

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 src/codegen_linux_aaarch64.c | 94 ++++++++++++++++++++++++++++++++++++
 src/codegen_linux_aarch64.h  | 25 ++++++++++
 src/main.c                   |  3 +-
 3 files changed, 121 insertions(+), 1 deletion(-)
 create mode 100644 src/codegen_linux_aaarch64.c
 create mode 100644 src/codegen_linux_aarch64.h

diff --git a/src/codegen_linux_aaarch64.c b/src/codegen_linux_aaarch64.c
new file mode 100644
index 0000000..657a4f4
--- /dev/null
+++ b/src/codegen_linux_aaarch64.c
@@ -0,0 +1,94 @@
+/*
+ * 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_aarch64.h"
+#include "list.h"
+
+#define SYS_exit (93)
+
+/**
+ *  ───────────────────────────────────────────────────────────────────
+ *  Arch/ABI    Instruction           System  Ret  Ret  Error    Notes
+ *                                    call #  val  val2
+ *  ───────────────────────────────────────────────────────────────────
+ *  arm64       svc #0                w8      x0   x1   -
+ *  ──────────────────────────────────────────────────────────────
+ *  Arch/ABI      arg1  arg2  arg3  arg4  arg5  arg6  arg7  Notes
+ *  ──────────────────────────────────────────────────────────────
+ *  arm64         x0    x1    x2    x3    x4    x5    -
+ */
+
+static void
+codegen_linux_aarch64_emit_start_entrypoint(FILE *out);
+
+static void
+codegen_linux_aarch64_emit_function(FILE *out, ast_fn_definition_t *fn);
+
+void
+codegen_linux_aarch64_emit_program(FILE *out, ast_node_t *node)
+{
+    codegen_linux_aarch64_emit_start_entrypoint(out);
+
+    assert(node->kind == AST_NODE_PROGRAM);
+    ast_program_t program = node->data.as_program;
+
+    ast_fn_definition_t fn = program.fn->data.as_fn_def;
+
+    assert(string_view_eq_to_cstr(fn.identifier, "main"));
+    codegen_linux_aarch64_emit_function(out, &fn);
+}
+
+static void
+codegen_linux_aarch64_emit_start_entrypoint(FILE *out)
+{
+    fprintf(out, ".text\n");
+    fprintf(out, ".globl _start\n\n");
+
+    fprintf(out, "_start:\n");
+    fprintf(out, "    bl main\n");
+    fprintf(out, "    mov w8, #%d\n", SYS_exit);
+    fprintf(out, "    svc #0\n");
+}
+
+static void
+codegen_linux_aarch64_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 x0, #%d\n", exit_code);
+    fprintf(out, "    ret\n");
+}
diff --git a/src/codegen_linux_aarch64.h b/src/codegen_linux_aarch64.h
new file mode 100644
index 0000000..fb88b64
--- /dev/null
+++ b/src/codegen_linux_aarch64.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_LINUX_AARCH64_H
+#define CODEGEN_LINUX_AARCH64_H
+
+#include "ast.h"
+
+void
+codegen_linux_aarch64_emit_program(FILE *out, ast_node_t *prog);
+
+#endif /* CODEGEN_LINUX_AARCH64_H */
diff --git a/src/main.c b/src/main.c
index 3e1b2a3..ff0aaa8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -23,6 +23,7 @@
 
 #include "arena.h"
 #include "cli.h"
+#include "codegen_linux_aarch64.h"
 #include "codegen_linux_x86_64.h"
 #include "lexer.h"
 #include "parser.h"
@@ -121,7 +122,7 @@ handle_codegen_linux(cli_opts_t *opts)
         if (strcmp(opts->arch, "x86_64") == 0) {
             codegen_linux_x86_64_emit_program(out, ast);
         } else if (strcmp(opts->arch, "aarch64") == 0) {
-            assert(false && "Not implemented yet.");
+            codegen_linux_aarch64_emit_program(out, ast);
         } else {
             fprintf(stderr, "error: architecture '%s' not supported\n", opts->arch);
             cli_print_usage(stderr, opts->compiler_path);
-- 
2.44.0


  parent reply	other threads:[~2024-03-19 19:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-19 19:57 [PATCH olang v1 0/3] fe: add compiler support to linux aarch64 Johnny Richard
2024-03-19 19:57 ` [PATCH olang v1 1/3] cli: refactor: move cli parsing to a separted file Johnny Richard
2024-03-23 20:18   ` Carlos Maniero
2024-03-24 16:22     ` Johnny Richard
2024-03-19 19:57 ` [PATCH olang v1 2/3] cli: add --sysroot and --arch options Johnny Richard
2024-03-19 19:57 ` Johnny Richard [this message]
2024-03-19 19:00   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-03-23 20:18   ` [PATCH olang v1 3/3] codegen: add compiler support to linux aarch64 arch 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=20240319195947.202414-4-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