From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.migadu.com ([2001:41d0:1008:1e59::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id gAYVBenO5mVhCwEAbAwnHQ (envelope-from ) for ; Tue, 05 Mar 2024 08:51:05 +0100 Received: from aspmx1.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id YLxbAOnO5mV+bAEA62LTzQ (envelope-from ) for ; Tue, 05 Mar 2024 08:51:05 +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 E4EDD6F959 for ; Tue, 5 Mar 2024 08:51:04 +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=1709625064; v=1; b=UREcsqutK2Lehn1IZ9e1R335D/DwYxAnu5n9dH4v/XsUH8Q9eMfZGMnYQTRCJHFjs5atxXKi C6qEFVRK/WMEsssuDkA3ryc5+iHr1mKWtTzK1NBS7u4LQ2reZVD6gqab+9Mkguj8fYCXXc2qSAm xlAq4y817Htb1rtCkzAGyCiIgCPgXWt3jh+wBk8b61AlS7g7VfhHTpiR4kzEF2VMpvWIG448rVa h0tnpwFSSnymqQ92gueiaPIV26xXDqN/cYHY22YTFyihsU06cQBB3wkFBQG4S3iwAIjFAUXGJnq qDmtD8cBqxLcOb7K9/rGnWyhNlmqXeM9zMtOHLdZMS1Xg== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 5AECC20183 for ; Tue, 5 Mar 2024 07:51:04 +0000 (UTC) Received: from out-183.mta0.migadu.com (out-183.mta0.migadu.com [91.218.175.183]) by mail-a.sr.ht (Postfix) with ESMTPS id BB9B320141 for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 5 Mar 2024 07:51:03 +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=1709625063; 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=0gqnRSUQo9wT/TRadtoFMQsjuiqGlSfVkLc1uEYx8+6R/EHcEAJ3eKnk0wIHLADHkQk+SG 5YgqUo4fOoNU4aXd3WukRanQjRE64a4xZjOS3YvQFiS/lLJ7Y0v/r0b0Or6keaYjKOXD2g yhqpZw5NHCvHmepsNxmHFXr8hiJC6kA5yAlb2iR4nBJxq1H1LaahnTpJtGrUpCjsB2uoZn YFR/hKjNEzUc/Fqoz22UJlEiprP0Y8NDDM3nNnI/nrG6ZycBzDChlnCsPDqS5w55t5ZVbr ukFEVR2Ahc5SZO0Op/41vlCI2in5JqGP6J+SkjejUkfy79CPnaHq0DHCLTz20g== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v2 1/3] be: create linux-x86_64 gas asm codegen Date: Tue, 5 Mar 2024 09:44:44 +0100 Message-ID: <20240305085101.968075-2-johnny@johnnyrichard.com> In-Reply-To: <20240305085101.968075-1-johnny@johnnyrichard.com> References: <20240305085101.968075-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: E4EDD6F959 X-TUID: xQMkO7W9y5aA 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