From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id iIZfKH/fLGbzCQEA62LTzQ:P1 (envelope-from ) for ; Sat, 27 Apr 2024 13:20:31 +0200 Received: from aspmx1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id iIZfKH/fLGbzCQEA62LTzQ (envelope-from ) for ; Sat, 27 Apr 2024 13:20:31 +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 993421BA31 for ; Sat, 27 Apr 2024 13:20:28 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=twXQ821N8nWDRA2rmeSpmjLN8/vOFnkwRWVozyOJuNA=; 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=1714216828; v=1; b=Rud7gK3yXa1UpYqBjnRTqI+34QH79acxi3Qc2PUHI4qXmAfuerTsTnQqP5HELkLYwB6GMOxu Urw0P54TycMwQDLB5ZP85Z6S7JMfoIcqkxRckVxl2rCpyXMQ1DegQCfJj2iSgGqR3L7Bo+bCgdg Kd+/LNrJf1ssypbJC6VT5ZtpDSKIvFMfQPO9aLNnUBJ4r3p4/qHf4t0TeuAkiIc4wVwHkw+1F6N inrh4hf1RhRCTRB0HpLCUUVMqdniS/wSuep6oqdvz9YllcfTlkC5JNONK+yFFZiHXaAio0qzAG6 Vj1zXKiE+w8Yojwzm3NN2h28TaGH/Msj+rWL+11W7Ee4A== Received: from lists.sr.ht (unknown [IPv6:2a03:6000:1813:1337::154]) by mail-a.sr.ht (Postfix) with ESMTPSA id EDF3B202D5 for ; Sat, 27 Apr 2024 11:20:27 +0000 (UTC) Received: from out-172.mta0.migadu.com (out-172.mta0.migadu.com [IPv6:2001:41d0:1004:224b::ac]) by mail-a.sr.ht (Postfix) with ESMTPS id 40DCA20381 for <~johnnyrichard/olang-devel@lists.sr.ht>; Sat, 27 Apr 2024 11:20:27 +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=1714216826; 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=ao+B13LLd3STHTnTfOIeXZ9Uz5p+uNE/qbydNEoUSqI=; b=JE2Su5KQSd2fxaGYazPLt6Be69wzrYZQFC9Lrszo3Z40P+p3hg9Aq4tNzTnrH1aNQt8ja7 5rgTAly1Rb7QTVTQQPAmk1tb35N3aTXAwqnD6tHOfMjyN9b9Z1HrW/p4CfuzA/AB1GVhA9 fJCZP26lhG+G+iY4d0UkrEHpKWaMEAVSXxW3pNKW3jbaKAh3GCWYq4eSQhC+x9HWQr/K0T bjt+tqnkSbFlNH5uQ31qbZNxhdCtiuqyzmgelNxtzsUGOx3JVmB9uzdpG9VTCSyZw26JK+ k4gVDZivI8Terua+x5OGyoG3GPNaiC5PrkLywYDf3SRZdoU0xQLp4c5E+6udUg== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v1 1/2] codegen: x86_64: extract function to emit expression Date: Sat, 27 Apr 2024 14:14:11 +0200 Message-ID: <20240427121832.203068-2-johnny@johnnyrichard.com> In-Reply-To: <20240427121832.203068-1-johnny@johnnyrichard.com> References: <20240427121832.203068-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: 993421BA31 X-Migadu-Scanner: mx12.migadu.com X-Migadu-Spam-Score: -4.00 X-Spam-Score: -4.00 X-TUID: yNYStjZSjAps 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 d4f8222..72db1c6 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->data.as_literal; + assert(literal_u32.kind == AST_LITERAL_U32); + uint32_t exit_code = literal_u32.value.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->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; + 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.44.0