From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id CJFtNYAqBWfOxgAAqHPOHw:P1 (envelope-from ) for ; Tue, 08 Oct 2024 14:50:09 +0200 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id CJFtNYAqBWfOxgAAqHPOHw (envelope-from ) for ; Tue, 08 Oct 2024 14:50:08 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=dA4RBee+; dkim=pass header.d=johnnyrichard.com header.s=key1 header.b=Tt+1e6e4; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; dmarc=pass (policy=quarantine) header.from=johnnyrichard.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1728391808; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:list-id: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=z8uzY8vggfGknI1crLRNixx9+5aQL2l877fYf94wgoA=; b=AxxrYGaDKHCr486/3ykVjHX9pVBgTaUz4A6HpiEUmZKc17GX0y3aZKS/Z3lFzn3CRvgVoH jDLIf78tZppuN2yTi2AjVQVnSofQJXPHhM5r/H1+n4+DyEdXHKMc8jyonHdBvC6bbnOWw5 gRuFqJlFSvcfb5/2BX5bSgPQ2wj1ciVU61PXujoZICrD42yMK/1wwqk/GYoVn/YjutCCIq IeEMU1YNJaoB/ziBBs0aHq3deNksP2gYJxz11DgSnlgfL5n1xQ2BBaknTNvw+FXO2r1/HB WV0s4bg90qF2m4B57I940s4txEpdIttA2mnUYrOPCV0Dxwf5H2/IrbVmx/efKQ== ARC-Seal: i=1; s=key1; d=johnnyrichard.com; t=1728391808; a=rsa-sha256; cv=none; b=F51LgohQNMkbeZwDLn2q7PNrvYEryOait2/HxQ6iJH13Fe5eh5DlxMrfMnUafECnSxCMTL thuoPLQcm20Y9Jckr5hdV5OrAc880CPUyz0MbjpCgrTgjfxyt4xyW/YW03sNUFcodODX8A 09qSaiUw6bTRPA4c3pxltdE3cBCPLZYR2kb9RgsncpXwmpa8iHOJ4ZPAogys+1q1L6jY4Q 6EJfFOfPH8Ll6431WBuT0J1HESfvu6d6/oPQNS48f1Z3HaiIhzfDt1wHftBb8o5PwJQXbQ 9cA2WbnZ8dAWjqNnA9on8xDaGCZDWMF47EjSpy3MFi28mnYt/uOLa3EsGTty4Q== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=dA4RBee+; dkim=pass header.d=johnnyrichard.com header.s=key1 header.b=Tt+1e6e4; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; dmarc=pass (policy=quarantine) header.from=johnnyrichard.com 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 63D9E2402 for ; Tue, 08 Oct 2024 14:50:08 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=fgXSwpmHRgCW+F55FO8HDER4ws+Um2l7O8K9+LFNgWk=; c=simple/simple; d=lists.sr.ht; h=From:To:Cc:Subject:Date:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID; q=dns/txt; s=20240113; t=1728391807; v=1; b=dA4RBee+aSmAPE1eiVvO833T4yD0jNCWv12bywcHK9U7FCIbw9h39dDV+S2KrPRA35386Vso ofQ9hXVz4OPiEn1BGVFL8q2mCvc0Ecs82iPKcgPl1eLxwypsRfYq0YwND6rz9yCFA29FgEI7kAj cZRTVwaSlA8IEfzKPcavvSEMMZhDHlpsCE91ozjQP0HGWSh38oSh8jjBko2Y3FIPatf5zPMWBOp 5VXHQUcOjyjl4IONjovB7JBqG2DF/T3baX/Tz+BlTH7X3h6q+e3mMFWa23ADZwbfnUiFiMbchzu Hmus6Wj6FQ3PvM0Cfu6iS2nnkVQc4c4H63y4UHSG8ArSQ== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id E2AF82015C for ; Tue, 08 Oct 2024 12:50:07 +0000 (UTC) Received: from out-185.mta0.migadu.com (out-185.mta0.migadu.com [91.218.175.185]) by mail-a.sr.ht (Postfix) with ESMTPS id 2BD4420140 for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 08 Oct 2024 12:50:07 +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=1728391806; 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; bh=z8uzY8vggfGknI1crLRNixx9+5aQL2l877fYf94wgoA=; b=Tt+1e6e488WWYCNckUKr3u26XWNEO35MMJc15nu/qH7PzNIGJs3JoYhieQmWcv+Zd+E+r1 dcGhp1HK9kAds3kh8iDAtZFaW9xEXr/dxVr4wh4gvyr352D+w2o85pLrwnqXKtRYigXI07 NKrgJZKpiHn3Joe/ptTKfgpiVBVDjW36jm9FQgpGlMvYwwGp+gk9MYeP2V866eS/q/6HoH JQDSc3w1kWsEBJJyz9QD9Wrh7rAe5MnFMHL3sYRDAWztHjMlDXDZXnEpUhyBurSgtJc2my cstFeZlDXa7V3UMzuaV7Q10dOXR+cW9JfIyWdtadajPWALsTfqG/c5sLSIaaZg== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v1] codegen: x86_64: emit assembly for var assignment stmts Date: Tue, 8 Oct 2024 16:49:34 +0200 Message-ID: <20241008144937.128709-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-Spam-Score: -4.90 X-Spam-Score: -4.90 X-Migadu-Queue-Id: 63D9E2402 X-Migadu-Scanner: mx13.migadu.com X-TUID: LhSQH0OlmtPt Since the codegen for var assignment also fetch offset on stack_offset map and this logic is spread everywhere, I've introduced two new functions to 'put' and 'get' stack_offsets to reduce code duplication. I also reduced the key string key size for stack_offset by replacing '%p' to '%lx' once the second option don't add an extra '0x' prefix. Signed-off-by: Johnny Richard --- src/codegen_linux_x86_64.c | 74 +++++++++++++++++++++++---------- tests/olc/0029_var_assigment.ol | 4 +- 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c index e501634..ebda8eb 100644 --- a/src/codegen_linux_x86_64.c +++ b/src/codegen_linux_x86_64.c @@ -24,7 +24,7 @@ #include "scope.h" #define SYS_exit (60) -#define PTR_HEX_CSTR_SIZE (18 + 1) +#define PTR_HEX_CSTR_SIZE (16 + 1) // The call instruction pushes EIP into stack so the first 8 bytes from stack // must be preserved else the ret instruction will jump to nowere. @@ -66,6 +66,12 @@ codegen_linux_x86_64_emit_start_entrypoint(codegen_x86_64_t *codegen); static void codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_t *fn); +static void +codegen_linux_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); + static size_t type_to_bytes(type_t *type); @@ -154,15 +160,11 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr symbol_t *symbol = scope_lookup(ref.scope, ref.id); assert(symbol); - char symbol_ptr[PTR_HEX_CSTR_SIZE]; - sprintf(symbol_ptr, "%p", (void *)symbol); - - size_t *offset = (size_t *)map_get(codegen->symbols_stack_offset, symbol_ptr); - assert(offset); + size_t offset = codegen_linux_x86_64_get_stack_offset(codegen, symbol); size_t bytes = type_to_bytes(&symbol->type); - fprintf(codegen->out, " mov -%ld(%%rbp), %s\n", *offset, get_reg_for(REG_ACCUMULATOR, bytes)); + fprintf(codegen->out, " mov -%ld(%%rbp), %s\n", offset, get_reg_for(REG_ACCUMULATOR, bytes)); return bytes; } case AST_NODE_FN_CALL: { @@ -559,18 +561,12 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) symbol_t *symbol = scope_lookup(scope, var_def.id); assert(symbol); - char symbol_ptr[PTR_HEX_CSTR_SIZE]; - sprintf(symbol_ptr, "%p", (void *)symbol); + codegen_linux_x86_64_put_stack_offset(codegen, symbol, codegen->base_offset); if (var_def.value) { codegen_linux_x86_64_emit_expression(codegen, var_def.value); } - size_t *offset = arena_alloc(codegen->arena, sizeof(size_t)); - *offset = codegen->base_offset; - - map_put(codegen->symbols_stack_offset, symbol_ptr, offset); - size_t type_size = type_to_bytes(&symbol->type); fprintf(codegen->out, @@ -582,6 +578,24 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) break; } + case AST_NODE_VAR_ASSIGN_STMT: { + ast_var_assign_stmt_t var_assign = node->as_var_assign_stmt; + ast_ref_t ref = var_assign.ref->as_ref; + scope_t *scope = ref.scope; + + symbol_t *symbol = scope_lookup(scope, ref.id); + assert(symbol); + + size_t offset = codegen_linux_x86_64_get_stack_offset(codegen, symbol); + + codegen_linux_x86_64_emit_expression(codegen, var_assign.expr); + + size_t type_size = type_to_bytes(&symbol->type); + fprintf(codegen->out, " mov %s, -%ld(%%rbp)\n", get_reg_for(REG_ACCUMULATOR, type_size), offset); + + break; + } + case AST_NODE_IF_STMT: { ast_if_stmt_t if_stmt = node->as_if_stmt; @@ -689,21 +703,17 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_ ast_fn_param_t *param = item->value; - size_t *offset = arena_alloc(codegen->arena, sizeof(size_t)); - *offset = codegen->base_offset; - symbol_t *symbol = scope_lookup(fn_def->scope, param->id); assert(symbol); - char symbol_ptr[PTR_HEX_CSTR_SIZE]; - sprintf(symbol_ptr, "%p", (void *)symbol); + size_t offset = codegen->base_offset; - map_put(codegen->symbols_stack_offset, symbol_ptr, offset); + codegen_linux_x86_64_put_stack_offset(codegen, symbol, codegen->base_offset); fprintf(codegen->out, " mov %s, -%ld(%%rbp)\n", get_reg_for(x86_call_args[i], symbol->type.as_primitive.size), - *offset); + offset); // FIXME: add offset according to the param size codegen->base_offset += 8; @@ -722,6 +732,28 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_ codegen_linux_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) +{ + + size_t *stack_offset = arena_alloc(codegen->arena, sizeof(size_t)); + *stack_offset = offset; + + char symbol_ptr[PTR_HEX_CSTR_SIZE]; + sprintf(symbol_ptr, "%lx", (uintptr_t)symbol); + + map_put(codegen->symbols_stack_offset, symbol_ptr, stack_offset); +} + +static size_t +codegen_linux_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); + + return *(size_t *)map_get(codegen->symbols_stack_offset, symbol_ptr); +} + static char * get_reg_for(x86_64_register_type_t type, size_t bytes) { diff --git a/tests/olc/0029_var_assigment.ol b/tests/olc/0029_var_assigment.ol index 6c5c46f..0e04fcc 100644 --- a/tests/olc/0029_var_assigment.ol +++ b/tests/olc/0029_var_assigment.ol @@ -19,9 +19,9 @@ fn main(): u32 { return code } -# XTEST test_compile(exit_code=0) +# TEST test_compile(exit_code=0) -# XTEST test_run_binary(exit_code=0) +# TEST test_run_binary(exit_code=0) # TEST test_ast WITH # Translation_Unit base-commit: dbbfdf2acab2b022cc9f0157d4341478d82b8838 -- 2.46.0