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) Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant 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 msousa@366: #include /* required for strlen() */ msousa@350: #include /* required for atoi() */ 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: msousa@350: /* extract the value of an integer from an integer_c object !! */ msousa@350: /* NOTE: it must ignore underscores! */ msousa@350: int extract_integer(symbol_c *sym) { msousa@366: std::string str = ""; msousa@366: integer_c *integer; msousa@366: neg_integer_c * neg_integer; msousa@366: msousa@366: if ((neg_integer = dynamic_cast(sym)) != NULL) msousa@366: return - extract_integer((integer_c *)neg_integer->exp); msousa@366: msousa@366: if ((integer = dynamic_cast(sym)) == NULL) ERROR; msousa@366: msousa@366: for(unsigned int i = 0; i < strlen(integer->value); i++) msousa@366: if (integer->value[i] != '_') str += integer->value[i]; msousa@366: msousa@366: return atoi(str.c_str()); msousa@350: } msousa@350: mario@181: msousa@366: 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! msousa@338: * msousa@338: * The symbol_c * associated to the value will point to the data type declaration. mario@181: */ mario@181: symbol_c null_symbol4; mario@181: symtable_c type_symtable; mario@181: laurent@328: /* A symbol table with all values declared for enumerated type... */ msousa@338: /* Notes: msousa@338: * - if the value is defined multiple times the value laurent@328: * is the null pointer. msousa@338: * msousa@338: * - The stored symbol_c * associated to the value points to the enumerated_type_name msousa@338: * (i.e. the name of the enumerated data type) in which the the value/identifier msousa@338: * is used/declared. msousa@338: * msousa@338: * - We could re-use the null_symbol4 object, but it is safer to use a distinct object msousa@338: * (i.e. it might make it easier to find strange bugs). msousa@338: */ msousa@338: symbol_c null_symbol5; msousa@338: symtable_c enumerated_value_symtable; 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: laurent@328: private: laurent@328: symbol_c *current_enumerated_type; laurent@328: laurent@328: public: laurent@328: populate_symtables_c(void) { laurent@328: current_enumerated_type = NULL; laurent@328: }; 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); laurent@328: current_enumerated_type = symbol->enumerated_type_name; laurent@328: symbol->enumerated_spec_init->accept(*this); laurent@328: current_enumerated_type = NULL; laurent@328: return NULL; laurent@328: } laurent@328: laurent@328: /* enumerated_specification ASSIGN enumerated_value */ laurent@328: void *visit(enumerated_spec_init_c *symbol) { laurent@328: return symbol->enumerated_specification->accept(*this); laurent@328: } laurent@328: laurent@328: /* [enumerated_type_name '#'] identifier */ laurent@328: void *visit(enumerated_value_c *symbol) { laurent@328: if (current_enumerated_type != NULL) { laurent@328: if (symbol->type != NULL) ERROR; laurent@328: laurent@328: symbol_c *value_type = enumerated_value_symtable.find_value(symbol->value); msousa@338: /* NOTE: The following condition checks whether the same identifier is used more than once msousa@338: * when defining the enumerated values of the type declaration of the new enumerated type. msousa@338: * If this occurs, then the program beeing compiled contains a semantic error, which msousa@338: * must be caught and reported by the semantic analyser. However, since msousa@338: * this code is run before the semantic analyser, we must not yet raise the ERROR (internal msousa@338: * compiler error message). msousa@338: * For this reason, the follosing check is commented out. msousa@338: */ msousa@338: /* if (value_type == current_enumerated_type) ERROR; */ laurent@328: laurent@328: if (value_type == enumerated_value_symtable.end_value()) msousa@338: /* This identifier has not yet been used in any previous declaration of an enumeration data type. msousa@338: * so we add it to the symbol table. msousa@338: */ laurent@328: enumerated_value_symtable.insert(symbol->value, current_enumerated_type); laurent@328: else if (value_type != NULL) msousa@338: /* This identifier has already been used in a previous declaration of an enumeration data type. msousa@338: * so we set the symbol in symbol table pointing to NULL. msousa@338: */ laurent@328: enumerated_value_symtable.set(symbol->value, NULL); laurent@328: } laurent@328: return NULL; laurent@328: } 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: