4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 #include "smatch_slist.h"
12 #include "smatch_extra.h"
14 static long long _get_implied_value(struct expression
*expr
, int *undefined
, int implied
);
15 static long long _get_value(struct expression
*expr
, int *undefined
, int implied
);
26 static long long cast_to_type(struct expression
*expr
, long long val
)
28 struct symbol
*type
= get_type(expr
);
33 switch (type
->bit_size
) {
35 if (type
->ctype
.modifiers
& MOD_UNSIGNED
)
36 val
= (long long)(unsigned char) val
;
38 val
= (long long)(char) val
;
41 if (type
->ctype
.modifiers
& MOD_UNSIGNED
)
42 val
= (long long)(unsigned short) val
;
44 val
= (long long)(short) val
;
47 if (type
->ctype
.modifiers
& MOD_UNSIGNED
)
48 val
= (long long)(unsigned int) val
;
50 val
= (long long)(int) val
;
56 static int opposite_implied(int implied
)
58 if (implied
== IMPLIED_MIN
)
60 if (implied
== IMPLIED_MAX
)
65 static long long handle_preop(struct expression
*expr
, int *undefined
, int implied
)
67 long long ret
= BOGUS
;
71 ret
= ~_get_value(expr
->unop
, undefined
, implied
);
72 ret
= cast_to_type(expr
->unop
, ret
);
75 ret
= -_get_value(expr
->unop
, undefined
, implied
);
78 ret
= _get_implied_value(expr
, undefined
, implied
);
86 static long long handle_divide(struct expression
*expr
, int *undefined
, int implied
)
90 long long ret
= BOGUS
;
92 left
= _get_value(expr
->left
, undefined
, implied
);
93 right
= _get_value(expr
->right
, undefined
, opposite_implied(implied
));
103 static long long handle_subtract(struct expression
*expr
, int *undefined
, int implied
)
108 left
= _get_value(expr
->left
, undefined
, implied
);
109 right
= _get_value(expr
->right
, undefined
, opposite_implied(implied
));
114 static long long handle_binop(struct expression
*expr
, int *undefined
, int implied
)
118 long long ret
= BOGUS
;
120 if (expr
->type
!= EXPR_BINOP
) {
125 left
= _get_value(expr
->left
, undefined
, implied
);
126 right
= _get_value(expr
->right
, undefined
, implied
);
133 ret
= handle_divide(expr
, undefined
, implied
);
139 ret
= handle_subtract(expr
, undefined
, implied
);
153 case SPECIAL_RIGHTSHIFT
:
156 case SPECIAL_LEFTSHIFT
:
168 static int get_implied_value_helper(struct expression
*expr
, long long *val
, int what
)
170 struct smatch_state
*state
;
174 if (get_value(expr
, val
))
177 name
= get_variable_from_expr(expr
, &sym
);
180 state
= get_state(SMATCH_EXTRA
, name
, sym
);
182 if (!state
|| !state
->data
)
185 return estate_get_single_value(state
, val
);
186 if (what
== IMPLIED_MAX
) {
187 *val
= estate_max(state
);
188 if (*val
== whole_range
.max
) /* this means just guessing */
192 *val
= estate_min(state
);
193 if (*val
== whole_range
.min
)
198 static int get_fuzzy_max_helper(struct expression
*expr
, long long *max
)
201 struct sm_state
*tmp
;
203 if (get_implied_max(expr
, max
))
206 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
210 *max
= whole_range
.min
;
211 FOR_EACH_PTR(sm
->possible
, tmp
) {
214 new_min
= estate_min(tmp
->state
);
217 } END_FOR_EACH_PTR(tmp
);
219 if (*max
> whole_range
.min
)
224 static int get_fuzzy_min_helper(struct expression
*expr
, long long *min
)
227 struct sm_state
*tmp
;
229 if (get_implied_min(expr
, min
))
232 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
236 *min
= whole_range
.max
;
237 FOR_EACH_PTR(sm
->possible
, tmp
) {
240 new_max
= estate_max(tmp
->state
);
243 } END_FOR_EACH_PTR(tmp
);
245 if (*min
< whole_range
.max
)
250 static long long _get_implied_value(struct expression
*expr
, int *undefined
, int implied
)
252 long long ret
= BOGUS
;
258 if (!get_implied_value_helper(expr
, &ret
, implied
))
262 if (!get_fuzzy_max_helper(expr
, &ret
))
266 if (!get_fuzzy_min_helper(expr
, &ret
))
275 static int get_const_value(struct expression
*expr
, long long *val
)
282 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
284 if (get_value(sym
->initializer
, val
))
289 static long long _get_value(struct expression
*expr
, int *undefined
, int implied
)
291 long long ret
= BOGUS
;
300 expr
= strip_parens(expr
);
302 switch (expr
->type
) {
305 ret
= cast_to_type(expr
, ret
);
308 ret
= handle_preop(expr
, undefined
, implied
);
311 ret
= _get_value(expr
->unop
, undefined
, implied
);
314 case EXPR_FORCE_CAST
:
315 case EXPR_IMPLIED_CAST
:
316 ret
= _get_value(expr
->cast_expression
, undefined
, implied
);
317 return cast_to_type(expr
, ret
);
319 ret
= handle_binop(expr
, undefined
, implied
);
323 ret
= get_expression_value(expr
);
326 if (get_const_value(expr
, &ret
))
329 ret
= _get_implied_value(expr
, undefined
, implied
);
336 /* returns 1 if it can get a value literal or else returns 0 */
337 int get_value(struct expression
*expr
, long long *val
)
341 *val
= _get_value(expr
, &undefined
, NOTIMPLIED
);
347 int get_implied_value(struct expression
*expr
, long long *val
)
351 *val
= _get_value(expr
, &undefined
, IMPLIED
);
355 int get_implied_min(struct expression
*expr
, long long *val
)
359 *val
= _get_value(expr
, &undefined
, IMPLIED_MIN
);
363 int get_implied_max(struct expression
*expr
, long long *val
)
367 *val
= _get_value(expr
, &undefined
, IMPLIED_MAX
);
371 int get_fuzzy_min(struct expression
*expr
, long long *val
)
375 *val
= _get_value(expr
, &undefined
, FUZZYMIN
);
379 int get_fuzzy_max(struct expression
*expr
, long long *val
)
383 *val
= _get_value(expr
, &undefined
, FUZZYMAX
);
387 int get_absolute_min(struct expression
*expr
, long long *val
)
392 type
= get_type(expr
);
394 if (get_value(expr
, val
))
398 min
= type_min(type
);
399 if (!get_implied_min(expr
, val
) || *val
< min
)
404 int get_absolute_max(struct expression
*expr
, long long *val
)
409 type
= get_type(expr
);
411 if (get_value(expr
, val
))
415 max
= type_max(type
);
416 if (!get_implied_max(expr
, val
) || *val
> max
)
418 if (*val
< type_min(type
))