diff --git a/example.elna b/example.elna new file mode 100644 index 0000000..8b8f8fc --- /dev/null +++ b/example.elna @@ -0,0 +1,81 @@ +type + T = array 5 of Int, + R = record + x: Int; + y: Int + end; + +const t = 5; + +proc test_string(); +var s: String; +begin + s := "Test string."; + + writei(""); + writei(s) +end; + +proc test_array(); +var a: T, x_3: Int; +begin + a[0] := 2; + a[1] := 5; + + writei(""); + writei("Test array:"); + + x_3 := 0; + while x_3 < 2 do + begin + writei(a[x_3]); + x_3 := x_3 + 1 + end +end; + +proc test_pointer(); +var x_2: Int, p: ^Int; +begin + x_2 := 5; + p := @x_2; + + writei(""); + writei("Test pointer:"); + writei(p); + writei(p^) +end; + +proc test_record(); +var r: R; +begin + writei(""); + writei("Test record:"); + + r.x := 4; + r.y := 8; + + writei(r.y) +end; + +var x_1: Int, y: Bool, z: Float, c: Char; +begin + z := 8.2; + x_1 := t; + y := false; + c := 'x'; + + if y then + z := z + 3.0 + else + z := z + 2.0; + + writei(z); + + writei(x_1); + writei(c); + + test_string(); + test_array(); + test_pointer(); + test_record() +end. diff --git a/gcc/elna-generic.cc b/gcc/elna-generic.cc index 83a41b4..c4b0264 100644 --- a/gcc/elna-generic.cc +++ b/gcc/elna-generic.cc @@ -8,6 +8,7 @@ #include "diagnostic.h" #include "realmpfr.h" #include "stor-layout.h" +#include "varasm.h" #include 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(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); diff --git a/include/elna/gcc/elna-generic.h b/include/elna/gcc/elna-generic.h index 2639474..4b8c3c8 100644 --- a/include/elna/gcc/elna-generic.h +++ b/include/elna/gcc/elna-generic.h @@ -32,6 +32,7 @@ namespace gcc public: void visit(source::program *program) override; + void visit(source::procedure_definition *definition) override; void visit(source::call_statement *statement) override; void visit(source::number_literal *literal) override; void visit(source::number_literal *literal) override; @@ -42,7 +43,7 @@ namespace gcc void visit(source::unary_expression *expression) override; void visit(source::constant_definition *definition) override; void visit(source::type_definition *definition) override; - void visit(source::declaration *declaration) override; + void visit(source::variable_declaration *declaration) override; void visit(source::variable_expression *expression) override; void visit(source::array_access_expression *expression) override; void visit(source::field_access_expression *expression) override; diff --git a/include/elna/source/ast.h b/include/elna/source/ast.h index 07ed8fe..e090df9 100644 --- a/include/elna/source/ast.h +++ b/include/elna/source/ast.h @@ -33,7 +33,7 @@ namespace source reference }; - class declaration; + class variable_declaration; class constant_definition; class procedure_definition; class type_definition; @@ -64,7 +64,7 @@ namespace source */ struct parser_visitor { - virtual void visit(declaration *) = 0; + virtual void visit(variable_declaration *) = 0; virtual void visit(constant_definition *) = 0; virtual void visit(procedure_definition *) = 0; virtual void visit(type_definition *) = 0; @@ -97,7 +97,7 @@ namespace source */ struct empty_visitor : parser_visitor { - virtual void visit(declaration *) override; + virtual void visit(variable_declaration *) override; virtual void visit(constant_definition *definition) override; virtual void visit(procedure_definition *definition) override; virtual void visit(type_definition *definition) override; @@ -331,7 +331,7 @@ namespace source /** * Variable declaration. */ - class declaration : public definition + class variable_declaration : public definition { type_expression *m_type; @@ -343,13 +343,13 @@ namespace source * \param identifier Definition name. * \param type Declared type. */ - declaration(const struct position position, const std::string& identifier, + variable_declaration(const struct position position, const std::string& identifier, type_expression *type); virtual void accept(parser_visitor *visitor) override; type_expression& type(); - virtual ~declaration() override; + virtual ~variable_declaration() override; }; /** @@ -380,7 +380,7 @@ namespace source class procedure_definition : public definition { block *m_body; - std::vector m_parameters; + std::vector m_parameters; public: /** @@ -393,7 +393,7 @@ namespace source virtual void accept(parser_visitor *visitor) override; block& body(); - std::vector& parameters(); + std::vector& parameters(); virtual ~procedure_definition() override; }; @@ -597,18 +597,18 @@ namespace source class block : public node { std::vector m_definitions; - std::vector m_declarations; statement *m_body; public: + std::vector value_definitions; + block(const struct position position, std::vector&& definitions, - std::vector&& declarations, + std::vector&& value_definitions, statement *body); virtual void accept(parser_visitor *visitor) override; statement& body(); std::vector& definitions(); - std::vector& declarations(); virtual ~block() override; }; @@ -617,8 +617,7 @@ namespace source { public: program(const struct position position, std::vector&& definitions, - std::vector&& declarations, - statement *body); + std::vector&& value_definitions, statement *body); virtual void accept(parser_visitor *visitor) override; }; diff --git a/source/ast.cc b/source/ast.cc index ad36dc0..1f2cb35 100644 --- a/source/ast.cc +++ b/source/ast.cc @@ -7,7 +7,7 @@ namespace elna { namespace source { - void empty_visitor::visit(declaration *) + void empty_visitor::visit(variable_declaration *) { } @@ -69,9 +69,9 @@ namespace source { constant->accept(this); } - for (const auto& block_declaration : block->declarations()) + for (const auto& definition : block->value_definitions) { - block_declaration->accept(this); + definition->accept(this); } block->body().accept(this); } @@ -344,23 +344,23 @@ namespace source } } - declaration::declaration(const struct position position, const std::string& identifier, + variable_declaration::variable_declaration(const struct position position, const std::string& identifier, type_expression *type) : definition(position, identifier), m_type(type) { } - declaration::~declaration() + variable_declaration::~variable_declaration() { delete m_type; } - void declaration::accept(parser_visitor *visitor) + void variable_declaration::accept(parser_visitor *visitor) { visitor->visit(this); } - type_expression& declaration::type() + type_expression& variable_declaration::type() { return *m_type; } @@ -412,7 +412,7 @@ namespace source return *m_body; } - std::vector& procedure_definition::parameters() + std::vector& procedure_definition::parameters() { return m_parameters; } @@ -448,10 +448,10 @@ namespace source } block::block(const struct position position, std::vector&& definitions, - std::vector&& declarations, + std::vector&& value_definitions, statement *body) : node(position), m_definitions(std::move(definitions)), - m_declarations(std::move(declarations)), m_body(std::move(body)) + m_body(std::move(body)), value_definitions(std::move(value_definitions)) { } @@ -460,19 +460,14 @@ namespace source visitor->visit(this); } - statement& block::body() - { - return *m_body; - } - std::vector& block::definitions() { return m_definitions; } - std::vector& block::declarations() + statement& block::body() { - return m_declarations; + return *m_body; } block::~block() @@ -481,17 +476,16 @@ namespace source { delete definition; } - for (auto declaration : m_declarations) + for (auto definition : value_definitions) { - delete declaration; + delete definition; } delete m_body; } program::program(const struct position position, std::vector&& definitions, - std::vector&& declarations, - statement *body) - : block(position, std::move(definitions), std::move(declarations), body) + std::vector&& value_definitions, statement *body) + : block(position, std::move(definitions), std::move(value_definitions), body) { } diff --git a/source/parser.yy b/source/parser.yy index 6f0d7cc..0276bb7 100644 --- a/source/parser.yy +++ b/source/parser.yy @@ -80,8 +80,8 @@ %type string_literal; %type constant_definition; %type > constant_part constant_definitions; -%type variable_declaration; -%type > variable_declarations variable_part +%type variable_declaration; +%type > variable_declarations variable_part formal_parameter_list; %type type_expression; %type expression pointer summand factor comparand; @@ -105,35 +105,45 @@ program: type_part constant_part procedure_part variable_part compound_statement DOT { - std::vector definitions($1.size() + $2.size() + $3.size()); + std::vector definitions($1.size() + $3.size()); std::vector::iterator definition = definitions.begin(); + std::vector value_definitions($2.size() + $4.size()); + std::vector::iterator value_definition = value_definitions.begin(); - for (auto& type : $1) + for (auto type : $1) { *definition++ = type; } - for (auto& constant : $2) + for (auto constant : $2) { - *definition++ = constant; + *value_definition++ = constant; } - for (auto& procedure : $3) + for (auto procedure : $3) { *definition++ = procedure; } + for (auto variable : $4) + { + *value_definition++ = variable; + } driver.tree = std::make_unique(elna::source::position{}, - std::move(definitions), std::move($4), std::move($5)); + std::move(definitions), std::move(value_definitions), std::move($5)); } block: constant_part variable_part statement { - std::vector definitions($1.size()); + std::vector definitions($1.size() + $2.size()); std::vector::iterator definition = definitions.begin(); - for (auto& constant : $1) + for (auto constant : $1) { *definition++ = constant; } + for (auto variable : $2) + { + *definition++ = variable; + } $$ = new elna::source::block(elna::source::position{}, - std::move(definitions), std::move($2), std::move($3)); + {}, std::move(definitions), std::move($3)); }; procedure_definition: PROCEDURE IDENTIFIER formal_parameter_list SEMICOLON block SEMICOLON @@ -335,7 +345,7 @@ type_expression: } variable_declaration: IDENTIFIER COLON type_expression { - $$ = new elna::source::declaration(elna::source::make_position(@1), $1, $3); + $$ = new elna::source::variable_declaration(elna::source::make_position(@1), $1, $3); }; variable_declarations: variable_declaration COMMA variable_declarations