stage3/forced_narrow_candidate_datatypes.cc
changeset 677 740da3255d9d
child 679 2f5618c0039a
equal deleted inserted replaced
676:ca4f17211251 677:740da3255d9d
       
     1 /*
       
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
       
     3  *
       
     4  *  Copyright (C) 2012  Mario de Sousa (msousa@fe.up.pt)
       
     5  *
       
     6  *  This program is free software: you can redistribute it and/or modify
       
     7  *  it under the terms of the GNU General Public License as published by
       
     8  *  the Free Software Foundation, either version 3 of the License, or
       
     9  *  (at your option) any later version.
       
    10  *
       
    11  *  This program is distributed in the hope that it will be useful,
       
    12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  *  GNU General Public License for more details.
       
    15  *
       
    16  *  You should have received a copy of the GNU General Public License
       
    17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    18  *
       
    19  *
       
    20  * This code is made available on the understanding that it will not be
       
    21  * used in safety-critical situations without a full and competent review.
       
    22  */
       
    23 
       
    24 /*
       
    25  * An IEC 61131-3 compiler.
       
    26  *
       
    27  * Based on the
       
    28  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    29  *
       
    30  */
       
    31 
       
    32 
       
    33 /*
       
    34  *  Data type analysis of IL code may leave some IL instructions with an undefined datatype.
       
    35  *  This visitor will set the datatype for all these symbols, so that all symbols have a well 
       
    36  *  defined datatype when we reach stage4.
       
    37  *
       
    38  *  Example:
       
    39  * =========
       
    40  *
       
    41  *  VAR 
       
    42  *     N   : INT := 99 ;    
       
    43  *     tonv: TON;
       
    44  *     byte_var: BYTE;
       
    45  *     tonv : TON;
       
    46  *     a : BYTE;
       
    47  *     t : time;
       
    48  *     tod1: tod;
       
    49  *  END_VAR
       
    50  *  
       
    51  * (0) --> Data type before executing forced_narrow_candidate_datatypes_c
       
    52  * (1) --> Data type after executing 1st pass of forced_narrow_candidate_datatypes_c
       
    53  * (2) --> Data type after executing 2nd pass of forced_narrow_candidate_datatypes_c
       
    54  * 
       
    55  * --- --> NULL (undefined datatype)
       
    56  * *** --> invalid_type_name_c (invalid datatype)
       
    57  *
       
    58  * (0)       (1)   (2)
       
    59  *      
       
    60  * ---       ***   ***       CAL tonv (                          
       
    61  *                                  PT := T#1s                   
       
    62  *                               )                               
       
    63  * ---       ***   ***       JMP l4                              
       
    64  *
       
    65  * ---       sint  sint  l0: LD  1                               
       
    66  * ---       sint  sint      ADD 2                               
       
    67  * --- (c)   sint  sint      CAL tonv (                          
       
    68  *                               PT := T#1s                      
       
    69  *                               )                               
       
    70  *                                                               
       
    71  * ---       sint  sint      LD  45                              
       
    72  * --- (c)   sint  sint      ADD 45                              
       
    73  *
       
    74  *
       
    75  * ---       sint  sint      LD  3                               
       
    76  * ---       sint  sint  l1:                                     
       
    77  * --- (c)   sint  sint  l2: ADD 4                               
       
    78  * int       int   int       LD  5                               
       
    79  * int       int   int       ST  n                               
       
    80  * int       int   int       JMP l3                              
       
    81  *                                                               
       
    82  * --- (d)   ---   sint      LD  5                               
       
    83  * --- (d)   ---   sint      SUB 6                               
       
    84  * --- (d)   sint  sint      JMP l1                              
       
    85  *
       
    86  * ---       bool  bool      LD  FALSE                           
       
    87  * ---       bool  bool      NOT                                 
       
    88  * --- (b)   bool  bool      RET                                 
       
    89  *
       
    90  * int       int   int   l3:                                     
       
    91  * int       int   int       ST  n                               
       
    92  * --- (b)   int   int       RET                                 
       
    93  *
       
    94  * ---       ***   ***   l4:                                     
       
    95  * ---       ***   ***       CAL tonv (                          
       
    96  *                                  PT := T#1s                   
       
    97  *                               )                               
       
    98  * --- (a)   ***   ***       JMP l0                              
       
    99  * --- (b)   byte  byte      LD  88                              
       
   100  *
       
   101  *
       
   102  *   
       
   103  */
       
   104 
       
   105 
       
   106 
       
   107 #include "forced_narrow_candidate_datatypes.hh"
       
   108 #include "datatype_functions.hh"
       
   109 
       
   110 
       
   111 /* set to 1 to see debug info during execution */
       
   112 static int debug = 0;
       
   113 
       
   114 forced_narrow_candidate_datatypes_c::forced_narrow_candidate_datatypes_c(symbol_c *ignore) 
       
   115  :narrow_candidate_datatypes_c(ignore) {
       
   116 }
       
   117 
       
   118 forced_narrow_candidate_datatypes_c::~forced_narrow_candidate_datatypes_c(void) {
       
   119 }
       
   120 
       
   121 
       
   122 
       
   123 
       
   124 
       
   125 /****************************************/
       
   126 /* B.2 - Language IL (Instruction List) */
       
   127 /****************************************/
       
   128 /***********************************/
       
   129 /* B 2.1 Instructions and Operands */
       
   130 /***********************************/
       
   131 
       
   132 /*| instruction_list il_instruction */
       
   133 // SYM_LIST(instruction_list_c)
       
   134 void *forced_narrow_candidate_datatypes_c::visit(instruction_list_c *symbol) {
       
   135 print_ast_c::print(symbol);
       
   136 
       
   137   for(int j = 0; j < 2; j++) {
       
   138     for(int i = symbol->n-1; i >= 0; i--) {
       
   139       symbol->elements[i]->accept(*this);
       
   140     }
       
   141   }
       
   142 
       
   143   /* Assert that this algorithm managed to remove all NULL datatypes! */
       
   144   for(int i = symbol->n-1; i >= 0; i--) {
       
   145     if (NULL == symbol->elements[i]->datatype)
       
   146       ERROR;
       
   147   }
       
   148 
       
   149   return NULL;
       
   150 }
       
   151 
       
   152 
       
   153   
       
   154 /* | label ':' [il_incomplete_instruction] eol_list */
       
   155 // SYM_REF2(il_instruction_c, label, il_instruction)
       
   156 // void *visit(instruction_list_c *symbol);
       
   157 void *forced_narrow_candidate_datatypes_c::visit(il_instruction_c *symbol) {
       
   158   if (NULL == symbol->datatype) {
       
   159     if (symbol->candidate_datatypes.empty()) {
       
   160       symbol->datatype = &(search_constant_type_c::invalid_type_name); // This will occur in the situations (a) in the above example
       
   161       // return NULL; // No need to return control to the visit() method of the base class... But we do so, just to be safe (called at the end of this function)!
       
   162     } else {
       
   163       if (symbol->next_il_instruction.empty()) {
       
   164         symbol->datatype = symbol->candidate_datatypes[0]; // This will occur in the situations (b) in the above example
       
   165       } else {
       
   166         symbol_c *next_datatype = NULL;
       
   167 
       
   168         /* find the datatype of the following IL instructions (they should all be identical by now, but we don't have an assertion checking for this. */
       
   169         for (unsigned int i=0; i < symbol->next_il_instruction.size(); i++)
       
   170           if (NULL != symbol->next_il_instruction[i]->datatype)
       
   171             next_datatype = symbol->next_il_instruction[i]->datatype;
       
   172         if (get_datatype_info_c::is_type_valid(next_datatype)) {
       
   173           /* This will occur in the following situations from the above example	   
       
   174            *    (d)   during the second pass of this algorithm (remember, we execute this algorithm twice, because of backward JMPs!)
       
   175            */
       
   176           symbol->datatype = symbol->candidate_datatypes[0]; 
       
   177         } else {
       
   178           /* This will occur in the following situations from the above example	   
       
   179            *    (d)   during the first pass of this algorithm (remember, we execute this algorithm twice, because of backward JMPs!)
       
   180            */
       
   181           // it is not possible to determine the exact situation in the current pass, so we can't do anything just yet. Leave it for the next time around!
       
   182         }
       
   183       }
       
   184     }
       
   185   }
       
   186   
       
   187   /* return control to the visit() method of the base class! */
       
   188   narrow_candidate_datatypes_c::visit(symbol);
       
   189   
       
   190   return NULL;
       
   191 }
       
   192 
       
   193 
       
   194 
       
   195 
       
   196 
       
   197 /* | il_simple_operator [il_operand] */
       
   198 // SYM_REF2(il_simple_operation_c, il_simple_operator, il_operand)
       
   199 // void *forced_narrow_candidate_datatypes_c::visit(il_simple_operation_c *symbol)
       
   200 
       
   201 /* | function_name [il_operand_list] */
       
   202 /* NOTE: The parameters 'called_function_declaration' and 'extensible_param_count' are used to pass data between the stage 3 and stage 4. */
       
   203 // SYM_REF2(il_function_call_c, function_name, il_operand_list, symbol_c *called_function_declaration; int extensible_param_count;)
       
   204 // void *forced_narrow_candidate_datatypes_c::visit(il_function_call_c *symbol) 
       
   205 
       
   206 /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */
       
   207 // SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list);
       
   208 // void *forced_narrow_candidate_datatypes_c::visit(il_expression_c *symbol)
       
   209 
       
   210 /*  il_jump_operator label */
       
   211 // SYM_REF2(il_jump_operation_c, il_jump_operator, label)
       
   212 // void *forced_narrow_candidate_datatypes_c::visit(il_jump_operation_c *symbol)
       
   213 
       
   214 /*   il_call_operator prev_declared_fb_name
       
   215  * | il_call_operator prev_declared_fb_name '(' ')'
       
   216  * | il_call_operator prev_declared_fb_name '(' eol_list ')'
       
   217  * | il_call_operator prev_declared_fb_name '(' il_operand_list ')'
       
   218  * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')'
       
   219  */
       
   220 /* NOTE: The parameter 'called_fb_declaration'is used to pass data between stage 3 and stage4 (although currently it is not used in stage 4 */
       
   221 // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list, symbol_c *called_fb_declaration)
       
   222 // void *forced_narrow_candidate_datatypes_c::visit(il_fb_call_c *symbol)
       
   223 
       
   224 /* | function_name '(' eol_list [il_param_list] ')' */
       
   225 /* NOTE: The parameter 'called_function_declaration' is used to pass data between the stage 3 and stage 4. */
       
   226 // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list, symbol_c *called_function_declaration; int extensible_param_count;)
       
   227 // void *forced_narrow_candidate_datatypes_c::visit(il_formal_funct_call_c *symbol)
       
   228 
       
   229 // void *visit(il_operand_list_c *symbol);
       
   230 // void *forced_narrow_candidate_datatypes_c::visit(simple_instr_list_c *symbol)
       
   231 
       
   232 // SYM_REF1(il_simple_instruction_c, il_simple_instruction, symbol_c *prev_il_instruction;)
       
   233 // void *forced_narrow_candidate_datatypes_c::visit(il_simple_instruction_c*symbol)
       
   234 
       
   235 /*
       
   236     void *visit(il_param_list_c *symbol);
       
   237     void *visit(il_param_assignment_c *symbol);
       
   238     void *visit(il_param_out_assignment_c *symbol);
       
   239  */
       
   240