stage4/generate_c/generate_c_st.cc
changeset 706 31553c22f318
parent 625 c0bda77b37a0
child 793 268bf4ca5fa1
equal deleted inserted replaced
705:f2323f79252e 706:31553c22f318
   142 
   142 
   143 
   143 
   144 void *print_getter(symbol_c *symbol) {
   144 void *print_getter(symbol_c *symbol) {
   145   unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
   145   unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
   146   if (wanted_variablegeneration == fparam_output_vg) {
   146   if (wanted_variablegeneration == fparam_output_vg) {
   147     if (vartype == search_var_instance_decl_c::external_vt)
   147     if (vartype == search_var_instance_decl_c::external_vt) {
   148       s4o.print(GET_EXTERNAL_BY_REF);
   148       if (search_var_instance_decl->type_is_fb(symbol))
       
   149         s4o.print(GET_EXTERNAL_FB_BY_REF);
       
   150       else
       
   151         s4o.print(GET_EXTERNAL_BY_REF);
       
   152     }
   149     else if (vartype == search_var_instance_decl_c::located_vt)
   153     else if (vartype == search_var_instance_decl_c::located_vt)
   150       s4o.print(GET_LOCATED_BY_REF);
   154       s4o.print(GET_LOCATED_BY_REF);
   151     else
   155     else
   152       s4o.print(GET_VAR_BY_REF);
   156       s4o.print(GET_VAR_BY_REF);
   153   }
   157   }
   154   else {
   158   else {
   155     if (vartype == search_var_instance_decl_c::external_vt)
   159     if (vartype == search_var_instance_decl_c::external_vt) {
   156       s4o.print(GET_EXTERNAL);
   160       if (search_var_instance_decl->type_is_fb(symbol))
       
   161         s4o.print(GET_EXTERNAL_FB);
       
   162       else
       
   163         s4o.print(GET_EXTERNAL);
       
   164     }
   157     else if (vartype == search_var_instance_decl_c::located_vt)
   165     else if (vartype == search_var_instance_decl_c::located_vt)
   158       s4o.print(GET_LOCATED);
   166       s4o.print(GET_LOCATED);
   159     else
   167     else
   160       s4o.print(GET_VAR);
   168       s4o.print(GET_VAR);
   161   }
   169   }
   172   wanted_variablegeneration = old_wanted_variablegeneration;
   180   wanted_variablegeneration = old_wanted_variablegeneration;
   173   return NULL;
   181   return NULL;
   174 }
   182 }
   175 
   183 
   176 void *print_setter(symbol_c* symbol,
   184 void *print_setter(symbol_c* symbol,
   177 		symbol_c* type,
   185         symbol_c* type,
   178 		symbol_c* value,
   186         symbol_c* value,
   179 		symbol_c* fb_symbol = NULL,
   187         symbol_c* fb_symbol = NULL,
   180 		symbol_c* fb_value = NULL) {
   188         symbol_c* fb_value = NULL) {
   181   
   189   
   182   bool type_is_complex = false;
   190   bool type_is_complex = false;
   183   if (fb_symbol == NULL) {
   191   if (fb_symbol == NULL) {
   184     unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
   192     unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
   185     type_is_complex = search_var_instance_decl->type_is_complex(symbol);
   193     type_is_complex = search_var_instance_decl->type_is_complex(symbol);
   186     if (vartype == search_var_instance_decl_c::external_vt)
   194     if (vartype == search_var_instance_decl_c::external_vt) {
   187       s4o.print(SET_EXTERNAL);
   195       if (search_var_instance_decl->type_is_fb(symbol))
       
   196         s4o.print(SET_EXTERNAL_FB);
       
   197       else
       
   198         s4o.print(SET_EXTERNAL);
       
   199     }
   188     else if (vartype == search_var_instance_decl_c::located_vt)
   200     else if (vartype == search_var_instance_decl_c::located_vt)
   189       s4o.print(SET_LOCATED);
   201       s4o.print(SET_LOCATED);
   190     else
   202     else
   191       s4o.print(SET_VAR);
   203       s4o.print(SET_VAR);
   192   }
   204   }
   193   else
   205   else {
   194 	s4o.print(SET_VAR);
   206     unsigned int vartype = search_var_instance_decl->get_vartype(fb_symbol);
       
   207     if (vartype == search_var_instance_decl_c::external_vt)
       
   208       s4o.print(SET_EXTERNAL_FB);
       
   209     else
       
   210       s4o.print(SET_VAR);
       
   211   }
   195   s4o.print("(");
   212   s4o.print("(");
   196 
   213 
   197   if (fb_symbol != NULL) {
   214   if (fb_symbol != NULL) {
   198     print_variable_prefix();
   215     print_variable_prefix();
   199     fb_symbol->accept(*this);
   216     fb_symbol->accept(*this);
   286   TRACE("direct_variable_c");
   303   TRACE("direct_variable_c");
   287   /* Do not use print_token() as it will change everything into uppercase */
   304   /* Do not use print_token() as it will change everything into uppercase */
   288   if (strlen(symbol->value) == 0) ERROR;
   305   if (strlen(symbol->value) == 0) ERROR;
   289   if (this->is_variable_prefix_null()) {
   306   if (this->is_variable_prefix_null()) {
   290     if (wanted_variablegeneration != fparam_output_vg)
   307     if (wanted_variablegeneration != fparam_output_vg)
   291 	  s4o.print("*(");
   308       s4o.print("*(");
   292   }
   309   }
   293   else {
   310   else {
   294     switch (wanted_variablegeneration) {
   311     switch (wanted_variablegeneration) {
   295       case expression_vg:
   312       case expression_vg:
   296   	    s4o.print(GET_LOCATED);
   313         s4o.print(GET_LOCATED);
   297   	    s4o.print("(");
   314         s4o.print("(");
   298   	    break;
   315         break;
   299       case fparam_output_vg:
   316       case fparam_output_vg:
   300         s4o.print(GET_LOCATED_BY_REF);
   317         s4o.print(GET_LOCATED_BY_REF);
   301         s4o.print("(");
   318         s4o.print("(");
   302         break;
   319         break;
   303       default:
   320       default:
   305     }
   322     }
   306   }
   323   }
   307   this->print_variable_prefix();
   324   this->print_variable_prefix();
   308   s4o.printlocation(symbol->value + 1);
   325   s4o.printlocation(symbol->value + 1);
   309   if ((this->is_variable_prefix_null() && wanted_variablegeneration != fparam_output_vg) ||
   326   if ((this->is_variable_prefix_null() && wanted_variablegeneration != fparam_output_vg) ||
   310 	  wanted_variablegeneration != assignment_vg)
   327       wanted_variablegeneration != assignment_vg)
   311     s4o.print(")");
   328     s4o.print(")");
   312   return NULL;
   329   return NULL;
   313 }
   330 }
   314 
   331 
   315 /*************************************/
   332 /*************************************/
   323   switch (wanted_variablegeneration) {
   340   switch (wanted_variablegeneration) {
   324     case complextype_base_vg:
   341     case complextype_base_vg:
   325     case complextype_base_assignment_vg:
   342     case complextype_base_assignment_vg:
   326       symbol->record_variable->accept(*this);
   343       symbol->record_variable->accept(*this);
   327       if (!type_is_complex) {
   344       if (!type_is_complex) {
   328     	s4o.print(".");
   345         s4o.print(".");
   329     	symbol->field_selector->accept(*this);
   346         symbol->field_selector->accept(*this);
   330       }
   347       }
   331       break;
   348       break;
   332     case complextype_suffix_vg:
   349     case complextype_suffix_vg:
   333       symbol->record_variable->accept(*this);
   350       symbol->record_variable->accept(*this);
   334       if (type_is_complex) {
   351       if (type_is_complex) {
   341       s4o.print(".");
   358       s4o.print(".");
   342       symbol->field_selector->accept(*this);
   359       symbol->field_selector->accept(*this);
   343       break;
   360       break;
   344     default:
   361     default:
   345       if (this->is_variable_prefix_null()) {
   362       if (this->is_variable_prefix_null()) {
   346     	symbol->record_variable->accept(*this);
   363         symbol->record_variable->accept(*this);
   347     	s4o.print(".");
   364         s4o.print(".");
   348     	symbol->field_selector->accept(*this);
   365         symbol->field_selector->accept(*this);
   349       }
   366       }
   350       else
   367       else
   351     	print_getter(symbol);
   368         print_getter(symbol);
   352       break;
   369       break;
   353   }
   370   }
   354   return NULL;
   371   return NULL;
   355 }
   372 }
   356 
   373 
   375 
   392 
   376       current_array_type = NULL;
   393       current_array_type = NULL;
   377       break;
   394       break;
   378     default:
   395     default:
   379       if (this->is_variable_prefix_null()) {
   396       if (this->is_variable_prefix_null()) {
   380     	symbol->subscripted_variable->accept(*this);
   397         symbol->subscripted_variable->accept(*this);
   381 
   398 
   382     	current_array_type = search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable);
   399         current_array_type = search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable);
   383     	if (current_array_type == NULL) ERROR;
   400         if (current_array_type == NULL) ERROR;
   384 
   401 
   385     	s4o.print(".table");
   402         s4o.print(".table");
   386         symbol->subscript_list->accept(*this);
   403         symbol->subscript_list->accept(*this);
   387 
   404 
   388         current_array_type = NULL;
   405         current_array_type = NULL;
   389       }
   406       }
   390       else
   407       else
   391     	print_getter(symbol);
   408         print_getter(symbol);
   392       break;
   409       break;
   393   }
   410   }
   394   return NULL;
   411   return NULL;
   395 }
   412 }
   396 
   413 
   397 /* subscript_list ',' subscript */
   414 /* subscript_list ',' subscript */
   398 void *visit(subscript_list_c *symbol) {
   415 void *visit(subscript_list_c *symbol) {
   399   array_dimension_iterator_c* array_dimension_iterator = new array_dimension_iterator_c(current_array_type);
   416   array_dimension_iterator_c* array_dimension_iterator = new array_dimension_iterator_c(current_array_type);
   400   for (int i =  0; i < symbol->n; i++) {
   417   for (int i =  0; i < symbol->n; i++) {
   401     symbol_c* dimension = array_dimension_iterator->next();
   418     symbol_c* dimension = array_dimension_iterator->next();
   402 	if (dimension == NULL) ERROR;
   419     if (dimension == NULL) ERROR;
   403 
   420 
   404 	s4o.print("[(");
   421     s4o.print("[(");
   405     symbol->elements[i]->accept(*this);
   422     symbol->elements[i]->accept(*this);
   406     s4o.print(") - (");
   423     s4o.print(") - (");
   407     dimension->accept(*this);
   424     dimension->accept(*this);
   408     s4o.print(")]");
   425     s4o.print(")]");
   409   }
   426   }
   568   return NULL;
   585   return NULL;
   569 }
   586 }
   570 
   587 
   571 void *visit(add_expression_c *symbol) {
   588 void *visit(add_expression_c *symbol) {
   572   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   589   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   573 	symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   590   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   574 	if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
   591   if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
   575       (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
   592       (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
   576       (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)))
   593       (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)))
   577     return print_binary_function("__time_add", symbol->l_exp, symbol->r_exp);
   594     return print_binary_function("__time_add", symbol->l_exp, symbol->r_exp);
   578   if (!search_expression_type->is_same_type(left_type, right_type))
   595   if (!search_expression_type->is_same_type(left_type, right_type))
   579       ERROR;
   596       ERROR;
   648 
   665 
   649 void *visit(power_expression_c *symbol) {
   666 void *visit(power_expression_c *symbol) {
   650   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   667   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   651   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   668   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   652   if (search_expression_type->is_real_type(left_type) && search_expression_type->is_num_type(right_type)) {
   669   if (search_expression_type->is_real_type(left_type) && search_expression_type->is_num_type(right_type)) {
   653 	s4o.print("EXPT__LREAL__LREAL__LREAL((BOOL)__BOOL_LITERAL(TRUE),\n");
   670     s4o.print("EXPT__LREAL__LREAL__LREAL((BOOL)__BOOL_LITERAL(TRUE),\n");
   654 	s4o.indent_right();
   671     s4o.indent_right();
   655 	s4o.print(s4o.indent_spaces + "NULL,\n");
   672     s4o.print(s4o.indent_spaces + "NULL,\n");
   656 	s4o.print(s4o.indent_spaces + "(LREAL)(");
   673     s4o.print(s4o.indent_spaces + "(LREAL)(");
   657 	symbol->l_exp->accept(*this);
   674     symbol->l_exp->accept(*this);
   658 	s4o.print("),\n");
   675     s4o.print("),\n");
   659 	s4o.print(s4o.indent_spaces + "(LREAL)(");
   676     s4o.print(s4o.indent_spaces + "(LREAL)(");
   660 	symbol->r_exp->accept(*this);
   677     symbol->r_exp->accept(*this);
   661 	s4o.print("))");
   678     s4o.print("))");
   662     return NULL;
   679     return NULL;
   663   }
   680   }
   664   ERROR;
   681   ERROR;
   665   return NULL;
   682   return NULL;
   666 }
   683 }
   822     symbol_c *param_value = PARAM_VALUE;
   839     symbol_c *param_value = PARAM_VALUE;
   823     current_param_type = PARAM_TYPE;
   840     current_param_type = PARAM_TYPE;
   824           
   841           
   825     switch (PARAM_DIRECTION) {
   842     switch (PARAM_DIRECTION) {
   826       case function_param_iterator_c::direction_in:
   843       case function_param_iterator_c::direction_in:
   827     	if (nb_param > 0)
   844         if (nb_param > 0)
   828     	  s4o.print(",\n"+s4o.indent_spaces);
   845           s4o.print(",\n"+s4o.indent_spaces);
   829     	if (param_value == NULL) {
   846         if (param_value == NULL) {
   830           /* If not, get the default value of this variable's type */
   847           /* If not, get the default value of this variable's type */
   831           param_value = (symbol_c *)current_param_type->accept(*type_initial_value_c::instance());
   848           param_value = (symbol_c *)current_param_type->accept(*type_initial_value_c::instance());
   832         }
   849         }
   833         if (param_value == NULL) ERROR;
   850         if (param_value == NULL) ERROR;
   834         s4o.print("(");
   851         s4o.print("(");
   842         print_check_function(current_param_type, param_value);
   859         print_check_function(current_param_type, param_value);
   843         nb_param++;
   860         nb_param++;
   844         break;
   861         break;
   845       case function_param_iterator_c::direction_out:
   862       case function_param_iterator_c::direction_out:
   846       case function_param_iterator_c::direction_inout:
   863       case function_param_iterator_c::direction_inout:
   847     	if (!has_output_params) {
   864         if (!has_output_params) {
   848           if (nb_param > 0)
   865           if (nb_param > 0)
   849         	s4o.print(",\n"+s4o.indent_spaces);
   866             s4o.print(",\n"+s4o.indent_spaces);
   850     	  if (param_value == NULL)
   867           if (param_value == NULL)
   851             s4o.print("NULL");
   868             s4o.print("NULL");
   852           else {
   869           else {
   853             wanted_variablegeneration = fparam_output_vg;
   870             wanted_variablegeneration = fparam_output_vg;
   854             param_value->accept(*this);
   871             param_value->accept(*this);
   855             wanted_variablegeneration = expression_vg;
   872             wanted_variablegeneration = expression_vg;
   856           }
   873           }
   857     	  nb_param++;
   874           nb_param++;
   858     	}
   875         }
   859         break;
   876         break;
   860       case function_param_iterator_c::direction_extref:
   877       case function_param_iterator_c::direction_extref:
   861         /* TODO! */
   878         /* TODO! */
   862         ERROR;
   879         ERROR;
   863         break;
   880         break;
   893     symbol->l_exp->accept(*this);
   910     symbol->l_exp->accept(*this);
   894     s4o.print(" = ");
   911     s4o.print(" = ");
   895     print_check_function(left_type, symbol->r_exp);
   912     print_check_function(left_type, symbol->r_exp);
   896   }
   913   }
   897   else {
   914   else {
   898 	print_setter(symbol->l_exp, left_type, symbol->r_exp);
   915     print_setter(symbol->l_exp, left_type, symbol->r_exp);
   899   }
   916   }
   900   return NULL;
   917   return NULL;
   901 }
   918 }
   902 
   919 
   903 /*****************************************/
   920 /*****************************************/
   968   } /* for(...) */
   985   } /* for(...) */
   969 
   986 
   970   /* now call the function... */
   987   /* now call the function... */
   971   function_block_type_name->accept(*this);
   988   function_block_type_name->accept(*this);
   972   s4o.print(FB_FUNCTION_SUFFIX);
   989   s4o.print(FB_FUNCTION_SUFFIX);
   973   s4o.print("(&");
   990   s4o.print("(");
       
   991   if (search_var_instance_decl->get_vartype(symbol->fb_name) != search_var_instance_decl_c::external_vt)
       
   992     s4o.print("&");
   974   print_variable_prefix();
   993   print_variable_prefix();
   975   symbol->fb_name->accept(*this);
   994   symbol->fb_name->accept(*this);
   976   s4o.print(")");
   995   s4o.print(")");
   977 
   996 
   978   /* loop through each function parameter, find the variable to which
   997   /* loop through each function parameter, find the variable to which
  1109   symbol_c *expression_type = search_expression_type->get_type(symbol->expression);
  1128   symbol_c *expression_type = search_expression_type->get_type(symbol->expression);
  1110   s4o.print("{\n");
  1129   s4o.print("{\n");
  1111   s4o.indent_right();
  1130   s4o.indent_right();
  1112   s4o.print(s4o.indent_spaces);
  1131   s4o.print(s4o.indent_spaces);
  1113   if (search_expression_type->is_literal_integer_type(expression_type))
  1132   if (search_expression_type->is_literal_integer_type(expression_type))
  1114 	search_expression_type->lint_type_name.accept(*this);
  1133     search_expression_type->lint_type_name.accept(*this);
  1115   else if (search_expression_type->is_literal_real_type(expression_type))
  1134   else if (search_expression_type->is_literal_real_type(expression_type))
  1116 	search_expression_type->lreal_type_name.accept(*this);
  1135     search_expression_type->lreal_type_name.accept(*this);
  1117   else
  1136   else
  1118     expression_type->accept(*this);
  1137     expression_type->accept(*this);
  1119   s4o.print(" __case_expression = ");
  1138   s4o.print(" __case_expression = ");
  1120   symbol->expression->accept(*this);
  1139   symbol->expression->accept(*this);
  1121   s4o.print(";\n" + s4o.indent_spaces + "switch (__case_expression) {\n");
  1140   s4o.print(";\n" + s4o.indent_spaces + "switch (__case_expression) {\n");
  1126   s4o.print(s4o.indent_spaces + "default:\n");
  1145   s4o.print(s4o.indent_spaces + "default:\n");
  1127   s4o.indent_right();
  1146   s4o.indent_right();
  1128   first_subrange_case_list = true;
  1147   first_subrange_case_list = true;
  1129   symbol->case_element_list->accept(*this);
  1148   symbol->case_element_list->accept(*this);
  1130   if (symbol->statement_list != NULL) {
  1149   if (symbol->statement_list != NULL) {
  1131 	if (!first_subrange_case_list) {
  1150     if (!first_subrange_case_list) {
  1132       s4o.print(s4o.indent_spaces + "else {\n");
  1151       s4o.print(s4o.indent_spaces + "else {\n");
  1133       s4o.indent_right();
  1152       s4o.indent_right();
  1134 	}
  1153     }
  1135     symbol->statement_list->accept(*this);
  1154     symbol->statement_list->accept(*this);
  1136     if (!first_subrange_case_list) {
  1155     if (!first_subrange_case_list) {
  1137       s4o.indent_left();
  1156       s4o.indent_left();
  1138       s4o.print(s4o.indent_spaces + "}\n");
  1157       s4o.print(s4o.indent_spaces + "}\n");
  1139     }
  1158     }
  1159   switch (wanted_casegeneration) {
  1178   switch (wanted_casegeneration) {
  1160     case single_cg:
  1179     case single_cg:
  1161       case_element_iterator = new case_element_iterator_c(symbol->case_list, case_element_iterator_c::element_single);
  1180       case_element_iterator = new case_element_iterator_c(symbol->case_list, case_element_iterator_c::element_single);
  1162       for (element = case_element_iterator->next(); element != NULL; element = case_element_iterator->next()) {
  1181       for (element = case_element_iterator->next(); element != NULL; element = case_element_iterator->next()) {
  1163         if (first_element) first_element = false;
  1182         if (first_element) first_element = false;
  1164     	s4o.print(s4o.indent_spaces + "case ");
  1183         s4o.print(s4o.indent_spaces + "case ");
  1165         element->accept(*this);
  1184         element->accept(*this);
  1166         s4o.print(":\n");
  1185         s4o.print(":\n");
  1167       }
  1186       }
  1168       delete case_element_iterator;
  1187       delete case_element_iterator;
  1169       break;
  1188       break;
  1175           if (first_subrange_case_list) {
  1194           if (first_subrange_case_list) {
  1176             s4o.print(s4o.indent_spaces + "if (");
  1195             s4o.print(s4o.indent_spaces + "if (");
  1177             first_subrange_case_list = false;
  1196             first_subrange_case_list = false;
  1178           }
  1197           }
  1179           else {
  1198           else {
  1180         	  s4o.print(s4o.indent_spaces + "else if (");
  1199             s4o.print(s4o.indent_spaces + "else if (");
  1181           }
  1200           }
  1182           first_element = false;
  1201           first_element = false;
  1183         }
  1202         }
  1184         else {
  1203         else {
  1185           s4o.print(" && ");
  1204           s4o.print(" && ");
  1203       case single_cg:
  1222       case single_cg:
  1204         s4o.print(s4o.indent_spaces + "break;\n");
  1223         s4o.print(s4o.indent_spaces + "break;\n");
  1205         s4o.indent_left();
  1224         s4o.indent_left();
  1206         break;
  1225         break;
  1207       case subrange_cg:
  1226       case subrange_cg:
  1208     	s4o.indent_left();
  1227         s4o.indent_left();
  1209   	    s4o.print(s4o.indent_spaces + "}\n");
  1228         s4o.print(s4o.indent_spaces + "}\n");
  1210   	    break;
  1229         break;
  1211       default:
  1230       default:
  1212         break;
  1231         break;
  1213     }
  1232     }
  1214   }
  1233   }
  1215   return NULL;
  1234   return NULL;