math: remove the get_implied_value_low_overhead() function
[smatch.git] / smatch_estate.c
blobeaa99632bf49394d78b8e326e5c273a34edfe598
1 /*
2 * Copyright (C) 2010 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * smatch_dinfo.c has helper functions for handling data_info structs
23 #include <stdlib.h>
24 #ifndef __USE_ISOC99
25 #define __USE_ISOC99
26 #endif
27 #include <limits.h>
28 #include "parse.h"
29 #include "smatch.h"
30 #include "smatch_slist.h"
31 #include "smatch_extra.h"
33 struct smatch_state *merge_estates(struct smatch_state *s1, struct smatch_state *s2)
35 struct smatch_state *tmp;
36 struct range_list *value_ranges;
37 struct related_list *rlist;
39 if (estates_equiv(s1, s2))
40 return s1;
42 value_ranges = rl_union(estate_rl(s1), estate_rl(s2));
43 tmp = alloc_estate_rl(value_ranges);
44 rlist = get_shared_relations(estate_related(s1), estate_related(s2));
45 set_related(tmp, rlist);
47 if ((estate_has_hard_max(s1) && (!estate_rl(s2) || estate_has_hard_max(s2))) ||
48 (estate_has_hard_max(s2) && (!estate_rl(s1) || estate_has_hard_max(s1))))
49 estate_set_hard_max(tmp);
51 estate_set_fuzzy_max(tmp, sval_max(estate_get_fuzzy_max(s1), estate_get_fuzzy_max(s2)));
53 return tmp;
56 struct data_info *get_dinfo(struct smatch_state *state)
58 if (!state)
59 return NULL;
60 return (struct data_info *)state->data;
63 struct range_list *estate_rl(struct smatch_state *state)
65 if (!state)
66 return NULL;
67 return get_dinfo(state)->value_ranges;
70 struct related_list *estate_related(struct smatch_state *state)
72 if (!state)
73 return NULL;
74 return get_dinfo(state)->related;
77 sval_t estate_get_fuzzy_max(struct smatch_state *state)
79 sval_t empty = {};
81 if (!state || !get_dinfo(state))
82 return empty;
83 return get_dinfo(state)->fuzzy_max;
86 int estate_has_fuzzy_max(struct smatch_state *state)
88 if (estate_get_fuzzy_max(state).type)
89 return 1;
90 return 0;
93 void estate_set_fuzzy_max(struct smatch_state *state, sval_t fuzzy_max)
95 if (!rl_has_sval(estate_rl(state), fuzzy_max))
96 return;
97 get_dinfo(state)->fuzzy_max = fuzzy_max;
100 void estate_copy_fuzzy_max(struct smatch_state *new, struct smatch_state *old)
102 if (!estate_has_fuzzy_max(old))
103 return;
104 estate_set_fuzzy_max(new, estate_get_fuzzy_max(old));
107 void estate_clear_fuzzy_max(struct smatch_state *state)
109 sval_t empty = {};
111 get_dinfo(state)->fuzzy_max = empty;
114 int estate_has_hard_max(struct smatch_state *state)
116 if (!state)
117 return 0;
118 return get_dinfo(state)->hard_max;
121 void estate_set_hard_max(struct smatch_state *state)
123 get_dinfo(state)->hard_max = 1;
126 void estate_clear_hard_max(struct smatch_state *state)
128 get_dinfo(state)->hard_max = 0;
131 int estate_get_hard_max(struct smatch_state *state, sval_t *sval)
133 if (!state || !get_dinfo(state)->hard_max || !estate_rl(state))
134 return 0;
135 *sval = rl_max(estate_rl(state));
136 return 1;
139 sval_t estate_min(struct smatch_state *state)
141 return rl_min(estate_rl(state));
144 sval_t estate_max(struct smatch_state *state)
146 return rl_max(estate_rl(state));
149 struct symbol *estate_type(struct smatch_state *state)
151 return rl_max(estate_rl(state)).type;
154 static int rlists_equiv(struct related_list *one, struct related_list *two)
156 struct relation *one_rel;
157 struct relation *two_rel;
159 PREPARE_PTR_LIST(one, one_rel);
160 PREPARE_PTR_LIST(two, two_rel);
161 for (;;) {
162 if (!one_rel && !two_rel)
163 return 1;
164 if (!one_rel || !two_rel)
165 return 0;
166 if (one_rel->sym != two_rel->sym)
167 return 0;
168 if (strcmp(one_rel->name, two_rel->name))
169 return 0;
170 NEXT_PTR_LIST(one_rel);
171 NEXT_PTR_LIST(two_rel);
173 FINISH_PTR_LIST(two_rel);
174 FINISH_PTR_LIST(one_rel);
176 return 1;
179 int estates_equiv(struct smatch_state *one, struct smatch_state *two)
181 if (!one || !two)
182 return 0;
183 if (one == two)
184 return 1;
185 if (!rlists_equiv(estate_related(one), estate_related(two)))
186 return 0;
187 if (strcmp(one->name, two->name) == 0)
188 return 1;
189 return 0;
192 int estate_is_whole(struct smatch_state *state)
194 return is_whole_rl(estate_rl(state));
197 int estate_is_empty(struct smatch_state *state)
199 return state && !estate_rl(state);
202 int estate_is_unknown(struct smatch_state *state)
204 if (!estate_is_whole(state))
205 return 0;
206 if (estate_related(state))
207 return 0;
208 if (estate_has_fuzzy_max(state))
209 return 0;
210 return 1;
213 int estate_get_single_value(struct smatch_state *state, sval_t *sval)
215 sval_t min, max;
217 min = rl_min(estate_rl(state));
218 max = rl_max(estate_rl(state));
219 if (sval_cmp(min, max) != 0)
220 return 0;
221 *sval = min;
222 return 1;
225 static struct data_info *alloc_dinfo(void)
227 struct data_info *ret;
229 ret = __alloc_data_info(0);
230 memset(ret, 0, sizeof(*ret));
231 return ret;
234 static struct data_info *alloc_dinfo_range(sval_t min, sval_t max)
236 struct data_info *ret;
238 ret = alloc_dinfo();
239 add_range(&ret->value_ranges, min, max);
240 return ret;
243 static struct data_info *alloc_dinfo_range_list(struct range_list *rl)
245 struct data_info *ret;
247 ret = alloc_dinfo();
248 ret->value_ranges = rl;
249 return ret;
252 static struct data_info *clone_dinfo(struct data_info *dinfo)
254 struct data_info *ret;
256 ret = alloc_dinfo();
257 ret->related = clone_related_list(dinfo->related);
258 ret->value_ranges = clone_rl(dinfo->value_ranges);
259 ret->hard_max = dinfo->hard_max;
260 ret->fuzzy_max = dinfo->fuzzy_max;
261 return ret;
264 struct smatch_state *clone_estate(struct smatch_state *state)
266 struct smatch_state *ret;
268 if (!state)
269 return NULL;
271 ret = __alloc_smatch_state(0);
272 ret->name = state->name;
273 ret->data = clone_dinfo(get_dinfo(state));
274 return ret;
277 struct smatch_state *clone_partial_estate(struct smatch_state *state, struct range_list *rl)
279 struct smatch_state *ret;
281 if (!state)
282 return NULL;
284 rl = cast_rl(estate_type(state), rl);
286 ret = alloc_estate_rl(rl);
287 set_related(ret, clone_related_list(estate_related(state)));
288 if (estate_has_hard_max(state))
289 estate_set_hard_max(ret);
290 if (estate_has_fuzzy_max(state))
291 estate_set_fuzzy_max(ret, estate_get_fuzzy_max(state));
293 return ret;
296 struct smatch_state *alloc_estate_empty(void)
298 struct smatch_state *state;
299 struct data_info *dinfo;
301 dinfo = alloc_dinfo();
302 state = __alloc_smatch_state(0);
303 state->data = dinfo;
304 state->name = "";
305 return state;
308 struct smatch_state *alloc_estate_whole(struct symbol *type)
310 return alloc_estate_rl(alloc_whole_rl(type));
313 struct smatch_state *extra_empty(void)
315 struct smatch_state *ret;
317 ret = __alloc_smatch_state(0);
318 ret->name = "empty";
319 ret->data = alloc_dinfo();
320 return ret;
323 struct smatch_state *alloc_estate_sval(sval_t sval)
325 struct smatch_state *state;
327 state = __alloc_smatch_state(0);
328 state->data = alloc_dinfo_range(sval, sval);
329 state->name = show_rl(get_dinfo(state)->value_ranges);
330 estate_set_hard_max(state);
331 estate_set_fuzzy_max(state, sval);
332 return state;
335 struct smatch_state *alloc_estate_range(sval_t min, sval_t max)
337 struct smatch_state *state;
339 state = __alloc_smatch_state(0);
340 state->data = alloc_dinfo_range(min, max);
341 state->name = show_rl(get_dinfo(state)->value_ranges);
342 return state;
345 struct smatch_state *alloc_estate_rl(struct range_list *rl)
347 struct smatch_state *state;
349 if (!rl)
350 return extra_empty();
352 state = __alloc_smatch_state(0);
353 state->data = alloc_dinfo_range_list(rl);
354 state->name = show_rl(rl);
355 return state;
358 struct smatch_state *clone_estate_cast(struct symbol *type, struct smatch_state *state)
360 struct smatch_state *ret;
361 struct data_info *dinfo;
363 if (!state)
364 return NULL;
366 dinfo = alloc_dinfo();
367 dinfo->value_ranges = clone_rl(cast_rl(type, estate_rl(state)));
369 ret = __alloc_smatch_state(0);
370 ret->name = show_rl(dinfo->value_ranges);
371 ret->data = dinfo;
373 return ret;
376 struct smatch_state *get_implied_estate(struct expression *expr)
378 struct smatch_state *state;
379 struct range_list *rl;
381 state = get_state_expr(SMATCH_EXTRA, expr);
382 if (state)
383 return state;
384 if (!get_implied_rl(expr, &rl))
385 rl = alloc_whole_rl(get_type(expr));
386 return alloc_estate_rl(rl);
390 * One of the complications is that smatch tries to free a bunch of data at the
391 * end of every function.
393 struct data_info *clone_dinfo_perm(struct data_info *dinfo)
395 struct data_info *ret;
397 ret = malloc(sizeof(*ret));
398 memset(ret, 0, sizeof(*ret));
399 ret->related = NULL;
400 ret->value_ranges = clone_rl_permanent(dinfo->value_ranges);
401 ret->hard_max = 0;
402 ret->fuzzy_max = dinfo->fuzzy_max;
403 return ret;
406 struct smatch_state *clone_estate_perm(struct smatch_state *state)
408 struct smatch_state *ret;
410 ret = malloc(sizeof(*ret));
411 ret->name = alloc_string(state->name);
412 ret->data = clone_dinfo_perm(get_dinfo(state));
413 return ret;