testsuite: cleanup result files
[smatch.git] / linearize.c
bloba9f36b82382a03b3715570f53cb5a464ce113089
1 /*
2 * Linearize - walk the statement tree (but _not_ the expressions)
3 * to generate a linear version of it and the basic blocks.
5 * NOTE! We're not interested in the actual sub-expressions yet,
6 * even though they can generate conditional branches and
7 * subroutine calls. That's all "local" behaviour.
9 * Copyright (C) 2004 Linus Torvalds
10 * Copyright (C) 2004 Christopher Li
13 #include <string.h>
14 #include <stdarg.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <assert.h>
19 #include "parse.h"
20 #include "expression.h"
21 #include "linearize.h"
22 #include "flow.h"
23 #include "target.h"
25 pseudo_t linearize_statement(struct entrypoint *ep, struct statement *stmt);
26 pseudo_t linearize_expression(struct entrypoint *ep, struct expression *expr);
28 static pseudo_t add_binary_op(struct entrypoint *ep, struct symbol *ctype, int op, pseudo_t left, pseudo_t right);
29 static pseudo_t add_setval(struct entrypoint *ep, struct symbol *ctype, struct expression *val);
30 static pseudo_t linearize_one_symbol(struct entrypoint *ep, struct symbol *sym);
32 struct access_data;
33 static pseudo_t add_load(struct entrypoint *ep, struct access_data *);
34 static pseudo_t linearize_initializer(struct entrypoint *ep, struct expression *initializer, struct access_data *);
35 static pseudo_t cast_pseudo(struct entrypoint *ep, pseudo_t src, struct symbol *from, struct symbol *to);
37 struct pseudo void_pseudo = {};
39 static struct position current_pos;
41 ALLOCATOR(pseudo_user, "pseudo_user");
43 static struct instruction *alloc_instruction(int opcode, int size)
45 struct instruction * insn = __alloc_instruction(0);
46 insn->opcode = opcode;
47 insn->size = size;
48 insn->pos = current_pos;
49 return insn;
52 static inline int type_size(struct symbol *type)
54 return type ? type->bit_size > 0 ? type->bit_size : 0 : 0;
57 static struct instruction *alloc_typed_instruction(int opcode, struct symbol *type)
59 struct instruction *insn = alloc_instruction(opcode, type_size(type));
60 insn->type = type;
61 return insn;
64 static struct entrypoint *alloc_entrypoint(void)
66 return __alloc_entrypoint(0);
69 static struct basic_block *alloc_basic_block(struct entrypoint *ep, struct position pos)
71 static int nr;
72 struct basic_block *bb = __alloc_basic_block(0);
73 bb->context = -1;
74 bb->pos = pos;
75 bb->ep = ep;
76 bb->nr = nr++;
77 return bb;
80 static struct multijmp *alloc_multijmp(struct basic_block *target, int begin, int end)
82 struct multijmp *multijmp = __alloc_multijmp(0);
83 multijmp->target = target;
84 multijmp->begin = begin;
85 multijmp->end = end;
86 return multijmp;
89 static inline int regno(pseudo_t n)
91 int retval = -1;
92 if (n && n->type == PSEUDO_REG)
93 retval = n->nr;
94 return retval;
97 const char *show_pseudo(pseudo_t pseudo)
99 static int n;
100 static char buffer[4][64];
101 char *buf;
102 int i;
104 if (!pseudo)
105 return "no pseudo";
106 if (pseudo == VOID)
107 return "VOID";
108 buf = buffer[3 & ++n];
109 switch(pseudo->type) {
110 case PSEUDO_SYM: {
111 struct symbol *sym = pseudo->sym;
112 struct expression *expr;
114 if (sym->bb_target) {
115 snprintf(buf, 64, ".L%u", sym->bb_target->nr);
116 break;
118 if (sym->ident) {
119 snprintf(buf, 64, "%s", show_ident(sym->ident));
120 break;
122 expr = sym->initializer;
123 snprintf(buf, 64, "<anon symbol:%p>", sym);
124 if (expr) {
125 switch (expr->type) {
126 case EXPR_VALUE:
127 snprintf(buf, 64, "<symbol value: %lld>", expr->value);
128 break;
129 case EXPR_STRING:
130 return show_string(expr->string);
131 default:
132 break;
135 break;
137 case PSEUDO_REG:
138 i = snprintf(buf, 64, "%%r%d", pseudo->nr);
139 if (pseudo->ident)
140 sprintf(buf+i, "(%s)", show_ident(pseudo->ident));
141 break;
142 case PSEUDO_VAL: {
143 long long value = pseudo->value;
144 if (value > 1000 || value < -1000)
145 snprintf(buf, 64, "$%#llx", value);
146 else
147 snprintf(buf, 64, "$%lld", value);
148 break;
150 case PSEUDO_ARG:
151 snprintf(buf, 64, "%%arg%d", pseudo->nr);
152 break;
153 case PSEUDO_PHI:
154 i = snprintf(buf, 64, "%%phi%d", pseudo->nr);
155 if (pseudo->ident)
156 sprintf(buf+i, "(%s)", show_ident(pseudo->ident));
157 break;
158 default:
159 snprintf(buf, 64, "<bad pseudo type %d>", pseudo->type);
161 return buf;
164 static const char *opcodes[] = {
165 [OP_BADOP] = "bad_op",
167 /* Fn entrypoint */
168 [OP_ENTRY] = "<entry-point>",
170 /* Terminator */
171 [OP_RET] = "ret",
172 [OP_BR] = "br",
173 [OP_CBR] = "cbr",
174 [OP_SWITCH] = "switch",
175 [OP_INVOKE] = "invoke",
176 [OP_COMPUTEDGOTO] = "jmp *",
177 [OP_UNWIND] = "unwind",
179 /* Binary */
180 [OP_ADD] = "add",
181 [OP_SUB] = "sub",
182 [OP_MULU] = "mulu",
183 [OP_MULS] = "muls",
184 [OP_DIVU] = "divu",
185 [OP_DIVS] = "divs",
186 [OP_MODU] = "modu",
187 [OP_MODS] = "mods",
188 [OP_SHL] = "shl",
189 [OP_LSR] = "lsr",
190 [OP_ASR] = "asr",
192 /* Logical */
193 [OP_AND] = "and",
194 [OP_OR] = "or",
195 [OP_XOR] = "xor",
196 [OP_AND_BOOL] = "and-bool",
197 [OP_OR_BOOL] = "or-bool",
199 /* Binary comparison */
200 [OP_SET_EQ] = "seteq",
201 [OP_SET_NE] = "setne",
202 [OP_SET_LE] = "setle",
203 [OP_SET_GE] = "setge",
204 [OP_SET_LT] = "setlt",
205 [OP_SET_GT] = "setgt",
206 [OP_SET_B] = "setb",
207 [OP_SET_A] = "seta",
208 [OP_SET_BE] = "setbe",
209 [OP_SET_AE] = "setae",
211 /* Uni */
212 [OP_NOT] = "not",
213 [OP_NEG] = "neg",
215 /* Special three-input */
216 [OP_SEL] = "select",
218 /* Memory */
219 [OP_MALLOC] = "malloc",
220 [OP_FREE] = "free",
221 [OP_ALLOCA] = "alloca",
222 [OP_LOAD] = "load",
223 [OP_STORE] = "store",
224 [OP_SETVAL] = "set",
225 [OP_SYMADDR] = "symaddr",
226 [OP_GET_ELEMENT_PTR] = "getelem",
228 /* Other */
229 [OP_PHI] = "phi",
230 [OP_PHISOURCE] = "phisrc",
231 [OP_CAST] = "cast",
232 [OP_SCAST] = "scast",
233 [OP_FPCAST] = "fpcast",
234 [OP_PTRCAST] = "ptrcast",
235 [OP_INLINED_CALL] = "# call",
236 [OP_CALL] = "call",
237 [OP_VANEXT] = "va_next",
238 [OP_VAARG] = "va_arg",
239 [OP_SLICE] = "slice",
240 [OP_SNOP] = "snop",
241 [OP_LNOP] = "lnop",
242 [OP_NOP] = "nop",
243 [OP_DEATHNOTE] = "dead",
244 [OP_ASM] = "asm",
246 /* Sparse tagging (line numbers, context, whatever) */
247 [OP_CONTEXT] = "context",
248 [OP_RANGE] = "range-check",
250 [OP_COPY] = "copy",
253 static char *show_asm_constraints(char *buf, const char *sep, struct asm_constraint_list *list)
255 struct asm_constraint *entry;
257 FOR_EACH_PTR(list, entry) {
258 buf += sprintf(buf, "%s\"%s\"", sep, entry->constraint);
259 if (entry->pseudo)
260 buf += sprintf(buf, " (%s)", show_pseudo(entry->pseudo));
261 if (entry->ident)
262 buf += sprintf(buf, " [%s]", show_ident(entry->ident));
263 sep = ", ";
264 } END_FOR_EACH_PTR(entry);
265 return buf;
268 static char *show_asm(char *buf, struct instruction *insn)
270 struct asm_rules *rules = insn->asm_rules;
272 buf += sprintf(buf, "\"%s\"", insn->string);
273 buf = show_asm_constraints(buf, "\n\t\tout: ", rules->outputs);
274 buf = show_asm_constraints(buf, "\n\t\tin: ", rules->inputs);
275 buf = show_asm_constraints(buf, "\n\t\tclobber: ", rules->clobbers);
276 return buf;
279 const char *show_instruction(struct instruction *insn)
281 int opcode = insn->opcode;
282 static char buffer[4096];
283 char *buf;
285 buf = buffer;
286 if (!insn->bb)
287 buf += sprintf(buf, "# ");
289 if (opcode < ARRAY_SIZE(opcodes)) {
290 const char *op = opcodes[opcode];
291 if (!op)
292 buf += sprintf(buf, "opcode:%d", opcode);
293 else
294 buf += sprintf(buf, "%s", op);
295 if (insn->size)
296 buf += sprintf(buf, ".%d", insn->size);
297 memset(buf, ' ', 20);
298 buf++;
301 if (buf < buffer + 12)
302 buf = buffer + 12;
303 switch (opcode) {
304 case OP_RET:
305 if (insn->src && insn->src != VOID)
306 buf += sprintf(buf, "%s", show_pseudo(insn->src));
307 break;
309 case OP_CBR:
310 buf += sprintf(buf, "%s, .L%u, .L%u", show_pseudo(insn->cond), insn->bb_true->nr, insn->bb_false->nr);
311 break;
313 case OP_BR:
314 buf += sprintf(buf, ".L%u", insn->bb_true->nr);
315 break;
317 case OP_SYMADDR: {
318 struct symbol *sym = insn->symbol->sym;
319 buf += sprintf(buf, "%s <- ", show_pseudo(insn->target));
321 if (!insn->bb && !sym)
322 break;
323 if (sym->bb_target) {
324 buf += sprintf(buf, ".L%u", sym->bb_target->nr);
325 break;
327 if (sym->ident) {
328 buf += sprintf(buf, "%s", show_ident(sym->ident));
329 break;
331 buf += sprintf(buf, "<anon symbol:%p>", sym);
332 break;
335 case OP_SETVAL: {
336 struct expression *expr = insn->val;
337 buf += sprintf(buf, "%s <- ", show_pseudo(insn->target));
339 if (!expr) {
340 buf += sprintf(buf, "%s", "<none>");
341 break;
344 switch (expr->type) {
345 case EXPR_VALUE:
346 buf += sprintf(buf, "%lld", expr->value);
347 break;
348 case EXPR_FVALUE:
349 buf += sprintf(buf, "%Lf", expr->fvalue);
350 break;
351 case EXPR_STRING:
352 buf += sprintf(buf, "%.40s", show_string(expr->string));
353 break;
354 case EXPR_SYMBOL:
355 buf += sprintf(buf, "%s", show_ident(expr->symbol->ident));
356 break;
357 case EXPR_LABEL:
358 buf += sprintf(buf, ".L%u", expr->symbol->bb_target->nr);
359 break;
360 default:
361 buf += sprintf(buf, "SETVAL EXPR TYPE %d", expr->type);
363 break;
365 case OP_SWITCH: {
366 struct multijmp *jmp;
367 buf += sprintf(buf, "%s", show_pseudo(insn->cond));
368 FOR_EACH_PTR(insn->multijmp_list, jmp) {
369 if (jmp->begin == jmp->end)
370 buf += sprintf(buf, ", %d -> .L%u", jmp->begin, jmp->target->nr);
371 else if (jmp->begin < jmp->end)
372 buf += sprintf(buf, ", %d ... %d -> .L%u", jmp->begin, jmp->end, jmp->target->nr);
373 else
374 buf += sprintf(buf, ", default -> .L%u", jmp->target->nr);
375 } END_FOR_EACH_PTR(jmp);
376 break;
378 case OP_COMPUTEDGOTO: {
379 struct multijmp *jmp;
380 buf += sprintf(buf, "%s", show_pseudo(insn->target));
381 FOR_EACH_PTR(insn->multijmp_list, jmp) {
382 buf += sprintf(buf, ", .L%u", jmp->target->nr);
383 } END_FOR_EACH_PTR(jmp);
384 break;
387 case OP_PHISOURCE: {
388 struct instruction *phi;
389 buf += sprintf(buf, "%s <- %s ", show_pseudo(insn->target), show_pseudo(insn->phi_src));
390 FOR_EACH_PTR(insn->phi_users, phi) {
391 buf += sprintf(buf, " (%s)", show_pseudo(phi->target));
392 } END_FOR_EACH_PTR(phi);
393 break;
396 case OP_PHI: {
397 pseudo_t phi;
398 const char *s = " <-";
399 buf += sprintf(buf, "%s", show_pseudo(insn->target));
400 FOR_EACH_PTR(insn->phi_list, phi) {
401 buf += sprintf(buf, "%s %s", s, show_pseudo(phi));
402 s = ",";
403 } END_FOR_EACH_PTR(phi);
404 break;
406 case OP_LOAD: case OP_LNOP:
407 buf += sprintf(buf, "%s <- %d[%s]", show_pseudo(insn->target), insn->offset, show_pseudo(insn->src));
408 break;
409 case OP_STORE: case OP_SNOP:
410 buf += sprintf(buf, "%s -> %d[%s]", show_pseudo(insn->target), insn->offset, show_pseudo(insn->src));
411 break;
412 case OP_INLINED_CALL:
413 case OP_CALL: {
414 struct pseudo *arg;
415 if (insn->target && insn->target != VOID)
416 buf += sprintf(buf, "%s <- ", show_pseudo(insn->target));
417 buf += sprintf(buf, "%s", show_pseudo(insn->func));
418 FOR_EACH_PTR(insn->arguments, arg) {
419 buf += sprintf(buf, ", %s", show_pseudo(arg));
420 } END_FOR_EACH_PTR(arg);
421 break;
423 case OP_CAST:
424 case OP_SCAST:
425 case OP_FPCAST:
426 case OP_PTRCAST:
427 buf += sprintf(buf, "%s <- (%d) %s",
428 show_pseudo(insn->target),
429 type_size(insn->orig_type),
430 show_pseudo(insn->src));
431 break;
432 case OP_BINARY ... OP_BINARY_END:
433 case OP_BINCMP ... OP_BINCMP_END:
434 buf += sprintf(buf, "%s <- %s, %s", show_pseudo(insn->target), show_pseudo(insn->src1), show_pseudo(insn->src2));
435 break;
437 case OP_SEL:
438 buf += sprintf(buf, "%s <- %s, %s, %s", show_pseudo(insn->target),
439 show_pseudo(insn->src1), show_pseudo(insn->src2), show_pseudo(insn->src3));
440 break;
442 case OP_SLICE:
443 buf += sprintf(buf, "%s <- %s, %d, %d", show_pseudo(insn->target), show_pseudo(insn->base), insn->from, insn->len);
444 break;
446 case OP_NOT: case OP_NEG:
447 buf += sprintf(buf, "%s <- %s", show_pseudo(insn->target), show_pseudo(insn->src1));
448 break;
450 case OP_CONTEXT:
451 buf += sprintf(buf, "%s%d", insn->check ? "check: " : "", insn->increment);
452 break;
453 case OP_RANGE:
454 buf += sprintf(buf, "%s between %s..%s", show_pseudo(insn->src1), show_pseudo(insn->src2), show_pseudo(insn->src3));
455 break;
456 case OP_NOP:
457 buf += sprintf(buf, "%s <- %s", show_pseudo(insn->target), show_pseudo(insn->src1));
458 break;
459 case OP_DEATHNOTE:
460 buf += sprintf(buf, "%s", show_pseudo(insn->target));
461 break;
462 case OP_ASM:
463 buf = show_asm(buf, insn);
464 break;
465 case OP_COPY:
466 buf += sprintf(buf, "%s <- %s", show_pseudo(insn->target), show_pseudo(insn->src));
467 break;
468 default:
469 break;
472 if (buf >= buffer + sizeof(buffer))
473 die("instruction buffer overflowed %td\n", buf - buffer);
474 do { --buf; } while (*buf == ' ');
475 *++buf = 0;
476 return buffer;
479 void show_bb(struct basic_block *bb)
481 struct instruction *insn;
483 printf(".L%u:\n", bb->nr);
484 if (verbose) {
485 pseudo_t needs, defines;
486 printf("%s:%d\n", stream_name(bb->pos.stream), bb->pos.line);
488 FOR_EACH_PTR(bb->needs, needs) {
489 struct instruction *def = needs->def;
490 if (def->opcode != OP_PHI) {
491 printf(" **uses %s (from .L%u)**\n", show_pseudo(needs), def->bb->nr);
492 } else {
493 pseudo_t phi;
494 const char *sep = " ";
495 printf(" **uses %s (from", show_pseudo(needs));
496 FOR_EACH_PTR(def->phi_list, phi) {
497 if (phi == VOID)
498 continue;
499 printf("%s(%s:.L%u)", sep, show_pseudo(phi), phi->def->bb->nr);
500 sep = ", ";
501 } END_FOR_EACH_PTR(phi);
502 printf(")**\n");
504 } END_FOR_EACH_PTR(needs);
506 FOR_EACH_PTR(bb->defines, defines) {
507 printf(" **defines %s **\n", show_pseudo(defines));
508 } END_FOR_EACH_PTR(defines);
510 if (bb->parents) {
511 struct basic_block *from;
512 FOR_EACH_PTR(bb->parents, from) {
513 printf(" **from .L%u (%s:%d:%d)**\n", from->nr,
514 stream_name(from->pos.stream), from->pos.line, from->pos.pos);
515 } END_FOR_EACH_PTR(from);
518 if (bb->children) {
519 struct basic_block *to;
520 FOR_EACH_PTR(bb->children, to) {
521 printf(" **to .L%u (%s:%d:%d)**\n", to->nr,
522 stream_name(to->pos.stream), to->pos.line, to->pos.pos);
523 } END_FOR_EACH_PTR(to);
527 FOR_EACH_PTR(bb->insns, insn) {
528 if (!insn->bb && verbose < 2)
529 continue;
530 printf("\t%s\n", show_instruction(insn));
531 } END_FOR_EACH_PTR(insn);
532 if (!bb_terminated(bb))
533 printf("\tEND\n");
536 static void show_symbol_usage(pseudo_t pseudo)
538 struct pseudo_user *pu;
540 if (pseudo) {
541 FOR_EACH_PTR(pseudo->users, pu) {
542 printf("\t%s\n", show_instruction(pu->insn));
543 } END_FOR_EACH_PTR(pu);
547 void show_entry(struct entrypoint *ep)
549 struct symbol *sym;
550 struct basic_block *bb;
552 printf("%s:\n", show_ident(ep->name->ident));
554 if (verbose) {
555 printf("ep %p: %s\n", ep, show_ident(ep->name->ident));
557 FOR_EACH_PTR(ep->syms, sym) {
558 if (!sym->pseudo)
559 continue;
560 if (!sym->pseudo->users)
561 continue;
562 printf(" sym: %p %s\n", sym, show_ident(sym->ident));
563 if (sym->ctype.modifiers & (MOD_EXTERN | MOD_STATIC | MOD_ADDRESSABLE))
564 printf("\texternal visibility\n");
565 show_symbol_usage(sym->pseudo);
566 } END_FOR_EACH_PTR(sym);
568 printf("\n");
571 FOR_EACH_PTR(ep->bbs, bb) {
572 if (!bb)
573 continue;
574 if (!bb->parents && !bb->children && !bb->insns && verbose < 2)
575 continue;
576 show_bb(bb);
577 printf("\n");
578 } END_FOR_EACH_PTR(bb);
580 printf("\n");
583 static void bind_label(struct symbol *label, struct basic_block *bb, struct position pos)
585 if (label->bb_target)
586 warning(pos, "label '%s' already bound", show_ident(label->ident));
587 label->bb_target = bb;
590 static struct basic_block * get_bound_block(struct entrypoint *ep, struct symbol *label)
592 struct basic_block *bb = label->bb_target;
594 if (!bb) {
595 bb = alloc_basic_block(ep, label->pos);
596 label->bb_target = bb;
598 return bb;
601 static void finish_block(struct entrypoint *ep)
603 struct basic_block *src = ep->active;
604 if (bb_reachable(src))
605 ep->active = NULL;
608 static void add_goto(struct entrypoint *ep, struct basic_block *dst)
610 struct basic_block *src = ep->active;
611 if (bb_reachable(src)) {
612 struct instruction *br = alloc_instruction(OP_BR, 0);
613 br->bb_true = dst;
614 add_bb(&dst->parents, src);
615 add_bb(&src->children, dst);
616 br->bb = src;
617 add_instruction(&src->insns, br);
618 ep->active = NULL;
622 static void add_one_insn(struct entrypoint *ep, struct instruction *insn)
624 struct basic_block *bb = ep->active;
626 if (bb_reachable(bb)) {
627 insn->bb = bb;
628 add_instruction(&bb->insns, insn);
632 static void set_activeblock(struct entrypoint *ep, struct basic_block *bb)
634 if (!bb_terminated(ep->active))
635 add_goto(ep, bb);
637 ep->active = bb;
638 if (bb_reachable(bb))
639 add_bb(&ep->bbs, bb);
642 static void remove_parent(struct basic_block *child, struct basic_block *parent)
644 remove_bb_from_list(&child->parents, parent, 1);
645 if (!child->parents)
646 kill_bb(child);
649 /* Change a "switch" into a branch */
650 void insert_branch(struct basic_block *bb, struct instruction *jmp, struct basic_block *target)
652 struct instruction *br, *old;
653 struct basic_block *child;
655 /* Remove the switch */
656 old = delete_last_instruction(&bb->insns);
657 assert(old == jmp);
659 br = alloc_instruction(OP_BR, 0);
660 br->bb = bb;
661 br->bb_true = target;
662 add_instruction(&bb->insns, br);
664 FOR_EACH_PTR(bb->children, child) {
665 if (child == target) {
666 target = NULL; /* Trigger just once */
667 continue;
669 DELETE_CURRENT_PTR(child);
670 remove_parent(child, bb);
671 } END_FOR_EACH_PTR(child);
672 PACK_PTR_LIST(&bb->children);
676 void insert_select(struct basic_block *bb, struct instruction *br, struct instruction *phi_node, pseudo_t if_true, pseudo_t if_false)
678 pseudo_t target;
679 struct instruction *select;
681 /* Remove the 'br' */
682 delete_last_instruction(&bb->insns);
684 select = alloc_instruction(OP_SEL, phi_node->size);
685 select->bb = bb;
687 assert(br->cond);
688 use_pseudo(select, br->cond, &select->src1);
690 target = phi_node->target;
691 assert(target->def == phi_node);
692 select->target = target;
693 target->def = select;
695 use_pseudo(select, if_true, &select->src2);
696 use_pseudo(select, if_false, &select->src3);
698 add_instruction(&bb->insns, select);
699 add_instruction(&bb->insns, br);
702 static inline int bb_empty(struct basic_block *bb)
704 return !bb->insns;
707 /* Add a label to the currently active block, return new active block */
708 static struct basic_block * add_label(struct entrypoint *ep, struct symbol *label)
710 struct basic_block *bb = label->bb_target;
712 if (bb) {
713 set_activeblock(ep, bb);
714 return bb;
716 bb = ep->active;
717 if (!bb_reachable(bb) || !bb_empty(bb)) {
718 bb = alloc_basic_block(ep, label->pos);
719 set_activeblock(ep, bb);
721 label->bb_target = bb;
722 return bb;
725 static void add_branch(struct entrypoint *ep, struct expression *expr, pseudo_t cond, struct basic_block *bb_true, struct basic_block *bb_false)
727 struct basic_block *bb = ep->active;
728 struct instruction *br;
730 if (bb_reachable(bb)) {
731 br = alloc_instruction(OP_CBR, 0);
732 use_pseudo(br, cond, &br->cond);
733 br->bb_true = bb_true;
734 br->bb_false = bb_false;
735 add_bb(&bb_true->parents, bb);
736 add_bb(&bb_false->parents, bb);
737 add_bb(&bb->children, bb_true);
738 add_bb(&bb->children, bb_false);
739 add_one_insn(ep, br);
743 /* Dummy pseudo allocator */
744 pseudo_t alloc_pseudo(struct instruction *def)
746 static int nr = 0;
747 struct pseudo * pseudo = __alloc_pseudo(0);
748 pseudo->type = PSEUDO_REG;
749 pseudo->nr = ++nr;
750 pseudo->def = def;
751 return pseudo;
754 static void clear_symbol_pseudos(struct entrypoint *ep)
756 pseudo_t pseudo;
758 FOR_EACH_PTR(ep->accesses, pseudo) {
759 pseudo->sym->pseudo = NULL;
760 } END_FOR_EACH_PTR(pseudo);
763 static pseudo_t symbol_pseudo(struct entrypoint *ep, struct symbol *sym)
765 pseudo_t pseudo;
767 if (!sym)
768 return VOID;
770 pseudo = sym->pseudo;
771 if (!pseudo) {
772 pseudo = __alloc_pseudo(0);
773 pseudo->nr = -1;
774 pseudo->type = PSEUDO_SYM;
775 pseudo->sym = sym;
776 pseudo->ident = sym->ident;
777 sym->pseudo = pseudo;
778 add_pseudo(&ep->accesses, pseudo);
780 /* Symbol pseudos have neither nr, usage nor def */
781 return pseudo;
784 pseudo_t value_pseudo(long long val)
786 #define MAX_VAL_HASH 64
787 static struct pseudo_list *prev[MAX_VAL_HASH];
788 int hash = val & (MAX_VAL_HASH-1);
789 struct pseudo_list **list = prev + hash;
790 pseudo_t pseudo;
792 FOR_EACH_PTR(*list, pseudo) {
793 if (pseudo->value == val)
794 return pseudo;
795 } END_FOR_EACH_PTR(pseudo);
797 pseudo = __alloc_pseudo(0);
798 pseudo->type = PSEUDO_VAL;
799 pseudo->value = val;
800 add_pseudo(list, pseudo);
802 /* Value pseudos have neither nr, usage nor def */
803 return pseudo;
806 static pseudo_t argument_pseudo(struct entrypoint *ep, int nr)
808 pseudo_t pseudo = __alloc_pseudo(0);
809 struct instruction *entry = ep->entry;
811 pseudo->type = PSEUDO_ARG;
812 pseudo->nr = nr;
813 pseudo->def = entry;
814 add_pseudo(&entry->arg_list, pseudo);
816 /* Argument pseudos have neither usage nor def */
817 return pseudo;
820 pseudo_t alloc_phi(struct basic_block *source, pseudo_t pseudo, int size)
822 struct instruction *insn = alloc_instruction(OP_PHISOURCE, size);
823 pseudo_t phi = __alloc_pseudo(0);
824 static int nr = 0;
826 phi->type = PSEUDO_PHI;
827 phi->nr = ++nr;
828 phi->def = insn;
830 use_pseudo(insn, pseudo, &insn->phi_src);
831 insn->bb = source;
832 insn->target = phi;
833 add_instruction(&source->insns, insn);
834 return phi;
838 * We carry the "access_data" structure around for any accesses,
839 * which simplifies things a lot. It contains all the access
840 * information in one place.
842 struct access_data {
843 struct symbol *result_type; // result ctype
844 struct symbol *source_type; // source ctype
845 pseudo_t address; // pseudo containing address ..
846 pseudo_t origval; // pseudo for original value ..
847 unsigned int offset, alignment; // byte offset
848 unsigned int bit_size, bit_offset; // which bits
849 struct position pos;
852 static void finish_address_gen(struct entrypoint *ep, struct access_data *ad)
856 static int linearize_simple_address(struct entrypoint *ep,
857 struct expression *addr,
858 struct access_data *ad)
860 if (addr->type == EXPR_SYMBOL) {
861 linearize_one_symbol(ep, addr->symbol);
862 ad->address = symbol_pseudo(ep, addr->symbol);
863 return 1;
865 if (addr->type == EXPR_BINOP) {
866 if (addr->right->type == EXPR_VALUE) {
867 if (addr->op == '+') {
868 ad->offset += get_expression_value(addr->right);
869 return linearize_simple_address(ep, addr->left, ad);
873 ad->address = linearize_expression(ep, addr);
874 return 1;
877 static struct symbol *base_type(struct symbol *sym)
879 struct symbol *base = sym;
881 if (sym) {
882 if (sym->type == SYM_NODE)
883 base = base->ctype.base_type;
884 if (base->type == SYM_BITFIELD)
885 return base->ctype.base_type;
887 return sym;
890 static int linearize_address_gen(struct entrypoint *ep,
891 struct expression *expr,
892 struct access_data *ad)
894 struct symbol *ctype = expr->ctype;
896 if (!ctype)
897 return 0;
898 ad->pos = expr->pos;
899 ad->result_type = ctype;
900 ad->source_type = base_type(ctype);
901 ad->bit_size = ctype->bit_size;
902 ad->alignment = ctype->ctype.alignment;
903 ad->bit_offset = ctype->bit_offset;
904 if (expr->type == EXPR_PREOP && expr->op == '*')
905 return linearize_simple_address(ep, expr->unop, ad);
907 warning(expr->pos, "generating address of non-lvalue (%d)", expr->type);
908 return 0;
911 static pseudo_t add_load(struct entrypoint *ep, struct access_data *ad)
913 struct instruction *insn;
914 pseudo_t new;
916 new = ad->origval;
917 if (0 && new)
918 return new;
920 insn = alloc_typed_instruction(OP_LOAD, ad->source_type);
921 new = alloc_pseudo(insn);
922 ad->origval = new;
924 insn->target = new;
925 insn->offset = ad->offset;
926 use_pseudo(insn, ad->address, &insn->src);
927 add_one_insn(ep, insn);
928 return new;
931 static void add_store(struct entrypoint *ep, struct access_data *ad, pseudo_t value)
933 struct basic_block *bb = ep->active;
935 if (bb_reachable(bb)) {
936 struct instruction *store = alloc_typed_instruction(OP_STORE, ad->source_type);
937 store->offset = ad->offset;
938 use_pseudo(store, value, &store->target);
939 use_pseudo(store, ad->address, &store->src);
940 add_one_insn(ep, store);
944 static pseudo_t linearize_store_gen(struct entrypoint *ep,
945 pseudo_t value,
946 struct access_data *ad)
948 pseudo_t store = value;
950 if (type_size(ad->source_type) != type_size(ad->result_type)) {
951 pseudo_t orig = add_load(ep, ad);
952 int shift = ad->bit_offset;
953 unsigned long long mask = (1ULL << ad->bit_size)-1;
955 if (shift) {
956 store = add_binary_op(ep, ad->source_type, OP_SHL, value, value_pseudo(shift));
957 mask <<= shift;
959 orig = add_binary_op(ep, ad->source_type, OP_AND, orig, value_pseudo(~mask));
960 store = add_binary_op(ep, ad->source_type, OP_OR, orig, store);
962 add_store(ep, ad, store);
963 return value;
966 static pseudo_t add_binary_op(struct entrypoint *ep, struct symbol *ctype, int op, pseudo_t left, pseudo_t right)
968 struct instruction *insn = alloc_typed_instruction(op, ctype);
969 pseudo_t target = alloc_pseudo(insn);
970 insn->target = target;
971 use_pseudo(insn, left, &insn->src1);
972 use_pseudo(insn, right, &insn->src2);
973 add_one_insn(ep, insn);
974 return target;
977 static pseudo_t add_setval(struct entrypoint *ep, struct symbol *ctype, struct expression *val)
979 struct instruction *insn = alloc_typed_instruction(OP_SETVAL, ctype);
980 pseudo_t target = alloc_pseudo(insn);
981 insn->target = target;
982 insn->val = val;
983 add_one_insn(ep, insn);
984 return target;
987 static pseudo_t add_symbol_address(struct entrypoint *ep, struct symbol *sym)
989 struct instruction *insn = alloc_instruction(OP_SYMADDR, bits_in_pointer);
990 pseudo_t target = alloc_pseudo(insn);
992 insn->target = target;
993 use_pseudo(insn, symbol_pseudo(ep, sym), &insn->symbol);
994 add_one_insn(ep, insn);
995 return target;
998 static pseudo_t linearize_load_gen(struct entrypoint *ep, struct access_data *ad)
1000 pseudo_t new = add_load(ep, ad);
1002 if (ad->bit_offset) {
1003 pseudo_t shift = value_pseudo(ad->bit_offset);
1004 pseudo_t newval = add_binary_op(ep, ad->source_type, OP_LSR, new, shift);
1005 new = newval;
1007 if (ad->bit_size != type_size(ad->source_type))
1008 new = cast_pseudo(ep, new, ad->source_type, ad->result_type);
1009 return new;
1012 static pseudo_t linearize_access(struct entrypoint *ep, struct expression *expr)
1014 struct access_data ad = { NULL, };
1015 pseudo_t value;
1017 if (!linearize_address_gen(ep, expr, &ad))
1018 return VOID;
1019 value = linearize_load_gen(ep, &ad);
1020 finish_address_gen(ep, &ad);
1021 return value;
1024 /* FIXME: FP */
1025 static pseudo_t linearize_inc_dec(struct entrypoint *ep, struct expression *expr, int postop)
1027 struct access_data ad = { NULL, };
1028 pseudo_t old, new, one;
1029 int op = expr->op == SPECIAL_INCREMENT ? OP_ADD : OP_SUB;
1031 if (!linearize_address_gen(ep, expr->unop, &ad))
1032 return VOID;
1034 old = linearize_load_gen(ep, &ad);
1035 one = value_pseudo(expr->op_value);
1036 new = add_binary_op(ep, expr->ctype, op, old, one);
1037 linearize_store_gen(ep, new, &ad);
1038 finish_address_gen(ep, &ad);
1039 return postop ? old : new;
1042 static pseudo_t add_uniop(struct entrypoint *ep, struct expression *expr, int op, pseudo_t src)
1044 struct instruction *insn = alloc_typed_instruction(op, expr->ctype);
1045 pseudo_t new = alloc_pseudo(insn);
1047 insn->target = new;
1048 use_pseudo(insn, src, &insn->src1);
1049 add_one_insn(ep, insn);
1050 return new;
1053 static pseudo_t linearize_slice(struct entrypoint *ep, struct expression *expr)
1055 pseudo_t pre = linearize_expression(ep, expr->base);
1056 struct instruction *insn = alloc_typed_instruction(OP_SLICE, expr->ctype);
1057 pseudo_t new = alloc_pseudo(insn);
1059 insn->target = new;
1060 insn->from = expr->r_bitpos;
1061 insn->len = expr->r_nrbits;
1062 use_pseudo(insn, pre, &insn->base);
1063 add_one_insn(ep, insn);
1064 return new;
1067 static pseudo_t linearize_regular_preop(struct entrypoint *ep, struct expression *expr)
1069 pseudo_t pre = linearize_expression(ep, expr->unop);
1070 switch (expr->op) {
1071 case '+':
1072 return pre;
1073 case '!': {
1074 pseudo_t zero = value_pseudo(0);
1075 return add_binary_op(ep, expr->ctype, OP_SET_EQ, pre, zero);
1077 case '~':
1078 return add_uniop(ep, expr, OP_NOT, pre);
1079 case '-':
1080 return add_uniop(ep, expr, OP_NEG, pre);
1082 return VOID;
1085 static pseudo_t linearize_preop(struct entrypoint *ep, struct expression *expr)
1088 * '*' is an lvalue access, and is fundamentally different
1089 * from an arithmetic operation. Maybe it should have an
1090 * expression type of its own..
1092 if (expr->op == '*')
1093 return linearize_access(ep, expr);
1094 if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
1095 return linearize_inc_dec(ep, expr, 0);
1096 return linearize_regular_preop(ep, expr);
1099 static pseudo_t linearize_postop(struct entrypoint *ep, struct expression *expr)
1101 return linearize_inc_dec(ep, expr, 1);
1105 * Casts to pointers are "less safe" than other casts, since
1106 * they imply type-unsafe accesses. "void *" is a special
1107 * case, since you can't access through it anyway without another
1108 * cast.
1110 static struct instruction *alloc_cast_instruction(struct symbol *src, struct symbol *ctype)
1112 int opcode = OP_CAST;
1113 struct symbol *base = ctype;
1115 if (src->ctype.modifiers & MOD_SIGNED)
1116 opcode = OP_SCAST;
1117 if (base->type == SYM_NODE)
1118 base = base->ctype.base_type;
1119 if (base->type == SYM_PTR) {
1120 base = base->ctype.base_type;
1121 if (base != &void_ctype)
1122 opcode = OP_PTRCAST;
1123 } else if (base->ctype.base_type == &fp_type)
1124 opcode = OP_FPCAST;
1125 return alloc_typed_instruction(opcode, ctype);
1128 static pseudo_t cast_pseudo(struct entrypoint *ep, pseudo_t src, struct symbol *from, struct symbol *to)
1130 pseudo_t result;
1131 struct instruction *insn;
1133 if (src == VOID)
1134 return VOID;
1135 if (!from || !to)
1136 return VOID;
1137 if (from->bit_size < 0 || to->bit_size < 0)
1138 return VOID;
1139 insn = alloc_cast_instruction(from, to);
1140 result = alloc_pseudo(insn);
1141 insn->target = result;
1142 insn->orig_type = from;
1143 use_pseudo(insn, src, &insn->src);
1144 add_one_insn(ep, insn);
1145 return result;
1148 static int opcode_sign(int opcode, struct symbol *ctype)
1150 if (ctype && (ctype->ctype.modifiers & MOD_SIGNED)) {
1151 switch(opcode) {
1152 case OP_MULU: case OP_DIVU: case OP_MODU: case OP_LSR:
1153 opcode++;
1156 return opcode;
1159 static pseudo_t linearize_assignment(struct entrypoint *ep, struct expression *expr)
1161 struct access_data ad = { NULL, };
1162 struct expression *target = expr->left;
1163 struct expression *src = expr->right;
1164 struct symbol *ctype;
1165 pseudo_t value;
1167 value = linearize_expression(ep, src);
1168 if (!target || !linearize_address_gen(ep, target, &ad))
1169 return value;
1170 if (expr->op != '=') {
1171 pseudo_t oldvalue = linearize_load_gen(ep, &ad);
1172 pseudo_t dst;
1173 static const int op_trans[] = {
1174 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = OP_ADD,
1175 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = OP_SUB,
1176 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = OP_MULU,
1177 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = OP_DIVU,
1178 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = OP_MODU,
1179 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = OP_SHL,
1180 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = OP_LSR,
1181 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = OP_AND,
1182 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = OP_OR,
1183 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = OP_XOR
1185 int opcode;
1187 if (!src)
1188 return VOID;
1190 ctype = src->ctype;
1191 oldvalue = cast_pseudo(ep, oldvalue, target->ctype, ctype);
1192 opcode = opcode_sign(op_trans[expr->op - SPECIAL_BASE], ctype);
1193 dst = add_binary_op(ep, ctype, opcode, oldvalue, value);
1194 value = cast_pseudo(ep, dst, ctype, expr->ctype);
1196 value = linearize_store_gen(ep, value, &ad);
1197 finish_address_gen(ep, &ad);
1198 return value;
1201 static pseudo_t linearize_call_expression(struct entrypoint *ep, struct expression *expr)
1203 struct expression *arg, *fn;
1204 struct instruction *insn = alloc_typed_instruction(OP_CALL, expr->ctype);
1205 pseudo_t retval, call;
1206 struct ctype *ctype = NULL;
1207 struct symbol *fntype;
1208 struct context *context;
1210 if (!expr->ctype) {
1211 warning(expr->pos, "call with no type!");
1212 return VOID;
1215 FOR_EACH_PTR(expr->args, arg) {
1216 pseudo_t new = linearize_expression(ep, arg);
1217 use_pseudo(insn, new, add_pseudo(&insn->arguments, new));
1218 } END_FOR_EACH_PTR(arg);
1220 fn = expr->fn;
1222 if (fn->ctype)
1223 ctype = &fn->ctype->ctype;
1225 fntype = fn->ctype;
1226 if (fntype) {
1227 if (fntype->type == SYM_NODE)
1228 fntype = fntype->ctype.base_type;
1230 insn->fntype = fntype;
1232 if (fn->type == EXPR_PREOP) {
1233 if (fn->unop->type == EXPR_SYMBOL) {
1234 struct symbol *sym = fn->unop->symbol;
1235 if (sym->ctype.base_type->type == SYM_FN)
1236 fn = fn->unop;
1239 if (fn->type == EXPR_SYMBOL) {
1240 call = symbol_pseudo(ep, fn->symbol);
1241 } else {
1242 call = linearize_expression(ep, fn);
1244 use_pseudo(insn, call, &insn->func);
1245 retval = VOID;
1246 if (expr->ctype != &void_ctype)
1247 retval = alloc_pseudo(insn);
1248 insn->target = retval;
1249 add_one_insn(ep, insn);
1251 if (ctype) {
1252 FOR_EACH_PTR(ctype->contexts, context) {
1253 int in = context->in;
1254 int out = context->out;
1255 int check = 0;
1256 int context_diff;
1257 if (in < 0) {
1258 check = 1;
1259 in = 0;
1261 if (out < 0) {
1262 check = 0;
1263 out = 0;
1265 context_diff = out - in;
1266 if (check || context_diff) {
1267 insn = alloc_instruction(OP_CONTEXT, 0);
1268 insn->increment = context_diff;
1269 insn->check = check;
1270 insn->context_expr = context->context;
1271 add_one_insn(ep, insn);
1273 } END_FOR_EACH_PTR(context);
1276 return retval;
1279 static pseudo_t linearize_binop(struct entrypoint *ep, struct expression *expr)
1281 pseudo_t src1, src2, dst;
1282 static const int opcode[] = {
1283 ['+'] = OP_ADD, ['-'] = OP_SUB,
1284 ['*'] = OP_MULU, ['/'] = OP_DIVU,
1285 ['%'] = OP_MODU, ['&'] = OP_AND,
1286 ['|'] = OP_OR, ['^'] = OP_XOR,
1287 [SPECIAL_LEFTSHIFT] = OP_SHL,
1288 [SPECIAL_RIGHTSHIFT] = OP_LSR,
1289 [SPECIAL_LOGICAL_AND] = OP_AND_BOOL,
1290 [SPECIAL_LOGICAL_OR] = OP_OR_BOOL,
1292 int op;
1294 src1 = linearize_expression(ep, expr->left);
1295 src2 = linearize_expression(ep, expr->right);
1296 op = opcode_sign(opcode[expr->op], expr->ctype);
1297 dst = add_binary_op(ep, expr->ctype, op, src1, src2);
1298 return dst;
1301 static pseudo_t linearize_logical_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false);
1303 pseudo_t linearize_cond_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false);
1305 static pseudo_t linearize_select(struct entrypoint *ep, struct expression *expr)
1307 pseudo_t cond, true, false, res;
1308 struct instruction *insn;
1310 true = linearize_expression(ep, expr->cond_true);
1311 false = linearize_expression(ep, expr->cond_false);
1312 cond = linearize_expression(ep, expr->conditional);
1314 insn = alloc_typed_instruction(OP_SEL, expr->ctype);
1315 if (!expr->cond_true)
1316 true = cond;
1317 use_pseudo(insn, cond, &insn->src1);
1318 use_pseudo(insn, true, &insn->src2);
1319 use_pseudo(insn, false, &insn->src3);
1321 res = alloc_pseudo(insn);
1322 insn->target = res;
1323 add_one_insn(ep, insn);
1324 return res;
1327 static pseudo_t add_join_conditional(struct entrypoint *ep, struct expression *expr,
1328 pseudo_t phi1, pseudo_t phi2)
1330 pseudo_t target;
1331 struct instruction *phi_node;
1333 if (phi1 == VOID)
1334 return phi2;
1335 if (phi2 == VOID)
1336 return phi1;
1338 phi_node = alloc_typed_instruction(OP_PHI, expr->ctype);
1339 use_pseudo(phi_node, phi1, add_pseudo(&phi_node->phi_list, phi1));
1340 use_pseudo(phi_node, phi2, add_pseudo(&phi_node->phi_list, phi2));
1341 phi_node->target = target = alloc_pseudo(phi_node);
1342 add_one_insn(ep, phi_node);
1343 return target;
1346 static pseudo_t linearize_short_conditional(struct entrypoint *ep, struct expression *expr,
1347 struct expression *cond,
1348 struct expression *expr_false)
1350 pseudo_t src1, src2;
1351 struct basic_block *bb_false;
1352 struct basic_block *merge = alloc_basic_block(ep, expr->pos);
1353 pseudo_t phi1, phi2;
1354 int size = type_size(expr->ctype);
1356 if (!expr_false || !ep->active)
1357 return VOID;
1359 bb_false = alloc_basic_block(ep, expr_false->pos);
1360 src1 = linearize_expression(ep, cond);
1361 phi1 = alloc_phi(ep->active, src1, size);
1362 add_branch(ep, expr, src1, merge, bb_false);
1364 set_activeblock(ep, bb_false);
1365 src2 = linearize_expression(ep, expr_false);
1366 phi2 = alloc_phi(ep->active, src2, size);
1367 set_activeblock(ep, merge);
1369 return add_join_conditional(ep, expr, phi1, phi2);
1372 static pseudo_t linearize_conditional(struct entrypoint *ep, struct expression *expr,
1373 struct expression *cond,
1374 struct expression *expr_true,
1375 struct expression *expr_false)
1377 pseudo_t src1, src2;
1378 pseudo_t phi1, phi2;
1379 struct basic_block *bb_true, *bb_false, *merge;
1380 int size = type_size(expr->ctype);
1382 if (!cond || !expr_true || !expr_false || !ep->active)
1383 return VOID;
1384 bb_true = alloc_basic_block(ep, expr_true->pos);
1385 bb_false = alloc_basic_block(ep, expr_false->pos);
1386 merge = alloc_basic_block(ep, expr->pos);
1388 linearize_cond_branch(ep, cond, bb_true, bb_false);
1390 set_activeblock(ep, bb_true);
1391 src1 = linearize_expression(ep, expr_true);
1392 phi1 = alloc_phi(ep->active, src1, size);
1393 add_goto(ep, merge);
1395 set_activeblock(ep, bb_false);
1396 src2 = linearize_expression(ep, expr_false);
1397 phi2 = alloc_phi(ep->active, src2, size);
1398 set_activeblock(ep, merge);
1400 return add_join_conditional(ep, expr, phi1, phi2);
1403 static pseudo_t linearize_logical(struct entrypoint *ep, struct expression *expr)
1405 struct expression *shortcut;
1407 shortcut = alloc_const_expression(expr->pos, expr->op == SPECIAL_LOGICAL_OR);
1408 shortcut->ctype = expr->ctype;
1409 if (expr->op == SPECIAL_LOGICAL_OR)
1410 return linearize_conditional(ep, expr, expr->left, shortcut, expr->right);
1411 return linearize_conditional(ep, expr, expr->left, expr->right, shortcut);
1414 static pseudo_t linearize_compare(struct entrypoint *ep, struct expression *expr)
1416 static const int cmpop[] = {
1417 ['>'] = OP_SET_GT, ['<'] = OP_SET_LT,
1418 [SPECIAL_EQUAL] = OP_SET_EQ,
1419 [SPECIAL_NOTEQUAL] = OP_SET_NE,
1420 [SPECIAL_GTE] = OP_SET_GE,
1421 [SPECIAL_LTE] = OP_SET_LE,
1422 [SPECIAL_UNSIGNED_LT] = OP_SET_B,
1423 [SPECIAL_UNSIGNED_GT] = OP_SET_A,
1424 [SPECIAL_UNSIGNED_LTE] = OP_SET_BE,
1425 [SPECIAL_UNSIGNED_GTE] = OP_SET_AE,
1428 pseudo_t src1 = linearize_expression(ep, expr->left);
1429 pseudo_t src2 = linearize_expression(ep, expr->right);
1430 pseudo_t dst = add_binary_op(ep, expr->ctype, cmpop[expr->op], src1, src2);
1431 return dst;
1435 pseudo_t linearize_cond_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false)
1437 pseudo_t cond;
1439 if (!expr || !bb_reachable(ep->active))
1440 return VOID;
1442 switch (expr->type) {
1444 case EXPR_STRING:
1445 case EXPR_VALUE:
1446 add_goto(ep, expr->value ? bb_true : bb_false);
1447 return VOID;
1449 case EXPR_FVALUE:
1450 add_goto(ep, expr->fvalue ? bb_true : bb_false);
1451 return VOID;
1453 case EXPR_LOGICAL:
1454 linearize_logical_branch(ep, expr, bb_true, bb_false);
1455 return VOID;
1457 case EXPR_COMPARE:
1458 cond = linearize_compare(ep, expr);
1459 add_branch(ep, expr, cond, bb_true, bb_false);
1460 break;
1462 case EXPR_PREOP:
1463 if (expr->op == '!')
1464 return linearize_cond_branch(ep, expr->unop, bb_false, bb_true);
1465 /* fall through */
1466 default: {
1467 cond = linearize_expression(ep, expr);
1468 add_branch(ep, expr, cond, bb_true, bb_false);
1470 return VOID;
1473 return VOID;
1478 static pseudo_t linearize_logical_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false)
1480 struct basic_block *next = alloc_basic_block(ep, expr->pos);
1482 if (expr->op == SPECIAL_LOGICAL_OR)
1483 linearize_cond_branch(ep, expr->left, bb_true, next);
1484 else
1485 linearize_cond_branch(ep, expr->left, next, bb_false);
1486 set_activeblock(ep, next);
1487 linearize_cond_branch(ep, expr->right, bb_true, bb_false);
1488 return VOID;
1491 static pseudo_t linearize_cast(struct entrypoint *ep, struct expression *expr)
1493 pseudo_t src;
1494 struct expression *orig = expr->cast_expression;
1496 if (!orig)
1497 return VOID;
1499 src = linearize_expression(ep, orig);
1500 return cast_pseudo(ep, src, orig->ctype, expr->ctype);
1503 static pseudo_t linearize_position(struct entrypoint *ep, struct expression *pos, struct access_data *ad)
1505 struct expression *init_expr = pos->init_expr;
1507 ad->offset = pos->init_offset;
1508 ad->source_type = base_type(init_expr->ctype);
1509 ad->result_type = init_expr->ctype;
1510 return linearize_initializer(ep, init_expr, ad);
1513 static pseudo_t linearize_initializer(struct entrypoint *ep, struct expression *initializer, struct access_data *ad)
1515 switch (initializer->type) {
1516 case EXPR_INITIALIZER: {
1517 struct expression *expr;
1518 FOR_EACH_PTR(initializer->expr_list, expr) {
1519 linearize_initializer(ep, expr, ad);
1520 } END_FOR_EACH_PTR(expr);
1521 break;
1523 case EXPR_POS:
1524 linearize_position(ep, initializer, ad);
1525 break;
1526 default: {
1527 pseudo_t value = linearize_expression(ep, initializer);
1528 ad->source_type = base_type(initializer->ctype);
1529 ad->result_type = initializer->ctype;
1530 linearize_store_gen(ep, value, ad);
1531 return value;
1535 return VOID;
1538 static void linearize_argument(struct entrypoint *ep, struct symbol *arg, int nr)
1540 struct access_data ad = { NULL, };
1542 ad.source_type = arg;
1543 ad.result_type = arg;
1544 ad.address = symbol_pseudo(ep, arg);
1545 linearize_store_gen(ep, argument_pseudo(ep, nr), &ad);
1546 finish_address_gen(ep, &ad);
1549 pseudo_t linearize_expression(struct entrypoint *ep, struct expression *expr)
1551 if (!expr)
1552 return VOID;
1554 current_pos = expr->pos;
1555 switch (expr->type) {
1556 case EXPR_SYMBOL:
1557 linearize_one_symbol(ep, expr->symbol);
1558 return add_symbol_address(ep, expr->symbol);
1560 case EXPR_VALUE:
1561 return value_pseudo(expr->value);
1563 case EXPR_STRING: case EXPR_FVALUE: case EXPR_LABEL:
1564 return add_setval(ep, expr->ctype, expr);
1566 case EXPR_STATEMENT:
1567 return linearize_statement(ep, expr->statement);
1569 case EXPR_CALL:
1570 return linearize_call_expression(ep, expr);
1572 case EXPR_BINOP:
1573 return linearize_binop(ep, expr);
1575 case EXPR_LOGICAL:
1576 return linearize_logical(ep, expr);
1578 case EXPR_COMPARE:
1579 return linearize_compare(ep, expr);
1581 case EXPR_SELECT:
1582 return linearize_select(ep, expr);
1584 case EXPR_CONDITIONAL:
1585 if (!expr->cond_true)
1586 return linearize_short_conditional(ep, expr, expr->conditional, expr->cond_false);
1588 return linearize_conditional(ep, expr, expr->conditional,
1589 expr->cond_true, expr->cond_false);
1591 case EXPR_COMMA:
1592 linearize_expression(ep, expr->left);
1593 return linearize_expression(ep, expr->right);
1595 case EXPR_ASSIGNMENT:
1596 return linearize_assignment(ep, expr);
1598 case EXPR_PREOP:
1599 return linearize_preop(ep, expr);
1601 case EXPR_POSTOP:
1602 return linearize_postop(ep, expr);
1604 case EXPR_CAST:
1605 case EXPR_FORCE_CAST:
1606 case EXPR_IMPLIED_CAST:
1607 return linearize_cast(ep, expr);
1609 case EXPR_SLICE:
1610 return linearize_slice(ep, expr);
1612 case EXPR_INITIALIZER:
1613 case EXPR_POS:
1614 warning(expr->pos, "unexpected initializer expression (%d %d)", expr->type, expr->op);
1615 return VOID;
1616 default:
1617 warning(expr->pos, "unknown expression (%d %d)", expr->type, expr->op);
1618 return VOID;
1620 return VOID;
1623 static pseudo_t linearize_one_symbol(struct entrypoint *ep, struct symbol *sym)
1625 struct access_data ad = { NULL, };
1626 pseudo_t value;
1628 if (!sym || !sym->initializer || sym->initialized)
1629 return VOID;
1631 /* We need to output these puppies some day too.. */
1632 if (sym->ctype.modifiers & (MOD_STATIC | MOD_TOPLEVEL))
1633 return VOID;
1635 sym->initialized = 1;
1636 ad.address = symbol_pseudo(ep, sym);
1637 value = linearize_initializer(ep, sym->initializer, &ad);
1638 finish_address_gen(ep, &ad);
1639 return value;
1642 static pseudo_t linearize_compound_statement(struct entrypoint *ep, struct statement *stmt)
1644 pseudo_t pseudo;
1645 struct statement *s;
1646 struct symbol *ret = stmt->ret;
1648 pseudo = VOID;
1649 FOR_EACH_PTR(stmt->stmts, s) {
1650 pseudo = linearize_statement(ep, s);
1651 } END_FOR_EACH_PTR(s);
1653 if (ret) {
1654 struct basic_block *bb = add_label(ep, ret);
1655 struct instruction *phi_node = first_instruction(bb->insns);
1657 if (!phi_node)
1658 return pseudo;
1660 if (pseudo_list_size(phi_node->phi_list)==1) {
1661 pseudo = first_pseudo(phi_node->phi_list);
1662 assert(pseudo->type == PSEUDO_PHI);
1663 return pseudo->def->src1;
1665 return phi_node->target;
1668 return pseudo;
1671 static pseudo_t linearize_inlined_call(struct entrypoint *ep, struct statement *stmt)
1673 struct instruction *insn = alloc_instruction(OP_INLINED_CALL, 0);
1674 struct statement *args = stmt->args;
1675 struct basic_block *bb;
1676 pseudo_t pseudo;
1678 if (args) {
1679 struct symbol *sym;
1681 concat_symbol_list(args->declaration, &ep->syms);
1682 FOR_EACH_PTR(args->declaration, sym) {
1683 pseudo_t value = linearize_one_symbol(ep, sym);
1684 use_pseudo(insn, value, add_pseudo(&insn->arguments, value));
1685 } END_FOR_EACH_PTR(sym);
1688 insn->target = pseudo = linearize_compound_statement(ep, stmt);
1689 use_pseudo(insn, symbol_pseudo(ep, stmt->inline_fn), &insn->func);
1690 bb = ep->active;
1691 if (bb && !bb->insns)
1692 bb->pos = stmt->pos;
1693 add_one_insn(ep, insn);
1694 return pseudo;
1697 static pseudo_t linearize_context(struct entrypoint *ep, struct statement *stmt)
1699 struct instruction *insn = alloc_instruction(OP_CONTEXT, 0);
1700 struct expression *expr = stmt->expression;
1701 int value = 0;
1703 if (expr->type == EXPR_VALUE)
1704 value = expr->value;
1706 insn->increment = value;
1707 insn->context_expr = stmt->context;
1708 add_one_insn(ep, insn);
1709 return VOID;
1712 static pseudo_t linearize_range(struct entrypoint *ep, struct statement *stmt)
1714 struct instruction *insn = alloc_instruction(OP_RANGE, 0);
1716 use_pseudo(insn, linearize_expression(ep, stmt->range_expression), &insn->src1);
1717 use_pseudo(insn, linearize_expression(ep, stmt->range_low), &insn->src2);
1718 use_pseudo(insn, linearize_expression(ep, stmt->range_high), &insn->src3);
1719 add_one_insn(ep, insn);
1720 return VOID;
1723 ALLOCATOR(asm_rules, "asm rules");
1724 ALLOCATOR(asm_constraint, "asm constraints");
1726 static void add_asm_input(struct entrypoint *ep, struct instruction *insn, struct expression *expr,
1727 const char *constraint, const struct ident *ident)
1729 pseudo_t pseudo = linearize_expression(ep, expr);
1730 struct asm_constraint *rule = __alloc_asm_constraint(0);
1732 rule->ident = ident;
1733 rule->constraint = constraint;
1734 use_pseudo(insn, pseudo, &rule->pseudo);
1735 add_ptr_list(&insn->asm_rules->inputs, rule);
1738 static void add_asm_output(struct entrypoint *ep, struct instruction *insn, struct expression *expr,
1739 const char *constraint, const struct ident *ident)
1741 struct access_data ad = { NULL, };
1742 pseudo_t pseudo = alloc_pseudo(insn);
1743 struct asm_constraint *rule;
1745 if (!expr || !linearize_address_gen(ep, expr, &ad))
1746 return;
1747 linearize_store_gen(ep, pseudo, &ad);
1748 finish_address_gen(ep, &ad);
1749 rule = __alloc_asm_constraint(0);
1750 rule->ident = ident;
1751 rule->constraint = constraint;
1752 use_pseudo(insn, pseudo, &rule->pseudo);
1753 add_ptr_list(&insn->asm_rules->outputs, rule);
1756 static pseudo_t linearize_asm_statement(struct entrypoint *ep, struct statement *stmt)
1758 int state;
1759 struct expression *expr;
1760 struct instruction *insn;
1761 struct asm_rules *rules;
1762 const char *constraint;
1763 struct ident *ident;
1765 insn = alloc_instruction(OP_ASM, 0);
1766 expr = stmt->asm_string;
1767 if (!expr || expr->type != EXPR_STRING) {
1768 warning(stmt->pos, "expected string in inline asm");
1769 return VOID;
1771 insn->string = expr->string->data;
1773 rules = __alloc_asm_rules(0);
1774 insn->asm_rules = rules;
1776 /* Gather the inputs.. */
1777 state = 0;
1778 ident = NULL;
1779 constraint = NULL;
1780 FOR_EACH_PTR(stmt->asm_inputs, expr) {
1781 switch (state) {
1782 case 0: /* Identifier */
1783 state = 1;
1784 ident = (struct ident *)expr;
1785 continue;
1787 case 1: /* Constraint */
1788 state = 2;
1789 constraint = expr ? expr->string->data : "";
1790 continue;
1792 case 2: /* Expression */
1793 state = 0;
1794 add_asm_input(ep, insn, expr, constraint, ident);
1796 } END_FOR_EACH_PTR(expr);
1798 add_one_insn(ep, insn);
1800 /* Assign the outputs */
1801 state = 0;
1802 ident = NULL;
1803 constraint = NULL;
1804 FOR_EACH_PTR(stmt->asm_outputs, expr) {
1805 switch (state) {
1806 case 0: /* Identifier */
1807 state = 1;
1808 ident = (struct ident *)expr;
1809 continue;
1811 case 1: /* Constraint */
1812 state = 2;
1813 constraint = expr ? expr->string->data : "";
1814 continue;
1816 case 2:
1817 state = 0;
1818 add_asm_output(ep, insn, expr, constraint, ident);
1820 } END_FOR_EACH_PTR(expr);
1822 return VOID;
1825 static int multijmp_cmp(const void *_a, const void *_b)
1827 const struct multijmp *a = _a;
1828 const struct multijmp *b = _b;
1830 // "default" case?
1831 if (a->begin > a->end) {
1832 if (b->begin > b->end)
1833 return 0;
1834 return 1;
1836 if (b->begin > b->end)
1837 return -1;
1838 if (a->begin == b->begin) {
1839 if (a->end == b->end)
1840 return 0;
1841 return (a->end < b->end) ? -1 : 1;
1843 return a->begin < b->begin ? -1 : 1;
1846 static void sort_switch_cases(struct instruction *insn)
1848 sort_list((struct ptr_list **)&insn->multijmp_list, multijmp_cmp);
1851 static pseudo_t linearize_declaration(struct entrypoint *ep, struct statement *stmt)
1853 struct symbol *sym;
1855 concat_symbol_list(stmt->declaration, &ep->syms);
1857 FOR_EACH_PTR(stmt->declaration, sym) {
1858 linearize_one_symbol(ep, sym);
1859 } END_FOR_EACH_PTR(sym);
1860 return VOID;
1863 static pseudo_t linearize_return(struct entrypoint *ep, struct statement *stmt)
1865 struct expression *expr = stmt->expression;
1866 struct basic_block *bb_return = get_bound_block(ep, stmt->ret_target);
1867 struct basic_block *active;
1868 pseudo_t src = linearize_expression(ep, expr);
1869 active = ep->active;
1870 if (active && src != VOID) {
1871 struct instruction *phi_node = first_instruction(bb_return->insns);
1872 pseudo_t phi;
1873 if (!phi_node) {
1874 phi_node = alloc_typed_instruction(OP_PHI, expr->ctype);
1875 phi_node->target = alloc_pseudo(phi_node);
1876 phi_node->bb = bb_return;
1877 add_instruction(&bb_return->insns, phi_node);
1879 phi = alloc_phi(active, src, type_size(expr->ctype));
1880 phi->ident = &return_ident;
1881 use_pseudo(phi_node, phi, add_pseudo(&phi_node->phi_list, phi));
1883 add_goto(ep, bb_return);
1884 return VOID;
1887 static pseudo_t linearize_switch(struct entrypoint *ep, struct statement *stmt)
1889 struct symbol *sym;
1890 struct instruction *switch_ins;
1891 struct basic_block *switch_end = alloc_basic_block(ep, stmt->pos);
1892 struct basic_block *active, *default_case;
1893 struct multijmp *jmp;
1894 pseudo_t pseudo;
1896 pseudo = linearize_expression(ep, stmt->switch_expression);
1898 active = ep->active;
1899 if (!bb_reachable(active))
1900 return VOID;
1902 switch_ins = alloc_instruction(OP_SWITCH, 0);
1903 use_pseudo(switch_ins, pseudo, &switch_ins->cond);
1904 add_one_insn(ep, switch_ins);
1905 finish_block(ep);
1907 default_case = NULL;
1908 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
1909 struct statement *case_stmt = sym->stmt;
1910 struct basic_block *bb_case = get_bound_block(ep, sym);
1912 if (!case_stmt->case_expression) {
1913 default_case = bb_case;
1914 continue;
1915 } else {
1916 int begin, end;
1918 begin = end = case_stmt->case_expression->value;
1919 if (case_stmt->case_to)
1920 end = case_stmt->case_to->value;
1921 if (begin > end)
1922 jmp = alloc_multijmp(bb_case, end, begin);
1923 else
1924 jmp = alloc_multijmp(bb_case, begin, end);
1927 add_multijmp(&switch_ins->multijmp_list, jmp);
1928 add_bb(&bb_case->parents, active);
1929 add_bb(&active->children, bb_case);
1930 } END_FOR_EACH_PTR(sym);
1932 bind_label(stmt->switch_break, switch_end, stmt->pos);
1934 /* And linearize the actual statement */
1935 linearize_statement(ep, stmt->switch_statement);
1936 set_activeblock(ep, switch_end);
1938 if (!default_case)
1939 default_case = switch_end;
1941 jmp = alloc_multijmp(default_case, 1, 0);
1942 add_multijmp(&switch_ins->multijmp_list, jmp);
1943 add_bb(&default_case->parents, active);
1944 add_bb(&active->children, default_case);
1945 sort_switch_cases(switch_ins);
1947 return VOID;
1950 static pseudo_t linearize_iterator(struct entrypoint *ep, struct statement *stmt)
1952 struct statement *pre_statement = stmt->iterator_pre_statement;
1953 struct expression *pre_condition = stmt->iterator_pre_condition;
1954 struct statement *statement = stmt->iterator_statement;
1955 struct statement *post_statement = stmt->iterator_post_statement;
1956 struct expression *post_condition = stmt->iterator_post_condition;
1957 struct basic_block *loop_top, *loop_body, *loop_continue, *loop_end;
1958 struct symbol *sym;
1960 FOR_EACH_PTR(stmt->iterator_syms, sym) {
1961 linearize_one_symbol(ep, sym);
1962 } END_FOR_EACH_PTR(sym);
1963 concat_symbol_list(stmt->iterator_syms, &ep->syms);
1964 linearize_statement(ep, pre_statement);
1966 loop_body = loop_top = alloc_basic_block(ep, stmt->pos);
1967 loop_continue = alloc_basic_block(ep, stmt->pos);
1968 loop_end = alloc_basic_block(ep, stmt->pos);
1970 /* An empty post-condition means that it's the same as the pre-condition */
1971 if (!post_condition) {
1972 loop_top = alloc_basic_block(ep, stmt->pos);
1973 set_activeblock(ep, loop_top);
1976 if (pre_condition)
1977 linearize_cond_branch(ep, pre_condition, loop_body, loop_end);
1979 bind_label(stmt->iterator_continue, loop_continue, stmt->pos);
1980 bind_label(stmt->iterator_break, loop_end, stmt->pos);
1982 set_activeblock(ep, loop_body);
1983 linearize_statement(ep, statement);
1984 add_goto(ep, loop_continue);
1986 set_activeblock(ep, loop_continue);
1987 linearize_statement(ep, post_statement);
1988 if (!post_condition)
1989 add_goto(ep, loop_top);
1990 else
1991 linearize_cond_branch(ep, post_condition, loop_top, loop_end);
1992 set_activeblock(ep, loop_end);
1994 return VOID;
1997 pseudo_t linearize_statement(struct entrypoint *ep, struct statement *stmt)
1999 struct basic_block *bb;
2001 if (!stmt)
2002 return VOID;
2004 bb = ep->active;
2005 if (bb && !bb->insns)
2006 bb->pos = stmt->pos;
2007 current_pos = stmt->pos;
2009 switch (stmt->type) {
2010 case STMT_NONE:
2011 break;
2013 case STMT_DECLARATION:
2014 return linearize_declaration(ep, stmt);
2016 case STMT_CONTEXT:
2017 return linearize_context(ep, stmt);
2019 case STMT_RANGE:
2020 return linearize_range(ep, stmt);
2022 case STMT_EXPRESSION:
2023 return linearize_expression(ep, stmt->expression);
2025 case STMT_ASM:
2026 return linearize_asm_statement(ep, stmt);
2028 case STMT_RETURN:
2029 return linearize_return(ep, stmt);
2031 case STMT_CASE: {
2032 add_label(ep, stmt->case_label);
2033 linearize_statement(ep, stmt->case_statement);
2034 break;
2037 case STMT_LABEL: {
2038 struct symbol *label = stmt->label_identifier;
2040 if (label->used) {
2041 add_label(ep, label);
2043 return linearize_statement(ep, stmt->label_statement);
2046 case STMT_GOTO: {
2047 struct symbol *sym;
2048 struct expression *expr;
2049 struct instruction *goto_ins;
2050 struct basic_block *active;
2051 pseudo_t pseudo;
2053 active = ep->active;
2054 if (!bb_reachable(active))
2055 break;
2057 if (stmt->goto_label) {
2058 add_goto(ep, get_bound_block(ep, stmt->goto_label));
2059 break;
2062 expr = stmt->goto_expression;
2063 if (!expr)
2064 break;
2066 /* This can happen as part of simplification */
2067 if (expr->type == EXPR_LABEL) {
2068 add_goto(ep, get_bound_block(ep, expr->label_symbol));
2069 break;
2072 pseudo = linearize_expression(ep, expr);
2073 goto_ins = alloc_instruction(OP_COMPUTEDGOTO, 0);
2074 use_pseudo(goto_ins, pseudo, &goto_ins->target);
2075 add_one_insn(ep, goto_ins);
2077 FOR_EACH_PTR(stmt->target_list, sym) {
2078 struct basic_block *bb_computed = get_bound_block(ep, sym);
2079 struct multijmp *jmp = alloc_multijmp(bb_computed, 1, 0);
2080 add_multijmp(&goto_ins->multijmp_list, jmp);
2081 add_bb(&bb_computed->parents, ep->active);
2082 add_bb(&active->children, bb_computed);
2083 } END_FOR_EACH_PTR(sym);
2085 finish_block(ep);
2086 break;
2089 case STMT_COMPOUND:
2090 if (stmt->inline_fn)
2091 return linearize_inlined_call(ep, stmt);
2092 return linearize_compound_statement(ep, stmt);
2095 * This could take 'likely/unlikely' into account, and
2096 * switch the arms around appropriately..
2098 case STMT_IF: {
2099 struct basic_block *bb_true, *bb_false, *endif;
2100 struct expression *cond = stmt->if_conditional;
2102 bb_true = alloc_basic_block(ep, stmt->pos);
2103 bb_false = endif = alloc_basic_block(ep, stmt->pos);
2105 linearize_cond_branch(ep, cond, bb_true, bb_false);
2107 set_activeblock(ep, bb_true);
2108 linearize_statement(ep, stmt->if_true);
2110 if (stmt->if_false) {
2111 endif = alloc_basic_block(ep, stmt->pos);
2112 add_goto(ep, endif);
2113 set_activeblock(ep, bb_false);
2114 linearize_statement(ep, stmt->if_false);
2116 set_activeblock(ep, endif);
2117 break;
2120 case STMT_SWITCH:
2121 return linearize_switch(ep, stmt);
2123 case STMT_ITERATOR:
2124 return linearize_iterator(ep, stmt);
2126 default:
2127 break;
2129 return VOID;
2132 static struct entrypoint *linearize_fn(struct symbol *sym, struct symbol *base_type)
2134 struct entrypoint *ep;
2135 struct basic_block *bb;
2136 struct symbol *arg;
2137 struct instruction *entry;
2138 pseudo_t result;
2139 int i;
2141 if (!base_type->stmt)
2142 return NULL;
2144 ep = alloc_entrypoint();
2145 bb = alloc_basic_block(ep, sym->pos);
2147 ep->name = sym;
2148 sym->ep = ep;
2149 set_activeblock(ep, bb);
2151 entry = alloc_instruction(OP_ENTRY, 0);
2152 add_one_insn(ep, entry);
2153 ep->entry = entry;
2155 concat_symbol_list(base_type->arguments, &ep->syms);
2157 /* FIXME!! We should do something else about varargs.. */
2158 i = 0;
2159 FOR_EACH_PTR(base_type->arguments, arg) {
2160 linearize_argument(ep, arg, ++i);
2161 } END_FOR_EACH_PTR(arg);
2163 result = linearize_statement(ep, base_type->stmt);
2164 if (bb_reachable(ep->active) && !bb_terminated(ep->active)) {
2165 struct symbol *ret_type = base_type->ctype.base_type;
2166 struct instruction *insn = alloc_typed_instruction(OP_RET, ret_type);
2168 if (type_size(ret_type) > 0)
2169 use_pseudo(insn, result, &insn->src);
2170 add_one_insn(ep, insn);
2174 * Do trivial flow simplification - branches to
2175 * branches, kill dead basicblocks etc
2177 kill_unreachable_bbs(ep);
2180 * Turn symbols into pseudos
2182 simplify_symbol_usage(ep);
2184 repeat:
2186 * Remove trivial instructions, and try to CSE
2187 * the rest.
2189 do {
2190 cleanup_and_cse(ep);
2191 pack_basic_blocks(ep);
2192 } while (repeat_phase & REPEAT_CSE);
2194 kill_unreachable_bbs(ep);
2195 vrfy_flow(ep);
2197 /* Cleanup */
2198 clear_symbol_pseudos(ep);
2200 /* And track pseudo register usage */
2201 track_pseudo_liveness(ep);
2204 * Some flow optimizations can only effectively
2205 * be done when we've done liveness analysis. But
2206 * if they trigger, we need to start all over
2207 * again
2209 if (simplify_flow(ep)) {
2210 clear_liveness(ep);
2211 goto repeat;
2214 /* Finally, add deathnotes to pseudos now that we have them */
2215 if (dbg_dead)
2216 track_pseudo_death(ep);
2218 return ep;
2221 struct entrypoint *linearize_symbol(struct symbol *sym)
2223 struct symbol *base_type;
2225 if (!sym)
2226 return NULL;
2227 current_pos = sym->pos;
2228 base_type = sym->ctype.base_type;
2229 if (!base_type)
2230 return NULL;
2231 if (base_type->type == SYM_FN)
2232 return linearize_fn(sym, base_type);
2233 return NULL;