stage4/generate_c/generate_c_base.cc
changeset 258 d7d92b2f87e9
parent 257 90782e241346
parent 240 f78fa87bb4cb
child 267 0a1204bcc9af
equal deleted inserted replaced
257:90782e241346 258:d7d92b2f87e9
    49 
    49 
    50 
    50 
    51 
    51 
    52 typedef struct
    52 typedef struct
    53 {
    53 {
       
    54   symbol_c *param_name;
    54   symbol_c *param_value;
    55   symbol_c *param_value;
    55   symbol_c *param_type;
    56   symbol_c *param_type;
    56   function_param_iterator_c::param_direction_t param_direction;
    57   function_param_iterator_c::param_direction_t param_direction;
    57 } FUNCTION_PARAM;
    58 } FUNCTION_PARAM;
    58 
    59 
    59 #define ADD_PARAM_LIST(value, type, direction)\
    60 #define DECLARE_PARAM_LIST()\
       
    61   std::list<FUNCTION_PARAM*> param_list;\
       
    62   std::list<FUNCTION_PARAM*>::iterator pt;\
       
    63   FUNCTION_PARAM *param;
       
    64 
       
    65 #define ADD_PARAM_LIST(name, value, type, direction)\
    60   param = new FUNCTION_PARAM;\
    66   param = new FUNCTION_PARAM;\
       
    67   param->param_name = name;\
    61   param->param_value = value;\
    68   param->param_value = value;\
    62   param->param_type = type;\
    69   param->param_type = type;\
    63   param->param_direction = direction;\
    70   param->param_direction = direction;\
    64   param_list.push_back(*param);
    71   param_list.push_back(param);
    65 
    72 
       
    73 #define PARAM_LIST_ITERATOR() for(pt = param_list.begin(); pt != param_list.end(); pt++)
       
    74 
       
    75 #define PARAM_NAME (*pt)->param_name
       
    76 #define PARAM_VALUE (*pt)->param_value
       
    77 #define PARAM_TYPE (*pt)->param_type
       
    78 #define PARAM_DIRECTION (*pt)->param_direction
       
    79 
       
    80 #define CLEAR_PARAM_LIST()\
       
    81   PARAM_LIST_ITERATOR()\
       
    82     delete *pt;\
       
    83   param_list.clear();
    66 
    84 
    67 
    85 
    68 class generate_c_base_c: public iterator_visitor_c {
    86 class generate_c_base_c: public iterator_visitor_c {
    69 
    87 
    70   protected:
    88   protected:
    71     stage4out_c &s4o;
    89     stage4out_c &s4o;
    72 
    90 
    73   private:
    91   private:
    74     /* Unlike programs that are mapped onto C++ classes, Function Blocks are mapped onto a data structure type
    92     /* Unlike programs that are mapped onto C++ classes, Function Blocks are mapped onto a data structure type
    75      * and a separate function conatining the code. This function is passed a pointer to an instance of the data
    93      * and a separate function containing the code. This function is passed a pointer to an instance of the data
    76      * structure. This means that the code inside the functions must insert a pointer to the data structure whenever
    94      * structure. This means that the code inside the functions must insert a pointer to the data structure whenever
    77      * it wishes to access a Function Block variable.
    95      * it wishes to access a Function Block variable.
    78      * The variable_prefix_ variable will contain the correct string which needs to be prefixed to all variable accesses.
    96      * The variable_prefix_ variable will contain the correct string which needs to be prefixed to all variable accesses.
    79      * This string is set with the set_variable_prefix() member function.
    97      * This string is set with the set_variable_prefix() member function.
    80      */
    98      */
    81     const char *variable_prefix_;
    99     const char *variable_prefix_;
    82 
   100 
    83 
       
    84 
       
    85   public:
   101   public:
    86     generate_c_base_c(stage4out_c *s4o_ptr): s4o(*s4o_ptr) {variable_prefix_ = NULL;}
   102     generate_c_base_c(stage4out_c *s4o_ptr): s4o(*s4o_ptr) {
       
   103       variable_prefix_ = NULL;
       
   104     }
    87     ~generate_c_base_c(void) {}
   105     ~generate_c_base_c(void) {}
    88 
   106 
    89     void set_variable_prefix(const char *variable_prefix) {variable_prefix_ = variable_prefix;}
   107     void set_variable_prefix(const char *variable_prefix) {variable_prefix_ = variable_prefix;}
    90     const char *get_variable_prefix(void) {return variable_prefix_;}
   108     const char *get_variable_prefix(void) {return variable_prefix_;}
    91     bool is_variable_prefix_null(void) {return variable_prefix_ == NULL;}
   109     bool is_variable_prefix_null(void) {return variable_prefix_ == NULL;}
   214       s4o.print("(__BOOL_LITERAL(TRUE), NULL, 2, ");
   232       s4o.print("(__BOOL_LITERAL(TRUE), NULL, 2, ");
   215       l_exp->accept(*this);
   233       l_exp->accept(*this);
   216       s4o.print(", ");
   234       s4o.print(", ");
   217       r_exp->accept(*this);
   235       r_exp->accept(*this);
   218       s4o.print(")");
   236       s4o.print(")");
       
   237       return NULL;
       
   238     }
       
   239 
       
   240     void *print_check_function(symbol_c *type,
       
   241           symbol_c *value,
       
   242           symbol_c *fb_name = NULL,
       
   243           bool temp = false) {
       
   244       search_base_type_c search_base_type;
       
   245       bool is_subrange = search_base_type.type_is_subrange(type);
       
   246       if (is_subrange) {
       
   247 		s4o.print("__CHECK_");
       
   248 		type->accept(*this);
       
   249 		s4o.print("(");
       
   250       }
       
   251       if (fb_name != NULL) {
       
   252         s4o.print(GET_VAR);
       
   253         s4o.print("(");
       
   254         print_variable_prefix();
       
   255         fb_name->accept(*this);
       
   256         s4o.print(".");
       
   257         value->accept(*this);
       
   258         s4o.print(")");
       
   259       }
       
   260       else {
       
   261         if (temp)
       
   262     	  s4o.print(TEMP_VAR);
       
   263         value->accept(*this);
       
   264       }
       
   265       if (is_subrange)
       
   266         s4o.print(")");
   219       return NULL;
   267       return NULL;
   220     }
   268     }
   221 
   269 
   222 /***************************/
   270 /***************************/
   223 /* 2.1.6 - Pragmas */
   271 /* 2.1.6 - Pragmas */
   345           ERROR;
   393           ERROR;
   346         } /* switch() */
   394         } /* switch() */
   347       } /* for() */
   395       } /* for() */
   348 
   396 
   349       str += '"';
   397       str += '"';
   350       s4o.print("(STRING){");
   398       s4o.print("__STRING_LITERAL(");
   351       s4o.print_integer(count); 
   399       s4o.print_integer(count); 
   352       s4o.print(",");
   400       s4o.print(",");
   353       s4o.print(str);
   401       s4o.print(str);
   354       s4o.print("}");
   402       s4o.print(")");
   355       return NULL;
   403       return NULL;
   356     }
   404     }
   357 
   405 
   358 
   406 
   359 /***************************/
   407 /***************************/