lbessard@70: /* lbessard@70: * (c) 2003 Mario de Sousa lbessard@70: * lbessard@70: * Offered to the public under the terms of the GNU General Public License lbessard@70: * as published by the Free Software Foundation; either version 2 of the lbessard@70: * License, or (at your option) any later version. lbessard@70: * lbessard@70: * This program is distributed in the hope that it will be useful, but lbessard@70: * WITHOUT ANY WARRANTY; without even the implied warranty of lbessard@70: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General lbessard@70: * Public License for more details. lbessard@70: * lbessard@70: * This code is made available on the understanding that it will not be lbessard@70: * used in safety-critical situations without a full and competent review. lbessard@70: */ lbessard@70: lbessard@70: /* lbessard@70: * An IEC 61131-3 IL and ST compiler. lbessard@70: * lbessard@70: * Based on the lbessard@70: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) lbessard@70: * lbessard@70: */ lbessard@70: lbessard@70: lbessard@70: /* lbessard@70: * This is one of the versions available for the 4th stage. lbessard@70: * lbessard@70: * This 4th stage generates a c++ source program equivalent lbessard@70: * to the IL and ST code. lbessard@70: */ lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: // #include /* required for NULL */ lbessard@70: #include lbessard@70: #include lbessard@70: #include lbessard@70: #include lbessard@70: lbessard@70: #include "../../util/symtable.hh" lbessard@70: #include "../../util/dsymtable.hh" lbessard@70: #include "../../absyntax/visitor.hh" lbessard@70: lbessard@70: #include "../stage4.hh" lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: //#define DEBUG lbessard@70: #ifdef DEBUG lbessard@70: #define TRACE(classname) printf("\n____%s____\n",classname); lbessard@70: #else lbessard@70: #define TRACE(classname) lbessard@70: #endif lbessard@70: lbessard@70: lbessard@70: lbessard@70: #define ERROR error_exit(__FILE__,__LINE__) lbessard@70: /* function defined in main.cc */ lbessard@70: extern void error_exit(const char *file_name, int line_no); lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: /* A symbol table with all globally declared functions... */ lbessard@70: function_declaration_c null_symbol1(NULL,NULL,NULL,NULL); lbessard@70: dsymtable_c function_symtable; lbessard@70: lbessard@70: /* A symbol table with all globally declared functions block types... */ lbessard@70: function_block_declaration_c null_symbol2(NULL,NULL,NULL); lbessard@70: symtable_c function_block_type_symtable; lbessard@70: lbessard@70: /* A symbol table with all globally declared program types... */ lbessard@70: program_declaration_c null_symbol3(NULL,NULL,NULL); lbessard@70: symtable_c program_type_symtable; lbessard@70: lbessard@70: /* A symbol table with all user declared type definitions... */ lbessard@70: /* Note that function block types and program types have their lbessard@70: * own symbol tables, so do not get placed in this symbol table! lbessard@70: */ lbessard@70: symbol_c null_symbol4; lbessard@70: symtable_c type_symtable; lbessard@70: lbessard@70: lbessard@70: lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: lbessard@70: lbessard@70: /* returns 0 if the names are equal!! */ lbessard@70: /* NOTE: it must ignore case!! */ lbessard@70: static int compare_identifiers(symbol_c *ident1, symbol_c *ident2) { lbessard@70: lbessard@70: token_c *name1 = dynamic_cast(ident1); lbessard@70: token_c *name2 = dynamic_cast(ident2); lbessard@70: lbessard@70: if ((name1 == NULL) || (name2 == NULL)) lbessard@70: /* invalid identifiers... */ lbessard@70: return -1; lbessard@70: lbessard@70: if (strcasecmp(name1->value, name2->value) == 0) lbessard@70: return 0; lbessard@70: lbessard@70: /* identifiers do not match! */ lbessard@70: return 1; lbessard@70: } lbessard@70: lbessard@70: lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: lbessard@70: /* Unlike Programs and Configurations which get mapped onto C++ classes, lbessard@70: * Function Blocks are mapped onto a C structure containing the variables, and lbessard@70: * a C function containing the code in the FB's body. This is to allow direct allocation lbessard@70: * of a FB variable (which is really an instance of the C data structure) to lbessard@70: * a member of a union variable (note that classes with constructors cannot lbessard@70: * be mebers of a union), which is done in IL when loading a FB onto IL's lbessard@70: * default variable. lbessard@70: * lbessard@70: * So as not to clash the names of the C data structure and the C function, lbessard@70: * the C structure is given a name identical to that of the FB name, whereas lbessard@70: * the name of the function is the FB name with a constant string appended. lbessard@70: * The value of that constant string which is appended is defined in the following lbessard@70: * constant. lbessard@70: * In order not to clash with any variable in the IL and ST source codem the lbessard@70: * following constant should contain a double underscore, which is not allowed lbessard@70: * in IL and ST. lbessard@70: * lbessard@70: * e.g.: FUNTION_BLOCK TEST lbessard@70: * is mapped onto a TEST data structure, and a TEST_body__ function. lbessard@70: */ lbessard@70: lbessard@70: #define FB_FUNCTION_SUFFIX "_body__" lbessard@70: lbessard@70: /* Idem as body, but for initializer FB function */ lbessard@70: #define FB_INIT_SUFFIX "_init__" lbessard@70: lbessard@70: /* Idem as body, but for run CONFIG and RESOURCE function */ lbessard@70: #define FB_RUN_SUFFIX "_run__" lbessard@70: lbessard@70: /* The FB body function is passed as the only parameter a pointer to the FB data lbessard@70: * structure instance. The name of this parameter is given by the following constant. lbessard@70: * In order not to clash with any variable in the IL and ST source codem the lbessard@70: * following constant should contain a double underscore, which is not allowed lbessard@70: * in IL and ST. lbessard@70: * lbessard@70: * e.g.: the body of FUNTION_BLOCK TEST lbessard@70: * is mapped onto the C function lbessard@70: * TEST_body__(TEST *data__) lbessard@70: */ lbessard@70: lbessard@70: #define FB_FUNCTION_PARAM "data__" lbessard@70: lbessard@70: lbessard@70: #define SFC_STEP_ACTION_PREFIX "__SFC_" lbessard@70: lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: lbessard@70: lbessard@70: #include "spec_init_separator.cc" lbessard@70: #include "function_param_iterator.cc" lbessard@70: #include "function_call_iterator.cc" lbessard@70: #include "function_call_param_iterator.cc" lbessard@70: #include "type_initial_value.cc" lbessard@70: #include "search_fb_instance_decl.cc" lbessard@70: #include "search_base_type.cc" lbessard@70: #include "search_var_instance_decl.cc" lbessard@70: #include "decompose_var_instance_name.cc" lbessard@70: #include "search_varfb_instance_type.cc" lbessard@70: #include "search_constant_type.cc" lbessard@70: #include "search_expression_type.cc" lbessard@70: lbessard@70: #include "generate_c_base.cc" lbessard@70: #include "generate_c_typedecl.cc" lbessard@70: #include "generate_c_sfcdecl.cc" lbessard@70: #include "generate_c_vardecl.cc" lbessard@70: #include "generate_c_configbody.cc" lbessard@70: #include "generate_location_list.cc" lbessard@70: lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: lbessard@70: /* Generate a name for a temporary variable. lbessard@70: * Each new name generated is appended a different number, lbessard@70: * starting off from 0. lbessard@70: * After calling reset(), the names will start off again from 0. lbessard@70: */ lbessard@70: #define VAR_LEADER "__" lbessard@70: #define TEMP_VAR VAR_LEADER "TMP_" lbessard@70: #define SOURCE_VAR VAR_LEADER "SRC_" lbessard@70: lbessard@70: #include "generate_c_tempvardecl.cc" lbessard@70: lbessard@70: #include "generate_c_st.cc" lbessard@70: #include "generate_c_il.cc" lbessard@70: lbessard@70: #include "generate_c.hh" lbessard@70: lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: lbessard@70: #define MILLISECOND 1000000 lbessard@70: #define SECOND 1000 * MILLISECOND lbessard@70: lbessard@70: /* A helper class that knows how to generate code for both the IL and ST languages... */ lbessard@70: class calculate_time_c: public iterator_visitor_c { lbessard@70: private: lbessard@70: unsigned long time; lbessard@70: float current_value; lbessard@70: lbessard@70: public: lbessard@70: calculate_time_c(void){time = 0;}; lbessard@70: lbessard@70: unsigned long get_time(void) {return time;}; lbessard@70: lbessard@70: void *get_integer_value(token_c *token) { lbessard@70: std::string str = ""; lbessard@70: for (unsigned int i = 0; i < strlen(token->value); i++) lbessard@70: if (token->value[i] != '_') lbessard@70: str += token->value[i]; lbessard@70: current_value = atof(str.c_str()); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *get_float_value(token_c *token) { lbessard@70: current_value = atof(token->value); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /******************************/ lbessard@70: /* B 1.2.1 - Numeric Literals */ lbessard@70: /******************************/ lbessard@70: lbessard@70: void *visit(integer_c *symbol) {return get_integer_value(symbol);} lbessard@70: lbessard@70: /************************/ lbessard@70: /* B 1.2.3.1 - Duration */ lbessard@70: /************************/ lbessard@70: lbessard@70: /* SYM_REF2(duration_c, neg, interval) */ lbessard@70: void *visit(duration_c *symbol) { lbessard@70: if (symbol->neg != NULL) lbessard@70: ERROR; lbessard@70: symbol->interval->accept(*this); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* SYM_TOKEN(fixed_point_c) */ lbessard@70: void *visit(fixed_point_c *symbol) {return get_float_value(symbol);} lbessard@70: lbessard@70: /* SYM_REF2(days_c, days, hours) */ lbessard@70: void *visit(days_c *symbol) { lbessard@70: if (symbol->hours) lbessard@70: symbol->hours->accept(*this); lbessard@70: symbol->days->accept(*this); lbessard@70: time += (unsigned long)(current_value * 24 * 3600 * SECOND); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* SYM_REF2(hours_c, hours, minutes) */ lbessard@70: void *visit(hours_c *symbol) { lbessard@70: if (symbol->minutes) lbessard@70: symbol->minutes->accept(*this); lbessard@70: symbol->hours->accept(*this); lbessard@70: time += (unsigned long)(current_value * 3600 * SECOND); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* SYM_REF2(minutes_c, minutes, seconds) */ lbessard@70: void *visit(minutes_c *symbol) { lbessard@70: if (symbol->seconds) lbessard@70: symbol->seconds->accept(*this); lbessard@70: symbol->minutes->accept(*this); lbessard@70: time += (unsigned long)(current_value * 60 * SECOND); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* SYM_REF2(seconds_c, seconds, milliseconds) */ lbessard@70: void *visit(seconds_c *symbol) { lbessard@70: if (symbol->milliseconds) lbessard@70: symbol->milliseconds->accept(*this); lbessard@70: symbol->seconds->accept(*this); lbessard@70: time += (unsigned long)(current_value * SECOND); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* SYM_REF2(milliseconds_c, milliseconds, unused) */ lbessard@70: void *visit(milliseconds_c *symbol) { lbessard@70: symbol->milliseconds->accept(*this); lbessard@70: time += (unsigned long)(current_value * MILLISECOND); lbessard@70: return NULL; lbessard@70: } lbessard@70: }; lbessard@70: lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: lbessard@70: class calculate_common_ticktime_c: public iterator_visitor_c { lbessard@70: private: lbessard@70: unsigned long common_ticktime; lbessard@70: lbessard@70: public: lbessard@70: calculate_common_ticktime_c(void){common_ticktime = 0;} lbessard@70: lbessard@70: unsigned long euclide(unsigned long a, unsigned long b) { lbessard@70: unsigned long c = a % b; lbessard@70: if (c == 0) lbessard@70: return b; lbessard@70: else lbessard@70: return euclide(b, c); lbessard@70: } lbessard@70: lbessard@70: void update_ticktime(unsigned long time) { lbessard@70: if (common_ticktime == 0) lbessard@70: common_ticktime = time; lbessard@70: else if (time > common_ticktime) lbessard@70: common_ticktime = euclide(time, common_ticktime); lbessard@70: else lbessard@70: common_ticktime = euclide(common_ticktime, time); lbessard@70: } lbessard@70: lbessard@70: unsigned long get_ticktime(void) { lbessard@70: return common_ticktime; lbessard@70: } lbessard@70: lbessard@70: /* TASK task_name task_initialization */ lbessard@70: //SYM_REF2(task_configuration_c, task_name, task_initialization) lbessard@70: void *visit(task_initialization_c *symbol) { lbessard@70: calculate_time_c calculate_time; lbessard@70: unsigned long time = 0; lbessard@70: if (symbol->interval_data_source != NULL) { lbessard@70: symbol->interval_data_source->accept(calculate_time); lbessard@70: time = calculate_time.get_time(); lbessard@70: } lbessard@70: if (time > 0) lbessard@70: update_ticktime(time); lbessard@70: return NULL; lbessard@70: } lbessard@70: }; lbessard@70: lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: lbessard@70: /* A helper class that knows how to generate code for both the IL and ST languages... */ lbessard@70: class generate_c_SFC_IL_ST_c: public null_visitor_c { lbessard@70: private: lbessard@70: stage4out_c *s4o_ptr; lbessard@70: symbol_c *scope; lbessard@70: const char *variable_prefix; lbessard@70: lbessard@70: public: lbessard@70: generate_c_SFC_IL_ST_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL); lbessard@70: /*********************************************/ lbessard@70: /* B.1.6 Sequential function chart elements */ lbessard@70: /*********************************************/ lbessard@70: lbessard@70: /*| sequential_function_chart sfc_network*/ lbessard@70: void *visit(sequential_function_chart_c * symbol); lbessard@70: lbessard@70: /****************************************/ lbessard@70: /* B.2 - Language IL (Instruction List) */ lbessard@70: /****************************************/ lbessard@70: lbessard@70: /***********************************/ lbessard@70: /* B 2.1 Instructions and Operands */ lbessard@70: /***********************************/ lbessard@70: /*| instruction_list il_instruction */ lbessard@70: void *visit(instruction_list_c *symbol); lbessard@70: lbessard@70: /* Remainder implemented in generate_c_il_c... */ lbessard@70: lbessard@70: /***************************************/ lbessard@70: /* B.3 - Language ST (Structured Text) */ lbessard@70: /***************************************/ lbessard@70: /***********************/ lbessard@70: /* B 3.1 - Expressions */ lbessard@70: /***********************/ lbessard@70: /* Implemented in generate_c_st_c */ lbessard@70: lbessard@70: /********************/ lbessard@70: /* B 3.2 Statements */ lbessard@70: /********************/ lbessard@70: void *visit(statement_list_c *symbol); lbessard@70: lbessard@70: /* Remainder implemented in generate_c_st_c... */ lbessard@70: }; lbessard@70: lbessard@70: #include "generate_c_sfc.cc" lbessard@70: lbessard@70: generate_c_SFC_IL_ST_c::generate_c_SFC_IL_ST_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix) { lbessard@70: if (NULL == scope) ERROR; lbessard@70: this->s4o_ptr = s4o_ptr; lbessard@70: this->scope = scope; lbessard@70: this->variable_prefix = variable_prefix; lbessard@70: } lbessard@70: lbessard@70: void *generate_c_SFC_IL_ST_c::visit(sequential_function_chart_c * symbol) { lbessard@70: generate_c_sfc_c generate_c_sfc(s4o_ptr, scope, variable_prefix); lbessard@70: generate_c_sfc.generate(symbol); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *generate_c_SFC_IL_ST_c::visit(instruction_list_c *symbol) { lbessard@70: generate_c_il_c generate_c_il(s4o_ptr, scope, variable_prefix); lbessard@70: generate_c_il.generate(symbol); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *generate_c_SFC_IL_ST_c::visit(statement_list_c *symbol) { lbessard@70: generate_c_st_c generate_c_st(s4o_ptr, scope, variable_prefix); lbessard@70: generate_c_st.generate(symbol); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: lbessard@70: lbessard@70: class generate_c_pous_c: public generate_c_typedecl_c { lbessard@70: lbessard@70: public: lbessard@70: generate_c_pous_c(stage4out_c *s4o_ptr) lbessard@70: : generate_c_typedecl_c(s4o_ptr) {}; lbessard@70: virtual ~generate_c_pous_c(void) {} lbessard@70: lbessard@70: lbessard@70: public: lbessard@70: lbessard@70: /*************************/ lbessard@70: /* B.1 - Common elements */ lbessard@70: /*************************/ lbessard@70: /*******************************************/ lbessard@70: /* B 1.1 - Letters, digits and identifiers */ lbessard@70: /*******************************************/ lbessard@70: /* done in base class(es) */ lbessard@70: lbessard@70: /*********************/ lbessard@70: /* B 1.2 - Constants */ lbessard@70: /*********************/ lbessard@70: /* originally empty... */ lbessard@70: lbessard@70: /******************************/ lbessard@70: /* B 1.2.1 - Numeric Literals */ lbessard@70: /******************************/ lbessard@70: /* done in base class(es) */ lbessard@70: lbessard@70: /*******************************/ lbessard@70: /* B.1.2.2 Character Strings */ lbessard@70: /*******************************/ lbessard@70: /* done in base class(es) */ lbessard@70: lbessard@70: /***************************/ lbessard@70: /* B 1.2.3 - Time Literals */ lbessard@70: /***************************/ lbessard@70: /************************/ lbessard@70: /* B 1.2.3.1 - Duration */ lbessard@70: /************************/ lbessard@70: /* done in base class(es) */ lbessard@70: lbessard@70: /************************************/ lbessard@70: /* B 1.2.3.2 - Time of day and Date */ lbessard@70: /************************************/ lbessard@70: /* done in base class(es) */ lbessard@70: lbessard@70: /**********************/ lbessard@70: /* B.1.3 - Data types */ lbessard@70: /**********************/ lbessard@70: /***********************************/ lbessard@70: /* B 1.3.1 - Elementary Data Types */ lbessard@70: /***********************************/ lbessard@70: /* done in base class(es) */ lbessard@70: lbessard@70: /********************************/ lbessard@70: /* B.1.3.2 - Generic data types */ lbessard@70: /********************************/ lbessard@70: /* originally empty... */ lbessard@70: lbessard@70: /********************************/ lbessard@70: /* B 1.3.3 - Derived data types */ lbessard@70: /********************************/ lbessard@70: /* done in base class(es) */ lbessard@70: lbessard@70: /*********************/ lbessard@70: /* B 1.4 - Variables */ lbessard@70: /*********************/ lbessard@70: /* done in base class(es) */ lbessard@70: lbessard@70: /********************************************/ lbessard@70: /* B.1.4.1 Directly Represented Variables */ lbessard@70: /********************************************/ lbessard@70: /* done in base class(es) */ lbessard@70: lbessard@70: /*************************************/ lbessard@70: /* B.1.4.2 Multi-element Variables */ lbessard@70: /*************************************/ lbessard@70: /* done in base class(es) */ lbessard@70: lbessard@70: /******************************************/ lbessard@70: /* B 1.4.3 - Declaration & Initialisation */ lbessard@70: /******************************************/ lbessard@70: /* done in base class(es) */ lbessard@70: lbessard@70: /**************************************/ lbessard@70: /* B.1.5 - Program organization units */ lbessard@70: /**************************************/ lbessard@70: /***********************/ lbessard@70: /* B 1.5.1 - Functions */ lbessard@70: /***********************/ lbessard@70: lbessard@70: public: lbessard@70: /* FUNCTION derived_function_name ':' elementary_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */ lbessard@70: /* | FUNCTION derived_function_name ':' derived_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */ lbessard@70: void *visit(function_declaration_c *symbol) { lbessard@70: generate_c_vardecl_c *vardecl; lbessard@70: TRACE("function_declaration_c"); lbessard@70: lbessard@70: /* start off by adding this declaration to the global lbessard@70: * function declaration symbol table... lbessard@70: */ lbessard@70: function_symtable.insert(symbol->derived_function_name, symbol); lbessard@70: lbessard@70: /* (A) Function declaration... */ lbessard@70: /* (A.1) Function return type */ lbessard@70: s4o.print("// FUNCTION\n"); lbessard@70: symbol->type_name->accept(*this); /* return type */ lbessard@70: s4o.print(" "); lbessard@70: /* (A.2) Function name */ lbessard@70: symbol->derived_function_name->accept(*this); lbessard@70: s4o.print("("); lbessard@70: lbessard@70: /* (A.3) Function parameters */ lbessard@70: s4o.indent_right(); lbessard@70: vardecl = new generate_c_vardecl_c(&s4o, lbessard@70: generate_c_vardecl_c::finterface_vf, lbessard@70: generate_c_vardecl_c::input_vt | lbessard@70: generate_c_vardecl_c::output_vt | lbessard@70: generate_c_vardecl_c::inoutput_vt); lbessard@70: vardecl->print(symbol->var_declarations_list); lbessard@70: delete vardecl; lbessard@70: s4o.indent_left(); lbessard@70: lbessard@70: s4o.print(")\n" + s4o.indent_spaces + "{\n"); lbessard@70: lbessard@70: /* (B) Function local variable declaration */ lbessard@70: /* (B.1) Variables declared in ST source code */ lbessard@70: s4o.indent_right(); lbessard@70: vardecl = new generate_c_vardecl_c(&s4o, generate_c_vardecl_c::localinit_vf, generate_c_vardecl_c::private_vt); lbessard@70: vardecl->print(symbol->var_declarations_list); lbessard@70: delete vardecl; lbessard@70: lbessard@70: /* (B.2) Temporary variable for function's return value */ lbessard@70: /* It will have the same name as the function itself! */ lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: symbol->type_name->accept(*this); /* return type */ lbessard@70: s4o.print(" "); lbessard@70: symbol->derived_function_name->accept(*this); lbessard@70: s4o.print(" = "); lbessard@70: { lbessard@70: /* get the default value of this variable's type */ lbessard@70: symbol_c *default_value = (symbol_c *)symbol->type_name->accept(*type_initial_value_c::instance()); lbessard@70: if (default_value == NULL) ERROR; lbessard@70: default_value->accept(*this); lbessard@70: } lbessard@70: s4o.print(";\n\n"); lbessard@70: lbessard@70: /* (C) Function body */ lbessard@70: generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol); lbessard@70: symbol->function_body->accept(generate_c_code); lbessard@70: s4o.print(s4o.indent_spaces + "return "); lbessard@70: symbol->derived_function_name->accept(*this); lbessard@70: s4o.print(";\n"); lbessard@70: s4o.indent_left(); lbessard@70: s4o.print(s4o.indent_spaces + "}\n\n\n"); lbessard@70: lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: lbessard@70: /* The remaining var_declarations_list_c, function_var_decls_c lbessard@70: * and var2_init_decl_list_c are handled in the generate_c_vardecl_c class lbessard@70: */ lbessard@70: lbessard@70: lbessard@70: /*****************************/ lbessard@70: /* B 1.5.2 - Function Blocks */ lbessard@70: /*****************************/ lbessard@70: public: lbessard@70: /* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ lbessard@70: //SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused) lbessard@70: void *visit(function_block_declaration_c *symbol) { lbessard@70: generate_c_vardecl_c *vardecl; lbessard@70: generate_c_sfcdecl_c *sfcdecl; lbessard@70: TRACE("function_block_declaration_c"); lbessard@70: lbessard@70: /* start off by adding this declaration to the global lbessard@70: * function block declaration symbol table... lbessard@70: */ lbessard@70: function_block_type_symtable.insert(symbol->fblock_name, symbol); lbessard@70: lbessard@70: /* (A) Function Block data structure declaration... */ lbessard@70: /* (A.1) Data structure declaration */ lbessard@70: s4o.print("// FUNCTION_BLOCK "); lbessard@70: symbol->fblock_name->accept(*this); lbessard@70: s4o.print("\n// Data part\n"); lbessard@70: s4o.print("typedef struct {\n"); lbessard@70: s4o.indent_right(); lbessard@70: /* (A.2) Public variables: i.e. the function parameters... */ lbessard@70: s4o.print(s4o.indent_spaces + "// FB Interface - IN, OUT, IN_OUT variables\n"); lbessard@70: vardecl = new generate_c_vardecl_c(&s4o, lbessard@70: generate_c_vardecl_c::local_vf, lbessard@70: generate_c_vardecl_c::input_vt | lbessard@70: generate_c_vardecl_c::output_vt | lbessard@70: generate_c_vardecl_c::inoutput_vt); lbessard@70: vardecl->print(symbol->var_declarations); lbessard@70: delete vardecl; lbessard@70: s4o.print("\n"); lbessard@70: /* (A.3) Private internal variables */ lbessard@70: s4o.print(s4o.indent_spaces + "// FB private variables - TEMP, private and located variables\n"); lbessard@70: vardecl = new generate_c_vardecl_c(&s4o, lbessard@70: generate_c_vardecl_c::local_vf, lbessard@70: generate_c_vardecl_c::temp_vt | lbessard@70: generate_c_vardecl_c::private_vt | lbessard@70: generate_c_vardecl_c::located_vt | lbessard@70: generate_c_vardecl_c::external_vt); lbessard@70: vardecl->print(symbol->var_declarations); lbessard@70: delete vardecl; lbessard@70: /* (A.4) Generate private internal variables for SFC */ lbessard@70: sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::sfcdecl_sd); lbessard@70: sfcdecl->print(symbol->fblock_body); lbessard@70: delete sfcdecl; lbessard@70: s4o.print("\n"); lbessard@70: lbessard@70: /* (A.5) Function Block data structure type name. */ lbessard@70: s4o.indent_left(); lbessard@70: s4o.print("} "); lbessard@70: symbol->fblock_name->accept(*this); lbessard@70: s4o.print(";\n\n"); lbessard@70: lbessard@70: lbessard@70: /* (B) Constructor */ lbessard@70: /* (B.1) Constructor name... */ lbessard@70: s4o.print(s4o.indent_spaces + "void "); lbessard@70: symbol->fblock_name->accept(*this); lbessard@70: s4o.print(FB_INIT_SUFFIX); lbessard@70: s4o.print("("); lbessard@70: lbessard@70: /* first and only parameter is a pointer to the data */ lbessard@70: symbol->fblock_name->accept(*this); lbessard@70: s4o.print(" *"); lbessard@70: s4o.print(FB_FUNCTION_PARAM); lbessard@70: s4o.print(") {\n"); lbessard@70: s4o.indent_right(); lbessard@70: lbessard@70: /* (B.2) Member initializations... */ lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: vardecl = new generate_c_vardecl_c(&s4o, lbessard@70: generate_c_vardecl_c::constructorinit_vf, lbessard@70: generate_c_vardecl_c::input_vt | lbessard@70: generate_c_vardecl_c::output_vt | lbessard@70: generate_c_vardecl_c::inoutput_vt | lbessard@70: generate_c_vardecl_c::private_vt | lbessard@70: generate_c_vardecl_c::located_vt | lbessard@70: generate_c_vardecl_c::external_vt); lbessard@70: vardecl->print(symbol->var_declarations, NULL, FB_FUNCTION_PARAM"->"); lbessard@70: delete vardecl; lbessard@70: s4o.print("\n"); lbessard@70: /* (B.3) Generate private internal variables for SFC */ lbessard@70: sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::sfcinit_sd); lbessard@70: sfcdecl->print(symbol->fblock_body, FB_FUNCTION_PARAM"->"); lbessard@70: delete sfcdecl; lbessard@70: s4o.indent_left(); lbessard@70: s4o.print(s4o.indent_spaces + "}\n\n"); lbessard@70: lbessard@70: lbessard@70: /* (C) Function with FB body */ lbessard@70: /* (C.1) Step definitions */ lbessard@70: sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::stepdef_sd); lbessard@70: sfcdecl->print(symbol->fblock_body); lbessard@70: delete sfcdecl; lbessard@70: lbessard@70: /* (C.2) Action definitions */ lbessard@70: sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::actiondef_sd); lbessard@70: sfcdecl->print(symbol->fblock_body); lbessard@70: delete sfcdecl; lbessard@70: lbessard@70: /* (C.3) Function declaration */ lbessard@70: s4o.print("// Code part\n"); lbessard@70: /* function interface */ lbessard@70: s4o.print("void "); lbessard@70: symbol->fblock_name->accept(*this); lbessard@70: s4o.print(FB_FUNCTION_SUFFIX); lbessard@70: s4o.print("("); lbessard@70: /* first and only parameter is a pointer to the data */ lbessard@70: symbol->fblock_name->accept(*this); lbessard@70: s4o.print(" *"); lbessard@70: s4o.print(FB_FUNCTION_PARAM); lbessard@70: s4o.print(") {\n"); lbessard@70: s4o.indent_right(); lbessard@70: lbessard@70: /* (C.4) Initialize TEMP variables */ lbessard@70: /* function body */ lbessard@70: s4o.print(s4o.indent_spaces + "// Initialise TEMP variables\n"); lbessard@70: vardecl = new generate_c_vardecl_c(&s4o, lbessard@70: generate_c_vardecl_c::init_vf, lbessard@70: generate_c_vardecl_c::temp_vt); lbessard@70: vardecl->print(symbol->var_declarations, NULL, FB_FUNCTION_PARAM"->"); lbessard@70: delete vardecl; lbessard@70: s4o.print("\n"); lbessard@70: lbessard@70: /* (C.5) Function code */ lbessard@70: generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol, FB_FUNCTION_PARAM"->"); lbessard@70: symbol->fblock_body->accept(generate_c_code); lbessard@70: s4o.indent_left(); lbessard@70: s4o.print(s4o.indent_spaces + "} // "); lbessard@70: symbol->fblock_name->accept(*this); lbessard@70: s4o.print(FB_FUNCTION_SUFFIX); lbessard@70: s4o.print(s4o.indent_spaces + "() \n\n"); lbessard@70: lbessard@70: /* (C.6) Step undefinitions */ lbessard@70: sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::stepundef_sd); lbessard@70: sfcdecl->print(symbol->fblock_body); lbessard@70: delete sfcdecl; lbessard@70: lbessard@70: /* (C.7) Action undefinitions */ lbessard@70: sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::actionundef_sd); lbessard@70: sfcdecl->print(symbol->fblock_body); lbessard@70: delete sfcdecl; lbessard@70: lbessard@70: s4o.indent_left(); lbessard@70: s4o.print("\n\n\n\n"); lbessard@70: lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: lbessard@70: /* The remaining temp_var_decls_c, temp_var_decls_list_c lbessard@70: * and non_retentive_var_decls_c are handled in the generate_c_vardecl_c class lbessard@70: */ lbessard@70: lbessard@70: lbessard@70: /**********************/ lbessard@70: /* B 1.5.3 - Programs */ lbessard@70: /**********************/ lbessard@70: lbessard@70: lbessard@70: lbessard@70: public: lbessard@70: /* PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */ lbessard@70: //SYM_REF4(program_declaration_c, program_type_name, var_declarations, function_block_body, unused) lbessard@70: void *visit(program_declaration_c *symbol) { lbessard@70: generate_c_vardecl_c *vardecl; lbessard@70: generate_c_sfcdecl_c *sfcdecl; lbessard@70: TRACE("program_declaration_c"); lbessard@70: lbessard@70: /* start off by adding this declaration to the global lbessard@70: * program declaration symbol table... lbessard@70: */ lbessard@70: program_type_symtable.insert(symbol->program_type_name, symbol); lbessard@70: lbessard@70: /* (A) Program data structure declaration... */ lbessard@70: /* (A.1) Data structure declaration */ lbessard@70: s4o.print("// PROGRAM "); lbessard@70: symbol->program_type_name->accept(*this); lbessard@70: s4o.print("\n// Data part\n"); lbessard@70: s4o.print("typedef struct {\n"); lbessard@70: s4o.indent_right(); lbessard@70: lbessard@70: /* (A.2) Public variables: i.e. the program parameters... */ lbessard@70: s4o.print(s4o.indent_spaces + "// PROGRAM Interface - IN, OUT, IN_OUT variables\n"); lbessard@70: vardecl = new generate_c_vardecl_c(&s4o, lbessard@70: generate_c_vardecl_c::local_vf, lbessard@70: generate_c_vardecl_c::input_vt | lbessard@70: generate_c_vardecl_c::output_vt | lbessard@70: generate_c_vardecl_c::inoutput_vt); lbessard@70: vardecl->print(symbol->var_declarations); lbessard@70: delete vardecl; lbessard@70: s4o.print("\n"); lbessard@70: /* (A.3) Private internal variables */ lbessard@70: s4o.print(s4o.indent_spaces + "// PROGRAM private variables - TEMP, private and located variables\n"); lbessard@70: vardecl = new generate_c_vardecl_c(&s4o, lbessard@70: generate_c_vardecl_c::local_vf, lbessard@70: generate_c_vardecl_c::temp_vt | lbessard@70: generate_c_vardecl_c::private_vt | lbessard@70: generate_c_vardecl_c::located_vt | lbessard@70: generate_c_vardecl_c::external_vt); lbessard@70: vardecl->print(symbol->var_declarations); lbessard@70: delete vardecl; lbessard@70: /* (A.4) Generate private internal variables for SFC */ lbessard@70: sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::sfcdecl_sd); lbessard@70: sfcdecl->print(symbol->function_block_body); lbessard@70: delete sfcdecl; lbessard@70: lbessard@70: /* (A.5) Program data structure type name. */ lbessard@70: s4o.indent_left(); lbessard@70: s4o.print("} "); lbessard@70: symbol->program_type_name->accept(*this); lbessard@70: s4o.print(";\n\n"); lbessard@70: lbessard@70: /* (B) Constructor */ lbessard@70: /* (B.1) Constructor name... */ lbessard@70: s4o.print(s4o.indent_spaces + "void "); lbessard@70: symbol->program_type_name->accept(*this); lbessard@70: s4o.print(FB_INIT_SUFFIX); lbessard@70: s4o.print("("); lbessard@70: lbessard@70: /* first and only parameter is a pointer to the data */ lbessard@70: symbol->program_type_name->accept(*this); lbessard@70: s4o.print(" *"); lbessard@70: s4o.print(FB_FUNCTION_PARAM); lbessard@70: s4o.print(") {\n"); lbessard@70: s4o.indent_right(); lbessard@70: lbessard@70: /* (B.2) Member initializations... */ lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: vardecl = new generate_c_vardecl_c(&s4o, lbessard@70: generate_c_vardecl_c::constructorinit_vf, lbessard@70: generate_c_vardecl_c::input_vt | lbessard@70: generate_c_vardecl_c::output_vt | lbessard@70: generate_c_vardecl_c::inoutput_vt | lbessard@70: generate_c_vardecl_c::private_vt | lbessard@70: generate_c_vardecl_c::located_vt | lbessard@70: generate_c_vardecl_c::external_vt); lbessard@70: vardecl->print(symbol->var_declarations, NULL, FB_FUNCTION_PARAM"->"); lbessard@70: delete vardecl; lbessard@70: s4o.print("\n"); lbessard@70: /* (B.3) Generate private internal variables for SFC */ lbessard@70: sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::sfcinit_sd); lbessard@70: sfcdecl->print(symbol->function_block_body,FB_FUNCTION_PARAM"->"); lbessard@70: delete sfcdecl; lbessard@70: lbessard@70: s4o.indent_left(); lbessard@70: s4o.print(s4o.indent_spaces + "}\n\n"); lbessard@70: lbessard@70: /* (C) Function with PROGRAM body */ lbessard@70: /* (C.1) Step definitions */ lbessard@70: sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::stepdef_sd); lbessard@70: sfcdecl->print(symbol->function_block_body); lbessard@70: delete sfcdecl; lbessard@70: lbessard@70: /* (C.2) Action definitions */ lbessard@70: sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::actiondef_sd); lbessard@70: sfcdecl->print(symbol->function_block_body); lbessard@70: delete sfcdecl; lbessard@70: lbessard@70: /* (C.3) Function declaration */ lbessard@70: s4o.print("// Code part\n"); lbessard@70: /* function interface */ lbessard@70: s4o.print("void "); lbessard@70: symbol->program_type_name->accept(*this); lbessard@70: s4o.print(FB_FUNCTION_SUFFIX); lbessard@70: s4o.print("("); lbessard@70: /* first and only parameter is a pointer to the data */ lbessard@70: symbol->program_type_name->accept(*this); lbessard@70: s4o.print(" *"); lbessard@70: s4o.print(FB_FUNCTION_PARAM); lbessard@70: s4o.print(") {\n"); lbessard@70: s4o.indent_right(); lbessard@70: lbessard@70: /* (C.4) Initialize TEMP variables */ lbessard@70: /* function body */ lbessard@70: s4o.print(s4o.indent_spaces + "// Initialise TEMP variables\n"); lbessard@70: vardecl = new generate_c_vardecl_c(&s4o, lbessard@70: generate_c_vardecl_c::init_vf, lbessard@70: generate_c_vardecl_c::temp_vt); lbessard@70: vardecl->print(symbol->var_declarations, NULL, FB_FUNCTION_PARAM"->"); lbessard@70: delete vardecl; lbessard@70: s4o.print("\n"); lbessard@70: lbessard@70: /* (C.5) Function code */ lbessard@70: generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol, FB_FUNCTION_PARAM"->"); lbessard@70: symbol->function_block_body->accept(generate_c_code); lbessard@70: s4o.indent_left(); lbessard@70: s4o.print(s4o.indent_spaces + "} // "); lbessard@70: symbol->program_type_name->accept(*this); lbessard@70: s4o.print(FB_FUNCTION_SUFFIX); lbessard@70: s4o.print(s4o.indent_spaces + "() \n\n"); lbessard@70: lbessard@70: /* (C.6) Step undefinitions */ lbessard@70: sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::stepundef_sd); lbessard@70: sfcdecl->print(symbol->function_block_body); lbessard@70: delete sfcdecl; lbessard@70: lbessard@70: /* (C.7) Action undefinitions */ lbessard@70: sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::actionundef_sd); lbessard@70: sfcdecl->print(symbol->function_block_body); lbessard@70: delete sfcdecl; lbessard@70: lbessard@70: s4o.indent_left(); lbessard@70: s4o.print("\n\n\n\n"); lbessard@70: lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: }; /* generate_c_pous_c */ lbessard@70: lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: lbessard@70: class generate_c_config_c: public generate_c_typedecl_c { lbessard@70: lbessard@70: public: lbessard@70: generate_c_config_c(stage4out_c *s4o_ptr) lbessard@70: : generate_c_typedecl_c(s4o_ptr) {}; lbessard@70: virtual ~generate_c_config_c(void) {} lbessard@70: lbessard@70: typedef enum { lbessard@70: initprotos_dt, lbessard@70: initdeclare_dt, lbessard@70: runprotos_dt, lbessard@70: rundeclare_dt lbessard@70: } declaretype_t; lbessard@70: lbessard@70: declaretype_t wanted_declaretype; lbessard@70: lbessard@70: /********************************/ lbessard@70: /* B 1.7 Configuration elements */ lbessard@70: /********************************/ lbessard@70: lbessard@70: lbessard@70: public: lbessard@70: /* lbessard@70: CONFIGURATION configuration_name lbessard@70: optional_global_var_declarations lbessard@70: (resource_declaration_list | single_resource_declaration) lbessard@70: optional_access_declarations lbessard@70: optional_instance_specific_initializations lbessard@70: END_CONFIGURATION lbessard@70: */ lbessard@70: /* lbessard@70: SYM_REF6(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations, unused) lbessard@70: */ lbessard@70: void *visit(configuration_declaration_c *symbol) { lbessard@70: generate_c_vardecl_c *vardecl; lbessard@70: lbessard@70: /* Insert the header... */ lbessard@70: s4o.print("/*******************************************/\n"); lbessard@70: s4o.print("/* FILE GENERATED BY iec2c */\n"); lbessard@70: s4o.print("/* Editing this file is not recommended... */\n"); lbessard@70: s4o.print("/*******************************************/\n\n"); lbessard@70: s4o.print("#include \"iec_std_lib.h\"\n\n"); lbessard@70: lbessard@70: /* (A) configuration declaration... */ lbessard@70: /* (A.1) configuration name in comment */ lbessard@70: s4o.print("// CONFIGURATION "); lbessard@70: symbol->configuration_name->accept(*this); lbessard@70: s4o.print("\n"); lbessard@70: lbessard@70: /* (A.2) Global variables */ lbessard@70: vardecl = new generate_c_vardecl_c(&s4o, lbessard@70: generate_c_vardecl_c::local_vf, lbessard@70: generate_c_vardecl_c::global_vt); lbessard@70: vardecl->print(symbol); lbessard@70: delete vardecl; lbessard@70: s4o.print("\n"); lbessard@70: lbessard@70: /* (B) Initialisation Function */ lbessard@70: /* (B.1) Ressources initialisation protos... */ lbessard@70: wanted_declaretype = initprotos_dt; lbessard@70: symbol->resource_declarations->accept(*this); lbessard@70: s4o.print("\n"); lbessard@70: lbessard@70: /* (B.2) Initialisation function name... */ lbessard@70: s4o.print(s4o.indent_spaces + "void config"); lbessard@70: s4o.print(FB_INIT_SUFFIX); lbessard@70: s4o.print("(void) {\n"); lbessard@70: s4o.indent_right(); lbessard@70: lbessard@70: /* (B.3) Global variables initializations... */ lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: vardecl = new generate_c_vardecl_c(&s4o, lbessard@70: generate_c_vardecl_c::constructorinit_vf, lbessard@70: generate_c_vardecl_c::global_vt); lbessard@70: vardecl->print(symbol); lbessard@70: delete vardecl; lbessard@70: s4o.print("\n"); lbessard@70: lbessard@70: /* (B.3) Resources initializations... */ lbessard@70: wanted_declaretype = initdeclare_dt; lbessard@70: symbol->resource_declarations->accept(*this); lbessard@70: lbessard@70: s4o.indent_left(); lbessard@70: s4o.print(s4o.indent_spaces + "}\n\n"); lbessard@70: lbessard@70: lbessard@70: /* (C) Run Function*/ lbessard@70: /* (C.1) Resources run functions protos... */ lbessard@70: wanted_declaretype = runprotos_dt; lbessard@70: symbol->resource_declarations->accept(*this); lbessard@70: s4o.print("\n"); lbessard@70: lbessard@70: /* (C.2) Run function name... */ lbessard@70: s4o.print(s4o.indent_spaces + "void config"); lbessard@70: s4o.print(FB_RUN_SUFFIX); lbessard@70: s4o.print("(int tick) {\n"); lbessard@70: s4o.indent_right(); lbessard@70: lbessard@70: /* (C.3) Resources initializations... */ lbessard@70: wanted_declaretype = rundeclare_dt; lbessard@70: symbol->resource_declarations->accept(*this); lbessard@70: lbessard@70: /* (C.3) Close Public Function body */ lbessard@70: s4o.indent_left(); lbessard@70: s4o.print(s4o.indent_spaces + "}\n"); lbessard@70: lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(resource_declaration_c *symbol) { lbessard@70: if (wanted_declaretype == initprotos_dt || wanted_declaretype == runprotos_dt) { lbessard@70: s4o.print(s4o.indent_spaces + "void "); lbessard@70: symbol->resource_name->accept(*this); lbessard@70: if (wanted_declaretype == initprotos_dt) { lbessard@70: s4o.print(FB_INIT_SUFFIX); lbessard@70: s4o.print("(void);\n"); lbessard@70: } lbessard@70: else { lbessard@70: s4o.print(FB_RUN_SUFFIX); lbessard@70: s4o.print("(int tick);\n"); lbessard@70: } lbessard@70: } lbessard@70: if (wanted_declaretype == initdeclare_dt || wanted_declaretype == rundeclare_dt) { lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: symbol->resource_name->accept(*this); lbessard@70: if (wanted_declaretype == initdeclare_dt) { lbessard@70: s4o.print(FB_INIT_SUFFIX); lbessard@70: s4o.print("();\n"); lbessard@70: } lbessard@70: else { lbessard@70: s4o.print(FB_RUN_SUFFIX); lbessard@70: s4o.print("(tick);\n"); lbessard@70: } lbessard@70: } lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(single_resource_declaration_c *symbol) { lbessard@70: if (wanted_declaretype == initprotos_dt || wanted_declaretype == runprotos_dt) { lbessard@70: s4o.print(s4o.indent_spaces + "void RESOURCE"); lbessard@70: if (wanted_declaretype == initprotos_dt) { lbessard@70: s4o.print(FB_INIT_SUFFIX); lbessard@70: s4o.print("(void);\n"); lbessard@70: } lbessard@70: else { lbessard@70: s4o.print(FB_RUN_SUFFIX); lbessard@70: s4o.print("(int tick);\n"); lbessard@70: } lbessard@70: } lbessard@70: if (wanted_declaretype == initdeclare_dt || wanted_declaretype == rundeclare_dt) { lbessard@70: s4o.print(s4o.indent_spaces + "RESOURCE"); lbessard@70: if (wanted_declaretype == initdeclare_dt) { lbessard@70: s4o.print(FB_INIT_SUFFIX); lbessard@70: s4o.print("();\n"); lbessard@70: } lbessard@70: else { lbessard@70: s4o.print(FB_RUN_SUFFIX); lbessard@70: s4o.print("(tick);\n"); lbessard@70: } lbessard@70: } lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: }; lbessard@70: lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: lbessard@70: lbessard@70: class generate_c_resources_c: public generate_c_typedecl_c { lbessard@70: lbessard@70: search_var_instance_decl_c *search_config_instance; lbessard@70: search_var_instance_decl_c *search_resource_instance; lbessard@70: lbessard@70: private: lbessard@70: /* The name of the resource curretnly being processed... */ lbessard@70: symbol_c *current_resource_name; lbessard@70: symbol_c *current_global_vars; lbessard@70: lbessard@70: public: lbessard@70: generate_c_resources_c(stage4out_c *s4o_ptr, symbol_c *config_scope, symbol_c *resource_scope, unsigned long time) lbessard@70: : generate_c_typedecl_c(s4o_ptr) { lbessard@70: search_config_instance = new search_var_instance_decl_c(config_scope); lbessard@70: search_resource_instance = new search_var_instance_decl_c(resource_scope); lbessard@70: common_ticktime = time; lbessard@70: current_resource_name = NULL; lbessard@70: current_global_vars = NULL; lbessard@70: }; lbessard@70: virtual ~generate_c_resources_c(void) { lbessard@70: delete search_config_instance; lbessard@70: delete search_resource_instance; lbessard@70: } lbessard@70: lbessard@70: typedef enum { lbessard@70: declare_dt, lbessard@70: init_dt, lbessard@70: run_dt lbessard@70: } declaretype_t; lbessard@70: lbessard@70: declaretype_t wanted_declaretype; lbessard@70: lbessard@70: unsigned long common_ticktime; lbessard@70: lbessard@70: const char *current_program_name; lbessard@70: lbessard@70: typedef enum { lbessard@70: assign_at, lbessard@70: send_at lbessard@70: } assigntype_t; lbessard@70: lbessard@70: assigntype_t wanted_assigntype; lbessard@70: lbessard@70: /********************************/ lbessard@70: /* B 1.7 Configuration elements */ lbessard@70: /********************************/ lbessard@70: lbessard@70: /* lbessard@70: RESOURCE resource_name ON resource_type_name lbessard@70: optional_global_var_declarations lbessard@70: single_resource_declaration lbessard@70: END_RESOURCE lbessard@70: */ lbessard@70: // SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration) lbessard@70: void *visit(resource_declaration_c *symbol) { lbessard@70: current_resource_name = symbol->resource_name; lbessard@70: current_global_vars = symbol->global_var_declarations; lbessard@70: lbessard@70: symbol->resource_declaration->accept(*this); lbessard@70: lbessard@70: current_resource_name = NULL; lbessard@70: current_global_vars = NULL; lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* task_configuration_list program_configuration_list */ lbessard@70: // SYM_REF2(single_resource_declaration_c, task_configuration_list, program_configuration_list) lbessard@70: void *visit(single_resource_declaration_c *symbol) { lbessard@70: bool single_resource = current_resource_name == NULL; lbessard@70: if (single_resource) lbessard@70: current_resource_name = new identifier_c("RESOURCE"); lbessard@70: generate_c_vardecl_c *vardecl; lbessard@70: lbessard@70: /* Insert the header... */ lbessard@70: s4o.print("/*******************************************/\n"); lbessard@70: s4o.print("/* FILE GENERATED BY iec2c */\n"); lbessard@70: s4o.print("/* Editing this file is not recommended... */\n"); lbessard@70: s4o.print("/*******************************************/\n\n"); lbessard@70: s4o.print("#include \"iec_std_lib.h\"\n\n"); lbessard@70: lbessard@70: /* (A) resource declaration... */ lbessard@70: /* (A.1) resource name in comment */ lbessard@70: s4o.print("// RESOURCE "); lbessard@70: current_resource_name->accept(*this); lbessard@70: s4o.print("\n"); lbessard@70: lbessard@70: /* (A.2) Global variables... */ lbessard@70: if (current_global_vars != NULL) { lbessard@70: vardecl = new generate_c_vardecl_c(&s4o, lbessard@70: generate_c_vardecl_c::local_vf, lbessard@70: generate_c_vardecl_c::global_vt); lbessard@70: vardecl->print(current_global_vars); lbessard@70: delete vardecl; lbessard@70: } lbessard@70: s4o.print("\n"); lbessard@70: lbessard@70: /* (A.3) POUs inclusion */ lbessard@70: s4o.print("#include \"POUS.c\"\n\n"); lbessard@70: lbessard@70: /* (A.4) Resource programs declaration... */ lbessard@70: wanted_declaretype = declare_dt; lbessard@70: symbol->program_configuration_list->accept(*this); lbessard@70: s4o.print("\n"); lbessard@70: lbessard@70: /* (B) resource initialisation function... */ lbessard@70: /* (B.1) initialisation function name... */ lbessard@70: s4o.print("void "); lbessard@70: current_resource_name->accept(*this); lbessard@70: s4o.print(FB_INIT_SUFFIX); lbessard@70: s4o.print("(void) {\n"); lbessard@70: s4o.indent_right(); lbessard@70: lbessard@70: /* (B.2) Global variables initialisations... */ lbessard@70: if (current_global_vars != NULL) { lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: vardecl = new generate_c_vardecl_c(&s4o, lbessard@70: generate_c_vardecl_c::constructorinit_vf, lbessard@70: generate_c_vardecl_c::global_vt); lbessard@70: vardecl->print(current_global_vars); lbessard@70: delete vardecl; lbessard@70: } lbessard@70: s4o.print("\n"); lbessard@70: lbessard@70: /* (B.3) Resource programs initialisations... */ lbessard@70: wanted_declaretype = init_dt; lbessard@70: symbol->program_configuration_list->accept(*this); lbessard@70: lbessard@70: s4o.indent_left(); lbessard@70: s4o.print("}\n\n"); lbessard@70: lbessard@70: /* (C) Resource run function... */ lbessard@70: /* (C.1) Run function name... */ lbessard@70: s4o.print("void "); lbessard@70: current_resource_name->accept(*this); lbessard@70: s4o.print(FB_RUN_SUFFIX); lbessard@70: s4o.print("(int tick) {\n"); lbessard@70: s4o.indent_right(); lbessard@70: lbessard@70: /* (C.2) Task management... */ lbessard@70: symbol->task_configuration_list->accept(*this); lbessard@70: lbessard@70: /* (C.3) Program run declaration... */ lbessard@70: wanted_declaretype = run_dt; lbessard@70: symbol->program_configuration_list->accept(*this); lbessard@70: lbessard@70: s4o.indent_left(); lbessard@70: s4o.print("}\n\n"); lbessard@70: lbessard@70: if (single_resource) { lbessard@70: delete current_resource_name; lbessard@70: current_resource_name = NULL; lbessard@70: } lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */ lbessard@70: //SYM_REF6(program_configuration_c, retain_option, program_name, task_name, program_type_name, prog_conf_elements, unused) lbessard@70: void *visit(program_configuration_c *symbol) { lbessard@70: if (wanted_declaretype == declare_dt) { lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: symbol->program_type_name->accept(*this); lbessard@70: s4o.print(" "); lbessard@70: symbol->program_name->accept(*this); lbessard@70: s4o.print(";\n"); lbessard@70: } lbessard@70: if (wanted_declaretype == init_dt) { lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: symbol->program_type_name->accept(*this); lbessard@70: s4o.print(FB_INIT_SUFFIX); lbessard@70: s4o.print("(&"); lbessard@70: symbol->program_name->accept(*this); lbessard@70: s4o.print(");\n"); lbessard@70: } lbessard@70: if (wanted_declaretype == run_dt) { lbessard@70: current_program_name = ((identifier_c*)(symbol->program_name))->value; lbessard@70: if (symbol->task_name != NULL) { lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: s4o.print("if (!"); lbessard@70: symbol->task_name->accept(*this); lbessard@70: s4o.print(") {\n"); lbessard@70: s4o.indent_right(); lbessard@70: } lbessard@70: lbessard@70: wanted_assigntype = assign_at; lbessard@70: if (symbol->prog_conf_elements != NULL) lbessard@70: symbol->prog_conf_elements->accept(*this); lbessard@70: lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: symbol->program_type_name->accept(*this); lbessard@70: s4o.print(FB_FUNCTION_SUFFIX); lbessard@70: s4o.print("(&"); lbessard@70: symbol->program_name->accept(*this); lbessard@70: s4o.print(");\n"); lbessard@70: lbessard@70: wanted_assigntype = send_at; lbessard@70: if (symbol->prog_conf_elements != NULL) lbessard@70: symbol->prog_conf_elements->accept(*this); lbessard@70: lbessard@70: if (symbol->task_name != NULL) { lbessard@70: s4o.indent_left(); lbessard@70: s4o.print(s4o.indent_spaces + "}\n"); lbessard@70: } lbessard@70: } lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* TASK task_name task_initialization */ lbessard@70: //SYM_REF2(task_configuration_c, task_name, task_initialization) lbessard@70: void *visit(task_configuration_c *symbol) { lbessard@70: s4o.print(s4o.indent_spaces + "int "); lbessard@70: symbol->task_name->accept(*this); lbessard@70: s4o.print(" = "); lbessard@70: symbol->task_initialization->accept(*this); lbessard@70: s4o.print(";\n"); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* '(' [SINGLE ASSIGN data_source ','] [INTERVAL ASSIGN data_source ','] PRIORITY ASSIGN integer ')' */ lbessard@70: //SYM_REF4(task_initialization_c, single_data_source, interval_data_source, priority_data_source, unused) lbessard@70: void *visit(task_initialization_c *symbol) { lbessard@70: if (symbol->interval_data_source != NULL) { lbessard@70: calculate_time_c calculate_time; lbessard@70: symbol->interval_data_source->accept(calculate_time); lbessard@70: unsigned long time = calculate_time.get_time(); lbessard@70: if (time != 0) { lbessard@70: s4o.print("tick % "); lbessard@70: s4o.print_integer((int)(time / common_ticktime)); lbessard@70: } lbessard@70: else lbessard@70: s4o.print("1"); lbessard@70: } lbessard@70: else lbessard@70: s4o.print("1"); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* any_symbolic_variable ASSIGN prog_data_source */ lbessard@70: //SYM_REF2(prog_cnxn_assign_c, symbolic_variable, prog_data_source) lbessard@70: void *visit(prog_cnxn_assign_c *symbol) { lbessard@70: if (wanted_assigntype == assign_at) { lbessard@70: symbol_c *var_decl; lbessard@70: unsigned int vartype = 0; lbessard@70: symbol_c *current_var_reference = ((global_var_reference_c *)(symbol->prog_data_source))->global_var_name; lbessard@70: var_decl = search_resource_instance->get_decl(current_var_reference); lbessard@70: if (var_decl == NULL) { lbessard@70: var_decl = search_config_instance->get_decl(current_var_reference); lbessard@70: if (var_decl == NULL) lbessard@70: ERROR; lbessard@70: else lbessard@70: vartype = search_config_instance->get_vartype(); lbessard@70: } lbessard@70: else lbessard@70: vartype = search_resource_instance->get_vartype(); lbessard@70: lbessard@70: s4o.print(s4o.indent_spaces + "{extern "); lbessard@70: var_decl->accept(*this); lbessard@70: s4o.print(" "); lbessard@70: symbol->prog_data_source->accept(*this); lbessard@70: s4o.print("; "); lbessard@70: s4o.print(current_program_name); lbessard@70: s4o.print("."); lbessard@70: symbol->symbolic_variable->accept(*this); lbessard@70: s4o.print(" = "); lbessard@70: if (vartype || search_var_instance_decl_c::global_vt) lbessard@70: s4o.print("*"); lbessard@70: symbol->prog_data_source->accept(*this); lbessard@70: s4o.print(";}\n"); lbessard@70: } lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* any_symbolic_variable SENDTO data_sink */ lbessard@70: //SYM_REF2(prog_cnxn_sendto_c, symbolic_variable, data_sink) lbessard@70: void *visit(prog_cnxn_sendto_c *symbol) { lbessard@70: if (wanted_assigntype == send_at) { lbessard@70: symbol_c *var_decl; lbessard@70: unsigned int vartype = 0; lbessard@70: symbol_c *current_var_reference = ((global_var_reference_c *)(symbol->data_sink))->global_var_name; lbessard@70: var_decl = search_resource_instance->get_decl(current_var_reference); lbessard@70: if (var_decl == NULL) { lbessard@70: var_decl = search_config_instance->get_decl(current_var_reference); lbessard@70: if (var_decl == NULL) lbessard@70: ERROR; lbessard@70: else lbessard@70: vartype = search_config_instance->get_vartype(); lbessard@70: } lbessard@70: else lbessard@70: vartype = search_resource_instance->get_vartype(); lbessard@70: lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: s4o.print(s4o.indent_spaces + "{extern "); lbessard@70: var_decl->accept(*this); lbessard@70: s4o.print(" "); lbessard@70: symbol->data_sink->accept(*this); lbessard@70: s4o.print("; "); lbessard@70: if (vartype || search_var_instance_decl_c::global_vt) lbessard@70: s4o.print("*"); lbessard@70: symbol->data_sink->accept(*this); lbessard@70: s4o.print(" = "); lbessard@70: s4o.print(current_program_name); lbessard@70: s4o.print("."); lbessard@70: symbol->symbolic_variable->accept(*this); lbessard@70: s4o.print("};\n"); lbessard@70: } lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: }; lbessard@70: lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: lbessard@70: class generate_c_c: public iterator_visitor_c { lbessard@70: protected: lbessard@70: stage4out_c &s4o; lbessard@70: stage4out_c pous_s4o; lbessard@70: stage4out_c located_variables_s4o; lbessard@70: generate_c_pous_c generate_c_pous; lbessard@70: lbessard@70: symbol_c *current_configuration; lbessard@70: lbessard@70: const char *current_name; lbessard@70: const char *current_builddir; lbessard@70: lbessard@70: unsigned long common_ticktime; lbessard@70: lbessard@70: public: lbessard@70: generate_c_c(stage4out_c *s4o_ptr, const char *builddir): lbessard@70: s4o(*s4o_ptr), lbessard@70: pous_s4o(builddir, "POUS", "c"), lbessard@70: located_variables_s4o(builddir, "LOCATED_VARIABLES","h"), lbessard@70: generate_c_pous(&pous_s4o) { lbessard@70: current_builddir = builddir; lbessard@70: current_configuration = NULL; lbessard@70: } lbessard@70: lbessard@70: ~generate_c_c(void) {} lbessard@70: lbessard@70: /***************************/ lbessard@70: /* B 0 - Programming Model */ lbessard@70: /***************************/ lbessard@70: void *visit(library_c *symbol) { lbessard@70: for(int i = 0; i < symbol->n; i++) { lbessard@70: symbol->elements[i]->accept(*this); lbessard@70: } lbessard@98: generate_location_list_c generate_location_list(&located_variables_s4o); lbessard@98: symbol->accept(generate_location_list); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /*************************/ lbessard@70: /* B.1 - Common elements */ lbessard@70: /*************************/ lbessard@70: /*******************************************/ lbessard@70: /* B 1.1 - Letters, digits and identifiers */ lbessard@70: /*******************************************/ lbessard@70: void *visit(identifier_c *symbol) { lbessard@70: current_name = symbol->value; lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@98: /********************************/ lbessard@98: /* B 1.3.3 - Derived data types */ lbessard@98: /********************************/ lbessard@98: /* TYPE type_declaration_list END_TYPE */ lbessard@98: void *visit(data_type_declaration_c *symbol) { lbessard@98: symbol->accept(generate_c_pous); lbessard@98: return NULL; lbessard@98: } lbessard@98: lbessard@70: /**************************************/ lbessard@70: /* B.1.5 - Program organization units */ lbessard@70: /**************************************/ lbessard@70: /***********************/ lbessard@70: /* B 1.5.1 - Functions */ lbessard@70: /***********************/ lbessard@70: void *visit(function_declaration_c *symbol) { lbessard@70: symbol->accept(generate_c_pous); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /*****************************/ lbessard@70: /* B 1.5.2 - Function Blocks */ lbessard@70: /*****************************/ lbessard@70: void *visit(function_block_declaration_c *symbol) { lbessard@70: symbol->accept(generate_c_pous); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /**********************/ lbessard@70: /* B 1.5.3 - Programs */ lbessard@70: /**********************/ lbessard@70: void *visit(program_declaration_c *symbol) { lbessard@70: symbol->accept(generate_c_pous); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: lbessard@70: /********************************/ lbessard@70: /* B 1.7 Configuration elements */ lbessard@70: /********************************/ lbessard@70: void *visit(configuration_declaration_c *symbol) { lbessard@70: static int configuration_count = 0; lbessard@70: lbessard@70: if (configuration_count++) { lbessard@70: /* the first configuration is the one we will use!! */ lbessard@70: ERROR; lbessard@70: } lbessard@70: lbessard@70: current_configuration = symbol; lbessard@70: lbessard@70: calculate_common_ticktime_c calculate_common_ticktime; lbessard@70: symbol->accept(calculate_common_ticktime); lbessard@70: common_ticktime = calculate_common_ticktime.get_ticktime(); lbessard@70: lbessard@70: symbol->configuration_name->accept(*this); lbessard@70: stage4out_c config_s4o(current_builddir, current_name, "c"); lbessard@70: generate_c_config_c generate_c_config(&config_s4o); lbessard@70: symbol->accept(generate_c_config); lbessard@70: lbessard@70: config_s4o.print("int common_ticktime__ = "); lbessard@70: config_s4o.print_integer((int)(common_ticktime / 1000000)); lbessard@70: config_s4o.print("; /*ms*/\n"); lbessard@70: lbessard@70: symbol->resource_declarations->accept(*this); lbessard@70: lbessard@70: current_configuration = NULL; lbessard@70: lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(resource_declaration_c *symbol) { lbessard@70: symbol->resource_name->accept(*this); lbessard@70: stage4out_c resources_s4o(current_builddir, current_name, "c"); lbessard@70: generate_c_resources_c generate_c_resources(&resources_s4o, current_configuration, symbol, common_ticktime); lbessard@70: symbol->accept(generate_c_resources); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(single_resource_declaration_c *symbol) { lbessard@70: stage4out_c resources_s4o(current_builddir, "RESOURCE", "c"); lbessard@70: generate_c_resources_c generate_c_resources(&resources_s4o, current_configuration, symbol, common_ticktime); lbessard@70: symbol->accept(generate_c_resources); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: }; lbessard@70: lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: visitor_c *new_code_generator(stage4out_c *s4o, const char *builddir) {return new generate_c_c(s4o, builddir);} lbessard@70: void delete_code_generator(visitor_c *code_generator) {delete code_generator;} lbessard@70: lbessard@70: