From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:700:3204::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id sBMNJfAT5mXDIAAAbAwnHQ (envelope-from ) for ; Mon, 04 Mar 2024 19:33:20 +0100 Received: from aspmx1.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id yMXdIPAT5mUb/AAAe85BDQ (envelope-from ) for ; Mon, 04 Mar 2024 19:33:20 +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 7698DD841 for ; Mon, 4 Mar 2024 19:33:17 +0100 (CET) DKIM-Signature: a=rsa-sha256; bh=GGx7geFv57TvoCZ96NTmIjOwt1WMoV16ByhQf1OZyHI=; 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=1709577196; v=1; b=VNp5by8A/+OZCI9y9REyWTqrJZp2PtE5aoTbUtR5tXXpJuZimumb+Da6pjIdNqLKAyoWdlwE vai30BXTjRfAuHaUpAajj79Xk/rLNNzxt5emP122zhJn2zRnKzDZ5TfCK6pd8k96cdhOryDlsbZ njMtdDDULMfCfYNaLdVhGKRtplim1fvrJQ5bwaT1TuZa0iNkAtD7SeOUnRS3WlyeCayqc11Fvhh yg4HUjSUAzVq7UbjGCX1i29IBvyunr66qNcCFIBJqQOg2ygKlRiBHwa7rORGCIF4Tpy8awRqaPr P6imd97csJL8nJA5yTnyePBQzSUkk/6xh/gTGOW1X1R/g== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 036822015F for ; Mon, 4 Mar 2024 18:33:16 +0000 (UTC) Received: from out-170.mta1.migadu.com (out-170.mta1.migadu.com [95.215.58.170]) by mail-a.sr.ht (Postfix) with ESMTPS id 59DEF20153 for <~johnnyrichard/olang-devel@lists.sr.ht>; Mon, 4 Mar 2024 18:33:15 +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=1709577192; 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=OUQ9YPbdzT20PqrmENaTBVCRUrJFfF5xqUGZg1yhLXg=; b=PHLRxzEGT4HPXedijW9hr89hTCTUCBj7bbAZUOw8vXmdsJKP9miL1TPHMayaZMcumTYEGc nOsPAOW4sC4J9jcHp/FRpvAyx22njeVnbmn+Q5iR1X8fzUocGqCOn03aM+j2tv0WuPkUc/ ZCu26Z0+2Z8N/63fM87B5zLMCht6OsobQNITsrqfDzjS4P0mooFE5UiSsHD0wsQTOtYW6j 1241TY9twk5vdeTJz6c3fjzC9PivNwHNGIXs8LYxcP3i9e0Tdqd4vMwnvjufL3c6TVNQGf dbZKThoq68kIg0x7bWfZKs3ox5uw2tr3NBbhvt0xAt3vaN5Vb3UzPUwo0BHbFw== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v1 1/3] be: create linux-x86_64 gas asm codegen Date: Mon, 4 Mar 2024 20:23:10 +0100 Message-ID: <20240304193313.961286-2-johnny@johnnyrichard.com> In-Reply-To: <20240304193313.961286-1-johnny@johnnyrichard.com> References: <20240304193313.961286-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-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: 7698DD841 X-TUID: LM+OvFo4pgXp This patch creates basic functions to generate GAS assembly for examples/main_exit.0. Signed-off-by: Johnny Richard --- src/codegen_linux_x86_64.c | 81 ++++++++++++++++++++++++++++++++++++++ src/codegen_linux_x86_64.h | 25 ++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 src/codegen_linux_x86_64.c create mode 100644 src/codegen_linux_x86_64.h diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c new file mode 100644 index 0000000..3d4b17e --- /dev/null +++ b/src/codegen_linux_x86_64.c @@ -0,0 +1,81 @@ +/* + * 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_x86_64.h" +#include "list.h" + +#define SYS_exit (60) + +static void +codegen_linux_x86_64_emit_start_entrypoint(FILE *out); + +static void +codegen_linux_x86_64_emit_function(FILE *out, ast_fn_definition_t *fn); + +void +codegen_linux_x86_64_emit_program(FILE *out, ast_node_t *prog) +{ + codegen_linux_x86_64_emit_start_entrypoint(out); + + assert(prog->kind == AST_NODE_FN_DEF); + ast_fn_definition_t fn = prog->data.as_fn_def; + + assert(string_view_eq_to_cstr(fn.identifier, "main")); + codegen_linux_x86_64_emit_function(out, &fn); +} + +static void +codegen_linux_x86_64_emit_start_entrypoint(FILE *out) +{ + fprintf(out, ".text\n"); + fprintf(out, ".globl _start\n\n"); + + fprintf(out, "_start:\n"); + fprintf(out, " call main\n"); + fprintf(out, " mov %%eax, %%edi\n"); + fprintf(out, " mov $%d, %%eax\n", SYS_exit); + fprintf(out, " syscall\n"); +} + +static void +codegen_linux_x86_64_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 $%d, %%eax\n", exit_code); + fprintf(out, " ret\n"); +} diff --git a/src/codegen_linux_x86_64.h b/src/codegen_linux_x86_64.h new file mode 100644 index 0000000..2050c91 --- /dev/null +++ b/src/codegen_linux_x86_64.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_X86_64_H +#define CODEGEN_X86_64_H + +#include "ast.h" + +void +codegen_linux_x86_64_emit_program(FILE *out, ast_node_t *prog); + +#endif /* CODEGEN_X86_64_H */ -- 2.44.0