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 <http://www.gnu.org/licenses/>.
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 <string>
lbessard@70: #include <iostream>
lbessard@70: #include <sstream>
lbessard@70: #include <typeinfo>
lbessard@111: #include <list>
laurent@377: #include <map>
laurent@377: #include <sstream>
etisserant@139: #include <strings.h>
lbessard@70: 
msousa@592: 
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"
msousa@596: #include "../../main.hh" // required for ERROR() and ERROR_MSG() macros.
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: 
msousa@522: 
msousa@522: #define STAGE4_ERROR(symbol1, symbol2, ...) {stage4err("while generating C code", symbol1, symbol2, __VA_ARGS__); exit(EXIT_FAILURE);}
msousa@522: 
msousa@522: 
msousa@592: /* Macros to access the constant value of each expression (if it exists) from the annotation introduced to the symbol_c object by constant_folding_c in stage3! */
msousa@612: #define VALID_CVALUE(dtype, symbol)           (symbol_c::cs_const_value == (symbol)->const_value._##dtype.status)
msousa@612: #define GET_CVALUE(dtype, symbol)             ((symbol)->const_value._##dtype.value) 
msousa@592: 
msousa@592: 
msousa@522: 
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@706: #define DECLARE_GLOBAL_FB "__DECLARE_GLOBAL_FB"
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@706: #define DECLARE_EXTERNAL_FB "__DECLARE_EXTERNAL_FB"
laurent@217: #define DECLARE_LOCATED "__DECLARE_LOCATED"
laurent@396: #define DECLARE_GLOBAL_PROTOTYPE "__DECLARE_GLOBAL_PROTOTYPE"
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@706: #define INIT_GLOBAL_FB "__INIT_GLOBAL_FB"
laurent@217: #define INIT_GLOBAL_LOCATED "__INIT_GLOBAL_LOCATED"
laurent@217: #define INIT_EXTERNAL "__INIT_EXTERNAL"
Laurent@706: #define INIT_EXTERNAL_FB "__INIT_EXTERNAL_FB"
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@706: #define GET_EXTERNAL_FB "__GET_EXTERNAL_FB"
laurent@217: #define GET_LOCATED "__GET_LOCATED"
mjsousa@873: 
mjsousa@873: #define GET_VAR_REF "__GET_VAR_REF"
mjsousa@873: #define GET_EXTERNAL_REF "__GET_EXTERNAL_REF"
mjsousa@873: #define GET_EXTERNAL_FB_REF "__GET_EXTERNAL_FB_REF"
mjsousa@873: #define GET_LOCATED_REF "__GET_LOCATED_REF"
mjsousa@873: 
mjsousa@933: #define GET_VAR_DREF "__GET_VAR_DREF"
mjsousa@933: #define GET_EXTERNAL_DREF "__GET_EXTERNAL_DREF"
mjsousa@933: #define GET_EXTERNAL_FB_DREF "__GET_EXTERNAL_FB_DREF"
mjsousa@933: #define GET_LOCATED_DREF "__GET_LOCATED_DREF"
mjsousa@933: 
laurent@217: #define GET_VAR_BY_REF "__GET_VAR_BY_REF"
laurent@217: #define GET_EXTERNAL_BY_REF "__GET_EXTERNAL_BY_REF"
Laurent@706: #define GET_EXTERNAL_FB_BY_REF "__GET_EXTERNAL_FB_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@706: #define SET_EXTERNAL_FB "__SET_EXTERNAL_FB"
laurent@217: #define SET_LOCATED "__SET_LOCATED"
laurent@217: 
laurent@396: /* Variable initial value symbol for accessor macros */
laurent@396: #define INITIAL_VALUE "__INITIAL_VALUE"
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: 
Edouard@882: 
Edouard@882: static int generate_line_directives__ = 0;
mjsousa@908: static int generate_pou_filepairs__   = 0;
Edouard@882: 
Edouard@882: #ifdef __unix__
mjsousa@878: /* Parse command line options passed from main.c !! */
mjsousa@878: #include <stdlib.h> // for getsybopt()
mjsousa@878: int  stage4_parse_options(char *options) {
mjsousa@908:   enum {                    LINE_OPT = 0            ,  SEPTFILE_OPT              /*, SOME_OTHER_OPT, YET_ANOTHER_OPT */};
mjsousa@908:   char *const token[] = { /*[LINE_OPT]=*/(char *)"l",/*SEPTFILE_OPT*/(char *)"p" /*, SOME_OTHER_OPT, ...             */, NULL };
mjsousa@878:   /* unfortunately, the above commented out syntax for array initialization is valid in C, but not in C++ */
mjsousa@878:   
mjsousa@878:   char *subopts = options;
mjsousa@878:   char *value;
mjsousa@878:   int opt;
mjsousa@878: 
mjsousa@878:   while (*subopts != '\0') {
mjsousa@878:     switch (getsubopt(&subopts, token, &value)) {
mjsousa@908:       case     LINE_OPT: generate_line_directives__  = 1; break;
mjsousa@908:       case SEPTFILE_OPT: generate_pou_filepairs__    = 1; break;
mjsousa@908:       default          : fprintf(stderr, "Unrecognized option: -O %s\n", value); return -1; break;
mjsousa@878:      }
mjsousa@878:   }     
mjsousa@878:   return 0;
mjsousa@878: }
mjsousa@878: 
mjsousa@878: 
mjsousa@878: void stage4_print_options(void) {
mjsousa@878:   printf("          (options must be separated by commas. Example: 'l,w,x')\n"); 
mjsousa@878:   printf("      l : insert '#line' directives in generated C code.\n"); 
mjsousa@908:   printf("      p : place each POU in a separate pair of files (<pou_name>.c, <pou_name>.h).\n"); 
mjsousa@878: }
Edouard@882: #else /* not __unix__ */
Edouard@882: /* getsubopt isn't supported with mingw, 
Edouard@882:  *  then stage4 options aren't available on windows*/
Edouard@882: void stage4_print_options(void) {}
Edouard@882: int  stage4_parse_options(char *options) {}
Edouard@882: #endif 
mjsousa@878: 
mjsousa@878: /***********************************************************************/
mjsousa@878: /***********************************************************************/
mjsousa@878: /***********************************************************************/
mjsousa@878: /***********************************************************************/
mjsousa@878: 
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<list_c *>(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@810:       print_list(symbol->fb_name_list, spec_init_sperator_c::get_spec(symbol->fb_spec_init)); 
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@852: /* A helper class that analyses if the datatype of a variable is 'complex'. */
msousa@852: /* 'complex' means that it is either a strcuture or an array!               */
msousa@863: class analyse_variable_c: public search_visitor_c {
msousa@852:   private:
msousa@852:     static analyse_variable_c *singleton_;
msousa@852: 
msousa@852:   public:
msousa@852:     analyse_variable_c(void) {};
msousa@852:     
msousa@852:     static bool is_complex_type(symbol_c *symbol) {
msousa@863:       if (NULL == symbol) ERROR;
mjsousa@872:       if (!get_datatype_info_c::is_type_valid     (symbol->datatype)) return false;
msousa@852:       return (   get_datatype_info_c::is_structure(symbol->datatype) 
msousa@852:               || get_datatype_info_c::is_array    (symbol->datatype) 
msousa@852:              );
msousa@852:     }
msousa@852: 
msousa@863:     
msousa@863:   private:
msousa@863:     symbol_c *last_fb, *first_non_fb_identifier;
msousa@863: 
msousa@863:   public:  
msousa@863:     /* returns the first element (from left to right) in a structured variable that is not a FB, i.e. is either a structure or an array! */
msousa@863:     /* eg:
msousa@863:      *      fb1.fb2.fb3.real       returns ??????
msousa@863:      *      fb1.fb2.struct1.real   returns struct1
msousa@863:      *      struct1.real           returns struct1
msousa@863:      */
msousa@863:     static symbol_c *find_first_nonfb(symbol_c *symbol) {
msousa@863:       if (NULL == singleton_)       singleton_ = new analyse_variable_c();
msousa@863:       if (NULL == singleton_)       ERROR;
msousa@863:       if (NULL == symbol)           ERROR;
msousa@863:       
msousa@863:       singleton_->last_fb                 = NULL;
msousa@863:       singleton_->first_non_fb_identifier = NULL;
msousa@863:       return (symbol_c *)symbol->accept(*singleton_);
msousa@863:     }
msousa@863:     
msousa@852:     /* returns true if a strcutured variable (e.g. fb1.fb2.strcut1.real) contains a structure or array */
msousa@852:     /* eg:
msousa@852:      *      fb1.fb2.fb3.real       returns FALSE
msousa@852:      *      fb1.fb2.struct1.real   returns TRUE
msousa@852:      *      struct1.real           returns TRUE
msousa@852:      */
msousa@852:     static bool contains_complex_type(symbol_c *symbol) {
msousa@863:       if (NULL == symbol) ERROR;
msousa@863:       if (!get_datatype_info_c::is_type_valid(symbol->datatype)) ERROR;
msousa@863:       
msousa@863:       symbol_c *first_non_fb = (symbol_c *)find_first_nonfb(symbol);
msousa@863:       return is_complex_type(first_non_fb->datatype);
msousa@863:     }
msousa@863:     
msousa@863:     
msousa@863:     /* returns the datatype of the variable returned by find_first_nonfb() */
msousa@863:     /* eg:
msousa@863:      *      fb1.fb2.fb3.real       returns ??????
msousa@863:      *      fb1.fb2.struct1.real   returns datatype of struct1
msousa@863:      *      struct1.real           returns datatype of struct1
msousa@863:      */
msousa@863:     static search_var_instance_decl_c::vt_t first_nonfb_vardecltype(symbol_c *symbol, symbol_c *scope) {
msousa@863:       if (NULL == symbol) ERROR;
msousa@863:       if (!get_datatype_info_c::is_type_valid(symbol->datatype)) ERROR;
msousa@863:       
msousa@863:       symbol_c *first_non_fb = (symbol_c *)find_first_nonfb(symbol);
msousa@863:       if (NULL != singleton_->last_fb) {
msousa@863:         scope = singleton_->last_fb->datatype;
msousa@863:         symbol = singleton_->first_non_fb_identifier;
msousa@863:       }
msousa@863:       
msousa@863:       search_var_instance_decl_c search_var_instance_decl(scope);
msousa@863:       
msousa@863:       return search_var_instance_decl.get_vartype(symbol);
msousa@852:     }
msousa@852:     
msousa@852:     
msousa@853:     /*********************/
msousa@853:     /* B 1.4 - Variables */
msousa@853:     /*********************/
msousa@853:     void *visit(symbolic_variable_c *symbol) {
msousa@863:       if (!get_datatype_info_c::is_type_valid    (symbol->datatype)) ERROR;
msousa@863:       if (!get_datatype_info_c::is_function_block(symbol->datatype)) {
msousa@863:          first_non_fb_identifier = symbol; 
msousa@863:          return (void *)symbol;
msousa@863:       }
msousa@863:       last_fb = symbol;
msousa@853:       return NULL;
msousa@853:     }
msousa@853:     
msousa@852:     /*************************************/
msousa@852:     /* B.1.4.2   Multi-element Variables */
msousa@852:     /*************************************/
msousa@852:     
msousa@852:     // SYM_REF2(structured_variable_c, record_variable, field_selector)
msousa@852:     void *visit(structured_variable_c *symbol) {
msousa@863:       symbol_c *res = (symbol_c *)symbol->record_variable->accept(*this);
msousa@863:       if (NULL != res) return res;
msousa@863:       
msousa@863:       if (!get_datatype_info_c::is_type_valid    (symbol->datatype)) ERROR;
msousa@863:       if (!get_datatype_info_c::is_function_block(symbol->datatype)) {
msousa@863:          first_non_fb_identifier = symbol->field_selector; 
msousa@863:          return (void *)symbol;
msousa@863:       }
msousa@863: 
msousa@863:       last_fb = symbol;      
msousa@863:       return NULL;      
msousa@852:     }
msousa@852:     
msousa@852:     /*  subscripted_variable '[' subscript_list ']' */
msousa@852:     //SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
msousa@852:     void *visit(array_variable_c *symbol) {
msousa@863:       void *res = symbol->subscripted_variable->accept(*this);
msousa@863:       if (NULL != res) return res;
msousa@863:       return (void *)symbol;      
msousa@863:     }
msousa@863: 
msousa@863:     
msousa@852: };
msousa@852: 
msousa@852: analyse_variable_c *analyse_variable_c::singleton_ = NULL;
msousa@852: 
msousa@852: /***********************************************************************/
msousa@852: /***********************************************************************/
msousa@852: /***********************************************************************/
msousa@852: /***********************************************************************/
msousa@852: 
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: 
msousa@594: unsigned long long calculate_time(symbol_c *symbol) {
msousa@594:   if (NULL == symbol) return 0;
lbessard@70:   
msousa@594:   interval_c *interval = dynamic_cast<interval_c *>(symbol);
msousa@594:   duration_c *duration = dynamic_cast<duration_c *>(symbol);
lbessard@70:   
Laurent@632:   if ((NULL == interval) && (NULL == duration))
Laurent@631:   	  {STAGE4_ERROR(symbol, symbol, "This type of interval value is not currently supported"); ERROR;}
msousa@594: 
msousa@594:   if (NULL != duration) {
lbessard@70:     /* SYM_REF2(duration_c, neg, interval) */
msousa@594:     if (duration->neg != NULL)
Laurent@631:       {STAGE4_ERROR(duration, duration, "Negative TIME literals for interval are not currently supported"); ERROR;}
msousa@594:     return calculate_time(duration->interval);
msousa@594:   }
msousa@594: 
msousa@594:   if (NULL != interval) {
msousa@547:     /* SYM_REF5(interval_c, days, hours, minutes, seconds, milliseconds) */
msousa@594:       unsigned long long int time_ull = 0; 
msousa@594:       long double            time_ld  = 0;
msousa@594:       /*
msousa@594:       const unsigned long long int MILLISECOND = 1000000;
msousa@594:       const unsigned long long int      SECOND = 1000 * MILLISECOND
msousa@594:       */
msousa@594:       
msousa@594:       if (NULL != interval->milliseconds) {
msousa@594:         if      (VALID_CVALUE( int64, interval->milliseconds) &&           GET_CVALUE( int64, interval->milliseconds) < 0) ERROR; // interval elements should always be positive!
msousa@594:         if      (VALID_CVALUE( int64, interval->milliseconds)) time_ull += GET_CVALUE( int64, interval->milliseconds) * MILLISECOND;
msousa@594:         else if (VALID_CVALUE(uint64, interval->milliseconds)) time_ull += GET_CVALUE(uint64, interval->milliseconds) * MILLISECOND;
msousa@594:         else if (VALID_CVALUE(real64, interval->milliseconds)) time_ld  += GET_CVALUE(real64, interval->milliseconds) * MILLISECOND;
msousa@594:         else ERROR; // if (NULL != interval->milliseconds) is true, then it must have a valid constant value!
msousa@594:       }
msousa@547:    
msousa@594:       if (NULL != interval->seconds     ) {
msousa@594:         if      (VALID_CVALUE( int64, interval->seconds     ) &&           GET_CVALUE( int64, interval->seconds     ) < 0) ERROR; // interval elements should always be positive!
msousa@594:         if      (VALID_CVALUE( int64, interval->seconds     )) time_ull += GET_CVALUE( int64, interval->seconds     ) * SECOND;
msousa@594:         else if (VALID_CVALUE(uint64, interval->seconds     )) time_ull += GET_CVALUE(uint64, interval->seconds     ) * SECOND;
msousa@594:         else if (VALID_CVALUE(real64, interval->seconds     )) time_ld  += GET_CVALUE(real64, interval->seconds     ) * SECOND;
msousa@594:         else ERROR; // if (NULL != interval->seconds) is true, then it must have a valid constant value!
msousa@594:       }
msousa@594: 
msousa@594:       if (NULL != interval->minutes     ) {
msousa@594:         if      (VALID_CVALUE( int64, interval->minutes     ) &&           GET_CVALUE( int64, interval->minutes     ) < 0) ERROR; // interval elements should always be positive!
msousa@594:         if      (VALID_CVALUE( int64, interval->minutes     )) time_ull += GET_CVALUE( int64, interval->minutes     ) * SECOND * 60;
msousa@594:         else if (VALID_CVALUE(uint64, interval->minutes     )) time_ull += GET_CVALUE(uint64, interval->minutes     ) * SECOND * 60;
msousa@594:         else if (VALID_CVALUE(real64, interval->minutes     )) time_ld  += GET_CVALUE(real64, interval->minutes     ) * SECOND * 60;
msousa@594:         else ERROR; // if (NULL != interval->minutes) is true, then it must have a valid constant value!
msousa@594:       }
msousa@594: 
msousa@594:       if (NULL != interval->hours       ) {
msousa@594:         if      (VALID_CVALUE( int64, interval->hours       ) &&           GET_CVALUE( int64, interval->hours       ) < 0) ERROR; // interval elements should always be positive!
msousa@594:         if      (VALID_CVALUE( int64, interval->hours       )) time_ull += GET_CVALUE( int64, interval->hours       ) * SECOND * 60 * 60;
msousa@594:         else if (VALID_CVALUE(uint64, interval->hours       )) time_ull += GET_CVALUE(uint64, interval->hours       ) * SECOND * 60 * 60;
msousa@594:         else if (VALID_CVALUE(real64, interval->hours       )) time_ld  += GET_CVALUE(real64, interval->hours       ) * SECOND * 60 * 60;
msousa@594:         else ERROR; // if (NULL != interval->hours) is true, then it must have a valid constant value!
msousa@594:       }
msousa@594: 
msousa@594:       if (NULL != interval->days        ) {
msousa@594:         if      (VALID_CVALUE( int64, interval->days        ) &&           GET_CVALUE( int64, interval->days        ) < 0) ERROR; // interval elements should always be positive!
msousa@594:         if      (VALID_CVALUE( int64, interval->days        )) time_ull += GET_CVALUE( int64, interval->days        ) * SECOND * 60 * 60 * 24;
msousa@594:         else if (VALID_CVALUE(uint64, interval->days        )) time_ull += GET_CVALUE(uint64, interval->days        ) * SECOND * 60 * 60 * 24;
msousa@594:         else if (VALID_CVALUE(real64, interval->days        )) time_ld  += GET_CVALUE(real64, interval->days        ) * SECOND * 60 * 60 * 24;
msousa@594:         else ERROR; // if (NULL != interval->days) is true, then it must have a valid constant value!
msousa@594:       }
msousa@594: 
msousa@594:       time_ull += time_ld;
msousa@594:       return time_ull;
msousa@594:   };
msousa@594:   ERROR; // should never reach this point!
msousa@594:   return 0; // humour the compiler!
conti@661: }
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) {
Laurent@816:       if (symbol->interval_data_source != NULL) {
Laurent@816:     	  unsigned long long time = calculate_time(symbol->interval_data_source);
Laurent@816:     	  if (time < 0)  ERROR;
Laurent@816:     	  else           update_ticktime(time);
Laurent@816:       }
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: 
mjsousa@914: identifier_c *generate_unique_id(const char *prefix = "", symbol_c *clone = NULL) {
mjsousa@913:   static int counter = 0;
mjsousa@913:   
mjsousa@913:   counter++;
mjsousa@914:   int   len = snprintf(NULL, 0, "%s__UID_%d", prefix, counter); // UID -> Unique IDentifier
mjsousa@913:   char *str = (char *)malloc(len+1);
mjsousa@914:   if (snprintf(str, len+1,      "%s__UID_%d", prefix, counter) < 0) ERROR;
mjsousa@913:   
mjsousa@913:   identifier_c *id = new identifier_c(str);
mjsousa@913:   if (NULL == id) ERROR;
mjsousa@913:   if (NULL != clone)
mjsousa@913:     *(dynamic_cast<symbol_c *>(id)) = *(dynamic_cast<symbol_c *>(clone));
mjsousa@913:   return id;
mjsousa@913: }
mjsousa@913: 
mjsousa@913: 
mjsousa@914: identifier_c *generate_unique_id(symbol_c *prefix, symbol_c *clone = NULL) {
mjsousa@914:   token_c *token = dynamic_cast<token_c *>(prefix);
mjsousa@914:   //if (NULL == token) ERROR;
mjsousa@914:   if (NULL == token)
mjsousa@914:     return generate_unique_id("", clone);
mjsousa@914:   return generate_unique_id(token->value, clone);
mjsousa@914: }
mjsousa@914: 
mjsousa@914: 
mjsousa@914: /* This class will generate a new datatype for each implicitly declared array datatype
mjsousa@914:  * (i.e. arrays declared in a variable declaration, or a struct datatype declaration...)
mjsousa@930:  * It will do the same for implicitly declared REF_TO datatypes.
mjsousa@916:  * 
mjsousa@916:  * The class will be called once for each POU declaration, and once for each derived datatype declaration.
mjsousa@914:  * 
mjsousa@914:  * e.g.:
mjsousa@914:  *      VAR  a: ARRAY [1..3] OF INT; END_VAR   <---- ARRAY datatype is implicitly declared inside the variable declaration
mjsousa@914:  *      TYPE STRUCT
mjsousa@914:  *               a: ARRAY [1..3] OF INT;       <---- ARRAY datatype is implicitly declared inside the struct type declaration  
mjsousa@914:  *               b: INT;
mjsousa@914:  *            END_STRUCT
mjsousa@914:  *      END_TYPE
mjsousa@914:  */
mjsousa@913: class generate_c_datatypes_c: public iterator_visitor_c {
laurent@377:   private:
mjsousa@913:     generate_c_typedecl_c generate_c_typedecl;
mjsousa@914:     symbol_c *prefix;
laurent@377:   public:
mjsousa@906:     generate_c_datatypes_c(stage4out_c *s4o_incl_ptr)
mjsousa@913:       : generate_c_typedecl(s4o_incl_ptr) {
mjsousa@914:         prefix = NULL;
laurent@377:     };
laurent@377:     virtual ~generate_c_datatypes_c(void) {
laurent@377:     }
laurent@377: 
laurent@377:     /*************************/
laurent@377:     /* B.1 - Common elements */
laurent@377:     /*************************/
laurent@377:     /**********************/
laurent@377:     /* B.1.3 - Data types */
laurent@377:     /**********************/
mjsousa@914:     /********************************/
mjsousa@914:     /* B 1.3.3 - Derived data types */
mjsousa@914:     /********************************/
mjsousa@914:     /*  identifier ':' array_spec_init */
mjsousa@914:     void *visit(array_type_declaration_c *symbol) {return NULL;} // This is not an implicitly defined array!
mjsousa@914: 
mjsousa@916:     /* ref_spec:  REF_TO (non_generic_type_name | function_block_type_name) */
mjsousa@916:     void *visit(ref_spec_c *symbol) {
mjsousa@916:       identifier_c *id = generate_unique_id(prefix, symbol);
mjsousa@916:       /* Warning: The following is dangerous... 
mjsousa@916:        * We are asking the generate_c_typedecl_c visitor to visit a newly created ref_spec_init_c object
mjsousa@916:        * that has not been through stage 3, and therefore does not have stage 3 annotations filled in.
mjsousa@916:        * This will only work if generate_c_typedecl_c does ot depend on the stage 3 annotations!
mjsousa@916:        */
mjsousa@916:       ref_spec_init_c   ref_spec(symbol, NULL);
mjsousa@916:       ref_type_decl_c   ref_decl(id, &ref_spec);
mjsousa@916:       ref_decl.accept(generate_c_typedecl); // Must be done _before_ adding the annotation, due to the way generate_c_typedecl_c works
mjsousa@916:       symbol->anotations_map["generate_c_annotaton__implicit_type_id"] = id;
mjsousa@916:       return NULL;
mjsousa@916:     }
mjsousa@916: 
mjsousa@916:     /* For the moment, we do not support initialising reference data types */
mjsousa@916:     /* ref_spec_init: ref_spec [ ASSIGN ref_initialization ] */ 
mjsousa@916:     /* NOTE: ref_initialization may be NULL!! */
mjsousa@916:     // SYM_REF2(ref_spec_init_c, ref_spec, ref_initialization)
mjsousa@916:     void *visit(ref_spec_init_c *symbol) {
mjsousa@916:       symbol->ref_spec->accept(*this);
mjsousa@916:       int implicit_id_count = symbol->ref_spec->anotations_map.count("generate_c_annotaton__implicit_type_id");
mjsousa@916:       if (implicit_id_count  > 1) ERROR;
mjsousa@916:       if (implicit_id_count == 1)
mjsousa@916:         symbol->anotations_map["generate_c_annotaton__implicit_type_id"] = symbol->ref_spec->anotations_map["generate_c_annotaton__implicit_type_id"];
mjsousa@916:       return NULL;
mjsousa@916:     }
mjsousa@916: 
mjsousa@916:     /* ref_type_decl: identifier ':' ref_spec_init */
mjsousa@916:     void *visit(ref_type_decl_c *symbol) {return NULL;} // This is not an implicitly defined REF_TO!
Laurent@635: 
laurent@377:     /******************************************/
laurent@377:     /* B 1.4.3 - Declaration & Initialization */
laurent@377:     /******************************************/
mjsousa@913:     void *visit(edge_declaration_c           *symbol) {return NULL;}
mjsousa@913:     void *visit(en_param_declaration_c       *symbol) {return NULL;}
mjsousa@913:     void *visit(eno_param_declaration_c      *symbol) {return NULL;}
mjsousa@913: 
laurent@377:     /* array_specification [ASSIGN array_initialization] */
laurent@377:     /* array_initialization may be NULL ! */
laurent@377:     void *visit(array_spec_init_c *symbol) {
mjsousa@913:       symbol->array_specification->accept(*this);
mjsousa@913:       int implicit_id_count = symbol->array_specification->anotations_map.count("generate_c_annotaton__implicit_type_id");
mjsousa@913:       if (implicit_id_count  > 1) ERROR;
mjsousa@913:       if (implicit_id_count == 1)
mjsousa@913:         symbol->anotations_map["generate_c_annotaton__implicit_type_id"] = symbol->array_specification->anotations_map["generate_c_annotaton__implicit_type_id"];
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) {
mjsousa@914:       identifier_c *id = generate_unique_id(prefix, symbol);
mjsousa@913:       /* Warning: The following is dangerous... 
mjsousa@913:        * We are asking the generate_c_typedecl_c visitor to visit a newly created array_type_declaration_c object
mjsousa@913:        * that has not been through stage 3, and therefore does not have stage 3 annotations filled in.
mjsousa@913:        * This will only work if generate_c_typedecl_c does ot depend on the stage 3 annotations!
mjsousa@913:        */
mjsousa@913:       array_spec_init_c        array_spec(symbol, NULL);
mjsousa@913:       array_type_declaration_c array_decl(id, &array_spec);
mjsousa@913:       array_decl.accept(generate_c_typedecl); // Must be done _before_ adding the annotation, due to the way generate_c_typedecl_c works
mjsousa@913:       symbol->anotations_map["generate_c_annotaton__implicit_type_id"] = id;
mjsousa@913:       return NULL;
mjsousa@913:     }
mjsousa@913:     
laurent@377:     /*  var1_list ':' initialized_structure */
laurent@377:     // SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure)
mjsousa@913:     void *visit(structured_var_init_decl_c   *symbol) {return NULL;}
laurent@377: 
laurent@377:     /* fb_name_list ':' function_block_type_name ASSIGN structure_initialization */
laurent@377:     /* structure_initialization -> may be NULL ! */
mjsousa@913:     void *visit(fb_name_decl_c               *symbol) {return NULL;}
laurent@377: 
msousa@624:     /*  var1_list ':' structure_type_name */
msousa@624:     //SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name)
mjsousa@913:     void *visit(structured_var_declaration_c *symbol) {return NULL;}
laurent@377: 
mjsousa@916: 
mjsousa@914:     /***********************/
mjsousa@914:     /* B 1.5.1 - Functions */
mjsousa@914:     /***********************/      
mjsousa@914:     void *visit(function_declaration_c *symbol) {
mjsousa@914:       prefix = symbol->derived_function_name;
mjsousa@914:       symbol->var_declarations_list->accept(*this);
mjsousa@914:       prefix = NULL;
mjsousa@914:       return NULL;
mjsousa@914:     }
mjsousa@914:     /*****************************/
mjsousa@914:     /* B 1.5.2 - Function Blocks */
mjsousa@914:     /*****************************/
mjsousa@914:     void *visit(function_block_declaration_c *symbol) {
mjsousa@914:       prefix = symbol->fblock_name;
mjsousa@914:       symbol->var_declarations->accept(*this);
mjsousa@914:       prefix = NULL;
mjsousa@914:       return NULL;
mjsousa@914:     }
mjsousa@914:     /**********************/
mjsousa@914:     /* B 1.5.3 - Programs */
mjsousa@914:     /**********************/    
mjsousa@914:     void *visit(program_declaration_c *symbol) {
mjsousa@914:       prefix = symbol->program_type_name;
mjsousa@914:       symbol->var_declarations->accept(*this);
mjsousa@914:       prefix = NULL;
mjsousa@914:       return NULL;
mjsousa@914:     }
laurent@377: };
laurent@377: 
laurent@377: 
laurent@377: 
laurent@377: 
mjsousa@913: 
mjsousa@913: 
mjsousa@913: 
mjsousa@913: 
mjsousa@913: 
laurent@377: /***********************************************************************/
laurent@377: /***********************************************************************/
laurent@377: /***********************************************************************/
laurent@377: /***********************************************************************/
laurent@377: /***********************************************************************/
laurent@377: 
mjsousa@907: class generate_c_pous_c {  
mjsousa@907:   /* NOTE: This is NOT a visistor class!!
mjsousa@907:    * 
mjsousa@907:    *       Actually, it does not even really need to be a class. It is simply a collection of similar functions!!
mjsousa@907:    */ 
mjsousa@907: 
msousa@267:   private:
mjsousa@907:     static void print_end_of_block_label(stage4out_c &s4o) {
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");
Edouard@634:       /* to humour the compiler, we insert a goto */
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: 
mjsousa@907:     /*************/
mjsousa@907:     /* Functions */
mjsousa@907:     /*************/
lbessard@70:   public:
mjsousa@907:     /* NOTE: The following function will be called twice:
mjsousa@907:      *         1st time:  s4o will reference the .h file, and print_declaration=true
mjsousa@907:      *                     Here, we generate the function prototypes...
mjsousa@907:      *         2nd time:  s4o will reference the .c file, and print_declaration=false
mjsousa@907:      *                     Here we generate the source code!
mjsousa@907:      */
mjsousa@907:     /*   FUNCTION derived_function_name ':' elementary_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */
mjsousa@907:     /* | FUNCTION derived_function_name ':' derived_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */
mjsousa@907:     static void handle_function(function_declaration_c *symbol, stage4out_c &s4o, bool print_declaration) {
mjsousa@907:       generate_c_vardecl_c *vardecl = NULL;
mjsousa@907:       generate_c_base_c     print_base(&s4o);
mjsousa@907:       
mjsousa@907:       TRACE("function_declaration_c");
mjsousa@907:     
mjsousa@907:       /* (A) Function declaration... */
mjsousa@907:       /* (A.1) Function return type */
mjsousa@907:       s4o.print("// FUNCTION\n");
mjsousa@907:       symbol->type_name->accept(print_base); /* return type */
mjsousa@907:       s4o.print(" ");
mjsousa@907:       /* (A.2) Function name */
mjsousa@907:       symbol->derived_function_name->accept(print_base);
mjsousa@907:       s4o.print("(");
mjsousa@907:     
mjsousa@907:       /* (A.3) Function parameters */
mjsousa@907:       s4o.indent_right();
mjsousa@907:       vardecl = new generate_c_vardecl_c(&s4o,
mjsousa@907:                                          generate_c_vardecl_c::finterface_vf,
mjsousa@907:                                          generate_c_vardecl_c::input_vt    |
mjsousa@907:                                          generate_c_vardecl_c::output_vt   |
mjsousa@907:                                          generate_c_vardecl_c::inoutput_vt |
mjsousa@907:                                          generate_c_vardecl_c::en_vt       |
mjsousa@907:                                          generate_c_vardecl_c::eno_vt);
mjsousa@907:       vardecl->print(symbol->var_declarations_list);
mjsousa@907:       delete vardecl;
mjsousa@907:       
mjsousa@907:       s4o.indent_left();
mjsousa@907:       
mjsousa@907:       s4o.print(")");
mjsousa@907:       
mjsousa@907:       /* If we only want the declaration/prototype, then return!! */
mjsousa@907:       if (print_declaration) 
mjsousa@907:         {s4o.print(";\n"); return;}
mjsousa@907:       
mjsousa@907:       /* continue generating the function definition/code... */
mjsousa@907:       s4o.print("\n" + s4o.indent_spaces + "{\n");
mjsousa@907:     
mjsousa@907:       /* (B) Function local variable declaration */
mjsousa@907:       /* (B.1) Variables declared in ST source code */
mjsousa@907:       s4o.indent_right();
mjsousa@907:       
mjsousa@907:       vardecl = new generate_c_vardecl_c(&s4o,
mjsousa@907:                     generate_c_vardecl_c::localinit_vf,
mjsousa@907:                     generate_c_vardecl_c::output_vt   |
mjsousa@907:                     generate_c_vardecl_c::inoutput_vt |
mjsousa@907:                     generate_c_vardecl_c::private_vt  |
mjsousa@907:                     generate_c_vardecl_c::eno_vt);
mjsousa@907:       vardecl->print(symbol->var_declarations_list);
mjsousa@907:       delete vardecl;
mjsousa@907:     
mjsousa@907:       /* (B.2) Temporary variable for function's return value */
mjsousa@907:       /* It will have the same name as the function itself! */
mjsousa@907:       s4o.print(s4o.indent_spaces);
mjsousa@907:       symbol->type_name->accept(print_base); /* return type */
mjsousa@907:       s4o.print(" ");
mjsousa@907:       symbol->derived_function_name->accept(print_base);
mjsousa@907:       s4o.print(" = ");
mjsousa@907:       {
mjsousa@907:         /* get the default value of this variable's type */
mjsousa@907:         symbol_c *default_value = type_initial_value_c::get(symbol->type_name);
mjsousa@907:         if (default_value == NULL) ERROR;
mjsousa@907:         initialization_analyzer_c initialization_analyzer(default_value);
mjsousa@907:         switch (initialization_analyzer.get_initialization_type()) {
mjsousa@907:           case initialization_analyzer_c::struct_it:
mjsousa@907:             {
mjsousa@907:               generate_c_structure_initialization_c *structure_initialization = new generate_c_structure_initialization_c(&s4o);
mjsousa@907:               structure_initialization->init_structure_default(symbol->type_name);
mjsousa@907:               structure_initialization->init_structure_values(default_value);
mjsousa@907:               delete structure_initialization;
mjsousa@907:             }
mjsousa@907:             break;
mjsousa@907:           case initialization_analyzer_c::array_it:
mjsousa@907:             {
mjsousa@907:               generate_c_array_initialization_c *array_initialization = new generate_c_array_initialization_c(&s4o);
mjsousa@907:               array_initialization->init_array_size(symbol->type_name);
mjsousa@907:               array_initialization->init_array_values(default_value);
mjsousa@907:               delete array_initialization;
mjsousa@907:             }
mjsousa@907:             break;
mjsousa@907:           default:
mjsousa@907:             default_value->accept(print_base);
mjsousa@907:             break;
mjsousa@907:         }
mjsousa@907:       }
mjsousa@907:       s4o.print(";\n\n");
mjsousa@907:       
mjsousa@907:       s4o.print(s4o.indent_spaces + "// Control execution\n");
mjsousa@907:       s4o.print(s4o.indent_spaces + "if (!EN) {\n");
mjsousa@907:       s4o.indent_right();
mjsousa@907:       s4o.print(s4o.indent_spaces + "if (__ENO != NULL) {\n");
mjsousa@907:       s4o.indent_right();
mjsousa@907:       s4o.print(s4o.indent_spaces + "*__ENO = __BOOL_LITERAL(FALSE);\n");
mjsousa@907:       s4o.indent_left();
mjsousa@907:       s4o.print(s4o.indent_spaces + "}\n");
mjsousa@907:       s4o.print(s4o.indent_spaces + "return ");
mjsousa@907:       symbol->derived_function_name->accept(print_base);
mjsousa@907:       s4o.print(";\n");
mjsousa@907:       s4o.indent_left();
mjsousa@907:       s4o.print(s4o.indent_spaces + "}\n");
mjsousa@907:     
mjsousa@907:       /* (C) Function body */
mjsousa@907:       generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol->derived_function_name, symbol);
mjsousa@907:       symbol->function_body->accept(generate_c_code);
mjsousa@907:       
mjsousa@907:       print_end_of_block_label(s4o);
mjsousa@907:       
mjsousa@907:       vardecl = new generate_c_vardecl_c(&s4o,
mjsousa@907:                     generate_c_vardecl_c::foutputassign_vf,
mjsousa@907:                     generate_c_vardecl_c::output_vt   |
mjsousa@907:                     generate_c_vardecl_c::inoutput_vt |
mjsousa@907:                     generate_c_vardecl_c::eno_vt);
mjsousa@907:       vardecl->print(symbol->var_declarations_list);
mjsousa@907:       delete vardecl;
mjsousa@907:       
mjsousa@907:       s4o.print(s4o.indent_spaces + "return ");
mjsousa@907:       symbol->derived_function_name->accept(print_base);
mjsousa@907:       s4o.print(";\n");
mjsousa@907:       s4o.indent_left();
mjsousa@907:       s4o.print(s4o.indent_spaces + "}\n\n\n");
mjsousa@907:     
mjsousa@907:       return;
mjsousa@907:     }
mjsousa@907:     
mjsousa@907:     
mjsousa@907:     /*******************/
mjsousa@907:     /* Function Blocks */
mjsousa@907:     /*******************/
mjsousa@907:   public:
mjsousa@907:     /* NOTE: The following function will be called twice:
mjsousa@907:      *         1st time:  s4o will reference the .h file, and print_declaration=true
mjsousa@907:      *                     Here, we generate the function prototypes...
mjsousa@907:      *         2nd time:  s4o will reference the .c file, and print_declaration=false
mjsousa@907:      *                     Here we generate the source code!
mjsousa@907:      */
mjsousa@907:     /*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
mjsousa@907:     //SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused)
mjsousa@907:     static void handle_function_block(function_block_declaration_c *symbol, stage4out_c &s4o, bool print_declaration) {
mjsousa@907:       generate_c_vardecl_c     *vardecl;
mjsousa@907:       generate_c_sfcdecl_c     *sfcdecl;
mjsousa@907:       generate_c_base_c         print_base(&s4o);
mjsousa@907:       TRACE("function_block_declaration_c");
mjsousa@907:     
mjsousa@907:       /* (A) Function Block data structure declaration... */
mjsousa@907:       if (print_declaration) {
mjsousa@907:         /* (A.1) Data structure declaration */
mjsousa@907:         s4o.print("// FUNCTION_BLOCK ");
mjsousa@907:         symbol->fblock_name->accept(print_base);
mjsousa@907:         s4o.print("\n// Data part\n");
mjsousa@907:         s4o.print("typedef struct {\n");
mjsousa@907:         s4o.indent_right();
mjsousa@907: 
mjsousa@907:         /* (A.2) Public variables: i.e. the function parameters... */
mjsousa@907:         s4o.print(s4o.indent_spaces + "// FB Interface - IN, OUT, IN_OUT variables\n");
mjsousa@907:         vardecl = new generate_c_vardecl_c(&s4o,
mjsousa@907:                                            generate_c_vardecl_c::local_vf,
mjsousa@907:                                            generate_c_vardecl_c::input_vt    |
mjsousa@907:                                            generate_c_vardecl_c::output_vt   |
mjsousa@907:                                            generate_c_vardecl_c::inoutput_vt |
mjsousa@907:                                            generate_c_vardecl_c::en_vt       |
mjsousa@907:                                            generate_c_vardecl_c::eno_vt);
mjsousa@907:         vardecl->print(symbol->var_declarations);
mjsousa@907:         delete vardecl;
mjsousa@907:         s4o.print("\n");
mjsousa@907: 
mjsousa@907:         /* (A.3) Private internal variables */
mjsousa@907:         s4o.print(s4o.indent_spaces + "// FB private variables - TEMP, private and located variables\n");
mjsousa@907:         vardecl = new generate_c_vardecl_c(&s4o,
mjsousa@907:                                            generate_c_vardecl_c::local_vf,
mjsousa@907:                                            generate_c_vardecl_c::temp_vt    |
mjsousa@907:                                            generate_c_vardecl_c::private_vt |
mjsousa@907:                                            generate_c_vardecl_c::located_vt |
mjsousa@907:                                            generate_c_vardecl_c::external_vt);
mjsousa@907:         vardecl->print(symbol->var_declarations);
mjsousa@907:         delete vardecl;
mjsousa@907:         
mjsousa@907:         /* (A.4) Generate private internal variables for SFC */
mjsousa@907:         sfcdecl = new generate_c_sfcdecl_c(&s4o, symbol);
mjsousa@907:         sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::sfcdecl_sd);
mjsousa@907:         delete sfcdecl;
mjsousa@907:         s4o.print("\n");
mjsousa@907:       
mjsousa@907:         /* (A.5) Function Block data structure type name. */
mjsousa@907:         s4o.indent_left();
mjsousa@907:         s4o.print("} ");
mjsousa@907:         symbol->fblock_name->accept(print_base);
mjsousa@907:         s4o.print(";\n\n");
mjsousa@907:       }
mjsousa@907:       
mjsousa@907:       if (!print_declaration) {
mjsousa@907:         /* (A.6) Function Block inline function declaration for function invocation */
mjsousa@907:         generate_c_inlinefcall_c *inlinedecl = new generate_c_inlinefcall_c(&s4o, symbol->fblock_name, symbol, FB_FUNCTION_PARAM"->");
mjsousa@907:         symbol->fblock_body->accept(*inlinedecl);
mjsousa@907:         delete inlinedecl;
mjsousa@907:       }
mjsousa@907:       
mjsousa@907:       /* (B) Constructor */
mjsousa@907:       /* (B.1) Constructor name... */
mjsousa@907:       s4o.print(s4o.indent_spaces + "void ");
mjsousa@907:       symbol->fblock_name->accept(print_base);
mjsousa@907:       s4o.print(FB_INIT_SUFFIX);
mjsousa@907:       s4o.print("(");
mjsousa@907:     
mjsousa@907:       /* first and only parameter is a pointer to the data */
mjsousa@907:       symbol->fblock_name->accept(print_base);
mjsousa@907:       s4o.print(" *");
mjsousa@907:       s4o.print(FB_FUNCTION_PARAM);
mjsousa@907:       s4o.print(", BOOL retain)");
mjsousa@907: 
mjsousa@907:       if (print_declaration) {
mjsousa@907:         s4o.print(";\n");
mjsousa@907:       } else {
mjsousa@907:         s4o.print(" {\n");
mjsousa@907:         s4o.indent_right();
mjsousa@907:       
mjsousa@907:         /* (B.2) Member initializations... */
mjsousa@907:         s4o.print(s4o.indent_spaces);
mjsousa@907:         vardecl = new generate_c_vardecl_c(&s4o,
mjsousa@907:                                            generate_c_vardecl_c::constructorinit_vf,
mjsousa@907:                                            generate_c_vardecl_c::input_vt    |
mjsousa@907:                                            generate_c_vardecl_c::output_vt   |
mjsousa@907:                                            generate_c_vardecl_c::inoutput_vt |
mjsousa@907:                                            generate_c_vardecl_c::private_vt  |
mjsousa@907:                                            generate_c_vardecl_c::located_vt  |
mjsousa@907:                                            generate_c_vardecl_c::external_vt |
mjsousa@907:                                            generate_c_vardecl_c::en_vt       |
mjsousa@907:                                            generate_c_vardecl_c::eno_vt);
mjsousa@907:         vardecl->print(symbol->var_declarations, NULL, FB_FUNCTION_PARAM"->");
mjsousa@907:         delete vardecl;
mjsousa@907:         s4o.print("\n");
mjsousa@907:             
mjsousa@907:         /* (B.3) Generate private internal variables for SFC */
mjsousa@907:         sfcdecl = new generate_c_sfcdecl_c(&s4o, symbol, FB_FUNCTION_PARAM"->");
mjsousa@907:         sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::sfcinit_sd);
mjsousa@907:       
mjsousa@907:         s4o.indent_left();
mjsousa@907:         s4o.print(s4o.indent_spaces + "}\n\n");
mjsousa@920: 
mjsousa@907:         /* (C) Function with FB body */
mjsousa@907:         /* (C.1) Step definitions */
mjsousa@907:         sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::stepdef_sd);
mjsousa@907:       
mjsousa@907:         /* (C.2) Action definitions */
mjsousa@907:         sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::actiondef_sd);
mjsousa@907:         delete sfcdecl;
mjsousa@907:       }
mjsousa@907:       
mjsousa@907:       /* (C.3) Function declaration */
mjsousa@907:       s4o.print("// Code part\n");
mjsousa@907:       /* function interface */
mjsousa@907:       s4o.print("void ");
mjsousa@907:       symbol->fblock_name->accept(print_base);
mjsousa@907:       s4o.print(FB_FUNCTION_SUFFIX);
mjsousa@907:       s4o.print("(");
mjsousa@907:       /* first and only parameter is a pointer to the data */
mjsousa@907:       symbol->fblock_name->accept(print_base);
mjsousa@907:       s4o.print(" *");
mjsousa@907:       s4o.print(FB_FUNCTION_PARAM);
mjsousa@907:       s4o.print(")");
mjsousa@907: 
mjsousa@907:       if (print_declaration) {
mjsousa@907:         s4o.print(";\n");
mjsousa@907:       } else {
mjsousa@907:         s4o.print(" {\n");
mjsousa@907:         s4o.indent_right();
mjsousa@907:       
mjsousa@907:         s4o.print(s4o.indent_spaces + "// Control execution\n");
mjsousa@907:         s4o.print(s4o.indent_spaces + "if (!");
mjsousa@907:         s4o.print(GET_VAR);
mjsousa@907:         s4o.print("(");
mjsousa@907:         s4o.print(FB_FUNCTION_PARAM);
mjsousa@907:         s4o.print("->EN)) {\n");
mjsousa@907:         s4o.indent_right();
mjsousa@907:         s4o.print(s4o.indent_spaces);
mjsousa@907:         s4o.print(SET_VAR);
mjsousa@907:         s4o.print("(");
mjsousa@907:         s4o.print(FB_FUNCTION_PARAM);
mjsousa@907:         s4o.print("->,ENO,,__BOOL_LITERAL(FALSE));\n");
mjsousa@907:         s4o.print(s4o.indent_spaces + "return;\n");
mjsousa@907:         s4o.indent_left();
mjsousa@907:         s4o.print(s4o.indent_spaces + "}\n");
mjsousa@907:         s4o.print(s4o.indent_spaces + "else {\n");
mjsousa@907:         s4o.indent_right();
mjsousa@907:         s4o.print(s4o.indent_spaces);
mjsousa@907:         s4o.print(SET_VAR);
mjsousa@907:         s4o.print("(");
mjsousa@907:         s4o.print(FB_FUNCTION_PARAM);
mjsousa@907:         s4o.print("->,ENO,,__BOOL_LITERAL(TRUE));\n");
mjsousa@907:         s4o.indent_left();
mjsousa@907:         s4o.print(s4o.indent_spaces + "}\n");
mjsousa@907:       
mjsousa@907:         /* (C.4) Initialize TEMP variables */
mjsousa@907:         /* function body */
mjsousa@907:         s4o.print(s4o.indent_spaces + "// Initialise TEMP variables\n");
mjsousa@907:         vardecl = new generate_c_vardecl_c(&s4o,
mjsousa@907:                                            generate_c_vardecl_c::init_vf,
mjsousa@907:                                            generate_c_vardecl_c::temp_vt);
mjsousa@907:         vardecl->print(symbol->var_declarations, NULL,  FB_FUNCTION_PARAM"->");
mjsousa@907:         delete vardecl;
mjsousa@907:         s4o.print("\n");
mjsousa@907:       
mjsousa@907:         /* (C.5) Function code */
mjsousa@907:         generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol->fblock_name, symbol, FB_FUNCTION_PARAM"->");
mjsousa@907:         symbol->fblock_body->accept(generate_c_code);
mjsousa@907:         print_end_of_block_label(s4o);
mjsousa@907:         s4o.print(s4o.indent_spaces + "return;\n");
mjsousa@907:         s4o.indent_left();
mjsousa@907:         s4o.print(s4o.indent_spaces + "} // ");
mjsousa@907:         symbol->fblock_name->accept(print_base);
mjsousa@907:         s4o.print(FB_FUNCTION_SUFFIX);
mjsousa@907:         s4o.print(s4o.indent_spaces + "() \n\n");
mjsousa@907:       
mjsousa@907:         /* (C.6) Step undefinitions */
mjsousa@907:         sfcdecl = new generate_c_sfcdecl_c(&s4o, symbol, FB_FUNCTION_PARAM"->");
mjsousa@907:         sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::stepundef_sd);
mjsousa@907:       
mjsousa@907:         /* (C.7) Action undefinitions */
mjsousa@907:         sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::actionundef_sd);
mjsousa@907:         delete sfcdecl;
mjsousa@907:       
mjsousa@907:         s4o.indent_left();
mjsousa@907:         s4o.print("\n\n\n\n");
mjsousa@907:       }
mjsousa@907:       return;
mjsousa@907:     }
mjsousa@907:     
mjsousa@907:     
mjsousa@907:     /************/
mjsousa@907:     /* Programs */
mjsousa@907:     /************/
mjsousa@907:   public:
mjsousa@907:     /* NOTE: The following function will be called twice:
mjsousa@907:      *         1st time:  s4o will reference the .h file, and print_declaration=true
mjsousa@907:      *                     Here, we generate the function prototypes...
mjsousa@907:      *         2nd time:  s4o will reference the .c file, and print_declaration=false
mjsousa@907:      *                     Here we generate the source code!
mjsousa@907:      */
mjsousa@907:     /*  PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */
mjsousa@907:     //SYM_REF4(program_declaration_c, program_type_name, var_declarations, function_block_body, unused)
mjsousa@907:     static void handle_program(program_declaration_c *symbol, stage4out_c &s4o, bool print_declaration) {
mjsousa@907:       generate_c_vardecl_c     *vardecl;
mjsousa@907:       generate_c_sfcdecl_c     *sfcdecl;
mjsousa@907:       generate_c_base_c         print_base(&s4o);
mjsousa@907:       TRACE("program_declaration_c");
mjsousa@907:     
mjsousa@907:       /* (A) Program data structure declaration... */
mjsousa@907:       if (print_declaration) {      
mjsousa@907:         /* (A.1) Data structure declaration */
mjsousa@907:         s4o.print("// PROGRAM ");
mjsousa@907:         symbol->program_type_name->accept(print_base);
mjsousa@907:         s4o.print("\n// Data part\n");
mjsousa@907:         s4o.print("typedef struct {\n");
mjsousa@907:         s4o.indent_right();
mjsousa@907:       
mjsousa@907:         /* (A.2) Public variables: i.e. the program parameters... */
mjsousa@907:         s4o.print(s4o.indent_spaces + "// PROGRAM Interface - IN, OUT, IN_OUT variables\n");
mjsousa@907:         vardecl = new generate_c_vardecl_c(&s4o,
mjsousa@907:                                            generate_c_vardecl_c::local_vf,
mjsousa@907:                                            generate_c_vardecl_c::input_vt  |
mjsousa@907:                                            generate_c_vardecl_c::output_vt |
mjsousa@907:                                            generate_c_vardecl_c::inoutput_vt);
mjsousa@907:         vardecl->print(symbol->var_declarations);
mjsousa@907:         delete vardecl;
mjsousa@907:         s4o.print("\n");
lbessard@146:   
mjsousa@907:         /* (A.3) Private internal variables */
mjsousa@907:         s4o.print(s4o.indent_spaces + "// PROGRAM private variables - TEMP, private and located variables\n");
mjsousa@907:         vardecl = new generate_c_vardecl_c(&s4o,
mjsousa@907:                       generate_c_vardecl_c::local_vf,
mjsousa@907:                       generate_c_vardecl_c::temp_vt    |
mjsousa@907:                       generate_c_vardecl_c::private_vt |
mjsousa@907:                       generate_c_vardecl_c::located_vt |
mjsousa@907:                       generate_c_vardecl_c::external_vt);
mjsousa@907:         vardecl->print(symbol->var_declarations);
mjsousa@907:         delete vardecl;
mjsousa@907:       
mjsousa@907:         /* (A.4) Generate private internal variables for SFC */
mjsousa@907:         sfcdecl = new generate_c_sfcdecl_c(&s4o, symbol);
mjsousa@907:         sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::sfcdecl_sd);
mjsousa@907:         delete sfcdecl;
mjsousa@907:         s4o.print("\n");
mjsousa@907:         
mjsousa@907:         /* (A.5) Program data structure type name. */
mjsousa@907:         s4o.indent_left();
mjsousa@907:         s4o.print("} ");
mjsousa@907:         symbol->program_type_name->accept(print_base);
mjsousa@907:         s4o.print(";\n\n");
mjsousa@907:       }
mjsousa@907:       
mjsousa@907:       if (!print_declaration) {      
mjsousa@907:         /* (A.6) Function Block inline function declaration for function invocation */
mjsousa@907:         generate_c_inlinefcall_c *inlinedecl = new generate_c_inlinefcall_c(&s4o, symbol->program_type_name, symbol, FB_FUNCTION_PARAM"->");
mjsousa@907:         symbol->function_block_body->accept(*inlinedecl);
mjsousa@907:         delete inlinedecl;
mjsousa@907:       }
mjsousa@907:     
mjsousa@907:       /* (B) Constructor */
mjsousa@907:       /* (B.1) Constructor name... */
mjsousa@907:       s4o.print(s4o.indent_spaces + "void ");
mjsousa@907:       symbol->program_type_name->accept(print_base);
mjsousa@907:       s4o.print(FB_INIT_SUFFIX);
mjsousa@907:       s4o.print("(");
mjsousa@907:     
mjsousa@907:       /* first and only parameter is a pointer to the data */
mjsousa@907:       symbol->program_type_name->accept(print_base);
mjsousa@907:       s4o.print(" *");
mjsousa@907:       s4o.print(FB_FUNCTION_PARAM);
mjsousa@907:       s4o.print(", BOOL retain)");
mjsousa@907: 
mjsousa@907:       if (print_declaration) {
mjsousa@907:         s4o.print(";\n");
mjsousa@907:       } else {
mjsousa@907:         s4o.print(" {\n");
mjsousa@907:         s4o.indent_right();
mjsousa@907:       
mjsousa@907:         /* (B.2) Member initializations... */
mjsousa@907:         s4o.print(s4o.indent_spaces);
mjsousa@907:         vardecl = new generate_c_vardecl_c(&s4o,
mjsousa@907:                                            generate_c_vardecl_c::constructorinit_vf,
mjsousa@907:                                            generate_c_vardecl_c::input_vt    |
mjsousa@907:                                            generate_c_vardecl_c::output_vt   |
mjsousa@907:                                            generate_c_vardecl_c::inoutput_vt |
mjsousa@907:                                            generate_c_vardecl_c::private_vt  |
mjsousa@907:                                            generate_c_vardecl_c::located_vt  |
mjsousa@907:                                            generate_c_vardecl_c::external_vt);
mjsousa@907:         vardecl->print(symbol->var_declarations, NULL,  FB_FUNCTION_PARAM"->");
mjsousa@907:         delete vardecl;
mjsousa@907:         s4o.print("\n");
mjsousa@907:       
mjsousa@907:         /* (B.3) Generate private internal variables for SFC */
mjsousa@907:         sfcdecl = new generate_c_sfcdecl_c(&s4o, symbol, FB_FUNCTION_PARAM"->");
mjsousa@907:         sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::sfcinit_sd);
mjsousa@907:         delete sfcdecl;
mjsousa@907:       
mjsousa@907:         s4o.indent_left();
mjsousa@907:         s4o.print(s4o.indent_spaces + "}\n\n");
mjsousa@907:       }
mjsousa@907:     
mjsousa@907:       if (!print_declaration) {    
mjsousa@907:         /* (C) Function with PROGRAM body */
mjsousa@907:         /* (C.1) Step definitions */
mjsousa@907:         sfcdecl = new generate_c_sfcdecl_c(&s4o, symbol, FB_FUNCTION_PARAM"->");
mjsousa@907:         sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::stepdef_sd);
mjsousa@907:       
mjsousa@907:         /* (C.2) Action definitions */
mjsousa@907:         sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::actiondef_sd);
mjsousa@907:         delete sfcdecl;
mjsousa@907:       }
mjsousa@907:       
mjsousa@907:       /* (C.3) Function declaration */
mjsousa@907:       s4o.print("// Code part\n");
mjsousa@907:       /* function interface */
mjsousa@907:       s4o.print("void ");
mjsousa@907:       symbol->program_type_name->accept(print_base);
mjsousa@907:       s4o.print(FB_FUNCTION_SUFFIX);
mjsousa@907:       s4o.print("(");
mjsousa@907:       /* first and only parameter is a pointer to the data */
mjsousa@907:       symbol->program_type_name->accept(print_base);
mjsousa@907:       s4o.print(" *");
mjsousa@907:       s4o.print(FB_FUNCTION_PARAM);
mjsousa@907:       s4o.print(")");
mjsousa@907: 
mjsousa@907:       if (print_declaration) {
mjsousa@907:         s4o.print(";\n");
mjsousa@907:       } else {
mjsousa@907:         s4o.print(" {\n");
mjsousa@907:         s4o.indent_right();
mjsousa@907:           
mjsousa@907:         /* (C.4) Initialize TEMP variables */
mjsousa@907:         /* function body */
mjsousa@907:         s4o.print(s4o.indent_spaces + "// Initialise TEMP variables\n");
mjsousa@907:         vardecl = new generate_c_vardecl_c(&s4o,
mjsousa@907:                                            generate_c_vardecl_c::init_vf,
mjsousa@907:                                            generate_c_vardecl_c::temp_vt);
mjsousa@907:         vardecl->print(symbol->var_declarations, NULL,  FB_FUNCTION_PARAM"->");
mjsousa@907:         delete vardecl;
mjsousa@907:         s4o.print("\n");
mjsousa@907:       
mjsousa@907:         /* (C.5) Function code */
mjsousa@907:         generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol->program_type_name, symbol, FB_FUNCTION_PARAM"->");
mjsousa@907:         symbol->function_block_body->accept(generate_c_code);
mjsousa@907:         print_end_of_block_label(s4o);
mjsousa@907:         s4o.print(s4o.indent_spaces + "return;\n");
mjsousa@907:         s4o.indent_left();
mjsousa@907:         s4o.print(s4o.indent_spaces + "} // ");
mjsousa@907:         symbol->program_type_name->accept(print_base);
mjsousa@907:         s4o.print(FB_FUNCTION_SUFFIX);
mjsousa@907:         s4o.print(s4o.indent_spaces + "() \n\n");
mjsousa@907:       
mjsousa@907:         /* (C.6) Step undefinitions */
mjsousa@907:         sfcdecl = new generate_c_sfcdecl_c(&s4o, symbol, FB_FUNCTION_PARAM"->");
mjsousa@907:         sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::stepundef_sd);
mjsousa@907:         
mjsousa@907:         /* (C.7) Action undefinitions */
mjsousa@907:         sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::actionundef_sd); 
mjsousa@907:         delete sfcdecl;
mjsousa@907:       
mjsousa@907:         s4o.indent_left();
mjsousa@907:         s4o.print("\n\n\n\n");
mjsousa@907:       }  
mjsousa@907:       return;
mjsousa@907:     }
lbessard@70: }; /* generate_c_pous_c */
lbessard@70: 
mjsousa@907:     
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: 
mjsousa@860: class generate_c_config_c: public generate_c_base_c {
msousa@267:     private:
mjsousa@860:     stage4out_c &s4o_incl;
laurent@396:     
lbessard@70:     public:
laurent@396:     generate_c_config_c(stage4out_c *s4o_ptr, stage4out_c *s4o_incl_ptr)
mjsousa@860:       : generate_c_base_c(s4o_ptr), s4o_incl(*s4o_incl_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: /********************/
laurent@396: void *visit(enable_code_generation_pragma_c * symbol)   {
mjsousa@860:     s4o.enable_output();
mjsousa@860:     s4o_incl.enable_output();
laurent@396:     return NULL;
laurent@396: }
laurent@396: 
laurent@396: void *visit(disable_code_generation_pragma_c * symbol)  {
mjsousa@860:     s4o.disable_output();
mjsousa@860:     s4o_incl.disable_output();    
laurent@396:     return NULL;
laurent@396: }
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,
laurent@392:                                      generate_c_vardecl_c::global_vt,
laurent@392:                                      symbol->configuration_name);
lbessard@70:   vardecl->print(symbol);
lbessard@70:   delete vardecl;
lbessard@70:   s4o.print("\n");
lbessard@70: 
laurent@396:   /* (A.3) Declare global prototypes in include file */
laurent@396:   vardecl = new generate_c_vardecl_c(&s4o_incl,
laurent@396:                                      generate_c_vardecl_c::globalprototype_vf,
laurent@396:                                      generate_c_vardecl_c::global_vt,
laurent@396:                                      symbol->configuration_name);
laurent@396:   vardecl->print(symbol);
laurent@396:   delete vardecl;
laurent@396:   s4o_incl.print("\n");
laurent@396: 
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,
manuele@537:                                      generate_c_vardecl_c::global_vt,
manuele@537:                                      symbol->configuration_name);
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... */
laurent@396:     symbol_c *current_configuration;
lbessard@70:     symbol_c *current_resource_name;
lbessard@184:     symbol_c *current_task_name;
lbessard@70:     symbol_c *current_global_vars;
laurent@396:     bool configuration_name;
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) {
laurent@396:       current_configuration = config_scope;
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;
laurent@396:       configuration_name = false;
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: 
msousa@594:     unsigned long 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: 
laurent@396:     /*************************/
laurent@396:     /* B.1 - Common elements */
laurent@396:     /*************************/
laurent@396:     /*******************************************/
laurent@396:     /* B 1.1 - Letters, digits and identifiers */
laurent@396:     /*******************************************/
laurent@396:     
laurent@396:     void *visit(identifier_c *symbol) {
laurent@396:         if (configuration_name)
laurent@396:           s4o.print(symbol->value);
laurent@396:         else
laurent@396:           generate_c_base_c::visit(symbol);
laurent@396:         return NULL;
laurent@396:     }
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: 
laurent@396:     /********************************/
laurent@396:     /* B 1.7 Configuration elements */
laurent@396:     /********************************/
laurent@396: 
laurent@396:     void *visit(configuration_declaration_c *symbol) {
laurent@396:       return symbol->configuration_name->accept(*this);
laurent@396:     }
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@396:       s4o.print("#include \"accessor.h\"\n");
laurent@397:       s4o.print("#include \"POUS.h\"\n\n");
laurent@396:       s4o.print("#include \"");
laurent@396:       configuration_name = true;
laurent@396:       current_configuration->accept(*this);
laurent@396:       configuration_name = false;
laurent@396:       s4o.print(".h\"\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@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@392:             s4o.print(s4o.indent_spaces + "{");
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;
lbessard@184:               config_var_decl->accept(*this);
lbessard@184:             }
laurent@248:             else {
laurent@392:               res_var_decl->accept(*this);
laurent@248:             }
laurent@392:             s4o.print("* ");
laurent@392:             symbol->single_data_source->accept(*this);
laurent@392:             s4o.print(" = __GET_GLOBAL_");
laurent@392:             symbol->single_data_source->accept(*this);
laurent@392:             s4o.print("();");
laurent@248:             s4o.print(SET_VAR);
laurent@248:             s4o.print("(");
lbessard@184:             current_task_name->accept(*this);
mjsousa@885:             s4o.print("_R_TRIG.,CLK,, *");
lbessard@184:             symbol->single_data_source->accept(*this);
laurent@392:             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) {
msousa@594:               unsigned long long int time = calculate_time(symbol->interval_data_source);
lbessard@184:               if (time != 0) {
lbessard@184:                 s4o.print("!(tick % ");
msousa@594:                 s4o.print(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
msousa@417:             vartype = search_config_instance->get_vartype(current_var_reference);
lbessard@70:         }
lbessard@70:         else
msousa@417:           vartype = search_resource_instance->get_vartype(current_var_reference);
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(" = ");
msousa@506:         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
msousa@417:             vartype = search_config_instance->get_vartype(current_var_reference);
lbessard@70:         }
lbessard@70:         else
msousa@417:           vartype = search_resource_instance->get_vartype(current_var_reference);
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("; ");
msousa@506:         if (vartype == search_var_instance_decl_c::global_vt)
lbessard@70:           s4o.print("*");
lbessard@70:         symbol->data_sink->accept(*this);
lbessard@70:         s4o.print(" = ");
ccb@202:         s4o.printupper(current_program_name);
lbessard@70:         s4o.print(".");
lbessard@70:         symbol->symbolic_variable->accept(*this);
ccb@202:         s4o.print(";};\n");
lbessard@70:       }
lbessard@70:       return NULL;
lbessard@70:     }
lbessard@70: 
lbessard@70: };
lbessard@70: 
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: 
lbessard@70: class generate_c_c: public iterator_visitor_c {
lbessard@70:   protected:
mjsousa@913:     stage4out_c                      &s4o;
mjsousa@913:     stage4out_c                  pous_s4o;
mjsousa@913:     stage4out_c             pous_incl_s4o;
mjsousa@913:     stage4out_c     located_variables_s4o;
mjsousa@913:     stage4out_c             variables_s4o;
mjsousa@913:     
laurent@377:     generate_c_datatypes_c generate_c_datatypes;
mjsousa@913:     generate_c_typedecl_c  generate_c_typedecl;
mjsousa@913:     generate_c_pous_c      generate_c_pous;
mjsousa@913:     
mjsousa@913:     symbol_c   *current_configuration;
lbessard@70: 
lbessard@70:     const char *current_name;
lbessard@70:     const char *current_builddir;
lbessard@70: 
mjsousa@908:     bool        allow_output;
mjsousa@908:     
laurent@237:     unsigned long long common_ticktime;
lbessard@70: 
lbessard@70:   public:
lbessard@70:     generate_c_c(stage4out_c *s4o_ptr, const char *builddir): 
lbessard@70:             s4o(*s4o_ptr),
lbessard@70:             pous_s4o(builddir, "POUS", "c"),
lbessard@121:             pous_incl_s4o(builddir, "POUS", "h"),
lbessard@70:             located_variables_s4o(builddir, "LOCATED_VARIABLES","h"),
etisserant@112:             variables_s4o(builddir, "VARIABLES","csv"),
mjsousa@913:             generate_c_datatypes(&pous_incl_s4o),
mjsousa@913:             generate_c_typedecl (&pous_incl_s4o)
mjsousa@907:     {
lbessard@70:       current_builddir = builddir;
lbessard@70:       current_configuration = NULL;
mjsousa@908:       allow_output = true;
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();  
mjsousa@908:       allow_output = true;      
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();  
mjsousa@908:       allow_output = false;      
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) {
mjsousa@917:       pous_incl_s4o.print("#ifndef __POUS_H\n#define __POUS_H\n\n#include \"accessor.h\"\n#include \"iec_std_lib.h\"\n\n");
laurent@377: 
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);
laurent@397:       variables_s4o.print("\n// Ticktime\n");
laurent@397:       variables_s4o.print_long_long_integer(common_ticktime, false);
laurent@397:       variables_s4o.print("\n");
laurent@397: 
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 */
mjsousa@913: //   void *visit(data_type_declaration_c *symbol)  // handled by iterator_visitor_c
mjsousa@913: 
mjsousa@913:     /* helper symbol for data_type_declaration */
mjsousa@913:     void *visit(type_declaration_list_c *symbol) {
mjsousa@913:       for(int i = 0; i < symbol->n; i++) {
mjsousa@913:         symbol->elements[i]->accept(generate_c_datatypes);
mjsousa@913:         symbol->elements[i]->accept(generate_c_typedecl);
mjsousa@913:       }
lbessard@98:       return NULL;
lbessard@98:     }
lbessard@98: 
lbessard@70: /**************************************/
lbessard@70: /* B.1.5 - Program organization units */
lbessard@70: /**************************************/
mjsousa@918: #define handle_pou(fname,pname) \
mjsousa@908:       if (!allow_output) return NULL;\
mjsousa@908:       if (generate_pou_filepairs__) {\
mjsousa@918: 	const char *pou_name = get_datatype_info_c::get_id_str(pname);\
mjsousa@918:         stage4out_c s4o_c(current_builddir, pou_name, "c");\
mjsousa@918:         stage4out_c s4o_h(current_builddir, pou_name, "h");\
mjsousa@918:         s4o_c.print("#include \""); s4o_c.print(pou_name); s4o_c.print(".h\"\n");\
mjsousa@918:         s4o_h.print("#ifndef __");  s4o_h.print(pou_name); s4o_h.print("_H\n");\
mjsousa@918:         s4o_h.print("#define __");  s4o_h.print(pou_name); s4o_h.print("_H\n");\
mjsousa@915:         generate_c_datatypes_c generate_c_datatypes__(&s4o_h);\
mjsousa@918:         symbol->accept(generate_c_datatypes__); /* generate implicitly delcared datatypes (arrays and ref_to) */\
mjsousa@908:         generate_c_pous_c::fname(symbol, s4o_h, true); /* generate the <pou_name>.h file */\
mjsousa@908:         generate_c_pous_c::fname(symbol, s4o_c, false);/* generate the <pou_name>.c file */\
mjsousa@918:         s4o_h.print("#endif /* __");  s4o_h.print(pou_name); s4o_h.print("_H */\n");\
mjsousa@908:         /* add #include directives to the POUS.h and POUS.c files... */\
mjsousa@908:         pous_incl_s4o.print("#include \"");\
mjsousa@908:         pous_s4o.     print("#include \"");\
mjsousa@918:         pous_incl_s4o.print(pou_name);\
mjsousa@918:         pous_s4o.     print(pou_name);\
mjsousa@908:         pous_incl_s4o.print(".h\"\n");\
mjsousa@908:         pous_s4o.     print(".c\"\n");\
mjsousa@908:       } else {\
mjsousa@918:         symbol->accept(generate_c_datatypes);\
mjsousa@908:         generate_c_pous_c::fname(symbol, pous_incl_s4o, true);\
mjsousa@908:         generate_c_pous_c::fname(symbol, pous_s4o,      false);\
mjsousa@908:       }
mjsousa@908: 
lbessard@70: /***********************/
lbessard@70: /* B 1.5.1 - Functions */
mjsousa@908: /***********************/      
lbessard@70:     void *visit(function_declaration_c *symbol) {
mjsousa@918:       handle_pou(handle_function,symbol->derived_function_name)
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) {
mjsousa@918:       handle_pou(handle_function_block,symbol->fblock_name)
mjsousa@907:       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) {
mjsousa@918:       handle_pou(handle_program,symbol->program_type_name)
mjsousa@907:       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) {
mjsousa@907:       if (symbol->global_var_declarations != NULL)
mjsousa@907:         symbol->global_var_declarations->accept(generate_c_datatypes);
mjsousa@907:       static int configuration_count = 0;
mjsousa@907: 
mjsousa@907:       if (configuration_count++) {
mjsousa@907:         /* the first configuration is the one we will use!! */
mjsousa@907:         STAGE4_ERROR(symbol, symbol, "A previous CONFIGURATION has already been declared (C code generation currently only allows a single configuration).");
mjsousa@907:         ERROR;
mjsousa@907:       }
mjsousa@907: 
mjsousa@907:       current_configuration = symbol;
mjsousa@907: 
mjsousa@907:       {
mjsousa@907:         calculate_common_ticktime_c calculate_common_ticktime;
mjsousa@907:         symbol->accept(calculate_common_ticktime);
mjsousa@907:         common_ticktime = calculate_common_ticktime.get_common_ticktime();
mjsousa@907:         if (common_ticktime == 0) {
mjsousa@907:           STAGE4_ERROR(symbol, symbol, "You must define at least one periodic task (to set cycle period)!");
mjsousa@907:           ERROR;
mjsousa@907:         }
mjsousa@907: 
mjsousa@907:         symbol->configuration_name->accept(*this);
mjsousa@907:         
mjsousa@907:         stage4out_c config_s4o(current_builddir, current_name, "c");
mjsousa@907:         stage4out_c config_incl_s4o(current_builddir, current_name, "h");
mjsousa@907:         generate_c_config_c generate_c_config(&config_s4o, &config_incl_s4o);
mjsousa@907:         symbol->accept(generate_c_config);
mjsousa@907: 
mjsousa@907:         config_s4o.print("unsigned long long common_ticktime__ = ");
mjsousa@907:         config_s4o.print_long_long_integer(common_ticktime);
mjsousa@907:         config_s4o.print("; /*ns*/\n");
mjsousa@907:         config_s4o.print("unsigned long greatest_tick_count__ = ");
mjsousa@907:         config_s4o.print_long_integer(calculate_common_ticktime.get_greatest_tick_count());
mjsousa@907:         config_s4o.print("; /*tick*/\n");
mjsousa@907:       }
mjsousa@907: 
mjsousa@907:       symbol->resource_declarations->accept(*this);
mjsousa@907: 
mjsousa@907:       current_configuration = NULL;
laurent@377:       return NULL;
laurent@377:     }
laurent@377: 
laurent@377:     void *visit(resource_declaration_c *symbol) {
mjsousa@907:       if (symbol->global_var_declarations != NULL)
mjsousa@907:         symbol->global_var_declarations->accept(generate_c_datatypes);
mjsousa@907:       symbol->resource_name->accept(*this);
mjsousa@907:       stage4out_c resources_s4o(current_builddir, current_name, "c");
mjsousa@907:       generate_c_resources_c generate_c_resources(&resources_s4o, current_configuration, symbol, common_ticktime);
mjsousa@907:       symbol->accept(generate_c_resources);
ccb@202:       return NULL;
lbessard@70:     }
lbessard@70: 
lbessard@70:     void *visit(single_resource_declaration_c *symbol) {
mjsousa@907:       stage4out_c resources_s4o(current_builddir, "RESOURCE", "c");
mjsousa@907:       generate_c_resources_c generate_c_resources(&resources_s4o, current_configuration, symbol, common_ticktime);
mjsousa@907:       symbol->accept(generate_c_resources);
ccb@202:       return NULL;
lbessard@70:     }
lbessard@70:     
lbessard@70: };
lbessard@70: 
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: 
lbessard@70: 
lbessard@70: 
lbessard@70: 
lbessard@70: visitor_c *new_code_generator(stage4out_c *s4o, const char *builddir)  {return new generate_c_c(s4o, builddir);}
lbessard@70: void delete_code_generator(visitor_c *code_generator) {delete code_generator;}
lbessard@70: 
lbessard@70: