From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id aGErM/HU3GawYAAAqHPOHw:P1 (envelope-from ) for ; Sun, 08 Sep 2024 00:34:26 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id aGErM/HU3GawYAAAqHPOHw (envelope-from ) for ; Sun, 08 Sep 2024 00:34:25 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; none 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 BDBB67B654 for ; Sun, 08 Sep 2024 00:34:25 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=V3NtIiK2xVTZEAtLHmxhnZeQ0ud9AFdeG1OQ40F31AI=; c=simple/simple; d=lists.sr.ht; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID; q=dns/txt; s=20240113; t=1725748465; v=1; b=SIz0gvJvK9kSa5VQbQaM9fh44aI/HNj18bFIg9dCzMce5BGNX/X2fWI5lZm7zQgHS4TVzYr6 Aj+9a00RCutuHXO2yymbF2ytxt7XsxUtvAEH64Vn6kMTNVPjW5yQuwNxYbpDuxBeAhCju2uNfZP P0aNqioWi532E2YPedlIdacPj4RK2So/N707Xncibx1ItpPHdrly0hjfsS6Y5xYjHXb3Lf9WASF P3c8DVcRcCRTXeWyvbwU2fd7R2EA6FwB4G/1eUZFULHQBPNu9m/fQBkQ6Tf4Kqh/hEwS/bTSvEK FXyoB6bsIscVfBG5cLEHbYnx0rQFn6WMs+722cViad/+w== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 8EB7720269 for ; Sat, 07 Sep 2024 22:34:25 +0000 (UTC) Received: from out-176.mta1.migadu.com (out-176.mta1.migadu.com [95.215.58.176]) by mail-a.sr.ht (Postfix) with ESMTPS id 794D020238 for <~johnnyrichard/olang-devel@lists.sr.ht>; Sat, 07 Sep 2024 22:34:24 +0000 (UTC) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1725748464; 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=KzQtNXSxu+At/Zxi6dDsWI1yBjPZckclZcH5npYSWtY=; b=rvp44piNJQMVXe9LXop5SuFXNt35AcTJS1MtL97ua8/FszlCy40Y5MjdfObyfzyNI9gh6l VdMm4b2ZNaM3urDEHhvzbZxv18gzw9eUD7ST6iI0ytBIAYeg/adOc1/KudjNhCyqavcdUF Era4aAUd0UJ8Mye9fwhkdbZmYHssM2QPN7bD5KZaS7AgPCRnDvRv9rW+bNl6QNwDvSOhkH pM8pBB3g2Xyn/6oHosoAIV74sYg9Vy6+uFrAaM03kWZwnSUDwUrYU7FsTB3B7EpLyXGVor FWiba/H9ZxHLrbs5Oy4jginyCFHq7t1lwx2Xnvv/z3o9OfQRpK8FGyGQAI/eOQ== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang 4/4] codegen: x86_64: add if cond support for literals Date: Sun, 8 Sep 2024 02:31:12 +0200 Message-ID: <20240908003402.145538-5-johnny@johnnyrichard.com> In-Reply-To: <20240908003402.145538-1-johnny@johnnyrichard.com> References: <20240908003402.145538-1-johnny@johnnyrichard.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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-Spam-Score: -4.00 X-Migadu-Queue-Id: BDBB67B654 X-Migadu-Scanner: mx10.migadu.com X-Migadu-Spam-Score: -4.00 X-TUID: ilKdhFKOg8uK We are not optimizing the assembly code, so every literal statement will emit a `cmp` instruction in order achieve if condition support for literals. Signed-off-by: Johnny Richard --- src/codegen_linux_x86_64.c | 1 + .../tests/0022_if_statement_literal.ol | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/integration/tests/0022_if_statement_literal.ol diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c index 91ce728..f131054 100644 --- a/src/codegen_linux_x86_64.c +++ b/src/codegen_linux_x86_64.c @@ -328,6 +328,7 @@ codegen_linux_x86_64_emit_block(FILE *out, ast_block_t *block) size_t end_if_label = codegen_linux_x86_64_get_next_label(); codegen_linux_x86_64_emit_expression(out, cond); + fprintf(out, " cmp $1, %%rax\n"); fprintf(out, " jnz .L%ld\n", end_if_label); assert(then->kind == AST_NODE_BLOCK && "invalid if then block"); diff --git a/tests/integration/tests/0022_if_statement_literal.ol b/tests/integration/tests/0022_if_statement_literal.ol new file mode 100644 index 0000000..3ae6ef3 --- /dev/null +++ b/tests/integration/tests/0022_if_statement_literal.ol @@ -0,0 +1,24 @@ +# 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 { + if 1 { + return 0 + } + return 1 +} + +# TEST test_compile(exit_code=0) +# TEST test_run_binary(exit_code=0) -- 2.46.0