* [PATCH olang v1 1/3] cli: refactor: move cli parsing to a separted file
2024-03-19 19:57 [PATCH olang v1 0/3] fe: add compiler support to linux aarch64 Johnny Richard
@ 2024-03-19 19:57 ` Johnny Richard
2024-03-23 20:18 ` Carlos Maniero
2024-03-19 19:57 ` [PATCH olang v1 2/3] cli: add --sysroot and --arch options Johnny Richard
2024-03-19 19:57 ` [PATCH olang v1 3/3] codegen: add compiler support to linux aarch64 arch Johnny Richard
2 siblings, 1 reply; 8+ messages in thread
From: Johnny Richard @ 2024-03-19 19:57 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
src/cli.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/cli.h | 50 ++++++++++++++++++++++++++++
src/main.c | 97 +++++-------------------------------------------------
3 files changed, 155 insertions(+), 89 deletions(-)
create mode 100644 src/cli.c
create mode 100644 src/cli.h
diff --git a/src/cli.c b/src/cli.c
new file mode 100644
index 0000000..3d1c936
--- /dev/null
+++ b/src/cli.c
@@ -0,0 +1,97 @@
+/*
+ * 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 "cli.h"
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static char *
+cli_args_shift(cli_args_t *args);
+
+static void
+cli_opts_parse_output(cli_opts_t *opts, cli_args_t *args);
+
+cli_opts_t
+cli_parse_args(int argc, char **argv)
+{
+ cli_args_t args = { .argc = argc, .argv = argv };
+ cli_opts_t opts = { 0 };
+
+ opts.compiler_path = cli_args_shift(&args);
+
+ char *arg = cli_args_shift(&args);
+ while (arg != NULL) {
+ if (strcmp(arg, "--dump-tokens") == 0) {
+ opts.options |= CLI_OPT_DUMP_TOKENS;
+ } else if (strcmp(arg, "--save-temps") == 0) {
+ opts.options |= CLI_OPT_SAVE_TEMPS;
+ } else if (strcmp(arg, "-o") == 0) {
+ cli_opts_parse_output(&opts, &args);
+ } else {
+ opts.file_path = arg;
+ }
+ arg = cli_args_shift(&args);
+ }
+
+ if (opts.options & CLI_OPT_OUTPUT || opts.options & CLI_OPT_DUMP_TOKENS) {
+ return opts;
+ }
+
+ cli_print_usage(stderr, opts.compiler_path);
+ exit(EXIT_FAILURE);
+ return opts;
+}
+
+static char *
+cli_args_shift(cli_args_t *args)
+{
+ if (args->argc == 0)
+ return NULL;
+ --(args->argc);
+ return *(args->argv)++;
+}
+
+static void
+cli_opts_parse_output(cli_opts_t *opts, cli_args_t *args)
+{
+ assert(opts && "opts is required");
+ assert(args && "args is required");
+
+ char *output_bin = cli_args_shift(args);
+
+ if (output_bin == NULL) {
+ fprintf(stderr, "error: missing filename after '-o'\n");
+ cli_print_usage(stderr, opts->compiler_path);
+ exit(EXIT_FAILURE);
+ }
+
+ opts->options |= CLI_OPT_OUTPUT;
+ opts->output_bin = string_view_from_cstr(output_bin);
+}
+
+void
+cli_print_usage(FILE *stream, char *compiler_path)
+{
+ fprintf(stream,
+ "Usage: %s [options] file...\n"
+ "Options:\n"
+ " --dump-tokens\t\tDisplay lexer token stream\n"
+ " -o <file>\t\tCompile program into a binary file\n"
+ " --save-temps\t\tKeep temp files used to compile program\n",
+ compiler_path);
+}
diff --git a/src/cli.h b/src/cli.h
new file mode 100644
index 0000000..bd6d6a7
--- /dev/null
+++ b/src/cli.h
@@ -0,0 +1,50 @@
+/*
+ * 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 CLI_H
+#define CLI_H
+#include "string_view.h"
+#include <stdint.h>
+#include <stdio.h>
+
+typedef struct cli_args
+{
+ int argc;
+ char **argv;
+} cli_args_t;
+
+typedef struct cli_opts
+{
+ uint32_t options;
+ char *compiler_path;
+ char *file_path;
+ string_view_t output_bin;
+} cli_opts_t;
+
+typedef enum
+{
+ CLI_OPT_DUMP_TOKENS = 1 << 0,
+ CLI_OPT_OUTPUT = 1 << 1,
+ CLI_OPT_SAVE_TEMPS = 1 << 2
+} cli_opt_t;
+
+cli_opts_t
+cli_parse_args(int argc, char **argv);
+
+void
+cli_print_usage(FILE *stream, char *compiler_path);
+
+#endif /* CLI_H */
diff --git a/src/main.c b/src/main.c
index f6d49f0..f5b12e4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -22,6 +22,7 @@
#include <string.h>
#include "arena.h"
+#include "cli.h"
#include "codegen_linux_x86_64.h"
#include "lexer.h"
#include "parser.h"
@@ -30,33 +31,6 @@
// TODO: find a better solution to define the arena capacity
#define ARENA_CAPACITY (1024 * 1024)
-typedef struct cli_args
-{
- int argc;
- char **argv;
-} cli_args_t;
-
-char *
-cli_args_shift(cli_args_t *args);
-
-typedef enum
-{
- CLI_OPT_DUMP_TOKENS = 1 << 0,
- CLI_OPT_OUTPUT = 1 << 1,
- CLI_OPT_SAVE_TEMPS = 1 << 2
-} cli_opt_t;
-
-typedef struct cli_opts
-{
- uint32_t options;
- char *compiler_path;
- char *file_path;
- string_view_t output_bin;
-} cli_opts_t;
-
-void
-print_usage(FILE *stream, char *compiler_path);
-
void
handle_dump_tokens(cli_opts_t *opts);
@@ -69,29 +43,14 @@ print_token(char *file_path, token_t *token);
string_view_t
read_entire_file(char *file_path, arena_t *arena);
-void
-cli_opts_parse_output(cli_opts_t *opts, cli_args_t *args);
-
int
main(int argc, char **argv)
{
- cli_args_t args = { .argc = argc, .argv = argv };
- cli_opts_t opts = { 0 };
-
- opts.compiler_path = cli_args_shift(&args);
-
- char *arg = cli_args_shift(&args);
- while (arg != NULL) {
- if (strcmp(arg, "--dump-tokens") == 0) {
- opts.options |= CLI_OPT_DUMP_TOKENS;
- } else if (strcmp(arg, "--save-temps") == 0) {
- opts.options |= CLI_OPT_SAVE_TEMPS;
- } else if (strcmp(arg, "-o") == 0) {
- cli_opts_parse_output(&opts, &args);
- } else {
- opts.file_path = arg;
- }
- arg = cli_args_shift(&args);
+ cli_opts_t opts = cli_parse_args(argc, argv);
+
+ if (opts.options & CLI_OPT_DUMP_TOKENS) {
+ handle_dump_tokens(&opts);
+ return EXIT_SUCCESS;
}
if (opts.options & CLI_OPT_OUTPUT) {
@@ -104,24 +63,14 @@ main(int argc, char **argv)
return EXIT_SUCCESS;
}
- print_usage(stderr, opts.compiler_path);
return EXIT_FAILURE;
}
-char *
-cli_args_shift(cli_args_t *args)
-{
- if (args->argc == 0)
- return NULL;
- --(args->argc);
- return *(args->argv)++;
-}
-
void
handle_dump_tokens(cli_opts_t *opts)
{
if (opts->file_path == NULL) {
- print_usage(stderr, opts->compiler_path);
+ cli_print_usage(stderr, opts->compiler_path);
exit(EXIT_FAILURE);
}
@@ -146,7 +95,7 @@ void
handle_codegen_linux_x86_64(cli_opts_t *opts)
{
if (opts->file_path == NULL) {
- print_usage(stderr, opts->compiler_path);
+ cli_print_usage(stderr, opts->compiler_path);
exit(EXIT_FAILURE);
}
@@ -188,18 +137,6 @@ handle_codegen_linux_x86_64(cli_opts_t *opts)
arena_free(&arena);
}
-void
-print_usage(FILE *stream, char *compiler_path)
-{
- fprintf(stream,
- "Usage: %s [options] file...\n"
- "Options:\n"
- " --dump-tokens\t\tDisplay lexer token stream\n"
- " -o <file>\t\tCompile program into a binary file\n"
- " --save-temps\t\tKeep temp files used to compile program\n",
- compiler_path);
-}
-
string_view_t
read_entire_file(char *file_path, arena_t *arena)
{
@@ -231,24 +168,6 @@ read_entire_file(char *file_path, arena_t *arena)
return file_content;
}
-void
-cli_opts_parse_output(cli_opts_t *opts, cli_args_t *args)
-{
- assert(opts && "opts is required");
- assert(args && "args is required");
-
- char *output_bin = cli_args_shift(args);
-
- if (output_bin == NULL) {
- fprintf(stderr, "error: missing filename after '-o'\n");
- print_usage(stderr, opts->compiler_path);
- exit(EXIT_FAILURE);
- }
-
- opts->options |= CLI_OPT_OUTPUT;
- opts->output_bin = string_view_from_cstr(output_bin);
-}
-
static void
print_token(char *file_path, token_t *token)
{
--
2.44.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH olang v1 2/3] cli: add --sysroot and --arch options
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-19 19:57 ` Johnny Richard
2024-03-19 19:57 ` [PATCH olang v1 3/3] codegen: add compiler support to linux aarch64 arch Johnny Richard
2 siblings, 0 replies; 8+ messages in thread
From: Johnny Richard @ 2024-03-19 19:57 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
In order to compile down into different architectures I am introducing
the --arch and --sysroot options.
The --arch option should enable the user to choose between to
"supported" architectures, which is x86_64 and aarch64. The default
architecture is x86_64.
The --sysroot option is from where the compiler will find the gas
assembler and the gnu linker.
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
docs/manpages/olang.md | 12 ++++++---
src/cli.c | 56 +++++++++++++++++++++++++++++++++++++++---
src/cli.h | 6 ++++-
src/main.c | 34 ++++++++++++++++++++-----
4 files changed, 95 insertions(+), 13 deletions(-)
diff --git a/docs/manpages/olang.md b/docs/manpages/olang.md
index c54c597..8ed6726 100644
--- a/docs/manpages/olang.md
+++ b/docs/manpages/olang.md
@@ -11,7 +11,7 @@ olang - O Programming Language compiler
**olang**
source_file
[**----dump-tokens**]
- [**--o** output_file [**----save-temps**]]
+ [**--o** ___output_file___ [**----save-temps**] [**----arch** ___arch___] [**----sysroot** ___dir___]]
# DESCRIPTION
@@ -23,8 +23,14 @@ contains utilities to help the language development.
**----dump-tokens**
: Display lexical tokens given a soruce.0 code.
-**--o <file>**
-: Compile program into a binary file
+**--o** ___file___
+: Compile program into a binary file
**----save-temps**
: Keep temp files used to compile program
+
+**----arch** ___architecture___
+: Binary arch: default to "x86_64", avaliable options ("x86_64" | "aarch64")
+
+**----sysroot** ___dir___
+: System root dir where the GNU Assembler and GNU Linker are located: default to '/'
diff --git a/src/cli.c b/src/cli.c
index 3d1c936..a113bbb 100644
--- a/src/cli.c
+++ b/src/cli.c
@@ -26,6 +26,12 @@ cli_args_shift(cli_args_t *args);
static void
cli_opts_parse_output(cli_opts_t *opts, cli_args_t *args);
+static void
+cli_opts_parse_arch(cli_opts_t *opts, cli_args_t *args);
+
+static void
+cli_opts_parse_sysroot(cli_opts_t *opts, cli_args_t *args);
+
cli_opts_t
cli_parse_args(int argc, char **argv)
{
@@ -42,6 +48,12 @@ cli_parse_args(int argc, char **argv)
opts.options |= CLI_OPT_SAVE_TEMPS;
} else if (strcmp(arg, "-o") == 0) {
cli_opts_parse_output(&opts, &args);
+ } else if (strcmp(arg, "--arch") == 0) {
+ opts.options |= CLI_OPT_ARCH;
+ cli_opts_parse_arch(&opts, &args);
+ } else if (strcmp(arg, "--sysroot") == 0) {
+ opts.options |= CLI_OPT_SYSROOT;
+ cli_opts_parse_sysroot(&opts, &args);
} else {
opts.file_path = arg;
}
@@ -84,14 +96,52 @@ cli_opts_parse_output(cli_opts_t *opts, cli_args_t *args)
opts->output_bin = string_view_from_cstr(output_bin);
}
+static void
+cli_opts_parse_arch(cli_opts_t *opts, cli_args_t *args)
+{
+ assert(opts && "opts is required");
+ assert(args && "args is required");
+
+ char *arch = cli_args_shift(args);
+
+ if (arch == NULL) {
+ fprintf(stderr, "error: missing architecture for arg '--arch': available options (x86_64 | aarch64)\n");
+ cli_print_usage(stderr, opts->compiler_path);
+ exit(EXIT_FAILURE);
+ }
+
+ opts->options |= CLI_OPT_ARCH;
+ opts->arch = arch;
+}
+
+static void
+cli_opts_parse_sysroot(cli_opts_t *opts, cli_args_t *args)
+{
+ assert(opts && "opts is required");
+ assert(args && "args is required");
+
+ char *sysroot = cli_args_shift(args);
+
+ if (sysroot == NULL) {
+ fprintf(stderr, "error: missing sysroot arg '--sysroot'\n");
+ cli_print_usage(stderr, opts->compiler_path);
+ exit(EXIT_FAILURE);
+ }
+
+ opts->options |= CLI_OPT_SYSROOT;
+ opts->sysroot = sysroot;
+}
+
void
cli_print_usage(FILE *stream, char *compiler_path)
{
fprintf(stream,
"Usage: %s [options] file...\n"
"Options:\n"
- " --dump-tokens\t\tDisplay lexer token stream\n"
- " -o <file>\t\tCompile program into a binary file\n"
- " --save-temps\t\tKeep temp files used to compile program\n",
+ " --dump-tokens Display lexer token stream\n"
+ " --arch <arch> Binary arch: default to x86_64 (x86_64 | aarch64)\n"
+ " --sysroot <dir> System root dir where the GNU Assembler and GNU Linker are located: default to '/'\n"
+ " -o <file> Compile program into a binary file\n"
+ " --save-temps Keep temp files used to compile program\n",
compiler_path);
}
diff --git a/src/cli.h b/src/cli.h
index bd6d6a7..b517b2c 100644
--- a/src/cli.h
+++ b/src/cli.h
@@ -29,6 +29,8 @@ typedef struct cli_args
typedef struct cli_opts
{
uint32_t options;
+ char *arch;
+ char *sysroot;
char *compiler_path;
char *file_path;
string_view_t output_bin;
@@ -38,7 +40,9 @@ typedef enum
{
CLI_OPT_DUMP_TOKENS = 1 << 0,
CLI_OPT_OUTPUT = 1 << 1,
- CLI_OPT_SAVE_TEMPS = 1 << 2
+ CLI_OPT_SAVE_TEMPS = 1 << 2,
+ CLI_OPT_ARCH = 1 << 3,
+ CLI_OPT_SYSROOT = 1 << 4
} cli_opt_t;
cli_opts_t
diff --git a/src/main.c b/src/main.c
index f5b12e4..3e1b2a3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -35,7 +35,7 @@ void
handle_dump_tokens(cli_opts_t *opts);
void
-handle_codegen_linux_x86_64(cli_opts_t *opts);
+handle_codegen_linux(cli_opts_t *opts);
static void
print_token(char *file_path, token_t *token);
@@ -54,7 +54,7 @@ main(int argc, char **argv)
}
if (opts.options & CLI_OPT_OUTPUT) {
- handle_codegen_linux_x86_64(&opts);
+ handle_codegen_linux(&opts);
return EXIT_SUCCESS;
}
@@ -92,7 +92,7 @@ handle_dump_tokens(cli_opts_t *opts)
}
void
-handle_codegen_linux_x86_64(cli_opts_t *opts)
+handle_codegen_linux(cli_opts_t *opts)
{
if (opts->file_path == NULL) {
cli_print_usage(stderr, opts->compiler_path);
@@ -114,14 +114,36 @@ handle_codegen_linux_x86_64(cli_opts_t *opts)
FILE *out = fopen(asm_file, "w");
assert(out);
- codegen_linux_x86_64_emit_program(out, ast);
+
+ if (!(opts->options & CLI_OPT_ARCH)) {
+ codegen_linux_x86_64_emit_program(out, ast);
+ } else {
+ 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.");
+ } else {
+ fprintf(stderr, "error: architecture '%s' not supported\n", opts->arch);
+ cli_print_usage(stderr, opts->compiler_path);
+ exit(EXIT_FAILURE);
+ }
+ }
+
fclose(out);
+ if (!(opts->options & CLI_OPT_SYSROOT)) {
+ opts->sysroot = "";
+ }
+
char command[512];
- sprintf(command, "as %s -o " SV_FMT ".o", asm_file, SV_ARG(opts->output_bin));
+ sprintf(command, "%s/bin/as %s -o " SV_FMT ".o", opts->sysroot, asm_file, SV_ARG(opts->output_bin));
system(command);
- sprintf(command, "ld " SV_FMT ".o -o " SV_FMT "", SV_ARG(opts->output_bin), SV_ARG(opts->output_bin));
+ sprintf(command,
+ "%s/bin/ld " SV_FMT ".o -o " SV_FMT "",
+ opts->sysroot,
+ SV_ARG(opts->output_bin),
+ SV_ARG(opts->output_bin));
system(command);
if (!(opts->options & CLI_OPT_SAVE_TEMPS)) {
--
2.44.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH olang v1 3/3] codegen: add compiler support to linux aarch64 arch
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-19 19:57 ` [PATCH olang v1 2/3] cli: add --sysroot and --arch options Johnny Richard
@ 2024-03-19 19:57 ` Johnny Richard
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
2 siblings, 2 replies; 8+ messages in thread
From: Johnny Richard @ 2024-03-19 19:57 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
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
^ permalink raw reply [flat|nested] 8+ messages in thread