stage4/generate_c/generate_c_st.cc
changeset 226 29f8ffc203c1
parent 221 c6aed7e5f070
child 228 43831b683764
equal deleted inserted replaced
225:36d05588f91b 226:29f8ffc203c1
    42 
    42 
    43   public:
    43   public:
    44     typedef enum {
    44     typedef enum {
    45       expression_vg,
    45       expression_vg,
    46       assignment_vg,
    46       assignment_vg,
       
    47       complextype_base_vg,
       
    48       complextype_suffix_vg,
    47       fparam_output_vg
    49       fparam_output_vg
    48     } variablegeneration_t;
    50     } variablegeneration_t;
    49 
    51 
    50   private:
    52   private:
    51     /* When calling a function block, we must first find it's type,
    53     /* When calling a function block, we must first find it's type,
   109       stl->accept(*this);
   111       stl->accept(*this);
   110     }
   112     }
   111 
   113 
   112   private:
   114   private:
   113 
   115 
       
   116 void *print_getter(symbol_c *symbol) {
       
   117   unsigned int vartype = search_varfb_instance_type->get_vartype(symbol);
       
   118   if (vartype == search_var_instance_decl_c::external_vt)
       
   119 	s4o.print(GET_EXTERNAL);
       
   120   else if (vartype == search_var_instance_decl_c::located_vt)
       
   121 	s4o.print(GET_LOCATED);
       
   122   else
       
   123 	s4o.print(GET_VAR);
       
   124   s4o.print("(");
       
   125 
       
   126   wanted_variablegeneration = complextype_base_vg;
       
   127   symbol->accept(*this);
       
   128   if (search_varfb_instance_type->type_is_complex())
       
   129 	s4o.print(",");
       
   130   wanted_variablegeneration = complextype_suffix_vg;
       
   131   symbol->accept(*this);
       
   132   s4o.print(")");
       
   133   wanted_variablegeneration = expression_vg;
       
   134   return NULL;
       
   135 }
       
   136 
       
   137 void *print_setter(symbol_c* symbol,
       
   138 		symbol_c* type,
       
   139 		symbol_c* value,
       
   140 		symbol_c* fb_symbol = NULL,
       
   141 		symbol_c* fb_value = NULL) {
       
   142   unsigned int vartype = search_varfb_instance_type->get_vartype(symbol);
       
   143   if (vartype == search_var_instance_decl_c::external_vt)
       
   144     s4o.print(SET_EXTERNAL);
       
   145   else if (vartype == search_var_instance_decl_c::located_vt)
       
   146     s4o.print(SET_LOCATED);
       
   147   else
       
   148     s4o.print(SET_VAR);
       
   149   s4o.print("(");
       
   150 
       
   151   if (fb_symbol != NULL) {
       
   152     print_variable_prefix();
       
   153     fb_symbol->accept(*this);
       
   154     s4o.print(".");
       
   155   }
       
   156   else
       
   157     wanted_variablegeneration = complextype_base_vg;
       
   158   symbol->accept(*this);
       
   159   s4o.print(",");
       
   160   wanted_variablegeneration = expression_vg;
       
   161   print_check_function(type, value, fb_value);
       
   162   if (search_varfb_instance_type->type_is_complex()) {
       
   163     s4o.print(",");
       
   164     wanted_variablegeneration = complextype_suffix_vg;
       
   165     symbol->accept(*this);
       
   166   }
       
   167   s4o.print(")");
       
   168   wanted_variablegeneration = expression_vg;
       
   169   return NULL;
       
   170 }
   114 
   171 
   115 /*********************/
   172 /*********************/
   116 /* B 1.4 - Variables */
   173 /* B 1.4 - Variables */
   117 /*********************/
   174 /*********************/
   118 void *visit(symbolic_variable_c *symbol) {
   175 void *visit(symbolic_variable_c *symbol) {
   119   unsigned int vartype = search_varfb_instance_type->get_vartype(symbol);
   176   unsigned int vartype;
   120   if (this->is_variable_prefix_null()) {
   177   if (wanted_variablegeneration == complextype_base_vg)
   121     if (wanted_variablegeneration == fparam_output_vg) {
   178 	generate_c_base_c::visit(symbol);
       
   179   else if (wanted_variablegeneration == complextype_suffix_vg)
       
   180 	return NULL;
       
   181   else if (this->is_variable_prefix_null()) {
       
   182 	vartype = search_varfb_instance_type->get_vartype(symbol);
       
   183 	if (wanted_variablegeneration == fparam_output_vg) {
   122       if (vartype == search_var_instance_decl_c::external_vt)
   184       if (vartype == search_var_instance_decl_c::external_vt)
   123     	s4o.print(GET_EXTERNAL);
   185     	s4o.print(GET_EXTERNAL);
   124       else
   186       else
   125     	s4o.print("&");
   187     	s4o.print("&");
   126       s4o.print("(");
   188       s4o.print("(");
   139     }
   201     }
   140   }
   202   }
   141   else {
   203   else {
   142     switch (wanted_variablegeneration) {
   204     switch (wanted_variablegeneration) {
   143       case expression_vg:
   205       case expression_vg:
   144         if (vartype == search_var_instance_decl_c::external_vt)
   206     	vartype = search_varfb_instance_type->get_vartype(symbol);
       
   207     	if (vartype == search_var_instance_decl_c::external_vt)
   145     	  s4o.print(GET_EXTERNAL);
   208     	  s4o.print(GET_EXTERNAL);
   146     	else if (vartype == search_var_instance_decl_c::located_vt)
   209     	else if (vartype == search_var_instance_decl_c::located_vt)
   147 		  s4o.print(GET_LOCATED);
   210 		  s4o.print(GET_LOCATED);
   148 	    else
   211 	    else
   149 		  s4o.print(GET_VAR);
   212 		  s4o.print(GET_VAR);
   150 	    s4o.print("(");
   213 	    s4o.print("(");
   151 	    generate_c_base_c::visit(symbol);
   214 	    generate_c_base_c::visit(symbol);
   152         s4o.print(")");
   215         s4o.print(")");
   153 		break;
   216 		break;
   154       case fparam_output_vg:
   217       case fparam_output_vg:
   155         if (vartype == search_var_instance_decl_c::external_vt)
   218     	vartype = search_varfb_instance_type->get_vartype(symbol);
       
   219     	if (vartype == search_var_instance_decl_c::external_vt)
   156           s4o.print(GET_EXTERNAL_BY_REF);
   220           s4o.print(GET_EXTERNAL_BY_REF);
   157         else if (vartype == search_var_instance_decl_c::located_vt)
   221         else if (vartype == search_var_instance_decl_c::located_vt)
   158           s4o.print(GET_LOCATED_BY_REF);
   222           s4o.print(GET_LOCATED_BY_REF);
   159         else
   223         else
   160           s4o.print(GET_VAR_BY_REF);
   224           s4o.print(GET_VAR_BY_REF);
   209 /*************************************/
   273 /*************************************/
   210 
   274 
   211 // SYM_REF2(structured_variable_c, record_variable, field_selector)
   275 // SYM_REF2(structured_variable_c, record_variable, field_selector)
   212 void *visit(structured_variable_c *symbol) {
   276 void *visit(structured_variable_c *symbol) {
   213   TRACE("structured_variable_c");
   277   TRACE("structured_variable_c");
   214 
   278   switch (wanted_variablegeneration) {
   215   unsigned int vartype = search_varfb_instance_type->get_vartype(symbol->record_variable);
   279     case complextype_base_vg:
   216   if (this->is_variable_prefix_null()) {
   280       symbol->record_variable->accept(*this);
   217 	  symbol->record_variable->accept(*this);
   281       break;
   218 	  s4o.print(".");
   282     case complextype_suffix_vg:
   219 	  symbol->field_selector->accept(*this);
   283       symbol->record_variable->accept(*this);
   220   }
   284       s4o.print(".");
   221   else {
   285       symbol->field_selector->accept(*this);
   222 	  variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration;
   286       break;
   223 	  switch (wanted_variablegeneration) {
   287     default:
   224       case expression_vg:
   288       if (this->is_variable_prefix_null()) {
   225     	wanted_variablegeneration = assignment_vg;
       
   226     	if (vartype == search_var_instance_decl_c::external_vt) {
       
   227     	  s4o.print(GET_EXTERNAL);
       
   228     	  s4o.print("(");
       
   229     	  symbol->record_variable->accept(*this);
       
   230     	  s4o.print(").");
       
   231     	  symbol->field_selector->accept(*this);
       
   232         }
       
   233         else {
       
   234           if (vartype == search_var_instance_decl_c::located_vt)
       
   235             s4o.print(GET_LOCATED);
       
   236           else
       
   237             s4o.print(GET_VAR);
       
   238           s4o.print("(");
       
   239           symbol->record_variable->accept(*this);
       
   240           s4o.print(".");
       
   241           symbol->field_selector->accept(*this);
       
   242           s4o.print(")");
       
   243         }
       
   244         wanted_variablegeneration = old_wanted_variablegeneration;
       
   245 		break;
       
   246       case fparam_output_vg:
       
   247     	wanted_variablegeneration = assignment_vg;
       
   248     	s4o.print("&(");
       
   249     	if (vartype == search_var_instance_decl_c::external_vt)
       
   250           s4o.print(GET_EXTERNAL);
       
   251         else if (vartype == search_var_instance_decl_c::located_vt)
       
   252           s4o.print(GET_LOCATED);
       
   253         else
       
   254           s4o.print(GET_VAR);
       
   255         s4o.print("(");
       
   256     	symbol->record_variable->accept(*this);
       
   257     	s4o.print(").");
       
   258     	symbol->field_selector->accept(*this);
       
   259     	s4o.print("))");
       
   260         wanted_variablegeneration = old_wanted_variablegeneration;
       
   261         break;
       
   262       default:
       
   263     	symbol->record_variable->accept(*this);
   289     	symbol->record_variable->accept(*this);
   264     	s4o.print(".");
   290     	s4o.print(".");
   265     	symbol->field_selector->accept(*this);
   291     	symbol->field_selector->accept(*this);
   266         break;
   292       }
   267 	}
   293       else
       
   294     	print_getter(symbol);
       
   295       break;
   268   }
   296   }
   269   return NULL;
   297   return NULL;
   270 }
   298 }
   271 
   299 
   272 /*  subscripted_variable '[' subscript_list ']' */
   300 /*  subscripted_variable '[' subscript_list ']' */
   273 //SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
   301 //SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
   274 void *visit(array_variable_c *symbol) {
   302 void *visit(array_variable_c *symbol) {
   275   current_array_type = search_varfb_instance_type->get_type(symbol->subscripted_variable, false);
   303   switch (wanted_variablegeneration) {
   276   symbol->subscripted_variable->accept(*this);
   304     case complextype_base_vg:
   277   if (current_array_type != NULL) {
   305       symbol->subscripted_variable->accept(*this);
   278     symbol->subscript_list->accept(*this);
   306       break;
   279     current_array_type = NULL;
   307     case complextype_suffix_vg:
       
   308       current_array_type = search_varfb_instance_type->get_rawtype(symbol->subscripted_variable);
       
   309       symbol->subscripted_variable->accept(*this);
       
   310       if (current_array_type != NULL) {
       
   311         symbol->subscript_list->accept(*this);
       
   312         current_array_type = NULL;
       
   313       }
       
   314       break;
       
   315     default:
       
   316       if (this->is_variable_prefix_null()) {
       
   317     	current_array_type = search_varfb_instance_type->get_rawtype(symbol->subscripted_variable);
       
   318     	symbol->subscripted_variable->accept(*this);
       
   319     	if (current_array_type != NULL) {
       
   320     	  symbol->subscript_list->accept(*this);
       
   321     	  current_array_type = NULL;
       
   322     	}
       
   323       }
       
   324       else
       
   325     	print_getter(symbol);
       
   326       break;
   280   }
   327   }
   281   return NULL;
   328   return NULL;
   282 }
   329 }
   283 
   330 
   284 /* subscript_list ',' subscript */
   331 /* subscript_list ',' subscript */
   706 
   753 
   707 /*********************************/
   754 /*********************************/
   708 /* B 3.2.1 Assignment Statements */
   755 /* B 3.2.1 Assignment Statements */
   709 /*********************************/
   756 /*********************************/
   710 void *visit(assignment_statement_c *symbol) {
   757 void *visit(assignment_statement_c *symbol) {
   711   symbol_c *left_type = search_varfb_instance_type->get_type(symbol->l_exp, false);
   758   symbol_c *left_type = search_varfb_instance_type->get_rawtype(symbol->l_exp);
   712   
   759   
   713   if (!this->is_variable_prefix_null()) {
   760   if (this->is_variable_prefix_null()) {
   714     unsigned int vartype = search_varfb_instance_type->get_vartype(symbol->l_exp);
       
   715     if (vartype == search_var_instance_decl_c::external_vt)
       
   716 	  s4o.print(SET_EXTERNAL);
       
   717     else if (vartype == search_var_instance_decl_c::located_vt)
       
   718 	  s4o.print(SET_LOCATED);
       
   719     else
       
   720 	  s4o.print(SET_VAR);
       
   721     s4o.print("(");
       
   722 
       
   723     wanted_variablegeneration = assignment_vg;
       
   724     symbol->l_exp->accept(*this);
   761     symbol->l_exp->accept(*this);
   725     wanted_variablegeneration = expression_vg;
   762     s4o.print(" = ");
   726 
   763     print_check_function(left_type, symbol->r_exp);
   727     s4o.print(",");
       
   728   }
   764   }
   729   else {
   765   else {
   730 	symbol->l_exp->accept(*this);
   766 	print_setter(symbol->l_exp, left_type, symbol->r_exp);
   731 
   767   }
   732     s4o.print(" = ");
       
   733   }
       
   734   print_check_function(left_type, symbol->r_exp);
       
   735   if (!this->is_variable_prefix_null())
       
   736     s4o.print(")");
       
   737   return NULL;
   768   return NULL;
   738 }
   769 }
   739 
   770 
   740 /*****************************************/
   771 /*****************************************/
   741 /* B 3.2.2 Subprogram Control Statements */
   772 /* B 3.2.2 Subprogram Control Statements */
   779     
   810     
   780     /* now output the value assignment */
   811     /* now output the value assignment */
   781     if (param_value != NULL)
   812     if (param_value != NULL)
   782       if ((param_direction == function_param_iterator_c::direction_in) ||
   813       if ((param_direction == function_param_iterator_c::direction_in) ||
   783           (param_direction == function_param_iterator_c::direction_inout)) {
   814           (param_direction == function_param_iterator_c::direction_inout)) {
   784         if (!this->is_variable_prefix_null()) {
   815         if (this->is_variable_prefix_null()) {
   785           s4o.print(SET_VAR);
   816           print_variable_prefix();
   786           s4o.print("(");
   817           symbol->fb_name->accept(*this);
       
   818           s4o.print(".");
       
   819           param_name->accept(*this);
       
   820           s4o.print(" = ");
       
   821           print_check_function(param_type, param_value);
   787         }
   822         }
   788     	print_variable_prefix();
   823         else {
   789         symbol->fb_name->accept(*this);
   824         	print_setter(param_name, param_type, param_value, symbol->fb_name);
   790         s4o.print(".");
   825         }
   791         param_name->accept(*this);
       
   792         if (this->is_variable_prefix_null())
       
   793           s4o.print(" = ");
       
   794         else
       
   795           s4o.print(",");
       
   796         print_check_function(param_type, param_value);
       
   797         if (!this->is_variable_prefix_null())
       
   798           s4o.print(")");
       
   799         s4o.print(";\n" + s4o.indent_spaces);
   826         s4o.print(";\n" + s4o.indent_spaces);
   800       }
   827       }
   801   } /* for(...) */
   828   } /* for(...) */
   802 
   829 
   803   /* now call the function... */
   830   /* now call the function... */
   825 
   852 
   826     /* now output the value assignment */
   853     /* now output the value assignment */
   827     if (param_value != NULL)
   854     if (param_value != NULL)
   828       if ((param_direction == function_param_iterator_c::direction_out) ||
   855       if ((param_direction == function_param_iterator_c::direction_out) ||
   829           (param_direction == function_param_iterator_c::direction_inout)) {
   856           (param_direction == function_param_iterator_c::direction_inout)) {
   830         symbol_c *param_type = search_varfb_instance_type->get_type(param_value, false);
   857         symbol_c *param_type = search_varfb_instance_type->get_rawtype(param_value);
   831 
   858         if (this->is_variable_prefix_null()) {
   832         if (!this->is_variable_prefix_null()) {
       
   833           unsigned int vartype = search_varfb_instance_type->get_vartype(param_value);
       
   834           s4o.print(";\n"+ s4o.indent_spaces);
       
   835           if (vartype == search_var_instance_decl_c::external_vt)
       
   836             s4o.print(SET_EXTERNAL);
       
   837           else if (vartype == search_var_instance_decl_c::located_vt)
       
   838             s4o.print(SET_LOCATED);
       
   839           else
       
   840             s4o.print(SET_VAR);
       
   841           s4o.print("(");
       
   842 
       
   843           wanted_variablegeneration = assignment_vg;
       
   844           param_value->accept(*this);
   859           param_value->accept(*this);
   845           wanted_variablegeneration = expression_vg;
   860           s4o.print(" = ");
   846           s4o.print(",");
   861           print_check_function(param_type, param_name, symbol->fb_name);
   847         }
   862         }
   848         else {
   863         else {
   849           param_value->accept(*this);
   864           print_setter(param_value, param_type, param_name, NULL, symbol->fb_name);
   850           s4o.print(" = ");
       
   851         }
   865         }
   852         print_check_function(param_type, param_name, symbol->fb_name);
       
   853         if (!this->is_variable_prefix_null())
       
   854           s4o.print(")");
       
   855       }
   866       }
   856   } /* for(...) */
   867   } /* for(...) */
   857 
   868 
   858   return NULL;
   869   return NULL;
   859 }
   870 }