Print this page
11972 resync smatch
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/linearize.c
+++ new/usr/src/tools/smatch/src/linearize.c
1 1 /*
2 2 * Linearize - walk the statement tree (but _not_ the expressions)
3 3 * to generate a linear version of it and the basic blocks.
4 4 *
5 5 * NOTE! We're not interested in the actual sub-expressions yet,
6 6 * even though they can generate conditional branches and
7 7 * subroutine calls. That's all "local" behaviour.
8 8 *
9 9 * Copyright (C) 2004 Linus Torvalds
10 10 * Copyright (C) 2004 Christopher Li
11 11 */
↓ open down ↓ |
11 lines elided |
↑ open up ↑ |
12 12
13 13 #include <string.h>
14 14 #include <stdarg.h>
15 15 #include <stdlib.h>
16 16 #include <stdio.h>
17 17 #include <assert.h>
18 18
19 19 #include "parse.h"
20 20 #include "expression.h"
21 21 #include "linearize.h"
22 +#include "optimize.h"
22 23 #include "flow.h"
23 24 #include "target.h"
24 25
25 -pseudo_t linearize_statement(struct entrypoint *ep, struct statement *stmt);
26 -pseudo_t linearize_expression(struct entrypoint *ep, struct expression *expr);
26 +static pseudo_t linearize_statement(struct entrypoint *ep, struct statement *stmt);
27 +static pseudo_t linearize_expression(struct entrypoint *ep, struct expression *expr);
27 28
29 +static pseudo_t add_cast(struct entrypoint *ep, struct symbol *to, struct symbol *from, int op, pseudo_t src);
28 30 static pseudo_t add_binary_op(struct entrypoint *ep, struct symbol *ctype, int op, pseudo_t left, pseudo_t right);
29 31 static pseudo_t add_setval(struct entrypoint *ep, struct symbol *ctype, struct expression *val);
30 32 static pseudo_t linearize_one_symbol(struct entrypoint *ep, struct symbol *sym);
31 33
32 34 struct access_data;
33 35 static pseudo_t add_load(struct entrypoint *ep, struct access_data *);
34 36 static pseudo_t linearize_initializer(struct entrypoint *ep, struct expression *initializer, struct access_data *);
35 37 static pseudo_t cast_pseudo(struct entrypoint *ep, pseudo_t src, struct symbol *from, struct symbol *to);
36 38
37 39 struct pseudo void_pseudo = {};
38 40
39 41 static struct position current_pos;
40 42
41 43 ALLOCATOR(pseudo_user, "pseudo_user");
42 44
43 45 static struct instruction *alloc_instruction(int opcode, int size)
44 46 {
45 47 struct instruction * insn = __alloc_instruction(0);
46 48 insn->opcode = opcode;
47 49 insn->size = size;
48 50 insn->pos = current_pos;
49 51 return insn;
50 52 }
51 53
52 54 static inline int type_size(struct symbol *type)
53 55 {
54 56 return type ? type->bit_size > 0 ? type->bit_size : 0 : 0;
55 57 }
56 58
57 59 static struct instruction *alloc_typed_instruction(int opcode, struct symbol *type)
58 60 {
59 61 struct instruction *insn = alloc_instruction(opcode, type_size(type));
60 62 insn->type = type;
61 63 return insn;
62 64 }
↓ open down ↓ |
25 lines elided |
↑ open up ↑ |
63 65
64 66 static struct entrypoint *alloc_entrypoint(void)
65 67 {
66 68 return __alloc_entrypoint(0);
67 69 }
68 70
69 71 static struct basic_block *alloc_basic_block(struct entrypoint *ep, struct position pos)
70 72 {
71 73 static int nr;
72 74 struct basic_block *bb = __alloc_basic_block(0);
73 - bb->context = -1;
74 75 bb->pos = pos;
75 76 bb->ep = ep;
76 77 bb->nr = nr++;
77 78 return bb;
78 79 }
79 80
80 -static struct multijmp *alloc_multijmp(struct basic_block *target, int begin, int end)
81 +static struct multijmp *alloc_multijmp(struct basic_block *target, long long begin, long long end)
81 82 {
82 83 struct multijmp *multijmp = __alloc_multijmp(0);
83 84 multijmp->target = target;
84 85 multijmp->begin = begin;
85 86 multijmp->end = end;
86 87 return multijmp;
87 88 }
88 89
89 -static inline int regno(pseudo_t n)
90 +const char *show_label(struct basic_block *bb)
90 91 {
91 - int retval = -1;
92 - if (n && n->type == PSEUDO_REG)
93 - retval = n->nr;
94 - return retval;
92 + static int n;
93 + static char buffer[4][16];
94 + char *buf = buffer[3 & ++n];
95 +
96 + if (!bb)
97 + return ".L???";
98 + snprintf(buf, 64, ".L%u", bb->nr);
99 + return buf;
95 100 }
96 101
97 102 const char *show_pseudo(pseudo_t pseudo)
98 103 {
99 104 static int n;
100 105 static char buffer[4][64];
101 106 char *buf;
102 107 int i;
103 108
104 109 if (!pseudo)
105 110 return "no pseudo";
106 111 if (pseudo == VOID)
107 112 return "VOID";
108 113 buf = buffer[3 & ++n];
109 114 switch(pseudo->type) {
110 115 case PSEUDO_SYM: {
111 116 struct symbol *sym = pseudo->sym;
112 117 struct expression *expr;
113 118
119 + if (!sym) {
120 + snprintf(buf, 64, "<bad symbol>");
121 + break;
122 + }
114 123 if (sym->bb_target) {
115 - snprintf(buf, 64, ".L%u", sym->bb_target->nr);
124 + snprintf(buf, 64, "%s", show_label(sym->bb_target));
116 125 break;
117 126 }
118 127 if (sym->ident) {
119 128 snprintf(buf, 64, "%s", show_ident(sym->ident));
120 129 break;
121 130 }
122 131 expr = sym->initializer;
123 - snprintf(buf, 64, "<anon symbol:%p>", sym);
132 + snprintf(buf, 64, "<anon symbol:%p>", verbose ? sym : NULL);
124 133 if (expr) {
125 134 switch (expr->type) {
126 135 case EXPR_VALUE:
127 136 snprintf(buf, 64, "<symbol value: %lld>", expr->value);
128 137 break;
129 138 case EXPR_STRING:
130 139 return show_string(expr->string);
131 140 default:
132 141 break;
133 142 }
134 143 }
135 144 break;
136 145 }
137 146 case PSEUDO_REG:
138 147 i = snprintf(buf, 64, "%%r%d", pseudo->nr);
139 148 if (pseudo->ident)
140 149 sprintf(buf+i, "(%s)", show_ident(pseudo->ident));
141 150 break;
142 151 case PSEUDO_VAL: {
143 152 long long value = pseudo->value;
144 153 if (value > 1000 || value < -1000)
145 154 snprintf(buf, 64, "$%#llx", value);
146 155 else
147 156 snprintf(buf, 64, "$%lld", value);
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
148 157 break;
149 158 }
150 159 case PSEUDO_ARG:
151 160 snprintf(buf, 64, "%%arg%d", pseudo->nr);
152 161 break;
153 162 case PSEUDO_PHI:
154 163 i = snprintf(buf, 64, "%%phi%d", pseudo->nr);
155 164 if (pseudo->ident)
156 165 sprintf(buf+i, "(%s)", show_ident(pseudo->ident));
157 166 break;
167 + case PSEUDO_UNDEF:
168 + return "UNDEF";
158 169 default:
159 170 snprintf(buf, 64, "<bad pseudo type %d>", pseudo->type);
160 171 }
161 172 return buf;
162 173 }
163 174
164 175 static const char *opcodes[] = {
165 176 [OP_BADOP] = "bad_op",
166 177
167 178 /* Fn entrypoint */
168 179 [OP_ENTRY] = "<entry-point>",
169 180
170 181 /* Terminator */
171 182 [OP_RET] = "ret",
172 183 [OP_BR] = "br",
173 184 [OP_CBR] = "cbr",
174 185 [OP_SWITCH] = "switch",
175 - [OP_INVOKE] = "invoke",
176 186 [OP_COMPUTEDGOTO] = "jmp *",
177 - [OP_UNWIND] = "unwind",
178 187
179 188 /* Binary */
180 189 [OP_ADD] = "add",
181 190 [OP_SUB] = "sub",
182 - [OP_MULU] = "mulu",
183 - [OP_MULS] = "muls",
191 + [OP_MUL] = "mul",
184 192 [OP_DIVU] = "divu",
185 193 [OP_DIVS] = "divs",
186 194 [OP_MODU] = "modu",
187 195 [OP_MODS] = "mods",
188 196 [OP_SHL] = "shl",
189 197 [OP_LSR] = "lsr",
190 198 [OP_ASR] = "asr",
191 199
200 + /* Floating-point Binary */
201 + [OP_FADD] = "fadd",
202 + [OP_FSUB] = "fsub",
203 + [OP_FMUL] = "fmul",
204 + [OP_FDIV] = "fdiv",
205 +
192 206 /* Logical */
193 207 [OP_AND] = "and",
194 208 [OP_OR] = "or",
195 209 [OP_XOR] = "xor",
196 - [OP_AND_BOOL] = "and-bool",
197 - [OP_OR_BOOL] = "or-bool",
198 210
199 211 /* Binary comparison */
200 212 [OP_SET_EQ] = "seteq",
201 213 [OP_SET_NE] = "setne",
202 214 [OP_SET_LE] = "setle",
203 215 [OP_SET_GE] = "setge",
204 216 [OP_SET_LT] = "setlt",
205 217 [OP_SET_GT] = "setgt",
206 218 [OP_SET_B] = "setb",
207 219 [OP_SET_A] = "seta",
208 220 [OP_SET_BE] = "setbe",
209 221 [OP_SET_AE] = "setae",
210 222
223 + /* floating-point comparison */
224 + [OP_FCMP_ORD] = "fcmpord",
225 + [OP_FCMP_OEQ] = "fcmpoeq",
226 + [OP_FCMP_ONE] = "fcmpone",
227 + [OP_FCMP_OLE] = "fcmpole",
228 + [OP_FCMP_OGE] = "fcmpoge",
229 + [OP_FCMP_OLT] = "fcmpolt",
230 + [OP_FCMP_OGT] = "fcmpogt",
231 + [OP_FCMP_UEQ] = "fcmpueq",
232 + [OP_FCMP_UNE] = "fcmpune",
233 + [OP_FCMP_ULE] = "fcmpule",
234 + [OP_FCMP_UGE] = "fcmpuge",
235 + [OP_FCMP_ULT] = "fcmpult",
236 + [OP_FCMP_UGT] = "fcmpugt",
237 + [OP_FCMP_UNO] = "fcmpuno",
238 +
211 239 /* Uni */
212 240 [OP_NOT] = "not",
213 241 [OP_NEG] = "neg",
242 + [OP_FNEG] = "fneg",
214 243
215 244 /* Special three-input */
216 245 [OP_SEL] = "select",
217 246
218 247 /* Memory */
219 - [OP_MALLOC] = "malloc",
220 - [OP_FREE] = "free",
221 - [OP_ALLOCA] = "alloca",
222 248 [OP_LOAD] = "load",
223 249 [OP_STORE] = "store",
224 250 [OP_SETVAL] = "set",
251 + [OP_SETFVAL] = "setfval",
225 252 [OP_SYMADDR] = "symaddr",
226 - [OP_GET_ELEMENT_PTR] = "getelem",
227 253
228 254 /* Other */
229 255 [OP_PHI] = "phi",
230 256 [OP_PHISOURCE] = "phisrc",
231 - [OP_CAST] = "cast",
232 - [OP_SCAST] = "scast",
233 - [OP_FPCAST] = "fpcast",
257 + [OP_SEXT] = "sext",
258 + [OP_ZEXT] = "zext",
259 + [OP_TRUNC] = "trunc",
260 + [OP_FCVTU] = "fcvtu",
261 + [OP_FCVTS] = "fcvts",
262 + [OP_UCVTF] = "ucvtf",
263 + [OP_SCVTF] = "scvtf",
264 + [OP_FCVTF] = "fcvtf",
265 + [OP_UTPTR] = "utptr",
266 + [OP_PTRTU] = "ptrtu",
234 267 [OP_PTRCAST] = "ptrcast",
235 268 [OP_INLINED_CALL] = "# call",
236 269 [OP_CALL] = "call",
237 - [OP_VANEXT] = "va_next",
238 - [OP_VAARG] = "va_arg",
239 270 [OP_SLICE] = "slice",
240 - [OP_SNOP] = "snop",
241 - [OP_LNOP] = "lnop",
242 271 [OP_NOP] = "nop",
243 272 [OP_DEATHNOTE] = "dead",
244 273 [OP_ASM] = "asm",
245 274
246 275 /* Sparse tagging (line numbers, context, whatever) */
247 276 [OP_CONTEXT] = "context",
248 277 [OP_RANGE] = "range-check",
249 278
250 279 [OP_COPY] = "copy",
251 280 };
252 281
253 282 static char *show_asm_constraints(char *buf, const char *sep, struct asm_constraint_list *list)
254 283 {
255 284 struct asm_constraint *entry;
256 285
257 286 FOR_EACH_PTR(list, entry) {
258 287 buf += sprintf(buf, "%s\"%s\"", sep, entry->constraint);
259 288 if (entry->pseudo)
260 289 buf += sprintf(buf, " (%s)", show_pseudo(entry->pseudo));
261 290 if (entry->ident)
262 291 buf += sprintf(buf, " [%s]", show_ident(entry->ident));
263 292 sep = ", ";
264 293 } END_FOR_EACH_PTR(entry);
265 294 return buf;
266 295 }
267 296
268 297 static char *show_asm(char *buf, struct instruction *insn)
269 298 {
270 299 struct asm_rules *rules = insn->asm_rules;
271 300
272 301 buf += sprintf(buf, "\"%s\"", insn->string);
273 302 buf = show_asm_constraints(buf, "\n\t\tout: ", rules->outputs);
274 303 buf = show_asm_constraints(buf, "\n\t\tin: ", rules->inputs);
275 304 buf = show_asm_constraints(buf, "\n\t\tclobber: ", rules->clobbers);
276 305 return buf;
277 306 }
278 307
279 308 const char *show_instruction(struct instruction *insn)
280 309 {
281 310 int opcode = insn->opcode;
282 311 static char buffer[4096];
283 312 char *buf;
284 313
285 314 buf = buffer;
286 315 if (!insn->bb)
287 316 buf += sprintf(buf, "# ");
288 317
289 318 if (opcode < ARRAY_SIZE(opcodes)) {
290 319 const char *op = opcodes[opcode];
291 320 if (!op)
292 321 buf += sprintf(buf, "opcode:%d", opcode);
293 322 else
294 323 buf += sprintf(buf, "%s", op);
295 324 if (insn->size)
296 325 buf += sprintf(buf, ".%d", insn->size);
297 326 memset(buf, ' ', 20);
298 327 buf++;
299 328 }
↓ open down ↓ |
48 lines elided |
↑ open up ↑ |
300 329
301 330 if (buf < buffer + 12)
302 331 buf = buffer + 12;
303 332 switch (opcode) {
304 333 case OP_RET:
305 334 if (insn->src && insn->src != VOID)
306 335 buf += sprintf(buf, "%s", show_pseudo(insn->src));
307 336 break;
308 337
309 338 case OP_CBR:
310 - buf += sprintf(buf, "%s, .L%u, .L%u", show_pseudo(insn->cond), insn->bb_true->nr, insn->bb_false->nr);
339 + buf += sprintf(buf, "%s, %s, %s", show_pseudo(insn->cond), show_label(insn->bb_true), show_label(insn->bb_false));
311 340 break;
312 341
313 342 case OP_BR:
314 - buf += sprintf(buf, ".L%u", insn->bb_true->nr);
343 + buf += sprintf(buf, "%s", show_label(insn->bb_true));
315 344 break;
316 345
317 - case OP_SYMADDR: {
318 - struct symbol *sym = insn->symbol->sym;
319 - buf += sprintf(buf, "%s <- ", show_pseudo(insn->target));
320 -
321 - if (!insn->bb && !sym)
322 - break;
323 - if (sym->bb_target) {
324 - buf += sprintf(buf, ".L%u", sym->bb_target->nr);
325 - break;
326 - }
327 - if (sym->ident) {
328 - buf += sprintf(buf, "%s", show_ident(sym->ident));
329 - break;
330 - }
331 - buf += sprintf(buf, "<anon symbol:%p>", sym);
332 - break;
333 - }
334 -
335 346 case OP_SETVAL: {
336 347 struct expression *expr = insn->val;
337 - struct symbol *sym;
338 348 buf += sprintf(buf, "%s <- ", show_pseudo(insn->target));
339 349
340 350 if (!expr) {
341 351 buf += sprintf(buf, "%s", "<none>");
342 352 break;
343 353 }
344 354
345 355 switch (expr->type) {
346 356 case EXPR_VALUE:
347 357 buf += sprintf(buf, "%lld", expr->value);
348 358 break;
349 359 case EXPR_FVALUE:
350 - buf += sprintf(buf, "%Lf", expr->fvalue);
360 + buf += sprintf(buf, "%Le", expr->fvalue);
351 361 break;
352 362 case EXPR_STRING:
353 363 buf += sprintf(buf, "%.40s", show_string(expr->string));
354 364 break;
355 365 case EXPR_SYMBOL:
356 366 buf += sprintf(buf, "%s", show_ident(expr->symbol->ident));
357 367 break;
358 368 case EXPR_LABEL:
359 - sym = expr->symbol;
360 - if (sym->bb_target)
361 - buf += sprintf(buf, ".L%u", sym->bb_target->nr);
369 + buf += sprintf(buf, "%s", show_label(expr->symbol->bb_target));
362 370 break;
363 371 default:
364 372 buf += sprintf(buf, "SETVAL EXPR TYPE %d", expr->type);
365 373 }
366 374 break;
367 375 }
376 + case OP_SETFVAL:
377 + buf += sprintf(buf, "%s <- ", show_pseudo(insn->target));
378 + buf += sprintf(buf, "%Le", insn->fvalue);
379 + break;
380 +
368 381 case OP_SWITCH: {
369 382 struct multijmp *jmp;
370 383 buf += sprintf(buf, "%s", show_pseudo(insn->cond));
371 384 FOR_EACH_PTR(insn->multijmp_list, jmp) {
372 385 if (jmp->begin == jmp->end)
373 - buf += sprintf(buf, ", %d -> .L%u", jmp->begin, jmp->target->nr);
386 + buf += sprintf(buf, ", %lld -> %s", jmp->begin, show_label(jmp->target));
374 387 else if (jmp->begin < jmp->end)
375 - buf += sprintf(buf, ", %d ... %d -> .L%u", jmp->begin, jmp->end, jmp->target->nr);
388 + buf += sprintf(buf, ", %lld ... %lld -> %s", jmp->begin, jmp->end, show_label(jmp->target));
376 389 else
377 - buf += sprintf(buf, ", default -> .L%u", jmp->target->nr);
390 + buf += sprintf(buf, ", default -> %s", show_label(jmp->target));
378 391 } END_FOR_EACH_PTR(jmp);
379 392 break;
380 393 }
381 394 case OP_COMPUTEDGOTO: {
382 395 struct multijmp *jmp;
383 - buf += sprintf(buf, "%s", show_pseudo(insn->target));
396 + buf += sprintf(buf, "%s", show_pseudo(insn->src));
384 397 FOR_EACH_PTR(insn->multijmp_list, jmp) {
385 - buf += sprintf(buf, ", .L%u", jmp->target->nr);
398 + buf += sprintf(buf, ", %s", show_label(jmp->target));
386 399 } END_FOR_EACH_PTR(jmp);
387 400 break;
388 401 }
389 402
390 403 case OP_PHISOURCE: {
391 404 struct instruction *phi;
392 405 buf += sprintf(buf, "%s <- %s ", show_pseudo(insn->target), show_pseudo(insn->phi_src));
393 406 FOR_EACH_PTR(insn->phi_users, phi) {
394 407 buf += sprintf(buf, " (%s)", show_pseudo(phi->target));
395 408 } END_FOR_EACH_PTR(phi);
396 409 break;
397 410 }
398 411
399 412 case OP_PHI: {
400 413 pseudo_t phi;
401 414 const char *s = " <-";
402 415 buf += sprintf(buf, "%s", show_pseudo(insn->target));
403 416 FOR_EACH_PTR(insn->phi_list, phi) {
417 + if (phi == VOID && !verbose)
418 + continue;
404 419 buf += sprintf(buf, "%s %s", s, show_pseudo(phi));
405 420 s = ",";
406 421 } END_FOR_EACH_PTR(phi);
407 422 break;
408 423 }
409 - case OP_LOAD: case OP_LNOP:
424 + case OP_LOAD:
410 425 buf += sprintf(buf, "%s <- %d[%s]", show_pseudo(insn->target), insn->offset, show_pseudo(insn->src));
411 426 break;
412 - case OP_STORE: case OP_SNOP:
427 + case OP_STORE:
413 428 buf += sprintf(buf, "%s -> %d[%s]", show_pseudo(insn->target), insn->offset, show_pseudo(insn->src));
414 429 break;
415 430 case OP_INLINED_CALL:
416 431 case OP_CALL: {
417 432 struct pseudo *arg;
418 433 if (insn->target && insn->target != VOID)
419 434 buf += sprintf(buf, "%s <- ", show_pseudo(insn->target));
420 435 buf += sprintf(buf, "%s", show_pseudo(insn->func));
421 436 FOR_EACH_PTR(insn->arguments, arg) {
422 437 buf += sprintf(buf, ", %s", show_pseudo(arg));
423 438 } END_FOR_EACH_PTR(arg);
424 439 break;
425 440 }
426 - case OP_CAST:
427 - case OP_SCAST:
428 - case OP_FPCAST:
441 + case OP_SEXT: case OP_ZEXT:
442 + case OP_TRUNC:
443 + case OP_FCVTU: case OP_FCVTS:
444 + case OP_UCVTF: case OP_SCVTF:
445 + case OP_FCVTF:
446 + case OP_UTPTR:
447 + case OP_PTRTU:
429 448 case OP_PTRCAST:
430 449 buf += sprintf(buf, "%s <- (%d) %s",
431 450 show_pseudo(insn->target),
432 451 type_size(insn->orig_type),
433 452 show_pseudo(insn->src));
434 453 break;
435 454 case OP_BINARY ... OP_BINARY_END:
455 + case OP_FPCMP ... OP_FPCMP_END:
436 456 case OP_BINCMP ... OP_BINCMP_END:
437 457 buf += sprintf(buf, "%s <- %s, %s", show_pseudo(insn->target), show_pseudo(insn->src1), show_pseudo(insn->src2));
438 458 break;
439 459
440 460 case OP_SEL:
441 461 buf += sprintf(buf, "%s <- %s, %s, %s", show_pseudo(insn->target),
442 462 show_pseudo(insn->src1), show_pseudo(insn->src2), show_pseudo(insn->src3));
443 463 break;
444 464
445 465 case OP_SLICE:
446 466 buf += sprintf(buf, "%s <- %s, %d, %d", show_pseudo(insn->target), show_pseudo(insn->base), insn->from, insn->len);
447 467 break;
448 468
449 469 case OP_NOT: case OP_NEG:
470 + case OP_FNEG:
471 + case OP_SYMADDR:
450 472 buf += sprintf(buf, "%s <- %s", show_pseudo(insn->target), show_pseudo(insn->src1));
451 473 break;
452 474
453 475 case OP_CONTEXT:
454 476 buf += sprintf(buf, "%s%d", insn->check ? "check: " : "", insn->increment);
455 477 break;
456 478 case OP_RANGE:
457 479 buf += sprintf(buf, "%s between %s..%s", show_pseudo(insn->src1), show_pseudo(insn->src2), show_pseudo(insn->src3));
458 480 break;
459 481 case OP_NOP:
460 482 buf += sprintf(buf, "%s <- %s", show_pseudo(insn->target), show_pseudo(insn->src1));
461 483 break;
462 484 case OP_DEATHNOTE:
463 485 buf += sprintf(buf, "%s", show_pseudo(insn->target));
464 486 break;
465 487 case OP_ASM:
466 488 buf = show_asm(buf, insn);
467 489 break;
468 490 case OP_COPY:
469 491 buf += sprintf(buf, "%s <- %s", show_pseudo(insn->target), show_pseudo(insn->src));
470 492 break;
471 493 default:
472 494 break;
473 495 }
474 496
475 497 if (buf >= buffer + sizeof(buffer))
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
476 498 die("instruction buffer overflowed %td\n", buf - buffer);
477 499 do { --buf; } while (*buf == ' ');
478 500 *++buf = 0;
479 501 return buffer;
480 502 }
481 503
482 504 void show_bb(struct basic_block *bb)
483 505 {
484 506 struct instruction *insn;
485 507
486 - printf(".L%u:\n", bb->nr);
508 + printf("%s:\n", show_label(bb));
487 509 if (verbose) {
488 510 pseudo_t needs, defines;
489 511 printf("%s:%d\n", stream_name(bb->pos.stream), bb->pos.line);
490 512
491 513 FOR_EACH_PTR(bb->needs, needs) {
492 514 struct instruction *def = needs->def;
493 515 if (def->opcode != OP_PHI) {
494 - printf(" **uses %s (from .L%u)**\n", show_pseudo(needs), def->bb->nr);
516 + printf(" **uses %s (from %s)**\n", show_pseudo(needs), show_label(def->bb));
495 517 } else {
496 518 pseudo_t phi;
497 519 const char *sep = " ";
498 520 printf(" **uses %s (from", show_pseudo(needs));
499 521 FOR_EACH_PTR(def->phi_list, phi) {
500 522 if (phi == VOID)
501 523 continue;
502 - printf("%s(%s:.L%u)", sep, show_pseudo(phi), phi->def->bb->nr);
524 + printf("%s(%s:%s)", sep, show_pseudo(phi), show_label(phi->def->bb));
503 525 sep = ", ";
504 526 } END_FOR_EACH_PTR(phi);
505 527 printf(")**\n");
506 528 }
507 529 } END_FOR_EACH_PTR(needs);
508 530
509 531 FOR_EACH_PTR(bb->defines, defines) {
510 532 printf(" **defines %s **\n", show_pseudo(defines));
511 533 } END_FOR_EACH_PTR(defines);
512 534
513 535 if (bb->parents) {
514 536 struct basic_block *from;
515 537 FOR_EACH_PTR(bb->parents, from) {
516 - printf(" **from .L%u (%s:%d:%d)**\n", from->nr,
538 + printf(" **from %s (%s:%d:%d)**\n", show_label(from),
517 539 stream_name(from->pos.stream), from->pos.line, from->pos.pos);
518 540 } END_FOR_EACH_PTR(from);
519 541 }
520 542
521 543 if (bb->children) {
522 544 struct basic_block *to;
523 545 FOR_EACH_PTR(bb->children, to) {
524 - printf(" **to .L%u (%s:%d:%d)**\n", to->nr,
546 + printf(" **to %s (%s:%d:%d)**\n", show_label(to),
525 547 stream_name(to->pos.stream), to->pos.line, to->pos.pos);
526 548 } END_FOR_EACH_PTR(to);
527 549 }
528 550 }
529 551
530 552 FOR_EACH_PTR(bb->insns, insn) {
531 553 if (!insn->bb && verbose < 2)
532 554 continue;
533 555 printf("\t%s\n", show_instruction(insn));
534 556 } END_FOR_EACH_PTR(insn);
535 557 if (!bb_terminated(bb))
536 558 printf("\tEND\n");
537 559 }
538 560
539 561 static void show_symbol_usage(pseudo_t pseudo)
540 562 {
541 563 struct pseudo_user *pu;
542 564
543 565 if (pseudo) {
544 566 FOR_EACH_PTR(pseudo->users, pu) {
545 567 printf("\t%s\n", show_instruction(pu->insn));
546 568 } END_FOR_EACH_PTR(pu);
547 569 }
548 570 }
549 571
550 572 void show_entry(struct entrypoint *ep)
551 573 {
552 574 struct symbol *sym;
553 575 struct basic_block *bb;
554 576
555 577 printf("%s:\n", show_ident(ep->name->ident));
556 578
557 579 if (verbose) {
558 580 printf("ep %p: %s\n", ep, show_ident(ep->name->ident));
559 581
560 582 FOR_EACH_PTR(ep->syms, sym) {
561 583 if (!sym->pseudo)
562 584 continue;
563 585 if (!sym->pseudo->users)
564 586 continue;
565 587 printf(" sym: %p %s\n", sym, show_ident(sym->ident));
566 588 if (sym->ctype.modifiers & (MOD_EXTERN | MOD_STATIC | MOD_ADDRESSABLE))
567 589 printf("\texternal visibility\n");
568 590 show_symbol_usage(sym->pseudo);
569 591 } END_FOR_EACH_PTR(sym);
570 592
571 593 printf("\n");
572 594 }
573 595
574 596 FOR_EACH_PTR(ep->bbs, bb) {
575 597 if (!bb)
576 598 continue;
577 599 if (!bb->parents && !bb->children && !bb->insns && verbose < 2)
578 600 continue;
579 601 show_bb(bb);
580 602 printf("\n");
581 603 } END_FOR_EACH_PTR(bb);
582 604
583 605 printf("\n");
584 606 }
585 607
586 608 static void bind_label(struct symbol *label, struct basic_block *bb, struct position pos)
587 609 {
588 610 if (label->bb_target)
589 611 warning(pos, "label '%s' already bound", show_ident(label->ident));
590 612 label->bb_target = bb;
591 613 }
592 614
593 615 static struct basic_block * get_bound_block(struct entrypoint *ep, struct symbol *label)
594 616 {
595 617 struct basic_block *bb = label->bb_target;
596 618
597 619 if (!bb) {
598 620 bb = alloc_basic_block(ep, label->pos);
599 621 label->bb_target = bb;
600 622 }
601 623 return bb;
602 624 }
603 625
604 626 static void finish_block(struct entrypoint *ep)
605 627 {
606 628 struct basic_block *src = ep->active;
607 629 if (bb_reachable(src))
608 630 ep->active = NULL;
609 631 }
610 632
611 633 static void add_goto(struct entrypoint *ep, struct basic_block *dst)
612 634 {
613 635 struct basic_block *src = ep->active;
614 636 if (bb_reachable(src)) {
615 637 struct instruction *br = alloc_instruction(OP_BR, 0);
616 638 br->bb_true = dst;
617 639 add_bb(&dst->parents, src);
618 640 add_bb(&src->children, dst);
619 641 br->bb = src;
620 642 add_instruction(&src->insns, br);
621 643 ep->active = NULL;
622 644 }
623 645 }
624 646
625 647 static void add_one_insn(struct entrypoint *ep, struct instruction *insn)
626 648 {
627 649 struct basic_block *bb = ep->active;
628 650
629 651 if (bb_reachable(bb)) {
630 652 insn->bb = bb;
631 653 add_instruction(&bb->insns, insn);
632 654 }
633 655 }
634 656
635 657 static void set_activeblock(struct entrypoint *ep, struct basic_block *bb)
636 658 {
637 659 if (!bb_terminated(ep->active))
638 660 add_goto(ep, bb);
639 661
640 662 ep->active = bb;
641 663 if (bb_reachable(bb))
642 664 add_bb(&ep->bbs, bb);
643 665 }
644 666
645 667 static void remove_parent(struct basic_block *child, struct basic_block *parent)
646 668 {
647 669 remove_bb_from_list(&child->parents, parent, 1);
648 670 if (!child->parents)
649 671 repeat_phase |= REPEAT_CFG_CLEANUP;
650 672 }
651 673
652 674 /* Change a "switch" or a conditional branch into a branch */
653 675 void insert_branch(struct basic_block *bb, struct instruction *jmp, struct basic_block *target)
654 676 {
655 677 struct instruction *br, *old;
656 678 struct basic_block *child;
657 679
658 680 /* Remove the switch */
659 681 old = delete_last_instruction(&bb->insns);
660 682 assert(old == jmp);
661 683 kill_instruction(old);
662 684
663 685 br = alloc_instruction(OP_BR, 0);
664 686 br->bb = bb;
665 687 br->bb_true = target;
666 688 add_instruction(&bb->insns, br);
667 689
668 690 FOR_EACH_PTR(bb->children, child) {
669 691 if (child == target) {
670 692 target = NULL; /* Trigger just once */
671 693 continue;
672 694 }
673 695 DELETE_CURRENT_PTR(child);
674 696 remove_parent(child, bb);
675 697 } END_FOR_EACH_PTR(child);
676 698 PACK_PTR_LIST(&bb->children);
677 699 }
↓ open down ↓ |
143 lines elided |
↑ open up ↑ |
678 700
679 701
680 702 void insert_select(struct basic_block *bb, struct instruction *br, struct instruction *phi_node, pseudo_t if_true, pseudo_t if_false)
681 703 {
682 704 pseudo_t target;
683 705 struct instruction *select;
684 706
685 707 /* Remove the 'br' */
686 708 delete_last_instruction(&bb->insns);
687 709
688 - select = alloc_instruction(OP_SEL, phi_node->size);
710 + select = alloc_typed_instruction(OP_SEL, phi_node->type);
689 711 select->bb = bb;
690 712
691 713 assert(br->cond);
692 714 use_pseudo(select, br->cond, &select->src1);
693 715
694 716 target = phi_node->target;
695 717 assert(target->def == phi_node);
696 718 select->target = target;
697 719 target->def = select;
698 720
699 721 use_pseudo(select, if_true, &select->src2);
700 722 use_pseudo(select, if_false, &select->src3);
701 723
702 724 add_instruction(&bb->insns, select);
703 725 add_instruction(&bb->insns, br);
704 726 }
705 727
706 728 static inline int bb_empty(struct basic_block *bb)
707 729 {
708 730 return !bb->insns;
709 731 }
710 732
711 733 /* Add a label to the currently active block, return new active block */
712 734 static struct basic_block * add_label(struct entrypoint *ep, struct symbol *label)
713 735 {
714 736 struct basic_block *bb = label->bb_target;
715 737
716 738 if (bb) {
717 739 set_activeblock(ep, bb);
718 740 return bb;
↓ open down ↓ |
20 lines elided |
↑ open up ↑ |
719 741 }
720 742 bb = ep->active;
721 743 if (!bb_reachable(bb) || !bb_empty(bb)) {
722 744 bb = alloc_basic_block(ep, label->pos);
723 745 set_activeblock(ep, bb);
724 746 }
725 747 label->bb_target = bb;
726 748 return bb;
727 749 }
728 750
729 -static void add_branch(struct entrypoint *ep, struct expression *expr, pseudo_t cond, struct basic_block *bb_true, struct basic_block *bb_false)
751 +static void add_branch(struct entrypoint *ep, pseudo_t cond, struct basic_block *bb_true, struct basic_block *bb_false)
730 752 {
731 753 struct basic_block *bb = ep->active;
732 754 struct instruction *br;
733 755
734 756 if (bb_reachable(bb)) {
735 757 br = alloc_instruction(OP_CBR, 0);
736 758 use_pseudo(br, cond, &br->cond);
737 759 br->bb_true = bb_true;
738 760 br->bb_false = bb_false;
739 761 add_bb(&bb_true->parents, bb);
740 762 add_bb(&bb_false->parents, bb);
741 763 add_bb(&bb->children, bb_true);
742 764 add_bb(&bb->children, bb_false);
743 765 add_one_insn(ep, br);
744 766 }
745 767 }
746 768
747 -/* Dummy pseudo allocator */
748 769 pseudo_t alloc_pseudo(struct instruction *def)
749 770 {
750 771 static int nr = 0;
751 772 struct pseudo * pseudo = __alloc_pseudo(0);
752 773 pseudo->type = PSEUDO_REG;
753 774 pseudo->nr = ++nr;
754 775 pseudo->def = def;
755 776 return pseudo;
756 777 }
757 778
758 -static void clear_symbol_pseudos(struct entrypoint *ep)
759 -{
760 - pseudo_t pseudo;
761 -
762 - FOR_EACH_PTR(ep->accesses, pseudo) {
763 - pseudo->sym->pseudo = NULL;
764 - } END_FOR_EACH_PTR(pseudo);
765 -}
766 -
767 779 static pseudo_t symbol_pseudo(struct entrypoint *ep, struct symbol *sym)
768 780 {
769 781 pseudo_t pseudo;
770 782
771 783 if (!sym)
772 784 return VOID;
773 785
774 786 pseudo = sym->pseudo;
775 787 if (!pseudo) {
776 788 pseudo = __alloc_pseudo(0);
777 789 pseudo->nr = -1;
778 790 pseudo->type = PSEUDO_SYM;
779 791 pseudo->sym = sym;
780 792 pseudo->ident = sym->ident;
781 793 sym->pseudo = pseudo;
782 794 add_pseudo(&ep->accesses, pseudo);
783 795 }
784 - /* Symbol pseudos have neither nr, usage nor def */
796 + /* Symbol pseudos have neither nr nor def */
785 797 return pseudo;
786 798 }
787 799
788 -pseudo_t value_pseudo(struct symbol *type, long long val)
800 +pseudo_t value_pseudo(long long val)
789 801 {
790 802 #define MAX_VAL_HASH 64
791 803 static struct pseudo_list *prev[MAX_VAL_HASH];
792 804 int hash = val & (MAX_VAL_HASH-1);
793 805 struct pseudo_list **list = prev + hash;
794 - int size = type ? type->bit_size : value_size(val);
795 806 pseudo_t pseudo;
796 807
797 -
798 808 FOR_EACH_PTR(*list, pseudo) {
799 - if (pseudo->value == val && pseudo->size == size)
809 + if (pseudo->value == val)
800 810 return pseudo;
801 811 } END_FOR_EACH_PTR(pseudo);
802 812
803 813 pseudo = __alloc_pseudo(0);
804 814 pseudo->type = PSEUDO_VAL;
805 815 pseudo->value = val;
806 - pseudo->size = size;
807 816 add_pseudo(list, pseudo);
808 817
809 818 /* Value pseudos have neither nr, usage nor def */
810 819 return pseudo;
811 820 }
812 821
822 +pseudo_t undef_pseudo(void)
823 +{
824 + pseudo_t pseudo = __alloc_pseudo(0);
825 + pseudo->type = PSEUDO_UNDEF;
826 + return pseudo;
827 +}
828 +
813 829 static pseudo_t argument_pseudo(struct entrypoint *ep, int nr)
814 830 {
815 831 pseudo_t pseudo = __alloc_pseudo(0);
816 832 struct instruction *entry = ep->entry;
817 833
818 834 pseudo->type = PSEUDO_ARG;
819 835 pseudo->nr = nr;
820 836 pseudo->def = entry;
821 837 add_pseudo(&entry->arg_list, pseudo);
822 838
823 839 /* Argument pseudos have neither usage nor def */
824 840 return pseudo;
825 841 }
826 842
827 -pseudo_t alloc_phi(struct basic_block *source, pseudo_t pseudo, int size)
843 +struct instruction *alloc_phisrc(pseudo_t pseudo, struct symbol *type)
828 844 {
829 - struct instruction *insn;
830 - pseudo_t phi;
845 + struct instruction *insn = alloc_typed_instruction(OP_PHISOURCE, type);
846 + pseudo_t phi = __alloc_pseudo(0);
831 847 static int nr = 0;
832 848
833 - if (!source)
834 - return VOID;
835 -
836 - insn = alloc_instruction(OP_PHISOURCE, size);
837 - phi = __alloc_pseudo(0);
838 849 phi->type = PSEUDO_PHI;
839 850 phi->nr = ++nr;
840 851 phi->def = insn;
841 852
842 853 use_pseudo(insn, pseudo, &insn->phi_src);
843 - insn->bb = source;
844 854 insn->target = phi;
855 + return insn;
856 +}
857 +
858 +pseudo_t alloc_phi(struct basic_block *source, pseudo_t pseudo, struct symbol *type)
859 +{
860 + struct instruction *insn;
861 +
862 + if (!source)
863 + return VOID;
864 +
865 + insn = alloc_phisrc(pseudo, type);
866 + insn->bb = source;
845 867 add_instruction(&source->insns, insn);
846 - return phi;
868 + return insn->target;
847 869 }
848 870
871 +struct instruction *alloc_phi_node(struct basic_block *bb, struct symbol *type, struct ident *ident)
872 +{
873 + struct instruction *phi_node = alloc_typed_instruction(OP_PHI, type);
874 + pseudo_t phi;
875 +
876 + phi = alloc_pseudo(phi_node);
877 + phi->ident = ident;
878 + phi->def = phi_node;
879 + phi_node->target = phi;
880 + phi_node->bb = bb;
881 + return phi_node;
882 +}
883 +
884 +void add_phi_node(struct basic_block *bb, struct instruction *phi_node)
885 +{
886 + struct instruction *insn;
887 +
888 + FOR_EACH_PTR(bb->insns, insn) {
889 + enum opcode op = insn->opcode;
890 + if (op == OP_PHI)
891 + continue;
892 + INSERT_CURRENT(phi_node, insn);
893 + return;
894 + } END_FOR_EACH_PTR(insn);
895 +
896 + // FIXME
897 + add_instruction(&bb->insns, phi_node);
898 +}
899 +
900 +struct instruction *insert_phi_node(struct basic_block *bb, struct symbol *var)
901 +{
902 + struct instruction *phi_node = alloc_phi_node(bb, var, var->ident);
903 + add_phi_node(bb, phi_node);
904 + return phi_node;
905 +}
906 +
849 907 /*
850 908 * We carry the "access_data" structure around for any accesses,
851 909 * which simplifies things a lot. It contains all the access
852 910 * information in one place.
853 911 */
854 912 struct access_data {
855 - struct symbol *result_type; // result ctype
856 - struct symbol *source_type; // source ctype
913 + struct symbol *type; // ctype
914 + struct symbol *btype; // base type of bitfields
857 915 pseudo_t address; // pseudo containing address ..
858 916 unsigned int offset; // byte offset
859 - struct position pos;
860 917 };
861 918
862 -static void finish_address_gen(struct entrypoint *ep, struct access_data *ad)
863 -{
864 -}
865 -
866 919 static int linearize_simple_address(struct entrypoint *ep,
867 920 struct expression *addr,
868 921 struct access_data *ad)
869 922 {
870 923 if (addr->type == EXPR_SYMBOL) {
871 924 linearize_one_symbol(ep, addr->symbol);
872 925 ad->address = symbol_pseudo(ep, addr->symbol);
873 926 return 1;
874 927 }
875 928 if (addr->type == EXPR_BINOP) {
876 929 if (addr->right->type == EXPR_VALUE) {
↓ open down ↓ |
1 lines elided |
↑ open up ↑ |
877 930 if (addr->op == '+') {
878 931 ad->offset += get_expression_value(addr->right);
879 932 return linearize_simple_address(ep, addr->left, ad);
880 933 }
881 934 }
882 935 }
883 936 ad->address = linearize_expression(ep, addr);
884 937 return 1;
885 938 }
886 939
887 -static struct symbol *base_type(struct symbol *sym)
940 +static struct symbol *bitfield_base_type(struct symbol *sym)
888 941 {
889 942 struct symbol *base = sym;
890 943
891 944 if (sym) {
892 945 if (sym->type == SYM_NODE)
893 946 base = base->ctype.base_type;
894 947 if (base->type == SYM_BITFIELD)
895 948 return base->ctype.base_type;
896 949 }
897 950 return sym;
898 951 }
899 952
900 953 static int linearize_address_gen(struct entrypoint *ep,
901 954 struct expression *expr,
902 955 struct access_data *ad)
903 956 {
904 957 struct symbol *ctype = expr->ctype;
905 958
906 959 if (!ctype)
907 960 return 0;
908 - ad->pos = expr->pos;
909 - ad->result_type = ctype;
910 - ad->source_type = base_type(ctype);
961 + ad->type = ctype;
911 962 if (expr->type == EXPR_PREOP && expr->op == '*')
912 963 return linearize_simple_address(ep, expr->unop, ad);
913 964
914 965 warning(expr->pos, "generating address of non-lvalue (%d)", expr->type);
915 966 return 0;
916 967 }
917 968
918 969 static pseudo_t add_load(struct entrypoint *ep, struct access_data *ad)
919 970 {
920 971 struct instruction *insn;
921 972 pseudo_t new;
922 973
923 - insn = alloc_typed_instruction(OP_LOAD, ad->source_type);
974 + if (!ep->active)
975 + return VOID;
976 +
977 + insn = alloc_typed_instruction(OP_LOAD, ad->btype);
924 978 new = alloc_pseudo(insn);
925 979
926 980 insn->target = new;
927 981 insn->offset = ad->offset;
982 + insn->is_volatile = ad->type && (ad->type->ctype.modifiers & MOD_VOLATILE);
928 983 use_pseudo(insn, ad->address, &insn->src);
929 984 add_one_insn(ep, insn);
930 985 return new;
931 986 }
932 987
933 988 static void add_store(struct entrypoint *ep, struct access_data *ad, pseudo_t value)
934 989 {
935 990 struct basic_block *bb = ep->active;
991 + struct instruction *store;
936 992
937 - if (bb_reachable(bb)) {
938 - struct instruction *store = alloc_typed_instruction(OP_STORE, ad->source_type);
939 - store->offset = ad->offset;
940 - use_pseudo(store, value, &store->target);
941 - use_pseudo(store, ad->address, &store->src);
942 - add_one_insn(ep, store);
993 + if (!bb)
994 + return;
995 +
996 + store = alloc_typed_instruction(OP_STORE, ad->btype);
997 + store->offset = ad->offset;
998 + store->is_volatile = ad->type && (ad->type->ctype.modifiers & MOD_VOLATILE);
999 + use_pseudo(store, value, &store->target);
1000 + use_pseudo(store, ad->address, &store->src);
1001 + add_one_insn(ep, store);
1002 +}
1003 +
1004 +static pseudo_t linearize_bitfield_insert(struct entrypoint *ep,
1005 + pseudo_t ori, pseudo_t val, struct symbol *ctype, struct symbol *btype)
1006 +{
1007 + unsigned int shift = ctype->bit_offset;
1008 + unsigned int size = ctype->bit_size;
1009 + unsigned long long mask = ((1ULL << size) - 1);
1010 + unsigned long long smask= bits_mask(btype->bit_size);
1011 +
1012 + val = add_cast(ep, btype, ctype, OP_ZEXT, val);
1013 + if (shift) {
1014 + val = add_binary_op(ep, btype, OP_SHL, val, value_pseudo(shift));
1015 + mask <<= shift;
943 1016 }
1017 + ori = add_binary_op(ep, btype, OP_AND, ori, value_pseudo(~mask & smask));
1018 + val = add_binary_op(ep, btype, OP_OR, ori, val);
1019 +
1020 + return val;
944 1021 }
945 1022
946 1023 static pseudo_t linearize_store_gen(struct entrypoint *ep,
947 1024 pseudo_t value,
948 1025 struct access_data *ad)
949 1026 {
1027 + struct symbol *ctype = ad->type;
1028 + struct symbol *btype;
950 1029 pseudo_t store = value;
951 1030
952 - if (type_size(ad->source_type) != type_size(ad->result_type)) {
953 - struct symbol *ctype = ad->result_type;
954 - unsigned int shift = ctype->bit_offset;
955 - unsigned int size = ctype->bit_size;
956 - pseudo_t orig = add_load(ep, ad);
957 - unsigned long long mask = (1ULL << size) - 1;
1031 + if (!ep->active)
1032 + return VOID;
958 1033
959 - if (shift) {
960 - store = add_binary_op(ep, ad->source_type, OP_SHL, value, value_pseudo(ctype, shift));
961 - mask <<= shift;
962 - }
963 - orig = add_binary_op(ep, ad->source_type, OP_AND, orig, value_pseudo(ctype, ~mask));
964 - store = add_binary_op(ep, ad->source_type, OP_OR, orig, store);
1034 + btype = ad->btype = bitfield_base_type(ctype);
1035 + if (type_size(btype) != type_size(ctype)) {
1036 + pseudo_t orig = add_load(ep, ad);
1037 + store = linearize_bitfield_insert(ep, orig, value, ctype, btype);
965 1038 }
966 1039 add_store(ep, ad, store);
967 1040 return value;
968 1041 }
969 1042
1043 +static void taint_undefined_behaviour(struct instruction *insn)
1044 +{
1045 + pseudo_t src2;
1046 +
1047 + switch (insn->opcode) {
1048 + case OP_LSR:
1049 + case OP_ASR:
1050 + case OP_SHL:
1051 + src2 = insn->src2;
1052 + if (src2->type != PSEUDO_VAL)
1053 + break;
1054 + if ((unsigned long long)src2->value >= insn->size)
1055 + insn->tainted = 1;
1056 + break;
1057 + }
1058 +}
1059 +
970 1060 static pseudo_t add_binary_op(struct entrypoint *ep, struct symbol *ctype, int op, pseudo_t left, pseudo_t right)
971 1061 {
972 1062 struct instruction *insn = alloc_typed_instruction(op, ctype);
973 1063 pseudo_t target = alloc_pseudo(insn);
974 1064 insn->target = target;
975 1065 use_pseudo(insn, left, &insn->src1);
976 1066 use_pseudo(insn, right, &insn->src2);
977 1067 add_one_insn(ep, insn);
978 1068 return target;
979 1069 }
980 1070
↓ open down ↓ |
1 lines elided |
↑ open up ↑ |
981 1071 static pseudo_t add_setval(struct entrypoint *ep, struct symbol *ctype, struct expression *val)
982 1072 {
983 1073 struct instruction *insn = alloc_typed_instruction(OP_SETVAL, ctype);
984 1074 pseudo_t target = alloc_pseudo(insn);
985 1075 insn->target = target;
986 1076 insn->val = val;
987 1077 add_one_insn(ep, insn);
988 1078 return target;
989 1079 }
990 1080
1081 +static pseudo_t add_setfval(struct entrypoint *ep, struct symbol *ctype, long double fval)
1082 +{
1083 + struct instruction *insn = alloc_typed_instruction(OP_SETFVAL, ctype);
1084 + pseudo_t target = alloc_pseudo(insn);
1085 + insn->target = target;
1086 + insn->fvalue = fval;
1087 + add_one_insn(ep, insn);
1088 + return target;
1089 +}
1090 +
991 1091 static pseudo_t add_symbol_address(struct entrypoint *ep, struct symbol *sym)
992 1092 {
993 1093 struct instruction *insn = alloc_instruction(OP_SYMADDR, bits_in_pointer);
994 1094 pseudo_t target = alloc_pseudo(insn);
995 1095
996 1096 insn->target = target;
997 - use_pseudo(insn, symbol_pseudo(ep, sym), &insn->symbol);
1097 + use_pseudo(insn, symbol_pseudo(ep, sym), &insn->src);
998 1098 add_one_insn(ep, insn);
999 1099 return target;
1000 1100 }
1001 1101
1002 -static pseudo_t linearize_load_gen(struct entrypoint *ep, struct access_data *ad)
1102 +static pseudo_t linearize_bitfield_extract(struct entrypoint *ep,
1103 + pseudo_t val, struct symbol *ctype, struct symbol *btype)
1003 1104 {
1004 - struct symbol *ctype = ad->result_type;
1005 - pseudo_t new = add_load(ep, ad);
1105 + unsigned int off = ctype->bit_offset;
1006 1106
1007 - if (ctype->bit_offset) {
1008 - pseudo_t shift = value_pseudo(ctype, ctype->bit_offset);
1009 - pseudo_t newval = add_binary_op(ep, ad->source_type, OP_LSR, new, shift);
1010 - new = newval;
1107 + if (off) {
1108 + pseudo_t shift = value_pseudo(off);
1109 + val = add_binary_op(ep, btype, OP_LSR, val, shift);
1011 1110 }
1012 - if (ctype->bit_size != type_size(ad->source_type))
1013 - new = cast_pseudo(ep, new, ad->source_type, ad->result_type);
1111 + val = cast_pseudo(ep, val, btype, ctype);
1112 + return val;
1113 +}
1114 +
1115 +static pseudo_t linearize_load_gen(struct entrypoint *ep, struct access_data *ad)
1116 +{
1117 + struct symbol *ctype = ad->type;
1118 + struct symbol *btype;
1119 + pseudo_t new;
1120 +
1121 + if (!ep->active)
1122 + return VOID;
1123 +
1124 + btype = ad->btype = bitfield_base_type(ctype);
1125 + new = add_load(ep, ad);
1126 + if (ctype->bit_size != type_size(btype))
1127 + new = linearize_bitfield_extract(ep, new, ctype, btype);
1014 1128 return new;
1015 1129 }
1016 1130
1017 1131 static pseudo_t linearize_access(struct entrypoint *ep, struct expression *expr)
1018 1132 {
1019 1133 struct access_data ad = { NULL, };
1020 1134 pseudo_t value;
1021 1135
1022 1136 if (!linearize_address_gen(ep, expr, &ad))
1023 1137 return VOID;
1024 1138 value = linearize_load_gen(ep, &ad);
1025 - finish_address_gen(ep, &ad);
1026 1139 return value;
1027 1140 }
1028 1141
1029 -/* FIXME: FP */
1030 1142 static pseudo_t linearize_inc_dec(struct entrypoint *ep, struct expression *expr, int postop)
1031 1143 {
1032 1144 struct access_data ad = { NULL, };
1033 - pseudo_t old, new, one;
1145 + pseudo_t old, new, one;
1034 1146 int op = expr->op == SPECIAL_INCREMENT ? OP_ADD : OP_SUB;
1035 1147
1036 1148 if (!linearize_address_gen(ep, expr->unop, &ad))
1037 1149 return VOID;
1038 1150
1039 1151 old = linearize_load_gen(ep, &ad);
1040 - one = value_pseudo(expr->ctype, expr->op_value);
1041 - new = add_binary_op(ep, expr->ctype, op, old, one);
1152 + op = opcode_float(op, expr->ctype);
1153 + if (is_float_type(expr->ctype))
1154 + one = add_setfval(ep, expr->ctype, expr->op_value);
1155 + else
1156 + one = value_pseudo(expr->op_value);
1157 + if (ad.btype != ad.type)
1158 + old = cast_pseudo(ep, old, ad.type, ad.btype);
1159 + new = add_binary_op(ep, ad.btype, op, old, one);
1160 + if (ad.btype != ad.type)
1161 + new = cast_pseudo(ep, new, ad.btype, ad.type);
1042 1162 linearize_store_gen(ep, new, &ad);
1043 - finish_address_gen(ep, &ad);
1044 1163 return postop ? old : new;
1045 1164 }
1046 1165
1047 -static pseudo_t add_uniop(struct entrypoint *ep, struct expression *expr, int op, pseudo_t src)
1166 +static pseudo_t add_unop(struct entrypoint *ep, struct symbol *ctype, int op, pseudo_t src)
1048 1167 {
1049 - struct instruction *insn = alloc_typed_instruction(op, expr->ctype);
1168 + struct instruction *insn = alloc_typed_instruction(op, ctype);
1050 1169 pseudo_t new = alloc_pseudo(insn);
1051 1170
1052 1171 insn->target = new;
1053 1172 use_pseudo(insn, src, &insn->src1);
1054 1173 add_one_insn(ep, insn);
1055 1174 return new;
1056 1175 }
1057 1176
1177 +static pseudo_t add_cast(struct entrypoint *ep, struct symbol *to,
1178 + struct symbol *from, int op, pseudo_t src)
1179 +{
1180 + pseudo_t new = add_unop(ep, to, op, src);
1181 + new->def->orig_type = from;
1182 + return new;
1183 +}
1184 +
1058 1185 static pseudo_t linearize_slice(struct entrypoint *ep, struct expression *expr)
1059 1186 {
1060 1187 pseudo_t pre = linearize_expression(ep, expr->base);
1061 1188 struct instruction *insn = alloc_typed_instruction(OP_SLICE, expr->ctype);
1062 1189 pseudo_t new = alloc_pseudo(insn);
1063 1190
1064 1191 insn->target = new;
1065 1192 insn->from = expr->r_bitpos;
1066 1193 insn->len = expr->r_nrbits;
1067 1194 use_pseudo(insn, pre, &insn->base);
1068 1195 add_one_insn(ep, insn);
1069 1196 return new;
1070 1197 }
1071 1198
1072 1199 static pseudo_t linearize_regular_preop(struct entrypoint *ep, struct expression *expr)
1073 1200 {
1074 1201 pseudo_t pre = linearize_expression(ep, expr->unop);
1202 + struct symbol *ctype = expr->ctype;
1075 1203 switch (expr->op) {
1076 1204 case '+':
1077 1205 return pre;
1078 1206 case '!': {
1079 - pseudo_t zero = value_pseudo(expr->ctype, 0);
1080 - return add_binary_op(ep, expr->ctype, OP_SET_EQ, pre, zero);
1207 + pseudo_t zero = value_pseudo(0);
1208 + return add_binary_op(ep, ctype, OP_SET_EQ, pre, zero);
1081 1209 }
1082 1210 case '~':
1083 - return add_uniop(ep, expr, OP_NOT, pre);
1211 + return add_unop(ep, ctype, OP_NOT, pre);
1084 1212 case '-':
1085 - return add_uniop(ep, expr, OP_NEG, pre);
1213 + return add_unop(ep, ctype, opcode_float(OP_NEG, ctype), pre);
1086 1214 }
1087 1215 return VOID;
1088 1216 }
1089 1217
1090 1218 static pseudo_t linearize_preop(struct entrypoint *ep, struct expression *expr)
1091 1219 {
1092 1220 /*
1093 1221 * '*' is an lvalue access, and is fundamentally different
1094 1222 * from an arithmetic operation. Maybe it should have an
1095 1223 * expression type of its own..
1096 1224 */
1097 1225 if (expr->op == '*')
1098 1226 return linearize_access(ep, expr);
1099 1227 if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
1100 1228 return linearize_inc_dec(ep, expr, 0);
1101 1229 return linearize_regular_preop(ep, expr);
1102 1230 }
1103 1231
1104 1232 static pseudo_t linearize_postop(struct entrypoint *ep, struct expression *expr)
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
1105 1233 {
1106 1234 return linearize_inc_dec(ep, expr, 1);
1107 1235 }
1108 1236
1109 1237 /*
1110 1238 * Casts to pointers are "less safe" than other casts, since
1111 1239 * they imply type-unsafe accesses. "void *" is a special
1112 1240 * case, since you can't access through it anyway without another
1113 1241 * cast.
1114 1242 */
1115 -static struct instruction *alloc_cast_instruction(struct symbol *src, struct symbol *ctype)
1243 +enum mtype {
1244 + MTYPE_UINT,
1245 + MTYPE_SINT,
1246 + MTYPE_PTR,
1247 + MTYPE_VPTR, // TODO: must be removed ?
1248 + MTYPE_FLOAT,
1249 + MTYPE_BAD,
1250 +};
1251 +
1252 +static enum mtype get_mtype(struct symbol *s)
1116 1253 {
1117 - int opcode = OP_CAST;
1118 - struct symbol *base = ctype;
1254 + int sign = (s->ctype.modifiers & MOD_SIGNED) ? 1 : 0;
1119 1255
1120 - if (src->ctype.modifiers & MOD_SIGNED)
1121 - opcode = OP_SCAST;
1122 - if (base->type == SYM_NODE)
1123 - base = base->ctype.base_type;
1124 - if (base->type == SYM_PTR) {
1125 - base = base->ctype.base_type;
1126 - if (base != &void_ctype)
1127 - opcode = OP_PTRCAST;
1128 - } else if (base->ctype.base_type == &fp_type)
1129 - opcode = OP_FPCAST;
1130 - return alloc_typed_instruction(opcode, ctype);
1256 +retry: switch (s->type) {
1257 + case SYM_NODE:
1258 + s = s->ctype.base_type;
1259 + goto retry;
1260 + case SYM_PTR:
1261 + if (s->ctype.base_type == &void_ctype)
1262 + return MTYPE_VPTR;
1263 + return MTYPE_PTR;
1264 + case SYM_BITFIELD:
1265 + case SYM_RESTRICT:
1266 + case SYM_FOULED:
1267 + case SYM_ENUM:
1268 + s = s->ctype.base_type;
1269 + /* fall-through */
1270 + case_int:
1271 + return sign ? MTYPE_SINT : MTYPE_UINT;
1272 + case SYM_BASETYPE:
1273 + if (s->ctype.base_type == &fp_type)
1274 + return MTYPE_FLOAT;
1275 + if (s->ctype.base_type == &int_type)
1276 + goto case_int;
1277 + /* fall-through */
1278 + default:
1279 + return MTYPE_BAD;
1280 + }
1131 1281 }
1132 1282
1283 +static int get_cast_opcode(struct symbol *dst, struct symbol *src)
1284 +{
1285 + enum mtype stype = get_mtype(src);
1286 + enum mtype dtype = get_mtype(dst);
1287 +
1288 + switch (dtype) {
1289 + case MTYPE_FLOAT:
1290 + switch (stype) {
1291 + case MTYPE_FLOAT:
1292 + if (dst->bit_size == src->bit_size)
1293 + return OP_NOP;
1294 + return OP_FCVTF;
1295 + case MTYPE_UINT:
1296 + return OP_UCVTF;
1297 + case MTYPE_SINT:
1298 + return OP_SCVTF;
1299 + default:
1300 + return OP_BADOP;
1301 + }
1302 + case MTYPE_PTR:
1303 + switch (stype) {
1304 + case MTYPE_UINT:
1305 + case MTYPE_SINT:
1306 + return OP_UTPTR;
1307 + case MTYPE_PTR:
1308 + case MTYPE_VPTR:
1309 + return OP_PTRCAST;
1310 + default:
1311 + return OP_BADOP;
1312 + }
1313 + case MTYPE_VPTR:
1314 + switch (stype) {
1315 + case MTYPE_PTR:
1316 + case MTYPE_VPTR:
1317 + case MTYPE_UINT:
1318 + stype = MTYPE_UINT;
1319 + /* fall through */
1320 + case MTYPE_SINT:
1321 + break;
1322 + default:
1323 + return OP_BADOP;
1324 + }
1325 + /* fall through */
1326 + case MTYPE_UINT:
1327 + case MTYPE_SINT:
1328 + switch (stype) {
1329 + case MTYPE_FLOAT:
1330 + return dtype == MTYPE_UINT ? OP_FCVTU : OP_FCVTS;
1331 + case MTYPE_PTR:
1332 + return OP_PTRTU;
1333 + case MTYPE_VPTR:
1334 + case MTYPE_UINT:
1335 + case MTYPE_SINT:
1336 + if (dst->bit_size ==src->bit_size)
1337 + return OP_NOP;
1338 + if (dst->bit_size < src->bit_size)
1339 + return OP_TRUNC;
1340 + return stype == MTYPE_SINT ? OP_SEXT : OP_ZEXT;
1341 + default:
1342 + return OP_BADOP;
1343 + }
1344 + /* fall through */
1345 + default:
1346 + if (src->type == SYM_NODE)
1347 + src = src->ctype.base_type;
1348 + if (dst->type == SYM_NODE)
1349 + dst = dst->ctype.base_type;
1350 + if (src == dst)
1351 + return OP_NOP;
1352 + return OP_BADOP;
1353 + }
1354 +}
1355 +
1133 1356 static pseudo_t cast_pseudo(struct entrypoint *ep, pseudo_t src, struct symbol *from, struct symbol *to)
1134 1357 {
1358 + const struct position pos = current_pos;
1135 1359 pseudo_t result;
1136 1360 struct instruction *insn;
1361 + int opcode;
1137 1362
1138 1363 if (src == VOID)
1139 1364 return VOID;
1140 1365 if (!from || !to)
1141 1366 return VOID;
1142 1367 if (from->bit_size < 0 || to->bit_size < 0)
1143 1368 return VOID;
1144 - insn = alloc_cast_instruction(from, to);
1369 + opcode = get_cast_opcode(to, from);
1370 + switch (opcode) {
1371 + case OP_NOP:
1372 + return src;
1373 + case OP_UTPTR:
1374 + if (from->bit_size == to->bit_size)
1375 + break;
1376 + if (src == value_pseudo(0))
1377 + break;
1378 + if (Wint_to_pointer_cast)
1379 + warning(pos, "non size-preserving integer to pointer cast");
1380 + src = cast_pseudo(ep, src, from, size_t_ctype);
1381 + from = size_t_ctype;
1382 + break;
1383 + case OP_PTRTU:
1384 + if (from->bit_size == to->bit_size)
1385 + break;
1386 + if (Wpointer_to_int_cast)
1387 + warning(pos, "non size-preserving pointer to integer cast");
1388 + src = cast_pseudo(ep, src, from, size_t_ctype);
1389 + return cast_pseudo(ep, src, size_t_ctype, to);
1390 + case OP_BADOP:
1391 + return VOID;
1392 + default:
1393 + break;
1394 + }
1395 + insn = alloc_typed_instruction(opcode, to);
1145 1396 result = alloc_pseudo(insn);
1146 1397 insn->target = result;
1147 1398 insn->orig_type = from;
1148 1399 use_pseudo(insn, src, &insn->src);
1149 1400 add_one_insn(ep, insn);
1150 1401 return result;
1151 1402 }
1152 1403
1153 -static int opcode_sign(int opcode, struct symbol *ctype)
1404 +static int map_opcode(int opcode, struct symbol *ctype)
1154 1405 {
1406 + if (ctype && is_float_type(ctype))
1407 + return opcode_table[opcode].to_float;
1155 1408 if (ctype && (ctype->ctype.modifiers & MOD_SIGNED)) {
1156 1409 switch(opcode) {
1157 - case OP_MULU: case OP_DIVU: case OP_MODU: case OP_LSR:
1410 + case OP_DIVU: case OP_MODU: case OP_LSR:
1158 1411 opcode++;
1159 1412 }
1160 1413 }
1161 1414 return opcode;
1162 1415 }
1163 1416
1164 1417 static inline pseudo_t add_convert_to_bool(struct entrypoint *ep, pseudo_t src, struct symbol *type)
1165 1418 {
1166 1419 pseudo_t zero;
1167 1420 int op;
1168 1421
1422 + if (!type || src == VOID)
1423 + return VOID;
1169 1424 if (is_bool_type(type))
1170 1425 return src;
1171 - zero = value_pseudo(type, 0);
1172 - op = OP_SET_NE;
1426 + if (src->type == PSEUDO_VAL && (src->value == 0 || src->value == 1))
1427 + return src;
1428 + if (is_float_type(type)) {
1429 + zero = add_setfval(ep, type, 0.0);
1430 + op = map_opcode(OP_SET_NE, type);
1431 + } else {
1432 + zero = value_pseudo(0);
1433 + op = OP_SET_NE;
1434 + }
1173 1435 return add_binary_op(ep, &bool_ctype, op, src, zero);
1174 1436 }
1175 1437
1176 1438 static pseudo_t linearize_expression_to_bool(struct entrypoint *ep, struct expression *expr)
1177 1439 {
1178 1440 pseudo_t dst;
1179 1441 dst = linearize_expression(ep, expr);
1180 1442 dst = add_convert_to_bool(ep, dst, expr->ctype);
1181 1443 return dst;
1182 1444 }
1183 1445
1184 1446 static pseudo_t linearize_assignment(struct entrypoint *ep, struct expression *expr)
1185 1447 {
1186 1448 struct access_data ad = { NULL, };
1187 1449 struct expression *target = expr->left;
1188 1450 struct expression *src = expr->right;
1189 1451 struct symbol *ctype;
1190 1452 pseudo_t value;
↓ open down ↓ |
8 lines elided |
↑ open up ↑ |
1191 1453
1192 1454 value = linearize_expression(ep, src);
1193 1455 if (!target || !linearize_address_gen(ep, target, &ad))
1194 1456 return value;
1195 1457 if (expr->op != '=') {
1196 1458 pseudo_t oldvalue = linearize_load_gen(ep, &ad);
1197 1459 pseudo_t dst;
1198 1460 static const int op_trans[] = {
1199 1461 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = OP_ADD,
1200 1462 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = OP_SUB,
1201 - [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = OP_MULU,
1463 + [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = OP_MUL,
1202 1464 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = OP_DIVU,
1203 1465 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = OP_MODU,
1204 1466 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = OP_SHL,
1205 1467 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = OP_LSR,
1206 1468 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = OP_AND,
1207 1469 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = OP_OR,
1208 1470 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = OP_XOR
1209 1471 };
1210 1472 int opcode;
1211 1473
1212 1474 if (!src)
1213 1475 return VOID;
1214 1476
1215 1477 ctype = src->ctype;
1216 1478 oldvalue = cast_pseudo(ep, oldvalue, target->ctype, ctype);
1217 - opcode = opcode_sign(op_trans[expr->op - SPECIAL_BASE], ctype);
1479 + opcode = map_opcode(op_trans[expr->op - SPECIAL_BASE], ctype);
1218 1480 dst = add_binary_op(ep, ctype, opcode, oldvalue, value);
1481 + taint_undefined_behaviour(dst->def);
1219 1482 value = cast_pseudo(ep, dst, ctype, expr->ctype);
1220 1483 }
1221 1484 value = linearize_store_gen(ep, value, &ad);
1222 - finish_address_gen(ep, &ad);
1223 1485 return value;
1224 1486 }
1225 1487
1226 1488 static pseudo_t linearize_call_expression(struct entrypoint *ep, struct expression *expr)
1227 1489 {
1228 1490 struct expression *arg, *fn;
1229 1491 struct instruction *insn = alloc_typed_instruction(OP_CALL, expr->ctype);
1230 1492 pseudo_t retval, call;
1231 1493 struct ctype *ctype = NULL;
1232 1494 struct symbol *fntype;
1233 1495 struct context *context;
1234 1496
1235 - if (!expr->ctype) {
1236 - warning(expr->pos, "call with no type!");
1497 + if (!expr->ctype)
1237 1498 return VOID;
1238 - }
1239 1499
1500 + fn = expr->fn;
1501 + fntype = fn->ctype;
1502 + ctype = &fntype->ctype;
1503 + if (fntype->type == SYM_NODE)
1504 + fntype = fntype->ctype.base_type;
1505 +
1506 + add_symbol(&insn->fntypes, fntype);
1240 1507 FOR_EACH_PTR(expr->args, arg) {
1241 1508 pseudo_t new = linearize_expression(ep, arg);
1242 1509 use_pseudo(insn, new, add_pseudo(&insn->arguments, new));
1510 + add_symbol(&insn->fntypes, arg->ctype);
1243 1511 } END_FOR_EACH_PTR(arg);
1244 1512
1245 - fn = expr->fn;
1513 + if (fn->type == EXPR_PREOP && fn->op == '*' && is_func_type(fn->ctype))
1514 + fn = fn->unop;
1246 1515
1247 - if (fn->ctype)
1248 - ctype = &fn->ctype->ctype;
1249 -
1250 - fntype = fn->ctype;
1251 - if (fntype) {
1252 - if (fntype->type == SYM_NODE)
1253 - fntype = fntype->ctype.base_type;
1254 - }
1255 - insn->fntype = fntype;
1256 -
1257 - if (fn->type == EXPR_PREOP) {
1258 - if (fn->unop->type == EXPR_SYMBOL) {
1259 - struct symbol *sym = fn->unop->symbol;
1260 - if (sym->ctype.base_type->type == SYM_FN)
1261 - fn = fn->unop;
1262 - }
1263 - }
1264 1516 if (fn->type == EXPR_SYMBOL) {
1265 1517 call = symbol_pseudo(ep, fn->symbol);
1266 1518 } else {
1267 1519 call = linearize_expression(ep, fn);
1268 1520 }
1269 1521 use_pseudo(insn, call, &insn->func);
1270 1522 retval = VOID;
1271 1523 if (expr->ctype != &void_ctype)
1272 1524 retval = alloc_pseudo(insn);
1273 1525 insn->target = retval;
1274 1526 add_one_insn(ep, insn);
1275 1527
1276 1528 if (ctype) {
1277 1529 FOR_EACH_PTR(ctype->contexts, context) {
1278 1530 int in = context->in;
1279 1531 int out = context->out;
1280 1532 int check = 0;
1281 1533 int context_diff;
1282 1534 if (in < 0) {
1283 1535 check = 1;
1284 1536 in = 0;
1285 1537 }
1286 1538 if (out < 0) {
1287 1539 check = 0;
1288 1540 out = 0;
1289 1541 }
1290 1542 context_diff = out - in;
1291 1543 if (check || context_diff) {
1292 1544 insn = alloc_instruction(OP_CONTEXT, 0);
1293 1545 insn->increment = context_diff;
1294 1546 insn->check = check;
1295 1547 insn->context_expr = context->context;
1296 1548 add_one_insn(ep, insn);
↓ open down ↓ |
23 lines elided |
↑ open up ↑ |
1297 1549 }
1298 1550 } END_FOR_EACH_PTR(context);
1299 1551 }
1300 1552
1301 1553 return retval;
1302 1554 }
1303 1555
1304 1556 static pseudo_t linearize_binop_bool(struct entrypoint *ep, struct expression *expr)
1305 1557 {
1306 1558 pseudo_t src1, src2, dst;
1307 - int op = (expr->op == SPECIAL_LOGICAL_OR) ? OP_OR_BOOL : OP_AND_BOOL;
1559 + int op = (expr->op == SPECIAL_LOGICAL_OR) ? OP_OR : OP_AND;
1308 1560
1309 1561 src1 = linearize_expression_to_bool(ep, expr->left);
1310 1562 src2 = linearize_expression_to_bool(ep, expr->right);
1311 1563 dst = add_binary_op(ep, &bool_ctype, op, src1, src2);
1312 1564 if (expr->ctype != &bool_ctype)
1313 1565 dst = cast_pseudo(ep, dst, &bool_ctype, expr->ctype);
1314 1566 return dst;
1315 1567 }
1316 1568
1317 1569 static pseudo_t linearize_binop(struct entrypoint *ep, struct expression *expr)
1318 1570 {
1319 1571 pseudo_t src1, src2, dst;
1320 1572 static const int opcode[] = {
1321 1573 ['+'] = OP_ADD, ['-'] = OP_SUB,
1322 - ['*'] = OP_MULU, ['/'] = OP_DIVU,
1574 + ['*'] = OP_MUL, ['/'] = OP_DIVU,
1323 1575 ['%'] = OP_MODU, ['&'] = OP_AND,
1324 1576 ['|'] = OP_OR, ['^'] = OP_XOR,
1325 1577 [SPECIAL_LEFTSHIFT] = OP_SHL,
1326 1578 [SPECIAL_RIGHTSHIFT] = OP_LSR,
1327 1579 };
1328 1580 int op;
1329 1581
1330 1582 src1 = linearize_expression(ep, expr->left);
1331 1583 src2 = linearize_expression(ep, expr->right);
1332 - op = opcode_sign(opcode[expr->op], expr->ctype);
1584 + op = map_opcode(opcode[expr->op], expr->ctype);
1333 1585 dst = add_binary_op(ep, expr->ctype, op, src1, src2);
1586 + taint_undefined_behaviour(dst->def);
1334 1587 return dst;
1335 1588 }
1336 1589
1337 1590 static pseudo_t linearize_logical_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false);
1338 1591
1339 -pseudo_t linearize_cond_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false);
1592 +static pseudo_t linearize_cond_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false);
1340 1593
1341 1594 static pseudo_t linearize_select(struct entrypoint *ep, struct expression *expr)
1342 1595 {
1343 - pseudo_t cond, true, false, res;
1596 + pseudo_t cond, valt, valf, res;
1344 1597 struct instruction *insn;
1345 1598
1346 - true = linearize_expression(ep, expr->cond_true);
1347 - false = linearize_expression(ep, expr->cond_false);
1599 + valt = linearize_expression(ep, expr->cond_true);
1600 + valf = linearize_expression(ep, expr->cond_false);
1348 1601 cond = linearize_expression(ep, expr->conditional);
1349 1602
1350 1603 insn = alloc_typed_instruction(OP_SEL, expr->ctype);
1351 1604 if (!expr->cond_true)
1352 - true = cond;
1605 + valt = cond;
1353 1606 use_pseudo(insn, cond, &insn->src1);
1354 - use_pseudo(insn, true, &insn->src2);
1355 - use_pseudo(insn, false, &insn->src3);
1607 + use_pseudo(insn, valt, &insn->src2);
1608 + use_pseudo(insn, valf, &insn->src3);
1356 1609
1357 1610 res = alloc_pseudo(insn);
1358 1611 insn->target = res;
1359 1612 add_one_insn(ep, insn);
1360 1613 return res;
1361 1614 }
1362 1615
1363 1616 static pseudo_t add_join_conditional(struct entrypoint *ep, struct expression *expr,
1364 1617 pseudo_t phi1, pseudo_t phi2)
1365 1618 {
1366 1619 pseudo_t target;
1367 1620 struct instruction *phi_node;
1368 1621
1369 1622 if (phi1 == VOID)
1370 1623 return phi2;
1371 1624 if (phi2 == VOID)
1372 1625 return phi1;
1373 1626
1374 1627 phi_node = alloc_typed_instruction(OP_PHI, expr->ctype);
1375 1628 use_pseudo(phi_node, phi1, add_pseudo(&phi_node->phi_list, phi1));
1376 1629 use_pseudo(phi_node, phi2, add_pseudo(&phi_node->phi_list, phi2));
1377 1630 phi_node->target = target = alloc_pseudo(phi_node);
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
1378 1631 add_one_insn(ep, phi_node);
1379 1632 return target;
1380 1633 }
1381 1634
1382 1635 static pseudo_t linearize_short_conditional(struct entrypoint *ep, struct expression *expr,
1383 1636 struct expression *cond,
1384 1637 struct expression *expr_false)
1385 1638 {
1386 1639 pseudo_t src1, src2;
1387 1640 struct basic_block *bb_false;
1388 - struct basic_block *merge = alloc_basic_block(ep, expr->pos);
1641 + struct basic_block *merge;
1389 1642 pseudo_t phi1, phi2;
1390 - int size = type_size(expr->ctype);
1391 1643
1392 1644 if (!expr_false || !ep->active)
1393 1645 return VOID;
1394 1646
1395 1647 bb_false = alloc_basic_block(ep, expr_false->pos);
1648 + merge = alloc_basic_block(ep, expr->pos);
1649 +
1396 1650 src1 = linearize_expression(ep, cond);
1397 - phi1 = alloc_phi(ep->active, src1, size);
1398 - add_branch(ep, expr, src1, merge, bb_false);
1651 + phi1 = alloc_phi(ep->active, src1, expr->ctype);
1652 + add_branch(ep, src1, merge, bb_false);
1399 1653
1400 1654 set_activeblock(ep, bb_false);
1401 1655 src2 = linearize_expression(ep, expr_false);
1402 - phi2 = alloc_phi(ep->active, src2, size);
1656 + phi2 = alloc_phi(ep->active, src2, expr->ctype);
1403 1657 set_activeblock(ep, merge);
1404 1658
1405 1659 return add_join_conditional(ep, expr, phi1, phi2);
1406 1660 }
1407 1661
1408 1662 static pseudo_t linearize_conditional(struct entrypoint *ep, struct expression *expr,
1409 1663 struct expression *cond,
1410 1664 struct expression *expr_true,
1411 1665 struct expression *expr_false)
1412 1666 {
1413 1667 pseudo_t src1, src2;
1414 1668 pseudo_t phi1, phi2;
1415 1669 struct basic_block *bb_true, *bb_false, *merge;
1416 - int size = type_size(expr->ctype);
1417 1670
1418 1671 if (!cond || !expr_true || !expr_false || !ep->active)
1419 1672 return VOID;
1420 1673 bb_true = alloc_basic_block(ep, expr_true->pos);
1421 1674 bb_false = alloc_basic_block(ep, expr_false->pos);
1422 1675 merge = alloc_basic_block(ep, expr->pos);
1423 1676
1424 1677 linearize_cond_branch(ep, cond, bb_true, bb_false);
1425 1678
1426 1679 set_activeblock(ep, bb_true);
1427 1680 src1 = linearize_expression(ep, expr_true);
1428 - phi1 = alloc_phi(ep->active, src1, size);
1681 + phi1 = alloc_phi(ep->active, src1, expr->ctype);
1429 1682 add_goto(ep, merge);
1430 1683
1431 1684 set_activeblock(ep, bb_false);
1432 1685 src2 = linearize_expression(ep, expr_false);
1433 - phi2 = alloc_phi(ep->active, src2, size);
1686 + phi2 = alloc_phi(ep->active, src2, expr->ctype);
1434 1687 set_activeblock(ep, merge);
1435 1688
1436 1689 return add_join_conditional(ep, expr, phi1, phi2);
1437 1690 }
1438 1691
1692 +static void insert_phis(struct basic_block *bb, pseudo_t src, struct symbol *ctype,
1693 + struct instruction *node)
1694 +{
1695 + struct basic_block *parent;
1696 +
1697 + FOR_EACH_PTR(bb->parents, parent) {
1698 + struct instruction *br = delete_last_instruction(&parent->insns);
1699 + pseudo_t phi = alloc_phi(parent, src, ctype);
1700 + add_instruction(&parent->insns, br);
1701 + use_pseudo(node, phi, add_pseudo(&node->phi_list, phi));
1702 + } END_FOR_EACH_PTR(parent);
1703 +}
1704 +
1439 1705 static pseudo_t linearize_logical(struct entrypoint *ep, struct expression *expr)
1440 1706 {
1441 - struct expression *shortcut;
1707 + struct symbol *ctype = expr->ctype;
1708 + struct basic_block *other, *merge;
1709 + struct instruction *node;
1710 + pseudo_t src1, src2, phi2;
1442 1711
1443 - shortcut = alloc_const_expression(expr->pos, expr->op == SPECIAL_LOGICAL_OR);
1444 - shortcut->ctype = expr->ctype;
1445 - if (expr->op == SPECIAL_LOGICAL_OR)
1446 - return linearize_conditional(ep, expr, expr->left, shortcut, expr->right);
1447 - return linearize_conditional(ep, expr, expr->left, expr->right, shortcut);
1712 + if (!ep->active || !expr->left || !expr->right)
1713 + return VOID;
1714 +
1715 + other = alloc_basic_block(ep, expr->right->pos);
1716 + merge = alloc_basic_block(ep, expr->pos);
1717 + node = alloc_phi_node(merge, ctype, NULL);
1718 +
1719 + // LHS and its shortcut
1720 + if (expr->op == SPECIAL_LOGICAL_OR) {
1721 + linearize_cond_branch(ep, expr->left, merge, other);
1722 + src1 = value_pseudo(1);
1723 + } else {
1724 + linearize_cond_branch(ep, expr->left, other, merge);
1725 + src1 = value_pseudo(0);
1726 + }
1727 + insert_phis(merge, src1, ctype, node);
1728 +
1729 + // RHS
1730 + set_activeblock(ep, other);
1731 + src2 = linearize_expression_to_bool(ep, expr->right);
1732 + src2 = cast_pseudo(ep, src2, &bool_ctype, ctype);
1733 + phi2 = alloc_phi(ep->active, src2, ctype);
1734 + use_pseudo(node, phi2, add_pseudo(&node->phi_list, phi2));
1735 +
1736 + // join
1737 + set_activeblock(ep, merge);
1738 + add_instruction(&merge->insns, node);
1739 + return node->target;
1448 1740 }
1449 1741
1450 1742 static pseudo_t linearize_compare(struct entrypoint *ep, struct expression *expr)
1451 1743 {
1452 1744 static const int cmpop[] = {
1453 1745 ['>'] = OP_SET_GT, ['<'] = OP_SET_LT,
1454 1746 [SPECIAL_EQUAL] = OP_SET_EQ,
1455 1747 [SPECIAL_NOTEQUAL] = OP_SET_NE,
1456 1748 [SPECIAL_GTE] = OP_SET_GE,
1457 1749 [SPECIAL_LTE] = OP_SET_LE,
1458 1750 [SPECIAL_UNSIGNED_LT] = OP_SET_B,
1459 1751 [SPECIAL_UNSIGNED_GT] = OP_SET_A,
1460 1752 [SPECIAL_UNSIGNED_LTE] = OP_SET_BE,
1461 1753 [SPECIAL_UNSIGNED_GTE] = OP_SET_AE,
1462 1754 };
1463 -
1755 + int op = opcode_float(cmpop[expr->op], expr->right->ctype);
1464 1756 pseudo_t src1 = linearize_expression(ep, expr->left);
1465 1757 pseudo_t src2 = linearize_expression(ep, expr->right);
1466 - pseudo_t dst = add_binary_op(ep, expr->ctype, cmpop[expr->op], src1, src2);
1758 + pseudo_t dst = add_binary_op(ep, expr->ctype, op, src1, src2);
1467 1759 return dst;
1468 1760 }
1469 1761
1470 1762
1471 -pseudo_t linearize_cond_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false)
1763 +static pseudo_t linearize_cond_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false)
1472 1764 {
1473 1765 pseudo_t cond;
1474 1766
1475 1767 if (!expr || !bb_reachable(ep->active))
1476 1768 return VOID;
1477 1769
1478 1770 switch (expr->type) {
1479 1771
1480 1772 case EXPR_STRING:
1481 1773 case EXPR_VALUE:
1482 1774 add_goto(ep, expr->value ? bb_true : bb_false);
1483 1775 return VOID;
1484 1776
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
1485 1777 case EXPR_FVALUE:
1486 1778 add_goto(ep, expr->fvalue ? bb_true : bb_false);
1487 1779 return VOID;
1488 1780
1489 1781 case EXPR_LOGICAL:
1490 1782 linearize_logical_branch(ep, expr, bb_true, bb_false);
1491 1783 return VOID;
1492 1784
1493 1785 case EXPR_COMPARE:
1494 1786 cond = linearize_compare(ep, expr);
1495 - add_branch(ep, expr, cond, bb_true, bb_false);
1787 + add_branch(ep, cond, bb_true, bb_false);
1496 1788 break;
1497 1789
1498 1790 case EXPR_PREOP:
1499 1791 if (expr->op == '!')
1500 1792 return linearize_cond_branch(ep, expr->unop, bb_false, bb_true);
1501 1793 /* fall through */
1502 1794 default: {
1503 - cond = linearize_expression(ep, expr);
1504 - add_branch(ep, expr, cond, bb_true, bb_false);
1795 + cond = linearize_expression_to_bool(ep, expr);
1796 + add_branch(ep, cond, bb_true, bb_false);
1505 1797
1506 1798 return VOID;
1507 1799 }
1508 1800 }
1509 1801 return VOID;
1510 1802 }
1511 1803
1512 1804
1513 1805
1514 1806 static pseudo_t linearize_logical_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false)
1515 1807 {
1516 1808 struct basic_block *next = alloc_basic_block(ep, expr->pos);
1517 1809
1518 1810 if (expr->op == SPECIAL_LOGICAL_OR)
1519 1811 linearize_cond_branch(ep, expr->left, bb_true, next);
1520 1812 else
1521 1813 linearize_cond_branch(ep, expr->left, next, bb_false);
1522 1814 set_activeblock(ep, next);
1523 1815 linearize_cond_branch(ep, expr->right, bb_true, bb_false);
1524 1816 return VOID;
1525 1817 }
1526 1818
1527 1819 static pseudo_t linearize_cast(struct entrypoint *ep, struct expression *expr)
1528 1820 {
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
1529 1821 pseudo_t src;
1530 1822 struct expression *orig = expr->cast_expression;
1531 1823
1532 1824 if (!orig)
1533 1825 return VOID;
1534 1826
1535 1827 src = linearize_expression(ep, orig);
1536 1828 return cast_pseudo(ep, src, orig->ctype, expr->ctype);
1537 1829 }
1538 1830
1539 -static pseudo_t linearize_position(struct entrypoint *ep, struct expression *pos, struct access_data *ad)
1540 -{
1541 - struct expression *init_expr = pos->init_expr;
1542 -
1543 - ad->offset = pos->init_offset;
1544 - ad->source_type = base_type(init_expr->ctype);
1545 - ad->result_type = init_expr->ctype;
1546 - return linearize_initializer(ep, init_expr, ad);
1547 -}
1548 -
1549 1831 static pseudo_t linearize_initializer(struct entrypoint *ep, struct expression *initializer, struct access_data *ad)
1550 1832 {
1551 1833 switch (initializer->type) {
1552 1834 case EXPR_INITIALIZER: {
1553 1835 struct expression *expr;
1554 1836 FOR_EACH_PTR(initializer->expr_list, expr) {
1555 1837 linearize_initializer(ep, expr, ad);
1556 1838 } END_FOR_EACH_PTR(expr);
1557 1839 break;
1558 1840 }
1559 1841 case EXPR_POS:
1560 - linearize_position(ep, initializer, ad);
1842 + ad->offset = initializer->init_offset;
1843 + linearize_initializer(ep, initializer->init_expr, ad);
1561 1844 break;
1562 1845 default: {
1563 1846 pseudo_t value = linearize_expression(ep, initializer);
1564 - ad->source_type = base_type(initializer->ctype);
1565 - ad->result_type = initializer->ctype;
1847 + ad->type = initializer->ctype;
1566 1848 linearize_store_gen(ep, value, ad);
1567 1849 return value;
1568 1850 }
1569 1851 }
1570 1852
1571 1853 return VOID;
1572 1854 }
1573 1855
1574 1856 static void linearize_argument(struct entrypoint *ep, struct symbol *arg, int nr)
1575 1857 {
1576 1858 struct access_data ad = { NULL, };
1577 1859
1578 - ad.source_type = arg;
1579 - ad.result_type = arg;
1860 + ad.type = arg;
1580 1861 ad.address = symbol_pseudo(ep, arg);
1581 1862 linearize_store_gen(ep, argument_pseudo(ep, nr), &ad);
1582 - finish_address_gen(ep, &ad);
1583 1863 }
1584 1864
1585 -pseudo_t linearize_expression(struct entrypoint *ep, struct expression *expr)
1865 +static pseudo_t linearize_expression(struct entrypoint *ep, struct expression *expr)
1586 1866 {
1587 1867 if (!expr)
1588 1868 return VOID;
1589 1869
1590 1870 current_pos = expr->pos;
1591 1871 switch (expr->type) {
1592 1872 case EXPR_SYMBOL:
1593 1873 linearize_one_symbol(ep, expr->symbol);
1594 1874 return add_symbol_address(ep, expr->symbol);
1595 1875
1596 1876 case EXPR_VALUE:
1597 - return value_pseudo(expr->ctype, expr->value);
1877 + return value_pseudo(expr->value);
1598 1878
1599 - case EXPR_STRING: case EXPR_FVALUE: case EXPR_LABEL:
1879 + case EXPR_STRING:
1880 + case EXPR_LABEL:
1600 1881 return add_setval(ep, expr->ctype, expr);
1601 1882
1883 + case EXPR_FVALUE:
1884 + return add_setfval(ep, expr->ctype, expr->fvalue);
1885 +
1602 1886 case EXPR_STATEMENT:
1603 1887 return linearize_statement(ep, expr->statement);
1604 1888
1605 1889 case EXPR_CALL:
1606 1890 return linearize_call_expression(ep, expr);
1607 1891
1608 1892 case EXPR_BINOP:
1609 1893 if (expr->op == SPECIAL_LOGICAL_AND || expr->op == SPECIAL_LOGICAL_OR)
1610 1894 return linearize_binop_bool(ep, expr);
1611 1895 return linearize_binop(ep, expr);
1612 1896
1613 1897 case EXPR_LOGICAL:
1614 1898 return linearize_logical(ep, expr);
1615 1899
1616 1900 case EXPR_COMPARE:
1617 1901 return linearize_compare(ep, expr);
1618 1902
1619 1903 case EXPR_SELECT:
1620 1904 return linearize_select(ep, expr);
1621 1905
1622 1906 case EXPR_CONDITIONAL:
1623 1907 if (!expr->cond_true)
1624 1908 return linearize_short_conditional(ep, expr, expr->conditional, expr->cond_false);
1625 1909
1626 1910 return linearize_conditional(ep, expr, expr->conditional,
1627 1911 expr->cond_true, expr->cond_false);
1628 1912
1629 1913 case EXPR_COMMA:
1630 1914 linearize_expression(ep, expr->left);
1631 1915 return linearize_expression(ep, expr->right);
1632 1916
1633 1917 case EXPR_ASSIGNMENT:
1634 1918 return linearize_assignment(ep, expr);
1635 1919
1636 1920 case EXPR_PREOP:
1637 1921 return linearize_preop(ep, expr);
1638 1922
1639 1923 case EXPR_POSTOP:
1640 1924 return linearize_postop(ep, expr);
1641 1925
1642 1926 case EXPR_CAST:
1643 1927 case EXPR_FORCE_CAST:
1644 1928 case EXPR_IMPLIED_CAST:
1645 1929 return linearize_cast(ep, expr);
1646 1930
1647 1931 case EXPR_SLICE:
1648 1932 return linearize_slice(ep, expr);
1649 1933
1650 1934 case EXPR_INITIALIZER:
1651 1935 case EXPR_POS:
1652 1936 warning(expr->pos, "unexpected initializer expression (%d %d)", expr->type, expr->op);
1653 1937 return VOID;
1654 1938 default:
1655 1939 warning(expr->pos, "unknown expression (%d %d)", expr->type, expr->op);
1656 1940 return VOID;
1657 1941 }
1658 1942 return VOID;
1659 1943 }
1660 1944
1661 1945 static pseudo_t linearize_one_symbol(struct entrypoint *ep, struct symbol *sym)
1662 1946 {
1663 1947 struct access_data ad = { NULL, };
1664 1948 pseudo_t value;
1665 1949
1666 1950 if (!sym || !sym->initializer || sym->initialized)
1667 1951 return VOID;
1668 1952
1669 1953 /* We need to output these puppies some day too.. */
1670 1954 if (sym->ctype.modifiers & (MOD_STATIC | MOD_TOPLEVEL))
1671 1955 return VOID;
↓ open down ↓ |
60 lines elided |
↑ open up ↑ |
1672 1956
1673 1957 sym->initialized = 1;
1674 1958 ad.address = symbol_pseudo(ep, sym);
1675 1959
1676 1960 if (sym->initializer && !is_scalar_type(sym)) {
1677 1961 // default zero initialization [6.7.9.21]
1678 1962 // FIXME: this init the whole aggregate while
1679 1963 // only the existing fields need to be initialized.
1680 1964 // FIXME: this init the whole aggregate even if
1681 1965 // all fields arelater explicitely initialized.
1682 - struct expression *expr = sym->initializer;
1683 - ad.pos = expr->pos;
1684 - ad.result_type = sym;
1685 - ad.source_type = base_type(sym);
1966 + ad.type = sym;
1686 1967 ad.address = symbol_pseudo(ep, sym);
1687 - linearize_store_gen(ep, value_pseudo(sym, 0), &ad);
1968 + linearize_store_gen(ep, value_pseudo(0), &ad);
1688 1969 }
1689 1970
1690 1971 value = linearize_initializer(ep, sym->initializer, &ad);
1691 - finish_address_gen(ep, &ad);
1692 1972 return value;
1693 1973 }
1694 1974
1695 1975 static pseudo_t linearize_compound_statement(struct entrypoint *ep, struct statement *stmt)
1696 1976 {
1697 1977 pseudo_t pseudo;
1698 1978 struct statement *s;
1699 - struct symbol *ret = stmt->ret;
1700 1979
1701 1980 pseudo = VOID;
1702 1981 FOR_EACH_PTR(stmt->stmts, s) {
1703 1982 pseudo = linearize_statement(ep, s);
1704 1983 } END_FOR_EACH_PTR(s);
1705 1984
1706 - if (ret) {
1707 - struct basic_block *bb = add_label(ep, ret);
1708 - struct instruction *phi_node = first_instruction(bb->insns);
1985 + return pseudo;
1986 +}
1709 1987
1710 - if (!phi_node)
1711 - return pseudo;
1988 +static void add_return(struct entrypoint *ep, struct basic_block *bb, struct symbol *ctype, pseudo_t src)
1989 +{
1990 + struct instruction *phi_node = first_instruction(bb->insns);
1991 + pseudo_t phi;
1992 + if (!phi_node) {
1993 + phi_node = alloc_typed_instruction(OP_PHI, ctype);
1994 + phi_node->target = alloc_pseudo(phi_node);
1995 + phi_node->bb = bb;
1996 + add_instruction(&bb->insns, phi_node);
1997 + }
1998 + phi = alloc_phi(ep->active, src, ctype);
1999 + phi->ident = &return_ident;
2000 + use_pseudo(phi_node, phi, add_pseudo(&phi_node->phi_list, phi));
2001 +}
1712 2002
1713 - if (pseudo_list_size(phi_node->phi_list)==1) {
1714 - pseudo = first_pseudo(phi_node->phi_list);
1715 - assert(pseudo->type == PSEUDO_PHI);
1716 - return pseudo->def->src1;
2003 +static pseudo_t linearize_fn_statement(struct entrypoint *ep, struct statement *stmt)
2004 +{
2005 + struct instruction *phi_node;
2006 + struct basic_block *bb;
2007 + pseudo_t pseudo;
2008 +
2009 + pseudo = linearize_compound_statement(ep, stmt);
2010 + if (!is_void_type(stmt->ret)) { // non-void function
2011 + struct basic_block *active = ep->active;
2012 + if (active && !bb_terminated(active)) { // missing return
2013 + struct basic_block *bb_ret;
2014 + bb_ret = get_bound_block(ep, stmt->ret);
2015 + add_return(ep, bb_ret, stmt->ret, undef_pseudo());
1717 2016 }
1718 - return phi_node->target;
1719 2017 }
1720 -
2018 + bb = add_label(ep, stmt->ret);
2019 + phi_node = first_instruction(bb->insns);
2020 + if (phi_node)
2021 + pseudo = phi_node->target;
1721 2022 return pseudo;
1722 2023 }
1723 2024
1724 2025 static pseudo_t linearize_inlined_call(struct entrypoint *ep, struct statement *stmt)
1725 2026 {
1726 2027 struct instruction *insn = alloc_instruction(OP_INLINED_CALL, 0);
1727 2028 struct statement *args = stmt->args;
1728 2029 struct basic_block *bb;
1729 2030 pseudo_t pseudo;
1730 2031
1731 2032 if (args) {
1732 2033 struct symbol *sym;
1733 2034
1734 2035 concat_symbol_list(args->declaration, &ep->syms);
1735 2036 FOR_EACH_PTR(args->declaration, sym) {
1736 2037 pseudo_t value = linearize_one_symbol(ep, sym);
1737 - use_pseudo(insn, value, add_pseudo(&insn->arguments, value));
2038 + add_pseudo(&insn->arguments, value);
1738 2039 } END_FOR_EACH_PTR(sym);
1739 2040 }
1740 2041
1741 - insn->target = pseudo = linearize_compound_statement(ep, stmt);
2042 + pseudo = linearize_fn_statement(ep, stmt);
2043 + insn->target = pseudo;
2044 +
1742 2045 use_pseudo(insn, symbol_pseudo(ep, stmt->inline_fn), &insn->func);
1743 2046 bb = ep->active;
1744 - if (bb && !bb->insns)
2047 + if (!bb->insns)
1745 2048 bb->pos = stmt->pos;
1746 2049 add_one_insn(ep, insn);
1747 2050 return pseudo;
1748 2051 }
1749 2052
1750 2053 static pseudo_t linearize_context(struct entrypoint *ep, struct statement *stmt)
1751 2054 {
1752 2055 struct instruction *insn = alloc_instruction(OP_CONTEXT, 0);
1753 2056 struct expression *expr = stmt->expression;
1754 - int value = 0;
1755 2057
1756 - if (expr->type == EXPR_VALUE)
1757 - value = expr->value;
1758 -
1759 - insn->increment = value;
2058 + insn->increment = get_expression_value(expr);
1760 2059 insn->context_expr = stmt->context;
1761 2060 add_one_insn(ep, insn);
1762 2061 return VOID;
1763 2062 }
1764 2063
1765 2064 static pseudo_t linearize_range(struct entrypoint *ep, struct statement *stmt)
1766 2065 {
1767 2066 struct instruction *insn = alloc_instruction(OP_RANGE, 0);
1768 2067
1769 2068 use_pseudo(insn, linearize_expression(ep, stmt->range_expression), &insn->src1);
1770 2069 use_pseudo(insn, linearize_expression(ep, stmt->range_low), &insn->src2);
1771 2070 use_pseudo(insn, linearize_expression(ep, stmt->range_high), &insn->src3);
1772 2071 add_one_insn(ep, insn);
1773 2072 return VOID;
1774 2073 }
1775 2074
1776 2075 ALLOCATOR(asm_rules, "asm rules");
1777 2076 ALLOCATOR(asm_constraint, "asm constraints");
1778 2077
1779 2078 static void add_asm_input(struct entrypoint *ep, struct instruction *insn, struct expression *expr,
1780 2079 const char *constraint, const struct ident *ident)
1781 2080 {
1782 2081 pseudo_t pseudo = linearize_expression(ep, expr);
1783 2082 struct asm_constraint *rule = __alloc_asm_constraint(0);
1784 2083
1785 2084 rule->ident = ident;
1786 2085 rule->constraint = constraint;
1787 2086 use_pseudo(insn, pseudo, &rule->pseudo);
1788 2087 add_ptr_list(&insn->asm_rules->inputs, rule);
1789 2088 }
1790 2089
↓ open down ↓ |
21 lines elided |
↑ open up ↑ |
1791 2090 static void add_asm_output(struct entrypoint *ep, struct instruction *insn, struct expression *expr,
1792 2091 const char *constraint, const struct ident *ident)
1793 2092 {
1794 2093 struct access_data ad = { NULL, };
1795 2094 pseudo_t pseudo = alloc_pseudo(insn);
1796 2095 struct asm_constraint *rule;
1797 2096
1798 2097 if (!expr || !linearize_address_gen(ep, expr, &ad))
1799 2098 return;
1800 2099 linearize_store_gen(ep, pseudo, &ad);
1801 - finish_address_gen(ep, &ad);
1802 2100 rule = __alloc_asm_constraint(0);
1803 2101 rule->ident = ident;
1804 2102 rule->constraint = constraint;
1805 2103 use_pseudo(insn, pseudo, &rule->pseudo);
1806 2104 add_ptr_list(&insn->asm_rules->outputs, rule);
1807 2105 }
1808 2106
1809 2107 static pseudo_t linearize_asm_statement(struct entrypoint *ep, struct statement *stmt)
1810 2108 {
1811 - int state;
1812 2109 struct expression *expr;
1813 2110 struct instruction *insn;
1814 2111 struct asm_rules *rules;
1815 2112 const char *constraint;
1816 - struct ident *ident;
1817 2113
1818 2114 insn = alloc_instruction(OP_ASM, 0);
1819 2115 expr = stmt->asm_string;
1820 2116 if (!expr || expr->type != EXPR_STRING) {
1821 2117 warning(stmt->pos, "expected string in inline asm");
1822 2118 return VOID;
1823 2119 }
1824 2120 insn->string = expr->string->data;
1825 2121
1826 2122 rules = __alloc_asm_rules(0);
1827 2123 insn->asm_rules = rules;
1828 2124
1829 2125 /* Gather the inputs.. */
1830 - state = 0;
1831 - ident = NULL;
1832 - constraint = NULL;
1833 2126 FOR_EACH_PTR(stmt->asm_inputs, expr) {
1834 - switch (state) {
1835 - case 0: /* Identifier */
1836 - state = 1;
1837 - ident = (struct ident *)expr;
1838 - continue;
1839 -
1840 - case 1: /* Constraint */
1841 - state = 2;
1842 - constraint = expr ? expr->string->data : "";
1843 - continue;
1844 -
1845 - case 2: /* Expression */
1846 - state = 0;
1847 - add_asm_input(ep, insn, expr, constraint, ident);
1848 - }
2127 + constraint = expr->constraint ? expr->constraint->string->data : "";
2128 + add_asm_input(ep, insn, expr->expr, constraint, expr->name);
1849 2129 } END_FOR_EACH_PTR(expr);
1850 2130
1851 2131 add_one_insn(ep, insn);
1852 2132
1853 2133 /* Assign the outputs */
1854 - state = 0;
1855 - ident = NULL;
1856 - constraint = NULL;
1857 2134 FOR_EACH_PTR(stmt->asm_outputs, expr) {
1858 - switch (state) {
1859 - case 0: /* Identifier */
1860 - state = 1;
1861 - ident = (struct ident *)expr;
1862 - continue;
1863 -
1864 - case 1: /* Constraint */
1865 - state = 2;
1866 - constraint = expr ? expr->string->data : "";
1867 - continue;
1868 -
1869 - case 2:
1870 - state = 0;
1871 - add_asm_output(ep, insn, expr, constraint, ident);
1872 - }
2135 + constraint = expr->constraint ? expr->constraint->string->data : "";
2136 + add_asm_output(ep, insn, expr->expr, constraint, expr->name);
1873 2137 } END_FOR_EACH_PTR(expr);
1874 2138
1875 2139 return VOID;
1876 2140 }
1877 2141
1878 2142 static int multijmp_cmp(const void *_a, const void *_b)
1879 2143 {
1880 2144 const struct multijmp *a = _a;
1881 2145 const struct multijmp *b = _b;
1882 2146
1883 2147 // "default" case?
1884 2148 if (a->begin > a->end) {
1885 2149 if (b->begin > b->end)
1886 2150 return 0;
1887 2151 return 1;
1888 2152 }
1889 2153 if (b->begin > b->end)
1890 2154 return -1;
1891 2155 if (a->begin == b->begin) {
1892 2156 if (a->end == b->end)
1893 2157 return 0;
1894 2158 return (a->end < b->end) ? -1 : 1;
1895 2159 }
1896 2160 return a->begin < b->begin ? -1 : 1;
1897 2161 }
1898 2162
1899 2163 static void sort_switch_cases(struct instruction *insn)
1900 2164 {
1901 2165 sort_list((struct ptr_list **)&insn->multijmp_list, multijmp_cmp);
1902 2166 }
1903 2167
1904 2168 static pseudo_t linearize_declaration(struct entrypoint *ep, struct statement *stmt)
1905 2169 {
1906 2170 struct symbol *sym;
1907 2171
1908 2172 concat_symbol_list(stmt->declaration, &ep->syms);
↓ open down ↓ |
26 lines elided |
↑ open up ↑ |
1909 2173
1910 2174 FOR_EACH_PTR(stmt->declaration, sym) {
1911 2175 linearize_one_symbol(ep, sym);
1912 2176 } END_FOR_EACH_PTR(sym);
1913 2177 return VOID;
1914 2178 }
1915 2179
1916 2180 static pseudo_t linearize_return(struct entrypoint *ep, struct statement *stmt)
1917 2181 {
1918 2182 struct expression *expr = stmt->expression;
1919 - struct basic_block *bb_return = get_bound_block(ep, stmt->ret_target);
2183 + struct symbol *ret = stmt->ret_target;
2184 + struct basic_block *bb_return = get_bound_block(ep, ret);
1920 2185 struct basic_block *active;
1921 2186 pseudo_t src = linearize_expression(ep, expr);
1922 2187 active = ep->active;
1923 - if (active && src != VOID) {
1924 - struct instruction *phi_node = first_instruction(bb_return->insns);
1925 - pseudo_t phi;
1926 - if (!phi_node) {
1927 - phi_node = alloc_typed_instruction(OP_PHI, expr->ctype);
1928 - phi_node->target = alloc_pseudo(phi_node);
1929 - phi_node->bb = bb_return;
1930 - add_instruction(&bb_return->insns, phi_node);
1931 - }
1932 - phi = alloc_phi(active, src, type_size(expr->ctype));
1933 - phi->ident = &return_ident;
1934 - use_pseudo(phi_node, phi, add_pseudo(&phi_node->phi_list, phi));
2188 + if (active && !is_void_type(ret)) {
2189 + add_return(ep, bb_return, ret, src);
1935 2190 }
1936 2191 add_goto(ep, bb_return);
1937 2192 return VOID;
1938 2193 }
1939 2194
1940 2195 static pseudo_t linearize_switch(struct entrypoint *ep, struct statement *stmt)
1941 2196 {
1942 2197 struct symbol *sym;
1943 2198 struct instruction *switch_ins;
1944 2199 struct basic_block *switch_end = alloc_basic_block(ep, stmt->pos);
1945 2200 struct basic_block *active, *default_case;
2201 + struct expression *expr = stmt->switch_expression;
1946 2202 struct multijmp *jmp;
1947 2203 pseudo_t pseudo;
1948 2204
1949 - pseudo = linearize_expression(ep, stmt->switch_expression);
1950 -
1951 - active = ep->active;
1952 - if (!bb_reachable(active))
2205 + if (!expr || !expr->ctype)
1953 2206 return VOID;
2207 + pseudo = linearize_expression(ep, expr);
2208 + active = ep->active;
2209 + if (!active) {
2210 + active = alloc_basic_block(ep, stmt->pos);
2211 + set_activeblock(ep, active);
2212 + }
1954 2213
1955 - switch_ins = alloc_instruction(OP_SWITCH, 0);
2214 + switch_ins = alloc_typed_instruction(OP_SWITCH, expr->ctype);
1956 2215 use_pseudo(switch_ins, pseudo, &switch_ins->cond);
1957 2216 add_one_insn(ep, switch_ins);
1958 2217 finish_block(ep);
1959 2218
1960 2219 default_case = NULL;
1961 2220 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
1962 2221 struct statement *case_stmt = sym->stmt;
1963 2222 struct basic_block *bb_case = get_bound_block(ep, sym);
1964 2223
1965 2224 if (!case_stmt->case_expression) {
1966 2225 default_case = bb_case;
1967 2226 continue;
2227 + } else if (case_stmt->case_expression->type != EXPR_VALUE) {
2228 + continue;
1968 2229 } else {
1969 - int begin, end;
2230 + struct expression *case_to = case_stmt->case_to;
2231 + long long begin, end;
1970 2232
1971 2233 begin = end = case_stmt->case_expression->value;
1972 - if (case_stmt->case_to)
1973 - end = case_stmt->case_to->value;
2234 + if (case_to && case_to->type == EXPR_VALUE)
2235 + end = case_to->value;
1974 2236 if (begin > end)
1975 2237 jmp = alloc_multijmp(bb_case, end, begin);
1976 2238 else
1977 2239 jmp = alloc_multijmp(bb_case, begin, end);
1978 2240
1979 2241 }
1980 2242 add_multijmp(&switch_ins->multijmp_list, jmp);
1981 2243 add_bb(&bb_case->parents, active);
1982 2244 add_bb(&active->children, bb_case);
1983 2245 } END_FOR_EACH_PTR(sym);
1984 2246
1985 2247 bind_label(stmt->switch_break, switch_end, stmt->pos);
1986 2248
1987 2249 /* And linearize the actual statement */
1988 2250 linearize_statement(ep, stmt->switch_statement);
1989 2251 set_activeblock(ep, switch_end);
1990 2252
1991 2253 if (!default_case)
1992 2254 default_case = switch_end;
1993 2255
1994 2256 jmp = alloc_multijmp(default_case, 1, 0);
1995 2257 add_multijmp(&switch_ins->multijmp_list, jmp);
1996 2258 add_bb(&default_case->parents, active);
1997 2259 add_bb(&active->children, default_case);
1998 2260 sort_switch_cases(switch_ins);
1999 2261
2000 2262 return VOID;
2001 2263 }
2002 2264
2003 2265 static pseudo_t linearize_iterator(struct entrypoint *ep, struct statement *stmt)
2004 2266 {
2005 2267 struct statement *pre_statement = stmt->iterator_pre_statement;
2006 2268 struct expression *pre_condition = stmt->iterator_pre_condition;
2007 2269 struct statement *statement = stmt->iterator_statement;
2008 2270 struct statement *post_statement = stmt->iterator_post_statement;
2009 2271 struct expression *post_condition = stmt->iterator_post_condition;
2010 2272 struct basic_block *loop_top, *loop_body, *loop_continue, *loop_end;
2011 2273 struct symbol *sym;
2012 2274
2013 2275 FOR_EACH_PTR(stmt->iterator_syms, sym) {
2014 2276 linearize_one_symbol(ep, sym);
2015 2277 } END_FOR_EACH_PTR(sym);
2016 2278 concat_symbol_list(stmt->iterator_syms, &ep->syms);
2017 2279 linearize_statement(ep, pre_statement);
2018 2280
2019 2281 loop_body = loop_top = alloc_basic_block(ep, stmt->pos);
2020 2282 loop_continue = alloc_basic_block(ep, stmt->pos);
2021 2283 loop_end = alloc_basic_block(ep, stmt->pos);
2022 2284
2023 2285 /* An empty post-condition means that it's the same as the pre-condition */
2024 2286 if (!post_condition) {
2025 2287 loop_top = alloc_basic_block(ep, stmt->pos);
2026 2288 set_activeblock(ep, loop_top);
2027 2289 }
2028 2290
2029 2291 if (pre_condition)
2030 2292 linearize_cond_branch(ep, pre_condition, loop_body, loop_end);
2031 2293
2032 2294 bind_label(stmt->iterator_continue, loop_continue, stmt->pos);
2033 2295 bind_label(stmt->iterator_break, loop_end, stmt->pos);
2034 2296
2035 2297 set_activeblock(ep, loop_body);
2036 2298 linearize_statement(ep, statement);
2037 2299 add_goto(ep, loop_continue);
2038 2300
2039 2301 set_activeblock(ep, loop_continue);
↓ open down ↓ |
56 lines elided |
↑ open up ↑ |
2040 2302 linearize_statement(ep, post_statement);
2041 2303 if (!post_condition)
2042 2304 add_goto(ep, loop_top);
2043 2305 else
2044 2306 linearize_cond_branch(ep, post_condition, loop_top, loop_end);
2045 2307 set_activeblock(ep, loop_end);
2046 2308
2047 2309 return VOID;
2048 2310 }
2049 2311
2050 -pseudo_t linearize_statement(struct entrypoint *ep, struct statement *stmt)
2312 +static pseudo_t linearize_statement(struct entrypoint *ep, struct statement *stmt)
2051 2313 {
2052 2314 struct basic_block *bb;
2053 2315
2054 2316 if (!stmt)
2055 2317 return VOID;
2056 2318
2057 2319 bb = ep->active;
2058 2320 if (bb && !bb->insns)
2059 2321 bb->pos = stmt->pos;
2060 2322 current_pos = stmt->pos;
2061 2323
2062 2324 switch (stmt->type) {
2063 2325 case STMT_NONE:
2064 2326 break;
2065 2327
2066 2328 case STMT_DECLARATION:
2067 2329 return linearize_declaration(ep, stmt);
2068 2330
2069 2331 case STMT_CONTEXT:
2070 2332 return linearize_context(ep, stmt);
2071 2333
2072 2334 case STMT_RANGE:
2073 2335 return linearize_range(ep, stmt);
2074 2336
2075 2337 case STMT_EXPRESSION:
2076 2338 return linearize_expression(ep, stmt->expression);
2077 2339
2078 2340 case STMT_ASM:
2079 2341 return linearize_asm_statement(ep, stmt);
2080 2342
2081 2343 case STMT_RETURN:
2082 2344 return linearize_return(ep, stmt);
2083 2345
2084 2346 case STMT_CASE: {
2085 2347 add_label(ep, stmt->case_label);
2086 2348 linearize_statement(ep, stmt->case_statement);
2087 2349 break;
2088 2350 }
2089 2351
2090 2352 case STMT_LABEL: {
2091 2353 struct symbol *label = stmt->label_identifier;
2092 2354
2093 2355 if (label->used) {
2094 2356 add_label(ep, label);
2095 2357 }
2096 2358 return linearize_statement(ep, stmt->label_statement);
2097 2359 }
2098 2360
2099 2361 case STMT_GOTO: {
2100 2362 struct symbol *sym;
2101 2363 struct expression *expr;
2102 2364 struct instruction *goto_ins;
2103 2365 struct basic_block *active;
2104 2366 pseudo_t pseudo;
2105 2367
2106 2368 active = ep->active;
2107 2369 if (!bb_reachable(active))
2108 2370 break;
2109 2371
2110 2372 if (stmt->goto_label) {
2111 2373 add_goto(ep, get_bound_block(ep, stmt->goto_label));
2112 2374 break;
2113 2375 }
2114 2376
2115 2377 expr = stmt->goto_expression;
2116 2378 if (!expr)
↓ open down ↓ |
56 lines elided |
↑ open up ↑ |
2117 2379 break;
2118 2380
2119 2381 /* This can happen as part of simplification */
2120 2382 if (expr->type == EXPR_LABEL) {
2121 2383 add_goto(ep, get_bound_block(ep, expr->label_symbol));
2122 2384 break;
2123 2385 }
2124 2386
2125 2387 pseudo = linearize_expression(ep, expr);
2126 2388 goto_ins = alloc_instruction(OP_COMPUTEDGOTO, 0);
2127 - use_pseudo(goto_ins, pseudo, &goto_ins->target);
2389 + use_pseudo(goto_ins, pseudo, &goto_ins->src);
2128 2390 add_one_insn(ep, goto_ins);
2129 2391
2130 2392 FOR_EACH_PTR(stmt->target_list, sym) {
2131 2393 struct basic_block *bb_computed = get_bound_block(ep, sym);
2132 2394 struct multijmp *jmp = alloc_multijmp(bb_computed, 1, 0);
2133 2395 add_multijmp(&goto_ins->multijmp_list, jmp);
2134 2396 add_bb(&bb_computed->parents, ep->active);
2135 2397 add_bb(&active->children, bb_computed);
2136 2398 } END_FOR_EACH_PTR(sym);
2137 2399
2138 2400 finish_block(ep);
2139 2401 break;
2140 2402 }
2141 2403
2142 2404 case STMT_COMPOUND:
2143 2405 if (stmt->inline_fn)
2144 2406 return linearize_inlined_call(ep, stmt);
2145 2407 return linearize_compound_statement(ep, stmt);
2146 2408
2147 2409 /*
2148 2410 * This could take 'likely/unlikely' into account, and
2149 2411 * switch the arms around appropriately..
2150 2412 */
2151 2413 case STMT_IF: {
2152 2414 struct basic_block *bb_true, *bb_false, *endif;
2153 2415 struct expression *cond = stmt->if_conditional;
2154 2416
2155 2417 bb_true = alloc_basic_block(ep, stmt->pos);
2156 2418 bb_false = endif = alloc_basic_block(ep, stmt->pos);
2157 2419
2158 2420 linearize_cond_branch(ep, cond, bb_true, bb_false);
2159 2421
2160 2422 set_activeblock(ep, bb_true);
2161 2423 linearize_statement(ep, stmt->if_true);
2162 2424
2163 2425 if (stmt->if_false) {
2164 2426 endif = alloc_basic_block(ep, stmt->pos);
2165 2427 add_goto(ep, endif);
2166 2428 set_activeblock(ep, bb_false);
2167 2429 linearize_statement(ep, stmt->if_false);
2168 2430 }
2169 2431 set_activeblock(ep, endif);
2170 2432 break;
2171 2433 }
2172 2434
2173 2435 case STMT_SWITCH:
2174 2436 return linearize_switch(ep, stmt);
2175 2437
2176 2438 case STMT_ITERATOR:
↓ open down ↓ |
39 lines elided |
↑ open up ↑ |
2177 2439 return linearize_iterator(ep, stmt);
2178 2440
2179 2441 default:
2180 2442 break;
2181 2443 }
2182 2444 return VOID;
2183 2445 }
2184 2446
2185 2447 static struct entrypoint *linearize_fn(struct symbol *sym, struct symbol *base_type)
2186 2448 {
2449 + struct statement *stmt = base_type->stmt;
2187 2450 struct entrypoint *ep;
2188 2451 struct basic_block *bb;
2452 + struct symbol *ret_type;
2189 2453 struct symbol *arg;
2190 2454 struct instruction *entry;
2455 + struct instruction *ret;
2191 2456 pseudo_t result;
2192 2457 int i;
2193 2458
2194 - if (!base_type->stmt)
2459 + if (!stmt)
2195 2460 return NULL;
2196 2461
2197 2462 ep = alloc_entrypoint();
2198 - bb = alloc_basic_block(ep, sym->pos);
2199 -
2200 2463 ep->name = sym;
2201 2464 sym->ep = ep;
2465 + bb = alloc_basic_block(ep, sym->pos);
2202 2466 set_activeblock(ep, bb);
2203 2467
2468 + if (stmt->type == STMT_ASM) { // top-level asm
2469 + linearize_asm_statement(ep, stmt);
2470 + return ep;
2471 + }
2472 +
2204 2473 entry = alloc_instruction(OP_ENTRY, 0);
2205 2474 add_one_insn(ep, entry);
2206 2475 ep->entry = entry;
2207 2476
2208 2477 concat_symbol_list(base_type->arguments, &ep->syms);
2209 2478
2210 2479 /* FIXME!! We should do something else about varargs.. */
2211 2480 i = 0;
2212 2481 FOR_EACH_PTR(base_type->arguments, arg) {
2213 2482 linearize_argument(ep, arg, ++i);
2214 2483 } END_FOR_EACH_PTR(arg);
2215 2484
2216 - result = linearize_statement(ep, base_type->stmt);
2217 - if (bb_reachable(ep->active) && !bb_terminated(ep->active)) {
2218 - struct symbol *ret_type = base_type->ctype.base_type;
2219 - struct instruction *insn = alloc_typed_instruction(OP_RET, ret_type);
2485 + result = linearize_fn_statement(ep, stmt);
2486 + ret_type = base_type->ctype.base_type;
2487 + ret = alloc_typed_instruction(OP_RET, ret_type);
2488 + if (type_size(ret_type) > 0)
2489 + use_pseudo(ret, result, &ret->src);
2490 + add_one_insn(ep, ret);
2220 2491
2221 - if (type_size(ret_type) > 0)
2222 - use_pseudo(insn, result, &insn->src);
2223 - add_one_insn(ep, insn);
2224 - }
2225 -
2226 - if (fdump_linearize) {
2227 - if (fdump_linearize == 2)
2228 - return ep;
2229 - show_entry(ep);
2230 - }
2231 -
2232 - /*
2233 - * Do trivial flow simplification - branches to
2234 - * branches, kill dead basicblocks etc
2235 - */
2236 - kill_unreachable_bbs(ep);
2237 -
2238 - /*
2239 - * Turn symbols into pseudos
2240 - */
2241 - simplify_symbol_usage(ep);
2242 -
2243 -repeat:
2244 - /*
2245 - * Remove trivial instructions, and try to CSE
2246 - * the rest.
2247 - */
2248 - do {
2249 - cleanup_and_cse(ep);
2250 - pack_basic_blocks(ep);
2251 - } while (repeat_phase & REPEAT_CSE);
2252 -
2253 - kill_unreachable_bbs(ep);
2254 - vrfy_flow(ep);
2255 -
2256 - /* Cleanup */
2257 - clear_symbol_pseudos(ep);
2258 -
2259 - /* And track pseudo register usage */
2260 - track_pseudo_liveness(ep);
2261 -
2262 - /*
2263 - * Some flow optimizations can only effectively
2264 - * be done when we've done liveness analysis. But
2265 - * if they trigger, we need to start all over
2266 - * again
2267 - */
2268 - if (simplify_flow(ep)) {
2269 - clear_liveness(ep);
2270 - goto repeat;
2271 - }
2272 -
2273 - /* Finally, add deathnotes to pseudos now that we have them */
2274 - if (dbg_dead)
2275 - track_pseudo_death(ep);
2276 -
2492 + optimize(ep);
2277 2493 return ep;
2278 2494 }
2279 2495
2280 2496 struct entrypoint *linearize_symbol(struct symbol *sym)
2281 2497 {
2282 2498 struct symbol *base_type;
2283 2499
2284 2500 if (!sym)
2285 2501 return NULL;
2286 2502 current_pos = sym->pos;
2287 2503 base_type = sym->ctype.base_type;
2288 2504 if (!base_type)
2289 2505 return NULL;
2290 2506 if (base_type->type == SYM_FN)
2291 2507 return linearize_fn(sym, base_type);
2292 2508 return NULL;
2293 2509 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX