Fix typos in comments
[smatch.git] / linearize.h
blob8db8e82de9f2c5b385237cb2fa81b1a99e183069
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_PTR_LIST(asm_constraint_list, struct asm_constraint);
60 struct asm_rules {
61 struct asm_constraint_list *inputs;
62 struct asm_constraint_list *outputs;
63 struct asm_constraint_list *clobbers;
66 struct instruction {
67 unsigned opcode:8,
68 size:24;
69 struct basic_block *bb;
70 struct position pos;
71 union {
72 pseudo_t target;
73 pseudo_t cond; /* for branch and switch */
75 union {
76 struct /* entrypoint */ {
77 struct pseudo_list *arg_list;
79 struct /* branch */ {
80 struct basic_block *bb_true, *bb_false;
82 struct /* switch */ {
83 struct multijmp_list *multijmp_list;
85 struct /* phi_node */ {
86 struct pseudo_list *phi_list;
88 struct /* phi source */ {
89 pseudo_t phi_src;
90 struct instruction_list *phi_users;
92 struct /* unops */ {
93 pseudo_t src;
94 struct symbol *orig_type; /* casts */
95 unsigned int offset; /* memops */
97 struct /* binops and sel */ {
98 pseudo_t src1, src2, src3;
100 struct /* slice */ {
101 pseudo_t base;
102 unsigned from, len;
104 struct /* multijump */ {
105 int begin, end;
107 struct /* setval */ {
108 pseudo_t symbol; /* Subtle: same offset as "src" !! */
109 struct expression *val;
111 struct /* call */ {
112 pseudo_t func;
113 struct pseudo_list *arguments;
115 struct /* context */ {
116 int increment;
117 int check;
118 struct expression *context_expr;
120 struct /* asm */ {
121 const char *string;
122 struct asm_rules *asm_rules;
127 enum opcode {
128 OP_BADOP,
130 /* Entry */
131 OP_ENTRY,
133 /* Terminator */
134 OP_TERMINATOR,
135 OP_RET = OP_TERMINATOR,
136 OP_BR,
137 OP_SWITCH,
138 OP_INVOKE,
139 OP_COMPUTEDGOTO,
140 OP_UNWIND,
141 OP_TERMINATOR_END = OP_UNWIND,
143 /* Binary */
144 OP_BINARY,
145 OP_ADD = OP_BINARY,
146 OP_SUB,
147 OP_MULU, OP_MULS,
148 OP_DIVU, OP_DIVS,
149 OP_MODU, OP_MODS,
150 OP_SHL,
151 OP_LSR, OP_ASR,
153 /* Logical */
154 OP_AND,
155 OP_OR,
156 OP_XOR,
157 OP_AND_BOOL,
158 OP_OR_BOOL,
159 OP_BINARY_END = OP_OR_BOOL,
161 /* Binary comparison */
162 OP_BINCMP,
163 OP_SET_EQ = OP_BINCMP,
164 OP_SET_NE,
165 OP_SET_LE,
166 OP_SET_GE,
167 OP_SET_LT,
168 OP_SET_GT,
169 OP_SET_B,
170 OP_SET_A,
171 OP_SET_BE,
172 OP_SET_AE,
173 OP_BINCMP_END = OP_SET_AE,
175 /* Uni */
176 OP_NOT,
177 OP_NEG,
179 /* Select - three input values */
180 OP_SEL,
182 /* Memory */
183 OP_MALLOC,
184 OP_FREE,
185 OP_ALLOCA,
186 OP_LOAD,
187 OP_STORE,
188 OP_SETVAL,
189 OP_SYMADDR,
190 OP_GET_ELEMENT_PTR,
192 /* Other */
193 OP_PHI,
194 OP_PHISOURCE,
195 OP_CAST,
196 OP_SCAST,
197 OP_FPCAST,
198 OP_PTRCAST,
199 OP_INLINED_CALL,
200 OP_CALL,
201 OP_VANEXT,
202 OP_VAARG,
203 OP_SLICE,
204 OP_SNOP,
205 OP_LNOP,
206 OP_NOP,
207 OP_DEATHNOTE,
208 OP_ASM,
210 /* Sparse tagging (line numbers, context, whatever) */
211 OP_CONTEXT,
212 OP_RANGE,
214 /* Needed to translate SSA back to normal form */
215 OP_COPY,
218 struct basic_block_list;
219 struct instruction_list;
221 struct basic_block {
222 struct position pos;
223 unsigned long generation;
224 int context;
225 struct entrypoint *ep;
226 struct basic_block_list *parents; /* sources */
227 struct basic_block_list *children; /* destinations */
228 struct instruction_list *insns; /* Linear list of instructions */
229 struct pseudo_list *needs, *defines;
232 static inline int is_branch_goto(struct instruction *br)
234 return br && br->opcode==OP_BR && (!br->bb_true || !br->bb_false);
237 static inline void add_bb(struct basic_block_list **list, struct basic_block *bb)
239 add_ptr_list(list, bb);
242 static inline void add_instruction(struct instruction_list **list, struct instruction *insn)
244 add_ptr_list(list, insn);
247 static inline void add_multijmp(struct multijmp_list **list, struct multijmp *multijmp)
249 add_ptr_list(list, multijmp);
252 static inline pseudo_t *add_pseudo(struct pseudo_list **list, pseudo_t pseudo)
254 return add_ptr_list(list, pseudo);
257 static inline int remove_pseudo(struct pseudo_list **list, pseudo_t pseudo)
259 return delete_ptr_list_entry((struct ptr_list **)list, pseudo, 0) != 0;
262 static inline int bb_terminated(struct basic_block *bb)
264 struct instruction *insn;
265 if (!bb)
266 return 0;
267 insn = last_instruction(bb->insns);
268 return insn && insn->opcode >= OP_TERMINATOR
269 && insn->opcode <= OP_TERMINATOR_END;
272 static inline int bb_reachable(struct basic_block *bb)
274 return bb != NULL;
277 static inline void add_pseudo_ptr(pseudo_t *ptr, struct pseudo_ptr_list **list)
279 add_ptr_list(list, ptr);
282 static inline void add_pseudo_user_ptr(struct pseudo_user *user, struct pseudo_user_list **list)
284 add_ptr_list(list, user);
287 static inline int has_use_list(pseudo_t p)
289 return (p && p->type != PSEUDO_VOID && p->type != PSEUDO_VAL);
292 static inline struct pseudo_user *alloc_pseudo_user(struct instruction *insn, pseudo_t *pp)
294 struct pseudo_user *user = __alloc_pseudo_user(0);
295 user->userp = pp;
296 user->insn = insn;
297 return user;
300 static inline void use_pseudo(struct instruction *insn, pseudo_t p, pseudo_t *pp)
302 *pp = p;
303 if (has_use_list(p))
304 add_pseudo_user_ptr(alloc_pseudo_user(insn, pp), &p->users);
307 static inline void remove_bb_from_list(struct basic_block_list **list, struct basic_block *entry, int count)
309 delete_ptr_list_entry((struct ptr_list **)list, entry, count);
312 static inline void replace_bb_in_list(struct basic_block_list **list,
313 struct basic_block *old, struct basic_block *new, int count)
315 replace_ptr_list_entry((struct ptr_list **)list, old, new, count);
318 struct entrypoint {
319 struct symbol *name;
320 struct symbol_list *syms;
321 struct pseudo_list *accesses;
322 struct basic_block_list *bbs;
323 struct basic_block *active;
324 struct instruction *entry;
327 extern void insert_select(struct basic_block *bb, struct instruction *br, struct instruction *phi, pseudo_t true, pseudo_t false);
328 extern void insert_branch(struct basic_block *bb, struct instruction *br, struct basic_block *target);
330 pseudo_t alloc_phi(struct basic_block *source, pseudo_t pseudo, int size);
331 pseudo_t alloc_pseudo(struct instruction *def);
332 pseudo_t value_pseudo(long long val);
334 struct entrypoint *linearize_symbol(struct symbol *sym);
335 int unssa(struct entrypoint *ep);
336 void show_entry(struct entrypoint *ep);
337 const char *show_pseudo(pseudo_t pseudo);
338 void show_bb(struct basic_block *bb);
339 const char *show_instruction(struct instruction *insn);
341 #endif /* LINEARIZE_H */