From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id oDP5FHg8EGfzaAEAe85BDQ:P1 (envelope-from ) for ; Thu, 17 Oct 2024 00:21:44 +0200 Received: from aspmx1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id oDP5FHg8EGfzaAEAe85BDQ (envelope-from ) for ; Thu, 17 Oct 2024 00:21:44 +0200 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 438731E8CC for ; Thu, 17 Oct 2024 00:21:44 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=MFA/aQeYpazWGyp74BcXodEh6ShCiwwwQgCg4PJUPzw=; 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=1729117303; v=1; b=XJio5eLvP59tvnZZ3Zsh8DcCjAv+Dk32dHgpYtsUMiM0rxdk4uO0ukkO9nnfpDh89v/QOgmM PEZ1QyhqF4eootit7eFK2YF8REr/DMCiGeC73b3RrwZMqhTnrkuLXmLYS2qoCsgAcqmO09aNAHv 3YxQqHa9fDPpuERgC0SEL6kIx2XlxtkakhSIVbKiDiz5ZGFqp4UHFmlKbQf3B/EwHyIZN9zZPyn pb2aSStRzShBGEgyt7q2ckJ1yPA7CjaLhBz9rWWDT1ePQwn/LLkR/xK2iSl/ImPBxuVi5HJi1V/ 28l7IUTubpcB5Tra8VR+QEiKT2giTnVtCiK52L/itGRWA== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 940D6202EF for ; Wed, 16 Oct 2024 22:21:43 +0000 (UTC) Received: from out-186.mta1.migadu.com (out-186.mta1.migadu.com [95.215.58.186]) by mail-a.sr.ht (Postfix) with ESMTPS id 74CC720139 for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 16 Oct 2024 22:21:42 +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=1729117302; 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=gHvuiWNlLxuo5ZM6jYSUdWN3w1mPVbm/6/qmFRkRKAM=; b=b8DM7HfwfKcDqjeJ5xrlX+yKftbax4b4BE5l6KbLS3nftb+M8XM+b2sPqPZgXt0YEe2ojc CnfG24ncjKgdtf3c3kZZynzyPQ1T/y5q4uXop5gg/o8d2RsR3mmbHsQ8Qgoww5hMjek8oJ SSrq92AmHgmNWjfxai/IuXB73tko5zl7MXr/O/ZZq4439GK3xL2IzHZx0kA61xQq46wtq4 hOK1e2KhOxZIaYQulXD3MxsrLXXlSLIDLY6A4v56rhJmlgKRGDTuHoFcjO6pVk2vIAzETi ktNbyxOckkdHiLekyhyCi6DfD6PI7YccWB+52cUXfhkQMghzNMKe5O8TSfMigg== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v1 2/3] cli: add option -c to compile without linking Date: Thu, 17 Oct 2024 02:19:59 +0200 Message-ID: <20241017002102.454851-3-johnny@johnnyrichard.com> In-Reply-To: <20241017002102.454851-1-johnny@johnnyrichard.com> References: <20241017002102.454851-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-Flow: FLOW_IN X-Migadu-Country: NL X-Migadu-Spam-Score: -4.00 X-Spam-Score: -4.00 X-Migadu-Queue-Id: 438731E8CC X-Migadu-Scanner: mx12.migadu.com X-TUID: ozDxoT+CG58x Signed-off-by: Johnny Richard --- docs/man/man1/olc.1 | 6 ++++++ src/cli.c | 3 +++ src/cli.h | 3 ++- src/codegen_linux_x86_64.c | 6 ------ src/main.c | 23 +++++++++++++---------- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/docs/man/man1/olc.1 b/docs/man/man1/olc.1 index 5b35f05..c44df5e 100644 --- a/docs/man/man1/olc.1 +++ b/docs/man/man1/olc.1 @@ -33,6 +33,12 @@ Display AST tree to stdout right after syntax analyzes .BI \-o\ file Compile program into a binary file +.TP +.BI \-c +Compile or assemble the source files, but do not link. The linking stage +simply is not done. The ultimate output is in the form of an object file +for each source file. + .TP .BR \-\-save\-temps Keep temp files used to compile program diff --git a/src/cli.c b/src/cli.c index 2f61acb..afed19b 100644 --- a/src/cli.c +++ b/src/cli.c @@ -53,6 +53,8 @@ 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, "-c") == 0) { + opts.options |= CLI_OPT_COMPILE_ONLY; } else if (strcmp(arg, "--arch") == 0) { opts.options |= CLI_OPT_ARCH; cli_opts_parse_arch(&opts, &args); @@ -153,6 +155,7 @@ cli_print_usage(FILE *stream, char *compiler_path) " --sysroot System root dir where the GNU Assembler and GNU " "Linker are located: default to '/'\n" " -o Compile program into a binary file\n" + " -c Assemble the source files, but do not link\n" " --save-temps Keep temp files used to compile program\n", compiler_path); } diff --git a/src/cli.h b/src/cli.h index 1a93443..ee02adf 100644 --- a/src/cli.h +++ b/src/cli.h @@ -43,7 +43,8 @@ typedef enum CLI_OPT_SAVE_TEMPS = 1 << 2, CLI_OPT_ARCH = 1 << 3, CLI_OPT_SYSROOT = 1 << 4, - CLI_OPT_DUMP_AST = 1 << 5 + CLI_OPT_DUMP_AST = 1 << 5, + CLI_OPT_COMPILE_ONLY = 1 << 6 } cli_opt_t; cli_opts_t diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c index 6d38b53..6348dad 100644 --- a/src/codegen_linux_x86_64.c +++ b/src/codegen_linux_x86_64.c @@ -107,24 +107,18 @@ codegen_linux_x86_64_emit_translation_unit(codegen_x86_64_t *codegen, list_item_t *item = list_head(translation_unit.decls); - bool main_found = false; - while (item != NULL) { ast_node_t *decl = (ast_node_t *)item->value; if (decl->kind == AST_NODE_FN_DEF) { ast_fn_definition_t fn = decl->as_fn_def; codegen_linux_x86_64_emit_function(codegen, &fn); - - main_found = main_found || string_view_eq_to_cstr(fn.id, "main"); } else { assert(0 && "translation unit only supports function declarations"); } item = list_next(item); } - - assert(main_found && "main function is required."); } static size_t diff --git a/src/main.c b/src/main.c index c68fd48..49b2240 100644 --- a/src/main.c +++ b/src/main.c @@ -173,10 +173,11 @@ handle_codegen_linux(cli_opts_t *opts) char command[512]; sprintf(command, - "%s/bin/as %s -o " SV_FMT ".o", + "%s/bin/as %s -o " SV_FMT "%s", opts->sysroot, asm_file, - SV_ARG(opts->output_bin)); + SV_ARG(opts->output_bin), + opts->options & CLI_OPT_COMPILE_ONLY ? "" : ".o"); int exit_code = system(command); @@ -184,16 +185,18 @@ handle_codegen_linux(cli_opts_t *opts) exit(exit_code); } - sprintf(command, - "%s/bin/cc " SV_FMT ".o -o " SV_FMT, - opts->sysroot, - SV_ARG(opts->output_bin), - SV_ARG(opts->output_bin)); + if (!(opts->options & CLI_OPT_COMPILE_ONLY)) { + sprintf(command, + "%s/bin/cc " SV_FMT ".o -o " SV_FMT, + opts->sysroot, + SV_ARG(opts->output_bin), + SV_ARG(opts->output_bin)); - exit_code = system(command); + exit_code = system(command); - if (exit_code != 0) { - exit(exit_code); + if (exit_code != 0) { + exit(exit_code); + } } if (!(opts->options & CLI_OPT_SAVE_TEMPS)) { -- 2.46.0