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 2/3] cli: add --sysroot and --arch options
Date: Tue, 19 Mar 2024 20:57:19 +0100	[thread overview]
Message-ID: <20240319195947.202414-3-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240319195947.202414-1-johnny@johnnyrichard.com>

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


  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 ` Johnny Richard [this message]
2024-03-19 19:57 ` [PATCH olang v1 3/3] codegen: add compiler support to linux aarch64 arch 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

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-3-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