Implement parameterless prcoedures

This commit is contained in:
2025-01-11 13:32:37 +01:00
parent 35c32fcf3f
commit 0fcf29c17e
6 changed files with 260 additions and 113 deletions

View File

@ -8,6 +8,7 @@
#include "diagnostic.h"
#include "realmpfr.h"
#include "stor-layout.h"
#include "varasm.h"
#include <set>
namespace elna
@ -16,81 +17,100 @@ namespace gcc
{
void generic_visitor::visit(source::call_statement *statement)
{
if (statement->name() != "writei")
if (statement->name() == "writei")
{
error_at(get_location(&statement->position()),
"procedure '%s' not declared",
statement->name().c_str());
return;
}
if (statement->arguments().size() != 1)
{
error_at(get_location(&statement->position()),
"procedure '%s' expects 1 argument, %lu given",
statement->name().c_str(), statement->arguments().size());
return;
}
auto& argument = statement->arguments().at(0);
argument->accept(this);
auto argument_type = TREE_TYPE(this->current_expression);
if (statement->arguments().size() != 1)
{
error_at(get_location(&statement->position()),
"procedure '%s' expects 1 argument, %lu given",
statement->name().c_str(), statement->arguments().size());
return;
}
auto& argument = statement->arguments().at(0);
argument->accept(this);
auto argument_type = TREE_TYPE(this->current_expression);
const char *format_number{ nullptr };
if (argument_type == integer_type_node)
{
format_number = "%d\n";
}
else if (argument_type == double_type_node)
{
format_number = "%f\n";
}
else if (argument_type == elna_char_type_node)
{
format_number = "%c\n";
}
else if (is_string_type(argument_type))
{
format_number = "%s\n";
}
else if (is_pointer_type(argument_type))
{
format_number = "%p\n";
const char *format_number{ nullptr };
if (argument_type == integer_type_node)
{
format_number = "%d\n";
}
else if (argument_type == double_type_node)
{
format_number = "%f\n";
}
else if (argument_type == elna_char_type_node)
{
format_number = "%c\n";
}
else if (is_string_type(argument_type))
{
format_number = "%s\n";
}
else if (is_pointer_type(argument_type))
{
format_number = "%p\n";
}
else
{
error_at(get_location(&argument->position()),
"invalid argument of type %s for procedure %s",
print_type(argument_type), statement->name().c_str());
this->current_expression = error_mark_node;
return;
}
tree args[] = {
build_string_literal(strlen(format_number) + 1, format_number),
this->current_expression
};
tree fndecl_type_param[] = {
build_pointer_type(build_qualified_type(char_type_node, TYPE_QUAL_CONST)) /* const char* */
};
tree fndecl_type = build_varargs_function_type_array(integer_type_node, 1, fndecl_type_param);
tree printf_fn_decl = build_fn_decl("printf", fndecl_type);
DECL_EXTERNAL(printf_fn_decl) = 1;
tree printf_fn = build1(ADDR_EXPR, build_pointer_type(fndecl_type), printf_fn_decl);
tree stmt = build_call_array(integer_type_node, printf_fn, 2, args);
append_to_statement_list(stmt, &this->current_statements);
this->current_expression = NULL_TREE;
}
else
{
error_at(get_location(&argument->position()),
"invalid argument of type %s for procedure %s",
print_type(argument_type), statement->name().c_str());
this->current_expression = error_mark_node;
return;
tree fndecl_type = build_function_type_list(integer_type_node, NULL_TREE);
tree printf_fn_decl = build_fn_decl(statement->name().c_str(), fndecl_type);
DECL_EXTERNAL(printf_fn_decl) = 1;
tree printf_fn = build1(ADDR_EXPR, build_pointer_type(fndecl_type), printf_fn_decl);
tree stmt = build_call_nary(integer_type_node, printf_fn, 0);
append_to_statement_list(stmt, &this->current_statements);
this->current_expression = NULL_TREE;
/*
error_at(get_location(&statement->position()),
"procedure '%s' not declared",
statement->name().c_str()); */
}
tree args[] = {
build_string_literal(strlen(format_number) + 1, format_number),
this->current_expression
};
tree fndecl_type_param[] = {
build_pointer_type(build_qualified_type(char_type_node, TYPE_QUAL_CONST)) /* const char* */
};
tree fndecl_type = build_varargs_function_type_array(integer_type_node, 1, fndecl_type_param);
tree printf_fn_decl = build_fn_decl("printf", fndecl_type);
DECL_EXTERNAL(printf_fn_decl) = 1;
tree printf_fn = build1(ADDR_EXPR, build_pointer_type(fndecl_type), printf_fn_decl);
tree stmt = build_call_array(integer_type_node, printf_fn, 2, args);
append_to_statement_list(stmt, &this->current_statements);
this->current_expression = NULL_TREE;
}
void generic_visitor::visit(source::program *program)
{
tree main_fndecl_type_param[] = {
for (const auto& constant : program->definitions())
{
constant->accept(this);
}
tree parameter_types[] = {
integer_type_node,
build_pointer_type(build_pointer_type(char_type_node))
};
tree main_fndecl_type = build_function_type_array(integer_type_node, 2, main_fndecl_type_param);
this->main_fndecl = build_fn_decl("main", main_fndecl_type);
tree declaration_type = build_function_type_array(integer_type_node, 2, parameter_types);
this->main_fndecl = build_fn_decl("main", declaration_type);
tree resdecl = build_decl(UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, integer_type_node);
DECL_CONTEXT(resdecl) = this->main_fndecl;
DECL_RESULT(this->main_fndecl) = resdecl;
@ -100,9 +120,13 @@ namespace gcc
enter_scope();
empty_visitor::visit(program);
append_to_statement_list(return_stmt, &this->current_statements);
for (const auto& definition : program->value_definitions)
{
definition->accept(this);
}
program->body().accept(this);
append_to_statement_list(return_stmt, &this->current_statements);
tree_symbol_mapping mapping = leave_scope();
BLOCK_SUPERCONTEXT(mapping.block()) = this->main_fndecl;
@ -117,6 +141,44 @@ namespace gcc
cgraph_node::finalize_function(this->main_fndecl, true);
}
void generic_visitor::visit(source::procedure_definition *definition)
{
tree *parameter_types = reinterpret_cast<tree *>(xmalloc(definition->parameters().size() * sizeof(tree)));
for (std::size_t i = 0; i < definition->parameters().size(); ++i)
{
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);
this->main_fndecl = build_fn_decl(definition->identifier().c_str(), declaration_type);
tree resdecl = build_decl(UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, integer_type_node);
DECL_CONTEXT(resdecl) = this->main_fndecl;
DECL_RESULT(this->main_fndecl) = resdecl;
tree set_result = build2(INIT_EXPR, void_type_node, DECL_RESULT(main_fndecl),
build_int_cst_type(integer_type_node, 0));
tree return_stmt = build1(RETURN_EXPR, void_type_node, set_result);
enter_scope();
definition->body().accept(this);
append_to_statement_list(return_stmt, &this->current_statements);
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();
DECL_EXTERNAL(this->main_fndecl) = 0;
DECL_PRESERVE_P(this->main_fndecl) = 1;
gimplify_function_tree(this->main_fndecl);
cgraph_node::finalize_function(this->main_fndecl, true);
free(parameter_types);
}
void generic_visitor::enter_scope()
{
this->current_statements = alloc_stmt_list();
@ -426,7 +488,7 @@ namespace gcc
return NULL_TREE;
}
void generic_visitor::visit(source::declaration *declaration)
void generic_visitor::visit(source::variable_declaration *declaration)
{
tree declaration_type = build_type(declaration->type());
gcc_assert(declaration_type != NULL_TREE);