14 #define DEBUG_ERROR_MESSAGES 0
15 #define DEBUG_COLOR_GRAPH 0
17 #define DEBUG_CONSISTENCY 1
18 #define DEBUG_RANGE_CONFLICTS 0
19 #define DEBUG_COALESCING 0
20 #define DEBUG_SDP_BLOCKS 0
21 #define DEBUG_TRIPLE_COLOR 0
23 #warning "FIXME boundary cases with small types in larger registers"
24 #warning "FIXME give clear error messages about unused variables"
25 #warning "FIXME properly handle multi dimensional arrays"
27 /* Control flow graph of a loop without goto.
38 * |\ GGG HHH | continue;
66 * DFlocal(X) = { Y <- Succ(X) | idom(Y) != X }
67 * DFup(Z) = { Y <- DF(Z) | idom(Y) != X }
70 * [] == DFlocal(X) U DF(X)
73 * Dominator graph of the same nodes.
77 * BBB JJJ BBB: [ JJJ ] ( JJJ ) JJJ: [ ] ()
79 * CCC CCC: [ ] ( BBB, JJJ )
81 * DDD EEE DDD: [ ] ( BBB ) EEE: [ JJJ ] ()
83 * FFF FFF: [ ] ( BBB )
85 * GGG HHH GGG: [ ] ( BBB ) HHH: [ BBB ] ()
90 * BBB and JJJ are definitely the dominance frontier.
91 * Where do I place phi functions and how do I make that decision.
94 static void die(char *fmt, ...)
99 vfprintf(stderr, fmt, args);
106 #define MALLOC_STRONG_DEBUG
107 static void *xmalloc(size_t size, const char *name)
112 die("Cannot malloc %ld bytes to hold %s: %s\n",
113 size + 0UL, name, strerror(errno));
118 static void *xcmalloc(size_t size, const char *name)
121 buf = xmalloc(size, name);
122 memset(buf, 0, size);
126 static void xfree(const void *ptr)
131 static char *xstrdup(const char *str)
136 new = xmalloc(len + 1, "xstrdup string");
137 memcpy(new, str, len);
142 static void xchdir(const char *path)
144 if (chdir(path) != 0) {
145 die("chdir to %s failed: %s\n",
146 path, strerror(errno));
150 static int exists(const char *dirname, const char *filename)
154 if (access(filename, O_RDONLY) < 0) {
155 if ((errno != EACCES) && (errno != EROFS)) {
163 static char *slurp_file(const char *dirname, const char *filename, off_t *r_size)
167 off_t size, progress;
176 fd = open(filename, O_RDONLY);
178 die("Cannot open '%s' : %s\n",
179 filename, strerror(errno));
181 result = fstat(fd, &stats);
183 die("Cannot stat: %s: %s\n",
184 filename, strerror(errno));
186 size = stats.st_size;
188 buf = xmalloc(size +2, filename);
189 buf[size] = '\n'; /* Make certain the file is newline terminated */
190 buf[size+1] = '\0'; /* Null terminate the file for good measure */
192 while(progress < size) {
193 result = read(fd, buf + progress, size - progress);
195 if ((errno == EINTR) || (errno == EAGAIN))
197 die("read on %s of %ld bytes failed: %s\n",
198 filename, (size - progress)+ 0UL, strerror(errno));
204 die("Close of %s failed: %s\n",
205 filename, strerror(errno));
210 /* Long on the destination platform */
212 typedef unsigned int ulong_t;
215 typedef unsigned long ulong_t;
220 struct file_state *prev;
221 const char *basename;
229 const char *report_name;
230 const char *report_dir;
235 struct hash_entry *ident;
243 /* I have two classes of types:
245 * Logical types. (The type the C standard says the operation is of)
247 * The operational types are:
262 * No memory is useable by the compiler.
263 * There is no floating point support.
264 * All operations take place in general purpose registers.
265 * There is one type of general purpose register.
266 * Unsigned longs are stored in that general purpose register.
269 /* Operations on general purpose registers.
288 #define OP_POS 16 /* Dummy positive operator don't use it */
298 #define OP_SLESSEQ 26
299 #define OP_ULESSEQ 27
300 #define OP_SMOREEQ 28
301 #define OP_UMOREEQ 29
303 #define OP_LFALSE 30 /* Test if the expression is logically false */
304 #define OP_LTRUE 31 /* Test if the expression is logcially true */
308 /* For OP_STORE ->type holds the type
309 * RHS(0) holds the destination address
310 * RHS(1) holds the value to store.
315 #define OP_MIN_CONST 50
316 #define OP_MAX_CONST 59
317 #define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
318 #define OP_INTCONST 50
319 /* For OP_INTCONST ->type holds the type.
320 * ->u.cval holds the constant value.
322 #define OP_BLOBCONST 51
323 /* For OP_BLOBCONST ->type holds the layout and size
324 * information. u.blob holds a pointer to the raw binary
325 * data for the constant initializer.
327 #define OP_ADDRCONST 52
328 /* For OP_ADDRCONST ->type holds the type.
329 * MISC(0) holds the reference to the static variable.
330 * ->u.cval holds an offset from that value.
334 /* OP_WRITE moves one pseudo register to another.
335 * RHS(0) holds the destination pseudo register, which must be an OP_DECL.
336 * RHS(1) holds the psuedo to move.
340 /* OP_READ reads the value of a variable and makes
341 * it available for the pseudo operation.
342 * Useful for things like def-use chains.
343 * RHS(0) holds points to the triple to read from.
346 /* OP_COPY makes a copy of the psedo register or constant in RHS(0).
349 /* OP_PIECE returns one piece of a instruction that returns a structure.
350 * MISC(0) is the instruction
351 * u.cval is the LHS piece of the instruction to return.
354 /* OP_ASM holds a sequence of assembly instructions, the result
355 * of a C asm directive.
356 * RHS(x) holds input value x to the assembly sequence.
357 * LHS(x) holds the output value x from the assembly sequence.
358 * u.blob holds the string of assembly instructions.
362 /* OP_DEREF generates an lvalue from a pointer.
363 * RHS(0) holds the pointer value.
364 * OP_DEREF serves as a place holder to indicate all necessary
365 * checks have been done to indicate a value is an lvalue.
368 /* OP_DOT references a submember of a structure lvalue.
369 * RHS(0) holds the lvalue.
370 * ->u.field holds the name of the field we want.
372 * Not seen outside of expressions.
375 /* OP_VAL returns the value of a subexpression of the current expression.
376 * Useful for operators that have side effects.
377 * RHS(0) holds the expression.
378 * MISC(0) holds the subexpression of RHS(0) that is the
379 * value of the expression.
381 * Not seen outside of expressions.
384 /* OP_LAND performs a C logical and between RHS(0) and RHS(1).
385 * Not seen outside of expressions.
388 /* OP_LOR performs a C logical or between RHS(0) and RHS(1).
389 * Not seen outside of expressions.
392 /* OP_CODE performas a C ? : operation.
393 * RHS(0) holds the test.
394 * RHS(1) holds the expression to evaluate if the test returns true.
395 * RHS(2) holds the expression to evaluate if the test returns false.
396 * Not seen outside of expressions.
399 /* OP_COMMA performacs a C comma operation.
400 * That is RHS(0) is evaluated, then RHS(1)
401 * and the value of RHS(1) is returned.
402 * Not seen outside of expressions.
406 /* OP_CALL performs a procedure call.
407 * MISC(0) holds a pointer to the OP_LIST of a function
408 * RHS(x) holds argument x of a function
410 * Currently not seen outside of expressions.
412 #define OP_VAL_VEC 74
413 /* OP_VAL_VEC is an array of triples that are either variable
414 * or values for a structure or an array.
415 * RHS(x) holds element x of the vector.
416 * triple->type->elements holds the size of the vector.
421 /* OP_LIST Holds a list of statements, and a result value.
422 * RHS(0) holds the list of statements.
423 * MISC(0) holds the value of the statements.
426 #define OP_BRANCH 81 /* branch */
427 /* For branch instructions
428 * TARG(0) holds the branch target.
429 * RHS(0) if present holds the branch condition.
430 * ->next holds where to branch to if the branch is not taken.
431 * The branch target can only be a decl...
435 /* OP_LABEL is a triple that establishes an target for branches.
436 * ->use is the list of all branches that use this label.
440 /* OP_DECL is a triple that establishes an lvalue for assignments.
441 * ->use is a list of statements that use the variable.
445 /* OP_SDECL is a triple that establishes a variable of static
447 * ->use is a list of statements that use the variable.
448 * MISC(0) holds the initializer expression.
453 /* OP_PHI is a triple used in SSA form code.
454 * It is used when multiple code paths merge and a variable needs
455 * a single assignment from any of those code paths.
456 * The operation is a cross between OP_DECL and OP_WRITE, which
457 * is what OP_PHI is geneared from.
459 * RHS(x) points to the value from code path x
460 * The number of RHS entries is the number of control paths into the block
461 * in which OP_PHI resides. The elements of the array point to point
462 * to the variables OP_PHI is derived from.
464 * MISC(0) holds a pointer to the orginal OP_DECL node.
467 /* Architecture specific instructions */
470 #define OP_SET_EQ 102
471 #define OP_SET_NOTEQ 103
472 #define OP_SET_SLESS 104
473 #define OP_SET_ULESS 105
474 #define OP_SET_SMORE 106
475 #define OP_SET_UMORE 107
476 #define OP_SET_SLESSEQ 108
477 #define OP_SET_ULESSEQ 109
478 #define OP_SET_SMOREEQ 110
479 #define OP_SET_UMOREEQ 111
482 #define OP_JMP_EQ 113
483 #define OP_JMP_NOTEQ 114
484 #define OP_JMP_SLESS 115
485 #define OP_JMP_ULESS 116
486 #define OP_JMP_SMORE 117
487 #define OP_JMP_UMORE 118
488 #define OP_JMP_SLESSEQ 119
489 #define OP_JMP_ULESSEQ 120
490 #define OP_JMP_SMOREEQ 121
491 #define OP_JMP_UMOREEQ 122
493 /* Builtin operators that it is just simpler to use the compiler for */
511 #define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
513 #define BLOCK 8 /* Triple stores the current block */
514 unsigned char lhs, rhs, misc, targ;
517 #define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \
525 static const struct op_info table_ops[] = {
526 [OP_SDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "sdivt"),
527 [OP_UDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "udivt"),
528 [OP_SMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smul"),
529 [OP_UMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umul"),
530 [OP_SDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sdiv"),
531 [OP_UDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "udiv"),
532 [OP_SMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smod"),
533 [OP_UMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umod"),
534 [OP_ADD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "add"),
535 [OP_SUB ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sub"),
536 [OP_SL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sl"),
537 [OP_USR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "usr"),
538 [OP_SSR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ssr"),
539 [OP_AND ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "and"),
540 [OP_XOR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "xor"),
541 [OP_OR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "or"),
542 [OP_POS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "pos"),
543 [OP_NEG ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "neg"),
544 [OP_INVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "invert"),
546 [OP_EQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "eq"),
547 [OP_NOTEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "noteq"),
548 [OP_SLESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sless"),
549 [OP_ULESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "uless"),
550 [OP_SMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smore"),
551 [OP_UMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umore"),
552 [OP_SLESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "slesseq"),
553 [OP_ULESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ulesseq"),
554 [OP_SMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smoreeq"),
555 [OP_UMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umoreeq"),
556 [OP_LFALSE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "lfalse"),
557 [OP_LTRUE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "ltrue"),
559 [OP_LOAD ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "load"),
560 [OP_STORE ] = OP( 0, 2, 0, 0, IMPURE | BLOCK , "store"),
562 [OP_NOOP ] = OP( 0, 0, 0, 0, PURE | BLOCK, "noop"),
564 [OP_INTCONST ] = OP( 0, 0, 0, 0, PURE | DEF, "intconst"),
565 [OP_BLOBCONST ] = OP( 0, 0, 0, 0, PURE, "blobconst"),
566 [OP_ADDRCONST ] = OP( 0, 0, 1, 0, PURE | DEF, "addrconst"),
568 [OP_WRITE ] = OP( 0, 2, 0, 0, PURE | BLOCK, "write"),
569 [OP_READ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "read"),
570 [OP_COPY ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "copy"),
571 [OP_PIECE ] = OP( 0, 0, 1, 0, PURE | DEF, "piece"),
572 [OP_ASM ] = OP(-1, -1, 0, 0, IMPURE, "asm"),
573 [OP_DEREF ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "deref"),
574 [OP_DOT ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "dot"),
576 [OP_VAL ] = OP( 0, 1, 1, 0, 0 | DEF | BLOCK, "val"),
577 [OP_LAND ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "land"),
578 [OP_LOR ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "lor"),
579 [OP_COND ] = OP( 0, 3, 0, 0, 0 | DEF | BLOCK, "cond"),
580 [OP_COMMA ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "comma"),
581 /* Call is special most it can stand in for anything so it depends on context */
582 [OP_CALL ] = OP(-1, -1, 1, 0, 0 | BLOCK, "call"),
583 /* The sizes of OP_CALL and OP_VAL_VEC depend upon context */
584 [OP_VAL_VEC ] = OP( 0, -1, 0, 0, 0 | BLOCK, "valvec"),
586 [OP_LIST ] = OP( 0, 1, 1, 0, 0 | DEF, "list"),
587 /* The number of targets for OP_BRANCH depends on context */
588 [OP_BRANCH ] = OP( 0, -1, 0, 1, PURE | BLOCK, "branch"),
589 [OP_LABEL ] = OP( 0, 0, 0, 0, PURE | BLOCK, "label"),
590 [OP_ADECL ] = OP( 0, 0, 0, 0, PURE | BLOCK, "adecl"),
591 [OP_SDECL ] = OP( 0, 0, 1, 0, PURE | BLOCK, "sdecl"),
592 /* The number of RHS elements of OP_PHI depend upon context */
593 [OP_PHI ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
595 [OP_CMP ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK, "cmp"),
596 [OP_TEST ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "test"),
597 [OP_SET_EQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_eq"),
598 [OP_SET_NOTEQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_noteq"),
599 [OP_SET_SLESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_sless"),
600 [OP_SET_ULESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_uless"),
601 [OP_SET_SMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smore"),
602 [OP_SET_UMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umore"),
603 [OP_SET_SLESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_slesseq"),
604 [OP_SET_ULESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_ulesseq"),
605 [OP_SET_SMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smoreq"),
606 [OP_SET_UMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umoreq"),
607 [OP_JMP ] = OP( 0, 0, 0, 1, PURE | BLOCK, "jmp"),
608 [OP_JMP_EQ ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_eq"),
609 [OP_JMP_NOTEQ ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_noteq"),
610 [OP_JMP_SLESS ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_sless"),
611 [OP_JMP_ULESS ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_uless"),
612 [OP_JMP_SMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_smore"),
613 [OP_JMP_UMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_umore"),
614 [OP_JMP_SLESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_slesseq"),
615 [OP_JMP_ULESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_ulesseq"),
616 [OP_JMP_SMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_smoreq"),
617 [OP_JMP_UMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_umoreq"),
619 [OP_INB ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inb"),
620 [OP_INW ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inw"),
621 [OP_INL ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inl"),
622 [OP_OUTB ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outb"),
623 [OP_OUTW ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outw"),
624 [OP_OUTL ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outl"),
625 [OP_BSF ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsf"),
626 [OP_BSR ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsr"),
627 [OP_RDMSR ] = OP( 2, 1, 0, 0, IMPURE | BLOCK, "__rdmsr"),
628 [OP_WRMSR ] = OP( 0, 3, 0, 0, IMPURE | BLOCK, "__wrmsr"),
629 [OP_HLT ] = OP( 0, 0, 0, 0, IMPURE | BLOCK, "__hlt"),
632 #define OP_MAX (sizeof(table_ops)/sizeof(table_ops[0]))
634 static const char *tops(int index)
636 static const char unknown[] = "unknown op";
640 if (index > OP_MAX) {
643 return table_ops[index].name;
650 struct triple_set *next;
651 struct triple *member;
661 const char *filename;
662 const char *function;
665 struct occurance *parent;
668 struct triple *next, *prev;
669 struct triple_set *use;
672 unsigned char template_id;
673 unsigned short sizes;
674 #define TRIPLE_LHS(SIZES) (((SIZES) >> 0) & 0x0f)
675 #define TRIPLE_RHS(SIZES) (((SIZES) >> 4) & 0xff)
676 #define TRIPLE_MISC(SIZES) (((SIZES) >> 12) & 0x03)
677 #define TRIPLE_TARG(SIZES) (((SIZES) >> 14) & 0x03)
678 #define TRIPLE_SIZE(SIZES) \
679 (TRIPLE_LHS(SIZES) + \
680 TRIPLE_RHS(SIZES) + \
681 TRIPLE_MISC(SIZES) + \
683 #define TRIPLE_SIZES(LHS, RHS, MISC, TARG) \
684 ((((LHS) & 0x0f) << 0) | \
685 (((RHS) & 0xff) << 4) | \
686 (((MISC) & 0x03) << 12) | \
687 (((TARG) & 0x03) << 14))
688 #define TRIPLE_LHS_OFF(SIZES) (0)
689 #define TRIPLE_RHS_OFF(SIZES) (TRIPLE_LHS_OFF(SIZES) + TRIPLE_LHS(SIZES))
690 #define TRIPLE_MISC_OFF(SIZES) (TRIPLE_RHS_OFF(SIZES) + TRIPLE_RHS(SIZES))
691 #define TRIPLE_TARG_OFF(SIZES) (TRIPLE_MISC_OFF(SIZES) + TRIPLE_MISC(SIZES))
692 #define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF((PTR)->sizes) + (INDEX)])
693 #define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF((PTR)->sizes) + (INDEX)])
694 #define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF((PTR)->sizes) + (INDEX)])
695 #define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF((PTR)->sizes) + (INDEX)])
696 unsigned id; /* A scratch value and finally the register */
697 #define TRIPLE_FLAG_FLATTENED (1 << 31)
698 #define TRIPLE_FLAG_PRE_SPLIT (1 << 30)
699 #define TRIPLE_FLAG_POST_SPLIT (1 << 29)
700 struct occurance *occurance;
705 struct hash_entry *field;
706 struct asm_info *ainfo;
708 struct triple *param[2];
715 struct ins_template {
716 struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
720 struct ins_template tmpl;
725 struct block_set *next;
726 struct block *member;
729 struct block *work_next;
730 struct block *left, *right;
731 struct triple *first, *last;
733 struct block_set *use;
734 struct block_set *idominates;
735 struct block_set *domfrontier;
737 struct block_set *ipdominates;
738 struct block_set *ipdomfrontier;
746 struct hash_entry *ident;
753 struct hash_entry *ident;
759 struct hash_entry *next;
763 struct macro *sym_define;
764 struct symbol *sym_label;
765 struct symbol *sym_struct;
766 struct symbol *sym_ident;
769 #define HASH_TABLE_SIZE 2048
771 struct compile_state {
772 const char *label_prefix;
773 const char *ofilename;
775 struct file_state *file;
776 struct occurance *last_occurance;
777 const char *function;
778 struct token token[4];
779 struct hash_entry *hash_table[HASH_TABLE_SIZE];
780 struct hash_entry *i_continue;
781 struct hash_entry *i_break;
783 int if_depth, if_value;
785 struct file_state *macro_file;
786 struct triple *main_function;
787 struct block *first_block, *last_block;
794 /* visibility global/local */
795 /* static/auto duration */
796 /* typedef, register, inline */
798 #define STOR_MASK 0x000f
800 #define STOR_GLOBAL 0x0001
802 #define STOR_PERM 0x0002
803 /* Storage specifiers */
804 #define STOR_AUTO 0x0000
805 #define STOR_STATIC 0x0002
806 #define STOR_EXTERN 0x0003
807 #define STOR_REGISTER 0x0004
808 #define STOR_TYPEDEF 0x0008
809 #define STOR_INLINE 0x000c
812 #define QUAL_MASK 0x0070
813 #define QUAL_NONE 0x0000
814 #define QUAL_CONST 0x0010
815 #define QUAL_VOLATILE 0x0020
816 #define QUAL_RESTRICT 0x0040
819 #define TYPE_MASK 0x1f00
820 #define TYPE_INTEGER(TYPE) (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG))
821 #define TYPE_ARITHMETIC(TYPE) (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE))
822 #define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
823 #define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
824 #define TYPE_MKUNSIGNED(TYPE) ((TYPE) | 0x0100)
825 #define TYPE_RANK(TYPE) ((TYPE) & ~0x0100)
826 #define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER)
827 #define TYPE_DEFAULT 0x0000
828 #define TYPE_VOID 0x0100
829 #define TYPE_CHAR 0x0200
830 #define TYPE_UCHAR 0x0300
831 #define TYPE_SHORT 0x0400
832 #define TYPE_USHORT 0x0500
833 #define TYPE_INT 0x0600
834 #define TYPE_UINT 0x0700
835 #define TYPE_LONG 0x0800
836 #define TYPE_ULONG 0x0900
837 #define TYPE_LLONG 0x0a00 /* long long */
838 #define TYPE_ULLONG 0x0b00
839 #define TYPE_FLOAT 0x0c00
840 #define TYPE_DOUBLE 0x0d00
841 #define TYPE_LDOUBLE 0x0e00 /* long double */
842 #define TYPE_STRUCT 0x1000
843 #define TYPE_ENUM 0x1100
844 #define TYPE_POINTER 0x1200
846 * type->left holds the type pointed to.
848 #define TYPE_FUNCTION 0x1300
849 /* For TYPE_FUNCTION:
850 * type->left holds the return type.
851 * type->right holds the...
853 #define TYPE_PRODUCT 0x1400
854 /* TYPE_PRODUCT is a basic building block when defining structures
855 * type->left holds the type that appears first in memory.
856 * type->right holds the type that appears next in memory.
858 #define TYPE_OVERLAP 0x1500
859 /* TYPE_OVERLAP is a basic building block when defining unions
860 * type->left and type->right holds to types that overlap
861 * each other in memory.
863 #define TYPE_ARRAY 0x1600
864 /* TYPE_ARRAY is a basic building block when definitng arrays.
865 * type->left holds the type we are an array of.
866 * type-> holds the number of elements.
870 #define ELEMENT_COUNT_UNSPECIFIED (~0U)
872 #define ELEMENT_COUNT_UNSPECIFIED (~0UL)
877 struct type *left, *right;
879 struct hash_entry *field_ident;
880 struct hash_entry *type_ident;
883 #define MAX_REGISTERS 75
884 #define MAX_REG_EQUIVS 16
885 #define REGISTER_BITS 16
886 #define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
887 #define TEMPLATE_BITS 7
888 #define MAX_TEMPLATES (1<<TEMPLATE_BITS)
891 #define REG_UNNEEDED 1
892 #define REG_VIRT0 (MAX_REGISTERS + 0)
893 #define REG_VIRT1 (MAX_REGISTERS + 1)
894 #define REG_VIRT2 (MAX_REGISTERS + 2)
895 #define REG_VIRT3 (MAX_REGISTERS + 3)
896 #define REG_VIRT4 (MAX_REGISTERS + 4)
897 #define REG_VIRT5 (MAX_REGISTERS + 5)
898 #define REG_VIRT6 (MAX_REGISTERS + 5)
899 #define REG_VIRT7 (MAX_REGISTERS + 5)
900 #define REG_VIRT8 (MAX_REGISTERS + 5)
901 #define REG_VIRT9 (MAX_REGISTERS + 5)
903 /* Provision for 8 register classes */
905 #define REGC_SHIFT REGISTER_BITS
906 #define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
907 #define REG_MASK (MAX_VIRT_REGISTERS -1)
908 #define ID_REG(ID) ((ID) & REG_MASK)
909 #define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
910 #define ID_REGCM(ID) (((ID) & REGC_MASK) >> REGC_SHIFT)
911 #define SET_REGCM(ID, REGCM) ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
912 #define SET_INFO(ID, INFO) ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
913 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
915 static unsigned arch_reg_regcm(struct compile_state *state, int reg);
916 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
917 static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm);
918 static void arch_reg_equivs(
919 struct compile_state *state, unsigned *equiv, int reg);
920 static int arch_select_free_register(
921 struct compile_state *state, char *used, int classes);
922 static unsigned arch_regc_size(struct compile_state *state, int class);
923 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
924 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
925 static const char *arch_reg_str(int reg);
926 static struct reg_info arch_reg_constraint(
927 struct compile_state *state, struct type *type, const char *constraint);
928 static struct reg_info arch_reg_clobber(
929 struct compile_state *state, const char *clobber);
930 static struct reg_info arch_reg_lhs(struct compile_state *state,
931 struct triple *ins, int index);
932 static struct reg_info arch_reg_rhs(struct compile_state *state,
933 struct triple *ins, int index);
934 static struct triple *transform_to_arch_instruction(
935 struct compile_state *state, struct triple *ins);
939 #define DEBUG_ABORT_ON_ERROR 0x0001
940 #define DEBUG_INTERMEDIATE_CODE 0x0002
941 #define DEBUG_CONTROL_FLOW 0x0004
942 #define DEBUG_BASIC_BLOCKS 0x0008
943 #define DEBUG_FDOMINATORS 0x0010
944 #define DEBUG_RDOMINATORS 0x0020
945 #define DEBUG_TRIPLES 0x0040
946 #define DEBUG_INTERFERENCE 0x0080
947 #define DEBUG_ARCH_CODE 0x0100
948 #define DEBUG_CODE_ELIMINATION 0x0200
949 #define DEBUG_INSERTED_COPIES 0x0400
951 #define GLOBAL_SCOPE_DEPTH 1
952 #define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
954 static void compile_file(struct compile_state *old_state, const char *filename, int local);
956 static void do_cleanup(struct compile_state *state)
959 fclose(state->output);
960 unlink(state->ofilename);
964 static int get_col(struct file_state *file)
968 ptr = file->line_start;
970 for(col = 0; ptr < end; ptr++) {
975 col = (col & ~7) + 8;
981 static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
984 if (triple && triple->occurance) {
985 struct occurance *spot;
986 spot = triple->occurance;
987 while(spot->parent) {
990 fprintf(fp, "%s:%d.%d: ",
991 spot->filename, spot->line, spot->col);
997 col = get_col(state->file);
998 fprintf(fp, "%s:%d.%d: ",
999 state->file->report_name, state->file->report_line, col);
1002 static void __internal_error(struct compile_state *state, struct triple *ptr,
1006 va_start(args, fmt);
1007 loc(stderr, state, ptr);
1009 fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
1011 fprintf(stderr, "Internal compiler error: ");
1012 vfprintf(stderr, fmt, args);
1013 fprintf(stderr, "\n");
1020 static void __internal_warning(struct compile_state *state, struct triple *ptr,
1024 va_start(args, fmt);
1025 loc(stderr, state, ptr);
1027 fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
1029 fprintf(stderr, "Internal compiler warning: ");
1030 vfprintf(stderr, fmt, args);
1031 fprintf(stderr, "\n");
1037 static void __error(struct compile_state *state, struct triple *ptr,
1041 va_start(args, fmt);
1042 loc(stderr, state, ptr);
1043 vfprintf(stderr, fmt, args);
1045 fprintf(stderr, "\n");
1047 if (state->debug & DEBUG_ABORT_ON_ERROR) {
1053 static void __warning(struct compile_state *state, struct triple *ptr,
1057 va_start(args, fmt);
1058 loc(stderr, state, ptr);
1059 fprintf(stderr, "warning: ");
1060 vfprintf(stderr, fmt, args);
1061 fprintf(stderr, "\n");
1065 #if DEBUG_ERROR_MESSAGES
1066 # define internal_error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_error
1067 # define internal_warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_warning
1068 # define error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__error
1069 # define warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__warning
1071 # define internal_error __internal_error
1072 # define internal_warning __internal_warning
1073 # define error __error
1074 # define warning __warning
1076 #define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1078 static void valid_op(struct compile_state *state, int op)
1080 char *fmt = "invalid op: %d";
1082 internal_error(state, 0, fmt, op);
1085 internal_error(state, 0, fmt, op);
1089 static void valid_ins(struct compile_state *state, struct triple *ptr)
1091 valid_op(state, ptr->op);
1094 static void process_trigraphs(struct compile_state *state)
1096 char *src, *dest, *end;
1097 struct file_state *file;
1099 src = dest = file->buf;
1100 end = file->buf + file->size;
1101 while((end - src) >= 3) {
1102 if ((src[0] == '?') && (src[1] == '?')) {
1105 case '=': c = '#'; break;
1106 case '/': c = '\\'; break;
1107 case '\'': c = '^'; break;
1108 case '(': c = '['; break;
1109 case ')': c = ']'; break;
1110 case '!': c = '!'; break;
1111 case '<': c = '{'; break;
1112 case '>': c = '}'; break;
1113 case '-': c = '~'; break;
1130 file->size = dest - file->buf;
1133 static void splice_lines(struct compile_state *state)
1135 char *src, *dest, *end;
1136 struct file_state *file;
1138 src = dest = file->buf;
1139 end = file->buf + file->size;
1140 while((end - src) >= 2) {
1141 if ((src[0] == '\\') && (src[1] == '\n')) {
1151 file->size = dest - file->buf;
1154 static struct type void_type;
1155 static void use_triple(struct triple *used, struct triple *user)
1157 struct triple_set **ptr, *new;
1164 if ((*ptr)->member == user) {
1167 ptr = &(*ptr)->next;
1169 /* Append new to the head of the list,
1170 * copy_func and rename_block_variables
1173 new = xcmalloc(sizeof(*new), "triple_set");
1175 new->next = used->use;
1179 static void unuse_triple(struct triple *used, struct triple *unuser)
1181 struct triple_set *use, **ptr;
1188 if (use->member == unuser) {
1198 static void put_occurance(struct occurance *occurance)
1200 occurance->count -= 1;
1201 if (occurance->count <= 0) {
1202 if (occurance->parent) {
1203 put_occurance(occurance->parent);
1209 static void get_occurance(struct occurance *occurance)
1211 occurance->count += 1;
1215 static struct occurance *new_occurance(struct compile_state *state)
1217 struct occurance *result, *last;
1218 const char *filename;
1219 const char *function;
1227 filename = state->file->report_name;
1228 line = state->file->report_line;
1229 col = get_col(state->file);
1231 if (state->function) {
1232 function = state->function;
1234 last = state->last_occurance;
1236 (last->col == col) &&
1237 (last->line == line) &&
1238 (last->function == function) &&
1239 (strcmp(last->filename, filename) == 0)) {
1240 get_occurance(last);
1244 state->last_occurance = 0;
1245 put_occurance(last);
1247 result = xmalloc(sizeof(*result), "occurance");
1249 result->filename = filename;
1250 result->function = function;
1251 result->line = line;
1254 state->last_occurance = result;
1258 static struct occurance *inline_occurance(struct compile_state *state,
1259 struct occurance *new, struct occurance *orig)
1261 struct occurance *result, *last;
1262 last = state->last_occurance;
1264 (last->parent == orig) &&
1265 (last->col == new->col) &&
1266 (last->line == new->line) &&
1267 (last->function == new->function) &&
1268 (last->filename == new->filename)) {
1269 get_occurance(last);
1273 state->last_occurance = 0;
1274 put_occurance(last);
1276 get_occurance(orig);
1277 result = xmalloc(sizeof(*result), "occurance");
1279 result->filename = new->filename;
1280 result->function = new->function;
1281 result->line = new->line;
1282 result->col = new->col;
1283 result->parent = orig;
1284 state->last_occurance = result;
1289 static struct occurance dummy_occurance = {
1291 .filename = __FILE__,
1298 /* The zero triple is used as a place holder when we are removing pointers
1299 * from a triple. Having allows certain sanity checks to pass even
1300 * when the original triple that was pointed to is gone.
1302 static struct triple zero_triple = {
1303 .next = &zero_triple,
1304 .prev = &zero_triple,
1307 .sizes = TRIPLE_SIZES(0, 0, 0, 0),
1308 .id = -1, /* An invalid id */
1309 .u = { .cval = 0, },
1310 .occurance = &dummy_occurance,
1311 .param = { [0] = 0, [1] = 0, },
1315 static unsigned short triple_sizes(struct compile_state *state,
1316 int op, struct type *type, int lhs_wanted, int rhs_wanted,
1317 struct occurance *occurance)
1319 int lhs, rhs, misc, targ;
1320 struct triple dummy;
1322 dummy.occurance = occurance;
1323 valid_op(state, op);
1324 lhs = table_ops[op].lhs;
1325 rhs = table_ops[op].rhs;
1326 misc = table_ops[op].misc;
1327 targ = table_ops[op].targ;
1330 if (op == OP_CALL) {
1333 param = type->right;
1334 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
1336 param = param->right;
1338 if ((param->type & TYPE_MASK) != TYPE_VOID) {
1342 if ((type->left->type & TYPE_MASK) == TYPE_STRUCT) {
1343 lhs = type->left->elements;
1346 else if (op == OP_VAL_VEC) {
1347 rhs = type->elements;
1349 else if ((op == OP_BRANCH) || (op == OP_PHI)) {
1352 else if (op == OP_ASM) {
1356 if ((rhs < 0) || (rhs > MAX_RHS)) {
1357 internal_error(state, &dummy, "bad rhs %d", rhs);
1359 if ((lhs < 0) || (lhs > MAX_LHS)) {
1360 internal_error(state, &dummy, "bad lhs");
1362 if ((misc < 0) || (misc > MAX_MISC)) {
1363 internal_error(state, &dummy, "bad misc");
1365 if ((targ < 0) || (targ > MAX_TARG)) {
1366 internal_error(state, &dummy, "bad targs");
1368 return TRIPLE_SIZES(lhs, rhs, misc, targ);
1371 static struct triple *alloc_triple(struct compile_state *state,
1372 int op, struct type *type, int lhs, int rhs,
1373 struct occurance *occurance)
1375 size_t size, sizes, extra_count, min_count;
1377 sizes = triple_sizes(state, op, type, lhs, rhs, occurance);
1379 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
1380 extra_count = TRIPLE_SIZE(sizes);
1381 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
1383 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
1384 ret = xcmalloc(size, "tripple");
1390 ret->occurance = occurance;
1394 struct triple *dup_triple(struct compile_state *state, struct triple *src)
1397 int src_lhs, src_rhs, src_size;
1398 src_lhs = TRIPLE_LHS(src->sizes);
1399 src_rhs = TRIPLE_RHS(src->sizes);
1400 src_size = TRIPLE_SIZE(src->sizes);
1401 get_occurance(src->occurance);
1402 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
1404 memcpy(dup, src, sizeof(*src));
1405 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
1409 static struct triple *new_triple(struct compile_state *state,
1410 int op, struct type *type, int lhs, int rhs)
1413 struct occurance *occurance;
1414 occurance = new_occurance(state);
1415 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
1419 static struct triple *build_triple(struct compile_state *state,
1420 int op, struct type *type, struct triple *left, struct triple *right,
1421 struct occurance *occurance)
1425 ret = alloc_triple(state, op, type, -1, -1, occurance);
1426 count = TRIPLE_SIZE(ret->sizes);
1428 ret->param[0] = left;
1431 ret->param[1] = right;
1436 static struct triple *triple(struct compile_state *state,
1437 int op, struct type *type, struct triple *left, struct triple *right)
1441 ret = new_triple(state, op, type, -1, -1);
1442 count = TRIPLE_SIZE(ret->sizes);
1444 ret->param[0] = left;
1447 ret->param[1] = right;
1452 static struct triple *branch(struct compile_state *state,
1453 struct triple *targ, struct triple *test)
1456 ret = new_triple(state, OP_BRANCH, &void_type, -1, test?1:0);
1460 TARG(ret, 0) = targ;
1461 /* record the branch target was used */
1462 if (!targ || (targ->op != OP_LABEL)) {
1463 internal_error(state, 0, "branch not to label");
1464 use_triple(targ, ret);
1470 static void insert_triple(struct compile_state *state,
1471 struct triple *first, struct triple *ptr)
1474 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
1475 internal_error(state, ptr, "expression already used");
1478 ptr->prev = first->prev;
1479 ptr->prev->next = ptr;
1480 ptr->next->prev = ptr;
1481 if ((ptr->prev->op == OP_BRANCH) &&
1482 TRIPLE_RHS(ptr->prev->sizes)) {
1483 unuse_triple(first, ptr->prev);
1484 use_triple(ptr, ptr->prev);
1489 static int triple_stores_block(struct compile_state *state, struct triple *ins)
1491 /* This function is used to determine if u.block
1492 * is utilized to store the current block number.
1495 valid_ins(state, ins);
1496 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
1497 return stores_block;
1500 static struct block *block_of_triple(struct compile_state *state,
1503 struct triple *first;
1504 first = RHS(state->main_function, 0);
1505 while(ins != first && !triple_stores_block(state, ins)) {
1506 if (ins == ins->prev) {
1507 internal_error(state, 0, "ins == ins->prev?");
1511 if (!triple_stores_block(state, ins)) {
1512 internal_error(state, ins, "Cannot find block");
1514 return ins->u.block;
1517 static struct triple *pre_triple(struct compile_state *state,
1518 struct triple *base,
1519 int op, struct type *type, struct triple *left, struct triple *right)
1521 struct block *block;
1523 /* If I am an OP_PIECE jump to the real instruction */
1524 if (base->op == OP_PIECE) {
1525 base = MISC(base, 0);
1527 block = block_of_triple(state, base);
1528 get_occurance(base->occurance);
1529 ret = build_triple(state, op, type, left, right, base->occurance);
1530 if (triple_stores_block(state, ret)) {
1531 ret->u.block = block;
1533 insert_triple(state, base, ret);
1534 if (block->first == base) {
1540 static struct triple *post_triple(struct compile_state *state,
1541 struct triple *base,
1542 int op, struct type *type, struct triple *left, struct triple *right)
1544 struct block *block;
1547 /* If I am an OP_PIECE jump to the real instruction */
1548 if (base->op == OP_PIECE) {
1549 base = MISC(base, 0);
1551 /* If I have a left hand side skip over it */
1552 zlhs = TRIPLE_LHS(base->sizes);
1554 base = LHS(base, zlhs - 1);
1557 block = block_of_triple(state, base);
1558 get_occurance(base->occurance);
1559 ret = build_triple(state, op, type, left, right, base->occurance);
1560 if (triple_stores_block(state, ret)) {
1561 ret->u.block = block;
1563 insert_triple(state, base->next, ret);
1564 if (block->last == base) {
1570 static struct triple *label(struct compile_state *state)
1572 /* Labels don't get a type */
1573 struct triple *result;
1574 result = triple(state, OP_LABEL, &void_type, 0, 0);
1578 static void display_triple(FILE *fp, struct triple *ins)
1580 struct occurance *ptr;
1584 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
1587 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
1590 reg = arch_reg_str(ID_REG(ins->id));
1591 if (ins->op == OP_INTCONST) {
1592 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s <0x%08lx> ",
1593 ins, pre, post, reg, ins->template_id, tops(ins->op),
1596 else if (ins->op == OP_ADDRCONST) {
1597 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s %-10p <0x%08lx>",
1598 ins, pre, post, reg, ins->template_id, tops(ins->op),
1599 MISC(ins, 0), ins->u.cval);
1603 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s",
1604 ins, pre, post, reg, ins->template_id, tops(ins->op));
1605 count = TRIPLE_SIZE(ins->sizes);
1606 for(i = 0; i < count; i++) {
1607 fprintf(fp, " %-10p", ins->param[i]);
1614 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
1615 fprintf(fp, " %s,%s:%d.%d",
1624 struct triple_set *user;
1625 for(user = ptr->use; user; user = user->next) {
1626 fprintf(fp, "use: %p\n", user->member);
1633 static int triple_is_pure(struct compile_state *state, struct triple *ins)
1635 /* Does the triple have no side effects.
1636 * I.e. Rexecuting the triple with the same arguments
1637 * gives the same value.
1640 valid_ins(state, ins);
1641 pure = PURE_BITS(table_ops[ins->op].flags);
1642 if ((pure != PURE) && (pure != IMPURE)) {
1643 internal_error(state, 0, "Purity of %s not known\n",
1646 return pure == PURE;
1649 static int triple_is_branch(struct compile_state *state, struct triple *ins)
1651 /* This function is used to determine which triples need
1655 valid_ins(state, ins);
1656 is_branch = (table_ops[ins->op].targ != 0);
1660 static int triple_is_cond_branch(struct compile_state *state, struct triple *ins)
1662 /* A conditional branch has the condition argument as a single
1665 return triple_is_branch(state, ins) &&
1666 (TRIPLE_RHS(ins->sizes) == 1);
1669 static int triple_is_uncond_branch(struct compile_state *state, struct triple *ins)
1671 /* A unconditional branch has no RHS parameters.
1673 return triple_is_branch(state, ins) &&
1674 (TRIPLE_RHS(ins->sizes) == 0);
1677 static int triple_is_def(struct compile_state *state, struct triple *ins)
1679 /* This function is used to determine which triples need
1683 valid_ins(state, ins);
1684 is_def = (table_ops[ins->op].flags & DEF) == DEF;
1688 static struct triple **triple_iter(struct compile_state *state,
1689 size_t count, struct triple **vector,
1690 struct triple *ins, struct triple **last)
1692 struct triple **ret;
1698 else if ((last >= vector) && (last < (vector + count - 1))) {
1706 static struct triple **triple_lhs(struct compile_state *state,
1707 struct triple *ins, struct triple **last)
1709 return triple_iter(state, TRIPLE_LHS(ins->sizes), &LHS(ins,0),
1713 static struct triple **triple_rhs(struct compile_state *state,
1714 struct triple *ins, struct triple **last)
1716 return triple_iter(state, TRIPLE_RHS(ins->sizes), &RHS(ins,0),
1720 static struct triple **triple_misc(struct compile_state *state,
1721 struct triple *ins, struct triple **last)
1723 return triple_iter(state, TRIPLE_MISC(ins->sizes), &MISC(ins,0),
1727 static struct triple **triple_targ(struct compile_state *state,
1728 struct triple *ins, struct triple **last)
1731 struct triple **ret, **vector;
1733 count = TRIPLE_TARG(ins->sizes);
1734 vector = &TARG(ins, 0);
1739 else if ((last >= vector) && (last < (vector + count - 1))) {
1742 else if ((last == (vector + count - 1)) &&
1743 TRIPLE_RHS(ins->sizes)) {
1751 static void verify_use(struct compile_state *state,
1752 struct triple *user, struct triple *used)
1755 size = TRIPLE_SIZE(user->sizes);
1756 for(i = 0; i < size; i++) {
1757 if (user->param[i] == used) {
1761 if (triple_is_branch(state, user)) {
1762 if (user->next == used) {
1767 internal_error(state, user, "%s(%p) does not use %s(%p)",
1768 tops(user->op), user, tops(used->op), used);
1772 static int find_rhs_use(struct compile_state *state,
1773 struct triple *user, struct triple *used)
1775 struct triple **param;
1777 verify_use(state, user, used);
1778 size = TRIPLE_RHS(user->sizes);
1779 param = &RHS(user, 0);
1780 for(i = 0; i < size; i++) {
1781 if (param[i] == used) {
1788 static void free_triple(struct compile_state *state, struct triple *ptr)
1791 size = sizeof(*ptr) - sizeof(ptr->param) +
1792 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr->sizes));
1793 ptr->prev->next = ptr->next;
1794 ptr->next->prev = ptr->prev;
1796 internal_error(state, ptr, "ptr->use != 0");
1798 put_occurance(ptr->occurance);
1799 memset(ptr, -1, size);
1803 static void release_triple(struct compile_state *state, struct triple *ptr)
1805 struct triple_set *set, *next;
1806 struct triple **expr;
1807 struct block *block;
1808 /* Make certain the we are not the first or last element of a block */
1809 block = block_of_triple(state, ptr);
1810 if (block && (block->last == ptr)) {
1811 block->last = ptr->prev;
1813 if (block && (block->first == ptr)) {
1814 block->first = ptr->next;
1816 /* Remove ptr from use chains where it is the user */
1817 expr = triple_rhs(state, ptr, 0);
1818 for(; expr; expr = triple_rhs(state, ptr, expr)) {
1820 unuse_triple(*expr, ptr);
1823 expr = triple_lhs(state, ptr, 0);
1824 for(; expr; expr = triple_lhs(state, ptr, expr)) {
1826 unuse_triple(*expr, ptr);
1829 expr = triple_misc(state, ptr, 0);
1830 for(; expr; expr = triple_misc(state, ptr, expr)) {
1832 unuse_triple(*expr, ptr);
1835 expr = triple_targ(state, ptr, 0);
1836 for(; expr; expr = triple_targ(state, ptr, expr)) {
1838 unuse_triple(*expr, ptr);
1841 /* Reomve ptr from use chains where it is used */
1842 for(set = ptr->use; set; set = next) {
1844 expr = triple_rhs(state, set->member, 0);
1845 for(; expr; expr = triple_rhs(state, set->member, expr)) {
1847 *expr = &zero_triple;
1850 expr = triple_lhs(state, set->member, 0);
1851 for(; expr; expr = triple_lhs(state, set->member, expr)) {
1853 *expr = &zero_triple;
1856 expr = triple_misc(state, set->member, 0);
1857 for(; expr; expr = triple_misc(state, set->member, expr)) {
1859 *expr = &zero_triple;
1862 expr = triple_targ(state, set->member, 0);
1863 for(; expr; expr = triple_targ(state, set->member, expr)) {
1865 *expr = &zero_triple;
1868 unuse_triple(ptr, set->member);
1870 free_triple(state, ptr);
1873 static void print_triple(struct compile_state *state, struct triple *ptr);
1875 #define TOK_UNKNOWN 0
1878 #define TOK_LBRACE 3
1879 #define TOK_RBRACE 4
1883 #define TOK_LBRACKET 8
1884 #define TOK_RBRACKET 9
1885 #define TOK_LPAREN 10
1886 #define TOK_RPAREN 11
1891 #define TOK_TIMESEQ 16
1892 #define TOK_DIVEQ 17
1893 #define TOK_MODEQ 18
1894 #define TOK_PLUSEQ 19
1895 #define TOK_MINUSEQ 20
1898 #define TOK_ANDEQ 23
1899 #define TOK_XOREQ 24
1902 #define TOK_NOTEQ 27
1903 #define TOK_QUEST 28
1904 #define TOK_LOGOR 29
1905 #define TOK_LOGAND 30
1909 #define TOK_LESSEQ 34
1910 #define TOK_MOREEQ 35
1914 #define TOK_MINUS 39
1917 #define TOK_PLUSPLUS 42
1918 #define TOK_MINUSMINUS 43
1920 #define TOK_ARROW 45
1922 #define TOK_TILDE 47
1923 #define TOK_LIT_STRING 48
1924 #define TOK_LIT_CHAR 49
1925 #define TOK_LIT_INT 50
1926 #define TOK_LIT_FLOAT 51
1927 #define TOK_MACRO 52
1928 #define TOK_CONCATENATE 53
1930 #define TOK_IDENT 54
1931 #define TOK_STRUCT_NAME 55
1932 #define TOK_ENUM_CONST 56
1933 #define TOK_TYPE_NAME 57
1936 #define TOK_BREAK 59
1939 #define TOK_CONST 62
1940 #define TOK_CONTINUE 63
1941 #define TOK_DEFAULT 64
1943 #define TOK_DOUBLE 66
1946 #define TOK_EXTERN 69
1947 #define TOK_FLOAT 70
1951 #define TOK_INLINE 74
1954 #define TOK_REGISTER 77
1955 #define TOK_RESTRICT 78
1956 #define TOK_RETURN 79
1957 #define TOK_SHORT 80
1958 #define TOK_SIGNED 81
1959 #define TOK_SIZEOF 82
1960 #define TOK_STATIC 83
1961 #define TOK_STRUCT 84
1962 #define TOK_SWITCH 85
1963 #define TOK_TYPEDEF 86
1964 #define TOK_UNION 87
1965 #define TOK_UNSIGNED 88
1967 #define TOK_VOLATILE 90
1968 #define TOK_WHILE 91
1970 #define TOK_ATTRIBUTE 93
1971 #define TOK_ALIGNOF 94
1972 #define TOK_FIRST_KEYWORD TOK_AUTO
1973 #define TOK_LAST_KEYWORD TOK_ALIGNOF
1975 #define TOK_DEFINE 100
1976 #define TOK_UNDEF 101
1977 #define TOK_INCLUDE 102
1978 #define TOK_LINE 103
1979 #define TOK_ERROR 104
1980 #define TOK_WARNING 105
1981 #define TOK_PRAGMA 106
1982 #define TOK_IFDEF 107
1983 #define TOK_IFNDEF 108
1984 #define TOK_ELIF 109
1985 #define TOK_ENDIF 110
1987 #define TOK_FIRST_MACRO TOK_DEFINE
1988 #define TOK_LAST_MACRO TOK_ENDIF
1992 static const char *tokens[] = {
1993 [TOK_UNKNOWN ] = "unknown",
1994 [TOK_SPACE ] = ":space:",
1996 [TOK_LBRACE ] = "{",
1997 [TOK_RBRACE ] = "}",
2001 [TOK_LBRACKET ] = "[",
2002 [TOK_RBRACKET ] = "]",
2003 [TOK_LPAREN ] = "(",
2004 [TOK_RPAREN ] = ")",
2006 [TOK_DOTS ] = "...",
2009 [TOK_TIMESEQ ] = "*=",
2010 [TOK_DIVEQ ] = "/=",
2011 [TOK_MODEQ ] = "%=",
2012 [TOK_PLUSEQ ] = "+=",
2013 [TOK_MINUSEQ ] = "-=",
2014 [TOK_SLEQ ] = "<<=",
2015 [TOK_SREQ ] = ">>=",
2016 [TOK_ANDEQ ] = "&=",
2017 [TOK_XOREQ ] = "^=",
2020 [TOK_NOTEQ ] = "!=",
2022 [TOK_LOGOR ] = "||",
2023 [TOK_LOGAND ] = "&&",
2027 [TOK_LESSEQ ] = "<=",
2028 [TOK_MOREEQ ] = ">=",
2035 [TOK_PLUSPLUS ] = "++",
2036 [TOK_MINUSMINUS ] = "--",
2038 [TOK_ARROW ] = "->",
2041 [TOK_LIT_STRING ] = ":string:",
2042 [TOK_IDENT ] = ":ident:",
2043 [TOK_TYPE_NAME ] = ":typename:",
2044 [TOK_LIT_CHAR ] = ":char:",
2045 [TOK_LIT_INT ] = ":integer:",
2046 [TOK_LIT_FLOAT ] = ":float:",
2048 [TOK_CONCATENATE ] = "##",
2050 [TOK_AUTO ] = "auto",
2051 [TOK_BREAK ] = "break",
2052 [TOK_CASE ] = "case",
2053 [TOK_CHAR ] = "char",
2054 [TOK_CONST ] = "const",
2055 [TOK_CONTINUE ] = "continue",
2056 [TOK_DEFAULT ] = "default",
2058 [TOK_DOUBLE ] = "double",
2059 [TOK_ELSE ] = "else",
2060 [TOK_ENUM ] = "enum",
2061 [TOK_EXTERN ] = "extern",
2062 [TOK_FLOAT ] = "float",
2064 [TOK_GOTO ] = "goto",
2066 [TOK_INLINE ] = "inline",
2068 [TOK_LONG ] = "long",
2069 [TOK_REGISTER ] = "register",
2070 [TOK_RESTRICT ] = "restrict",
2071 [TOK_RETURN ] = "return",
2072 [TOK_SHORT ] = "short",
2073 [TOK_SIGNED ] = "signed",
2074 [TOK_SIZEOF ] = "sizeof",
2075 [TOK_STATIC ] = "static",
2076 [TOK_STRUCT ] = "struct",
2077 [TOK_SWITCH ] = "switch",
2078 [TOK_TYPEDEF ] = "typedef",
2079 [TOK_UNION ] = "union",
2080 [TOK_UNSIGNED ] = "unsigned",
2081 [TOK_VOID ] = "void",
2082 [TOK_VOLATILE ] = "volatile",
2083 [TOK_WHILE ] = "while",
2085 [TOK_ATTRIBUTE ] = "__attribute__",
2086 [TOK_ALIGNOF ] = "__alignof__",
2088 [TOK_DEFINE ] = "define",
2089 [TOK_UNDEF ] = "undef",
2090 [TOK_INCLUDE ] = "include",
2091 [TOK_LINE ] = "line",
2092 [TOK_ERROR ] = "error",
2093 [TOK_WARNING ] = "warning",
2094 [TOK_PRAGMA ] = "pragma",
2095 [TOK_IFDEF ] = "ifdef",
2096 [TOK_IFNDEF ] = "ifndef",
2097 [TOK_ELIF ] = "elif",
2098 [TOK_ENDIF ] = "endif",
2103 static unsigned int hash(const char *str, int str_len)
2107 end = str + str_len;
2109 for(; str < end; str++) {
2110 hash = (hash *263) + *str;
2112 hash = hash & (HASH_TABLE_SIZE -1);
2116 static struct hash_entry *lookup(
2117 struct compile_state *state, const char *name, int name_len)
2119 struct hash_entry *entry;
2121 index = hash(name, name_len);
2122 entry = state->hash_table[index];
2124 ((entry->name_len != name_len) ||
2125 (memcmp(entry->name, name, name_len) != 0))) {
2126 entry = entry->next;
2130 /* Get a private copy of the name */
2131 new_name = xmalloc(name_len + 1, "hash_name");
2132 memcpy(new_name, name, name_len);
2133 new_name[name_len] = '\0';
2135 /* Create a new hash entry */
2136 entry = xcmalloc(sizeof(*entry), "hash_entry");
2137 entry->next = state->hash_table[index];
2138 entry->name = new_name;
2139 entry->name_len = name_len;
2141 /* Place the new entry in the hash table */
2142 state->hash_table[index] = entry;
2147 static void ident_to_keyword(struct compile_state *state, struct token *tk)
2149 struct hash_entry *entry;
2151 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
2152 (entry->tok == TOK_ENUM_CONST) ||
2153 ((entry->tok >= TOK_FIRST_KEYWORD) &&
2154 (entry->tok <= TOK_LAST_KEYWORD)))) {
2155 tk->tok = entry->tok;
2159 static void ident_to_macro(struct compile_state *state, struct token *tk)
2161 struct hash_entry *entry;
2164 (entry->tok >= TOK_FIRST_MACRO) &&
2165 (entry->tok <= TOK_LAST_MACRO)) {
2166 tk->tok = entry->tok;
2170 static void hash_keyword(
2171 struct compile_state *state, const char *keyword, int tok)
2173 struct hash_entry *entry;
2174 entry = lookup(state, keyword, strlen(keyword));
2175 if (entry && entry->tok != TOK_UNKNOWN) {
2176 die("keyword %s already hashed", keyword);
2182 struct compile_state *state, struct hash_entry *ident,
2183 struct symbol **chain, struct triple *def, struct type *type)
2186 if (*chain && ((*chain)->scope_depth == state->scope_depth)) {
2187 error(state, 0, "%s already defined", ident->name);
2189 sym = xcmalloc(sizeof(*sym), "symbol");
2193 sym->scope_depth = state->scope_depth;
2198 static void label_symbol(struct compile_state *state,
2199 struct hash_entry *ident, struct triple *label)
2202 if (ident->sym_label) {
2203 error(state, 0, "label %s already defined", ident->name);
2205 sym = xcmalloc(sizeof(*sym), "label");
2208 sym->type = &void_type;
2209 sym->scope_depth = FUNCTION_SCOPE_DEPTH;
2211 ident->sym_label = sym;
2214 static void start_scope(struct compile_state *state)
2216 state->scope_depth++;
2219 static void end_scope_syms(struct symbol **chain, int depth)
2221 struct symbol *sym, *next;
2223 while(sym && (sym->scope_depth == depth)) {
2231 static void end_scope(struct compile_state *state)
2235 /* Walk through the hash table and remove all symbols
2236 * in the current scope.
2238 depth = state->scope_depth;
2239 for(i = 0; i < HASH_TABLE_SIZE; i++) {
2240 struct hash_entry *entry;
2241 entry = state->hash_table[i];
2243 end_scope_syms(&entry->sym_label, depth);
2244 end_scope_syms(&entry->sym_struct, depth);
2245 end_scope_syms(&entry->sym_ident, depth);
2246 entry = entry->next;
2249 state->scope_depth = depth - 1;
2252 static void register_keywords(struct compile_state *state)
2254 hash_keyword(state, "auto", TOK_AUTO);
2255 hash_keyword(state, "break", TOK_BREAK);
2256 hash_keyword(state, "case", TOK_CASE);
2257 hash_keyword(state, "char", TOK_CHAR);
2258 hash_keyword(state, "const", TOK_CONST);
2259 hash_keyword(state, "continue", TOK_CONTINUE);
2260 hash_keyword(state, "default", TOK_DEFAULT);
2261 hash_keyword(state, "do", TOK_DO);
2262 hash_keyword(state, "double", TOK_DOUBLE);
2263 hash_keyword(state, "else", TOK_ELSE);
2264 hash_keyword(state, "enum", TOK_ENUM);
2265 hash_keyword(state, "extern", TOK_EXTERN);
2266 hash_keyword(state, "float", TOK_FLOAT);
2267 hash_keyword(state, "for", TOK_FOR);
2268 hash_keyword(state, "goto", TOK_GOTO);
2269 hash_keyword(state, "if", TOK_IF);
2270 hash_keyword(state, "inline", TOK_INLINE);
2271 hash_keyword(state, "int", TOK_INT);
2272 hash_keyword(state, "long", TOK_LONG);
2273 hash_keyword(state, "register", TOK_REGISTER);
2274 hash_keyword(state, "restrict", TOK_RESTRICT);
2275 hash_keyword(state, "return", TOK_RETURN);
2276 hash_keyword(state, "short", TOK_SHORT);
2277 hash_keyword(state, "signed", TOK_SIGNED);
2278 hash_keyword(state, "sizeof", TOK_SIZEOF);
2279 hash_keyword(state, "static", TOK_STATIC);
2280 hash_keyword(state, "struct", TOK_STRUCT);
2281 hash_keyword(state, "switch", TOK_SWITCH);
2282 hash_keyword(state, "typedef", TOK_TYPEDEF);
2283 hash_keyword(state, "union", TOK_UNION);
2284 hash_keyword(state, "unsigned", TOK_UNSIGNED);
2285 hash_keyword(state, "void", TOK_VOID);
2286 hash_keyword(state, "volatile", TOK_VOLATILE);
2287 hash_keyword(state, "__volatile__", TOK_VOLATILE);
2288 hash_keyword(state, "while", TOK_WHILE);
2289 hash_keyword(state, "asm", TOK_ASM);
2290 hash_keyword(state, "__asm__", TOK_ASM);
2291 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
2292 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
2295 static void register_macro_keywords(struct compile_state *state)
2297 hash_keyword(state, "define", TOK_DEFINE);
2298 hash_keyword(state, "undef", TOK_UNDEF);
2299 hash_keyword(state, "include", TOK_INCLUDE);
2300 hash_keyword(state, "line", TOK_LINE);
2301 hash_keyword(state, "error", TOK_ERROR);
2302 hash_keyword(state, "warning", TOK_WARNING);
2303 hash_keyword(state, "pragma", TOK_PRAGMA);
2304 hash_keyword(state, "ifdef", TOK_IFDEF);
2305 hash_keyword(state, "ifndef", TOK_IFNDEF);
2306 hash_keyword(state, "elif", TOK_ELIF);
2307 hash_keyword(state, "endif", TOK_ENDIF);
2310 static int spacep(int c)
2326 static int digitp(int c)
2330 case '0': case '1': case '2': case '3': case '4':
2331 case '5': case '6': case '7': case '8': case '9':
2337 static int digval(int c)
2340 if ((c >= '0') && (c <= '9')) {
2346 static int hexdigitp(int c)
2350 case '0': case '1': case '2': case '3': case '4':
2351 case '5': case '6': case '7': case '8': case '9':
2352 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2353 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2359 static int hexdigval(int c)
2362 if ((c >= '0') && (c <= '9')) {
2365 else if ((c >= 'A') && (c <= 'F')) {
2366 val = 10 + (c - 'A');
2368 else if ((c >= 'a') && (c <= 'f')) {
2369 val = 10 + (c - 'a');
2374 static int octdigitp(int c)
2378 case '0': case '1': case '2': case '3':
2379 case '4': case '5': case '6': case '7':
2385 static int octdigval(int c)
2388 if ((c >= '0') && (c <= '7')) {
2394 static int letterp(int c)
2398 case 'a': case 'b': case 'c': case 'd': case 'e':
2399 case 'f': case 'g': case 'h': case 'i': case 'j':
2400 case 'k': case 'l': case 'm': case 'n': case 'o':
2401 case 'p': case 'q': case 'r': case 's': case 't':
2402 case 'u': case 'v': case 'w': case 'x': case 'y':
2404 case 'A': case 'B': case 'C': case 'D': case 'E':
2405 case 'F': case 'G': case 'H': case 'I': case 'J':
2406 case 'K': case 'L': case 'M': case 'N': case 'O':
2407 case 'P': case 'Q': case 'R': case 'S': case 'T':
2408 case 'U': case 'V': case 'W': case 'X': case 'Y':
2417 static int char_value(struct compile_state *state,
2418 const signed char **strp, const signed char *end)
2420 const signed char *str;
2424 if ((c == '\\') && (str < end)) {
2426 case 'n': c = '\n'; str++; break;
2427 case 't': c = '\t'; str++; break;
2428 case 'v': c = '\v'; str++; break;
2429 case 'b': c = '\b'; str++; break;
2430 case 'r': c = '\r'; str++; break;
2431 case 'f': c = '\f'; str++; break;
2432 case 'a': c = '\a'; str++; break;
2433 case '\\': c = '\\'; str++; break;
2434 case '?': c = '?'; str++; break;
2435 case '\'': c = '\''; str++; break;
2436 case '"': c = '"'; break;
2440 while((str < end) && hexdigitp(*str)) {
2442 c += hexdigval(*str);
2446 case '0': case '1': case '2': case '3':
2447 case '4': case '5': case '6': case '7':
2449 while((str < end) && octdigitp(*str)) {
2451 c += octdigval(*str);
2456 error(state, 0, "Invalid character constant");
2464 static char *after_digits(char *ptr, char *end)
2466 while((ptr < end) && digitp(*ptr)) {
2472 static char *after_octdigits(char *ptr, char *end)
2474 while((ptr < end) && octdigitp(*ptr)) {
2480 static char *after_hexdigits(char *ptr, char *end)
2482 while((ptr < end) && hexdigitp(*ptr)) {
2488 static void save_string(struct compile_state *state,
2489 struct token *tk, char *start, char *end, const char *id)
2493 /* Create a private copy of the string */
2494 str_len = end - start + 1;
2495 str = xmalloc(str_len + 1, id);
2496 memcpy(str, start, str_len);
2497 str[str_len] = '\0';
2499 /* Store the copy in the token */
2501 tk->str_len = str_len;
2503 static void next_token(struct compile_state *state, int index)
2505 struct file_state *file;
2513 tk = &state->token[index];
2516 token = tokp = file->pos;
2517 end = file->buf + file->size;
2524 if ((tokp + 1) < end) {
2528 if ((tokp + 2) < end) {
2532 if ((tokp + 3) < end) {
2540 else if (spacep(c)) {
2542 while ((tokp < end) && spacep(c)) {
2545 file->report_line++;
2546 file->line_start = tokp + 1;
2555 else if ((c == '/') && (c1 == '/')) {
2557 for(tokp += 2; tokp < end; tokp++) {
2561 file->report_line++;
2562 file->line_start = tokp +1;
2568 else if ((c == '/') && (c1 == '*')) {
2572 line_start = file->line_start;
2573 for(tokp += 2; (end - tokp) >= 2; tokp++) {
2577 line_start = tokp +1;
2579 else if ((c == '*') && (tokp[1] == '/')) {
2585 if (tok == TOK_UNKNOWN) {
2586 error(state, 0, "unterminated comment");
2588 file->report_line += line - file->line;
2590 file->line_start = line_start;
2592 /* string constants */
2593 else if ((c == '"') ||
2594 ((c == 'L') && (c1 == '"'))) {
2599 line_start = file->line_start;
2605 for(tokp += 1; tokp < end; tokp++) {
2609 line_start = tokp + 1;
2611 else if ((c == '\\') && (tokp +1 < end)) {
2614 else if (c == '"') {
2615 tok = TOK_LIT_STRING;
2619 if (tok == TOK_UNKNOWN) {
2620 error(state, 0, "unterminated string constant");
2622 if (line != file->line) {
2623 warning(state, 0, "multiline string constant");
2625 file->report_line += line - file->line;
2627 file->line_start = line_start;
2629 /* Save the string value */
2630 save_string(state, tk, token, tokp, "literal string");
2632 /* character constants */
2633 else if ((c == '\'') ||
2634 ((c == 'L') && (c1 == '\''))) {
2639 line_start = file->line_start;
2645 for(tokp += 1; tokp < end; tokp++) {
2649 line_start = tokp + 1;
2651 else if ((c == '\\') && (tokp +1 < end)) {
2654 else if (c == '\'') {
2659 if (tok == TOK_UNKNOWN) {
2660 error(state, 0, "unterminated character constant");
2662 if (line != file->line) {
2663 warning(state, 0, "multiline character constant");
2665 file->report_line += line - file->line;
2667 file->line_start = line_start;
2669 /* Save the character value */
2670 save_string(state, tk, token, tokp, "literal character");
2672 /* integer and floating constants
2678 * Floating constants
2679 * {digits}.{digits}[Ee][+-]?{digits}
2681 * {digits}[Ee][+-]?{digits}
2682 * .{digits}[Ee][+-]?{digits}
2686 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
2691 next = after_digits(tokp, end);
2696 if (next[0] == '.') {
2697 new = after_digits(next, end);
2698 is_float = (new != next);
2701 if ((next[0] == 'e') || (next[0] == 'E')) {
2702 if (((next + 1) < end) &&
2703 ((next[1] == '+') || (next[1] == '-'))) {
2706 new = after_digits(next, end);
2707 is_float = (new != next);
2711 tok = TOK_LIT_FLOAT;
2712 if ((next < end) && (
2721 if (!is_float && digitp(c)) {
2723 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
2724 next = after_hexdigits(tokp + 2, end);
2726 else if (c == '0') {
2727 next = after_octdigits(tokp, end);
2730 next = after_digits(tokp, end);
2732 /* crazy integer suffixes */
2734 ((next[0] == 'u') || (next[0] == 'U'))) {
2737 ((next[0] == 'l') || (next[0] == 'L'))) {
2741 else if ((next < end) &&
2742 ((next[0] == 'l') || (next[0] == 'L'))) {
2745 ((next[0] == 'u') || (next[0] == 'U'))) {
2752 /* Save the integer/floating point value */
2753 save_string(state, tk, token, tokp, "literal number");
2756 else if (letterp(c)) {
2758 for(tokp += 1; tokp < end; tokp++) {
2760 if (!letterp(c) && !digitp(c)) {
2765 tk->ident = lookup(state, token, tokp +1 - token);
2767 /* C99 alternate macro characters */
2768 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
2770 tok = TOK_CONCATENATE;
2772 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { tokp += 2; tok = TOK_DOTS; }
2773 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { tokp += 2; tok = TOK_SLEQ; }
2774 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { tokp += 2; tok = TOK_SREQ; }
2775 else if ((c == '*') && (c1 == '=')) { tokp += 1; tok = TOK_TIMESEQ; }
2776 else if ((c == '/') && (c1 == '=')) { tokp += 1; tok = TOK_DIVEQ; }
2777 else if ((c == '%') && (c1 == '=')) { tokp += 1; tok = TOK_MODEQ; }
2778 else if ((c == '+') && (c1 == '=')) { tokp += 1; tok = TOK_PLUSEQ; }
2779 else if ((c == '-') && (c1 == '=')) { tokp += 1; tok = TOK_MINUSEQ; }
2780 else if ((c == '&') && (c1 == '=')) { tokp += 1; tok = TOK_ANDEQ; }
2781 else if ((c == '^') && (c1 == '=')) { tokp += 1; tok = TOK_XOREQ; }
2782 else if ((c == '|') && (c1 == '=')) { tokp += 1; tok = TOK_OREQ; }
2783 else if ((c == '=') && (c1 == '=')) { tokp += 1; tok = TOK_EQEQ; }
2784 else if ((c == '!') && (c1 == '=')) { tokp += 1; tok = TOK_NOTEQ; }
2785 else if ((c == '|') && (c1 == '|')) { tokp += 1; tok = TOK_LOGOR; }
2786 else if ((c == '&') && (c1 == '&')) { tokp += 1; tok = TOK_LOGAND; }
2787 else if ((c == '<') && (c1 == '=')) { tokp += 1; tok = TOK_LESSEQ; }
2788 else if ((c == '>') && (c1 == '=')) { tokp += 1; tok = TOK_MOREEQ; }
2789 else if ((c == '<') && (c1 == '<')) { tokp += 1; tok = TOK_SL; }
2790 else if ((c == '>') && (c1 == '>')) { tokp += 1; tok = TOK_SR; }
2791 else if ((c == '+') && (c1 == '+')) { tokp += 1; tok = TOK_PLUSPLUS; }
2792 else if ((c == '-') && (c1 == '-')) { tokp += 1; tok = TOK_MINUSMINUS; }
2793 else if ((c == '-') && (c1 == '>')) { tokp += 1; tok = TOK_ARROW; }
2794 else if ((c == '<') && (c1 == ':')) { tokp += 1; tok = TOK_LBRACKET; }
2795 else if ((c == ':') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACKET; }
2796 else if ((c == '<') && (c1 == '%')) { tokp += 1; tok = TOK_LBRACE; }
2797 else if ((c == '%') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACE; }
2798 else if ((c == '%') && (c1 == ':')) { tokp += 1; tok = TOK_MACRO; }
2799 else if ((c == '#') && (c1 == '#')) { tokp += 1; tok = TOK_CONCATENATE; }
2800 else if (c == ';') { tok = TOK_SEMI; }
2801 else if (c == '{') { tok = TOK_LBRACE; }
2802 else if (c == '}') { tok = TOK_RBRACE; }
2803 else if (c == ',') { tok = TOK_COMMA; }
2804 else if (c == '=') { tok = TOK_EQ; }
2805 else if (c == ':') { tok = TOK_COLON; }
2806 else if (c == '[') { tok = TOK_LBRACKET; }
2807 else if (c == ']') { tok = TOK_RBRACKET; }
2808 else if (c == '(') { tok = TOK_LPAREN; }
2809 else if (c == ')') { tok = TOK_RPAREN; }
2810 else if (c == '*') { tok = TOK_STAR; }
2811 else if (c == '>') { tok = TOK_MORE; }
2812 else if (c == '<') { tok = TOK_LESS; }
2813 else if (c == '?') { tok = TOK_QUEST; }
2814 else if (c == '|') { tok = TOK_OR; }
2815 else if (c == '&') { tok = TOK_AND; }
2816 else if (c == '^') { tok = TOK_XOR; }
2817 else if (c == '+') { tok = TOK_PLUS; }
2818 else if (c == '-') { tok = TOK_MINUS; }
2819 else if (c == '/') { tok = TOK_DIV; }
2820 else if (c == '%') { tok = TOK_MOD; }
2821 else if (c == '!') { tok = TOK_BANG; }
2822 else if (c == '.') { tok = TOK_DOT; }
2823 else if (c == '~') { tok = TOK_TILDE; }
2824 else if (c == '#') { tok = TOK_MACRO; }
2825 if (tok == TOK_MACRO) {
2826 /* Only match preprocessor directives at the start of a line */
2828 for(ptr = file->line_start; spacep(*ptr); ptr++)
2834 if (tok == TOK_UNKNOWN) {
2835 error(state, 0, "unknown token");
2838 file->pos = tokp + 1;
2840 if (tok == TOK_IDENT) {
2841 ident_to_keyword(state, tk);
2843 /* Don't return space tokens. */
2844 if (tok == TOK_SPACE) {
2849 static void compile_macro(struct compile_state *state, struct token *tk)
2851 struct file_state *file;
2852 struct hash_entry *ident;
2854 file = xmalloc(sizeof(*file), "file_state");
2855 file->basename = xstrdup(tk->ident->name);
2856 file->dirname = xstrdup("");
2857 file->size = ident->sym_define->buf_len;
2858 file->buf = xmalloc(file->size +2, file->basename);
2859 memcpy(file->buf, ident->sym_define->buf, file->size);
2860 file->buf[file->size] = '\n';
2861 file->buf[file->size + 1] = '\0';
2862 file->pos = file->buf;
2863 file->line_start = file->pos;
2865 file->report_line = 1;
2866 file->report_name = file->basename;
2867 file->report_dir = file->dirname;
2868 file->prev = state->file;
2873 static int mpeek(struct compile_state *state, int index)
2877 tk = &state->token[index + 1];
2878 if (tk->tok == -1) {
2879 next_token(state, index + 1);
2883 if ((tk->tok == TOK_EOF) &&
2884 (state->file != state->macro_file) &&
2885 (state->file->prev)) {
2886 struct file_state *file = state->file;
2887 state->file = file->prev;
2888 /* file->basename is used keep it */
2889 if (file->report_dir != file->dirname) {
2890 xfree(file->report_dir);
2892 xfree(file->dirname);
2895 next_token(state, index + 1);
2898 else if (tk->ident && tk->ident->sym_define) {
2899 compile_macro(state, tk);
2900 next_token(state, index + 1);
2904 /* Don't show the token on the next line */
2905 if (state->macro_line < state->macro_file->line) {
2908 return state->token[index +1].tok;
2911 static void meat(struct compile_state *state, int index, int tok)
2915 next_tok = mpeek(state, index);
2916 if (next_tok != tok) {
2917 const char *name1, *name2;
2918 name1 = tokens[next_tok];
2920 if (next_tok == TOK_IDENT) {
2921 name2 = state->token[index + 1].ident->name;
2923 error(state, 0, "found %s %s expected %s",
2924 name1, name2, tokens[tok]);
2926 /* Free the old token value */
2927 if (state->token[index].str_len) {
2928 memset((void *)(state->token[index].val.str), -1,
2929 state->token[index].str_len);
2930 xfree(state->token[index].val.str);
2932 for(i = index; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
2933 state->token[i] = state->token[i + 1];
2935 memset(&state->token[i], 0, sizeof(state->token[i]));
2936 state->token[i].tok = -1;
2939 static long_t mcexpr(struct compile_state *state, int index);
2941 static long_t mprimary_expr(struct compile_state *state, int index)
2945 tok = mpeek(state, index);
2946 while(state->token[index + 1].ident &&
2947 state->token[index + 1].ident->sym_define) {
2948 meat(state, index, tok);
2949 compile_macro(state, &state->token[index]);
2950 tok = mpeek(state, index);
2954 meat(state, index, TOK_LPAREN);
2955 val = mcexpr(state, index);
2956 meat(state, index, TOK_RPAREN);
2961 meat(state, index, TOK_LIT_INT);
2963 val = strtol(state->token[index].val.str, &end, 0);
2965 if (((val == INT_MIN) || (val == INT_MAX)) &&
2966 (errno == ERANGE)) {
2968 if (((val == LONG_MIN) || (val == LONG_MAX)) &&
2969 (errno == ERANGE)) {
2971 error(state, 0, "Integer constant to large");
2976 meat(state, index, TOK_LIT_INT);
2981 static long_t munary_expr(struct compile_state *state, int index)
2984 switch(mpeek(state, index)) {
2986 meat(state, index, TOK_PLUS);
2987 val = munary_expr(state, index);
2991 meat(state, index, TOK_MINUS);
2992 val = munary_expr(state, index);
2996 meat(state, index, TOK_BANG);
2997 val = munary_expr(state, index);
3001 meat(state, index, TOK_BANG);
3002 val = munary_expr(state, index);
3006 val = mprimary_expr(state, index);
3012 static long_t mmul_expr(struct compile_state *state, int index)
3016 val = munary_expr(state, index);
3020 switch(mpeek(state, index)) {
3022 meat(state, index, TOK_STAR);
3023 right = munary_expr(state, index);
3027 meat(state, index, TOK_DIV);
3028 right = munary_expr(state, index);
3032 meat(state, index, TOK_MOD);
3033 right = munary_expr(state, index);
3045 static long_t madd_expr(struct compile_state *state, int index)
3049 val = mmul_expr(state, index);
3053 switch(mpeek(state, index)) {
3055 meat(state, index, TOK_PLUS);
3056 right = mmul_expr(state, index);
3060 meat(state, index, TOK_MINUS);
3061 right = mmul_expr(state, index);
3073 static long_t mshift_expr(struct compile_state *state, int index)
3077 val = madd_expr(state, index);
3081 switch(mpeek(state, index)) {
3083 meat(state, index, TOK_SL);
3084 right = madd_expr(state, index);
3088 meat(state, index, TOK_SR);
3089 right = madd_expr(state, index);
3101 static long_t mrel_expr(struct compile_state *state, int index)
3105 val = mshift_expr(state, index);
3109 switch(mpeek(state, index)) {
3111 meat(state, index, TOK_LESS);
3112 right = mshift_expr(state, index);
3116 meat(state, index, TOK_MORE);
3117 right = mshift_expr(state, index);
3121 meat(state, index, TOK_LESSEQ);
3122 right = mshift_expr(state, index);
3126 meat(state, index, TOK_MOREEQ);
3127 right = mshift_expr(state, index);
3138 static long_t meq_expr(struct compile_state *state, int index)
3142 val = mrel_expr(state, index);
3146 switch(mpeek(state, index)) {
3148 meat(state, index, TOK_EQEQ);
3149 right = mrel_expr(state, index);
3153 meat(state, index, TOK_NOTEQ);
3154 right = mrel_expr(state, index);
3165 static long_t mand_expr(struct compile_state *state, int index)
3168 val = meq_expr(state, index);
3169 if (mpeek(state, index) == TOK_AND) {
3171 meat(state, index, TOK_AND);
3172 right = meq_expr(state, index);
3178 static long_t mxor_expr(struct compile_state *state, int index)
3181 val = mand_expr(state, index);
3182 if (mpeek(state, index) == TOK_XOR) {
3184 meat(state, index, TOK_XOR);
3185 right = mand_expr(state, index);
3191 static long_t mor_expr(struct compile_state *state, int index)
3194 val = mxor_expr(state, index);
3195 if (mpeek(state, index) == TOK_OR) {
3197 meat(state, index, TOK_OR);
3198 right = mxor_expr(state, index);
3204 static long_t mland_expr(struct compile_state *state, int index)
3207 val = mor_expr(state, index);
3208 if (mpeek(state, index) == TOK_LOGAND) {
3210 meat(state, index, TOK_LOGAND);
3211 right = mor_expr(state, index);
3216 static long_t mlor_expr(struct compile_state *state, int index)
3219 val = mland_expr(state, index);
3220 if (mpeek(state, index) == TOK_LOGOR) {
3222 meat(state, index, TOK_LOGOR);
3223 right = mland_expr(state, index);
3229 static long_t mcexpr(struct compile_state *state, int index)
3231 return mlor_expr(state, index);
3233 static void preprocess(struct compile_state *state, int index)
3235 /* Doing much more with the preprocessor would require
3236 * a parser and a major restructuring.
3237 * Postpone that for later.
3239 struct file_state *file;
3245 tk = &state->token[index];
3246 state->macro_line = line = file->line;
3247 state->macro_file = file;
3249 next_token(state, index);
3250 ident_to_macro(state, tk);
3251 if (tk->tok == TOK_IDENT) {
3252 error(state, 0, "undefined preprocessing directive `%s'",
3259 override_line = strtoul(tk->val.str, 0, 10);
3260 next_token(state, index);
3261 /* I have a cpp line marker parse it */
3262 if (tk->tok == TOK_LIT_STRING) {
3263 const char *token, *base;
3265 int name_len, dir_len;
3266 name = xmalloc(tk->str_len, "report_name");
3267 token = tk->val.str + 1;
3268 base = strrchr(token, '/');
3269 name_len = tk->str_len -2;
3271 dir_len = base - token;
3273 name_len -= base - token;
3278 memcpy(name, base, name_len);
3279 name[name_len] = '\0';
3280 dir = xmalloc(dir_len + 1, "report_dir");
3281 memcpy(dir, token, dir_len);
3282 dir[dir_len] = '\0';
3283 file->report_line = override_line - 1;
3284 file->report_name = name;
3285 file->report_dir = dir;
3290 meat(state, index, TOK_LINE);
3291 meat(state, index, TOK_LIT_INT);
3292 file->report_line = strtoul(tk->val.str, 0, 10) -1;
3293 if (mpeek(state, index) == TOK_LIT_STRING) {
3294 const char *token, *base;
3296 int name_len, dir_len;
3297 meat(state, index, TOK_LIT_STRING);
3298 name = xmalloc(tk->str_len, "report_name");
3299 token = tk->val.str + 1;
3300 name_len = tk->str_len - 2;
3302 dir_len = base - token;
3304 name_len -= base - token;
3309 memcpy(name, base, name_len);
3310 name[name_len] = '\0';
3311 dir = xmalloc(dir_len + 1, "report_dir");
3312 memcpy(dir, token, dir_len);
3313 dir[dir_len] = '\0';
3314 file->report_name = name;
3315 file->report_dir = dir;
3320 if (state->if_value < 0) {
3323 warning(state, 0, "Ignoring preprocessor directive: %s",
3327 error(state, 0, "#elif not supported");
3328 #warning "FIXME multiple #elif and #else in an #if do not work properly"
3329 if (state->if_depth == 0) {
3330 error(state, 0, "#elif without #if");
3332 /* If the #if was taken the #elif just disables the following code */
3333 if (state->if_value >= 0) {
3334 state->if_value = - state->if_value;
3336 /* If the previous #if was not taken see if the #elif enables the
3339 else if ((state->if_value < 0) &&
3340 (state->if_depth == - state->if_value))
3342 if (mcexpr(state, index) != 0) {
3343 state->if_value = state->if_depth;
3346 state->if_value = - state->if_depth;
3352 if (state->if_value < 0) {
3355 if (mcexpr(state, index) != 0) {
3356 state->if_value = state->if_depth;
3359 state->if_value = - state->if_depth;
3364 if (state->if_value < 0) {
3367 next_token(state, index);
3368 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3369 error(state, 0, "Invalid macro name");
3371 if (tk->ident->sym_define == 0) {
3372 state->if_value = state->if_depth;
3375 state->if_value = - state->if_depth;
3380 if (state->if_value < 0) {
3383 next_token(state, index);
3384 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3385 error(state, 0, "Invalid macro name");
3387 if (tk->ident->sym_define != 0) {
3388 state->if_value = state->if_depth;
3391 state->if_value = - state->if_depth;
3395 if (state->if_depth == 0) {
3396 error(state, 0, "#else without #if");
3398 if ((state->if_value >= 0) ||
3399 ((state->if_value < 0) &&
3400 (state->if_depth == -state->if_value)))
3402 state->if_value = - state->if_value;
3406 if (state->if_depth == 0) {
3407 error(state, 0, "#endif without #if");
3409 if ((state->if_value >= 0) ||
3410 ((state->if_value < 0) &&
3411 (state->if_depth == -state->if_value)))
3413 state->if_value = state->if_depth - 1;
3419 struct hash_entry *ident;
3420 struct macro *macro;
3423 if (state->if_value < 0) /* quit early when #if'd out */
3426 meat(state, index, TOK_IDENT);
3430 if (*file->pos == '(') {
3431 #warning "FIXME macros with arguments not supported"
3432 error(state, 0, "Macros with arguments not supported");
3435 /* Find the end of the line to get an estimate of
3436 * the macro's length.
3438 for(ptr = file->pos; *ptr != '\n'; ptr++)
3441 if (ident->sym_define != 0) {
3442 error(state, 0, "macro %s already defined\n", ident->name);
3444 macro = xmalloc(sizeof(*macro), "macro");
3445 macro->ident = ident;
3446 macro->buf_len = ptr - file->pos +1;
3447 macro->buf = xmalloc(macro->buf_len +2, "macro buf");
3449 memcpy(macro->buf, file->pos, macro->buf_len);
3450 macro->buf[macro->buf_len] = '\n';
3451 macro->buf[macro->buf_len +1] = '\0';
3453 ident->sym_define = macro;
3460 /* Find the end of the line */
3461 for(end = file->pos; *end != '\n'; end++)
3463 len = (end - file->pos);
3464 if (state->if_value >= 0) {
3465 error(state, 0, "%*.*s", len, len, file->pos);
3474 /* Find the end of the line */
3475 for(end = file->pos; *end != '\n'; end++)
3477 len = (end - file->pos);
3478 if (state->if_value >= 0) {
3479 warning(state, 0, "%*.*s", len, len, file->pos);
3491 next_token(state, index);
3492 if (tk->tok == TOK_LIT_STRING) {
3495 name = xmalloc(tk->str_len, "include");
3496 token = tk->val.str +1;
3497 name_len = tk->str_len -2;
3498 if (*token == '"') {
3502 memcpy(name, token, name_len);
3503 name[name_len] = '\0';
3506 else if (tk->tok == TOK_LESS) {
3509 for(end = start; *end != '\n'; end++) {
3515 error(state, 0, "Unterminated included directive");
3517 name = xmalloc(end - start + 1, "include");
3518 memcpy(name, start, end - start);
3519 name[end - start] = '\0';
3524 error(state, 0, "Invalid include directive");
3526 /* Error if there are any characters after the include */
3527 for(ptr = file->pos; *ptr != '\n'; ptr++) {
3534 error(state, 0, "garbage after include directive");
3537 if (state->if_value >= 0) {
3538 compile_file(state, name, local);
3541 next_token(state, index);
3545 /* Ignore # without a following ident */
3546 if (tk->tok == TOK_IDENT) {
3547 error(state, 0, "Invalid preprocessor directive: %s",
3552 /* Consume the rest of the macro line */
3554 tok = mpeek(state, index);
3555 meat(state, index, tok);
3556 } while(tok != TOK_EOF);
3560 static void token(struct compile_state *state, int index)
3562 struct file_state *file;
3566 tk = &state->token[index];
3567 next_token(state, index);
3571 if (tk->tok == TOK_EOF && file->prev) {
3572 state->file = file->prev;
3573 /* file->basename is used keep it */
3574 xfree(file->dirname);
3577 next_token(state, index);
3580 else if (tk->tok == TOK_MACRO) {
3581 preprocess(state, index);
3584 else if (tk->ident && tk->ident->sym_define) {
3585 compile_macro(state, tk);
3586 next_token(state, index);
3589 else if (state->if_value < 0) {
3590 next_token(state, index);
3596 static int peek(struct compile_state *state)
3598 if (state->token[1].tok == -1) {
3601 return state->token[1].tok;
3604 static int peek2(struct compile_state *state)
3606 if (state->token[1].tok == -1) {
3609 if (state->token[2].tok == -1) {
3612 return state->token[2].tok;
3615 static void eat(struct compile_state *state, int tok)
3619 next_tok = peek(state);
3620 if (next_tok != tok) {
3621 const char *name1, *name2;
3622 name1 = tokens[next_tok];
3624 if (next_tok == TOK_IDENT) {
3625 name2 = state->token[1].ident->name;
3627 error(state, 0, "\tfound %s %s expected %s",
3628 name1, name2 ,tokens[tok]);
3630 /* Free the old token value */
3631 if (state->token[0].str_len) {
3632 xfree((void *)(state->token[0].val.str));
3634 for(i = 0; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
3635 state->token[i] = state->token[i + 1];
3637 memset(&state->token[i], 0, sizeof(state->token[i]));
3638 state->token[i].tok = -1;
3641 #warning "FIXME do not hardcode the include paths"
3642 static char *include_paths[] = {
3643 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/include",
3644 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/arch/i386/include",
3645 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src",
3649 static void compile_file(struct compile_state *state, const char *filename, int local)
3652 const char *subdir, *base;
3654 struct file_state *file;
3656 file = xmalloc(sizeof(*file), "file_state");
3658 base = strrchr(filename, '/');
3661 subdir_len = base - filename;
3668 basename = xmalloc(strlen(base) +1, "basename");
3669 strcpy(basename, base);
3670 file->basename = basename;
3672 if (getcwd(cwd, sizeof(cwd)) == 0) {
3673 die("cwd buffer to small");
3676 if (subdir[0] == '/') {
3677 file->dirname = xmalloc(subdir_len + 1, "dirname");
3678 memcpy(file->dirname, subdir, subdir_len);
3679 file->dirname[subdir_len] = '\0';
3685 /* Find the appropriate directory... */
3687 if (!state->file && exists(cwd, filename)) {
3690 if (local && state->file && exists(state->file->dirname, filename)) {
3691 dir = state->file->dirname;
3693 for(path = include_paths; !dir && *path; path++) {
3694 if (exists(*path, filename)) {
3699 error(state, 0, "Cannot find `%s'\n", filename);
3701 dirlen = strlen(dir);
3702 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
3703 memcpy(file->dirname, dir, dirlen);
3704 file->dirname[dirlen] = '/';
3705 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
3706 file->dirname[dirlen + 1 + subdir_len] = '\0';
3708 file->buf = slurp_file(file->dirname, file->basename, &file->size);
3711 file->pos = file->buf;
3712 file->line_start = file->pos;
3715 file->report_line = 1;
3716 file->report_name = file->basename;
3717 file->report_dir = file->dirname;
3719 file->prev = state->file;
3722 process_trigraphs(state);
3723 splice_lines(state);
3726 /* Type helper functions */
3728 static struct type *new_type(
3729 unsigned int type, struct type *left, struct type *right)
3731 struct type *result;
3732 result = xmalloc(sizeof(*result), "type");
3733 result->type = type;
3734 result->left = left;
3735 result->right = right;
3736 result->field_ident = 0;
3737 result->type_ident = 0;
3741 static struct type *clone_type(unsigned int specifiers, struct type *old)
3743 struct type *result;
3744 result = xmalloc(sizeof(*result), "type");
3745 memcpy(result, old, sizeof(*result));
3746 result->type &= TYPE_MASK;
3747 result->type |= specifiers;
3751 #define SIZEOF_SHORT 2
3752 #define SIZEOF_INT 4
3753 #define SIZEOF_LONG (sizeof(long_t))
3755 #define ALIGNOF_SHORT 2
3756 #define ALIGNOF_INT 4
3757 #define ALIGNOF_LONG (sizeof(long_t))
3759 #define MASK_UCHAR(X) ((X) & ((ulong_t)0xff))
3760 #define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT*8)) - 1))
3761 static inline ulong_t mask_uint(ulong_t x)
3763 if (SIZEOF_INT < SIZEOF_LONG) {
3764 ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT*8))) -1;
3769 #define MASK_UINT(X) (mask_uint(X))
3770 #define MASK_ULONG(X) (X)
3772 static struct type void_type = { .type = TYPE_VOID };
3773 static struct type char_type = { .type = TYPE_CHAR };
3774 static struct type uchar_type = { .type = TYPE_UCHAR };
3775 static struct type short_type = { .type = TYPE_SHORT };
3776 static struct type ushort_type = { .type = TYPE_USHORT };
3777 static struct type int_type = { .type = TYPE_INT };
3778 static struct type uint_type = { .type = TYPE_UINT };
3779 static struct type long_type = { .type = TYPE_LONG };
3780 static struct type ulong_type = { .type = TYPE_ULONG };
3782 static struct triple *variable(struct compile_state *state, struct type *type)
3784 struct triple *result;
3785 if ((type->type & STOR_MASK) != STOR_PERM) {
3786 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3787 result = triple(state, OP_ADECL, type, 0, 0);
3790 struct triple **vector;
3792 result = new_triple(state, OP_VAL_VEC, type, -1, -1);
3793 vector = &result->param[0];
3797 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
3798 vector[index] = variable(state, field->left);
3799 field = field->right;
3802 vector[index] = variable(state, field);
3806 result = triple(state, OP_SDECL, type, 0, 0);
3811 static void stor_of(FILE *fp, struct type *type)
3813 switch(type->type & STOR_MASK) {
3815 fprintf(fp, "auto ");
3818 fprintf(fp, "static ");
3821 fprintf(fp, "extern ");
3824 fprintf(fp, "register ");
3827 fprintf(fp, "typedef ");
3830 fprintf(fp, "inline ");
3834 static void qual_of(FILE *fp, struct type *type)
3836 if (type->type & QUAL_CONST) {
3837 fprintf(fp, " const");
3839 if (type->type & QUAL_VOLATILE) {
3840 fprintf(fp, " volatile");
3842 if (type->type & QUAL_RESTRICT) {
3843 fprintf(fp, " restrict");
3847 static void name_of(FILE *fp, struct type *type)
3850 switch(type->type & TYPE_MASK) {
3852 fprintf(fp, "void");
3856 fprintf(fp, "signed char");
3860 fprintf(fp, "unsigned char");
3864 fprintf(fp, "signed short");
3868 fprintf(fp, "unsigned short");
3872 fprintf(fp, "signed int");
3876 fprintf(fp, "unsigned int");
3880 fprintf(fp, "signed long");
3884 fprintf(fp, "unsigned long");
3888 name_of(fp, type->left);
3894 name_of(fp, type->left);
3896 name_of(fp, type->right);
3899 fprintf(fp, "enum %s", type->type_ident->name);
3903 fprintf(fp, "struct %s", type->type_ident->name);
3908 name_of(fp, type->left);
3909 fprintf(fp, " (*)(");
3910 name_of(fp, type->right);
3915 name_of(fp, type->left);
3916 fprintf(fp, " [%ld]", type->elements);
3919 fprintf(fp, "????: %x", type->type & TYPE_MASK);
3924 static size_t align_of(struct compile_state *state, struct type *type)
3928 switch(type->type & TYPE_MASK) {
3938 align = ALIGNOF_SHORT;
3943 align = ALIGNOF_INT;
3948 align = ALIGNOF_LONG;
3953 size_t left_align, right_align;
3954 left_align = align_of(state, type->left);
3955 right_align = align_of(state, type->right);
3956 align = (left_align >= right_align) ? left_align : right_align;
3960 align = align_of(state, type->left);
3963 align = align_of(state, type->left);
3966 error(state, 0, "alignof not yet defined for type\n");
3972 static size_t needed_padding(size_t offset, size_t align)
3976 if (offset % align) {
3977 padding = align - (offset % align);
3981 static size_t size_of(struct compile_state *state, struct type *type)
3985 switch(type->type & TYPE_MASK) {
3995 size = SIZEOF_SHORT;
4011 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4012 align = align_of(state, type->left);
4013 pad = needed_padding(size, align);
4014 size = size + pad + size_of(state, type->left);
4017 align = align_of(state, type);
4018 pad = needed_padding(size, align);
4019 size = size + pad + size_of(state, type);
4024 size_t size_left, size_right;
4025 size_left = size_of(state, type->left);
4026 size_right = size_of(state, type->right);
4027 size = (size_left >= size_right)? size_left : size_right;
4031 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4032 internal_error(state, 0, "Invalid array type");
4034 size = size_of(state, type->left) * type->elements;
4040 size = size_of(state, type->left);
4041 /* Pad structures so their size is a multiples of their alignment */
4042 align = align_of(state, type);
4043 pad = needed_padding(size, align);
4048 internal_error(state, 0, "sizeof not yet defined for type\n");
4054 static size_t field_offset(struct compile_state *state,
4055 struct type *type, struct hash_entry *field)
4057 struct type *member;
4059 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4060 internal_error(state, 0, "field_offset only works on structures");
4063 member = type->left;
4064 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4065 align = align_of(state, member->left);
4066 size += needed_padding(size, align);
4067 if (member->left->field_ident == field) {
4068 member = member->left;
4071 size += size_of(state, member->left);
4072 member = member->right;
4074 align = align_of(state, member);
4075 size += needed_padding(size, align);
4076 if (member->field_ident != field) {
4077 error(state, 0, "member %s not present", field->name);
4082 static struct type *field_type(struct compile_state *state,
4083 struct type *type, struct hash_entry *field)
4085 struct type *member;
4086 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4087 internal_error(state, 0, "field_type only works on structures");
4089 member = type->left;
4090 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4091 if (member->left->field_ident == field) {
4092 member = member->left;
4095 member = member->right;
4097 if (member->field_ident != field) {
4098 error(state, 0, "member %s not present", field->name);
4103 static struct type *next_field(struct compile_state *state,
4104 struct type *type, struct type *prev_member)
4106 struct type *member;
4107 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4108 internal_error(state, 0, "next_field only works on structures");
4110 member = type->left;
4111 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4113 member = member->left;
4116 if (member->left == prev_member) {
4119 member = member->right;
4121 if (member == prev_member) {
4125 internal_error(state, 0, "prev_member %s not present",
4126 prev_member->field_ident->name);
4131 static struct triple *struct_field(struct compile_state *state,
4132 struct triple *decl, struct hash_entry *field)
4134 struct triple **vector;
4138 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4141 if (decl->op != OP_VAL_VEC) {
4142 internal_error(state, 0, "Invalid struct variable");
4145 internal_error(state, 0, "Missing structure field");
4148 vector = &RHS(decl, 0);
4150 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4151 if (type->left->field_ident == field) {
4158 if (type->field_ident != field) {
4159 internal_error(state, 0, "field %s not found?", field->name);
4161 return vector[index];
4164 static void arrays_complete(struct compile_state *state, struct type *type)
4166 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
4167 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4168 error(state, 0, "array size not specified");
4170 arrays_complete(state, type->left);
4174 static unsigned int do_integral_promotion(unsigned int type)
4177 if (TYPE_INTEGER(type) &&
4178 TYPE_RANK(type) < TYPE_RANK(TYPE_INT)) {
4184 static unsigned int do_arithmetic_conversion(
4185 unsigned int left, unsigned int right)
4189 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
4190 return TYPE_LDOUBLE;
4192 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
4195 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
4198 left = do_integral_promotion(left);
4199 right = do_integral_promotion(right);
4200 /* If both operands have the same size done */
4201 if (left == right) {
4204 /* If both operands have the same signedness pick the larger */
4205 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
4206 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
4208 /* If the signed type can hold everything use it */
4209 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
4212 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
4215 /* Convert to the unsigned type with the same rank as the signed type */
4216 else if (TYPE_SIGNED(left)) {
4217 return TYPE_MKUNSIGNED(left);
4220 return TYPE_MKUNSIGNED(right);
4224 /* see if two types are the same except for qualifiers */
4225 static int equiv_types(struct type *left, struct type *right)
4228 /* Error if the basic types do not match */
4229 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4232 type = left->type & TYPE_MASK;
4233 /* If the basic types match and it is a void type we are done */
4234 if (type == TYPE_VOID) {
4237 /* if the basic types match and it is an arithmetic type we are done */
4238 if (TYPE_ARITHMETIC(type)) {
4241 /* If it is a pointer type recurse and keep testing */
4242 if (type == TYPE_POINTER) {
4243 return equiv_types(left->left, right->left);
4245 else if (type == TYPE_ARRAY) {
4246 return (left->elements == right->elements) &&
4247 equiv_types(left->left, right->left);
4249 /* test for struct/union equality */
4250 else if (type == TYPE_STRUCT) {
4251 return left->type_ident == right->type_ident;
4253 /* Test for equivalent functions */
4254 else if (type == TYPE_FUNCTION) {
4255 return equiv_types(left->left, right->left) &&
4256 equiv_types(left->right, right->right);
4258 /* We only see TYPE_PRODUCT as part of function equivalence matching */
4259 else if (type == TYPE_PRODUCT) {
4260 return equiv_types(left->left, right->left) &&
4261 equiv_types(left->right, right->right);
4263 /* We should see TYPE_OVERLAP */
4269 static int equiv_ptrs(struct type *left, struct type *right)
4271 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4272 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4275 return equiv_types(left->left, right->left);
4278 static struct type *compatible_types(struct type *left, struct type *right)
4280 struct type *result;
4281 unsigned int type, qual_type;
4282 /* Error if the basic types do not match */
4283 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4286 type = left->type & TYPE_MASK;
4287 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4289 /* if the basic types match and it is an arithmetic type we are done */
4290 if (TYPE_ARITHMETIC(type)) {
4291 result = new_type(qual_type, 0, 0);
4293 /* If it is a pointer type recurse and keep testing */
4294 else if (type == TYPE_POINTER) {
4295 result = compatible_types(left->left, right->left);
4297 result = new_type(qual_type, result, 0);
4300 /* test for struct/union equality */
4301 else if (type == TYPE_STRUCT) {
4302 if (left->type_ident == right->type_ident) {
4306 /* Test for equivalent functions */
4307 else if (type == TYPE_FUNCTION) {
4308 struct type *lf, *rf;
4309 lf = compatible_types(left->left, right->left);
4310 rf = compatible_types(left->right, right->right);
4312 result = new_type(qual_type, lf, rf);
4315 /* We only see TYPE_PRODUCT as part of function equivalence matching */
4316 else if (type == TYPE_PRODUCT) {
4317 struct type *lf, *rf;
4318 lf = compatible_types(left->left, right->left);
4319 rf = compatible_types(left->right, right->right);
4321 result = new_type(qual_type, lf, rf);
4325 /* Nothing else is compatible */
4330 static struct type *compatible_ptrs(struct type *left, struct type *right)
4332 struct type *result;
4333 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4334 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4337 result = compatible_types(left->left, right->left);
4339 unsigned int qual_type;
4340 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4341 result = new_type(qual_type, result, 0);
4346 static struct triple *integral_promotion(
4347 struct compile_state *state, struct triple *def)
4351 /* As all operations are carried out in registers
4352 * the values are converted on load I just convert
4353 * logical type of the operand.
4355 if (TYPE_INTEGER(type->type)) {
4356 unsigned int int_type;
4357 int_type = type->type & ~TYPE_MASK;
4358 int_type |= do_integral_promotion(type->type);
4359 if (int_type != type->type) {
4360 def->type = new_type(int_type, 0, 0);
4367 static void arithmetic(struct compile_state *state, struct triple *def)
4369 if (!TYPE_ARITHMETIC(def->type->type)) {
4370 error(state, 0, "arithmetic type expexted");
4374 static void ptr_arithmetic(struct compile_state *state, struct triple *def)
4376 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
4377 error(state, def, "pointer or arithmetic type expected");
4381 static int is_integral(struct triple *ins)
4383 return TYPE_INTEGER(ins->type->type);
4386 static void integral(struct compile_state *state, struct triple *def)
4388 if (!is_integral(def)) {
4389 error(state, 0, "integral type expected");
4394 static void bool(struct compile_state *state, struct triple *def)
4396 if (!TYPE_ARITHMETIC(def->type->type) &&
4397 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
4398 error(state, 0, "arithmetic or pointer type expected");
4402 static int is_signed(struct type *type)
4404 return !!TYPE_SIGNED(type->type);
4407 /* Is this value located in a register otherwise it must be in memory */
4408 static int is_in_reg(struct compile_state *state, struct triple *def)
4411 if (def->op == OP_ADECL) {
4414 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
4417 else if (def->op == OP_VAL_VEC) {
4418 in_reg = is_in_reg(state, RHS(def, 0));
4420 else if (def->op == OP_DOT) {
4421 in_reg = is_in_reg(state, RHS(def, 0));
4424 internal_error(state, 0, "unknown expr storage location");
4430 /* Is this a stable variable location otherwise it must be a temporary */
4431 static int is_stable(struct compile_state *state, struct triple *def)
4438 if ((def->op == OP_ADECL) ||
4439 (def->op == OP_SDECL) ||
4440 (def->op == OP_DEREF) ||
4441 (def->op == OP_BLOBCONST)) {
4444 else if (def->op == OP_DOT) {
4445 ret = is_stable(state, RHS(def, 0));
4447 else if (def->op == OP_VAL_VEC) {
4448 struct triple **vector;
4451 vector = &RHS(def, 0);
4452 for(i = 0; i < def->type->elements; i++) {
4453 if (!is_stable(state, vector[i])) {
4462 static int is_lvalue(struct compile_state *state, struct triple *def)
4469 if (!is_stable(state, def)) {
4472 if (def->op == OP_DOT) {
4473 ret = is_lvalue(state, RHS(def, 0));
4478 static void clvalue(struct compile_state *state, struct triple *def)
4481 internal_error(state, def, "nothing where lvalue expected?");
4483 if (!is_lvalue(state, def)) {
4484 error(state, def, "lvalue expected");
4487 static void lvalue(struct compile_state *state, struct triple *def)
4489 clvalue(state, def);
4490 if (def->type->type & QUAL_CONST) {
4491 error(state, def, "modifable lvalue expected");
4495 static int is_pointer(struct triple *def)
4497 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
4500 static void pointer(struct compile_state *state, struct triple *def)
4502 if (!is_pointer(def)) {
4503 error(state, def, "pointer expected");
4507 static struct triple *int_const(
4508 struct compile_state *state, struct type *type, ulong_t value)
4510 struct triple *result;
4511 switch(type->type & TYPE_MASK) {
4513 case TYPE_INT: case TYPE_UINT:
4514 case TYPE_LONG: case TYPE_ULONG:
4517 internal_error(state, 0, "constant for unkown type");
4519 result = triple(state, OP_INTCONST, type, 0, 0);
4520 result->u.cval = value;
4525 static struct triple *do_mk_addr_expr(struct compile_state *state,
4526 struct triple *expr, struct type *type, ulong_t offset)
4528 struct triple *result;
4529 clvalue(state, expr);
4531 type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
4534 if (expr->op == OP_ADECL) {
4535 error(state, expr, "address of auto variables not supported");
4537 else if (expr->op == OP_SDECL) {
4538 result = triple(state, OP_ADDRCONST, type, 0, 0);
4539 MISC(result, 0) = expr;
4540 result->u.cval = offset;
4542 else if (expr->op == OP_DEREF) {
4543 result = triple(state, OP_ADD, type,
4545 int_const(state, &ulong_type, offset));
4550 static struct triple *mk_addr_expr(
4551 struct compile_state *state, struct triple *expr, ulong_t offset)
4553 return do_mk_addr_expr(state, expr, expr->type, offset);
4556 static struct triple *mk_deref_expr(
4557 struct compile_state *state, struct triple *expr)
4559 struct type *base_type;
4560 pointer(state, expr);
4561 base_type = expr->type->left;
4562 return triple(state, OP_DEREF, base_type, expr, 0);
4565 static struct triple *array_to_pointer(struct compile_state *state, struct triple *def)
4567 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
4570 TYPE_POINTER | (def->type->type & QUAL_MASK),
4571 def->type->left, 0);
4572 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
4573 struct triple *addrconst;
4574 if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
4575 internal_error(state, def, "bad array constant");
4577 addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
4578 MISC(addrconst, 0) = def;
4582 def = triple(state, OP_COPY, type, def, 0);
4588 static struct triple *deref_field(
4589 struct compile_state *state, struct triple *expr, struct hash_entry *field)
4591 struct triple *result;
4592 struct type *type, *member;
4594 internal_error(state, 0, "No field passed to deref_field");
4598 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4599 error(state, 0, "request for member %s in something not a struct or union",
4602 member = field_type(state, type, field);
4603 if ((type->type & STOR_MASK) == STOR_PERM) {
4604 /* Do the pointer arithmetic to get a deref the field */
4606 offset = field_offset(state, type, field);
4607 result = do_mk_addr_expr(state, expr, member, offset);
4608 result = mk_deref_expr(state, result);
4611 /* Find the variable for the field I want. */
4612 result = triple(state, OP_DOT, member, expr, 0);
4613 result->u.field = field;
4618 static struct triple *read_expr(struct compile_state *state, struct triple *def)
4624 if (!is_stable(state, def)) {
4627 /* Tranform an array to a pointer to the first element */
4629 #warning "CHECK_ME is this the right place to transform arrays to pointers?"
4630 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
4631 return array_to_pointer(state, def);
4633 if (is_in_reg(state, def)) {
4638 return triple(state, op, def->type, def, 0);
4641 int is_write_compatible(struct compile_state *state,
4642 struct type *dest, struct type *rval)
4645 /* Both operands have arithmetic type */
4646 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
4649 /* One operand is a pointer and the other is a pointer to void */
4650 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
4651 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
4652 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
4653 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
4656 /* If both types are the same without qualifiers we are good */
4657 else if (equiv_ptrs(dest, rval)) {
4660 /* test for struct/union equality */
4661 else if (((dest->type & TYPE_MASK) == TYPE_STRUCT) &&
4662 ((rval->type & TYPE_MASK) == TYPE_STRUCT) &&
4663 (dest->type_ident == rval->type_ident)) {
4670 static void write_compatible(struct compile_state *state,
4671 struct type *dest, struct type *rval)
4673 if (!is_write_compatible(state, dest, rval)) {
4674 error(state, 0, "Incompatible types in assignment");
4678 static int is_init_compatible(struct compile_state *state,
4679 struct type *dest, struct type *rval)
4682 if (is_write_compatible(state, dest, rval)) {
4685 else if (equiv_types(dest, rval)) {
4691 static struct triple *write_expr(
4692 struct compile_state *state, struct triple *dest, struct triple *rval)
4699 internal_error(state, 0, "missing rval");
4702 if (rval->op == OP_LIST) {
4703 internal_error(state, 0, "expression of type OP_LIST?");
4705 if (!is_lvalue(state, dest)) {
4706 internal_error(state, 0, "writing to a non lvalue?");
4708 if (dest->type->type & QUAL_CONST) {
4709 internal_error(state, 0, "modifable lvalue expexted");
4712 write_compatible(state, dest->type, rval->type);
4714 /* Now figure out which assignment operator to use */
4716 if (is_in_reg(state, dest)) {
4721 def = triple(state, op, dest->type, dest, rval);
4725 static struct triple *init_expr(
4726 struct compile_state *state, struct triple *dest, struct triple *rval)
4732 internal_error(state, 0, "missing rval");
4734 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
4735 rval = read_expr(state, rval);
4736 def = write_expr(state, dest, rval);
4739 /* Fill in the array size if necessary */
4740 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
4741 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
4742 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4743 dest->type->elements = rval->type->elements;
4746 if (!equiv_types(dest->type, rval->type)) {
4747 error(state, 0, "Incompatible types in inializer");
4749 MISC(dest, 0) = rval;
4750 insert_triple(state, dest, rval);
4751 rval->id |= TRIPLE_FLAG_FLATTENED;
4752 use_triple(MISC(dest, 0), dest);
4757 struct type *arithmetic_result(
4758 struct compile_state *state, struct triple *left, struct triple *right)
4761 /* Sanity checks to ensure I am working with arithmetic types */
4762 arithmetic(state, left);
4763 arithmetic(state, right);
4765 do_arithmetic_conversion(
4767 right->type->type), 0, 0);
4771 struct type *ptr_arithmetic_result(
4772 struct compile_state *state, struct triple *left, struct triple *right)
4775 /* Sanity checks to ensure I am working with the proper types */
4776 ptr_arithmetic(state, left);
4777 arithmetic(state, right);
4778 if (TYPE_ARITHMETIC(left->type->type) &&
4779 TYPE_ARITHMETIC(right->type->type)) {
4780 type = arithmetic_result(state, left, right);
4782 else if (TYPE_PTR(left->type->type)) {
4786 internal_error(state, 0, "huh?");
4793 /* boolean helper function */
4795 static struct triple *ltrue_expr(struct compile_state *state,
4796 struct triple *expr)
4799 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
4800 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
4801 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
4802 /* If the expression is already boolean do nothing */
4805 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
4811 static struct triple *lfalse_expr(struct compile_state *state,
4812 struct triple *expr)
4814 return triple(state, OP_LFALSE, &int_type, expr, 0);
4817 static struct triple *cond_expr(
4818 struct compile_state *state,
4819 struct triple *test, struct triple *left, struct triple *right)
4822 struct type *result_type;
4823 unsigned int left_type, right_type;
4825 left_type = left->type->type;
4826 right_type = right->type->type;
4828 /* Both operands have arithmetic type */
4829 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
4830 result_type = arithmetic_result(state, left, right);
4832 /* Both operands have void type */
4833 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
4834 ((right_type & TYPE_MASK) == TYPE_VOID)) {
4835 result_type = &void_type;
4837 /* pointers to the same type... */
4838 else if ((result_type = compatible_ptrs(left->type, right->type))) {
4841 /* Both operands are pointers and left is a pointer to void */
4842 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4843 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4844 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4845 result_type = right->type;
4847 /* Both operands are pointers and right is a pointer to void */
4848 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4849 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4850 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4851 result_type = left->type;
4854 error(state, 0, "Incompatible types in conditional expression");
4856 /* Cleanup and invert the test */
4857 test = lfalse_expr(state, read_expr(state, test));
4858 def = new_triple(state, OP_COND, result_type, 0, 3);
4859 def->param[0] = test;
4860 def->param[1] = left;
4861 def->param[2] = right;
4866 static int expr_depth(struct compile_state *state, struct triple *ins)
4870 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
4873 else if (ins->op == OP_DEREF) {
4874 count = expr_depth(state, RHS(ins, 0)) - 1;
4876 else if (ins->op == OP_VAL) {
4877 count = expr_depth(state, RHS(ins, 0)) - 1;
4879 else if (ins->op == OP_COMMA) {
4881 ldepth = expr_depth(state, RHS(ins, 0));
4882 rdepth = expr_depth(state, RHS(ins, 1));
4883 count = (ldepth >= rdepth)? ldepth : rdepth;
4885 else if (ins->op == OP_CALL) {
4886 /* Don't figure the depth of a call just guess it is huge */
4890 struct triple **expr;
4891 expr = triple_rhs(state, ins, 0);
4892 for(;expr; expr = triple_rhs(state, ins, expr)) {
4895 depth = expr_depth(state, *expr);
4896 if (depth > count) {
4905 static struct triple *flatten(
4906 struct compile_state *state, struct triple *first, struct triple *ptr);
4908 static struct triple *flatten_generic(
4909 struct compile_state *state, struct triple *first, struct triple *ptr)
4913 struct triple **ins;
4916 /* Only operations with just a rhs should come here */
4917 rhs = TRIPLE_RHS(ptr->sizes);
4918 lhs = TRIPLE_LHS(ptr->sizes);
4919 if (TRIPLE_SIZE(ptr->sizes) != lhs + rhs) {
4920 internal_error(state, ptr, "unexpected args for: %d %s",
4921 ptr->op, tops(ptr->op));
4923 /* Find the depth of the rhs elements */
4924 for(i = 0; i < rhs; i++) {
4925 vector[i].ins = &RHS(ptr, i);
4926 vector[i].depth = expr_depth(state, *vector[i].ins);
4928 /* Selection sort the rhs */
4929 for(i = 0; i < rhs; i++) {
4931 for(j = i + 1; j < rhs; j++ ) {
4932 if (vector[j].depth > vector[max].depth) {
4937 struct rhs_vector tmp;
4939 vector[i] = vector[max];
4943 /* Now flatten the rhs elements */
4944 for(i = 0; i < rhs; i++) {
4945 *vector[i].ins = flatten(state, first, *vector[i].ins);
4946 use_triple(*vector[i].ins, ptr);
4949 /* Now flatten the lhs elements */
4950 for(i = 0; i < lhs; i++) {
4951 struct triple **ins = &LHS(ptr, i);
4952 *ins = flatten(state, first, *ins);
4953 use_triple(*ins, ptr);
4958 static struct triple *flatten_land(
4959 struct compile_state *state, struct triple *first, struct triple *ptr)
4961 struct triple *left, *right;
4962 struct triple *val, *test, *jmp, *label1, *end;
4964 /* Find the triples */
4966 right = RHS(ptr, 1);
4968 /* Generate the needed triples */
4971 /* Thread the triples together */
4972 val = flatten(state, first, variable(state, ptr->type));
4973 left = flatten(state, first, write_expr(state, val, left));
4974 test = flatten(state, first,
4975 lfalse_expr(state, read_expr(state, val)));
4976 jmp = flatten(state, first, branch(state, end, test));
4977 label1 = flatten(state, first, label(state));
4978 right = flatten(state, first, write_expr(state, val, right));
4979 TARG(jmp, 0) = flatten(state, first, end);
4981 /* Now give the caller something to chew on */
4982 return read_expr(state, val);
4985 static struct triple *flatten_lor(
4986 struct compile_state *state, struct triple *first, struct triple *ptr)
4988 struct triple *left, *right;
4989 struct triple *val, *jmp, *label1, *end;
4991 /* Find the triples */
4993 right = RHS(ptr, 1);
4995 /* Generate the needed triples */
4998 /* Thread the triples together */
4999 val = flatten(state, first, variable(state, ptr->type));
5000 left = flatten(state, first, write_expr(state, val, left));
5001 jmp = flatten(state, first, branch(state, end, left));
5002 label1 = flatten(state, first, label(state));
5003 right = flatten(state, first, write_expr(state, val, right));
5004 TARG(jmp, 0) = flatten(state, first, end);
5007 /* Now give the caller something to chew on */
5008 return read_expr(state, val);
5011 static struct triple *flatten_cond(
5012 struct compile_state *state, struct triple *first, struct triple *ptr)
5014 struct triple *test, *left, *right;
5015 struct triple *val, *mv1, *jmp1, *label1, *mv2, *middle, *jmp2, *end;
5017 /* Find the triples */
5020 right = RHS(ptr, 2);
5022 /* Generate the needed triples */
5024 middle = label(state);
5026 /* Thread the triples together */
5027 val = flatten(state, first, variable(state, ptr->type));
5028 test = flatten(state, first, test);
5029 jmp1 = flatten(state, first, branch(state, middle, test));
5030 label1 = flatten(state, first, label(state));
5031 left = flatten(state, first, left);
5032 mv1 = flatten(state, first, write_expr(state, val, left));
5033 jmp2 = flatten(state, first, branch(state, end, 0));
5034 TARG(jmp1, 0) = flatten(state, first, middle);
5035 right = flatten(state, first, right);
5036 mv2 = flatten(state, first, write_expr(state, val, right));
5037 TARG(jmp2, 0) = flatten(state, first, end);
5039 /* Now give the caller something to chew on */
5040 return read_expr(state, val);
5043 struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
5044 struct occurance *base_occurance)
5046 struct triple *nfunc;
5047 struct triple *nfirst, *ofirst;
5048 struct triple *new, *old;
5051 fprintf(stdout, "\n");
5052 loc(stdout, state, 0);
5053 fprintf(stdout, "\n__________ copy_func _________\n");
5054 print_triple(state, ofunc);
5055 fprintf(stdout, "__________ copy_func _________ done\n\n");
5058 /* Make a new copy of the old function */
5059 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
5061 ofirst = old = RHS(ofunc, 0);
5064 struct occurance *occurance;
5065 int old_lhs, old_rhs;
5066 old_lhs = TRIPLE_LHS(old->sizes);
5067 old_rhs = TRIPLE_RHS(old->sizes);
5068 occurance = inline_occurance(state, base_occurance, old->occurance);
5069 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
5071 if (!triple_stores_block(state, new)) {
5072 memcpy(&new->u, &old->u, sizeof(new->u));
5075 RHS(nfunc, 0) = nfirst = new;
5078 insert_triple(state, nfirst, new);
5080 new->id |= TRIPLE_FLAG_FLATTENED;
5082 /* During the copy remember new as user of old */
5083 use_triple(old, new);
5085 /* Populate the return type if present */
5086 if (old == MISC(ofunc, 0)) {
5087 MISC(nfunc, 0) = new;
5090 } while(old != ofirst);
5092 /* Make a second pass to fix up any unresolved references */
5096 struct triple **oexpr, **nexpr;
5098 /* Lookup where the copy is, to join pointers */
5099 count = TRIPLE_SIZE(old->sizes);
5100 for(i = 0; i < count; i++) {
5101 oexpr = &old->param[i];
5102 nexpr = &new->param[i];
5103 if (!*nexpr && *oexpr && (*oexpr)->use) {
5104 *nexpr = (*oexpr)->use->member;
5105 if (*nexpr == old) {
5106 internal_error(state, 0, "new == old?");
5108 use_triple(*nexpr, new);
5110 if (!*nexpr && *oexpr) {
5111 internal_error(state, 0, "Could not copy %d\n", i);
5116 } while((old != ofirst) && (new != nfirst));
5118 /* Make a third pass to cleanup the extra useses */
5122 unuse_triple(old, new);
5125 } while ((old != ofirst) && (new != nfirst));
5129 static struct triple *flatten_call(
5130 struct compile_state *state, struct triple *first, struct triple *ptr)
5132 /* Inline the function call */
5134 struct triple *ofunc, *nfunc, *nfirst, *param, *result;
5135 struct triple *end, *nend;
5138 /* Find the triples */
5139 ofunc = MISC(ptr, 0);
5140 if (ofunc->op != OP_LIST) {
5141 internal_error(state, 0, "improper function");
5143 nfunc = copy_func(state, ofunc, ptr->occurance);
5144 nfirst = RHS(nfunc, 0)->next;
5145 /* Prepend the parameter reading into the new function list */
5146 ptype = nfunc->type->right;
5147 param = RHS(nfunc, 0)->next;
5148 pvals = TRIPLE_RHS(ptr->sizes);
5149 for(i = 0; i < pvals; i++) {
5153 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
5154 atype = ptype->left;
5156 while((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
5157 param = param->next;
5160 flatten(state, nfirst, write_expr(state, param, arg));
5161 ptype = ptype->right;
5162 param = param->next;
5165 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
5166 result = read_expr(state, MISC(nfunc,0));
5169 fprintf(stdout, "\n");
5170 loc(stdout, state, 0);
5171 fprintf(stdout, "\n__________ flatten_call _________\n");
5172 print_triple(state, nfunc);
5173 fprintf(stdout, "__________ flatten_call _________ done\n\n");
5176 /* Get rid of the extra triples */
5177 nfirst = RHS(nfunc, 0)->next;
5178 free_triple(state, RHS(nfunc, 0));
5180 free_triple(state, nfunc);
5182 /* Append the new function list onto the return list */
5184 nend = nfirst->prev;
5193 static struct triple *flatten(
5194 struct compile_state *state, struct triple *first, struct triple *ptr)
5196 struct triple *orig_ptr;
5201 /* Only flatten triples once */
5202 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
5207 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5211 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5212 return MISC(ptr, 0);
5215 ptr = flatten_land(state, first, ptr);
5218 ptr = flatten_lor(state, first, ptr);
5221 ptr = flatten_cond(state, first, ptr);
5224 ptr = flatten_call(state, first, ptr);
5228 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5229 use_triple(RHS(ptr, 0), ptr);
5232 use_triple(TARG(ptr, 0), ptr);
5233 if (TRIPLE_RHS(ptr->sizes)) {
5234 use_triple(RHS(ptr, 0), ptr);
5235 if (ptr->next != ptr) {
5236 use_triple(ptr->next, ptr);
5241 insert_triple(state, first, ptr);
5242 ptr->id |= TRIPLE_FLAG_FLATTENED;
5243 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
5244 use_triple(MISC(ptr, 0), ptr);
5247 /* Since OP_DEREF is just a marker delete it when I flatten it */
5249 RHS(orig_ptr, 0) = 0;
5250 free_triple(state, orig_ptr);
5254 struct triple *base;
5256 if (base->op == OP_DEREF) {
5257 struct triple *left;
5259 offset = field_offset(state, base->type, ptr->u.field);
5260 left = RHS(base, 0);
5261 ptr = triple(state, OP_ADD, left->type,
5262 read_expr(state, left),
5263 int_const(state, &ulong_type, offset));
5264 free_triple(state, base);
5266 else if (base->op == OP_VAL_VEC) {
5267 base = flatten(state, first, base);
5268 ptr = struct_field(state, base, ptr->u.field);
5273 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5274 use_triple(MISC(ptr, 0), ptr);
5275 use_triple(ptr, MISC(ptr, 0));
5279 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5280 use_triple(MISC(ptr, 0), ptr);
5285 /* Flatten the easy cases we don't override */
5286 ptr = flatten_generic(state, first, ptr);
5289 } while(ptr && (ptr != orig_ptr));
5291 insert_triple(state, first, ptr);
5292 ptr->id |= TRIPLE_FLAG_FLATTENED;
5297 static void release_expr(struct compile_state *state, struct triple *expr)
5299 struct triple *head;
5300 head = label(state);
5301 flatten(state, head, expr);
5302 while(head->next != head) {
5303 release_triple(state, head->next);
5305 free_triple(state, head);
5308 static int replace_rhs_use(struct compile_state *state,
5309 struct triple *orig, struct triple *new, struct triple *use)
5311 struct triple **expr;
5314 expr = triple_rhs(state, use, 0);
5315 for(;expr; expr = triple_rhs(state, use, expr)) {
5316 if (*expr == orig) {
5322 unuse_triple(orig, use);
5323 use_triple(new, use);
5328 static int replace_lhs_use(struct compile_state *state,
5329 struct triple *orig, struct triple *new, struct triple *use)
5331 struct triple **expr;
5334 expr = triple_lhs(state, use, 0);
5335 for(;expr; expr = triple_lhs(state, use, expr)) {
5336 if (*expr == orig) {
5342 unuse_triple(orig, use);
5343 use_triple(new, use);
5348 static void propogate_use(struct compile_state *state,
5349 struct triple *orig, struct triple *new)
5351 struct triple_set *user, *next;
5352 for(user = orig->use; user; user = next) {
5358 found |= replace_rhs_use(state, orig, new, use);
5359 found |= replace_lhs_use(state, orig, new, use);
5361 internal_error(state, use, "use without use");
5365 internal_error(state, orig, "used after propogate_use");
5371 * ===========================
5374 static struct triple *mk_add_expr(
5375 struct compile_state *state, struct triple *left, struct triple *right)
5377 struct type *result_type;
5378 /* Put pointer operands on the left */
5379 if (is_pointer(right)) {
5385 left = read_expr(state, left);
5386 right = read_expr(state, right);
5387 result_type = ptr_arithmetic_result(state, left, right);
5388 if (is_pointer(left)) {
5389 right = triple(state,
5390 is_signed(right->type)? OP_SMUL : OP_UMUL,
5393 int_const(state, &ulong_type,
5394 size_of(state, left->type->left)));
5396 return triple(state, OP_ADD, result_type, left, right);
5399 static struct triple *mk_sub_expr(
5400 struct compile_state *state, struct triple *left, struct triple *right)
5402 struct type *result_type;
5403 result_type = ptr_arithmetic_result(state, left, right);
5404 left = read_expr(state, left);
5405 right = read_expr(state, right);
5406 if (is_pointer(left)) {
5407 right = triple(state,
5408 is_signed(right->type)? OP_SMUL : OP_UMUL,
5411 int_const(state, &ulong_type,
5412 size_of(state, left->type->left)));
5414 return triple(state, OP_SUB, result_type, left, right);
5417 static struct triple *mk_pre_inc_expr(
5418 struct compile_state *state, struct triple *def)
5422 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
5423 return triple(state, OP_VAL, def->type,
5424 write_expr(state, def, val),
5428 static struct triple *mk_pre_dec_expr(
5429 struct compile_state *state, struct triple *def)
5433 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
5434 return triple(state, OP_VAL, def->type,
5435 write_expr(state, def, val),
5439 static struct triple *mk_post_inc_expr(
5440 struct compile_state *state, struct triple *def)
5444 val = read_expr(state, def);
5445 return triple(state, OP_VAL, def->type,
5446 write_expr(state, def,
5447 mk_add_expr(state, val, int_const(state, &int_type, 1)))
5451 static struct triple *mk_post_dec_expr(
5452 struct compile_state *state, struct triple *def)
5456 val = read_expr(state, def);
5457 return triple(state, OP_VAL, def->type,
5458 write_expr(state, def,
5459 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
5463 static struct triple *mk_subscript_expr(
5464 struct compile_state *state, struct triple *left, struct triple *right)
5466 left = read_expr(state, left);
5467 right = read_expr(state, right);
5468 if (!is_pointer(left) && !is_pointer(right)) {
5469 error(state, left, "subscripted value is not a pointer");
5471 return mk_deref_expr(state, mk_add_expr(state, left, right));
5474 static struct triple *mk_cast_expr(
5475 struct compile_state *state, struct type *type, struct triple *expr)
5478 def = read_expr(state, expr);
5479 def = triple(state, OP_COPY, type, def, 0);
5484 * Compile time evaluation
5485 * ===========================
5487 static int is_const(struct triple *ins)
5489 return IS_CONST_OP(ins->op);
5492 static int constants_equal(struct compile_state *state,
5493 struct triple *left, struct triple *right)
5496 if (!is_const(left) || !is_const(right)) {
5499 else if (left->op != right->op) {
5502 else if (!equiv_types(left->type, right->type)) {
5509 if (left->u.cval == right->u.cval) {
5515 size_t lsize, rsize;
5516 lsize = size_of(state, left->type);
5517 rsize = size_of(state, right->type);
5518 if (lsize != rsize) {
5521 if (memcmp(left->u.blob, right->u.blob, lsize) == 0) {
5527 if ((MISC(left, 0) == MISC(right, 0)) &&
5528 (left->u.cval == right->u.cval)) {
5533 internal_error(state, left, "uknown constant type");
5540 static int is_zero(struct triple *ins)
5542 return is_const(ins) && (ins->u.cval == 0);
5545 static int is_one(struct triple *ins)
5547 return is_const(ins) && (ins->u.cval == 1);
5550 static long_t bit_count(ulong_t value)
5555 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
5566 static long_t bsr(ulong_t value)
5569 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
5580 static long_t bsf(ulong_t value)
5583 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
5594 static long_t log2(ulong_t value)
5599 static long_t tlog2(struct triple *ins)
5601 return log2(ins->u.cval);
5604 static int is_pow2(struct triple *ins)
5606 ulong_t value, mask;
5608 if (!is_const(ins)) {
5611 value = ins->u.cval;
5618 return ((value & mask) == value);
5621 static ulong_t read_const(struct compile_state *state,
5622 struct triple *ins, struct triple **expr)
5626 switch(rhs->type->type &TYPE_MASK) {
5638 internal_error(state, rhs, "bad type to read_const\n");
5644 static long_t read_sconst(struct triple *ins, struct triple **expr)
5648 return (long_t)(rhs->u.cval);
5651 static void unuse_rhs(struct compile_state *state, struct triple *ins)
5653 struct triple **expr;
5654 expr = triple_rhs(state, ins, 0);
5655 for(;expr;expr = triple_rhs(state, ins, expr)) {
5657 unuse_triple(*expr, ins);
5663 static void unuse_lhs(struct compile_state *state, struct triple *ins)
5665 struct triple **expr;
5666 expr = triple_lhs(state, ins, 0);
5667 for(;expr;expr = triple_lhs(state, ins, expr)) {
5668 unuse_triple(*expr, ins);
5673 static void check_lhs(struct compile_state *state, struct triple *ins)
5675 struct triple **expr;
5676 expr = triple_lhs(state, ins, 0);
5677 for(;expr;expr = triple_lhs(state, ins, expr)) {
5678 internal_error(state, ins, "unexpected lhs");
5682 static void check_targ(struct compile_state *state, struct triple *ins)
5684 struct triple **expr;
5685 expr = triple_targ(state, ins, 0);
5686 for(;expr;expr = triple_targ(state, ins, expr)) {
5687 internal_error(state, ins, "unexpected targ");
5691 static void wipe_ins(struct compile_state *state, struct triple *ins)
5693 /* Becareful which instructions you replace the wiped
5694 * instruction with, as there are not enough slots
5695 * in all instructions to hold all others.
5697 check_targ(state, ins);
5698 unuse_rhs(state, ins);
5699 unuse_lhs(state, ins);
5702 static void mkcopy(struct compile_state *state,
5703 struct triple *ins, struct triple *rhs)
5705 wipe_ins(state, ins);
5707 ins->sizes = TRIPLE_SIZES(0, 1, 0, 0);
5709 use_triple(RHS(ins, 0), ins);
5712 static void mkconst(struct compile_state *state,
5713 struct triple *ins, ulong_t value)
5715 if (!is_integral(ins) && !is_pointer(ins)) {
5716 internal_error(state, ins, "unknown type to make constant\n");
5718 wipe_ins(state, ins);
5719 ins->op = OP_INTCONST;
5720 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
5721 ins->u.cval = value;
5724 static void mkaddr_const(struct compile_state *state,
5725 struct triple *ins, struct triple *sdecl, ulong_t value)
5727 if (sdecl->op != OP_SDECL) {
5728 internal_error(state, ins, "bad base for addrconst");
5730 wipe_ins(state, ins);
5731 ins->op = OP_ADDRCONST;
5732 ins->sizes = TRIPLE_SIZES(0, 0, 1, 0);
5733 MISC(ins, 0) = sdecl;
5734 ins->u.cval = value;
5735 use_triple(sdecl, ins);
5738 /* Transform multicomponent variables into simple register variables */
5739 static void flatten_structures(struct compile_state *state)
5741 struct triple *ins, *first;
5742 first = RHS(state->main_function, 0);
5744 /* Pass one expand structure values into valvecs.
5748 struct triple *next;
5750 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5751 if (ins->op == OP_VAL_VEC) {
5754 else if ((ins->op == OP_LOAD) || (ins->op == OP_READ)) {
5755 struct triple *def, **vector;
5762 get_occurance(ins->occurance);
5763 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
5766 vector = &RHS(next, 0);
5767 tptr = next->type->left;
5768 for(i = 0; i < next->type->elements; i++) {
5769 struct triple *sfield;
5772 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5773 mtype = mtype->left;
5775 sfield = deref_field(state, def, mtype->field_ident);
5778 state, op, mtype, sfield, 0);
5779 put_occurance(vector[i]->occurance);
5780 get_occurance(next->occurance);
5781 vector[i]->occurance = next->occurance;
5784 propogate_use(state, ins, next);
5785 flatten(state, ins, next);
5786 free_triple(state, ins);
5788 else if ((ins->op == OP_STORE) || (ins->op == OP_WRITE)) {
5789 struct triple *src, *dst, **vector;
5797 get_occurance(ins->occurance);
5798 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
5801 vector = &RHS(next, 0);
5802 tptr = next->type->left;
5803 for(i = 0; i < ins->type->elements; i++) {
5804 struct triple *dfield, *sfield;
5807 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5808 mtype = mtype->left;
5810 sfield = deref_field(state, src, mtype->field_ident);
5811 dfield = deref_field(state, dst, mtype->field_ident);
5813 state, op, mtype, dfield, sfield);
5814 put_occurance(vector[i]->occurance);
5815 get_occurance(next->occurance);
5816 vector[i]->occurance = next->occurance;
5819 propogate_use(state, ins, next);
5820 flatten(state, ins, next);
5821 free_triple(state, ins);
5825 } while(ins != first);
5826 /* Pass two flatten the valvecs.
5830 struct triple *next;
5832 if (ins->op == OP_VAL_VEC) {
5833 release_triple(state, ins);
5836 } while(ins != first);
5837 /* Pass three verify the state and set ->id to 0.
5841 ins->id &= ~TRIPLE_FLAG_FLATTENED;
5842 if ((ins->op != OP_BLOBCONST) && (ins->op != OP_SDECL) &&
5843 ((ins->type->type & TYPE_MASK) == TYPE_STRUCT)) {
5844 internal_error(state, ins, "STRUCT_TYPE remains?");
5846 if (ins->op == OP_DOT) {
5847 internal_error(state, ins, "OP_DOT remains?");
5849 if (ins->op == OP_VAL_VEC) {
5850 internal_error(state, ins, "OP_VAL_VEC remains?");
5853 } while(ins != first);
5856 /* For those operations that cannot be simplified */
5857 static void simplify_noop(struct compile_state *state, struct triple *ins)
5862 static void simplify_smul(struct compile_state *state, struct triple *ins)
5864 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5867 RHS(ins, 0) = RHS(ins, 1);
5870 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5872 left = read_sconst(ins, &RHS(ins, 0));
5873 right = read_sconst(ins, &RHS(ins, 1));
5874 mkconst(state, ins, left * right);
5876 else if (is_zero(RHS(ins, 1))) {
5877 mkconst(state, ins, 0);
5879 else if (is_one(RHS(ins, 1))) {
5880 mkcopy(state, ins, RHS(ins, 0));
5882 else if (is_pow2(RHS(ins, 1))) {
5884 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5886 insert_triple(state, ins, val);
5887 unuse_triple(RHS(ins, 1), ins);
5888 use_triple(val, ins);
5893 static void simplify_umul(struct compile_state *state, struct triple *ins)
5895 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5898 RHS(ins, 0) = RHS(ins, 1);
5901 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5902 ulong_t left, right;
5903 left = read_const(state, ins, &RHS(ins, 0));
5904 right = read_const(state, ins, &RHS(ins, 1));
5905 mkconst(state, ins, left * right);
5907 else if (is_zero(RHS(ins, 1))) {
5908 mkconst(state, ins, 0);
5910 else if (is_one(RHS(ins, 1))) {
5911 mkcopy(state, ins, RHS(ins, 0));
5913 else if (is_pow2(RHS(ins, 1))) {
5915 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5917 insert_triple(state, ins, val);
5918 unuse_triple(RHS(ins, 1), ins);
5919 use_triple(val, ins);
5924 static void simplify_sdiv(struct compile_state *state, struct triple *ins)
5926 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5928 left = read_sconst(ins, &RHS(ins, 0));
5929 right = read_sconst(ins, &RHS(ins, 1));
5930 mkconst(state, ins, left / right);
5932 else if (is_zero(RHS(ins, 0))) {
5933 mkconst(state, ins, 0);
5935 else if (is_zero(RHS(ins, 1))) {
5936 error(state, ins, "division by zero");
5938 else if (is_one(RHS(ins, 1))) {
5939 mkcopy(state, ins, RHS(ins, 0));
5941 else if (is_pow2(RHS(ins, 1))) {
5943 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5945 insert_triple(state, ins, val);
5946 unuse_triple(RHS(ins, 1), ins);
5947 use_triple(val, ins);
5952 static void simplify_udiv(struct compile_state *state, struct triple *ins)
5954 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5955 ulong_t left, right;
5956 left = read_const(state, ins, &RHS(ins, 0));
5957 right = read_const(state, ins, &RHS(ins, 1));
5958 mkconst(state, ins, left / right);
5960 else if (is_zero(RHS(ins, 0))) {
5961 mkconst(state, ins, 0);
5963 else if (is_zero(RHS(ins, 1))) {
5964 error(state, ins, "division by zero");
5966 else if (is_one(RHS(ins, 1))) {
5967 mkcopy(state, ins, RHS(ins, 0));
5969 else if (is_pow2(RHS(ins, 1))) {
5971 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5973 insert_triple(state, ins, val);
5974 unuse_triple(RHS(ins, 1), ins);
5975 use_triple(val, ins);
5980 static void simplify_smod(struct compile_state *state, struct triple *ins)
5982 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5984 left = read_const(state, ins, &RHS(ins, 0));
5985 right = read_const(state, ins, &RHS(ins, 1));
5986 mkconst(state, ins, left % right);
5988 else if (is_zero(RHS(ins, 0))) {
5989 mkconst(state, ins, 0);
5991 else if (is_zero(RHS(ins, 1))) {
5992 error(state, ins, "division by zero");
5994 else if (is_one(RHS(ins, 1))) {
5995 mkconst(state, ins, 0);
5997 else if (is_pow2(RHS(ins, 1))) {
5999 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
6001 insert_triple(state, ins, val);
6002 unuse_triple(RHS(ins, 1), ins);
6003 use_triple(val, ins);
6007 static void simplify_umod(struct compile_state *state, struct triple *ins)
6009 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6010 ulong_t left, right;
6011 left = read_const(state, ins, &RHS(ins, 0));
6012 right = read_const(state, ins, &RHS(ins, 1));
6013 mkconst(state, ins, left % right);
6015 else if (is_zero(RHS(ins, 0))) {
6016 mkconst(state, ins, 0);
6018 else if (is_zero(RHS(ins, 1))) {
6019 error(state, ins, "division by zero");
6021 else if (is_one(RHS(ins, 1))) {
6022 mkconst(state, ins, 0);
6024 else if (is_pow2(RHS(ins, 1))) {
6026 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
6028 insert_triple(state, ins, val);
6029 unuse_triple(RHS(ins, 1), ins);
6030 use_triple(val, ins);
6035 static void simplify_add(struct compile_state *state, struct triple *ins)
6037 /* start with the pointer on the left */
6038 if (is_pointer(RHS(ins, 1))) {
6041 RHS(ins, 0) = RHS(ins, 1);
6044 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6045 if (RHS(ins, 0)->op == OP_INTCONST) {
6046 ulong_t left, right;
6047 left = read_const(state, ins, &RHS(ins, 0));
6048 right = read_const(state, ins, &RHS(ins, 1));
6049 mkconst(state, ins, left + right);
6051 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
6052 struct triple *sdecl;
6053 ulong_t left, right;
6054 sdecl = MISC(RHS(ins, 0), 0);
6055 left = RHS(ins, 0)->u.cval;
6056 right = RHS(ins, 1)->u.cval;
6057 mkaddr_const(state, ins, sdecl, left + right);
6060 internal_warning(state, ins, "Optimize me!");
6063 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
6066 RHS(ins, 1) = RHS(ins, 0);
6071 static void simplify_sub(struct compile_state *state, struct triple *ins)
6073 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6074 if (RHS(ins, 0)->op == OP_INTCONST) {
6075 ulong_t left, right;
6076 left = read_const(state, ins, &RHS(ins, 0));
6077 right = read_const(state, ins, &RHS(ins, 1));
6078 mkconst(state, ins, left - right);
6080 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
6081 struct triple *sdecl;
6082 ulong_t left, right;
6083 sdecl = MISC(RHS(ins, 0), 0);
6084 left = RHS(ins, 0)->u.cval;
6085 right = RHS(ins, 1)->u.cval;
6086 mkaddr_const(state, ins, sdecl, left - right);
6089 internal_warning(state, ins, "Optimize me!");
6094 static void simplify_sl(struct compile_state *state, struct triple *ins)
6096 if (is_const(RHS(ins, 1))) {
6098 right = read_const(state, ins, &RHS(ins, 1));
6099 if (right >= (size_of(state, ins->type)*8)) {
6100 warning(state, ins, "left shift count >= width of type");
6103 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6104 ulong_t left, right;
6105 left = read_const(state, ins, &RHS(ins, 0));
6106 right = read_const(state, ins, &RHS(ins, 1));
6107 mkconst(state, ins, left << right);
6111 static void simplify_usr(struct compile_state *state, struct triple *ins)
6113 if (is_const(RHS(ins, 1))) {
6115 right = read_const(state, ins, &RHS(ins, 1));
6116 if (right >= (size_of(state, ins->type)*8)) {
6117 warning(state, ins, "right shift count >= width of type");
6120 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6121 ulong_t left, right;
6122 left = read_const(state, ins, &RHS(ins, 0));
6123 right = read_const(state, ins, &RHS(ins, 1));
6124 mkconst(state, ins, left >> right);
6128 static void simplify_ssr(struct compile_state *state, struct triple *ins)
6130 if (is_const(RHS(ins, 1))) {
6132 right = read_const(state, ins, &RHS(ins, 1));
6133 if (right >= (size_of(state, ins->type)*8)) {
6134 warning(state, ins, "right shift count >= width of type");
6137 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6139 left = read_sconst(ins, &RHS(ins, 0));
6140 right = read_sconst(ins, &RHS(ins, 1));
6141 mkconst(state, ins, left >> right);
6145 static void simplify_and(struct compile_state *state, struct triple *ins)
6147 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6148 ulong_t left, right;
6149 left = read_const(state, ins, &RHS(ins, 0));
6150 right = read_const(state, ins, &RHS(ins, 1));
6151 mkconst(state, ins, left & right);
6155 static void simplify_or(struct compile_state *state, struct triple *ins)
6157 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6158 ulong_t left, right;
6159 left = read_const(state, ins, &RHS(ins, 0));
6160 right = read_const(state, ins, &RHS(ins, 1));
6161 mkconst(state, ins, left | right);
6165 static void simplify_xor(struct compile_state *state, struct triple *ins)
6167 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6168 ulong_t left, right;
6169 left = read_const(state, ins, &RHS(ins, 0));
6170 right = read_const(state, ins, &RHS(ins, 1));
6171 mkconst(state, ins, left ^ right);
6175 static void simplify_pos(struct compile_state *state, struct triple *ins)
6177 if (is_const(RHS(ins, 0))) {
6178 mkconst(state, ins, RHS(ins, 0)->u.cval);
6181 mkcopy(state, ins, RHS(ins, 0));
6185 static void simplify_neg(struct compile_state *state, struct triple *ins)
6187 if (is_const(RHS(ins, 0))) {
6189 left = read_const(state, ins, &RHS(ins, 0));
6190 mkconst(state, ins, -left);
6192 else if (RHS(ins, 0)->op == OP_NEG) {
6193 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
6197 static void simplify_invert(struct compile_state *state, struct triple *ins)
6199 if (is_const(RHS(ins, 0))) {
6201 left = read_const(state, ins, &RHS(ins, 0));
6202 mkconst(state, ins, ~left);
6206 static void simplify_eq(struct compile_state *state, struct triple *ins)
6208 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6209 ulong_t left, right;
6210 left = read_const(state, ins, &RHS(ins, 0));
6211 right = read_const(state, ins, &RHS(ins, 1));
6212 mkconst(state, ins, left == right);
6214 else if (RHS(ins, 0) == RHS(ins, 1)) {
6215 mkconst(state, ins, 1);
6219 static void simplify_noteq(struct compile_state *state, struct triple *ins)
6221 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6222 ulong_t left, right;
6223 left = read_const(state, ins, &RHS(ins, 0));
6224 right = read_const(state, ins, &RHS(ins, 1));
6225 mkconst(state, ins, left != right);
6227 else if (RHS(ins, 0) == RHS(ins, 1)) {
6228 mkconst(state, ins, 0);
6232 static void simplify_sless(struct compile_state *state, struct triple *ins)
6234 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6236 left = read_sconst(ins, &RHS(ins, 0));
6237 right = read_sconst(ins, &RHS(ins, 1));
6238 mkconst(state, ins, left < right);
6240 else if (RHS(ins, 0) == RHS(ins, 1)) {
6241 mkconst(state, ins, 0);
6245 static void simplify_uless(struct compile_state *state, struct triple *ins)
6247 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6248 ulong_t left, right;
6249 left = read_const(state, ins, &RHS(ins, 0));
6250 right = read_const(state, ins, &RHS(ins, 1));
6251 mkconst(state, ins, left < right);
6253 else if (is_zero(RHS(ins, 0))) {
6254 mkconst(state, ins, 1);
6256 else if (RHS(ins, 0) == RHS(ins, 1)) {
6257 mkconst(state, ins, 0);
6261 static void simplify_smore(struct compile_state *state, struct triple *ins)
6263 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6265 left = read_sconst(ins, &RHS(ins, 0));
6266 right = read_sconst(ins, &RHS(ins, 1));
6267 mkconst(state, ins, left > right);
6269 else if (RHS(ins, 0) == RHS(ins, 1)) {
6270 mkconst(state, ins, 0);
6274 static void simplify_umore(struct compile_state *state, struct triple *ins)
6276 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6277 ulong_t left, right;
6278 left = read_const(state, ins, &RHS(ins, 0));
6279 right = read_const(state, ins, &RHS(ins, 1));
6280 mkconst(state, ins, left > right);
6282 else if (is_zero(RHS(ins, 1))) {
6283 mkconst(state, ins, 1);
6285 else if (RHS(ins, 0) == RHS(ins, 1)) {
6286 mkconst(state, ins, 0);
6291 static void simplify_slesseq(struct compile_state *state, struct triple *ins)
6293 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6295 left = read_sconst(ins, &RHS(ins, 0));
6296 right = read_sconst(ins, &RHS(ins, 1));
6297 mkconst(state, ins, left <= right);
6299 else if (RHS(ins, 0) == RHS(ins, 1)) {
6300 mkconst(state, ins, 1);
6304 static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
6306 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6307 ulong_t left, right;
6308 left = read_const(state, ins, &RHS(ins, 0));
6309 right = read_const(state, ins, &RHS(ins, 1));
6310 mkconst(state, ins, left <= right);
6312 else if (is_zero(RHS(ins, 0))) {
6313 mkconst(state, ins, 1);
6315 else if (RHS(ins, 0) == RHS(ins, 1)) {
6316 mkconst(state, ins, 1);
6320 static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
6322 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 0))) {
6324 left = read_sconst(ins, &RHS(ins, 0));
6325 right = read_sconst(ins, &RHS(ins, 1));
6326 mkconst(state, ins, left >= right);
6328 else if (RHS(ins, 0) == RHS(ins, 1)) {
6329 mkconst(state, ins, 1);
6333 static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
6335 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6336 ulong_t left, right;
6337 left = read_const(state, ins, &RHS(ins, 0));
6338 right = read_const(state, ins, &RHS(ins, 1));
6339 mkconst(state, ins, left >= right);
6341 else if (is_zero(RHS(ins, 1))) {
6342 mkconst(state, ins, 1);
6344 else if (RHS(ins, 0) == RHS(ins, 1)) {
6345 mkconst(state, ins, 1);
6349 static void simplify_lfalse(struct compile_state *state, struct triple *ins)
6351 if (is_const(RHS(ins, 0))) {
6353 left = read_const(state, ins, &RHS(ins, 0));
6354 mkconst(state, ins, left == 0);
6356 /* Otherwise if I am the only user... */
6357 else if ((RHS(ins, 0)->use->member == ins) && (RHS(ins, 0)->use->next == 0)) {
6359 /* Invert a boolean operation */
6360 switch(RHS(ins, 0)->op) {
6361 case OP_LTRUE: RHS(ins, 0)->op = OP_LFALSE; break;
6362 case OP_LFALSE: RHS(ins, 0)->op = OP_LTRUE; break;
6363 case OP_EQ: RHS(ins, 0)->op = OP_NOTEQ; break;
6364 case OP_NOTEQ: RHS(ins, 0)->op = OP_EQ; break;
6365 case OP_SLESS: RHS(ins, 0)->op = OP_SMOREEQ; break;
6366 case OP_ULESS: RHS(ins, 0)->op = OP_UMOREEQ; break;
6367 case OP_SMORE: RHS(ins, 0)->op = OP_SLESSEQ; break;
6368 case OP_UMORE: RHS(ins, 0)->op = OP_ULESSEQ; break;
6369 case OP_SLESSEQ: RHS(ins, 0)->op = OP_SMORE; break;
6370 case OP_ULESSEQ: RHS(ins, 0)->op = OP_UMORE; break;
6371 case OP_SMOREEQ: RHS(ins, 0)->op = OP_SLESS; break;
6372 case OP_UMOREEQ: RHS(ins, 0)->op = OP_ULESS; break;
6378 mkcopy(state, ins, RHS(ins, 0));
6383 static void simplify_ltrue (struct compile_state *state, struct triple *ins)
6385 if (is_const(RHS(ins, 0))) {
6387 left = read_const(state, ins, &RHS(ins, 0));
6388 mkconst(state, ins, left != 0);
6390 else switch(RHS(ins, 0)->op) {
6391 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
6392 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
6393 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
6394 mkcopy(state, ins, RHS(ins, 0));
6399 static void simplify_copy(struct compile_state *state, struct triple *ins)
6401 if (is_const(RHS(ins, 0))) {
6402 switch(RHS(ins, 0)->op) {
6406 left = read_const(state, ins, &RHS(ins, 0));
6407 mkconst(state, ins, left);
6412 struct triple *sdecl;
6414 sdecl = MISC(RHS(ins, 0), 0);
6415 offset = RHS(ins, 0)->u.cval;
6416 mkaddr_const(state, ins, sdecl, offset);
6420 internal_error(state, ins, "uknown constant");
6426 static void simplify_branch(struct compile_state *state, struct triple *ins)
6428 struct block *block;
6429 if (ins->op != OP_BRANCH) {
6430 internal_error(state, ins, "not branch");
6432 if (ins->use != 0) {
6433 internal_error(state, ins, "branch use");
6435 #warning "FIXME implement simplify branch."
6436 /* The challenge here with simplify branch is that I need to
6437 * make modifications to the control flow graph as well
6438 * as to the branch instruction itself.
6440 block = ins->u.block;
6442 if (TRIPLE_RHS(ins->sizes) && is_const(RHS(ins, 0))) {
6443 struct triple *targ;
6445 value = read_const(state, ins, &RHS(ins, 0));
6446 unuse_triple(RHS(ins, 0), ins);
6447 targ = TARG(ins, 0);
6448 ins->sizes = TRIPLE_SIZES(0, 0, 0, 1);
6450 unuse_triple(ins->next, ins);
6451 TARG(ins, 0) = targ;
6454 unuse_triple(targ, ins);
6455 TARG(ins, 0) = ins->next;
6457 #warning "FIXME handle the case of making a branch unconditional"
6459 if (TARG(ins, 0) == ins->next) {
6460 unuse_triple(ins->next, ins);
6461 if (TRIPLE_RHS(ins->sizes)) {
6462 unuse_triple(RHS(ins, 0), ins);
6463 unuse_triple(ins->next, ins);
6465 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
6468 internal_error(state, ins, "noop use != 0");
6470 #warning "FIXME handle the case of killing a branch"
6474 int phi_present(struct block *block)
6482 if (ptr->op == OP_PHI) {
6486 } while(ptr != block->last);
6490 static void simplify_label(struct compile_state *state, struct triple *ins)
6492 #warning "FIXME enable simplify_label"
6493 struct triple *first, *last;
6494 first = RHS(state->main_function, 0);
6496 /* Ignore the first and last instructions */
6497 if ((ins == first) || (ins == last)) {
6500 if (ins->use == 0) {
6503 else if (ins->prev->op == OP_LABEL) {
6504 struct block *block;
6505 block = ins->prev->u.block;
6506 /* In general it is not safe to merge one label that
6507 * imediately follows another. The problem is that the empty
6508 * looking block may have phi functions that depend on it.
6511 (!phi_present(block->left) &&
6512 !phi_present(block->right)))
6514 struct triple_set *user, *next;
6516 for(user = ins->use; user; user = next) {
6520 if (TARG(use, 0) == ins) {
6521 TARG(use, 0) = ins->prev;
6522 unuse_triple(ins, use);
6523 use_triple(ins->prev, use);
6527 internal_error(state, ins, "noop use != 0");
6533 static void simplify_phi(struct compile_state *state, struct triple *ins)
6535 struct triple **expr;
6537 expr = triple_rhs(state, ins, 0);
6538 if (!*expr || !is_const(*expr)) {
6541 value = read_const(state, ins, expr);
6542 for(;expr;expr = triple_rhs(state, ins, expr)) {
6543 if (!*expr || !is_const(*expr)) {
6546 if (value != read_const(state, ins, expr)) {
6550 mkconst(state, ins, value);
6554 static void simplify_bsf(struct compile_state *state, struct triple *ins)
6556 if (is_const(RHS(ins, 0))) {
6558 left = read_const(state, ins, &RHS(ins, 0));
6559 mkconst(state, ins, bsf(left));
6563 static void simplify_bsr(struct compile_state *state, struct triple *ins)
6565 if (is_const(RHS(ins, 0))) {
6567 left = read_const(state, ins, &RHS(ins, 0));
6568 mkconst(state, ins, bsr(left));
6573 typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
6574 static const simplify_t table_simplify[] = {
6576 #define simplify_sdivt simplify_noop
6577 #define simplify_udivt simplify_noop
6580 #define simplify_smul simplify_noop
6581 #define simplify_umul simplify_noop
6582 #define simplify_sdiv simplify_noop
6583 #define simplify_udiv simplify_noop
6584 #define simplify_smod simplify_noop
6585 #define simplify_umod simplify_noop
6588 #define simplify_add simplify_noop
6589 #define simplify_sub simplify_noop
6592 #define simplify_sl simplify_noop
6593 #define simplify_usr simplify_noop
6594 #define simplify_ssr simplify_noop
6597 #define simplify_and simplify_noop
6598 #define simplify_xor simplify_noop
6599 #define simplify_or simplify_noop
6602 #define simplify_pos simplify_noop
6603 #define simplify_neg simplify_noop
6604 #define simplify_invert simplify_noop
6608 #define simplify_eq simplify_noop
6609 #define simplify_noteq simplify_noop
6612 #define simplify_sless simplify_noop
6613 #define simplify_uless simplify_noop
6614 #define simplify_smore simplify_noop
6615 #define simplify_umore simplify_noop
6618 #define simplify_slesseq simplify_noop
6619 #define simplify_ulesseq simplify_noop
6620 #define simplify_smoreeq simplify_noop
6621 #define simplify_umoreeq simplify_noop
6624 #define simplify_lfalse simplify_noop
6627 #define simplify_ltrue simplify_noop
6631 #define simplify_copy simplify_noop
6635 #define simplify_branch simplify_noop
6638 #define simplify_label simplify_noop
6642 #define simplify_phi simplify_noop
6646 #define simplify_bsf simplify_noop
6647 #define simplify_bsr simplify_noop
6650 [OP_SDIVT ] = simplify_sdivt,
6651 [OP_UDIVT ] = simplify_udivt,
6652 [OP_SMUL ] = simplify_smul,
6653 [OP_UMUL ] = simplify_umul,
6654 [OP_SDIV ] = simplify_sdiv,
6655 [OP_UDIV ] = simplify_udiv,
6656 [OP_SMOD ] = simplify_smod,
6657 [OP_UMOD ] = simplify_umod,
6658 [OP_ADD ] = simplify_add,
6659 [OP_SUB ] = simplify_sub,
6660 [OP_SL ] = simplify_sl,
6661 [OP_USR ] = simplify_usr,
6662 [OP_SSR ] = simplify_ssr,
6663 [OP_AND ] = simplify_and,
6664 [OP_XOR ] = simplify_xor,
6665 [OP_OR ] = simplify_or,
6666 [OP_POS ] = simplify_pos,
6667 [OP_NEG ] = simplify_neg,
6668 [OP_INVERT ] = simplify_invert,
6670 [OP_EQ ] = simplify_eq,
6671 [OP_NOTEQ ] = simplify_noteq,
6672 [OP_SLESS ] = simplify_sless,
6673 [OP_ULESS ] = simplify_uless,
6674 [OP_SMORE ] = simplify_smore,
6675 [OP_UMORE ] = simplify_umore,
6676 [OP_SLESSEQ ] = simplify_slesseq,
6677 [OP_ULESSEQ ] = simplify_ulesseq,
6678 [OP_SMOREEQ ] = simplify_smoreeq,
6679 [OP_UMOREEQ ] = simplify_umoreeq,
6680 [OP_LFALSE ] = simplify_lfalse,
6681 [OP_LTRUE ] = simplify_ltrue,
6683 [OP_LOAD ] = simplify_noop,
6684 [OP_STORE ] = simplify_noop,
6686 [OP_NOOP ] = simplify_noop,
6688 [OP_INTCONST ] = simplify_noop,
6689 [OP_BLOBCONST ] = simplify_noop,
6690 [OP_ADDRCONST ] = simplify_noop,
6692 [OP_WRITE ] = simplify_noop,
6693 [OP_READ ] = simplify_noop,
6694 [OP_COPY ] = simplify_copy,
6695 [OP_PIECE ] = simplify_noop,
6696 [OP_ASM ] = simplify_noop,
6698 [OP_DOT ] = simplify_noop,
6699 [OP_VAL_VEC ] = simplify_noop,
6701 [OP_LIST ] = simplify_noop,
6702 [OP_BRANCH ] = simplify_branch,
6703 [OP_LABEL ] = simplify_label,
6704 [OP_ADECL ] = simplify_noop,
6705 [OP_SDECL ] = simplify_noop,
6706 [OP_PHI ] = simplify_phi,
6708 [OP_INB ] = simplify_noop,
6709 [OP_INW ] = simplify_noop,
6710 [OP_INL ] = simplify_noop,
6711 [OP_OUTB ] = simplify_noop,
6712 [OP_OUTW ] = simplify_noop,
6713 [OP_OUTL ] = simplify_noop,
6714 [OP_BSF ] = simplify_bsf,
6715 [OP_BSR ] = simplify_bsr,
6716 [OP_RDMSR ] = simplify_noop,
6717 [OP_WRMSR ] = simplify_noop,
6718 [OP_HLT ] = simplify_noop,
6721 static void simplify(struct compile_state *state, struct triple *ins)
6724 simplify_t do_simplify;
6728 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
6732 do_simplify = table_simplify[op];
6735 internal_error(state, ins, "cannot simplify op: %d %s\n",
6739 do_simplify(state, ins);
6740 } while(ins->op != op);
6743 static void simplify_all(struct compile_state *state)
6745 struct triple *ins, *first;
6746 first = RHS(state->main_function, 0);
6749 simplify(state, ins);
6751 }while(ins != first);
6756 * ============================
6759 static void register_builtin_function(struct compile_state *state,
6760 const char *name, int op, struct type *rtype, ...)
6762 struct type *ftype, *atype, *param, **next;
6763 struct triple *def, *arg, *result, *work, *last, *first;
6764 struct hash_entry *ident;
6765 struct file_state file;
6771 /* Dummy file state to get debug handling right */
6772 memset(&file, 0, sizeof(file));
6773 file.basename = "<built-in>";
6775 file.report_line = 1;
6776 file.report_name = file.basename;
6777 file.prev = state->file;
6778 state->file = &file;
6779 state->function = name;
6781 /* Find the Parameter count */
6782 valid_op(state, op);
6783 parameters = table_ops[op].rhs;
6784 if (parameters < 0 ) {
6785 internal_error(state, 0, "Invalid builtin parameter count");
6788 /* Find the function type */
6789 ftype = new_type(TYPE_FUNCTION, rtype, 0);
6790 next = &ftype->right;
6791 va_start(args, rtype);
6792 for(i = 0; i < parameters; i++) {
6793 atype = va_arg(args, struct type *);
6797 *next = new_type(TYPE_PRODUCT, *next, atype);
6798 next = &((*next)->right);
6806 /* Generate the needed triples */
6807 def = triple(state, OP_LIST, ftype, 0, 0);
6808 first = label(state);
6809 RHS(def, 0) = first;
6811 /* Now string them together */
6812 param = ftype->right;
6813 for(i = 0; i < parameters; i++) {
6814 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6815 atype = param->left;
6819 arg = flatten(state, first, variable(state, atype));
6820 param = param->right;
6823 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
6824 result = flatten(state, first, variable(state, rtype));
6826 MISC(def, 0) = result;
6827 work = new_triple(state, op, rtype, -1, parameters);
6828 for(i = 0, arg = first->next; i < parameters; i++, arg = arg->next) {
6829 RHS(work, i) = read_expr(state, arg);
6831 if (result && ((rtype->type & TYPE_MASK) == TYPE_STRUCT)) {
6833 /* Populate the LHS with the target registers */
6834 work = flatten(state, first, work);
6835 work->type = &void_type;
6836 param = rtype->left;
6837 if (rtype->elements != TRIPLE_LHS(work->sizes)) {
6838 internal_error(state, 0, "Invalid result type");
6840 val = new_triple(state, OP_VAL_VEC, rtype, -1, -1);
6841 for(i = 0; i < rtype->elements; i++) {
6842 struct triple *piece;
6844 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6845 atype = param->left;
6847 if (!TYPE_ARITHMETIC(atype->type) &&
6848 !TYPE_PTR(atype->type)) {
6849 internal_error(state, 0, "Invalid lhs type");
6851 piece = triple(state, OP_PIECE, atype, work, 0);
6853 LHS(work, i) = piece;
6854 RHS(val, i) = piece;
6859 work = write_expr(state, result, work);
6861 work = flatten(state, first, work);
6862 last = flatten(state, first, label(state));
6863 name_len = strlen(name);
6864 ident = lookup(state, name, name_len);
6865 symbol(state, ident, &ident->sym_ident, def, ftype);
6867 state->file = file.prev;
6868 state->function = 0;
6870 fprintf(stdout, "\n");
6871 loc(stdout, state, 0);
6872 fprintf(stdout, "\n__________ builtin_function _________\n");
6873 print_triple(state, def);
6874 fprintf(stdout, "__________ builtin_function _________ done\n\n");
6878 static struct type *partial_struct(struct compile_state *state,
6879 const char *field_name, struct type *type, struct type *rest)
6881 struct hash_entry *field_ident;
6882 struct type *result;
6885 field_name_len = strlen(field_name);
6886 field_ident = lookup(state, field_name, field_name_len);
6888 result = clone_type(0, type);
6889 result->field_ident = field_ident;
6892 result = new_type(TYPE_PRODUCT, result, rest);
6897 static struct type *register_builtin_type(struct compile_state *state,
6898 const char *name, struct type *type)
6900 struct hash_entry *ident;
6903 name_len = strlen(name);
6904 ident = lookup(state, name, name_len);
6906 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6907 ulong_t elements = 0;
6909 type = new_type(TYPE_STRUCT, type, 0);
6911 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
6913 field = field->right;
6916 symbol(state, ident, &ident->sym_struct, 0, type);
6917 type->type_ident = ident;
6918 type->elements = elements;
6920 symbol(state, ident, &ident->sym_ident, 0, type);
6921 ident->tok = TOK_TYPE_NAME;
6926 static void register_builtins(struct compile_state *state)
6928 struct type *div_type, *ldiv_type;
6929 struct type *udiv_type, *uldiv_type;
6930 struct type *msr_type;
6932 div_type = register_builtin_type(state, "__builtin_div_t",
6933 partial_struct(state, "quot", &int_type,
6934 partial_struct(state, "rem", &int_type, 0)));
6935 ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
6936 partial_struct(state, "quot", &long_type,
6937 partial_struct(state, "rem", &long_type, 0)));
6938 udiv_type = register_builtin_type(state, "__builtin_udiv_t",
6939 partial_struct(state, "quot", &uint_type,
6940 partial_struct(state, "rem", &uint_type, 0)));
6941 uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
6942 partial_struct(state, "quot", &ulong_type,
6943 partial_struct(state, "rem", &ulong_type, 0)));
6945 register_builtin_function(state, "__builtin_div", OP_SDIVT, div_type,
6946 &int_type, &int_type);
6947 register_builtin_function(state, "__builtin_ldiv", OP_SDIVT, ldiv_type,
6948 &long_type, &long_type);
6949 register_builtin_function(state, "__builtin_udiv", OP_UDIVT, udiv_type,
6950 &uint_type, &uint_type);
6951 register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
6952 &ulong_type, &ulong_type);
6954 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
6956 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
6958 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
6961 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
6962 &uchar_type, &ushort_type);
6963 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
6964 &ushort_type, &ushort_type);
6965 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
6966 &uint_type, &ushort_type);
6968 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
6970 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
6973 msr_type = register_builtin_type(state, "__builtin_msr_t",
6974 partial_struct(state, "lo", &ulong_type,
6975 partial_struct(state, "hi", &ulong_type, 0)));
6977 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
6979 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
6980 &ulong_type, &ulong_type, &ulong_type);
6982 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
6986 static struct type *declarator(
6987 struct compile_state *state, struct type *type,
6988 struct hash_entry **ident, int need_ident);
6989 static void decl(struct compile_state *state, struct triple *first);
6990 static struct type *specifier_qualifier_list(struct compile_state *state);
6991 static int isdecl_specifier(int tok);
6992 static struct type *decl_specifiers(struct compile_state *state);
6993 static int istype(int tok);
6994 static struct triple *expr(struct compile_state *state);
6995 static struct triple *assignment_expr(struct compile_state *state);
6996 static struct type *type_name(struct compile_state *state);
6997 static void statement(struct compile_state *state, struct triple *fist);
6999 static struct triple *call_expr(
7000 struct compile_state *state, struct triple *func)
7003 struct type *param, *type;
7004 ulong_t pvals, index;
7006 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
7007 error(state, 0, "Called object is not a function");
7009 if (func->op != OP_LIST) {
7010 internal_error(state, 0, "improper function");
7012 eat(state, TOK_LPAREN);
7013 /* Find the return type without any specifiers */
7014 type = clone_type(0, func->type->left);
7015 def = new_triple(state, OP_CALL, func->type, -1, -1);
7018 pvals = TRIPLE_RHS(def->sizes);
7019 MISC(def, 0) = func;
7021 param = func->type->right;
7022 for(index = 0; index < pvals; index++) {
7024 struct type *arg_type;
7025 val = read_expr(state, assignment_expr(state));
7027 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
7028 arg_type = param->left;
7030 write_compatible(state, arg_type, val->type);
7031 RHS(def, index) = val;
7032 if (index != (pvals - 1)) {
7033 eat(state, TOK_COMMA);
7034 param = param->right;
7037 eat(state, TOK_RPAREN);
7042 static struct triple *character_constant(struct compile_state *state)
7046 const signed char *str, *end;
7049 eat(state, TOK_LIT_CHAR);
7050 tk = &state->token[0];
7051 str = tk->val.str + 1;
7052 str_len = tk->str_len - 2;
7054 error(state, 0, "empty character constant");
7056 end = str + str_len;
7057 c = char_value(state, &str, end);
7059 error(state, 0, "multibyte character constant not supported");
7061 def = int_const(state, &char_type, (ulong_t)((long_t)c));
7065 static struct triple *string_constant(struct compile_state *state)
7070 const signed char *str, *end;
7071 signed char *buf, *ptr;
7075 type = new_type(TYPE_ARRAY, &char_type, 0);
7077 /* The while loop handles string concatenation */
7079 eat(state, TOK_LIT_STRING);
7080 tk = &state->token[0];
7081 str = tk->val.str + 1;
7082 str_len = tk->str_len - 2;
7084 error(state, 0, "negative string constant length");
7086 end = str + str_len;
7088 buf = xmalloc(type->elements + str_len + 1, "string_constant");
7089 memcpy(buf, ptr, type->elements);
7090 ptr = buf + type->elements;
7092 *ptr++ = char_value(state, &str, end);
7094 type->elements = ptr - buf;
7095 } while(peek(state) == TOK_LIT_STRING);
7097 type->elements += 1;
7098 def = triple(state, OP_BLOBCONST, type, 0, 0);
7104 static struct triple *integer_constant(struct compile_state *state)
7113 eat(state, TOK_LIT_INT);
7114 tk = &state->token[0];
7116 decimal = (tk->val.str[0] != '0');
7117 val = strtoul(tk->val.str, &end, 0);
7119 if ((val == UINT_MAX) && (errno == ERANGE)) {
7121 if ((val == ULONG_MAX) && (errno == ERANGE)) {
7123 error(state, 0, "Integer constant to large");
7126 if ((*end == 'u') || (*end == 'U')) {
7130 if ((*end == 'l') || (*end == 'L')) {
7134 if ((*end == 'u') || (*end == 'U')) {
7139 error(state, 0, "Junk at end of integer constant");
7147 if (!decimal && (val > INT_MAX)) {
7149 if (!decimal && (val > LONG_MAX)) {
7156 if (val > UINT_MAX) {
7162 if (!decimal && (val > INT_MAX) && (val <= UINT_MAX)) {
7166 else if (!decimal && (val > INT_MAX)) {
7168 else if (!decimal && (val > LONG_MAX)) {
7172 else if (val > INT_MAX) {
7176 def = int_const(state, type, val);
7180 static struct triple *primary_expr(struct compile_state *state)
7188 struct hash_entry *ident;
7189 /* Here ident is either:
7192 * an enumeration constant.
7194 eat(state, TOK_IDENT);
7195 ident = state->token[0].ident;
7196 if (!ident->sym_ident) {
7197 error(state, 0, "%s undeclared", ident->name);
7199 def = ident->sym_ident->def;
7202 case TOK_ENUM_CONST:
7203 /* Here ident is an enumeration constant */
7204 eat(state, TOK_ENUM_CONST);
7209 eat(state, TOK_LPAREN);
7211 eat(state, TOK_RPAREN);
7214 def = integer_constant(state);
7217 eat(state, TOK_LIT_FLOAT);
7218 error(state, 0, "Floating point constants not supported");
7223 def = character_constant(state);
7225 case TOK_LIT_STRING:
7226 def = string_constant(state);
7230 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
7235 static struct triple *postfix_expr(struct compile_state *state)
7239 def = primary_expr(state);
7241 struct triple *left;
7245 switch((tok = peek(state))) {
7247 eat(state, TOK_LBRACKET);
7248 def = mk_subscript_expr(state, left, expr(state));
7249 eat(state, TOK_RBRACKET);
7252 def = call_expr(state, def);
7256 struct hash_entry *field;
7257 eat(state, TOK_DOT);
7258 eat(state, TOK_IDENT);
7259 field = state->token[0].ident;
7260 def = deref_field(state, def, field);
7265 struct hash_entry *field;
7266 eat(state, TOK_ARROW);
7267 eat(state, TOK_IDENT);
7268 field = state->token[0].ident;
7269 def = mk_deref_expr(state, read_expr(state, def));
7270 def = deref_field(state, def, field);
7274 eat(state, TOK_PLUSPLUS);
7275 def = mk_post_inc_expr(state, left);
7277 case TOK_MINUSMINUS:
7278 eat(state, TOK_MINUSMINUS);
7279 def = mk_post_dec_expr(state, left);
7289 static struct triple *cast_expr(struct compile_state *state);
7291 static struct triple *unary_expr(struct compile_state *state)
7293 struct triple *def, *right;
7295 switch((tok = peek(state))) {
7297 eat(state, TOK_PLUSPLUS);
7298 def = mk_pre_inc_expr(state, unary_expr(state));
7300 case TOK_MINUSMINUS:
7301 eat(state, TOK_MINUSMINUS);
7302 def = mk_pre_dec_expr(state, unary_expr(state));
7305 eat(state, TOK_AND);
7306 def = mk_addr_expr(state, cast_expr(state), 0);
7309 eat(state, TOK_STAR);
7310 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
7313 eat(state, TOK_PLUS);
7314 right = read_expr(state, cast_expr(state));
7315 arithmetic(state, right);
7316 def = integral_promotion(state, right);
7319 eat(state, TOK_MINUS);
7320 right = read_expr(state, cast_expr(state));
7321 arithmetic(state, right);
7322 def = integral_promotion(state, right);
7323 def = triple(state, OP_NEG, def->type, def, 0);
7326 eat(state, TOK_TILDE);
7327 right = read_expr(state, cast_expr(state));
7328 integral(state, right);
7329 def = integral_promotion(state, right);
7330 def = triple(state, OP_INVERT, def->type, def, 0);
7333 eat(state, TOK_BANG);
7334 right = read_expr(state, cast_expr(state));
7336 def = lfalse_expr(state, right);
7342 eat(state, TOK_SIZEOF);
7344 tok2 = peek2(state);
7345 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7346 eat(state, TOK_LPAREN);
7347 type = type_name(state);
7348 eat(state, TOK_RPAREN);
7351 struct triple *expr;
7352 expr = unary_expr(state);
7354 release_expr(state, expr);
7356 def = int_const(state, &ulong_type, size_of(state, type));
7363 eat(state, TOK_ALIGNOF);
7365 tok2 = peek2(state);
7366 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7367 eat(state, TOK_LPAREN);
7368 type = type_name(state);
7369 eat(state, TOK_RPAREN);
7372 struct triple *expr;
7373 expr = unary_expr(state);
7375 release_expr(state, expr);
7377 def = int_const(state, &ulong_type, align_of(state, type));
7381 def = postfix_expr(state);
7387 static struct triple *cast_expr(struct compile_state *state)
7392 tok2 = peek2(state);
7393 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7395 eat(state, TOK_LPAREN);
7396 type = type_name(state);
7397 eat(state, TOK_RPAREN);
7398 def = mk_cast_expr(state, type, cast_expr(state));
7401 def = unary_expr(state);
7406 static struct triple *mult_expr(struct compile_state *state)
7410 def = cast_expr(state);
7412 struct triple *left, *right;
7413 struct type *result_type;
7416 switch(tok = (peek(state))) {
7420 left = read_expr(state, def);
7421 arithmetic(state, left);
7425 right = read_expr(state, cast_expr(state));
7426 arithmetic(state, right);
7428 result_type = arithmetic_result(state, left, right);
7429 sign = is_signed(result_type);
7432 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
7433 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
7434 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
7436 def = triple(state, op, result_type, left, right);
7446 static struct triple *add_expr(struct compile_state *state)
7450 def = mult_expr(state);
7453 switch( peek(state)) {
7455 eat(state, TOK_PLUS);
7456 def = mk_add_expr(state, def, mult_expr(state));
7459 eat(state, TOK_MINUS);
7460 def = mk_sub_expr(state, def, mult_expr(state));
7470 static struct triple *shift_expr(struct compile_state *state)
7474 def = add_expr(state);
7476 struct triple *left, *right;
7479 switch((tok = peek(state))) {
7482 left = read_expr(state, def);
7483 integral(state, left);
7484 left = integral_promotion(state, left);
7488 right = read_expr(state, add_expr(state));
7489 integral(state, right);
7490 right = integral_promotion(state, right);
7492 op = (tok == TOK_SL)? OP_SL :
7493 is_signed(left->type)? OP_SSR: OP_USR;
7495 def = triple(state, op, left->type, left, right);
7505 static struct triple *relational_expr(struct compile_state *state)
7507 #warning "Extend relational exprs to work on more than arithmetic types"
7510 def = shift_expr(state);
7512 struct triple *left, *right;
7513 struct type *arg_type;
7516 switch((tok = peek(state))) {
7521 left = read_expr(state, def);
7522 arithmetic(state, left);
7526 right = read_expr(state, shift_expr(state));
7527 arithmetic(state, right);
7529 arg_type = arithmetic_result(state, left, right);
7530 sign = is_signed(arg_type);
7533 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
7534 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
7535 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
7536 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
7538 def = triple(state, op, &int_type, left, right);
7548 static struct triple *equality_expr(struct compile_state *state)
7550 #warning "Extend equality exprs to work on more than arithmetic types"
7553 def = relational_expr(state);
7555 struct triple *left, *right;
7558 switch((tok = peek(state))) {
7561 left = read_expr(state, def);
7562 arithmetic(state, left);
7564 right = read_expr(state, relational_expr(state));
7565 arithmetic(state, right);
7566 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
7567 def = triple(state, op, &int_type, left, right);
7577 static struct triple *and_expr(struct compile_state *state)
7580 def = equality_expr(state);
7581 while(peek(state) == TOK_AND) {
7582 struct triple *left, *right;
7583 struct type *result_type;
7584 left = read_expr(state, def);
7585 integral(state, left);
7586 eat(state, TOK_AND);
7587 right = read_expr(state, equality_expr(state));
7588 integral(state, right);
7589 result_type = arithmetic_result(state, left, right);
7590 def = triple(state, OP_AND, result_type, left, right);
7595 static struct triple *xor_expr(struct compile_state *state)
7598 def = and_expr(state);
7599 while(peek(state) == TOK_XOR) {
7600 struct triple *left, *right;
7601 struct type *result_type;
7602 left = read_expr(state, def);
7603 integral(state, left);
7604 eat(state, TOK_XOR);
7605 right = read_expr(state, and_expr(state));
7606 integral(state, right);
7607 result_type = arithmetic_result(state, left, right);
7608 def = triple(state, OP_XOR, result_type, left, right);
7613 static struct triple *or_expr(struct compile_state *state)
7616 def = xor_expr(state);
7617 while(peek(state) == TOK_OR) {
7618 struct triple *left, *right;
7619 struct type *result_type;
7620 left = read_expr(state, def);
7621 integral(state, left);
7623 right = read_expr(state, xor_expr(state));
7624 integral(state, right);
7625 result_type = arithmetic_result(state, left, right);
7626 def = triple(state, OP_OR, result_type, left, right);
7631 static struct triple *land_expr(struct compile_state *state)
7634 def = or_expr(state);
7635 while(peek(state) == TOK_LOGAND) {
7636 struct triple *left, *right;
7637 left = read_expr(state, def);
7639 eat(state, TOK_LOGAND);
7640 right = read_expr(state, or_expr(state));
7643 def = triple(state, OP_LAND, &int_type,
7644 ltrue_expr(state, left),
7645 ltrue_expr(state, right));
7650 static struct triple *lor_expr(struct compile_state *state)
7653 def = land_expr(state);
7654 while(peek(state) == TOK_LOGOR) {
7655 struct triple *left, *right;
7656 left = read_expr(state, def);
7658 eat(state, TOK_LOGOR);
7659 right = read_expr(state, land_expr(state));
7662 def = triple(state, OP_LOR, &int_type,
7663 ltrue_expr(state, left),
7664 ltrue_expr(state, right));
7669 static struct triple *conditional_expr(struct compile_state *state)
7672 def = lor_expr(state);
7673 if (peek(state) == TOK_QUEST) {
7674 struct triple *test, *left, *right;
7676 test = ltrue_expr(state, read_expr(state, def));
7677 eat(state, TOK_QUEST);
7678 left = read_expr(state, expr(state));
7679 eat(state, TOK_COLON);
7680 right = read_expr(state, conditional_expr(state));
7682 def = cond_expr(state, test, left, right);
7687 static struct triple *eval_const_expr(
7688 struct compile_state *state, struct triple *expr)
7691 if (is_const(expr)) {
7695 /* If we don't start out as a constant simplify into one */
7696 struct triple *head, *ptr;
7697 head = label(state); /* dummy initial triple */
7698 flatten(state, head, expr);
7699 for(ptr = head->next; ptr != head; ptr = ptr->next) {
7700 simplify(state, ptr);
7702 /* Remove the constant value the tail of the list */
7704 def->prev->next = def->next;
7705 def->next->prev = def->prev;
7706 def->next = def->prev = def;
7707 if (!is_const(def)) {
7708 error(state, 0, "Not a constant expression");
7710 /* Free the intermediate expressions */
7711 while(head->next != head) {
7712 release_triple(state, head->next);
7714 free_triple(state, head);
7719 static struct triple *constant_expr(struct compile_state *state)
7721 return eval_const_expr(state, conditional_expr(state));
7724 static struct triple *assignment_expr(struct compile_state *state)
7726 struct triple *def, *left, *right;
7728 /* The C grammer in K&R shows assignment expressions
7729 * only taking unary expressions as input on their
7730 * left hand side. But specifies the precedence of
7731 * assignemnt as the lowest operator except for comma.
7733 * Allowing conditional expressions on the left hand side
7734 * of an assignement results in a grammar that accepts
7735 * a larger set of statements than standard C. As long
7736 * as the subset of the grammar that is standard C behaves
7737 * correctly this should cause no problems.
7739 * For the extra token strings accepted by the grammar
7740 * none of them should produce a valid lvalue, so they
7741 * should not produce functioning programs.
7743 * GCC has this bug as well, so surprises should be minimal.
7745 def = conditional_expr(state);
7747 switch((tok = peek(state))) {
7749 lvalue(state, left);
7751 def = write_expr(state, left,
7752 read_expr(state, assignment_expr(state)));
7757 lvalue(state, left);
7758 arithmetic(state, left);
7760 right = read_expr(state, assignment_expr(state));
7761 arithmetic(state, right);
7763 sign = is_signed(left->type);
7766 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
7767 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
7768 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
7770 def = write_expr(state, left,
7771 triple(state, op, left->type,
7772 read_expr(state, left), right));
7775 lvalue(state, left);
7776 eat(state, TOK_PLUSEQ);
7777 def = write_expr(state, left,
7778 mk_add_expr(state, left, assignment_expr(state)));
7781 lvalue(state, left);
7782 eat(state, TOK_MINUSEQ);
7783 def = write_expr(state, left,
7784 mk_sub_expr(state, left, assignment_expr(state)));
7791 lvalue(state, left);
7792 integral(state, left);
7794 right = read_expr(state, assignment_expr(state));
7795 integral(state, right);
7796 right = integral_promotion(state, right);
7797 sign = is_signed(left->type);
7800 case TOK_SLEQ: op = OP_SL; break;
7801 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
7802 case TOK_ANDEQ: op = OP_AND; break;
7803 case TOK_XOREQ: op = OP_XOR; break;
7804 case TOK_OREQ: op = OP_OR; break;
7806 def = write_expr(state, left,
7807 triple(state, op, left->type,
7808 read_expr(state, left), right));
7814 static struct triple *expr(struct compile_state *state)
7817 def = assignment_expr(state);
7818 while(peek(state) == TOK_COMMA) {
7819 struct triple *left, *right;
7821 eat(state, TOK_COMMA);
7822 right = assignment_expr(state);
7823 def = triple(state, OP_COMMA, right->type, left, right);
7828 static void expr_statement(struct compile_state *state, struct triple *first)
7830 if (peek(state) != TOK_SEMI) {
7831 flatten(state, first, expr(state));
7833 eat(state, TOK_SEMI);
7836 static void if_statement(struct compile_state *state, struct triple *first)
7838 struct triple *test, *jmp1, *jmp2, *middle, *end;
7840 jmp1 = jmp2 = middle = 0;
7842 eat(state, TOK_LPAREN);
7845 /* Cleanup and invert the test */
7846 test = lfalse_expr(state, read_expr(state, test));
7847 eat(state, TOK_RPAREN);
7848 /* Generate the needed pieces */
7849 middle = label(state);
7850 jmp1 = branch(state, middle, test);
7851 /* Thread the pieces together */
7852 flatten(state, first, test);
7853 flatten(state, first, jmp1);
7854 flatten(state, first, label(state));
7855 statement(state, first);
7856 if (peek(state) == TOK_ELSE) {
7857 eat(state, TOK_ELSE);
7858 /* Generate the rest of the pieces */
7860 jmp2 = branch(state, end, 0);
7861 /* Thread them together */
7862 flatten(state, first, jmp2);
7863 flatten(state, first, middle);
7864 statement(state, first);
7865 flatten(state, first, end);
7868 flatten(state, first, middle);
7872 static void for_statement(struct compile_state *state, struct triple *first)
7874 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
7875 struct triple *label1, *label2, *label3;
7876 struct hash_entry *ident;
7878 eat(state, TOK_FOR);
7879 eat(state, TOK_LPAREN);
7880 head = test = tail = jmp1 = jmp2 = 0;
7881 if (peek(state) != TOK_SEMI) {
7884 eat(state, TOK_SEMI);
7885 if (peek(state) != TOK_SEMI) {
7888 test = ltrue_expr(state, read_expr(state, test));
7890 eat(state, TOK_SEMI);
7891 if (peek(state) != TOK_RPAREN) {
7894 eat(state, TOK_RPAREN);
7895 /* Generate the needed pieces */
7896 label1 = label(state);
7897 label2 = label(state);
7898 label3 = label(state);
7900 jmp1 = branch(state, label3, 0);
7901 jmp2 = branch(state, label1, test);
7904 jmp2 = branch(state, label1, 0);
7907 /* Remember where break and continue go */
7909 ident = state->i_break;
7910 symbol(state, ident, &ident->sym_ident, end, end->type);
7911 ident = state->i_continue;
7912 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7913 /* Now include the body */
7914 flatten(state, first, head);
7915 flatten(state, first, jmp1);
7916 flatten(state, first, label1);
7917 statement(state, first);
7918 flatten(state, first, label2);
7919 flatten(state, first, tail);
7920 flatten(state, first, label3);
7921 flatten(state, first, test);
7922 flatten(state, first, jmp2);
7923 flatten(state, first, end);
7924 /* Cleanup the break/continue scope */
7928 static void while_statement(struct compile_state *state, struct triple *first)
7930 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
7931 struct hash_entry *ident;
7932 eat(state, TOK_WHILE);
7933 eat(state, TOK_LPAREN);
7936 test = ltrue_expr(state, read_expr(state, test));
7937 eat(state, TOK_RPAREN);
7938 /* Generate the needed pieces */
7939 label1 = label(state);
7940 label2 = label(state);
7941 jmp1 = branch(state, label2, 0);
7942 jmp2 = branch(state, label1, test);
7944 /* Remember where break and continue go */
7946 ident = state->i_break;
7947 symbol(state, ident, &ident->sym_ident, end, end->type);
7948 ident = state->i_continue;
7949 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7950 /* Thread them together */
7951 flatten(state, first, jmp1);
7952 flatten(state, first, label1);
7953 statement(state, first);
7954 flatten(state, first, label2);
7955 flatten(state, first, test);
7956 flatten(state, first, jmp2);
7957 flatten(state, first, end);
7958 /* Cleanup the break/continue scope */
7962 static void do_statement(struct compile_state *state, struct triple *first)
7964 struct triple *label1, *label2, *test, *end;
7965 struct hash_entry *ident;
7967 /* Generate the needed pieces */
7968 label1 = label(state);
7969 label2 = label(state);
7971 /* Remember where break and continue go */
7973 ident = state->i_break;
7974 symbol(state, ident, &ident->sym_ident, end, end->type);
7975 ident = state->i_continue;
7976 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7977 /* Now include the body */
7978 flatten(state, first, label1);
7979 statement(state, first);
7980 /* Cleanup the break/continue scope */
7982 /* Eat the rest of the loop */
7983 eat(state, TOK_WHILE);
7984 eat(state, TOK_LPAREN);
7985 test = read_expr(state, expr(state));
7987 eat(state, TOK_RPAREN);
7988 eat(state, TOK_SEMI);
7989 /* Thread the pieces together */
7990 test = ltrue_expr(state, test);
7991 flatten(state, first, label2);
7992 flatten(state, first, test);
7993 flatten(state, first, branch(state, label1, test));
7994 flatten(state, first, end);
7998 static void return_statement(struct compile_state *state, struct triple *first)
8000 struct triple *jmp, *mv, *dest, *var, *val;
8002 eat(state, TOK_RETURN);
8004 #warning "FIXME implement a more general excess branch elimination"
8006 /* If we have a return value do some more work */
8007 if (peek(state) != TOK_SEMI) {
8008 val = read_expr(state, expr(state));
8010 eat(state, TOK_SEMI);
8012 /* See if this last statement in a function */
8013 last = ((peek(state) == TOK_RBRACE) &&
8014 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
8016 /* Find the return variable */
8017 var = MISC(state->main_function, 0);
8018 /* Find the return destination */
8019 dest = RHS(state->main_function, 0)->prev;
8021 /* If needed generate a jump instruction */
8023 jmp = branch(state, dest, 0);
8025 /* If needed generate an assignment instruction */
8027 mv = write_expr(state, var, val);
8029 /* Now put the code together */
8031 flatten(state, first, mv);
8032 flatten(state, first, jmp);
8035 flatten(state, first, jmp);
8039 static void break_statement(struct compile_state *state, struct triple *first)
8041 struct triple *dest;
8042 eat(state, TOK_BREAK);
8043 eat(state, TOK_SEMI);
8044 if (!state->i_break->sym_ident) {
8045 error(state, 0, "break statement not within loop or switch");
8047 dest = state->i_break->sym_ident->def;
8048 flatten(state, first, branch(state, dest, 0));
8051 static void continue_statement(struct compile_state *state, struct triple *first)
8053 struct triple *dest;
8054 eat(state, TOK_CONTINUE);
8055 eat(state, TOK_SEMI);
8056 if (!state->i_continue->sym_ident) {
8057 error(state, 0, "continue statement outside of a loop");
8059 dest = state->i_continue->sym_ident->def;
8060 flatten(state, first, branch(state, dest, 0));
8063 static void goto_statement(struct compile_state *state, struct triple *first)
8065 struct hash_entry *ident;
8066 eat(state, TOK_GOTO);
8067 eat(state, TOK_IDENT);
8068 ident = state->token[0].ident;
8069 if (!ident->sym_label) {
8070 /* If this is a forward branch allocate the label now,
8071 * it will be flattend in the appropriate location later.
8075 label_symbol(state, ident, ins);
8077 eat(state, TOK_SEMI);
8079 flatten(state, first, branch(state, ident->sym_label->def, 0));
8082 static void labeled_statement(struct compile_state *state, struct triple *first)
8085 struct hash_entry *ident;
8086 eat(state, TOK_IDENT);
8088 ident = state->token[0].ident;
8089 if (ident->sym_label && ident->sym_label->def) {
8090 ins = ident->sym_label->def;
8091 put_occurance(ins->occurance);
8092 ins->occurance = new_occurance(state);
8096 label_symbol(state, ident, ins);
8098 if (ins->id & TRIPLE_FLAG_FLATTENED) {
8099 error(state, 0, "label %s already defined", ident->name);
8101 flatten(state, first, ins);
8103 eat(state, TOK_COLON);
8104 statement(state, first);
8107 static void switch_statement(struct compile_state *state, struct triple *first)
8110 eat(state, TOK_SWITCH);
8111 eat(state, TOK_LPAREN);
8113 eat(state, TOK_RPAREN);
8114 statement(state, first);
8115 error(state, 0, "switch statements are not implemented");
8119 static void case_statement(struct compile_state *state, struct triple *first)
8122 eat(state, TOK_CASE);
8123 constant_expr(state);
8124 eat(state, TOK_COLON);
8125 statement(state, first);
8126 error(state, 0, "case statements are not implemented");
8130 static void default_statement(struct compile_state *state, struct triple *first)
8133 eat(state, TOK_DEFAULT);
8134 eat(state, TOK_COLON);
8135 statement(state, first);
8136 error(state, 0, "default statements are not implemented");
8140 static void asm_statement(struct compile_state *state, struct triple *first)
8142 struct asm_info *info;
8144 struct triple *constraint;
8145 struct triple *expr;
8146 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
8147 struct triple *def, *asm_str;
8148 int out, in, clobbers, more, colons, i;
8150 eat(state, TOK_ASM);
8151 /* For now ignore the qualifiers */
8152 switch(peek(state)) {
8154 eat(state, TOK_CONST);
8157 eat(state, TOK_VOLATILE);
8160 eat(state, TOK_LPAREN);
8161 asm_str = string_constant(state);
8164 out = in = clobbers = 0;
8166 if ((colons == 0) && (peek(state) == TOK_COLON)) {
8167 eat(state, TOK_COLON);
8169 more = (peek(state) == TOK_LIT_STRING);
8172 struct triple *constraint;
8175 if (out > MAX_LHS) {
8176 error(state, 0, "Maximum output count exceeded.");
8178 constraint = string_constant(state);
8179 str = constraint->u.blob;
8180 if (str[0] != '=') {
8181 error(state, 0, "Output constraint does not start with =");
8183 constraint->u.blob = str + 1;
8184 eat(state, TOK_LPAREN);
8185 var = conditional_expr(state);
8186 eat(state, TOK_RPAREN);
8189 out_param[out].constraint = constraint;
8190 out_param[out].expr = var;
8191 if (peek(state) == TOK_COMMA) {
8192 eat(state, TOK_COMMA);
8199 if ((colons == 1) && (peek(state) == TOK_COLON)) {
8200 eat(state, TOK_COLON);
8202 more = (peek(state) == TOK_LIT_STRING);
8205 struct triple *constraint;
8209 error(state, 0, "Maximum input count exceeded.");
8211 constraint = string_constant(state);
8212 str = constraint->u.blob;
8213 if (digitp(str[0] && str[1] == '\0')) {
8215 val = digval(str[0]);
8216 if ((val < 0) || (val >= out)) {
8217 error(state, 0, "Invalid input constraint %d", val);
8220 eat(state, TOK_LPAREN);
8221 val = conditional_expr(state);
8222 eat(state, TOK_RPAREN);
8224 in_param[in].constraint = constraint;
8225 in_param[in].expr = val;
8226 if (peek(state) == TOK_COMMA) {
8227 eat(state, TOK_COMMA);
8235 if ((colons == 2) && (peek(state) == TOK_COLON)) {
8236 eat(state, TOK_COLON);
8238 more = (peek(state) == TOK_LIT_STRING);
8240 struct triple *clobber;
8242 if ((clobbers + out) > MAX_LHS) {
8243 error(state, 0, "Maximum clobber limit exceeded.");
8245 clobber = string_constant(state);
8247 clob_param[clobbers].constraint = clobber;
8248 if (peek(state) == TOK_COMMA) {
8249 eat(state, TOK_COMMA);
8255 eat(state, TOK_RPAREN);
8256 eat(state, TOK_SEMI);
8259 info = xcmalloc(sizeof(*info), "asm_info");
8260 info->str = asm_str->u.blob;
8261 free_triple(state, asm_str);
8263 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
8264 def->u.ainfo = info;
8266 /* Find the register constraints */
8267 for(i = 0; i < out; i++) {
8268 struct triple *constraint;
8269 constraint = out_param[i].constraint;
8270 info->tmpl.lhs[i] = arch_reg_constraint(state,
8271 out_param[i].expr->type, constraint->u.blob);
8272 free_triple(state, constraint);
8274 for(; i - out < clobbers; i++) {
8275 struct triple *constraint;
8276 constraint = clob_param[i - out].constraint;
8277 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
8278 free_triple(state, constraint);
8280 for(i = 0; i < in; i++) {
8281 struct triple *constraint;
8283 constraint = in_param[i].constraint;
8284 str = constraint->u.blob;
8285 if (digitp(str[0]) && str[1] == '\0') {
8286 struct reg_info cinfo;
8288 val = digval(str[0]);
8289 cinfo.reg = info->tmpl.lhs[val].reg;
8290 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
8291 cinfo.regcm &= info->tmpl.lhs[val].regcm;
8292 if (cinfo.reg == REG_UNSET) {
8293 cinfo.reg = REG_VIRT0 + val;
8295 if (cinfo.regcm == 0) {
8296 error(state, 0, "No registers for %d", val);
8298 info->tmpl.lhs[val] = cinfo;
8299 info->tmpl.rhs[i] = cinfo;
8302 info->tmpl.rhs[i] = arch_reg_constraint(state,
8303 in_param[i].expr->type, str);
8305 free_triple(state, constraint);
8308 /* Now build the helper expressions */
8309 for(i = 0; i < in; i++) {
8310 RHS(def, i) = read_expr(state,in_param[i].expr);
8312 flatten(state, first, def);
8313 for(i = 0; i < (out + clobbers); i++) {
8315 struct triple *piece;
8316 type = (i < out)? out_param[i].expr->type : &void_type;
8317 piece = triple(state, OP_PIECE, type, def, 0);
8319 LHS(def, i) = piece;
8320 flatten(state, first, piece);
8322 /* And write the helpers to their destinations */
8323 for(i = 0; i < out; i++) {
8324 struct triple *piece;
8325 piece = LHS(def, i);
8326 flatten(state, first,
8327 write_expr(state, out_param[i].expr, piece));
8332 static int isdecl(int tok)
8355 case TOK_TYPE_NAME: /* typedef name */
8362 static void compound_statement(struct compile_state *state, struct triple *first)
8364 eat(state, TOK_LBRACE);
8367 /* statement-list opt */
8368 while (peek(state) != TOK_RBRACE) {
8369 statement(state, first);
8372 eat(state, TOK_RBRACE);
8375 static void statement(struct compile_state *state, struct triple *first)
8379 if (tok == TOK_LBRACE) {
8380 compound_statement(state, first);
8382 else if (tok == TOK_IF) {
8383 if_statement(state, first);
8385 else if (tok == TOK_FOR) {
8386 for_statement(state, first);
8388 else if (tok == TOK_WHILE) {
8389 while_statement(state, first);
8391 else if (tok == TOK_DO) {
8392 do_statement(state, first);
8394 else if (tok == TOK_RETURN) {
8395 return_statement(state, first);
8397 else if (tok == TOK_BREAK) {
8398 break_statement(state, first);
8400 else if (tok == TOK_CONTINUE) {
8401 continue_statement(state, first);
8403 else if (tok == TOK_GOTO) {
8404 goto_statement(state, first);
8406 else if (tok == TOK_SWITCH) {
8407 switch_statement(state, first);
8409 else if (tok == TOK_ASM) {
8410 asm_statement(state, first);
8412 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
8413 labeled_statement(state, first);
8415 else if (tok == TOK_CASE) {
8416 case_statement(state, first);
8418 else if (tok == TOK_DEFAULT) {
8419 default_statement(state, first);
8421 else if (isdecl(tok)) {
8422 /* This handles C99 intermixing of statements and decls */
8426 expr_statement(state, first);
8430 static struct type *param_decl(struct compile_state *state)
8433 struct hash_entry *ident;
8434 /* Cheat so the declarator will know we are not global */
8437 type = decl_specifiers(state);
8438 type = declarator(state, type, &ident, 0);
8439 type->field_ident = ident;
8444 static struct type *param_type_list(struct compile_state *state, struct type *type)
8446 struct type *ftype, **next;
8447 ftype = new_type(TYPE_FUNCTION, type, param_decl(state));
8448 next = &ftype->right;
8449 while(peek(state) == TOK_COMMA) {
8450 eat(state, TOK_COMMA);
8451 if (peek(state) == TOK_DOTS) {
8452 eat(state, TOK_DOTS);
8453 error(state, 0, "variadic functions not supported");
8456 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
8457 next = &((*next)->right);
8464 static struct type *type_name(struct compile_state *state)
8467 type = specifier_qualifier_list(state);
8468 /* abstract-declarator (may consume no tokens) */
8469 type = declarator(state, type, 0, 0);
8473 static struct type *direct_declarator(
8474 struct compile_state *state, struct type *type,
8475 struct hash_entry **ident, int need_ident)
8480 arrays_complete(state, type);
8481 switch(peek(state)) {
8483 eat(state, TOK_IDENT);
8485 error(state, 0, "Unexpected identifier found");
8487 /* The name of what we are declaring */
8488 *ident = state->token[0].ident;
8491 eat(state, TOK_LPAREN);
8492 outer = declarator(state, type, ident, need_ident);
8493 eat(state, TOK_RPAREN);
8497 error(state, 0, "Identifier expected");
8503 arrays_complete(state, type);
8504 switch(peek(state)) {
8506 eat(state, TOK_LPAREN);
8507 type = param_type_list(state, type);
8508 eat(state, TOK_RPAREN);
8512 unsigned int qualifiers;
8513 struct triple *value;
8515 eat(state, TOK_LBRACKET);
8516 if (peek(state) != TOK_RBRACKET) {
8517 value = constant_expr(state);
8518 integral(state, value);
8520 eat(state, TOK_RBRACKET);
8522 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
8523 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
8525 type->elements = value->u.cval;
8526 free_triple(state, value);
8528 type->elements = ELEMENT_COUNT_UNSPECIFIED;
8540 arrays_complete(state, type);
8542 for(inner = outer; inner->left; inner = inner->left)
8550 static struct type *declarator(
8551 struct compile_state *state, struct type *type,
8552 struct hash_entry **ident, int need_ident)
8554 while(peek(state) == TOK_STAR) {
8555 eat(state, TOK_STAR);
8556 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
8558 type = direct_declarator(state, type, ident, need_ident);
8563 static struct type *typedef_name(
8564 struct compile_state *state, unsigned int specifiers)
8566 struct hash_entry *ident;
8568 eat(state, TOK_TYPE_NAME);
8569 ident = state->token[0].ident;
8570 type = ident->sym_ident->type;
8571 specifiers |= type->type & QUAL_MASK;
8572 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
8573 (type->type & (STOR_MASK | QUAL_MASK))) {
8574 type = clone_type(specifiers, type);
8579 static struct type *enum_specifier(
8580 struct compile_state *state, unsigned int specifiers)
8586 eat(state, TOK_ENUM);
8588 if (tok == TOK_IDENT) {
8589 eat(state, TOK_IDENT);
8591 if ((tok != TOK_IDENT) || (peek(state) == TOK_LBRACE)) {
8592 eat(state, TOK_LBRACE);
8594 eat(state, TOK_IDENT);
8595 if (peek(state) == TOK_EQ) {
8597 constant_expr(state);
8599 if (peek(state) == TOK_COMMA) {
8600 eat(state, TOK_COMMA);
8602 } while(peek(state) != TOK_RBRACE);
8603 eat(state, TOK_RBRACE);
8609 static struct type *struct_declarator(
8610 struct compile_state *state, struct type *type, struct hash_entry **ident)
8614 if (tok != TOK_COLON) {
8615 type = declarator(state, type, ident, 1);
8617 if ((tok == TOK_COLON) || (peek(state) == TOK_COLON)) {
8618 struct triple *value;
8619 eat(state, TOK_COLON);
8620 value = constant_expr(state);
8621 #warning "FIXME implement bitfields to reduce register usage"
8622 error(state, 0, "bitfields not yet implemented");
8627 static struct type *struct_or_union_specifier(
8628 struct compile_state *state, unsigned int spec)
8630 struct type *struct_type;
8631 struct hash_entry *ident;
8632 unsigned int type_join;
8636 switch(peek(state)) {
8638 eat(state, TOK_STRUCT);
8639 type_join = TYPE_PRODUCT;
8642 eat(state, TOK_UNION);
8643 type_join = TYPE_OVERLAP;
8644 error(state, 0, "unions not yet supported\n");
8647 eat(state, TOK_STRUCT);
8648 type_join = TYPE_PRODUCT;
8652 if ((tok == TOK_IDENT) || (tok == TOK_TYPE_NAME)) {
8654 ident = state->token[0].ident;
8656 if (!ident || (peek(state) == TOK_LBRACE)) {
8660 eat(state, TOK_LBRACE);
8661 next = &struct_type;
8663 struct type *base_type;
8665 base_type = specifier_qualifier_list(state);
8668 struct hash_entry *fident;
8670 type = struct_declarator(state, base_type, &fident);
8672 if (peek(state) == TOK_COMMA) {
8674 eat(state, TOK_COMMA);
8676 type = clone_type(0, type);
8677 type->field_ident = fident;
8679 *next = new_type(type_join, *next, type);
8680 next = &((*next)->right);
8685 eat(state, TOK_SEMI);
8686 } while(peek(state) != TOK_RBRACE);
8687 eat(state, TOK_RBRACE);
8688 struct_type = new_type(TYPE_STRUCT | spec, struct_type, 0);
8689 struct_type->type_ident = ident;
8690 struct_type->elements = elements;
8692 symbol(state, ident, &ident->sym_struct, 0, struct_type);
8695 if (ident && ident->sym_struct) {
8696 struct_type = clone_type(spec, ident->sym_struct->type);
8698 else if (ident && !ident->sym_struct) {
8699 error(state, 0, "struct %s undeclared", ident->name);
8704 static unsigned int storage_class_specifier_opt(struct compile_state *state)
8706 unsigned int specifiers;
8707 switch(peek(state)) {
8709 eat(state, TOK_AUTO);
8710 specifiers = STOR_AUTO;
8713 eat(state, TOK_REGISTER);
8714 specifiers = STOR_REGISTER;
8717 eat(state, TOK_STATIC);
8718 specifiers = STOR_STATIC;
8721 eat(state, TOK_EXTERN);
8722 specifiers = STOR_EXTERN;
8725 eat(state, TOK_TYPEDEF);
8726 specifiers = STOR_TYPEDEF;
8729 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
8730 specifiers = STOR_STATIC;
8733 specifiers = STOR_AUTO;
8739 static unsigned int function_specifier_opt(struct compile_state *state)
8741 /* Ignore the inline keyword */
8742 unsigned int specifiers;
8744 switch(peek(state)) {
8746 eat(state, TOK_INLINE);
8747 specifiers = STOR_INLINE;
8752 static unsigned int type_qualifiers(struct compile_state *state)
8754 unsigned int specifiers;
8757 specifiers = QUAL_NONE;
8759 switch(peek(state)) {
8761 eat(state, TOK_CONST);
8762 specifiers = QUAL_CONST;
8765 eat(state, TOK_VOLATILE);
8766 specifiers = QUAL_VOLATILE;
8769 eat(state, TOK_RESTRICT);
8770 specifiers = QUAL_RESTRICT;
8780 static struct type *type_specifier(
8781 struct compile_state *state, unsigned int spec)
8785 switch(peek(state)) {
8787 eat(state, TOK_VOID);
8788 type = new_type(TYPE_VOID | spec, 0, 0);
8791 eat(state, TOK_CHAR);
8792 type = new_type(TYPE_CHAR | spec, 0, 0);
8795 eat(state, TOK_SHORT);
8796 if (peek(state) == TOK_INT) {
8797 eat(state, TOK_INT);
8799 type = new_type(TYPE_SHORT | spec, 0, 0);
8802 eat(state, TOK_INT);
8803 type = new_type(TYPE_INT | spec, 0, 0);
8806 eat(state, TOK_LONG);
8807 switch(peek(state)) {
8809 eat(state, TOK_LONG);
8810 error(state, 0, "long long not supported");
8813 eat(state, TOK_DOUBLE);
8814 error(state, 0, "long double not supported");
8817 eat(state, TOK_INT);
8818 type = new_type(TYPE_LONG | spec, 0, 0);
8821 type = new_type(TYPE_LONG | spec, 0, 0);
8826 eat(state, TOK_FLOAT);
8827 error(state, 0, "type float not supported");
8830 eat(state, TOK_DOUBLE);
8831 error(state, 0, "type double not supported");
8834 eat(state, TOK_SIGNED);
8835 switch(peek(state)) {
8837 eat(state, TOK_LONG);
8838 switch(peek(state)) {
8840 eat(state, TOK_LONG);
8841 error(state, 0, "type long long not supported");
8844 eat(state, TOK_INT);
8845 type = new_type(TYPE_LONG | spec, 0, 0);
8848 type = new_type(TYPE_LONG | spec, 0, 0);
8853 eat(state, TOK_INT);
8854 type = new_type(TYPE_INT | spec, 0, 0);
8857 eat(state, TOK_SHORT);
8858 type = new_type(TYPE_SHORT | spec, 0, 0);
8861 eat(state, TOK_CHAR);
8862 type = new_type(TYPE_CHAR | spec, 0, 0);
8865 type = new_type(TYPE_INT | spec, 0, 0);
8870 eat(state, TOK_UNSIGNED);
8871 switch(peek(state)) {
8873 eat(state, TOK_LONG);
8874 switch(peek(state)) {
8876 eat(state, TOK_LONG);
8877 error(state, 0, "unsigned long long not supported");
8880 eat(state, TOK_INT);
8881 type = new_type(TYPE_ULONG | spec, 0, 0);
8884 type = new_type(TYPE_ULONG | spec, 0, 0);
8889 eat(state, TOK_INT);
8890 type = new_type(TYPE_UINT | spec, 0, 0);
8893 eat(state, TOK_SHORT);
8894 type = new_type(TYPE_USHORT | spec, 0, 0);
8897 eat(state, TOK_CHAR);
8898 type = new_type(TYPE_UCHAR | spec, 0, 0);
8901 type = new_type(TYPE_UINT | spec, 0, 0);
8905 /* struct or union specifier */
8908 type = struct_or_union_specifier(state, spec);
8910 /* enum-spefifier */
8912 type = enum_specifier(state, spec);
8916 type = typedef_name(state, spec);
8919 error(state, 0, "bad type specifier %s",
8920 tokens[peek(state)]);
8926 static int istype(int tok)
8952 static struct type *specifier_qualifier_list(struct compile_state *state)
8955 unsigned int specifiers = 0;
8957 /* type qualifiers */
8958 specifiers |= type_qualifiers(state);
8960 /* type specifier */
8961 type = type_specifier(state, specifiers);
8966 static int isdecl_specifier(int tok)
8969 /* storage class specifier */
8975 /* type qualifier */
8979 /* type specifiers */
8989 /* struct or union specifier */
8992 /* enum-spefifier */
8996 /* function specifiers */
9004 static struct type *decl_specifiers(struct compile_state *state)
9007 unsigned int specifiers;
9008 /* I am overly restrictive in the arragement of specifiers supported.
9009 * C is overly flexible in this department it makes interpreting
9010 * the parse tree difficult.
9014 /* storage class specifier */
9015 specifiers |= storage_class_specifier_opt(state);
9017 /* function-specifier */
9018 specifiers |= function_specifier_opt(state);
9020 /* type qualifier */
9021 specifiers |= type_qualifiers(state);
9023 /* type specifier */
9024 type = type_specifier(state, specifiers);
9033 static struct field_info designator(struct compile_state *state, struct type *type)
9036 struct field_info info;
9040 switch(peek(state)) {
9043 struct triple *value;
9044 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
9045 error(state, 0, "Array designator not in array initializer");
9047 eat(state, TOK_LBRACKET);
9048 value = constant_expr(state);
9049 eat(state, TOK_RBRACKET);
9051 info.type = type->left;
9052 info.offset = value->u.cval * size_of(state, info.type);
9057 struct hash_entry *field;
9058 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
9059 error(state, 0, "Struct designator not in struct initializer");
9061 eat(state, TOK_DOT);
9062 eat(state, TOK_IDENT);
9063 field = state->token[0].ident;
9064 info.offset = field_offset(state, type, field);
9065 info.type = field_type(state, type, field);
9069 error(state, 0, "Invalid designator");
9072 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
9077 static struct triple *initializer(
9078 struct compile_state *state, struct type *type)
9080 struct triple *result;
9081 #warning "FIXME handle string pointer initializers "
9082 #warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
9083 if (peek(state) != TOK_LBRACE) {
9084 result = assignment_expr(state);
9085 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
9086 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
9087 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
9088 (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
9089 (equiv_types(type->left, result->type->left))) {
9090 type->elements = result->type->elements;
9092 if (!is_init_compatible(state, type, result->type)) {
9093 error(state, 0, "Incompatible types in initializer");
9095 if (!equiv_types(type, result->type)) {
9096 result = mk_cast_expr(state, type, result);
9102 struct field_info info;
9104 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
9105 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
9106 internal_error(state, 0, "unknown initializer type");
9109 info.type = type->left;
9110 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
9111 info.type = next_field(state, type, 0);
9113 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
9116 max_offset = size_of(state, type);
9118 buf = xcmalloc(max_offset, "initializer");
9119 eat(state, TOK_LBRACE);
9121 struct triple *value;
9122 struct type *value_type;
9128 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
9129 info = designator(state, type);
9131 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
9132 (info.offset >= max_offset)) {
9133 error(state, 0, "element beyond bounds");
9135 value_type = info.type;
9136 value = eval_const_expr(state, initializer(state, value_type));
9137 value_size = size_of(state, value_type);
9138 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
9139 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
9140 (max_offset <= info.offset)) {
9144 old_size = max_offset;
9145 max_offset = info.offset + value_size;
9146 buf = xmalloc(max_offset, "initializer");
9147 memcpy(buf, old_buf, old_size);
9150 dest = ((char *)buf) + info.offset;
9151 if (value->op == OP_BLOBCONST) {
9152 memcpy(dest, value->u.blob, value_size);
9154 else if ((value->op == OP_INTCONST) && (value_size == 1)) {
9155 *((uint8_t *)dest) = value->u.cval & 0xff;
9157 else if ((value->op == OP_INTCONST) && (value_size == 2)) {
9158 *((uint16_t *)dest) = value->u.cval & 0xffff;
9160 else if ((value->op == OP_INTCONST) && (value_size == 4)) {
9161 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
9164 internal_error(state, 0, "unhandled constant initializer");
9166 free_triple(state, value);
9167 if (peek(state) == TOK_COMMA) {
9168 eat(state, TOK_COMMA);
9171 info.offset += value_size;
9172 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
9173 info.type = next_field(state, type, info.type);
9174 info.offset = field_offset(state, type,
9175 info.type->field_ident);
9177 } while(comma && (peek(state) != TOK_RBRACE));
9178 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
9179 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
9180 type->elements = max_offset / size_of(state, type->left);
9182 eat(state, TOK_RBRACE);
9183 result = triple(state, OP_BLOBCONST, type, 0, 0);
9184 result->u.blob = buf;
9189 static void resolve_branches(struct compile_state *state)
9191 /* Make a second pass and finish anything outstanding
9192 * with respect to branches. The only outstanding item
9193 * is to see if there are goto to labels that have not
9194 * been defined and to error about them.
9197 for(i = 0; i < HASH_TABLE_SIZE; i++) {
9198 struct hash_entry *entry;
9199 for(entry = state->hash_table[i]; entry; entry = entry->next) {
9201 if (!entry->sym_label) {
9204 ins = entry->sym_label->def;
9205 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
9206 error(state, ins, "label `%s' used but not defined",
9213 static struct triple *function_definition(
9214 struct compile_state *state, struct type *type)
9216 struct triple *def, *tmp, *first, *end;
9217 struct hash_entry *ident;
9220 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
9221 error(state, 0, "Invalid function header");
9224 /* Verify the function type */
9225 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
9226 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
9227 (type->right->field_ident == 0)) {
9228 error(state, 0, "Invalid function parameters");
9230 param = type->right;
9232 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
9234 if (!param->left->field_ident) {
9235 error(state, 0, "No identifier for parameter %d\n", i);
9237 param = param->right;
9240 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
9241 error(state, 0, "No identifier for paramter %d\n", i);
9244 /* Get a list of statements for this function. */
9245 def = triple(state, OP_LIST, type, 0, 0);
9247 /* Start a new scope for the passed parameters */
9250 /* Put a label at the very start of a function */
9251 first = label(state);
9252 RHS(def, 0) = first;
9254 /* Put a label at the very end of a function */
9256 flatten(state, first, end);
9258 /* Walk through the parameters and create symbol table entries
9261 param = type->right;
9262 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
9263 ident = param->left->field_ident;
9264 tmp = variable(state, param->left);
9265 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
9266 flatten(state, end, tmp);
9267 param = param->right;
9269 if ((param->type & TYPE_MASK) != TYPE_VOID) {
9270 /* And don't forget the last parameter */
9271 ident = param->field_ident;
9272 tmp = variable(state, param);
9273 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
9274 flatten(state, end, tmp);
9276 /* Add a variable for the return value */
9278 if ((type->left->type & TYPE_MASK) != TYPE_VOID) {
9279 /* Remove all type qualifiers from the return type */
9280 tmp = variable(state, clone_type(0, type->left));
9281 flatten(state, end, tmp);
9282 /* Remember where the return value is */
9286 /* Remember which function I am compiling.
9287 * Also assume the last defined function is the main function.
9289 state->main_function = def;
9291 /* Now get the actual function definition */
9292 compound_statement(state, end);
9294 /* Finish anything unfinished with branches */
9295 resolve_branches(state);
9297 /* Remove the parameter scope */
9301 fprintf(stdout, "\n");
9302 loc(stdout, state, 0);
9303 fprintf(stdout, "\n__________ function_definition _________\n");
9304 print_triple(state, def);
9305 fprintf(stdout, "__________ function_definition _________ done\n\n");
9311 static struct triple *do_decl(struct compile_state *state,
9312 struct type *type, struct hash_entry *ident)
9316 /* Clean up the storage types used */
9317 switch (type->type & STOR_MASK) {
9320 /* These are the good types I am aiming for */
9323 type->type &= ~STOR_MASK;
9324 type->type |= STOR_AUTO;
9327 type->type &= ~STOR_MASK;
9328 type->type |= STOR_STATIC;
9332 error(state, 0, "typedef without name");
9334 symbol(state, ident, &ident->sym_ident, 0, type);
9335 ident->tok = TOK_TYPE_NAME;
9339 internal_error(state, 0, "Undefined storage class");
9341 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
9342 error(state, 0, "Function prototypes not supported");
9345 ((type->type & STOR_MASK) == STOR_STATIC) &&
9346 ((type->type & QUAL_CONST) == 0)) {
9347 error(state, 0, "non const static variables not supported");
9350 def = variable(state, type);
9351 symbol(state, ident, &ident->sym_ident, def, type);
9356 static void decl(struct compile_state *state, struct triple *first)
9358 struct type *base_type, *type;
9359 struct hash_entry *ident;
9362 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
9363 base_type = decl_specifiers(state);
9365 type = declarator(state, base_type, &ident, 0);
9366 if (global && ident && (peek(state) == TOK_LBRACE)) {
9368 state->function = ident->name;
9369 def = function_definition(state, type);
9370 symbol(state, ident, &ident->sym_ident, def, type);
9371 state->function = 0;
9375 flatten(state, first, do_decl(state, type, ident));
9376 /* type or variable definition */
9379 if (peek(state) == TOK_EQ) {
9381 error(state, 0, "cannot assign to a type");
9384 flatten(state, first,
9386 ident->sym_ident->def,
9387 initializer(state, type)));
9389 arrays_complete(state, type);
9390 if (peek(state) == TOK_COMMA) {
9391 eat(state, TOK_COMMA);
9393 type = declarator(state, base_type, &ident, 0);
9394 flatten(state, first, do_decl(state, type, ident));
9398 eat(state, TOK_SEMI);
9402 static void decls(struct compile_state *state)
9404 struct triple *list;
9406 list = label(state);
9409 if (tok == TOK_EOF) {
9412 if (tok == TOK_SPACE) {
9413 eat(state, TOK_SPACE);
9416 if (list->next != list) {
9417 error(state, 0, "global variables not supported");
9423 * Data structurs for optimation.
9426 static void do_use_block(
9427 struct block *used, struct block_set **head, struct block *user,
9430 struct block_set **ptr, *new;
9437 if ((*ptr)->member == user) {
9440 ptr = &(*ptr)->next;
9442 new = xcmalloc(sizeof(*new), "block_set");
9453 static void do_unuse_block(
9454 struct block *used, struct block_set **head, struct block *unuser)
9456 struct block_set *use, **ptr;
9460 if (use->member == unuser) {
9462 memset(use, -1, sizeof(*use));
9471 static void use_block(struct block *used, struct block *user)
9473 /* Append new to the head of the list, print_block
9476 do_use_block(used, &used->use, user, 1);
9479 static void unuse_block(struct block *used, struct block *unuser)
9481 do_unuse_block(used, &used->use, unuser);
9485 static void idom_block(struct block *idom, struct block *user)
9487 do_use_block(idom, &idom->idominates, user, 0);
9490 static void unidom_block(struct block *idom, struct block *unuser)
9492 do_unuse_block(idom, &idom->idominates, unuser);
9495 static void domf_block(struct block *block, struct block *domf)
9497 do_use_block(block, &block->domfrontier, domf, 0);
9500 static void undomf_block(struct block *block, struct block *undomf)
9502 do_unuse_block(block, &block->domfrontier, undomf);
9505 static void ipdom_block(struct block *ipdom, struct block *user)
9507 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
9510 static void unipdom_block(struct block *ipdom, struct block *unuser)
9512 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
9515 static void ipdomf_block(struct block *block, struct block *ipdomf)
9517 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
9520 static void unipdomf_block(struct block *block, struct block *unipdomf)
9522 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
9527 static int do_walk_triple(struct compile_state *state,
9528 struct triple *ptr, int depth,
9529 int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
9532 result = cb(state, ptr, depth);
9533 if ((result == 0) && (ptr->op == OP_LIST)) {
9534 struct triple *list;
9538 result = do_walk_triple(state, ptr, depth + 1, cb);
9539 if (ptr->next->prev != ptr) {
9540 internal_error(state, ptr->next, "bad prev");
9544 } while((result == 0) && (ptr != RHS(list, 0)));
9549 static int walk_triple(
9550 struct compile_state *state,
9552 int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
9554 return do_walk_triple(state, ptr, 0, cb);
9557 static void do_print_prefix(int depth)
9560 for(i = 0; i < depth; i++) {
9565 #define PRINT_LIST 1
9566 static int do_print_triple(struct compile_state *state, struct triple *ins, int depth)
9570 if (op == OP_LIST) {
9575 if ((op == OP_LABEL) && (ins->use)) {
9576 printf("\n%p:\n", ins);
9578 do_print_prefix(depth);
9579 display_triple(stdout, ins);
9581 if ((ins->op == OP_BRANCH) && ins->use) {
9582 internal_error(state, ins, "branch used?");
9584 if (triple_is_branch(state, ins)) {
9590 static void print_triple(struct compile_state *state, struct triple *ins)
9592 walk_triple(state, ins, do_print_triple);
9595 static void print_triples(struct compile_state *state)
9597 print_triple(state, state->main_function);
9601 struct block *block;
9603 static void find_cf_blocks(struct cf_block *cf, struct block *block)
9605 if (!block || (cf[block->vertex].block == block)) {
9608 cf[block->vertex].block = block;
9609 find_cf_blocks(cf, block->left);
9610 find_cf_blocks(cf, block->right);
9613 static void print_control_flow(struct compile_state *state)
9615 struct cf_block *cf;
9617 printf("\ncontrol flow\n");
9618 cf = xcmalloc(sizeof(*cf) * (state->last_vertex + 1), "cf_block");
9619 find_cf_blocks(cf, state->first_block);
9621 for(i = 1; i <= state->last_vertex; i++) {
9622 struct block *block;
9623 block = cf[i].block;
9626 printf("(%p) %d:", block, block->vertex);
9628 printf(" %d", block->left->vertex);
9630 if (block->right && (block->right != block->left)) {
9631 printf(" %d", block->right->vertex);
9640 static struct block *basic_block(struct compile_state *state,
9641 struct triple *first)
9643 struct block *block;
9644 struct triple *ptr, *final;
9646 if (first->op != OP_LABEL) {
9647 internal_error(state, 0, "block does not start with a label");
9649 /* See if this basic block has already been setup */
9650 if (first->u.block != 0) {
9651 return first->u.block;
9653 /* Lookup the final instruction.
9654 * It is important that the final instruction has it's own
9657 final = RHS(state->main_function, 0)->prev;
9658 /* Allocate another basic block structure */
9659 state->last_vertex += 1;
9660 block = xcmalloc(sizeof(*block), "block");
9661 block->first = block->last = first;
9662 block->vertex = state->last_vertex;
9665 if ((ptr != first) && (ptr->op == OP_LABEL) &&
9666 ((ptr->use) || ptr == final)) {
9670 /* If ptr->u is not used remember where the baic block is */
9671 if (triple_stores_block(state, ptr)) {
9672 ptr->u.block = block;
9674 if (ptr->op == OP_BRANCH) {
9678 } while (ptr != RHS(state->main_function, 0));
9679 if (ptr == RHS(state->main_function, 0))
9682 if (op == OP_LABEL) {
9683 block->left = basic_block(state, ptr);
9685 use_block(block->left, block);
9687 else if (op == OP_BRANCH) {
9689 /* Trace the branch target */
9690 block->right = basic_block(state, TARG(ptr, 0));
9691 use_block(block->right, block);
9692 /* If there is a test trace the branch as well */
9693 if (TRIPLE_RHS(ptr->sizes)) {
9694 block->left = basic_block(state, ptr->next);
9695 use_block(block->left, block);
9699 internal_error(state, 0, "Bad basic block split");
9705 static void walk_blocks(struct compile_state *state,
9706 void (*cb)(struct compile_state *state, struct block *block, void *arg),
9709 struct triple *ptr, *first;
9710 struct block *last_block;
9712 first = RHS(state->main_function, 0);
9715 struct block *block;
9716 if (triple_stores_block(state, ptr)) {
9717 block = ptr->u.block;
9718 if (block && (block != last_block)) {
9719 cb(state, block, arg);
9723 if (block && (block->last == ptr)) {
9727 } while(ptr != first);
9730 static void print_block(
9731 struct compile_state *state, struct block *block, void *arg)
9733 struct block_set *user;
9737 fprintf(fp, "\nblock: %p (%d) %p<-%p %p<-%p\n",
9741 block->left && block->left->use?block->left->use->member : 0,
9743 block->right && block->right->use?block->right->use->member : 0);
9744 if (block->first->op == OP_LABEL) {
9745 fprintf(fp, "%p:\n", block->first);
9747 for(ptr = block->first; ; ptr = ptr->next) {
9748 display_triple(fp, ptr);
9749 if (ptr == block->last)
9752 fprintf(fp, "users %d: ", block->users);
9753 for(user = block->use; user; user = user->next) {
9754 fprintf(fp, "%p (%d) ",
9756 user->member->vertex);
9762 static void print_blocks(struct compile_state *state, FILE *fp)
9764 fprintf(fp, "--------------- blocks ---------------\n");
9765 walk_blocks(state, print_block, fp);
9768 static void prune_nonblock_triples(struct compile_state *state)
9770 struct block *block;
9771 struct triple *first, *ins, *next;
9772 /* Delete the triples not in a basic block */
9773 first = RHS(state->main_function, 0);
9778 if (ins->op == OP_LABEL) {
9779 block = ins->u.block;
9782 release_triple(state, ins);
9784 if (block && block->last == ins) {
9788 } while(ins != first);
9791 static void setup_basic_blocks(struct compile_state *state)
9793 if (!triple_stores_block(state, RHS(state->main_function, 0)) ||
9794 !triple_stores_block(state, RHS(state->main_function,0)->prev)) {
9795 internal_error(state, 0, "ins will not store block?");
9797 /* Find the basic blocks */
9798 state->last_vertex = 0;
9799 state->first_block = basic_block(state, RHS(state->main_function,0));
9800 /* Delete the triples not in a basic block */
9801 prune_nonblock_triples(state);
9802 /* Find the last basic block */
9803 state->last_block = RHS(state->main_function, 0)->prev->u.block;
9804 if (!state->last_block) {
9805 internal_error(state, 0, "end not used?");
9807 /* If we are debugging print what I have just done */
9808 if (state->debug & DEBUG_BASIC_BLOCKS) {
9809 print_blocks(state, stdout);
9810 print_control_flow(state);
9814 static void free_basic_block(struct compile_state *state, struct block *block)
9816 struct block_set *entry, *next;
9817 struct block *child;
9821 if (block->vertex == -1) {
9826 unuse_block(block->left, block);
9829 unuse_block(block->right, block);
9832 unidom_block(block->idom, block);
9836 unipdom_block(block->ipdom, block);
9839 for(entry = block->use; entry; entry = next) {
9841 child = entry->member;
9842 unuse_block(block, child);
9843 if (child->left == block) {
9846 if (child->right == block) {
9850 for(entry = block->idominates; entry; entry = next) {
9852 child = entry->member;
9853 unidom_block(block, child);
9856 for(entry = block->domfrontier; entry; entry = next) {
9858 child = entry->member;
9859 undomf_block(block, child);
9861 for(entry = block->ipdominates; entry; entry = next) {
9863 child = entry->member;
9864 unipdom_block(block, child);
9867 for(entry = block->ipdomfrontier; entry; entry = next) {
9869 child = entry->member;
9870 unipdomf_block(block, child);
9872 if (block->users != 0) {
9873 internal_error(state, 0, "block still has users");
9875 free_basic_block(state, block->left);
9877 free_basic_block(state, block->right);
9879 memset(block, -1, sizeof(*block));
9883 static void free_basic_blocks(struct compile_state *state)
9885 struct triple *first, *ins;
9886 free_basic_block(state, state->first_block);
9887 state->last_vertex = 0;
9888 state->first_block = state->last_block = 0;
9889 first = RHS(state->main_function, 0);
9892 if (triple_stores_block(state, ins)) {
9896 } while(ins != first);
9901 struct block *block;
9902 struct sdom_block *sdominates;
9903 struct sdom_block *sdom_next;
9904 struct sdom_block *sdom;
9905 struct sdom_block *label;
9906 struct sdom_block *parent;
9907 struct sdom_block *ancestor;
9912 static void unsdom_block(struct sdom_block *block)
9914 struct sdom_block **ptr;
9915 if (!block->sdom_next) {
9918 ptr = &block->sdom->sdominates;
9920 if ((*ptr) == block) {
9921 *ptr = block->sdom_next;
9924 ptr = &(*ptr)->sdom_next;
9928 static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
9930 unsdom_block(block);
9932 block->sdom_next = sdom->sdominates;
9933 sdom->sdominates = block;
9938 static int initialize_sdblock(struct sdom_block *sd,
9939 struct block *parent, struct block *block, int vertex)
9941 if (!block || (sd[block->vertex].block == block)) {
9945 /* Renumber the blocks in a convinient fashion */
9946 block->vertex = vertex;
9947 sd[vertex].block = block;
9948 sd[vertex].sdom = &sd[vertex];
9949 sd[vertex].label = &sd[vertex];
9950 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
9951 sd[vertex].ancestor = 0;
9952 sd[vertex].vertex = vertex;
9953 vertex = initialize_sdblock(sd, block, block->left, vertex);
9954 vertex = initialize_sdblock(sd, block, block->right, vertex);
9958 static int initialize_sdpblock(
9959 struct compile_state *state, struct sdom_block *sd,
9960 struct block *parent, struct block *block, int vertex)
9962 struct block_set *user;
9963 if (!block || (sd[block->vertex].block == block)) {
9967 /* Renumber the blocks in a convinient fashion */
9968 block->vertex = vertex;
9969 sd[vertex].block = block;
9970 sd[vertex].sdom = &sd[vertex];
9971 sd[vertex].label = &sd[vertex];
9972 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
9973 sd[vertex].ancestor = 0;
9974 sd[vertex].vertex = vertex;
9975 for(user = block->use; user; user = user->next) {
9976 vertex = initialize_sdpblock(state, sd, block, user->member, vertex);
9981 static int setup_sdpblocks(struct compile_state *state, struct sdom_block *sd)
9983 struct block *block;
9985 /* Setup as many sdpblocks as possible without using fake edges */
9986 vertex = initialize_sdpblock(state, sd, 0, state->last_block, 0);
9988 /* Walk through the graph and find unconnected blocks. If
9989 * we can, add a fake edge from the unconnected blocks to the
9992 block = state->first_block->last->next->u.block;
9993 for(; block && block != state->first_block; block = block->last->next->u.block) {
9994 if (sd[block->vertex].block == block) {
9997 if (block->left != 0) {
10001 #if DEBUG_SDP_BLOCKS
10002 fprintf(stderr, "Adding %d\n", vertex +1);
10005 block->left = state->last_block;
10006 use_block(block->left, block);
10007 vertex = initialize_sdpblock(state, sd, state->last_block, block, vertex);
10012 static void compress_ancestors(struct sdom_block *v)
10014 /* This procedure assumes ancestor(v) != 0 */
10015 /* if (ancestor(ancestor(v)) != 0) {
10016 * compress(ancestor(ancestor(v)));
10017 * if (semi(label(ancestor(v))) < semi(label(v))) {
10018 * label(v) = label(ancestor(v));
10020 * ancestor(v) = ancestor(ancestor(v));
10023 if (!v->ancestor) {
10026 if (v->ancestor->ancestor) {
10027 compress_ancestors(v->ancestor->ancestor);
10028 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
10029 v->label = v->ancestor->label;
10031 v->ancestor = v->ancestor->ancestor;
10035 static void compute_sdom(struct compile_state *state, struct sdom_block *sd)
10039 * for each v <= pred(w) {
10041 * if (semi[u] < semi[w] {
10042 * semi[w] = semi[u];
10045 * add w to bucket(vertex(semi[w]));
10046 * LINK(parent(w), w);
10049 * for each v <= bucket(parent(w)) {
10050 * delete v from bucket(parent(w));
10052 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
10055 for(i = state->last_vertex; i >= 2; i--) {
10056 struct sdom_block *v, *parent, *next;
10057 struct block_set *user;
10058 struct block *block;
10059 block = sd[i].block;
10060 parent = sd[i].parent;
10062 for(user = block->use; user; user = user->next) {
10063 struct sdom_block *v, *u;
10064 v = &sd[user->member->vertex];
10065 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
10066 if (u->sdom->vertex < sd[i].sdom->vertex) {
10067 sd[i].sdom = u->sdom;
10070 sdom_block(sd[i].sdom, &sd[i]);
10071 sd[i].ancestor = parent;
10073 for(v = parent->sdominates; v; v = next) {
10074 struct sdom_block *u;
10075 next = v->sdom_next;
10077 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
10078 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
10079 u->block : parent->block;
10084 static void compute_spdom(struct compile_state *state, struct sdom_block *sd)
10088 * for each v <= pred(w) {
10090 * if (semi[u] < semi[w] {
10091 * semi[w] = semi[u];
10094 * add w to bucket(vertex(semi[w]));
10095 * LINK(parent(w), w);
10098 * for each v <= bucket(parent(w)) {
10099 * delete v from bucket(parent(w));
10101 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
10104 for(i = state->last_vertex; i >= 2; i--) {
10105 struct sdom_block *u, *v, *parent, *next;
10106 struct block *block;
10107 block = sd[i].block;
10108 parent = sd[i].parent;
10111 v = &sd[block->left->vertex];
10112 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
10113 if (u->sdom->vertex < sd[i].sdom->vertex) {
10114 sd[i].sdom = u->sdom;
10117 if (block->right && (block->right != block->left)) {
10118 v = &sd[block->right->vertex];
10119 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
10120 if (u->sdom->vertex < sd[i].sdom->vertex) {
10121 sd[i].sdom = u->sdom;
10124 sdom_block(sd[i].sdom, &sd[i]);
10125 sd[i].ancestor = parent;
10127 for(v = parent->sdominates; v; v = next) {
10128 struct sdom_block *u;
10129 next = v->sdom_next;
10131 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
10132 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
10133 u->block : parent->block;
10138 static void compute_idom(struct compile_state *state, struct sdom_block *sd)
10141 for(i = 2; i <= state->last_vertex; i++) {
10142 struct block *block;
10143 block = sd[i].block;
10144 if (block->idom->vertex != sd[i].sdom->vertex) {
10145 block->idom = block->idom->idom;
10147 idom_block(block->idom, block);
10149 sd[1].block->idom = 0;
10152 static void compute_ipdom(struct compile_state *state, struct sdom_block *sd)
10155 for(i = 2; i <= state->last_vertex; i++) {
10156 struct block *block;
10157 block = sd[i].block;
10158 if (block->ipdom->vertex != sd[i].sdom->vertex) {
10159 block->ipdom = block->ipdom->ipdom;
10161 ipdom_block(block->ipdom, block);
10163 sd[1].block->ipdom = 0;
10167 * Every vertex of a flowgraph G = (V, E, r) except r has
10168 * a unique immediate dominator.
10169 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
10170 * rooted at r, called the dominator tree of G, such that
10171 * v dominates w if and only if v is a proper ancestor of w in
10172 * the dominator tree.
10175 * If v and w are vertices of G such that v <= w,
10176 * than any path from v to w must contain a common ancestor
10179 /* Lemma 2: For any vertex w != r, idom(w) -> w */
10180 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
10181 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
10183 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
10184 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
10187 * Let w != r and let u be a vertex for which sdom(u) is
10188 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
10189 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
10191 /* Lemma 5: Let vertices v,w satisfy v -> w.
10192 * Then v -> idom(w) or idom(w) -> idom(v)
10195 static void find_immediate_dominators(struct compile_state *state)
10197 struct sdom_block *sd;
10198 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
10199 * vi > w for (1 <= i <= k - 1}
10202 * For any vertex w != r.
10204 * {v|(v,w) <= E and v < w } U
10205 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
10208 * Let w != r and let u be a vertex for which sdom(u) is
10209 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
10211 * { sdom(w) if sdom(w) = sdom(u),
10213 * { idom(u) otherwise
10215 /* The algorithm consists of the following 4 steps.
10216 * Step 1. Carry out a depth-first search of the problem graph.
10217 * Number the vertices from 1 to N as they are reached during
10218 * the search. Initialize the variables used in succeeding steps.
10219 * Step 2. Compute the semidominators of all vertices by applying
10220 * theorem 4. Carry out the computation vertex by vertex in
10221 * decreasing order by number.
10222 * Step 3. Implicitly define the immediate dominator of each vertex
10223 * by applying Corollary 1.
10224 * Step 4. Explicitly define the immediate dominator of each vertex,
10225 * carrying out the computation vertex by vertex in increasing order
10228 /* Step 1 initialize the basic block information */
10229 sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
10230 initialize_sdblock(sd, 0, state->first_block, 0);
10236 /* Step 2 compute the semidominators */
10237 /* Step 3 implicitly define the immediate dominator of each vertex */
10238 compute_sdom(state, sd);
10239 /* Step 4 explicitly define the immediate dominator of each vertex */
10240 compute_idom(state, sd);
10244 static void find_post_dominators(struct compile_state *state)
10246 struct sdom_block *sd;
10248 /* Step 1 initialize the basic block information */
10249 sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
10251 vertex = setup_sdpblocks(state, sd);
10252 if (vertex != state->last_vertex) {
10253 internal_error(state, 0, "missing %d blocks\n",
10254 state->last_vertex - vertex);
10257 /* Step 2 compute the semidominators */
10258 /* Step 3 implicitly define the immediate dominator of each vertex */
10259 compute_spdom(state, sd);
10260 /* Step 4 explicitly define the immediate dominator of each vertex */
10261 compute_ipdom(state, sd);
10267 static void find_block_domf(struct compile_state *state, struct block *block)
10269 struct block *child;
10270 struct block_set *user;
10271 if (block->domfrontier != 0) {
10272 internal_error(state, block->first, "domfrontier present?");
10274 for(user = block->idominates; user; user = user->next) {
10275 child = user->member;
10276 if (child->idom != block) {
10277 internal_error(state, block->first, "bad idom");
10279 find_block_domf(state, child);
10281 if (block->left && block->left->idom != block) {
10282 domf_block(block, block->left);
10284 if (block->right && block->right->idom != block) {
10285 domf_block(block, block->right);
10287 for(user = block->idominates; user; user = user->next) {
10288 struct block_set *frontier;
10289 child = user->member;
10290 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
10291 if (frontier->member->idom != block) {
10292 domf_block(block, frontier->member);
10298 static void find_block_ipdomf(struct compile_state *state, struct block *block)
10300 struct block *child;
10301 struct block_set *user;
10302 if (block->ipdomfrontier != 0) {
10303 internal_error(state, block->first, "ipdomfrontier present?");
10305 for(user = block->ipdominates; user; user = user->next) {
10306 child = user->member;
10307 if (child->ipdom != block) {
10308 internal_error(state, block->first, "bad ipdom");
10310 find_block_ipdomf(state, child);
10312 if (block->left && block->left->ipdom != block) {
10313 ipdomf_block(block, block->left);
10315 if (block->right && block->right->ipdom != block) {
10316 ipdomf_block(block, block->right);
10318 for(user = block->idominates; user; user = user->next) {
10319 struct block_set *frontier;
10320 child = user->member;
10321 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
10322 if (frontier->member->ipdom != block) {
10323 ipdomf_block(block, frontier->member);
10329 static void print_dominated(
10330 struct compile_state *state, struct block *block, void *arg)
10332 struct block_set *user;
10335 fprintf(fp, "%d:", block->vertex);
10336 for(user = block->idominates; user; user = user->next) {
10337 fprintf(fp, " %d", user->member->vertex);
10338 if (user->member->idom != block) {
10339 internal_error(state, user->member->first, "bad idom");
10345 static void print_dominators(struct compile_state *state, FILE *fp)
10347 fprintf(fp, "\ndominates\n");
10348 walk_blocks(state, print_dominated, fp);
10352 static int print_frontiers(
10353 struct compile_state *state, struct block *block, int vertex)
10355 struct block_set *user;
10357 if (!block || (block->vertex != vertex + 1)) {
10362 printf("%d:", block->vertex);
10363 for(user = block->domfrontier; user; user = user->next) {
10364 printf(" %d", user->member->vertex);
10368 vertex = print_frontiers(state, block->left, vertex);
10369 vertex = print_frontiers(state, block->right, vertex);
10372 static void print_dominance_frontiers(struct compile_state *state)
10374 printf("\ndominance frontiers\n");
10375 print_frontiers(state, state->first_block, 0);
10379 static void analyze_idominators(struct compile_state *state)
10381 /* Find the immediate dominators */
10382 find_immediate_dominators(state);
10383 /* Find the dominance frontiers */
10384 find_block_domf(state, state->first_block);
10385 /* If debuging print the print what I have just found */
10386 if (state->debug & DEBUG_FDOMINATORS) {
10387 print_dominators(state, stdout);
10388 print_dominance_frontiers(state);
10389 print_control_flow(state);
10395 static void print_ipdominated(
10396 struct compile_state *state, struct block *block, void *arg)
10398 struct block_set *user;
10401 fprintf(fp, "%d:", block->vertex);
10402 for(user = block->ipdominates; user; user = user->next) {
10403 fprintf(fp, " %d", user->member->vertex);
10404 if (user->member->ipdom != block) {
10405 internal_error(state, user->member->first, "bad ipdom");
10411 static void print_ipdominators(struct compile_state *state, FILE *fp)
10413 fprintf(fp, "\nipdominates\n");
10414 walk_blocks(state, print_ipdominated, fp);
10417 static int print_pfrontiers(
10418 struct compile_state *state, struct block *block, int vertex)
10420 struct block_set *user;
10422 if (!block || (block->vertex != vertex + 1)) {
10427 printf("%d:", block->vertex);
10428 for(user = block->ipdomfrontier; user; user = user->next) {
10429 printf(" %d", user->member->vertex);
10432 for(user = block->use; user; user = user->next) {
10433 vertex = print_pfrontiers(state, user->member, vertex);
10437 static void print_ipdominance_frontiers(struct compile_state *state)
10439 printf("\nipdominance frontiers\n");
10440 print_pfrontiers(state, state->last_block, 0);
10444 static void analyze_ipdominators(struct compile_state *state)
10446 /* Find the post dominators */
10447 find_post_dominators(state);
10448 /* Find the control dependencies (post dominance frontiers) */
10449 find_block_ipdomf(state, state->last_block);
10450 /* If debuging print the print what I have just found */
10451 if (state->debug & DEBUG_RDOMINATORS) {
10452 print_ipdominators(state, stdout);
10453 print_ipdominance_frontiers(state);
10454 print_control_flow(state);
10458 static int bdominates(struct compile_state *state,
10459 struct block *dom, struct block *sub)
10461 while(sub && (sub != dom)) {
10467 static int tdominates(struct compile_state *state,
10468 struct triple *dom, struct triple *sub)
10470 struct block *bdom, *bsub;
10472 bdom = block_of_triple(state, dom);
10473 bsub = block_of_triple(state, sub);
10474 if (bdom != bsub) {
10475 result = bdominates(state, bdom, bsub);
10478 struct triple *ins;
10480 while((ins != bsub->first) && (ins != dom)) {
10483 result = (ins == dom);
10488 static void insert_phi_operations(struct compile_state *state)
10491 struct triple *first;
10492 int *has_already, *work;
10493 struct block *work_list, **work_list_tail;
10495 struct triple *var, *vnext;
10497 size = sizeof(int) * (state->last_vertex + 1);
10498 has_already = xcmalloc(size, "has_already");
10499 work = xcmalloc(size, "work");
10502 first = RHS(state->main_function, 0);
10503 for(var = first->next; var != first ; var = vnext) {
10504 struct block *block;
10505 struct triple_set *user, *unext;
10507 if ((var->op != OP_ADECL) || !var->use) {
10512 work_list_tail = &work_list;
10513 for(user = var->use; user; user = unext) {
10514 unext = user->next;
10515 if (user->member->op == OP_READ) {
10518 if (user->member->op != OP_WRITE) {
10519 internal_error(state, user->member,
10520 "bad variable access");
10522 block = user->member->u.block;
10524 warning(state, user->member, "dead code");
10525 release_triple(state, user->member);
10528 if (work[block->vertex] >= iter) {
10531 work[block->vertex] = iter;
10532 *work_list_tail = block;
10533 block->work_next = 0;
10534 work_list_tail = &block->work_next;
10536 for(block = work_list; block; block = block->work_next) {
10537 struct block_set *df;
10538 for(df = block->domfrontier; df; df = df->next) {
10539 struct triple *phi;
10540 struct block *front;
10542 front = df->member;
10544 if (has_already[front->vertex] >= iter) {
10547 /* Count how many edges flow into this block */
10548 in_edges = front->users;
10549 /* Insert a phi function for this variable */
10550 get_occurance(var->occurance);
10551 phi = alloc_triple(
10552 state, OP_PHI, var->type, -1, in_edges,
10554 phi->u.block = front;
10555 MISC(phi, 0) = var;
10556 use_triple(var, phi);
10557 /* Insert the phi functions immediately after the label */
10558 insert_triple(state, front->first->next, phi);
10559 if (front->first == front->last) {
10560 front->last = front->first->next;
10562 has_already[front->vertex] = iter;
10564 /* If necessary plan to visit the basic block */
10565 if (work[front->vertex] >= iter) {
10568 work[front->vertex] = iter;
10569 *work_list_tail = front;
10570 front->work_next = 0;
10571 work_list_tail = &front->work_next;
10575 xfree(has_already);
10580 static int count_and_number_adecls(struct compile_state *state)
10582 struct triple *first, *ins;
10584 first = RHS(state->main_function, 0);
10587 if (ins->op == OP_ADECL) {
10592 } while(ins != first);
10596 static struct triple *peek_triple(struct triple_set **stacks, struct triple *var)
10598 struct triple_set *head;
10599 struct triple *top_val;
10601 head = stacks[var->id];
10603 top_val = head->member;
10608 static void push_triple(struct triple_set **stacks, struct triple *var, struct triple *val)
10610 struct triple_set *new;
10611 /* Append new to the head of the list,
10612 * it's the only sensible behavoir for a stack.
10614 new = xcmalloc(sizeof(*new), "triple_set");
10616 new->next = stacks[var->id];
10617 stacks[var->id] = new;
10620 static void pop_triple(struct triple_set **stacks, struct triple *var, struct triple *oldval)
10622 struct triple_set *set, **ptr;
10623 ptr = &stacks[var->id];
10626 if (set->member == oldval) {
10629 /* Only free one occurance from the stack */
10642 static void fixup_block_phi_variables(
10643 struct compile_state *state, struct triple_set **stacks, struct block *parent, struct block *block)
10645 struct block_set *set;
10646 struct triple *ptr;
10648 if (!parent || !block)
10650 /* Find the edge I am coming in on */
10652 for(set = block->use; set; set = set->next, edge++) {
10653 if (set->member == parent) {
10658 internal_error(state, 0, "phi input is not on a control predecessor");
10660 for(ptr = block->first; ; ptr = ptr->next) {
10661 if (ptr->op == OP_PHI) {
10662 struct triple *var, *val, **slot;
10663 var = MISC(ptr, 0);
10665 internal_error(state, ptr, "no var???");
10667 /* Find the current value of the variable */
10668 val = peek_triple(stacks, var);
10669 if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
10670 internal_error(state, val, "bad value in phi");
10672 if (edge >= TRIPLE_RHS(ptr->sizes)) {
10673 internal_error(state, ptr, "edges > phi rhs");
10675 slot = &RHS(ptr, edge);
10676 if ((*slot != 0) && (*slot != val)) {
10677 internal_error(state, ptr, "phi already bound on this edge");
10680 use_triple(val, ptr);
10682 if (ptr == block->last) {
10689 static void rename_block_variables(
10690 struct compile_state *state, struct triple_set **stacks, struct block *block)
10692 struct block_set *user;
10693 struct triple *ptr, *next, *last;
10697 last = block->first;
10699 for(ptr = block->first; !done; ptr = next) {
10701 if (ptr == block->last) {
10705 if (ptr->op == OP_READ) {
10706 struct triple *var, *val;
10708 unuse_triple(var, ptr);
10709 /* Find the current value of the variable */
10710 val = peek_triple(stacks, var);
10712 error(state, ptr, "variable used without being set");
10714 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10715 internal_error(state, val, "bad value in read");
10717 propogate_use(state, ptr, val);
10718 release_triple(state, ptr);
10722 if (ptr->op == OP_WRITE) {
10723 struct triple *var, *val, *tval;
10725 tval = val = RHS(ptr, 1);
10726 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10727 internal_error(state, ptr, "bad value in write");
10729 /* Insert a copy if the types differ */
10730 if (!equiv_types(ptr->type, val->type)) {
10731 if (val->op == OP_INTCONST) {
10732 tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
10733 tval->u.cval = val->u.cval;
10736 tval = pre_triple(state, ptr, OP_COPY, ptr->type, val, 0);
10737 use_triple(val, tval);
10739 unuse_triple(val, ptr);
10740 RHS(ptr, 1) = tval;
10741 use_triple(tval, ptr);
10743 propogate_use(state, ptr, tval);
10744 unuse_triple(var, ptr);
10745 /* Push OP_WRITE ptr->right onto a stack of variable uses */
10746 push_triple(stacks, var, tval);
10748 if (ptr->op == OP_PHI) {
10749 struct triple *var;
10750 var = MISC(ptr, 0);
10751 /* Push OP_PHI onto a stack of variable uses */
10752 push_triple(stacks, var, ptr);
10756 block->last = last;
10758 /* Fixup PHI functions in the cf successors */
10759 fixup_block_phi_variables(state, stacks, block, block->left);
10760 fixup_block_phi_variables(state, stacks, block, block->right);
10761 /* rename variables in the dominated nodes */
10762 for(user = block->idominates; user; user = user->next) {
10763 rename_block_variables(state, stacks, user->member);
10765 /* pop the renamed variable stack */
10766 last = block->first;
10768 for(ptr = block->first; !done ; ptr = next) {
10770 if (ptr == block->last) {
10773 if (ptr->op == OP_WRITE) {
10774 struct triple *var;
10776 /* Pop OP_WRITE ptr->right from the stack of variable uses */
10777 pop_triple(stacks, var, RHS(ptr, 1));
10778 release_triple(state, ptr);
10781 if (ptr->op == OP_PHI) {
10782 struct triple *var;
10783 var = MISC(ptr, 0);
10784 /* Pop OP_WRITE ptr->right from the stack of variable uses */
10785 pop_triple(stacks, var, ptr);
10789 block->last = last;
10792 static void prune_block_variables(struct compile_state *state,
10793 struct block *block)
10795 struct block_set *user;
10796 struct triple *next, *last, *ptr;
10798 last = block->first;
10800 for(ptr = block->first; !done; ptr = next) {
10802 if (ptr == block->last) {
10805 if (ptr->op == OP_ADECL) {
10806 struct triple_set *user, *next;
10807 for(user = ptr->use; user; user = next) {
10808 struct triple *use;
10810 use = user->member;
10811 if (use->op != OP_PHI) {
10812 internal_error(state, use, "decl still used");
10814 if (MISC(use, 0) != ptr) {
10815 internal_error(state, use, "bad phi use of decl");
10817 unuse_triple(ptr, use);
10820 release_triple(state, ptr);
10825 block->last = last;
10826 for(user = block->idominates; user; user = user->next) {
10827 prune_block_variables(state, user->member);
10831 struct phi_triple {
10832 struct triple *phi;
10837 static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi)
10839 struct triple **slot;
10841 if (live[phi->id].alive) {
10844 live[phi->id].alive = 1;
10845 zrhs = TRIPLE_RHS(phi->sizes);
10846 slot = &RHS(phi, 0);
10847 for(i = 0; i < zrhs; i++) {
10848 struct triple *used;
10850 if (used && (used->op == OP_PHI)) {
10851 keep_phi(state, live, used);
10856 static void prune_unused_phis(struct compile_state *state)
10858 struct triple *first, *phi;
10859 struct phi_triple *live;
10863 /* Find the first instruction */
10864 first = RHS(state->main_function, 0);
10866 /* Count how many phi functions I need to process */
10868 for(phi = first->next; phi != first; phi = phi->next) {
10869 if (phi->op == OP_PHI) {
10874 /* Mark them all dead */
10875 live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple");
10877 for(phi = first->next; phi != first; phi = phi->next) {
10878 if (phi->op != OP_PHI) {
10881 live[phis].alive = 0;
10882 live[phis].orig_id = phi->id;
10883 live[phis].phi = phi;
10888 /* Mark phis alive that are used by non phis */
10889 for(i = 0; i < phis; i++) {
10890 struct triple_set *set;
10891 for(set = live[i].phi->use; !live[i].alive && set; set = set->next) {
10892 if (set->member->op != OP_PHI) {
10893 keep_phi(state, live, live[i].phi);
10899 /* Delete the extraneous phis */
10900 for(i = 0; i < phis; i++) {
10901 struct triple **slot;
10903 if (!live[i].alive) {
10904 release_triple(state, live[i].phi);
10908 slot = &RHS(phi, 0);
10909 zrhs = TRIPLE_RHS(phi->sizes);
10910 for(j = 0; j < zrhs; j++) {
10912 error(state, phi, "variable not set on all paths to use");
10920 static void transform_to_ssa_form(struct compile_state *state)
10922 struct triple_set **stacks;
10924 insert_phi_operations(state);
10926 printf("@%s:%d\n", __FILE__, __LINE__);
10927 print_blocks(state, stdout);
10930 /* Allocate stacks for the Variables */
10931 adecls = count_and_number_adecls(state);
10932 stacks = xcmalloc(sizeof(stacks[0])*(adecls + 1), "adecl stacks");
10933 rename_block_variables(state, stacks, state->first_block);
10936 prune_block_variables(state, state->first_block);
10939 prune_unused_phis(state);
10945 static void clear_vertex(
10946 struct compile_state *state, struct block *block, void *arg)
10951 static void mark_live_block(
10952 struct compile_state *state, struct block *block, int *next_vertex)
10954 /* See if this is a block that has not been marked */
10955 if (block->vertex != 0) {
10958 block->vertex = *next_vertex;
10960 if (triple_is_branch(state, block->last)) {
10961 struct triple **targ;
10962 targ = triple_targ(state, block->last, 0);
10963 for(; targ; targ = triple_targ(state, block->last, targ)) {
10967 if (!triple_stores_block(state, *targ)) {
10968 internal_error(state, 0, "bad targ");
10970 mark_live_block(state, (*targ)->u.block, next_vertex);
10973 else if (block->last->next != RHS(state->main_function, 0)) {
10974 struct triple *ins;
10975 ins = block->last->next;
10976 if (!triple_stores_block(state, ins)) {
10977 internal_error(state, 0, "bad block start");
10979 mark_live_block(state, ins->u.block, next_vertex);
10983 static void transform_from_ssa_form(struct compile_state *state)
10985 /* To get out of ssa form we insert moves on the incoming
10986 * edges to blocks containting phi functions.
10988 struct triple *first;
10989 struct triple *phi, *next;
10992 /* Walk the control flow to see which blocks remain alive */
10993 walk_blocks(state, clear_vertex, 0);
10995 mark_live_block(state, state->first_block, &next_vertex);
10997 /* Walk all of the operations to find the phi functions */
10998 first = RHS(state->main_function, 0);
10999 for(phi = first->next; phi != first ; phi = next) {
11000 struct block_set *set;
11001 struct block *block;
11002 struct triple **slot;
11003 struct triple *var, *read;
11004 struct triple_set *use, *use_next;
11007 if (phi->op != OP_PHI) {
11010 block = phi->u.block;
11011 slot = &RHS(phi, 0);
11013 /* Forget uses from code in dead blocks */
11014 for(use = phi->use; use; use = use_next) {
11015 struct block *ublock;
11016 struct triple **expr;
11017 use_next = use->next;
11018 ublock = block_of_triple(state, use->member);
11019 if ((use->member == phi) || (ublock->vertex != 0)) {
11022 expr = triple_rhs(state, use->member, 0);
11023 for(; expr; expr = triple_rhs(state, use->member, expr)) {
11024 if (*expr == phi) {
11028 unuse_triple(phi, use->member);
11031 #warning "CHECK_ME does the OP_ADECL need to be placed somewhere that dominates all of the incoming phi edges?"
11032 /* A variable to replace the phi function */
11033 var = post_triple(state, phi, OP_ADECL, phi->type, 0,0);
11034 /* A read of the single value that is set into the variable */
11035 read = post_triple(state, var, OP_READ, phi->type, var, 0);
11036 use_triple(var, read);
11038 /* Replaces uses of the phi with variable reads */
11039 propogate_use(state, phi, read);
11041 /* Walk all of the incoming edges/blocks and insert moves.
11043 for(edge = 0, set = block->use; set; set = set->next, edge++) {
11044 struct block *eblock;
11045 struct triple *move;
11046 struct triple *val, *base;
11047 eblock = set->member;
11050 unuse_triple(val, phi);
11052 if (!val || (val == &zero_triple) ||
11053 (block->vertex == 0) || (eblock->vertex == 0) ||
11054 (val == phi) || (val == read)) {
11058 /* Make certain the write is placed in the edge block... */
11059 base = eblock->first;
11060 if (block_of_triple(state, val) == eblock) {
11063 move = post_triple(state, base, OP_WRITE, phi->type, var, val);
11064 use_triple(val, move);
11065 use_triple(var, move);
11067 /* See if there are any writers of var */
11069 for(use = var->use; use; use = use->next) {
11070 if ((use->member->op == OP_WRITE) &&
11071 (RHS(use->member, 0) == var)) {
11075 /* If var is not used free it */
11077 unuse_triple(var, read);
11078 free_triple(state, read);
11079 free_triple(state, var);
11082 /* Release the phi function */
11083 release_triple(state, phi);
11090 * Register conflict resolution
11091 * =========================================================
11094 static struct reg_info find_def_color(
11095 struct compile_state *state, struct triple *def)
11097 struct triple_set *set;
11098 struct reg_info info;
11099 info.reg = REG_UNSET;
11101 if (!triple_is_def(state, def)) {
11104 info = arch_reg_lhs(state, def, 0);
11105 if (info.reg >= MAX_REGISTERS) {
11106 info.reg = REG_UNSET;
11108 for(set = def->use; set; set = set->next) {
11109 struct reg_info tinfo;
11111 i = find_rhs_use(state, set->member, def);
11115 tinfo = arch_reg_rhs(state, set->member, i);
11116 if (tinfo.reg >= MAX_REGISTERS) {
11117 tinfo.reg = REG_UNSET;
11119 if ((tinfo.reg != REG_UNSET) &&
11120 (info.reg != REG_UNSET) &&
11121 (tinfo.reg != info.reg)) {
11122 internal_error(state, def, "register conflict");
11124 if ((info.regcm & tinfo.regcm) == 0) {
11125 internal_error(state, def, "regcm conflict %x & %x == 0",
11126 info.regcm, tinfo.regcm);
11128 if (info.reg == REG_UNSET) {
11129 info.reg = tinfo.reg;
11131 info.regcm &= tinfo.regcm;
11133 if (info.reg >= MAX_REGISTERS) {
11134 internal_error(state, def, "register out of range");
11139 static struct reg_info find_lhs_pre_color(
11140 struct compile_state *state, struct triple *ins, int index)
11142 struct reg_info info;
11144 zrhs = TRIPLE_RHS(ins->sizes);
11145 zlhs = TRIPLE_LHS(ins->sizes);
11146 if (!zlhs && triple_is_def(state, ins)) {
11149 if (index >= zlhs) {
11150 internal_error(state, ins, "Bad lhs %d", index);
11152 info = arch_reg_lhs(state, ins, index);
11153 for(i = 0; i < zrhs; i++) {
11154 struct reg_info rinfo;
11155 rinfo = arch_reg_rhs(state, ins, i);
11156 if ((info.reg == rinfo.reg) &&
11157 (rinfo.reg >= MAX_REGISTERS)) {
11158 struct reg_info tinfo;
11159 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
11160 info.reg = tinfo.reg;
11161 info.regcm &= tinfo.regcm;
11165 if (info.reg >= MAX_REGISTERS) {
11166 info.reg = REG_UNSET;
11171 static struct reg_info find_rhs_post_color(
11172 struct compile_state *state, struct triple *ins, int index);
11174 static struct reg_info find_lhs_post_color(
11175 struct compile_state *state, struct triple *ins, int index)
11177 struct triple_set *set;
11178 struct reg_info info;
11179 struct triple *lhs;
11180 #if DEBUG_TRIPLE_COLOR
11181 fprintf(stderr, "find_lhs_post_color(%p, %d)\n",
11184 if ((index == 0) && triple_is_def(state, ins)) {
11187 else if (index < TRIPLE_LHS(ins->sizes)) {
11188 lhs = LHS(ins, index);
11191 internal_error(state, ins, "Bad lhs %d", index);
11194 info = arch_reg_lhs(state, ins, index);
11195 if (info.reg >= MAX_REGISTERS) {
11196 info.reg = REG_UNSET;
11198 for(set = lhs->use; set; set = set->next) {
11199 struct reg_info rinfo;
11200 struct triple *user;
11202 user = set->member;
11203 zrhs = TRIPLE_RHS(user->sizes);
11204 for(i = 0; i < zrhs; i++) {
11205 if (RHS(user, i) != lhs) {
11208 rinfo = find_rhs_post_color(state, user, i);
11209 if ((info.reg != REG_UNSET) &&
11210 (rinfo.reg != REG_UNSET) &&
11211 (info.reg != rinfo.reg)) {
11212 internal_error(state, ins, "register conflict");
11214 if ((info.regcm & rinfo.regcm) == 0) {
11215 internal_error(state, ins, "regcm conflict %x & %x == 0",
11216 info.regcm, rinfo.regcm);
11218 if (info.reg == REG_UNSET) {
11219 info.reg = rinfo.reg;
11221 info.regcm &= rinfo.regcm;
11224 #if DEBUG_TRIPLE_COLOR
11225 fprintf(stderr, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
11226 ins, index, info.reg, info.regcm);
11231 static struct reg_info find_rhs_post_color(
11232 struct compile_state *state, struct triple *ins, int index)
11234 struct reg_info info, rinfo;
11236 #if DEBUG_TRIPLE_COLOR
11237 fprintf(stderr, "find_rhs_post_color(%p, %d)\n",
11240 rinfo = arch_reg_rhs(state, ins, index);
11241 zlhs = TRIPLE_LHS(ins->sizes);
11242 if (!zlhs && triple_is_def(state, ins)) {
11246 if (info.reg >= MAX_REGISTERS) {
11247 info.reg = REG_UNSET;
11249 for(i = 0; i < zlhs; i++) {
11250 struct reg_info linfo;
11251 linfo = arch_reg_lhs(state, ins, i);
11252 if ((linfo.reg == rinfo.reg) &&
11253 (linfo.reg >= MAX_REGISTERS)) {
11254 struct reg_info tinfo;
11255 tinfo = find_lhs_post_color(state, ins, i);
11256 if (tinfo.reg >= MAX_REGISTERS) {
11257 tinfo.reg = REG_UNSET;
11259 info.regcm &= linfo.regcm;
11260 info.regcm &= tinfo.regcm;
11261 if (info.reg != REG_UNSET) {
11262 internal_error(state, ins, "register conflict");
11264 if (info.regcm == 0) {
11265 internal_error(state, ins, "regcm conflict");
11267 info.reg = tinfo.reg;
11270 #if DEBUG_TRIPLE_COLOR
11271 fprintf(stderr, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
11272 ins, index, info.reg, info.regcm);
11277 static struct reg_info find_lhs_color(
11278 struct compile_state *state, struct triple *ins, int index)
11280 struct reg_info pre, post, info;
11281 #if DEBUG_TRIPLE_COLOR
11282 fprintf(stderr, "find_lhs_color(%p, %d)\n",
11285 pre = find_lhs_pre_color(state, ins, index);
11286 post = find_lhs_post_color(state, ins, index);
11287 if ((pre.reg != post.reg) &&
11288 (pre.reg != REG_UNSET) &&
11289 (post.reg != REG_UNSET)) {
11290 internal_error(state, ins, "register conflict");
11292 info.regcm = pre.regcm & post.regcm;
11293 info.reg = pre.reg;
11294 if (info.reg == REG_UNSET) {
11295 info.reg = post.reg;
11297 #if DEBUG_TRIPLE_COLOR
11298 fprintf(stderr, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
11299 ins, index, info.reg, info.regcm,
11300 pre.reg, pre.regcm, post.reg, post.regcm);
11305 static struct triple *post_copy(struct compile_state *state, struct triple *ins)
11307 struct triple_set *entry, *next;
11308 struct triple *out;
11309 struct reg_info info, rinfo;
11311 info = arch_reg_lhs(state, ins, 0);
11312 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
11313 use_triple(RHS(out, 0), out);
11314 /* Get the users of ins to use out instead */
11315 for(entry = ins->use; entry; entry = next) {
11317 next = entry->next;
11318 if (entry->member == out) {
11321 i = find_rhs_use(state, entry->member, ins);
11325 rinfo = arch_reg_rhs(state, entry->member, i);
11326 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
11329 replace_rhs_use(state, ins, out, entry->member);
11331 transform_to_arch_instruction(state, out);
11335 static struct triple *typed_pre_copy(
11336 struct compile_state *state, struct type *type, struct triple *ins, int index)
11338 /* Carefully insert enough operations so that I can
11339 * enter any operation with a GPR32.
11342 struct triple **expr;
11344 struct reg_info info;
11345 if (ins->op == OP_PHI) {
11346 internal_error(state, ins, "pre_copy on a phi?");
11348 classes = arch_type_to_regcm(state, type);
11349 info = arch_reg_rhs(state, ins, index);
11350 expr = &RHS(ins, index);
11351 if ((info.regcm & classes) == 0) {
11352 internal_error(state, ins, "pre_copy with no register classes");
11354 in = pre_triple(state, ins, OP_COPY, type, *expr, 0);
11355 unuse_triple(*expr, ins);
11357 use_triple(RHS(in, 0), in);
11358 use_triple(in, ins);
11359 transform_to_arch_instruction(state, in);
11363 static struct triple *pre_copy(
11364 struct compile_state *state, struct triple *ins, int index)
11366 return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
11370 static void insert_copies_to_phi(struct compile_state *state)
11372 /* To get out of ssa form we insert moves on the incoming
11373 * edges to blocks containting phi functions.
11375 struct triple *first;
11376 struct triple *phi;
11378 /* Walk all of the operations to find the phi functions */
11379 first = RHS(state->main_function, 0);
11380 for(phi = first->next; phi != first ; phi = phi->next) {
11381 struct block_set *set;
11382 struct block *block;
11383 struct triple **slot, *copy;
11385 if (phi->op != OP_PHI) {
11388 phi->id |= TRIPLE_FLAG_POST_SPLIT;
11389 block = phi->u.block;
11390 slot = &RHS(phi, 0);
11391 /* Phi's that feed into mandatory live range joins
11392 * cause nasty complications. Insert a copy of
11393 * the phi value so I never have to deal with
11394 * that in the rest of the code.
11396 copy = post_copy(state, phi);
11397 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
11398 /* Walk all of the incoming edges/blocks and insert moves.
11400 for(edge = 0, set = block->use; set; set = set->next, edge++) {
11401 struct block *eblock;
11402 struct triple *move;
11403 struct triple *val;
11404 struct triple *ptr;
11405 eblock = set->member;
11412 get_occurance(val->occurance);
11413 move = build_triple(state, OP_COPY, phi->type, val, 0,
11415 move->u.block = eblock;
11416 move->id |= TRIPLE_FLAG_PRE_SPLIT;
11417 use_triple(val, move);
11420 unuse_triple(val, phi);
11421 use_triple(move, phi);
11423 /* Walk up the dominator tree until I have found the appropriate block */
11424 while(eblock && !tdominates(state, val, eblock->last)) {
11425 eblock = eblock->idom;
11428 internal_error(state, phi, "Cannot find block dominated by %p",
11432 /* Walk through the block backwards to find
11433 * an appropriate location for the OP_COPY.
11435 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
11436 struct triple **expr;
11437 if ((ptr == phi) || (ptr == val)) {
11440 expr = triple_rhs(state, ptr, 0);
11441 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11442 if ((*expr) == phi) {
11448 if (triple_is_branch(state, ptr)) {
11449 internal_error(state, ptr,
11450 "Could not insert write to phi");
11452 insert_triple(state, ptr->next, move);
11453 if (eblock->last == ptr) {
11454 eblock->last = move;
11456 transform_to_arch_instruction(state, move);
11461 struct triple_reg_set {
11462 struct triple_reg_set *next;
11463 struct triple *member;
11464 struct triple *new;
11468 struct block *block;
11469 struct triple_reg_set *in;
11470 struct triple_reg_set *out;
11474 static int do_triple_set(struct triple_reg_set **head,
11475 struct triple *member, struct triple *new_member)
11477 struct triple_reg_set **ptr, *new;
11482 if ((*ptr)->member == member) {
11485 ptr = &(*ptr)->next;
11487 new = xcmalloc(sizeof(*new), "triple_set");
11488 new->member = member;
11489 new->new = new_member;
11495 static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
11497 struct triple_reg_set *entry, **ptr;
11501 if (entry->member == member) {
11502 *ptr = entry->next;
11507 ptr = &entry->next;
11512 static int in_triple(struct reg_block *rb, struct triple *in)
11514 return do_triple_set(&rb->in, in, 0);
11516 static void unin_triple(struct reg_block *rb, struct triple *unin)
11518 do_triple_unset(&rb->in, unin);
11521 static int out_triple(struct reg_block *rb, struct triple *out)
11523 return do_triple_set(&rb->out, out, 0);
11525 static void unout_triple(struct reg_block *rb, struct triple *unout)
11527 do_triple_unset(&rb->out, unout);
11530 static int initialize_regblock(struct reg_block *blocks,
11531 struct block *block, int vertex)
11533 struct block_set *user;
11534 if (!block || (blocks[block->vertex].block == block)) {
11538 /* Renumber the blocks in a convinient fashion */
11539 block->vertex = vertex;
11540 blocks[vertex].block = block;
11541 blocks[vertex].vertex = vertex;
11542 for(user = block->use; user; user = user->next) {
11543 vertex = initialize_regblock(blocks, user->member, vertex);
11548 static int phi_in(struct compile_state *state, struct reg_block *blocks,
11549 struct reg_block *rb, struct block *suc)
11551 /* Read the conditional input set of a successor block
11552 * (i.e. the input to the phi nodes) and place it in the
11553 * current blocks output set.
11555 struct block_set *set;
11556 struct triple *ptr;
11560 /* Find the edge I am coming in on */
11561 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
11562 if (set->member == rb->block) {
11567 internal_error(state, 0, "Not coming on a control edge?");
11569 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
11570 struct triple **slot, *expr, *ptr2;
11571 int out_change, done2;
11572 done = (ptr == suc->last);
11573 if (ptr->op != OP_PHI) {
11576 slot = &RHS(ptr, 0);
11578 out_change = out_triple(rb, expr);
11582 /* If we don't define the variable also plast it
11583 * in the current blocks input set.
11585 ptr2 = rb->block->first;
11586 for(done2 = 0; !done2; ptr2 = ptr2->next) {
11587 if (ptr2 == expr) {
11590 done2 = (ptr2 == rb->block->last);
11595 change |= in_triple(rb, expr);
11600 static int reg_in(struct compile_state *state, struct reg_block *blocks,
11601 struct reg_block *rb, struct block *suc)
11603 struct triple_reg_set *in_set;
11606 /* Read the input set of a successor block
11607 * and place it in the current blocks output set.
11609 in_set = blocks[suc->vertex].in;
11610 for(; in_set; in_set = in_set->next) {
11611 int out_change, done;
11612 struct triple *first, *last, *ptr;
11613 out_change = out_triple(rb, in_set->member);
11617 /* If we don't define the variable also place it
11618 * in the current blocks input set.
11620 first = rb->block->first;
11621 last = rb->block->last;
11623 for(ptr = first; !done; ptr = ptr->next) {
11624 if (ptr == in_set->member) {
11627 done = (ptr == last);
11632 change |= in_triple(rb, in_set->member);
11634 change |= phi_in(state, blocks, rb, suc);
11639 static int use_in(struct compile_state *state, struct reg_block *rb)
11641 /* Find the variables we use but don't define and add
11642 * it to the current blocks input set.
11644 #warning "FIXME is this O(N^2) algorithm bad?"
11645 struct block *block;
11646 struct triple *ptr;
11651 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
11652 struct triple **expr;
11653 done = (ptr == block->first);
11654 /* The variable a phi function uses depends on the
11655 * control flow, and is handled in phi_in, not
11658 if (ptr->op == OP_PHI) {
11661 expr = triple_rhs(state, ptr, 0);
11662 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11663 struct triple *rhs, *test;
11669 /* See if rhs is defined in this block */
11670 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
11671 tdone = (test == block->first);
11677 /* If I still have a valid rhs add it to in */
11678 change |= in_triple(rb, rhs);
11684 static struct reg_block *compute_variable_lifetimes(
11685 struct compile_state *state)
11687 struct reg_block *blocks;
11690 sizeof(*blocks)*(state->last_vertex + 1), "reg_block");
11691 initialize_regblock(blocks, state->last_block, 0);
11695 for(i = 1; i <= state->last_vertex; i++) {
11696 struct reg_block *rb;
11698 /* Add the left successor's input set to in */
11699 if (rb->block->left) {
11700 change |= reg_in(state, blocks, rb, rb->block->left);
11702 /* Add the right successor's input set to in */
11703 if ((rb->block->right) &&
11704 (rb->block->right != rb->block->left)) {
11705 change |= reg_in(state, blocks, rb, rb->block->right);
11707 /* Add use to in... */
11708 change |= use_in(state, rb);
11714 static void free_variable_lifetimes(
11715 struct compile_state *state, struct reg_block *blocks)
11718 /* free in_set && out_set on each block */
11719 for(i = 1; i <= state->last_vertex; i++) {
11720 struct triple_reg_set *entry, *next;
11721 struct reg_block *rb;
11723 for(entry = rb->in; entry ; entry = next) {
11724 next = entry->next;
11725 do_triple_unset(&rb->in, entry->member);
11727 for(entry = rb->out; entry; entry = next) {
11728 next = entry->next;
11729 do_triple_unset(&rb->out, entry->member);
11736 typedef void (*wvl_cb_t)(
11737 struct compile_state *state,
11738 struct reg_block *blocks, struct triple_reg_set *live,
11739 struct reg_block *rb, struct triple *ins, void *arg);
11741 static void walk_variable_lifetimes(struct compile_state *state,
11742 struct reg_block *blocks, wvl_cb_t cb, void *arg)
11746 for(i = 1; i <= state->last_vertex; i++) {
11747 struct triple_reg_set *live;
11748 struct triple_reg_set *entry, *next;
11749 struct triple *ptr, *prev;
11750 struct reg_block *rb;
11751 struct block *block;
11754 /* Get the blocks */
11758 /* Copy out into live */
11760 for(entry = rb->out; entry; entry = next) {
11761 next = entry->next;
11762 do_triple_set(&live, entry->member, entry->new);
11764 /* Walk through the basic block calculating live */
11765 for(done = 0, ptr = block->last; !done; ptr = prev) {
11766 struct triple **expr;
11769 done = (ptr == block->first);
11771 /* Ensure the current definition is in live */
11772 if (triple_is_def(state, ptr)) {
11773 do_triple_set(&live, ptr, 0);
11776 /* Inform the callback function of what is
11779 cb(state, blocks, live, rb, ptr, arg);
11781 /* Remove the current definition from live */
11782 do_triple_unset(&live, ptr);
11784 /* Add the current uses to live.
11786 * It is safe to skip phi functions because they do
11787 * not have any block local uses, and the block
11788 * output sets already properly account for what
11789 * control flow depedent uses phi functions do have.
11791 if (ptr->op == OP_PHI) {
11794 expr = triple_rhs(state, ptr, 0);
11795 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11796 /* If the triple is not a definition skip it. */
11797 if (!*expr || !triple_is_def(state, *expr)) {
11800 do_triple_set(&live, *expr, 0);
11804 for(entry = live; entry; entry = next) {
11805 next = entry->next;
11806 do_triple_unset(&live, entry->member);
11811 static int count_triples(struct compile_state *state)
11813 struct triple *first, *ins;
11815 first = RHS(state->main_function, 0);
11820 } while (ins != first);
11825 struct dead_triple {
11826 struct triple *triple;
11827 struct dead_triple *work_next;
11828 struct block *block;
11831 #define TRIPLE_FLAG_ALIVE 1
11835 static void awaken(
11836 struct compile_state *state,
11837 struct dead_triple *dtriple, struct triple **expr,
11838 struct dead_triple ***work_list_tail)
11840 struct triple *triple;
11841 struct dead_triple *dt;
11849 if (triple->id <= 0) {
11850 internal_error(state, triple, "bad triple id: %d",
11853 if (triple->op == OP_NOOP) {
11854 internal_warning(state, triple, "awakening noop?");
11857 dt = &dtriple[triple->id];
11858 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11859 dt->flags |= TRIPLE_FLAG_ALIVE;
11860 if (!dt->work_next) {
11861 **work_list_tail = dt;
11862 *work_list_tail = &dt->work_next;
11867 static void eliminate_inefectual_code(struct compile_state *state)
11869 struct block *block;
11870 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
11872 struct triple *first, *ins;
11874 /* Setup the work list */
11876 work_list_tail = &work_list;
11878 first = RHS(state->main_function, 0);
11880 /* Count how many triples I have */
11881 triples = count_triples(state);
11883 /* Now put then in an array and mark all of the triples dead */
11884 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
11890 if (ins->op == OP_LABEL) {
11891 block = ins->u.block;
11893 dtriple[i].triple = ins;
11894 dtriple[i].block = block;
11895 dtriple[i].flags = 0;
11896 dtriple[i].color = ins->id;
11898 /* See if it is an operation we always keep */
11899 #warning "FIXME handle the case of killing a branch instruction"
11900 if (!triple_is_pure(state, ins) || triple_is_branch(state, ins)) {
11901 awaken(state, dtriple, &ins, &work_list_tail);
11904 /* Unconditionally keep the very last instruction */
11905 else if (ins->next == first) {
11906 awaken(state, dtriple, &ins, &work_list_tail);
11911 } while(ins != first);
11913 struct dead_triple *dt;
11914 struct block_set *user;
11915 struct triple **expr;
11917 work_list = dt->work_next;
11919 work_list_tail = &work_list;
11921 /* Wake up the data depencencies of this triple */
11924 expr = triple_rhs(state, dt->triple, expr);
11925 awaken(state, dtriple, expr, &work_list_tail);
11928 expr = triple_lhs(state, dt->triple, expr);
11929 awaken(state, dtriple, expr, &work_list_tail);
11932 expr = triple_misc(state, dt->triple, expr);
11933 awaken(state, dtriple, expr, &work_list_tail);
11935 /* Wake up the forward control dependencies */
11937 expr = triple_targ(state, dt->triple, expr);
11938 awaken(state, dtriple, expr, &work_list_tail);
11940 /* Wake up the reverse control dependencies of this triple */
11941 for(user = dt->block->ipdomfrontier; user; user = user->next) {
11942 awaken(state, dtriple, &user->member->last, &work_list_tail);
11945 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
11946 if ((dt->triple->op == OP_NOOP) &&
11947 (dt->flags & TRIPLE_FLAG_ALIVE)) {
11948 internal_error(state, dt->triple, "noop effective?");
11950 dt->triple->id = dt->color; /* Restore the color */
11951 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11952 #warning "FIXME handle the case of killing a basic block"
11953 if (dt->block->first == dt->triple) {
11956 if (dt->block->last == dt->triple) {
11957 dt->block->last = dt->triple->prev;
11959 release_triple(state, dt->triple);
11966 static void insert_mandatory_copies(struct compile_state *state)
11968 struct triple *ins, *first;
11970 /* The object is with a minimum of inserted copies,
11971 * to resolve in fundamental register conflicts between
11972 * register value producers and consumers.
11973 * Theoretically we may be greater than minimal when we
11974 * are inserting copies before instructions but that
11975 * case should be rare.
11977 first = RHS(state->main_function, 0);
11980 struct triple_set *entry, *next;
11981 struct triple *tmp;
11982 struct reg_info info;
11983 unsigned reg, regcm;
11984 int do_post_copy, do_pre_copy;
11986 if (!triple_is_def(state, ins)) {
11989 /* Find the architecture specific color information */
11990 info = arch_reg_lhs(state, ins, 0);
11991 if (info.reg >= MAX_REGISTERS) {
11992 info.reg = REG_UNSET;
11996 regcm = arch_type_to_regcm(state, ins->type);
11997 do_post_copy = do_pre_copy = 0;
11999 /* Walk through the uses of ins and check for conflicts */
12000 for(entry = ins->use; entry; entry = next) {
12001 struct reg_info rinfo;
12003 next = entry->next;
12004 i = find_rhs_use(state, entry->member, ins);
12009 /* Find the users color requirements */
12010 rinfo = arch_reg_rhs(state, entry->member, i);
12011 if (rinfo.reg >= MAX_REGISTERS) {
12012 rinfo.reg = REG_UNSET;
12015 /* See if I need a pre_copy */
12016 if (rinfo.reg != REG_UNSET) {
12017 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
12022 regcm &= rinfo.regcm;
12023 regcm = arch_regcm_normalize(state, regcm);
12027 /* Always use pre_copies for constants.
12028 * They do not take up any registers until a
12029 * copy places them in one.
12031 if ((info.reg == REG_UNNEEDED) &&
12032 (rinfo.reg != REG_UNNEEDED)) {
12038 (((info.reg != REG_UNSET) &&
12039 (reg != REG_UNSET) &&
12040 (info.reg != reg)) ||
12041 ((info.regcm & regcm) == 0));
12044 regcm = info.regcm;
12045 /* Walk through the uses of ins and do a pre_copy or see if a post_copy is warranted */
12046 for(entry = ins->use; entry; entry = next) {
12047 struct reg_info rinfo;
12049 next = entry->next;
12050 i = find_rhs_use(state, entry->member, ins);
12055 /* Find the users color requirements */
12056 rinfo = arch_reg_rhs(state, entry->member, i);
12057 if (rinfo.reg >= MAX_REGISTERS) {
12058 rinfo.reg = REG_UNSET;
12061 /* Now see if it is time to do the pre_copy */
12062 if (rinfo.reg != REG_UNSET) {
12063 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
12064 ((regcm & rinfo.regcm) == 0) ||
12065 /* Don't let a mandatory coalesce sneak
12066 * into a operation that is marked to prevent
12069 ((reg != REG_UNNEEDED) &&
12070 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
12071 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
12074 struct triple *user;
12075 user = entry->member;
12076 if (RHS(user, i) != ins) {
12077 internal_error(state, user, "bad rhs");
12079 tmp = pre_copy(state, user, i);
12080 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
12088 if ((regcm & rinfo.regcm) == 0) {
12090 struct triple *user;
12091 user = entry->member;
12092 if (RHS(user, i) != ins) {
12093 internal_error(state, user, "bad rhs");
12095 tmp = pre_copy(state, user, i);
12096 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
12102 regcm &= rinfo.regcm;
12105 if (do_post_copy) {
12106 struct reg_info pre, post;
12107 tmp = post_copy(state, ins);
12108 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
12109 pre = arch_reg_lhs(state, ins, 0);
12110 post = arch_reg_lhs(state, tmp, 0);
12111 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
12112 internal_error(state, tmp, "useless copy");
12117 } while(ins != first);
12121 struct live_range_edge;
12122 struct live_range_def;
12123 struct live_range {
12124 struct live_range_edge *edges;
12125 struct live_range_def *defs;
12126 /* Note. The list pointed to by defs is kept in order.
12127 * That is baring splits in the flow control
12128 * defs dominates defs->next wich dominates defs->next->next
12135 struct live_range *group_next, **group_prev;
12138 struct live_range_edge {
12139 struct live_range_edge *next;
12140 struct live_range *node;
12143 struct live_range_def {
12144 struct live_range_def *next;
12145 struct live_range_def *prev;
12146 struct live_range *lr;
12147 struct triple *def;
12151 #define LRE_HASH_SIZE 2048
12153 struct lre_hash *next;
12154 struct live_range *left;
12155 struct live_range *right;
12160 struct lre_hash *hash[LRE_HASH_SIZE];
12161 struct reg_block *blocks;
12162 struct live_range_def *lrd;
12163 struct live_range *lr;
12164 struct live_range *low, **low_tail;
12165 struct live_range *high, **high_tail;
12168 int passes, max_passes;
12169 #define MAX_ALLOCATION_PASSES 100
12174 struct print_interference_block_info {
12175 struct reg_state *rstate;
12179 static void print_interference_block(
12180 struct compile_state *state, struct block *block, void *arg)
12183 struct print_interference_block_info *info = arg;
12184 struct reg_state *rstate = info->rstate;
12185 FILE *fp = info->fp;
12186 struct reg_block *rb;
12187 struct triple *ptr;
12190 rb = &rstate->blocks[block->vertex];
12192 fprintf(fp, "\nblock: %p (%d), %p<-%p %p<-%p\n",
12196 block->left && block->left->use?block->left->use->member : 0,
12198 block->right && block->right->use?block->right->use->member : 0);
12200 struct triple_reg_set *in_set;
12201 fprintf(fp, " in:");
12202 for(in_set = rb->in; in_set; in_set = in_set->next) {
12203 fprintf(fp, " %-10p", in_set->member);
12208 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
12209 done = (ptr == block->last);
12210 if (ptr->op == OP_PHI) {
12217 for(edge = 0; edge < block->users; edge++) {
12218 fprintf(fp, " in(%d):", edge);
12219 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
12220 struct triple **slot;
12221 done = (ptr == block->last);
12222 if (ptr->op != OP_PHI) {
12225 slot = &RHS(ptr, 0);
12226 fprintf(fp, " %-10p", slot[edge]);
12231 if (block->first->op == OP_LABEL) {
12232 fprintf(fp, "%p:\n", block->first);
12234 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
12235 struct live_range *lr;
12239 done = (ptr == block->last);
12240 lr = rstate->lrd[ptr->id].lr;
12243 ptr->id = rstate->lrd[id].orig_id;
12244 SET_REG(ptr->id, lr->color);
12245 display_triple(fp, ptr);
12248 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
12249 internal_error(state, ptr, "lr has no defs!");
12251 if (info->need_edges) {
12253 struct live_range_def *lrd;
12254 fprintf(fp, " range:");
12257 fprintf(fp, " %-10p", lrd->def);
12259 } while(lrd != lr->defs);
12262 if (lr->edges > 0) {
12263 struct live_range_edge *edge;
12264 fprintf(fp, " edges:");
12265 for(edge = lr->edges; edge; edge = edge->next) {
12266 struct live_range_def *lrd;
12267 lrd = edge->node->defs;
12269 fprintf(fp, " %-10p", lrd->def);
12271 } while(lrd != edge->node->defs);
12277 /* Do a bunch of sanity checks */
12278 valid_ins(state, ptr);
12279 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
12280 internal_error(state, ptr, "Invalid triple id: %d",
12285 struct triple_reg_set *out_set;
12286 fprintf(fp, " out:");
12287 for(out_set = rb->out; out_set; out_set = out_set->next) {
12288 fprintf(fp, " %-10p", out_set->member);
12295 static void print_interference_blocks(
12296 struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
12298 struct print_interference_block_info info;
12299 info.rstate = rstate;
12301 info.need_edges = need_edges;
12302 fprintf(fp, "\nlive variables by block\n");
12303 walk_blocks(state, print_interference_block, &info);
12307 static unsigned regc_max_size(struct compile_state *state, int classes)
12312 for(i = 0; i < MAX_REGC; i++) {
12313 if (classes & (1 << i)) {
12315 size = arch_regc_size(state, i);
12316 if (size > max_size) {
12324 static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
12326 unsigned equivs[MAX_REG_EQUIVS];
12328 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
12329 internal_error(state, 0, "invalid register");
12331 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
12332 internal_error(state, 0, "invalid register");
12334 arch_reg_equivs(state, equivs, reg1);
12335 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
12336 if (equivs[i] == reg2) {
12343 static void reg_fill_used(struct compile_state *state, char *used, int reg)
12345 unsigned equivs[MAX_REG_EQUIVS];
12347 if (reg == REG_UNNEEDED) {
12350 arch_reg_equivs(state, equivs, reg);
12351 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
12352 used[equivs[i]] = 1;
12357 static void reg_inc_used(struct compile_state *state, char *used, int reg)
12359 unsigned equivs[MAX_REG_EQUIVS];
12361 if (reg == REG_UNNEEDED) {
12364 arch_reg_equivs(state, equivs, reg);
12365 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
12366 used[equivs[i]] += 1;
12371 static unsigned int hash_live_edge(
12372 struct live_range *left, struct live_range *right)
12374 unsigned int hash, val;
12375 unsigned long lval, rval;
12376 lval = ((unsigned long)left)/sizeof(struct live_range);
12377 rval = ((unsigned long)right)/sizeof(struct live_range);
12382 hash = (hash *263) + val;
12387 hash = (hash *263) + val;
12389 hash = hash & (LRE_HASH_SIZE - 1);
12393 static struct lre_hash **lre_probe(struct reg_state *rstate,
12394 struct live_range *left, struct live_range *right)
12396 struct lre_hash **ptr;
12397 unsigned int index;
12398 /* Ensure left <= right */
12399 if (left > right) {
12400 struct live_range *tmp;
12405 index = hash_live_edge(left, right);
12407 ptr = &rstate->hash[index];
12409 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
12412 ptr = &(*ptr)->next;
12417 static int interfere(struct reg_state *rstate,
12418 struct live_range *left, struct live_range *right)
12420 struct lre_hash **ptr;
12421 ptr = lre_probe(rstate, left, right);
12422 return ptr && *ptr;
12425 static void add_live_edge(struct reg_state *rstate,
12426 struct live_range *left, struct live_range *right)
12428 /* FIXME the memory allocation overhead is noticeable here... */
12429 struct lre_hash **ptr, *new_hash;
12430 struct live_range_edge *edge;
12432 if (left == right) {
12435 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
12438 /* Ensure left <= right */
12439 if (left > right) {
12440 struct live_range *tmp;
12445 ptr = lre_probe(rstate, left, right);
12450 fprintf(stderr, "new_live_edge(%p, %p)\n",
12453 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
12454 new_hash->next = *ptr;
12455 new_hash->left = left;
12456 new_hash->right = right;
12459 edge = xmalloc(sizeof(*edge), "live_range_edge");
12460 edge->next = left->edges;
12461 edge->node = right;
12462 left->edges = edge;
12465 edge = xmalloc(sizeof(*edge), "live_range_edge");
12466 edge->next = right->edges;
12468 right->edges = edge;
12469 right->degree += 1;
12472 static void remove_live_edge(struct reg_state *rstate,
12473 struct live_range *left, struct live_range *right)
12475 struct live_range_edge *edge, **ptr;
12476 struct lre_hash **hptr, *entry;
12477 hptr = lre_probe(rstate, left, right);
12478 if (!hptr || !*hptr) {
12482 *hptr = entry->next;
12485 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
12487 if (edge->node == right) {
12489 memset(edge, 0, sizeof(*edge));
12495 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
12497 if (edge->node == left) {
12499 memset(edge, 0, sizeof(*edge));
12507 static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
12509 struct live_range_edge *edge, *next;
12510 for(edge = range->edges; edge; edge = next) {
12512 remove_live_edge(rstate, range, edge->node);
12516 static void transfer_live_edges(struct reg_state *rstate,
12517 struct live_range *dest, struct live_range *src)
12519 struct live_range_edge *edge, *next;
12520 for(edge = src->edges; edge; edge = next) {
12521 struct live_range *other;
12523 other = edge->node;
12524 remove_live_edge(rstate, src, other);
12525 add_live_edge(rstate, dest, other);
12530 /* Interference graph...
12532 * new(n) --- Return a graph with n nodes but no edges.
12533 * add(g,x,y) --- Return a graph including g with an between x and y
12534 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
12535 * x and y in the graph g
12536 * degree(g, x) --- Return the degree of the node x in the graph g
12537 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
12539 * Implement with a hash table && a set of adjcency vectors.
12540 * The hash table supports constant time implementations of add and interfere.
12541 * The adjacency vectors support an efficient implementation of neighbors.
12545 * +---------------------------------------------------+
12546 * | +--------------+ |
12548 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
12550 * -- In simplify implment optimistic coloring... (No backtracking)
12551 * -- Implement Rematerialization it is the only form of spilling we can perform
12552 * Essentially this means dropping a constant from a register because
12553 * we can regenerate it later.
12555 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
12556 * coalesce at phi points...
12557 * --- Bias coloring if at all possible do the coalesing a compile time.
12562 static void different_colored(
12563 struct compile_state *state, struct reg_state *rstate,
12564 struct triple *parent, struct triple *ins)
12566 struct live_range *lr;
12567 struct triple **expr;
12568 lr = rstate->lrd[ins->id].lr;
12569 expr = triple_rhs(state, ins, 0);
12570 for(;expr; expr = triple_rhs(state, ins, expr)) {
12571 struct live_range *lr2;
12572 if (!*expr || (*expr == parent) || (*expr == ins)) {
12575 lr2 = rstate->lrd[(*expr)->id].lr;
12576 if (lr->color == lr2->color) {
12577 internal_error(state, ins, "live range too big");
12583 static struct live_range *coalesce_ranges(
12584 struct compile_state *state, struct reg_state *rstate,
12585 struct live_range *lr1, struct live_range *lr2)
12587 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
12593 if (!lr1->defs || !lr2->defs) {
12594 internal_error(state, 0,
12595 "cannot coalese dead live ranges");
12597 if ((lr1->color == REG_UNNEEDED) ||
12598 (lr2->color == REG_UNNEEDED)) {
12599 internal_error(state, 0,
12600 "cannot coalesce live ranges without a possible color");
12602 if ((lr1->color != lr2->color) &&
12603 (lr1->color != REG_UNSET) &&
12604 (lr2->color != REG_UNSET)) {
12605 internal_error(state, lr1->defs->def,
12606 "cannot coalesce live ranges of different colors");
12608 color = lr1->color;
12609 if (color == REG_UNSET) {
12610 color = lr2->color;
12612 classes = lr1->classes & lr2->classes;
12614 internal_error(state, lr1->defs->def,
12615 "cannot coalesce live ranges with dissimilar register classes");
12617 #if DEBUG_COALESCING
12618 fprintf(stderr, "coalescing:");
12621 fprintf(stderr, " %p", lrd->def);
12623 } while(lrd != lr1->defs);
12624 fprintf(stderr, " |");
12627 fprintf(stderr, " %p", lrd->def);
12629 } while(lrd != lr2->defs);
12630 fprintf(stderr, "\n");
12632 /* If there is a clear dominate live range put it in lr1,
12633 * For purposes of this test phi functions are
12634 * considered dominated by the definitions that feed into
12637 if ((lr1->defs->prev->def->op == OP_PHI) ||
12638 ((lr2->defs->prev->def->op != OP_PHI) &&
12639 tdominates(state, lr2->defs->def, lr1->defs->def))) {
12640 struct live_range *tmp;
12646 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
12647 fprintf(stderr, "lr1 post\n");
12649 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
12650 fprintf(stderr, "lr1 pre\n");
12652 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
12653 fprintf(stderr, "lr2 post\n");
12655 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
12656 fprintf(stderr, "lr2 pre\n");
12660 fprintf(stderr, "coalesce color1(%p): %3d color2(%p) %3d\n",
12667 /* Append lr2 onto lr1 */
12668 #warning "FIXME should this be a merge instead of a splice?"
12669 /* This FIXME item applies to the correctness of live_range_end
12670 * and to the necessity of making multiple passes of coalesce_live_ranges.
12671 * A failure to find some coalesce opportunities in coaleace_live_ranges
12672 * does not impact the correct of the compiler just the efficiency with
12673 * which registers are allocated.
12676 mid1 = lr1->defs->prev;
12678 end = lr2->defs->prev;
12686 /* Fixup the live range in the added live range defs */
12691 } while(lrd != head);
12693 /* Mark lr2 as free. */
12695 lr2->color = REG_UNNEEDED;
12699 internal_error(state, 0, "lr1->defs == 0 ?");
12702 lr1->color = color;
12703 lr1->classes = classes;
12705 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
12706 transfer_live_edges(rstate, lr1, lr2);
12711 static struct live_range_def *live_range_head(
12712 struct compile_state *state, struct live_range *lr,
12713 struct live_range_def *last)
12715 struct live_range_def *result;
12720 else if (!tdominates(state, lr->defs->def, last->next->def)) {
12721 result = last->next;
12726 static struct live_range_def *live_range_end(
12727 struct compile_state *state, struct live_range *lr,
12728 struct live_range_def *last)
12730 struct live_range_def *result;
12733 result = lr->defs->prev;
12735 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
12736 result = last->prev;
12742 static void initialize_live_ranges(
12743 struct compile_state *state, struct reg_state *rstate)
12745 struct triple *ins, *first;
12746 size_t count, size;
12749 first = RHS(state->main_function, 0);
12750 /* First count how many instructions I have.
12752 count = count_triples(state);
12753 /* Potentially I need one live range definitions for each
12756 rstate->defs = count;
12757 /* Potentially I need one live range for each instruction
12758 * plus an extra for the dummy live range.
12760 rstate->ranges = count + 1;
12761 size = sizeof(rstate->lrd[0]) * rstate->defs;
12762 rstate->lrd = xcmalloc(size, "live_range_def");
12763 size = sizeof(rstate->lr[0]) * rstate->ranges;
12764 rstate->lr = xcmalloc(size, "live_range");
12766 /* Setup the dummy live range */
12767 rstate->lr[0].classes = 0;
12768 rstate->lr[0].color = REG_UNSET;
12769 rstate->lr[0].defs = 0;
12773 /* If the triple is a variable give it a live range */
12774 if (triple_is_def(state, ins)) {
12775 struct reg_info info;
12776 /* Find the architecture specific color information */
12777 info = find_def_color(state, ins);
12779 rstate->lr[i].defs = &rstate->lrd[j];
12780 rstate->lr[i].color = info.reg;
12781 rstate->lr[i].classes = info.regcm;
12782 rstate->lr[i].degree = 0;
12783 rstate->lrd[j].lr = &rstate->lr[i];
12785 /* Otherwise give the triple the dummy live range. */
12787 rstate->lrd[j].lr = &rstate->lr[0];
12790 /* Initalize the live_range_def */
12791 rstate->lrd[j].next = &rstate->lrd[j];
12792 rstate->lrd[j].prev = &rstate->lrd[j];
12793 rstate->lrd[j].def = ins;
12794 rstate->lrd[j].orig_id = ins->id;
12799 } while(ins != first);
12800 rstate->ranges = i;
12802 /* Make a second pass to handle achitecture specific register
12807 int zlhs, zrhs, i, j;
12808 if (ins->id > rstate->defs) {
12809 internal_error(state, ins, "bad id");
12812 /* Walk through the template of ins and coalesce live ranges */
12813 zlhs = TRIPLE_LHS(ins->sizes);
12814 if ((zlhs == 0) && triple_is_def(state, ins)) {
12817 zrhs = TRIPLE_RHS(ins->sizes);
12819 #if DEBUG_COALESCING > 1
12820 fprintf(stderr, "mandatory coalesce: %p %d %d\n",
12823 for(i = 0; i < zlhs; i++) {
12824 struct reg_info linfo;
12825 struct live_range_def *lhs;
12826 linfo = arch_reg_lhs(state, ins, i);
12827 if (linfo.reg < MAX_REGISTERS) {
12830 if (triple_is_def(state, ins)) {
12831 lhs = &rstate->lrd[ins->id];
12833 lhs = &rstate->lrd[LHS(ins, i)->id];
12835 #if DEBUG_COALESCING > 1
12836 fprintf(stderr, "coalesce lhs(%d): %p %d\n",
12837 i, lhs, linfo.reg);
12840 for(j = 0; j < zrhs; j++) {
12841 struct reg_info rinfo;
12842 struct live_range_def *rhs;
12843 rinfo = arch_reg_rhs(state, ins, j);
12844 if (rinfo.reg < MAX_REGISTERS) {
12847 rhs = &rstate->lrd[RHS(ins, j)->id];
12848 #if DEBUG_COALESCING > 1
12849 fprintf(stderr, "coalesce rhs(%d): %p %d\n",
12850 j, rhs, rinfo.reg);
12853 if (rinfo.reg == linfo.reg) {
12854 coalesce_ranges(state, rstate,
12860 } while(ins != first);
12863 static void graph_ins(
12864 struct compile_state *state,
12865 struct reg_block *blocks, struct triple_reg_set *live,
12866 struct reg_block *rb, struct triple *ins, void *arg)
12868 struct reg_state *rstate = arg;
12869 struct live_range *def;
12870 struct triple_reg_set *entry;
12872 /* If the triple is not a definition
12873 * we do not have a definition to add to
12874 * the interference graph.
12876 if (!triple_is_def(state, ins)) {
12879 def = rstate->lrd[ins->id].lr;
12881 /* Create an edge between ins and everything that is
12882 * alive, unless the live_range cannot share
12883 * a physical register with ins.
12885 for(entry = live; entry; entry = entry->next) {
12886 struct live_range *lr;
12887 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
12888 internal_error(state, 0, "bad entry?");
12890 lr = rstate->lrd[entry->member->id].lr;
12894 if (!arch_regcm_intersect(def->classes, lr->classes)) {
12897 add_live_edge(rstate, def, lr);
12902 static struct live_range *get_verify_live_range(
12903 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
12905 struct live_range *lr;
12906 struct live_range_def *lrd;
12908 if ((ins->id < 0) || (ins->id > rstate->defs)) {
12909 internal_error(state, ins, "bad ins?");
12911 lr = rstate->lrd[ins->id].lr;
12915 if (lrd->def == ins) {
12919 } while(lrd != lr->defs);
12921 internal_error(state, ins, "ins not in live range");
12926 static void verify_graph_ins(
12927 struct compile_state *state,
12928 struct reg_block *blocks, struct triple_reg_set *live,
12929 struct reg_block *rb, struct triple *ins, void *arg)
12931 struct reg_state *rstate = arg;
12932 struct triple_reg_set *entry1, *entry2;
12935 /* Compare live against edges and make certain the code is working */
12936 for(entry1 = live; entry1; entry1 = entry1->next) {
12937 struct live_range *lr1;
12938 lr1 = get_verify_live_range(state, rstate, entry1->member);
12939 for(entry2 = live; entry2; entry2 = entry2->next) {
12940 struct live_range *lr2;
12941 struct live_range_edge *edge2;
12944 if (entry2 == entry1) {
12947 lr2 = get_verify_live_range(state, rstate, entry2->member);
12949 internal_error(state, entry2->member,
12950 "live range with 2 values simultaneously alive");
12952 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
12955 if (!interfere(rstate, lr1, lr2)) {
12956 internal_error(state, entry2->member,
12957 "edges don't interfere?");
12962 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
12964 if (edge2->node == lr1) {
12968 if (lr2_degree != lr2->degree) {
12969 internal_error(state, entry2->member,
12970 "computed degree: %d does not match reported degree: %d\n",
12971 lr2_degree, lr2->degree);
12974 internal_error(state, entry2->member, "missing edge");
12981 #if DEBUG_CONSISTENCY > 1
12982 static void verify_interference_graph(
12983 struct compile_state *state, struct reg_state *rstate)
12986 fprintf(stderr, "verify_interference_graph...\n");
12989 walk_variable_lifetimes(state, rstate->blocks, verify_graph_ins, rstate);
12991 fprintf(stderr, "verify_interference_graph done\n");
12995 static inline void verify_interference_graph(
12996 struct compile_state *state, struct reg_state *rstate) {}
12999 static void print_interference_ins(
13000 struct compile_state *state,
13001 struct reg_block *blocks, struct triple_reg_set *live,
13002 struct reg_block *rb, struct triple *ins, void *arg)
13004 struct reg_state *rstate = arg;
13005 struct live_range *lr;
13008 lr = rstate->lrd[ins->id].lr;
13010 ins->id = rstate->lrd[id].orig_id;
13011 SET_REG(ins->id, lr->color);
13012 display_triple(stdout, ins);
13016 struct live_range_def *lrd;
13020 printf(" %-10p", lrd->def);
13022 } while(lrd != lr->defs);
13026 struct triple_reg_set *entry;
13028 for(entry = live; entry; entry = entry->next) {
13029 printf(" %-10p", entry->member);
13034 struct live_range_edge *entry;
13036 for(entry = lr->edges; entry; entry = entry->next) {
13037 struct live_range_def *lrd;
13038 lrd = entry->node->defs;
13040 printf(" %-10p", lrd->def);
13042 } while(lrd != entry->node->defs);
13047 if (triple_is_branch(state, ins)) {
13053 static int coalesce_live_ranges(
13054 struct compile_state *state, struct reg_state *rstate)
13056 /* At the point where a value is moved from one
13057 * register to another that value requires two
13058 * registers, thus increasing register pressure.
13059 * Live range coaleescing reduces the register
13060 * pressure by keeping a value in one register
13063 * In the case of a phi function all paths leading
13064 * into it must be allocated to the same register
13065 * otherwise the phi function may not be removed.
13067 * Forcing a value to stay in a single register
13068 * for an extended period of time does have
13069 * limitations when applied to non homogenous
13072 * The two cases I have identified are:
13073 * 1) Two forced register assignments may
13075 * 2) Registers may go unused because they
13076 * are only good for storing the value
13077 * and not manipulating it.
13079 * Because of this I need to split live ranges,
13080 * even outside of the context of coalesced live
13081 * ranges. The need to split live ranges does
13082 * impose some constraints on live range coalescing.
13084 * - Live ranges may not be coalesced across phi
13085 * functions. This creates a 2 headed live
13086 * range that cannot be sanely split.
13088 * - phi functions (coalesced in initialize_live_ranges)
13089 * are handled as pre split live ranges so we will
13090 * never attempt to split them.
13096 for(i = 0; i <= rstate->ranges; i++) {
13097 struct live_range *lr1;
13098 struct live_range_def *lrd1;
13099 lr1 = &rstate->lr[i];
13103 lrd1 = live_range_end(state, lr1, 0);
13104 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
13105 struct triple_set *set;
13106 if (lrd1->def->op != OP_COPY) {
13109 /* Skip copies that are the result of a live range split. */
13110 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
13113 for(set = lrd1->def->use; set; set = set->next) {
13114 struct live_range_def *lrd2;
13115 struct live_range *lr2, *res;
13117 lrd2 = &rstate->lrd[set->member->id];
13119 /* Don't coalesce with instructions
13120 * that are the result of a live range
13123 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
13126 lr2 = rstate->lrd[set->member->id].lr;
13130 if ((lr1->color != lr2->color) &&
13131 (lr1->color != REG_UNSET) &&
13132 (lr2->color != REG_UNSET)) {
13135 if ((lr1->classes & lr2->classes) == 0) {
13139 if (interfere(rstate, lr1, lr2)) {
13143 res = coalesce_ranges(state, rstate, lr1, lr2);
13157 static void fix_coalesce_conflicts(struct compile_state *state,
13158 struct reg_block *blocks, struct triple_reg_set *live,
13159 struct reg_block *rb, struct triple *ins, void *arg)
13161 int *conflicts = arg;
13162 int zlhs, zrhs, i, j;
13164 /* See if we have a mandatory coalesce operation between
13165 * a lhs and a rhs value. If so and the rhs value is also
13166 * alive then this triple needs to be pre copied. Otherwise
13167 * we would have two definitions in the same live range simultaneously
13170 zlhs = TRIPLE_LHS(ins->sizes);
13171 if ((zlhs == 0) && triple_is_def(state, ins)) {
13174 zrhs = TRIPLE_RHS(ins->sizes);
13175 for(i = 0; i < zlhs; i++) {
13176 struct reg_info linfo;
13177 linfo = arch_reg_lhs(state, ins, i);
13178 if (linfo.reg < MAX_REGISTERS) {
13181 for(j = 0; j < zrhs; j++) {
13182 struct reg_info rinfo;
13183 struct triple *rhs;
13184 struct triple_reg_set *set;
13187 rinfo = arch_reg_rhs(state, ins, j);
13188 if (rinfo.reg != linfo.reg) {
13192 for(set = live; set && !found; set = set->next) {
13193 if (set->member == rhs) {
13198 struct triple *copy;
13199 copy = pre_copy(state, ins, j);
13200 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
13208 static int correct_coalesce_conflicts(
13209 struct compile_state *state, struct reg_block *blocks)
13213 walk_variable_lifetimes(state, blocks, fix_coalesce_conflicts, &conflicts);
13217 static void replace_set_use(struct compile_state *state,
13218 struct triple_reg_set *head, struct triple *orig, struct triple *new)
13220 struct triple_reg_set *set;
13221 for(set = head; set; set = set->next) {
13222 if (set->member == orig) {
13228 static void replace_block_use(struct compile_state *state,
13229 struct reg_block *blocks, struct triple *orig, struct triple *new)
13232 #warning "WISHLIST visit just those blocks that need it *"
13233 for(i = 1; i <= state->last_vertex; i++) {
13234 struct reg_block *rb;
13236 replace_set_use(state, rb->in, orig, new);
13237 replace_set_use(state, rb->out, orig, new);
13241 static void color_instructions(struct compile_state *state)
13243 struct triple *ins, *first;
13244 first = RHS(state->main_function, 0);
13247 if (triple_is_def(state, ins)) {
13248 struct reg_info info;
13249 info = find_lhs_color(state, ins, 0);
13250 if (info.reg >= MAX_REGISTERS) {
13251 info.reg = REG_UNSET;
13253 SET_INFO(ins->id, info);
13256 } while(ins != first);
13259 static struct reg_info read_lhs_color(
13260 struct compile_state *state, struct triple *ins, int index)
13262 struct reg_info info;
13263 if ((index == 0) && triple_is_def(state, ins)) {
13264 info.reg = ID_REG(ins->id);
13265 info.regcm = ID_REGCM(ins->id);
13267 else if (index < TRIPLE_LHS(ins->sizes)) {
13268 info = read_lhs_color(state, LHS(ins, index), 0);
13271 internal_error(state, ins, "Bad lhs %d", index);
13272 info.reg = REG_UNSET;
13278 static struct triple *resolve_tangle(
13279 struct compile_state *state, struct triple *tangle)
13281 struct reg_info info, uinfo;
13282 struct triple_set *set, *next;
13283 struct triple *copy;
13285 #warning "WISHLIST recalculate all affected instructions colors"
13286 info = find_lhs_color(state, tangle, 0);
13287 for(set = tangle->use; set; set = next) {
13288 struct triple *user;
13291 user = set->member;
13292 zrhs = TRIPLE_RHS(user->sizes);
13293 for(i = 0; i < zrhs; i++) {
13294 if (RHS(user, i) != tangle) {
13297 uinfo = find_rhs_post_color(state, user, i);
13298 if (uinfo.reg == info.reg) {
13299 copy = pre_copy(state, user, i);
13300 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
13301 SET_INFO(copy->id, uinfo);
13306 uinfo = find_lhs_pre_color(state, tangle, 0);
13307 if (uinfo.reg == info.reg) {
13308 struct reg_info linfo;
13309 copy = post_copy(state, tangle);
13310 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
13311 linfo = find_lhs_color(state, copy, 0);
13312 SET_INFO(copy->id, linfo);
13314 info = find_lhs_color(state, tangle, 0);
13315 SET_INFO(tangle->id, info);
13321 static void fix_tangles(struct compile_state *state,
13322 struct reg_block *blocks, struct triple_reg_set *live,
13323 struct reg_block *rb, struct triple *ins, void *arg)
13325 int *tangles = arg;
13326 struct triple *tangle;
13328 char used[MAX_REGISTERS];
13329 struct triple_reg_set *set;
13332 /* Find out which registers have multiple uses at this point */
13333 memset(used, 0, sizeof(used));
13334 for(set = live; set; set = set->next) {
13335 struct reg_info info;
13336 info = read_lhs_color(state, set->member, 0);
13337 if (info.reg == REG_UNSET) {
13340 reg_inc_used(state, used, info.reg);
13343 /* Now find the least dominated definition of a register in
13344 * conflict I have seen so far.
13346 for(set = live; set; set = set->next) {
13347 struct reg_info info;
13348 info = read_lhs_color(state, set->member, 0);
13349 if (used[info.reg] < 2) {
13352 /* Changing copies that feed into phi functions
13355 if (set->member->use &&
13356 (set->member->use->member->op == OP_PHI)) {
13359 if (!tangle || tdominates(state, set->member, tangle)) {
13360 tangle = set->member;
13363 /* If I have found a tangle resolve it */
13365 struct triple *post_copy;
13367 post_copy = resolve_tangle(state, tangle);
13369 replace_block_use(state, blocks, tangle, post_copy);
13371 if (post_copy && (tangle != ins)) {
13372 replace_set_use(state, live, tangle, post_copy);
13379 static int correct_tangles(
13380 struct compile_state *state, struct reg_block *blocks)
13384 color_instructions(state);
13385 walk_variable_lifetimes(state, blocks, fix_tangles, &tangles);
13390 static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
13391 static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
13393 struct triple *find_constrained_def(
13394 struct compile_state *state, struct live_range *range, struct triple *constrained)
13396 struct live_range_def *lrd;
13399 struct reg_info info;
13401 int is_constrained;
13402 regcm = arch_type_to_regcm(state, lrd->def->type);
13403 info = find_lhs_color(state, lrd->def, 0);
13404 regcm = arch_regcm_reg_normalize(state, regcm);
13405 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
13406 /* If the 2 register class masks are not equal the
13407 * the current register class is constrained.
13409 is_constrained = regcm != info.regcm;
13411 /* Of the constrained live ranges deal with the
13412 * least dominated one first.
13414 if (is_constrained) {
13415 #if DEBUG_RANGE_CONFLICTS
13416 fprintf(stderr, "canidate: %p %-8s regcm: %x %x\n",
13417 lrd->def, tops(lrd->def->op), regcm, info.regcm);
13419 if (!constrained ||
13420 tdominates(state, lrd->def, constrained))
13422 constrained = lrd->def;
13426 } while(lrd != range->defs);
13427 return constrained;
13430 static int split_constrained_ranges(
13431 struct compile_state *state, struct reg_state *rstate,
13432 struct live_range *range)
13434 /* Walk through the edges in conflict and our current live
13435 * range, and find definitions that are more severly constrained
13436 * than they type of data they contain require.
13438 * Then pick one of those ranges and relax the constraints.
13440 struct live_range_edge *edge;
13441 struct triple *constrained;
13444 for(edge = range->edges; edge; edge = edge->next) {
13445 constrained = find_constrained_def(state, edge->node, constrained);
13447 if (!constrained) {
13448 constrained = find_constrained_def(state, range, constrained);
13450 #if DEBUG_RANGE_CONFLICTS
13451 fprintf(stderr, "constrained: %p %-8s\n",
13452 constrained, tops(constrained->op));
13455 ids_from_rstate(state, rstate);
13456 cleanup_rstate(state, rstate);
13457 resolve_tangle(state, constrained);
13459 return !!constrained;
13462 static int split_ranges(
13463 struct compile_state *state, struct reg_state *rstate,
13464 char *used, struct live_range *range)
13467 #if DEBUG_RANGE_CONFLICTS
13468 fprintf(stderr, "split_ranges %d %s %p\n",
13469 rstate->passes, tops(range->defs->def->op), range->defs->def);
13471 if ((range->color == REG_UNNEEDED) ||
13472 (rstate->passes >= rstate->max_passes)) {
13475 split = split_constrained_ranges(state, rstate, range);
13477 /* Ideally I would split the live range that will not be used
13478 * for the longest period of time in hopes that this will
13479 * (a) allow me to spill a register or
13480 * (b) allow me to place a value in another register.
13482 * So far I don't have a test case for this, the resolving
13483 * of mandatory constraints has solved all of my
13484 * know issues. So I have choosen not to write any
13485 * code until I cat get a better feel for cases where
13486 * it would be useful to have.
13489 #warning "WISHLIST implement live range splitting..."
13490 if ((DEBUG_RANGE_CONFLICTS > 1) &&
13491 (!split || (DEBUG_RANGE_CONFLICTS > 2))) {
13492 print_interference_blocks(state, rstate, stderr, 0);
13493 print_dominators(state, stderr);
13498 #if DEBUG_COLOR_GRAPH > 1
13499 #define cgdebug_printf(...) fprintf(stdout, __VA_ARGS__)
13500 #define cgdebug_flush() fflush(stdout)
13501 #define cgdebug_loc(STATE, TRIPLE) loc(stdout, STATE, TRIPLE)
13502 #elif DEBUG_COLOR_GRAPH == 1
13503 #define cgdebug_printf(...) fprintf(stderr, __VA_ARGS__)
13504 #define cgdebug_flush() fflush(stderr)
13505 #define cgdebug_loc(STATE, TRIPLE) loc(stderr, STATE, TRIPLE)
13507 #define cgdebug_printf(...)
13508 #define cgdebug_flush()
13509 #define cgdebug_loc(STATE, TRIPLE)
13513 static int select_free_color(struct compile_state *state,
13514 struct reg_state *rstate, struct live_range *range)
13516 struct triple_set *entry;
13517 struct live_range_def *lrd;
13518 struct live_range_def *phi;
13519 struct live_range_edge *edge;
13520 char used[MAX_REGISTERS];
13521 struct triple **expr;
13523 /* Instead of doing just the trivial color select here I try
13524 * a few extra things because a good color selection will help reduce
13528 /* Find the registers currently in use */
13529 memset(used, 0, sizeof(used));
13530 for(edge = range->edges; edge; edge = edge->next) {
13531 if (edge->node->color == REG_UNSET) {
13534 reg_fill_used(state, used, edge->node->color);
13536 #if DEBUG_COLOR_GRAPH > 1
13540 for(edge = range->edges; edge; edge = edge->next) {
13543 cgdebug_printf("\n%s edges: %d @%s:%d.%d\n",
13544 tops(range->def->op), i,
13545 range->def->filename, range->def->line, range->def->col);
13546 for(i = 0; i < MAX_REGISTERS; i++) {
13548 cgdebug_printf("used: %s\n",
13555 /* If a color is already assigned see if it will work */
13556 if (range->color != REG_UNSET) {
13557 struct live_range_def *lrd;
13558 if (!used[range->color]) {
13561 for(edge = range->edges; edge; edge = edge->next) {
13562 if (edge->node->color != range->color) {
13565 warning(state, edge->node->defs->def, "edge: ");
13566 lrd = edge->node->defs;
13568 warning(state, lrd->def, " %p %s",
13569 lrd->def, tops(lrd->def->op));
13571 } while(lrd != edge->node->defs);
13574 warning(state, range->defs->def, "def: ");
13576 warning(state, lrd->def, " %p %s",
13577 lrd->def, tops(lrd->def->op));
13579 } while(lrd != range->defs);
13580 internal_error(state, range->defs->def,
13581 "live range with already used color %s",
13582 arch_reg_str(range->color));
13585 /* If I feed into an expression reuse it's color.
13586 * This should help remove copies in the case of 2 register instructions
13587 * and phi functions.
13590 lrd = live_range_end(state, range, 0);
13591 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
13592 entry = lrd->def->use;
13593 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
13594 struct live_range_def *insd;
13596 insd = &rstate->lrd[entry->member->id];
13597 if (insd->lr->defs == 0) {
13600 if (!phi && (insd->def->op == OP_PHI) &&
13601 !interfere(rstate, range, insd->lr)) {
13604 if (insd->lr->color == REG_UNSET) {
13607 regcm = insd->lr->classes;
13608 if (((regcm & range->classes) == 0) ||
13609 (used[insd->lr->color])) {
13612 if (interfere(rstate, range, insd->lr)) {
13615 range->color = insd->lr->color;
13618 /* If I feed into a phi function reuse it's color or the color
13619 * of something else that feeds into the phi function.
13622 if (phi->lr->color != REG_UNSET) {
13623 if (used[phi->lr->color]) {
13624 range->color = phi->lr->color;
13628 expr = triple_rhs(state, phi->def, 0);
13629 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
13630 struct live_range *lr;
13635 lr = rstate->lrd[(*expr)->id].lr;
13636 if (lr->color == REG_UNSET) {
13639 regcm = lr->classes;
13640 if (((regcm & range->classes) == 0) ||
13641 (used[lr->color])) {
13644 if (interfere(rstate, range, lr)) {
13647 range->color = lr->color;
13651 /* If I don't interfere with a rhs node reuse it's color */
13652 lrd = live_range_head(state, range, 0);
13653 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
13654 expr = triple_rhs(state, lrd->def, 0);
13655 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
13656 struct live_range *lr;
13661 lr = rstate->lrd[(*expr)->id].lr;
13662 if (lr->color == REG_UNSET) {
13665 regcm = lr->classes;
13666 if (((regcm & range->classes) == 0) ||
13667 (used[lr->color])) {
13670 if (interfere(rstate, range, lr)) {
13673 range->color = lr->color;
13677 /* If I have not opportunitically picked a useful color
13678 * pick the first color that is free.
13680 if (range->color == REG_UNSET) {
13682 arch_select_free_register(state, used, range->classes);
13684 if (range->color == REG_UNSET) {
13685 struct live_range_def *lrd;
13687 if (split_ranges(state, rstate, used, range)) {
13690 for(edge = range->edges; edge; edge = edge->next) {
13691 warning(state, edge->node->defs->def, "edge reg %s",
13692 arch_reg_str(edge->node->color));
13693 lrd = edge->node->defs;
13695 warning(state, lrd->def, " %s %p",
13696 tops(lrd->def->op), lrd->def);
13698 } while(lrd != edge->node->defs);
13700 warning(state, range->defs->def, "range: ");
13703 warning(state, lrd->def, " %s %p",
13704 tops(lrd->def->op), lrd->def);
13706 } while(lrd != range->defs);
13708 warning(state, range->defs->def, "classes: %x",
13710 for(i = 0; i < MAX_REGISTERS; i++) {
13712 warning(state, range->defs->def, "used: %s",
13716 #if DEBUG_COLOR_GRAPH < 2
13717 error(state, range->defs->def, "too few registers");
13719 internal_error(state, range->defs->def, "too few registers");
13722 range->classes &= arch_reg_regcm(state, range->color);
13723 if ((range->color == REG_UNSET) || (range->classes == 0)) {
13724 internal_error(state, range->defs->def, "select_free_color did not?");
13729 static int color_graph(struct compile_state *state, struct reg_state *rstate)
13732 struct live_range_edge *edge;
13733 struct live_range *range;
13735 cgdebug_printf("Lo: ");
13736 range = rstate->low;
13737 if (*range->group_prev != range) {
13738 internal_error(state, 0, "lo: *prev != range?");
13740 *range->group_prev = range->group_next;
13741 if (range->group_next) {
13742 range->group_next->group_prev = range->group_prev;
13744 if (&range->group_next == rstate->low_tail) {
13745 rstate->low_tail = range->group_prev;
13747 if (rstate->low == range) {
13748 internal_error(state, 0, "low: next != prev?");
13751 else if (rstate->high) {
13752 cgdebug_printf("Hi: ");
13753 range = rstate->high;
13754 if (*range->group_prev != range) {
13755 internal_error(state, 0, "hi: *prev != range?");
13757 *range->group_prev = range->group_next;
13758 if (range->group_next) {
13759 range->group_next->group_prev = range->group_prev;
13761 if (&range->group_next == rstate->high_tail) {
13762 rstate->high_tail = range->group_prev;
13764 if (rstate->high == range) {
13765 internal_error(state, 0, "high: next != prev?");
13771 cgdebug_printf(" %d\n", range - rstate->lr);
13772 range->group_prev = 0;
13773 for(edge = range->edges; edge; edge = edge->next) {
13774 struct live_range *node;
13776 /* Move nodes from the high to the low list */
13777 if (node->group_prev && (node->color == REG_UNSET) &&
13778 (node->degree == regc_max_size(state, node->classes))) {
13779 if (*node->group_prev != node) {
13780 internal_error(state, 0, "move: *prev != node?");
13782 *node->group_prev = node->group_next;
13783 if (node->group_next) {
13784 node->group_next->group_prev = node->group_prev;
13786 if (&node->group_next == rstate->high_tail) {
13787 rstate->high_tail = node->group_prev;
13789 cgdebug_printf("Moving...%d to low\n", node - rstate->lr);
13790 node->group_prev = rstate->low_tail;
13791 node->group_next = 0;
13792 *rstate->low_tail = node;
13793 rstate->low_tail = &node->group_next;
13794 if (*node->group_prev != node) {
13795 internal_error(state, 0, "move2: *prev != node?");
13800 colored = color_graph(state, rstate);
13802 cgdebug_printf("Coloring %d @", range - rstate->lr);
13803 cgdebug_loc(state, range->defs->def);
13805 colored = select_free_color(state, rstate, range);
13806 cgdebug_printf(" %s\n", arch_reg_str(range->color));
13811 #if DEBUG_CONSISTENCY
13812 static void verify_colors(struct compile_state *state, struct reg_state *rstate)
13814 struct live_range *lr;
13815 struct live_range_edge *edge;
13816 struct triple *ins, *first;
13817 char used[MAX_REGISTERS];
13818 first = RHS(state->main_function, 0);
13821 if (triple_is_def(state, ins)) {
13822 if ((ins->id < 0) || (ins->id > rstate->defs)) {
13823 internal_error(state, ins,
13824 "triple without a live range def");
13826 lr = rstate->lrd[ins->id].lr;
13827 if (lr->color == REG_UNSET) {
13828 internal_error(state, ins,
13829 "triple without a color");
13831 /* Find the registers used by the edges */
13832 memset(used, 0, sizeof(used));
13833 for(edge = lr->edges; edge; edge = edge->next) {
13834 if (edge->node->color == REG_UNSET) {
13835 internal_error(state, 0,
13836 "live range without a color");
13838 reg_fill_used(state, used, edge->node->color);
13840 if (used[lr->color]) {
13841 internal_error(state, ins,
13842 "triple with already used color");
13846 } while(ins != first);
13849 static inline void verify_colors(struct compile_state *state, struct reg_state *rstate) {}
13852 static void color_triples(struct compile_state *state, struct reg_state *rstate)
13854 struct live_range *lr;
13855 struct triple *first, *ins;
13856 first = RHS(state->main_function, 0);
13859 if ((ins->id < 0) || (ins->id > rstate->defs)) {
13860 internal_error(state, ins,
13861 "triple without a live range");
13863 lr = rstate->lrd[ins->id].lr;
13864 SET_REG(ins->id, lr->color);
13866 } while (ins != first);
13869 static struct live_range *merge_sort_lr(
13870 struct live_range *first, struct live_range *last)
13872 struct live_range *mid, *join, **join_tail, *pick;
13874 size = (last - first) + 1;
13876 mid = first + size/2;
13877 first = merge_sort_lr(first, mid -1);
13878 mid = merge_sort_lr(mid, last);
13882 /* merge the two lists */
13883 while(first && mid) {
13884 if ((first->degree < mid->degree) ||
13885 ((first->degree == mid->degree) &&
13886 (first->length < mid->length))) {
13888 first = first->group_next;
13890 first->group_prev = 0;
13895 mid = mid->group_next;
13897 mid->group_prev = 0;
13900 pick->group_next = 0;
13901 pick->group_prev = join_tail;
13903 join_tail = &pick->group_next;
13905 /* Splice the remaining list */
13906 pick = (first)? first : mid;
13909 pick->group_prev = join_tail;
13913 if (!first->defs) {
13921 static void ids_from_rstate(struct compile_state *state,
13922 struct reg_state *rstate)
13924 struct triple *ins, *first;
13925 if (!rstate->defs) {
13928 /* Display the graph if desired */
13929 if (state->debug & DEBUG_INTERFERENCE) {
13930 print_blocks(state, stdout);
13931 print_control_flow(state);
13933 first = RHS(state->main_function, 0);
13937 struct live_range_def *lrd;
13938 lrd = &rstate->lrd[ins->id];
13939 ins->id = lrd->orig_id;
13942 } while(ins != first);
13945 static void cleanup_live_edges(struct reg_state *rstate)
13948 /* Free the edges on each node */
13949 for(i = 1; i <= rstate->ranges; i++) {
13950 remove_live_edges(rstate, &rstate->lr[i]);
13954 static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
13956 cleanup_live_edges(rstate);
13957 xfree(rstate->lrd);
13960 /* Free the variable lifetime information */
13961 if (rstate->blocks) {
13962 free_variable_lifetimes(state, rstate->blocks);
13965 rstate->ranges = 0;
13968 rstate->blocks = 0;
13971 static void verify_consistency(struct compile_state *state);
13972 static void allocate_registers(struct compile_state *state)
13974 struct reg_state rstate;
13977 /* Clear out the reg_state */
13978 memset(&rstate, 0, sizeof(rstate));
13979 rstate.max_passes = MAX_ALLOCATION_PASSES;
13982 struct live_range **point, **next;
13987 #if DEBUG_RANGE_CONFLICTS
13988 fprintf(stderr, "pass: %d\n", rstate.passes);
13992 ids_from_rstate(state, &rstate);
13994 /* Cleanup the temporary data structures */
13995 cleanup_rstate(state, &rstate);
13997 /* Compute the variable lifetimes */
13998 rstate.blocks = compute_variable_lifetimes(state);
14000 /* Fix invalid mandatory live range coalesce conflicts */
14001 conflicts = correct_coalesce_conflicts(state, rstate.blocks);
14003 /* Fix two simultaneous uses of the same register.
14004 * In a few pathlogical cases a partial untangle moves
14005 * the tangle to a part of the graph we won't revisit.
14006 * So we keep looping until we have no more tangle fixes
14010 tangles = correct_tangles(state, rstate.blocks);
14013 if (state->debug & DEBUG_INSERTED_COPIES) {
14014 printf("After resolve_tangles\n");
14015 print_blocks(state, stdout);
14016 print_control_flow(state);
14018 verify_consistency(state);
14020 /* Allocate and initialize the live ranges */
14021 initialize_live_ranges(state, &rstate);
14023 /* Note current doing coalescing in a loop appears to
14024 * buys me nothing. The code is left this way in case
14025 * there is some value in it. Or if a future bugfix
14026 * yields some benefit.
14029 #if DEBUG_COALESCING
14030 fprintf(stderr, "coalescing\n");
14032 /* Remove any previous live edge calculations */
14033 cleanup_live_edges(&rstate);
14035 /* Compute the interference graph */
14036 walk_variable_lifetimes(
14037 state, rstate.blocks, graph_ins, &rstate);
14039 /* Display the interference graph if desired */
14040 if (state->debug & DEBUG_INTERFERENCE) {
14041 print_interference_blocks(state, &rstate, stdout, 1);
14042 printf("\nlive variables by instruction\n");
14043 walk_variable_lifetimes(
14044 state, rstate.blocks,
14045 print_interference_ins, &rstate);
14048 coalesced = coalesce_live_ranges(state, &rstate);
14050 #if DEBUG_COALESCING
14051 fprintf(stderr, "coalesced: %d\n", coalesced);
14053 } while(coalesced);
14055 /* Verify the interference graph */
14056 verify_interference_graph(state, &rstate);
14058 /* Build the groups low and high. But with the nodes
14059 * first sorted by degree order.
14061 rstate.low_tail = &rstate.low;
14062 rstate.high_tail = &rstate.high;
14063 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
14065 rstate.high->group_prev = &rstate.high;
14067 for(point = &rstate.high; *point; point = &(*point)->group_next)
14069 rstate.high_tail = point;
14070 /* Walk through the high list and move everything that needs
14073 for(point = &rstate.high; *point; point = next) {
14074 struct live_range *range;
14075 next = &(*point)->group_next;
14078 /* If it has a low degree or it already has a color
14079 * place the node in low.
14081 if ((range->degree < regc_max_size(state, range->classes)) ||
14082 (range->color != REG_UNSET)) {
14083 cgdebug_printf("Lo: %5d degree %5d%s\n",
14084 range - rstate.lr, range->degree,
14085 (range->color != REG_UNSET) ? " (colored)": "");
14086 *range->group_prev = range->group_next;
14087 if (range->group_next) {
14088 range->group_next->group_prev = range->group_prev;
14090 if (&range->group_next == rstate.high_tail) {
14091 rstate.high_tail = range->group_prev;
14093 range->group_prev = rstate.low_tail;
14094 range->group_next = 0;
14095 *rstate.low_tail = range;
14096 rstate.low_tail = &range->group_next;
14100 cgdebug_printf("hi: %5d degree %5d%s\n",
14101 range - rstate.lr, range->degree,
14102 (range->color != REG_UNSET) ? " (colored)": "");
14105 /* Color the live_ranges */
14106 colored = color_graph(state, &rstate);
14108 } while (!colored);
14110 /* Verify the graph was properly colored */
14111 verify_colors(state, &rstate);
14113 /* Move the colors from the graph to the triples */
14114 color_triples(state, &rstate);
14116 /* Cleanup the temporary data structures */
14117 cleanup_rstate(state, &rstate);
14120 /* Sparce Conditional Constant Propogation
14121 * =========================================
14125 struct lattice_node {
14127 struct triple *def;
14128 struct ssa_edge *out;
14129 struct flow_block *fblock;
14130 struct triple *val;
14131 /* lattice high val && !is_const(val)
14132 * lattice const is_const(val)
14133 * lattice low val == 0
14137 struct lattice_node *src;
14138 struct lattice_node *dst;
14139 struct ssa_edge *work_next;
14140 struct ssa_edge *work_prev;
14141 struct ssa_edge *out_next;
14144 struct flow_block *src;
14145 struct flow_block *dst;
14146 struct flow_edge *work_next;
14147 struct flow_edge *work_prev;
14148 struct flow_edge *in_next;
14149 struct flow_edge *out_next;
14152 struct flow_block {
14153 struct block *block;
14154 struct flow_edge *in;
14155 struct flow_edge *out;
14156 struct flow_edge left, right;
14161 struct lattice_node *lattice;
14162 struct ssa_edge *ssa_edges;
14163 struct flow_block *flow_blocks;
14164 struct flow_edge *flow_work_list;
14165 struct ssa_edge *ssa_work_list;
14169 static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
14170 struct flow_edge *fedge)
14172 if (!scc->flow_work_list) {
14173 scc->flow_work_list = fedge;
14174 fedge->work_next = fedge->work_prev = fedge;
14177 struct flow_edge *ftail;
14178 ftail = scc->flow_work_list->work_prev;
14179 fedge->work_next = ftail->work_next;
14180 fedge->work_prev = ftail;
14181 fedge->work_next->work_prev = fedge;
14182 fedge->work_prev->work_next = fedge;
14186 static struct flow_edge *scc_next_fedge(
14187 struct compile_state *state, struct scc_state *scc)
14189 struct flow_edge *fedge;
14190 fedge = scc->flow_work_list;
14192 fedge->work_next->work_prev = fedge->work_prev;
14193 fedge->work_prev->work_next = fedge->work_next;
14194 if (fedge->work_next != fedge) {
14195 scc->flow_work_list = fedge->work_next;
14197 scc->flow_work_list = 0;
14203 static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
14204 struct ssa_edge *sedge)
14206 if (!scc->ssa_work_list) {
14207 scc->ssa_work_list = sedge;
14208 sedge->work_next = sedge->work_prev = sedge;
14211 struct ssa_edge *stail;
14212 stail = scc->ssa_work_list->work_prev;
14213 sedge->work_next = stail->work_next;
14214 sedge->work_prev = stail;
14215 sedge->work_next->work_prev = sedge;
14216 sedge->work_prev->work_next = sedge;
14220 static struct ssa_edge *scc_next_sedge(
14221 struct compile_state *state, struct scc_state *scc)
14223 struct ssa_edge *sedge;
14224 sedge = scc->ssa_work_list;
14226 sedge->work_next->work_prev = sedge->work_prev;
14227 sedge->work_prev->work_next = sedge->work_next;
14228 if (sedge->work_next != sedge) {
14229 scc->ssa_work_list = sedge->work_next;
14231 scc->ssa_work_list = 0;
14237 static void initialize_scc_state(
14238 struct compile_state *state, struct scc_state *scc)
14240 int ins_count, ssa_edge_count;
14241 int ins_index, ssa_edge_index, fblock_index;
14242 struct triple *first, *ins;
14243 struct block *block;
14244 struct flow_block *fblock;
14246 memset(scc, 0, sizeof(*scc));
14248 /* Inialize pass zero find out how much memory we need */
14249 first = RHS(state->main_function, 0);
14251 ins_count = ssa_edge_count = 0;
14253 struct triple_set *edge;
14255 for(edge = ins->use; edge; edge = edge->next) {
14259 } while(ins != first);
14261 fprintf(stderr, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
14262 ins_count, ssa_edge_count, state->last_vertex);
14264 scc->ins_count = ins_count;
14266 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
14268 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
14270 xcmalloc(sizeof(*scc->flow_blocks)*(state->last_vertex + 1),
14273 /* Initialize pass one collect up the nodes */
14276 ins_index = ssa_edge_index = fblock_index = 0;
14279 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
14280 block = ins->u.block;
14282 internal_error(state, ins, "label without block");
14285 block->vertex = fblock_index;
14286 fblock = &scc->flow_blocks[fblock_index];
14287 fblock->block = block;
14290 struct lattice_node *lnode;
14292 lnode = &scc->lattice[ins_index];
14295 lnode->fblock = fblock;
14296 lnode->val = ins; /* LATTICE HIGH */
14297 lnode->old_id = ins->id;
14298 ins->id = ins_index;
14301 } while(ins != first);
14302 /* Initialize pass two collect up the edges */
14307 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
14308 struct flow_edge *fedge, **ftail;
14309 struct block_set *bedge;
14310 block = ins->u.block;
14311 fblock = &scc->flow_blocks[block->vertex];
14314 ftail = &fblock->out;
14316 fblock->left.dst = &scc->flow_blocks[block->left->vertex];
14317 if (fblock->left.dst->block != block->left) {
14318 internal_error(state, 0, "block mismatch");
14320 fblock->left.out_next = 0;
14321 *ftail = &fblock->left;
14322 ftail = &fblock->left.out_next;
14324 if (block->right) {
14325 fblock->right.dst = &scc->flow_blocks[block->right->vertex];
14326 if (fblock->right.dst->block != block->right) {
14327 internal_error(state, 0, "block mismatch");
14329 fblock->right.out_next = 0;
14330 *ftail = &fblock->right;
14331 ftail = &fblock->right.out_next;
14333 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
14334 fedge->src = fblock;
14335 fedge->work_next = fedge->work_prev = fedge;
14336 fedge->executable = 0;
14338 ftail = &fblock->in;
14339 for(bedge = block->use; bedge; bedge = bedge->next) {
14340 struct block *src_block;
14341 struct flow_block *sfblock;
14342 struct flow_edge *sfedge;
14343 src_block = bedge->member;
14344 sfblock = &scc->flow_blocks[src_block->vertex];
14346 if (src_block->left == block) {
14347 sfedge = &sfblock->left;
14349 sfedge = &sfblock->right;
14352 ftail = &sfedge->in_next;
14353 sfedge->in_next = 0;
14357 struct triple_set *edge;
14358 struct ssa_edge **stail;
14359 struct lattice_node *lnode;
14360 lnode = &scc->lattice[ins->id];
14362 stail = &lnode->out;
14363 for(edge = ins->use; edge; edge = edge->next) {
14364 struct ssa_edge *sedge;
14365 ssa_edge_index += 1;
14366 sedge = &scc->ssa_edges[ssa_edge_index];
14368 stail = &sedge->out_next;
14369 sedge->src = lnode;
14370 sedge->dst = &scc->lattice[edge->member->id];
14371 sedge->work_next = sedge->work_prev = sedge;
14372 sedge->out_next = 0;
14376 } while(ins != first);
14377 /* Setup a dummy block 0 as a node above the start node */
14379 struct flow_block *fblock, *dst;
14380 struct flow_edge *fedge;
14381 fblock = &scc->flow_blocks[0];
14384 fblock->out = &fblock->left;
14385 dst = &scc->flow_blocks[state->first_block->vertex];
14386 fedge = &fblock->left;
14387 fedge->src = fblock;
14389 fedge->work_next = fedge;
14390 fedge->work_prev = fedge;
14391 fedge->in_next = fedge->dst->in;
14392 fedge->out_next = 0;
14393 fedge->executable = 0;
14394 fedge->dst->in = fedge;
14396 /* Initialize the work lists */
14397 scc->flow_work_list = 0;
14398 scc->ssa_work_list = 0;
14399 scc_add_fedge(state, scc, fedge);
14402 fprintf(stderr, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
14403 ins_index, ssa_edge_index, fblock_index);
14408 static void free_scc_state(
14409 struct compile_state *state, struct scc_state *scc)
14411 xfree(scc->flow_blocks);
14412 xfree(scc->ssa_edges);
14413 xfree(scc->lattice);
14417 static struct lattice_node *triple_to_lattice(
14418 struct compile_state *state, struct scc_state *scc, struct triple *ins)
14420 if (ins->id <= 0) {
14421 internal_error(state, ins, "bad id");
14423 return &scc->lattice[ins->id];
14426 static struct triple *preserve_lval(
14427 struct compile_state *state, struct lattice_node *lnode)
14429 struct triple *old;
14430 /* Preserve the original value */
14432 old = dup_triple(state, lnode->val);
14433 if (lnode->val != lnode->def) {
14443 static int lval_changed(struct compile_state *state,
14444 struct triple *old, struct lattice_node *lnode)
14447 /* See if the lattice value has changed */
14449 if (!old && !lnode->val) {
14452 if (changed && lnode->val && !is_const(lnode->val)) {
14456 lnode->val && old &&
14457 (memcmp(lnode->val->param, old->param,
14458 TRIPLE_SIZE(lnode->val->sizes) * sizeof(lnode->val->param[0])) == 0) &&
14459 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
14469 static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
14470 struct lattice_node *lnode)
14472 struct lattice_node *tmp;
14473 struct triple **slot, *old;
14474 struct flow_edge *fedge;
14476 if (lnode->def->op != OP_PHI) {
14477 internal_error(state, lnode->def, "not phi");
14479 /* Store the original value */
14480 old = preserve_lval(state, lnode);
14482 /* default to lattice high */
14483 lnode->val = lnode->def;
14484 slot = &RHS(lnode->def, 0);
14486 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
14487 if (!fedge->executable) {
14490 if (!slot[index]) {
14491 internal_error(state, lnode->def, "no phi value");
14493 tmp = triple_to_lattice(state, scc, slot[index]);
14494 /* meet(X, lattice low) = lattice low */
14498 /* meet(X, lattice high) = X */
14499 else if (!tmp->val) {
14500 lnode->val = lnode->val;
14502 /* meet(lattice high, X) = X */
14503 else if (!is_const(lnode->val)) {
14504 lnode->val = dup_triple(state, tmp->val);
14505 lnode->val->type = lnode->def->type;
14507 /* meet(const, const) = const or lattice low */
14508 else if (!constants_equal(state, lnode->val, tmp->val)) {
14516 fprintf(stderr, "phi: %d -> %s\n",
14518 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14520 /* If the lattice value has changed update the work lists. */
14521 if (lval_changed(state, old, lnode)) {
14522 struct ssa_edge *sedge;
14523 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14524 scc_add_sedge(state, scc, sedge);
14529 static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
14530 struct lattice_node *lnode)
14533 struct triple *old, *scratch;
14534 struct triple **dexpr, **vexpr;
14537 /* Store the original value */
14538 old = preserve_lval(state, lnode);
14540 /* Reinitialize the value */
14541 lnode->val = scratch = dup_triple(state, lnode->def);
14542 scratch->id = lnode->old_id;
14543 scratch->next = scratch;
14544 scratch->prev = scratch;
14547 count = TRIPLE_SIZE(scratch->sizes);
14548 for(i = 0; i < count; i++) {
14549 dexpr = &lnode->def->param[i];
14550 vexpr = &scratch->param[i];
14552 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14553 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14555 struct lattice_node *tmp;
14556 tmp = triple_to_lattice(state, scc, *dexpr);
14557 *vexpr = (tmp->val)? tmp->val : tmp->def;
14560 if (scratch->op == OP_BRANCH) {
14561 scratch->next = lnode->def->next;
14563 /* Recompute the value */
14564 #warning "FIXME see if simplify does anything bad"
14565 /* So far it looks like only the strength reduction
14566 * optimization are things I need to worry about.
14568 simplify(state, scratch);
14569 /* Cleanup my value */
14570 if (scratch->use) {
14571 internal_error(state, lnode->def, "scratch used?");
14573 if ((scratch->prev != scratch) ||
14574 ((scratch->next != scratch) &&
14575 ((lnode->def->op != OP_BRANCH) ||
14576 (scratch->next != lnode->def->next)))) {
14577 internal_error(state, lnode->def, "scratch in list?");
14579 /* undo any uses... */
14580 count = TRIPLE_SIZE(scratch->sizes);
14581 for(i = 0; i < count; i++) {
14582 vexpr = &scratch->param[i];
14584 unuse_triple(*vexpr, scratch);
14587 if (!is_const(scratch)) {
14588 for(i = 0; i < count; i++) {
14589 dexpr = &lnode->def->param[i];
14590 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14591 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14593 struct lattice_node *tmp;
14594 tmp = triple_to_lattice(state, scc, *dexpr);
14602 (lnode->val->op == lnode->def->op) &&
14603 (memcmp(lnode->val->param, lnode->def->param,
14604 count * sizeof(lnode->val->param[0])) == 0) &&
14605 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
14606 lnode->val = lnode->def;
14608 /* Find the cases that are always lattice lo */
14610 triple_is_def(state, lnode->val) &&
14611 !triple_is_pure(state, lnode->val)) {
14615 (lnode->val->op == OP_SDECL) &&
14616 (lnode->val != lnode->def)) {
14617 internal_error(state, lnode->def, "bad sdecl");
14619 /* See if the lattice value has changed */
14620 changed = lval_changed(state, old, lnode);
14621 if (lnode->val != scratch) {
14627 static void scc_visit_branch(struct compile_state *state, struct scc_state *scc,
14628 struct lattice_node *lnode)
14630 struct lattice_node *cond;
14633 struct flow_edge *fedge;
14634 fprintf(stderr, "branch: %d (",
14637 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
14638 fprintf(stderr, " %d", fedge->dst->block->vertex);
14640 fprintf(stderr, " )");
14641 if (TRIPLE_RHS(lnode->def->sizes) > 0) {
14642 fprintf(stderr, " <- %d",
14643 RHS(lnode->def, 0)->id);
14645 fprintf(stderr, "\n");
14648 if (lnode->def->op != OP_BRANCH) {
14649 internal_error(state, lnode->def, "not branch");
14651 /* This only applies to conditional branches */
14652 if (TRIPLE_RHS(lnode->def->sizes) == 0) {
14655 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
14656 if (cond->val && !is_const(cond->val)) {
14657 #warning "FIXME do I need to do something here?"
14658 warning(state, cond->def, "condition not constant?");
14661 if (cond->val == 0) {
14662 scc_add_fedge(state, scc, cond->fblock->out);
14663 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14665 else if (cond->val->u.cval) {
14666 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14669 scc_add_fedge(state, scc, cond->fblock->out);
14674 static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
14675 struct lattice_node *lnode)
14679 changed = compute_lnode_val(state, scc, lnode);
14682 struct triple **expr;
14683 fprintf(stderr, "expr: %3d %10s (",
14684 lnode->def->id, tops(lnode->def->op));
14685 expr = triple_rhs(state, lnode->def, 0);
14686 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
14688 fprintf(stderr, " %d", (*expr)->id);
14691 fprintf(stderr, " ) -> %s\n",
14692 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14695 if (lnode->def->op == OP_BRANCH) {
14696 scc_visit_branch(state, scc, lnode);
14699 else if (changed) {
14700 struct ssa_edge *sedge;
14701 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14702 scc_add_sedge(state, scc, sedge);
14707 static void scc_writeback_values(
14708 struct compile_state *state, struct scc_state *scc)
14710 struct triple *first, *ins;
14711 first = RHS(state->main_function, 0);
14714 struct lattice_node *lnode;
14715 lnode = triple_to_lattice(state, scc, ins);
14717 ins->id = lnode->old_id;
14719 if (lnode->val && !is_const(lnode->val)) {
14720 warning(state, lnode->def,
14721 "lattice node still high?");
14724 if (lnode->val && (lnode->val != ins)) {
14725 /* See if it something I know how to write back */
14726 switch(lnode->val->op) {
14728 mkconst(state, ins, lnode->val->u.cval);
14731 mkaddr_const(state, ins,
14732 MISC(lnode->val, 0), lnode->val->u.cval);
14735 /* By default don't copy the changes,
14736 * recompute them in place instead.
14738 simplify(state, ins);
14741 if (is_const(lnode->val) &&
14742 !constants_equal(state, lnode->val, ins)) {
14743 internal_error(state, 0, "constants not equal");
14745 /* Free the lattice nodes */
14750 } while(ins != first);
14753 static void scc_transform(struct compile_state *state)
14755 struct scc_state scc;
14757 initialize_scc_state(state, &scc);
14759 while(scc.flow_work_list || scc.ssa_work_list) {
14760 struct flow_edge *fedge;
14761 struct ssa_edge *sedge;
14762 struct flow_edge *fptr;
14763 while((fedge = scc_next_fedge(state, &scc))) {
14764 struct block *block;
14765 struct triple *ptr;
14766 struct flow_block *fblock;
14769 if (fedge->executable) {
14773 internal_error(state, 0, "fedge without dst");
14776 internal_error(state, 0, "fedge without src");
14778 fedge->executable = 1;
14779 fblock = fedge->dst;
14780 block = fblock->block;
14782 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14783 if (fptr->executable) {
14788 fprintf(stderr, "vertex: %d time: %d\n",
14789 block->vertex, time);
14793 for(ptr = block->first; !done; ptr = ptr->next) {
14794 struct lattice_node *lnode;
14795 done = (ptr == block->last);
14796 lnode = &scc.lattice[ptr->id];
14797 if (ptr->op == OP_PHI) {
14798 scc_visit_phi(state, &scc, lnode);
14800 else if (time == 1) {
14801 scc_visit_expr(state, &scc, lnode);
14804 if (fblock->out && !fblock->out->out_next) {
14805 scc_add_fedge(state, &scc, fblock->out);
14808 while((sedge = scc_next_sedge(state, &scc))) {
14809 struct lattice_node *lnode;
14810 struct flow_block *fblock;
14811 lnode = sedge->dst;
14812 fblock = lnode->fblock;
14814 fprintf(stderr, "sedge: %5d (%5d -> %5d)\n",
14815 sedge - scc.ssa_edges,
14816 sedge->src->def->id,
14817 sedge->dst->def->id);
14819 if (lnode->def->op == OP_PHI) {
14820 scc_visit_phi(state, &scc, lnode);
14823 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14824 if (fptr->executable) {
14829 scc_visit_expr(state, &scc, lnode);
14835 scc_writeback_values(state, &scc);
14836 free_scc_state(state, &scc);
14840 static void transform_to_arch_instructions(struct compile_state *state)
14842 struct triple *ins, *first;
14843 first = RHS(state->main_function, 0);
14846 ins = transform_to_arch_instruction(state, ins);
14847 } while(ins != first);
14850 #if DEBUG_CONSISTENCY
14851 static void verify_uses(struct compile_state *state)
14853 struct triple *first, *ins;
14854 struct triple_set *set;
14855 first = RHS(state->main_function, 0);
14858 struct triple **expr;
14859 expr = triple_rhs(state, ins, 0);
14860 for(; expr; expr = triple_rhs(state, ins, expr)) {
14861 struct triple *rhs;
14863 for(set = rhs?rhs->use:0; set; set = set->next) {
14864 if (set->member == ins) {
14869 internal_error(state, ins, "rhs not used");
14872 expr = triple_lhs(state, ins, 0);
14873 for(; expr; expr = triple_lhs(state, ins, expr)) {
14874 struct triple *lhs;
14876 for(set = lhs?lhs->use:0; set; set = set->next) {
14877 if (set->member == ins) {
14882 internal_error(state, ins, "lhs not used");
14886 } while(ins != first);
14889 static void verify_blocks_present(struct compile_state *state)
14891 struct triple *first, *ins;
14892 if (!state->first_block) {
14895 first = RHS(state->main_function, 0);
14898 valid_ins(state, ins);
14899 if (triple_stores_block(state, ins)) {
14900 if (!ins->u.block) {
14901 internal_error(state, ins,
14902 "%p not in a block?\n", ins);
14906 } while(ins != first);
14910 static void verify_blocks(struct compile_state *state)
14912 struct triple *ins;
14913 struct block *block;
14915 block = state->first_block;
14922 struct block_set *user;
14924 for(ins = block->first; ins != block->last->next; ins = ins->next) {
14925 if (triple_stores_block(state, ins) && (ins->u.block != block)) {
14926 internal_error(state, ins, "inconsitent block specified");
14928 valid_ins(state, ins);
14931 for(user = block->use; user; user = user->next) {
14933 if ((block == state->last_block) &&
14934 (user->member == state->first_block)) {
14937 if ((user->member->left != block) &&
14938 (user->member->right != block)) {
14939 internal_error(state, user->member->first,
14940 "user does not use block");
14943 if (triple_is_branch(state, block->last) &&
14944 (block->right != block_of_triple(state, TARG(block->last, 0))))
14946 internal_error(state, block->last, "block->right != TARG(0)");
14948 if (!triple_is_uncond_branch(state, block->last) &&
14949 (block != state->last_block) &&
14950 (block->left != block_of_triple(state, block->last->next)))
14952 internal_error(state, block->last, "block->left != block->last->next");
14955 for(user = block->left->use; user; user = user->next) {
14956 if (user->member == block) {
14960 if (!user || user->member != block) {
14961 internal_error(state, block->first,
14962 "block does not use left");
14965 if (block->right) {
14966 for(user = block->right->use; user; user = user->next) {
14967 if (user->member == block) {
14971 if (!user || user->member != block) {
14972 internal_error(state, block->first,
14973 "block does not use right");
14976 if (block->users != users) {
14977 internal_error(state, block->first,
14978 "computed users %d != stored users %d\n",
14979 users, block->users);
14981 if (!triple_stores_block(state, block->last->next)) {
14982 internal_error(state, block->last->next,
14983 "cannot find next block");
14985 block = block->last->next->u.block;
14987 internal_error(state, block->last->next,
14990 } while(block != state->first_block);
14991 if (blocks != state->last_vertex) {
14992 internal_error(state, 0, "computed blocks != stored blocks %d\n",
14993 blocks, state->last_vertex);
14997 static void verify_domination(struct compile_state *state)
14999 struct triple *first, *ins;
15000 struct triple_set *set;
15001 if (!state->first_block) {
15005 first = RHS(state->main_function, 0);
15008 for(set = ins->use; set; set = set->next) {
15009 struct triple **slot;
15010 struct triple *use_point;
15013 zrhs = TRIPLE_RHS(ins->sizes);
15014 slot = &RHS(set->member, 0);
15015 /* See if the use is on the right hand side */
15016 for(i = 0; i < zrhs; i++) {
15017 if (slot[i] == ins) {
15022 use_point = set->member;
15023 if (set->member->op == OP_PHI) {
15024 struct block_set *bset;
15026 bset = set->member->u.block->use;
15027 for(edge = 0; bset && (edge < i); edge++) {
15031 internal_error(state, set->member,
15032 "no edge for phi rhs %d\n", i);
15034 use_point = bset->member->last;
15038 !tdominates(state, ins, use_point)) {
15039 internal_warning(state, ins,
15040 "ins does not dominate rhs use");
15041 internal_error(state, use_point,
15042 "non dominated rhs use point?");
15046 } while(ins != first);
15049 static void verify_piece(struct compile_state *state)
15051 struct triple *first, *ins;
15052 first = RHS(state->main_function, 0);
15055 struct triple *ptr;
15057 lhs = TRIPLE_LHS(ins->sizes);
15058 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
15059 if (ptr != LHS(ins, i)) {
15060 internal_error(state, ins, "malformed lhs on %s",
15063 if (ptr->op != OP_PIECE) {
15064 internal_error(state, ins, "bad lhs op %s at %d on %s",
15065 tops(ptr->op), i, tops(ins->op));
15067 if (ptr->u.cval != i) {
15068 internal_error(state, ins, "bad u.cval of %d %d expected",
15073 } while(ins != first);
15075 static void verify_ins_colors(struct compile_state *state)
15077 struct triple *first, *ins;
15079 first = RHS(state->main_function, 0);
15083 } while(ins != first);
15085 static void verify_consistency(struct compile_state *state)
15087 verify_uses(state);
15088 verify_blocks_present(state);
15089 verify_blocks(state);
15090 verify_domination(state);
15091 verify_piece(state);
15092 verify_ins_colors(state);
15095 static void verify_consistency(struct compile_state *state) {}
15096 #endif /* DEBUG_USES */
15098 static void optimize(struct compile_state *state)
15100 if (state->debug & DEBUG_TRIPLES) {
15101 print_triples(state);
15103 /* Replace structures with simpler data types */
15104 flatten_structures(state);
15105 if (state->debug & DEBUG_TRIPLES) {
15106 print_triples(state);
15108 verify_consistency(state);
15109 /* Analize the intermediate code */
15110 setup_basic_blocks(state);
15111 analyze_idominators(state);
15112 analyze_ipdominators(state);
15114 /* Transform the code to ssa form. */
15116 * The transformation to ssa form puts a phi function
15117 * on each of edge of a dominance frontier where that
15118 * phi function might be needed. At -O2 if we don't
15119 * eleminate the excess phi functions we can get an
15120 * exponential code size growth. So I kill the extra
15121 * phi functions early and I kill them often.
15123 transform_to_ssa_form(state);
15124 eliminate_inefectual_code(state);
15126 verify_consistency(state);
15127 if (state->debug & DEBUG_CODE_ELIMINATION) {
15128 fprintf(stdout, "After transform_to_ssa_form\n");
15129 print_blocks(state, stdout);
15131 /* Do strength reduction and simple constant optimizations */
15132 if (state->optimize >= 1) {
15133 simplify_all(state);
15134 transform_from_ssa_form(state);
15135 free_basic_blocks(state);
15136 setup_basic_blocks(state);
15137 analyze_idominators(state);
15138 analyze_ipdominators(state);
15139 transform_to_ssa_form(state);
15140 eliminate_inefectual_code(state);
15142 if (state->debug & DEBUG_CODE_ELIMINATION) {
15143 fprintf(stdout, "After simplify_all\n");
15144 print_blocks(state, stdout);
15146 verify_consistency(state);
15147 /* Propogate constants throughout the code */
15148 if (state->optimize >= 2) {
15149 scc_transform(state);
15150 transform_from_ssa_form(state);
15151 free_basic_blocks(state);
15152 setup_basic_blocks(state);
15153 analyze_idominators(state);
15154 analyze_ipdominators(state);
15155 transform_to_ssa_form(state);
15156 eliminate_inefectual_code(state);
15158 verify_consistency(state);
15159 #warning "WISHLIST implement single use constants (least possible register pressure)"
15160 #warning "WISHLIST implement induction variable elimination"
15161 /* Select architecture instructions and an initial partial
15162 * coloring based on architecture constraints.
15164 transform_to_arch_instructions(state);
15165 verify_consistency(state);
15166 if (state->debug & DEBUG_ARCH_CODE) {
15167 printf("After transform_to_arch_instructions\n");
15168 print_blocks(state, stdout);
15169 print_control_flow(state);
15171 eliminate_inefectual_code(state);
15172 verify_consistency(state);
15173 if (state->debug & DEBUG_CODE_ELIMINATION) {
15174 printf("After eliminate_inefectual_code\n");
15175 print_blocks(state, stdout);
15176 print_control_flow(state);
15178 verify_consistency(state);
15179 /* Color all of the variables to see if they will fit in registers */
15180 insert_copies_to_phi(state);
15181 if (state->debug & DEBUG_INSERTED_COPIES) {
15182 printf("After insert_copies_to_phi\n");
15183 print_blocks(state, stdout);
15184 print_control_flow(state);
15186 verify_consistency(state);
15187 insert_mandatory_copies(state);
15188 if (state->debug & DEBUG_INSERTED_COPIES) {
15189 printf("After insert_mandatory_copies\n");
15190 print_blocks(state, stdout);
15191 print_control_flow(state);
15193 verify_consistency(state);
15194 allocate_registers(state);
15195 verify_consistency(state);
15196 if (state->debug & DEBUG_INTERMEDIATE_CODE) {
15197 print_blocks(state, stdout);
15199 if (state->debug & DEBUG_CONTROL_FLOW) {
15200 print_control_flow(state);
15202 /* Remove the optimization information.
15203 * This is more to check for memory consistency than to free memory.
15205 free_basic_blocks(state);
15208 static void print_op_asm(struct compile_state *state,
15209 struct triple *ins, FILE *fp)
15211 struct asm_info *info;
15213 unsigned lhs, rhs, i;
15214 info = ins->u.ainfo;
15215 lhs = TRIPLE_LHS(ins->sizes);
15216 rhs = TRIPLE_RHS(ins->sizes);
15217 /* Don't count the clobbers in lhs */
15218 for(i = 0; i < lhs; i++) {
15219 if (LHS(ins, i)->type == &void_type) {
15224 fprintf(fp, "#ASM\n");
15226 for(ptr = info->str; *ptr; ptr++) {
15228 unsigned long param;
15229 struct triple *piece;
15239 param = strtoul(ptr, &next, 10);
15241 error(state, ins, "Invalid asm template");
15243 if (param >= (lhs + rhs)) {
15244 error(state, ins, "Invalid param %%%u in asm template",
15247 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
15249 arch_reg_str(ID_REG(piece->id)));
15252 fprintf(fp, "\n#NOT ASM\n");
15256 /* Only use the low x86 byte registers. This allows me
15257 * allocate the entire register when a byte register is used.
15259 #define X86_4_8BIT_GPRS 1
15261 /* Recognized x86 cpu variants */
15269 #define CPU_DEFAULT CPU_I386
15271 /* The x86 register classes */
15272 #define REGC_FLAGS 0
15273 #define REGC_GPR8 1
15274 #define REGC_GPR16 2
15275 #define REGC_GPR32 3
15276 #define REGC_DIVIDEND64 4
15277 #define REGC_DIVIDEND32 5
15280 #define REGC_GPR32_8 8
15281 #define REGC_GPR16_8 9
15282 #define REGC_GPR8_LO 10
15283 #define REGC_IMM32 11
15284 #define REGC_IMM16 12
15285 #define REGC_IMM8 13
15286 #define LAST_REGC REGC_IMM8
15287 #if LAST_REGC >= MAX_REGC
15288 #error "MAX_REGC is to low"
15291 /* Register class masks */
15292 #define REGCM_FLAGS (1 << REGC_FLAGS)
15293 #define REGCM_GPR8 (1 << REGC_GPR8)
15294 #define REGCM_GPR16 (1 << REGC_GPR16)
15295 #define REGCM_GPR32 (1 << REGC_GPR32)
15296 #define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
15297 #define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
15298 #define REGCM_MMX (1 << REGC_MMX)
15299 #define REGCM_XMM (1 << REGC_XMM)
15300 #define REGCM_GPR32_8 (1 << REGC_GPR32_8)
15301 #define REGCM_GPR16_8 (1 << REGC_GPR16_8)
15302 #define REGCM_GPR8_LO (1 << REGC_GPR8_LO)
15303 #define REGCM_IMM32 (1 << REGC_IMM32)
15304 #define REGCM_IMM16 (1 << REGC_IMM16)
15305 #define REGCM_IMM8 (1 << REGC_IMM8)
15306 #define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
15308 /* The x86 registers */
15309 #define REG_EFLAGS 2
15310 #define REGC_FLAGS_FIRST REG_EFLAGS
15311 #define REGC_FLAGS_LAST REG_EFLAGS
15320 #define REGC_GPR8_LO_FIRST REG_AL
15321 #define REGC_GPR8_LO_LAST REG_DL
15322 #define REGC_GPR8_FIRST REG_AL
15323 #define REGC_GPR8_LAST REG_DH
15332 #define REGC_GPR16_FIRST REG_AX
15333 #define REGC_GPR16_LAST REG_SP
15342 #define REGC_GPR32_FIRST REG_EAX
15343 #define REGC_GPR32_LAST REG_ESP
15344 #define REG_EDXEAX 27
15345 #define REGC_DIVIDEND64_FIRST REG_EDXEAX
15346 #define REGC_DIVIDEND64_LAST REG_EDXEAX
15347 #define REG_DXAX 28
15348 #define REGC_DIVIDEND32_FIRST REG_DXAX
15349 #define REGC_DIVIDEND32_LAST REG_DXAX
15350 #define REG_MMX0 29
15351 #define REG_MMX1 30
15352 #define REG_MMX2 31
15353 #define REG_MMX3 32
15354 #define REG_MMX4 33
15355 #define REG_MMX5 34
15356 #define REG_MMX6 35
15357 #define REG_MMX7 36
15358 #define REGC_MMX_FIRST REG_MMX0
15359 #define REGC_MMX_LAST REG_MMX7
15360 #define REG_XMM0 37
15361 #define REG_XMM1 38
15362 #define REG_XMM2 39
15363 #define REG_XMM3 40
15364 #define REG_XMM4 41
15365 #define REG_XMM5 42
15366 #define REG_XMM6 43
15367 #define REG_XMM7 44
15368 #define REGC_XMM_FIRST REG_XMM0
15369 #define REGC_XMM_LAST REG_XMM7
15370 #warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
15371 #define LAST_REG REG_XMM7
15373 #define REGC_GPR32_8_FIRST REG_EAX
15374 #define REGC_GPR32_8_LAST REG_EDX
15375 #define REGC_GPR16_8_FIRST REG_AX
15376 #define REGC_GPR16_8_LAST REG_DX
15378 #define REGC_IMM8_FIRST -1
15379 #define REGC_IMM8_LAST -1
15380 #define REGC_IMM16_FIRST -2
15381 #define REGC_IMM16_LAST -1
15382 #define REGC_IMM32_FIRST -4
15383 #define REGC_IMM32_LAST -1
15385 #if LAST_REG >= MAX_REGISTERS
15386 #error "MAX_REGISTERS to low"
15390 static unsigned regc_size[LAST_REGC +1] = {
15391 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
15392 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
15393 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
15394 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
15395 [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
15396 [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
15397 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
15398 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
15399 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
15400 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
15401 [REGC_GPR8_LO] = REGC_GPR8_LO_LAST - REGC_GPR8_LO_FIRST + 1,
15407 static const struct {
15409 } regcm_bound[LAST_REGC + 1] = {
15410 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
15411 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
15412 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
15413 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
15414 [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
15415 [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
15416 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
15417 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
15418 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
15419 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
15420 [REGC_GPR8_LO] = { REGC_GPR8_LO_FIRST, REGC_GPR8_LO_LAST },
15421 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
15422 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
15423 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
15426 static int arch_encode_cpu(const char *cpu)
15432 { "i386", CPU_I386 },
15440 for(ptr = cpus; ptr->name; ptr++) {
15441 if (strcmp(ptr->name, cpu) == 0) {
15448 static unsigned arch_regc_size(struct compile_state *state, int class)
15450 if ((class < 0) || (class > LAST_REGC)) {
15453 return regc_size[class];
15456 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
15458 /* See if two register classes may have overlapping registers */
15459 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
15460 REGCM_GPR32_8 | REGCM_GPR32 |
15461 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
15463 /* Special case for the immediates */
15464 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
15465 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
15466 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
15467 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
15470 return (regcm1 & regcm2) ||
15471 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
15474 static void arch_reg_equivs(
15475 struct compile_state *state, unsigned *equiv, int reg)
15477 if ((reg < 0) || (reg > LAST_REG)) {
15478 internal_error(state, 0, "invalid register");
15483 #if X86_4_8BIT_GPRS
15487 *equiv++ = REG_EAX;
15488 *equiv++ = REG_DXAX;
15489 *equiv++ = REG_EDXEAX;
15492 #if X86_4_8BIT_GPRS
15496 *equiv++ = REG_EAX;
15497 *equiv++ = REG_DXAX;
15498 *equiv++ = REG_EDXEAX;
15501 #if X86_4_8BIT_GPRS
15505 *equiv++ = REG_EBX;
15509 #if X86_4_8BIT_GPRS
15513 *equiv++ = REG_EBX;
15516 #if X86_4_8BIT_GPRS
15520 *equiv++ = REG_ECX;
15524 #if X86_4_8BIT_GPRS
15528 *equiv++ = REG_ECX;
15531 #if X86_4_8BIT_GPRS
15535 *equiv++ = REG_EDX;
15536 *equiv++ = REG_DXAX;
15537 *equiv++ = REG_EDXEAX;
15540 #if X86_4_8BIT_GPRS
15544 *equiv++ = REG_EDX;
15545 *equiv++ = REG_DXAX;
15546 *equiv++ = REG_EDXEAX;
15551 *equiv++ = REG_EAX;
15552 *equiv++ = REG_DXAX;
15553 *equiv++ = REG_EDXEAX;
15558 *equiv++ = REG_EBX;
15563 *equiv++ = REG_ECX;
15568 *equiv++ = REG_EDX;
15569 *equiv++ = REG_DXAX;
15570 *equiv++ = REG_EDXEAX;
15573 *equiv++ = REG_ESI;
15576 *equiv++ = REG_EDI;
15579 *equiv++ = REG_EBP;
15582 *equiv++ = REG_ESP;
15588 *equiv++ = REG_DXAX;
15589 *equiv++ = REG_EDXEAX;
15605 *equiv++ = REG_DXAX;
15606 *equiv++ = REG_EDXEAX;
15627 *equiv++ = REG_EAX;
15628 *equiv++ = REG_EDX;
15629 *equiv++ = REG_EDXEAX;
15638 *equiv++ = REG_EAX;
15639 *equiv++ = REG_EDX;
15640 *equiv++ = REG_DXAX;
15643 *equiv++ = REG_UNSET;
15646 static unsigned arch_avail_mask(struct compile_state *state)
15648 unsigned avail_mask;
15649 /* REGCM_GPR8 is not available */
15650 avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
15651 REGCM_GPR32 | REGCM_GPR32_8 |
15652 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
15653 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
15654 switch(state->cpu) {
15657 avail_mask |= REGCM_MMX;
15661 avail_mask |= REGCM_MMX | REGCM_XMM;
15667 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
15669 unsigned mask, result;
15673 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
15674 if ((result & mask) == 0) {
15677 if (class > LAST_REGC) {
15680 for(class2 = 0; class2 <= LAST_REGC; class2++) {
15681 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
15682 (regcm_bound[class2].last <= regcm_bound[class].last)) {
15683 result |= (1 << class2);
15687 result &= arch_avail_mask(state);
15691 static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
15693 /* Like arch_regcm_normalize except immediate register classes are excluded */
15694 regcm = arch_regcm_normalize(state, regcm);
15695 /* Remove the immediate register classes */
15696 regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
15701 static unsigned arch_reg_regcm(struct compile_state *state, int reg)
15706 for(class = 0; class <= LAST_REGC; class++) {
15707 if ((reg >= regcm_bound[class].first) &&
15708 (reg <= regcm_bound[class].last)) {
15709 mask |= (1 << class);
15713 internal_error(state, 0, "reg %d not in any class", reg);
15718 static struct reg_info arch_reg_constraint(
15719 struct compile_state *state, struct type *type, const char *constraint)
15721 static const struct {
15725 } constraints[] = {
15726 { 'r', REGCM_GPR32, REG_UNSET },
15727 { 'g', REGCM_GPR32, REG_UNSET },
15728 { 'p', REGCM_GPR32, REG_UNSET },
15729 { 'q', REGCM_GPR8_LO, REG_UNSET },
15730 { 'Q', REGCM_GPR32_8, REG_UNSET },
15731 { 'x', REGCM_XMM, REG_UNSET },
15732 { 'y', REGCM_MMX, REG_UNSET },
15733 { 'a', REGCM_GPR32, REG_EAX },
15734 { 'b', REGCM_GPR32, REG_EBX },
15735 { 'c', REGCM_GPR32, REG_ECX },
15736 { 'd', REGCM_GPR32, REG_EDX },
15737 { 'D', REGCM_GPR32, REG_EDI },
15738 { 'S', REGCM_GPR32, REG_ESI },
15739 { '\0', 0, REG_UNSET },
15741 unsigned int regcm;
15742 unsigned int mask, reg;
15743 struct reg_info result;
15745 regcm = arch_type_to_regcm(state, type);
15748 for(ptr = constraint; *ptr; ptr++) {
15753 for(i = 0; constraints[i].class != '\0'; i++) {
15754 if (constraints[i].class == *ptr) {
15758 if (constraints[i].class == '\0') {
15759 error(state, 0, "invalid register constraint ``%c''", *ptr);
15762 if ((constraints[i].mask & regcm) == 0) {
15763 error(state, 0, "invalid register class %c specified",
15766 mask |= constraints[i].mask;
15767 if (constraints[i].reg != REG_UNSET) {
15768 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
15769 error(state, 0, "Only one register may be specified");
15771 reg = constraints[i].reg;
15775 result.regcm = mask;
15779 static struct reg_info arch_reg_clobber(
15780 struct compile_state *state, const char *clobber)
15782 struct reg_info result;
15783 if (strcmp(clobber, "memory") == 0) {
15784 result.reg = REG_UNSET;
15787 else if (strcmp(clobber, "%eax") == 0) {
15788 result.reg = REG_EAX;
15789 result.regcm = REGCM_GPR32;
15791 else if (strcmp(clobber, "%ebx") == 0) {
15792 result.reg = REG_EBX;
15793 result.regcm = REGCM_GPR32;
15795 else if (strcmp(clobber, "%ecx") == 0) {
15796 result.reg = REG_ECX;
15797 result.regcm = REGCM_GPR32;
15799 else if (strcmp(clobber, "%edx") == 0) {
15800 result.reg = REG_EDX;
15801 result.regcm = REGCM_GPR32;
15803 else if (strcmp(clobber, "%esi") == 0) {
15804 result.reg = REG_ESI;
15805 result.regcm = REGCM_GPR32;
15807 else if (strcmp(clobber, "%edi") == 0) {
15808 result.reg = REG_EDI;
15809 result.regcm = REGCM_GPR32;
15811 else if (strcmp(clobber, "%ebp") == 0) {
15812 result.reg = REG_EBP;
15813 result.regcm = REGCM_GPR32;
15815 else if (strcmp(clobber, "%esp") == 0) {
15816 result.reg = REG_ESP;
15817 result.regcm = REGCM_GPR32;
15819 else if (strcmp(clobber, "cc") == 0) {
15820 result.reg = REG_EFLAGS;
15821 result.regcm = REGCM_FLAGS;
15823 else if ((strncmp(clobber, "xmm", 3) == 0) &&
15824 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15825 result.reg = REG_XMM0 + octdigval(clobber[3]);
15826 result.regcm = REGCM_XMM;
15828 else if ((strncmp(clobber, "mmx", 3) == 0) &&
15829 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15830 result.reg = REG_MMX0 + octdigval(clobber[3]);
15831 result.regcm = REGCM_MMX;
15834 error(state, 0, "Invalid register clobber");
15835 result.reg = REG_UNSET;
15841 static int do_select_reg(struct compile_state *state,
15842 char *used, int reg, unsigned classes)
15848 mask = arch_reg_regcm(state, reg);
15849 return (classes & mask) ? reg : REG_UNSET;
15852 static int arch_select_free_register(
15853 struct compile_state *state, char *used, int classes)
15855 /* Live ranges with the most neighbors are colored first.
15857 * Generally it does not matter which colors are given
15858 * as the register allocator attempts to color live ranges
15859 * in an order where you are guaranteed not to run out of colors.
15861 * Occasionally the register allocator cannot find an order
15862 * of register selection that will find a free color. To
15863 * increase the odds the register allocator will work when
15864 * it guesses first give out registers from register classes
15865 * least likely to run out of registers.
15870 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
15871 reg = do_select_reg(state, used, i, classes);
15873 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
15874 reg = do_select_reg(state, used, i, classes);
15876 for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
15877 reg = do_select_reg(state, used, i, classes);
15879 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
15880 reg = do_select_reg(state, used, i, classes);
15882 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
15883 reg = do_select_reg(state, used, i, classes);
15885 for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
15886 reg = do_select_reg(state, used, i, classes);
15888 for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
15889 reg = do_select_reg(state, used, i, classes);
15891 for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
15892 reg = do_select_reg(state, used, i, classes);
15894 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
15895 reg = do_select_reg(state, used, i, classes);
15901 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
15903 #warning "FIXME force types smaller (if legal) before I get here"
15906 switch(type->type & TYPE_MASK) {
15913 mask = REGCM_GPR8 | REGCM_GPR8_LO |
15914 REGCM_GPR16 | REGCM_GPR16_8 |
15915 REGCM_GPR32 | REGCM_GPR32_8 |
15916 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
15917 REGCM_MMX | REGCM_XMM |
15918 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
15922 mask = REGCM_GPR16 | REGCM_GPR16_8 |
15923 REGCM_GPR32 | REGCM_GPR32_8 |
15924 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
15925 REGCM_MMX | REGCM_XMM |
15926 REGCM_IMM32 | REGCM_IMM16;
15933 mask = REGCM_GPR32 | REGCM_GPR32_8 |
15934 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
15935 REGCM_MMX | REGCM_XMM |
15939 internal_error(state, 0, "no register class for type");
15942 mask = arch_regcm_normalize(state, mask);
15946 static int is_imm32(struct triple *imm)
15948 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
15949 (imm->op == OP_ADDRCONST);
15952 static int is_imm16(struct triple *imm)
15954 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
15956 static int is_imm8(struct triple *imm)
15958 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
15961 static int get_imm32(struct triple *ins, struct triple **expr)
15963 struct triple *imm;
15965 while(imm->op == OP_COPY) {
15968 if (!is_imm32(imm)) {
15971 unuse_triple(*expr, ins);
15972 use_triple(imm, ins);
15977 static int get_imm8(struct triple *ins, struct triple **expr)
15979 struct triple *imm;
15981 while(imm->op == OP_COPY) {
15984 if (!is_imm8(imm)) {
15987 unuse_triple(*expr, ins);
15988 use_triple(imm, ins);
15993 #define TEMPLATE_NOP 0
15994 #define TEMPLATE_INTCONST8 1
15995 #define TEMPLATE_INTCONST32 2
15996 #define TEMPLATE_COPY8_REG 3
15997 #define TEMPLATE_COPY16_REG 4
15998 #define TEMPLATE_COPY32_REG 5
15999 #define TEMPLATE_COPY_IMM8 6
16000 #define TEMPLATE_COPY_IMM16 7
16001 #define TEMPLATE_COPY_IMM32 8
16002 #define TEMPLATE_PHI8 9
16003 #define TEMPLATE_PHI16 10
16004 #define TEMPLATE_PHI32 11
16005 #define TEMPLATE_STORE8 12
16006 #define TEMPLATE_STORE16 13
16007 #define TEMPLATE_STORE32 14
16008 #define TEMPLATE_LOAD8 15
16009 #define TEMPLATE_LOAD16 16
16010 #define TEMPLATE_LOAD32 17
16011 #define TEMPLATE_BINARY8_REG 18
16012 #define TEMPLATE_BINARY16_REG 19
16013 #define TEMPLATE_BINARY32_REG 20
16014 #define TEMPLATE_BINARY8_IMM 21
16015 #define TEMPLATE_BINARY16_IMM 22
16016 #define TEMPLATE_BINARY32_IMM 23
16017 #define TEMPLATE_SL8_CL 24
16018 #define TEMPLATE_SL16_CL 25
16019 #define TEMPLATE_SL32_CL 26
16020 #define TEMPLATE_SL8_IMM 27
16021 #define TEMPLATE_SL16_IMM 28
16022 #define TEMPLATE_SL32_IMM 29
16023 #define TEMPLATE_UNARY8 30
16024 #define TEMPLATE_UNARY16 31
16025 #define TEMPLATE_UNARY32 32
16026 #define TEMPLATE_CMP8_REG 33
16027 #define TEMPLATE_CMP16_REG 34
16028 #define TEMPLATE_CMP32_REG 35
16029 #define TEMPLATE_CMP8_IMM 36
16030 #define TEMPLATE_CMP16_IMM 37
16031 #define TEMPLATE_CMP32_IMM 38
16032 #define TEMPLATE_TEST8 39
16033 #define TEMPLATE_TEST16 40
16034 #define TEMPLATE_TEST32 41
16035 #define TEMPLATE_SET 42
16036 #define TEMPLATE_JMP 43
16037 #define TEMPLATE_INB_DX 44
16038 #define TEMPLATE_INB_IMM 45
16039 #define TEMPLATE_INW_DX 46
16040 #define TEMPLATE_INW_IMM 47
16041 #define TEMPLATE_INL_DX 48
16042 #define TEMPLATE_INL_IMM 49
16043 #define TEMPLATE_OUTB_DX 50
16044 #define TEMPLATE_OUTB_IMM 51
16045 #define TEMPLATE_OUTW_DX 52
16046 #define TEMPLATE_OUTW_IMM 53
16047 #define TEMPLATE_OUTL_DX 54
16048 #define TEMPLATE_OUTL_IMM 55
16049 #define TEMPLATE_BSF 56
16050 #define TEMPLATE_RDMSR 57
16051 #define TEMPLATE_WRMSR 58
16052 #define TEMPLATE_UMUL8 59
16053 #define TEMPLATE_UMUL16 60
16054 #define TEMPLATE_UMUL32 61
16055 #define TEMPLATE_DIV8 62
16056 #define TEMPLATE_DIV16 63
16057 #define TEMPLATE_DIV32 64
16058 #define LAST_TEMPLATE TEMPLATE_DIV32
16059 #if LAST_TEMPLATE >= MAX_TEMPLATES
16060 #error "MAX_TEMPLATES to low"
16063 #define COPY8_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO | REGCM_MMX | REGCM_XMM)
16064 #define COPY16_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)
16065 #define COPY32_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
16068 static struct ins_template templates[] = {
16069 [TEMPLATE_NOP] = {},
16070 [TEMPLATE_INTCONST8] = {
16071 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16073 [TEMPLATE_INTCONST32] = {
16074 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
16076 [TEMPLATE_COPY8_REG] = {
16077 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
16078 .rhs = { [0] = { REG_UNSET, COPY8_REGCM } },
16080 [TEMPLATE_COPY16_REG] = {
16081 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
16082 .rhs = { [0] = { REG_UNSET, COPY16_REGCM } },
16084 [TEMPLATE_COPY32_REG] = {
16085 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
16086 .rhs = { [0] = { REG_UNSET, COPY32_REGCM } },
16088 [TEMPLATE_COPY_IMM8] = {
16089 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
16090 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16092 [TEMPLATE_COPY_IMM16] = {
16093 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
16094 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
16096 [TEMPLATE_COPY_IMM32] = {
16097 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
16098 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
16100 [TEMPLATE_PHI8] = {
16101 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
16103 [ 0] = { REG_VIRT0, COPY8_REGCM },
16104 [ 1] = { REG_VIRT0, COPY8_REGCM },
16105 [ 2] = { REG_VIRT0, COPY8_REGCM },
16106 [ 3] = { REG_VIRT0, COPY8_REGCM },
16107 [ 4] = { REG_VIRT0, COPY8_REGCM },
16108 [ 5] = { REG_VIRT0, COPY8_REGCM },
16109 [ 6] = { REG_VIRT0, COPY8_REGCM },
16110 [ 7] = { REG_VIRT0, COPY8_REGCM },
16111 [ 8] = { REG_VIRT0, COPY8_REGCM },
16112 [ 9] = { REG_VIRT0, COPY8_REGCM },
16113 [10] = { REG_VIRT0, COPY8_REGCM },
16114 [11] = { REG_VIRT0, COPY8_REGCM },
16115 [12] = { REG_VIRT0, COPY8_REGCM },
16116 [13] = { REG_VIRT0, COPY8_REGCM },
16117 [14] = { REG_VIRT0, COPY8_REGCM },
16118 [15] = { REG_VIRT0, COPY8_REGCM },
16120 [TEMPLATE_PHI16] = {
16121 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
16123 [ 0] = { REG_VIRT0, COPY16_REGCM },
16124 [ 1] = { REG_VIRT0, COPY16_REGCM },
16125 [ 2] = { REG_VIRT0, COPY16_REGCM },
16126 [ 3] = { REG_VIRT0, COPY16_REGCM },
16127 [ 4] = { REG_VIRT0, COPY16_REGCM },
16128 [ 5] = { REG_VIRT0, COPY16_REGCM },
16129 [ 6] = { REG_VIRT0, COPY16_REGCM },
16130 [ 7] = { REG_VIRT0, COPY16_REGCM },
16131 [ 8] = { REG_VIRT0, COPY16_REGCM },
16132 [ 9] = { REG_VIRT0, COPY16_REGCM },
16133 [10] = { REG_VIRT0, COPY16_REGCM },
16134 [11] = { REG_VIRT0, COPY16_REGCM },
16135 [12] = { REG_VIRT0, COPY16_REGCM },
16136 [13] = { REG_VIRT0, COPY16_REGCM },
16137 [14] = { REG_VIRT0, COPY16_REGCM },
16138 [15] = { REG_VIRT0, COPY16_REGCM },
16140 [TEMPLATE_PHI32] = {
16141 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
16143 [ 0] = { REG_VIRT0, COPY32_REGCM },
16144 [ 1] = { REG_VIRT0, COPY32_REGCM },
16145 [ 2] = { REG_VIRT0, COPY32_REGCM },
16146 [ 3] = { REG_VIRT0, COPY32_REGCM },
16147 [ 4] = { REG_VIRT0, COPY32_REGCM },
16148 [ 5] = { REG_VIRT0, COPY32_REGCM },
16149 [ 6] = { REG_VIRT0, COPY32_REGCM },
16150 [ 7] = { REG_VIRT0, COPY32_REGCM },
16151 [ 8] = { REG_VIRT0, COPY32_REGCM },
16152 [ 9] = { REG_VIRT0, COPY32_REGCM },
16153 [10] = { REG_VIRT0, COPY32_REGCM },
16154 [11] = { REG_VIRT0, COPY32_REGCM },
16155 [12] = { REG_VIRT0, COPY32_REGCM },
16156 [13] = { REG_VIRT0, COPY32_REGCM },
16157 [14] = { REG_VIRT0, COPY32_REGCM },
16158 [15] = { REG_VIRT0, COPY32_REGCM },
16160 [TEMPLATE_STORE8] = {
16162 [0] = { REG_UNSET, REGCM_GPR32 },
16163 [1] = { REG_UNSET, REGCM_GPR8_LO },
16166 [TEMPLATE_STORE16] = {
16168 [0] = { REG_UNSET, REGCM_GPR32 },
16169 [1] = { REG_UNSET, REGCM_GPR16 },
16172 [TEMPLATE_STORE32] = {
16174 [0] = { REG_UNSET, REGCM_GPR32 },
16175 [1] = { REG_UNSET, REGCM_GPR32 },
16178 [TEMPLATE_LOAD8] = {
16179 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
16180 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16182 [TEMPLATE_LOAD16] = {
16183 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
16184 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16186 [TEMPLATE_LOAD32] = {
16187 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16188 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16190 [TEMPLATE_BINARY8_REG] = {
16191 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16193 [0] = { REG_VIRT0, REGCM_GPR8_LO },
16194 [1] = { REG_UNSET, REGCM_GPR8_LO },
16197 [TEMPLATE_BINARY16_REG] = {
16198 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16200 [0] = { REG_VIRT0, REGCM_GPR16 },
16201 [1] = { REG_UNSET, REGCM_GPR16 },
16204 [TEMPLATE_BINARY32_REG] = {
16205 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16207 [0] = { REG_VIRT0, REGCM_GPR32 },
16208 [1] = { REG_UNSET, REGCM_GPR32 },
16211 [TEMPLATE_BINARY8_IMM] = {
16212 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16214 [0] = { REG_VIRT0, REGCM_GPR8_LO },
16215 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16218 [TEMPLATE_BINARY16_IMM] = {
16219 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16221 [0] = { REG_VIRT0, REGCM_GPR16 },
16222 [1] = { REG_UNNEEDED, REGCM_IMM16 },
16225 [TEMPLATE_BINARY32_IMM] = {
16226 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16228 [0] = { REG_VIRT0, REGCM_GPR32 },
16229 [1] = { REG_UNNEEDED, REGCM_IMM32 },
16232 [TEMPLATE_SL8_CL] = {
16233 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16235 [0] = { REG_VIRT0, REGCM_GPR8_LO },
16236 [1] = { REG_CL, REGCM_GPR8_LO },
16239 [TEMPLATE_SL16_CL] = {
16240 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16242 [0] = { REG_VIRT0, REGCM_GPR16 },
16243 [1] = { REG_CL, REGCM_GPR8_LO },
16246 [TEMPLATE_SL32_CL] = {
16247 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16249 [0] = { REG_VIRT0, REGCM_GPR32 },
16250 [1] = { REG_CL, REGCM_GPR8_LO },
16253 [TEMPLATE_SL8_IMM] = {
16254 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16256 [0] = { REG_VIRT0, REGCM_GPR8_LO },
16257 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16260 [TEMPLATE_SL16_IMM] = {
16261 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16263 [0] = { REG_VIRT0, REGCM_GPR16 },
16264 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16267 [TEMPLATE_SL32_IMM] = {
16268 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16270 [0] = { REG_VIRT0, REGCM_GPR32 },
16271 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16274 [TEMPLATE_UNARY8] = {
16275 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16276 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16278 [TEMPLATE_UNARY16] = {
16279 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16280 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16282 [TEMPLATE_UNARY32] = {
16283 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16284 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16286 [TEMPLATE_CMP8_REG] = {
16287 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16289 [0] = { REG_UNSET, REGCM_GPR8_LO },
16290 [1] = { REG_UNSET, REGCM_GPR8_LO },
16293 [TEMPLATE_CMP16_REG] = {
16294 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16296 [0] = { REG_UNSET, REGCM_GPR16 },
16297 [1] = { REG_UNSET, REGCM_GPR16 },
16300 [TEMPLATE_CMP32_REG] = {
16301 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16303 [0] = { REG_UNSET, REGCM_GPR32 },
16304 [1] = { REG_UNSET, REGCM_GPR32 },
16307 [TEMPLATE_CMP8_IMM] = {
16308 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16310 [0] = { REG_UNSET, REGCM_GPR8_LO },
16311 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16314 [TEMPLATE_CMP16_IMM] = {
16315 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16317 [0] = { REG_UNSET, REGCM_GPR16 },
16318 [1] = { REG_UNNEEDED, REGCM_IMM16 },
16321 [TEMPLATE_CMP32_IMM] = {
16322 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16324 [0] = { REG_UNSET, REGCM_GPR32 },
16325 [1] = { REG_UNNEEDED, REGCM_IMM32 },
16328 [TEMPLATE_TEST8] = {
16329 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16330 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
16332 [TEMPLATE_TEST16] = {
16333 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16334 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
16336 [TEMPLATE_TEST32] = {
16337 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16338 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16341 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
16342 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16345 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16347 [TEMPLATE_INB_DX] = {
16348 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
16349 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
16351 [TEMPLATE_INB_IMM] = {
16352 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
16353 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16355 [TEMPLATE_INW_DX] = {
16356 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
16357 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
16359 [TEMPLATE_INW_IMM] = {
16360 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
16361 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16363 [TEMPLATE_INL_DX] = {
16364 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
16365 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
16367 [TEMPLATE_INL_IMM] = {
16368 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
16369 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16371 [TEMPLATE_OUTB_DX] = {
16373 [0] = { REG_AL, REGCM_GPR8_LO },
16374 [1] = { REG_DX, REGCM_GPR16 },
16377 [TEMPLATE_OUTB_IMM] = {
16379 [0] = { REG_AL, REGCM_GPR8_LO },
16380 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16383 [TEMPLATE_OUTW_DX] = {
16385 [0] = { REG_AX, REGCM_GPR16 },
16386 [1] = { REG_DX, REGCM_GPR16 },
16389 [TEMPLATE_OUTW_IMM] = {
16391 [0] = { REG_AX, REGCM_GPR16 },
16392 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16395 [TEMPLATE_OUTL_DX] = {
16397 [0] = { REG_EAX, REGCM_GPR32 },
16398 [1] = { REG_DX, REGCM_GPR16 },
16401 [TEMPLATE_OUTL_IMM] = {
16403 [0] = { REG_EAX, REGCM_GPR32 },
16404 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16408 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16409 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16411 [TEMPLATE_RDMSR] = {
16413 [0] = { REG_EAX, REGCM_GPR32 },
16414 [1] = { REG_EDX, REGCM_GPR32 },
16416 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
16418 [TEMPLATE_WRMSR] = {
16420 [0] = { REG_ECX, REGCM_GPR32 },
16421 [1] = { REG_EAX, REGCM_GPR32 },
16422 [2] = { REG_EDX, REGCM_GPR32 },
16425 [TEMPLATE_UMUL8] = {
16426 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
16428 [0] = { REG_AL, REGCM_GPR8_LO },
16429 [1] = { REG_UNSET, REGCM_GPR8_LO },
16432 [TEMPLATE_UMUL16] = {
16433 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
16435 [0] = { REG_AX, REGCM_GPR16 },
16436 [1] = { REG_UNSET, REGCM_GPR16 },
16439 [TEMPLATE_UMUL32] = {
16440 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
16442 [0] = { REG_EAX, REGCM_GPR32 },
16443 [1] = { REG_UNSET, REGCM_GPR32 },
16446 [TEMPLATE_DIV8] = {
16448 [0] = { REG_AL, REGCM_GPR8_LO },
16449 [1] = { REG_AH, REGCM_GPR8 },
16452 [0] = { REG_AX, REGCM_GPR16 },
16453 [1] = { REG_UNSET, REGCM_GPR8_LO },
16456 [TEMPLATE_DIV16] = {
16458 [0] = { REG_AX, REGCM_GPR16 },
16459 [1] = { REG_DX, REGCM_GPR16 },
16462 [0] = { REG_DXAX, REGCM_DIVIDEND32 },
16463 [1] = { REG_UNSET, REGCM_GPR16 },
16466 [TEMPLATE_DIV32] = {
16468 [0] = { REG_EAX, REGCM_GPR32 },
16469 [1] = { REG_EDX, REGCM_GPR32 },
16472 [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
16473 [1] = { REG_UNSET, REGCM_GPR32 },
16478 static void fixup_branches(struct compile_state *state,
16479 struct triple *cmp, struct triple *use, int jmp_op)
16481 struct triple_set *entry, *next;
16482 for(entry = use->use; entry; entry = next) {
16483 next = entry->next;
16484 if (entry->member->op == OP_COPY) {
16485 fixup_branches(state, cmp, entry->member, jmp_op);
16487 else if (entry->member->op == OP_BRANCH) {
16488 struct triple *branch, *test;
16489 struct triple *left, *right;
16491 left = RHS(cmp, 0);
16492 if (TRIPLE_RHS(cmp->sizes) > 1) {
16493 right = RHS(cmp, 1);
16495 branch = entry->member;
16496 test = pre_triple(state, branch,
16497 cmp->op, cmp->type, left, right);
16498 test->template_id = TEMPLATE_TEST32;
16499 if (cmp->op == OP_CMP) {
16500 test->template_id = TEMPLATE_CMP32_REG;
16501 if (get_imm32(test, &RHS(test, 1))) {
16502 test->template_id = TEMPLATE_CMP32_IMM;
16505 use_triple(RHS(test, 0), test);
16506 use_triple(RHS(test, 1), test);
16507 unuse_triple(RHS(branch, 0), branch);
16508 RHS(branch, 0) = test;
16509 branch->op = jmp_op;
16510 branch->template_id = TEMPLATE_JMP;
16511 use_triple(RHS(branch, 0), branch);
16516 static void bool_cmp(struct compile_state *state,
16517 struct triple *ins, int cmp_op, int jmp_op, int set_op)
16519 struct triple_set *entry, *next;
16520 struct triple *set;
16522 /* Put a barrier up before the cmp which preceeds the
16523 * copy instruction. If a set actually occurs this gives
16524 * us a chance to move variables in registers out of the way.
16527 /* Modify the comparison operator */
16529 ins->template_id = TEMPLATE_TEST32;
16530 if (cmp_op == OP_CMP) {
16531 ins->template_id = TEMPLATE_CMP32_REG;
16532 if (get_imm32(ins, &RHS(ins, 1))) {
16533 ins->template_id = TEMPLATE_CMP32_IMM;
16536 /* Generate the instruction sequence that will transform the
16537 * result of the comparison into a logical value.
16539 set = post_triple(state, ins, set_op, &char_type, ins, 0);
16540 use_triple(ins, set);
16541 set->template_id = TEMPLATE_SET;
16543 for(entry = ins->use; entry; entry = next) {
16544 next = entry->next;
16545 if (entry->member == set) {
16548 replace_rhs_use(state, ins, set, entry->member);
16550 fixup_branches(state, ins, set, jmp_op);
16553 static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
16555 struct triple *next;
16557 lhs = TRIPLE_LHS(ins->sizes);
16558 for(next = ins->next, i = 0; i < lhs; i++, next = next->next) {
16559 if (next != LHS(ins, i)) {
16560 internal_error(state, ins, "malformed lhs on %s",
16563 if (next->op != OP_PIECE) {
16564 internal_error(state, ins, "bad lhs op %s at %d on %s",
16565 tops(next->op), i, tops(ins->op));
16567 if (next->u.cval != i) {
16568 internal_error(state, ins, "bad u.cval of %d %d expected",
16575 struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
16577 struct ins_template *template;
16578 struct reg_info result;
16580 if (ins->op == OP_PIECE) {
16581 index = ins->u.cval;
16582 ins = MISC(ins, 0);
16584 zlhs = TRIPLE_LHS(ins->sizes);
16585 if (triple_is_def(state, ins)) {
16588 if (index >= zlhs) {
16589 internal_error(state, ins, "index %d out of range for %s\n",
16590 index, tops(ins->op));
16594 template = &ins->u.ainfo->tmpl;
16597 if (ins->template_id > LAST_TEMPLATE) {
16598 internal_error(state, ins, "bad template number %d",
16601 template = &templates[ins->template_id];
16604 result = template->lhs[index];
16605 result.regcm = arch_regcm_normalize(state, result.regcm);
16606 if (result.reg != REG_UNNEEDED) {
16607 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
16609 if (result.regcm == 0) {
16610 internal_error(state, ins, "lhs %d regcm == 0", index);
16615 struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
16617 struct reg_info result;
16618 struct ins_template *template;
16619 if ((index > TRIPLE_RHS(ins->sizes)) ||
16620 (ins->op == OP_PIECE)) {
16621 internal_error(state, ins, "index %d out of range for %s\n",
16622 index, tops(ins->op));
16626 template = &ins->u.ainfo->tmpl;
16629 if (ins->template_id > LAST_TEMPLATE) {
16630 internal_error(state, ins, "bad template number %d",
16633 template = &templates[ins->template_id];
16636 result = template->rhs[index];
16637 result.regcm = arch_regcm_normalize(state, result.regcm);
16638 if (result.regcm == 0) {
16639 internal_error(state, ins, "rhs %d regcm == 0", index);
16644 static struct triple *mod_div(struct compile_state *state,
16645 struct triple *ins, int div_op, int index)
16647 struct triple *div, *piece0, *piece1;
16649 /* Generate a piece to hold the remainder */
16650 piece1 = post_triple(state, ins, OP_PIECE, ins->type, 0, 0);
16651 piece1->u.cval = 1;
16653 /* Generate a piece to hold the quotient */
16654 piece0 = post_triple(state, ins, OP_PIECE, ins->type, 0, 0);
16655 piece0->u.cval = 0;
16657 /* Generate the appropriate division instruction */
16658 div = post_triple(state, ins, div_op, ins->type, 0, 0);
16659 RHS(div, 0) = RHS(ins, 0);
16660 RHS(div, 1) = RHS(ins, 1);
16661 LHS(div, 0) = piece0;
16662 LHS(div, 1) = piece1;
16663 div->template_id = TEMPLATE_DIV32;
16664 use_triple(RHS(div, 0), div);
16665 use_triple(RHS(div, 1), div);
16666 use_triple(LHS(div, 0), div);
16667 use_triple(LHS(div, 1), div);
16669 /* Hook on piece0 */
16670 MISC(piece0, 0) = div;
16671 use_triple(div, piece0);
16673 /* Hook on piece1 */
16674 MISC(piece1, 0) = div;
16675 use_triple(div, piece1);
16677 /* Replate uses of ins with the appropriate piece of the div */
16678 propogate_use(state, ins, LHS(div, index));
16679 release_triple(state, ins);
16681 /* Return the address of the next instruction */
16682 return piece1->next;
16685 static struct triple *transform_to_arch_instruction(
16686 struct compile_state *state, struct triple *ins)
16688 /* Transform from generic 3 address instructions
16689 * to archtecture specific instructions.
16690 * And apply architecture specific constraints to instructions.
16691 * Copies are inserted to preserve the register flexibility
16692 * of 3 address instructions.
16694 struct triple *next;
16699 ins->template_id = TEMPLATE_INTCONST32;
16700 if (ins->u.cval < 256) {
16701 ins->template_id = TEMPLATE_INTCONST8;
16705 ins->template_id = TEMPLATE_INTCONST32;
16711 ins->template_id = TEMPLATE_NOP;
16714 size = size_of(state, ins->type);
16715 if (is_imm8(RHS(ins, 0)) && (size <= 1)) {
16716 ins->template_id = TEMPLATE_COPY_IMM8;
16718 else if (is_imm16(RHS(ins, 0)) && (size <= 2)) {
16719 ins->template_id = TEMPLATE_COPY_IMM16;
16721 else if (is_imm32(RHS(ins, 0)) && (size <= 4)) {
16722 ins->template_id = TEMPLATE_COPY_IMM32;
16724 else if (is_const(RHS(ins, 0))) {
16725 internal_error(state, ins, "bad constant passed to copy");
16727 else if (size <= 1) {
16728 ins->template_id = TEMPLATE_COPY8_REG;
16730 else if (size <= 2) {
16731 ins->template_id = TEMPLATE_COPY16_REG;
16733 else if (size <= 4) {
16734 ins->template_id = TEMPLATE_COPY32_REG;
16737 internal_error(state, ins, "bad type passed to copy");
16741 size = size_of(state, ins->type);
16743 ins->template_id = TEMPLATE_PHI8;
16745 else if (size <= 2) {
16746 ins->template_id = TEMPLATE_PHI16;
16748 else if (size <= 4) {
16749 ins->template_id = TEMPLATE_PHI32;
16752 internal_error(state, ins, "bad type passed to phi");
16756 switch(ins->type->type & TYPE_MASK) {
16757 case TYPE_CHAR: case TYPE_UCHAR:
16758 ins->template_id = TEMPLATE_STORE8;
16760 case TYPE_SHORT: case TYPE_USHORT:
16761 ins->template_id = TEMPLATE_STORE16;
16763 case TYPE_INT: case TYPE_UINT:
16764 case TYPE_LONG: case TYPE_ULONG:
16766 ins->template_id = TEMPLATE_STORE32;
16769 internal_error(state, ins, "unknown type in store");
16774 switch(ins->type->type & TYPE_MASK) {
16775 case TYPE_CHAR: case TYPE_UCHAR:
16776 ins->template_id = TEMPLATE_LOAD8;
16780 ins->template_id = TEMPLATE_LOAD16;
16787 ins->template_id = TEMPLATE_LOAD32;
16790 internal_error(state, ins, "unknown type in load");
16800 ins->template_id = TEMPLATE_BINARY32_REG;
16801 if (get_imm32(ins, &RHS(ins, 1))) {
16802 ins->template_id = TEMPLATE_BINARY32_IMM;
16807 ins->template_id = TEMPLATE_DIV32;
16808 next = after_lhs(state, ins);
16810 /* FIXME UMUL does not work yet.. */
16812 ins->template_id = TEMPLATE_UMUL32;
16815 next = mod_div(state, ins, OP_UDIVT, 0);
16818 next = mod_div(state, ins, OP_SDIVT, 0);
16821 next = mod_div(state, ins, OP_UDIVT, 1);
16824 next = mod_div(state, ins, OP_SDIVT, 1);
16829 ins->template_id = TEMPLATE_SL32_CL;
16830 if (get_imm8(ins, &RHS(ins, 1))) {
16831 ins->template_id = TEMPLATE_SL32_IMM;
16832 } else if (size_of(state, RHS(ins, 1)->type) > 1) {
16833 typed_pre_copy(state, &char_type, ins, 1);
16838 ins->template_id = TEMPLATE_UNARY32;
16841 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
16844 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
16847 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
16850 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
16853 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
16856 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
16859 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
16862 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
16865 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
16868 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
16871 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
16874 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
16877 if (TRIPLE_RHS(ins->sizes) > 0) {
16878 internal_error(state, ins, "bad branch test");
16881 ins->template_id = TEMPLATE_NOP;
16887 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
16888 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
16889 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
16891 if (get_imm8(ins, &RHS(ins, 0))) {
16892 ins->template_id += 1;
16899 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
16900 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
16901 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
16903 if (get_imm8(ins, &RHS(ins, 1))) {
16904 ins->template_id += 1;
16909 ins->template_id = TEMPLATE_BSF;
16912 ins->template_id = TEMPLATE_RDMSR;
16913 next = after_lhs(state, ins);
16916 ins->template_id = TEMPLATE_WRMSR;
16919 ins->template_id = TEMPLATE_NOP;
16922 ins->template_id = TEMPLATE_NOP;
16923 next = after_lhs(state, ins);
16925 /* Already transformed instructions */
16927 ins->template_id = TEMPLATE_TEST32;
16930 ins->template_id = TEMPLATE_CMP32_REG;
16931 if (get_imm32(ins, &RHS(ins, 1))) {
16932 ins->template_id = TEMPLATE_CMP32_IMM;
16935 case OP_JMP_EQ: case OP_JMP_NOTEQ:
16936 case OP_JMP_SLESS: case OP_JMP_ULESS:
16937 case OP_JMP_SMORE: case OP_JMP_UMORE:
16938 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
16939 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
16940 ins->template_id = TEMPLATE_JMP;
16942 case OP_SET_EQ: case OP_SET_NOTEQ:
16943 case OP_SET_SLESS: case OP_SET_ULESS:
16944 case OP_SET_SMORE: case OP_SET_UMORE:
16945 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
16946 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
16947 ins->template_id = TEMPLATE_SET;
16949 /* Unhandled instructions */
16952 internal_error(state, ins, "unhandled ins: %d %s\n",
16953 ins->op, tops(ins->op));
16959 static long next_label(struct compile_state *state)
16961 static long label_counter = 0;
16962 return ++label_counter;
16964 static void generate_local_labels(struct compile_state *state)
16966 struct triple *first, *label;
16967 first = RHS(state->main_function, 0);
16970 if ((label->op == OP_LABEL) ||
16971 (label->op == OP_SDECL)) {
16973 label->u.cval = next_label(state);
16979 label = label->next;
16980 } while(label != first);
16983 static int check_reg(struct compile_state *state,
16984 struct triple *triple, int classes)
16988 reg = ID_REG(triple->id);
16989 if (reg == REG_UNSET) {
16990 internal_error(state, triple, "register not set");
16992 mask = arch_reg_regcm(state, reg);
16993 if (!(classes & mask)) {
16994 internal_error(state, triple, "reg %d in wrong class",
17000 static const char *arch_reg_str(int reg)
17003 #error "Registers have renumberd fix arch_reg_str"
17005 static const char *regs[] = {
17009 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
17010 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
17011 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
17014 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
17015 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
17016 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
17018 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
17025 static const char *reg(struct compile_state *state, struct triple *triple,
17029 reg = check_reg(state, triple, classes);
17030 return arch_reg_str(reg);
17033 const char *type_suffix(struct compile_state *state, struct type *type)
17035 const char *suffix;
17036 switch(size_of(state, type)) {
17037 case 1: suffix = "b"; break;
17038 case 2: suffix = "w"; break;
17039 case 4: suffix = "l"; break;
17041 internal_error(state, 0, "unknown suffix");
17048 static void print_const_val(
17049 struct compile_state *state, struct triple *ins, FILE *fp)
17053 fprintf(fp, " $%ld ",
17054 (long_t)(ins->u.cval));
17057 if (MISC(ins, 0)->op != OP_SDECL) {
17058 internal_error(state, ins, "bad base for addrconst");
17060 if (MISC(ins, 0)->u.cval <= 0) {
17061 internal_error(state, ins, "unlabeled constant");
17063 fprintf(fp, " $L%s%lu+%lu ",
17064 state->label_prefix,
17065 MISC(ins, 0)->u.cval,
17069 internal_error(state, ins, "unknown constant type");
17074 static void print_const(struct compile_state *state,
17075 struct triple *ins, FILE *fp)
17079 switch(ins->type->type & TYPE_MASK) {
17082 fprintf(fp, ".byte 0x%02lx\n", ins->u.cval);
17086 fprintf(fp, ".short 0x%04lx\n", ins->u.cval);
17092 fprintf(fp, ".int %lu\n", ins->u.cval);
17095 internal_error(state, ins, "Unknown constant type");
17099 if (MISC(ins, 0)->op != OP_SDECL) {
17100 internal_error(state, ins, "bad base for addrconst");
17102 if (MISC(ins, 0)->u.cval <= 0) {
17103 internal_error(state, ins, "unlabeled constant");
17105 fprintf(fp, ".int L%s%lu+%lu\n",
17106 state->label_prefix,
17107 MISC(ins, 0)->u.cval,
17112 unsigned char *blob;
17114 size = size_of(state, ins->type);
17115 blob = ins->u.blob;
17116 for(i = 0; i < size; i++) {
17117 fprintf(fp, ".byte 0x%02x\n",
17123 internal_error(state, ins, "Unknown constant type");
17128 #define TEXT_SECTION ".rom.text"
17129 #define DATA_SECTION ".rom.data"
17131 static long get_const_pool_ref(
17132 struct compile_state *state, struct triple *ins, FILE *fp)
17135 ref = next_label(state);
17136 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
17137 fprintf(fp, ".balign %d\n", align_of(state, ins->type));
17138 fprintf(fp, "L%s%lu:\n", state->label_prefix, ref);
17139 print_const(state, ins, fp);
17140 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
17144 static void print_binary_op(struct compile_state *state,
17145 const char *op, struct triple *ins, FILE *fp)
17148 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
17149 if (RHS(ins, 0)->id != ins->id) {
17150 internal_error(state, ins, "invalid register assignment");
17152 if (is_const(RHS(ins, 1))) {
17153 fprintf(fp, "\t%s ", op);
17154 print_const_val(state, RHS(ins, 1), fp);
17155 fprintf(fp, ", %s\n",
17156 reg(state, RHS(ins, 0), mask));
17159 unsigned lmask, rmask;
17161 lreg = check_reg(state, RHS(ins, 0), mask);
17162 rreg = check_reg(state, RHS(ins, 1), mask);
17163 lmask = arch_reg_regcm(state, lreg);
17164 rmask = arch_reg_regcm(state, rreg);
17165 mask = lmask & rmask;
17166 fprintf(fp, "\t%s %s, %s\n",
17168 reg(state, RHS(ins, 1), mask),
17169 reg(state, RHS(ins, 0), mask));
17172 static void print_unary_op(struct compile_state *state,
17173 const char *op, struct triple *ins, FILE *fp)
17176 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
17177 fprintf(fp, "\t%s %s\n",
17179 reg(state, RHS(ins, 0), mask));
17182 static void print_op_shift(struct compile_state *state,
17183 const char *op, struct triple *ins, FILE *fp)
17186 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
17187 if (RHS(ins, 0)->id != ins->id) {
17188 internal_error(state, ins, "invalid register assignment");
17190 if (is_const(RHS(ins, 1))) {
17191 fprintf(fp, "\t%s ", op);
17192 print_const_val(state, RHS(ins, 1), fp);
17193 fprintf(fp, ", %s\n",
17194 reg(state, RHS(ins, 0), mask));
17197 fprintf(fp, "\t%s %s, %s\n",
17199 reg(state, RHS(ins, 1), REGCM_GPR8_LO),
17200 reg(state, RHS(ins, 0), mask));
17204 static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
17211 case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
17212 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
17213 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
17215 internal_error(state, ins, "not an in operation");
17219 dreg = check_reg(state, ins, mask);
17220 if (!reg_is_reg(state, dreg, REG_EAX)) {
17221 internal_error(state, ins, "dst != %%eax");
17223 if (is_const(RHS(ins, 0))) {
17224 fprintf(fp, "\t%s ", op);
17225 print_const_val(state, RHS(ins, 0), fp);
17226 fprintf(fp, ", %s\n",
17227 reg(state, ins, mask));
17231 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
17232 if (!reg_is_reg(state, addr_reg, REG_DX)) {
17233 internal_error(state, ins, "src != %%dx");
17235 fprintf(fp, "\t%s %s, %s\n",
17237 reg(state, RHS(ins, 0), REGCM_GPR16),
17238 reg(state, ins, mask));
17242 static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
17249 case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
17250 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
17251 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
17253 internal_error(state, ins, "not an out operation");
17257 lreg = check_reg(state, RHS(ins, 0), mask);
17258 if (!reg_is_reg(state, lreg, REG_EAX)) {
17259 internal_error(state, ins, "src != %%eax");
17261 if (is_const(RHS(ins, 1))) {
17262 fprintf(fp, "\t%s %s,",
17263 op, reg(state, RHS(ins, 0), mask));
17264 print_const_val(state, RHS(ins, 1), fp);
17269 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
17270 if (!reg_is_reg(state, addr_reg, REG_DX)) {
17271 internal_error(state, ins, "dst != %%dx");
17273 fprintf(fp, "\t%s %s, %s\n",
17275 reg(state, RHS(ins, 0), mask),
17276 reg(state, RHS(ins, 1), REGCM_GPR16));
17280 static void print_op_move(struct compile_state *state,
17281 struct triple *ins, FILE *fp)
17283 /* op_move is complex because there are many types
17284 * of registers we can move between.
17285 * Because OP_COPY will be introduced in arbitrary locations
17286 * OP_COPY must not affect flags.
17288 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
17289 struct triple *dst, *src;
17290 if (ins->op == OP_COPY) {
17295 internal_error(state, ins, "unknown move operation");
17298 if (!is_const(src)) {
17299 int src_reg, dst_reg;
17300 int src_regcm, dst_regcm;
17301 src_reg = ID_REG(src->id);
17302 dst_reg = ID_REG(dst->id);
17303 src_regcm = arch_reg_regcm(state, src_reg);
17304 dst_regcm = arch_reg_regcm(state, dst_reg);
17305 /* If the class is the same just move the register */
17306 if (src_regcm & dst_regcm &
17307 (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
17308 if ((src_reg != dst_reg) || !omit_copy) {
17309 fprintf(fp, "\tmov %s, %s\n",
17310 reg(state, src, src_regcm),
17311 reg(state, dst, dst_regcm));
17314 /* Move 32bit to 16bit */
17315 else if ((src_regcm & REGCM_GPR32) &&
17316 (dst_regcm & REGCM_GPR16)) {
17317 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
17318 if ((src_reg != dst_reg) || !omit_copy) {
17319 fprintf(fp, "\tmovw %s, %s\n",
17320 arch_reg_str(src_reg),
17321 arch_reg_str(dst_reg));
17324 /* Move from 32bit gprs to 16bit gprs */
17325 else if ((src_regcm & REGCM_GPR32) &&
17326 (dst_regcm & REGCM_GPR16)) {
17327 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
17328 if ((src_reg != dst_reg) || !omit_copy) {
17329 fprintf(fp, "\tmov %s, %s\n",
17330 arch_reg_str(src_reg),
17331 arch_reg_str(dst_reg));
17334 /* Move 32bit to 8bit */
17335 else if ((src_regcm & REGCM_GPR32_8) &&
17336 (dst_regcm & REGCM_GPR8_LO))
17338 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
17339 if ((src_reg != dst_reg) || !omit_copy) {
17340 fprintf(fp, "\tmovb %s, %s\n",
17341 arch_reg_str(src_reg),
17342 arch_reg_str(dst_reg));
17345 /* Move 16bit to 8bit */
17346 else if ((src_regcm & REGCM_GPR16_8) &&
17347 (dst_regcm & REGCM_GPR8_LO))
17349 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
17350 if ((src_reg != dst_reg) || !omit_copy) {
17351 fprintf(fp, "\tmovb %s, %s\n",
17352 arch_reg_str(src_reg),
17353 arch_reg_str(dst_reg));
17356 /* Move 8/16bit to 16/32bit */
17357 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) &&
17358 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
17360 op = is_signed(src->type)? "movsx": "movzx";
17361 fprintf(fp, "\t%s %s, %s\n",
17363 reg(state, src, src_regcm),
17364 reg(state, dst, dst_regcm));
17366 /* Move between sse registers */
17367 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
17368 if ((src_reg != dst_reg) || !omit_copy) {
17369 fprintf(fp, "\tmovdqa %s, %s\n",
17370 reg(state, src, src_regcm),
17371 reg(state, dst, dst_regcm));
17374 /* Move between mmx registers */
17375 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
17376 if ((src_reg != dst_reg) || !omit_copy) {
17377 fprintf(fp, "\tmovq %s, %s\n",
17378 reg(state, src, src_regcm),
17379 reg(state, dst, dst_regcm));
17382 /* Move from sse to mmx registers */
17383 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
17384 fprintf(fp, "\tmovdq2q %s, %s\n",
17385 reg(state, src, src_regcm),
17386 reg(state, dst, dst_regcm));
17388 /* Move from mmx to sse registers */
17389 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
17390 fprintf(fp, "\tmovq2dq %s, %s\n",
17391 reg(state, src, src_regcm),
17392 reg(state, dst, dst_regcm));
17394 /* Move between 32bit gprs & mmx/sse registers */
17395 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
17396 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
17397 fprintf(fp, "\tmovd %s, %s\n",
17398 reg(state, src, src_regcm),
17399 reg(state, dst, dst_regcm));
17401 /* Move from 16bit gprs & mmx/sse registers */
17402 else if ((src_regcm & REGCM_GPR16) &&
17403 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
17406 op = is_signed(src->type)? "movsx":"movzx";
17407 mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
17408 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
17410 arch_reg_str(src_reg),
17411 arch_reg_str(mid_reg),
17412 arch_reg_str(mid_reg),
17413 arch_reg_str(dst_reg));
17415 /* Move from mmx/sse registers to 16bit gprs */
17416 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
17417 (dst_regcm & REGCM_GPR16)) {
17418 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
17419 fprintf(fp, "\tmovd %s, %s\n",
17420 arch_reg_str(src_reg),
17421 arch_reg_str(dst_reg));
17423 /* Move from gpr to 64bit dividend */
17424 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) &&
17425 (dst_regcm & REGCM_DIVIDEND64)) {
17426 const char *extend;
17427 extend = is_signed(src->type)? "cltd":"movl $0, %edx";
17428 fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
17429 arch_reg_str(src_reg),
17432 /* Move from 64bit gpr to gpr */
17433 else if ((src_regcm & REGCM_DIVIDEND64) &&
17434 (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
17435 if (dst_regcm & REGCM_GPR32) {
17438 else if (dst_regcm & REGCM_GPR16) {
17441 else if (dst_regcm & REGCM_GPR8_LO) {
17444 fprintf(fp, "\tmov %s, %s\n",
17445 arch_reg_str(src_reg),
17446 arch_reg_str(dst_reg));
17448 /* Move from mmx/sse registers to 64bit gpr */
17449 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
17450 (dst_regcm & REGCM_DIVIDEND64)) {
17451 const char *extend;
17452 extend = is_signed(src->type)? "cltd": "movl $0, %edx";
17453 fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
17454 arch_reg_str(src_reg),
17457 /* Move from 64bit gpr to mmx/sse register */
17458 else if ((src_regcm & REGCM_DIVIDEND64) &&
17459 (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
17460 fprintf(fp, "\tmovd %%eax, %s\n",
17461 arch_reg_str(dst_reg));
17463 #if X86_4_8BIT_GPRS
17464 /* Move from 8bit gprs to mmx/sse registers */
17465 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
17466 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
17469 op = is_signed(src->type)? "movsx":"movzx";
17470 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
17471 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
17473 reg(state, src, src_regcm),
17474 arch_reg_str(mid_reg),
17475 arch_reg_str(mid_reg),
17476 reg(state, dst, dst_regcm));
17478 /* Move from mmx/sse registers and 8bit gprs */
17479 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
17480 (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
17482 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
17483 fprintf(fp, "\tmovd %s, %s\n",
17484 reg(state, src, src_regcm),
17485 arch_reg_str(mid_reg));
17487 /* Move from 32bit gprs to 8bit gprs */
17488 else if ((src_regcm & REGCM_GPR32) &&
17489 (dst_regcm & REGCM_GPR8_LO)) {
17490 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
17491 if ((src_reg != dst_reg) || !omit_copy) {
17492 fprintf(fp, "\tmov %s, %s\n",
17493 arch_reg_str(src_reg),
17494 arch_reg_str(dst_reg));
17497 /* Move from 16bit gprs to 8bit gprs */
17498 else if ((src_regcm & REGCM_GPR16) &&
17499 (dst_regcm & REGCM_GPR8_LO)) {
17500 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
17501 if ((src_reg != dst_reg) || !omit_copy) {
17502 fprintf(fp, "\tmov %s, %s\n",
17503 arch_reg_str(src_reg),
17504 arch_reg_str(dst_reg));
17507 #endif /* X86_4_8BIT_GPRS */
17509 internal_error(state, ins, "unknown copy type");
17515 dst_reg = ID_REG(dst->id);
17516 dst_regcm = arch_reg_regcm(state, dst_reg);
17517 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
17518 fprintf(fp, "\tmov ");
17519 print_const_val(state, src, fp);
17520 fprintf(fp, ", %s\n",
17521 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
17523 else if (dst_regcm & REGCM_DIVIDEND64) {
17524 if (size_of(state, dst->type) > 4) {
17525 internal_error(state, ins, "64bit constant...");
17527 fprintf(fp, "\tmov $0, %%edx\n");
17528 fprintf(fp, "\tmov ");
17529 print_const_val(state, src, fp);
17530 fprintf(fp, ", %%eax\n");
17532 else if (dst_regcm & REGCM_DIVIDEND32) {
17533 if (size_of(state, dst->type) > 2) {
17534 internal_error(state, ins, "32bit constant...");
17536 fprintf(fp, "\tmov $0, %%dx\n");
17537 fprintf(fp, "\tmov ");
17538 print_const_val(state, src, fp);
17539 fprintf(fp, ", %%ax");
17541 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
17543 ref = get_const_pool_ref(state, src, fp);
17544 fprintf(fp, "\tmovq L%s%lu, %s\n",
17545 state->label_prefix, ref,
17546 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
17549 internal_error(state, ins, "unknown copy immediate type");
17554 static void print_op_load(struct compile_state *state,
17555 struct triple *ins, FILE *fp)
17557 struct triple *dst, *src;
17560 if (is_const(src) || is_const(dst)) {
17561 internal_error(state, ins, "unknown load operation");
17563 fprintf(fp, "\tmov (%s), %s\n",
17564 reg(state, src, REGCM_GPR32),
17565 reg(state, dst, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32));
17569 static void print_op_store(struct compile_state *state,
17570 struct triple *ins, FILE *fp)
17572 struct triple *dst, *src;
17575 if (is_const(src) && (src->op == OP_INTCONST)) {
17577 value = (long_t)(src->u.cval);
17578 fprintf(fp, "\tmov%s $%ld, (%s)\n",
17579 type_suffix(state, src->type),
17581 reg(state, dst, REGCM_GPR32));
17583 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
17584 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
17585 type_suffix(state, src->type),
17586 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
17590 if (is_const(src) || is_const(dst)) {
17591 internal_error(state, ins, "unknown store operation");
17593 fprintf(fp, "\tmov%s %s, (%s)\n",
17594 type_suffix(state, src->type),
17595 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
17596 reg(state, dst, REGCM_GPR32));
17602 static void print_op_smul(struct compile_state *state,
17603 struct triple *ins, FILE *fp)
17605 if (!is_const(RHS(ins, 1))) {
17606 fprintf(fp, "\timul %s, %s\n",
17607 reg(state, RHS(ins, 1), REGCM_GPR32),
17608 reg(state, RHS(ins, 0), REGCM_GPR32));
17611 fprintf(fp, "\timul ");
17612 print_const_val(state, RHS(ins, 1), fp);
17613 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
17617 static void print_op_cmp(struct compile_state *state,
17618 struct triple *ins, FILE *fp)
17622 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
17623 dreg = check_reg(state, ins, REGCM_FLAGS);
17624 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
17625 internal_error(state, ins, "bad dest register for cmp");
17627 if (is_const(RHS(ins, 1))) {
17628 fprintf(fp, "\tcmp ");
17629 print_const_val(state, RHS(ins, 1), fp);
17630 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
17633 unsigned lmask, rmask;
17635 lreg = check_reg(state, RHS(ins, 0), mask);
17636 rreg = check_reg(state, RHS(ins, 1), mask);
17637 lmask = arch_reg_regcm(state, lreg);
17638 rmask = arch_reg_regcm(state, rreg);
17639 mask = lmask & rmask;
17640 fprintf(fp, "\tcmp %s, %s\n",
17641 reg(state, RHS(ins, 1), mask),
17642 reg(state, RHS(ins, 0), mask));
17646 static void print_op_test(struct compile_state *state,
17647 struct triple *ins, FILE *fp)
17650 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
17651 fprintf(fp, "\ttest %s, %s\n",
17652 reg(state, RHS(ins, 0), mask),
17653 reg(state, RHS(ins, 0), mask));
17656 static void print_op_branch(struct compile_state *state,
17657 struct triple *branch, FILE *fp)
17659 const char *bop = "j";
17660 if (branch->op == OP_JMP) {
17661 if (TRIPLE_RHS(branch->sizes) != 0) {
17662 internal_error(state, branch, "jmp with condition?");
17667 struct triple *ptr;
17668 if (TRIPLE_RHS(branch->sizes) != 1) {
17669 internal_error(state, branch, "jmpcc without condition?");
17671 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
17672 if ((RHS(branch, 0)->op != OP_CMP) &&
17673 (RHS(branch, 0)->op != OP_TEST)) {
17674 internal_error(state, branch, "bad branch test");
17676 #warning "FIXME I have observed instructions between the test and branch instructions"
17677 ptr = RHS(branch, 0);
17678 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
17679 if (ptr->op != OP_COPY) {
17680 internal_error(state, branch, "branch does not follow test");
17683 switch(branch->op) {
17684 case OP_JMP_EQ: bop = "jz"; break;
17685 case OP_JMP_NOTEQ: bop = "jnz"; break;
17686 case OP_JMP_SLESS: bop = "jl"; break;
17687 case OP_JMP_ULESS: bop = "jb"; break;
17688 case OP_JMP_SMORE: bop = "jg"; break;
17689 case OP_JMP_UMORE: bop = "ja"; break;
17690 case OP_JMP_SLESSEQ: bop = "jle"; break;
17691 case OP_JMP_ULESSEQ: bop = "jbe"; break;
17692 case OP_JMP_SMOREEQ: bop = "jge"; break;
17693 case OP_JMP_UMOREEQ: bop = "jae"; break;
17695 internal_error(state, branch, "Invalid branch op");
17700 fprintf(fp, "\t%s L%s%lu\n",
17702 state->label_prefix,
17703 TARG(branch, 0)->u.cval);
17706 static void print_op_set(struct compile_state *state,
17707 struct triple *set, FILE *fp)
17709 const char *sop = "set";
17710 if (TRIPLE_RHS(set->sizes) != 1) {
17711 internal_error(state, set, "setcc without condition?");
17713 check_reg(state, RHS(set, 0), REGCM_FLAGS);
17714 if ((RHS(set, 0)->op != OP_CMP) &&
17715 (RHS(set, 0)->op != OP_TEST)) {
17716 internal_error(state, set, "bad set test");
17718 if (RHS(set, 0)->next != set) {
17719 internal_error(state, set, "set does not follow test");
17722 case OP_SET_EQ: sop = "setz"; break;
17723 case OP_SET_NOTEQ: sop = "setnz"; break;
17724 case OP_SET_SLESS: sop = "setl"; break;
17725 case OP_SET_ULESS: sop = "setb"; break;
17726 case OP_SET_SMORE: sop = "setg"; break;
17727 case OP_SET_UMORE: sop = "seta"; break;
17728 case OP_SET_SLESSEQ: sop = "setle"; break;
17729 case OP_SET_ULESSEQ: sop = "setbe"; break;
17730 case OP_SET_SMOREEQ: sop = "setge"; break;
17731 case OP_SET_UMOREEQ: sop = "setae"; break;
17733 internal_error(state, set, "Invalid set op");
17736 fprintf(fp, "\t%s %s\n",
17737 sop, reg(state, set, REGCM_GPR8_LO));
17740 static void print_op_bit_scan(struct compile_state *state,
17741 struct triple *ins, FILE *fp)
17745 case OP_BSF: op = "bsf"; break;
17746 case OP_BSR: op = "bsr"; break;
17748 internal_error(state, ins, "unknown bit scan");
17758 reg(state, RHS(ins, 0), REGCM_GPR32),
17759 reg(state, ins, REGCM_GPR32),
17760 reg(state, ins, REGCM_GPR32));
17764 static void print_sdecl(struct compile_state *state,
17765 struct triple *ins, FILE *fp)
17767 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
17768 fprintf(fp, ".balign %d\n", align_of(state, ins->type));
17769 fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
17770 print_const(state, MISC(ins, 0), fp);
17771 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
17775 static void print_instruction(struct compile_state *state,
17776 struct triple *ins, FILE *fp)
17778 /* Assumption: after I have exted the register allocator
17779 * everything is in a valid register.
17783 print_op_asm(state, ins, fp);
17785 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
17786 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
17787 case OP_AND: print_binary_op(state, "and", ins, fp); break;
17788 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
17789 case OP_OR: print_binary_op(state, "or", ins, fp); break;
17790 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
17791 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
17792 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
17793 case OP_POS: break;
17794 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
17795 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
17799 /* Don't generate anything here for constants */
17801 /* Don't generate anything for variable declarations. */
17804 print_sdecl(state, ins, fp);
17807 print_op_move(state, ins, fp);
17810 print_op_load(state, ins, fp);
17813 print_op_store(state, ins, fp);
17816 print_op_smul(state, ins, fp);
17818 case OP_CMP: print_op_cmp(state, ins, fp); break;
17819 case OP_TEST: print_op_test(state, ins, fp); break;
17821 case OP_JMP_EQ: case OP_JMP_NOTEQ:
17822 case OP_JMP_SLESS: case OP_JMP_ULESS:
17823 case OP_JMP_SMORE: case OP_JMP_UMORE:
17824 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
17825 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
17826 print_op_branch(state, ins, fp);
17828 case OP_SET_EQ: case OP_SET_NOTEQ:
17829 case OP_SET_SLESS: case OP_SET_ULESS:
17830 case OP_SET_SMORE: case OP_SET_UMORE:
17831 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
17832 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
17833 print_op_set(state, ins, fp);
17835 case OP_INB: case OP_INW: case OP_INL:
17836 print_op_in(state, ins, fp);
17838 case OP_OUTB: case OP_OUTW: case OP_OUTL:
17839 print_op_out(state, ins, fp);
17843 print_op_bit_scan(state, ins, fp);
17846 after_lhs(state, ins);
17847 fprintf(fp, "\trdmsr\n");
17850 fprintf(fp, "\twrmsr\n");
17853 fprintf(fp, "\thlt\n");
17856 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
17859 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
17862 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
17868 fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
17870 /* Ignore OP_PIECE */
17873 /* Operations that should never get here */
17874 case OP_SDIV: case OP_UDIV:
17875 case OP_SMOD: case OP_UMOD:
17876 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
17877 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
17878 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
17880 internal_error(state, ins, "unknown op: %d %s",
17881 ins->op, tops(ins->op));
17886 static void print_instructions(struct compile_state *state)
17888 struct triple *first, *ins;
17889 int print_location;
17890 struct occurance *last_occurance;
17892 int max_inline_depth;
17893 max_inline_depth = 0;
17894 print_location = 1;
17895 last_occurance = 0;
17896 fp = state->output;
17897 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
17898 first = RHS(state->main_function, 0);
17901 if (print_location &&
17902 last_occurance != ins->occurance) {
17903 if (!ins->occurance->parent) {
17904 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
17905 ins->occurance->function,
17906 ins->occurance->filename,
17907 ins->occurance->line,
17908 ins->occurance->col);
17911 struct occurance *ptr;
17913 fprintf(fp, "\t/*\n");
17915 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
17917 fprintf(fp, "\t * %s,%s:%d.%d\n",
17923 fprintf(fp, "\t */\n");
17924 if (inline_depth > max_inline_depth) {
17925 max_inline_depth = inline_depth;
17928 if (last_occurance) {
17929 put_occurance(last_occurance);
17931 get_occurance(ins->occurance);
17932 last_occurance = ins->occurance;
17935 print_instruction(state, ins, fp);
17937 } while(ins != first);
17938 if (print_location) {
17939 fprintf(fp, "/* max inline depth %d */\n",
17944 static void generate_code(struct compile_state *state)
17946 generate_local_labels(state);
17947 print_instructions(state);
17951 static void print_tokens(struct compile_state *state)
17954 tk = &state->token[0];
17959 next_token(state, 0);
17961 loc(stdout, state, 0);
17962 printf("%s <- `%s'\n",
17964 tk->ident ? tk->ident->name :
17965 tk->str_len ? tk->val.str : "");
17967 } while(tk->tok != TOK_EOF);
17970 static void compile(const char *filename, const char *ofilename,
17971 int cpu, int debug, int opt, const char *label_prefix)
17974 struct compile_state state;
17975 memset(&state, 0, sizeof(state));
17977 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
17978 memset(&state.token[i], 0, sizeof(state.token[i]));
17979 state.token[i].tok = -1;
17981 /* Remember the debug settings */
17983 state.debug = debug;
17984 state.optimize = opt;
17985 /* Remember the output filename */
17986 state.ofilename = ofilename;
17987 state.output = fopen(state.ofilename, "w");
17988 if (!state.output) {
17989 error(&state, 0, "Cannot open output file %s\n",
17992 /* Remember the label prefix */
17993 state.label_prefix = label_prefix;
17994 /* Prep the preprocessor */
17995 state.if_depth = 0;
17996 state.if_value = 0;
17997 /* register the C keywords */
17998 register_keywords(&state);
17999 /* register the keywords the macro preprocessor knows */
18000 register_macro_keywords(&state);
18001 /* Memorize where some special keywords are. */
18002 state.i_continue = lookup(&state, "continue", 8);
18003 state.i_break = lookup(&state, "break", 5);
18004 /* Enter the globl definition scope */
18005 start_scope(&state);
18006 register_builtins(&state);
18007 compile_file(&state, filename, 1);
18009 print_tokens(&state);
18012 /* Exit the global definition scope */
18015 /* Now that basic compilation has happened
18016 * optimize the intermediate code
18020 generate_code(&state);
18022 fprintf(stderr, "done\n");
18026 static void version(void)
18028 printf("romcc " VERSION " released " RELEASE_DATE "\n");
18031 static void usage(void)
18035 "Usage: romcc <source>.c\n"
18036 "Compile a C source file without using ram\n"
18040 static void arg_error(char *fmt, ...)
18043 va_start(args, fmt);
18044 vfprintf(stderr, fmt, args);
18050 int main(int argc, char **argv)
18052 const char *filename;
18053 const char *ofilename;
18054 const char *label_prefix;
18061 ofilename = "auto.inc";
18065 while((argc > 1) && (argc != last_argc)) {
18067 if (strncmp(argv[1], "--debug=", 8) == 0) {
18068 debug = atoi(argv[1] + 8);
18072 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
18073 label_prefix= argv[1] + 15;
18077 else if ((strcmp(argv[1],"-O") == 0) ||
18078 (strcmp(argv[1], "-O1") == 0)) {
18083 else if (strcmp(argv[1],"-O2") == 0) {
18088 else if ((strcmp(argv[1], "-o") == 0) && (argc > 2)) {
18089 ofilename = argv[2];
18093 else if (strncmp(argv[1], "-mcpu=", 6) == 0) {
18094 cpu = arch_encode_cpu(argv[1] + 6);
18095 if (cpu == BAD_CPU) {
18096 arg_error("Invalid cpu specified: %s\n",
18104 arg_error("Wrong argument count %d\n", argc);
18106 filename = argv[1];
18107 compile(filename, ofilename, cpu, debug, optimize, label_prefix);