From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.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 VxfQFuEwy2amTAEA62LTzQ:P1 (envelope-from ) for ; Sun, 25 Aug 2024 15:25:53 +0200 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id VxfQFuEwy2amTAEA62LTzQ (envelope-from ) for ; Sun, 25 Aug 2024 15:25:53 +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 48D2E57DAA for ; Sun, 25 Aug 2024 15:25:50 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=YybWj8AEF3YhaFS3sRMwUvlk4EJzrBjH/XGqDYt6aC4=; 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=1724592349; v=1; b=LHUsPX5eSq5ojXKcBJ6NlyuGado5blxuqYO4UchaHIBJ3gT9aTTsv8PxgZWi5dSLEuEKMz4+ aqKGF2ZJCtbnB+1yw3hSukmZKiA1T2dfDnb81t4HikCh9ehEfJyuZHM4BFxOS564QPo2VfqA6v8 v8VRUkRW5e6PV0RzGdDXvqaoZOKhUNY+UCXn1BXtAS/yADo/AwgQxdxEraXSqaEJz0yhnF1wf7i P2UWSCnL/SJqj13aQVsNKhr+2OgMrmRsutNGXafpj6rSQih9e6c0fcNBPdJ9WF87wG8swuSCt+x 0Xz58SXK1YFkEir5ud3qmdVgN1S2SE9QxXkSzaGnaFOVQ== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 895BE20204 for ; Sun, 25 Aug 2024 13:25:49 +0000 (UTC) Received: from out-178.mta1.migadu.com (out-178.mta1.migadu.com [95.215.58.178]) by mail-a.sr.ht (Postfix) with ESMTPS id E6C4A201FE for <~johnnyrichard/olang-devel@lists.sr.ht>; Sun, 25 Aug 2024 13:25:48 +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=1724592348; 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=DQcWn9RN/+CIIUNGZocpmgjV40tKEEjxSnQKAXohlU0=; b=BqYJkLGgenxK049vOjMHhsEsFWkaq+ysANnFo25Bn32QnKwia416QMjhr6IKAKNzp+vrYi uuw6ubTqCX212MaKlIXorJC6Nb//CR7D0GXQ56UWxicEpZtl76frfQqHElHsTnztMxGnmU le2eMDS3oQWvBKl1Ik7IxdfkCfxv4vTqiYo9TKepYbJzG5qcIrwWOLG8G6YEsVqJEanJu/ dwxncuau/cF5H/gW4QMJgzqHrBWK7k9CEPjyEyvHJr2Muu0CH5U2al2qbZeXgbHzI7Czck uqK4rmA/B4VnwKE6hRGLrYoRvb2Ftyu4XTGJajmqtyjjGer3dmlUS9aCjqaOHQ== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v2 1/2] codegen: x86_64: extract function to emit expression Date: Sun, 25 Aug 2024 15:16:43 +0200 Message-ID: <20240825132523.253490-2-johnny@johnnyrichard.com> In-Reply-To: <20240825132523.253490-1-johnny@johnnyrichard.com> References: <20240825132523.253490-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: 48D2E57DAA X-Migadu-Scanner: mx13.migadu.com X-Migadu-Spam-Score: -4.00 X-Spam-Score: -4.00 X-TUID: pUmfP+75svT+ We want to generalize how we emit expressions since we could have different ast_node types. Today we are expecting integer literal but we want to make room for emitting binary operations as well. Signed-off-by: Johnny Richard --- src/codegen_linux_x86_64.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c index 7ccb252..b277014 100644 --- a/src/codegen_linux_x86_64.c +++ b/src/codegen_linux_x86_64.c @@ -56,6 +56,24 @@ codegen_linux_x86_64_emit_start_entrypoint(FILE *out) fprintf(out, " syscall\n"); } +static void +codegen_linux_x86_64_emit_expression(FILE *out, ast_node_t *expr_node) +{ + switch (expr_node->kind) { + case AST_NODE_LITERAL: { + ast_literal_t literal_u32 = expr_node->as_literal; + assert(literal_u32.kind == AST_LITERAL_U32); + uint32_t exit_code = literal_u32.as_u32; + + fprintf(out, " mov $%d, %%eax\n", exit_code); + return; + } + case AST_NODE_BINARY_OP: + default: + assert(0 && "NOT IMPLEMENTED"); + } +} + static void codegen_linux_x86_64_emit_function(FILE *out, ast_fn_definition_t *fn) { @@ -70,14 +88,11 @@ codegen_linux_x86_64_emit_function(FILE *out, ast_fn_definition_t *fn) assert(return_node->kind == AST_NODE_RETURN_STMT); ast_return_stmt_t return_stmt = return_node->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->as_literal; - - assert(literal_u32.kind == AST_LITERAL_U32); - uint32_t exit_code = literal_u32.as_u32; + ast_node_t *expr = return_stmt.data; fprintf(out, "" SV_FMT ":\n", SV_ARG(fn->identifier)); - fprintf(out, " mov $%d, %%eax\n", exit_code); + + codegen_linux_x86_64_emit_expression(out, expr); + fprintf(out, " ret\n"); } -- 2.46.0