From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.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 UCH1LyB7EGfuzAAAqHPOHw:P1 (envelope-from ) for ; Thu, 17 Oct 2024 04:49:05 +0200 Received: from aspmx1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id UCH1LyB7EGfuzAAAqHPOHw (envelope-from ) for ; Thu, 17 Oct 2024 04:49:04 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=qXbYCRkv; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=qYRCqMZJ; dmarc=pass (policy=none) header.from=maniero.me; 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") ARC-Seal: i=2; s=key1; d=johnnyrichard.com; t=1729133344; a=rsa-sha256; cv=pass; b=l69qcsj0BM1CcFmwCUiQymMTaBJfW6w+OaDitTAejTDaN4D2GYeusAypa3WIMBVLNv0RTc Sm3y24vOdrqbiTAqiiOAWhWJcbV6Je1sMUD4hLTVIPXeQ++uiZFNlPuS1APR8Gaospd7GF IXeqbEAJr9tclrl+iOySppW1Mgilayd08Z9YSAHlWxI2orLans4BBI9zcL4qgYkT5FJK55 eMwzI8Vv+d9bAO4qz93G1FfsQUwfBlqo/ZYLP/bd/sx+l7K0P1ciZ2v78w12x7sCVyH8BI 5kpBbHZP8aE7n19nut6YTajtT1c9tLKoPrVw7JF62FayyPAptrwr2zvor/fzmw== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=qXbYCRkv; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=qYRCqMZJ; dmarc=pass (policy=none) header.from=maniero.me; 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") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1729133344; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: 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=5l7Ddv6rIQEdv4+lWdZZyXSTrm2zzzhnz1HcKUXOeaI=; b=YdTu33/XQCR600kmTSxXJq4nLDDfsOeuf0QEys8VETZsmCekSP+I2OSCOv8Ukkug9W3jIR FAnwN20NTI2mnLen25PG+Fe426l5wCdXHKIkAW4G4QeOweMJ1wy3QRLDDgttD2zuOpt3vo pYWZh63Dq8fzR4wgj6HYt/f5IsSoyjIduV7V0ugq/eoqdZaJLWUuqfK+eww/34aYUcX/2S o7sx3tq6NsN3dXtO2vey2WtvL4/CsjHlM5bf1nI7wvuV1Umvae0jF48730jD6YBEm8vZSu IDFgAnAxyr5xQc9uPRbLp40uX+hLNM5MDkOoeAN3BFzprbxaK+iss2GgXMRhNQ== 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 561F466E48 for ; Thu, 17 Oct 2024 04:49:04 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=CeSeUWLCmPouubBermCsVDkDoo0EiIbhYkqmsuBx03c=; 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=1729133344; v=1; b=qXbYCRkvDjB24pZ5K1EfOh30xMwXEe5G3QU3QCOxYpJprxdto4cIlh4GdJEUjVCYxiWFis90 z1IiptCb0LWAAKP+DbgQOtM8z7kDoDZb1Vecb14QPXPkkSmSkNmXC9MNQZOBPoJ671W/cobdasn L0tYN0YDqyNkHr7uC1B7V25kenIohXp1q7Jq/DuA5WAGbfNjSHv0QyisDXquRjKCgu3QaZqrnUe zQGoEKenVJU3uuPZzqwxyDTVYs3eEYSNxK73ZqdJNnvNDeCfM12Nw5Y7xSfAqPmSieY7OGyk1gY 9+XCOiaR9Ju9c2NiP8COttSE6RhKVhC6KRVfzCzoIYBNw== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 01198202E8 for ; Thu, 17 Oct 2024 02:49:04 +0000 (UTC) Received: from bird.elm.relay.mailchannels.net (bird.elm.relay.mailchannels.net [23.83.212.17]) by mail-a.sr.ht (Postfix) with ESMTPS id 061902028E for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 17 Oct 2024 02:49:02 +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 3234183FF1 for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 17 Oct 2024 02:49:01 +0000 (UTC) Received: from fr-int-smtpout5.hostinger.io (trex-9.trex.outbound.svc.cluster.local [100.99.180.193]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id 866FE83FDB for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 17 Oct 2024 02:49:00 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1729133340; a=rsa-sha256; cv=none; b=bQQChzlIzwQzHmU+mByRkaBrP9BwH7SCuir+5QmzbhEnkLM99vZD8ZrltMpyzV5+UdYLFa 8tl/H3PliyaCc0saG8AYITHVvLfgM9D2nkY07/IM9AcZqRYvN3HeJfYynLzULcbmxUuQQI fhinqDXgFptVAfie27zkb8zLJ9jcGpDSNsqgtTGRAS3ecPVthZ6l82F5+O9IFFnjiBg0Hj aMUzU3DyRL52qFj5KiDTXrRXJJx+IdIGySPFGHeWvkUBNf1oHW9b2VRqBg3CXHCZJ06jDO fqdGfobIEpWpBbFsb5s8qObxot4qbgkQAc5vHdTPE4E7c7fwYhiPlT8muOs64A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1729133340; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5l7Ddv6rIQEdv4+lWdZZyXSTrm2zzzhnz1HcKUXOeaI=; b=JkWO1AK6OUnUBxkMxm5JrVWgjHQbzRP6QUcjKdSaFCocLkc8layRXJVRGwJnnH3Qkaw9gu gRflQjG/x17/oluFdKF79oEK6M3r8k6JEry/wEDyTYLtk9vLuqnxx8H8r6C2ZELVeRGFq2 uKkS6txWNHH8cpGSOnfr58rC44akIjkYbE/gBGsGykhgx4d20j9/1qVMP09Mxq7yjqZKO6 YBfS4F3I7Sl17orq+PUla1mGWPFsxZJofNwvn5tVk+NzTk+2LO5SQZxLxOg2Bj1y2o/nDG u/SXrPzfWz3QsOvH+cR60R9aHeHOFAyks+TKKGWO55kpSDa/gsVzbGQ+Uqoofw== ARC-Authentication-Results: i=1; rspamd-75d86777c9-m4jlz; 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-Spill-Hysterical: 27747b2f7d9d2c92_1729133341042_3963425633 X-MC-Loop-Signature: 1729133341042:2352581827 X-MC-Ingress-Time: 1729133341042 Received: from fr-int-smtpout5.hostinger.io (fr-int-smtpout5.hostinger.io [89.116.146.168]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.99.180.193 (trex/7.0.2); Thu, 17 Oct 2024 02:49:01 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1729133338; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5l7Ddv6rIQEdv4+lWdZZyXSTrm2zzzhnz1HcKUXOeaI=; b=qYRCqMZJ/Qj43TvtSgq64wE8ierI/fOWlYXyruRCrSShtEQmRua4gUJuKI6tYWPANawmf4 1qFh0HvC3oS6VSC1rDTA7cyesdQt7SYR9sMli30MFTH7UPEc7zDZfleHqnIaDF1H/36O4B MXWqBrC9rD1pSYzggkgrSFtdKyEUX6rrL1BwnGsWDECJBgTozAYZhotNbrMs0I0idtL9Eh yNwVepxCxaGyv4iXVCDLIGuYOUKzWHCxRocl4Xslh2KxBXqZToORqZSoKwyN1cr+gnG3ey HHJXIe5bgASYxmbE/b2fF1upXIB+NFZbqToTLJMnFMg9xoKshX2finAdPSaPEw== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang v1 1/1] codegen: x64: deref returns pointer value Message-ID: <20241017024849.320062-2-carlos@maniero.me> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241017024849.320062-1-carlos@maniero.me> References: <20241017024849.320062-1-carlos@maniero.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Date: Thu, 17 Oct 2024 02:48:58 +0000 (UTC) X-CM-Envelope: MS4xfGcAGjjQO2RPkFG3uQQ79TR8Wcli2khnKmevHsde+v9iNA4Sac0NtmBVxlvf08E1B6VWjVsTdxM31TRA/SnVpaCvXyGPf0hcKSzD82+tEqcvdF4OZA/B m8t+2sTs3sPOC36rj1KT743INuJQUUZsk8RwlY67FOTJef8Rhp2EaLI+3BtamWzMkCgYwt262Ro8qEY1xqRvANqylZxzhE++y20a/T/v4e0lejF3jqhlbsYY X-CM-Analysis: v=2.4 cv=Z6G+H2RA c=1 sm=1 tr=0 ts=67107b1a a=WwxFCuf3mf1fs3oSi6/dng==:117 a=WwxFCuf3mf1fs3oSi6/dng==:17 a=IkcTkHD0fZMA:10 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=M2cMyoFjWooY1w7Eky8A:9 a=3ZKOabzyN94A:10 a=QEXdDO2ut3YA:10 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-Spam-Score: -1.27 X-Spam-Score: -1.27 X-Migadu-Queue-Id: 561F466E48 X-Migadu-Scanner: mx12.migadu.com X-TUID: qtgFZ/a8SnUe Deref is context dependent, when doing an assignment: *a = 1 It is expected for the deref codegen to return the pointer location, so than the assignment binop would be able to assign a new value at that location. On another hand, when performing: return *a It is expected for deref to actually returns the pointer location value. The codegen of both behaviors were defined in new functions to avoid indiscriminately increase the codegen_x86_64_emit_expression switch/case. Register choice: ================ Since the operation *mov (%rax), %rax* is not possible, I arbitrarily choose R10 as it is a caller-saved register. Signed-off-by: Carlos Maniero --- src/codegen_x86_64.c | 61 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/src/codegen_x86_64.c b/src/codegen_x86_64.c index deb7e24..db1b77a 100644 --- a/src/codegen_x86_64.c +++ b/src/codegen_x86_64.c @@ -52,6 +52,8 @@ typedef enum x86_64_register_type REG_R15 } x86_64_register_type_t; +typedef size_t size_in_bytes_t; + /** * Arch/ABI arg1 arg2 arg3 arg4 arg5 arg6 arg7 Notes * ────────────────────────────────────────────────────────────── @@ -76,6 +78,14 @@ codegen_x86_64_put_stack_offset(codegen_x86_64_t *codegen, static size_t codegen_x86_64_get_stack_offset(codegen_x86_64_t *codegen, symbol_t *symbol); +static size_in_bytes_t +codegen_x86_64_emit_unary_deref_address(codegen_x86_64_t *codegen, + ast_unary_op_t *unary_op); + +static size_in_bytes_t +codegen_x86_64_emit_unary_deref_value(codegen_x86_64_t *codegen, + ast_unary_op_t *unary_op); + static size_t type_to_bytes(type_t *type); @@ -126,8 +136,6 @@ codegen_x86_64_get_next_label(codegen_x86_64_t *codegen) return ++codegen->label_index; } -typedef size_t size_in_bytes_t; - static size_in_bytes_t codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node) { @@ -619,7 +627,8 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node) AST_UNARY_DEREFERENCE && "unsupported assignment lhs"); - codegen_x86_64_emit_expression(codegen, bin_op.lhs); + codegen_x86_64_emit_unary_deref_address( + codegen, &bin_op.lhs->as_unary_op); fprintf(codegen->out, " push %%rax\n"); @@ -679,12 +688,8 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node) return 8; } case AST_UNARY_DEREFERENCE: { - // FIXME: support dereference of dereference (**) - assert(unary_op.expr->kind == AST_NODE_REF && - "unsupported unary expression for dereference (*)"); - - return codegen_x86_64_emit_expression(codegen, - unary_op.expr); + return codegen_x86_64_emit_unary_deref_value( + codegen, &unary_op); } default: { assert(0 && "unsupported unary operation"); @@ -829,6 +834,44 @@ codegen_x86_64_emit_if(codegen_x86_64_t *codegen, ast_if_stmt_t if_stmt) fprintf(codegen->out, ".L%ld:\n", end_else_label); } +static size_in_bytes_t +codegen_x86_64_emit_unary_deref_address(codegen_x86_64_t *codegen, + ast_unary_op_t *unary_op) +{ + assert(unary_op->kind == AST_UNARY_DEREFERENCE); + // FIXME: support dereference of dereference (**) + assert(unary_op->expr->kind == AST_NODE_REF && + "unsupported unary expression for dereference (*)"); + + return codegen_x86_64_emit_expression(codegen, unary_op->expr); +} + +static size_in_bytes_t +codegen_x86_64_emit_unary_deref_value(codegen_x86_64_t *codegen, + ast_unary_op_t *unary_op) +{ + codegen_x86_64_emit_unary_deref_address(codegen, unary_op); + + ast_ref_t ref = unary_op->expr->as_ref; + + symbol_t *symbol = scope_lookup(ref.scope, ref.id); + + assert(symbol->type->kind == TYPE_PTR); + + size_in_bytes_t deref_size = type_to_bytes(symbol->type->as_ptr.type); + + fprintf(codegen->out, + " mov (%%rax), %s\n", + get_reg_for(REG_R10, deref_size)); + + fprintf(codegen->out, + " mov %s, %s\n", + get_reg_for(REG_R10, deref_size), + get_reg_for(REG_ACCUMULATOR, deref_size)); + + return deref_size; +} + static size_t type_to_bytes(type_t *type) { -- 2.46.1