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