stage4/generate_c/generate_c.cc
changeset 267 0a1204bcc9af
parent 262 197ba42d78b2
child 272 579db02bebd9
equal deleted inserted replaced
266:2f6d8866ec8d 267:0a1204bcc9af
     1 /*
     1 /*
     2  * (c) 2003 Mario de Sousa
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
     3  *
     3  *
     4  * Offered to the public under the terms of the GNU General Public License
     4  *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
     5  * as published by the Free Software Foundation; either version 2 of the
       
     6  * License, or (at your option) any later version.
       
     7  *
     5  *
     8  * This program is distributed in the hope that it will be useful, but
     6  *  This program is free software: you can redistribute it and/or modify
     9  * WITHOUT ANY WARRANTY; without even the implied warranty of
     7  *  it under the terms of the GNU General Public License as published by
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
     8  *  the Free Software Foundation, either version 3 of the License, or
    11  * Public License for more details.
     9  *  (at your option) any later version.
       
    10  *
       
    11  *  This program is distributed in the hope that it will be useful,
       
    12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  *  GNU General Public License for more details.
       
    15  *
       
    16  *  You should have received a copy of the GNU General Public License
       
    17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    18  *
    12  *
    19  *
    13  * This code is made available on the understanding that it will not be
    20  * This code is made available on the understanding that it will not be
    14  * used in safety-critical situations without a full and competent review.
    21  * used in safety-critical situations without a full and competent review.
    15  */
    22  */
    16 
    23 
    17 /*
    24 /*
    18  * An IEC 61131-3 IL and ST compiler.
    25  * An IEC 61131-3 compiler.
    19  *
    26  *
    20  * Based on the
    27  * Based on the
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
    28  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
    22  *
    29  *
    23  */
    30  */
   362     symbol_c *fbname;
   369     symbol_c *fbname;
   363     const char *variable_prefix;
   370     const char *variable_prefix;
   364 
   371 
   365   public:
   372   public:
   366     generate_c_SFC_IL_ST_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL);
   373     generate_c_SFC_IL_ST_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL);
       
   374     
       
   375     /********************/
       
   376     /* 2.1.6 - Pragmas  */
       
   377     /********************/
       
   378     void *visit(enable_code_generation_pragma_c * symbol)   {s4o_ptr->enable_output();  return NULL;}
       
   379     void *visit(disable_code_generation_pragma_c * symbol)  {s4o_ptr->disable_output(); return NULL;} 
       
   380     
   367     /*********************************************/
   381     /*********************************************/
   368     /* B.1.6  Sequential function chart elements */
   382     /* B.1.6  Sequential function chart elements */
   369     /*********************************************/
   383     /*********************************************/
   370     
       
   371     /*| sequential_function_chart sfc_network*/
   384     /*| sequential_function_chart sfc_network*/
   372     void *visit(sequential_function_chart_c * symbol);
   385     void *visit(sequential_function_chart_c * symbol);
   373     
   386     
   374     /****************************************/
   387     /****************************************/
   375     /* B.2 - Language IL (Instruction List) */
   388     /* B.2 - Language IL (Instruction List) */
   436 /***********************************************************************/
   449 /***********************************************************************/
   437 /***********************************************************************/
   450 /***********************************************************************/
   438 
   451 
   439 
   452 
   440 class generate_c_pous_c: public generate_c_typedecl_c {
   453 class generate_c_pous_c: public generate_c_typedecl_c {
   441 
   454   private:
       
   455     stage4out_c *s4o_ptr;
       
   456     
   442   public:
   457   public:
   443     generate_c_pous_c(stage4out_c *s4o_ptr, stage4out_c *s4o_incl_ptr)
   458     generate_c_pous_c(stage4out_c *s4o_ptr, stage4out_c *s4o_incl_ptr)
   444       : generate_c_typedecl_c(s4o_ptr, s4o_incl_ptr) {};
   459       : generate_c_typedecl_c(s4o_ptr, s4o_incl_ptr) {
       
   460       generate_c_pous_c::s4o_ptr = s4o_ptr;
       
   461     };
   445     virtual ~generate_c_pous_c(void) {}
   462     virtual ~generate_c_pous_c(void) {}
   446 
   463 
   447 
   464 
   448   public:
   465   public:
       
   466 /********************/
       
   467 /* 2.1.6 - Pragmas  */
       
   468 /********************/
       
   469 void *visit(enable_code_generation_pragma_c * symbol)   {s4o_ptr->enable_output();  return NULL;}
       
   470 void *visit(disable_code_generation_pragma_c * symbol)  {s4o_ptr->disable_output(); return NULL;} 
   449 
   471 
   450 /*************************/
   472 /*************************/
   451 /* B.1 - Common elements */
   473 /* B.1 - Common elements */
   452 /*************************/
   474 /*************************/
   453 /*******************************************/
   475 /*******************************************/
   979 /***********************************************************************/
  1001 /***********************************************************************/
   980 /***********************************************************************/
  1002 /***********************************************************************/
   981 /***********************************************************************/
  1003 /***********************************************************************/
   982 
  1004 
   983 class generate_c_config_c: public generate_c_typedecl_c {
  1005 class generate_c_config_c: public generate_c_typedecl_c {
       
  1006     private:
       
  1007     stage4out_c *s4o_ptr;
   984 
  1008 
   985     public:
  1009     public:
   986     generate_c_config_c(stage4out_c *s4o_ptr)
  1010     generate_c_config_c(stage4out_c *s4o_ptr)
   987       : generate_c_typedecl_c(s4o_ptr) {};
  1011       : generate_c_typedecl_c(s4o_ptr) {
       
  1012       generate_c_config_c::s4o_ptr = s4o_ptr;
       
  1013     };
       
  1014 	
   988     virtual ~generate_c_config_c(void) {}
  1015     virtual ~generate_c_config_c(void) {}
   989 
  1016 
   990     typedef enum {
  1017     typedef enum {
   991       initprotos_dt,
  1018       initprotos_dt,
   992       initdeclare_dt,
  1019       initdeclare_dt,
   994       rundeclare_dt
  1021       rundeclare_dt
   995     } declaretype_t;
  1022     } declaretype_t;
   996 
  1023 
   997     declaretype_t wanted_declaretype;
  1024     declaretype_t wanted_declaretype;
   998 
  1025 
       
  1026     
       
  1027 public:
       
  1028 /********************/
       
  1029 /* 2.1.6 - Pragmas  */
       
  1030 /********************/
       
  1031 void *visit(enable_code_generation_pragma_c * symbol)   {s4o_ptr->enable_output();  return NULL;}
       
  1032 void *visit(disable_code_generation_pragma_c * symbol)  {s4o_ptr->disable_output(); return NULL;} 
       
  1033     
       
  1034     
   999 /********************************/
  1035 /********************************/
  1000 /* B 1.7 Configuration elements */
  1036 /* B 1.7 Configuration elements */
  1001 /********************************/
  1037 /********************************/
  1002 
       
  1003 
       
  1004 public:
       
  1005 /*
  1038 /*
  1006 CONFIGURATION configuration_name
  1039 CONFIGURATION configuration_name
  1007    optional_global_var_declarations
  1040    optional_global_var_declarations
  1008    (resource_declaration_list | single_resource_declaration)
  1041    (resource_declaration_list | single_resource_declaration)
  1009    optional_access_declarations
  1042    optional_access_declarations
  1166   private:
  1199   private:
  1167     /* The name of the resource curretnly being processed... */
  1200     /* The name of the resource curretnly being processed... */
  1168     symbol_c *current_resource_name;
  1201     symbol_c *current_resource_name;
  1169     symbol_c *current_task_name;
  1202     symbol_c *current_task_name;
  1170     symbol_c *current_global_vars;
  1203     symbol_c *current_global_vars;
       
  1204     stage4out_c *s4o_ptr;
  1171 
  1205 
  1172   public:
  1206   public:
  1173     generate_c_resources_c(stage4out_c *s4o_ptr, symbol_c *config_scope, symbol_c *resource_scope, unsigned long time)
  1207     generate_c_resources_c(stage4out_c *s4o_ptr, symbol_c *config_scope, symbol_c *resource_scope, unsigned long time)
  1174       : generate_c_typedecl_c(s4o_ptr) {
  1208       : generate_c_typedecl_c(s4o_ptr) {
  1175       search_config_instance   = new search_var_instance_decl_c(config_scope);
  1209       search_config_instance   = new search_var_instance_decl_c(config_scope);
  1176       search_resource_instance = new search_var_instance_decl_c(resource_scope);
  1210       search_resource_instance = new search_var_instance_decl_c(resource_scope);
  1177       common_ticktime = time;
  1211       common_ticktime = time;
  1178       current_resource_name = NULL;
  1212       current_resource_name = NULL;
  1179       current_task_name = NULL;
  1213       current_task_name = NULL;
  1180       current_global_vars = NULL;
  1214       current_global_vars = NULL;
       
  1215       generate_c_resources_c::s4o_ptr = s4o_ptr;
  1181     };
  1216     };
       
  1217 
  1182     virtual ~generate_c_resources_c(void) {
  1218     virtual ~generate_c_resources_c(void) {
  1183       delete search_config_instance;
  1219       delete search_config_instance;
  1184       delete search_resource_instance;
  1220       delete search_resource_instance;
  1185     }
  1221     }
  1186 
  1222 
  1225 		  s4o.print("retain");
  1261 		  s4o.print("retain");
  1226 		  break;
  1262 		  break;
  1227       }
  1263       }
  1228       return NULL;
  1264       return NULL;
  1229     }
  1265     }
       
  1266 
       
  1267 
       
  1268     /********************/
       
  1269     /* 2.1.6 - Pragmas  */
       
  1270     /********************/
       
  1271     void *visit(enable_code_generation_pragma_c * symbol)   {s4o_ptr->enable_output();  return NULL;}
       
  1272     void *visit(disable_code_generation_pragma_c * symbol)  {s4o_ptr->disable_output(); return NULL;} 
       
  1273     
  1230 
  1274 
  1231     /******************************************/
  1275     /******************************************/
  1232     /* B 1.4.3 - Declaration & Initialisation */
  1276     /* B 1.4.3 - Declaration & Initialisation */
  1233     /******************************************/
  1277     /******************************************/
  1234 
  1278 
  1671       current_configuration = NULL;
  1715       current_configuration = NULL;
  1672     }
  1716     }
  1673             
  1717             
  1674     ~generate_c_c(void) {}
  1718     ~generate_c_c(void) {}
  1675 
  1719 
       
  1720 
       
  1721 
       
  1722 /********************/
       
  1723 /* 2.1.6 - Pragmas  */
       
  1724 /********************/
       
  1725     void *visit(enable_code_generation_pragma_c * symbol)  {
       
  1726       s4o                  .enable_output();  
       
  1727       pous_s4o             .enable_output();  
       
  1728       pous_incl_s4o        .enable_output();  
       
  1729       located_variables_s4o.enable_output();  
       
  1730       variables_s4o        .enable_output();  
       
  1731       return NULL;
       
  1732     }
       
  1733     
       
  1734     void *visit(disable_code_generation_pragma_c * symbol)  {
       
  1735       s4o                  .disable_output();  
       
  1736       pous_s4o             .disable_output();  
       
  1737       pous_incl_s4o        .disable_output();  
       
  1738       located_variables_s4o.disable_output();  
       
  1739       variables_s4o        .disable_output();  
       
  1740       return NULL;
       
  1741     } 
       
  1742 
       
  1743 
  1676 /***************************/
  1744 /***************************/
  1677 /* B 0 - Programming Model */
  1745 /* B 0 - Programming Model */
  1678 /***************************/
  1746 /***************************/
  1679     void *visit(library_c *symbol) {
  1747     void *visit(library_c *symbol) {
  1680       pous_incl_s4o.print("#ifndef __POUS_H\n#define __POUS_H\n\n#include \"accessor.h\"\n\n");
  1748       pous_incl_s4o.print("#ifndef __POUS_H\n#define __POUS_H\n\n#include \"accessor.h\"\n\n");