*new* check_macros: find macro precedence bugs
[smatch.git] / linearize.h
blob50b36018466374a9e4a34872497ec12c26393fe8
1 #ifndef LINEARIZE_H
2 #define LINEARIZE_H
4 #include "lib.h"
5 #include "allocate.h"
6 #include "token.h"
7 #include "parse.h"
8 #include "symbol.h"
10 struct instruction;
11 DECLARE_PTR_LIST(pseudo_ptr_list, pseudo_t);
13 struct pseudo_user {
14 struct instruction *insn;
15 pseudo_t *userp;
18 DECLARE_ALLOCATOR(pseudo_user);
19 DECLARE_PTR_LIST(pseudo_user_list, struct pseudo_user);
22 enum pseudo_type {
23 PSEUDO_VOID,
24 PSEUDO_REG,
25 PSEUDO_SYM,
26 PSEUDO_VAL,
27 PSEUDO_ARG,
28 PSEUDO_PHI,
31 struct pseudo {
32 int nr;
33 enum pseudo_type type;
34 struct pseudo_user_list *users;
35 struct ident *ident;
36 union {
37 struct symbol *sym;
38 struct instruction *def;
39 long long value;
43 extern struct pseudo void_pseudo;
45 #define VOID (&void_pseudo)
47 struct multijmp {
48 struct basic_block *target;
49 int begin, end;
52 struct asm_constraint {
53 pseudo_t pseudo;
54 const char *constraint;
55 const struct ident *ident;
58 DECLARE_ALLOCATOR(asm_constraint);
59 DECLARE_PTR_LIST(asm_constraint_list, struct asm_constraint);
61 struct asm_rules {
62 struct asm_constraint_list *inputs;
63 struct asm_constraint_list *outputs;
64 struct asm_constraint_list *clobbers;
67 DECLARE_ALLOCATOR(asm_rules);
69 struct instruction {
70 unsigned opcode:8,
71 size:24;
72 struct basic_block *bb;
73 struct position pos;
74 struct symbol *type;
75 union {
76 pseudo_t target;
77 pseudo_t cond; /* for branch and switch */
79 union {
80 struct /* entrypoint */ {
81 struct pseudo_list *arg_list;
83 struct /* branch */ {
84 struct basic_block *bb_true, *bb_false;
86 struct /* switch */ {
87 struct multijmp_list *multijmp_list;
89 struct /* phi_node */ {
90 struct pseudo_list *phi_list;
92 struct /* phi source */ {
93 pseudo_t phi_src;
94 struct instruction_list *phi_users;
96 struct /* unops */ {
97 pseudo_t src;
98 struct symbol *orig_type; /* casts */
99 unsigned int offset; /* memops */
101 struct /* binops and sel */ {
102 pseudo_t src1, src2, src3;
104 struct /* slice */ {
105 pseudo_t base;
106 unsigned from, len;
108 struct /* multijump */ {
109 int begin, end;
111 struct /* setval */ {
112 pseudo_t symbol; /* Subtle: same offset as "src" !! */
113 struct expression *val;
115 struct /* call */ {
116 pseudo_t func;
117 struct pseudo_list *arguments;
119 struct /* context */ {
120 int increment;
121 int check;
122 struct expression *context_expr;
124 struct /* asm */ {
125 const char *string;
126 struct asm_rules *asm_rules;
131 enum opcode {
132 OP_BADOP,
134 /* Entry */
135 OP_ENTRY,
137 /* Terminator */
138 OP_TERMINATOR,
139 OP_RET = OP_TERMINATOR,
140 OP_BR,
141 OP_SWITCH,
142 OP_INVOKE,
143 OP_COMPUTEDGOTO,
144 OP_UNWIND,
145 OP_TERMINATOR_END = OP_UNWIND,
147 /* Binary */
148 OP_BINARY,
149 OP_ADD = OP_BINARY,
150 OP_SUB,
151 OP_MULU, OP_MULS,
152 OP_DIVU, OP_DIVS,
153 OP_MODU, OP_MODS,
154 OP_SHL,
155 OP_LSR, OP_ASR,
157 /* Logical */
158 OP_AND,
159 OP_OR,
160 OP_XOR,
161 OP_AND_BOOL,
162 OP_OR_BOOL,
163 OP_BINARY_END = OP_OR_BOOL,
165 /* Binary comparison */
166 OP_BINCMP,
167 OP_SET_EQ = OP_BINCMP,
168 OP_SET_NE,
169 OP_SET_LE,
170 OP_SET_GE,
171 OP_SET_LT,
172 OP_SET_GT,
173 OP_SET_B,
174 OP_SET_A,
175 OP_SET_BE,
176 OP_SET_AE,
177 OP_BINCMP_END = OP_SET_AE,
179 /* Uni */
180 OP_NOT,
181 OP_NEG,
183 /* Select - three input values */
184 OP_SEL,
186 /* Memory */
187 OP_MALLOC,
188 OP_FREE,
189 OP_ALLOCA,
190 OP_LOAD,
191 OP_STORE,
192 OP_SETVAL,
193 OP_SYMADDR,
194 OP_GET_ELEMENT_PTR,
196 /* Other */
197 OP_PHI,
198 OP_PHISOURCE,
199 OP_CAST,
200 OP_SCAST,
201 OP_FPCAST,
202 OP_PTRCAST,
203 OP_INLINED_CALL,
204 OP_CALL,
205 OP_VANEXT,
206 OP_VAARG,
207 OP_SLICE,
208 OP_SNOP,
209 OP_LNOP,
210 OP_NOP,
211 OP_DEATHNOTE,
212 OP_ASM,
214 /* Sparse tagging (line numbers, context, whatever) */
215 OP_CONTEXT,
216 OP_RANGE,
218 /* Needed to translate SSA back to normal form */
219 OP_COPY,
222 struct basic_block_list;
223 struct instruction_list;
225 struct basic_block {
226 struct position pos;
227 unsigned long generation;
228 int context;
229 struct entrypoint *ep;
230 struct basic_block_list *parents; /* sources */
231 struct basic_block_list *children; /* destinations */
232 struct instruction_list *insns; /* Linear list of instructions */
233 struct pseudo_list *needs, *defines;
236 static inline int is_branch_goto(struct instruction *br)
238 return br && br->opcode==OP_BR && (!br->bb_true || !br->bb_false);
241 static inline void add_bb(struct basic_block_list **list, struct basic_block *bb)
243 add_ptr_list(list, bb);
246 static inline void add_instruction(struct instruction_list **list, struct instruction *insn)
248 add_ptr_list(list, insn);
251 static inline void add_multijmp(struct multijmp_list **list, struct multijmp *multijmp)
253 add_ptr_list(list, multijmp);
256 static inline pseudo_t *add_pseudo(struct pseudo_list **list, pseudo_t pseudo)
258 return add_ptr_list(list, pseudo);
261 static inline int remove_pseudo(struct pseudo_list **list, pseudo_t pseudo)
263 return delete_ptr_list_entry((struct ptr_list **)list, pseudo, 0) != 0;
266 static inline int bb_terminated(struct basic_block *bb)
268 struct instruction *insn;
269 if (!bb)
270 return 0;
271 insn = last_instruction(bb->insns);
272 return insn && insn->opcode >= OP_TERMINATOR
273 && insn->opcode <= OP_TERMINATOR_END;
276 static inline int bb_reachable(struct basic_block *bb)
278 return bb != NULL;
281 static inline void add_pseudo_ptr(pseudo_t *ptr, struct pseudo_ptr_list **list)
283 add_ptr_list(list, ptr);
286 static inline void add_pseudo_user_ptr(struct pseudo_user *user, struct pseudo_user_list **list)
288 add_ptr_list(list, user);
291 static inline int has_use_list(pseudo_t p)
293 return (p && p->type != PSEUDO_VOID && p->type != PSEUDO_VAL);
296 static inline struct pseudo_user *alloc_pseudo_user(struct instruction *insn, pseudo_t *pp)
298 struct pseudo_user *user = __alloc_pseudo_user(0);
299 user->userp = pp;
300 user->insn = insn;
301 return user;
304 static inline void use_pseudo(struct instruction *insn, pseudo_t p, pseudo_t *pp)
306 *pp = p;
307 if (has_use_list(p))
308 add_pseudo_user_ptr(alloc_pseudo_user(insn, pp), &p->users);
311 static inline void remove_bb_from_list(struct basic_block_list **list, struct basic_block *entry, int count)
313 delete_ptr_list_entry((struct ptr_list **)list, entry, count);
316 static inline void replace_bb_in_list(struct basic_block_list **list,
317 struct basic_block *old, struct basic_block *new, int count)
319 replace_ptr_list_entry((struct ptr_list **)list, old, new, count);
322 struct entrypoint {
323 struct symbol *name;
324 struct symbol_list *syms;
325 struct pseudo_list *accesses;
326 struct basic_block_list *bbs;
327 struct basic_block *active;
328 struct instruction *entry;
331 extern void insert_select(struct basic_block *bb, struct instruction *br, struct instruction *phi, pseudo_t if_true, pseudo_t if_false);
332 extern void insert_branch(struct basic_block *bb, struct instruction *br, struct basic_block *target);
334 pseudo_t alloc_phi(struct basic_block *source, pseudo_t pseudo, int size);
335 pseudo_t alloc_pseudo(struct instruction *def);
336 pseudo_t value_pseudo(long long val);
338 struct entrypoint *linearize_symbol(struct symbol *sym);
339 int unssa(struct entrypoint *ep);
340 void show_entry(struct entrypoint *ep);
341 const char *show_pseudo(pseudo_t pseudo);
342 void show_bb(struct basic_block *bb);
343 const char *show_instruction(struct instruction *insn);
345 #endif /* LINEARIZE_H */