From 5c3e59eaf50871ddf727ed49c1fda1d87f6a1f79 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sun, 2 Aug 2026 22:27:26 +0300 Subject: [PATCH] Modules wip: refactor IR compiler --- apps/interpreter/source/main.cpp | 4 +- libs/ir/include/pslang/ir/compiler.hpp | 30 +-- libs/ir/include/pslang/ir/module.hpp | 36 +++ libs/ir/include/pslang/ir/print.hpp | 6 +- libs/ir/source/compiler.cpp | 250 +++++++++--------- libs/ir/source/print.cpp | 12 +- .../pslang/jit/arch/linux_x86_64/compiler.hpp | 4 +- .../jit/arch/macos_aarch64/compiler.hpp | 4 +- libs/jit/include/pslang/jit/jit.hpp | 4 +- .../jit/source/arch/linux_x86_64/compiler.cpp | 18 +- .../source/arch/macos_aarch64/compiler.cpp | 18 +- libs/jit/source/jit.cpp | 6 +- 12 files changed, 201 insertions(+), 191 deletions(-) create mode 100644 libs/ir/include/pslang/ir/module.hpp diff --git a/apps/interpreter/source/main.cpp b/apps/interpreter/source/main.cpp index 3e6917f..8c3e91c 100644 --- a/apps/interpreter/source/main.cpp +++ b/apps/interpreter/source/main.cpp @@ -97,7 +97,7 @@ int main(int argc, char ** argv) std::vector filenames; std::vector modules; - std::vector ir_compiled; + std::vector ir_compiled; bool no_more_options = false; @@ -199,7 +199,7 @@ int main(int argc, char ** argv) modules.push_back(std::move(module)); ir_compiled.emplace_back(); - ir::compile(ir_compiled.back(), modules.back().statements); + ir::compile(ir_compiled.back(), modules.back()); if (dump_ir) { diff --git a/libs/ir/include/pslang/ir/compiler.hpp b/libs/ir/include/pslang/ir/compiler.hpp index 7b37ca4..7bbf0b3 100644 --- a/libs/ir/include/pslang/ir/compiler.hpp +++ b/libs/ir/include/pslang/ir/compiler.hpp @@ -1,36 +1,10 @@ #pragma once -#include -#include - -#include - -namespace pslang::ast -{ - - struct function_definition; - -} +#include namespace pslang::ir { - struct module_context - { - node_list_ptr nodes; - - std::unordered_map labels; - - struct symbol_info - { - node_ref begin; - node_ref end; - }; - - std::unordered_map symbols; - node_ref entry_point; - }; - - void compile(module_context & context, ast::statement_list_ptr statements); + void compile(compiled_module & result, semantic::module const & module); } diff --git a/libs/ir/include/pslang/ir/module.hpp b/libs/ir/include/pslang/ir/module.hpp new file mode 100644 index 0000000..591340c --- /dev/null +++ b/libs/ir/include/pslang/ir/module.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include +#include + +#include + +namespace pslang::ast +{ + + struct function_definition; + +} + +namespace pslang::ir +{ + + struct compiled_module + { + node_list_ptr nodes; + + // Needed for debug IR dump + std::unordered_map labels; + + struct function_info + { + node_ref begin; + node_ref end; + }; + + std::unordered_map functions; + + node_ref entry_point; + }; + +} diff --git a/libs/ir/include/pslang/ir/print.hpp b/libs/ir/include/pslang/ir/print.hpp index ef7f085..2935bbe 100644 --- a/libs/ir/include/pslang/ir/print.hpp +++ b/libs/ir/include/pslang/ir/print.hpp @@ -7,9 +7,9 @@ namespace pslang::ir { - struct module_context; + struct compiled_module; void print(std::ostream & out, node_list const & nodes); - void print(std::ostream & out, module_context const & context); + void print(std::ostream & out, compiled_module const & module); -} \ No newline at end of file +} diff --git a/libs/ir/source/compiler.cpp b/libs/ir/source/compiler.cpp index c34f98d..9ddf8f8 100644 --- a/libs/ir/source/compiler.cpp +++ b/libs/ir/source/compiler.cpp @@ -53,14 +53,14 @@ namespace pslang::ir struct zero_literal_visitor : types::const_visitor { - module_context & mcontext; + compiled_module & module; using const_visitor::apply; template void apply(types::primitive_type_base) { - mcontext.nodes->emplace_back(literal{ast::literal{ast::primitive_literal_base{.value = {}}}}, + module.nodes->emplace_back(literal{ast::literal{ast::primitive_literal_base{.value = {}}}}, std::make_shared(types::primitive_type{types::primitive_type_base{}})); } @@ -80,7 +80,7 @@ namespace pslang::ir using const_statement_visitor::apply; using const_expression_visitor::apply; - module_context & mcontext; + compiled_module & module; local_context & lcontext; template @@ -94,7 +94,7 @@ namespace pslang::ir template node_ref apply(ast::primitive_literal_base const & node) { - mcontext.nodes->emplace_back(literal{node}, ast::get_type(node)); + module.nodes->emplace_back(literal{node}, ast::get_type(node)); return last(); } @@ -115,13 +115,13 @@ namespace pslang::ir } else if (node.function_node) { - mcontext.nodes->emplace_back(instruction_address{}, node.inferred_type); + module.nodes->emplace_back(instruction_address{}, node.inferred_type); lcontext.resolve_address.emplace_back(last(), node.function_node); return last(); } else if (node.foreign_function_node) { - mcontext.nodes->emplace_back(extern_symbol{node.name}, node.inferred_type); + module.nodes->emplace_back(extern_symbol{node.name}, node.inferred_type); return last(); } else @@ -134,7 +134,7 @@ namespace pslang::ir if (node.type == ast::unary_operation_type::dereference) { - mcontext.nodes->emplace_back(load{arg1}, node.inferred_type); + module.nodes->emplace_back(load{arg1}, node.inferred_type); return last(); } @@ -145,7 +145,7 @@ namespace pslang::ir throw std::runtime_error("Error compiling address of operator"); } - mcontext.nodes->emplace_back(unary_operation{node.type, arg1}, node.inferred_type); + module.nodes->emplace_back(unary_operation{node.type, arg1}, node.inferred_type); return last(); } @@ -157,29 +157,29 @@ namespace pslang::ir if (node.type == ast::binary_operation_type::logical_and) { - mcontext.nodes->emplace_back(copy{arg1}, ast::get_type(*node.arg1)); + module.nodes->emplace_back(copy{arg1}, ast::get_type(*node.arg1)); auto result = last(); - mcontext.nodes->emplace_back(jump_if_zero{result}); + module.nodes->emplace_back(jump_if_zero{result}); auto jump = last(); auto arg2 = apply(*node.arg2); - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::binary_and, result, arg2}, node.inferred_type); - mcontext.nodes->emplace_back(assignment{result, last()}); - mcontext.nodes->emplace_back(label{}); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::binary_and, result, arg2}, node.inferred_type); + module.nodes->emplace_back(assignment{result, last()}); + module.nodes->emplace_back(label{}); std::get(jump->instruction).target = last(); return result; } if (node.type == ast::binary_operation_type::logical_or) { - mcontext.nodes->emplace_back(copy{arg1}, ast::get_type(*node.arg1)); + module.nodes->emplace_back(copy{arg1}, ast::get_type(*node.arg1)); auto result = last(); - mcontext.nodes->emplace_back(unary_operation{ast::unary_operation_type::logical_not, result}, node.inferred_type); - mcontext.nodes->emplace_back(jump_if_zero{last()}); + module.nodes->emplace_back(unary_operation{ast::unary_operation_type::logical_not, result}, node.inferred_type); + module.nodes->emplace_back(jump_if_zero{last()}); auto jump = last(); auto arg2 = apply(*node.arg2); - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::binary_or, result, arg2}, node.inferred_type); - mcontext.nodes->emplace_back(assignment{result, last()}); - mcontext.nodes->emplace_back(label{}); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::binary_or, result, arg2}, node.inferred_type); + module.nodes->emplace_back(assignment{result, last()}); + module.nodes->emplace_back(label{}); std::get(jump->instruction).target = last(); return result; } @@ -206,28 +206,28 @@ namespace pslang::ir auto i64_type = std::make_shared(types::primitive_type{types::i64_type{}}); - mcontext.nodes->emplace_back(literal{ast::i64_literal{element_size}}, i64_type); + module.nodes->emplace_back(literal{ast::i64_literal{element_size}}, i64_type); auto element_size_node = last(); if (node.type == ast::binary_operation_type::addition) { if (arg1_is_pointer) { - mcontext.nodes->emplace_back(cast_operation{arg2, i64_type}, i64_type); - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::multiplication, last(), element_size_node}, i64_type); - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::addition, arg1, last()}, node.inferred_type); + module.nodes->emplace_back(cast_operation{arg2, i64_type}, i64_type); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::multiplication, last(), element_size_node}, i64_type); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::addition, arg1, last()}, node.inferred_type); } else // if (arg2_is_pointer) { - mcontext.nodes->emplace_back(cast_operation{arg1, i64_type}, i64_type); - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::multiplication, last(), element_size_node}, i64_type); - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::addition, arg2, last()}, node.inferred_type); + module.nodes->emplace_back(cast_operation{arg1, i64_type}, i64_type); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::multiplication, last(), element_size_node}, i64_type); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::addition, arg2, last()}, node.inferred_type); } } else if (node.type == ast::binary_operation_type::subtraction) { - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::subtraction, arg1, arg2}, node.inferred_type); - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::division, last(), element_size_node}, i64_type); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::subtraction, arg1, arg2}, node.inferred_type); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::division, last(), element_size_node}, i64_type); } return last(); @@ -253,14 +253,14 @@ namespace pslang::ir // Both signed or both unsigned: just cast the smaller one to the larger type if (arg1_size < arg2_size) { - mcontext.nodes->emplace_back(cast_operation{arg1, max_type}, max_type); - mcontext.nodes->emplace_back(binary_operation{node.type, last(), arg2}, node.inferred_type); + module.nodes->emplace_back(cast_operation{arg1, max_type}, max_type); + module.nodes->emplace_back(binary_operation{node.type, last(), arg2}, node.inferred_type); return last(); } else { - mcontext.nodes->emplace_back(cast_operation{arg2, max_type}, max_type); - mcontext.nodes->emplace_back(binary_operation{node.type, arg1, last()}, node.inferred_type); + module.nodes->emplace_back(cast_operation{arg2, max_type}, max_type); + module.nodes->emplace_back(binary_operation{node.type, arg1, last()}, node.inferred_type); return last(); } } @@ -288,32 +288,32 @@ namespace pslang::ir } // Compare with zero first - zero_literal_visitor{{}, mcontext}.apply(*arg1_type); + zero_literal_visitor{{}, module}.apply(*arg1_type); switch (type) { case ast::binary_operation_type::equals: - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::greater_equals, arg1, last()}, node.inferred_type); - mcontext.nodes->emplace_back(jump_if_zero{last(), {}}); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::greater_equals, arg1, last()}, node.inferred_type); + module.nodes->emplace_back(jump_if_zero{last(), {}}); break; case ast::binary_operation_type::not_equals: - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::less, arg1, last()}, node.inferred_type); - mcontext.nodes->emplace_back(jump_if_nonzero{last(), {}}); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::less, arg1, last()}, node.inferred_type); + module.nodes->emplace_back(jump_if_nonzero{last(), {}}); break; case ast::binary_operation_type::less: - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::less, arg1, last()}, node.inferred_type); - mcontext.nodes->emplace_back(jump_if_nonzero{last(), {}}); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::less, arg1, last()}, node.inferred_type); + module.nodes->emplace_back(jump_if_nonzero{last(), {}}); break; case ast::binary_operation_type::greater: - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::greater, arg1, last()}, node.inferred_type); - mcontext.nodes->emplace_back(jump_if_zero{last(), {}}); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::greater, arg1, last()}, node.inferred_type); + module.nodes->emplace_back(jump_if_zero{last(), {}}); break; case ast::binary_operation_type::less_equals: - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::less_equals, arg1, last()}, node.inferred_type); - mcontext.nodes->emplace_back(jump_if_nonzero{last(), {}}); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::less_equals, arg1, last()}, node.inferred_type); + module.nodes->emplace_back(jump_if_nonzero{last(), {}}); break; case ast::binary_operation_type::greater_equals: - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::greater_equals, arg1, last()}, node.inferred_type); - mcontext.nodes->emplace_back(jump_if_zero{last(), {}}); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::greater_equals, arg1, last()}, node.inferred_type); + module.nodes->emplace_back(jump_if_zero{last(), {}}); break; default: break; @@ -332,13 +332,13 @@ namespace pslang::ir else if (max_size == 8) max_unsigned_type = std::make_unique(types::primitive_type{types::u64_type{}}); - mcontext.nodes->emplace_back(cast_operation{arg1, max_unsigned_type}, max_unsigned_type); + module.nodes->emplace_back(cast_operation{arg1, max_unsigned_type}, max_unsigned_type); auto new_arg1 = last(); - mcontext.nodes->emplace_back(cast_operation{arg2, max_unsigned_type}, max_unsigned_type); + module.nodes->emplace_back(cast_operation{arg2, max_unsigned_type}, max_unsigned_type); auto new_arg2 = last(); - mcontext.nodes->emplace_back(binary_operation{type, new_arg1, new_arg2}, node.inferred_type); - mcontext.nodes->emplace_back(assignment{result, last()}); - mcontext.nodes->emplace_back(label{}); + module.nodes->emplace_back(binary_operation{type, new_arg1, new_arg2}, node.inferred_type); + module.nodes->emplace_back(assignment{result, last()}); + module.nodes->emplace_back(label{}); if (auto jump_if_zero = std::get_if(&jump_node->instruction)) jump_if_zero->target = last(); else if (auto jump_if_nonzero = std::get_if(&jump_node->instruction)) @@ -349,7 +349,7 @@ namespace pslang::ir // General case - mcontext.nodes->emplace_back(binary_operation{node.type, arg1, arg2}, node.inferred_type); + module.nodes->emplace_back(binary_operation{node.type, arg1, arg2}, node.inferred_type); return last(); } @@ -363,7 +363,7 @@ namespace pslang::ir } auto arg = apply(*node.expression); - mcontext.nodes->emplace_back(cast_operation{arg, node.inferred_type}, node.inferred_type); + module.nodes->emplace_back(cast_operation{arg, node.inferred_type}, node.inferred_type); return last(); } @@ -378,13 +378,13 @@ namespace pslang::ir if (auto identifier = std::get_if(node.function.get()); identifier && identifier->function_node) { - mcontext.nodes->emplace_back(call{{}, std::move(arguments)}, node.inferred_type); + module.nodes->emplace_back(call{{}, std::move(arguments)}, node.inferred_type); lcontext.resolve_call.emplace_back(last(), identifier->function_node); return last(); } auto function = apply(*node.function); - mcontext.nodes->emplace_back(call_pointer{function, std::move(arguments)}, node.inferred_type); + module.nodes->emplace_back(call_pointer{function, std::move(arguments)}, node.inferred_type); return last(); } else // if (node.type) @@ -392,13 +392,13 @@ namespace pslang::ir auto type = ast::get_type(*node.type); if (auto struct_type = std::get_if(type.get())) { - mcontext.nodes->emplace_back(alloc{}, node.inferred_type); + module.nodes->emplace_back(alloc{}, node.inferred_type); auto result = last(); for (std::size_t i = 0; i < node.arguments.size(); ++i) { auto const & field = struct_type->node->fields[i]; auto arg = apply(*node.arguments[i]); - mcontext.nodes->emplace_back(assignment{result, arg, {i}}, field.inferred_type); + module.nodes->emplace_back(assignment{result, arg, {i}}, field.inferred_type); } return result; } @@ -408,12 +408,12 @@ namespace pslang::ir node_ref apply(ast::array const & node) { - mcontext.nodes->emplace_back(alloc{}, node.inferred_type); + module.nodes->emplace_back(alloc{}, node.inferred_type); auto array = last(); for (std::size_t i = 0; i < node.elements.size(); ++i) { auto element = apply(node.elements[i]); - mcontext.nodes->emplace_back(assignment{array, element, {i}}); + module.nodes->emplace_back(assignment{array, element, {i}}); } return array; } @@ -438,7 +438,7 @@ namespace pslang::ir if (array_type) { auto array = apply(*node.array); - mcontext.nodes->emplace_back(cast_operation{array, target_pointer_type}, target_pointer_type); + module.nodes->emplace_back(cast_operation{array, target_pointer_type}, target_pointer_type); base_ptr = last(); } else // if (pointer_type) @@ -450,12 +450,12 @@ namespace pslang::ir std::int64_t element_size = ast::type_size(*element_type); auto index = apply(*node.index); - mcontext.nodes->emplace_back(cast_operation{index, i64_type}, i64_type); + module.nodes->emplace_back(cast_operation{index, i64_type}, i64_type); auto index_cast = last(); - mcontext.nodes->emplace_back(literal{ast::i64_literal{element_size}}, i64_type); - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::multiplication, index_cast, last()}, i64_type); - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::addition, base_ptr, last()}, target_pointer_type); - mcontext.nodes->emplace_back(load{last()}, node.inferred_type); + module.nodes->emplace_back(literal{ast::i64_literal{element_size}}, i64_type); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::multiplication, index_cast, last()}, i64_type); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::addition, base_ptr, last()}, target_pointer_type); + module.nodes->emplace_back(load{last()}, node.inferred_type); return last(); } else @@ -478,16 +478,16 @@ namespace pslang::ir { if (pointer_type) { - mcontext.nodes->emplace_back(literal{ast::literal{ast::u64_literal{field.layout.offset}}}, + module.nodes->emplace_back(literal{ast::literal{ast::u64_literal{field.layout.offset}}}, std::make_shared(types::primitive_type{types::u64_type{}})); - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::addition, object, last()}, + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::addition, object, last()}, std::make_shared(types::pointer_type{field.inferred_type, pointer_type->is_mutable})); - mcontext.nodes->emplace_back(load{last()}, field.inferred_type); + module.nodes->emplace_back(load{last()}, field.inferred_type); return last(); } else { - mcontext.nodes->emplace_back(copy{object, {i}}, node.inferred_type); + module.nodes->emplace_back(copy{object, {i}}, node.inferred_type); return last(); } } @@ -497,20 +497,20 @@ namespace pslang::ir node_ref apply(ast::if_expression const & node) { - mcontext.nodes->emplace_back(alloc{}, node.inferred_type); + module.nodes->emplace_back(alloc{}, node.inferred_type); auto result = last(); auto condition = apply(*node.condition); - mcontext.nodes->emplace_back(jump_if_zero{condition}); + module.nodes->emplace_back(jump_if_zero{condition}); auto jump_skip_if_true = last(); auto true_result = apply(*node.if_true); - mcontext.nodes->emplace_back(assignment{result, true_result}, node.inferred_type); - mcontext.nodes->emplace_back(jump{}); + module.nodes->emplace_back(assignment{result, true_result}, node.inferred_type); + module.nodes->emplace_back(jump{}); auto jump_end_if_true = last(); - mcontext.nodes->emplace_back(label{}); + module.nodes->emplace_back(label{}); auto after_if_true = last(); auto false_result = apply(*node.if_false); - mcontext.nodes->emplace_back(assignment{result, false_result}, node.inferred_type); - mcontext.nodes->emplace_back(label{}); + module.nodes->emplace_back(assignment{result, false_result}, node.inferred_type); + module.nodes->emplace_back(label{}); auto end = last(); std::get(jump_skip_if_true->instruction).target = after_if_true; @@ -520,13 +520,13 @@ namespace pslang::ir node_ref apply(ast::sizeof_operator const & node) { - mcontext.nodes->emplace_back(literal{ast::literal{ast::u64_literal{ast::type_size(*node.inferred_source_type)}}}, node.inferred_type); + module.nodes->emplace_back(literal{ast::literal{ast::u64_literal{ast::type_size(*node.inferred_source_type)}}}, node.inferred_type); return last(); } node_ref apply(ast::alignof_operator const & node) { - mcontext.nodes->emplace_back(literal{ast::literal{ast::u64_literal{ast::type_alignment(*node.inferred_source_type)}}}, node.inferred_type); + module.nodes->emplace_back(literal{ast::literal{ast::u64_literal{ast::type_alignment(*node.inferred_source_type)}}}, node.inferred_type); return last(); } @@ -542,7 +542,7 @@ namespace pslang::ir if (auto identifier = std::get_if(lhs_node.get())) { auto lhs = apply(*lhs_node); - mcontext.nodes->emplace_back(assignment{lhs, rhs, std::move(path)}, identifier->inferred_type); + module.nodes->emplace_back(assignment{lhs, rhs, std::move(path)}, identifier->inferred_type); return last(); } else if (auto field_access = std::get_if(lhs_node.get())) @@ -572,7 +572,7 @@ namespace pslang::ir if (auto identifier = std::get_if(node.get())) { auto object = apply(*node); - mcontext.nodes->emplace_back(unary_operation{ast::unary_operation_type::address_of, object}, result_type); + module.nodes->emplace_back(unary_operation{ast::unary_operation_type::address_of, object}, result_type); return last(); } @@ -614,11 +614,11 @@ namespace pslang::ir std::int64_t element_size = ast::type_size(*element_type); auto index = apply(*array_access->index); - mcontext.nodes->emplace_back(cast_operation{index, i64_type}, i64_type); + module.nodes->emplace_back(cast_operation{index, i64_type}, i64_type); auto index_cast = last(); - mcontext.nodes->emplace_back(literal{ast::i64_literal{element_size}}, i64_type); - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::multiplication, index_cast, last()}, i64_type); - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::addition, base_ptr, last()}, target_pointer_type); + module.nodes->emplace_back(literal{ast::i64_literal{element_size}}, i64_type); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::multiplication, index_cast, last()}, i64_type); + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::addition, base_ptr, last()}, target_pointer_type); return last(); } } @@ -648,9 +648,9 @@ namespace pslang::ir auto const & field = struct_node->fields[i]; if (field.name == field_access->field_name) { - mcontext.nodes->emplace_back(literal{ast::literal{ast::u64_literal{field.layout.offset}}}, + module.nodes->emplace_back(literal{ast::literal{ast::u64_literal{field.layout.offset}}}, std::make_shared(types::primitive_type{types::u64_type{}})); - mcontext.nodes->emplace_back(binary_operation{ast::binary_operation_type::addition, *object_ptr, last()}, + module.nodes->emplace_back(binary_operation{ast::binary_operation_type::addition, *object_ptr, last()}, result_type); return last(); } @@ -674,7 +674,7 @@ namespace pslang::ir // Otherwise, compile into explicit memory store if (auto lhs_ptr = apply_get_address(node.lhs)) { - mcontext.nodes->emplace_back(store{*lhs_ptr, rhs}, ast::get_type(*node.rhs)); + module.nodes->emplace_back(store{*lhs_ptr, rhs}, ast::get_type(*node.rhs)); return last(); } @@ -685,17 +685,17 @@ namespace pslang::ir { if (node.global) { - mcontext.nodes->emplace_back(global{.initializer = {0}}, std::make_unique(types::primitive_type{types::bool_type{}})); + module.nodes->emplace_back(global{.initializer = {0}}, std::make_unique(types::primitive_type{types::bool_type{}})); auto guard = last(); - mcontext.nodes->emplace_back(global{.initializer = {}}, node.inferred_type); + module.nodes->emplace_back(global{.initializer = {}}, node.inferred_type); auto result = last(); - mcontext.nodes->emplace_back(jump_if_nonzero{.condition = guard, .target = {}}); + module.nodes->emplace_back(jump_if_nonzero{.condition = guard, .target = {}}); auto jump = last(); - mcontext.nodes->emplace_back(literal{ast::bool_literal{true}}, std::make_unique(types::primitive_type{types::bool_type{}})); - mcontext.nodes->emplace_back(assignment{guard, last()}); + module.nodes->emplace_back(literal{ast::bool_literal{true}}, std::make_unique(types::primitive_type{types::bool_type{}})); + module.nodes->emplace_back(assignment{guard, last()}); auto value = apply(*node.initializer); - mcontext.nodes->emplace_back(assignment{result, value}); - mcontext.nodes->emplace_back(label{}); + module.nodes->emplace_back(assignment{result, value}); + module.nodes->emplace_back(label{}); std::get(jump->instruction).target = last(); lcontext.globals[&node] = result; return result; @@ -708,7 +708,7 @@ namespace pslang::ir // Evaluating variable initializer didn't produce any nodes // It must have been just a reference to another variable or smth like that // Introduce a copy node to prevent accidental variable coalescing - mcontext.nodes->emplace_back(copy{result}, node.inferred_type); + module.nodes->emplace_back(copy{result}, node.inferred_type); result = last(); } lcontext.variables[&node] = result; @@ -725,7 +725,7 @@ namespace pslang::ir if (block.condition) { auto condition = apply(*block.condition); - mcontext.nodes->emplace_back(jump_if_zero{condition, {}}); + module.nodes->emplace_back(jump_if_zero{condition, {}}); jump_to_next = last(); } @@ -733,10 +733,10 @@ namespace pslang::ir apply(*block.statements); lcontext.scopes.pop_back(); - mcontext.nodes->emplace_back(jump{}); + module.nodes->emplace_back(jump{}); jumps_to_end.push_back(last()); - mcontext.nodes->emplace_back(label{}); + module.nodes->emplace_back(label{}); if (jump_to_next) std::get((*jump_to_next)->instruction).target = last(); } @@ -750,10 +750,10 @@ namespace pslang::ir node_ref apply(ast::while_block const & node) { - mcontext.nodes->emplace_back(label{}); + module.nodes->emplace_back(label{}); auto begin = last(); auto condition = apply(*node.condition); - mcontext.nodes->emplace_back(jump_if_zero{condition, {}}); + module.nodes->emplace_back(jump_if_zero{condition, {}}); auto jump_to_end = last(); lcontext.loop_scopes.emplace_back(); @@ -761,8 +761,8 @@ namespace pslang::ir apply(*node.statements); lcontext.scopes.pop_back(); - mcontext.nodes->emplace_back(jump{begin}); - mcontext.nodes->emplace_back(label{}); + module.nodes->emplace_back(jump{begin}); + module.nodes->emplace_back(label{}); auto end = last(); std::get(jump_to_end->instruction).target = end; @@ -778,14 +778,14 @@ namespace pslang::ir node_ref apply(ast::break_statement const &) { - mcontext.nodes->emplace_back(jump{}); + module.nodes->emplace_back(jump{}); lcontext.loop_scopes.back().resolve_break.push_back(last()); return last(); } node_ref apply(ast::continue_statement const &) { - mcontext.nodes->emplace_back(jump{}); + module.nodes->emplace_back(jump{}); lcontext.loop_scopes.back().resolve_continue.push_back(last()); return last(); } @@ -805,10 +805,10 @@ namespace pslang::ir if (node.value) { auto value = apply(*node.value); - mcontext.nodes->emplace_back(return_value{value}, value->inferred_type); + module.nodes->emplace_back(return_value{value}, value->inferred_type); } else - mcontext.nodes->emplace_back(return_value{}, std::make_shared(types::unit_type{})); + module.nodes->emplace_back(return_value{}, std::make_shared(types::unit_type{})); return last(); } @@ -821,24 +821,24 @@ namespace pslang::ir void do_apply(ast::function_definition const & node) { - mcontext.nodes->emplace_back(label{}); + module.nodes->emplace_back(label{}); auto begin = last(); lcontext.scopes.emplace_back(); for (std::size_t i = 0; i < node.arguments.size(); ++i) { - mcontext.nodes->emplace_back(argument{i}, ast::get_type(*node.arguments[i].type)); + module.nodes->emplace_back(argument{i}, ast::get_type(*node.arguments[i].type)); lcontext.variables[&node.arguments[i]] = last(); } apply(*node.statements); if (types::equal(*ast::get_type(*node.return_type), types::unit_type{})) - mcontext.nodes->emplace_back(return_value{}, std::make_shared(types::unit_type{})); + module.nodes->emplace_back(return_value{}, std::make_shared(types::unit_type{})); auto end = last(); lcontext.functions[&node] = {begin, end}; - mcontext.labels[&(*begin)] = lcontext.scopes.back().label_prefix + node.name; + module.labels[&(*begin)] = lcontext.scopes.back().label_prefix + node.name; lcontext.scopes.pop_back(); } @@ -846,7 +846,7 @@ namespace pslang::ir private: node_ref last() { - return std::prev(mcontext.nodes->end()); + return std::prev(module.nodes->end()); } }; @@ -854,7 +854,7 @@ namespace pslang::ir struct compile_visitor : ast::const_statement_visitor { - module_context & mcontext; + compiled_module & module; local_context & lcontext; using const_statement_visitor::apply; @@ -890,7 +890,7 @@ namespace pslang::ir void apply(ast::function_definition const & node) { - compile_function_visitor{{}, {}, mcontext, lcontext}.do_apply(node); + compile_function_visitor{{}, {}, module, lcontext}.do_apply(node); std::string label_prefix; if (!lcontext.scopes.empty()) label_prefix = lcontext.scopes.back().label_prefix + node.name + "."; @@ -911,10 +911,10 @@ namespace pslang::ir } - void compile(module_context & mcontext, ast::statement_list_ptr statements) + void compile(compiled_module & module, semantic::module const & module_in) { - if (!mcontext.nodes) - mcontext.nodes = std::make_shared(); + if (!module.nodes) + module.nodes = std::make_shared(); // Add a fake root AST function node // for module entry point @@ -928,20 +928,20 @@ namespace pslang::ir std::make_shared(types::unit_type{}), std::make_shared(types::function_type{{}, std::make_shared(types::unit_type{})}), }, - statements, + module_in.statements, {}, } ); local_context lcontext; - mcontext.nodes->emplace_back(label{}); - auto extra_label = std::prev(mcontext.nodes->end()); - compile_visitor{{}, mcontext, lcontext}.apply(*root); - mcontext.nodes->erase(extra_label); + module.nodes->emplace_back(label{}); + auto extra_label = std::prev(module.nodes->end()); + compile_visitor{{}, module, lcontext}.apply(*root); + module.nodes->erase(extra_label); - for (auto & symbol : lcontext.functions) - symbol.second.second++; + for (auto & function : lcontext.functions) + function.second.second++; for (auto const & resolve : lcontext.resolve_address) std::get(resolve.address->instruction).target = lcontext.functions.at(resolve.target).first; @@ -949,10 +949,10 @@ namespace pslang::ir for (auto const & resolve : lcontext.resolve_call) std::get(resolve.call->instruction).target = lcontext.functions.at(resolve.target).first; - for (auto const & symbol : lcontext.functions) - mcontext.symbols[symbol.first] = {.begin = symbol.second.first, .end = symbol.second.second}; + for (auto const & function : lcontext.functions) + module.functions[function.first] = {.begin = function.second.first, .end = function.second.second}; - mcontext.entry_point = lcontext.functions.at(std::get_if(root.get())).first; + module.entry_point = lcontext.functions.at(std::get_if(root.get())).first; } } diff --git a/libs/ir/source/print.cpp b/libs/ir/source/print.cpp index 6d68365..c4ac51d 100644 --- a/libs/ir/source/print.cpp +++ b/libs/ir/source/print.cpp @@ -316,7 +316,7 @@ namespace pslang::ir } }; - void print_impl(std::ostream & out, node_list const & nodes, module_context const * context) + void print_impl(std::ostream & out, node_list const & nodes, compiled_module const * module) { print_visitor visitor{out}; visitor.fill_index(nodes); @@ -324,12 +324,12 @@ namespace pslang::ir std::size_t index = 0; for (auto const & node : nodes) { - if (context && context->labels.contains(&node)) + if (module && module->labels.contains(&node)) { if (index > 0) out << '\n'; - out << context->labels.at(&node) << ":\n"; + out << module->labels.at(&node) << ":\n"; } - if (context) + if (module) out << " "; visitor.prelude(); out << index << ": "; @@ -351,9 +351,9 @@ namespace pslang::ir print_impl(out, nodes, nullptr); } - void print(std::ostream & out, module_context const & context) + void print(std::ostream & out, compiled_module const & module) { - print_impl(out, *context.nodes, &context); + print_impl(out, *module.nodes, &module); } } diff --git a/libs/jit/include/pslang/jit/arch/linux_x86_64/compiler.hpp b/libs/jit/include/pslang/jit/arch/linux_x86_64/compiler.hpp index 4157d8a..77a3c11 100644 --- a/libs/jit/include/pslang/jit/arch/linux_x86_64/compiler.hpp +++ b/libs/jit/include/pslang/jit/arch/linux_x86_64/compiler.hpp @@ -1,11 +1,11 @@ #pragma once #include -#include +#include namespace pslang::jit::linux_x86_64 { - void compile(program_context & pcontext, ir::module_context const & mcontext); + void compile(program_context & pcontext, ir::compiled_module const & module_in); } diff --git a/libs/jit/include/pslang/jit/arch/macos_aarch64/compiler.hpp b/libs/jit/include/pslang/jit/arch/macos_aarch64/compiler.hpp index e933590..3e74832 100644 --- a/libs/jit/include/pslang/jit/arch/macos_aarch64/compiler.hpp +++ b/libs/jit/include/pslang/jit/arch/macos_aarch64/compiler.hpp @@ -1,11 +1,11 @@ #pragma once #include -#include +#include namespace pslang::jit::macos_aarch64 { - void compile(program_context & pcontext, ir::module_context const & mcontext); + void compile(program_context & pcontext, ir::compiled_module const & module_in); } diff --git a/libs/jit/include/pslang/jit/jit.hpp b/libs/jit/include/pslang/jit/jit.hpp index a29e107..eb1373e 100644 --- a/libs/jit/include/pslang/jit/jit.hpp +++ b/libs/jit/include/pslang/jit/jit.hpp @@ -2,11 +2,11 @@ #include #include -#include +#include namespace pslang::jit { - void compile(program_context & pcontext, ir::module_context const & mcontext); + void compile(program_context & pcontext, ir::compiled_module const & module_in); } diff --git a/libs/jit/source/arch/linux_x86_64/compiler.cpp b/libs/jit/source/arch/linux_x86_64/compiler.cpp index 603c2bc..0482363 100644 --- a/libs/jit/source/arch/linux_x86_64/compiler.cpp +++ b/libs/jit/source/arch/linux_x86_64/compiler.cpp @@ -206,7 +206,7 @@ namespace pslang::jit::linux_x86_64 struct compile_visitor { program_context & pcontext; - ir::module_context const & mcontext; + ir::compiled_module const & module; local_context & lcontext; instruction_builder & builder; @@ -1281,14 +1281,14 @@ namespace pslang::jit::linux_x86_64 } - void compile(program_context & pcontext, ir::module_context const & mcontext) + void compile(program_context & pcontext, ir::compiled_module const & module_in) { local_context lcontext; auto data_begin = pcontext.storage.align(); { populate_globals_visitor visitor{.storage = pcontext.storage.storage, .lcontext = lcontext}; - for (auto it = mcontext.nodes->begin(); it != mcontext.nodes->end(); ++it) + for (auto it = module_in.nodes->begin(); it != module_in.nodes->end(); ++it) std::visit([&](auto const & instruction){ visitor.apply(it, instruction, it->inferred_type); }, it->instruction); } @@ -1308,20 +1308,20 @@ namespace pslang::jit::linux_x86_64 { populate_const_data_visitor visitor{pcontext, lcontext}; - for (auto it = mcontext.nodes->begin(); it != mcontext.nodes->end(); ++it) + for (auto it = module_in.nodes->begin(); it != module_in.nodes->end(); ++it) std::visit([&](auto const & instruction){ visitor.apply(instruction, it->inferred_type); }, it->instruction); } instruction_builder builder{pcontext.storage.storage}; - for (auto const & symbol : mcontext.symbols) + for (auto const & function : module_in.functions) { - pcontext.symbols[symbol.first] = pcontext.storage.size(); - compile_visitor visitor{pcontext, mcontext, lcontext, builder}; - visitor.compile(symbol.first, symbol.second.begin, symbol.second.end); + pcontext.symbols[function.first] = pcontext.storage.size(); + compile_visitor visitor{pcontext, module_in, lcontext, builder}; + visitor.compile(function.first, function.second.begin, function.second.end); } - pcontext.entry_point = lcontext.nodes.at(mcontext.entry_point); + pcontext.entry_point = lcontext.nodes.at(module_in.entry_point); for (auto const & resolve : lcontext.jump_resolve) builder.jump_inject_prev(pcontext.storage.storage.data() + resolve.offset, lcontext.nodes.at(resolve.target) - resolve.offset); diff --git a/libs/jit/source/arch/macos_aarch64/compiler.cpp b/libs/jit/source/arch/macos_aarch64/compiler.cpp index 0e8320c..d10d1c6 100644 --- a/libs/jit/source/arch/macos_aarch64/compiler.cpp +++ b/libs/jit/source/arch/macos_aarch64/compiler.cpp @@ -313,7 +313,7 @@ namespace pslang::jit::macos_aarch64 struct compile_visitor { program_context & pcontext; - ir::module_context const & mcontext; + ir::compiled_module const & module_in; local_context & lcontext; instruction_builder & builder; @@ -1217,14 +1217,14 @@ namespace pslang::jit::macos_aarch64 } - void compile(program_context & pcontext, ir::module_context const & mcontext) + void compile(program_context & pcontext, ir::compiled_module const & module_in) { local_context lcontext; auto data_begin = pcontext.storage.align(); { populate_globals_visitor visitor{.storage = pcontext.storage.storage, .lcontext = lcontext}; - for (auto it = mcontext.nodes->begin(); it != mcontext.nodes->end(); ++it) + for (auto it = module_in.nodes->begin(); it != module_in.nodes->end(); ++it) std::visit([&](auto const & instruction){ visitor.apply(it, instruction, it->inferred_type); }, it->instruction); } auto data_end = pcontext.storage.align(); @@ -1235,20 +1235,20 @@ namespace pslang::jit::macos_aarch64 { populate_const_data_visitor visitor{pcontext, lcontext}; - for (auto it = mcontext.nodes->begin(); it != mcontext.nodes->end(); ++it) + for (auto it = module_in.nodes->begin(); it != module_in.nodes->end(); ++it) std::visit([&](auto const & instruction){ visitor.apply(instruction, it->inferred_type); }, it->instruction); } instruction_builder builder{pcontext.storage.storage}; - for (auto const & symbol : mcontext.symbols) + for (auto const & function : module_in.functions) { - pcontext.symbols[symbol.first] = pcontext.storage.size(); - compile_visitor visitor{pcontext, mcontext, lcontext, builder}; - visitor.compile(symbol.first, symbol.second.begin, symbol.second.end); + pcontext.symbols[function.first] = pcontext.storage.size(); + compile_visitor visitor{pcontext, module_in, lcontext, builder}; + visitor.compile(function.first, function.second.begin, function.second.end); } - pcontext.entry_point = lcontext.nodes.at(mcontext.entry_point); + pcontext.entry_point = lcontext.nodes.at(module_in.entry_point); for (auto const & resolve : lcontext.branch_resolve) builder.b_inject(pcontext.storage.storage.data() + resolve.offset, (lcontext.nodes.at(resolve.target) - resolve.offset) / 4); diff --git a/libs/jit/source/jit.cpp b/libs/jit/source/jit.cpp index b59e63d..0135855 100644 --- a/libs/jit/source/jit.cpp +++ b/libs/jit/source/jit.cpp @@ -7,7 +7,7 @@ namespace pslang::jit { - void compile(program_context & pcontext, ir::module_context const & mcontext) + void compile(program_context & pcontext, ir::compiled_module const & module_in) { switch (pcontext.abi.platform) { @@ -15,7 +15,7 @@ namespace pslang::jit switch (pcontext.abi.isa) { case isa::x86_64: - linux_x86_64::compile(pcontext, mcontext); + linux_x86_64::compile(pcontext, module_in); break; case isa::aarch64: throw std::runtime_error("Linux aarch64 JIT compilation not supported"); @@ -30,7 +30,7 @@ namespace pslang::jit case isa::x86_64: throw std::runtime_error("macOS x86_64 JIT compilation not supported"); case isa::aarch64: - macos_aarch64::compile(pcontext, mcontext); + macos_aarch64::compile(pcontext, module_in); break; } break;