stage4/generate_c/generate_c.cc
changeset 416 0c2ef191b22a
parent 397 85151b2f35ca
child 399 55b074ea7255
child 417 d48f53715f77
equal deleted inserted replaced
415:5c115720149c 416:0c2ef191b22a
   102 #define DECLARE_GLOBAL "__DECLARE_GLOBAL"
   102 #define DECLARE_GLOBAL "__DECLARE_GLOBAL"
   103 #define DECLARE_GLOBAL_LOCATION "__DECLARE_GLOBAL_LOCATION"
   103 #define DECLARE_GLOBAL_LOCATION "__DECLARE_GLOBAL_LOCATION"
   104 #define DECLARE_GLOBAL_LOCATED "__DECLARE_GLOBAL_LOCATED"
   104 #define DECLARE_GLOBAL_LOCATED "__DECLARE_GLOBAL_LOCATED"
   105 #define DECLARE_EXTERNAL "__DECLARE_EXTERNAL"
   105 #define DECLARE_EXTERNAL "__DECLARE_EXTERNAL"
   106 #define DECLARE_LOCATED "__DECLARE_LOCATED"
   106 #define DECLARE_LOCATED "__DECLARE_LOCATED"
       
   107 #define DECLARE_GLOBAL_PROTOTYPE "__DECLARE_GLOBAL_PROTOTYPE"
   107 
   108 
   108 /* Variable declaration symbol for accessor macros */
   109 /* Variable declaration symbol for accessor macros */
   109 #define INIT_VAR "__INIT_VAR"
   110 #define INIT_VAR "__INIT_VAR"
   110 #define INIT_GLOBAL "__INIT_GLOBAL"
   111 #define INIT_GLOBAL "__INIT_GLOBAL"
   111 #define INIT_GLOBAL_LOCATED "__INIT_GLOBAL_LOCATED"
   112 #define INIT_GLOBAL_LOCATED "__INIT_GLOBAL_LOCATED"
   124 /* Variable setter symbol for accessor macros */
   125 /* Variable setter symbol for accessor macros */
   125 #define SET_VAR "__SET_VAR"
   126 #define SET_VAR "__SET_VAR"
   126 #define SET_EXTERNAL "__SET_EXTERNAL"
   127 #define SET_EXTERNAL "__SET_EXTERNAL"
   127 #define SET_LOCATED "__SET_LOCATED"
   128 #define SET_LOCATED "__SET_LOCATED"
   128 
   129 
       
   130 /* Variable initial value symbol for accessor macros */
       
   131 #define INITIAL_VALUE "__INITIAL_VALUE"
   129 
   132 
   130 /* Generate a name for a temporary variable.
   133 /* Generate a name for a temporary variable.
   131  * Each new name generated is appended a different number,
   134  * Each new name generated is appended a different number,
   132  * starting off from 0.
   135  * starting off from 0.
   133  * After calling reset(), the names will start off again from 0.
   136  * After calling reset(), the names will start off again from 0.
  1556 /***********************************************************************/
  1559 /***********************************************************************/
  1557 
  1560 
  1558 class generate_c_config_c: public generate_c_typedecl_c {
  1561 class generate_c_config_c: public generate_c_typedecl_c {
  1559     private:
  1562     private:
  1560     stage4out_c *s4o_ptr;
  1563     stage4out_c *s4o_ptr;
  1561 
  1564     stage4out_c *s4o_incl_ptr;
       
  1565     
  1562     public:
  1566     public:
  1563     generate_c_config_c(stage4out_c *s4o_ptr)
  1567     generate_c_config_c(stage4out_c *s4o_ptr, stage4out_c *s4o_incl_ptr)
  1564       : generate_c_typedecl_c(s4o_ptr) {
  1568       : generate_c_typedecl_c(s4o_ptr, s4o_incl_ptr) {
  1565       generate_c_config_c::s4o_ptr = s4o_ptr;
  1569       generate_c_config_c::s4o_ptr = s4o_ptr;
       
  1570       generate_c_config_c::s4o_incl_ptr = s4o_incl_ptr;
  1566     };
  1571     };
  1567 
  1572 
  1568     virtual ~generate_c_config_c(void) {}
  1573     virtual ~generate_c_config_c(void) {}
  1569 
  1574 
  1570     typedef enum {
  1575     typedef enum {
  1579     
  1584     
  1580 public:
  1585 public:
  1581 /********************/
  1586 /********************/
  1582 /* 2.1.6 - Pragmas  */
  1587 /* 2.1.6 - Pragmas  */
  1583 /********************/
  1588 /********************/
  1584 void *visit(enable_code_generation_pragma_c * symbol)   {s4o_ptr->enable_output();  return NULL;}
  1589 void *visit(enable_code_generation_pragma_c * symbol)   {
  1585 void *visit(disable_code_generation_pragma_c * symbol)  {s4o_ptr->disable_output(); return NULL;} 
  1590     s4o_ptr->enable_output();
       
  1591     s4o_incl_ptr->enable_output();
       
  1592     return NULL;
       
  1593 }
       
  1594 
       
  1595 void *visit(disable_code_generation_pragma_c * symbol)  {
       
  1596     s4o_ptr->disable_output();
       
  1597     s4o_incl_ptr->disable_output();    
       
  1598     return NULL;
       
  1599 }
  1586     
  1600     
  1587     
  1601     
  1588 /********************************/
  1602 /********************************/
  1589 /* B 1.7 Configuration elements */
  1603 /* B 1.7 Configuration elements */
  1590 /********************************/
  1604 /********************************/
  1623                                      generate_c_vardecl_c::global_vt,
  1637                                      generate_c_vardecl_c::global_vt,
  1624                                      symbol->configuration_name);
  1638                                      symbol->configuration_name);
  1625   vardecl->print(symbol);
  1639   vardecl->print(symbol);
  1626   delete vardecl;
  1640   delete vardecl;
  1627   s4o.print("\n");
  1641   s4o.print("\n");
       
  1642 
       
  1643   /* (A.3) Declare global prototypes in include file */
       
  1644   vardecl = new generate_c_vardecl_c(&s4o_incl,
       
  1645                                      generate_c_vardecl_c::globalprototype_vf,
       
  1646                                      generate_c_vardecl_c::global_vt,
       
  1647                                      symbol->configuration_name);
       
  1648   vardecl->print(symbol);
       
  1649   delete vardecl;
       
  1650   s4o_incl.print("\n");
  1628 
  1651 
  1629   /* (B) Initialisation Function */
  1652   /* (B) Initialisation Function */
  1630   /* (B.1) Ressources initialisation protos... */
  1653   /* (B.1) Ressources initialisation protos... */
  1631   wanted_declaretype = initprotos_dt;
  1654   wanted_declaretype = initprotos_dt;
  1632   symbol->resource_declarations->accept(*this);
  1655   symbol->resource_declarations->accept(*this);
  1753   search_var_instance_decl_c *search_config_instance;
  1776   search_var_instance_decl_c *search_config_instance;
  1754   search_var_instance_decl_c *search_resource_instance;
  1777   search_var_instance_decl_c *search_resource_instance;
  1755 
  1778 
  1756   private:
  1779   private:
  1757     /* The name of the resource curretnly being processed... */
  1780     /* The name of the resource curretnly being processed... */
       
  1781     symbol_c *current_configuration;
  1758     symbol_c *current_resource_name;
  1782     symbol_c *current_resource_name;
  1759     symbol_c *current_task_name;
  1783     symbol_c *current_task_name;
  1760     symbol_c *current_global_vars;
  1784     symbol_c *current_global_vars;
       
  1785     bool configuration_name;
  1761     stage4out_c *s4o_ptr;
  1786     stage4out_c *s4o_ptr;
  1762 
  1787 
  1763   public:
  1788   public:
  1764     generate_c_resources_c(stage4out_c *s4o_ptr, symbol_c *config_scope, symbol_c *resource_scope, unsigned long time)
  1789     generate_c_resources_c(stage4out_c *s4o_ptr, symbol_c *config_scope, symbol_c *resource_scope, unsigned long time)
  1765       : generate_c_typedecl_c(s4o_ptr) {
  1790       : generate_c_typedecl_c(s4o_ptr) {
       
  1791       current_configuration = config_scope;
  1766       search_config_instance   = new search_var_instance_decl_c(config_scope);
  1792       search_config_instance   = new search_var_instance_decl_c(config_scope);
  1767       search_resource_instance = new search_var_instance_decl_c(resource_scope);
  1793       search_resource_instance = new search_var_instance_decl_c(resource_scope);
  1768       common_ticktime = time;
  1794       common_ticktime = time;
  1769       current_resource_name = NULL;
  1795       current_resource_name = NULL;
  1770       current_task_name = NULL;
  1796       current_task_name = NULL;
  1771       current_global_vars = NULL;
  1797       current_global_vars = NULL;
       
  1798       configuration_name = false;
  1772       generate_c_resources_c::s4o_ptr = s4o_ptr;
  1799       generate_c_resources_c::s4o_ptr = s4o_ptr;
  1773     };
  1800     };
  1774 
  1801 
  1775     virtual ~generate_c_resources_c(void) {
  1802     virtual ~generate_c_resources_c(void) {
  1776       delete search_config_instance;
  1803       delete search_config_instance;
  1819           break;
  1846           break;
  1820       }
  1847       }
  1821       return NULL;
  1848       return NULL;
  1822     }
  1849     }
  1823 
  1850 
       
  1851     /*************************/
       
  1852     /* B.1 - Common elements */
       
  1853     /*************************/
       
  1854     /*******************************************/
       
  1855     /* B 1.1 - Letters, digits and identifiers */
       
  1856     /*******************************************/
       
  1857     
       
  1858     void *visit(identifier_c *symbol) {
       
  1859         if (configuration_name)
       
  1860           s4o.print(symbol->value);
       
  1861         else
       
  1862           generate_c_base_c::visit(symbol);
       
  1863         return NULL;
       
  1864     }
  1824 
  1865 
  1825     /********************/
  1866     /********************/
  1826     /* 2.1.6 - Pragmas  */
  1867     /* 2.1.6 - Pragmas  */
  1827     /********************/
  1868     /********************/
  1828     void *visit(enable_code_generation_pragma_c * symbol)   {s4o_ptr->enable_output();  return NULL;}
  1869     void *visit(enable_code_generation_pragma_c * symbol)   {s4o_ptr->enable_output();  return NULL;}
  1846     void *visit(non_retain_option_c *symbol) {
  1887     void *visit(non_retain_option_c *symbol) {
  1847       current_varqualifier = non_retain_vq;
  1888       current_varqualifier = non_retain_vq;
  1848       return NULL;
  1889       return NULL;
  1849     }
  1890     }
  1850 
  1891 
  1851 /********************************/
  1892     /********************************/
  1852 /* B 1.7 Configuration elements */
  1893     /* B 1.7 Configuration elements */
  1853 /********************************/
  1894     /********************************/
       
  1895 
       
  1896     void *visit(configuration_declaration_c *symbol) {
       
  1897       return symbol->configuration_name->accept(*this);
       
  1898     }
  1854 
  1899 
  1855 /*
  1900 /*
  1856 RESOURCE resource_name ON resource_type_name
  1901 RESOURCE resource_name ON resource_type_name
  1857    optional_global_var_declarations
  1902    optional_global_var_declarations
  1858    single_resource_declaration
  1903    single_resource_declaration
  1891       current_resource_name->accept(*this);
  1936       current_resource_name->accept(*this);
  1892       s4o.print("\n\n");
  1937       s4o.print("\n\n");
  1893       
  1938       
  1894       s4o.print("extern unsigned long long common_ticktime__;\n\n");
  1939       s4o.print("extern unsigned long long common_ticktime__;\n\n");
  1895 
  1940 
  1896       s4o.print("#include \"accessor.h\"\n\n");
  1941       s4o.print("#include \"accessor.h\"\n");
  1897       s4o.print("#include \"POUS.h\"\n\n");
  1942       s4o.print("#include \"POUS.h\"\n\n");
       
  1943       s4o.print("#include \"");
       
  1944       configuration_name = true;
       
  1945       current_configuration->accept(*this);
       
  1946       configuration_name = false;
       
  1947       s4o.print(".h\"\n");
  1898 
  1948 
  1899       /* (A.2) Global variables... */
  1949       /* (A.2) Global variables... */
  1900       if (current_global_vars != NULL) {
  1950       if (current_global_vars != NULL) {
  1901         vardecl = new generate_c_vardecl_c(&s4o,
  1951         vardecl = new generate_c_vardecl_c(&s4o,
  1902                                            generate_c_vardecl_c::local_vf,
  1952                                            generate_c_vardecl_c::local_vf,
  2327       pous_incl_s4o.print("#endif //__POUS_H\n");
  2377       pous_incl_s4o.print("#endif //__POUS_H\n");
  2328       
  2378       
  2329       generate_var_list_c generate_var_list(&variables_s4o, symbol);
  2379       generate_var_list_c generate_var_list(&variables_s4o, symbol);
  2330       generate_var_list.generate_programs(symbol);
  2380       generate_var_list.generate_programs(symbol);
  2331       generate_var_list.generate_variables(symbol);
  2381       generate_var_list.generate_variables(symbol);
  2332       
  2382       variables_s4o.print("\n// Ticktime\n");
       
  2383       variables_s4o.print_long_long_integer(common_ticktime, false);
       
  2384       variables_s4o.print("\n");
       
  2385 
  2333       generate_location_list_c generate_location_list(&located_variables_s4o);
  2386       generate_location_list_c generate_location_list(&located_variables_s4o);
  2334       symbol->accept(generate_location_list);
  2387       symbol->accept(generate_location_list);
  2335       return NULL;
  2388       return NULL;
  2336     }
  2389     }
  2337 
  2390 
  2444               fprintf(stderr, "\nYou must at least define a periodic task to set cycle period!");
  2497               fprintf(stderr, "\nYou must at least define a periodic task to set cycle period!");
  2445               ERROR;
  2498               ERROR;
  2446             }
  2499             }
  2447 
  2500 
  2448             symbol->configuration_name->accept(*this);
  2501             symbol->configuration_name->accept(*this);
  2449 
  2502             
  2450             stage4out_c config_s4o(current_builddir, current_name, "c");
  2503             stage4out_c config_s4o(current_builddir, current_name, "c");
  2451             generate_c_config_c generate_c_config(&config_s4o);
  2504             stage4out_c config_incl_s4o(current_builddir, current_name, "h");
       
  2505             generate_c_config_c generate_c_config(&config_s4o, &config_incl_s4o);
  2452             symbol->accept(generate_c_config);
  2506             symbol->accept(generate_c_config);
  2453 
  2507 
  2454             config_s4o.print("unsigned long long common_ticktime__ = ");
  2508             config_s4o.print("unsigned long long common_ticktime__ = ");
  2455             config_s4o.print_long_long_integer(common_ticktime);
  2509             config_s4o.print_long_long_integer(common_ticktime);
  2456             config_s4o.print("; /*ns*/\n");
  2510             config_s4o.print("; /*ns*/\n");