stage3/narrow_candidate_datatypes.cc
changeset 466 d97a29b7fa8b
parent 461 fcbc0927fc96
child 467 4910eaa1206a
equal deleted inserted replaced
465:b52ec62773db 466:d97a29b7fa8b
    58 /* Only set the symbol's desired datatype to 'datatype' if that datatype is in the candidate_datatype list */
    58 /* Only set the symbol's desired datatype to 'datatype' if that datatype is in the candidate_datatype list */
    59 static void set_datatype(symbol_c *datatype, symbol_c *symbol) {
    59 static void set_datatype(symbol_c *datatype, symbol_c *symbol) {
    60   
    60   
    61 	/* If we are trying to set to the undefined type, and the symbol's datatype has already been set to something else, 
    61 	/* If we are trying to set to the undefined type, and the symbol's datatype has already been set to something else, 
    62 	 * we abort the compoiler as I don't think this should ever occur. 
    62 	 * we abort the compoiler as I don't think this should ever occur. 
    63 	 * However, I (Mario) am not too sure of this, so if the compiler ever aborts here, please analyse the situation
    63 	 * NOTE: In order to handle JMPs to labels that come before the JMP itself, we run the narrow algorithm twice.
    64 	 * carefully as it might be perfectly legal and something I have missed!
    64 	 *       This means that this situation may legally occur, so we cannot abort the compiler here!
    65 	 */
    65 	 */
    66 	if ((NULL == datatype) && (NULL != symbol->datatype)) ERROR;
    66 // 	if ((NULL == datatype) && (NULL != symbol->datatype)) ERROR;
    67 	
    67  	if ((NULL == datatype) && (NULL != symbol->datatype)) return;
    68 	if ((NULL == datatype) && (NULL == symbol->datatype)) return;
    68 	if ((NULL == datatype) && (NULL == symbol->datatype)) return;
    69 	
    69 	
    70 	if (search_in_candidate_datatype_list(datatype, symbol->candidate_datatypes) < 0)
    70 	if (search_in_candidate_datatype_list(datatype, symbol->candidate_datatypes) < 0)
    71 		symbol->datatype = &(search_constant_type_c::invalid_type_name);   
    71 		symbol->datatype = &(search_constant_type_c::invalid_type_name);   
    72 	else {
    72 	else {
   519 // SYM_LIST(instruction_list_c)
   519 // SYM_LIST(instruction_list_c)
   520 void *narrow_candidate_datatypes_c::visit(instruction_list_c *symbol) {
   520 void *narrow_candidate_datatypes_c::visit(instruction_list_c *symbol) {
   521 	/* In order to execute the narrow algoritm correctly, we need to go through the instructions backwards,
   521 	/* In order to execute the narrow algoritm correctly, we need to go through the instructions backwards,
   522 	 * so we can not use the base class' visitor 
   522 	 * so we can not use the base class' visitor 
   523 	 */
   523 	 */
   524 	for(int i = symbol->n-1; i >= 0; i--) {
   524 	/* In order to execute the narrow algoritm correctly
   525 		symbol->elements[i]->accept(*this);
   525 	 * in IL instruction lists containing JMPs to labels that come before the JMP instruction
       
   526 	 * itself, we need to run the narrow algorithm twice on the Instruction List.
       
   527 	 * e.g.:  ...
       
   528 	 *          ld 23
       
   529 	 *   label1:st byte_var
       
   530 	 *          ld 34
       
   531 	 *          JMP label1     
       
   532 	 *
       
   533 	 * Note that the second time we run the narrow, most of the datatypes are already filled
       
   534 	 * in, so it will be able to produce tha correct datatypes for the IL instruction referenced
       
   535 	 * by the label, as in the 2nd pass we already know the datatypes of the JMP instruction!
       
   536 	 */
       
   537 	for(int j = 0; j < 2; j++) {
       
   538 		for(int i = symbol->n-1; i >= 0; i--) {
       
   539 			symbol->elements[i]->accept(*this);
       
   540 		}
   526 	}
   541 	}
   527 	return NULL;
   542 	return NULL;
   528 }
   543 }
   529 
   544 
   530 /* | label ':' [il_incomplete_instruction] eol_list */
   545 /* | label ':' [il_incomplete_instruction] eol_list */
   628   fake_prev_il_instruction = save_fake_prev_il_instruction;
   643   fake_prev_il_instruction = save_fake_prev_il_instruction;
   629   return NULL;
   644   return NULL;
   630 }
   645 }
   631 
   646 
   632 
   647 
       
   648 
       
   649 
       
   650 /*  il_jump_operator label */
       
   651 void *narrow_candidate_datatypes_c::visit(il_jump_operation_c *symbol) {
       
   652   /* recursive call to fill the datatype */
       
   653   symbol->il_jump_operator->datatype = symbol->datatype;
       
   654   symbol->il_jump_operator->accept(*this);
       
   655   return NULL;
       
   656 }
       
   657 
       
   658 
       
   659 
       
   660 
       
   661 
       
   662 
       
   663 
   633 /*   il_call_operator prev_declared_fb_name
   664 /*   il_call_operator prev_declared_fb_name
   634  * | il_call_operator prev_declared_fb_name '(' ')'
   665  * | il_call_operator prev_declared_fb_name '(' ')'
   635  * | il_call_operator prev_declared_fb_name '(' eol_list ')'
   666  * | il_call_operator prev_declared_fb_name '(' eol_list ')'
   636  * | il_call_operator prev_declared_fb_name '(' il_operand_list ')'
   667  * | il_call_operator prev_declared_fb_name '(' il_operand_list ')'
   637  * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')'
   668  * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')'