From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id mHdKLfHU3GaHaAAAe85BDQ:P1 (envelope-from ) for ; Sun, 08 Sep 2024 00:34:25 +0200 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id mHdKLfHU3GaHaAAAe85BDQ (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 A9F6E72856 for ; Sun, 08 Sep 2024 00:34:25 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=EKshrjLwtaBzP+3Q3w7fOc+5z6Y1z8Q3vD/ECpe6A/c=; 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=UDJZSKxus4/QbZATDcofPiJdzPhDtlJPK1u5O+TUjb47EY1daM6/+1ttzQkjItJWpwveUmy9 B7DFXCsM9qfR2gDug+VIy2PDPXg04W60XfzTHeUyYv2v25uFKNDpeZvrzOjXcF4TYM1XiSyULCN p3oYEPj6vYTY8THhZ8h/vgZH9J4LdWJxJJ96EepUUB2GvO6x3XSyt5Fkj4MYy4PiT77/iswJ5wL 9BgJHszXZjJ6hsreL4+tB8ZvetMP+XEAg5YRqMXHDsGfZ41puFwytfw/m3zebSZr05glMzIUdd1 slO/KWV+F90X6oFdQdh21fxQ1v5gEznAPppU3etyaI28w== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 1160920234 for ; Sat, 07 Sep 2024 22:34:25 +0000 (UTC) Received: from out-183.mta1.migadu.com (out-183.mta1.migadu.com [95.215.58.183]) by mail-a.sr.ht (Postfix) with ESMTPS id 49ACC2025E 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=CGfIrZ4SmWKJKh+iZscdAwJ+n3iw2veVZ78srB0/6Uw=; b=2IuTybqPB9KPMht6Clpe6Aka0QaUyWWsXaajZRmjhqKfSqqscm0yh7zrhFHJJjiFFkaUk7 VryHdL4HrjjG5nymq8NJnQ9u85pNXd+5gKmHimheZQ7C7YVtzyIXcuVpo9+tgaZebzkgKj g2wO3NSnlHjIMp7BNyKCMObfUbzWP8zGqWpGZlvtAt70FlGsed/QHLKzqibV1MJYyPYEY8 yVOxtVq910l/hATrU4fHqvTNprj/6eocV96x1jyct/3MvZnOU3Pa1QYXWSqoiQ419y97Mb seYEOh4qk6G7HDDmE6/GGiGjRjJwosbEw0dvtCS9N/8NsNu5z6W8/I/tFyGYYQ== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang 3/4] codegen: x86_64: add support to emit if statements Date: Sun, 8 Sep 2024 02:31:11 +0200 Message-ID: <20240908003402.145538-4-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: 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-Country: NL X-Migadu-Flow: FLOW_IN X-Migadu-Queue-Id: A9F6E72856 X-Migadu-Scanner: mx13.migadu.com X-Migadu-Spam-Score: -4.00 X-Spam-Score: -4.00 X-TUID: jKZcARvxhokM 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..91ce728 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