From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.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 iI96BHg8EGdzHQAA62LTzQ:P1 (envelope-from ) for ; Thu, 17 Oct 2024 00:21:44 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id iI96BHg8EGdzHQAA62LTzQ (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 0105E83F84 for ; Thu, 17 Oct 2024 00:21:44 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=VBu/dzEZ9oSvt/0kHVDHuQf8ha01RJVBlV2rgryg5ng=; 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=UnOaItJRjjfDkSzXB1JjcviujI8i6QCBXck+xex6TA3AhGFvOgQBqmECoVn/GIAF0yIQaG7R VuKbL3TWKg8bjdnW64KJnBce3/Z0EtzfOIv3mmZL6E58RqEsFtYqL8MVG3mVa++oCWJ0WDhUeNY mrsRhW4wrl+cex+fZrcSM8ytbfEOXQ1IBPWZvpYmxaJ/uaFIwBrq9VbCsKxbf9KOBCEQ8UWALHC 2GoJY/tu/tklLK4JzvwUMabglwbGfmaVFdf1lOziwCXlhaOh3aMcZRdnBIHg5nXG1AGWP6lGubk +Vkt4N8lSmpIhs49+Z6uEU/VNTURlOvSTkZWNE7Mkr3oA== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id C20F3202E8 for ; Wed, 16 Oct 2024 22:21:43 +0000 (UTC) Received: from out-184.mta1.migadu.com (out-184.mta1.migadu.com [95.215.58.184]) by mail-a.sr.ht (Postfix) with ESMTPS id C1A8E2020A 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=FShu8hHW+fFFU3E5wmCAL5HkqkpOOkppStTVFaxXKeo=; b=JixAz/Jz9oHXbb9l+qmY07ZR9rLdeoq7sTAzYLt8WKdNGj57YpLV7UgurrReAZzYNFvYZC ww1KWrMVgIB8Rc5jzbSVdokVpo2AoNaGcFwz2XVWQyrmxjAQWnr+yECSnGj1uq90HEQQOx +aRAAnyxKD4PnnMPoRDh+QpcktFcEbptO63+mWHAF39Hne7SCp5z4DvATT4Ht/sl599/XD 1ITuF7jskOJ6PUkcRB5nDvGeR2qfMzZxyfhrzpSfAxEI4V1s4SLtslXlZ7fk9P/+R0mLgr FXKltJUbHXjxqCIbeKBIjiLKFoWj8EfZCTVOrd8sLFzgv3ROm747qY5VD+sZuA== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v1 3/3] codegen: remove linux from codegen namespace Date: Thu, 17 Oct 2024 02:20:00 +0200 Message-ID: <20241017002102.454851-4-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: 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-Scanner: mx11.migadu.com X-Migadu-Spam-Score: -4.00 X-Spam-Score: -4.00 X-Migadu-Queue-Id: 0105E83F84 X-TUID: zabAIEFUrjCh Since the codegen is OS dependent, we don't need to tie the compilation to linux. Signed-off-by: Johnny Richard --- ...egen_linux_aarch64.c => codegen_aarch64.c} | 16 +- ...egen_linux_aarch64.h => codegen_aarch64.h} | 2 +- ...odegen_linux_x86_64.c => codegen_x86_64.c} | 229 +++++++----------- ...odegen_linux_x86_64.h => codegen_x86_64.h} | 6 +- src/main.c | 14 +- 5 files changed, 109 insertions(+), 158 deletions(-) rename src/{codegen_linux_aarch64.c => codegen_aarch64.c} (88%) rename src/{codegen_linux_aarch64.h => codegen_aarch64.h} (91%) rename src/{codegen_linux_x86_64.c => codegen_x86_64.c} (78%) rename src/{codegen_linux_x86_64.h => codegen_x86_64.h} (82%) diff --git a/src/codegen_linux_aarch64.c b/src/codegen_aarch64.c similarity index 88% rename from src/codegen_linux_aarch64.c rename to src/codegen_aarch64.c index d8187ab..1de238d 100644 --- a/src/codegen_linux_aarch64.c +++ b/src/codegen_aarch64.c @@ -18,7 +18,7 @@ #include #include -#include "codegen_linux_aarch64.h" +#include "codegen_aarch64.h" #include "list.h" #define SYS_exit (93) @@ -36,15 +36,15 @@ */ static void -codegen_linux_aarch64_emit_start_entrypoint(FILE *out); +codegen_aarch64_emit_start_entrypoint(FILE *out); static void -codegen_linux_aarch64_emit_function(FILE *out, ast_fn_definition_t *fn); +codegen_aarch64_emit_function(FILE *out, ast_fn_definition_t *fn); void -codegen_linux_aarch64_emit_translation_unit(FILE *out, ast_node_t *node) +codegen_aarch64_emit_translation_unit(FILE *out, ast_node_t *node) { - codegen_linux_aarch64_emit_start_entrypoint(out); + codegen_aarch64_emit_start_entrypoint(out); assert(node->kind == AST_NODE_TRANSLATION_UNIT); ast_translation_unit_t translation_unit = node->as_translation_unit; @@ -58,7 +58,7 @@ codegen_linux_aarch64_emit_translation_unit(FILE *out, ast_node_t *node) if (decl->kind == AST_NODE_FN_DEF) { ast_fn_definition_t fn = decl->as_fn_def; - codegen_linux_aarch64_emit_function(out, &fn); + codegen_aarch64_emit_function(out, &fn); main_found = main_found || string_view_eq_to_cstr(fn.id, "main"); } else { @@ -72,7 +72,7 @@ codegen_linux_aarch64_emit_translation_unit(FILE *out, ast_node_t *node) } static void -codegen_linux_aarch64_emit_start_entrypoint(FILE *out) +codegen_aarch64_emit_start_entrypoint(FILE *out) { fprintf(out, ".text\n"); fprintf(out, ".globl _start\n\n"); @@ -84,7 +84,7 @@ codegen_linux_aarch64_emit_start_entrypoint(FILE *out) } static void -codegen_linux_aarch64_emit_function(FILE *out, ast_fn_definition_t *fn) +codegen_aarch64_emit_function(FILE *out, ast_fn_definition_t *fn) { ast_node_t *block_node = fn->block; assert(block_node->kind == AST_NODE_BLOCK); diff --git a/src/codegen_linux_aarch64.h b/src/codegen_aarch64.h similarity index 91% rename from src/codegen_linux_aarch64.h rename to src/codegen_aarch64.h index 03e2f46..3f7e0b0 100644 --- a/src/codegen_linux_aarch64.h +++ b/src/codegen_aarch64.h @@ -20,6 +20,6 @@ #include "ast.h" void -codegen_linux_aarch64_emit_translation_unit(FILE *out, ast_node_t *prog); +codegen_aarch64_emit_translation_unit(FILE *out, ast_node_t *prog); #endif /* CODEGEN_LINUX_AARCH64_H */ diff --git a/src/codegen_linux_x86_64.c b/src/codegen_x86_64.c similarity index 78% rename from src/codegen_linux_x86_64.c rename to src/codegen_x86_64.c index 6348dad..6758fc1 100644 --- a/src/codegen_linux_x86_64.c +++ b/src/codegen_x86_64.c @@ -18,7 +18,7 @@ #include #include -#include "codegen_linux_x86_64.h" +#include "codegen_x86_64.h" #include "list.h" #include "map.h" #include "scope.h" @@ -62,20 +62,19 @@ static int x86_call_args[X86_CALL_ARG_SIZE] = { REG_DEST_IDX, REG_SRC_IDX, REG_R8, REG_R9 }; static void -codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, - ast_fn_definition_t *fn); +codegen_x86_64_emit_function(codegen_x86_64_t *codegen, + ast_fn_definition_t *fn); static void -codegen_linux_x86_64_emit_if(codegen_x86_64_t *codegen, ast_if_stmt_t is_stmt); +codegen_x86_64_emit_if(codegen_x86_64_t *codegen, ast_if_stmt_t is_stmt); static void -codegen_linux_x86_64_put_stack_offset(codegen_x86_64_t *codegen, - symbol_t *symbol, - size_t offset); +codegen_x86_64_put_stack_offset(codegen_x86_64_t *codegen, + symbol_t *symbol, + size_t offset); static size_t -codegen_linux_x86_64_get_stack_offset(codegen_x86_64_t *codegen, - symbol_t *symbol); +codegen_x86_64_get_stack_offset(codegen_x86_64_t *codegen, symbol_t *symbol); static size_t type_to_bytes(type_t *type); @@ -84,7 +83,7 @@ static char * get_reg_for(x86_64_register_type_t type, size_t bytes); void -codegen_linux_x86_64_init(codegen_x86_64_t *codegen, arena_t *arena, FILE *out) +codegen_x86_64_init(codegen_x86_64_t *codegen, arena_t *arena, FILE *out) { assert(codegen); assert(arena); @@ -96,8 +95,8 @@ codegen_linux_x86_64_init(codegen_x86_64_t *codegen, arena_t *arena, FILE *out) } void -codegen_linux_x86_64_emit_translation_unit(codegen_x86_64_t *codegen, - ast_node_t *node) +codegen_x86_64_emit_translation_unit(codegen_x86_64_t *codegen, + ast_node_t *node) { codegen->label_index = 0; fprintf(codegen->out, ".text\n"); @@ -112,7 +111,7 @@ codegen_linux_x86_64_emit_translation_unit(codegen_x86_64_t *codegen, if (decl->kind == AST_NODE_FN_DEF) { ast_fn_definition_t fn = decl->as_fn_def; - codegen_linux_x86_64_emit_function(codegen, &fn); + codegen_x86_64_emit_function(codegen, &fn); } else { assert(0 && "translation unit only supports function declarations"); } @@ -122,7 +121,7 @@ codegen_linux_x86_64_emit_translation_unit(codegen_x86_64_t *codegen, } static size_t -codegen_linux_x86_64_get_next_label(codegen_x86_64_t *codegen) +codegen_x86_64_get_next_label(codegen_x86_64_t *codegen) { return ++codegen->label_index; } @@ -130,8 +129,7 @@ codegen_linux_x86_64_get_next_label(codegen_x86_64_t *codegen) typedef size_t size_in_bytes_t; static size_in_bytes_t -codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, - ast_node_t *expr_node) +codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node) { switch (expr_node->kind) { case AST_NODE_LITERAL: { @@ -148,8 +146,7 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, symbol_t *symbol = scope_lookup(ref.scope, ref.id); assert(symbol); - size_t offset = - codegen_linux_x86_64_get_stack_offset(codegen, symbol); + size_t offset = codegen_x86_64_get_stack_offset(codegen, symbol); size_t bytes = type_to_bytes(symbol->type); @@ -173,7 +170,7 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *arg_node = (ast_node_t *)item->value; - codegen_linux_x86_64_emit_expression(codegen, arg_node); + codegen_x86_64_emit_expression(codegen, arg_node); fprintf(codegen->out, " push %s\n", @@ -197,14 +194,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, case AST_BINOP_ADDITION: { fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t rhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); @@ -220,14 +215,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, case AST_BINOP_MULTIPLICATION: { fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t rhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); @@ -242,14 +235,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, case AST_BINOP_DIVISION: { fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t rhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); @@ -265,14 +256,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, case AST_BINOP_REMINDER: { fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t rhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); @@ -292,14 +281,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, case AST_BINOP_SUBTRACTION: { fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t rhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); @@ -315,14 +302,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, case AST_BINOP_CMP_EQ: { fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t rhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); @@ -342,14 +327,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, case AST_BINOP_CMP_LT: { fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t rhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); @@ -369,14 +352,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, case AST_BINOP_CMP_GT: { fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t rhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); @@ -396,14 +377,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, case AST_BINOP_CMP_NEQ: { fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t rhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); @@ -423,14 +402,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, case AST_BINOP_CMP_LEQ: { fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t rhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); @@ -450,14 +427,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, case AST_BINOP_CMP_GEQ: { fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t rhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); @@ -476,13 +451,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, } case AST_BINOP_BITWISE_LSHIFT: { fprintf(codegen->out, " xor %%rax, %%rax\n"); - codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); fprintf(codegen->out, " pop %%rcx\n"); fprintf(codegen->out, @@ -493,13 +467,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, } case AST_BINOP_BITWISE_RSHIFT: { fprintf(codegen->out, " xor %%rax, %%rax\n"); - codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); fprintf(codegen->out, " pop %%rcx\n"); fprintf(codegen->out, @@ -511,14 +484,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, case AST_BINOP_BITWISE_XOR: { fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t rhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); @@ -534,14 +505,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, case AST_BINOP_BITWISE_AND: { fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t rhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); @@ -557,14 +526,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, case AST_BINOP_BITWISE_OR: { fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t rhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); @@ -578,13 +545,11 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, return expr_bytes; } case AST_BINOP_LOGICAL_AND: { - size_t label_exit = - codegen_linux_x86_64_get_next_label(codegen); + size_t label_exit = codegen_x86_64_get_next_label(codegen); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); fprintf(codegen->out, " cmp $0, %s\n", get_reg_for(REG_ACCUMULATOR, lhs_bytes)); @@ -592,8 +557,7 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t rhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " cmp $0, %s\n", get_reg_for(REG_ACCUMULATOR, rhs_bytes)); @@ -604,15 +568,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, return 1; } case AST_BINOP_LOGICAL_OR: { - size_t label_t = - codegen_linux_x86_64_get_next_label(codegen); - size_t label_f = - codegen_linux_x86_64_get_next_label(codegen); + size_t label_t = codegen_x86_64_get_next_label(codegen); + size_t label_f = codegen_x86_64_get_next_label(codegen); fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t lhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); fprintf(codegen->out, " cmp $0, %s\n", get_reg_for(REG_ACCUMULATOR, lhs_bytes)); @@ -620,8 +581,7 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, fprintf(codegen->out, " xor %%rax, %%rax\n"); size_in_bytes_t rhs_bytes = - codegen_linux_x86_64_emit_expression(codegen, - bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); fprintf(codegen->out, " cmp $0, %s\n", get_reg_for(REG_ACCUMULATOR, rhs_bytes)); @@ -642,12 +602,10 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, symbol_t *symbol = scope_lookup(scope, ref.id); assert(symbol); - size_t offset = - codegen_linux_x86_64_get_stack_offset(codegen, - symbol); + size_t offset = codegen_x86_64_get_stack_offset( + codegen, symbol); - codegen_linux_x86_64_emit_expression(codegen, - bin_op.rhs); + codegen_x86_64_emit_expression(codegen, bin_op.rhs); size_t type_size = type_to_bytes(symbol->type); fprintf(codegen->out, @@ -661,14 +619,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, AST_UNARY_DEREFERENCE && "unsupported assignment lhs"); - codegen_linux_x86_64_emit_expression(codegen, - bin_op.lhs); + codegen_x86_64_emit_expression(codegen, bin_op.lhs); fprintf(codegen->out, " push %%rax\n"); - size_t type_size = - codegen_linux_x86_64_emit_expression( - codegen, bin_op.rhs); + size_t type_size = codegen_x86_64_emit_expression( + codegen, bin_op.rhs); fprintf(codegen->out, " pop %%rdx\n"); @@ -698,8 +654,7 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, switch (unary_op.kind) { case AST_UNARY_BITWISE_NOT: { size_in_bytes_t expr_bytes = - codegen_linux_x86_64_emit_expression(codegen, - unary_op.expr); + codegen_x86_64_emit_expression(codegen, unary_op.expr); fprintf(codegen->out, " not %s\n", @@ -717,7 +672,7 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, assert(symbol); size_t offset = - codegen_linux_x86_64_get_stack_offset(codegen, symbol); + codegen_x86_64_get_stack_offset(codegen, symbol); fprintf( codegen->out, " lea -%ld(%%rbp), %%rax\n", offset); @@ -728,8 +683,8 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, assert(unary_op.expr->kind == AST_NODE_REF && "unsupported unary expression for dereference (*)"); - return codegen_linux_x86_64_emit_expression(codegen, - unary_op.expr); + return codegen_x86_64_emit_expression(codegen, + unary_op.expr); } default: { assert(0 && "unsupported unary operation"); @@ -743,7 +698,7 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, } } static void -codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) +codegen_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) { size_t block_offset = codegen->base_offset; size_t nodes_len = list_size(block->nodes); @@ -756,7 +711,7 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) ast_node_t *expr = return_stmt.expr; - codegen_linux_x86_64_emit_expression(codegen, expr); + codegen_x86_64_emit_expression(codegen, expr); fprintf(codegen->out, " mov %%rbp, %%rsp\n"); fprintf(codegen->out, " pop %%rbp\n"); @@ -775,12 +730,11 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) size_t type_size = type_to_bytes(symbol->type); codegen->base_offset += type_size; - codegen_linux_x86_64_put_stack_offset( + codegen_x86_64_put_stack_offset( codegen, symbol, codegen->base_offset); if (var_def.value) { - codegen_linux_x86_64_emit_expression(codegen, - var_def.value); + codegen_x86_64_emit_expression(codegen, var_def.value); } fprintf(codegen->out, @@ -792,12 +746,12 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) } case AST_NODE_BINARY_OP: { - codegen_linux_x86_64_emit_expression(codegen, node); + codegen_x86_64_emit_expression(codegen, node); break; } case AST_NODE_IF_STMT: { - codegen_linux_x86_64_emit_if(codegen, node->as_if_stmt); + codegen_x86_64_emit_if(codegen, node->as_if_stmt); break; } @@ -807,12 +761,11 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) ast_node_t *cond = while_stmt.cond; ast_node_t *then = while_stmt.then; - size_t begin_label = - codegen_linux_x86_64_get_next_label(codegen); - size_t end_label = codegen_linux_x86_64_get_next_label(codegen); + size_t begin_label = codegen_x86_64_get_next_label(codegen); + size_t end_label = codegen_x86_64_get_next_label(codegen); fprintf(codegen->out, ".L%ld:\n", begin_label); - codegen_linux_x86_64_emit_expression(codegen, cond); + codegen_x86_64_emit_expression(codegen, cond); fprintf(codegen->out, " cmp $1, %%rax\n"); fprintf(codegen->out, " jnz .L%ld\n", end_label); @@ -820,7 +773,7 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) "invalid while-then block"); ast_block_t then_block = then->as_block; - codegen_linux_x86_64_emit_block(codegen, &then_block); + codegen_x86_64_emit_block(codegen, &then_block); fprintf(codegen->out, " jmp .L%ld\n", begin_label); fprintf(codegen->out, ".L%ld:\n", end_label); @@ -841,23 +794,23 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) } static void -codegen_linux_x86_64_emit_if(codegen_x86_64_t *codegen, ast_if_stmt_t if_stmt) +codegen_x86_64_emit_if(codegen_x86_64_t *codegen, ast_if_stmt_t if_stmt) { ast_node_t *cond = if_stmt.cond; ast_node_t *then = if_stmt.then; ast_node_t *_else = if_stmt._else; - size_t end_if_label = codegen_linux_x86_64_get_next_label(codegen); - size_t end_else_label = codegen_linux_x86_64_get_next_label(codegen); + size_t end_if_label = codegen_x86_64_get_next_label(codegen); + size_t end_else_label = codegen_x86_64_get_next_label(codegen); - codegen_linux_x86_64_emit_expression(codegen, cond); + codegen_x86_64_emit_expression(codegen, cond); fprintf(codegen->out, " cmp $1, %%rax\n"); fprintf(codegen->out, " jnz .L%ld\n", end_if_label); assert(then->kind == AST_NODE_BLOCK && "invalid if-then block"); ast_block_t then_block = then->as_block; - codegen_linux_x86_64_emit_block(codegen, &then_block); + codegen_x86_64_emit_block(codegen, &then_block); fprintf(codegen->out, " jmp .L%ld\n", end_else_label); fprintf(codegen->out, ".L%ld:\n", end_if_label); @@ -865,10 +818,10 @@ codegen_linux_x86_64_emit_if(codegen_x86_64_t *codegen, ast_if_stmt_t if_stmt) if (_else != NULL) { if (_else->kind == AST_NODE_IF_STMT) { ast_if_stmt_t else_if = _else->as_if_stmt; - codegen_linux_x86_64_emit_if(codegen, else_if); + codegen_x86_64_emit_if(codegen, else_if); } else { ast_block_t else_block = _else->as_block; - codegen_linux_x86_64_emit_block(codegen, &else_block); + codegen_x86_64_emit_block(codegen, &else_block); } } @@ -931,8 +884,8 @@ calculate_fn_local_size(scope_t *scope) } static void -codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, - ast_fn_definition_t *fn_def) +codegen_x86_64_emit_function(codegen_x86_64_t *codegen, + ast_fn_definition_t *fn_def) { fprintf(codegen->out, ".globl " SV_FMT "\n", SV_ARG(fn_def->id)); codegen->base_offset = 0; @@ -957,8 +910,7 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, codegen->base_offset += 8; size_t offset = codegen->base_offset; - codegen_linux_x86_64_put_stack_offset( - codegen, symbol, codegen->base_offset); + codegen_x86_64_put_stack_offset(codegen, symbol, codegen->base_offset); fprintf(codegen->out, " mov %s, -%ld(%%rbp)\n", @@ -978,13 +930,13 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, assert(block_node->kind == AST_NODE_BLOCK); ast_block_t block = block_node->as_block; - codegen_linux_x86_64_emit_block(codegen, &block); + codegen_x86_64_emit_block(codegen, &block); } static void -codegen_linux_x86_64_put_stack_offset(codegen_x86_64_t *codegen, - symbol_t *symbol, - size_t offset) +codegen_x86_64_put_stack_offset(codegen_x86_64_t *codegen, + symbol_t *symbol, + size_t offset) { size_t *stack_offset = arena_alloc(codegen->arena, sizeof(size_t)); @@ -997,8 +949,7 @@ codegen_linux_x86_64_put_stack_offset(codegen_x86_64_t *codegen, } static size_t -codegen_linux_x86_64_get_stack_offset(codegen_x86_64_t *codegen, - symbol_t *symbol) +codegen_x86_64_get_stack_offset(codegen_x86_64_t *codegen, symbol_t *symbol) { char symbol_ptr[PTR_HEX_CSTR_SIZE]; sprintf(symbol_ptr, "%lx", (uintptr_t)symbol); diff --git a/src/codegen_linux_x86_64.h b/src/codegen_x86_64.h similarity index 82% rename from src/codegen_linux_x86_64.h rename to src/codegen_x86_64.h index 7e2841d..d4948e1 100644 --- a/src/codegen_linux_x86_64.h +++ b/src/codegen_x86_64.h @@ -32,10 +32,10 @@ typedef struct codegen_x86_64 } codegen_x86_64_t; void -codegen_linux_x86_64_init(codegen_x86_64_t *codegen, arena_t *arena, FILE *out); +codegen_x86_64_init(codegen_x86_64_t *codegen, arena_t *arena, FILE *out); void -codegen_linux_x86_64_emit_translation_unit(codegen_x86_64_t *codegen, - ast_node_t *prog); +codegen_x86_64_emit_translation_unit(codegen_x86_64_t *codegen, + ast_node_t *prog); #endif /* CODEGEN_X86_64_H */ diff --git a/src/main.c b/src/main.c index 49b2240..685d8ce 100644 --- a/src/main.c +++ b/src/main.c @@ -24,8 +24,8 @@ #include "arena.h" #include "checker.h" #include "cli.h" -#include "codegen_linux_aarch64.h" -#include "codegen_linux_x86_64.h" +#include "codegen_aarch64.h" +#include "codegen_x86_64.h" #include "lexer.h" #include "parser.h" #include "pretty_print_ast.h" @@ -148,15 +148,15 @@ handle_codegen_linux(cli_opts_t *opts) if (!(opts->options & CLI_OPT_ARCH)) { codegen_x86_64_t codegen = { 0 }; - codegen_linux_x86_64_init(&codegen, &arena, out); - codegen_linux_x86_64_emit_translation_unit(&codegen, ast); + codegen_x86_64_init(&codegen, &arena, out); + codegen_x86_64_emit_translation_unit(&codegen, ast); } else { if (strcmp(opts->arch, "x86_64") == 0) { codegen_x86_64_t codegen = { 0 }; - codegen_linux_x86_64_init(&codegen, &arena, out); - codegen_linux_x86_64_emit_translation_unit(&codegen, ast); + codegen_x86_64_init(&codegen, &arena, out); + codegen_x86_64_emit_translation_unit(&codegen, ast); } else if (strcmp(opts->arch, "aarch64") == 0) { - codegen_linux_aarch64_emit_translation_unit(out, ast); + codegen_aarch64_emit_translation_unit(out, ast); } else { fprintf( stderr, "error: architecture '%s' not supported\n", opts->arch); -- 2.46.0