Modules wip: refactor IR compiler

This commit is contained in:
Nikita Lisitsa 2026-08-02 22:27:26 +03:00
parent cb84430883
commit 5c3e59eaf5
12 changed files with 201 additions and 191 deletions

View file

@ -97,7 +97,7 @@ int main(int argc, char ** argv)
std::vector<std::string> filenames;
std::vector<semantic::module> modules;
std::vector<ir::module_context> ir_compiled;
std::vector<ir::compiled_module> 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)
{

View file

@ -1,36 +1,10 @@
#pragma once
#include <pslang/ast/statement_fwd.hpp>
#include <pslang/ir/node_fwd.hpp>
#include <unordered_map>
namespace pslang::ast
{
struct function_definition;
}
#include <pslang/ir/module.hpp>
namespace pslang::ir
{
struct module_context
{
node_list_ptr nodes;
std::unordered_map<node const *, std::string> labels;
struct symbol_info
{
node_ref begin;
node_ref end;
};
std::unordered_map<ast::function_definition const *, symbol_info> symbols;
node_ref entry_point;
};
void compile(module_context & context, ast::statement_list_ptr statements);
void compile(compiled_module & result, semantic::module const & module);
}

View file

@ -0,0 +1,36 @@
#pragma once
#include <pslang/semantic/module.hpp>
#include <pslang/ir/node_fwd.hpp>
#include <unordered_map>
namespace pslang::ast
{
struct function_definition;
}
namespace pslang::ir
{
struct compiled_module
{
node_list_ptr nodes;
// Needed for debug IR dump
std::unordered_map<node const *, std::string> labels;
struct function_info
{
node_ref begin;
node_ref end;
};
std::unordered_map<ast::function_definition const *, function_info> functions;
node_ref entry_point;
};
}

View file

@ -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);
}
}

View file

@ -53,14 +53,14 @@ namespace pslang::ir
struct zero_literal_visitor
: types::const_visitor<zero_literal_visitor>
{
module_context & mcontext;
compiled_module & module;
using const_visitor::apply;
template <typename T>
void apply(types::primitive_type_base<T>)
{
mcontext.nodes->emplace_back(literal{ast::literal{ast::primitive_literal_base<T>{.value = {}}}},
module.nodes->emplace_back(literal{ast::literal{ast::primitive_literal_base<T>{.value = {}}}},
std::make_shared<types::type>(types::primitive_type{types::primitive_type_base<T>{}}));
}
@ -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 <typename Node>
@ -94,7 +94,7 @@ namespace pslang::ir
template <typename T>
node_ref apply(ast::primitive_literal_base<T> 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_if_zero>(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_if_zero>(jump->instruction).target = last();
return result;
}
@ -206,28 +206,28 @@ namespace pslang::ir
auto i64_type = std::make_shared<types::type>(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::type>(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<ir::jump_if_zero>(&jump_node->instruction))
jump_if_zero->target = last();
else if (auto jump_if_nonzero = std::get_if<ir::jump_if_nonzero>(&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<ast::identifier>(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<types::struct_type>(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::type>(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::type>(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_if_zero>(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<ast::identifier>(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<ast::field_access>(lhs_node.get()))
@ -572,7 +572,7 @@ namespace pslang::ir
if (auto identifier = std::get_if<ast::identifier>(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::type>(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::type>(types::primitive_type{types::bool_type{}}));
module.nodes->emplace_back(global{.initializer = {0}}, std::make_unique<types::type>(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::type>(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::type>(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_if_nonzero>(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_if_zero>((*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_if_zero>(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::type>(types::unit_type{}));
module.nodes->emplace_back(return_value{}, std::make_shared<types::type>(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::type>(types::unit_type{}));
module.nodes->emplace_back(return_value{}, std::make_shared<types::type>(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<compile_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<node_list>();
if (!module.nodes)
module.nodes = std::make_shared<node_list>();
// Add a fake root AST function node
// for module entry point
@ -928,20 +928,20 @@ namespace pslang::ir
std::make_shared<types::type>(types::unit_type{}),
std::make_shared<types::type>(types::function_type{{}, std::make_shared<types::type>(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<instruction_address>(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<call>(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<ast::function_definition>(root.get())).first;
module.entry_point = lcontext.functions.at(std::get_if<ast::function_definition>(root.get())).first;
}
}

View file

@ -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);
}
}

View file

@ -1,11 +1,11 @@
#pragma once
#include <pslang/jit/jit.hpp>
#include <pslang/ir/compiler.hpp>
#include <pslang/ir/module.hpp>
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);
}

View file

@ -1,11 +1,11 @@
#pragma once
#include <pslang/jit/jit.hpp>
#include <pslang/ir/compiler.hpp>
#include <pslang/ir/module.hpp>
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);
}

View file

@ -2,11 +2,11 @@
#include <pslang/ast/statement_fwd.hpp>
#include <pslang/jit/program_context.hpp>
#include <pslang/ir/compiler.hpp>
#include <pslang/ir/module.hpp>
namespace pslang::jit
{
void compile(program_context & pcontext, ir::module_context const & mcontext);
void compile(program_context & pcontext, ir::compiled_module const & module_in);
}

View file

@ -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);

View file

@ -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);

View file

@ -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;