lbessard@70: /* msousa@267: * matiec - a compiler for the programming languages defined in IEC 61131-3 lbessard@70: * msousa@267: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant lbessard@70: * msousa@267: * This program is free software: you can redistribute it and/or modify msousa@267: * it under the terms of the GNU General Public License as published by msousa@267: * the Free Software Foundation, either version 3 of the License, or msousa@267: * (at your option) any later version. msousa@267: * msousa@267: * This program is distributed in the hope that it will be useful, msousa@267: * but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@267: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@267: * GNU General Public License for more details. msousa@267: * msousa@267: * You should have received a copy of the GNU General Public License msousa@267: * along with this program. If not, see . msousa@267: * 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: #include lbessard@70: #include lbessard@70: #include lbessard@70: #include lbessard@111: #include etisserant@139: #include lbessard@70: lbessard@70: #include "../../util/symtable.hh" lbessard@70: #include "../../util/dsymtable.hh" lbessard@70: #include "../../absyntax/visitor.hh" mario@181: #include "../../absyntax_utils/absyntax_utils.hh" lbessard@70: lbessard@70: #include "../stage4.hh" 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: #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: /* 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: laurent@217: laurent@217: /* Variable declaration symbol for accessor macros */ laurent@217: #define DECLARE_VAR "__DECLARE_VAR" laurent@217: #define DECLARE_GLOBAL "__DECLARE_GLOBAL" laurent@217: #define DECLARE_GLOBAL_LOCATION "__DECLARE_GLOBAL_LOCATION" laurent@217: #define DECLARE_GLOBAL_LOCATED "__DECLARE_GLOBAL_LOCATED" laurent@217: #define DECLARE_EXTERNAL "__DECLARE_EXTERNAL" laurent@217: #define DECLARE_LOCATED "__DECLARE_LOCATED" laurent@217: laurent@217: /* Variable declaration symbol for accessor macros */ laurent@217: #define INIT_VAR "__INIT_VAR" laurent@217: #define INIT_GLOBAL "__INIT_GLOBAL" laurent@217: #define INIT_GLOBAL_LOCATED "__INIT_GLOBAL_LOCATED" laurent@217: #define INIT_EXTERNAL "__INIT_EXTERNAL" laurent@217: #define INIT_LOCATED "__INIT_LOCATED" laurent@217: #define INIT_LOCATED_VALUE "__INIT_LOCATED_VALUE" laurent@217: laurent@217: laurent@217: /* Variable getter symbol for accessor macros */ laurent@217: #define GET_VAR "__GET_VAR" laurent@217: #define GET_EXTERNAL "__GET_EXTERNAL" laurent@217: #define GET_LOCATED "__GET_LOCATED" laurent@217: #define GET_VAR_BY_REF "__GET_VAR_BY_REF" laurent@217: #define GET_EXTERNAL_BY_REF "__GET_EXTERNAL_BY_REF" laurent@217: #define GET_LOCATED_BY_REF "__GET_LOCATED_BY_REF" laurent@217: laurent@217: /* Variable setter symbol for accessor macros */ laurent@217: #define SET_VAR "__SET_VAR" laurent@217: #define SET_EXTERNAL "__SET_EXTERNAL" laurent@231: #define SET_COMPLEX_EXTERNAL "__SET_COMPLEX_EXTERNAL" laurent@217: #define SET_LOCATED "__SET_LOCATED" laurent@217: 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: laurent@217: /***********************************************************************/ laurent@217: /***********************************************************************/ laurent@217: /***********************************************************************/ laurent@217: /***********************************************************************/ laurent@217: laurent@217: #include "generate_c_base.cc" laurent@217: #include "generate_c_typedecl.cc" laurent@217: #include "generate_c_sfcdecl.cc" laurent@217: #include "generate_c_vardecl.cc" laurent@217: #include "generate_c_configbody.cc" laurent@217: #include "generate_location_list.cc" laurent@217: #include "generate_var_list.cc" laurent@217: laurent@217: /***********************************************************************/ laurent@217: /***********************************************************************/ laurent@217: /***********************************************************************/ laurent@217: /***********************************************************************/ laurent@217: lbessard@70: #include "generate_c_st.cc" lbessard@70: #include "generate_c_il.cc" laurent@217: #include "generate_c_inlinefcall.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: laurent@210: unsigned long long time; lbessard@70: float current_value; lbessard@70: lbessard@70: public: lbessard@70: calculate_time_c(void){time = 0;}; lbessard@70: laurent@210: unsigned long 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); laurent@210: time += (unsigned long 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); laurent@210: time += (unsigned long 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); laurent@210: time += (unsigned long 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); laurent@210: time += (unsigned long 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); laurent@210: time += (unsigned long 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: laurent@210: unsigned long long common_ticktime; laurent@210: unsigned long long least_common_ticktime; lbessard@70: lbessard@70: public: laurent@210: calculate_common_ticktime_c(void){ laurent@210: common_ticktime = 0; laurent@210: least_common_ticktime = 0; laurent@210: } laurent@210: laurent@210: unsigned long long euclide(unsigned long long a, unsigned long long b) { laurent@210: unsigned long 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: laurent@210: void update_ticktime(unsigned long 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); laurent@210: if (least_common_ticktime == 0) laurent@210: least_common_ticktime = time; laurent@210: else laurent@210: least_common_ticktime = (least_common_ticktime * time) / common_ticktime; laurent@210: } laurent@210: laurent@210: unsigned long long get_common_ticktime(void) { lbessard@70: return common_ticktime; lbessard@70: } lbessard@70: laurent@210: unsigned long get_greatest_tick_count(void) { laurent@210: unsigned long long least_common_tick = least_common_ticktime / common_ticktime; laurent@210: if (least_common_tick >> 32) laurent@210: ERROR; laurent@237: return (unsigned long)(~(((unsigned long)-1) % (unsigned long)least_common_tick) + 1); laurent@210: } laurent@210: 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; laurent@210: unsigned long 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: laurent@217: /* A helper class that knows how to generate code for the SFC, 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; laurent@217: symbol_c *fbname; lbessard@70: const char *variable_prefix; lbessard@70: lbessard@70: public: laurent@217: generate_c_SFC_IL_ST_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL); msousa@267: msousa@267: /********************/ msousa@267: /* 2.1.6 - Pragmas */ msousa@267: /********************/ msousa@267: void *visit(enable_code_generation_pragma_c * symbol) {s4o_ptr->enable_output(); return NULL;} msousa@267: void *visit(disable_code_generation_pragma_c * symbol) {s4o_ptr->disable_output(); return NULL;} msousa@267: lbessard@70: /*********************************************/ lbessard@70: /* B.1.6 Sequential function chart elements */ 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: laurent@217: generate_c_SFC_IL_ST_c::generate_c_SFC_IL_ST_c(stage4out_c *s4o_ptr, symbol_c *name, 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; laurent@217: this->fbname = name; 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) { laurent@217: generate_c_sfc_c generate_c_sfc(s4o_ptr, fbname, 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) { laurent@217: generate_c_il_c generate_c_il(s4o_ptr, fbname, 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) { laurent@217: generate_c_st_c generate_c_st(s4o_ptr, fbname, 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 { msousa@267: private: msousa@267: stage4out_c *s4o_ptr; msousa@267: lbessard@70: public: lbessard@121: generate_c_pous_c(stage4out_c *s4o_ptr, stage4out_c *s4o_incl_ptr) msousa@267: : generate_c_typedecl_c(s4o_ptr, s4o_incl_ptr) { msousa@267: generate_c_pous_c::s4o_ptr = s4o_ptr; msousa@267: }; lbessard@70: virtual ~generate_c_pous_c(void) {} lbessard@70: lbessard@70: lbessard@70: public: msousa@267: /********************/ msousa@267: /* 2.1.6 - Pragmas */ msousa@267: /********************/ msousa@267: void *visit(enable_code_generation_pragma_c * symbol) {s4o_ptr->enable_output(); return NULL;} msousa@267: void *visit(disable_code_generation_pragma_c * symbol) {s4o_ptr->disable_output(); return NULL;} 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: /* (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, ccb@202: generate_c_vardecl_c::finterface_vf, ccb@202: generate_c_vardecl_c::input_vt | ccb@202: generate_c_vardecl_c::output_vt | ccb@202: generate_c_vardecl_c::inoutput_vt | ccb@202: generate_c_vardecl_c::en_vt | ccb@202: generate_c_vardecl_c::eno_vt); lbessard@146: vardecl->print(symbol->var_declarations_list); lbessard@146: delete vardecl; lbessard@146: lbessard@146: s4o.indent_left(); lbessard@146: lbessard@146: s4o.print(")\n" + s4o.indent_spaces + "{\n"); lbessard@146: lbessard@146: /* (B) Function local variable declaration */ lbessard@146: /* (B.1) Variables declared in ST source code */ lbessard@146: s4o.indent_right(); lbessard@146: ccb@202: vardecl = new generate_c_vardecl_c(&s4o, ccb@202: generate_c_vardecl_c::localinit_vf, ccb@202: generate_c_vardecl_c::output_vt | lbessard@146: generate_c_vardecl_c::inoutput_vt | ccb@202: generate_c_vardecl_c::private_vt | ccb@202: generate_c_vardecl_c::eno_vt); lbessard@70: vardecl->print(symbol->var_declarations_list); lbessard@70: delete vardecl; lbessard@146: lbessard@146: /* (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@146: lbessard@146: s4o.print(s4o.indent_spaces + "// Control execution\n"); lbessard@146: s4o.print(s4o.indent_spaces + "if (!EN) {\n"); lbessard@146: s4o.indent_right(); ccb@202: s4o.print(s4o.indent_spaces + "if (__ENO != NULL) {\n"); lbessard@146: s4o.indent_right(); ccb@202: s4o.print(s4o.indent_spaces + "*__ENO = __BOOL_LITERAL(FALSE);\n"); lbessard@146: s4o.indent_left(); lbessard@146: s4o.print(s4o.indent_spaces + "}\n"); lbessard@146: s4o.print(s4o.indent_spaces + "return "); lbessard@146: symbol->derived_function_name->accept(*this); lbessard@146: s4o.print(";\n"); lbessard@146: s4o.indent_left(); lbessard@146: s4o.print(s4o.indent_spaces + "}\n"); ccb@202: lbessard@70: /* (C) Function body */ laurent@217: generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol->derived_function_name, symbol); lbessard@70: symbol->function_body->accept(generate_c_code); lbessard@145: lbessard@145: vardecl = new generate_c_vardecl_c(&s4o, lbessard@145: generate_c_vardecl_c::foutputassign_vf, ccb@202: generate_c_vardecl_c::output_vt | ccb@202: generate_c_vardecl_c::inoutput_vt | ccb@202: generate_c_vardecl_c::eno_vt); lbessard@145: vardecl->print(symbol->var_declarations_list); lbessard@145: delete vardecl; lbessard@145: 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@121: generate_c_typedecl_c *typedecl; laurent@234: generate_c_inlinefcall_c *inlinedecl; lbessard@70: TRACE("function_block_declaration_c"); lbessard@70: lbessard@70: /* (A) Function Block data structure declaration... */ lbessard@121: typedecl = new generate_c_typedecl_c(&s4o_incl); lbessard@70: /* (A.1) Data structure declaration */ lbessard@121: s4o_incl.print("// FUNCTION_BLOCK "); lbessard@121: symbol->fblock_name->accept(*typedecl); lbessard@121: s4o_incl.print("\n// Data part\n"); lbessard@121: s4o_incl.print("typedef struct {\n"); lbessard@121: s4o_incl.indent_right(); lbessard@70: /* (A.2) Public variables: i.e. the function parameters... */ lbessard@121: s4o_incl.print(s4o_incl.indent_spaces + "// FB Interface - IN, OUT, IN_OUT variables\n"); lbessard@121: vardecl = new generate_c_vardecl_c(&s4o_incl, ccb@202: generate_c_vardecl_c::local_vf, ccb@202: generate_c_vardecl_c::input_vt | ccb@202: generate_c_vardecl_c::output_vt | ccb@202: generate_c_vardecl_c::inoutput_vt | ccb@202: generate_c_vardecl_c::en_vt | ccb@202: generate_c_vardecl_c::eno_vt); lbessard@70: vardecl->print(symbol->var_declarations); lbessard@70: delete vardecl; lbessard@121: s4o_incl.print("\n"); lbessard@70: /* (A.3) Private internal variables */ lbessard@121: s4o_incl.print(s4o_incl.indent_spaces + "// FB private variables - TEMP, private and located variables\n"); lbessard@121: vardecl = new generate_c_vardecl_c(&s4o_incl, ccb@202: generate_c_vardecl_c::local_vf, ccb@202: generate_c_vardecl_c::temp_vt | ccb@202: generate_c_vardecl_c::private_vt | ccb@202: generate_c_vardecl_c::located_vt | ccb@202: generate_c_vardecl_c::external_vt); lbessard@70: vardecl->print(symbol->var_declarations); lbessard@70: delete vardecl; lbessard@146: lbessard@70: /* (A.4) Generate private internal variables for SFC */ laurent@244: sfcdecl = new generate_c_sfcdecl_c(&s4o_incl, symbol); laurent@244: sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::sfcdecl_sd); lbessard@70: delete sfcdecl; lbessard@121: s4o_incl.print("\n"); laurent@244: lbessard@70: /* (A.5) Function Block data structure type name. */ lbessard@121: s4o_incl.indent_left(); lbessard@121: s4o_incl.print("} "); lbessard@121: symbol->fblock_name->accept(*typedecl); lbessard@121: s4o_incl.print(";\n\n"); lbessard@121: delete typedecl; lbessard@70: laurent@217: /* (A.6) Function Block inline function declaration for function invocation */ laurent@234: inlinedecl = new generate_c_inlinefcall_c(&s4o, symbol->fblock_name, symbol, FB_FUNCTION_PARAM"->"); laurent@234: inlinedecl->print(symbol->fblock_body); laurent@234: delete inlinedecl; laurent@217: 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); laurent@221: s4o.print(", BOOL retain) {\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, ccb@202: generate_c_vardecl_c::constructorinit_vf, ccb@202: generate_c_vardecl_c::input_vt | ccb@202: generate_c_vardecl_c::output_vt | ccb@202: generate_c_vardecl_c::inoutput_vt | ccb@202: generate_c_vardecl_c::private_vt | ccb@202: generate_c_vardecl_c::located_vt | ccb@202: generate_c_vardecl_c::external_vt | ccb@202: generate_c_vardecl_c::en_vt | ccb@202: generate_c_vardecl_c::eno_vt); lbessard@70: vardecl->print(symbol->var_declarations, NULL, FB_FUNCTION_PARAM"->"); lbessard@70: delete vardecl; lbessard@70: s4o.print("\n"); laurent@244: laurent@244: sfcdecl = new generate_c_sfcdecl_c(&s4o, symbol, FB_FUNCTION_PARAM"->"); laurent@244: lbessard@70: /* (B.3) Generate private internal variables for SFC */ laurent@244: sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::sfcinit_sd); laurent@244: 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 */ laurent@244: sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::stepdef_sd); lbessard@70: lbessard@70: /* (C.2) Action definitions */ laurent@244: sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::actiondef_sd); laurent@244: 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@146: s4o.print(s4o.indent_spaces + "// Control execution\n"); lbessard@146: s4o.print(s4o.indent_spaces + "if (!"); laurent@217: s4o.print(GET_VAR); laurent@217: s4o.print("("); lbessard@146: s4o.print(FB_FUNCTION_PARAM); laurent@217: s4o.print("->EN)) {\n"); lbessard@146: s4o.indent_right(); lbessard@146: s4o.print(s4o.indent_spaces); laurent@217: s4o.print(SET_VAR); laurent@217: s4o.print("("); lbessard@146: s4o.print(FB_FUNCTION_PARAM); laurent@217: s4o.print("->ENO,__BOOL_LITERAL(FALSE));\n"); lbessard@146: s4o.print(s4o.indent_spaces + "return;\n"); lbessard@146: s4o.indent_left(); lbessard@146: s4o.print(s4o.indent_spaces + "}\n"); lbessard@146: s4o.print(s4o.indent_spaces + "else {\n"); lbessard@146: s4o.indent_right(); lbessard@146: s4o.print(s4o.indent_spaces); laurent@217: s4o.print(SET_VAR); laurent@217: s4o.print("("); lbessard@146: s4o.print(FB_FUNCTION_PARAM); laurent@217: s4o.print("->ENO,__BOOL_LITERAL(TRUE));\n"); lbessard@146: s4o.indent_left(); lbessard@146: s4o.print(s4o.indent_spaces + "}\n"); lbessard@146: 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, ccb@202: generate_c_vardecl_c::init_vf, ccb@202: 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 */ laurent@217: generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol->fblock_name, 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 */ laurent@244: sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::stepundef_sd); laurent@244: lbessard@70: /* (C.7) Action undefinitions */ laurent@244: sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::actionundef_sd); laurent@244: 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@121: generate_c_typedecl_c *typedecl; laurent@234: generate_c_inlinefcall_c *inlinedecl; lbessard@70: TRACE("program_declaration_c"); lbessard@70: lbessard@70: /* (A) Program data structure declaration... */ lbessard@121: typedecl = new generate_c_typedecl_c(&s4o_incl); lbessard@70: /* (A.1) Data structure declaration */ lbessard@121: s4o_incl.print("// PROGRAM "); lbessard@121: symbol->program_type_name->accept(*typedecl); lbessard@121: s4o_incl.print("\n// Data part\n"); lbessard@121: s4o_incl.print("typedef struct {\n"); lbessard@121: s4o_incl.indent_right(); lbessard@70: lbessard@70: /* (A.2) Public variables: i.e. the program parameters... */ lbessard@121: s4o_incl.print(s4o_incl.indent_spaces + "// PROGRAM Interface - IN, OUT, IN_OUT variables\n"); lbessard@121: vardecl = new generate_c_vardecl_c(&s4o_incl, ccb@202: generate_c_vardecl_c::local_vf, ccb@202: generate_c_vardecl_c::input_vt | ccb@202: generate_c_vardecl_c::output_vt | ccb@202: generate_c_vardecl_c::inoutput_vt); lbessard@70: vardecl->print(symbol->var_declarations); lbessard@70: delete vardecl; lbessard@121: s4o_incl.print("\n"); lbessard@70: /* (A.3) Private internal variables */ lbessard@121: s4o_incl.print(s4o_incl.indent_spaces + "// PROGRAM private variables - TEMP, private and located variables\n"); lbessard@121: vardecl = new generate_c_vardecl_c(&s4o_incl, lbessard@70: generate_c_vardecl_c::local_vf, ccb@202: 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; laurent@244: lbessard@70: /* (A.4) Generate private internal variables for SFC */ laurent@244: sfcdecl = new generate_c_sfcdecl_c(&s4o_incl, symbol); laurent@244: sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::sfcdecl_sd); lbessard@70: delete sfcdecl; laurent@244: s4o_incl.print("\n"); lbessard@70: lbessard@70: /* (A.5) Program data structure type name. */ lbessard@121: s4o_incl.indent_left(); lbessard@121: s4o_incl.print("} "); lbessard@121: symbol->program_type_name->accept(*typedecl); lbessard@121: s4o_incl.print(";\n\n"); lbessard@121: delete typedecl; lbessard@70: laurent@217: /* (A.6) Function Block inline function declaration for function invocation */ laurent@234: inlinedecl = new generate_c_inlinefcall_c(&s4o, symbol->program_type_name, symbol, FB_FUNCTION_PARAM"->"); laurent@234: inlinedecl->print(symbol->function_block_body); laurent@234: delete inlinedecl; laurent@217: 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); laurent@221: s4o.print(", BOOL retain) {\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, ccb@202: generate_c_vardecl_c::constructorinit_vf, ccb@202: generate_c_vardecl_c::input_vt | ccb@202: generate_c_vardecl_c::output_vt | ccb@202: generate_c_vardecl_c::inoutput_vt | ccb@202: generate_c_vardecl_c::private_vt | ccb@202: generate_c_vardecl_c::located_vt | ccb@202: 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"); laurent@244: laurent@244: sfcdecl = new generate_c_sfcdecl_c(&s4o, symbol, FB_FUNCTION_PARAM"->"); laurent@244: lbessard@70: /* (B.3) Generate private internal variables for SFC */ laurent@244: sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::sfcinit_sd); 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 */ laurent@244: sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::stepdef_sd); lbessard@70: lbessard@70: /* (C.2) Action definitions */ laurent@244: sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::actiondef_sd); 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, ccb@202: generate_c_vardecl_c::init_vf, ccb@202: 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 */ laurent@217: generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol->program_type_name, 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 */ laurent@244: sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::stepundef_sd); laurent@244: laurent@244: /* (C.7) Action undefinitions */ laurent@244: sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::actionundef_sd); laurent@244: lbessard@70: delete sfcdecl; laurent@244: 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 { msousa@267: private: msousa@267: stage4out_c *s4o_ptr; lbessard@70: lbessard@70: public: lbessard@70: generate_c_config_c(stage4out_c *s4o_ptr) msousa@267: : generate_c_typedecl_c(s4o_ptr) { msousa@267: generate_c_config_c::s4o_ptr = s4o_ptr; msousa@267: }; msousa@267: 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: msousa@267: msousa@267: public: msousa@267: /********************/ msousa@267: /* 2.1.6 - Pragmas */ msousa@267: /********************/ msousa@267: void *visit(enable_code_generation_pragma_c * symbol) {s4o_ptr->enable_output(); return NULL;} msousa@267: void *visit(disable_code_generation_pragma_c * symbol) {s4o_ptr->disable_output(); return NULL;} msousa@267: msousa@267: lbessard@70: /********************************/ lbessard@70: /* B 1.7 Configuration elements */ lbessard@70: /********************************/ 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"); msousa@257: s4o.print("#include \"accessor.h\"\n\n"); laurent@250: 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, ccb@202: generate_c_vardecl_c::local_vf, ccb@202: 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(); laurent@250: s4o.print(s4o.indent_spaces); laurent@255: s4o.print("BOOL retain;\n"); laurent@255: s4o.print(s4o.indent_spaces); laurent@255: s4o.print("retain = 0;\n"); 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, ccb@202: generate_c_vardecl_c::constructorinit_vf, ccb@202: 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); laurent@210: s4o.print("(unsigned long 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); laurent@210: s4o.print("(unsigned long 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); laurent@210: s4o.print("(unsigned long 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@184: symbol_c *current_task_name; lbessard@70: symbol_c *current_global_vars; msousa@267: stage4out_c *s4o_ptr; 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) { ccb@202: 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@184: current_task_name = NULL; lbessard@70: current_global_vars = NULL; msousa@267: generate_c_resources_c::s4o_ptr = s4o_ptr; lbessard@70: }; msousa@267: 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: laurent@221: /* the qualifier of variables that need to be processed... */ laurent@221: static const unsigned int none_vq = 0x0000; laurent@221: static const unsigned int constant_vq = 0x0001; // CONSTANT laurent@221: static const unsigned int retain_vq = 0x0002; // RETAIN laurent@221: static const unsigned int non_retain_vq = 0x0004; // NON_RETAIN laurent@221: laurent@221: /* variable used to store the qualifier of program currently being processed... */ laurent@221: unsigned int current_varqualifier; laurent@221: laurent@221: void *print_retain(void) { laurent@221: s4o.print(","); laurent@221: switch (current_varqualifier) { laurent@221: case retain_vq: laurent@221: s4o.print("1"); laurent@221: break; laurent@221: case non_retain_vq: laurent@221: s4o.print("0"); laurent@221: break; laurent@221: default: laurent@221: s4o.print("retain"); laurent@221: break; laurent@221: } laurent@221: return NULL; laurent@221: } laurent@221: msousa@267: msousa@267: /********************/ msousa@267: /* 2.1.6 - Pragmas */ msousa@267: /********************/ msousa@267: void *visit(enable_code_generation_pragma_c * symbol) {s4o_ptr->enable_output(); return NULL;} msousa@267: void *visit(disable_code_generation_pragma_c * symbol) {s4o_ptr->disable_output(); return NULL;} msousa@267: msousa@267: laurent@221: /******************************************/ laurent@221: /* B 1.4.3 - Declaration & Initialisation */ laurent@221: /******************************************/ laurent@221: laurent@221: void *visit(constant_option_c *symbol) { laurent@221: current_varqualifier = constant_vq; laurent@221: return NULL; laurent@221: } laurent@221: laurent@221: void *visit(retain_option_c *symbol) { laurent@221: current_varqualifier = retain_vq; laurent@221: return NULL; laurent@221: } laurent@221: laurent@221: void *visit(non_retain_option_c *symbol) { laurent@221: current_varqualifier = non_retain_vq; laurent@221: return NULL; laurent@221: } laurent@221: 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) { ccb@202: 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@120: s4o.print("\n\n"); lbessard@120: laurent@237: s4o.print("extern unsigned long long common_ticktime__;\n\n"); laurent@217: laurent@217: s4o.print("#include \"accessor.h\"\n\n"); laurent@217: lbessard@70: /* (A.2) Global variables... */ lbessard@70: if (current_global_vars != NULL) { lbessard@70: vardecl = new generate_c_vardecl_c(&s4o, ccb@202: generate_c_vardecl_c::local_vf, ccb@202: generate_c_vardecl_c::global_vt, ccb@202: current_resource_name); lbessard@70: vardecl->print(current_global_vars); lbessard@70: delete vardecl; lbessard@120: s4o.print("\n"); lbessard@70: } lbessard@70: lbessard@70: /* (A.3) POUs inclusion */ lbessard@121: s4o.print("#include \"POUS.h\"\n\n"); lbessard@70: s4o.print("#include \"POUS.c\"\n\n"); lbessard@70: lbessard@184: wanted_declaretype = declare_dt; lbessard@184: lbessard@70: /* (A.4) Resource programs declaration... */ lbessard@184: symbol->task_configuration_list->accept(*this); lbessard@184: lbessard@184: /* (A.5) Resource programs declaration... */ lbessard@70: symbol->program_configuration_list->accept(*this); lbessard@184: 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(); laurent@221: s4o.print(s4o.indent_spaces); laurent@255: s4o.print("BOOL retain;\n"); laurent@255: s4o.print(s4o.indent_spaces); laurent@255: s4o.print("retain = 0;\n"); 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, ccb@202: generate_c_vardecl_c::constructorinit_vf, laurent@241: generate_c_vardecl_c::global_vt, laurent@241: current_resource_name); lbessard@70: vardecl->print(current_global_vars); lbessard@70: delete vardecl; lbessard@70: } lbessard@70: s4o.print("\n"); lbessard@70: lbessard@70: wanted_declaretype = init_dt; lbessard@184: lbessard@184: /* (B.3) Tasks initialisations... */ lbessard@184: symbol->task_configuration_list->accept(*this); lbessard@184: lbessard@184: /* (B.4) Resource programs initialisations... */ 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); laurent@210: s4o.print("(unsigned long tick) {\n"); lbessard@70: s4o.indent_right(); lbessard@70: lbessard@184: wanted_declaretype = run_dt; lbessard@184: 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: 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@184: switch (wanted_declaretype) { lbessard@184: case declare_dt: lbessard@70: s4o.print(s4o.indent_spaces); lbessard@184: symbol->program_type_name->accept(*this); lbessard@184: s4o.print(" "); lbessard@184: current_resource_name->accept(*this); lbessard@184: s4o.print("__"); lbessard@184: symbol->program_name->accept(*this); lbessard@184: s4o.print(";\n#define "); lbessard@184: symbol->program_name->accept(*this); lbessard@184: s4o.print(" "); lbessard@184: current_resource_name->accept(*this); lbessard@184: s4o.print("__"); lbessard@184: symbol->program_name->accept(*this); lbessard@184: s4o.print("\n"); lbessard@184: break; lbessard@184: case init_dt: laurent@221: if (symbol->retain_option != NULL) laurent@221: symbol->retain_option->accept(*this); lbessard@184: s4o.print(s4o.indent_spaces); lbessard@184: symbol->program_type_name->accept(*this); lbessard@184: s4o.print(FB_INIT_SUFFIX); lbessard@184: s4o.print("(&"); lbessard@184: symbol->program_name->accept(*this); laurent@221: print_retain(); lbessard@184: s4o.print(");\n"); lbessard@184: break; lbessard@184: case run_dt: lbessard@184: current_program_name = ((identifier_c*)(symbol->program_name))->value; lbessard@184: if (symbol->task_name != NULL) { lbessard@184: s4o.print(s4o.indent_spaces); lbessard@184: s4o.print("if ("); lbessard@184: symbol->task_name->accept(*this); lbessard@184: s4o.print(") {\n"); lbessard@184: s4o.indent_right(); lbessard@184: } lbessard@70: lbessard@184: wanted_assigntype = assign_at; lbessard@184: if (symbol->prog_conf_elements != NULL) lbessard@184: symbol->prog_conf_elements->accept(*this); lbessard@184: lbessard@184: s4o.print(s4o.indent_spaces); lbessard@184: symbol->program_type_name->accept(*this); lbessard@184: s4o.print(FB_FUNCTION_SUFFIX); lbessard@184: s4o.print("(&"); lbessard@184: symbol->program_name->accept(*this); lbessard@184: s4o.print(");\n"); lbessard@184: lbessard@184: wanted_assigntype = send_at; lbessard@184: if (symbol->prog_conf_elements != NULL) lbessard@184: symbol->prog_conf_elements->accept(*this); lbessard@184: lbessard@184: if (symbol->task_name != NULL) { lbessard@184: s4o.indent_left(); lbessard@184: s4o.print(s4o.indent_spaces + "}\n"); lbessard@184: } lbessard@184: break; lbessard@184: default: lbessard@184: break; 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@184: current_task_name = symbol->task_name; lbessard@184: switch (wanted_declaretype) { lbessard@184: case declare_dt: lbessard@184: s4o.print(s4o.indent_spaces + "BOOL "); lbessard@184: current_task_name->accept(*this); lbessard@184: s4o.print(";\n"); lbessard@184: symbol->task_initialization->accept(*this); lbessard@184: break; lbessard@184: case init_dt: lbessard@184: s4o.print(s4o.indent_spaces); lbessard@184: current_task_name->accept(*this); lbessard@184: s4o.print(" = __BOOL_LITERAL(FALSE);\n"); lbessard@184: symbol->task_initialization->accept(*this); lbessard@184: break; lbessard@184: case run_dt: lbessard@184: symbol->task_initialization->accept(*this); lbessard@184: break; lbessard@184: default: lbessard@184: break; lbessard@184: } lbessard@184: current_task_name = NULL; 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@184: switch (wanted_declaretype) { lbessard@184: case declare_dt: lbessard@184: if (symbol->single_data_source != NULL) { lbessard@184: s4o.print(s4o.indent_spaces + "R_TRIG "); lbessard@184: current_task_name->accept(*this); lbessard@184: s4o.print("_R_TRIG;\n"); lbessard@184: } lbessard@184: break; lbessard@184: case init_dt: lbessard@184: if (symbol->single_data_source != NULL) { lbessard@184: s4o.print(s4o.indent_spaces + "R_TRIG"); lbessard@184: s4o.print(FB_INIT_SUFFIX); lbessard@184: s4o.print("(&"); lbessard@184: current_task_name->accept(*this); laurent@248: s4o.print("_R_TRIG, retain);\n"); lbessard@184: } lbessard@184: break; lbessard@184: case run_dt: lbessard@184: if (symbol->single_data_source != NULL) { lbessard@184: symbol_c *config_var_decl = NULL; lbessard@184: symbol_c *res_var_decl = NULL; laurent@248: unsigned int vartype; lbessard@184: symbol_c *current_var_reference = ((global_var_reference_c *)(symbol->single_data_source))->global_var_name; lbessard@184: res_var_decl = search_resource_instance->get_decl(current_var_reference); lbessard@184: if (res_var_decl == NULL) { lbessard@184: config_var_decl = search_config_instance->get_decl(current_var_reference); lbessard@184: if (config_var_decl == NULL) lbessard@184: ERROR; laurent@248: vartype = search_config_instance->get_vartype(); lbessard@184: s4o.print(s4o.indent_spaces + "{extern "); lbessard@184: config_var_decl->accept(*this); lbessard@184: s4o.print(" *"); lbessard@184: symbol->single_data_source->accept(*this); lbessard@184: s4o.print("; "); lbessard@184: } laurent@248: else { laurent@248: vartype = search_resource_instance->get_vartype(); lbessard@184: s4o.print(s4o.indent_spaces); laurent@248: } laurent@248: s4o.print(SET_VAR); laurent@248: s4o.print("("); lbessard@184: current_task_name->accept(*this); laurent@248: s4o.print("_R_TRIG.CLK, *__GET_GLOBAL_"); lbessard@184: symbol->single_data_source->accept(*this); laurent@248: s4o.print("());"); lbessard@184: if (config_var_decl != NULL) lbessard@184: s4o.print("}"); lbessard@184: s4o.print("\n"); lbessard@184: s4o.print(s4o.indent_spaces + "R_TRIG"); lbessard@184: s4o.print(FB_FUNCTION_SUFFIX); lbessard@184: s4o.print("(&"); lbessard@184: current_task_name->accept(*this); lbessard@184: s4o.print("_R_TRIG);\n"); lbessard@184: s4o.print(s4o.indent_spaces); lbessard@184: current_task_name->accept(*this); lbessard@184: s4o.print(" = "); laurent@248: s4o.print(GET_VAR); laurent@248: s4o.print("("); lbessard@184: current_task_name->accept(*this); laurent@248: s4o.print("_R_TRIG.Q)"); lbessard@184: } lbessard@184: else { lbessard@184: s4o.print(s4o.indent_spaces); lbessard@184: current_task_name->accept(*this); lbessard@184: s4o.print(" = "); lbessard@184: if (symbol->interval_data_source != NULL) { lbessard@184: calculate_time_c calculate_time; lbessard@184: symbol->interval_data_source->accept(calculate_time); lbessard@184: unsigned long time = calculate_time.get_time(); lbessard@184: if (time != 0) { lbessard@184: s4o.print("!(tick % "); lbessard@184: s4o.print_integer((int)(time / common_ticktime)); lbessard@184: s4o.print(")"); lbessard@184: } lbessard@184: else lbessard@184: s4o.print("1"); lbessard@184: } lbessard@184: else lbessard@184: s4o.print("1"); lbessard@184: } lbessard@184: s4o.print(";\n"); lbessard@184: break; lbessard@184: default: lbessard@184: break; lbessard@70: } 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); ccb@202: s4o.print(" *"); lbessard@70: symbol->prog_data_source->accept(*this); lbessard@70: s4o.print("; "); ccb@202: s4o.printupper(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 + "{extern "); lbessard@70: var_decl->accept(*this); ccb@202: 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(" = "); ccb@202: s4o.printupper(current_program_name); lbessard@70: s4o.print("."); lbessard@70: symbol->symbolic_variable->accept(*this); ccb@202: 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@121: stage4out_c pous_incl_s4o; lbessard@70: stage4out_c located_variables_s4o; lbessard@111: stage4out_c variables_s4o; lbessard@70: generate_c_pous_c generate_c_pous; lbessard@111: lbessard@70: symbol_c *current_configuration; lbessard@70: lbessard@70: const char *current_name; lbessard@70: const char *current_builddir; lbessard@70: laurent@237: unsigned long 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@121: pous_incl_s4o(builddir, "POUS", "h"), lbessard@70: located_variables_s4o(builddir, "LOCATED_VARIABLES","h"), etisserant@112: variables_s4o(builddir, "VARIABLES","csv"), lbessard@121: generate_c_pous(&pous_s4o, &pous_incl_s4o) { lbessard@70: current_builddir = builddir; lbessard@70: current_configuration = NULL; lbessard@70: } lbessard@70: lbessard@70: ~generate_c_c(void) {} lbessard@70: msousa@267: msousa@267: msousa@267: /********************/ msousa@267: /* 2.1.6 - Pragmas */ msousa@267: /********************/ msousa@267: void *visit(enable_code_generation_pragma_c * symbol) { msousa@267: s4o .enable_output(); msousa@267: pous_s4o .enable_output(); msousa@267: pous_incl_s4o .enable_output(); msousa@267: located_variables_s4o.enable_output(); msousa@267: variables_s4o .enable_output(); msousa@267: return NULL; msousa@267: } msousa@267: msousa@267: void *visit(disable_code_generation_pragma_c * symbol) { msousa@267: s4o .disable_output(); msousa@267: pous_s4o .disable_output(); msousa@267: pous_incl_s4o .disable_output(); msousa@267: located_variables_s4o.disable_output(); msousa@267: variables_s4o .disable_output(); msousa@267: return NULL; msousa@267: } msousa@267: msousa@267: lbessard@70: /***************************/ lbessard@70: /* B 0 - Programming Model */ lbessard@70: /***************************/ lbessard@70: void *visit(library_c *symbol) { laurent@217: pous_incl_s4o.print("#ifndef __POUS_H\n#define __POUS_H\n\n#include \"accessor.h\"\n\n"); lbessard@70: for(int i = 0; i < symbol->n; i++) { lbessard@70: symbol->elements[i]->accept(*this); lbessard@70: } lbessard@121: pous_incl_s4o.print("#endif //__POUS_H\n"); lbessard@121: lbessard@121: generate_var_list_c generate_var_list(&variables_s4o, symbol); lbessard@121: generate_var_list.generate_programs(symbol); lbessard@121: generate_var_list.generate_variables(symbol); lbessard@111: 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); laurent@210: common_ticktime = calculate_common_ticktime.get_common_ticktime(); laurent@248: if (common_ticktime == 0) { laurent@248: fprintf(stderr, "\nYou must at least define a periodic task to set cycle period!"); laurent@248: ERROR; laurent@248: } 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: laurent@210: config_s4o.print("unsigned long long common_ticktime__ = "); laurent@210: config_s4o.print_long_long_integer(common_ticktime); laurent@210: config_s4o.print("; /*ns*/\n"); laurent@210: config_s4o.print("unsigned long greatest_tick_count__ = "); laurent@210: config_s4o.print_long_integer(calculate_common_ticktime.get_greatest_tick_count()); laurent@210: config_s4o.print("; /*tick*/\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) { ccb@202: symbol->resource_name->accept(*this); ccb@202: 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); ccb@202: symbol->accept(generate_c_resources); ccb@202: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(single_resource_declaration_c *symbol) { ccb@202: stage4out_c resources_s4o(current_builddir, "RESOURCE", "c"); lbessard@70: generate_c_resources_c generate_c_resources(&resources_s4o, current_configuration, symbol, common_ticktime); ccb@202: symbol->accept(generate_c_resources); ccb@202: 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: