stage3/narrow_candidate_datatypes.cc
changeset 456 ca8d98289ff9
parent 455 933c0dccc82f
child 457 67d8b07bac22
equal deleted inserted replaced
455:933c0dccc82f 456:ca8d98289ff9
   257  *        CU counter_var
   257  *        CU counter_var
   258  *
   258  *
   259  * The algorithm will be to build a fake il_fb_call_c equivalent to the implicit IL FB call, and let 
   259  * The algorithm will be to build a fake il_fb_call_c equivalent to the implicit IL FB call, and let 
   260  * the visit(il_fb_call_c *) method handle it!
   260  * the visit(il_fb_call_c *) method handle it!
   261  */
   261  */
   262 void narrow_candidate_datatypes_c::narrow_implicit_il_fb_call(symbol_c *il_instruction, const char *param_name, symbol_c *&called_fb_declaration) {
   262 void *narrow_candidate_datatypes_c::narrow_implicit_il_fb_call(symbol_c *il_instruction, const char *param_name, symbol_c *&called_fb_declaration) {
   263 
   263 
   264 	/* set the datatype of the il_operand, this is, the FB being called! */
   264 	/* set the datatype of the il_operand, this is, the FB being called! */
   265 	if (NULL != il_operand) {
   265 	if (NULL != il_operand) {
   266 		/* only set it if it is in the candidate datatypes list! */  
   266 		/* only set it if it is in the candidate datatypes list! */  
   267 		set_datatype(called_fb_declaration, il_operand);
   267 		set_datatype(called_fb_declaration, il_operand);
   272 	if (NULL == prev_il_instruction) {
   272 	if (NULL == prev_il_instruction) {
   273 		/* This IL implicit FB call (e.g. CLK ton_var) is not preceded by another IL instruction
   273 		/* This IL implicit FB call (e.g. CLK ton_var) is not preceded by another IL instruction
   274 		 * (or list of instructions) that will set the IL current/default value.
   274 		 * (or list of instructions) that will set the IL current/default value.
   275 		 * We cannot proceed verifying type compatibility of something that does not exist.
   275 		 * We cannot proceed verifying type compatibility of something that does not exist.
   276 		 */
   276 		 */
   277 		return;
   277 		return NULL;
   278 	}
   278 	}
   279 
   279 
   280 	if (NULL == fb_decl) {
   280 	if (NULL == fb_decl) {
   281 		/* the il_operand is a not FB instance */
   281 		/* the il_operand is a not FB instance */
   282 		/* so we simply pass on the required datatype to the prev_il_instruction */
   282 		/* so we simply pass on the required datatype to the prev_il_instruction */
   283 		/* The invalid FB invocation will be caught by the print_datatypes_error_c by analysing NULL value in il_operand->datatype! */
   283 		/* The invalid FB invocation will be caught by the print_datatypes_error_c by analysing NULL value in il_operand->datatype! */
   284 		prev_il_instruction->datatype = il_instruction->datatype;
   284 		prev_il_instruction->datatype = il_instruction->datatype;
   285 		return;
   285 		return NULL;
   286 	}
   286 	}
   287 	
   287 	
   288 
   288 
   289 	/* The value being passed to the 'param_name' parameter is actually the prev_il_instruction.
   289 	/* The value being passed to the 'param_name' parameter is actually the prev_il_instruction.
   290 	 * However, we do not place that object directly in the fake il_param_list_c that we will be
   290 	 * However, we do not place that object directly in the fake il_param_list_c that we will be
   343 	if ((NULL == il_instruction->datatype) || (is_type_equal(param_value.datatype, il_instruction->datatype))) {
   343 	if ((NULL == il_instruction->datatype) || (is_type_equal(param_value.datatype, il_instruction->datatype))) {
   344 		prev_il_instruction->datatype = param_value.datatype;
   344 		prev_il_instruction->datatype = param_value.datatype;
   345 	} else {
   345 	} else {
   346 		prev_il_instruction->datatype = NULL;
   346 		prev_il_instruction->datatype = NULL;
   347 	}
   347 	}
       
   348 	return NULL;
   348 }
   349 }
   349 
   350 
   350 
   351 
   351 /* a helper function... */
   352 /* a helper function... */
   352 symbol_c *narrow_candidate_datatypes_c::base_type(symbol_c *symbol) {
   353 symbol_c *narrow_candidate_datatypes_c::base_type(symbol_c *symbol) {
   591   symbol_c *save_prev_il_instruction = prev_il_instruction; /*this is not really necessary, but lets play it safe */
   592   symbol_c *save_prev_il_instruction = prev_il_instruction; /*this is not really necessary, but lets play it safe */
   592   symbol->simple_instr_list->accept(*this);
   593   symbol->simple_instr_list->accept(*this);
   593   prev_il_instruction = save_prev_il_instruction;
   594   prev_il_instruction = save_prev_il_instruction;
   594 return NULL;
   595 return NULL;
   595 }
   596 }
       
   597 
   596 
   598 
   597 /*   il_call_operator prev_declared_fb_name
   599 /*   il_call_operator prev_declared_fb_name
   598  * | il_call_operator prev_declared_fb_name '(' ')'
   600  * | il_call_operator prev_declared_fb_name '(' ')'
   599  * | il_call_operator prev_declared_fb_name '(' eol_list ')'
   601  * | il_call_operator prev_declared_fb_name '(' eol_list ')'
   600  * | il_call_operator prev_declared_fb_name '(' il_operand_list ')'
   602  * | il_call_operator prev_declared_fb_name '(' il_operand_list ')'
   744 	prev_il_instruction->datatype = symbol->datatype;
   746 	prev_il_instruction->datatype = symbol->datatype;
   745 	return NULL;
   747 	return NULL;
   746 }
   748 }
   747 
   749 
   748 void *narrow_candidate_datatypes_c::visit(NOT_operator_c *symbol) {
   750 void *narrow_candidate_datatypes_c::visit(NOT_operator_c *symbol) {
       
   751   /* TODO: ... */
   749 	return NULL;
   752 	return NULL;
   750 }
   753 }
   751 
   754 
   752 void *narrow_candidate_datatypes_c::visit(S_operator_c *symbol)  {
   755 void *narrow_candidate_datatypes_c::visit(S_operator_c *symbol)  {
   753   /* TODO: what if this is a FB call? */
   756   /* TODO: what if this is a FB call? */
   757   /* TODO: what if this is a FB call? */
   760   /* TODO: what if this is a FB call? */
   758 	return handle_il_instruction(symbol);
   761 	return handle_il_instruction(symbol);
   759 }
   762 }
   760 
   763 
   761 
   764 
   762 void *narrow_candidate_datatypes_c::visit(S1_operator_c *symbol) {
   765 void *narrow_candidate_datatypes_c::visit(  S1_operator_c *symbol)  {return narrow_implicit_il_fb_call(symbol, "S1",  symbol->called_fb_declaration);}
   763 	narrow_implicit_il_fb_call(symbol, "S1", symbol->called_fb_declaration);
   766 void *narrow_candidate_datatypes_c::visit(  R1_operator_c *symbol)  {return narrow_implicit_il_fb_call(symbol, "R1",  symbol->called_fb_declaration);}
   764 	return NULL;
   767 void *narrow_candidate_datatypes_c::visit( CLK_operator_c *symbol)  {return narrow_implicit_il_fb_call(symbol, "CLK", symbol->called_fb_declaration);}
   765 }
   768 void *narrow_candidate_datatypes_c::visit(  CU_operator_c *symbol)  {return narrow_implicit_il_fb_call(symbol, "CU",  symbol->called_fb_declaration);}
   766 
   769 void *narrow_candidate_datatypes_c::visit(  CD_operator_c *symbol)  {return narrow_implicit_il_fb_call(symbol, "CD",  symbol->called_fb_declaration);}
   767 void *narrow_candidate_datatypes_c::visit(R1_operator_c *symbol) {
   770 void *narrow_candidate_datatypes_c::visit(  PV_operator_c *symbol)  {return narrow_implicit_il_fb_call(symbol, "PV",  symbol->called_fb_declaration);}
   768 	narrow_implicit_il_fb_call(symbol, "R1", symbol->called_fb_declaration);
   771 void *narrow_candidate_datatypes_c::visit(  IN_operator_c *symbol)  {return narrow_implicit_il_fb_call(symbol, "IN",  symbol->called_fb_declaration);}
   769 	return NULL;
   772 void *narrow_candidate_datatypes_c::visit(  PT_operator_c *symbol)  {return narrow_implicit_il_fb_call(symbol, "PT",  symbol->called_fb_declaration);}
   770 }
   773 
   771 
   774 
   772 void *narrow_candidate_datatypes_c::visit(CLK_operator_c *symbol) {
   775 void *narrow_candidate_datatypes_c::visit( AND_operator_c *symbol)  {return handle_il_instruction(symbol);}
   773 	narrow_implicit_il_fb_call(symbol, "CLK", symbol->called_fb_declaration);
   776 void *narrow_candidate_datatypes_c::visit(  OR_operator_c *symbol)  {return handle_il_instruction(symbol);}
   774 	return NULL;
   777 void *narrow_candidate_datatypes_c::visit( XOR_operator_c *symbol)  {return handle_il_instruction(symbol);}
   775 }
       
   776 
       
   777 void *narrow_candidate_datatypes_c::visit(CU_operator_c *symbol) {
       
   778 	narrow_implicit_il_fb_call(symbol, "CU", symbol->called_fb_declaration);
       
   779 	return NULL;
       
   780 }
       
   781 
       
   782 void *narrow_candidate_datatypes_c::visit(CD_operator_c *symbol) {
       
   783 	narrow_implicit_il_fb_call(symbol, "CD", symbol->called_fb_declaration);
       
   784 	return NULL;
       
   785 }
       
   786 
       
   787 void *narrow_candidate_datatypes_c::visit(PV_operator_c *symbol) {
       
   788 	narrow_implicit_il_fb_call(symbol, "PV", symbol->called_fb_declaration);
       
   789 	return NULL;
       
   790 }
       
   791 
       
   792 void *narrow_candidate_datatypes_c::visit(IN_operator_c *symbol) {
       
   793 	narrow_implicit_il_fb_call(symbol, "IN", symbol->called_fb_declaration);
       
   794 	return NULL;
       
   795 }
       
   796 
       
   797 void *narrow_candidate_datatypes_c::visit(PT_operator_c *symbol) {
       
   798 	narrow_implicit_il_fb_call(symbol, "PT", symbol->called_fb_declaration);
       
   799 	return NULL;
       
   800 }
       
   801 
       
   802 
       
   803 
       
   804 void *narrow_candidate_datatypes_c::visit(AND_operator_c  *symbol)  {return handle_il_instruction(symbol);}
       
   805 void *narrow_candidate_datatypes_c::visit(OR_operator_c   *symbol)  {return handle_il_instruction(symbol);}
       
   806 void *narrow_candidate_datatypes_c::visit(XOR_operator_c  *symbol)  {return handle_il_instruction(symbol);}
       
   807 void *narrow_candidate_datatypes_c::visit(ANDN_operator_c *symbol)  {return handle_il_instruction(symbol);}
   778 void *narrow_candidate_datatypes_c::visit(ANDN_operator_c *symbol)  {return handle_il_instruction(symbol);}
   808 void *narrow_candidate_datatypes_c::visit(ORN_operator_c  *symbol)  {return handle_il_instruction(symbol);}
   779 void *narrow_candidate_datatypes_c::visit( ORN_operator_c *symbol)  {return handle_il_instruction(symbol);}
   809 void *narrow_candidate_datatypes_c::visit(XORN_operator_c *symbol)  {return handle_il_instruction(symbol);}
   780 void *narrow_candidate_datatypes_c::visit(XORN_operator_c *symbol)  {return handle_il_instruction(symbol);}
   810 void *narrow_candidate_datatypes_c::visit(ADD_operator_c  *symbol)  {return handle_il_instruction(symbol);}
   781 void *narrow_candidate_datatypes_c::visit( ADD_operator_c *symbol)  {return handle_il_instruction(symbol);}
   811 void *narrow_candidate_datatypes_c::visit(SUB_operator_c  *symbol)  {return handle_il_instruction(symbol);}
   782 void *narrow_candidate_datatypes_c::visit( SUB_operator_c *symbol)  {return handle_il_instruction(symbol);}
   812 void *narrow_candidate_datatypes_c::visit(MUL_operator_c  *symbol)  {return handle_il_instruction(symbol);}
   783 void *narrow_candidate_datatypes_c::visit( MUL_operator_c *symbol)  {return handle_il_instruction(symbol);}
   813 void *narrow_candidate_datatypes_c::visit(DIV_operator_c  *symbol)  {return handle_il_instruction(symbol);}
   784 void *narrow_candidate_datatypes_c::visit( DIV_operator_c *symbol)  {return handle_il_instruction(symbol);}
   814 void *narrow_candidate_datatypes_c::visit(MOD_operator_c  *symbol)  {return handle_il_instruction(symbol);}
   785 void *narrow_candidate_datatypes_c::visit( MOD_operator_c *symbol)  {return handle_il_instruction(symbol);}
   815 void *narrow_candidate_datatypes_c::visit(GT_operator_c   *symbol)  {return handle_il_instruction(symbol);}
   786 void *narrow_candidate_datatypes_c::visit(  GT_operator_c *symbol)  {return handle_il_instruction(symbol);}
   816 void *narrow_candidate_datatypes_c::visit(GE_operator_c   *symbol)  {return handle_il_instruction(symbol);}
   787 void *narrow_candidate_datatypes_c::visit(  GE_operator_c *symbol)  {return handle_il_instruction(symbol);}
   817 void *narrow_candidate_datatypes_c::visit(EQ_operator_c   *symbol)  {return handle_il_instruction(symbol);}
   788 void *narrow_candidate_datatypes_c::visit(  EQ_operator_c *symbol)  {return handle_il_instruction(symbol);}
   818 void *narrow_candidate_datatypes_c::visit(LT_operator_c   *symbol)  {return handle_il_instruction(symbol);}
   789 void *narrow_candidate_datatypes_c::visit(  LT_operator_c *symbol)  {return handle_il_instruction(symbol);}
   819 void *narrow_candidate_datatypes_c::visit(LE_operator_c   *symbol)  {return handle_il_instruction(symbol);}
   790 void *narrow_candidate_datatypes_c::visit(  LE_operator_c *symbol)  {return handle_il_instruction(symbol);}
   820 void *narrow_candidate_datatypes_c::visit(NE_operator_c   *symbol)  {return handle_il_instruction(symbol);}
   791 void *narrow_candidate_datatypes_c::visit(  NE_operator_c *symbol)  {return handle_il_instruction(symbol);}
   821 
   792 
   822 
   793 
   823 // SYM_REF0(CAL_operator_c)
   794 // SYM_REF0(CAL_operator_c)
   824 /* called from il_fb_call_c (symbol->il_call_operator->accpet(*this) ) */
   795 /* called from il_fb_call_c (symbol->il_call_operator->accpet(*this) ) */
   825 void *narrow_candidate_datatypes_c::visit(CAL_operator_c *symbol) {
   796 void *narrow_candidate_datatypes_c::visit(CAL_operator_c *symbol) {
   836 	 * then we have an internal compiler error - most likely in fill_candidate_datatypes_c 
   807 	 * then we have an internal compiler error - most likely in fill_candidate_datatypes_c 
   837 	 */
   808 	 */
   838 	if ((NULL != symbol->datatype) && (!is_type(symbol->datatype, bool_type_name_c))) ERROR;
   809 	if ((NULL != symbol->datatype) && (!is_type(symbol->datatype, bool_type_name_c))) ERROR;
   839 	if (symbol->candidate_datatypes.size() > 1) ERROR;
   810 	if (symbol->candidate_datatypes.size() > 1) ERROR;
   840 
   811 
   841 	/* NOTE: If there is not IL instruction following this CALC, CALCN, JMPC, JMPC, ..., instruction,
   812 	/* NOTE: If there is no IL instruction following this CALC, CALCN, JMPC, JMPC, ..., instruction,
   842 	 *       we must still provide a bool_type_name_c datatype (if possible, i.e. if it exists in the candidate datatype list).
   813 	 *       we must still provide a bool_type_name_c datatype (if possible, i.e. if it exists in the candidate datatype list).
   843 	 *       If it is not possible, we set it to NULL
   814 	 *       If it is not possible, we set it to NULL
   844 	 */
   815 	 */
   845 	if (symbol->candidate_datatypes.size() == 0)    symbol->datatype = NULL;
   816 	if (symbol->candidate_datatypes.size() == 0)    symbol->datatype = NULL;
   846 	else    symbol->datatype = symbol->candidate_datatypes[0]; /* i.e. a bool_type_name_c! */
   817 	else    symbol->datatype = symbol->candidate_datatypes[0]; /* i.e. a bool_type_name_c! */
   851 	return NULL;
   822 	return NULL;
   852 }
   823 }
   853 
   824 
   854 
   825 
   855 // SYM_REF0(CALC_operator_c)
   826 // SYM_REF0(CALC_operator_c)
   856 /* called from il_fb_call_c (symbol->il_call_operator->accpet(*this) ) */
       
   857 void *narrow_candidate_datatypes_c::visit(CALC_operator_c *symbol) {
       
   858 	return narrow_conditional_flow_control_IL_instruction(symbol);
       
   859 }
       
   860 
       
   861 
       
   862 // SYM_REF0(CALCN_operator_c)
   827 // SYM_REF0(CALCN_operator_c)
   863 /* called from il_fb_call_c (symbol->il_call_operator->accpet(*this) ) */
   828 /* called from visit(il_fb_call_c *) {symbol->il_call_operator->accpet(*this)} */
   864 void *narrow_candidate_datatypes_c::visit(CALCN_operator_c *symbol) {
   829 void *narrow_candidate_datatypes_c::visit( CALC_operator_c *symbol) {return narrow_conditional_flow_control_IL_instruction(symbol);}
   865 	return narrow_conditional_flow_control_IL_instruction(symbol);
   830 void *narrow_candidate_datatypes_c::visit(CALCN_operator_c *symbol) {return narrow_conditional_flow_control_IL_instruction(symbol);}
   866 }
       
   867 
   831 
   868 
   832 
   869 void *narrow_candidate_datatypes_c::visit(RET_operator_c *symbol) {
   833 void *narrow_candidate_datatypes_c::visit(RET_operator_c *symbol) {
   870 	/* set the desired datatype of the previous il instruction */
   834 	/* set the desired datatype of the previous il instruction */
   871 	/* This RET instruction does not change the value in the current/default IL variable, so we pass the required datatype to the previous IL instruction.
   835 	/* This RET instruction does not change the value in the current/default IL variable, so we pass the required datatype to the previous IL instruction.
   876 	if (NULL != prev_il_instruction)
   840 	if (NULL != prev_il_instruction)
   877 		prev_il_instruction->datatype = symbol->datatype;
   841 		prev_il_instruction->datatype = symbol->datatype;
   878 	return NULL;
   842 	return NULL;
   879 }
   843 }
   880 
   844 
   881 void *narrow_candidate_datatypes_c::visit(RETC_operator_c *symbol) {
   845 void *narrow_candidate_datatypes_c::visit( RETC_operator_c *symbol) {return narrow_conditional_flow_control_IL_instruction(symbol);}
   882 	return narrow_conditional_flow_control_IL_instruction(symbol);
   846 void *narrow_candidate_datatypes_c::visit(RETCN_operator_c *symbol) {return narrow_conditional_flow_control_IL_instruction(symbol);}
   883 }
       
   884 
       
   885 void *narrow_candidate_datatypes_c::visit(RETCN_operator_c *symbol) {
       
   886 	return narrow_conditional_flow_control_IL_instruction(symbol);
       
   887 }
       
   888 
   847 
   889 void *narrow_candidate_datatypes_c::visit(JMP_operator_c *symbol) {
   848 void *narrow_candidate_datatypes_c::visit(JMP_operator_c *symbol) {
   890 	/* set the desired datatype of the previous il instruction */
   849 	/* set the desired datatype of the previous il instruction */
   891 	/* This JMP instruction does not change the value in the current/default IL variable, so we pass the required datatype to the previous IL instruction.
   850 	/* This JMP instruction does not change the value in the current/default IL variable, so we pass the required datatype to the previous IL instruction.
   892 	 * Actually this should always be NULL, otherwise we have a bug in the flow_control_analysis_c
   851 	 * Actually this should always be NULL, otherwise we have a bug in the flow_control_analysis_c
   896 	if (NULL != prev_il_instruction)
   855 	if (NULL != prev_il_instruction)
   897 		prev_il_instruction->datatype = symbol->datatype;
   856 		prev_il_instruction->datatype = symbol->datatype;
   898 	return NULL;
   857 	return NULL;
   899 }
   858 }
   900 
   859 
   901 void *narrow_candidate_datatypes_c::visit(JMPC_operator_c *symbol) {
   860 void *narrow_candidate_datatypes_c::visit( JMPC_operator_c *symbol) {return narrow_conditional_flow_control_IL_instruction(symbol);}
   902 	return narrow_conditional_flow_control_IL_instruction(symbol);
   861 void *narrow_candidate_datatypes_c::visit(JMPCN_operator_c *symbol) {return narrow_conditional_flow_control_IL_instruction(symbol);}
   903 }
       
   904 
       
   905 void *narrow_candidate_datatypes_c::visit(JMPCN_operator_c *symbol) {
       
   906 	return narrow_conditional_flow_control_IL_instruction(symbol);
       
   907 }
       
   908 
   862 
   909 /* Symbol class handled together with function call checks */
   863 /* Symbol class handled together with function call checks */
   910 // void *visit(il_assign_operator_c *symbol, variable_name);
   864 // void *visit(il_assign_operator_c *symbol, variable_name);
   911 /* Symbol class handled together with function call checks */
   865 /* Symbol class handled together with function call checks */
   912 // void *visit(il_assign_operator_c *symbol, option, variable_name);
   866 // void *visit(il_assign_operator_c *symbol, option, variable_name);