From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.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 iEN6FNXe3GbgCwAAe85BDQ:P1 (envelope-from ) for ; Sun, 08 Sep 2024 01:16:37 +0200 Received: from aspmx1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id iEN6FNXe3GbgCwAAe85BDQ (envelope-from ) for ; Sun, 08 Sep 2024 01:16:37 +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 4456C108EC for ; Sun, 8 Sep 2024 01:16:37 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=taazDDvfu8AOIDEptVfJakV+CE/gGFfzwjZYphcD8NU=; 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=1725750997; v=1; b=oYgnwEZBE/G1hEt//J1ZDTAIuDsekMf39nIRCS9e8E59viY2xI7igsuLmKFdhCkkNumdxh6e TBO6buuKz6j/w0xvovDN5vqxRtFBM8Ie8DFgw1X6bcNYKDwLh3gWWpOgb79dQVA8Xm+IvssagLj SvtO5N1q8i1zwQiwo3jzXf6cpCl8p+C1vBC+Zu0Y11Yqc6Dz4Vap6pJ7Z05a6/l491sOFsAOKTi A7Gmx/Kn3mRVuBFThWEAAHGOmTwlMO6013Sx5QDTH3LGuKXkDli6eYzeubaVGEi+jW3k75HrMZz mCTUAmfY1BfONAYk/8zYAp0LALYmO8/6FVCq8Z2rmrYJA== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 06EF320277 for ; Sat, 07 Sep 2024 23:16:37 +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 701832024F for <~johnnyrichard/olang-devel@lists.sr.ht>; Sat, 07 Sep 2024 23:16:36 +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=1725750996; 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=R2hv0M51GsHocoiGkMma7ifGXZF/hZXc4bFRdcclmZc=; b=o4FEHGcKAI9lVBDou/KB+sqijpeKpCwFE8SB1w4VWALTGd52by4B8jgmbS5SVfwZVssNYo 0R8IjYUBEhosq92BtW/3YV9Gl42zLpNmo0IFuc+egbxwtMtEC4cqrxAuermNth/gT+Zwza MajezWErk0MRVQO2GUYnFnkX4fW3fzCAaT/ffl0aYRI9GOSwFQOIEiwK/1UZSuSbR87JdO 0lYEnhfbgttZ2oIOSY2h2tKWN9XlfsBMRZf3s0gftn1bSkWhTGnzoPD+6i1c96llphAdPg vQ67Xdikhk2wd/ueJcPugtGysIIq4eqd0QJtI+u2qdlnlKHW/JQtJE5Sbhugbw== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v2 4/4] codegen: x86_64: add if cond support for literals Date: Sun, 8 Sep 2024 03:10:58 +0200 Message-ID: <20240908011512.152684-6-johnny@johnnyrichard.com> In-Reply-To: <20240908011512.152684-2-johnny@johnnyrichard.com> References: <20240908011512.152684-2-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-Flow: FLOW_IN X-Migadu-Country: NL X-Migadu-Queue-Id: 4456C108EC X-Migadu-Scanner: mx12.migadu.com X-Migadu-Spam-Score: -4.00 X-Spam-Score: -4.00 X-TUID: onh2dx6vYN3W 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 3b509ba..958c46e 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