From 56c0c620078b906a1994e0833c522913928b37aa Mon Sep 17 00:00:00 2001 From: Mel Date: Sun, 3 May 2026 22:31:29 +0200 Subject: Lowering pass initial driver shell Signed-off-by: Mel --- Makefile | 2 +- boot/catboot.c | 3 +-- boot/catboot.h | 1 + boot/lower.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 boot/lower.c diff --git a/Makefile b/Makefile index 7e1f989..727ecf3 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ LDFLAGS ?= -ltcc .DEFAULT_GOAL := all -BOOTSTRAP_SOURCES = boot/catboot.c boot/catboot.h boot/common.c boot/lex.c boot/tree.c boot/visit/visit.c boot/visit/tree.c boot/ir.c boot/visit/ir.c boot/parse.c boot/transpile.c boot/build.c +BOOTSTRAP_SOURCES = boot/catboot.c boot/catboot.h boot/common.c boot/lex.c boot/tree.c boot/visit/visit.c boot/visit/tree.c boot/ir.c boot/visit/ir.c boot/lower.c boot/parse.c boot/transpile.c boot/build.c BOOTSTRAP_TEST_SOURCES = boot/tests/test.c SOURCES = src/catskill.csk diff --git a/boot/catboot.c b/boot/catboot.c index 18bf6dc..fe879a1 100644 --- a/boot/catboot.c +++ b/boot/catboot.c @@ -129,9 +129,8 @@ debug_lower_pass(struct Source_File source_file) return parser_result; } - // TODO: real lowering isn't quite done yet, just output an - // empty unit for now! struct Unit unit = { 0 }; + lower_tree(&tree, source_file, &unit); printer(&unit); return 0; diff --git a/boot/catboot.h b/boot/catboot.h index 71c2366..dc5fc82 100644 --- a/boot/catboot.h +++ b/boot/catboot.h @@ -21,5 +21,6 @@ #include "visit/tree.c" #include "ir.c" #include "visit/ir.c" +#include "lower.c" #include "transpile.c" #include "build.c" diff --git a/boot/lower.c b/boot/lower.c new file mode 100644 index 0000000..1fd1a7e --- /dev/null +++ b/boot/lower.c @@ -0,0 +1,70 @@ +/* + * lowering pass to create an ir from a catskill source tree. + * + * the idea is to fully de-sugar a catskill source file + * into an ir that can be very easily re-expressed into + * a low-level language, like our transpilation target, c. + * + * the lowering pass is itself split into two passes, one + * initial pass, which collects all top-level type & function + * declarations to build up a table of all available language objects, + * and then a second pass going over each function body. + * this de-couples usage of the functions and types from their ordering + * allowing for more free-flowing files than c would allow. + * + * additionally, we handle type dependencies by collecting every + * direct reference a type has to another, and topologically + * sort the types to create the correct ordering of them, + * pointing out any unbreakable cycles to the user as they come up. + * + * Copyright (c) 2026, Mel G. + * + * SPDX-License-Identifier: MPL-2.0 + */ + +#pragma once + +#include "catboot.h" + +Type_Id +lower_seed_primitive(struct Unit* unit, const ascii* name) +{ + Type_Id id = array_length(&unit->types.entries); + struct String name_str = string_from_static_c_string(name); + struct Type* type = type_new(id, TYPE_PRIMITIVE, name_str, span_empty()); + array_push(&unit->types.entries, &type); + + struct Type_Name_To_Id mapping = { .name = name_str, .id = id }; + array_push(&unit->types.by_name, &mapping); + + return id; +} + +void +lower_tree(struct Tree* tree, struct Source_File source, struct Unit* unit) +{ + // TODO: implement! + (void)tree; + (void)source; + + unit->types.entries = array_new(struct Type*, 256); + unit->types.by_hash = array_new(struct Type_Hash_To_Id, 256); + unit->types.by_name = array_new(struct Type_Name_To_Id, 256); + + unit->functions.entries = array_new(struct Function*, 256); + unit->functions.by_name = array_new(struct Function_Name_To_Id, 256); + + unit->imports = array_new(struct Import, 32); + unit->type_emission_order = array_new(Type_Id, 256); + unit->had_error = false; + unit->diagnostics = array_new(struct Diagnostic, 64); + + unit->types.primitive_int_id = lower_seed_primitive(unit, "int"); + unit->types.primitive_uint_id = lower_seed_primitive(unit, "uint"); + unit->types.primitive_bool_id = lower_seed_primitive(unit, "bool"); + unit->types.primitive_string_id = lower_seed_primitive(unit, "string"); + unit->types.primitive_float_id = lower_seed_primitive(unit, "float"); + unit->types.primitive_byte_id = lower_seed_primitive(unit, "byte"); + unit->types.primitive_ascii_id = lower_seed_primitive(unit, "ascii"); + unit->types.primitive_void_id = lower_seed_primitive(unit, "void"); +} -- cgit 1.4.1