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