From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms1.migadu.com with LMTPS id iEnBCIddGGeiKgEAe85BDQ:P1 (envelope-from ) for ; Wed, 23 Oct 2024 04:20:55 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id iEnBCIddGGeiKgEAe85BDQ (envelope-from ) for ; Wed, 23 Oct 2024 04:20:55 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=d+fC6pCg; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b="oiY1vZL/"; 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=none) header.from=maniero.me; arc=pass ("mailchannels.net:s=arc-2022:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1729650055; 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=KhJOKdWgjbCRyp4r3+emVo7J8g9gazCKrnf3e3s8oog=; b=ncp8TQ5dnmbPYaLVzcXRFV0WYEjrPD40ZFAr6bGv9Xepzs7mE1vikz2yf+82BUdkk14dd8 HjOB4IzZKtmYwX1YsesXh1uwTIGK4rZn7v+gFWZF0b319r6WiT9m+SZhSdDoO2RPXpA2Sv Cz4TILHPC/vsF1hU7LC/U5joM4VvgnpUjAGlQSPzwloGkZJVV3g8Q8gn0Rm6VPj6iVcUPB /+4rP00cRzIPeW3DrVgLLtp2UKXO2W12RpacxXaFzw+V8fybVVXyeHMgNjMyKx8KPA99bB qjHFCoMZxS7x5oUy5IgdjlGGfzcV1nX9BXd55gd6NdyfMUlYMQRbyWDWilP2bA== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=d+fC6pCg; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b="oiY1vZL/"; 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=none) header.from=maniero.me; arc=pass ("mailchannels.net:s=arc-2022:i=1") ARC-Seal: i=2; s=key1; d=johnnyrichard.com; t=1729650055; a=rsa-sha256; cv=pass; b=2ODvxtydRFM3fdMfPrjt+tvjnpkk5xpg0nw7AqDhKXHKYlxfRkDpeDpgNm7T2RNR/OAcFH C7+oco5AqVFN7YJL24Il3lYRfnZfIegRS9C2CkS/SWXz6vLR7cvLevo+6MKDr/1M/ruZ+Q nkk/R+l3pavqUG7VeLbBM8CkrCgn/IypDa2nBhZk2Faa+d/l5+lA2hadNntFozD1/LbeAy d/1/DAVFyaAEoOJV0jTOI7rIVOrk+r5ymuiJqWIynKmXtu/1sOgKUcoiHnW2vfpJeOSdR7 i9A66oWhBvMSc7dPPPUpwor1dfnuZMpXLgdLx0xUorEPmy3udX9e/WF878WYlQ== 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 06F92819D9 for ; Wed, 23 Oct 2024 04:20:51 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=kWJDGTUyJI+xP3LOeVdXoyAgjBLge+rZeYz0pnEqHx4=; 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=1729650051; v=1; b=d+fC6pCgTMPBq8w+uI6Uwfz/eUClmqUXKBglfWuZqhK/nCwOjpyRdpBV+w7H23+ghQXBBXVK FcbEJQXzZvVOENV+eK13TxVkmeRmIuwVjBtO0z74nGLBhKjDmwtaRD276TJtFfshwTfzYpIPF0Y 4wzQcVADaMtbYoAeXY2++4Yt47u34TGb6wOwGw8ox8iM++Qlk2u6abbUx/o7couCty8Pu2iT8zE GZMZ62B1RRD/ytP2w2lACWmlFLCHZMdVYeO0J8EBR41z1F04j+b3sGR5xdSbLIq0+VaM2/2jc8Z m5kzVwTEOr9FhXu0+g657G2qWHZjOn3ujA9Q5AVYiu5pg== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id BF2A4202C2 for ; Wed, 23 Oct 2024 02:20:51 +0000 (UTC) Received: from aye.elm.relay.mailchannels.net (aye.elm.relay.mailchannels.net [23.83.212.6]) by mail-a.sr.ht (Postfix) with ESMTPS id C6D8D202B1 for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 23 Oct 2024 02:20:50 +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 57E0AA512B for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 23 Oct 2024 02:20:49 +0000 (UTC) Received: from uk-fast-smtpout10.hostinger.io (trex-3.trex.outbound.svc.cluster.local [100.103.25.208]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id 9AD7EA405C for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 23 Oct 2024 02:20:48 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1729650049; a=rsa-sha256; cv=none; b=OJbEVxYK3V2cqpTh6C8U2cfn/D7pE+7034EGqyyKol5deRgmGjUq3O+KMHklZRIHUJ+dAl eK8lic4y5i1UNMcWUJP6E19TOkD1D7KaorlGZPthiLPZaEIOVhpfYSJDqIUNGj6dWzNFwo CylxYGWLxz+kWoCr24aL97OyUQRXWk+YPndjwFRS/DKG+ZfCEAJVGI4bJKnEiVnKTXvKKQ yiqEi+IHjyccyRPKFZDfqR1yW2HveAGbNeKx9x+Fu/rus8oChRrGK+MQggpBXqAXCMRRJc J+tWGplv+p1A4XHbeRVLX2vMLmzbwJbQykuzgs78+IHtdHRgcim1viHAOHlssg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1729650049; 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=KhJOKdWgjbCRyp4r3+emVo7J8g9gazCKrnf3e3s8oog=; b=YznezHPBna6zlm0ezcTyuD5KUCNt5tV5YVlSBk+SOgzvPz5sa/lIqAv+b7WpyAS6B+WzLg eo8g/9cfT9YLu5WugfiVRfD9EQz9MIpfNPy/9CzuroTcez2veoOyEiwCScrZKSUDd1Yihu y8+3vfGTjpVSsvS6jdVbrqscsddFl4syr0Hj3AZxxye5gRKu+YV+YUfo9YPRQXywxT8BU9 0gbDR9SyxygxcHptx+o151WOpJcMWbGzPpZb5Vf3Eiw4ccQhqX2XLfnXDPcmzsjBzkS428 m0RpMBlJ4CMUTUaxT9tjsdkyS8DoCc4qfZpMxP3opgGNYOApMRSnh77O5kl5Ew== ARC-Authentication-Results: i=1; rspamd-5bbb596db8-sksfx; 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-Minister-Tart: 546e51524dcf4b44_1729650049266_2837500156 X-MC-Loop-Signature: 1729650049266:2869650293 X-MC-Ingress-Time: 1729650049266 Received: from uk-fast-smtpout10.hostinger.io (uk-fast-smtpout10.hostinger.io [145.14.155.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.103.25.208 (trex/7.0.2); Wed, 23 Oct 2024 02:20:49 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1729650046; 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=KhJOKdWgjbCRyp4r3+emVo7J8g9gazCKrnf3e3s8oog=; b=oiY1vZL/ChUkb4s355V0A2UxBcqEDDnVcPf2cI/uVp4UMzMthDEcpK9sKAD/9xVQeFdCcp PmYaD3RMrkzJWIB8+QVFprXoARQryCNrk3o/MHb/gC/7Y+oG0hop2Wd9cfGb9jamRLypIQ T7iqzf7N842nNuxOS3QeQ5T3lrLAoV0pIy5xMsf62Q6rdz5dzsozPUYiudpTO09lBHzyTH sTXk6jf4mEh1ATt9QxMaecgXrjzDqyevEgZOBQcEJ56a8QtnK7gSJ6Uuwdkd3d7NgnsLpO Zys/CUy9A6Y7WGvxf9zQ/ncFtUHVS+yPcd74lMl2tlob0VftM6KRvOg6dUsy9w== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang v1 5/5] semantics: add scope to all nodes Message-ID: <20241023022022.38379-6-carlos@maniero.me> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241023022022.38379-1-carlos@maniero.me> References: <20241023022022.38379-1-carlos@maniero.me> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Date: Wed, 23 Oct 2024 02:20:46 +0000 (UTC) X-CM-Envelope: MS4xfA0TsTVHOe5DI/F/FZ+r/4CYnGczrivI1UIqVbL050a8uqVtu82CK+proAtikiZVgE9pxM6wcf5i35bOvFyEfvm/F9Q+EQ6tp7ucxgsLbdPOIj0/NiOh S6EILCE3msRQzszhGGUj0guan/8koJjRxCOTmRs+jimUM7j8m2r90n4KMuMW4EZu4YXRk6kgpae0R6xdah23qUzZoUndDZMaeqk3JCpvRJU1WWWdHOAoCuG0 X-CM-Analysis: v=2.4 cv=TcnEtgQh c=1 sm=1 tr=0 ts=67185d7e a=WwxFCuf3mf1fs3oSi6/dng==:117 a=WwxFCuf3mf1fs3oSi6/dng==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=FXxGotEPVFvnrxr-ttQA:9 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-Country: NL X-Migadu-Flow: FLOW_IN X-Migadu-Scanner: mx11.migadu.com X-Migadu-Spam-Score: -1.54 X-Spam-Score: -1.54 X-Migadu-Queue-Id: 06F92819D9 X-TUID: QP0wLgKQGT+F Instead of manually adding scopes to nodes as needed, now the scope is a base attribute. This makes the implementation simpler and consistent so we no longer need to think if some node kind requires or not the scope. Signed-off-by: Carlos Maniero --- src/ast.h | 5 +---- src/codegen_x86_64.c | 15 ++++++++------- src/type_checker.c | 12 +++++------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/ast.h b/src/ast.h index 7c5e9af..abc12e2 100644 --- a/src/ast.h +++ b/src/ast.h @@ -30,6 +30,7 @@ { \ ast_node_kind_t kind; \ token_loc_t loc; \ + scope_t *scope; \ } /** @@ -88,7 +89,6 @@ typedef struct ast_fn_definition type_t *return_type; bool _extern; ast_node_t *block; - scope_t *scope; } ast_fn_definition_t; typedef struct ast_fn_call @@ -96,7 +96,6 @@ typedef struct ast_fn_call AST_NODE_HEAD; string_view_t id; list_t *args; - scope_t *scope; } ast_fn_call_t; typedef struct ast_var_definition @@ -105,7 +104,6 @@ typedef struct ast_var_definition string_view_t id; type_t *type; ast_node_t *value; - scope_t *scope; } ast_var_definition_t; typedef enum @@ -127,7 +125,6 @@ typedef struct ast_ref { AST_NODE_HEAD; string_view_t id; - scope_t *scope; } ast_ref_t; typedef enum ast_binary_op_kind diff --git a/src/codegen_x86_64.c b/src/codegen_x86_64.c index 9006250..616fa2b 100644 --- a/src/codegen_x86_64.c +++ b/src/codegen_x86_64.c @@ -143,7 +143,7 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node) case AST_NODE_REF: { ast_ref_t ref = expr_node->as_ref; - symbol_t *symbol = scope_lookup(ref.scope, ref.id); + symbol_t *symbol = scope_lookup(expr_node->scope, ref.id); assert(symbol); size_t offset = codegen_x86_64_get_stack_offset(codegen, symbol); @@ -159,7 +159,7 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node) case AST_NODE_FN_CALL: { ast_fn_call_t fn_call = expr_node->as_fn_call; - symbol_t *symbol = scope_lookup(fn_call.scope, fn_call.id); + symbol_t *symbol = scope_lookup(expr_node->scope, fn_call.id); assert(symbol); size_t i = 0; @@ -597,7 +597,7 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node) switch (bin_op.lhs->kind) { case AST_NODE_REF: { ast_ref_t ref = bin_op.lhs->as_ref; - scope_t *scope = ref.scope; + scope_t *scope = bin_op.lhs->scope; symbol_t *symbol = scope_lookup(scope, ref.id); assert(symbol); @@ -668,7 +668,8 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node) ast_ref_t ref = unary_op.operand->as_ref; - symbol_t *symbol = scope_lookup(ref.scope, ref.id); + symbol_t *symbol = + scope_lookup(unary_op.operand->scope, ref.id); assert(symbol); size_t offset = @@ -722,7 +723,7 @@ codegen_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) case AST_NODE_VAR_DEF: { ast_var_definition_t var_def = node->as_var_def; - scope_t *scope = var_def.scope; + scope_t *scope = node->scope; symbol_t *symbol = scope_lookup(scope, var_def.id); assert(symbol); @@ -908,7 +909,7 @@ codegen_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_param_t *param = item->value; - symbol_t *symbol = scope_lookup(fn_def->scope, param->id); + symbol_t *symbol = scope_lookup(fn_def->base.scope, param->id); assert(symbol); // FIXME: add offset according to the param size @@ -926,7 +927,7 @@ codegen_x86_64_emit_function(codegen_x86_64_t *codegen, ++i; } - size_t local_size = calculate_fn_local_size(fn_def->scope); + size_t local_size = calculate_fn_local_size(fn_def->base.scope); if (local_size != 0) { fprintf(codegen->out, " sub $%ld, %%rsp\n", local_size); diff --git a/src/type_checker.c b/src/type_checker.c index 235d711..cc87832 100644 --- a/src/type_checker.c +++ b/src/type_checker.c @@ -108,6 +108,8 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast) assert(checker); assert(scope); + ast->scope = scope; + switch (ast->kind) { case AST_NODE_TRANSLATION_UNIT: { list_item_t *item = list_head(ast->as_translation_unit.decls); @@ -121,7 +123,7 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast) case AST_NODE_FN_DEF: { ast_fn_definition_t *fn_def = &ast->as_fn_def; - fn_def->scope = scope_push(scope); + ast->scope = scope_push(scope); type_resolve(fn_def->return_type); symbol_t *symbol = @@ -136,20 +138,18 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast) type_resolve(param->type); symbol_t *symbol = symbol_new(checker->arena, param->id, param->type); - scope_insert(fn_def->scope, symbol); + scope_insert(ast->scope, symbol); item = list_next(item); } if (ast->as_fn_def.block != NULL) { - populate_scope(checker, fn_def->scope, ast->as_fn_def.block); + populate_scope(checker, ast->scope, ast->as_fn_def.block); } return; } case AST_NODE_FN_CALL: { - ast->as_fn_call.scope = scope; - list_item_t *item = list_head(ast->as_fn_call.args); while (item != NULL) { @@ -223,14 +223,12 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast) symbol_new(checker->arena, id, ast->as_var_def.type); scope_insert(scope, symbol); - ast->as_var_def.scope = scope; populate_scope(checker, scope, ast->as_var_def.value); return; } case AST_NODE_REF: { - ast->as_ref.scope = scope; return; } -- 2.46.1