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 0J0KD7Dg+WXTZgAAqHPOHw:P1 (envelope-from ) for ; Tue, 19 Mar 2024 20:00:00 +0100 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 0J0KD7Dg+WXTZgAAqHPOHw (envelope-from ) for ; Tue, 19 Mar 2024 20:00:00 +0100 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 2C9597BEFA for ; Tue, 19 Mar 2024 20:00:00 +0100 (CET) DKIM-Signature: a=rsa-sha256; bh=HMrq/9aZSA5DD48wG4XbEcVKiI6F+YWSerjpBs0ZDOI=; 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=1710874799; v=1; b=AOPym6ZdRw+WZaib55iH1a3EcSg3p7dU8Tp43z+5vN3NwYcQBpvWs+kv8ZYui/GocBgCEnhd El2s2yfV180NferBl/cMKT1xEtk5lwuEO/DndweJqCAzZFEzo73xxUgQc4hrjgQp7XFXsYMO1gB JFvKgH0OiuJQo6nACSHpx2GtSkcWK1Va680OiazeGRbZcfmzkfN6Ubr5fULbActWt5JC1K9EFRG CMEROqhLd6Px/ONJ9yT4hYjv4xzSOLdQYKFkbPKmyHQtcFRPilzMCKUT8ch6YYFPxNfnngAZyHG e6IGDiJovyhZPXcf7IStqH8cR9X/RV8BHkTbj6lDJQ27g== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 9598020106 for ; Tue, 19 Mar 2024 18:59:59 +0000 (UTC) Received: from out-177.mta1.migadu.com (out-177.mta1.migadu.com [IPv6:2001:41d0:203:375::b1]) by mail-a.sr.ht (Postfix) with ESMTPS id F0335200CD for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 19 Mar 2024 18:59:58 +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=1710874798; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=L8q4HXfw8hVzfU7axE8yt1CFQyyBqAgFdsaZ0Dm5wW4=; b=yUg6r6fVam4kjvZZ9snXsVLBIzYlD5PNaXpJxOR7Ai5V5oScUnoYfNiEdOgH/r1ZC9jxVG dmvL+aGUNlhk6a70mxcDdBznC+AgTEwHjh4jA58kP4aX7oXL3vUvDJ0mEXjS/RKNHhpow3 tUcPom+Nql/vz+x7Eu0NgupJAyeDO9C+OIGJCs8mXfrSQu15MNmypXCZBVlTRNlojLVLMO O6zFgRxSasc/v2dIv1qaGiWPv5yzBWRRAFzCiBArCsRfPHN/wbp5fpMXPNjSMSe3OBdJJf EGR6AOoUHDrzsLnGMzRGzvlY0kowQn29dEJflMhoF/YqUB7lCXIcpMH4oy5TGQ== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v1 3/3] codegen: add compiler support to linux aarch64 arch Date: Tue, 19 Mar 2024 20:57:20 +0100 Message-ID: <20240319195947.202414-4-johnny@johnnyrichard.com> In-Reply-To: <20240319195947.202414-1-johnny@johnnyrichard.com> References: <20240319195947.202414-1-johnny@johnnyrichard.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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-Scanner: mx10.migadu.com X-Migadu-Spam-Score: -4.00 X-Spam-Score: -4.00 X-Migadu-Queue-Id: 2C9597BEFA X-TUID: wHZTeUoCeMBP This patch adds codegen for aarch64. If you want compile to aarch64 from a x86_64 machine you have to set the --sysroot and --arch argument correctly as well (you might want to install gcc for the target architecture and qemu to run the program). $ ./olang examples/main_exit.ol -o main --arch aarch64 --sysroot /usr/aarch64-linux-gnu $ qemu-aarch64 ./main $ echo $? # should return 0 Signed-off-by: Johnny Richard --- src/codegen_linux_aaarch64.c | 94 ++++++++++++++++++++++++++++++++++++ src/codegen_linux_aarch64.h | 25 ++++++++++ src/main.c | 3 +- 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 src/codegen_linux_aaarch64.c create mode 100644 src/codegen_linux_aarch64.h diff --git a/src/codegen_linux_aaarch64.c b/src/codegen_linux_aaarch64.c new file mode 100644 index 0000000..657a4f4 --- /dev/null +++ b/src/codegen_linux_aaarch64.c @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2024 olang maintainers + * + * 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 . + */ +#include +#include +#include + +#include "codegen_linux_aarch64.h" +#include "list.h" + +#define SYS_exit (93) + +/** + * ─────────────────────────────────────────────────────────────────── + * Arch/ABI Instruction System Ret Ret Error Notes + * call # val val2 + * ─────────────────────────────────────────────────────────────────── + * arm64 svc #0 w8 x0 x1 - + * ────────────────────────────────────────────────────────────── + * Arch/ABI arg1 arg2 arg3 arg4 arg5 arg6 arg7 Notes + * ────────────────────────────────────────────────────────────── + * arm64 x0 x1 x2 x3 x4 x5 - + */ + +static void +codegen_linux_aarch64_emit_start_entrypoint(FILE *out); + +static void +codegen_linux_aarch64_emit_function(FILE *out, ast_fn_definition_t *fn); + +void +codegen_linux_aarch64_emit_program(FILE *out, ast_node_t *node) +{ + codegen_linux_aarch64_emit_start_entrypoint(out); + + assert(node->kind == AST_NODE_PROGRAM); + ast_program_t program = node->data.as_program; + + ast_fn_definition_t fn = program.fn->data.as_fn_def; + + assert(string_view_eq_to_cstr(fn.identifier, "main")); + codegen_linux_aarch64_emit_function(out, &fn); +} + +static void +codegen_linux_aarch64_emit_start_entrypoint(FILE *out) +{ + fprintf(out, ".text\n"); + fprintf(out, ".globl _start\n\n"); + + fprintf(out, "_start:\n"); + fprintf(out, " bl main\n"); + fprintf(out, " mov w8, #%d\n", SYS_exit); + fprintf(out, " svc #0\n"); +} + +static void +codegen_linux_aarch64_emit_function(FILE *out, ast_fn_definition_t *fn) +{ + ast_node_t *block_node = fn->block; + assert(block_node->kind == AST_NODE_BLOCK); + ast_block_t block = block_node->data.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->data.as_return_stmt; + + ast_node_t *literal_node = return_stmt.data; + assert(literal_node->kind == AST_NODE_LITERAL); + ast_literal_t literal_u32 = literal_node->data.as_literal; + + assert(literal_u32.kind == AST_LITERAL_U32); + uint32_t exit_code = literal_u32.value.as_u32; + + fprintf(out, "" SV_FMT ":\n", SV_ARG(fn->identifier)); + fprintf(out, " mov x0, #%d\n", exit_code); + fprintf(out, " ret\n"); +} diff --git a/src/codegen_linux_aarch64.h b/src/codegen_linux_aarch64.h new file mode 100644 index 0000000..fb88b64 --- /dev/null +++ b/src/codegen_linux_aarch64.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2024 olang maintainers + * + * 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 . + */ +#ifndef CODEGEN_LINUX_AARCH64_H +#define CODEGEN_LINUX_AARCH64_H + +#include "ast.h" + +void +codegen_linux_aarch64_emit_program(FILE *out, ast_node_t *prog); + +#endif /* CODEGEN_LINUX_AARCH64_H */ diff --git a/src/main.c b/src/main.c index 3e1b2a3..ff0aaa8 100644 --- a/src/main.c +++ b/src/main.c @@ -23,6 +23,7 @@ #include "arena.h" #include "cli.h" +#include "codegen_linux_aarch64.h" #include "codegen_linux_x86_64.h" #include "lexer.h" #include "parser.h" @@ -121,7 +122,7 @@ handle_codegen_linux(cli_opts_t *opts) if (strcmp(opts->arch, "x86_64") == 0) { codegen_linux_x86_64_emit_program(out, ast); } else if (strcmp(opts->arch, "aarch64") == 0) { - assert(false && "Not implemented yet."); + codegen_linux_aarch64_emit_program(out, ast); } else { fprintf(stderr, "error: architecture '%s' not supported\n", opts->arch); cli_print_usage(stderr, opts->compiler_path); -- 2.44.0