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 ms5.migadu.com with LMTPS id UEhAGAiD7maNzQAAe85BDQ:P1 (envelope-from ) for ; Sat, 21 Sep 2024 10:25: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 mp2.migadu.com with LMTPS id UEhAGAiD7maNzQAAe85BDQ (envelope-from ) for ; Sat, 21 Sep 2024 10:25:44 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=jo0OhN+A; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=NARlduYf; 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=1726907144; 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=/Nk6+89VfVmG8Q/mLpLEu48HuPGcQtBPA3yO0nOKzbg=; b=3oQehyhbvJUBAhzTCVo8Plxe1VvVLAMcWMeycyxL2YyPkNU9fdPtWJIne0letaJP3HTbWw bR5n+/I8brDSWa207uy6E/T3PXUMcubs96oNWnJmVqNOFvUKBCtSbNYIIxcqlmGXnjdKZW I/rZ/Cq35g/716TdC1JHZtB8zieWz82g1Ju6X0nQEiv+ym6IKUSKJUqUZ6N0W3rnW2ojl2 Knq1eENCa8D13pYTwQyB+ogKyvn23URAGHdmG4bJnwj/fky7Sy2coJ4s/9ftVSZweL8BXp kelAYjLOs4HQ9GwNkoM10hvIpNRmpXfv07Mi8nYYhmqEeGtGRIoDCXijssQEXg== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=jo0OhN+A; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=NARlduYf; 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=1726907144; a=rsa-sha256; cv=pass; b=Hkt8Yr3yzJEE9EXgj81JjJRZ9SzErps9+OCBnjvJmNeZQoxrd6Lwop7qrBTLFZtJ36StYm Urx4M+Y21BO0AVl6PXCCN7jBOVDMzjKtRfFn96rec4jFmespGrkobc1Pgi73pjszW5BZgc 9cDI9cfb+nfn/r1VBZUH0rT8FFkzwgEI+WP7eKBZ0fPvfs6maRWQJ4Zj4cNxeGhWzbtgJr qjGkCSqnJw2hUokQObHxO97A3GDIky4xojGFs5zUwOdGvmoatLqzycNw08sCdBn9pH5Bxs TB4URp6oy+BppRPxOnynqgi5IQyWChVcGfSvPLRM98Pz85S5ZVDEmaTQklKZpw== 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 2DDD4648A for ; Sat, 21 Sep 2024 10:25:44 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=SbMw6YdqvOElS9vuapVt84QN2+zzO+YyuDhm43JHxuA=; 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=1726907143; v=1; b=jo0OhN+A/pg7UFdz9X5MDYOCy43ihP0/piiee9iO6OAp4lQ8EK82/5Vrr8pF/CqZDd5iyMLL buLXqDhiRk05ruTRpVW0Qg6CSiCV0zYRwJg/UBK9OWWXCyFbCyJnyvErq46+lLqigvB6nVrrBRx 8w0bPzONJf2W8KgdPYsDDDlTY4f9upTuAk7MY63MV79I2UCsQj+s9mimiL5j0JoGZoAO/Vy51pX Aw/naNVrzOclwJDg5dBMAWw+rA4x3uDeE/0PPd7h4rmdPjaFeXPZ7X8FjQROaz6AdM7RudV6hqc UkovIJK7Fg7XvnFCw0YyHSu6cbESg3MUSOiq1YsVzCHFw== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id E6EC2201EF for ; Sat, 21 Sep 2024 08:25:43 +0000 (UTC) Received: from dwarf.ash.relay.mailchannels.net (dwarf.ash.relay.mailchannels.net [23.83.222.53]) by mail-a.sr.ht (Postfix) with ESMTPS id 9EFD2201D6 for <~johnnyrichard/olang-devel@lists.sr.ht>; Sat, 21 Sep 2024 08:25:07 +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 02C18903C68 for <~johnnyrichard/olang-devel@lists.sr.ht>; Sat, 21 Sep 2024 08:25:06 +0000 (UTC) Received: from nl-srv-smtpout1.hostinger.io (trex-10.trex.outbound.svc.cluster.local [100.99.103.236]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id 6E9E4902C41 for <~johnnyrichard/olang-devel@lists.sr.ht>; Sat, 21 Sep 2024 08:25:05 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1726907105; a=rsa-sha256; cv=none; b=WhCFDUezheX/qtK9Jouj1lJIdle5FjqbMhFS+Ewd0UMyxCl9gFQhNDztciiLwlTA+06Ote X0W7gM6ymyQqH6wXhRsAQJT9AZaSBbuAXoa1mDC3EVzDHTVTwy2Y+S8j4jLCTz8kg3v7ZB o72+l9Bvdu/lJK9I49DJvlM+AhnSHvS9xFB3RWrojlmcdgRyxHAeTT6aaHeBP8HMoVKkeR McZdvVURyLJKiA70mE82pXoTL2CdpAnS1gP1Fq7zTHhGJ2+zcrxHRsqQb8WbYyjrsb+zZY zUKe5qrhC+mJXiqVWV34o2N2d0xgufmWEywu1AbCeXRldL74QwVE4jGFjqL3+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1726907105; 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=/Nk6+89VfVmG8Q/mLpLEu48HuPGcQtBPA3yO0nOKzbg=; b=Ct+JOjPUB5ojn/LAzoosPHCvw7IWZ3v2ybuRe/jfrPNHmS59QGOlE56HZAexQVpQgSkxz/ pur0jMuiWzYhiZoW/dSW5yU5NlMGUc4JX9zO99L43GdwO+s0qbT/5nDNBb+9JGYg/JMWlb JcC5N3ZGUqyqUPQLBuAUB8q1P3vq4BiMOJBh5ekJpefElhxGqLdkeVtzZQ5JMKLjRz/2nh 8fm//S+FjFpwrL0jcUQ0NxHt3EGFvJcKUfK3Lspep4fwOBUdOYfrkhYuY6vW08SoPznnDf GJr7RH25Eb3S2CplgdB9HdH+DPz5oy76N4NRmqdsj8A2IoAQTf8/G/3MghHYkA== ARC-Authentication-Results: i=1; rspamd-5b46bcd97f-2pssd; 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-Turn-Spill: 585042114a8e5ac4_1726907105917_1437259522 X-MC-Loop-Signature: 1726907105917:3038872685 X-MC-Ingress-Time: 1726907105916 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.99.103.236 (trex/7.0.2); Sat, 21 Sep 2024 08:25:05 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1726907103; 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=/Nk6+89VfVmG8Q/mLpLEu48HuPGcQtBPA3yO0nOKzbg=; b=NARlduYfDO65SEuFmPa49SmCt9vJoPaqz8WlQ7/vtMa8BKdWUPdH1ipNSc47NbihxIS01d vGHzDtuIDMYLZTVIwr+t9rP0n80nDXfZAx28A5wS5BhJnRWWBvUwvLuYTaIl7fjWUWY2IN NB1599FTpv/JX4UKVhKPlTrKNRJ3zRXf+Ag+k3brIyVdO1XrJ7YCHXckcELvmp6cnDkhwf 1Og/HRTtPB69QG6kaozq0eguMqtu6zxbXp3GqZfzIY1rwT/PB6A+T56gIVRt4kCgp2fxy3 BR7qqbr3LfXf8Yh2p50wIYUDiIVPxAcGdmGgIfg9tDeWHFjvQP62sUQ7MyXsrw== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang 5/5] codegen: perform mov instructions based on variable type Message-Id: <20240921082437.396691-6-carlos@maniero.me> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240921082437.396691-1-carlos@maniero.me> References: <20240921082437.396691-1-carlos@maniero.me> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Date: Sat, 21 Sep 2024 08:25:03 +0000 (UTC) X-CM-Envelope: MS4xfDCGItmS/H9e3QDOXAs+LXZ0PI4mIlPli9iZVTvD7LzA5cN/uLQoz3TqiZY3phHyfmDCUi2i8nMynu+WvYOhQwBXmfAz9Isx19zqNf5G/DEty7Ir5ft2 yC0eZ2VYaEPcF8hOu+N5VDdKidGVo64d0jFb6WdbrwGbzLyqreilwOrgidVBQPejUlM3abGM4UzvGrQE0sYTrf6WpOZbdHiQ7hCPQjZHX1UynQttibeS4qZv X-CM-Analysis: v=2.4 cv=c5X5Qg9l c=1 sm=1 tr=0 ts=66ee82df a=WwxFCuf3mf1fs3oSi6/dng==:117 a=WwxFCuf3mf1fs3oSi6/dng==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=mDV3o1hIAAAA:8 a=JdIIvraH6pShSjMSpRMA: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-Flow: FLOW_IN X-Migadu-Country: NL X-Migadu-Queue-Id: 2DDD4648A X-Migadu-Scanner: mx11.migadu.com X-Spam-Score: -5.80 X-Migadu-Spam-Score: -5.80 X-TUID: 3e05sOLtdj3n There are two function that was introduced to performe the translations bellow: type | type_to_bytes | bytes_to_rax ----------------------------------- u8 | 1 | ah u16 | 2 | ax u32 | 4 | eax u64 | 8 | rax I opted to create *bytes_to_rax* instead of *type_to_rax* mainly because we may use the same function to mov literals once we extend the literals as well (We say that all literals are u32 but we actually handle them as u64 on codegen). Signed-off-by: Carlos Maniero --- src/checker.c | 15 +++++ src/codegen_linux_x86_64.c | 64 +++++++++++++++++-- src/type.h | 5 +- .../tests/0026_primitive_unsigneds.ol | 27 ++++++++ 4 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 tests/integration/tests/0026_primitive_unsigneds.ol diff --git a/src/checker.c b/src/checker.c index f5068e0..3a78a59 100644 --- a/src/checker.c +++ b/src/checker.c @@ -133,11 +133,26 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast) static void evaluate_type(type_t *type) { + if (string_view_eq_to_cstr(type->id, "u8")) { + type->kind = TYPE_PRIMITIVE; + type->as_primitive = TYPE_U8; + return; + } + if (string_view_eq_to_cstr(type->id, "u16")) { + type->kind = TYPE_PRIMITIVE; + type->as_primitive = TYPE_U16; + return; + } if (string_view_eq_to_cstr(type->id, "u32")) { type->kind = TYPE_PRIMITIVE; type->as_primitive = TYPE_U32; return; } + if (string_view_eq_to_cstr(type->id, "u64")) { + type->kind = TYPE_PRIMITIVE; + type->as_primitive = TYPE_U64; + return; + } type->kind = TYPE_USER_DEFINED; } diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c index 415c81b..fa2a082 100644 --- a/src/codegen_linux_x86_64.c +++ b/src/codegen_linux_x86_64.c @@ -38,6 +38,12 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_ static size_t type_to_bytes(type_t *type); +static char * +bytes_to_mov(size_t bytes); + +static char * +bytes_to_rax(size_t bytes); + void codegen_linux_x86_64_init(codegen_x86_64_t *codegen, arena_t *arena, FILE *out) { @@ -108,7 +114,10 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr size_t *offset = (size_t *)map_get(codegen->symbols_stack_offset, symbol_ptr); assert(offset); - fprintf(codegen->out, " mov -%ld(%%rbp), %%rax\n", *offset); + size_t type_size = type_to_bytes(&symbol->type); + + fprintf( + codegen->out, " %s -%ld(%%rbp), %s\n", bytes_to_mov(type_size), *offset, bytes_to_rax(type_size)); return; } case AST_NODE_BINARY_OP: { @@ -369,12 +378,19 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) codegen_linux_x86_64_emit_expression(codegen, var_def.value); } - codegen->base_offset += type_to_bytes(&symbol->type); size_t *offset = arena_alloc(codegen->arena, sizeof(size_t)); *offset = codegen->base_offset; map_put(codegen->symbols_stack_offset, symbol_ptr, offset); - fprintf(codegen->out, " mov %%rax, -%ld(%%rbp)\n", codegen->base_offset); + + size_t type_size = type_to_bytes(&symbol->type); + + fprintf(codegen->out, + " %s %s, -%ld(%%rbp)\n", + bytes_to_mov(type_size), + bytes_to_rax(type_size), + codegen->base_offset); + codegen->base_offset += type_size; break; } @@ -427,7 +443,19 @@ type_to_bytes(type_t *type) { switch (type->kind) { case TYPE_PRIMITIVE: { - return 8; + switch (type->as_primitive) { + case TYPE_U8: + return 1; + case TYPE_U16: + return 2; + case TYPE_U32: + return 4; + case TYPE_U64: + return 8; + } + + assert(0 && "unreachable"); + return 0; } case TYPE_USER_DEFINED: { assert(0 && "user defined types are not defined yet"); @@ -474,7 +502,7 @@ 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) { - codegen->base_offset = 0; + codegen->base_offset = 8; ast_node_t *block_node = fn->block; fprintf(codegen->out, "" SV_FMT ":\n", SV_ARG(fn->identifier)); @@ -493,3 +521,29 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_ codegen_linux_x86_64_emit_block(codegen, &block); } + +static char * +bytes_to_mov(size_t bytes) +{ + if (bytes <= 1) { + return "movb"; + } else if (bytes <= 2) { + return "movw"; + } else if (bytes <= 4) { + return "movl"; + } + return "movq"; +} + +static char * +bytes_to_rax(size_t bytes) +{ + if (bytes <= 1) { + return "%ah"; + } else if (bytes <= 2) { + return "%ax"; + } else if (bytes <= 4) { + return "%eax"; + } + return "%rax"; +} diff --git a/src/type.h b/src/type.h index 855cd83..32da9c0 100644 --- a/src/type.h +++ b/src/type.h @@ -24,7 +24,10 @@ typedef enum typedef enum { - TYPE_U32 + TYPE_U8, + TYPE_U16, + TYPE_U32, + TYPE_U64 } type_primitive_t; typedef struct type diff --git a/tests/integration/tests/0026_primitive_unsigneds.ol b/tests/integration/tests/0026_primitive_unsigneds.ol new file mode 100644 index 0000000..25f0f7e --- /dev/null +++ b/tests/integration/tests/0026_primitive_unsigneds.ol @@ -0,0 +1,27 @@ +# 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(): u64 { + var a: u8 = 255 + var b: u16 = 65535 + var c: u32 = 4294967295 + var d: u64 = 4294967296 + + return a + b + c + d - a - b - c - d +} + +# TEST test_compile(exit_code=0) + +# TEST test_run_binary(exit_code=0) -- 2.34.1