2024-12-23 13:54:11 +01:00
|
|
|
#include "elna/gcc/generic-visitor.h"
|
|
|
|
|
|
|
|
#include "input.h"
|
|
|
|
#include "cgraph.h"
|
|
|
|
#include "gimplify.h"
|
|
|
|
|
|
|
|
namespace elna
|
|
|
|
{
|
|
|
|
namespace gcc
|
|
|
|
{
|
|
|
|
void generic_visitor::visit(source::call_statement *statement)
|
|
|
|
{
|
2024-12-27 10:51:46 +01:00
|
|
|
empty_visitor::visit(statement);
|
|
|
|
|
2024-12-23 13:54:11 +01:00
|
|
|
const char *format_integer = "%d\n";
|
|
|
|
tree args[] = {
|
|
|
|
build_string_literal(strlen(format_integer) + 1, format_integer),
|
2024-12-27 10:51:46 +01:00
|
|
|
this->current_expression
|
2024-12-23 13:54:11 +01:00
|
|
|
};
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void generic_visitor::visit(source::program *program)
|
|
|
|
{
|
|
|
|
tree main_fndecl_type_param[] = {
|
|
|
|
integer_type_node,
|
2024-12-27 10:51:46 +01:00
|
|
|
build_pointer_type(build_pointer_type (char_type_node))
|
2024-12-23 13:54:11 +01:00
|
|
|
};
|
|
|
|
tree main_fndecl_type = build_function_type_array(integer_type_node, 2, main_fndecl_type_param);
|
|
|
|
tree main_fndecl = build_fn_decl("main", main_fndecl_type);
|
|
|
|
tree resdecl = build_decl(UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, integer_type_node);
|
|
|
|
DECL_RESULT(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);
|
|
|
|
|
|
|
|
this->current_statements = alloc_stmt_list();
|
|
|
|
|
|
|
|
empty_visitor::visit(program);
|
|
|
|
|
|
|
|
append_to_statement_list(return_stmt, &this->current_statements);
|
|
|
|
|
|
|
|
tree new_block = build_block(NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
|
|
|
|
tree bind_expr = build3(BIND_EXPR, void_type_node, NULL_TREE, this->current_statements, new_block);
|
|
|
|
|
|
|
|
BLOCK_SUPERCONTEXT(new_block) = main_fndecl;
|
|
|
|
DECL_INITIAL(main_fndecl) = new_block;
|
|
|
|
DECL_SAVED_TREE(main_fndecl) = bind_expr;
|
|
|
|
|
|
|
|
DECL_EXTERNAL(main_fndecl) = 0;
|
|
|
|
DECL_PRESERVE_P(main_fndecl) = 1;
|
|
|
|
|
|
|
|
gimplify_function_tree(main_fndecl);
|
|
|
|
|
|
|
|
cgraph_node::finalize_function(main_fndecl, true);
|
|
|
|
}
|
2024-12-27 10:51:46 +01:00
|
|
|
|
|
|
|
void generic_visitor::visit(source::integer_literal *literal)
|
|
|
|
{
|
|
|
|
current_expression = build_int_cst_type(integer_type_node, literal->number());
|
|
|
|
}
|
|
|
|
|
|
|
|
void generic_visitor::visit(source::binary_expression *expression)
|
|
|
|
{
|
|
|
|
expression->lhs().accept(this);
|
|
|
|
auto left = this->current_expression;
|
|
|
|
|
|
|
|
expression->rhs().accept(this);
|
|
|
|
auto right = this->current_expression;
|
|
|
|
|
|
|
|
tree_code operator_code{};
|
|
|
|
|
|
|
|
switch (expression->operation())
|
|
|
|
{
|
|
|
|
case source::binary_operator::sum:
|
|
|
|
operator_code = PLUS_EXPR;
|
|
|
|
break;
|
|
|
|
case source::binary_operator::subtraction:
|
|
|
|
operator_code = MINUS_EXPR;
|
|
|
|
break;
|
|
|
|
case source::binary_operator::division:
|
|
|
|
operator_code = TRUNC_DIV_EXPR;
|
|
|
|
break;
|
|
|
|
case source::binary_operator::multiplication:
|
|
|
|
operator_code = MULT_EXPR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
gcc_unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->current_expression = build2(operator_code, integer_type_node, left, right);
|
|
|
|
}
|
2024-12-23 13:54:11 +01:00
|
|
|
}
|
|
|
|
}
|