stage4/generate_c/generate_c_il.cc
changeset 98 d0cdf1d00b74
parent 70 e1f0ebd2d9ec
child 123 a9b4af71cfa4
equal deleted inserted replaced
97:55ffcf693d6d 98:d0cdf1d00b74
   289      * so we do not create an object instance when handling
   289      * so we do not create an object instance when handling
   290      * a function declaration.
   290      * a function declaration.
   291      */
   291      */
   292     search_fb_instance_decl_c *search_fb_instance_decl;
   292     search_fb_instance_decl_c *search_fb_instance_decl;
   293 
   293 
       
   294     search_varfb_instance_type_c *search_varfb_instance_type;
       
   295 
       
   296     search_base_type_c search_base_type;
   294 
   297 
   295   public:
   298   public:
   296     generate_c_il_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL)
   299     generate_c_il_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL)
   297     : generate_c_typedecl_c(s4o_ptr),
   300     : generate_c_typedecl_c(s4o_ptr),
   298       default_variable_name(IL_DEFVAR, NULL),
   301       default_variable_name(IL_DEFVAR, NULL),
   299       default_variable_back_name(IL_DEFVAR_BACK, NULL)
   302       default_variable_back_name(IL_DEFVAR_BACK, NULL)
   300     {
   303     {
   301       //search_il_operand_type  = new search_il_operand_type_c(scope);
   304       //search_il_operand_type  = new search_il_operand_type_c(scope);
   302       search_expression_type = new search_expression_type_c(scope);
   305       search_expression_type = new search_expression_type_c(scope);
   303       search_fb_instance_decl = new search_fb_instance_decl_c(scope);
   306       search_fb_instance_decl = new search_fb_instance_decl_c(scope);
       
   307       search_varfb_instance_type = new search_varfb_instance_type_c(scope);
   304       current_operand = NULL;
   308       current_operand = NULL;
   305       current_operand_type = NULL;
   309       current_operand_type = NULL;
   306       il_default_variable_init_value = NULL;
   310       il_default_variable_init_value = NULL;
   307       this->set_variable_prefix(variable_prefix);
   311       this->set_variable_prefix(variable_prefix);
   308     }
   312     }
   309 
   313 
   310     virtual ~generate_c_il_c(void) {
   314     virtual ~generate_c_il_c(void) {
   311       delete search_fb_instance_decl;
   315       delete search_fb_instance_decl;
   312       //delete search_il_operand_type;
   316       //delete search_il_operand_type;
   313       delete search_expression_type;
   317       delete search_expression_type;
       
   318       delete search_varfb_instance_type;
   314     }
   319     }
   315 
   320 
   316     void generate(instruction_list_c *il) {
   321     void generate(instruction_list_c *il) {
   317       generate_c_tempvardecl_c generate_c_tempvardecl(&s4o);
   322       generate_c_tempvardecl_c generate_c_tempvardecl(&s4o);
   318       generate_c_tempvardecl.generate(il, &temp_var_name_factory);
   323       generate_c_tempvardecl.generate(il, &temp_var_name_factory);
   811 
   816 
   812     /* Get the value from a foo(<param_value>) style call */
   817     /* Get the value from a foo(<param_value>) style call */
   813     if (param_value == NULL)
   818     if (param_value == NULL)
   814       param_value = function_call_param_iterator.next();
   819       param_value = function_call_param_iterator.next();
   815 
   820 
       
   821     symbol_c *param_type = fp_iterator.param_type();
       
   822     if (param_type == NULL) ERROR;
       
   823     
       
   824     search_base_type.explore_type(param_type);    
       
   825 
   816     /* now output the value assignment */
   826     /* now output the value assignment */
   817     if (param_value != NULL)
   827     if (param_value != NULL)
   818       if ((param_direction == function_param_iterator_c::direction_in) ||
   828       if ((param_direction == function_param_iterator_c::direction_in) ||
   819           (param_direction == function_param_iterator_c::direction_inout)) {
   829           (param_direction == function_param_iterator_c::direction_inout)) {
   820         symbol->fb_name->accept(*this);
   830         symbol->fb_name->accept(*this);
   821         s4o.print(".");
   831         s4o.print(".");
   822         param_name->accept(*this);
   832         param_name->accept(*this);
   823         s4o.print(" = ");
   833         s4o.print(" = ");
       
   834         if (search_base_type.base_is_subrange()) {
       
   835           s4o.print("__CHECK_");
       
   836           param_type->accept(*this);
       
   837           s4o.print("(");
       
   838         }
   824         param_value->accept(*this);
   839         param_value->accept(*this);
       
   840         if (search_base_type.base_is_subrange())
       
   841           s4o.print(")");
   825         s4o.print(";\n" + s4o.indent_spaces);
   842         s4o.print(";\n" + s4o.indent_spaces);
   826       }
   843       }
   827   } /* for(...) */
   844   } /* for(...) */
   828 
   845 
   829   /* now call the function... */
   846   /* now call the function... */
   850 
   867 
   851     /* now output the value assignment */
   868     /* now output the value assignment */
   852     if (param_value != NULL)
   869     if (param_value != NULL)
   853       if ((param_direction == function_param_iterator_c::direction_out) ||
   870       if ((param_direction == function_param_iterator_c::direction_out) ||
   854           (param_direction == function_param_iterator_c::direction_inout)) {
   871           (param_direction == function_param_iterator_c::direction_inout)) {
       
   872         symbol_c *param_type = search_varfb_instance_type->get_type(param_value, false);
       
   873         search_base_type.explore_type(param_type);
       
   874         
   855         s4o.print(";\n"+ s4o.indent_spaces);
   875         s4o.print(";\n"+ s4o.indent_spaces);
   856         param_value->accept(*this);
   876         param_value->accept(*this);
   857         s4o.print(" = ");
   877         s4o.print(" = ");
       
   878         if (search_base_type.base_is_subrange()) {
       
   879           s4o.print("__CHECK_");
       
   880           param_type->accept(*this);
       
   881           s4o.print("(");
       
   882         }
   858         symbol->fb_name->accept(*this);
   883         symbol->fb_name->accept(*this);
   859         s4o.print(".");
   884         s4o.print(".");
   860         param_name->accept(*this);
   885         param_name->accept(*this);
       
   886         if (search_base_type.base_is_subrange())
       
   887           s4o.print(")");
   861       }
   888       }
   862   } /* for(...) */
   889   } /* for(...) */
   863 
   890 
   864   s4o.print(";\n");
   891   s4o.print(";\n");
   865   s4o.indent_left();
   892   s4o.indent_left();
   914      * with the function calling code in generate_c_st_c, which does require
   941      * with the function calling code in generate_c_st_c, which does require
   915      * the following line...
   942      * the following line...
   916      */
   943      */
   917     if (param_value == NULL)
   944     if (param_value == NULL)
   918       param_value = function_call_param_iterator.next();
   945       param_value = function_call_param_iterator.next();
       
   946 
       
   947     search_base_type.explore_type(param_type);
   919 
   948 
   920     switch (param_direction) {
   949     switch (param_direction) {
   921       case function_param_iterator_c::direction_in:
   950       case function_param_iterator_c::direction_in:
   922         if (param_value == NULL) {
   951         if (param_value == NULL) {
   923           /* No value given for parameter, so we must use the default... */
   952           /* No value given for parameter, so we must use the default... */
   927         if (param_value == NULL) {
   956         if (param_value == NULL) {
   928           /* If not, get the default value of this variable's type */
   957           /* If not, get the default value of this variable's type */
   929           param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
   958           param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
   930         }
   959         }
   931         if (param_value == NULL) ERROR;
   960         if (param_value == NULL) ERROR;
       
   961         if (search_base_type.base_is_subrange()) {
       
   962           s4o.print("__CHECK_");
       
   963           param_type->accept(*this);
       
   964           s4o.print("(");
       
   965         }
   932         param_value->accept(*this);
   966         param_value->accept(*this);
       
   967         if (search_base_type.base_is_subrange())
       
   968           s4o.print(")");
   933 	break;
   969 	break;
   934       case function_param_iterator_c::direction_out:
   970       case function_param_iterator_c::direction_out:
   935       case function_param_iterator_c::direction_inout:
   971       case function_param_iterator_c::direction_inout:
   936         if (param_value == NULL) {
   972         if (param_value == NULL) {
   937 	  /* no parameter value given, so we pass a previously declared temporary variable. */
   973 	  /* no parameter value given, so we pass a previously declared temporary variable. */
  1093                this->current_operand);
  1129                this->current_operand);
  1094   return NULL;
  1130   return NULL;
  1095 }
  1131 }
  1096 
  1132 
  1097 void *visit(ST_operator_c *symbol)	{
  1133 void *visit(ST_operator_c *symbol)	{
  1098   XXX_operator(this->current_operand, " = ",&(this->default_variable_name));
  1134   symbol_c *operand_type = search_varfb_instance_type->get_type(this->current_operand, false);
       
  1135   search_base_type.explore_type(operand_type);
       
  1136 
       
  1137   this->current_operand->accept(*this);
       
  1138   s4o.print(" = ");
       
  1139   if (search_base_type.base_is_subrange()) {
       
  1140     s4o.print("__CHECK_");
       
  1141     operand_type->accept(*this);
       
  1142     s4o.print("(");
       
  1143   }
       
  1144   this->default_variable_name.accept(*this);
       
  1145   if (search_base_type.base_is_subrange())
       
  1146     s4o.print(")");
  1099   /* the data type resulting from this operation is unchamged. */
  1147   /* the data type resulting from this operation is unchamged. */
  1100   return NULL;
  1148   return NULL;
  1101 }
  1149 }
  1102 
  1150 
  1103 void *visit(STN_operator_c *symbol)	{
  1151 void *visit(STN_operator_c *symbol)	{
  1104   XXX_operator(this->current_operand,
  1152   symbol_c *operand_type = search_varfb_instance_type->get_type(this->current_operand, false);
  1105                search_expression_type->is_bool_type(this->current_operand_type)?" = !":" = ~",
  1153   search_base_type.explore_type(operand_type);
  1106                &(this->default_variable_name));
  1154 
       
  1155   this->current_operand->accept(*this);
       
  1156   s4o.print(" = ");
       
  1157   if (search_base_type.base_is_subrange()) {
       
  1158     s4o.print("__CHECK_");
       
  1159     operand_type->accept(*this);
       
  1160     s4o.print("(");
       
  1161   }
       
  1162   if (search_expression_type->is_bool_type(this->current_operand_type))
       
  1163     s4o.print("!");
       
  1164   else
       
  1165     s4o.print("~");
       
  1166   this->default_variable_name.accept(*this);
       
  1167   if (search_base_type.base_is_subrange())
       
  1168     s4o.print(")");
  1107   /* the data type resulting from this operation is unchamged. */
  1169   /* the data type resulting from this operation is unchamged. */
  1108   return NULL;
  1170   return NULL;
  1109 }
  1171 }
  1110 
  1172 
  1111 void *visit(NOT_operator_c *symbol)	{
  1173 void *visit(NOT_operator_c *symbol)	{