From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:700:3204::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id sP1AFvAT5mUOXAAAbAwnHQ (envelope-from ) for ; Mon, 04 Mar 2024 19:33:20 +0100 Received: from aspmx1.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id sL+kDvAT5mV3RgAAe85BDQ (envelope-from ) for ; Mon, 04 Mar 2024 19:33:20 +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 DBFDBD6DF for ; Mon, 4 Mar 2024 19:33:19 +0100 (CET) DKIM-Signature: a=rsa-sha256; bh=fS+42oE3KyYk1H5d9tVZyErYIutCy7/+pTkl8epjI1U=; 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=1709577198; v=1; b=JQhlkQQz6uMMPB8ZUTEq8F4Z16OOkIfwIT24+lU6R3U3tfcjz3tRZxJ+Xyn7QrqJftcA0SuM bIUirRXzX0+BxUTaYASuMFcyBEg3x2idPy9kS83l7hgGD6mi98r+LRjcXuQ6qbAj+Zjd/cIna0N oWDhm9dSqC3d5qm8t4VdF6H8uQVqdOWWEpZiCl2MJsr9FsR1Vx+w9Q2YyczgbLlM0ifUqKrs+Qs xtlDOnhCt/VzmN7i3Pf52kFlWkfIiGZYi53mva3dKsVzfeIW9pN18mco/AZBwJsTYGJf6iQuNnr 5WKF+nYkOHUFTBs1Kppx2ypSRj77HhR1UDGLy1P47tRxw== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 84BE12015B for ; Mon, 4 Mar 2024 18:33:18 +0000 (UTC) Received: from out-171.mta1.migadu.com (out-171.mta1.migadu.com [IPv6:2001:41d0:203:375::ab]) by mail-a.sr.ht (Postfix) with ESMTPS id DFE4720130 for <~johnnyrichard/olang-devel@lists.sr.ht>; Mon, 4 Mar 2024 18:33:17 +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=1709577197; 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=1XIY/uY3WoHCa3Rxq5VhCkG9Hd52OrMH7pVx/j53u18=; b=Gfaiw6pCl3g25bzFONGyJk85XCWpbCP22YEynJqQLEGG1H57CvA5Vyhzpmrc2t4wa10hSm W8VLKMJaa6FxMo+JTXE/GIYAUYz/4aCoI6+l6q+PknwijvMe9nHzk8qYAOL2/NWEnWqUoJ gHIcJfNFU4j/inhGoVu+gYA3MMRDSezZbY6vCV2IBkOSLZJU/ncHtE4DPhzTsN9OOX9ri0 6YUsh6pAllb9bw2640UCwW+DRq93bbQVS8f+85VY2dW3TSAWC3DR71Hvbp/WgJ+5bUlMeT 7gjKwiVkKg/0pafwTDKUbTQlajZ9RKDypb3HIdAGnoivoskVYkyOSHj1M/aBTw== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v1 3/3] cli: add compilation -o option with --save-temps Date: Mon, 4 Mar 2024 20:23:12 +0100 Message-ID: <20240304193313.961286-4-johnny@johnnyrichard.com> In-Reply-To: <20240304193313.961286-1-johnny@johnnyrichard.com> References: <20240304193313.961286-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-Flow: FLOW_IN X-Migadu-Country: NL X-Migadu-Scanner: mx10.migadu.com X-Migadu-Spam-Score: -4.00 X-Spam-Score: -4.00 X-Migadu-Queue-Id: DBFDBD6DF X-TUID: 4VlgYTfvrr1x 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 --- docs/manpages/0c.md | 13 +++- src/main.c | 180 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 157 insertions(+), 36 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..b27715a 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,20 +39,38 @@ typedef struct cli_args char * cli_args_shift(cli_args_t *args); +typedef enum +{ + CLI_OPT_DUMP_TOKENS = 1 << 1, + CLI_OPT_OUTPUT = 1 << 2, + CLI_OPT_SAVE_TEMPS = 1 << 3 +} cli_opt; + typedef struct cli_opts { - bool dump_tokens; - char *file_path; + uint32_t options; + string_view_t prog; + string_view_t file_path; + string_view_t output_bin; } cli_opts_t; void -print_usage(FILE *stream, char *prog); +print_usage(FILE *stream, string_view_t prog); + +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); string_view_t -read_entire_file(char *file_path, arena_t *arena); +read_entire_file(string_view_t file_path, arena_t *arena); + +void +cli_opts_parse_output(cli_opts_t *opts, cli_args_t *args); int main(int argc, char **argv) @@ -58,28 +78,58 @@ 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.prog = string_view_from_cstr(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; + arg = cli_args_shift(&args); + } else if (strcmp(arg, "--save-temps") == 0) { + opts.options |= CLI_OPT_SAVE_TEMPS; + arg = cli_args_shift(&args); + } else if (strcmp(arg, "-o") == 0) { + cli_opts_parse_output(&opts, &args); + arg = cli_args_shift(&args); } else { - opts.file_path = arg; + opts.file_path = string_view_from_cstr(arg); + arg = cli_args_shift(&args); } } - if (!opts.dump_tokens) { - print_usage(stderr, prog); - return EXIT_FAILURE; + if (opts.options & CLI_OPT_OUTPUT) { + handle_codegen_linux_x86_64(&opts); + return EXIT_SUCCESS; + } + + if (opts.options & CLI_OPT_DUMP_TOKENS) { + handle_dump_tokens(&opts); + return EXIT_SUCCESS; + } + + print_usage(stderr, opts.prog); + 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.chars == NULL) { + print_usage(stderr, opts->prog); + 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,38 +137,82 @@ 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.chars, &token); lexer_next_token(&lexer, &token); } - print_token(opts.file_path, &token); + print_token(opts->file_path.chars, &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.chars == NULL) { + print_usage(stderr, opts->prog); + 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.chars); + + ast_node_t *ast = parser_parse_fn_definition(&parser); + + string_view_t asm_ext = string_view_from_cstr(".s"); + char asm_file[opts->output_bin.size + asm_ext.size + 1]; + memcpy(asm_file, opts->output_bin.chars, opts->output_bin.size); + memcpy(asm_file + opts->output_bin.size, asm_ext.chars, asm_ext.size); + asm_file[opts->output_bin.size + asm_ext.size] = 0; + + 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, string_view_t prog) { - fprintf(stream, "usage: %s --dump-tokens\n", prog); + fprintf(stream, + "Usage: " SV_FMT " [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", + SV_ARG(prog)); } string_view_t -read_entire_file(char *file_path, arena_t *arena) +read_entire_file(string_view_t file_path, arena_t *arena) { - FILE *stream = fopen(file_path, "rb"); + FILE *stream = fopen(file_path.chars, "rb"); if (stream == NULL) { - fprintf(stderr, "Could not open file %s: %s\n", file_path, strerror(errno)); + fprintf(stderr, "error: could not open file " SV_FMT ": %s\n", SV_ARG(file_path), strerror(errno)); exit(EXIT_FAILURE); } @@ -133,7 +227,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, "Could not read file " SV_FMT ": %s\n", SV_ARG(file_path), strerror(errno)); exit(EXIT_FAILURE); } @@ -143,6 +237,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(opts && "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->prog); + 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