stage4/generate_c/generate_c_il.cc
changeset 625 c0bda77b37a0
parent 411 3e21d98d0a71
parent 594 c8092e909886
child 667 bd1360f29f15
child 706 31553c22f318
equal deleted inserted replaced
412:aad38592bdde 625:c0bda77b37a0
   230      * a function declaration.
   230      * a function declaration.
   231      */
   231      */
   232     search_fb_instance_decl_c *search_fb_instance_decl;
   232     search_fb_instance_decl_c *search_fb_instance_decl;
   233 
   233 
   234     search_varfb_instance_type_c *search_varfb_instance_type;
   234     search_varfb_instance_type_c *search_varfb_instance_type;
       
   235     search_var_instance_decl_c   *search_var_instance_decl;
   235 
   236 
   236     search_base_type_c search_base_type;
   237     search_base_type_c search_base_type;
   237 
   238 
   238     symbol_c* current_array_type;
   239     symbol_c* current_array_type;
   239     symbol_c* current_param_type;
   240     symbol_c* current_param_type;
   250       default_variable_back_name(IL_DEFVAR_BACK, NULL)
   251       default_variable_back_name(IL_DEFVAR_BACK, NULL)
   251     {
   252     {
   252       search_expression_type = new search_expression_type_c(scope);
   253       search_expression_type = new search_expression_type_c(scope);
   253       search_fb_instance_decl = new search_fb_instance_decl_c(scope);
   254       search_fb_instance_decl = new search_fb_instance_decl_c(scope);
   254       search_varfb_instance_type = new search_varfb_instance_type_c(scope);
   255       search_varfb_instance_type = new search_varfb_instance_type_c(scope);
       
   256       search_var_instance_decl   = new search_var_instance_decl_c(scope);
       
   257       
   255       current_operand = NULL;
   258       current_operand = NULL;
   256       current_operand_type = NULL;
   259       current_operand_type = NULL;
   257       il_default_variable_init_value = NULL;
   260       il_default_variable_init_value = NULL;
   258       current_array_type = NULL;
   261       current_array_type = NULL;
   259       current_param_type = NULL;
   262       current_param_type = NULL;
   265 
   268 
   266     virtual ~generate_c_il_c(void) {
   269     virtual ~generate_c_il_c(void) {
   267       delete search_fb_instance_decl;
   270       delete search_fb_instance_decl;
   268       delete search_expression_type;
   271       delete search_expression_type;
   269       delete search_varfb_instance_type;
   272       delete search_varfb_instance_type;
       
   273       delete search_var_instance_decl;
   270     }
   274     }
   271 
   275 
   272     void generate(instruction_list_c *il) {
   276     void generate(instruction_list_c *il) {
   273       il->accept(*this);
   277       il->accept(*this);
   274     }
   278     }
   433                search_expression_type->is_literal_real_type(this->current_operand_type))
   437                search_expression_type->is_literal_real_type(this->current_operand_type))
   434         this->current_operand_type = this->default_variable_name.current_type;
   438         this->current_operand_type = this->default_variable_name.current_type;
   435     }
   439     }
   436 
   440 
   437     void *print_getter(symbol_c *symbol) {
   441     void *print_getter(symbol_c *symbol) {
   438       unsigned int vartype = search_varfb_instance_type->get_vartype(symbol);
   442       unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
   439       if (wanted_variablegeneration == fparam_output_vg) {
   443       if (wanted_variablegeneration == fparam_output_vg) {
   440       	if (vartype == search_var_instance_decl_c::external_vt)
   444       	if (vartype == search_var_instance_decl_c::external_vt)
   441           s4o.print(GET_EXTERNAL_BY_REF);
   445           s4o.print(GET_EXTERNAL_BY_REF);
   442         else if (vartype == search_var_instance_decl_c::located_vt)
   446         else if (vartype == search_var_instance_decl_c::located_vt)
   443           s4o.print(GET_LOCATED_BY_REF);
   447           s4o.print(GET_LOCATED_BY_REF);
   455       s4o.print("(");
   459       s4o.print("(");
   456 
   460 
   457       variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration;
   461       variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration;
   458       wanted_variablegeneration = complextype_base_vg;
   462       wanted_variablegeneration = complextype_base_vg;
   459       symbol->accept(*this);
   463       symbol->accept(*this);
   460       if (search_varfb_instance_type->type_is_complex())
   464       if (search_var_instance_decl->type_is_complex(symbol))
   461         s4o.print(",");
   465         s4o.print(",");
   462       wanted_variablegeneration = complextype_suffix_vg;
   466       wanted_variablegeneration = complextype_suffix_vg;
   463       symbol->accept(*this);
   467       symbol->accept(*this);
   464       s4o.print(")");
   468       s4o.print(")");
   465       wanted_variablegeneration = old_wanted_variablegeneration;
   469       wanted_variablegeneration = old_wanted_variablegeneration;
   473     		symbol_c* fb_value = NULL,
   477     		symbol_c* fb_value = NULL,
   474     		bool negative = false) {
   478     		bool negative = false) {
   475 
   479 
   476       bool type_is_complex = false;
   480       bool type_is_complex = false;
   477       if (fb_symbol == NULL) {
   481       if (fb_symbol == NULL) {
   478         unsigned int vartype = search_varfb_instance_type->get_vartype(symbol);
   482         unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
   479         type_is_complex = search_varfb_instance_type->type_is_complex();
   483         type_is_complex = search_var_instance_decl->type_is_complex(symbol);
   480         if (vartype == search_var_instance_decl_c::external_vt)
   484         if (vartype == search_var_instance_decl_c::external_vt)
   481           s4o.print(SET_EXTERNAL);
   485           s4o.print(SET_EXTERNAL);
   482         else if (vartype == search_var_instance_decl_c::located_vt)
   486         else if (vartype == search_var_instance_decl_c::located_vt)
   483           s4o.print(SET_LOCATED);
   487           s4o.print(SET_LOCATED);
   484         else
   488         else
   518       return NULL;
   522       return NULL;
   519     }
   523     }
   520 
   524 
   521 public:
   525 public:
   522 void *visit(il_default_variable_c *symbol) {
   526 void *visit(il_default_variable_c *symbol) {
   523   //s4o.print("il_default_variable_c VISITOR!!\n");
       
   524   symbol->var_name->accept(*this);
   527   symbol->var_name->accept(*this);
   525   if (NULL != symbol->current_type) {
   528   if (NULL != symbol->current_type) {
   526     s4o.print(".");
   529     s4o.print(".");
   527     if (search_expression_type->is_literal_integer_type(symbol->current_type))
   530     if      ( search_expression_type->is_literal_integer_type(symbol->current_type))                  this->lint_type.accept(*this);
   528       this->lint_type.accept(*this);
   531     else if ( search_expression_type->is_literal_real_type(this->default_variable_name.current_type)) this->lreal_type.accept(*this);
   529     else if (search_expression_type->is_literal_real_type(this->default_variable_name.current_type))
   532     else if ( search_expression_type->is_bool_type(this->default_variable_name.current_type))         this->bool_type.accept(*this); 
   530       this->lreal_type.accept(*this);
   533     else symbol->current_type->accept(*this);
   531     else
       
   532       symbol->current_type->accept(*this);
       
   533     s4o.print("var");
   534     s4o.print("var");
   534   }
   535   } return NULL;
   535   return NULL;
       
   536 }
   536 }
   537 
   537 
   538 
   538 
   539 private:
   539 private:
   540 
   540 
   589       break;
   589       break;
   590     case complextype_suffix_vg:
   590     case complextype_suffix_vg:
   591 	  break;
   591 	  break;
   592     default:
   592     default:
   593       if (this->is_variable_prefix_null()) {
   593       if (this->is_variable_prefix_null()) {
   594 	    vartype = search_varfb_instance_type->get_vartype(symbol);
   594 	    vartype = search_var_instance_decl->get_vartype(symbol);
   595         if (wanted_variablegeneration == fparam_output_vg) {
   595         if (wanted_variablegeneration == fparam_output_vg) {
   596           s4o.print("&(");
   596           s4o.print("&(");
   597           generate_c_base_c::visit(symbol);
   597           generate_c_base_c::visit(symbol);
   598           s4o.print(")");
   598           s4o.print(")");
   599         }
   599         }
   647 /*************************************/
   647 /*************************************/
   648 
   648 
   649 // SYM_REF2(structured_variable_c, record_variable, field_selector)
   649 // SYM_REF2(structured_variable_c, record_variable, field_selector)
   650 void *visit(structured_variable_c *symbol) {
   650 void *visit(structured_variable_c *symbol) {
   651   TRACE("structured_variable_c");
   651   TRACE("structured_variable_c");
   652   unsigned int vartype = search_varfb_instance_type->get_vartype(symbol->record_variable);
   652   bool type_is_complex = search_var_instance_decl->type_is_complex(symbol->record_variable);
   653   bool type_is_complex = search_varfb_instance_type->type_is_complex();
       
   654   switch (wanted_variablegeneration) {
   653   switch (wanted_variablegeneration) {
   655     case complextype_base_vg:
   654     case complextype_base_vg:
   656     case complextype_base_assignment_vg:
   655     case complextype_base_assignment_vg:
   657       symbol->record_variable->accept(*this);
   656       symbol->record_variable->accept(*this);
   658       if (!type_is_complex) {
   657       if (!type_is_complex) {
   981       /* function being called is overloaded! */
   980       /* function being called is overloaded! */
   982       s4o.print("__");
   981       s4o.print("__");
   983       print_function_parameter_data_types_c overloaded_func_suf(&s4o);
   982       print_function_parameter_data_types_c overloaded_func_suf(&s4o);
   984       f_decl->accept(overloaded_func_suf);
   983       f_decl->accept(overloaded_func_suf);
   985     }
   984     }
   986     s4o.print_integer(fcall_number);
   985     s4o.print(fcall_number);
   987   }
   986   }
   988   else {
   987   else {
   989     if (function_name != NULL) {
   988     if (function_name != NULL) {
   990           function_name->accept(*this);
   989           function_name->accept(*this);
   991           if (fdecl_mutiplicity == 2) {
   990           if (fdecl_mutiplicity == 2) {
  1389       /* function being called is overloaded! */
  1388       /* function being called is overloaded! */
  1390       s4o.print("__");
  1389       s4o.print("__");
  1391       print_function_parameter_data_types_c overloaded_func_suf(&s4o);
  1390       print_function_parameter_data_types_c overloaded_func_suf(&s4o);
  1392       f_decl->accept(overloaded_func_suf);
  1391       f_decl->accept(overloaded_func_suf);
  1393     }
  1392     }
  1394     s4o.print_integer(fcall_number);
  1393     s4o.print(fcall_number);
  1395   }
  1394   }
  1396   else {
  1395   else {
  1397     if (function_name != NULL) {
  1396     if (function_name != NULL) {
  1398       function_name->accept(*this);
  1397       function_name->accept(*this);
  1399       if (fdecl_mutiplicity == 2) {
  1398       if (fdecl_mutiplicity == 2) {
  1572   s4o.print("}\n");
  1571   s4o.print("}\n");
  1573   s4o.print(s4o.indent_spaces);
  1572   s4o.print(s4o.indent_spaces);
  1574   return NULL;
  1573   return NULL;
  1575 }
  1574 }
  1576 
  1575 
       
  1576 // SYM_REF1(il_simple_instruction_c, il_simple_instruction, symbol_c *prev_il_instruction;)
       
  1577 void *visit(il_simple_instruction_c *symbol)	{
       
  1578   return symbol->il_simple_instruction->accept(*this);
       
  1579 }
       
  1580 
       
  1581 
  1577 /* | il_initial_param_list il_param_instruction */
  1582 /* | il_initial_param_list il_param_instruction */
  1578 // SYM_LIST(il_param_list_c)
  1583 // SYM_LIST(il_param_list_c)
  1579 void *visit(il_param_list_c *symbol) {ERROR; return NULL;} // should never get called!
  1584 void *visit(il_param_list_c *symbol) {ERROR; return NULL;} // should never get called!
  1580 
  1585 
  1581 /*  il_assign_operator il_operand
  1586 /*  il_assign_operator il_operand
  1650   /* the data type resulting from this operation is unchanged. */
  1655   /* the data type resulting from this operation is unchanged. */
  1651   return NULL;
  1656   return NULL;
  1652 }
  1657 }
  1653 
  1658 
  1654 void *visit(NOT_operator_c *symbol)	{
  1659 void *visit(NOT_operator_c *symbol)	{
       
  1660   /* NOTE: the standard allows syntax in which the NOT operator is followed by an optional <il_operand>
       
  1661    *              NOT [<il_operand>]
       
  1662    *       However, it does not define the semantic of the NOT operation when the <il_operand> is specified.
       
  1663    *       We therefore consider it an error if an il_operand is specified!
       
  1664    *       The error is caught in stage 3!
       
  1665    */  
  1655   if ((NULL != this->current_operand) || (NULL != this->current_operand_type)) ERROR;
  1666   if ((NULL != this->current_operand) || (NULL != this->current_operand_type)) ERROR;
  1656   XXX_operator(&(this->default_variable_name),
  1667   XXX_operator(&(this->default_variable_name),
  1657                search_expression_type->is_bool_type(this->default_variable_name.current_type)?" = !":" = ~",
  1668                search_expression_type->is_bool_type(this->default_variable_name.current_type)?" = !":" = ~",
  1658                &(this->default_variable_name));
  1669                &(this->default_variable_name));
  1659   /* the data type resulting from this operation is unchanged. */
  1670   /* the data type resulting from this operation is unchanged. */