14 #define DEBUG_ERROR_MESSAGES 0
15 #define DEBUG_COLOR_GRAPH 0
17 #define DEBUG_CONSISTENCY 2
19 #warning "FIXME boundary cases with small types in larger registers"
20 #warning "FIXME give clear error messages about unused variables"
22 /* Control flow graph of a loop without goto.
33 * |\ GGG HHH | continue;
61 * DFlocal(X) = { Y <- Succ(X) | idom(Y) != X }
62 * DFup(Z) = { Y <- DF(Z) | idom(Y) != X }
65 * [] == DFlocal(X) U DF(X)
68 * Dominator graph of the same nodes.
72 * BBB JJJ BBB: [ JJJ ] ( JJJ ) JJJ: [ ] ()
74 * CCC CCC: [ ] ( BBB, JJJ )
76 * DDD EEE DDD: [ ] ( BBB ) EEE: [ JJJ ] ()
78 * FFF FFF: [ ] ( BBB )
80 * GGG HHH GGG: [ ] ( BBB ) HHH: [ BBB ] ()
85 * BBB and JJJ are definitely the dominance frontier.
86 * Where do I place phi functions and how do I make that decision.
89 static void die(char *fmt, ...)
94 vfprintf(stderr, fmt, args);
101 #define MALLOC_STRONG_DEBUG
102 static void *xmalloc(size_t size, const char *name)
107 die("Cannot malloc %ld bytes to hold %s: %s\n",
108 size + 0UL, name, strerror(errno));
113 static void *xcmalloc(size_t size, const char *name)
116 buf = xmalloc(size, name);
117 memset(buf, 0, size);
121 static void xfree(const void *ptr)
126 static char *xstrdup(const char *str)
131 new = xmalloc(len + 1, "xstrdup string");
132 memcpy(new, str, len);
137 static void xchdir(const char *path)
139 if (chdir(path) != 0) {
140 die("chdir to %s failed: %s\n",
141 path, strerror(errno));
145 static int exists(const char *dirname, const char *filename)
149 if (access(filename, O_RDONLY) < 0) {
150 if ((errno != EACCES) && (errno != EROFS)) {
158 static char *slurp_file(const char *dirname, const char *filename, off_t *r_size)
162 off_t size, progress;
171 fd = open(filename, O_RDONLY);
173 die("Cannot open '%s' : %s\n",
174 filename, strerror(errno));
176 result = fstat(fd, &stats);
178 die("Cannot stat: %s: %s\n",
179 filename, strerror(errno));
181 size = stats.st_size;
183 buf = xmalloc(size +2, filename);
184 buf[size] = '\n'; /* Make certain the file is newline terminated */
185 buf[size+1] = '\0'; /* Null terminate the file for good measure */
187 while(progress < size) {
188 result = read(fd, buf + progress, size - progress);
190 if ((errno == EINTR) || (errno == EAGAIN))
192 die("read on %s of %ld bytes failed: %s\n",
193 filename, (size - progress)+ 0UL, strerror(errno));
199 die("Close of %s failed: %s\n",
200 filename, strerror(errno));
205 /* Long on the destination platform */
206 typedef unsigned long ulong_t;
210 struct file_state *prev;
211 const char *basename;
219 const char *report_name;
220 const char *report_dir;
225 struct hash_entry *ident;
233 /* I have two classes of types:
235 * Logical types. (The type the C standard says the operation is of)
237 * The operational types are:
252 * No memory is useable by the compiler.
253 * There is no floating point support.
254 * All operations take place in general purpose registers.
255 * There is one type of general purpose register.
256 * Unsigned longs are stored in that general purpose register.
259 /* Operations on general purpose registers.
276 #define OP_POS 14 /* Dummy positive operator don't use it */
286 #define OP_SLESSEQ 26
287 #define OP_ULESSEQ 27
288 #define OP_SMOREEQ 28
289 #define OP_UMOREEQ 29
291 #define OP_LFALSE 30 /* Test if the expression is logically false */
292 #define OP_LTRUE 31 /* Test if the expression is logcially true */
299 #define OP_MIN_CONST 50
300 #define OP_MAX_CONST 59
301 #define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
302 #define OP_INTCONST 50
303 #define OP_BLOBCONST 51
304 /* For OP_BLOBCONST ->type holds the layout and size
305 * information. u.blob holds a pointer to the raw binary
306 * data for the constant initializer.
308 #define OP_ADDRCONST 52
309 /* For OP_ADDRCONST ->type holds the type.
310 * MISC(0) holds the reference to the static variable.
311 * ->u.cval holds an offset from that value.
315 /* OP_WRITE moves one pseudo register to another.
316 * LHS(0) holds the destination pseudo register, which must be an OP_DECL.
317 * RHS(0) holds the psuedo to move.
321 /* OP_READ reads the value of a variable and makes
322 * it available for the pseudo operation.
323 * Useful for things like def-use chains.
324 * RHS(0) holds points to the triple to read from.
327 /* OP_COPY makes a copy of the psedo register or constant in RHS(0).
330 /* OP_PIECE returns one piece of a instruction that returns a structure.
331 * MISC(0) is the instruction
332 * u.cval is the LHS piece of the instruction to return.
335 /* OP_ASM holds a sequence of assembly instructions, the result
336 * of a C asm directive.
337 * RHS(x) holds input value x to the assembly sequence.
338 * LHS(x) holds the output value x from the assembly sequence.
339 * u.blob holds the string of assembly instructions.
343 /* OP_DEREF generates an lvalue from a pointer.
344 * RHS(0) holds the pointer value.
345 * OP_DEREF serves as a place holder to indicate all necessary
346 * checks have been done to indicate a value is an lvalue.
349 /* OP_DOT references a submember of a structure lvalue.
350 * RHS(0) holds the lvalue.
351 * ->u.field holds the name of the field we want.
353 * Not seen outside of expressions.
356 /* OP_VAL returns the value of a subexpression of the current expression.
357 * Useful for operators that have side effects.
358 * RHS(0) holds the expression.
359 * MISC(0) holds the subexpression of RHS(0) that is the
360 * value of the expression.
362 * Not seen outside of expressions.
365 /* OP_LAND performs a C logical and between RHS(0) and RHS(1).
366 * Not seen outside of expressions.
369 /* OP_LOR performs a C logical or between RHS(0) and RHS(1).
370 * Not seen outside of expressions.
373 /* OP_CODE performas a C ? : operation.
374 * RHS(0) holds the test.
375 * RHS(1) holds the expression to evaluate if the test returns true.
376 * RHS(2) holds the expression to evaluate if the test returns false.
377 * Not seen outside of expressions.
380 /* OP_COMMA performacs a C comma operation.
381 * That is RHS(0) is evaluated, then RHS(1)
382 * and the value of RHS(1) is returned.
383 * Not seen outside of expressions.
387 /* OP_CALL performs a procedure call.
388 * MISC(0) holds a pointer to the OP_LIST of a function
389 * RHS(x) holds argument x of a function
391 * Currently not seen outside of expressions.
393 #define OP_VAL_VEC 74
394 /* OP_VAL_VEC is an array of triples that are either variable
395 * or values for a structure or an array.
396 * RHS(x) holds element x of the vector.
397 * triple->type->elements holds the size of the vector.
402 /* OP_LIST Holds a list of statements, and a result value.
403 * RHS(0) holds the list of statements.
404 * MISC(0) holds the value of the statements.
407 #define OP_BRANCH 81 /* branch */
408 /* For branch instructions
409 * TARG(0) holds the branch target.
410 * RHS(0) if present holds the branch condition.
411 * ->next holds where to branch to if the branch is not taken.
412 * The branch target can only be a decl...
416 /* OP_LABEL is a triple that establishes an target for branches.
417 * ->use is the list of all branches that use this label.
421 /* OP_DECL is a triple that establishes an lvalue for assignments.
422 * ->use is a list of statements that use the variable.
426 /* OP_SDECL is a triple that establishes a variable of static
428 * ->use is a list of statements that use the variable.
429 * MISC(0) holds the initializer expression.
434 /* OP_PHI is a triple used in SSA form code.
435 * It is used when multiple code paths merge and a variable needs
436 * a single assignment from any of those code paths.
437 * The operation is a cross between OP_DECL and OP_WRITE, which
438 * is what OP_PHI is geneared from.
440 * RHS(x) points to the value from code path x
441 * The number of RHS entries is the number of control paths into the block
442 * in which OP_PHI resides. The elements of the array point to point
443 * to the variables OP_PHI is derived from.
445 * MISC(0) holds a pointer to the orginal OP_DECL node.
448 /* Architecture specific instructions */
451 #define OP_SET_EQ 102
452 #define OP_SET_NOTEQ 103
453 #define OP_SET_SLESS 104
454 #define OP_SET_ULESS 105
455 #define OP_SET_SMORE 106
456 #define OP_SET_UMORE 107
457 #define OP_SET_SLESSEQ 108
458 #define OP_SET_ULESSEQ 109
459 #define OP_SET_SMOREEQ 110
460 #define OP_SET_UMOREEQ 111
463 #define OP_JMP_EQ 113
464 #define OP_JMP_NOTEQ 114
465 #define OP_JMP_SLESS 115
466 #define OP_JMP_ULESS 116
467 #define OP_JMP_SMORE 117
468 #define OP_JMP_UMORE 118
469 #define OP_JMP_SLESSEQ 119
470 #define OP_JMP_ULESSEQ 120
471 #define OP_JMP_SMOREEQ 121
472 #define OP_JMP_UMOREEQ 122
474 /* Builtin operators that it is just simpler to use the compiler for */
492 #define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
494 #define BLOCK 8 /* Triple stores the current block */
495 unsigned char lhs, rhs, misc, targ;
498 #define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \
506 static const struct op_info table_ops[] = {
507 [OP_SMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smul"),
508 [OP_UMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umul"),
509 [OP_SDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sdiv"),
510 [OP_UDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "udiv"),
511 [OP_SMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smod"),
512 [OP_UMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umod"),
513 [OP_ADD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "add"),
514 [OP_SUB ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sub"),
515 [OP_SL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sl"),
516 [OP_USR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "usr"),
517 [OP_SSR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ssr"),
518 [OP_AND ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "and"),
519 [OP_XOR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "xor"),
520 [OP_OR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "or"),
521 [OP_POS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "pos"),
522 [OP_NEG ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "neg"),
523 [OP_INVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "invert"),
525 [OP_EQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "eq"),
526 [OP_NOTEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "noteq"),
527 [OP_SLESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sless"),
528 [OP_ULESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "uless"),
529 [OP_SMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smore"),
530 [OP_UMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umore"),
531 [OP_SLESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "slesseq"),
532 [OP_ULESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ulesseq"),
533 [OP_SMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smoreeq"),
534 [OP_UMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umoreeq"),
535 [OP_LFALSE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "lfalse"),
536 [OP_LTRUE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "ltrue"),
538 [OP_LOAD ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "load"),
539 [OP_STORE ] = OP( 1, 1, 0, 0, IMPURE | BLOCK , "store"),
541 [OP_NOOP ] = OP( 0, 0, 0, 0, PURE | BLOCK, "noop"),
543 [OP_INTCONST ] = OP( 0, 0, 0, 0, PURE | DEF, "intconst"),
544 [OP_BLOBCONST ] = OP( 0, 0, 0, 0, PURE, "blobconst"),
545 [OP_ADDRCONST ] = OP( 0, 0, 1, 0, PURE | DEF, "addrconst"),
547 [OP_WRITE ] = OP( 1, 1, 0, 0, PURE | BLOCK, "write"),
548 [OP_READ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "read"),
549 [OP_COPY ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "copy"),
550 [OP_PIECE ] = OP( 0, 0, 1, 0, PURE | DEF, "piece"),
551 [OP_ASM ] = OP(-1, -1, 0, 0, IMPURE, "asm"),
552 [OP_DEREF ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "deref"),
553 [OP_DOT ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "dot"),
555 [OP_VAL ] = OP( 0, 1, 1, 0, 0 | DEF | BLOCK, "val"),
556 [OP_LAND ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "land"),
557 [OP_LOR ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "lor"),
558 [OP_COND ] = OP( 0, 3, 0, 0, 0 | DEF | BLOCK, "cond"),
559 [OP_COMMA ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "comma"),
560 /* Call is special most it can stand in for anything so it depends on context */
561 [OP_CALL ] = OP(-1, -1, 1, 0, 0 | BLOCK, "call"),
562 /* The sizes of OP_CALL and OP_VAL_VEC depend upon context */
563 [OP_VAL_VEC ] = OP( 0, -1, 0, 0, 0 | BLOCK, "valvec"),
565 [OP_LIST ] = OP( 0, 1, 1, 0, 0 | DEF, "list"),
566 /* The number of targets for OP_BRANCH depends on context */
567 [OP_BRANCH ] = OP( 0, -1, 0, 1, PURE | BLOCK, "branch"),
568 [OP_LABEL ] = OP( 0, 0, 0, 0, PURE | BLOCK, "label"),
569 [OP_ADECL ] = OP( 0, 0, 0, 0, PURE | BLOCK, "adecl"),
570 [OP_SDECL ] = OP( 0, 0, 1, 0, PURE | BLOCK, "sdecl"),
571 /* The number of RHS elements of OP_PHI depend upon context */
572 [OP_PHI ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
574 [OP_CMP ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK, "cmp"),
575 [OP_TEST ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "test"),
576 [OP_SET_EQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_eq"),
577 [OP_SET_NOTEQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_noteq"),
578 [OP_SET_SLESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_sless"),
579 [OP_SET_ULESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_uless"),
580 [OP_SET_SMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smore"),
581 [OP_SET_UMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umore"),
582 [OP_SET_SLESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_slesseq"),
583 [OP_SET_ULESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_ulesseq"),
584 [OP_SET_SMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smoreq"),
585 [OP_SET_UMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umoreq"),
586 [OP_JMP ] = OP( 0, 0, 0, 1, PURE | BLOCK, "jmp"),
587 [OP_JMP_EQ ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_eq"),
588 [OP_JMP_NOTEQ ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_noteq"),
589 [OP_JMP_SLESS ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_sless"),
590 [OP_JMP_ULESS ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_uless"),
591 [OP_JMP_SMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_smore"),
592 [OP_JMP_UMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_umore"),
593 [OP_JMP_SLESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_slesseq"),
594 [OP_JMP_ULESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_ulesseq"),
595 [OP_JMP_SMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_smoreq"),
596 [OP_JMP_UMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_umoreq"),
598 [OP_INB ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inb"),
599 [OP_INW ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inw"),
600 [OP_INL ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inl"),
601 [OP_OUTB ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outb"),
602 [OP_OUTW ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outw"),
603 [OP_OUTL ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outl"),
604 [OP_BSF ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsf"),
605 [OP_BSR ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsr"),
606 [OP_RDMSR ] = OP( 2, 1, 0, 0, IMPURE | BLOCK, "__rdmsr"),
607 [OP_WRMSR ] = OP( 0, 3, 0, 0, IMPURE | BLOCK, "__wrmsr"),
608 [OP_HLT ] = OP( 0, 0, 0, 0, IMPURE | BLOCK, "__hlt"),
611 #define OP_MAX (sizeof(table_ops)/sizeof(table_ops[0]))
613 static const char *tops(int index)
615 static const char unknown[] = "unknown op";
619 if (index > OP_MAX) {
622 return table_ops[index].name;
629 struct triple_set *next;
630 struct triple *member;
640 const char *filename;
641 const char *function;
644 struct occurance *parent;
647 struct triple *next, *prev;
648 struct triple_set *use;
651 unsigned char template_id;
652 unsigned short sizes;
653 #define TRIPLE_LHS(SIZES) (((SIZES) >> 0) & 0x0f)
654 #define TRIPLE_RHS(SIZES) (((SIZES) >> 4) & 0x0f)
655 #define TRIPLE_MISC(SIZES) (((SIZES) >> 8) & 0x0f)
656 #define TRIPLE_TARG(SIZES) (((SIZES) >> 12) & 0x0f)
657 #define TRIPLE_SIZE(SIZES) \
658 ((((SIZES) >> 0) & 0x0f) + \
659 (((SIZES) >> 4) & 0x0f) + \
660 (((SIZES) >> 8) & 0x0f) + \
661 (((SIZES) >> 12) & 0x0f))
662 #define TRIPLE_SIZES(LHS, RHS, MISC, TARG) \
663 ((((LHS) & 0x0f) << 0) | \
664 (((RHS) & 0x0f) << 4) | \
665 (((MISC) & 0x0f) << 8) | \
666 (((TARG) & 0x0f) << 12))
667 #define TRIPLE_LHS_OFF(SIZES) (0)
668 #define TRIPLE_RHS_OFF(SIZES) (TRIPLE_LHS_OFF(SIZES) + TRIPLE_LHS(SIZES))
669 #define TRIPLE_MISC_OFF(SIZES) (TRIPLE_RHS_OFF(SIZES) + TRIPLE_RHS(SIZES))
670 #define TRIPLE_TARG_OFF(SIZES) (TRIPLE_MISC_OFF(SIZES) + TRIPLE_MISC(SIZES))
671 #define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF((PTR)->sizes) + (INDEX)])
672 #define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF((PTR)->sizes) + (INDEX)])
673 #define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF((PTR)->sizes) + (INDEX)])
674 #define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF((PTR)->sizes) + (INDEX)])
675 unsigned id; /* A scratch value and finally the register */
676 #define TRIPLE_FLAG_FLATTENED (1 << 31)
677 #define TRIPLE_FLAG_PRE_SPLIT (1 << 30)
678 #define TRIPLE_FLAG_POST_SPLIT (1 << 29)
679 struct occurance *occurance;
684 struct hash_entry *field;
685 struct asm_info *ainfo;
687 struct triple *param[2];
694 struct ins_template {
695 struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
699 struct ins_template tmpl;
704 struct block_set *next;
705 struct block *member;
708 struct block *work_next;
709 struct block *left, *right;
710 struct triple *first, *last;
712 struct block_set *use;
713 struct block_set *idominates;
714 struct block_set *domfrontier;
716 struct block_set *ipdominates;
717 struct block_set *ipdomfrontier;
725 struct hash_entry *ident;
732 struct hash_entry *ident;
738 struct hash_entry *next;
742 struct macro *sym_define;
743 struct symbol *sym_label;
744 struct symbol *sym_struct;
745 struct symbol *sym_ident;
748 #define HASH_TABLE_SIZE 2048
750 struct compile_state {
751 const char *label_prefix;
752 const char *ofilename;
754 struct file_state *file;
755 struct occurance *last_occurance;
756 const char *function;
757 struct token token[4];
758 struct hash_entry *hash_table[HASH_TABLE_SIZE];
759 struct hash_entry *i_continue;
760 struct hash_entry *i_break;
762 int if_depth, if_value;
764 struct file_state *macro_file;
765 struct triple *main_function;
766 struct block *first_block, *last_block;
773 /* visibility global/local */
774 /* static/auto duration */
775 /* typedef, register, inline */
777 #define STOR_MASK 0x000f
779 #define STOR_GLOBAL 0x0001
781 #define STOR_PERM 0x0002
782 /* Storage specifiers */
783 #define STOR_AUTO 0x0000
784 #define STOR_STATIC 0x0002
785 #define STOR_EXTERN 0x0003
786 #define STOR_REGISTER 0x0004
787 #define STOR_TYPEDEF 0x0008
788 #define STOR_INLINE 0x000c
791 #define QUAL_MASK 0x0070
792 #define QUAL_NONE 0x0000
793 #define QUAL_CONST 0x0010
794 #define QUAL_VOLATILE 0x0020
795 #define QUAL_RESTRICT 0x0040
798 #define TYPE_MASK 0x1f00
799 #define TYPE_INTEGER(TYPE) (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG))
800 #define TYPE_ARITHMETIC(TYPE) (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE))
801 #define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
802 #define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
803 #define TYPE_MKUNSIGNED(TYPE) ((TYPE) | 0x0100)
804 #define TYPE_RANK(TYPE) ((TYPE) & ~0x0100)
805 #define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER)
806 #define TYPE_DEFAULT 0x0000
807 #define TYPE_VOID 0x0100
808 #define TYPE_CHAR 0x0200
809 #define TYPE_UCHAR 0x0300
810 #define TYPE_SHORT 0x0400
811 #define TYPE_USHORT 0x0500
812 #define TYPE_INT 0x0600
813 #define TYPE_UINT 0x0700
814 #define TYPE_LONG 0x0800
815 #define TYPE_ULONG 0x0900
816 #define TYPE_LLONG 0x0a00 /* long long */
817 #define TYPE_ULLONG 0x0b00
818 #define TYPE_FLOAT 0x0c00
819 #define TYPE_DOUBLE 0x0d00
820 #define TYPE_LDOUBLE 0x0e00 /* long double */
821 #define TYPE_STRUCT 0x1000
822 #define TYPE_ENUM 0x1100
823 #define TYPE_POINTER 0x1200
825 * type->left holds the type pointed to.
827 #define TYPE_FUNCTION 0x1300
828 /* For TYPE_FUNCTION:
829 * type->left holds the return type.
830 * type->right holds the...
832 #define TYPE_PRODUCT 0x1400
833 /* TYPE_PRODUCT is a basic building block when defining structures
834 * type->left holds the type that appears first in memory.
835 * type->right holds the type that appears next in memory.
837 #define TYPE_OVERLAP 0x1500
838 /* TYPE_OVERLAP is a basic building block when defining unions
839 * type->left and type->right holds to types that overlap
840 * each other in memory.
842 #define TYPE_ARRAY 0x1600
843 /* TYPE_ARRAY is a basic building block when definitng arrays.
844 * type->left holds the type we are an array of.
845 * type-> holds the number of elements.
848 #define ELEMENT_COUNT_UNSPECIFIED (~0UL)
852 struct type *left, *right;
854 struct hash_entry *field_ident;
855 struct hash_entry *type_ident;
858 #define MAX_REGISTERS 75
859 #define MAX_REG_EQUIVS 16
860 #define REGISTER_BITS 16
861 #define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
862 #define TEMPLATE_BITS 6
863 #define MAX_TEMPLATES (1<<TEMPLATE_BITS)
866 #define REG_UNNEEDED 1
867 #define REG_VIRT0 (MAX_REGISTERS + 0)
868 #define REG_VIRT1 (MAX_REGISTERS + 1)
869 #define REG_VIRT2 (MAX_REGISTERS + 2)
870 #define REG_VIRT3 (MAX_REGISTERS + 3)
871 #define REG_VIRT4 (MAX_REGISTERS + 4)
872 #define REG_VIRT5 (MAX_REGISTERS + 5)
873 #define REG_VIRT6 (MAX_REGISTERS + 5)
874 #define REG_VIRT7 (MAX_REGISTERS + 5)
875 #define REG_VIRT8 (MAX_REGISTERS + 5)
876 #define REG_VIRT9 (MAX_REGISTERS + 5)
878 /* Provision for 8 register classes */
880 #define REGC_SHIFT REGISTER_BITS
881 #define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
882 #define REG_MASK (MAX_VIRT_REGISTERS -1)
883 #define ID_REG(ID) ((ID) & REG_MASK)
884 #define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
885 #define ID_REGCM(ID) (((ID) & REGC_MASK) >> REGC_SHIFT)
886 #define SET_REGCM(ID, REGCM) ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
887 #define SET_INFO(ID, INFO) ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
888 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
890 static unsigned arch_reg_regcm(struct compile_state *state, int reg);
891 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
892 static void arch_reg_equivs(
893 struct compile_state *state, unsigned *equiv, int reg);
894 static int arch_select_free_register(
895 struct compile_state *state, char *used, int classes);
896 static unsigned arch_regc_size(struct compile_state *state, int class);
897 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
898 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
899 static const char *arch_reg_str(int reg);
900 static struct reg_info arch_reg_constraint(
901 struct compile_state *state, struct type *type, const char *constraint);
902 static struct reg_info arch_reg_clobber(
903 struct compile_state *state, const char *clobber);
904 static struct reg_info arch_reg_lhs(struct compile_state *state,
905 struct triple *ins, int index);
906 static struct reg_info arch_reg_rhs(struct compile_state *state,
907 struct triple *ins, int index);
908 static struct triple *transform_to_arch_instruction(
909 struct compile_state *state, struct triple *ins);
913 #define DEBUG_ABORT_ON_ERROR 0x0001
914 #define DEBUG_INTERMEDIATE_CODE 0x0002
915 #define DEBUG_CONTROL_FLOW 0x0004
916 #define DEBUG_BASIC_BLOCKS 0x0008
917 #define DEBUG_FDOMINATORS 0x0010
918 #define DEBUG_RDOMINATORS 0x0020
919 #define DEBUG_TRIPLES 0x0040
920 #define DEBUG_INTERFERENCE 0x0080
921 #define DEBUG_ARCH_CODE 0x0100
922 #define DEBUG_CODE_ELIMINATION 0x0200
923 #define DEBUG_INSERTED_COPIES 0x0400
925 #define GLOBAL_SCOPE_DEPTH 1
926 #define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
928 static void compile_file(struct compile_state *old_state, const char *filename, int local);
930 static void do_cleanup(struct compile_state *state)
933 fclose(state->output);
934 unlink(state->ofilename);
938 static int get_col(struct file_state *file)
942 ptr = file->line_start;
944 for(col = 0; ptr < end; ptr++) {
949 col = (col & ~7) + 8;
955 static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
959 struct occurance *spot;
960 spot = triple->occurance;
961 while(spot->parent) {
964 fprintf(fp, "%s:%d.%d: ",
965 spot->filename, spot->line, spot->col);
971 col = get_col(state->file);
972 fprintf(fp, "%s:%d.%d: ",
973 state->file->report_name, state->file->report_line, col);
976 static void __internal_error(struct compile_state *state, struct triple *ptr,
981 loc(stderr, state, ptr);
983 fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
985 fprintf(stderr, "Internal compiler error: ");
986 vfprintf(stderr, fmt, args);
987 fprintf(stderr, "\n");
994 static void __internal_warning(struct compile_state *state, struct triple *ptr,
999 loc(stderr, state, ptr);
1000 fprintf(stderr, "Internal compiler warning: ");
1001 vfprintf(stderr, fmt, args);
1002 fprintf(stderr, "\n");
1008 static void __error(struct compile_state *state, struct triple *ptr,
1012 va_start(args, fmt);
1013 loc(stderr, state, ptr);
1014 vfprintf(stderr, fmt, args);
1016 fprintf(stderr, "\n");
1018 if (state->debug & DEBUG_ABORT_ON_ERROR) {
1024 static void __warning(struct compile_state *state, struct triple *ptr,
1028 va_start(args, fmt);
1029 loc(stderr, state, ptr);
1030 fprintf(stderr, "warning: ");
1031 vfprintf(stderr, fmt, args);
1032 fprintf(stderr, "\n");
1036 #if DEBUG_ERROR_MESSAGES
1037 # define internal_error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_error
1038 # define internal_warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_warning
1039 # define error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__error
1040 # define warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__warning
1042 # define internal_error __internal_error
1043 # define internal_warning __internal_warning
1044 # define error __error
1045 # define warning __warning
1047 #define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1049 static void valid_op(struct compile_state *state, int op)
1051 char *fmt = "invalid op: %d";
1053 internal_error(state, 0, fmt, op);
1056 internal_error(state, 0, fmt, op);
1060 static void valid_ins(struct compile_state *state, struct triple *ptr)
1062 valid_op(state, ptr->op);
1065 static void process_trigraphs(struct compile_state *state)
1067 char *src, *dest, *end;
1068 struct file_state *file;
1070 src = dest = file->buf;
1071 end = file->buf + file->size;
1072 while((end - src) >= 3) {
1073 if ((src[0] == '?') && (src[1] == '?')) {
1076 case '=': c = '#'; break;
1077 case '/': c = '\\'; break;
1078 case '\'': c = '^'; break;
1079 case '(': c = '['; break;
1080 case ')': c = ']'; break;
1081 case '!': c = '!'; break;
1082 case '<': c = '{'; break;
1083 case '>': c = '}'; break;
1084 case '-': c = '~'; break;
1101 file->size = dest - file->buf;
1104 static void splice_lines(struct compile_state *state)
1106 char *src, *dest, *end;
1107 struct file_state *file;
1109 src = dest = file->buf;
1110 end = file->buf + file->size;
1111 while((end - src) >= 2) {
1112 if ((src[0] == '\\') && (src[1] == '\n')) {
1122 file->size = dest - file->buf;
1125 static struct type void_type;
1126 static void use_triple(struct triple *used, struct triple *user)
1128 struct triple_set **ptr, *new;
1135 if ((*ptr)->member == user) {
1138 ptr = &(*ptr)->next;
1140 /* Append new to the head of the list,
1141 * copy_func and rename_block_variables
1144 new = xcmalloc(sizeof(*new), "triple_set");
1146 new->next = used->use;
1150 static void unuse_triple(struct triple *used, struct triple *unuser)
1152 struct triple_set *use, **ptr;
1159 if (use->member == unuser) {
1169 static void push_triple(struct triple *used, struct triple *user)
1171 struct triple_set *new;
1176 /* Append new to the head of the list,
1177 * it's the only sensible behavoir for a stack.
1179 new = xcmalloc(sizeof(*new), "triple_set");
1181 new->next = used->use;
1185 static void pop_triple(struct triple *used, struct triple *unuser)
1187 struct triple_set *use, **ptr;
1191 if (use->member == unuser) {
1194 /* Only free one occurance from the stack */
1203 static void put_occurance(struct occurance *occurance)
1205 occurance->count -= 1;
1206 if (occurance->count <= 0) {
1207 if (occurance->parent) {
1208 put_occurance(occurance->parent);
1214 static void get_occurance(struct occurance *occurance)
1216 occurance->count += 1;
1220 static struct occurance *new_occurance(struct compile_state *state)
1222 struct occurance *result, *last;
1223 const char *filename;
1224 const char *function;
1232 filename = state->file->report_name;
1233 line = state->file->report_line;
1234 col = get_col(state->file);
1236 if (state->function) {
1237 function = state->function;
1239 last = state->last_occurance;
1241 (last->col == col) &&
1242 (last->line == line) &&
1243 (last->function == function) &&
1244 (strcmp(last->filename, filename) == 0)) {
1245 get_occurance(last);
1249 state->last_occurance = 0;
1250 put_occurance(last);
1252 result = xmalloc(sizeof(*result), "occurance");
1254 result->filename = filename;
1255 result->function = function;
1256 result->line = line;
1259 state->last_occurance = result;
1263 static struct occurance *inline_occurance(struct compile_state *state,
1264 struct occurance *new, struct occurance *orig)
1266 struct occurance *result, *last;
1267 last = state->last_occurance;
1269 (last->parent == orig) &&
1270 (last->col == new->col) &&
1271 (last->line == new->line) &&
1272 (last->function == new->function) &&
1273 (last->filename == new->filename)) {
1274 get_occurance(last);
1278 state->last_occurance = 0;
1279 put_occurance(last);
1281 get_occurance(orig);
1282 result = xmalloc(sizeof(*result), "occurance");
1284 result->filename = new->filename;
1285 result->function = new->function;
1286 result->line = new->line;
1287 result->col = new->col;
1288 result->parent = orig;
1289 state->last_occurance = result;
1294 static struct occurance dummy_occurance = {
1296 .filename = __FILE__,
1303 /* The zero triple is used as a place holder when we are removing pointers
1304 * from a triple. Having allows certain sanity checks to pass even
1305 * when the original triple that was pointed to is gone.
1307 static struct triple zero_triple = {
1308 .next = &zero_triple,
1309 .prev = &zero_triple,
1312 .sizes = TRIPLE_SIZES(0, 0, 0, 0),
1313 .id = -1, /* An invalid id */
1314 .u = { .cval = 0, },
1315 .occurance = &dummy_occurance,
1316 .param { [0] = 0, [1] = 0, },
1320 static unsigned short triple_sizes(struct compile_state *state,
1321 int op, struct type *type, int lhs_wanted, int rhs_wanted)
1323 int lhs, rhs, misc, targ;
1324 valid_op(state, op);
1325 lhs = table_ops[op].lhs;
1326 rhs = table_ops[op].rhs;
1327 misc = table_ops[op].misc;
1328 targ = table_ops[op].targ;
1331 if (op == OP_CALL) {
1334 param = type->right;
1335 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
1337 param = param->right;
1339 if ((param->type & TYPE_MASK) != TYPE_VOID) {
1343 if ((type->left->type & TYPE_MASK) == TYPE_STRUCT) {
1344 lhs = type->left->elements;
1347 else if (op == OP_VAL_VEC) {
1348 rhs = type->elements;
1350 else if ((op == OP_BRANCH) || (op == OP_PHI)) {
1353 else if (op == OP_ASM) {
1357 if ((rhs < 0) || (rhs > MAX_RHS)) {
1358 internal_error(state, 0, "bad rhs");
1360 if ((lhs < 0) || (lhs > MAX_LHS)) {
1361 internal_error(state, 0, "bad lhs");
1363 if ((misc < 0) || (misc > MAX_MISC)) {
1364 internal_error(state, 0, "bad misc");
1366 if ((targ < 0) || (targ > MAX_TARG)) {
1367 internal_error(state, 0, "bad targs");
1369 return TRIPLE_SIZES(lhs, rhs, misc, targ);
1372 static struct triple *alloc_triple(struct compile_state *state,
1373 int op, struct type *type, int lhs, int rhs,
1374 struct occurance *occurance)
1376 size_t size, sizes, extra_count, min_count;
1378 sizes = triple_sizes(state, op, type, lhs, rhs);
1380 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
1381 extra_count = TRIPLE_SIZE(sizes);
1382 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
1384 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
1385 ret = xcmalloc(size, "tripple");
1391 ret->occurance = occurance;
1395 struct triple *dup_triple(struct compile_state *state, struct triple *src)
1398 int src_lhs, src_rhs, src_size;
1399 src_lhs = TRIPLE_LHS(src->sizes);
1400 src_rhs = TRIPLE_RHS(src->sizes);
1401 src_size = TRIPLE_SIZE(src->sizes);
1402 get_occurance(src->occurance);
1403 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
1405 memcpy(dup, src, sizeof(*src));
1406 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
1410 static struct triple *new_triple(struct compile_state *state,
1411 int op, struct type *type, int lhs, int rhs)
1414 struct occurance *occurance;
1415 occurance = new_occurance(state);
1416 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
1420 static struct triple *build_triple(struct compile_state *state,
1421 int op, struct type *type, struct triple *left, struct triple *right,
1422 struct occurance *occurance)
1426 ret = alloc_triple(state, op, type, -1, -1, occurance);
1427 count = TRIPLE_SIZE(ret->sizes);
1429 ret->param[0] = left;
1432 ret->param[1] = right;
1437 static struct triple *triple(struct compile_state *state,
1438 int op, struct type *type, struct triple *left, struct triple *right)
1442 ret = new_triple(state, op, type, -1, -1);
1443 count = TRIPLE_SIZE(ret->sizes);
1445 ret->param[0] = left;
1448 ret->param[1] = right;
1453 static struct triple *branch(struct compile_state *state,
1454 struct triple *targ, struct triple *test)
1457 ret = new_triple(state, OP_BRANCH, &void_type, -1, test?1:0);
1461 TARG(ret, 0) = targ;
1462 /* record the branch target was used */
1463 if (!targ || (targ->op != OP_LABEL)) {
1464 internal_error(state, 0, "branch not to label");
1465 use_triple(targ, ret);
1471 static void insert_triple(struct compile_state *state,
1472 struct triple *first, struct triple *ptr)
1475 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
1476 internal_error(state, ptr, "expression already used");
1479 ptr->prev = first->prev;
1480 ptr->prev->next = ptr;
1481 ptr->next->prev = ptr;
1482 if ((ptr->prev->op == OP_BRANCH) &&
1483 TRIPLE_RHS(ptr->prev->sizes)) {
1484 unuse_triple(first, ptr->prev);
1485 use_triple(ptr, ptr->prev);
1490 static int triple_stores_block(struct compile_state *state, struct triple *ins)
1492 /* This function is used to determine if u.block
1493 * is utilized to store the current block number.
1496 valid_ins(state, ins);
1497 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
1498 return stores_block;
1501 static struct block *block_of_triple(struct compile_state *state,
1504 struct triple *first;
1505 first = RHS(state->main_function, 0);
1506 while(ins != first && !triple_stores_block(state, ins)) {
1507 if (ins == ins->prev) {
1508 internal_error(state, 0, "ins == ins->prev?");
1512 if (!triple_stores_block(state, ins)) {
1513 internal_error(state, ins, "Cannot find block");
1515 return ins->u.block;
1518 static struct triple *pre_triple(struct compile_state *state,
1519 struct triple *base,
1520 int op, struct type *type, struct triple *left, struct triple *right)
1522 struct block *block;
1524 /* If I am an OP_PIECE jump to the real instruction */
1525 if (base->op == OP_PIECE) {
1526 base = MISC(base, 0);
1528 block = block_of_triple(state, base);
1529 get_occurance(base->occurance);
1530 ret = build_triple(state, op, type, left, right, base->occurance);
1531 if (triple_stores_block(state, ret)) {
1532 ret->u.block = block;
1534 insert_triple(state, base, ret);
1535 if (block->first == base) {
1541 static struct triple *post_triple(struct compile_state *state,
1542 struct triple *base,
1543 int op, struct type *type, struct triple *left, struct triple *right)
1545 struct block *block;
1548 /* If I am an OP_PIECE jump to the real instruction */
1549 if (base->op == OP_PIECE) {
1550 base = MISC(base, 0);
1552 /* If I have a left hand side skip over it */
1553 zlhs = TRIPLE_LHS(base->sizes);
1554 if (zlhs && (base->op != OP_WRITE) && (base->op != OP_STORE)) {
1555 base = LHS(base, zlhs - 1);
1558 block = block_of_triple(state, base);
1559 get_occurance(base->occurance);
1560 ret = build_triple(state, op, type, left, right, base->occurance);
1561 if (triple_stores_block(state, ret)) {
1562 ret->u.block = block;
1564 insert_triple(state, base->next, ret);
1565 if (block->last == base) {
1571 static struct triple *label(struct compile_state *state)
1573 /* Labels don't get a type */
1574 struct triple *result;
1575 result = triple(state, OP_LABEL, &void_type, 0, 0);
1579 static void display_triple(FILE *fp, struct triple *ins)
1581 struct occurance *ptr;
1585 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
1588 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
1591 reg = arch_reg_str(ID_REG(ins->id));
1592 if (ins->op == OP_INTCONST) {
1593 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s <0x%08lx> ",
1594 ins, pre, post, reg, ins->template_id, tops(ins->op),
1597 else if (ins->op == OP_ADDRCONST) {
1598 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s %-10p <0x%08lx>",
1599 ins, pre, post, reg, ins->template_id, tops(ins->op),
1600 MISC(ins, 0), ins->u.cval);
1604 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s",
1605 ins, pre, post, reg, ins->template_id, tops(ins->op));
1606 count = TRIPLE_SIZE(ins->sizes);
1607 for(i = 0; i < count; i++) {
1608 fprintf(fp, " %-10p", ins->param[i]);
1615 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
1616 fprintf(fp, " %s,%s:%d.%d",
1626 static int triple_is_pure(struct compile_state *state, struct triple *ins)
1628 /* Does the triple have no side effects.
1629 * I.e. Rexecuting the triple with the same arguments
1630 * gives the same value.
1633 valid_ins(state, ins);
1634 pure = PURE_BITS(table_ops[ins->op].flags);
1635 if ((pure != PURE) && (pure != IMPURE)) {
1636 internal_error(state, 0, "Purity of %s not known\n",
1639 return pure == PURE;
1642 static int triple_is_branch(struct compile_state *state, struct triple *ins)
1644 /* This function is used to determine which triples need
1648 valid_ins(state, ins);
1649 is_branch = (table_ops[ins->op].targ != 0);
1653 static int triple_is_def(struct compile_state *state, struct triple *ins)
1655 /* This function is used to determine which triples need
1659 valid_ins(state, ins);
1660 is_def = (table_ops[ins->op].flags & DEF) == DEF;
1664 static struct triple **triple_iter(struct compile_state *state,
1665 size_t count, struct triple **vector,
1666 struct triple *ins, struct triple **last)
1668 struct triple **ret;
1674 else if ((last >= vector) && (last < (vector + count - 1))) {
1682 static struct triple **triple_lhs(struct compile_state *state,
1683 struct triple *ins, struct triple **last)
1685 return triple_iter(state, TRIPLE_LHS(ins->sizes), &LHS(ins,0),
1689 static struct triple **triple_rhs(struct compile_state *state,
1690 struct triple *ins, struct triple **last)
1692 return triple_iter(state, TRIPLE_RHS(ins->sizes), &RHS(ins,0),
1696 static struct triple **triple_misc(struct compile_state *state,
1697 struct triple *ins, struct triple **last)
1699 return triple_iter(state, TRIPLE_MISC(ins->sizes), &MISC(ins,0),
1703 static struct triple **triple_targ(struct compile_state *state,
1704 struct triple *ins, struct triple **last)
1707 struct triple **ret, **vector;
1709 count = TRIPLE_TARG(ins->sizes);
1710 vector = &TARG(ins, 0);
1715 else if ((last >= vector) && (last < (vector + count - 1))) {
1718 else if ((last == (vector + count - 1)) &&
1719 TRIPLE_RHS(ins->sizes)) {
1727 static void verify_use(struct compile_state *state,
1728 struct triple *user, struct triple *used)
1731 size = TRIPLE_SIZE(user->sizes);
1732 for(i = 0; i < size; i++) {
1733 if (user->param[i] == used) {
1737 if (triple_is_branch(state, user)) {
1738 if (user->next == used) {
1743 internal_error(state, user, "%s(%p) does not use %s(%p)",
1744 tops(user->op), user, tops(used->op), used);
1748 static int find_rhs_use(struct compile_state *state,
1749 struct triple *user, struct triple *used)
1751 struct triple **param;
1753 verify_use(state, user, used);
1754 size = TRIPLE_RHS(user->sizes);
1755 param = &RHS(user, 0);
1756 for(i = 0; i < size; i++) {
1757 if (param[i] == used) {
1764 static void free_triple(struct compile_state *state, struct triple *ptr)
1767 size = sizeof(*ptr) - sizeof(ptr->param) +
1768 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr->sizes));
1769 ptr->prev->next = ptr->next;
1770 ptr->next->prev = ptr->prev;
1772 internal_error(state, ptr, "ptr->use != 0");
1774 put_occurance(ptr->occurance);
1775 memset(ptr, -1, size);
1779 static void release_triple(struct compile_state *state, struct triple *ptr)
1781 struct triple_set *set, *next;
1782 struct triple **expr;
1783 /* Remove ptr from use chains where it is the user */
1784 expr = triple_rhs(state, ptr, 0);
1785 for(; expr; expr = triple_rhs(state, ptr, expr)) {
1787 unuse_triple(*expr, ptr);
1790 expr = triple_lhs(state, ptr, 0);
1791 for(; expr; expr = triple_lhs(state, ptr, expr)) {
1793 unuse_triple(*expr, ptr);
1796 expr = triple_misc(state, ptr, 0);
1797 for(; expr; expr = triple_misc(state, ptr, expr)) {
1799 unuse_triple(*expr, ptr);
1802 expr = triple_targ(state, ptr, 0);
1803 for(; expr; expr = triple_targ(state, ptr, expr)) {
1805 unuse_triple(*expr, ptr);
1808 /* Reomve ptr from use chains where it is used */
1809 for(set = ptr->use; set; set = next) {
1811 expr = triple_rhs(state, set->member, 0);
1812 for(; expr; expr = triple_rhs(state, set->member, expr)) {
1814 *expr = &zero_triple;
1817 expr = triple_lhs(state, set->member, 0);
1818 for(; expr; expr = triple_lhs(state, set->member, expr)) {
1820 *expr = &zero_triple;
1823 expr = triple_misc(state, set->member, 0);
1824 for(; expr; expr = triple_misc(state, set->member, expr)) {
1826 *expr = &zero_triple;
1829 expr = triple_targ(state, set->member, 0);
1830 for(; expr; expr = triple_targ(state, set->member, expr)) {
1832 *expr = &zero_triple;
1835 unuse_triple(ptr, set->member);
1837 free_triple(state, ptr);
1840 static void print_triple(struct compile_state *state, struct triple *ptr);
1842 #define TOK_UNKNOWN 0
1845 #define TOK_LBRACE 3
1846 #define TOK_RBRACE 4
1850 #define TOK_LBRACKET 8
1851 #define TOK_RBRACKET 9
1852 #define TOK_LPAREN 10
1853 #define TOK_RPAREN 11
1858 #define TOK_TIMESEQ 16
1859 #define TOK_DIVEQ 17
1860 #define TOK_MODEQ 18
1861 #define TOK_PLUSEQ 19
1862 #define TOK_MINUSEQ 20
1865 #define TOK_ANDEQ 23
1866 #define TOK_XOREQ 24
1869 #define TOK_NOTEQ 27
1870 #define TOK_QUEST 28
1871 #define TOK_LOGOR 29
1872 #define TOK_LOGAND 30
1876 #define TOK_LESSEQ 34
1877 #define TOK_MOREEQ 35
1881 #define TOK_MINUS 39
1884 #define TOK_PLUSPLUS 42
1885 #define TOK_MINUSMINUS 43
1887 #define TOK_ARROW 45
1889 #define TOK_TILDE 47
1890 #define TOK_LIT_STRING 48
1891 #define TOK_LIT_CHAR 49
1892 #define TOK_LIT_INT 50
1893 #define TOK_LIT_FLOAT 51
1894 #define TOK_MACRO 52
1895 #define TOK_CONCATENATE 53
1897 #define TOK_IDENT 54
1898 #define TOK_STRUCT_NAME 55
1899 #define TOK_ENUM_CONST 56
1900 #define TOK_TYPE_NAME 57
1903 #define TOK_BREAK 59
1906 #define TOK_CONST 62
1907 #define TOK_CONTINUE 63
1908 #define TOK_DEFAULT 64
1910 #define TOK_DOUBLE 66
1913 #define TOK_EXTERN 69
1914 #define TOK_FLOAT 70
1918 #define TOK_INLINE 74
1921 #define TOK_REGISTER 77
1922 #define TOK_RESTRICT 78
1923 #define TOK_RETURN 79
1924 #define TOK_SHORT 80
1925 #define TOK_SIGNED 81
1926 #define TOK_SIZEOF 82
1927 #define TOK_STATIC 83
1928 #define TOK_STRUCT 84
1929 #define TOK_SWITCH 85
1930 #define TOK_TYPEDEF 86
1931 #define TOK_UNION 87
1932 #define TOK_UNSIGNED 88
1934 #define TOK_VOLATILE 90
1935 #define TOK_WHILE 91
1937 #define TOK_ATTRIBUTE 93
1938 #define TOK_ALIGNOF 94
1939 #define TOK_FIRST_KEYWORD TOK_AUTO
1940 #define TOK_LAST_KEYWORD TOK_ALIGNOF
1942 #define TOK_DEFINE 100
1943 #define TOK_UNDEF 101
1944 #define TOK_INCLUDE 102
1945 #define TOK_LINE 103
1946 #define TOK_ERROR 104
1947 #define TOK_WARNING 105
1948 #define TOK_PRAGMA 106
1949 #define TOK_IFDEF 107
1950 #define TOK_IFNDEF 108
1951 #define TOK_ELIF 109
1952 #define TOK_ENDIF 110
1954 #define TOK_FIRST_MACRO TOK_DEFINE
1955 #define TOK_LAST_MACRO TOK_ENDIF
1959 static const char *tokens[] = {
1960 [TOK_UNKNOWN ] = "unknown",
1961 [TOK_SPACE ] = ":space:",
1963 [TOK_LBRACE ] = "{",
1964 [TOK_RBRACE ] = "}",
1968 [TOK_LBRACKET ] = "[",
1969 [TOK_RBRACKET ] = "]",
1970 [TOK_LPAREN ] = "(",
1971 [TOK_RPAREN ] = ")",
1973 [TOK_DOTS ] = "...",
1976 [TOK_TIMESEQ ] = "*=",
1977 [TOK_DIVEQ ] = "/=",
1978 [TOK_MODEQ ] = "%=",
1979 [TOK_PLUSEQ ] = "+=",
1980 [TOK_MINUSEQ ] = "-=",
1981 [TOK_SLEQ ] = "<<=",
1982 [TOK_SREQ ] = ">>=",
1983 [TOK_ANDEQ ] = "&=",
1984 [TOK_XOREQ ] = "^=",
1987 [TOK_NOTEQ ] = "!=",
1989 [TOK_LOGOR ] = "||",
1990 [TOK_LOGAND ] = "&&",
1994 [TOK_LESSEQ ] = "<=",
1995 [TOK_MOREEQ ] = ">=",
2002 [TOK_PLUSPLUS ] = "++",
2003 [TOK_MINUSMINUS ] = "--",
2005 [TOK_ARROW ] = "->",
2008 [TOK_LIT_STRING ] = ":string:",
2009 [TOK_IDENT ] = ":ident:",
2010 [TOK_TYPE_NAME ] = ":typename:",
2011 [TOK_LIT_CHAR ] = ":char:",
2012 [TOK_LIT_INT ] = ":integer:",
2013 [TOK_LIT_FLOAT ] = ":float:",
2015 [TOK_CONCATENATE ] = "##",
2017 [TOK_AUTO ] = "auto",
2018 [TOK_BREAK ] = "break",
2019 [TOK_CASE ] = "case",
2020 [TOK_CHAR ] = "char",
2021 [TOK_CONST ] = "const",
2022 [TOK_CONTINUE ] = "continue",
2023 [TOK_DEFAULT ] = "default",
2025 [TOK_DOUBLE ] = "double",
2026 [TOK_ELSE ] = "else",
2027 [TOK_ENUM ] = "enum",
2028 [TOK_EXTERN ] = "extern",
2029 [TOK_FLOAT ] = "float",
2031 [TOK_GOTO ] = "goto",
2033 [TOK_INLINE ] = "inline",
2035 [TOK_LONG ] = "long",
2036 [TOK_REGISTER ] = "register",
2037 [TOK_RESTRICT ] = "restrict",
2038 [TOK_RETURN ] = "return",
2039 [TOK_SHORT ] = "short",
2040 [TOK_SIGNED ] = "signed",
2041 [TOK_SIZEOF ] = "sizeof",
2042 [TOK_STATIC ] = "static",
2043 [TOK_STRUCT ] = "struct",
2044 [TOK_SWITCH ] = "switch",
2045 [TOK_TYPEDEF ] = "typedef",
2046 [TOK_UNION ] = "union",
2047 [TOK_UNSIGNED ] = "unsigned",
2048 [TOK_VOID ] = "void",
2049 [TOK_VOLATILE ] = "volatile",
2050 [TOK_WHILE ] = "while",
2052 [TOK_ATTRIBUTE ] = "__attribute__",
2053 [TOK_ALIGNOF ] = "__alignof__",
2055 [TOK_DEFINE ] = "define",
2056 [TOK_UNDEF ] = "undef",
2057 [TOK_INCLUDE ] = "include",
2058 [TOK_LINE ] = "line",
2059 [TOK_ERROR ] = "error",
2060 [TOK_WARNING ] = "warning",
2061 [TOK_PRAGMA ] = "pragma",
2062 [TOK_IFDEF ] = "ifdef",
2063 [TOK_IFNDEF ] = "ifndef",
2064 [TOK_ELIF ] = "elif",
2065 [TOK_ENDIF ] = "endif",
2070 static unsigned int hash(const char *str, int str_len)
2074 end = str + str_len;
2076 for(; str < end; str++) {
2077 hash = (hash *263) + *str;
2079 hash = hash & (HASH_TABLE_SIZE -1);
2083 static struct hash_entry *lookup(
2084 struct compile_state *state, const char *name, int name_len)
2086 struct hash_entry *entry;
2088 index = hash(name, name_len);
2089 entry = state->hash_table[index];
2091 ((entry->name_len != name_len) ||
2092 (memcmp(entry->name, name, name_len) != 0))) {
2093 entry = entry->next;
2097 /* Get a private copy of the name */
2098 new_name = xmalloc(name_len + 1, "hash_name");
2099 memcpy(new_name, name, name_len);
2100 new_name[name_len] = '\0';
2102 /* Create a new hash entry */
2103 entry = xcmalloc(sizeof(*entry), "hash_entry");
2104 entry->next = state->hash_table[index];
2105 entry->name = new_name;
2106 entry->name_len = name_len;
2108 /* Place the new entry in the hash table */
2109 state->hash_table[index] = entry;
2114 static void ident_to_keyword(struct compile_state *state, struct token *tk)
2116 struct hash_entry *entry;
2118 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
2119 (entry->tok == TOK_ENUM_CONST) ||
2120 ((entry->tok >= TOK_FIRST_KEYWORD) &&
2121 (entry->tok <= TOK_LAST_KEYWORD)))) {
2122 tk->tok = entry->tok;
2126 static void ident_to_macro(struct compile_state *state, struct token *tk)
2128 struct hash_entry *entry;
2131 (entry->tok >= TOK_FIRST_MACRO) &&
2132 (entry->tok <= TOK_LAST_MACRO)) {
2133 tk->tok = entry->tok;
2137 static void hash_keyword(
2138 struct compile_state *state, const char *keyword, int tok)
2140 struct hash_entry *entry;
2141 entry = lookup(state, keyword, strlen(keyword));
2142 if (entry && entry->tok != TOK_UNKNOWN) {
2143 die("keyword %s already hashed", keyword);
2149 struct compile_state *state, struct hash_entry *ident,
2150 struct symbol **chain, struct triple *def, struct type *type)
2153 if (*chain && ((*chain)->scope_depth == state->scope_depth)) {
2154 error(state, 0, "%s already defined", ident->name);
2156 sym = xcmalloc(sizeof(*sym), "symbol");
2160 sym->scope_depth = state->scope_depth;
2165 static void label_symbol(struct compile_state *state,
2166 struct hash_entry *ident, struct triple *label)
2169 if (ident->sym_label) {
2170 error(state, 0, "label %s already defined", ident->name);
2172 sym = xcmalloc(sizeof(*sym), "label");
2175 sym->type = &void_type;
2176 sym->scope_depth = FUNCTION_SCOPE_DEPTH;
2178 ident->sym_label = sym;
2181 static void start_scope(struct compile_state *state)
2183 state->scope_depth++;
2186 static void end_scope_syms(struct symbol **chain, int depth)
2188 struct symbol *sym, *next;
2190 while(sym && (sym->scope_depth == depth)) {
2198 static void end_scope(struct compile_state *state)
2202 /* Walk through the hash table and remove all symbols
2203 * in the current scope.
2205 depth = state->scope_depth;
2206 for(i = 0; i < HASH_TABLE_SIZE; i++) {
2207 struct hash_entry *entry;
2208 entry = state->hash_table[i];
2210 end_scope_syms(&entry->sym_label, depth);
2211 end_scope_syms(&entry->sym_struct, depth);
2212 end_scope_syms(&entry->sym_ident, depth);
2213 entry = entry->next;
2216 state->scope_depth = depth - 1;
2219 static void register_keywords(struct compile_state *state)
2221 hash_keyword(state, "auto", TOK_AUTO);
2222 hash_keyword(state, "break", TOK_BREAK);
2223 hash_keyword(state, "case", TOK_CASE);
2224 hash_keyword(state, "char", TOK_CHAR);
2225 hash_keyword(state, "const", TOK_CONST);
2226 hash_keyword(state, "continue", TOK_CONTINUE);
2227 hash_keyword(state, "default", TOK_DEFAULT);
2228 hash_keyword(state, "do", TOK_DO);
2229 hash_keyword(state, "double", TOK_DOUBLE);
2230 hash_keyword(state, "else", TOK_ELSE);
2231 hash_keyword(state, "enum", TOK_ENUM);
2232 hash_keyword(state, "extern", TOK_EXTERN);
2233 hash_keyword(state, "float", TOK_FLOAT);
2234 hash_keyword(state, "for", TOK_FOR);
2235 hash_keyword(state, "goto", TOK_GOTO);
2236 hash_keyword(state, "if", TOK_IF);
2237 hash_keyword(state, "inline", TOK_INLINE);
2238 hash_keyword(state, "int", TOK_INT);
2239 hash_keyword(state, "long", TOK_LONG);
2240 hash_keyword(state, "register", TOK_REGISTER);
2241 hash_keyword(state, "restrict", TOK_RESTRICT);
2242 hash_keyword(state, "return", TOK_RETURN);
2243 hash_keyword(state, "short", TOK_SHORT);
2244 hash_keyword(state, "signed", TOK_SIGNED);
2245 hash_keyword(state, "sizeof", TOK_SIZEOF);
2246 hash_keyword(state, "static", TOK_STATIC);
2247 hash_keyword(state, "struct", TOK_STRUCT);
2248 hash_keyword(state, "switch", TOK_SWITCH);
2249 hash_keyword(state, "typedef", TOK_TYPEDEF);
2250 hash_keyword(state, "union", TOK_UNION);
2251 hash_keyword(state, "unsigned", TOK_UNSIGNED);
2252 hash_keyword(state, "void", TOK_VOID);
2253 hash_keyword(state, "volatile", TOK_VOLATILE);
2254 hash_keyword(state, "__volatile__", TOK_VOLATILE);
2255 hash_keyword(state, "while", TOK_WHILE);
2256 hash_keyword(state, "asm", TOK_ASM);
2257 hash_keyword(state, "__asm__", TOK_ASM);
2258 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
2259 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
2262 static void register_macro_keywords(struct compile_state *state)
2264 hash_keyword(state, "define", TOK_DEFINE);
2265 hash_keyword(state, "undef", TOK_UNDEF);
2266 hash_keyword(state, "include", TOK_INCLUDE);
2267 hash_keyword(state, "line", TOK_LINE);
2268 hash_keyword(state, "error", TOK_ERROR);
2269 hash_keyword(state, "warning", TOK_WARNING);
2270 hash_keyword(state, "pragma", TOK_PRAGMA);
2271 hash_keyword(state, "ifdef", TOK_IFDEF);
2272 hash_keyword(state, "ifndef", TOK_IFNDEF);
2273 hash_keyword(state, "elif", TOK_ELIF);
2274 hash_keyword(state, "endif", TOK_ENDIF);
2277 static int spacep(int c)
2293 static int digitp(int c)
2297 case '0': case '1': case '2': case '3': case '4':
2298 case '5': case '6': case '7': case '8': case '9':
2304 static int digval(int c)
2307 if ((c >= '0') && (c <= '9')) {
2313 static int hexdigitp(int c)
2317 case '0': case '1': case '2': case '3': case '4':
2318 case '5': case '6': case '7': case '8': case '9':
2319 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2320 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2326 static int hexdigval(int c)
2329 if ((c >= '0') && (c <= '9')) {
2332 else if ((c >= 'A') && (c <= 'F')) {
2333 val = 10 + (c - 'A');
2335 else if ((c >= 'a') && (c <= 'f')) {
2336 val = 10 + (c - 'a');
2341 static int octdigitp(int c)
2345 case '0': case '1': case '2': case '3':
2346 case '4': case '5': case '6': case '7':
2352 static int octdigval(int c)
2355 if ((c >= '0') && (c <= '7')) {
2361 static int letterp(int c)
2365 case 'a': case 'b': case 'c': case 'd': case 'e':
2366 case 'f': case 'g': case 'h': case 'i': case 'j':
2367 case 'k': case 'l': case 'm': case 'n': case 'o':
2368 case 'p': case 'q': case 'r': case 's': case 't':
2369 case 'u': case 'v': case 'w': case 'x': case 'y':
2371 case 'A': case 'B': case 'C': case 'D': case 'E':
2372 case 'F': case 'G': case 'H': case 'I': case 'J':
2373 case 'K': case 'L': case 'M': case 'N': case 'O':
2374 case 'P': case 'Q': case 'R': case 'S': case 'T':
2375 case 'U': case 'V': case 'W': case 'X': case 'Y':
2384 static int char_value(struct compile_state *state,
2385 const signed char **strp, const signed char *end)
2387 const signed char *str;
2391 if ((c == '\\') && (str < end)) {
2393 case 'n': c = '\n'; str++; break;
2394 case 't': c = '\t'; str++; break;
2395 case 'v': c = '\v'; str++; break;
2396 case 'b': c = '\b'; str++; break;
2397 case 'r': c = '\r'; str++; break;
2398 case 'f': c = '\f'; str++; break;
2399 case 'a': c = '\a'; str++; break;
2400 case '\\': c = '\\'; str++; break;
2401 case '?': c = '?'; str++; break;
2402 case '\'': c = '\''; str++; break;
2403 case '"': c = '"'; break;
2407 while((str < end) && hexdigitp(*str)) {
2409 c += hexdigval(*str);
2413 case '0': case '1': case '2': case '3':
2414 case '4': case '5': case '6': case '7':
2416 while((str < end) && octdigitp(*str)) {
2418 c += octdigval(*str);
2423 error(state, 0, "Invalid character constant");
2431 static char *after_digits(char *ptr, char *end)
2433 while((ptr < end) && digitp(*ptr)) {
2439 static char *after_octdigits(char *ptr, char *end)
2441 while((ptr < end) && octdigitp(*ptr)) {
2447 static char *after_hexdigits(char *ptr, char *end)
2449 while((ptr < end) && hexdigitp(*ptr)) {
2455 static void save_string(struct compile_state *state,
2456 struct token *tk, char *start, char *end, const char *id)
2460 /* Create a private copy of the string */
2461 str_len = end - start + 1;
2462 str = xmalloc(str_len + 1, id);
2463 memcpy(str, start, str_len);
2464 str[str_len] = '\0';
2466 /* Store the copy in the token */
2468 tk->str_len = str_len;
2470 static void next_token(struct compile_state *state, int index)
2472 struct file_state *file;
2480 tk = &state->token[index];
2483 token = tokp = file->pos;
2484 end = file->buf + file->size;
2491 if ((tokp + 1) < end) {
2495 if ((tokp + 2) < end) {
2499 if ((tokp + 3) < end) {
2507 else if (spacep(c)) {
2509 while ((tokp < end) && spacep(c)) {
2512 file->report_line++;
2513 file->line_start = tokp + 1;
2522 else if ((c == '/') && (c1 == '/')) {
2524 for(tokp += 2; tokp < end; tokp++) {
2528 file->report_line++;
2529 file->line_start = tokp +1;
2535 else if ((c == '/') && (c1 == '*')) {
2539 line_start = file->line_start;
2540 for(tokp += 2; (end - tokp) >= 2; tokp++) {
2544 line_start = tokp +1;
2546 else if ((c == '*') && (tokp[1] == '/')) {
2552 if (tok == TOK_UNKNOWN) {
2553 error(state, 0, "unterminated comment");
2555 file->report_line += line - file->line;
2557 file->line_start = line_start;
2559 /* string constants */
2560 else if ((c == '"') ||
2561 ((c == 'L') && (c1 == '"'))) {
2566 line_start = file->line_start;
2572 for(tokp += 1; tokp < end; tokp++) {
2576 line_start = tokp + 1;
2578 else if ((c == '\\') && (tokp +1 < end)) {
2581 else if (c == '"') {
2582 tok = TOK_LIT_STRING;
2586 if (tok == TOK_UNKNOWN) {
2587 error(state, 0, "unterminated string constant");
2589 if (line != file->line) {
2590 warning(state, 0, "multiline string constant");
2592 file->report_line += line - file->line;
2594 file->line_start = line_start;
2596 /* Save the string value */
2597 save_string(state, tk, token, tokp, "literal string");
2599 /* character constants */
2600 else if ((c == '\'') ||
2601 ((c == 'L') && (c1 == '\''))) {
2606 line_start = file->line_start;
2612 for(tokp += 1; tokp < end; tokp++) {
2616 line_start = tokp + 1;
2618 else if ((c == '\\') && (tokp +1 < end)) {
2621 else if (c == '\'') {
2626 if (tok == TOK_UNKNOWN) {
2627 error(state, 0, "unterminated character constant");
2629 if (line != file->line) {
2630 warning(state, 0, "multiline character constant");
2632 file->report_line += line - file->line;
2634 file->line_start = line_start;
2636 /* Save the character value */
2637 save_string(state, tk, token, tokp, "literal character");
2639 /* integer and floating constants
2645 * Floating constants
2646 * {digits}.{digits}[Ee][+-]?{digits}
2648 * {digits}[Ee][+-]?{digits}
2649 * .{digits}[Ee][+-]?{digits}
2653 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
2658 next = after_digits(tokp, end);
2663 if (next[0] == '.') {
2664 new = after_digits(next, end);
2665 is_float = (new != next);
2668 if ((next[0] == 'e') || (next[0] == 'E')) {
2669 if (((next + 1) < end) &&
2670 ((next[1] == '+') || (next[1] == '-'))) {
2673 new = after_digits(next, end);
2674 is_float = (new != next);
2678 tok = TOK_LIT_FLOAT;
2679 if ((next < end) && (
2688 if (!is_float && digitp(c)) {
2690 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
2691 next = after_hexdigits(tokp + 2, end);
2693 else if (c == '0') {
2694 next = after_octdigits(tokp, end);
2697 next = after_digits(tokp, end);
2699 /* crazy integer suffixes */
2701 ((next[0] == 'u') || (next[0] == 'U'))) {
2704 ((next[0] == 'l') || (next[0] == 'L'))) {
2708 else if ((next < end) &&
2709 ((next[0] == 'l') || (next[0] == 'L'))) {
2712 ((next[0] == 'u') || (next[0] == 'U'))) {
2719 /* Save the integer/floating point value */
2720 save_string(state, tk, token, tokp, "literal number");
2723 else if (letterp(c)) {
2725 for(tokp += 1; tokp < end; tokp++) {
2727 if (!letterp(c) && !digitp(c)) {
2732 tk->ident = lookup(state, token, tokp +1 - token);
2734 /* C99 alternate macro characters */
2735 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
2737 tok = TOK_CONCATENATE;
2739 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { tokp += 2; tok = TOK_DOTS; }
2740 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { tokp += 2; tok = TOK_SLEQ; }
2741 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { tokp += 2; tok = TOK_SREQ; }
2742 else if ((c == '*') && (c1 == '=')) { tokp += 1; tok = TOK_TIMESEQ; }
2743 else if ((c == '/') && (c1 == '=')) { tokp += 1; tok = TOK_DIVEQ; }
2744 else if ((c == '%') && (c1 == '=')) { tokp += 1; tok = TOK_MODEQ; }
2745 else if ((c == '+') && (c1 == '=')) { tokp += 1; tok = TOK_PLUSEQ; }
2746 else if ((c == '-') && (c1 == '=')) { tokp += 1; tok = TOK_MINUSEQ; }
2747 else if ((c == '&') && (c1 == '=')) { tokp += 1; tok = TOK_ANDEQ; }
2748 else if ((c == '^') && (c1 == '=')) { tokp += 1; tok = TOK_XOREQ; }
2749 else if ((c == '|') && (c1 == '=')) { tokp += 1; tok = TOK_OREQ; }
2750 else if ((c == '=') && (c1 == '=')) { tokp += 1; tok = TOK_EQEQ; }
2751 else if ((c == '!') && (c1 == '=')) { tokp += 1; tok = TOK_NOTEQ; }
2752 else if ((c == '|') && (c1 == '|')) { tokp += 1; tok = TOK_LOGOR; }
2753 else if ((c == '&') && (c1 == '&')) { tokp += 1; tok = TOK_LOGAND; }
2754 else if ((c == '<') && (c1 == '=')) { tokp += 1; tok = TOK_LESSEQ; }
2755 else if ((c == '>') && (c1 == '=')) { tokp += 1; tok = TOK_MOREEQ; }
2756 else if ((c == '<') && (c1 == '<')) { tokp += 1; tok = TOK_SL; }
2757 else if ((c == '>') && (c1 == '>')) { tokp += 1; tok = TOK_SR; }
2758 else if ((c == '+') && (c1 == '+')) { tokp += 1; tok = TOK_PLUSPLUS; }
2759 else if ((c == '-') && (c1 == '-')) { tokp += 1; tok = TOK_MINUSMINUS; }
2760 else if ((c == '-') && (c1 == '>')) { tokp += 1; tok = TOK_ARROW; }
2761 else if ((c == '<') && (c1 == ':')) { tokp += 1; tok = TOK_LBRACKET; }
2762 else if ((c == ':') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACKET; }
2763 else if ((c == '<') && (c1 == '%')) { tokp += 1; tok = TOK_LBRACE; }
2764 else if ((c == '%') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACE; }
2765 else if ((c == '%') && (c1 == ':')) { tokp += 1; tok = TOK_MACRO; }
2766 else if ((c == '#') && (c1 == '#')) { tokp += 1; tok = TOK_CONCATENATE; }
2767 else if (c == ';') { tok = TOK_SEMI; }
2768 else if (c == '{') { tok = TOK_LBRACE; }
2769 else if (c == '}') { tok = TOK_RBRACE; }
2770 else if (c == ',') { tok = TOK_COMMA; }
2771 else if (c == '=') { tok = TOK_EQ; }
2772 else if (c == ':') { tok = TOK_COLON; }
2773 else if (c == '[') { tok = TOK_LBRACKET; }
2774 else if (c == ']') { tok = TOK_RBRACKET; }
2775 else if (c == '(') { tok = TOK_LPAREN; }
2776 else if (c == ')') { tok = TOK_RPAREN; }
2777 else if (c == '*') { tok = TOK_STAR; }
2778 else if (c == '>') { tok = TOK_MORE; }
2779 else if (c == '<') { tok = TOK_LESS; }
2780 else if (c == '?') { tok = TOK_QUEST; }
2781 else if (c == '|') { tok = TOK_OR; }
2782 else if (c == '&') { tok = TOK_AND; }
2783 else if (c == '^') { tok = TOK_XOR; }
2784 else if (c == '+') { tok = TOK_PLUS; }
2785 else if (c == '-') { tok = TOK_MINUS; }
2786 else if (c == '/') { tok = TOK_DIV; }
2787 else if (c == '%') { tok = TOK_MOD; }
2788 else if (c == '!') { tok = TOK_BANG; }
2789 else if (c == '.') { tok = TOK_DOT; }
2790 else if (c == '~') { tok = TOK_TILDE; }
2791 else if (c == '#') { tok = TOK_MACRO; }
2792 if (tok == TOK_MACRO) {
2793 /* Only match preprocessor directives at the start of a line */
2795 for(ptr = file->line_start; spacep(*ptr); ptr++)
2801 if (tok == TOK_UNKNOWN) {
2802 error(state, 0, "unknown token");
2805 file->pos = tokp + 1;
2807 if (tok == TOK_IDENT) {
2808 ident_to_keyword(state, tk);
2810 /* Don't return space tokens. */
2811 if (tok == TOK_SPACE) {
2816 static void compile_macro(struct compile_state *state, struct token *tk)
2818 struct file_state *file;
2819 struct hash_entry *ident;
2821 file = xmalloc(sizeof(*file), "file_state");
2822 file->basename = xstrdup(tk->ident->name);
2823 file->dirname = xstrdup("");
2824 file->size = ident->sym_define->buf_len;
2825 file->buf = xmalloc(file->size +2, file->basename);
2826 memcpy(file->buf, ident->sym_define->buf, file->size);
2827 file->buf[file->size] = '\n';
2828 file->buf[file->size + 1] = '\0';
2829 file->pos = file->buf;
2830 file->line_start = file->pos;
2832 file->report_line = 1;
2833 file->report_name = file->basename;
2834 file->report_dir = file->dirname;
2835 file->prev = state->file;
2840 static int mpeek(struct compile_state *state, int index)
2844 tk = &state->token[index + 1];
2845 if (tk->tok == -1) {
2846 next_token(state, index + 1);
2850 if ((tk->tok == TOK_EOF) &&
2851 (state->file != state->macro_file) &&
2852 (state->file->prev)) {
2853 struct file_state *file = state->file;
2854 state->file = file->prev;
2855 /* file->basename is used keep it */
2856 if (file->report_dir != file->dirname) {
2857 xfree(file->report_dir);
2859 xfree(file->dirname);
2862 next_token(state, index + 1);
2865 else if (tk->ident && tk->ident->sym_define) {
2866 compile_macro(state, tk);
2867 next_token(state, index + 1);
2871 /* Don't show the token on the next line */
2872 if (state->macro_line < state->macro_file->line) {
2875 return state->token[index +1].tok;
2878 static void meat(struct compile_state *state, int index, int tok)
2882 next_tok = mpeek(state, index);
2883 if (next_tok != tok) {
2884 const char *name1, *name2;
2885 name1 = tokens[next_tok];
2887 if (next_tok == TOK_IDENT) {
2888 name2 = state->token[index + 1].ident->name;
2890 error(state, 0, "found %s %s expected %s",
2891 name1, name2, tokens[tok]);
2893 /* Free the old token value */
2894 if (state->token[index].str_len) {
2895 memset((void *)(state->token[index].val.str), -1,
2896 state->token[index].str_len);
2897 xfree(state->token[index].val.str);
2899 for(i = index; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
2900 state->token[i] = state->token[i + 1];
2902 memset(&state->token[i], 0, sizeof(state->token[i]));
2903 state->token[i].tok = -1;
2906 static long_t mcexpr(struct compile_state *state, int index);
2908 static long_t mprimary_expr(struct compile_state *state, int index)
2912 tok = mpeek(state, index);
2913 while(state->token[index + 1].ident &&
2914 state->token[index + 1].ident->sym_define) {
2915 meat(state, index, tok);
2916 compile_macro(state, &state->token[index]);
2917 tok = mpeek(state, index);
2921 meat(state, index, TOK_LPAREN);
2922 val = mcexpr(state, index);
2923 meat(state, index, TOK_RPAREN);
2928 meat(state, index, TOK_LIT_INT);
2930 val = strtol(state->token[index].val.str, &end, 0);
2931 if (((val == LONG_MIN) || (val == LONG_MAX)) &&
2932 (errno == ERANGE)) {
2933 error(state, 0, "Integer constant to large");
2938 meat(state, index, TOK_LIT_INT);
2943 static long_t munary_expr(struct compile_state *state, int index)
2946 switch(mpeek(state, index)) {
2948 meat(state, index, TOK_PLUS);
2949 val = munary_expr(state, index);
2953 meat(state, index, TOK_MINUS);
2954 val = munary_expr(state, index);
2958 meat(state, index, TOK_BANG);
2959 val = munary_expr(state, index);
2963 meat(state, index, TOK_BANG);
2964 val = munary_expr(state, index);
2968 val = mprimary_expr(state, index);
2974 static long_t mmul_expr(struct compile_state *state, int index)
2978 val = munary_expr(state, index);
2982 switch(mpeek(state, index)) {
2984 meat(state, index, TOK_STAR);
2985 right = munary_expr(state, index);
2989 meat(state, index, TOK_DIV);
2990 right = munary_expr(state, index);
2994 meat(state, index, TOK_MOD);
2995 right = munary_expr(state, index);
3007 static long_t madd_expr(struct compile_state *state, int index)
3011 val = mmul_expr(state, index);
3015 switch(mpeek(state, index)) {
3017 meat(state, index, TOK_PLUS);
3018 right = mmul_expr(state, index);
3022 meat(state, index, TOK_MINUS);
3023 right = mmul_expr(state, index);
3035 static long_t mshift_expr(struct compile_state *state, int index)
3039 val = madd_expr(state, index);
3043 switch(mpeek(state, index)) {
3045 meat(state, index, TOK_SL);
3046 right = madd_expr(state, index);
3050 meat(state, index, TOK_SR);
3051 right = madd_expr(state, index);
3063 static long_t mrel_expr(struct compile_state *state, int index)
3067 val = mshift_expr(state, index);
3071 switch(mpeek(state, index)) {
3073 meat(state, index, TOK_LESS);
3074 right = mshift_expr(state, index);
3078 meat(state, index, TOK_MORE);
3079 right = mshift_expr(state, index);
3083 meat(state, index, TOK_LESSEQ);
3084 right = mshift_expr(state, index);
3088 meat(state, index, TOK_MOREEQ);
3089 right = mshift_expr(state, index);
3100 static long_t meq_expr(struct compile_state *state, int index)
3104 val = mrel_expr(state, index);
3108 switch(mpeek(state, index)) {
3110 meat(state, index, TOK_EQEQ);
3111 right = mrel_expr(state, index);
3115 meat(state, index, TOK_NOTEQ);
3116 right = mrel_expr(state, index);
3127 static long_t mand_expr(struct compile_state *state, int index)
3130 val = meq_expr(state, index);
3131 if (mpeek(state, index) == TOK_AND) {
3133 meat(state, index, TOK_AND);
3134 right = meq_expr(state, index);
3140 static long_t mxor_expr(struct compile_state *state, int index)
3143 val = mand_expr(state, index);
3144 if (mpeek(state, index) == TOK_XOR) {
3146 meat(state, index, TOK_XOR);
3147 right = mand_expr(state, index);
3153 static long_t mor_expr(struct compile_state *state, int index)
3156 val = mxor_expr(state, index);
3157 if (mpeek(state, index) == TOK_OR) {
3159 meat(state, index, TOK_OR);
3160 right = mxor_expr(state, index);
3166 static long_t mland_expr(struct compile_state *state, int index)
3169 val = mor_expr(state, index);
3170 if (mpeek(state, index) == TOK_LOGAND) {
3172 meat(state, index, TOK_LOGAND);
3173 right = mor_expr(state, index);
3178 static long_t mlor_expr(struct compile_state *state, int index)
3181 val = mland_expr(state, index);
3182 if (mpeek(state, index) == TOK_LOGOR) {
3184 meat(state, index, TOK_LOGOR);
3185 right = mland_expr(state, index);
3191 static long_t mcexpr(struct compile_state *state, int index)
3193 return mlor_expr(state, index);
3195 static void preprocess(struct compile_state *state, int index)
3197 /* Doing much more with the preprocessor would require
3198 * a parser and a major restructuring.
3199 * Postpone that for later.
3201 struct file_state *file;
3207 tk = &state->token[index];
3208 state->macro_line = line = file->line;
3209 state->macro_file = file;
3211 next_token(state, index);
3212 ident_to_macro(state, tk);
3213 if (tk->tok == TOK_IDENT) {
3214 error(state, 0, "undefined preprocessing directive `%s'",
3221 override_line = strtoul(tk->val.str, 0, 10);
3222 next_token(state, index);
3223 /* I have a cpp line marker parse it */
3224 if (tk->tok == TOK_LIT_STRING) {
3225 const char *token, *base;
3227 int name_len, dir_len;
3228 name = xmalloc(tk->str_len, "report_name");
3229 token = tk->val.str + 1;
3230 base = strrchr(token, '/');
3231 name_len = tk->str_len -2;
3233 dir_len = base - token;
3235 name_len -= base - token;
3240 memcpy(name, base, name_len);
3241 name[name_len] = '\0';
3242 dir = xmalloc(dir_len + 1, "report_dir");
3243 memcpy(dir, token, dir_len);
3244 dir[dir_len] = '\0';
3245 file->report_line = override_line - 1;
3246 file->report_name = name;
3247 file->report_dir = dir;
3252 meat(state, index, TOK_LINE);
3253 meat(state, index, TOK_LIT_INT);
3254 file->report_line = strtoul(tk->val.str, 0, 10) -1;
3255 if (mpeek(state, index) == TOK_LIT_STRING) {
3256 const char *token, *base;
3258 int name_len, dir_len;
3259 meat(state, index, TOK_LIT_STRING);
3260 name = xmalloc(tk->str_len, "report_name");
3261 token = tk->val.str + 1;
3262 name_len = tk->str_len - 2;
3264 dir_len = base - token;
3266 name_len -= base - token;
3271 memcpy(name, base, name_len);
3272 name[name_len] = '\0';
3273 dir = xmalloc(dir_len + 1, "report_dir");
3274 memcpy(dir, token, dir_len);
3275 dir[dir_len] = '\0';
3276 file->report_name = name;
3277 file->report_dir = dir;
3282 if (state->if_value < 0) {
3285 warning(state, 0, "Ignoring preprocessor directive: %s",
3289 error(state, 0, "#elif not supported");
3290 #warning "FIXME multiple #elif and #else in an #if do not work properly"
3291 if (state->if_depth == 0) {
3292 error(state, 0, "#elif without #if");
3294 /* If the #if was taken the #elif just disables the following code */
3295 if (state->if_value >= 0) {
3296 state->if_value = - state->if_value;
3298 /* If the previous #if was not taken see if the #elif enables the
3301 else if ((state->if_value < 0) &&
3302 (state->if_depth == - state->if_value))
3304 if (mcexpr(state, index) != 0) {
3305 state->if_value = state->if_depth;
3308 state->if_value = - state->if_depth;
3314 if (state->if_value < 0) {
3317 if (mcexpr(state, index) != 0) {
3318 state->if_value = state->if_depth;
3321 state->if_value = - state->if_depth;
3326 if (state->if_value < 0) {
3329 next_token(state, index);
3330 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3331 error(state, 0, "Invalid macro name");
3333 if (tk->ident->sym_define == 0) {
3334 state->if_value = state->if_depth;
3337 state->if_value = - state->if_depth;
3342 if (state->if_value < 0) {
3345 next_token(state, index);
3346 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3347 error(state, 0, "Invalid macro name");
3349 if (tk->ident->sym_define != 0) {
3350 state->if_value = state->if_depth;
3353 state->if_value = - state->if_depth;
3357 if (state->if_depth == 0) {
3358 error(state, 0, "#else without #if");
3360 if ((state->if_value >= 0) ||
3361 ((state->if_value < 0) &&
3362 (state->if_depth == -state->if_value)))
3364 state->if_value = - state->if_value;
3368 if (state->if_depth == 0) {
3369 error(state, 0, "#endif without #if");
3371 if ((state->if_value >= 0) ||
3372 ((state->if_value < 0) &&
3373 (state->if_depth == -state->if_value)))
3375 state->if_value = state->if_depth - 1;
3381 struct hash_entry *ident;
3382 struct macro *macro;
3385 if (state->if_value < 0) /* quit early when #if'd out */
3388 meat(state, index, TOK_IDENT);
3392 if (*file->pos == '(') {
3393 #warning "FIXME macros with arguments not supported"
3394 error(state, 0, "Macros with arguments not supported");
3397 /* Find the end of the line to get an estimate of
3398 * the macro's length.
3400 for(ptr = file->pos; *ptr != '\n'; ptr++)
3403 if (ident->sym_define != 0) {
3404 error(state, 0, "macro %s already defined\n", ident->name);
3406 macro = xmalloc(sizeof(*macro), "macro");
3407 macro->ident = ident;
3408 macro->buf_len = ptr - file->pos +1;
3409 macro->buf = xmalloc(macro->buf_len +2, "macro buf");
3411 memcpy(macro->buf, file->pos, macro->buf_len);
3412 macro->buf[macro->buf_len] = '\n';
3413 macro->buf[macro->buf_len +1] = '\0';
3415 ident->sym_define = macro;
3422 /* Find the end of the line */
3423 for(end = file->pos; *end != '\n'; end++)
3425 len = (end - file->pos);
3426 if (state->if_value >= 0) {
3427 error(state, 0, "%*.*s", len, len, file->pos);
3436 /* Find the end of the line */
3437 for(end = file->pos; *end != '\n'; end++)
3439 len = (end - file->pos);
3440 if (state->if_value >= 0) {
3441 warning(state, 0, "%*.*s", len, len, file->pos);
3453 next_token(state, index);
3454 if (tk->tok == TOK_LIT_STRING) {
3457 name = xmalloc(tk->str_len, "include");
3458 token = tk->val.str +1;
3459 name_len = tk->str_len -2;
3460 if (*token == '"') {
3464 memcpy(name, token, name_len);
3465 name[name_len] = '\0';
3468 else if (tk->tok == TOK_LESS) {
3471 for(end = start; *end != '\n'; end++) {
3477 error(state, 0, "Unterminated included directive");
3479 name = xmalloc(end - start + 1, "include");
3480 memcpy(name, start, end - start);
3481 name[end - start] = '\0';
3486 error(state, 0, "Invalid include directive");
3488 /* Error if there are any characters after the include */
3489 for(ptr = file->pos; *ptr != '\n'; ptr++) {
3496 error(state, 0, "garbage after include directive");
3499 if (state->if_value >= 0) {
3500 compile_file(state, name, local);
3503 next_token(state, index);
3507 /* Ignore # without a following ident */
3508 if (tk->tok == TOK_IDENT) {
3509 error(state, 0, "Invalid preprocessor directive: %s",
3514 /* Consume the rest of the macro line */
3516 tok = mpeek(state, index);
3517 meat(state, index, tok);
3518 } while(tok != TOK_EOF);
3522 static void token(struct compile_state *state, int index)
3524 struct file_state *file;
3528 tk = &state->token[index];
3529 next_token(state, index);
3533 if (tk->tok == TOK_EOF && file->prev) {
3534 state->file = file->prev;
3535 /* file->basename is used keep it */
3536 xfree(file->dirname);
3539 next_token(state, index);
3542 else if (tk->tok == TOK_MACRO) {
3543 preprocess(state, index);
3546 else if (tk->ident && tk->ident->sym_define) {
3547 compile_macro(state, tk);
3548 next_token(state, index);
3551 else if (state->if_value < 0) {
3552 next_token(state, index);
3558 static int peek(struct compile_state *state)
3560 if (state->token[1].tok == -1) {
3563 return state->token[1].tok;
3566 static int peek2(struct compile_state *state)
3568 if (state->token[1].tok == -1) {
3571 if (state->token[2].tok == -1) {
3574 return state->token[2].tok;
3577 static void eat(struct compile_state *state, int tok)
3581 next_tok = peek(state);
3582 if (next_tok != tok) {
3583 const char *name1, *name2;
3584 name1 = tokens[next_tok];
3586 if (next_tok == TOK_IDENT) {
3587 name2 = state->token[1].ident->name;
3589 error(state, 0, "\tfound %s %s expected %s",
3590 name1, name2 ,tokens[tok]);
3592 /* Free the old token value */
3593 if (state->token[0].str_len) {
3594 xfree((void *)(state->token[0].val.str));
3596 for(i = 0; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
3597 state->token[i] = state->token[i + 1];
3599 memset(&state->token[i], 0, sizeof(state->token[i]));
3600 state->token[i].tok = -1;
3603 #warning "FIXME do not hardcode the include paths"
3604 static char *include_paths[] = {
3605 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/include",
3606 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/arch/i386/include",
3607 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src",
3611 static void compile_file(struct compile_state *state, const char *filename, int local)
3614 const char *subdir, *base;
3616 struct file_state *file;
3618 file = xmalloc(sizeof(*file), "file_state");
3620 base = strrchr(filename, '/');
3623 subdir_len = base - filename;
3630 basename = xmalloc(strlen(base) +1, "basename");
3631 strcpy(basename, base);
3632 file->basename = basename;
3634 if (getcwd(cwd, sizeof(cwd)) == 0) {
3635 die("cwd buffer to small");
3638 if (subdir[0] == '/') {
3639 file->dirname = xmalloc(subdir_len + 1, "dirname");
3640 memcpy(file->dirname, subdir, subdir_len);
3641 file->dirname[subdir_len] = '\0';
3647 /* Find the appropriate directory... */
3649 if (!state->file && exists(cwd, filename)) {
3652 if (local && state->file && exists(state->file->dirname, filename)) {
3653 dir = state->file->dirname;
3655 for(path = include_paths; !dir && *path; path++) {
3656 if (exists(*path, filename)) {
3661 error(state, 0, "Cannot find `%s'\n", filename);
3663 dirlen = strlen(dir);
3664 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
3665 memcpy(file->dirname, dir, dirlen);
3666 file->dirname[dirlen] = '/';
3667 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
3668 file->dirname[dirlen + 1 + subdir_len] = '\0';
3670 file->buf = slurp_file(file->dirname, file->basename, &file->size);
3673 file->pos = file->buf;
3674 file->line_start = file->pos;
3677 file->report_line = 1;
3678 file->report_name = file->basename;
3679 file->report_dir = file->dirname;
3681 file->prev = state->file;
3684 process_trigraphs(state);
3685 splice_lines(state);
3688 /* Type helper functions */
3690 static struct type *new_type(
3691 unsigned int type, struct type *left, struct type *right)
3693 struct type *result;
3694 result = xmalloc(sizeof(*result), "type");
3695 result->type = type;
3696 result->left = left;
3697 result->right = right;
3698 result->field_ident = 0;
3699 result->type_ident = 0;
3703 static struct type *clone_type(unsigned int specifiers, struct type *old)
3705 struct type *result;
3706 result = xmalloc(sizeof(*result), "type");
3707 memcpy(result, old, sizeof(*result));
3708 result->type &= TYPE_MASK;
3709 result->type |= specifiers;
3713 #define SIZEOF_SHORT 2
3714 #define SIZEOF_INT 4
3715 #define SIZEOF_LONG (sizeof(long_t))
3717 #define ALIGNOF_SHORT 2
3718 #define ALIGNOF_INT 4
3719 #define ALIGNOF_LONG (sizeof(long_t))
3721 #define MASK_UCHAR(X) ((X) & ((ulong_t)0xff))
3722 #define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT*8)) - 1))
3723 static inline ulong_t mask_uint(ulong_t x)
3725 if (SIZEOF_INT < SIZEOF_LONG) {
3726 ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT*8))) -1;
3731 #define MASK_UINT(X) (mask_uint(X))
3732 #define MASK_ULONG(X) (X)
3734 static struct type void_type = { .type = TYPE_VOID };
3735 static struct type char_type = { .type = TYPE_CHAR };
3736 static struct type uchar_type = { .type = TYPE_UCHAR };
3737 static struct type short_type = { .type = TYPE_SHORT };
3738 static struct type ushort_type = { .type = TYPE_USHORT };
3739 static struct type int_type = { .type = TYPE_INT };
3740 static struct type uint_type = { .type = TYPE_UINT };
3741 static struct type long_type = { .type = TYPE_LONG };
3742 static struct type ulong_type = { .type = TYPE_ULONG };
3744 static struct triple *variable(struct compile_state *state, struct type *type)
3746 struct triple *result;
3747 if ((type->type & STOR_MASK) != STOR_PERM) {
3748 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3749 result = triple(state, OP_ADECL, type, 0, 0);
3752 struct triple **vector;
3754 result = new_triple(state, OP_VAL_VEC, type, -1, -1);
3755 vector = &result->param[0];
3759 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
3760 vector[index] = variable(state, field->left);
3761 field = field->right;
3764 vector[index] = variable(state, field);
3768 result = triple(state, OP_SDECL, type, 0, 0);
3773 static void stor_of(FILE *fp, struct type *type)
3775 switch(type->type & STOR_MASK) {
3777 fprintf(fp, "auto ");
3780 fprintf(fp, "static ");
3783 fprintf(fp, "extern ");
3786 fprintf(fp, "register ");
3789 fprintf(fp, "typedef ");
3792 fprintf(fp, "inline ");
3796 static void qual_of(FILE *fp, struct type *type)
3798 if (type->type & QUAL_CONST) {
3799 fprintf(fp, " const");
3801 if (type->type & QUAL_VOLATILE) {
3802 fprintf(fp, " volatile");
3804 if (type->type & QUAL_RESTRICT) {
3805 fprintf(fp, " restrict");
3809 static void name_of(FILE *fp, struct type *type)
3812 switch(type->type & TYPE_MASK) {
3814 fprintf(fp, "void");
3818 fprintf(fp, "signed char");
3822 fprintf(fp, "unsigned char");
3826 fprintf(fp, "signed short");
3830 fprintf(fp, "unsigned short");
3834 fprintf(fp, "signed int");
3838 fprintf(fp, "unsigned int");
3842 fprintf(fp, "signed long");
3846 fprintf(fp, "unsigned long");
3850 name_of(fp, type->left);
3856 name_of(fp, type->left);
3858 name_of(fp, type->right);
3861 fprintf(fp, "enum %s", type->type_ident->name);
3865 fprintf(fp, "struct %s", type->type_ident->name);
3870 name_of(fp, type->left);
3871 fprintf(fp, " (*)(");
3872 name_of(fp, type->right);
3877 name_of(fp, type->left);
3878 fprintf(fp, " [%ld]", type->elements);
3881 fprintf(fp, "????: %x", type->type & TYPE_MASK);
3886 static size_t align_of(struct compile_state *state, struct type *type)
3890 switch(type->type & TYPE_MASK) {
3900 align = ALIGNOF_SHORT;
3905 align = ALIGNOF_INT;
3910 align = ALIGNOF_LONG;
3915 size_t left_align, right_align;
3916 left_align = align_of(state, type->left);
3917 right_align = align_of(state, type->right);
3918 align = (left_align >= right_align) ? left_align : right_align;
3922 align = align_of(state, type->left);
3925 align = align_of(state, type->left);
3928 error(state, 0, "alignof not yet defined for type\n");
3934 static size_t needed_padding(size_t offset, size_t align)
3938 if (offset % align) {
3939 padding = align - (offset % align);
3943 static size_t size_of(struct compile_state *state, struct type *type)
3947 switch(type->type & TYPE_MASK) {
3957 size = SIZEOF_SHORT;
3973 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
3974 align = align_of(state, type->left);
3975 pad = needed_padding(size, align);
3976 size = size + pad + size_of(state, type->left);
3979 align = align_of(state, type);
3980 pad = needed_padding(size, align);
3981 size = size + pad + sizeof(type);
3986 size_t size_left, size_right;
3987 size_left = size_of(state, type->left);
3988 size_right = size_of(state, type->right);
3989 size = (size_left >= size_right)? size_left : size_right;
3993 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
3994 internal_error(state, 0, "Invalid array type");
3996 size = size_of(state, type->left) * type->elements;
4000 size = size_of(state, type->left);
4003 error(state, 0, "sizeof not yet defined for type\n");
4009 static size_t field_offset(struct compile_state *state,
4010 struct type *type, struct hash_entry *field)
4012 struct type *member;
4014 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4015 internal_error(state, 0, "field_offset only works on structures");
4018 member = type->left;
4019 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4020 align = align_of(state, member->left);
4021 size += needed_padding(size, align);
4022 if (member->left->field_ident == field) {
4023 member = member->left;
4026 size += size_of(state, member->left);
4027 member = member->right;
4029 align = align_of(state, member);
4030 size += needed_padding(size, align);
4031 if (member->field_ident != field) {
4032 error(state, 0, "member %s not present", field->name);
4037 static struct type *field_type(struct compile_state *state,
4038 struct type *type, struct hash_entry *field)
4040 struct type *member;
4041 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4042 internal_error(state, 0, "field_type only works on structures");
4044 member = type->left;
4045 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4046 if (member->left->field_ident == field) {
4047 member = member->left;
4050 member = member->right;
4052 if (member->field_ident != field) {
4053 error(state, 0, "member %s not present", field->name);
4058 static struct type *next_field(struct compile_state *state,
4059 struct type *type, struct type *prev_member)
4061 struct type *member;
4062 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4063 internal_error(state, 0, "next_field only works on structures");
4065 member = type->left;
4066 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4068 member = member->left;
4071 if (member->left == prev_member) {
4074 member = member->right;
4076 if (member == prev_member) {
4080 internal_error(state, 0, "prev_member %s not present",
4081 prev_member->field_ident->name);
4086 static struct triple *struct_field(struct compile_state *state,
4087 struct triple *decl, struct hash_entry *field)
4089 struct triple **vector;
4093 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4096 if (decl->op != OP_VAL_VEC) {
4097 internal_error(state, 0, "Invalid struct variable");
4100 internal_error(state, 0, "Missing structure field");
4103 vector = &RHS(decl, 0);
4105 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4106 if (type->left->field_ident == field) {
4113 if (type->field_ident != field) {
4114 internal_error(state, 0, "field %s not found?", field->name);
4116 return vector[index];
4119 static void arrays_complete(struct compile_state *state, struct type *type)
4121 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
4122 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4123 error(state, 0, "array size not specified");
4125 arrays_complete(state, type->left);
4129 static unsigned int do_integral_promotion(unsigned int type)
4132 if (TYPE_INTEGER(type) &&
4133 TYPE_RANK(type) < TYPE_RANK(TYPE_INT)) {
4139 static unsigned int do_arithmetic_conversion(
4140 unsigned int left, unsigned int right)
4144 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
4145 return TYPE_LDOUBLE;
4147 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
4150 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
4153 left = do_integral_promotion(left);
4154 right = do_integral_promotion(right);
4155 /* If both operands have the same size done */
4156 if (left == right) {
4159 /* If both operands have the same signedness pick the larger */
4160 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
4161 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
4163 /* If the signed type can hold everything use it */
4164 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
4167 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
4170 /* Convert to the unsigned type with the same rank as the signed type */
4171 else if (TYPE_SIGNED(left)) {
4172 return TYPE_MKUNSIGNED(left);
4175 return TYPE_MKUNSIGNED(right);
4179 /* see if two types are the same except for qualifiers */
4180 static int equiv_types(struct type *left, struct type *right)
4183 /* Error if the basic types do not match */
4184 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4187 type = left->type & TYPE_MASK;
4188 /* if the basic types match and it is an arithmetic type we are done */
4189 if (TYPE_ARITHMETIC(type)) {
4192 /* If it is a pointer type recurse and keep testing */
4193 if (type == TYPE_POINTER) {
4194 return equiv_types(left->left, right->left);
4196 else if (type == TYPE_ARRAY) {
4197 return (left->elements == right->elements) &&
4198 equiv_types(left->left, right->left);
4200 /* test for struct/union equality */
4201 else if (type == TYPE_STRUCT) {
4202 return left->type_ident == right->type_ident;
4204 /* Test for equivalent functions */
4205 else if (type == TYPE_FUNCTION) {
4206 return equiv_types(left->left, right->left) &&
4207 equiv_types(left->right, right->right);
4209 /* We only see TYPE_PRODUCT as part of function equivalence matching */
4210 else if (type == TYPE_PRODUCT) {
4211 return equiv_types(left->left, right->left) &&
4212 equiv_types(left->right, right->right);
4214 /* We should see TYPE_OVERLAP */
4220 static int equiv_ptrs(struct type *left, struct type *right)
4222 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4223 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4226 return equiv_types(left->left, right->left);
4229 static struct type *compatible_types(struct type *left, struct type *right)
4231 struct type *result;
4232 unsigned int type, qual_type;
4233 /* Error if the basic types do not match */
4234 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4237 type = left->type & TYPE_MASK;
4238 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4240 /* if the basic types match and it is an arithmetic type we are done */
4241 if (TYPE_ARITHMETIC(type)) {
4242 result = new_type(qual_type, 0, 0);
4244 /* If it is a pointer type recurse and keep testing */
4245 else if (type == TYPE_POINTER) {
4246 result = compatible_types(left->left, right->left);
4248 result = new_type(qual_type, result, 0);
4251 /* test for struct/union equality */
4252 else if (type == TYPE_STRUCT) {
4253 if (left->type_ident == right->type_ident) {
4257 /* Test for equivalent functions */
4258 else if (type == TYPE_FUNCTION) {
4259 struct type *lf, *rf;
4260 lf = compatible_types(left->left, right->left);
4261 rf = compatible_types(left->right, right->right);
4263 result = new_type(qual_type, lf, rf);
4266 /* We only see TYPE_PRODUCT as part of function equivalence matching */
4267 else if (type == TYPE_PRODUCT) {
4268 struct type *lf, *rf;
4269 lf = compatible_types(left->left, right->left);
4270 rf = compatible_types(left->right, right->right);
4272 result = new_type(qual_type, lf, rf);
4276 /* Nothing else is compatible */
4281 static struct type *compatible_ptrs(struct type *left, struct type *right)
4283 struct type *result;
4284 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4285 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4288 result = compatible_types(left->left, right->left);
4290 unsigned int qual_type;
4291 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4292 result = new_type(qual_type, result, 0);
4297 static struct triple *integral_promotion(
4298 struct compile_state *state, struct triple *def)
4302 /* As all operations are carried out in registers
4303 * the values are converted on load I just convert
4304 * logical type of the operand.
4306 if (TYPE_INTEGER(type->type)) {
4307 unsigned int int_type;
4308 int_type = type->type & ~TYPE_MASK;
4309 int_type |= do_integral_promotion(type->type);
4310 if (int_type != type->type) {
4311 def->type = new_type(int_type, 0, 0);
4318 static void arithmetic(struct compile_state *state, struct triple *def)
4320 if (!TYPE_ARITHMETIC(def->type->type)) {
4321 error(state, 0, "arithmetic type expexted");
4325 static void ptr_arithmetic(struct compile_state *state, struct triple *def)
4327 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
4328 error(state, def, "pointer or arithmetic type expected");
4332 static int is_integral(struct triple *ins)
4334 return TYPE_INTEGER(ins->type->type);
4337 static void integral(struct compile_state *state, struct triple *def)
4339 if (!is_integral(def)) {
4340 error(state, 0, "integral type expected");
4345 static void bool(struct compile_state *state, struct triple *def)
4347 if (!TYPE_ARITHMETIC(def->type->type) &&
4348 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
4349 error(state, 0, "arithmetic or pointer type expected");
4353 static int is_signed(struct type *type)
4355 return !!TYPE_SIGNED(type->type);
4358 /* Is this value located in a register otherwise it must be in memory */
4359 static int is_in_reg(struct compile_state *state, struct triple *def)
4362 if (def->op == OP_ADECL) {
4365 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
4368 else if (def->op == OP_VAL_VEC) {
4369 in_reg = is_in_reg(state, RHS(def, 0));
4371 else if (def->op == OP_DOT) {
4372 in_reg = is_in_reg(state, RHS(def, 0));
4375 internal_error(state, 0, "unknown expr storage location");
4381 /* Is this a stable variable location otherwise it must be a temporary */
4382 static int is_stable(struct compile_state *state, struct triple *def)
4389 if ((def->op == OP_ADECL) ||
4390 (def->op == OP_SDECL) ||
4391 (def->op == OP_DEREF) ||
4392 (def->op == OP_BLOBCONST)) {
4395 else if (def->op == OP_DOT) {
4396 ret = is_stable(state, RHS(def, 0));
4398 else if (def->op == OP_VAL_VEC) {
4399 struct triple **vector;
4402 vector = &RHS(def, 0);
4403 for(i = 0; i < def->type->elements; i++) {
4404 if (!is_stable(state, vector[i])) {
4413 static int is_lvalue(struct compile_state *state, struct triple *def)
4420 if (!is_stable(state, def)) {
4423 if (def->op == OP_DOT) {
4424 ret = is_lvalue(state, RHS(def, 0));
4429 static void clvalue(struct compile_state *state, struct triple *def)
4432 internal_error(state, def, "nothing where lvalue expected?");
4434 if (!is_lvalue(state, def)) {
4435 error(state, def, "lvalue expected");
4438 static void lvalue(struct compile_state *state, struct triple *def)
4440 clvalue(state, def);
4441 if (def->type->type & QUAL_CONST) {
4442 error(state, def, "modifable lvalue expected");
4446 static int is_pointer(struct triple *def)
4448 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
4451 static void pointer(struct compile_state *state, struct triple *def)
4453 if (!is_pointer(def)) {
4454 error(state, def, "pointer expected");
4458 static struct triple *int_const(
4459 struct compile_state *state, struct type *type, ulong_t value)
4461 struct triple *result;
4462 switch(type->type & TYPE_MASK) {
4464 case TYPE_INT: case TYPE_UINT:
4465 case TYPE_LONG: case TYPE_ULONG:
4468 internal_error(state, 0, "constant for unkown type");
4470 result = triple(state, OP_INTCONST, type, 0, 0);
4471 result->u.cval = value;
4476 static struct triple *do_mk_addr_expr(struct compile_state *state,
4477 struct triple *expr, struct type *type, ulong_t offset)
4479 struct triple *result;
4480 clvalue(state, expr);
4482 type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
4485 if (expr->op == OP_ADECL) {
4486 error(state, expr, "address of auto variables not supported");
4488 else if (expr->op == OP_SDECL) {
4489 result = triple(state, OP_ADDRCONST, type, 0, 0);
4490 MISC(result, 0) = expr;
4491 result->u.cval = offset;
4493 else if (expr->op == OP_DEREF) {
4494 result = triple(state, OP_ADD, type,
4496 int_const(state, &ulong_type, offset));
4501 static struct triple *mk_addr_expr(
4502 struct compile_state *state, struct triple *expr, ulong_t offset)
4504 return do_mk_addr_expr(state, expr, expr->type, offset);
4507 static struct triple *mk_deref_expr(
4508 struct compile_state *state, struct triple *expr)
4510 struct type *base_type;
4511 pointer(state, expr);
4512 base_type = expr->type->left;
4513 return triple(state, OP_DEREF, base_type, expr, 0);
4516 static struct triple *array_to_pointer(struct compile_state *state, struct triple *def)
4518 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
4520 struct triple *addrconst;
4522 TYPE_POINTER | (def->type->type & QUAL_MASK),
4523 def->type->left, 0);
4524 addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
4525 MISC(addrconst, 0) = def;
4531 static struct triple *deref_field(
4532 struct compile_state *state, struct triple *expr, struct hash_entry *field)
4534 struct triple *result;
4535 struct type *type, *member;
4537 internal_error(state, 0, "No field passed to deref_field");
4541 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4542 error(state, 0, "request for member %s in something not a struct or union",
4545 member = field_type(state, type, field);
4546 if ((type->type & STOR_MASK) == STOR_PERM) {
4547 /* Do the pointer arithmetic to get a deref the field */
4549 offset = field_offset(state, type, field);
4550 result = do_mk_addr_expr(state, expr, member, offset);
4551 result = mk_deref_expr(state, result);
4554 /* Find the variable for the field I want. */
4555 result = triple(state, OP_DOT, member, expr, 0);
4556 result->u.field = field;
4561 static struct triple *read_expr(struct compile_state *state, struct triple *def)
4567 if (!is_stable(state, def)) {
4570 /* Tranform an array to a pointer to the first element */
4572 #warning "CHECK_ME is this the right place to transform arrays to pointers?"
4573 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
4574 return array_to_pointer(state, def);
4576 if (is_in_reg(state, def)) {
4581 return triple(state, op, def->type, def, 0);
4584 static void write_compatible(struct compile_state *state,
4585 struct type *dest, struct type *rval)
4588 /* Both operands have arithmetic type */
4589 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
4592 /* One operand is a pointer and the other is a pointer to void */
4593 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
4594 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
4595 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
4596 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
4599 /* If both types are the same without qualifiers we are good */
4600 else if (equiv_ptrs(dest, rval)) {
4603 /* test for struct/union equality */
4604 else if (((dest->type & TYPE_MASK) == TYPE_STRUCT) &&
4605 ((rval->type & TYPE_MASK) == TYPE_STRUCT) &&
4606 (dest->type_ident == rval->type_ident)) {
4610 error(state, 0, "Incompatible types in assignment");
4614 static struct triple *write_expr(
4615 struct compile_state *state, struct triple *dest, struct triple *rval)
4622 internal_error(state, 0, "missing rval");
4625 if (rval->op == OP_LIST) {
4626 internal_error(state, 0, "expression of type OP_LIST?");
4628 if (!is_lvalue(state, dest)) {
4629 internal_error(state, 0, "writing to a non lvalue?");
4631 if (dest->type->type & QUAL_CONST) {
4632 internal_error(state, 0, "modifable lvalue expexted");
4635 write_compatible(state, dest->type, rval->type);
4637 /* Now figure out which assignment operator to use */
4639 if (is_in_reg(state, dest)) {
4644 def = triple(state, op, dest->type, dest, rval);
4648 static struct triple *init_expr(
4649 struct compile_state *state, struct triple *dest, struct triple *rval)
4655 internal_error(state, 0, "missing rval");
4657 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
4658 rval = read_expr(state, rval);
4659 def = write_expr(state, dest, rval);
4662 /* Fill in the array size if necessary */
4663 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
4664 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
4665 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4666 dest->type->elements = rval->type->elements;
4669 if (!equiv_types(dest->type, rval->type)) {
4670 error(state, 0, "Incompatible types in inializer");
4672 MISC(dest, 0) = rval;
4673 insert_triple(state, dest, rval);
4674 rval->id |= TRIPLE_FLAG_FLATTENED;
4675 use_triple(MISC(dest, 0), dest);
4680 struct type *arithmetic_result(
4681 struct compile_state *state, struct triple *left, struct triple *right)
4684 /* Sanity checks to ensure I am working with arithmetic types */
4685 arithmetic(state, left);
4686 arithmetic(state, right);
4688 do_arithmetic_conversion(
4690 right->type->type), 0, 0);
4694 struct type *ptr_arithmetic_result(
4695 struct compile_state *state, struct triple *left, struct triple *right)
4698 /* Sanity checks to ensure I am working with the proper types */
4699 ptr_arithmetic(state, left);
4700 arithmetic(state, right);
4701 if (TYPE_ARITHMETIC(left->type->type) &&
4702 TYPE_ARITHMETIC(right->type->type)) {
4703 type = arithmetic_result(state, left, right);
4705 else if (TYPE_PTR(left->type->type)) {
4709 internal_error(state, 0, "huh?");
4716 /* boolean helper function */
4718 static struct triple *ltrue_expr(struct compile_state *state,
4719 struct triple *expr)
4722 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
4723 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
4724 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
4725 /* If the expression is already boolean do nothing */
4728 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
4734 static struct triple *lfalse_expr(struct compile_state *state,
4735 struct triple *expr)
4737 return triple(state, OP_LFALSE, &int_type, expr, 0);
4740 static struct triple *cond_expr(
4741 struct compile_state *state,
4742 struct triple *test, struct triple *left, struct triple *right)
4745 struct type *result_type;
4746 unsigned int left_type, right_type;
4748 left_type = left->type->type;
4749 right_type = right->type->type;
4751 /* Both operands have arithmetic type */
4752 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
4753 result_type = arithmetic_result(state, left, right);
4755 /* Both operands have void type */
4756 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
4757 ((right_type & TYPE_MASK) == TYPE_VOID)) {
4758 result_type = &void_type;
4760 /* pointers to the same type... */
4761 else if ((result_type = compatible_ptrs(left->type, right->type))) {
4764 /* Both operands are pointers and left is a pointer to void */
4765 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4766 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4767 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4768 result_type = right->type;
4770 /* Both operands are pointers and right is a pointer to void */
4771 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4772 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4773 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4774 result_type = left->type;
4777 error(state, 0, "Incompatible types in conditional expression");
4779 /* Cleanup and invert the test */
4780 test = lfalse_expr(state, read_expr(state, test));
4781 def = new_triple(state, OP_COND, result_type, 0, 3);
4782 def->param[0] = test;
4783 def->param[1] = left;
4784 def->param[2] = right;
4789 static int expr_depth(struct compile_state *state, struct triple *ins)
4793 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
4796 else if (ins->op == OP_DEREF) {
4797 count = expr_depth(state, RHS(ins, 0)) - 1;
4799 else if (ins->op == OP_VAL) {
4800 count = expr_depth(state, RHS(ins, 0)) - 1;
4802 else if (ins->op == OP_COMMA) {
4804 ldepth = expr_depth(state, RHS(ins, 0));
4805 rdepth = expr_depth(state, RHS(ins, 1));
4806 count = (ldepth >= rdepth)? ldepth : rdepth;
4808 else if (ins->op == OP_CALL) {
4809 /* Don't figure the depth of a call just guess it is huge */
4813 struct triple **expr;
4814 expr = triple_rhs(state, ins, 0);
4815 for(;expr; expr = triple_rhs(state, ins, expr)) {
4818 depth = expr_depth(state, *expr);
4819 if (depth > count) {
4828 static struct triple *flatten(
4829 struct compile_state *state, struct triple *first, struct triple *ptr);
4831 static struct triple *flatten_generic(
4832 struct compile_state *state, struct triple *first, struct triple *ptr)
4836 struct triple **ins;
4839 /* Only operations with just a rhs should come here */
4840 rhs = TRIPLE_RHS(ptr->sizes);
4841 lhs = TRIPLE_LHS(ptr->sizes);
4842 if (TRIPLE_SIZE(ptr->sizes) != lhs + rhs) {
4843 internal_error(state, ptr, "unexpected args for: %d %s",
4844 ptr->op, tops(ptr->op));
4846 /* Find the depth of the rhs elements */
4847 for(i = 0; i < rhs; i++) {
4848 vector[i].ins = &RHS(ptr, i);
4849 vector[i].depth = expr_depth(state, *vector[i].ins);
4851 /* Selection sort the rhs */
4852 for(i = 0; i < rhs; i++) {
4854 for(j = i + 1; j < rhs; j++ ) {
4855 if (vector[j].depth > vector[max].depth) {
4860 struct rhs_vector tmp;
4862 vector[i] = vector[max];
4866 /* Now flatten the rhs elements */
4867 for(i = 0; i < rhs; i++) {
4868 *vector[i].ins = flatten(state, first, *vector[i].ins);
4869 use_triple(*vector[i].ins, ptr);
4872 /* Now flatten the lhs elements */
4873 for(i = 0; i < lhs; i++) {
4874 struct triple **ins = &LHS(ptr, i);
4875 *ins = flatten(state, first, *ins);
4876 use_triple(*ins, ptr);
4881 static struct triple *flatten_land(
4882 struct compile_state *state, struct triple *first, struct triple *ptr)
4884 struct triple *left, *right;
4885 struct triple *val, *test, *jmp, *label1, *end;
4887 /* Find the triples */
4889 right = RHS(ptr, 1);
4891 /* Generate the needed triples */
4894 /* Thread the triples together */
4895 val = flatten(state, first, variable(state, ptr->type));
4896 left = flatten(state, first, write_expr(state, val, left));
4897 test = flatten(state, first,
4898 lfalse_expr(state, read_expr(state, val)));
4899 jmp = flatten(state, first, branch(state, end, test));
4900 label1 = flatten(state, first, label(state));
4901 right = flatten(state, first, write_expr(state, val, right));
4902 TARG(jmp, 0) = flatten(state, first, end);
4904 /* Now give the caller something to chew on */
4905 return read_expr(state, val);
4908 static struct triple *flatten_lor(
4909 struct compile_state *state, struct triple *first, struct triple *ptr)
4911 struct triple *left, *right;
4912 struct triple *val, *jmp, *label1, *end;
4914 /* Find the triples */
4916 right = RHS(ptr, 1);
4918 /* Generate the needed triples */
4921 /* Thread the triples together */
4922 val = flatten(state, first, variable(state, ptr->type));
4923 left = flatten(state, first, write_expr(state, val, left));
4924 jmp = flatten(state, first, branch(state, end, left));
4925 label1 = flatten(state, first, label(state));
4926 right = flatten(state, first, write_expr(state, val, right));
4927 TARG(jmp, 0) = flatten(state, first, end);
4930 /* Now give the caller something to chew on */
4931 return read_expr(state, val);
4934 static struct triple *flatten_cond(
4935 struct compile_state *state, struct triple *first, struct triple *ptr)
4937 struct triple *test, *left, *right;
4938 struct triple *val, *mv1, *jmp1, *label1, *mv2, *middle, *jmp2, *end;
4940 /* Find the triples */
4943 right = RHS(ptr, 2);
4945 /* Generate the needed triples */
4947 middle = label(state);
4949 /* Thread the triples together */
4950 val = flatten(state, first, variable(state, ptr->type));
4951 test = flatten(state, first, test);
4952 jmp1 = flatten(state, first, branch(state, middle, test));
4953 label1 = flatten(state, first, label(state));
4954 left = flatten(state, first, left);
4955 mv1 = flatten(state, first, write_expr(state, val, left));
4956 jmp2 = flatten(state, first, branch(state, end, 0));
4957 TARG(jmp1, 0) = flatten(state, first, middle);
4958 right = flatten(state, first, right);
4959 mv2 = flatten(state, first, write_expr(state, val, right));
4960 TARG(jmp2, 0) = flatten(state, first, end);
4962 /* Now give the caller something to chew on */
4963 return read_expr(state, val);
4966 struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
4967 struct occurance *base_occurance)
4969 struct triple *nfunc;
4970 struct triple *nfirst, *ofirst;
4971 struct triple *new, *old;
4974 fprintf(stdout, "\n");
4975 loc(stdout, state, 0);
4976 fprintf(stdout, "\n__________ copy_func _________\n");
4977 print_triple(state, ofunc);
4978 fprintf(stdout, "__________ copy_func _________ done\n\n");
4981 /* Make a new copy of the old function */
4982 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
4984 ofirst = old = RHS(ofunc, 0);
4987 struct occurance *occurance;
4988 int old_lhs, old_rhs;
4989 old_lhs = TRIPLE_LHS(old->sizes);
4990 old_rhs = TRIPLE_RHS(old->sizes);
4991 occurance = inline_occurance(state, base_occurance, old->occurance);
4992 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
4994 if (!triple_stores_block(state, new)) {
4995 memcpy(&new->u, &old->u, sizeof(new->u));
4998 RHS(nfunc, 0) = nfirst = new;
5001 insert_triple(state, nfirst, new);
5003 new->id |= TRIPLE_FLAG_FLATTENED;
5005 /* During the copy remember new as user of old */
5006 use_triple(old, new);
5008 /* Populate the return type if present */
5009 if (old == MISC(ofunc, 0)) {
5010 MISC(nfunc, 0) = new;
5013 } while(old != ofirst);
5015 /* Make a second pass to fix up any unresolved references */
5019 struct triple **oexpr, **nexpr;
5021 /* Lookup where the copy is, to join pointers */
5022 count = TRIPLE_SIZE(old->sizes);
5023 for(i = 0; i < count; i++) {
5024 oexpr = &old->param[i];
5025 nexpr = &new->param[i];
5026 if (!*nexpr && *oexpr && (*oexpr)->use) {
5027 *nexpr = (*oexpr)->use->member;
5028 if (*nexpr == old) {
5029 internal_error(state, 0, "new == old?");
5031 use_triple(*nexpr, new);
5033 if (!*nexpr && *oexpr) {
5034 internal_error(state, 0, "Could not copy %d\n", i);
5039 } while((old != ofirst) && (new != nfirst));
5041 /* Make a third pass to cleanup the extra useses */
5045 unuse_triple(old, new);
5048 } while ((old != ofirst) && (new != nfirst));
5052 static struct triple *flatten_call(
5053 struct compile_state *state, struct triple *first, struct triple *ptr)
5055 /* Inline the function call */
5057 struct triple *ofunc, *nfunc, *nfirst, *param, *result;
5058 struct triple *end, *nend;
5061 /* Find the triples */
5062 ofunc = MISC(ptr, 0);
5063 if (ofunc->op != OP_LIST) {
5064 internal_error(state, 0, "improper function");
5066 nfunc = copy_func(state, ofunc, ptr->occurance);
5067 nfirst = RHS(nfunc, 0)->next;
5068 /* Prepend the parameter reading into the new function list */
5069 ptype = nfunc->type->right;
5070 param = RHS(nfunc, 0)->next;
5071 pvals = TRIPLE_RHS(ptr->sizes);
5072 for(i = 0; i < pvals; i++) {
5076 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
5077 atype = ptype->left;
5079 while((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
5080 param = param->next;
5083 flatten(state, nfirst, write_expr(state, param, arg));
5084 ptype = ptype->right;
5085 param = param->next;
5088 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
5089 result = read_expr(state, MISC(nfunc,0));
5092 fprintf(stdout, "\n");
5093 loc(stdout, state, 0);
5094 fprintf(stdout, "\n__________ flatten_call _________\n");
5095 print_triple(state, nfunc);
5096 fprintf(stdout, "__________ flatten_call _________ done\n\n");
5099 /* Get rid of the extra triples */
5100 nfirst = RHS(nfunc, 0)->next;
5101 free_triple(state, RHS(nfunc, 0));
5103 free_triple(state, nfunc);
5105 /* Append the new function list onto the return list */
5107 nend = nfirst->prev;
5116 static struct triple *flatten(
5117 struct compile_state *state, struct triple *first, struct triple *ptr)
5119 struct triple *orig_ptr;
5124 /* Only flatten triples once */
5125 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
5131 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5132 LHS(ptr, 0) = flatten(state, first, LHS(ptr, 0));
5133 use_triple(LHS(ptr, 0), ptr);
5134 use_triple(RHS(ptr, 0), ptr);
5137 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5141 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5142 return MISC(ptr, 0);
5145 ptr = flatten_land(state, first, ptr);
5148 ptr = flatten_lor(state, first, ptr);
5151 ptr = flatten_cond(state, first, ptr);
5154 ptr = flatten_call(state, first, ptr);
5158 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5159 use_triple(RHS(ptr, 0), ptr);
5162 use_triple(TARG(ptr, 0), ptr);
5163 if (TRIPLE_RHS(ptr->sizes)) {
5164 use_triple(RHS(ptr, 0), ptr);
5165 if (ptr->next != ptr) {
5166 use_triple(ptr->next, ptr);
5171 insert_triple(state, first, ptr);
5172 ptr->id |= TRIPLE_FLAG_FLATTENED;
5173 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
5174 use_triple(MISC(ptr, 0), ptr);
5177 /* Since OP_DEREF is just a marker delete it when I flatten it */
5179 RHS(orig_ptr, 0) = 0;
5180 free_triple(state, orig_ptr);
5184 struct triple *base;
5186 if (base->op == OP_DEREF) {
5187 struct triple *left;
5189 offset = field_offset(state, base->type, ptr->u.field);
5190 left = RHS(base, 0);
5191 ptr = triple(state, OP_ADD, left->type,
5192 read_expr(state, left),
5193 int_const(state, &ulong_type, offset));
5194 free_triple(state, base);
5196 else if (base->op == OP_VAL_VEC) {
5197 base = flatten(state, first, base);
5198 ptr = struct_field(state, base, ptr->u.field);
5203 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5204 use_triple(MISC(ptr, 0), ptr);
5205 use_triple(ptr, MISC(ptr, 0));
5209 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5210 use_triple(MISC(ptr, 0), ptr);
5215 /* Flatten the easy cases we don't override */
5216 ptr = flatten_generic(state, first, ptr);
5219 } while(ptr && (ptr != orig_ptr));
5221 insert_triple(state, first, ptr);
5222 ptr->id |= TRIPLE_FLAG_FLATTENED;
5227 static void release_expr(struct compile_state *state, struct triple *expr)
5229 struct triple *head;
5230 head = label(state);
5231 flatten(state, head, expr);
5232 while(head->next != head) {
5233 release_triple(state, head->next);
5235 free_triple(state, head);
5238 static int replace_rhs_use(struct compile_state *state,
5239 struct triple *orig, struct triple *new, struct triple *use)
5241 struct triple **expr;
5244 expr = triple_rhs(state, use, 0);
5245 for(;expr; expr = triple_rhs(state, use, expr)) {
5246 if (*expr == orig) {
5252 unuse_triple(orig, use);
5253 use_triple(new, use);
5258 static int replace_lhs_use(struct compile_state *state,
5259 struct triple *orig, struct triple *new, struct triple *use)
5261 struct triple **expr;
5264 expr = triple_lhs(state, use, 0);
5265 for(;expr; expr = triple_lhs(state, use, expr)) {
5266 if (*expr == orig) {
5272 unuse_triple(orig, use);
5273 use_triple(new, use);
5278 static void propogate_use(struct compile_state *state,
5279 struct triple *orig, struct triple *new)
5281 struct triple_set *user, *next;
5282 for(user = orig->use; user; user = next) {
5288 found |= replace_rhs_use(state, orig, new, use);
5289 found |= replace_lhs_use(state, orig, new, use);
5291 internal_error(state, use, "use without use");
5295 internal_error(state, orig, "used after propogate_use");
5301 * ===========================
5304 static struct triple *mk_add_expr(
5305 struct compile_state *state, struct triple *left, struct triple *right)
5307 struct type *result_type;
5308 /* Put pointer operands on the left */
5309 if (is_pointer(right)) {
5315 left = read_expr(state, left);
5316 right = read_expr(state, right);
5317 result_type = ptr_arithmetic_result(state, left, right);
5318 if (is_pointer(left)) {
5319 right = triple(state,
5320 is_signed(right->type)? OP_SMUL : OP_UMUL,
5323 int_const(state, &ulong_type,
5324 size_of(state, left->type->left)));
5326 return triple(state, OP_ADD, result_type, left, right);
5329 static struct triple *mk_sub_expr(
5330 struct compile_state *state, struct triple *left, struct triple *right)
5332 struct type *result_type;
5333 result_type = ptr_arithmetic_result(state, left, right);
5334 left = read_expr(state, left);
5335 right = read_expr(state, right);
5336 if (is_pointer(left)) {
5337 right = triple(state,
5338 is_signed(right->type)? OP_SMUL : OP_UMUL,
5341 int_const(state, &ulong_type,
5342 size_of(state, left->type->left)));
5344 return triple(state, OP_SUB, result_type, left, right);
5347 static struct triple *mk_pre_inc_expr(
5348 struct compile_state *state, struct triple *def)
5352 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
5353 return triple(state, OP_VAL, def->type,
5354 write_expr(state, def, val),
5358 static struct triple *mk_pre_dec_expr(
5359 struct compile_state *state, struct triple *def)
5363 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
5364 return triple(state, OP_VAL, def->type,
5365 write_expr(state, def, val),
5369 static struct triple *mk_post_inc_expr(
5370 struct compile_state *state, struct triple *def)
5374 val = read_expr(state, def);
5375 return triple(state, OP_VAL, def->type,
5376 write_expr(state, def,
5377 mk_add_expr(state, val, int_const(state, &int_type, 1)))
5381 static struct triple *mk_post_dec_expr(
5382 struct compile_state *state, struct triple *def)
5386 val = read_expr(state, def);
5387 return triple(state, OP_VAL, def->type,
5388 write_expr(state, def,
5389 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
5393 static struct triple *mk_subscript_expr(
5394 struct compile_state *state, struct triple *left, struct triple *right)
5396 left = read_expr(state, left);
5397 right = read_expr(state, right);
5398 if (!is_pointer(left) && !is_pointer(right)) {
5399 error(state, left, "subscripted value is not a pointer");
5401 return mk_deref_expr(state, mk_add_expr(state, left, right));
5405 * Compile time evaluation
5406 * ===========================
5408 static int is_const(struct triple *ins)
5410 return IS_CONST_OP(ins->op);
5413 static int constants_equal(struct compile_state *state,
5414 struct triple *left, struct triple *right)
5417 if (!is_const(left) || !is_const(right)) {
5420 else if (left->op != right->op) {
5423 else if (!equiv_types(left->type, right->type)) {
5430 if (left->u.cval == right->u.cval) {
5436 size_t lsize, rsize;
5437 lsize = size_of(state, left->type);
5438 rsize = size_of(state, right->type);
5439 if (lsize != rsize) {
5442 if (memcmp(left->u.blob, right->u.blob, lsize) == 0) {
5448 if ((MISC(left, 0) == MISC(right, 0)) &&
5449 (left->u.cval == right->u.cval)) {
5454 internal_error(state, left, "uknown constant type");
5461 static int is_zero(struct triple *ins)
5463 return is_const(ins) && (ins->u.cval == 0);
5466 static int is_one(struct triple *ins)
5468 return is_const(ins) && (ins->u.cval == 1);
5471 static long_t bsr(ulong_t value)
5474 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
5485 static long_t bsf(ulong_t value)
5488 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
5499 static long_t log2(ulong_t value)
5504 static long_t tlog2(struct triple *ins)
5506 return log2(ins->u.cval);
5509 static int is_pow2(struct triple *ins)
5511 ulong_t value, mask;
5513 if (!is_const(ins)) {
5516 value = ins->u.cval;
5523 return ((value & mask) == value);
5526 static ulong_t read_const(struct compile_state *state,
5527 struct triple *ins, struct triple **expr)
5531 switch(rhs->type->type &TYPE_MASK) {
5543 internal_error(state, rhs, "bad type to read_const\n");
5549 static long_t read_sconst(struct triple *ins, struct triple **expr)
5553 return (long_t)(rhs->u.cval);
5556 static void unuse_rhs(struct compile_state *state, struct triple *ins)
5558 struct triple **expr;
5559 expr = triple_rhs(state, ins, 0);
5560 for(;expr;expr = triple_rhs(state, ins, expr)) {
5562 unuse_triple(*expr, ins);
5568 static void unuse_lhs(struct compile_state *state, struct triple *ins)
5570 struct triple **expr;
5571 expr = triple_lhs(state, ins, 0);
5572 for(;expr;expr = triple_lhs(state, ins, expr)) {
5573 unuse_triple(*expr, ins);
5578 static void check_lhs(struct compile_state *state, struct triple *ins)
5580 struct triple **expr;
5581 expr = triple_lhs(state, ins, 0);
5582 for(;expr;expr = triple_lhs(state, ins, expr)) {
5583 internal_error(state, ins, "unexpected lhs");
5587 static void check_targ(struct compile_state *state, struct triple *ins)
5589 struct triple **expr;
5590 expr = triple_targ(state, ins, 0);
5591 for(;expr;expr = triple_targ(state, ins, expr)) {
5592 internal_error(state, ins, "unexpected targ");
5596 static void wipe_ins(struct compile_state *state, struct triple *ins)
5598 /* Becareful which instructions you replace the wiped
5599 * instruction with, as there are not enough slots
5600 * in all instructions to hold all others.
5602 check_targ(state, ins);
5603 unuse_rhs(state, ins);
5604 unuse_lhs(state, ins);
5607 static void mkcopy(struct compile_state *state,
5608 struct triple *ins, struct triple *rhs)
5610 wipe_ins(state, ins);
5612 ins->sizes = TRIPLE_SIZES(0, 1, 0, 0);
5614 use_triple(RHS(ins, 0), ins);
5617 static void mkconst(struct compile_state *state,
5618 struct triple *ins, ulong_t value)
5620 if (!is_integral(ins) && !is_pointer(ins)) {
5621 internal_error(state, ins, "unknown type to make constant\n");
5623 wipe_ins(state, ins);
5624 ins->op = OP_INTCONST;
5625 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
5626 ins->u.cval = value;
5629 static void mkaddr_const(struct compile_state *state,
5630 struct triple *ins, struct triple *sdecl, ulong_t value)
5632 wipe_ins(state, ins);
5633 ins->op = OP_ADDRCONST;
5634 ins->sizes = TRIPLE_SIZES(0, 0, 1, 0);
5635 MISC(ins, 0) = sdecl;
5636 ins->u.cval = value;
5637 use_triple(sdecl, ins);
5640 /* Transform multicomponent variables into simple register variables */
5641 static void flatten_structures(struct compile_state *state)
5643 struct triple *ins, *first;
5644 first = RHS(state->main_function, 0);
5646 /* Pass one expand structure values into valvecs.
5650 struct triple *next;
5652 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5653 if (ins->op == OP_VAL_VEC) {
5656 else if ((ins->op == OP_LOAD) || (ins->op == OP_READ)) {
5657 struct triple *def, **vector;
5664 get_occurance(ins->occurance);
5665 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
5668 vector = &RHS(next, 0);
5669 tptr = next->type->left;
5670 for(i = 0; i < next->type->elements; i++) {
5671 struct triple *sfield;
5674 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5675 mtype = mtype->left;
5677 sfield = deref_field(state, def, mtype->field_ident);
5680 state, op, mtype, sfield, 0);
5681 put_occurance(vector[i]->occurance);
5682 get_occurance(next->occurance);
5683 vector[i]->occurance = next->occurance;
5686 propogate_use(state, ins, next);
5687 flatten(state, ins, next);
5688 free_triple(state, ins);
5690 else if ((ins->op == OP_STORE) || (ins->op == OP_WRITE)) {
5691 struct triple *src, *dst, **vector;
5699 get_occurance(ins->occurance);
5700 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
5703 vector = &RHS(next, 0);
5704 tptr = next->type->left;
5705 for(i = 0; i < ins->type->elements; i++) {
5706 struct triple *dfield, *sfield;
5709 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5710 mtype = mtype->left;
5712 sfield = deref_field(state, src, mtype->field_ident);
5713 dfield = deref_field(state, dst, mtype->field_ident);
5715 state, op, mtype, dfield, sfield);
5716 put_occurance(vector[i]->occurance);
5717 get_occurance(next->occurance);
5718 vector[i]->occurance = next->occurance;
5721 propogate_use(state, ins, next);
5722 flatten(state, ins, next);
5723 free_triple(state, ins);
5727 } while(ins != first);
5728 /* Pass two flatten the valvecs.
5732 struct triple *next;
5734 if (ins->op == OP_VAL_VEC) {
5735 release_triple(state, ins);
5738 } while(ins != first);
5739 /* Pass three verify the state and set ->id to 0.
5743 ins->id &= ~TRIPLE_FLAG_FLATTENED;
5744 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5745 internal_error(state, ins, "STRUCT_TYPE remains?");
5747 if (ins->op == OP_DOT) {
5748 internal_error(state, ins, "OP_DOT remains?");
5750 if (ins->op == OP_VAL_VEC) {
5751 internal_error(state, ins, "OP_VAL_VEC remains?");
5754 } while(ins != first);
5757 /* For those operations that cannot be simplified */
5758 static void simplify_noop(struct compile_state *state, struct triple *ins)
5763 static void simplify_smul(struct compile_state *state, struct triple *ins)
5765 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5768 RHS(ins, 0) = RHS(ins, 1);
5771 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5773 left = read_sconst(ins, &RHS(ins, 0));
5774 right = read_sconst(ins, &RHS(ins, 1));
5775 mkconst(state, ins, left * right);
5777 else if (is_zero(RHS(ins, 1))) {
5778 mkconst(state, ins, 0);
5780 else if (is_one(RHS(ins, 1))) {
5781 mkcopy(state, ins, RHS(ins, 0));
5783 else if (is_pow2(RHS(ins, 1))) {
5785 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5787 insert_triple(state, ins, val);
5788 unuse_triple(RHS(ins, 1), ins);
5789 use_triple(val, ins);
5794 static void simplify_umul(struct compile_state *state, struct triple *ins)
5796 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5799 RHS(ins, 0) = RHS(ins, 1);
5802 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5803 ulong_t left, right;
5804 left = read_const(state, ins, &RHS(ins, 0));
5805 right = read_const(state, ins, &RHS(ins, 1));
5806 mkconst(state, ins, left * right);
5808 else if (is_zero(RHS(ins, 1))) {
5809 mkconst(state, ins, 0);
5811 else if (is_one(RHS(ins, 1))) {
5812 mkcopy(state, ins, RHS(ins, 0));
5814 else if (is_pow2(RHS(ins, 1))) {
5816 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5818 insert_triple(state, ins, val);
5819 unuse_triple(RHS(ins, 1), ins);
5820 use_triple(val, ins);
5825 static void simplify_sdiv(struct compile_state *state, struct triple *ins)
5827 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5829 left = read_sconst(ins, &RHS(ins, 0));
5830 right = read_sconst(ins, &RHS(ins, 1));
5831 mkconst(state, ins, left / right);
5833 else if (is_zero(RHS(ins, 0))) {
5834 mkconst(state, ins, 0);
5836 else if (is_zero(RHS(ins, 1))) {
5837 error(state, ins, "division by zero");
5839 else if (is_one(RHS(ins, 1))) {
5840 mkcopy(state, ins, RHS(ins, 0));
5842 else if (is_pow2(RHS(ins, 1))) {
5844 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5846 insert_triple(state, ins, val);
5847 unuse_triple(RHS(ins, 1), ins);
5848 use_triple(val, ins);
5853 static void simplify_udiv(struct compile_state *state, struct triple *ins)
5855 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5856 ulong_t left, right;
5857 left = read_const(state, ins, &RHS(ins, 0));
5858 right = read_const(state, ins, &RHS(ins, 1));
5859 mkconst(state, ins, left / right);
5861 else if (is_zero(RHS(ins, 0))) {
5862 mkconst(state, ins, 0);
5864 else if (is_zero(RHS(ins, 1))) {
5865 error(state, ins, "division by zero");
5867 else if (is_one(RHS(ins, 1))) {
5868 mkcopy(state, ins, RHS(ins, 0));
5870 else if (is_pow2(RHS(ins, 1))) {
5872 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5874 insert_triple(state, ins, val);
5875 unuse_triple(RHS(ins, 1), ins);
5876 use_triple(val, ins);
5881 static void simplify_smod(struct compile_state *state, struct triple *ins)
5883 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5885 left = read_const(state, ins, &RHS(ins, 0));
5886 right = read_const(state, ins, &RHS(ins, 1));
5887 mkconst(state, ins, left % right);
5889 else if (is_zero(RHS(ins, 0))) {
5890 mkconst(state, ins, 0);
5892 else if (is_zero(RHS(ins, 1))) {
5893 error(state, ins, "division by zero");
5895 else if (is_one(RHS(ins, 1))) {
5896 mkconst(state, ins, 0);
5898 else if (is_pow2(RHS(ins, 1))) {
5900 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
5902 insert_triple(state, ins, val);
5903 unuse_triple(RHS(ins, 1), ins);
5904 use_triple(val, ins);
5908 static void simplify_umod(struct compile_state *state, struct triple *ins)
5910 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5911 ulong_t left, right;
5912 left = read_const(state, ins, &RHS(ins, 0));
5913 right = read_const(state, ins, &RHS(ins, 1));
5914 mkconst(state, ins, left % right);
5916 else if (is_zero(RHS(ins, 0))) {
5917 mkconst(state, ins, 0);
5919 else if (is_zero(RHS(ins, 1))) {
5920 error(state, ins, "division by zero");
5922 else if (is_one(RHS(ins, 1))) {
5923 mkconst(state, ins, 0);
5925 else if (is_pow2(RHS(ins, 1))) {
5927 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
5929 insert_triple(state, ins, val);
5930 unuse_triple(RHS(ins, 1), ins);
5931 use_triple(val, ins);
5936 static void simplify_add(struct compile_state *state, struct triple *ins)
5938 /* start with the pointer on the left */
5939 if (is_pointer(RHS(ins, 1))) {
5942 RHS(ins, 0) = RHS(ins, 1);
5945 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5946 if (!is_pointer(RHS(ins, 0))) {
5947 ulong_t left, right;
5948 left = read_const(state, ins, &RHS(ins, 0));
5949 right = read_const(state, ins, &RHS(ins, 1));
5950 mkconst(state, ins, left + right);
5952 else /* op == OP_ADDRCONST */ {
5953 struct triple *sdecl;
5954 ulong_t left, right;
5955 sdecl = MISC(RHS(ins, 0), 0);
5956 left = RHS(ins, 0)->u.cval;
5957 right = RHS(ins, 1)->u.cval;
5958 mkaddr_const(state, ins, sdecl, left + right);
5961 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5964 RHS(ins, 1) = RHS(ins, 0);
5969 static void simplify_sub(struct compile_state *state, struct triple *ins)
5971 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5972 if (!is_pointer(RHS(ins, 0))) {
5973 ulong_t left, right;
5974 left = read_const(state, ins, &RHS(ins, 0));
5975 right = read_const(state, ins, &RHS(ins, 1));
5976 mkconst(state, ins, left - right);
5978 else /* op == OP_ADDRCONST */ {
5979 struct triple *sdecl;
5980 ulong_t left, right;
5981 sdecl = MISC(RHS(ins, 0), 0);
5982 left = RHS(ins, 0)->u.cval;
5983 right = RHS(ins, 1)->u.cval;
5984 mkaddr_const(state, ins, sdecl, left - right);
5989 static void simplify_sl(struct compile_state *state, struct triple *ins)
5991 if (is_const(RHS(ins, 1))) {
5993 right = read_const(state, ins, &RHS(ins, 1));
5994 if (right >= (size_of(state, ins->type)*8)) {
5995 warning(state, ins, "left shift count >= width of type");
5998 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5999 ulong_t left, right;
6000 left = read_const(state, ins, &RHS(ins, 0));
6001 right = read_const(state, ins, &RHS(ins, 1));
6002 mkconst(state, ins, left << right);
6006 static void simplify_usr(struct compile_state *state, struct triple *ins)
6008 if (is_const(RHS(ins, 1))) {
6010 right = read_const(state, ins, &RHS(ins, 1));
6011 if (right >= (size_of(state, ins->type)*8)) {
6012 warning(state, ins, "right shift count >= width of type");
6015 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6016 ulong_t left, right;
6017 left = read_const(state, ins, &RHS(ins, 0));
6018 right = read_const(state, ins, &RHS(ins, 1));
6019 mkconst(state, ins, left >> right);
6023 static void simplify_ssr(struct compile_state *state, struct triple *ins)
6025 if (is_const(RHS(ins, 1))) {
6027 right = read_const(state, ins, &RHS(ins, 1));
6028 if (right >= (size_of(state, ins->type)*8)) {
6029 warning(state, ins, "right shift count >= width of type");
6032 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6034 left = read_sconst(ins, &RHS(ins, 0));
6035 right = read_sconst(ins, &RHS(ins, 1));
6036 mkconst(state, ins, left >> right);
6040 static void simplify_and(struct compile_state *state, struct triple *ins)
6042 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6043 ulong_t left, right;
6044 left = read_const(state, ins, &RHS(ins, 0));
6045 right = read_const(state, ins, &RHS(ins, 1));
6046 mkconst(state, ins, left & right);
6050 static void simplify_or(struct compile_state *state, struct triple *ins)
6052 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6053 ulong_t left, right;
6054 left = read_const(state, ins, &RHS(ins, 0));
6055 right = read_const(state, ins, &RHS(ins, 1));
6056 mkconst(state, ins, left | right);
6060 static void simplify_xor(struct compile_state *state, struct triple *ins)
6062 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6063 ulong_t left, right;
6064 left = read_const(state, ins, &RHS(ins, 0));
6065 right = read_const(state, ins, &RHS(ins, 1));
6066 mkconst(state, ins, left ^ right);
6070 static void simplify_pos(struct compile_state *state, struct triple *ins)
6072 if (is_const(RHS(ins, 0))) {
6073 mkconst(state, ins, RHS(ins, 0)->u.cval);
6076 mkcopy(state, ins, RHS(ins, 0));
6080 static void simplify_neg(struct compile_state *state, struct triple *ins)
6082 if (is_const(RHS(ins, 0))) {
6084 left = read_const(state, ins, &RHS(ins, 0));
6085 mkconst(state, ins, -left);
6087 else if (RHS(ins, 0)->op == OP_NEG) {
6088 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
6092 static void simplify_invert(struct compile_state *state, struct triple *ins)
6094 if (is_const(RHS(ins, 0))) {
6096 left = read_const(state, ins, &RHS(ins, 0));
6097 mkconst(state, ins, ~left);
6101 static void simplify_eq(struct compile_state *state, struct triple *ins)
6103 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6104 ulong_t left, right;
6105 left = read_const(state, ins, &RHS(ins, 0));
6106 right = read_const(state, ins, &RHS(ins, 1));
6107 mkconst(state, ins, left == right);
6109 else if (RHS(ins, 0) == RHS(ins, 1)) {
6110 mkconst(state, ins, 1);
6114 static void simplify_noteq(struct compile_state *state, struct triple *ins)
6116 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6117 ulong_t left, right;
6118 left = read_const(state, ins, &RHS(ins, 0));
6119 right = read_const(state, ins, &RHS(ins, 1));
6120 mkconst(state, ins, left != right);
6122 else if (RHS(ins, 0) == RHS(ins, 1)) {
6123 mkconst(state, ins, 0);
6127 static void simplify_sless(struct compile_state *state, struct triple *ins)
6129 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6131 left = read_sconst(ins, &RHS(ins, 0));
6132 right = read_sconst(ins, &RHS(ins, 1));
6133 mkconst(state, ins, left < right);
6135 else if (RHS(ins, 0) == RHS(ins, 1)) {
6136 mkconst(state, ins, 0);
6140 static void simplify_uless(struct compile_state *state, struct triple *ins)
6142 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6143 ulong_t left, right;
6144 left = read_const(state, ins, &RHS(ins, 0));
6145 right = read_const(state, ins, &RHS(ins, 1));
6146 mkconst(state, ins, left < right);
6148 else if (is_zero(RHS(ins, 0))) {
6149 mkconst(state, ins, 1);
6151 else if (RHS(ins, 0) == RHS(ins, 1)) {
6152 mkconst(state, ins, 0);
6156 static void simplify_smore(struct compile_state *state, struct triple *ins)
6158 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6160 left = read_sconst(ins, &RHS(ins, 0));
6161 right = read_sconst(ins, &RHS(ins, 1));
6162 mkconst(state, ins, left > right);
6164 else if (RHS(ins, 0) == RHS(ins, 1)) {
6165 mkconst(state, ins, 0);
6169 static void simplify_umore(struct compile_state *state, struct triple *ins)
6171 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6172 ulong_t left, right;
6173 left = read_const(state, ins, &RHS(ins, 0));
6174 right = read_const(state, ins, &RHS(ins, 1));
6175 mkconst(state, ins, left > right);
6177 else if (is_zero(RHS(ins, 1))) {
6178 mkconst(state, ins, 1);
6180 else if (RHS(ins, 0) == RHS(ins, 1)) {
6181 mkconst(state, ins, 0);
6186 static void simplify_slesseq(struct compile_state *state, struct triple *ins)
6188 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6190 left = read_sconst(ins, &RHS(ins, 0));
6191 right = read_sconst(ins, &RHS(ins, 1));
6192 mkconst(state, ins, left <= right);
6194 else if (RHS(ins, 0) == RHS(ins, 1)) {
6195 mkconst(state, ins, 1);
6199 static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
6201 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6202 ulong_t left, right;
6203 left = read_const(state, ins, &RHS(ins, 0));
6204 right = read_const(state, ins, &RHS(ins, 1));
6205 mkconst(state, ins, left <= right);
6207 else if (is_zero(RHS(ins, 0))) {
6208 mkconst(state, ins, 1);
6210 else if (RHS(ins, 0) == RHS(ins, 1)) {
6211 mkconst(state, ins, 1);
6215 static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
6217 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 0))) {
6219 left = read_sconst(ins, &RHS(ins, 0));
6220 right = read_sconst(ins, &RHS(ins, 1));
6221 mkconst(state, ins, left >= right);
6223 else if (RHS(ins, 0) == RHS(ins, 1)) {
6224 mkconst(state, ins, 1);
6228 static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
6230 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6231 ulong_t left, right;
6232 left = read_const(state, ins, &RHS(ins, 0));
6233 right = read_const(state, ins, &RHS(ins, 1));
6234 mkconst(state, ins, left >= right);
6236 else if (is_zero(RHS(ins, 1))) {
6237 mkconst(state, ins, 1);
6239 else if (RHS(ins, 0) == RHS(ins, 1)) {
6240 mkconst(state, ins, 1);
6244 static void simplify_lfalse(struct compile_state *state, struct triple *ins)
6246 if (is_const(RHS(ins, 0))) {
6248 left = read_const(state, ins, &RHS(ins, 0));
6249 mkconst(state, ins, left == 0);
6251 /* Otherwise if I am the only user... */
6252 else if ((RHS(ins, 0)->use->member == ins) && (RHS(ins, 0)->use->next == 0)) {
6254 /* Invert a boolean operation */
6255 switch(RHS(ins, 0)->op) {
6256 case OP_LTRUE: RHS(ins, 0)->op = OP_LFALSE; break;
6257 case OP_LFALSE: RHS(ins, 0)->op = OP_LTRUE; break;
6258 case OP_EQ: RHS(ins, 0)->op = OP_NOTEQ; break;
6259 case OP_NOTEQ: RHS(ins, 0)->op = OP_EQ; break;
6260 case OP_SLESS: RHS(ins, 0)->op = OP_SMOREEQ; break;
6261 case OP_ULESS: RHS(ins, 0)->op = OP_UMOREEQ; break;
6262 case OP_SMORE: RHS(ins, 0)->op = OP_SLESSEQ; break;
6263 case OP_UMORE: RHS(ins, 0)->op = OP_ULESSEQ; break;
6264 case OP_SLESSEQ: RHS(ins, 0)->op = OP_SMORE; break;
6265 case OP_ULESSEQ: RHS(ins, 0)->op = OP_UMORE; break;
6266 case OP_SMOREEQ: RHS(ins, 0)->op = OP_SLESS; break;
6267 case OP_UMOREEQ: RHS(ins, 0)->op = OP_ULESS; break;
6273 mkcopy(state, ins, RHS(ins, 0));
6278 static void simplify_ltrue (struct compile_state *state, struct triple *ins)
6280 if (is_const(RHS(ins, 0))) {
6282 left = read_const(state, ins, &RHS(ins, 0));
6283 mkconst(state, ins, left != 0);
6285 else switch(RHS(ins, 0)->op) {
6286 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
6287 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
6288 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
6289 mkcopy(state, ins, RHS(ins, 0));
6294 static void simplify_copy(struct compile_state *state, struct triple *ins)
6296 if (is_const(RHS(ins, 0))) {
6297 switch(RHS(ins, 0)->op) {
6301 left = read_const(state, ins, &RHS(ins, 0));
6302 mkconst(state, ins, left);
6307 struct triple *sdecl;
6309 sdecl = MISC(RHS(ins, 0), 0);
6310 offset = RHS(ins, 0)->u.cval;
6311 mkaddr_const(state, ins, sdecl, offset);
6315 internal_error(state, ins, "uknown constant");
6321 static void simplify_branch(struct compile_state *state, struct triple *ins)
6323 struct block *block;
6324 if (ins->op != OP_BRANCH) {
6325 internal_error(state, ins, "not branch");
6327 if (ins->use != 0) {
6328 internal_error(state, ins, "branch use");
6330 #warning "FIXME implement simplify branch."
6331 /* The challenge here with simplify branch is that I need to
6332 * make modifications to the control flow graph as well
6333 * as to the branch instruction itself.
6335 block = ins->u.block;
6337 if (TRIPLE_RHS(ins->sizes) && is_const(RHS(ins, 0))) {
6338 struct triple *targ;
6340 value = read_const(state, ins, &RHS(ins, 0));
6341 unuse_triple(RHS(ins, 0), ins);
6342 targ = TARG(ins, 0);
6343 ins->sizes = TRIPLE_SIZES(0, 0, 0, 1);
6345 unuse_triple(ins->next, ins);
6346 TARG(ins, 0) = targ;
6349 unuse_triple(targ, ins);
6350 TARG(ins, 0) = ins->next;
6352 #warning "FIXME handle the case of making a branch unconditional"
6354 if (TARG(ins, 0) == ins->next) {
6355 unuse_triple(ins->next, ins);
6356 if (TRIPLE_RHS(ins->sizes)) {
6357 unuse_triple(RHS(ins, 0), ins);
6358 unuse_triple(ins->next, ins);
6360 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
6363 internal_error(state, ins, "noop use != 0");
6365 #warning "FIXME handle the case of killing a branch"
6369 static void simplify_phi(struct compile_state *state, struct triple *ins)
6371 struct triple **expr;
6373 expr = triple_rhs(state, ins, 0);
6374 if (!*expr || !is_const(*expr)) {
6377 value = read_const(state, ins, expr);
6378 for(;expr;expr = triple_rhs(state, ins, expr)) {
6379 if (!*expr || !is_const(*expr)) {
6382 if (value != read_const(state, ins, expr)) {
6386 mkconst(state, ins, value);
6390 static void simplify_bsf(struct compile_state *state, struct triple *ins)
6392 if (is_const(RHS(ins, 0))) {
6394 left = read_const(state, ins, &RHS(ins, 0));
6395 mkconst(state, ins, bsf(left));
6399 static void simplify_bsr(struct compile_state *state, struct triple *ins)
6401 if (is_const(RHS(ins, 0))) {
6403 left = read_const(state, ins, &RHS(ins, 0));
6404 mkconst(state, ins, bsr(left));
6409 typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
6410 static const simplify_t table_simplify[] = {
6412 #define simplify_smul simplify_noop
6413 #define simplify_umul simplify_noop
6414 #define simplify_sdiv simplify_noop
6415 #define simplify_udiv simplify_noop
6416 #define simplify_smod simplify_noop
6417 #define simplify_umod simplify_noop
6420 #define simplify_add simplify_noop
6421 #define simplify_sub simplify_noop
6424 #define simplify_sl simplify_noop
6425 #define simplify_usr simplify_noop
6426 #define simplify_ssr simplify_noop
6429 #define simplify_and simplify_noop
6430 #define simplify_xor simplify_noop
6431 #define simplify_or simplify_noop
6434 #define simplify_pos simplify_noop
6435 #define simplify_neg simplify_noop
6436 #define simplify_invert simplify_noop
6440 #define simplify_eq simplify_noop
6441 #define simplify_noteq simplify_noop
6444 #define simplify_sless simplify_noop
6445 #define simplify_uless simplify_noop
6446 #define simplify_smore simplify_noop
6447 #define simplify_umore simplify_noop
6450 #define simplify_slesseq simplify_noop
6451 #define simplify_ulesseq simplify_noop
6452 #define simplify_smoreeq simplify_noop
6453 #define simplify_umoreeq simplify_noop
6456 #define simplify_lfalse simplify_noop
6459 #define simplify_ltrue simplify_noop
6463 #define simplify_copy simplify_noop
6467 #define simplify_branch simplify_noop
6471 #define simplify_phi simplify_noop
6475 #define simplify_bsf simplify_noop
6476 #define simplify_bsr simplify_noop
6479 [OP_SMUL ] = simplify_smul,
6480 [OP_UMUL ] = simplify_umul,
6481 [OP_SDIV ] = simplify_sdiv,
6482 [OP_UDIV ] = simplify_udiv,
6483 [OP_SMOD ] = simplify_smod,
6484 [OP_UMOD ] = simplify_umod,
6485 [OP_ADD ] = simplify_add,
6486 [OP_SUB ] = simplify_sub,
6487 [OP_SL ] = simplify_sl,
6488 [OP_USR ] = simplify_usr,
6489 [OP_SSR ] = simplify_ssr,
6490 [OP_AND ] = simplify_and,
6491 [OP_XOR ] = simplify_xor,
6492 [OP_OR ] = simplify_or,
6493 [OP_POS ] = simplify_pos,
6494 [OP_NEG ] = simplify_neg,
6495 [OP_INVERT ] = simplify_invert,
6497 [OP_EQ ] = simplify_eq,
6498 [OP_NOTEQ ] = simplify_noteq,
6499 [OP_SLESS ] = simplify_sless,
6500 [OP_ULESS ] = simplify_uless,
6501 [OP_SMORE ] = simplify_smore,
6502 [OP_UMORE ] = simplify_umore,
6503 [OP_SLESSEQ ] = simplify_slesseq,
6504 [OP_ULESSEQ ] = simplify_ulesseq,
6505 [OP_SMOREEQ ] = simplify_smoreeq,
6506 [OP_UMOREEQ ] = simplify_umoreeq,
6507 [OP_LFALSE ] = simplify_lfalse,
6508 [OP_LTRUE ] = simplify_ltrue,
6510 [OP_LOAD ] = simplify_noop,
6511 [OP_STORE ] = simplify_noop,
6513 [OP_NOOP ] = simplify_noop,
6515 [OP_INTCONST ] = simplify_noop,
6516 [OP_BLOBCONST ] = simplify_noop,
6517 [OP_ADDRCONST ] = simplify_noop,
6519 [OP_WRITE ] = simplify_noop,
6520 [OP_READ ] = simplify_noop,
6521 [OP_COPY ] = simplify_copy,
6522 [OP_PIECE ] = simplify_noop,
6523 [OP_ASM ] = simplify_noop,
6525 [OP_DOT ] = simplify_noop,
6526 [OP_VAL_VEC ] = simplify_noop,
6528 [OP_LIST ] = simplify_noop,
6529 [OP_BRANCH ] = simplify_branch,
6530 [OP_LABEL ] = simplify_noop,
6531 [OP_ADECL ] = simplify_noop,
6532 [OP_SDECL ] = simplify_noop,
6533 [OP_PHI ] = simplify_phi,
6535 [OP_INB ] = simplify_noop,
6536 [OP_INW ] = simplify_noop,
6537 [OP_INL ] = simplify_noop,
6538 [OP_OUTB ] = simplify_noop,
6539 [OP_OUTW ] = simplify_noop,
6540 [OP_OUTL ] = simplify_noop,
6541 [OP_BSF ] = simplify_bsf,
6542 [OP_BSR ] = simplify_bsr,
6543 [OP_RDMSR ] = simplify_noop,
6544 [OP_WRMSR ] = simplify_noop,
6545 [OP_HLT ] = simplify_noop,
6548 static void simplify(struct compile_state *state, struct triple *ins)
6551 simplify_t do_simplify;
6555 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
6559 do_simplify = table_simplify[op];
6562 internal_error(state, ins, "cannot simplify op: %d %s\n",
6566 do_simplify(state, ins);
6567 } while(ins->op != op);
6570 static void simplify_all(struct compile_state *state)
6572 struct triple *ins, *first;
6573 first = RHS(state->main_function, 0);
6576 simplify(state, ins);
6578 } while(ins != first);
6583 * ============================
6586 static void register_builtin_function(struct compile_state *state,
6587 const char *name, int op, struct type *rtype, ...)
6589 struct type *ftype, *atype, *param, **next;
6590 struct triple *def, *arg, *result, *work, *last, *first;
6591 struct hash_entry *ident;
6592 struct file_state file;
6598 /* Dummy file state to get debug handling right */
6599 memset(&file, 0, sizeof(file));
6600 file.basename = "<built-in>";
6602 file.report_line = 1;
6603 file.report_name = file.basename;
6604 file.prev = state->file;
6605 state->file = &file;
6606 state->function = name;
6608 /* Find the Parameter count */
6609 valid_op(state, op);
6610 parameters = table_ops[op].rhs;
6611 if (parameters < 0 ) {
6612 internal_error(state, 0, "Invalid builtin parameter count");
6615 /* Find the function type */
6616 ftype = new_type(TYPE_FUNCTION, rtype, 0);
6617 next = &ftype->right;
6618 va_start(args, rtype);
6619 for(i = 0; i < parameters; i++) {
6620 atype = va_arg(args, struct type *);
6624 *next = new_type(TYPE_PRODUCT, *next, atype);
6625 next = &((*next)->right);
6633 /* Generate the needed triples */
6634 def = triple(state, OP_LIST, ftype, 0, 0);
6635 first = label(state);
6636 RHS(def, 0) = first;
6638 /* Now string them together */
6639 param = ftype->right;
6640 for(i = 0; i < parameters; i++) {
6641 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6642 atype = param->left;
6646 arg = flatten(state, first, variable(state, atype));
6647 param = param->right;
6650 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
6651 result = flatten(state, first, variable(state, rtype));
6653 MISC(def, 0) = result;
6654 work = new_triple(state, op, rtype, -1, parameters);
6655 for(i = 0, arg = first->next; i < parameters; i++, arg = arg->next) {
6656 RHS(work, i) = read_expr(state, arg);
6658 if (result && ((rtype->type & TYPE_MASK) == TYPE_STRUCT)) {
6660 /* Populate the LHS with the target registers */
6661 work = flatten(state, first, work);
6662 work->type = &void_type;
6663 param = rtype->left;
6664 if (rtype->elements != TRIPLE_LHS(work->sizes)) {
6665 internal_error(state, 0, "Invalid result type");
6667 val = new_triple(state, OP_VAL_VEC, rtype, -1, -1);
6668 for(i = 0; i < rtype->elements; i++) {
6669 struct triple *piece;
6671 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6672 atype = param->left;
6674 if (!TYPE_ARITHMETIC(atype->type) &&
6675 !TYPE_PTR(atype->type)) {
6676 internal_error(state, 0, "Invalid lhs type");
6678 piece = triple(state, OP_PIECE, atype, work, 0);
6680 LHS(work, i) = piece;
6681 RHS(val, i) = piece;
6686 work = write_expr(state, result, work);
6688 work = flatten(state, first, work);
6689 last = flatten(state, first, label(state));
6690 name_len = strlen(name);
6691 ident = lookup(state, name, name_len);
6692 symbol(state, ident, &ident->sym_ident, def, ftype);
6694 state->file = file.prev;
6695 state->function = 0;
6697 fprintf(stdout, "\n");
6698 loc(stdout, state, 0);
6699 fprintf(stdout, "\n__________ builtin_function _________\n");
6700 print_triple(state, def);
6701 fprintf(stdout, "__________ builtin_function _________ done\n\n");
6705 static struct type *partial_struct(struct compile_state *state,
6706 const char *field_name, struct type *type, struct type *rest)
6708 struct hash_entry *field_ident;
6709 struct type *result;
6712 field_name_len = strlen(field_name);
6713 field_ident = lookup(state, field_name, field_name_len);
6715 result = clone_type(0, type);
6716 result->field_ident = field_ident;
6719 result = new_type(TYPE_PRODUCT, result, rest);
6724 static struct type *register_builtin_type(struct compile_state *state,
6725 const char *name, struct type *type)
6727 struct hash_entry *ident;
6730 name_len = strlen(name);
6731 ident = lookup(state, name, name_len);
6733 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6734 ulong_t elements = 0;
6736 type = new_type(TYPE_STRUCT, type, 0);
6738 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
6740 field = field->right;
6743 symbol(state, ident, &ident->sym_struct, 0, type);
6744 type->type_ident = ident;
6745 type->elements = elements;
6747 symbol(state, ident, &ident->sym_ident, 0, type);
6748 ident->tok = TOK_TYPE_NAME;
6753 static void register_builtins(struct compile_state *state)
6755 struct type *msr_type;
6757 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
6759 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
6761 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
6764 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
6765 &uchar_type, &ushort_type);
6766 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
6767 &ushort_type, &ushort_type);
6768 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
6769 &uint_type, &ushort_type);
6771 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
6773 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
6776 msr_type = register_builtin_type(state, "__builtin_msr_t",
6777 partial_struct(state, "lo", &ulong_type,
6778 partial_struct(state, "hi", &ulong_type, 0)));
6780 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
6782 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
6783 &ulong_type, &ulong_type, &ulong_type);
6785 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
6789 static struct type *declarator(
6790 struct compile_state *state, struct type *type,
6791 struct hash_entry **ident, int need_ident);
6792 static void decl(struct compile_state *state, struct triple *first);
6793 static struct type *specifier_qualifier_list(struct compile_state *state);
6794 static int isdecl_specifier(int tok);
6795 static struct type *decl_specifiers(struct compile_state *state);
6796 static int istype(int tok);
6797 static struct triple *expr(struct compile_state *state);
6798 static struct triple *assignment_expr(struct compile_state *state);
6799 static struct type *type_name(struct compile_state *state);
6800 static void statement(struct compile_state *state, struct triple *fist);
6802 static struct triple *call_expr(
6803 struct compile_state *state, struct triple *func)
6806 struct type *param, *type;
6807 ulong_t pvals, index;
6809 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
6810 error(state, 0, "Called object is not a function");
6812 if (func->op != OP_LIST) {
6813 internal_error(state, 0, "improper function");
6815 eat(state, TOK_LPAREN);
6816 /* Find the return type without any specifiers */
6817 type = clone_type(0, func->type->left);
6818 def = new_triple(state, OP_CALL, func->type, -1, -1);
6821 pvals = TRIPLE_RHS(def->sizes);
6822 MISC(def, 0) = func;
6824 param = func->type->right;
6825 for(index = 0; index < pvals; index++) {
6827 struct type *arg_type;
6828 val = read_expr(state, assignment_expr(state));
6830 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6831 arg_type = param->left;
6833 write_compatible(state, arg_type, val->type);
6834 RHS(def, index) = val;
6835 if (index != (pvals - 1)) {
6836 eat(state, TOK_COMMA);
6837 param = param->right;
6840 eat(state, TOK_RPAREN);
6845 static struct triple *character_constant(struct compile_state *state)
6849 const signed char *str, *end;
6852 eat(state, TOK_LIT_CHAR);
6853 tk = &state->token[0];
6854 str = tk->val.str + 1;
6855 str_len = tk->str_len - 2;
6857 error(state, 0, "empty character constant");
6859 end = str + str_len;
6860 c = char_value(state, &str, end);
6862 error(state, 0, "multibyte character constant not supported");
6864 def = int_const(state, &char_type, (ulong_t)((long_t)c));
6868 static struct triple *string_constant(struct compile_state *state)
6873 const signed char *str, *end;
6874 signed char *buf, *ptr;
6878 type = new_type(TYPE_ARRAY, &char_type, 0);
6880 /* The while loop handles string concatenation */
6882 eat(state, TOK_LIT_STRING);
6883 tk = &state->token[0];
6884 str = tk->val.str + 1;
6885 str_len = tk->str_len - 2;
6887 error(state, 0, "negative string constant length");
6889 end = str + str_len;
6891 buf = xmalloc(type->elements + str_len + 1, "string_constant");
6892 memcpy(buf, ptr, type->elements);
6893 ptr = buf + type->elements;
6895 *ptr++ = char_value(state, &str, end);
6897 type->elements = ptr - buf;
6898 } while(peek(state) == TOK_LIT_STRING);
6900 type->elements += 1;
6901 def = triple(state, OP_BLOBCONST, type, 0, 0);
6907 static struct triple *integer_constant(struct compile_state *state)
6916 eat(state, TOK_LIT_INT);
6917 tk = &state->token[0];
6919 decimal = (tk->val.str[0] != '0');
6920 val = strtoul(tk->val.str, &end, 0);
6921 if ((val == ULONG_MAX) && (errno == ERANGE)) {
6922 error(state, 0, "Integer constant to large");
6925 if ((*end == 'u') || (*end == 'U')) {
6929 if ((*end == 'l') || (*end == 'L')) {
6933 if ((*end == 'u') || (*end == 'U')) {
6938 error(state, 0, "Junk at end of integer constant");
6945 if (!decimal && (val > LONG_MAX)) {
6951 if (val > UINT_MAX) {
6957 if (!decimal && (val > INT_MAX) && (val <= UINT_MAX)) {
6960 else if (!decimal && (val > LONG_MAX)) {
6963 else if (val > INT_MAX) {
6967 def = int_const(state, type, val);
6971 static struct triple *primary_expr(struct compile_state *state)
6979 struct hash_entry *ident;
6980 /* Here ident is either:
6983 * an enumeration constant.
6985 eat(state, TOK_IDENT);
6986 ident = state->token[0].ident;
6987 if (!ident->sym_ident) {
6988 error(state, 0, "%s undeclared", ident->name);
6990 def = ident->sym_ident->def;
6993 case TOK_ENUM_CONST:
6994 /* Here ident is an enumeration constant */
6995 eat(state, TOK_ENUM_CONST);
7000 eat(state, TOK_LPAREN);
7002 eat(state, TOK_RPAREN);
7005 def = integer_constant(state);
7008 eat(state, TOK_LIT_FLOAT);
7009 error(state, 0, "Floating point constants not supported");
7014 def = character_constant(state);
7016 case TOK_LIT_STRING:
7017 def = string_constant(state);
7021 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
7026 static struct triple *postfix_expr(struct compile_state *state)
7030 def = primary_expr(state);
7032 struct triple *left;
7036 switch((tok = peek(state))) {
7038 eat(state, TOK_LBRACKET);
7039 def = mk_subscript_expr(state, left, expr(state));
7040 eat(state, TOK_RBRACKET);
7043 def = call_expr(state, def);
7047 struct hash_entry *field;
7048 eat(state, TOK_DOT);
7049 eat(state, TOK_IDENT);
7050 field = state->token[0].ident;
7051 def = deref_field(state, def, field);
7056 struct hash_entry *field;
7057 eat(state, TOK_ARROW);
7058 eat(state, TOK_IDENT);
7059 field = state->token[0].ident;
7060 def = mk_deref_expr(state, read_expr(state, def));
7061 def = deref_field(state, def, field);
7065 eat(state, TOK_PLUSPLUS);
7066 def = mk_post_inc_expr(state, left);
7068 case TOK_MINUSMINUS:
7069 eat(state, TOK_MINUSMINUS);
7070 def = mk_post_dec_expr(state, left);
7080 static struct triple *cast_expr(struct compile_state *state);
7082 static struct triple *unary_expr(struct compile_state *state)
7084 struct triple *def, *right;
7086 switch((tok = peek(state))) {
7088 eat(state, TOK_PLUSPLUS);
7089 def = mk_pre_inc_expr(state, unary_expr(state));
7091 case TOK_MINUSMINUS:
7092 eat(state, TOK_MINUSMINUS);
7093 def = mk_pre_dec_expr(state, unary_expr(state));
7096 eat(state, TOK_AND);
7097 def = mk_addr_expr(state, cast_expr(state), 0);
7100 eat(state, TOK_STAR);
7101 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
7104 eat(state, TOK_PLUS);
7105 right = read_expr(state, cast_expr(state));
7106 arithmetic(state, right);
7107 def = integral_promotion(state, right);
7110 eat(state, TOK_MINUS);
7111 right = read_expr(state, cast_expr(state));
7112 arithmetic(state, right);
7113 def = integral_promotion(state, right);
7114 def = triple(state, OP_NEG, def->type, def, 0);
7117 eat(state, TOK_TILDE);
7118 right = read_expr(state, cast_expr(state));
7119 integral(state, right);
7120 def = integral_promotion(state, right);
7121 def = triple(state, OP_INVERT, def->type, def, 0);
7124 eat(state, TOK_BANG);
7125 right = read_expr(state, cast_expr(state));
7127 def = lfalse_expr(state, right);
7133 eat(state, TOK_SIZEOF);
7135 tok2 = peek2(state);
7136 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7137 eat(state, TOK_LPAREN);
7138 type = type_name(state);
7139 eat(state, TOK_RPAREN);
7142 struct triple *expr;
7143 expr = unary_expr(state);
7145 release_expr(state, expr);
7147 def = int_const(state, &ulong_type, size_of(state, type));
7154 eat(state, TOK_ALIGNOF);
7156 tok2 = peek2(state);
7157 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7158 eat(state, TOK_LPAREN);
7159 type = type_name(state);
7160 eat(state, TOK_RPAREN);
7163 struct triple *expr;
7164 expr = unary_expr(state);
7166 release_expr(state, expr);
7168 def = int_const(state, &ulong_type, align_of(state, type));
7172 def = postfix_expr(state);
7178 static struct triple *cast_expr(struct compile_state *state)
7183 tok2 = peek2(state);
7184 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7186 eat(state, TOK_LPAREN);
7187 type = type_name(state);
7188 eat(state, TOK_RPAREN);
7189 def = read_expr(state, cast_expr(state));
7190 def = triple(state, OP_COPY, type, def, 0);
7193 def = unary_expr(state);
7198 static struct triple *mult_expr(struct compile_state *state)
7202 def = cast_expr(state);
7204 struct triple *left, *right;
7205 struct type *result_type;
7208 switch(tok = (peek(state))) {
7212 left = read_expr(state, def);
7213 arithmetic(state, left);
7217 right = read_expr(state, cast_expr(state));
7218 arithmetic(state, right);
7220 result_type = arithmetic_result(state, left, right);
7221 sign = is_signed(result_type);
7224 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
7225 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
7226 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
7228 def = triple(state, op, result_type, left, right);
7238 static struct triple *add_expr(struct compile_state *state)
7242 def = mult_expr(state);
7245 switch( peek(state)) {
7247 eat(state, TOK_PLUS);
7248 def = mk_add_expr(state, def, mult_expr(state));
7251 eat(state, TOK_MINUS);
7252 def = mk_sub_expr(state, def, mult_expr(state));
7262 static struct triple *shift_expr(struct compile_state *state)
7266 def = add_expr(state);
7268 struct triple *left, *right;
7271 switch((tok = peek(state))) {
7274 left = read_expr(state, def);
7275 integral(state, left);
7276 left = integral_promotion(state, left);
7280 right = read_expr(state, add_expr(state));
7281 integral(state, right);
7282 right = integral_promotion(state, right);
7284 op = (tok == TOK_SL)? OP_SL :
7285 is_signed(left->type)? OP_SSR: OP_USR;
7287 def = triple(state, op, left->type, left, right);
7297 static struct triple *relational_expr(struct compile_state *state)
7299 #warning "Extend relational exprs to work on more than arithmetic types"
7302 def = shift_expr(state);
7304 struct triple *left, *right;
7305 struct type *arg_type;
7308 switch((tok = peek(state))) {
7313 left = read_expr(state, def);
7314 arithmetic(state, left);
7318 right = read_expr(state, shift_expr(state));
7319 arithmetic(state, right);
7321 arg_type = arithmetic_result(state, left, right);
7322 sign = is_signed(arg_type);
7325 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
7326 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
7327 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
7328 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
7330 def = triple(state, op, &int_type, left, right);
7340 static struct triple *equality_expr(struct compile_state *state)
7342 #warning "Extend equality exprs to work on more than arithmetic types"
7345 def = relational_expr(state);
7347 struct triple *left, *right;
7350 switch((tok = peek(state))) {
7353 left = read_expr(state, def);
7354 arithmetic(state, left);
7356 right = read_expr(state, relational_expr(state));
7357 arithmetic(state, right);
7358 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
7359 def = triple(state, op, &int_type, left, right);
7369 static struct triple *and_expr(struct compile_state *state)
7372 def = equality_expr(state);
7373 while(peek(state) == TOK_AND) {
7374 struct triple *left, *right;
7375 struct type *result_type;
7376 left = read_expr(state, def);
7377 integral(state, left);
7378 eat(state, TOK_AND);
7379 right = read_expr(state, equality_expr(state));
7380 integral(state, right);
7381 result_type = arithmetic_result(state, left, right);
7382 def = triple(state, OP_AND, result_type, left, right);
7387 static struct triple *xor_expr(struct compile_state *state)
7390 def = and_expr(state);
7391 while(peek(state) == TOK_XOR) {
7392 struct triple *left, *right;
7393 struct type *result_type;
7394 left = read_expr(state, def);
7395 integral(state, left);
7396 eat(state, TOK_XOR);
7397 right = read_expr(state, and_expr(state));
7398 integral(state, right);
7399 result_type = arithmetic_result(state, left, right);
7400 def = triple(state, OP_XOR, result_type, left, right);
7405 static struct triple *or_expr(struct compile_state *state)
7408 def = xor_expr(state);
7409 while(peek(state) == TOK_OR) {
7410 struct triple *left, *right;
7411 struct type *result_type;
7412 left = read_expr(state, def);
7413 integral(state, left);
7415 right = read_expr(state, xor_expr(state));
7416 integral(state, right);
7417 result_type = arithmetic_result(state, left, right);
7418 def = triple(state, OP_OR, result_type, left, right);
7423 static struct triple *land_expr(struct compile_state *state)
7426 def = or_expr(state);
7427 while(peek(state) == TOK_LOGAND) {
7428 struct triple *left, *right;
7429 left = read_expr(state, def);
7431 eat(state, TOK_LOGAND);
7432 right = read_expr(state, or_expr(state));
7435 def = triple(state, OP_LAND, &int_type,
7436 ltrue_expr(state, left),
7437 ltrue_expr(state, right));
7442 static struct triple *lor_expr(struct compile_state *state)
7445 def = land_expr(state);
7446 while(peek(state) == TOK_LOGOR) {
7447 struct triple *left, *right;
7448 left = read_expr(state, def);
7450 eat(state, TOK_LOGOR);
7451 right = read_expr(state, land_expr(state));
7454 def = triple(state, OP_LOR, &int_type,
7455 ltrue_expr(state, left),
7456 ltrue_expr(state, right));
7461 static struct triple *conditional_expr(struct compile_state *state)
7464 def = lor_expr(state);
7465 if (peek(state) == TOK_QUEST) {
7466 struct triple *test, *left, *right;
7468 test = ltrue_expr(state, read_expr(state, def));
7469 eat(state, TOK_QUEST);
7470 left = read_expr(state, expr(state));
7471 eat(state, TOK_COLON);
7472 right = read_expr(state, conditional_expr(state));
7474 def = cond_expr(state, test, left, right);
7479 static struct triple *eval_const_expr(
7480 struct compile_state *state, struct triple *expr)
7483 if (is_const(expr)) {
7487 /* If we don't start out as a constant simplify into one */
7488 struct triple *head, *ptr;
7489 head = label(state); /* dummy initial triple */
7490 flatten(state, head, expr);
7491 for(ptr = head->next; ptr != head; ptr = ptr->next) {
7492 simplify(state, ptr);
7494 /* Remove the constant value the tail of the list */
7496 def->prev->next = def->next;
7497 def->next->prev = def->prev;
7498 def->next = def->prev = def;
7499 if (!is_const(def)) {
7500 error(state, 0, "Not a constant expression");
7502 /* Free the intermediate expressions */
7503 while(head->next != head) {
7504 release_triple(state, head->next);
7506 free_triple(state, head);
7511 static struct triple *constant_expr(struct compile_state *state)
7513 return eval_const_expr(state, conditional_expr(state));
7516 static struct triple *assignment_expr(struct compile_state *state)
7518 struct triple *def, *left, *right;
7520 /* The C grammer in K&R shows assignment expressions
7521 * only taking unary expressions as input on their
7522 * left hand side. But specifies the precedence of
7523 * assignemnt as the lowest operator except for comma.
7525 * Allowing conditional expressions on the left hand side
7526 * of an assignement results in a grammar that accepts
7527 * a larger set of statements than standard C. As long
7528 * as the subset of the grammar that is standard C behaves
7529 * correctly this should cause no problems.
7531 * For the extra token strings accepted by the grammar
7532 * none of them should produce a valid lvalue, so they
7533 * should not produce functioning programs.
7535 * GCC has this bug as well, so surprises should be minimal.
7537 def = conditional_expr(state);
7539 switch((tok = peek(state))) {
7541 lvalue(state, left);
7543 def = write_expr(state, left,
7544 read_expr(state, assignment_expr(state)));
7549 lvalue(state, left);
7550 arithmetic(state, left);
7552 right = read_expr(state, assignment_expr(state));
7553 arithmetic(state, right);
7555 sign = is_signed(left->type);
7558 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
7559 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
7560 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
7562 def = write_expr(state, left,
7563 triple(state, op, left->type,
7564 read_expr(state, left), right));
7567 lvalue(state, left);
7568 eat(state, TOK_PLUSEQ);
7569 def = write_expr(state, left,
7570 mk_add_expr(state, left, assignment_expr(state)));
7573 lvalue(state, left);
7574 eat(state, TOK_MINUSEQ);
7575 def = write_expr(state, left,
7576 mk_sub_expr(state, left, assignment_expr(state)));
7583 lvalue(state, left);
7584 integral(state, left);
7586 right = read_expr(state, assignment_expr(state));
7587 integral(state, right);
7588 right = integral_promotion(state, right);
7589 sign = is_signed(left->type);
7592 case TOK_SLEQ: op = OP_SL; break;
7593 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
7594 case TOK_ANDEQ: op = OP_AND; break;
7595 case TOK_XOREQ: op = OP_XOR; break;
7596 case TOK_OREQ: op = OP_OR; break;
7598 def = write_expr(state, left,
7599 triple(state, op, left->type,
7600 read_expr(state, left), right));
7606 static struct triple *expr(struct compile_state *state)
7609 def = assignment_expr(state);
7610 while(peek(state) == TOK_COMMA) {
7611 struct triple *left, *right;
7613 eat(state, TOK_COMMA);
7614 right = assignment_expr(state);
7615 def = triple(state, OP_COMMA, right->type, left, right);
7620 static void expr_statement(struct compile_state *state, struct triple *first)
7622 if (peek(state) != TOK_SEMI) {
7623 flatten(state, first, expr(state));
7625 eat(state, TOK_SEMI);
7628 static void if_statement(struct compile_state *state, struct triple *first)
7630 struct triple *test, *jmp1, *jmp2, *middle, *end;
7632 jmp1 = jmp2 = middle = 0;
7634 eat(state, TOK_LPAREN);
7637 /* Cleanup and invert the test */
7638 test = lfalse_expr(state, read_expr(state, test));
7639 eat(state, TOK_RPAREN);
7640 /* Generate the needed pieces */
7641 middle = label(state);
7642 jmp1 = branch(state, middle, test);
7643 /* Thread the pieces together */
7644 flatten(state, first, test);
7645 flatten(state, first, jmp1);
7646 flatten(state, first, label(state));
7647 statement(state, first);
7648 if (peek(state) == TOK_ELSE) {
7649 eat(state, TOK_ELSE);
7650 /* Generate the rest of the pieces */
7652 jmp2 = branch(state, end, 0);
7653 /* Thread them together */
7654 flatten(state, first, jmp2);
7655 flatten(state, first, middle);
7656 statement(state, first);
7657 flatten(state, first, end);
7660 flatten(state, first, middle);
7664 static void for_statement(struct compile_state *state, struct triple *first)
7666 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
7667 struct triple *label1, *label2, *label3;
7668 struct hash_entry *ident;
7670 eat(state, TOK_FOR);
7671 eat(state, TOK_LPAREN);
7672 head = test = tail = jmp1 = jmp2 = 0;
7673 if (peek(state) != TOK_SEMI) {
7676 eat(state, TOK_SEMI);
7677 if (peek(state) != TOK_SEMI) {
7680 test = ltrue_expr(state, read_expr(state, test));
7682 eat(state, TOK_SEMI);
7683 if (peek(state) != TOK_RPAREN) {
7686 eat(state, TOK_RPAREN);
7687 /* Generate the needed pieces */
7688 label1 = label(state);
7689 label2 = label(state);
7690 label3 = label(state);
7692 jmp1 = branch(state, label3, 0);
7693 jmp2 = branch(state, label1, test);
7696 jmp2 = branch(state, label1, 0);
7699 /* Remember where break and continue go */
7701 ident = state->i_break;
7702 symbol(state, ident, &ident->sym_ident, end, end->type);
7703 ident = state->i_continue;
7704 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7705 /* Now include the body */
7706 flatten(state, first, head);
7707 flatten(state, first, jmp1);
7708 flatten(state, first, label1);
7709 statement(state, first);
7710 flatten(state, first, label2);
7711 flatten(state, first, tail);
7712 flatten(state, first, label3);
7713 flatten(state, first, test);
7714 flatten(state, first, jmp2);
7715 flatten(state, first, end);
7716 /* Cleanup the break/continue scope */
7720 static void while_statement(struct compile_state *state, struct triple *first)
7722 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
7723 struct hash_entry *ident;
7724 eat(state, TOK_WHILE);
7725 eat(state, TOK_LPAREN);
7728 test = ltrue_expr(state, read_expr(state, test));
7729 eat(state, TOK_RPAREN);
7730 /* Generate the needed pieces */
7731 label1 = label(state);
7732 label2 = label(state);
7733 jmp1 = branch(state, label2, 0);
7734 jmp2 = branch(state, label1, test);
7736 /* Remember where break and continue go */
7738 ident = state->i_break;
7739 symbol(state, ident, &ident->sym_ident, end, end->type);
7740 ident = state->i_continue;
7741 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7742 /* Thread them together */
7743 flatten(state, first, jmp1);
7744 flatten(state, first, label1);
7745 statement(state, first);
7746 flatten(state, first, label2);
7747 flatten(state, first, test);
7748 flatten(state, first, jmp2);
7749 flatten(state, first, end);
7750 /* Cleanup the break/continue scope */
7754 static void do_statement(struct compile_state *state, struct triple *first)
7756 struct triple *label1, *label2, *test, *end;
7757 struct hash_entry *ident;
7759 /* Generate the needed pieces */
7760 label1 = label(state);
7761 label2 = label(state);
7763 /* Remember where break and continue go */
7765 ident = state->i_break;
7766 symbol(state, ident, &ident->sym_ident, end, end->type);
7767 ident = state->i_continue;
7768 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7769 /* Now include the body */
7770 flatten(state, first, label1);
7771 statement(state, first);
7772 /* Cleanup the break/continue scope */
7774 /* Eat the rest of the loop */
7775 eat(state, TOK_WHILE);
7776 eat(state, TOK_LPAREN);
7777 test = read_expr(state, expr(state));
7779 eat(state, TOK_RPAREN);
7780 eat(state, TOK_SEMI);
7781 /* Thread the pieces together */
7782 test = ltrue_expr(state, test);
7783 flatten(state, first, label2);
7784 flatten(state, first, test);
7785 flatten(state, first, branch(state, label1, test));
7786 flatten(state, first, end);
7790 static void return_statement(struct compile_state *state, struct triple *first)
7792 struct triple *jmp, *mv, *dest, *var, *val;
7794 eat(state, TOK_RETURN);
7796 #warning "FIXME implement a more general excess branch elimination"
7798 /* If we have a return value do some more work */
7799 if (peek(state) != TOK_SEMI) {
7800 val = read_expr(state, expr(state));
7802 eat(state, TOK_SEMI);
7804 /* See if this last statement in a function */
7805 last = ((peek(state) == TOK_RBRACE) &&
7806 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
7808 /* Find the return variable */
7809 var = MISC(state->main_function, 0);
7810 /* Find the return destination */
7811 dest = RHS(state->main_function, 0)->prev;
7813 /* If needed generate a jump instruction */
7815 jmp = branch(state, dest, 0);
7817 /* If needed generate an assignment instruction */
7819 mv = write_expr(state, var, val);
7821 /* Now put the code together */
7823 flatten(state, first, mv);
7824 flatten(state, first, jmp);
7827 flatten(state, first, jmp);
7831 static void break_statement(struct compile_state *state, struct triple *first)
7833 struct triple *dest;
7834 eat(state, TOK_BREAK);
7835 eat(state, TOK_SEMI);
7836 if (!state->i_break->sym_ident) {
7837 error(state, 0, "break statement not within loop or switch");
7839 dest = state->i_break->sym_ident->def;
7840 flatten(state, first, branch(state, dest, 0));
7843 static void continue_statement(struct compile_state *state, struct triple *first)
7845 struct triple *dest;
7846 eat(state, TOK_CONTINUE);
7847 eat(state, TOK_SEMI);
7848 if (!state->i_continue->sym_ident) {
7849 error(state, 0, "continue statement outside of a loop");
7851 dest = state->i_continue->sym_ident->def;
7852 flatten(state, first, branch(state, dest, 0));
7855 static void goto_statement(struct compile_state *state, struct triple *first)
7857 struct hash_entry *ident;
7858 eat(state, TOK_GOTO);
7859 eat(state, TOK_IDENT);
7860 ident = state->token[0].ident;
7861 if (!ident->sym_label) {
7862 /* If this is a forward branch allocate the label now,
7863 * it will be flattend in the appropriate location later.
7867 label_symbol(state, ident, ins);
7869 eat(state, TOK_SEMI);
7871 flatten(state, first, branch(state, ident->sym_label->def, 0));
7874 static void labeled_statement(struct compile_state *state, struct triple *first)
7877 struct hash_entry *ident;
7878 eat(state, TOK_IDENT);
7880 ident = state->token[0].ident;
7881 if (ident->sym_label && ident->sym_label->def) {
7882 ins = ident->sym_label->def;
7883 put_occurance(ins->occurance);
7884 ins->occurance = new_occurance(state);
7888 label_symbol(state, ident, ins);
7890 if (ins->id & TRIPLE_FLAG_FLATTENED) {
7891 error(state, 0, "label %s already defined", ident->name);
7893 flatten(state, first, ins);
7895 eat(state, TOK_COLON);
7896 statement(state, first);
7899 static void switch_statement(struct compile_state *state, struct triple *first)
7902 eat(state, TOK_SWITCH);
7903 eat(state, TOK_LPAREN);
7905 eat(state, TOK_RPAREN);
7906 statement(state, first);
7907 error(state, 0, "switch statements are not implemented");
7911 static void case_statement(struct compile_state *state, struct triple *first)
7914 eat(state, TOK_CASE);
7915 constant_expr(state);
7916 eat(state, TOK_COLON);
7917 statement(state, first);
7918 error(state, 0, "case statements are not implemented");
7922 static void default_statement(struct compile_state *state, struct triple *first)
7925 eat(state, TOK_DEFAULT);
7926 eat(state, TOK_COLON);
7927 statement(state, first);
7928 error(state, 0, "default statements are not implemented");
7932 static void asm_statement(struct compile_state *state, struct triple *first)
7934 struct asm_info *info;
7936 struct triple *constraint;
7937 struct triple *expr;
7938 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
7939 struct triple *def, *asm_str;
7940 int out, in, clobbers, more, colons, i;
7942 eat(state, TOK_ASM);
7943 /* For now ignore the qualifiers */
7944 switch(peek(state)) {
7946 eat(state, TOK_CONST);
7949 eat(state, TOK_VOLATILE);
7952 eat(state, TOK_LPAREN);
7953 asm_str = string_constant(state);
7956 out = in = clobbers = 0;
7958 if ((colons == 0) && (peek(state) == TOK_COLON)) {
7959 eat(state, TOK_COLON);
7961 more = (peek(state) == TOK_LIT_STRING);
7964 struct triple *constraint;
7967 if (out > MAX_LHS) {
7968 error(state, 0, "Maximum output count exceeded.");
7970 constraint = string_constant(state);
7971 str = constraint->u.blob;
7972 if (str[0] != '=') {
7973 error(state, 0, "Output constraint does not start with =");
7975 constraint->u.blob = str + 1;
7976 eat(state, TOK_LPAREN);
7977 var = conditional_expr(state);
7978 eat(state, TOK_RPAREN);
7981 out_param[out].constraint = constraint;
7982 out_param[out].expr = var;
7983 if (peek(state) == TOK_COMMA) {
7984 eat(state, TOK_COMMA);
7991 if ((colons == 1) && (peek(state) == TOK_COLON)) {
7992 eat(state, TOK_COLON);
7994 more = (peek(state) == TOK_LIT_STRING);
7997 struct triple *constraint;
8001 error(state, 0, "Maximum input count exceeded.");
8003 constraint = string_constant(state);
8004 str = constraint->u.blob;
8005 if (digitp(str[0] && str[1] == '\0')) {
8007 val = digval(str[0]);
8008 if ((val < 0) || (val >= out)) {
8009 error(state, 0, "Invalid input constraint %d", val);
8012 eat(state, TOK_LPAREN);
8013 val = conditional_expr(state);
8014 eat(state, TOK_RPAREN);
8016 in_param[in].constraint = constraint;
8017 in_param[in].expr = val;
8018 if (peek(state) == TOK_COMMA) {
8019 eat(state, TOK_COMMA);
8027 if ((colons == 2) && (peek(state) == TOK_COLON)) {
8028 eat(state, TOK_COLON);
8030 more = (peek(state) == TOK_LIT_STRING);
8032 struct triple *clobber;
8034 if ((clobbers + out) > MAX_LHS) {
8035 error(state, 0, "Maximum clobber limit exceeded.");
8037 clobber = string_constant(state);
8038 eat(state, TOK_RPAREN);
8040 clob_param[clobbers].constraint = clobber;
8041 if (peek(state) == TOK_COMMA) {
8042 eat(state, TOK_COMMA);
8048 eat(state, TOK_RPAREN);
8049 eat(state, TOK_SEMI);
8052 info = xcmalloc(sizeof(*info), "asm_info");
8053 info->str = asm_str->u.blob;
8054 free_triple(state, asm_str);
8056 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
8057 def->u.ainfo = info;
8059 /* Find the register constraints */
8060 for(i = 0; i < out; i++) {
8061 struct triple *constraint;
8062 constraint = out_param[i].constraint;
8063 info->tmpl.lhs[i] = arch_reg_constraint(state,
8064 out_param[i].expr->type, constraint->u.blob);
8065 free_triple(state, constraint);
8067 for(; i - out < clobbers; i++) {
8068 struct triple *constraint;
8069 constraint = clob_param[i - out].constraint;
8070 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
8071 free_triple(state, constraint);
8073 for(i = 0; i < in; i++) {
8074 struct triple *constraint;
8076 constraint = in_param[i].constraint;
8077 str = constraint->u.blob;
8078 if (digitp(str[0]) && str[1] == '\0') {
8079 struct reg_info cinfo;
8081 val = digval(str[0]);
8082 cinfo.reg = info->tmpl.lhs[val].reg;
8083 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
8084 cinfo.regcm &= info->tmpl.lhs[val].regcm;
8085 if (cinfo.reg == REG_UNSET) {
8086 cinfo.reg = REG_VIRT0 + val;
8088 if (cinfo.regcm == 0) {
8089 error(state, 0, "No registers for %d", val);
8091 info->tmpl.lhs[val] = cinfo;
8092 info->tmpl.rhs[i] = cinfo;
8095 info->tmpl.rhs[i] = arch_reg_constraint(state,
8096 in_param[i].expr->type, str);
8098 free_triple(state, constraint);
8101 /* Now build the helper expressions */
8102 for(i = 0; i < in; i++) {
8103 RHS(def, i) = read_expr(state,in_param[i].expr);
8105 flatten(state, first, def);
8106 for(i = 0; i < out; i++) {
8107 struct triple *piece;
8108 piece = triple(state, OP_PIECE, out_param[i].expr->type, def, 0);
8110 LHS(def, i) = piece;
8111 flatten(state, first,
8112 write_expr(state, out_param[i].expr, piece));
8114 for(; i - out < clobbers; i++) {
8115 struct triple *piece;
8116 piece = triple(state, OP_PIECE, &void_type, def, 0);
8118 LHS(def, i) = piece;
8119 flatten(state, first, piece);
8124 static int isdecl(int tok)
8147 case TOK_TYPE_NAME: /* typedef name */
8154 static void compound_statement(struct compile_state *state, struct triple *first)
8156 eat(state, TOK_LBRACE);
8159 /* statement-list opt */
8160 while (peek(state) != TOK_RBRACE) {
8161 statement(state, first);
8164 eat(state, TOK_RBRACE);
8167 static void statement(struct compile_state *state, struct triple *first)
8171 if (tok == TOK_LBRACE) {
8172 compound_statement(state, first);
8174 else if (tok == TOK_IF) {
8175 if_statement(state, first);
8177 else if (tok == TOK_FOR) {
8178 for_statement(state, first);
8180 else if (tok == TOK_WHILE) {
8181 while_statement(state, first);
8183 else if (tok == TOK_DO) {
8184 do_statement(state, first);
8186 else if (tok == TOK_RETURN) {
8187 return_statement(state, first);
8189 else if (tok == TOK_BREAK) {
8190 break_statement(state, first);
8192 else if (tok == TOK_CONTINUE) {
8193 continue_statement(state, first);
8195 else if (tok == TOK_GOTO) {
8196 goto_statement(state, first);
8198 else if (tok == TOK_SWITCH) {
8199 switch_statement(state, first);
8201 else if (tok == TOK_ASM) {
8202 asm_statement(state, first);
8204 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
8205 labeled_statement(state, first);
8207 else if (tok == TOK_CASE) {
8208 case_statement(state, first);
8210 else if (tok == TOK_DEFAULT) {
8211 default_statement(state, first);
8213 else if (isdecl(tok)) {
8214 /* This handles C99 intermixing of statements and decls */
8218 expr_statement(state, first);
8222 static struct type *param_decl(struct compile_state *state)
8225 struct hash_entry *ident;
8226 /* Cheat so the declarator will know we are not global */
8229 type = decl_specifiers(state);
8230 type = declarator(state, type, &ident, 0);
8231 type->field_ident = ident;
8236 static struct type *param_type_list(struct compile_state *state, struct type *type)
8238 struct type *ftype, **next;
8239 ftype = new_type(TYPE_FUNCTION, type, param_decl(state));
8240 next = &ftype->right;
8241 while(peek(state) == TOK_COMMA) {
8242 eat(state, TOK_COMMA);
8243 if (peek(state) == TOK_DOTS) {
8244 eat(state, TOK_DOTS);
8245 error(state, 0, "variadic functions not supported");
8248 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
8249 next = &((*next)->right);
8256 static struct type *type_name(struct compile_state *state)
8259 type = specifier_qualifier_list(state);
8260 /* abstract-declarator (may consume no tokens) */
8261 type = declarator(state, type, 0, 0);
8265 static struct type *direct_declarator(
8266 struct compile_state *state, struct type *type,
8267 struct hash_entry **ident, int need_ident)
8272 arrays_complete(state, type);
8273 switch(peek(state)) {
8275 eat(state, TOK_IDENT);
8277 error(state, 0, "Unexpected identifier found");
8279 /* The name of what we are declaring */
8280 *ident = state->token[0].ident;
8283 eat(state, TOK_LPAREN);
8284 outer = declarator(state, type, ident, need_ident);
8285 eat(state, TOK_RPAREN);
8289 error(state, 0, "Identifier expected");
8295 arrays_complete(state, type);
8296 switch(peek(state)) {
8298 eat(state, TOK_LPAREN);
8299 type = param_type_list(state, type);
8300 eat(state, TOK_RPAREN);
8304 unsigned int qualifiers;
8305 struct triple *value;
8307 eat(state, TOK_LBRACKET);
8308 if (peek(state) != TOK_RBRACKET) {
8309 value = constant_expr(state);
8310 integral(state, value);
8312 eat(state, TOK_RBRACKET);
8314 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
8315 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
8317 type->elements = value->u.cval;
8318 free_triple(state, value);
8320 type->elements = ELEMENT_COUNT_UNSPECIFIED;
8332 arrays_complete(state, type);
8334 for(inner = outer; inner->left; inner = inner->left)
8342 static struct type *declarator(
8343 struct compile_state *state, struct type *type,
8344 struct hash_entry **ident, int need_ident)
8346 while(peek(state) == TOK_STAR) {
8347 eat(state, TOK_STAR);
8348 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
8350 type = direct_declarator(state, type, ident, need_ident);
8355 static struct type *typedef_name(
8356 struct compile_state *state, unsigned int specifiers)
8358 struct hash_entry *ident;
8360 eat(state, TOK_TYPE_NAME);
8361 ident = state->token[0].ident;
8362 type = ident->sym_ident->type;
8363 specifiers |= type->type & QUAL_MASK;
8364 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
8365 (type->type & (STOR_MASK | QUAL_MASK))) {
8366 type = clone_type(specifiers, type);
8371 static struct type *enum_specifier(
8372 struct compile_state *state, unsigned int specifiers)
8378 eat(state, TOK_ENUM);
8380 if (tok == TOK_IDENT) {
8381 eat(state, TOK_IDENT);
8383 if ((tok != TOK_IDENT) || (peek(state) == TOK_LBRACE)) {
8384 eat(state, TOK_LBRACE);
8386 eat(state, TOK_IDENT);
8387 if (peek(state) == TOK_EQ) {
8389 constant_expr(state);
8391 if (peek(state) == TOK_COMMA) {
8392 eat(state, TOK_COMMA);
8394 } while(peek(state) != TOK_RBRACE);
8395 eat(state, TOK_RBRACE);
8402 static struct type *struct_declarator(
8403 struct compile_state *state, struct type *type, struct hash_entry **ident)
8406 #warning "struct_declarator is complicated because of bitfields, kill them?"
8408 if (tok != TOK_COLON) {
8409 type = declarator(state, type, ident, 1);
8411 if ((tok == TOK_COLON) || (peek(state) == TOK_COLON)) {
8412 eat(state, TOK_COLON);
8413 constant_expr(state);
8420 static struct type *struct_or_union_specifier(
8421 struct compile_state *state, unsigned int spec)
8423 struct type *struct_type;
8424 struct hash_entry *ident;
8425 unsigned int type_join;
8429 switch(peek(state)) {
8431 eat(state, TOK_STRUCT);
8432 type_join = TYPE_PRODUCT;
8435 eat(state, TOK_UNION);
8436 type_join = TYPE_OVERLAP;
8437 error(state, 0, "unions not yet supported\n");
8440 eat(state, TOK_STRUCT);
8441 type_join = TYPE_PRODUCT;
8445 if ((tok == TOK_IDENT) || (tok == TOK_TYPE_NAME)) {
8447 ident = state->token[0].ident;
8449 if (!ident || (peek(state) == TOK_LBRACE)) {
8453 eat(state, TOK_LBRACE);
8454 next = &struct_type;
8456 struct type *base_type;
8458 base_type = specifier_qualifier_list(state);
8461 struct hash_entry *fident;
8463 type = declarator(state, base_type, &fident, 1);
8465 if (peek(state) == TOK_COMMA) {
8467 eat(state, TOK_COMMA);
8469 type = clone_type(0, type);
8470 type->field_ident = fident;
8472 *next = new_type(type_join, *next, type);
8473 next = &((*next)->right);
8478 eat(state, TOK_SEMI);
8479 } while(peek(state) != TOK_RBRACE);
8480 eat(state, TOK_RBRACE);
8481 struct_type = new_type(TYPE_STRUCT | spec, struct_type, 0);
8482 struct_type->type_ident = ident;
8483 struct_type->elements = elements;
8484 symbol(state, ident, &ident->sym_struct, 0, struct_type);
8486 if (ident && ident->sym_struct) {
8487 struct_type = clone_type(spec, ident->sym_struct->type);
8489 else if (ident && !ident->sym_struct) {
8490 error(state, 0, "struct %s undeclared", ident->name);
8495 static unsigned int storage_class_specifier_opt(struct compile_state *state)
8497 unsigned int specifiers;
8498 switch(peek(state)) {
8500 eat(state, TOK_AUTO);
8501 specifiers = STOR_AUTO;
8504 eat(state, TOK_REGISTER);
8505 specifiers = STOR_REGISTER;
8508 eat(state, TOK_STATIC);
8509 specifiers = STOR_STATIC;
8512 eat(state, TOK_EXTERN);
8513 specifiers = STOR_EXTERN;
8516 eat(state, TOK_TYPEDEF);
8517 specifiers = STOR_TYPEDEF;
8520 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
8521 specifiers = STOR_STATIC;
8524 specifiers = STOR_AUTO;
8530 static unsigned int function_specifier_opt(struct compile_state *state)
8532 /* Ignore the inline keyword */
8533 unsigned int specifiers;
8535 switch(peek(state)) {
8537 eat(state, TOK_INLINE);
8538 specifiers = STOR_INLINE;
8543 static unsigned int type_qualifiers(struct compile_state *state)
8545 unsigned int specifiers;
8548 specifiers = QUAL_NONE;
8550 switch(peek(state)) {
8552 eat(state, TOK_CONST);
8553 specifiers = QUAL_CONST;
8556 eat(state, TOK_VOLATILE);
8557 specifiers = QUAL_VOLATILE;
8560 eat(state, TOK_RESTRICT);
8561 specifiers = QUAL_RESTRICT;
8571 static struct type *type_specifier(
8572 struct compile_state *state, unsigned int spec)
8576 switch(peek(state)) {
8578 eat(state, TOK_VOID);
8579 type = new_type(TYPE_VOID | spec, 0, 0);
8582 eat(state, TOK_CHAR);
8583 type = new_type(TYPE_CHAR | spec, 0, 0);
8586 eat(state, TOK_SHORT);
8587 if (peek(state) == TOK_INT) {
8588 eat(state, TOK_INT);
8590 type = new_type(TYPE_SHORT | spec, 0, 0);
8593 eat(state, TOK_INT);
8594 type = new_type(TYPE_INT | spec, 0, 0);
8597 eat(state, TOK_LONG);
8598 switch(peek(state)) {
8600 eat(state, TOK_LONG);
8601 error(state, 0, "long long not supported");
8604 eat(state, TOK_DOUBLE);
8605 error(state, 0, "long double not supported");
8608 eat(state, TOK_INT);
8609 type = new_type(TYPE_LONG | spec, 0, 0);
8612 type = new_type(TYPE_LONG | spec, 0, 0);
8617 eat(state, TOK_FLOAT);
8618 error(state, 0, "type float not supported");
8621 eat(state, TOK_DOUBLE);
8622 error(state, 0, "type double not supported");
8625 eat(state, TOK_SIGNED);
8626 switch(peek(state)) {
8628 eat(state, TOK_LONG);
8629 switch(peek(state)) {
8631 eat(state, TOK_LONG);
8632 error(state, 0, "type long long not supported");
8635 eat(state, TOK_INT);
8636 type = new_type(TYPE_LONG | spec, 0, 0);
8639 type = new_type(TYPE_LONG | spec, 0, 0);
8644 eat(state, TOK_INT);
8645 type = new_type(TYPE_INT | spec, 0, 0);
8648 eat(state, TOK_SHORT);
8649 type = new_type(TYPE_SHORT | spec, 0, 0);
8652 eat(state, TOK_CHAR);
8653 type = new_type(TYPE_CHAR | spec, 0, 0);
8656 type = new_type(TYPE_INT | spec, 0, 0);
8661 eat(state, TOK_UNSIGNED);
8662 switch(peek(state)) {
8664 eat(state, TOK_LONG);
8665 switch(peek(state)) {
8667 eat(state, TOK_LONG);
8668 error(state, 0, "unsigned long long not supported");
8671 eat(state, TOK_INT);
8672 type = new_type(TYPE_ULONG | spec, 0, 0);
8675 type = new_type(TYPE_ULONG | spec, 0, 0);
8680 eat(state, TOK_INT);
8681 type = new_type(TYPE_UINT | spec, 0, 0);
8684 eat(state, TOK_SHORT);
8685 type = new_type(TYPE_USHORT | spec, 0, 0);
8688 eat(state, TOK_CHAR);
8689 type = new_type(TYPE_UCHAR | spec, 0, 0);
8692 type = new_type(TYPE_UINT | spec, 0, 0);
8696 /* struct or union specifier */
8699 type = struct_or_union_specifier(state, spec);
8701 /* enum-spefifier */
8703 type = enum_specifier(state, spec);
8707 type = typedef_name(state, spec);
8710 error(state, 0, "bad type specifier %s",
8711 tokens[peek(state)]);
8717 static int istype(int tok)
8743 static struct type *specifier_qualifier_list(struct compile_state *state)
8746 unsigned int specifiers = 0;
8748 /* type qualifiers */
8749 specifiers |= type_qualifiers(state);
8751 /* type specifier */
8752 type = type_specifier(state, specifiers);
8757 static int isdecl_specifier(int tok)
8760 /* storage class specifier */
8766 /* type qualifier */
8770 /* type specifiers */
8780 /* struct or union specifier */
8783 /* enum-spefifier */
8787 /* function specifiers */
8795 static struct type *decl_specifiers(struct compile_state *state)
8798 unsigned int specifiers;
8799 /* I am overly restrictive in the arragement of specifiers supported.
8800 * C is overly flexible in this department it makes interpreting
8801 * the parse tree difficult.
8805 /* storage class specifier */
8806 specifiers |= storage_class_specifier_opt(state);
8808 /* function-specifier */
8809 specifiers |= function_specifier_opt(state);
8811 /* type qualifier */
8812 specifiers |= type_qualifiers(state);
8814 /* type specifier */
8815 type = type_specifier(state, specifiers);
8824 static struct field_info designator(struct compile_state *state, struct type *type)
8827 struct field_info info;
8831 switch(peek(state)) {
8834 struct triple *value;
8835 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
8836 error(state, 0, "Array designator not in array initializer");
8838 eat(state, TOK_LBRACKET);
8839 value = constant_expr(state);
8840 eat(state, TOK_RBRACKET);
8842 info.type = type->left;
8843 info.offset = value->u.cval * size_of(state, info.type);
8848 struct hash_entry *field;
8849 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
8850 error(state, 0, "Struct designator not in struct initializer");
8852 eat(state, TOK_DOT);
8853 eat(state, TOK_IDENT);
8854 field = state->token[0].ident;
8855 info.offset = field_offset(state, type, field);
8856 info.type = field_type(state, type, field);
8860 error(state, 0, "Invalid designator");
8863 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
8868 static struct triple *initializer(
8869 struct compile_state *state, struct type *type)
8871 struct triple *result;
8872 if (peek(state) != TOK_LBRACE) {
8873 result = assignment_expr(state);
8878 struct field_info info;
8880 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
8881 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
8882 internal_error(state, 0, "unknown initializer type");
8885 info.type = type->left;
8886 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
8887 info.type = next_field(state, type, 0);
8889 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
8892 max_offset = size_of(state, type);
8894 buf = xcmalloc(max_offset, "initializer");
8895 eat(state, TOK_LBRACE);
8897 struct triple *value;
8898 struct type *value_type;
8904 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
8905 info = designator(state, type);
8907 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
8908 (info.offset >= max_offset)) {
8909 error(state, 0, "element beyond bounds");
8911 value_type = info.type;
8912 value = eval_const_expr(state, initializer(state, value_type));
8913 value_size = size_of(state, value_type);
8914 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
8915 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
8916 (max_offset <= info.offset)) {
8920 old_size = max_offset;
8921 max_offset = info.offset + value_size;
8922 buf = xmalloc(max_offset, "initializer");
8923 memcpy(buf, old_buf, old_size);
8926 dest = ((char *)buf) + info.offset;
8927 if (value->op == OP_BLOBCONST) {
8928 memcpy(dest, value->u.blob, value_size);
8930 else if ((value->op == OP_INTCONST) && (value_size == 1)) {
8931 *((uint8_t *)dest) = value->u.cval & 0xff;
8933 else if ((value->op == OP_INTCONST) && (value_size == 2)) {
8934 *((uint16_t *)dest) = value->u.cval & 0xffff;
8936 else if ((value->op == OP_INTCONST) && (value_size == 4)) {
8937 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
8940 internal_error(state, 0, "unhandled constant initializer");
8942 free_triple(state, value);
8943 if (peek(state) == TOK_COMMA) {
8944 eat(state, TOK_COMMA);
8947 info.offset += value_size;
8948 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
8949 info.type = next_field(state, type, info.type);
8950 info.offset = field_offset(state, type,
8951 info.type->field_ident);
8953 } while(comma && (peek(state) != TOK_RBRACE));
8954 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
8955 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
8956 type->elements = max_offset / size_of(state, type->left);
8958 eat(state, TOK_RBRACE);
8959 result = triple(state, OP_BLOBCONST, type, 0, 0);
8960 result->u.blob = buf;
8965 static void resolve_branches(struct compile_state *state)
8967 /* Make a second pass and finish anything outstanding
8968 * with respect to branches. The only outstanding item
8969 * is to see if there are goto to labels that have not
8970 * been defined and to error about them.
8973 for(i = 0; i < HASH_TABLE_SIZE; i++) {
8974 struct hash_entry *entry;
8975 for(entry = state->hash_table[i]; entry; entry = entry->next) {
8977 if (!entry->sym_label) {
8980 ins = entry->sym_label->def;
8981 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
8982 error(state, ins, "label `%s' used but not defined",
8989 static struct triple *function_definition(
8990 struct compile_state *state, struct type *type)
8992 struct triple *def, *tmp, *first, *end;
8993 struct hash_entry *ident;
8996 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
8997 error(state, 0, "Invalid function header");
9000 /* Verify the function type */
9001 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
9002 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
9003 (type->right->field_ident == 0)) {
9004 error(state, 0, "Invalid function parameters");
9006 param = type->right;
9008 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
9010 if (!param->left->field_ident) {
9011 error(state, 0, "No identifier for parameter %d\n", i);
9013 param = param->right;
9016 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
9017 error(state, 0, "No identifier for paramter %d\n", i);
9020 /* Get a list of statements for this function. */
9021 def = triple(state, OP_LIST, type, 0, 0);
9023 /* Start a new scope for the passed parameters */
9026 /* Put a label at the very start of a function */
9027 first = label(state);
9028 RHS(def, 0) = first;
9030 /* Put a label at the very end of a function */
9032 flatten(state, first, end);
9034 /* Walk through the parameters and create symbol table entries
9037 param = type->right;
9038 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
9039 ident = param->left->field_ident;
9040 tmp = variable(state, param->left);
9041 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
9042 flatten(state, end, tmp);
9043 param = param->right;
9045 if ((param->type & TYPE_MASK) != TYPE_VOID) {
9046 /* And don't forget the last parameter */
9047 ident = param->field_ident;
9048 tmp = variable(state, param);
9049 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
9050 flatten(state, end, tmp);
9052 /* Add a variable for the return value */
9054 if ((type->left->type & TYPE_MASK) != TYPE_VOID) {
9055 /* Remove all type qualifiers from the return type */
9056 tmp = variable(state, clone_type(0, type->left));
9057 flatten(state, end, tmp);
9058 /* Remember where the return value is */
9062 /* Remember which function I am compiling.
9063 * Also assume the last defined function is the main function.
9065 state->main_function = def;
9067 /* Now get the actual function definition */
9068 compound_statement(state, end);
9070 /* Finish anything unfinished with branches */
9071 resolve_branches(state);
9073 /* Remove the parameter scope */
9077 fprintf(stdout, "\n");
9078 loc(stdout, state, 0);
9079 fprintf(stdout, "\n__________ function_definition _________\n");
9080 print_triple(state, def);
9081 fprintf(stdout, "__________ function_definition _________ done\n\n");
9087 static struct triple *do_decl(struct compile_state *state,
9088 struct type *type, struct hash_entry *ident)
9092 /* Clean up the storage types used */
9093 switch (type->type & STOR_MASK) {
9096 /* These are the good types I am aiming for */
9099 type->type &= ~STOR_MASK;
9100 type->type |= STOR_AUTO;
9103 type->type &= ~STOR_MASK;
9104 type->type |= STOR_STATIC;
9108 error(state, 0, "typedef without name");
9110 symbol(state, ident, &ident->sym_ident, 0, type);
9111 ident->tok = TOK_TYPE_NAME;
9115 internal_error(state, 0, "Undefined storage class");
9117 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
9118 error(state, 0, "Function prototypes not supported");
9121 ((type->type & STOR_MASK) == STOR_STATIC) &&
9122 ((type->type & QUAL_CONST) == 0)) {
9123 error(state, 0, "non const static variables not supported");
9126 def = variable(state, type);
9127 symbol(state, ident, &ident->sym_ident, def, type);
9132 static void decl(struct compile_state *state, struct triple *first)
9134 struct type *base_type, *type;
9135 struct hash_entry *ident;
9138 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
9139 base_type = decl_specifiers(state);
9141 type = declarator(state, base_type, &ident, 0);
9142 if (global && ident && (peek(state) == TOK_LBRACE)) {
9144 state->function = ident->name;
9145 def = function_definition(state, type);
9146 symbol(state, ident, &ident->sym_ident, def, type);
9147 state->function = 0;
9151 flatten(state, first, do_decl(state, type, ident));
9152 /* type or variable definition */
9155 if (peek(state) == TOK_EQ) {
9157 error(state, 0, "cannot assign to a type");
9160 flatten(state, first,
9162 ident->sym_ident->def,
9163 initializer(state, type)));
9165 arrays_complete(state, type);
9166 if (peek(state) == TOK_COMMA) {
9167 eat(state, TOK_COMMA);
9169 type = declarator(state, base_type, &ident, 0);
9170 flatten(state, first, do_decl(state, type, ident));
9174 eat(state, TOK_SEMI);
9178 static void decls(struct compile_state *state)
9180 struct triple *list;
9182 list = label(state);
9185 if (tok == TOK_EOF) {
9188 if (tok == TOK_SPACE) {
9189 eat(state, TOK_SPACE);
9192 if (list->next != list) {
9193 error(state, 0, "global variables not supported");
9199 * Data structurs for optimation.
9202 static void do_use_block(
9203 struct block *used, struct block_set **head, struct block *user,
9206 struct block_set **ptr, *new;
9213 if ((*ptr)->member == user) {
9216 ptr = &(*ptr)->next;
9218 new = xcmalloc(sizeof(*new), "block_set");
9229 static void do_unuse_block(
9230 struct block *used, struct block_set **head, struct block *unuser)
9232 struct block_set *use, **ptr;
9236 if (use->member == unuser) {
9238 memset(use, -1, sizeof(*use));
9247 static void use_block(struct block *used, struct block *user)
9249 /* Append new to the head of the list, print_block
9252 do_use_block(used, &used->use, user, 1);
9255 static void unuse_block(struct block *used, struct block *unuser)
9257 do_unuse_block(used, &used->use, unuser);
9261 static void idom_block(struct block *idom, struct block *user)
9263 do_use_block(idom, &idom->idominates, user, 0);
9266 static void unidom_block(struct block *idom, struct block *unuser)
9268 do_unuse_block(idom, &idom->idominates, unuser);
9271 static void domf_block(struct block *block, struct block *domf)
9273 do_use_block(block, &block->domfrontier, domf, 0);
9276 static void undomf_block(struct block *block, struct block *undomf)
9278 do_unuse_block(block, &block->domfrontier, undomf);
9281 static void ipdom_block(struct block *ipdom, struct block *user)
9283 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
9286 static void unipdom_block(struct block *ipdom, struct block *unuser)
9288 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
9291 static void ipdomf_block(struct block *block, struct block *ipdomf)
9293 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
9296 static void unipdomf_block(struct block *block, struct block *unipdomf)
9298 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
9303 static int do_walk_triple(struct compile_state *state,
9304 struct triple *ptr, int depth,
9305 int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
9308 result = cb(state, ptr, depth);
9309 if ((result == 0) && (ptr->op == OP_LIST)) {
9310 struct triple *list;
9314 result = do_walk_triple(state, ptr, depth + 1, cb);
9315 if (ptr->next->prev != ptr) {
9316 internal_error(state, ptr->next, "bad prev");
9320 } while((result == 0) && (ptr != RHS(list, 0)));
9325 static int walk_triple(
9326 struct compile_state *state,
9328 int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
9330 return do_walk_triple(state, ptr, 0, cb);
9333 static void do_print_prefix(int depth)
9336 for(i = 0; i < depth; i++) {
9341 #define PRINT_LIST 1
9342 static int do_print_triple(struct compile_state *state, struct triple *ins, int depth)
9346 if (op == OP_LIST) {
9351 if ((op == OP_LABEL) && (ins->use)) {
9352 printf("\n%p:\n", ins);
9354 do_print_prefix(depth);
9355 display_triple(stdout, ins);
9357 if ((ins->op == OP_BRANCH) && ins->use) {
9358 internal_error(state, ins, "branch used?");
9362 struct triple_set *user;
9363 for(user = ins->use; user; user = user->next) {
9364 printf("use: %p\n", user->member);
9368 if (triple_is_branch(state, ins)) {
9374 static void print_triple(struct compile_state *state, struct triple *ins)
9376 walk_triple(state, ins, do_print_triple);
9379 static void print_triples(struct compile_state *state)
9381 print_triple(state, state->main_function);
9385 struct block *block;
9387 static void find_cf_blocks(struct cf_block *cf, struct block *block)
9389 if (!block || (cf[block->vertex].block == block)) {
9392 cf[block->vertex].block = block;
9393 find_cf_blocks(cf, block->left);
9394 find_cf_blocks(cf, block->right);
9397 static void print_control_flow(struct compile_state *state)
9399 struct cf_block *cf;
9401 printf("\ncontrol flow\n");
9402 cf = xcmalloc(sizeof(*cf) * (state->last_vertex + 1), "cf_block");
9403 find_cf_blocks(cf, state->first_block);
9405 for(i = 1; i <= state->last_vertex; i++) {
9406 struct block *block;
9407 block = cf[i].block;
9410 printf("(%p) %d:", block, block->vertex);
9412 printf(" %d", block->left->vertex);
9414 if (block->right && (block->right != block->left)) {
9415 printf(" %d", block->right->vertex);
9424 static struct block *basic_block(struct compile_state *state,
9425 struct triple *first)
9427 struct block *block;
9430 if (first->op != OP_LABEL) {
9431 internal_error(state, 0, "block does not start with a label");
9433 /* See if this basic block has already been setup */
9434 if (first->u.block != 0) {
9435 return first->u.block;
9437 /* Allocate another basic block structure */
9438 state->last_vertex += 1;
9439 block = xcmalloc(sizeof(*block), "block");
9440 block->first = block->last = first;
9441 block->vertex = state->last_vertex;
9444 if ((ptr != first) && (ptr->op == OP_LABEL) && ptr->use) {
9448 /* If ptr->u is not used remember where the baic block is */
9449 if (triple_stores_block(state, ptr)) {
9450 ptr->u.block = block;
9452 if (ptr->op == OP_BRANCH) {
9456 } while (ptr != RHS(state->main_function, 0));
9457 if (ptr == RHS(state->main_function, 0))
9460 if (op == OP_LABEL) {
9461 block->left = basic_block(state, ptr);
9463 use_block(block->left, block);
9465 else if (op == OP_BRANCH) {
9467 /* Trace the branch target */
9468 block->right = basic_block(state, TARG(ptr, 0));
9469 use_block(block->right, block);
9470 /* If there is a test trace the branch as well */
9471 if (TRIPLE_RHS(ptr->sizes)) {
9472 block->left = basic_block(state, ptr->next);
9473 use_block(block->left, block);
9477 internal_error(state, 0, "Bad basic block split");
9483 static void walk_blocks(struct compile_state *state,
9484 void (*cb)(struct compile_state *state, struct block *block, void *arg),
9487 struct triple *ptr, *first;
9488 struct block *last_block;
9490 first = RHS(state->main_function, 0);
9493 struct block *block;
9494 if (ptr->op == OP_LABEL) {
9495 block = ptr->u.block;
9496 if (block && (block != last_block)) {
9497 cb(state, block, arg);
9502 } while(ptr != first);
9505 static void print_block(
9506 struct compile_state *state, struct block *block, void *arg)
9511 fprintf(fp, "\nblock: %p (%d), %p<-%p %p<-%p\n",
9515 block->left && block->left->use?block->left->use->member : 0,
9517 block->right && block->right->use?block->right->use->member : 0);
9518 if (block->first->op == OP_LABEL) {
9519 fprintf(fp, "%p:\n", block->first);
9521 for(ptr = block->first; ; ptr = ptr->next) {
9522 struct triple_set *user;
9525 if (triple_stores_block(state, ptr)) {
9526 if (ptr->u.block != block) {
9527 internal_error(state, ptr,
9528 "Wrong block pointer: %p\n",
9532 if (op == OP_ADECL) {
9533 for(user = ptr->use; user; user = user->next) {
9534 if (!user->member->u.block) {
9535 internal_error(state, user->member,
9536 "Use %p not in a block?\n",
9541 display_triple(fp, ptr);
9544 for(user = ptr->use; user; user = user->next) {
9545 fprintf(fp, "use: %p\n", user->member);
9549 /* Sanity checks... */
9550 valid_ins(state, ptr);
9551 for(user = ptr->use; user; user = user->next) {
9554 valid_ins(state, use);
9555 if (triple_stores_block(state, user->member) &&
9556 !user->member->u.block) {
9557 internal_error(state, user->member,
9558 "Use %p not in a block?",
9563 if (ptr == block->last)
9570 static void print_blocks(struct compile_state *state, FILE *fp)
9572 fprintf(fp, "--------------- blocks ---------------\n");
9573 walk_blocks(state, print_block, fp);
9576 static void prune_nonblock_triples(struct compile_state *state)
9578 struct block *block;
9579 struct triple *first, *ins, *next;
9580 /* Delete the triples not in a basic block */
9581 first = RHS(state->main_function, 0);
9586 if (ins->op == OP_LABEL) {
9587 block = ins->u.block;
9590 release_triple(state, ins);
9593 } while(ins != first);
9596 static void setup_basic_blocks(struct compile_state *state)
9598 if (!triple_stores_block(state, RHS(state->main_function, 0)) ||
9599 !triple_stores_block(state, RHS(state->main_function,0)->prev)) {
9600 internal_error(state, 0, "ins will not store block?");
9602 /* Find the basic blocks */
9603 state->last_vertex = 0;
9604 state->first_block = basic_block(state, RHS(state->main_function,0));
9605 /* Delete the triples not in a basic block */
9606 prune_nonblock_triples(state);
9607 /* Find the last basic block */
9608 state->last_block = RHS(state->main_function, 0)->prev->u.block;
9609 if (!state->last_block) {
9610 internal_error(state, 0, "end not used?");
9612 /* Insert an extra unused edge from start to the end
9613 * This helps with reverse control flow calculations.
9615 use_block(state->first_block, state->last_block);
9616 /* If we are debugging print what I have just done */
9617 if (state->debug & DEBUG_BASIC_BLOCKS) {
9618 print_blocks(state, stdout);
9619 print_control_flow(state);
9623 static void free_basic_block(struct compile_state *state, struct block *block)
9625 struct block_set *entry, *next;
9626 struct block *child;
9630 if (block->vertex == -1) {
9635 unuse_block(block->left, block);
9638 unuse_block(block->right, block);
9641 unidom_block(block->idom, block);
9645 unipdom_block(block->ipdom, block);
9648 for(entry = block->use; entry; entry = next) {
9650 child = entry->member;
9651 unuse_block(block, child);
9652 if (child->left == block) {
9655 if (child->right == block) {
9659 for(entry = block->idominates; entry; entry = next) {
9661 child = entry->member;
9662 unidom_block(block, child);
9665 for(entry = block->domfrontier; entry; entry = next) {
9667 child = entry->member;
9668 undomf_block(block, child);
9670 for(entry = block->ipdominates; entry; entry = next) {
9672 child = entry->member;
9673 unipdom_block(block, child);
9676 for(entry = block->ipdomfrontier; entry; entry = next) {
9678 child = entry->member;
9679 unipdomf_block(block, child);
9681 if (block->users != 0) {
9682 internal_error(state, 0, "block still has users");
9684 free_basic_block(state, block->left);
9686 free_basic_block(state, block->right);
9688 memset(block, -1, sizeof(*block));
9692 static void free_basic_blocks(struct compile_state *state)
9694 struct triple *first, *ins;
9695 free_basic_block(state, state->first_block);
9696 state->last_vertex = 0;
9697 state->first_block = state->last_block = 0;
9698 first = RHS(state->main_function, 0);
9701 if (triple_stores_block(state, ins)) {
9705 } while(ins != first);
9710 struct block *block;
9711 struct sdom_block *sdominates;
9712 struct sdom_block *sdom_next;
9713 struct sdom_block *sdom;
9714 struct sdom_block *label;
9715 struct sdom_block *parent;
9716 struct sdom_block *ancestor;
9721 static void unsdom_block(struct sdom_block *block)
9723 struct sdom_block **ptr;
9724 if (!block->sdom_next) {
9727 ptr = &block->sdom->sdominates;
9729 if ((*ptr) == block) {
9730 *ptr = block->sdom_next;
9733 ptr = &(*ptr)->sdom_next;
9737 static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
9739 unsdom_block(block);
9741 block->sdom_next = sdom->sdominates;
9742 sdom->sdominates = block;
9747 static int initialize_sdblock(struct sdom_block *sd,
9748 struct block *parent, struct block *block, int vertex)
9750 if (!block || (sd[block->vertex].block == block)) {
9754 /* Renumber the blocks in a convinient fashion */
9755 block->vertex = vertex;
9756 sd[vertex].block = block;
9757 sd[vertex].sdom = &sd[vertex];
9758 sd[vertex].label = &sd[vertex];
9759 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
9760 sd[vertex].ancestor = 0;
9761 sd[vertex].vertex = vertex;
9762 vertex = initialize_sdblock(sd, block, block->left, vertex);
9763 vertex = initialize_sdblock(sd, block, block->right, vertex);
9767 static int initialize_sdpblock(struct sdom_block *sd,
9768 struct block *parent, struct block *block, int vertex)
9770 struct block_set *user;
9771 if (!block || (sd[block->vertex].block == block)) {
9775 /* Renumber the blocks in a convinient fashion */
9776 block->vertex = vertex;
9777 sd[vertex].block = block;
9778 sd[vertex].sdom = &sd[vertex];
9779 sd[vertex].label = &sd[vertex];
9780 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
9781 sd[vertex].ancestor = 0;
9782 sd[vertex].vertex = vertex;
9783 for(user = block->use; user; user = user->next) {
9784 vertex = initialize_sdpblock(sd, block, user->member, vertex);
9789 static void compress_ancestors(struct sdom_block *v)
9791 /* This procedure assumes ancestor(v) != 0 */
9792 /* if (ancestor(ancestor(v)) != 0) {
9793 * compress(ancestor(ancestor(v)));
9794 * if (semi(label(ancestor(v))) < semi(label(v))) {
9795 * label(v) = label(ancestor(v));
9797 * ancestor(v) = ancestor(ancestor(v));
9803 if (v->ancestor->ancestor) {
9804 compress_ancestors(v->ancestor->ancestor);
9805 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
9806 v->label = v->ancestor->label;
9808 v->ancestor = v->ancestor->ancestor;
9812 static void compute_sdom(struct compile_state *state, struct sdom_block *sd)
9816 * for each v <= pred(w) {
9818 * if (semi[u] < semi[w] {
9819 * semi[w] = semi[u];
9822 * add w to bucket(vertex(semi[w]));
9823 * LINK(parent(w), w);
9826 * for each v <= bucket(parent(w)) {
9827 * delete v from bucket(parent(w));
9829 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
9832 for(i = state->last_vertex; i >= 2; i--) {
9833 struct sdom_block *v, *parent, *next;
9834 struct block_set *user;
9835 struct block *block;
9836 block = sd[i].block;
9837 parent = sd[i].parent;
9839 for(user = block->use; user; user = user->next) {
9840 struct sdom_block *v, *u;
9841 v = &sd[user->member->vertex];
9842 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9843 if (u->sdom->vertex < sd[i].sdom->vertex) {
9844 sd[i].sdom = u->sdom;
9847 sdom_block(sd[i].sdom, &sd[i]);
9848 sd[i].ancestor = parent;
9850 for(v = parent->sdominates; v; v = next) {
9851 struct sdom_block *u;
9852 next = v->sdom_next;
9854 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
9855 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
9856 u->block : parent->block;
9861 static void compute_spdom(struct compile_state *state, struct sdom_block *sd)
9865 * for each v <= pred(w) {
9867 * if (semi[u] < semi[w] {
9868 * semi[w] = semi[u];
9871 * add w to bucket(vertex(semi[w]));
9872 * LINK(parent(w), w);
9875 * for each v <= bucket(parent(w)) {
9876 * delete v from bucket(parent(w));
9878 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
9881 for(i = state->last_vertex; i >= 2; i--) {
9882 struct sdom_block *u, *v, *parent, *next;
9883 struct block *block;
9884 block = sd[i].block;
9885 parent = sd[i].parent;
9888 v = &sd[block->left->vertex];
9889 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9890 if (u->sdom->vertex < sd[i].sdom->vertex) {
9891 sd[i].sdom = u->sdom;
9894 if (block->right && (block->right != block->left)) {
9895 v = &sd[block->right->vertex];
9896 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9897 if (u->sdom->vertex < sd[i].sdom->vertex) {
9898 sd[i].sdom = u->sdom;
9901 sdom_block(sd[i].sdom, &sd[i]);
9902 sd[i].ancestor = parent;
9904 for(v = parent->sdominates; v; v = next) {
9905 struct sdom_block *u;
9906 next = v->sdom_next;
9908 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
9909 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
9910 u->block : parent->block;
9915 static void compute_idom(struct compile_state *state, struct sdom_block *sd)
9918 for(i = 2; i <= state->last_vertex; i++) {
9919 struct block *block;
9920 block = sd[i].block;
9921 if (block->idom->vertex != sd[i].sdom->vertex) {
9922 block->idom = block->idom->idom;
9924 idom_block(block->idom, block);
9926 sd[1].block->idom = 0;
9929 static void compute_ipdom(struct compile_state *state, struct sdom_block *sd)
9932 for(i = 2; i <= state->last_vertex; i++) {
9933 struct block *block;
9934 block = sd[i].block;
9935 if (block->ipdom->vertex != sd[i].sdom->vertex) {
9936 block->ipdom = block->ipdom->ipdom;
9938 ipdom_block(block->ipdom, block);
9940 sd[1].block->ipdom = 0;
9944 * Every vertex of a flowgraph G = (V, E, r) except r has
9945 * a unique immediate dominator.
9946 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
9947 * rooted at r, called the dominator tree of G, such that
9948 * v dominates w if and only if v is a proper ancestor of w in
9949 * the dominator tree.
9952 * If v and w are vertices of G such that v <= w,
9953 * than any path from v to w must contain a common ancestor
9956 /* Lemma 2: For any vertex w != r, idom(w) -> w */
9957 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
9958 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
9960 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
9961 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
9964 * Let w != r and let u be a vertex for which sdom(u) is
9965 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
9966 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
9968 /* Lemma 5: Let vertices v,w satisfy v -> w.
9969 * Then v -> idom(w) or idom(w) -> idom(v)
9972 static void find_immediate_dominators(struct compile_state *state)
9974 struct sdom_block *sd;
9975 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
9976 * vi > w for (1 <= i <= k - 1}
9979 * For any vertex w != r.
9981 * {v|(v,w) <= E and v < w } U
9982 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
9985 * Let w != r and let u be a vertex for which sdom(u) is
9986 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
9988 * { sdom(w) if sdom(w) = sdom(u),
9990 * { idom(u) otherwise
9992 /* The algorithm consists of the following 4 steps.
9993 * Step 1. Carry out a depth-first search of the problem graph.
9994 * Number the vertices from 1 to N as they are reached during
9995 * the search. Initialize the variables used in succeeding steps.
9996 * Step 2. Compute the semidominators of all vertices by applying
9997 * theorem 4. Carry out the computation vertex by vertex in
9998 * decreasing order by number.
9999 * Step 3. Implicitly define the immediate dominator of each vertex
10000 * by applying Corollary 1.
10001 * Step 4. Explicitly define the immediate dominator of each vertex,
10002 * carrying out the computation vertex by vertex in increasing order
10005 /* Step 1 initialize the basic block information */
10006 sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
10007 initialize_sdblock(sd, 0, state->first_block, 0);
10013 /* Step 2 compute the semidominators */
10014 /* Step 3 implicitly define the immediate dominator of each vertex */
10015 compute_sdom(state, sd);
10016 /* Step 4 explicitly define the immediate dominator of each vertex */
10017 compute_idom(state, sd);
10021 static void find_post_dominators(struct compile_state *state)
10023 struct sdom_block *sd;
10024 /* Step 1 initialize the basic block information */
10025 sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
10027 initialize_sdpblock(sd, 0, state->last_block, 0);
10029 /* Step 2 compute the semidominators */
10030 /* Step 3 implicitly define the immediate dominator of each vertex */
10031 compute_spdom(state, sd);
10032 /* Step 4 explicitly define the immediate dominator of each vertex */
10033 compute_ipdom(state, sd);
10039 static void find_block_domf(struct compile_state *state, struct block *block)
10041 struct block *child;
10042 struct block_set *user;
10043 if (block->domfrontier != 0) {
10044 internal_error(state, block->first, "domfrontier present?");
10046 for(user = block->idominates; user; user = user->next) {
10047 child = user->member;
10048 if (child->idom != block) {
10049 internal_error(state, block->first, "bad idom");
10051 find_block_domf(state, child);
10053 if (block->left && block->left->idom != block) {
10054 domf_block(block, block->left);
10056 if (block->right && block->right->idom != block) {
10057 domf_block(block, block->right);
10059 for(user = block->idominates; user; user = user->next) {
10060 struct block_set *frontier;
10061 child = user->member;
10062 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
10063 if (frontier->member->idom != block) {
10064 domf_block(block, frontier->member);
10070 static void find_block_ipdomf(struct compile_state *state, struct block *block)
10072 struct block *child;
10073 struct block_set *user;
10074 if (block->ipdomfrontier != 0) {
10075 internal_error(state, block->first, "ipdomfrontier present?");
10077 for(user = block->ipdominates; user; user = user->next) {
10078 child = user->member;
10079 if (child->ipdom != block) {
10080 internal_error(state, block->first, "bad ipdom");
10082 find_block_ipdomf(state, child);
10084 if (block->left && block->left->ipdom != block) {
10085 ipdomf_block(block, block->left);
10087 if (block->right && block->right->ipdom != block) {
10088 ipdomf_block(block, block->right);
10090 for(user = block->idominates; user; user = user->next) {
10091 struct block_set *frontier;
10092 child = user->member;
10093 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
10094 if (frontier->member->ipdom != block) {
10095 ipdomf_block(block, frontier->member);
10101 static void print_dominated(
10102 struct compile_state *state, struct block *block, void *arg)
10104 struct block_set *user;
10107 fprintf(fp, "%d:", block->vertex);
10108 for(user = block->idominates; user; user = user->next) {
10109 fprintf(fp, " %d", user->member->vertex);
10110 if (user->member->idom != block) {
10111 internal_error(state, user->member->first, "bad idom");
10117 static void print_dominators(struct compile_state *state, FILE *fp)
10119 fprintf(fp, "\ndominates\n");
10120 walk_blocks(state, print_dominated, fp);
10124 static int print_frontiers(
10125 struct compile_state *state, struct block *block, int vertex)
10127 struct block_set *user;
10129 if (!block || (block->vertex != vertex + 1)) {
10134 printf("%d:", block->vertex);
10135 for(user = block->domfrontier; user; user = user->next) {
10136 printf(" %d", user->member->vertex);
10140 vertex = print_frontiers(state, block->left, vertex);
10141 vertex = print_frontiers(state, block->right, vertex);
10144 static void print_dominance_frontiers(struct compile_state *state)
10146 printf("\ndominance frontiers\n");
10147 print_frontiers(state, state->first_block, 0);
10151 static void analyze_idominators(struct compile_state *state)
10153 /* Find the immediate dominators */
10154 find_immediate_dominators(state);
10155 /* Find the dominance frontiers */
10156 find_block_domf(state, state->first_block);
10157 /* If debuging print the print what I have just found */
10158 if (state->debug & DEBUG_FDOMINATORS) {
10159 print_dominators(state, stdout);
10160 print_dominance_frontiers(state);
10161 print_control_flow(state);
10167 static void print_ipdominated(
10168 struct compile_state *state, struct block *block, void *arg)
10170 struct block_set *user;
10173 fprintf(fp, "%d:", block->vertex);
10174 for(user = block->ipdominates; user; user = user->next) {
10175 fprintf(fp, " %d", user->member->vertex);
10176 if (user->member->ipdom != block) {
10177 internal_error(state, user->member->first, "bad ipdom");
10183 static void print_ipdominators(struct compile_state *state, FILE *fp)
10185 fprintf(fp, "\nipdominates\n");
10186 walk_blocks(state, print_ipdominated, fp);
10189 static int print_pfrontiers(
10190 struct compile_state *state, struct block *block, int vertex)
10192 struct block_set *user;
10194 if (!block || (block->vertex != vertex + 1)) {
10199 printf("%d:", block->vertex);
10200 for(user = block->ipdomfrontier; user; user = user->next) {
10201 printf(" %d", user->member->vertex);
10204 for(user = block->use; user; user = user->next) {
10205 vertex = print_pfrontiers(state, user->member, vertex);
10209 static void print_ipdominance_frontiers(struct compile_state *state)
10211 printf("\nipdominance frontiers\n");
10212 print_pfrontiers(state, state->last_block, 0);
10216 static void analyze_ipdominators(struct compile_state *state)
10218 /* Find the post dominators */
10219 find_post_dominators(state);
10220 /* Find the control dependencies (post dominance frontiers) */
10221 find_block_ipdomf(state, state->last_block);
10222 /* If debuging print the print what I have just found */
10223 if (state->debug & DEBUG_RDOMINATORS) {
10224 print_ipdominators(state, stdout);
10225 print_ipdominance_frontiers(state);
10226 print_control_flow(state);
10230 static int bdominates(struct compile_state *state,
10231 struct block *dom, struct block *sub)
10233 while(sub && (sub != dom)) {
10239 static int tdominates(struct compile_state *state,
10240 struct triple *dom, struct triple *sub)
10242 struct block *bdom, *bsub;
10244 bdom = block_of_triple(state, dom);
10245 bsub = block_of_triple(state, sub);
10246 if (bdom != bsub) {
10247 result = bdominates(state, bdom, bsub);
10250 struct triple *ins;
10252 while((ins != bsub->first) && (ins != dom)) {
10255 result = (ins == dom);
10260 static void insert_phi_operations(struct compile_state *state)
10263 struct triple *first;
10264 int *has_already, *work;
10265 struct block *work_list, **work_list_tail;
10267 struct triple *var;
10269 size = sizeof(int) * (state->last_vertex + 1);
10270 has_already = xcmalloc(size, "has_already");
10271 work = xcmalloc(size, "work");
10274 first = RHS(state->main_function, 0);
10275 for(var = first->next; var != first ; var = var->next) {
10276 struct block *block;
10277 struct triple_set *user;
10278 if ((var->op != OP_ADECL) || !var->use) {
10283 work_list_tail = &work_list;
10284 for(user = var->use; user; user = user->next) {
10285 if (user->member->op == OP_READ) {
10288 if (user->member->op != OP_WRITE) {
10289 internal_error(state, user->member,
10290 "bad variable access");
10292 block = user->member->u.block;
10294 warning(state, user->member, "dead code");
10296 if (work[block->vertex] >= iter) {
10299 work[block->vertex] = iter;
10300 *work_list_tail = block;
10301 block->work_next = 0;
10302 work_list_tail = &block->work_next;
10304 for(block = work_list; block; block = block->work_next) {
10305 struct block_set *df;
10306 for(df = block->domfrontier; df; df = df->next) {
10307 struct triple *phi;
10308 struct block *front;
10310 front = df->member;
10312 if (has_already[front->vertex] >= iter) {
10315 /* Count how many edges flow into this block */
10316 in_edges = front->users;
10317 /* Insert a phi function for this variable */
10318 get_occurance(front->first->occurance);
10319 phi = alloc_triple(
10320 state, OP_PHI, var->type, -1, in_edges,
10321 front->first->occurance);
10322 phi->u.block = front;
10323 MISC(phi, 0) = var;
10324 use_triple(var, phi);
10325 /* Insert the phi functions immediately after the label */
10326 insert_triple(state, front->first->next, phi);
10327 if (front->first == front->last) {
10328 front->last = front->first->next;
10330 has_already[front->vertex] = iter;
10332 /* If necessary plan to visit the basic block */
10333 if (work[front->vertex] >= iter) {
10336 work[front->vertex] = iter;
10337 *work_list_tail = front;
10338 front->work_next = 0;
10339 work_list_tail = &front->work_next;
10343 xfree(has_already);
10351 static void fixup_block_phi_variables(
10352 struct compile_state *state, struct block *parent, struct block *block)
10354 struct block_set *set;
10355 struct triple *ptr;
10357 if (!parent || !block)
10359 /* Find the edge I am coming in on */
10361 for(set = block->use; set; set = set->next, edge++) {
10362 if (set->member == parent) {
10367 internal_error(state, 0, "phi input is not on a control predecessor");
10369 for(ptr = block->first; ; ptr = ptr->next) {
10370 if (ptr->op == OP_PHI) {
10371 struct triple *var, *val, **slot;
10372 var = MISC(ptr, 0);
10374 internal_error(state, ptr, "no var???");
10376 /* Find the current value of the variable */
10377 val = var->use->member;
10378 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10379 internal_error(state, val, "bad value in phi");
10381 if (edge >= TRIPLE_RHS(ptr->sizes)) {
10382 internal_error(state, ptr, "edges > phi rhs");
10384 slot = &RHS(ptr, edge);
10385 if ((*slot != 0) && (*slot != val)) {
10386 internal_error(state, ptr, "phi already bound on this edge");
10389 use_triple(val, ptr);
10391 if (ptr == block->last) {
10398 static void rename_block_variables(
10399 struct compile_state *state, struct block *block)
10401 struct block_set *user;
10402 struct triple *ptr, *next, *last;
10406 last = block->first;
10408 for(ptr = block->first; !done; ptr = next) {
10410 if (ptr == block->last) {
10414 if (ptr->op == OP_READ) {
10415 struct triple *var, *val;
10417 unuse_triple(var, ptr);
10419 error(state, ptr, "variable used without being set");
10421 /* Find the current value of the variable */
10422 val = var->use->member;
10423 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10424 internal_error(state, val, "bad value in read");
10426 propogate_use(state, ptr, val);
10427 release_triple(state, ptr);
10431 if (ptr->op == OP_WRITE) {
10432 struct triple *var, *val;
10435 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10436 internal_error(state, val, "bad value in write");
10438 propogate_use(state, ptr, val);
10439 unuse_triple(var, ptr);
10440 /* Push OP_WRITE ptr->right onto a stack of variable uses */
10441 push_triple(var, val);
10443 if (ptr->op == OP_PHI) {
10444 struct triple *var;
10445 var = MISC(ptr, 0);
10446 /* Push OP_PHI onto a stack of variable uses */
10447 push_triple(var, ptr);
10451 block->last = last;
10453 /* Fixup PHI functions in the cf successors */
10454 fixup_block_phi_variables(state, block, block->left);
10455 fixup_block_phi_variables(state, block, block->right);
10456 /* rename variables in the dominated nodes */
10457 for(user = block->idominates; user; user = user->next) {
10458 rename_block_variables(state, user->member);
10460 /* pop the renamed variable stack */
10461 last = block->first;
10463 for(ptr = block->first; !done ; ptr = next) {
10465 if (ptr == block->last) {
10468 if (ptr->op == OP_WRITE) {
10469 struct triple *var;
10471 /* Pop OP_WRITE ptr->right from the stack of variable uses */
10472 pop_triple(var, RHS(ptr, 0));
10473 release_triple(state, ptr);
10476 if (ptr->op == OP_PHI) {
10477 struct triple *var;
10478 var = MISC(ptr, 0);
10479 /* Pop OP_WRITE ptr->right from the stack of variable uses */
10480 pop_triple(var, ptr);
10484 block->last = last;
10487 static void prune_block_variables(struct compile_state *state,
10488 struct block *block)
10490 struct block_set *user;
10491 struct triple *next, *last, *ptr;
10493 last = block->first;
10495 for(ptr = block->first; !done; ptr = next) {
10497 if (ptr == block->last) {
10500 if (ptr->op == OP_ADECL) {
10501 struct triple_set *user, *next;
10502 for(user = ptr->use; user; user = next) {
10503 struct triple *use;
10505 use = user->member;
10506 if (use->op != OP_PHI) {
10507 internal_error(state, use, "decl still used");
10509 if (MISC(use, 0) != ptr) {
10510 internal_error(state, use, "bad phi use of decl");
10512 unuse_triple(ptr, use);
10515 release_triple(state, ptr);
10520 block->last = last;
10521 for(user = block->idominates; user; user = user->next) {
10522 prune_block_variables(state, user->member);
10526 static void transform_to_ssa_form(struct compile_state *state)
10528 insert_phi_operations(state);
10530 printf("@%s:%d\n", __FILE__, __LINE__);
10531 print_blocks(state, stdout);
10533 rename_block_variables(state, state->first_block);
10534 prune_block_variables(state, state->first_block);
10538 static void clear_vertex(
10539 struct compile_state *state, struct block *block, void *arg)
10544 static void mark_live_block(
10545 struct compile_state *state, struct block *block, int *next_vertex)
10547 /* See if this is a block that has not been marked */
10548 if (block->vertex != 0) {
10551 block->vertex = *next_vertex;
10553 if (triple_is_branch(state, block->last)) {
10554 struct triple **targ;
10555 targ = triple_targ(state, block->last, 0);
10556 for(; targ; targ = triple_targ(state, block->last, targ)) {
10560 if (!triple_stores_block(state, *targ)) {
10561 internal_error(state, 0, "bad targ");
10563 mark_live_block(state, (*targ)->u.block, next_vertex);
10566 else if (block->last->next != RHS(state->main_function, 0)) {
10567 struct triple *ins;
10568 ins = block->last->next;
10569 if (!triple_stores_block(state, ins)) {
10570 internal_error(state, 0, "bad block start");
10572 mark_live_block(state, ins->u.block, next_vertex);
10576 static void transform_from_ssa_form(struct compile_state *state)
10578 /* To get out of ssa form we insert moves on the incoming
10579 * edges to blocks containting phi functions.
10581 struct triple *first;
10582 struct triple *phi, *next;
10585 /* Walk the control flow to see which blocks remain alive */
10586 walk_blocks(state, clear_vertex, 0);
10588 mark_live_block(state, state->first_block, &next_vertex);
10590 /* Walk all of the operations to find the phi functions */
10591 first = RHS(state->main_function, 0);
10592 for(phi = first->next; phi != first ; phi = next) {
10593 struct block_set *set;
10594 struct block *block;
10595 struct triple **slot;
10596 struct triple *var, *read;
10597 struct triple_set *use, *use_next;
10600 if (phi->op != OP_PHI) {
10603 block = phi->u.block;
10604 slot = &RHS(phi, 0);
10606 /* Forget uses from code in dead blocks */
10607 for(use = phi->use; use; use = use_next) {
10608 struct block *ublock;
10609 struct triple **expr;
10610 use_next = use->next;
10611 ublock = block_of_triple(state, use->member);
10612 if ((use->member == phi) || (ublock->vertex != 0)) {
10615 expr = triple_rhs(state, use->member, 0);
10616 for(; expr; expr = triple_rhs(state, use->member, expr)) {
10617 if (*expr == phi) {
10621 unuse_triple(phi, use->member);
10624 /* A variable to replace the phi function */
10625 var = post_triple(state, phi, OP_ADECL, phi->type, 0,0);
10626 /* A read of the single value that is set into the variable */
10627 read = post_triple(state, var, OP_READ, phi->type, var, 0);
10628 use_triple(var, read);
10630 /* Replaces uses of the phi with variable reads */
10631 propogate_use(state, phi, read);
10633 /* Walk all of the incoming edges/blocks and insert moves.
10635 for(edge = 0, set = block->use; set; set = set->next, edge++) {
10636 struct block *eblock;
10637 struct triple *move;
10638 struct triple *val;
10639 eblock = set->member;
10642 unuse_triple(val, phi);
10644 if (!val || (val == &zero_triple) ||
10645 (block->vertex == 0) || (eblock->vertex == 0) ||
10646 (val == phi) || (val == read)) {
10650 move = post_triple(state,
10651 val, OP_WRITE, phi->type, var, val);
10652 use_triple(val, move);
10653 use_triple(var, move);
10655 /* See if there are any writers of var */
10657 for(use = var->use; use; use = use->next) {
10658 struct triple **expr;
10659 expr = triple_lhs(state, use->member, 0);
10660 for(; expr; expr = triple_lhs(state, use->member, expr)) {
10661 if (*expr == var) {
10666 /* If var is not used free it */
10668 unuse_triple(var, read);
10669 free_triple(state, read);
10670 free_triple(state, var);
10673 /* Release the phi function */
10674 release_triple(state, phi);
10681 * Register conflict resolution
10682 * =========================================================
10685 static struct reg_info find_def_color(
10686 struct compile_state *state, struct triple *def)
10688 struct triple_set *set;
10689 struct reg_info info;
10690 info.reg = REG_UNSET;
10692 if (!triple_is_def(state, def)) {
10695 info = arch_reg_lhs(state, def, 0);
10696 if (info.reg >= MAX_REGISTERS) {
10697 info.reg = REG_UNSET;
10699 for(set = def->use; set; set = set->next) {
10700 struct reg_info tinfo;
10702 i = find_rhs_use(state, set->member, def);
10706 tinfo = arch_reg_rhs(state, set->member, i);
10707 if (tinfo.reg >= MAX_REGISTERS) {
10708 tinfo.reg = REG_UNSET;
10710 if ((tinfo.reg != REG_UNSET) &&
10711 (info.reg != REG_UNSET) &&
10712 (tinfo.reg != info.reg)) {
10713 internal_error(state, def, "register conflict");
10715 if ((info.regcm & tinfo.regcm) == 0) {
10716 internal_error(state, def, "regcm conflict %x & %x == 0",
10717 info.regcm, tinfo.regcm);
10719 if (info.reg == REG_UNSET) {
10720 info.reg = tinfo.reg;
10722 info.regcm &= tinfo.regcm;
10724 if (info.reg >= MAX_REGISTERS) {
10725 internal_error(state, def, "register out of range");
10730 static struct reg_info find_lhs_pre_color(
10731 struct compile_state *state, struct triple *ins, int index)
10733 struct reg_info info;
10735 zrhs = TRIPLE_RHS(ins->sizes);
10736 zlhs = TRIPLE_LHS(ins->sizes);
10737 if (!zlhs && triple_is_def(state, ins)) {
10740 if (index >= zlhs) {
10741 internal_error(state, ins, "Bad lhs %d", index);
10743 info = arch_reg_lhs(state, ins, index);
10744 for(i = 0; i < zrhs; i++) {
10745 struct reg_info rinfo;
10746 rinfo = arch_reg_rhs(state, ins, i);
10747 if ((info.reg == rinfo.reg) &&
10748 (rinfo.reg >= MAX_REGISTERS)) {
10749 struct reg_info tinfo;
10750 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
10751 info.reg = tinfo.reg;
10752 info.regcm &= tinfo.regcm;
10756 if (info.reg >= MAX_REGISTERS) {
10757 info.reg = REG_UNSET;
10762 static struct reg_info find_rhs_post_color(
10763 struct compile_state *state, struct triple *ins, int index);
10765 static struct reg_info find_lhs_post_color(
10766 struct compile_state *state, struct triple *ins, int index)
10768 struct triple_set *set;
10769 struct reg_info info;
10770 struct triple *lhs;
10772 fprintf(stderr, "find_lhs_post_color(%p, %d)\n",
10775 if ((index == 0) && triple_is_def(state, ins)) {
10778 else if (index < TRIPLE_LHS(ins->sizes)) {
10779 lhs = LHS(ins, index);
10782 internal_error(state, ins, "Bad lhs %d", index);
10785 info = arch_reg_lhs(state, ins, index);
10786 if (info.reg >= MAX_REGISTERS) {
10787 info.reg = REG_UNSET;
10789 for(set = lhs->use; set; set = set->next) {
10790 struct reg_info rinfo;
10791 struct triple *user;
10793 user = set->member;
10794 zrhs = TRIPLE_RHS(user->sizes);
10795 for(i = 0; i < zrhs; i++) {
10796 if (RHS(user, i) != lhs) {
10799 rinfo = find_rhs_post_color(state, user, i);
10800 if ((info.reg != REG_UNSET) &&
10801 (rinfo.reg != REG_UNSET) &&
10802 (info.reg != rinfo.reg)) {
10803 internal_error(state, ins, "register conflict");
10805 if ((info.regcm & rinfo.regcm) == 0) {
10806 internal_error(state, ins, "regcm conflict %x & %x == 0",
10807 info.regcm, rinfo.regcm);
10809 if (info.reg == REG_UNSET) {
10810 info.reg = rinfo.reg;
10812 info.regcm &= rinfo.regcm;
10816 fprintf(stderr, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
10817 ins, index, info.reg, info.regcm);
10822 static struct reg_info find_rhs_post_color(
10823 struct compile_state *state, struct triple *ins, int index)
10825 struct reg_info info, rinfo;
10828 fprintf(stderr, "find_rhs_post_color(%p, %d)\n",
10831 rinfo = arch_reg_rhs(state, ins, index);
10832 zlhs = TRIPLE_LHS(ins->sizes);
10833 if (!zlhs && triple_is_def(state, ins)) {
10837 if (info.reg >= MAX_REGISTERS) {
10838 info.reg = REG_UNSET;
10840 for(i = 0; i < zlhs; i++) {
10841 struct reg_info linfo;
10842 linfo = arch_reg_lhs(state, ins, i);
10843 if ((linfo.reg == rinfo.reg) &&
10844 (linfo.reg >= MAX_REGISTERS)) {
10845 struct reg_info tinfo;
10846 tinfo = find_lhs_post_color(state, ins, i);
10847 if (tinfo.reg >= MAX_REGISTERS) {
10848 tinfo.reg = REG_UNSET;
10850 info.regcm &= linfo.reg;
10851 info.regcm &= tinfo.regcm;
10852 if (info.reg != REG_UNSET) {
10853 internal_error(state, ins, "register conflict");
10855 if (info.regcm == 0) {
10856 internal_error(state, ins, "regcm conflict");
10858 info.reg = tinfo.reg;
10862 fprintf(stderr, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
10863 ins, index, info.reg, info.regcm);
10868 static struct reg_info find_lhs_color(
10869 struct compile_state *state, struct triple *ins, int index)
10871 struct reg_info pre, post, info;
10873 fprintf(stderr, "find_lhs_color(%p, %d)\n",
10876 pre = find_lhs_pre_color(state, ins, index);
10877 post = find_lhs_post_color(state, ins, index);
10878 if ((pre.reg != post.reg) &&
10879 (pre.reg != REG_UNSET) &&
10880 (post.reg != REG_UNSET)) {
10881 internal_error(state, ins, "register conflict");
10883 info.regcm = pre.regcm & post.regcm;
10884 info.reg = pre.reg;
10885 if (info.reg == REG_UNSET) {
10886 info.reg = post.reg;
10889 fprintf(stderr, "find_lhs_color(%p, %d) -> ( %d, %x)\n",
10890 ins, index, info.reg, info.regcm);
10895 static struct triple *post_copy(struct compile_state *state, struct triple *ins)
10897 struct triple_set *entry, *next;
10898 struct triple *out;
10899 struct reg_info info, rinfo;
10901 info = arch_reg_lhs(state, ins, 0);
10902 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
10903 use_triple(RHS(out, 0), out);
10904 /* Get the users of ins to use out instead */
10905 for(entry = ins->use; entry; entry = next) {
10907 next = entry->next;
10908 if (entry->member == out) {
10911 i = find_rhs_use(state, entry->member, ins);
10915 rinfo = arch_reg_rhs(state, entry->member, i);
10916 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
10919 replace_rhs_use(state, ins, out, entry->member);
10921 transform_to_arch_instruction(state, out);
10925 static struct triple *pre_copy(
10926 struct compile_state *state, struct triple *ins, int index)
10928 /* Carefully insert enough operations so that I can
10929 * enter any operation with a GPR32.
10932 struct triple **expr;
10933 if (ins->op == OP_PHI) {
10934 internal_error(state, ins, "pre_copy on a phi?");
10936 expr = &RHS(ins, index);
10937 in = pre_triple(state, ins, OP_COPY, (*expr)->type, *expr, 0);
10938 unuse_triple(*expr, ins);
10940 use_triple(RHS(in, 0), in);
10941 use_triple(in, ins);
10942 transform_to_arch_instruction(state, in);
10947 static void insert_copies_to_phi(struct compile_state *state)
10949 /* To get out of ssa form we insert moves on the incoming
10950 * edges to blocks containting phi functions.
10952 struct triple *first;
10953 struct triple *phi;
10955 /* Walk all of the operations to find the phi functions */
10956 first = RHS(state->main_function, 0);
10957 for(phi = first->next; phi != first ; phi = phi->next) {
10958 struct block_set *set;
10959 struct block *block;
10960 struct triple **slot;
10962 if (phi->op != OP_PHI) {
10965 phi->id |= TRIPLE_FLAG_POST_SPLIT;
10966 block = phi->u.block;
10967 slot = &RHS(phi, 0);
10968 /* Walk all of the incoming edges/blocks and insert moves.
10970 for(edge = 0, set = block->use; set; set = set->next, edge++) {
10971 struct block *eblock;
10972 struct triple *move;
10973 struct triple *val;
10974 struct triple *ptr;
10975 eblock = set->member;
10982 get_occurance(val->occurance);
10983 move = build_triple(state, OP_COPY, phi->type, val, 0,
10985 move->u.block = eblock;
10986 move->id |= TRIPLE_FLAG_PRE_SPLIT;
10987 use_triple(val, move);
10990 unuse_triple(val, phi);
10991 use_triple(move, phi);
10993 /* Walk through the block backwards to find
10994 * an appropriate location for the OP_COPY.
10996 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
10997 struct triple **expr;
10998 if ((ptr == phi) || (ptr == val)) {
11001 expr = triple_rhs(state, ptr, 0);
11002 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11003 if ((*expr) == phi) {
11009 if (triple_is_branch(state, ptr)) {
11010 internal_error(state, ptr,
11011 "Could not insert write to phi");
11013 insert_triple(state, ptr->next, move);
11014 if (eblock->last == ptr) {
11015 eblock->last = move;
11017 transform_to_arch_instruction(state, move);
11022 struct triple_reg_set {
11023 struct triple_reg_set *next;
11024 struct triple *member;
11025 struct triple *new;
11029 struct block *block;
11030 struct triple_reg_set *in;
11031 struct triple_reg_set *out;
11035 static int do_triple_set(struct triple_reg_set **head,
11036 struct triple *member, struct triple *new_member)
11038 struct triple_reg_set **ptr, *new;
11043 if ((*ptr)->member == member) {
11046 ptr = &(*ptr)->next;
11048 new = xcmalloc(sizeof(*new), "triple_set");
11049 new->member = member;
11050 new->new = new_member;
11056 static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
11058 struct triple_reg_set *entry, **ptr;
11062 if (entry->member == member) {
11063 *ptr = entry->next;
11068 ptr = &entry->next;
11073 static int in_triple(struct reg_block *rb, struct triple *in)
11075 return do_triple_set(&rb->in, in, 0);
11077 static void unin_triple(struct reg_block *rb, struct triple *unin)
11079 do_triple_unset(&rb->in, unin);
11082 static int out_triple(struct reg_block *rb, struct triple *out)
11084 return do_triple_set(&rb->out, out, 0);
11086 static void unout_triple(struct reg_block *rb, struct triple *unout)
11088 do_triple_unset(&rb->out, unout);
11091 static int initialize_regblock(struct reg_block *blocks,
11092 struct block *block, int vertex)
11094 struct block_set *user;
11095 if (!block || (blocks[block->vertex].block == block)) {
11099 /* Renumber the blocks in a convinient fashion */
11100 block->vertex = vertex;
11101 blocks[vertex].block = block;
11102 blocks[vertex].vertex = vertex;
11103 for(user = block->use; user; user = user->next) {
11104 vertex = initialize_regblock(blocks, user->member, vertex);
11109 static int phi_in(struct compile_state *state, struct reg_block *blocks,
11110 struct reg_block *rb, struct block *suc)
11112 /* Read the conditional input set of a successor block
11113 * (i.e. the input to the phi nodes) and place it in the
11114 * current blocks output set.
11116 struct block_set *set;
11117 struct triple *ptr;
11121 /* Find the edge I am coming in on */
11122 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
11123 if (set->member == rb->block) {
11128 internal_error(state, 0, "Not coming on a control edge?");
11130 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
11131 struct triple **slot, *expr, *ptr2;
11132 int out_change, done2;
11133 done = (ptr == suc->last);
11134 if (ptr->op != OP_PHI) {
11137 slot = &RHS(ptr, 0);
11139 out_change = out_triple(rb, expr);
11143 /* If we don't define the variable also plast it
11144 * in the current blocks input set.
11146 ptr2 = rb->block->first;
11147 for(done2 = 0; !done2; ptr2 = ptr2->next) {
11148 if (ptr2 == expr) {
11151 done2 = (ptr2 == rb->block->last);
11156 change |= in_triple(rb, expr);
11161 static int reg_in(struct compile_state *state, struct reg_block *blocks,
11162 struct reg_block *rb, struct block *suc)
11164 struct triple_reg_set *in_set;
11167 /* Read the input set of a successor block
11168 * and place it in the current blocks output set.
11170 in_set = blocks[suc->vertex].in;
11171 for(; in_set; in_set = in_set->next) {
11172 int out_change, done;
11173 struct triple *first, *last, *ptr;
11174 out_change = out_triple(rb, in_set->member);
11178 /* If we don't define the variable also place it
11179 * in the current blocks input set.
11181 first = rb->block->first;
11182 last = rb->block->last;
11184 for(ptr = first; !done; ptr = ptr->next) {
11185 if (ptr == in_set->member) {
11188 done = (ptr == last);
11193 change |= in_triple(rb, in_set->member);
11195 change |= phi_in(state, blocks, rb, suc);
11200 static int use_in(struct compile_state *state, struct reg_block *rb)
11202 /* Find the variables we use but don't define and add
11203 * it to the current blocks input set.
11205 #warning "FIXME is this O(N^2) algorithm bad?"
11206 struct block *block;
11207 struct triple *ptr;
11212 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
11213 struct triple **expr;
11214 done = (ptr == block->first);
11215 /* The variable a phi function uses depends on the
11216 * control flow, and is handled in phi_in, not
11219 if (ptr->op == OP_PHI) {
11222 expr = triple_rhs(state, ptr, 0);
11223 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11224 struct triple *rhs, *test;
11230 /* See if rhs is defined in this block */
11231 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
11232 tdone = (test == block->first);
11238 /* If I still have a valid rhs add it to in */
11239 change |= in_triple(rb, rhs);
11245 static struct reg_block *compute_variable_lifetimes(
11246 struct compile_state *state)
11248 struct reg_block *blocks;
11251 sizeof(*blocks)*(state->last_vertex + 1), "reg_block");
11252 initialize_regblock(blocks, state->last_block, 0);
11256 for(i = 1; i <= state->last_vertex; i++) {
11257 struct reg_block *rb;
11259 /* Add the left successor's input set to in */
11260 if (rb->block->left) {
11261 change |= reg_in(state, blocks, rb, rb->block->left);
11263 /* Add the right successor's input set to in */
11264 if ((rb->block->right) &&
11265 (rb->block->right != rb->block->left)) {
11266 change |= reg_in(state, blocks, rb, rb->block->right);
11268 /* Add use to in... */
11269 change |= use_in(state, rb);
11275 static void free_variable_lifetimes(
11276 struct compile_state *state, struct reg_block *blocks)
11279 /* free in_set && out_set on each block */
11280 for(i = 1; i <= state->last_vertex; i++) {
11281 struct triple_reg_set *entry, *next;
11282 struct reg_block *rb;
11284 for(entry = rb->in; entry ; entry = next) {
11285 next = entry->next;
11286 do_triple_unset(&rb->in, entry->member);
11288 for(entry = rb->out; entry; entry = next) {
11289 next = entry->next;
11290 do_triple_unset(&rb->out, entry->member);
11297 typedef void (*wvl_cb_t)(
11298 struct compile_state *state,
11299 struct reg_block *blocks, struct triple_reg_set *live,
11300 struct reg_block *rb, struct triple *ins, void *arg);
11302 static void walk_variable_lifetimes(struct compile_state *state,
11303 struct reg_block *blocks, wvl_cb_t cb, void *arg)
11307 for(i = 1; i <= state->last_vertex; i++) {
11308 struct triple_reg_set *live;
11309 struct triple_reg_set *entry, *next;
11310 struct triple *ptr, *prev;
11311 struct reg_block *rb;
11312 struct block *block;
11315 /* Get the blocks */
11319 /* Copy out into live */
11321 for(entry = rb->out; entry; entry = next) {
11322 next = entry->next;
11323 do_triple_set(&live, entry->member, entry->new);
11325 /* Walk through the basic block calculating live */
11326 for(done = 0, ptr = block->last; !done; ptr = prev) {
11327 struct triple **expr;
11330 done = (ptr == block->first);
11332 /* Ensure the current definition is in live */
11333 if (triple_is_def(state, ptr)) {
11334 do_triple_set(&live, ptr, 0);
11337 /* Inform the callback function of what is
11340 cb(state, blocks, live, rb, ptr, arg);
11342 /* Remove the current definition from live */
11343 do_triple_unset(&live, ptr);
11345 /* Add the current uses to live.
11347 * It is safe to skip phi functions because they do
11348 * not have any block local uses, and the block
11349 * output sets already properly account for what
11350 * control flow depedent uses phi functions do have.
11352 if (ptr->op == OP_PHI) {
11355 expr = triple_rhs(state, ptr, 0);
11356 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11357 /* If the triple is not a definition skip it. */
11358 if (!*expr || !triple_is_def(state, *expr)) {
11361 do_triple_set(&live, *expr, 0);
11365 for(entry = live; entry; entry = next) {
11366 next = entry->next;
11367 do_triple_unset(&live, entry->member);
11372 static int count_triples(struct compile_state *state)
11374 struct triple *first, *ins;
11376 first = RHS(state->main_function, 0);
11381 } while (ins != first);
11384 struct dead_triple {
11385 struct triple *triple;
11386 struct dead_triple *work_next;
11387 struct block *block;
11390 #define TRIPLE_FLAG_ALIVE 1
11394 static void awaken(
11395 struct compile_state *state,
11396 struct dead_triple *dtriple, struct triple **expr,
11397 struct dead_triple ***work_list_tail)
11399 struct triple *triple;
11400 struct dead_triple *dt;
11408 if (triple->id <= 0) {
11409 internal_error(state, triple, "bad triple id: %d",
11412 if (triple->op == OP_NOOP) {
11413 internal_warning(state, triple, "awakening noop?");
11416 dt = &dtriple[triple->id];
11417 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11418 dt->flags |= TRIPLE_FLAG_ALIVE;
11419 if (!dt->work_next) {
11420 **work_list_tail = dt;
11421 *work_list_tail = &dt->work_next;
11426 static void eliminate_inefectual_code(struct compile_state *state)
11428 struct block *block;
11429 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
11431 struct triple *first, *ins;
11433 /* Setup the work list */
11435 work_list_tail = &work_list;
11437 first = RHS(state->main_function, 0);
11439 /* Count how many triples I have */
11440 triples = count_triples(state);
11442 /* Now put then in an array and mark all of the triples dead */
11443 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
11449 if (ins->op == OP_LABEL) {
11450 block = ins->u.block;
11452 dtriple[i].triple = ins;
11453 dtriple[i].block = block;
11454 dtriple[i].flags = 0;
11455 dtriple[i].color = ins->id;
11457 /* See if it is an operation we always keep */
11458 #warning "FIXME handle the case of killing a branch instruction"
11459 if (!triple_is_pure(state, ins) || triple_is_branch(state, ins)) {
11460 awaken(state, dtriple, &ins, &work_list_tail);
11464 } while(ins != first);
11466 struct dead_triple *dt;
11467 struct block_set *user;
11468 struct triple **expr;
11470 work_list = dt->work_next;
11472 work_list_tail = &work_list;
11474 /* Wake up the data depencencies of this triple */
11477 expr = triple_rhs(state, dt->triple, expr);
11478 awaken(state, dtriple, expr, &work_list_tail);
11481 expr = triple_lhs(state, dt->triple, expr);
11482 awaken(state, dtriple, expr, &work_list_tail);
11485 expr = triple_misc(state, dt->triple, expr);
11486 awaken(state, dtriple, expr, &work_list_tail);
11488 /* Wake up the forward control dependencies */
11490 expr = triple_targ(state, dt->triple, expr);
11491 awaken(state, dtriple, expr, &work_list_tail);
11493 /* Wake up the reverse control dependencies of this triple */
11494 for(user = dt->block->ipdomfrontier; user; user = user->next) {
11495 awaken(state, dtriple, &user->member->last, &work_list_tail);
11498 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
11499 if ((dt->triple->op == OP_NOOP) &&
11500 (dt->flags & TRIPLE_FLAG_ALIVE)) {
11501 internal_error(state, dt->triple, "noop effective?");
11503 dt->triple->id = dt->color; /* Restore the color */
11504 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11505 #warning "FIXME handle the case of killing a basic block"
11506 if (dt->block->first == dt->triple) {
11509 if (dt->block->last == dt->triple) {
11510 dt->block->last = dt->triple->prev;
11512 release_triple(state, dt->triple);
11519 static void insert_mandatory_copies(struct compile_state *state)
11521 struct triple *ins, *first;
11523 /* The object is with a minimum of inserted copies,
11524 * to resolve in fundamental register conflicts between
11525 * register value producers and consumers.
11526 * Theoretically we may be greater than minimal when we
11527 * are inserting copies before instructions but that
11528 * case should be rare.
11530 first = RHS(state->main_function, 0);
11533 struct triple_set *entry, *next;
11534 struct triple *tmp;
11535 struct reg_info info;
11536 unsigned reg, regcm;
11537 int do_post_copy, do_pre_copy;
11539 if (!triple_is_def(state, ins)) {
11542 /* Find the architecture specific color information */
11543 info = arch_reg_lhs(state, ins, 0);
11544 if (info.reg >= MAX_REGISTERS) {
11545 info.reg = REG_UNSET;
11549 regcm = arch_type_to_regcm(state, ins->type);
11550 do_post_copy = do_pre_copy = 0;
11552 /* Walk through the uses of ins and check for conflicts */
11553 for(entry = ins->use; entry; entry = next) {
11554 struct reg_info rinfo;
11556 next = entry->next;
11557 i = find_rhs_use(state, entry->member, ins);
11562 /* Find the users color requirements */
11563 rinfo = arch_reg_rhs(state, entry->member, i);
11564 if (rinfo.reg >= MAX_REGISTERS) {
11565 rinfo.reg = REG_UNSET;
11568 /* See if I need a pre_copy */
11569 if (rinfo.reg != REG_UNSET) {
11570 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
11575 regcm &= rinfo.regcm;
11576 regcm = arch_regcm_normalize(state, regcm);
11583 (((info.reg != REG_UNSET) &&
11584 (reg != REG_UNSET) &&
11585 (info.reg != reg)) ||
11586 ((info.regcm & regcm) == 0));
11589 regcm = info.regcm;
11590 /* Walk through the uses of insert and do a pre_copy or see if a post_copy is warranted */
11591 for(entry = ins->use; entry; entry = next) {
11592 struct reg_info rinfo;
11594 next = entry->next;
11595 i = find_rhs_use(state, entry->member, ins);
11600 /* Find the users color requirements */
11601 rinfo = arch_reg_rhs(state, entry->member, i);
11602 if (rinfo.reg >= MAX_REGISTERS) {
11603 rinfo.reg = REG_UNSET;
11606 /* Now see if it is time to do the pre_copy */
11607 if (rinfo.reg != REG_UNSET) {
11608 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
11609 ((regcm & rinfo.regcm) == 0) ||
11610 /* Don't let a mandatory coalesce sneak
11611 * into a operation that is marked to prevent
11614 ((reg != REG_UNNEEDED) &&
11615 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
11616 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
11619 struct triple *user;
11620 user = entry->member;
11621 if (RHS(user, i) != ins) {
11622 internal_error(state, user, "bad rhs");
11624 tmp = pre_copy(state, user, i);
11625 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
11633 if ((regcm & rinfo.regcm) == 0) {
11635 struct triple *user;
11636 user = entry->member;
11637 if (RHS(user, i) != ins) {
11638 internal_error(state, user, "bad rhs");
11640 tmp = pre_copy(state, user, i);
11641 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
11647 regcm &= rinfo.regcm;
11650 if (do_post_copy) {
11651 struct reg_info pre, post;
11652 tmp = post_copy(state, ins);
11653 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
11654 pre = arch_reg_lhs(state, ins, 0);
11655 post = arch_reg_lhs(state, tmp, 0);
11656 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
11657 internal_error(state, tmp, "useless copy");
11662 } while(ins != first);
11666 struct live_range_edge;
11667 struct live_range_def;
11668 struct live_range {
11669 struct live_range_edge *edges;
11670 struct live_range_def *defs;
11671 /* Note. The list pointed to by defs is kept in order.
11672 * That is baring splits in the flow control
11673 * defs dominates defs->next wich dominates defs->next->next
11680 struct live_range *group_next, **group_prev;
11683 struct live_range_edge {
11684 struct live_range_edge *next;
11685 struct live_range *node;
11688 struct live_range_def {
11689 struct live_range_def *next;
11690 struct live_range_def *prev;
11691 struct live_range *lr;
11692 struct triple *def;
11696 #define LRE_HASH_SIZE 2048
11698 struct lre_hash *next;
11699 struct live_range *left;
11700 struct live_range *right;
11705 struct lre_hash *hash[LRE_HASH_SIZE];
11706 struct reg_block *blocks;
11707 struct live_range_def *lrd;
11708 struct live_range *lr;
11709 struct live_range *low, **low_tail;
11710 struct live_range *high, **high_tail;
11713 int passes, max_passes;
11714 #define MAX_ALLOCATION_PASSES 100
11718 static unsigned regc_max_size(struct compile_state *state, int classes)
11723 for(i = 0; i < MAX_REGC; i++) {
11724 if (classes & (1 << i)) {
11726 size = arch_regc_size(state, i);
11727 if (size > max_size) {
11735 static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
11737 unsigned equivs[MAX_REG_EQUIVS];
11739 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
11740 internal_error(state, 0, "invalid register");
11742 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
11743 internal_error(state, 0, "invalid register");
11745 arch_reg_equivs(state, equivs, reg1);
11746 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11747 if (equivs[i] == reg2) {
11754 static void reg_fill_used(struct compile_state *state, char *used, int reg)
11756 unsigned equivs[MAX_REG_EQUIVS];
11758 if (reg == REG_UNNEEDED) {
11761 arch_reg_equivs(state, equivs, reg);
11762 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11763 used[equivs[i]] = 1;
11768 static void reg_inc_used(struct compile_state *state, char *used, int reg)
11770 unsigned equivs[MAX_REG_EQUIVS];
11772 if (reg == REG_UNNEEDED) {
11775 arch_reg_equivs(state, equivs, reg);
11776 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11777 used[equivs[i]] += 1;
11782 static unsigned int hash_live_edge(
11783 struct live_range *left, struct live_range *right)
11785 unsigned int hash, val;
11786 unsigned long lval, rval;
11787 lval = ((unsigned long)left)/sizeof(struct live_range);
11788 rval = ((unsigned long)right)/sizeof(struct live_range);
11793 hash = (hash *263) + val;
11798 hash = (hash *263) + val;
11800 hash = hash & (LRE_HASH_SIZE - 1);
11804 static struct lre_hash **lre_probe(struct reg_state *rstate,
11805 struct live_range *left, struct live_range *right)
11807 struct lre_hash **ptr;
11808 unsigned int index;
11809 /* Ensure left <= right */
11810 if (left > right) {
11811 struct live_range *tmp;
11816 index = hash_live_edge(left, right);
11818 ptr = &rstate->hash[index];
11820 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
11823 ptr = &(*ptr)->next;
11828 static int interfere(struct reg_state *rstate,
11829 struct live_range *left, struct live_range *right)
11831 struct lre_hash **ptr;
11832 ptr = lre_probe(rstate, left, right);
11833 return ptr && *ptr;
11836 static void add_live_edge(struct reg_state *rstate,
11837 struct live_range *left, struct live_range *right)
11839 /* FIXME the memory allocation overhead is noticeable here... */
11840 struct lre_hash **ptr, *new_hash;
11841 struct live_range_edge *edge;
11843 if (left == right) {
11846 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
11849 /* Ensure left <= right */
11850 if (left > right) {
11851 struct live_range *tmp;
11856 ptr = lre_probe(rstate, left, right);
11861 fprintf(stderr, "new_live_edge(%p, %p)\n",
11864 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
11865 new_hash->next = *ptr;
11866 new_hash->left = left;
11867 new_hash->right = right;
11870 edge = xmalloc(sizeof(*edge), "live_range_edge");
11871 edge->next = left->edges;
11872 edge->node = right;
11873 left->edges = edge;
11876 edge = xmalloc(sizeof(*edge), "live_range_edge");
11877 edge->next = right->edges;
11879 right->edges = edge;
11880 right->degree += 1;
11883 static void remove_live_edge(struct reg_state *rstate,
11884 struct live_range *left, struct live_range *right)
11886 struct live_range_edge *edge, **ptr;
11887 struct lre_hash **hptr, *entry;
11888 hptr = lre_probe(rstate, left, right);
11889 if (!hptr || !*hptr) {
11893 *hptr = entry->next;
11896 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
11898 if (edge->node == right) {
11900 memset(edge, 0, sizeof(*edge));
11906 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
11908 if (edge->node == left) {
11910 memset(edge, 0, sizeof(*edge));
11918 static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
11920 struct live_range_edge *edge, *next;
11921 for(edge = range->edges; edge; edge = next) {
11923 remove_live_edge(rstate, range, edge->node);
11927 static void transfer_live_edges(struct reg_state *rstate,
11928 struct live_range *dest, struct live_range *src)
11930 struct live_range_edge *edge, *next;
11931 for(edge = src->edges; edge; edge = next) {
11932 struct live_range *other;
11934 other = edge->node;
11935 remove_live_edge(rstate, src, other);
11936 add_live_edge(rstate, dest, other);
11941 /* Interference graph...
11943 * new(n) --- Return a graph with n nodes but no edges.
11944 * add(g,x,y) --- Return a graph including g with an between x and y
11945 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
11946 * x and y in the graph g
11947 * degree(g, x) --- Return the degree of the node x in the graph g
11948 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
11950 * Implement with a hash table && a set of adjcency vectors.
11951 * The hash table supports constant time implementations of add and interfere.
11952 * The adjacency vectors support an efficient implementation of neighbors.
11956 * +---------------------------------------------------+
11957 * | +--------------+ |
11959 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
11961 * -- In simplify implment optimistic coloring... (No backtracking)
11962 * -- Implement Rematerialization it is the only form of spilling we can perform
11963 * Essentially this means dropping a constant from a register because
11964 * we can regenerate it later.
11966 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
11967 * coalesce at phi points...
11968 * --- Bias coloring if at all possible do the coalesing a compile time.
11973 static void different_colored(
11974 struct compile_state *state, struct reg_state *rstate,
11975 struct triple *parent, struct triple *ins)
11977 struct live_range *lr;
11978 struct triple **expr;
11979 lr = rstate->lrd[ins->id].lr;
11980 expr = triple_rhs(state, ins, 0);
11981 for(;expr; expr = triple_rhs(state, ins, expr)) {
11982 struct live_range *lr2;
11983 if (!*expr || (*expr == parent) || (*expr == ins)) {
11986 lr2 = rstate->lrd[(*expr)->id].lr;
11987 if (lr->color == lr2->color) {
11988 internal_error(state, ins, "live range too big");
11994 static struct live_range *coalesce_ranges(
11995 struct compile_state *state, struct reg_state *rstate,
11996 struct live_range *lr1, struct live_range *lr2)
11998 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
12004 if (!lr1->defs || !lr2->defs) {
12005 internal_error(state, 0,
12006 "cannot coalese dead live ranges");
12008 if ((lr1->color == REG_UNNEEDED) ||
12009 (lr2->color == REG_UNNEEDED)) {
12010 internal_error(state, 0,
12011 "cannot coalesce live ranges without a possible color");
12013 if ((lr1->color != lr2->color) &&
12014 (lr1->color != REG_UNSET) &&
12015 (lr2->color != REG_UNSET)) {
12016 internal_error(state, lr1->defs->def,
12017 "cannot coalesce live ranges of different colors");
12019 color = lr1->color;
12020 if (color == REG_UNSET) {
12021 color = lr2->color;
12023 classes = lr1->classes & lr2->classes;
12025 internal_error(state, lr1->defs->def,
12026 "cannot coalesce live ranges with dissimilar register classes");
12028 /* If there is a clear dominate live range put it in lr1,
12029 * For purposes of this test phi functions are
12030 * considered dominated by the definitions that feed into
12033 if ((lr1->defs->prev->def->op == OP_PHI) ||
12034 ((lr2->defs->prev->def->op != OP_PHI) &&
12035 tdominates(state, lr2->defs->def, lr1->defs->def))) {
12036 struct live_range *tmp;
12042 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
12043 fprintf(stderr, "lr1 post\n");
12045 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
12046 fprintf(stderr, "lr1 pre\n");
12048 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
12049 fprintf(stderr, "lr2 post\n");
12051 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
12052 fprintf(stderr, "lr2 pre\n");
12056 fprintf(stderr, "coalesce color1(%p): %3d color2(%p) %3d\n",
12063 lr1->classes = classes;
12064 /* Append lr2 onto lr1 */
12065 #warning "FIXME should this be a merge instead of a splice?"
12066 /* This FIXME item applies to the correctness of live_range_end
12067 * and to the necessity of making multiple passes of coalesce_live_ranges.
12068 * A failure to find some coalesce opportunities in coaleace_live_ranges
12069 * does not impact the correct of the compiler just the efficiency with
12070 * which registers are allocated.
12073 mid1 = lr1->defs->prev;
12075 end = lr2->defs->prev;
12083 /* Fixup the live range in the added live range defs */
12088 } while(lrd != head);
12090 /* Mark lr2 as free. */
12092 lr2->color = REG_UNNEEDED;
12096 internal_error(state, 0, "lr1->defs == 0 ?");
12099 lr1->color = color;
12100 lr1->classes = classes;
12102 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
12103 transfer_live_edges(rstate, lr1, lr2);
12108 static struct live_range_def *live_range_head(
12109 struct compile_state *state, struct live_range *lr,
12110 struct live_range_def *last)
12112 struct live_range_def *result;
12117 else if (!tdominates(state, lr->defs->def, last->next->def)) {
12118 result = last->next;
12123 static struct live_range_def *live_range_end(
12124 struct compile_state *state, struct live_range *lr,
12125 struct live_range_def *last)
12127 struct live_range_def *result;
12130 result = lr->defs->prev;
12132 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
12133 result = last->prev;
12139 static void initialize_live_ranges(
12140 struct compile_state *state, struct reg_state *rstate)
12142 struct triple *ins, *first;
12143 size_t count, size;
12146 first = RHS(state->main_function, 0);
12147 /* First count how many instructions I have.
12149 count = count_triples(state);
12150 /* Potentially I need one live range definitions for each
12151 * instruction, plus an extra for the split routines.
12153 rstate->defs = count + 1;
12154 /* Potentially I need one live range for each instruction
12155 * plus an extra for the dummy live range.
12157 rstate->ranges = count + 1;
12158 size = sizeof(rstate->lrd[0]) * rstate->defs;
12159 rstate->lrd = xcmalloc(size, "live_range_def");
12160 size = sizeof(rstate->lr[0]) * rstate->ranges;
12161 rstate->lr = xcmalloc(size, "live_range");
12163 /* Setup the dummy live range */
12164 rstate->lr[0].classes = 0;
12165 rstate->lr[0].color = REG_UNSET;
12166 rstate->lr[0].defs = 0;
12170 /* If the triple is a variable give it a live range */
12171 if (triple_is_def(state, ins)) {
12172 struct reg_info info;
12173 /* Find the architecture specific color information */
12174 info = find_def_color(state, ins);
12177 rstate->lr[i].defs = &rstate->lrd[j];
12178 rstate->lr[i].color = info.reg;
12179 rstate->lr[i].classes = info.regcm;
12180 rstate->lr[i].degree = 0;
12181 rstate->lrd[j].lr = &rstate->lr[i];
12183 /* Otherwise give the triple the dummy live range. */
12185 rstate->lrd[j].lr = &rstate->lr[0];
12188 /* Initalize the live_range_def */
12189 rstate->lrd[j].next = &rstate->lrd[j];
12190 rstate->lrd[j].prev = &rstate->lrd[j];
12191 rstate->lrd[j].def = ins;
12192 rstate->lrd[j].orig_id = ins->id;
12197 } while(ins != first);
12198 rstate->ranges = i;
12201 /* Make a second pass to handle achitecture specific register
12206 int zlhs, zrhs, i, j;
12207 if (ins->id > rstate->defs) {
12208 internal_error(state, ins, "bad id");
12211 /* Walk through the template of ins and coalesce live ranges */
12212 zlhs = TRIPLE_LHS(ins->sizes);
12213 if ((zlhs == 0) && triple_is_def(state, ins)) {
12216 zrhs = TRIPLE_RHS(ins->sizes);
12218 for(i = 0; i < zlhs; i++) {
12219 struct reg_info linfo;
12220 struct live_range_def *lhs;
12221 linfo = arch_reg_lhs(state, ins, i);
12222 if (linfo.reg < MAX_REGISTERS) {
12225 if (triple_is_def(state, ins)) {
12226 lhs = &rstate->lrd[ins->id];
12228 lhs = &rstate->lrd[LHS(ins, i)->id];
12230 for(j = 0; j < zrhs; j++) {
12231 struct reg_info rinfo;
12232 struct live_range_def *rhs;
12233 rinfo = arch_reg_rhs(state, ins, j);
12234 if (rinfo.reg < MAX_REGISTERS) {
12237 rhs = &rstate->lrd[RHS(ins, i)->id];
12238 if (rinfo.reg == linfo.reg) {
12239 coalesce_ranges(state, rstate,
12245 } while(ins != first);
12248 static void graph_ins(
12249 struct compile_state *state,
12250 struct reg_block *blocks, struct triple_reg_set *live,
12251 struct reg_block *rb, struct triple *ins, void *arg)
12253 struct reg_state *rstate = arg;
12254 struct live_range *def;
12255 struct triple_reg_set *entry;
12257 /* If the triple is not a definition
12258 * we do not have a definition to add to
12259 * the interference graph.
12261 if (!triple_is_def(state, ins)) {
12264 def = rstate->lrd[ins->id].lr;
12266 /* Create an edge between ins and everything that is
12267 * alive, unless the live_range cannot share
12268 * a physical register with ins.
12270 for(entry = live; entry; entry = entry->next) {
12271 struct live_range *lr;
12272 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
12273 internal_error(state, 0, "bad entry?");
12275 lr = rstate->lrd[entry->member->id].lr;
12279 if (!arch_regcm_intersect(def->classes, lr->classes)) {
12282 add_live_edge(rstate, def, lr);
12287 static struct live_range *get_verify_live_range(
12288 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
12290 struct live_range *lr;
12291 struct live_range_def *lrd;
12293 if ((ins->id < 0) || (ins->id > rstate->defs)) {
12294 internal_error(state, ins, "bad ins?");
12296 lr = rstate->lrd[ins->id].lr;
12300 if (lrd->def == ins) {
12304 } while(lrd != lr->defs);
12306 internal_error(state, ins, "ins not in live range");
12311 static void verify_graph_ins(
12312 struct compile_state *state,
12313 struct reg_block *blocks, struct triple_reg_set *live,
12314 struct reg_block *rb, struct triple *ins, void *arg)
12316 struct reg_state *rstate = arg;
12317 struct triple_reg_set *entry1, *entry2;
12320 /* Compare live against edges and make certain the code is working */
12321 for(entry1 = live; entry1; entry1 = entry1->next) {
12322 struct live_range *lr1;
12323 lr1 = get_verify_live_range(state, rstate, entry1->member);
12324 for(entry2 = live; entry2; entry2 = entry2->next) {
12325 struct live_range *lr2;
12326 struct live_range_edge *edge2;
12329 if (entry2 == entry1) {
12332 lr2 = get_verify_live_range(state, rstate, entry2->member);
12334 internal_error(state, entry2->member,
12335 "live range with 2 values simultaneously alive");
12337 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
12340 if (!interfere(rstate, lr1, lr2)) {
12341 internal_error(state, entry2->member,
12342 "edges don't interfere?");
12347 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
12349 if (edge2->node == lr1) {
12353 if (lr2_degree != lr2->degree) {
12354 internal_error(state, entry2->member,
12355 "computed degree: %d does not match reported degree: %d\n",
12356 lr2_degree, lr2->degree);
12359 internal_error(state, entry2->member, "missing edge");
12367 static void print_interference_ins(
12368 struct compile_state *state,
12369 struct reg_block *blocks, struct triple_reg_set *live,
12370 struct reg_block *rb, struct triple *ins, void *arg)
12372 struct reg_state *rstate = arg;
12373 struct live_range *lr;
12376 lr = rstate->lrd[ins->id].lr;
12378 ins->id = rstate->lrd[id].orig_id;
12379 SET_REG(ins->id, lr->color);
12380 display_triple(stdout, ins);
12384 struct live_range_def *lrd;
12388 printf(" %-10p", lrd->def);
12390 } while(lrd != lr->defs);
12394 struct triple_reg_set *entry;
12396 for(entry = live; entry; entry = entry->next) {
12397 printf(" %-10p", entry->member);
12402 struct live_range_edge *entry;
12404 for(entry = lr->edges; entry; entry = entry->next) {
12405 struct live_range_def *lrd;
12406 lrd = entry->node->defs;
12408 printf(" %-10p", lrd->def);
12410 } while(lrd != entry->node->defs);
12415 if (triple_is_branch(state, ins)) {
12421 static int coalesce_live_ranges(
12422 struct compile_state *state, struct reg_state *rstate)
12424 /* At the point where a value is moved from one
12425 * register to another that value requires two
12426 * registers, thus increasing register pressure.
12427 * Live range coaleescing reduces the register
12428 * pressure by keeping a value in one register
12431 * In the case of a phi function all paths leading
12432 * into it must be allocated to the same register
12433 * otherwise the phi function may not be removed.
12435 * Forcing a value to stay in a single register
12436 * for an extended period of time does have
12437 * limitations when applied to non homogenous
12440 * The two cases I have identified are:
12441 * 1) Two forced register assignments may
12443 * 2) Registers may go unused because they
12444 * are only good for storing the value
12445 * and not manipulating it.
12447 * Because of this I need to split live ranges,
12448 * even outside of the context of coalesced live
12449 * ranges. The need to split live ranges does
12450 * impose some constraints on live range coalescing.
12452 * - Live ranges may not be coalesced across phi
12453 * functions. This creates a 2 headed live
12454 * range that cannot be sanely split.
12456 * - phi functions (coalesced in initialize_live_ranges)
12457 * are handled as pre split live ranges so we will
12458 * never attempt to split them.
12464 for(i = 0; i <= rstate->ranges; i++) {
12465 struct live_range *lr1;
12466 struct live_range_def *lrd1;
12467 lr1 = &rstate->lr[i];
12471 lrd1 = live_range_end(state, lr1, 0);
12472 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
12473 struct triple_set *set;
12474 if (lrd1->def->op != OP_COPY) {
12477 /* Skip copies that are the result of a live range split. */
12478 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
12481 for(set = lrd1->def->use; set; set = set->next) {
12482 struct live_range_def *lrd2;
12483 struct live_range *lr2, *res;
12485 lrd2 = &rstate->lrd[set->member->id];
12487 /* Don't coalesce with instructions
12488 * that are the result of a live range
12491 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
12494 lr2 = rstate->lrd[set->member->id].lr;
12498 if ((lr1->color != lr2->color) &&
12499 (lr1->color != REG_UNSET) &&
12500 (lr2->color != REG_UNSET)) {
12503 if ((lr1->classes & lr2->classes) == 0) {
12507 if (interfere(rstate, lr1, lr2)) {
12511 res = coalesce_ranges(state, rstate, lr1, lr2);
12525 static void fix_coalesce_conflicts(struct compile_state *state,
12526 struct reg_block *blocks, struct triple_reg_set *live,
12527 struct reg_block *rb, struct triple *ins, void *arg)
12529 int zlhs, zrhs, i, j;
12531 /* See if we have a mandatory coalesce operation between
12532 * a lhs and a rhs value. If so and the rhs value is also
12533 * alive then this triple needs to be pre copied. Otherwise
12534 * we would have two definitions in the same live range simultaneously
12537 zlhs = TRIPLE_LHS(ins->sizes);
12538 if ((zlhs == 0) && triple_is_def(state, ins)) {
12541 zrhs = TRIPLE_RHS(ins->sizes);
12542 for(i = 0; i < zlhs; i++) {
12543 struct reg_info linfo;
12544 linfo = arch_reg_lhs(state, ins, i);
12545 if (linfo.reg < MAX_REGISTERS) {
12548 for(j = 0; j < zrhs; j++) {
12549 struct reg_info rinfo;
12550 struct triple *rhs;
12551 struct triple_reg_set *set;
12554 rinfo = arch_reg_rhs(state, ins, j);
12555 if (rinfo.reg != linfo.reg) {
12559 for(set = live; set && !found; set = set->next) {
12560 if (set->member == rhs) {
12565 struct triple *copy;
12566 copy = pre_copy(state, ins, j);
12567 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12574 static void replace_set_use(struct compile_state *state,
12575 struct triple_reg_set *head, struct triple *orig, struct triple *new)
12577 struct triple_reg_set *set;
12578 for(set = head; set; set = set->next) {
12579 if (set->member == orig) {
12585 static void replace_block_use(struct compile_state *state,
12586 struct reg_block *blocks, struct triple *orig, struct triple *new)
12589 #warning "WISHLIST visit just those blocks that need it *"
12590 for(i = 1; i <= state->last_vertex; i++) {
12591 struct reg_block *rb;
12593 replace_set_use(state, rb->in, orig, new);
12594 replace_set_use(state, rb->out, orig, new);
12598 static void color_instructions(struct compile_state *state)
12600 struct triple *ins, *first;
12601 first = RHS(state->main_function, 0);
12604 if (triple_is_def(state, ins)) {
12605 struct reg_info info;
12606 info = find_lhs_color(state, ins, 0);
12607 if (info.reg >= MAX_REGISTERS) {
12608 info.reg = REG_UNSET;
12610 SET_INFO(ins->id, info);
12613 } while(ins != first);
12616 static struct reg_info read_lhs_color(
12617 struct compile_state *state, struct triple *ins, int index)
12619 struct reg_info info;
12620 if ((index == 0) && triple_is_def(state, ins)) {
12621 info.reg = ID_REG(ins->id);
12622 info.regcm = ID_REGCM(ins->id);
12624 else if (index < TRIPLE_LHS(ins->sizes)) {
12625 info = read_lhs_color(state, LHS(ins, index), 0);
12628 internal_error(state, ins, "Bad lhs %d", index);
12629 info.reg = REG_UNSET;
12635 static struct triple *resolve_tangle(
12636 struct compile_state *state, struct triple *tangle)
12638 struct reg_info info, uinfo;
12639 struct triple_set *set, *next;
12640 struct triple *copy;
12642 #warning "WISHLIST recalculate all affected instructions colors"
12643 info = find_lhs_color(state, tangle, 0);
12644 for(set = tangle->use; set; set = next) {
12645 struct triple *user;
12648 user = set->member;
12649 zrhs = TRIPLE_RHS(user->sizes);
12650 for(i = 0; i < zrhs; i++) {
12651 if (RHS(user, i) != tangle) {
12654 uinfo = find_rhs_post_color(state, user, i);
12655 if (uinfo.reg == info.reg) {
12656 copy = pre_copy(state, user, i);
12657 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12658 SET_INFO(copy->id, uinfo);
12663 uinfo = find_lhs_pre_color(state, tangle, 0);
12664 if (uinfo.reg == info.reg) {
12665 struct reg_info linfo;
12666 copy = post_copy(state, tangle);
12667 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12668 linfo = find_lhs_color(state, copy, 0);
12669 SET_INFO(copy->id, linfo);
12671 info = find_lhs_color(state, tangle, 0);
12672 SET_INFO(tangle->id, info);
12678 static void fix_tangles(struct compile_state *state,
12679 struct reg_block *blocks, struct triple_reg_set *live,
12680 struct reg_block *rb, struct triple *ins, void *arg)
12682 int *tangles = arg;
12683 struct triple *tangle;
12685 char used[MAX_REGISTERS];
12686 struct triple_reg_set *set;
12689 /* Find out which registers have multiple uses at this point */
12690 memset(used, 0, sizeof(used));
12691 for(set = live; set; set = set->next) {
12692 struct reg_info info;
12693 info = read_lhs_color(state, set->member, 0);
12694 if (info.reg == REG_UNSET) {
12697 reg_inc_used(state, used, info.reg);
12700 /* Now find the least dominated definition of a register in
12701 * conflict I have seen so far.
12703 for(set = live; set; set = set->next) {
12704 struct reg_info info;
12705 info = read_lhs_color(state, set->member, 0);
12706 if (used[info.reg] < 2) {
12709 /* Changing copies that feed into phi functions
12712 if (set->member->use &&
12713 (set->member->use->member->op == OP_PHI)) {
12716 if (!tangle || tdominates(state, set->member, tangle)) {
12717 tangle = set->member;
12720 /* If I have found a tangle resolve it */
12722 struct triple *post_copy;
12724 post_copy = resolve_tangle(state, tangle);
12726 replace_block_use(state, blocks, tangle, post_copy);
12728 if (post_copy && (tangle != ins)) {
12729 replace_set_use(state, live, tangle, post_copy);
12736 static int correct_tangles(
12737 struct compile_state *state, struct reg_block *blocks)
12741 color_instructions(state);
12742 walk_variable_lifetimes(state, blocks, fix_tangles, &tangles);
12746 struct least_conflict {
12747 struct reg_state *rstate;
12748 struct live_range *ref_range;
12749 struct triple *ins;
12750 struct triple_reg_set *live;
12754 static void least_conflict(struct compile_state *state,
12755 struct reg_block *blocks, struct triple_reg_set *live,
12756 struct reg_block *rb, struct triple *ins, void *arg)
12758 struct least_conflict *conflict = arg;
12759 struct live_range_edge *edge;
12760 struct triple_reg_set *set;
12764 #warning "FIXME handle instructions with left hand sides..."
12765 /* Only instructions that introduce a new definition
12766 * can be the conflict instruction.
12768 if (!triple_is_def(state, ins)) {
12772 /* See if live ranges at this instruction are a
12773 * strict subset of the live ranges that are in conflict.
12776 for(set = live; set; set = set->next) {
12777 struct live_range *lr;
12778 lr = conflict->rstate->lrd[set->member->id].lr;
12779 /* Ignore it if there cannot be an edge between these two nodes */
12780 if (!arch_regcm_intersect(conflict->ref_range->classes, lr->classes)) {
12783 for(edge = conflict->ref_range->edges; edge; edge = edge->next) {
12784 if (edge->node == lr) {
12788 if (!edge && (lr != conflict->ref_range)) {
12798 /* See if there is an uncolored member in this subset.
12800 for(set = live; set; set = set->next) {
12801 struct live_range *lr;
12802 lr = conflict->rstate->lrd[set->member->id].lr;
12803 if (lr->color == REG_UNSET) {
12807 if (!set && (conflict->ref_range != REG_UNSET)) {
12812 /* See if any of the live registers are constrained,
12813 * if not it won't be productive to pick this as
12814 * a conflict instruction.
12817 for(set = live; set; set = set->next) {
12818 struct triple_set *uset;
12819 struct reg_info info;
12821 unsigned cur_size, size;
12822 /* Skip this instruction */
12823 if (set->member == ins) {
12826 /* Find how many registers this value can potentially
12829 classes = arch_type_to_regcm(state, set->member->type);
12830 size = regc_max_size(state, classes);
12832 /* Find how many registers we allow this value to
12835 info = arch_reg_lhs(state, set->member, 0);
12837 /* If the value does not live in a register it
12838 * isn't constrained.
12840 if (info.reg == REG_UNNEEDED) {
12844 if ((info.reg == REG_UNSET) || (info.reg >= MAX_REGISTERS)) {
12845 cur_size = regc_max_size(state, info.regcm);
12850 /* If there is no difference between potential and
12851 * actual register count there is not a constraint
12853 if (cur_size >= size) {
12857 /* If this live_range feeds into conflict->inds
12858 * it isn't a constraint we can relieve.
12860 for(uset = set->member->use; uset; uset = uset->next) {
12861 if (uset->member == ins) {
12871 /* Don't drop canidates with constraints */
12872 if (conflict->constraints && !constraints) {
12878 fprintf(stderr, "conflict ins? %p %s count: %d constraints: %d\n",
12879 ins, tops(ins->op), count, constraints);
12881 /* Find the instruction with the largest possible subset of
12882 * conflict ranges and that dominates any other instruction
12883 * with an equal sized set of conflicting ranges.
12885 if ((count > conflict->count) ||
12886 ((count == conflict->count) &&
12887 tdominates(state, ins, conflict->ins))) {
12888 struct triple_reg_set *next;
12889 /* Remember the canidate instruction */
12890 conflict->ins = ins;
12891 conflict->count = count;
12892 conflict->constraints = constraints;
12893 /* Free the old collection of live registers */
12894 for(set = conflict->live; set; set = next) {
12896 do_triple_unset(&conflict->live, set->member);
12898 conflict->live = 0;
12899 /* Rember the registers that are alive but do not feed
12900 * into or out of conflict->ins.
12902 for(set = live; set; set = set->next) {
12903 struct triple **expr;
12904 if (set->member == ins) {
12907 expr = triple_rhs(state, ins, 0);
12908 for(;expr; expr = triple_rhs(state, ins, expr)) {
12909 if (*expr == set->member) {
12913 expr = triple_lhs(state, ins, 0);
12914 for(; expr; expr = triple_lhs(state, ins, expr)) {
12915 if (*expr == set->member) {
12919 do_triple_set(&conflict->live, set->member, set->new);
12927 static void find_range_conflict(struct compile_state *state,
12928 struct reg_state *rstate, char *used, struct live_range *ref_range,
12929 struct least_conflict *conflict)
12932 /* there are 3 kinds ways conflicts can occure.
12933 * 1) the life time of 2 values simply overlap.
12934 * 2) the 2 values feed into the same instruction.
12935 * 3) the 2 values feed into a phi function.
12938 /* find the instruction where the problematic conflict comes
12939 * into existance. that the instruction where all of
12940 * the values are alive, and among such instructions it is
12941 * the least dominated one.
12943 * a value is alive an an instruction if either;
12944 * 1) the value defintion dominates the instruction and there
12945 * is a use at or after that instrction
12946 * 2) the value definition feeds into a phi function in the
12947 * same block as the instruction. and the phi function
12948 * is at or after the instruction.
12950 memset(conflict, 0, sizeof(*conflict));
12951 conflict->rstate = rstate;
12952 conflict->ref_range = ref_range;
12954 conflict->live = 0;
12955 conflict->count = 0;
12956 conflict->constraints = 0;
12957 walk_variable_lifetimes(state, rstate->blocks, least_conflict, conflict);
12959 if (!conflict->ins) {
12960 internal_error(state, ref_range->defs->def, "No conflict ins?");
12962 if (!conflict->live) {
12963 internal_error(state, ref_range->defs->def, "No conflict live?");
12966 fprintf(stderr, "conflict ins: %p %s count: %d constraints: %d\n",
12967 conflict->ins, tops(conflict->ins->op),
12968 conflict->count, conflict->constraints);
12973 static struct triple *split_constrained_range(struct compile_state *state,
12974 struct reg_state *rstate, char *used, struct least_conflict *conflict)
12976 unsigned constrained_size;
12977 struct triple *new, *constrained;
12978 struct triple_reg_set *cset;
12979 /* Find a range that is having problems because it is
12980 * artificially constrained.
12982 constrained_size = ~0;
12985 for(cset = conflict->live; cset; cset = cset->next) {
12986 struct triple_set *set;
12987 struct reg_info info;
12989 unsigned cur_size, size;
12990 /* Skip the live range that starts with conflict->ins */
12991 if (cset->member == conflict->ins) {
12994 /* Find how many registers this value can potentially
12997 classes = arch_type_to_regcm(state, cset->member->type);
12998 size = regc_max_size(state, classes);
13000 /* Find how many registers we allow this value to
13003 info = arch_reg_lhs(state, cset->member, 0);
13005 /* If the register doesn't need a register
13006 * splitting it can't help.
13008 if (info.reg == REG_UNNEEDED) {
13011 #warning "FIXME do I need a call to arch_reg_rhs around here somewhere?"
13012 if ((info.reg == REG_UNSET) || (info.reg >= MAX_REGISTERS)) {
13013 cur_size = regc_max_size(state, info.regcm);
13017 /* If this live_range feeds into conflict->ins
13018 * splitting it is unlikely to help.
13020 for(set = cset->member->use; set; set = set->next) {
13021 if (set->member == conflict->ins) {
13026 /* If there is no difference between potential and
13027 * actual register count there is nothing to do.
13029 if (cur_size >= size) {
13032 /* Of the constrained registers deal with the
13033 * most constrained one first.
13035 if (!constrained ||
13036 (size < constrained_size)) {
13037 constrained = cset->member;
13038 constrained_size = size;
13044 new = post_copy(state, constrained);
13045 new->id |= TRIPLE_FLAG_POST_SPLIT;
13050 static int split_ranges(
13051 struct compile_state *state, struct reg_state *rstate,
13052 char *used, struct live_range *range)
13054 struct triple *new;
13057 fprintf(stderr, "split_ranges %d %s %p\n",
13058 rstate->passes, tops(range->defs->def->op), range->defs->def);
13060 if ((range->color == REG_UNNEEDED) ||
13061 (rstate->passes >= rstate->max_passes)) {
13065 /* If I can't allocate a register something needs to be split */
13066 if (arch_select_free_register(state, used, range->classes) == REG_UNSET) {
13067 struct least_conflict conflict;
13070 fprintf(stderr, "find_range_conflict\n");
13072 /* Find where in the set of registers the conflict
13075 find_range_conflict(state, rstate, used, range, &conflict);
13077 /* If a range has been artifically constrained split it */
13078 new = split_constrained_range(state, rstate, used, &conflict);
13081 /* Ideally I would split the live range that will not be used
13082 * for the longest period of time in hopes that this will
13083 * (a) allow me to spill a register or
13084 * (b) allow me to place a value in another register.
13086 * So far I don't have a test case for this, the resolving
13087 * of mandatory constraints has solved all of my
13088 * know issues. So I have choosen not to write any
13089 * code until I cat get a better feel for cases where
13090 * it would be useful to have.
13093 #warning "WISHLIST implement live range splitting..."
13095 print_blocks(state, stderr);
13096 print_dominators(state, stderr);
13103 rstate->lrd[rstate->defs].orig_id = new->id;
13104 new->id = rstate->defs;
13107 fprintf(stderr, "new: %p old: %s %p\n",
13108 new, tops(RHS(new, 0)->op), RHS(new, 0));
13111 print_blocks(state, stderr);
13112 print_dominators(state, stderr);
13120 #if DEBUG_COLOR_GRAPH > 1
13121 #define cgdebug_printf(...) fprintf(stdout, __VA_ARGS__)
13122 #define cgdebug_flush() fflush(stdout)
13123 #elif DEBUG_COLOR_GRAPH == 1
13124 #define cgdebug_printf(...) fprintf(stderr, __VA_ARGS__)
13125 #define cgdebug_flush() fflush(stderr)
13127 #define cgdebug_printf(...)
13128 #define cgdebug_flush()
13132 static int select_free_color(struct compile_state *state,
13133 struct reg_state *rstate, struct live_range *range)
13135 struct triple_set *entry;
13136 struct live_range_def *lrd;
13137 struct live_range_def *phi;
13138 struct live_range_edge *edge;
13139 char used[MAX_REGISTERS];
13140 struct triple **expr;
13142 /* Instead of doing just the trivial color select here I try
13143 * a few extra things because a good color selection will help reduce
13147 /* Find the registers currently in use */
13148 memset(used, 0, sizeof(used));
13149 for(edge = range->edges; edge; edge = edge->next) {
13150 if (edge->node->color == REG_UNSET) {
13153 reg_fill_used(state, used, edge->node->color);
13155 #if DEBUG_COLOR_GRAPH > 1
13159 for(edge = range->edges; edge; edge = edge->next) {
13162 cgdebug_printf("\n%s edges: %d @%s:%d.%d\n",
13163 tops(range->def->op), i,
13164 range->def->filename, range->def->line, range->def->col);
13165 for(i = 0; i < MAX_REGISTERS; i++) {
13167 cgdebug_printf("used: %s\n",
13174 #warning "FIXME detect conflicts caused by the source and destination being the same register"
13176 /* If a color is already assigned see if it will work */
13177 if (range->color != REG_UNSET) {
13178 struct live_range_def *lrd;
13179 if (!used[range->color]) {
13182 for(edge = range->edges; edge; edge = edge->next) {
13183 if (edge->node->color != range->color) {
13186 warning(state, edge->node->defs->def, "edge: ");
13187 lrd = edge->node->defs;
13189 warning(state, lrd->def, " %p %s",
13190 lrd->def, tops(lrd->def->op));
13192 } while(lrd != edge->node->defs);
13195 warning(state, range->defs->def, "def: ");
13197 warning(state, lrd->def, " %p %s",
13198 lrd->def, tops(lrd->def->op));
13200 } while(lrd != range->defs);
13201 internal_error(state, range->defs->def,
13202 "live range with already used color %s",
13203 arch_reg_str(range->color));
13206 /* If I feed into an expression reuse it's color.
13207 * This should help remove copies in the case of 2 register instructions
13208 * and phi functions.
13211 lrd = live_range_end(state, range, 0);
13212 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
13213 entry = lrd->def->use;
13214 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
13215 struct live_range_def *insd;
13216 insd = &rstate->lrd[entry->member->id];
13217 if (insd->lr->defs == 0) {
13220 if (!phi && (insd->def->op == OP_PHI) &&
13221 !interfere(rstate, range, insd->lr)) {
13224 if ((insd->lr->color == REG_UNSET) ||
13225 ((insd->lr->classes & range->classes) == 0) ||
13226 (used[insd->lr->color])) {
13229 if (interfere(rstate, range, insd->lr)) {
13232 range->color = insd->lr->color;
13235 /* If I feed into a phi function reuse it's color or the color
13236 * of something else that feeds into the phi function.
13239 if (phi->lr->color != REG_UNSET) {
13240 if (used[phi->lr->color]) {
13241 range->color = phi->lr->color;
13245 expr = triple_rhs(state, phi->def, 0);
13246 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
13247 struct live_range *lr;
13251 lr = rstate->lrd[(*expr)->id].lr;
13252 if ((lr->color == REG_UNSET) ||
13253 ((lr->classes & range->classes) == 0) ||
13254 (used[lr->color])) {
13257 if (interfere(rstate, range, lr)) {
13260 range->color = lr->color;
13264 /* If I don't interfere with a rhs node reuse it's color */
13265 lrd = live_range_head(state, range, 0);
13266 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
13267 expr = triple_rhs(state, lrd->def, 0);
13268 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
13269 struct live_range *lr;
13273 lr = rstate->lrd[(*expr)->id].lr;
13274 if ((lr->color == -1) ||
13275 ((lr->classes & range->classes) == 0) ||
13276 (used[lr->color])) {
13279 if (interfere(rstate, range, lr)) {
13282 range->color = lr->color;
13286 /* If I have not opportunitically picked a useful color
13287 * pick the first color that is free.
13289 if (range->color == REG_UNSET) {
13291 arch_select_free_register(state, used, range->classes);
13293 if (range->color == REG_UNSET) {
13294 struct live_range_def *lrd;
13296 if (split_ranges(state, rstate, used, range)) {
13299 for(edge = range->edges; edge; edge = edge->next) {
13300 warning(state, edge->node->defs->def, "edge reg %s",
13301 arch_reg_str(edge->node->color));
13302 lrd = edge->node->defs;
13304 warning(state, lrd->def, " %s",
13305 tops(lrd->def->op));
13307 } while(lrd != edge->node->defs);
13309 warning(state, range->defs->def, "range: ");
13312 warning(state, lrd->def, " %s",
13313 tops(lrd->def->op));
13315 } while(lrd != range->defs);
13317 warning(state, range->defs->def, "classes: %x",
13319 for(i = 0; i < MAX_REGISTERS; i++) {
13321 warning(state, range->defs->def, "used: %s",
13325 #if DEBUG_COLOR_GRAPH < 2
13326 error(state, range->defs->def, "too few registers");
13328 internal_error(state, range->defs->def, "too few registers");
13331 range->classes = arch_reg_regcm(state, range->color);
13332 if (range->color == -1) {
13333 internal_error(state, range->defs->def, "select_free_color did not?");
13338 static int color_graph(struct compile_state *state, struct reg_state *rstate)
13341 struct live_range_edge *edge;
13342 struct live_range *range;
13344 cgdebug_printf("Lo: ");
13345 range = rstate->low;
13346 if (*range->group_prev != range) {
13347 internal_error(state, 0, "lo: *prev != range?");
13349 *range->group_prev = range->group_next;
13350 if (range->group_next) {
13351 range->group_next->group_prev = range->group_prev;
13353 if (&range->group_next == rstate->low_tail) {
13354 rstate->low_tail = range->group_prev;
13356 if (rstate->low == range) {
13357 internal_error(state, 0, "low: next != prev?");
13360 else if (rstate->high) {
13361 cgdebug_printf("Hi: ");
13362 range = rstate->high;
13363 if (*range->group_prev != range) {
13364 internal_error(state, 0, "hi: *prev != range?");
13366 *range->group_prev = range->group_next;
13367 if (range->group_next) {
13368 range->group_next->group_prev = range->group_prev;
13370 if (&range->group_next == rstate->high_tail) {
13371 rstate->high_tail = range->group_prev;
13373 if (rstate->high == range) {
13374 internal_error(state, 0, "high: next != prev?");
13380 cgdebug_printf(" %d\n", range - rstate->lr);
13381 range->group_prev = 0;
13382 for(edge = range->edges; edge; edge = edge->next) {
13383 struct live_range *node;
13385 /* Move nodes from the high to the low list */
13386 if (node->group_prev && (node->color == REG_UNSET) &&
13387 (node->degree == regc_max_size(state, node->classes))) {
13388 if (*node->group_prev != node) {
13389 internal_error(state, 0, "move: *prev != node?");
13391 *node->group_prev = node->group_next;
13392 if (node->group_next) {
13393 node->group_next->group_prev = node->group_prev;
13395 if (&node->group_next == rstate->high_tail) {
13396 rstate->high_tail = node->group_prev;
13398 cgdebug_printf("Moving...%d to low\n", node - rstate->lr);
13399 node->group_prev = rstate->low_tail;
13400 node->group_next = 0;
13401 *rstate->low_tail = node;
13402 rstate->low_tail = &node->group_next;
13403 if (*node->group_prev != node) {
13404 internal_error(state, 0, "move2: *prev != node?");
13409 colored = color_graph(state, rstate);
13411 cgdebug_printf("Coloring %d @%s:%d.%d:",
13412 range - rstate->lr,
13413 range->def->filename, range->def->line, range->def->col);
13415 colored = select_free_color(state, rstate, range);
13416 cgdebug_printf(" %s\n", arch_reg_str(range->color));
13421 static void verify_colors(struct compile_state *state, struct reg_state *rstate)
13423 struct live_range *lr;
13424 struct live_range_edge *edge;
13425 struct triple *ins, *first;
13426 char used[MAX_REGISTERS];
13427 first = RHS(state->main_function, 0);
13430 if (triple_is_def(state, ins)) {
13431 if ((ins->id < 0) || (ins->id > rstate->defs)) {
13432 internal_error(state, ins,
13433 "triple without a live range def");
13435 lr = rstate->lrd[ins->id].lr;
13436 if (lr->color == REG_UNSET) {
13437 internal_error(state, ins,
13438 "triple without a color");
13440 /* Find the registers used by the edges */
13441 memset(used, 0, sizeof(used));
13442 for(edge = lr->edges; edge; edge = edge->next) {
13443 if (edge->node->color == REG_UNSET) {
13444 internal_error(state, 0,
13445 "live range without a color");
13447 reg_fill_used(state, used, edge->node->color);
13449 if (used[lr->color]) {
13450 internal_error(state, ins,
13451 "triple with already used color");
13455 } while(ins != first);
13458 static void color_triples(struct compile_state *state, struct reg_state *rstate)
13460 struct live_range *lr;
13461 struct triple *first, *ins;
13462 first = RHS(state->main_function, 0);
13465 if ((ins->id < 0) || (ins->id > rstate->defs)) {
13466 internal_error(state, ins,
13467 "triple without a live range");
13469 lr = rstate->lrd[ins->id].lr;
13470 SET_REG(ins->id, lr->color);
13472 } while (ins != first);
13475 static void print_interference_block(
13476 struct compile_state *state, struct block *block, void *arg)
13479 struct reg_state *rstate = arg;
13480 struct reg_block *rb;
13481 struct triple *ptr;
13484 rb = &rstate->blocks[block->vertex];
13486 printf("\nblock: %p (%d), %p<-%p %p<-%p\n",
13490 block->left && block->left->use?block->left->use->member : 0,
13492 block->right && block->right->use?block->right->use->member : 0);
13494 struct triple_reg_set *in_set;
13496 for(in_set = rb->in; in_set; in_set = in_set->next) {
13497 printf(" %-10p", in_set->member);
13502 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13503 done = (ptr == block->last);
13504 if (ptr->op == OP_PHI) {
13511 for(edge = 0; edge < block->users; edge++) {
13512 printf(" in(%d):", edge);
13513 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13514 struct triple **slot;
13515 done = (ptr == block->last);
13516 if (ptr->op != OP_PHI) {
13519 slot = &RHS(ptr, 0);
13520 printf(" %-10p", slot[edge]);
13525 if (block->first->op == OP_LABEL) {
13526 printf("%p:\n", block->first);
13528 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13529 struct triple_set *user;
13530 struct live_range *lr;
13534 done = (ptr == block->last);
13535 lr = rstate->lrd[ptr->id].lr;
13537 if (triple_stores_block(state, ptr)) {
13538 if (ptr->u.block != block) {
13539 internal_error(state, ptr,
13540 "Wrong block pointer: %p",
13544 if (op == OP_ADECL) {
13545 for(user = ptr->use; user; user = user->next) {
13546 if (!user->member->u.block) {
13547 internal_error(state, user->member,
13548 "Use %p not in a block?",
13555 ptr->id = rstate->lrd[id].orig_id;
13556 SET_REG(ptr->id, lr->color);
13557 display_triple(stdout, ptr);
13560 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
13561 internal_error(state, ptr, "lr has no defs!");
13565 struct live_range_def *lrd;
13569 printf(" %-10p", lrd->def);
13571 } while(lrd != lr->defs);
13574 if (lr->edges > 0) {
13575 struct live_range_edge *edge;
13577 for(edge = lr->edges; edge; edge = edge->next) {
13578 struct live_range_def *lrd;
13579 lrd = edge->node->defs;
13581 printf(" %-10p", lrd->def);
13583 } while(lrd != edge->node->defs);
13588 /* Do a bunch of sanity checks */
13589 valid_ins(state, ptr);
13590 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
13591 internal_error(state, ptr, "Invalid triple id: %d",
13594 for(user = ptr->use; user; user = user->next) {
13595 struct triple *use;
13596 struct live_range *ulr;
13597 use = user->member;
13598 valid_ins(state, use);
13599 if ((use->id < 0) || (use->id > rstate->defs)) {
13600 internal_error(state, use, "Invalid triple id: %d",
13603 ulr = rstate->lrd[user->member->id].lr;
13604 if (triple_stores_block(state, user->member) &&
13605 !user->member->u.block) {
13606 internal_error(state, user->member,
13607 "Use %p not in a block?",
13613 struct triple_reg_set *out_set;
13615 for(out_set = rb->out; out_set; out_set = out_set->next) {
13616 printf(" %-10p", out_set->member);
13623 static struct live_range *merge_sort_lr(
13624 struct live_range *first, struct live_range *last)
13626 struct live_range *mid, *join, **join_tail, *pick;
13628 size = (last - first) + 1;
13630 mid = first + size/2;
13631 first = merge_sort_lr(first, mid -1);
13632 mid = merge_sort_lr(mid, last);
13636 /* merge the two lists */
13637 while(first && mid) {
13638 if ((first->degree < mid->degree) ||
13639 ((first->degree == mid->degree) &&
13640 (first->length < mid->length))) {
13642 first = first->group_next;
13644 first->group_prev = 0;
13649 mid = mid->group_next;
13651 mid->group_prev = 0;
13654 pick->group_next = 0;
13655 pick->group_prev = join_tail;
13657 join_tail = &pick->group_next;
13659 /* Splice the remaining list */
13660 pick = (first)? first : mid;
13663 pick->group_prev = join_tail;
13667 if (!first->defs) {
13675 static void ids_from_rstate(struct compile_state *state,
13676 struct reg_state *rstate)
13678 struct triple *ins, *first;
13679 if (!rstate->defs) {
13682 /* Display the graph if desired */
13683 if (state->debug & DEBUG_INTERFERENCE) {
13684 print_blocks(state, stdout);
13685 print_control_flow(state);
13687 first = RHS(state->main_function, 0);
13691 struct live_range_def *lrd;
13692 lrd = &rstate->lrd[ins->id];
13693 ins->id = lrd->orig_id;
13696 } while(ins != first);
13699 static void cleanup_live_edges(struct reg_state *rstate)
13702 /* Free the edges on each node */
13703 for(i = 1; i <= rstate->ranges; i++) {
13704 remove_live_edges(rstate, &rstate->lr[i]);
13708 static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
13710 cleanup_live_edges(rstate);
13711 xfree(rstate->lrd);
13714 /* Free the variable lifetime information */
13715 if (rstate->blocks) {
13716 free_variable_lifetimes(state, rstate->blocks);
13719 rstate->ranges = 0;
13722 rstate->blocks = 0;
13725 static void verify_consistency(struct compile_state *state);
13726 static void allocate_registers(struct compile_state *state)
13728 struct reg_state rstate;
13731 /* Clear out the reg_state */
13732 memset(&rstate, 0, sizeof(rstate));
13733 rstate.max_passes = MAX_ALLOCATION_PASSES;
13736 struct live_range **point, **next;
13741 fprintf(stderr, "pass: %d\n", rstate.passes);
13745 ids_from_rstate(state, &rstate);
13747 /* Cleanup the temporary data structures */
13748 cleanup_rstate(state, &rstate);
13750 /* Compute the variable lifetimes */
13751 rstate.blocks = compute_variable_lifetimes(state);
13753 /* Fix invalid mandatory live range coalesce conflicts */
13754 walk_variable_lifetimes(
13755 state, rstate.blocks, fix_coalesce_conflicts, 0);
13757 /* Fix two simultaneous uses of the same register.
13758 * In a few pathlogical cases a partial untangle moves
13759 * the tangle to a part of the graph we won't revisit.
13760 * So we keep looping until we have no more tangle fixes
13764 tangles = correct_tangles(state, rstate.blocks);
13767 if (state->debug & DEBUG_INSERTED_COPIES) {
13768 printf("After resolve_tangles\n");
13769 print_blocks(state, stdout);
13770 print_control_flow(state);
13772 verify_consistency(state);
13774 /* Allocate and initialize the live ranges */
13775 initialize_live_ranges(state, &rstate);
13777 /* Note current doing coalescing in a loop appears to
13778 * buys me nothing. The code is left this way in case
13779 * there is some value in it. Or if a future bugfix
13780 * yields some benefit.
13784 fprintf(stderr, "coalescing\n");
13786 /* Remove any previous live edge calculations */
13787 cleanup_live_edges(&rstate);
13789 /* Compute the interference graph */
13790 walk_variable_lifetimes(
13791 state, rstate.blocks, graph_ins, &rstate);
13793 /* Display the interference graph if desired */
13794 if (state->debug & DEBUG_INTERFERENCE) {
13795 printf("\nlive variables by block\n");
13796 walk_blocks(state, print_interference_block, &rstate);
13797 printf("\nlive variables by instruction\n");
13798 walk_variable_lifetimes(
13799 state, rstate.blocks,
13800 print_interference_ins, &rstate);
13803 coalesced = coalesce_live_ranges(state, &rstate);
13806 fprintf(stderr, "coalesced: %d\n", coalesced);
13808 } while(coalesced);
13810 #if DEBUG_CONSISTENCY > 1
13812 fprintf(stderr, "verify_graph_ins...\n");
13814 /* Verify the interference graph */
13815 walk_variable_lifetimes(
13816 state, rstate.blocks, verify_graph_ins, &rstate);
13818 fprintf(stderr, "verify_graph_ins done\n");
13822 /* Build the groups low and high. But with the nodes
13823 * first sorted by degree order.
13825 rstate.low_tail = &rstate.low;
13826 rstate.high_tail = &rstate.high;
13827 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
13829 rstate.high->group_prev = &rstate.high;
13831 for(point = &rstate.high; *point; point = &(*point)->group_next)
13833 rstate.high_tail = point;
13834 /* Walk through the high list and move everything that needs
13837 for(point = &rstate.high; *point; point = next) {
13838 struct live_range *range;
13839 next = &(*point)->group_next;
13842 /* If it has a low degree or it already has a color
13843 * place the node in low.
13845 if ((range->degree < regc_max_size(state, range->classes)) ||
13846 (range->color != REG_UNSET)) {
13847 cgdebug_printf("Lo: %5d degree %5d%s\n",
13848 range - rstate.lr, range->degree,
13849 (range->color != REG_UNSET) ? " (colored)": "");
13850 *range->group_prev = range->group_next;
13851 if (range->group_next) {
13852 range->group_next->group_prev = range->group_prev;
13854 if (&range->group_next == rstate.high_tail) {
13855 rstate.high_tail = range->group_prev;
13857 range->group_prev = rstate.low_tail;
13858 range->group_next = 0;
13859 *rstate.low_tail = range;
13860 rstate.low_tail = &range->group_next;
13864 cgdebug_printf("hi: %5d degree %5d%s\n",
13865 range - rstate.lr, range->degree,
13866 (range->color != REG_UNSET) ? " (colored)": "");
13869 /* Color the live_ranges */
13870 colored = color_graph(state, &rstate);
13872 } while (!colored);
13874 /* Verify the graph was properly colored */
13875 verify_colors(state, &rstate);
13877 /* Move the colors from the graph to the triples */
13878 color_triples(state, &rstate);
13880 /* Cleanup the temporary data structures */
13881 cleanup_rstate(state, &rstate);
13884 /* Sparce Conditional Constant Propogation
13885 * =========================================
13889 struct lattice_node {
13891 struct triple *def;
13892 struct ssa_edge *out;
13893 struct flow_block *fblock;
13894 struct triple *val;
13895 /* lattice high val && !is_const(val)
13896 * lattice const is_const(val)
13897 * lattice low val == 0
13901 struct lattice_node *src;
13902 struct lattice_node *dst;
13903 struct ssa_edge *work_next;
13904 struct ssa_edge *work_prev;
13905 struct ssa_edge *out_next;
13908 struct flow_block *src;
13909 struct flow_block *dst;
13910 struct flow_edge *work_next;
13911 struct flow_edge *work_prev;
13912 struct flow_edge *in_next;
13913 struct flow_edge *out_next;
13916 struct flow_block {
13917 struct block *block;
13918 struct flow_edge *in;
13919 struct flow_edge *out;
13920 struct flow_edge left, right;
13925 struct lattice_node *lattice;
13926 struct ssa_edge *ssa_edges;
13927 struct flow_block *flow_blocks;
13928 struct flow_edge *flow_work_list;
13929 struct ssa_edge *ssa_work_list;
13933 static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
13934 struct flow_edge *fedge)
13936 if (!scc->flow_work_list) {
13937 scc->flow_work_list = fedge;
13938 fedge->work_next = fedge->work_prev = fedge;
13941 struct flow_edge *ftail;
13942 ftail = scc->flow_work_list->work_prev;
13943 fedge->work_next = ftail->work_next;
13944 fedge->work_prev = ftail;
13945 fedge->work_next->work_prev = fedge;
13946 fedge->work_prev->work_next = fedge;
13950 static struct flow_edge *scc_next_fedge(
13951 struct compile_state *state, struct scc_state *scc)
13953 struct flow_edge *fedge;
13954 fedge = scc->flow_work_list;
13956 fedge->work_next->work_prev = fedge->work_prev;
13957 fedge->work_prev->work_next = fedge->work_next;
13958 if (fedge->work_next != fedge) {
13959 scc->flow_work_list = fedge->work_next;
13961 scc->flow_work_list = 0;
13967 static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
13968 struct ssa_edge *sedge)
13970 if (!scc->ssa_work_list) {
13971 scc->ssa_work_list = sedge;
13972 sedge->work_next = sedge->work_prev = sedge;
13975 struct ssa_edge *stail;
13976 stail = scc->ssa_work_list->work_prev;
13977 sedge->work_next = stail->work_next;
13978 sedge->work_prev = stail;
13979 sedge->work_next->work_prev = sedge;
13980 sedge->work_prev->work_next = sedge;
13984 static struct ssa_edge *scc_next_sedge(
13985 struct compile_state *state, struct scc_state *scc)
13987 struct ssa_edge *sedge;
13988 sedge = scc->ssa_work_list;
13990 sedge->work_next->work_prev = sedge->work_prev;
13991 sedge->work_prev->work_next = sedge->work_next;
13992 if (sedge->work_next != sedge) {
13993 scc->ssa_work_list = sedge->work_next;
13995 scc->ssa_work_list = 0;
14001 static void initialize_scc_state(
14002 struct compile_state *state, struct scc_state *scc)
14004 int ins_count, ssa_edge_count;
14005 int ins_index, ssa_edge_index, fblock_index;
14006 struct triple *first, *ins;
14007 struct block *block;
14008 struct flow_block *fblock;
14010 memset(scc, 0, sizeof(*scc));
14012 /* Inialize pass zero find out how much memory we need */
14013 first = RHS(state->main_function, 0);
14015 ins_count = ssa_edge_count = 0;
14017 struct triple_set *edge;
14019 for(edge = ins->use; edge; edge = edge->next) {
14023 } while(ins != first);
14025 fprintf(stderr, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
14026 ins_count, ssa_edge_count, state->last_vertex);
14028 scc->ins_count = ins_count;
14030 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
14032 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
14034 xcmalloc(sizeof(*scc->flow_blocks)*(state->last_vertex + 1),
14037 /* Initialize pass one collect up the nodes */
14040 ins_index = ssa_edge_index = fblock_index = 0;
14043 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
14044 block = ins->u.block;
14046 internal_error(state, ins, "label without block");
14049 block->vertex = fblock_index;
14050 fblock = &scc->flow_blocks[fblock_index];
14051 fblock->block = block;
14054 struct lattice_node *lnode;
14056 lnode = &scc->lattice[ins_index];
14059 lnode->fblock = fblock;
14060 lnode->val = ins; /* LATTICE HIGH */
14061 lnode->old_id = ins->id;
14062 ins->id = ins_index;
14065 } while(ins != first);
14066 /* Initialize pass two collect up the edges */
14071 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
14072 struct flow_edge *fedge, **ftail;
14073 struct block_set *bedge;
14074 block = ins->u.block;
14075 fblock = &scc->flow_blocks[block->vertex];
14078 ftail = &fblock->out;
14080 fblock->left.dst = &scc->flow_blocks[block->left->vertex];
14081 if (fblock->left.dst->block != block->left) {
14082 internal_error(state, 0, "block mismatch");
14084 fblock->left.out_next = 0;
14085 *ftail = &fblock->left;
14086 ftail = &fblock->left.out_next;
14088 if (block->right) {
14089 fblock->right.dst = &scc->flow_blocks[block->right->vertex];
14090 if (fblock->right.dst->block != block->right) {
14091 internal_error(state, 0, "block mismatch");
14093 fblock->right.out_next = 0;
14094 *ftail = &fblock->right;
14095 ftail = &fblock->right.out_next;
14097 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
14098 fedge->src = fblock;
14099 fedge->work_next = fedge->work_prev = fedge;
14100 fedge->executable = 0;
14102 ftail = &fblock->in;
14103 for(bedge = block->use; bedge; bedge = bedge->next) {
14104 struct block *src_block;
14105 struct flow_block *sfblock;
14106 struct flow_edge *sfedge;
14107 src_block = bedge->member;
14108 sfblock = &scc->flow_blocks[src_block->vertex];
14110 if (src_block->left == block) {
14111 sfedge = &sfblock->left;
14113 sfedge = &sfblock->right;
14116 ftail = &sfedge->in_next;
14117 sfedge->in_next = 0;
14121 struct triple_set *edge;
14122 struct ssa_edge **stail;
14123 struct lattice_node *lnode;
14124 lnode = &scc->lattice[ins->id];
14126 stail = &lnode->out;
14127 for(edge = ins->use; edge; edge = edge->next) {
14128 struct ssa_edge *sedge;
14129 ssa_edge_index += 1;
14130 sedge = &scc->ssa_edges[ssa_edge_index];
14132 stail = &sedge->out_next;
14133 sedge->src = lnode;
14134 sedge->dst = &scc->lattice[edge->member->id];
14135 sedge->work_next = sedge->work_prev = sedge;
14136 sedge->out_next = 0;
14140 } while(ins != first);
14141 /* Setup a dummy block 0 as a node above the start node */
14143 struct flow_block *fblock, *dst;
14144 struct flow_edge *fedge;
14145 fblock = &scc->flow_blocks[0];
14148 fblock->out = &fblock->left;
14149 dst = &scc->flow_blocks[state->first_block->vertex];
14150 fedge = &fblock->left;
14151 fedge->src = fblock;
14153 fedge->work_next = fedge;
14154 fedge->work_prev = fedge;
14155 fedge->in_next = fedge->dst->in;
14156 fedge->out_next = 0;
14157 fedge->executable = 0;
14158 fedge->dst->in = fedge;
14160 /* Initialize the work lists */
14161 scc->flow_work_list = 0;
14162 scc->ssa_work_list = 0;
14163 scc_add_fedge(state, scc, fedge);
14166 fprintf(stderr, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
14167 ins_index, ssa_edge_index, fblock_index);
14172 static void free_scc_state(
14173 struct compile_state *state, struct scc_state *scc)
14175 xfree(scc->flow_blocks);
14176 xfree(scc->ssa_edges);
14177 xfree(scc->lattice);
14181 static struct lattice_node *triple_to_lattice(
14182 struct compile_state *state, struct scc_state *scc, struct triple *ins)
14184 if (ins->id <= 0) {
14185 internal_error(state, ins, "bad id");
14187 return &scc->lattice[ins->id];
14190 static struct triple *preserve_lval(
14191 struct compile_state *state, struct lattice_node *lnode)
14193 struct triple *old;
14194 /* Preserve the original value */
14196 old = dup_triple(state, lnode->val);
14197 if (lnode->val != lnode->def) {
14207 static int lval_changed(struct compile_state *state,
14208 struct triple *old, struct lattice_node *lnode)
14211 /* See if the lattice value has changed */
14213 if (!old && !lnode->val) {
14216 if (changed && lnode->val && !is_const(lnode->val)) {
14220 lnode->val && old &&
14221 (memcmp(lnode->val->param, old->param,
14222 TRIPLE_SIZE(lnode->val->sizes) * sizeof(lnode->val->param[0])) == 0) &&
14223 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
14233 static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
14234 struct lattice_node *lnode)
14236 struct lattice_node *tmp;
14237 struct triple **slot, *old;
14238 struct flow_edge *fedge;
14240 if (lnode->def->op != OP_PHI) {
14241 internal_error(state, lnode->def, "not phi");
14243 /* Store the original value */
14244 old = preserve_lval(state, lnode);
14246 /* default to lattice high */
14247 lnode->val = lnode->def;
14248 slot = &RHS(lnode->def, 0);
14250 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
14251 if (!fedge->executable) {
14254 if (!slot[index]) {
14255 internal_error(state, lnode->def, "no phi value");
14257 tmp = triple_to_lattice(state, scc, slot[index]);
14258 /* meet(X, lattice low) = lattice low */
14262 /* meet(X, lattice high) = X */
14263 else if (!tmp->val) {
14264 lnode->val = lnode->val;
14266 /* meet(lattice high, X) = X */
14267 else if (!is_const(lnode->val)) {
14268 lnode->val = dup_triple(state, tmp->val);
14269 lnode->val->type = lnode->def->type;
14271 /* meet(const, const) = const or lattice low */
14272 else if (!constants_equal(state, lnode->val, tmp->val)) {
14280 fprintf(stderr, "phi: %d -> %s\n",
14282 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14284 /* If the lattice value has changed update the work lists. */
14285 if (lval_changed(state, old, lnode)) {
14286 struct ssa_edge *sedge;
14287 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14288 scc_add_sedge(state, scc, sedge);
14293 static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
14294 struct lattice_node *lnode)
14297 struct triple *old, *scratch;
14298 struct triple **dexpr, **vexpr;
14301 /* Store the original value */
14302 old = preserve_lval(state, lnode);
14304 /* Reinitialize the value */
14305 lnode->val = scratch = dup_triple(state, lnode->def);
14306 scratch->id = lnode->old_id;
14307 scratch->next = scratch;
14308 scratch->prev = scratch;
14311 count = TRIPLE_SIZE(scratch->sizes);
14312 for(i = 0; i < count; i++) {
14313 dexpr = &lnode->def->param[i];
14314 vexpr = &scratch->param[i];
14316 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14317 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14319 struct lattice_node *tmp;
14320 tmp = triple_to_lattice(state, scc, *dexpr);
14321 *vexpr = (tmp->val)? tmp->val : tmp->def;
14324 if (scratch->op == OP_BRANCH) {
14325 scratch->next = lnode->def->next;
14327 /* Recompute the value */
14328 #warning "FIXME see if simplify does anything bad"
14329 /* So far it looks like only the strength reduction
14330 * optimization are things I need to worry about.
14332 simplify(state, scratch);
14333 /* Cleanup my value */
14334 if (scratch->use) {
14335 internal_error(state, lnode->def, "scratch used?");
14337 if ((scratch->prev != scratch) ||
14338 ((scratch->next != scratch) &&
14339 ((lnode->def->op != OP_BRANCH) ||
14340 (scratch->next != lnode->def->next)))) {
14341 internal_error(state, lnode->def, "scratch in list?");
14343 /* undo any uses... */
14344 count = TRIPLE_SIZE(scratch->sizes);
14345 for(i = 0; i < count; i++) {
14346 vexpr = &scratch->param[i];
14348 unuse_triple(*vexpr, scratch);
14351 if (!is_const(scratch)) {
14352 for(i = 0; i < count; i++) {
14353 dexpr = &lnode->def->param[i];
14354 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14355 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14357 struct lattice_node *tmp;
14358 tmp = triple_to_lattice(state, scc, *dexpr);
14366 (lnode->val->op == lnode->def->op) &&
14367 (memcmp(lnode->val->param, lnode->def->param,
14368 count * sizeof(lnode->val->param[0])) == 0) &&
14369 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
14370 lnode->val = lnode->def;
14372 /* Find the cases that are always lattice lo */
14374 triple_is_def(state, lnode->val) &&
14375 !triple_is_pure(state, lnode->val)) {
14379 (lnode->val->op == OP_SDECL) &&
14380 (lnode->val != lnode->def)) {
14381 internal_error(state, lnode->def, "bad sdecl");
14383 /* See if the lattice value has changed */
14384 changed = lval_changed(state, old, lnode);
14385 if (lnode->val != scratch) {
14391 static void scc_visit_branch(struct compile_state *state, struct scc_state *scc,
14392 struct lattice_node *lnode)
14394 struct lattice_node *cond;
14397 struct flow_edge *fedge;
14398 fprintf(stderr, "branch: %d (",
14401 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
14402 fprintf(stderr, " %d", fedge->dst->block->vertex);
14404 fprintf(stderr, " )");
14405 if (TRIPLE_RHS(lnode->def->sizes) > 0) {
14406 fprintf(stderr, " <- %d",
14407 RHS(lnode->def, 0)->id);
14409 fprintf(stderr, "\n");
14412 if (lnode->def->op != OP_BRANCH) {
14413 internal_error(state, lnode->def, "not branch");
14415 /* This only applies to conditional branches */
14416 if (TRIPLE_RHS(lnode->def->sizes) == 0) {
14419 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
14420 if (cond->val && !is_const(cond->val)) {
14421 #warning "FIXME do I need to do something here?"
14422 warning(state, cond->def, "condition not constant?");
14425 if (cond->val == 0) {
14426 scc_add_fedge(state, scc, cond->fblock->out);
14427 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14429 else if (cond->val->u.cval) {
14430 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14433 scc_add_fedge(state, scc, cond->fblock->out);
14438 static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
14439 struct lattice_node *lnode)
14443 changed = compute_lnode_val(state, scc, lnode);
14446 struct triple **expr;
14447 fprintf(stderr, "expr: %3d %10s (",
14448 lnode->def->id, tops(lnode->def->op));
14449 expr = triple_rhs(state, lnode->def, 0);
14450 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
14452 fprintf(stderr, " %d", (*expr)->id);
14455 fprintf(stderr, " ) -> %s\n",
14456 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14459 if (lnode->def->op == OP_BRANCH) {
14460 scc_visit_branch(state, scc, lnode);
14463 else if (changed) {
14464 struct ssa_edge *sedge;
14465 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14466 scc_add_sedge(state, scc, sedge);
14471 static void scc_writeback_values(
14472 struct compile_state *state, struct scc_state *scc)
14474 struct triple *first, *ins;
14475 first = RHS(state->main_function, 0);
14478 struct lattice_node *lnode;
14479 lnode = triple_to_lattice(state, scc, ins);
14481 ins->id = lnode->old_id;
14483 if (lnode->val && !is_const(lnode->val)) {
14484 warning(state, lnode->def,
14485 "lattice node still high?");
14488 if (lnode->val && (lnode->val != ins)) {
14489 /* See if it something I know how to write back */
14490 switch(lnode->val->op) {
14492 mkconst(state, ins, lnode->val->u.cval);
14495 mkaddr_const(state, ins,
14496 MISC(lnode->val, 0), lnode->val->u.cval);
14499 /* By default don't copy the changes,
14500 * recompute them in place instead.
14502 simplify(state, ins);
14505 if (is_const(lnode->val) &&
14506 !constants_equal(state, lnode->val, ins)) {
14507 internal_error(state, 0, "constants not equal");
14509 /* Free the lattice nodes */
14514 } while(ins != first);
14517 static void scc_transform(struct compile_state *state)
14519 struct scc_state scc;
14521 initialize_scc_state(state, &scc);
14523 while(scc.flow_work_list || scc.ssa_work_list) {
14524 struct flow_edge *fedge;
14525 struct ssa_edge *sedge;
14526 struct flow_edge *fptr;
14527 while((fedge = scc_next_fedge(state, &scc))) {
14528 struct block *block;
14529 struct triple *ptr;
14530 struct flow_block *fblock;
14533 if (fedge->executable) {
14537 internal_error(state, 0, "fedge without dst");
14540 internal_error(state, 0, "fedge without src");
14542 fedge->executable = 1;
14543 fblock = fedge->dst;
14544 block = fblock->block;
14546 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14547 if (fptr->executable) {
14552 fprintf(stderr, "vertex: %d time: %d\n",
14553 block->vertex, time);
14557 for(ptr = block->first; !done; ptr = ptr->next) {
14558 struct lattice_node *lnode;
14559 done = (ptr == block->last);
14560 lnode = &scc.lattice[ptr->id];
14561 if (ptr->op == OP_PHI) {
14562 scc_visit_phi(state, &scc, lnode);
14564 else if (time == 1) {
14565 scc_visit_expr(state, &scc, lnode);
14568 if (fblock->out && !fblock->out->out_next) {
14569 scc_add_fedge(state, &scc, fblock->out);
14572 while((sedge = scc_next_sedge(state, &scc))) {
14573 struct lattice_node *lnode;
14574 struct flow_block *fblock;
14575 lnode = sedge->dst;
14576 fblock = lnode->fblock;
14578 fprintf(stderr, "sedge: %5d (%5d -> %5d)\n",
14579 sedge - scc.ssa_edges,
14580 sedge->src->def->id,
14581 sedge->dst->def->id);
14583 if (lnode->def->op == OP_PHI) {
14584 scc_visit_phi(state, &scc, lnode);
14587 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14588 if (fptr->executable) {
14593 scc_visit_expr(state, &scc, lnode);
14599 scc_writeback_values(state, &scc);
14600 free_scc_state(state, &scc);
14604 static void transform_to_arch_instructions(struct compile_state *state)
14606 struct triple *ins, *first;
14607 first = RHS(state->main_function, 0);
14610 ins = transform_to_arch_instruction(state, ins);
14611 } while(ins != first);
14614 #if DEBUG_CONSISTENCY
14615 static void verify_uses(struct compile_state *state)
14617 struct triple *first, *ins;
14618 struct triple_set *set;
14619 first = RHS(state->main_function, 0);
14622 struct triple **expr;
14623 expr = triple_rhs(state, ins, 0);
14624 for(; expr; expr = triple_rhs(state, ins, expr)) {
14625 struct triple *rhs;
14627 for(set = rhs?rhs->use:0; set; set = set->next) {
14628 if (set->member == ins) {
14633 internal_error(state, ins, "rhs not used");
14636 expr = triple_lhs(state, ins, 0);
14637 for(; expr; expr = triple_lhs(state, ins, expr)) {
14638 struct triple *lhs;
14640 for(set = lhs?lhs->use:0; set; set = set->next) {
14641 if (set->member == ins) {
14646 internal_error(state, ins, "lhs not used");
14650 } while(ins != first);
14653 static void verify_blocks(struct compile_state *state)
14655 struct triple *ins;
14656 struct block *block;
14657 block = state->first_block;
14662 for(ins = block->first; ins != block->last->next; ins = ins->next) {
14663 if (!triple_stores_block(state, ins)) {
14666 if (ins->u.block != block) {
14667 internal_error(state, ins, "inconsitent block specified");
14670 if (!triple_stores_block(state, block->last->next)) {
14671 internal_error(state, block->last->next,
14672 "cannot find next block");
14674 block = block->last->next->u.block;
14676 internal_error(state, block->last->next,
14679 } while(block != state->first_block);
14682 static void verify_domination(struct compile_state *state)
14684 struct triple *first, *ins;
14685 struct triple_set *set;
14686 if (!state->first_block) {
14690 first = RHS(state->main_function, 0);
14693 for(set = ins->use; set; set = set->next) {
14694 struct triple **expr;
14695 if (set->member->op == OP_PHI) {
14698 /* See if the use is on the righ hand side */
14699 expr = triple_rhs(state, set->member, 0);
14700 for(; expr ; expr = triple_rhs(state, set->member, expr)) {
14701 if (*expr == ins) {
14706 !tdominates(state, ins, set->member)) {
14707 internal_error(state, set->member,
14708 "non dominated rhs use?");
14712 } while(ins != first);
14715 static void verify_piece(struct compile_state *state)
14717 struct triple *first, *ins;
14718 first = RHS(state->main_function, 0);
14721 struct triple *ptr;
14723 lhs = TRIPLE_LHS(ins->sizes);
14724 if ((ins->op == OP_WRITE) || (ins->op == OP_STORE)) {
14727 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
14728 if (ptr != LHS(ins, i)) {
14729 internal_error(state, ins, "malformed lhs on %s",
14732 if (ptr->op != OP_PIECE) {
14733 internal_error(state, ins, "bad lhs op %s at %d on %s",
14734 tops(ptr->op), i, tops(ins->op));
14736 if (ptr->u.cval != i) {
14737 internal_error(state, ins, "bad u.cval of %d %d expected",
14742 } while(ins != first);
14744 static void verify_ins_colors(struct compile_state *state)
14746 struct triple *first, *ins;
14748 first = RHS(state->main_function, 0);
14752 } while(ins != first);
14754 static void verify_consistency(struct compile_state *state)
14756 verify_uses(state);
14757 verify_blocks(state);
14758 verify_domination(state);
14759 verify_piece(state);
14760 verify_ins_colors(state);
14763 static void verify_consistency(struct compile_state *state) {}
14764 #endif /* DEBUG_USES */
14766 static void optimize(struct compile_state *state)
14768 if (state->debug & DEBUG_TRIPLES) {
14769 print_triples(state);
14771 /* Replace structures with simpler data types */
14772 flatten_structures(state);
14773 if (state->debug & DEBUG_TRIPLES) {
14774 print_triples(state);
14776 verify_consistency(state);
14777 /* Analize the intermediate code */
14778 setup_basic_blocks(state);
14779 analyze_idominators(state);
14780 analyze_ipdominators(state);
14781 /* Transform the code to ssa form */
14782 transform_to_ssa_form(state);
14783 verify_consistency(state);
14784 if (state->debug & DEBUG_CODE_ELIMINATION) {
14785 fprintf(stdout, "After transform_to_ssa_form\n");
14786 print_blocks(state, stdout);
14788 /* Do strength reduction and simple constant optimizations */
14789 if (state->optimize >= 1) {
14790 simplify_all(state);
14792 verify_consistency(state);
14793 /* Propogate constants throughout the code */
14794 if (state->optimize >= 2) {
14795 #warning "FIXME fix scc_transform"
14796 scc_transform(state);
14797 transform_from_ssa_form(state);
14798 free_basic_blocks(state);
14799 setup_basic_blocks(state);
14800 analyze_idominators(state);
14801 analyze_ipdominators(state);
14802 transform_to_ssa_form(state);
14804 verify_consistency(state);
14805 #warning "WISHLIST implement single use constants (least possible register pressure)"
14806 #warning "WISHLIST implement induction variable elimination"
14807 /* Select architecture instructions and an initial partial
14808 * coloring based on architecture constraints.
14810 transform_to_arch_instructions(state);
14811 verify_consistency(state);
14812 if (state->debug & DEBUG_ARCH_CODE) {
14813 printf("After transform_to_arch_instructions\n");
14814 print_blocks(state, stdout);
14815 print_control_flow(state);
14817 eliminate_inefectual_code(state);
14818 verify_consistency(state);
14819 if (state->debug & DEBUG_CODE_ELIMINATION) {
14820 printf("After eliminate_inefectual_code\n");
14821 print_blocks(state, stdout);
14822 print_control_flow(state);
14824 verify_consistency(state);
14825 /* Color all of the variables to see if they will fit in registers */
14826 insert_copies_to_phi(state);
14827 if (state->debug & DEBUG_INSERTED_COPIES) {
14828 printf("After insert_copies_to_phi\n");
14829 print_blocks(state, stdout);
14830 print_control_flow(state);
14832 verify_consistency(state);
14833 insert_mandatory_copies(state);
14834 if (state->debug & DEBUG_INSERTED_COPIES) {
14835 printf("After insert_mandatory_copies\n");
14836 print_blocks(state, stdout);
14837 print_control_flow(state);
14839 verify_consistency(state);
14840 allocate_registers(state);
14841 verify_consistency(state);
14842 if (state->debug & DEBUG_INTERMEDIATE_CODE) {
14843 print_blocks(state, stdout);
14845 if (state->debug & DEBUG_CONTROL_FLOW) {
14846 print_control_flow(state);
14848 /* Remove the optimization information.
14849 * This is more to check for memory consistency than to free memory.
14851 free_basic_blocks(state);
14854 static void print_op_asm(struct compile_state *state,
14855 struct triple *ins, FILE *fp)
14857 struct asm_info *info;
14859 unsigned lhs, rhs, i;
14860 info = ins->u.ainfo;
14861 lhs = TRIPLE_LHS(ins->sizes);
14862 rhs = TRIPLE_RHS(ins->sizes);
14863 /* Don't count the clobbers in lhs */
14864 for(i = 0; i < lhs; i++) {
14865 if (LHS(ins, i)->type == &void_type) {
14870 fprintf(fp, "#ASM\n");
14872 for(ptr = info->str; *ptr; ptr++) {
14874 unsigned long param;
14875 struct triple *piece;
14885 param = strtoul(ptr, &next, 10);
14887 error(state, ins, "Invalid asm template");
14889 if (param >= (lhs + rhs)) {
14890 error(state, ins, "Invalid param %%%u in asm template",
14893 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
14895 arch_reg_str(ID_REG(piece->id)));
14898 fprintf(fp, "\n#NOT ASM\n");
14902 /* Only use the low x86 byte registers. This allows me
14903 * allocate the entire register when a byte register is used.
14905 #define X86_4_8BIT_GPRS 1
14907 /* Recognized x86 cpu variants */
14915 #define CPU_DEFAULT CPU_I386
14917 /* The x86 register classes */
14918 #define REGC_FLAGS 0
14919 #define REGC_GPR8 1
14920 #define REGC_GPR16 2
14921 #define REGC_GPR32 3
14922 #define REGC_GPR64 4
14925 #define REGC_GPR32_8 7
14926 #define REGC_GPR16_8 8
14927 #define REGC_IMM32 9
14928 #define REGC_IMM16 10
14929 #define REGC_IMM8 11
14930 #define LAST_REGC REGC_IMM8
14931 #if LAST_REGC >= MAX_REGC
14932 #error "MAX_REGC is to low"
14935 /* Register class masks */
14936 #define REGCM_FLAGS (1 << REGC_FLAGS)
14937 #define REGCM_GPR8 (1 << REGC_GPR8)
14938 #define REGCM_GPR16 (1 << REGC_GPR16)
14939 #define REGCM_GPR32 (1 << REGC_GPR32)
14940 #define REGCM_GPR64 (1 << REGC_GPR64)
14941 #define REGCM_MMX (1 << REGC_MMX)
14942 #define REGCM_XMM (1 << REGC_XMM)
14943 #define REGCM_GPR32_8 (1 << REGC_GPR32_8)
14944 #define REGCM_GPR16_8 (1 << REGC_GPR16_8)
14945 #define REGCM_IMM32 (1 << REGC_IMM32)
14946 #define REGCM_IMM16 (1 << REGC_IMM16)
14947 #define REGCM_IMM8 (1 << REGC_IMM8)
14948 #define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
14950 /* The x86 registers */
14951 #define REG_EFLAGS 2
14952 #define REGC_FLAGS_FIRST REG_EFLAGS
14953 #define REGC_FLAGS_LAST REG_EFLAGS
14962 #define REGC_GPR8_FIRST REG_AL
14963 #if X86_4_8BIT_GPRS
14964 #define REGC_GPR8_LAST REG_DL
14966 #define REGC_GPR8_LAST REG_DH
14976 #define REGC_GPR16_FIRST REG_AX
14977 #define REGC_GPR16_LAST REG_SP
14986 #define REGC_GPR32_FIRST REG_EAX
14987 #define REGC_GPR32_LAST REG_ESP
14988 #define REG_EDXEAX 27
14989 #define REGC_GPR64_FIRST REG_EDXEAX
14990 #define REGC_GPR64_LAST REG_EDXEAX
14991 #define REG_MMX0 28
14992 #define REG_MMX1 29
14993 #define REG_MMX2 30
14994 #define REG_MMX3 31
14995 #define REG_MMX4 32
14996 #define REG_MMX5 33
14997 #define REG_MMX6 34
14998 #define REG_MMX7 35
14999 #define REGC_MMX_FIRST REG_MMX0
15000 #define REGC_MMX_LAST REG_MMX7
15001 #define REG_XMM0 36
15002 #define REG_XMM1 37
15003 #define REG_XMM2 38
15004 #define REG_XMM3 39
15005 #define REG_XMM4 40
15006 #define REG_XMM5 41
15007 #define REG_XMM6 42
15008 #define REG_XMM7 43
15009 #define REGC_XMM_FIRST REG_XMM0
15010 #define REGC_XMM_LAST REG_XMM7
15011 #warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
15012 #define LAST_REG REG_XMM7
15014 #define REGC_GPR32_8_FIRST REG_EAX
15015 #define REGC_GPR32_8_LAST REG_EDX
15016 #define REGC_GPR16_8_FIRST REG_AX
15017 #define REGC_GPR16_8_LAST REG_DX
15019 #define REGC_IMM8_FIRST -1
15020 #define REGC_IMM8_LAST -1
15021 #define REGC_IMM16_FIRST -2
15022 #define REGC_IMM16_LAST -1
15023 #define REGC_IMM32_FIRST -4
15024 #define REGC_IMM32_LAST -1
15026 #if LAST_REG >= MAX_REGISTERS
15027 #error "MAX_REGISTERS to low"
15031 static unsigned regc_size[LAST_REGC +1] = {
15032 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
15033 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
15034 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
15035 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
15036 [REGC_GPR64] = REGC_GPR64_LAST - REGC_GPR64_FIRST + 1,
15037 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
15038 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
15039 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
15040 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
15046 static const struct {
15048 } regcm_bound[LAST_REGC + 1] = {
15049 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
15050 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
15051 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
15052 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
15053 [REGC_GPR64] = { REGC_GPR64_FIRST, REGC_GPR64_LAST },
15054 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
15055 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
15056 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
15057 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
15058 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
15059 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
15060 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
15063 static int arch_encode_cpu(const char *cpu)
15069 { "i386", CPU_I386 },
15077 for(ptr = cpus; ptr->name; ptr++) {
15078 if (strcmp(ptr->name, cpu) == 0) {
15085 static unsigned arch_regc_size(struct compile_state *state, int class)
15087 if ((class < 0) || (class > LAST_REGC)) {
15090 return regc_size[class];
15092 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
15094 /* See if two register classes may have overlapping registers */
15095 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 |
15096 REGCM_GPR32_8 | REGCM_GPR32 | REGCM_GPR64;
15098 /* Special case for the immediates */
15099 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
15100 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
15101 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
15102 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
15105 return (regcm1 & regcm2) ||
15106 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
15109 static void arch_reg_equivs(
15110 struct compile_state *state, unsigned *equiv, int reg)
15112 if ((reg < 0) || (reg > LAST_REG)) {
15113 internal_error(state, 0, "invalid register");
15118 #if X86_4_8BIT_GPRS
15122 *equiv++ = REG_EAX;
15123 *equiv++ = REG_EDXEAX;
15126 #if X86_4_8BIT_GPRS
15130 *equiv++ = REG_EAX;
15131 *equiv++ = REG_EDXEAX;
15134 #if X86_4_8BIT_GPRS
15138 *equiv++ = REG_EBX;
15142 #if X86_4_8BIT_GPRS
15146 *equiv++ = REG_EBX;
15149 #if X86_4_8BIT_GPRS
15153 *equiv++ = REG_ECX;
15157 #if X86_4_8BIT_GPRS
15161 *equiv++ = REG_ECX;
15164 #if X86_4_8BIT_GPRS
15168 *equiv++ = REG_EDX;
15169 *equiv++ = REG_EDXEAX;
15172 #if X86_4_8BIT_GPRS
15176 *equiv++ = REG_EDX;
15177 *equiv++ = REG_EDXEAX;
15182 *equiv++ = REG_EAX;
15183 *equiv++ = REG_EDXEAX;
15188 *equiv++ = REG_EBX;
15193 *equiv++ = REG_ECX;
15198 *equiv++ = REG_EDX;
15199 *equiv++ = REG_EDXEAX;
15202 *equiv++ = REG_ESI;
15205 *equiv++ = REG_EDI;
15208 *equiv++ = REG_EBP;
15211 *equiv++ = REG_ESP;
15217 *equiv++ = REG_EDXEAX;
15233 *equiv++ = REG_EDXEAX;
15254 *equiv++ = REG_EAX;
15255 *equiv++ = REG_EDX;
15258 *equiv++ = REG_UNSET;
15261 static unsigned arch_avail_mask(struct compile_state *state)
15263 unsigned avail_mask;
15264 avail_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 |
15265 REGCM_GPR32 | REGCM_GPR32_8 | REGCM_GPR64 |
15266 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
15267 switch(state->cpu) {
15270 avail_mask |= REGCM_MMX;
15274 avail_mask |= REGCM_MMX | REGCM_XMM;
15278 /* Don't enable 8 bit values until I can force both operands
15279 * to be 8bits simultaneously.
15281 avail_mask &= ~(REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16);
15286 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
15288 unsigned mask, result;
15291 result &= arch_avail_mask(state);
15293 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
15294 if ((result & mask) == 0) {
15297 if (class > LAST_REGC) {
15300 for(class2 = 0; class2 <= LAST_REGC; class2++) {
15301 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
15302 (regcm_bound[class2].last <= regcm_bound[class].last)) {
15303 result |= (1 << class2);
15310 static unsigned arch_reg_regcm(struct compile_state *state, int reg)
15315 for(class = 0; class <= LAST_REGC; class++) {
15316 if ((reg >= regcm_bound[class].first) &&
15317 (reg <= regcm_bound[class].last)) {
15318 mask |= (1 << class);
15322 internal_error(state, 0, "reg %d not in any class", reg);
15327 static struct reg_info arch_reg_constraint(
15328 struct compile_state *state, struct type *type, const char *constraint)
15330 static const struct {
15334 } constraints[] = {
15335 { 'r', REGCM_GPR32, REG_UNSET },
15336 { 'g', REGCM_GPR32, REG_UNSET },
15337 { 'p', REGCM_GPR32, REG_UNSET },
15338 { 'q', REGCM_GPR8, REG_UNSET },
15339 { 'Q', REGCM_GPR32_8, REG_UNSET },
15340 { 'x', REGCM_XMM, REG_UNSET },
15341 { 'y', REGCM_MMX, REG_UNSET },
15342 { 'a', REGCM_GPR32, REG_EAX },
15343 { 'b', REGCM_GPR32, REG_EBX },
15344 { 'c', REGCM_GPR32, REG_ECX },
15345 { 'd', REGCM_GPR32, REG_EDX },
15346 { 'D', REGCM_GPR32, REG_EDI },
15347 { 'S', REGCM_GPR32, REG_ESI },
15348 { '\0', 0, REG_UNSET },
15350 unsigned int regcm;
15351 unsigned int mask, reg;
15352 struct reg_info result;
15354 regcm = arch_type_to_regcm(state, type);
15357 for(ptr = constraint; *ptr; ptr++) {
15362 for(i = 0; constraints[i].class != '\0'; i++) {
15363 if (constraints[i].class == *ptr) {
15367 if (constraints[i].class == '\0') {
15368 error(state, 0, "invalid register constraint ``%c''", *ptr);
15371 if ((constraints[i].mask & regcm) == 0) {
15372 error(state, 0, "invalid register class %c specified",
15375 mask |= constraints[i].mask;
15376 if (constraints[i].reg != REG_UNSET) {
15377 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
15378 error(state, 0, "Only one register may be specified");
15380 reg = constraints[i].reg;
15384 result.regcm = mask;
15388 static struct reg_info arch_reg_clobber(
15389 struct compile_state *state, const char *clobber)
15391 struct reg_info result;
15392 if (strcmp(clobber, "memory") == 0) {
15393 result.reg = REG_UNSET;
15396 else if (strcmp(clobber, "%eax") == 0) {
15397 result.reg = REG_EAX;
15398 result.regcm = REGCM_GPR32;
15400 else if (strcmp(clobber, "%ebx") == 0) {
15401 result.reg = REG_EBX;
15402 result.regcm = REGCM_GPR32;
15404 else if (strcmp(clobber, "%ecx") == 0) {
15405 result.reg = REG_ECX;
15406 result.regcm = REGCM_GPR32;
15408 else if (strcmp(clobber, "%edx") == 0) {
15409 result.reg = REG_EDX;
15410 result.regcm = REGCM_GPR32;
15412 else if (strcmp(clobber, "%esi") == 0) {
15413 result.reg = REG_ESI;
15414 result.regcm = REGCM_GPR32;
15416 else if (strcmp(clobber, "%edi") == 0) {
15417 result.reg = REG_EDI;
15418 result.regcm = REGCM_GPR32;
15420 else if (strcmp(clobber, "%ebp") == 0) {
15421 result.reg = REG_EBP;
15422 result.regcm = REGCM_GPR32;
15424 else if (strcmp(clobber, "%esp") == 0) {
15425 result.reg = REG_ESP;
15426 result.regcm = REGCM_GPR32;
15428 else if (strcmp(clobber, "cc") == 0) {
15429 result.reg = REG_EFLAGS;
15430 result.regcm = REGCM_FLAGS;
15432 else if ((strncmp(clobber, "xmm", 3) == 0) &&
15433 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15434 result.reg = REG_XMM0 + octdigval(clobber[3]);
15435 result.regcm = REGCM_XMM;
15437 else if ((strncmp(clobber, "mmx", 3) == 0) &&
15438 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15439 result.reg = REG_MMX0 + octdigval(clobber[3]);
15440 result.regcm = REGCM_MMX;
15443 error(state, 0, "Invalid register clobber");
15444 result.reg = REG_UNSET;
15450 static int do_select_reg(struct compile_state *state,
15451 char *used, int reg, unsigned classes)
15457 mask = arch_reg_regcm(state, reg);
15458 return (classes & mask) ? reg : REG_UNSET;
15461 static int arch_select_free_register(
15462 struct compile_state *state, char *used, int classes)
15464 /* Preference: flags, 8bit gprs, 32bit gprs, other 32bit reg
15465 * other types of registers.
15469 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
15470 reg = do_select_reg(state, used, i, classes);
15472 for(i = REGC_GPR32_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR32_LAST); i++) {
15473 reg = do_select_reg(state, used, i, classes);
15475 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
15476 reg = do_select_reg(state, used, i, classes);
15478 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
15479 reg = do_select_reg(state, used, i, classes);
15481 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
15482 reg = do_select_reg(state, used, i, classes);
15484 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
15485 reg = do_select_reg(state, used, i, classes);
15487 for(i = REGC_GPR64_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR64_LAST); i++) {
15488 reg = do_select_reg(state, used, i, classes);
15494 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
15496 #warning "FIXME force types smaller (if legal) before I get here"
15497 unsigned avail_mask;
15500 avail_mask = arch_avail_mask(state);
15501 switch(type->type & TYPE_MASK) {
15508 mask = REGCM_GPR8 |
15509 REGCM_GPR16 | REGCM_GPR16_8 |
15510 REGCM_GPR32 | REGCM_GPR32_8 |
15512 REGCM_MMX | REGCM_XMM |
15513 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
15517 mask = REGCM_GPR16 | REGCM_GPR16_8 |
15518 REGCM_GPR32 | REGCM_GPR32_8 |
15520 REGCM_MMX | REGCM_XMM |
15521 REGCM_IMM32 | REGCM_IMM16;
15528 mask = REGCM_GPR32 | REGCM_GPR32_8 |
15529 REGCM_GPR64 | REGCM_MMX | REGCM_XMM |
15533 internal_error(state, 0, "no register class for type");
15536 mask &= avail_mask;
15540 static int is_imm32(struct triple *imm)
15542 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
15543 (imm->op == OP_ADDRCONST);
15546 static int is_imm16(struct triple *imm)
15548 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
15550 static int is_imm8(struct triple *imm)
15552 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
15555 static int get_imm32(struct triple *ins, struct triple **expr)
15557 struct triple *imm;
15559 while(imm->op == OP_COPY) {
15562 if (!is_imm32(imm)) {
15565 unuse_triple(*expr, ins);
15566 use_triple(imm, ins);
15571 static int get_imm8(struct triple *ins, struct triple **expr)
15573 struct triple *imm;
15575 while(imm->op == OP_COPY) {
15578 if (!is_imm8(imm)) {
15581 unuse_triple(*expr, ins);
15582 use_triple(imm, ins);
15587 #define TEMPLATE_NOP 0
15588 #define TEMPLATE_INTCONST8 1
15589 #define TEMPLATE_INTCONST32 2
15590 #define TEMPLATE_COPY_REG 3
15591 #define TEMPLATE_COPY_IMM32 4
15592 #define TEMPLATE_COPY_IMM16 5
15593 #define TEMPLATE_COPY_IMM8 6
15594 #define TEMPLATE_PHI 7
15595 #define TEMPLATE_STORE8 8
15596 #define TEMPLATE_STORE16 9
15597 #define TEMPLATE_STORE32 10
15598 #define TEMPLATE_LOAD8 11
15599 #define TEMPLATE_LOAD16 12
15600 #define TEMPLATE_LOAD32 13
15601 #define TEMPLATE_BINARY_REG 14
15602 #define TEMPLATE_BINARY_IMM 15
15603 #define TEMPLATE_SL_CL 16
15604 #define TEMPLATE_SL_IMM 17
15605 #define TEMPLATE_UNARY 18
15606 #define TEMPLATE_CMP_REG 19
15607 #define TEMPLATE_CMP_IMM 20
15608 #define TEMPLATE_TEST 21
15609 #define TEMPLATE_SET 22
15610 #define TEMPLATE_JMP 23
15611 #define TEMPLATE_INB_DX 24
15612 #define TEMPLATE_INB_IMM 25
15613 #define TEMPLATE_INW_DX 26
15614 #define TEMPLATE_INW_IMM 27
15615 #define TEMPLATE_INL_DX 28
15616 #define TEMPLATE_INL_IMM 29
15617 #define TEMPLATE_OUTB_DX 30
15618 #define TEMPLATE_OUTB_IMM 31
15619 #define TEMPLATE_OUTW_DX 32
15620 #define TEMPLATE_OUTW_IMM 33
15621 #define TEMPLATE_OUTL_DX 34
15622 #define TEMPLATE_OUTL_IMM 35
15623 #define TEMPLATE_BSF 36
15624 #define TEMPLATE_RDMSR 37
15625 #define TEMPLATE_WRMSR 38
15626 #define LAST_TEMPLATE TEMPLATE_WRMSR
15627 #if LAST_TEMPLATE >= MAX_TEMPLATES
15628 #error "MAX_TEMPLATES to low"
15631 #define COPY_REGCM (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8 | REGCM_MMX | REGCM_XMM)
15632 #define COPY32_REGCM (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
15634 static struct ins_template templates[] = {
15635 [TEMPLATE_NOP] = {},
15636 [TEMPLATE_INTCONST8] = {
15637 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15639 [TEMPLATE_INTCONST32] = {
15640 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
15642 [TEMPLATE_COPY_REG] = {
15643 .lhs = { [0] = { REG_UNSET, COPY_REGCM } },
15644 .rhs = { [0] = { REG_UNSET, COPY_REGCM } },
15646 [TEMPLATE_COPY_IMM32] = {
15647 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
15648 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
15650 [TEMPLATE_COPY_IMM16] = {
15651 .lhs = { [0] = { REG_UNSET, COPY32_REGCM | REGCM_GPR16 } },
15652 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 } },
15654 [TEMPLATE_COPY_IMM8] = {
15655 .lhs = { [0] = { REG_UNSET, COPY_REGCM } },
15656 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15659 .lhs = { [0] = { REG_VIRT0, COPY_REGCM } },
15661 [ 0] = { REG_VIRT0, COPY_REGCM },
15662 [ 1] = { REG_VIRT0, COPY_REGCM },
15663 [ 2] = { REG_VIRT0, COPY_REGCM },
15664 [ 3] = { REG_VIRT0, COPY_REGCM },
15665 [ 4] = { REG_VIRT0, COPY_REGCM },
15666 [ 5] = { REG_VIRT0, COPY_REGCM },
15667 [ 6] = { REG_VIRT0, COPY_REGCM },
15668 [ 7] = { REG_VIRT0, COPY_REGCM },
15669 [ 8] = { REG_VIRT0, COPY_REGCM },
15670 [ 9] = { REG_VIRT0, COPY_REGCM },
15671 [10] = { REG_VIRT0, COPY_REGCM },
15672 [11] = { REG_VIRT0, COPY_REGCM },
15673 [12] = { REG_VIRT0, COPY_REGCM },
15674 [13] = { REG_VIRT0, COPY_REGCM },
15675 [14] = { REG_VIRT0, COPY_REGCM },
15676 [15] = { REG_VIRT0, COPY_REGCM },
15678 [TEMPLATE_STORE8] = {
15679 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15680 .rhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15682 [TEMPLATE_STORE16] = {
15683 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15684 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
15686 [TEMPLATE_STORE32] = {
15687 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15688 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15690 [TEMPLATE_LOAD8] = {
15691 .lhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15692 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15694 [TEMPLATE_LOAD16] = {
15695 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
15696 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15698 [TEMPLATE_LOAD32] = {
15699 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15700 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15702 [TEMPLATE_BINARY_REG] = {
15703 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15705 [0] = { REG_VIRT0, REGCM_GPR32 },
15706 [1] = { REG_UNSET, REGCM_GPR32 },
15709 [TEMPLATE_BINARY_IMM] = {
15710 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15712 [0] = { REG_VIRT0, REGCM_GPR32 },
15713 [1] = { REG_UNNEEDED, REGCM_IMM32 },
15716 [TEMPLATE_SL_CL] = {
15717 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15719 [0] = { REG_VIRT0, REGCM_GPR32 },
15720 [1] = { REG_CL, REGCM_GPR8 },
15723 [TEMPLATE_SL_IMM] = {
15724 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15726 [0] = { REG_VIRT0, REGCM_GPR32 },
15727 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15730 [TEMPLATE_UNARY] = {
15731 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15732 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15734 [TEMPLATE_CMP_REG] = {
15735 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15737 [0] = { REG_UNSET, REGCM_GPR32 },
15738 [1] = { REG_UNSET, REGCM_GPR32 },
15741 [TEMPLATE_CMP_IMM] = {
15742 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15744 [0] = { REG_UNSET, REGCM_GPR32 },
15745 [1] = { REG_UNNEEDED, REGCM_IMM32 },
15748 [TEMPLATE_TEST] = {
15749 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15750 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15753 .lhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15754 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15757 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15759 [TEMPLATE_INB_DX] = {
15760 .lhs = { [0] = { REG_AL, REGCM_GPR8 } },
15761 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15763 [TEMPLATE_INB_IMM] = {
15764 .lhs = { [0] = { REG_AL, REGCM_GPR8 } },
15765 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15767 [TEMPLATE_INW_DX] = {
15768 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
15769 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15771 [TEMPLATE_INW_IMM] = {
15772 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
15773 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15775 [TEMPLATE_INL_DX] = {
15776 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
15777 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15779 [TEMPLATE_INL_IMM] = {
15780 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
15781 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15783 [TEMPLATE_OUTB_DX] = {
15785 [0] = { REG_AL, REGCM_GPR8 },
15786 [1] = { REG_DX, REGCM_GPR16 },
15789 [TEMPLATE_OUTB_IMM] = {
15791 [0] = { REG_AL, REGCM_GPR8 },
15792 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15795 [TEMPLATE_OUTW_DX] = {
15797 [0] = { REG_AX, REGCM_GPR16 },
15798 [1] = { REG_DX, REGCM_GPR16 },
15801 [TEMPLATE_OUTW_IMM] = {
15803 [0] = { REG_AX, REGCM_GPR16 },
15804 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15807 [TEMPLATE_OUTL_DX] = {
15809 [0] = { REG_EAX, REGCM_GPR32 },
15810 [1] = { REG_DX, REGCM_GPR16 },
15813 [TEMPLATE_OUTL_IMM] = {
15815 [0] = { REG_EAX, REGCM_GPR32 },
15816 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15820 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15821 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15823 [TEMPLATE_RDMSR] = {
15825 [0] = { REG_EAX, REGCM_GPR32 },
15826 [1] = { REG_EDX, REGCM_GPR32 },
15828 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
15830 [TEMPLATE_WRMSR] = {
15832 [0] = { REG_ECX, REGCM_GPR32 },
15833 [1] = { REG_EAX, REGCM_GPR32 },
15834 [2] = { REG_EDX, REGCM_GPR32 },
15839 static void fixup_branches(struct compile_state *state,
15840 struct triple *cmp, struct triple *use, int jmp_op)
15842 struct triple_set *entry, *next;
15843 for(entry = use->use; entry; entry = next) {
15844 next = entry->next;
15845 if (entry->member->op == OP_COPY) {
15846 fixup_branches(state, cmp, entry->member, jmp_op);
15848 else if (entry->member->op == OP_BRANCH) {
15849 struct triple *branch, *test;
15850 struct triple *left, *right;
15852 left = RHS(cmp, 0);
15853 if (TRIPLE_RHS(cmp->sizes) > 1) {
15854 right = RHS(cmp, 1);
15856 branch = entry->member;
15857 test = pre_triple(state, branch,
15858 cmp->op, cmp->type, left, right);
15859 test->template_id = TEMPLATE_TEST;
15860 if (cmp->op == OP_CMP) {
15861 test->template_id = TEMPLATE_CMP_REG;
15862 if (get_imm32(test, &RHS(test, 1))) {
15863 test->template_id = TEMPLATE_CMP_IMM;
15866 use_triple(RHS(test, 0), test);
15867 use_triple(RHS(test, 1), test);
15868 unuse_triple(RHS(branch, 0), branch);
15869 RHS(branch, 0) = test;
15870 branch->op = jmp_op;
15871 branch->template_id = TEMPLATE_JMP;
15872 use_triple(RHS(branch, 0), branch);
15877 static void bool_cmp(struct compile_state *state,
15878 struct triple *ins, int cmp_op, int jmp_op, int set_op)
15880 struct triple_set *entry, *next;
15881 struct triple *set;
15883 /* Put a barrier up before the cmp which preceeds the
15884 * copy instruction. If a set actually occurs this gives
15885 * us a chance to move variables in registers out of the way.
15888 /* Modify the comparison operator */
15890 ins->template_id = TEMPLATE_TEST;
15891 if (cmp_op == OP_CMP) {
15892 ins->template_id = TEMPLATE_CMP_REG;
15893 if (get_imm32(ins, &RHS(ins, 1))) {
15894 ins->template_id = TEMPLATE_CMP_IMM;
15897 /* Generate the instruction sequence that will transform the
15898 * result of the comparison into a logical value.
15900 set = post_triple(state, ins, set_op, ins->type, ins, 0);
15901 use_triple(ins, set);
15902 set->template_id = TEMPLATE_SET;
15904 for(entry = ins->use; entry; entry = next) {
15905 next = entry->next;
15906 if (entry->member == set) {
15909 replace_rhs_use(state, ins, set, entry->member);
15911 fixup_branches(state, ins, set, jmp_op);
15914 static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
15916 struct triple *next;
15918 lhs = TRIPLE_LHS(ins->sizes);
15919 for(next = ins->next, i = 0; i < lhs; i++, next = next->next) {
15920 if (next != LHS(ins, i)) {
15921 internal_error(state, ins, "malformed lhs on %s",
15924 if (next->op != OP_PIECE) {
15925 internal_error(state, ins, "bad lhs op %s at %d on %s",
15926 tops(next->op), i, tops(ins->op));
15928 if (next->u.cval != i) {
15929 internal_error(state, ins, "bad u.cval of %d %d expected",
15936 struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
15938 struct ins_template *template;
15939 struct reg_info result;
15941 if (ins->op == OP_PIECE) {
15942 index = ins->u.cval;
15943 ins = MISC(ins, 0);
15945 zlhs = TRIPLE_LHS(ins->sizes);
15946 if (triple_is_def(state, ins)) {
15949 if (index >= zlhs) {
15950 internal_error(state, ins, "index %d out of range for %s\n",
15951 index, tops(ins->op));
15955 template = &ins->u.ainfo->tmpl;
15958 if (ins->template_id > LAST_TEMPLATE) {
15959 internal_error(state, ins, "bad template number %d",
15962 template = &templates[ins->template_id];
15965 result = template->lhs[index];
15966 result.regcm = arch_regcm_normalize(state, result.regcm);
15967 if (result.reg != REG_UNNEEDED) {
15968 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
15970 if (result.regcm == 0) {
15971 internal_error(state, ins, "lhs %d regcm == 0", index);
15976 struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
15978 struct reg_info result;
15979 struct ins_template *template;
15980 if ((index > TRIPLE_RHS(ins->sizes)) ||
15981 (ins->op == OP_PIECE)) {
15982 internal_error(state, ins, "index %d out of range for %s\n",
15983 index, tops(ins->op));
15987 template = &ins->u.ainfo->tmpl;
15990 if (ins->template_id > LAST_TEMPLATE) {
15991 internal_error(state, ins, "bad template number %d",
15994 template = &templates[ins->template_id];
15997 result = template->rhs[index];
15998 result.regcm = arch_regcm_normalize(state, result.regcm);
15999 if (result.regcm == 0) {
16000 internal_error(state, ins, "rhs %d regcm == 0", index);
16005 static struct triple *transform_to_arch_instruction(
16006 struct compile_state *state, struct triple *ins)
16008 /* Transform from generic 3 address instructions
16009 * to archtecture specific instructions.
16010 * And apply architecture specific constrains to instructions.
16011 * Copies are inserted to preserve the register flexibility
16012 * of 3 address instructions.
16014 struct triple *next;
16018 ins->template_id = TEMPLATE_INTCONST32;
16019 if (ins->u.cval < 256) {
16020 ins->template_id = TEMPLATE_INTCONST8;
16024 ins->template_id = TEMPLATE_INTCONST32;
16030 ins->template_id = TEMPLATE_NOP;
16033 ins->template_id = TEMPLATE_COPY_REG;
16034 if (is_imm8(RHS(ins, 0))) {
16035 ins->template_id = TEMPLATE_COPY_IMM8;
16037 else if (is_imm16(RHS(ins, 0))) {
16038 ins->template_id = TEMPLATE_COPY_IMM16;
16040 else if (is_imm32(RHS(ins, 0))) {
16041 ins->template_id = TEMPLATE_COPY_IMM32;
16043 else if (is_const(RHS(ins, 0))) {
16044 internal_error(state, ins, "bad constant passed to copy");
16048 ins->template_id = TEMPLATE_PHI;
16051 switch(ins->type->type & TYPE_MASK) {
16052 case TYPE_CHAR: case TYPE_UCHAR:
16053 ins->template_id = TEMPLATE_STORE8;
16055 case TYPE_SHORT: case TYPE_USHORT:
16056 ins->template_id = TEMPLATE_STORE16;
16058 case TYPE_INT: case TYPE_UINT:
16059 case TYPE_LONG: case TYPE_ULONG:
16061 ins->template_id = TEMPLATE_STORE32;
16064 internal_error(state, ins, "unknown type in store");
16069 switch(ins->type->type & TYPE_MASK) {
16070 case TYPE_CHAR: case TYPE_UCHAR:
16071 ins->template_id = TEMPLATE_LOAD8;
16075 ins->template_id = TEMPLATE_LOAD16;
16082 ins->template_id = TEMPLATE_LOAD32;
16085 internal_error(state, ins, "unknown type in load");
16095 ins->template_id = TEMPLATE_BINARY_REG;
16096 if (get_imm32(ins, &RHS(ins, 1))) {
16097 ins->template_id = TEMPLATE_BINARY_IMM;
16103 ins->template_id = TEMPLATE_SL_CL;
16104 if (get_imm8(ins, &RHS(ins, 1))) {
16105 ins->template_id = TEMPLATE_SL_IMM;
16110 ins->template_id = TEMPLATE_UNARY;
16113 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
16116 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
16119 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
16122 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
16125 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
16128 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
16131 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
16134 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
16137 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
16140 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
16143 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
16146 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
16149 if (TRIPLE_RHS(ins->sizes) > 0) {
16150 internal_error(state, ins, "bad branch test");
16153 ins->template_id = TEMPLATE_NOP;
16159 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
16160 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
16161 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
16163 if (get_imm8(ins, &RHS(ins, 0))) {
16164 ins->template_id += 1;
16171 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
16172 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
16173 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
16175 if (get_imm8(ins, &RHS(ins, 1))) {
16176 ins->template_id += 1;
16181 ins->template_id = TEMPLATE_BSF;
16184 ins->template_id = TEMPLATE_RDMSR;
16185 next = after_lhs(state, ins);
16188 ins->template_id = TEMPLATE_WRMSR;
16191 ins->template_id = TEMPLATE_NOP;
16194 ins->template_id = TEMPLATE_NOP;
16195 next = after_lhs(state, ins);
16197 /* Already transformed instructions */
16199 ins->template_id = TEMPLATE_TEST;
16202 ins->template_id = TEMPLATE_CMP_REG;
16203 if (get_imm32(ins, &RHS(ins, 1))) {
16204 ins->template_id = TEMPLATE_CMP_IMM;
16207 case OP_JMP_EQ: case OP_JMP_NOTEQ:
16208 case OP_JMP_SLESS: case OP_JMP_ULESS:
16209 case OP_JMP_SMORE: case OP_JMP_UMORE:
16210 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
16211 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
16212 ins->template_id = TEMPLATE_JMP;
16214 case OP_SET_EQ: case OP_SET_NOTEQ:
16215 case OP_SET_SLESS: case OP_SET_ULESS:
16216 case OP_SET_SMORE: case OP_SET_UMORE:
16217 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
16218 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
16219 ins->template_id = TEMPLATE_SET;
16221 /* Unhandled instructions */
16224 internal_error(state, ins, "unhandled ins: %d %s\n",
16225 ins->op, tops(ins->op));
16231 static void generate_local_labels(struct compile_state *state)
16233 struct triple *first, *label;
16236 first = RHS(state->main_function, 0);
16239 if ((label->op == OP_LABEL) ||
16240 (label->op == OP_SDECL)) {
16242 label->u.cval = ++label_counter;
16248 label = label->next;
16249 } while(label != first);
16252 static int check_reg(struct compile_state *state,
16253 struct triple *triple, int classes)
16257 reg = ID_REG(triple->id);
16258 if (reg == REG_UNSET) {
16259 internal_error(state, triple, "register not set");
16261 mask = arch_reg_regcm(state, reg);
16262 if (!(classes & mask)) {
16263 internal_error(state, triple, "reg %d in wrong class",
16269 static const char *arch_reg_str(int reg)
16271 static const char *regs[] = {
16275 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
16276 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
16277 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
16279 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
16280 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
16281 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
16283 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
16290 static const char *reg(struct compile_state *state, struct triple *triple,
16294 reg = check_reg(state, triple, classes);
16295 return arch_reg_str(reg);
16298 const char *type_suffix(struct compile_state *state, struct type *type)
16300 const char *suffix;
16301 switch(size_of(state, type)) {
16302 case 1: suffix = "b"; break;
16303 case 2: suffix = "w"; break;
16304 case 4: suffix = "l"; break;
16306 internal_error(state, 0, "unknown suffix");
16313 static void print_const_val(
16314 struct compile_state *state, struct triple *ins, FILE *fp)
16318 fprintf(fp, " $%ld ",
16319 (long_t)(ins->u.cval));
16322 fprintf(fp, " $L%s%lu+%lu ",
16323 state->label_prefix,
16324 MISC(ins, 0)->u.cval,
16328 internal_error(state, ins, "unknown constant type");
16333 static void print_binary_op(struct compile_state *state,
16334 const char *op, struct triple *ins, FILE *fp)
16337 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16338 if (RHS(ins, 0)->id != ins->id) {
16339 internal_error(state, ins, "invalid register assignment");
16341 if (is_const(RHS(ins, 1))) {
16342 fprintf(fp, "\t%s ", op);
16343 print_const_val(state, RHS(ins, 1), fp);
16344 fprintf(fp, ", %s\n",
16345 reg(state, RHS(ins, 0), mask));
16348 unsigned lmask, rmask;
16350 lreg = check_reg(state, RHS(ins, 0), mask);
16351 rreg = check_reg(state, RHS(ins, 1), mask);
16352 lmask = arch_reg_regcm(state, lreg);
16353 rmask = arch_reg_regcm(state, rreg);
16354 mask = lmask & rmask;
16355 fprintf(fp, "\t%s %s, %s\n",
16357 reg(state, RHS(ins, 1), mask),
16358 reg(state, RHS(ins, 0), mask));
16361 static void print_unary_op(struct compile_state *state,
16362 const char *op, struct triple *ins, FILE *fp)
16365 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16366 fprintf(fp, "\t%s %s\n",
16368 reg(state, RHS(ins, 0), mask));
16371 static void print_op_shift(struct compile_state *state,
16372 const char *op, struct triple *ins, FILE *fp)
16375 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16376 if (RHS(ins, 0)->id != ins->id) {
16377 internal_error(state, ins, "invalid register assignment");
16379 if (is_const(RHS(ins, 1))) {
16380 fprintf(fp, "\t%s ", op);
16381 print_const_val(state, RHS(ins, 1), fp);
16382 fprintf(fp, ", %s\n",
16383 reg(state, RHS(ins, 0), mask));
16386 fprintf(fp, "\t%s %s, %s\n",
16388 reg(state, RHS(ins, 1), REGCM_GPR8),
16389 reg(state, RHS(ins, 0), mask));
16393 static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
16400 case OP_INB: op = "inb", mask = REGCM_GPR8; break;
16401 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
16402 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
16404 internal_error(state, ins, "not an in operation");
16408 dreg = check_reg(state, ins, mask);
16409 if (!reg_is_reg(state, dreg, REG_EAX)) {
16410 internal_error(state, ins, "dst != %%eax");
16412 if (is_const(RHS(ins, 0))) {
16413 fprintf(fp, "\t%s ", op);
16414 print_const_val(state, RHS(ins, 0), fp);
16415 fprintf(fp, ", %s\n",
16416 reg(state, ins, mask));
16420 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
16421 if (!reg_is_reg(state, addr_reg, REG_DX)) {
16422 internal_error(state, ins, "src != %%dx");
16424 fprintf(fp, "\t%s %s, %s\n",
16426 reg(state, RHS(ins, 0), REGCM_GPR16),
16427 reg(state, ins, mask));
16431 static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
16438 case OP_OUTB: op = "outb", mask = REGCM_GPR8; break;
16439 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
16440 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
16442 internal_error(state, ins, "not an out operation");
16446 lreg = check_reg(state, RHS(ins, 0), mask);
16447 if (!reg_is_reg(state, lreg, REG_EAX)) {
16448 internal_error(state, ins, "src != %%eax");
16450 if (is_const(RHS(ins, 1))) {
16451 fprintf(fp, "\t%s %s,",
16452 op, reg(state, RHS(ins, 0), mask));
16453 print_const_val(state, RHS(ins, 1), fp);
16458 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
16459 if (!reg_is_reg(state, addr_reg, REG_DX)) {
16460 internal_error(state, ins, "dst != %%dx");
16462 fprintf(fp, "\t%s %s, %s\n",
16464 reg(state, RHS(ins, 0), mask),
16465 reg(state, RHS(ins, 1), REGCM_GPR16));
16469 static void print_op_move(struct compile_state *state,
16470 struct triple *ins, FILE *fp)
16472 /* op_move is complex because there are many types
16473 * of registers we can move between.
16474 * Because OP_COPY will be introduced in arbitrary locations
16475 * OP_COPY must not affect flags.
16477 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
16478 struct triple *dst, *src;
16479 if (ins->op == OP_COPY) {
16483 else if (ins->op == OP_WRITE) {
16488 internal_error(state, ins, "unknown move operation");
16491 if (!is_const(src)) {
16492 int src_reg, dst_reg;
16493 int src_regcm, dst_regcm;
16494 src_reg = ID_REG(src->id);
16495 dst_reg = ID_REG(dst->id);
16496 src_regcm = arch_reg_regcm(state, src_reg);
16497 dst_regcm = arch_reg_regcm(state, dst_reg);
16498 /* If the class is the same just move the register */
16499 if (src_regcm & dst_regcm &
16500 (REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32)) {
16501 if ((src_reg != dst_reg) || !omit_copy) {
16502 fprintf(fp, "\tmov %s, %s\n",
16503 reg(state, src, src_regcm),
16504 reg(state, dst, dst_regcm));
16507 /* Move 32bit to 16bit */
16508 else if ((src_regcm & REGCM_GPR32) &&
16509 (dst_regcm & REGCM_GPR16)) {
16510 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
16511 if ((src_reg != dst_reg) || !omit_copy) {
16512 fprintf(fp, "\tmovw %s, %s\n",
16513 arch_reg_str(src_reg),
16514 arch_reg_str(dst_reg));
16517 /* Move 32bit to 8bit */
16518 else if ((src_regcm & REGCM_GPR32_8) &&
16519 (dst_regcm & REGCM_GPR8))
16521 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
16522 if ((src_reg != dst_reg) || !omit_copy) {
16523 fprintf(fp, "\tmovb %s, %s\n",
16524 arch_reg_str(src_reg),
16525 arch_reg_str(dst_reg));
16528 /* Move 16bit to 8bit */
16529 else if ((src_regcm & REGCM_GPR16_8) &&
16530 (dst_regcm & REGCM_GPR8))
16532 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
16533 if ((src_reg != dst_reg) || !omit_copy) {
16534 fprintf(fp, "\tmovb %s, %s\n",
16535 arch_reg_str(src_reg),
16536 arch_reg_str(dst_reg));
16539 /* Move 8/16bit to 16/32bit */
16540 else if ((src_regcm & (REGCM_GPR8 | REGCM_GPR16)) &&
16541 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
16543 op = is_signed(src->type)? "movsx": "movzx";
16544 fprintf(fp, "\t%s %s, %s\n",
16546 reg(state, src, src_regcm),
16547 reg(state, dst, dst_regcm));
16549 /* Move between sse registers */
16550 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
16551 if ((src_reg != dst_reg) || !omit_copy) {
16552 fprintf(fp, "\tmovdqa %s, %s\n",
16553 reg(state, src, src_regcm),
16554 reg(state, dst, dst_regcm));
16557 /* Move between mmx registers or mmx & sse registers */
16558 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
16559 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
16560 if ((src_reg != dst_reg) || !omit_copy) {
16561 fprintf(fp, "\tmovq %s, %s\n",
16562 reg(state, src, src_regcm),
16563 reg(state, dst, dst_regcm));
16566 /* Move between 32bit gprs & mmx/sse registers */
16567 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
16568 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
16569 fprintf(fp, "\tmovd %s, %s\n",
16570 reg(state, src, src_regcm),
16571 reg(state, dst, dst_regcm));
16573 #if X86_4_8BIT_GPRS
16574 /* Move from 8bit gprs to mmx/sse registers */
16575 else if ((src_regcm & REGCM_GPR8) && (src_reg <= REG_DL) &&
16576 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
16579 op = is_signed(src->type)? "movsx":"movzx";
16580 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16581 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
16583 reg(state, src, src_regcm),
16584 arch_reg_str(mid_reg),
16585 arch_reg_str(mid_reg),
16586 reg(state, dst, dst_regcm));
16588 /* Move from mmx/sse registers and 8bit gprs */
16589 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
16590 (dst_regcm & REGCM_GPR8) && (dst_reg <= REG_DL)) {
16592 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16593 fprintf(fp, "\tmovd %s, %s\n",
16594 reg(state, src, src_regcm),
16595 arch_reg_str(mid_reg));
16597 /* Move from 32bit gprs to 16bit gprs */
16598 else if ((src_regcm & REGCM_GPR32) &&
16599 (dst_regcm & REGCM_GPR16)) {
16600 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
16601 if ((src_reg != dst_reg) || !omit_copy) {
16602 fprintf(fp, "\tmov %s, %s\n",
16603 arch_reg_str(src_reg),
16604 arch_reg_str(dst_reg));
16607 /* Move from 32bit gprs to 8bit gprs */
16608 else if ((src_regcm & REGCM_GPR32) &&
16609 (dst_regcm & REGCM_GPR8)) {
16610 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16611 if ((src_reg != dst_reg) || !omit_copy) {
16612 fprintf(fp, "\tmov %s, %s\n",
16613 arch_reg_str(src_reg),
16614 arch_reg_str(dst_reg));
16617 /* Move from 16bit gprs to 8bit gprs */
16618 else if ((src_regcm & REGCM_GPR16) &&
16619 (dst_regcm & REGCM_GPR8)) {
16620 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
16621 if ((src_reg != dst_reg) || !omit_copy) {
16622 fprintf(fp, "\tmov %s, %s\n",
16623 arch_reg_str(src_reg),
16624 arch_reg_str(dst_reg));
16627 #endif /* X86_4_8BIT_GPRS */
16629 internal_error(state, ins, "unknown copy type");
16633 fprintf(fp, "\tmov ");
16634 print_const_val(state, src, fp);
16635 fprintf(fp, ", %s\n",
16636 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8));
16640 static void print_op_load(struct compile_state *state,
16641 struct triple *ins, FILE *fp)
16643 struct triple *dst, *src;
16646 if (is_const(src) || is_const(dst)) {
16647 internal_error(state, ins, "unknown load operation");
16649 fprintf(fp, "\tmov (%s), %s\n",
16650 reg(state, src, REGCM_GPR32),
16651 reg(state, dst, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32));
16655 static void print_op_store(struct compile_state *state,
16656 struct triple *ins, FILE *fp)
16658 struct triple *dst, *src;
16661 if (is_const(src) && (src->op == OP_INTCONST)) {
16663 value = (long_t)(src->u.cval);
16664 fprintf(fp, "\tmov%s $%ld, (%s)\n",
16665 type_suffix(state, src->type),
16667 reg(state, dst, REGCM_GPR32));
16669 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
16670 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
16671 type_suffix(state, src->type),
16672 reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
16676 if (is_const(src) || is_const(dst)) {
16677 internal_error(state, ins, "unknown store operation");
16679 fprintf(fp, "\tmov%s %s, (%s)\n",
16680 type_suffix(state, src->type),
16681 reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
16682 reg(state, dst, REGCM_GPR32));
16688 static void print_op_smul(struct compile_state *state,
16689 struct triple *ins, FILE *fp)
16691 if (!is_const(RHS(ins, 1))) {
16692 fprintf(fp, "\timul %s, %s\n",
16693 reg(state, RHS(ins, 1), REGCM_GPR32),
16694 reg(state, RHS(ins, 0), REGCM_GPR32));
16697 fprintf(fp, "\timul ");
16698 print_const_val(state, RHS(ins, 1), fp);
16699 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
16703 static void print_op_cmp(struct compile_state *state,
16704 struct triple *ins, FILE *fp)
16708 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16709 dreg = check_reg(state, ins, REGCM_FLAGS);
16710 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
16711 internal_error(state, ins, "bad dest register for cmp");
16713 if (is_const(RHS(ins, 1))) {
16714 fprintf(fp, "\tcmp ");
16715 print_const_val(state, RHS(ins, 1), fp);
16716 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
16719 unsigned lmask, rmask;
16721 lreg = check_reg(state, RHS(ins, 0), mask);
16722 rreg = check_reg(state, RHS(ins, 1), mask);
16723 lmask = arch_reg_regcm(state, lreg);
16724 rmask = arch_reg_regcm(state, rreg);
16725 mask = lmask & rmask;
16726 fprintf(fp, "\tcmp %s, %s\n",
16727 reg(state, RHS(ins, 1), mask),
16728 reg(state, RHS(ins, 0), mask));
16732 static void print_op_test(struct compile_state *state,
16733 struct triple *ins, FILE *fp)
16736 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16737 fprintf(fp, "\ttest %s, %s\n",
16738 reg(state, RHS(ins, 0), mask),
16739 reg(state, RHS(ins, 0), mask));
16742 static void print_op_branch(struct compile_state *state,
16743 struct triple *branch, FILE *fp)
16745 const char *bop = "j";
16746 if (branch->op == OP_JMP) {
16747 if (TRIPLE_RHS(branch->sizes) != 0) {
16748 internal_error(state, branch, "jmp with condition?");
16753 struct triple *ptr;
16754 if (TRIPLE_RHS(branch->sizes) != 1) {
16755 internal_error(state, branch, "jmpcc without condition?");
16757 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
16758 if ((RHS(branch, 0)->op != OP_CMP) &&
16759 (RHS(branch, 0)->op != OP_TEST)) {
16760 internal_error(state, branch, "bad branch test");
16762 #warning "FIXME I have observed instructions between the test and branch instructions"
16763 ptr = RHS(branch, 0);
16764 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
16765 if (ptr->op != OP_COPY) {
16766 internal_error(state, branch, "branch does not follow test");
16769 switch(branch->op) {
16770 case OP_JMP_EQ: bop = "jz"; break;
16771 case OP_JMP_NOTEQ: bop = "jnz"; break;
16772 case OP_JMP_SLESS: bop = "jl"; break;
16773 case OP_JMP_ULESS: bop = "jb"; break;
16774 case OP_JMP_SMORE: bop = "jg"; break;
16775 case OP_JMP_UMORE: bop = "ja"; break;
16776 case OP_JMP_SLESSEQ: bop = "jle"; break;
16777 case OP_JMP_ULESSEQ: bop = "jbe"; break;
16778 case OP_JMP_SMOREEQ: bop = "jge"; break;
16779 case OP_JMP_UMOREEQ: bop = "jae"; break;
16781 internal_error(state, branch, "Invalid branch op");
16786 fprintf(fp, "\t%s L%s%lu\n",
16788 state->label_prefix,
16789 TARG(branch, 0)->u.cval);
16792 static void print_op_set(struct compile_state *state,
16793 struct triple *set, FILE *fp)
16795 const char *sop = "set";
16796 if (TRIPLE_RHS(set->sizes) != 1) {
16797 internal_error(state, set, "setcc without condition?");
16799 check_reg(state, RHS(set, 0), REGCM_FLAGS);
16800 if ((RHS(set, 0)->op != OP_CMP) &&
16801 (RHS(set, 0)->op != OP_TEST)) {
16802 internal_error(state, set, "bad set test");
16804 if (RHS(set, 0)->next != set) {
16805 internal_error(state, set, "set does not follow test");
16808 case OP_SET_EQ: sop = "setz"; break;
16809 case OP_SET_NOTEQ: sop = "setnz"; break;
16810 case OP_SET_SLESS: sop = "setl"; break;
16811 case OP_SET_ULESS: sop = "setb"; break;
16812 case OP_SET_SMORE: sop = "setg"; break;
16813 case OP_SET_UMORE: sop = "seta"; break;
16814 case OP_SET_SLESSEQ: sop = "setle"; break;
16815 case OP_SET_ULESSEQ: sop = "setbe"; break;
16816 case OP_SET_SMOREEQ: sop = "setge"; break;
16817 case OP_SET_UMOREEQ: sop = "setae"; break;
16819 internal_error(state, set, "Invalid set op");
16822 fprintf(fp, "\t%s %s\n",
16823 sop, reg(state, set, REGCM_GPR8));
16826 static void print_op_bit_scan(struct compile_state *state,
16827 struct triple *ins, FILE *fp)
16831 case OP_BSF: op = "bsf"; break;
16832 case OP_BSR: op = "bsr"; break;
16834 internal_error(state, ins, "unknown bit scan");
16844 reg(state, RHS(ins, 0), REGCM_GPR32),
16845 reg(state, ins, REGCM_GPR32),
16846 reg(state, ins, REGCM_GPR32));
16849 static void print_const(struct compile_state *state,
16850 struct triple *ins, FILE *fp)
16854 switch(ins->type->type & TYPE_MASK) {
16857 fprintf(fp, ".byte 0x%02lx\n", ins->u.cval);
16861 fprintf(fp, ".short 0x%04lx\n", ins->u.cval);
16867 fprintf(fp, ".int %lu\n", ins->u.cval);
16870 internal_error(state, ins, "Unknown constant type");
16875 unsigned char *blob;
16877 size = size_of(state, ins->type);
16878 blob = ins->u.blob;
16879 for(i = 0; i < size; i++) {
16880 fprintf(fp, ".byte 0x%02x\n",
16886 internal_error(state, ins, "Unknown constant type");
16891 #define TEXT_SECTION ".rom.text"
16892 #define DATA_SECTION ".rom.data"
16894 static void print_sdecl(struct compile_state *state,
16895 struct triple *ins, FILE *fp)
16897 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
16898 fprintf(fp, ".balign %d\n", align_of(state, ins->type));
16899 fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
16900 print_const(state, MISC(ins, 0), fp);
16901 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
16905 static void print_instruction(struct compile_state *state,
16906 struct triple *ins, FILE *fp)
16908 /* Assumption: after I have exted the register allocator
16909 * everything is in a valid register.
16913 print_op_asm(state, ins, fp);
16915 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
16916 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
16917 case OP_AND: print_binary_op(state, "and", ins, fp); break;
16918 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
16919 case OP_OR: print_binary_op(state, "or", ins, fp); break;
16920 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
16921 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
16922 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
16923 case OP_POS: break;
16924 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
16925 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
16929 /* Don't generate anything here for constants */
16931 /* Don't generate anything for variable declarations. */
16934 print_sdecl(state, ins, fp);
16938 print_op_move(state, ins, fp);
16941 print_op_load(state, ins, fp);
16944 print_op_store(state, ins, fp);
16947 print_op_smul(state, ins, fp);
16949 case OP_CMP: print_op_cmp(state, ins, fp); break;
16950 case OP_TEST: print_op_test(state, ins, fp); break;
16952 case OP_JMP_EQ: case OP_JMP_NOTEQ:
16953 case OP_JMP_SLESS: case OP_JMP_ULESS:
16954 case OP_JMP_SMORE: case OP_JMP_UMORE:
16955 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
16956 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
16957 print_op_branch(state, ins, fp);
16959 case OP_SET_EQ: case OP_SET_NOTEQ:
16960 case OP_SET_SLESS: case OP_SET_ULESS:
16961 case OP_SET_SMORE: case OP_SET_UMORE:
16962 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
16963 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
16964 print_op_set(state, ins, fp);
16966 case OP_INB: case OP_INW: case OP_INL:
16967 print_op_in(state, ins, fp);
16969 case OP_OUTB: case OP_OUTW: case OP_OUTL:
16970 print_op_out(state, ins, fp);
16974 print_op_bit_scan(state, ins, fp);
16977 after_lhs(state, ins);
16978 fprintf(fp, "\trdmsr\n");
16981 fprintf(fp, "\twrmsr\n");
16984 fprintf(fp, "\thlt\n");
16990 fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
16992 /* Ignore OP_PIECE */
16995 /* Operations I am not yet certain how to handle */
16997 case OP_SDIV: case OP_UDIV:
16998 case OP_SMOD: case OP_UMOD:
16999 /* Operations that should never get here */
17000 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
17001 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
17002 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
17004 internal_error(state, ins, "unknown op: %d %s",
17005 ins->op, tops(ins->op));
17010 static void print_instructions(struct compile_state *state)
17012 struct triple *first, *ins;
17013 int print_location;
17014 struct occurance *last_occurance;
17016 print_location = 1;
17017 last_occurance = 0;
17018 fp = state->output;
17019 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
17020 first = RHS(state->main_function, 0);
17023 if (print_location &&
17024 last_occurance != ins->occurance) {
17025 if (!ins->occurance->parent) {
17026 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
17027 ins->occurance->function,
17028 ins->occurance->filename,
17029 ins->occurance->line,
17030 ins->occurance->col);
17033 struct occurance *ptr;
17034 fprintf(fp, "\t/*\n");
17035 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
17036 fprintf(fp, "\t * %s,%s:%d.%d\n",
17042 fprintf(fp, "\t */\n");
17045 if (last_occurance) {
17046 put_occurance(last_occurance);
17048 get_occurance(ins->occurance);
17049 last_occurance = ins->occurance;
17052 print_instruction(state, ins, fp);
17054 } while(ins != first);
17057 static void generate_code(struct compile_state *state)
17059 generate_local_labels(state);
17060 print_instructions(state);
17064 static void print_tokens(struct compile_state *state)
17067 tk = &state->token[0];
17072 next_token(state, 0);
17074 loc(stdout, state, 0);
17075 printf("%s <- `%s'\n",
17077 tk->ident ? tk->ident->name :
17078 tk->str_len ? tk->val.str : "");
17080 } while(tk->tok != TOK_EOF);
17083 static void compile(const char *filename, const char *ofilename,
17084 int cpu, int debug, int opt, const char *label_prefix)
17087 struct compile_state state;
17088 memset(&state, 0, sizeof(state));
17090 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
17091 memset(&state.token[i], 0, sizeof(state.token[i]));
17092 state.token[i].tok = -1;
17094 /* Remember the debug settings */
17096 state.debug = debug;
17097 state.optimize = opt;
17098 /* Remember the output filename */
17099 state.ofilename = ofilename;
17100 state.output = fopen(state.ofilename, "w");
17101 if (!state.output) {
17102 error(&state, 0, "Cannot open output file %s\n",
17105 /* Remember the label prefix */
17106 state.label_prefix = label_prefix;
17107 /* Prep the preprocessor */
17108 state.if_depth = 0;
17109 state.if_value = 0;
17110 /* register the C keywords */
17111 register_keywords(&state);
17112 /* register the keywords the macro preprocessor knows */
17113 register_macro_keywords(&state);
17114 /* Memorize where some special keywords are. */
17115 state.i_continue = lookup(&state, "continue", 8);
17116 state.i_break = lookup(&state, "break", 5);
17117 /* Enter the globl definition scope */
17118 start_scope(&state);
17119 register_builtins(&state);
17120 compile_file(&state, filename, 1);
17122 print_tokens(&state);
17125 /* Exit the global definition scope */
17128 /* Now that basic compilation has happened
17129 * optimize the intermediate code
17133 generate_code(&state);
17135 fprintf(stderr, "done\n");
17139 static void version(void)
17141 printf("romcc " VERSION " released " RELEASE_DATE "\n");
17144 static void usage(void)
17148 "Usage: romcc <source>.c\n"
17149 "Compile a C source file without using ram\n"
17153 static void arg_error(char *fmt, ...)
17156 va_start(args, fmt);
17157 vfprintf(stderr, fmt, args);
17163 int main(int argc, char **argv)
17165 const char *filename;
17166 const char *ofilename;
17167 const char *label_prefix;
17174 ofilename = "auto.inc";
17178 while((argc > 1) && (argc != last_argc)) {
17180 if (strncmp(argv[1], "--debug=", 8) == 0) {
17181 debug = atoi(argv[1] + 8);
17185 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
17186 label_prefix= argv[1] + 15;
17190 else if ((strcmp(argv[1],"-O") == 0) ||
17191 (strcmp(argv[1], "-O1") == 0)) {
17196 else if (strcmp(argv[1],"-O2") == 0) {
17201 else if ((strcmp(argv[1], "-o") == 0) && (argc > 2)) {
17202 ofilename = argv[2];
17206 else if (strncmp(argv[1], "-mcpu=", 6) == 0) {
17207 cpu = arch_encode_cpu(argv[1] + 6);
17208 if (cpu == BAD_CPU) {
17209 arg_error("Invalid cpu specified: %s\n",
17217 arg_error("Wrong argument count %d\n", argc);
17219 filename = argv[1];
17220 compile(filename, ofilename, cpu, debug, optimize, label_prefix);