stage4/generate_c/generate_c_sfc.cc
changeset 992 aa642302fa60
parent 945 477393b00f95
child 1011 76175defb87b
equal deleted inserted replaced
991:7c01878bd525 992:aa642302fa60
   159             symbol->step_name->accept(*this);
   159             symbol->step_name->accept(*this);
   160             s4o.print(" action associations\n");
   160             s4o.print(" action associations\n");
   161             current_step = symbol->step_name;
   161             current_step = symbol->step_name;
   162             s4o.print(s4o.indent_spaces + "{\n");
   162             s4o.print(s4o.indent_spaces + "{\n");
   163             s4o.indent_right();
   163             s4o.indent_right();
   164             s4o.print(s4o.indent_spaces + "char activated = ");
   164             s4o.print(s4o.indent_spaces + "char active = ");
   165             s4o.print(GET_VAR);
   165             s4o.print(GET_VAR);
   166             s4o.print("(");
   166             s4o.print("(");
   167             print_step_argument(current_step, "X");
   167             print_step_argument(current_step, "X");
   168             s4o.print(") && !");
   168             s4o.print(");\n");
       
   169             s4o.print(s4o.indent_spaces + "char activated = active && !");
   169             print_step_argument(current_step, "prev_state");
   170             print_step_argument(current_step, "prev_state");
   170             s4o.print(";\n");
   171             s4o.print(";\n");
   171             s4o.print(s4o.indent_spaces + "char desactivated = !");
   172             s4o.print(s4o.indent_spaces + "char desactivated = !active && ");
   172             s4o.print(GET_VAR);
       
   173             s4o.print("(");
       
   174             print_step_argument(current_step, "X");
       
   175             s4o.print(") && ");
       
   176             print_step_argument(current_step, "prev_state");
   173             print_step_argument(current_step, "prev_state");
   177             s4o.print(";\n");
   174             s4o.print(";\n\n");
   178             s4o.print(s4o.indent_spaces + "char active = ");
       
   179             s4o.print(GET_VAR);
       
   180             s4o.print("(");
       
   181             print_step_argument(current_step, "X");
       
   182             s4o.print(");\n");
       
   183             symbol->action_association_list->accept(*this);
   175             symbol->action_association_list->accept(*this);
   184             s4o.indent_left();
   176             s4o.indent_left();
   185             s4o.print(s4o.indent_spaces + "}\n\n");
   177             s4o.print(s4o.indent_spaces + "}\n\n");
   186           }
   178           }
   187           break;
   179           break;
   199             symbol->step_name->accept(*this);
   191             symbol->step_name->accept(*this);
   200             s4o.print(" action associations\n");
   192             s4o.print(" action associations\n");
   201             current_step = symbol->step_name;
   193             current_step = symbol->step_name;
   202             s4o.print(s4o.indent_spaces + "{\n");
   194             s4o.print(s4o.indent_spaces + "{\n");
   203             s4o.indent_right();
   195             s4o.indent_right();
   204             s4o.print(s4o.indent_spaces + "char activated, desactivated, active;\n");
   196             s4o.print(s4o.indent_spaces + "char active = ");
   205             s4o.print(s4o.indent_spaces + "activated = ");
       
   206             s4o.print(GET_VAR);
   197             s4o.print(GET_VAR);
   207             s4o.print("(");
   198             s4o.print("(");
   208             print_step_argument(current_step, "X");
   199             print_step_argument(current_step, "X");
   209             s4o.print(") && !");
   200             s4o.print(");\n");
       
   201             s4o.print(s4o.indent_spaces + "char activated = active && !");
   210             print_step_argument(current_step, "prev_state");
   202             print_step_argument(current_step, "prev_state");
   211             s4o.print(";\n");
   203             s4o.print(";\n");
   212             s4o.print(s4o.indent_spaces + "desactivated = !");
   204             s4o.print(s4o.indent_spaces + "char desactivated = !active && ");
   213             s4o.print(GET_VAR);
       
   214             s4o.print("(");
       
   215             print_step_argument(current_step, "X");
       
   216             s4o.print(") && ");
       
   217             print_step_argument(current_step, "prev_state");
   205             print_step_argument(current_step, "prev_state");
   218             s4o.print(";\n");
   206             s4o.print(";\n\n");
   219             s4o.print(s4o.indent_spaces + "active = ");
       
   220             s4o.print(GET_VAR);
       
   221             s4o.print("(");
       
   222             print_step_argument(current_step, "X");
       
   223             s4o.print(");\n");
       
   224             symbol->action_association_list->accept(*this);
   207             symbol->action_association_list->accept(*this);
   225             s4o.indent_left();
   208             s4o.indent_left();
   226             s4o.print(s4o.indent_spaces + "}\n\n");
   209             s4o.print(s4o.indent_spaces + "}\n\n");
   227           }
   210           }
   228           break;
   211           break;
   521           break;
   504           break;
   522       }
   505       }
   523       return NULL;
   506       return NULL;
   524     }
   507     }
   525 
   508 
       
   509     
       
   510     void print_set_var(symbol_c *var, const char *value) {  
       
   511       unsigned int vartype = search_var_instance_decl->get_vartype(var);
       
   512       s4o.print("{"); // it is safer to embed these macros nside a {..} block
       
   513       if (vartype == search_var_instance_decl_c::external_vt)
       
   514         s4o.print(SET_EXTERNAL);
       
   515       else if (vartype == search_var_instance_decl_c::located_vt)
       
   516         s4o.print(SET_LOCATED);
       
   517       else
       
   518         s4o.print(SET_VAR);
       
   519       s4o.print("(");
       
   520       print_variable_prefix();
       
   521       s4o.print(",");
       
   522       var->accept(*this);
       
   523       s4o.print(",,");
       
   524       s4o.print(value);
       
   525       s4o.print(");}");
       
   526     }
       
   527 
       
   528     void print_set_action_state(symbol_c *action, const char *value) {  
       
   529       s4o.print("{"); // it is safer to embed these macros nside a {..} block
       
   530       s4o.print(SET_VAR);
       
   531       s4o.print("(");
       
   532       print_action_argument(action, "state", true);
       
   533       s4o.print(",,");
       
   534       s4o.print(value);
       
   535       s4o.print(");}");
       
   536     }
       
   537     
       
   538     void print_set_var_or_action_state(symbol_c *action, const char *value) {  
       
   539       if (is_variable(current_action))  
       
   540         print_set_var         (action, value);
       
   541       else
       
   542         print_set_action_state(action, value);
       
   543     }
       
   544     
   526     void *visit(action_qualifier_c *symbol) {
   545     void *visit(action_qualifier_c *symbol) {
   527       switch (wanted_sfcgeneration) {
   546       switch (wanted_sfcgeneration) {
   528         case actionassociation_sg:
   547         case actionassociation_sg:
   529           {
   548           {
   530             char *qualifier = (char *)symbol->action_qualifier->accept(*this);
   549             char *qualifier = (char *)symbol->action_qualifier->accept(*this);
   531             s4o.print(s4o.indent_spaces + "if (");
   550             /* N qualifier */
   532             if (strcmp(qualifier, "N") == 0 || strcmp(qualifier, "S") == 0 ||
   551             if (strcmp(qualifier, "N") == 0) {
   533                 strcmp(qualifier, "R") == 0) {
   552               s4o.print(s4o.indent_spaces + "if (active)       ");
   534               s4o.print("active");
   553               print_set_var_or_action_state(current_action, "1");
       
   554               s4o.print(";\n");
       
   555               s4o.print(s4o.indent_spaces + "if (desactivated) ");
       
   556               print_set_var_or_action_state(current_action, "0");
       
   557               s4o.print(";\n");
       
   558               return NULL;
   535             }
   559             }
   536             else if (strcmp(qualifier, "P") == 0 || strcmp(qualifier, "SD") == 0 ||
   560             /* S qualifier */
   537                      strcmp(qualifier, "DS") == 0 || strcmp(qualifier, "SL") == 0 ||
   561             if (strcmp(qualifier, "S") == 0) {
   538                      strcmp(qualifier, "P0") == 0) {
   562               s4o.print(s4o.indent_spaces + "if (active)       {");
   539               s4o.print("activated");
   563               print_action_argument(current_action, "set");
       
   564               s4o.print(" = 1;}\n");
       
   565               return NULL;
   540             }
   566             }
   541             else if (strcmp(qualifier, "P1") == 0) {
   567             /* R qualifier */
   542               s4o.print("desactivated");
   568             if (strcmp(qualifier, "R") == 0) {
       
   569               s4o.print(s4o.indent_spaces + "if (active)       {");
       
   570               print_action_argument(current_action, "reset");
       
   571               s4o.print(" = 1;}\n");
       
   572               return NULL;
   543             }
   573             }
   544             else if (strcmp(qualifier, "D") == 0 || strcmp(qualifier, "L") == 0) {
   574             /* L or D qualifiers */
   545               s4o.print("active && __time_cmp(");
   575             if ((strcmp(qualifier, "L") == 0) || 
       
   576                 (strcmp(qualifier, "D") == 0)) {
       
   577               s4o.print(s4o.indent_spaces + "if (active && __time_cmp(");
   546               print_step_argument(current_step, "T.value");
   578               print_step_argument(current_step, "T.value");
   547               s4o.print(", ");
   579               s4o.print(", ");
   548               symbol->action_time->accept(*this);
   580               symbol->action_time->accept(*this);
   549               if (strcmp(qualifier, "D") == 0) {
   581               if (strcmp(qualifier, "L") == 0)
   550                 s4o.print(") >= 0");
   582                 s4o.print(") < 0) ");
   551               }
   583               else
   552               else {
   584                 s4o.print(") >= 0) ");
   553                 s4o.print(") < 0");
   585               s4o.print("\n" + s4o.indent_spaces + "                  ");
   554               }
   586               print_set_var_or_action_state(current_action, "1");
       
   587               if (strcmp(qualifier, "L") == 0)
       
   588                 // in L, we force to zero while state is active and time has been reached
       
   589                 // or when the state is deactivated.
       
   590                 s4o.print("\n" + s4o.indent_spaces + "else if (desactivated || active)");
       
   591               else
       
   592                 s4o.print("\n" + s4o.indent_spaces + "else if (desactivated)");
       
   593               s4o.print("\n" + s4o.indent_spaces + "                  ");
       
   594               print_set_var_or_action_state(current_action, "0");
       
   595               s4o.print(";\n");
       
   596               return NULL;
   555             }
   597             }
   556             s4o.print(") {\n");
   598             /* P, P1 or P0 qualifiers */
   557             s4o.indent_right();
   599             if ( (strcmp(qualifier, "P" ) == 0) || 
   558             s4o.print(s4o.indent_spaces);
   600                  (strcmp(qualifier, "P1") == 0) ||
   559             if (strcmp(qualifier, "N") == 0 || strcmp(qualifier, "P") == 0 ||
   601                  (strcmp(qualifier, "P0") == 0)) {
   560                 strcmp(qualifier, "D") == 0 || strcmp(qualifier, "L") == 0 ||
   602               if (strcmp(qualifier, "P0") == 0)
   561                 strcmp(qualifier, "P0") == 0 || strcmp(qualifier, "P1") == 0) {
   603                 s4o.print(s4o.indent_spaces + "if (desactivated) ");
   562 
   604               else
   563               if (is_variable(current_action)) {
   605                 s4o.print(s4o.indent_spaces + "if (activated)    ");
   564                 unsigned int vartype = search_var_instance_decl->get_vartype(current_action);
   606               print_set_var_or_action_state(current_action, "1");
   565 
   607               s4o.print("\n" + s4o.indent_spaces + "else              ");
   566                 if (vartype == search_var_instance_decl_c::external_vt)
   608               print_set_var_or_action_state(current_action, "0");
   567                   s4o.print(SET_EXTERNAL);
   609               s4o.print(";\n");
   568                 else if (vartype == search_var_instance_decl_c::located_vt)
   610               return NULL;
   569                   s4o.print(SET_LOCATED);
       
   570                 else
       
   571                   s4o.print(SET_VAR);
       
   572                 s4o.print("(");
       
   573                 print_variable_prefix();
       
   574                 s4o.print(",");
       
   575                 current_action->accept(*this);
       
   576                 s4o.print(",,1);\n");
       
   577                 s4o.indent_left();
       
   578                 s4o.print(s4o.indent_spaces + "}\n");
       
   579                 s4o.print(s4o.indent_spaces + "else if (active) {\n");
       
   580                 s4o.indent_right();
       
   581                 s4o.print(s4o.indent_spaces);
       
   582                 if (vartype == search_var_instance_decl_c::external_vt)
       
   583                   s4o.print(SET_EXTERNAL);
       
   584                 else if (vartype == search_var_instance_decl_c::located_vt)
       
   585                   s4o.print(SET_LOCATED);
       
   586                 else
       
   587                   s4o.print(SET_VAR);
       
   588                 s4o.print("(");
       
   589                 print_variable_prefix();
       
   590                 s4o.print(",");
       
   591                 current_action->accept(*this);
       
   592                 s4o.print(",,0);\n");
       
   593               }
       
   594 
       
   595               else {
       
   596                 s4o.print(SET_VAR);
       
   597                 s4o.print("(");
       
   598                 print_action_argument(current_action, "state", true);
       
   599                 s4o.print(",,1);\n");
       
   600               }
       
   601             }
   611             }
   602             if (strcmp(qualifier, "S") == 0 || strcmp(qualifier, "SL") == 0) {
   612             /* SL qualifier */
       
   613             if (strcmp(qualifier, "SL") == 0) {
       
   614               s4o.print(s4o.indent_spaces + "if (activated) {");
       
   615               s4o.indent_right();
       
   616               s4o.print("\n" + s4o.indent_spaces);
   603               print_action_argument(current_action, "set");
   617               print_action_argument(current_action, "set");
   604               s4o.print(" = 1;\n");
   618               s4o.print(" = 1;\n" + s4o.indent_spaces);
   605             }
   619               print_action_argument(current_action, "reset_remaining_time");
   606             if (strcmp(qualifier, "R") == 0) {
       
   607               print_action_argument(current_action, "reset");
       
   608               s4o.print(" = 1;\n");
       
   609             }
       
   610             if (strcmp(qualifier, "SD") == 0 || strcmp(qualifier, "DS") == 0 || 
       
   611                 strcmp(qualifier, "SL") == 0) {
       
   612               if (strcmp(qualifier, "SL") == 0) {
       
   613             	s4o.print(s4o.indent_spaces);
       
   614             	print_action_argument(current_action, "reset_remaining_time");
       
   615               }
       
   616               else {
       
   617                 print_action_argument(current_action, "set_remaining_time");
       
   618               }
       
   619               s4o.print(" = ");
   620               s4o.print(" = ");
   620               symbol->action_time->accept(*this);
   621               symbol->action_time->accept(*this);
   621               s4o.print(";\n");
   622               s4o.print(";\n");
   622             }
       
   623             s4o.indent_left();
       
   624             s4o.print(s4o.indent_spaces + "}\n");
       
   625             if (strcmp(qualifier, "DS") == 0) {
       
   626               s4o.print(s4o.indent_spaces + "if (desactivated) {\n");
       
   627               s4o.indent_right();
       
   628               s4o.print(s4o.indent_spaces);
       
   629               print_action_argument(current_action, "set_remaining_time");
       
   630               s4o.print(" = __time_to_timespec(1, 0, 0, 0, 0, 0);\n");
       
   631               s4o.indent_left();
   623               s4o.indent_left();
   632               s4o.print(s4o.indent_spaces + "}\n");
   624               s4o.print(s4o.indent_spaces + "}\n");
       
   625               return NULL;
   633             }
   626             }
       
   627             /* SD and DS qualifiers */
       
   628             if ( (strcmp(qualifier, "SD") == 0) ||
       
   629                  (strcmp(qualifier, "DS") == 0)) {
       
   630               s4o.print(s4o.indent_spaces + "if (activated) {");
       
   631               s4o.indent_right();
       
   632               s4o.print("\n" + s4o.indent_spaces);
       
   633               print_action_argument(current_action, "set_remaining_time");
       
   634               s4o.print(" = ");
       
   635               symbol->action_time->accept(*this);
       
   636               s4o.print(";\n");
       
   637               s4o.indent_left();
       
   638               s4o.print(s4o.indent_spaces + "}\n");
       
   639               if (strcmp(qualifier, "DS") == 0) {
       
   640                 s4o.print(s4o.indent_spaces + "if (desactivated) {");
       
   641                 s4o.indent_right();
       
   642                 s4o.print("\n" + s4o.indent_spaces);
       
   643                 print_action_argument(current_action, "set_remaining_time");
       
   644                 s4o.print(" = __time_to_timespec(1, 0, 0, 0, 0, 0);\n");
       
   645                 s4o.indent_left();
       
   646                 s4o.print(s4o.indent_spaces + "}\n");
       
   647               }
       
   648               return NULL;
       
   649             }
       
   650             ERROR;
   634           }
   651           }
   635           break;
   652           break;
   636         default:
   653         default:
   637           break;
   654           break;
   638       }
   655       }
   873       print_variable_prefix();
   890       print_variable_prefix();
   874       s4o.print("__action_list[i].set) {\n");
   891       s4o.print("__action_list[i].set) {\n");
   875       s4o.indent_right();
   892       s4o.indent_right();
   876       s4o.print(s4o.indent_spaces);
   893       s4o.print(s4o.indent_spaces);
   877       print_variable_prefix();
   894       print_variable_prefix();
       
   895       s4o.print("__action_list[i].set_remaining_time = __time_to_timespec(1, 0, 0, 0, 0, 0);\n" + s4o.indent_spaces);
       
   896       print_variable_prefix();
   878       s4o.print("__action_list[i].stored = 1;\n");
   897       s4o.print("__action_list[i].stored = 1;\n");
   879       s4o.indent_left();
   898       s4o.indent_left();
   880       s4o.print(s4o.indent_spaces + "}\n" + s4o.indent_spaces + "if (");
   899       s4o.print(s4o.indent_spaces + "}\n" + s4o.indent_spaces + "if (");
   881       print_variable_prefix();
   900       print_variable_prefix();
   882       s4o.print("__action_list[i].reset) {\n");
   901       s4o.print("__action_list[i].reset) {\n");
   883       s4o.indent_right();
   902       s4o.indent_right();
   884       s4o.print(s4o.indent_spaces);
   903       s4o.print(s4o.indent_spaces);
   885       print_variable_prefix();
       
   886       s4o.print("__action_list[i].set_remaining_time = __time_to_timespec(1, 0, 0, 0, 0, 0);\n" + s4o.indent_spaces);
       
   887       print_variable_prefix();
   904       print_variable_prefix();
   888       s4o.print("__action_list[i].reset_remaining_time = __time_to_timespec(1, 0, 0, 0, 0, 0);\n" + s4o.indent_spaces);
   905       s4o.print("__action_list[i].reset_remaining_time = __time_to_timespec(1, 0, 0, 0, 0, 0);\n" + s4o.indent_spaces);
   889       print_variable_prefix();
   906       print_variable_prefix();
   890       s4o.print("__action_list[i].stored = 0;\n");
   907       s4o.print("__action_list[i].stored = 0;\n");
   891       s4o.indent_left();
   908       s4o.indent_left();