From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.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 eFayELQd7mZNRAAA62LTzQ:P1 (envelope-from ) for ; Sat, 21 Sep 2024 03:13:24 +0200 Received: from aspmx1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id eFayELQd7mZNRAAA62LTzQ (envelope-from ) for ; Sat, 21 Sep 2024 03:13:24 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=DiHFrbBF; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=GQiy423T; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; arc=pass ("mailchannels.net:s=arc-2022:i=1"); dmarc=pass (policy=none) header.from=maniero.me ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1726881204; 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: in-reply-to:in-reply-to:references:references:list-id: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=YB+rVf3fmUgFXMIXwFbQr7z+xCZGNy71mDvnXJngkRE=; b=szSiM02DFRWusXK5igXSh7Ze/2Ht7vggA+iZAd+ixCq+VkF1ejid+aXJuShKYV3CE2nZh5 aev1LLnc9DqtW7maaJ5b3jrEC2DpBXUD28OMu1NM9aV+igUbLkX8ajjNJeRsBdc6LwsUpj h/u/hIDaer3HXGfZ2ujOSaTtyr+TEbHMDkFQmsTUykVljyzIz1uN71XbquhJrHn3sJLrTs e0kalsadCHVTUmXRJs2lxNE9TQhO07xFFfGFRTk1CD0TNFoHtFWMfeTLsvFydPVEAWQS/e gIhfDpzygfjatRQ0OAbpsqQ/8iqekRo/2jLAYWspfLNXaslYdkhdu6r0TXEW3A== ARC-Seal: i=2; s=key1; d=johnnyrichard.com; t=1726881204; a=rsa-sha256; cv=pass; b=uSdsgiwoA/zFtUhx8M6PuCiUoChkRgBZQyi0ifuc+V/eLVMqw7r4GKQns/hxPp2Ca/mVjG MdsrmAL6bzoM8BvMI9g6mdtjGb8Tyv6ASfSjLSDOiSaqYjlZV9DMGvTNtKeeztxRIfRj+Z dybNumVn9QsELIhilhO1nDB4vMsc0ChQUm63LCqu7+NXoR4Cq7NM6mwR6M0MOey2G8Bv8x W1As3iegUhzM/EwZpmDA4lEwiyfS0dugxsEGBBtI13m8EhgNeesi7fIDW97LS3lyt2af92 Ws+VtccJasbZ90gjSpGbXJTLO33nchdJv/u90W2YUKhRoMSmhiteiKQCk7xWOQ== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=DiHFrbBF; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=GQiy423T; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; arc=pass ("mailchannels.net:s=arc-2022:i=1"); dmarc=pass (policy=none) header.from=maniero.me 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 149D47A147 for ; Sat, 21 Sep 2024 03:13:24 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=42Pslnt8p7flyGvZhNVz3oS6wmA+c1scfosaGP1glU4=; c=simple/simple; d=lists.sr.ht; h=From:To:Cc:Subject:In-Reply-To:References:Date:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID; q=dns/txt; s=20240113; t=1726881203; v=1; b=DiHFrbBF9xQZweQB0DBpGAzpWC+kzI4WpenvQnBygbWtqZm+4cM0Afup8LFhWGim5MDqQ4np DzURPHZYSfybEVIrvcvPnX/acqGY7gduGCqktwNoVUusObFAgoAw7YajHfmKjkEoJDfGCmLv8KE 4M2poJKoVMEk70s7VcpnWFHVhGME4yYhFs+ESWM26iWXcFAvlTVZ5bMFKpuuacMxAcTE8Qt5O7M wtxHcWo2xlSwzRyOKhx08Ixbm8ITI/F98nfRzmh36OTN681J5E400/Osy3G9TfCW20WrNwiqCUt tJpyaks2Aogz2y7rA5BXz6zcmzpX9wAVfesEM+Kk4s91g== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 90D4A2028C for ; Sat, 21 Sep 2024 01:13:23 +0000 (UTC) Received: from weasel.tulip.relay.mailchannels.net (weasel.tulip.relay.mailchannels.net [23.83.218.247]) by mail-a.sr.ht (Postfix) with ESMTPS id 0681320278 for <~johnnyrichard/olang-devel@lists.sr.ht>; Sat, 21 Sep 2024 01:13:21 +0000 (UTC) X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 6C921908719 for <~johnnyrichard/olang-devel@lists.sr.ht>; Sat, 21 Sep 2024 01:13:20 +0000 (UTC) Received: from nl-srv-smtpout1.hostinger.io (unknown [127.0.0.6]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id D5D019083B0 for <~johnnyrichard/olang-devel@lists.sr.ht>; Sat, 21 Sep 2024 01:13:19 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1726881200; a=rsa-sha256; cv=none; b=Cgpb6NDt+LYE6+FnywtwlspIIikLiukpm1faNpuxM8rvQO7OhWQu4PtoWgSn2vH1smlsAr L9yPwlXVWDbQ+HZHoDPdssHYUQOrr4/7ByNFDvXJfQ86783j71At8uPueldKkEeepeRDS1 /HGn+uoQ/E8dn4GxNP8f3MZr5vovGa3/65KMcR7zqRAv7JMvNfIsNTS5L3Hl96gYJghssi NAy1+JGGZpH4LeKbS54VFRdIJw50mxQyA5vME9WcgvGhR2nek4uVlMe6k3uOONk6IVjltj zknZPdhYWsqujMcC4L1XcEfuUpQdEQoKxt1fw0gO4R5GfCYMXZb5sxbVEMYvaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1726881200; 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:dkim-signature; bh=YB+rVf3fmUgFXMIXwFbQr7z+xCZGNy71mDvnXJngkRE=; b=qIebHbPbLzuDn24tw7BkGLe4oMbAiU3dxGAIygZFU3z3i13mM7Ta2fdDqhdf2ivg/Mn4a+ avUcKnlP+05QuiM+PYHL7LSAuyKyENReEMPh0xV+JUiqxXd9Hi83Vhbc7dbBZPc8ftSxO/ PIourob9cWsblNlhSwDlKexzqlHzabpFog55GZ8JQXVY56if3EDXT5iekSxOpVNrgYpSZm mxApPCj+nsaJsdtwNzDwbj1iWTrBYTbFrGGfm03zWNB3EMCFX2iP9pzp6su4p+ZlzhcsRk VU6xpK3vO5jYP6AE15IigHDsIYFHSLurPS45woGQKc5jvCJRfhubHQXOeIRYCA== ARC-Authentication-Results: i=1; rspamd-b5ccff48b-bxtmn; auth=pass smtp.auth=hostingeremail smtp.mailfrom=carlos@maniero.me X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me X-MC-Relay: Neutral X-MailChannels-SenderId: hostingeremail|x-authuser|carlos@maniero.me X-MailChannels-Auth-Id: hostingeremail X-Bottle-Wide-Eyed: 1fea79ea4cfd2bc7_1726881200342_476999513 X-MC-Loop-Signature: 1726881200342:461011679 X-MC-Ingress-Time: 1726881200342 Received: from nl-srv-smtpout1.hostinger.io (nl-srv-smtpout1.hostinger.io [145.14.150.87]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.105.111.227 (trex/7.0.2); Sat, 21 Sep 2024 01:13:20 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1726881198; 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=YB+rVf3fmUgFXMIXwFbQr7z+xCZGNy71mDvnXJngkRE=; b=GQiy423TpWkqyztcoB5AoCU0KHMsI+QDeFLjnBlMhrglGWPmEhM8qqiNgOD/BvkXUKH2Sc ZC9RbJOol0rhxgU089znWsPRVs0mEPVM8EEEPiWVl48igjjAnav/0NyIbpRGIROPk7WR1U AkzjRU5PMNf+si+OumpEGbBFeF2FrUCFi1K9pKKRPFabsj1+GnghFFJEW+Q4jgbs2SYnCB ks5TFX2okTi04v3RDxKA77KpxFHL/PBjj70p8+ntRY2hvGGDQ++KiXrHycb3f5fl/GqwcQ HO/dc/2YHcSaPtWkkZfxybtSZoydef6cWFpxs096nT0rKu0vHz/WqrBaWl8jIQ== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero , Johnny Richard Subject: [PATCH olang 5/5] codegen: preserve function's variable stack location Message-Id: <20240921011234.191248-6-carlos@maniero.me> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240921011234.191248-1-carlos@maniero.me> References: <20240921011234.191248-1-carlos@maniero.me> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Date: Sat, 21 Sep 2024 01:13:17 +0000 (UTC) X-CM-Envelope: MS4xfMLEAGLDLKFhG47oAh6OD0MccBjmVwlL61WlEViDYzMj1t3zyxjxHKkH59QattxJ0rapxpLTVyFvqxTTG5LB6iTukIt/Mq1hSro0afaBkcdt+GGINcYS e41yhhQ5cK9aehZYMjYqwvMlGtSR42T+k81KEqlG2SmeH5aS+LIadqhlckwT2o4EmwMhUtyLkYIjoAS59cpb9lFuFbXoKP6m+3aSL7l/L85vUotVGcCkYpQy cl96mDbXrYGE+kR9Ce7fQ9kHsxixrnDpfyFzVnfpmwE= X-CM-Analysis: v=2.4 cv=c5X5Qg9l c=1 sm=1 tr=0 ts=66ee1dae a=WwxFCuf3mf1fs3oSi6/dng==:117 a=WwxFCuf3mf1fs3oSi6/dng==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=jziKOLAnAAAA:8 a=mDV3o1hIAAAA:8 a=-7ccb9dv-vah0gjtVvwA:9 a=1ljl-wuu_96014yfT5Vk:22 a=BXDaF_L80NY05PYiAFlV:22 X-AuthUser: carlos@maniero.me 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: mx12.migadu.com X-Migadu-Spam-Score: -5.13 X-Migadu-Queue-Id: 149D47A147 X-Spam-Score: -5.13 X-TUID: k2VWeFn4y1Sw Since the x86 expressions uses stack instructions (push/pop), a prologue is required to ensure the space reserved for variables are not modified by these instructions. Signed-off-by: Carlos Maniero Co-authored-by: Johnny Richard --- src/codegen_linux_x86_64.c | 43 +++++++++++++++++++ .../tests/0025_var_definition_nested.ol | 35 +++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 tests/integration/tests/0025_var_definition_nested.ol diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c index 640c9fb..3ce11a7 100644 --- a/src/codegen_linux_x86_64.c +++ b/src/codegen_linux_x86_64.c @@ -346,6 +346,7 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) codegen_linux_x86_64_emit_expression(codegen, expr); + fprintf(codegen->out, " mov %%rbp, %%rsp\n"); fprintf(codegen->out, " ret\n"); break; @@ -418,6 +419,40 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) codegen->base_offset = block_offset; } +static size_t +calculate_fn_local_size(scope_t *scope) +{ + assert(scope); + + size_t local_size = 0; + + map_kv_t kvs[scope->symbols->size]; + + map_get_kvs(scope->symbols, (map_kv_t **)kvs); + + for (size_t i = 0; i < scope->symbols->size; ++i) { + // FIXME: symbols must have their types. Since we just have 8bytes + // variables it is hard coded. + local_size += 8; + } + + size_t max_child_local_size = 0; + + list_item_t *item = list_head(scope->children); + + while (item != NULL) { + size_t child_local_size = calculate_fn_local_size((scope_t *)item->value); + + if (child_local_size > max_child_local_size) { + max_child_local_size = child_local_size; + } + + item = list_next(item); + } + + return local_size + max_child_local_size; +} + static void codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_t *fn) { @@ -427,6 +462,14 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_ fprintf(codegen->out, " mov %%rsp, %%rbp\n"); + size_t local_size = calculate_fn_local_size(fn->scope); + + // TODO: get the local_size from function scope + + if (local_size != 0) { + fprintf(codegen->out, " sub $%ld, %%rsp\n", local_size); + } + assert(block_node->kind == AST_NODE_BLOCK); ast_block_t block = block_node->as_block; diff --git a/tests/integration/tests/0025_var_definition_nested.ol b/tests/integration/tests/0025_var_definition_nested.ol new file mode 100644 index 0000000..fdbe903 --- /dev/null +++ b/tests/integration/tests/0025_var_definition_nested.ol @@ -0,0 +1,35 @@ +# Copyright (C) 2024 olang mantainers +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +fn main(): u32 { + var a: u32 = 1 + + if a == 1 { + var b: u32 = 43 + var c: u32 = 2 + + return a + b - c + } else { + var b: u32 = 41 + + return a + b + } + + return a +} + +# TEST test_compile(exit_code=0) + +# TEST test_run_binary(exit_code=42) -- 2.34.1