2 * Copyright (C) 2006 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 * Miscellaneous helper functions.
26 #include "smatch_extra.h"
27 #include "smatch_slist.h"
31 char *alloc_string(const char *str
)
37 tmp
= malloc(strlen(str
) + 1);
42 char *alloc_string_newline(const char *str
)
50 tmp
= malloc(len
+ 2);
51 snprintf(tmp
, len
+ 2, "%s\n", str
);
55 void free_string(char *str
)
60 void remove_parens(char *str
)
65 while (*src
!= '\0') {
66 if (*src
== '(' || *src
== ')') {
75 struct smatch_state
*alloc_state_num(int num
)
77 struct smatch_state
*state
;
78 static char buff
[256];
80 state
= __alloc_smatch_state(0);
81 snprintf(buff
, 255, "%d", num
);
83 state
->name
= alloc_string(buff
);
84 state
->data
= INT_PTR(num
);
88 struct smatch_state
*alloc_state_str(const char *name
)
90 struct smatch_state
*state
;
92 state
= __alloc_smatch_state(0);
93 state
->name
= alloc_string(name
);
97 struct smatch_state
*merge_str_state(struct smatch_state
*s1
, struct smatch_state
*s2
)
99 if (!s1
->name
|| !s2
->name
)
101 if (strcmp(s1
->name
, s2
->name
) == 0)
106 struct smatch_state
*alloc_state_expr(struct expression
*expr
)
108 struct smatch_state
*state
;
111 expr
= strip_expr(expr
);
112 name
= expr_to_str(expr
);
116 state
= __alloc_smatch_state(0);
117 state
->name
= alloc_sname(name
);
123 static int FORMAT_ATTR(4) append(char *dest
, int off
, int len
, const char *fmt
, ...)
128 if (len
<= 0 || off
< 0 || off
>= len
- 1)
132 n
= vsnprintf(dest
+ off
, len
- off
, fmt
, args
);
135 if (n
> len
- off
- 1)
136 return len
- off
- 1;
141 * If you have "foo(a, b, 1);" then use
142 * get_argument_from_call_expr(expr, 0) to return the expression for
143 * a. Yes, it does start counting from 0.
145 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
148 struct expression
*expr
;
154 FOR_EACH_PTR(args
, expr
) {
158 } END_FOR_EACH_PTR(expr
);
162 struct expression
*get_array_expr(struct expression
*expr
)
164 struct expression
*parent
;
167 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
170 type
= get_type(expr
->left
);
173 if (type
->type
== SYM_ARRAY
)
175 if (type
->type
!= SYM_PTR
)
178 parent
= expr_get_parent_expr(expr
);
179 if (!parent
) /* Sometimes we haven't set up the ->parent yet. FIXME!! */
181 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '*')
187 static struct expression
*strip_star_address(struct expression
*expr
)
189 struct expression
*unop
;
191 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
193 unop
= strip_parens(expr
->unop
);
194 if (unop
->type
!= EXPR_PREOP
|| unop
->op
!= '&')
200 static struct expression
*strip_parens_symbol(struct expression
*expr
)
202 struct expression
*unop
;
204 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '(')
206 unop
= strip_parens(expr
->unop
);
207 if (unop
->type
!= EXPR_SYMBOL
)
212 static int __get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
213 struct expression
*expr
, int off
, int len
,
219 /* can't happen on valid code */
224 expr
= strip_star_address(expr
);
225 expr
= strip_parens_symbol(expr
);
227 switch (expr
->type
) {
229 struct expression
*deref
;
235 /* this is pure guess work and nonsense programming */
236 if (deref
->type
== EXPR_PREOP
&& deref
->op
== '*') {
241 off
+= __get_variable_from_expr(sym_ptr
, buf
, deref
, off
, len
, complicated
);
244 off
+= append(buf
, off
, len
, "->");
246 off
+= append(buf
, off
, len
, ".");
249 off
+= append(buf
, off
, len
, "%s", expr
->member
->name
);
251 off
+= append(buf
, off
, len
, "unknown_member");
252 return off
- orig_off
;
255 if (expr
->symbol_name
)
256 off
+= append(buf
, off
, len
, "%s", expr
->symbol_name
->name
);
260 *sym_ptr
= expr
->symbol
;
262 return off
- orig_off
;
266 if (get_expression_statement(expr
)) {
270 if (expr
->op
== SPECIAL_DECREMENT
||
271 expr
->op
== SPECIAL_INCREMENT
)
274 if (expr
->op
== '*' && get_array_expr(expr
->unop
))
277 tmp
= show_special(expr
->op
);
279 off
+= append(buf
, off
, len
, "%s", tmp
);
280 off
+= __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
, off
,
283 off
+= append(buf
, off
, len
, ")");
285 return off
- orig_off
;
288 off
+= __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
, off
, len
,
290 off
+= append(buf
, off
, len
, "%s", show_special(expr
->op
));
292 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
294 return off
- orig_off
;
296 case EXPR_ASSIGNMENT
:
300 struct expression
*array_expr
;
303 array_expr
= get_array_expr(expr
);
305 off
+= __get_variable_from_expr(sym_ptr
, buf
, array_expr
, off
, len
, complicated
);
306 off
+= append(buf
, off
, len
, "[");
308 off
+= __get_variable_from_expr(sym_ptr
, buf
, expr
->left
, off
, len
, complicated
);
309 off
+= append(buf
, off
, len
, " %s ", show_special(expr
->op
));
311 off
+= __get_variable_from_expr(NULL
, buf
, expr
->right
, off
, len
, complicated
);
313 off
+= append(buf
, off
, len
, "]");
314 return off
- orig_off
;
320 if (!get_value(expr
, &sval
))
322 off
+= append(buf
, off
, len
, "%s", sval_to_numstr(sval
));
323 return off
- orig_off
;
329 if (!get_value(expr
, &sval
))
332 off
+= append(buf
, off
, len
, "%s", sval_to_numstr(sval
));
333 return off
- orig_off
;
336 off
+= append(buf
, off
, len
, "\"");
338 off
+= append(buf
, off
, len
, "%s", expr
->string
->data
);
339 off
+= append(buf
, off
, len
, "\"");
340 return off
- orig_off
;
342 struct expression
*tmp
;
346 off
+= __get_variable_from_expr(NULL
, buf
, expr
->fn
, off
, len
, complicated
);
347 off
+= append(buf
, off
, len
, "(");
349 FOR_EACH_PTR(expr
->args
, tmp
) {
351 off
+= append(buf
, off
, len
, ", ");
352 off
+= __get_variable_from_expr(NULL
, buf
, tmp
, off
, len
, complicated
);
353 } END_FOR_EACH_PTR(tmp
);
354 off
+= append(buf
, off
, len
, ")");
355 return off
- orig_off
;
358 case EXPR_FORCE_CAST
:
359 return __get_variable_from_expr(sym_ptr
, buf
,
360 expr
->cast_expression
, off
, len
,
366 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
367 size
= type_bytes(get_base_type(expr
->cast_type
));
368 off
+= append(buf
, off
, len
, "%d", size
);
369 } else if (get_value(expr
, &sval
)) {
370 off
+= append(buf
, off
, len
, "%s", sval_to_str(sval
));
372 return off
- orig_off
;
374 case EXPR_IDENTIFIER
:
376 if (expr
->expr_ident
)
377 off
+= append(buf
, off
, len
, "%s", expr
->expr_ident
->name
);
378 return off
- orig_off
;
380 case EXPR_CONDITIONAL
:
382 off
+= append(buf
, off
, len
, "(");
383 off
+= __get_variable_from_expr(NULL
, buf
, expr
->conditional
, off
, len
, complicated
);
384 off
+= append(buf
, off
, len
, ") ?");
386 off
+= __get_variable_from_expr(NULL
, buf
, expr
->cond_true
, off
, len
, complicated
);
387 off
+= append(buf
, off
, len
, ":");
388 off
+= __get_variable_from_expr(NULL
, buf
, expr
->cond_false
, off
, len
, complicated
);
389 return off
- orig_off
;
391 off
+= append(buf
, off
, len
, "$expr_%p(%d)", expr
, expr
->type
);
392 return off
- orig_off
;
396 struct expr_str_cache_results
{
397 struct expression
*expr
;
403 static void get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
404 struct expression
*expr
, int len
,
407 static struct expr_str_cache_results cached
[8];
408 struct symbol
*tmp_sym
= NULL
;
412 for (i
= 0; i
< ARRAY_SIZE(cached
); i
++) {
413 if (expr
== cached
[i
].expr
) {
414 strncpy(buf
, cached
[i
].str
, len
);
416 *sym_ptr
= cached
[i
].sym
;
417 *complicated
= cached
[i
].complicated
;
422 __get_variable_from_expr(&tmp_sym
, buf
, expr
, 0, len
, complicated
);
426 if (expr
->smatch_flags
& Tmp
)
429 cached
[idx
].expr
= expr
;
430 strncpy(cached
[idx
].str
, buf
, VAR_LEN
);
431 cached
[idx
].sym
= tmp_sym
;
432 cached
[idx
].complicated
= *complicated
;
434 idx
= (idx
+ 1) % ARRAY_SIZE(cached
);
438 * This is returns a stylized "c looking" representation of the
441 * It uses the same buffer every time so you have to save the result
442 * yourself if you want to keep it.
446 char *expr_to_str_sym(struct expression
*expr
, struct symbol
**sym_ptr
)
448 static char var_name
[VAR_LEN
];
457 get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
460 return alloc_string(var_name
);
465 char *expr_to_str(struct expression
*expr
)
467 return expr_to_str_sym(expr
, NULL
);
471 * get_variable_from_expr_simple() only returns simple variables.
472 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
473 * then it returns NULL.
475 char *expr_to_var_sym(struct expression
*expr
,
476 struct symbol
**sym_ptr
)
478 static char var_name
[VAR_LEN
];
487 expr
= strip_expr(expr
);
488 get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
496 return alloc_string(var_name
);
499 char *expr_to_var(struct expression
*expr
)
501 return expr_to_var_sym(expr
, NULL
);
504 struct symbol
*expr_to_sym(struct expression
*expr
)
509 name
= expr_to_var_sym(expr
, &sym
);
514 int get_complication_score(struct expression
*expr
)
516 expr
= strip_expr(expr
);
519 * Don't forget to keep get_complication_score() and store_all_links()
527 switch (expr
->type
) {
532 return get_complication_score(expr
->left
) +
533 get_complication_score(expr
->right
);
537 if (expr
->op
== '*' || expr
->op
== '(')
538 return get_complication_score(expr
->unop
);
541 return get_complication_score(expr
->deref
);
550 struct expression
*reorder_expr_alphabetically(struct expression
*expr
)
552 struct expression
*ret
;
555 if (expr
->type
!= EXPR_BINOP
)
557 if (expr
->op
!= '+' && expr
->op
!= '*')
560 left
= expr_to_var(expr
->left
);
561 right
= expr_to_var(expr
->right
);
565 if (strcmp(left
, right
) <= 0)
568 ret
= binop_expression(expr
->right
, expr
->op
, expr
->left
);
576 char *expr_to_chunk_helper(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
578 struct var_sym_list
*tmp_vsl
;
588 expr
= strip_parens(expr
);
592 name
= expr_to_var_sym(expr
, &tmp
);
597 add_var_sym(vsl
, name
, tmp
);
602 score
= get_complication_score(expr
);
603 if (score
<= 0 || score
> 2)
606 tmp_vsl
= expr_to_vsl(expr
);
613 if (ptr_list_size((struct ptr_list
*)tmp_vsl
) == 1) {
616 vs
= first_ptr_list((struct ptr_list
*)tmp_vsl
);
621 expr
= reorder_expr_alphabetically(expr
);
623 return expr_to_str(expr
);
626 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
)
628 return expr_to_chunk_helper(expr
, sym
, NULL
);
631 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
633 return expr_to_chunk_helper(expr
, sym
, vsl
);
636 int sym_name_is(const char *name
, struct expression
*expr
)
640 if (expr
->type
!= EXPR_SYMBOL
)
642 if (!strcmp(expr
->symbol_name
->name
, name
))
647 int expr_is_zero(struct expression
*expr
)
651 if (get_value(expr
, &sval
) && sval
.value
== 0)
656 int is_array(struct expression
*expr
)
660 expr
= strip_expr(expr
);
664 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
665 expr
= strip_expr(expr
->unop
);
668 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
672 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
675 type
= get_type(expr
->left
);
676 if (!type
|| type
->type
!= SYM_ARRAY
)
682 struct expression
*get_array_base(struct expression
*expr
)
686 expr
= strip_expr(expr
);
687 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
688 expr
= strip_expr(expr
->unop
);
689 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
691 return strip_parens(expr
->left
);
694 struct expression
*get_array_offset(struct expression
*expr
)
698 expr
= strip_expr(expr
);
699 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
700 expr
= strip_expr(expr
->unop
);
701 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
703 return strip_parens(expr
->right
);
706 const char *show_state(struct smatch_state
*state
)
713 struct statement
*get_expression_statement(struct expression
*expr
)
715 /* What are those things called? if (({....; ret;})) { ...*/
717 if (expr
->type
!= EXPR_PREOP
)
723 if (expr
->unop
->type
!= EXPR_STATEMENT
)
725 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
727 return expr
->unop
->statement
;
730 struct expression
*strip_parens(struct expression
*expr
)
735 if (expr
->type
== EXPR_PREOP
) {
737 return expr
; /* parsing invalid code */
739 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
740 expr
->unop
->statement
->type
== STMT_COMPOUND
)
743 return strip_parens(expr
->unop
);
748 static struct expression
*strip_expr_helper(struct expression
*expr
, bool set_parent
)
753 switch (expr
->type
) {
754 case EXPR_FORCE_CAST
:
757 expr_set_parent_expr(expr
->cast_expression
, expr
);
759 if (!expr
->cast_expression
)
761 return strip_expr_helper(expr
->cast_expression
, set_parent
);
763 struct expression
*unop
;
765 if (!expr
->unop
) /* parsing invalid code */
768 expr_set_parent_expr(expr
->unop
, expr
);
771 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
772 expr
->unop
->statement
->type
== STMT_COMPOUND
)
775 unop
= strip_expr_helper(expr
->unop
, set_parent
);
777 if (expr
->op
== '*' && unop
&&
778 unop
->type
== EXPR_PREOP
&& unop
->op
== '&') {
779 struct symbol
*type
= get_type(unop
->unop
);
781 if (type
&& type
->type
== SYM_ARRAY
)
783 return strip_expr_helper(unop
->unop
, set_parent
);
791 case EXPR_CONDITIONAL
:
792 if (known_condition_true(expr
->conditional
)) {
793 if (expr
->cond_true
) {
795 expr_set_parent_expr(expr
->cond_true
, expr
);
796 return strip_expr_helper(expr
->cond_true
, set_parent
);
799 expr_set_parent_expr(expr
->conditional
, expr
);
800 return strip_expr_helper(expr
->conditional
, set_parent
);
802 if (known_condition_false(expr
->conditional
)) {
804 expr_set_parent_expr(expr
->cond_false
, expr
);
805 return strip_expr_helper(expr
->cond_false
, set_parent
);
809 if (sym_name_is("__builtin_expect", expr
->fn
) ||
810 sym_name_is("__builtin_bswap16", expr
->fn
) ||
811 sym_name_is("__builtin_bswap32", expr
->fn
) ||
812 sym_name_is("__builtin_bswap64", expr
->fn
)) {
813 expr
= get_argument_from_call_expr(expr
->args
, 0);
814 return strip_expr_helper(expr
, set_parent
);
821 struct expression
*strip_expr(struct expression
*expr
)
823 return strip_expr_helper(expr
, false);
826 struct expression
*strip_expr_set_parent(struct expression
*expr
)
828 return strip_expr_helper(expr
, true);
831 static void delete_state_tracker(struct tracker
*t
)
833 __delete_state(t
->owner
, t
->name
, t
->sym
);
837 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
841 t
= alloc_tracker(my_id
, name
, sym
);
842 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
845 int is_error_return(struct expression
*expr
)
847 struct symbol
*cur_func
= cur_func_sym
;
848 struct range_list
*rl
;
853 if (cur_func
->type
!= SYM_NODE
)
855 cur_func
= get_base_type(cur_func
);
856 if (cur_func
->type
!= SYM_FN
)
858 cur_func
= get_base_type(cur_func
);
859 if (cur_func
== &void_ctype
)
861 if (option_project
== PROJ_KERNEL
&&
862 get_implied_rl(expr
, &rl
) &&
863 rl_type(rl
) == &int_ctype
&&
864 sval_is_negative(rl_min(rl
)) &&
865 rl_max(rl
).value
== -1)
867 if (!get_implied_value(expr
, &sval
))
871 if (cur_func
->type
== SYM_PTR
&& sval
.value
== 0)
876 int getting_address(struct expression
*expr
)
880 while ((expr
= expr_get_parent_expr(expr
))) {
881 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
882 /* &foo->bar->baz dereferences "foo->bar" */
883 if (deref_count
== 0)
887 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
893 int get_struct_and_member(struct expression
*expr
, const char **type
, const char **member
)
897 expr
= strip_expr(expr
);
898 if (expr
->type
!= EXPR_DEREF
)
903 sym
= get_type(expr
->deref
);
906 if (sym
->type
== SYM_UNION
)
911 *type
= sym
->ident
->name
;
912 *member
= expr
->member
->name
;
916 char *get_member_name(struct expression
*expr
)
921 expr
= strip_expr(expr
);
922 if (!expr
|| expr
->type
!= EXPR_DEREF
)
927 sym
= get_type(expr
->deref
);
930 if (sym
->type
== SYM_UNION
) {
931 snprintf(buf
, sizeof(buf
), "(union %s)->%s",
932 sym
->ident
? sym
->ident
->name
: "anonymous",
934 return alloc_string(buf
);
937 struct expression
*deref
;
942 * If we're in an anonymous struct then maybe we can find an
943 * outer struct name to use as a name. This code should be
944 * recursive and cleaner. I am not very proud of it.
949 if (deref
->type
!= EXPR_DEREF
|| !deref
->member
)
951 sym
= get_type(deref
->deref
);
952 if (!sym
|| sym
->type
!= SYM_STRUCT
|| !sym
->ident
)
955 full
= expr_to_str(expr
);
958 deref
= deref
->deref
;
959 if (deref
->type
== EXPR_PREOP
&& deref
->op
== '*')
961 outer
= expr_to_str(deref
);
967 if (strncmp(outer
, full
, len
) != 0) {
972 if (full
[len
] == '-' && full
[len
+ 1] == '>')
974 if (full
[len
] == '.')
976 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, full
+ len
);
980 return alloc_string(buf
);
982 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, expr
->member
->name
);
983 return alloc_string(buf
);
986 int cmp_pos(struct position pos1
, struct position pos2
)
988 /* the stream position is ... */
989 if (pos1
.stream
> pos2
.stream
)
991 if (pos1
.stream
< pos2
.stream
)
994 if (pos1
.line
< pos2
.line
)
996 if (pos1
.line
> pos2
.line
)
999 if (pos1
.pos
< pos2
.pos
)
1001 if (pos1
.pos
> pos2
.pos
)
1007 int positions_eq(struct position pos1
, struct position pos2
)
1009 if (pos1
.line
!= pos2
.line
)
1011 if (pos1
.pos
!= pos2
.pos
)
1013 if (pos1
.stream
!= pos2
.stream
)
1018 struct statement
*get_current_statement(void)
1020 struct statement
*prev
, *tmp
;
1022 prev
= last_ptr_list((struct ptr_list
*)big_statement_stack
);
1024 if (!prev
|| !get_macro_name(prev
->pos
))
1027 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
1028 if (positions_eq(tmp
->pos
, prev
->pos
))
1030 if (prev
->pos
.line
> tmp
->pos
.line
)
1033 } END_FOR_EACH_PTR_REVERSE(tmp
);
1037 struct statement
*get_prev_statement(void)
1039 struct statement
*tmp
;
1043 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
1046 } END_FOR_EACH_PTR_REVERSE(tmp
);
1050 struct expression
*get_last_expr_from_expression_stmt(struct expression
*expr
)
1052 struct statement
*stmt
;
1053 struct statement
*last_stmt
;
1055 while (expr
->type
== EXPR_PREOP
&& expr
->op
== '(')
1057 if (expr
->type
!= EXPR_STATEMENT
)
1059 stmt
= expr
->statement
;
1062 if (stmt
->type
== STMT_COMPOUND
) {
1063 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
1066 if (last_stmt
->type
== STMT_LABEL
)
1067 last_stmt
= last_stmt
->label_statement
;
1068 if (last_stmt
->type
!= STMT_EXPRESSION
)
1070 return last_stmt
->expression
;
1072 if (stmt
->type
== STMT_EXPRESSION
)
1073 return stmt
->expression
;
1077 int ms_since(struct timeval
*start
)
1082 gettimeofday(&end
, NULL
);
1083 diff
= (end
.tv_sec
- start
->tv_sec
) * 1000.0;
1084 diff
+= (end
.tv_usec
- start
->tv_usec
) / 1000.0;
1088 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
)
1093 if (parent_is_err_or_null_var_sym(name
, sym
) ||
1094 parent_is_free_var_sym(name
, sym
))
1099 int parent_is_gone(struct expression
*expr
)
1105 expr
= strip_expr(expr
);
1106 var
= expr_to_var_sym(expr
, &sym
);
1109 ret
= parent_is_gone_var_sym(var
, sym
);
1115 int invert_op(int op
)
1126 case SPECIAL_LEFTSHIFT
:
1127 return SPECIAL_RIGHTSHIFT
;
1128 case SPECIAL_RIGHTSHIFT
:
1129 return SPECIAL_LEFTSHIFT
;
1134 int op_remove_assign(int op
)
1137 case SPECIAL_ADD_ASSIGN
:
1139 case SPECIAL_SUB_ASSIGN
:
1141 case SPECIAL_MUL_ASSIGN
:
1143 case SPECIAL_DIV_ASSIGN
:
1145 case SPECIAL_MOD_ASSIGN
:
1147 case SPECIAL_AND_ASSIGN
:
1149 case SPECIAL_OR_ASSIGN
:
1151 case SPECIAL_XOR_ASSIGN
:
1153 case SPECIAL_SHL_ASSIGN
:
1154 return SPECIAL_LEFTSHIFT
;
1155 case SPECIAL_SHR_ASSIGN
:
1156 return SPECIAL_RIGHTSHIFT
;
1162 int expr_equiv(struct expression
*one
, struct expression
*two
)
1164 struct symbol
*one_sym
= NULL
;
1165 struct symbol
*two_sym
= NULL
;
1166 char *one_name
= NULL
;
1167 char *two_name
= NULL
;
1172 if (one
->type
!= two
->type
)
1174 if (is_fake_call(one
) || is_fake_call(two
))
1177 one_name
= expr_to_str_sym(one
, &one_sym
);
1180 two_name
= expr_to_str_sym(two
, &two_sym
);
1183 if (one_sym
!= two_sym
)
1186 * This is a terrible hack because expr_to_str() sometimes gives up in
1187 * the middle and just returns what it has. If you see a () you know
1188 * the string is bogus.
1190 if (strstr(one_name
, "()"))
1192 if (strcmp(one_name
, two_name
) == 0)
1195 free_string(one_name
);
1196 free_string(two_name
);
1200 void push_int(struct int_stack
**stack
, int num
)
1205 * Just put the int on directly instead of a pointer to the int.
1206 * Shift it to the left because Sparse uses the last two bits.
1207 * This is sort of a dirty hack, yes.
1210 munged
= INT_PTR(num
<< 2);
1212 add_ptr_list(stack
, munged
);
1215 int pop_int(struct int_stack
**stack
)
1219 num
= last_ptr_list((struct ptr_list
*)*stack
);
1220 delete_ptr_list_last((struct ptr_list
**)stack
);
1222 return PTR_INT(num
) >> 2;