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