stage4/generate_c/generate_c_inlinefcall.cc
changeset 670 400bf52a2691
parent 667 bd1360f29f15
child 683 2d96a47a75b1
equal deleted inserted replaced
669:7049fd6fe515 670:400bf52a2691
   101 
   101 
   102 	int fcall_number;
   102 	int fcall_number;
   103 	bool generating_inlinefunction;
   103 	bool generating_inlinefunction;
   104 	symbol_c *fbname;
   104 	symbol_c *fbname;
   105 
   105 
   106     search_expression_type_c *search_expression_type;
       
   107 
       
   108     search_varfb_instance_type_c *search_varfb_instance_type;
   106     search_varfb_instance_type_c *search_varfb_instance_type;
   109     search_var_instance_decl_c   *search_var_instance_decl;
   107     search_var_instance_decl_c   *search_var_instance_decl;
   110 
   108 
   111     search_base_type_c search_base_type;
   109     search_base_type_c search_base_type;
   112 
   110 
   116     generate_c_inlinefcall_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL)
   114     generate_c_inlinefcall_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL)
   117     : generate_c_typedecl_c(s4o_ptr),
   115     : generate_c_typedecl_c(s4o_ptr),
   118       default_variable_name(IL_DEFVAR, NULL),
   116       default_variable_name(IL_DEFVAR, NULL),
   119       default_variable_back_name(IL_DEFVAR_BACK, NULL)
   117       default_variable_back_name(IL_DEFVAR_BACK, NULL)
   120     {
   118     {
   121       search_expression_type = new search_expression_type_c(scope);
       
   122       search_varfb_instance_type = new search_varfb_instance_type_c(scope);
   119       search_varfb_instance_type = new search_varfb_instance_type_c(scope);
   123       search_var_instance_decl   = new search_var_instance_decl_c(scope);
   120       search_var_instance_decl   = new search_var_instance_decl_c  (scope);
   124       
   121       
   125       this->set_variable_prefix(variable_prefix);
   122       this->set_variable_prefix(variable_prefix);
   126       current_operand = NULL;
   123       current_operand = NULL;
   127       current_operand_type = NULL;
   124       current_operand_type = NULL;
   128       il_default_variable_init_value = NULL;
   125       il_default_variable_init_value = NULL;
   131       wanted_variablegeneration = expression_vg;
   128       wanted_variablegeneration = expression_vg;
   132       generating_inlinefunction = false;
   129       generating_inlinefunction = false;
   133     }
   130     }
   134 
   131 
   135     virtual ~generate_c_inlinefcall_c(void) {
   132     virtual ~generate_c_inlinefcall_c(void) {
   136       delete search_expression_type;
       
   137       delete search_varfb_instance_type;
   133       delete search_varfb_instance_type;
   138       delete search_var_instance_decl;
   134       delete search_var_instance_decl;
   139     }
   135     }
   140 
   136 
   141     void print(symbol_c* symbol) {
   137     void print(symbol_c* symbol) {
   156 
   152 
   157       std::list<FUNCTION_PARAM*>::iterator pt;
   153       std::list<FUNCTION_PARAM*>::iterator pt;
   158       generating_inlinefunction = true;
   154       generating_inlinefunction = true;
   159 
   155 
   160       fcall_number++;
   156       fcall_number++;
   161       function_type_prefix = search_expression_type->default_literal_type(function_type_prefix);
   157       function_type_prefix = default_literal_type(function_type_prefix);
   162       if (function_type_suffix) {
   158       if (function_type_suffix) {
   163         function_type_suffix = search_expression_type->default_literal_type(function_type_suffix);
   159         function_type_suffix = default_literal_type(function_type_suffix);
   164       }
   160       }
   165 
   161 
   166       s4o.print(s4o.indent_spaces);
   162       s4o.print(s4o.indent_spaces);
   167       s4o.print("inline ");
   163       s4o.print("inline ");
   168       function_type_prefix->accept(*this);
   164       function_type_prefix->accept(*this);
   183       s4o.print("(");
   179       s4o.print("(");
   184       s4o.indent_right();
   180       s4o.indent_right();
   185 
   181 
   186       PARAM_LIST_ITERATOR() {
   182       PARAM_LIST_ITERATOR() {
   187         if (PARAM_DIRECTION == function_param_iterator_c::direction_in) {
   183         if (PARAM_DIRECTION == function_param_iterator_c::direction_in) {
   188           search_expression_type->default_literal_type(PARAM_TYPE)->accept(*this);
   184           default_literal_type(PARAM_TYPE)->accept(*this);
   189           s4o.print(" ");
   185           s4o.print(" ");
   190           PARAM_NAME->accept(*this);
   186           PARAM_NAME->accept(*this);
   191           s4o.print(",\n" + s4o.indent_spaces);
   187           s4o.print(",\n" + s4o.indent_spaces);
   192         }
   188         }
   193       }
   189       }
   269 
   265 
   270       generating_inlinefunction = false;
   266       generating_inlinefunction = false;
   271     }
   267     }
   272 
   268 
   273   private:
   269   private:
       
   270     /* a small helper function */
       
   271     symbol_c *default_literal_type(symbol_c *symbol) {
       
   272       if (get_datatype_info_c::is_ANY_INT_literal(symbol)) {
       
   273         return &search_constant_type_c::lint_type_name;
       
   274       }
       
   275       else if (get_datatype_info_c::is_ANY_REAL_literal(symbol)) {
       
   276         return &search_constant_type_c::lreal_type_name;
       
   277       }
       
   278       return symbol;
       
   279     }
   274 
   280 
   275     /* A helper function... */
   281     /* A helper function... */
   276 	void CMP_operator_result_type() {
   282 	void CMP_operator_result_type() {
   277 	  /* the data type resulting from this operation... */
   283 	  /* the data type resulting from this operation... */
   278 	  this->default_variable_name.current_type = &(this->bool_type);
   284 	  this->default_variable_name.current_type = &(this->bool_type);
   279 	}
   285 	}
   280 
   286 
   281 	/* A helper function... */
   287 	/* A helper function... */
   282     void BYTE_operator_result_type(void) {
   288     void BYTE_operator_result_type(void) {
   283 	  if (search_expression_type->is_literal_integer_type(this->default_variable_name.current_type)) {
   289 	  if (get_datatype_info_c::is_ANY_INT_literal(this->default_variable_name.current_type)) {
   284 		if (search_expression_type->is_literal_integer_type(this->current_operand_type))
   290 		if (get_datatype_info_c::is_ANY_INT_literal(this->current_operand_type))
   285 		  this->default_variable_name.current_type = &(this->lword_type);
   291 		  this->default_variable_name.current_type = &(this->lword_type);
   286 		else
   292 		else
   287 		  this->default_variable_name.current_type = this->current_operand_type;
   293 		  this->default_variable_name.current_type = this->current_operand_type;
   288 	  }
   294 	  }
   289 	  else if (search_expression_type->is_literal_integer_type(this->current_operand_type))
   295 	  else if (get_datatype_info_c::is_ANY_INT_literal(this->current_operand_type))
   290 		  this->current_operand_type = this->default_variable_name.current_type;
   296 		  this->current_operand_type = this->default_variable_name.current_type;
   291 	}
   297 	}
   292 
   298 
   293     /* A helper function... */
   299     /* A helper function... */
   294     void NUM_operator_result_type(void) {
   300     void NUM_operator_result_type(void) {
   295 	  if (search_expression_type->is_literal_real_type(this->default_variable_name.current_type)) {
   301 	  if (get_datatype_info_c::is_ANY_REAL_literal(this->default_variable_name.current_type)) {
   296 		if (search_expression_type->is_literal_integer_type(this->current_operand_type) ||
   302 		if (get_datatype_info_c::is_ANY_INT_literal(this->current_operand_type) ||
   297 			search_expression_type->is_literal_real_type(this->current_operand_type))
   303 			get_datatype_info_c::is_ANY_REAL_literal(this->current_operand_type))
   298 		  this->default_variable_name.current_type = &(this->lreal_type);
   304 		  this->default_variable_name.current_type = &(this->lreal_type);
   299 		else
   305 		else
   300 		  this->default_variable_name.current_type = this->current_operand_type;
   306 		  this->default_variable_name.current_type = this->current_operand_type;
   301 	  }
   307 	  }
   302 	  else if (search_expression_type->is_literal_integer_type(this->default_variable_name.current_type)) {
   308 	  else if (get_datatype_info_c::is_ANY_INT_literal(this->default_variable_name.current_type)) {
   303 		if (search_expression_type->is_literal_integer_type(this->current_operand_type))
   309 		if (get_datatype_info_c::is_ANY_INT_literal(this->current_operand_type))
   304 		  this->default_variable_name.current_type = &(this->lint_type);
   310 		  this->default_variable_name.current_type = &(this->lint_type);
   305 		else if (search_expression_type->is_literal_real_type(this->current_operand_type))
   311 		else if (get_datatype_info_c::is_ANY_REAL_literal(this->current_operand_type))
   306 		  this->default_variable_name.current_type = &(this->lreal_type);
   312 		  this->default_variable_name.current_type = &(this->lreal_type);
   307 		else
   313 		else
   308 		  this->default_variable_name.current_type = this->current_operand_type;
   314 		  this->default_variable_name.current_type = this->current_operand_type;
   309 	  }
   315 	  }
   310 	  else if (search_expression_type->is_literal_integer_type(this->current_operand_type) ||
   316 	  else if (get_datatype_info_c::is_ANY_INT_literal(this->current_operand_type) ||
   311 			   search_expression_type->is_literal_real_type(this->current_operand_type))
   317 			   get_datatype_info_c::is_ANY_REAL_literal(this->current_operand_type))
   312 		this->current_operand_type = this->default_variable_name.current_type;
   318 		this->current_operand_type = this->default_variable_name.current_type;
   313 	}
   319 	}
   314 
   320 
   315     void *print_getter(symbol_c *symbol) {
   321     void *print_getter(symbol_c *symbol) {
   316       unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
   322       unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
   332       wanted_variablegeneration = expression_vg;
   338       wanted_variablegeneration = expression_vg;
   333       return NULL;
   339       return NULL;
   334     }
   340     }
   335 
   341 
   336     void *print_setter(symbol_c* symbol,
   342     void *print_setter(symbol_c* symbol,
   337     		symbol_c* type,
   343                        symbol_c* type,
   338     		symbol_c* value) {
   344                        symbol_c* value) {
   339       unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
   345       unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
   340       if (vartype == search_var_instance_decl_c::external_vt)
   346       if (vartype == search_var_instance_decl_c::external_vt)
   341         s4o.print(SET_EXTERNAL);
   347         s4o.print(SET_EXTERNAL);
   342       else if (vartype == search_var_instance_decl_c::located_vt)
   348       else if (vartype == search_var_instance_decl_c::located_vt)
   343         s4o.print(SET_LOCATED);
   349         s4o.print(SET_LOCATED);
   475     void *visit(il_simple_operation_c *symbol) {
   481     void *visit(il_simple_operation_c *symbol) {
   476       this->current_operand = symbol->il_operand;
   482       this->current_operand = symbol->il_operand;
   477       if (NULL == this->current_operand) {
   483       if (NULL == this->current_operand) {
   478         this->current_operand_type = NULL;
   484         this->current_operand_type = NULL;
   479       } else {
   485       } else {
   480         this->current_operand_type = search_expression_type->get_type(this->current_operand);
   486         this->current_operand_type = this->current_operand->datatype;
   481         if (NULL == this->current_operand_type) ERROR;
   487         if (NULL == this->current_operand_type) ERROR;
   482       }
   488       }
   483 
   489 
   484       symbol->il_simple_operator->accept(*this);
   490       symbol->il_simple_operator->accept(*this);
   485 
   491 
   854       this->default_variable_name.current_type = this->current_operand_type;
   860       this->default_variable_name.current_type = this->current_operand_type;
   855       return NULL;
   861       return NULL;
   856     }
   862     }
   857 
   863 
   858     void *visit(AND_operator_c *symbol)	{
   864     void *visit(AND_operator_c *symbol)	{
   859       if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
   865       if (get_datatype_info_c::is_ANY_BIT_compatible(this->default_variable_name.current_type)) {
   860           search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
   861     	BYTE_operator_result_type();
   866     	BYTE_operator_result_type();
   862       }
   867       }
   863       else {ERROR;}
   868       else {ERROR;}
   864       return NULL;
   869       return NULL;
   865     }
   870     }
   866 
   871 
   867     void *visit(OR_operator_c *symbol)	{
   872     void *visit(OR_operator_c *symbol)	{
   868       if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
   873       if (get_datatype_info_c::is_ANY_BIT_compatible(this->default_variable_name.current_type)) {
   869           search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
   870     	BYTE_operator_result_type();
   874     	BYTE_operator_result_type();
   871       }
   875       }
   872       else {ERROR;}
   876       else {ERROR;}
   873       return NULL;
   877       return NULL;
   874     }
   878     }
   875 
   879 
   876     void *visit(XOR_operator_c *symbol)	{
   880     void *visit(XOR_operator_c *symbol)	{
   877       if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
   881       if (get_datatype_info_c::is_ANY_BIT_compatible(this->default_variable_name.current_type)) {
   878           search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
   879     	BYTE_operator_result_type();
   882     	BYTE_operator_result_type();
   880       }
   883       }
   881       else {ERROR;}
   884       else {ERROR;}
   882       return NULL;
   885       return NULL;
   883     }
   886     }
   884 
   887 
   885     void *visit(ANDN_operator_c *symbol)	{
   888     void *visit(ANDN_operator_c *symbol)	{
   886       if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
   889       if (get_datatype_info_c::is_ANY_BIT_compatible(this->default_variable_name.current_type)) {
   887           search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
   888     	BYTE_operator_result_type();
   890     	BYTE_operator_result_type();
   889       }
   891       }
   890       else {ERROR;}
   892       else {ERROR;}
   891       return NULL;
   893       return NULL;
   892     }
   894     }
   893 
   895 
   894     void *visit(ORN_operator_c *symbol)	{
   896     void *visit(ORN_operator_c *symbol)	{
   895       if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
   897       if (get_datatype_info_c::is_ANY_BIT_compatible(this->default_variable_name.current_type)) {
   896           search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
   897     	BYTE_operator_result_type();
   898     	BYTE_operator_result_type();
   898       }
   899       }
   899       else {ERROR;}
   900       else {ERROR;}
   900       return NULL;
   901       return NULL;
   901     }
   902     }
   902 
   903 
   903     void *visit(XORN_operator_c *symbol)	{
   904     void *visit(XORN_operator_c *symbol)	{
   904       if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
   905       if (get_datatype_info_c::is_ANY_BIT_compatible(this->default_variable_name.current_type)) {
   905           search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
   906     	BYTE_operator_result_type();
   906     	BYTE_operator_result_type();
   907       }
   907       }
   908       else {ERROR;}
   908       else {ERROR;}
   909       return NULL;
   909       return NULL;
   910     }
   910     }
   911 
   911 
   912     void *visit(ADD_operator_c *symbol)	{
   912     void *visit(ADD_operator_c *symbol)	{
   913       if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
   913       if (get_datatype_info_c::is_TIME_compatible      (symbol->datatype) ||
   914           search_expression_type->is_time_type(this->current_operand_type)) {
   914           get_datatype_info_c::is_ANY_DATE_compatible  (symbol->datatype)) {
   915         /* the data type resulting from this operation... */
   915         /* the data type resulting from this operation... */
   916         this->default_variable_name.current_type = this->current_operand_type;
   916         this->default_variable_name.current_type = this->current_operand_type;
   917       }
   917       } else {
   918       else if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
       
   919           search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
   920         NUM_operator_result_type();
   918         NUM_operator_result_type();
   921       }
   919       }
   922       else {ERROR;}
       
   923       return NULL;
   920       return NULL;
   924     }
   921     }
   925 
   922 
   926     void *visit(SUB_operator_c *symbol)	{
   923     void *visit(SUB_operator_c *symbol)	{
   927       if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
   924       if (get_datatype_info_c::is_TIME_compatible      (symbol->datatype) ||
   928           search_expression_type->is_time_type(this->current_operand_type)) {
   925           get_datatype_info_c::is_ANY_DATE_compatible  (symbol->datatype)) {
   929         /* the data type resulting from this operation... */
   926         /* the data type resulting from this operation... */
   930         this->default_variable_name.current_type = this->current_operand_type;
   927         this->default_variable_name.current_type = this->current_operand_type;
   931       }
   928       } else {
   932       else if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
   929         NUM_operator_result_type();
   933           search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
   930       }
   934     	NUM_operator_result_type();
       
   935       }
       
   936       else {ERROR;}
       
   937       return NULL;
   931       return NULL;
   938     }
   932     }
   939 
   933 
   940     void *visit(MUL_operator_c *symbol)	{
   934     void *visit(MUL_operator_c *symbol)	{
   941       if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
   935       if (get_datatype_info_c::is_TIME_compatible      (symbol->datatype)) {
   942           search_expression_type->is_integer_type(this->current_operand_type)) {
       
   943         /* the data type resulting from this operation is unchanged! */
   936         /* the data type resulting from this operation is unchanged! */
   944       }
   937       } else {
   945       else if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
   938         NUM_operator_result_type();
   946           search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
   939       }
   947     	NUM_operator_result_type();
       
   948       }
       
   949       else {ERROR;}
       
   950       return NULL;
   940       return NULL;
   951     }
   941     }
   952 
   942 
   953     void *visit(DIV_operator_c *symbol)	{
   943     void *visit(DIV_operator_c *symbol)	{
   954       if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
   944       if (get_datatype_info_c::is_TIME_compatible      (symbol->datatype)) {
   955           search_expression_type->is_integer_type(this->current_operand_type)) {
       
   956         /* the data type resulting from this operation is unchanged! */
   945         /* the data type resulting from this operation is unchanged! */
   957       }
   946       } else {
   958       else if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
       
   959           search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
   960         NUM_operator_result_type();
   947         NUM_operator_result_type();
   961       }
   948       }
   962       else {ERROR;}
       
   963       return NULL;
   949       return NULL;
   964     }
   950     }
   965 
   951 
   966     void *visit(MOD_operator_c *symbol)	{
   952     void *visit(MOD_operator_c *symbol)	{
   967       if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
   953       NUM_operator_result_type();
   968           search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
   969         NUM_operator_result_type();
       
   970       }
       
   971       else {ERROR;}
       
   972       return NULL;
   954       return NULL;
   973     }
   955     }
   974 
   956 
   975     void *visit(GT_operator_c *symbol)	{CMP_operator_result_type(); return NULL;}
   957     void *visit(GT_operator_c *symbol)	{CMP_operator_result_type(); return NULL;}
   976     void *visit(GE_operator_c *symbol)	{CMP_operator_result_type(); return NULL;}
   958     void *visit(GE_operator_c *symbol)	{CMP_operator_result_type(); return NULL;}