mario@181: /*
msousa@265: * matiec - a compiler for the programming languages defined in IEC 61131-3
msousa@265: *
msousa@265: * Copyright (C) 2009-2011 Mario de Sousa (msousa@fe.up.pt)
msousa@265: *
msousa@265: * This program is free software: you can redistribute it and/or modify
msousa@265: * it under the terms of the GNU General Public License as published by
msousa@265: * the Free Software Foundation, either version 3 of the License, or
msousa@265: * (at your option) any later version.
msousa@265: *
msousa@265: * This program is distributed in the hope that it will be useful,
msousa@265: * but WITHOUT ANY WARRANTY; without even the implied warranty of
msousa@265: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
msousa@265: * GNU General Public License for more details.
msousa@265: *
msousa@265: * You should have received a copy of the GNU General Public License
msousa@265: * along with this program. If not, see .
msousa@265: *
mario@181: *
mario@181: * This code is made available on the understanding that it will not be
mario@181: * used in safety-critical situations without a full and competent review.
mario@181: */
mario@181:
mario@181: /*
msousa@265: * An IEC 61131-3 compiler.
mario@181: *
mario@181: * Based on the
mario@181: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
mario@181: *
mario@181: */
mario@181:
mario@181:
mario@181: /*
mario@181: * This is the main stage 3a file.
mario@181: *
mario@181: * In stage 3a some helpful symbol tables are instanciated and populated.
mario@181: * These symbol tables wll then be used by stage3b and atage4 code generators.
mario@181: */
mario@181:
mario@181:
mario@181:
mario@181:
mario@181:
mario@181:
mario@181: // #include /* required for NULL */
mario@181: #include
mario@181: #include
mario@181: #include
mario@181: #include
mario@181: #include
mario@181: #include
mario@181:
mario@181: #include "../util/symtable.hh"
mario@181: #include "../util/dsymtable.hh"
mario@181: #include "../absyntax/visitor.hh"
mario@181:
mario@181:
mario@181:
mario@181: //#define DEBUG
mario@181: #ifdef DEBUG
mario@181: #define TRACE(classname) printf("\n____%s____\n",classname);
mario@181: #else
mario@181: #define TRACE(classname)
mario@181: #endif
mario@181:
mario@181: #define ERROR error_exit(__FILE__,__LINE__)
mario@181: /* function defined in main.cc */
mario@181: extern void error_exit(const char *file_name, int line_no);
mario@181:
mario@181:
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181:
mario@181:
mario@181: /* returns 0 if the names are equal!! */
mario@181: /* NOTE: it must ignore case!! */
mario@181: int compare_identifiers(symbol_c *ident1, symbol_c *ident2) {
mario@181:
mario@181: token_c *name1 = dynamic_cast(ident1);
mario@181: token_c *name2 = dynamic_cast(ident2);
mario@181:
mario@181: if ((name1 == NULL) || (name2 == NULL))
mario@181: /* invalid identifiers... */
mario@181: return -1;
mario@181:
mario@181: if (strcasecmp(name1->value, name2->value) == 0)
mario@181: return 0;
mario@181:
mario@181: /* identifiers do not match! */
mario@181: return 1;
mario@181: }
mario@181:
mario@181:
mario@181:
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181:
mario@181:
mario@181:
mario@181: /* A symbol table with all globally declared functions... */
mario@181: function_declaration_c null_symbol1(NULL,NULL,NULL,NULL);
mario@181: dsymtable_c function_symtable;
mario@181:
mario@181: /* A symbol table with all globally declared functions block types... */
mario@181: function_block_declaration_c null_symbol2(NULL,NULL,NULL);
mario@181: symtable_c function_block_type_symtable;
mario@181:
mario@181: /* A symbol table with all globally declared program types... */
mario@181: program_declaration_c null_symbol3(NULL,NULL,NULL);
mario@181: symtable_c program_type_symtable;
mario@181:
mario@181: /* A symbol table with all user declared type definitions... */
mario@181: /* Note that function block types and program types have their
mario@181: * own symbol tables, so do not get placed in this symbol table!
mario@181: */
mario@181: symbol_c null_symbol4;
mario@181: symtable_c type_symtable;
mario@181:
mario@181:
mario@181:
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181:
mario@181:
mario@181: class populate_symtables_c: public iterator_visitor_c {
mario@181:
mario@181: public:
mario@181: populate_symtables_c(void) {};
mario@181: virtual ~populate_symtables_c(void) {}
mario@181:
mario@181:
mario@181: public:
mario@181:
mario@181: /*************************/
mario@181: /* B.1 - Common elements */
mario@181: /*************************/
mario@181: /*******************************************/
mario@181: /* B 1.1 - Letters, digits and identifiers */
mario@181: /*******************************************/
mario@181: /*********************/
mario@181: /* B 1.2 - Constants */
mario@181: /*********************/
mario@181: /******************************/
mario@181: /* B 1.2.1 - Numeric Literals */
mario@181: /******************************/
mario@181: /*******************************/
mario@181: /* B.1.2.2 Character Strings */
mario@181: /*******************************/
mario@181: /***************************/
mario@181: /* B 1.2.3 - Time Literals */
mario@181: /***************************/
mario@181: /************************/
mario@181: /* B 1.2.3.1 - Duration */
mario@181: /************************/
mario@181: /************************************/
mario@181: /* B 1.2.3.2 - Time of day and Date */
mario@181: /************************************/
mario@181: /**********************/
mario@181: /* B.1.3 - Data types */
mario@181: /**********************/
mario@181: /***********************************/
mario@181: /* B 1.3.1 - Elementary Data Types */
mario@181: /***********************************/
mario@181: /********************************/
mario@181: /* B.1.3.2 - Generic data types */
mario@181: /********************************/
mario@181: /********************************/
mario@181: /* B 1.3.3 - Derived data types */
mario@181: /********************************/
mario@181:
mario@181: /* subrange_type_name ':' subrange_spec_init */
mario@181: void *visit(subrange_type_declaration_c *symbol) {
mario@181: TRACE("subrange_type_declaration_c");
mario@181: type_symtable.insert(symbol->subrange_type_name, symbol->subrange_spec_init);
mario@181: return NULL;
mario@181: }
mario@181:
mario@181:
mario@181: /* enumerated_type_name ':' enumerated_spec_init */
mario@181: void *visit(enumerated_type_declaration_c *symbol) {
mario@181: TRACE("enumerated_type_declaration_c");
mario@181: type_symtable.insert(symbol->enumerated_type_name, symbol->enumerated_spec_init);
mario@181: return NULL;
mario@181: }
mario@181:
mario@181:
mario@181: /* identifier ':' array_spec_init */
mario@181: void *visit(array_type_declaration_c *symbol) {
mario@181: TRACE("array_type_declaration_c");
mario@181: type_symtable.insert(symbol->identifier, symbol->array_spec_init);
mario@181: return NULL;
mario@181: }
mario@181:
mario@181:
mario@181: /* simple_type_name ':' simple_spec_init */
mario@181: void *visit(simple_type_declaration_c *symbol) {
mario@181: TRACE("simple_type_declaration_c");
mario@181: type_symtable.insert(symbol->simple_type_name, symbol->simple_spec_init);
mario@181: return NULL;
mario@181: }
mario@181:
mario@181:
mario@181: /* structure_type_name ':' structure_specification */
mario@181: void *visit(structure_type_declaration_c *symbol) {
mario@181: TRACE("structure_type_declaration_c");
mario@181: type_symtable.insert(symbol->structure_type_name, symbol->structure_specification);
mario@181: return NULL;
mario@181: }
mario@181:
mario@181:
mario@181:
mario@181: /*********************/
mario@181: /* B 1.4 - Variables */
mario@181: /*********************/
mario@181: /********************************************/
mario@181: /* B.1.4.1 Directly Represented Variables */
mario@181: /********************************************/
mario@181: /*************************************/
mario@181: /* B.1.4.2 Multi-element Variables */
mario@181: /*************************************/
mario@181: /******************************************/
mario@181: /* B 1.4.3 - Declaration & Initialisation */
mario@181: /******************************************/
mario@181: /**************************************/
mario@181: /* B.1.5 - Program organization units */
mario@181: /**************************************/
mario@181: /***********************/
mario@181: /* B 1.5.1 - Functions */
mario@181: /***********************/
mario@181: public:
mario@181: /* FUNCTION derived_function_name ':' elementary_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */
mario@181: /* | FUNCTION derived_function_name ':' derived_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */
mario@181: void *visit(function_declaration_c *symbol) {
mario@181: TRACE("function_declaration_c");
mario@181: function_symtable.insert(symbol->derived_function_name, symbol);
mario@181:
mario@181: /* symbol->derived_function_name->accept(*this); */ /* Function name */
mario@181: /* symbol->type_name->accept(*this); */ /* return data type */
mario@181: /* symbol->var_declarations_list->accept(*this); */ /* Function parameters and variables */
mario@181: /* symbol->function_body->accept(*this); */ /* Function body */
mario@181: return NULL;
mario@181: }
mario@181:
mario@181:
mario@181: /*****************************/
mario@181: /* B 1.5.2 - Function Blocks */
mario@181: /*****************************/
mario@181: public:
mario@181: /* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
mario@181: //SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused)
mario@181: void *visit(function_block_declaration_c *symbol) {
mario@181: TRACE("function_block_declaration_c");
mario@181: function_block_type_symtable.insert(symbol->fblock_name, symbol);
mario@181: /*
mario@181: symbol->fblock_name->accept(*this);
mario@181: symbol->var_declarations->accept(*this);
mario@181: symbol->fblock_body->accept(*this);
mario@181: */
mario@181: return NULL;
mario@181: }
mario@181:
mario@181:
mario@181: /**********************/
mario@181: /* B 1.5.3 - Programs */
mario@181: /**********************/
mario@181: public:
mario@181: /* PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */
mario@181: //SYM_REF4(program_declaration_c, program_type_name, var_declarations, function_block_body, unused)
mario@181: void *visit(program_declaration_c *symbol) {
mario@181: TRACE("program_declaration_c");
mario@181: program_type_symtable.insert(symbol->program_type_name, symbol);
mario@181: /*
mario@181: symbol->program_type_name->accept(*this);
mario@181: symbol->var_declarations->accept(*this);
mario@181: symbol->function_block_body->accept(*this);
mario@181: */
mario@181: return NULL;
mario@181: }
mario@181:
mario@181: }; /* populate_symtables_c */
mario@181:
mario@181:
mario@181:
mario@181:
mario@181:
mario@181: void absyntax_utils_init(symbol_c *tree_root) {
mario@181: populate_symtables_c populate_symbols;
mario@181:
mario@181: tree_root->accept(populate_symbols);
mario@181: }
mario@181: