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 MB2uHflo72ZLLwAA62LTzQ:P1 (envelope-from ) for ; Sun, 22 Sep 2024 02:46:49 +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 MB2uHflo72ZLLwAA62LTzQ (envelope-from ) for ; Sun, 22 Sep 2024 02:46:49 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=QGnhhTPb; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=N2+Kivsv; 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=1726966009; 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=h1zr3xVw51cJdy1uv9LaLIbc10oDIxVIsKCFtJK+DkQ=; b=sTfLu5jaIK88R/MuWAHyddMhZiiNG/xWeG6nC6e1UUEQohihKIw1gDyL69ISm1ATUa7seq WtikjOgH1mrwsG5K3d1GPXM6Tif5pQbz2GbON1bTmq0jmR+fFt6KOcclKHjvtzqdxa79CE +eppaJZ20K4fhn9JzQE+c8eU8bAYqNqS2lDx/pDWsaaJ9EnxVWDn5pu+jvDmTCyo0h5VTM pHEcn3oudRgVEYhSw2r3FHvAPpwyJ/hOBNmfjzc3F1oXIzjKIkDNEMGmMPWY3jtSlrCSa0 01rpcpWMBcY7krmzdZDvl/UAseQ3fUU4x/V7tmfzZ8Qr86WvsVgctVfDFVgy+Q== ARC-Seal: i=2; s=key1; d=johnnyrichard.com; t=1726966009; a=rsa-sha256; cv=pass; b=ZvKhHmtWd7j/XSZBHA6/5jLx4R+3ErHJlp9yJVH+b0DDrjhfzqosbireYetZ7ZrEjapHWJ R3ytSVTI615rzzya9ptLlp8bqzdXWiy1GWZmujp6NxC/sj7CChguvTdK0g2uLYHp/MdEwo CtcckYtjN7IaicKbkbBMYJUuNR7BDAcSEBn9yTUtI5rolxEQbuDzMpW5kboC+IOFJF4CeJ 9+taxQm1mqrxBWcUkhk4LLTw7T/h0HHP+TJmo8pdkyT8CgIeuJVzhUndPvK0uHlXcp5v04 zT/JN4r+AYQ9RcZatAgZWEgjespUczMnMhNgVd+eiEknp89eRG/VecVShSXjjg== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=QGnhhTPb; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=N2+Kivsv; 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 675176088C for ; Sun, 22 Sep 2024 02:46:49 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=pj9vajSK1yeJkGza5UuvPjmoNj0SQHpxUloidem2z78=; 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=1726966009; v=1; b=QGnhhTPbUchVL0W91GBGk+kn6Oe0XeqLH6zHcU2QztNq2e6GnKgDssEHDlD7EDWGaQRHccVJ B9Wh7K2/ZddyOXTV9nYUZ6PM5Q+cIY/36EtoBpgwqPqpoWCEPRoTPjeG+mMljzlNK884iDXg/Lm LSFq6D/cU1jaRdLw53isesqlm802b9BFwRH7J9yNFoEjx7dRw7MTHeG8JOy1gI5471rE6r59KOZ KNaIlD0MLUedBdkcdULeBwwJB8kRWT0xFJ5ZgKrZMYt6d0hxni5a8yTRaY9i2vucveIRtZr8JaX Dc5iF2ku3WNijvpMRTPcix90Q6c41hzaMOHKYlZc/DVNw== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 2899F202D9 for ; Sun, 22 Sep 2024 00:46:49 +0000 (UTC) Received: from buffalo.ash.relay.mailchannels.net (buffalo.ash.relay.mailchannels.net [23.83.222.24]) by mail-a.sr.ht (Postfix) with ESMTPS id 18891202CB for <~johnnyrichard/olang-devel@lists.sr.ht>; Sun, 22 Sep 2024 00:46:47 +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 730FF4E2B69 for <~johnnyrichard/olang-devel@lists.sr.ht>; Sun, 22 Sep 2024 00:46:46 +0000 (UTC) Received: from uk-fast-smtpout10.hostinger.io (100-99-96-38.trex-nlb.outbound.svc.cluster.local [100.99.96.38]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id B81A44E42A3 for <~johnnyrichard/olang-devel@lists.sr.ht>; Sun, 22 Sep 2024 00:46:45 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1726966006; a=rsa-sha256; cv=none; b=i6RXWWlAFpT3amQ3tcdvz9wVmU72YLJZqcqkdJ8PrEEA59fu/PALQFZT4HtnadsHvhiXhp SjqPG5sH/4pMfR2gOEiLI5wVPkYw1C6hbTE/Woe11+d+ctVfdSUxesIuc4Scf2yZ+BBniS DK3n4HEFhYBDOkc0szoyhP02k+UmZiIyjhmnOsQIHS/OwFzhNYMkwRD/oMV0PV13xgkJsu nBK/K1gAQqi6EYNKTjA0r8qPlQcnPKxLD+8xV+vBgjaZtMP+YV4IKfJkvGB+4lHyGwWK5G MM4TWC700hmIA5tAfvc65llF25eFuH/bDcOVKWBMezdlURxxuzmyKq0cmP5ZUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1726966006; 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=h1zr3xVw51cJdy1uv9LaLIbc10oDIxVIsKCFtJK+DkQ=; b=XBd2ggx1N08L4j1lCGBW9sEUv0aW9aJ5qUY9SOxH8MrRuyednjbTijgkOiGHBBF6Itjv0v i2LzGZyHTASb5qS8hHY5CW/Kvz5PLkFS0y11PayTJM1pPoygxXzyZ9pUDTtvN03tf43GET +5Md+NYQ2QIopZfk12H4hbJaNcJU2oDpWKCBaMXKypTSkZvy+USCmygjbJsQrdryzFs/a9 oWyuT1trSeQd/zWfQ6Zt515ReE67negg5yBevKuuUfekBQEdUGKUxbU2Ddfh5cX6piIo/V VJEdM3TF4/XRk6KgxWwz6RIhZr54z8PWXrCBEmls/R8bPigtBfm9xUYzuxz/Ig== ARC-Authentication-Results: i=1; rspamd-5b46bcd97f-s2rjn; 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-Quick-Cooperative: 799aad343ceeea3d_1726966006387_2925326952 X-MC-Loop-Signature: 1726966006387:2800403049 X-MC-Ingress-Time: 1726966006386 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.99.96.38 (trex/7.0.2); Sun, 22 Sep 2024 00:46:46 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1726966003; 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=h1zr3xVw51cJdy1uv9LaLIbc10oDIxVIsKCFtJK+DkQ=; b=N2+KivsvQ5HS9E/l5mOc2cb9+onKgqPjLoPO2ov5IJ9wMb/41AOLq2q33CaLcVGrQOyxVe Sp2hIVIYbA5DE+2AI6eVA7BPExPpl/BQeoQKXonpCLOuTm9zm1DwXdSbzpGbIxto8/Gq+I 9h00OgH1YmlX15ypdtNCm0oeLjcz+Ym/Se+tiG4H/zGZ1zarW5Afdrh91VOIr2AK58P1Lk opZMH1bcGPz6ETLFsBhhBRBQcOXOse4STzAQu1Qfz9HeCQjJ9gwMT5cUIfWKSDDKGTk3TN 5chzlwOtW9zAnXIR2NxzQBCUhsu568jhNCHPA1PgiFXpGcOLnWGqASgj+1INLg== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang v2 4/4] codegen: operate mov instructions based on the symbol's type Message-Id: <20240922004619.518310-5-carlos@maniero.me> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240922004619.518310-1-carlos@maniero.me> References: <20240922004619.518310-1-carlos@maniero.me> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Date: Sun, 22 Sep 2024 00:46:43 +0000 (UTC) X-CM-Analysis: v=2.4 cv=TcnEtgQh c=1 sm=1 tr=0 ts=66ef68f3 a=WwxFCuf3mf1fs3oSi6/dng==:117 a=WwxFCuf3mf1fs3oSi6/dng==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=mDV3o1hIAAAA:8 a=TzFelwp0wH2zj8ozVFgA:9 a=BXDaF_L80NY05PYiAFlV:22 X-CM-Envelope: MS4xfA/0xlZtb83OjEIGqNkoimKtEeea2CJgPx8tJZpwnJTfhllaRevWK9i0zlNvtAIYd+KxTbCKoyWkNUsiIw3cECcdMYQ2HNGLuQsJw5BFz/yPFl4gO8Bt s/nAcNNofoD4OK1eQITprAon5PubDZkCLF8qcU4LKEQ+lR0XjOK/fuU5GzYGm9fm1s8LL3T1elMm8MjYxcmWovPzII0995JgW+dBYmz0NKJayPoaEFDDiqEa 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.80 X-Migadu-Queue-Id: 675176088C X-Spam-Score: -5.80 X-TUID: cGTBTxedDzTh Signed-off-by: Carlos Maniero --- src/codegen_linux_x86_64.c | 60 ++++++++++++++++--- src/type.c | 18 ++++++ src/type.h | 5 +- .../tests/0026_primitive_unsigneds.ol | 27 +++++++++ 4 files changed, 100 insertions(+), 10 deletions(-) create mode 100644 tests/integration/tests/0026_primitive_unsigneds.ol diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c index 1fa6c58..25cda2d 100644 --- a/src/codegen_linux_x86_64.c +++ b/src/codegen_linux_x86_64.c @@ -26,6 +26,10 @@ #define SYS_exit (60) #define PTR_HEX_CSTR_SIZE (18 + 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. +#define X86_CALL_EIP_STACK_OFFSET (8) + // FIXME: move label_index to codegen_linux_x86_64_t structure size_t label_index; @@ -35,6 +39,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 size_t +type_to_bytes(type_t *type); + +static char * +get_accumulator_reg_for(size_t bytes); + void codegen_linux_x86_64_init(codegen_x86_64_t *codegen, arena_t *arena, FILE *out) { @@ -105,7 +115,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); + fprintf(codegen->out, + " mov -%ld(%%rbp), %s\n", + *offset, + get_accumulator_reg_for(type_to_bytes(&symbol->type))); return; } case AST_NODE_BINARY_OP: { @@ -366,12 +379,18 @@ 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 += 8; 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, + " mov %s, -%ld(%%rbp)\n", + get_accumulator_reg_for(type_size), + codegen->base_offset); + codegen->base_offset += type_size; break; } @@ -419,6 +438,18 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) codegen->base_offset = block_offset; } +static size_t +type_to_bytes(type_t *type) +{ + switch (type->kind) { + case TYPE_PRIMITIVE: { + return type->as_primitive.size; + } + } + + assert(0 && "unreachable"); +} + static size_t calculate_fn_local_size(scope_t *scope) { @@ -431,9 +462,8 @@ calculate_fn_local_size(scope_t *scope) map_get_kvs(scope->symbols, 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; + symbol_t *symbol = (symbol_t *)kvs[i]->value; + local_size += type_to_bytes(&symbol->type); } size_t max_child_local_size = 0; @@ -456,7 +486,8 @@ 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 = X86_CALL_EIP_STACK_OFFSET; + ast_node_t *block_node = fn->block; fprintf(codegen->out, "" SV_FMT ":\n", SV_ARG(fn->identifier)); @@ -464,8 +495,6 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_ 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); } @@ -475,3 +504,16 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_ codegen_linux_x86_64_emit_block(codegen, &block); } + +static char * +get_accumulator_reg_for(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.c b/src/type.c index cbdfbde..64147a2 100644 --- a/src/type.c +++ b/src/type.c @@ -21,12 +21,30 @@ type_t type_from_id(string_view_t id) { type_t type = { 0 }; + if (string_view_eq_to_cstr(id, "u8")) { + type.kind = TYPE_PRIMITIVE; + type.as_primitive.size = 1; + type.as_primitive.kind = TYPE_U8; + return type; + } + if (string_view_eq_to_cstr(id, "u16")) { + type.kind = TYPE_PRIMITIVE; + type.as_primitive.size = 2; + type.as_primitive.kind = TYPE_U16; + return type; + } if (string_view_eq_to_cstr(id, "u32")) { type.kind = TYPE_PRIMITIVE; type.as_primitive.size = 4; type.as_primitive.kind = TYPE_U32; return type; } + if (string_view_eq_to_cstr(id, "u64")) { + type.kind = TYPE_PRIMITIVE; + type.as_primitive.size = 8; + type.as_primitive.kind = TYPE_U64; + return type; + } // FIXME: handle user defined types assert(0 && "unknown type"); diff --git a/src/type.h b/src/type.h index b431171..1da3a11 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_kind_t; typedef struct type_primitive 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