From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:303:5f26::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id 8LO0EO7O5mWqCwEAbAwnHQ (envelope-from ) for ; Tue, 05 Mar 2024 08:51:10 +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 gKNFCu7O5mWFjwAAqHPOHw (envelope-from ) for ; Tue, 05 Mar 2024 08:51:10 +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 19883397D0 for ; Tue, 5 Mar 2024 08:51:07 +0100 (CET) DKIM-Signature: a=rsa-sha256; bh=R6HWxy/DAFF599lZ3mjXkh8BBNbfmftWPcuNaxGgU4E=; 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=1709625066; v=1; b=i5W2/Cq9UupveWoG0QGVJj0fe63dKOumvPtUDfkutao84ULoxxWG9bSnI58qe4itP/OU6f7n IACuEAVLC3va7XZS5Y1drlNBCMqcNPjqvZLlSybGxIAPFACNMB9y8Y40+MCdifRZjfv5b/WueqP HAah36SyxRJ43DRSebwmyAN0qU1uJPM/nWB+BIHcAMS5QuhdK/cRVD/DgmL8S3xaZQyUZ5GSm9X Ng2hyujTfMfp2aMEncFwTq+1sNvVByl6xwCIH9w4yGI163p1V9c6O21/EW3l41Yb+TI8pMl/n8u mYfgnO8R/IuK4xwbT6wLCuC3o5/v2nmS5JaRxB2GQ1T5g== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id C1BFE201A1 for ; Tue, 5 Mar 2024 07:51:06 +0000 (UTC) Received: from out-187.mta0.migadu.com (out-187.mta0.migadu.com [91.218.175.187]) by mail-a.sr.ht (Postfix) with ESMTPS id 26C4B20172 for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 5 Mar 2024 07:51:06 +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=1709625065; 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=oS/qF++DhWG6un5sd2C9uT07o0EZGtLu18MW+1HWkC8=; b=wUYiisgLVLvyBED6F+uhQeLc/ucaVqe++voV4ZFclVYy42ybhtVkCH/qX3aqztY1a1V0/z xl6aiXKXltQvJ1VRWY+W2dV9z5LM3f2LEz+73R0GI2ZuDzvz0z6sUhfJ5tk94NQhoP4Eqk Stdd1sejo5dF53Kw2vuCiZ02x3zVxOKxCjCiECswQjzg+wDyvUyUhHBrlDE7267MasDr6c 63p89qe/8x5qHqWAGT2xjR1TA8iF4tFXM1HUYu1eXT9+gEGdbQh6sVv3Jjk3GjpLRkwD1k LnU2k1GSVRZ9JLIDoao2m+Kv1zUY8yRfoI2Xu0+n+u9tP7OAy+c/uB0PUpSmIw== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v2 3/3] cli: add compilation -o option with --save-temps Date: Tue, 5 Mar 2024 09:44:46 +0100 Message-ID: <20240305085101.968075-4-johnny@johnnyrichard.com> In-Reply-To: <20240305085101.968075-1-johnny@johnnyrichard.com> References: <20240305085101.968075-1-johnny@johnnyrichard.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Sourcehut-Patchset-Status: PROPOSED 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: 19883397D0 X-Migadu-Scanner: mx11.migadu.com X-TUID: cM/c+c/JwE4Q In order to compile the program, we are introduction the option -o to the compiler. This implementation is very similar to the gcc one. Since the program is made of a single file, no need to over complicate the compilation with multiple files. The option --save-temps is used to keep files used to create the binary file (.a and .o). By default this option is disabled. WARNING ------- This implementation relies on "as" (GNU Assembler) and the "ld" (GNU linker) command. Make sure you have it install before trying to compile any program. Signed-off-by: Johnny Richard --- v1 -> v2: Change file_path type from string_view_t to cstr. Use first bit for enum cli_opt_t. Change cli_opts_t#prog -> cli_opts_t#compiler_path. Fix assert on args. Refactor creation of cstr asm_file. docs/manpages/0c.md | 13 +++- src/main.c | 164 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 146 insertions(+), 31 deletions(-) diff --git a/docs/manpages/0c.md b/docs/manpages/0c.md index 87a56df..e3d3cfc 100644 --- a/docs/manpages/0c.md +++ b/docs/manpages/0c.md @@ -4,11 +4,14 @@ # NAME -0c - zero langague compiler +0c - zero language compiler # SYNOPSIS -**0c** **----dump-tokens** source.0 +**0c** + source_file + [**----dump-tokens**] + [**--o** output_file [**----save-temps**]] # DESCRIPTION @@ -19,3 +22,9 @@ contains utilities to help the language development. **----dump-tokens** : Display lexical tokens given a soruce.0 code. + +**--o ** +: Compile program into a binary file + +**----save-temps** +: Keep temp files used to compile program diff --git a/src/main.c b/src/main.c index 2b2f12a..e4be5f7 100644 --- a/src/main.c +++ b/src/main.c @@ -22,7 +22,9 @@ #include #include "arena.h" +#include "codegen_linux_x86_64.h" #include "lexer.h" +#include "parser.h" #include "string_view.h" // TODO: find a better solution to define the arena capacity @@ -37,14 +39,29 @@ typedef struct cli_args 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 { - bool dump_tokens; + uint32_t options; + char *compiler_path; char *file_path; + string_view_t output_bin; } cli_opts_t; void -print_usage(FILE *stream, char *prog); +print_usage(FILE *stream, char *compiler_path); + +void +handle_dump_tokens(cli_opts_t *opts); + +void +handle_codegen_linux_x86_64(cli_opts_t *opts); static void print_token(char *file_path, token_t *token); @@ -52,34 +69,64 @@ 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 }; - char *prog = cli_args_shift(&args); + opts.compiler_path = cli_args_shift(&args); - if (argc != 3) { - print_usage(stderr, prog); - return EXIT_FAILURE; - } - - for (char *arg = cli_args_shift(&args); arg != NULL; arg = cli_args_shift(&args)) { + char *arg = cli_args_shift(&args); + while (arg != NULL) { if (strcmp(arg, "--dump-tokens") == 0) { - opts.dump_tokens = true; + 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) { + handle_codegen_linux_x86_64(&opts); + return EXIT_SUCCESS; } - if (!opts.dump_tokens) { - print_usage(stderr, prog); - return EXIT_FAILURE; + if (opts.options & CLI_OPT_DUMP_TOKENS) { + handle_dump_tokens(&opts); + 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); + exit(EXIT_FAILURE); } arena_t arena = arena_new(ARENA_CAPACITY); - string_view_t file_content = read_entire_file(opts.file_path, &arena); + string_view_t file_content = read_entire_file(opts->file_path, &arena); lexer_t lexer = { 0 }; lexer_init(&lexer, file_content); @@ -87,29 +134,70 @@ main(int argc, char **argv) token_t token = { 0 }; lexer_next_token(&lexer, &token); while (token.kind != TOKEN_EOF) { - print_token(opts.file_path, &token); + print_token(opts->file_path, &token); lexer_next_token(&lexer, &token); } - print_token(opts.file_path, &token); + print_token(opts->file_path, &token); - free(file_content.chars); - - return EXIT_SUCCESS; + arena_free(&arena); } -char * -cli_args_shift(cli_args_t *args) +void +handle_codegen_linux_x86_64(cli_opts_t *opts) { - if (args->argc == 0) - return NULL; - --(args->argc); - return *(args->argv)++; + if (opts->file_path == NULL) { + print_usage(stderr, opts->compiler_path); + exit(EXIT_FAILURE); + } + + arena_t arena = arena_new(ARENA_CAPACITY); + lexer_t lexer = { 0 }; + parser_t parser = { 0 }; + + string_view_t file_content = read_entire_file(opts->file_path, &arena); + lexer_init(&lexer, file_content); + parser_init(&parser, &lexer, &arena, opts->file_path); + + ast_node_t *ast = parser_parse_fn_definition(&parser); + + char asm_file[opts->output_bin.size + 3]; + sprintf(asm_file, "" SV_FMT ".s", SV_ARG(opts->output_bin)); + + FILE *out = fopen(asm_file, "w"); + assert(out); + codegen_linux_x86_64_emit_program(out, ast); + fclose(out); + + char command[512]; + sprintf(command, "as %s -o " SV_FMT ".o", 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)); + system(command); + + if (!(opts->options & CLI_OPT_SAVE_TEMPS)) { + char output_file[256]; + + sprintf(output_file, "" SV_FMT ".s", SV_ARG(opts->output_bin)); + remove(output_file); + + sprintf(output_file, "" SV_FMT ".o", SV_ARG(opts->output_bin)); + remove(output_file); + } + + arena_free(&arena); } void -print_usage(FILE *stream, char *prog) +print_usage(FILE *stream, char *compiler_path) { - fprintf(stream, "usage: %s --dump-tokens\n", prog); + 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", + compiler_path); } string_view_t @@ -118,7 +206,7 @@ read_entire_file(char *file_path, arena_t *arena) FILE *stream = fopen(file_path, "rb"); if (stream == NULL) { - fprintf(stderr, "Could not open file %s: %s\n", file_path, strerror(errno)); + fprintf(stderr, "error: could not open file %s: %s\n", file_path, strerror(errno)); exit(EXIT_FAILURE); } @@ -133,7 +221,7 @@ read_entire_file(char *file_path, arena_t *arena) file_content.chars = (char *)arena_alloc(arena, (size_t)file_content.size); if (file_content.chars == NULL) { - fprintf(stderr, "Could not read file %s: %s\n", file_path, strerror(errno)); + fprintf(stderr, "error: could not read file %s: %s\n", file_path, strerror(errno)); exit(EXIT_FAILURE); } @@ -143,6 +231,24 @@ 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