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 uOfhBdXe3GZcfQEAe85BDQ:P1 (envelope-from ) for ; Sun, 08 Sep 2024 01:16:37 +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 uOfhBdXe3GZcfQEAe85BDQ (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 0158B76831 for ; Sun, 08 Sep 2024 01:16:37 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=GpME5CYqlTyfbUfzDRfTJe2lBkA37UnkHjhh5V5YBz4=; 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=1725750996; v=1; b=GQjtWrK/DQp95ImYKfhcM53DMJ9sFXNZAyVuQrE5VaM7NIdR6BHUj/aL+yJR+NWvOgpQxlZL +8KYeISP1XjQO321cdSKrO94lehn924gRTmCYMjeG8XAlVjz84Qy4U6XuLaYD7xRpUX2ak9/tMR xKuVy8yKQWDBycVUE6P3D7n3pzqW/IrfK6BhvFNEVP50q/wDCj8UjHPlPuj9oj1BxqhUegvkw8H UVtLvlCDg3X8KgWvoczPw8E3jzXOQ4T1/W2KM/f63N5LCIZcwcx1hY6NzG/0vNB7Bqa9fguDx52 wF6cvln5NyBCnCotO3DNHYQEpI0Hlogvsk0xHbUWaiFlw== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id BDC2120270 for ; Sat, 07 Sep 2024 23:16:36 +0000 (UTC) Received: from out-187.mta1.migadu.com (out-187.mta1.migadu.com [95.215.58.187]) by mail-a.sr.ht (Postfix) with ESMTPS id 3C4842024F 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=fFGReHWnjNPh/LZrXcH/NNWEdoRNg3tCbNYl3Kx0Y0s=; b=gxfWUoJg9L/4pQgO2qeyVHzR0JdTHMLSCOyncedWYLce/OimcguLBGYaIE67qRao7Ad0QZ E2/wAsMyFvtBMYN1ZjgvBsXf5yDc4Mn8sm6JRL4zFQZMTEJJRlB+zbvbfrulAc3W7SP4Nu v5yak8KaW/2J+VTxbbebzb1mYOJStYNpGGNMsOZkdcxecQWrK735H3qnz9cBJhhjAr2MZ4 NdmUqYIkc+eJz6+/GI2n1iyij7TLO9T96feeBgicM/5Yh9LqObdDK51WEPlLsORw5LHsmF /yi2ElbBkpKmXLAs9An9Ax3torQyosQnUEVhNld45z9T7yW8xBQ1OrmIXuBsDw== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v2 3/4] codegen: x86_64: add support to emit if statements Date: Sun, 8 Sep 2024 03:10:57 +0200 Message-ID: <20240908011512.152684-5-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: UNKNOWN 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: -4.00 X-Spam-Score: -4.00 X-Migadu-Queue-Id: 0158B76831 X-Migadu-Scanner: mx11.migadu.com X-TUID: +27FzfLFAix4 Signed-off-by: Johnny Richard --- src/codegen_linux_x86_64.c | 63 ++++++++++++++----- tests/integration/tests/0020_if_statement.ol | 3 + .../tests/0021_if_statement_failed.ol | 24 +++++++ 3 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 tests/integration/tests/0021_if_statement_failed.ol diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c index 64ec0e0..3b509ba 100644 --- a/src/codegen_linux_x86_64.c +++ b/src/codegen_linux_x86_64.c @@ -299,26 +299,61 @@ codegen_linux_x86_64_emit_expression(FILE *out, ast_node_t *expr_node) assert(0 && "unsupported expression"); } } +static void +codegen_linux_x86_64_emit_block(FILE *out, ast_block_t *block) +{ + + size_t nodes_len = list_size(block->nodes); + + for (size_t i = 0; i < nodes_len; ++i) { + ast_node_t *node = list_get(block->nodes, i)->value; + switch (node->kind) { + case AST_NODE_RETURN_STMT: { + ast_return_stmt_t return_stmt = node->as_return_stmt; + + ast_node_t *expr = return_stmt.data; + + codegen_linux_x86_64_emit_expression(out, expr); + + fprintf(out, " ret\n"); + + break; + } + case AST_NODE_IF_STMT: { + ast_if_stmt_t if_stmt = node->as_if_stmt; + + ast_node_t *cond = if_stmt.cond; + ast_node_t *then = if_stmt.then; + + size_t end_if_label = codegen_linux_x86_64_get_next_label(); + + codegen_linux_x86_64_emit_expression(out, cond); + fprintf(out, " jnz .L%ld\n", end_if_label); + + assert(then->kind == AST_NODE_BLOCK && "invalid if-then block"); + ast_block_t then_block = then->as_block; + + codegen_linux_x86_64_emit_block(out, &then_block); + + fprintf(out, ".L%ld:\n", end_if_label); + break; + } + default: { + assert(0 && "unsupported block statement"); + break; + } + } + } +} static void codegen_linux_x86_64_emit_function(FILE *out, ast_fn_definition_t *fn) { ast_node_t *block_node = fn->block; + fprintf(out, "" SV_FMT ":\n", SV_ARG(fn->identifier)); + assert(block_node->kind == AST_NODE_BLOCK); ast_block_t block = block_node->as_block; - assert(list_size(block.nodes) == 1); - - list_item_t *nodes_item = list_get(block.nodes, 0); - ast_node_t *return_node = nodes_item->value; - assert(return_node->kind == AST_NODE_RETURN_STMT); - ast_return_stmt_t return_stmt = return_node->as_return_stmt; - - ast_node_t *expr = return_stmt.data; - - fprintf(out, "" SV_FMT ":\n", SV_ARG(fn->identifier)); - - codegen_linux_x86_64_emit_expression(out, expr); - - fprintf(out, " ret\n"); + codegen_linux_x86_64_emit_block(out, &block); } diff --git a/tests/integration/tests/0020_if_statement.ol b/tests/integration/tests/0020_if_statement.ol index ef3cd36..d48122f 100644 --- a/tests/integration/tests/0020_if_statement.ol +++ b/tests/integration/tests/0020_if_statement.ol @@ -20,6 +20,9 @@ fn main(): u32 { return 1 } +# TEST test_compile(exit_code=0) +# TEST test_run_binary(exit_code=0) +# # TEST test_contains_tokens WITH # ./tests/0020_if_statement.ol:17:3: # END diff --git a/tests/integration/tests/0021_if_statement_failed.ol b/tests/integration/tests/0021_if_statement_failed.ol new file mode 100644 index 0000000..4bc2838 --- /dev/null +++ b/tests/integration/tests/0021_if_statement_failed.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 0 == 1 { + return 1 + } + return 0 +} + +# TEST test_compile(exit_code=0) +# TEST test_run_binary(exit_code=0) -- 2.46.0