stage4/generate_c/generate_c.cc
changeset 210 8387cac2aba6
parent 202 da1a8186f86f
child 217 f5dfadf5de54
equal deleted inserted replaced
209:30cbae46190d 210:8387cac2aba6
   163 #define SECOND 1000 * MILLISECOND
   163 #define SECOND 1000 * MILLISECOND
   164 
   164 
   165 /* A helper class that knows how to generate code for both the IL and ST languages... */
   165 /* A helper class that knows how to generate code for both the IL and ST languages... */
   166 class calculate_time_c: public iterator_visitor_c {
   166 class calculate_time_c: public iterator_visitor_c {
   167   private:
   167   private:
   168     unsigned long time;
   168     unsigned long long time;
   169     float current_value;
   169     float current_value;
   170   
   170   
   171   public:
   171   public:
   172     calculate_time_c(void){time = 0;};
   172     calculate_time_c(void){time = 0;};
   173     
   173     
   174     unsigned long get_time(void) {return time;};
   174     unsigned long long get_time(void) {return time;};
   175 
   175 
   176     void *get_integer_value(token_c *token) {
   176     void *get_integer_value(token_c *token) {
   177       std::string str = "";
   177       std::string str = "";
   178       for (unsigned int i = 0; i < strlen(token->value); i++)
   178       for (unsigned int i = 0; i < strlen(token->value); i++)
   179         if (token->value[i] != '_')
   179         if (token->value[i] != '_')
   211     /* SYM_REF2(days_c, days, hours) */
   211     /* SYM_REF2(days_c, days, hours) */
   212     void *visit(days_c *symbol) {
   212     void *visit(days_c *symbol) {
   213       if (symbol->hours)
   213       if (symbol->hours)
   214         symbol->hours->accept(*this);
   214         symbol->hours->accept(*this);
   215       symbol->days->accept(*this);
   215       symbol->days->accept(*this);
   216       time += (unsigned long)(current_value * 24 * 3600 * SECOND);
   216       time += (unsigned long long)(current_value * 24 * 3600 * SECOND);
   217       return NULL;
   217       return NULL;
   218     }
   218     }
   219     
   219     
   220     /* SYM_REF2(hours_c, hours, minutes) */
   220     /* SYM_REF2(hours_c, hours, minutes) */
   221     void *visit(hours_c *symbol) {
   221     void *visit(hours_c *symbol) {
   222       if (symbol->minutes)
   222       if (symbol->minutes)
   223         symbol->minutes->accept(*this);
   223         symbol->minutes->accept(*this);
   224       symbol->hours->accept(*this);
   224       symbol->hours->accept(*this);
   225       time += (unsigned long)(current_value * 3600 * SECOND);
   225       time += (unsigned long long)(current_value * 3600 * SECOND);
   226       return NULL;
   226       return NULL;
   227     }
   227     }
   228     
   228     
   229     /* SYM_REF2(minutes_c, minutes, seconds) */
   229     /* SYM_REF2(minutes_c, minutes, seconds) */
   230     void *visit(minutes_c *symbol) {
   230     void *visit(minutes_c *symbol) {
   231       if (symbol->seconds)
   231       if (symbol->seconds)
   232         symbol->seconds->accept(*this);
   232         symbol->seconds->accept(*this);
   233       symbol->minutes->accept(*this);
   233       symbol->minutes->accept(*this);
   234       time += (unsigned long)(current_value * 60 * SECOND);
   234       time += (unsigned long long)(current_value * 60 * SECOND);
   235       return NULL;
   235       return NULL;
   236     }
   236     }
   237     
   237     
   238     /* SYM_REF2(seconds_c, seconds, milliseconds) */
   238     /* SYM_REF2(seconds_c, seconds, milliseconds) */
   239     void *visit(seconds_c *symbol) {
   239     void *visit(seconds_c *symbol) {
   240       if (symbol->milliseconds)
   240       if (symbol->milliseconds)
   241         symbol->milliseconds->accept(*this);
   241         symbol->milliseconds->accept(*this);
   242       symbol->seconds->accept(*this);
   242       symbol->seconds->accept(*this);
   243       time += (unsigned long)(current_value * SECOND);
   243       time += (unsigned long long)(current_value * SECOND);
   244       return NULL;
   244       return NULL;
   245     }
   245     }
   246     
   246     
   247     /* SYM_REF2(milliseconds_c, milliseconds, unused) */
   247     /* SYM_REF2(milliseconds_c, milliseconds, unused) */
   248     void *visit(milliseconds_c *symbol) {
   248     void *visit(milliseconds_c *symbol) {
   249       symbol->milliseconds->accept(*this);
   249       symbol->milliseconds->accept(*this);
   250       time += (unsigned long)(current_value * MILLISECOND);
   250       time += (unsigned long long)(current_value * MILLISECOND);
   251       return NULL;
   251       return NULL;
   252     }
   252     }
   253 };
   253 };
   254 
   254 
   255 /***********************************************************************/
   255 /***********************************************************************/
   257 /***********************************************************************/
   257 /***********************************************************************/
   258 /***********************************************************************/
   258 /***********************************************************************/
   259 
   259 
   260 class calculate_common_ticktime_c: public iterator_visitor_c {
   260 class calculate_common_ticktime_c: public iterator_visitor_c {
   261   private:
   261   private:
   262     unsigned long common_ticktime;
   262     unsigned long long common_ticktime;
       
   263     unsigned long long least_common_ticktime;
   263     
   264     
   264   public:
   265   public:
   265     calculate_common_ticktime_c(void){common_ticktime = 0;}
   266     calculate_common_ticktime_c(void){
   266     
   267       common_ticktime = 0;
   267     unsigned long euclide(unsigned long a, unsigned long b) {
   268       least_common_ticktime = 0;
   268       unsigned long c = a % b;
   269     }
       
   270     
       
   271     unsigned long long euclide(unsigned long long a, unsigned long long b) {
       
   272       unsigned long long c = a % b;
   269       if (c == 0)
   273       if (c == 0)
   270         return b;
   274         return b;
   271       else
   275       else
   272         return euclide(b, c);
   276         return euclide(b, c);
   273     }
   277     }
   274     
   278     
   275     void update_ticktime(unsigned long time) {
   279     void update_ticktime(unsigned long long time) {
   276       if (common_ticktime == 0)
   280       if (common_ticktime == 0)
   277         common_ticktime = time;
   281         common_ticktime = time;
   278       else if (time > common_ticktime)
   282       else if (time > common_ticktime)
   279         common_ticktime = euclide(time, common_ticktime);
   283         common_ticktime = euclide(time, common_ticktime);
   280       else
   284       else
   281         common_ticktime = euclide(common_ticktime, time);
   285         common_ticktime = euclide(common_ticktime, time);
   282     }
   286       if (least_common_ticktime == 0)
   283 
   287         least_common_ticktime = time;
   284     unsigned long get_ticktime(void) {
   288       else
       
   289         least_common_ticktime = (least_common_ticktime * time) / common_ticktime;
       
   290     }
       
   291 
       
   292     unsigned long long get_common_ticktime(void) {
   285       return common_ticktime;
   293       return common_ticktime;
       
   294     }
       
   295 
       
   296     unsigned long get_greatest_tick_count(void) {
       
   297       unsigned long long least_common_tick = least_common_ticktime / common_ticktime;
       
   298       if (least_common_tick >> 32)
       
   299         ERROR;
       
   300       return (unsigned long)(~(((unsigned long)-2) % (unsigned long)least_common_tick) + 1);
   286     }
   301     }
   287 
   302 
   288 /*  TASK task_name task_initialization */
   303 /*  TASK task_name task_initialization */
   289 //SYM_REF2(task_configuration_c, task_name, task_initialization)  
   304 //SYM_REF2(task_configuration_c, task_name, task_initialization)  
   290     void *visit(task_initialization_c *symbol) {
   305     void *visit(task_initialization_c *symbol) {
   291       calculate_time_c calculate_time;
   306       calculate_time_c calculate_time;
   292       unsigned long time = 0;
   307       unsigned long long time = 0;
   293       if (symbol->interval_data_source != NULL) {
   308       if (symbol->interval_data_source != NULL) {
   294         symbol->interval_data_source->accept(calculate_time);
   309         symbol->interval_data_source->accept(calculate_time);
   295         time = calculate_time.get_time();
   310         time = calculate_time.get_time();
   296       }
   311       }
   297       if (time > 0)
   312       if (time > 0)
  1011   s4o.print("\n");
  1026   s4o.print("\n");
  1012 
  1027 
  1013   /* (C.2) Run function name... */
  1028   /* (C.2) Run function name... */
  1014   s4o.print(s4o.indent_spaces + "void config");
  1029   s4o.print(s4o.indent_spaces + "void config");
  1015   s4o.print(FB_RUN_SUFFIX);
  1030   s4o.print(FB_RUN_SUFFIX);
  1016   s4o.print("(int tick) {\n");
  1031   s4o.print("(unsigned long tick) {\n");
  1017   s4o.indent_right();
  1032   s4o.indent_right();
  1018 
  1033 
  1019   /* (C.3) Resources initializations... */
  1034   /* (C.3) Resources initializations... */
  1020   wanted_declaretype = rundeclare_dt;
  1035   wanted_declaretype = rundeclare_dt;
  1021   symbol->resource_declarations->accept(*this);
  1036   symbol->resource_declarations->accept(*this);
  1035       s4o.print(FB_INIT_SUFFIX);
  1050       s4o.print(FB_INIT_SUFFIX);
  1036       s4o.print("(void);\n");
  1051       s4o.print("(void);\n");
  1037     }
  1052     }
  1038     else {
  1053     else {
  1039       s4o.print(FB_RUN_SUFFIX);
  1054       s4o.print(FB_RUN_SUFFIX);
  1040       s4o.print("(int tick);\n");
  1055       s4o.print("(unsigned long tick);\n");
  1041     }
  1056     }
  1042   }
  1057   }
  1043   if (wanted_declaretype == initdeclare_dt || wanted_declaretype == rundeclare_dt) {
  1058   if (wanted_declaretype == initdeclare_dt || wanted_declaretype == rundeclare_dt) {
  1044     s4o.print(s4o.indent_spaces);
  1059     s4o.print(s4o.indent_spaces);
  1045     symbol->resource_name->accept(*this);
  1060     symbol->resource_name->accept(*this);
  1062       s4o.print(FB_INIT_SUFFIX);
  1077       s4o.print(FB_INIT_SUFFIX);
  1063       s4o.print("(void);\n");
  1078       s4o.print("(void);\n");
  1064     }
  1079     }
  1065     else {
  1080     else {
  1066       s4o.print(FB_RUN_SUFFIX);
  1081       s4o.print(FB_RUN_SUFFIX);
  1067       s4o.print("(int tick);\n");
  1082       s4o.print("(unsigned long tick);\n");
  1068     }
  1083     }
  1069   }
  1084   }
  1070   if (wanted_declaretype == initdeclare_dt || wanted_declaretype == rundeclare_dt) {
  1085   if (wanted_declaretype == initdeclare_dt || wanted_declaretype == rundeclare_dt) {
  1071     s4o.print(s4o.indent_spaces + "RESOURCE");
  1086     s4o.print(s4o.indent_spaces + "RESOURCE");
  1072     if (wanted_declaretype == initdeclare_dt) {
  1087     if (wanted_declaretype == initdeclare_dt) {
  1241       /* (C) Resource run function... */
  1256       /* (C) Resource run function... */
  1242       /* (C.1) Run function name... */
  1257       /* (C.1) Run function name... */
  1243       s4o.print("void ");
  1258       s4o.print("void ");
  1244       current_resource_name->accept(*this);
  1259       current_resource_name->accept(*this);
  1245       s4o.print(FB_RUN_SUFFIX);
  1260       s4o.print(FB_RUN_SUFFIX);
  1246       s4o.print("(int tick) {\n");
  1261       s4o.print("(unsigned long tick) {\n");
  1247       s4o.indent_right();
  1262       s4o.indent_right();
  1248       
  1263       
  1249       wanted_declaretype = run_dt;
  1264       wanted_declaretype = run_dt;
  1250       
  1265       
  1251       /* (C.2) Task management... */
  1266       /* (C.2) Task management... */
  1628       
  1643       
  1629       current_configuration = symbol;
  1644       current_configuration = symbol;
  1630       
  1645       
  1631       calculate_common_ticktime_c calculate_common_ticktime;
  1646       calculate_common_ticktime_c calculate_common_ticktime;
  1632       symbol->accept(calculate_common_ticktime);
  1647       symbol->accept(calculate_common_ticktime);
  1633       common_ticktime = calculate_common_ticktime.get_ticktime();
  1648       common_ticktime = calculate_common_ticktime.get_common_ticktime();
  1634       
  1649       
  1635       symbol->configuration_name->accept(*this);
  1650       symbol->configuration_name->accept(*this);
  1636       stage4out_c config_s4o(current_builddir, current_name, "c");
  1651       stage4out_c config_s4o(current_builddir, current_name, "c");
  1637       generate_c_config_c generate_c_config(&config_s4o);
  1652       generate_c_config_c generate_c_config(&config_s4o);
  1638       symbol->accept(generate_c_config);
  1653       symbol->accept(generate_c_config);
  1639         
  1654         
  1640       config_s4o.print("int common_ticktime__ = ");
  1655       config_s4o.print("unsigned long long common_ticktime__ = ");
  1641       config_s4o.print_integer((int)(common_ticktime / 1000000));
  1656       config_s4o.print_long_long_integer(common_ticktime);
  1642       config_s4o.print("; /*ms*/\n");
  1657       config_s4o.print("; /*ns*/\n");
       
  1658       config_s4o.print("unsigned long greatest_tick_count__ = ");
       
  1659       config_s4o.print_long_integer(calculate_common_ticktime.get_greatest_tick_count());
       
  1660       config_s4o.print("; /*tick*/\n");
  1643       
  1661       
  1644       symbol->resource_declarations->accept(*this);
  1662       symbol->resource_declarations->accept(*this);
  1645 
  1663 
  1646       current_configuration = NULL;
  1664       current_configuration = NULL;
  1647       
  1665