elna/gcc/elna-generic.cc

860 lines
33 KiB
C++

#include <array>
#include "elna/gcc/elna-generic.h"
#include "elna/gcc/elna-diagnostic.h"
#include "input.h"
#include "cgraph.h"
#include "gimplify.h"
#include "stringpool.h"
#include "diagnostic.h"
#include "realmpfr.h"
#include "stor-layout.h"
#include "varasm.h"
#include "fold-const.h"
#include <set>
namespace elna
{
namespace gcc
{
generic_visitor::generic_visitor(std::shared_ptr<boot::symbol_table<tree>> symbol_table)
{
this->symbol_map = symbol_table;
}
void generic_visitor::visit(boot::call_expression *expression)
{
tree symbol = this->symbol_map->lookup(expression->name());
if (symbol == NULL_TREE)
{
error_at(get_location(&expression->position()),
"procedure '%s' not declared",
expression->name().c_str());
this->current_expression = error_mark_node;
return;
}
tree return_type = TREE_TYPE(TREE_TYPE(symbol));
tree fndecl_type = build_function_type(return_type, TYPE_ARG_TYPES(symbol));
tree printf_fn = build1(ADDR_EXPR, build_pointer_type(fndecl_type), symbol);
std::vector<tree> arguments(expression->arguments().size());
for (std::size_t i = 0; i < expression->arguments().size(); ++i)
{
expression->arguments().at(i)->accept(this);
arguments[i] = this->current_expression;
}
tree stmt = build_call_array_loc(get_location(&expression->position()),
return_type, printf_fn, arguments.size(), arguments.data());
if (return_type == void_type_node)
{
append_to_statement_list(stmt, &this->current_statements);
this->current_expression = NULL_TREE;
}
else
{
this->current_expression = stmt;
}
}
void generic_visitor::visit(boot::cast_expression *expression)
{
tree cast_target = build_type(expression->target());
gcc_assert(cast_target != NULL_TREE);
expression->value().accept(this);
this->current_expression = build1_loc(get_location(&expression->position()), CONVERT_EXPR,
cast_target, this->current_expression);
}
void generic_visitor::visit(boot::size_of_expression *expression)
{
auto body_type = build_type(expression->body());
this->current_expression = build1(CONVERT_EXPR, this->symbol_map->lookup("Word"), TYPE_SIZE_UNIT(body_type));
}
bool generic_visitor::is_integral_type(tree type)
{
gcc_assert(TYPE_P(type));
return type == this->symbol_map->lookup("Int")
|| type == this->symbol_map->lookup("Word");
}
bool generic_visitor::is_numeric_type(tree type)
{
return is_integral_type(type)
|| type == this->symbol_map->lookup("Float");
}
void generic_visitor::visit(boot::program *program)
{
for (boot::constant_definition *const constant : program->constants)
{
constant->accept(this);
}
for (boot::type_definition *const type : program->types)
{
type->accept(this);
}
for (boot::variable_declaration *const variable : program->variables)
{
variable->accept(this);
}
for (boot::procedure_definition *const procedure : program->procedures)
{
procedure->accept(this);
}
std::array<tree, 2> parameter_types{
integer_type_node,
build_pointer_type(build_pointer_type(char_type_node))
};
tree declaration_type = build_function_type_array(integer_type_node, 2, parameter_types.data());
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;
enter_scope();
tree_chain argument_chain;
for (std::size_t i = 0; i < 2; ++i)
{
std::string argument_name = i == 0 ? "count" : "parameters";
tree argc_declaration_tree = build_decl(UNKNOWN_LOCATION, PARM_DECL,
get_identifier(argument_name.c_str()), parameter_types[i]);
DECL_CONTEXT(argc_declaration_tree) = this->main_fndecl;
DECL_ARG_TYPE(argc_declaration_tree) = parameter_types[i];
this->symbol_map->enter(argument_name, argc_declaration_tree);
argument_chain.append(argc_declaration_tree);
}
DECL_ARGUMENTS(this->main_fndecl) = argument_chain.head();
for (boot::statement *const body_statement : program->body)
{
body_statement->accept(this);
}
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);
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);
}
void generic_visitor::visit(boot::procedure_definition *definition)
{
std::vector<tree> parameter_types(definition->parameters.size());
for (std::size_t i = 0; i < definition->parameters.size(); ++i)
{
parameter_types[i] = build_type(definition->parameters.at(i)->type());
}
tree return_type = definition->return_type() == nullptr
? void_type_node
: build_type(*definition->return_type());
tree declaration_type = build_function_type_array(return_type,
definition->parameters.size(), parameter_types.data());
this->main_fndecl = build_fn_decl(definition->identifier().c_str(), declaration_type);
this->symbol_map->enter(definition->identifier(), this->main_fndecl);
if (definition->body() != nullptr)
{
tree resdecl = build_decl(UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, return_type);
DECL_CONTEXT(resdecl) = this->main_fndecl;
DECL_RESULT(this->main_fndecl) = resdecl;
enter_scope();
}
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];
if (definition->body() != nullptr)
{
this->symbol_map->enter(parameter->identifier(), declaration_tree);
}
argument_chain.append(declaration_tree);
}
DECL_ARGUMENTS(this->main_fndecl) = argument_chain.head();
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();
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);
}
else
{
DECL_EXTERNAL(this->main_fndecl) = 1;
}
}
void generic_visitor::enter_scope()
{
this->current_statements = alloc_stmt_list();
this->variable_chain = tree_chain();
this->symbol_map = std::make_shared<boot::symbol_table<tree>>(this->symbol_map);
}
tree_symbol_mapping generic_visitor::leave_scope()
{
tree new_block = build_block(variable_chain.head(),
NULL_TREE, NULL_TREE, NULL_TREE);
tree bind_expr = build3(BIND_EXPR, void_type_node, variable_chain.head(),
this->current_statements, new_block);
this->symbol_map = this->symbol_map->scope();
return tree_symbol_mapping{ bind_expr, new_block };
}
void generic_visitor::visit(boot::number_literal<std::int32_t> *literal)
{
auto symbol = this->symbol_map->lookup("Int");
this->current_expression = build_int_cst(symbol, literal->number());
}
void generic_visitor::visit(boot::number_literal<std::uint32_t> *literal)
{
auto symbol = this->symbol_map->lookup("Word");
this->current_expression = build_int_cstu(symbol, literal->number());
}
void generic_visitor::visit(boot::number_literal<double> *literal)
{
REAL_VALUE_TYPE real_value1;
mpfr_t number;
mpfr_init2(number, SIGNIFICAND_BITS);
mpfr_set_d(number, literal->number(), MPFR_RNDN);
real_from_mpfr(&real_value1, number, double_type_node, MPFR_RNDN);
this->current_expression = build_real(double_type_node, real_value1);
mpfr_clear(number);
}
void generic_visitor::visit(boot::number_literal<bool> *boolean)
{
auto symbol = this->symbol_map->lookup("Bool");
this->current_expression = build_int_cst_type(symbol, boolean->number());
}
void generic_visitor::visit(boot::number_literal<unsigned char> *character)
{
auto symbol = this->symbol_map->lookup("Char");
this->current_expression = build_int_cstu(symbol, character->number());
}
void generic_visitor::visit(boot::number_literal<nullptr_t> *)
{
this->current_expression = null_pointer_node;
}
void generic_visitor::visit(boot::string_literal *string)
{
this->current_expression = build_string_literal(string->string().size() + 1, string->string().c_str());
}
tree generic_visitor::build_arithmetic_operation(boot::binary_expression *expression,
tree_code operator_code, tree left, tree right)
{
return build_binary_operation(is_numeric_type(TREE_TYPE(left)),
expression, operator_code, left, right, TREE_TYPE(left));
}
tree generic_visitor::build_comparison_operation(boot::binary_expression *expression,
tree_code operator_code, tree left, tree right)
{
return build_binary_operation(is_numeric_type(TREE_TYPE(left)) || is_pointer_type(TREE_TYPE(left)),
expression, operator_code, left, right, this->symbol_map->lookup("Bool"));
}
tree generic_visitor::build_logic_operation(boot::binary_expression *expression,
tree_code operator_code, tree left, tree right)
{
auto symbol = this->symbol_map->lookup("Bool");
return build_binary_operation(TREE_TYPE(left) == symbol,
expression, operator_code, left, right, symbol);
}
tree generic_visitor::build_equality_operation(boot::binary_expression *expression,
tree_code operator_code, tree left, tree right)
{
return build_binary_operation(true, expression,
operator_code, left, right, this->symbol_map->lookup("Bool"));
}
void generic_visitor::visit(boot::binary_expression *expression)
{
expression->lhs().accept(this);
tree left = this->current_expression;
tree left_type = TREE_TYPE(left);
expression->rhs().accept(this);
tree right = this->current_expression;
tree right_type = TREE_TYPE(right);
location_t expression_location = get_location(&expression->position());
if (is_pointer_type(left_type) && is_integral_type(right_type))
{
this->current_expression = do_pointer_arithmetic(expression->operation(), left, right);
if (this->current_expression == error_mark_node)
{
error_at(expression_location,
"invalid operation %s on a pointer and an integral type",
boot::print_binary_operator(expression->operation()));
}
return;
}
if (left_type != right_type && !are_compatible_pointers(left, right))
{
error_at(expression_location,
"invalid operands of type %s and %s for operator %s",
print_type(left_type), print_type(right_type),
boot::print_binary_operator(expression->operation()));
this->current_expression = error_mark_node;
return;
}
switch (expression->operation())
{
case boot::binary_operator::sum:
this->current_expression = build_arithmetic_operation(expression, PLUS_EXPR, left, right);
break;
case boot::binary_operator::subtraction:
this->current_expression = build_arithmetic_operation(expression, MINUS_EXPR, left, right);
break;
case boot::binary_operator::division:
this->current_expression = build_arithmetic_operation(expression, TRUNC_DIV_EXPR, left, right);
break;
case boot::binary_operator::remainder:
this->current_expression = build_arithmetic_operation(expression, TRUNC_MOD_EXPR, left, right);
break;
case boot::binary_operator::multiplication:
this->current_expression = build_arithmetic_operation(expression, MULT_EXPR, left, right);
break;
case boot::binary_operator::less:
this->current_expression = build_comparison_operation(expression, LT_EXPR, left, right);
break;
case boot::binary_operator::greater:
this->current_expression = build_comparison_operation(expression, GT_EXPR, left, right);
break;
case boot::binary_operator::less_equal:
this->current_expression = build_comparison_operation(expression, LE_EXPR, left, right);
break;
case boot::binary_operator::greater_equal:
this->current_expression = build_comparison_operation(expression, GE_EXPR, left, right);
break;
case boot::binary_operator::conjunction:
this->current_expression = build_logic_operation(expression, TRUTH_ANDIF_EXPR, left, right);
break;
case boot::binary_operator::disjunction:
this->current_expression = build_logic_operation(expression, TRUTH_ORIF_EXPR, left, right);
break;
case boot::binary_operator::equals:
this->current_expression = build_equality_operation(expression, EQ_EXPR, left, right);
break;
case boot::binary_operator::not_equals:
this->current_expression = build_equality_operation(expression, NE_EXPR, left, right);
break;
}
}
void generic_visitor::visit(boot::unary_expression *expression)
{
expression->operand().accept(this);
switch (expression->operation())
{
case boot::unary_operator::reference:
TREE_ADDRESSABLE(this->current_expression) = 1;
this->current_expression = build_fold_addr_expr_with_type_loc(get_location(&expression->position()),
this->current_expression,
build_pointer_type_for_mode(TREE_TYPE(this->current_expression), VOIDmode, true));
TREE_NO_TRAMPOLINE(this->current_expression) = 1;
break;
case boot::unary_operator::negation:
this->current_expression = build1_loc(get_location(&expression->position()), TRUTH_NOT_EXPR,
boolean_type_node, this->current_expression);
break;
}
}
void generic_visitor::visit(boot::constant_definition *definition)
{
location_t definition_location = get_location(&definition->position());
definition->body().accept(this);
tree definition_tree = build_decl(definition_location, CONST_DECL,
get_identifier(definition->identifier().c_str()), TREE_TYPE(this->current_expression));
auto result = this->symbol_map->enter(definition->identifier(), definition_tree);
if (result)
{
DECL_INITIAL(definition_tree) = this->current_expression;
TREE_CONSTANT(definition_tree) = 1;
TREE_READONLY(definition_tree) = 1;
auto declaration_statement = build1_loc(definition_location, DECL_EXPR,
void_type_node, definition_tree);
append_to_statement_list(declaration_statement, &this->current_statements);
}
else
{
error_at(definition_location,
"variable '%s' already declared in this scope",
definition->identifier().c_str());
}
this->current_expression = NULL_TREE;
}
void generic_visitor::visit(boot::type_definition *definition)
{
tree tree_type = build_type(definition->body());
if (tree_type == NULL_TREE)
{
return;
}
location_t definition_location = get_location(&definition->position());
tree definition_tree = build_decl(definition_location, TYPE_DECL,
get_identifier(definition->identifier().c_str()), tree_type);
auto result = this->symbol_map->enter(definition->identifier(), tree_type);
if (result)
{
DECL_CONTEXT(definition_tree) = this->main_fndecl;
variable_chain.append(definition_tree);
auto declaration_statement = build1_loc(definition_location, DECL_EXPR,
void_type_node, definition_tree);
append_to_statement_list(declaration_statement, &this->current_statements);
}
else
{
error_at(definition_location,
"type '%s' already declared in this scope",
definition->identifier().c_str());
}
}
tree generic_visitor::build_type(boot::type_expression& type)
{
if (boot::basic_type_expression *basic_type = type.is_basic())
{
tree symbol = this->symbol_map->lookup(basic_type->base_name());
if (symbol != NULL_TREE && TYPE_P(symbol))
{
return symbol;
}
error_at(get_location(&basic_type->position()),
"type '%s' not declared", basic_type->base_name().c_str());
return error_mark_node;
}
else if (boot::array_type_expression *array_type = type.is_array())
{
tree lower_bound = build_int_cst_type(integer_type_node, 0);
tree upper_bound = build_int_cst_type(integer_type_node, array_type->size);
tree base_type = build_type(array_type->base());
if (base_type == NULL_TREE || base_type == error_mark_node)
{
return base_type;
}
tree range_type = build_range_type(integer_type_node, lower_bound, upper_bound);
return build_array_type(base_type, range_type);
}
else if (boot::pointer_type_expression *pointer_type = type.is_pointer())
{
tree base_type = build_type(pointer_type->base());
if (base_type == NULL_TREE || base_type == error_mark_node)
{
return base_type;
}
return build_pointer_type_for_mode(base_type, VOIDmode, true);
}
else if (boot::record_type_expression *record_type = type.is_record())
{
std::set<std::string> field_names;
tree record_type_node = make_node(RECORD_TYPE);
tree_chain record_chain;
for (auto& field : record_type->fields)
{
if (field_names.find(field.first) != field_names.cend())
{
error_at(get_location(&field.second->position()), "repeated field name");
return error_mark_node;
}
field_names.insert(field.first);
tree field_type = build_type(*field.second);
if (field_type == NULL_TREE || field_type == error_mark_node)
{
return field_type;
}
tree field_declaration = build_decl(get_location(&field.second->position()),
FIELD_DECL, get_identifier(field.first.c_str()), field_type);
TREE_ADDRESSABLE(field_declaration) = 1;
DECL_CONTEXT(field_declaration) = record_type_node;
record_chain.append(field_declaration);
}
TYPE_FIELDS(record_type_node) = record_chain.head();
layout_type(record_type_node);
return record_type_node;
}
else if (boot::union_type_expression *union_type = type.is_union())
{
std::set<std::string> field_names;
tree union_type_node = make_node(UNION_TYPE);
tree_chain union_chain;
for (auto& field : union_type->fields)
{
if (field_names.find(field.first) != field_names.cend())
{
error_at(get_location(&field.second->position()), "repeated field name");
return error_mark_node;
}
field_names.insert(field.first);
tree field_type = build_type(*field.second);
if (field_type == NULL_TREE || field_type == error_mark_node)
{
return field_type;
}
tree field_declaration = build_decl(get_location(&field.second->position()),
FIELD_DECL, get_identifier(field.first.c_str()), field_type);
TREE_ADDRESSABLE(field_declaration) = 1;
DECL_CONTEXT(field_declaration) = union_type_node;
union_chain.append(field_declaration);
}
TYPE_FIELDS(union_type_node) = union_chain.head();
layout_type(union_type_node);
return union_type_node;
}
return NULL_TREE;
}
void generic_visitor::visit(boot::variable_declaration *declaration)
{
tree declaration_type = build_type(declaration->type());
gcc_assert(declaration_type != NULL_TREE);
location_t declaration_location = get_location(&declaration->position());
tree declaration_tree = build_decl(declaration_location, VAR_DECL,
get_identifier(declaration->identifier().c_str()), declaration_type);
bool result = this->symbol_map->enter(declaration->identifier(), declaration_tree);
if (!result)
{
error_at(declaration_location, "variable '%s' already declared in this scope",
declaration->identifier().c_str());
}
else if (this->main_fndecl == NULL_TREE)
{
TREE_STATIC(declaration_tree) = 1;
varpool_node::get_create(declaration_tree);
varpool_node::finalize_decl(declaration_tree);
}
else
{
DECL_CONTEXT(declaration_tree) = this->main_fndecl;
variable_chain.append(declaration_tree);
auto declaration_statement = build1_loc(declaration_location, DECL_EXPR,
void_type_node, declaration_tree);
append_to_statement_list(declaration_statement, &this->current_statements);
}
}
void generic_visitor::visit(boot::variable_expression *expression)
{
auto symbol = this->symbol_map->lookup(expression->name());
if (symbol == NULL_TREE)
{
error_at(get_location(&expression->position()),
"variable '%s' not declared in the current scope",
expression->name().c_str());
this->current_expression = error_mark_node;
return;
}
this->current_expression = symbol;
}
void generic_visitor::visit(boot::array_access_expression *expression)
{
expression->base().accept(this);
tree designator = this->current_expression;
expression->index().accept(this);
tree index = this->current_expression;
tree element_type = TREE_TYPE(TREE_TYPE(designator));
this->current_expression = build4_loc(get_location(&expression->position()),
ARRAY_REF, element_type, designator, index, NULL_TREE, NULL_TREE);
}
void generic_visitor::visit(boot::field_access_expression *expression)
{
expression->base().accept(this);
tree field_declaration = TYPE_FIELDS(TREE_TYPE(this->current_expression));
while (field_declaration != NULL_TREE)
{
tree declaration_name = DECL_NAME(field_declaration);
const char *identifier_pointer = IDENTIFIER_POINTER(declaration_name);
if (expression->field() == identifier_pointer)
{
break;
}
field_declaration = TREE_CHAIN(field_declaration);
}
location_t expression_location = get_location(&expression->position());
if (field_declaration == NULL_TREE)
{
error_at(expression_location,
"record type does not have a field named '%s'",
expression->field().c_str());
this->current_expression = error_mark_node;
}
else
{
this->current_expression = build3_loc(expression_location, COMPONENT_REF,
TREE_TYPE(field_declaration), this->current_expression,
field_declaration, NULL_TREE);
}
}
void generic_visitor::visit(boot::dereference_expression *expression)
{
expression->base().accept(this);
this->current_expression = build1_loc(get_location(&expression->position()), INDIRECT_REF,
TREE_TYPE(TREE_TYPE(this->current_expression)), this->current_expression);
}
void generic_visitor::visit(boot::assign_statement *statement)
{
statement->lvalue().accept(this);
auto lvalue = this->current_expression;
auto statement_location = get_location(&statement->position());
statement->rvalue().accept(this);
if (TREE_CODE(lvalue) == CONST_DECL)
{
error_at(statement_location, "cannot modify constant '%s'",
statement->lvalue().is_variable()->name().c_str());
this->current_expression = error_mark_node;
return;
}
if (TREE_TYPE(this->current_expression) == TREE_TYPE(lvalue)
|| (is_pointer_type(TREE_TYPE(lvalue)) && this->current_expression == null_pointer_node))
{
tree assignment = build2_loc(statement_location, MODIFY_EXPR,
void_type_node, lvalue, this->current_expression);
append_to_statement_list(assignment, &this->current_statements);
this->current_expression = NULL_TREE;
}
else
{
error_at(statement_location,
"cannot assign value of type %s to variable of type %s",
print_type(TREE_TYPE(this->current_expression)),
print_type(TREE_TYPE(lvalue)));
this->current_expression = error_mark_node;
}
}
void generic_visitor::visit(boot::if_statement *statement)
{
tree endif_label_decl = build_label_decl("endif", UNKNOWN_LOCATION);
tree goto_endif = build1(GOTO_EXPR, void_type_node, endif_label_decl);
make_if_branch(statement->body(), goto_endif);
for (const auto branch : statement->branches)
{
make_if_branch(*branch, goto_endif);
}
if (statement->alternative() != nullptr)
{
for (const auto body_statement : *statement->alternative())
{
body_statement->accept(this);
}
}
tree endif_label_expr = build1(LABEL_EXPR, void_type_node, endif_label_decl);
append_to_statement_list(endif_label_expr, &this->current_statements);
this->current_expression = NULL_TREE;
}
void generic_visitor::make_if_branch(boot::conditional_statements& branch, tree goto_endif)
{
branch.prerequisite().accept(this);
if (TREE_TYPE(this->current_expression) != boolean_type_node)
{
error_at(get_location(&branch.prerequisite().position()),
"expected expression of boolean type but its type is %s",
print_type(TREE_TYPE(this->current_expression)));
this->current_expression = error_mark_node;
return;
}
tree then_label_decl = build_label_decl("then", UNKNOWN_LOCATION);
tree goto_then = build1(GOTO_EXPR, void_type_node, then_label_decl);
tree else_label_decl = build_label_decl("else", UNKNOWN_LOCATION);
tree goto_else = build1(GOTO_EXPR, void_type_node, else_label_decl);
auto cond_expr = build3(COND_EXPR, void_type_node, this->current_expression, goto_then, goto_else);
append_to_statement_list(cond_expr, &this->current_statements);
tree then_label_expr = build1(LABEL_EXPR, void_type_node, then_label_decl);
append_to_statement_list(then_label_expr, &this->current_statements);
for (const auto body_statement : branch.statements)
{
body_statement->accept(this);
}
append_to_statement_list(goto_endif, &this->current_statements);
tree else_label_expr = build1(LABEL_EXPR, void_type_node, else_label_decl);
append_to_statement_list(else_label_expr, &this->current_statements);
}
tree generic_visitor::build_label_decl(const char *name, location_t loc)
{
auto label_decl = build_decl(loc,
LABEL_DECL, get_identifier(name), void_type_node);
DECL_CONTEXT(label_decl) = this->main_fndecl;
return label_decl;
}
void generic_visitor::visit(boot::while_statement *statement)
{
statement->body().prerequisite().accept(this);
if (TREE_TYPE(this->current_expression) != boolean_type_node)
{
error_at(get_location(&statement->body().prerequisite().position()),
"expected expression of boolean type but its type is %s",
print_type(TREE_TYPE(this->current_expression)));
this->current_expression = error_mark_node;
return;
}
auto prerequisite_location = get_location(&statement->body().prerequisite().position());
auto body_location = get_location(&statement->position());
auto prerequisite_label_decl = build_label_decl("while_check", prerequisite_location);
auto prerequisite_label_expr = build1_loc(prerequisite_location, LABEL_EXPR,
void_type_node, prerequisite_label_decl);
append_to_statement_list(prerequisite_label_expr, &this->current_statements);
auto body_label_decl = build_label_decl("while_body", body_location);
auto end_label_decl = build_label_decl("end_while", UNKNOWN_LOCATION);
auto goto_body = build1_loc(prerequisite_location, GOTO_EXPR,
void_type_node, body_label_decl);
auto goto_end = build1_loc(prerequisite_location, GOTO_EXPR,
void_type_node, end_label_decl);
auto cond_expr = build3_loc(prerequisite_location, COND_EXPR,
void_type_node, this->current_expression, goto_body, goto_end);
append_to_statement_list(cond_expr, &this->current_statements);
auto body_label_expr = build1_loc(body_location, LABEL_EXPR,
void_type_node, body_label_decl);
append_to_statement_list(body_label_expr, &this->current_statements);
for (const auto body_statement : statement->body().statements)
{
body_statement->accept(this);
}
auto goto_check = build1(GOTO_EXPR, void_type_node, prerequisite_label_decl);
append_to_statement_list(goto_check, &this->current_statements);
auto endif_label_expr = build1(LABEL_EXPR, void_type_node, end_label_decl);
append_to_statement_list(endif_label_expr, &this->current_statements);
this->current_expression = NULL_TREE;
}
void generic_visitor::visit(boot::call_statement *statement)
{
statement->body().accept(this);
append_to_statement_list(this->current_expression, &this->current_statements);
}
void generic_visitor::visit(boot::return_statement *statement)
{
boot::expression *return_expression = statement->return_expression();
if (return_expression == nullptr)
{
return;
}
return_expression->accept(this);
tree set_result = build2(INIT_EXPR, void_type_node, DECL_RESULT(main_fndecl),
this->current_expression);
tree return_stmt = build1(RETURN_EXPR, void_type_node, set_result);
append_to_statement_list(return_stmt, &this->current_statements);
}
}
}