From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id GAB2CLLg+WVnQAAAqHPOHw:P1 (envelope-from ) for ; Tue, 19 Mar 2024 20:00:02 +0100 Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id GAB2CLLg+WVnQAAAqHPOHw (envelope-from ) for ; Tue, 19 Mar 2024 20:00:02 +0100 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; none Received: from mail-a.sr.ht (mail-a.sr.ht [46.23.81.152]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 10C1F59C49 for ; Tue, 19 Mar 2024 19:59:58 +0100 (CET) DKIM-Signature: a=rsa-sha256; bh=XsrB9z/yWvQK4I/cm8nLReVYimbiT9qqImCcw2oBRSE=; c=simple/simple; d=lists.sr.ht; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID; q=dns/txt; s=20240113; t=1710874798; v=1; b=kSltMgaDxpyHR1b1lFc4E8UxlB3O7L7LFFKUEuqcSnsBoO+YAf1PZotqBZzsDQDjtQWWhl0U saafLZ+6NoX6Y6t16UXv7cuuIuFrru4EC0Z80yNyvam+1FKS2TQ28OhFljiEjq2WulZ3fGwWR2n bKAlM0fM+6LKcQ0TlLOGS7tqxqxxbYSsiP+hcAkIzI1Zku3Q893tKgtkuaz+ihg4jAlSEAY233q GZ79QuxLuAqN+YUlX1UkVgfQ0Qtvt1szOcNSv+JhmIDfIhmXK+A42j8kQtQaRIgLJxvZnzOrPep EU2PVDL3E0S1eFembdSsPmScAMCLEiqBI0QzJkWbR7L8w== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 5D96320108 for ; Tue, 19 Mar 2024 18:59:58 +0000 (UTC) Received: from out-183.mta1.migadu.com (out-183.mta1.migadu.com [95.215.58.183]) by mail-a.sr.ht (Postfix) with ESMTPS id C7E18200DB for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 19 Mar 2024 18:59:57 +0000 (UTC) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1710874797; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jid7EQWui1QHmFh2SDmFOtSNlF424CONMVsn69vspe4=; b=ioSZyY2UUa2fflw1D0mksds854DhSbZoMoQo8m7HIQDD7jSt8xalBSZwUkVO1N46WYR39J aZqCVA9r4GbMmuN7AM1FqPSpuE1FfuWfkvAdk5WXQ//wA4yZ6VGVVHgXEyq3F3VFpGXTjH Gi7H8BRH4TcLOONEwALy1UJBtu2QlUcPmNOYZuyy7S56ZcidhFCYrADRy9vXq1gBzwLO2+ 1+g4NlhtZaDC2B62VeLCbGXM9JJCKFS/cRCJCr9VEIthhiLowzXKseK7a73ASRZ7ur4ewl V1hT4Q68LK32qXoHaGeSKDoMIa/E44huoneS7UU033gNRITNQdHIgta8lO1MTA== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v1 2/3] cli: add --sysroot and --arch options Date: Tue, 19 Mar 2024 20:57:19 +0100 Message-ID: <20240319195947.202414-3-johnny@johnnyrichard.com> In-Reply-To: <20240319195947.202414-1-johnny@johnnyrichard.com> References: <20240319195947.202414-1-johnny@johnnyrichard.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Sourcehut-Patchset-Status: UNKNOWN List-Unsubscribe: List-Subscribe: List-Archive: Archived-At: List-Post: List-ID: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel.lists.sr.ht> Sender: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel@lists.sr.ht> X-Migadu-Country: NL X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -4.00 X-Spam-Score: -4.00 X-Migadu-Queue-Id: 10C1F59C49 X-Migadu-Scanner: mx11.migadu.com X-TUID: RgUC35yL3T0+ 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 --- 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 ** -: 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 \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 Binary arch: default to x86_64 (x86_64 | aarch64)\n" + " --sysroot System root dir where the GNU Assembler and GNU Linker are located: default to '/'\n" + " -o 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