stage4/generate_c/generate_c.cc
changeset 217 f5dfadf5de54
parent 210 8387cac2aba6
child 221 c6aed7e5f070
equal deleted inserted replaced
216:136d6ae70745 217:f5dfadf5de54
   120 #define FB_FUNCTION_PARAM "data__"
   120 #define FB_FUNCTION_PARAM "data__"
   121 
   121 
   122 
   122 
   123 #define SFC_STEP_ACTION_PREFIX "__SFC_"
   123 #define SFC_STEP_ACTION_PREFIX "__SFC_"
   124 
   124 
   125 /***********************************************************************/
   125 
   126 /***********************************************************************/
   126 /* Variable declaration symbol for accessor macros */
   127 /***********************************************************************/
   127 #define DECLARE_VAR "__DECLARE_VAR"
   128 /***********************************************************************/
   128 #define DECLARE_GLOBAL "__DECLARE_GLOBAL"
   129 
   129 #define DECLARE_GLOBAL_LOCATION "__DECLARE_GLOBAL_LOCATION"
   130 #include "generate_c_base.cc"
   130 #define DECLARE_GLOBAL_LOCATED "__DECLARE_GLOBAL_LOCATED"
   131 #include "generate_c_typedecl.cc"
   131 #define DECLARE_EXTERNAL "__DECLARE_EXTERNAL"
   132 #include "generate_c_sfcdecl.cc"
   132 #define DECLARE_LOCATED "__DECLARE_LOCATED"
   133 #include "generate_c_vardecl.cc"
   133 
   134 #include "generate_c_configbody.cc"
   134 /* Variable declaration symbol for accessor macros */
   135 #include "generate_location_list.cc"
   135 #define INIT_VAR "__INIT_VAR"
   136 #include "generate_var_list.cc"
   136 #define INIT_GLOBAL "__INIT_GLOBAL"
   137 
   137 #define INIT_GLOBAL_LOCATED "__INIT_GLOBAL_LOCATED"
   138 /***********************************************************************/
   138 #define INIT_EXTERNAL "__INIT_EXTERNAL"
   139 /***********************************************************************/
   139 #define INIT_LOCATED "__INIT_LOCATED"
   140 /***********************************************************************/
   140 #define INIT_LOCATED_VALUE "__INIT_LOCATED_VALUE"
   141 /***********************************************************************/
   141 
       
   142 
       
   143 /* Variable getter symbol for accessor macros */
       
   144 #define GET_VAR "__GET_VAR"
       
   145 #define GET_EXTERNAL "__GET_EXTERNAL"
       
   146 #define GET_LOCATED "__GET_LOCATED"
       
   147 #define GET_VAR_BY_REF "__GET_VAR_BY_REF"
       
   148 #define GET_EXTERNAL_BY_REF "__GET_EXTERNAL_BY_REF"
       
   149 #define GET_LOCATED_BY_REF "__GET_LOCATED_BY_REF"
       
   150 
       
   151 /* Variable setter symbol for accessor macros */
       
   152 #define SET_VAR "__SET_VAR"
       
   153 #define SET_EXTERNAL "__SET_EXTERNAL"
       
   154 #define SET_LOCATED "__SET_LOCATED"
       
   155 
   142 
   156 
   143 /* Generate a name for a temporary variable.
   157 /* Generate a name for a temporary variable.
   144  * Each new name generated is appended a different number,
   158  * Each new name generated is appended a different number,
   145  * starting off from 0.
   159  * starting off from 0.
   146  * After calling reset(), the names will start off again from 0.
   160  * After calling reset(), the names will start off again from 0.
   147  */
   161  */
   148 #define VAR_LEADER "__"
   162 #define VAR_LEADER "__"
   149 #define TEMP_VAR VAR_LEADER "TMP_"
   163 #define TEMP_VAR VAR_LEADER "TMP_"
   150 #define SOURCE_VAR VAR_LEADER "SRC_"
   164 #define SOURCE_VAR VAR_LEADER "SRC_"
   151 
   165 
       
   166 /***********************************************************************/
       
   167 /***********************************************************************/
       
   168 /***********************************************************************/
       
   169 /***********************************************************************/
       
   170 
       
   171 #include "generate_c_base.cc"
       
   172 #include "generate_c_typedecl.cc"
       
   173 #include "generate_c_sfcdecl.cc"
       
   174 #include "generate_c_vardecl.cc"
       
   175 #include "generate_c_configbody.cc"
       
   176 #include "generate_location_list.cc"
       
   177 #include "generate_var_list.cc"
       
   178 
       
   179 /***********************************************************************/
       
   180 /***********************************************************************/
       
   181 /***********************************************************************/
       
   182 /***********************************************************************/
       
   183 
   152 #include "generate_c_st.cc"
   184 #include "generate_c_st.cc"
   153 #include "generate_c_il.cc"
   185 #include "generate_c_il.cc"
       
   186 #include "generate_c_inlinefcall.cc"
   154 
   187 
   155 #include "generate_c.hh"
   188 #include "generate_c.hh"
   156 
   189 
   157 /***********************************************************************/
   190 /***********************************************************************/
   158 /***********************************************************************/
   191 /***********************************************************************/
   318 /***********************************************************************/
   351 /***********************************************************************/
   319 /***********************************************************************/
   352 /***********************************************************************/
   320 /***********************************************************************/
   353 /***********************************************************************/
   321 /***********************************************************************/
   354 /***********************************************************************/
   322 
   355 
   323 /* A helper class that knows how to generate code for both the IL and ST languages... */
   356 /* A helper class that knows how to generate code for the SFC, IL and ST languages... */
   324 class generate_c_SFC_IL_ST_c: public null_visitor_c {
   357 class generate_c_SFC_IL_ST_c: public null_visitor_c {
   325   private:
   358   private:
   326     stage4out_c *s4o_ptr;
   359     stage4out_c *s4o_ptr;
   327     symbol_c *scope;
   360     symbol_c *scope;
       
   361     symbol_c *fbname;
   328     const char *variable_prefix;
   362     const char *variable_prefix;
   329 
   363 
   330   public:
   364   public:
   331     generate_c_SFC_IL_ST_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL);
   365     generate_c_SFC_IL_ST_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL);
   332     /*********************************************/
   366     /*********************************************/
   333     /* B.1.6  Sequential function chart elements */
   367     /* B.1.6  Sequential function chart elements */
   334     /*********************************************/
   368     /*********************************************/
   335     
   369     
   336     /*| sequential_function_chart sfc_network*/
   370     /*| sequential_function_chart sfc_network*/
   364 /* Remainder implemented in generate_c_st_c... */
   398 /* Remainder implemented in generate_c_st_c... */
   365 };
   399 };
   366 
   400 
   367 #include "generate_c_sfc.cc"
   401 #include "generate_c_sfc.cc"
   368 
   402 
   369 generate_c_SFC_IL_ST_c::generate_c_SFC_IL_ST_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix) {
   403 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) {
   370   if (NULL == scope) ERROR;
   404   if (NULL == scope) ERROR;
   371   this->s4o_ptr = s4o_ptr;
   405   this->s4o_ptr = s4o_ptr;
   372   this->scope = scope;
   406   this->scope = scope;
       
   407   this->fbname = name;
   373   this->variable_prefix = variable_prefix;
   408   this->variable_prefix = variable_prefix;
   374 }
   409 }
   375 
   410 
   376 void *generate_c_SFC_IL_ST_c::visit(sequential_function_chart_c * symbol) {
   411 void *generate_c_SFC_IL_ST_c::visit(sequential_function_chart_c * symbol) {
   377   generate_c_sfc_c generate_c_sfc(s4o_ptr, scope, variable_prefix);
   412   generate_c_sfc_c generate_c_sfc(s4o_ptr, fbname, scope, variable_prefix);
   378   generate_c_sfc.generate(symbol);
   413   generate_c_sfc.generate(symbol);
   379   return NULL;
   414   return NULL;
   380 }
   415 }
   381 
   416 
   382 void *generate_c_SFC_IL_ST_c::visit(instruction_list_c *symbol) {
   417 void *generate_c_SFC_IL_ST_c::visit(instruction_list_c *symbol) {
   383   generate_c_il_c generate_c_il(s4o_ptr, scope, variable_prefix);
   418   generate_c_il_c generate_c_il(s4o_ptr, fbname, scope, variable_prefix);
   384   generate_c_il.generate(symbol);
   419   generate_c_il.generate(symbol);
   385   return NULL;
   420   return NULL;
   386 }
   421 }
   387 
   422 
   388 void *generate_c_SFC_IL_ST_c::visit(statement_list_c *symbol) {
   423 void *generate_c_SFC_IL_ST_c::visit(statement_list_c *symbol) {
   389   generate_c_st_c generate_c_st(s4o_ptr, scope, variable_prefix);
   424   generate_c_st_c generate_c_st(s4o_ptr, fbname, scope, variable_prefix);
   390   generate_c_st.generate(symbol);
   425   generate_c_st.generate(symbol);
   391   return NULL;
   426   return NULL;
   392 }
   427 }
   393 
   428 
   394 
   429 
   565   s4o.print(";\n");
   600   s4o.print(";\n");
   566   s4o.indent_left();
   601   s4o.indent_left();
   567   s4o.print(s4o.indent_spaces + "}\n");
   602   s4o.print(s4o.indent_spaces + "}\n");
   568 
   603 
   569   /* (C) Function body */
   604   /* (C) Function body */
   570   generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol);
   605   generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol->derived_function_name, symbol);
   571   symbol->function_body->accept(generate_c_code);
   606   symbol->function_body->accept(generate_c_code);
   572   
   607   
   573   vardecl = new generate_c_vardecl_c(&s4o,
   608   vardecl = new generate_c_vardecl_c(&s4o,
   574                 generate_c_vardecl_c::foutputassign_vf,
   609                 generate_c_vardecl_c::foutputassign_vf,
   575                 generate_c_vardecl_c::output_vt   |
   610                 generate_c_vardecl_c::output_vt   |
   647   s4o_incl.print("} ");
   682   s4o_incl.print("} ");
   648   symbol->fblock_name->accept(*typedecl);
   683   symbol->fblock_name->accept(*typedecl);
   649   s4o_incl.print(";\n\n");
   684   s4o_incl.print(";\n\n");
   650   delete typedecl;
   685   delete typedecl;
   651 
   686 
       
   687   /* (A.6) Function Block inline function declaration for function invocation */
       
   688   generate_c_inlinefcall_c inline_decl(&s4o, symbol->fblock_name, symbol, FB_FUNCTION_PARAM"->");
       
   689   symbol->fblock_body->accept(inline_decl);
       
   690 
   652   /* (B) Constructor */
   691   /* (B) Constructor */
   653   /* (B.1) Constructor name... */
   692   /* (B.1) Constructor name... */
   654   s4o.print(s4o.indent_spaces + "void ");
   693   s4o.print(s4o.indent_spaces + "void ");
   655   symbol->fblock_name->accept(*this);
   694   symbol->fblock_name->accept(*this);
   656   s4o.print(FB_INIT_SUFFIX);
   695   s4o.print(FB_INIT_SUFFIX);
   711   s4o.print(") {\n");
   750   s4o.print(") {\n");
   712   s4o.indent_right();
   751   s4o.indent_right();
   713 
   752 
   714   s4o.print(s4o.indent_spaces + "// Control execution\n");
   753   s4o.print(s4o.indent_spaces + "// Control execution\n");
   715   s4o.print(s4o.indent_spaces + "if (!");
   754   s4o.print(s4o.indent_spaces + "if (!");
       
   755   s4o.print(GET_VAR);
       
   756   s4o.print("(");
   716   s4o.print(FB_FUNCTION_PARAM);
   757   s4o.print(FB_FUNCTION_PARAM);
   717   s4o.print("->EN) {\n");
   758   s4o.print("->EN)) {\n");
   718   s4o.indent_right();
   759   s4o.indent_right();
   719   s4o.print(s4o.indent_spaces);
   760   s4o.print(s4o.indent_spaces);
       
   761   s4o.print(SET_VAR);
       
   762   s4o.print("(");
   720   s4o.print(FB_FUNCTION_PARAM);
   763   s4o.print(FB_FUNCTION_PARAM);
   721   s4o.print("->ENO = __BOOL_LITERAL(FALSE);\n");
   764   s4o.print("->ENO,__BOOL_LITERAL(FALSE));\n");
   722   s4o.print(s4o.indent_spaces + "return;\n");
   765   s4o.print(s4o.indent_spaces + "return;\n");
   723   s4o.indent_left();
   766   s4o.indent_left();
   724   s4o.print(s4o.indent_spaces + "}\n");
   767   s4o.print(s4o.indent_spaces + "}\n");
   725   s4o.print(s4o.indent_spaces + "else {\n");
   768   s4o.print(s4o.indent_spaces + "else {\n");
   726   s4o.indent_right();
   769   s4o.indent_right();
   727   s4o.print(s4o.indent_spaces);
   770   s4o.print(s4o.indent_spaces);
       
   771   s4o.print(SET_VAR);
       
   772   s4o.print("(");
   728   s4o.print(FB_FUNCTION_PARAM);
   773   s4o.print(FB_FUNCTION_PARAM);
   729   s4o.print("->ENO = __BOOL_LITERAL(TRUE);\n");
   774   s4o.print("->ENO,__BOOL_LITERAL(TRUE));\n");
   730   s4o.indent_left();
   775   s4o.indent_left();
   731   s4o.print(s4o.indent_spaces + "}\n");
   776   s4o.print(s4o.indent_spaces + "}\n");
   732 
   777 
   733   /* (C.4) Initialize TEMP variables */
   778   /* (C.4) Initialize TEMP variables */
   734   /* function body */
   779   /* function body */
   739   vardecl->print(symbol->var_declarations, NULL,  FB_FUNCTION_PARAM"->");
   784   vardecl->print(symbol->var_declarations, NULL,  FB_FUNCTION_PARAM"->");
   740   delete vardecl;
   785   delete vardecl;
   741   s4o.print("\n");
   786   s4o.print("\n");
   742 
   787 
   743   /* (C.5) Function code */
   788   /* (C.5) Function code */
   744   generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol, FB_FUNCTION_PARAM"->");
   789   generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol->fblock_name, symbol, FB_FUNCTION_PARAM"->");
   745   symbol->fblock_body->accept(generate_c_code);
   790   symbol->fblock_body->accept(generate_c_code);
   746   s4o.indent_left();
   791   s4o.indent_left();
   747   s4o.print(s4o.indent_spaces + "} // ");
   792   s4o.print(s4o.indent_spaces + "} // ");
   748   symbol->fblock_name->accept(*this);
   793   symbol->fblock_name->accept(*this);
   749   s4o.print(FB_FUNCTION_SUFFIX);
   794   s4o.print(FB_FUNCTION_SUFFIX);
   825   s4o_incl.print("} ");
   870   s4o_incl.print("} ");
   826   symbol->program_type_name->accept(*typedecl);
   871   symbol->program_type_name->accept(*typedecl);
   827   s4o_incl.print(";\n\n");
   872   s4o_incl.print(";\n\n");
   828   delete typedecl;
   873   delete typedecl;
   829 
   874 
       
   875   /* (A.6) Function Block inline function declaration for function invocation */
       
   876   generate_c_inlinefcall_c inline_decl(&s4o, symbol->program_type_name, symbol, FB_FUNCTION_PARAM"->");
       
   877   symbol->function_block_body->accept(inline_decl);
       
   878 
   830   /* (B) Constructor */
   879   /* (B) Constructor */
   831   /* (B.1) Constructor name... */
   880   /* (B.1) Constructor name... */
   832   s4o.print(s4o.indent_spaces + "void ");
   881   s4o.print(s4o.indent_spaces + "void ");
   833   symbol->program_type_name->accept(*this);
   882   symbol->program_type_name->accept(*this);
   834   s4o.print(FB_INIT_SUFFIX);
   883   s4o.print(FB_INIT_SUFFIX);
   896   vardecl->print(symbol->var_declarations, NULL,  FB_FUNCTION_PARAM"->");
   945   vardecl->print(symbol->var_declarations, NULL,  FB_FUNCTION_PARAM"->");
   897   delete vardecl;
   946   delete vardecl;
   898   s4o.print("\n");
   947   s4o.print("\n");
   899 
   948 
   900   /* (C.5) Function code */
   949   /* (C.5) Function code */
   901   generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol, FB_FUNCTION_PARAM"->");
   950   generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol->program_type_name, symbol, FB_FUNCTION_PARAM"->");
   902   symbol->function_block_body->accept(generate_c_code);
   951   symbol->function_block_body->accept(generate_c_code);
   903   s4o.indent_left();
   952   s4o.indent_left();
   904   s4o.print(s4o.indent_spaces + "} // ");
   953   s4o.print(s4o.indent_spaces + "} // ");
   905   symbol->program_type_name->accept(*this);
   954   symbol->program_type_name->accept(*this);
   906   s4o.print(FB_FUNCTION_SUFFIX);
   955   s4o.print(FB_FUNCTION_SUFFIX);
  1195       s4o.print("// RESOURCE ");
  1244       s4o.print("// RESOURCE ");
  1196       current_resource_name->accept(*this);
  1245       current_resource_name->accept(*this);
  1197       s4o.print("\n\n");
  1246       s4o.print("\n\n");
  1198       
  1247       
  1199       s4o.print("extern int common_ticktime__;\n\n");
  1248       s4o.print("extern int common_ticktime__;\n\n");
  1200        
  1249 
       
  1250       s4o.print("#include \"accessor.h\"\n\n");
       
  1251 
  1201       /* (A.2) Global variables... */
  1252       /* (A.2) Global variables... */
  1202       if (current_global_vars != NULL) {
  1253       if (current_global_vars != NULL) {
  1203         vardecl = new generate_c_vardecl_c(&s4o,
  1254         vardecl = new generate_c_vardecl_c(&s4o,
  1204                                            generate_c_vardecl_c::local_vf,
  1255                                            generate_c_vardecl_c::local_vf,
  1205                                            generate_c_vardecl_c::global_vt,
  1256                                            generate_c_vardecl_c::global_vt,
  1565 
  1616 
  1566 /***************************/
  1617 /***************************/
  1567 /* B 0 - Programming Model */
  1618 /* B 0 - Programming Model */
  1568 /***************************/
  1619 /***************************/
  1569     void *visit(library_c *symbol) {
  1620     void *visit(library_c *symbol) {
  1570       pous_incl_s4o.print("#ifndef __POUS_H\n#define __POUS_H\n\n");
  1621       pous_incl_s4o.print("#ifndef __POUS_H\n#define __POUS_H\n\n#include \"accessor.h\"\n\n");
  1571       for(int i = 0; i < symbol->n; i++) {
  1622       for(int i = 0; i < symbol->n; i++) {
  1572         symbol->elements[i]->accept(*this);
  1623         symbol->elements[i]->accept(*this);
  1573       }
  1624       }
  1574       pous_incl_s4o.print("#endif //__POUS_H\n");
  1625       pous_incl_s4o.print("#endif //__POUS_H\n");
  1575       
  1626