Implement external functions

This commit is contained in:
2025-01-16 15:09:58 +01:00
parent 1e45d66359
commit ef667e3ace
7 changed files with 98 additions and 73 deletions

View File

@ -132,12 +132,14 @@ namespace gcc
enter_scope();
for (const auto& definition : program->value_definitions)
for (const auto definition : program->value_definitions)
{
definition->accept(this);
}
program->body().accept(this);
for (const auto body_statement : program->body)
{
body_statement->accept(this);
}
append_to_statement_list(return_stmt, &this->current_statements);
tree_symbol_mapping mapping = leave_scope();
@ -155,53 +157,64 @@ namespace gcc
void generic_visitor::visit(source::procedure_definition *definition)
{
std::vector<tree> parameter_types(definition->parameters().size());
std::vector<tree> parameter_types(definition->parameters.size());
for (std::size_t i = 0; i < definition->parameters().size(); ++i)
for (std::size_t i = 0; i < definition->parameters.size(); ++i)
{
parameter_types[i] = build_type(definition->parameters().at(i)->type());
parameter_types[i] = build_type(definition->parameters.at(i)->type());
}
tree declaration_type = build_function_type_array(void_type_node,
definition->parameters().size(), parameter_types.data());
definition->parameters.size(), parameter_types.data());
this->main_fndecl = build_fn_decl(definition->identifier().c_str(), declaration_type);
this->symbol_map->enter(definition->identifier(), source::make_info(this->main_fndecl));
tree resdecl = build_decl(UNKNOWN_LOCATION, RESULT_DECL,
NULL_TREE, TREE_TYPE(TREE_TYPE(this->main_fndecl)));
DECL_CONTEXT(resdecl) = this->main_fndecl;
DECL_RESULT(this->main_fndecl) = resdecl;
enter_scope();
gcc::tree_chain argument_chain;
for (std::size_t i = 0; i < definition->parameters().size(); ++i)
if (definition->body() != nullptr)
{
auto parameter = definition->parameters().at(i);
tree resdecl = build_decl(UNKNOWN_LOCATION, RESULT_DECL,
NULL_TREE, TREE_TYPE(TREE_TYPE(this->main_fndecl)));
DECL_CONTEXT(resdecl) = this->main_fndecl;
DECL_RESULT(this->main_fndecl) = resdecl;
enter_scope();
}
gcc::tree_chain argument_chain;
for (std::size_t i = 0; i < definition->parameters.size(); ++i)
{
auto parameter = definition->parameters.at(i);
tree declaration_tree = build_decl(get_location(&parameter->position()), PARM_DECL,
get_identifier(parameter->identifier().c_str()), parameter_types[i]);
DECL_CONTEXT(declaration_tree) = this->main_fndecl;
DECL_ARG_TYPE(declaration_tree) = parameter_types[i];
this->symbol_map->enter(parameter->identifier(), source::make_info(declaration_tree));
if (definition->body() != nullptr)
{
this->symbol_map->enter(parameter->identifier(), source::make_info(declaration_tree));
}
argument_chain.append(declaration_tree);
}
DECL_ARGUMENTS(this->main_fndecl) = argument_chain.head();
definition->body().accept(this);
tree_symbol_mapping mapping = leave_scope();
if (definition->body() != nullptr)
{
definition->body()->accept(this);
tree_symbol_mapping mapping = leave_scope();
BLOCK_SUPERCONTEXT(mapping.block()) = this->main_fndecl;
DECL_INITIAL(this->main_fndecl) = mapping.block();
DECL_SAVED_TREE(this->main_fndecl) = mapping.bind_expression();
BLOCK_SUPERCONTEXT(mapping.block()) = this->main_fndecl;
DECL_INITIAL(this->main_fndecl) = mapping.block();
DECL_SAVED_TREE(this->main_fndecl) = mapping.bind_expression();
DECL_EXTERNAL(this->main_fndecl) = 0;
DECL_PRESERVE_P(this->main_fndecl) = 1;
DECL_EXTERNAL(this->main_fndecl) = 0;
DECL_PRESERVE_P(this->main_fndecl) = 1;
gimplify_function_tree(this->main_fndecl);
gimplify_function_tree(this->main_fndecl);
cgraph_node::finalize_function(this->main_fndecl, true);
cgraph_node::finalize_function(this->main_fndecl, true);
}
else
{
DECL_EXTERNAL(this->main_fndecl) = 1;
}
}
void generic_visitor::enter_scope()