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 laurent@377: #include laurent@377: #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: /* 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: msousa@283: /* please see the comment before the RET_operator_c visitor for details... */ msousa@283: #define END_LABEL VAR_LEADER "end" msousa@283: msousa@283: 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: msousa@350: msousa@350: #include "generate_c.hh" msousa@350: msousa@350: msousa@350: /***********************************************************************/ msousa@350: /***********************************************************************/ msousa@350: /***********************************************************************/ msousa@350: /***********************************************************************/ msousa@350: msousa@350: /* A helper class that prints out the identifiers for function calls to overloaded functions */ msousa@350: /* Given a function declaration of the function being called, it msousa@350: * will simply print out the returned data type, msousa@350: * followed by the data types of all input, output, and in_out parameters. msousa@350: * for e.g.; msousa@350: * SIN( REAL) : REAL -> prints out -> REAL__REAL msousa@350: * LEN( STRING) : INT -> prints out -> INT__STRING msousa@350: * MUL(TIME, INT) : TIME -> prints out -> TIME__TIME__INT msousa@350: */ msousa@350: class print_function_parameter_data_types_c: public generate_c_base_c { msousa@350: private: msousa@350: symbol_c *current_type; msousa@350: bool_type_name_c tmp_bool; msousa@350: msousa@350: void print_list(symbol_c *var_list, symbol_c *data_type) { msousa@350: if (data_type != NULL) { msousa@350: /* print out the data type once for every variable! */ msousa@350: list_c *list = dynamic_cast(var_list); msousa@350: if (list == NULL) ERROR; msousa@350: for (int i=0; i < list->n; i++) { msousa@350: s4o.print("__"); msousa@350: data_type->accept(*this); msousa@350: } msousa@350: } msousa@350: } msousa@350: msousa@350: public: msousa@350: print_function_parameter_data_types_c(stage4out_c *s4o_ptr): msousa@350: generate_c_base_c(s4o_ptr) msousa@350: {current_type = NULL;} msousa@350: msousa@350: /**************************************/ msousa@350: /* B.1.5 - Program organization units */ msousa@350: /**************************************/ msousa@350: /***********************/ msousa@350: /* B 1.5.1 - Functions */ msousa@350: /***********************/ msousa@350: /* FUNCTION derived_function_name ':' elementary_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */ msousa@350: /* | FUNCTION derived_function_name ':' derived_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */ msousa@350: void *visit(function_declaration_c *symbol) { msousa@350: symbol->type_name->accept(*this); /* return type */ msousa@350: symbol->var_declarations_list->accept(*this); msousa@350: return NULL; msousa@350: } msousa@350: msousa@350: /* already handled by iterator base class (note that generate_c_base_c inherits from iterator_c) */ msousa@350: //void *visit(var_declarations_list_c *symbol) {// iterate through list} msousa@350: msousa@350: /* already handled by iterator base class (note that generate_c_base_c inherits from iterator_c) */ msousa@350: //void *visit(input_declarations_c *symbol) {// iterate through list} msousa@350: msousa@350: /* already handled by iterator base class (note that generate_c_base_c inherits from iterator_c) */ msousa@350: //void *visit(input_declaration_list_c *symbol) {// iterate through list} msousa@350: msousa@350: void *visit(edge_declaration_c *symbol) { msousa@350: current_type = &tmp_bool; msousa@350: symbol->var1_list->accept(*this); msousa@350: current_type = NULL; msousa@350: return NULL; msousa@350: } msousa@350: msousa@350: /* We do NOT print out EN and ENO parameters! */ msousa@350: void *visit(en_param_declaration_c *symbol) {return NULL;} msousa@350: msousa@350: /* already handled by iterator base class (note that generate_c_base_c inherits from iterator_c) */ msousa@350: //void *visit(output_declarations_c *symbol) {// iterate through list} msousa@350: msousa@350: /* already handled by iterator base class (note that generate_c_base_c inherits from iterator_c) */ msousa@350: //void *visit(var_init_decl_list_c *symbol) {// iterate through list} msousa@350: msousa@350: void *visit(simple_spec_init_c *symbol) { msousa@350: /* return the data type */ msousa@350: return symbol->simple_specification; msousa@350: } msousa@350: msousa@350: /* currently we do not support data types defined in the declaration itself */ msousa@350: /* For now, sugest the user define a TYPE .. END_TYPE */ msousa@350: /* NOTE: although this class may also sometimes point to a previously_declared_subrange_type_name msousa@350: * we don't need this for now, so it is easier to just skip it allocation msousa@350: */ msousa@350: void *visit(subrange_spec_init_c *symbol) {return NULL;} msousa@350: msousa@350: /* currently we do not support data types defined in the declaration itself */ msousa@350: /* For now, sugest the user define a TYPE .. END_TYPE */ msousa@350: /* NOTE: although this class may also sometimes point to a previously_declared_enumerated_type_name msousa@350: * we don't need this for now, so it is easier to just skip it allocation msousa@350: */ msousa@350: void *visit(enumerated_spec_init_c *symbol) {return NULL;} msousa@350: msousa@350: /* currently we do not support data types defined in the declaration itself */ msousa@350: /* For now, sugest the user define a TYPE .. END_TYPE */ msousa@350: /* NOTE: although this class may also sometimes point to a previously_declared_array_type_name msousa@350: * we don't need this for now, so it is easier to just skip it allocation msousa@350: */ msousa@350: void *visit(array_var_init_decl_c *symbol) {return NULL;} msousa@350: msousa@350: /* currently we do not support data types defined in the declaration itself */ msousa@350: /* For now, sugest the user define a TYPE .. END_TYPE */ msousa@350: /* NOTE: although this class may also sometimes point to a previously_declared_structured_type_name msousa@350: * we don't need this for now, so it is easier to just skip it allocation msousa@350: */ msousa@350: void *visit(structured_var_init_decl_c *symbol) {return NULL;} msousa@350: msousa@350: /* We do NOT print out EN and ENO parameters! */ msousa@350: void *visit(eno_param_declaration_c *symbol) {return NULL;} msousa@350: msousa@350: /* already handled by iterator base class (note that generate_c_base_c inherits from iterator_c) */ msousa@350: //void *visit(input_output_declarations_c *symbol) {// iterate through list} msousa@350: msousa@350: /* already handled by iterator base class (note that generate_c_base_c inherits from iterator_c) */ msousa@350: //void *visit(var_declaration_list_c *symbol) {iterate through list} msousa@350: msousa@350: void *visit(fb_name_decl_c *symbol) { msousa@350: print_list(symbol->fb_name_list, symbol->function_block_type_name); msousa@350: return NULL; msousa@350: } msousa@350: msousa@350: void *visit(var1_init_decl_c *symbol) { msousa@350: print_list(symbol->var1_list, (symbol_c *)symbol->spec_init->accept(*this)); msousa@350: return NULL; msousa@350: } msousa@350: msousa@350: /* currently we do not support data types defined in the declaration itself */ msousa@350: /* For now, sugest the user define a TYPE .. END_TYPE */ msousa@350: void *visit(array_var_declaration_c *symbol) {return NULL;} msousa@350: msousa@350: void *visit(structured_var_declaration_c *symbol) { msousa@350: current_type = symbol->structure_type_name; msousa@350: symbol->var1_list->accept(*this); msousa@350: current_type = NULL; msousa@350: return NULL; msousa@350: } msousa@350: msousa@350: /* currently we do not support data types defined in the declaration itself */ msousa@350: /* For now, sugest the user define a TYPE .. END_TYPE */ msousa@350: /* Note that this class is used for fixed length strings... msousa@350: * STRING [ 42 ] msousa@350: */ msousa@350: void *visit(single_byte_string_var_declaration_c *symbol) {return NULL;} msousa@350: msousa@350: /* currently we do not support data types defined in the declaration itself */ msousa@350: /* For now, sugest the user define a TYPE .. END_TYPE */ msousa@350: /* Note that this class is used for fixed length strings... msousa@350: * WSTRING [ 42 ] msousa@350: */ msousa@350: void *visit(double_byte_string_var_declaration_c *symbol) {return NULL;} msousa@350: }; msousa@350: msousa@350: msousa@350: /***********************************************************************/ msousa@350: /***********************************************************************/ msousa@350: /***********************************************************************/ msousa@350: /***********************************************************************/ msousa@350: msousa@350: 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: /***********************************************************************/ 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: laurent@377: class generate_c_datatypes_c: public generate_c_typedecl_c { laurent@377: public: laurent@377: typedef enum { laurent@377: none_im, laurent@377: arrayname_im, laurent@377: arraydeclaration_im, laurent@377: } inlinearray_mode_t; laurent@377: laurent@377: private: laurent@377: stage4out_c *s4o_ptr; laurent@377: std::map inline_array_defined; laurent@377: std::string current_array_name; laurent@377: inlinearray_mode_t current_mode; laurent@377: laurent@377: public: laurent@377: generate_c_datatypes_c(stage4out_c *s4o_ptr, stage4out_c *s4o_incl_ptr) laurent@377: : generate_c_typedecl_c(s4o_ptr, s4o_incl_ptr) { laurent@377: generate_c_datatypes_c::s4o_ptr = s4o_ptr; laurent@377: current_mode = none_im; laurent@377: }; laurent@377: virtual ~generate_c_datatypes_c(void) { laurent@377: while (!inline_array_defined.empty()) { laurent@377: inline_array_defined.erase(inline_array_defined.begin()); laurent@377: } laurent@377: } laurent@377: laurent@377: /*************************/ laurent@377: /* B.1 - Common elements */ laurent@377: /*************************/ laurent@377: /*******************************************/ laurent@377: /* B 1.1 - Letters, digits and identifiers */ laurent@377: /*******************************************/ laurent@377: void *visit(identifier_c *symbol) { laurent@377: switch (current_mode) { laurent@377: case arrayname_im: laurent@377: current_array_name += symbol->value; laurent@377: break; laurent@377: case arraydeclaration_im: laurent@377: s4o_incl.print(symbol->value); laurent@377: break; laurent@377: default: laurent@377: return generate_c_base_c::visit(symbol); laurent@377: break; laurent@377: } laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /**********************/ laurent@377: /* B.1.3 - Data types */ laurent@377: /**********************/ laurent@377: /***********************************/ laurent@377: /* B 1.3.1 - Elementary Data Types */ laurent@377: /***********************************/ laurent@377: laurent@377: #define HANDLE_ELEMENTARY_DATA_TYPE(datatype_symbol, datatype_name)\ laurent@377: void *visit(datatype_symbol *symbol) {\ laurent@377: switch (current_mode) {\ laurent@377: case arrayname_im:\ laurent@377: current_array_name += datatype_name;\ laurent@377: break;\ laurent@377: case arraydeclaration_im:\ laurent@377: s4o_incl.print(datatype_name);\ laurent@377: break;\ laurent@377: default:\ laurent@377: return generate_c_base_c::visit(symbol);\ laurent@377: break;\ laurent@377: }\ laurent@377: return NULL;\ laurent@377: } laurent@377: laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(time_type_name_c, "TIME") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(bool_type_name_c, "BOOL") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(sint_type_name_c, "SINT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(int_type_name_c, "INT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(dint_type_name_c, "DINT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(lint_type_name_c, "LINT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(usint_type_name_c, "USINT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(uint_type_name_c, "UINT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(udint_type_name_c, "UDINT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(ulint_type_name_c, "ULINT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(real_type_name_c, "REAL") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(lreal_type_name_c, "LREAL") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(date_type_name_c, "DATE") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(tod_type_name_c, "TOD") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(dt_type_name_c, "DT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(byte_type_name_c, "BYTE") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(word_type_name_c, "WORD") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(dword_type_name_c, "DWORD") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(lword_type_name_c, "LWORD") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(string_type_name_c, "STRING") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(wstring_type_name_c, "WSTRING") laurent@377: laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safetime_type_name_c, "TIME") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safebool_type_name_c, "BOOL") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safesint_type_name_c, "SINT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safeint_type_name_c, "INT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safedint_type_name_c, "DINT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safelint_type_name_c, "LINT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safeusint_type_name_c, "USINT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safeuint_type_name_c, "UINT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safeudint_type_name_c, "UDINT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safeulint_type_name_c, "ULINT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safereal_type_name_c, "REAL") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safelreal_type_name_c, "LREAL") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safedate_type_name_c, "DATE") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safetod_type_name_c, "TOD") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safedt_type_name_c, "DT") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safebyte_type_name_c, "BYTE") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safeword_type_name_c, "WORD") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safedword_type_name_c, "DWORD") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safelword_type_name_c, "LWORD") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safestring_type_name_c, "STRING") laurent@377: HANDLE_ELEMENTARY_DATA_TYPE(safewstring_type_name_c, "WSTRING") laurent@377: laurent@377: /******************************************/ laurent@377: /* B 1.4.3 - Declaration & Initialization */ laurent@377: /******************************************/ laurent@377: laurent@377: void *visit(input_declarations_c *symbol) { laurent@377: symbol->input_declaration_list->accept(*this); laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: void *visit(edge_declaration_c *symbol) { laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: void *visit(en_param_declaration_c *symbol) { laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: void *visit(eno_param_declaration_c *symbol) { laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: void *visit(var1_init_decl_c *symbol) { laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /* var1_list ':' array_spec_init */ laurent@377: // SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init) laurent@377: void *visit(array_var_init_decl_c *symbol) { laurent@377: current_mode = arrayname_im; laurent@377: symbol->array_spec_init->accept(*this); laurent@377: current_mode = none_im; laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /* array_specification [ASSIGN array_initialization] */ laurent@377: /* array_initialization may be NULL ! */ laurent@377: void *visit(array_spec_init_c *symbol) { laurent@377: switch (current_mode) { laurent@377: case arrayname_im: laurent@377: { laurent@377: array_specification_c *specification = dynamic_cast(symbol->array_specification); laurent@377: if (specification != NULL) laurent@377: symbol->array_specification->accept(*this); laurent@377: } laurent@377: break; laurent@377: default: laurent@377: return generate_c_typedecl_c::visit(symbol); laurent@377: break; laurent@377: } laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ laurent@377: void *visit(array_specification_c *symbol) { laurent@377: switch (current_mode) { laurent@377: case arrayname_im: laurent@377: { laurent@377: std::map::iterator definition; laurent@377: current_array_name = "__"; laurent@377: symbol->non_generic_type_name->accept(*this); laurent@377: symbol->array_subrange_list->accept(*this); laurent@377: laurent@377: definition = inline_array_defined.find(current_array_name); laurent@377: if (definition == inline_array_defined.end()) { laurent@377: current_mode = arraydeclaration_im; laurent@377: laurent@377: s4o_incl.print("__DECLARE_ARRAY_TYPE("); laurent@377: s4o_incl.print(current_array_name); laurent@377: s4o_incl.print(","); laurent@377: symbol->non_generic_type_name->accept(*this); laurent@377: s4o_incl.print(","); laurent@377: symbol->array_subrange_list->accept(*this); laurent@377: s4o_incl.print(")\n\n"); laurent@377: laurent@377: inline_array_defined[current_array_name] = 0; laurent@377: } laurent@377: } laurent@377: break; laurent@377: default: laurent@377: return generate_c_typedecl_c::visit(symbol); laurent@377: break; laurent@377: } laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /* signed_integer DOTDOT signed_integer */ laurent@377: //SYM_REF2(subrange_c, lower_limit, upper_limit) laurent@377: void *visit(subrange_c *symbol) { laurent@377: int dimension = extract_integer(symbol->upper_limit) - extract_integer(symbol->lower_limit) + 1; laurent@377: switch (current_mode) { laurent@377: case arrayname_im: laurent@377: current_array_name += "_"; laurent@377: { laurent@377: std::stringstream ss; laurent@377: ss << dimension; laurent@377: current_array_name += ss.str(); laurent@377: } laurent@377: break; laurent@377: case arraydeclaration_im: laurent@377: s4o_incl.print("["); laurent@377: s4o_incl.print_integer(dimension); laurent@377: s4o_incl.print("]"); laurent@377: default: laurent@377: generate_c_typedecl_c::visit(symbol); laurent@377: break; laurent@377: } laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /* var1_list ':' initialized_structure */ laurent@377: // SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure) laurent@377: void *visit(structured_var_init_decl_c *symbol) { laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /* fb_name_list ':' function_block_type_name ASSIGN structure_initialization */ laurent@377: /* structure_initialization -> may be NULL ! */ laurent@377: void *visit(fb_name_decl_c *symbol) { laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /* VAR_OUTPUT [RETAIN | NON_RETAIN] var_init_decl_list END_VAR */ laurent@377: /* option -> may be NULL ! */ laurent@377: void *visit(output_declarations_c *symbol) { laurent@377: symbol->var_init_decl_list->accept(*this); laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /* VAR_IN_OUT var_declaration_list END_VAR */ laurent@377: void *visit(input_output_declarations_c *symbol) { laurent@377: symbol->var_declaration_list->accept(*this); laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /* var1_list ':' array_specification */ laurent@377: //SYM_REF2(array_var_declaration_c, var1_list, array_specification) laurent@377: void *visit(array_var_declaration_c *symbol) { laurent@377: array_specification_c *specification = dynamic_cast(symbol->array_specification); laurent@377: if (specification != NULL) { laurent@377: current_mode = arrayname_im; laurent@377: symbol->array_specification->accept(*this); laurent@377: current_mode = none_im; laurent@377: } laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /* VAR [CONSTANT] var_init_decl_list END_VAR */ laurent@377: /* option -> may be NULL ! */ laurent@377: /* helper symbol for input_declarations */ laurent@377: void *visit(var_declarations_c *symbol) { laurent@377: symbol->var_init_decl_list->accept(*this); laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /* VAR RETAIN var_init_decl_list END_VAR */ laurent@377: void *visit(retentive_var_declarations_c *symbol) { laurent@377: symbol->var_init_decl_list->accept(*this); laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /* VAR [CONSTANT|RETAIN|NON_RETAIN] located_var_decl_list END_VAR */ laurent@377: /* option -> may be NULL ! */ laurent@377: //SYM_REF2(located_var_declarations_c, option, located_var_decl_list) laurent@377: void *visit(located_var_declarations_c *symbol) { laurent@377: symbol->located_var_decl_list->accept(*this); laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /* [variable_name] location ':' located_var_spec_init */ laurent@377: /* variable_name -> may be NULL ! */ laurent@377: //SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused) laurent@377: void *visit(located_var_decl_c *symbol) { laurent@381: array_spec_init_c* array_spec_init = dynamic_cast(symbol->located_var_spec_init); laurent@381: if (array_spec_init != NULL) { laurent@381: current_mode = arrayname_im; laurent@381: symbol->located_var_spec_init->accept(*this); laurent@381: current_mode = none_im; laurent@381: } laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /*| VAR_EXTERNAL [CONSTANT] external_declaration_list END_VAR */ laurent@377: /* option -> may be NULL ! */ laurent@377: //SYM_REF2(external_var_declarations_c, option, external_declaration_list) laurent@377: void *visit(external_var_declarations_c *symbol) { laurent@377: symbol->external_declaration_list->accept(*this); laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /* global_var_name ':' (simple_specification|subrange_specification|enumerated_specification|array_specification|prev_declared_structure_type_name|function_block_type_name */ laurent@377: //SYM_REF2(external_declaration_c, global_var_name, specification) laurent@377: void *visit(external_declaration_c *symbol) { laurent@377: array_specification_c* array_specification = dynamic_cast(symbol->specification); laurent@377: if (array_specification != NULL) { laurent@377: current_mode = arrayname_im; laurent@377: symbol->specification->accept(*this); laurent@377: current_mode = none_im; laurent@377: } laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /*| VAR_GLOBAL [CONSTANT|RETAIN] global_var_decl_list END_VAR */ laurent@377: /* option -> may be NULL ! */ laurent@377: // SYM_REF2(global_var_declarations_c, option, global_var_decl_list) laurent@377: void *visit(global_var_declarations_c *symbol) { laurent@377: symbol->global_var_decl_list->accept(*this); laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */ laurent@377: /* type_specification ->may be NULL ! */ laurent@377: // SYM_REF2(global_var_decl_c, global_var_spec, type_specification) laurent@377: void *visit(global_var_decl_c *symbol) { laurent@377: array_spec_init_c* array_spec_init = dynamic_cast(symbol->type_specification); laurent@377: if (array_spec_init != NULL) { laurent@377: current_mode = arrayname_im; laurent@377: symbol->type_specification->accept(*this); laurent@377: current_mode = none_im; laurent@377: } laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: void *visit(function_var_decls_c *symbol) { laurent@377: symbol->decl_list->accept(*this); laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /*****************************/ laurent@377: /* B 1.5.2 - Function Blocks */ laurent@377: /*****************************/ laurent@377: laurent@377: /* VAR_TEMP temp_var_decl_list END_VAR */ laurent@377: void *visit(temp_var_decls_c *symbol) { laurent@377: symbol->var_decl_list->accept(*this); laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: /* VAR NON_RETAIN var_init_decl_list END_VAR */ laurent@377: void *visit(non_retentive_var_decls_c *symbol) { laurent@377: symbol->var_decl_list->accept(*this); laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: }; laurent@377: laurent@377: laurent@377: laurent@377: laurent@377: /***********************************************************************/ laurent@377: /***********************************************************************/ laurent@377: /***********************************************************************/ laurent@377: /***********************************************************************/ laurent@377: /***********************************************************************/ laurent@377: laurent@377: 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: msousa@283: private: msousa@283: void print_end_of_block_label(void) { msousa@283: /* Print and __end label for return statements! msousa@283: * If label is not used by at least one goto, compiler will generate a warning. msousa@283: * To work around this we introduce the useless goto. msousa@283: */ msousa@283: s4o.print("\n"); msousa@283: s4o.print(s4o.indent_spaces); msousa@283: s4o.print("/* to humour the compiler, we insert a goto */\n"); msousa@283: s4o.print(s4o.indent_spaces); msousa@283: s4o.print("goto "); msousa@283: s4o.print(END_LABEL); msousa@283: s4o.print(";\n"); msousa@283: s4o.indent_left(); msousa@283: msousa@283: /* write the label marking the end of the code block */ msousa@283: /* please see the comment before the RET_operator_c visitor for details... */ msousa@283: /* also needed for return_statement_c */ msousa@283: s4o.print("\n"); msousa@283: s4o.print(s4o.indent_spaces); msousa@283: s4o.print(END_LABEL); msousa@283: s4o.print(":\n"); msousa@283: s4o.indent_right(); msousa@283: } msousa@283: msousa@283: 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; laurent@318: initialization_analyzer_c initialization_analyzer(default_value); laurent@318: switch (initialization_analyzer.get_initialization_type()) { laurent@318: case initialization_analyzer_c::struct_it: laurent@318: { laurent@318: generate_c_structure_initialization_c *structure_initialization = new generate_c_structure_initialization_c(&s4o); laurent@318: structure_initialization->init_structure_default(symbol->type_name); laurent@318: structure_initialization->init_structure_values(default_value); laurent@318: delete structure_initialization; laurent@318: } laurent@377: break; laurent@318: case initialization_analyzer_c::array_it: laurent@318: { laurent@377: generate_c_array_initialization_c *array_initialization = new generate_c_array_initialization_c(&s4o); laurent@377: array_initialization->init_array_size(symbol->type_name); laurent@377: array_initialization->init_array_values(default_value); laurent@377: delete array_initialization; laurent@318: } laurent@377: break; laurent@318: default: laurent@318: default_value->accept(*this); laurent@318: break; laurent@318: } 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: msousa@283: print_end_of_block_label(); msousa@283: 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@337: symbol->fblock_body->accept(*inlinedecl); 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); msousa@283: print_end_of_block_label(); laurent@330: s4o.print(s4o.indent_spaces + "return;\n"); 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@337: symbol->function_block_body->accept(*inlinedecl); 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); msousa@283: print_end_of_block_label(); laurent@330: s4o.print(s4o.indent_spaces + "return;\n"); 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: }; laurent@377: 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@377: s4o.print("#include \"POUS.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@377: s4o.print(","); laurent@221: switch (current_varqualifier) { laurent@377: 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@377: default: laurent@377: s4o.print("retain"); laurent@377: 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@377: 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 { laurent@377: public: laurent@377: typedef enum { laurent@377: none_gm, laurent@377: datatypes_gm, laurent@377: pous_gm, laurent@377: } generate_mode_t; laurent@377: 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; laurent@377: generate_c_datatypes_c generate_c_datatypes; 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: laurent@377: generate_mode_t current_mode; laurent@377: 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"), laurent@377: generate_c_datatypes(&pous_s4o, &pous_incl_s4o), lbessard@121: generate_c_pous(&pous_s4o, &pous_incl_s4o) { lbessard@70: current_builddir = builddir; lbessard@70: current_configuration = NULL; laurent@377: current_mode = none_gm; 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"); laurent@377: laurent@377: current_mode = datatypes_gm; lbessard@70: for(int i = 0; i < symbol->n; i++) { lbessard@70: symbol->elements[i]->accept(*this); lbessard@70: } laurent@377: laurent@377: current_mode = pous_gm; laurent@377: for(int i = 0; i < symbol->n; i++) { laurent@377: symbol->elements[i]->accept(*this); laurent@377: } laurent@377: 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) { laurent@377: current_name = symbol->value; laurent@377: 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) { laurent@377: switch (current_mode) { laurent@377: case datatypes_gm: laurent@377: symbol->accept(generate_c_datatypes); laurent@377: break; laurent@377: default: laurent@377: break; laurent@377: } 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) { laurent@377: switch (current_mode) { laurent@377: case datatypes_gm: laurent@377: symbol->var_declarations_list->accept(generate_c_datatypes); laurent@377: break; laurent@377: case pous_gm: laurent@377: symbol->accept(generate_c_pous); laurent@377: break; laurent@377: default: laurent@377: break; laurent@377: } laurent@377: 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) { laurent@377: switch (current_mode) { laurent@377: case datatypes_gm: laurent@377: symbol->var_declarations->accept(generate_c_datatypes); laurent@377: break; laurent@377: case pous_gm: laurent@377: symbol->accept(generate_c_pous); laurent@377: break; laurent@377: default: laurent@377: break; laurent@377: } laurent@377: 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) { laurent@377: switch (current_mode) { laurent@377: case datatypes_gm: laurent@377: symbol->var_declarations->accept(generate_c_datatypes); laurent@377: break; laurent@377: case pous_gm: laurent@377: symbol->accept(generate_c_pous); laurent@377: break; laurent@377: default: laurent@377: break; laurent@377: } laurent@377: 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) { laurent@377: switch (current_mode) { laurent@377: case datatypes_gm: laurent@377: if (symbol->global_var_declarations != NULL) laurent@377: symbol->global_var_declarations->accept(generate_c_datatypes); laurent@377: break; laurent@377: laurent@377: case pous_gm: laurent@377: static int configuration_count = 0; laurent@377: laurent@377: if (configuration_count++) { laurent@377: /* the first configuration is the one we will use!! */ laurent@377: ERROR; laurent@377: } laurent@377: laurent@377: current_configuration = symbol; laurent@377: laurent@377: { laurent@377: calculate_common_ticktime_c calculate_common_ticktime; laurent@377: symbol->accept(calculate_common_ticktime); laurent@377: common_ticktime = calculate_common_ticktime.get_common_ticktime(); laurent@377: if (common_ticktime == 0) { laurent@377: fprintf(stderr, "\nYou must at least define a periodic task to set cycle period!"); laurent@377: ERROR; laurent@377: } laurent@377: laurent@377: symbol->configuration_name->accept(*this); laurent@377: laurent@377: stage4out_c config_s4o(current_builddir, current_name, "c"); laurent@377: generate_c_config_c generate_c_config(&config_s4o); laurent@377: symbol->accept(generate_c_config); laurent@377: laurent@377: config_s4o.print("unsigned long long common_ticktime__ = "); laurent@377: config_s4o.print_long_long_integer(common_ticktime); laurent@377: config_s4o.print("; /*ns*/\n"); laurent@377: config_s4o.print("unsigned long greatest_tick_count__ = "); laurent@377: config_s4o.print_long_integer(calculate_common_ticktime.get_greatest_tick_count()); laurent@377: config_s4o.print("; /*tick*/\n"); laurent@377: } laurent@377: laurent@377: symbol->resource_declarations->accept(*this); laurent@377: laurent@377: current_configuration = NULL; laurent@377: break; laurent@377: laurent@377: default: laurent@377: break; lbessard@70: } laurent@377: return NULL; laurent@377: } laurent@377: laurent@377: void *visit(resource_declaration_c *symbol) { laurent@377: switch (current_mode) { laurent@377: case datatypes_gm: laurent@377: if (symbol->global_var_declarations != NULL) laurent@377: symbol->global_var_declarations->accept(generate_c_datatypes); laurent@377: break; laurent@377: case pous_gm: laurent@377: symbol->resource_name->accept(*this); laurent@377: { laurent@377: stage4out_c resources_s4o(current_builddir, current_name, "c"); laurent@377: generate_c_resources_c generate_c_resources(&resources_s4o, current_configuration, symbol, common_ticktime); laurent@377: symbol->accept(generate_c_resources); laurent@377: } laurent@377: break; laurent@377: default: laurent@377: break; laurent@248: } ccb@202: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(single_resource_declaration_c *symbol) { laurent@377: switch (current_mode) { laurent@377: case pous_gm: laurent@377: { laurent@377: stage4out_c resources_s4o(current_builddir, "RESOURCE", "c"); laurent@377: generate_c_resources_c generate_c_resources(&resources_s4o, current_configuration, symbol, common_ticktime); laurent@377: symbol->accept(generate_c_resources); laurent@377: } laurent@377: break; laurent@377: default: laurent@377: break; laurent@377: } 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: