stage3/narrow_candidate_datatypes.cc
changeset 454 099aa5d655de
parent 453 4733f662362a
child 455 933c0dccc82f
equal deleted inserted replaced
453:4733f662362a 454:099aa5d655de
   570 /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */
   570 /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */
   571 // SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list);
   571 // SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list);
   572 void *narrow_candidate_datatypes_c::visit(il_expression_c *symbol) {
   572 void *narrow_candidate_datatypes_c::visit(il_expression_c *symbol) {
   573   /* first handle the operation (il_expr_operator) that will use the result coming from the parenthesised IL list (i.e. simple_instr_list) */
   573   /* first handle the operation (il_expr_operator) that will use the result coming from the parenthesised IL list (i.e. simple_instr_list) */
   574   symbol->il_expr_operator->datatype = symbol->datatype;
   574   symbol->il_expr_operator->datatype = symbol->datatype;
   575   il_operand = symbol->il_operand;
   575   il_operand = symbol->simple_instr_list; /* This is not a bug! The parenthesised expression will be used as the operator! */
   576   symbol->il_expr_operator->accept(*this);
   576   symbol->il_expr_operator->accept(*this);
   577 
   577 
   578   /* now give the parenthesised IL list a chance to narrow the datatypes */
   578   /* now give the parenthesised IL list a chance to narrow the datatypes */
       
   579   /* The datatype that is must return was set by the call symbol->il_expr_operator->accept(*this) */
   579   symbol_c *save_prev_il_instruction = prev_il_instruction; /*this is not really necessary, but lets play it safe */
   580   symbol_c *save_prev_il_instruction = prev_il_instruction; /*this is not really necessary, but lets play it safe */
   580   symbol->simple_instr_list->accept(*this);
   581   symbol->simple_instr_list->accept(*this);
   581   prev_il_instruction = save_prev_il_instruction;
   582   prev_il_instruction = save_prev_il_instruction;
   582 return NULL;
   583 return NULL;
   583 }
   584 }
   677 
   678 
   678 void *narrow_candidate_datatypes_c::handle_il_instruction(symbol_c *symbol) {
   679 void *narrow_candidate_datatypes_c::handle_il_instruction(symbol_c *symbol) {
   679 	if (NULL == symbol->datatype)
   680 	if (NULL == symbol->datatype)
   680 		/* next IL instructions were unable to determine the datatype this instruction should produce */
   681 		/* next IL instructions were unable to determine the datatype this instruction should produce */
   681 		return NULL;
   682 		return NULL;
       
   683 	/* NOTE 1: the il_operand __may__ be pointing to a parenthesized list of IL instructions. 
       
   684 	 * e.g.  LD 33
       
   685 	 *       AND ( 45
       
   686 	 *            OR 56
       
   687 	 *            )
       
   688 	 *       When we handle the first 'AND' IL_operator, the il_operand will point to an simple_instr_list_c.
       
   689 	 *       In this case, when we call il_operand->accept(*this);, the prev_il_instruction pointer will be overwritten!
       
   690 	 *
       
   691 	 *       We must therefore set the prev_il_instruction->datatype = symbol->datatype;
       
   692 	 *       __before__ calling il_operand->accept(*this) !!
       
   693 	 *
       
   694 	 * NOTE 2: We do not need to call prev_il_instruction->accept(*this), as the object to which prev_il_instruction
       
   695 	 *         is pointing to will be later narrowed by the call from the for() loop of the instruction_list_c
       
   696 	 *         (or simple_instr_list_c), which iterates backwards.
       
   697 	 */
       
   698 	/* set the desired datatype of the previous il instruction */
       
   699 	prev_il_instruction->datatype = symbol->datatype;
   682 	/* set the datatype for the operand */
   700 	/* set the datatype for the operand */
   683 	il_operand->datatype = symbol->datatype;
   701 	il_operand->datatype = symbol->datatype;
   684 	il_operand->accept(*this);
   702 	il_operand->accept(*this);
   685 	/* set the desired datatype of the previous il instruction */
       
   686 	prev_il_instruction->datatype = symbol->datatype;
       
   687 	return NULL;
   703 	return NULL;
   688 }
   704 }
   689 
   705 
   690 void *narrow_candidate_datatypes_c::visit(LD_operator_c *symbol)   {
   706 void *narrow_candidate_datatypes_c::visit(LD_operator_c *symbol)   {
   691 	if (NULL == symbol->datatype)
   707 	if (NULL == symbol->datatype)
   733 	prev_il_instruction->datatype = symbol->datatype;
   749 	prev_il_instruction->datatype = symbol->datatype;
   734 	return NULL;
   750 	return NULL;
   735 }
   751 }
   736 
   752 
   737 void *narrow_candidate_datatypes_c::visit(NOT_operator_c *symbol) {
   753 void *narrow_candidate_datatypes_c::visit(NOT_operator_c *symbol) {
   738 	prev_il_instruction = symbol;
       
   739 	return NULL;
   754 	return NULL;
   740 }
   755 }
   741 
   756 
   742 void *narrow_candidate_datatypes_c::visit(S_operator_c *symbol)  {
   757 void *narrow_candidate_datatypes_c::visit(S_operator_c *symbol)  {
   743   /* TODO: what if this is a FB call? */
   758   /* TODO: what if this is a FB call? */