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;
755 struct file_state *file;
756 struct occurance *last_occurance;
757 const char *function;
758 struct token token[4];
759 struct hash_entry *hash_table[HASH_TABLE_SIZE];
760 struct hash_entry *i_continue;
761 struct hash_entry *i_break;
763 int if_depth, if_value;
765 struct file_state *macro_file;
766 struct triple *main_function;
767 struct block *first_block, *last_block;
774 /* visibility global/local */
775 /* static/auto duration */
776 /* typedef, register, inline */
778 #define STOR_MASK 0x000f
780 #define STOR_GLOBAL 0x0001
782 #define STOR_PERM 0x0002
783 /* Storage specifiers */
784 #define STOR_AUTO 0x0000
785 #define STOR_STATIC 0x0002
786 #define STOR_EXTERN 0x0003
787 #define STOR_REGISTER 0x0004
788 #define STOR_TYPEDEF 0x0008
789 #define STOR_INLINE 0x000c
792 #define QUAL_MASK 0x0070
793 #define QUAL_NONE 0x0000
794 #define QUAL_CONST 0x0010
795 #define QUAL_VOLATILE 0x0020
796 #define QUAL_RESTRICT 0x0040
799 #define TYPE_MASK 0x1f00
800 #define TYPE_INTEGER(TYPE) (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG))
801 #define TYPE_ARITHMETIC(TYPE) (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE))
802 #define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
803 #define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
804 #define TYPE_MKUNSIGNED(TYPE) ((TYPE) | 0x0100)
805 #define TYPE_RANK(TYPE) ((TYPE) & ~0x0100)
806 #define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER)
807 #define TYPE_DEFAULT 0x0000
808 #define TYPE_VOID 0x0100
809 #define TYPE_CHAR 0x0200
810 #define TYPE_UCHAR 0x0300
811 #define TYPE_SHORT 0x0400
812 #define TYPE_USHORT 0x0500
813 #define TYPE_INT 0x0600
814 #define TYPE_UINT 0x0700
815 #define TYPE_LONG 0x0800
816 #define TYPE_ULONG 0x0900
817 #define TYPE_LLONG 0x0a00 /* long long */
818 #define TYPE_ULLONG 0x0b00
819 #define TYPE_FLOAT 0x0c00
820 #define TYPE_DOUBLE 0x0d00
821 #define TYPE_LDOUBLE 0x0e00 /* long double */
822 #define TYPE_STRUCT 0x1000
823 #define TYPE_ENUM 0x1100
824 #define TYPE_POINTER 0x1200
826 * type->left holds the type pointed to.
828 #define TYPE_FUNCTION 0x1300
829 /* For TYPE_FUNCTION:
830 * type->left holds the return type.
831 * type->right holds the...
833 #define TYPE_PRODUCT 0x1400
834 /* TYPE_PRODUCT is a basic building block when defining structures
835 * type->left holds the type that appears first in memory.
836 * type->right holds the type that appears next in memory.
838 #define TYPE_OVERLAP 0x1500
839 /* TYPE_OVERLAP is a basic building block when defining unions
840 * type->left and type->right holds to types that overlap
841 * each other in memory.
843 #define TYPE_ARRAY 0x1600
844 /* TYPE_ARRAY is a basic building block when definitng arrays.
845 * type->left holds the type we are an array of.
846 * type-> holds the number of elements.
849 #define ELEMENT_COUNT_UNSPECIFIED (~0UL)
853 struct type *left, *right;
855 struct hash_entry *field_ident;
856 struct hash_entry *type_ident;
859 #define MAX_REGISTERS 75
860 #define MAX_REG_EQUIVS 16
861 #define REGISTER_BITS 16
862 #define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
863 #define TEMPLATE_BITS 6
864 #define MAX_TEMPLATES (1<<TEMPLATE_BITS)
867 #define REG_UNNEEDED 1
868 #define REG_VIRT0 (MAX_REGISTERS + 0)
869 #define REG_VIRT1 (MAX_REGISTERS + 1)
870 #define REG_VIRT2 (MAX_REGISTERS + 2)
871 #define REG_VIRT3 (MAX_REGISTERS + 3)
872 #define REG_VIRT4 (MAX_REGISTERS + 4)
873 #define REG_VIRT5 (MAX_REGISTERS + 5)
874 #define REG_VIRT6 (MAX_REGISTERS + 5)
875 #define REG_VIRT7 (MAX_REGISTERS + 5)
876 #define REG_VIRT8 (MAX_REGISTERS + 5)
877 #define REG_VIRT9 (MAX_REGISTERS + 5)
879 /* Provision for 8 register classes */
881 #define REGC_SHIFT REGISTER_BITS
882 #define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
883 #define REG_MASK (MAX_VIRT_REGISTERS -1)
884 #define ID_REG(ID) ((ID) & REG_MASK)
885 #define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
886 #define ID_REGCM(ID) (((ID) & REGC_MASK) >> REGC_SHIFT)
887 #define SET_REGCM(ID, REGCM) ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
888 #define SET_INFO(ID, INFO) ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
889 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
891 static unsigned arch_reg_regcm(struct compile_state *state, int reg);
892 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
893 static void arch_reg_equivs(
894 struct compile_state *state, unsigned *equiv, int reg);
895 static int arch_select_free_register(
896 struct compile_state *state, char *used, int classes);
897 static unsigned arch_regc_size(struct compile_state *state, int class);
898 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
899 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
900 static const char *arch_reg_str(int reg);
901 static struct reg_info arch_reg_constraint(
902 struct compile_state *state, struct type *type, const char *constraint);
903 static struct reg_info arch_reg_clobber(
904 struct compile_state *state, const char *clobber);
905 static struct reg_info arch_reg_lhs(struct compile_state *state,
906 struct triple *ins, int index);
907 static struct reg_info arch_reg_rhs(struct compile_state *state,
908 struct triple *ins, int index);
909 static struct triple *transform_to_arch_instruction(
910 struct compile_state *state, struct triple *ins);
914 #define DEBUG_ABORT_ON_ERROR 0x0001
915 #define DEBUG_INTERMEDIATE_CODE 0x0002
916 #define DEBUG_CONTROL_FLOW 0x0004
917 #define DEBUG_BASIC_BLOCKS 0x0008
918 #define DEBUG_FDOMINATORS 0x0010
919 #define DEBUG_RDOMINATORS 0x0020
920 #define DEBUG_TRIPLES 0x0040
921 #define DEBUG_INTERFERENCE 0x0080
922 #define DEBUG_ARCH_CODE 0x0100
923 #define DEBUG_CODE_ELIMINATION 0x0200
924 #define DEBUG_INSERTED_COPIES 0x0400
926 #define GLOBAL_SCOPE_DEPTH 1
927 #define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
929 static void compile_file(struct compile_state *old_state, const char *filename, int local);
931 static void do_cleanup(struct compile_state *state)
934 fclose(state->output);
935 unlink(state->ofilename);
939 static int get_col(struct file_state *file)
943 ptr = file->line_start;
945 for(col = 0; ptr < end; ptr++) {
950 col = (col & ~7) + 8;
956 static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
960 fprintf(fp, "%s:%d.%d: ",
961 triple->occurance->filename,
962 triple->occurance->line,
963 triple->occurance->col);
969 col = get_col(state->file);
970 fprintf(fp, "%s:%d.%d: ",
971 state->file->report_name, state->file->report_line, col);
974 static void __internal_error(struct compile_state *state, struct triple *ptr,
979 loc(stderr, state, ptr);
981 fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
983 fprintf(stderr, "Internal compiler error: ");
984 vfprintf(stderr, fmt, args);
985 fprintf(stderr, "\n");
992 static void __internal_warning(struct compile_state *state, struct triple *ptr,
997 loc(stderr, state, ptr);
998 fprintf(stderr, "Internal compiler warning: ");
999 vfprintf(stderr, fmt, args);
1000 fprintf(stderr, "\n");
1006 static void __error(struct compile_state *state, struct triple *ptr,
1010 va_start(args, fmt);
1011 loc(stderr, state, ptr);
1012 vfprintf(stderr, fmt, args);
1014 fprintf(stderr, "\n");
1016 if (state->debug & DEBUG_ABORT_ON_ERROR) {
1022 static void __warning(struct compile_state *state, struct triple *ptr,
1026 va_start(args, fmt);
1027 loc(stderr, state, ptr);
1028 fprintf(stderr, "warning: ");
1029 vfprintf(stderr, fmt, args);
1030 fprintf(stderr, "\n");
1034 #if DEBUG_ERROR_MESSAGES
1035 # define internal_error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_error
1036 # define internal_warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_warning
1037 # define error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__error
1038 # define warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__warning
1040 # define internal_error __internal_error
1041 # define internal_warning __internal_warning
1042 # define error __error
1043 # define warning __warning
1045 #define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1047 static void valid_op(struct compile_state *state, int op)
1049 char *fmt = "invalid op: %d";
1051 internal_error(state, 0, fmt, op);
1054 internal_error(state, 0, fmt, op);
1058 static void valid_ins(struct compile_state *state, struct triple *ptr)
1060 valid_op(state, ptr->op);
1063 static void process_trigraphs(struct compile_state *state)
1065 char *src, *dest, *end;
1066 struct file_state *file;
1068 src = dest = file->buf;
1069 end = file->buf + file->size;
1070 while((end - src) >= 3) {
1071 if ((src[0] == '?') && (src[1] == '?')) {
1074 case '=': c = '#'; break;
1075 case '/': c = '\\'; break;
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;
1099 file->size = dest - file->buf;
1102 static void splice_lines(struct compile_state *state)
1104 char *src, *dest, *end;
1105 struct file_state *file;
1107 src = dest = file->buf;
1108 end = file->buf + file->size;
1109 while((end - src) >= 2) {
1110 if ((src[0] == '\\') && (src[1] == '\n')) {
1120 file->size = dest - file->buf;
1123 static struct type void_type;
1124 static void use_triple(struct triple *used, struct triple *user)
1126 struct triple_set **ptr, *new;
1133 if ((*ptr)->member == user) {
1136 ptr = &(*ptr)->next;
1138 /* Append new to the head of the list,
1139 * copy_func and rename_block_variables
1142 new = xcmalloc(sizeof(*new), "triple_set");
1144 new->next = used->use;
1148 static void unuse_triple(struct triple *used, struct triple *unuser)
1150 struct triple_set *use, **ptr;
1157 if (use->member == unuser) {
1167 static void push_triple(struct triple *used, struct triple *user)
1169 struct triple_set *new;
1174 /* Append new to the head of the list,
1175 * it's the only sensible behavoir for a stack.
1177 new = xcmalloc(sizeof(*new), "triple_set");
1179 new->next = used->use;
1183 static void pop_triple(struct triple *used, struct triple *unuser)
1185 struct triple_set *use, **ptr;
1189 if (use->member == unuser) {
1192 /* Only free one occurance from the stack */
1201 static void put_occurance(struct occurance *occurance)
1203 occurance->count -= 1;
1204 if (occurance->count <= 0) {
1205 if (occurance->parent) {
1206 put_occurance(occurance->parent);
1212 static void get_occurance(struct occurance *occurance)
1214 occurance->count += 1;
1218 static struct occurance *new_occurance(struct compile_state *state)
1220 struct occurance *result, *last;
1221 const char *filename;
1222 const char *function;
1230 filename = state->file->report_name;
1231 line = state->file->report_line;
1232 col = get_col(state->file);
1234 if (state->function) {
1235 function = state->function;
1237 last = state->last_occurance;
1239 (last->col == col) &&
1240 (last->line == line) &&
1241 (last->function == function) &&
1242 (strcmp(last->filename, filename) == 0)) {
1243 get_occurance(last);
1247 state->last_occurance = 0;
1248 put_occurance(last);
1250 result = xmalloc(sizeof(*result), "occurance");
1252 result->filename = filename;
1253 result->function = function;
1254 result->line = line;
1257 state->last_occurance = result;
1261 static struct occurance *inline_occurance(struct compile_state *state,
1262 struct occurance *new, struct occurance *orig)
1264 struct occurance *result, *last;
1265 last = state->last_occurance;
1267 (last->parent == orig) &&
1268 (last->col == new->col) &&
1269 (last->line == new->line) &&
1270 (last->function == new->function) &&
1271 (last->filename == new->filename)) {
1272 get_occurance(last);
1276 state->last_occurance = 0;
1277 put_occurance(last);
1279 get_occurance(orig);
1280 result = xmalloc(sizeof(*result), "occurance");
1282 result->filename = new->filename;
1283 result->function = new->function;
1284 result->line = new->line;
1285 result->col = new->col;
1286 result->parent = orig;
1287 state->last_occurance = result;
1292 static struct occurance dummy_occurance = {
1294 .filename = __FILE__,
1301 /* The zero triple is used as a place holder when we are removing pointers
1302 * from a triple. Having allows certain sanity checks to pass even
1303 * when the original triple that was pointed to is gone.
1305 static struct triple zero_triple = {
1306 .next = &zero_triple,
1307 .prev = &zero_triple,
1310 .sizes = TRIPLE_SIZES(0, 0, 0, 0),
1311 .id = -1, /* An invalid id */
1312 .u = { .cval = 0, },
1313 .occurance = &dummy_occurance,
1314 .param { [0] = 0, [1] = 0, },
1318 static unsigned short triple_sizes(struct compile_state *state,
1319 int op, struct type *type, int lhs_wanted, int rhs_wanted)
1321 int lhs, rhs, misc, targ;
1322 valid_op(state, op);
1323 lhs = table_ops[op].lhs;
1324 rhs = table_ops[op].rhs;
1325 misc = table_ops[op].misc;
1326 targ = table_ops[op].targ;
1329 if (op == OP_CALL) {
1332 param = type->right;
1333 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
1335 param = param->right;
1337 if ((param->type & TYPE_MASK) != TYPE_VOID) {
1341 if ((type->left->type & TYPE_MASK) == TYPE_STRUCT) {
1342 lhs = type->left->elements;
1345 else if (op == OP_VAL_VEC) {
1346 rhs = type->elements;
1348 else if ((op == OP_BRANCH) || (op == OP_PHI)) {
1351 else if (op == OP_ASM) {
1355 if ((rhs < 0) || (rhs > MAX_RHS)) {
1356 internal_error(state, 0, "bad rhs");
1358 if ((lhs < 0) || (lhs > MAX_LHS)) {
1359 internal_error(state, 0, "bad lhs");
1361 if ((misc < 0) || (misc > MAX_MISC)) {
1362 internal_error(state, 0, "bad misc");
1364 if ((targ < 0) || (targ > MAX_TARG)) {
1365 internal_error(state, 0, "bad targs");
1367 return TRIPLE_SIZES(lhs, rhs, misc, targ);
1370 static struct triple *alloc_triple(struct compile_state *state,
1371 int op, struct type *type, int lhs, int rhs,
1372 struct occurance *occurance)
1374 size_t size, sizes, extra_count, min_count;
1376 sizes = triple_sizes(state, op, type, lhs, rhs);
1378 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
1379 extra_count = TRIPLE_SIZE(sizes);
1380 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
1382 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
1383 ret = xcmalloc(size, "tripple");
1389 ret->occurance = occurance;
1393 struct triple *dup_triple(struct compile_state *state, struct triple *src)
1396 int src_lhs, src_rhs, src_size;
1397 src_lhs = TRIPLE_LHS(src->sizes);
1398 src_rhs = TRIPLE_RHS(src->sizes);
1399 src_size = TRIPLE_SIZE(src->sizes);
1400 get_occurance(src->occurance);
1401 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
1403 memcpy(dup, src, sizeof(*src));
1404 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
1408 static struct triple *new_triple(struct compile_state *state,
1409 int op, struct type *type, int lhs, int rhs)
1412 struct occurance *occurance;
1413 occurance = new_occurance(state);
1414 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
1418 static struct triple *build_triple(struct compile_state *state,
1419 int op, struct type *type, struct triple *left, struct triple *right,
1420 struct occurance *occurance)
1424 ret = alloc_triple(state, op, type, -1, -1, occurance);
1425 count = TRIPLE_SIZE(ret->sizes);
1427 ret->param[0] = left;
1430 ret->param[1] = right;
1435 static struct triple *triple(struct compile_state *state,
1436 int op, struct type *type, struct triple *left, struct triple *right)
1440 ret = new_triple(state, op, type, -1, -1);
1441 count = TRIPLE_SIZE(ret->sizes);
1443 ret->param[0] = left;
1446 ret->param[1] = right;
1451 static struct triple *branch(struct compile_state *state,
1452 struct triple *targ, struct triple *test)
1455 ret = new_triple(state, OP_BRANCH, &void_type, -1, test?1:0);
1459 TARG(ret, 0) = targ;
1460 /* record the branch target was used */
1461 if (!targ || (targ->op != OP_LABEL)) {
1462 internal_error(state, 0, "branch not to label");
1463 use_triple(targ, ret);
1469 static void insert_triple(struct compile_state *state,
1470 struct triple *first, struct triple *ptr)
1473 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
1474 internal_error(state, ptr, "expression already used");
1477 ptr->prev = first->prev;
1478 ptr->prev->next = ptr;
1479 ptr->next->prev = ptr;
1480 if ((ptr->prev->op == OP_BRANCH) &&
1481 TRIPLE_RHS(ptr->prev->sizes)) {
1482 unuse_triple(first, ptr->prev);
1483 use_triple(ptr, ptr->prev);
1488 static int triple_stores_block(struct compile_state *state, struct triple *ins)
1490 /* This function is used to determine if u.block
1491 * is utilized to store the current block number.
1494 valid_ins(state, ins);
1495 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
1496 return stores_block;
1499 static struct block *block_of_triple(struct compile_state *state,
1502 struct triple *first;
1503 first = RHS(state->main_function, 0);
1504 while(ins != first && !triple_stores_block(state, ins)) {
1505 if (ins == ins->prev) {
1506 internal_error(state, 0, "ins == ins->prev?");
1510 if (!triple_stores_block(state, ins)) {
1511 internal_error(state, ins, "Cannot find block");
1513 return ins->u.block;
1516 static struct triple *pre_triple(struct compile_state *state,
1517 struct triple *base,
1518 int op, struct type *type, struct triple *left, struct triple *right)
1520 struct block *block;
1522 /* If I am an OP_PIECE jump to the real instruction */
1523 if (base->op == OP_PIECE) {
1524 base = MISC(base, 0);
1526 block = block_of_triple(state, base);
1527 get_occurance(base->occurance);
1528 ret = build_triple(state, op, type, left, right, base->occurance);
1529 if (triple_stores_block(state, ret)) {
1530 ret->u.block = block;
1532 insert_triple(state, base, ret);
1533 if (block->first == base) {
1539 static struct triple *post_triple(struct compile_state *state,
1540 struct triple *base,
1541 int op, struct type *type, struct triple *left, struct triple *right)
1543 struct block *block;
1546 /* If I am an OP_PIECE jump to the real instruction */
1547 if (base->op == OP_PIECE) {
1548 base = MISC(base, 0);
1550 /* If I have a left hand side skip over it */
1551 zlhs = TRIPLE_LHS(base->sizes);
1552 if (zlhs && (base->op != OP_WRITE) && (base->op != OP_STORE)) {
1553 base = LHS(base, zlhs - 1);
1556 block = block_of_triple(state, base);
1557 get_occurance(base->occurance);
1558 ret = build_triple(state, op, type, left, right, base->occurance);
1559 if (triple_stores_block(state, ret)) {
1560 ret->u.block = block;
1562 insert_triple(state, base->next, ret);
1563 if (block->last == base) {
1569 static struct triple *label(struct compile_state *state)
1571 /* Labels don't get a type */
1572 struct triple *result;
1573 result = triple(state, OP_LABEL, &void_type, 0, 0);
1577 static void display_triple(FILE *fp, struct triple *ins)
1579 struct occurance *ptr;
1583 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
1586 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
1589 reg = arch_reg_str(ID_REG(ins->id));
1590 if (ins->op == OP_INTCONST) {
1591 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s <0x%08lx> ",
1592 ins, pre, post, reg, ins->template_id, tops(ins->op),
1595 else if (ins->op == OP_ADDRCONST) {
1596 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s %-10p <0x%08lx>",
1597 ins, pre, post, reg, ins->template_id, tops(ins->op),
1598 MISC(ins, 0), ins->u.cval);
1602 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s",
1603 ins, pre, post, reg, ins->template_id, tops(ins->op));
1604 count = TRIPLE_SIZE(ins->sizes);
1605 for(i = 0; i < count; i++) {
1606 fprintf(fp, " %-10p", ins->param[i]);
1613 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
1614 fprintf(fp, " %s,%s:%d.%d",
1624 static int triple_is_pure(struct compile_state *state, struct triple *ins)
1626 /* Does the triple have no side effects.
1627 * I.e. Rexecuting the triple with the same arguments
1628 * gives the same value.
1631 valid_ins(state, ins);
1632 pure = PURE_BITS(table_ops[ins->op].flags);
1633 if ((pure != PURE) && (pure != IMPURE)) {
1634 internal_error(state, 0, "Purity of %s not known\n",
1637 return pure == PURE;
1640 static int triple_is_branch(struct compile_state *state, struct triple *ins)
1642 /* This function is used to determine which triples need
1646 valid_ins(state, ins);
1647 is_branch = (table_ops[ins->op].targ != 0);
1651 static int triple_is_def(struct compile_state *state, struct triple *ins)
1653 /* This function is used to determine which triples need
1657 valid_ins(state, ins);
1658 is_def = (table_ops[ins->op].flags & DEF) == DEF;
1662 static struct triple **triple_iter(struct compile_state *state,
1663 size_t count, struct triple **vector,
1664 struct triple *ins, struct triple **last)
1666 struct triple **ret;
1672 else if ((last >= vector) && (last < (vector + count - 1))) {
1680 static struct triple **triple_lhs(struct compile_state *state,
1681 struct triple *ins, struct triple **last)
1683 return triple_iter(state, TRIPLE_LHS(ins->sizes), &LHS(ins,0),
1687 static struct triple **triple_rhs(struct compile_state *state,
1688 struct triple *ins, struct triple **last)
1690 return triple_iter(state, TRIPLE_RHS(ins->sizes), &RHS(ins,0),
1694 static struct triple **triple_misc(struct compile_state *state,
1695 struct triple *ins, struct triple **last)
1697 return triple_iter(state, TRIPLE_MISC(ins->sizes), &MISC(ins,0),
1701 static struct triple **triple_targ(struct compile_state *state,
1702 struct triple *ins, struct triple **last)
1705 struct triple **ret, **vector;
1707 count = TRIPLE_TARG(ins->sizes);
1708 vector = &TARG(ins, 0);
1713 else if ((last >= vector) && (last < (vector + count - 1))) {
1716 else if ((last == (vector + count - 1)) &&
1717 TRIPLE_RHS(ins->sizes)) {
1725 static void verify_use(struct compile_state *state,
1726 struct triple *user, struct triple *used)
1729 size = TRIPLE_SIZE(user->sizes);
1730 for(i = 0; i < size; i++) {
1731 if (user->param[i] == used) {
1735 if (triple_is_branch(state, user)) {
1736 if (user->next == used) {
1741 internal_error(state, user, "%s(%p) does not use %s(%p)",
1742 tops(user->op), user, tops(used->op), used);
1746 static int find_rhs_use(struct compile_state *state,
1747 struct triple *user, struct triple *used)
1749 struct triple **param;
1751 verify_use(state, user, used);
1752 size = TRIPLE_RHS(user->sizes);
1753 param = &RHS(user, 0);
1754 for(i = 0; i < size; i++) {
1755 if (param[i] == used) {
1762 static void free_triple(struct compile_state *state, struct triple *ptr)
1765 size = sizeof(*ptr) - sizeof(ptr->param) +
1766 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr->sizes));
1767 ptr->prev->next = ptr->next;
1768 ptr->next->prev = ptr->prev;
1770 internal_error(state, ptr, "ptr->use != 0");
1772 put_occurance(ptr->occurance);
1773 memset(ptr, -1, size);
1777 static void release_triple(struct compile_state *state, struct triple *ptr)
1779 struct triple_set *set, *next;
1780 struct triple **expr;
1781 /* Remove ptr from use chains where it is the user */
1782 expr = triple_rhs(state, ptr, 0);
1783 for(; expr; expr = triple_rhs(state, ptr, expr)) {
1785 unuse_triple(*expr, ptr);
1788 expr = triple_lhs(state, ptr, 0);
1789 for(; expr; expr = triple_lhs(state, ptr, expr)) {
1791 unuse_triple(*expr, ptr);
1794 expr = triple_misc(state, ptr, 0);
1795 for(; expr; expr = triple_misc(state, ptr, expr)) {
1797 unuse_triple(*expr, ptr);
1800 expr = triple_targ(state, ptr, 0);
1801 for(; expr; expr = triple_targ(state, ptr, expr)) {
1803 unuse_triple(*expr, ptr);
1806 /* Reomve ptr from use chains where it is used */
1807 for(set = ptr->use; set; set = next) {
1809 expr = triple_rhs(state, set->member, 0);
1810 for(; expr; expr = triple_rhs(state, set->member, expr)) {
1812 *expr = &zero_triple;
1815 expr = triple_lhs(state, set->member, 0);
1816 for(; expr; expr = triple_lhs(state, set->member, expr)) {
1818 *expr = &zero_triple;
1821 expr = triple_misc(state, set->member, 0);
1822 for(; expr; expr = triple_misc(state, set->member, expr)) {
1824 *expr = &zero_triple;
1827 expr = triple_targ(state, set->member, 0);
1828 for(; expr; expr = triple_targ(state, set->member, expr)) {
1830 *expr = &zero_triple;
1833 unuse_triple(ptr, set->member);
1835 free_triple(state, ptr);
1838 static void print_triple(struct compile_state *state, struct triple *ptr);
1840 #define TOK_UNKNOWN 0
1843 #define TOK_LBRACE 3
1844 #define TOK_RBRACE 4
1848 #define TOK_LBRACKET 8
1849 #define TOK_RBRACKET 9
1850 #define TOK_LPAREN 10
1851 #define TOK_RPAREN 11
1856 #define TOK_TIMESEQ 16
1857 #define TOK_DIVEQ 17
1858 #define TOK_MODEQ 18
1859 #define TOK_PLUSEQ 19
1860 #define TOK_MINUSEQ 20
1863 #define TOK_ANDEQ 23
1864 #define TOK_XOREQ 24
1867 #define TOK_NOTEQ 27
1868 #define TOK_QUEST 28
1869 #define TOK_LOGOR 29
1870 #define TOK_LOGAND 30
1874 #define TOK_LESSEQ 34
1875 #define TOK_MOREEQ 35
1879 #define TOK_MINUS 39
1882 #define TOK_PLUSPLUS 42
1883 #define TOK_MINUSMINUS 43
1885 #define TOK_ARROW 45
1887 #define TOK_TILDE 47
1888 #define TOK_LIT_STRING 48
1889 #define TOK_LIT_CHAR 49
1890 #define TOK_LIT_INT 50
1891 #define TOK_LIT_FLOAT 51
1892 #define TOK_MACRO 52
1893 #define TOK_CONCATENATE 53
1895 #define TOK_IDENT 54
1896 #define TOK_STRUCT_NAME 55
1897 #define TOK_ENUM_CONST 56
1898 #define TOK_TYPE_NAME 57
1901 #define TOK_BREAK 59
1904 #define TOK_CONST 62
1905 #define TOK_CONTINUE 63
1906 #define TOK_DEFAULT 64
1908 #define TOK_DOUBLE 66
1911 #define TOK_EXTERN 69
1912 #define TOK_FLOAT 70
1916 #define TOK_INLINE 74
1919 #define TOK_REGISTER 77
1920 #define TOK_RESTRICT 78
1921 #define TOK_RETURN 79
1922 #define TOK_SHORT 80
1923 #define TOK_SIGNED 81
1924 #define TOK_SIZEOF 82
1925 #define TOK_STATIC 83
1926 #define TOK_STRUCT 84
1927 #define TOK_SWITCH 85
1928 #define TOK_TYPEDEF 86
1929 #define TOK_UNION 87
1930 #define TOK_UNSIGNED 88
1932 #define TOK_VOLATILE 90
1933 #define TOK_WHILE 91
1935 #define TOK_ATTRIBUTE 93
1936 #define TOK_ALIGNOF 94
1937 #define TOK_FIRST_KEYWORD TOK_AUTO
1938 #define TOK_LAST_KEYWORD TOK_ALIGNOF
1940 #define TOK_DEFINE 100
1941 #define TOK_UNDEF 101
1942 #define TOK_INCLUDE 102
1943 #define TOK_LINE 103
1944 #define TOK_ERROR 104
1945 #define TOK_WARNING 105
1946 #define TOK_PRAGMA 106
1947 #define TOK_IFDEF 107
1948 #define TOK_IFNDEF 108
1949 #define TOK_ELIF 109
1950 #define TOK_ENDIF 110
1952 #define TOK_FIRST_MACRO TOK_DEFINE
1953 #define TOK_LAST_MACRO TOK_ENDIF
1957 static const char *tokens[] = {
1958 [TOK_UNKNOWN ] = "unknown",
1959 [TOK_SPACE ] = ":space:",
1961 [TOK_LBRACE ] = "{",
1962 [TOK_RBRACE ] = "}",
1966 [TOK_LBRACKET ] = "[",
1967 [TOK_RBRACKET ] = "]",
1968 [TOK_LPAREN ] = "(",
1969 [TOK_RPAREN ] = ")",
1971 [TOK_DOTS ] = "...",
1974 [TOK_TIMESEQ ] = "*=",
1975 [TOK_DIVEQ ] = "/=",
1976 [TOK_MODEQ ] = "%=",
1977 [TOK_PLUSEQ ] = "+=",
1978 [TOK_MINUSEQ ] = "-=",
1979 [TOK_SLEQ ] = "<<=",
1980 [TOK_SREQ ] = ">>=",
1981 [TOK_ANDEQ ] = "&=",
1982 [TOK_XOREQ ] = "^=",
1985 [TOK_NOTEQ ] = "!=",
1987 [TOK_LOGOR ] = "||",
1988 [TOK_LOGAND ] = "&&",
1992 [TOK_LESSEQ ] = "<=",
1993 [TOK_MOREEQ ] = ">=",
2000 [TOK_PLUSPLUS ] = "++",
2001 [TOK_MINUSMINUS ] = "--",
2003 [TOK_ARROW ] = "->",
2006 [TOK_LIT_STRING ] = ":string:",
2007 [TOK_IDENT ] = ":ident:",
2008 [TOK_TYPE_NAME ] = ":typename:",
2009 [TOK_LIT_CHAR ] = ":char:",
2010 [TOK_LIT_INT ] = ":integer:",
2011 [TOK_LIT_FLOAT ] = ":float:",
2013 [TOK_CONCATENATE ] = "##",
2015 [TOK_AUTO ] = "auto",
2016 [TOK_BREAK ] = "break",
2017 [TOK_CASE ] = "case",
2018 [TOK_CHAR ] = "char",
2019 [TOK_CONST ] = "const",
2020 [TOK_CONTINUE ] = "continue",
2021 [TOK_DEFAULT ] = "default",
2023 [TOK_DOUBLE ] = "double",
2024 [TOK_ELSE ] = "else",
2025 [TOK_ENUM ] = "enum",
2026 [TOK_EXTERN ] = "extern",
2027 [TOK_FLOAT ] = "float",
2029 [TOK_GOTO ] = "goto",
2031 [TOK_INLINE ] = "inline",
2033 [TOK_LONG ] = "long",
2034 [TOK_REGISTER ] = "register",
2035 [TOK_RESTRICT ] = "restrict",
2036 [TOK_RETURN ] = "return",
2037 [TOK_SHORT ] = "short",
2038 [TOK_SIGNED ] = "signed",
2039 [TOK_SIZEOF ] = "sizeof",
2040 [TOK_STATIC ] = "static",
2041 [TOK_STRUCT ] = "struct",
2042 [TOK_SWITCH ] = "switch",
2043 [TOK_TYPEDEF ] = "typedef",
2044 [TOK_UNION ] = "union",
2045 [TOK_UNSIGNED ] = "unsigned",
2046 [TOK_VOID ] = "void",
2047 [TOK_VOLATILE ] = "volatile",
2048 [TOK_WHILE ] = "while",
2050 [TOK_ATTRIBUTE ] = "__attribute__",
2051 [TOK_ALIGNOF ] = "__alignof__",
2053 [TOK_DEFINE ] = "define",
2054 [TOK_UNDEF ] = "undef",
2055 [TOK_INCLUDE ] = "include",
2056 [TOK_LINE ] = "line",
2057 [TOK_ERROR ] = "error",
2058 [TOK_WARNING ] = "warning",
2059 [TOK_PRAGMA ] = "pragma",
2060 [TOK_IFDEF ] = "ifdef",
2061 [TOK_IFNDEF ] = "ifndef",
2062 [TOK_ELIF ] = "elif",
2063 [TOK_ENDIF ] = "endif",
2068 static unsigned int hash(const char *str, int str_len)
2072 end = str + str_len;
2074 for(; str < end; str++) {
2075 hash = (hash *263) + *str;
2077 hash = hash & (HASH_TABLE_SIZE -1);
2081 static struct hash_entry *lookup(
2082 struct compile_state *state, const char *name, int name_len)
2084 struct hash_entry *entry;
2086 index = hash(name, name_len);
2087 entry = state->hash_table[index];
2089 ((entry->name_len != name_len) ||
2090 (memcmp(entry->name, name, name_len) != 0))) {
2091 entry = entry->next;
2095 /* Get a private copy of the name */
2096 new_name = xmalloc(name_len + 1, "hash_name");
2097 memcpy(new_name, name, name_len);
2098 new_name[name_len] = '\0';
2100 /* Create a new hash entry */
2101 entry = xcmalloc(sizeof(*entry), "hash_entry");
2102 entry->next = state->hash_table[index];
2103 entry->name = new_name;
2104 entry->name_len = name_len;
2106 /* Place the new entry in the hash table */
2107 state->hash_table[index] = entry;
2112 static void ident_to_keyword(struct compile_state *state, struct token *tk)
2114 struct hash_entry *entry;
2116 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
2117 (entry->tok == TOK_ENUM_CONST) ||
2118 ((entry->tok >= TOK_FIRST_KEYWORD) &&
2119 (entry->tok <= TOK_LAST_KEYWORD)))) {
2120 tk->tok = entry->tok;
2124 static void ident_to_macro(struct compile_state *state, struct token *tk)
2126 struct hash_entry *entry;
2129 (entry->tok >= TOK_FIRST_MACRO) &&
2130 (entry->tok <= TOK_LAST_MACRO)) {
2131 tk->tok = entry->tok;
2135 static void hash_keyword(
2136 struct compile_state *state, const char *keyword, int tok)
2138 struct hash_entry *entry;
2139 entry = lookup(state, keyword, strlen(keyword));
2140 if (entry && entry->tok != TOK_UNKNOWN) {
2141 die("keyword %s already hashed", keyword);
2147 struct compile_state *state, struct hash_entry *ident,
2148 struct symbol **chain, struct triple *def, struct type *type)
2151 if (*chain && ((*chain)->scope_depth == state->scope_depth)) {
2152 error(state, 0, "%s already defined", ident->name);
2154 sym = xcmalloc(sizeof(*sym), "symbol");
2158 sym->scope_depth = state->scope_depth;
2163 static void label_symbol(struct compile_state *state,
2164 struct hash_entry *ident, struct triple *label)
2167 if (ident->sym_label) {
2168 error(state, 0, "label %s already defined", ident->name);
2170 sym = xcmalloc(sizeof(*sym), "label");
2173 sym->type = &void_type;
2174 sym->scope_depth = FUNCTION_SCOPE_DEPTH;
2176 ident->sym_label = sym;
2179 static void start_scope(struct compile_state *state)
2181 state->scope_depth++;
2184 static void end_scope_syms(struct symbol **chain, int depth)
2186 struct symbol *sym, *next;
2188 while(sym && (sym->scope_depth == depth)) {
2196 static void end_scope(struct compile_state *state)
2200 /* Walk through the hash table and remove all symbols
2201 * in the current scope.
2203 depth = state->scope_depth;
2204 for(i = 0; i < HASH_TABLE_SIZE; i++) {
2205 struct hash_entry *entry;
2206 entry = state->hash_table[i];
2208 end_scope_syms(&entry->sym_label, depth);
2209 end_scope_syms(&entry->sym_struct, depth);
2210 end_scope_syms(&entry->sym_ident, depth);
2211 entry = entry->next;
2214 state->scope_depth = depth - 1;
2217 static void register_keywords(struct compile_state *state)
2219 hash_keyword(state, "auto", TOK_AUTO);
2220 hash_keyword(state, "break", TOK_BREAK);
2221 hash_keyword(state, "case", TOK_CASE);
2222 hash_keyword(state, "char", TOK_CHAR);
2223 hash_keyword(state, "const", TOK_CONST);
2224 hash_keyword(state, "continue", TOK_CONTINUE);
2225 hash_keyword(state, "default", TOK_DEFAULT);
2226 hash_keyword(state, "do", TOK_DO);
2227 hash_keyword(state, "double", TOK_DOUBLE);
2228 hash_keyword(state, "else", TOK_ELSE);
2229 hash_keyword(state, "enum", TOK_ENUM);
2230 hash_keyword(state, "extern", TOK_EXTERN);
2231 hash_keyword(state, "float", TOK_FLOAT);
2232 hash_keyword(state, "for", TOK_FOR);
2233 hash_keyword(state, "goto", TOK_GOTO);
2234 hash_keyword(state, "if", TOK_IF);
2235 hash_keyword(state, "inline", TOK_INLINE);
2236 hash_keyword(state, "int", TOK_INT);
2237 hash_keyword(state, "long", TOK_LONG);
2238 hash_keyword(state, "register", TOK_REGISTER);
2239 hash_keyword(state, "restrict", TOK_RESTRICT);
2240 hash_keyword(state, "return", TOK_RETURN);
2241 hash_keyword(state, "short", TOK_SHORT);
2242 hash_keyword(state, "signed", TOK_SIGNED);
2243 hash_keyword(state, "sizeof", TOK_SIZEOF);
2244 hash_keyword(state, "static", TOK_STATIC);
2245 hash_keyword(state, "struct", TOK_STRUCT);
2246 hash_keyword(state, "switch", TOK_SWITCH);
2247 hash_keyword(state, "typedef", TOK_TYPEDEF);
2248 hash_keyword(state, "union", TOK_UNION);
2249 hash_keyword(state, "unsigned", TOK_UNSIGNED);
2250 hash_keyword(state, "void", TOK_VOID);
2251 hash_keyword(state, "volatile", TOK_VOLATILE);
2252 hash_keyword(state, "__volatile__", TOK_VOLATILE);
2253 hash_keyword(state, "while", TOK_WHILE);
2254 hash_keyword(state, "asm", TOK_ASM);
2255 hash_keyword(state, "__asm__", TOK_ASM);
2256 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
2257 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
2260 static void register_macro_keywords(struct compile_state *state)
2262 hash_keyword(state, "define", TOK_DEFINE);
2263 hash_keyword(state, "undef", TOK_UNDEF);
2264 hash_keyword(state, "include", TOK_INCLUDE);
2265 hash_keyword(state, "line", TOK_LINE);
2266 hash_keyword(state, "error", TOK_ERROR);
2267 hash_keyword(state, "warning", TOK_WARNING);
2268 hash_keyword(state, "pragma", TOK_PRAGMA);
2269 hash_keyword(state, "ifdef", TOK_IFDEF);
2270 hash_keyword(state, "ifndef", TOK_IFNDEF);
2271 hash_keyword(state, "elif", TOK_ELIF);
2272 hash_keyword(state, "endif", TOK_ENDIF);
2275 static int spacep(int c)
2291 static int digitp(int c)
2295 case '0': case '1': case '2': case '3': case '4':
2296 case '5': case '6': case '7': case '8': case '9':
2302 static int digval(int c)
2305 if ((c >= '0') && (c <= '9')) {
2311 static int hexdigitp(int c)
2315 case '0': case '1': case '2': case '3': case '4':
2316 case '5': case '6': case '7': case '8': case '9':
2317 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2318 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2324 static int hexdigval(int c)
2327 if ((c >= '0') && (c <= '9')) {
2330 else if ((c >= 'A') && (c <= 'F')) {
2331 val = 10 + (c - 'A');
2333 else if ((c >= 'a') && (c <= 'f')) {
2334 val = 10 + (c - 'a');
2339 static int octdigitp(int c)
2343 case '0': case '1': case '2': case '3':
2344 case '4': case '5': case '6': case '7':
2350 static int octdigval(int c)
2353 if ((c >= '0') && (c <= '7')) {
2359 static int letterp(int c)
2363 case 'a': case 'b': case 'c': case 'd': case 'e':
2364 case 'f': case 'g': case 'h': case 'i': case 'j':
2365 case 'k': case 'l': case 'm': case 'n': case 'o':
2366 case 'p': case 'q': case 'r': case 's': case 't':
2367 case 'u': case 'v': case 'w': case 'x': case 'y':
2369 case 'A': case 'B': case 'C': case 'D': case 'E':
2370 case 'F': case 'G': case 'H': case 'I': case 'J':
2371 case 'K': case 'L': case 'M': case 'N': case 'O':
2372 case 'P': case 'Q': case 'R': case 'S': case 'T':
2373 case 'U': case 'V': case 'W': case 'X': case 'Y':
2382 static int char_value(struct compile_state *state,
2383 const signed char **strp, const signed char *end)
2385 const signed char *str;
2389 if ((c == '\\') && (str < end)) {
2391 case 'n': c = '\n'; str++; break;
2392 case 't': c = '\t'; str++; break;
2393 case 'v': c = '\v'; str++; break;
2394 case 'b': c = '\b'; str++; break;
2395 case 'r': c = '\r'; str++; break;
2396 case 'f': c = '\f'; str++; break;
2397 case 'a': c = '\a'; str++; break;
2398 case '\\': c = '\\'; str++; break;
2399 case '?': c = '?'; str++; break;
2400 case '\'': c = '\''; str++; break;
2401 case '"': c = '"'; break;
2405 while((str < end) && hexdigitp(*str)) {
2407 c += hexdigval(*str);
2411 case '0': case '1': case '2': case '3':
2412 case '4': case '5': case '6': case '7':
2414 while((str < end) && octdigitp(*str)) {
2416 c += octdigval(*str);
2421 error(state, 0, "Invalid character constant");
2429 static char *after_digits(char *ptr, char *end)
2431 while((ptr < end) && digitp(*ptr)) {
2437 static char *after_octdigits(char *ptr, char *end)
2439 while((ptr < end) && octdigitp(*ptr)) {
2445 static char *after_hexdigits(char *ptr, char *end)
2447 while((ptr < end) && hexdigitp(*ptr)) {
2453 static void save_string(struct compile_state *state,
2454 struct token *tk, char *start, char *end, const char *id)
2458 /* Create a private copy of the string */
2459 str_len = end - start + 1;
2460 str = xmalloc(str_len + 1, id);
2461 memcpy(str, start, str_len);
2462 str[str_len] = '\0';
2464 /* Store the copy in the token */
2466 tk->str_len = str_len;
2468 static void next_token(struct compile_state *state, int index)
2470 struct file_state *file;
2478 tk = &state->token[index];
2481 token = tokp = file->pos;
2482 end = file->buf + file->size;
2489 if ((tokp + 1) < end) {
2493 if ((tokp + 2) < end) {
2497 if ((tokp + 3) < end) {
2505 else if (spacep(c)) {
2507 while ((tokp < end) && spacep(c)) {
2510 file->report_line++;
2511 file->line_start = tokp + 1;
2520 else if ((c == '/') && (c1 == '/')) {
2522 for(tokp += 2; tokp < end; tokp++) {
2526 file->report_line++;
2527 file->line_start = tokp +1;
2533 else if ((c == '/') && (c1 == '*')) {
2537 line_start = file->line_start;
2538 for(tokp += 2; (end - tokp) >= 2; tokp++) {
2542 line_start = tokp +1;
2544 else if ((c == '*') && (tokp[1] == '/')) {
2550 if (tok == TOK_UNKNOWN) {
2551 error(state, 0, "unterminated comment");
2553 file->report_line += line - file->line;
2555 file->line_start = line_start;
2557 /* string constants */
2558 else if ((c == '"') ||
2559 ((c == 'L') && (c1 == '"'))) {
2564 line_start = file->line_start;
2570 for(tokp += 1; tokp < end; tokp++) {
2574 line_start = tokp + 1;
2576 else if ((c == '\\') && (tokp +1 < end)) {
2579 else if (c == '"') {
2580 tok = TOK_LIT_STRING;
2584 if (tok == TOK_UNKNOWN) {
2585 error(state, 0, "unterminated string constant");
2587 if (line != file->line) {
2588 warning(state, 0, "multiline string constant");
2590 file->report_line += line - file->line;
2592 file->line_start = line_start;
2594 /* Save the string value */
2595 save_string(state, tk, token, tokp, "literal string");
2597 /* character constants */
2598 else if ((c == '\'') ||
2599 ((c == 'L') && (c1 == '\''))) {
2604 line_start = file->line_start;
2610 for(tokp += 1; tokp < end; tokp++) {
2614 line_start = tokp + 1;
2616 else if ((c == '\\') && (tokp +1 < end)) {
2619 else if (c == '\'') {
2624 if (tok == TOK_UNKNOWN) {
2625 error(state, 0, "unterminated character constant");
2627 if (line != file->line) {
2628 warning(state, 0, "multiline character constant");
2630 file->report_line += line - file->line;
2632 file->line_start = line_start;
2634 /* Save the character value */
2635 save_string(state, tk, token, tokp, "literal character");
2637 /* integer and floating constants
2643 * Floating constants
2644 * {digits}.{digits}[Ee][+-]?{digits}
2646 * {digits}[Ee][+-]?{digits}
2647 * .{digits}[Ee][+-]?{digits}
2651 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
2656 next = after_digits(tokp, end);
2661 if (next[0] == '.') {
2662 new = after_digits(next, end);
2663 is_float = (new != next);
2666 if ((next[0] == 'e') || (next[0] == 'E')) {
2667 if (((next + 1) < end) &&
2668 ((next[1] == '+') || (next[1] == '-'))) {
2671 new = after_digits(next, end);
2672 is_float = (new != next);
2676 tok = TOK_LIT_FLOAT;
2677 if ((next < end) && (
2686 if (!is_float && digitp(c)) {
2688 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
2689 next = after_hexdigits(tokp + 2, end);
2691 else if (c == '0') {
2692 next = after_octdigits(tokp, end);
2695 next = after_digits(tokp, end);
2697 /* crazy integer suffixes */
2699 ((next[0] == 'u') || (next[0] == 'U'))) {
2702 ((next[0] == 'l') || (next[0] == 'L'))) {
2706 else if ((next < end) &&
2707 ((next[0] == 'l') || (next[0] == 'L'))) {
2710 ((next[0] == 'u') || (next[0] == 'U'))) {
2717 /* Save the integer/floating point value */
2718 save_string(state, tk, token, tokp, "literal number");
2721 else if (letterp(c)) {
2723 for(tokp += 1; tokp < end; tokp++) {
2725 if (!letterp(c) && !digitp(c)) {
2730 tk->ident = lookup(state, token, tokp +1 - token);
2732 /* C99 alternate macro characters */
2733 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
2735 tok = TOK_CONCATENATE;
2737 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { tokp += 2; tok = TOK_DOTS; }
2738 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { tokp += 2; tok = TOK_SLEQ; }
2739 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { tokp += 2; tok = TOK_SREQ; }
2740 else if ((c == '*') && (c1 == '=')) { tokp += 1; tok = TOK_TIMESEQ; }
2741 else if ((c == '/') && (c1 == '=')) { tokp += 1; tok = TOK_DIVEQ; }
2742 else if ((c == '%') && (c1 == '=')) { tokp += 1; tok = TOK_MODEQ; }
2743 else if ((c == '+') && (c1 == '=')) { tokp += 1; tok = TOK_PLUSEQ; }
2744 else if ((c == '-') && (c1 == '=')) { tokp += 1; tok = TOK_MINUSEQ; }
2745 else if ((c == '&') && (c1 == '=')) { tokp += 1; tok = TOK_ANDEQ; }
2746 else if ((c == '^') && (c1 == '=')) { tokp += 1; tok = TOK_XOREQ; }
2747 else if ((c == '|') && (c1 == '=')) { tokp += 1; tok = TOK_OREQ; }
2748 else if ((c == '=') && (c1 == '=')) { tokp += 1; tok = TOK_EQEQ; }
2749 else if ((c == '!') && (c1 == '=')) { tokp += 1; tok = TOK_NOTEQ; }
2750 else if ((c == '|') && (c1 == '|')) { tokp += 1; tok = TOK_LOGOR; }
2751 else if ((c == '&') && (c1 == '&')) { tokp += 1; tok = TOK_LOGAND; }
2752 else if ((c == '<') && (c1 == '=')) { tokp += 1; tok = TOK_LESSEQ; }
2753 else if ((c == '>') && (c1 == '=')) { tokp += 1; tok = TOK_MOREEQ; }
2754 else if ((c == '<') && (c1 == '<')) { tokp += 1; tok = TOK_SL; }
2755 else if ((c == '>') && (c1 == '>')) { tokp += 1; tok = TOK_SR; }
2756 else if ((c == '+') && (c1 == '+')) { tokp += 1; tok = TOK_PLUSPLUS; }
2757 else if ((c == '-') && (c1 == '-')) { tokp += 1; tok = TOK_MINUSMINUS; }
2758 else if ((c == '-') && (c1 == '>')) { tokp += 1; tok = TOK_ARROW; }
2759 else if ((c == '<') && (c1 == ':')) { tokp += 1; tok = TOK_LBRACKET; }
2760 else if ((c == ':') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACKET; }
2761 else if ((c == '<') && (c1 == '%')) { tokp += 1; tok = TOK_LBRACE; }
2762 else if ((c == '%') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACE; }
2763 else if ((c == '%') && (c1 == ':')) { tokp += 1; tok = TOK_MACRO; }
2764 else if ((c == '#') && (c1 == '#')) { tokp += 1; tok = TOK_CONCATENATE; }
2765 else if (c == ';') { tok = TOK_SEMI; }
2766 else if (c == '{') { tok = TOK_LBRACE; }
2767 else if (c == '}') { tok = TOK_RBRACE; }
2768 else if (c == ',') { tok = TOK_COMMA; }
2769 else if (c == '=') { tok = TOK_EQ; }
2770 else if (c == ':') { tok = TOK_COLON; }
2771 else if (c == '[') { tok = TOK_LBRACKET; }
2772 else if (c == ']') { tok = TOK_RBRACKET; }
2773 else if (c == '(') { tok = TOK_LPAREN; }
2774 else if (c == ')') { tok = TOK_RPAREN; }
2775 else if (c == '*') { tok = TOK_STAR; }
2776 else if (c == '>') { tok = TOK_MORE; }
2777 else if (c == '<') { tok = TOK_LESS; }
2778 else if (c == '?') { tok = TOK_QUEST; }
2779 else if (c == '|') { tok = TOK_OR; }
2780 else if (c == '&') { tok = TOK_AND; }
2781 else if (c == '^') { tok = TOK_XOR; }
2782 else if (c == '+') { tok = TOK_PLUS; }
2783 else if (c == '-') { tok = TOK_MINUS; }
2784 else if (c == '/') { tok = TOK_DIV; }
2785 else if (c == '%') { tok = TOK_MOD; }
2786 else if (c == '!') { tok = TOK_BANG; }
2787 else if (c == '.') { tok = TOK_DOT; }
2788 else if (c == '~') { tok = TOK_TILDE; }
2789 else if (c == '#') { tok = TOK_MACRO; }
2790 if (tok == TOK_MACRO) {
2791 /* Only match preprocessor directives at the start of a line */
2793 for(ptr = file->line_start; spacep(*ptr); ptr++)
2799 if (tok == TOK_UNKNOWN) {
2800 error(state, 0, "unknown token");
2803 file->pos = tokp + 1;
2805 if (tok == TOK_IDENT) {
2806 ident_to_keyword(state, tk);
2808 /* Don't return space tokens. */
2809 if (tok == TOK_SPACE) {
2814 static void compile_macro(struct compile_state *state, struct token *tk)
2816 struct file_state *file;
2817 struct hash_entry *ident;
2819 file = xmalloc(sizeof(*file), "file_state");
2820 file->basename = xstrdup(tk->ident->name);
2821 file->dirname = xstrdup("");
2822 file->size = ident->sym_define->buf_len;
2823 file->buf = xmalloc(file->size +2, file->basename);
2824 memcpy(file->buf, ident->sym_define->buf, file->size);
2825 file->buf[file->size] = '\n';
2826 file->buf[file->size + 1] = '\0';
2827 file->pos = file->buf;
2828 file->line_start = file->pos;
2830 file->report_line = 1;
2831 file->report_name = file->basename;
2832 file->report_dir = file->dirname;
2833 file->prev = state->file;
2838 static int mpeek(struct compile_state *state, int index)
2842 tk = &state->token[index + 1];
2843 if (tk->tok == -1) {
2844 next_token(state, index + 1);
2848 if ((tk->tok == TOK_EOF) &&
2849 (state->file != state->macro_file) &&
2850 (state->file->prev)) {
2851 struct file_state *file = state->file;
2852 state->file = file->prev;
2853 /* file->basename is used keep it */
2854 if (file->report_dir != file->dirname) {
2855 xfree(file->report_dir);
2857 xfree(file->dirname);
2860 next_token(state, index + 1);
2863 else if (tk->ident && tk->ident->sym_define) {
2864 compile_macro(state, tk);
2865 next_token(state, index + 1);
2869 /* Don't show the token on the next line */
2870 if (state->macro_line < state->macro_file->line) {
2873 return state->token[index +1].tok;
2876 static void meat(struct compile_state *state, int index, int tok)
2880 next_tok = mpeek(state, index);
2881 if (next_tok != tok) {
2882 const char *name1, *name2;
2883 name1 = tokens[next_tok];
2885 if (next_tok == TOK_IDENT) {
2886 name2 = state->token[index + 1].ident->name;
2888 error(state, 0, "found %s %s expected %s",
2889 name1, name2, tokens[tok]);
2891 /* Free the old token value */
2892 if (state->token[index].str_len) {
2893 memset((void *)(state->token[index].val.str), -1,
2894 state->token[index].str_len);
2895 xfree(state->token[index].val.str);
2897 for(i = index; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
2898 state->token[i] = state->token[i + 1];
2900 memset(&state->token[i], 0, sizeof(state->token[i]));
2901 state->token[i].tok = -1;
2904 static long_t mcexpr(struct compile_state *state, int index);
2906 static long_t mprimary_expr(struct compile_state *state, int index)
2910 tok = mpeek(state, index);
2911 while(state->token[index + 1].ident &&
2912 state->token[index + 1].ident->sym_define) {
2913 meat(state, index, tok);
2914 compile_macro(state, &state->token[index]);
2915 tok = mpeek(state, index);
2919 meat(state, index, TOK_LPAREN);
2920 val = mcexpr(state, index);
2921 meat(state, index, TOK_RPAREN);
2926 meat(state, index, TOK_LIT_INT);
2928 val = strtol(state->token[index].val.str, &end, 0);
2929 if (((val == LONG_MIN) || (val == LONG_MAX)) &&
2930 (errno == ERANGE)) {
2931 error(state, 0, "Integer constant to large");
2936 meat(state, index, TOK_LIT_INT);
2941 static long_t munary_expr(struct compile_state *state, int index)
2944 switch(mpeek(state, index)) {
2946 meat(state, index, TOK_PLUS);
2947 val = munary_expr(state, index);
2951 meat(state, index, TOK_MINUS);
2952 val = munary_expr(state, index);
2956 meat(state, index, TOK_BANG);
2957 val = munary_expr(state, index);
2961 meat(state, index, TOK_BANG);
2962 val = munary_expr(state, index);
2966 val = mprimary_expr(state, index);
2972 static long_t mmul_expr(struct compile_state *state, int index)
2976 val = munary_expr(state, index);
2980 switch(mpeek(state, index)) {
2982 meat(state, index, TOK_STAR);
2983 right = munary_expr(state, index);
2987 meat(state, index, TOK_DIV);
2988 right = munary_expr(state, index);
2992 meat(state, index, TOK_MOD);
2993 right = munary_expr(state, index);
3005 static long_t madd_expr(struct compile_state *state, int index)
3009 val = mmul_expr(state, index);
3013 switch(mpeek(state, index)) {
3015 meat(state, index, TOK_PLUS);
3016 right = mmul_expr(state, index);
3020 meat(state, index, TOK_MINUS);
3021 right = mmul_expr(state, index);
3033 static long_t mshift_expr(struct compile_state *state, int index)
3037 val = madd_expr(state, index);
3041 switch(mpeek(state, index)) {
3043 meat(state, index, TOK_SL);
3044 right = madd_expr(state, index);
3048 meat(state, index, TOK_SR);
3049 right = madd_expr(state, index);
3061 static long_t mrel_expr(struct compile_state *state, int index)
3065 val = mshift_expr(state, index);
3069 switch(mpeek(state, index)) {
3071 meat(state, index, TOK_LESS);
3072 right = mshift_expr(state, index);
3076 meat(state, index, TOK_MORE);
3077 right = mshift_expr(state, index);
3081 meat(state, index, TOK_LESSEQ);
3082 right = mshift_expr(state, index);
3086 meat(state, index, TOK_MOREEQ);
3087 right = mshift_expr(state, index);
3098 static long_t meq_expr(struct compile_state *state, int index)
3102 val = mrel_expr(state, index);
3106 switch(mpeek(state, index)) {
3108 meat(state, index, TOK_EQEQ);
3109 right = mrel_expr(state, index);
3113 meat(state, index, TOK_NOTEQ);
3114 right = mrel_expr(state, index);
3125 static long_t mand_expr(struct compile_state *state, int index)
3128 val = meq_expr(state, index);
3129 if (mpeek(state, index) == TOK_AND) {
3131 meat(state, index, TOK_AND);
3132 right = meq_expr(state, index);
3138 static long_t mxor_expr(struct compile_state *state, int index)
3141 val = mand_expr(state, index);
3142 if (mpeek(state, index) == TOK_XOR) {
3144 meat(state, index, TOK_XOR);
3145 right = mand_expr(state, index);
3151 static long_t mor_expr(struct compile_state *state, int index)
3154 val = mxor_expr(state, index);
3155 if (mpeek(state, index) == TOK_OR) {
3157 meat(state, index, TOK_OR);
3158 right = mxor_expr(state, index);
3164 static long_t mland_expr(struct compile_state *state, int index)
3167 val = mor_expr(state, index);
3168 if (mpeek(state, index) == TOK_LOGAND) {
3170 meat(state, index, TOK_LOGAND);
3171 right = mor_expr(state, index);
3176 static long_t mlor_expr(struct compile_state *state, int index)
3179 val = mland_expr(state, index);
3180 if (mpeek(state, index) == TOK_LOGOR) {
3182 meat(state, index, TOK_LOGOR);
3183 right = mland_expr(state, index);
3189 static long_t mcexpr(struct compile_state *state, int index)
3191 return mlor_expr(state, index);
3193 static void preprocess(struct compile_state *state, int index)
3195 /* Doing much more with the preprocessor would require
3196 * a parser and a major restructuring.
3197 * Postpone that for later.
3199 struct file_state *file;
3205 tk = &state->token[index];
3206 state->macro_line = line = file->line;
3207 state->macro_file = file;
3209 next_token(state, index);
3210 ident_to_macro(state, tk);
3211 if (tk->tok == TOK_IDENT) {
3212 error(state, 0, "undefined preprocessing directive `%s'",
3219 override_line = strtoul(tk->val.str, 0, 10);
3220 next_token(state, index);
3221 /* I have a cpp line marker parse it */
3222 if (tk->tok == TOK_LIT_STRING) {
3223 const char *token, *base;
3225 int name_len, dir_len;
3226 name = xmalloc(tk->str_len, "report_name");
3227 token = tk->val.str + 1;
3228 base = strrchr(token, '/');
3229 name_len = tk->str_len -2;
3231 dir_len = base - token;
3233 name_len -= base - token;
3238 memcpy(name, base, name_len);
3239 name[name_len] = '\0';
3240 dir = xmalloc(dir_len + 1, "report_dir");
3241 memcpy(dir, token, dir_len);
3242 dir[dir_len] = '\0';
3243 file->report_line = override_line - 1;
3244 file->report_name = name;
3245 file->report_dir = dir;
3250 meat(state, index, TOK_LINE);
3251 meat(state, index, TOK_LIT_INT);
3252 file->report_line = strtoul(tk->val.str, 0, 10) -1;
3253 if (mpeek(state, index) == TOK_LIT_STRING) {
3254 const char *token, *base;
3256 int name_len, dir_len;
3257 meat(state, index, TOK_LIT_STRING);
3258 name = xmalloc(tk->str_len, "report_name");
3259 token = tk->val.str + 1;
3260 name_len = tk->str_len - 2;
3262 dir_len = base - token;
3264 name_len -= base - token;
3269 memcpy(name, base, name_len);
3270 name[name_len] = '\0';
3271 dir = xmalloc(dir_len + 1, "report_dir");
3272 memcpy(dir, token, dir_len);
3273 dir[dir_len] = '\0';
3274 file->report_name = name;
3275 file->report_dir = dir;
3280 if (state->if_value < 0) {
3283 warning(state, 0, "Ignoring preprocessor directive: %s",
3287 error(state, 0, "#elif not supported");
3288 #warning "FIXME multiple #elif and #else in an #if do not work properly"
3289 if (state->if_depth == 0) {
3290 error(state, 0, "#elif without #if");
3292 /* If the #if was taken the #elif just disables the following code */
3293 if (state->if_value >= 0) {
3294 state->if_value = - state->if_value;
3296 /* If the previous #if was not taken see if the #elif enables the
3299 else if ((state->if_value < 0) &&
3300 (state->if_depth == - state->if_value))
3302 if (mcexpr(state, index) != 0) {
3303 state->if_value = state->if_depth;
3306 state->if_value = - state->if_depth;
3312 if (state->if_value < 0) {
3315 if (mcexpr(state, index) != 0) {
3316 state->if_value = state->if_depth;
3319 state->if_value = - state->if_depth;
3324 if (state->if_value < 0) {
3327 next_token(state, index);
3328 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3329 error(state, 0, "Invalid macro name");
3331 if (tk->ident->sym_define == 0) {
3332 state->if_value = state->if_depth;
3335 state->if_value = - state->if_depth;
3340 if (state->if_value < 0) {
3343 next_token(state, index);
3344 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3345 error(state, 0, "Invalid macro name");
3347 if (tk->ident->sym_define != 0) {
3348 state->if_value = state->if_depth;
3351 state->if_value = - state->if_depth;
3355 if (state->if_depth == 0) {
3356 error(state, 0, "#else without #if");
3358 if ((state->if_value >= 0) ||
3359 ((state->if_value < 0) &&
3360 (state->if_depth == -state->if_value)))
3362 state->if_value = - state->if_value;
3366 if (state->if_depth == 0) {
3367 error(state, 0, "#endif without #if");
3369 if ((state->if_value >= 0) ||
3370 ((state->if_value < 0) &&
3371 (state->if_depth == -state->if_value)))
3373 state->if_value = state->if_depth - 1;
3379 struct hash_entry *ident;
3380 struct macro *macro;
3383 if (state->if_value < 0) /* quit early when #if'd out */
3386 meat(state, index, TOK_IDENT);
3390 if (*file->pos == '(') {
3391 #warning "FIXME macros with arguments not supported"
3392 error(state, 0, "Macros with arguments not supported");
3395 /* Find the end of the line to get an estimate of
3396 * the macro's length.
3398 for(ptr = file->pos; *ptr != '\n'; ptr++)
3401 if (ident->sym_define != 0) {
3402 error(state, 0, "macro %s already defined\n", ident->name);
3404 macro = xmalloc(sizeof(*macro), "macro");
3405 macro->ident = ident;
3406 macro->buf_len = ptr - file->pos +1;
3407 macro->buf = xmalloc(macro->buf_len +2, "macro buf");
3409 memcpy(macro->buf, file->pos, macro->buf_len);
3410 macro->buf[macro->buf_len] = '\n';
3411 macro->buf[macro->buf_len +1] = '\0';
3413 ident->sym_define = macro;
3420 /* Find the end of the line */
3421 for(end = file->pos; *end != '\n'; end++)
3423 len = (end - file->pos);
3424 if (state->if_value >= 0) {
3425 error(state, 0, "%*.*s", len, len, file->pos);
3434 /* Find the end of the line */
3435 for(end = file->pos; *end != '\n'; end++)
3437 len = (end - file->pos);
3438 if (state->if_value >= 0) {
3439 warning(state, 0, "%*.*s", len, len, file->pos);
3451 next_token(state, index);
3452 if (tk->tok == TOK_LIT_STRING) {
3455 name = xmalloc(tk->str_len, "include");
3456 token = tk->val.str +1;
3457 name_len = tk->str_len -2;
3458 if (*token == '"') {
3462 memcpy(name, token, name_len);
3463 name[name_len] = '\0';
3466 else if (tk->tok == TOK_LESS) {
3469 for(end = start; *end != '\n'; end++) {
3475 error(state, 0, "Unterminated included directive");
3477 name = xmalloc(end - start + 1, "include");
3478 memcpy(name, start, end - start);
3479 name[end - start] = '\0';
3484 error(state, 0, "Invalid include directive");
3486 /* Error if there are any characters after the include */
3487 for(ptr = file->pos; *ptr != '\n'; ptr++) {
3494 error(state, 0, "garbage after include directive");
3497 if (state->if_value >= 0) {
3498 compile_file(state, name, local);
3501 next_token(state, index);
3505 /* Ignore # without a following ident */
3506 if (tk->tok == TOK_IDENT) {
3507 error(state, 0, "Invalid preprocessor directive: %s",
3512 /* Consume the rest of the macro line */
3514 tok = mpeek(state, index);
3515 meat(state, index, tok);
3516 } while(tok != TOK_EOF);
3520 static void token(struct compile_state *state, int index)
3522 struct file_state *file;
3526 tk = &state->token[index];
3527 next_token(state, index);
3531 if (tk->tok == TOK_EOF && file->prev) {
3532 state->file = file->prev;
3533 /* file->basename is used keep it */
3534 xfree(file->dirname);
3537 next_token(state, index);
3540 else if (tk->tok == TOK_MACRO) {
3541 preprocess(state, index);
3544 else if (tk->ident && tk->ident->sym_define) {
3545 compile_macro(state, tk);
3546 next_token(state, index);
3549 else if (state->if_value < 0) {
3550 next_token(state, index);
3556 static int peek(struct compile_state *state)
3558 if (state->token[1].tok == -1) {
3561 return state->token[1].tok;
3564 static int peek2(struct compile_state *state)
3566 if (state->token[1].tok == -1) {
3569 if (state->token[2].tok == -1) {
3572 return state->token[2].tok;
3575 static void eat(struct compile_state *state, int tok)
3579 next_tok = peek(state);
3580 if (next_tok != tok) {
3581 const char *name1, *name2;
3582 name1 = tokens[next_tok];
3584 if (next_tok == TOK_IDENT) {
3585 name2 = state->token[1].ident->name;
3587 error(state, 0, "\tfound %s %s expected %s",
3588 name1, name2 ,tokens[tok]);
3590 /* Free the old token value */
3591 if (state->token[0].str_len) {
3592 xfree((void *)(state->token[0].val.str));
3594 for(i = 0; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
3595 state->token[i] = state->token[i + 1];
3597 memset(&state->token[i], 0, sizeof(state->token[i]));
3598 state->token[i].tok = -1;
3601 #warning "FIXME do not hardcode the include paths"
3602 static char *include_paths[] = {
3603 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/include",
3604 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/arch/i386/include",
3605 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src",
3609 static void compile_file(struct compile_state *state, const char *filename, int local)
3612 const char *subdir, *base;
3614 struct file_state *file;
3616 file = xmalloc(sizeof(*file), "file_state");
3618 base = strrchr(filename, '/');
3621 subdir_len = base - filename;
3628 basename = xmalloc(strlen(base) +1, "basename");
3629 strcpy(basename, base);
3630 file->basename = basename;
3632 if (getcwd(cwd, sizeof(cwd)) == 0) {
3633 die("cwd buffer to small");
3636 if (subdir[0] == '/') {
3637 file->dirname = xmalloc(subdir_len + 1, "dirname");
3638 memcpy(file->dirname, subdir, subdir_len);
3639 file->dirname[subdir_len] = '\0';
3645 /* Find the appropriate directory... */
3647 if (!state->file && exists(cwd, filename)) {
3650 if (local && state->file && exists(state->file->dirname, filename)) {
3651 dir = state->file->dirname;
3653 for(path = include_paths; !dir && *path; path++) {
3654 if (exists(*path, filename)) {
3659 error(state, 0, "Cannot find `%s'\n", filename);
3661 dirlen = strlen(dir);
3662 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
3663 memcpy(file->dirname, dir, dirlen);
3664 file->dirname[dirlen] = '/';
3665 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
3666 file->dirname[dirlen + 1 + subdir_len] = '\0';
3668 file->buf = slurp_file(file->dirname, file->basename, &file->size);
3671 file->pos = file->buf;
3672 file->line_start = file->pos;
3675 file->report_line = 1;
3676 file->report_name = file->basename;
3677 file->report_dir = file->dirname;
3679 file->prev = state->file;
3682 process_trigraphs(state);
3683 splice_lines(state);
3686 /* Type helper functions */
3688 static struct type *new_type(
3689 unsigned int type, struct type *left, struct type *right)
3691 struct type *result;
3692 result = xmalloc(sizeof(*result), "type");
3693 result->type = type;
3694 result->left = left;
3695 result->right = right;
3696 result->field_ident = 0;
3697 result->type_ident = 0;
3701 static struct type *clone_type(unsigned int specifiers, struct type *old)
3703 struct type *result;
3704 result = xmalloc(sizeof(*result), "type");
3705 memcpy(result, old, sizeof(*result));
3706 result->type &= TYPE_MASK;
3707 result->type |= specifiers;
3711 #define SIZEOF_SHORT 2
3712 #define SIZEOF_INT 4
3713 #define SIZEOF_LONG (sizeof(long_t))
3715 #define ALIGNOF_SHORT 2
3716 #define ALIGNOF_INT 4
3717 #define ALIGNOF_LONG (sizeof(long_t))
3719 #define MASK_UCHAR(X) ((X) & ((ulong_t)0xff))
3720 #define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT*8)) - 1))
3721 static inline ulong_t mask_uint(ulong_t x)
3723 if (SIZEOF_INT < SIZEOF_LONG) {
3724 ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT*8))) -1;
3729 #define MASK_UINT(X) (mask_uint(X))
3730 #define MASK_ULONG(X) (X)
3732 static struct type void_type = { .type = TYPE_VOID };
3733 static struct type char_type = { .type = TYPE_CHAR };
3734 static struct type uchar_type = { .type = TYPE_UCHAR };
3735 static struct type short_type = { .type = TYPE_SHORT };
3736 static struct type ushort_type = { .type = TYPE_USHORT };
3737 static struct type int_type = { .type = TYPE_INT };
3738 static struct type uint_type = { .type = TYPE_UINT };
3739 static struct type long_type = { .type = TYPE_LONG };
3740 static struct type ulong_type = { .type = TYPE_ULONG };
3742 static struct triple *variable(struct compile_state *state, struct type *type)
3744 struct triple *result;
3745 if ((type->type & STOR_MASK) != STOR_PERM) {
3746 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3747 result = triple(state, OP_ADECL, type, 0, 0);
3750 struct triple **vector;
3752 result = new_triple(state, OP_VAL_VEC, type, -1, -1);
3753 vector = &result->param[0];
3757 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
3758 vector[index] = variable(state, field->left);
3759 field = field->right;
3762 vector[index] = variable(state, field);
3766 result = triple(state, OP_SDECL, type, 0, 0);
3771 static void stor_of(FILE *fp, struct type *type)
3773 switch(type->type & STOR_MASK) {
3775 fprintf(fp, "auto ");
3778 fprintf(fp, "static ");
3781 fprintf(fp, "extern ");
3784 fprintf(fp, "register ");
3787 fprintf(fp, "typedef ");
3790 fprintf(fp, "inline ");
3794 static void qual_of(FILE *fp, struct type *type)
3796 if (type->type & QUAL_CONST) {
3797 fprintf(fp, " const");
3799 if (type->type & QUAL_VOLATILE) {
3800 fprintf(fp, " volatile");
3802 if (type->type & QUAL_RESTRICT) {
3803 fprintf(fp, " restrict");
3807 static void name_of(FILE *fp, struct type *type)
3810 switch(type->type & TYPE_MASK) {
3812 fprintf(fp, "void");
3816 fprintf(fp, "signed char");
3820 fprintf(fp, "unsigned char");
3824 fprintf(fp, "signed short");
3828 fprintf(fp, "unsigned short");
3832 fprintf(fp, "signed int");
3836 fprintf(fp, "unsigned int");
3840 fprintf(fp, "signed long");
3844 fprintf(fp, "unsigned long");
3848 name_of(fp, type->left);
3854 name_of(fp, type->left);
3856 name_of(fp, type->right);
3859 fprintf(fp, "enum %s", type->type_ident->name);
3863 fprintf(fp, "struct %s", type->type_ident->name);
3868 name_of(fp, type->left);
3869 fprintf(fp, " (*)(");
3870 name_of(fp, type->right);
3875 name_of(fp, type->left);
3876 fprintf(fp, " [%ld]", type->elements);
3879 fprintf(fp, "????: %x", type->type & TYPE_MASK);
3884 static size_t align_of(struct compile_state *state, struct type *type)
3888 switch(type->type & TYPE_MASK) {
3898 align = ALIGNOF_SHORT;
3903 align = ALIGNOF_INT;
3908 align = ALIGNOF_LONG;
3913 size_t left_align, right_align;
3914 left_align = align_of(state, type->left);
3915 right_align = align_of(state, type->right);
3916 align = (left_align >= right_align) ? left_align : right_align;
3920 align = align_of(state, type->left);
3923 align = align_of(state, type->left);
3926 error(state, 0, "alignof not yet defined for type\n");
3932 static size_t size_of(struct compile_state *state, struct type *type)
3936 switch(type->type & TYPE_MASK) {
3946 size = SIZEOF_SHORT;
3961 size = size_of(state, type->left);
3962 while((type->right->type & TYPE_MASK) == TYPE_PRODUCT) {
3964 align = align_of(state, type->left);
3965 pad = align - (size % align);
3966 size = size + pad + size_of(state, type->left);
3968 align = align_of(state, type->right);
3969 pad = align - (size % align);
3970 size = size + pad + sizeof(type->right);
3975 size_t size_left, size_right;
3976 size_left = size_of(state, type->left);
3977 size_right = size_of(state, type->right);
3978 size = (size_left >= size_right)? size_left : size_right;
3982 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
3983 internal_error(state, 0, "Invalid array type");
3985 size = size_of(state, type->left) * type->elements;
3989 size = size_of(state, type->left);
3992 error(state, 0, "sizeof not yet defined for type\n");
3998 static size_t field_offset(struct compile_state *state,
3999 struct type *type, struct hash_entry *field)
4001 size_t size, align, pad;
4002 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4003 internal_error(state, 0, "field_offset only works on structures");
4007 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4008 if (type->left->field_ident == field) {
4011 size += size_of(state, type->left);
4013 align = align_of(state, type->left);
4014 pad = align - (size % align);
4017 if (type->field_ident != field) {
4018 internal_error(state, 0, "field_offset: member %s not present",
4024 static struct type *field_type(struct compile_state *state,
4025 struct type *type, struct hash_entry *field)
4027 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4028 internal_error(state, 0, "field_type only works on structures");
4031 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4032 if (type->left->field_ident == field) {
4038 if (type->field_ident != field) {
4039 internal_error(state, 0, "field_type: member %s not present",
4045 static struct triple *struct_field(struct compile_state *state,
4046 struct triple *decl, struct hash_entry *field)
4048 struct triple **vector;
4052 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4055 if (decl->op != OP_VAL_VEC) {
4056 internal_error(state, 0, "Invalid struct variable");
4059 internal_error(state, 0, "Missing structure field");
4062 vector = &RHS(decl, 0);
4064 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4065 if (type->left->field_ident == field) {
4072 if (type->field_ident != field) {
4073 internal_error(state, 0, "field %s not found?", field->name);
4075 return vector[index];
4078 static void arrays_complete(struct compile_state *state, struct type *type)
4080 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
4081 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4082 error(state, 0, "array size not specified");
4084 arrays_complete(state, type->left);
4088 static unsigned int do_integral_promotion(unsigned int type)
4091 if (TYPE_INTEGER(type) &&
4092 TYPE_RANK(type) < TYPE_RANK(TYPE_INT)) {
4098 static unsigned int do_arithmetic_conversion(
4099 unsigned int left, unsigned int right)
4103 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
4104 return TYPE_LDOUBLE;
4106 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
4109 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
4112 left = do_integral_promotion(left);
4113 right = do_integral_promotion(right);
4114 /* If both operands have the same size done */
4115 if (left == right) {
4118 /* If both operands have the same signedness pick the larger */
4119 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
4120 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
4122 /* If the signed type can hold everything use it */
4123 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
4126 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
4129 /* Convert to the unsigned type with the same rank as the signed type */
4130 else if (TYPE_SIGNED(left)) {
4131 return TYPE_MKUNSIGNED(left);
4134 return TYPE_MKUNSIGNED(right);
4138 /* see if two types are the same except for qualifiers */
4139 static int equiv_types(struct type *left, struct type *right)
4142 /* Error if the basic types do not match */
4143 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4146 type = left->type & TYPE_MASK;
4147 /* if the basic types match and it is an arithmetic type we are done */
4148 if (TYPE_ARITHMETIC(type)) {
4151 /* If it is a pointer type recurse and keep testing */
4152 if (type == TYPE_POINTER) {
4153 return equiv_types(left->left, right->left);
4155 else if (type == TYPE_ARRAY) {
4156 return (left->elements == right->elements) &&
4157 equiv_types(left->left, right->left);
4159 /* test for struct/union equality */
4160 else if (type == TYPE_STRUCT) {
4161 return left->type_ident == right->type_ident;
4163 /* Test for equivalent functions */
4164 else if (type == TYPE_FUNCTION) {
4165 return equiv_types(left->left, right->left) &&
4166 equiv_types(left->right, right->right);
4168 /* We only see TYPE_PRODUCT as part of function equivalence matching */
4169 else if (type == TYPE_PRODUCT) {
4170 return equiv_types(left->left, right->left) &&
4171 equiv_types(left->right, right->right);
4173 /* We should see TYPE_OVERLAP */
4179 static int equiv_ptrs(struct type *left, struct type *right)
4181 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4182 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4185 return equiv_types(left->left, right->left);
4188 static struct type *compatible_types(struct type *left, struct type *right)
4190 struct type *result;
4191 unsigned int type, qual_type;
4192 /* Error if the basic types do not match */
4193 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4196 type = left->type & TYPE_MASK;
4197 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4199 /* if the basic types match and it is an arithmetic type we are done */
4200 if (TYPE_ARITHMETIC(type)) {
4201 result = new_type(qual_type, 0, 0);
4203 /* If it is a pointer type recurse and keep testing */
4204 else if (type == TYPE_POINTER) {
4205 result = compatible_types(left->left, right->left);
4207 result = new_type(qual_type, result, 0);
4210 /* test for struct/union equality */
4211 else if (type == TYPE_STRUCT) {
4212 if (left->type_ident == right->type_ident) {
4216 /* Test for equivalent functions */
4217 else if (type == TYPE_FUNCTION) {
4218 struct type *lf, *rf;
4219 lf = compatible_types(left->left, right->left);
4220 rf = compatible_types(left->right, right->right);
4222 result = new_type(qual_type, lf, rf);
4225 /* We only see TYPE_PRODUCT as part of function equivalence matching */
4226 else if (type == TYPE_PRODUCT) {
4227 struct type *lf, *rf;
4228 lf = compatible_types(left->left, right->left);
4229 rf = compatible_types(left->right, right->right);
4231 result = new_type(qual_type, lf, rf);
4235 /* Nothing else is compatible */
4240 static struct type *compatible_ptrs(struct type *left, struct type *right)
4242 struct type *result;
4243 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4244 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4247 result = compatible_types(left->left, right->left);
4249 unsigned int qual_type;
4250 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4251 result = new_type(qual_type, result, 0);
4256 static struct triple *integral_promotion(
4257 struct compile_state *state, struct triple *def)
4261 /* As all operations are carried out in registers
4262 * the values are converted on load I just convert
4263 * logical type of the operand.
4265 if (TYPE_INTEGER(type->type)) {
4266 unsigned int int_type;
4267 int_type = type->type & ~TYPE_MASK;
4268 int_type |= do_integral_promotion(type->type);
4269 if (int_type != type->type) {
4270 def->type = new_type(int_type, 0, 0);
4277 static void arithmetic(struct compile_state *state, struct triple *def)
4279 if (!TYPE_ARITHMETIC(def->type->type)) {
4280 error(state, 0, "arithmetic type expexted");
4284 static void ptr_arithmetic(struct compile_state *state, struct triple *def)
4286 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
4287 error(state, def, "pointer or arithmetic type expected");
4291 static int is_integral(struct triple *ins)
4293 return TYPE_INTEGER(ins->type->type);
4296 static void integral(struct compile_state *state, struct triple *def)
4298 if (!is_integral(def)) {
4299 error(state, 0, "integral type expected");
4304 static void bool(struct compile_state *state, struct triple *def)
4306 if (!TYPE_ARITHMETIC(def->type->type) &&
4307 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
4308 error(state, 0, "arithmetic or pointer type expected");
4312 static int is_signed(struct type *type)
4314 return !!TYPE_SIGNED(type->type);
4317 /* Is this value located in a register otherwise it must be in memory */
4318 static int is_in_reg(struct compile_state *state, struct triple *def)
4321 if (def->op == OP_ADECL) {
4324 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
4327 else if (def->op == OP_VAL_VEC) {
4328 in_reg = is_in_reg(state, RHS(def, 0));
4330 else if (def->op == OP_DOT) {
4331 in_reg = is_in_reg(state, RHS(def, 0));
4334 internal_error(state, 0, "unknown expr storage location");
4340 /* Is this a stable variable location otherwise it must be a temporary */
4341 static int is_stable(struct compile_state *state, struct triple *def)
4348 if ((def->op == OP_ADECL) ||
4349 (def->op == OP_SDECL) ||
4350 (def->op == OP_DEREF) ||
4351 (def->op == OP_BLOBCONST)) {
4354 else if (def->op == OP_DOT) {
4355 ret = is_stable(state, RHS(def, 0));
4357 else if (def->op == OP_VAL_VEC) {
4358 struct triple **vector;
4361 vector = &RHS(def, 0);
4362 for(i = 0; i < def->type->elements; i++) {
4363 if (!is_stable(state, vector[i])) {
4372 static int is_lvalue(struct compile_state *state, struct triple *def)
4379 if (!is_stable(state, def)) {
4382 if (def->type->type & QUAL_CONST) {
4385 else if (def->op == OP_DOT) {
4386 ret = is_lvalue(state, RHS(def, 0));
4391 static void lvalue(struct compile_state *state, struct triple *def)
4394 internal_error(state, def, "nothing where lvalue expected?");
4396 if (!is_lvalue(state, def)) {
4397 error(state, def, "lvalue expected");
4401 static int is_pointer(struct triple *def)
4403 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
4406 static void pointer(struct compile_state *state, struct triple *def)
4408 if (!is_pointer(def)) {
4409 error(state, def, "pointer expected");
4413 static struct triple *int_const(
4414 struct compile_state *state, struct type *type, ulong_t value)
4416 struct triple *result;
4417 switch(type->type & TYPE_MASK) {
4419 case TYPE_INT: case TYPE_UINT:
4420 case TYPE_LONG: case TYPE_ULONG:
4423 internal_error(state, 0, "constant for unkown type");
4425 result = triple(state, OP_INTCONST, type, 0, 0);
4426 result->u.cval = value;
4431 static struct triple *do_mk_addr_expr(struct compile_state *state,
4432 struct triple *expr, struct type *type, ulong_t offset)
4434 struct triple *result;
4435 lvalue(state, expr);
4438 if (expr->op == OP_ADECL) {
4439 error(state, expr, "address of auto variables not supported");
4441 else if (expr->op == OP_SDECL) {
4442 result = triple(state, OP_ADDRCONST, type, 0, 0);
4443 MISC(result, 0) = expr;
4444 result->u.cval = offset;
4446 else if (expr->op == OP_DEREF) {
4447 result = triple(state, OP_ADD, type,
4449 int_const(state, &ulong_type, offset));
4454 static struct triple *mk_addr_expr(
4455 struct compile_state *state, struct triple *expr, ulong_t offset)
4460 TYPE_POINTER | (expr->type->type & QUAL_MASK),
4463 return do_mk_addr_expr(state, expr, type, offset);
4466 static struct triple *mk_deref_expr(
4467 struct compile_state *state, struct triple *expr)
4469 struct type *base_type;
4470 pointer(state, expr);
4471 base_type = expr->type->left;
4472 if (!TYPE_PTR(base_type->type) && !TYPE_ARITHMETIC(base_type->type)) {
4474 "Only pointer and arithmetic values can be dereferenced");
4476 return triple(state, OP_DEREF, base_type, expr, 0);
4479 static struct triple *deref_field(
4480 struct compile_state *state, struct triple *expr, struct hash_entry *field)
4482 struct triple *result;
4483 struct type *type, *member;
4485 internal_error(state, 0, "No field passed to deref_field");
4489 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4490 error(state, 0, "request for member %s in something not a struct or union",
4493 member = type->left;
4494 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4495 if (member->left->field_ident == field) {
4496 member = member->left;
4499 member = member->right;
4501 if (member->field_ident != field) {
4502 error(state, 0, "%s is not a member", field->name);
4504 if ((type->type & STOR_MASK) == STOR_PERM) {
4505 /* Do the pointer arithmetic to get a deref the field */
4507 offset = field_offset(state, type, field);
4508 result = do_mk_addr_expr(state, expr, member, offset);
4509 result = mk_deref_expr(state, result);
4512 /* Find the variable for the field I want. */
4513 result = triple(state, OP_DOT,
4514 field_type(state, type, field), expr, 0);
4515 result->u.field = field;
4520 static struct triple *read_expr(struct compile_state *state, struct triple *def)
4526 if (!is_stable(state, def)) {
4529 /* Tranform an array to a pointer to the first element */
4530 #warning "CHECK_ME is this the right place to transform arrays to pointers?"
4531 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
4533 struct triple *result;
4535 TYPE_POINTER | (def->type->type & QUAL_MASK),
4536 def->type->left, 0);
4537 result = triple(state, OP_ADDRCONST, type, 0, 0);
4538 MISC(result, 0) = def;
4541 if (is_in_reg(state, def)) {
4546 return triple(state, op, def->type, def, 0);
4549 static void write_compatible(struct compile_state *state,
4550 struct type *dest, struct type *rval)
4553 /* Both operands have arithmetic type */
4554 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
4557 /* One operand is a pointer and the other is a pointer to void */
4558 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
4559 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
4560 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
4561 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
4564 /* If both types are the same without qualifiers we are good */
4565 else if (equiv_ptrs(dest, rval)) {
4568 /* test for struct/union equality */
4569 else if (((dest->type & TYPE_MASK) == TYPE_STRUCT) &&
4570 ((rval->type & TYPE_MASK) == TYPE_STRUCT) &&
4571 (dest->type_ident == rval->type_ident)) {
4575 error(state, 0, "Incompatible types in assignment");
4579 static struct triple *write_expr(
4580 struct compile_state *state, struct triple *dest, struct triple *rval)
4587 internal_error(state, 0, "missing rval");
4590 if (rval->op == OP_LIST) {
4591 internal_error(state, 0, "expression of type OP_LIST?");
4593 if (!is_lvalue(state, dest)) {
4594 internal_error(state, 0, "writing to a non lvalue?");
4597 write_compatible(state, dest->type, rval->type);
4599 /* Now figure out which assignment operator to use */
4601 if (is_in_reg(state, dest)) {
4606 def = triple(state, op, dest->type, dest, rval);
4610 static struct triple *init_expr(
4611 struct compile_state *state, struct triple *dest, struct triple *rval)
4617 internal_error(state, 0, "missing rval");
4619 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
4620 rval = read_expr(state, rval);
4621 def = write_expr(state, dest, rval);
4624 /* Fill in the array size if necessary */
4625 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
4626 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
4627 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4628 dest->type->elements = rval->type->elements;
4631 if (!equiv_types(dest->type, rval->type)) {
4632 error(state, 0, "Incompatible types in inializer");
4634 MISC(dest, 0) = rval;
4635 insert_triple(state, dest, rval);
4636 rval->id |= TRIPLE_FLAG_FLATTENED;
4637 use_triple(MISC(dest, 0), dest);
4642 struct type *arithmetic_result(
4643 struct compile_state *state, struct triple *left, struct triple *right)
4646 /* Sanity checks to ensure I am working with arithmetic types */
4647 arithmetic(state, left);
4648 arithmetic(state, right);
4650 do_arithmetic_conversion(
4652 right->type->type), 0, 0);
4656 struct type *ptr_arithmetic_result(
4657 struct compile_state *state, struct triple *left, struct triple *right)
4660 /* Sanity checks to ensure I am working with the proper types */
4661 ptr_arithmetic(state, left);
4662 arithmetic(state, right);
4663 if (TYPE_ARITHMETIC(left->type->type) &&
4664 TYPE_ARITHMETIC(right->type->type)) {
4665 type = arithmetic_result(state, left, right);
4667 else if (TYPE_PTR(left->type->type)) {
4671 internal_error(state, 0, "huh?");
4678 /* boolean helper function */
4680 static struct triple *ltrue_expr(struct compile_state *state,
4681 struct triple *expr)
4684 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
4685 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
4686 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
4687 /* If the expression is already boolean do nothing */
4690 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
4696 static struct triple *lfalse_expr(struct compile_state *state,
4697 struct triple *expr)
4699 return triple(state, OP_LFALSE, &int_type, expr, 0);
4702 static struct triple *cond_expr(
4703 struct compile_state *state,
4704 struct triple *test, struct triple *left, struct triple *right)
4707 struct type *result_type;
4708 unsigned int left_type, right_type;
4710 left_type = left->type->type;
4711 right_type = right->type->type;
4713 /* Both operands have arithmetic type */
4714 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
4715 result_type = arithmetic_result(state, left, right);
4717 /* Both operands have void type */
4718 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
4719 ((right_type & TYPE_MASK) == TYPE_VOID)) {
4720 result_type = &void_type;
4722 /* pointers to the same type... */
4723 else if ((result_type = compatible_ptrs(left->type, right->type))) {
4726 /* Both operands are pointers and left is a pointer to void */
4727 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4728 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4729 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4730 result_type = right->type;
4732 /* Both operands are pointers and right is a pointer to void */
4733 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4734 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4735 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4736 result_type = left->type;
4739 error(state, 0, "Incompatible types in conditional expression");
4741 /* Cleanup and invert the test */
4742 test = lfalse_expr(state, read_expr(state, test));
4743 def = new_triple(state, OP_COND, result_type, 0, 3);
4744 def->param[0] = test;
4745 def->param[1] = left;
4746 def->param[2] = right;
4751 static int expr_depth(struct compile_state *state, struct triple *ins)
4755 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
4758 else if (ins->op == OP_DEREF) {
4759 count = expr_depth(state, RHS(ins, 0)) - 1;
4761 else if (ins->op == OP_VAL) {
4762 count = expr_depth(state, RHS(ins, 0)) - 1;
4764 else if (ins->op == OP_COMMA) {
4766 ldepth = expr_depth(state, RHS(ins, 0));
4767 rdepth = expr_depth(state, RHS(ins, 1));
4768 count = (ldepth >= rdepth)? ldepth : rdepth;
4770 else if (ins->op == OP_CALL) {
4771 /* Don't figure the depth of a call just guess it is huge */
4775 struct triple **expr;
4776 expr = triple_rhs(state, ins, 0);
4777 for(;expr; expr = triple_rhs(state, ins, expr)) {
4780 depth = expr_depth(state, *expr);
4781 if (depth > count) {
4790 static struct triple *flatten(
4791 struct compile_state *state, struct triple *first, struct triple *ptr);
4793 static struct triple *flatten_generic(
4794 struct compile_state *state, struct triple *first, struct triple *ptr)
4798 struct triple **ins;
4801 /* Only operations with just a rhs should come here */
4802 rhs = TRIPLE_RHS(ptr->sizes);
4803 lhs = TRIPLE_LHS(ptr->sizes);
4804 if (TRIPLE_SIZE(ptr->sizes) != lhs + rhs) {
4805 internal_error(state, ptr, "unexpected args for: %d %s",
4806 ptr->op, tops(ptr->op));
4808 /* Find the depth of the rhs elements */
4809 for(i = 0; i < rhs; i++) {
4810 vector[i].ins = &RHS(ptr, i);
4811 vector[i].depth = expr_depth(state, *vector[i].ins);
4813 /* Selection sort the rhs */
4814 for(i = 0; i < rhs; i++) {
4816 for(j = i + 1; j < rhs; j++ ) {
4817 if (vector[j].depth > vector[max].depth) {
4822 struct rhs_vector tmp;
4824 vector[i] = vector[max];
4828 /* Now flatten the rhs elements */
4829 for(i = 0; i < rhs; i++) {
4830 *vector[i].ins = flatten(state, first, *vector[i].ins);
4831 use_triple(*vector[i].ins, ptr);
4834 /* Now flatten the lhs elements */
4835 for(i = 0; i < lhs; i++) {
4836 struct triple **ins = &LHS(ptr, i);
4837 *ins = flatten(state, first, *ins);
4838 use_triple(*ins, ptr);
4843 static struct triple *flatten_land(
4844 struct compile_state *state, struct triple *first, struct triple *ptr)
4846 struct triple *left, *right;
4847 struct triple *val, *test, *jmp, *label1, *end;
4849 /* Find the triples */
4851 right = RHS(ptr, 1);
4853 /* Generate the needed triples */
4856 /* Thread the triples together */
4857 val = flatten(state, first, variable(state, ptr->type));
4858 left = flatten(state, first, write_expr(state, val, left));
4859 test = flatten(state, first,
4860 lfalse_expr(state, read_expr(state, val)));
4861 jmp = flatten(state, first, branch(state, end, test));
4862 label1 = flatten(state, first, label(state));
4863 right = flatten(state, first, write_expr(state, val, right));
4864 TARG(jmp, 0) = flatten(state, first, end);
4866 /* Now give the caller something to chew on */
4867 return read_expr(state, val);
4870 static struct triple *flatten_lor(
4871 struct compile_state *state, struct triple *first, struct triple *ptr)
4873 struct triple *left, *right;
4874 struct triple *val, *jmp, *label1, *end;
4876 /* Find the triples */
4878 right = RHS(ptr, 1);
4880 /* Generate the needed triples */
4883 /* Thread the triples together */
4884 val = flatten(state, first, variable(state, ptr->type));
4885 left = flatten(state, first, write_expr(state, val, left));
4886 jmp = flatten(state, first, branch(state, end, left));
4887 label1 = flatten(state, first, label(state));
4888 right = flatten(state, first, write_expr(state, val, right));
4889 TARG(jmp, 0) = flatten(state, first, end);
4892 /* Now give the caller something to chew on */
4893 return read_expr(state, val);
4896 static struct triple *flatten_cond(
4897 struct compile_state *state, struct triple *first, struct triple *ptr)
4899 struct triple *test, *left, *right;
4900 struct triple *val, *mv1, *jmp1, *label1, *mv2, *middle, *jmp2, *end;
4902 /* Find the triples */
4905 right = RHS(ptr, 2);
4907 /* Generate the needed triples */
4909 middle = label(state);
4911 /* Thread the triples together */
4912 val = flatten(state, first, variable(state, ptr->type));
4913 test = flatten(state, first, test);
4914 jmp1 = flatten(state, first, branch(state, middle, test));
4915 label1 = flatten(state, first, label(state));
4916 left = flatten(state, first, left);
4917 mv1 = flatten(state, first, write_expr(state, val, left));
4918 jmp2 = flatten(state, first, branch(state, end, 0));
4919 TARG(jmp1, 0) = flatten(state, first, middle);
4920 right = flatten(state, first, right);
4921 mv2 = flatten(state, first, write_expr(state, val, right));
4922 TARG(jmp2, 0) = flatten(state, first, end);
4924 /* Now give the caller something to chew on */
4925 return read_expr(state, val);
4928 struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
4929 struct occurance *base_occurance)
4931 struct triple *nfunc;
4932 struct triple *nfirst, *ofirst;
4933 struct triple *new, *old;
4936 fprintf(stdout, "\n");
4937 loc(stdout, state, 0);
4938 fprintf(stdout, "\n__________ copy_func _________\n");
4939 print_triple(state, ofunc);
4940 fprintf(stdout, "__________ copy_func _________ done\n\n");
4943 /* Make a new copy of the old function */
4944 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
4946 ofirst = old = RHS(ofunc, 0);
4949 struct occurance *occurance;
4950 int old_lhs, old_rhs;
4951 old_lhs = TRIPLE_LHS(old->sizes);
4952 old_rhs = TRIPLE_RHS(old->sizes);
4953 occurance = inline_occurance(state, base_occurance, old->occurance);
4954 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
4956 if (!triple_stores_block(state, new)) {
4957 memcpy(&new->u, &old->u, sizeof(new->u));
4960 RHS(nfunc, 0) = nfirst = new;
4963 insert_triple(state, nfirst, new);
4965 new->id |= TRIPLE_FLAG_FLATTENED;
4967 /* During the copy remember new as user of old */
4968 use_triple(old, new);
4970 /* Populate the return type if present */
4971 if (old == MISC(ofunc, 0)) {
4972 MISC(nfunc, 0) = new;
4975 } while(old != ofirst);
4977 /* Make a second pass to fix up any unresolved references */
4981 struct triple **oexpr, **nexpr;
4983 /* Lookup where the copy is, to join pointers */
4984 count = TRIPLE_SIZE(old->sizes);
4985 for(i = 0; i < count; i++) {
4986 oexpr = &old->param[i];
4987 nexpr = &new->param[i];
4988 if (!*nexpr && *oexpr && (*oexpr)->use) {
4989 *nexpr = (*oexpr)->use->member;
4990 if (*nexpr == old) {
4991 internal_error(state, 0, "new == old?");
4993 use_triple(*nexpr, new);
4995 if (!*nexpr && *oexpr) {
4996 internal_error(state, 0, "Could not copy %d\n", i);
5001 } while((old != ofirst) && (new != nfirst));
5003 /* Make a third pass to cleanup the extra useses */
5007 unuse_triple(old, new);
5010 } while ((old != ofirst) && (new != nfirst));
5014 static struct triple *flatten_call(
5015 struct compile_state *state, struct triple *first, struct triple *ptr)
5017 /* Inline the function call */
5019 struct triple *ofunc, *nfunc, *nfirst, *param, *result;
5020 struct triple *end, *nend;
5023 /* Find the triples */
5024 ofunc = MISC(ptr, 0);
5025 if (ofunc->op != OP_LIST) {
5026 internal_error(state, 0, "improper function");
5028 nfunc = copy_func(state, ofunc, ptr->occurance);
5029 nfirst = RHS(nfunc, 0)->next;
5030 /* Prepend the parameter reading into the new function list */
5031 ptype = nfunc->type->right;
5032 param = RHS(nfunc, 0)->next;
5033 pvals = TRIPLE_RHS(ptr->sizes);
5034 for(i = 0; i < pvals; i++) {
5038 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
5039 atype = ptype->left;
5041 while((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
5042 param = param->next;
5045 flatten(state, nfirst, write_expr(state, param, arg));
5046 ptype = ptype->right;
5047 param = param->next;
5050 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
5051 result = read_expr(state, MISC(nfunc,0));
5054 fprintf(stdout, "\n");
5055 loc(stdout, state, 0);
5056 fprintf(stdout, "\n__________ flatten_call _________\n");
5057 print_triple(state, nfunc);
5058 fprintf(stdout, "__________ flatten_call _________ done\n\n");
5061 /* Get rid of the extra triples */
5062 nfirst = RHS(nfunc, 0)->next;
5063 free_triple(state, RHS(nfunc, 0));
5065 free_triple(state, nfunc);
5067 /* Append the new function list onto the return list */
5069 nend = nfirst->prev;
5078 static struct triple *flatten(
5079 struct compile_state *state, struct triple *first, struct triple *ptr)
5081 struct triple *orig_ptr;
5086 /* Only flatten triples once */
5087 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
5093 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5094 LHS(ptr, 0) = flatten(state, first, LHS(ptr, 0));
5095 use_triple(LHS(ptr, 0), ptr);
5096 use_triple(RHS(ptr, 0), ptr);
5099 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5103 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5104 return MISC(ptr, 0);
5107 ptr = flatten_land(state, first, ptr);
5110 ptr = flatten_lor(state, first, ptr);
5113 ptr = flatten_cond(state, first, ptr);
5116 ptr = flatten_call(state, first, ptr);
5120 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5121 use_triple(RHS(ptr, 0), ptr);
5124 use_triple(TARG(ptr, 0), ptr);
5125 if (TRIPLE_RHS(ptr->sizes)) {
5126 use_triple(RHS(ptr, 0), ptr);
5127 if (ptr->next != ptr) {
5128 use_triple(ptr->next, ptr);
5133 insert_triple(state, first, ptr);
5134 ptr->id |= TRIPLE_FLAG_FLATTENED;
5135 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
5136 use_triple(MISC(ptr, 0), ptr);
5139 /* Since OP_DEREF is just a marker delete it when I flatten it */
5141 RHS(orig_ptr, 0) = 0;
5142 free_triple(state, orig_ptr);
5146 struct triple *base;
5148 base = flatten(state, first, base);
5149 if (base->op == OP_VAL_VEC) {
5150 ptr = struct_field(state, base, ptr->u.field);
5155 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5156 use_triple(MISC(ptr, 0), ptr);
5157 use_triple(ptr, MISC(ptr, 0));
5161 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5162 use_triple(MISC(ptr, 0), ptr);
5167 /* Flatten the easy cases we don't override */
5168 ptr = flatten_generic(state, first, ptr);
5171 } while(ptr && (ptr != orig_ptr));
5173 insert_triple(state, first, ptr);
5174 ptr->id |= TRIPLE_FLAG_FLATTENED;
5179 static void release_expr(struct compile_state *state, struct triple *expr)
5181 struct triple *head;
5182 head = label(state);
5183 flatten(state, head, expr);
5184 while(head->next != head) {
5185 release_triple(state, head->next);
5187 free_triple(state, head);
5190 static int replace_rhs_use(struct compile_state *state,
5191 struct triple *orig, struct triple *new, struct triple *use)
5193 struct triple **expr;
5196 expr = triple_rhs(state, use, 0);
5197 for(;expr; expr = triple_rhs(state, use, expr)) {
5198 if (*expr == orig) {
5204 unuse_triple(orig, use);
5205 use_triple(new, use);
5210 static int replace_lhs_use(struct compile_state *state,
5211 struct triple *orig, struct triple *new, struct triple *use)
5213 struct triple **expr;
5216 expr = triple_lhs(state, use, 0);
5217 for(;expr; expr = triple_lhs(state, use, expr)) {
5218 if (*expr == orig) {
5224 unuse_triple(orig, use);
5225 use_triple(new, use);
5230 static void propogate_use(struct compile_state *state,
5231 struct triple *orig, struct triple *new)
5233 struct triple_set *user, *next;
5234 for(user = orig->use; user; user = next) {
5240 found |= replace_rhs_use(state, orig, new, use);
5241 found |= replace_lhs_use(state, orig, new, use);
5243 internal_error(state, use, "use without use");
5247 internal_error(state, orig, "used after propogate_use");
5253 * ===========================
5256 static struct triple *mk_add_expr(
5257 struct compile_state *state, struct triple *left, struct triple *right)
5259 struct type *result_type;
5260 /* Put pointer operands on the left */
5261 if (is_pointer(right)) {
5267 left = read_expr(state, left);
5268 right = read_expr(state, right);
5269 result_type = ptr_arithmetic_result(state, left, right);
5270 if (is_pointer(left)) {
5271 right = triple(state,
5272 is_signed(right->type)? OP_SMUL : OP_UMUL,
5275 int_const(state, &ulong_type,
5276 size_of(state, left->type->left)));
5278 return triple(state, OP_ADD, result_type, left, right);
5281 static struct triple *mk_sub_expr(
5282 struct compile_state *state, struct triple *left, struct triple *right)
5284 struct type *result_type;
5285 result_type = ptr_arithmetic_result(state, left, right);
5286 left = read_expr(state, left);
5287 right = read_expr(state, right);
5288 if (is_pointer(left)) {
5289 right = triple(state,
5290 is_signed(right->type)? OP_SMUL : OP_UMUL,
5293 int_const(state, &ulong_type,
5294 size_of(state, left->type->left)));
5296 return triple(state, OP_SUB, result_type, left, right);
5299 static struct triple *mk_pre_inc_expr(
5300 struct compile_state *state, struct triple *def)
5304 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
5305 return triple(state, OP_VAL, def->type,
5306 write_expr(state, def, val),
5310 static struct triple *mk_pre_dec_expr(
5311 struct compile_state *state, struct triple *def)
5315 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
5316 return triple(state, OP_VAL, def->type,
5317 write_expr(state, def, val),
5321 static struct triple *mk_post_inc_expr(
5322 struct compile_state *state, struct triple *def)
5326 val = read_expr(state, def);
5327 return triple(state, OP_VAL, def->type,
5328 write_expr(state, def,
5329 mk_add_expr(state, val, int_const(state, &int_type, 1)))
5333 static struct triple *mk_post_dec_expr(
5334 struct compile_state *state, struct triple *def)
5338 val = read_expr(state, def);
5339 return triple(state, OP_VAL, def->type,
5340 write_expr(state, def,
5341 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
5345 static struct triple *mk_subscript_expr(
5346 struct compile_state *state, struct triple *left, struct triple *right)
5348 left = read_expr(state, left);
5349 right = read_expr(state, right);
5350 if (!is_pointer(left) && !is_pointer(right)) {
5351 error(state, left, "subscripted value is not a pointer");
5353 return mk_deref_expr(state, mk_add_expr(state, left, right));
5357 * Compile time evaluation
5358 * ===========================
5360 static int is_const(struct triple *ins)
5362 return IS_CONST_OP(ins->op);
5365 static int constants_equal(struct compile_state *state,
5366 struct triple *left, struct triple *right)
5369 if (!is_const(left) || !is_const(right)) {
5372 else if (left->op != right->op) {
5375 else if (!equiv_types(left->type, right->type)) {
5382 if (left->u.cval == right->u.cval) {
5388 size_t lsize, rsize;
5389 lsize = size_of(state, left->type);
5390 rsize = size_of(state, right->type);
5391 if (lsize != rsize) {
5394 if (memcmp(left->u.blob, right->u.blob, lsize) == 0) {
5400 if ((MISC(left, 0) == MISC(right, 0)) &&
5401 (left->u.cval == right->u.cval)) {
5406 internal_error(state, left, "uknown constant type");
5413 static int is_zero(struct triple *ins)
5415 return is_const(ins) && (ins->u.cval == 0);
5418 static int is_one(struct triple *ins)
5420 return is_const(ins) && (ins->u.cval == 1);
5423 static long_t bsr(ulong_t value)
5426 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
5437 static long_t bsf(ulong_t value)
5440 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
5451 static long_t log2(ulong_t value)
5456 static long_t tlog2(struct triple *ins)
5458 return log2(ins->u.cval);
5461 static int is_pow2(struct triple *ins)
5463 ulong_t value, mask;
5465 if (!is_const(ins)) {
5468 value = ins->u.cval;
5475 return ((value & mask) == value);
5478 static ulong_t read_const(struct compile_state *state,
5479 struct triple *ins, struct triple **expr)
5483 switch(rhs->type->type &TYPE_MASK) {
5495 internal_error(state, rhs, "bad type to read_const\n");
5501 static long_t read_sconst(struct triple *ins, struct triple **expr)
5505 return (long_t)(rhs->u.cval);
5508 static void unuse_rhs(struct compile_state *state, struct triple *ins)
5510 struct triple **expr;
5511 expr = triple_rhs(state, ins, 0);
5512 for(;expr;expr = triple_rhs(state, ins, expr)) {
5514 unuse_triple(*expr, ins);
5520 static void unuse_lhs(struct compile_state *state, struct triple *ins)
5522 struct triple **expr;
5523 expr = triple_lhs(state, ins, 0);
5524 for(;expr;expr = triple_lhs(state, ins, expr)) {
5525 unuse_triple(*expr, ins);
5530 static void check_lhs(struct compile_state *state, struct triple *ins)
5532 struct triple **expr;
5533 expr = triple_lhs(state, ins, 0);
5534 for(;expr;expr = triple_lhs(state, ins, expr)) {
5535 internal_error(state, ins, "unexpected lhs");
5539 static void check_targ(struct compile_state *state, struct triple *ins)
5541 struct triple **expr;
5542 expr = triple_targ(state, ins, 0);
5543 for(;expr;expr = triple_targ(state, ins, expr)) {
5544 internal_error(state, ins, "unexpected targ");
5548 static void wipe_ins(struct compile_state *state, struct triple *ins)
5550 /* Becareful which instructions you replace the wiped
5551 * instruction with, as there are not enough slots
5552 * in all instructions to hold all others.
5554 check_targ(state, ins);
5555 unuse_rhs(state, ins);
5556 unuse_lhs(state, ins);
5559 static void mkcopy(struct compile_state *state,
5560 struct triple *ins, struct triple *rhs)
5562 wipe_ins(state, ins);
5564 ins->sizes = TRIPLE_SIZES(0, 1, 0, 0);
5566 use_triple(RHS(ins, 0), ins);
5569 static void mkconst(struct compile_state *state,
5570 struct triple *ins, ulong_t value)
5572 if (!is_integral(ins) && !is_pointer(ins)) {
5573 internal_error(state, ins, "unknown type to make constant\n");
5575 wipe_ins(state, ins);
5576 ins->op = OP_INTCONST;
5577 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
5578 ins->u.cval = value;
5581 static void mkaddr_const(struct compile_state *state,
5582 struct triple *ins, struct triple *sdecl, ulong_t value)
5584 wipe_ins(state, ins);
5585 ins->op = OP_ADDRCONST;
5586 ins->sizes = TRIPLE_SIZES(0, 0, 1, 0);
5587 MISC(ins, 0) = sdecl;
5588 ins->u.cval = value;
5589 use_triple(sdecl, ins);
5592 /* Transform multicomponent variables into simple register variables */
5593 static void flatten_structures(struct compile_state *state)
5595 struct triple *ins, *first;
5596 first = RHS(state->main_function, 0);
5598 /* Pass one expand structure values into valvecs.
5602 struct triple *next;
5604 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5605 if (ins->op == OP_VAL_VEC) {
5608 else if ((ins->op == OP_LOAD) || (ins->op == OP_READ)) {
5609 struct triple *def, **vector;
5616 get_occurance(ins->occurance);
5617 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
5620 vector = &RHS(next, 0);
5621 tptr = next->type->left;
5622 for(i = 0; i < next->type->elements; i++) {
5623 struct triple *sfield;
5626 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5627 mtype = mtype->left;
5629 sfield = deref_field(state, def, mtype->field_ident);
5632 state, op, mtype, sfield, 0);
5633 put_occurance(vector[i]->occurance);
5634 get_occurance(next->occurance);
5635 vector[i]->occurance = next->occurance;
5638 propogate_use(state, ins, next);
5639 flatten(state, ins, next);
5640 free_triple(state, ins);
5642 else if ((ins->op == OP_STORE) || (ins->op == OP_WRITE)) {
5643 struct triple *src, *dst, **vector;
5651 get_occurance(ins->occurance);
5652 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
5655 vector = &RHS(next, 0);
5656 tptr = next->type->left;
5657 for(i = 0; i < ins->type->elements; i++) {
5658 struct triple *dfield, *sfield;
5661 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5662 mtype = mtype->left;
5664 sfield = deref_field(state, src, mtype->field_ident);
5665 dfield = deref_field(state, dst, mtype->field_ident);
5667 state, op, mtype, dfield, sfield);
5668 put_occurance(vector[i]->occurance);
5669 get_occurance(next->occurance);
5670 vector[i]->occurance = next->occurance;
5673 propogate_use(state, ins, next);
5674 flatten(state, ins, next);
5675 free_triple(state, ins);
5679 } while(ins != first);
5680 /* Pass two flatten the valvecs.
5684 struct triple *next;
5686 if (ins->op == OP_VAL_VEC) {
5687 release_triple(state, ins);
5690 } while(ins != first);
5691 /* Pass three verify the state and set ->id to 0.
5695 ins->id &= ~TRIPLE_FLAG_FLATTENED;
5696 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5697 internal_error(state, 0, "STRUCT_TYPE remains?");
5699 if (ins->op == OP_DOT) {
5700 internal_error(state, 0, "OP_DOT remains?");
5702 if (ins->op == OP_VAL_VEC) {
5703 internal_error(state, 0, "OP_VAL_VEC remains?");
5706 } while(ins != first);
5709 /* For those operations that cannot be simplified */
5710 static void simplify_noop(struct compile_state *state, struct triple *ins)
5715 static void simplify_smul(struct compile_state *state, struct triple *ins)
5717 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5720 RHS(ins, 0) = RHS(ins, 1);
5723 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5725 left = read_sconst(ins, &RHS(ins, 0));
5726 right = read_sconst(ins, &RHS(ins, 1));
5727 mkconst(state, ins, left * right);
5729 else if (is_zero(RHS(ins, 1))) {
5730 mkconst(state, ins, 0);
5732 else if (is_one(RHS(ins, 1))) {
5733 mkcopy(state, ins, RHS(ins, 0));
5735 else if (is_pow2(RHS(ins, 1))) {
5737 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5739 insert_triple(state, ins, val);
5740 unuse_triple(RHS(ins, 1), ins);
5741 use_triple(val, ins);
5746 static void simplify_umul(struct compile_state *state, struct triple *ins)
5748 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5751 RHS(ins, 0) = RHS(ins, 1);
5754 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5755 ulong_t left, right;
5756 left = read_const(state, ins, &RHS(ins, 0));
5757 right = read_const(state, ins, &RHS(ins, 1));
5758 mkconst(state, ins, left * right);
5760 else if (is_zero(RHS(ins, 1))) {
5761 mkconst(state, ins, 0);
5763 else if (is_one(RHS(ins, 1))) {
5764 mkcopy(state, ins, RHS(ins, 0));
5766 else if (is_pow2(RHS(ins, 1))) {
5768 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5770 insert_triple(state, ins, val);
5771 unuse_triple(RHS(ins, 1), ins);
5772 use_triple(val, ins);
5777 static void simplify_sdiv(struct compile_state *state, struct triple *ins)
5779 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5781 left = read_sconst(ins, &RHS(ins, 0));
5782 right = read_sconst(ins, &RHS(ins, 1));
5783 mkconst(state, ins, left / right);
5785 else if (is_zero(RHS(ins, 0))) {
5786 mkconst(state, ins, 0);
5788 else if (is_zero(RHS(ins, 1))) {
5789 error(state, ins, "division by zero");
5791 else if (is_one(RHS(ins, 1))) {
5792 mkcopy(state, ins, RHS(ins, 0));
5794 else if (is_pow2(RHS(ins, 1))) {
5796 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5798 insert_triple(state, ins, val);
5799 unuse_triple(RHS(ins, 1), ins);
5800 use_triple(val, ins);
5805 static void simplify_udiv(struct compile_state *state, struct triple *ins)
5807 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5808 ulong_t left, right;
5809 left = read_const(state, ins, &RHS(ins, 0));
5810 right = read_const(state, ins, &RHS(ins, 1));
5811 mkconst(state, ins, left / right);
5813 else if (is_zero(RHS(ins, 0))) {
5814 mkconst(state, ins, 0);
5816 else if (is_zero(RHS(ins, 1))) {
5817 error(state, ins, "division by zero");
5819 else if (is_one(RHS(ins, 1))) {
5820 mkcopy(state, ins, RHS(ins, 0));
5822 else if (is_pow2(RHS(ins, 1))) {
5824 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5826 insert_triple(state, ins, val);
5827 unuse_triple(RHS(ins, 1), ins);
5828 use_triple(val, ins);
5833 static void simplify_smod(struct compile_state *state, struct triple *ins)
5835 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5837 left = read_const(state, ins, &RHS(ins, 0));
5838 right = read_const(state, ins, &RHS(ins, 1));
5839 mkconst(state, ins, left % right);
5841 else if (is_zero(RHS(ins, 0))) {
5842 mkconst(state, ins, 0);
5844 else if (is_zero(RHS(ins, 1))) {
5845 error(state, ins, "division by zero");
5847 else if (is_one(RHS(ins, 1))) {
5848 mkconst(state, ins, 0);
5850 else if (is_pow2(RHS(ins, 1))) {
5852 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
5854 insert_triple(state, ins, val);
5855 unuse_triple(RHS(ins, 1), ins);
5856 use_triple(val, ins);
5860 static void simplify_umod(struct compile_state *state, struct triple *ins)
5862 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5863 ulong_t left, right;
5864 left = read_const(state, ins, &RHS(ins, 0));
5865 right = read_const(state, ins, &RHS(ins, 1));
5866 mkconst(state, ins, left % right);
5868 else if (is_zero(RHS(ins, 0))) {
5869 mkconst(state, ins, 0);
5871 else if (is_zero(RHS(ins, 1))) {
5872 error(state, ins, "division by zero");
5874 else if (is_one(RHS(ins, 1))) {
5875 mkconst(state, ins, 0);
5877 else if (is_pow2(RHS(ins, 1))) {
5879 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
5881 insert_triple(state, ins, val);
5882 unuse_triple(RHS(ins, 1), ins);
5883 use_triple(val, ins);
5888 static void simplify_add(struct compile_state *state, struct triple *ins)
5890 /* start with the pointer on the left */
5891 if (is_pointer(RHS(ins, 1))) {
5894 RHS(ins, 0) = RHS(ins, 1);
5897 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5898 if (!is_pointer(RHS(ins, 0))) {
5899 ulong_t left, right;
5900 left = read_const(state, ins, &RHS(ins, 0));
5901 right = read_const(state, ins, &RHS(ins, 1));
5902 mkconst(state, ins, left + right);
5904 else /* op == OP_ADDRCONST */ {
5905 struct triple *sdecl;
5906 ulong_t left, right;
5907 sdecl = MISC(RHS(ins, 0), 0);
5908 left = RHS(ins, 0)->u.cval;
5909 right = RHS(ins, 1)->u.cval;
5910 mkaddr_const(state, ins, sdecl, left + right);
5913 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5916 RHS(ins, 1) = RHS(ins, 0);
5921 static void simplify_sub(struct compile_state *state, struct triple *ins)
5923 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5924 if (!is_pointer(RHS(ins, 0))) {
5925 ulong_t left, right;
5926 left = read_const(state, ins, &RHS(ins, 0));
5927 right = read_const(state, ins, &RHS(ins, 1));
5928 mkconst(state, ins, left - right);
5930 else /* op == OP_ADDRCONST */ {
5931 struct triple *sdecl;
5932 ulong_t left, right;
5933 sdecl = MISC(RHS(ins, 0), 0);
5934 left = RHS(ins, 0)->u.cval;
5935 right = RHS(ins, 1)->u.cval;
5936 mkaddr_const(state, ins, sdecl, left - right);
5941 static void simplify_sl(struct compile_state *state, struct triple *ins)
5943 if (is_const(RHS(ins, 1))) {
5945 right = read_const(state, ins, &RHS(ins, 1));
5946 if (right >= (size_of(state, ins->type)*8)) {
5947 warning(state, ins, "left shift count >= width of type");
5950 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5951 ulong_t left, right;
5952 left = read_const(state, ins, &RHS(ins, 0));
5953 right = read_const(state, ins, &RHS(ins, 1));
5954 mkconst(state, ins, left << right);
5958 static void simplify_usr(struct compile_state *state, struct triple *ins)
5960 if (is_const(RHS(ins, 1))) {
5962 right = read_const(state, ins, &RHS(ins, 1));
5963 if (right >= (size_of(state, ins->type)*8)) {
5964 warning(state, ins, "right shift count >= width of type");
5967 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5968 ulong_t left, right;
5969 left = read_const(state, ins, &RHS(ins, 0));
5970 right = read_const(state, ins, &RHS(ins, 1));
5971 mkconst(state, ins, left >> right);
5975 static void simplify_ssr(struct compile_state *state, struct triple *ins)
5977 if (is_const(RHS(ins, 1))) {
5979 right = read_const(state, ins, &RHS(ins, 1));
5980 if (right >= (size_of(state, ins->type)*8)) {
5981 warning(state, ins, "right shift count >= width of type");
5984 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5986 left = read_sconst(ins, &RHS(ins, 0));
5987 right = read_sconst(ins, &RHS(ins, 1));
5988 mkconst(state, ins, left >> right);
5992 static void simplify_and(struct compile_state *state, struct triple *ins)
5994 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5995 ulong_t left, right;
5996 left = read_const(state, ins, &RHS(ins, 0));
5997 right = read_const(state, ins, &RHS(ins, 1));
5998 mkconst(state, ins, left & right);
6002 static void simplify_or(struct compile_state *state, struct triple *ins)
6004 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6005 ulong_t left, right;
6006 left = read_const(state, ins, &RHS(ins, 0));
6007 right = read_const(state, ins, &RHS(ins, 1));
6008 mkconst(state, ins, left | right);
6012 static void simplify_xor(struct compile_state *state, struct triple *ins)
6014 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6015 ulong_t left, right;
6016 left = read_const(state, ins, &RHS(ins, 0));
6017 right = read_const(state, ins, &RHS(ins, 1));
6018 mkconst(state, ins, left ^ right);
6022 static void simplify_pos(struct compile_state *state, struct triple *ins)
6024 if (is_const(RHS(ins, 0))) {
6025 mkconst(state, ins, RHS(ins, 0)->u.cval);
6028 mkcopy(state, ins, RHS(ins, 0));
6032 static void simplify_neg(struct compile_state *state, struct triple *ins)
6034 if (is_const(RHS(ins, 0))) {
6036 left = read_const(state, ins, &RHS(ins, 0));
6037 mkconst(state, ins, -left);
6039 else if (RHS(ins, 0)->op == OP_NEG) {
6040 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
6044 static void simplify_invert(struct compile_state *state, struct triple *ins)
6046 if (is_const(RHS(ins, 0))) {
6048 left = read_const(state, ins, &RHS(ins, 0));
6049 mkconst(state, ins, ~left);
6053 static void simplify_eq(struct compile_state *state, struct triple *ins)
6055 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6056 ulong_t left, right;
6057 left = read_const(state, ins, &RHS(ins, 0));
6058 right = read_const(state, ins, &RHS(ins, 1));
6059 mkconst(state, ins, left == right);
6061 else if (RHS(ins, 0) == RHS(ins, 1)) {
6062 mkconst(state, ins, 1);
6066 static void simplify_noteq(struct compile_state *state, struct triple *ins)
6068 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6069 ulong_t left, right;
6070 left = read_const(state, ins, &RHS(ins, 0));
6071 right = read_const(state, ins, &RHS(ins, 1));
6072 mkconst(state, ins, left != right);
6074 else if (RHS(ins, 0) == RHS(ins, 1)) {
6075 mkconst(state, ins, 0);
6079 static void simplify_sless(struct compile_state *state, struct triple *ins)
6081 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6083 left = read_sconst(ins, &RHS(ins, 0));
6084 right = read_sconst(ins, &RHS(ins, 1));
6085 mkconst(state, ins, left < right);
6087 else if (RHS(ins, 0) == RHS(ins, 1)) {
6088 mkconst(state, ins, 0);
6092 static void simplify_uless(struct compile_state *state, struct triple *ins)
6094 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6095 ulong_t left, right;
6096 left = read_const(state, ins, &RHS(ins, 0));
6097 right = read_const(state, ins, &RHS(ins, 1));
6098 mkconst(state, ins, left < right);
6100 else if (is_zero(RHS(ins, 0))) {
6101 mkconst(state, ins, 1);
6103 else if (RHS(ins, 0) == RHS(ins, 1)) {
6104 mkconst(state, ins, 0);
6108 static void simplify_smore(struct compile_state *state, struct triple *ins)
6110 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6112 left = read_sconst(ins, &RHS(ins, 0));
6113 right = read_sconst(ins, &RHS(ins, 1));
6114 mkconst(state, ins, left > right);
6116 else if (RHS(ins, 0) == RHS(ins, 1)) {
6117 mkconst(state, ins, 0);
6121 static void simplify_umore(struct compile_state *state, struct triple *ins)
6123 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6124 ulong_t left, right;
6125 left = read_const(state, ins, &RHS(ins, 0));
6126 right = read_const(state, ins, &RHS(ins, 1));
6127 mkconst(state, ins, left > right);
6129 else if (is_zero(RHS(ins, 1))) {
6130 mkconst(state, ins, 1);
6132 else if (RHS(ins, 0) == RHS(ins, 1)) {
6133 mkconst(state, ins, 0);
6138 static void simplify_slesseq(struct compile_state *state, struct triple *ins)
6140 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6142 left = read_sconst(ins, &RHS(ins, 0));
6143 right = read_sconst(ins, &RHS(ins, 1));
6144 mkconst(state, ins, left <= right);
6146 else if (RHS(ins, 0) == RHS(ins, 1)) {
6147 mkconst(state, ins, 1);
6151 static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
6153 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6154 ulong_t left, right;
6155 left = read_const(state, ins, &RHS(ins, 0));
6156 right = read_const(state, ins, &RHS(ins, 1));
6157 mkconst(state, ins, left <= right);
6159 else if (is_zero(RHS(ins, 0))) {
6160 mkconst(state, ins, 1);
6162 else if (RHS(ins, 0) == RHS(ins, 1)) {
6163 mkconst(state, ins, 1);
6167 static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
6169 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 0))) {
6171 left = read_sconst(ins, &RHS(ins, 0));
6172 right = read_sconst(ins, &RHS(ins, 1));
6173 mkconst(state, ins, left >= right);
6175 else if (RHS(ins, 0) == RHS(ins, 1)) {
6176 mkconst(state, ins, 1);
6180 static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
6182 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6183 ulong_t left, right;
6184 left = read_const(state, ins, &RHS(ins, 0));
6185 right = read_const(state, ins, &RHS(ins, 1));
6186 mkconst(state, ins, left >= right);
6188 else if (is_zero(RHS(ins, 1))) {
6189 mkconst(state, ins, 1);
6191 else if (RHS(ins, 0) == RHS(ins, 1)) {
6192 mkconst(state, ins, 1);
6196 static void simplify_lfalse(struct compile_state *state, struct triple *ins)
6198 if (is_const(RHS(ins, 0))) {
6200 left = read_const(state, ins, &RHS(ins, 0));
6201 mkconst(state, ins, left == 0);
6203 /* Otherwise if I am the only user... */
6204 else if ((RHS(ins, 0)->use->member == ins) && (RHS(ins, 0)->use->next == 0)) {
6206 /* Invert a boolean operation */
6207 switch(RHS(ins, 0)->op) {
6208 case OP_LTRUE: RHS(ins, 0)->op = OP_LFALSE; break;
6209 case OP_LFALSE: RHS(ins, 0)->op = OP_LTRUE; break;
6210 case OP_EQ: RHS(ins, 0)->op = OP_NOTEQ; break;
6211 case OP_NOTEQ: RHS(ins, 0)->op = OP_EQ; break;
6212 case OP_SLESS: RHS(ins, 0)->op = OP_SMOREEQ; break;
6213 case OP_ULESS: RHS(ins, 0)->op = OP_UMOREEQ; break;
6214 case OP_SMORE: RHS(ins, 0)->op = OP_SLESSEQ; break;
6215 case OP_UMORE: RHS(ins, 0)->op = OP_ULESSEQ; break;
6216 case OP_SLESSEQ: RHS(ins, 0)->op = OP_SMORE; break;
6217 case OP_ULESSEQ: RHS(ins, 0)->op = OP_UMORE; break;
6218 case OP_SMOREEQ: RHS(ins, 0)->op = OP_SLESS; break;
6219 case OP_UMOREEQ: RHS(ins, 0)->op = OP_ULESS; break;
6225 mkcopy(state, ins, RHS(ins, 0));
6230 static void simplify_ltrue (struct compile_state *state, struct triple *ins)
6232 if (is_const(RHS(ins, 0))) {
6234 left = read_const(state, ins, &RHS(ins, 0));
6235 mkconst(state, ins, left != 0);
6237 else switch(RHS(ins, 0)->op) {
6238 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
6239 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
6240 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
6241 mkcopy(state, ins, RHS(ins, 0));
6246 static void simplify_copy(struct compile_state *state, struct triple *ins)
6248 if (is_const(RHS(ins, 0))) {
6249 switch(RHS(ins, 0)->op) {
6253 left = read_const(state, ins, &RHS(ins, 0));
6254 mkconst(state, ins, left);
6259 struct triple *sdecl;
6261 sdecl = MISC(RHS(ins, 0), 0);
6262 offset = RHS(ins, 0)->u.cval;
6263 mkaddr_const(state, ins, sdecl, offset);
6267 internal_error(state, ins, "uknown constant");
6273 static void simplify_branch(struct compile_state *state, struct triple *ins)
6275 struct block *block;
6276 if (ins->op != OP_BRANCH) {
6277 internal_error(state, ins, "not branch");
6279 if (ins->use != 0) {
6280 internal_error(state, ins, "branch use");
6282 #warning "FIXME implement simplify branch."
6283 /* The challenge here with simplify branch is that I need to
6284 * make modifications to the control flow graph as well
6285 * as to the branch instruction itself.
6287 block = ins->u.block;
6289 if (TRIPLE_RHS(ins->sizes) && is_const(RHS(ins, 0))) {
6290 struct triple *targ;
6292 value = read_const(state, ins, &RHS(ins, 0));
6293 unuse_triple(RHS(ins, 0), ins);
6294 targ = TARG(ins, 0);
6295 ins->sizes = TRIPLE_SIZES(0, 0, 0, 1);
6297 unuse_triple(ins->next, ins);
6298 TARG(ins, 0) = targ;
6301 unuse_triple(targ, ins);
6302 TARG(ins, 0) = ins->next;
6304 #warning "FIXME handle the case of making a branch unconditional"
6306 if (TARG(ins, 0) == ins->next) {
6307 unuse_triple(ins->next, ins);
6308 if (TRIPLE_RHS(ins->sizes)) {
6309 unuse_triple(RHS(ins, 0), ins);
6310 unuse_triple(ins->next, ins);
6312 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
6315 internal_error(state, ins, "noop use != 0");
6317 #warning "FIXME handle the case of killing a branch"
6321 static void simplify_phi(struct compile_state *state, struct triple *ins)
6323 struct triple **expr;
6325 expr = triple_rhs(state, ins, 0);
6326 if (!*expr || !is_const(*expr)) {
6329 value = read_const(state, ins, expr);
6330 for(;expr;expr = triple_rhs(state, ins, expr)) {
6331 if (!*expr || !is_const(*expr)) {
6334 if (value != read_const(state, ins, expr)) {
6338 mkconst(state, ins, value);
6342 static void simplify_bsf(struct compile_state *state, struct triple *ins)
6344 if (is_const(RHS(ins, 0))) {
6346 left = read_const(state, ins, &RHS(ins, 0));
6347 mkconst(state, ins, bsf(left));
6351 static void simplify_bsr(struct compile_state *state, struct triple *ins)
6353 if (is_const(RHS(ins, 0))) {
6355 left = read_const(state, ins, &RHS(ins, 0));
6356 mkconst(state, ins, bsr(left));
6361 typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
6362 static const simplify_t table_simplify[] = {
6364 #define simplify_smul simplify_noop
6365 #define simplify_umul simplify_noop
6366 #define simplify_sdiv simplify_noop
6367 #define simplify_udiv simplify_noop
6368 #define simplify_smod simplify_noop
6369 #define simplify_umod simplify_noop
6372 #define simplify_add simplify_noop
6373 #define simplify_sub simplify_noop
6376 #define simplify_sl simplify_noop
6377 #define simplify_usr simplify_noop
6378 #define simplify_ssr simplify_noop
6381 #define simplify_and simplify_noop
6382 #define simplify_xor simplify_noop
6383 #define simplify_or simplify_noop
6386 #define simplify_pos simplify_noop
6387 #define simplify_neg simplify_noop
6388 #define simplify_invert simplify_noop
6392 #define simplify_eq simplify_noop
6393 #define simplify_noteq simplify_noop
6396 #define simplify_sless simplify_noop
6397 #define simplify_uless simplify_noop
6398 #define simplify_smore simplify_noop
6399 #define simplify_umore simplify_noop
6402 #define simplify_slesseq simplify_noop
6403 #define simplify_ulesseq simplify_noop
6404 #define simplify_smoreeq simplify_noop
6405 #define simplify_umoreeq simplify_noop
6408 #define simplify_lfalse simplify_noop
6411 #define simplify_ltrue simplify_noop
6415 #define simplify_copy simplify_noop
6419 #define simplify_branch simplify_noop
6423 #define simplify_phi simplify_noop
6427 #define simplify_bsf simplify_noop
6428 #define simplify_bsr simplify_noop
6431 [OP_SMUL ] = simplify_smul,
6432 [OP_UMUL ] = simplify_umul,
6433 [OP_SDIV ] = simplify_sdiv,
6434 [OP_UDIV ] = simplify_udiv,
6435 [OP_SMOD ] = simplify_smod,
6436 [OP_UMOD ] = simplify_umod,
6437 [OP_ADD ] = simplify_add,
6438 [OP_SUB ] = simplify_sub,
6439 [OP_SL ] = simplify_sl,
6440 [OP_USR ] = simplify_usr,
6441 [OP_SSR ] = simplify_ssr,
6442 [OP_AND ] = simplify_and,
6443 [OP_XOR ] = simplify_xor,
6444 [OP_OR ] = simplify_or,
6445 [OP_POS ] = simplify_pos,
6446 [OP_NEG ] = simplify_neg,
6447 [OP_INVERT ] = simplify_invert,
6449 [OP_EQ ] = simplify_eq,
6450 [OP_NOTEQ ] = simplify_noteq,
6451 [OP_SLESS ] = simplify_sless,
6452 [OP_ULESS ] = simplify_uless,
6453 [OP_SMORE ] = simplify_smore,
6454 [OP_UMORE ] = simplify_umore,
6455 [OP_SLESSEQ ] = simplify_slesseq,
6456 [OP_ULESSEQ ] = simplify_ulesseq,
6457 [OP_SMOREEQ ] = simplify_smoreeq,
6458 [OP_UMOREEQ ] = simplify_umoreeq,
6459 [OP_LFALSE ] = simplify_lfalse,
6460 [OP_LTRUE ] = simplify_ltrue,
6462 [OP_LOAD ] = simplify_noop,
6463 [OP_STORE ] = simplify_noop,
6465 [OP_NOOP ] = simplify_noop,
6467 [OP_INTCONST ] = simplify_noop,
6468 [OP_BLOBCONST ] = simplify_noop,
6469 [OP_ADDRCONST ] = simplify_noop,
6471 [OP_WRITE ] = simplify_noop,
6472 [OP_READ ] = simplify_noop,
6473 [OP_COPY ] = simplify_copy,
6474 [OP_PIECE ] = simplify_noop,
6475 [OP_ASM ] = simplify_noop,
6477 [OP_DOT ] = simplify_noop,
6478 [OP_VAL_VEC ] = simplify_noop,
6480 [OP_LIST ] = simplify_noop,
6481 [OP_BRANCH ] = simplify_branch,
6482 [OP_LABEL ] = simplify_noop,
6483 [OP_ADECL ] = simplify_noop,
6484 [OP_SDECL ] = simplify_noop,
6485 [OP_PHI ] = simplify_phi,
6487 [OP_INB ] = simplify_noop,
6488 [OP_INW ] = simplify_noop,
6489 [OP_INL ] = simplify_noop,
6490 [OP_OUTB ] = simplify_noop,
6491 [OP_OUTW ] = simplify_noop,
6492 [OP_OUTL ] = simplify_noop,
6493 [OP_BSF ] = simplify_bsf,
6494 [OP_BSR ] = simplify_bsr,
6495 [OP_RDMSR ] = simplify_noop,
6496 [OP_WRMSR ] = simplify_noop,
6497 [OP_HLT ] = simplify_noop,
6500 static void simplify(struct compile_state *state, struct triple *ins)
6503 simplify_t do_simplify;
6507 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
6511 do_simplify = table_simplify[op];
6514 internal_error(state, ins, "cannot simplify op: %d %s\n",
6518 do_simplify(state, ins);
6519 } while(ins->op != op);
6522 static void simplify_all(struct compile_state *state)
6524 struct triple *ins, *first;
6525 first = RHS(state->main_function, 0);
6528 simplify(state, ins);
6530 } while(ins != first);
6535 * ============================
6538 static void register_builtin_function(struct compile_state *state,
6539 const char *name, int op, struct type *rtype, ...)
6541 struct type *ftype, *atype, *param, **next;
6542 struct triple *def, *arg, *result, *work, *last, *first;
6543 struct hash_entry *ident;
6544 struct file_state file;
6550 /* Dummy file state to get debug handling right */
6551 memset(&file, 0, sizeof(file));
6552 file.basename = "<built-in>";
6554 file.report_line = 1;
6555 file.report_name = file.basename;
6556 file.prev = state->file;
6557 state->file = &file;
6558 state->function = name;
6560 /* Find the Parameter count */
6561 valid_op(state, op);
6562 parameters = table_ops[op].rhs;
6563 if (parameters < 0 ) {
6564 internal_error(state, 0, "Invalid builtin parameter count");
6567 /* Find the function type */
6568 ftype = new_type(TYPE_FUNCTION, rtype, 0);
6569 next = &ftype->right;
6570 va_start(args, rtype);
6571 for(i = 0; i < parameters; i++) {
6572 atype = va_arg(args, struct type *);
6576 *next = new_type(TYPE_PRODUCT, *next, atype);
6577 next = &((*next)->right);
6585 /* Generate the needed triples */
6586 def = triple(state, OP_LIST, ftype, 0, 0);
6587 first = label(state);
6588 RHS(def, 0) = first;
6590 /* Now string them together */
6591 param = ftype->right;
6592 for(i = 0; i < parameters; i++) {
6593 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6594 atype = param->left;
6598 arg = flatten(state, first, variable(state, atype));
6599 param = param->right;
6602 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
6603 result = flatten(state, first, variable(state, rtype));
6605 MISC(def, 0) = result;
6606 work = new_triple(state, op, rtype, -1, parameters);
6607 for(i = 0, arg = first->next; i < parameters; i++, arg = arg->next) {
6608 RHS(work, i) = read_expr(state, arg);
6610 if (result && ((rtype->type & TYPE_MASK) == TYPE_STRUCT)) {
6612 /* Populate the LHS with the target registers */
6613 work = flatten(state, first, work);
6614 work->type = &void_type;
6615 param = rtype->left;
6616 if (rtype->elements != TRIPLE_LHS(work->sizes)) {
6617 internal_error(state, 0, "Invalid result type");
6619 val = new_triple(state, OP_VAL_VEC, rtype, -1, -1);
6620 for(i = 0; i < rtype->elements; i++) {
6621 struct triple *piece;
6623 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6624 atype = param->left;
6626 if (!TYPE_ARITHMETIC(atype->type) &&
6627 !TYPE_PTR(atype->type)) {
6628 internal_error(state, 0, "Invalid lhs type");
6630 piece = triple(state, OP_PIECE, atype, work, 0);
6632 LHS(work, i) = piece;
6633 RHS(val, i) = piece;
6638 work = write_expr(state, result, work);
6640 work = flatten(state, first, work);
6641 last = flatten(state, first, label(state));
6642 name_len = strlen(name);
6643 ident = lookup(state, name, name_len);
6644 symbol(state, ident, &ident->sym_ident, def, ftype);
6646 state->file = file.prev;
6647 state->function = 0;
6649 fprintf(stdout, "\n");
6650 loc(stdout, state, 0);
6651 fprintf(stdout, "\n__________ builtin_function _________\n");
6652 print_triple(state, def);
6653 fprintf(stdout, "__________ builtin_function _________ done\n\n");
6657 static struct type *partial_struct(struct compile_state *state,
6658 const char *field_name, struct type *type, struct type *rest)
6660 struct hash_entry *field_ident;
6661 struct type *result;
6664 field_name_len = strlen(field_name);
6665 field_ident = lookup(state, field_name, field_name_len);
6667 result = clone_type(0, type);
6668 result->field_ident = field_ident;
6671 result = new_type(TYPE_PRODUCT, result, rest);
6676 static struct type *register_builtin_type(struct compile_state *state,
6677 const char *name, struct type *type)
6679 struct hash_entry *ident;
6682 name_len = strlen(name);
6683 ident = lookup(state, name, name_len);
6685 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6686 ulong_t elements = 0;
6688 type = new_type(TYPE_STRUCT, type, 0);
6690 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
6692 field = field->right;
6695 symbol(state, ident, &ident->sym_struct, 0, type);
6696 type->type_ident = ident;
6697 type->elements = elements;
6699 symbol(state, ident, &ident->sym_ident, 0, type);
6700 ident->tok = TOK_TYPE_NAME;
6705 static void register_builtins(struct compile_state *state)
6707 struct type *msr_type;
6709 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
6711 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
6713 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
6716 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
6717 &uchar_type, &ushort_type);
6718 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
6719 &ushort_type, &ushort_type);
6720 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
6721 &uint_type, &ushort_type);
6723 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
6725 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
6728 msr_type = register_builtin_type(state, "__builtin_msr_t",
6729 partial_struct(state, "lo", &ulong_type,
6730 partial_struct(state, "hi", &ulong_type, 0)));
6732 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
6734 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
6735 &ulong_type, &ulong_type, &ulong_type);
6737 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
6741 static struct type *declarator(
6742 struct compile_state *state, struct type *type,
6743 struct hash_entry **ident, int need_ident);
6744 static void decl(struct compile_state *state, struct triple *first);
6745 static struct type *specifier_qualifier_list(struct compile_state *state);
6746 static int isdecl_specifier(int tok);
6747 static struct type *decl_specifiers(struct compile_state *state);
6748 static int istype(int tok);
6749 static struct triple *expr(struct compile_state *state);
6750 static struct triple *assignment_expr(struct compile_state *state);
6751 static struct type *type_name(struct compile_state *state);
6752 static void statement(struct compile_state *state, struct triple *fist);
6754 static struct triple *call_expr(
6755 struct compile_state *state, struct triple *func)
6758 struct type *param, *type;
6759 ulong_t pvals, index;
6761 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
6762 error(state, 0, "Called object is not a function");
6764 if (func->op != OP_LIST) {
6765 internal_error(state, 0, "improper function");
6767 eat(state, TOK_LPAREN);
6768 /* Find the return type without any specifiers */
6769 type = clone_type(0, func->type->left);
6770 def = new_triple(state, OP_CALL, func->type, -1, -1);
6773 pvals = TRIPLE_RHS(def->sizes);
6774 MISC(def, 0) = func;
6776 param = func->type->right;
6777 for(index = 0; index < pvals; index++) {
6779 struct type *arg_type;
6780 val = read_expr(state, assignment_expr(state));
6782 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6783 arg_type = param->left;
6785 write_compatible(state, arg_type, val->type);
6786 RHS(def, index) = val;
6787 if (index != (pvals - 1)) {
6788 eat(state, TOK_COMMA);
6789 param = param->right;
6792 eat(state, TOK_RPAREN);
6797 static struct triple *character_constant(struct compile_state *state)
6801 const signed char *str, *end;
6804 eat(state, TOK_LIT_CHAR);
6805 tk = &state->token[0];
6806 str = tk->val.str + 1;
6807 str_len = tk->str_len - 2;
6809 error(state, 0, "empty character constant");
6811 end = str + str_len;
6812 c = char_value(state, &str, end);
6814 error(state, 0, "multibyte character constant not supported");
6816 def = int_const(state, &char_type, (ulong_t)((long_t)c));
6820 static struct triple *string_constant(struct compile_state *state)
6825 const signed char *str, *end;
6826 signed char *buf, *ptr;
6830 type = new_type(TYPE_ARRAY, &char_type, 0);
6832 /* The while loop handles string concatenation */
6834 eat(state, TOK_LIT_STRING);
6835 tk = &state->token[0];
6836 str = tk->val.str + 1;
6837 str_len = tk->str_len - 2;
6839 error(state, 0, "negative string constant length");
6841 end = str + str_len;
6843 buf = xmalloc(type->elements + str_len + 1, "string_constant");
6844 memcpy(buf, ptr, type->elements);
6845 ptr = buf + type->elements;
6847 *ptr++ = char_value(state, &str, end);
6849 type->elements = ptr - buf;
6850 } while(peek(state) == TOK_LIT_STRING);
6852 type->elements += 1;
6853 def = triple(state, OP_BLOBCONST, type, 0, 0);
6859 static struct triple *integer_constant(struct compile_state *state)
6868 eat(state, TOK_LIT_INT);
6869 tk = &state->token[0];
6871 decimal = (tk->val.str[0] != '0');
6872 val = strtoul(tk->val.str, &end, 0);
6873 if ((val == ULONG_MAX) && (errno == ERANGE)) {
6874 error(state, 0, "Integer constant to large");
6877 if ((*end == 'u') || (*end == 'U')) {
6881 if ((*end == 'l') || (*end == 'L')) {
6885 if ((*end == 'u') || (*end == 'U')) {
6890 error(state, 0, "Junk at end of integer constant");
6897 if (!decimal && (val > LONG_MAX)) {
6903 if (val > UINT_MAX) {
6909 if (!decimal && (val > INT_MAX) && (val <= UINT_MAX)) {
6912 else if (!decimal && (val > LONG_MAX)) {
6915 else if (val > INT_MAX) {
6919 def = int_const(state, type, val);
6923 static struct triple *primary_expr(struct compile_state *state)
6931 struct hash_entry *ident;
6932 /* Here ident is either:
6935 * an enumeration constant.
6937 eat(state, TOK_IDENT);
6938 ident = state->token[0].ident;
6939 if (!ident->sym_ident) {
6940 error(state, 0, "%s undeclared", ident->name);
6942 def = ident->sym_ident->def;
6945 case TOK_ENUM_CONST:
6946 /* Here ident is an enumeration constant */
6947 eat(state, TOK_ENUM_CONST);
6952 eat(state, TOK_LPAREN);
6954 eat(state, TOK_RPAREN);
6957 def = integer_constant(state);
6960 eat(state, TOK_LIT_FLOAT);
6961 error(state, 0, "Floating point constants not supported");
6966 def = character_constant(state);
6968 case TOK_LIT_STRING:
6969 def = string_constant(state);
6973 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
6978 static struct triple *postfix_expr(struct compile_state *state)
6982 def = primary_expr(state);
6984 struct triple *left;
6988 switch((tok = peek(state))) {
6990 eat(state, TOK_LBRACKET);
6991 def = mk_subscript_expr(state, left, expr(state));
6992 eat(state, TOK_RBRACKET);
6995 def = call_expr(state, def);
6999 struct hash_entry *field;
7000 eat(state, TOK_DOT);
7001 eat(state, TOK_IDENT);
7002 field = state->token[0].ident;
7003 def = deref_field(state, def, field);
7008 struct hash_entry *field;
7009 eat(state, TOK_ARROW);
7010 eat(state, TOK_IDENT);
7011 field = state->token[0].ident;
7012 def = mk_deref_expr(state, read_expr(state, def));
7013 def = deref_field(state, def, field);
7017 eat(state, TOK_PLUSPLUS);
7018 def = mk_post_inc_expr(state, left);
7020 case TOK_MINUSMINUS:
7021 eat(state, TOK_MINUSMINUS);
7022 def = mk_post_dec_expr(state, left);
7032 static struct triple *cast_expr(struct compile_state *state);
7034 static struct triple *unary_expr(struct compile_state *state)
7036 struct triple *def, *right;
7038 switch((tok = peek(state))) {
7040 eat(state, TOK_PLUSPLUS);
7041 def = mk_pre_inc_expr(state, unary_expr(state));
7043 case TOK_MINUSMINUS:
7044 eat(state, TOK_MINUSMINUS);
7045 def = mk_pre_dec_expr(state, unary_expr(state));
7048 eat(state, TOK_AND);
7049 def = mk_addr_expr(state, cast_expr(state), 0);
7052 eat(state, TOK_STAR);
7053 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
7056 eat(state, TOK_PLUS);
7057 right = read_expr(state, cast_expr(state));
7058 arithmetic(state, right);
7059 def = integral_promotion(state, right);
7062 eat(state, TOK_MINUS);
7063 right = read_expr(state, cast_expr(state));
7064 arithmetic(state, right);
7065 def = integral_promotion(state, right);
7066 def = triple(state, OP_NEG, def->type, def, 0);
7069 eat(state, TOK_TILDE);
7070 right = read_expr(state, cast_expr(state));
7071 integral(state, right);
7072 def = integral_promotion(state, right);
7073 def = triple(state, OP_INVERT, def->type, def, 0);
7076 eat(state, TOK_BANG);
7077 right = read_expr(state, cast_expr(state));
7079 def = lfalse_expr(state, right);
7085 eat(state, TOK_SIZEOF);
7087 tok2 = peek2(state);
7088 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7089 eat(state, TOK_LPAREN);
7090 type = type_name(state);
7091 eat(state, TOK_RPAREN);
7094 struct triple *expr;
7095 expr = unary_expr(state);
7097 release_expr(state, expr);
7099 def = int_const(state, &ulong_type, size_of(state, type));
7106 eat(state, TOK_ALIGNOF);
7108 tok2 = peek2(state);
7109 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7110 eat(state, TOK_LPAREN);
7111 type = type_name(state);
7112 eat(state, TOK_RPAREN);
7115 struct triple *expr;
7116 expr = unary_expr(state);
7118 release_expr(state, expr);
7120 def = int_const(state, &ulong_type, align_of(state, type));
7124 def = postfix_expr(state);
7130 static struct triple *cast_expr(struct compile_state *state)
7135 tok2 = peek2(state);
7136 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7138 eat(state, TOK_LPAREN);
7139 type = type_name(state);
7140 eat(state, TOK_RPAREN);
7141 def = read_expr(state, cast_expr(state));
7142 def = triple(state, OP_COPY, type, def, 0);
7145 def = unary_expr(state);
7150 static struct triple *mult_expr(struct compile_state *state)
7154 def = cast_expr(state);
7156 struct triple *left, *right;
7157 struct type *result_type;
7160 switch(tok = (peek(state))) {
7164 left = read_expr(state, def);
7165 arithmetic(state, left);
7169 right = read_expr(state, cast_expr(state));
7170 arithmetic(state, right);
7172 result_type = arithmetic_result(state, left, right);
7173 sign = is_signed(result_type);
7176 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
7177 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
7178 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
7180 def = triple(state, op, result_type, left, right);
7190 static struct triple *add_expr(struct compile_state *state)
7194 def = mult_expr(state);
7197 switch( peek(state)) {
7199 eat(state, TOK_PLUS);
7200 def = mk_add_expr(state, def, mult_expr(state));
7203 eat(state, TOK_MINUS);
7204 def = mk_sub_expr(state, def, mult_expr(state));
7214 static struct triple *shift_expr(struct compile_state *state)
7218 def = add_expr(state);
7220 struct triple *left, *right;
7223 switch((tok = peek(state))) {
7226 left = read_expr(state, def);
7227 integral(state, left);
7228 left = integral_promotion(state, left);
7232 right = read_expr(state, add_expr(state));
7233 integral(state, right);
7234 right = integral_promotion(state, right);
7236 op = (tok == TOK_SL)? OP_SL :
7237 is_signed(left->type)? OP_SSR: OP_USR;
7239 def = triple(state, op, left->type, left, right);
7249 static struct triple *relational_expr(struct compile_state *state)
7251 #warning "Extend relational exprs to work on more than arithmetic types"
7254 def = shift_expr(state);
7256 struct triple *left, *right;
7257 struct type *arg_type;
7260 switch((tok = peek(state))) {
7265 left = read_expr(state, def);
7266 arithmetic(state, left);
7270 right = read_expr(state, shift_expr(state));
7271 arithmetic(state, right);
7273 arg_type = arithmetic_result(state, left, right);
7274 sign = is_signed(arg_type);
7277 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
7278 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
7279 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
7280 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
7282 def = triple(state, op, &int_type, left, right);
7292 static struct triple *equality_expr(struct compile_state *state)
7294 #warning "Extend equality exprs to work on more than arithmetic types"
7297 def = relational_expr(state);
7299 struct triple *left, *right;
7302 switch((tok = peek(state))) {
7305 left = read_expr(state, def);
7306 arithmetic(state, left);
7308 right = read_expr(state, relational_expr(state));
7309 arithmetic(state, right);
7310 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
7311 def = triple(state, op, &int_type, left, right);
7321 static struct triple *and_expr(struct compile_state *state)
7324 def = equality_expr(state);
7325 while(peek(state) == TOK_AND) {
7326 struct triple *left, *right;
7327 struct type *result_type;
7328 left = read_expr(state, def);
7329 integral(state, left);
7330 eat(state, TOK_AND);
7331 right = read_expr(state, equality_expr(state));
7332 integral(state, right);
7333 result_type = arithmetic_result(state, left, right);
7334 def = triple(state, OP_AND, result_type, left, right);
7339 static struct triple *xor_expr(struct compile_state *state)
7342 def = and_expr(state);
7343 while(peek(state) == TOK_XOR) {
7344 struct triple *left, *right;
7345 struct type *result_type;
7346 left = read_expr(state, def);
7347 integral(state, left);
7348 eat(state, TOK_XOR);
7349 right = read_expr(state, and_expr(state));
7350 integral(state, right);
7351 result_type = arithmetic_result(state, left, right);
7352 def = triple(state, OP_XOR, result_type, left, right);
7357 static struct triple *or_expr(struct compile_state *state)
7360 def = xor_expr(state);
7361 while(peek(state) == TOK_OR) {
7362 struct triple *left, *right;
7363 struct type *result_type;
7364 left = read_expr(state, def);
7365 integral(state, left);
7367 right = read_expr(state, xor_expr(state));
7368 integral(state, right);
7369 result_type = arithmetic_result(state, left, right);
7370 def = triple(state, OP_OR, result_type, left, right);
7375 static struct triple *land_expr(struct compile_state *state)
7378 def = or_expr(state);
7379 while(peek(state) == TOK_LOGAND) {
7380 struct triple *left, *right;
7381 left = read_expr(state, def);
7383 eat(state, TOK_LOGAND);
7384 right = read_expr(state, or_expr(state));
7387 def = triple(state, OP_LAND, &int_type,
7388 ltrue_expr(state, left),
7389 ltrue_expr(state, right));
7394 static struct triple *lor_expr(struct compile_state *state)
7397 def = land_expr(state);
7398 while(peek(state) == TOK_LOGOR) {
7399 struct triple *left, *right;
7400 left = read_expr(state, def);
7402 eat(state, TOK_LOGOR);
7403 right = read_expr(state, land_expr(state));
7406 def = triple(state, OP_LOR, &int_type,
7407 ltrue_expr(state, left),
7408 ltrue_expr(state, right));
7413 static struct triple *conditional_expr(struct compile_state *state)
7416 def = lor_expr(state);
7417 if (peek(state) == TOK_QUEST) {
7418 struct triple *test, *left, *right;
7420 test = ltrue_expr(state, read_expr(state, def));
7421 eat(state, TOK_QUEST);
7422 left = read_expr(state, expr(state));
7423 eat(state, TOK_COLON);
7424 right = read_expr(state, conditional_expr(state));
7426 def = cond_expr(state, test, left, right);
7431 static struct triple *eval_const_expr(
7432 struct compile_state *state, struct triple *expr)
7435 struct triple *head, *ptr;
7436 head = label(state); /* dummy initial triple */
7437 flatten(state, head, expr);
7438 for(ptr = head->next; ptr != head; ptr = ptr->next) {
7439 simplify(state, ptr);
7441 /* Remove the constant value the tail of the list */
7443 def->prev->next = def->next;
7444 def->next->prev = def->prev;
7445 def->next = def->prev = def;
7446 if (!is_const(def)) {
7447 internal_error(state, 0, "Not a constant expression");
7449 /* Free the intermediate expressions */
7450 while(head->next != head) {
7451 release_triple(state, head->next);
7453 free_triple(state, head);
7457 static struct triple *constant_expr(struct compile_state *state)
7459 return eval_const_expr(state, conditional_expr(state));
7462 static struct triple *assignment_expr(struct compile_state *state)
7464 struct triple *def, *left, *right;
7466 /* The C grammer in K&R shows assignment expressions
7467 * only taking unary expressions as input on their
7468 * left hand side. But specifies the precedence of
7469 * assignemnt as the lowest operator except for comma.
7471 * Allowing conditional expressions on the left hand side
7472 * of an assignement results in a grammar that accepts
7473 * a larger set of statements than standard C. As long
7474 * as the subset of the grammar that is standard C behaves
7475 * correctly this should cause no problems.
7477 * For the extra token strings accepted by the grammar
7478 * none of them should produce a valid lvalue, so they
7479 * should not produce functioning programs.
7481 * GCC has this bug as well, so surprises should be minimal.
7483 def = conditional_expr(state);
7485 switch((tok = peek(state))) {
7487 lvalue(state, left);
7489 def = write_expr(state, left,
7490 read_expr(state, assignment_expr(state)));
7495 lvalue(state, left);
7496 arithmetic(state, left);
7498 right = read_expr(state, assignment_expr(state));
7499 arithmetic(state, right);
7501 sign = is_signed(left->type);
7504 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
7505 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
7506 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
7508 def = write_expr(state, left,
7509 triple(state, op, left->type,
7510 read_expr(state, left), right));
7513 lvalue(state, left);
7514 eat(state, TOK_PLUSEQ);
7515 def = write_expr(state, left,
7516 mk_add_expr(state, left, assignment_expr(state)));
7519 lvalue(state, left);
7520 eat(state, TOK_MINUSEQ);
7521 def = write_expr(state, left,
7522 mk_sub_expr(state, left, assignment_expr(state)));
7529 lvalue(state, left);
7530 integral(state, left);
7532 right = read_expr(state, assignment_expr(state));
7533 integral(state, right);
7534 right = integral_promotion(state, right);
7535 sign = is_signed(left->type);
7538 case TOK_SLEQ: op = OP_SL; break;
7539 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
7540 case TOK_ANDEQ: op = OP_AND; break;
7541 case TOK_XOREQ: op = OP_XOR; break;
7542 case TOK_OREQ: op = OP_OR; break;
7544 def = write_expr(state, left,
7545 triple(state, op, left->type,
7546 read_expr(state, left), right));
7552 static struct triple *expr(struct compile_state *state)
7555 def = assignment_expr(state);
7556 while(peek(state) == TOK_COMMA) {
7557 struct triple *left, *right;
7559 eat(state, TOK_COMMA);
7560 right = assignment_expr(state);
7561 def = triple(state, OP_COMMA, right->type, left, right);
7566 static void expr_statement(struct compile_state *state, struct triple *first)
7568 if (peek(state) != TOK_SEMI) {
7569 flatten(state, first, expr(state));
7571 eat(state, TOK_SEMI);
7574 static void if_statement(struct compile_state *state, struct triple *first)
7576 struct triple *test, *jmp1, *jmp2, *middle, *end;
7578 jmp1 = jmp2 = middle = 0;
7580 eat(state, TOK_LPAREN);
7583 /* Cleanup and invert the test */
7584 test = lfalse_expr(state, read_expr(state, test));
7585 eat(state, TOK_RPAREN);
7586 /* Generate the needed pieces */
7587 middle = label(state);
7588 jmp1 = branch(state, middle, test);
7589 /* Thread the pieces together */
7590 flatten(state, first, test);
7591 flatten(state, first, jmp1);
7592 flatten(state, first, label(state));
7593 statement(state, first);
7594 if (peek(state) == TOK_ELSE) {
7595 eat(state, TOK_ELSE);
7596 /* Generate the rest of the pieces */
7598 jmp2 = branch(state, end, 0);
7599 /* Thread them together */
7600 flatten(state, first, jmp2);
7601 flatten(state, first, middle);
7602 statement(state, first);
7603 flatten(state, first, end);
7606 flatten(state, first, middle);
7610 static void for_statement(struct compile_state *state, struct triple *first)
7612 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
7613 struct triple *label1, *label2, *label3;
7614 struct hash_entry *ident;
7616 eat(state, TOK_FOR);
7617 eat(state, TOK_LPAREN);
7618 head = test = tail = jmp1 = jmp2 = 0;
7619 if (peek(state) != TOK_SEMI) {
7622 eat(state, TOK_SEMI);
7623 if (peek(state) != TOK_SEMI) {
7626 test = ltrue_expr(state, read_expr(state, test));
7628 eat(state, TOK_SEMI);
7629 if (peek(state) != TOK_RPAREN) {
7632 eat(state, TOK_RPAREN);
7633 /* Generate the needed pieces */
7634 label1 = label(state);
7635 label2 = label(state);
7636 label3 = label(state);
7638 jmp1 = branch(state, label3, 0);
7639 jmp2 = branch(state, label1, test);
7642 jmp2 = branch(state, label1, 0);
7645 /* Remember where break and continue go */
7647 ident = state->i_break;
7648 symbol(state, ident, &ident->sym_ident, end, end->type);
7649 ident = state->i_continue;
7650 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7651 /* Now include the body */
7652 flatten(state, first, head);
7653 flatten(state, first, jmp1);
7654 flatten(state, first, label1);
7655 statement(state, first);
7656 flatten(state, first, label2);
7657 flatten(state, first, tail);
7658 flatten(state, first, label3);
7659 flatten(state, first, test);
7660 flatten(state, first, jmp2);
7661 flatten(state, first, end);
7662 /* Cleanup the break/continue scope */
7666 static void while_statement(struct compile_state *state, struct triple *first)
7668 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
7669 struct hash_entry *ident;
7670 eat(state, TOK_WHILE);
7671 eat(state, TOK_LPAREN);
7674 test = ltrue_expr(state, read_expr(state, test));
7675 eat(state, TOK_RPAREN);
7676 /* Generate the needed pieces */
7677 label1 = label(state);
7678 label2 = label(state);
7679 jmp1 = branch(state, label2, 0);
7680 jmp2 = branch(state, label1, test);
7682 /* Remember where break and continue go */
7684 ident = state->i_break;
7685 symbol(state, ident, &ident->sym_ident, end, end->type);
7686 ident = state->i_continue;
7687 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7688 /* Thread them together */
7689 flatten(state, first, jmp1);
7690 flatten(state, first, label1);
7691 statement(state, first);
7692 flatten(state, first, label2);
7693 flatten(state, first, test);
7694 flatten(state, first, jmp2);
7695 flatten(state, first, end);
7696 /* Cleanup the break/continue scope */
7700 static void do_statement(struct compile_state *state, struct triple *first)
7702 struct triple *label1, *label2, *test, *end;
7703 struct hash_entry *ident;
7705 /* Generate the needed pieces */
7706 label1 = label(state);
7707 label2 = label(state);
7709 /* Remember where break and continue go */
7711 ident = state->i_break;
7712 symbol(state, ident, &ident->sym_ident, end, end->type);
7713 ident = state->i_continue;
7714 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7715 /* Now include the body */
7716 flatten(state, first, label1);
7717 statement(state, first);
7718 /* Cleanup the break/continue scope */
7720 /* Eat the rest of the loop */
7721 eat(state, TOK_WHILE);
7722 eat(state, TOK_LPAREN);
7723 test = read_expr(state, expr(state));
7725 eat(state, TOK_RPAREN);
7726 eat(state, TOK_SEMI);
7727 /* Thread the pieces together */
7728 test = ltrue_expr(state, test);
7729 flatten(state, first, label2);
7730 flatten(state, first, test);
7731 flatten(state, first, branch(state, label1, test));
7732 flatten(state, first, end);
7736 static void return_statement(struct compile_state *state, struct triple *first)
7738 struct triple *jmp, *mv, *dest, *var, *val;
7740 eat(state, TOK_RETURN);
7742 #warning "FIXME implement a more general excess branch elimination"
7744 /* If we have a return value do some more work */
7745 if (peek(state) != TOK_SEMI) {
7746 val = read_expr(state, expr(state));
7748 eat(state, TOK_SEMI);
7750 /* See if this last statement in a function */
7751 last = ((peek(state) == TOK_RBRACE) &&
7752 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
7754 /* Find the return variable */
7755 var = MISC(state->main_function, 0);
7756 /* Find the return destination */
7757 dest = RHS(state->main_function, 0)->prev;
7759 /* If needed generate a jump instruction */
7761 jmp = branch(state, dest, 0);
7763 /* If needed generate an assignment instruction */
7765 mv = write_expr(state, var, val);
7767 /* Now put the code together */
7769 flatten(state, first, mv);
7770 flatten(state, first, jmp);
7773 flatten(state, first, jmp);
7777 static void break_statement(struct compile_state *state, struct triple *first)
7779 struct triple *dest;
7780 eat(state, TOK_BREAK);
7781 eat(state, TOK_SEMI);
7782 if (!state->i_break->sym_ident) {
7783 error(state, 0, "break statement not within loop or switch");
7785 dest = state->i_break->sym_ident->def;
7786 flatten(state, first, branch(state, dest, 0));
7789 static void continue_statement(struct compile_state *state, struct triple *first)
7791 struct triple *dest;
7792 eat(state, TOK_CONTINUE);
7793 eat(state, TOK_SEMI);
7794 if (!state->i_continue->sym_ident) {
7795 error(state, 0, "continue statement outside of a loop");
7797 dest = state->i_continue->sym_ident->def;
7798 flatten(state, first, branch(state, dest, 0));
7801 static void goto_statement(struct compile_state *state, struct triple *first)
7803 struct hash_entry *ident;
7804 eat(state, TOK_GOTO);
7805 eat(state, TOK_IDENT);
7806 ident = state->token[0].ident;
7807 if (!ident->sym_label) {
7808 /* If this is a forward branch allocate the label now,
7809 * it will be flattend in the appropriate location later.
7813 label_symbol(state, ident, ins);
7815 eat(state, TOK_SEMI);
7817 flatten(state, first, branch(state, ident->sym_label->def, 0));
7820 static void labeled_statement(struct compile_state *state, struct triple *first)
7823 struct hash_entry *ident;
7824 eat(state, TOK_IDENT);
7826 ident = state->token[0].ident;
7827 if (ident->sym_label && ident->sym_label->def) {
7828 ins = ident->sym_label->def;
7829 put_occurance(ins->occurance);
7830 ins->occurance = new_occurance(state);
7834 label_symbol(state, ident, ins);
7836 if (ins->id & TRIPLE_FLAG_FLATTENED) {
7837 error(state, 0, "label %s already defined", ident->name);
7839 flatten(state, first, ins);
7841 eat(state, TOK_COLON);
7842 statement(state, first);
7845 static void switch_statement(struct compile_state *state, struct triple *first)
7848 eat(state, TOK_SWITCH);
7849 eat(state, TOK_LPAREN);
7851 eat(state, TOK_RPAREN);
7852 statement(state, first);
7853 error(state, 0, "switch statements are not implemented");
7857 static void case_statement(struct compile_state *state, struct triple *first)
7860 eat(state, TOK_CASE);
7861 constant_expr(state);
7862 eat(state, TOK_COLON);
7863 statement(state, first);
7864 error(state, 0, "case statements are not implemented");
7868 static void default_statement(struct compile_state *state, struct triple *first)
7871 eat(state, TOK_DEFAULT);
7872 eat(state, TOK_COLON);
7873 statement(state, first);
7874 error(state, 0, "default statements are not implemented");
7878 static void asm_statement(struct compile_state *state, struct triple *first)
7880 struct asm_info *info;
7882 struct triple *constraint;
7883 struct triple *expr;
7884 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
7885 struct triple *def, *asm_str;
7886 int out, in, clobbers, more, colons, i;
7888 eat(state, TOK_ASM);
7889 /* For now ignore the qualifiers */
7890 switch(peek(state)) {
7892 eat(state, TOK_CONST);
7895 eat(state, TOK_VOLATILE);
7898 eat(state, TOK_LPAREN);
7899 asm_str = string_constant(state);
7902 out = in = clobbers = 0;
7904 if ((colons == 0) && (peek(state) == TOK_COLON)) {
7905 eat(state, TOK_COLON);
7907 more = (peek(state) == TOK_LIT_STRING);
7910 struct triple *constraint;
7913 if (out > MAX_LHS) {
7914 error(state, 0, "Maximum output count exceeded.");
7916 constraint = string_constant(state);
7917 str = constraint->u.blob;
7918 if (str[0] != '=') {
7919 error(state, 0, "Output constraint does not start with =");
7921 constraint->u.blob = str + 1;
7922 eat(state, TOK_LPAREN);
7923 var = conditional_expr(state);
7924 eat(state, TOK_RPAREN);
7927 out_param[out].constraint = constraint;
7928 out_param[out].expr = var;
7929 if (peek(state) == TOK_COMMA) {
7930 eat(state, TOK_COMMA);
7937 if ((colons == 1) && (peek(state) == TOK_COLON)) {
7938 eat(state, TOK_COLON);
7940 more = (peek(state) == TOK_LIT_STRING);
7943 struct triple *constraint;
7947 error(state, 0, "Maximum input count exceeded.");
7949 constraint = string_constant(state);
7950 str = constraint->u.blob;
7951 if (digitp(str[0] && str[1] == '\0')) {
7953 val = digval(str[0]);
7954 if ((val < 0) || (val >= out)) {
7955 error(state, 0, "Invalid input constraint %d", val);
7958 eat(state, TOK_LPAREN);
7959 val = conditional_expr(state);
7960 eat(state, TOK_RPAREN);
7962 in_param[in].constraint = constraint;
7963 in_param[in].expr = val;
7964 if (peek(state) == TOK_COMMA) {
7965 eat(state, TOK_COMMA);
7973 if ((colons == 2) && (peek(state) == TOK_COLON)) {
7974 eat(state, TOK_COLON);
7976 more = (peek(state) == TOK_LIT_STRING);
7978 struct triple *clobber;
7980 if ((clobbers + out) > MAX_LHS) {
7981 error(state, 0, "Maximum clobber limit exceeded.");
7983 clobber = string_constant(state);
7984 eat(state, TOK_RPAREN);
7986 clob_param[clobbers].constraint = clobber;
7987 if (peek(state) == TOK_COMMA) {
7988 eat(state, TOK_COMMA);
7994 eat(state, TOK_RPAREN);
7995 eat(state, TOK_SEMI);
7998 info = xcmalloc(sizeof(*info), "asm_info");
7999 info->str = asm_str->u.blob;
8000 free_triple(state, asm_str);
8002 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
8003 def->u.ainfo = info;
8005 /* Find the register constraints */
8006 for(i = 0; i < out; i++) {
8007 struct triple *constraint;
8008 constraint = out_param[i].constraint;
8009 info->tmpl.lhs[i] = arch_reg_constraint(state,
8010 out_param[i].expr->type, constraint->u.blob);
8011 free_triple(state, constraint);
8013 for(; i - out < clobbers; i++) {
8014 struct triple *constraint;
8015 constraint = clob_param[i - out].constraint;
8016 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
8017 free_triple(state, constraint);
8019 for(i = 0; i < in; i++) {
8020 struct triple *constraint;
8022 constraint = in_param[i].constraint;
8023 str = constraint->u.blob;
8024 if (digitp(str[0]) && str[1] == '\0') {
8025 struct reg_info cinfo;
8027 val = digval(str[0]);
8028 cinfo.reg = info->tmpl.lhs[val].reg;
8029 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
8030 cinfo.regcm &= info->tmpl.lhs[val].regcm;
8031 if (cinfo.reg == REG_UNSET) {
8032 cinfo.reg = REG_VIRT0 + val;
8034 if (cinfo.regcm == 0) {
8035 error(state, 0, "No registers for %d", val);
8037 info->tmpl.lhs[val] = cinfo;
8038 info->tmpl.rhs[i] = cinfo;
8041 info->tmpl.rhs[i] = arch_reg_constraint(state,
8042 in_param[i].expr->type, str);
8044 free_triple(state, constraint);
8047 /* Now build the helper expressions */
8048 for(i = 0; i < in; i++) {
8049 RHS(def, i) = read_expr(state,in_param[i].expr);
8051 flatten(state, first, def);
8052 for(i = 0; i < out; i++) {
8053 struct triple *piece;
8054 piece = triple(state, OP_PIECE, out_param[i].expr->type, def, 0);
8056 LHS(def, i) = piece;
8057 flatten(state, first,
8058 write_expr(state, out_param[i].expr, piece));
8060 for(; i - out < clobbers; i++) {
8061 struct triple *piece;
8062 piece = triple(state, OP_PIECE, &void_type, def, 0);
8064 LHS(def, i) = piece;
8065 flatten(state, first, piece);
8070 static int isdecl(int tok)
8093 case TOK_TYPE_NAME: /* typedef name */
8100 static void compound_statement(struct compile_state *state, struct triple *first)
8102 eat(state, TOK_LBRACE);
8105 /* statement-list opt */
8106 while (peek(state) != TOK_RBRACE) {
8107 statement(state, first);
8110 eat(state, TOK_RBRACE);
8113 static void statement(struct compile_state *state, struct triple *first)
8117 if (tok == TOK_LBRACE) {
8118 compound_statement(state, first);
8120 else if (tok == TOK_IF) {
8121 if_statement(state, first);
8123 else if (tok == TOK_FOR) {
8124 for_statement(state, first);
8126 else if (tok == TOK_WHILE) {
8127 while_statement(state, first);
8129 else if (tok == TOK_DO) {
8130 do_statement(state, first);
8132 else if (tok == TOK_RETURN) {
8133 return_statement(state, first);
8135 else if (tok == TOK_BREAK) {
8136 break_statement(state, first);
8138 else if (tok == TOK_CONTINUE) {
8139 continue_statement(state, first);
8141 else if (tok == TOK_GOTO) {
8142 goto_statement(state, first);
8144 else if (tok == TOK_SWITCH) {
8145 switch_statement(state, first);
8147 else if (tok == TOK_ASM) {
8148 asm_statement(state, first);
8150 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
8151 labeled_statement(state, first);
8153 else if (tok == TOK_CASE) {
8154 case_statement(state, first);
8156 else if (tok == TOK_DEFAULT) {
8157 default_statement(state, first);
8159 else if (isdecl(tok)) {
8160 /* This handles C99 intermixing of statements and decls */
8164 expr_statement(state, first);
8168 static struct type *param_decl(struct compile_state *state)
8171 struct hash_entry *ident;
8172 /* Cheat so the declarator will know we are not global */
8175 type = decl_specifiers(state);
8176 type = declarator(state, type, &ident, 0);
8177 type->field_ident = ident;
8182 static struct type *param_type_list(struct compile_state *state, struct type *type)
8184 struct type *ftype, **next;
8185 ftype = new_type(TYPE_FUNCTION, type, param_decl(state));
8186 next = &ftype->right;
8187 while(peek(state) == TOK_COMMA) {
8188 eat(state, TOK_COMMA);
8189 if (peek(state) == TOK_DOTS) {
8190 eat(state, TOK_DOTS);
8191 error(state, 0, "variadic functions not supported");
8194 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
8195 next = &((*next)->right);
8202 static struct type *type_name(struct compile_state *state)
8205 type = specifier_qualifier_list(state);
8206 /* abstract-declarator (may consume no tokens) */
8207 type = declarator(state, type, 0, 0);
8211 static struct type *direct_declarator(
8212 struct compile_state *state, struct type *type,
8213 struct hash_entry **ident, int need_ident)
8218 arrays_complete(state, type);
8219 switch(peek(state)) {
8221 eat(state, TOK_IDENT);
8223 error(state, 0, "Unexpected identifier found");
8225 /* The name of what we are declaring */
8226 *ident = state->token[0].ident;
8229 eat(state, TOK_LPAREN);
8230 outer = declarator(state, type, ident, need_ident);
8231 eat(state, TOK_RPAREN);
8235 error(state, 0, "Identifier expected");
8241 arrays_complete(state, type);
8242 switch(peek(state)) {
8244 eat(state, TOK_LPAREN);
8245 type = param_type_list(state, type);
8246 eat(state, TOK_RPAREN);
8250 unsigned int qualifiers;
8251 struct triple *value;
8253 eat(state, TOK_LBRACKET);
8254 if (peek(state) != TOK_RBRACKET) {
8255 value = constant_expr(state);
8256 integral(state, value);
8258 eat(state, TOK_RBRACKET);
8260 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
8261 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
8263 type->elements = value->u.cval;
8264 free_triple(state, value);
8266 type->elements = ELEMENT_COUNT_UNSPECIFIED;
8278 arrays_complete(state, type);
8280 for(inner = outer; inner->left; inner = inner->left)
8288 static struct type *declarator(
8289 struct compile_state *state, struct type *type,
8290 struct hash_entry **ident, int need_ident)
8292 while(peek(state) == TOK_STAR) {
8293 eat(state, TOK_STAR);
8294 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
8296 type = direct_declarator(state, type, ident, need_ident);
8301 static struct type *typedef_name(
8302 struct compile_state *state, unsigned int specifiers)
8304 struct hash_entry *ident;
8306 eat(state, TOK_TYPE_NAME);
8307 ident = state->token[0].ident;
8308 type = ident->sym_ident->type;
8309 specifiers |= type->type & QUAL_MASK;
8310 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
8311 (type->type & (STOR_MASK | QUAL_MASK))) {
8312 type = clone_type(specifiers, type);
8317 static struct type *enum_specifier(
8318 struct compile_state *state, unsigned int specifiers)
8324 eat(state, TOK_ENUM);
8326 if (tok == TOK_IDENT) {
8327 eat(state, TOK_IDENT);
8329 if ((tok != TOK_IDENT) || (peek(state) == TOK_LBRACE)) {
8330 eat(state, TOK_LBRACE);
8332 eat(state, TOK_IDENT);
8333 if (peek(state) == TOK_EQ) {
8335 constant_expr(state);
8337 if (peek(state) == TOK_COMMA) {
8338 eat(state, TOK_COMMA);
8340 } while(peek(state) != TOK_RBRACE);
8341 eat(state, TOK_RBRACE);
8348 static struct type *struct_declarator(
8349 struct compile_state *state, struct type *type, struct hash_entry **ident)
8352 #warning "struct_declarator is complicated because of bitfields, kill them?"
8354 if (tok != TOK_COLON) {
8355 type = declarator(state, type, ident, 1);
8357 if ((tok == TOK_COLON) || (peek(state) == TOK_COLON)) {
8358 eat(state, TOK_COLON);
8359 constant_expr(state);
8366 static struct type *struct_or_union_specifier(
8367 struct compile_state *state, unsigned int specifiers)
8369 struct type *struct_type;
8370 struct hash_entry *ident;
8371 unsigned int type_join;
8375 switch(peek(state)) {
8377 eat(state, TOK_STRUCT);
8378 type_join = TYPE_PRODUCT;
8381 eat(state, TOK_UNION);
8382 type_join = TYPE_OVERLAP;
8383 error(state, 0, "unions not yet supported\n");
8386 eat(state, TOK_STRUCT);
8387 type_join = TYPE_PRODUCT;
8391 if ((tok == TOK_IDENT) || (tok == TOK_TYPE_NAME)) {
8393 ident = state->token[0].ident;
8395 if (!ident || (peek(state) == TOK_LBRACE)) {
8398 eat(state, TOK_LBRACE);
8400 struct type *base_type;
8403 base_type = specifier_qualifier_list(state);
8404 next = &struct_type;
8407 struct hash_entry *fident;
8409 type = declarator(state, base_type, &fident, 1);
8411 if (peek(state) == TOK_COMMA) {
8413 eat(state, TOK_COMMA);
8415 type = clone_type(0, type);
8416 type->field_ident = fident;
8418 *next = new_type(type_join, *next, type);
8419 next = &((*next)->right);
8424 eat(state, TOK_SEMI);
8425 } while(peek(state) != TOK_RBRACE);
8426 eat(state, TOK_RBRACE);
8427 struct_type = new_type(TYPE_STRUCT, struct_type, 0);
8428 struct_type->type_ident = ident;
8429 struct_type->elements = elements;
8430 symbol(state, ident, &ident->sym_struct, 0, struct_type);
8432 if (ident && ident->sym_struct) {
8433 struct_type = ident->sym_struct->type;
8435 else if (ident && !ident->sym_struct) {
8436 error(state, 0, "struct %s undeclared", ident->name);
8441 static unsigned int storage_class_specifier_opt(struct compile_state *state)
8443 unsigned int specifiers;
8444 switch(peek(state)) {
8446 eat(state, TOK_AUTO);
8447 specifiers = STOR_AUTO;
8450 eat(state, TOK_REGISTER);
8451 specifiers = STOR_REGISTER;
8454 eat(state, TOK_STATIC);
8455 specifiers = STOR_STATIC;
8458 eat(state, TOK_EXTERN);
8459 specifiers = STOR_EXTERN;
8462 eat(state, TOK_TYPEDEF);
8463 specifiers = STOR_TYPEDEF;
8466 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
8467 specifiers = STOR_STATIC;
8470 specifiers = STOR_AUTO;
8476 static unsigned int function_specifier_opt(struct compile_state *state)
8478 /* Ignore the inline keyword */
8479 unsigned int specifiers;
8481 switch(peek(state)) {
8483 eat(state, TOK_INLINE);
8484 specifiers = STOR_INLINE;
8489 static unsigned int type_qualifiers(struct compile_state *state)
8491 unsigned int specifiers;
8494 specifiers = QUAL_NONE;
8496 switch(peek(state)) {
8498 eat(state, TOK_CONST);
8499 specifiers = QUAL_CONST;
8502 eat(state, TOK_VOLATILE);
8503 specifiers = QUAL_VOLATILE;
8506 eat(state, TOK_RESTRICT);
8507 specifiers = QUAL_RESTRICT;
8517 static struct type *type_specifier(
8518 struct compile_state *state, unsigned int spec)
8522 switch(peek(state)) {
8524 eat(state, TOK_VOID);
8525 type = new_type(TYPE_VOID | spec, 0, 0);
8528 eat(state, TOK_CHAR);
8529 type = new_type(TYPE_CHAR | spec, 0, 0);
8532 eat(state, TOK_SHORT);
8533 if (peek(state) == TOK_INT) {
8534 eat(state, TOK_INT);
8536 type = new_type(TYPE_SHORT | spec, 0, 0);
8539 eat(state, TOK_INT);
8540 type = new_type(TYPE_INT | spec, 0, 0);
8543 eat(state, TOK_LONG);
8544 switch(peek(state)) {
8546 eat(state, TOK_LONG);
8547 error(state, 0, "long long not supported");
8550 eat(state, TOK_DOUBLE);
8551 error(state, 0, "long double not supported");
8554 eat(state, TOK_INT);
8555 type = new_type(TYPE_LONG | spec, 0, 0);
8558 type = new_type(TYPE_LONG | spec, 0, 0);
8563 eat(state, TOK_FLOAT);
8564 error(state, 0, "type float not supported");
8567 eat(state, TOK_DOUBLE);
8568 error(state, 0, "type double not supported");
8571 eat(state, TOK_SIGNED);
8572 switch(peek(state)) {
8574 eat(state, TOK_LONG);
8575 switch(peek(state)) {
8577 eat(state, TOK_LONG);
8578 error(state, 0, "type long long not supported");
8581 eat(state, TOK_INT);
8582 type = new_type(TYPE_LONG | spec, 0, 0);
8585 type = new_type(TYPE_LONG | spec, 0, 0);
8590 eat(state, TOK_INT);
8591 type = new_type(TYPE_INT | spec, 0, 0);
8594 eat(state, TOK_SHORT);
8595 type = new_type(TYPE_SHORT | spec, 0, 0);
8598 eat(state, TOK_CHAR);
8599 type = new_type(TYPE_CHAR | spec, 0, 0);
8602 type = new_type(TYPE_INT | spec, 0, 0);
8607 eat(state, TOK_UNSIGNED);
8608 switch(peek(state)) {
8610 eat(state, TOK_LONG);
8611 switch(peek(state)) {
8613 eat(state, TOK_LONG);
8614 error(state, 0, "unsigned long long not supported");
8617 eat(state, TOK_INT);
8618 type = new_type(TYPE_ULONG | spec, 0, 0);
8621 type = new_type(TYPE_ULONG | spec, 0, 0);
8626 eat(state, TOK_INT);
8627 type = new_type(TYPE_UINT | spec, 0, 0);
8630 eat(state, TOK_SHORT);
8631 type = new_type(TYPE_USHORT | spec, 0, 0);
8634 eat(state, TOK_CHAR);
8635 type = new_type(TYPE_UCHAR | spec, 0, 0);
8638 type = new_type(TYPE_UINT | spec, 0, 0);
8642 /* struct or union specifier */
8645 type = struct_or_union_specifier(state, spec);
8647 /* enum-spefifier */
8649 type = enum_specifier(state, spec);
8653 type = typedef_name(state, spec);
8656 error(state, 0, "bad type specifier %s",
8657 tokens[peek(state)]);
8663 static int istype(int tok)
8689 static struct type *specifier_qualifier_list(struct compile_state *state)
8692 unsigned int specifiers = 0;
8694 /* type qualifiers */
8695 specifiers |= type_qualifiers(state);
8697 /* type specifier */
8698 type = type_specifier(state, specifiers);
8703 static int isdecl_specifier(int tok)
8706 /* storage class specifier */
8712 /* type qualifier */
8716 /* type specifiers */
8726 /* struct or union specifier */
8729 /* enum-spefifier */
8733 /* function specifiers */
8741 static struct type *decl_specifiers(struct compile_state *state)
8744 unsigned int specifiers;
8745 /* I am overly restrictive in the arragement of specifiers supported.
8746 * C is overly flexible in this department it makes interpreting
8747 * the parse tree difficult.
8751 /* storage class specifier */
8752 specifiers |= storage_class_specifier_opt(state);
8754 /* function-specifier */
8755 specifiers |= function_specifier_opt(state);
8757 /* type qualifier */
8758 specifiers |= type_qualifiers(state);
8760 /* type specifier */
8761 type = type_specifier(state, specifiers);
8765 static unsigned designator(struct compile_state *state)
8771 switch(peek(state)) {
8774 struct triple *value;
8775 eat(state, TOK_LBRACKET);
8776 value = constant_expr(state);
8777 eat(state, TOK_RBRACKET);
8778 index = value->u.cval;
8782 eat(state, TOK_DOT);
8783 eat(state, TOK_IDENT);
8784 error(state, 0, "Struct Designators not currently supported");
8787 error(state, 0, "Invalid designator");
8790 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
8795 static struct triple *initializer(
8796 struct compile_state *state, struct type *type)
8798 struct triple *result;
8799 if (peek(state) != TOK_LBRACE) {
8800 result = assignment_expr(state);
8804 unsigned index, max_index;
8806 max_index = index = 0;
8807 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
8808 max_index = type->elements;
8809 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
8813 error(state, 0, "Struct initializers not currently supported");
8815 buf = xcmalloc(size_of(state, type), "initializer");
8816 eat(state, TOK_LBRACE);
8818 struct triple *value;
8819 struct type *value_type;
8824 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
8825 index = designator(state);
8827 if ((max_index != ELEMENT_COUNT_UNSPECIFIED) &&
8828 (index > max_index)) {
8829 error(state, 0, "element beyond bounds");
8832 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
8833 value_type = type->left;
8835 value = eval_const_expr(state, initializer(state, value_type));
8836 value_size = size_of(state, value_type);
8837 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
8838 (max_index == ELEMENT_COUNT_UNSPECIFIED) &&
8839 (type->elements <= index)) {
8843 old_size = size_of(state, type);
8844 type->elements = index + 1;
8845 buf = xmalloc(size_of(state, type), "initializer");
8846 memcpy(buf, old_buf, old_size);
8849 if (value->op == OP_BLOBCONST) {
8850 memcpy((char *)buf + index * value_size, value->u.blob, value_size);
8852 else if ((value->op == OP_INTCONST) && (value_size == 1)) {
8853 *(((uint8_t *)buf) + index) = value->u.cval & 0xff;
8855 else if ((value->op == OP_INTCONST) && (value_size == 2)) {
8856 *(((uint16_t *)buf) + index) = value->u.cval & 0xffff;
8858 else if ((value->op == OP_INTCONST) && (value_size == 4)) {
8859 *(((uint32_t *)buf) + index) = value->u.cval & 0xffffffff;
8862 fprintf(stderr, "%d %d\n",
8863 value->op, value_size);
8864 internal_error(state, 0, "unhandled constant initializer");
8866 if (peek(state) == TOK_COMMA) {
8867 eat(state, TOK_COMMA);
8871 } while(comma && (peek(state) != TOK_RBRACE));
8872 eat(state, TOK_RBRACE);
8873 result = triple(state, OP_BLOBCONST, type, 0, 0);
8874 result->u.blob = buf;
8879 static void resolve_branches(struct compile_state *state)
8881 /* Make a second pass and finish anything outstanding
8882 * with respect to branches. The only outstanding item
8883 * is to see if there are goto to labels that have not
8884 * been defined and to error about them.
8887 for(i = 0; i < HASH_TABLE_SIZE; i++) {
8888 struct hash_entry *entry;
8889 for(entry = state->hash_table[i]; entry; entry = entry->next) {
8891 if (!entry->sym_label) {
8894 ins = entry->sym_label->def;
8895 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
8896 error(state, ins, "label `%s' used but not defined",
8903 static struct triple *function_definition(
8904 struct compile_state *state, struct type *type)
8906 struct triple *def, *tmp, *first, *end;
8907 struct hash_entry *ident;
8910 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
8911 error(state, 0, "Invalid function header");
8914 /* Verify the function type */
8915 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
8916 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
8917 (type->right->field_ident == 0)) {
8918 error(state, 0, "Invalid function parameters");
8920 param = type->right;
8922 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
8924 if (!param->left->field_ident) {
8925 error(state, 0, "No identifier for parameter %d\n", i);
8927 param = param->right;
8930 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
8931 error(state, 0, "No identifier for paramter %d\n", i);
8934 /* Get a list of statements for this function. */
8935 def = triple(state, OP_LIST, type, 0, 0);
8937 /* Start a new scope for the passed parameters */
8940 /* Put a label at the very start of a function */
8941 first = label(state);
8942 RHS(def, 0) = first;
8944 /* Put a label at the very end of a function */
8946 flatten(state, first, end);
8948 /* Walk through the parameters and create symbol table entries
8951 param = type->right;
8952 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
8953 ident = param->left->field_ident;
8954 tmp = variable(state, param->left);
8955 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
8956 flatten(state, end, tmp);
8957 param = param->right;
8959 if ((param->type & TYPE_MASK) != TYPE_VOID) {
8960 /* And don't forget the last parameter */
8961 ident = param->field_ident;
8962 tmp = variable(state, param);
8963 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
8964 flatten(state, end, tmp);
8966 /* Add a variable for the return value */
8968 if ((type->left->type & TYPE_MASK) != TYPE_VOID) {
8969 /* Remove all type qualifiers from the return type */
8970 tmp = variable(state, clone_type(0, type->left));
8971 flatten(state, end, tmp);
8972 /* Remember where the return value is */
8976 /* Remember which function I am compiling.
8977 * Also assume the last defined function is the main function.
8979 state->main_function = def;
8981 /* Now get the actual function definition */
8982 compound_statement(state, end);
8984 /* Finish anything unfinished with branches */
8985 resolve_branches(state);
8987 /* Remove the parameter scope */
8991 fprintf(stdout, "\n");
8992 loc(stdout, state, 0);
8993 fprintf(stdout, "\n__________ function_definition _________\n");
8994 print_triple(state, def);
8995 fprintf(stdout, "__________ function_definition _________ done\n\n");
9001 static struct triple *do_decl(struct compile_state *state,
9002 struct type *type, struct hash_entry *ident)
9006 /* Clean up the storage types used */
9007 switch (type->type & STOR_MASK) {
9010 /* These are the good types I am aiming for */
9013 type->type &= ~STOR_MASK;
9014 type->type |= STOR_AUTO;
9017 type->type &= ~STOR_MASK;
9018 type->type |= STOR_STATIC;
9022 error(state, 0, "typedef without name");
9024 symbol(state, ident, &ident->sym_ident, 0, type);
9025 ident->tok = TOK_TYPE_NAME;
9029 internal_error(state, 0, "Undefined storage class");
9031 if (((type->type & STOR_MASK) == STOR_STATIC) &&
9032 ((type->type & QUAL_CONST) == 0)) {
9033 error(state, 0, "non const static variables not supported");
9036 def = variable(state, type);
9037 symbol(state, ident, &ident->sym_ident, def, type);
9042 static void decl(struct compile_state *state, struct triple *first)
9044 struct type *base_type, *type;
9045 struct hash_entry *ident;
9048 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
9049 base_type = decl_specifiers(state);
9051 type = declarator(state, base_type, &ident, 0);
9052 if (global && ident && (peek(state) == TOK_LBRACE)) {
9054 state->function = ident->name;
9055 def = function_definition(state, type);
9056 symbol(state, ident, &ident->sym_ident, def, type);
9057 state->function = 0;
9061 flatten(state, first, do_decl(state, type, ident));
9062 /* type or variable definition */
9065 if (peek(state) == TOK_EQ) {
9067 error(state, 0, "cannot assign to a type");
9070 flatten(state, first,
9072 ident->sym_ident->def,
9073 initializer(state, type)));
9075 arrays_complete(state, type);
9076 if (peek(state) == TOK_COMMA) {
9077 eat(state, TOK_COMMA);
9079 type = declarator(state, base_type, &ident, 0);
9080 flatten(state, first, do_decl(state, type, ident));
9084 eat(state, TOK_SEMI);
9088 static void decls(struct compile_state *state)
9090 struct triple *list;
9092 list = label(state);
9095 if (tok == TOK_EOF) {
9098 if (tok == TOK_SPACE) {
9099 eat(state, TOK_SPACE);
9102 if (list->next != list) {
9103 error(state, 0, "global variables not supported");
9109 * Data structurs for optimation.
9112 static void do_use_block(
9113 struct block *used, struct block_set **head, struct block *user,
9116 struct block_set **ptr, *new;
9123 if ((*ptr)->member == user) {
9126 ptr = &(*ptr)->next;
9128 new = xcmalloc(sizeof(*new), "block_set");
9139 static void do_unuse_block(
9140 struct block *used, struct block_set **head, struct block *unuser)
9142 struct block_set *use, **ptr;
9146 if (use->member == unuser) {
9148 memset(use, -1, sizeof(*use));
9157 static void use_block(struct block *used, struct block *user)
9159 /* Append new to the head of the list, print_block
9162 do_use_block(used, &used->use, user, 1);
9165 static void unuse_block(struct block *used, struct block *unuser)
9167 do_unuse_block(used, &used->use, unuser);
9171 static void idom_block(struct block *idom, struct block *user)
9173 do_use_block(idom, &idom->idominates, user, 0);
9176 static void unidom_block(struct block *idom, struct block *unuser)
9178 do_unuse_block(idom, &idom->idominates, unuser);
9181 static void domf_block(struct block *block, struct block *domf)
9183 do_use_block(block, &block->domfrontier, domf, 0);
9186 static void undomf_block(struct block *block, struct block *undomf)
9188 do_unuse_block(block, &block->domfrontier, undomf);
9191 static void ipdom_block(struct block *ipdom, struct block *user)
9193 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
9196 static void unipdom_block(struct block *ipdom, struct block *unuser)
9198 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
9201 static void ipdomf_block(struct block *block, struct block *ipdomf)
9203 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
9206 static void unipdomf_block(struct block *block, struct block *unipdomf)
9208 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
9213 static int do_walk_triple(struct compile_state *state,
9214 struct triple *ptr, int depth,
9215 int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
9218 result = cb(state, ptr, depth);
9219 if ((result == 0) && (ptr->op == OP_LIST)) {
9220 struct triple *list;
9224 result = do_walk_triple(state, ptr, depth + 1, cb);
9225 if (ptr->next->prev != ptr) {
9226 internal_error(state, ptr->next, "bad prev");
9230 } while((result == 0) && (ptr != RHS(list, 0)));
9235 static int walk_triple(
9236 struct compile_state *state,
9238 int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
9240 return do_walk_triple(state, ptr, 0, cb);
9243 static void do_print_prefix(int depth)
9246 for(i = 0; i < depth; i++) {
9251 #define PRINT_LIST 1
9252 static int do_print_triple(struct compile_state *state, struct triple *ins, int depth)
9256 if (op == OP_LIST) {
9261 if ((op == OP_LABEL) && (ins->use)) {
9262 printf("\n%p:\n", ins);
9264 do_print_prefix(depth);
9265 display_triple(stdout, ins);
9267 if ((ins->op == OP_BRANCH) && ins->use) {
9268 internal_error(state, ins, "branch used?");
9272 struct triple_set *user;
9273 for(user = ins->use; user; user = user->next) {
9274 printf("use: %p\n", user->member);
9278 if (triple_is_branch(state, ins)) {
9284 static void print_triple(struct compile_state *state, struct triple *ins)
9286 walk_triple(state, ins, do_print_triple);
9289 static void print_triples(struct compile_state *state)
9291 print_triple(state, state->main_function);
9295 struct block *block;
9297 static void find_cf_blocks(struct cf_block *cf, struct block *block)
9299 if (!block || (cf[block->vertex].block == block)) {
9302 cf[block->vertex].block = block;
9303 find_cf_blocks(cf, block->left);
9304 find_cf_blocks(cf, block->right);
9307 static void print_control_flow(struct compile_state *state)
9309 struct cf_block *cf;
9311 printf("\ncontrol flow\n");
9312 cf = xcmalloc(sizeof(*cf) * (state->last_vertex + 1), "cf_block");
9313 find_cf_blocks(cf, state->first_block);
9315 for(i = 1; i <= state->last_vertex; i++) {
9316 struct block *block;
9317 block = cf[i].block;
9320 printf("(%p) %d:", block, block->vertex);
9322 printf(" %d", block->left->vertex);
9324 if (block->right && (block->right != block->left)) {
9325 printf(" %d", block->right->vertex);
9334 static struct block *basic_block(struct compile_state *state,
9335 struct triple *first)
9337 struct block *block;
9340 if (first->op != OP_LABEL) {
9341 internal_error(state, 0, "block does not start with a label");
9343 /* See if this basic block has already been setup */
9344 if (first->u.block != 0) {
9345 return first->u.block;
9347 /* Allocate another basic block structure */
9348 state->last_vertex += 1;
9349 block = xcmalloc(sizeof(*block), "block");
9350 block->first = block->last = first;
9351 block->vertex = state->last_vertex;
9354 if ((ptr != first) && (ptr->op == OP_LABEL) && ptr->use) {
9358 /* If ptr->u is not used remember where the baic block is */
9359 if (triple_stores_block(state, ptr)) {
9360 ptr->u.block = block;
9362 if (ptr->op == OP_BRANCH) {
9366 } while (ptr != RHS(state->main_function, 0));
9367 if (ptr == RHS(state->main_function, 0))
9370 if (op == OP_LABEL) {
9371 block->left = basic_block(state, ptr);
9373 use_block(block->left, block);
9375 else if (op == OP_BRANCH) {
9377 /* Trace the branch target */
9378 block->right = basic_block(state, TARG(ptr, 0));
9379 use_block(block->right, block);
9380 /* If there is a test trace the branch as well */
9381 if (TRIPLE_RHS(ptr->sizes)) {
9382 block->left = basic_block(state, ptr->next);
9383 use_block(block->left, block);
9387 internal_error(state, 0, "Bad basic block split");
9393 static void walk_blocks(struct compile_state *state,
9394 void (*cb)(struct compile_state *state, struct block *block, void *arg),
9397 struct triple *ptr, *first;
9398 struct block *last_block;
9400 first = RHS(state->main_function, 0);
9403 struct block *block;
9404 if (ptr->op == OP_LABEL) {
9405 block = ptr->u.block;
9406 if (block && (block != last_block)) {
9407 cb(state, block, arg);
9412 } while(ptr != first);
9415 static void print_block(
9416 struct compile_state *state, struct block *block, void *arg)
9421 fprintf(fp, "\nblock: %p (%d), %p<-%p %p<-%p\n",
9425 block->left && block->left->use?block->left->use->member : 0,
9427 block->right && block->right->use?block->right->use->member : 0);
9428 if (block->first->op == OP_LABEL) {
9429 fprintf(fp, "%p:\n", block->first);
9431 for(ptr = block->first; ; ptr = ptr->next) {
9432 struct triple_set *user;
9435 if (triple_stores_block(state, ptr)) {
9436 if (ptr->u.block != block) {
9437 internal_error(state, ptr,
9438 "Wrong block pointer: %p\n",
9442 if (op == OP_ADECL) {
9443 for(user = ptr->use; user; user = user->next) {
9444 if (!user->member->u.block) {
9445 internal_error(state, user->member,
9446 "Use %p not in a block?\n",
9451 display_triple(fp, ptr);
9454 for(user = ptr->use; user; user = user->next) {
9455 fprintf(fp, "use: %p\n", user->member);
9459 /* Sanity checks... */
9460 valid_ins(state, ptr);
9461 for(user = ptr->use; user; user = user->next) {
9464 valid_ins(state, use);
9465 if (triple_stores_block(state, user->member) &&
9466 !user->member->u.block) {
9467 internal_error(state, user->member,
9468 "Use %p not in a block?",
9473 if (ptr == block->last)
9480 static void print_blocks(struct compile_state *state, FILE *fp)
9482 fprintf(fp, "--------------- blocks ---------------\n");
9483 walk_blocks(state, print_block, fp);
9486 static void prune_nonblock_triples(struct compile_state *state)
9488 struct block *block;
9489 struct triple *first, *ins, *next;
9490 /* Delete the triples not in a basic block */
9491 first = RHS(state->main_function, 0);
9496 if (ins->op == OP_LABEL) {
9497 block = ins->u.block;
9500 release_triple(state, ins);
9503 } while(ins != first);
9506 static void setup_basic_blocks(struct compile_state *state)
9508 if (!triple_stores_block(state, RHS(state->main_function, 0)) ||
9509 !triple_stores_block(state, RHS(state->main_function,0)->prev)) {
9510 internal_error(state, 0, "ins will not store block?");
9512 /* Find the basic blocks */
9513 state->last_vertex = 0;
9514 state->first_block = basic_block(state, RHS(state->main_function,0));
9515 /* Delete the triples not in a basic block */
9516 prune_nonblock_triples(state);
9517 /* Find the last basic block */
9518 state->last_block = RHS(state->main_function, 0)->prev->u.block;
9519 if (!state->last_block) {
9520 internal_error(state, 0, "end not used?");
9522 /* Insert an extra unused edge from start to the end
9523 * This helps with reverse control flow calculations.
9525 use_block(state->first_block, state->last_block);
9526 /* If we are debugging print what I have just done */
9527 if (state->debug & DEBUG_BASIC_BLOCKS) {
9528 print_blocks(state, stdout);
9529 print_control_flow(state);
9533 static void free_basic_block(struct compile_state *state, struct block *block)
9535 struct block_set *entry, *next;
9536 struct block *child;
9540 if (block->vertex == -1) {
9545 unuse_block(block->left, block);
9548 unuse_block(block->right, block);
9551 unidom_block(block->idom, block);
9555 unipdom_block(block->ipdom, block);
9558 for(entry = block->use; entry; entry = next) {
9560 child = entry->member;
9561 unuse_block(block, child);
9562 if (child->left == block) {
9565 if (child->right == block) {
9569 for(entry = block->idominates; entry; entry = next) {
9571 child = entry->member;
9572 unidom_block(block, child);
9575 for(entry = block->domfrontier; entry; entry = next) {
9577 child = entry->member;
9578 undomf_block(block, child);
9580 for(entry = block->ipdominates; entry; entry = next) {
9582 child = entry->member;
9583 unipdom_block(block, child);
9586 for(entry = block->ipdomfrontier; entry; entry = next) {
9588 child = entry->member;
9589 unipdomf_block(block, child);
9591 if (block->users != 0) {
9592 internal_error(state, 0, "block still has users");
9594 free_basic_block(state, block->left);
9596 free_basic_block(state, block->right);
9598 memset(block, -1, sizeof(*block));
9602 static void free_basic_blocks(struct compile_state *state)
9604 struct triple *first, *ins;
9605 free_basic_block(state, state->first_block);
9606 state->last_vertex = 0;
9607 state->first_block = state->last_block = 0;
9608 first = RHS(state->main_function, 0);
9611 if (triple_stores_block(state, ins)) {
9615 } while(ins != first);
9620 struct block *block;
9621 struct sdom_block *sdominates;
9622 struct sdom_block *sdom_next;
9623 struct sdom_block *sdom;
9624 struct sdom_block *label;
9625 struct sdom_block *parent;
9626 struct sdom_block *ancestor;
9631 static void unsdom_block(struct sdom_block *block)
9633 struct sdom_block **ptr;
9634 if (!block->sdom_next) {
9637 ptr = &block->sdom->sdominates;
9639 if ((*ptr) == block) {
9640 *ptr = block->sdom_next;
9643 ptr = &(*ptr)->sdom_next;
9647 static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
9649 unsdom_block(block);
9651 block->sdom_next = sdom->sdominates;
9652 sdom->sdominates = block;
9657 static int initialize_sdblock(struct sdom_block *sd,
9658 struct block *parent, struct block *block, int vertex)
9660 if (!block || (sd[block->vertex].block == block)) {
9664 /* Renumber the blocks in a convinient fashion */
9665 block->vertex = vertex;
9666 sd[vertex].block = block;
9667 sd[vertex].sdom = &sd[vertex];
9668 sd[vertex].label = &sd[vertex];
9669 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
9670 sd[vertex].ancestor = 0;
9671 sd[vertex].vertex = vertex;
9672 vertex = initialize_sdblock(sd, block, block->left, vertex);
9673 vertex = initialize_sdblock(sd, block, block->right, vertex);
9677 static int initialize_sdpblock(struct sdom_block *sd,
9678 struct block *parent, struct block *block, int vertex)
9680 struct block_set *user;
9681 if (!block || (sd[block->vertex].block == block)) {
9685 /* Renumber the blocks in a convinient fashion */
9686 block->vertex = vertex;
9687 sd[vertex].block = block;
9688 sd[vertex].sdom = &sd[vertex];
9689 sd[vertex].label = &sd[vertex];
9690 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
9691 sd[vertex].ancestor = 0;
9692 sd[vertex].vertex = vertex;
9693 for(user = block->use; user; user = user->next) {
9694 vertex = initialize_sdpblock(sd, block, user->member, vertex);
9699 static void compress_ancestors(struct sdom_block *v)
9701 /* This procedure assumes ancestor(v) != 0 */
9702 /* if (ancestor(ancestor(v)) != 0) {
9703 * compress(ancestor(ancestor(v)));
9704 * if (semi(label(ancestor(v))) < semi(label(v))) {
9705 * label(v) = label(ancestor(v));
9707 * ancestor(v) = ancestor(ancestor(v));
9713 if (v->ancestor->ancestor) {
9714 compress_ancestors(v->ancestor->ancestor);
9715 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
9716 v->label = v->ancestor->label;
9718 v->ancestor = v->ancestor->ancestor;
9722 static void compute_sdom(struct compile_state *state, struct sdom_block *sd)
9726 * for each v <= pred(w) {
9728 * if (semi[u] < semi[w] {
9729 * semi[w] = semi[u];
9732 * add w to bucket(vertex(semi[w]));
9733 * LINK(parent(w), w);
9736 * for each v <= bucket(parent(w)) {
9737 * delete v from bucket(parent(w));
9739 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
9742 for(i = state->last_vertex; i >= 2; i--) {
9743 struct sdom_block *v, *parent, *next;
9744 struct block_set *user;
9745 struct block *block;
9746 block = sd[i].block;
9747 parent = sd[i].parent;
9749 for(user = block->use; user; user = user->next) {
9750 struct sdom_block *v, *u;
9751 v = &sd[user->member->vertex];
9752 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9753 if (u->sdom->vertex < sd[i].sdom->vertex) {
9754 sd[i].sdom = u->sdom;
9757 sdom_block(sd[i].sdom, &sd[i]);
9758 sd[i].ancestor = parent;
9760 for(v = parent->sdominates; v; v = next) {
9761 struct sdom_block *u;
9762 next = v->sdom_next;
9764 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
9765 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
9766 u->block : parent->block;
9771 static void compute_spdom(struct compile_state *state, struct sdom_block *sd)
9775 * for each v <= pred(w) {
9777 * if (semi[u] < semi[w] {
9778 * semi[w] = semi[u];
9781 * add w to bucket(vertex(semi[w]));
9782 * LINK(parent(w), w);
9785 * for each v <= bucket(parent(w)) {
9786 * delete v from bucket(parent(w));
9788 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
9791 for(i = state->last_vertex; i >= 2; i--) {
9792 struct sdom_block *u, *v, *parent, *next;
9793 struct block *block;
9794 block = sd[i].block;
9795 parent = sd[i].parent;
9798 v = &sd[block->left->vertex];
9799 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9800 if (u->sdom->vertex < sd[i].sdom->vertex) {
9801 sd[i].sdom = u->sdom;
9804 if (block->right && (block->right != block->left)) {
9805 v = &sd[block->right->vertex];
9806 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9807 if (u->sdom->vertex < sd[i].sdom->vertex) {
9808 sd[i].sdom = u->sdom;
9811 sdom_block(sd[i].sdom, &sd[i]);
9812 sd[i].ancestor = parent;
9814 for(v = parent->sdominates; v; v = next) {
9815 struct sdom_block *u;
9816 next = v->sdom_next;
9818 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
9819 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
9820 u->block : parent->block;
9825 static void compute_idom(struct compile_state *state, struct sdom_block *sd)
9828 for(i = 2; i <= state->last_vertex; i++) {
9829 struct block *block;
9830 block = sd[i].block;
9831 if (block->idom->vertex != sd[i].sdom->vertex) {
9832 block->idom = block->idom->idom;
9834 idom_block(block->idom, block);
9836 sd[1].block->idom = 0;
9839 static void compute_ipdom(struct compile_state *state, struct sdom_block *sd)
9842 for(i = 2; i <= state->last_vertex; i++) {
9843 struct block *block;
9844 block = sd[i].block;
9845 if (block->ipdom->vertex != sd[i].sdom->vertex) {
9846 block->ipdom = block->ipdom->ipdom;
9848 ipdom_block(block->ipdom, block);
9850 sd[1].block->ipdom = 0;
9854 * Every vertex of a flowgraph G = (V, E, r) except r has
9855 * a unique immediate dominator.
9856 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
9857 * rooted at r, called the dominator tree of G, such that
9858 * v dominates w if and only if v is a proper ancestor of w in
9859 * the dominator tree.
9862 * If v and w are vertices of G such that v <= w,
9863 * than any path from v to w must contain a common ancestor
9866 /* Lemma 2: For any vertex w != r, idom(w) -> w */
9867 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
9868 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
9870 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
9871 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
9874 * Let w != r and let u be a vertex for which sdom(u) is
9875 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
9876 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
9878 /* Lemma 5: Let vertices v,w satisfy v -> w.
9879 * Then v -> idom(w) or idom(w) -> idom(v)
9882 static void find_immediate_dominators(struct compile_state *state)
9884 struct sdom_block *sd;
9885 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
9886 * vi > w for (1 <= i <= k - 1}
9889 * For any vertex w != r.
9891 * {v|(v,w) <= E and v < w } U
9892 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
9895 * Let w != r and let u be a vertex for which sdom(u) is
9896 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
9898 * { sdom(w) if sdom(w) = sdom(u),
9900 * { idom(u) otherwise
9902 /* The algorithm consists of the following 4 steps.
9903 * Step 1. Carry out a depth-first search of the problem graph.
9904 * Number the vertices from 1 to N as they are reached during
9905 * the search. Initialize the variables used in succeeding steps.
9906 * Step 2. Compute the semidominators of all vertices by applying
9907 * theorem 4. Carry out the computation vertex by vertex in
9908 * decreasing order by number.
9909 * Step 3. Implicitly define the immediate dominator of each vertex
9910 * by applying Corollary 1.
9911 * Step 4. Explicitly define the immediate dominator of each vertex,
9912 * carrying out the computation vertex by vertex in increasing order
9915 /* Step 1 initialize the basic block information */
9916 sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
9917 initialize_sdblock(sd, 0, state->first_block, 0);
9923 /* Step 2 compute the semidominators */
9924 /* Step 3 implicitly define the immediate dominator of each vertex */
9925 compute_sdom(state, sd);
9926 /* Step 4 explicitly define the immediate dominator of each vertex */
9927 compute_idom(state, sd);
9931 static void find_post_dominators(struct compile_state *state)
9933 struct sdom_block *sd;
9934 /* Step 1 initialize the basic block information */
9935 sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
9937 initialize_sdpblock(sd, 0, state->last_block, 0);
9939 /* Step 2 compute the semidominators */
9940 /* Step 3 implicitly define the immediate dominator of each vertex */
9941 compute_spdom(state, sd);
9942 /* Step 4 explicitly define the immediate dominator of each vertex */
9943 compute_ipdom(state, sd);
9949 static void find_block_domf(struct compile_state *state, struct block *block)
9951 struct block *child;
9952 struct block_set *user;
9953 if (block->domfrontier != 0) {
9954 internal_error(state, block->first, "domfrontier present?");
9956 for(user = block->idominates; user; user = user->next) {
9957 child = user->member;
9958 if (child->idom != block) {
9959 internal_error(state, block->first, "bad idom");
9961 find_block_domf(state, child);
9963 if (block->left && block->left->idom != block) {
9964 domf_block(block, block->left);
9966 if (block->right && block->right->idom != block) {
9967 domf_block(block, block->right);
9969 for(user = block->idominates; user; user = user->next) {
9970 struct block_set *frontier;
9971 child = user->member;
9972 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
9973 if (frontier->member->idom != block) {
9974 domf_block(block, frontier->member);
9980 static void find_block_ipdomf(struct compile_state *state, struct block *block)
9982 struct block *child;
9983 struct block_set *user;
9984 if (block->ipdomfrontier != 0) {
9985 internal_error(state, block->first, "ipdomfrontier present?");
9987 for(user = block->ipdominates; user; user = user->next) {
9988 child = user->member;
9989 if (child->ipdom != block) {
9990 internal_error(state, block->first, "bad ipdom");
9992 find_block_ipdomf(state, child);
9994 if (block->left && block->left->ipdom != block) {
9995 ipdomf_block(block, block->left);
9997 if (block->right && block->right->ipdom != block) {
9998 ipdomf_block(block, block->right);
10000 for(user = block->idominates; user; user = user->next) {
10001 struct block_set *frontier;
10002 child = user->member;
10003 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
10004 if (frontier->member->ipdom != block) {
10005 ipdomf_block(block, frontier->member);
10011 static void print_dominated(
10012 struct compile_state *state, struct block *block, void *arg)
10014 struct block_set *user;
10017 fprintf(fp, "%d:", block->vertex);
10018 for(user = block->idominates; user; user = user->next) {
10019 fprintf(fp, " %d", user->member->vertex);
10020 if (user->member->idom != block) {
10021 internal_error(state, user->member->first, "bad idom");
10027 static void print_dominators(struct compile_state *state, FILE *fp)
10029 fprintf(fp, "\ndominates\n");
10030 walk_blocks(state, print_dominated, fp);
10034 static int print_frontiers(
10035 struct compile_state *state, struct block *block, int vertex)
10037 struct block_set *user;
10039 if (!block || (block->vertex != vertex + 1)) {
10044 printf("%d:", block->vertex);
10045 for(user = block->domfrontier; user; user = user->next) {
10046 printf(" %d", user->member->vertex);
10050 vertex = print_frontiers(state, block->left, vertex);
10051 vertex = print_frontiers(state, block->right, vertex);
10054 static void print_dominance_frontiers(struct compile_state *state)
10056 printf("\ndominance frontiers\n");
10057 print_frontiers(state, state->first_block, 0);
10061 static void analyze_idominators(struct compile_state *state)
10063 /* Find the immediate dominators */
10064 find_immediate_dominators(state);
10065 /* Find the dominance frontiers */
10066 find_block_domf(state, state->first_block);
10067 /* If debuging print the print what I have just found */
10068 if (state->debug & DEBUG_FDOMINATORS) {
10069 print_dominators(state, stdout);
10070 print_dominance_frontiers(state);
10071 print_control_flow(state);
10077 static void print_ipdominated(
10078 struct compile_state *state, struct block *block, void *arg)
10080 struct block_set *user;
10083 fprintf(fp, "%d:", block->vertex);
10084 for(user = block->ipdominates; user; user = user->next) {
10085 fprintf(fp, " %d", user->member->vertex);
10086 if (user->member->ipdom != block) {
10087 internal_error(state, user->member->first, "bad ipdom");
10093 static void print_ipdominators(struct compile_state *state, FILE *fp)
10095 fprintf(fp, "\nipdominates\n");
10096 walk_blocks(state, print_ipdominated, fp);
10099 static int print_pfrontiers(
10100 struct compile_state *state, struct block *block, int vertex)
10102 struct block_set *user;
10104 if (!block || (block->vertex != vertex + 1)) {
10109 printf("%d:", block->vertex);
10110 for(user = block->ipdomfrontier; user; user = user->next) {
10111 printf(" %d", user->member->vertex);
10114 for(user = block->use; user; user = user->next) {
10115 vertex = print_pfrontiers(state, user->member, vertex);
10119 static void print_ipdominance_frontiers(struct compile_state *state)
10121 printf("\nipdominance frontiers\n");
10122 print_pfrontiers(state, state->last_block, 0);
10126 static void analyze_ipdominators(struct compile_state *state)
10128 /* Find the post dominators */
10129 find_post_dominators(state);
10130 /* Find the control dependencies (post dominance frontiers) */
10131 find_block_ipdomf(state, state->last_block);
10132 /* If debuging print the print what I have just found */
10133 if (state->debug & DEBUG_RDOMINATORS) {
10134 print_ipdominators(state, stdout);
10135 print_ipdominance_frontiers(state);
10136 print_control_flow(state);
10140 static int bdominates(struct compile_state *state,
10141 struct block *dom, struct block *sub)
10143 while(sub && (sub != dom)) {
10149 static int tdominates(struct compile_state *state,
10150 struct triple *dom, struct triple *sub)
10152 struct block *bdom, *bsub;
10154 bdom = block_of_triple(state, dom);
10155 bsub = block_of_triple(state, sub);
10156 if (bdom != bsub) {
10157 result = bdominates(state, bdom, bsub);
10160 struct triple *ins;
10162 while((ins != bsub->first) && (ins != dom)) {
10165 result = (ins == dom);
10170 static void insert_phi_operations(struct compile_state *state)
10173 struct triple *first;
10174 int *has_already, *work;
10175 struct block *work_list, **work_list_tail;
10177 struct triple *var;
10179 size = sizeof(int) * (state->last_vertex + 1);
10180 has_already = xcmalloc(size, "has_already");
10181 work = xcmalloc(size, "work");
10184 first = RHS(state->main_function, 0);
10185 for(var = first->next; var != first ; var = var->next) {
10186 struct block *block;
10187 struct triple_set *user;
10188 if ((var->op != OP_ADECL) || !var->use) {
10193 work_list_tail = &work_list;
10194 for(user = var->use; user; user = user->next) {
10195 if (user->member->op == OP_READ) {
10198 if (user->member->op != OP_WRITE) {
10199 internal_error(state, user->member,
10200 "bad variable access");
10202 block = user->member->u.block;
10204 warning(state, user->member, "dead code");
10206 if (work[block->vertex] >= iter) {
10209 work[block->vertex] = iter;
10210 *work_list_tail = block;
10211 block->work_next = 0;
10212 work_list_tail = &block->work_next;
10214 for(block = work_list; block; block = block->work_next) {
10215 struct block_set *df;
10216 for(df = block->domfrontier; df; df = df->next) {
10217 struct triple *phi;
10218 struct block *front;
10220 front = df->member;
10222 if (has_already[front->vertex] >= iter) {
10225 /* Count how many edges flow into this block */
10226 in_edges = front->users;
10227 /* Insert a phi function for this variable */
10228 get_occurance(front->first->occurance);
10229 phi = alloc_triple(
10230 state, OP_PHI, var->type, -1, in_edges,
10231 front->first->occurance);
10232 phi->u.block = front;
10233 MISC(phi, 0) = var;
10234 use_triple(var, phi);
10235 /* Insert the phi functions immediately after the label */
10236 insert_triple(state, front->first->next, phi);
10237 if (front->first == front->last) {
10238 front->last = front->first->next;
10240 has_already[front->vertex] = iter;
10242 /* If necessary plan to visit the basic block */
10243 if (work[front->vertex] >= iter) {
10246 work[front->vertex] = iter;
10247 *work_list_tail = front;
10248 front->work_next = 0;
10249 work_list_tail = &front->work_next;
10253 xfree(has_already);
10261 static void fixup_block_phi_variables(
10262 struct compile_state *state, struct block *parent, struct block *block)
10264 struct block_set *set;
10265 struct triple *ptr;
10267 if (!parent || !block)
10269 /* Find the edge I am coming in on */
10271 for(set = block->use; set; set = set->next, edge++) {
10272 if (set->member == parent) {
10277 internal_error(state, 0, "phi input is not on a control predecessor");
10279 for(ptr = block->first; ; ptr = ptr->next) {
10280 if (ptr->op == OP_PHI) {
10281 struct triple *var, *val, **slot;
10282 var = MISC(ptr, 0);
10284 internal_error(state, ptr, "no var???");
10286 /* Find the current value of the variable */
10287 val = var->use->member;
10288 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10289 internal_error(state, val, "bad value in phi");
10291 if (edge >= TRIPLE_RHS(ptr->sizes)) {
10292 internal_error(state, ptr, "edges > phi rhs");
10294 slot = &RHS(ptr, edge);
10295 if ((*slot != 0) && (*slot != val)) {
10296 internal_error(state, ptr, "phi already bound on this edge");
10299 use_triple(val, ptr);
10301 if (ptr == block->last) {
10308 static void rename_block_variables(
10309 struct compile_state *state, struct block *block)
10311 struct block_set *user;
10312 struct triple *ptr, *next, *last;
10316 last = block->first;
10318 for(ptr = block->first; !done; ptr = next) {
10320 if (ptr == block->last) {
10324 if (ptr->op == OP_READ) {
10325 struct triple *var, *val;
10327 unuse_triple(var, ptr);
10329 error(state, ptr, "variable used without being set");
10331 /* Find the current value of the variable */
10332 val = var->use->member;
10333 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10334 internal_error(state, val, "bad value in read");
10336 propogate_use(state, ptr, val);
10337 release_triple(state, ptr);
10341 if (ptr->op == OP_WRITE) {
10342 struct triple *var, *val;
10345 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10346 internal_error(state, val, "bad value in write");
10348 propogate_use(state, ptr, val);
10349 unuse_triple(var, ptr);
10350 /* Push OP_WRITE ptr->right onto a stack of variable uses */
10351 push_triple(var, val);
10353 if (ptr->op == OP_PHI) {
10354 struct triple *var;
10355 var = MISC(ptr, 0);
10356 /* Push OP_PHI onto a stack of variable uses */
10357 push_triple(var, ptr);
10361 block->last = last;
10363 /* Fixup PHI functions in the cf successors */
10364 fixup_block_phi_variables(state, block, block->left);
10365 fixup_block_phi_variables(state, block, block->right);
10366 /* rename variables in the dominated nodes */
10367 for(user = block->idominates; user; user = user->next) {
10368 rename_block_variables(state, user->member);
10370 /* pop the renamed variable stack */
10371 last = block->first;
10373 for(ptr = block->first; !done ; ptr = next) {
10375 if (ptr == block->last) {
10378 if (ptr->op == OP_WRITE) {
10379 struct triple *var;
10381 /* Pop OP_WRITE ptr->right from the stack of variable uses */
10382 pop_triple(var, RHS(ptr, 0));
10383 release_triple(state, ptr);
10386 if (ptr->op == OP_PHI) {
10387 struct triple *var;
10388 var = MISC(ptr, 0);
10389 /* Pop OP_WRITE ptr->right from the stack of variable uses */
10390 pop_triple(var, ptr);
10394 block->last = last;
10397 static void prune_block_variables(struct compile_state *state,
10398 struct block *block)
10400 struct block_set *user;
10401 struct triple *next, *last, *ptr;
10403 last = block->first;
10405 for(ptr = block->first; !done; ptr = next) {
10407 if (ptr == block->last) {
10410 if (ptr->op == OP_ADECL) {
10411 struct triple_set *user, *next;
10412 for(user = ptr->use; user; user = next) {
10413 struct triple *use;
10415 use = user->member;
10416 if (use->op != OP_PHI) {
10417 internal_error(state, use, "decl still used");
10419 if (MISC(use, 0) != ptr) {
10420 internal_error(state, use, "bad phi use of decl");
10422 unuse_triple(ptr, use);
10425 release_triple(state, ptr);
10430 block->last = last;
10431 for(user = block->idominates; user; user = user->next) {
10432 prune_block_variables(state, user->member);
10436 static void transform_to_ssa_form(struct compile_state *state)
10438 insert_phi_operations(state);
10440 printf("@%s:%d\n", __FILE__, __LINE__);
10441 print_blocks(state, stdout);
10443 rename_block_variables(state, state->first_block);
10444 prune_block_variables(state, state->first_block);
10448 static void clear_vertex(
10449 struct compile_state *state, struct block *block, void *arg)
10454 static void mark_live_block(
10455 struct compile_state *state, struct block *block, int *next_vertex)
10457 /* See if this is a block that has not been marked */
10458 if (block->vertex != 0) {
10461 block->vertex = *next_vertex;
10463 if (triple_is_branch(state, block->last)) {
10464 struct triple **targ;
10465 targ = triple_targ(state, block->last, 0);
10466 for(; targ; targ = triple_targ(state, block->last, targ)) {
10470 if (!triple_stores_block(state, *targ)) {
10471 internal_error(state, 0, "bad targ");
10473 mark_live_block(state, (*targ)->u.block, next_vertex);
10476 else if (block->last->next != RHS(state->main_function, 0)) {
10477 struct triple *ins;
10478 ins = block->last->next;
10479 if (!triple_stores_block(state, ins)) {
10480 internal_error(state, 0, "bad block start");
10482 mark_live_block(state, ins->u.block, next_vertex);
10486 static void transform_from_ssa_form(struct compile_state *state)
10488 /* To get out of ssa form we insert moves on the incoming
10489 * edges to blocks containting phi functions.
10491 struct triple *first;
10492 struct triple *phi, *next;
10495 /* Walk the control flow to see which blocks remain alive */
10496 walk_blocks(state, clear_vertex, 0);
10498 mark_live_block(state, state->first_block, &next_vertex);
10500 /* Walk all of the operations to find the phi functions */
10501 first = RHS(state->main_function, 0);
10502 for(phi = first->next; phi != first ; phi = next) {
10503 struct block_set *set;
10504 struct block *block;
10505 struct triple **slot;
10506 struct triple *var, *read;
10507 struct triple_set *use, *use_next;
10510 if (phi->op != OP_PHI) {
10513 block = phi->u.block;
10514 slot = &RHS(phi, 0);
10516 /* Forget uses from code in dead blocks */
10517 for(use = phi->use; use; use = use_next) {
10518 struct block *ublock;
10519 struct triple **expr;
10520 use_next = use->next;
10521 ublock = block_of_triple(state, use->member);
10522 if ((use->member == phi) || (ublock->vertex != 0)) {
10525 expr = triple_rhs(state, use->member, 0);
10526 for(; expr; expr = triple_rhs(state, use->member, expr)) {
10527 if (*expr == phi) {
10531 unuse_triple(phi, use->member);
10534 /* A variable to replace the phi function */
10535 var = post_triple(state, phi, OP_ADECL, phi->type, 0,0);
10536 /* A read of the single value that is set into the variable */
10537 read = post_triple(state, var, OP_READ, phi->type, var, 0);
10538 use_triple(var, read);
10540 /* Replaces uses of the phi with variable reads */
10541 propogate_use(state, phi, read);
10543 /* Walk all of the incoming edges/blocks and insert moves.
10545 for(edge = 0, set = block->use; set; set = set->next, edge++) {
10546 struct block *eblock;
10547 struct triple *move;
10548 struct triple *val;
10549 eblock = set->member;
10552 unuse_triple(val, phi);
10554 if (!val || (val == &zero_triple) ||
10555 (block->vertex == 0) || (eblock->vertex == 0) ||
10556 (val == phi) || (val == read)) {
10560 move = post_triple(state,
10561 val, OP_WRITE, phi->type, var, val);
10562 use_triple(val, move);
10563 use_triple(var, move);
10565 /* See if there are any writers of var */
10567 for(use = var->use; use; use = use->next) {
10568 struct triple **expr;
10569 expr = triple_lhs(state, use->member, 0);
10570 for(; expr; expr = triple_lhs(state, use->member, expr)) {
10571 if (*expr == var) {
10576 /* If var is not used free it */
10578 unuse_triple(var, read);
10579 free_triple(state, read);
10580 free_triple(state, var);
10583 /* Release the phi function */
10584 release_triple(state, phi);
10591 * Register conflict resolution
10592 * =========================================================
10595 static struct reg_info find_def_color(
10596 struct compile_state *state, struct triple *def)
10598 struct triple_set *set;
10599 struct reg_info info;
10600 info.reg = REG_UNSET;
10602 if (!triple_is_def(state, def)) {
10605 info = arch_reg_lhs(state, def, 0);
10606 if (info.reg >= MAX_REGISTERS) {
10607 info.reg = REG_UNSET;
10609 for(set = def->use; set; set = set->next) {
10610 struct reg_info tinfo;
10612 i = find_rhs_use(state, set->member, def);
10616 tinfo = arch_reg_rhs(state, set->member, i);
10617 if (tinfo.reg >= MAX_REGISTERS) {
10618 tinfo.reg = REG_UNSET;
10620 if ((tinfo.reg != REG_UNSET) &&
10621 (info.reg != REG_UNSET) &&
10622 (tinfo.reg != info.reg)) {
10623 internal_error(state, def, "register conflict");
10625 if ((info.regcm & tinfo.regcm) == 0) {
10626 internal_error(state, def, "regcm conflict %x & %x == 0",
10627 info.regcm, tinfo.regcm);
10629 if (info.reg == REG_UNSET) {
10630 info.reg = tinfo.reg;
10632 info.regcm &= tinfo.regcm;
10634 if (info.reg >= MAX_REGISTERS) {
10635 internal_error(state, def, "register out of range");
10640 static struct reg_info find_lhs_pre_color(
10641 struct compile_state *state, struct triple *ins, int index)
10643 struct reg_info info;
10645 zrhs = TRIPLE_RHS(ins->sizes);
10646 zlhs = TRIPLE_LHS(ins->sizes);
10647 if (!zlhs && triple_is_def(state, ins)) {
10650 if (index >= zlhs) {
10651 internal_error(state, ins, "Bad lhs %d", index);
10653 info = arch_reg_lhs(state, ins, index);
10654 for(i = 0; i < zrhs; i++) {
10655 struct reg_info rinfo;
10656 rinfo = arch_reg_rhs(state, ins, i);
10657 if ((info.reg == rinfo.reg) &&
10658 (rinfo.reg >= MAX_REGISTERS)) {
10659 struct reg_info tinfo;
10660 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
10661 info.reg = tinfo.reg;
10662 info.regcm &= tinfo.regcm;
10666 if (info.reg >= MAX_REGISTERS) {
10667 info.reg = REG_UNSET;
10672 static struct reg_info find_rhs_post_color(
10673 struct compile_state *state, struct triple *ins, int index);
10675 static struct reg_info find_lhs_post_color(
10676 struct compile_state *state, struct triple *ins, int index)
10678 struct triple_set *set;
10679 struct reg_info info;
10680 struct triple *lhs;
10682 fprintf(stderr, "find_lhs_post_color(%p, %d)\n",
10685 if ((index == 0) && triple_is_def(state, ins)) {
10688 else if (index < TRIPLE_LHS(ins->sizes)) {
10689 lhs = LHS(ins, index);
10692 internal_error(state, ins, "Bad lhs %d", index);
10695 info = arch_reg_lhs(state, ins, index);
10696 if (info.reg >= MAX_REGISTERS) {
10697 info.reg = REG_UNSET;
10699 for(set = lhs->use; set; set = set->next) {
10700 struct reg_info rinfo;
10701 struct triple *user;
10703 user = set->member;
10704 zrhs = TRIPLE_RHS(user->sizes);
10705 for(i = 0; i < zrhs; i++) {
10706 if (RHS(user, i) != lhs) {
10709 rinfo = find_rhs_post_color(state, user, i);
10710 if ((info.reg != REG_UNSET) &&
10711 (rinfo.reg != REG_UNSET) &&
10712 (info.reg != rinfo.reg)) {
10713 internal_error(state, ins, "register conflict");
10715 if ((info.regcm & rinfo.regcm) == 0) {
10716 internal_error(state, ins, "regcm conflict %x & %x == 0",
10717 info.regcm, rinfo.regcm);
10719 if (info.reg == REG_UNSET) {
10720 info.reg = rinfo.reg;
10722 info.regcm &= rinfo.regcm;
10726 fprintf(stderr, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
10727 ins, index, info.reg, info.regcm);
10732 static struct reg_info find_rhs_post_color(
10733 struct compile_state *state, struct triple *ins, int index)
10735 struct reg_info info, rinfo;
10738 fprintf(stderr, "find_rhs_post_color(%p, %d)\n",
10741 rinfo = arch_reg_rhs(state, ins, index);
10742 zlhs = TRIPLE_LHS(ins->sizes);
10743 if (!zlhs && triple_is_def(state, ins)) {
10747 if (info.reg >= MAX_REGISTERS) {
10748 info.reg = REG_UNSET;
10750 for(i = 0; i < zlhs; i++) {
10751 struct reg_info linfo;
10752 linfo = arch_reg_lhs(state, ins, i);
10753 if ((linfo.reg == rinfo.reg) &&
10754 (linfo.reg >= MAX_REGISTERS)) {
10755 struct reg_info tinfo;
10756 tinfo = find_lhs_post_color(state, ins, i);
10757 if (tinfo.reg >= MAX_REGISTERS) {
10758 tinfo.reg = REG_UNSET;
10760 info.regcm &= linfo.reg;
10761 info.regcm &= tinfo.regcm;
10762 if (info.reg != REG_UNSET) {
10763 internal_error(state, ins, "register conflict");
10765 if (info.regcm == 0) {
10766 internal_error(state, ins, "regcm conflict");
10768 info.reg = tinfo.reg;
10772 fprintf(stderr, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
10773 ins, index, info.reg, info.regcm);
10778 static struct reg_info find_lhs_color(
10779 struct compile_state *state, struct triple *ins, int index)
10781 struct reg_info pre, post, info;
10783 fprintf(stderr, "find_lhs_color(%p, %d)\n",
10786 pre = find_lhs_pre_color(state, ins, index);
10787 post = find_lhs_post_color(state, ins, index);
10788 if ((pre.reg != post.reg) &&
10789 (pre.reg != REG_UNSET) &&
10790 (post.reg != REG_UNSET)) {
10791 internal_error(state, ins, "register conflict");
10793 info.regcm = pre.regcm & post.regcm;
10794 info.reg = pre.reg;
10795 if (info.reg == REG_UNSET) {
10796 info.reg = post.reg;
10799 fprintf(stderr, "find_lhs_color(%p, %d) -> ( %d, %x)\n",
10800 ins, index, info.reg, info.regcm);
10805 static struct triple *post_copy(struct compile_state *state, struct triple *ins)
10807 struct triple_set *entry, *next;
10808 struct triple *out;
10809 struct reg_info info, rinfo;
10811 info = arch_reg_lhs(state, ins, 0);
10812 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
10813 use_triple(RHS(out, 0), out);
10814 /* Get the users of ins to use out instead */
10815 for(entry = ins->use; entry; entry = next) {
10817 next = entry->next;
10818 if (entry->member == out) {
10821 i = find_rhs_use(state, entry->member, ins);
10825 rinfo = arch_reg_rhs(state, entry->member, i);
10826 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
10829 replace_rhs_use(state, ins, out, entry->member);
10831 transform_to_arch_instruction(state, out);
10835 static struct triple *pre_copy(
10836 struct compile_state *state, struct triple *ins, int index)
10838 /* Carefully insert enough operations so that I can
10839 * enter any operation with a GPR32.
10842 struct triple **expr;
10843 if (ins->op == OP_PHI) {
10844 internal_error(state, ins, "pre_copy on a phi?");
10846 expr = &RHS(ins, index);
10847 in = pre_triple(state, ins, OP_COPY, (*expr)->type, *expr, 0);
10848 unuse_triple(*expr, ins);
10850 use_triple(RHS(in, 0), in);
10851 use_triple(in, ins);
10852 transform_to_arch_instruction(state, in);
10857 static void insert_copies_to_phi(struct compile_state *state)
10859 /* To get out of ssa form we insert moves on the incoming
10860 * edges to blocks containting phi functions.
10862 struct triple *first;
10863 struct triple *phi;
10865 /* Walk all of the operations to find the phi functions */
10866 first = RHS(state->main_function, 0);
10867 for(phi = first->next; phi != first ; phi = phi->next) {
10868 struct block_set *set;
10869 struct block *block;
10870 struct triple **slot;
10872 if (phi->op != OP_PHI) {
10875 phi->id |= TRIPLE_FLAG_POST_SPLIT;
10876 block = phi->u.block;
10877 slot = &RHS(phi, 0);
10878 /* Walk all of the incoming edges/blocks and insert moves.
10880 for(edge = 0, set = block->use; set; set = set->next, edge++) {
10881 struct block *eblock;
10882 struct triple *move;
10883 struct triple *val;
10884 struct triple *ptr;
10885 eblock = set->member;
10892 get_occurance(val->occurance);
10893 move = build_triple(state, OP_COPY, phi->type, val, 0,
10895 move->u.block = eblock;
10896 move->id |= TRIPLE_FLAG_PRE_SPLIT;
10897 use_triple(val, move);
10900 unuse_triple(val, phi);
10901 use_triple(move, phi);
10903 /* Walk through the block backwards to find
10904 * an appropriate location for the OP_COPY.
10906 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
10907 struct triple **expr;
10908 if ((ptr == phi) || (ptr == val)) {
10911 expr = triple_rhs(state, ptr, 0);
10912 for(;expr; expr = triple_rhs(state, ptr, expr)) {
10913 if ((*expr) == phi) {
10919 if (triple_is_branch(state, ptr)) {
10920 internal_error(state, ptr,
10921 "Could not insert write to phi");
10923 insert_triple(state, ptr->next, move);
10924 if (eblock->last == ptr) {
10925 eblock->last = move;
10927 transform_to_arch_instruction(state, move);
10932 struct triple_reg_set {
10933 struct triple_reg_set *next;
10934 struct triple *member;
10935 struct triple *new;
10939 struct block *block;
10940 struct triple_reg_set *in;
10941 struct triple_reg_set *out;
10945 static int do_triple_set(struct triple_reg_set **head,
10946 struct triple *member, struct triple *new_member)
10948 struct triple_reg_set **ptr, *new;
10953 if ((*ptr)->member == member) {
10956 ptr = &(*ptr)->next;
10958 new = xcmalloc(sizeof(*new), "triple_set");
10959 new->member = member;
10960 new->new = new_member;
10966 static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
10968 struct triple_reg_set *entry, **ptr;
10972 if (entry->member == member) {
10973 *ptr = entry->next;
10978 ptr = &entry->next;
10983 static int in_triple(struct reg_block *rb, struct triple *in)
10985 return do_triple_set(&rb->in, in, 0);
10987 static void unin_triple(struct reg_block *rb, struct triple *unin)
10989 do_triple_unset(&rb->in, unin);
10992 static int out_triple(struct reg_block *rb, struct triple *out)
10994 return do_triple_set(&rb->out, out, 0);
10996 static void unout_triple(struct reg_block *rb, struct triple *unout)
10998 do_triple_unset(&rb->out, unout);
11001 static int initialize_regblock(struct reg_block *blocks,
11002 struct block *block, int vertex)
11004 struct block_set *user;
11005 if (!block || (blocks[block->vertex].block == block)) {
11009 /* Renumber the blocks in a convinient fashion */
11010 block->vertex = vertex;
11011 blocks[vertex].block = block;
11012 blocks[vertex].vertex = vertex;
11013 for(user = block->use; user; user = user->next) {
11014 vertex = initialize_regblock(blocks, user->member, vertex);
11019 static int phi_in(struct compile_state *state, struct reg_block *blocks,
11020 struct reg_block *rb, struct block *suc)
11022 /* Read the conditional input set of a successor block
11023 * (i.e. the input to the phi nodes) and place it in the
11024 * current blocks output set.
11026 struct block_set *set;
11027 struct triple *ptr;
11031 /* Find the edge I am coming in on */
11032 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
11033 if (set->member == rb->block) {
11038 internal_error(state, 0, "Not coming on a control edge?");
11040 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
11041 struct triple **slot, *expr, *ptr2;
11042 int out_change, done2;
11043 done = (ptr == suc->last);
11044 if (ptr->op != OP_PHI) {
11047 slot = &RHS(ptr, 0);
11049 out_change = out_triple(rb, expr);
11053 /* If we don't define the variable also plast it
11054 * in the current blocks input set.
11056 ptr2 = rb->block->first;
11057 for(done2 = 0; !done2; ptr2 = ptr2->next) {
11058 if (ptr2 == expr) {
11061 done2 = (ptr2 == rb->block->last);
11066 change |= in_triple(rb, expr);
11071 static int reg_in(struct compile_state *state, struct reg_block *blocks,
11072 struct reg_block *rb, struct block *suc)
11074 struct triple_reg_set *in_set;
11077 /* Read the input set of a successor block
11078 * and place it in the current blocks output set.
11080 in_set = blocks[suc->vertex].in;
11081 for(; in_set; in_set = in_set->next) {
11082 int out_change, done;
11083 struct triple *first, *last, *ptr;
11084 out_change = out_triple(rb, in_set->member);
11088 /* If we don't define the variable also place it
11089 * in the current blocks input set.
11091 first = rb->block->first;
11092 last = rb->block->last;
11094 for(ptr = first; !done; ptr = ptr->next) {
11095 if (ptr == in_set->member) {
11098 done = (ptr == last);
11103 change |= in_triple(rb, in_set->member);
11105 change |= phi_in(state, blocks, rb, suc);
11110 static int use_in(struct compile_state *state, struct reg_block *rb)
11112 /* Find the variables we use but don't define and add
11113 * it to the current blocks input set.
11115 #warning "FIXME is this O(N^2) algorithm bad?"
11116 struct block *block;
11117 struct triple *ptr;
11122 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
11123 struct triple **expr;
11124 done = (ptr == block->first);
11125 /* The variable a phi function uses depends on the
11126 * control flow, and is handled in phi_in, not
11129 if (ptr->op == OP_PHI) {
11132 expr = triple_rhs(state, ptr, 0);
11133 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11134 struct triple *rhs, *test;
11140 /* See if rhs is defined in this block */
11141 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
11142 tdone = (test == block->first);
11148 /* If I still have a valid rhs add it to in */
11149 change |= in_triple(rb, rhs);
11155 static struct reg_block *compute_variable_lifetimes(
11156 struct compile_state *state)
11158 struct reg_block *blocks;
11161 sizeof(*blocks)*(state->last_vertex + 1), "reg_block");
11162 initialize_regblock(blocks, state->last_block, 0);
11166 for(i = 1; i <= state->last_vertex; i++) {
11167 struct reg_block *rb;
11169 /* Add the left successor's input set to in */
11170 if (rb->block->left) {
11171 change |= reg_in(state, blocks, rb, rb->block->left);
11173 /* Add the right successor's input set to in */
11174 if ((rb->block->right) &&
11175 (rb->block->right != rb->block->left)) {
11176 change |= reg_in(state, blocks, rb, rb->block->right);
11178 /* Add use to in... */
11179 change |= use_in(state, rb);
11185 static void free_variable_lifetimes(
11186 struct compile_state *state, struct reg_block *blocks)
11189 /* free in_set && out_set on each block */
11190 for(i = 1; i <= state->last_vertex; i++) {
11191 struct triple_reg_set *entry, *next;
11192 struct reg_block *rb;
11194 for(entry = rb->in; entry ; entry = next) {
11195 next = entry->next;
11196 do_triple_unset(&rb->in, entry->member);
11198 for(entry = rb->out; entry; entry = next) {
11199 next = entry->next;
11200 do_triple_unset(&rb->out, entry->member);
11207 typedef void (*wvl_cb_t)(
11208 struct compile_state *state,
11209 struct reg_block *blocks, struct triple_reg_set *live,
11210 struct reg_block *rb, struct triple *ins, void *arg);
11212 static void walk_variable_lifetimes(struct compile_state *state,
11213 struct reg_block *blocks, wvl_cb_t cb, void *arg)
11217 for(i = 1; i <= state->last_vertex; i++) {
11218 struct triple_reg_set *live;
11219 struct triple_reg_set *entry, *next;
11220 struct triple *ptr, *prev;
11221 struct reg_block *rb;
11222 struct block *block;
11225 /* Get the blocks */
11229 /* Copy out into live */
11231 for(entry = rb->out; entry; entry = next) {
11232 next = entry->next;
11233 do_triple_set(&live, entry->member, entry->new);
11235 /* Walk through the basic block calculating live */
11236 for(done = 0, ptr = block->last; !done; ptr = prev) {
11237 struct triple **expr;
11240 done = (ptr == block->first);
11242 /* Ensure the current definition is in live */
11243 if (triple_is_def(state, ptr)) {
11244 do_triple_set(&live, ptr, 0);
11247 /* Inform the callback function of what is
11250 cb(state, blocks, live, rb, ptr, arg);
11252 /* Remove the current definition from live */
11253 do_triple_unset(&live, ptr);
11255 /* Add the current uses to live.
11257 * It is safe to skip phi functions because they do
11258 * not have any block local uses, and the block
11259 * output sets already properly account for what
11260 * control flow depedent uses phi functions do have.
11262 if (ptr->op == OP_PHI) {
11265 expr = triple_rhs(state, ptr, 0);
11266 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11267 /* If the triple is not a definition skip it. */
11268 if (!*expr || !triple_is_def(state, *expr)) {
11271 do_triple_set(&live, *expr, 0);
11275 for(entry = live; entry; entry = next) {
11276 next = entry->next;
11277 do_triple_unset(&live, entry->member);
11282 static int count_triples(struct compile_state *state)
11284 struct triple *first, *ins;
11286 first = RHS(state->main_function, 0);
11291 } while (ins != first);
11294 struct dead_triple {
11295 struct triple *triple;
11296 struct dead_triple *work_next;
11297 struct block *block;
11300 #define TRIPLE_FLAG_ALIVE 1
11304 static void awaken(
11305 struct compile_state *state,
11306 struct dead_triple *dtriple, struct triple **expr,
11307 struct dead_triple ***work_list_tail)
11309 struct triple *triple;
11310 struct dead_triple *dt;
11318 if (triple->id <= 0) {
11319 internal_error(state, triple, "bad triple id: %d",
11322 if (triple->op == OP_NOOP) {
11323 internal_warning(state, triple, "awakening noop?");
11326 dt = &dtriple[triple->id];
11327 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11328 dt->flags |= TRIPLE_FLAG_ALIVE;
11329 if (!dt->work_next) {
11330 **work_list_tail = dt;
11331 *work_list_tail = &dt->work_next;
11336 static void eliminate_inefectual_code(struct compile_state *state)
11338 struct block *block;
11339 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
11341 struct triple *first, *ins;
11343 /* Setup the work list */
11345 work_list_tail = &work_list;
11347 first = RHS(state->main_function, 0);
11349 /* Count how many triples I have */
11350 triples = count_triples(state);
11352 /* Now put then in an array and mark all of the triples dead */
11353 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
11359 if (ins->op == OP_LABEL) {
11360 block = ins->u.block;
11362 dtriple[i].triple = ins;
11363 dtriple[i].block = block;
11364 dtriple[i].flags = 0;
11365 dtriple[i].color = ins->id;
11367 /* See if it is an operation we always keep */
11368 #warning "FIXME handle the case of killing a branch instruction"
11369 if (!triple_is_pure(state, ins) || triple_is_branch(state, ins)) {
11370 awaken(state, dtriple, &ins, &work_list_tail);
11374 } while(ins != first);
11376 struct dead_triple *dt;
11377 struct block_set *user;
11378 struct triple **expr;
11380 work_list = dt->work_next;
11382 work_list_tail = &work_list;
11384 /* Wake up the data depencencies of this triple */
11387 expr = triple_rhs(state, dt->triple, expr);
11388 awaken(state, dtriple, expr, &work_list_tail);
11391 expr = triple_lhs(state, dt->triple, expr);
11392 awaken(state, dtriple, expr, &work_list_tail);
11395 expr = triple_misc(state, dt->triple, expr);
11396 awaken(state, dtriple, expr, &work_list_tail);
11398 /* Wake up the forward control dependencies */
11400 expr = triple_targ(state, dt->triple, expr);
11401 awaken(state, dtriple, expr, &work_list_tail);
11403 /* Wake up the reverse control dependencies of this triple */
11404 for(user = dt->block->ipdomfrontier; user; user = user->next) {
11405 awaken(state, dtriple, &user->member->last, &work_list_tail);
11408 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
11409 if ((dt->triple->op == OP_NOOP) &&
11410 (dt->flags & TRIPLE_FLAG_ALIVE)) {
11411 internal_error(state, dt->triple, "noop effective?");
11413 dt->triple->id = dt->color; /* Restore the color */
11414 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11415 #warning "FIXME handle the case of killing a basic block"
11416 if (dt->block->first == dt->triple) {
11419 if (dt->block->last == dt->triple) {
11420 dt->block->last = dt->triple->prev;
11422 release_triple(state, dt->triple);
11429 static void insert_mandatory_copies(struct compile_state *state)
11431 struct triple *ins, *first;
11433 /* The object is with a minimum of inserted copies,
11434 * to resolve in fundamental register conflicts between
11435 * register value producers and consumers.
11436 * Theoretically we may be greater than minimal when we
11437 * are inserting copies before instructions but that
11438 * case should be rare.
11440 first = RHS(state->main_function, 0);
11443 struct triple_set *entry, *next;
11444 struct triple *tmp;
11445 struct reg_info info;
11446 unsigned reg, regcm;
11447 int do_post_copy, do_pre_copy;
11449 if (!triple_is_def(state, ins)) {
11452 /* Find the architecture specific color information */
11453 info = arch_reg_lhs(state, ins, 0);
11454 if (info.reg >= MAX_REGISTERS) {
11455 info.reg = REG_UNSET;
11459 regcm = arch_type_to_regcm(state, ins->type);
11460 do_post_copy = do_pre_copy = 0;
11462 /* Walk through the uses of ins and check for conflicts */
11463 for(entry = ins->use; entry; entry = next) {
11464 struct reg_info rinfo;
11466 next = entry->next;
11467 i = find_rhs_use(state, entry->member, ins);
11472 /* Find the users color requirements */
11473 rinfo = arch_reg_rhs(state, entry->member, i);
11474 if (rinfo.reg >= MAX_REGISTERS) {
11475 rinfo.reg = REG_UNSET;
11478 /* See if I need a pre_copy */
11479 if (rinfo.reg != REG_UNSET) {
11480 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
11485 regcm &= rinfo.regcm;
11486 regcm = arch_regcm_normalize(state, regcm);
11493 (((info.reg != REG_UNSET) &&
11494 (reg != REG_UNSET) &&
11495 (info.reg != reg)) ||
11496 ((info.regcm & regcm) == 0));
11499 regcm = info.regcm;
11500 /* Walk through the uses of insert and do a pre_copy or see if a post_copy is warranted */
11501 for(entry = ins->use; entry; entry = next) {
11502 struct reg_info rinfo;
11504 next = entry->next;
11505 i = find_rhs_use(state, entry->member, ins);
11510 /* Find the users color requirements */
11511 rinfo = arch_reg_rhs(state, entry->member, i);
11512 if (rinfo.reg >= MAX_REGISTERS) {
11513 rinfo.reg = REG_UNSET;
11516 /* Now see if it is time to do the pre_copy */
11517 if (rinfo.reg != REG_UNSET) {
11518 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
11519 ((regcm & rinfo.regcm) == 0) ||
11520 /* Don't let a mandatory coalesce sneak
11521 * into a operation that is marked to prevent
11524 ((reg != REG_UNNEEDED) &&
11525 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
11526 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
11529 struct triple *user;
11530 user = entry->member;
11531 if (RHS(user, i) != ins) {
11532 internal_error(state, user, "bad rhs");
11534 tmp = pre_copy(state, user, i);
11535 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
11543 if ((regcm & rinfo.regcm) == 0) {
11545 struct triple *user;
11546 user = entry->member;
11547 if (RHS(user, i) != ins) {
11548 internal_error(state, user, "bad rhs");
11550 tmp = pre_copy(state, user, i);
11551 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
11557 regcm &= rinfo.regcm;
11560 if (do_post_copy) {
11561 struct reg_info pre, post;
11562 tmp = post_copy(state, ins);
11563 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
11564 pre = arch_reg_lhs(state, ins, 0);
11565 post = arch_reg_lhs(state, tmp, 0);
11566 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
11567 internal_error(state, tmp, "useless copy");
11572 } while(ins != first);
11576 struct live_range_edge;
11577 struct live_range_def;
11578 struct live_range {
11579 struct live_range_edge *edges;
11580 struct live_range_def *defs;
11581 /* Note. The list pointed to by defs is kept in order.
11582 * That is baring splits in the flow control
11583 * defs dominates defs->next wich dominates defs->next->next
11590 struct live_range *group_next, **group_prev;
11593 struct live_range_edge {
11594 struct live_range_edge *next;
11595 struct live_range *node;
11598 struct live_range_def {
11599 struct live_range_def *next;
11600 struct live_range_def *prev;
11601 struct live_range *lr;
11602 struct triple *def;
11606 #define LRE_HASH_SIZE 2048
11608 struct lre_hash *next;
11609 struct live_range *left;
11610 struct live_range *right;
11615 struct lre_hash *hash[LRE_HASH_SIZE];
11616 struct reg_block *blocks;
11617 struct live_range_def *lrd;
11618 struct live_range *lr;
11619 struct live_range *low, **low_tail;
11620 struct live_range *high, **high_tail;
11623 int passes, max_passes;
11624 #define MAX_ALLOCATION_PASSES 100
11628 static unsigned regc_max_size(struct compile_state *state, int classes)
11633 for(i = 0; i < MAX_REGC; i++) {
11634 if (classes & (1 << i)) {
11636 size = arch_regc_size(state, i);
11637 if (size > max_size) {
11645 static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
11647 unsigned equivs[MAX_REG_EQUIVS];
11649 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
11650 internal_error(state, 0, "invalid register");
11652 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
11653 internal_error(state, 0, "invalid register");
11655 arch_reg_equivs(state, equivs, reg1);
11656 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11657 if (equivs[i] == reg2) {
11664 static void reg_fill_used(struct compile_state *state, char *used, int reg)
11666 unsigned equivs[MAX_REG_EQUIVS];
11668 if (reg == REG_UNNEEDED) {
11671 arch_reg_equivs(state, equivs, reg);
11672 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11673 used[equivs[i]] = 1;
11678 static void reg_inc_used(struct compile_state *state, char *used, int reg)
11680 unsigned equivs[MAX_REG_EQUIVS];
11682 if (reg == REG_UNNEEDED) {
11685 arch_reg_equivs(state, equivs, reg);
11686 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11687 used[equivs[i]] += 1;
11692 static unsigned int hash_live_edge(
11693 struct live_range *left, struct live_range *right)
11695 unsigned int hash, val;
11696 unsigned long lval, rval;
11697 lval = ((unsigned long)left)/sizeof(struct live_range);
11698 rval = ((unsigned long)right)/sizeof(struct live_range);
11703 hash = (hash *263) + val;
11708 hash = (hash *263) + val;
11710 hash = hash & (LRE_HASH_SIZE - 1);
11714 static struct lre_hash **lre_probe(struct reg_state *rstate,
11715 struct live_range *left, struct live_range *right)
11717 struct lre_hash **ptr;
11718 unsigned int index;
11719 /* Ensure left <= right */
11720 if (left > right) {
11721 struct live_range *tmp;
11726 index = hash_live_edge(left, right);
11728 ptr = &rstate->hash[index];
11730 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
11733 ptr = &(*ptr)->next;
11738 static int interfere(struct reg_state *rstate,
11739 struct live_range *left, struct live_range *right)
11741 struct lre_hash **ptr;
11742 ptr = lre_probe(rstate, left, right);
11743 return ptr && *ptr;
11746 static void add_live_edge(struct reg_state *rstate,
11747 struct live_range *left, struct live_range *right)
11749 /* FIXME the memory allocation overhead is noticeable here... */
11750 struct lre_hash **ptr, *new_hash;
11751 struct live_range_edge *edge;
11753 if (left == right) {
11756 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
11759 /* Ensure left <= right */
11760 if (left > right) {
11761 struct live_range *tmp;
11766 ptr = lre_probe(rstate, left, right);
11771 fprintf(stderr, "new_live_edge(%p, %p)\n",
11774 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
11775 new_hash->next = *ptr;
11776 new_hash->left = left;
11777 new_hash->right = right;
11780 edge = xmalloc(sizeof(*edge), "live_range_edge");
11781 edge->next = left->edges;
11782 edge->node = right;
11783 left->edges = edge;
11786 edge = xmalloc(sizeof(*edge), "live_range_edge");
11787 edge->next = right->edges;
11789 right->edges = edge;
11790 right->degree += 1;
11793 static void remove_live_edge(struct reg_state *rstate,
11794 struct live_range *left, struct live_range *right)
11796 struct live_range_edge *edge, **ptr;
11797 struct lre_hash **hptr, *entry;
11798 hptr = lre_probe(rstate, left, right);
11799 if (!hptr || !*hptr) {
11803 *hptr = entry->next;
11806 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
11808 if (edge->node == right) {
11810 memset(edge, 0, sizeof(*edge));
11816 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
11818 if (edge->node == left) {
11820 memset(edge, 0, sizeof(*edge));
11828 static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
11830 struct live_range_edge *edge, *next;
11831 for(edge = range->edges; edge; edge = next) {
11833 remove_live_edge(rstate, range, edge->node);
11837 static void transfer_live_edges(struct reg_state *rstate,
11838 struct live_range *dest, struct live_range *src)
11840 struct live_range_edge *edge, *next;
11841 for(edge = src->edges; edge; edge = next) {
11842 struct live_range *other;
11844 other = edge->node;
11845 remove_live_edge(rstate, src, other);
11846 add_live_edge(rstate, dest, other);
11851 /* Interference graph...
11853 * new(n) --- Return a graph with n nodes but no edges.
11854 * add(g,x,y) --- Return a graph including g with an between x and y
11855 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
11856 * x and y in the graph g
11857 * degree(g, x) --- Return the degree of the node x in the graph g
11858 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
11860 * Implement with a hash table && a set of adjcency vectors.
11861 * The hash table supports constant time implementations of add and interfere.
11862 * The adjacency vectors support an efficient implementation of neighbors.
11866 * +---------------------------------------------------+
11867 * | +--------------+ |
11869 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
11871 * -- In simplify implment optimistic coloring... (No backtracking)
11872 * -- Implement Rematerialization it is the only form of spilling we can perform
11873 * Essentially this means dropping a constant from a register because
11874 * we can regenerate it later.
11876 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
11877 * coalesce at phi points...
11878 * --- Bias coloring if at all possible do the coalesing a compile time.
11883 static void different_colored(
11884 struct compile_state *state, struct reg_state *rstate,
11885 struct triple *parent, struct triple *ins)
11887 struct live_range *lr;
11888 struct triple **expr;
11889 lr = rstate->lrd[ins->id].lr;
11890 expr = triple_rhs(state, ins, 0);
11891 for(;expr; expr = triple_rhs(state, ins, expr)) {
11892 struct live_range *lr2;
11893 if (!*expr || (*expr == parent) || (*expr == ins)) {
11896 lr2 = rstate->lrd[(*expr)->id].lr;
11897 if (lr->color == lr2->color) {
11898 internal_error(state, ins, "live range too big");
11904 static struct live_range *coalesce_ranges(
11905 struct compile_state *state, struct reg_state *rstate,
11906 struct live_range *lr1, struct live_range *lr2)
11908 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
11914 if (!lr1->defs || !lr2->defs) {
11915 internal_error(state, 0,
11916 "cannot coalese dead live ranges");
11918 if ((lr1->color == REG_UNNEEDED) ||
11919 (lr2->color == REG_UNNEEDED)) {
11920 internal_error(state, 0,
11921 "cannot coalesce live ranges without a possible color");
11923 if ((lr1->color != lr2->color) &&
11924 (lr1->color != REG_UNSET) &&
11925 (lr2->color != REG_UNSET)) {
11926 internal_error(state, lr1->defs->def,
11927 "cannot coalesce live ranges of different colors");
11929 color = lr1->color;
11930 if (color == REG_UNSET) {
11931 color = lr2->color;
11933 classes = lr1->classes & lr2->classes;
11935 internal_error(state, lr1->defs->def,
11936 "cannot coalesce live ranges with dissimilar register classes");
11938 /* If there is a clear dominate live range put it in lr1,
11939 * For purposes of this test phi functions are
11940 * considered dominated by the definitions that feed into
11943 if ((lr1->defs->prev->def->op == OP_PHI) ||
11944 ((lr2->defs->prev->def->op != OP_PHI) &&
11945 tdominates(state, lr2->defs->def, lr1->defs->def))) {
11946 struct live_range *tmp;
11952 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
11953 fprintf(stderr, "lr1 post\n");
11955 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
11956 fprintf(stderr, "lr1 pre\n");
11958 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
11959 fprintf(stderr, "lr2 post\n");
11961 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
11962 fprintf(stderr, "lr2 pre\n");
11966 fprintf(stderr, "coalesce color1(%p): %3d color2(%p) %3d\n",
11973 lr1->classes = classes;
11974 /* Append lr2 onto lr1 */
11975 #warning "FIXME should this be a merge instead of a splice?"
11976 /* This FIXME item applies to the correctness of live_range_end
11977 * and to the necessity of making multiple passes of coalesce_live_ranges.
11978 * A failure to find some coalesce opportunities in coaleace_live_ranges
11979 * does not impact the correct of the compiler just the efficiency with
11980 * which registers are allocated.
11983 mid1 = lr1->defs->prev;
11985 end = lr2->defs->prev;
11993 /* Fixup the live range in the added live range defs */
11998 } while(lrd != head);
12000 /* Mark lr2 as free. */
12002 lr2->color = REG_UNNEEDED;
12006 internal_error(state, 0, "lr1->defs == 0 ?");
12009 lr1->color = color;
12010 lr1->classes = classes;
12012 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
12013 transfer_live_edges(rstate, lr1, lr2);
12018 static struct live_range_def *live_range_head(
12019 struct compile_state *state, struct live_range *lr,
12020 struct live_range_def *last)
12022 struct live_range_def *result;
12027 else if (!tdominates(state, lr->defs->def, last->next->def)) {
12028 result = last->next;
12033 static struct live_range_def *live_range_end(
12034 struct compile_state *state, struct live_range *lr,
12035 struct live_range_def *last)
12037 struct live_range_def *result;
12040 result = lr->defs->prev;
12042 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
12043 result = last->prev;
12049 static void initialize_live_ranges(
12050 struct compile_state *state, struct reg_state *rstate)
12052 struct triple *ins, *first;
12053 size_t count, size;
12056 first = RHS(state->main_function, 0);
12057 /* First count how many instructions I have.
12059 count = count_triples(state);
12060 /* Potentially I need one live range definitions for each
12061 * instruction, plus an extra for the split routines.
12063 rstate->defs = count + 1;
12064 /* Potentially I need one live range for each instruction
12065 * plus an extra for the dummy live range.
12067 rstate->ranges = count + 1;
12068 size = sizeof(rstate->lrd[0]) * rstate->defs;
12069 rstate->lrd = xcmalloc(size, "live_range_def");
12070 size = sizeof(rstate->lr[0]) * rstate->ranges;
12071 rstate->lr = xcmalloc(size, "live_range");
12073 /* Setup the dummy live range */
12074 rstate->lr[0].classes = 0;
12075 rstate->lr[0].color = REG_UNSET;
12076 rstate->lr[0].defs = 0;
12080 /* If the triple is a variable give it a live range */
12081 if (triple_is_def(state, ins)) {
12082 struct reg_info info;
12083 /* Find the architecture specific color information */
12084 info = find_def_color(state, ins);
12087 rstate->lr[i].defs = &rstate->lrd[j];
12088 rstate->lr[i].color = info.reg;
12089 rstate->lr[i].classes = info.regcm;
12090 rstate->lr[i].degree = 0;
12091 rstate->lrd[j].lr = &rstate->lr[i];
12093 /* Otherwise give the triple the dummy live range. */
12095 rstate->lrd[j].lr = &rstate->lr[0];
12098 /* Initalize the live_range_def */
12099 rstate->lrd[j].next = &rstate->lrd[j];
12100 rstate->lrd[j].prev = &rstate->lrd[j];
12101 rstate->lrd[j].def = ins;
12102 rstate->lrd[j].orig_id = ins->id;
12107 } while(ins != first);
12108 rstate->ranges = i;
12111 /* Make a second pass to handle achitecture specific register
12116 int zlhs, zrhs, i, j;
12117 if (ins->id > rstate->defs) {
12118 internal_error(state, ins, "bad id");
12121 /* Walk through the template of ins and coalesce live ranges */
12122 zlhs = TRIPLE_LHS(ins->sizes);
12123 if ((zlhs == 0) && triple_is_def(state, ins)) {
12126 zrhs = TRIPLE_RHS(ins->sizes);
12128 for(i = 0; i < zlhs; i++) {
12129 struct reg_info linfo;
12130 struct live_range_def *lhs;
12131 linfo = arch_reg_lhs(state, ins, i);
12132 if (linfo.reg < MAX_REGISTERS) {
12135 if (triple_is_def(state, ins)) {
12136 lhs = &rstate->lrd[ins->id];
12138 lhs = &rstate->lrd[LHS(ins, i)->id];
12140 for(j = 0; j < zrhs; j++) {
12141 struct reg_info rinfo;
12142 struct live_range_def *rhs;
12143 rinfo = arch_reg_rhs(state, ins, j);
12144 if (rinfo.reg < MAX_REGISTERS) {
12147 rhs = &rstate->lrd[RHS(ins, i)->id];
12148 if (rinfo.reg == linfo.reg) {
12149 coalesce_ranges(state, rstate,
12155 } while(ins != first);
12158 static void graph_ins(
12159 struct compile_state *state,
12160 struct reg_block *blocks, struct triple_reg_set *live,
12161 struct reg_block *rb, struct triple *ins, void *arg)
12163 struct reg_state *rstate = arg;
12164 struct live_range *def;
12165 struct triple_reg_set *entry;
12167 /* If the triple is not a definition
12168 * we do not have a definition to add to
12169 * the interference graph.
12171 if (!triple_is_def(state, ins)) {
12174 def = rstate->lrd[ins->id].lr;
12176 /* Create an edge between ins and everything that is
12177 * alive, unless the live_range cannot share
12178 * a physical register with ins.
12180 for(entry = live; entry; entry = entry->next) {
12181 struct live_range *lr;
12182 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
12183 internal_error(state, 0, "bad entry?");
12185 lr = rstate->lrd[entry->member->id].lr;
12189 if (!arch_regcm_intersect(def->classes, lr->classes)) {
12192 add_live_edge(rstate, def, lr);
12197 static struct live_range *get_verify_live_range(
12198 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
12200 struct live_range *lr;
12201 struct live_range_def *lrd;
12203 if ((ins->id < 0) || (ins->id > rstate->defs)) {
12204 internal_error(state, ins, "bad ins?");
12206 lr = rstate->lrd[ins->id].lr;
12210 if (lrd->def == ins) {
12214 } while(lrd != lr->defs);
12216 internal_error(state, ins, "ins not in live range");
12221 static void verify_graph_ins(
12222 struct compile_state *state,
12223 struct reg_block *blocks, struct triple_reg_set *live,
12224 struct reg_block *rb, struct triple *ins, void *arg)
12226 struct reg_state *rstate = arg;
12227 struct triple_reg_set *entry1, *entry2;
12230 /* Compare live against edges and make certain the code is working */
12231 for(entry1 = live; entry1; entry1 = entry1->next) {
12232 struct live_range *lr1;
12233 lr1 = get_verify_live_range(state, rstate, entry1->member);
12234 for(entry2 = live; entry2; entry2 = entry2->next) {
12235 struct live_range *lr2;
12236 struct live_range_edge *edge2;
12239 if (entry2 == entry1) {
12242 lr2 = get_verify_live_range(state, rstate, entry2->member);
12244 internal_error(state, entry2->member,
12245 "live range with 2 values simultaneously alive");
12247 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
12250 if (!interfere(rstate, lr1, lr2)) {
12251 internal_error(state, entry2->member,
12252 "edges don't interfere?");
12257 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
12259 if (edge2->node == lr1) {
12263 if (lr2_degree != lr2->degree) {
12264 internal_error(state, entry2->member,
12265 "computed degree: %d does not match reported degree: %d\n",
12266 lr2_degree, lr2->degree);
12269 internal_error(state, entry2->member, "missing edge");
12277 static void print_interference_ins(
12278 struct compile_state *state,
12279 struct reg_block *blocks, struct triple_reg_set *live,
12280 struct reg_block *rb, struct triple *ins, void *arg)
12282 struct reg_state *rstate = arg;
12283 struct live_range *lr;
12286 lr = rstate->lrd[ins->id].lr;
12288 ins->id = rstate->lrd[id].orig_id;
12289 SET_REG(ins->id, lr->color);
12290 display_triple(stdout, ins);
12294 struct live_range_def *lrd;
12298 printf(" %-10p", lrd->def);
12300 } while(lrd != lr->defs);
12304 struct triple_reg_set *entry;
12306 for(entry = live; entry; entry = entry->next) {
12307 printf(" %-10p", entry->member);
12312 struct live_range_edge *entry;
12314 for(entry = lr->edges; entry; entry = entry->next) {
12315 struct live_range_def *lrd;
12316 lrd = entry->node->defs;
12318 printf(" %-10p", lrd->def);
12320 } while(lrd != entry->node->defs);
12325 if (triple_is_branch(state, ins)) {
12331 static int coalesce_live_ranges(
12332 struct compile_state *state, struct reg_state *rstate)
12334 /* At the point where a value is moved from one
12335 * register to another that value requires two
12336 * registers, thus increasing register pressure.
12337 * Live range coaleescing reduces the register
12338 * pressure by keeping a value in one register
12341 * In the case of a phi function all paths leading
12342 * into it must be allocated to the same register
12343 * otherwise the phi function may not be removed.
12345 * Forcing a value to stay in a single register
12346 * for an extended period of time does have
12347 * limitations when applied to non homogenous
12350 * The two cases I have identified are:
12351 * 1) Two forced register assignments may
12353 * 2) Registers may go unused because they
12354 * are only good for storing the value
12355 * and not manipulating it.
12357 * Because of this I need to split live ranges,
12358 * even outside of the context of coalesced live
12359 * ranges. The need to split live ranges does
12360 * impose some constraints on live range coalescing.
12362 * - Live ranges may not be coalesced across phi
12363 * functions. This creates a 2 headed live
12364 * range that cannot be sanely split.
12366 * - phi functions (coalesced in initialize_live_ranges)
12367 * are handled as pre split live ranges so we will
12368 * never attempt to split them.
12374 for(i = 0; i <= rstate->ranges; i++) {
12375 struct live_range *lr1;
12376 struct live_range_def *lrd1;
12377 lr1 = &rstate->lr[i];
12381 lrd1 = live_range_end(state, lr1, 0);
12382 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
12383 struct triple_set *set;
12384 if (lrd1->def->op != OP_COPY) {
12387 /* Skip copies that are the result of a live range split. */
12388 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
12391 for(set = lrd1->def->use; set; set = set->next) {
12392 struct live_range_def *lrd2;
12393 struct live_range *lr2, *res;
12395 lrd2 = &rstate->lrd[set->member->id];
12397 /* Don't coalesce with instructions
12398 * that are the result of a live range
12401 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
12404 lr2 = rstate->lrd[set->member->id].lr;
12408 if ((lr1->color != lr2->color) &&
12409 (lr1->color != REG_UNSET) &&
12410 (lr2->color != REG_UNSET)) {
12413 if ((lr1->classes & lr2->classes) == 0) {
12417 if (interfere(rstate, lr1, lr2)) {
12421 res = coalesce_ranges(state, rstate, lr1, lr2);
12435 static void fix_coalesce_conflicts(struct compile_state *state,
12436 struct reg_block *blocks, struct triple_reg_set *live,
12437 struct reg_block *rb, struct triple *ins, void *arg)
12439 int zlhs, zrhs, i, j;
12441 /* See if we have a mandatory coalesce operation between
12442 * a lhs and a rhs value. If so and the rhs value is also
12443 * alive then this triple needs to be pre copied. Otherwise
12444 * we would have two definitions in the same live range simultaneously
12447 zlhs = TRIPLE_LHS(ins->sizes);
12448 if ((zlhs == 0) && triple_is_def(state, ins)) {
12451 zrhs = TRIPLE_RHS(ins->sizes);
12452 for(i = 0; i < zlhs; i++) {
12453 struct reg_info linfo;
12454 linfo = arch_reg_lhs(state, ins, i);
12455 if (linfo.reg < MAX_REGISTERS) {
12458 for(j = 0; j < zrhs; j++) {
12459 struct reg_info rinfo;
12460 struct triple *rhs;
12461 struct triple_reg_set *set;
12464 rinfo = arch_reg_rhs(state, ins, j);
12465 if (rinfo.reg != linfo.reg) {
12469 for(set = live; set && !found; set = set->next) {
12470 if (set->member == rhs) {
12475 struct triple *copy;
12476 copy = pre_copy(state, ins, j);
12477 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12484 static void replace_set_use(struct compile_state *state,
12485 struct triple_reg_set *head, struct triple *orig, struct triple *new)
12487 struct triple_reg_set *set;
12488 for(set = head; set; set = set->next) {
12489 if (set->member == orig) {
12495 static void replace_block_use(struct compile_state *state,
12496 struct reg_block *blocks, struct triple *orig, struct triple *new)
12499 #warning "WISHLIST visit just those blocks that need it *"
12500 for(i = 1; i <= state->last_vertex; i++) {
12501 struct reg_block *rb;
12503 replace_set_use(state, rb->in, orig, new);
12504 replace_set_use(state, rb->out, orig, new);
12508 static void color_instructions(struct compile_state *state)
12510 struct triple *ins, *first;
12511 first = RHS(state->main_function, 0);
12514 if (triple_is_def(state, ins)) {
12515 struct reg_info info;
12516 info = find_lhs_color(state, ins, 0);
12517 if (info.reg >= MAX_REGISTERS) {
12518 info.reg = REG_UNSET;
12520 SET_INFO(ins->id, info);
12523 } while(ins != first);
12526 static struct reg_info read_lhs_color(
12527 struct compile_state *state, struct triple *ins, int index)
12529 struct reg_info info;
12530 if ((index == 0) && triple_is_def(state, ins)) {
12531 info.reg = ID_REG(ins->id);
12532 info.regcm = ID_REGCM(ins->id);
12534 else if (index < TRIPLE_LHS(ins->sizes)) {
12535 info = read_lhs_color(state, LHS(ins, index), 0);
12538 internal_error(state, ins, "Bad lhs %d", index);
12539 info.reg = REG_UNSET;
12545 static struct triple *resolve_tangle(
12546 struct compile_state *state, struct triple *tangle)
12548 struct reg_info info, uinfo;
12549 struct triple_set *set, *next;
12550 struct triple *copy;
12552 #warning "WISHLIST recalculate all affected instructions colors"
12553 info = find_lhs_color(state, tangle, 0);
12554 for(set = tangle->use; set; set = next) {
12555 struct triple *user;
12558 user = set->member;
12559 zrhs = TRIPLE_RHS(user->sizes);
12560 for(i = 0; i < zrhs; i++) {
12561 if (RHS(user, i) != tangle) {
12564 uinfo = find_rhs_post_color(state, user, i);
12565 if (uinfo.reg == info.reg) {
12566 copy = pre_copy(state, user, i);
12567 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12568 SET_INFO(copy->id, uinfo);
12573 uinfo = find_lhs_pre_color(state, tangle, 0);
12574 if (uinfo.reg == info.reg) {
12575 struct reg_info linfo;
12576 copy = post_copy(state, tangle);
12577 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12578 linfo = find_lhs_color(state, copy, 0);
12579 SET_INFO(copy->id, linfo);
12581 info = find_lhs_color(state, tangle, 0);
12582 SET_INFO(tangle->id, info);
12588 static void fix_tangles(struct compile_state *state,
12589 struct reg_block *blocks, struct triple_reg_set *live,
12590 struct reg_block *rb, struct triple *ins, void *arg)
12592 int *tangles = arg;
12593 struct triple *tangle;
12595 char used[MAX_REGISTERS];
12596 struct triple_reg_set *set;
12599 /* Find out which registers have multiple uses at this point */
12600 memset(used, 0, sizeof(used));
12601 for(set = live; set; set = set->next) {
12602 struct reg_info info;
12603 info = read_lhs_color(state, set->member, 0);
12604 if (info.reg == REG_UNSET) {
12607 reg_inc_used(state, used, info.reg);
12610 /* Now find the least dominated definition of a register in
12611 * conflict I have seen so far.
12613 for(set = live; set; set = set->next) {
12614 struct reg_info info;
12615 info = read_lhs_color(state, set->member, 0);
12616 if (used[info.reg] < 2) {
12619 /* Changing copies that feed into phi functions
12622 if (set->member->use &&
12623 (set->member->use->member->op == OP_PHI)) {
12626 if (!tangle || tdominates(state, set->member, tangle)) {
12627 tangle = set->member;
12630 /* If I have found a tangle resolve it */
12632 struct triple *post_copy;
12634 post_copy = resolve_tangle(state, tangle);
12636 replace_block_use(state, blocks, tangle, post_copy);
12638 if (post_copy && (tangle != ins)) {
12639 replace_set_use(state, live, tangle, post_copy);
12646 static int correct_tangles(
12647 struct compile_state *state, struct reg_block *blocks)
12651 color_instructions(state);
12652 walk_variable_lifetimes(state, blocks, fix_tangles, &tangles);
12656 struct least_conflict {
12657 struct reg_state *rstate;
12658 struct live_range *ref_range;
12659 struct triple *ins;
12660 struct triple_reg_set *live;
12664 static void least_conflict(struct compile_state *state,
12665 struct reg_block *blocks, struct triple_reg_set *live,
12666 struct reg_block *rb, struct triple *ins, void *arg)
12668 struct least_conflict *conflict = arg;
12669 struct live_range_edge *edge;
12670 struct triple_reg_set *set;
12674 #warning "FIXME handle instructions with left hand sides..."
12675 /* Only instructions that introduce a new definition
12676 * can be the conflict instruction.
12678 if (!triple_is_def(state, ins)) {
12682 /* See if live ranges at this instruction are a
12683 * strict subset of the live ranges that are in conflict.
12686 for(set = live; set; set = set->next) {
12687 struct live_range *lr;
12688 lr = conflict->rstate->lrd[set->member->id].lr;
12689 /* Ignore it if there cannot be an edge between these two nodes */
12690 if (!arch_regcm_intersect(conflict->ref_range->classes, lr->classes)) {
12693 for(edge = conflict->ref_range->edges; edge; edge = edge->next) {
12694 if (edge->node == lr) {
12698 if (!edge && (lr != conflict->ref_range)) {
12708 /* See if there is an uncolored member in this subset.
12710 for(set = live; set; set = set->next) {
12711 struct live_range *lr;
12712 lr = conflict->rstate->lrd[set->member->id].lr;
12713 if (lr->color == REG_UNSET) {
12717 if (!set && (conflict->ref_range != REG_UNSET)) {
12722 /* See if any of the live registers are constrained,
12723 * if not it won't be productive to pick this as
12724 * a conflict instruction.
12727 for(set = live; set; set = set->next) {
12728 struct triple_set *uset;
12729 struct reg_info info;
12731 unsigned cur_size, size;
12732 /* Skip this instruction */
12733 if (set->member == ins) {
12736 /* Find how many registers this value can potentially
12739 classes = arch_type_to_regcm(state, set->member->type);
12740 size = regc_max_size(state, classes);
12742 /* Find how many registers we allow this value to
12745 info = arch_reg_lhs(state, set->member, 0);
12747 /* If the value does not live in a register it
12748 * isn't constrained.
12750 if (info.reg == REG_UNNEEDED) {
12754 if ((info.reg == REG_UNSET) || (info.reg >= MAX_REGISTERS)) {
12755 cur_size = regc_max_size(state, info.regcm);
12760 /* If there is no difference between potential and
12761 * actual register count there is not a constraint
12763 if (cur_size >= size) {
12767 /* If this live_range feeds into conflict->inds
12768 * it isn't a constraint we can relieve.
12770 for(uset = set->member->use; uset; uset = uset->next) {
12771 if (uset->member == ins) {
12781 /* Don't drop canidates with constraints */
12782 if (conflict->constraints && !constraints) {
12788 fprintf(stderr, "conflict ins? %p %s count: %d constraints: %d\n",
12789 ins, tops(ins->op), count, constraints);
12791 /* Find the instruction with the largest possible subset of
12792 * conflict ranges and that dominates any other instruction
12793 * with an equal sized set of conflicting ranges.
12795 if ((count > conflict->count) ||
12796 ((count == conflict->count) &&
12797 tdominates(state, ins, conflict->ins))) {
12798 struct triple_reg_set *next;
12799 /* Remember the canidate instruction */
12800 conflict->ins = ins;
12801 conflict->count = count;
12802 conflict->constraints = constraints;
12803 /* Free the old collection of live registers */
12804 for(set = conflict->live; set; set = next) {
12806 do_triple_unset(&conflict->live, set->member);
12808 conflict->live = 0;
12809 /* Rember the registers that are alive but do not feed
12810 * into or out of conflict->ins.
12812 for(set = live; set; set = set->next) {
12813 struct triple **expr;
12814 if (set->member == ins) {
12817 expr = triple_rhs(state, ins, 0);
12818 for(;expr; expr = triple_rhs(state, ins, expr)) {
12819 if (*expr == set->member) {
12823 expr = triple_lhs(state, ins, 0);
12824 for(; expr; expr = triple_lhs(state, ins, expr)) {
12825 if (*expr == set->member) {
12829 do_triple_set(&conflict->live, set->member, set->new);
12837 static void find_range_conflict(struct compile_state *state,
12838 struct reg_state *rstate, char *used, struct live_range *ref_range,
12839 struct least_conflict *conflict)
12842 /* there are 3 kinds ways conflicts can occure.
12843 * 1) the life time of 2 values simply overlap.
12844 * 2) the 2 values feed into the same instruction.
12845 * 3) the 2 values feed into a phi function.
12848 /* find the instruction where the problematic conflict comes
12849 * into existance. that the instruction where all of
12850 * the values are alive, and among such instructions it is
12851 * the least dominated one.
12853 * a value is alive an an instruction if either;
12854 * 1) the value defintion dominates the instruction and there
12855 * is a use at or after that instrction
12856 * 2) the value definition feeds into a phi function in the
12857 * same block as the instruction. and the phi function
12858 * is at or after the instruction.
12860 memset(conflict, 0, sizeof(*conflict));
12861 conflict->rstate = rstate;
12862 conflict->ref_range = ref_range;
12864 conflict->live = 0;
12865 conflict->count = 0;
12866 conflict->constraints = 0;
12867 walk_variable_lifetimes(state, rstate->blocks, least_conflict, conflict);
12869 if (!conflict->ins) {
12870 internal_error(state, ref_range->defs->def, "No conflict ins?");
12872 if (!conflict->live) {
12873 internal_error(state, ref_range->defs->def, "No conflict live?");
12876 fprintf(stderr, "conflict ins: %p %s count: %d constraints: %d\n",
12877 conflict->ins, tops(conflict->ins->op),
12878 conflict->count, conflict->constraints);
12883 static struct triple *split_constrained_range(struct compile_state *state,
12884 struct reg_state *rstate, char *used, struct least_conflict *conflict)
12886 unsigned constrained_size;
12887 struct triple *new, *constrained;
12888 struct triple_reg_set *cset;
12889 /* Find a range that is having problems because it is
12890 * artificially constrained.
12892 constrained_size = ~0;
12895 for(cset = conflict->live; cset; cset = cset->next) {
12896 struct triple_set *set;
12897 struct reg_info info;
12899 unsigned cur_size, size;
12900 /* Skip the live range that starts with conflict->ins */
12901 if (cset->member == conflict->ins) {
12904 /* Find how many registers this value can potentially
12907 classes = arch_type_to_regcm(state, cset->member->type);
12908 size = regc_max_size(state, classes);
12910 /* Find how many registers we allow this value to
12913 info = arch_reg_lhs(state, cset->member, 0);
12915 /* If the register doesn't need a register
12916 * splitting it can't help.
12918 if (info.reg == REG_UNNEEDED) {
12921 #warning "FIXME do I need a call to arch_reg_rhs around here somewhere?"
12922 if ((info.reg == REG_UNSET) || (info.reg >= MAX_REGISTERS)) {
12923 cur_size = regc_max_size(state, info.regcm);
12927 /* If this live_range feeds into conflict->ins
12928 * splitting it is unlikely to help.
12930 for(set = cset->member->use; set; set = set->next) {
12931 if (set->member == conflict->ins) {
12936 /* If there is no difference between potential and
12937 * actual register count there is nothing to do.
12939 if (cur_size >= size) {
12942 /* Of the constrained registers deal with the
12943 * most constrained one first.
12945 if (!constrained ||
12946 (size < constrained_size)) {
12947 constrained = cset->member;
12948 constrained_size = size;
12954 new = post_copy(state, constrained);
12955 new->id |= TRIPLE_FLAG_POST_SPLIT;
12960 static int split_ranges(
12961 struct compile_state *state, struct reg_state *rstate,
12962 char *used, struct live_range *range)
12964 struct triple *new;
12967 fprintf(stderr, "split_ranges %d %s %p\n",
12968 rstate->passes, tops(range->defs->def->op), range->defs->def);
12970 if ((range->color == REG_UNNEEDED) ||
12971 (rstate->passes >= rstate->max_passes)) {
12975 /* If I can't allocate a register something needs to be split */
12976 if (arch_select_free_register(state, used, range->classes) == REG_UNSET) {
12977 struct least_conflict conflict;
12980 fprintf(stderr, "find_range_conflict\n");
12982 /* Find where in the set of registers the conflict
12985 find_range_conflict(state, rstate, used, range, &conflict);
12987 /* If a range has been artifically constrained split it */
12988 new = split_constrained_range(state, rstate, used, &conflict);
12991 /* Ideally I would split the live range that will not be used
12992 * for the longest period of time in hopes that this will
12993 * (a) allow me to spill a register or
12994 * (b) allow me to place a value in another register.
12996 * So far I don't have a test case for this, the resolving
12997 * of mandatory constraints has solved all of my
12998 * know issues. So I have choosen not to write any
12999 * code until I cat get a better feel for cases where
13000 * it would be useful to have.
13003 #warning "WISHLIST implement live range splitting..."
13005 print_blocks(state, stderr);
13006 print_dominators(state, stderr);
13013 rstate->lrd[rstate->defs].orig_id = new->id;
13014 new->id = rstate->defs;
13017 fprintf(stderr, "new: %p old: %s %p\n",
13018 new, tops(RHS(new, 0)->op), RHS(new, 0));
13021 print_blocks(state, stderr);
13022 print_dominators(state, stderr);
13030 #if DEBUG_COLOR_GRAPH > 1
13031 #define cgdebug_printf(...) fprintf(stdout, __VA_ARGS__)
13032 #define cgdebug_flush() fflush(stdout)
13033 #elif DEBUG_COLOR_GRAPH == 1
13034 #define cgdebug_printf(...) fprintf(stderr, __VA_ARGS__)
13035 #define cgdebug_flush() fflush(stderr)
13037 #define cgdebug_printf(...)
13038 #define cgdebug_flush()
13042 static int select_free_color(struct compile_state *state,
13043 struct reg_state *rstate, struct live_range *range)
13045 struct triple_set *entry;
13046 struct live_range_def *lrd;
13047 struct live_range_def *phi;
13048 struct live_range_edge *edge;
13049 char used[MAX_REGISTERS];
13050 struct triple **expr;
13052 /* Instead of doing just the trivial color select here I try
13053 * a few extra things because a good color selection will help reduce
13057 /* Find the registers currently in use */
13058 memset(used, 0, sizeof(used));
13059 for(edge = range->edges; edge; edge = edge->next) {
13060 if (edge->node->color == REG_UNSET) {
13063 reg_fill_used(state, used, edge->node->color);
13065 #if DEBUG_COLOR_GRAPH > 1
13069 for(edge = range->edges; edge; edge = edge->next) {
13072 cgdebug_printf("\n%s edges: %d @%s:%d.%d\n",
13073 tops(range->def->op), i,
13074 range->def->filename, range->def->line, range->def->col);
13075 for(i = 0; i < MAX_REGISTERS; i++) {
13077 cgdebug_printf("used: %s\n",
13084 #warning "FIXME detect conflicts caused by the source and destination being the same register"
13086 /* If a color is already assigned see if it will work */
13087 if (range->color != REG_UNSET) {
13088 struct live_range_def *lrd;
13089 if (!used[range->color]) {
13092 for(edge = range->edges; edge; edge = edge->next) {
13093 if (edge->node->color != range->color) {
13096 warning(state, edge->node->defs->def, "edge: ");
13097 lrd = edge->node->defs;
13099 warning(state, lrd->def, " %p %s",
13100 lrd->def, tops(lrd->def->op));
13102 } while(lrd != edge->node->defs);
13105 warning(state, range->defs->def, "def: ");
13107 warning(state, lrd->def, " %p %s",
13108 lrd->def, tops(lrd->def->op));
13110 } while(lrd != range->defs);
13111 internal_error(state, range->defs->def,
13112 "live range with already used color %s",
13113 arch_reg_str(range->color));
13116 /* If I feed into an expression reuse it's color.
13117 * This should help remove copies in the case of 2 register instructions
13118 * and phi functions.
13121 lrd = live_range_end(state, range, 0);
13122 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
13123 entry = lrd->def->use;
13124 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
13125 struct live_range_def *insd;
13126 insd = &rstate->lrd[entry->member->id];
13127 if (insd->lr->defs == 0) {
13130 if (!phi && (insd->def->op == OP_PHI) &&
13131 !interfere(rstate, range, insd->lr)) {
13134 if ((insd->lr->color == REG_UNSET) ||
13135 ((insd->lr->classes & range->classes) == 0) ||
13136 (used[insd->lr->color])) {
13139 if (interfere(rstate, range, insd->lr)) {
13142 range->color = insd->lr->color;
13145 /* If I feed into a phi function reuse it's color or the color
13146 * of something else that feeds into the phi function.
13149 if (phi->lr->color != REG_UNSET) {
13150 if (used[phi->lr->color]) {
13151 range->color = phi->lr->color;
13155 expr = triple_rhs(state, phi->def, 0);
13156 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
13157 struct live_range *lr;
13161 lr = rstate->lrd[(*expr)->id].lr;
13162 if ((lr->color == REG_UNSET) ||
13163 ((lr->classes & range->classes) == 0) ||
13164 (used[lr->color])) {
13167 if (interfere(rstate, range, lr)) {
13170 range->color = lr->color;
13174 /* If I don't interfere with a rhs node reuse it's color */
13175 lrd = live_range_head(state, range, 0);
13176 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
13177 expr = triple_rhs(state, lrd->def, 0);
13178 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
13179 struct live_range *lr;
13183 lr = rstate->lrd[(*expr)->id].lr;
13184 if ((lr->color == -1) ||
13185 ((lr->classes & range->classes) == 0) ||
13186 (used[lr->color])) {
13189 if (interfere(rstate, range, lr)) {
13192 range->color = lr->color;
13196 /* If I have not opportunitically picked a useful color
13197 * pick the first color that is free.
13199 if (range->color == REG_UNSET) {
13201 arch_select_free_register(state, used, range->classes);
13203 if (range->color == REG_UNSET) {
13204 struct live_range_def *lrd;
13206 if (split_ranges(state, rstate, used, range)) {
13209 for(edge = range->edges; edge; edge = edge->next) {
13210 warning(state, edge->node->defs->def, "edge reg %s",
13211 arch_reg_str(edge->node->color));
13212 lrd = edge->node->defs;
13214 warning(state, lrd->def, " %s",
13215 tops(lrd->def->op));
13217 } while(lrd != edge->node->defs);
13219 warning(state, range->defs->def, "range: ");
13222 warning(state, lrd->def, " %s",
13223 tops(lrd->def->op));
13225 } while(lrd != range->defs);
13227 warning(state, range->defs->def, "classes: %x",
13229 for(i = 0; i < MAX_REGISTERS; i++) {
13231 warning(state, range->defs->def, "used: %s",
13235 #if DEBUG_COLOR_GRAPH < 2
13236 error(state, range->defs->def, "too few registers");
13238 internal_error(state, range->defs->def, "too few registers");
13241 range->classes = arch_reg_regcm(state, range->color);
13242 if (range->color == -1) {
13243 internal_error(state, range->defs->def, "select_free_color did not?");
13248 static int color_graph(struct compile_state *state, struct reg_state *rstate)
13251 struct live_range_edge *edge;
13252 struct live_range *range;
13254 cgdebug_printf("Lo: ");
13255 range = rstate->low;
13256 if (*range->group_prev != range) {
13257 internal_error(state, 0, "lo: *prev != range?");
13259 *range->group_prev = range->group_next;
13260 if (range->group_next) {
13261 range->group_next->group_prev = range->group_prev;
13263 if (&range->group_next == rstate->low_tail) {
13264 rstate->low_tail = range->group_prev;
13266 if (rstate->low == range) {
13267 internal_error(state, 0, "low: next != prev?");
13270 else if (rstate->high) {
13271 cgdebug_printf("Hi: ");
13272 range = rstate->high;
13273 if (*range->group_prev != range) {
13274 internal_error(state, 0, "hi: *prev != range?");
13276 *range->group_prev = range->group_next;
13277 if (range->group_next) {
13278 range->group_next->group_prev = range->group_prev;
13280 if (&range->group_next == rstate->high_tail) {
13281 rstate->high_tail = range->group_prev;
13283 if (rstate->high == range) {
13284 internal_error(state, 0, "high: next != prev?");
13290 cgdebug_printf(" %d\n", range - rstate->lr);
13291 range->group_prev = 0;
13292 for(edge = range->edges; edge; edge = edge->next) {
13293 struct live_range *node;
13295 /* Move nodes from the high to the low list */
13296 if (node->group_prev && (node->color == REG_UNSET) &&
13297 (node->degree == regc_max_size(state, node->classes))) {
13298 if (*node->group_prev != node) {
13299 internal_error(state, 0, "move: *prev != node?");
13301 *node->group_prev = node->group_next;
13302 if (node->group_next) {
13303 node->group_next->group_prev = node->group_prev;
13305 if (&node->group_next == rstate->high_tail) {
13306 rstate->high_tail = node->group_prev;
13308 cgdebug_printf("Moving...%d to low\n", node - rstate->lr);
13309 node->group_prev = rstate->low_tail;
13310 node->group_next = 0;
13311 *rstate->low_tail = node;
13312 rstate->low_tail = &node->group_next;
13313 if (*node->group_prev != node) {
13314 internal_error(state, 0, "move2: *prev != node?");
13319 colored = color_graph(state, rstate);
13321 cgdebug_printf("Coloring %d @%s:%d.%d:",
13322 range - rstate->lr,
13323 range->def->filename, range->def->line, range->def->col);
13325 colored = select_free_color(state, rstate, range);
13326 cgdebug_printf(" %s\n", arch_reg_str(range->color));
13331 static void verify_colors(struct compile_state *state, struct reg_state *rstate)
13333 struct live_range *lr;
13334 struct live_range_edge *edge;
13335 struct triple *ins, *first;
13336 char used[MAX_REGISTERS];
13337 first = RHS(state->main_function, 0);
13340 if (triple_is_def(state, ins)) {
13341 if ((ins->id < 0) || (ins->id > rstate->defs)) {
13342 internal_error(state, ins,
13343 "triple without a live range def");
13345 lr = rstate->lrd[ins->id].lr;
13346 if (lr->color == REG_UNSET) {
13347 internal_error(state, ins,
13348 "triple without a color");
13350 /* Find the registers used by the edges */
13351 memset(used, 0, sizeof(used));
13352 for(edge = lr->edges; edge; edge = edge->next) {
13353 if (edge->node->color == REG_UNSET) {
13354 internal_error(state, 0,
13355 "live range without a color");
13357 reg_fill_used(state, used, edge->node->color);
13359 if (used[lr->color]) {
13360 internal_error(state, ins,
13361 "triple with already used color");
13365 } while(ins != first);
13368 static void color_triples(struct compile_state *state, struct reg_state *rstate)
13370 struct live_range *lr;
13371 struct triple *first, *ins;
13372 first = RHS(state->main_function, 0);
13375 if ((ins->id < 0) || (ins->id > rstate->defs)) {
13376 internal_error(state, ins,
13377 "triple without a live range");
13379 lr = rstate->lrd[ins->id].lr;
13380 SET_REG(ins->id, lr->color);
13382 } while (ins != first);
13385 static void print_interference_block(
13386 struct compile_state *state, struct block *block, void *arg)
13389 struct reg_state *rstate = arg;
13390 struct reg_block *rb;
13391 struct triple *ptr;
13394 rb = &rstate->blocks[block->vertex];
13396 printf("\nblock: %p (%d), %p<-%p %p<-%p\n",
13400 block->left && block->left->use?block->left->use->member : 0,
13402 block->right && block->right->use?block->right->use->member : 0);
13404 struct triple_reg_set *in_set;
13406 for(in_set = rb->in; in_set; in_set = in_set->next) {
13407 printf(" %-10p", in_set->member);
13412 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13413 done = (ptr == block->last);
13414 if (ptr->op == OP_PHI) {
13421 for(edge = 0; edge < block->users; edge++) {
13422 printf(" in(%d):", edge);
13423 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13424 struct triple **slot;
13425 done = (ptr == block->last);
13426 if (ptr->op != OP_PHI) {
13429 slot = &RHS(ptr, 0);
13430 printf(" %-10p", slot[edge]);
13435 if (block->first->op == OP_LABEL) {
13436 printf("%p:\n", block->first);
13438 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13439 struct triple_set *user;
13440 struct live_range *lr;
13444 done = (ptr == block->last);
13445 lr = rstate->lrd[ptr->id].lr;
13447 if (triple_stores_block(state, ptr)) {
13448 if (ptr->u.block != block) {
13449 internal_error(state, ptr,
13450 "Wrong block pointer: %p",
13454 if (op == OP_ADECL) {
13455 for(user = ptr->use; user; user = user->next) {
13456 if (!user->member->u.block) {
13457 internal_error(state, user->member,
13458 "Use %p not in a block?",
13465 ptr->id = rstate->lrd[id].orig_id;
13466 SET_REG(ptr->id, lr->color);
13467 display_triple(stdout, ptr);
13470 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
13471 internal_error(state, ptr, "lr has no defs!");
13475 struct live_range_def *lrd;
13479 printf(" %-10p", lrd->def);
13481 } while(lrd != lr->defs);
13484 if (lr->edges > 0) {
13485 struct live_range_edge *edge;
13487 for(edge = lr->edges; edge; edge = edge->next) {
13488 struct live_range_def *lrd;
13489 lrd = edge->node->defs;
13491 printf(" %-10p", lrd->def);
13493 } while(lrd != edge->node->defs);
13498 /* Do a bunch of sanity checks */
13499 valid_ins(state, ptr);
13500 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
13501 internal_error(state, ptr, "Invalid triple id: %d",
13504 for(user = ptr->use; user; user = user->next) {
13505 struct triple *use;
13506 struct live_range *ulr;
13507 use = user->member;
13508 valid_ins(state, use);
13509 if ((use->id < 0) || (use->id > rstate->defs)) {
13510 internal_error(state, use, "Invalid triple id: %d",
13513 ulr = rstate->lrd[user->member->id].lr;
13514 if (triple_stores_block(state, user->member) &&
13515 !user->member->u.block) {
13516 internal_error(state, user->member,
13517 "Use %p not in a block?",
13523 struct triple_reg_set *out_set;
13525 for(out_set = rb->out; out_set; out_set = out_set->next) {
13526 printf(" %-10p", out_set->member);
13533 static struct live_range *merge_sort_lr(
13534 struct live_range *first, struct live_range *last)
13536 struct live_range *mid, *join, **join_tail, *pick;
13538 size = (last - first) + 1;
13540 mid = first + size/2;
13541 first = merge_sort_lr(first, mid -1);
13542 mid = merge_sort_lr(mid, last);
13546 /* merge the two lists */
13547 while(first && mid) {
13548 if ((first->degree < mid->degree) ||
13549 ((first->degree == mid->degree) &&
13550 (first->length < mid->length))) {
13552 first = first->group_next;
13554 first->group_prev = 0;
13559 mid = mid->group_next;
13561 mid->group_prev = 0;
13564 pick->group_next = 0;
13565 pick->group_prev = join_tail;
13567 join_tail = &pick->group_next;
13569 /* Splice the remaining list */
13570 pick = (first)? first : mid;
13573 pick->group_prev = join_tail;
13577 if (!first->defs) {
13585 static void ids_from_rstate(struct compile_state *state,
13586 struct reg_state *rstate)
13588 struct triple *ins, *first;
13589 if (!rstate->defs) {
13592 /* Display the graph if desired */
13593 if (state->debug & DEBUG_INTERFERENCE) {
13594 print_blocks(state, stdout);
13595 print_control_flow(state);
13597 first = RHS(state->main_function, 0);
13601 struct live_range_def *lrd;
13602 lrd = &rstate->lrd[ins->id];
13603 ins->id = lrd->orig_id;
13606 } while(ins != first);
13609 static void cleanup_live_edges(struct reg_state *rstate)
13612 /* Free the edges on each node */
13613 for(i = 1; i <= rstate->ranges; i++) {
13614 remove_live_edges(rstate, &rstate->lr[i]);
13618 static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
13620 cleanup_live_edges(rstate);
13621 xfree(rstate->lrd);
13624 /* Free the variable lifetime information */
13625 if (rstate->blocks) {
13626 free_variable_lifetimes(state, rstate->blocks);
13629 rstate->ranges = 0;
13632 rstate->blocks = 0;
13635 static void verify_consistency(struct compile_state *state);
13636 static void allocate_registers(struct compile_state *state)
13638 struct reg_state rstate;
13641 /* Clear out the reg_state */
13642 memset(&rstate, 0, sizeof(rstate));
13643 rstate.max_passes = MAX_ALLOCATION_PASSES;
13646 struct live_range **point, **next;
13651 fprintf(stderr, "pass: %d\n", rstate.passes);
13655 ids_from_rstate(state, &rstate);
13657 /* Cleanup the temporary data structures */
13658 cleanup_rstate(state, &rstate);
13660 /* Compute the variable lifetimes */
13661 rstate.blocks = compute_variable_lifetimes(state);
13663 /* Fix invalid mandatory live range coalesce conflicts */
13664 walk_variable_lifetimes(
13665 state, rstate.blocks, fix_coalesce_conflicts, 0);
13667 /* Fix two simultaneous uses of the same register.
13668 * In a few pathlogical cases a partial untangle moves
13669 * the tangle to a part of the graph we won't revisit.
13670 * So we keep looping until we have no more tangle fixes
13674 tangles = correct_tangles(state, rstate.blocks);
13677 if (state->debug & DEBUG_INSERTED_COPIES) {
13678 printf("After resolve_tangles\n");
13679 print_blocks(state, stdout);
13680 print_control_flow(state);
13682 verify_consistency(state);
13684 /* Allocate and initialize the live ranges */
13685 initialize_live_ranges(state, &rstate);
13687 /* Note current doing coalescing in a loop appears to
13688 * buys me nothing. The code is left this way in case
13689 * there is some value in it. Or if a future bugfix
13690 * yields some benefit.
13694 fprintf(stderr, "coalescing\n");
13696 /* Remove any previous live edge calculations */
13697 cleanup_live_edges(&rstate);
13699 /* Compute the interference graph */
13700 walk_variable_lifetimes(
13701 state, rstate.blocks, graph_ins, &rstate);
13703 /* Display the interference graph if desired */
13704 if (state->debug & DEBUG_INTERFERENCE) {
13705 printf("\nlive variables by block\n");
13706 walk_blocks(state, print_interference_block, &rstate);
13707 printf("\nlive variables by instruction\n");
13708 walk_variable_lifetimes(
13709 state, rstate.blocks,
13710 print_interference_ins, &rstate);
13713 coalesced = coalesce_live_ranges(state, &rstate);
13716 fprintf(stderr, "coalesced: %d\n", coalesced);
13718 } while(coalesced);
13720 #if DEBUG_CONSISTENCY > 1
13722 fprintf(stderr, "verify_graph_ins...\n");
13724 /* Verify the interference graph */
13725 walk_variable_lifetimes(
13726 state, rstate.blocks, verify_graph_ins, &rstate);
13728 fprintf(stderr, "verify_graph_ins done\n");
13732 /* Build the groups low and high. But with the nodes
13733 * first sorted by degree order.
13735 rstate.low_tail = &rstate.low;
13736 rstate.high_tail = &rstate.high;
13737 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
13739 rstate.high->group_prev = &rstate.high;
13741 for(point = &rstate.high; *point; point = &(*point)->group_next)
13743 rstate.high_tail = point;
13744 /* Walk through the high list and move everything that needs
13747 for(point = &rstate.high; *point; point = next) {
13748 struct live_range *range;
13749 next = &(*point)->group_next;
13752 /* If it has a low degree or it already has a color
13753 * place the node in low.
13755 if ((range->degree < regc_max_size(state, range->classes)) ||
13756 (range->color != REG_UNSET)) {
13757 cgdebug_printf("Lo: %5d degree %5d%s\n",
13758 range - rstate.lr, range->degree,
13759 (range->color != REG_UNSET) ? " (colored)": "");
13760 *range->group_prev = range->group_next;
13761 if (range->group_next) {
13762 range->group_next->group_prev = range->group_prev;
13764 if (&range->group_next == rstate.high_tail) {
13765 rstate.high_tail = range->group_prev;
13767 range->group_prev = rstate.low_tail;
13768 range->group_next = 0;
13769 *rstate.low_tail = range;
13770 rstate.low_tail = &range->group_next;
13774 cgdebug_printf("hi: %5d degree %5d%s\n",
13775 range - rstate.lr, range->degree,
13776 (range->color != REG_UNSET) ? " (colored)": "");
13779 /* Color the live_ranges */
13780 colored = color_graph(state, &rstate);
13782 } while (!colored);
13784 /* Verify the graph was properly colored */
13785 verify_colors(state, &rstate);
13787 /* Move the colors from the graph to the triples */
13788 color_triples(state, &rstate);
13790 /* Cleanup the temporary data structures */
13791 cleanup_rstate(state, &rstate);
13794 /* Sparce Conditional Constant Propogation
13795 * =========================================
13799 struct lattice_node {
13801 struct triple *def;
13802 struct ssa_edge *out;
13803 struct flow_block *fblock;
13804 struct triple *val;
13805 /* lattice high val && !is_const(val)
13806 * lattice const is_const(val)
13807 * lattice low val == 0
13811 struct lattice_node *src;
13812 struct lattice_node *dst;
13813 struct ssa_edge *work_next;
13814 struct ssa_edge *work_prev;
13815 struct ssa_edge *out_next;
13818 struct flow_block *src;
13819 struct flow_block *dst;
13820 struct flow_edge *work_next;
13821 struct flow_edge *work_prev;
13822 struct flow_edge *in_next;
13823 struct flow_edge *out_next;
13826 struct flow_block {
13827 struct block *block;
13828 struct flow_edge *in;
13829 struct flow_edge *out;
13830 struct flow_edge left, right;
13835 struct lattice_node *lattice;
13836 struct ssa_edge *ssa_edges;
13837 struct flow_block *flow_blocks;
13838 struct flow_edge *flow_work_list;
13839 struct ssa_edge *ssa_work_list;
13843 static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
13844 struct flow_edge *fedge)
13846 if (!scc->flow_work_list) {
13847 scc->flow_work_list = fedge;
13848 fedge->work_next = fedge->work_prev = fedge;
13851 struct flow_edge *ftail;
13852 ftail = scc->flow_work_list->work_prev;
13853 fedge->work_next = ftail->work_next;
13854 fedge->work_prev = ftail;
13855 fedge->work_next->work_prev = fedge;
13856 fedge->work_prev->work_next = fedge;
13860 static struct flow_edge *scc_next_fedge(
13861 struct compile_state *state, struct scc_state *scc)
13863 struct flow_edge *fedge;
13864 fedge = scc->flow_work_list;
13866 fedge->work_next->work_prev = fedge->work_prev;
13867 fedge->work_prev->work_next = fedge->work_next;
13868 if (fedge->work_next != fedge) {
13869 scc->flow_work_list = fedge->work_next;
13871 scc->flow_work_list = 0;
13877 static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
13878 struct ssa_edge *sedge)
13880 if (!scc->ssa_work_list) {
13881 scc->ssa_work_list = sedge;
13882 sedge->work_next = sedge->work_prev = sedge;
13885 struct ssa_edge *stail;
13886 stail = scc->ssa_work_list->work_prev;
13887 sedge->work_next = stail->work_next;
13888 sedge->work_prev = stail;
13889 sedge->work_next->work_prev = sedge;
13890 sedge->work_prev->work_next = sedge;
13894 static struct ssa_edge *scc_next_sedge(
13895 struct compile_state *state, struct scc_state *scc)
13897 struct ssa_edge *sedge;
13898 sedge = scc->ssa_work_list;
13900 sedge->work_next->work_prev = sedge->work_prev;
13901 sedge->work_prev->work_next = sedge->work_next;
13902 if (sedge->work_next != sedge) {
13903 scc->ssa_work_list = sedge->work_next;
13905 scc->ssa_work_list = 0;
13911 static void initialize_scc_state(
13912 struct compile_state *state, struct scc_state *scc)
13914 int ins_count, ssa_edge_count;
13915 int ins_index, ssa_edge_index, fblock_index;
13916 struct triple *first, *ins;
13917 struct block *block;
13918 struct flow_block *fblock;
13920 memset(scc, 0, sizeof(*scc));
13922 /* Inialize pass zero find out how much memory we need */
13923 first = RHS(state->main_function, 0);
13925 ins_count = ssa_edge_count = 0;
13927 struct triple_set *edge;
13929 for(edge = ins->use; edge; edge = edge->next) {
13933 } while(ins != first);
13935 fprintf(stderr, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
13936 ins_count, ssa_edge_count, state->last_vertex);
13938 scc->ins_count = ins_count;
13940 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
13942 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
13944 xcmalloc(sizeof(*scc->flow_blocks)*(state->last_vertex + 1),
13947 /* Initialize pass one collect up the nodes */
13950 ins_index = ssa_edge_index = fblock_index = 0;
13953 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
13954 block = ins->u.block;
13956 internal_error(state, ins, "label without block");
13959 block->vertex = fblock_index;
13960 fblock = &scc->flow_blocks[fblock_index];
13961 fblock->block = block;
13964 struct lattice_node *lnode;
13966 lnode = &scc->lattice[ins_index];
13969 lnode->fblock = fblock;
13970 lnode->val = ins; /* LATTICE HIGH */
13971 lnode->old_id = ins->id;
13972 ins->id = ins_index;
13975 } while(ins != first);
13976 /* Initialize pass two collect up the edges */
13981 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
13982 struct flow_edge *fedge, **ftail;
13983 struct block_set *bedge;
13984 block = ins->u.block;
13985 fblock = &scc->flow_blocks[block->vertex];
13988 ftail = &fblock->out;
13990 fblock->left.dst = &scc->flow_blocks[block->left->vertex];
13991 if (fblock->left.dst->block != block->left) {
13992 internal_error(state, 0, "block mismatch");
13994 fblock->left.out_next = 0;
13995 *ftail = &fblock->left;
13996 ftail = &fblock->left.out_next;
13998 if (block->right) {
13999 fblock->right.dst = &scc->flow_blocks[block->right->vertex];
14000 if (fblock->right.dst->block != block->right) {
14001 internal_error(state, 0, "block mismatch");
14003 fblock->right.out_next = 0;
14004 *ftail = &fblock->right;
14005 ftail = &fblock->right.out_next;
14007 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
14008 fedge->src = fblock;
14009 fedge->work_next = fedge->work_prev = fedge;
14010 fedge->executable = 0;
14012 ftail = &fblock->in;
14013 for(bedge = block->use; bedge; bedge = bedge->next) {
14014 struct block *src_block;
14015 struct flow_block *sfblock;
14016 struct flow_edge *sfedge;
14017 src_block = bedge->member;
14018 sfblock = &scc->flow_blocks[src_block->vertex];
14020 if (src_block->left == block) {
14021 sfedge = &sfblock->left;
14023 sfedge = &sfblock->right;
14026 ftail = &sfedge->in_next;
14027 sfedge->in_next = 0;
14031 struct triple_set *edge;
14032 struct ssa_edge **stail;
14033 struct lattice_node *lnode;
14034 lnode = &scc->lattice[ins->id];
14036 stail = &lnode->out;
14037 for(edge = ins->use; edge; edge = edge->next) {
14038 struct ssa_edge *sedge;
14039 ssa_edge_index += 1;
14040 sedge = &scc->ssa_edges[ssa_edge_index];
14042 stail = &sedge->out_next;
14043 sedge->src = lnode;
14044 sedge->dst = &scc->lattice[edge->member->id];
14045 sedge->work_next = sedge->work_prev = sedge;
14046 sedge->out_next = 0;
14050 } while(ins != first);
14051 /* Setup a dummy block 0 as a node above the start node */
14053 struct flow_block *fblock, *dst;
14054 struct flow_edge *fedge;
14055 fblock = &scc->flow_blocks[0];
14058 fblock->out = &fblock->left;
14059 dst = &scc->flow_blocks[state->first_block->vertex];
14060 fedge = &fblock->left;
14061 fedge->src = fblock;
14063 fedge->work_next = fedge;
14064 fedge->work_prev = fedge;
14065 fedge->in_next = fedge->dst->in;
14066 fedge->out_next = 0;
14067 fedge->executable = 0;
14068 fedge->dst->in = fedge;
14070 /* Initialize the work lists */
14071 scc->flow_work_list = 0;
14072 scc->ssa_work_list = 0;
14073 scc_add_fedge(state, scc, fedge);
14076 fprintf(stderr, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
14077 ins_index, ssa_edge_index, fblock_index);
14082 static void free_scc_state(
14083 struct compile_state *state, struct scc_state *scc)
14085 xfree(scc->flow_blocks);
14086 xfree(scc->ssa_edges);
14087 xfree(scc->lattice);
14091 static struct lattice_node *triple_to_lattice(
14092 struct compile_state *state, struct scc_state *scc, struct triple *ins)
14094 if (ins->id <= 0) {
14095 internal_error(state, ins, "bad id");
14097 return &scc->lattice[ins->id];
14100 static struct triple *preserve_lval(
14101 struct compile_state *state, struct lattice_node *lnode)
14103 struct triple *old;
14104 /* Preserve the original value */
14106 old = dup_triple(state, lnode->val);
14107 if (lnode->val != lnode->def) {
14117 static int lval_changed(struct compile_state *state,
14118 struct triple *old, struct lattice_node *lnode)
14121 /* See if the lattice value has changed */
14123 if (!old && !lnode->val) {
14126 if (changed && lnode->val && !is_const(lnode->val)) {
14130 lnode->val && old &&
14131 (memcmp(lnode->val->param, old->param,
14132 TRIPLE_SIZE(lnode->val->sizes) * sizeof(lnode->val->param[0])) == 0) &&
14133 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
14143 static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
14144 struct lattice_node *lnode)
14146 struct lattice_node *tmp;
14147 struct triple **slot, *old;
14148 struct flow_edge *fedge;
14150 if (lnode->def->op != OP_PHI) {
14151 internal_error(state, lnode->def, "not phi");
14153 /* Store the original value */
14154 old = preserve_lval(state, lnode);
14156 /* default to lattice high */
14157 lnode->val = lnode->def;
14158 slot = &RHS(lnode->def, 0);
14160 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
14161 if (!fedge->executable) {
14164 if (!slot[index]) {
14165 internal_error(state, lnode->def, "no phi value");
14167 tmp = triple_to_lattice(state, scc, slot[index]);
14168 /* meet(X, lattice low) = lattice low */
14172 /* meet(X, lattice high) = X */
14173 else if (!tmp->val) {
14174 lnode->val = lnode->val;
14176 /* meet(lattice high, X) = X */
14177 else if (!is_const(lnode->val)) {
14178 lnode->val = dup_triple(state, tmp->val);
14179 lnode->val->type = lnode->def->type;
14181 /* meet(const, const) = const or lattice low */
14182 else if (!constants_equal(state, lnode->val, tmp->val)) {
14190 fprintf(stderr, "phi: %d -> %s\n",
14192 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14194 /* If the lattice value has changed update the work lists. */
14195 if (lval_changed(state, old, lnode)) {
14196 struct ssa_edge *sedge;
14197 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14198 scc_add_sedge(state, scc, sedge);
14203 static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
14204 struct lattice_node *lnode)
14207 struct triple *old, *scratch;
14208 struct triple **dexpr, **vexpr;
14211 /* Store the original value */
14212 old = preserve_lval(state, lnode);
14214 /* Reinitialize the value */
14215 lnode->val = scratch = dup_triple(state, lnode->def);
14216 scratch->id = lnode->old_id;
14217 scratch->next = scratch;
14218 scratch->prev = scratch;
14221 count = TRIPLE_SIZE(scratch->sizes);
14222 for(i = 0; i < count; i++) {
14223 dexpr = &lnode->def->param[i];
14224 vexpr = &scratch->param[i];
14226 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14227 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14229 struct lattice_node *tmp;
14230 tmp = triple_to_lattice(state, scc, *dexpr);
14231 *vexpr = (tmp->val)? tmp->val : tmp->def;
14234 if (scratch->op == OP_BRANCH) {
14235 scratch->next = lnode->def->next;
14237 /* Recompute the value */
14238 #warning "FIXME see if simplify does anything bad"
14239 /* So far it looks like only the strength reduction
14240 * optimization are things I need to worry about.
14242 simplify(state, scratch);
14243 /* Cleanup my value */
14244 if (scratch->use) {
14245 internal_error(state, lnode->def, "scratch used?");
14247 if ((scratch->prev != scratch) ||
14248 ((scratch->next != scratch) &&
14249 ((lnode->def->op != OP_BRANCH) ||
14250 (scratch->next != lnode->def->next)))) {
14251 internal_error(state, lnode->def, "scratch in list?");
14253 /* undo any uses... */
14254 count = TRIPLE_SIZE(scratch->sizes);
14255 for(i = 0; i < count; i++) {
14256 vexpr = &scratch->param[i];
14258 unuse_triple(*vexpr, scratch);
14261 if (!is_const(scratch)) {
14262 for(i = 0; i < count; i++) {
14263 dexpr = &lnode->def->param[i];
14264 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14265 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14267 struct lattice_node *tmp;
14268 tmp = triple_to_lattice(state, scc, *dexpr);
14276 (lnode->val->op == lnode->def->op) &&
14277 (memcmp(lnode->val->param, lnode->def->param,
14278 count * sizeof(lnode->val->param[0])) == 0) &&
14279 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
14280 lnode->val = lnode->def;
14282 /* Find the cases that are always lattice lo */
14284 triple_is_def(state, lnode->val) &&
14285 !triple_is_pure(state, lnode->val)) {
14289 (lnode->val->op == OP_SDECL) &&
14290 (lnode->val != lnode->def)) {
14291 internal_error(state, lnode->def, "bad sdecl");
14293 /* See if the lattice value has changed */
14294 changed = lval_changed(state, old, lnode);
14295 if (lnode->val != scratch) {
14301 static void scc_visit_branch(struct compile_state *state, struct scc_state *scc,
14302 struct lattice_node *lnode)
14304 struct lattice_node *cond;
14307 struct flow_edge *fedge;
14308 fprintf(stderr, "branch: %d (",
14311 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
14312 fprintf(stderr, " %d", fedge->dst->block->vertex);
14314 fprintf(stderr, " )");
14315 if (TRIPLE_RHS(lnode->def->sizes) > 0) {
14316 fprintf(stderr, " <- %d",
14317 RHS(lnode->def, 0)->id);
14319 fprintf(stderr, "\n");
14322 if (lnode->def->op != OP_BRANCH) {
14323 internal_error(state, lnode->def, "not branch");
14325 /* This only applies to conditional branches */
14326 if (TRIPLE_RHS(lnode->def->sizes) == 0) {
14329 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
14330 if (cond->val && !is_const(cond->val)) {
14331 #warning "FIXME do I need to do something here?"
14332 warning(state, cond->def, "condition not constant?");
14335 if (cond->val == 0) {
14336 scc_add_fedge(state, scc, cond->fblock->out);
14337 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14339 else if (cond->val->u.cval) {
14340 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14343 scc_add_fedge(state, scc, cond->fblock->out);
14348 static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
14349 struct lattice_node *lnode)
14353 changed = compute_lnode_val(state, scc, lnode);
14356 struct triple **expr;
14357 fprintf(stderr, "expr: %3d %10s (",
14358 lnode->def->id, tops(lnode->def->op));
14359 expr = triple_rhs(state, lnode->def, 0);
14360 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
14362 fprintf(stderr, " %d", (*expr)->id);
14365 fprintf(stderr, " ) -> %s\n",
14366 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14369 if (lnode->def->op == OP_BRANCH) {
14370 scc_visit_branch(state, scc, lnode);
14373 else if (changed) {
14374 struct ssa_edge *sedge;
14375 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14376 scc_add_sedge(state, scc, sedge);
14381 static void scc_writeback_values(
14382 struct compile_state *state, struct scc_state *scc)
14384 struct triple *first, *ins;
14385 first = RHS(state->main_function, 0);
14388 struct lattice_node *lnode;
14389 lnode = triple_to_lattice(state, scc, ins);
14391 ins->id = lnode->old_id;
14393 if (lnode->val && !is_const(lnode->val)) {
14394 warning(state, lnode->def,
14395 "lattice node still high?");
14398 if (lnode->val && (lnode->val != ins)) {
14399 /* See if it something I know how to write back */
14400 switch(lnode->val->op) {
14402 mkconst(state, ins, lnode->val->u.cval);
14405 mkaddr_const(state, ins,
14406 MISC(lnode->val, 0), lnode->val->u.cval);
14409 /* By default don't copy the changes,
14410 * recompute them in place instead.
14412 simplify(state, ins);
14415 if (is_const(lnode->val) &&
14416 !constants_equal(state, lnode->val, ins)) {
14417 internal_error(state, 0, "constants not equal");
14419 /* Free the lattice nodes */
14424 } while(ins != first);
14427 static void scc_transform(struct compile_state *state)
14429 struct scc_state scc;
14431 initialize_scc_state(state, &scc);
14433 while(scc.flow_work_list || scc.ssa_work_list) {
14434 struct flow_edge *fedge;
14435 struct ssa_edge *sedge;
14436 struct flow_edge *fptr;
14437 while((fedge = scc_next_fedge(state, &scc))) {
14438 struct block *block;
14439 struct triple *ptr;
14440 struct flow_block *fblock;
14443 if (fedge->executable) {
14447 internal_error(state, 0, "fedge without dst");
14450 internal_error(state, 0, "fedge without src");
14452 fedge->executable = 1;
14453 fblock = fedge->dst;
14454 block = fblock->block;
14456 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14457 if (fptr->executable) {
14462 fprintf(stderr, "vertex: %d time: %d\n",
14463 block->vertex, time);
14467 for(ptr = block->first; !done; ptr = ptr->next) {
14468 struct lattice_node *lnode;
14469 done = (ptr == block->last);
14470 lnode = &scc.lattice[ptr->id];
14471 if (ptr->op == OP_PHI) {
14472 scc_visit_phi(state, &scc, lnode);
14474 else if (time == 1) {
14475 scc_visit_expr(state, &scc, lnode);
14478 if (fblock->out && !fblock->out->out_next) {
14479 scc_add_fedge(state, &scc, fblock->out);
14482 while((sedge = scc_next_sedge(state, &scc))) {
14483 struct lattice_node *lnode;
14484 struct flow_block *fblock;
14485 lnode = sedge->dst;
14486 fblock = lnode->fblock;
14488 fprintf(stderr, "sedge: %5d (%5d -> %5d)\n",
14489 sedge - scc.ssa_edges,
14490 sedge->src->def->id,
14491 sedge->dst->def->id);
14493 if (lnode->def->op == OP_PHI) {
14494 scc_visit_phi(state, &scc, lnode);
14497 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14498 if (fptr->executable) {
14503 scc_visit_expr(state, &scc, lnode);
14509 scc_writeback_values(state, &scc);
14510 free_scc_state(state, &scc);
14514 static void transform_to_arch_instructions(struct compile_state *state)
14516 struct triple *ins, *first;
14517 first = RHS(state->main_function, 0);
14520 ins = transform_to_arch_instruction(state, ins);
14521 } while(ins != first);
14524 #if DEBUG_CONSISTENCY
14525 static void verify_uses(struct compile_state *state)
14527 struct triple *first, *ins;
14528 struct triple_set *set;
14529 first = RHS(state->main_function, 0);
14532 struct triple **expr;
14533 expr = triple_rhs(state, ins, 0);
14534 for(; expr; expr = triple_rhs(state, ins, expr)) {
14535 struct triple *rhs;
14537 for(set = rhs?rhs->use:0; set; set = set->next) {
14538 if (set->member == ins) {
14543 internal_error(state, ins, "rhs not used");
14546 expr = triple_lhs(state, ins, 0);
14547 for(; expr; expr = triple_lhs(state, ins, expr)) {
14548 struct triple *lhs;
14550 for(set = lhs?lhs->use:0; set; set = set->next) {
14551 if (set->member == ins) {
14556 internal_error(state, ins, "lhs not used");
14560 } while(ins != first);
14563 static void verify_blocks(struct compile_state *state)
14565 struct triple *ins;
14566 struct block *block;
14567 block = state->first_block;
14572 for(ins = block->first; ins != block->last->next; ins = ins->next) {
14573 if (!triple_stores_block(state, ins)) {
14576 if (ins->u.block != block) {
14577 internal_error(state, ins, "inconsitent block specified");
14580 if (!triple_stores_block(state, block->last->next)) {
14581 internal_error(state, block->last->next,
14582 "cannot find next block");
14584 block = block->last->next->u.block;
14586 internal_error(state, block->last->next,
14589 } while(block != state->first_block);
14592 static void verify_domination(struct compile_state *state)
14594 struct triple *first, *ins;
14595 struct triple_set *set;
14596 if (!state->first_block) {
14600 first = RHS(state->main_function, 0);
14603 for(set = ins->use; set; set = set->next) {
14604 struct triple **expr;
14605 if (set->member->op == OP_PHI) {
14608 /* See if the use is on the righ hand side */
14609 expr = triple_rhs(state, set->member, 0);
14610 for(; expr ; expr = triple_rhs(state, set->member, expr)) {
14611 if (*expr == ins) {
14616 !tdominates(state, ins, set->member)) {
14617 internal_error(state, set->member,
14618 "non dominated rhs use?");
14622 } while(ins != first);
14625 static void verify_piece(struct compile_state *state)
14627 struct triple *first, *ins;
14628 first = RHS(state->main_function, 0);
14631 struct triple *ptr;
14633 lhs = TRIPLE_LHS(ins->sizes);
14634 if ((ins->op == OP_WRITE) || (ins->op == OP_STORE)) {
14637 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
14638 if (ptr != LHS(ins, i)) {
14639 internal_error(state, ins, "malformed lhs on %s",
14642 if (ptr->op != OP_PIECE) {
14643 internal_error(state, ins, "bad lhs op %s at %d on %s",
14644 tops(ptr->op), i, tops(ins->op));
14646 if (ptr->u.cval != i) {
14647 internal_error(state, ins, "bad u.cval of %d %d expected",
14652 } while(ins != first);
14654 static void verify_ins_colors(struct compile_state *state)
14656 struct triple *first, *ins;
14658 first = RHS(state->main_function, 0);
14662 } while(ins != first);
14664 static void verify_consistency(struct compile_state *state)
14666 verify_uses(state);
14667 verify_blocks(state);
14668 verify_domination(state);
14669 verify_piece(state);
14670 verify_ins_colors(state);
14673 static void verify_consistency(struct compile_state *state) {}
14674 #endif /* DEBUG_USES */
14676 static void optimize(struct compile_state *state)
14678 if (state->debug & DEBUG_TRIPLES) {
14679 print_triples(state);
14681 /* Replace structures with simpler data types */
14682 flatten_structures(state);
14683 if (state->debug & DEBUG_TRIPLES) {
14684 print_triples(state);
14686 verify_consistency(state);
14687 /* Analize the intermediate code */
14688 setup_basic_blocks(state);
14689 analyze_idominators(state);
14690 analyze_ipdominators(state);
14691 /* Transform the code to ssa form */
14692 transform_to_ssa_form(state);
14693 verify_consistency(state);
14694 if (state->debug & DEBUG_CODE_ELIMINATION) {
14695 fprintf(stdout, "After transform_to_ssa_form\n");
14696 print_blocks(state, stdout);
14698 /* Do strength reduction and simple constant optimizations */
14699 if (state->optimize >= 1) {
14700 simplify_all(state);
14702 verify_consistency(state);
14703 /* Propogate constants throughout the code */
14704 if (state->optimize >= 2) {
14705 #warning "FIXME fix scc_transform"
14706 scc_transform(state);
14707 transform_from_ssa_form(state);
14708 free_basic_blocks(state);
14709 setup_basic_blocks(state);
14710 analyze_idominators(state);
14711 analyze_ipdominators(state);
14712 transform_to_ssa_form(state);
14714 verify_consistency(state);
14715 #warning "WISHLIST implement single use constants (least possible register pressure)"
14716 #warning "WISHLIST implement induction variable elimination"
14717 /* Select architecture instructions and an initial partial
14718 * coloring based on architecture constraints.
14720 transform_to_arch_instructions(state);
14721 verify_consistency(state);
14722 if (state->debug & DEBUG_ARCH_CODE) {
14723 printf("After transform_to_arch_instructions\n");
14724 print_blocks(state, stdout);
14725 print_control_flow(state);
14727 eliminate_inefectual_code(state);
14728 verify_consistency(state);
14729 if (state->debug & DEBUG_CODE_ELIMINATION) {
14730 printf("After eliminate_inefectual_code\n");
14731 print_blocks(state, stdout);
14732 print_control_flow(state);
14734 verify_consistency(state);
14735 /* Color all of the variables to see if they will fit in registers */
14736 insert_copies_to_phi(state);
14737 if (state->debug & DEBUG_INSERTED_COPIES) {
14738 printf("After insert_copies_to_phi\n");
14739 print_blocks(state, stdout);
14740 print_control_flow(state);
14742 verify_consistency(state);
14743 insert_mandatory_copies(state);
14744 if (state->debug & DEBUG_INSERTED_COPIES) {
14745 printf("After insert_mandatory_copies\n");
14746 print_blocks(state, stdout);
14747 print_control_flow(state);
14749 verify_consistency(state);
14750 allocate_registers(state);
14751 verify_consistency(state);
14752 if (state->debug & DEBUG_INTERMEDIATE_CODE) {
14753 print_blocks(state, stdout);
14755 if (state->debug & DEBUG_CONTROL_FLOW) {
14756 print_control_flow(state);
14758 /* Remove the optimization information.
14759 * This is more to check for memory consistency than to free memory.
14761 free_basic_blocks(state);
14764 static void print_op_asm(struct compile_state *state,
14765 struct triple *ins, FILE *fp)
14767 struct asm_info *info;
14769 unsigned lhs, rhs, i;
14770 info = ins->u.ainfo;
14771 lhs = TRIPLE_LHS(ins->sizes);
14772 rhs = TRIPLE_RHS(ins->sizes);
14773 /* Don't count the clobbers in lhs */
14774 for(i = 0; i < lhs; i++) {
14775 if (LHS(ins, i)->type == &void_type) {
14780 fprintf(fp, "#ASM\n");
14782 for(ptr = info->str; *ptr; ptr++) {
14784 unsigned long param;
14785 struct triple *piece;
14795 param = strtoul(ptr, &next, 10);
14797 error(state, ins, "Invalid asm template");
14799 if (param >= (lhs + rhs)) {
14800 error(state, ins, "Invalid param %%%u in asm template",
14803 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
14805 arch_reg_str(ID_REG(piece->id)));
14808 fprintf(fp, "\n#NOT ASM\n");
14812 /* Only use the low x86 byte registers. This allows me
14813 * allocate the entire register when a byte register is used.
14815 #define X86_4_8BIT_GPRS 1
14817 /* Recognized x86 cpu variants */
14825 #define CPU_DEFAULT CPU_I386
14827 /* The x86 register classes */
14828 #define REGC_FLAGS 0
14829 #define REGC_GPR8 1
14830 #define REGC_GPR16 2
14831 #define REGC_GPR32 3
14832 #define REGC_GPR64 4
14835 #define REGC_GPR32_8 7
14836 #define REGC_GPR16_8 8
14837 #define REGC_IMM32 9
14838 #define REGC_IMM16 10
14839 #define REGC_IMM8 11
14840 #define LAST_REGC REGC_IMM8
14841 #if LAST_REGC >= MAX_REGC
14842 #error "MAX_REGC is to low"
14845 /* Register class masks */
14846 #define REGCM_FLAGS (1 << REGC_FLAGS)
14847 #define REGCM_GPR8 (1 << REGC_GPR8)
14848 #define REGCM_GPR16 (1 << REGC_GPR16)
14849 #define REGCM_GPR32 (1 << REGC_GPR32)
14850 #define REGCM_GPR64 (1 << REGC_GPR64)
14851 #define REGCM_MMX (1 << REGC_MMX)
14852 #define REGCM_XMM (1 << REGC_XMM)
14853 #define REGCM_GPR32_8 (1 << REGC_GPR32_8)
14854 #define REGCM_GPR16_8 (1 << REGC_GPR16_8)
14855 #define REGCM_IMM32 (1 << REGC_IMM32)
14856 #define REGCM_IMM16 (1 << REGC_IMM16)
14857 #define REGCM_IMM8 (1 << REGC_IMM8)
14858 #define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
14860 /* The x86 registers */
14861 #define REG_EFLAGS 2
14862 #define REGC_FLAGS_FIRST REG_EFLAGS
14863 #define REGC_FLAGS_LAST REG_EFLAGS
14872 #define REGC_GPR8_FIRST REG_AL
14873 #if X86_4_8BIT_GPRS
14874 #define REGC_GPR8_LAST REG_DL
14876 #define REGC_GPR8_LAST REG_DH
14886 #define REGC_GPR16_FIRST REG_AX
14887 #define REGC_GPR16_LAST REG_SP
14896 #define REGC_GPR32_FIRST REG_EAX
14897 #define REGC_GPR32_LAST REG_ESP
14898 #define REG_EDXEAX 27
14899 #define REGC_GPR64_FIRST REG_EDXEAX
14900 #define REGC_GPR64_LAST REG_EDXEAX
14901 #define REG_MMX0 28
14902 #define REG_MMX1 29
14903 #define REG_MMX2 30
14904 #define REG_MMX3 31
14905 #define REG_MMX4 32
14906 #define REG_MMX5 33
14907 #define REG_MMX6 34
14908 #define REG_MMX7 35
14909 #define REGC_MMX_FIRST REG_MMX0
14910 #define REGC_MMX_LAST REG_MMX7
14911 #define REG_XMM0 36
14912 #define REG_XMM1 37
14913 #define REG_XMM2 38
14914 #define REG_XMM3 39
14915 #define REG_XMM4 40
14916 #define REG_XMM5 41
14917 #define REG_XMM6 42
14918 #define REG_XMM7 43
14919 #define REGC_XMM_FIRST REG_XMM0
14920 #define REGC_XMM_LAST REG_XMM7
14921 #warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
14922 #define LAST_REG REG_XMM7
14924 #define REGC_GPR32_8_FIRST REG_EAX
14925 #define REGC_GPR32_8_LAST REG_EDX
14926 #define REGC_GPR16_8_FIRST REG_AX
14927 #define REGC_GPR16_8_LAST REG_DX
14929 #define REGC_IMM8_FIRST -1
14930 #define REGC_IMM8_LAST -1
14931 #define REGC_IMM16_FIRST -2
14932 #define REGC_IMM16_LAST -1
14933 #define REGC_IMM32_FIRST -4
14934 #define REGC_IMM32_LAST -1
14936 #if LAST_REG >= MAX_REGISTERS
14937 #error "MAX_REGISTERS to low"
14941 static unsigned regc_size[LAST_REGC +1] = {
14942 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
14943 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
14944 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
14945 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
14946 [REGC_GPR64] = REGC_GPR64_LAST - REGC_GPR64_FIRST + 1,
14947 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
14948 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
14949 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
14950 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
14956 static const struct {
14958 } regcm_bound[LAST_REGC + 1] = {
14959 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
14960 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
14961 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
14962 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
14963 [REGC_GPR64] = { REGC_GPR64_FIRST, REGC_GPR64_LAST },
14964 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
14965 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
14966 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
14967 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
14968 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
14969 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
14970 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
14973 static int arch_encode_cpu(const char *cpu)
14979 { "i386", CPU_I386 },
14987 for(ptr = cpus; ptr->name; ptr++) {
14988 if (strcmp(ptr->name, cpu) == 0) {
14995 static unsigned arch_regc_size(struct compile_state *state, int class)
14997 if ((class < 0) || (class > LAST_REGC)) {
15000 return regc_size[class];
15002 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
15004 /* See if two register classes may have overlapping registers */
15005 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 |
15006 REGCM_GPR32_8 | REGCM_GPR32 | REGCM_GPR64;
15008 /* Special case for the immediates */
15009 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
15010 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
15011 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
15012 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
15015 return (regcm1 & regcm2) ||
15016 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
15019 static void arch_reg_equivs(
15020 struct compile_state *state, unsigned *equiv, int reg)
15022 if ((reg < 0) || (reg > LAST_REG)) {
15023 internal_error(state, 0, "invalid register");
15028 #if X86_4_8BIT_GPRS
15032 *equiv++ = REG_EAX;
15033 *equiv++ = REG_EDXEAX;
15036 #if X86_4_8BIT_GPRS
15040 *equiv++ = REG_EAX;
15041 *equiv++ = REG_EDXEAX;
15044 #if X86_4_8BIT_GPRS
15048 *equiv++ = REG_EBX;
15052 #if X86_4_8BIT_GPRS
15056 *equiv++ = REG_EBX;
15059 #if X86_4_8BIT_GPRS
15063 *equiv++ = REG_ECX;
15067 #if X86_4_8BIT_GPRS
15071 *equiv++ = REG_ECX;
15074 #if X86_4_8BIT_GPRS
15078 *equiv++ = REG_EDX;
15079 *equiv++ = REG_EDXEAX;
15082 #if X86_4_8BIT_GPRS
15086 *equiv++ = REG_EDX;
15087 *equiv++ = REG_EDXEAX;
15092 *equiv++ = REG_EAX;
15093 *equiv++ = REG_EDXEAX;
15098 *equiv++ = REG_EBX;
15103 *equiv++ = REG_ECX;
15108 *equiv++ = REG_EDX;
15109 *equiv++ = REG_EDXEAX;
15112 *equiv++ = REG_ESI;
15115 *equiv++ = REG_EDI;
15118 *equiv++ = REG_EBP;
15121 *equiv++ = REG_ESP;
15127 *equiv++ = REG_EDXEAX;
15143 *equiv++ = REG_EDXEAX;
15164 *equiv++ = REG_EAX;
15165 *equiv++ = REG_EDX;
15168 *equiv++ = REG_UNSET;
15171 static unsigned arch_avail_mask(struct compile_state *state)
15173 unsigned avail_mask;
15174 avail_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 |
15175 REGCM_GPR32 | REGCM_GPR32_8 | REGCM_GPR64 |
15176 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
15177 switch(state->cpu) {
15180 avail_mask |= REGCM_MMX;
15184 avail_mask |= REGCM_MMX | REGCM_XMM;
15188 /* Don't enable 8 bit values until I can force both operands
15189 * to be 8bits simultaneously.
15191 avail_mask &= ~(REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16);
15196 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
15198 unsigned mask, result;
15201 result &= arch_avail_mask(state);
15203 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
15204 if ((result & mask) == 0) {
15207 if (class > LAST_REGC) {
15210 for(class2 = 0; class2 <= LAST_REGC; class2++) {
15211 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
15212 (regcm_bound[class2].last <= regcm_bound[class].last)) {
15213 result |= (1 << class2);
15220 static unsigned arch_reg_regcm(struct compile_state *state, int reg)
15225 for(class = 0; class <= LAST_REGC; class++) {
15226 if ((reg >= regcm_bound[class].first) &&
15227 (reg <= regcm_bound[class].last)) {
15228 mask |= (1 << class);
15232 internal_error(state, 0, "reg %d not in any class", reg);
15237 static struct reg_info arch_reg_constraint(
15238 struct compile_state *state, struct type *type, const char *constraint)
15240 static const struct {
15244 } constraints[] = {
15245 { 'r', REGCM_GPR32, REG_UNSET },
15246 { 'g', REGCM_GPR32, REG_UNSET },
15247 { 'p', REGCM_GPR32, REG_UNSET },
15248 { 'q', REGCM_GPR8, REG_UNSET },
15249 { 'Q', REGCM_GPR32_8, REG_UNSET },
15250 { 'x', REGCM_XMM, REG_UNSET },
15251 { 'y', REGCM_MMX, REG_UNSET },
15252 { 'a', REGCM_GPR32, REG_EAX },
15253 { 'b', REGCM_GPR32, REG_EBX },
15254 { 'c', REGCM_GPR32, REG_ECX },
15255 { 'd', REGCM_GPR32, REG_EDX },
15256 { 'D', REGCM_GPR32, REG_EDI },
15257 { 'S', REGCM_GPR32, REG_ESI },
15258 { '\0', 0, REG_UNSET },
15260 unsigned int regcm;
15261 unsigned int mask, reg;
15262 struct reg_info result;
15264 regcm = arch_type_to_regcm(state, type);
15267 for(ptr = constraint; *ptr; ptr++) {
15272 for(i = 0; constraints[i].class != '\0'; i++) {
15273 if (constraints[i].class == *ptr) {
15277 if (constraints[i].class == '\0') {
15278 error(state, 0, "invalid register constraint ``%c''", *ptr);
15281 if ((constraints[i].mask & regcm) == 0) {
15282 error(state, 0, "invalid register class %c specified",
15285 mask |= constraints[i].mask;
15286 if (constraints[i].reg != REG_UNSET) {
15287 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
15288 error(state, 0, "Only one register may be specified");
15290 reg = constraints[i].reg;
15294 result.regcm = mask;
15298 static struct reg_info arch_reg_clobber(
15299 struct compile_state *state, const char *clobber)
15301 struct reg_info result;
15302 if (strcmp(clobber, "memory") == 0) {
15303 result.reg = REG_UNSET;
15306 else if (strcmp(clobber, "%eax") == 0) {
15307 result.reg = REG_EAX;
15308 result.regcm = REGCM_GPR32;
15310 else if (strcmp(clobber, "%ebx") == 0) {
15311 result.reg = REG_EBX;
15312 result.regcm = REGCM_GPR32;
15314 else if (strcmp(clobber, "%ecx") == 0) {
15315 result.reg = REG_ECX;
15316 result.regcm = REGCM_GPR32;
15318 else if (strcmp(clobber, "%edx") == 0) {
15319 result.reg = REG_EDX;
15320 result.regcm = REGCM_GPR32;
15322 else if (strcmp(clobber, "%esi") == 0) {
15323 result.reg = REG_ESI;
15324 result.regcm = REGCM_GPR32;
15326 else if (strcmp(clobber, "%edi") == 0) {
15327 result.reg = REG_EDI;
15328 result.regcm = REGCM_GPR32;
15330 else if (strcmp(clobber, "%ebp") == 0) {
15331 result.reg = REG_EBP;
15332 result.regcm = REGCM_GPR32;
15334 else if (strcmp(clobber, "%esp") == 0) {
15335 result.reg = REG_ESP;
15336 result.regcm = REGCM_GPR32;
15338 else if (strcmp(clobber, "cc") == 0) {
15339 result.reg = REG_EFLAGS;
15340 result.regcm = REGCM_FLAGS;
15342 else if ((strncmp(clobber, "xmm", 3) == 0) &&
15343 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15344 result.reg = REG_XMM0 + octdigval(clobber[3]);
15345 result.regcm = REGCM_XMM;
15347 else if ((strncmp(clobber, "mmx", 3) == 0) &&
15348 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15349 result.reg = REG_MMX0 + octdigval(clobber[3]);
15350 result.regcm = REGCM_MMX;
15353 error(state, 0, "Invalid register clobber");
15354 result.reg = REG_UNSET;
15360 static int do_select_reg(struct compile_state *state,
15361 char *used, int reg, unsigned classes)
15367 mask = arch_reg_regcm(state, reg);
15368 return (classes & mask) ? reg : REG_UNSET;
15371 static int arch_select_free_register(
15372 struct compile_state *state, char *used, int classes)
15374 /* Preference: flags, 8bit gprs, 32bit gprs, other 32bit reg
15375 * other types of registers.
15379 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
15380 reg = do_select_reg(state, used, i, classes);
15382 for(i = REGC_GPR32_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR32_LAST); i++) {
15383 reg = do_select_reg(state, used, i, classes);
15385 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
15386 reg = do_select_reg(state, used, i, classes);
15388 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
15389 reg = do_select_reg(state, used, i, classes);
15391 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
15392 reg = do_select_reg(state, used, i, classes);
15394 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
15395 reg = do_select_reg(state, used, i, classes);
15397 for(i = REGC_GPR64_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR64_LAST); i++) {
15398 reg = do_select_reg(state, used, i, classes);
15404 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
15406 #warning "FIXME force types smaller (if legal) before I get here"
15407 unsigned avail_mask;
15410 avail_mask = arch_avail_mask(state);
15411 switch(type->type & TYPE_MASK) {
15418 mask = REGCM_GPR8 |
15419 REGCM_GPR16 | REGCM_GPR16_8 |
15420 REGCM_GPR32 | REGCM_GPR32_8 |
15422 REGCM_MMX | REGCM_XMM |
15423 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
15427 mask = REGCM_GPR16 | REGCM_GPR16_8 |
15428 REGCM_GPR32 | REGCM_GPR32_8 |
15430 REGCM_MMX | REGCM_XMM |
15431 REGCM_IMM32 | REGCM_IMM16;
15438 mask = REGCM_GPR32 | REGCM_GPR32_8 |
15439 REGCM_GPR64 | REGCM_MMX | REGCM_XMM |
15443 internal_error(state, 0, "no register class for type");
15446 mask &= avail_mask;
15450 static int is_imm32(struct triple *imm)
15452 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
15453 (imm->op == OP_ADDRCONST);
15456 static int is_imm16(struct triple *imm)
15458 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
15460 static int is_imm8(struct triple *imm)
15462 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
15465 static int get_imm32(struct triple *ins, struct triple **expr)
15467 struct triple *imm;
15469 while(imm->op == OP_COPY) {
15472 if (!is_imm32(imm)) {
15475 unuse_triple(*expr, ins);
15476 use_triple(imm, ins);
15481 static int get_imm8(struct triple *ins, struct triple **expr)
15483 struct triple *imm;
15485 while(imm->op == OP_COPY) {
15488 if (!is_imm8(imm)) {
15491 unuse_triple(*expr, ins);
15492 use_triple(imm, ins);
15497 #define TEMPLATE_NOP 0
15498 #define TEMPLATE_INTCONST8 1
15499 #define TEMPLATE_INTCONST32 2
15500 #define TEMPLATE_COPY_REG 3
15501 #define TEMPLATE_COPY_IMM32 4
15502 #define TEMPLATE_COPY_IMM16 5
15503 #define TEMPLATE_COPY_IMM8 6
15504 #define TEMPLATE_PHI 7
15505 #define TEMPLATE_STORE8 8
15506 #define TEMPLATE_STORE16 9
15507 #define TEMPLATE_STORE32 10
15508 #define TEMPLATE_LOAD8 11
15509 #define TEMPLATE_LOAD16 12
15510 #define TEMPLATE_LOAD32 13
15511 #define TEMPLATE_BINARY_REG 14
15512 #define TEMPLATE_BINARY_IMM 15
15513 #define TEMPLATE_SL_CL 16
15514 #define TEMPLATE_SL_IMM 17
15515 #define TEMPLATE_UNARY 18
15516 #define TEMPLATE_CMP_REG 19
15517 #define TEMPLATE_CMP_IMM 20
15518 #define TEMPLATE_TEST 21
15519 #define TEMPLATE_SET 22
15520 #define TEMPLATE_JMP 23
15521 #define TEMPLATE_INB_DX 24
15522 #define TEMPLATE_INB_IMM 25
15523 #define TEMPLATE_INW_DX 26
15524 #define TEMPLATE_INW_IMM 27
15525 #define TEMPLATE_INL_DX 28
15526 #define TEMPLATE_INL_IMM 29
15527 #define TEMPLATE_OUTB_DX 30
15528 #define TEMPLATE_OUTB_IMM 31
15529 #define TEMPLATE_OUTW_DX 32
15530 #define TEMPLATE_OUTW_IMM 33
15531 #define TEMPLATE_OUTL_DX 34
15532 #define TEMPLATE_OUTL_IMM 35
15533 #define TEMPLATE_BSF 36
15534 #define TEMPLATE_RDMSR 37
15535 #define TEMPLATE_WRMSR 38
15536 #define LAST_TEMPLATE TEMPLATE_WRMSR
15537 #if LAST_TEMPLATE >= MAX_TEMPLATES
15538 #error "MAX_TEMPLATES to low"
15541 #define COPY_REGCM (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8 | REGCM_MMX | REGCM_XMM)
15542 #define COPY32_REGCM (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
15544 static struct ins_template templates[] = {
15545 [TEMPLATE_NOP] = {},
15546 [TEMPLATE_INTCONST8] = {
15547 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15549 [TEMPLATE_INTCONST32] = {
15550 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
15552 [TEMPLATE_COPY_REG] = {
15553 .lhs = { [0] = { REG_UNSET, COPY_REGCM } },
15554 .rhs = { [0] = { REG_UNSET, COPY_REGCM } },
15556 [TEMPLATE_COPY_IMM32] = {
15557 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
15558 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
15560 [TEMPLATE_COPY_IMM16] = {
15561 .lhs = { [0] = { REG_UNSET, COPY32_REGCM | REGCM_GPR16 } },
15562 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 } },
15564 [TEMPLATE_COPY_IMM8] = {
15565 .lhs = { [0] = { REG_UNSET, COPY_REGCM } },
15566 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15569 .lhs = { [0] = { REG_VIRT0, COPY_REGCM } },
15571 [ 0] = { REG_VIRT0, COPY_REGCM },
15572 [ 1] = { REG_VIRT0, COPY_REGCM },
15573 [ 2] = { REG_VIRT0, COPY_REGCM },
15574 [ 3] = { REG_VIRT0, COPY_REGCM },
15575 [ 4] = { REG_VIRT0, COPY_REGCM },
15576 [ 5] = { REG_VIRT0, COPY_REGCM },
15577 [ 6] = { REG_VIRT0, COPY_REGCM },
15578 [ 7] = { REG_VIRT0, COPY_REGCM },
15579 [ 8] = { REG_VIRT0, COPY_REGCM },
15580 [ 9] = { REG_VIRT0, COPY_REGCM },
15581 [10] = { REG_VIRT0, COPY_REGCM },
15582 [11] = { REG_VIRT0, COPY_REGCM },
15583 [12] = { REG_VIRT0, COPY_REGCM },
15584 [13] = { REG_VIRT0, COPY_REGCM },
15585 [14] = { REG_VIRT0, COPY_REGCM },
15586 [15] = { REG_VIRT0, COPY_REGCM },
15588 [TEMPLATE_STORE8] = {
15589 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15590 .rhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15592 [TEMPLATE_STORE16] = {
15593 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15594 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
15596 [TEMPLATE_STORE32] = {
15597 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15598 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15600 [TEMPLATE_LOAD8] = {
15601 .lhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15602 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15604 [TEMPLATE_LOAD16] = {
15605 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
15606 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15608 [TEMPLATE_LOAD32] = {
15609 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15610 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15612 [TEMPLATE_BINARY_REG] = {
15613 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15615 [0] = { REG_VIRT0, REGCM_GPR32 },
15616 [1] = { REG_UNSET, REGCM_GPR32 },
15619 [TEMPLATE_BINARY_IMM] = {
15620 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15622 [0] = { REG_VIRT0, REGCM_GPR32 },
15623 [1] = { REG_UNNEEDED, REGCM_IMM32 },
15626 [TEMPLATE_SL_CL] = {
15627 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15629 [0] = { REG_VIRT0, REGCM_GPR32 },
15630 [1] = { REG_CL, REGCM_GPR8 },
15633 [TEMPLATE_SL_IMM] = {
15634 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15636 [0] = { REG_VIRT0, REGCM_GPR32 },
15637 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15640 [TEMPLATE_UNARY] = {
15641 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15642 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15644 [TEMPLATE_CMP_REG] = {
15645 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15647 [0] = { REG_UNSET, REGCM_GPR32 },
15648 [1] = { REG_UNSET, REGCM_GPR32 },
15651 [TEMPLATE_CMP_IMM] = {
15652 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15654 [0] = { REG_UNSET, REGCM_GPR32 },
15655 [1] = { REG_UNNEEDED, REGCM_IMM32 },
15658 [TEMPLATE_TEST] = {
15659 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15660 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15663 .lhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15664 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15667 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15669 [TEMPLATE_INB_DX] = {
15670 .lhs = { [0] = { REG_AL, REGCM_GPR8 } },
15671 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15673 [TEMPLATE_INB_IMM] = {
15674 .lhs = { [0] = { REG_AL, REGCM_GPR8 } },
15675 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15677 [TEMPLATE_INW_DX] = {
15678 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
15679 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15681 [TEMPLATE_INW_IMM] = {
15682 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
15683 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15685 [TEMPLATE_INL_DX] = {
15686 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
15687 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15689 [TEMPLATE_INL_IMM] = {
15690 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
15691 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15693 [TEMPLATE_OUTB_DX] = {
15695 [0] = { REG_AL, REGCM_GPR8 },
15696 [1] = { REG_DX, REGCM_GPR16 },
15699 [TEMPLATE_OUTB_IMM] = {
15701 [0] = { REG_AL, REGCM_GPR8 },
15702 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15705 [TEMPLATE_OUTW_DX] = {
15707 [0] = { REG_AX, REGCM_GPR16 },
15708 [1] = { REG_DX, REGCM_GPR16 },
15711 [TEMPLATE_OUTW_IMM] = {
15713 [0] = { REG_AX, REGCM_GPR16 },
15714 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15717 [TEMPLATE_OUTL_DX] = {
15719 [0] = { REG_EAX, REGCM_GPR32 },
15720 [1] = { REG_DX, REGCM_GPR16 },
15723 [TEMPLATE_OUTL_IMM] = {
15725 [0] = { REG_EAX, REGCM_GPR32 },
15726 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15730 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15731 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15733 [TEMPLATE_RDMSR] = {
15735 [0] = { REG_EAX, REGCM_GPR32 },
15736 [1] = { REG_EDX, REGCM_GPR32 },
15738 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
15740 [TEMPLATE_WRMSR] = {
15742 [0] = { REG_ECX, REGCM_GPR32 },
15743 [1] = { REG_EAX, REGCM_GPR32 },
15744 [2] = { REG_EDX, REGCM_GPR32 },
15749 static void fixup_branches(struct compile_state *state,
15750 struct triple *cmp, struct triple *use, int jmp_op)
15752 struct triple_set *entry, *next;
15753 for(entry = use->use; entry; entry = next) {
15754 next = entry->next;
15755 if (entry->member->op == OP_COPY) {
15756 fixup_branches(state, cmp, entry->member, jmp_op);
15758 else if (entry->member->op == OP_BRANCH) {
15759 struct triple *branch, *test;
15760 struct triple *left, *right;
15762 left = RHS(cmp, 0);
15763 if (TRIPLE_RHS(cmp->sizes) > 1) {
15764 right = RHS(cmp, 1);
15766 branch = entry->member;
15767 test = pre_triple(state, branch,
15768 cmp->op, cmp->type, left, right);
15769 test->template_id = TEMPLATE_TEST;
15770 if (cmp->op == OP_CMP) {
15771 test->template_id = TEMPLATE_CMP_REG;
15772 if (get_imm32(test, &RHS(test, 1))) {
15773 test->template_id = TEMPLATE_CMP_IMM;
15776 use_triple(RHS(test, 0), test);
15777 use_triple(RHS(test, 1), test);
15778 unuse_triple(RHS(branch, 0), branch);
15779 RHS(branch, 0) = test;
15780 branch->op = jmp_op;
15781 branch->template_id = TEMPLATE_JMP;
15782 use_triple(RHS(branch, 0), branch);
15787 static void bool_cmp(struct compile_state *state,
15788 struct triple *ins, int cmp_op, int jmp_op, int set_op)
15790 struct triple_set *entry, *next;
15791 struct triple *set;
15793 /* Put a barrier up before the cmp which preceeds the
15794 * copy instruction. If a set actually occurs this gives
15795 * us a chance to move variables in registers out of the way.
15798 /* Modify the comparison operator */
15800 ins->template_id = TEMPLATE_TEST;
15801 if (cmp_op == OP_CMP) {
15802 ins->template_id = TEMPLATE_CMP_REG;
15803 if (get_imm32(ins, &RHS(ins, 1))) {
15804 ins->template_id = TEMPLATE_CMP_IMM;
15807 /* Generate the instruction sequence that will transform the
15808 * result of the comparison into a logical value.
15810 set = post_triple(state, ins, set_op, ins->type, ins, 0);
15811 use_triple(ins, set);
15812 set->template_id = TEMPLATE_SET;
15814 for(entry = ins->use; entry; entry = next) {
15815 next = entry->next;
15816 if (entry->member == set) {
15819 replace_rhs_use(state, ins, set, entry->member);
15821 fixup_branches(state, ins, set, jmp_op);
15824 static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
15826 struct triple *next;
15828 lhs = TRIPLE_LHS(ins->sizes);
15829 for(next = ins->next, i = 0; i < lhs; i++, next = next->next) {
15830 if (next != LHS(ins, i)) {
15831 internal_error(state, ins, "malformed lhs on %s",
15834 if (next->op != OP_PIECE) {
15835 internal_error(state, ins, "bad lhs op %s at %d on %s",
15836 tops(next->op), i, tops(ins->op));
15838 if (next->u.cval != i) {
15839 internal_error(state, ins, "bad u.cval of %d %d expected",
15846 struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
15848 struct ins_template *template;
15849 struct reg_info result;
15851 if (ins->op == OP_PIECE) {
15852 index = ins->u.cval;
15853 ins = MISC(ins, 0);
15855 zlhs = TRIPLE_LHS(ins->sizes);
15856 if (triple_is_def(state, ins)) {
15859 if (index >= zlhs) {
15860 internal_error(state, ins, "index %d out of range for %s\n",
15861 index, tops(ins->op));
15865 template = &ins->u.ainfo->tmpl;
15868 if (ins->template_id > LAST_TEMPLATE) {
15869 internal_error(state, ins, "bad template number %d",
15872 template = &templates[ins->template_id];
15875 result = template->lhs[index];
15876 result.regcm = arch_regcm_normalize(state, result.regcm);
15877 if (result.reg != REG_UNNEEDED) {
15878 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
15880 if (result.regcm == 0) {
15881 internal_error(state, ins, "lhs %d regcm == 0", index);
15886 struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
15888 struct reg_info result;
15889 struct ins_template *template;
15890 if ((index > TRIPLE_RHS(ins->sizes)) ||
15891 (ins->op == OP_PIECE)) {
15892 internal_error(state, ins, "index %d out of range for %s\n",
15893 index, tops(ins->op));
15897 template = &ins->u.ainfo->tmpl;
15900 if (ins->template_id > LAST_TEMPLATE) {
15901 internal_error(state, ins, "bad template number %d",
15904 template = &templates[ins->template_id];
15907 result = template->rhs[index];
15908 result.regcm = arch_regcm_normalize(state, result.regcm);
15909 if (result.regcm == 0) {
15910 internal_error(state, ins, "rhs %d regcm == 0", index);
15915 static struct triple *transform_to_arch_instruction(
15916 struct compile_state *state, struct triple *ins)
15918 /* Transform from generic 3 address instructions
15919 * to archtecture specific instructions.
15920 * And apply architecture specific constrains to instructions.
15921 * Copies are inserted to preserve the register flexibility
15922 * of 3 address instructions.
15924 struct triple *next;
15928 ins->template_id = TEMPLATE_INTCONST32;
15929 if (ins->u.cval < 256) {
15930 ins->template_id = TEMPLATE_INTCONST8;
15934 ins->template_id = TEMPLATE_INTCONST32;
15940 ins->template_id = TEMPLATE_NOP;
15943 ins->template_id = TEMPLATE_COPY_REG;
15944 if (is_imm8(RHS(ins, 0))) {
15945 ins->template_id = TEMPLATE_COPY_IMM8;
15947 else if (is_imm16(RHS(ins, 0))) {
15948 ins->template_id = TEMPLATE_COPY_IMM16;
15950 else if (is_imm32(RHS(ins, 0))) {
15951 ins->template_id = TEMPLATE_COPY_IMM32;
15953 else if (is_const(RHS(ins, 0))) {
15954 internal_error(state, ins, "bad constant passed to copy");
15958 ins->template_id = TEMPLATE_PHI;
15961 switch(ins->type->type & TYPE_MASK) {
15962 case TYPE_CHAR: case TYPE_UCHAR:
15963 ins->template_id = TEMPLATE_STORE8;
15965 case TYPE_SHORT: case TYPE_USHORT:
15966 ins->template_id = TEMPLATE_STORE16;
15968 case TYPE_INT: case TYPE_UINT:
15969 case TYPE_LONG: case TYPE_ULONG:
15971 ins->template_id = TEMPLATE_STORE32;
15974 internal_error(state, ins, "unknown type in store");
15979 switch(ins->type->type & TYPE_MASK) {
15980 case TYPE_CHAR: case TYPE_UCHAR:
15981 ins->template_id = TEMPLATE_LOAD8;
15985 ins->template_id = TEMPLATE_LOAD16;
15992 ins->template_id = TEMPLATE_LOAD32;
15995 internal_error(state, ins, "unknown type in load");
16005 ins->template_id = TEMPLATE_BINARY_REG;
16006 if (get_imm32(ins, &RHS(ins, 1))) {
16007 ins->template_id = TEMPLATE_BINARY_IMM;
16013 ins->template_id = TEMPLATE_SL_CL;
16014 if (get_imm8(ins, &RHS(ins, 1))) {
16015 ins->template_id = TEMPLATE_SL_IMM;
16020 ins->template_id = TEMPLATE_UNARY;
16023 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
16026 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
16029 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
16032 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
16035 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
16038 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
16041 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
16044 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
16047 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
16050 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
16053 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
16056 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
16059 if (TRIPLE_RHS(ins->sizes) > 0) {
16060 internal_error(state, ins, "bad branch test");
16063 ins->template_id = TEMPLATE_NOP;
16069 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
16070 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
16071 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
16073 if (get_imm8(ins, &RHS(ins, 0))) {
16074 ins->template_id += 1;
16081 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
16082 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
16083 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
16085 if (get_imm8(ins, &RHS(ins, 1))) {
16086 ins->template_id += 1;
16091 ins->template_id = TEMPLATE_BSF;
16094 ins->template_id = TEMPLATE_RDMSR;
16095 next = after_lhs(state, ins);
16098 ins->template_id = TEMPLATE_WRMSR;
16101 ins->template_id = TEMPLATE_NOP;
16104 ins->template_id = TEMPLATE_NOP;
16105 next = after_lhs(state, ins);
16107 /* Already transformed instructions */
16109 ins->template_id = TEMPLATE_TEST;
16112 ins->template_id = TEMPLATE_CMP_REG;
16113 if (get_imm32(ins, &RHS(ins, 1))) {
16114 ins->template_id = TEMPLATE_CMP_IMM;
16117 case OP_JMP_EQ: case OP_JMP_NOTEQ:
16118 case OP_JMP_SLESS: case OP_JMP_ULESS:
16119 case OP_JMP_SMORE: case OP_JMP_UMORE:
16120 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
16121 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
16122 ins->template_id = TEMPLATE_JMP;
16124 case OP_SET_EQ: case OP_SET_NOTEQ:
16125 case OP_SET_SLESS: case OP_SET_ULESS:
16126 case OP_SET_SMORE: case OP_SET_UMORE:
16127 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
16128 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
16129 ins->template_id = TEMPLATE_SET;
16131 /* Unhandled instructions */
16134 internal_error(state, ins, "unhandled ins: %d %s\n",
16135 ins->op, tops(ins->op));
16141 static void generate_local_labels(struct compile_state *state)
16143 struct triple *first, *label;
16146 first = RHS(state->main_function, 0);
16149 if ((label->op == OP_LABEL) ||
16150 (label->op == OP_SDECL)) {
16152 label->u.cval = ++label_counter;
16158 label = label->next;
16159 } while(label != first);
16162 static int check_reg(struct compile_state *state,
16163 struct triple *triple, int classes)
16167 reg = ID_REG(triple->id);
16168 if (reg == REG_UNSET) {
16169 internal_error(state, triple, "register not set");
16171 mask = arch_reg_regcm(state, reg);
16172 if (!(classes & mask)) {
16173 internal_error(state, triple, "reg %d in wrong class",
16179 static const char *arch_reg_str(int reg)
16181 static const char *regs[] = {
16185 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
16186 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
16187 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
16189 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
16190 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
16191 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
16193 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
16200 static const char *reg(struct compile_state *state, struct triple *triple,
16204 reg = check_reg(state, triple, classes);
16205 return arch_reg_str(reg);
16208 const char *type_suffix(struct compile_state *state, struct type *type)
16210 const char *suffix;
16211 switch(size_of(state, type)) {
16212 case 1: suffix = "b"; break;
16213 case 2: suffix = "w"; break;
16214 case 4: suffix = "l"; break;
16216 internal_error(state, 0, "unknown suffix");
16223 static void print_const_val(
16224 struct compile_state *state, struct triple *ins, FILE *fp)
16228 fprintf(fp, " $%ld ",
16229 (long_t)(ins->u.cval));
16232 fprintf(fp, " $L%s%lu+%lu ",
16233 state->label_prefix,
16234 MISC(ins, 0)->u.cval,
16238 internal_error(state, ins, "unknown constant type");
16243 static void print_binary_op(struct compile_state *state,
16244 const char *op, struct triple *ins, FILE *fp)
16247 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16248 if (RHS(ins, 0)->id != ins->id) {
16249 internal_error(state, ins, "invalid register assignment");
16251 if (is_const(RHS(ins, 1))) {
16252 fprintf(fp, "\t%s ", op);
16253 print_const_val(state, RHS(ins, 1), fp);
16254 fprintf(fp, ", %s\n",
16255 reg(state, RHS(ins, 0), mask));
16258 unsigned lmask, rmask;
16260 lreg = check_reg(state, RHS(ins, 0), mask);
16261 rreg = check_reg(state, RHS(ins, 1), mask);
16262 lmask = arch_reg_regcm(state, lreg);
16263 rmask = arch_reg_regcm(state, rreg);
16264 mask = lmask & rmask;
16265 fprintf(fp, "\t%s %s, %s\n",
16267 reg(state, RHS(ins, 1), mask),
16268 reg(state, RHS(ins, 0), mask));
16271 static void print_unary_op(struct compile_state *state,
16272 const char *op, struct triple *ins, FILE *fp)
16275 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16276 fprintf(fp, "\t%s %s\n",
16278 reg(state, RHS(ins, 0), mask));
16281 static void print_op_shift(struct compile_state *state,
16282 const char *op, struct triple *ins, FILE *fp)
16285 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16286 if (RHS(ins, 0)->id != ins->id) {
16287 internal_error(state, ins, "invalid register assignment");
16289 if (is_const(RHS(ins, 1))) {
16290 fprintf(fp, "\t%s ", op);
16291 print_const_val(state, RHS(ins, 1), fp);
16292 fprintf(fp, ", %s\n",
16293 reg(state, RHS(ins, 0), mask));
16296 fprintf(fp, "\t%s %s, %s\n",
16298 reg(state, RHS(ins, 1), REGCM_GPR8),
16299 reg(state, RHS(ins, 0), mask));
16303 static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
16310 case OP_INB: op = "inb", mask = REGCM_GPR8; break;
16311 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
16312 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
16314 internal_error(state, ins, "not an in operation");
16318 dreg = check_reg(state, ins, mask);
16319 if (!reg_is_reg(state, dreg, REG_EAX)) {
16320 internal_error(state, ins, "dst != %%eax");
16322 if (is_const(RHS(ins, 0))) {
16323 fprintf(fp, "\t%s ", op);
16324 print_const_val(state, RHS(ins, 0), fp);
16325 fprintf(fp, ", %s\n",
16326 reg(state, ins, mask));
16330 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
16331 if (!reg_is_reg(state, addr_reg, REG_DX)) {
16332 internal_error(state, ins, "src != %%dx");
16334 fprintf(fp, "\t%s %s, %s\n",
16336 reg(state, RHS(ins, 0), REGCM_GPR16),
16337 reg(state, ins, mask));
16341 static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
16348 case OP_OUTB: op = "outb", mask = REGCM_GPR8; break;
16349 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
16350 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
16352 internal_error(state, ins, "not an out operation");
16356 lreg = check_reg(state, RHS(ins, 0), mask);
16357 if (!reg_is_reg(state, lreg, REG_EAX)) {
16358 internal_error(state, ins, "src != %%eax");
16360 if (is_const(RHS(ins, 1))) {
16361 fprintf(fp, "\t%s %s,",
16362 op, reg(state, RHS(ins, 0), mask));
16363 print_const_val(state, RHS(ins, 1), fp);
16368 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
16369 if (!reg_is_reg(state, addr_reg, REG_DX)) {
16370 internal_error(state, ins, "dst != %%dx");
16372 fprintf(fp, "\t%s %s, %s\n",
16374 reg(state, RHS(ins, 0), mask),
16375 reg(state, RHS(ins, 1), REGCM_GPR16));
16379 static void print_op_move(struct compile_state *state,
16380 struct triple *ins, FILE *fp)
16382 /* op_move is complex because there are many types
16383 * of registers we can move between.
16384 * Because OP_COPY will be introduced in arbitrary locations
16385 * OP_COPY must not affect flags.
16387 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
16388 struct triple *dst, *src;
16389 if (ins->op == OP_COPY) {
16393 else if (ins->op == OP_WRITE) {
16398 internal_error(state, ins, "unknown move operation");
16401 if (!is_const(src)) {
16402 int src_reg, dst_reg;
16403 int src_regcm, dst_regcm;
16404 src_reg = ID_REG(src->id);
16405 dst_reg = ID_REG(dst->id);
16406 src_regcm = arch_reg_regcm(state, src_reg);
16407 dst_regcm = arch_reg_regcm(state, dst_reg);
16408 /* If the class is the same just move the register */
16409 if (src_regcm & dst_regcm &
16410 (REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32)) {
16411 if ((src_reg != dst_reg) || !omit_copy) {
16412 fprintf(fp, "\tmov %s, %s\n",
16413 reg(state, src, src_regcm),
16414 reg(state, dst, dst_regcm));
16417 /* Move 32bit to 16bit */
16418 else if ((src_regcm & REGCM_GPR32) &&
16419 (dst_regcm & REGCM_GPR16)) {
16420 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
16421 if ((src_reg != dst_reg) || !omit_copy) {
16422 fprintf(fp, "\tmovw %s, %s\n",
16423 arch_reg_str(src_reg),
16424 arch_reg_str(dst_reg));
16427 /* Move 32bit to 8bit */
16428 else if ((src_regcm & REGCM_GPR32_8) &&
16429 (dst_regcm & REGCM_GPR8))
16431 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
16432 if ((src_reg != dst_reg) || !omit_copy) {
16433 fprintf(fp, "\tmovb %s, %s\n",
16434 arch_reg_str(src_reg),
16435 arch_reg_str(dst_reg));
16438 /* Move 16bit to 8bit */
16439 else if ((src_regcm & REGCM_GPR16_8) &&
16440 (dst_regcm & REGCM_GPR8))
16442 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
16443 if ((src_reg != dst_reg) || !omit_copy) {
16444 fprintf(fp, "\tmovb %s, %s\n",
16445 arch_reg_str(src_reg),
16446 arch_reg_str(dst_reg));
16449 /* Move 8/16bit to 16/32bit */
16450 else if ((src_regcm & (REGCM_GPR8 | REGCM_GPR16)) &&
16451 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
16453 op = is_signed(src->type)? "movsx": "movzx";
16454 fprintf(fp, "\t%s %s, %s\n",
16456 reg(state, src, src_regcm),
16457 reg(state, dst, dst_regcm));
16459 /* Move between sse registers */
16460 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
16461 if ((src_reg != dst_reg) || !omit_copy) {
16462 fprintf(fp, "\tmovdqa %s, %s\n",
16463 reg(state, src, src_regcm),
16464 reg(state, dst, dst_regcm));
16467 /* Move between mmx registers or mmx & sse registers */
16468 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
16469 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
16470 if ((src_reg != dst_reg) || !omit_copy) {
16471 fprintf(fp, "\tmovq %s, %s\n",
16472 reg(state, src, src_regcm),
16473 reg(state, dst, dst_regcm));
16476 /* Move between 32bit gprs & mmx/sse registers */
16477 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
16478 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
16479 fprintf(fp, "\tmovd %s, %s\n",
16480 reg(state, src, src_regcm),
16481 reg(state, dst, dst_regcm));
16483 #if X86_4_8BIT_GPRS
16484 /* Move from 8bit gprs to mmx/sse registers */
16485 else if ((src_regcm & REGCM_GPR8) && (src_reg <= REG_DL) &&
16486 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
16489 op = is_signed(src->type)? "movsx":"movzx";
16490 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16491 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
16493 reg(state, src, src_regcm),
16494 arch_reg_str(mid_reg),
16495 arch_reg_str(mid_reg),
16496 reg(state, dst, dst_regcm));
16498 /* Move from mmx/sse registers and 8bit gprs */
16499 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
16500 (dst_regcm & REGCM_GPR8) && (dst_reg <= REG_DL)) {
16502 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16503 fprintf(fp, "\tmovd %s, %s\n",
16504 reg(state, src, src_regcm),
16505 arch_reg_str(mid_reg));
16507 /* Move from 32bit gprs to 16bit gprs */
16508 else if ((src_regcm & REGCM_GPR32) &&
16509 (dst_regcm & REGCM_GPR16)) {
16510 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
16511 if ((src_reg != dst_reg) || !omit_copy) {
16512 fprintf(fp, "\tmov %s, %s\n",
16513 arch_reg_str(src_reg),
16514 arch_reg_str(dst_reg));
16517 /* Move from 32bit gprs to 8bit gprs */
16518 else if ((src_regcm & REGCM_GPR32) &&
16519 (dst_regcm & REGCM_GPR8)) {
16520 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16521 if ((src_reg != dst_reg) || !omit_copy) {
16522 fprintf(fp, "\tmov %s, %s\n",
16523 arch_reg_str(src_reg),
16524 arch_reg_str(dst_reg));
16527 /* Move from 16bit gprs to 8bit gprs */
16528 else if ((src_regcm & REGCM_GPR16) &&
16529 (dst_regcm & REGCM_GPR8)) {
16530 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
16531 if ((src_reg != dst_reg) || !omit_copy) {
16532 fprintf(fp, "\tmov %s, %s\n",
16533 arch_reg_str(src_reg),
16534 arch_reg_str(dst_reg));
16537 #endif /* X86_4_8BIT_GPRS */
16539 internal_error(state, ins, "unknown copy type");
16543 fprintf(fp, "\tmov ");
16544 print_const_val(state, src, fp);
16545 fprintf(fp, ", %s\n",
16546 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8));
16550 static void print_op_load(struct compile_state *state,
16551 struct triple *ins, FILE *fp)
16553 struct triple *dst, *src;
16556 if (is_const(src) || is_const(dst)) {
16557 internal_error(state, ins, "unknown load operation");
16559 fprintf(fp, "\tmov (%s), %s\n",
16560 reg(state, src, REGCM_GPR32),
16561 reg(state, dst, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32));
16565 static void print_op_store(struct compile_state *state,
16566 struct triple *ins, FILE *fp)
16568 struct triple *dst, *src;
16571 if (is_const(src) && (src->op == OP_INTCONST)) {
16573 value = (long_t)(src->u.cval);
16574 fprintf(fp, "\tmov%s $%ld, (%s)\n",
16575 type_suffix(state, src->type),
16577 reg(state, dst, REGCM_GPR32));
16579 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
16580 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
16581 type_suffix(state, src->type),
16582 reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
16586 if (is_const(src) || is_const(dst)) {
16587 internal_error(state, ins, "unknown store operation");
16589 fprintf(fp, "\tmov%s %s, (%s)\n",
16590 type_suffix(state, src->type),
16591 reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
16592 reg(state, dst, REGCM_GPR32));
16598 static void print_op_smul(struct compile_state *state,
16599 struct triple *ins, FILE *fp)
16601 if (!is_const(RHS(ins, 1))) {
16602 fprintf(fp, "\timul %s, %s\n",
16603 reg(state, RHS(ins, 1), REGCM_GPR32),
16604 reg(state, RHS(ins, 0), REGCM_GPR32));
16607 fprintf(fp, "\timul ");
16608 print_const_val(state, RHS(ins, 1), fp);
16609 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
16613 static void print_op_cmp(struct compile_state *state,
16614 struct triple *ins, FILE *fp)
16618 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16619 dreg = check_reg(state, ins, REGCM_FLAGS);
16620 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
16621 internal_error(state, ins, "bad dest register for cmp");
16623 if (is_const(RHS(ins, 1))) {
16624 fprintf(fp, "\tcmp ");
16625 print_const_val(state, RHS(ins, 1), fp);
16626 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
16629 unsigned lmask, rmask;
16631 lreg = check_reg(state, RHS(ins, 0), mask);
16632 rreg = check_reg(state, RHS(ins, 1), mask);
16633 lmask = arch_reg_regcm(state, lreg);
16634 rmask = arch_reg_regcm(state, rreg);
16635 mask = lmask & rmask;
16636 fprintf(fp, "\tcmp %s, %s\n",
16637 reg(state, RHS(ins, 1), mask),
16638 reg(state, RHS(ins, 0), mask));
16642 static void print_op_test(struct compile_state *state,
16643 struct triple *ins, FILE *fp)
16646 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16647 fprintf(fp, "\ttest %s, %s\n",
16648 reg(state, RHS(ins, 0), mask),
16649 reg(state, RHS(ins, 0), mask));
16652 static void print_op_branch(struct compile_state *state,
16653 struct triple *branch, FILE *fp)
16655 const char *bop = "j";
16656 if (branch->op == OP_JMP) {
16657 if (TRIPLE_RHS(branch->sizes) != 0) {
16658 internal_error(state, branch, "jmp with condition?");
16663 struct triple *ptr;
16664 if (TRIPLE_RHS(branch->sizes) != 1) {
16665 internal_error(state, branch, "jmpcc without condition?");
16667 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
16668 if ((RHS(branch, 0)->op != OP_CMP) &&
16669 (RHS(branch, 0)->op != OP_TEST)) {
16670 internal_error(state, branch, "bad branch test");
16672 #warning "FIXME I have observed instructions between the test and branch instructions"
16673 ptr = RHS(branch, 0);
16674 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
16675 if (ptr->op != OP_COPY) {
16676 internal_error(state, branch, "branch does not follow test");
16679 switch(branch->op) {
16680 case OP_JMP_EQ: bop = "jz"; break;
16681 case OP_JMP_NOTEQ: bop = "jnz"; break;
16682 case OP_JMP_SLESS: bop = "jl"; break;
16683 case OP_JMP_ULESS: bop = "jb"; break;
16684 case OP_JMP_SMORE: bop = "jg"; break;
16685 case OP_JMP_UMORE: bop = "ja"; break;
16686 case OP_JMP_SLESSEQ: bop = "jle"; break;
16687 case OP_JMP_ULESSEQ: bop = "jbe"; break;
16688 case OP_JMP_SMOREEQ: bop = "jge"; break;
16689 case OP_JMP_UMOREEQ: bop = "jae"; break;
16691 internal_error(state, branch, "Invalid branch op");
16696 fprintf(fp, "\t%s L%s%lu\n",
16698 state->label_prefix,
16699 TARG(branch, 0)->u.cval);
16702 static void print_op_set(struct compile_state *state,
16703 struct triple *set, FILE *fp)
16705 const char *sop = "set";
16706 if (TRIPLE_RHS(set->sizes) != 1) {
16707 internal_error(state, set, "setcc without condition?");
16709 check_reg(state, RHS(set, 0), REGCM_FLAGS);
16710 if ((RHS(set, 0)->op != OP_CMP) &&
16711 (RHS(set, 0)->op != OP_TEST)) {
16712 internal_error(state, set, "bad set test");
16714 if (RHS(set, 0)->next != set) {
16715 internal_error(state, set, "set does not follow test");
16718 case OP_SET_EQ: sop = "setz"; break;
16719 case OP_SET_NOTEQ: sop = "setnz"; break;
16720 case OP_SET_SLESS: sop = "setl"; break;
16721 case OP_SET_ULESS: sop = "setb"; break;
16722 case OP_SET_SMORE: sop = "setg"; break;
16723 case OP_SET_UMORE: sop = "seta"; break;
16724 case OP_SET_SLESSEQ: sop = "setle"; break;
16725 case OP_SET_ULESSEQ: sop = "setbe"; break;
16726 case OP_SET_SMOREEQ: sop = "setge"; break;
16727 case OP_SET_UMOREEQ: sop = "setae"; break;
16729 internal_error(state, set, "Invalid set op");
16732 fprintf(fp, "\t%s %s\n",
16733 sop, reg(state, set, REGCM_GPR8));
16736 static void print_op_bit_scan(struct compile_state *state,
16737 struct triple *ins, FILE *fp)
16741 case OP_BSF: op = "bsf"; break;
16742 case OP_BSR: op = "bsr"; break;
16744 internal_error(state, ins, "unknown bit scan");
16754 reg(state, RHS(ins, 0), REGCM_GPR32),
16755 reg(state, ins, REGCM_GPR32),
16756 reg(state, ins, REGCM_GPR32));
16759 static void print_const(struct compile_state *state,
16760 struct triple *ins, FILE *fp)
16764 switch(ins->type->type & TYPE_MASK) {
16767 fprintf(fp, ".byte 0x%02lx\n", ins->u.cval);
16771 fprintf(fp, ".short 0x%04lx\n", ins->u.cval);
16777 fprintf(fp, ".int %lu\n", ins->u.cval);
16780 internal_error(state, ins, "Unknown constant type");
16785 unsigned char *blob;
16787 size = size_of(state, ins->type);
16788 blob = ins->u.blob;
16789 for(i = 0; i < size; i++) {
16790 fprintf(fp, ".byte 0x%02x\n",
16796 internal_error(state, ins, "Unknown constant type");
16801 #define TEXT_SECTION ".rom.text"
16802 #define DATA_SECTION ".rom.data"
16804 static void print_sdecl(struct compile_state *state,
16805 struct triple *ins, FILE *fp)
16807 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
16808 fprintf(fp, ".balign %d\n", align_of(state, ins->type));
16809 fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
16810 print_const(state, MISC(ins, 0), fp);
16811 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
16815 static void print_instruction(struct compile_state *state,
16816 struct triple *ins, FILE *fp)
16818 /* Assumption: after I have exted the register allocator
16819 * everything is in a valid register.
16823 print_op_asm(state, ins, fp);
16825 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
16826 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
16827 case OP_AND: print_binary_op(state, "and", ins, fp); break;
16828 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
16829 case OP_OR: print_binary_op(state, "or", ins, fp); break;
16830 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
16831 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
16832 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
16833 case OP_POS: break;
16834 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
16835 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
16839 /* Don't generate anything here for constants */
16841 /* Don't generate anything for variable declarations. */
16844 print_sdecl(state, ins, fp);
16848 print_op_move(state, ins, fp);
16851 print_op_load(state, ins, fp);
16854 print_op_store(state, ins, fp);
16857 print_op_smul(state, ins, fp);
16859 case OP_CMP: print_op_cmp(state, ins, fp); break;
16860 case OP_TEST: print_op_test(state, ins, fp); break;
16862 case OP_JMP_EQ: case OP_JMP_NOTEQ:
16863 case OP_JMP_SLESS: case OP_JMP_ULESS:
16864 case OP_JMP_SMORE: case OP_JMP_UMORE:
16865 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
16866 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
16867 print_op_branch(state, ins, fp);
16869 case OP_SET_EQ: case OP_SET_NOTEQ:
16870 case OP_SET_SLESS: case OP_SET_ULESS:
16871 case OP_SET_SMORE: case OP_SET_UMORE:
16872 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
16873 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
16874 print_op_set(state, ins, fp);
16876 case OP_INB: case OP_INW: case OP_INL:
16877 print_op_in(state, ins, fp);
16879 case OP_OUTB: case OP_OUTW: case OP_OUTL:
16880 print_op_out(state, ins, fp);
16884 print_op_bit_scan(state, ins, fp);
16887 after_lhs(state, ins);
16888 fprintf(fp, "\trdmsr\n");
16891 fprintf(fp, "\twrmsr\n");
16894 fprintf(fp, "\thlt\n");
16900 fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
16902 /* Ignore OP_PIECE */
16905 /* Operations I am not yet certain how to handle */
16907 case OP_SDIV: case OP_UDIV:
16908 case OP_SMOD: case OP_UMOD:
16909 /* Operations that should never get here */
16910 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
16911 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
16912 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
16914 internal_error(state, ins, "unknown op: %d %s",
16915 ins->op, tops(ins->op));
16920 static void print_instructions(struct compile_state *state)
16922 struct triple *first, *ins;
16923 int print_location;
16924 struct occurance *last_occurance;
16926 print_location = 1;
16927 last_occurance = 0;
16928 fp = state->output;
16929 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
16930 first = RHS(state->main_function, 0);
16933 if (print_location &&
16934 last_occurance != ins->occurance) {
16935 if (!ins->occurance->parent) {
16936 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
16937 ins->occurance->function,
16938 ins->occurance->filename,
16939 ins->occurance->line,
16940 ins->occurance->col);
16943 struct occurance *ptr;
16944 fprintf(fp, "\t/*\n");
16945 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
16946 fprintf(fp, "\t * %s,%s:%d.%d\n",
16952 fprintf(fp, "\t */\n");
16955 if (last_occurance) {
16956 put_occurance(last_occurance);
16958 get_occurance(ins->occurance);
16959 last_occurance = ins->occurance;
16962 print_instruction(state, ins, fp);
16964 } while(ins != first);
16967 static void generate_code(struct compile_state *state)
16969 generate_local_labels(state);
16970 print_instructions(state);
16974 static void print_tokens(struct compile_state *state)
16977 tk = &state->token[0];
16982 next_token(state, 0);
16984 loc(stdout, state, 0);
16985 printf("%s <- `%s'\n",
16987 tk->ident ? tk->ident->name :
16988 tk->str_len ? tk->val.str : "");
16990 } while(tk->tok != TOK_EOF);
16993 static void compile(const char *filename, const char *ofilename,
16994 int cpu, int debug, int opt, const char *label_prefix)
16997 struct compile_state state;
16998 memset(&state, 0, sizeof(state));
17000 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
17001 memset(&state.token[i], 0, sizeof(state.token[i]));
17002 state.token[i].tok = -1;
17004 /* Remember the debug settings */
17006 state.debug = debug;
17007 state.optimize = opt;
17008 /* Remember the output filename */
17009 state.ofilename = ofilename;
17010 state.output = fopen(state.ofilename, "w");
17011 if (!state.output) {
17012 error(&state, 0, "Cannot open output file %s\n",
17015 /* Remember the label prefix */
17016 state.label_prefix = label_prefix;
17017 /* Prep the preprocessor */
17018 state.if_depth = 0;
17019 state.if_value = 0;
17020 /* register the C keywords */
17021 register_keywords(&state);
17022 /* register the keywords the macro preprocessor knows */
17023 register_macro_keywords(&state);
17024 /* Memorize where some special keywords are. */
17025 state.i_continue = lookup(&state, "continue", 8);
17026 state.i_break = lookup(&state, "break", 5);
17027 /* Enter the globl definition scope */
17028 start_scope(&state);
17029 register_builtins(&state);
17030 compile_file(&state, filename, 1);
17032 print_tokens(&state);
17035 /* Exit the global definition scope */
17038 /* Now that basic compilation has happened
17039 * optimize the intermediate code
17043 generate_code(&state);
17045 fprintf(stderr, "done\n");
17049 static void version(void)
17051 printf("romcc " VERSION " released " RELEASE_DATE "\n");
17054 static void usage(void)
17058 "Usage: romcc <source>.c\n"
17059 "Compile a C source file without using ram\n"
17063 static void arg_error(char *fmt, ...)
17066 va_start(args, fmt);
17067 vfprintf(stderr, fmt, args);
17073 int main(int argc, char **argv)
17075 const char *filename;
17076 const char *ofilename;
17077 const char *label_prefix;
17084 ofilename = "auto.inc";
17088 while((argc > 1) && (argc != last_argc)) {
17090 if (strncmp(argv[1], "--debug=", 8) == 0) {
17091 debug = atoi(argv[1] + 8);
17095 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
17096 label_prefix= argv[1] + 15;
17100 else if ((strcmp(argv[1],"-O") == 0) ||
17101 (strcmp(argv[1], "-O1") == 0)) {
17106 else if (strcmp(argv[1],"-O2") == 0) {
17111 else if ((strcmp(argv[1], "-o") == 0) && (argc > 2)) {
17112 ofilename = argv[2];
17116 else if (strncmp(argv[1], "-mcpu=", 6) == 0) {
17117 cpu = arch_encode_cpu(argv[1] + 6);
17118 if (cpu == BAD_CPU) {
17119 arg_error("Invalid cpu specified: %s\n",
17127 arg_error("Wrong argument count %d\n", argc);
17129 filename = argv[1];
17130 compile(filename, ofilename, cpu, debug, optimize, label_prefix);