stage3/array_range_check.cc
changeset 620 aef32856eeb5
parent 614 31bda4cde875
child 621 e3616f6b6959
equal deleted inserted replaced
619:f8c9ac5c529a 620:aef32856eeb5
    69 }
    69 }
    70 
    70 
    71 #define GET_CVALUE(dtype, symbol)             ((symbol)->const_value._##dtype.value)
    71 #define GET_CVALUE(dtype, symbol)             ((symbol)->const_value._##dtype.value)
    72 #define VALID_CVALUE(dtype, symbol)           (symbol_c::cs_const_value == (symbol)->const_value._##dtype.status)
    72 #define VALID_CVALUE(dtype, symbol)           (symbol_c::cs_const_value == (symbol)->const_value._##dtype.status)
    73 
    73 
    74 
    74 /*  The cmp_unsigned_signed function compares two numbers u and s.
       
    75  *  It returns an integer indicating the relationship between the numbers:
       
    76  *  - A zero value indicates that both numbers are equal.
       
    77  *  - A value greater than zero indicates that numbers does not match and
       
    78  *    first has a greater value.
       
    79  *  - A value less than zero indicates that numbers does not match and
       
    80  *    first has a lesser value.
       
    81  */
       
    82 static inline int cmp_unsigned_signed(const uint64_t u, const int64_t s) {
       
    83   if (u <= INT64_MAX)
       
    84     return ((int64_t)u - s);
       
    85   return -1;
       
    86 }
    75 
    87 
    76 array_range_check_c::array_range_check_c(symbol_c *ignore) {
    88 array_range_check_c::array_range_check_c(symbol_c *ignore) {
    77 	error_count = 0;
    89 	error_count = 0;
    78 	current_display_error_level = 0;
    90 	current_display_error_level = 0;
    79 }
    91 }
   117     if (NULL == dimension) 
   129     if (NULL == dimension) 
   118       return;
   130       return;
   119 
   131 
   120     /* Check lower limit */
   132     /* Check lower limit */
   121     if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE( int64, dimension->lower_limit))
   133     if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE( int64, dimension->lower_limit))
   122       if ( GET_CVALUE( int64, l->elements[i])   <  GET_CVALUE( int64, dimension->lower_limit))
   134       if ( GET_CVALUE( int64, l->elements[i]) < GET_CVALUE( int64, dimension->lower_limit) )
   123       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   135       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   124 
   136 
   125     if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE(uint64, dimension->lower_limit))
   137     if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE(uint64, dimension->lower_limit))
   126       if ( GET_CVALUE( int64, l->elements[i])   <  GET_CVALUE(uint64, dimension->lower_limit))
   138       if ( cmp_unsigned_signed( GET_CVALUE(uint64, dimension->lower_limit), GET_CVALUE( int64, l->elements[i])) > 0 )
   127       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   139       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   128 
   140 
   129     if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE(uint64, dimension->lower_limit))
   141     if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE(uint64, dimension->lower_limit))
   130       if ( GET_CVALUE(uint64, l->elements[i])   <  GET_CVALUE(uint64, dimension->lower_limit))
   142       if ( GET_CVALUE(uint64, l->elements[i])   <  GET_CVALUE(uint64, dimension->lower_limit))
   131       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   143       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   132 
   144 
   133     if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE( int64, dimension->lower_limit))
   145     if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE( int64, dimension->lower_limit))
   134       if ( GET_CVALUE(uint64, l->elements[i])   <  GET_CVALUE( int64, dimension->lower_limit))
   146       if ( cmp_unsigned_signed(GET_CVALUE(uint64, l->elements[i]), GET_CVALUE( int64, dimension->lower_limit)) < 0 )
   135       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   147       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   136 
   148 
   137     /* Repeat the same check, now for upper limit */
   149     /* Repeat the same check, now for upper limit */
   138     if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE( int64, dimension->upper_limit))
   150     if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE( int64, dimension->upper_limit))
   139       if ( GET_CVALUE( int64, l->elements[i])   >  GET_CVALUE( int64, dimension->upper_limit))
   151       if ( GET_CVALUE( int64, l->elements[i])   >  GET_CVALUE( int64, dimension->upper_limit))
   140       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   152       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   141 
   153 
   142     if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE(uint64, dimension->upper_limit))
   154     if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE(uint64, dimension->upper_limit))
   143       if ( GET_CVALUE( int64, l->elements[i])   >  GET_CVALUE(uint64, dimension->upper_limit))
   155       if ( cmp_unsigned_signed( GET_CVALUE(uint64, dimension->upper_limit), GET_CVALUE( int64, l->elements[i])) < 0 )
   144       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   156       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   145 
   157 
   146     if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE(uint64, dimension->upper_limit))
   158     if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE(uint64, dimension->upper_limit))
   147       if ( GET_CVALUE(uint64, l->elements[i])   >  GET_CVALUE(uint64, dimension->upper_limit))
   159       if ( GET_CVALUE(uint64, l->elements[i])   >  GET_CVALUE(uint64, dimension->upper_limit))
   148       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   160       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   149       
   161       
   150     if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE( int64, dimension->upper_limit))
   162     if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE( int64, dimension->upper_limit))
   151       if ( GET_CVALUE(uint64, l->elements[i])   >  GET_CVALUE( int64, dimension->upper_limit))
   163       if ( cmp_unsigned_signed(GET_CVALUE(uint64, l->elements[i]), GET_CVALUE( int64, dimension->upper_limit)) < 0 )
   152       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   164       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
   153       
   165       
   154    /* TODO: How do we make these comparisons between int and unsigned int, without the compiler complaining? */
       
   155   }
   166   }
   156 }
   167 }
   157 
   168 
   158 
   169 
   159 
   170