Get comparison sizes right.
[smatch.git] / linearize.h
blob407754274af79598b4a257461c0cc45a4fd6dd9d
1 #ifndef LINEARIZE_H
2 #define LINEARIZE_H
4 #include "lib.h"
5 #include "token.h"
6 #include "parse.h"
7 #include "symbol.h"
9 struct instruction;
10 DECLARE_PTR_LIST(pseudo_ptr_list, pseudo_t);
12 enum pseudo_type {
13 PSEUDO_VOID,
14 PSEUDO_REG,
15 PSEUDO_SYM,
16 PSEUDO_VAL,
17 PSEUDO_ARG,
18 PSEUDO_PHI,
21 struct pseudo {
22 int nr;
23 enum pseudo_type type;
24 struct pseudo_ptr_list *users;
25 struct ident *ident;
26 union {
27 struct symbol *sym;
28 struct instruction *def;
29 long long value;
33 extern struct pseudo void_pseudo;
35 #define VOID (&void_pseudo)
37 struct multijmp {
38 struct basic_block *target;
39 int begin, end;
42 struct asm_constraint {
43 pseudo_t pseudo;
44 const char *constraint;
45 const struct ident *ident;
48 DECLARE_PTR_LIST(asm_constraint_list, struct asm_constraint);
50 struct asm_rules {
51 struct asm_constraint_list *inputs;
52 struct asm_constraint_list *outputs;
53 struct asm_constraint_list *clobbers;
56 struct instruction {
57 unsigned opcode:8,
58 size:24;
59 struct basic_block *bb;
60 union {
61 pseudo_t target;
62 pseudo_t cond; /* for branch and switch */
64 union {
65 struct /* entrypoint */ {
66 struct pseudo_list *arg_list;
68 struct /* branch */ {
69 struct basic_block *bb_true, *bb_false;
71 struct /* switch */ {
72 struct multijmp_list *multijmp_list;
74 struct /* phi_node */ {
75 struct pseudo_list *phi_list;
77 struct /* phi source */ {
78 pseudo_t phi_src;
79 struct instruction_list *phi_users;
81 struct /* unops */ {
82 pseudo_t src;
83 struct symbol *orig_type; /* casts */
84 unsigned int offset; /* memops */
86 struct /* binops and sel */ {
87 pseudo_t src1, src2, src3;
89 struct /* slice */ {
90 pseudo_t base;
91 unsigned from, len;
93 struct /* multijump */ {
94 int begin, end;
96 struct /* setval */ {
97 pseudo_t symbol; /* Subtle: same offset as "src" !! */
98 struct expression *val;
100 struct /* call */ {
101 pseudo_t func;
102 struct pseudo_list *arguments;
104 struct /* context */ {
105 int increment;
107 struct /* asm */ {
108 const char *string;
109 struct asm_rules *asm_rules;
114 enum opcode {
115 OP_BADOP,
117 /* Entry */
118 OP_ENTRY,
120 /* Terminator */
121 OP_TERMINATOR,
122 OP_RET = OP_TERMINATOR,
123 OP_BR,
124 OP_SWITCH,
125 OP_INVOKE,
126 OP_COMPUTEDGOTO,
127 OP_UNWIND,
128 OP_TERMINATOR_END = OP_UNWIND,
130 /* Binary */
131 OP_BINARY,
132 OP_ADD = OP_BINARY,
133 OP_SUB,
134 OP_MUL,
135 OP_DIV,
136 OP_MOD,
137 OP_SHL,
138 OP_SHR,
140 /* Logical */
141 OP_AND,
142 OP_OR,
143 OP_XOR,
144 OP_AND_BOOL,
145 OP_OR_BOOL,
146 OP_BINARY_END = OP_OR_BOOL,
148 /* Binary comparison */
149 OP_BINCMP,
150 OP_SET_EQ = OP_BINCMP,
151 OP_SET_NE,
152 OP_SET_LE,
153 OP_SET_GE,
154 OP_SET_LT,
155 OP_SET_GT,
156 OP_SET_B,
157 OP_SET_A,
158 OP_SET_BE,
159 OP_SET_AE,
160 OP_BINCMP_END = OP_SET_AE,
162 /* Uni */
163 OP_NOT,
164 OP_NEG,
166 /* Select - three input values */
167 OP_SEL,
169 /* Memory */
170 OP_MALLOC,
171 OP_FREE,
172 OP_ALLOCA,
173 OP_LOAD,
174 OP_STORE,
175 OP_SETVAL,
176 OP_SYMADDR,
177 OP_GET_ELEMENT_PTR,
179 /* Other */
180 OP_PHI,
181 OP_PHISOURCE,
182 OP_CAST,
183 OP_PTRCAST,
184 OP_CALL,
185 OP_VANEXT,
186 OP_VAARG,
187 OP_SLICE,
188 OP_SNOP,
189 OP_LNOP,
190 OP_NOP,
191 OP_DEATHNOTE,
192 OP_ASM,
194 /* Sparse tagging (line numbers, context, whatever) */
195 OP_CONTEXT,
198 struct basic_block_list;
199 struct instruction_list;
201 struct basic_block {
202 struct position pos;
203 unsigned long generation;
204 int context;
205 struct entrypoint *ep;
206 struct basic_block_list *parents; /* sources */
207 struct basic_block_list *children; /* destinations */
208 struct instruction_list *insns; /* Linear list of instructions */
209 struct pseudo_list *needs, *defines;
212 static inline int is_branch_goto(struct instruction *br)
214 return br && br->opcode==OP_BR && (!br->bb_true || !br->bb_false);
217 static inline void add_bb(struct basic_block_list **list, struct basic_block *bb)
219 add_ptr_list(list, bb);
222 static inline void add_instruction(struct instruction_list **list, struct instruction *insn)
224 add_ptr_list(list, insn);
227 static inline void add_multijmp(struct multijmp_list **list, struct multijmp *multijmp)
229 add_ptr_list(list, multijmp);
232 static inline void *add_pseudo(struct pseudo_list **list, struct pseudo *pseudo)
234 return add_ptr_list(list, pseudo);
237 static inline int remove_pseudo(struct pseudo_list **list, pseudo_t pseudo)
239 return delete_ptr_list_entry((struct ptr_list **)list, pseudo, 0) != 0;
242 static inline int bb_terminated(struct basic_block *bb)
244 struct instruction *insn;
245 if (!bb)
246 return 0;
247 insn = last_instruction(bb->insns);
248 return insn && insn->opcode >= OP_RET && insn->opcode <= OP_UNWIND;
251 static inline int bb_reachable(struct basic_block *bb)
253 return bb != NULL;
256 static inline void add_pseudo_ptr(pseudo_t *ptr, struct pseudo_ptr_list **list)
258 add_ptr_list(list, ptr);
261 static inline int has_use_list(pseudo_t p)
263 return (p && p->type != PSEUDO_VOID && p->type != PSEUDO_VAL);
266 static inline void use_pseudo(pseudo_t p, pseudo_t *pp)
268 *pp = p;
269 if (has_use_list(p))
270 add_pseudo_ptr(pp, &p->users);
273 static inline void remove_bb_from_list(struct basic_block_list **list, struct basic_block *entry, int count)
275 delete_ptr_list_entry((struct ptr_list **)list, entry, count);
278 static inline void replace_bb_in_list(struct basic_block_list **list,
279 struct basic_block *old, struct basic_block *new, int count)
281 replace_ptr_list_entry((struct ptr_list **)list, old, new, count);
284 struct entrypoint {
285 struct symbol *name;
286 struct symbol_list *syms;
287 struct symbol_list *accesses;
288 struct basic_block_list *bbs;
289 struct basic_block *active;
290 struct instruction *entry;
293 extern void insert_select(struct basic_block *bb, struct instruction *br, struct instruction *phi, pseudo_t true, pseudo_t false);
294 extern void insert_branch(struct basic_block *bb, struct instruction *br, struct basic_block *target);
296 pseudo_t alloc_phi(struct basic_block *source, pseudo_t pseudo, int size);
297 pseudo_t alloc_pseudo(struct instruction *def);
298 pseudo_t value_pseudo(long long val);
300 struct entrypoint *linearize_symbol(struct symbol *sym);
301 void show_entry(struct entrypoint *ep);
302 const char *show_pseudo(pseudo_t pseudo);
303 void show_bb(struct basic_block *bb);
304 const char *show_instruction(struct instruction *insn);
306 #endif /* LINEARIZE_H */