#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) { const char *format_integer = "%d\n"; tree args[] = { build_string_literal(strlen(format_integer) + 1, format_integer), build_int_cst_type(integer_type_node, 8) }; 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, 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); 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); } } }