Warn about sizeof of zero size.
[smatch.git] / linearize.h
blobfb243c5f4fefd3d3eaf8464b8f6949e8eb986b9c
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 struct position pos;
61 union {
62 pseudo_t target;
63 pseudo_t cond; /* for branch and switch */
65 union {
66 struct /* entrypoint */ {
67 struct pseudo_list *arg_list;
69 struct /* branch */ {
70 struct basic_block *bb_true, *bb_false;
72 struct /* switch */ {
73 struct multijmp_list *multijmp_list;
75 struct /* phi_node */ {
76 struct pseudo_list *phi_list;
78 struct /* phi source */ {
79 pseudo_t phi_src;
80 struct instruction_list *phi_users;
82 struct /* unops */ {
83 pseudo_t src;
84 struct symbol *orig_type; /* casts */
85 unsigned int offset; /* memops */
87 struct /* binops and sel */ {
88 pseudo_t src1, src2, src3;
90 struct /* slice */ {
91 pseudo_t base;
92 unsigned from, len;
94 struct /* multijump */ {
95 int begin, end;
97 struct /* setval */ {
98 pseudo_t symbol; /* Subtle: same offset as "src" !! */
99 struct expression *val;
101 struct /* call */ {
102 pseudo_t func;
103 struct pseudo_list *arguments;
105 struct /* context */ {
106 int increment;
108 struct /* asm */ {
109 const char *string;
110 struct asm_rules *asm_rules;
115 enum opcode {
116 OP_BADOP,
118 /* Entry */
119 OP_ENTRY,
121 /* Terminator */
122 OP_TERMINATOR,
123 OP_RET = OP_TERMINATOR,
124 OP_BR,
125 OP_SWITCH,
126 OP_INVOKE,
127 OP_COMPUTEDGOTO,
128 OP_UNWIND,
129 OP_TERMINATOR_END = OP_UNWIND,
131 /* Binary */
132 OP_BINARY,
133 OP_ADD = OP_BINARY,
134 OP_SUB,
135 OP_MULU, OP_MULS,
136 OP_DIVU, OP_DIVS,
137 OP_MODU, OP_MODS,
138 OP_SHL,
139 OP_LSR, OP_ASR,
141 /* Logical */
142 OP_AND,
143 OP_OR,
144 OP_XOR,
145 OP_AND_BOOL,
146 OP_OR_BOOL,
147 OP_BINARY_END = OP_OR_BOOL,
149 /* Binary comparison */
150 OP_BINCMP,
151 OP_SET_EQ = OP_BINCMP,
152 OP_SET_NE,
153 OP_SET_LE,
154 OP_SET_GE,
155 OP_SET_LT,
156 OP_SET_GT,
157 OP_SET_B,
158 OP_SET_A,
159 OP_SET_BE,
160 OP_SET_AE,
161 OP_BINCMP_END = OP_SET_AE,
163 /* Uni */
164 OP_NOT,
165 OP_NEG,
167 /* Select - three input values */
168 OP_SEL,
170 /* Memory */
171 OP_MALLOC,
172 OP_FREE,
173 OP_ALLOCA,
174 OP_LOAD,
175 OP_STORE,
176 OP_SETVAL,
177 OP_SYMADDR,
178 OP_GET_ELEMENT_PTR,
180 /* Other */
181 OP_PHI,
182 OP_PHISOURCE,
183 OP_CAST,
184 OP_SCAST,
185 OP_FPCAST,
186 OP_PTRCAST,
187 OP_CALL,
188 OP_VANEXT,
189 OP_VAARG,
190 OP_SLICE,
191 OP_SNOP,
192 OP_LNOP,
193 OP_NOP,
194 OP_DEATHNOTE,
195 OP_ASM,
197 /* Sparse tagging (line numbers, context, whatever) */
198 OP_CONTEXT,
199 OP_RANGE,
202 struct basic_block_list;
203 struct instruction_list;
205 struct basic_block {
206 struct position pos;
207 unsigned long generation;
208 int context;
209 struct entrypoint *ep;
210 struct basic_block_list *parents; /* sources */
211 struct basic_block_list *children; /* destinations */
212 struct instruction_list *insns; /* Linear list of instructions */
213 struct pseudo_list *needs, *defines;
216 static inline int is_branch_goto(struct instruction *br)
218 return br && br->opcode==OP_BR && (!br->bb_true || !br->bb_false);
221 static inline void add_bb(struct basic_block_list **list, struct basic_block *bb)
223 add_ptr_list(list, bb);
226 static inline void add_instruction(struct instruction_list **list, struct instruction *insn)
228 add_ptr_list(list, insn);
231 static inline void add_multijmp(struct multijmp_list **list, struct multijmp *multijmp)
233 add_ptr_list(list, multijmp);
236 static inline void *add_pseudo(struct pseudo_list **list, struct pseudo *pseudo)
238 return add_ptr_list(list, pseudo);
241 static inline int remove_pseudo(struct pseudo_list **list, pseudo_t pseudo)
243 return delete_ptr_list_entry((struct ptr_list **)list, pseudo, 0) != 0;
246 static inline int bb_terminated(struct basic_block *bb)
248 struct instruction *insn;
249 if (!bb)
250 return 0;
251 insn = last_instruction(bb->insns);
252 return insn && insn->opcode >= OP_RET && insn->opcode <= OP_UNWIND;
255 static inline int bb_reachable(struct basic_block *bb)
257 return bb != NULL;
260 static inline void add_pseudo_ptr(pseudo_t *ptr, struct pseudo_ptr_list **list)
262 add_ptr_list(list, ptr);
265 static inline int has_use_list(pseudo_t p)
267 return (p && p->type != PSEUDO_VOID && p->type != PSEUDO_VAL);
270 static inline void use_pseudo(pseudo_t p, pseudo_t *pp)
272 *pp = p;
273 if (has_use_list(p))
274 add_pseudo_ptr(pp, &p->users);
277 static inline void remove_bb_from_list(struct basic_block_list **list, struct basic_block *entry, int count)
279 delete_ptr_list_entry((struct ptr_list **)list, entry, count);
282 static inline void replace_bb_in_list(struct basic_block_list **list,
283 struct basic_block *old, struct basic_block *new, int count)
285 replace_ptr_list_entry((struct ptr_list **)list, old, new, count);
288 struct entrypoint {
289 struct symbol *name;
290 struct symbol_list *syms;
291 struct symbol_list *accesses;
292 struct basic_block_list *bbs;
293 struct basic_block *active;
294 struct instruction *entry;
297 extern void insert_select(struct basic_block *bb, struct instruction *br, struct instruction *phi, pseudo_t true, pseudo_t false);
298 extern void insert_branch(struct basic_block *bb, struct instruction *br, struct basic_block *target);
300 pseudo_t alloc_phi(struct basic_block *source, pseudo_t pseudo, int size);
301 pseudo_t alloc_pseudo(struct instruction *def);
302 pseudo_t value_pseudo(long long val);
304 struct entrypoint *linearize_symbol(struct symbol *sym);
305 void show_entry(struct entrypoint *ep);
306 const char *show_pseudo(pseudo_t pseudo);
307 void show_bb(struct basic_block *bb);
308 const char *show_instruction(struct instruction *insn);
310 #endif /* LINEARIZE_H */