From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.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 uOq5FiBZCWdXBgAAqHPOHw:P1 (envelope-from ) for ; Fri, 11 Oct 2024 18:58:08 +0200 Received: from aspmx1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id uOq5FiBZCWdXBgAAqHPOHw (envelope-from ) for ; Fri, 11 Oct 2024 18:58:08 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=Lqg4o8nM; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=s0py9hEJ; dmarc=pass (policy=none) header.from=maniero.me; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; arc=pass ("mailchannels.net:s=arc-2022:i=1") ARC-Seal: i=2; s=key1; d=johnnyrichard.com; t=1728665887; a=rsa-sha256; cv=pass; b=EMqrcU+yC9FmJrULKUHlkDpPUafafwvVg5UQTQFVyPimMOsvUpPri6shRIrIOpdhywzlNB eTqNGpKpcpX/LpnNUJFcwBpmePXA8ctOzg16HI/IJeKqFBe6Smcq44fe7uIDMOOk+I0SCc yEGOp2rzxXJEq+ExivVOXLsZGGKKgYaIRLxfNww+seEqWXszkYf0CaX2XtqZuyDJAYpgrj pkFuURvff7D0nREUko8bN7OVAFkJa8+v1t6mdpI2YZgRJxTRe2D/t/rPz/yG08FnUPHijg y10plNgaB0/G8i/tJ/Wy9kgX0X/2wAR/Qr048zXPQQZvisnqVxd09Ib5xHut7Q== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=Lqg4o8nM; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=s0py9hEJ; dmarc=pass (policy=none) header.from=maniero.me; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; arc=pass ("mailchannels.net:s=arc-2022:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1728665887; h=from:from:sender:sender: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:list-id: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=Nbwmft1AhlauBywHTfbYn/Ss1keWxIs96q72JRpiXsA=; b=wVFCLTEYM3kcrrUt1iQnlhyiY98fTC8S6BUyJzPmOEFcDAK/H3obJDEtmA40LeG3mMz/yi lbFkuhwcTFm9KoJRWYwGLrMEED5KZsrTE64fSOZTswncQKg82O4z7APTj2LzsXImEaRL1Z HYQIZQG9zFBds6XBdq1lhap4ZmFJaJxgmdlBPlE0Uav5/TV9Z6Jam5/J4S3q0tiEicec/l BlogoUICGyN8WfKXJeMgke6TZBE9k/k4YlTylYRBotCRrAUOclzactCWTDMlsrube+XXze o4HO19KBUsLN+4Cr60Fab16pjuw1q1Dfl3sbnEtGbO6DiiHbm0/7YlSD6mcynQ== 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 3A94E7197 for ; Fri, 11 Oct 2024 18:58:07 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=+jbqAnWz06cnyYLkc4r4dw8hr+fLnhierwPuZQSdqnM=; c=simple/simple; d=lists.sr.ht; h=From:To:Cc:Subject:In-Reply-To:References:Date:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID; q=dns/txt; s=20240113; t=1728665886; v=1; b=Lqg4o8nMOuPRtb/59tF/h60cbsIY5G5rHpspiHw5sh+YhMQsJOurl0I1qUXEIoy7lMWow3+3 J+BOd+IMpfoRT8N7cve0WOGs2imQ0qUHtIqVaOzWkEUqBCc0sgjQGvAGfy/xdsBQ3bXw3OZ8dQl 2OmaBLSIE3LDI7yMaqizw43YvFxCchWBTZB8U+Fq7Wo+atrhuf0iTeWUe5mWjMBrGLhJ6TCLRXz B5O+Ai9918mch52L7JoDwuZW76CGC6bhbVtYsdmFRbUfd3SDrEkZODAuMNIYuR4kX+t2wzxTcpU fj84IhOch7zMaom4tEtIcMqfrWCLvqevE1coXeVVau9Gw== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id D7E8D2029E for ; Fri, 11 Oct 2024 16:58:06 +0000 (UTC) Received: from panther.cherry.relay.mailchannels.net (panther.cherry.relay.mailchannels.net [23.83.223.141]) by mail-a.sr.ht (Postfix) with ESMTPS id 423F220294 for <~johnnyrichard/olang-devel@lists.sr.ht>; Fri, 11 Oct 2024 16:57:31 +0000 (UTC) X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id D9B3D902C6D for <~johnnyrichard/olang-devel@lists.sr.ht>; Fri, 11 Oct 2024 16:57:29 +0000 (UTC) Received: from uk-fast-smtpout10.hostinger.io (100-99-173-91.trex-nlb.outbound.svc.cluster.local [100.99.173.91]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id 069CD902D97 for <~johnnyrichard/olang-devel@lists.sr.ht>; Fri, 11 Oct 2024 16:57:28 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1728665849; a=rsa-sha256; cv=none; b=kWAX8XsSyucY94wvkNd5zM4Kau6RZbP1kZ4x+1rob931jJhXhBWcJzkp1bG1BxTC5b2TJ5 7EVwQXPMxjcrMxkh/BtopLjXLN20241WLYCZtbg3bAyqfXNrVSj5yT4HvgHZQ1FFsPCzrb 0+vKVDMkvB36QaclM7Pm9DzCNm8ppTpR2pMZimAnXmPRtDhpY34G6bpdVPz2RQ1uY0999e tDJgMDGwK8rVul/wwuIAvj4h796cdws0cZRCrr7M5tOFn/a5QbeQyccZHy59moVioVc33C MOLbJvXvmNR1NBjgg1lLHDiV2ye9gNBShTtRjCHz8V6cYajIB6EmlxssVT6NGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1728665849; 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:dkim-signature; bh=Nbwmft1AhlauBywHTfbYn/Ss1keWxIs96q72JRpiXsA=; b=pQUllt8IP8qMRc0Xh17bUxk2dFSIDQODRgJL6ld3p69pKqZ6IJr/rWhNZrQy/lj3aeadqJ LxVk0I38gg2iy/TQhlhzktmPHP2H0TM2c0T+FCwNEeYnsyJs+lBnIeeKngIlyvWJK84yC5 2jEUlm3g9eln9ZXfVFR1EfWRN/U6T6elAaIfL80LS+6A/EFEwfQBZHlpFKmLF217/XrvIH p2iNFs4kCrssg4aWlP4EtM8oQtU0wfPpGWyOHjNhP0E97j+ICIy4nNEzN78t9D6Ofw4MVk EsVgqhbKlZ5dbbL0eYmIY/Xe1w2L4SkNsdcDxjR+q48VxxFH60NfpmpOqpB/yQ== ARC-Authentication-Results: i=1; rspamd-667df746d9-g7zjq; auth=pass smtp.auth=hostingeremail smtp.mailfrom=carlos@maniero.me X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me X-MC-Relay: Neutral X-MailChannels-SenderId: hostingeremail|x-authuser|carlos@maniero.me X-MailChannels-Auth-Id: hostingeremail X-Arithmetic-Rock: 32df87193f96eb55_1728665849820_1177415248 X-MC-Loop-Signature: 1728665849820:1105687367 X-MC-Ingress-Time: 1728665849820 Received: from uk-fast-smtpout10.hostinger.io (uk-fast-smtpout10.hostinger.io [145.14.155.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.99.173.91 (trex/7.0.2); Fri, 11 Oct 2024 16:57:29 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1728665847; 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=Nbwmft1AhlauBywHTfbYn/Ss1keWxIs96q72JRpiXsA=; b=s0py9hEJN7qPz5Kq3fImiYlJA1XzzVghXQTiDf+dMMRHKYsaFeFHt3W6igfSFSW2XJBfIS P5iOMPHZwrCWJCq7bCF91WeKdmqyMCPUyHvo3YrKTCSUTNZHFVBKdjjgxKlxtf4bRt0gv5 NI6eEWxAYJng+GPY9Ldtb3s0dMD72pCo044u+zDYRyh+19yGtGJjFyXTemJpzTYpYP30wP o6q2e8pJMmJOUh2GxIZkV5e6nflR37Y2pnnOVh7tGVUSvnVSY24+9cl+5gh40HPwcda/yz Y83cu0FEbCyQOjntAryVX6XzwPJzK6bOQQwsIx9XnVweRae3DWD5UNmFaJVrpA== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang v2 4/4] codestyle: limit the code to 80 characters Message-ID: <20241011165708.553265-5-carlos@maniero.me> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241011165708.553265-1-carlos@maniero.me> References: <20241011165708.553265-1-carlos@maniero.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Date: Fri, 11 Oct 2024 16:57:26 +0000 (UTC) X-CM-Envelope: MS4xfA7EoorzedwfCxugxZzi4+pkbLLKFceN/6w9HJb5NLT+TN+qv4CeKU8/rFR+w4aBuCbP35pw8CCnpB50iBzEdYahoYdAzieimzXa/ngvDnSi85xxbm6U CY+HV+wAf8XSC5wG2BOpG2WrpzTQb0LukeRPlZDu3VG2TcDS16CPWDKkuhDu8+k71aWo8HyXIf9QMLLWVpknOhqVw3sNB4S2lJX0hh698wwo33GYmwUhdDYh X-CM-Analysis: v=2.4 cv=TcnEtgQh c=1 sm=1 tr=0 ts=670958f7 a=WwxFCuf3mf1fs3oSi6/dng==:117 a=WwxFCuf3mf1fs3oSi6/dng==:17 a=IkcTkHD0fZMA:10 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=_8W3QpQxywzS9kSF71kA:9 a=3ZKOabzyN94A:10 a=QEXdDO2ut3YA:10 a=Aoyea0QO1OMM0vxGbGWR:22 X-AuthUser: carlos@maniero.me 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-Spam-Score: -1.29 X-Spam-Score: -1.29 X-Migadu-Queue-Id: 3A94E7197 X-Migadu-Scanner: mx12.migadu.com X-TUID: IAWpwMwR/jfw Signed-off-by: Carlos Maniero --- .clang-format | 4 +- src/ast.c | 60 ++++-- src/ast.h | 38 +++- src/checker.c | 13 +- src/cli.c | 29 +-- src/codegen_linux_x86_64.c | 339 ++++++++++++++++++++++++---------- src/codegen_linux_x86_64.h | 3 +- src/lexer.c | 46 +++-- src/main.c | 19 +- src/map.c | 12 +- src/parser.c | 73 ++++++-- src/pretty_print_ast.c | 81 +++++--- src/scope.c | 9 +- tests/unit/arena_test.c | 26 ++- tests/unit/list_test.c | 33 +++- tests/unit/map_test.c | 20 +- tests/unit/string_view_test.c | 26 ++- 17 files changed, 608 insertions(+), 223 deletions(-) diff --git a/.clang-format b/.clang-format index fa8dbc8..f7c0cc7 100644 --- a/.clang-format +++ b/.clang-format @@ -77,7 +77,7 @@ BreakConstructorInitializersBeforeComma: false BreakConstructorInitializers: BeforeComma BreakAfterJavaFieldAnnotations: false BreakStringLiterals: true -ColumnLimit: 120 +ColumnLimit: 80 CommentPragmas: '^ IWYU pragma:' QualifierAlignment: Leave CompactNamespaces: false @@ -145,7 +145,7 @@ ObjCSpaceBeforeProtocolList: false PenaltyBreakAssignment: 2 PenaltyBreakBeforeFirstCallParameter: 19 PenaltyBreakComment: 300 -PenaltyBreakFirstLessLess: 120 +PenaltyBreakFirstLessLess: 80 PenaltyBreakOpenParenthesis: 0 PenaltyBreakString: 1000 PenaltyBreakTemplateDeclaration: 10 diff --git a/src/ast.c b/src/ast.c index c64e660..800239e 100644 --- a/src/ast.c +++ b/src/ast.c @@ -51,7 +51,8 @@ ast_new_node_fn_def(arena_t *arena, assert(params); assert(block); - ast_node_t *node_fn_def = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); + ast_node_t *node_fn_def = + (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); assert(node_fn_def); node_fn_def->kind = AST_NODE_FN_DEF; @@ -67,12 +68,16 @@ ast_new_node_fn_def(arena_t *arena, } ast_node_t * -ast_new_node_fn_call(arena_t *arena, token_loc_t loc, string_view_t id, list_t *args) +ast_new_node_fn_call(arena_t *arena, + token_loc_t loc, + string_view_t id, + list_t *args) { assert(arena); assert(args); - ast_node_t *node_fn_call = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); + ast_node_t *node_fn_call = + (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); assert(node_fn_call); node_fn_call->kind = AST_NODE_FN_CALL; @@ -86,9 +91,14 @@ ast_new_node_fn_call(arena_t *arena, token_loc_t loc, string_view_t id, list_t * } ast_node_t * -ast_new_node_var_def(arena_t *arena, token_loc_t loc, string_view_t id, type_t *type, ast_node_t *value) +ast_new_node_var_def(arena_t *arena, + token_loc_t loc, + string_view_t id, + type_t *type, + ast_node_t *value) { - ast_node_t *node_var_def = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); + ast_node_t *node_var_def = + (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); assert(node_var_def); node_var_def->kind = AST_NODE_VAR_DEF; @@ -103,9 +113,14 @@ ast_new_node_var_def(arena_t *arena, token_loc_t loc, string_view_t id, type_t * } ast_node_t * -ast_new_node_bin_op(arena_t *arena, token_loc_t loc, ast_binary_op_kind_t kind, ast_node_t *lhs, ast_node_t *rhs) +ast_new_node_bin_op(arena_t *arena, + token_loc_t loc, + ast_binary_op_kind_t kind, + ast_node_t *lhs, + ast_node_t *rhs) { - ast_node_t *node_bin_op = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); + ast_node_t *node_bin_op = + (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); assert(node_bin_op); node_bin_op->kind = AST_NODE_BINARY_OP; @@ -118,9 +133,13 @@ ast_new_node_bin_op(arena_t *arena, token_loc_t loc, ast_binary_op_kind_t kind, } ast_node_t * -ast_new_node_unary_op(arena_t *arena, token_loc_t loc, ast_unary_op_kind_t kind, ast_node_t *expr) +ast_new_node_unary_op(arena_t *arena, + token_loc_t loc, + ast_unary_op_kind_t kind, + ast_node_t *expr) { - ast_node_t *node_unary_op = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); + ast_node_t *node_unary_op = + (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); assert(node_unary_op); node_unary_op->kind = AST_NODE_UNARY_OP; @@ -134,7 +153,8 @@ ast_new_node_unary_op(arena_t *arena, token_loc_t loc, ast_unary_op_kind_t kind, ast_node_t * ast_new_node_literal_u32(arena_t *arena, token_loc_t loc, uint32_t value) { - ast_node_t *node_literal = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); + ast_node_t *node_literal = + (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); assert(node_literal); node_literal->kind = AST_NODE_LITERAL; @@ -161,7 +181,8 @@ ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t id) ast_node_t * ast_new_node_return_stmt(arena_t *arena, token_loc_t loc, ast_node_t *expr) { - ast_node_t *node_return_stmt = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); + ast_node_t *node_return_stmt = + (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); assert(node_return_stmt); node_return_stmt->kind = AST_NODE_RETURN_STMT; @@ -172,7 +193,11 @@ ast_new_node_return_stmt(arena_t *arena, token_loc_t loc, ast_node_t *expr) } ast_node_t * -ast_new_node_if_stmt(arena_t *arena, token_loc_t loc, ast_node_t *cond, ast_node_t *then, ast_node_t *_else) +ast_new_node_if_stmt(arena_t *arena, + token_loc_t loc, + ast_node_t *cond, + ast_node_t *then, + ast_node_t *_else) { ast_node_t *node_if_stmt = arena_alloc(arena, sizeof(ast_node_t)); assert(node_if_stmt); @@ -187,7 +212,10 @@ ast_new_node_if_stmt(arena_t *arena, token_loc_t loc, ast_node_t *cond, ast_node } ast_node_t * -ast_new_node_while_stmt(arena_t *arena, token_loc_t loc, ast_node_t *cond, ast_node_t *then) +ast_new_node_while_stmt(arena_t *arena, + token_loc_t loc, + ast_node_t *cond, + ast_node_t *then) { ast_node_t *node_while_stmt = arena_alloc(arena, sizeof(ast_node_t)); assert(node_while_stmt); @@ -203,7 +231,8 @@ ast_new_node_while_stmt(arena_t *arena, token_loc_t loc, ast_node_t *cond, ast_n ast_node_t * ast_new_node_block(arena_t *arena) { - ast_node_t *node_block = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); + ast_node_t *node_block = + (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); assert(node_block); node_block->kind = AST_NODE_BLOCK; @@ -219,7 +248,8 @@ ast_new_node_block(arena_t *arena) ast_fn_param_t * ast_new_fn_param(arena_t *arena, string_view_t id, type_t *type) { - ast_fn_param_t *fn_param = (ast_fn_param_t *)arena_alloc(arena, sizeof(ast_fn_param_t)); + ast_fn_param_t *fn_param = + (ast_fn_param_t *)arena_alloc(arena, sizeof(ast_fn_param_t)); assert(fn_param); fn_param->id = id; diff --git a/src/ast.h b/src/ast.h index 6da11cf..217a264 100644 --- a/src/ast.h +++ b/src/ast.h @@ -229,16 +229,30 @@ ast_new_node_fn_def(arena_t *arena, ast_node_t *block); ast_node_t * -ast_new_node_fn_call(arena_t *arena, token_loc_t loc, string_view_t id, list_t *args); +ast_new_node_fn_call(arena_t *arena, + token_loc_t loc, + string_view_t id, + list_t *args); ast_node_t * -ast_new_node_var_def(arena_t *arena, token_loc_t loc, string_view_t id, type_t *type, ast_node_t *value); +ast_new_node_var_def(arena_t *arena, + token_loc_t loc, + string_view_t id, + type_t *type, + ast_node_t *value); ast_node_t * -ast_new_node_bin_op(arena_t *arena, token_loc_t loc, ast_binary_op_kind_t kind, ast_node_t *lhs, ast_node_t *rhs); +ast_new_node_bin_op(arena_t *arena, + token_loc_t loc, + ast_binary_op_kind_t kind, + ast_node_t *lhs, + ast_node_t *rhs); ast_node_t * -ast_new_node_unary_op(arena_t *arena, token_loc_t loc, ast_unary_op_kind_t kind, ast_node_t *expr); +ast_new_node_unary_op(arena_t *arena, + token_loc_t loc, + ast_unary_op_kind_t kind, + ast_node_t *expr); ast_node_t * ast_new_node_literal_u32(arena_t *arena, token_loc_t loc, uint32_t value); @@ -247,16 +261,26 @@ ast_node_t * ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t id); ast_node_t * -ast_new_node_var_assign_stmt(arena_t *arena, token_loc_t loc, ast_node_t *ref, ast_node_t *expr); +ast_new_node_var_assign_stmt(arena_t *arena, + token_loc_t loc, + ast_node_t *ref, + ast_node_t *expr); ast_node_t * ast_new_node_return_stmt(arena_t *arena, token_loc_t loc, ast_node_t *expr); ast_node_t * -ast_new_node_if_stmt(arena_t *arena, token_loc_t loc, ast_node_t *cond, ast_node_t *then, ast_node_t *_else); +ast_new_node_if_stmt(arena_t *arena, + token_loc_t loc, + ast_node_t *cond, + ast_node_t *then, + ast_node_t *_else); ast_node_t * -ast_new_node_while_stmt(arena_t *arena, token_loc_t loc, ast_node_t *cond, ast_node_t *then); +ast_new_node_while_stmt(arena_t *arena, + token_loc_t loc, + ast_node_t *cond, + ast_node_t *then); ast_node_t * ast_new_node_block(arena_t *arena); diff --git a/src/checker.c b/src/checker.c index e06cfe9..1230c3c 100644 --- a/src/checker.c +++ b/src/checker.c @@ -30,7 +30,9 @@ checker_new(arena_t *arena) checker_t *checker = (checker_t *)arena_alloc(arena, sizeof(checker_t)); if (checker == NULL) { - fprintf(stderr, "[FATAL] Out of memory: checker_new: %s\n", strerror(errno)); + fprintf(stderr, + "[FATAL] Out of memory: checker_new: %s\n", + strerror(errno)); exit(EXIT_FAILURE); } checker->arena = arena; @@ -119,7 +121,8 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast) fn_def->scope = scope_push(scope); type_resolve(fn_def->return_type); - symbol_t *symbol = symbol_new(checker->arena, fn_def->id, fn_def->return_type); + symbol_t *symbol = + symbol_new(checker->arena, fn_def->id, fn_def->return_type); scope_insert(scope, symbol); list_item_t *item = list_head(fn_def->params); @@ -128,7 +131,8 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast) ast_fn_param_t *param = (ast_fn_param_t *)item->value; type_resolve(param->type); - symbol_t *symbol = symbol_new(checker->arena, param->id, param->type); + symbol_t *symbol = + symbol_new(checker->arena, param->id, param->type); scope_insert(fn_def->scope, symbol); item = list_next(item); @@ -210,7 +214,8 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast) type_resolve(ast->as_var_def.type); - symbol_t *symbol = symbol_new(checker->arena, id, ast->as_var_def.type); + symbol_t *symbol = + symbol_new(checker->arena, id, ast->as_var_def.type); scope_insert(scope, symbol); ast->as_var_def.scope = scope; diff --git a/src/cli.c b/src/cli.c index d6b69e1..2f61acb 100644 --- a/src/cli.c +++ b/src/cli.c @@ -65,7 +65,8 @@ cli_parse_args(int argc, char **argv) arg = cli_args_shift(&args); } - if (opts.options & CLI_OPT_OUTPUT || opts.options & CLI_OPT_DUMP_TOKENS || opts.options & CLI_OPT_DUMP_AST) { + if (opts.options & CLI_OPT_OUTPUT || opts.options & CLI_OPT_DUMP_TOKENS || + opts.options & CLI_OPT_DUMP_AST) { return opts; } @@ -110,7 +111,9 @@ cli_opts_parse_arch(cli_opts_t *opts, cli_args_t *args) char *arch = cli_args_shift(args); if (arch == NULL) { - fprintf(stderr, "error: missing architecture for arg '--arch': available options (x86_64 | aarch64)\n"); + fprintf(stderr, + "error: missing architecture for arg '--arch': available " + "options (x86_64 | aarch64)\n"); cli_print_usage(stderr, opts->compiler_path); exit(EXIT_FAILURE); } @@ -140,14 +143,16 @@ cli_opts_parse_sysroot(cli_opts_t *opts, cli_args_t *args) void cli_print_usage(FILE *stream, char *compiler_path) { - fprintf(stream, - "Usage: %s [options] file...\n" - "Options:\n" - " --dump-tokens Display lexer token stream\n" - " --dump-ast Display ast tree to stdout\n" - " --arch Binary arch: default to x86_64 (x86_64 | aarch64)\n" - " --sysroot System root dir where the GNU Assembler and GNU Linker are located: default to '/'\n" - " -o Compile program into a binary file\n" - " --save-temps Keep temp files used to compile program\n", - compiler_path); + fprintf( + stream, + "Usage: %s [options] file...\n" + "Options:\n" + " --dump-tokens Display lexer token stream\n" + " --dump-ast Display ast tree to stdout\n" + " --arch Binary arch: default to x86_64 (x86_64 | aarch64)\n" + " --sysroot System root dir where the GNU Assembler and GNU " + "Linker are located: default to '/'\n" + " -o Compile program into a binary file\n" + " --save-temps Keep temp files used to compile program\n", + compiler_path); } diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c index 8a73263..053963a 100644 --- a/src/codegen_linux_x86_64.c +++ b/src/codegen_linux_x86_64.c @@ -58,22 +58,28 @@ typedef enum x86_64_register_type * ────────────────────────────────────────────────────────────── * x86-64 rdi rsi rdx r10 r8 r9 - */ -static int x86_call_args[X86_CALL_ARG_SIZE] = { REG_DEST_IDX, REG_SRC_IDX, REG_DATA, REG_R10, REG_R8, REG_R9 }; +static int x86_call_args[X86_CALL_ARG_SIZE] = { REG_DEST_IDX, REG_SRC_IDX, + REG_DATA, REG_R10, + REG_R8, REG_R9 }; static void codegen_linux_x86_64_emit_start_entrypoint(codegen_x86_64_t *codegen); static void -codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_t *fn); +codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, + ast_fn_definition_t *fn); static void codegen_linux_x86_64_emit_if(codegen_x86_64_t *codegen, ast_if_stmt_t is_stmt); static void -codegen_linux_x86_64_put_stack_offset(codegen_x86_64_t *codegen, symbol_t *symbol, size_t offset); +codegen_linux_x86_64_put_stack_offset(codegen_x86_64_t *codegen, + symbol_t *symbol, + size_t offset); static size_t -codegen_linux_x86_64_get_stack_offset(codegen_x86_64_t *codegen, symbol_t *symbol); +codegen_linux_x86_64_get_stack_offset(codegen_x86_64_t *codegen, + symbol_t *symbol); static size_t type_to_bytes(type_t *type); @@ -94,7 +100,8 @@ codegen_linux_x86_64_init(codegen_x86_64_t *codegen, arena_t *arena, FILE *out) } void -codegen_linux_x86_64_emit_translation_unit(codegen_x86_64_t *codegen, ast_node_t *node) +codegen_linux_x86_64_emit_translation_unit(codegen_x86_64_t *codegen, + ast_node_t *node) { codegen->label_index = 0; codegen_linux_x86_64_emit_start_entrypoint(codegen); @@ -146,7 +153,8 @@ codegen_linux_x86_64_get_next_label(codegen_x86_64_t *codegen) typedef size_t size_in_bytes_t; static size_in_bytes_t -codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node) +codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, + ast_node_t *expr_node) { switch (expr_node->kind) { case AST_NODE_LITERAL: { @@ -163,11 +171,15 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr symbol_t *symbol = scope_lookup(ref.scope, ref.id); assert(symbol); - size_t offset = codegen_linux_x86_64_get_stack_offset(codegen, symbol); + size_t offset = + codegen_linux_x86_64_get_stack_offset(codegen, symbol); size_t bytes = type_to_bytes(symbol->type); - fprintf(codegen->out, " mov -%ld(%%rbp), %s\n", offset, get_reg_for(REG_ACCUMULATOR, bytes)); + fprintf(codegen->out, + " mov -%ld(%%rbp), %s\n", + offset, + get_reg_for(REG_ACCUMULATOR, bytes)); return bytes; } case AST_NODE_FN_CALL: { @@ -177,7 +189,8 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr assert(symbol); size_t i = 0; - for (list_item_t *item = list_head(fn_call.args); item != NULL; item = list_next(item)) { + for (list_item_t *item = list_head(fn_call.args); item != NULL; + item = list_next(item)) { // FIXME: add support for more args than X86_CALL_ARG_SIZE assert(i < X86_CALL_ARG_SIZE); @@ -185,12 +198,16 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr codegen_linux_x86_64_emit_expression(codegen, arg_node); - fprintf(codegen->out, " push %s\n", get_reg_for(REG_ACCUMULATOR, 8)); + fprintf(codegen->out, + " push %s\n", + get_reg_for(REG_ACCUMULATOR, 8)); ++i; } for (; i > 0; --i) { - fprintf(codegen->out, " pop %s\n", get_reg_for(x86_call_args[i - 1], 8)); + fprintf(codegen->out, + " pop %s\n", + get_reg_for(x86_call_args[i - 1], 8)); } fprintf(codegen->out, " call " SV_FMT "\n", SV_ARG(fn_call.id)); @@ -202,13 +219,18 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr switch (bin_op.kind) { case AST_BINOP_ADDITION: { fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t rhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); + size_in_bytes_t rhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); - size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); + size_in_bytes_t expr_bytes = + bytes_max(rhs_bytes, lhs_bytes); fprintf(codegen->out, " pop %%rcx\n"); fprintf(codegen->out, @@ -220,48 +242,69 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr } case AST_BINOP_MULTIPLICATION: { fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t rhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); + size_in_bytes_t rhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); - size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); + size_in_bytes_t expr_bytes = + bytes_max(rhs_bytes, lhs_bytes); fprintf(codegen->out, " pop %%rcx\n"); - fprintf(codegen->out, " mul %s\n", get_reg_for(REG_COUNTER, expr_bytes)); + fprintf(codegen->out, + " mul %s\n", + get_reg_for(REG_COUNTER, expr_bytes)); return expr_bytes; } case AST_BINOP_DIVISION: { fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t rhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); + size_in_bytes_t rhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); - size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); + size_in_bytes_t expr_bytes = + bytes_max(rhs_bytes, lhs_bytes); fprintf(codegen->out, " pop %%rcx\n"); fprintf(codegen->out, " xor %%rdx, %%rdx\n"); - fprintf(codegen->out, " div %s\n", get_reg_for(REG_COUNTER, expr_bytes)); + fprintf(codegen->out, + " div %s\n", + get_reg_for(REG_COUNTER, expr_bytes)); return expr_bytes; } case AST_BINOP_REMINDER: { fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t rhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); + size_in_bytes_t rhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); - size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); + size_in_bytes_t expr_bytes = + bytes_max(rhs_bytes, lhs_bytes); fprintf(codegen->out, " pop %%rcx\n"); fprintf(codegen->out, " xor %%rdx, %%rdx\n"); - fprintf(codegen->out, " div %s\n", get_reg_for(REG_COUNTER, expr_bytes)); + fprintf(codegen->out, + " div %s\n", + get_reg_for(REG_COUNTER, expr_bytes)); fprintf(codegen->out, " mov %s, %s\n", get_reg_for(REG_DATA, expr_bytes), @@ -271,13 +314,18 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr } case AST_BINOP_SUBTRACTION: { fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t rhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); + size_in_bytes_t rhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); - size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); + size_in_bytes_t expr_bytes = + bytes_max(rhs_bytes, lhs_bytes); fprintf(codegen->out, " pop %%rcx\n"); fprintf(codegen->out, @@ -289,13 +337,18 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr } case AST_BINOP_CMP_EQ: { fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t rhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); + size_in_bytes_t rhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); - size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); + size_in_bytes_t expr_bytes = + bytes_max(rhs_bytes, lhs_bytes); fprintf(codegen->out, " pop %%rcx\n"); fprintf(codegen->out, @@ -303,19 +356,26 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr get_reg_for(REG_COUNTER, expr_bytes), get_reg_for(REG_ACCUMULATOR, expr_bytes)); fprintf(codegen->out, " sete %%al\n"); - fprintf(codegen->out, " movzb %%al, %s\n", get_reg_for(REG_ACCUMULATOR, expr_bytes)); + fprintf(codegen->out, + " movzb %%al, %s\n", + get_reg_for(REG_ACCUMULATOR, expr_bytes)); return expr_bytes; } case AST_BINOP_CMP_LT: { fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t rhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); + size_in_bytes_t rhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); - size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); + size_in_bytes_t expr_bytes = + bytes_max(rhs_bytes, lhs_bytes); fprintf(codegen->out, " pop %%rcx\n"); fprintf(codegen->out, @@ -323,19 +383,26 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr get_reg_for(REG_COUNTER, expr_bytes), get_reg_for(REG_ACCUMULATOR, expr_bytes)); fprintf(codegen->out, " setl %%al\n"); - fprintf(codegen->out, " movzb %%al, %s\n", get_reg_for(REG_ACCUMULATOR, expr_bytes)); + fprintf(codegen->out, + " movzb %%al, %s\n", + get_reg_for(REG_ACCUMULATOR, expr_bytes)); return expr_bytes; } case AST_BINOP_CMP_GT: { fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t rhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); + size_in_bytes_t rhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); - size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); + size_in_bytes_t expr_bytes = + bytes_max(rhs_bytes, lhs_bytes); fprintf(codegen->out, " pop %%rcx\n"); fprintf(codegen->out, @@ -343,19 +410,26 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr get_reg_for(REG_COUNTER, expr_bytes), get_reg_for(REG_ACCUMULATOR, expr_bytes)); fprintf(codegen->out, " setg %%al\n"); - fprintf(codegen->out, " movzb %%al, %s\n", get_reg_for(REG_ACCUMULATOR, expr_bytes)); + fprintf(codegen->out, + " movzb %%al, %s\n", + get_reg_for(REG_ACCUMULATOR, expr_bytes)); return expr_bytes; } case AST_BINOP_CMP_NEQ: { fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t rhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); + size_in_bytes_t rhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); - size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); + size_in_bytes_t expr_bytes = + bytes_max(rhs_bytes, lhs_bytes); fprintf(codegen->out, " pop %%rcx\n"); fprintf(codegen->out, @@ -363,19 +437,26 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr get_reg_for(REG_COUNTER, expr_bytes), get_reg_for(REG_ACCUMULATOR, expr_bytes)); fprintf(codegen->out, " setne %%al\n"); - fprintf(codegen->out, " movzb %%al, %s\n", get_reg_for(REG_ACCUMULATOR, expr_bytes)); + fprintf(codegen->out, + " movzb %%al, %s\n", + get_reg_for(REG_ACCUMULATOR, expr_bytes)); return expr_bytes; } case AST_BINOP_CMP_LEQ: { fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t rhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); + size_in_bytes_t rhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); - size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); + size_in_bytes_t expr_bytes = + bytes_max(rhs_bytes, lhs_bytes); fprintf(codegen->out, " pop %%rcx\n"); fprintf(codegen->out, @@ -383,19 +464,26 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr get_reg_for(REG_COUNTER, expr_bytes), get_reg_for(REG_ACCUMULATOR, expr_bytes)); fprintf(codegen->out, " setle %%al\n"); - fprintf(codegen->out, " movzb %%al, %s\n", get_reg_for(REG_ACCUMULATOR, expr_bytes)); + fprintf(codegen->out, + " movzb %%al, %s\n", + get_reg_for(REG_ACCUMULATOR, expr_bytes)); return expr_bytes; } case AST_BINOP_CMP_GEQ: { fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t rhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); + size_in_bytes_t rhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); - size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); + size_in_bytes_t expr_bytes = + bytes_max(rhs_bytes, lhs_bytes); fprintf(codegen->out, " pop %%rcx\n"); fprintf(codegen->out, @@ -403,7 +491,9 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr get_reg_for(REG_COUNTER, expr_bytes), get_reg_for(REG_ACCUMULATOR, expr_bytes)); fprintf(codegen->out, " setge %%al\n"); - fprintf(codegen->out, " movzb %%al, %s\n", get_reg_for(REG_ACCUMULATOR, expr_bytes)); + fprintf(codegen->out, + " movzb %%al, %s\n", + get_reg_for(REG_ACCUMULATOR, expr_bytes)); return expr_bytes; } @@ -413,10 +503,14 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); fprintf(codegen->out, " pop %%rcx\n"); - fprintf(codegen->out, " shl %%cl, %s\n", get_reg_for(REG_ACCUMULATOR, lhs_bytes)); + fprintf(codegen->out, + " shl %%cl, %s\n", + get_reg_for(REG_ACCUMULATOR, lhs_bytes)); return lhs_bytes; } @@ -426,22 +520,31 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); fprintf(codegen->out, " pop %%rcx\n"); - fprintf(codegen->out, " shr %%cl, %s\n", get_reg_for(REG_ACCUMULATOR, lhs_bytes)); + fprintf(codegen->out, + " shr %%cl, %s\n", + get_reg_for(REG_ACCUMULATOR, lhs_bytes)); return lhs_bytes; } case AST_BINOP_BITWISE_XOR: { fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t rhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); + size_in_bytes_t rhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); - size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); + size_in_bytes_t expr_bytes = + bytes_max(rhs_bytes, lhs_bytes); fprintf(codegen->out, " pop %%rcx\n"); fprintf(codegen->out, @@ -453,13 +556,18 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr } case AST_BINOP_BITWISE_AND: { fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t rhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); + size_in_bytes_t rhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); - size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); + size_in_bytes_t expr_bytes = + bytes_max(rhs_bytes, lhs_bytes); fprintf(codegen->out, " pop %%rcx\n"); fprintf(codegen->out, @@ -471,13 +579,18 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr } case AST_BINOP_BITWISE_OR: { fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t rhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); + size_in_bytes_t rhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.rhs); fprintf(codegen->out, " push %%rax\n"); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); - size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes); + size_in_bytes_t expr_bytes = + bytes_max(rhs_bytes, lhs_bytes); fprintf(codegen->out, " pop %%rcx\n"); fprintf(codegen->out, @@ -488,16 +601,25 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr return expr_bytes; } case AST_BINOP_LOGICAL_AND: { - size_t label_exit = codegen_linux_x86_64_get_next_label(codegen); + size_t label_exit = + codegen_linux_x86_64_get_next_label(codegen); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); - fprintf(codegen->out, " cmp $0, %s\n", get_reg_for(REG_ACCUMULATOR, lhs_bytes)); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); + fprintf(codegen->out, + " cmp $0, %s\n", + get_reg_for(REG_ACCUMULATOR, lhs_bytes)); fprintf(codegen->out, " je .L%ld\n", label_exit); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t rhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); - fprintf(codegen->out, " cmp $0, %s\n", get_reg_for(REG_ACCUMULATOR, rhs_bytes)); + size_in_bytes_t rhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.rhs); + fprintf(codegen->out, + " cmp $0, %s\n", + get_reg_for(REG_ACCUMULATOR, rhs_bytes)); fprintf(codegen->out, " je .L%ld\n", label_exit); fprintf(codegen->out, " mov $1, %%rax\n"); fprintf(codegen->out, ".L%ld:\n", label_exit); @@ -505,17 +627,27 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr return 1; } case AST_BINOP_LOGICAL_OR: { - size_t label_t = codegen_linux_x86_64_get_next_label(codegen); - size_t label_f = codegen_linux_x86_64_get_next_label(codegen); + size_t label_t = + codegen_linux_x86_64_get_next_label(codegen); + size_t label_f = + codegen_linux_x86_64_get_next_label(codegen); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t lhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs); - fprintf(codegen->out, " cmp $0, %s\n", get_reg_for(REG_ACCUMULATOR, lhs_bytes)); + size_in_bytes_t lhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.lhs); + fprintf(codegen->out, + " cmp $0, %s\n", + get_reg_for(REG_ACCUMULATOR, lhs_bytes)); fprintf(codegen->out, " jne .L%ld\n", label_t); fprintf(codegen->out, " xor %%rax, %%rax\n"); - size_in_bytes_t rhs_bytes = codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); - fprintf(codegen->out, " cmp $0, %s\n", get_reg_for(REG_ACCUMULATOR, rhs_bytes)); + size_in_bytes_t rhs_bytes = + codegen_linux_x86_64_emit_expression(codegen, + bin_op.rhs); + fprintf(codegen->out, + " cmp $0, %s\n", + get_reg_for(REG_ACCUMULATOR, rhs_bytes)); fprintf(codegen->out, " je .L%ld\n", label_f); fprintf(codegen->out, ".L%ld:\n", label_t); @@ -532,12 +664,16 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr symbol_t *symbol = scope_lookup(scope, ref.id); assert(symbol); - size_t offset = codegen_linux_x86_64_get_stack_offset(codegen, symbol); + size_t offset = + codegen_linux_x86_64_get_stack_offset(codegen, symbol); codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs); size_t type_size = type_to_bytes(symbol->type); - fprintf(codegen->out, " mov %s, -%ld(%%rbp)\n", get_reg_for(REG_ACCUMULATOR, type_size), offset); + fprintf(codegen->out, + " mov %s, -%ld(%%rbp)\n", + get_reg_for(REG_ACCUMULATOR, type_size), + offset); // FIXME: we don't support a = b = c return 0; @@ -553,9 +689,13 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr ast_unary_op_t unary_op = expr_node->as_unary_op; switch (unary_op.kind) { case AST_UNARY_BITWISE_NOT: { - size_in_bytes_t expr_bytes = codegen_linux_x86_64_emit_expression(codegen, unary_op.expr); + size_in_bytes_t expr_bytes = + codegen_linux_x86_64_emit_expression(codegen, + unary_op.expr); - fprintf(codegen->out, " not %s\n", get_reg_for(REG_ACCUMULATOR, expr_bytes)); + fprintf(codegen->out, + " not %s\n", + get_reg_for(REG_ACCUMULATOR, expr_bytes)); return expr_bytes; } @@ -600,10 +740,12 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) symbol_t *symbol = scope_lookup(scope, var_def.id); assert(symbol); - codegen_linux_x86_64_put_stack_offset(codegen, symbol, codegen->base_offset); + codegen_linux_x86_64_put_stack_offset( + codegen, symbol, codegen->base_offset); if (var_def.value) { - codegen_linux_x86_64_emit_expression(codegen, var_def.value); + codegen_linux_x86_64_emit_expression(codegen, + var_def.value); } size_t type_size = type_to_bytes(symbol->type); @@ -633,7 +775,8 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) ast_node_t *cond = while_stmt.cond; ast_node_t *then = while_stmt.then; - size_t begin_label = codegen_linux_x86_64_get_next_label(codegen); + size_t begin_label = + codegen_linux_x86_64_get_next_label(codegen); size_t end_label = codegen_linux_x86_64_get_next_label(codegen); fprintf(codegen->out, ".L%ld:\n", begin_label); @@ -641,7 +784,8 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) fprintf(codegen->out, " cmp $1, %%rax\n"); fprintf(codegen->out, " jnz .L%ld\n", end_label); - assert(then->kind == AST_NODE_BLOCK && "invalid while-then block"); + assert(then->kind == AST_NODE_BLOCK && + "invalid while-then block"); ast_block_t then_block = then->as_block; codegen_linux_x86_64_emit_block(codegen, &then_block); @@ -652,7 +796,8 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) break; } default: { - // FIXME: improve error: replace the node->kind to a string representation + // FIXME: improve error: replace the node->kind to a string + // representation fprintf(stderr, "node kind %d not supported\n", node->kind); assert(0 && "unsupported block statement"); break; @@ -709,7 +854,8 @@ type_to_bytes(type_t *type) return 8; } case TYPE_UNKNOWN: { - assert(0 && "cannot calculate size of an unknown type: probably a parser issue."); + assert(0 && "cannot calculate size of an unknown type: probably a " + "parser issue."); } } @@ -739,7 +885,8 @@ calculate_fn_local_size(scope_t *scope) list_item_t *item = list_head(scope->children); while (item != NULL) { - size_t child_local_size = calculate_fn_local_size((scope_t *)item->value); + size_t child_local_size = + calculate_fn_local_size((scope_t *)item->value); if (child_local_size > max_child_local_size) { max_child_local_size = child_local_size; @@ -752,7 +899,8 @@ calculate_fn_local_size(scope_t *scope) } static void -codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_t *fn_def) +codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, + ast_fn_definition_t *fn_def) { codegen->base_offset = X86_CALL_EIP_STACK_OFFSET; @@ -763,7 +911,8 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_ fprintf(codegen->out, " mov %%rsp, %%rbp\n"); size_t i = 0; - for (list_item_t *item = list_head(fn_def->params); item != NULL; item = list_next(item)) { + for (list_item_t *item = list_head(fn_def->params); item != NULL; + item = list_next(item)) { assert(i < X86_CALL_ARG_SIZE); ast_fn_param_t *param = item->value; @@ -773,7 +922,8 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_ size_t offset = codegen->base_offset; - codegen_linux_x86_64_put_stack_offset(codegen, symbol, codegen->base_offset); + codegen_linux_x86_64_put_stack_offset( + codegen, symbol, codegen->base_offset); fprintf(codegen->out, " mov %s, -%ld(%%rbp)\n", @@ -799,7 +949,9 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_ } static void -codegen_linux_x86_64_put_stack_offset(codegen_x86_64_t *codegen, symbol_t *symbol, size_t offset) +codegen_linux_x86_64_put_stack_offset(codegen_x86_64_t *codegen, + symbol_t *symbol, + size_t offset) { size_t *stack_offset = arena_alloc(codegen->arena, sizeof(size_t)); @@ -812,7 +964,8 @@ codegen_linux_x86_64_put_stack_offset(codegen_x86_64_t *codegen, symbol_t *symbo } static size_t -codegen_linux_x86_64_get_stack_offset(codegen_x86_64_t *codegen, symbol_t *symbol) +codegen_linux_x86_64_get_stack_offset(codegen_x86_64_t *codegen, + symbol_t *symbol) { char symbol_ptr[PTR_HEX_CSTR_SIZE]; sprintf(symbol_ptr, "%lx", (uintptr_t)symbol); diff --git a/src/codegen_linux_x86_64.h b/src/codegen_linux_x86_64.h index a4137de..7e2841d 100644 --- a/src/codegen_linux_x86_64.h +++ b/src/codegen_linux_x86_64.h @@ -35,6 +35,7 @@ void codegen_linux_x86_64_init(codegen_x86_64_t *codegen, arena_t *arena, FILE *out); void -codegen_linux_x86_64_emit_translation_unit(codegen_x86_64_t *codegen, ast_node_t *prog); +codegen_linux_x86_64_emit_translation_unit(codegen_x86_64_t *codegen, + ast_node_t *prog); #endif /* CODEGEN_X86_64_H */ diff --git a/src/lexer.c b/src/lexer.c index 03763de..1a6c24b 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -50,7 +50,10 @@ static void lexer_init_char_value_token(lexer_t *lexer, token_t *token, token_kind_t kind); static void -lexer_init_str_value_token(lexer_t *lexer, token_t *token, token_kind_t kind, lexer_cursor_t cur); +lexer_init_str_value_token(lexer_t *lexer, + token_t *token, + token_kind_t kind, + lexer_cursor_t cur); static void lexer_init_eof_token(lexer_t *lexer, token_t *token); @@ -95,7 +98,8 @@ lexer_next_token(lexer_t *lexer, token_t *token) .size = lexer->cur.offset - start_cur.offset, }; - lexer_init_str_value_token(lexer, token, lexer_str_to_token_kind(text), start_cur); + lexer_init_str_value_token( + lexer, token, lexer_str_to_token_kind(text), start_cur); return; } @@ -117,7 +121,8 @@ lexer_next_token(lexer_t *lexer, token_t *token) if (lexer_current_char(lexer) == '=') { lexer_skip_char(lexer); - lexer_init_str_value_token(lexer, token, TOKEN_CMP_EQ, start_cur); + lexer_init_str_value_token( + lexer, token, TOKEN_CMP_EQ, start_cur); return; } @@ -130,7 +135,8 @@ lexer_next_token(lexer_t *lexer, token_t *token) if (lexer_current_char(lexer) == '=') { lexer_skip_char(lexer); - lexer_init_str_value_token(lexer, token, TOKEN_CMP_NEQ, start_cur); + lexer_init_str_value_token( + lexer, token, TOKEN_CMP_NEQ, start_cur); return; } @@ -143,7 +149,8 @@ lexer_next_token(lexer_t *lexer, token_t *token) if (lexer_current_char(lexer) == '&') { lexer_skip_char(lexer); - lexer_init_str_value_token(lexer, token, TOKEN_LOGICAL_AND, start_cur); + lexer_init_str_value_token( + lexer, token, TOKEN_LOGICAL_AND, start_cur); return; } @@ -156,7 +163,8 @@ lexer_next_token(lexer_t *lexer, token_t *token) if (lexer_current_char(lexer) == '|') { lexer_skip_char(lexer); - lexer_init_str_value_token(lexer, token, TOKEN_LOGICAL_OR, start_cur); + lexer_init_str_value_token( + lexer, token, TOKEN_LOGICAL_OR, start_cur); return; } @@ -170,16 +178,19 @@ lexer_next_token(lexer_t *lexer, token_t *token) switch (lexer_current_char(lexer)) { case '<': { lexer_skip_char(lexer); - lexer_init_str_value_token(lexer, token, TOKEN_BITWISE_LSHIFT, start_cur); + lexer_init_str_value_token( + lexer, token, TOKEN_BITWISE_LSHIFT, start_cur); return; } case '=': { lexer_skip_char(lexer); - lexer_init_str_value_token(lexer, token, TOKEN_CMP_LEQ, start_cur); + lexer_init_str_value_token( + lexer, token, TOKEN_CMP_LEQ, start_cur); return; } default: { - lexer_init_str_value_token(lexer, token, TOKEN_LT, start_cur); + lexer_init_str_value_token( + lexer, token, TOKEN_LT, start_cur); return; } } @@ -191,16 +202,19 @@ lexer_next_token(lexer_t *lexer, token_t *token) switch (lexer_current_char(lexer)) { case '>': { lexer_skip_char(lexer); - lexer_init_str_value_token(lexer, token, TOKEN_BITWISE_RSHIFT, start_cur); + lexer_init_str_value_token( + lexer, token, TOKEN_BITWISE_RSHIFT, start_cur); return; } case '=': { lexer_skip_char(lexer); - lexer_init_str_value_token(lexer, token, TOKEN_CMP_GEQ, start_cur); + lexer_init_str_value_token( + lexer, token, TOKEN_CMP_GEQ, start_cur); return; } default: { - lexer_init_str_value_token(lexer, token, TOKEN_GT, start_cur); + lexer_init_str_value_token( + lexer, token, TOKEN_GT, start_cur); return; } } @@ -421,7 +435,10 @@ lexer_init_char_value_token(lexer_t *lexer, token_t *token, token_kind_t kind) } static void -lexer_init_str_value_token(lexer_t *lexer, token_t *token, token_kind_t kind, lexer_cursor_t cur) +lexer_init_str_value_token(lexer_t *lexer, + token_t *token, + token_kind_t kind, + lexer_cursor_t cur) { string_view_t str = { .chars = lexer->src.code.chars + cur.offset, @@ -510,7 +527,8 @@ token_loc_to_line(token_loc_t loc) .size = 0, }; - while ((line.size + offset) < loc.src.code.size && line.chars[line.size] != '\n' && line.chars[line.size] != 0) { + while ((line.size + offset) < loc.src.code.size && + line.chars[line.size] != '\n' && line.chars[line.size] != 0) { ++line.size; } diff --git a/src/main.c b/src/main.c index aa09f34..2b02564 100644 --- a/src/main.c +++ b/src/main.c @@ -158,7 +158,8 @@ handle_codegen_linux(cli_opts_t *opts) } else if (strcmp(opts->arch, "aarch64") == 0) { codegen_linux_aarch64_emit_translation_unit(out, ast); } else { - fprintf(stderr, "error: architecture '%s' not supported\n", opts->arch); + fprintf( + stderr, "error: architecture '%s' not supported\n", opts->arch); cli_print_usage(stderr, opts->compiler_path); exit(EXIT_FAILURE); } @@ -171,7 +172,11 @@ handle_codegen_linux(cli_opts_t *opts) } char command[512]; - sprintf(command, "%s/bin/as %s -o " SV_FMT ".o", opts->sysroot, asm_file, SV_ARG(opts->output_bin)); + sprintf(command, + "%s/bin/as %s -o " SV_FMT ".o", + opts->sysroot, + asm_file, + SV_ARG(opts->output_bin)); int exit_code = system(command); @@ -210,7 +215,10 @@ read_entire_file(char *filepath, arena_t *arena) FILE *stream = fopen(filepath, "rb"); if (stream == NULL) { - fprintf(stderr, "error: could not open file %s: %s\n", filepath, strerror(errno)); + fprintf(stderr, + "error: could not open file %s: %s\n", + filepath, + strerror(errno)); exit(EXIT_FAILURE); } @@ -225,7 +233,10 @@ read_entire_file(char *filepath, arena_t *arena) code.chars = (char *)arena_alloc(arena, (size_t)code.size); if (code.chars == NULL) { - fprintf(stderr, "error: could not read file %s: %s\n", filepath, strerror(errno)); + fprintf(stderr, + "error: could not read file %s: %s\n", + filepath, + strerror(errno)); exit(EXIT_FAILURE); } diff --git a/src/map.c b/src/map.c index 42801b0..97db39e 100644 --- a/src/map.c +++ b/src/map.c @@ -42,7 +42,8 @@ map_new(arena_t *arena) { map_t *map = (map_t *)arena_alloc(arena, sizeof(map_t)); if (map == NULL) { - fprintf(stderr, "[FATAL] Out of memory: map_new: %s\n", strerror(errno)); + fprintf( + stderr, "[FATAL] Out of memory: map_new: %s\n", strerror(errno)); exit(EXIT_FAILURE); } map->arena = arena; @@ -54,11 +55,13 @@ static void map_init(map_t *map) { assert(map); - map->entries = (map_entry_t *)arena_alloc(map->arena, MAP_INITIAL_CAPACITY * sizeof(map_entry_t)); + map->entries = (map_entry_t *)arena_alloc( + map->arena, MAP_INITIAL_CAPACITY * sizeof(map_entry_t)); assert(map->entries != NULL); memset(map->entries, 0, MAP_INITIAL_CAPACITY * sizeof(map_entry_t)); if (map->entries == NULL) { - fprintf(stderr, "[FATAL] Out of memory: map_init: %s\n", strerror(errno)); + fprintf( + stderr, "[FATAL] Out of memory: map_init: %s\n", strerror(errno)); exit(EXIT_FAILURE); } map->capacity = MAP_INITIAL_CAPACITY; @@ -101,7 +104,8 @@ map_put(map_t *map, char *key, void *value) break; } if (entry->next == NULL) { - entry->next = (map_entry_t *)arena_alloc(map->arena, sizeof(map_entry_t)); + entry->next = + (map_entry_t *)arena_alloc(map->arena, sizeof(map_entry_t)); *entry->next = (map_entry_t){ .key = _strdup(key, map->arena), .hash = hash, diff --git a/src/parser.c b/src/parser.c index 177195d..4282f0a 100644 --- a/src/parser.c +++ b/src/parser.c @@ -150,7 +150,10 @@ token_kind_to_binary_op_kind(token_kind_t kind) case TOKEN_EQ: return AST_BINOP_ASSIGN; default: { - fprintf(stderr, "error: token kind (%s) not compatible with binary op kind\n", token_kind_to_cstr(kind)); + fprintf( + stderr, + "error: token kind (%s) not compatible with binary op kind\n", + token_kind_to_cstr(kind)); assert(false); } } @@ -228,7 +231,8 @@ token_kind_to_unary_op_kind(token_kind_t token_kind) case TOKEN_BANG: return AST_UNARY_LOGICAL_NOT; default: - assert(false && "unable to covert the token_kind_t to unary_op_kind_t"); + assert(false && + "unable to covert the token_kind_t to unary_op_kind_t"); } } @@ -251,12 +255,18 @@ parser_parse_expr_1(parser_t *parser, ast_node_t *lhs, size_t prev_precedence) lexer_peek_next(parser->lexer, &lookahead_token); while (token_kind_is_binary_op(lookahead_token.kind) && - get_binary_op_precedence(lookahead_token.kind) > get_binary_op_precedence(token_op.kind)) { - rhs = parser_parse_expr_1(parser, rhs, get_binary_op_precedence(token_op.kind)); + get_binary_op_precedence(lookahead_token.kind) > + get_binary_op_precedence(token_op.kind)) { + rhs = parser_parse_expr_1( + parser, rhs, get_binary_op_precedence(token_op.kind)); lexer_peek_next(parser->lexer, &lookahead_token); } - lhs = ast_new_node_bin_op(parser->arena, token_op.loc, token_kind_to_binary_op_kind(token_op.kind), lhs, rhs); + lhs = ast_new_node_bin_op(parser->arena, + token_op.loc, + token_kind_to_binary_op_kind(token_op.kind), + lhs, + rhs); if (lhs == NULL) { return NULL; } @@ -284,7 +294,8 @@ parser_parse_factor(parser_t *parser) switch (token.kind) { case TOKEN_NUMBER: - return ast_new_node_literal_u32(parser->arena, token.loc, string_view_to_u32(token.value)); + return ast_new_node_literal_u32( + parser->arena, token.loc, string_view_to_u32(token.value)); case TOKEN_ID: { token_t token_id = token; @@ -293,9 +304,11 @@ parser_parse_factor(parser_t *parser) if (token.kind == TOKEN_OPAREN) { list_t *args = parser_parse_fn_args(parser); - return ast_new_node_fn_call(parser->arena, token_id.loc, token_id.value, args); + return ast_new_node_fn_call( + parser->arena, token_id.loc, token_id.value, args); } - return ast_new_node_ref(parser->arena, token_id.loc, token_id.value); + return ast_new_node_ref( + parser->arena, token_id.loc, token_id.value); } case TOKEN_AND: case TOKEN_STAR: @@ -325,7 +338,9 @@ parser_parse_factor(parser_t *parser) return expr; } default: { - fprintf(stderr, "error: parse_factor: unsupported or invalid token (%s)\n", token_kind_to_cstr(token.kind)); + fprintf(stderr, + "error: parse_factor: unsupported or invalid token (%s)\n", + token_kind_to_cstr(token.kind)); assert(false); } } @@ -340,7 +355,9 @@ parser_parse_fn_args(parser_t *parser) list_t *args = arena_alloc(parser->arena, sizeof(list_t)); if (args == NULL) { - fprintf(stderr, "[FATAL] Out of memory: parser_parse_fn_args: %s\n", strerror(errno)); + fprintf(stderr, + "[FATAL] Out of memory: parser_parse_fn_args: %s\n", + strerror(errno)); exit(EXIT_FAILURE); } @@ -382,7 +399,9 @@ parser_parse_fn_params(parser_t *parser) list_t *params = arena_alloc(parser->arena, sizeof(list_t)); if (params == NULL) { - fprintf(stderr, "[FATAL] Out of memory: parser_parse_fn_params: %s\n", strerror(errno)); + fprintf(stderr, + "[FATAL] Out of memory: parser_parse_fn_params: %s\n", + strerror(errno)); exit(EXIT_FAILURE); } @@ -410,7 +429,8 @@ parser_parse_fn_params(parser_t *parser) return NULL; } - ast_fn_param_t *param = ast_new_fn_param(parser->arena, token.value, type); + ast_fn_param_t *param = + ast_new_fn_param(parser->arena, token.value, type); list_append(params, param); skip_line_feeds(parser->lexer); @@ -460,7 +480,12 @@ parser_parse_fn_definition(parser_t *parser) return NULL; } - return ast_new_node_fn_def(parser->arena, fn_name_token.loc, fn_name_token.value, params, ret_type, block); + return ast_new_node_fn_def(parser->arena, + fn_name_token.loc, + fn_name_token.value, + params, + ret_type, + block); } static type_t * @@ -492,7 +517,8 @@ parser_parse_type(parser_t *parser) } string_view_t ptr_id = token.value; - ptr_id.size = ptr_token.value.chars - token.value.chars + ptr_token.value.size; + ptr_id.size = + ptr_token.value.chars - token.value.chars + ptr_token.value.size; return type_new_ptr(parser->arena, ptr_id, type); } @@ -581,7 +607,8 @@ parser_parse_return_stmt(parser_t *parser) return NULL; } - ast_node_t *node_return_stmt = ast_new_node_return_stmt(parser->arena, token_ret.loc, expr); + ast_node_t *node_return_stmt = + ast_new_node_return_stmt(parser->arena, token_ret.loc, expr); assert(node_return_stmt); return node_return_stmt; @@ -632,7 +659,8 @@ parser_parse_if_stmt(parser_t *parser) } } - ast_node_t *node_if_stmt = ast_new_node_if_stmt(parser->arena, token_if.loc, cond, then, _else); + ast_node_t *node_if_stmt = + ast_new_node_if_stmt(parser->arena, token_if.loc, cond, then, _else); assert(node_if_stmt); @@ -664,7 +692,8 @@ parser_parse_while_stmt(parser_t *parser) token_t next_token; peek_next_non_lf_token(parser->lexer, &next_token); - ast_node_t *node_while_stmt = ast_new_node_while_stmt(parser->arena, token_while.loc, cond, then); + ast_node_t *node_while_stmt = + ast_new_node_while_stmt(parser->arena, token_while.loc, cond, then); assert(node_while_stmt); @@ -698,7 +727,8 @@ parser_parse_var_def(parser_t *parser) return NULL; } - ast_node_t *var_node = ast_new_node_var_def(parser->arena, token_id.loc, token_id.value, type, expr); + ast_node_t *var_node = ast_new_node_var_def( + parser->arena, token_id.loc, token_id.value, type, expr); return var_node; } @@ -711,7 +741,9 @@ skip_expected_token(parser_t *parser, token_kind_t expected_kind) } static bool -expected_next_token(parser_t *parser, token_t *token, token_kind_t expected_kind) +expected_next_token(parser_t *parser, + token_t *token, + token_kind_t expected_kind) { lexer_next_token(parser->lexer, token); return expected_token(token, expected_kind); @@ -722,7 +754,8 @@ expected_token(token_t *token, token_kind_t expected_kind) { if (token->kind != expected_kind) { fprintf(stderr, - "%s:%lu:%lu: syntax error: got '" SV_FMT "' token but expect '%s'\n", + "%s:%lu:%lu: syntax error: got '" SV_FMT + "' token but expect '%s'\n", token->loc.src.filepath, token_loc_to_lineno(token->loc), token_loc_to_colno(token->loc), diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c index 387cde4..d2164eb 100644 --- a/src/pretty_print_ast.c +++ b/src/pretty_print_ast.c @@ -80,7 +80,10 @@ pretty_print_print_ident(uint64_t *prefix, size_t level, bool lst_children) } static void -pretty_print_tree(pretty_print_node_t *node, uint64_t *prefix, size_t level, bool lst_children) +pretty_print_tree(pretty_print_node_t *node, + uint64_t *prefix, + size_t level, + bool lst_children) { pretty_print_print_ident(prefix, level, lst_children); @@ -94,7 +97,8 @@ pretty_print_tree(pretty_print_node_t *node, uint64_t *prefix, size_t level, boo size_t size = list_size(list); for (size_t i = 0; i < size; ++i) { - pretty_print_node_t *it = (pretty_print_node_t *)list_get(list, i)->value; + pretty_print_node_t *it = + (pretty_print_node_t *)list_get(list, i)->value; pretty_print_tree(it, prefix, level + 1, i + 1 == size); } } @@ -102,7 +106,8 @@ pretty_print_tree(pretty_print_node_t *node, uint64_t *prefix, size_t level, boo static pretty_print_node_t * pretty_print_node_new(arena_t *arena) { - pretty_print_node_t *node = (pretty_print_node_t *)arena_alloc(arena, sizeof(pretty_print_node_t)); + pretty_print_node_t *node = + (pretty_print_node_t *)arena_alloc(arena, sizeof(pretty_print_node_t)); node->children = (list_t *)arena_alloc(arena, sizeof(list_t)); list_init(node->children, arena); return node; @@ -113,7 +118,10 @@ pretty_print_new_fn_param(ast_fn_param_t *param, arena_t *arena) { pretty_print_node_t *node = pretty_print_node_new(arena); char name[256]; - sprintf(name, "Param_Definition ", SV_ARG(param->id), SV_ARG(param->type->id)); + sprintf(name, + "Param_Definition ", + SV_ARG(param->id), + SV_ARG(param->type->id)); node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1)); strcpy(node->name, name); return node; @@ -132,7 +140,8 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) while (item != NULL) { ast_node_t *decl = (ast_node_t *)item->value; - pretty_print_node_t *fn_node = ast_node_to_pretty_print_node(decl, arena); + pretty_print_node_t *fn_node = + ast_node_to_pretty_print_node(decl, arena); list_append(node->children, fn_node); item = list_next(item); @@ -149,16 +158,19 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) "Function_Definition ", SV_ARG(fn_def.id), SV_ARG(fn_def.return_type->id)); - node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1)); + node->name = + (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1)); strcpy(node->name, name); list_item_t *param = list_head(fn_def.params); while (param != NULL) { - list_append(node->children, pretty_print_new_fn_param(param->value, arena)); + list_append(node->children, + pretty_print_new_fn_param(param->value, arena)); param = list_next(param); } - pretty_print_node_t *block = ast_node_to_pretty_print_node(fn_def.block, arena); + pretty_print_node_t *block = + ast_node_to_pretty_print_node(fn_def.block, arena); list_append(node->children, block); return node; } @@ -167,13 +179,16 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) ast_fn_call_t fn_call = ast->as_fn_call; char name[256]; - sprintf(name, "Function_Call ", SV_ARG(fn_call.id)); - node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1)); + sprintf( + name, "Function_Call ", SV_ARG(fn_call.id)); + node->name = + (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1)); strcpy(node->name, name); list_item_t *item = list_head(fn_call.args); while (item != NULL) { - list_append(node->children, ast_node_to_pretty_print_node(item->value, arena)); + list_append(node->children, + ast_node_to_pretty_print_node(item->value, arena)); item = list_next(item); } @@ -187,8 +202,10 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) size_t block_nodes_size = list_size(block.nodes); for (size_t i = 0; i < block_nodes_size; ++i) { - ast_node_t *ast_node = (ast_node_t *)list_get(block.nodes, i)->value; - pretty_print_node_t *child = ast_node_to_pretty_print_node(ast_node, arena); + ast_node_t *ast_node = + (ast_node_t *)list_get(block.nodes, i)->value; + pretty_print_node_t *child = + ast_node_to_pretty_print_node(ast_node, arena); list_append(node->children, child); } return node; @@ -199,7 +216,8 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) node->name = "Return_Statement"; - pretty_print_node_t *child = ast_node_to_pretty_print_node(return_stmt.expr, arena); + pretty_print_node_t *child = + ast_node_to_pretty_print_node(return_stmt.expr, arena); list_append(node->children, child); return node; @@ -210,7 +228,8 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) node->name = "If_Statement"; - pretty_print_node_t *child = ast_node_to_pretty_print_node(if_stmt.cond, arena); + pretty_print_node_t *child = + ast_node_to_pretty_print_node(if_stmt.cond, arena); list_append(node->children, child); child = ast_node_to_pretty_print_node(if_stmt.then, arena); @@ -229,7 +248,8 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) node->name = "While_Statement"; - pretty_print_node_t *child = ast_node_to_pretty_print_node(while_stmt.cond, arena); + pretty_print_node_t *child = + ast_node_to_pretty_print_node(while_stmt.cond, arena); list_append(node->children, child); child = ast_node_to_pretty_print_node(while_stmt.then, arena); @@ -244,8 +264,10 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) char name[256]; switch (literal.kind) { case AST_LITERAL_U32: { - sprintf(name, "Literal ", literal.as_u32); - node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1)); + sprintf( + name, "Literal ", literal.as_u32); + node->name = (char *)arena_alloc( + arena, sizeof(char) * (strlen(name) + 1)); strcpy(node->name, name); break; } @@ -260,11 +282,16 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) ast_var_definition_t var = ast->as_var_def; char name[256]; - sprintf(name, "Var_Definition ", SV_ARG(var.id), SV_ARG(var.type->id)); - node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1)); + sprintf(name, + "Var_Definition ", + SV_ARG(var.id), + SV_ARG(var.type->id)); + node->name = + (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1)); strcpy(node->name, name); - pretty_print_node_t *child = ast_node_to_pretty_print_node(var.value, arena); + pretty_print_node_t *child = + ast_node_to_pretty_print_node(var.value, arena); list_append(node->children, child); return node; @@ -275,7 +302,8 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) char name[256]; sprintf(name, "Reference ", SV_ARG(ref.id)); - node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1)); + node->name = + (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1)); strcpy(node->name, name); return node; @@ -365,8 +393,10 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) assert(false && "binop not implemented"); } - pretty_print_node_t *lhs = ast_node_to_pretty_print_node(binop.lhs, arena); - pretty_print_node_t *rhs = ast_node_to_pretty_print_node(binop.rhs, arena); + pretty_print_node_t *lhs = + ast_node_to_pretty_print_node(binop.lhs, arena); + pretty_print_node_t *rhs = + ast_node_to_pretty_print_node(binop.rhs, arena); list_append(node->children, lhs); list_append(node->children, rhs); @@ -405,7 +435,8 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) } } - pretty_print_node_t *expr = ast_node_to_pretty_print_node(unary_op.expr, arena); + pretty_print_node_t *expr = + ast_node_to_pretty_print_node(unary_op.expr, arena); list_append(node->children, expr); return node; diff --git a/src/scope.c b/src/scope.c index e483fbe..274982f 100644 --- a/src/scope.c +++ b/src/scope.c @@ -28,7 +28,8 @@ scope_new(arena_t *arena) assert(arena); scope_t *scope = (scope_t *)arena_alloc(arena, sizeof(scope_t)); if (scope == NULL) { - fprintf(stderr, "[FATAL] Out of memory: scope_new: %s\n", strerror(errno)); + fprintf( + stderr, "[FATAL] Out of memory: scope_new: %s\n", strerror(errno)); exit(EXIT_FAILURE); } scope->arena = arena; @@ -38,7 +39,8 @@ scope_new(arena_t *arena) list_t *children = (list_t *)arena_alloc(arena, sizeof(list_t)); if (children == NULL) { - fprintf(stderr, "[FATAL] Out of memory: scope_new: %s\n", strerror(errno)); + fprintf( + stderr, "[FATAL] Out of memory: scope_new: %s\n", strerror(errno)); exit(EXIT_FAILURE); } @@ -54,7 +56,8 @@ symbol_new(arena_t *arena, string_view_t id, type_t *type) assert(arena); symbol_t *symbol = (symbol_t *)arena_alloc(arena, sizeof(symbol_t)); if (symbol == NULL) { - fprintf(stderr, "[FATAL] Out of memory: symbol_new: %s\n", strerror(errno)); + fprintf( + stderr, "[FATAL] Out of memory: symbol_new: %s\n", strerror(errno)); exit(EXIT_FAILURE); } symbol->id = id; diff --git a/tests/unit/arena_test.c b/tests/unit/arena_test.c index a471572..1c16492 100644 --- a/tests/unit/arena_test.c +++ b/tests/unit/arena_test.c @@ -84,11 +84,27 @@ arena_padding_test(const MunitParameter params[], void *user_data_or_fixture) return MUNIT_OK; } -static MunitTest tests[] = { { "/arena_alloc_test", arena_alloc_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, - { "/arena_padding_test", arena_padding_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, - { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } }; - -static const MunitSuite suite = { "/arena", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE }; +static MunitTest tests[] = { + { "/arena_alloc_test", + arena_alloc_test, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL }, + { "/arena_padding_test", + arena_padding_test, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL }, + { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } +}; + +static const MunitSuite suite = { "/arena", + tests, + NULL, + 1, + MUNIT_SUITE_OPTION_NONE }; int main(int argc, char *argv[]) diff --git a/tests/unit/list_test.c b/tests/unit/list_test.c index 8b759f9..72e7788 100644 --- a/tests/unit/list_test.c +++ b/tests/unit/list_test.c @@ -95,12 +95,33 @@ list_next_test(const MunitParameter params[], void *user_data_or_fixture) return MUNIT_OK; } -static MunitTest tests[] = { { "/list_append_test", list_append_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, - { "/list_get_test", list_get_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, - { "/list_next_test", list_next_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, - { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } }; - -static const MunitSuite suite = { "/list", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE }; +static MunitTest tests[] = { + { "/list_append_test", + list_append_test, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL }, + { "/list_get_test", + list_get_test, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL }, + { "/list_next_test", + list_next_test, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL }, + { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } +}; + +static const MunitSuite suite = { "/list", + tests, + NULL, + 1, + MUNIT_SUITE_OPTION_NONE }; int main(int argc, char *argv[]) diff --git a/tests/unit/map_test.c b/tests/unit/map_test.c index 9497c7c..2be431e 100644 --- a/tests/unit/map_test.c +++ b/tests/unit/map_test.c @@ -92,12 +92,26 @@ test_map_get_kvs(const MunitParameter params[], void *user_data_or_fixture) } static MunitTest tests[] = { - { "/test_create_new", test_create_new, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, - { "/test_map_put_and_get", test_map_put_and_get, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, + { "/test_create_new", + test_create_new, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL }, + { "/test_map_put_and_get", + test_map_put_and_get, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL }, { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } }; -static const MunitSuite suite = { "/map", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE }; +static const MunitSuite suite = { "/map", + tests, + NULL, + 1, + MUNIT_SUITE_OPTION_NONE }; int main(int argc, char *argv[]) diff --git a/tests/unit/string_view_test.c b/tests/unit/string_view_test.c index d85f19b..9600c77 100644 --- a/tests/unit/string_view_test.c +++ b/tests/unit/string_view_test.c @@ -22,7 +22,8 @@ #include static MunitResult -string_view_eq_to_cstr_test(const MunitParameter params[], void *user_data_or_fixture) +string_view_eq_to_cstr_test(const MunitParameter params[], + void *user_data_or_fixture) { char *name = "John Doe"; @@ -46,7 +47,8 @@ string_view_eq_to_cstr_test(const MunitParameter params[], void *user_data_or_fi } static MunitResult -string_view_to_u32_test(const MunitParameter params[], void *user_data_or_fixture) +string_view_to_u32_test(const MunitParameter params[], + void *user_data_or_fixture) { char *number = "69"; @@ -68,12 +70,26 @@ string_view_to_u32_test(const MunitParameter params[], void *user_data_or_fixtur } static MunitTest tests[] = { - { "/eq_to_cstr_test", string_view_eq_to_cstr_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, - { "/to_u32_test", string_view_to_u32_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, + { "/eq_to_cstr_test", + string_view_eq_to_cstr_test, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL }, + { "/to_u32_test", + string_view_to_u32_test, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL }, { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } }; -static const MunitSuite suite = { "/string_view", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE }; +static const MunitSuite suite = { "/string_view", + tests, + NULL, + 1, + MUNIT_SUITE_OPTION_NONE }; int main(int argc, char *argv[]) -- 2.46.1