5 #define VERSION_MAJOR "0"
6 #define VERSION_MINOR "65"
7 #define RELEASE_DATE "8 November 2004"
8 #define VERSION VERSION_MAJOR "." VERSION_MINOR
15 #include <sys/types.h>
25 #define MAX_CWD_SIZE 4096
26 #define MAX_ALLOCATION_PASSES 100
28 #define DEBUG_CONSISTENCY 1
29 #define DEBUG_SDP_BLOCKS 0
30 #define DEBUG_TRIPLE_COLOR 0
32 #define DEBUG_DISPLAY_USES 1
33 #define DEBUG_DISPLAY_TYPES 1
34 #define DEBUG_REPLACE_CLOSURE_TYPE_HIRES 0
35 #define DEBUG_DECOMPOSE_PRINT_TUPLES 0
36 #define DEBUG_DECOMPOSE_HIRES 0
37 #define DEBUG_INITIALIZER 0
38 #define DEBUG_UPDATE_CLOSURE_TYPE 0
39 #define DEBUG_LOCAL_TRIPLE 0
40 #define DEBUG_BASIC_BLOCKS_VERBOSE 0
41 #define DEBUG_CPS_RENAME_VARIABLES_HIRES 0
42 #define DEBUG_SIMPLIFY_HIRES 0
43 #define DEBUG_SHRINKING 0
44 #define DEBUG_COALESCE_HITCHES 0
45 #define DEBUG_CODE_ELIMINATION 0
47 #define DEBUG_EXPLICIT_CLOSURES 0
49 #warning "FIXME give clear error messages about unused variables"
50 #warning "FIXME properly handle multi dimensional arrays"
51 #warning "FIXME handle multiple register sizes"
53 /* Control flow graph of a loop without goto.
64 * |\ GGG HHH | continue;
92 * DFlocal(X) = { Y <- Succ(X) | idom(Y) != X }
93 * DFup(Z) = { Y <- DF(Z) | idom(Y) != X }
96 * [] == DFlocal(X) U DF(X)
99 * Dominator graph of the same nodes.
103 * BBB JJJ BBB: [ JJJ ] ( JJJ ) JJJ: [ ] ()
105 * CCC CCC: [ ] ( BBB, JJJ )
107 * DDD EEE DDD: [ ] ( BBB ) EEE: [ JJJ ] ()
109 * FFF FFF: [ ] ( BBB )
111 * GGG HHH GGG: [ ] ( BBB ) HHH: [ BBB ] ()
113 * III III: [ BBB ] ()
116 * BBB and JJJ are definitely the dominance frontier.
117 * Where do I place phi functions and how do I make that decision.
120 static void die(char *fmt, ...)
125 vfprintf(stderr, fmt, args);
132 static void *xmalloc(size_t size, const char *name)
137 die("Cannot malloc %ld bytes to hold %s: %s\n",
138 size + 0UL, name, strerror(errno));
143 static void *xcmalloc(size_t size, const char *name)
146 buf = xmalloc(size, name);
147 memset(buf, 0, size);
151 static void *xrealloc(void *ptr, size_t size, const char *name)
154 buf = realloc(ptr, size);
156 die("Cannot realloc %ld bytes to hold %s: %s\n",
157 size + 0UL, name, strerror(errno));
162 static void xfree(const void *ptr)
167 static char *xstrdup(const char *str)
172 new = xmalloc(len + 1, "xstrdup string");
173 memcpy(new, str, len);
178 static void xchdir(const char *path)
180 if (chdir(path) != 0) {
181 die("chdir to `%s' failed: %s\n",
182 path, strerror(errno));
186 static int exists(const char *dirname, const char *filename)
188 char cwd[MAX_CWD_SIZE];
191 if (getcwd(cwd, sizeof(cwd)) == 0) {
192 die("cwd buffer to small");
196 if (chdir(dirname) != 0) {
199 if (does_exist && (access(filename, O_RDONLY) < 0)) {
200 if ((errno != EACCES) && (errno != EROFS)) {
209 static char *slurp_file(const char *dirname, const char *filename, off_t *r_size)
211 char cwd[MAX_CWD_SIZE];
214 off_t size, progress;
222 if (getcwd(cwd, sizeof(cwd)) == 0) {
223 die("cwd buffer to small");
226 fd = open(filename, O_RDONLY);
229 die("Cannot open '%s' : %s\n",
230 filename, strerror(errno));
232 result = fstat(fd, &stats);
234 die("Cannot stat: %s: %s\n",
235 filename, strerror(errno));
237 size = stats.st_size;
239 buf = xmalloc(size +2, filename);
240 buf[size] = '\n'; /* Make certain the file is newline terminated */
241 buf[size+1] = '\0'; /* Null terminate the file for good measure */
243 while(progress < size) {
244 result = read(fd, buf + progress, size - progress);
246 if ((errno == EINTR) || (errno == EAGAIN))
248 die("read on %s of %ld bytes failed: %s\n",
249 filename, (size - progress)+ 0UL, strerror(errno));
255 die("Close of %s failed: %s\n",
256 filename, strerror(errno));
261 /* Types on the destination platform */
262 #warning "FIXME this assumes 32bit x86 is the destination"
263 typedef int8_t schar_t;
264 typedef uint8_t uchar_t;
265 typedef int8_t char_t;
266 typedef int16_t short_t;
267 typedef uint16_t ushort_t;
268 typedef int32_t int_t;
269 typedef uint32_t uint_t;
270 typedef int32_t long_t;
271 typedef uint32_t ulong_t;
273 #define SCHAR_T_MIN (-128)
274 #define SCHAR_T_MAX 127
275 #define UCHAR_T_MAX 255
276 #define CHAR_T_MIN SCHAR_T_MIN
277 #define CHAR_T_MAX SCHAR_T_MAX
278 #define SHRT_T_MIN (-32768)
279 #define SHRT_T_MAX 32767
280 #define USHRT_T_MAX 65535
281 #define INT_T_MIN (-LONG_T_MAX - 1)
282 #define INT_T_MAX 2147483647
283 #define UINT_T_MAX 4294967295U
284 #define LONG_T_MIN (-LONG_T_MAX - 1)
285 #define LONG_T_MAX 2147483647
286 #define ULONG_T_MAX 4294967295U
289 #define SIZEOF_I16 16
290 #define SIZEOF_I32 32
291 #define SIZEOF_I64 64
293 #define SIZEOF_CHAR 8
294 #define SIZEOF_SHORT 16
295 #define SIZEOF_INT 32
296 #define SIZEOF_LONG (sizeof(long_t)*SIZEOF_CHAR)
299 #define ALIGNOF_CHAR 8
300 #define ALIGNOF_SHORT 16
301 #define ALIGNOF_INT 32
302 #define ALIGNOF_LONG (sizeof(long_t)*SIZEOF_CHAR)
304 #define REG_SIZEOF_REG 32
305 #define REG_SIZEOF_CHAR REG_SIZEOF_REG
306 #define REG_SIZEOF_SHORT REG_SIZEOF_REG
307 #define REG_SIZEOF_INT REG_SIZEOF_REG
308 #define REG_SIZEOF_LONG REG_SIZEOF_REG
310 #define REG_ALIGNOF_REG REG_SIZEOF_REG
311 #define REG_ALIGNOF_CHAR REG_SIZEOF_REG
312 #define REG_ALIGNOF_SHORT REG_SIZEOF_REG
313 #define REG_ALIGNOF_INT REG_SIZEOF_REG
314 #define REG_ALIGNOF_LONG REG_SIZEOF_REG
316 /* Additional definitions for clarity.
317 * I currently assume a long is the largest native
318 * machine word and that a pointer fits into it.
320 #define SIZEOF_WORD SIZEOF_LONG
321 #define SIZEOF_POINTER SIZEOF_LONG
322 #define ALIGNOF_WORD ALIGNOF_LONG
323 #define ALIGNOF_POINTER ALIGNOF_LONG
324 #define REG_SIZEOF_POINTER REG_SIZEOF_LONG
325 #define REG_ALIGNOF_POINTER REG_ALIGNOF_LONG
328 struct file_state *prev;
329 const char *basename;
335 const char *line_start;
337 const char *report_name;
338 const char *report_dir;
343 struct hash_entry *ident;
352 /* I have two classes of types:
354 * Logical types. (The type the C standard says the operation is of)
356 * The operational types are:
371 * No memory is useable by the compiler.
372 * There is no floating point support.
373 * All operations take place in general purpose registers.
374 * There is one type of general purpose register.
375 * Unsigned longs are stored in that general purpose register.
378 /* Operations on general purpose registers.
397 #define OP_POS 16 /* Dummy positive operator don't use it */
407 #define OP_SLESSEQ 26
408 #define OP_ULESSEQ 27
409 #define OP_SMOREEQ 28
410 #define OP_UMOREEQ 29
412 #define OP_LFALSE 30 /* Test if the expression is logically false */
413 #define OP_LTRUE 31 /* Test if the expression is logcially true */
417 /* For OP_STORE ->type holds the type
418 * RHS(0) holds the destination address
419 * RHS(1) holds the value to store.
422 #define OP_UEXTRACT 34
423 /* OP_UEXTRACT extracts an unsigned bitfield from a pseudo register
424 * RHS(0) holds the psuedo register to extract from
425 * ->type holds the size of the bitfield.
426 * ->u.bitfield.size holds the size of the bitfield.
427 * ->u.bitfield.offset holds the offset to extract from
429 #define OP_SEXTRACT 35
430 /* OP_SEXTRACT extracts a signed bitfield from a pseudo register
431 * RHS(0) holds the psuedo register to extract from
432 * ->type holds the size of the bitfield.
433 * ->u.bitfield.size holds the size of the bitfield.
434 * ->u.bitfield.offset holds the offset to extract from
436 #define OP_DEPOSIT 36
437 /* OP_DEPOSIT replaces a bitfield with a new value.
438 * RHS(0) holds the value to replace a bitifield in.
439 * RHS(1) holds the replacement value
440 * ->u.bitfield.size holds the size of the bitfield.
441 * ->u.bitfield.offset holds the deposit into
446 #define OP_MIN_CONST 50
447 #define OP_MAX_CONST 58
448 #define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
449 #define OP_INTCONST 50
450 /* For OP_INTCONST ->type holds the type.
451 * ->u.cval holds the constant value.
453 #define OP_BLOBCONST 51
454 /* For OP_BLOBCONST ->type holds the layout and size
455 * information. u.blob holds a pointer to the raw binary
456 * data for the constant initializer.
458 #define OP_ADDRCONST 52
459 /* For OP_ADDRCONST ->type holds the type.
460 * MISC(0) holds the reference to the static variable.
461 * ->u.cval holds an offset from that value.
463 #define OP_UNKNOWNVAL 59
464 /* For OP_UNKNOWNAL ->type holds the type.
465 * For some reason we don't know what value this type has.
466 * This allows for variables that have don't have values
467 * assigned yet, or variables whose value we simply do not know.
471 /* OP_WRITE moves one pseudo register to another.
472 * MISC(0) holds the destination pseudo register, which must be an OP_DECL.
473 * RHS(0) holds the psuedo to move.
477 /* OP_READ reads the value of a variable and makes
478 * it available for the pseudo operation.
479 * Useful for things like def-use chains.
480 * RHS(0) holds points to the triple to read from.
483 /* OP_COPY makes a copy of the pseudo register or constant in RHS(0).
485 #define OP_CONVERT 63
486 /* OP_CONVERT makes a copy of the pseudo register or constant in RHS(0).
487 * And then the type is converted appropriately.
490 /* OP_PIECE returns one piece of a instruction that returns a structure.
491 * MISC(0) is the instruction
492 * u.cval is the LHS piece of the instruction to return.
495 /* OP_ASM holds a sequence of assembly instructions, the result
496 * of a C asm directive.
497 * RHS(x) holds input value x to the assembly sequence.
498 * LHS(x) holds the output value x from the assembly sequence.
499 * u.blob holds the string of assembly instructions.
503 /* OP_DEREF generates an lvalue from a pointer.
504 * RHS(0) holds the pointer value.
505 * OP_DEREF serves as a place holder to indicate all necessary
506 * checks have been done to indicate a value is an lvalue.
509 /* OP_DOT references a submember of a structure lvalue.
510 * MISC(0) holds the lvalue.
511 * ->u.field holds the name of the field we want.
513 * Not seen after structures are flattened.
516 /* OP_INDEX references a submember of a tuple or array lvalue.
517 * MISC(0) holds the lvalue.
518 * ->u.cval holds the index into the lvalue.
520 * Not seen after structures are flattened.
523 /* OP_VAL returns the value of a subexpression of the current expression.
524 * Useful for operators that have side effects.
525 * RHS(0) holds the expression.
526 * MISC(0) holds the subexpression of RHS(0) that is the
527 * value of the expression.
529 * Not seen outside of expressions.
533 /* OP_TUPLE is an array of triples that are either variable
534 * or values for a structure or an array. It is used as
535 * a place holder when flattening compound types.
536 * The value represented by an OP_TUPLE is held in N registers.
537 * LHS(0..N-1) refer to those registers.
538 * ->use is a list of statements that use the value.
540 * Although OP_TUPLE always has register sized pieces they are not
541 * used until structures are flattened/decomposed into their register
543 * ???? registers ????
547 /* OP_BITREF describes a bitfield as an lvalue.
548 * RHS(0) holds the register value.
549 * ->type holds the type of the bitfield.
550 * ->u.bitfield.size holds the size of the bitfield.
551 * ->u.bitfield.offset holds the offset of the bitfield in the register
556 /* OP_FCALL performs a procedure call.
557 * MISC(0) holds a pointer to the OP_LIST of a function
558 * RHS(x) holds argument x of a function
560 * Currently not seen outside of expressions.
563 /* OP_PROG is an expression that holds a list of statements, or
564 * expressions. The final expression is the value of the expression.
565 * RHS(0) holds the start of the list.
570 /* OP_LIST Holds a list of statements that compose a function, and a result value.
571 * RHS(0) holds the list of statements.
572 * A list of all functions is maintained.
575 #define OP_BRANCH 81 /* an unconditional branch */
576 /* For branch instructions
577 * TARG(0) holds the branch target.
578 * ->next holds where to branch to if the branch is not taken.
579 * The branch target can only be a label
582 #define OP_CBRANCH 82 /* a conditional branch */
583 /* For conditional branch instructions
584 * RHS(0) holds the branch condition.
585 * TARG(0) holds the branch target.
586 * ->next holds where to branch to if the branch is not taken.
587 * The branch target can only be a label
590 #define OP_CALL 83 /* an uncontional branch that will return */
591 /* For call instructions
592 * MISC(0) holds the OP_RET that returns from the branch
593 * TARG(0) holds the branch target.
594 * ->next holds where to branch to if the branch is not taken.
595 * The branch target can only be a label
598 #define OP_RET 84 /* an uncontinonal branch through a variable back to an OP_CALL */
599 /* For call instructions
600 * RHS(0) holds the variable with the return address
601 * The branch target can only be a label
605 /* OP_LABEL is a triple that establishes an target for branches.
606 * ->use is the list of all branches that use this label.
610 /* OP_ADECL is a triple that establishes an lvalue for assignments.
611 * A variable takes N registers to contain.
612 * LHS(0..N-1) refer to an OP_PIECE triple that represents
613 * the Xth register that the variable is stored in.
614 * ->use is a list of statements that use the variable.
616 * Although OP_ADECL always has register sized pieces they are not
617 * used until structures are flattened/decomposed into their register
622 /* OP_SDECL is a triple that establishes a variable of static
624 * ->use is a list of statements that use the variable.
625 * MISC(0) holds the initializer expression.
630 /* OP_PHI is a triple used in SSA form code.
631 * It is used when multiple code paths merge and a variable needs
632 * a single assignment from any of those code paths.
633 * The operation is a cross between OP_DECL and OP_WRITE, which
634 * is what OP_PHI is generated from.
636 * RHS(x) points to the value from code path x
637 * The number of RHS entries is the number of control paths into the block
638 * in which OP_PHI resides. The elements of the array point to point
639 * to the variables OP_PHI is derived from.
641 * MISC(0) holds a pointer to the orginal OP_DECL node.
645 /* continuation helpers
647 #define OP_CPS_BRANCH 90 /* an unconditional branch */
648 /* OP_CPS_BRANCH calls a continuation
649 * RHS(x) holds argument x of the function
650 * TARG(0) holds OP_CPS_START target
652 #define OP_CPS_CBRANCH 91 /* a conditional branch */
653 /* OP_CPS_CBRANCH conditionally calls one of two continuations
654 * RHS(0) holds the branch condition
655 * RHS(x + 1) holds argument x of the function
656 * TARG(0) holds the OP_CPS_START to jump to when true
657 * ->next holds the OP_CPS_START to jump to when false
659 #define OP_CPS_CALL 92 /* an uncontional branch that will return */
660 /* For OP_CPS_CALL instructions
661 * RHS(x) holds argument x of the function
662 * MISC(0) holds the OP_CPS_RET that returns from the branch
663 * TARG(0) holds the branch target.
664 * ->next holds where the OP_CPS_RET will return to.
666 #define OP_CPS_RET 93
667 /* OP_CPS_RET conditionally calls one of two continuations
668 * RHS(0) holds the variable with the return function address
669 * RHS(x + 1) holds argument x of the function
670 * The branch target may be any OP_CPS_START
672 #define OP_CPS_END 94
673 /* OP_CPS_END is the triple at the end of the program.
674 * For most practical purposes it is a branch.
676 #define OP_CPS_START 95
677 /* OP_CPS_START is a triple at the start of a continuation
678 * The arguments variables takes N registers to contain.
679 * LHS(0..N-1) refer to an OP_PIECE triple that represents
680 * the Xth register that the arguments are stored in.
684 /* Architecture specific instructions */
687 #define OP_SET_EQ 102
688 #define OP_SET_NOTEQ 103
689 #define OP_SET_SLESS 104
690 #define OP_SET_ULESS 105
691 #define OP_SET_SMORE 106
692 #define OP_SET_UMORE 107
693 #define OP_SET_SLESSEQ 108
694 #define OP_SET_ULESSEQ 109
695 #define OP_SET_SMOREEQ 110
696 #define OP_SET_UMOREEQ 111
699 #define OP_JMP_EQ 113
700 #define OP_JMP_NOTEQ 114
701 #define OP_JMP_SLESS 115
702 #define OP_JMP_ULESS 116
703 #define OP_JMP_SMORE 117
704 #define OP_JMP_UMORE 118
705 #define OP_JMP_SLESSEQ 119
706 #define OP_JMP_ULESSEQ 120
707 #define OP_JMP_SMOREEQ 121
708 #define OP_JMP_UMOREEQ 122
710 /* Builtin operators that it is just simpler to use the compiler for */
726 #define PURE 0x001 /* Triple has no side effects */
727 #define IMPURE 0x002 /* Triple has side effects */
728 #define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
729 #define DEF 0x004 /* Triple is a variable definition */
730 #define BLOCK 0x008 /* Triple stores the current block */
731 #define STRUCTURAL 0x010 /* Triple does not generate a machine instruction */
732 #define BRANCH_BITS(FLAGS) ((FLAGS) & 0xe0 )
733 #define UBRANCH 0x020 /* Triple is an unconditional branch instruction */
734 #define CBRANCH 0x040 /* Triple is a conditional branch instruction */
735 #define RETBRANCH 0x060 /* Triple is a return instruction */
736 #define CALLBRANCH 0x080 /* Triple is a call instruction */
737 #define ENDBRANCH 0x0a0 /* Triple is an end instruction */
738 #define PART 0x100 /* Triple is really part of another triple */
739 #define BITFIELD 0x200 /* Triple manipulates a bitfield */
740 signed char lhs, rhs, misc, targ;
743 #define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \
751 static const struct op_info table_ops[] = {
752 [OP_SDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "sdivt"),
753 [OP_UDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "udivt"),
754 [OP_SMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smul"),
755 [OP_UMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umul"),
756 [OP_SDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sdiv"),
757 [OP_UDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "udiv"),
758 [OP_SMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smod"),
759 [OP_UMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umod"),
760 [OP_ADD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "add"),
761 [OP_SUB ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sub"),
762 [OP_SL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sl"),
763 [OP_USR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "usr"),
764 [OP_SSR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ssr"),
765 [OP_AND ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "and"),
766 [OP_XOR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "xor"),
767 [OP_OR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "or"),
768 [OP_POS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "pos"),
769 [OP_NEG ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "neg"),
770 [OP_INVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "invert"),
772 [OP_EQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "eq"),
773 [OP_NOTEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "noteq"),
774 [OP_SLESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sless"),
775 [OP_ULESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "uless"),
776 [OP_SMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smore"),
777 [OP_UMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umore"),
778 [OP_SLESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "slesseq"),
779 [OP_ULESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ulesseq"),
780 [OP_SMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smoreeq"),
781 [OP_UMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umoreeq"),
782 [OP_LFALSE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "lfalse"),
783 [OP_LTRUE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "ltrue"),
785 [OP_LOAD ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "load"),
786 [OP_STORE ] = OP( 0, 2, 0, 0, PURE | BLOCK , "store"),
788 [OP_UEXTRACT ] = OP( 0, 1, 0, 0, PURE | DEF | BITFIELD, "uextract"),
789 [OP_SEXTRACT ] = OP( 0, 1, 0, 0, PURE | DEF | BITFIELD, "sextract"),
790 [OP_DEPOSIT ] = OP( 0, 2, 0, 0, PURE | DEF | BITFIELD, "deposit"),
792 [OP_NOOP ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "noop"),
794 [OP_INTCONST ] = OP( 0, 0, 0, 0, PURE | DEF, "intconst"),
795 [OP_BLOBCONST ] = OP( 0, 0, 0, 0, PURE , "blobconst"),
796 [OP_ADDRCONST ] = OP( 0, 0, 1, 0, PURE | DEF, "addrconst"),
797 [OP_UNKNOWNVAL ] = OP( 0, 0, 0, 0, PURE | DEF, "unknown"),
799 #warning "FIXME is it correct for OP_WRITE to be a def? I currently use it as one..."
800 [OP_WRITE ] = OP( 0, 1, 1, 0, PURE | DEF | BLOCK, "write"),
801 [OP_READ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "read"),
802 [OP_COPY ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "copy"),
803 [OP_CONVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "convert"),
804 [OP_PIECE ] = OP( 0, 0, 1, 0, PURE | DEF | STRUCTURAL | PART, "piece"),
805 [OP_ASM ] = OP(-1, -1, 0, 0, PURE, "asm"),
806 [OP_DEREF ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "deref"),
807 [OP_DOT ] = OP( 0, 0, 1, 0, PURE | DEF | PART, "dot"),
808 [OP_INDEX ] = OP( 0, 0, 1, 0, PURE | DEF | PART, "index"),
810 [OP_VAL ] = OP( 0, 1, 1, 0, 0 | DEF | BLOCK, "val"),
811 [OP_TUPLE ] = OP(-1, 0, 0, 0, 0 | PURE | BLOCK | STRUCTURAL, "tuple"),
812 [OP_BITREF ] = OP( 0, 1, 0, 0, 0 | DEF | PURE | STRUCTURAL | BITFIELD, "bitref"),
813 /* Call is special most it can stand in for anything so it depends on context */
814 [OP_FCALL ] = OP( 0, -1, 1, 0, 0 | BLOCK | CALLBRANCH, "fcall"),
815 [OP_PROG ] = OP( 0, 1, 0, 0, 0 | IMPURE | BLOCK | STRUCTURAL, "prog"),
816 /* The sizes of OP_FCALL depends upon context */
818 [OP_LIST ] = OP( 0, 1, 1, 0, 0 | DEF | STRUCTURAL, "list"),
819 [OP_BRANCH ] = OP( 0, 0, 0, 1, PURE | BLOCK | UBRANCH, "branch"),
820 [OP_CBRANCH ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "cbranch"),
821 [OP_CALL ] = OP( 0, 0, 1, 1, PURE | BLOCK | CALLBRANCH, "call"),
822 [OP_RET ] = OP( 0, 1, 0, 0, PURE | BLOCK | RETBRANCH, "ret"),
823 [OP_LABEL ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "label"),
824 [OP_ADECL ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "adecl"),
825 [OP_SDECL ] = OP( 0, 0, 1, 0, PURE | BLOCK | STRUCTURAL, "sdecl"),
826 /* The number of RHS elements of OP_PHI depend upon context */
827 [OP_PHI ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
830 [OP_CPS_BRANCH ] = OP( 0, -1, 0, 1, PURE | BLOCK | UBRANCH, "cps_branch"),
831 [OP_CPS_CBRANCH] = OP( 0, -1, 0, 1, PURE | BLOCK | CBRANCH, "cps_cbranch"),
832 [OP_CPS_CALL ] = OP( 0, -1, 1, 1, PURE | BLOCK | CALLBRANCH, "cps_call"),
833 [OP_CPS_RET ] = OP( 0, -1, 0, 0, PURE | BLOCK | RETBRANCH, "cps_ret"),
834 [OP_CPS_END ] = OP( 0, -1, 0, 0, IMPURE | BLOCK | ENDBRANCH, "cps_end"),
835 [OP_CPS_START ] = OP( -1, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "cps_start"),
838 [OP_CMP ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK, "cmp"),
839 [OP_TEST ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "test"),
840 [OP_SET_EQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_eq"),
841 [OP_SET_NOTEQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_noteq"),
842 [OP_SET_SLESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_sless"),
843 [OP_SET_ULESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_uless"),
844 [OP_SET_SMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smore"),
845 [OP_SET_UMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umore"),
846 [OP_SET_SLESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_slesseq"),
847 [OP_SET_ULESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_ulesseq"),
848 [OP_SET_SMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smoreq"),
849 [OP_SET_UMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umoreq"),
850 [OP_JMP ] = OP( 0, 0, 0, 1, PURE | BLOCK | UBRANCH, "jmp"),
851 [OP_JMP_EQ ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_eq"),
852 [OP_JMP_NOTEQ ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_noteq"),
853 [OP_JMP_SLESS ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_sless"),
854 [OP_JMP_ULESS ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_uless"),
855 [OP_JMP_SMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_smore"),
856 [OP_JMP_UMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_umore"),
857 [OP_JMP_SLESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_slesseq"),
858 [OP_JMP_ULESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_ulesseq"),
859 [OP_JMP_SMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_smoreq"),
860 [OP_JMP_UMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_umoreq"),
862 [OP_INB ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inb"),
863 [OP_INW ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inw"),
864 [OP_INL ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inl"),
865 [OP_OUTB ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outb"),
866 [OP_OUTW ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outw"),
867 [OP_OUTL ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outl"),
868 [OP_BSF ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsf"),
869 [OP_BSR ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsr"),
870 [OP_RDMSR ] = OP( 2, 1, 0, 0, IMPURE | BLOCK, "__rdmsr"),
871 [OP_WRMSR ] = OP( 0, 3, 0, 0, IMPURE | BLOCK, "__wrmsr"),
872 [OP_HLT ] = OP( 0, 0, 0, 0, IMPURE | BLOCK, "__hlt"),
875 #define OP_MAX (sizeof(table_ops)/sizeof(table_ops[0]))
877 static const char *tops(int index)
879 static const char unknown[] = "unknown op";
883 if (index > OP_MAX) {
886 return table_ops[index].name;
893 struct triple_set *next;
894 struct triple *member;
904 const char *filename;
905 const char *function;
908 struct occurance *parent;
915 struct triple *next, *prev;
916 struct triple_set *use;
919 unsigned int template_id : 7;
920 unsigned int lhs : 6;
921 unsigned int rhs : 7;
922 unsigned int misc : 2;
923 unsigned int targ : 1;
924 #define TRIPLE_SIZE(TRIPLE) \
925 ((TRIPLE)->lhs + (TRIPLE)->rhs + (TRIPLE)->misc + (TRIPLE)->targ)
926 #define TRIPLE_LHS_OFF(PTR) (0)
927 #define TRIPLE_RHS_OFF(PTR) (TRIPLE_LHS_OFF(PTR) + (PTR)->lhs)
928 #define TRIPLE_MISC_OFF(PTR) (TRIPLE_RHS_OFF(PTR) + (PTR)->rhs)
929 #define TRIPLE_TARG_OFF(PTR) (TRIPLE_MISC_OFF(PTR) + (PTR)->misc)
930 #define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF(PTR) + (INDEX)])
931 #define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF(PTR) + (INDEX)])
932 #define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF(PTR) + (INDEX)])
933 #define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF(PTR) + (INDEX)])
934 unsigned id; /* A scratch value and finally the register */
935 #define TRIPLE_FLAG_FLATTENED (1 << 31)
936 #define TRIPLE_FLAG_PRE_SPLIT (1 << 30)
937 #define TRIPLE_FLAG_POST_SPLIT (1 << 29)
938 #define TRIPLE_FLAG_VOLATILE (1 << 28)
939 #define TRIPLE_FLAG_INLINE (1 << 27) /* ???? */
940 #define TRIPLE_FLAG_LOCAL (1 << 26)
942 #define TRIPLE_FLAG_COPY TRIPLE_FLAG_VOLATILE
943 struct occurance *occurance;
946 struct bitfield bitfield;
949 struct hash_entry *field;
950 struct asm_info *ainfo;
952 struct symbol *symbol;
954 struct triple *param[2];
961 struct ins_template {
962 struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
966 struct ins_template tmpl;
971 struct block_set *next;
972 struct block *member;
975 struct block *work_next;
976 struct triple *first, *last;
978 struct block_set *edges;
980 struct block_set *use;
981 struct block_set *idominates;
982 struct block_set *domfrontier;
984 struct block_set *ipdominates;
985 struct block_set *ipdomfrontier;
993 struct hash_entry *ident;
1000 struct macro_arg *next;
1001 struct hash_entry *ident;
1004 struct hash_entry *ident;
1008 struct macro_arg *args;
1013 struct hash_entry *next;
1017 struct macro *sym_define;
1018 struct symbol *sym_label;
1019 struct symbol *sym_tag;
1020 struct symbol *sym_ident;
1023 #define HASH_TABLE_SIZE 2048
1025 struct compiler_state {
1026 const char *label_prefix;
1027 const char *ofilename;
1028 unsigned long flags;
1029 unsigned long debug;
1030 unsigned long max_allocation_passes;
1032 size_t include_path_count;
1033 const char **include_paths;
1035 size_t define_count;
1036 const char **defines;
1039 const char **undefs;
1042 unsigned long features;
1044 struct basic_blocks {
1045 struct triple *func;
1046 struct triple *first;
1047 struct block *first_block, *last_block;
1050 #define MAX_CPP_IF_DEPTH 63
1051 struct compile_state {
1052 struct compiler_state *compiler;
1053 struct arch_state *arch;
1057 struct file_state *file;
1058 struct occurance *last_occurance;
1059 const char *function;
1061 struct token token[6];
1062 struct hash_entry *hash_table[HASH_TABLE_SIZE];
1063 struct hash_entry *i_switch;
1064 struct hash_entry *i_case;
1065 struct hash_entry *i_continue;
1066 struct hash_entry *i_break;
1067 struct hash_entry *i_default;
1068 struct hash_entry *i_return;
1069 /* Additional hash entries for predefined macros */
1070 struct hash_entry *i_defined;
1071 struct hash_entry *i___VA_ARGS__;
1072 struct hash_entry *i___FILE__;
1073 struct hash_entry *i___LINE__;
1074 /* Additional hash entries for predefined identifiers */
1075 struct hash_entry *i___func__;
1076 /* Additional hash entries for attributes */
1077 struct hash_entry *i_noinline;
1078 struct hash_entry *i_always_inline;
1080 unsigned char if_bytes[(MAX_CPP_IF_DEPTH + CHAR_BIT -1)/CHAR_BIT];
1082 int eat_depth, eat_targ;
1084 struct file_state *macro_file;
1085 struct triple *functions;
1086 struct triple *main_function;
1087 struct triple *first;
1088 struct triple *global_pool;
1089 struct basic_blocks bb;
1090 int functions_joined;
1093 /* visibility global/local */
1094 /* static/auto duration */
1095 /* typedef, register, inline */
1096 #define STOR_SHIFT 0
1097 #define STOR_MASK 0x001f
1099 #define STOR_GLOBAL 0x0001
1101 #define STOR_PERM 0x0002
1102 /* Definition locality */
1103 #define STOR_NONLOCAL 0x0004 /* The definition is not in this translation unit */
1104 /* Storage specifiers */
1105 #define STOR_AUTO 0x0000
1106 #define STOR_STATIC 0x0002
1107 #define STOR_LOCAL 0x0003
1108 #define STOR_EXTERN 0x0007
1109 #define STOR_INLINE 0x0008
1110 #define STOR_REGISTER 0x0010
1111 #define STOR_TYPEDEF 0x0018
1113 #define QUAL_SHIFT 5
1114 #define QUAL_MASK 0x00e0
1115 #define QUAL_NONE 0x0000
1116 #define QUAL_CONST 0x0020
1117 #define QUAL_VOLATILE 0x0040
1118 #define QUAL_RESTRICT 0x0080
1120 #define TYPE_SHIFT 8
1121 #define TYPE_MASK 0x1f00
1122 #define TYPE_INTEGER(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
1123 #define TYPE_ARITHMETIC(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
1124 #define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
1125 #define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
1126 #define TYPE_MKUNSIGNED(TYPE) (((TYPE) & ~0xF000) | 0x0100)
1127 #define TYPE_RANK(TYPE) ((TYPE) & ~0xF1FF)
1128 #define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER)
1129 #define TYPE_DEFAULT 0x0000
1130 #define TYPE_VOID 0x0100
1131 #define TYPE_CHAR 0x0200
1132 #define TYPE_UCHAR 0x0300
1133 #define TYPE_SHORT 0x0400
1134 #define TYPE_USHORT 0x0500
1135 #define TYPE_INT 0x0600
1136 #define TYPE_UINT 0x0700
1137 #define TYPE_LONG 0x0800
1138 #define TYPE_ULONG 0x0900
1139 #define TYPE_LLONG 0x0a00 /* long long */
1140 #define TYPE_ULLONG 0x0b00
1141 #define TYPE_FLOAT 0x0c00
1142 #define TYPE_DOUBLE 0x0d00
1143 #define TYPE_LDOUBLE 0x0e00 /* long double */
1145 /* Note: TYPE_ENUM is chosen very carefully so TYPE_RANK works */
1146 #define TYPE_ENUM 0x1600
1147 #define TYPE_LIST 0x1700
1148 /* TYPE_LIST is a basic building block when defining enumerations
1149 * type->field_ident holds the name of this enumeration entry.
1150 * type->right holds the entry in the list.
1153 #define TYPE_STRUCT 0x1000
1155 * type->left holds the link list of TYPE_PRODUCT entries that
1156 * make up the structure.
1157 * type->elements hold the length of the linked list
1159 #define TYPE_UNION 0x1100
1161 * type->left holds the link list of TYPE_OVERLAP entries that
1162 * make up the union.
1163 * type->elements hold the length of the linked list
1165 #define TYPE_POINTER 0x1200
1166 /* For TYPE_POINTER:
1167 * type->left holds the type pointed to.
1169 #define TYPE_FUNCTION 0x1300
1170 /* For TYPE_FUNCTION:
1171 * type->left holds the return type.
1172 * type->right holds the type of the arguments
1173 * type->elements holds the count of the arguments
1175 #define TYPE_PRODUCT 0x1400
1176 /* TYPE_PRODUCT is a basic building block when defining structures
1177 * type->left holds the type that appears first in memory.
1178 * type->right holds the type that appears next in memory.
1180 #define TYPE_OVERLAP 0x1500
1181 /* TYPE_OVERLAP is a basic building block when defining unions
1182 * type->left and type->right holds to types that overlap
1183 * each other in memory.
1185 #define TYPE_ARRAY 0x1800
1186 /* TYPE_ARRAY is a basic building block when definitng arrays.
1187 * type->left holds the type we are an array of.
1188 * type->elements holds the number of elements.
1190 #define TYPE_TUPLE 0x1900
1191 /* TYPE_TUPLE is a basic building block when defining
1192 * positionally reference type conglomerations. (i.e. closures)
1193 * In essence it is a wrapper for TYPE_PRODUCT, like TYPE_STRUCT
1194 * except it has no field names.
1195 * type->left holds the liked list of TYPE_PRODUCT entries that
1196 * make up the closure type.
1197 * type->elements hold the number of elements in the closure.
1199 #define TYPE_JOIN 0x1a00
1200 /* TYPE_JOIN is a basic building block when defining
1201 * positionally reference type conglomerations. (i.e. closures)
1202 * In essence it is a wrapper for TYPE_OVERLAP, like TYPE_UNION
1203 * except it has no field names.
1204 * type->left holds the liked list of TYPE_OVERLAP entries that
1205 * make up the closure type.
1206 * type->elements hold the number of elements in the closure.
1208 #define TYPE_BITFIELD 0x1b00
1209 /* TYPE_BITFIED is the type of a bitfield.
1210 * type->left holds the type basic type TYPE_BITFIELD is derived from.
1211 * type->elements holds the number of bits in the bitfield.
1213 #define TYPE_UNKNOWN 0x1c00
1214 /* TYPE_UNKNOWN is the type of an unknown value.
1215 * Used on unknown consts and other places where I don't know the type.
1218 #define ATTRIB_SHIFT 16
1219 #define ATTRIB_MASK 0xffff0000
1220 #define ATTRIB_NOINLINE 0x00010000
1221 #define ATTRIB_ALWAYS_INLINE 0x00020000
1223 #define ELEMENT_COUNT_UNSPECIFIED ULONG_T_MAX
1227 struct type *left, *right;
1229 struct hash_entry *field_ident;
1230 struct hash_entry *type_ident;
1233 #define TEMPLATE_BITS 7
1234 #define MAX_TEMPLATES (1<<TEMPLATE_BITS)
1235 #define MAX_REG_EQUIVS 16
1237 #define MAX_REGISTERS 75
1238 #define REGISTER_BITS 7
1239 #define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
1242 #define REG_UNNEEDED 2
1243 #define REG_VIRT0 (MAX_REGISTERS + 0)
1244 #define REG_VIRT1 (MAX_REGISTERS + 1)
1245 #define REG_VIRT2 (MAX_REGISTERS + 2)
1246 #define REG_VIRT3 (MAX_REGISTERS + 3)
1247 #define REG_VIRT4 (MAX_REGISTERS + 4)
1248 #define REG_VIRT5 (MAX_REGISTERS + 5)
1249 #define REG_VIRT6 (MAX_REGISTERS + 6)
1250 #define REG_VIRT7 (MAX_REGISTERS + 7)
1251 #define REG_VIRT8 (MAX_REGISTERS + 8)
1252 #define REG_VIRT9 (MAX_REGISTERS + 9)
1254 #if (MAX_REGISTERS + 9) > MAX_VIRT_REGISTERS
1255 #error "MAX_VIRT_REGISTERS to small"
1257 #if (MAX_REGC + REGISTER_BITS) >= 26
1258 #error "Too many id bits used"
1261 /* Provision for 8 register classes */
1263 #define REGC_SHIFT REGISTER_BITS
1264 #define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
1265 #define REG_MASK (MAX_VIRT_REGISTERS -1)
1266 #define ID_REG(ID) ((ID) & REG_MASK)
1267 #define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
1268 #define ID_REGCM(ID) (((ID) & REGC_MASK) >> REGC_SHIFT)
1269 #define SET_REGCM(ID, REGCM) ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
1270 #define SET_INFO(ID, INFO) ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
1271 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
1273 #define ARCH_INPUT_REGS 4
1274 #define ARCH_OUTPUT_REGS 4
1276 static const struct reg_info arch_input_regs[ARCH_INPUT_REGS];
1277 static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS];
1278 static unsigned arch_reg_regcm(struct compile_state *state, int reg);
1279 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
1280 static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm);
1281 static void arch_reg_equivs(
1282 struct compile_state *state, unsigned *equiv, int reg);
1283 static int arch_select_free_register(
1284 struct compile_state *state, char *used, int classes);
1285 static unsigned arch_regc_size(struct compile_state *state, int class);
1286 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
1287 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
1288 static const char *arch_reg_str(int reg);
1289 static struct reg_info arch_reg_constraint(
1290 struct compile_state *state, struct type *type, const char *constraint);
1291 static struct reg_info arch_reg_clobber(
1292 struct compile_state *state, const char *clobber);
1293 static struct reg_info arch_reg_lhs(struct compile_state *state,
1294 struct triple *ins, int index);
1295 static struct reg_info arch_reg_rhs(struct compile_state *state,
1296 struct triple *ins, int index);
1297 static int arch_reg_size(int reg);
1298 static struct triple *transform_to_arch_instruction(
1299 struct compile_state *state, struct triple *ins);
1300 static struct triple *flatten(
1301 struct compile_state *state, struct triple *first, struct triple *ptr);
1306 #define DEBUG_ABORT_ON_ERROR 0x00000001
1307 #define DEBUG_BASIC_BLOCKS 0x00000002
1308 #define DEBUG_FDOMINATORS 0x00000004
1309 #define DEBUG_RDOMINATORS 0x00000008
1310 #define DEBUG_TRIPLES 0x00000010
1311 #define DEBUG_INTERFERENCE 0x00000020
1312 #define DEBUG_SCC_TRANSFORM 0x00000040
1313 #define DEBUG_SCC_TRANSFORM2 0x00000080
1314 #define DEBUG_REBUILD_SSA_FORM 0x00000100
1315 #define DEBUG_INLINE 0x00000200
1316 #define DEBUG_RANGE_CONFLICTS 0x00000400
1317 #define DEBUG_RANGE_CONFLICTS2 0x00000800
1318 #define DEBUG_COLOR_GRAPH 0x00001000
1319 #define DEBUG_COLOR_GRAPH2 0x00002000
1320 #define DEBUG_COALESCING 0x00004000
1321 #define DEBUG_COALESCING2 0x00008000
1322 #define DEBUG_VERIFICATION 0x00010000
1323 #define DEBUG_CALLS 0x00020000
1324 #define DEBUG_CALLS2 0x00040000
1325 #define DEBUG_TOKENS 0x80000000
1327 #define DEBUG_DEFAULT ( \
1328 DEBUG_ABORT_ON_ERROR | \
1329 DEBUG_BASIC_BLOCKS | \
1330 DEBUG_FDOMINATORS | \
1331 DEBUG_RDOMINATORS | \
1335 #define DEBUG_ALL ( \
1336 DEBUG_ABORT_ON_ERROR | \
1337 DEBUG_BASIC_BLOCKS | \
1338 DEBUG_FDOMINATORS | \
1339 DEBUG_RDOMINATORS | \
1341 DEBUG_INTERFERENCE | \
1342 DEBUG_SCC_TRANSFORM | \
1343 DEBUG_SCC_TRANSFORM2 | \
1344 DEBUG_REBUILD_SSA_FORM | \
1346 DEBUG_RANGE_CONFLICTS | \
1347 DEBUG_RANGE_CONFLICTS2 | \
1348 DEBUG_COLOR_GRAPH | \
1349 DEBUG_COLOR_GRAPH2 | \
1350 DEBUG_COALESCING | \
1351 DEBUG_COALESCING2 | \
1352 DEBUG_VERIFICATION | \
1358 #define COMPILER_INLINE_MASK 0x00000007
1359 #define COMPILER_INLINE_ALWAYS 0x00000000
1360 #define COMPILER_INLINE_NEVER 0x00000001
1361 #define COMPILER_INLINE_DEFAULTON 0x00000002
1362 #define COMPILER_INLINE_DEFAULTOFF 0x00000003
1363 #define COMPILER_INLINE_NOPENALTY 0x00000004
1364 #define COMPILER_ELIMINATE_INEFECTUAL_CODE 0x00000008
1365 #define COMPILER_SIMPLIFY 0x00000010
1366 #define COMPILER_SCC_TRANSFORM 0x00000020
1367 #define COMPILER_SIMPLIFY_OP 0x00000040
1368 #define COMPILER_SIMPLIFY_PHI 0x00000080
1369 #define COMPILER_SIMPLIFY_LABEL 0x00000100
1370 #define COMPILER_SIMPLIFY_BRANCH 0x00000200
1371 #define COMPILER_SIMPLIFY_COPY 0x00000400
1372 #define COMPILER_SIMPLIFY_ARITH 0x00000800
1373 #define COMPILER_SIMPLIFY_SHIFT 0x00001000
1374 #define COMPILER_SIMPLIFY_BITWISE 0x00002000
1375 #define COMPILER_SIMPLIFY_LOGICAL 0x00004000
1376 #define COMPILER_SIMPLIFY_BITFIELD 0x00008000
1378 #define COMPILER_CPP_ONLY 0x80000000
1380 #define COMPILER_DEFAULT_FLAGS ( \
1381 COMPILER_ELIMINATE_INEFECTUAL_CODE | \
1382 COMPILER_INLINE_DEFAULTON | \
1383 COMPILER_SIMPLIFY_OP | \
1384 COMPILER_SIMPLIFY_PHI | \
1385 COMPILER_SIMPLIFY_LABEL | \
1386 COMPILER_SIMPLIFY_BRANCH | \
1387 COMPILER_SIMPLIFY_COPY | \
1388 COMPILER_SIMPLIFY_ARITH | \
1389 COMPILER_SIMPLIFY_SHIFT | \
1390 COMPILER_SIMPLIFY_BITWISE | \
1391 COMPILER_SIMPLIFY_LOGICAL | \
1392 COMPILER_SIMPLIFY_BITFIELD | \
1395 #define GLOBAL_SCOPE_DEPTH 1
1396 #define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
1398 static void compile_file(struct compile_state *old_state, const char *filename, int local);
1402 static void init_compiler_state(struct compiler_state *compiler)
1404 memset(compiler, 0, sizeof(*compiler));
1405 compiler->label_prefix = "";
1406 compiler->ofilename = "auto.inc";
1407 compiler->flags = COMPILER_DEFAULT_FLAGS;
1408 compiler->debug = 0;
1409 compiler->max_allocation_passes = MAX_ALLOCATION_PASSES;
1410 compiler->include_path_count = 1;
1411 compiler->include_paths = xcmalloc(sizeof(char *), "include_paths");
1412 compiler->define_count = 1;
1413 compiler->defines = xcmalloc(sizeof(char *), "defines");
1414 compiler->undef_count = 1;
1415 compiler->undefs = xcmalloc(sizeof(char *), "undefs");
1418 struct compiler_flag {
1423 struct compiler_arg {
1426 struct compiler_flag flags[16];
1429 static int set_flag(
1430 const struct compiler_flag *ptr, unsigned long *flags,
1431 int act, const char *flag)
1434 for(; ptr->name; ptr++) {
1435 if (strcmp(ptr->name, flag) == 0) {
1441 *flags &= ~(ptr->flag);
1443 *flags |= ptr->flag;
1450 const struct compiler_arg *ptr, unsigned long *flags, const char *arg)
1455 val = strchr(arg, '=');
1459 for(; ptr->name; ptr++) {
1460 if (strncmp(ptr->name, arg, len) == 0) {
1465 *flags &= ~ptr->mask;
1466 result = set_flag(&ptr->flags[0], flags, 1, val);
1473 static void flag_usage(FILE *fp, const struct compiler_flag *ptr,
1474 const char *prefix, const char *invert_prefix)
1476 for(;ptr->name; ptr++) {
1477 fprintf(fp, "%s%s\n", prefix, ptr->name);
1478 if (invert_prefix) {
1479 fprintf(fp, "%s%s\n", invert_prefix, ptr->name);
1484 static void arg_usage(FILE *fp, const struct compiler_arg *ptr,
1487 for(;ptr->name; ptr++) {
1488 const struct compiler_flag *flag;
1489 for(flag = &ptr->flags[0]; flag->name; flag++) {
1490 fprintf(fp, "%s%s=%s\n",
1491 prefix, ptr->name, flag->name);
1496 static int append_string(size_t *max, const char ***vec, const char *str,
1501 *vec = xrealloc(*vec, sizeof(char *)*count, "name");
1502 (*vec)[count -1] = 0;
1503 (*vec)[count -2] = str;
1507 static void arg_error(char *fmt, ...);
1508 static const char *identifier(const char *str, const char *end);
1510 static int append_include_path(struct compiler_state *compiler, const char *str)
1513 if (!exists(str, ".")) {
1514 arg_error("Nonexistent include path: `%s'\n",
1517 result = append_string(&compiler->include_path_count,
1518 &compiler->include_paths, str, "include_paths");
1522 static int append_define(struct compiler_state *compiler, const char *str)
1524 const char *end, *rest;
1527 end = strchr(str, '=');
1529 end = str + strlen(str);
1531 rest = identifier(str, end);
1533 int len = end - str - 1;
1534 arg_error("Invalid name cannot define macro: `%*.*s'\n",
1537 result = append_string(&compiler->define_count,
1538 &compiler->defines, str, "defines");
1542 static int append_undef(struct compiler_state *compiler, const char *str)
1544 const char *end, *rest;
1547 end = str + strlen(str);
1548 rest = identifier(str, end);
1550 int len = end - str - 1;
1551 arg_error("Invalid name cannot undefine macro: `%*.*s'\n",
1554 result = append_string(&compiler->undef_count,
1555 &compiler->undefs, str, "undefs");
1559 static const struct compiler_flag romcc_flags[] = {
1560 { "cpp-only", COMPILER_CPP_ONLY },
1561 { "eliminate-inefectual-code", COMPILER_ELIMINATE_INEFECTUAL_CODE },
1562 { "simplify", COMPILER_SIMPLIFY },
1563 { "scc-transform", COMPILER_SCC_TRANSFORM },
1564 { "simplify-op", COMPILER_SIMPLIFY_OP },
1565 { "simplify-phi", COMPILER_SIMPLIFY_PHI },
1566 { "simplify-label", COMPILER_SIMPLIFY_LABEL },
1567 { "simplify-branch", COMPILER_SIMPLIFY_BRANCH },
1568 { "simplify-copy", COMPILER_SIMPLIFY_COPY },
1569 { "simplify-arith", COMPILER_SIMPLIFY_ARITH },
1570 { "simplify-shift", COMPILER_SIMPLIFY_SHIFT },
1571 { "simplify-bitwise", COMPILER_SIMPLIFY_BITWISE },
1572 { "simplify-logical", COMPILER_SIMPLIFY_LOGICAL },
1573 { "simplify-bitfield", COMPILER_SIMPLIFY_BITFIELD },
1576 static const struct compiler_arg romcc_args[] = {
1577 { "inline-policy", COMPILER_INLINE_MASK,
1579 { "always", COMPILER_INLINE_ALWAYS, },
1580 { "never", COMPILER_INLINE_NEVER, },
1581 { "defaulton", COMPILER_INLINE_DEFAULTON, },
1582 { "defaultoff", COMPILER_INLINE_DEFAULTOFF, },
1583 { "nopenalty", COMPILER_INLINE_NOPENALTY, },
1589 static const struct compiler_flag romcc_opt_flags[] = {
1590 { "-O", COMPILER_SIMPLIFY },
1591 { "-O2", COMPILER_SIMPLIFY | COMPILER_SCC_TRANSFORM },
1592 { "-E", COMPILER_CPP_ONLY },
1595 static const struct compiler_flag romcc_debug_flags[] = {
1596 { "all", DEBUG_ALL },
1597 { "abort-on-error", DEBUG_ABORT_ON_ERROR },
1598 { "basic-blocks", DEBUG_BASIC_BLOCKS },
1599 { "fdominators", DEBUG_FDOMINATORS },
1600 { "rdominators", DEBUG_RDOMINATORS },
1601 { "triples", DEBUG_TRIPLES },
1602 { "interference", DEBUG_INTERFERENCE },
1603 { "scc-transform", DEBUG_SCC_TRANSFORM },
1604 { "scc-transform2", DEBUG_SCC_TRANSFORM2 },
1605 { "rebuild-ssa-form", DEBUG_REBUILD_SSA_FORM },
1606 { "inline", DEBUG_INLINE },
1607 { "live-range-conflicts", DEBUG_RANGE_CONFLICTS },
1608 { "live-range-conflicts2", DEBUG_RANGE_CONFLICTS2 },
1609 { "color-graph", DEBUG_COLOR_GRAPH },
1610 { "color-graph2", DEBUG_COLOR_GRAPH2 },
1611 { "coalescing", DEBUG_COALESCING },
1612 { "coalescing2", DEBUG_COALESCING2 },
1613 { "verification", DEBUG_VERIFICATION },
1614 { "calls", DEBUG_CALLS },
1615 { "calls2", DEBUG_CALLS2 },
1616 { "tokens", DEBUG_TOKENS },
1620 static int compiler_encode_flag(
1621 struct compiler_state *compiler, const char *flag)
1628 if (strncmp(flag, "no-", 3) == 0) {
1632 if (strncmp(flag, "-O", 2) == 0) {
1633 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1635 else if (strncmp(flag, "-E", 2) == 0) {
1636 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1638 else if (strncmp(flag, "-I", 2) == 0) {
1639 result = append_include_path(compiler, flag + 2);
1641 else if (strncmp(flag, "-D", 2) == 0) {
1642 result = append_define(compiler, flag + 2);
1644 else if (strncmp(flag, "-U", 2) == 0) {
1645 result = append_undef(compiler, flag + 2);
1647 else if (act && strncmp(flag, "label-prefix=", 13) == 0) {
1649 compiler->label_prefix = flag + 13;
1651 else if (act && strncmp(flag, "max-allocation-passes=", 22) == 0) {
1652 unsigned long max_passes;
1654 max_passes = strtoul(flag + 22, &end, 10);
1655 if (end[0] == '\0') {
1657 compiler->max_allocation_passes = max_passes;
1660 else if (act && strcmp(flag, "debug") == 0) {
1662 compiler->debug |= DEBUG_DEFAULT;
1664 else if (strncmp(flag, "debug-", 6) == 0) {
1666 result = set_flag(romcc_debug_flags, &compiler->debug, act, flag);
1669 result = set_flag(romcc_flags, &compiler->flags, act, flag);
1671 result = set_arg(romcc_args, &compiler->flags, flag);
1677 static void compiler_usage(FILE *fp)
1679 flag_usage(fp, romcc_opt_flags, "", 0);
1680 flag_usage(fp, romcc_flags, "-f", "-fno-");
1681 arg_usage(fp, romcc_args, "-f");
1682 flag_usage(fp, romcc_debug_flags, "-fdebug-", "-fno-debug-");
1683 fprintf(fp, "-flabel-prefix=<prefix for assembly language labels>\n");
1684 fprintf(fp, "--label-prefix=<prefix for assembly language labels>\n");
1685 fprintf(fp, "-I<include path>\n");
1686 fprintf(fp, "-D<macro>[=defn]\n");
1687 fprintf(fp, "-U<macro>\n");
1690 static void do_cleanup(struct compile_state *state)
1692 if (state->output) {
1693 fclose(state->output);
1694 unlink(state->compiler->ofilename);
1697 if (state->dbgout) {
1698 fflush(state->dbgout);
1700 if (state->errout) {
1701 fflush(state->errout);
1705 static struct compile_state *exit_state;
1706 static void exit_cleanup(void)
1709 do_cleanup(exit_state);
1713 static int get_col(struct file_state *file)
1716 const char *ptr, *end;
1717 ptr = file->line_start;
1719 for(col = 0; ptr < end; ptr++) {
1724 col = (col & ~7) + 8;
1730 static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
1733 if (triple && triple->occurance) {
1734 struct occurance *spot;
1735 for(spot = triple->occurance; spot; spot = spot->parent) {
1736 fprintf(fp, "%s:%d.%d: ",
1737 spot->filename, spot->line, spot->col);
1744 col = get_col(state->file);
1745 fprintf(fp, "%s:%d.%d: ",
1746 state->file->report_name, state->file->report_line, col);
1749 static void internal_error(struct compile_state *state, struct triple *ptr,
1750 const char *fmt, ...)
1752 FILE *fp = state->errout;
1754 va_start(args, fmt);
1755 loc(fp, state, ptr);
1758 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
1760 fprintf(fp, "Internal compiler error: ");
1761 vfprintf(fp, fmt, args);
1769 static void internal_warning(struct compile_state *state, struct triple *ptr,
1770 const char *fmt, ...)
1772 FILE *fp = state->errout;
1774 va_start(args, fmt);
1775 loc(fp, state, ptr);
1777 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
1779 fprintf(fp, "Internal compiler warning: ");
1780 vfprintf(fp, fmt, args);
1787 static void error(struct compile_state *state, struct triple *ptr,
1788 const char *fmt, ...)
1790 FILE *fp = state->errout;
1792 va_start(args, fmt);
1793 loc(fp, state, ptr);
1795 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
1796 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
1798 vfprintf(fp, fmt, args);
1802 if (state->compiler->debug & DEBUG_ABORT_ON_ERROR) {
1808 static void warning(struct compile_state *state, struct triple *ptr,
1809 const char *fmt, ...)
1811 FILE *fp = state->errout;
1813 va_start(args, fmt);
1814 loc(fp, state, ptr);
1815 fprintf(fp, "warning: ");
1816 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
1817 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
1819 vfprintf(fp, fmt, args);
1824 #define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1826 static void valid_op(struct compile_state *state, int op)
1828 char *fmt = "invalid op: %d";
1830 internal_error(state, 0, fmt, op);
1833 internal_error(state, 0, fmt, op);
1837 static void valid_ins(struct compile_state *state, struct triple *ptr)
1839 valid_op(state, ptr->op);
1842 static void valid_param_count(struct compile_state *state, struct triple *ins)
1844 int lhs, rhs, misc, targ;
1845 valid_ins(state, ins);
1846 lhs = table_ops[ins->op].lhs;
1847 rhs = table_ops[ins->op].rhs;
1848 misc = table_ops[ins->op].misc;
1849 targ = table_ops[ins->op].targ;
1851 if ((lhs >= 0) && (ins->lhs != lhs)) {
1852 internal_error(state, ins, "Bad lhs count");
1854 if ((rhs >= 0) && (ins->rhs != rhs)) {
1855 internal_error(state, ins, "Bad rhs count");
1857 if ((misc >= 0) && (ins->misc != misc)) {
1858 internal_error(state, ins, "Bad misc count");
1860 if ((targ >= 0) && (ins->targ != targ)) {
1861 internal_error(state, ins, "Bad targ count");
1865 static void process_trigraphs(struct compile_state *state)
1867 char *src, *dest, *end;
1868 struct file_state *file;
1870 src = dest = file->buf;
1871 end = file->buf + file->size;
1872 while((end - src) >= 3) {
1873 if ((src[0] == '?') && (src[1] == '?')) {
1876 case '=': c = '#'; break;
1877 case '/': c = '\\'; break;
1878 case '\'': c = '^'; break;
1879 case '(': c = '['; break;
1880 case ')': c = ']'; break;
1881 case '!': c = '!'; break;
1882 case '<': c = '{'; break;
1883 case '>': c = '}'; break;
1884 case '-': c = '~'; break;
1901 file->size = dest - file->buf;
1904 static void splice_lines(struct compile_state *state)
1906 char *src, *dest, *end;
1907 struct file_state *file;
1909 src = dest = file->buf;
1910 end = file->buf + file->size;
1911 while((end - src) >= 2) {
1912 if ((src[0] == '\\') && (src[1] == '\n')) {
1922 file->size = dest - file->buf;
1925 static struct type void_type;
1926 static struct type unknown_type;
1927 static void use_triple(struct triple *used, struct triple *user)
1929 struct triple_set **ptr, *new;
1936 if ((*ptr)->member == user) {
1939 ptr = &(*ptr)->next;
1941 /* Append new to the head of the list,
1942 * copy_func and rename_block_variables
1945 new = xcmalloc(sizeof(*new), "triple_set");
1947 new->next = used->use;
1951 static void unuse_triple(struct triple *used, struct triple *unuser)
1953 struct triple_set *use, **ptr;
1960 if (use->member == unuser) {
1970 static void put_occurance(struct occurance *occurance)
1973 occurance->count -= 1;
1974 if (occurance->count <= 0) {
1975 if (occurance->parent) {
1976 put_occurance(occurance->parent);
1983 static void get_occurance(struct occurance *occurance)
1986 occurance->count += 1;
1991 static struct occurance *new_occurance(struct compile_state *state)
1993 struct occurance *result, *last;
1994 const char *filename;
1995 const char *function;
2003 filename = state->file->report_name;
2004 line = state->file->report_line;
2005 col = get_col(state->file);
2007 if (state->function) {
2008 function = state->function;
2010 last = state->last_occurance;
2012 (last->col == col) &&
2013 (last->line == line) &&
2014 (last->function == function) &&
2015 ((last->filename == filename) ||
2016 (strcmp(last->filename, filename) == 0)))
2018 get_occurance(last);
2022 state->last_occurance = 0;
2023 put_occurance(last);
2025 result = xmalloc(sizeof(*result), "occurance");
2027 result->filename = filename;
2028 result->function = function;
2029 result->line = line;
2032 state->last_occurance = result;
2036 static struct occurance *inline_occurance(struct compile_state *state,
2037 struct occurance *base, struct occurance *top)
2039 struct occurance *result, *last;
2041 internal_error(state, 0, "inlining an already inlined function?");
2043 /* If I have a null base treat it that way */
2044 if ((base->parent == 0) &&
2046 (base->line == 0) &&
2047 (base->function[0] == '\0') &&
2048 (base->filename[0] == '\0')) {
2051 /* See if I can reuse the last occurance I had */
2052 last = state->last_occurance;
2054 (last->parent == base) &&
2055 (last->col == top->col) &&
2056 (last->line == top->line) &&
2057 (last->function == top->function) &&
2058 (last->filename == top->filename)) {
2059 get_occurance(last);
2062 /* I can't reuse the last occurance so free it */
2064 state->last_occurance = 0;
2065 put_occurance(last);
2067 /* Generate a new occurance structure */
2068 get_occurance(base);
2069 result = xmalloc(sizeof(*result), "occurance");
2071 result->filename = top->filename;
2072 result->function = top->function;
2073 result->line = top->line;
2074 result->col = top->col;
2075 result->parent = base;
2076 state->last_occurance = result;
2080 static struct occurance dummy_occurance = {
2082 .filename = __FILE__,
2089 /* The undef triple is used as a place holder when we are removing pointers
2090 * from a triple. Having allows certain sanity checks to pass even
2091 * when the original triple that was pointed to is gone.
2093 static struct triple unknown_triple = {
2094 .next = &unknown_triple,
2095 .prev = &unknown_triple,
2097 .op = OP_UNKNOWNVAL,
2102 .type = &unknown_type,
2103 .id = -1, /* An invalid id */
2104 .u = { .cval = 0, },
2105 .occurance = &dummy_occurance,
2106 .param = { [0] = 0, [1] = 0, },
2110 static size_t registers_of(struct compile_state *state, struct type *type);
2112 static struct triple *alloc_triple(struct compile_state *state,
2113 int op, struct type *type, int lhs_wanted, int rhs_wanted,
2114 struct occurance *occurance)
2116 size_t size, extra_count, min_count;
2117 int lhs, rhs, misc, targ;
2118 struct triple *ret, dummy;
2120 dummy.occurance = occurance;
2121 valid_op(state, op);
2122 lhs = table_ops[op].lhs;
2123 rhs = table_ops[op].rhs;
2124 misc = table_ops[op].misc;
2125 targ = table_ops[op].targ;
2135 lhs = registers_of(state, type);
2138 lhs = registers_of(state, type);
2145 if ((rhs < 0) || (rhs > MAX_RHS)) {
2146 internal_error(state, &dummy, "bad rhs count %d", rhs);
2148 if ((lhs < 0) || (lhs > MAX_LHS)) {
2149 internal_error(state, &dummy, "bad lhs count %d", lhs);
2151 if ((misc < 0) || (misc > MAX_MISC)) {
2152 internal_error(state, &dummy, "bad misc count %d", misc);
2154 if ((targ < 0) || (targ > MAX_TARG)) {
2155 internal_error(state, &dummy, "bad targs count %d", targ);
2158 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
2159 extra_count = lhs + rhs + misc + targ;
2160 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
2162 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
2163 ret = xcmalloc(size, "tripple");
2172 ret->occurance = occurance;
2173 /* A simple sanity check */
2174 if ((ret->op != op) ||
2175 (ret->lhs != lhs) ||
2176 (ret->rhs != rhs) ||
2177 (ret->misc != misc) ||
2178 (ret->targ != targ) ||
2179 (ret->type != type) ||
2180 (ret->next != ret) ||
2181 (ret->prev != ret) ||
2182 (ret->occurance != occurance)) {
2183 internal_error(state, ret, "huh?");
2188 struct triple *dup_triple(struct compile_state *state, struct triple *src)
2191 int src_lhs, src_rhs, src_size;
2194 src_size = TRIPLE_SIZE(src);
2195 get_occurance(src->occurance);
2196 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
2198 memcpy(dup, src, sizeof(*src));
2199 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
2203 static struct triple *copy_triple(struct compile_state *state, struct triple *src)
2205 struct triple *copy;
2206 copy = dup_triple(state, src);
2208 copy->next = copy->prev = copy;
2212 static struct triple *new_triple(struct compile_state *state,
2213 int op, struct type *type, int lhs, int rhs)
2216 struct occurance *occurance;
2217 occurance = new_occurance(state);
2218 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
2222 static struct triple *build_triple(struct compile_state *state,
2223 int op, struct type *type, struct triple *left, struct triple *right,
2224 struct occurance *occurance)
2228 ret = alloc_triple(state, op, type, -1, -1, occurance);
2229 count = TRIPLE_SIZE(ret);
2231 ret->param[0] = left;
2234 ret->param[1] = right;
2239 static struct triple *triple(struct compile_state *state,
2240 int op, struct type *type, struct triple *left, struct triple *right)
2244 ret = new_triple(state, op, type, -1, -1);
2245 count = TRIPLE_SIZE(ret);
2247 ret->param[0] = left;
2250 ret->param[1] = right;
2255 static struct triple *branch(struct compile_state *state,
2256 struct triple *targ, struct triple *test)
2260 ret = new_triple(state, OP_CBRANCH, &void_type, -1, 1);
2263 ret = new_triple(state, OP_BRANCH, &void_type, -1, 0);
2265 TARG(ret, 0) = targ;
2266 /* record the branch target was used */
2267 if (!targ || (targ->op != OP_LABEL)) {
2268 internal_error(state, 0, "branch not to label");
2273 static int triple_is_label(struct compile_state *state, struct triple *ins);
2274 static int triple_is_call(struct compile_state *state, struct triple *ins);
2275 static int triple_is_cbranch(struct compile_state *state, struct triple *ins);
2276 static void insert_triple(struct compile_state *state,
2277 struct triple *first, struct triple *ptr)
2280 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
2281 internal_error(state, ptr, "expression already used");
2284 ptr->prev = first->prev;
2285 ptr->prev->next = ptr;
2286 ptr->next->prev = ptr;
2288 if (triple_is_cbranch(state, ptr->prev) ||
2289 triple_is_call(state, ptr->prev)) {
2290 unuse_triple(first, ptr->prev);
2291 use_triple(ptr, ptr->prev);
2296 static int triple_stores_block(struct compile_state *state, struct triple *ins)
2298 /* This function is used to determine if u.block
2299 * is utilized to store the current block number.
2302 valid_ins(state, ins);
2303 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
2304 return stores_block;
2307 static int triple_is_branch(struct compile_state *state, struct triple *ins);
2308 static struct block *block_of_triple(struct compile_state *state,
2311 struct triple *first;
2312 if (!ins || ins == &unknown_triple) {
2315 first = state->first;
2316 while(ins != first && !triple_is_branch(state, ins->prev) &&
2317 !triple_stores_block(state, ins))
2319 if (ins == ins->prev) {
2320 internal_error(state, ins, "ins == ins->prev?");
2324 return triple_stores_block(state, ins)? ins->u.block: 0;
2327 static void generate_lhs_pieces(struct compile_state *state, struct triple *ins);
2328 static struct triple *pre_triple(struct compile_state *state,
2329 struct triple *base,
2330 int op, struct type *type, struct triple *left, struct triple *right)
2332 struct block *block;
2335 /* If I am an OP_PIECE jump to the real instruction */
2336 if (base->op == OP_PIECE) {
2337 base = MISC(base, 0);
2339 block = block_of_triple(state, base);
2340 get_occurance(base->occurance);
2341 ret = build_triple(state, op, type, left, right, base->occurance);
2342 generate_lhs_pieces(state, ret);
2343 if (triple_stores_block(state, ret)) {
2344 ret->u.block = block;
2346 insert_triple(state, base, ret);
2347 for(i = 0; i < ret->lhs; i++) {
2348 struct triple *piece;
2349 piece = LHS(ret, i);
2350 insert_triple(state, base, piece);
2351 use_triple(ret, piece);
2352 use_triple(piece, ret);
2354 if (block && (block->first == base)) {
2360 static struct triple *post_triple(struct compile_state *state,
2361 struct triple *base,
2362 int op, struct type *type, struct triple *left, struct triple *right)
2364 struct block *block;
2365 struct triple *ret, *next;
2367 /* If I am an OP_PIECE jump to the real instruction */
2368 if (base->op == OP_PIECE) {
2369 base = MISC(base, 0);
2371 /* If I have a left hand side skip over it */
2374 base = LHS(base, zlhs - 1);
2377 block = block_of_triple(state, base);
2378 get_occurance(base->occurance);
2379 ret = build_triple(state, op, type, left, right, base->occurance);
2380 generate_lhs_pieces(state, ret);
2381 if (triple_stores_block(state, ret)) {
2382 ret->u.block = block;
2385 insert_triple(state, next, ret);
2387 for(i = 0; i < zlhs; i++) {
2388 struct triple *piece;
2389 piece = LHS(ret, i);
2390 insert_triple(state, next, piece);
2391 use_triple(ret, piece);
2392 use_triple(piece, ret);
2394 if (block && (block->last == base)) {
2397 block->last = LHS(ret, zlhs - 1);
2403 static struct type *reg_type(
2404 struct compile_state *state, struct type *type, int reg);
2406 static void generate_lhs_piece(
2407 struct compile_state *state, struct triple *ins, int index)
2409 struct type *piece_type;
2410 struct triple *piece;
2411 get_occurance(ins->occurance);
2412 piece_type = reg_type(state, ins->type, index * REG_SIZEOF_REG);
2414 if ((piece_type->type & TYPE_MASK) == TYPE_BITFIELD) {
2415 piece_type = piece_type->left;
2419 static void name_of(FILE *fp, struct type *type);
2420 FILE * fp = state->errout;
2421 fprintf(fp, "piece_type(%d): ", index);
2422 name_of(fp, piece_type);
2426 piece = alloc_triple(state, OP_PIECE, piece_type, -1, -1, ins->occurance);
2427 piece->u.cval = index;
2428 LHS(ins, piece->u.cval) = piece;
2429 MISC(piece, 0) = ins;
2432 static void generate_lhs_pieces(struct compile_state *state, struct triple *ins)
2436 for(i = 0; i < zlhs; i++) {
2437 generate_lhs_piece(state, ins, i);
2441 static struct triple *label(struct compile_state *state)
2443 /* Labels don't get a type */
2444 struct triple *result;
2445 result = triple(state, OP_LABEL, &void_type, 0, 0);
2449 static struct triple *mkprog(struct compile_state *state, ...)
2451 struct triple *prog, *head, *arg;
2455 head = label(state);
2456 prog = new_triple(state, OP_PROG, &void_type, -1, -1);
2457 RHS(prog, 0) = head;
2458 va_start(args, state);
2460 while((arg = va_arg(args, struct triple *)) != 0) {
2462 internal_error(state, 0, "too many arguments to mkprog");
2464 flatten(state, head, arg);
2467 prog->type = head->prev->type;
2470 static void name_of(FILE *fp, struct type *type);
2471 static void display_triple(FILE *fp, struct triple *ins)
2473 struct occurance *ptr;
2475 char pre, post, vol;
2476 pre = post = vol = ' ';
2478 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
2481 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
2484 if (ins->id & TRIPLE_FLAG_VOLATILE) {
2487 reg = arch_reg_str(ID_REG(ins->id));
2490 fprintf(fp, "(%p) <nothing> ", ins);
2492 else if (ins->op == OP_INTCONST) {
2493 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s <0x%08lx> ",
2494 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
2495 (unsigned long)(ins->u.cval));
2497 else if (ins->op == OP_ADDRCONST) {
2498 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2499 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
2500 MISC(ins, 0), (unsigned long)(ins->u.cval));
2502 else if (ins->op == OP_INDEX) {
2503 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2504 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
2505 RHS(ins, 0), (unsigned long)(ins->u.cval));
2507 else if (ins->op == OP_PIECE) {
2508 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2509 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
2510 MISC(ins, 0), (unsigned long)(ins->u.cval));
2514 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s",
2515 ins, pre, post, vol, reg, ins->template_id, tops(ins->op));
2516 if (table_ops[ins->op].flags & BITFIELD) {
2517 fprintf(fp, " <%2d-%2d:%2d>",
2518 ins->u.bitfield.offset,
2519 ins->u.bitfield.offset + ins->u.bitfield.size,
2520 ins->u.bitfield.size);
2522 count = TRIPLE_SIZE(ins);
2523 for(i = 0; i < count; i++) {
2524 fprintf(fp, " %-10p", ins->param[i]);
2531 struct triple_set *user;
2532 #if DEBUG_DISPLAY_TYPES
2534 name_of(fp, ins->type);
2537 #if DEBUG_DISPLAY_USES
2539 for(user = ins->use; user; user = user->next) {
2540 fprintf(fp, " %-10p", user->member);
2545 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
2546 fprintf(fp, " %s,%s:%d.%d",
2552 if (ins->op == OP_ASM) {
2553 fprintf(fp, "\n\t%s", ins->u.ainfo->str);
2560 static int equiv_types(struct type *left, struct type *right);
2561 static void display_triple_changes(
2562 FILE *fp, const struct triple *new, const struct triple *orig)
2565 int new_count, orig_count;
2566 new_count = TRIPLE_SIZE(new);
2567 orig_count = TRIPLE_SIZE(orig);
2568 if ((new->op != orig->op) ||
2569 (new_count != orig_count) ||
2570 (memcmp(orig->param, new->param,
2571 orig_count * sizeof(orig->param[0])) != 0) ||
2572 (memcmp(&orig->u, &new->u, sizeof(orig->u)) != 0))
2574 struct occurance *ptr;
2575 int i, min_count, indent;
2576 fprintf(fp, "(%p %p)", new, orig);
2577 if (orig->op == new->op) {
2578 fprintf(fp, " %-11s", tops(orig->op));
2580 fprintf(fp, " [%-10s %-10s]",
2581 tops(new->op), tops(orig->op));
2583 min_count = new_count;
2584 if (min_count > orig_count) {
2585 min_count = orig_count;
2587 for(indent = i = 0; i < min_count; i++) {
2588 if (orig->param[i] == new->param[i]) {
2589 fprintf(fp, " %-11p",
2593 fprintf(fp, " [%-10p %-10p]",
2599 for(; i < orig_count; i++) {
2600 fprintf(fp, " [%-9p]", orig->param[i]);
2603 for(; i < new_count; i++) {
2604 fprintf(fp, " [%-9p]", new->param[i]);
2607 if ((new->op == OP_INTCONST)||
2608 (new->op == OP_ADDRCONST)) {
2609 fprintf(fp, " <0x%08lx>",
2610 (unsigned long)(new->u.cval));
2613 for(;indent < 36; indent++) {
2617 #if DEBUG_DISPLAY_TYPES
2619 name_of(fp, new->type);
2620 if (!equiv_types(new->type, orig->type)) {
2621 fprintf(fp, " -- ");
2622 name_of(fp, orig->type);
2628 for(ptr = orig->occurance; ptr; ptr = ptr->parent) {
2629 fprintf(fp, " %s,%s:%d.%d",
2641 static int triple_is_pure(struct compile_state *state, struct triple *ins, unsigned id)
2643 /* Does the triple have no side effects.
2644 * I.e. Rexecuting the triple with the same arguments
2645 * gives the same value.
2648 valid_ins(state, ins);
2649 pure = PURE_BITS(table_ops[ins->op].flags);
2650 if ((pure != PURE) && (pure != IMPURE)) {
2651 internal_error(state, 0, "Purity of %s not known",
2654 return (pure == PURE) && !(id & TRIPLE_FLAG_VOLATILE);
2657 static int triple_is_branch_type(struct compile_state *state,
2658 struct triple *ins, unsigned type)
2660 /* Is this one of the passed branch types? */
2661 valid_ins(state, ins);
2662 return (BRANCH_BITS(table_ops[ins->op].flags) == type);
2665 static int triple_is_branch(struct compile_state *state, struct triple *ins)
2667 /* Is this triple a branch instruction? */
2668 valid_ins(state, ins);
2669 return (BRANCH_BITS(table_ops[ins->op].flags) != 0);
2672 static int triple_is_cbranch(struct compile_state *state, struct triple *ins)
2674 /* Is this triple a conditional branch instruction? */
2675 return triple_is_branch_type(state, ins, CBRANCH);
2678 static int triple_is_ubranch(struct compile_state *state, struct triple *ins)
2680 /* Is this triple a unconditional branch instruction? */
2682 valid_ins(state, ins);
2683 type = BRANCH_BITS(table_ops[ins->op].flags);
2684 return (type != 0) && (type != CBRANCH);
2687 static int triple_is_call(struct compile_state *state, struct triple *ins)
2689 /* Is this triple a call instruction? */
2690 return triple_is_branch_type(state, ins, CALLBRANCH);
2693 static int triple_is_ret(struct compile_state *state, struct triple *ins)
2695 /* Is this triple a return instruction? */
2696 return triple_is_branch_type(state, ins, RETBRANCH);
2699 static int triple_is_simple_ubranch(struct compile_state *state, struct triple *ins)
2701 /* Is this triple an unconditional branch and not a call or a
2703 return triple_is_branch_type(state, ins, UBRANCH);
2706 static int triple_is_end(struct compile_state *state, struct triple *ins)
2708 return triple_is_branch_type(state, ins, ENDBRANCH);
2711 static int triple_is_label(struct compile_state *state, struct triple *ins)
2713 valid_ins(state, ins);
2714 return (ins->op == OP_LABEL);
2717 static struct triple *triple_to_block_start(
2718 struct compile_state *state, struct triple *start)
2720 while(!triple_is_branch(state, start->prev) &&
2721 (!triple_is_label(state, start) || !start->use)) {
2722 start = start->prev;
2727 static int triple_is_def(struct compile_state *state, struct triple *ins)
2729 /* This function is used to determine which triples need
2733 valid_ins(state, ins);
2734 is_def = (table_ops[ins->op].flags & DEF) == DEF;
2735 if (ins->lhs >= 1) {
2741 static int triple_is_structural(struct compile_state *state, struct triple *ins)
2744 valid_ins(state, ins);
2745 is_structural = (table_ops[ins->op].flags & STRUCTURAL) == STRUCTURAL;
2746 return is_structural;
2749 static int triple_is_part(struct compile_state *state, struct triple *ins)
2752 valid_ins(state, ins);
2753 is_part = (table_ops[ins->op].flags & PART) == PART;
2757 static int triple_is_auto_var(struct compile_state *state, struct triple *ins)
2759 return (ins->op == OP_PIECE) && (MISC(ins, 0)->op == OP_ADECL);
2762 static struct triple **triple_iter(struct compile_state *state,
2763 size_t count, struct triple **vector,
2764 struct triple *ins, struct triple **last)
2766 struct triple **ret;
2772 else if ((last >= vector) && (last < (vector + count - 1))) {
2780 static struct triple **triple_lhs(struct compile_state *state,
2781 struct triple *ins, struct triple **last)
2783 return triple_iter(state, ins->lhs, &LHS(ins,0),
2787 static struct triple **triple_rhs(struct compile_state *state,
2788 struct triple *ins, struct triple **last)
2790 return triple_iter(state, ins->rhs, &RHS(ins,0),
2794 static struct triple **triple_misc(struct compile_state *state,
2795 struct triple *ins, struct triple **last)
2797 return triple_iter(state, ins->misc, &MISC(ins,0),
2801 static struct triple **do_triple_targ(struct compile_state *state,
2802 struct triple *ins, struct triple **last, int call_edges, int next_edges)
2805 struct triple **ret, **vector;
2810 if (triple_is_cbranch(state, ins)) {
2813 if (!call_edges && triple_is_call(state, ins)) {
2816 if (next_edges && triple_is_call(state, ins)) {
2819 vector = &TARG(ins, 0);
2820 if (!ret && next_is_targ) {
2823 } else if (last == &ins->next) {
2827 if (!ret && count) {
2831 else if ((last >= vector) && (last < (vector + count - 1))) {
2834 else if (last == vector + count - 1) {
2838 if (!ret && triple_is_ret(state, ins) && call_edges) {
2839 struct triple_set *use;
2840 for(use = ins->use; use; use = use->next) {
2841 if (!triple_is_call(state, use->member)) {
2845 ret = &use->member->next;
2848 else if (last == &use->member->next) {
2856 static struct triple **triple_targ(struct compile_state *state,
2857 struct triple *ins, struct triple **last)
2859 return do_triple_targ(state, ins, last, 1, 1);
2862 static struct triple **triple_edge_targ(struct compile_state *state,
2863 struct triple *ins, struct triple **last)
2865 return do_triple_targ(state, ins, last,
2866 state->functions_joined, !state->functions_joined);
2869 static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
2871 struct triple *next;
2875 for(i = 0; i < lhs; i++) {
2876 struct triple *piece;
2877 piece = LHS(ins, i);
2878 if (next != piece) {
2879 internal_error(state, ins, "malformed lhs on %s",
2882 if (next->op != OP_PIECE) {
2883 internal_error(state, ins, "bad lhs op %s at %d on %s",
2884 tops(next->op), i, tops(ins->op));
2886 if (next->u.cval != i) {
2887 internal_error(state, ins, "bad u.cval of %d %d expected",
2895 /* Function piece accessor functions */
2896 static struct triple *do_farg(struct compile_state *state,
2897 struct triple *func, unsigned index)
2900 struct triple *first, *arg;
2904 if((index < 0) || (index >= (ftype->elements + 2))) {
2905 internal_error(state, func, "bad argument index: %d", index);
2907 first = RHS(func, 0);
2909 for(i = 0; i < index; i++, arg = after_lhs(state, arg)) {
2912 if (arg->op != OP_ADECL) {
2913 internal_error(state, 0, "arg not adecl?");
2917 static struct triple *fresult(struct compile_state *state, struct triple *func)
2919 return do_farg(state, func, 0);
2921 static struct triple *fretaddr(struct compile_state *state, struct triple *func)
2923 return do_farg(state, func, 1);
2925 static struct triple *farg(struct compile_state *state,
2926 struct triple *func, unsigned index)
2928 return do_farg(state, func, index + 2);
2932 static void display_func(struct compile_state *state, FILE *fp, struct triple *func)
2934 struct triple *first, *ins;
2935 fprintf(fp, "display_func %s\n", func->type->type_ident->name);
2936 first = ins = RHS(func, 0);
2938 if (triple_is_label(state, ins) && ins->use) {
2939 fprintf(fp, "%p:\n", ins);
2941 display_triple(fp, ins);
2943 if (triple_is_branch(state, ins)) {
2946 if (ins->next->prev != ins) {
2947 internal_error(state, ins->next, "bad prev");
2950 } while(ins != first);
2953 static void verify_use(struct compile_state *state,
2954 struct triple *user, struct triple *used)
2957 size = TRIPLE_SIZE(user);
2958 for(i = 0; i < size; i++) {
2959 if (user->param[i] == used) {
2963 if (triple_is_branch(state, user)) {
2964 if (user->next == used) {
2969 internal_error(state, user, "%s(%p) does not use %s(%p)",
2970 tops(user->op), user, tops(used->op), used);
2974 static int find_rhs_use(struct compile_state *state,
2975 struct triple *user, struct triple *used)
2977 struct triple **param;
2979 verify_use(state, user, used);
2980 #warning "AUDIT ME ->rhs"
2982 param = &RHS(user, 0);
2983 for(i = 0; i < size; i++) {
2984 if (param[i] == used) {
2991 static void free_triple(struct compile_state *state, struct triple *ptr)
2994 size = sizeof(*ptr) - sizeof(ptr->param) +
2995 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr));
2996 ptr->prev->next = ptr->next;
2997 ptr->next->prev = ptr->prev;
2999 internal_error(state, ptr, "ptr->use != 0");
3001 put_occurance(ptr->occurance);
3002 memset(ptr, -1, size);
3006 static void release_triple(struct compile_state *state, struct triple *ptr)
3008 struct triple_set *set, *next;
3009 struct triple **expr;
3010 struct block *block;
3011 if (ptr == &unknown_triple) {
3014 valid_ins(state, ptr);
3015 /* Make certain the we are not the first or last element of a block */
3016 block = block_of_triple(state, ptr);
3018 if ((block->last == ptr) && (block->first == ptr)) {
3019 block->last = block->first = 0;
3021 else if (block->last == ptr) {
3022 block->last = ptr->prev;
3024 else if (block->first == ptr) {
3025 block->first = ptr->next;
3028 /* Remove ptr from use chains where it is the user */
3029 expr = triple_rhs(state, ptr, 0);
3030 for(; expr; expr = triple_rhs(state, ptr, expr)) {
3032 unuse_triple(*expr, ptr);
3035 expr = triple_lhs(state, ptr, 0);
3036 for(; expr; expr = triple_lhs(state, ptr, expr)) {
3038 unuse_triple(*expr, ptr);
3041 expr = triple_misc(state, ptr, 0);
3042 for(; expr; expr = triple_misc(state, ptr, expr)) {
3044 unuse_triple(*expr, ptr);
3047 expr = triple_targ(state, ptr, 0);
3048 for(; expr; expr = triple_targ(state, ptr, expr)) {
3050 unuse_triple(*expr, ptr);
3053 /* Reomve ptr from use chains where it is used */
3054 for(set = ptr->use; set; set = next) {
3056 valid_ins(state, set->member);
3057 expr = triple_rhs(state, set->member, 0);
3058 for(; expr; expr = triple_rhs(state, set->member, expr)) {
3060 *expr = &unknown_triple;
3063 expr = triple_lhs(state, set->member, 0);
3064 for(; expr; expr = triple_lhs(state, set->member, expr)) {
3066 *expr = &unknown_triple;
3069 expr = triple_misc(state, set->member, 0);
3070 for(; expr; expr = triple_misc(state, set->member, expr)) {
3072 *expr = &unknown_triple;
3075 expr = triple_targ(state, set->member, 0);
3076 for(; expr; expr = triple_targ(state, set->member, expr)) {
3078 *expr = &unknown_triple;
3081 unuse_triple(ptr, set->member);
3083 free_triple(state, ptr);
3086 static void print_triples(struct compile_state *state);
3087 static void print_blocks(struct compile_state *state, const char *func, FILE *fp);
3089 #define TOK_UNKNOWN 0
3092 #define TOK_LBRACE 3
3093 #define TOK_RBRACE 4
3097 #define TOK_LBRACKET 8
3098 #define TOK_RBRACKET 9
3099 #define TOK_LPAREN 10
3100 #define TOK_RPAREN 11
3105 #define TOK_TIMESEQ 16
3106 #define TOK_DIVEQ 17
3107 #define TOK_MODEQ 18
3108 #define TOK_PLUSEQ 19
3109 #define TOK_MINUSEQ 20
3112 #define TOK_ANDEQ 23
3113 #define TOK_XOREQ 24
3116 #define TOK_NOTEQ 27
3117 #define TOK_QUEST 28
3118 #define TOK_LOGOR 29
3119 #define TOK_LOGAND 30
3123 #define TOK_LESSEQ 34
3124 #define TOK_MOREEQ 35
3128 #define TOK_MINUS 39
3131 #define TOK_PLUSPLUS 42
3132 #define TOK_MINUSMINUS 43
3134 #define TOK_ARROW 45
3136 #define TOK_TILDE 47
3137 #define TOK_LIT_STRING 48
3138 #define TOK_LIT_CHAR 49
3139 #define TOK_LIT_INT 50
3140 #define TOK_LIT_FLOAT 51
3141 #define TOK_MACRO 52
3142 #define TOK_CONCATENATE 53
3144 #define TOK_IDENT 54
3145 #define TOK_STRUCT_NAME 55
3146 #define TOK_ENUM_CONST 56
3147 #define TOK_TYPE_NAME 57
3150 #define TOK_BREAK 59
3153 #define TOK_CONST 62
3154 #define TOK_CONTINUE 63
3155 #define TOK_DEFAULT 64
3157 #define TOK_DOUBLE 66
3160 #define TOK_EXTERN 69
3161 #define TOK_FLOAT 70
3165 #define TOK_INLINE 74
3168 #define TOK_REGISTER 77
3169 #define TOK_RESTRICT 78
3170 #define TOK_RETURN 79
3171 #define TOK_SHORT 80
3172 #define TOK_SIGNED 81
3173 #define TOK_SIZEOF 82
3174 #define TOK_STATIC 83
3175 #define TOK_STRUCT 84
3176 #define TOK_SWITCH 85
3177 #define TOK_TYPEDEF 86
3178 #define TOK_UNION 87
3179 #define TOK_UNSIGNED 88
3181 #define TOK_VOLATILE 90
3182 #define TOK_WHILE 91
3184 #define TOK_ATTRIBUTE 93
3185 #define TOK_ALIGNOF 94
3186 #define TOK_FIRST_KEYWORD TOK_AUTO
3187 #define TOK_LAST_KEYWORD TOK_ALIGNOF
3189 #define TOK_MDEFINE 100
3190 #define TOK_MDEFINED 101
3191 #define TOK_MUNDEF 102
3192 #define TOK_MINCLUDE 103
3193 #define TOK_MLINE 104
3194 #define TOK_MERROR 105
3195 #define TOK_MWARNING 106
3196 #define TOK_MPRAGMA 107
3197 #define TOK_MIFDEF 108
3198 #define TOK_MIFNDEF 109
3199 #define TOK_MELIF 110
3200 #define TOK_MENDIF 111
3202 #define TOK_FIRST_MACRO TOK_MDEFINE
3203 #define TOK_LAST_MACRO TOK_MENDIF
3206 #define TOK_MELSE 113
3207 #define TOK_MIDENT 114
3212 static const char *tokens[] = {
3213 [TOK_UNKNOWN ] = ":unknown:",
3214 [TOK_SPACE ] = ":space:",
3216 [TOK_LBRACE ] = "{",
3217 [TOK_RBRACE ] = "}",
3221 [TOK_LBRACKET ] = "[",
3222 [TOK_RBRACKET ] = "]",
3223 [TOK_LPAREN ] = "(",
3224 [TOK_RPAREN ] = ")",
3226 [TOK_DOTS ] = "...",
3229 [TOK_TIMESEQ ] = "*=",
3230 [TOK_DIVEQ ] = "/=",
3231 [TOK_MODEQ ] = "%=",
3232 [TOK_PLUSEQ ] = "+=",
3233 [TOK_MINUSEQ ] = "-=",
3234 [TOK_SLEQ ] = "<<=",
3235 [TOK_SREQ ] = ">>=",
3236 [TOK_ANDEQ ] = "&=",
3237 [TOK_XOREQ ] = "^=",
3240 [TOK_NOTEQ ] = "!=",
3242 [TOK_LOGOR ] = "||",
3243 [TOK_LOGAND ] = "&&",
3247 [TOK_LESSEQ ] = "<=",
3248 [TOK_MOREEQ ] = ">=",
3255 [TOK_PLUSPLUS ] = "++",
3256 [TOK_MINUSMINUS ] = "--",
3258 [TOK_ARROW ] = "->",
3261 [TOK_LIT_STRING ] = ":string:",
3262 [TOK_IDENT ] = ":ident:",
3263 [TOK_TYPE_NAME ] = ":typename:",
3264 [TOK_LIT_CHAR ] = ":char:",
3265 [TOK_LIT_INT ] = ":integer:",
3266 [TOK_LIT_FLOAT ] = ":float:",
3268 [TOK_CONCATENATE ] = "##",
3270 [TOK_AUTO ] = "auto",
3271 [TOK_BREAK ] = "break",
3272 [TOK_CASE ] = "case",
3273 [TOK_CHAR ] = "char",
3274 [TOK_CONST ] = "const",
3275 [TOK_CONTINUE ] = "continue",
3276 [TOK_DEFAULT ] = "default",
3278 [TOK_DOUBLE ] = "double",
3279 [TOK_ELSE ] = "else",
3280 [TOK_ENUM ] = "enum",
3281 [TOK_EXTERN ] = "extern",
3282 [TOK_FLOAT ] = "float",
3284 [TOK_GOTO ] = "goto",
3286 [TOK_INLINE ] = "inline",
3288 [TOK_LONG ] = "long",
3289 [TOK_REGISTER ] = "register",
3290 [TOK_RESTRICT ] = "restrict",
3291 [TOK_RETURN ] = "return",
3292 [TOK_SHORT ] = "short",
3293 [TOK_SIGNED ] = "signed",
3294 [TOK_SIZEOF ] = "sizeof",
3295 [TOK_STATIC ] = "static",
3296 [TOK_STRUCT ] = "struct",
3297 [TOK_SWITCH ] = "switch",
3298 [TOK_TYPEDEF ] = "typedef",
3299 [TOK_UNION ] = "union",
3300 [TOK_UNSIGNED ] = "unsigned",
3301 [TOK_VOID ] = "void",
3302 [TOK_VOLATILE ] = "volatile",
3303 [TOK_WHILE ] = "while",
3305 [TOK_ATTRIBUTE ] = "__attribute__",
3306 [TOK_ALIGNOF ] = "__alignof__",
3308 [TOK_MDEFINE ] = "#define",
3309 [TOK_MDEFINED ] = "#defined",
3310 [TOK_MUNDEF ] = "#undef",
3311 [TOK_MINCLUDE ] = "#include",
3312 [TOK_MLINE ] = "#line",
3313 [TOK_MERROR ] = "#error",
3314 [TOK_MWARNING ] = "#warning",
3315 [TOK_MPRAGMA ] = "#pragma",
3316 [TOK_MIFDEF ] = "#ifdef",
3317 [TOK_MIFNDEF ] = "#ifndef",
3318 [TOK_MELIF ] = "#elif",
3319 [TOK_MENDIF ] = "#endif",
3322 [TOK_MELSE ] = "#else",
3323 [TOK_MIDENT ] = "#:ident:",
3328 static unsigned int hash(const char *str, int str_len)
3332 end = str + str_len;
3334 for(; str < end; str++) {
3335 hash = (hash *263) + *str;
3337 hash = hash & (HASH_TABLE_SIZE -1);
3341 static struct hash_entry *lookup(
3342 struct compile_state *state, const char *name, int name_len)
3344 struct hash_entry *entry;
3346 index = hash(name, name_len);
3347 entry = state->hash_table[index];
3349 ((entry->name_len != name_len) ||
3350 (memcmp(entry->name, name, name_len) != 0))) {
3351 entry = entry->next;
3355 /* Get a private copy of the name */
3356 new_name = xmalloc(name_len + 1, "hash_name");
3357 memcpy(new_name, name, name_len);
3358 new_name[name_len] = '\0';
3360 /* Create a new hash entry */
3361 entry = xcmalloc(sizeof(*entry), "hash_entry");
3362 entry->next = state->hash_table[index];
3363 entry->name = new_name;
3364 entry->name_len = name_len;
3366 /* Place the new entry in the hash table */
3367 state->hash_table[index] = entry;
3372 static void ident_to_keyword(struct compile_state *state, struct token *tk)
3374 struct hash_entry *entry;
3376 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
3377 (entry->tok == TOK_ENUM_CONST) ||
3378 ((entry->tok >= TOK_FIRST_KEYWORD) &&
3379 (entry->tok <= TOK_LAST_KEYWORD)))) {
3380 tk->tok = entry->tok;
3384 static void ident_to_macro(struct compile_state *state, struct token *tk)
3386 struct hash_entry *entry;
3390 if ((entry->tok >= TOK_FIRST_MACRO) && (entry->tok <= TOK_LAST_MACRO)) {
3391 tk->tok = entry->tok;
3393 else if (entry->tok == TOK_IF) {
3396 else if (entry->tok == TOK_ELSE) {
3397 tk->tok = TOK_MELSE;
3400 tk->tok = TOK_MIDENT;
3404 static void hash_keyword(
3405 struct compile_state *state, const char *keyword, int tok)
3407 struct hash_entry *entry;
3408 entry = lookup(state, keyword, strlen(keyword));
3409 if (entry && entry->tok != TOK_UNKNOWN) {
3410 die("keyword %s already hashed", keyword);
3415 static void romcc_symbol(
3416 struct compile_state *state, struct hash_entry *ident,
3417 struct symbol **chain, struct triple *def, struct type *type, int depth)
3420 if (*chain && ((*chain)->scope_depth >= depth)) {
3421 error(state, 0, "%s already defined", ident->name);
3423 sym = xcmalloc(sizeof(*sym), "symbol");
3427 sym->scope_depth = depth;
3433 struct compile_state *state, struct hash_entry *ident,
3434 struct symbol **chain, struct triple *def, struct type *type)
3436 romcc_symbol(state, ident, chain, def, type, state->scope_depth);
3439 static void var_symbol(struct compile_state *state,
3440 struct hash_entry *ident, struct triple *def)
3442 if ((def->type->type & TYPE_MASK) == TYPE_PRODUCT) {
3443 internal_error(state, 0, "bad var type");
3445 symbol(state, ident, &ident->sym_ident, def, def->type);
3448 static void label_symbol(struct compile_state *state,
3449 struct hash_entry *ident, struct triple *label, int depth)
3451 romcc_symbol(state, ident, &ident->sym_label, label, &void_type, depth);
3454 static void start_scope(struct compile_state *state)
3456 state->scope_depth++;
3459 static void end_scope_syms(struct compile_state *state,
3460 struct symbol **chain, int depth)
3462 struct symbol *sym, *next;
3464 while(sym && (sym->scope_depth == depth)) {
3472 static void end_scope(struct compile_state *state)
3476 /* Walk through the hash table and remove all symbols
3477 * in the current scope.
3479 depth = state->scope_depth;
3480 for(i = 0; i < HASH_TABLE_SIZE; i++) {
3481 struct hash_entry *entry;
3482 entry = state->hash_table[i];
3484 end_scope_syms(state, &entry->sym_label, depth);
3485 end_scope_syms(state, &entry->sym_tag, depth);
3486 end_scope_syms(state, &entry->sym_ident, depth);
3487 entry = entry->next;
3490 state->scope_depth = depth - 1;
3493 static void register_keywords(struct compile_state *state)
3495 hash_keyword(state, "auto", TOK_AUTO);
3496 hash_keyword(state, "break", TOK_BREAK);
3497 hash_keyword(state, "case", TOK_CASE);
3498 hash_keyword(state, "char", TOK_CHAR);
3499 hash_keyword(state, "const", TOK_CONST);
3500 hash_keyword(state, "continue", TOK_CONTINUE);
3501 hash_keyword(state, "default", TOK_DEFAULT);
3502 hash_keyword(state, "do", TOK_DO);
3503 hash_keyword(state, "double", TOK_DOUBLE);
3504 hash_keyword(state, "else", TOK_ELSE);
3505 hash_keyword(state, "enum", TOK_ENUM);
3506 hash_keyword(state, "extern", TOK_EXTERN);
3507 hash_keyword(state, "float", TOK_FLOAT);
3508 hash_keyword(state, "for", TOK_FOR);
3509 hash_keyword(state, "goto", TOK_GOTO);
3510 hash_keyword(state, "if", TOK_IF);
3511 hash_keyword(state, "inline", TOK_INLINE);
3512 hash_keyword(state, "int", TOK_INT);
3513 hash_keyword(state, "long", TOK_LONG);
3514 hash_keyword(state, "register", TOK_REGISTER);
3515 hash_keyword(state, "restrict", TOK_RESTRICT);
3516 hash_keyword(state, "return", TOK_RETURN);
3517 hash_keyword(state, "short", TOK_SHORT);
3518 hash_keyword(state, "signed", TOK_SIGNED);
3519 hash_keyword(state, "sizeof", TOK_SIZEOF);
3520 hash_keyword(state, "static", TOK_STATIC);
3521 hash_keyword(state, "struct", TOK_STRUCT);
3522 hash_keyword(state, "switch", TOK_SWITCH);
3523 hash_keyword(state, "typedef", TOK_TYPEDEF);
3524 hash_keyword(state, "union", TOK_UNION);
3525 hash_keyword(state, "unsigned", TOK_UNSIGNED);
3526 hash_keyword(state, "void", TOK_VOID);
3527 hash_keyword(state, "volatile", TOK_VOLATILE);
3528 hash_keyword(state, "__volatile__", TOK_VOLATILE);
3529 hash_keyword(state, "while", TOK_WHILE);
3530 hash_keyword(state, "asm", TOK_ASM);
3531 hash_keyword(state, "__asm__", TOK_ASM);
3532 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
3533 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
3536 static void register_macro_keywords(struct compile_state *state)
3538 hash_keyword(state, "define", TOK_MDEFINE);
3539 hash_keyword(state, "defined", TOK_MDEFINED);
3540 hash_keyword(state, "undef", TOK_MUNDEF);
3541 hash_keyword(state, "include", TOK_MINCLUDE);
3542 hash_keyword(state, "line", TOK_MLINE);
3543 hash_keyword(state, "error", TOK_MERROR);
3544 hash_keyword(state, "warning", TOK_MWARNING);
3545 hash_keyword(state, "pragma", TOK_MPRAGMA);
3546 hash_keyword(state, "ifdef", TOK_MIFDEF);
3547 hash_keyword(state, "ifndef", TOK_MIFNDEF);
3548 hash_keyword(state, "elif", TOK_MELIF);
3549 hash_keyword(state, "endif", TOK_MENDIF);
3553 static void undef_macro(struct compile_state *state, struct hash_entry *ident)
3555 if (ident->sym_define != 0) {
3556 struct macro *macro;
3557 struct macro_arg *arg, *anext;
3558 macro = ident->sym_define;
3559 ident->sym_define = 0;
3561 /* Free the macro arguments... */
3562 anext = macro->args;
3569 /* Free the macro buffer */
3572 /* Now free the macro itself */
3577 static void define_macro(
3578 struct compile_state *state,
3579 struct hash_entry *ident,
3580 const char *value, int value_len, int value_off,
3581 struct macro_arg *args)
3583 struct macro *macro;
3584 struct macro_arg *arg;
3585 macro = ident->sym_define;
3587 /* Explicitly allow identical redefinitions of the same macro */
3588 if ((macro->buf_len == value_len) &&
3589 (memcmp(macro->buf, value, value_len) == 0)) {
3592 error(state, 0, "macro %s already defined\n", ident->name);
3595 fprintf(state->errout, "%s: `%*.*s'\n",
3597 value_len - value_off,
3598 value_len - value_off,
3601 macro = xmalloc(sizeof(*macro), "macro");
3602 macro->ident = ident;
3603 macro->buf_len = value_len;
3604 macro->buf_off = value_off;
3606 macro->buf = xmalloc(macro->buf_len + 1, "macro buf");
3609 for(arg = args; arg; arg = arg->next) {
3613 memcpy(macro->buf, value, macro->buf_len);
3614 macro->buf[macro->buf_len] = '\0';
3616 ident->sym_define = macro;
3619 static void register_builtin_macro(struct compile_state *state,
3620 const char *name, const char *value)
3622 struct hash_entry *ident;
3624 if (value[0] == '(') {
3625 internal_error(state, 0, "Builtin macros with arguments not supported");
3627 ident = lookup(state, name, strlen(name));
3628 define_macro(state, ident, value, strlen(value), 0, 0);
3631 static void register_builtin_macros(struct compile_state *state)
3638 tm = localtime(&now);
3640 register_builtin_macro(state, "__ROMCC__", VERSION_MAJOR);
3641 register_builtin_macro(state, "__ROMCC_MINOR__", VERSION_MINOR);
3642 register_builtin_macro(state, "__FILE__", "\"This should be the filename\"");
3643 register_builtin_macro(state, "__LINE__", "54321");
3645 strftime(scratch, sizeof(scratch), "%b %e %Y", tm);
3646 sprintf(buf, "\"%s\"", scratch);
3647 register_builtin_macro(state, "__DATE__", buf);
3649 strftime(scratch, sizeof(scratch), "%H:%M:%S", tm);
3650 sprintf(buf, "\"%s\"", scratch);
3651 register_builtin_macro(state, "__TIME__", buf);
3653 /* I can't be a conforming implementation of C :( */
3654 register_builtin_macro(state, "__STDC__", "0");
3655 /* In particular I don't conform to C99 */
3656 register_builtin_macro(state, "__STDC_VERSION__", "199901L");
3660 static void process_cmdline_macros(struct compile_state *state)
3662 const char **macro, *name;
3663 struct hash_entry *ident;
3664 for(macro = state->compiler->defines; (name = *macro); macro++) {
3668 name_len = strlen(name);
3669 body = strchr(name, '=');
3673 name_len = body - name;
3676 ident = lookup(state, name, name_len);
3677 define_macro(state, ident, body, strlen(body), 0, 0);
3679 for(macro = state->compiler->undefs; (name = *macro); macro++) {
3680 ident = lookup(state, name, strlen(name));
3681 undef_macro(state, ident);
3685 static int spacep(int c)
3700 static int eolp(int c)
3711 static int digitp(int c)
3715 case '0': case '1': case '2': case '3': case '4':
3716 case '5': case '6': case '7': case '8': case '9':
3722 static int digval(int c)
3725 if ((c >= '0') && (c <= '9')) {
3731 static int hexdigitp(int c)
3735 case '0': case '1': case '2': case '3': case '4':
3736 case '5': case '6': case '7': case '8': case '9':
3737 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3738 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3744 static int hexdigval(int c)
3747 if ((c >= '0') && (c <= '9')) {
3750 else if ((c >= 'A') && (c <= 'F')) {
3751 val = 10 + (c - 'A');
3753 else if ((c >= 'a') && (c <= 'f')) {
3754 val = 10 + (c - 'a');
3759 static int octdigitp(int c)
3763 case '0': case '1': case '2': case '3':
3764 case '4': case '5': case '6': case '7':
3770 static int octdigval(int c)
3773 if ((c >= '0') && (c <= '7')) {
3779 static int letterp(int c)
3783 case 'a': case 'b': case 'c': case 'd': case 'e':
3784 case 'f': case 'g': case 'h': case 'i': case 'j':
3785 case 'k': case 'l': case 'm': case 'n': case 'o':
3786 case 'p': case 'q': case 'r': case 's': case 't':
3787 case 'u': case 'v': case 'w': case 'x': case 'y':
3789 case 'A': case 'B': case 'C': case 'D': case 'E':
3790 case 'F': case 'G': case 'H': case 'I': case 'J':
3791 case 'K': case 'L': case 'M': case 'N': case 'O':
3792 case 'P': case 'Q': case 'R': case 'S': case 'T':
3793 case 'U': case 'V': case 'W': case 'X': case 'Y':
3802 static const char *identifier(const char *str, const char *end)
3804 if (letterp(*str)) {
3805 for(; str < end; str++) {
3808 if (!letterp(c) && !digitp(c)) {
3816 static int char_value(struct compile_state *state,
3817 const signed char **strp, const signed char *end)
3819 const signed char *str;
3823 if ((c == '\\') && (str < end)) {
3825 case 'n': c = '\n'; str++; break;
3826 case 't': c = '\t'; str++; break;
3827 case 'v': c = '\v'; str++; break;
3828 case 'b': c = '\b'; str++; break;
3829 case 'r': c = '\r'; str++; break;
3830 case 'f': c = '\f'; str++; break;
3831 case 'a': c = '\a'; str++; break;
3832 case '\\': c = '\\'; str++; break;
3833 case '?': c = '?'; str++; break;
3834 case '\'': c = '\''; str++; break;
3835 case '"': c = '"'; str++; break;
3839 while((str < end) && hexdigitp(*str)) {
3841 c += hexdigval(*str);
3845 case '0': case '1': case '2': case '3':
3846 case '4': case '5': case '6': case '7':
3848 while((str < end) && octdigitp(*str)) {
3850 c += octdigval(*str);
3855 error(state, 0, "Invalid character constant");
3863 static const char *after_digits(const char *ptr, const char *end)
3865 while((ptr < end) && digitp(*ptr)) {
3871 static const char *after_octdigits(const char *ptr, const char *end)
3873 while((ptr < end) && octdigitp(*ptr)) {
3879 static const char *after_hexdigits(const char *ptr, const char *end)
3881 while((ptr < end) && hexdigitp(*ptr)) {
3887 static void save_string(struct compile_state *state,
3888 struct token *tk, const char *start, const char *end, const char *id)
3892 /* Create a private copy of the string */
3893 str_len = end - start + 1;
3894 str = xmalloc(str_len + 1, id);
3895 memcpy(str, start, str_len);
3896 str[str_len] = '\0';
3898 /* Store the copy in the token */
3900 tk->str_len = str_len;
3903 static int lparen_peek(struct compile_state *state, struct file_state *file)
3905 const char *tokp, *end;
3906 /* Is the next token going to be an lparen?
3907 * Whitespace tokens are significant for seeing if a macro
3908 * should be expanded.
3911 end = file->buf + file->size;
3912 return (tokp < end) && (*tokp == '(');
3915 static void raw_next_token(struct compile_state *state,
3916 struct file_state *file, struct token *tk)
3920 const char *tokp, *end;
3925 token = tokp = file->pos;
3926 end = file->buf + file->size;
3933 if ((tokp + 1) < end) {
3937 if ((tokp + 2) < end) {
3941 if ((tokp + 3) < end) {
3952 file->report_line++;
3953 file->line_start = tokp + 1;
3956 else if (spacep(c)) {
3958 while ((tokp < end) && spacep(c)) {
3966 else if ((c == '/') && (c1 == '/')) {
3968 for(tokp += 2; tokp < end; tokp++) {
3977 else if ((c == '/') && (c1 == '*')) {
3979 const char *line_start;
3981 line_start = file->line_start;
3982 for(tokp += 2; (end - tokp) >= 2; tokp++) {
3986 line_start = tokp +1;
3988 else if ((c == '*') && (tokp[1] == '/')) {
3994 if (tok == TOK_UNKNOWN) {
3995 error(state, 0, "unterminated comment");
3997 if (state->token_base && (line != file->line)) {
3999 "multiline comment in preprocessor directive");
4001 file->report_line += line - file->line;
4003 file->line_start = line_start;
4005 /* string constants */
4006 else if ((c == '"') ||
4007 ((c == 'L') && (c1 == '"'))) {
4009 const char *line_start;
4012 line_start = file->line_start;
4018 for(tokp += 1; tokp < end; tokp++) {
4022 line_start = tokp + 1;
4024 else if ((c == '\\') && (tokp +1 < end)) {
4027 else if (c == '"') {
4028 tok = TOK_LIT_STRING;
4032 if (tok == TOK_UNKNOWN) {
4033 error(state, 0, "unterminated string constant");
4035 if (line != file->line) {
4036 if (state->token_base) {
4037 /* Preprocessor directives cannot span lines */
4038 error(state, 0, "multiline string constant");
4040 warning(state, 0, "multiline string constant");
4043 file->report_line += line - file->line;
4045 file->line_start = line_start;
4047 /* Save the string value */
4048 save_string(state, tk, token, tokp, "literal string");
4050 /* character constants */
4051 else if ((c == '\'') ||
4052 ((c == 'L') && (c1 == '\''))) {
4054 const char *line_start;
4057 line_start = file->line_start;
4063 for(tokp += 1; tokp < end; tokp++) {
4067 line_start = tokp + 1;
4069 else if ((c == '\\') && (tokp +1 < end)) {
4072 else if (c == '\'') {
4077 if (tok == TOK_UNKNOWN) {
4078 error(state, 0, "unterminated character constant");
4080 if (line != file->line) {
4081 if (state->token_base) {
4082 /* Preprocessor directives cannot span lines */
4083 error(state, 0, "multiline character constant");
4085 warning(state, 0, "multiline character constant");
4088 file->report_line += line - file->line;
4090 file->line_start = line_start;
4092 /* Save the character value */
4093 save_string(state, tk, token, tokp, "literal character");
4095 /* integer and floating constants
4101 * Floating constants
4102 * {digits}.{digits}[Ee][+-]?{digits}
4104 * {digits}[Ee][+-]?{digits}
4105 * .{digits}[Ee][+-]?{digits}
4109 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
4110 const char *next, *new;
4114 next = after_digits(tokp, end);
4119 if (next[0] == '.') {
4120 new = after_digits(next, end);
4121 is_float = (new != next);
4124 if ((next[0] == 'e') || (next[0] == 'E')) {
4125 if (((next + 1) < end) &&
4126 ((next[1] == '+') || (next[1] == '-'))) {
4129 new = after_digits(next, end);
4130 is_float = (new != next);
4134 tok = TOK_LIT_FLOAT;
4135 if ((next < end) && (
4144 if (!is_float && digitp(c)) {
4146 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
4147 next = after_hexdigits(tokp + 2, end);
4149 else if (c == '0') {
4150 next = after_octdigits(tokp, end);
4153 next = after_digits(tokp, end);
4155 /* crazy integer suffixes */
4157 ((next[0] == 'u') || (next[0] == 'U'))) {
4160 ((next[0] == 'l') || (next[0] == 'L'))) {
4164 else if ((next < end) &&
4165 ((next[0] == 'l') || (next[0] == 'L'))) {
4168 ((next[0] == 'u') || (next[0] == 'U'))) {
4175 /* Save the integer/floating point value */
4176 save_string(state, tk, token, tokp, "literal number");
4179 else if (letterp(c)) {
4181 tokp = identifier(tokp, end);
4183 tk->ident = lookup(state, token, tokp +1 - token);
4184 /* See if this identifier can be macro expanded */
4185 tk->val.notmacro = 0;
4186 if ((tokp < end) && (tokp[1] == '$')) {
4188 tk->val.notmacro = 1;
4191 /* C99 alternate macro characters */
4192 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
4194 tok = TOK_CONCATENATE;
4196 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { tokp += 2; tok = TOK_DOTS; }
4197 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { tokp += 2; tok = TOK_SLEQ; }
4198 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { tokp += 2; tok = TOK_SREQ; }
4199 else if ((c == '*') && (c1 == '=')) { tokp += 1; tok = TOK_TIMESEQ; }
4200 else if ((c == '/') && (c1 == '=')) { tokp += 1; tok = TOK_DIVEQ; }
4201 else if ((c == '%') && (c1 == '=')) { tokp += 1; tok = TOK_MODEQ; }
4202 else if ((c == '+') && (c1 == '=')) { tokp += 1; tok = TOK_PLUSEQ; }
4203 else if ((c == '-') && (c1 == '=')) { tokp += 1; tok = TOK_MINUSEQ; }
4204 else if ((c == '&') && (c1 == '=')) { tokp += 1; tok = TOK_ANDEQ; }
4205 else if ((c == '^') && (c1 == '=')) { tokp += 1; tok = TOK_XOREQ; }
4206 else if ((c == '|') && (c1 == '=')) { tokp += 1; tok = TOK_OREQ; }
4207 else if ((c == '=') && (c1 == '=')) { tokp += 1; tok = TOK_EQEQ; }
4208 else if ((c == '!') && (c1 == '=')) { tokp += 1; tok = TOK_NOTEQ; }
4209 else if ((c == '|') && (c1 == '|')) { tokp += 1; tok = TOK_LOGOR; }
4210 else if ((c == '&') && (c1 == '&')) { tokp += 1; tok = TOK_LOGAND; }
4211 else if ((c == '<') && (c1 == '=')) { tokp += 1; tok = TOK_LESSEQ; }
4212 else if ((c == '>') && (c1 == '=')) { tokp += 1; tok = TOK_MOREEQ; }
4213 else if ((c == '<') && (c1 == '<')) { tokp += 1; tok = TOK_SL; }
4214 else if ((c == '>') && (c1 == '>')) { tokp += 1; tok = TOK_SR; }
4215 else if ((c == '+') && (c1 == '+')) { tokp += 1; tok = TOK_PLUSPLUS; }
4216 else if ((c == '-') && (c1 == '-')) { tokp += 1; tok = TOK_MINUSMINUS; }
4217 else if ((c == '-') && (c1 == '>')) { tokp += 1; tok = TOK_ARROW; }
4218 else if ((c == '<') && (c1 == ':')) { tokp += 1; tok = TOK_LBRACKET; }
4219 else if ((c == ':') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACKET; }
4220 else if ((c == '<') && (c1 == '%')) { tokp += 1; tok = TOK_LBRACE; }
4221 else if ((c == '%') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACE; }
4222 else if ((c == '%') && (c1 == ':')) { tokp += 1; tok = TOK_MACRO; }
4223 else if ((c == '#') && (c1 == '#')) { tokp += 1; tok = TOK_CONCATENATE; }
4224 else if (c == ';') { tok = TOK_SEMI; }
4225 else if (c == '{') { tok = TOK_LBRACE; }
4226 else if (c == '}') { tok = TOK_RBRACE; }
4227 else if (c == ',') { tok = TOK_COMMA; }
4228 else if (c == '=') { tok = TOK_EQ; }
4229 else if (c == ':') { tok = TOK_COLON; }
4230 else if (c == '[') { tok = TOK_LBRACKET; }
4231 else if (c == ']') { tok = TOK_RBRACKET; }
4232 else if (c == '(') { tok = TOK_LPAREN; }
4233 else if (c == ')') { tok = TOK_RPAREN; }
4234 else if (c == '*') { tok = TOK_STAR; }
4235 else if (c == '>') { tok = TOK_MORE; }
4236 else if (c == '<') { tok = TOK_LESS; }
4237 else if (c == '?') { tok = TOK_QUEST; }
4238 else if (c == '|') { tok = TOK_OR; }
4239 else if (c == '&') { tok = TOK_AND; }
4240 else if (c == '^') { tok = TOK_XOR; }
4241 else if (c == '+') { tok = TOK_PLUS; }
4242 else if (c == '-') { tok = TOK_MINUS; }
4243 else if (c == '/') { tok = TOK_DIV; }
4244 else if (c == '%') { tok = TOK_MOD; }
4245 else if (c == '!') { tok = TOK_BANG; }
4246 else if (c == '.') { tok = TOK_DOT; }
4247 else if (c == '~') { tok = TOK_TILDE; }
4248 else if (c == '#') { tok = TOK_MACRO; }
4250 file->pos = tokp + 1;
4252 if (tok == TOK_IDENT) {
4253 if (state->token_base == 0) {
4254 ident_to_keyword(state, tk);
4256 ident_to_macro(state, tk);
4261 static void next_token(struct compile_state *state, struct token *tk)
4263 struct file_state *file;
4265 /* Don't return space tokens. */
4267 raw_next_token(state, file, tk);
4268 if (tk->tok == TOK_MACRO) {
4269 /* Only match preprocessor directives at the start of a line */
4271 for(ptr = file->line_start; spacep(*ptr); ptr++)
4273 if (ptr != file->pos - 1) {
4274 tk->tok = TOK_UNKNOWN;
4277 if (tk->tok == TOK_UNKNOWN) {
4278 error(state, 0, "unknown token");
4280 } while(tk->tok == TOK_SPACE);
4283 static void check_tok(struct compile_state *state, struct token *tk, int tok)
4285 if (tk->tok != tok) {
4286 const char *name1, *name2;
4287 name1 = tokens[tk->tok];
4289 if ((tk->tok == TOK_IDENT) || (tk->tok == TOK_MIDENT)) {
4290 name2 = tk->ident->name;
4292 error(state, 0, "\tfound %s %s expected %s",
4293 name1, name2, tokens[tok]);
4297 struct macro_arg_value {
4298 struct hash_entry *ident;
4299 unsigned char *value;
4302 static struct macro_arg_value *read_macro_args(
4303 struct compile_state *state, struct macro *macro,
4304 struct file_state *file, struct token *tk)
4306 struct macro_arg_value *argv;
4307 struct macro_arg *arg;
4311 if (macro->argc == 0) {
4313 raw_next_token(state, file, tk);
4314 } while(tk->tok == TOK_SPACE);
4317 argv = xcmalloc(sizeof(*argv) * macro->argc, "macro args");
4318 for(i = 0, arg = macro->args; arg; arg = arg->next, i++) {
4321 argv[i].ident = arg->ident;
4330 raw_next_token(state, file, tk);
4332 if (!paren_depth && (tk->tok == TOK_COMMA) &&
4333 (argv[i].ident != state->i___VA_ARGS__))
4336 if (i >= macro->argc) {
4337 error(state, 0, "too many args to %s\n",
4338 macro->ident->name);
4343 if (tk->tok == TOK_LPAREN) {
4347 if (tk->tok == TOK_RPAREN) {
4348 if (paren_depth == 0) {
4353 if (tk->tok == TOK_EOF) {
4354 error(state, 0, "End of file encountered while parsing macro arguments");
4357 len = file->pos - start;
4358 argv[i].value = xrealloc(
4359 argv[i].value, argv[i].len + len, "macro args");
4360 memcpy(argv[i].value + argv[i].len, start, len);
4363 if (i != macro->argc -1) {
4364 error(state, 0, "missing %s arg %d\n",
4365 macro->ident->name, i +2);
4371 static void free_macro_args(struct macro *macro, struct macro_arg_value *argv)
4374 for(i = 0; i < macro->argc; i++) {
4375 xfree(argv[i].value);
4385 static void append_macro_text(struct compile_state *state,
4386 struct macro *macro, struct macro_buf *buf,
4387 const char *fstart, size_t flen)
4390 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4391 buf->pos, buf->pos, buf->str,
4392 flen, flen, fstart);
4394 if ((buf->pos + flen) < buf->len) {
4395 memcpy(buf->str + buf->pos, fstart, flen);
4397 buf->str = xrealloc(buf->str, buf->len + flen, macro->ident->name);
4398 memcpy(buf->str + buf->pos, fstart, flen);
4404 static int compile_macro(struct compile_state *state,
4405 struct file_state **filep, struct token *tk);
4407 static void macro_expand_args(struct compile_state *state,
4408 struct macro *macro, struct macro_arg_value *argv, struct token *tk)
4412 for(i = 0; i < macro->argc; i++) {
4413 struct file_state fmacro, *file;
4414 struct macro_buf buf;
4418 fmacro.basename = argv[i].ident->name;
4419 fmacro.dirname = "";
4420 fmacro.size = argv[i].len;
4421 fmacro.buf = argv[i].value;
4422 fmacro.pos = fmacro.buf;
4423 fmacro.line_start = fmacro.buf;
4425 fmacro.report_line = 1;
4426 fmacro.report_name = fmacro.basename;
4427 fmacro.report_dir = fmacro.dirname;
4430 buf.len = argv[i].len;
4431 buf.str = xmalloc(buf.len, argv[i].ident->name);
4437 raw_next_token(state, file, tk);
4438 flen = file->pos - fstart;
4440 if (tk->tok == TOK_EOF) {
4441 struct file_state *old;
4447 /* old->basename is used keep it */
4448 xfree(old->dirname);
4453 else if (tk->ident && tk->ident->sym_define) {
4454 if (compile_macro(state, &file, tk)) {
4459 append_macro_text(state, macro, &buf,
4463 xfree(argv[i].value);
4464 argv[i].value = buf.str;
4465 argv[i].len = buf.pos;
4470 static void expand_macro(struct compile_state *state,
4471 struct macro *macro, struct macro_buf *buf,
4472 struct macro_arg_value *argv, struct token *tk)
4474 struct file_state fmacro;
4475 const char space[] = " ";
4479 fmacro.basename = macro->ident->name;
4480 fmacro.dirname = "";
4481 fmacro.size = macro->buf_len - macro->buf_off;;
4482 fmacro.buf = macro->buf + macro->buf_off;
4483 fmacro.pos = fmacro.buf;
4484 fmacro.line_start = fmacro.buf;
4486 fmacro.report_line = 1;
4487 fmacro.report_name = fmacro.basename;
4488 fmacro.report_dir = fmacro.dirname;
4491 buf->len = macro->buf_len + 3;
4492 buf->str = xmalloc(buf->len, macro->ident->name);
4495 fstart = fmacro.pos;
4496 raw_next_token(state, &fmacro, tk);
4497 while(tk->tok != TOK_EOF) {
4498 flen = fmacro.pos - fstart;
4501 for(i = 0; i < macro->argc; i++) {
4502 if (argv[i].ident == tk->ident) {
4506 if (i >= macro->argc) {
4509 /* Substitute macro parameter */
4510 fstart = argv[i].value;
4514 if (!macro->buf_off) {
4518 raw_next_token(state, &fmacro, tk);
4519 } while(tk->tok == TOK_SPACE);
4520 check_tok(state, tk, TOK_IDENT);
4521 for(i = 0; i < macro->argc; i++) {
4522 if (argv[i].ident == tk->ident) {
4526 if (i >= macro->argc) {
4527 error(state, 0, "parameter `%s' not found",
4530 /* Stringize token */
4531 append_macro_text(state, macro, buf, "\"", 1);
4532 for(j = 0; j < argv[i].len; j++) {
4533 char *str = argv[i].value + j;
4539 else if (*str == '"') {
4543 append_macro_text(state, macro, buf, str, len);
4545 append_macro_text(state, macro, buf, "\"", 1);
4549 case TOK_CONCATENATE:
4550 /* Concatenate tokens */
4551 /* Delete the previous whitespace token */
4552 if (buf->str[buf->pos - 1] == ' ') {
4555 /* Skip the next sequence of whitspace tokens */
4557 fstart = fmacro.pos;
4558 raw_next_token(state, &fmacro, tk);
4559 } while(tk->tok == TOK_SPACE);
4560 /* Restart at the top of the loop.
4561 * I need to process the non white space token.
4566 /* Collapse multiple spaces into one */
4567 if (buf->str[buf->pos - 1] != ' ') {
4579 append_macro_text(state, macro, buf, fstart, flen);
4581 fstart = fmacro.pos;
4582 raw_next_token(state, &fmacro, tk);
4586 static void tag_macro_name(struct compile_state *state,
4587 struct macro *macro, struct macro_buf *buf,
4590 /* Guard all instances of the macro name in the replacement
4591 * text from further macro expansion.
4593 struct file_state fmacro;
4596 fmacro.basename = macro->ident->name;
4597 fmacro.dirname = "";
4598 fmacro.size = buf->pos;
4599 fmacro.buf = buf->str;
4600 fmacro.pos = fmacro.buf;
4601 fmacro.line_start = fmacro.buf;
4603 fmacro.report_line = 1;
4604 fmacro.report_name = fmacro.basename;
4605 fmacro.report_dir = fmacro.dirname;
4608 buf->len = macro->buf_len + 3;
4609 buf->str = xmalloc(buf->len, macro->ident->name);
4612 fstart = fmacro.pos;
4613 raw_next_token(state, &fmacro, tk);
4614 while(tk->tok != TOK_EOF) {
4615 flen = fmacro.pos - fstart;
4616 if ((tk->tok == TOK_IDENT) &&
4617 (tk->ident == macro->ident) &&
4618 (tk->val.notmacro == 0)) {
4619 append_macro_text(state, macro, buf, fstart, flen);
4624 append_macro_text(state, macro, buf, fstart, flen);
4626 fstart = fmacro.pos;
4627 raw_next_token(state, &fmacro, tk);
4632 static int compile_macro(struct compile_state *state,
4633 struct file_state **filep, struct token *tk)
4635 struct file_state *file;
4636 struct hash_entry *ident;
4637 struct macro *macro;
4638 struct macro_arg_value *argv;
4639 struct macro_buf buf;
4642 fprintf(state->errout, "macro: %s\n", tk->ident->name);
4645 macro = ident->sym_define;
4647 /* If this token comes from a macro expansion ignore it */
4648 if (tk->val.notmacro) {
4651 /* If I am a function like macro and the identifier is not followed
4652 * by a left parenthesis, do nothing.
4654 if ((macro->buf_off != 0) && !lparen_peek(state, *filep)) {
4658 /* Read in the macro arguments */
4660 if (macro->buf_off) {
4661 raw_next_token(state, *filep, tk);
4662 check_tok(state, tk, TOK_LPAREN);
4664 argv = read_macro_args(state, macro, *filep, tk);
4666 check_tok(state, tk, TOK_RPAREN);
4668 /* Macro expand the macro arguments */
4669 macro_expand_args(state, macro, argv, tk);
4674 if (ident == state->i___FILE__) {
4675 buf.len = strlen(state->file->basename) + 1 + 2 + 3;
4676 buf.str = xmalloc(buf.len, ident->name);
4677 sprintf(buf.str, "\"%s\"", state->file->basename);
4678 buf.pos = strlen(buf.str);
4680 else if (ident == state->i___LINE__) {
4682 buf.str = xmalloc(buf.len, ident->name);
4683 sprintf(buf.str, "%d", state->file->line);
4684 buf.pos = strlen(buf.str);
4687 expand_macro(state, macro, &buf, argv, tk);
4689 /* Tag the macro name with a $ so it will no longer
4690 * be regonized as a canidate for macro expansion.
4692 tag_macro_name(state, macro, &buf, tk);
4693 append_macro_text(state, macro, &buf, "\n\0", 2);
4696 fprintf(state->errout, "%s: %d -> `%*.*s'\n",
4697 ident->name, buf.pos, buf.pos, (int)(buf.pos), buf.str);
4700 free_macro_args(macro, argv);
4702 file = xmalloc(sizeof(*file), "file_state");
4703 file->basename = xstrdup(ident->name);
4704 file->dirname = xstrdup("");
4705 file->buf = buf.str;
4706 file->size = buf.pos - 2;
4707 file->pos = file->buf;
4708 file->line_start = file->pos;
4710 file->report_line = 1;
4711 file->report_name = file->basename;
4712 file->report_dir = file->dirname;
4713 file->prev = *filep;
4718 static void eat_tokens(struct compile_state *state, int targ_tok)
4720 if (state->eat_depth > 0) {
4721 internal_error(state, 0, "Already eating...");
4723 state->eat_depth = state->if_depth;
4724 state->eat_targ = targ_tok;
4726 static int if_eat(struct compile_state *state)
4728 return state->eat_depth > 0;
4730 static int if_value(struct compile_state *state)
4733 index = state->if_depth / CHAR_BIT;
4734 offset = state->if_depth % CHAR_BIT;
4735 return !!(state->if_bytes[index] & (1 << (offset)));
4737 static void set_if_value(struct compile_state *state, int value)
4740 index = state->if_depth / CHAR_BIT;
4741 offset = state->if_depth % CHAR_BIT;
4743 state->if_bytes[index] &= ~(1 << offset);
4745 state->if_bytes[index] |= (1 << offset);
4748 static void in_if(struct compile_state *state, const char *name)
4750 if (state->if_depth <= 0) {
4751 error(state, 0, "%s without #if", name);
4754 static void enter_if(struct compile_state *state)
4756 state->if_depth += 1;
4757 if (state->if_depth > MAX_CPP_IF_DEPTH) {
4758 error(state, 0, "#if depth too great");
4761 static void reenter_if(struct compile_state *state, const char *name)
4764 if ((state->eat_depth == state->if_depth) &&
4765 (state->eat_targ == TOK_MELSE)) {
4766 state->eat_depth = 0;
4767 state->eat_targ = 0;
4770 static void enter_else(struct compile_state *state, const char *name)
4773 if ((state->eat_depth == state->if_depth) &&
4774 (state->eat_targ == TOK_MELSE)) {
4775 state->eat_depth = 0;
4776 state->eat_targ = 0;
4779 static void exit_if(struct compile_state *state, const char *name)
4782 if (state->eat_depth == state->if_depth) {
4783 state->eat_depth = 0;
4784 state->eat_targ = 0;
4786 state->if_depth -= 1;
4789 static void cpp_token(struct compile_state *state, struct token *tk)
4791 struct file_state *file;
4794 next_token(state, tk);
4798 /* Exit out of an include directive or macro call */
4799 if ((tk->tok == TOK_EOF) &&
4800 (state->file && state->macro_file) &&
4803 state->file = file->prev;
4804 /* file->basename is used keep it */
4805 xfree(file->dirname);
4808 next_token(state, tk);
4814 static void preprocess(struct compile_state *state, struct token *tk);
4816 static void token(struct compile_state *state, struct token *tk)
4819 cpp_token(state, tk);
4822 /* Process a macro directive */
4823 if (tk->tok == TOK_MACRO) {
4824 preprocess(state, tk);
4827 /* Expand a macro call */
4828 else if (tk->ident && tk->ident->sym_define) {
4829 rescan = compile_macro(state, &state->file, tk);
4831 cpp_token(state, tk);
4834 /* Eat tokens disabled by the preprocessor */
4835 else if (if_eat(state)) {
4836 cpp_token(state, tk);
4839 /* When not in macro context hide EOL */
4840 else if ((tk->tok == TOK_EOL) && (state->token_base == 0)) {
4841 next_token(state, tk);
4848 static inline struct token *get_token(struct compile_state *state, int offset)
4851 index = state->token_base + offset;
4852 if (index >= sizeof(state->token)/sizeof(state->token[0])) {
4853 internal_error(state, 0, "token array to small");
4855 return &state->token[index];
4858 static struct token *do_eat_token(struct compile_state *state, int tok)
4862 check_tok(state, get_token(state, 1), tok);
4864 /* Free the old token value */
4865 tk = get_token(state, 0);
4867 memset((void *)tk->val.str, -1, tk->str_len);
4870 /* Overwrite the old token with newer tokens */
4871 for(i = state->token_base; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
4872 state->token[i] = state->token[i + 1];
4874 /* Clear the last token */
4875 memset(&state->token[i], 0, sizeof(state->token[i]));
4876 state->token[i].tok = -1;
4878 /* Return the token */
4882 static int cpp_peek(struct compile_state *state)
4885 tk1 = get_token(state, 1);
4886 if (tk1->tok == -1) {
4887 cpp_token(state, tk1);
4892 static struct token *cpp_eat(struct compile_state *state, int tok)
4895 return do_eat_token(state, tok);
4898 static int peek(struct compile_state *state)
4901 tk1 = get_token(state, 1);
4902 if (tk1->tok == -1) {
4908 static int peek2(struct compile_state *state)
4910 struct token *tk1, *tk2;
4911 tk1 = get_token(state, 1);
4912 tk2 = get_token(state, 2);
4913 if (tk1->tok == -1) {
4916 if (tk2->tok == -1) {
4922 static struct token *eat(struct compile_state *state, int tok)
4925 return do_eat_token(state, tok);
4928 static void compile_file(struct compile_state *state, const char *filename, int local)
4930 char cwd[MAX_CWD_SIZE];
4931 const char *subdir, *base;
4933 struct file_state *file;
4935 file = xmalloc(sizeof(*file), "file_state");
4937 base = strrchr(filename, '/');
4940 subdir_len = base - filename;
4947 basename = xmalloc(strlen(base) +1, "basename");
4948 strcpy(basename, base);
4949 file->basename = basename;
4951 if (getcwd(cwd, sizeof(cwd)) == 0) {
4952 die("cwd buffer to small");
4954 if (subdir[0] == '/') {
4955 file->dirname = xmalloc(subdir_len + 1, "dirname");
4956 memcpy(file->dirname, subdir, subdir_len);
4957 file->dirname[subdir_len] = '\0';
4963 /* Find the appropriate directory... */
4965 if (!state->file && exists(cwd, filename)) {
4968 if (local && state->file && exists(state->file->dirname, filename)) {
4969 dir = state->file->dirname;
4971 for(path = state->compiler->include_paths; !dir && *path; path++) {
4972 if (exists(*path, filename)) {
4977 error(state, 0, "Cannot find `%s'\n", filename);
4979 dirlen = strlen(dir);
4980 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
4981 memcpy(file->dirname, dir, dirlen);
4982 file->dirname[dirlen] = '/';
4983 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
4984 file->dirname[dirlen + 1 + subdir_len] = '\0';
4986 file->buf = slurp_file(file->dirname, file->basename, &file->size);
4988 file->pos = file->buf;
4989 file->line_start = file->pos;
4992 file->report_line = 1;
4993 file->report_name = file->basename;
4994 file->report_dir = file->dirname;
4996 file->prev = state->file;
4999 process_trigraphs(state);
5000 splice_lines(state);
5003 static struct triple *constant_expr(struct compile_state *state);
5004 static void integral(struct compile_state *state, struct triple *def);
5006 static int mcexpr(struct compile_state *state)
5008 struct triple *cvalue;
5009 cvalue = constant_expr(state);
5010 integral(state, cvalue);
5011 if (cvalue->op != OP_INTCONST) {
5012 error(state, 0, "integer constant expected");
5014 return cvalue->u.cval != 0;
5017 static void preprocess(struct compile_state *state, struct token *current_token)
5019 /* Doing much more with the preprocessor would require
5020 * a parser and a major restructuring.
5021 * Postpone that for later.
5023 struct file_state *file;
5029 state->macro_line = line = file->line;
5030 state->macro_file = file;
5032 old_token_base = state->token_base;
5033 state->token_base = current_token - state->token;
5035 tok = cpp_peek(state);
5041 tk = cpp_eat(state, TOK_LIT_INT);
5042 override_line = strtoul(tk->val.str, 0, 10);
5043 /* I have a cpp line marker parse it */
5044 if (cpp_peek(state) == TOK_LIT_STRING) {
5045 const char *token, *base;
5047 int name_len, dir_len;
5048 tk = cpp_eat(state, TOK_LIT_STRING);
5049 name = xmalloc(tk->str_len, "report_name");
5050 token = tk->val.str + 1;
5051 base = strrchr(token, '/');
5052 name_len = tk->str_len -2;
5054 dir_len = base - token;
5056 name_len -= base - token;
5061 memcpy(name, base, name_len);
5062 name[name_len] = '\0';
5063 dir = xmalloc(dir_len + 1, "report_dir");
5064 memcpy(dir, token, dir_len);
5065 dir[dir_len] = '\0';
5066 file->report_line = override_line - 1;
5067 file->report_name = name;
5068 file->report_dir = dir;
5075 cpp_eat(state, TOK_MLINE);
5076 tk = eat(state, TOK_LIT_INT);
5077 file->report_line = strtoul(tk->val.str, 0, 10) -1;
5078 if (cpp_peek(state) == TOK_LIT_STRING) {
5079 const char *token, *base;
5081 int name_len, dir_len;
5082 tk = cpp_eat(state, TOK_LIT_STRING);
5083 name = xmalloc(tk->str_len, "report_name");
5084 token = tk->val.str + 1;
5085 base = strrchr(token, '/');
5086 name_len = tk->str_len - 2;
5088 dir_len = base - token;
5090 name_len -= base - token;
5095 memcpy(name, base, name_len);
5096 name[name_len] = '\0';
5097 dir = xmalloc(dir_len + 1, "report_dir");
5098 memcpy(dir, token, dir_len);
5099 dir[dir_len] = '\0';
5100 file->report_name = name;
5101 file->report_dir = dir;
5107 struct hash_entry *ident;
5108 cpp_eat(state, TOK_MUNDEF);
5109 if (if_eat(state)) /* quit early when #if'd out */
5112 ident = cpp_eat(state, TOK_MIDENT)->ident;
5114 undef_macro(state, ident);
5118 cpp_eat(state, TOK_MPRAGMA);
5119 if (if_eat(state)) /* quit early when #if'd out */
5121 warning(state, 0, "Ignoring pragma");
5124 cpp_eat(state, TOK_MELIF);
5125 reenter_if(state, "#elif");
5126 if (if_eat(state)) /* quit early when #if'd out */
5128 /* If the #if was taken the #elif just disables the following code */
5129 if (if_value(state)) {
5130 eat_tokens(state, TOK_MENDIF);
5132 /* If the previous #if was not taken see if the #elif enables the
5136 set_if_value(state, mcexpr(state));
5137 if (!if_value(state)) {
5138 eat_tokens(state, TOK_MELSE);
5143 cpp_eat(state, TOK_MIF);
5145 if (if_eat(state)) /* quit early when #if'd out */
5147 set_if_value(state, mcexpr(state));
5148 if (!if_value(state)) {
5149 eat_tokens(state, TOK_MELSE);
5154 struct hash_entry *ident;
5156 cpp_eat(state, TOK_MIFNDEF);
5158 if (if_eat(state)) /* quit early when #if'd out */
5160 ident = cpp_eat(state, TOK_MIDENT)->ident;
5161 set_if_value(state, ident->sym_define == 0);
5162 if (!if_value(state)) {
5163 eat_tokens(state, TOK_MELSE);
5169 struct hash_entry *ident;
5170 cpp_eat(state, TOK_MIFDEF);
5172 if (if_eat(state)) /* quit early when #if'd out */
5174 ident = cpp_eat(state, TOK_MIDENT)->ident;
5175 set_if_value(state, ident->sym_define != 0);
5176 if (!if_value(state)) {
5177 eat_tokens(state, TOK_MELSE);
5182 cpp_eat(state, TOK_MELSE);
5183 enter_else(state, "#else");
5184 if (!if_eat(state) && if_value(state)) {
5185 eat_tokens(state, TOK_MENDIF);
5189 cpp_eat(state, TOK_MENDIF);
5190 exit_if(state, "#endif");
5194 struct hash_entry *ident;
5195 struct macro_arg *args, **larg;
5196 const char *start, *mstart, *ptr;
5198 cpp_eat(state, TOK_MDEFINE);
5199 if (if_eat(state)) /* quit early when #if'd out */
5202 ident = cpp_eat(state, TOK_MIDENT)->ident;
5206 /* Remember the start of the macro */
5209 /* Find the end of the line. */
5210 for(ptr = start; *ptr != '\n'; ptr++)
5213 /* remove the trailing whitespace */
5215 while(spacep(*ptr)) {
5219 /* Remove leading whitespace */
5220 while(spacep(*start) && (start < ptr)) {
5223 /* Remember where the macro starts */
5226 /* Parse macro parameters */
5227 if (lparen_peek(state, state->file)) {
5228 cpp_eat(state, TOK_LPAREN);
5231 struct macro_arg *narg, *arg;
5232 struct hash_entry *aident;
5235 tok = cpp_peek(state);
5236 if (!args && (tok == TOK_RPAREN)) {
5239 else if (tok == TOK_DOTS) {
5240 cpp_eat(state, TOK_DOTS);
5241 aident = state->i___VA_ARGS__;
5244 aident = cpp_eat(state, TOK_MIDENT)->ident;
5247 narg = xcmalloc(sizeof(*arg), "macro arg");
5248 narg->ident = aident;
5250 /* Verify I don't have a duplicate identifier */
5251 for(arg = args; arg; arg = arg->next) {
5252 if (arg->ident == narg->ident) {
5253 error(state, 0, "Duplicate macro arg `%s'",
5257 /* Add the new argument to the end of the list */
5261 if ((aident == state->i___VA_ARGS__) ||
5262 (cpp_peek(state) != TOK_COMMA)) {
5265 cpp_eat(state, TOK_COMMA);
5267 cpp_eat(state, TOK_RPAREN);
5269 /* Get the start of the macro body */
5272 /* Remove leading whitespace */
5273 while(spacep(*mstart) && (mstart < ptr)) {
5277 define_macro(state, ident, start, ptr - start + 1,
5278 mstart - start, args);
5286 cpp_eat(state, TOK_MERROR);
5287 /* Find the end of the line */
5288 for(end = file->pos; *end != '\n'; end++)
5290 len = (end - file->pos);
5291 if (!if_eat(state)) {
5292 error(state, 0, "%*.*s", len, len, file->pos);
5302 cpp_eat(state, TOK_MWARNING);
5303 /* Find the end of the line */
5304 for(end = file->pos; *end != '\n'; end++)
5306 len = (end - file->pos);
5307 if (!if_eat(state)) {
5308 warning(state, 0, "%*.*s", len, len, file->pos);
5320 cpp_eat(state, TOK_MINCLUDE);
5322 if (tok == TOK_LIT_STRING) {
5326 tk = eat(state, TOK_LIT_STRING);
5327 name = xmalloc(tk->str_len, "include");
5328 token = tk->val.str +1;
5329 name_len = tk->str_len -2;
5330 if (*token == '"') {
5334 memcpy(name, token, name_len);
5335 name[name_len] = '\0';
5338 else if (tok == TOK_LESS) {
5339 const char *start, *end;
5340 eat(state, TOK_LESS);
5342 for(end = start; *end != '\n'; end++) {
5348 error(state, 0, "Unterminated include directive");
5350 name = xmalloc(end - start + 1, "include");
5351 memcpy(name, start, end - start);
5352 name[end - start] = '\0';
5355 eat(state, TOK_MORE);
5358 error(state, 0, "Invalid include directive");
5360 /* Error if there are any tokens after the include */
5361 if (cpp_peek(state) != TOK_EOL) {
5362 error(state, 0, "garbage after include directive");
5364 if (!if_eat(state)) {
5365 compile_file(state, name, local);
5371 /* Ignore # without a follwing ident */
5375 const char *name1, *name2;
5376 name1 = tokens[tok];
5378 if (tok == TOK_MIDENT) {
5379 name2 = get_token(state, 1)->ident->name;
5381 error(state, 0, "Invalid preprocessor directive: %s %s",
5386 /* Consume the rest of the macro line */
5388 tok = cpp_peek(state);
5389 cpp_eat(state, tok);
5390 } while((tok != TOK_EOF) && (tok != TOK_EOL));
5391 state->token_base = old_token_base;
5395 /* Type helper functions */
5397 static struct type *new_type(
5398 unsigned int type, struct type *left, struct type *right)
5400 struct type *result;
5401 result = xmalloc(sizeof(*result), "type");
5402 result->type = type;
5403 result->left = left;
5404 result->right = right;
5405 result->field_ident = 0;
5406 result->type_ident = 0;
5407 result->elements = 0;
5411 static struct type *clone_type(unsigned int specifiers, struct type *old)
5413 struct type *result;
5414 result = xmalloc(sizeof(*result), "type");
5415 memcpy(result, old, sizeof(*result));
5416 result->type &= TYPE_MASK;
5417 result->type |= specifiers;
5421 static struct type *dup_type(struct compile_state *state, struct type *orig)
5424 new = xcmalloc(sizeof(*new), "type");
5425 new->type = orig->type;
5426 new->field_ident = orig->field_ident;
5427 new->type_ident = orig->type_ident;
5428 new->elements = orig->elements;
5430 new->left = dup_type(state, orig->left);
5433 new->right = dup_type(state, orig->right);
5439 static struct type *invalid_type(struct compile_state *state, struct type *type)
5441 struct type *invalid, *member;
5444 internal_error(state, 0, "type missing?");
5446 switch(type->type & TYPE_MASK) {
5448 case TYPE_CHAR: case TYPE_UCHAR:
5449 case TYPE_SHORT: case TYPE_USHORT:
5450 case TYPE_INT: case TYPE_UINT:
5451 case TYPE_LONG: case TYPE_ULONG:
5452 case TYPE_LLONG: case TYPE_ULLONG:
5457 invalid = invalid_type(state, type->left);
5460 invalid = invalid_type(state, type->left);
5464 member = type->left;
5465 while(member && (invalid == 0) &&
5466 ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
5467 invalid = invalid_type(state, member->left);
5468 member = member->right;
5471 invalid = invalid_type(state, member);
5476 member = type->left;
5477 while(member && (invalid == 0) &&
5478 ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
5479 invalid = invalid_type(state, member->left);
5480 member = member->right;
5483 invalid = invalid_type(state, member);
5494 #define MASK_UCHAR(X) ((X) & ((ulong_t)0xff))
5495 #define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT)) - 1))
5496 static inline ulong_t mask_uint(ulong_t x)
5498 if (SIZEOF_INT < SIZEOF_LONG) {
5499 ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT))) -1;
5504 #define MASK_UINT(X) (mask_uint(X))
5505 #define MASK_ULONG(X) (X)
5507 static struct type void_type = { .type = TYPE_VOID };
5508 static struct type char_type = { .type = TYPE_CHAR };
5509 static struct type uchar_type = { .type = TYPE_UCHAR };
5510 static struct type short_type = { .type = TYPE_SHORT };
5511 static struct type ushort_type = { .type = TYPE_USHORT };
5512 static struct type int_type = { .type = TYPE_INT };
5513 static struct type uint_type = { .type = TYPE_UINT };
5514 static struct type long_type = { .type = TYPE_LONG };
5515 static struct type ulong_type = { .type = TYPE_ULONG };
5516 static struct type unknown_type = { .type = TYPE_UNKNOWN };
5518 static struct type void_ptr_type = {
5519 .type = TYPE_POINTER,
5523 static struct type void_func_type = {
5524 .type = TYPE_FUNCTION,
5526 .right = &void_type,
5529 static size_t bits_to_bytes(size_t size)
5531 return (size + SIZEOF_CHAR - 1)/SIZEOF_CHAR;
5534 static struct triple *variable(struct compile_state *state, struct type *type)
5536 struct triple *result;
5537 if ((type->type & STOR_MASK) != STOR_PERM) {
5538 result = triple(state, OP_ADECL, type, 0, 0);
5539 generate_lhs_pieces(state, result);
5542 result = triple(state, OP_SDECL, type, 0, 0);
5547 static void stor_of(FILE *fp, struct type *type)
5549 switch(type->type & STOR_MASK) {
5551 fprintf(fp, "auto ");
5554 fprintf(fp, "static ");
5557 fprintf(fp, "local ");
5560 fprintf(fp, "extern ");
5563 fprintf(fp, "register ");
5566 fprintf(fp, "typedef ");
5568 case STOR_INLINE | STOR_LOCAL:
5569 fprintf(fp, "inline ");
5571 case STOR_INLINE | STOR_STATIC:
5572 fprintf(fp, "static inline");
5574 case STOR_INLINE | STOR_EXTERN:
5575 fprintf(fp, "extern inline");
5578 fprintf(fp, "stor:%x", type->type & STOR_MASK);
5582 static void qual_of(FILE *fp, struct type *type)
5584 if (type->type & QUAL_CONST) {
5585 fprintf(fp, " const");
5587 if (type->type & QUAL_VOLATILE) {
5588 fprintf(fp, " volatile");
5590 if (type->type & QUAL_RESTRICT) {
5591 fprintf(fp, " restrict");
5595 static void name_of(FILE *fp, struct type *type)
5597 unsigned int base_type;
5598 base_type = type->type & TYPE_MASK;
5599 if ((base_type != TYPE_PRODUCT) && (base_type != TYPE_OVERLAP)) {
5604 fprintf(fp, "void");
5608 fprintf(fp, "signed char");
5612 fprintf(fp, "unsigned char");
5616 fprintf(fp, "signed short");
5620 fprintf(fp, "unsigned short");
5624 fprintf(fp, "signed int");
5628 fprintf(fp, "unsigned int");
5632 fprintf(fp, "signed long");
5636 fprintf(fp, "unsigned long");
5640 name_of(fp, type->left);
5645 name_of(fp, type->left);
5647 name_of(fp, type->right);
5650 name_of(fp, type->left);
5652 name_of(fp, type->right);
5655 fprintf(fp, "enum %s",
5656 (type->type_ident)? type->type_ident->name : "");
5660 fprintf(fp, "struct %s { ",
5661 (type->type_ident)? type->type_ident->name : "");
5662 name_of(fp, type->left);
5667 fprintf(fp, "union %s { ",
5668 (type->type_ident)? type->type_ident->name : "");
5669 name_of(fp, type->left);
5674 name_of(fp, type->left);
5675 fprintf(fp, " (*)(");
5676 name_of(fp, type->right);
5680 name_of(fp, type->left);
5681 fprintf(fp, " [%ld]", (long)(type->elements));
5684 fprintf(fp, "tuple { ");
5685 name_of(fp, type->left);
5690 fprintf(fp, "join { ");
5691 name_of(fp, type->left);
5696 name_of(fp, type->left);
5697 fprintf(fp, " : %d ", type->elements);
5701 fprintf(fp, "unknown_t");
5704 fprintf(fp, "????: %x", base_type);
5707 if (type->field_ident && type->field_ident->name) {
5708 fprintf(fp, " .%s", type->field_ident->name);
5712 static size_t align_of(struct compile_state *state, struct type *type)
5716 switch(type->type & TYPE_MASK) {
5725 align = ALIGNOF_CHAR;
5729 align = ALIGNOF_SHORT;
5734 align = ALIGNOF_INT;
5738 align = ALIGNOF_LONG;
5741 align = ALIGNOF_POINTER;
5746 size_t left_align, right_align;
5747 left_align = align_of(state, type->left);
5748 right_align = align_of(state, type->right);
5749 align = (left_align >= right_align) ? left_align : right_align;
5753 align = align_of(state, type->left);
5759 align = align_of(state, type->left);
5762 error(state, 0, "alignof not yet defined for type\n");
5768 static size_t reg_align_of(struct compile_state *state, struct type *type)
5772 switch(type->type & TYPE_MASK) {
5781 align = REG_ALIGNOF_CHAR;
5785 align = REG_ALIGNOF_SHORT;
5790 align = REG_ALIGNOF_INT;
5794 align = REG_ALIGNOF_LONG;
5797 align = REG_ALIGNOF_POINTER;
5802 size_t left_align, right_align;
5803 left_align = reg_align_of(state, type->left);
5804 right_align = reg_align_of(state, type->right);
5805 align = (left_align >= right_align) ? left_align : right_align;
5809 align = reg_align_of(state, type->left);
5815 align = reg_align_of(state, type->left);
5818 error(state, 0, "alignof not yet defined for type\n");
5824 static size_t align_of_in_bytes(struct compile_state *state, struct type *type)
5826 return bits_to_bytes(align_of(state, type));
5828 static size_t size_of(struct compile_state *state, struct type *type);
5829 static size_t reg_size_of(struct compile_state *state, struct type *type);
5831 static size_t needed_padding(struct compile_state *state,
5832 struct type *type, size_t offset)
5834 size_t padding, align;
5835 align = align_of(state, type);
5836 /* Align to the next machine word if the bitfield does completely
5837 * fit into the current word.
5839 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
5841 size = size_of(state, type);
5842 if ((offset + type->elements)/size != offset/size) {
5847 if (offset % align) {
5848 padding = align - (offset % align);
5853 static size_t reg_needed_padding(struct compile_state *state,
5854 struct type *type, size_t offset)
5856 size_t padding, align;
5857 align = reg_align_of(state, type);
5858 /* Align to the next register word if the bitfield does completely
5859 * fit into the current register.
5861 if (((type->type & TYPE_MASK) == TYPE_BITFIELD) &&
5862 (((offset + type->elements)/REG_SIZEOF_REG) != (offset/REG_SIZEOF_REG)))
5864 align = REG_SIZEOF_REG;
5867 if (offset % align) {
5868 padding = align - (offset % align);
5873 static size_t size_of(struct compile_state *state, struct type *type)
5877 switch(type->type & TYPE_MASK) {
5882 size = type->elements;
5890 size = SIZEOF_SHORT;
5902 size = SIZEOF_POINTER;
5908 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
5909 pad = needed_padding(state, type->left, size);
5910 size = size + pad + size_of(state, type->left);
5913 pad = needed_padding(state, type, size);
5914 size = size + pad + size_of(state, type);
5919 size_t size_left, size_right;
5920 size_left = size_of(state, type->left);
5921 size_right = size_of(state, type->right);
5922 size = (size_left >= size_right)? size_left : size_right;
5926 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
5927 internal_error(state, 0, "Invalid array type");
5929 size = size_of(state, type->left) * type->elements;
5936 size = size_of(state, type->left);
5937 /* Pad structures so their size is a multiples of their alignment */
5938 pad = needed_padding(state, type, size);
5946 size = size_of(state, type->left);
5947 /* Pad unions so their size is a multiple of their alignment */
5948 pad = needed_padding(state, type, size);
5953 internal_error(state, 0, "sizeof not yet defined for type");
5959 static size_t reg_size_of(struct compile_state *state, struct type *type)
5963 switch(type->type & TYPE_MASK) {
5968 size = type->elements;
5972 size = REG_SIZEOF_CHAR;
5976 size = REG_SIZEOF_SHORT;
5981 size = REG_SIZEOF_INT;
5985 size = REG_SIZEOF_LONG;
5988 size = REG_SIZEOF_POINTER;
5994 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
5995 pad = reg_needed_padding(state, type->left, size);
5996 size = size + pad + reg_size_of(state, type->left);
5999 pad = reg_needed_padding(state, type, size);
6000 size = size + pad + reg_size_of(state, type);
6005 size_t size_left, size_right;
6006 size_left = reg_size_of(state, type->left);
6007 size_right = reg_size_of(state, type->right);
6008 size = (size_left >= size_right)? size_left : size_right;
6012 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6013 internal_error(state, 0, "Invalid array type");
6015 size = reg_size_of(state, type->left) * type->elements;
6022 size = reg_size_of(state, type->left);
6023 /* Pad structures so their size is a multiples of their alignment */
6024 pad = reg_needed_padding(state, type, size);
6032 size = reg_size_of(state, type->left);
6033 /* Pad unions so their size is a multiple of their alignment */
6034 pad = reg_needed_padding(state, type, size);
6039 internal_error(state, 0, "sizeof not yet defined for type");
6045 static size_t registers_of(struct compile_state *state, struct type *type)
6048 registers = reg_size_of(state, type);
6049 registers += REG_SIZEOF_REG - 1;
6050 registers /= REG_SIZEOF_REG;
6054 static size_t size_of_in_bytes(struct compile_state *state, struct type *type)
6056 return bits_to_bytes(size_of(state, type));
6059 static size_t field_offset(struct compile_state *state,
6060 struct type *type, struct hash_entry *field)
6062 struct type *member;
6067 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6068 member = type->left;
6069 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6070 size += needed_padding(state, member->left, size);
6071 if (member->left->field_ident == field) {
6072 member = member->left;
6075 size += size_of(state, member->left);
6076 member = member->right;
6078 size += needed_padding(state, member, size);
6080 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6081 member = type->left;
6082 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6083 if (member->left->field_ident == field) {
6084 member = member->left;
6087 member = member->right;
6091 internal_error(state, 0, "field_offset only works on structures and unions");
6094 if (!member || (member->field_ident != field)) {
6095 error(state, 0, "member %s not present", field->name);
6100 static size_t field_reg_offset(struct compile_state *state,
6101 struct type *type, struct hash_entry *field)
6103 struct type *member;
6108 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6109 member = type->left;
6110 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6111 size += reg_needed_padding(state, member->left, size);
6112 if (member->left->field_ident == field) {
6113 member = member->left;
6116 size += reg_size_of(state, member->left);
6117 member = member->right;
6120 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6121 member = type->left;
6122 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6123 if (member->left->field_ident == field) {
6124 member = member->left;
6127 member = member->right;
6131 internal_error(state, 0, "field_reg_offset only works on structures and unions");
6134 size += reg_needed_padding(state, member, size);
6135 if (!member || (member->field_ident != field)) {
6136 error(state, 0, "member %s not present", field->name);
6141 static struct type *field_type(struct compile_state *state,
6142 struct type *type, struct hash_entry *field)
6144 struct type *member;
6147 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6148 member = type->left;
6149 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6150 if (member->left->field_ident == field) {
6151 member = member->left;
6154 member = member->right;
6157 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6158 member = type->left;
6159 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6160 if (member->left->field_ident == field) {
6161 member = member->left;
6164 member = member->right;
6168 internal_error(state, 0, "field_type only works on structures and unions");
6171 if (!member || (member->field_ident != field)) {
6172 error(state, 0, "member %s not present", field->name);
6177 static size_t index_offset(struct compile_state *state,
6178 struct type *type, ulong_t index)
6180 struct type *member;
6183 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6184 size = size_of(state, type->left) * index;
6186 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6188 member = type->left;
6190 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6191 size += needed_padding(state, member->left, size);
6193 member = member->left;
6196 size += size_of(state, member->left);
6198 member = member->right;
6200 size += needed_padding(state, member, size);
6202 internal_error(state, 0, "Missing member index: %u", index);
6205 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6208 member = type->left;
6210 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6212 member = member->left;
6216 member = member->right;
6219 internal_error(state, 0, "Missing member index: %u", index);
6223 internal_error(state, 0,
6224 "request for index %u in something not an array, tuple or join",
6230 static size_t index_reg_offset(struct compile_state *state,
6231 struct type *type, ulong_t index)
6233 struct type *member;
6236 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6237 size = reg_size_of(state, type->left) * index;
6239 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6241 member = type->left;
6243 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6244 size += reg_needed_padding(state, member->left, size);
6246 member = member->left;
6249 size += reg_size_of(state, member->left);
6251 member = member->right;
6253 size += reg_needed_padding(state, member, size);
6255 internal_error(state, 0, "Missing member index: %u", index);
6259 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6262 member = type->left;
6264 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6266 member = member->left;
6270 member = member->right;
6273 internal_error(state, 0, "Missing member index: %u", index);
6277 internal_error(state, 0,
6278 "request for index %u in something not an array, tuple or join",
6284 static struct type *index_type(struct compile_state *state,
6285 struct type *type, ulong_t index)
6287 struct type *member;
6288 if (index >= type->elements) {
6289 internal_error(state, 0, "Invalid element %u requested", index);
6291 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6292 member = type->left;
6294 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6296 member = type->left;
6298 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6300 member = member->left;
6304 member = member->right;
6307 internal_error(state, 0, "Missing member index: %u", index);
6310 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6312 member = type->left;
6314 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6316 member = member->left;
6320 member = member->right;
6323 internal_error(state, 0, "Missing member index: %u", index);
6328 internal_error(state, 0,
6329 "request for index %u in something not an array, tuple or join",
6335 static struct type *unpack_type(struct compile_state *state, struct type *type)
6337 /* If I have a single register compound type not a bit-field
6338 * find the real type.
6340 struct type *start_type;
6342 /* Get out early if I need multiple registers for this type */
6343 size = reg_size_of(state, type);
6344 if (size > REG_SIZEOF_REG) {
6347 /* Get out early if I don't need any registers for this type */
6351 /* Loop until I have no more layers I can remove */
6354 switch(type->type & TYPE_MASK) {
6356 /* If I have a single element the unpacked type
6359 if (type->elements == 1) {
6365 /* If I have a single element the unpacked type
6368 if (type->elements == 1) {
6371 /* If I have multiple elements the unpacked
6372 * type is the non-void element.
6375 struct type *next, *member;
6376 struct type *sub_type;
6382 if ((member->type & TYPE_MASK) == TYPE_PRODUCT) {
6383 next = member->right;
6384 member = member->left;
6386 if (reg_size_of(state, member) > 0) {
6388 internal_error(state, 0, "true compound type in a register");
6401 /* If I have a single element the unpacked type
6404 if (type->elements == 1) {
6407 /* I can't in general unpack union types */
6410 /* If I'm not a compound type I can't unpack it */
6413 } while(start_type != type);
6414 switch(type->type & TYPE_MASK) {
6418 internal_error(state, 0, "irredicible type?");
6424 static int equiv_types(struct type *left, struct type *right);
6425 static int is_compound_type(struct type *type);
6427 static struct type *reg_type(
6428 struct compile_state *state, struct type *type, int reg_offset)
6430 struct type *member;
6433 struct type *invalid;
6434 invalid = invalid_type(state, type);
6436 fprintf(state->errout, "type: ");
6437 name_of(state->errout, type);
6438 fprintf(state->errout, "\n");
6439 fprintf(state->errout, "invalid: ");
6440 name_of(state->errout, invalid);
6441 fprintf(state->errout, "\n");
6442 internal_error(state, 0, "bad input type?");
6446 size = reg_size_of(state, type);
6447 if (reg_offset > size) {
6449 fprintf(state->errout, "type: ");
6450 name_of(state->errout, type);
6451 fprintf(state->errout, "\n");
6452 internal_error(state, 0, "offset outside of type");
6455 switch(type->type & TYPE_MASK) {
6456 /* Don't do anything with the basic types */
6458 case TYPE_CHAR: case TYPE_UCHAR:
6459 case TYPE_SHORT: case TYPE_USHORT:
6460 case TYPE_INT: case TYPE_UINT:
6461 case TYPE_LONG: case TYPE_ULONG:
6462 case TYPE_LLONG: case TYPE_ULLONG:
6463 case TYPE_FLOAT: case TYPE_DOUBLE:
6471 member = type->left;
6472 size = reg_size_of(state, member);
6473 if (size > REG_SIZEOF_REG) {
6474 member = reg_type(state, member, reg_offset % size);
6482 member = type->left;
6483 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6484 size = reg_size_of(state, member->left);
6485 offset += reg_needed_padding(state, member->left, offset);
6486 if ((offset + size) > reg_offset) {
6487 member = member->left;
6491 member = member->right;
6493 offset += reg_needed_padding(state, member, offset);
6494 member = reg_type(state, member, reg_offset - offset);
6500 struct type *join, **jnext, *mnext;
6501 join = new_type(TYPE_JOIN, 0, 0);
6502 jnext = &join->left;
6508 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
6509 mnext = member->right;
6510 member = member->left;
6512 size = reg_size_of(state, member);
6513 if (size > reg_offset) {
6514 struct type *part, *hunt;
6515 part = reg_type(state, member, reg_offset);
6516 /* See if this type is already in the union */
6519 struct type *test = hunt;
6521 if ((test->type & TYPE_MASK) == TYPE_OVERLAP) {
6525 if (equiv_types(part, test)) {
6533 *jnext = new_type(TYPE_OVERLAP, *jnext, part);
6534 jnext = &(*jnext)->right;
6541 if (join->elements == 0) {
6542 internal_error(state, 0, "No elements?");
6549 fprintf(state->errout, "type: ");
6550 name_of(state->errout, type);
6551 fprintf(state->errout, "\n");
6552 internal_error(state, 0, "reg_type not yet defined for type");
6556 /* If I have a single register compound type not a bit-field
6557 * find the real type.
6559 member = unpack_type(state, member);
6561 size = reg_size_of(state, member);
6562 if (size > REG_SIZEOF_REG) {
6563 internal_error(state, 0, "Cannot find type of single register");
6566 invalid = invalid_type(state, member);
6568 fprintf(state->errout, "type: ");
6569 name_of(state->errout, member);
6570 fprintf(state->errout, "\n");
6571 fprintf(state->errout, "invalid: ");
6572 name_of(state->errout, invalid);
6573 fprintf(state->errout, "\n");
6574 internal_error(state, 0, "returning bad type?");
6580 static struct type *next_field(struct compile_state *state,
6581 struct type *type, struct type *prev_member)
6583 struct type *member;
6584 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
6585 internal_error(state, 0, "next_field only works on structures");
6587 member = type->left;
6588 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
6590 member = member->left;
6593 if (member->left == prev_member) {
6596 member = member->right;
6598 if (member == prev_member) {
6602 internal_error(state, 0, "prev_member %s not present",
6603 prev_member->field_ident->name);
6608 typedef void (*walk_type_fields_cb_t)(struct compile_state *state, struct type *type,
6609 size_t ret_offset, size_t mem_offset, void *arg);
6611 static void walk_type_fields(struct compile_state *state,
6612 struct type *type, size_t reg_offset, size_t mem_offset,
6613 walk_type_fields_cb_t cb, void *arg);
6615 static void walk_struct_fields(struct compile_state *state,
6616 struct type *type, size_t reg_offset, size_t mem_offset,
6617 walk_type_fields_cb_t cb, void *arg)
6621 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
6622 internal_error(state, 0, "walk_struct_fields only works on structures");
6625 for(i = 0; i < type->elements; i++) {
6628 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
6629 mtype = mtype->left;
6631 walk_type_fields(state, mtype,
6633 field_reg_offset(state, type, mtype->field_ident),
6635 field_offset(state, type, mtype->field_ident),
6642 static void walk_type_fields(struct compile_state *state,
6643 struct type *type, size_t reg_offset, size_t mem_offset,
6644 walk_type_fields_cb_t cb, void *arg)
6646 switch(type->type & TYPE_MASK) {
6648 walk_struct_fields(state, type, reg_offset, mem_offset, cb, arg);
6658 cb(state, type, reg_offset, mem_offset, arg);
6663 internal_error(state, 0, "walk_type_fields not yet implemented for type");
6667 static void arrays_complete(struct compile_state *state, struct type *type)
6669 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6670 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6671 error(state, 0, "array size not specified");
6673 arrays_complete(state, type->left);
6677 static unsigned int get_basic_type(struct type *type)
6680 basic = type->type & TYPE_MASK;
6681 /* Convert enums to ints */
6682 if (basic == TYPE_ENUM) {
6685 /* Convert bitfields to standard types */
6686 else if (basic == TYPE_BITFIELD) {
6687 if (type->elements <= SIZEOF_CHAR) {
6690 else if (type->elements <= SIZEOF_SHORT) {
6693 else if (type->elements <= SIZEOF_INT) {
6696 else if (type->elements <= SIZEOF_LONG) {
6699 if (!TYPE_SIGNED(type->left->type)) {
6706 static unsigned int do_integral_promotion(unsigned int type)
6708 if (TYPE_INTEGER(type) && (TYPE_RANK(type) < TYPE_RANK(TYPE_INT))) {
6714 static unsigned int do_arithmetic_conversion(
6715 unsigned int left, unsigned int right)
6717 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
6718 return TYPE_LDOUBLE;
6720 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
6723 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
6726 left = do_integral_promotion(left);
6727 right = do_integral_promotion(right);
6728 /* If both operands have the same size done */
6729 if (left == right) {
6732 /* If both operands have the same signedness pick the larger */
6733 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
6734 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
6736 /* If the signed type can hold everything use it */
6737 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
6740 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
6743 /* Convert to the unsigned type with the same rank as the signed type */
6744 else if (TYPE_SIGNED(left)) {
6745 return TYPE_MKUNSIGNED(left);
6748 return TYPE_MKUNSIGNED(right);
6752 /* see if two types are the same except for qualifiers */
6753 static int equiv_types(struct type *left, struct type *right)
6756 /* Error if the basic types do not match */
6757 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6760 type = left->type & TYPE_MASK;
6761 /* If the basic types match and it is a void type we are done */
6762 if (type == TYPE_VOID) {
6765 /* For bitfields we need to compare the sizes */
6766 else if (type == TYPE_BITFIELD) {
6767 return (left->elements == right->elements) &&
6768 (TYPE_SIGNED(left->left->type) == TYPE_SIGNED(right->left->type));
6770 /* if the basic types match and it is an arithmetic type we are done */
6771 else if (TYPE_ARITHMETIC(type)) {
6774 /* If it is a pointer type recurse and keep testing */
6775 else if (type == TYPE_POINTER) {
6776 return equiv_types(left->left, right->left);
6778 else if (type == TYPE_ARRAY) {
6779 return (left->elements == right->elements) &&
6780 equiv_types(left->left, right->left);
6782 /* test for struct equality */
6783 else if (type == TYPE_STRUCT) {
6784 return left->type_ident == right->type_ident;
6786 /* test for union equality */
6787 else if (type == TYPE_UNION) {
6788 return left->type_ident == right->type_ident;
6790 /* Test for equivalent functions */
6791 else if (type == TYPE_FUNCTION) {
6792 return equiv_types(left->left, right->left) &&
6793 equiv_types(left->right, right->right);
6795 /* We only see TYPE_PRODUCT as part of function equivalence matching */
6796 /* We also see TYPE_PRODUCT as part of of tuple equivalence matchin */
6797 else if (type == TYPE_PRODUCT) {
6798 return equiv_types(left->left, right->left) &&
6799 equiv_types(left->right, right->right);
6801 /* We should see TYPE_OVERLAP when comparing joins */
6802 else if (type == TYPE_OVERLAP) {
6803 return equiv_types(left->left, right->left) &&
6804 equiv_types(left->right, right->right);
6806 /* Test for equivalence of tuples */
6807 else if (type == TYPE_TUPLE) {
6808 return (left->elements == right->elements) &&
6809 equiv_types(left->left, right->left);
6811 /* Test for equivalence of joins */
6812 else if (type == TYPE_JOIN) {
6813 return (left->elements == right->elements) &&
6814 equiv_types(left->left, right->left);
6821 static int equiv_ptrs(struct type *left, struct type *right)
6823 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
6824 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
6827 return equiv_types(left->left, right->left);
6830 static struct type *compatible_types(struct type *left, struct type *right)
6832 struct type *result;
6833 unsigned int type, qual_type;
6834 /* Error if the basic types do not match */
6835 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6838 type = left->type & TYPE_MASK;
6839 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
6841 /* if the basic types match and it is an arithmetic type we are done */
6842 if (TYPE_ARITHMETIC(type)) {
6843 result = new_type(qual_type, 0, 0);
6845 /* If it is a pointer type recurse and keep testing */
6846 else if (type == TYPE_POINTER) {
6847 result = compatible_types(left->left, right->left);
6849 result = new_type(qual_type, result, 0);
6852 /* test for struct equality */
6853 else if (type == TYPE_STRUCT) {
6854 if (left->type_ident == right->type_ident) {
6858 /* test for union equality */
6859 else if (type == TYPE_UNION) {
6860 if (left->type_ident == right->type_ident) {
6864 /* Test for equivalent functions */
6865 else if (type == TYPE_FUNCTION) {
6866 struct type *lf, *rf;
6867 lf = compatible_types(left->left, right->left);
6868 rf = compatible_types(left->right, right->right);
6870 result = new_type(qual_type, lf, rf);
6873 /* We only see TYPE_PRODUCT as part of function equivalence matching */
6874 else if (type == TYPE_PRODUCT) {
6875 struct type *lf, *rf;
6876 lf = compatible_types(left->left, right->left);
6877 rf = compatible_types(left->right, right->right);
6879 result = new_type(qual_type, lf, rf);
6883 /* Nothing else is compatible */
6888 /* See if left is a equivalent to right or right is a union member of left */
6889 static int is_subset_type(struct type *left, struct type *right)
6891 if (equiv_types(left, right)) {
6894 if ((left->type & TYPE_MASK) == TYPE_JOIN) {
6895 struct type *member, *mnext;
6900 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
6901 mnext = member->right;
6902 member = member->left;
6904 if (is_subset_type( member, right)) {
6912 static struct type *compatible_ptrs(struct type *left, struct type *right)
6914 struct type *result;
6915 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
6916 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
6919 result = compatible_types(left->left, right->left);
6921 unsigned int qual_type;
6922 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
6923 result = new_type(qual_type, result, 0);
6928 static struct triple *integral_promotion(
6929 struct compile_state *state, struct triple *def)
6933 /* As all operations are carried out in registers
6934 * the values are converted on load I just convert
6935 * logical type of the operand.
6937 if (TYPE_INTEGER(type->type)) {
6938 unsigned int int_type;
6939 int_type = type->type & ~TYPE_MASK;
6940 int_type |= do_integral_promotion(get_basic_type(type));
6941 if (int_type != type->type) {
6942 if (def->op != OP_LOAD) {
6943 def->type = new_type(int_type, 0, 0);
6946 def = triple(state, OP_CONVERT,
6947 new_type(int_type, 0, 0), def, 0);
6955 static void arithmetic(struct compile_state *state, struct triple *def)
6957 if (!TYPE_ARITHMETIC(def->type->type)) {
6958 error(state, 0, "arithmetic type expexted");
6962 static void ptr_arithmetic(struct compile_state *state, struct triple *def)
6964 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
6965 error(state, def, "pointer or arithmetic type expected");
6969 static int is_integral(struct triple *ins)
6971 return TYPE_INTEGER(ins->type->type);
6974 static void integral(struct compile_state *state, struct triple *def)
6976 if (!is_integral(def)) {
6977 error(state, 0, "integral type expected");
6982 static void bool(struct compile_state *state, struct triple *def)
6984 if (!TYPE_ARITHMETIC(def->type->type) &&
6985 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
6986 error(state, 0, "arithmetic or pointer type expected");
6990 static int is_signed(struct type *type)
6992 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
6995 return !!TYPE_SIGNED(type->type);
6997 static int is_compound_type(struct type *type)
7000 switch((type->type & TYPE_MASK)) {
7015 /* Is this value located in a register otherwise it must be in memory */
7016 static int is_in_reg(struct compile_state *state, struct triple *def)
7019 if (def->op == OP_ADECL) {
7022 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
7025 else if (triple_is_part(state, def)) {
7026 in_reg = is_in_reg(state, MISC(def, 0));
7029 internal_error(state, def, "unknown expr storage location");
7035 /* Is this an auto or static variable location? Something that can
7036 * be assigned to. Otherwise it must must be a pure value, a temporary.
7038 static int is_lvalue(struct compile_state *state, struct triple *def)
7045 if ((def->op == OP_ADECL) ||
7046 (def->op == OP_SDECL) ||
7047 (def->op == OP_DEREF) ||
7048 (def->op == OP_BLOBCONST) ||
7049 (def->op == OP_LIST)) {
7052 else if (triple_is_part(state, def)) {
7053 ret = is_lvalue(state, MISC(def, 0));
7058 static void clvalue(struct compile_state *state, struct triple *def)
7061 internal_error(state, def, "nothing where lvalue expected?");
7063 if (!is_lvalue(state, def)) {
7064 error(state, def, "lvalue expected");
7067 static void lvalue(struct compile_state *state, struct triple *def)
7069 clvalue(state, def);
7070 if (def->type->type & QUAL_CONST) {
7071 error(state, def, "modifable lvalue expected");
7075 static int is_pointer(struct triple *def)
7077 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
7080 static void pointer(struct compile_state *state, struct triple *def)
7082 if (!is_pointer(def)) {
7083 error(state, def, "pointer expected");
7087 static struct triple *int_const(
7088 struct compile_state *state, struct type *type, ulong_t value)
7090 struct triple *result;
7091 switch(type->type & TYPE_MASK) {
7093 case TYPE_INT: case TYPE_UINT:
7094 case TYPE_LONG: case TYPE_ULONG:
7097 internal_error(state, 0, "constant for unknown type");
7099 result = triple(state, OP_INTCONST, type, 0, 0);
7100 result->u.cval = value;
7105 static struct triple *read_expr(struct compile_state *state, struct triple *def);
7107 static struct triple *do_mk_addr_expr(struct compile_state *state,
7108 struct triple *expr, struct type *type, ulong_t offset)
7110 struct triple *result;
7111 struct type *ptr_type;
7112 clvalue(state, expr);
7114 ptr_type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
7118 if (expr->op == OP_ADECL) {
7119 error(state, expr, "address of auto variables not supported");
7121 else if (expr->op == OP_SDECL) {
7122 result = triple(state, OP_ADDRCONST, ptr_type, 0, 0);
7123 MISC(result, 0) = expr;
7124 result->u.cval = offset;
7126 else if (expr->op == OP_DEREF) {
7127 result = triple(state, OP_ADD, ptr_type,
7129 int_const(state, &ulong_type, offset));
7131 else if (expr->op == OP_BLOBCONST) {
7133 internal_error(state, expr, "not yet implemented");
7135 else if (expr->op == OP_LIST) {
7136 error(state, 0, "Function addresses not supported");
7138 else if (triple_is_part(state, expr)) {
7139 struct triple *part;
7141 expr = MISC(expr, 0);
7142 if (part->op == OP_DOT) {
7143 offset += bits_to_bytes(
7144 field_offset(state, expr->type, part->u.field));
7146 else if (part->op == OP_INDEX) {
7147 offset += bits_to_bytes(
7148 index_offset(state, expr->type, part->u.cval));
7151 internal_error(state, part, "unhandled part type");
7153 result = do_mk_addr_expr(state, expr, type, offset);
7156 internal_error(state, expr, "cannot take address of expression");
7161 static struct triple *mk_addr_expr(
7162 struct compile_state *state, struct triple *expr, ulong_t offset)
7164 return do_mk_addr_expr(state, expr, expr->type, offset);
7167 static struct triple *mk_deref_expr(
7168 struct compile_state *state, struct triple *expr)
7170 struct type *base_type;
7171 pointer(state, expr);
7172 base_type = expr->type->left;
7173 return triple(state, OP_DEREF, base_type, expr, 0);
7176 /* lvalue conversions always apply except when certain operators
7177 * are applied. So I apply apply it when I know no more
7178 * operators will be applied.
7180 static struct triple *lvalue_conversion(struct compile_state *state, struct triple *def)
7182 /* Tranform an array to a pointer to the first element */
7183 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
7186 TYPE_POINTER | (def->type->type & QUAL_MASK),
7187 def->type->left, 0);
7188 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
7189 struct triple *addrconst;
7190 if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
7191 internal_error(state, def, "bad array constant");
7193 addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
7194 MISC(addrconst, 0) = def;
7198 def = triple(state, OP_CONVERT, type, def, 0);
7201 /* Transform a function to a pointer to it */
7202 else if ((def->type->type & TYPE_MASK) == TYPE_FUNCTION) {
7203 def = mk_addr_expr(state, def, 0);
7208 static struct triple *deref_field(
7209 struct compile_state *state, struct triple *expr, struct hash_entry *field)
7211 struct triple *result;
7212 struct type *type, *member;
7215 internal_error(state, 0, "No field passed to deref_field");
7219 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
7220 ((type->type & TYPE_MASK) != TYPE_UNION)) {
7221 error(state, 0, "request for member %s in something not a struct or union",
7224 member = field_type(state, type, field);
7225 if ((type->type & STOR_MASK) == STOR_PERM) {
7226 /* Do the pointer arithmetic to get a deref the field */
7227 offset = bits_to_bytes(field_offset(state, type, field));
7228 result = do_mk_addr_expr(state, expr, member, offset);
7229 result = mk_deref_expr(state, result);
7232 /* Find the variable for the field I want. */
7233 result = triple(state, OP_DOT, member, expr, 0);
7234 result->u.field = field;
7239 static struct triple *deref_index(
7240 struct compile_state *state, struct triple *expr, size_t index)
7242 struct triple *result;
7243 struct type *type, *member;
7248 member = index_type(state, type, index);
7250 if ((type->type & STOR_MASK) == STOR_PERM) {
7251 offset = bits_to_bytes(index_offset(state, type, index));
7252 result = do_mk_addr_expr(state, expr, member, offset);
7253 result = mk_deref_expr(state, result);
7256 result = triple(state, OP_INDEX, member, expr, 0);
7257 result->u.cval = index;
7262 static struct triple *read_expr(struct compile_state *state, struct triple *def)
7268 #warning "CHECK_ME is this the only place I need to do lvalue conversions?"
7269 /* Transform lvalues into something we can read */
7270 def = lvalue_conversion(state, def);
7271 if (!is_lvalue(state, def)) {
7274 if (is_in_reg(state, def)) {
7277 if (def->op == OP_SDECL) {
7278 def = mk_addr_expr(state, def, 0);
7279 def = mk_deref_expr(state, def);
7283 def = triple(state, op, def->type, def, 0);
7284 if (def->type->type & QUAL_VOLATILE) {
7285 def->id |= TRIPLE_FLAG_VOLATILE;
7290 int is_write_compatible(struct compile_state *state,
7291 struct type *dest, struct type *rval)
7294 /* Both operands have arithmetic type */
7295 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
7298 /* One operand is a pointer and the other is a pointer to void */
7299 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
7300 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
7301 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
7302 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
7305 /* If both types are the same without qualifiers we are good */
7306 else if (equiv_ptrs(dest, rval)) {
7309 /* test for struct/union equality */
7310 else if (equiv_types(dest, rval)) {
7316 static void write_compatible(struct compile_state *state,
7317 struct type *dest, struct type *rval)
7319 if (!is_write_compatible(state, dest, rval)) {
7320 FILE *fp = state->errout;
7321 fprintf(fp, "dest: ");
7323 fprintf(fp,"\nrval: ");
7326 error(state, 0, "Incompatible types in assignment");
7330 static int is_init_compatible(struct compile_state *state,
7331 struct type *dest, struct type *rval)
7334 if (is_write_compatible(state, dest, rval)) {
7337 else if (equiv_types(dest, rval)) {
7343 static struct triple *write_expr(
7344 struct compile_state *state, struct triple *dest, struct triple *rval)
7351 internal_error(state, 0, "missing rval");
7354 if (rval->op == OP_LIST) {
7355 internal_error(state, 0, "expression of type OP_LIST?");
7357 if (!is_lvalue(state, dest)) {
7358 internal_error(state, 0, "writing to a non lvalue?");
7360 if (dest->type->type & QUAL_CONST) {
7361 internal_error(state, 0, "modifable lvalue expexted");
7364 write_compatible(state, dest->type, rval->type);
7365 if (!equiv_types(dest->type, rval->type)) {
7366 rval = triple(state, OP_CONVERT, dest->type, rval, 0);
7369 /* Now figure out which assignment operator to use */
7371 if (is_in_reg(state, dest)) {
7372 def = triple(state, OP_WRITE, dest->type, rval, dest);
7373 if (MISC(def, 0) != dest) {
7374 internal_error(state, def, "huh?");
7376 if (RHS(def, 0) != rval) {
7377 internal_error(state, def, "huh?");
7380 def = triple(state, OP_STORE, dest->type, dest, rval);
7382 if (def->type->type & QUAL_VOLATILE) {
7383 def->id |= TRIPLE_FLAG_VOLATILE;
7388 static struct triple *init_expr(
7389 struct compile_state *state, struct triple *dest, struct triple *rval)
7395 internal_error(state, 0, "missing rval");
7397 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
7398 rval = read_expr(state, rval);
7399 def = write_expr(state, dest, rval);
7402 /* Fill in the array size if necessary */
7403 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
7404 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
7405 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
7406 dest->type->elements = rval->type->elements;
7409 if (!equiv_types(dest->type, rval->type)) {
7410 error(state, 0, "Incompatible types in inializer");
7412 MISC(dest, 0) = rval;
7413 insert_triple(state, dest, rval);
7414 rval->id |= TRIPLE_FLAG_FLATTENED;
7415 use_triple(MISC(dest, 0), dest);
7420 struct type *arithmetic_result(
7421 struct compile_state *state, struct triple *left, struct triple *right)
7424 /* Sanity checks to ensure I am working with arithmetic types */
7425 arithmetic(state, left);
7426 arithmetic(state, right);
7428 do_arithmetic_conversion(
7429 get_basic_type(left->type),
7430 get_basic_type(right->type)),
7435 struct type *ptr_arithmetic_result(
7436 struct compile_state *state, struct triple *left, struct triple *right)
7439 /* Sanity checks to ensure I am working with the proper types */
7440 ptr_arithmetic(state, left);
7441 arithmetic(state, right);
7442 if (TYPE_ARITHMETIC(left->type->type) &&
7443 TYPE_ARITHMETIC(right->type->type)) {
7444 type = arithmetic_result(state, left, right);
7446 else if (TYPE_PTR(left->type->type)) {
7450 internal_error(state, 0, "huh?");
7456 /* boolean helper function */
7458 static struct triple *ltrue_expr(struct compile_state *state,
7459 struct triple *expr)
7462 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
7463 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
7464 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
7465 /* If the expression is already boolean do nothing */
7468 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
7474 static struct triple *lfalse_expr(struct compile_state *state,
7475 struct triple *expr)
7477 return triple(state, OP_LFALSE, &int_type, expr, 0);
7480 static struct triple *mkland_expr(
7481 struct compile_state *state,
7482 struct triple *left, struct triple *right)
7484 struct triple *def, *val, *var, *jmp, *mid, *end;
7485 struct triple *lstore, *rstore;
7487 /* Generate some intermediate triples */
7489 var = variable(state, &int_type);
7491 /* Store the left hand side value */
7492 lstore = write_expr(state, var, left);
7494 /* Jump if the value is false */
7495 jmp = branch(state, end,
7496 lfalse_expr(state, read_expr(state, var)));
7499 /* Store the right hand side value */
7500 rstore = write_expr(state, var, right);
7502 /* An expression for the computed value */
7503 val = read_expr(state, var);
7505 /* Generate the prog for a logical and */
7506 def = mkprog(state, var, lstore, jmp, mid, rstore, end, val, 0);
7511 static struct triple *mklor_expr(
7512 struct compile_state *state,
7513 struct triple *left, struct triple *right)
7515 struct triple *def, *val, *var, *jmp, *mid, *end;
7517 /* Generate some intermediate triples */
7519 var = variable(state, &int_type);
7521 /* Store the left hand side value */
7522 left = write_expr(state, var, left);
7524 /* Jump if the value is true */
7525 jmp = branch(state, end, read_expr(state, var));
7528 /* Store the right hand side value */
7529 right = write_expr(state, var, right);
7531 /* An expression for the computed value*/
7532 val = read_expr(state, var);
7534 /* Generate the prog for a logical or */
7535 def = mkprog(state, var, left, jmp, mid, right, end, val, 0);
7540 static struct triple *mkcond_expr(
7541 struct compile_state *state,
7542 struct triple *test, struct triple *left, struct triple *right)
7544 struct triple *def, *val, *var, *jmp1, *jmp2, *top, *mid, *end;
7545 struct type *result_type;
7546 unsigned int left_type, right_type;
7548 left_type = left->type->type;
7549 right_type = right->type->type;
7551 /* Both operands have arithmetic type */
7552 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
7553 result_type = arithmetic_result(state, left, right);
7555 /* Both operands have void type */
7556 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
7557 ((right_type & TYPE_MASK) == TYPE_VOID)) {
7558 result_type = &void_type;
7560 /* pointers to the same type... */
7561 else if ((result_type = compatible_ptrs(left->type, right->type))) {
7564 /* Both operands are pointers and left is a pointer to void */
7565 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7566 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7567 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7568 result_type = right->type;
7570 /* Both operands are pointers and right is a pointer to void */
7571 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7572 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7573 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7574 result_type = left->type;
7577 error(state, 0, "Incompatible types in conditional expression");
7579 /* Generate some intermediate triples */
7582 var = variable(state, result_type);
7584 /* Branch if the test is false */
7585 jmp1 = branch(state, mid, lfalse_expr(state, read_expr(state, test)));
7588 /* Store the left hand side value */
7589 left = write_expr(state, var, left);
7591 /* Branch to the end */
7592 jmp2 = branch(state, end, 0);
7594 /* Store the right hand side value */
7595 right = write_expr(state, var, right);
7597 /* An expression for the computed value */
7598 val = read_expr(state, var);
7600 /* Generate the prog for a conditional expression */
7601 def = mkprog(state, var, jmp1, top, left, jmp2, mid, right, end, val, 0);
7607 static int expr_depth(struct compile_state *state, struct triple *ins)
7609 #warning "FIXME move optimal ordering of subexpressions into the optimizer"
7612 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
7615 else if (ins->op == OP_DEREF) {
7616 count = expr_depth(state, RHS(ins, 0)) - 1;
7618 else if (ins->op == OP_VAL) {
7619 count = expr_depth(state, RHS(ins, 0)) - 1;
7621 else if (ins->op == OP_FCALL) {
7622 /* Don't figure the depth of a call just guess it is huge */
7626 struct triple **expr;
7627 expr = triple_rhs(state, ins, 0);
7628 for(;expr; expr = triple_rhs(state, ins, expr)) {
7631 depth = expr_depth(state, *expr);
7632 if (depth > count) {
7641 static struct triple *flatten_generic(
7642 struct compile_state *state, struct triple *first, struct triple *ptr,
7647 struct triple **ins;
7650 /* Only operations with just a rhs and a lhs should come here */
7653 if (TRIPLE_SIZE(ptr) != lhs + rhs + ignored) {
7654 internal_error(state, ptr, "unexpected args for: %d %s",
7655 ptr->op, tops(ptr->op));
7657 /* Find the depth of the rhs elements */
7658 for(i = 0; i < rhs; i++) {
7659 vector[i].ins = &RHS(ptr, i);
7660 vector[i].depth = expr_depth(state, *vector[i].ins);
7662 /* Selection sort the rhs */
7663 for(i = 0; i < rhs; i++) {
7665 for(j = i + 1; j < rhs; j++ ) {
7666 if (vector[j].depth > vector[max].depth) {
7671 struct rhs_vector tmp;
7673 vector[i] = vector[max];
7677 /* Now flatten the rhs elements */
7678 for(i = 0; i < rhs; i++) {
7679 *vector[i].ins = flatten(state, first, *vector[i].ins);
7680 use_triple(*vector[i].ins, ptr);
7683 insert_triple(state, first, ptr);
7684 ptr->id |= TRIPLE_FLAG_FLATTENED;
7685 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7687 /* Now flatten the lhs elements */
7688 for(i = 0; i < lhs; i++) {
7689 struct triple **ins = &LHS(ptr, i);
7690 *ins = flatten(state, first, *ins);
7691 use_triple(*ins, ptr);
7697 static struct triple *flatten_prog(
7698 struct compile_state *state, struct triple *first, struct triple *ptr)
7700 struct triple *head, *body, *val;
7705 release_triple(state, head);
7706 release_triple(state, ptr);
7708 body->prev = first->prev;
7709 body->prev->next = body;
7710 val->next->prev = val;
7712 if (triple_is_cbranch(state, body->prev) ||
7713 triple_is_call(state, body->prev)) {
7714 unuse_triple(first, body->prev);
7715 use_triple(body, body->prev);
7718 if (!(val->id & TRIPLE_FLAG_FLATTENED)) {
7719 internal_error(state, val, "val not flattened?");
7726 static struct triple *flatten_part(
7727 struct compile_state *state, struct triple *first, struct triple *ptr)
7729 if (!triple_is_part(state, ptr)) {
7730 internal_error(state, ptr, "not a part");
7732 if (ptr->rhs || ptr->lhs || ptr->targ || (ptr->misc != 1)) {
7733 internal_error(state, ptr, "unexpected args for: %d %s",
7734 ptr->op, tops(ptr->op));
7736 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7737 use_triple(MISC(ptr, 0), ptr);
7738 return flatten_generic(state, first, ptr, 1);
7741 static struct triple *flatten(
7742 struct compile_state *state, struct triple *first, struct triple *ptr)
7744 struct triple *orig_ptr;
7749 /* Only flatten triples once */
7750 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
7755 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7756 return MISC(ptr, 0);
7759 ptr = flatten_prog(state, first, ptr);
7762 ptr = flatten_generic(state, first, ptr, 1);
7763 insert_triple(state, first, ptr);
7764 ptr->id |= TRIPLE_FLAG_FLATTENED;
7765 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7766 if (ptr->next != ptr) {
7767 use_triple(ptr->next, ptr);
7772 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7773 use_triple(RHS(ptr, 0), ptr);
7776 ptr = flatten_generic(state, first, ptr, 1);
7777 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7778 use_triple(MISC(ptr, 0), ptr);
7781 use_triple(TARG(ptr, 0), ptr);
7784 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7785 use_triple(RHS(ptr, 0), ptr);
7786 use_triple(TARG(ptr, 0), ptr);
7787 insert_triple(state, first, ptr);
7788 ptr->id |= TRIPLE_FLAG_FLATTENED;
7789 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7790 if (ptr->next != ptr) {
7791 use_triple(ptr->next, ptr);
7795 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7796 use_triple(MISC(ptr, 0), ptr);
7797 use_triple(TARG(ptr, 0), ptr);
7798 insert_triple(state, first, ptr);
7799 ptr->id |= TRIPLE_FLAG_FLATTENED;
7800 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7801 if (ptr->next != ptr) {
7802 use_triple(ptr->next, ptr);
7806 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7807 use_triple(RHS(ptr, 0), ptr);
7810 insert_triple(state, state->global_pool, ptr);
7811 ptr->id |= TRIPLE_FLAG_FLATTENED;
7812 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7813 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
7814 use_triple(MISC(ptr, 0), ptr);
7817 /* Since OP_DEREF is just a marker delete it when I flatten it */
7819 RHS(orig_ptr, 0) = 0;
7820 free_triple(state, orig_ptr);
7823 if (RHS(ptr, 0)->op == OP_DEREF) {
7824 struct triple *base, *left;
7826 base = MISC(ptr, 0);
7827 offset = bits_to_bytes(field_offset(state, base->type, ptr->u.field));
7828 left = RHS(base, 0);
7829 ptr = triple(state, OP_ADD, left->type,
7830 read_expr(state, left),
7831 int_const(state, &ulong_type, offset));
7832 free_triple(state, base);
7835 ptr = flatten_part(state, first, ptr);
7839 if (RHS(ptr, 0)->op == OP_DEREF) {
7840 struct triple *base, *left;
7842 base = MISC(ptr, 0);
7843 offset = bits_to_bytes(index_offset(state, base->type, ptr->u.cval));
7844 left = RHS(base, 0);
7845 ptr = triple(state, OP_ADD, left->type,
7846 read_expr(state, left),
7847 int_const(state, &long_type, offset));
7848 free_triple(state, base);
7851 ptr = flatten_part(state, first, ptr);
7855 ptr = flatten_part(state, first, ptr);
7856 use_triple(ptr, MISC(ptr, 0));
7859 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7860 use_triple(MISC(ptr, 0), ptr);
7863 first = state->global_pool;
7864 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7865 use_triple(MISC(ptr, 0), ptr);
7866 insert_triple(state, first, ptr);
7867 ptr->id |= TRIPLE_FLAG_FLATTENED;
7868 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7871 ptr = flatten_generic(state, first, ptr, 0);
7874 /* Flatten the easy cases we don't override */
7875 ptr = flatten_generic(state, first, ptr, 0);
7878 } while(ptr && (ptr != orig_ptr));
7879 if (ptr && !(ptr->id & TRIPLE_FLAG_FLATTENED)) {
7880 insert_triple(state, first, ptr);
7881 ptr->id |= TRIPLE_FLAG_FLATTENED;
7882 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7887 static void release_expr(struct compile_state *state, struct triple *expr)
7889 struct triple *head;
7890 head = label(state);
7891 flatten(state, head, expr);
7892 while(head->next != head) {
7893 release_triple(state, head->next);
7895 free_triple(state, head);
7898 static int replace_rhs_use(struct compile_state *state,
7899 struct triple *orig, struct triple *new, struct triple *use)
7901 struct triple **expr;
7904 expr = triple_rhs(state, use, 0);
7905 for(;expr; expr = triple_rhs(state, use, expr)) {
7906 if (*expr == orig) {
7912 unuse_triple(orig, use);
7913 use_triple(new, use);
7918 static int replace_lhs_use(struct compile_state *state,
7919 struct triple *orig, struct triple *new, struct triple *use)
7921 struct triple **expr;
7924 expr = triple_lhs(state, use, 0);
7925 for(;expr; expr = triple_lhs(state, use, expr)) {
7926 if (*expr == orig) {
7932 unuse_triple(orig, use);
7933 use_triple(new, use);
7938 static int replace_misc_use(struct compile_state *state,
7939 struct triple *orig, struct triple *new, struct triple *use)
7941 struct triple **expr;
7944 expr = triple_misc(state, use, 0);
7945 for(;expr; expr = triple_misc(state, use, expr)) {
7946 if (*expr == orig) {
7952 unuse_triple(orig, use);
7953 use_triple(new, use);
7958 static int replace_targ_use(struct compile_state *state,
7959 struct triple *orig, struct triple *new, struct triple *use)
7961 struct triple **expr;
7964 expr = triple_targ(state, use, 0);
7965 for(;expr; expr = triple_targ(state, use, expr)) {
7966 if (*expr == orig) {
7972 unuse_triple(orig, use);
7973 use_triple(new, use);
7978 static void replace_use(struct compile_state *state,
7979 struct triple *orig, struct triple *new, struct triple *use)
7983 found |= replace_rhs_use(state, orig, new, use);
7984 found |= replace_lhs_use(state, orig, new, use);
7985 found |= replace_misc_use(state, orig, new, use);
7986 found |= replace_targ_use(state, orig, new, use);
7988 internal_error(state, use, "use without use");
7992 static void propogate_use(struct compile_state *state,
7993 struct triple *orig, struct triple *new)
7995 struct triple_set *user, *next;
7996 for(user = orig->use; user; user = next) {
7997 /* Careful replace_use modifies the use chain and
7998 * removes use. So we must get a copy of the next
8002 replace_use(state, orig, new, user->member);
8005 internal_error(state, orig, "used after propogate_use");
8011 * ===========================
8014 static struct triple *mk_cast_expr(
8015 struct compile_state *state, struct type *type, struct triple *expr)
8018 def = read_expr(state, expr);
8019 def = triple(state, OP_CONVERT, type, def, 0);
8023 static struct triple *mk_add_expr(
8024 struct compile_state *state, struct triple *left, struct triple *right)
8026 struct type *result_type;
8027 /* Put pointer operands on the left */
8028 if (is_pointer(right)) {
8034 left = read_expr(state, left);
8035 right = read_expr(state, right);
8036 result_type = ptr_arithmetic_result(state, left, right);
8037 if (is_pointer(left)) {
8038 struct type *ptr_math;
8040 if (is_signed(right->type)) {
8041 ptr_math = &long_type;
8044 ptr_math = &ulong_type;
8047 if (!equiv_types(right->type, ptr_math)) {
8048 right = mk_cast_expr(state, ptr_math, right);
8050 right = triple(state, op, ptr_math, right,
8051 int_const(state, ptr_math,
8052 size_of_in_bytes(state, left->type->left)));
8054 return triple(state, OP_ADD, result_type, left, right);
8057 static struct triple *mk_sub_expr(
8058 struct compile_state *state, struct triple *left, struct triple *right)
8060 struct type *result_type;
8061 result_type = ptr_arithmetic_result(state, left, right);
8062 left = read_expr(state, left);
8063 right = read_expr(state, right);
8064 if (is_pointer(left)) {
8065 struct type *ptr_math;
8067 if (is_signed(right->type)) {
8068 ptr_math = &long_type;
8071 ptr_math = &ulong_type;
8074 if (!equiv_types(right->type, ptr_math)) {
8075 right = mk_cast_expr(state, ptr_math, right);
8077 right = triple(state, op, ptr_math, right,
8078 int_const(state, ptr_math,
8079 size_of_in_bytes(state, left->type->left)));
8081 return triple(state, OP_SUB, result_type, left, right);
8084 static struct triple *mk_pre_inc_expr(
8085 struct compile_state *state, struct triple *def)
8089 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
8090 return triple(state, OP_VAL, def->type,
8091 write_expr(state, def, val),
8095 static struct triple *mk_pre_dec_expr(
8096 struct compile_state *state, struct triple *def)
8100 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
8101 return triple(state, OP_VAL, def->type,
8102 write_expr(state, def, val),
8106 static struct triple *mk_post_inc_expr(
8107 struct compile_state *state, struct triple *def)
8111 val = read_expr(state, def);
8112 return triple(state, OP_VAL, def->type,
8113 write_expr(state, def,
8114 mk_add_expr(state, val, int_const(state, &int_type, 1)))
8118 static struct triple *mk_post_dec_expr(
8119 struct compile_state *state, struct triple *def)
8123 val = read_expr(state, def);
8124 return triple(state, OP_VAL, def->type,
8125 write_expr(state, def,
8126 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
8130 static struct triple *mk_subscript_expr(
8131 struct compile_state *state, struct triple *left, struct triple *right)
8133 left = read_expr(state, left);
8134 right = read_expr(state, right);
8135 if (!is_pointer(left) && !is_pointer(right)) {
8136 error(state, left, "subscripted value is not a pointer");
8138 return mk_deref_expr(state, mk_add_expr(state, left, right));
8143 * Compile time evaluation
8144 * ===========================
8146 static int is_const(struct triple *ins)
8148 return IS_CONST_OP(ins->op);
8151 static int is_simple_const(struct triple *ins)
8153 /* Is this a constant that u.cval has the value.
8154 * Or equivalently is this a constant that read_const
8156 * So far only OP_INTCONST qualifies.
8158 return (ins->op == OP_INTCONST);
8161 static int constants_equal(struct compile_state *state,
8162 struct triple *left, struct triple *right)
8165 if ((left->op == OP_UNKNOWNVAL) || (right->op == OP_UNKNOWNVAL)) {
8168 else if (!is_const(left) || !is_const(right)) {
8171 else if (left->op != right->op) {
8174 else if (!equiv_types(left->type, right->type)) {
8181 if (left->u.cval == right->u.cval) {
8187 size_t lsize, rsize, bytes;
8188 lsize = size_of(state, left->type);
8189 rsize = size_of(state, right->type);
8190 if (lsize != rsize) {
8193 bytes = bits_to_bytes(lsize);
8194 if (memcmp(left->u.blob, right->u.blob, bytes) == 0) {
8200 if ((MISC(left, 0) == MISC(right, 0)) &&
8201 (left->u.cval == right->u.cval)) {
8206 internal_error(state, left, "uknown constant type");
8213 static int is_zero(struct triple *ins)
8215 return is_simple_const(ins) && (ins->u.cval == 0);
8218 static int is_one(struct triple *ins)
8220 return is_simple_const(ins) && (ins->u.cval == 1);
8223 static long_t bit_count(ulong_t value)
8228 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8239 static long_t bsr(ulong_t value)
8242 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8253 static long_t bsf(ulong_t value)
8256 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
8267 static long_t log2(ulong_t value)
8272 static long_t tlog2(struct triple *ins)
8274 return log2(ins->u.cval);
8277 static int is_pow2(struct triple *ins)
8279 ulong_t value, mask;
8281 if (!is_const(ins)) {
8284 value = ins->u.cval;
8291 return ((value & mask) == value);
8294 static ulong_t read_const(struct compile_state *state,
8295 struct triple *ins, struct triple *rhs)
8297 switch(rhs->type->type &TYPE_MASK) {
8310 fprintf(state->errout, "type: ");
8311 name_of(state->errout, rhs->type);
8312 fprintf(state->errout, "\n");
8313 internal_warning(state, rhs, "bad type to read_const");
8316 if (!is_simple_const(rhs)) {
8317 internal_error(state, rhs, "bad op to read_const");
8322 static long_t read_sconst(struct compile_state *state,
8323 struct triple *ins, struct triple *rhs)
8325 return (long_t)(rhs->u.cval);
8328 int const_ltrue(struct compile_state *state, struct triple *ins, struct triple *rhs)
8330 if (!is_const(rhs)) {
8331 internal_error(state, 0, "non const passed to const_true");
8333 return !is_zero(rhs);
8336 int const_eq(struct compile_state *state, struct triple *ins,
8337 struct triple *left, struct triple *right)
8340 if (!is_const(left) || !is_const(right)) {
8341 internal_warning(state, ins, "non const passed to const_eq");
8344 else if (left == right) {
8347 else if (is_simple_const(left) && is_simple_const(right)) {
8349 lval = read_const(state, ins, left);
8350 rval = read_const(state, ins, right);
8351 result = (lval == rval);
8353 else if ((left->op == OP_ADDRCONST) &&
8354 (right->op == OP_ADDRCONST)) {
8355 result = (MISC(left, 0) == MISC(right, 0)) &&
8356 (left->u.cval == right->u.cval);
8359 internal_warning(state, ins, "incomparable constants passed to const_eq");
8366 int const_ucmp(struct compile_state *state, struct triple *ins,
8367 struct triple *left, struct triple *right)
8370 if (!is_const(left) || !is_const(right)) {
8371 internal_warning(state, ins, "non const past to const_ucmp");
8374 else if (left == right) {
8377 else if (is_simple_const(left) && is_simple_const(right)) {
8379 lval = read_const(state, ins, left);
8380 rval = read_const(state, ins, right);
8384 } else if (rval > lval) {
8388 else if ((left->op == OP_ADDRCONST) &&
8389 (right->op == OP_ADDRCONST) &&
8390 (MISC(left, 0) == MISC(right, 0))) {
8392 if (left->u.cval > right->u.cval) {
8394 } else if (left->u.cval < right->u.cval) {
8399 internal_warning(state, ins, "incomparable constants passed to const_ucmp");
8405 int const_scmp(struct compile_state *state, struct triple *ins,
8406 struct triple *left, struct triple *right)
8409 if (!is_const(left) || !is_const(right)) {
8410 internal_warning(state, ins, "non const past to ucmp_const");
8413 else if (left == right) {
8416 else if (is_simple_const(left) && is_simple_const(right)) {
8418 lval = read_sconst(state, ins, left);
8419 rval = read_sconst(state, ins, right);
8423 } else if (rval > lval) {
8428 internal_warning(state, ins, "incomparable constants passed to const_scmp");
8434 static void unuse_rhs(struct compile_state *state, struct triple *ins)
8436 struct triple **expr;
8437 expr = triple_rhs(state, ins, 0);
8438 for(;expr;expr = triple_rhs(state, ins, expr)) {
8440 unuse_triple(*expr, ins);
8446 static void unuse_lhs(struct compile_state *state, struct triple *ins)
8448 struct triple **expr;
8449 expr = triple_lhs(state, ins, 0);
8450 for(;expr;expr = triple_lhs(state, ins, expr)) {
8451 unuse_triple(*expr, ins);
8456 static void unuse_misc(struct compile_state *state, struct triple *ins)
8458 struct triple **expr;
8459 expr = triple_misc(state, ins, 0);
8460 for(;expr;expr = triple_misc(state, ins, expr)) {
8461 unuse_triple(*expr, ins);
8466 static void unuse_targ(struct compile_state *state, struct triple *ins)
8469 struct triple **slot;
8470 slot = &TARG(ins, 0);
8471 for(i = 0; i < ins->targ; i++) {
8472 unuse_triple(slot[i], ins);
8477 static void check_lhs(struct compile_state *state, struct triple *ins)
8479 struct triple **expr;
8480 expr = triple_lhs(state, ins, 0);
8481 for(;expr;expr = triple_lhs(state, ins, expr)) {
8482 internal_error(state, ins, "unexpected lhs");
8487 static void check_misc(struct compile_state *state, struct triple *ins)
8489 struct triple **expr;
8490 expr = triple_misc(state, ins, 0);
8491 for(;expr;expr = triple_misc(state, ins, expr)) {
8493 internal_error(state, ins, "unexpected misc");
8498 static void check_targ(struct compile_state *state, struct triple *ins)
8500 struct triple **expr;
8501 expr = triple_targ(state, ins, 0);
8502 for(;expr;expr = triple_targ(state, ins, expr)) {
8503 internal_error(state, ins, "unexpected targ");
8507 static void wipe_ins(struct compile_state *state, struct triple *ins)
8509 /* Becareful which instructions you replace the wiped
8510 * instruction with, as there are not enough slots
8511 * in all instructions to hold all others.
8513 check_targ(state, ins);
8514 check_misc(state, ins);
8515 unuse_rhs(state, ins);
8516 unuse_lhs(state, ins);
8523 static void wipe_branch(struct compile_state *state, struct triple *ins)
8525 /* Becareful which instructions you replace the wiped
8526 * instruction with, as there are not enough slots
8527 * in all instructions to hold all others.
8529 unuse_rhs(state, ins);
8530 unuse_lhs(state, ins);
8531 unuse_misc(state, ins);
8532 unuse_targ(state, ins);
8539 static void mkcopy(struct compile_state *state,
8540 struct triple *ins, struct triple *rhs)
8542 struct block *block;
8543 if (!equiv_types(ins->type, rhs->type)) {
8544 FILE *fp = state->errout;
8545 fprintf(fp, "src type: ");
8546 name_of(fp, rhs->type);
8547 fprintf(fp, "\ndst type: ");
8548 name_of(fp, ins->type);
8550 internal_error(state, ins, "mkcopy type mismatch");
8552 block = block_of_triple(state, ins);
8553 wipe_ins(state, ins);
8556 ins->u.block = block;
8558 use_triple(RHS(ins, 0), ins);
8561 static void mkconst(struct compile_state *state,
8562 struct triple *ins, ulong_t value)
8564 if (!is_integral(ins) && !is_pointer(ins)) {
8565 fprintf(state->errout, "type: ");
8566 name_of(state->errout, ins->type);
8567 fprintf(state->errout, "\n");
8568 internal_error(state, ins, "unknown type to make constant value: %ld",
8571 wipe_ins(state, ins);
8572 ins->op = OP_INTCONST;
8573 ins->u.cval = value;
8576 static void mkaddr_const(struct compile_state *state,
8577 struct triple *ins, struct triple *sdecl, ulong_t value)
8579 if ((sdecl->op != OP_SDECL) && (sdecl->op != OP_LABEL)) {
8580 internal_error(state, ins, "bad base for addrconst");
8582 wipe_ins(state, ins);
8583 ins->op = OP_ADDRCONST;
8585 MISC(ins, 0) = sdecl;
8586 ins->u.cval = value;
8587 use_triple(sdecl, ins);
8590 #if DEBUG_DECOMPOSE_PRINT_TUPLES
8591 static void print_tuple(struct compile_state *state,
8592 struct triple *ins, struct triple *tuple)
8594 FILE *fp = state->dbgout;
8595 fprintf(fp, "%5s %p tuple: %p ", tops(ins->op), ins, tuple);
8596 name_of(fp, tuple->type);
8597 if (tuple->lhs > 0) {
8598 fprintf(fp, " lhs: ");
8599 name_of(fp, LHS(tuple, 0)->type);
8606 static struct triple *decompose_with_tuple(struct compile_state *state,
8607 struct triple *ins, struct triple *tuple)
8609 struct triple *next;
8611 flatten(state, next, tuple);
8612 #if DEBUG_DECOMPOSE_PRINT_TUPLES
8613 print_tuple(state, ins, tuple);
8616 if (!is_compound_type(tuple->type) && (tuple->lhs > 0)) {
8618 if (tuple->lhs != 1) {
8619 internal_error(state, tuple, "plain type in multiple registers?");
8621 tmp = LHS(tuple, 0);
8622 release_triple(state, tuple);
8626 propogate_use(state, ins, tuple);
8627 release_triple(state, ins);
8632 static struct triple *decompose_unknownval(struct compile_state *state,
8635 struct triple *tuple;
8638 #if DEBUG_DECOMPOSE_HIRES
8639 FILE *fp = state->dbgout;
8640 fprintf(fp, "unknown type: ");
8641 name_of(fp, ins->type);
8645 get_occurance(ins->occurance);
8646 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8649 for(i = 0; i < tuple->lhs; i++) {
8650 struct type *piece_type;
8651 struct triple *unknown;
8653 piece_type = reg_type(state, ins->type, i * REG_SIZEOF_REG);
8654 get_occurance(tuple->occurance);
8655 unknown = alloc_triple(state, OP_UNKNOWNVAL, piece_type, 0, 0,
8657 LHS(tuple, i) = unknown;
8659 return decompose_with_tuple(state, ins, tuple);
8663 static struct triple *decompose_read(struct compile_state *state,
8666 struct triple *tuple, *lval;
8671 if (lval->op == OP_PIECE) {
8674 get_occurance(ins->occurance);
8675 tuple = alloc_triple(state, OP_TUPLE, lval->type, -1, -1,
8678 if ((tuple->lhs != lval->lhs) &&
8679 (!triple_is_def(state, lval) || (tuple->lhs != 1)))
8681 internal_error(state, ins, "lhs size inconsistency?");
8683 for(i = 0; i < tuple->lhs; i++) {
8684 struct triple *piece, *read, *bitref;
8685 if ((i != 0) || !triple_is_def(state, lval)) {
8686 piece = LHS(lval, i);
8691 /* See if the piece is really a bitref */
8693 if (piece->op == OP_BITREF) {
8695 piece = RHS(bitref, 0);
8698 get_occurance(tuple->occurance);
8699 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8701 RHS(read, 0) = piece;
8704 struct triple *extract;
8706 if (is_signed(bitref->type->left)) {
8711 get_occurance(tuple->occurance);
8712 extract = alloc_triple(state, op, bitref->type, -1, -1,
8714 RHS(extract, 0) = read;
8715 extract->u.bitfield.size = bitref->u.bitfield.size;
8716 extract->u.bitfield.offset = bitref->u.bitfield.offset;
8721 LHS(tuple, i) = read;
8723 return decompose_with_tuple(state, ins, tuple);
8726 static struct triple *decompose_write(struct compile_state *state,
8729 struct triple *tuple, *lval, *val;
8732 lval = MISC(ins, 0);
8734 get_occurance(ins->occurance);
8735 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8738 if ((tuple->lhs != lval->lhs) &&
8739 (!triple_is_def(state, lval) || tuple->lhs != 1))
8741 internal_error(state, ins, "lhs size inconsistency?");
8743 for(i = 0; i < tuple->lhs; i++) {
8744 struct triple *piece, *write, *pval, *bitref;
8745 if ((i != 0) || !triple_is_def(state, lval)) {
8746 piece = LHS(lval, i);
8750 if ((i == 0) && (tuple->lhs == 1) && (val->lhs == 0)) {
8755 internal_error(state, ins, "lhs size inconsistency?");
8760 /* See if the piece is really a bitref */
8762 if (piece->op == OP_BITREF) {
8763 struct triple *read, *deposit;
8765 piece = RHS(bitref, 0);
8767 /* Read the destination register */
8768 get_occurance(tuple->occurance);
8769 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8771 RHS(read, 0) = piece;
8773 /* Deposit the new bitfield value */
8774 get_occurance(tuple->occurance);
8775 deposit = alloc_triple(state, OP_DEPOSIT, piece->type, -1, -1,
8777 RHS(deposit, 0) = read;
8778 RHS(deposit, 1) = pval;
8779 deposit->u.bitfield.size = bitref->u.bitfield.size;
8780 deposit->u.bitfield.offset = bitref->u.bitfield.offset;
8782 /* Now write the newly generated value */
8786 get_occurance(tuple->occurance);
8787 write = alloc_triple(state, OP_WRITE, piece->type, -1, -1,
8789 MISC(write, 0) = piece;
8790 RHS(write, 0) = pval;
8791 LHS(tuple, i) = write;
8793 return decompose_with_tuple(state, ins, tuple);
8796 struct decompose_load_info {
8797 struct occurance *occurance;
8798 struct triple *lval;
8799 struct triple *tuple;
8801 static void decompose_load_cb(struct compile_state *state,
8802 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
8804 struct decompose_load_info *info = arg;
8805 struct triple *load;
8807 if (reg_offset > info->tuple->lhs) {
8808 internal_error(state, info->tuple, "lhs to small?");
8810 get_occurance(info->occurance);
8811 load = alloc_triple(state, OP_LOAD, type, -1, -1, info->occurance);
8812 RHS(load, 0) = mk_addr_expr(state, info->lval, mem_offset);
8813 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = load;
8816 static struct triple *decompose_load(struct compile_state *state,
8819 struct triple *tuple;
8820 struct decompose_load_info info;
8822 if (!is_compound_type(ins->type)) {
8825 get_occurance(ins->occurance);
8826 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8829 info.occurance = ins->occurance;
8830 info.lval = RHS(ins, 0);
8832 walk_type_fields(state, ins->type, 0, 0, decompose_load_cb, &info);
8834 return decompose_with_tuple(state, ins, tuple);
8838 struct decompose_store_info {
8839 struct occurance *occurance;
8840 struct triple *lval;
8842 struct triple *tuple;
8844 static void decompose_store_cb(struct compile_state *state,
8845 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
8847 struct decompose_store_info *info = arg;
8848 struct triple *store;
8850 if (reg_offset > info->tuple->lhs) {
8851 internal_error(state, info->tuple, "lhs to small?");
8853 get_occurance(info->occurance);
8854 store = alloc_triple(state, OP_STORE, type, -1, -1, info->occurance);
8855 RHS(store, 0) = mk_addr_expr(state, info->lval, mem_offset);
8856 RHS(store, 1) = LHS(info->val, reg_offset);
8857 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = store;
8860 static struct triple *decompose_store(struct compile_state *state,
8863 struct triple *tuple;
8864 struct decompose_store_info info;
8866 if (!is_compound_type(ins->type)) {
8869 get_occurance(ins->occurance);
8870 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8873 info.occurance = ins->occurance;
8874 info.lval = RHS(ins, 0);
8875 info.val = RHS(ins, 1);
8877 walk_type_fields(state, ins->type, 0, 0, decompose_store_cb, &info);
8879 return decompose_with_tuple(state, ins, tuple);
8882 static struct triple *decompose_dot(struct compile_state *state,
8885 struct triple *tuple, *lval;
8890 lval = MISC(ins, 0);
8891 reg_offset = field_reg_offset(state, lval->type, ins->u.field);
8892 idx = reg_offset/REG_SIZEOF_REG;
8893 type = field_type(state, lval->type, ins->u.field);
8894 #if DEBUG_DECOMPOSE_HIRES
8896 FILE *fp = state->dbgout;
8897 fprintf(fp, "field type: ");
8903 get_occurance(ins->occurance);
8904 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
8907 if (((ins->type->type & TYPE_MASK) == TYPE_BITFIELD) &&
8910 internal_error(state, ins, "multi register bitfield?");
8913 for(i = 0; i < tuple->lhs; i++, idx++) {
8914 struct triple *piece;
8915 if (!triple_is_def(state, lval)) {
8916 if (idx > lval->lhs) {
8917 internal_error(state, ins, "inconsistent lhs count");
8919 piece = LHS(lval, idx);
8922 internal_error(state, ins, "bad reg_offset into def");
8925 internal_error(state, ins, "bad reg count from def");
8930 /* Remember the offset of the bitfield */
8931 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
8932 get_occurance(ins->occurance);
8933 piece = build_triple(state, OP_BITREF, type, piece, 0,
8935 piece->u.bitfield.size = size_of(state, type);
8936 piece->u.bitfield.offset = reg_offset % REG_SIZEOF_REG;
8938 else if ((reg_offset % REG_SIZEOF_REG) != 0) {
8939 internal_error(state, ins,
8940 "request for a nonbitfield sub register?");
8943 LHS(tuple, i) = piece;
8946 return decompose_with_tuple(state, ins, tuple);
8949 static struct triple *decompose_index(struct compile_state *state,
8952 struct triple *tuple, *lval;
8956 lval = MISC(ins, 0);
8957 idx = index_reg_offset(state, lval->type, ins->u.cval)/REG_SIZEOF_REG;
8958 type = index_type(state, lval->type, ins->u.cval);
8959 #if DEBUG_DECOMPOSE_HIRES
8961 FILE *fp = state->dbgout;
8962 fprintf(fp, "index type: ");
8968 get_occurance(ins->occurance);
8969 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
8972 for(i = 0; i < tuple->lhs; i++, idx++) {
8973 struct triple *piece;
8974 if (!triple_is_def(state, lval)) {
8975 if (idx > lval->lhs) {
8976 internal_error(state, ins, "inconsistent lhs count");
8978 piece = LHS(lval, idx);
8981 internal_error(state, ins, "bad reg_offset into def");
8984 internal_error(state, ins, "bad reg count from def");
8988 LHS(tuple, i) = piece;
8991 return decompose_with_tuple(state, ins, tuple);
8994 static void decompose_compound_types(struct compile_state *state)
8996 struct triple *ins, *next, *first;
8999 first = state->first;
9002 /* Pass one expand compound values into pseudo registers.
9010 next = decompose_unknownval(state, ins);
9014 next = decompose_read(state, ins);
9018 next = decompose_write(state, ins);
9022 /* Be very careful with the load/store logic. These
9023 * operations must convert from the in register layout
9024 * to the in memory layout, which is nontrivial.
9027 next = decompose_load(state, ins);
9030 next = decompose_store(state, ins);
9034 next = decompose_dot(state, ins);
9037 next = decompose_index(state, ins);
9041 #if DEBUG_DECOMPOSE_HIRES
9042 fprintf(fp, "decompose next: %p \n", next);
9044 fprintf(fp, "next->op: %d %s\n",
9045 next->op, tops(next->op));
9046 /* High resolution debugging mode */
9047 print_triples(state);
9049 } while (next != first);
9051 /* Pass two remove the tuples.
9056 if (ins->op == OP_TUPLE) {
9058 internal_error(state, ins, "tuple used");
9061 release_triple(state, ins);
9065 } while(ins != first);
9069 if (ins->op == OP_BITREF) {
9071 internal_error(state, ins, "bitref used");
9074 release_triple(state, ins);
9078 } while(ins != first);
9080 /* Pass three verify the state and set ->id to 0.
9086 ins->id &= ~TRIPLE_FLAG_FLATTENED;
9087 if (triple_stores_block(state, ins)) {
9090 if (triple_is_def(state, ins)) {
9091 if (reg_size_of(state, ins->type) > REG_SIZEOF_REG) {
9092 internal_error(state, ins, "multi register value remains?");
9095 if (ins->op == OP_DOT) {
9096 internal_error(state, ins, "OP_DOT remains?");
9098 if (ins->op == OP_INDEX) {
9099 internal_error(state, ins, "OP_INDEX remains?");
9101 if (ins->op == OP_BITREF) {
9102 internal_error(state, ins, "OP_BITREF remains?");
9104 if (ins->op == OP_TUPLE) {
9105 internal_error(state, ins, "OP_TUPLE remains?");
9107 } while(next != first);
9110 /* For those operations that cannot be simplified */
9111 static void simplify_noop(struct compile_state *state, struct triple *ins)
9116 static void simplify_smul(struct compile_state *state, struct triple *ins)
9118 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
9121 RHS(ins, 0) = RHS(ins, 1);
9124 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
9126 left = read_sconst(state, ins, RHS(ins, 0));
9127 right = read_sconst(state, ins, RHS(ins, 1));
9128 mkconst(state, ins, left * right);
9130 else if (is_zero(RHS(ins, 1))) {
9131 mkconst(state, ins, 0);
9133 else if (is_one(RHS(ins, 1))) {
9134 mkcopy(state, ins, RHS(ins, 0));
9136 else if (is_pow2(RHS(ins, 1))) {
9138 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
9140 insert_triple(state, state->global_pool, val);
9141 unuse_triple(RHS(ins, 1), ins);
9142 use_triple(val, ins);
9147 static void simplify_umul(struct compile_state *state, struct triple *ins)
9149 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
9152 RHS(ins, 0) = RHS(ins, 1);
9155 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9156 ulong_t left, right;
9157 left = read_const(state, ins, RHS(ins, 0));
9158 right = read_const(state, ins, RHS(ins, 1));
9159 mkconst(state, ins, left * right);
9161 else if (is_zero(RHS(ins, 1))) {
9162 mkconst(state, ins, 0);
9164 else if (is_one(RHS(ins, 1))) {
9165 mkcopy(state, ins, RHS(ins, 0));
9167 else if (is_pow2(RHS(ins, 1))) {
9169 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
9171 insert_triple(state, state->global_pool, val);
9172 unuse_triple(RHS(ins, 1), ins);
9173 use_triple(val, ins);
9178 static void simplify_sdiv(struct compile_state *state, struct triple *ins)
9180 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
9182 left = read_sconst(state, ins, RHS(ins, 0));
9183 right = read_sconst(state, ins, RHS(ins, 1));
9184 mkconst(state, ins, left / right);
9186 else if (is_zero(RHS(ins, 0))) {
9187 mkconst(state, ins, 0);
9189 else if (is_zero(RHS(ins, 1))) {
9190 error(state, ins, "division by zero");
9192 else if (is_one(RHS(ins, 1))) {
9193 mkcopy(state, ins, RHS(ins, 0));
9195 else if (is_pow2(RHS(ins, 1))) {
9197 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
9199 insert_triple(state, state->global_pool, val);
9200 unuse_triple(RHS(ins, 1), ins);
9201 use_triple(val, ins);
9206 static void simplify_udiv(struct compile_state *state, struct triple *ins)
9208 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9209 ulong_t left, right;
9210 left = read_const(state, ins, RHS(ins, 0));
9211 right = read_const(state, ins, RHS(ins, 1));
9212 mkconst(state, ins, left / right);
9214 else if (is_zero(RHS(ins, 0))) {
9215 mkconst(state, ins, 0);
9217 else if (is_zero(RHS(ins, 1))) {
9218 error(state, ins, "division by zero");
9220 else if (is_one(RHS(ins, 1))) {
9221 mkcopy(state, ins, RHS(ins, 0));
9223 else if (is_pow2(RHS(ins, 1))) {
9225 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
9227 insert_triple(state, state->global_pool, val);
9228 unuse_triple(RHS(ins, 1), ins);
9229 use_triple(val, ins);
9234 static void simplify_smod(struct compile_state *state, struct triple *ins)
9236 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9238 left = read_const(state, ins, RHS(ins, 0));
9239 right = read_const(state, ins, RHS(ins, 1));
9240 mkconst(state, ins, left % right);
9242 else if (is_zero(RHS(ins, 0))) {
9243 mkconst(state, ins, 0);
9245 else if (is_zero(RHS(ins, 1))) {
9246 error(state, ins, "division by zero");
9248 else if (is_one(RHS(ins, 1))) {
9249 mkconst(state, ins, 0);
9251 else if (is_pow2(RHS(ins, 1))) {
9253 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
9255 insert_triple(state, state->global_pool, val);
9256 unuse_triple(RHS(ins, 1), ins);
9257 use_triple(val, ins);
9262 static void simplify_umod(struct compile_state *state, struct triple *ins)
9264 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9265 ulong_t left, right;
9266 left = read_const(state, ins, RHS(ins, 0));
9267 right = read_const(state, ins, RHS(ins, 1));
9268 mkconst(state, ins, left % right);
9270 else if (is_zero(RHS(ins, 0))) {
9271 mkconst(state, ins, 0);
9273 else if (is_zero(RHS(ins, 1))) {
9274 error(state, ins, "division by zero");
9276 else if (is_one(RHS(ins, 1))) {
9277 mkconst(state, ins, 0);
9279 else if (is_pow2(RHS(ins, 1))) {
9281 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
9283 insert_triple(state, state->global_pool, val);
9284 unuse_triple(RHS(ins, 1), ins);
9285 use_triple(val, ins);
9290 static void simplify_add(struct compile_state *state, struct triple *ins)
9292 /* start with the pointer on the left */
9293 if (is_pointer(RHS(ins, 1))) {
9296 RHS(ins, 0) = RHS(ins, 1);
9299 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9300 if (RHS(ins, 0)->op == OP_INTCONST) {
9301 ulong_t left, right;
9302 left = read_const(state, ins, RHS(ins, 0));
9303 right = read_const(state, ins, RHS(ins, 1));
9304 mkconst(state, ins, left + right);
9306 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
9307 struct triple *sdecl;
9308 ulong_t left, right;
9309 sdecl = MISC(RHS(ins, 0), 0);
9310 left = RHS(ins, 0)->u.cval;
9311 right = RHS(ins, 1)->u.cval;
9312 mkaddr_const(state, ins, sdecl, left + right);
9315 internal_warning(state, ins, "Optimize me!");
9318 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
9321 RHS(ins, 1) = RHS(ins, 0);
9326 static void simplify_sub(struct compile_state *state, struct triple *ins)
9328 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9329 if (RHS(ins, 0)->op == OP_INTCONST) {
9330 ulong_t left, right;
9331 left = read_const(state, ins, RHS(ins, 0));
9332 right = read_const(state, ins, RHS(ins, 1));
9333 mkconst(state, ins, left - right);
9335 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
9336 struct triple *sdecl;
9337 ulong_t left, right;
9338 sdecl = MISC(RHS(ins, 0), 0);
9339 left = RHS(ins, 0)->u.cval;
9340 right = RHS(ins, 1)->u.cval;
9341 mkaddr_const(state, ins, sdecl, left - right);
9344 internal_warning(state, ins, "Optimize me!");
9349 static void simplify_sl(struct compile_state *state, struct triple *ins)
9351 if (is_simple_const(RHS(ins, 1))) {
9353 right = read_const(state, ins, RHS(ins, 1));
9354 if (right >= (size_of(state, ins->type))) {
9355 warning(state, ins, "left shift count >= width of type");
9358 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9359 ulong_t left, right;
9360 left = read_const(state, ins, RHS(ins, 0));
9361 right = read_const(state, ins, RHS(ins, 1));
9362 mkconst(state, ins, left << right);
9366 static void simplify_usr(struct compile_state *state, struct triple *ins)
9368 if (is_simple_const(RHS(ins, 1))) {
9370 right = read_const(state, ins, RHS(ins, 1));
9371 if (right >= (size_of(state, ins->type))) {
9372 warning(state, ins, "right shift count >= width of type");
9375 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9376 ulong_t left, right;
9377 left = read_const(state, ins, RHS(ins, 0));
9378 right = read_const(state, ins, RHS(ins, 1));
9379 mkconst(state, ins, left >> right);
9383 static void simplify_ssr(struct compile_state *state, struct triple *ins)
9385 if (is_simple_const(RHS(ins, 1))) {
9387 right = read_const(state, ins, RHS(ins, 1));
9388 if (right >= (size_of(state, ins->type))) {
9389 warning(state, ins, "right shift count >= width of type");
9392 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9394 left = read_sconst(state, ins, RHS(ins, 0));
9395 right = read_sconst(state, ins, RHS(ins, 1));
9396 mkconst(state, ins, left >> right);
9400 static void simplify_and(struct compile_state *state, struct triple *ins)
9402 struct triple *left, *right;
9404 right = RHS(ins, 1);
9406 if (is_simple_const(left) && is_simple_const(right)) {
9408 lval = read_const(state, ins, left);
9409 rval = read_const(state, ins, right);
9410 mkconst(state, ins, lval & rval);
9412 else if (is_zero(right) || is_zero(left)) {
9413 mkconst(state, ins, 0);
9417 static void simplify_or(struct compile_state *state, struct triple *ins)
9419 struct triple *left, *right;
9421 right = RHS(ins, 1);
9423 if (is_simple_const(left) && is_simple_const(right)) {
9425 lval = read_const(state, ins, left);
9426 rval = read_const(state, ins, right);
9427 mkconst(state, ins, lval | rval);
9429 #if 0 /* I need to handle type mismatches here... */
9430 else if (is_zero(right)) {
9431 mkcopy(state, ins, left);
9433 else if (is_zero(left)) {
9434 mkcopy(state, ins, right);
9439 static void simplify_xor(struct compile_state *state, struct triple *ins)
9441 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9442 ulong_t left, right;
9443 left = read_const(state, ins, RHS(ins, 0));
9444 right = read_const(state, ins, RHS(ins, 1));
9445 mkconst(state, ins, left ^ right);
9449 static void simplify_pos(struct compile_state *state, struct triple *ins)
9451 if (is_const(RHS(ins, 0))) {
9452 mkconst(state, ins, RHS(ins, 0)->u.cval);
9455 mkcopy(state, ins, RHS(ins, 0));
9459 static void simplify_neg(struct compile_state *state, struct triple *ins)
9461 if (is_simple_const(RHS(ins, 0))) {
9463 left = read_const(state, ins, RHS(ins, 0));
9464 mkconst(state, ins, -left);
9466 else if (RHS(ins, 0)->op == OP_NEG) {
9467 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
9471 static void simplify_invert(struct compile_state *state, struct triple *ins)
9473 if (is_simple_const(RHS(ins, 0))) {
9475 left = read_const(state, ins, RHS(ins, 0));
9476 mkconst(state, ins, ~left);
9480 static void simplify_eq(struct compile_state *state, struct triple *ins)
9482 struct triple *left, *right;
9484 right = RHS(ins, 1);
9486 if (is_const(left) && is_const(right)) {
9488 val = const_eq(state, ins, left, right);
9490 mkconst(state, ins, val == 1);
9493 else if (left == right) {
9494 mkconst(state, ins, 1);
9498 static void simplify_noteq(struct compile_state *state, struct triple *ins)
9500 struct triple *left, *right;
9502 right = RHS(ins, 1);
9504 if (is_const(left) && is_const(right)) {
9506 val = const_eq(state, ins, left, right);
9508 mkconst(state, ins, val != 1);
9511 if (left == right) {
9512 mkconst(state, ins, 0);
9516 static void simplify_sless(struct compile_state *state, struct triple *ins)
9518 struct triple *left, *right;
9520 right = RHS(ins, 1);
9522 if (is_const(left) && is_const(right)) {
9524 val = const_scmp(state, ins, left, right);
9525 if ((val >= -1) && (val <= 1)) {
9526 mkconst(state, ins, val < 0);
9529 else if (left == right) {
9530 mkconst(state, ins, 0);
9534 static void simplify_uless(struct compile_state *state, struct triple *ins)
9536 struct triple *left, *right;
9538 right = RHS(ins, 1);
9540 if (is_const(left) && is_const(right)) {
9542 val = const_ucmp(state, ins, left, right);
9543 if ((val >= -1) && (val <= 1)) {
9544 mkconst(state, ins, val < 0);
9547 else if (is_zero(right)) {
9548 mkconst(state, ins, 0);
9550 else if (left == right) {
9551 mkconst(state, ins, 0);
9555 static void simplify_smore(struct compile_state *state, struct triple *ins)
9557 struct triple *left, *right;
9559 right = RHS(ins, 1);
9561 if (is_const(left) && is_const(right)) {
9563 val = const_scmp(state, ins, left, right);
9564 if ((val >= -1) && (val <= 1)) {
9565 mkconst(state, ins, val > 0);
9568 else if (left == right) {
9569 mkconst(state, ins, 0);
9573 static void simplify_umore(struct compile_state *state, struct triple *ins)
9575 struct triple *left, *right;
9577 right = RHS(ins, 1);
9579 if (is_const(left) && is_const(right)) {
9581 val = const_ucmp(state, ins, left, right);
9582 if ((val >= -1) && (val <= 1)) {
9583 mkconst(state, ins, val > 0);
9586 else if (is_zero(left)) {
9587 mkconst(state, ins, 0);
9589 else if (left == right) {
9590 mkconst(state, ins, 0);
9595 static void simplify_slesseq(struct compile_state *state, struct triple *ins)
9597 struct triple *left, *right;
9599 right = RHS(ins, 1);
9601 if (is_const(left) && is_const(right)) {
9603 val = const_scmp(state, ins, left, right);
9604 if ((val >= -1) && (val <= 1)) {
9605 mkconst(state, ins, val <= 0);
9608 else if (left == right) {
9609 mkconst(state, ins, 1);
9613 static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
9615 struct triple *left, *right;
9617 right = RHS(ins, 1);
9619 if (is_const(left) && is_const(right)) {
9621 val = const_ucmp(state, ins, left, right);
9622 if ((val >= -1) && (val <= 1)) {
9623 mkconst(state, ins, val <= 0);
9626 else if (is_zero(left)) {
9627 mkconst(state, ins, 1);
9629 else if (left == right) {
9630 mkconst(state, ins, 1);
9634 static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
9636 struct triple *left, *right;
9638 right = RHS(ins, 1);
9640 if (is_const(left) && is_const(right)) {
9642 val = const_scmp(state, ins, left, right);
9643 if ((val >= -1) && (val <= 1)) {
9644 mkconst(state, ins, val >= 0);
9647 else if (left == right) {
9648 mkconst(state, ins, 1);
9652 static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
9654 struct triple *left, *right;
9656 right = RHS(ins, 1);
9658 if (is_const(left) && is_const(right)) {
9660 val = const_ucmp(state, ins, left, right);
9661 if ((val >= -1) && (val <= 1)) {
9662 mkconst(state, ins, val >= 0);
9665 else if (is_zero(right)) {
9666 mkconst(state, ins, 1);
9668 else if (left == right) {
9669 mkconst(state, ins, 1);
9673 static void simplify_lfalse(struct compile_state *state, struct triple *ins)
9678 if (is_const(rhs)) {
9679 mkconst(state, ins, !const_ltrue(state, ins, rhs));
9681 /* Otherwise if I am the only user... */
9682 else if ((rhs->use) &&
9683 (rhs->use->member == ins) && (rhs->use->next == 0)) {
9685 /* Invert a boolean operation */
9687 case OP_LTRUE: rhs->op = OP_LFALSE; break;
9688 case OP_LFALSE: rhs->op = OP_LTRUE; break;
9689 case OP_EQ: rhs->op = OP_NOTEQ; break;
9690 case OP_NOTEQ: rhs->op = OP_EQ; break;
9691 case OP_SLESS: rhs->op = OP_SMOREEQ; break;
9692 case OP_ULESS: rhs->op = OP_UMOREEQ; break;
9693 case OP_SMORE: rhs->op = OP_SLESSEQ; break;
9694 case OP_UMORE: rhs->op = OP_ULESSEQ; break;
9695 case OP_SLESSEQ: rhs->op = OP_SMORE; break;
9696 case OP_ULESSEQ: rhs->op = OP_UMORE; break;
9697 case OP_SMOREEQ: rhs->op = OP_SLESS; break;
9698 case OP_UMOREEQ: rhs->op = OP_ULESS; break;
9704 mkcopy(state, ins, rhs);
9709 static void simplify_ltrue (struct compile_state *state, struct triple *ins)
9714 if (is_const(rhs)) {
9715 mkconst(state, ins, const_ltrue(state, ins, rhs));
9717 else switch(rhs->op) {
9718 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
9719 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
9720 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
9721 mkcopy(state, ins, rhs);
9726 static void simplify_load(struct compile_state *state, struct triple *ins)
9728 struct triple *addr, *sdecl, *blob;
9730 /* If I am doing a load with a constant pointer from a constant
9731 * table get the value.
9734 if ((addr->op == OP_ADDRCONST) && (sdecl = MISC(addr, 0)) &&
9735 (sdecl->op == OP_SDECL) && (blob = MISC(sdecl, 0)) &&
9736 (blob->op == OP_BLOBCONST)) {
9737 unsigned char buffer[SIZEOF_WORD];
9738 size_t reg_size, mem_size;
9739 const char *src, *end;
9741 reg_size = reg_size_of(state, ins->type);
9742 if (reg_size > REG_SIZEOF_REG) {
9743 internal_error(state, ins, "load size greater than register");
9745 mem_size = size_of(state, ins->type);
9747 end += bits_to_bytes(size_of(state, sdecl->type));
9749 src += addr->u.cval;
9752 error(state, ins, "Load address out of bounds");
9755 memset(buffer, 0, sizeof(buffer));
9756 memcpy(buffer, src, bits_to_bytes(mem_size));
9759 case SIZEOF_I8: val = *((uint8_t *) buffer); break;
9760 case SIZEOF_I16: val = *((uint16_t *)buffer); break;
9761 case SIZEOF_I32: val = *((uint32_t *)buffer); break;
9762 case SIZEOF_I64: val = *((uint64_t *)buffer); break;
9764 internal_error(state, ins, "mem_size: %d not handled",
9769 mkconst(state, ins, val);
9773 static void simplify_uextract(struct compile_state *state, struct triple *ins)
9775 if (is_simple_const(RHS(ins, 0))) {
9778 val = read_const(state, ins, RHS(ins, 0));
9780 mask <<= ins->u.bitfield.size;
9782 val >>= ins->u.bitfield.offset;
9784 mkconst(state, ins, val);
9788 static void simplify_sextract(struct compile_state *state, struct triple *ins)
9790 if (is_simple_const(RHS(ins, 0))) {
9794 val = read_const(state, ins, RHS(ins, 0));
9796 mask <<= ins->u.bitfield.size;
9798 val >>= ins->u.bitfield.offset;
9800 val <<= (SIZEOF_LONG - ins->u.bitfield.size);
9802 sval >>= (SIZEOF_LONG - ins->u.bitfield.size);
9803 mkconst(state, ins, sval);
9807 static void simplify_deposit(struct compile_state *state, struct triple *ins)
9809 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9812 targ = read_const(state, ins, RHS(ins, 0));
9813 val = read_const(state, ins, RHS(ins, 1));
9815 mask <<= ins->u.bitfield.size;
9817 mask <<= ins->u.bitfield.offset;
9819 val <<= ins->u.bitfield.offset;
9822 mkconst(state, ins, targ);
9826 static void simplify_copy(struct compile_state *state, struct triple *ins)
9828 struct triple *right;
9829 right = RHS(ins, 0);
9830 if (is_subset_type(ins->type, right->type)) {
9831 ins->type = right->type;
9833 if (equiv_types(ins->type, right->type)) {
9834 ins->op = OP_COPY;/* I don't need to convert if the types match */
9836 if (ins->op == OP_COPY) {
9837 internal_error(state, ins, "type mismatch on copy");
9840 if (is_const(right) && (right->op == OP_ADDRCONST) && is_pointer(ins)) {
9841 struct triple *sdecl;
9843 sdecl = MISC(right, 0);
9844 offset = right->u.cval;
9845 mkaddr_const(state, ins, sdecl, offset);
9847 else if (is_const(right) && is_write_compatible(state, ins->type, right->type)) {
9852 left = read_const(state, ins, right);
9853 /* Ensure I have not overflowed the destination. */
9854 if (size_of(state, right->type) > size_of(state, ins->type)) {
9857 mask <<= size_of(state, ins->type);
9861 /* Ensure I am properly sign extended */
9862 if (size_of(state, right->type) < size_of(state, ins->type) &&
9863 is_signed(right->type)) {
9866 shift = SIZEOF_LONG - size_of(state, right->type);
9872 mkconst(state, ins, left);
9876 internal_error(state, ins, "uknown constant");
9882 static int phi_present(struct block *block)
9890 if (ptr->op == OP_PHI) {
9894 } while(ptr != block->last);
9898 static int phi_dependency(struct block *block)
9900 /* A block has a phi dependency if a phi function
9901 * depends on that block to exist, and makes a block
9902 * that is otherwise useless unsafe to remove.
9905 struct block_set *edge;
9906 for(edge = block->edges; edge; edge = edge->next) {
9907 if (phi_present(edge->member)) {
9915 static struct triple *branch_target(struct compile_state *state, struct triple *ins)
9917 struct triple *targ;
9918 targ = TARG(ins, 0);
9919 /* During scc_transform temporary triples are allocated that
9920 * loop back onto themselves. If I see one don't advance the
9923 while(triple_is_structural(state, targ) &&
9924 (targ->next != targ) && (targ->next != state->first)) {
9931 static void simplify_branch(struct compile_state *state, struct triple *ins)
9933 int simplified, loops;
9934 if ((ins->op != OP_BRANCH) && (ins->op != OP_CBRANCH)) {
9935 internal_error(state, ins, "not branch");
9937 if (ins->use != 0) {
9938 internal_error(state, ins, "branch use");
9940 /* The challenge here with simplify branch is that I need to
9941 * make modifications to the control flow graph as well
9942 * as to the branch instruction itself. That is handled
9943 * by rebuilding the basic blocks after simplify all is called.
9946 /* If we have a branch to an unconditional branch update
9947 * our target. But watch out for dependencies from phi
9949 * Also only do this a limited number of times so
9950 * we don't get into an infinite loop.
9954 struct triple *targ;
9956 targ = branch_target(state, ins);
9957 if ((targ != ins) && (targ->op == OP_BRANCH) &&
9958 !phi_dependency(targ->u.block))
9960 unuse_triple(TARG(ins, 0), ins);
9961 TARG(ins, 0) = TARG(targ, 0);
9962 use_triple(TARG(ins, 0), ins);
9965 } while(simplified && (++loops < 20));
9967 /* If we have a conditional branch with a constant condition
9968 * make it an unconditional branch.
9970 if ((ins->op == OP_CBRANCH) && is_simple_const(RHS(ins, 0))) {
9971 struct triple *targ;
9973 value = read_const(state, ins, RHS(ins, 0));
9974 unuse_triple(RHS(ins, 0), ins);
9975 targ = TARG(ins, 0);
9978 ins->op = OP_BRANCH;
9980 unuse_triple(ins->next, ins);
9981 TARG(ins, 0) = targ;
9984 unuse_triple(targ, ins);
9985 TARG(ins, 0) = ins->next;
9989 /* If we have a branch to the next instruction,
9992 if (TARG(ins, 0) == ins->next) {
9993 unuse_triple(TARG(ins, 0), ins);
9994 if (ins->op == OP_CBRANCH) {
9995 unuse_triple(RHS(ins, 0), ins);
9996 unuse_triple(ins->next, ins);
10004 internal_error(state, ins, "noop use != 0");
10009 static void simplify_label(struct compile_state *state, struct triple *ins)
10011 /* Ignore volatile labels */
10012 if (!triple_is_pure(state, ins, ins->id)) {
10015 if (ins->use == 0) {
10018 else if (ins->prev->op == OP_LABEL) {
10019 /* In general it is not safe to merge one label that
10020 * imediately follows another. The problem is that the empty
10021 * looking block may have phi functions that depend on it.
10023 if (!phi_dependency(ins->prev->u.block)) {
10024 struct triple_set *user, *next;
10026 for(user = ins->use; user; user = next) {
10027 struct triple *use, **expr;
10029 use = user->member;
10030 expr = triple_targ(state, use, 0);
10031 for(;expr; expr = triple_targ(state, use, expr)) {
10032 if (*expr == ins) {
10034 unuse_triple(ins, use);
10035 use_triple(ins->prev, use);
10041 internal_error(state, ins, "noop use != 0");
10047 static void simplify_phi(struct compile_state *state, struct triple *ins)
10049 struct triple **slot;
10050 struct triple *value;
10053 slot = &RHS(ins, 0);
10058 /* See if all of the rhs members of a phi have the same value */
10059 if (slot[0] && is_simple_const(slot[0])) {
10060 cvalue = read_const(state, ins, slot[0]);
10061 for(i = 1; i < zrhs; i++) {
10063 !is_simple_const(slot[i]) ||
10064 !equiv_types(slot[0]->type, slot[i]->type) ||
10065 (cvalue != read_const(state, ins, slot[i]))) {
10070 mkconst(state, ins, cvalue);
10075 /* See if all of rhs members of a phi are the same */
10077 for(i = 1; i < zrhs; i++) {
10078 if (slot[i] != value) {
10083 /* If the phi has a single value just copy it */
10084 if (!is_subset_type(ins->type, value->type)) {
10085 internal_error(state, ins, "bad input type to phi");
10087 /* Make the types match */
10088 if (!equiv_types(ins->type, value->type)) {
10089 ins->type = value->type;
10091 /* Now make the actual copy */
10092 mkcopy(state, ins, value);
10098 static void simplify_bsf(struct compile_state *state, struct triple *ins)
10100 if (is_simple_const(RHS(ins, 0))) {
10102 left = read_const(state, ins, RHS(ins, 0));
10103 mkconst(state, ins, bsf(left));
10107 static void simplify_bsr(struct compile_state *state, struct triple *ins)
10109 if (is_simple_const(RHS(ins, 0))) {
10111 left = read_const(state, ins, RHS(ins, 0));
10112 mkconst(state, ins, bsr(left));
10117 typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
10118 static const struct simplify_table {
10120 unsigned long flag;
10121 } table_simplify[] = {
10122 #define simplify_sdivt simplify_noop
10123 #define simplify_udivt simplify_noop
10124 #define simplify_piece simplify_noop
10126 [OP_SDIVT ] = { simplify_sdivt, COMPILER_SIMPLIFY_ARITH },
10127 [OP_UDIVT ] = { simplify_udivt, COMPILER_SIMPLIFY_ARITH },
10128 [OP_SMUL ] = { simplify_smul, COMPILER_SIMPLIFY_ARITH },
10129 [OP_UMUL ] = { simplify_umul, COMPILER_SIMPLIFY_ARITH },
10130 [OP_SDIV ] = { simplify_sdiv, COMPILER_SIMPLIFY_ARITH },
10131 [OP_UDIV ] = { simplify_udiv, COMPILER_SIMPLIFY_ARITH },
10132 [OP_SMOD ] = { simplify_smod, COMPILER_SIMPLIFY_ARITH },
10133 [OP_UMOD ] = { simplify_umod, COMPILER_SIMPLIFY_ARITH },
10134 [OP_ADD ] = { simplify_add, COMPILER_SIMPLIFY_ARITH },
10135 [OP_SUB ] = { simplify_sub, COMPILER_SIMPLIFY_ARITH },
10136 [OP_SL ] = { simplify_sl, COMPILER_SIMPLIFY_SHIFT },
10137 [OP_USR ] = { simplify_usr, COMPILER_SIMPLIFY_SHIFT },
10138 [OP_SSR ] = { simplify_ssr, COMPILER_SIMPLIFY_SHIFT },
10139 [OP_AND ] = { simplify_and, COMPILER_SIMPLIFY_BITWISE },
10140 [OP_XOR ] = { simplify_xor, COMPILER_SIMPLIFY_BITWISE },
10141 [OP_OR ] = { simplify_or, COMPILER_SIMPLIFY_BITWISE },
10142 [OP_POS ] = { simplify_pos, COMPILER_SIMPLIFY_ARITH },
10143 [OP_NEG ] = { simplify_neg, COMPILER_SIMPLIFY_ARITH },
10144 [OP_INVERT ] = { simplify_invert, COMPILER_SIMPLIFY_BITWISE },
10146 [OP_EQ ] = { simplify_eq, COMPILER_SIMPLIFY_LOGICAL },
10147 [OP_NOTEQ ] = { simplify_noteq, COMPILER_SIMPLIFY_LOGICAL },
10148 [OP_SLESS ] = { simplify_sless, COMPILER_SIMPLIFY_LOGICAL },
10149 [OP_ULESS ] = { simplify_uless, COMPILER_SIMPLIFY_LOGICAL },
10150 [OP_SMORE ] = { simplify_smore, COMPILER_SIMPLIFY_LOGICAL },
10151 [OP_UMORE ] = { simplify_umore, COMPILER_SIMPLIFY_LOGICAL },
10152 [OP_SLESSEQ ] = { simplify_slesseq, COMPILER_SIMPLIFY_LOGICAL },
10153 [OP_ULESSEQ ] = { simplify_ulesseq, COMPILER_SIMPLIFY_LOGICAL },
10154 [OP_SMOREEQ ] = { simplify_smoreeq, COMPILER_SIMPLIFY_LOGICAL },
10155 [OP_UMOREEQ ] = { simplify_umoreeq, COMPILER_SIMPLIFY_LOGICAL },
10156 [OP_LFALSE ] = { simplify_lfalse, COMPILER_SIMPLIFY_LOGICAL },
10157 [OP_LTRUE ] = { simplify_ltrue, COMPILER_SIMPLIFY_LOGICAL },
10159 [OP_LOAD ] = { simplify_load, COMPILER_SIMPLIFY_OP },
10160 [OP_STORE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10162 [OP_UEXTRACT ] = { simplify_uextract, COMPILER_SIMPLIFY_BITFIELD },
10163 [OP_SEXTRACT ] = { simplify_sextract, COMPILER_SIMPLIFY_BITFIELD },
10164 [OP_DEPOSIT ] = { simplify_deposit, COMPILER_SIMPLIFY_BITFIELD },
10166 [OP_NOOP ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10168 [OP_INTCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10169 [OP_BLOBCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10170 [OP_ADDRCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10171 [OP_UNKNOWNVAL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10173 [OP_WRITE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10174 [OP_READ ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10175 [OP_COPY ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
10176 [OP_CONVERT ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
10177 [OP_PIECE ] = { simplify_piece, COMPILER_SIMPLIFY_OP },
10178 [OP_ASM ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10180 [OP_DOT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10181 [OP_INDEX ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10183 [OP_LIST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10184 [OP_BRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10185 [OP_CBRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10186 [OP_CALL ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10187 [OP_RET ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10188 [OP_LABEL ] = { simplify_label, COMPILER_SIMPLIFY_LABEL },
10189 [OP_ADECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10190 [OP_SDECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10191 [OP_PHI ] = { simplify_phi, COMPILER_SIMPLIFY_PHI },
10193 [OP_INB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10194 [OP_INW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10195 [OP_INL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10196 [OP_OUTB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10197 [OP_OUTW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10198 [OP_OUTL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10199 [OP_BSF ] = { simplify_bsf, COMPILER_SIMPLIFY_OP },
10200 [OP_BSR ] = { simplify_bsr, COMPILER_SIMPLIFY_OP },
10201 [OP_RDMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10202 [OP_WRMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10203 [OP_HLT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10206 static inline void debug_simplify(struct compile_state *state,
10207 simplify_t do_simplify, struct triple *ins)
10209 #if DEBUG_SIMPLIFY_HIRES
10210 if (state->functions_joined && (do_simplify != simplify_noop)) {
10211 /* High resolution debugging mode */
10212 fprintf(state->dbgout, "simplifing: ");
10213 display_triple(state->dbgout, ins);
10216 do_simplify(state, ins);
10217 #if DEBUG_SIMPLIFY_HIRES
10218 if (state->functions_joined && (do_simplify != simplify_noop)) {
10219 /* High resolution debugging mode */
10220 fprintf(state->dbgout, "simplified: ");
10221 display_triple(state->dbgout, ins);
10225 static void simplify(struct compile_state *state, struct triple *ins)
10228 simplify_t do_simplify;
10229 if (ins == &unknown_triple) {
10230 internal_error(state, ins, "simplifying the unknown triple?");
10235 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
10239 do_simplify = table_simplify[op].func;
10242 !(state->compiler->flags & table_simplify[op].flag)) {
10243 do_simplify = simplify_noop;
10245 if (do_simplify && (ins->id & TRIPLE_FLAG_VOLATILE)) {
10246 do_simplify = simplify_noop;
10249 if (!do_simplify) {
10250 internal_error(state, ins, "cannot simplify op: %d %s",
10254 debug_simplify(state, do_simplify, ins);
10255 } while(ins->op != op);
10258 static void rebuild_ssa_form(struct compile_state *state);
10260 static void simplify_all(struct compile_state *state)
10262 struct triple *ins, *first;
10263 if (!(state->compiler->flags & COMPILER_SIMPLIFY)) {
10266 first = state->first;
10269 simplify(state, ins);
10271 } while(ins != first->prev);
10274 simplify(state, ins);
10276 }while(ins != first);
10277 rebuild_ssa_form(state);
10279 print_blocks(state, __func__, state->dbgout);
10284 * ============================
10287 static void register_builtin_function(struct compile_state *state,
10288 const char *name, int op, struct type *rtype, ...)
10290 struct type *ftype, *atype, *ctype, *crtype, *param, **next;
10291 struct triple *def, *arg, *result, *work, *last, *first, *retvar, *ret;
10292 struct hash_entry *ident;
10293 struct file_state file;
10299 /* Dummy file state to get debug handling right */
10300 memset(&file, 0, sizeof(file));
10301 file.basename = "<built-in>";
10303 file.report_line = 1;
10304 file.report_name = file.basename;
10305 file.prev = state->file;
10306 state->file = &file;
10307 state->function = name;
10309 /* Find the Parameter count */
10310 valid_op(state, op);
10311 parameters = table_ops[op].rhs;
10312 if (parameters < 0 ) {
10313 internal_error(state, 0, "Invalid builtin parameter count");
10316 /* Find the function type */
10317 ftype = new_type(TYPE_FUNCTION | STOR_INLINE | STOR_STATIC, rtype, 0);
10318 ftype->elements = parameters;
10319 next = &ftype->right;
10320 va_start(args, rtype);
10321 for(i = 0; i < parameters; i++) {
10322 atype = va_arg(args, struct type *);
10326 *next = new_type(TYPE_PRODUCT, *next, atype);
10327 next = &((*next)->right);
10331 *next = &void_type;
10335 /* Get the initial closure type */
10336 ctype = new_type(TYPE_JOIN, &void_type, 0);
10337 ctype->elements = 1;
10339 /* Get the return type */
10340 crtype = new_type(TYPE_TUPLE, new_type(TYPE_PRODUCT, ctype, rtype), 0);
10341 crtype->elements = 2;
10343 /* Generate the needed triples */
10344 def = triple(state, OP_LIST, ftype, 0, 0);
10345 first = label(state);
10346 RHS(def, 0) = first;
10347 result = flatten(state, first, variable(state, crtype));
10348 retvar = flatten(state, first, variable(state, &void_ptr_type));
10349 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
10351 /* Now string them together */
10352 param = ftype->right;
10353 for(i = 0; i < parameters; i++) {
10354 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10355 atype = param->left;
10359 arg = flatten(state, first, variable(state, atype));
10360 param = param->right;
10362 work = new_triple(state, op, rtype, -1, parameters);
10363 generate_lhs_pieces(state, work);
10364 for(i = 0; i < parameters; i++) {
10365 RHS(work, i) = read_expr(state, farg(state, def, i));
10367 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
10368 work = write_expr(state, deref_index(state, result, 1), work);
10370 work = flatten(state, first, work);
10371 last = flatten(state, first, label(state));
10372 ret = flatten(state, first, ret);
10373 name_len = strlen(name);
10374 ident = lookup(state, name, name_len);
10375 ftype->type_ident = ident;
10376 symbol(state, ident, &ident->sym_ident, def, ftype);
10378 state->file = file.prev;
10379 state->function = 0;
10380 state->main_function = 0;
10382 if (!state->functions) {
10383 state->functions = def;
10385 insert_triple(state, state->functions, def);
10387 if (state->compiler->debug & DEBUG_INLINE) {
10388 FILE *fp = state->dbgout;
10391 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
10392 display_func(state, fp, def);
10393 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
10397 static struct type *partial_struct(struct compile_state *state,
10398 const char *field_name, struct type *type, struct type *rest)
10400 struct hash_entry *field_ident;
10401 struct type *result;
10402 int field_name_len;
10404 field_name_len = strlen(field_name);
10405 field_ident = lookup(state, field_name, field_name_len);
10407 result = clone_type(0, type);
10408 result->field_ident = field_ident;
10411 result = new_type(TYPE_PRODUCT, result, rest);
10416 static struct type *register_builtin_type(struct compile_state *state,
10417 const char *name, struct type *type)
10419 struct hash_entry *ident;
10422 name_len = strlen(name);
10423 ident = lookup(state, name, name_len);
10425 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
10426 ulong_t elements = 0;
10427 struct type *field;
10428 type = new_type(TYPE_STRUCT, type, 0);
10429 field = type->left;
10430 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
10432 field = field->right;
10435 symbol(state, ident, &ident->sym_tag, 0, type);
10436 type->type_ident = ident;
10437 type->elements = elements;
10439 symbol(state, ident, &ident->sym_ident, 0, type);
10440 ident->tok = TOK_TYPE_NAME;
10445 static void register_builtins(struct compile_state *state)
10447 struct type *div_type, *ldiv_type;
10448 struct type *udiv_type, *uldiv_type;
10449 struct type *msr_type;
10451 div_type = register_builtin_type(state, "__builtin_div_t",
10452 partial_struct(state, "quot", &int_type,
10453 partial_struct(state, "rem", &int_type, 0)));
10454 ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
10455 partial_struct(state, "quot", &long_type,
10456 partial_struct(state, "rem", &long_type, 0)));
10457 udiv_type = register_builtin_type(state, "__builtin_udiv_t",
10458 partial_struct(state, "quot", &uint_type,
10459 partial_struct(state, "rem", &uint_type, 0)));
10460 uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
10461 partial_struct(state, "quot", &ulong_type,
10462 partial_struct(state, "rem", &ulong_type, 0)));
10464 register_builtin_function(state, "__builtin_div", OP_SDIVT, div_type,
10465 &int_type, &int_type);
10466 register_builtin_function(state, "__builtin_ldiv", OP_SDIVT, ldiv_type,
10467 &long_type, &long_type);
10468 register_builtin_function(state, "__builtin_udiv", OP_UDIVT, udiv_type,
10469 &uint_type, &uint_type);
10470 register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
10471 &ulong_type, &ulong_type);
10473 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
10475 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
10477 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
10480 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
10481 &uchar_type, &ushort_type);
10482 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
10483 &ushort_type, &ushort_type);
10484 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
10485 &uint_type, &ushort_type);
10487 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
10489 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
10492 msr_type = register_builtin_type(state, "__builtin_msr_t",
10493 partial_struct(state, "lo", &ulong_type,
10494 partial_struct(state, "hi", &ulong_type, 0)));
10496 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
10498 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
10499 &ulong_type, &ulong_type, &ulong_type);
10501 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
10505 static struct type *declarator(
10506 struct compile_state *state, struct type *type,
10507 struct hash_entry **ident, int need_ident);
10508 static void decl(struct compile_state *state, struct triple *first);
10509 static struct type *specifier_qualifier_list(struct compile_state *state);
10510 static int isdecl_specifier(int tok);
10511 static struct type *decl_specifiers(struct compile_state *state);
10512 static int istype(int tok);
10513 static struct triple *expr(struct compile_state *state);
10514 static struct triple *assignment_expr(struct compile_state *state);
10515 static struct type *type_name(struct compile_state *state);
10516 static void statement(struct compile_state *state, struct triple *first);
10518 static struct triple *call_expr(
10519 struct compile_state *state, struct triple *func)
10521 struct triple *def;
10522 struct type *param, *type;
10523 ulong_t pvals, index;
10525 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
10526 error(state, 0, "Called object is not a function");
10528 if (func->op != OP_LIST) {
10529 internal_error(state, 0, "improper function");
10531 eat(state, TOK_LPAREN);
10532 /* Find the return type without any specifiers */
10533 type = clone_type(0, func->type->left);
10534 /* Count the number of rhs entries for OP_FCALL */
10535 param = func->type->right;
10537 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10539 param = param->right;
10541 if ((param->type & TYPE_MASK) != TYPE_VOID) {
10544 def = new_triple(state, OP_FCALL, type, -1, pvals);
10545 MISC(def, 0) = func;
10547 param = func->type->right;
10548 for(index = 0; index < pvals; index++) {
10549 struct triple *val;
10550 struct type *arg_type;
10551 val = read_expr(state, assignment_expr(state));
10553 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10554 arg_type = param->left;
10556 write_compatible(state, arg_type, val->type);
10557 RHS(def, index) = val;
10558 if (index != (pvals - 1)) {
10559 eat(state, TOK_COMMA);
10560 param = param->right;
10563 eat(state, TOK_RPAREN);
10568 static struct triple *character_constant(struct compile_state *state)
10570 struct triple *def;
10572 const signed char *str, *end;
10575 tk = eat(state, TOK_LIT_CHAR);
10576 str = tk->val.str + 1;
10577 str_len = tk->str_len - 2;
10578 if (str_len <= 0) {
10579 error(state, 0, "empty character constant");
10581 end = str + str_len;
10582 c = char_value(state, &str, end);
10584 error(state, 0, "multibyte character constant not supported");
10586 def = int_const(state, &char_type, (ulong_t)((long_t)c));
10590 static struct triple *string_constant(struct compile_state *state)
10592 struct triple *def;
10595 const signed char *str, *end;
10596 signed char *buf, *ptr;
10600 type = new_type(TYPE_ARRAY, &char_type, 0);
10601 type->elements = 0;
10602 /* The while loop handles string concatenation */
10604 tk = eat(state, TOK_LIT_STRING);
10605 str = tk->val.str + 1;
10606 str_len = tk->str_len - 2;
10608 error(state, 0, "negative string constant length");
10610 end = str + str_len;
10612 buf = xmalloc(type->elements + str_len + 1, "string_constant");
10613 memcpy(buf, ptr, type->elements);
10614 ptr = buf + type->elements;
10616 *ptr++ = char_value(state, &str, end);
10617 } while(str < end);
10618 type->elements = ptr - buf;
10619 } while(peek(state) == TOK_LIT_STRING);
10621 type->elements += 1;
10622 def = triple(state, OP_BLOBCONST, type, 0, 0);
10629 static struct triple *integer_constant(struct compile_state *state)
10631 struct triple *def;
10638 tk = eat(state, TOK_LIT_INT);
10640 decimal = (tk->val.str[0] != '0');
10641 val = strtoul(tk->val.str, &end, 0);
10642 if ((val > ULONG_T_MAX) || ((val == ULONG_MAX) && (errno == ERANGE))) {
10643 error(state, 0, "Integer constant to large");
10646 if ((*end == 'u') || (*end == 'U')) {
10650 if ((*end == 'l') || (*end == 'L')) {
10654 if ((*end == 'u') || (*end == 'U')) {
10659 error(state, 0, "Junk at end of integer constant");
10662 type = &ulong_type;
10666 if (!decimal && (val > LONG_T_MAX)) {
10667 type = &ulong_type;
10672 if (val > UINT_T_MAX) {
10673 type = &ulong_type;
10678 if (!decimal && (val > INT_T_MAX) && (val <= UINT_T_MAX)) {
10681 else if (!decimal && (val > LONG_T_MAX)) {
10682 type = &ulong_type;
10684 else if (val > INT_T_MAX) {
10688 def = int_const(state, type, val);
10692 static struct triple *primary_expr(struct compile_state *state)
10694 struct triple *def;
10700 struct hash_entry *ident;
10701 /* Here ident is either:
10705 ident = eat(state, TOK_IDENT)->ident;
10706 if (!ident->sym_ident) {
10707 error(state, 0, "%s undeclared", ident->name);
10709 def = ident->sym_ident->def;
10712 case TOK_ENUM_CONST:
10714 struct hash_entry *ident;
10715 /* Here ident is an enumeration constant */
10716 ident = eat(state, TOK_ENUM_CONST)->ident;
10717 if (!ident->sym_ident) {
10718 error(state, 0, "%s undeclared", ident->name);
10720 def = ident->sym_ident->def;
10725 struct hash_entry *ident;
10726 ident = eat(state, TOK_MIDENT)->ident;
10727 warning(state, 0, "Replacing undefined macro: %s with 0",
10729 def = int_const(state, &int_type, 0);
10733 eat(state, TOK_LPAREN);
10735 eat(state, TOK_RPAREN);
10738 def = integer_constant(state);
10740 case TOK_LIT_FLOAT:
10741 eat(state, TOK_LIT_FLOAT);
10742 error(state, 0, "Floating point constants not supported");
10747 def = character_constant(state);
10749 case TOK_LIT_STRING:
10750 def = string_constant(state);
10754 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
10759 static struct triple *postfix_expr(struct compile_state *state)
10761 struct triple *def;
10763 def = primary_expr(state);
10765 struct triple *left;
10769 switch((tok = peek(state))) {
10771 eat(state, TOK_LBRACKET);
10772 def = mk_subscript_expr(state, left, expr(state));
10773 eat(state, TOK_RBRACKET);
10776 def = call_expr(state, def);
10780 struct hash_entry *field;
10781 eat(state, TOK_DOT);
10782 field = eat(state, TOK_IDENT)->ident;
10783 def = deref_field(state, def, field);
10788 struct hash_entry *field;
10789 eat(state, TOK_ARROW);
10790 field = eat(state, TOK_IDENT)->ident;
10791 def = mk_deref_expr(state, read_expr(state, def));
10792 def = deref_field(state, def, field);
10796 eat(state, TOK_PLUSPLUS);
10797 def = mk_post_inc_expr(state, left);
10799 case TOK_MINUSMINUS:
10800 eat(state, TOK_MINUSMINUS);
10801 def = mk_post_dec_expr(state, left);
10811 static struct triple *cast_expr(struct compile_state *state);
10813 static struct triple *unary_expr(struct compile_state *state)
10815 struct triple *def, *right;
10817 switch((tok = peek(state))) {
10819 eat(state, TOK_PLUSPLUS);
10820 def = mk_pre_inc_expr(state, unary_expr(state));
10822 case TOK_MINUSMINUS:
10823 eat(state, TOK_MINUSMINUS);
10824 def = mk_pre_dec_expr(state, unary_expr(state));
10827 eat(state, TOK_AND);
10828 def = mk_addr_expr(state, cast_expr(state), 0);
10831 eat(state, TOK_STAR);
10832 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
10835 eat(state, TOK_PLUS);
10836 right = read_expr(state, cast_expr(state));
10837 arithmetic(state, right);
10838 def = integral_promotion(state, right);
10841 eat(state, TOK_MINUS);
10842 right = read_expr(state, cast_expr(state));
10843 arithmetic(state, right);
10844 def = integral_promotion(state, right);
10845 def = triple(state, OP_NEG, def->type, def, 0);
10848 eat(state, TOK_TILDE);
10849 right = read_expr(state, cast_expr(state));
10850 integral(state, right);
10851 def = integral_promotion(state, right);
10852 def = triple(state, OP_INVERT, def->type, def, 0);
10855 eat(state, TOK_BANG);
10856 right = read_expr(state, cast_expr(state));
10857 bool(state, right);
10858 def = lfalse_expr(state, right);
10864 eat(state, TOK_SIZEOF);
10865 tok1 = peek(state);
10866 tok2 = peek2(state);
10867 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
10868 eat(state, TOK_LPAREN);
10869 type = type_name(state);
10870 eat(state, TOK_RPAREN);
10873 struct triple *expr;
10874 expr = unary_expr(state);
10876 release_expr(state, expr);
10878 def = int_const(state, &ulong_type, size_of_in_bytes(state, type));
10885 eat(state, TOK_ALIGNOF);
10886 tok1 = peek(state);
10887 tok2 = peek2(state);
10888 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
10889 eat(state, TOK_LPAREN);
10890 type = type_name(state);
10891 eat(state, TOK_RPAREN);
10894 struct triple *expr;
10895 expr = unary_expr(state);
10897 release_expr(state, expr);
10899 def = int_const(state, &ulong_type, align_of_in_bytes(state, type));
10904 /* We only come here if we are called from the preprocessor */
10905 struct hash_entry *ident;
10907 eat(state, TOK_MDEFINED);
10909 if (cpp_peek(state) == TOK_LPAREN) {
10910 cpp_eat(state, TOK_LPAREN);
10913 ident = cpp_eat(state, TOK_MIDENT)->ident;
10915 eat(state, TOK_RPAREN);
10917 def = int_const(state, &int_type, ident->sym_define != 0);
10921 def = postfix_expr(state);
10927 static struct triple *cast_expr(struct compile_state *state)
10929 struct triple *def;
10931 tok1 = peek(state);
10932 tok2 = peek2(state);
10933 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
10935 eat(state, TOK_LPAREN);
10936 type = type_name(state);
10937 eat(state, TOK_RPAREN);
10938 def = mk_cast_expr(state, type, cast_expr(state));
10941 def = unary_expr(state);
10946 static struct triple *mult_expr(struct compile_state *state)
10948 struct triple *def;
10950 def = cast_expr(state);
10952 struct triple *left, *right;
10953 struct type *result_type;
10956 switch(tok = (peek(state))) {
10960 left = read_expr(state, def);
10961 arithmetic(state, left);
10965 right = read_expr(state, cast_expr(state));
10966 arithmetic(state, right);
10968 result_type = arithmetic_result(state, left, right);
10969 sign = is_signed(result_type);
10972 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
10973 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
10974 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
10976 def = triple(state, op, result_type, left, right);
10986 static struct triple *add_expr(struct compile_state *state)
10988 struct triple *def;
10990 def = mult_expr(state);
10993 switch( peek(state)) {
10995 eat(state, TOK_PLUS);
10996 def = mk_add_expr(state, def, mult_expr(state));
10999 eat(state, TOK_MINUS);
11000 def = mk_sub_expr(state, def, mult_expr(state));
11010 static struct triple *shift_expr(struct compile_state *state)
11012 struct triple *def;
11014 def = add_expr(state);
11016 struct triple *left, *right;
11019 switch((tok = peek(state))) {
11022 left = read_expr(state, def);
11023 integral(state, left);
11024 left = integral_promotion(state, left);
11028 right = read_expr(state, add_expr(state));
11029 integral(state, right);
11030 right = integral_promotion(state, right);
11032 op = (tok == TOK_SL)? OP_SL :
11033 is_signed(left->type)? OP_SSR: OP_USR;
11035 def = triple(state, op, left->type, left, right);
11045 static struct triple *relational_expr(struct compile_state *state)
11047 #warning "Extend relational exprs to work on more than arithmetic types"
11048 struct triple *def;
11050 def = shift_expr(state);
11052 struct triple *left, *right;
11053 struct type *arg_type;
11056 switch((tok = peek(state))) {
11061 left = read_expr(state, def);
11062 arithmetic(state, left);
11066 right = read_expr(state, shift_expr(state));
11067 arithmetic(state, right);
11069 arg_type = arithmetic_result(state, left, right);
11070 sign = is_signed(arg_type);
11073 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
11074 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
11075 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
11076 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
11078 def = triple(state, op, &int_type, left, right);
11088 static struct triple *equality_expr(struct compile_state *state)
11090 #warning "Extend equality exprs to work on more than arithmetic types"
11091 struct triple *def;
11093 def = relational_expr(state);
11095 struct triple *left, *right;
11098 switch((tok = peek(state))) {
11101 left = read_expr(state, def);
11102 arithmetic(state, left);
11104 right = read_expr(state, relational_expr(state));
11105 arithmetic(state, right);
11106 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
11107 def = triple(state, op, &int_type, left, right);
11117 static struct triple *and_expr(struct compile_state *state)
11119 struct triple *def;
11120 def = equality_expr(state);
11121 while(peek(state) == TOK_AND) {
11122 struct triple *left, *right;
11123 struct type *result_type;
11124 left = read_expr(state, def);
11125 integral(state, left);
11126 eat(state, TOK_AND);
11127 right = read_expr(state, equality_expr(state));
11128 integral(state, right);
11129 result_type = arithmetic_result(state, left, right);
11130 def = triple(state, OP_AND, result_type, left, right);
11135 static struct triple *xor_expr(struct compile_state *state)
11137 struct triple *def;
11138 def = and_expr(state);
11139 while(peek(state) == TOK_XOR) {
11140 struct triple *left, *right;
11141 struct type *result_type;
11142 left = read_expr(state, def);
11143 integral(state, left);
11144 eat(state, TOK_XOR);
11145 right = read_expr(state, and_expr(state));
11146 integral(state, right);
11147 result_type = arithmetic_result(state, left, right);
11148 def = triple(state, OP_XOR, result_type, left, right);
11153 static struct triple *or_expr(struct compile_state *state)
11155 struct triple *def;
11156 def = xor_expr(state);
11157 while(peek(state) == TOK_OR) {
11158 struct triple *left, *right;
11159 struct type *result_type;
11160 left = read_expr(state, def);
11161 integral(state, left);
11162 eat(state, TOK_OR);
11163 right = read_expr(state, xor_expr(state));
11164 integral(state, right);
11165 result_type = arithmetic_result(state, left, right);
11166 def = triple(state, OP_OR, result_type, left, right);
11171 static struct triple *land_expr(struct compile_state *state)
11173 struct triple *def;
11174 def = or_expr(state);
11175 while(peek(state) == TOK_LOGAND) {
11176 struct triple *left, *right;
11177 left = read_expr(state, def);
11179 eat(state, TOK_LOGAND);
11180 right = read_expr(state, or_expr(state));
11181 bool(state, right);
11183 def = mkland_expr(state,
11184 ltrue_expr(state, left),
11185 ltrue_expr(state, right));
11190 static struct triple *lor_expr(struct compile_state *state)
11192 struct triple *def;
11193 def = land_expr(state);
11194 while(peek(state) == TOK_LOGOR) {
11195 struct triple *left, *right;
11196 left = read_expr(state, def);
11198 eat(state, TOK_LOGOR);
11199 right = read_expr(state, land_expr(state));
11200 bool(state, right);
11202 def = mklor_expr(state,
11203 ltrue_expr(state, left),
11204 ltrue_expr(state, right));
11209 static struct triple *conditional_expr(struct compile_state *state)
11211 struct triple *def;
11212 def = lor_expr(state);
11213 if (peek(state) == TOK_QUEST) {
11214 struct triple *test, *left, *right;
11216 test = ltrue_expr(state, read_expr(state, def));
11217 eat(state, TOK_QUEST);
11218 left = read_expr(state, expr(state));
11219 eat(state, TOK_COLON);
11220 right = read_expr(state, conditional_expr(state));
11222 def = mkcond_expr(state, test, left, right);
11228 struct triple *val;
11232 static void set_cv(struct compile_state *state, struct cv_triple *cv,
11233 struct triple *dest, struct triple *val)
11235 if (cv[dest->id].val) {
11236 free_triple(state, cv[dest->id].val);
11238 cv[dest->id].val = val;
11240 static struct triple *get_cv(struct compile_state *state, struct cv_triple *cv,
11241 struct triple *src)
11243 return cv[src->id].val;
11246 static struct triple *eval_const_expr(
11247 struct compile_state *state, struct triple *expr)
11249 struct triple *def;
11250 if (is_const(expr)) {
11254 /* If we don't start out as a constant simplify into one */
11255 struct triple *head, *ptr;
11256 struct cv_triple *cv;
11258 head = label(state); /* dummy initial triple */
11259 flatten(state, head, expr);
11261 for(ptr = head->next; ptr != head; ptr = ptr->next) {
11264 cv = xcmalloc(sizeof(struct cv_triple)*count, "const value vector");
11266 for(ptr = head->next; ptr != head; ptr = ptr->next) {
11268 cv[i].id = ptr->id;
11274 valid_ins(state, ptr);
11275 if ((ptr->op == OP_PHI) || (ptr->op == OP_LIST)) {
11276 internal_error(state, ptr,
11277 "unexpected %s in constant expression",
11280 else if (ptr->op == OP_LIST) {
11282 else if (triple_is_structural(state, ptr)) {
11285 else if (triple_is_ubranch(state, ptr)) {
11286 ptr = TARG(ptr, 0);
11288 else if (triple_is_cbranch(state, ptr)) {
11289 struct triple *cond_val;
11290 cond_val = get_cv(state, cv, RHS(ptr, 0));
11291 if (!cond_val || !is_const(cond_val) ||
11292 (cond_val->op != OP_INTCONST))
11294 internal_error(state, ptr, "bad branch condition");
11296 if (cond_val->u.cval == 0) {
11299 ptr = TARG(ptr, 0);
11302 else if (triple_is_branch(state, ptr)) {
11303 error(state, ptr, "bad branch type in constant expression");
11305 else if (ptr->op == OP_WRITE) {
11306 struct triple *val;
11307 val = get_cv(state, cv, RHS(ptr, 0));
11309 set_cv(state, cv, MISC(ptr, 0),
11310 copy_triple(state, val));
11311 set_cv(state, cv, ptr,
11312 copy_triple(state, val));
11315 else if (ptr->op == OP_READ) {
11316 set_cv(state, cv, ptr,
11318 get_cv(state, cv, RHS(ptr, 0))));
11321 else if (triple_is_pure(state, ptr, cv[ptr->id].id)) {
11322 struct triple *val, **rhs;
11323 val = copy_triple(state, ptr);
11324 rhs = triple_rhs(state, val, 0);
11325 for(; rhs; rhs = triple_rhs(state, val, rhs)) {
11327 internal_error(state, ptr, "Missing rhs");
11329 *rhs = get_cv(state, cv, *rhs);
11331 simplify(state, val);
11332 set_cv(state, cv, ptr, val);
11336 error(state, ptr, "impure operation in constant expression");
11339 } while(ptr != head);
11341 /* Get the result value */
11342 def = get_cv(state, cv, head->prev);
11343 cv[head->prev->id].val = 0;
11345 /* Free the temporary values */
11346 for(i = 0; i < count; i++) {
11348 free_triple(state, cv[i].val);
11353 /* Free the intermediate expressions */
11354 while(head->next != head) {
11355 release_triple(state, head->next);
11357 free_triple(state, head);
11359 if (!is_const(def)) {
11360 error(state, expr, "Not a constant expression");
11365 static struct triple *constant_expr(struct compile_state *state)
11367 return eval_const_expr(state, conditional_expr(state));
11370 static struct triple *assignment_expr(struct compile_state *state)
11372 struct triple *def, *left, *right;
11374 /* The C grammer in K&R shows assignment expressions
11375 * only taking unary expressions as input on their
11376 * left hand side. But specifies the precedence of
11377 * assignemnt as the lowest operator except for comma.
11379 * Allowing conditional expressions on the left hand side
11380 * of an assignement results in a grammar that accepts
11381 * a larger set of statements than standard C. As long
11382 * as the subset of the grammar that is standard C behaves
11383 * correctly this should cause no problems.
11385 * For the extra token strings accepted by the grammar
11386 * none of them should produce a valid lvalue, so they
11387 * should not produce functioning programs.
11389 * GCC has this bug as well, so surprises should be minimal.
11391 def = conditional_expr(state);
11393 switch((tok = peek(state))) {
11395 lvalue(state, left);
11396 eat(state, TOK_EQ);
11397 def = write_expr(state, left,
11398 read_expr(state, assignment_expr(state)));
11403 lvalue(state, left);
11404 arithmetic(state, left);
11406 right = read_expr(state, assignment_expr(state));
11407 arithmetic(state, right);
11409 sign = is_signed(left->type);
11412 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
11413 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
11414 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
11416 def = write_expr(state, left,
11417 triple(state, op, left->type,
11418 read_expr(state, left), right));
11421 lvalue(state, left);
11422 eat(state, TOK_PLUSEQ);
11423 def = write_expr(state, left,
11424 mk_add_expr(state, left, assignment_expr(state)));
11427 lvalue(state, left);
11428 eat(state, TOK_MINUSEQ);
11429 def = write_expr(state, left,
11430 mk_sub_expr(state, left, assignment_expr(state)));
11437 lvalue(state, left);
11438 integral(state, left);
11440 right = read_expr(state, assignment_expr(state));
11441 integral(state, right);
11442 right = integral_promotion(state, right);
11443 sign = is_signed(left->type);
11446 case TOK_SLEQ: op = OP_SL; break;
11447 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
11448 case TOK_ANDEQ: op = OP_AND; break;
11449 case TOK_XOREQ: op = OP_XOR; break;
11450 case TOK_OREQ: op = OP_OR; break;
11452 def = write_expr(state, left,
11453 triple(state, op, left->type,
11454 read_expr(state, left), right));
11460 static struct triple *expr(struct compile_state *state)
11462 struct triple *def;
11463 def = assignment_expr(state);
11464 while(peek(state) == TOK_COMMA) {
11465 eat(state, TOK_COMMA);
11466 def = mkprog(state, def, assignment_expr(state), 0);
11471 static void expr_statement(struct compile_state *state, struct triple *first)
11473 if (peek(state) != TOK_SEMI) {
11474 /* lvalue conversions always apply except when certian operators
11475 * are applied. I apply the lvalue conversions here
11476 * as I know no more operators will be applied.
11478 flatten(state, first, lvalue_conversion(state, expr(state)));
11480 eat(state, TOK_SEMI);
11483 static void if_statement(struct compile_state *state, struct triple *first)
11485 struct triple *test, *jmp1, *jmp2, *middle, *end;
11487 jmp1 = jmp2 = middle = 0;
11488 eat(state, TOK_IF);
11489 eat(state, TOK_LPAREN);
11490 test = expr(state);
11492 /* Cleanup and invert the test */
11493 test = lfalse_expr(state, read_expr(state, test));
11494 eat(state, TOK_RPAREN);
11495 /* Generate the needed pieces */
11496 middle = label(state);
11497 jmp1 = branch(state, middle, test);
11498 /* Thread the pieces together */
11499 flatten(state, first, test);
11500 flatten(state, first, jmp1);
11501 flatten(state, first, label(state));
11502 statement(state, first);
11503 if (peek(state) == TOK_ELSE) {
11504 eat(state, TOK_ELSE);
11505 /* Generate the rest of the pieces */
11506 end = label(state);
11507 jmp2 = branch(state, end, 0);
11508 /* Thread them together */
11509 flatten(state, first, jmp2);
11510 flatten(state, first, middle);
11511 statement(state, first);
11512 flatten(state, first, end);
11515 flatten(state, first, middle);
11519 static void for_statement(struct compile_state *state, struct triple *first)
11521 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
11522 struct triple *label1, *label2, *label3;
11523 struct hash_entry *ident;
11525 eat(state, TOK_FOR);
11526 eat(state, TOK_LPAREN);
11527 head = test = tail = jmp1 = jmp2 = 0;
11528 if (peek(state) != TOK_SEMI) {
11529 head = expr(state);
11531 eat(state, TOK_SEMI);
11532 if (peek(state) != TOK_SEMI) {
11533 test = expr(state);
11535 test = ltrue_expr(state, read_expr(state, test));
11537 eat(state, TOK_SEMI);
11538 if (peek(state) != TOK_RPAREN) {
11539 tail = expr(state);
11541 eat(state, TOK_RPAREN);
11542 /* Generate the needed pieces */
11543 label1 = label(state);
11544 label2 = label(state);
11545 label3 = label(state);
11547 jmp1 = branch(state, label3, 0);
11548 jmp2 = branch(state, label1, test);
11551 jmp2 = branch(state, label1, 0);
11553 end = label(state);
11554 /* Remember where break and continue go */
11555 start_scope(state);
11556 ident = state->i_break;
11557 symbol(state, ident, &ident->sym_ident, end, end->type);
11558 ident = state->i_continue;
11559 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11560 /* Now include the body */
11561 flatten(state, first, head);
11562 flatten(state, first, jmp1);
11563 flatten(state, first, label1);
11564 statement(state, first);
11565 flatten(state, first, label2);
11566 flatten(state, first, tail);
11567 flatten(state, first, label3);
11568 flatten(state, first, test);
11569 flatten(state, first, jmp2);
11570 flatten(state, first, end);
11571 /* Cleanup the break/continue scope */
11575 static void while_statement(struct compile_state *state, struct triple *first)
11577 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
11578 struct hash_entry *ident;
11579 eat(state, TOK_WHILE);
11580 eat(state, TOK_LPAREN);
11581 test = expr(state);
11583 test = ltrue_expr(state, read_expr(state, test));
11584 eat(state, TOK_RPAREN);
11585 /* Generate the needed pieces */
11586 label1 = label(state);
11587 label2 = label(state);
11588 jmp1 = branch(state, label2, 0);
11589 jmp2 = branch(state, label1, test);
11590 end = label(state);
11591 /* Remember where break and continue go */
11592 start_scope(state);
11593 ident = state->i_break;
11594 symbol(state, ident, &ident->sym_ident, end, end->type);
11595 ident = state->i_continue;
11596 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11597 /* Thread them together */
11598 flatten(state, first, jmp1);
11599 flatten(state, first, label1);
11600 statement(state, first);
11601 flatten(state, first, label2);
11602 flatten(state, first, test);
11603 flatten(state, first, jmp2);
11604 flatten(state, first, end);
11605 /* Cleanup the break/continue scope */
11609 static void do_statement(struct compile_state *state, struct triple *first)
11611 struct triple *label1, *label2, *test, *end;
11612 struct hash_entry *ident;
11613 eat(state, TOK_DO);
11614 /* Generate the needed pieces */
11615 label1 = label(state);
11616 label2 = label(state);
11617 end = label(state);
11618 /* Remember where break and continue go */
11619 start_scope(state);
11620 ident = state->i_break;
11621 symbol(state, ident, &ident->sym_ident, end, end->type);
11622 ident = state->i_continue;
11623 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11624 /* Now include the body */
11625 flatten(state, first, label1);
11626 statement(state, first);
11627 /* Cleanup the break/continue scope */
11629 /* Eat the rest of the loop */
11630 eat(state, TOK_WHILE);
11631 eat(state, TOK_LPAREN);
11632 test = read_expr(state, expr(state));
11634 eat(state, TOK_RPAREN);
11635 eat(state, TOK_SEMI);
11636 /* Thread the pieces together */
11637 test = ltrue_expr(state, test);
11638 flatten(state, first, label2);
11639 flatten(state, first, test);
11640 flatten(state, first, branch(state, label1, test));
11641 flatten(state, first, end);
11645 static void return_statement(struct compile_state *state, struct triple *first)
11647 struct triple *jmp, *mv, *dest, *var, *val;
11649 eat(state, TOK_RETURN);
11651 #warning "FIXME implement a more general excess branch elimination"
11653 /* If we have a return value do some more work */
11654 if (peek(state) != TOK_SEMI) {
11655 val = read_expr(state, expr(state));
11657 eat(state, TOK_SEMI);
11659 /* See if this last statement in a function */
11660 last = ((peek(state) == TOK_RBRACE) &&
11661 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
11663 /* Find the return variable */
11664 var = fresult(state, state->main_function);
11666 /* Find the return destination */
11667 dest = state->i_return->sym_ident->def;
11669 /* If needed generate a jump instruction */
11671 jmp = branch(state, dest, 0);
11673 /* If needed generate an assignment instruction */
11675 mv = write_expr(state, deref_index(state, var, 1), val);
11677 /* Now put the code together */
11679 flatten(state, first, mv);
11680 flatten(state, first, jmp);
11683 flatten(state, first, jmp);
11687 static void break_statement(struct compile_state *state, struct triple *first)
11689 struct triple *dest;
11690 eat(state, TOK_BREAK);
11691 eat(state, TOK_SEMI);
11692 if (!state->i_break->sym_ident) {
11693 error(state, 0, "break statement not within loop or switch");
11695 dest = state->i_break->sym_ident->def;
11696 flatten(state, first, branch(state, dest, 0));
11699 static void continue_statement(struct compile_state *state, struct triple *first)
11701 struct triple *dest;
11702 eat(state, TOK_CONTINUE);
11703 eat(state, TOK_SEMI);
11704 if (!state->i_continue->sym_ident) {
11705 error(state, 0, "continue statement outside of a loop");
11707 dest = state->i_continue->sym_ident->def;
11708 flatten(state, first, branch(state, dest, 0));
11711 static void goto_statement(struct compile_state *state, struct triple *first)
11713 struct hash_entry *ident;
11714 eat(state, TOK_GOTO);
11715 ident = eat(state, TOK_IDENT)->ident;
11716 if (!ident->sym_label) {
11717 /* If this is a forward branch allocate the label now,
11718 * it will be flattend in the appropriate location later.
11720 struct triple *ins;
11721 ins = label(state);
11722 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
11724 eat(state, TOK_SEMI);
11726 flatten(state, first, branch(state, ident->sym_label->def, 0));
11729 static void labeled_statement(struct compile_state *state, struct triple *first)
11731 struct triple *ins;
11732 struct hash_entry *ident;
11734 ident = eat(state, TOK_IDENT)->ident;
11735 if (ident->sym_label && ident->sym_label->def) {
11736 ins = ident->sym_label->def;
11737 put_occurance(ins->occurance);
11738 ins->occurance = new_occurance(state);
11741 ins = label(state);
11742 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
11744 if (ins->id & TRIPLE_FLAG_FLATTENED) {
11745 error(state, 0, "label %s already defined", ident->name);
11747 flatten(state, first, ins);
11749 eat(state, TOK_COLON);
11750 statement(state, first);
11753 static void switch_statement(struct compile_state *state, struct triple *first)
11755 struct triple *value, *top, *end, *dbranch;
11756 struct hash_entry *ident;
11758 /* See if we have a valid switch statement */
11759 eat(state, TOK_SWITCH);
11760 eat(state, TOK_LPAREN);
11761 value = expr(state);
11762 integral(state, value);
11763 value = read_expr(state, value);
11764 eat(state, TOK_RPAREN);
11765 /* Generate the needed pieces */
11766 top = label(state);
11767 end = label(state);
11768 dbranch = branch(state, end, 0);
11769 /* Remember where case branches and break goes */
11770 start_scope(state);
11771 ident = state->i_switch;
11772 symbol(state, ident, &ident->sym_ident, value, value->type);
11773 ident = state->i_case;
11774 symbol(state, ident, &ident->sym_ident, top, top->type);
11775 ident = state->i_break;
11776 symbol(state, ident, &ident->sym_ident, end, end->type);
11777 ident = state->i_default;
11778 symbol(state, ident, &ident->sym_ident, dbranch, dbranch->type);
11779 /* Thread them together */
11780 flatten(state, first, value);
11781 flatten(state, first, top);
11782 flatten(state, first, dbranch);
11783 statement(state, first);
11784 flatten(state, first, end);
11785 /* Cleanup the switch scope */
11789 static void case_statement(struct compile_state *state, struct triple *first)
11791 struct triple *cvalue, *dest, *test, *jmp;
11792 struct triple *ptr, *value, *top, *dbranch;
11794 /* See if w have a valid case statement */
11795 eat(state, TOK_CASE);
11796 cvalue = constant_expr(state);
11797 integral(state, cvalue);
11798 if (cvalue->op != OP_INTCONST) {
11799 error(state, 0, "integer constant expected");
11801 eat(state, TOK_COLON);
11802 if (!state->i_case->sym_ident) {
11803 error(state, 0, "case statement not within a switch");
11806 /* Lookup the interesting pieces */
11807 top = state->i_case->sym_ident->def;
11808 value = state->i_switch->sym_ident->def;
11809 dbranch = state->i_default->sym_ident->def;
11811 /* See if this case label has already been used */
11812 for(ptr = top; ptr != dbranch; ptr = ptr->next) {
11813 if (ptr->op != OP_EQ) {
11816 if (RHS(ptr, 1)->u.cval == cvalue->u.cval) {
11817 error(state, 0, "duplicate case %d statement",
11821 /* Generate the needed pieces */
11822 dest = label(state);
11823 test = triple(state, OP_EQ, &int_type, value, cvalue);
11824 jmp = branch(state, dest, test);
11825 /* Thread the pieces together */
11826 flatten(state, dbranch, test);
11827 flatten(state, dbranch, jmp);
11828 flatten(state, dbranch, label(state));
11829 flatten(state, first, dest);
11830 statement(state, first);
11833 static void default_statement(struct compile_state *state, struct triple *first)
11835 struct triple *dest;
11836 struct triple *dbranch, *end;
11838 /* See if we have a valid default statement */
11839 eat(state, TOK_DEFAULT);
11840 eat(state, TOK_COLON);
11842 if (!state->i_case->sym_ident) {
11843 error(state, 0, "default statement not within a switch");
11846 /* Lookup the interesting pieces */
11847 dbranch = state->i_default->sym_ident->def;
11848 end = state->i_break->sym_ident->def;
11850 /* See if a default statement has already happened */
11851 if (TARG(dbranch, 0) != end) {
11852 error(state, 0, "duplicate default statement");
11855 /* Generate the needed pieces */
11856 dest = label(state);
11858 /* Blame the branch on the default statement */
11859 put_occurance(dbranch->occurance);
11860 dbranch->occurance = new_occurance(state);
11862 /* Thread the pieces together */
11863 TARG(dbranch, 0) = dest;
11864 use_triple(dest, dbranch);
11865 flatten(state, first, dest);
11866 statement(state, first);
11869 static void asm_statement(struct compile_state *state, struct triple *first)
11871 struct asm_info *info;
11873 struct triple *constraint;
11874 struct triple *expr;
11875 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
11876 struct triple *def, *asm_str;
11877 int out, in, clobbers, more, colons, i;
11881 eat(state, TOK_ASM);
11882 /* For now ignore the qualifiers */
11883 switch(peek(state)) {
11885 eat(state, TOK_CONST);
11888 eat(state, TOK_VOLATILE);
11889 flags |= TRIPLE_FLAG_VOLATILE;
11892 eat(state, TOK_LPAREN);
11893 asm_str = string_constant(state);
11896 out = in = clobbers = 0;
11898 if ((colons == 0) && (peek(state) == TOK_COLON)) {
11899 eat(state, TOK_COLON);
11901 more = (peek(state) == TOK_LIT_STRING);
11903 struct triple *var;
11904 struct triple *constraint;
11907 if (out > MAX_LHS) {
11908 error(state, 0, "Maximum output count exceeded.");
11910 constraint = string_constant(state);
11911 str = constraint->u.blob;
11912 if (str[0] != '=') {
11913 error(state, 0, "Output constraint does not start with =");
11915 constraint->u.blob = str + 1;
11916 eat(state, TOK_LPAREN);
11917 var = conditional_expr(state);
11918 eat(state, TOK_RPAREN);
11920 lvalue(state, var);
11921 out_param[out].constraint = constraint;
11922 out_param[out].expr = var;
11923 if (peek(state) == TOK_COMMA) {
11924 eat(state, TOK_COMMA);
11931 if ((colons == 1) && (peek(state) == TOK_COLON)) {
11932 eat(state, TOK_COLON);
11934 more = (peek(state) == TOK_LIT_STRING);
11936 struct triple *val;
11937 struct triple *constraint;
11940 if (in > MAX_RHS) {
11941 error(state, 0, "Maximum input count exceeded.");
11943 constraint = string_constant(state);
11944 str = constraint->u.blob;
11945 if (digitp(str[0] && str[1] == '\0')) {
11947 val = digval(str[0]);
11948 if ((val < 0) || (val >= out)) {
11949 error(state, 0, "Invalid input constraint %d", val);
11952 eat(state, TOK_LPAREN);
11953 val = conditional_expr(state);
11954 eat(state, TOK_RPAREN);
11956 in_param[in].constraint = constraint;
11957 in_param[in].expr = val;
11958 if (peek(state) == TOK_COMMA) {
11959 eat(state, TOK_COMMA);
11967 if ((colons == 2) && (peek(state) == TOK_COLON)) {
11968 eat(state, TOK_COLON);
11970 more = (peek(state) == TOK_LIT_STRING);
11972 struct triple *clobber;
11974 if ((clobbers + out) > MAX_LHS) {
11975 error(state, 0, "Maximum clobber limit exceeded.");
11977 clobber = string_constant(state);
11979 clob_param[clobbers].constraint = clobber;
11980 if (peek(state) == TOK_COMMA) {
11981 eat(state, TOK_COMMA);
11987 eat(state, TOK_RPAREN);
11988 eat(state, TOK_SEMI);
11991 info = xcmalloc(sizeof(*info), "asm_info");
11992 info->str = asm_str->u.blob;
11993 free_triple(state, asm_str);
11995 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
11996 def->u.ainfo = info;
11999 /* Find the register constraints */
12000 for(i = 0; i < out; i++) {
12001 struct triple *constraint;
12002 constraint = out_param[i].constraint;
12003 info->tmpl.lhs[i] = arch_reg_constraint(state,
12004 out_param[i].expr->type, constraint->u.blob);
12005 free_triple(state, constraint);
12007 for(; i - out < clobbers; i++) {
12008 struct triple *constraint;
12009 constraint = clob_param[i - out].constraint;
12010 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
12011 free_triple(state, constraint);
12013 for(i = 0; i < in; i++) {
12014 struct triple *constraint;
12016 constraint = in_param[i].constraint;
12017 str = constraint->u.blob;
12018 if (digitp(str[0]) && str[1] == '\0') {
12019 struct reg_info cinfo;
12021 val = digval(str[0]);
12022 cinfo.reg = info->tmpl.lhs[val].reg;
12023 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
12024 cinfo.regcm &= info->tmpl.lhs[val].regcm;
12025 if (cinfo.reg == REG_UNSET) {
12026 cinfo.reg = REG_VIRT0 + val;
12028 if (cinfo.regcm == 0) {
12029 error(state, 0, "No registers for %d", val);
12031 info->tmpl.lhs[val] = cinfo;
12032 info->tmpl.rhs[i] = cinfo;
12035 info->tmpl.rhs[i] = arch_reg_constraint(state,
12036 in_param[i].expr->type, str);
12038 free_triple(state, constraint);
12041 /* Now build the helper expressions */
12042 for(i = 0; i < in; i++) {
12043 RHS(def, i) = read_expr(state, in_param[i].expr);
12045 flatten(state, first, def);
12046 for(i = 0; i < (out + clobbers); i++) {
12048 struct triple *piece;
12050 type = out_param[i].expr->type;
12052 size_t size = arch_reg_size(info->tmpl.lhs[i].reg);
12053 if (size >= SIZEOF_LONG) {
12054 type = &ulong_type;
12056 else if (size >= SIZEOF_INT) {
12059 else if (size >= SIZEOF_SHORT) {
12060 type = &ushort_type;
12063 type = &uchar_type;
12066 piece = triple(state, OP_PIECE, type, def, 0);
12068 LHS(def, i) = piece;
12069 flatten(state, first, piece);
12071 /* And write the helpers to their destinations */
12072 for(i = 0; i < out; i++) {
12073 struct triple *piece;
12074 piece = LHS(def, i);
12075 flatten(state, first,
12076 write_expr(state, out_param[i].expr, piece));
12081 static int isdecl(int tok)
12104 case TOK_TYPE_NAME: /* typedef name */
12111 static void compound_statement(struct compile_state *state, struct triple *first)
12113 eat(state, TOK_LBRACE);
12114 start_scope(state);
12116 /* statement-list opt */
12117 while (peek(state) != TOK_RBRACE) {
12118 statement(state, first);
12121 eat(state, TOK_RBRACE);
12124 static void statement(struct compile_state *state, struct triple *first)
12128 if (tok == TOK_LBRACE) {
12129 compound_statement(state, first);
12131 else if (tok == TOK_IF) {
12132 if_statement(state, first);
12134 else if (tok == TOK_FOR) {
12135 for_statement(state, first);
12137 else if (tok == TOK_WHILE) {
12138 while_statement(state, first);
12140 else if (tok == TOK_DO) {
12141 do_statement(state, first);
12143 else if (tok == TOK_RETURN) {
12144 return_statement(state, first);
12146 else if (tok == TOK_BREAK) {
12147 break_statement(state, first);
12149 else if (tok == TOK_CONTINUE) {
12150 continue_statement(state, first);
12152 else if (tok == TOK_GOTO) {
12153 goto_statement(state, first);
12155 else if (tok == TOK_SWITCH) {
12156 switch_statement(state, first);
12158 else if (tok == TOK_ASM) {
12159 asm_statement(state, first);
12161 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
12162 labeled_statement(state, first);
12164 else if (tok == TOK_CASE) {
12165 case_statement(state, first);
12167 else if (tok == TOK_DEFAULT) {
12168 default_statement(state, first);
12170 else if (isdecl(tok)) {
12171 /* This handles C99 intermixing of statements and decls */
12172 decl(state, first);
12175 expr_statement(state, first);
12179 static struct type *param_decl(struct compile_state *state)
12182 struct hash_entry *ident;
12183 /* Cheat so the declarator will know we are not global */
12184 start_scope(state);
12186 type = decl_specifiers(state);
12187 type = declarator(state, type, &ident, 0);
12188 type->field_ident = ident;
12193 static struct type *param_type_list(struct compile_state *state, struct type *type)
12195 struct type *ftype, **next;
12196 ftype = new_type(TYPE_FUNCTION | (type->type & STOR_MASK), type, param_decl(state));
12197 next = &ftype->right;
12198 ftype->elements = 1;
12199 while(peek(state) == TOK_COMMA) {
12200 eat(state, TOK_COMMA);
12201 if (peek(state) == TOK_DOTS) {
12202 eat(state, TOK_DOTS);
12203 error(state, 0, "variadic functions not supported");
12206 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
12207 next = &((*next)->right);
12214 static struct type *type_name(struct compile_state *state)
12217 type = specifier_qualifier_list(state);
12218 /* abstract-declarator (may consume no tokens) */
12219 type = declarator(state, type, 0, 0);
12223 static struct type *direct_declarator(
12224 struct compile_state *state, struct type *type,
12225 struct hash_entry **pident, int need_ident)
12227 struct hash_entry *ident;
12228 struct type *outer;
12231 arrays_complete(state, type);
12232 switch(peek(state)) {
12234 ident = eat(state, TOK_IDENT)->ident;
12236 error(state, 0, "Unexpected identifier found");
12238 /* The name of what we are declaring */
12242 eat(state, TOK_LPAREN);
12243 outer = declarator(state, type, pident, need_ident);
12244 eat(state, TOK_RPAREN);
12248 error(state, 0, "Identifier expected");
12254 arrays_complete(state, type);
12255 switch(peek(state)) {
12257 eat(state, TOK_LPAREN);
12258 type = param_type_list(state, type);
12259 eat(state, TOK_RPAREN);
12263 unsigned int qualifiers;
12264 struct triple *value;
12266 eat(state, TOK_LBRACKET);
12267 if (peek(state) != TOK_RBRACKET) {
12268 value = constant_expr(state);
12269 integral(state, value);
12271 eat(state, TOK_RBRACKET);
12273 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
12274 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
12276 type->elements = value->u.cval;
12277 free_triple(state, value);
12279 type->elements = ELEMENT_COUNT_UNSPECIFIED;
12290 struct type *inner;
12291 arrays_complete(state, type);
12293 for(inner = outer; inner->left; inner = inner->left)
12295 inner->left = type;
12301 static struct type *declarator(
12302 struct compile_state *state, struct type *type,
12303 struct hash_entry **pident, int need_ident)
12305 while(peek(state) == TOK_STAR) {
12306 eat(state, TOK_STAR);
12307 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
12309 type = direct_declarator(state, type, pident, need_ident);
12313 static struct type *typedef_name(
12314 struct compile_state *state, unsigned int specifiers)
12316 struct hash_entry *ident;
12318 ident = eat(state, TOK_TYPE_NAME)->ident;
12319 type = ident->sym_ident->type;
12320 specifiers |= type->type & QUAL_MASK;
12321 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
12322 (type->type & (STOR_MASK | QUAL_MASK))) {
12323 type = clone_type(specifiers, type);
12328 static struct type *enum_specifier(
12329 struct compile_state *state, unsigned int spec)
12331 struct hash_entry *ident;
12334 struct type *enum_type;
12337 eat(state, TOK_ENUM);
12339 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
12340 ident = eat(state, tok)->ident;
12343 if (!ident || (peek(state) == TOK_LBRACE)) {
12344 struct type **next;
12345 eat(state, TOK_LBRACE);
12346 enum_type = new_type(TYPE_ENUM | spec, 0, 0);
12347 enum_type->type_ident = ident;
12348 next = &enum_type->right;
12350 struct hash_entry *eident;
12351 struct triple *value;
12352 struct type *entry;
12353 eident = eat(state, TOK_IDENT)->ident;
12354 if (eident->sym_ident) {
12355 error(state, 0, "%s already declared",
12358 eident->tok = TOK_ENUM_CONST;
12359 if (peek(state) == TOK_EQ) {
12360 struct triple *val;
12361 eat(state, TOK_EQ);
12362 val = constant_expr(state);
12363 integral(state, val);
12364 base = val->u.cval;
12366 value = int_const(state, &int_type, base);
12367 symbol(state, eident, &eident->sym_ident, value, &int_type);
12368 entry = new_type(TYPE_LIST, 0, 0);
12369 entry->field_ident = eident;
12371 next = &entry->right;
12373 if (peek(state) == TOK_COMMA) {
12374 eat(state, TOK_COMMA);
12376 } while(peek(state) != TOK_RBRACE);
12377 eat(state, TOK_RBRACE);
12379 symbol(state, ident, &ident->sym_tag, 0, enum_type);
12382 if (ident && ident->sym_tag &&
12383 ident->sym_tag->type &&
12384 ((ident->sym_tag->type->type & TYPE_MASK) == TYPE_ENUM)) {
12385 enum_type = clone_type(spec, ident->sym_tag->type);
12387 else if (ident && !enum_type) {
12388 error(state, 0, "enum %s undeclared", ident->name);
12393 static struct type *struct_declarator(
12394 struct compile_state *state, struct type *type, struct hash_entry **ident)
12396 if (peek(state) != TOK_COLON) {
12397 type = declarator(state, type, ident, 1);
12399 if (peek(state) == TOK_COLON) {
12400 struct triple *value;
12401 eat(state, TOK_COLON);
12402 value = constant_expr(state);
12403 if (value->op != OP_INTCONST) {
12404 error(state, 0, "Invalid constant expression");
12406 if (value->u.cval > size_of(state, type)) {
12407 error(state, 0, "bitfield larger than base type");
12409 if (!TYPE_INTEGER(type->type) || ((type->type & TYPE_MASK) == TYPE_BITFIELD)) {
12410 error(state, 0, "bitfield base not an integer type");
12412 type = new_type(TYPE_BITFIELD, type, 0);
12413 type->elements = value->u.cval;
12418 static struct type *struct_or_union_specifier(
12419 struct compile_state *state, unsigned int spec)
12421 struct type *struct_type;
12422 struct hash_entry *ident;
12423 unsigned int type_main;
12424 unsigned int type_join;
12428 switch(peek(state)) {
12430 eat(state, TOK_STRUCT);
12431 type_main = TYPE_STRUCT;
12432 type_join = TYPE_PRODUCT;
12435 eat(state, TOK_UNION);
12436 type_main = TYPE_UNION;
12437 type_join = TYPE_OVERLAP;
12440 eat(state, TOK_STRUCT);
12441 type_main = TYPE_STRUCT;
12442 type_join = TYPE_PRODUCT;
12446 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
12447 ident = eat(state, tok)->ident;
12449 if (!ident || (peek(state) == TOK_LBRACE)) {
12451 struct type **next;
12453 eat(state, TOK_LBRACE);
12454 next = &struct_type;
12456 struct type *base_type;
12458 base_type = specifier_qualifier_list(state);
12461 struct hash_entry *fident;
12463 type = struct_declarator(state, base_type, &fident);
12465 if (peek(state) == TOK_COMMA) {
12467 eat(state, TOK_COMMA);
12469 type = clone_type(0, type);
12470 type->field_ident = fident;
12472 *next = new_type(type_join, *next, type);
12473 next = &((*next)->right);
12478 eat(state, TOK_SEMI);
12479 } while(peek(state) != TOK_RBRACE);
12480 eat(state, TOK_RBRACE);
12481 struct_type = new_type(type_main | spec, struct_type, 0);
12482 struct_type->type_ident = ident;
12483 struct_type->elements = elements;
12485 symbol(state, ident, &ident->sym_tag, 0, struct_type);
12488 if (ident && ident->sym_tag &&
12489 ident->sym_tag->type &&
12490 ((ident->sym_tag->type->type & TYPE_MASK) == type_main)) {
12491 struct_type = clone_type(spec, ident->sym_tag->type);
12493 else if (ident && !struct_type) {
12494 error(state, 0, "%s %s undeclared",
12495 (type_main == TYPE_STRUCT)?"struct" : "union",
12498 return struct_type;
12501 static unsigned int storage_class_specifier_opt(struct compile_state *state)
12503 unsigned int specifiers;
12504 switch(peek(state)) {
12506 eat(state, TOK_AUTO);
12507 specifiers = STOR_AUTO;
12510 eat(state, TOK_REGISTER);
12511 specifiers = STOR_REGISTER;
12514 eat(state, TOK_STATIC);
12515 specifiers = STOR_STATIC;
12518 eat(state, TOK_EXTERN);
12519 specifiers = STOR_EXTERN;
12522 eat(state, TOK_TYPEDEF);
12523 specifiers = STOR_TYPEDEF;
12526 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
12527 specifiers = STOR_LOCAL;
12530 specifiers = STOR_AUTO;
12536 static unsigned int function_specifier_opt(struct compile_state *state)
12538 /* Ignore the inline keyword */
12539 unsigned int specifiers;
12541 switch(peek(state)) {
12543 eat(state, TOK_INLINE);
12544 specifiers = STOR_INLINE;
12549 static unsigned int attrib(struct compile_state *state, unsigned int attributes)
12551 int tok = peek(state);
12555 /* The empty attribute ignore it */
12558 case TOK_ENUM_CONST:
12559 case TOK_TYPE_NAME:
12561 struct hash_entry *ident;
12562 ident = eat(state, TOK_IDENT)->ident;
12564 if (ident == state->i_noinline) {
12565 if (attributes & ATTRIB_ALWAYS_INLINE) {
12566 error(state, 0, "both always_inline and noinline attribtes");
12568 attributes |= ATTRIB_NOINLINE;
12570 else if (ident == state->i_always_inline) {
12571 if (attributes & ATTRIB_NOINLINE) {
12572 error(state, 0, "both noinline and always_inline attribtes");
12574 attributes |= ATTRIB_ALWAYS_INLINE;
12577 error(state, 0, "Unknown attribute:%s", ident->name);
12582 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
12588 static unsigned int attribute_list(struct compile_state *state, unsigned type)
12590 type = attrib(state, type);
12591 while(peek(state) == TOK_COMMA) {
12592 eat(state, TOK_COMMA);
12593 type = attrib(state, type);
12598 static unsigned int attributes_opt(struct compile_state *state, unsigned type)
12600 if (peek(state) == TOK_ATTRIBUTE) {
12601 eat(state, TOK_ATTRIBUTE);
12602 eat(state, TOK_LPAREN);
12603 eat(state, TOK_LPAREN);
12604 type = attribute_list(state, type);
12605 eat(state, TOK_RPAREN);
12606 eat(state, TOK_RPAREN);
12611 static unsigned int type_qualifiers(struct compile_state *state)
12613 unsigned int specifiers;
12616 specifiers = QUAL_NONE;
12618 switch(peek(state)) {
12620 eat(state, TOK_CONST);
12621 specifiers |= QUAL_CONST;
12624 eat(state, TOK_VOLATILE);
12625 specifiers |= QUAL_VOLATILE;
12628 eat(state, TOK_RESTRICT);
12629 specifiers |= QUAL_RESTRICT;
12639 static struct type *type_specifier(
12640 struct compile_state *state, unsigned int spec)
12645 switch((tok = peek(state))) {
12647 eat(state, TOK_VOID);
12648 type = new_type(TYPE_VOID | spec, 0, 0);
12651 eat(state, TOK_CHAR);
12652 type = new_type(TYPE_CHAR | spec, 0, 0);
12655 eat(state, TOK_SHORT);
12656 if (peek(state) == TOK_INT) {
12657 eat(state, TOK_INT);
12659 type = new_type(TYPE_SHORT | spec, 0, 0);
12662 eat(state, TOK_INT);
12663 type = new_type(TYPE_INT | spec, 0, 0);
12666 eat(state, TOK_LONG);
12667 switch(peek(state)) {
12669 eat(state, TOK_LONG);
12670 error(state, 0, "long long not supported");
12673 eat(state, TOK_DOUBLE);
12674 error(state, 0, "long double not supported");
12677 eat(state, TOK_INT);
12678 type = new_type(TYPE_LONG | spec, 0, 0);
12681 type = new_type(TYPE_LONG | spec, 0, 0);
12686 eat(state, TOK_FLOAT);
12687 error(state, 0, "type float not supported");
12690 eat(state, TOK_DOUBLE);
12691 error(state, 0, "type double not supported");
12694 eat(state, TOK_SIGNED);
12695 switch(peek(state)) {
12697 eat(state, TOK_LONG);
12698 switch(peek(state)) {
12700 eat(state, TOK_LONG);
12701 error(state, 0, "type long long not supported");
12704 eat(state, TOK_INT);
12705 type = new_type(TYPE_LONG | spec, 0, 0);
12708 type = new_type(TYPE_LONG | spec, 0, 0);
12713 eat(state, TOK_INT);
12714 type = new_type(TYPE_INT | spec, 0, 0);
12717 eat(state, TOK_SHORT);
12718 type = new_type(TYPE_SHORT | spec, 0, 0);
12721 eat(state, TOK_CHAR);
12722 type = new_type(TYPE_CHAR | spec, 0, 0);
12725 type = new_type(TYPE_INT | spec, 0, 0);
12730 eat(state, TOK_UNSIGNED);
12731 switch(peek(state)) {
12733 eat(state, TOK_LONG);
12734 switch(peek(state)) {
12736 eat(state, TOK_LONG);
12737 error(state, 0, "unsigned long long not supported");
12740 eat(state, TOK_INT);
12741 type = new_type(TYPE_ULONG | spec, 0, 0);
12744 type = new_type(TYPE_ULONG | spec, 0, 0);
12749 eat(state, TOK_INT);
12750 type = new_type(TYPE_UINT | spec, 0, 0);
12753 eat(state, TOK_SHORT);
12754 type = new_type(TYPE_USHORT | spec, 0, 0);
12757 eat(state, TOK_CHAR);
12758 type = new_type(TYPE_UCHAR | spec, 0, 0);
12761 type = new_type(TYPE_UINT | spec, 0, 0);
12765 /* struct or union specifier */
12768 type = struct_or_union_specifier(state, spec);
12770 /* enum-spefifier */
12772 type = enum_specifier(state, spec);
12775 case TOK_TYPE_NAME:
12776 type = typedef_name(state, spec);
12779 error(state, 0, "bad type specifier %s",
12786 static int istype(int tok)
12804 case TOK_TYPE_NAME:
12812 static struct type *specifier_qualifier_list(struct compile_state *state)
12815 unsigned int specifiers = 0;
12817 /* type qualifiers */
12818 specifiers |= type_qualifiers(state);
12820 /* type specifier */
12821 type = type_specifier(state, specifiers);
12826 static int isdecl_specifier(int tok)
12829 /* storage class specifier */
12835 /* type qualifier */
12839 /* type specifiers */
12849 /* struct or union specifier */
12852 /* enum-spefifier */
12855 case TOK_TYPE_NAME:
12856 /* function specifiers */
12864 static struct type *decl_specifiers(struct compile_state *state)
12867 unsigned int specifiers;
12868 /* I am overly restrictive in the arragement of specifiers supported.
12869 * C is overly flexible in this department it makes interpreting
12870 * the parse tree difficult.
12874 /* storage class specifier */
12875 specifiers |= storage_class_specifier_opt(state);
12877 /* function-specifier */
12878 specifiers |= function_specifier_opt(state);
12881 specifiers |= attributes_opt(state, 0);
12883 /* type qualifier */
12884 specifiers |= type_qualifiers(state);
12886 /* type specifier */
12887 type = type_specifier(state, specifiers);
12891 struct field_info {
12896 static struct field_info designator(struct compile_state *state, struct type *type)
12899 struct field_info info;
12903 switch(peek(state)) {
12906 struct triple *value;
12907 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
12908 error(state, 0, "Array designator not in array initializer");
12910 eat(state, TOK_LBRACKET);
12911 value = constant_expr(state);
12912 eat(state, TOK_RBRACKET);
12914 info.type = type->left;
12915 info.offset = value->u.cval * size_of(state, info.type);
12920 struct hash_entry *field;
12921 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
12922 ((type->type & TYPE_MASK) != TYPE_UNION))
12924 error(state, 0, "Struct designator not in struct initializer");
12926 eat(state, TOK_DOT);
12927 field = eat(state, TOK_IDENT)->ident;
12928 info.offset = field_offset(state, type, field);
12929 info.type = field_type(state, type, field);
12933 error(state, 0, "Invalid designator");
12936 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
12937 eat(state, TOK_EQ);
12941 static struct triple *initializer(
12942 struct compile_state *state, struct type *type)
12944 struct triple *result;
12945 #warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
12946 if (peek(state) != TOK_LBRACE) {
12947 result = assignment_expr(state);
12948 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
12949 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
12950 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
12951 (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
12952 (equiv_types(type->left, result->type->left))) {
12953 type->elements = result->type->elements;
12955 if (is_lvalue(state, result) &&
12956 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
12957 (type->type & TYPE_MASK) != TYPE_ARRAY)
12959 result = lvalue_conversion(state, result);
12961 if (!is_init_compatible(state, type, result->type)) {
12962 error(state, 0, "Incompatible types in initializer");
12964 if (!equiv_types(type, result->type)) {
12965 result = mk_cast_expr(state, type, result);
12971 struct field_info info;
12973 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
12974 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
12975 internal_error(state, 0, "unknown initializer type");
12978 info.type = type->left;
12979 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
12980 info.type = next_field(state, type, 0);
12982 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
12985 max_offset = size_of(state, type);
12987 buf = xcmalloc(bits_to_bytes(max_offset), "initializer");
12988 eat(state, TOK_LBRACE);
12990 struct triple *value;
12991 struct type *value_type;
12997 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
12998 info = designator(state, type);
13000 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13001 (info.offset >= max_offset)) {
13002 error(state, 0, "element beyond bounds");
13004 value_type = info.type;
13005 value = eval_const_expr(state, initializer(state, value_type));
13006 value_size = size_of(state, value_type);
13007 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13008 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13009 (max_offset <= info.offset)) {
13013 old_size = max_offset;
13014 max_offset = info.offset + value_size;
13015 buf = xmalloc(bits_to_bytes(max_offset), "initializer");
13016 memcpy(buf, old_buf, bits_to_bytes(old_size));
13019 dest = ((char *)buf) + bits_to_bytes(info.offset);
13020 #if DEBUG_INITIALIZER
13021 fprintf(state->errout, "dest = buf + %d max_offset: %d value_size: %d op: %d\n",
13023 bits_to_bytes(max_offset),
13024 bits_to_bytes(value_size),
13027 if (value->op == OP_BLOBCONST) {
13028 memcpy(dest, value->u.blob, bits_to_bytes(value_size));
13030 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I8)) {
13031 #if DEBUG_INITIALIZER
13032 fprintf(state->errout, "byte: %02x\n", value->u.cval & 0xff);
13034 *((uint8_t *)dest) = value->u.cval & 0xff;
13036 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I16)) {
13037 *((uint16_t *)dest) = value->u.cval & 0xffff;
13039 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I32)) {
13040 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
13043 internal_error(state, 0, "unhandled constant initializer");
13045 free_triple(state, value);
13046 if (peek(state) == TOK_COMMA) {
13047 eat(state, TOK_COMMA);
13050 info.offset += value_size;
13051 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13052 info.type = next_field(state, type, info.type);
13053 info.offset = field_offset(state, type,
13054 info.type->field_ident);
13056 } while(comma && (peek(state) != TOK_RBRACE));
13057 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13058 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
13059 type->elements = max_offset / size_of(state, type->left);
13061 eat(state, TOK_RBRACE);
13062 result = triple(state, OP_BLOBCONST, type, 0, 0);
13063 result->u.blob = buf;
13068 static void resolve_branches(struct compile_state *state, struct triple *first)
13070 /* Make a second pass and finish anything outstanding
13071 * with respect to branches. The only outstanding item
13072 * is to see if there are goto to labels that have not
13073 * been defined and to error about them.
13076 struct triple *ins;
13077 /* Also error on branches that do not use their targets */
13080 if (!triple_is_ret(state, ins)) {
13081 struct triple **expr ;
13082 struct triple_set *set;
13083 expr = triple_targ(state, ins, 0);
13084 for(; expr; expr = triple_targ(state, ins, expr)) {
13085 struct triple *targ;
13087 for(set = targ?targ->use:0; set; set = set->next) {
13088 if (set->member == ins) {
13093 internal_error(state, ins, "targ not used");
13098 } while(ins != first);
13099 /* See if there are goto to labels that have not been defined */
13100 for(i = 0; i < HASH_TABLE_SIZE; i++) {
13101 struct hash_entry *entry;
13102 for(entry = state->hash_table[i]; entry; entry = entry->next) {
13103 struct triple *ins;
13104 if (!entry->sym_label) {
13107 ins = entry->sym_label->def;
13108 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
13109 error(state, ins, "label `%s' used but not defined",
13116 static struct triple *function_definition(
13117 struct compile_state *state, struct type *type)
13119 struct triple *def, *tmp, *first, *end, *retvar, *result, *ret;
13120 struct triple *fname;
13121 struct type *fname_type;
13122 struct hash_entry *ident;
13123 struct type *param, *crtype, *ctype;
13125 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
13126 error(state, 0, "Invalid function header");
13129 /* Verify the function type */
13130 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
13131 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
13132 (type->right->field_ident == 0)) {
13133 error(state, 0, "Invalid function parameters");
13135 param = type->right;
13137 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
13139 if (!param->left->field_ident) {
13140 error(state, 0, "No identifier for parameter %d\n", i);
13142 param = param->right;
13145 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
13146 error(state, 0, "No identifier for paramter %d\n", i);
13149 /* Get a list of statements for this function. */
13150 def = triple(state, OP_LIST, type, 0, 0);
13152 /* Start a new scope for the passed parameters */
13153 start_scope(state);
13155 /* Put a label at the very start of a function */
13156 first = label(state);
13157 RHS(def, 0) = first;
13159 /* Put a label at the very end of a function */
13160 end = label(state);
13161 flatten(state, first, end);
13162 /* Remember where return goes */
13163 ident = state->i_return;
13164 symbol(state, ident, &ident->sym_ident, end, end->type);
13166 /* Get the initial closure type */
13167 ctype = new_type(TYPE_JOIN, &void_type, 0);
13168 ctype->elements = 1;
13170 /* Add a variable for the return value */
13171 crtype = new_type(TYPE_TUPLE,
13172 /* Remove all type qualifiers from the return type */
13173 new_type(TYPE_PRODUCT, ctype, clone_type(0, type->left)), 0);
13174 crtype->elements = 2;
13175 result = flatten(state, end, variable(state, crtype));
13177 /* Allocate a variable for the return address */
13178 retvar = flatten(state, end, variable(state, &void_ptr_type));
13180 /* Add in the return instruction */
13181 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
13182 ret = flatten(state, first, ret);
13184 /* Walk through the parameters and create symbol table entries
13187 param = type->right;
13188 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
13189 ident = param->left->field_ident;
13190 tmp = variable(state, param->left);
13191 var_symbol(state, ident, tmp);
13192 flatten(state, end, tmp);
13193 param = param->right;
13195 if ((param->type & TYPE_MASK) != TYPE_VOID) {
13196 /* And don't forget the last parameter */
13197 ident = param->field_ident;
13198 tmp = variable(state, param);
13199 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
13200 flatten(state, end, tmp);
13203 /* Add the declaration static const char __func__ [] = "func-name" */
13204 fname_type = new_type(TYPE_ARRAY,
13205 clone_type(QUAL_CONST | STOR_STATIC, &char_type), 0);
13206 fname_type->type |= QUAL_CONST | STOR_STATIC;
13207 fname_type->elements = strlen(state->function) + 1;
13209 fname = triple(state, OP_BLOBCONST, fname_type, 0, 0);
13210 fname->u.blob = (void *)state->function;
13211 fname = flatten(state, end, fname);
13213 ident = state->i___func__;
13214 symbol(state, ident, &ident->sym_ident, fname, fname_type);
13216 /* Remember which function I am compiling.
13217 * Also assume the last defined function is the main function.
13219 state->main_function = def;
13221 /* Now get the actual function definition */
13222 compound_statement(state, end);
13224 /* Finish anything unfinished with branches */
13225 resolve_branches(state, first);
13227 /* Remove the parameter scope */
13231 /* Remember I have defined a function */
13232 if (!state->functions) {
13233 state->functions = def;
13235 insert_triple(state, state->functions, def);
13237 if (state->compiler->debug & DEBUG_INLINE) {
13238 FILE *fp = state->dbgout;
13241 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13242 display_func(state, fp, def);
13243 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
13249 static struct triple *do_decl(struct compile_state *state,
13250 struct type *type, struct hash_entry *ident)
13252 struct triple *def;
13254 /* Clean up the storage types used */
13255 switch (type->type & STOR_MASK) {
13258 /* These are the good types I am aiming for */
13260 case STOR_REGISTER:
13261 type->type &= ~STOR_MASK;
13262 type->type |= STOR_AUTO;
13266 type->type &= ~STOR_MASK;
13267 type->type |= STOR_STATIC;
13271 error(state, 0, "typedef without name");
13273 symbol(state, ident, &ident->sym_ident, 0, type);
13274 ident->tok = TOK_TYPE_NAME;
13278 internal_error(state, 0, "Undefined storage class");
13280 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
13281 error(state, 0, "Function prototypes not supported");
13284 ((type->type & STOR_MASK) == STOR_STATIC) &&
13285 ((type->type & QUAL_CONST) == 0)) {
13286 error(state, 0, "non const static variables not supported");
13289 def = variable(state, type);
13290 var_symbol(state, ident, def);
13295 static void decl(struct compile_state *state, struct triple *first)
13297 struct type *base_type, *type;
13298 struct hash_entry *ident;
13299 struct triple *def;
13301 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
13302 base_type = decl_specifiers(state);
13304 type = declarator(state, base_type, &ident, 0);
13305 type->type = attributes_opt(state, type->type);
13306 if (global && ident && (peek(state) == TOK_LBRACE)) {
13308 type->type_ident = ident;
13309 state->function = ident->name;
13310 def = function_definition(state, type);
13311 symbol(state, ident, &ident->sym_ident, def, type);
13312 state->function = 0;
13316 flatten(state, first, do_decl(state, type, ident));
13317 /* type or variable definition */
13320 if (peek(state) == TOK_EQ) {
13322 error(state, 0, "cannot assign to a type");
13324 eat(state, TOK_EQ);
13325 flatten(state, first,
13327 ident->sym_ident->def,
13328 initializer(state, type)));
13330 arrays_complete(state, type);
13331 if (peek(state) == TOK_COMMA) {
13332 eat(state, TOK_COMMA);
13334 type = declarator(state, base_type, &ident, 0);
13335 flatten(state, first, do_decl(state, type, ident));
13339 eat(state, TOK_SEMI);
13343 static void decls(struct compile_state *state)
13345 struct triple *list;
13347 list = label(state);
13350 if (tok == TOK_EOF) {
13353 if (tok == TOK_SPACE) {
13354 eat(state, TOK_SPACE);
13357 if (list->next != list) {
13358 error(state, 0, "global variables not supported");
13364 * Function inlining
13366 struct triple_reg_set {
13367 struct triple_reg_set *next;
13368 struct triple *member;
13369 struct triple *new;
13372 struct block *block;
13373 struct triple_reg_set *in;
13374 struct triple_reg_set *out;
13377 static void setup_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13378 static void analyze_basic_blocks(struct compile_state *state, struct basic_blocks *bb);
13379 static void free_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13380 static int tdominates(struct compile_state *state, struct triple *dom, struct triple *sub);
13381 static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
13382 void (*cb)(struct compile_state *state, struct block *block, void *arg),
13384 static void print_block(
13385 struct compile_state *state, struct block *block, void *arg);
13386 static int do_triple_set(struct triple_reg_set **head,
13387 struct triple *member, struct triple *new_member);
13388 static void do_triple_unset(struct triple_reg_set **head, struct triple *member);
13389 static struct reg_block *compute_variable_lifetimes(
13390 struct compile_state *state, struct basic_blocks *bb);
13391 static void free_variable_lifetimes(struct compile_state *state,
13392 struct basic_blocks *bb, struct reg_block *blocks);
13393 static void print_live_variables(struct compile_state *state,
13394 struct basic_blocks *bb, struct reg_block *rb, FILE *fp);
13397 static struct triple *call(struct compile_state *state,
13398 struct triple *retvar, struct triple *ret_addr,
13399 struct triple *targ, struct triple *ret)
13401 struct triple *call;
13403 if (!retvar || !is_lvalue(state, retvar)) {
13404 internal_error(state, 0, "writing to a non lvalue?");
13406 write_compatible(state, retvar->type, &void_ptr_type);
13408 call = new_triple(state, OP_CALL, &void_type, 1, 0);
13409 TARG(call, 0) = targ;
13410 MISC(call, 0) = ret;
13411 if (!targ || (targ->op != OP_LABEL)) {
13412 internal_error(state, 0, "call not to a label");
13414 if (!ret || (ret->op != OP_RET)) {
13415 internal_error(state, 0, "call not matched with return");
13420 static void walk_functions(struct compile_state *state,
13421 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13424 struct triple *func, *first;
13425 func = first = state->functions;
13427 cb(state, func, arg);
13429 } while(func != first);
13432 static void reverse_walk_functions(struct compile_state *state,
13433 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13436 struct triple *func, *first;
13437 func = first = state->functions;
13440 cb(state, func, arg);
13441 } while(func != first);
13445 static void mark_live(struct compile_state *state, struct triple *func, void *arg)
13447 struct triple *ptr, *first;
13448 if (func->u.cval == 0) {
13451 ptr = first = RHS(func, 0);
13453 if (ptr->op == OP_FCALL) {
13454 struct triple *called_func;
13455 called_func = MISC(ptr, 0);
13456 /* Mark the called function as used */
13457 if (!(func->id & TRIPLE_FLAG_FLATTENED)) {
13458 called_func->u.cval++;
13460 /* Remove the called function from the list */
13461 called_func->prev->next = called_func->next;
13462 called_func->next->prev = called_func->prev;
13464 /* Place the called function before me on the list */
13465 called_func->next = func;
13466 called_func->prev = func->prev;
13467 called_func->prev->next = called_func;
13468 called_func->next->prev = called_func;
13471 } while(ptr != first);
13472 func->id |= TRIPLE_FLAG_FLATTENED;
13475 static void mark_live_functions(struct compile_state *state)
13477 /* Ensure state->main_function is the last function in
13478 * the list of functions.
13480 if ((state->main_function->next != state->functions) ||
13481 (state->functions->prev != state->main_function)) {
13482 internal_error(state, 0,
13483 "state->main_function is not at the end of the function list ");
13485 state->main_function->u.cval = 1;
13486 reverse_walk_functions(state, mark_live, 0);
13489 static int local_triple(struct compile_state *state,
13490 struct triple *func, struct triple *ins)
13492 int local = (ins->id & TRIPLE_FLAG_LOCAL);
13495 FILE *fp = state->errout;
13496 fprintf(fp, "global: ");
13497 display_triple(fp, ins);
13503 struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
13504 struct occurance *base_occurance)
13506 struct triple *nfunc;
13507 struct triple *nfirst, *ofirst;
13508 struct triple *new, *old;
13510 if (state->compiler->debug & DEBUG_INLINE) {
13511 FILE *fp = state->dbgout;
13514 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13515 display_func(state, fp, ofunc);
13516 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
13519 /* Make a new copy of the old function */
13520 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
13522 ofirst = old = RHS(ofunc, 0);
13524 struct triple *new;
13525 struct occurance *occurance;
13526 int old_lhs, old_rhs;
13527 old_lhs = old->lhs;
13528 old_rhs = old->rhs;
13529 occurance = inline_occurance(state, base_occurance, old->occurance);
13530 if (ofunc->u.cval && (old->op == OP_FCALL)) {
13531 MISC(old, 0)->u.cval += 1;
13533 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
13535 if (!triple_stores_block(state, new)) {
13536 memcpy(&new->u, &old->u, sizeof(new->u));
13539 RHS(nfunc, 0) = nfirst = new;
13542 insert_triple(state, nfirst, new);
13544 new->id |= TRIPLE_FLAG_FLATTENED;
13545 new->id |= old->id & TRIPLE_FLAG_COPY;
13547 /* During the copy remember new as user of old */
13548 use_triple(old, new);
13550 /* Remember which instructions are local */
13551 old->id |= TRIPLE_FLAG_LOCAL;
13553 } while(old != ofirst);
13555 /* Make a second pass to fix up any unresolved references */
13559 struct triple **oexpr, **nexpr;
13561 /* Lookup where the copy is, to join pointers */
13562 count = TRIPLE_SIZE(old);
13563 for(i = 0; i < count; i++) {
13564 oexpr = &old->param[i];
13565 nexpr = &new->param[i];
13566 if (*oexpr && !*nexpr) {
13567 if (!local_triple(state, ofunc, *oexpr)) {
13570 else if ((*oexpr)->use) {
13571 *nexpr = (*oexpr)->use->member;
13573 if (*nexpr == old) {
13574 internal_error(state, 0, "new == old?");
13576 use_triple(*nexpr, new);
13578 if (!*nexpr && *oexpr) {
13579 internal_error(state, 0, "Could not copy %d", i);
13584 } while((old != ofirst) && (new != nfirst));
13586 /* Make a third pass to cleanup the extra useses */
13590 unuse_triple(old, new);
13591 /* Forget which instructions are local */
13592 old->id &= ~TRIPLE_FLAG_LOCAL;
13595 } while ((old != ofirst) && (new != nfirst));
13599 static void expand_inline_call(
13600 struct compile_state *state, struct triple *me, struct triple *fcall)
13602 /* Inline the function call */
13603 struct type *ptype;
13604 struct triple *ofunc, *nfunc, *nfirst, *result, *retvar, *ins;
13605 struct triple *end, *nend;
13608 /* Find the triples */
13609 ofunc = MISC(fcall, 0);
13610 if (ofunc->op != OP_LIST) {
13611 internal_error(state, 0, "improper function");
13613 nfunc = copy_func(state, ofunc, fcall->occurance);
13614 /* Prepend the parameter reading into the new function list */
13615 ptype = nfunc->type->right;
13616 pvals = fcall->rhs;
13617 for(i = 0; i < pvals; i++) {
13618 struct type *atype;
13619 struct triple *arg, *param;
13621 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
13622 atype = ptype->left;
13624 param = farg(state, nfunc, i);
13625 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
13626 internal_error(state, fcall, "param %d type mismatch", i);
13628 arg = RHS(fcall, i);
13629 flatten(state, fcall, write_expr(state, param, arg));
13630 ptype = ptype->right;
13633 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
13634 result = read_expr(state,
13635 deref_index(state, fresult(state, nfunc), 1));
13637 if (state->compiler->debug & DEBUG_INLINE) {
13638 FILE *fp = state->dbgout;
13641 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13642 display_func(state, fp, nfunc);
13643 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
13647 * Get rid of the extra triples
13649 /* Remove the read of the return address */
13650 ins = RHS(nfunc, 0)->prev->prev;
13651 if ((ins->op != OP_READ) || (RHS(ins, 0) != fretaddr(state, nfunc))) {
13652 internal_error(state, ins, "Not return addres read?");
13654 release_triple(state, ins);
13655 /* Remove the return instruction */
13656 ins = RHS(nfunc, 0)->prev;
13657 if (ins->op != OP_RET) {
13658 internal_error(state, ins, "Not return?");
13660 release_triple(state, ins);
13661 /* Remove the retaddres variable */
13662 retvar = fretaddr(state, nfunc);
13663 if ((retvar->lhs != 1) ||
13664 (retvar->op != OP_ADECL) ||
13665 (retvar->next->op != OP_PIECE) ||
13666 (MISC(retvar->next, 0) != retvar)) {
13667 internal_error(state, retvar, "Not the return address?");
13669 release_triple(state, retvar->next);
13670 release_triple(state, retvar);
13672 /* Remove the label at the start of the function */
13673 ins = RHS(nfunc, 0);
13674 if (ins->op != OP_LABEL) {
13675 internal_error(state, ins, "Not label?");
13677 nfirst = ins->next;
13678 free_triple(state, ins);
13679 /* Release the new function header */
13681 free_triple(state, nfunc);
13683 /* Append the new function list onto the return list */
13685 nend = nfirst->prev;
13686 end->next = nfirst;
13687 nfirst->prev = end;
13688 nend->next = fcall;
13689 fcall->prev = nend;
13691 /* Now the result reading code */
13693 result = flatten(state, fcall, result);
13694 propogate_use(state, fcall, result);
13697 /* Release the original fcall instruction */
13698 release_triple(state, fcall);
13705 * Type of the result variable.
13709 * +----------+------------+
13711 * union of closures result_type
13713 * +------------------+---------------+
13715 * closure1 ... closuerN
13717 * +----+--+-+--------+-----+ +----+----+---+-----+
13718 * | | | | | | | | |
13719 * var1 var2 var3 ... varN result var1 var2 ... varN result
13721 * +--------+---------+
13723 * union of closures result_type
13725 * +-----+-------------------+
13727 * closure1 ... closureN
13729 * +-----+---+----+----+ +----+---+----+-----+
13731 * var1 var2 ... varN result var1 var2 ... varN result
13734 static int add_closure_type(struct compile_state *state,
13735 struct triple *func, struct type *closure_type)
13737 struct type *type, *ctype, **next;
13738 struct triple *var, *new_var;
13742 FILE *fp = state->errout;
13743 fprintf(fp, "original_type: ");
13744 name_of(fp, fresult(state, func)->type);
13747 /* find the original type */
13748 var = fresult(state, func);
13750 if (type->elements != 2) {
13751 internal_error(state, var, "bad return type");
13754 /* Find the complete closure type and update it */
13755 ctype = type->left->left;
13756 next = &ctype->left;
13757 while(((*next)->type & TYPE_MASK) == TYPE_OVERLAP) {
13758 next = &(*next)->right;
13760 *next = new_type(TYPE_OVERLAP, *next, dup_type(state, closure_type));
13761 ctype->elements += 1;
13764 fprintf(fp, "new_type: ");
13767 fprintf(fp, "ctype: %p %d bits: %d ",
13768 ctype, ctype->elements, reg_size_of(state, ctype));
13769 name_of(fp, ctype);
13773 /* Regenerate the variable with the new type definition */
13774 new_var = pre_triple(state, var, OP_ADECL, type, 0, 0);
13775 new_var->id |= TRIPLE_FLAG_FLATTENED;
13776 for(i = 0; i < new_var->lhs; i++) {
13777 LHS(new_var, i)->id |= TRIPLE_FLAG_FLATTENED;
13780 /* Point everyone at the new variable */
13781 propogate_use(state, var, new_var);
13783 /* Release the original variable */
13784 for(i = 0; i < var->lhs; i++) {
13785 release_triple(state, LHS(var, i));
13787 release_triple(state, var);
13789 /* Return the index of the added closure type */
13790 return ctype->elements - 1;
13793 static struct triple *closure_expr(struct compile_state *state,
13794 struct triple *func, int closure_idx, int var_idx)
13796 return deref_index(state,
13798 deref_index(state, fresult(state, func), 0),
13804 static void insert_triple_set(
13805 struct triple_reg_set **head, struct triple *member)
13807 struct triple_reg_set *new;
13808 new = xcmalloc(sizeof(*new), "triple_set");
13809 new->member = member;
13815 static int ordered_triple_set(
13816 struct triple_reg_set **head, struct triple *member)
13818 struct triple_reg_set **ptr;
13823 if (member == (*ptr)->member) {
13826 /* keep the list ordered */
13827 if (member->id < (*ptr)->member->id) {
13830 ptr = &(*ptr)->next;
13832 insert_triple_set(ptr, member);
13837 static void free_closure_variables(struct compile_state *state,
13838 struct triple_reg_set **enclose)
13840 struct triple_reg_set *entry, *next;
13841 for(entry = *enclose; entry; entry = next) {
13842 next = entry->next;
13843 do_triple_unset(enclose, entry->member);
13847 static int lookup_closure_index(struct compile_state *state,
13848 struct triple *me, struct triple *val)
13850 struct triple *first, *ins, *next;
13851 first = RHS(me, 0);
13852 ins = next = first;
13854 struct triple *result;
13855 struct triple *index0, *index1, *index2, *read, *write;
13858 if (ins->op != OP_CALL) {
13861 /* I am at a previous call point examine it closely */
13862 if (ins->next->op != OP_LABEL) {
13863 internal_error(state, ins, "call not followed by label");
13865 /* Does this call does not enclose any variables? */
13866 if ((ins->next->next->op != OP_INDEX) ||
13867 (ins->next->next->u.cval != 0) ||
13868 (result = MISC(ins->next->next, 0)) ||
13869 (result->id & TRIPLE_FLAG_LOCAL)) {
13872 index0 = ins->next->next;
13874 * 0 index result < 0 >
13880 for(index0 = ins->next->next;
13881 (index0->op == OP_INDEX) &&
13882 (MISC(index0, 0) == result) &&
13883 (index0->u.cval == 0) ;
13884 index0 = write->next)
13886 index1 = index0->next;
13887 index2 = index1->next;
13888 read = index2->next;
13889 write = read->next;
13890 if ((index0->op != OP_INDEX) ||
13891 (index1->op != OP_INDEX) ||
13892 (index2->op != OP_INDEX) ||
13893 (read->op != OP_READ) ||
13894 (write->op != OP_WRITE) ||
13895 (MISC(index1, 0) != index0) ||
13896 (MISC(index2, 0) != index1) ||
13897 (RHS(read, 0) != index2) ||
13898 (RHS(write, 0) != read)) {
13899 internal_error(state, index0, "bad var read");
13901 if (MISC(write, 0) == val) {
13902 return index2->u.cval;
13905 } while(next != first);
13909 static inline int enclose_triple(struct triple *ins)
13911 return (ins && ((ins->type->type & TYPE_MASK) != TYPE_VOID));
13914 static void compute_closure_variables(struct compile_state *state,
13915 struct triple *me, struct triple *fcall, struct triple_reg_set **enclose)
13917 struct triple_reg_set *set, *vars, **last_var;
13918 struct basic_blocks bb;
13919 struct reg_block *rb;
13920 struct block *block;
13921 struct triple *old_result, *first, *ins;
13923 unsigned long used_indicies;
13925 #define MAX_INDICIES (sizeof(used_indicies)*CHAR_BIT)
13926 #define ID_BITS(X) ((X) & (TRIPLE_FLAG_LOCAL -1))
13933 /* Find the basic blocks of this function */
13935 bb.first = RHS(me, 0);
13937 if (!triple_is_ret(state, bb.first->prev)) {
13940 old_result = fresult(state, me);
13942 analyze_basic_blocks(state, &bb);
13944 /* Find which variables are currently alive in a given block */
13945 rb = compute_variable_lifetimes(state, &bb);
13947 /* Find the variables that are currently alive */
13948 block = block_of_triple(state, fcall);
13949 if (!block || (block->vertex <= 0) || (block->vertex > bb.last_vertex)) {
13950 internal_error(state, fcall, "No reg block? block: %p", block);
13953 #if DEBUG_EXPLICIT_CLOSURES
13954 print_live_variables(state, &bb, rb, state->dbgout);
13955 fflush(state->dbgout);
13958 /* Count the number of triples in the function */
13959 first = RHS(me, 0);
13965 } while(ins != first);
13967 /* Allocate some memory to temorary hold the id info */
13968 info = xcmalloc(sizeof(*info) * (count +1), "info");
13970 /* Mark the local function */
13971 first = RHS(me, 0);
13975 info[idx].id = ins->id;
13976 ins->id = TRIPLE_FLAG_LOCAL | idx;
13979 } while(ins != first);
13982 * Build the list of variables to enclose.
13984 * A target it to put the same variable in the
13985 * same slot for ever call of a given function.
13986 * After coloring this removes all of the variable
13987 * manipulation code.
13989 * The list of variables to enclose is built ordered
13990 * program order because except in corner cases this
13991 * gives me the stability of assignment I need.
13993 * To gurantee that stability I lookup the variables
13994 * to see where they have been used before and
13995 * I build my final list with the assigned indicies.
13998 if (enclose_triple(old_result)) {
13999 ordered_triple_set(&vars, old_result);
14001 for(set = rb[block->vertex].out; set; set = set->next) {
14002 if (!enclose_triple(set->member)) {
14005 if ((set->member == fcall) || (set->member == old_result)) {
14008 if (!local_triple(state, me, set->member)) {
14009 internal_error(state, set->member, "not local?");
14011 ordered_triple_set(&vars, set->member);
14014 /* Lookup the current indicies of the live varialbe */
14017 for(set = vars; set ; set = set->next) {
14018 struct triple *ins;
14021 index = lookup_closure_index(state, me, ins);
14022 info[ID_BITS(ins->id)].index = index;
14026 if (index >= MAX_INDICIES) {
14027 internal_error(state, ins, "index unexpectedly large");
14029 if (used_indicies & (1 << index)) {
14030 internal_error(state, ins, "index previously used?");
14032 /* Remember which indicies have been used */
14033 used_indicies |= (1 << index);
14034 if (index > max_index) {
14039 /* Walk through the live variables and make certain
14040 * everything is assigned an index.
14042 for(set = vars; set; set = set->next) {
14043 struct triple *ins;
14046 index = info[ID_BITS(ins->id)].index;
14050 /* Find the lowest unused index value */
14051 for(index = 0; index < MAX_INDICIES; index++) {
14052 if (!(used_indicies & (1 << index))) {
14056 if (index == MAX_INDICIES) {
14057 internal_error(state, ins, "no free indicies?");
14059 info[ID_BITS(ins->id)].index = index;
14060 /* Remember which indicies have been used */
14061 used_indicies |= (1 << index);
14062 if (index > max_index) {
14067 /* Build the return list of variables with positions matching
14071 last_var = enclose;
14072 for(i = 0; i <= max_index; i++) {
14073 struct triple *var;
14075 if (used_indicies & (1 << i)) {
14076 for(set = vars; set; set = set->next) {
14078 index = info[ID_BITS(set->member->id)].index;
14085 internal_error(state, me, "missing variable");
14088 insert_triple_set(last_var, var);
14089 last_var = &(*last_var)->next;
14092 #if DEBUG_EXPLICIT_CLOSURES
14093 /* Print out the variables to be enclosed */
14094 loc(state->dbgout, state, fcall);
14095 fprintf(state->dbgout, "Alive: \n");
14096 for(set = *enclose; set; set = set->next) {
14097 display_triple(state->dbgout, set->member);
14099 fflush(state->dbgout);
14102 /* Clear the marks */
14105 ins->id = info[ID_BITS(ins->id)].id;
14107 } while(ins != first);
14109 /* Release the ordered list of live variables */
14110 free_closure_variables(state, &vars);
14112 /* Release the storage of the old ids */
14115 /* Release the variable lifetime information */
14116 free_variable_lifetimes(state, &bb, rb);
14118 /* Release the basic blocks of this function */
14119 free_basic_blocks(state, &bb);
14122 static void expand_function_call(
14123 struct compile_state *state, struct triple *me, struct triple *fcall)
14125 /* Generate an ordinary function call */
14126 struct type *closure_type, **closure_next;
14127 struct triple *func, *func_first, *func_last, *retvar;
14128 struct triple *first;
14129 struct type *ptype, *rtype;
14130 struct triple *jmp;
14131 struct triple *ret_addr, *ret_loc, *ret_set;
14132 struct triple_reg_set *enclose, *set;
14133 int closure_idx, pvals, i;
14135 #if DEBUG_EXPLICIT_CLOSURES
14136 FILE *fp = state->dbgout;
14137 fprintf(fp, "\ndisplay_func(me) ptr: %p\n", fcall);
14138 display_func(state, fp, MISC(fcall, 0));
14139 display_func(state, fp, me);
14140 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14143 /* Find the triples */
14144 func = MISC(fcall, 0);
14145 func_first = RHS(func, 0);
14146 retvar = fretaddr(state, func);
14147 func_last = func_first->prev;
14148 first = fcall->next;
14150 /* Find what I need to enclose */
14151 compute_closure_variables(state, me, fcall, &enclose);
14153 /* Compute the closure type */
14154 closure_type = new_type(TYPE_TUPLE, 0, 0);
14155 closure_type->elements = 0;
14156 closure_next = &closure_type->left;
14157 for(set = enclose; set ; set = set->next) {
14161 type = set->member->type;
14163 if (!*closure_next) {
14164 *closure_next = type;
14166 *closure_next = new_type(TYPE_PRODUCT, *closure_next,
14168 closure_next = &(*closure_next)->right;
14170 closure_type->elements += 1;
14172 if (closure_type->elements == 0) {
14173 closure_type->type = TYPE_VOID;
14177 #if DEBUG_EXPLICIT_CLOSURES
14178 fprintf(state->dbgout, "closure type: ");
14179 name_of(state->dbgout, closure_type);
14180 fprintf(state->dbgout, "\n");
14183 /* Update the called functions closure variable */
14184 closure_idx = add_closure_type(state, func, closure_type);
14186 /* Generate some needed triples */
14187 ret_loc = label(state);
14188 ret_addr = triple(state, OP_ADDRCONST, &void_ptr_type, ret_loc, 0);
14190 /* Pass the parameters to the new function */
14191 ptype = func->type->right;
14192 pvals = fcall->rhs;
14193 for(i = 0; i < pvals; i++) {
14194 struct type *atype;
14195 struct triple *arg, *param;
14197 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
14198 atype = ptype->left;
14200 param = farg(state, func, i);
14201 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
14202 internal_error(state, fcall, "param type mismatch");
14204 arg = RHS(fcall, i);
14205 flatten(state, first, write_expr(state, param, arg));
14206 ptype = ptype->right;
14208 rtype = func->type->left;
14210 /* Thread the triples together */
14211 ret_loc = flatten(state, first, ret_loc);
14213 /* Save the active variables in the result variable */
14214 for(i = 0, set = enclose; set ; set = set->next, i++) {
14215 if (!set->member) {
14218 flatten(state, ret_loc,
14220 closure_expr(state, func, closure_idx, i),
14221 read_expr(state, set->member)));
14224 /* Initialize the return value */
14225 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14226 flatten(state, ret_loc,
14228 deref_index(state, fresult(state, func), 1),
14229 new_triple(state, OP_UNKNOWNVAL, rtype, 0, 0)));
14232 ret_addr = flatten(state, ret_loc, ret_addr);
14233 ret_set = flatten(state, ret_loc, write_expr(state, retvar, ret_addr));
14234 jmp = flatten(state, ret_loc,
14235 call(state, retvar, ret_addr, func_first, func_last));
14237 /* Find the result */
14238 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14239 struct triple * result;
14240 result = flatten(state, first,
14242 deref_index(state, fresult(state, func), 1)));
14244 propogate_use(state, fcall, result);
14247 /* Release the original fcall instruction */
14248 release_triple(state, fcall);
14250 /* Restore the active variables from the result variable */
14251 for(i = 0, set = enclose; set ; set = set->next, i++) {
14252 struct triple_set *use, *next;
14253 struct triple *new;
14254 struct basic_blocks bb;
14255 if (!set->member || (set->member == fcall)) {
14258 /* Generate an expression for the value */
14259 new = flatten(state, first,
14261 closure_expr(state, func, closure_idx, i)));
14264 /* If the original is an lvalue restore the preserved value */
14265 if (is_lvalue(state, set->member)) {
14266 flatten(state, first,
14267 write_expr(state, set->member, new));
14271 * If the original is a value update the dominated uses.
14274 /* Analyze the basic blocks so I can see who dominates whom */
14276 bb.first = RHS(me, 0);
14277 if (!triple_is_ret(state, bb.first->prev)) {
14280 analyze_basic_blocks(state, &bb);
14283 #if DEBUG_EXPLICIT_CLOSURES
14284 fprintf(state->errout, "Updating domindated uses: %p -> %p\n",
14287 /* If fcall dominates the use update the expression */
14288 for(use = set->member->use; use; use = next) {
14289 /* Replace use modifies the use chain and
14290 * removes use, so I must take a copy of the
14291 * next entry early.
14294 if (!tdominates(state, fcall, use->member)) {
14297 replace_use(state, set->member, new, use->member);
14300 /* Release the basic blocks, the instructions will be
14301 * different next time, and flatten/insert_triple does
14302 * not update the block values so I can't cache the analysis.
14304 free_basic_blocks(state, &bb);
14307 /* Release the closure variable list */
14308 free_closure_variables(state, &enclose);
14310 if (state->compiler->debug & DEBUG_INLINE) {
14311 FILE *fp = state->dbgout;
14314 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
14315 display_func(state, fp, func);
14316 display_func(state, fp, me);
14317 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14323 static int do_inline(struct compile_state *state, struct triple *func)
14328 policy = state->compiler->flags & COMPILER_INLINE_MASK;
14330 case COMPILER_INLINE_ALWAYS:
14332 if (func->type->type & ATTRIB_NOINLINE) {
14333 error(state, func, "noinline with always_inline compiler option");
14336 case COMPILER_INLINE_NEVER:
14338 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14339 error(state, func, "always_inline with noinline compiler option");
14342 case COMPILER_INLINE_DEFAULTON:
14343 switch(func->type->type & STOR_MASK) {
14344 case STOR_STATIC | STOR_INLINE:
14345 case STOR_LOCAL | STOR_INLINE:
14346 case STOR_EXTERN | STOR_INLINE:
14354 case COMPILER_INLINE_DEFAULTOFF:
14355 switch(func->type->type & STOR_MASK) {
14356 case STOR_STATIC | STOR_INLINE:
14357 case STOR_LOCAL | STOR_INLINE:
14358 case STOR_EXTERN | STOR_INLINE:
14366 case COMPILER_INLINE_NOPENALTY:
14367 switch(func->type->type & STOR_MASK) {
14368 case STOR_STATIC | STOR_INLINE:
14369 case STOR_LOCAL | STOR_INLINE:
14370 case STOR_EXTERN | STOR_INLINE:
14374 do_inline = (func->u.cval == 1);
14380 internal_error(state, 0, "Unimplemented inline policy");
14383 /* Force inlining */
14384 if (func->type->type & ATTRIB_NOINLINE) {
14387 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14393 static void inline_function(struct compile_state *state, struct triple *me, void *arg)
14395 struct triple *first, *ptr, *next;
14396 /* If the function is not used don't bother */
14397 if (me->u.cval <= 0) {
14400 if (state->compiler->debug & DEBUG_CALLS2) {
14401 FILE *fp = state->dbgout;
14402 fprintf(fp, "in: %s\n",
14403 me->type->type_ident->name);
14406 first = RHS(me, 0);
14407 ptr = next = first;
14409 struct triple *func, *prev;
14413 if (ptr->op != OP_FCALL) {
14416 func = MISC(ptr, 0);
14417 /* See if the function should be inlined */
14418 if (!do_inline(state, func)) {
14419 /* Put a label after the fcall */
14420 post_triple(state, ptr, OP_LABEL, &void_type, 0, 0);
14423 if (state->compiler->debug & DEBUG_CALLS) {
14424 FILE *fp = state->dbgout;
14425 if (state->compiler->debug & DEBUG_CALLS2) {
14426 loc(fp, state, ptr);
14428 fprintf(fp, "inlining %s\n",
14429 func->type->type_ident->name);
14433 /* Update the function use counts */
14436 /* Replace the fcall with the called function */
14437 expand_inline_call(state, me, ptr);
14440 } while (next != first);
14442 ptr = next = first;
14444 struct triple *prev, *func;
14448 if (ptr->op != OP_FCALL) {
14451 func = MISC(ptr, 0);
14452 if (state->compiler->debug & DEBUG_CALLS) {
14453 FILE *fp = state->dbgout;
14454 if (state->compiler->debug & DEBUG_CALLS2) {
14455 loc(fp, state, ptr);
14457 fprintf(fp, "calling %s\n",
14458 func->type->type_ident->name);
14461 /* Replace the fcall with the instruction sequence
14462 * needed to make the call.
14464 expand_function_call(state, me, ptr);
14466 } while(next != first);
14469 static void inline_functions(struct compile_state *state, struct triple *func)
14471 inline_function(state, func, 0);
14472 reverse_walk_functions(state, inline_function, 0);
14475 static void insert_function(struct compile_state *state,
14476 struct triple *func, void *arg)
14478 struct triple *first, *end, *ffirst, *fend;
14480 if (state->compiler->debug & DEBUG_INLINE) {
14481 FILE *fp = state->errout;
14482 fprintf(fp, "%s func count: %d\n",
14483 func->type->type_ident->name, func->u.cval);
14485 if (func->u.cval == 0) {
14489 /* Find the end points of the lists */
14492 ffirst = RHS(func, 0);
14493 fend = ffirst->prev;
14495 /* splice the lists together */
14496 end->next = ffirst;
14497 ffirst->prev = end;
14498 fend->next = first;
14499 first->prev = fend;
14502 struct triple *input_asm(struct compile_state *state)
14504 struct asm_info *info;
14505 struct triple *def;
14508 info = xcmalloc(sizeof(*info), "asm_info");
14511 out = sizeof(arch_input_regs)/sizeof(arch_input_regs[0]);
14512 memcpy(&info->tmpl.lhs, arch_input_regs, sizeof(arch_input_regs));
14514 def = new_triple(state, OP_ASM, &void_type, out, 0);
14515 def->u.ainfo = info;
14516 def->id |= TRIPLE_FLAG_VOLATILE;
14518 for(i = 0; i < out; i++) {
14519 struct triple *piece;
14520 piece = triple(state, OP_PIECE, &int_type, def, 0);
14522 LHS(def, i) = piece;
14528 struct triple *output_asm(struct compile_state *state)
14530 struct asm_info *info;
14531 struct triple *def;
14534 info = xcmalloc(sizeof(*info), "asm_info");
14537 in = sizeof(arch_output_regs)/sizeof(arch_output_regs[0]);
14538 memcpy(&info->tmpl.rhs, arch_output_regs, sizeof(arch_output_regs));
14540 def = new_triple(state, OP_ASM, &void_type, 0, in);
14541 def->u.ainfo = info;
14542 def->id |= TRIPLE_FLAG_VOLATILE;
14547 static void join_functions(struct compile_state *state)
14549 struct triple *jmp, *start, *end, *call, *in, *out, *func;
14550 struct file_state file;
14551 struct type *pnext, *param;
14552 struct type *result_type, *args_type;
14555 /* Be clear the functions have not been joined yet */
14556 state->functions_joined = 0;
14558 /* Dummy file state to get debug handing right */
14559 memset(&file, 0, sizeof(file));
14560 file.basename = "";
14562 file.report_line = 0;
14563 file.report_name = file.basename;
14564 file.prev = state->file;
14565 state->file = &file;
14566 state->function = "";
14568 if (!state->main_function) {
14569 error(state, 0, "No functions to compile\n");
14572 /* The type of arguments */
14573 args_type = state->main_function->type->right;
14574 /* The return type without any specifiers */
14575 result_type = clone_type(0, state->main_function->type->left);
14578 /* Verify the external arguments */
14579 if (registers_of(state, args_type) > ARCH_INPUT_REGS) {
14580 error(state, state->main_function,
14581 "Too many external input arguments");
14583 if (registers_of(state, result_type) > ARCH_OUTPUT_REGS) {
14584 error(state, state->main_function,
14585 "Too many external output arguments");
14588 /* Lay down the basic program structure */
14589 end = label(state);
14590 start = label(state);
14591 start = flatten(state, state->first, start);
14592 end = flatten(state, state->first, end);
14593 in = input_asm(state);
14594 out = output_asm(state);
14595 call = new_triple(state, OP_FCALL, result_type, -1, registers_of(state, args_type));
14596 MISC(call, 0) = state->main_function;
14597 in = flatten(state, state->first, in);
14598 call = flatten(state, state->first, call);
14599 out = flatten(state, state->first, out);
14602 /* Read the external input arguments */
14605 while(pnext && ((pnext->type & TYPE_MASK) != TYPE_VOID)) {
14606 struct triple *expr;
14609 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
14610 pnext = param->right;
14611 param = param->left;
14613 if (registers_of(state, param) != 1) {
14614 error(state, state->main_function,
14615 "Arg: %d %s requires multiple registers",
14616 idx + 1, param->field_ident->name);
14618 expr = read_expr(state, LHS(in, idx));
14619 RHS(call, idx) = expr;
14620 expr = flatten(state, call, expr);
14621 use_triple(expr, call);
14627 /* Write the external output arguments */
14628 pnext = result_type;
14629 if ((pnext->type & TYPE_MASK) == TYPE_STRUCT) {
14630 pnext = result_type->left;
14632 for(idx = 0; idx < out->rhs; idx++) {
14633 struct triple *expr;
14636 if (param && ((param->type & TYPE_MASK) == TYPE_PRODUCT)) {
14637 pnext = param->right;
14638 param = param->left;
14640 if (param && ((param->type & TYPE_MASK) == TYPE_VOID)) {
14644 if (registers_of(state, param) != 1) {
14645 error(state, state->main_function,
14646 "Result: %d %s requires multiple registers",
14647 idx, param->field_ident->name);
14649 expr = read_expr(state, call);
14650 if ((result_type->type & TYPE_MASK) == TYPE_STRUCT) {
14651 expr = deref_field(state, expr, param->field_ident);
14654 expr = triple(state, OP_UNKNOWNVAL, &int_type, 0, 0);
14656 flatten(state, out, expr);
14657 RHS(out, idx) = expr;
14658 use_triple(expr, out);
14661 /* Allocate a dummy containing function */
14662 func = triple(state, OP_LIST,
14663 new_type(TYPE_FUNCTION, &void_type, &void_type), 0, 0);
14664 func->type->type_ident = lookup(state, "", 0);
14665 RHS(func, 0) = state->first;
14668 /* See which functions are called, and how often */
14669 mark_live_functions(state);
14670 inline_functions(state, func);
14671 walk_functions(state, insert_function, end);
14673 if (start->next != end) {
14674 jmp = flatten(state, start, branch(state, end, 0));
14677 /* OK now the functions have been joined. */
14678 state->functions_joined = 1;
14680 /* Done now cleanup */
14681 state->file = file.prev;
14682 state->function = 0;
14686 * Data structurs for optimation.
14690 static int do_use_block(
14691 struct block *used, struct block_set **head, struct block *user,
14694 struct block_set **ptr, *new;
14701 if ((*ptr)->member == user) {
14704 ptr = &(*ptr)->next;
14706 new = xcmalloc(sizeof(*new), "block_set");
14707 new->member = user;
14718 static int do_unuse_block(
14719 struct block *used, struct block_set **head, struct block *unuser)
14721 struct block_set *use, **ptr;
14727 if (use->member == unuser) {
14729 memset(use, -1, sizeof(*use));
14740 static void use_block(struct block *used, struct block *user)
14743 /* Append new to the head of the list, print_block
14746 count = do_use_block(used, &used->use, user, 1);
14747 used->users += count;
14749 static void unuse_block(struct block *used, struct block *unuser)
14752 count = do_unuse_block(used, &used->use, unuser);
14753 used->users -= count;
14756 static void add_block_edge(struct block *block, struct block *edge, int front)
14759 count = do_use_block(block, &block->edges, edge, front);
14760 block->edge_count += count;
14763 static void remove_block_edge(struct block *block, struct block *edge)
14766 count = do_unuse_block(block, &block->edges, edge);
14767 block->edge_count -= count;
14770 static void idom_block(struct block *idom, struct block *user)
14772 do_use_block(idom, &idom->idominates, user, 0);
14775 static void unidom_block(struct block *idom, struct block *unuser)
14777 do_unuse_block(idom, &idom->idominates, unuser);
14780 static void domf_block(struct block *block, struct block *domf)
14782 do_use_block(block, &block->domfrontier, domf, 0);
14785 static void undomf_block(struct block *block, struct block *undomf)
14787 do_unuse_block(block, &block->domfrontier, undomf);
14790 static void ipdom_block(struct block *ipdom, struct block *user)
14792 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
14795 static void unipdom_block(struct block *ipdom, struct block *unuser)
14797 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
14800 static void ipdomf_block(struct block *block, struct block *ipdomf)
14802 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
14805 static void unipdomf_block(struct block *block, struct block *unipdomf)
14807 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
14810 static int walk_triples(
14811 struct compile_state *state,
14812 int (*cb)(struct compile_state *state, struct triple *ptr, void *arg),
14815 struct triple *ptr;
14817 ptr = state->first;
14819 result = cb(state, ptr, arg);
14820 if (ptr->next->prev != ptr) {
14821 internal_error(state, ptr->next, "bad prev");
14824 } while((result == 0) && (ptr != state->first));
14828 #define PRINT_LIST 1
14829 static int do_print_triple(struct compile_state *state, struct triple *ins, void *arg)
14834 if (op == OP_LIST) {
14839 if ((op == OP_LABEL) && (ins->use)) {
14840 fprintf(fp, "\n%p:\n", ins);
14842 display_triple(fp, ins);
14844 if (triple_is_branch(state, ins) && ins->use &&
14845 (ins->op != OP_RET) && (ins->op != OP_FCALL)) {
14846 internal_error(state, ins, "branch used?");
14848 if (triple_is_branch(state, ins)) {
14854 static void print_triples(struct compile_state *state)
14856 if (state->compiler->debug & DEBUG_TRIPLES) {
14857 FILE *fp = state->dbgout;
14858 fprintf(fp, "--------------- triples ---------------\n");
14859 walk_triples(state, do_print_triple, fp);
14865 struct block *block;
14867 static void find_cf_blocks(struct cf_block *cf, struct block *block)
14869 struct block_set *edge;
14870 if (!block || (cf[block->vertex].block == block)) {
14873 cf[block->vertex].block = block;
14874 for(edge = block->edges; edge; edge = edge->next) {
14875 find_cf_blocks(cf, edge->member);
14879 static void print_control_flow(struct compile_state *state,
14880 FILE *fp, struct basic_blocks *bb)
14882 struct cf_block *cf;
14884 fprintf(fp, "\ncontrol flow\n");
14885 cf = xcmalloc(sizeof(*cf) * (bb->last_vertex + 1), "cf_block");
14886 find_cf_blocks(cf, bb->first_block);
14888 for(i = 1; i <= bb->last_vertex; i++) {
14889 struct block *block;
14890 struct block_set *edge;
14891 block = cf[i].block;
14894 fprintf(fp, "(%p) %d:", block, block->vertex);
14895 for(edge = block->edges; edge; edge = edge->next) {
14896 fprintf(fp, " %d", edge->member->vertex);
14904 static void free_basic_block(struct compile_state *state, struct block *block)
14906 struct block_set *edge, *entry;
14907 struct block *child;
14911 if (block->vertex == -1) {
14914 block->vertex = -1;
14915 for(edge = block->edges; edge; edge = edge->next) {
14916 if (edge->member) {
14917 unuse_block(edge->member, block);
14921 unidom_block(block->idom, block);
14924 if (block->ipdom) {
14925 unipdom_block(block->ipdom, block);
14928 while((entry = block->use)) {
14929 child = entry->member;
14930 unuse_block(block, child);
14931 if (child && (child->vertex != -1)) {
14932 for(edge = child->edges; edge; edge = edge->next) {
14937 while((entry = block->idominates)) {
14938 child = entry->member;
14939 unidom_block(block, child);
14940 if (child && (child->vertex != -1)) {
14944 while((entry = block->domfrontier)) {
14945 child = entry->member;
14946 undomf_block(block, child);
14948 while((entry = block->ipdominates)) {
14949 child = entry->member;
14950 unipdom_block(block, child);
14951 if (child && (child->vertex != -1)) {
14955 while((entry = block->ipdomfrontier)) {
14956 child = entry->member;
14957 unipdomf_block(block, child);
14959 if (block->users != 0) {
14960 internal_error(state, 0, "block still has users");
14962 while((edge = block->edges)) {
14963 child = edge->member;
14964 remove_block_edge(block, child);
14966 if (child && (child->vertex != -1)) {
14967 free_basic_block(state, child);
14970 memset(block, -1, sizeof(*block));
14974 static void free_basic_blocks(struct compile_state *state,
14975 struct basic_blocks *bb)
14977 struct triple *first, *ins;
14978 free_basic_block(state, bb->first_block);
14979 bb->last_vertex = 0;
14980 bb->first_block = bb->last_block = 0;
14984 if (triple_stores_block(state, ins)) {
14988 } while(ins != first);
14992 static struct block *basic_block(struct compile_state *state,
14993 struct basic_blocks *bb, struct triple *first)
14995 struct block *block;
14996 struct triple *ptr;
14997 if (!triple_is_label(state, first)) {
14998 internal_error(state, first, "block does not start with a label");
15000 /* See if this basic block has already been setup */
15001 if (first->u.block != 0) {
15002 return first->u.block;
15004 /* Allocate another basic block structure */
15005 bb->last_vertex += 1;
15006 block = xcmalloc(sizeof(*block), "block");
15007 block->first = block->last = first;
15008 block->vertex = bb->last_vertex;
15011 if ((ptr != first) && triple_is_label(state, ptr) && (ptr->use)) {
15015 /* If ptr->u is not used remember where the baic block is */
15016 if (triple_stores_block(state, ptr)) {
15017 ptr->u.block = block;
15019 if (triple_is_branch(state, ptr)) {
15023 } while (ptr != bb->first);
15024 if ((ptr == bb->first) ||
15025 ((ptr->next == bb->first) && (
15026 triple_is_end(state, ptr) ||
15027 triple_is_ret(state, ptr))))
15029 /* The block has no outflowing edges */
15031 else if (triple_is_label(state, ptr)) {
15032 struct block *next;
15033 next = basic_block(state, bb, ptr);
15034 add_block_edge(block, next, 0);
15035 use_block(next, block);
15037 else if (triple_is_branch(state, ptr)) {
15038 struct triple **expr, *first;
15039 struct block *child;
15040 /* Find the branch targets.
15041 * I special case the first branch as that magically
15042 * avoids some difficult cases for the register allocator.
15044 expr = triple_edge_targ(state, ptr, 0);
15046 internal_error(state, ptr, "branch without targets");
15049 expr = triple_edge_targ(state, ptr, expr);
15050 for(; expr; expr = triple_edge_targ(state, ptr, expr)) {
15051 if (!*expr) continue;
15052 child = basic_block(state, bb, *expr);
15053 use_block(child, block);
15054 add_block_edge(block, child, 0);
15057 child = basic_block(state, bb, first);
15058 use_block(child, block);
15059 add_block_edge(block, child, 1);
15061 /* Be certain the return block of a call is
15062 * in a basic block. When it is not find
15063 * start of the block, insert a label if
15064 * necessary and build the basic block.
15065 * Then add a fake edge from the start block
15066 * to the return block of the function.
15068 if (state->functions_joined && triple_is_call(state, ptr)
15069 && !block_of_triple(state, MISC(ptr, 0))) {
15070 struct block *tail;
15071 struct triple *start;
15072 start = triple_to_block_start(state, MISC(ptr, 0));
15073 if (!triple_is_label(state, start)) {
15074 start = pre_triple(state,
15075 start, OP_LABEL, &void_type, 0, 0);
15077 tail = basic_block(state, bb, start);
15078 add_block_edge(child, tail, 0);
15079 use_block(tail, child);
15084 internal_error(state, 0, "Bad basic block split");
15088 struct block_set *edge;
15089 FILE *fp = state->errout;
15090 fprintf(fp, "basic_block: %10p [%2d] ( %10p - %10p )",
15091 block, block->vertex,
15092 block->first, block->last);
15093 for(edge = block->edges; edge; edge = edge->next) {
15094 fprintf(fp, " %10p [%2d]",
15095 edge->member ? edge->member->first : 0,
15096 edge->member ? edge->member->vertex : -1);
15105 static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
15106 void (*cb)(struct compile_state *state, struct block *block, void *arg),
15109 struct triple *ptr, *first;
15110 struct block *last_block;
15115 if (triple_stores_block(state, ptr)) {
15116 struct block *block;
15117 block = ptr->u.block;
15118 if (block && (block != last_block)) {
15119 cb(state, block, arg);
15121 last_block = block;
15124 } while(ptr != first);
15127 static void print_block(
15128 struct compile_state *state, struct block *block, void *arg)
15130 struct block_set *user, *edge;
15131 struct triple *ptr;
15134 fprintf(fp, "\nblock: %p (%d) ",
15138 for(edge = block->edges; edge; edge = edge->next) {
15139 fprintf(fp, " %p<-%p",
15141 (edge->member && edge->member->use)?
15142 edge->member->use->member : 0);
15145 if (block->first->op == OP_LABEL) {
15146 fprintf(fp, "%p:\n", block->first);
15148 for(ptr = block->first; ; ) {
15149 display_triple(fp, ptr);
15150 if (ptr == block->last)
15153 if (ptr == block->first) {
15154 internal_error(state, 0, "missing block last?");
15157 fprintf(fp, "users %d: ", block->users);
15158 for(user = block->use; user; user = user->next) {
15159 fprintf(fp, "%p (%d) ",
15161 user->member->vertex);
15163 fprintf(fp,"\n\n");
15167 static void romcc_print_blocks(struct compile_state *state, FILE *fp)
15169 fprintf(fp, "--------------- blocks ---------------\n");
15170 walk_blocks(state, &state->bb, print_block, fp);
15172 static void print_blocks(struct compile_state *state, const char *func, FILE *fp)
15174 static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb);
15175 static void print_dominance_frontiers(struct compile_state *state, FILE *fp, struct basic_blocks *bb);
15176 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15177 fprintf(fp, "After %s\n", func);
15178 romcc_print_blocks(state, fp);
15179 if (state->compiler->debug & DEBUG_FDOMINATORS) {
15180 print_dominators(state, fp, &state->bb);
15181 print_dominance_frontiers(state, fp, &state->bb);
15183 print_control_flow(state, fp, &state->bb);
15187 static void prune_nonblock_triples(struct compile_state *state,
15188 struct basic_blocks *bb)
15190 struct block *block;
15191 struct triple *first, *ins, *next;
15192 /* Delete the triples not in a basic block */
15198 if (ins->op == OP_LABEL) {
15199 block = ins->u.block;
15202 struct triple_set *use;
15203 for(use = ins->use; use; use = use->next) {
15204 struct block *block;
15205 block = block_of_triple(state, use->member);
15207 internal_error(state, ins, "pruning used ins?");
15210 release_triple(state, ins);
15212 if (block && block->last == ins) {
15216 } while(ins != first);
15219 static void setup_basic_blocks(struct compile_state *state,
15220 struct basic_blocks *bb)
15222 if (!triple_stores_block(state, bb->first)) {
15223 internal_error(state, 0, "ins will not store block?");
15225 /* Initialize the state */
15226 bb->first_block = bb->last_block = 0;
15227 bb->last_vertex = 0;
15228 free_basic_blocks(state, bb);
15230 /* Find the basic blocks */
15231 bb->first_block = basic_block(state, bb, bb->first);
15233 /* Be certain the last instruction of a function, or the
15234 * entire program is in a basic block. When it is not find
15235 * the start of the block, insert a label if necessary and build
15236 * basic block. Then add a fake edge from the start block
15237 * to the final block.
15239 if (!block_of_triple(state, bb->first->prev)) {
15240 struct triple *start;
15241 struct block *tail;
15242 start = triple_to_block_start(state, bb->first->prev);
15243 if (!triple_is_label(state, start)) {
15244 start = pre_triple(state,
15245 start, OP_LABEL, &void_type, 0, 0);
15247 tail = basic_block(state, bb, start);
15248 add_block_edge(bb->first_block, tail, 0);
15249 use_block(tail, bb->first_block);
15252 /* Find the last basic block.
15254 bb->last_block = block_of_triple(state, bb->first->prev);
15256 /* Delete the triples not in a basic block */
15257 prune_nonblock_triples(state, bb);
15260 /* If we are debugging print what I have just done */
15261 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15262 print_blocks(state, state->dbgout);
15263 print_control_flow(state, bb);
15269 struct sdom_block {
15270 struct block *block;
15271 struct sdom_block *sdominates;
15272 struct sdom_block *sdom_next;
15273 struct sdom_block *sdom;
15274 struct sdom_block *label;
15275 struct sdom_block *parent;
15276 struct sdom_block *ancestor;
15281 static void unsdom_block(struct sdom_block *block)
15283 struct sdom_block **ptr;
15284 if (!block->sdom_next) {
15287 ptr = &block->sdom->sdominates;
15289 if ((*ptr) == block) {
15290 *ptr = block->sdom_next;
15293 ptr = &(*ptr)->sdom_next;
15297 static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
15299 unsdom_block(block);
15300 block->sdom = sdom;
15301 block->sdom_next = sdom->sdominates;
15302 sdom->sdominates = block;
15307 static int initialize_sdblock(struct sdom_block *sd,
15308 struct block *parent, struct block *block, int vertex)
15310 struct block_set *edge;
15311 if (!block || (sd[block->vertex].block == block)) {
15315 /* Renumber the blocks in a convinient fashion */
15316 block->vertex = vertex;
15317 sd[vertex].block = block;
15318 sd[vertex].sdom = &sd[vertex];
15319 sd[vertex].label = &sd[vertex];
15320 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15321 sd[vertex].ancestor = 0;
15322 sd[vertex].vertex = vertex;
15323 for(edge = block->edges; edge; edge = edge->next) {
15324 vertex = initialize_sdblock(sd, block, edge->member, vertex);
15329 static int initialize_spdblock(
15330 struct compile_state *state, struct sdom_block *sd,
15331 struct block *parent, struct block *block, int vertex)
15333 struct block_set *user;
15334 if (!block || (sd[block->vertex].block == block)) {
15338 /* Renumber the blocks in a convinient fashion */
15339 block->vertex = vertex;
15340 sd[vertex].block = block;
15341 sd[vertex].sdom = &sd[vertex];
15342 sd[vertex].label = &sd[vertex];
15343 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15344 sd[vertex].ancestor = 0;
15345 sd[vertex].vertex = vertex;
15346 for(user = block->use; user; user = user->next) {
15347 vertex = initialize_spdblock(state, sd, block, user->member, vertex);
15352 static int setup_spdblocks(struct compile_state *state,
15353 struct basic_blocks *bb, struct sdom_block *sd)
15355 struct block *block;
15357 /* Setup as many sdpblocks as possible without using fake edges */
15358 vertex = initialize_spdblock(state, sd, 0, bb->last_block, 0);
15360 /* Walk through the graph and find unconnected blocks. Add a
15361 * fake edge from the unconnected blocks to the end of the
15364 block = bb->first_block->last->next->u.block;
15365 for(; block && block != bb->first_block; block = block->last->next->u.block) {
15366 if (sd[block->vertex].block == block) {
15369 #if DEBUG_SDP_BLOCKS
15371 FILE *fp = state->errout;
15372 fprintf(fp, "Adding %d\n", vertex +1);
15375 add_block_edge(block, bb->last_block, 0);
15376 use_block(bb->last_block, block);
15378 vertex = initialize_spdblock(state, sd, bb->last_block, block, vertex);
15383 static void compress_ancestors(struct sdom_block *v)
15385 /* This procedure assumes ancestor(v) != 0 */
15386 /* if (ancestor(ancestor(v)) != 0) {
15387 * compress(ancestor(ancestor(v)));
15388 * if (semi(label(ancestor(v))) < semi(label(v))) {
15389 * label(v) = label(ancestor(v));
15391 * ancestor(v) = ancestor(ancestor(v));
15394 if (!v->ancestor) {
15397 if (v->ancestor->ancestor) {
15398 compress_ancestors(v->ancestor->ancestor);
15399 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
15400 v->label = v->ancestor->label;
15402 v->ancestor = v->ancestor->ancestor;
15406 static void compute_sdom(struct compile_state *state,
15407 struct basic_blocks *bb, struct sdom_block *sd)
15411 * for each v <= pred(w) {
15413 * if (semi[u] < semi[w] {
15414 * semi[w] = semi[u];
15417 * add w to bucket(vertex(semi[w]));
15418 * LINK(parent(w), w);
15421 * for each v <= bucket(parent(w)) {
15422 * delete v from bucket(parent(w));
15424 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15427 for(i = bb->last_vertex; i >= 2; i--) {
15428 struct sdom_block *v, *parent, *next;
15429 struct block_set *user;
15430 struct block *block;
15431 block = sd[i].block;
15432 parent = sd[i].parent;
15434 for(user = block->use; user; user = user->next) {
15435 struct sdom_block *v, *u;
15436 v = &sd[user->member->vertex];
15437 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15438 if (u->sdom->vertex < sd[i].sdom->vertex) {
15439 sd[i].sdom = u->sdom;
15442 sdom_block(sd[i].sdom, &sd[i]);
15443 sd[i].ancestor = parent;
15445 for(v = parent->sdominates; v; v = next) {
15446 struct sdom_block *u;
15447 next = v->sdom_next;
15449 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
15450 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
15451 u->block : parent->block;
15456 static void compute_spdom(struct compile_state *state,
15457 struct basic_blocks *bb, struct sdom_block *sd)
15461 * for each v <= pred(w) {
15463 * if (semi[u] < semi[w] {
15464 * semi[w] = semi[u];
15467 * add w to bucket(vertex(semi[w]));
15468 * LINK(parent(w), w);
15471 * for each v <= bucket(parent(w)) {
15472 * delete v from bucket(parent(w));
15474 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15477 for(i = bb->last_vertex; i >= 2; i--) {
15478 struct sdom_block *u, *v, *parent, *next;
15479 struct block_set *edge;
15480 struct block *block;
15481 block = sd[i].block;
15482 parent = sd[i].parent;
15484 for(edge = block->edges; edge; edge = edge->next) {
15485 v = &sd[edge->member->vertex];
15486 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15487 if (u->sdom->vertex < sd[i].sdom->vertex) {
15488 sd[i].sdom = u->sdom;
15491 sdom_block(sd[i].sdom, &sd[i]);
15492 sd[i].ancestor = parent;
15494 for(v = parent->sdominates; v; v = next) {
15495 struct sdom_block *u;
15496 next = v->sdom_next;
15498 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
15499 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
15500 u->block : parent->block;
15505 static void compute_idom(struct compile_state *state,
15506 struct basic_blocks *bb, struct sdom_block *sd)
15509 for(i = 2; i <= bb->last_vertex; i++) {
15510 struct block *block;
15511 block = sd[i].block;
15512 if (block->idom->vertex != sd[i].sdom->vertex) {
15513 block->idom = block->idom->idom;
15515 idom_block(block->idom, block);
15517 sd[1].block->idom = 0;
15520 static void compute_ipdom(struct compile_state *state,
15521 struct basic_blocks *bb, struct sdom_block *sd)
15524 for(i = 2; i <= bb->last_vertex; i++) {
15525 struct block *block;
15526 block = sd[i].block;
15527 if (block->ipdom->vertex != sd[i].sdom->vertex) {
15528 block->ipdom = block->ipdom->ipdom;
15530 ipdom_block(block->ipdom, block);
15532 sd[1].block->ipdom = 0;
15536 * Every vertex of a flowgraph G = (V, E, r) except r has
15537 * a unique immediate dominator.
15538 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
15539 * rooted at r, called the dominator tree of G, such that
15540 * v dominates w if and only if v is a proper ancestor of w in
15541 * the dominator tree.
15544 * If v and w are vertices of G such that v <= w,
15545 * than any path from v to w must contain a common ancestor
15548 /* Lemma 2: For any vertex w != r, idom(w) -> w */
15549 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
15550 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
15552 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
15553 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
15556 * Let w != r and let u be a vertex for which sdom(u) is
15557 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15558 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
15560 /* Lemma 5: Let vertices v,w satisfy v -> w.
15561 * Then v -> idom(w) or idom(w) -> idom(v)
15564 static void find_immediate_dominators(struct compile_state *state,
15565 struct basic_blocks *bb)
15567 struct sdom_block *sd;
15568 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
15569 * vi > w for (1 <= i <= k - 1}
15572 * For any vertex w != r.
15574 * {v|(v,w) <= E and v < w } U
15575 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
15578 * Let w != r and let u be a vertex for which sdom(u) is
15579 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15581 * { sdom(w) if sdom(w) = sdom(u),
15583 * { idom(u) otherwise
15585 /* The algorithm consists of the following 4 steps.
15586 * Step 1. Carry out a depth-first search of the problem graph.
15587 * Number the vertices from 1 to N as they are reached during
15588 * the search. Initialize the variables used in succeeding steps.
15589 * Step 2. Compute the semidominators of all vertices by applying
15590 * theorem 4. Carry out the computation vertex by vertex in
15591 * decreasing order by number.
15592 * Step 3. Implicitly define the immediate dominator of each vertex
15593 * by applying Corollary 1.
15594 * Step 4. Explicitly define the immediate dominator of each vertex,
15595 * carrying out the computation vertex by vertex in increasing order
15598 /* Step 1 initialize the basic block information */
15599 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
15600 initialize_sdblock(sd, 0, bb->first_block, 0);
15606 /* Step 2 compute the semidominators */
15607 /* Step 3 implicitly define the immediate dominator of each vertex */
15608 compute_sdom(state, bb, sd);
15609 /* Step 4 explicitly define the immediate dominator of each vertex */
15610 compute_idom(state, bb, sd);
15614 static void find_post_dominators(struct compile_state *state,
15615 struct basic_blocks *bb)
15617 struct sdom_block *sd;
15619 /* Step 1 initialize the basic block information */
15620 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
15622 vertex = setup_spdblocks(state, bb, sd);
15623 if (vertex != bb->last_vertex) {
15624 internal_error(state, 0, "missing %d blocks",
15625 bb->last_vertex - vertex);
15628 /* Step 2 compute the semidominators */
15629 /* Step 3 implicitly define the immediate dominator of each vertex */
15630 compute_spdom(state, bb, sd);
15631 /* Step 4 explicitly define the immediate dominator of each vertex */
15632 compute_ipdom(state, bb, sd);
15638 static void find_block_domf(struct compile_state *state, struct block *block)
15640 struct block *child;
15641 struct block_set *user, *edge;
15642 if (block->domfrontier != 0) {
15643 internal_error(state, block->first, "domfrontier present?");
15645 for(user = block->idominates; user; user = user->next) {
15646 child = user->member;
15647 if (child->idom != block) {
15648 internal_error(state, block->first, "bad idom");
15650 find_block_domf(state, child);
15652 for(edge = block->edges; edge; edge = edge->next) {
15653 if (edge->member->idom != block) {
15654 domf_block(block, edge->member);
15657 for(user = block->idominates; user; user = user->next) {
15658 struct block_set *frontier;
15659 child = user->member;
15660 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
15661 if (frontier->member->idom != block) {
15662 domf_block(block, frontier->member);
15668 static void find_block_ipdomf(struct compile_state *state, struct block *block)
15670 struct block *child;
15671 struct block_set *user;
15672 if (block->ipdomfrontier != 0) {
15673 internal_error(state, block->first, "ipdomfrontier present?");
15675 for(user = block->ipdominates; user; user = user->next) {
15676 child = user->member;
15677 if (child->ipdom != block) {
15678 internal_error(state, block->first, "bad ipdom");
15680 find_block_ipdomf(state, child);
15682 for(user = block->use; user; user = user->next) {
15683 if (user->member->ipdom != block) {
15684 ipdomf_block(block, user->member);
15687 for(user = block->ipdominates; user; user = user->next) {
15688 struct block_set *frontier;
15689 child = user->member;
15690 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
15691 if (frontier->member->ipdom != block) {
15692 ipdomf_block(block, frontier->member);
15698 static void print_dominated(
15699 struct compile_state *state, struct block *block, void *arg)
15701 struct block_set *user;
15704 fprintf(fp, "%d:", block->vertex);
15705 for(user = block->idominates; user; user = user->next) {
15706 fprintf(fp, " %d", user->member->vertex);
15707 if (user->member->idom != block) {
15708 internal_error(state, user->member->first, "bad idom");
15714 static void print_dominated2(
15715 struct compile_state *state, FILE *fp, int depth, struct block *block)
15717 struct block_set *user;
15718 struct triple *ins;
15719 struct occurance *ptr, *ptr2;
15720 const char *filename1, *filename2;
15721 int equal_filenames;
15723 for(i = 0; i < depth; i++) {
15726 fprintf(fp, "%3d: %p (%p - %p) @",
15727 block->vertex, block, block->first, block->last);
15728 ins = block->first;
15729 while(ins != block->last && (ins->occurance->line == 0)) {
15732 ptr = ins->occurance;
15733 ptr2 = block->last->occurance;
15734 filename1 = ptr->filename? ptr->filename : "";
15735 filename2 = ptr2->filename? ptr2->filename : "";
15736 equal_filenames = (strcmp(filename1, filename2) == 0);
15737 if ((ptr == ptr2) || (equal_filenames && ptr->line == ptr2->line)) {
15738 fprintf(fp, " %s:%d", ptr->filename, ptr->line);
15739 } else if (equal_filenames) {
15740 fprintf(fp, " %s:(%d - %d)",
15741 ptr->filename, ptr->line, ptr2->line);
15743 fprintf(fp, " (%s:%d - %s:%d)",
15744 ptr->filename, ptr->line,
15745 ptr2->filename, ptr2->line);
15748 for(user = block->idominates; user; user = user->next) {
15749 print_dominated2(state, fp, depth + 1, user->member);
15753 static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb)
15755 fprintf(fp, "\ndominates\n");
15756 walk_blocks(state, bb, print_dominated, fp);
15757 fprintf(fp, "dominates\n");
15758 print_dominated2(state, fp, 0, bb->first_block);
15762 static int print_frontiers(
15763 struct compile_state *state, FILE *fp, struct block *block, int vertex)
15765 struct block_set *user, *edge;
15767 if (!block || (block->vertex != vertex + 1)) {
15772 fprintf(fp, "%d:", block->vertex);
15773 for(user = block->domfrontier; user; user = user->next) {
15774 fprintf(fp, " %d", user->member->vertex);
15778 for(edge = block->edges; edge; edge = edge->next) {
15779 vertex = print_frontiers(state, fp, edge->member, vertex);
15783 static void print_dominance_frontiers(struct compile_state *state,
15784 FILE *fp, struct basic_blocks *bb)
15786 fprintf(fp, "\ndominance frontiers\n");
15787 print_frontiers(state, fp, bb->first_block, 0);
15791 static void analyze_idominators(struct compile_state *state, struct basic_blocks *bb)
15793 /* Find the immediate dominators */
15794 find_immediate_dominators(state, bb);
15795 /* Find the dominance frontiers */
15796 find_block_domf(state, bb->first_block);
15797 /* If debuging print the print what I have just found */
15798 if (state->compiler->debug & DEBUG_FDOMINATORS) {
15799 print_dominators(state, state->dbgout, bb);
15800 print_dominance_frontiers(state, state->dbgout, bb);
15801 print_control_flow(state, state->dbgout, bb);
15806 static void print_ipdominated(
15807 struct compile_state *state, struct block *block, void *arg)
15809 struct block_set *user;
15812 fprintf(fp, "%d:", block->vertex);
15813 for(user = block->ipdominates; user; user = user->next) {
15814 fprintf(fp, " %d", user->member->vertex);
15815 if (user->member->ipdom != block) {
15816 internal_error(state, user->member->first, "bad ipdom");
15822 static void print_ipdominators(struct compile_state *state, FILE *fp,
15823 struct basic_blocks *bb)
15825 fprintf(fp, "\nipdominates\n");
15826 walk_blocks(state, bb, print_ipdominated, fp);
15829 static int print_pfrontiers(
15830 struct compile_state *state, FILE *fp, struct block *block, int vertex)
15832 struct block_set *user;
15834 if (!block || (block->vertex != vertex + 1)) {
15839 fprintf(fp, "%d:", block->vertex);
15840 for(user = block->ipdomfrontier; user; user = user->next) {
15841 fprintf(fp, " %d", user->member->vertex);
15844 for(user = block->use; user; user = user->next) {
15845 vertex = print_pfrontiers(state, fp, user->member, vertex);
15849 static void print_ipdominance_frontiers(struct compile_state *state,
15850 FILE *fp, struct basic_blocks *bb)
15852 fprintf(fp, "\nipdominance frontiers\n");
15853 print_pfrontiers(state, fp, bb->last_block, 0);
15857 static void analyze_ipdominators(struct compile_state *state,
15858 struct basic_blocks *bb)
15860 /* Find the post dominators */
15861 find_post_dominators(state, bb);
15862 /* Find the control dependencies (post dominance frontiers) */
15863 find_block_ipdomf(state, bb->last_block);
15864 /* If debuging print the print what I have just found */
15865 if (state->compiler->debug & DEBUG_RDOMINATORS) {
15866 print_ipdominators(state, state->dbgout, bb);
15867 print_ipdominance_frontiers(state, state->dbgout, bb);
15868 print_control_flow(state, state->dbgout, bb);
15872 static int bdominates(struct compile_state *state,
15873 struct block *dom, struct block *sub)
15875 while(sub && (sub != dom)) {
15881 static int tdominates(struct compile_state *state,
15882 struct triple *dom, struct triple *sub)
15884 struct block *bdom, *bsub;
15886 bdom = block_of_triple(state, dom);
15887 bsub = block_of_triple(state, sub);
15888 if (bdom != bsub) {
15889 result = bdominates(state, bdom, bsub);
15892 struct triple *ins;
15893 if (!bdom || !bsub) {
15894 internal_error(state, dom, "huh?");
15897 while((ins != bsub->first) && (ins != dom)) {
15900 result = (ins == dom);
15905 static void analyze_basic_blocks(
15906 struct compile_state *state, struct basic_blocks *bb)
15908 setup_basic_blocks(state, bb);
15909 analyze_idominators(state, bb);
15910 analyze_ipdominators(state, bb);
15913 static void insert_phi_operations(struct compile_state *state)
15916 struct triple *first;
15917 int *has_already, *work;
15918 struct block *work_list, **work_list_tail;
15920 struct triple *var, *vnext;
15922 size = sizeof(int) * (state->bb.last_vertex + 1);
15923 has_already = xcmalloc(size, "has_already");
15924 work = xcmalloc(size, "work");
15927 first = state->first;
15928 for(var = first->next; var != first ; var = vnext) {
15929 struct block *block;
15930 struct triple_set *user, *unext;
15933 if (!triple_is_auto_var(state, var) || !var->use) {
15939 work_list_tail = &work_list;
15940 for(user = var->use; user; user = unext) {
15941 unext = user->next;
15942 if (MISC(var, 0) == user->member) {
15945 if (user->member->op == OP_READ) {
15948 if (user->member->op != OP_WRITE) {
15949 internal_error(state, user->member,
15950 "bad variable access");
15952 block = user->member->u.block;
15954 warning(state, user->member, "dead code");
15955 release_triple(state, user->member);
15958 if (work[block->vertex] >= iter) {
15961 work[block->vertex] = iter;
15962 *work_list_tail = block;
15963 block->work_next = 0;
15964 work_list_tail = &block->work_next;
15966 for(block = work_list; block; block = block->work_next) {
15967 struct block_set *df;
15968 for(df = block->domfrontier; df; df = df->next) {
15969 struct triple *phi;
15970 struct block *front;
15972 front = df->member;
15974 if (has_already[front->vertex] >= iter) {
15977 /* Count how many edges flow into this block */
15978 in_edges = front->users;
15979 /* Insert a phi function for this variable */
15980 get_occurance(var->occurance);
15981 phi = alloc_triple(
15982 state, OP_PHI, var->type, -1, in_edges,
15984 phi->u.block = front;
15985 MISC(phi, 0) = var;
15986 use_triple(var, phi);
15988 if (phi->rhs != in_edges) {
15989 internal_error(state, phi, "phi->rhs: %d != in_edges: %d",
15990 phi->rhs, in_edges);
15993 /* Insert the phi functions immediately after the label */
15994 insert_triple(state, front->first->next, phi);
15995 if (front->first == front->last) {
15996 front->last = front->first->next;
15998 has_already[front->vertex] = iter;
15999 transform_to_arch_instruction(state, phi);
16001 /* If necessary plan to visit the basic block */
16002 if (work[front->vertex] >= iter) {
16005 work[front->vertex] = iter;
16006 *work_list_tail = front;
16007 front->work_next = 0;
16008 work_list_tail = &front->work_next;
16012 xfree(has_already);
16018 struct triple_set *top;
16022 static int count_auto_vars(struct compile_state *state)
16024 struct triple *first, *ins;
16026 first = state->first;
16029 if (triple_is_auto_var(state, ins)) {
16033 } while(ins != first);
16037 static void number_auto_vars(struct compile_state *state, struct stack *stacks)
16039 struct triple *first, *ins;
16041 first = state->first;
16044 if (triple_is_auto_var(state, ins)) {
16046 stacks[auto_vars].orig_id = ins->id;
16047 ins->id = auto_vars;
16050 } while(ins != first);
16053 static void restore_auto_vars(struct compile_state *state, struct stack *stacks)
16055 struct triple *first, *ins;
16056 first = state->first;
16059 if (triple_is_auto_var(state, ins)) {
16060 ins->id = stacks[ins->id].orig_id;
16063 } while(ins != first);
16066 static struct triple *peek_triple(struct stack *stacks, struct triple *var)
16068 struct triple_set *head;
16069 struct triple *top_val;
16071 head = stacks[var->id].top;
16073 top_val = head->member;
16078 static void push_triple(struct stack *stacks, struct triple *var, struct triple *val)
16080 struct triple_set *new;
16081 /* Append new to the head of the list,
16082 * it's the only sensible behavoir for a stack.
16084 new = xcmalloc(sizeof(*new), "triple_set");
16086 new->next = stacks[var->id].top;
16087 stacks[var->id].top = new;
16090 static void pop_triple(struct stack *stacks, struct triple *var, struct triple *oldval)
16092 struct triple_set *set, **ptr;
16093 ptr = &stacks[var->id].top;
16096 if (set->member == oldval) {
16099 /* Only free one occurance from the stack */
16112 static void fixup_block_phi_variables(
16113 struct compile_state *state, struct stack *stacks, struct block *parent, struct block *block)
16115 struct block_set *set;
16116 struct triple *ptr;
16118 if (!parent || !block)
16120 /* Find the edge I am coming in on */
16122 for(set = block->use; set; set = set->next, edge++) {
16123 if (set->member == parent) {
16128 internal_error(state, 0, "phi input is not on a control predecessor");
16130 for(ptr = block->first; ; ptr = ptr->next) {
16131 if (ptr->op == OP_PHI) {
16132 struct triple *var, *val, **slot;
16133 var = MISC(ptr, 0);
16135 internal_error(state, ptr, "no var???");
16137 /* Find the current value of the variable */
16138 val = peek_triple(stacks, var);
16139 if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
16140 internal_error(state, val, "bad value in phi");
16142 if (edge >= ptr->rhs) {
16143 internal_error(state, ptr, "edges > phi rhs");
16145 slot = &RHS(ptr, edge);
16146 if ((*slot != 0) && (*slot != val)) {
16147 internal_error(state, ptr, "phi already bound on this edge");
16150 use_triple(val, ptr);
16152 if (ptr == block->last) {
16159 static void rename_block_variables(
16160 struct compile_state *state, struct stack *stacks, struct block *block)
16162 struct block_set *user, *edge;
16163 struct triple *ptr, *next, *last;
16167 last = block->first;
16169 for(ptr = block->first; !done; ptr = next) {
16171 if (ptr == block->last) {
16175 if (ptr->op == OP_READ) {
16176 struct triple *var, *val;
16178 if (!triple_is_auto_var(state, var)) {
16179 internal_error(state, ptr, "read of non auto var!");
16181 unuse_triple(var, ptr);
16182 /* Find the current value of the variable */
16183 val = peek_triple(stacks, var);
16185 /* Let the optimizer at variables that are not initially
16186 * set. But give it a bogus value so things seem to
16187 * work by accident. This is useful for bitfields because
16188 * setting them always involves a read-modify-write.
16190 if (TYPE_ARITHMETIC(ptr->type->type)) {
16191 val = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16192 val->u.cval = 0xdeadbeaf;
16194 val = pre_triple(state, ptr, OP_UNKNOWNVAL, ptr->type, 0, 0);
16198 error(state, ptr, "variable used without being set");
16200 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
16201 internal_error(state, val, "bad value in read");
16203 propogate_use(state, ptr, val);
16204 release_triple(state, ptr);
16208 if (ptr->op == OP_WRITE) {
16209 struct triple *var, *val, *tval;
16210 var = MISC(ptr, 0);
16211 if (!triple_is_auto_var(state, var)) {
16212 internal_error(state, ptr, "write to non auto var!");
16214 tval = val = RHS(ptr, 0);
16215 if ((val->op == OP_WRITE) || (val->op == OP_READ) ||
16216 triple_is_auto_var(state, val)) {
16217 internal_error(state, ptr, "bad value in write");
16219 /* Insert a cast if the types differ */
16220 if (!is_subset_type(ptr->type, val->type)) {
16221 if (val->op == OP_INTCONST) {
16222 tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16223 tval->u.cval = val->u.cval;
16226 tval = pre_triple(state, ptr, OP_CONVERT, ptr->type, val, 0);
16227 use_triple(val, tval);
16229 transform_to_arch_instruction(state, tval);
16230 unuse_triple(val, ptr);
16231 RHS(ptr, 0) = tval;
16232 use_triple(tval, ptr);
16234 propogate_use(state, ptr, tval);
16235 unuse_triple(var, ptr);
16236 /* Push OP_WRITE ptr->right onto a stack of variable uses */
16237 push_triple(stacks, var, tval);
16239 if (ptr->op == OP_PHI) {
16240 struct triple *var;
16241 var = MISC(ptr, 0);
16242 if (!triple_is_auto_var(state, var)) {
16243 internal_error(state, ptr, "phi references non auto var!");
16245 /* Push OP_PHI onto a stack of variable uses */
16246 push_triple(stacks, var, ptr);
16250 block->last = last;
16252 /* Fixup PHI functions in the cf successors */
16253 for(edge = block->edges; edge; edge = edge->next) {
16254 fixup_block_phi_variables(state, stacks, block, edge->member);
16256 /* rename variables in the dominated nodes */
16257 for(user = block->idominates; user; user = user->next) {
16258 rename_block_variables(state, stacks, user->member);
16260 /* pop the renamed variable stack */
16261 last = block->first;
16263 for(ptr = block->first; !done ; ptr = next) {
16265 if (ptr == block->last) {
16268 if (ptr->op == OP_WRITE) {
16269 struct triple *var;
16270 var = MISC(ptr, 0);
16271 /* Pop OP_WRITE ptr->right from the stack of variable uses */
16272 pop_triple(stacks, var, RHS(ptr, 0));
16273 release_triple(state, ptr);
16276 if (ptr->op == OP_PHI) {
16277 struct triple *var;
16278 var = MISC(ptr, 0);
16279 /* Pop OP_WRITE ptr->right from the stack of variable uses */
16280 pop_triple(stacks, var, ptr);
16284 block->last = last;
16287 static void rename_variables(struct compile_state *state)
16289 struct stack *stacks;
16292 /* Allocate stacks for the Variables */
16293 auto_vars = count_auto_vars(state);
16294 stacks = xcmalloc(sizeof(stacks[0])*(auto_vars + 1), "auto var stacks");
16296 /* Give each auto_var a stack */
16297 number_auto_vars(state, stacks);
16299 /* Rename the variables */
16300 rename_block_variables(state, stacks, state->bb.first_block);
16302 /* Remove the stacks from the auto_vars */
16303 restore_auto_vars(state, stacks);
16307 static void prune_block_variables(struct compile_state *state,
16308 struct block *block)
16310 struct block_set *user;
16311 struct triple *next, *ptr;
16315 for(ptr = block->first; !done; ptr = next) {
16316 /* Be extremely careful I am deleting the list
16317 * as I walk trhough it.
16320 if (ptr == block->last) {
16323 if (triple_is_auto_var(state, ptr)) {
16324 struct triple_set *user, *next;
16325 for(user = ptr->use; user; user = next) {
16326 struct triple *use;
16328 use = user->member;
16329 if (MISC(ptr, 0) == user->member) {
16332 if (use->op != OP_PHI) {
16333 internal_error(state, use, "decl still used");
16335 if (MISC(use, 0) != ptr) {
16336 internal_error(state, use, "bad phi use of decl");
16338 unuse_triple(ptr, use);
16341 if ((ptr->u.cval == 0) && (MISC(ptr, 0)->lhs == 1)) {
16342 /* Delete the adecl */
16343 release_triple(state, MISC(ptr, 0));
16344 /* And the piece */
16345 release_triple(state, ptr);
16350 for(user = block->idominates; user; user = user->next) {
16351 prune_block_variables(state, user->member);
16355 struct phi_triple {
16356 struct triple *phi;
16361 static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi)
16363 struct triple **slot;
16365 if (live[phi->id].alive) {
16368 live[phi->id].alive = 1;
16370 slot = &RHS(phi, 0);
16371 for(i = 0; i < zrhs; i++) {
16372 struct triple *used;
16374 if (used && (used->op == OP_PHI)) {
16375 keep_phi(state, live, used);
16380 static void prune_unused_phis(struct compile_state *state)
16382 struct triple *first, *phi;
16383 struct phi_triple *live;
16386 /* Find the first instruction */
16387 first = state->first;
16389 /* Count how many phi functions I need to process */
16391 for(phi = first->next; phi != first; phi = phi->next) {
16392 if (phi->op == OP_PHI) {
16397 /* Mark them all dead */
16398 live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple");
16400 for(phi = first->next; phi != first; phi = phi->next) {
16401 if (phi->op != OP_PHI) {
16404 live[phis].alive = 0;
16405 live[phis].orig_id = phi->id;
16406 live[phis].phi = phi;
16411 /* Mark phis alive that are used by non phis */
16412 for(i = 0; i < phis; i++) {
16413 struct triple_set *set;
16414 for(set = live[i].phi->use; !live[i].alive && set; set = set->next) {
16415 if (set->member->op != OP_PHI) {
16416 keep_phi(state, live, live[i].phi);
16422 /* Delete the extraneous phis */
16423 for(i = 0; i < phis; i++) {
16424 struct triple **slot;
16426 if (!live[i].alive) {
16427 release_triple(state, live[i].phi);
16431 slot = &RHS(phi, 0);
16433 for(j = 0; j < zrhs; j++) {
16435 struct triple *unknown;
16436 get_occurance(phi->occurance);
16437 unknown = flatten(state, state->global_pool,
16438 alloc_triple(state, OP_UNKNOWNVAL,
16439 phi->type, 0, 0, phi->occurance));
16441 use_triple(unknown, phi);
16442 transform_to_arch_instruction(state, unknown);
16444 warning(state, phi, "variable not set at index %d on all paths to use", j);
16452 static void transform_to_ssa_form(struct compile_state *state)
16454 insert_phi_operations(state);
16455 rename_variables(state);
16457 prune_block_variables(state, state->bb.first_block);
16458 prune_unused_phis(state);
16460 print_blocks(state, __func__, state->dbgout);
16464 static void clear_vertex(
16465 struct compile_state *state, struct block *block, void *arg)
16467 /* Clear the current blocks vertex and the vertex of all
16468 * of the current blocks neighbors in case there are malformed
16469 * blocks with now instructions at this point.
16471 struct block_set *user, *edge;
16473 for(edge = block->edges; edge; edge = edge->next) {
16474 edge->member->vertex = 0;
16476 for(user = block->use; user; user = user->next) {
16477 user->member->vertex = 0;
16481 static void mark_live_block(
16482 struct compile_state *state, struct block *block, int *next_vertex)
16484 /* See if this is a block that has not been marked */
16485 if (block->vertex != 0) {
16488 block->vertex = *next_vertex;
16490 if (triple_is_branch(state, block->last)) {
16491 struct triple **targ;
16492 targ = triple_edge_targ(state, block->last, 0);
16493 for(; targ; targ = triple_edge_targ(state, block->last, targ)) {
16497 if (!triple_stores_block(state, *targ)) {
16498 internal_error(state, 0, "bad targ");
16500 mark_live_block(state, (*targ)->u.block, next_vertex);
16502 /* Ensure the last block of a function remains alive */
16503 if (triple_is_call(state, block->last)) {
16504 mark_live_block(state, MISC(block->last, 0)->u.block, next_vertex);
16507 else if (block->last->next != state->first) {
16508 struct triple *ins;
16509 ins = block->last->next;
16510 if (!triple_stores_block(state, ins)) {
16511 internal_error(state, 0, "bad block start");
16513 mark_live_block(state, ins->u.block, next_vertex);
16517 static void transform_from_ssa_form(struct compile_state *state)
16519 /* To get out of ssa form we insert moves on the incoming
16520 * edges to blocks containting phi functions.
16522 struct triple *first;
16523 struct triple *phi, *var, *next;
16526 /* Walk the control flow to see which blocks remain alive */
16527 walk_blocks(state, &state->bb, clear_vertex, 0);
16529 mark_live_block(state, state->bb.first_block, &next_vertex);
16531 /* Walk all of the operations to find the phi functions */
16532 first = state->first;
16533 for(phi = first->next; phi != first ; phi = next) {
16534 struct block_set *set;
16535 struct block *block;
16536 struct triple **slot;
16537 struct triple *var;
16538 struct triple_set *use, *use_next;
16539 int edge, writers, readers;
16541 if (phi->op != OP_PHI) {
16545 block = phi->u.block;
16546 slot = &RHS(phi, 0);
16548 /* If this phi is in a dead block just forget it */
16549 if (block->vertex == 0) {
16550 release_triple(state, phi);
16554 /* Forget uses from code in dead blocks */
16555 for(use = phi->use; use; use = use_next) {
16556 struct block *ublock;
16557 struct triple **expr;
16558 use_next = use->next;
16559 ublock = block_of_triple(state, use->member);
16560 if ((use->member == phi) || (ublock->vertex != 0)) {
16563 expr = triple_rhs(state, use->member, 0);
16564 for(; expr; expr = triple_rhs(state, use->member, expr)) {
16565 if (*expr == phi) {
16569 unuse_triple(phi, use->member);
16571 /* A variable to replace the phi function */
16572 if (registers_of(state, phi->type) != 1) {
16573 internal_error(state, phi, "phi->type does not fit in a single register!");
16575 var = post_triple(state, phi, OP_ADECL, phi->type, 0, 0);
16576 var = var->next; /* point at the var */
16578 /* Replaces use of phi with var */
16579 propogate_use(state, phi, var);
16581 /* Count the readers */
16583 for(use = var->use; use; use = use->next) {
16584 if (use->member != MISC(var, 0)) {
16589 /* Walk all of the incoming edges/blocks and insert moves.
16592 for(edge = 0, set = block->use; set; set = set->next, edge++) {
16593 struct block *eblock, *vblock;
16594 struct triple *move;
16595 struct triple *val, *base;
16596 eblock = set->member;
16599 unuse_triple(val, phi);
16600 vblock = block_of_triple(state, val);
16602 /* If we don't have a value that belongs in an OP_WRITE
16605 if (!val || (val == &unknown_triple) || (val == phi)
16606 || (vblock && (vblock->vertex == 0))) {
16609 /* If the value should never occur error */
16611 internal_error(state, val, "no vblock?");
16615 /* If the value occurs in a dead block see if a replacement
16616 * block can be found.
16618 while(eblock && (eblock->vertex == 0)) {
16619 eblock = eblock->idom;
16621 /* If not continue on with the next value. */
16622 if (!eblock || (eblock->vertex == 0)) {
16626 /* If we have an empty incoming block ignore it. */
16627 if (!eblock->first) {
16628 internal_error(state, 0, "empty block?");
16631 /* Make certain the write is placed in the edge block... */
16632 /* Walk through the edge block backwards to find an
16633 * appropriate location for the OP_WRITE.
16635 for(base = eblock->last; base != eblock->first; base = base->prev) {
16636 struct triple **expr;
16637 if (base->op == OP_PIECE) {
16638 base = MISC(base, 0);
16640 if ((base == var) || (base == val)) {
16643 expr = triple_lhs(state, base, 0);
16644 for(; expr; expr = triple_lhs(state, base, expr)) {
16645 if ((*expr) == val) {
16649 expr = triple_rhs(state, base, 0);
16650 for(; expr; expr = triple_rhs(state, base, expr)) {
16651 if ((*expr) == var) {
16657 if (triple_is_branch(state, base)) {
16658 internal_error(state, base,
16659 "Could not insert write to phi");
16661 move = post_triple(state, base, OP_WRITE, var->type, val, var);
16662 use_triple(val, move);
16663 use_triple(var, move);
16666 if (!writers && readers) {
16667 internal_error(state, var, "no value written to in use phi?");
16669 /* If var is not used free it */
16671 release_triple(state, MISC(var, 0));
16672 release_triple(state, var);
16674 /* Release the phi function */
16675 release_triple(state, phi);
16678 /* Walk all of the operations to find the adecls */
16679 for(var = first->next; var != first ; var = var->next) {
16680 struct triple_set *use, *use_next;
16681 if (!triple_is_auto_var(state, var)) {
16685 /* Walk through all of the rhs uses of var and
16686 * replace them with read of var.
16688 for(use = var->use; use; use = use_next) {
16689 struct triple *read, *user;
16690 struct triple **slot;
16692 use_next = use->next;
16693 user = use->member;
16695 /* Generate a read of var */
16696 read = pre_triple(state, user, OP_READ, var->type, var, 0);
16697 use_triple(var, read);
16699 /* Find the rhs uses and see if they need to be replaced */
16702 slot = &RHS(user, 0);
16703 for(i = 0; i < zrhs; i++) {
16704 if (slot[i] == var) {
16709 /* If we did use it cleanup the uses */
16711 unuse_triple(var, user);
16712 use_triple(read, user);
16714 /* If we didn't use it release the extra triple */
16716 release_triple(state, read);
16722 #define HI() if (state->compiler->debug & DEBUG_REBUILD_SSA_FORM) { \
16723 FILE *fp = state->dbgout; \
16724 fprintf(fp, "@ %s:%d\n", __FILE__, __LINE__); romcc_print_blocks(state, fp); \
16727 static void rebuild_ssa_form(struct compile_state *state)
16730 transform_from_ssa_form(state);
16732 state->bb.first = state->first;
16733 free_basic_blocks(state, &state->bb);
16734 analyze_basic_blocks(state, &state->bb);
16736 insert_phi_operations(state);
16738 rename_variables(state);
16741 prune_block_variables(state, state->bb.first_block);
16743 prune_unused_phis(state);
16749 * Register conflict resolution
16750 * =========================================================
16753 static struct reg_info find_def_color(
16754 struct compile_state *state, struct triple *def)
16756 struct triple_set *set;
16757 struct reg_info info;
16758 info.reg = REG_UNSET;
16760 if (!triple_is_def(state, def)) {
16763 info = arch_reg_lhs(state, def, 0);
16764 if (info.reg >= MAX_REGISTERS) {
16765 info.reg = REG_UNSET;
16767 for(set = def->use; set; set = set->next) {
16768 struct reg_info tinfo;
16770 i = find_rhs_use(state, set->member, def);
16774 tinfo = arch_reg_rhs(state, set->member, i);
16775 if (tinfo.reg >= MAX_REGISTERS) {
16776 tinfo.reg = REG_UNSET;
16778 if ((tinfo.reg != REG_UNSET) &&
16779 (info.reg != REG_UNSET) &&
16780 (tinfo.reg != info.reg)) {
16781 internal_error(state, def, "register conflict");
16783 if ((info.regcm & tinfo.regcm) == 0) {
16784 internal_error(state, def, "regcm conflict %x & %x == 0",
16785 info.regcm, tinfo.regcm);
16787 if (info.reg == REG_UNSET) {
16788 info.reg = tinfo.reg;
16790 info.regcm &= tinfo.regcm;
16792 if (info.reg >= MAX_REGISTERS) {
16793 internal_error(state, def, "register out of range");
16798 static struct reg_info find_lhs_pre_color(
16799 struct compile_state *state, struct triple *ins, int index)
16801 struct reg_info info;
16805 if (!zlhs && triple_is_def(state, ins)) {
16808 if (index >= zlhs) {
16809 internal_error(state, ins, "Bad lhs %d", index);
16811 info = arch_reg_lhs(state, ins, index);
16812 for(i = 0; i < zrhs; i++) {
16813 struct reg_info rinfo;
16814 rinfo = arch_reg_rhs(state, ins, i);
16815 if ((info.reg == rinfo.reg) &&
16816 (rinfo.reg >= MAX_REGISTERS)) {
16817 struct reg_info tinfo;
16818 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
16819 info.reg = tinfo.reg;
16820 info.regcm &= tinfo.regcm;
16824 if (info.reg >= MAX_REGISTERS) {
16825 info.reg = REG_UNSET;
16830 static struct reg_info find_rhs_post_color(
16831 struct compile_state *state, struct triple *ins, int index);
16833 static struct reg_info find_lhs_post_color(
16834 struct compile_state *state, struct triple *ins, int index)
16836 struct triple_set *set;
16837 struct reg_info info;
16838 struct triple *lhs;
16839 #if DEBUG_TRIPLE_COLOR
16840 fprintf(state->errout, "find_lhs_post_color(%p, %d)\n",
16843 if ((index == 0) && triple_is_def(state, ins)) {
16846 else if (index < ins->lhs) {
16847 lhs = LHS(ins, index);
16850 internal_error(state, ins, "Bad lhs %d", index);
16853 info = arch_reg_lhs(state, ins, index);
16854 if (info.reg >= MAX_REGISTERS) {
16855 info.reg = REG_UNSET;
16857 for(set = lhs->use; set; set = set->next) {
16858 struct reg_info rinfo;
16859 struct triple *user;
16861 user = set->member;
16863 for(i = 0; i < zrhs; i++) {
16864 if (RHS(user, i) != lhs) {
16867 rinfo = find_rhs_post_color(state, user, i);
16868 if ((info.reg != REG_UNSET) &&
16869 (rinfo.reg != REG_UNSET) &&
16870 (info.reg != rinfo.reg)) {
16871 internal_error(state, ins, "register conflict");
16873 if ((info.regcm & rinfo.regcm) == 0) {
16874 internal_error(state, ins, "regcm conflict %x & %x == 0",
16875 info.regcm, rinfo.regcm);
16877 if (info.reg == REG_UNSET) {
16878 info.reg = rinfo.reg;
16880 info.regcm &= rinfo.regcm;
16883 #if DEBUG_TRIPLE_COLOR
16884 fprintf(state->errout, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
16885 ins, index, info.reg, info.regcm);
16890 static struct reg_info find_rhs_post_color(
16891 struct compile_state *state, struct triple *ins, int index)
16893 struct reg_info info, rinfo;
16895 #if DEBUG_TRIPLE_COLOR
16896 fprintf(state->errout, "find_rhs_post_color(%p, %d)\n",
16899 rinfo = arch_reg_rhs(state, ins, index);
16901 if (!zlhs && triple_is_def(state, ins)) {
16905 if (info.reg >= MAX_REGISTERS) {
16906 info.reg = REG_UNSET;
16908 for(i = 0; i < zlhs; i++) {
16909 struct reg_info linfo;
16910 linfo = arch_reg_lhs(state, ins, i);
16911 if ((linfo.reg == rinfo.reg) &&
16912 (linfo.reg >= MAX_REGISTERS)) {
16913 struct reg_info tinfo;
16914 tinfo = find_lhs_post_color(state, ins, i);
16915 if (tinfo.reg >= MAX_REGISTERS) {
16916 tinfo.reg = REG_UNSET;
16918 info.regcm &= linfo.regcm;
16919 info.regcm &= tinfo.regcm;
16920 if (info.reg != REG_UNSET) {
16921 internal_error(state, ins, "register conflict");
16923 if (info.regcm == 0) {
16924 internal_error(state, ins, "regcm conflict");
16926 info.reg = tinfo.reg;
16929 #if DEBUG_TRIPLE_COLOR
16930 fprintf(state->errout, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
16931 ins, index, info.reg, info.regcm);
16936 static struct reg_info find_lhs_color(
16937 struct compile_state *state, struct triple *ins, int index)
16939 struct reg_info pre, post, info;
16940 #if DEBUG_TRIPLE_COLOR
16941 fprintf(state->errout, "find_lhs_color(%p, %d)\n",
16944 pre = find_lhs_pre_color(state, ins, index);
16945 post = find_lhs_post_color(state, ins, index);
16946 if ((pre.reg != post.reg) &&
16947 (pre.reg != REG_UNSET) &&
16948 (post.reg != REG_UNSET)) {
16949 internal_error(state, ins, "register conflict");
16951 info.regcm = pre.regcm & post.regcm;
16952 info.reg = pre.reg;
16953 if (info.reg == REG_UNSET) {
16954 info.reg = post.reg;
16956 #if DEBUG_TRIPLE_COLOR
16957 fprintf(state->errout, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
16958 ins, index, info.reg, info.regcm,
16959 pre.reg, pre.regcm, post.reg, post.regcm);
16964 static struct triple *post_copy(struct compile_state *state, struct triple *ins)
16966 struct triple_set *entry, *next;
16967 struct triple *out;
16968 struct reg_info info, rinfo;
16970 info = arch_reg_lhs(state, ins, 0);
16971 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
16972 use_triple(RHS(out, 0), out);
16973 /* Get the users of ins to use out instead */
16974 for(entry = ins->use; entry; entry = next) {
16976 next = entry->next;
16977 if (entry->member == out) {
16980 i = find_rhs_use(state, entry->member, ins);
16984 rinfo = arch_reg_rhs(state, entry->member, i);
16985 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
16988 replace_rhs_use(state, ins, out, entry->member);
16990 transform_to_arch_instruction(state, out);
16994 static struct triple *typed_pre_copy(
16995 struct compile_state *state, struct type *type, struct triple *ins, int index)
16997 /* Carefully insert enough operations so that I can
16998 * enter any operation with a GPR32.
17001 struct triple **expr;
17003 struct reg_info info;
17005 if (ins->op == OP_PHI) {
17006 internal_error(state, ins, "pre_copy on a phi?");
17008 classes = arch_type_to_regcm(state, type);
17009 info = arch_reg_rhs(state, ins, index);
17010 expr = &RHS(ins, index);
17011 if ((info.regcm & classes) == 0) {
17012 FILE *fp = state->errout;
17013 fprintf(fp, "src_type: ");
17014 name_of(fp, ins->type);
17015 fprintf(fp, "\ndst_type: ");
17018 internal_error(state, ins, "pre_copy with no register classes");
17021 if (!equiv_types(type, (*expr)->type)) {
17024 in = pre_triple(state, ins, op, type, *expr, 0);
17025 unuse_triple(*expr, ins);
17027 use_triple(RHS(in, 0), in);
17028 use_triple(in, ins);
17029 transform_to_arch_instruction(state, in);
17033 static struct triple *pre_copy(
17034 struct compile_state *state, struct triple *ins, int index)
17036 return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
17040 static void insert_copies_to_phi(struct compile_state *state)
17042 /* To get out of ssa form we insert moves on the incoming
17043 * edges to blocks containting phi functions.
17045 struct triple *first;
17046 struct triple *phi;
17048 /* Walk all of the operations to find the phi functions */
17049 first = state->first;
17050 for(phi = first->next; phi != first ; phi = phi->next) {
17051 struct block_set *set;
17052 struct block *block;
17053 struct triple **slot, *copy;
17055 if (phi->op != OP_PHI) {
17058 phi->id |= TRIPLE_FLAG_POST_SPLIT;
17059 block = phi->u.block;
17060 slot = &RHS(phi, 0);
17061 /* Phi's that feed into mandatory live range joins
17062 * cause nasty complications. Insert a copy of
17063 * the phi value so I never have to deal with
17064 * that in the rest of the code.
17066 copy = post_copy(state, phi);
17067 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
17068 /* Walk all of the incoming edges/blocks and insert moves.
17070 for(edge = 0, set = block->use; set; set = set->next, edge++) {
17071 struct block *eblock;
17072 struct triple *move;
17073 struct triple *val;
17074 struct triple *ptr;
17075 eblock = set->member;
17082 get_occurance(val->occurance);
17083 move = build_triple(state, OP_COPY, val->type, val, 0,
17085 move->u.block = eblock;
17086 move->id |= TRIPLE_FLAG_PRE_SPLIT;
17087 use_triple(val, move);
17090 unuse_triple(val, phi);
17091 use_triple(move, phi);
17093 /* Walk up the dominator tree until I have found the appropriate block */
17094 while(eblock && !tdominates(state, val, eblock->last)) {
17095 eblock = eblock->idom;
17098 internal_error(state, phi, "Cannot find block dominated by %p",
17102 /* Walk through the block backwards to find
17103 * an appropriate location for the OP_COPY.
17105 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
17106 struct triple **expr;
17107 if (ptr->op == OP_PIECE) {
17108 ptr = MISC(ptr, 0);
17110 if ((ptr == phi) || (ptr == val)) {
17113 expr = triple_lhs(state, ptr, 0);
17114 for(;expr; expr = triple_lhs(state, ptr, expr)) {
17115 if ((*expr) == val) {
17119 expr = triple_rhs(state, ptr, 0);
17120 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17121 if ((*expr) == phi) {
17127 if (triple_is_branch(state, ptr)) {
17128 internal_error(state, ptr,
17129 "Could not insert write to phi");
17131 insert_triple(state, after_lhs(state, ptr), move);
17132 if (eblock->last == after_lhs(state, ptr)->prev) {
17133 eblock->last = move;
17135 transform_to_arch_instruction(state, move);
17138 print_blocks(state, __func__, state->dbgout);
17141 struct triple_reg_set;
17145 static int do_triple_set(struct triple_reg_set **head,
17146 struct triple *member, struct triple *new_member)
17148 struct triple_reg_set **ptr, *new;
17153 if ((*ptr)->member == member) {
17156 ptr = &(*ptr)->next;
17158 new = xcmalloc(sizeof(*new), "triple_set");
17159 new->member = member;
17160 new->new = new_member;
17166 static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
17168 struct triple_reg_set *entry, **ptr;
17172 if (entry->member == member) {
17173 *ptr = entry->next;
17178 ptr = &entry->next;
17183 static int in_triple(struct reg_block *rb, struct triple *in)
17185 return do_triple_set(&rb->in, in, 0);
17187 static void unin_triple(struct reg_block *rb, struct triple *unin)
17189 do_triple_unset(&rb->in, unin);
17192 static int out_triple(struct reg_block *rb, struct triple *out)
17194 return do_triple_set(&rb->out, out, 0);
17196 static void unout_triple(struct reg_block *rb, struct triple *unout)
17198 do_triple_unset(&rb->out, unout);
17201 static int initialize_regblock(struct reg_block *blocks,
17202 struct block *block, int vertex)
17204 struct block_set *user;
17205 if (!block || (blocks[block->vertex].block == block)) {
17209 /* Renumber the blocks in a convinient fashion */
17210 block->vertex = vertex;
17211 blocks[vertex].block = block;
17212 blocks[vertex].vertex = vertex;
17213 for(user = block->use; user; user = user->next) {
17214 vertex = initialize_regblock(blocks, user->member, vertex);
17219 static struct triple *part_to_piece(struct compile_state *state, struct triple *ins)
17221 /* Part to piece is a best attempt and it cannot be correct all by
17222 * itself. If various values are read as different sizes in different
17223 * parts of the code this function cannot work. Or rather it cannot
17224 * work in conjunction with compute_variable_liftimes. As the
17225 * analysis will get confused.
17227 struct triple *base;
17229 if (!is_lvalue(state, ins)) {
17234 while(ins && triple_is_part(state, ins) && (ins->op != OP_PIECE)) {
17235 base = MISC(ins, 0);
17238 reg += index_reg_offset(state, base->type, ins->u.cval)/REG_SIZEOF_REG;
17241 reg += field_reg_offset(state, base->type, ins->u.field)/REG_SIZEOF_REG;
17244 internal_error(state, ins, "unhandled part");
17250 if (reg > base->lhs) {
17251 internal_error(state, base, "part out of range?");
17253 ins = LHS(base, reg);
17258 static int this_def(struct compile_state *state,
17259 struct triple *ins, struct triple *other)
17261 if (ins == other) {
17264 if (ins->op == OP_WRITE) {
17265 ins = part_to_piece(state, MISC(ins, 0));
17267 return ins == other;
17270 static int phi_in(struct compile_state *state, struct reg_block *blocks,
17271 struct reg_block *rb, struct block *suc)
17273 /* Read the conditional input set of a successor block
17274 * (i.e. the input to the phi nodes) and place it in the
17275 * current blocks output set.
17277 struct block_set *set;
17278 struct triple *ptr;
17282 /* Find the edge I am coming in on */
17283 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
17284 if (set->member == rb->block) {
17289 internal_error(state, 0, "Not coming on a control edge?");
17291 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
17292 struct triple **slot, *expr, *ptr2;
17293 int out_change, done2;
17294 done = (ptr == suc->last);
17295 if (ptr->op != OP_PHI) {
17298 slot = &RHS(ptr, 0);
17300 out_change = out_triple(rb, expr);
17304 /* If we don't define the variable also plast it
17305 * in the current blocks input set.
17307 ptr2 = rb->block->first;
17308 for(done2 = 0; !done2; ptr2 = ptr2->next) {
17309 if (this_def(state, ptr2, expr)) {
17312 done2 = (ptr2 == rb->block->last);
17317 change |= in_triple(rb, expr);
17322 static int reg_in(struct compile_state *state, struct reg_block *blocks,
17323 struct reg_block *rb, struct block *suc)
17325 struct triple_reg_set *in_set;
17328 /* Read the input set of a successor block
17329 * and place it in the current blocks output set.
17331 in_set = blocks[suc->vertex].in;
17332 for(; in_set; in_set = in_set->next) {
17333 int out_change, done;
17334 struct triple *first, *last, *ptr;
17335 out_change = out_triple(rb, in_set->member);
17339 /* If we don't define the variable also place it
17340 * in the current blocks input set.
17342 first = rb->block->first;
17343 last = rb->block->last;
17345 for(ptr = first; !done; ptr = ptr->next) {
17346 if (this_def(state, ptr, in_set->member)) {
17349 done = (ptr == last);
17354 change |= in_triple(rb, in_set->member);
17356 change |= phi_in(state, blocks, rb, suc);
17360 static int use_in(struct compile_state *state, struct reg_block *rb)
17362 /* Find the variables we use but don't define and add
17363 * it to the current blocks input set.
17365 #warning "FIXME is this O(N^2) algorithm bad?"
17366 struct block *block;
17367 struct triple *ptr;
17372 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
17373 struct triple **expr;
17374 done = (ptr == block->first);
17375 /* The variable a phi function uses depends on the
17376 * control flow, and is handled in phi_in, not
17379 if (ptr->op == OP_PHI) {
17382 expr = triple_rhs(state, ptr, 0);
17383 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17384 struct triple *rhs, *test;
17386 rhs = part_to_piece(state, *expr);
17391 /* See if rhs is defined in this block.
17392 * A write counts as a definition.
17394 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
17395 tdone = (test == block->first);
17396 if (this_def(state, test, rhs)) {
17401 /* If I still have a valid rhs add it to in */
17402 change |= in_triple(rb, rhs);
17408 static struct reg_block *compute_variable_lifetimes(
17409 struct compile_state *state, struct basic_blocks *bb)
17411 struct reg_block *blocks;
17414 sizeof(*blocks)*(bb->last_vertex + 1), "reg_block");
17415 initialize_regblock(blocks, bb->last_block, 0);
17419 for(i = 1; i <= bb->last_vertex; i++) {
17420 struct block_set *edge;
17421 struct reg_block *rb;
17423 /* Add the all successor's input set to in */
17424 for(edge = rb->block->edges; edge; edge = edge->next) {
17425 change |= reg_in(state, blocks, rb, edge->member);
17427 /* Add use to in... */
17428 change |= use_in(state, rb);
17434 static void free_variable_lifetimes(struct compile_state *state,
17435 struct basic_blocks *bb, struct reg_block *blocks)
17438 /* free in_set && out_set on each block */
17439 for(i = 1; i <= bb->last_vertex; i++) {
17440 struct triple_reg_set *entry, *next;
17441 struct reg_block *rb;
17443 for(entry = rb->in; entry ; entry = next) {
17444 next = entry->next;
17445 do_triple_unset(&rb->in, entry->member);
17447 for(entry = rb->out; entry; entry = next) {
17448 next = entry->next;
17449 do_triple_unset(&rb->out, entry->member);
17456 typedef void (*wvl_cb_t)(
17457 struct compile_state *state,
17458 struct reg_block *blocks, struct triple_reg_set *live,
17459 struct reg_block *rb, struct triple *ins, void *arg);
17461 static void walk_variable_lifetimes(struct compile_state *state,
17462 struct basic_blocks *bb, struct reg_block *blocks,
17463 wvl_cb_t cb, void *arg)
17467 for(i = 1; i <= state->bb.last_vertex; i++) {
17468 struct triple_reg_set *live;
17469 struct triple_reg_set *entry, *next;
17470 struct triple *ptr, *prev;
17471 struct reg_block *rb;
17472 struct block *block;
17475 /* Get the blocks */
17479 /* Copy out into live */
17481 for(entry = rb->out; entry; entry = next) {
17482 next = entry->next;
17483 do_triple_set(&live, entry->member, entry->new);
17485 /* Walk through the basic block calculating live */
17486 for(done = 0, ptr = block->last; !done; ptr = prev) {
17487 struct triple **expr;
17490 done = (ptr == block->first);
17492 /* Ensure the current definition is in live */
17493 if (triple_is_def(state, ptr)) {
17494 do_triple_set(&live, ptr, 0);
17497 /* Inform the callback function of what is
17500 cb(state, blocks, live, rb, ptr, arg);
17502 /* Remove the current definition from live */
17503 do_triple_unset(&live, ptr);
17505 /* Add the current uses to live.
17507 * It is safe to skip phi functions because they do
17508 * not have any block local uses, and the block
17509 * output sets already properly account for what
17510 * control flow depedent uses phi functions do have.
17512 if (ptr->op == OP_PHI) {
17515 expr = triple_rhs(state, ptr, 0);
17516 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17517 /* If the triple is not a definition skip it. */
17518 if (!*expr || !triple_is_def(state, *expr)) {
17521 do_triple_set(&live, *expr, 0);
17525 for(entry = live; entry; entry = next) {
17526 next = entry->next;
17527 do_triple_unset(&live, entry->member);
17532 struct print_live_variable_info {
17533 struct reg_block *rb;
17536 static void print_live_variables_block(
17537 struct compile_state *state, struct block *block, void *arg)
17540 struct print_live_variable_info *info = arg;
17541 struct block_set *edge;
17542 FILE *fp = info->fp;
17543 struct reg_block *rb;
17544 struct triple *ptr;
17547 rb = &info->rb[block->vertex];
17549 fprintf(fp, "\nblock: %p (%d),",
17550 block, block->vertex);
17551 for(edge = block->edges; edge; edge = edge->next) {
17552 fprintf(fp, " %p<-%p",
17554 edge->member && edge->member->use?edge->member->use->member : 0);
17558 struct triple_reg_set *in_set;
17559 fprintf(fp, " in:");
17560 for(in_set = rb->in; in_set; in_set = in_set->next) {
17561 fprintf(fp, " %-10p", in_set->member);
17566 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17567 done = (ptr == block->last);
17568 if (ptr->op == OP_PHI) {
17575 for(edge = 0; edge < block->users; edge++) {
17576 fprintf(fp, " in(%d):", edge);
17577 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17578 struct triple **slot;
17579 done = (ptr == block->last);
17580 if (ptr->op != OP_PHI) {
17583 slot = &RHS(ptr, 0);
17584 fprintf(fp, " %-10p", slot[edge]);
17589 if (block->first->op == OP_LABEL) {
17590 fprintf(fp, "%p:\n", block->first);
17592 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17593 done = (ptr == block->last);
17594 display_triple(fp, ptr);
17597 struct triple_reg_set *out_set;
17598 fprintf(fp, " out:");
17599 for(out_set = rb->out; out_set; out_set = out_set->next) {
17600 fprintf(fp, " %-10p", out_set->member);
17607 static void print_live_variables(struct compile_state *state,
17608 struct basic_blocks *bb, struct reg_block *rb, FILE *fp)
17610 struct print_live_variable_info info;
17613 fprintf(fp, "\nlive variables by block\n");
17614 walk_blocks(state, bb, print_live_variables_block, &info);
17619 static int count_triples(struct compile_state *state)
17621 struct triple *first, *ins;
17623 first = state->first;
17628 } while (ins != first);
17633 struct dead_triple {
17634 struct triple *triple;
17635 struct dead_triple *work_next;
17636 struct block *block;
17639 #define TRIPLE_FLAG_ALIVE 1
17640 #define TRIPLE_FLAG_FREE 1
17643 static void print_dead_triples(struct compile_state *state,
17644 struct dead_triple *dtriple)
17646 struct triple *first, *ins;
17647 struct dead_triple *dt;
17649 if (!(state->compiler->debug & DEBUG_TRIPLES)) {
17652 fp = state->dbgout;
17653 fprintf(fp, "--------------- dtriples ---------------\n");
17654 first = state->first;
17657 dt = &dtriple[ins->id];
17658 if ((ins->op == OP_LABEL) && (ins->use)) {
17659 fprintf(fp, "\n%p:\n", ins);
17662 (dt->flags & TRIPLE_FLAG_ALIVE)?' ': '-');
17663 display_triple(fp, ins);
17664 if (triple_is_branch(state, ins)) {
17668 } while(ins != first);
17673 static void awaken(
17674 struct compile_state *state,
17675 struct dead_triple *dtriple, struct triple **expr,
17676 struct dead_triple ***work_list_tail)
17678 struct triple *triple;
17679 struct dead_triple *dt;
17687 if (triple->id <= 0) {
17688 internal_error(state, triple, "bad triple id: %d",
17691 if (triple->op == OP_NOOP) {
17692 internal_error(state, triple, "awakening noop?");
17695 dt = &dtriple[triple->id];
17696 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
17697 dt->flags |= TRIPLE_FLAG_ALIVE;
17698 if (!dt->work_next) {
17699 **work_list_tail = dt;
17700 *work_list_tail = &dt->work_next;
17705 static void eliminate_inefectual_code(struct compile_state *state)
17707 struct block *block;
17708 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
17710 struct triple *first, *final, *ins;
17712 if (!(state->compiler->flags & COMPILER_ELIMINATE_INEFECTUAL_CODE)) {
17716 /* Setup the work list */
17718 work_list_tail = &work_list;
17720 first = state->first;
17721 final = state->first->prev;
17723 /* Count how many triples I have */
17724 triples = count_triples(state);
17726 /* Now put then in an array and mark all of the triples dead */
17727 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
17733 dtriple[i].triple = ins;
17734 dtriple[i].block = block_of_triple(state, ins);
17735 dtriple[i].flags = 0;
17736 dtriple[i].old_id = ins->id;
17738 /* See if it is an operation we always keep */
17739 if (!triple_is_pure(state, ins, dtriple[i].old_id)) {
17740 awaken(state, dtriple, &ins, &work_list_tail);
17744 } while(ins != first);
17746 struct block *block;
17747 struct dead_triple *dt;
17748 struct block_set *user;
17749 struct triple **expr;
17751 work_list = dt->work_next;
17753 work_list_tail = &work_list;
17755 /* Make certain the block the current instruction is in lives */
17756 block = block_of_triple(state, dt->triple);
17757 awaken(state, dtriple, &block->first, &work_list_tail);
17758 if (triple_is_branch(state, block->last)) {
17759 awaken(state, dtriple, &block->last, &work_list_tail);
17761 awaken(state, dtriple, &block->last->next, &work_list_tail);
17764 /* Wake up the data depencencies of this triple */
17767 expr = triple_rhs(state, dt->triple, expr);
17768 awaken(state, dtriple, expr, &work_list_tail);
17771 expr = triple_lhs(state, dt->triple, expr);
17772 awaken(state, dtriple, expr, &work_list_tail);
17775 expr = triple_misc(state, dt->triple, expr);
17776 awaken(state, dtriple, expr, &work_list_tail);
17778 /* Wake up the forward control dependencies */
17780 expr = triple_targ(state, dt->triple, expr);
17781 awaken(state, dtriple, expr, &work_list_tail);
17783 /* Wake up the reverse control dependencies of this triple */
17784 for(user = dt->block->ipdomfrontier; user; user = user->next) {
17785 struct triple *last;
17786 last = user->member->last;
17787 while((last->op == OP_NOOP) && (last != user->member->first)) {
17788 internal_warning(state, last, "awakening noop?");
17791 awaken(state, dtriple, &last, &work_list_tail);
17794 print_dead_triples(state, dtriple);
17795 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
17796 if ((dt->triple->op == OP_NOOP) &&
17797 (dt->flags & TRIPLE_FLAG_ALIVE)) {
17798 internal_error(state, dt->triple, "noop effective?");
17800 dt->triple->id = dt->old_id; /* Restore the color */
17801 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
17802 release_triple(state, dt->triple);
17807 rebuild_ssa_form(state);
17809 print_blocks(state, __func__, state->dbgout);
17813 static void insert_mandatory_copies(struct compile_state *state)
17815 struct triple *ins, *first;
17817 /* The object is with a minimum of inserted copies,
17818 * to resolve in fundamental register conflicts between
17819 * register value producers and consumers.
17820 * Theoretically we may be greater than minimal when we
17821 * are inserting copies before instructions but that
17822 * case should be rare.
17824 first = state->first;
17827 struct triple_set *entry, *next;
17828 struct triple *tmp;
17829 struct reg_info info;
17830 unsigned reg, regcm;
17831 int do_post_copy, do_pre_copy;
17833 if (!triple_is_def(state, ins)) {
17836 /* Find the architecture specific color information */
17837 info = find_lhs_pre_color(state, ins, 0);
17838 if (info.reg >= MAX_REGISTERS) {
17839 info.reg = REG_UNSET;
17843 regcm = arch_type_to_regcm(state, ins->type);
17844 do_post_copy = do_pre_copy = 0;
17846 /* Walk through the uses of ins and check for conflicts */
17847 for(entry = ins->use; entry; entry = next) {
17848 struct reg_info rinfo;
17850 next = entry->next;
17851 i = find_rhs_use(state, entry->member, ins);
17856 /* Find the users color requirements */
17857 rinfo = arch_reg_rhs(state, entry->member, i);
17858 if (rinfo.reg >= MAX_REGISTERS) {
17859 rinfo.reg = REG_UNSET;
17862 /* See if I need a pre_copy */
17863 if (rinfo.reg != REG_UNSET) {
17864 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
17869 regcm &= rinfo.regcm;
17870 regcm = arch_regcm_normalize(state, regcm);
17874 /* Always use pre_copies for constants.
17875 * They do not take up any registers until a
17876 * copy places them in one.
17878 if ((info.reg == REG_UNNEEDED) &&
17879 (rinfo.reg != REG_UNNEEDED)) {
17885 (((info.reg != REG_UNSET) &&
17886 (reg != REG_UNSET) &&
17887 (info.reg != reg)) ||
17888 ((info.regcm & regcm) == 0));
17891 regcm = info.regcm;
17892 /* Walk through the uses of ins and do a pre_copy or see if a post_copy is warranted */
17893 for(entry = ins->use; entry; entry = next) {
17894 struct reg_info rinfo;
17896 next = entry->next;
17897 i = find_rhs_use(state, entry->member, ins);
17902 /* Find the users color requirements */
17903 rinfo = arch_reg_rhs(state, entry->member, i);
17904 if (rinfo.reg >= MAX_REGISTERS) {
17905 rinfo.reg = REG_UNSET;
17908 /* Now see if it is time to do the pre_copy */
17909 if (rinfo.reg != REG_UNSET) {
17910 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
17911 ((regcm & rinfo.regcm) == 0) ||
17912 /* Don't let a mandatory coalesce sneak
17913 * into a operation that is marked to prevent
17916 ((reg != REG_UNNEEDED) &&
17917 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
17918 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
17921 struct triple *user;
17922 user = entry->member;
17923 if (RHS(user, i) != ins) {
17924 internal_error(state, user, "bad rhs");
17926 tmp = pre_copy(state, user, i);
17927 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
17935 if ((regcm & rinfo.regcm) == 0) {
17937 struct triple *user;
17938 user = entry->member;
17939 if (RHS(user, i) != ins) {
17940 internal_error(state, user, "bad rhs");
17942 tmp = pre_copy(state, user, i);
17943 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
17949 regcm &= rinfo.regcm;
17952 if (do_post_copy) {
17953 struct reg_info pre, post;
17954 tmp = post_copy(state, ins);
17955 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
17956 pre = arch_reg_lhs(state, ins, 0);
17957 post = arch_reg_lhs(state, tmp, 0);
17958 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
17959 internal_error(state, tmp, "useless copy");
17964 } while(ins != first);
17966 print_blocks(state, __func__, state->dbgout);
17970 struct live_range_edge;
17971 struct live_range_def;
17972 struct live_range {
17973 struct live_range_edge *edges;
17974 struct live_range_def *defs;
17975 /* Note. The list pointed to by defs is kept in order.
17976 * That is baring splits in the flow control
17977 * defs dominates defs->next wich dominates defs->next->next
17984 struct live_range *group_next, **group_prev;
17987 struct live_range_edge {
17988 struct live_range_edge *next;
17989 struct live_range *node;
17992 struct live_range_def {
17993 struct live_range_def *next;
17994 struct live_range_def *prev;
17995 struct live_range *lr;
17996 struct triple *def;
18000 #define LRE_HASH_SIZE 2048
18002 struct lre_hash *next;
18003 struct live_range *left;
18004 struct live_range *right;
18009 struct lre_hash *hash[LRE_HASH_SIZE];
18010 struct reg_block *blocks;
18011 struct live_range_def *lrd;
18012 struct live_range *lr;
18013 struct live_range *low, **low_tail;
18014 struct live_range *high, **high_tail;
18017 int passes, max_passes;
18021 struct print_interference_block_info {
18022 struct reg_state *rstate;
18026 static void print_interference_block(
18027 struct compile_state *state, struct block *block, void *arg)
18030 struct print_interference_block_info *info = arg;
18031 struct reg_state *rstate = info->rstate;
18032 struct block_set *edge;
18033 FILE *fp = info->fp;
18034 struct reg_block *rb;
18035 struct triple *ptr;
18038 rb = &rstate->blocks[block->vertex];
18040 fprintf(fp, "\nblock: %p (%d),",
18041 block, block->vertex);
18042 for(edge = block->edges; edge; edge = edge->next) {
18043 fprintf(fp, " %p<-%p",
18045 edge->member && edge->member->use?edge->member->use->member : 0);
18049 struct triple_reg_set *in_set;
18050 fprintf(fp, " in:");
18051 for(in_set = rb->in; in_set; in_set = in_set->next) {
18052 fprintf(fp, " %-10p", in_set->member);
18057 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18058 done = (ptr == block->last);
18059 if (ptr->op == OP_PHI) {
18066 for(edge = 0; edge < block->users; edge++) {
18067 fprintf(fp, " in(%d):", edge);
18068 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18069 struct triple **slot;
18070 done = (ptr == block->last);
18071 if (ptr->op != OP_PHI) {
18074 slot = &RHS(ptr, 0);
18075 fprintf(fp, " %-10p", slot[edge]);
18080 if (block->first->op == OP_LABEL) {
18081 fprintf(fp, "%p:\n", block->first);
18083 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18084 struct live_range *lr;
18088 done = (ptr == block->last);
18089 lr = rstate->lrd[ptr->id].lr;
18092 ptr->id = rstate->lrd[id].orig_id;
18093 SET_REG(ptr->id, lr->color);
18094 display_triple(fp, ptr);
18097 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
18098 internal_error(state, ptr, "lr has no defs!");
18100 if (info->need_edges) {
18102 struct live_range_def *lrd;
18103 fprintf(fp, " range:");
18106 fprintf(fp, " %-10p", lrd->def);
18108 } while(lrd != lr->defs);
18111 if (lr->edges > 0) {
18112 struct live_range_edge *edge;
18113 fprintf(fp, " edges:");
18114 for(edge = lr->edges; edge; edge = edge->next) {
18115 struct live_range_def *lrd;
18116 lrd = edge->node->defs;
18118 fprintf(fp, " %-10p", lrd->def);
18120 } while(lrd != edge->node->defs);
18126 /* Do a bunch of sanity checks */
18127 valid_ins(state, ptr);
18128 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
18129 internal_error(state, ptr, "Invalid triple id: %d",
18134 struct triple_reg_set *out_set;
18135 fprintf(fp, " out:");
18136 for(out_set = rb->out; out_set; out_set = out_set->next) {
18137 fprintf(fp, " %-10p", out_set->member);
18144 static void print_interference_blocks(
18145 struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
18147 struct print_interference_block_info info;
18148 info.rstate = rstate;
18150 info.need_edges = need_edges;
18151 fprintf(fp, "\nlive variables by block\n");
18152 walk_blocks(state, &state->bb, print_interference_block, &info);
18156 static unsigned regc_max_size(struct compile_state *state, int classes)
18161 for(i = 0; i < MAX_REGC; i++) {
18162 if (classes & (1 << i)) {
18164 size = arch_regc_size(state, i);
18165 if (size > max_size) {
18173 static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
18175 unsigned equivs[MAX_REG_EQUIVS];
18177 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
18178 internal_error(state, 0, "invalid register");
18180 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
18181 internal_error(state, 0, "invalid register");
18183 arch_reg_equivs(state, equivs, reg1);
18184 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18185 if (equivs[i] == reg2) {
18192 static void reg_fill_used(struct compile_state *state, char *used, int reg)
18194 unsigned equivs[MAX_REG_EQUIVS];
18196 if (reg == REG_UNNEEDED) {
18199 arch_reg_equivs(state, equivs, reg);
18200 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18201 used[equivs[i]] = 1;
18206 static void reg_inc_used(struct compile_state *state, char *used, int reg)
18208 unsigned equivs[MAX_REG_EQUIVS];
18210 if (reg == REG_UNNEEDED) {
18213 arch_reg_equivs(state, equivs, reg);
18214 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18215 used[equivs[i]] += 1;
18220 static unsigned int hash_live_edge(
18221 struct live_range *left, struct live_range *right)
18223 unsigned int hash, val;
18224 unsigned long lval, rval;
18225 lval = ((unsigned long)left)/sizeof(struct live_range);
18226 rval = ((unsigned long)right)/sizeof(struct live_range);
18231 hash = (hash *263) + val;
18236 hash = (hash *263) + val;
18238 hash = hash & (LRE_HASH_SIZE - 1);
18242 static struct lre_hash **lre_probe(struct reg_state *rstate,
18243 struct live_range *left, struct live_range *right)
18245 struct lre_hash **ptr;
18246 unsigned int index;
18247 /* Ensure left <= right */
18248 if (left > right) {
18249 struct live_range *tmp;
18254 index = hash_live_edge(left, right);
18256 ptr = &rstate->hash[index];
18258 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
18261 ptr = &(*ptr)->next;
18266 static int interfere(struct reg_state *rstate,
18267 struct live_range *left, struct live_range *right)
18269 struct lre_hash **ptr;
18270 ptr = lre_probe(rstate, left, right);
18271 return ptr && *ptr;
18274 static void add_live_edge(struct reg_state *rstate,
18275 struct live_range *left, struct live_range *right)
18277 /* FIXME the memory allocation overhead is noticeable here... */
18278 struct lre_hash **ptr, *new_hash;
18279 struct live_range_edge *edge;
18281 if (left == right) {
18284 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
18287 /* Ensure left <= right */
18288 if (left > right) {
18289 struct live_range *tmp;
18294 ptr = lre_probe(rstate, left, right);
18299 fprintf(state->errout, "new_live_edge(%p, %p)\n",
18302 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
18303 new_hash->next = *ptr;
18304 new_hash->left = left;
18305 new_hash->right = right;
18308 edge = xmalloc(sizeof(*edge), "live_range_edge");
18309 edge->next = left->edges;
18310 edge->node = right;
18311 left->edges = edge;
18314 edge = xmalloc(sizeof(*edge), "live_range_edge");
18315 edge->next = right->edges;
18317 right->edges = edge;
18318 right->degree += 1;
18321 static void remove_live_edge(struct reg_state *rstate,
18322 struct live_range *left, struct live_range *right)
18324 struct live_range_edge *edge, **ptr;
18325 struct lre_hash **hptr, *entry;
18326 hptr = lre_probe(rstate, left, right);
18327 if (!hptr || !*hptr) {
18331 *hptr = entry->next;
18334 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
18336 if (edge->node == right) {
18338 memset(edge, 0, sizeof(*edge));
18344 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
18346 if (edge->node == left) {
18348 memset(edge, 0, sizeof(*edge));
18356 static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
18358 struct live_range_edge *edge, *next;
18359 for(edge = range->edges; edge; edge = next) {
18361 remove_live_edge(rstate, range, edge->node);
18365 static void transfer_live_edges(struct reg_state *rstate,
18366 struct live_range *dest, struct live_range *src)
18368 struct live_range_edge *edge, *next;
18369 for(edge = src->edges; edge; edge = next) {
18370 struct live_range *other;
18372 other = edge->node;
18373 remove_live_edge(rstate, src, other);
18374 add_live_edge(rstate, dest, other);
18379 /* Interference graph...
18381 * new(n) --- Return a graph with n nodes but no edges.
18382 * add(g,x,y) --- Return a graph including g with an between x and y
18383 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
18384 * x and y in the graph g
18385 * degree(g, x) --- Return the degree of the node x in the graph g
18386 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
18388 * Implement with a hash table && a set of adjcency vectors.
18389 * The hash table supports constant time implementations of add and interfere.
18390 * The adjacency vectors support an efficient implementation of neighbors.
18394 * +---------------------------------------------------+
18395 * | +--------------+ |
18397 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
18399 * -- In simplify implment optimistic coloring... (No backtracking)
18400 * -- Implement Rematerialization it is the only form of spilling we can perform
18401 * Essentially this means dropping a constant from a register because
18402 * we can regenerate it later.
18404 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
18405 * coalesce at phi points...
18406 * --- Bias coloring if at all possible do the coalesing a compile time.
18411 static void different_colored(
18412 struct compile_state *state, struct reg_state *rstate,
18413 struct triple *parent, struct triple *ins)
18415 struct live_range *lr;
18416 struct triple **expr;
18417 lr = rstate->lrd[ins->id].lr;
18418 expr = triple_rhs(state, ins, 0);
18419 for(;expr; expr = triple_rhs(state, ins, expr)) {
18420 struct live_range *lr2;
18421 if (!*expr || (*expr == parent) || (*expr == ins)) {
18424 lr2 = rstate->lrd[(*expr)->id].lr;
18425 if (lr->color == lr2->color) {
18426 internal_error(state, ins, "live range too big");
18432 static struct live_range *coalesce_ranges(
18433 struct compile_state *state, struct reg_state *rstate,
18434 struct live_range *lr1, struct live_range *lr2)
18436 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
18442 if (!lr1->defs || !lr2->defs) {
18443 internal_error(state, 0,
18444 "cannot coalese dead live ranges");
18446 if ((lr1->color == REG_UNNEEDED) ||
18447 (lr2->color == REG_UNNEEDED)) {
18448 internal_error(state, 0,
18449 "cannot coalesce live ranges without a possible color");
18451 if ((lr1->color != lr2->color) &&
18452 (lr1->color != REG_UNSET) &&
18453 (lr2->color != REG_UNSET)) {
18454 internal_error(state, lr1->defs->def,
18455 "cannot coalesce live ranges of different colors");
18457 color = lr1->color;
18458 if (color == REG_UNSET) {
18459 color = lr2->color;
18461 classes = lr1->classes & lr2->classes;
18463 internal_error(state, lr1->defs->def,
18464 "cannot coalesce live ranges with dissimilar register classes");
18466 if (state->compiler->debug & DEBUG_COALESCING) {
18467 FILE *fp = state->errout;
18468 fprintf(fp, "coalescing:");
18471 fprintf(fp, " %p", lrd->def);
18473 } while(lrd != lr1->defs);
18477 fprintf(fp, " %p", lrd->def);
18479 } while(lrd != lr2->defs);
18482 /* If there is a clear dominate live range put it in lr1,
18483 * For purposes of this test phi functions are
18484 * considered dominated by the definitions that feed into
18487 if ((lr1->defs->prev->def->op == OP_PHI) ||
18488 ((lr2->defs->prev->def->op != OP_PHI) &&
18489 tdominates(state, lr2->defs->def, lr1->defs->def))) {
18490 struct live_range *tmp;
18496 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
18497 fprintf(state->errout, "lr1 post\n");
18499 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
18500 fprintf(state->errout, "lr1 pre\n");
18502 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
18503 fprintf(state->errout, "lr2 post\n");
18505 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
18506 fprintf(state->errout, "lr2 pre\n");
18510 fprintf(state->errout, "coalesce color1(%p): %3d color2(%p) %3d\n",
18517 /* Append lr2 onto lr1 */
18518 #warning "FIXME should this be a merge instead of a splice?"
18519 /* This FIXME item applies to the correctness of live_range_end
18520 * and to the necessity of making multiple passes of coalesce_live_ranges.
18521 * A failure to find some coalesce opportunities in coaleace_live_ranges
18522 * does not impact the correct of the compiler just the efficiency with
18523 * which registers are allocated.
18526 mid1 = lr1->defs->prev;
18528 end = lr2->defs->prev;
18536 /* Fixup the live range in the added live range defs */
18541 } while(lrd != head);
18543 /* Mark lr2 as free. */
18545 lr2->color = REG_UNNEEDED;
18549 internal_error(state, 0, "lr1->defs == 0 ?");
18552 lr1->color = color;
18553 lr1->classes = classes;
18555 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
18556 transfer_live_edges(rstate, lr1, lr2);
18561 static struct live_range_def *live_range_head(
18562 struct compile_state *state, struct live_range *lr,
18563 struct live_range_def *last)
18565 struct live_range_def *result;
18570 else if (!tdominates(state, lr->defs->def, last->next->def)) {
18571 result = last->next;
18576 static struct live_range_def *live_range_end(
18577 struct compile_state *state, struct live_range *lr,
18578 struct live_range_def *last)
18580 struct live_range_def *result;
18583 result = lr->defs->prev;
18585 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
18586 result = last->prev;
18592 static void initialize_live_ranges(
18593 struct compile_state *state, struct reg_state *rstate)
18595 struct triple *ins, *first;
18596 size_t count, size;
18599 first = state->first;
18600 /* First count how many instructions I have.
18602 count = count_triples(state);
18603 /* Potentially I need one live range definitions for each
18606 rstate->defs = count;
18607 /* Potentially I need one live range for each instruction
18608 * plus an extra for the dummy live range.
18610 rstate->ranges = count + 1;
18611 size = sizeof(rstate->lrd[0]) * rstate->defs;
18612 rstate->lrd = xcmalloc(size, "live_range_def");
18613 size = sizeof(rstate->lr[0]) * rstate->ranges;
18614 rstate->lr = xcmalloc(size, "live_range");
18616 /* Setup the dummy live range */
18617 rstate->lr[0].classes = 0;
18618 rstate->lr[0].color = REG_UNSET;
18619 rstate->lr[0].defs = 0;
18623 /* If the triple is a variable give it a live range */
18624 if (triple_is_def(state, ins)) {
18625 struct reg_info info;
18626 /* Find the architecture specific color information */
18627 info = find_def_color(state, ins);
18629 rstate->lr[i].defs = &rstate->lrd[j];
18630 rstate->lr[i].color = info.reg;
18631 rstate->lr[i].classes = info.regcm;
18632 rstate->lr[i].degree = 0;
18633 rstate->lrd[j].lr = &rstate->lr[i];
18635 /* Otherwise give the triple the dummy live range. */
18637 rstate->lrd[j].lr = &rstate->lr[0];
18640 /* Initalize the live_range_def */
18641 rstate->lrd[j].next = &rstate->lrd[j];
18642 rstate->lrd[j].prev = &rstate->lrd[j];
18643 rstate->lrd[j].def = ins;
18644 rstate->lrd[j].orig_id = ins->id;
18649 } while(ins != first);
18650 rstate->ranges = i;
18652 /* Make a second pass to handle achitecture specific register
18657 int zlhs, zrhs, i, j;
18658 if (ins->id > rstate->defs) {
18659 internal_error(state, ins, "bad id");
18662 /* Walk through the template of ins and coalesce live ranges */
18664 if ((zlhs == 0) && triple_is_def(state, ins)) {
18669 if (state->compiler->debug & DEBUG_COALESCING2) {
18670 fprintf(state->errout, "mandatory coalesce: %p %d %d\n",
18674 for(i = 0; i < zlhs; i++) {
18675 struct reg_info linfo;
18676 struct live_range_def *lhs;
18677 linfo = arch_reg_lhs(state, ins, i);
18678 if (linfo.reg < MAX_REGISTERS) {
18681 if (triple_is_def(state, ins)) {
18682 lhs = &rstate->lrd[ins->id];
18684 lhs = &rstate->lrd[LHS(ins, i)->id];
18687 if (state->compiler->debug & DEBUG_COALESCING2) {
18688 fprintf(state->errout, "coalesce lhs(%d): %p %d\n",
18689 i, lhs, linfo.reg);
18692 for(j = 0; j < zrhs; j++) {
18693 struct reg_info rinfo;
18694 struct live_range_def *rhs;
18695 rinfo = arch_reg_rhs(state, ins, j);
18696 if (rinfo.reg < MAX_REGISTERS) {
18699 rhs = &rstate->lrd[RHS(ins, j)->id];
18701 if (state->compiler->debug & DEBUG_COALESCING2) {
18702 fprintf(state->errout, "coalesce rhs(%d): %p %d\n",
18703 j, rhs, rinfo.reg);
18706 if (rinfo.reg == linfo.reg) {
18707 coalesce_ranges(state, rstate,
18713 } while(ins != first);
18716 static void graph_ins(
18717 struct compile_state *state,
18718 struct reg_block *blocks, struct triple_reg_set *live,
18719 struct reg_block *rb, struct triple *ins, void *arg)
18721 struct reg_state *rstate = arg;
18722 struct live_range *def;
18723 struct triple_reg_set *entry;
18725 /* If the triple is not a definition
18726 * we do not have a definition to add to
18727 * the interference graph.
18729 if (!triple_is_def(state, ins)) {
18732 def = rstate->lrd[ins->id].lr;
18734 /* Create an edge between ins and everything that is
18735 * alive, unless the live_range cannot share
18736 * a physical register with ins.
18738 for(entry = live; entry; entry = entry->next) {
18739 struct live_range *lr;
18740 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
18741 internal_error(state, 0, "bad entry?");
18743 lr = rstate->lrd[entry->member->id].lr;
18747 if (!arch_regcm_intersect(def->classes, lr->classes)) {
18750 add_live_edge(rstate, def, lr);
18755 static struct live_range *get_verify_live_range(
18756 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
18758 struct live_range *lr;
18759 struct live_range_def *lrd;
18761 if ((ins->id < 0) || (ins->id > rstate->defs)) {
18762 internal_error(state, ins, "bad ins?");
18764 lr = rstate->lrd[ins->id].lr;
18768 if (lrd->def == ins) {
18772 } while(lrd != lr->defs);
18774 internal_error(state, ins, "ins not in live range");
18779 static void verify_graph_ins(
18780 struct compile_state *state,
18781 struct reg_block *blocks, struct triple_reg_set *live,
18782 struct reg_block *rb, struct triple *ins, void *arg)
18784 struct reg_state *rstate = arg;
18785 struct triple_reg_set *entry1, *entry2;
18788 /* Compare live against edges and make certain the code is working */
18789 for(entry1 = live; entry1; entry1 = entry1->next) {
18790 struct live_range *lr1;
18791 lr1 = get_verify_live_range(state, rstate, entry1->member);
18792 for(entry2 = live; entry2; entry2 = entry2->next) {
18793 struct live_range *lr2;
18794 struct live_range_edge *edge2;
18797 if (entry2 == entry1) {
18800 lr2 = get_verify_live_range(state, rstate, entry2->member);
18802 internal_error(state, entry2->member,
18803 "live range with 2 values simultaneously alive");
18805 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
18808 if (!interfere(rstate, lr1, lr2)) {
18809 internal_error(state, entry2->member,
18810 "edges don't interfere?");
18815 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
18817 if (edge2->node == lr1) {
18821 if (lr2_degree != lr2->degree) {
18822 internal_error(state, entry2->member,
18823 "computed degree: %d does not match reported degree: %d\n",
18824 lr2_degree, lr2->degree);
18827 internal_error(state, entry2->member, "missing edge");
18835 static void print_interference_ins(
18836 struct compile_state *state,
18837 struct reg_block *blocks, struct triple_reg_set *live,
18838 struct reg_block *rb, struct triple *ins, void *arg)
18840 struct reg_state *rstate = arg;
18841 struct live_range *lr;
18843 FILE *fp = state->dbgout;
18845 lr = rstate->lrd[ins->id].lr;
18847 ins->id = rstate->lrd[id].orig_id;
18848 SET_REG(ins->id, lr->color);
18849 display_triple(state->dbgout, ins);
18853 struct live_range_def *lrd;
18854 fprintf(fp, " range:");
18857 fprintf(fp, " %-10p", lrd->def);
18859 } while(lrd != lr->defs);
18863 struct triple_reg_set *entry;
18864 fprintf(fp, " live:");
18865 for(entry = live; entry; entry = entry->next) {
18866 fprintf(fp, " %-10p", entry->member);
18871 struct live_range_edge *entry;
18872 fprintf(fp, " edges:");
18873 for(entry = lr->edges; entry; entry = entry->next) {
18874 struct live_range_def *lrd;
18875 lrd = entry->node->defs;
18877 fprintf(fp, " %-10p", lrd->def);
18879 } while(lrd != entry->node->defs);
18884 if (triple_is_branch(state, ins)) {
18890 static int coalesce_live_ranges(
18891 struct compile_state *state, struct reg_state *rstate)
18893 /* At the point where a value is moved from one
18894 * register to another that value requires two
18895 * registers, thus increasing register pressure.
18896 * Live range coaleescing reduces the register
18897 * pressure by keeping a value in one register
18900 * In the case of a phi function all paths leading
18901 * into it must be allocated to the same register
18902 * otherwise the phi function may not be removed.
18904 * Forcing a value to stay in a single register
18905 * for an extended period of time does have
18906 * limitations when applied to non homogenous
18909 * The two cases I have identified are:
18910 * 1) Two forced register assignments may
18912 * 2) Registers may go unused because they
18913 * are only good for storing the value
18914 * and not manipulating it.
18916 * Because of this I need to split live ranges,
18917 * even outside of the context of coalesced live
18918 * ranges. The need to split live ranges does
18919 * impose some constraints on live range coalescing.
18921 * - Live ranges may not be coalesced across phi
18922 * functions. This creates a 2 headed live
18923 * range that cannot be sanely split.
18925 * - phi functions (coalesced in initialize_live_ranges)
18926 * are handled as pre split live ranges so we will
18927 * never attempt to split them.
18933 for(i = 0; i <= rstate->ranges; i++) {
18934 struct live_range *lr1;
18935 struct live_range_def *lrd1;
18936 lr1 = &rstate->lr[i];
18940 lrd1 = live_range_end(state, lr1, 0);
18941 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
18942 struct triple_set *set;
18943 if (lrd1->def->op != OP_COPY) {
18946 /* Skip copies that are the result of a live range split. */
18947 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
18950 for(set = lrd1->def->use; set; set = set->next) {
18951 struct live_range_def *lrd2;
18952 struct live_range *lr2, *res;
18954 lrd2 = &rstate->lrd[set->member->id];
18956 /* Don't coalesce with instructions
18957 * that are the result of a live range
18960 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
18963 lr2 = rstate->lrd[set->member->id].lr;
18967 if ((lr1->color != lr2->color) &&
18968 (lr1->color != REG_UNSET) &&
18969 (lr2->color != REG_UNSET)) {
18972 if ((lr1->classes & lr2->classes) == 0) {
18976 if (interfere(rstate, lr1, lr2)) {
18980 res = coalesce_ranges(state, rstate, lr1, lr2);
18994 static void fix_coalesce_conflicts(struct compile_state *state,
18995 struct reg_block *blocks, struct triple_reg_set *live,
18996 struct reg_block *rb, struct triple *ins, void *arg)
18998 int *conflicts = arg;
18999 int zlhs, zrhs, i, j;
19001 /* See if we have a mandatory coalesce operation between
19002 * a lhs and a rhs value. If so and the rhs value is also
19003 * alive then this triple needs to be pre copied. Otherwise
19004 * we would have two definitions in the same live range simultaneously
19008 if ((zlhs == 0) && triple_is_def(state, ins)) {
19012 for(i = 0; i < zlhs; i++) {
19013 struct reg_info linfo;
19014 linfo = arch_reg_lhs(state, ins, i);
19015 if (linfo.reg < MAX_REGISTERS) {
19018 for(j = 0; j < zrhs; j++) {
19019 struct reg_info rinfo;
19020 struct triple *rhs;
19021 struct triple_reg_set *set;
19024 rinfo = arch_reg_rhs(state, ins, j);
19025 if (rinfo.reg != linfo.reg) {
19029 for(set = live; set && !found; set = set->next) {
19030 if (set->member == rhs) {
19035 struct triple *copy;
19036 copy = pre_copy(state, ins, j);
19037 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
19045 static int correct_coalesce_conflicts(
19046 struct compile_state *state, struct reg_block *blocks)
19050 walk_variable_lifetimes(state, &state->bb, blocks,
19051 fix_coalesce_conflicts, &conflicts);
19055 static void replace_set_use(struct compile_state *state,
19056 struct triple_reg_set *head, struct triple *orig, struct triple *new)
19058 struct triple_reg_set *set;
19059 for(set = head; set; set = set->next) {
19060 if (set->member == orig) {
19066 static void replace_block_use(struct compile_state *state,
19067 struct reg_block *blocks, struct triple *orig, struct triple *new)
19070 #warning "WISHLIST visit just those blocks that need it *"
19071 for(i = 1; i <= state->bb.last_vertex; i++) {
19072 struct reg_block *rb;
19074 replace_set_use(state, rb->in, orig, new);
19075 replace_set_use(state, rb->out, orig, new);
19079 static void color_instructions(struct compile_state *state)
19081 struct triple *ins, *first;
19082 first = state->first;
19085 if (triple_is_def(state, ins)) {
19086 struct reg_info info;
19087 info = find_lhs_color(state, ins, 0);
19088 if (info.reg >= MAX_REGISTERS) {
19089 info.reg = REG_UNSET;
19091 SET_INFO(ins->id, info);
19094 } while(ins != first);
19097 static struct reg_info read_lhs_color(
19098 struct compile_state *state, struct triple *ins, int index)
19100 struct reg_info info;
19101 if ((index == 0) && triple_is_def(state, ins)) {
19102 info.reg = ID_REG(ins->id);
19103 info.regcm = ID_REGCM(ins->id);
19105 else if (index < ins->lhs) {
19106 info = read_lhs_color(state, LHS(ins, index), 0);
19109 internal_error(state, ins, "Bad lhs %d", index);
19110 info.reg = REG_UNSET;
19116 static struct triple *resolve_tangle(
19117 struct compile_state *state, struct triple *tangle)
19119 struct reg_info info, uinfo;
19120 struct triple_set *set, *next;
19121 struct triple *copy;
19123 #warning "WISHLIST recalculate all affected instructions colors"
19124 info = find_lhs_color(state, tangle, 0);
19125 for(set = tangle->use; set; set = next) {
19126 struct triple *user;
19129 user = set->member;
19131 for(i = 0; i < zrhs; i++) {
19132 if (RHS(user, i) != tangle) {
19135 uinfo = find_rhs_post_color(state, user, i);
19136 if (uinfo.reg == info.reg) {
19137 copy = pre_copy(state, user, i);
19138 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
19139 SET_INFO(copy->id, uinfo);
19144 uinfo = find_lhs_pre_color(state, tangle, 0);
19145 if (uinfo.reg == info.reg) {
19146 struct reg_info linfo;
19147 copy = post_copy(state, tangle);
19148 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
19149 linfo = find_lhs_color(state, copy, 0);
19150 SET_INFO(copy->id, linfo);
19152 info = find_lhs_color(state, tangle, 0);
19153 SET_INFO(tangle->id, info);
19159 static void fix_tangles(struct compile_state *state,
19160 struct reg_block *blocks, struct triple_reg_set *live,
19161 struct reg_block *rb, struct triple *ins, void *arg)
19163 int *tangles = arg;
19164 struct triple *tangle;
19166 char used[MAX_REGISTERS];
19167 struct triple_reg_set *set;
19170 /* Find out which registers have multiple uses at this point */
19171 memset(used, 0, sizeof(used));
19172 for(set = live; set; set = set->next) {
19173 struct reg_info info;
19174 info = read_lhs_color(state, set->member, 0);
19175 if (info.reg == REG_UNSET) {
19178 reg_inc_used(state, used, info.reg);
19181 /* Now find the least dominated definition of a register in
19182 * conflict I have seen so far.
19184 for(set = live; set; set = set->next) {
19185 struct reg_info info;
19186 info = read_lhs_color(state, set->member, 0);
19187 if (used[info.reg] < 2) {
19190 /* Changing copies that feed into phi functions
19193 if (set->member->use &&
19194 (set->member->use->member->op == OP_PHI)) {
19197 if (!tangle || tdominates(state, set->member, tangle)) {
19198 tangle = set->member;
19201 /* If I have found a tangle resolve it */
19203 struct triple *post_copy;
19205 post_copy = resolve_tangle(state, tangle);
19207 replace_block_use(state, blocks, tangle, post_copy);
19209 if (post_copy && (tangle != ins)) {
19210 replace_set_use(state, live, tangle, post_copy);
19217 static int correct_tangles(
19218 struct compile_state *state, struct reg_block *blocks)
19222 color_instructions(state);
19223 walk_variable_lifetimes(state, &state->bb, blocks,
19224 fix_tangles, &tangles);
19229 static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
19230 static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
19232 struct triple *find_constrained_def(
19233 struct compile_state *state, struct live_range *range, struct triple *constrained)
19235 struct live_range_def *lrd, *lrd_next;
19236 lrd_next = range->defs;
19238 struct reg_info info;
19242 lrd_next = lrd->next;
19244 regcm = arch_type_to_regcm(state, lrd->def->type);
19245 info = find_lhs_color(state, lrd->def, 0);
19246 regcm = arch_regcm_reg_normalize(state, regcm);
19247 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
19248 /* If the 2 register class masks are equal then
19249 * the current register class is not constrained.
19251 if (regcm == info.regcm) {
19255 /* If there is just one use.
19256 * That use cannot accept a larger register class.
19257 * There are no intervening definitions except
19258 * definitions that feed into that use.
19259 * Then a triple is not constrained.
19260 * FIXME handle this case!
19262 #warning "FIXME ignore cases that cannot be fixed (a definition followed by a use)"
19265 /* Of the constrained live ranges deal with the
19266 * least dominated one first.
19268 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
19269 fprintf(state->errout, "canidate: %p %-8s regcm: %x %x\n",
19270 lrd->def, tops(lrd->def->op), regcm, info.regcm);
19272 if (!constrained ||
19273 tdominates(state, lrd->def, constrained))
19275 constrained = lrd->def;
19277 } while(lrd_next != range->defs);
19278 return constrained;
19281 static int split_constrained_ranges(
19282 struct compile_state *state, struct reg_state *rstate,
19283 struct live_range *range)
19285 /* Walk through the edges in conflict and our current live
19286 * range, and find definitions that are more severly constrained
19287 * than they type of data they contain require.
19289 * Then pick one of those ranges and relax the constraints.
19291 struct live_range_edge *edge;
19292 struct triple *constrained;
19295 for(edge = range->edges; edge; edge = edge->next) {
19296 constrained = find_constrained_def(state, edge->node, constrained);
19298 #warning "FIXME should I call find_constrained_def here only if no previous constrained def was found?"
19299 if (!constrained) {
19300 constrained = find_constrained_def(state, range, constrained);
19303 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
19304 fprintf(state->errout, "constrained: ");
19305 display_triple(state->errout, constrained);
19308 ids_from_rstate(state, rstate);
19309 cleanup_rstate(state, rstate);
19310 resolve_tangle(state, constrained);
19312 return !!constrained;
19315 static int split_ranges(
19316 struct compile_state *state, struct reg_state *rstate,
19317 char *used, struct live_range *range)
19320 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
19321 fprintf(state->errout, "split_ranges %d %s %p\n",
19322 rstate->passes, tops(range->defs->def->op), range->defs->def);
19324 if ((range->color == REG_UNNEEDED) ||
19325 (rstate->passes >= rstate->max_passes)) {
19328 split = split_constrained_ranges(state, rstate, range);
19330 /* Ideally I would split the live range that will not be used
19331 * for the longest period of time in hopes that this will
19332 * (a) allow me to spill a register or
19333 * (b) allow me to place a value in another register.
19335 * So far I don't have a test case for this, the resolving
19336 * of mandatory constraints has solved all of my
19337 * know issues. So I have choosen not to write any
19338 * code until I cat get a better feel for cases where
19339 * it would be useful to have.
19342 #warning "WISHLIST implement live range splitting..."
19344 if (!split && (state->compiler->debug & DEBUG_RANGE_CONFLICTS2)) {
19345 FILE *fp = state->errout;
19346 print_interference_blocks(state, rstate, fp, 0);
19347 print_dominators(state, fp, &state->bb);
19352 static FILE *cgdebug_fp(struct compile_state *state)
19356 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH2)) {
19357 fp = state->errout;
19359 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH)) {
19360 fp = state->dbgout;
19365 static void cgdebug_printf(struct compile_state *state, const char *fmt, ...)
19368 fp = cgdebug_fp(state);
19371 va_start(args, fmt);
19372 vfprintf(fp, fmt, args);
19377 static void cgdebug_flush(struct compile_state *state)
19380 fp = cgdebug_fp(state);
19386 static void cgdebug_loc(struct compile_state *state, struct triple *ins)
19389 fp = cgdebug_fp(state);
19391 loc(fp, state, ins);
19395 static int select_free_color(struct compile_state *state,
19396 struct reg_state *rstate, struct live_range *range)
19398 struct triple_set *entry;
19399 struct live_range_def *lrd;
19400 struct live_range_def *phi;
19401 struct live_range_edge *edge;
19402 char used[MAX_REGISTERS];
19403 struct triple **expr;
19405 /* Instead of doing just the trivial color select here I try
19406 * a few extra things because a good color selection will help reduce
19410 /* Find the registers currently in use */
19411 memset(used, 0, sizeof(used));
19412 for(edge = range->edges; edge; edge = edge->next) {
19413 if (edge->node->color == REG_UNSET) {
19416 reg_fill_used(state, used, edge->node->color);
19419 if (state->compiler->debug & DEBUG_COLOR_GRAPH2) {
19422 for(edge = range->edges; edge; edge = edge->next) {
19425 cgdebug_printf(state, "\n%s edges: %d",
19426 tops(range->defs->def->op), i);
19427 cgdebug_loc(state, range->defs->def);
19428 cgdebug_printf(state, "\n");
19429 for(i = 0; i < MAX_REGISTERS; i++) {
19431 cgdebug_printf(state, "used: %s\n",
19437 /* If a color is already assigned see if it will work */
19438 if (range->color != REG_UNSET) {
19439 struct live_range_def *lrd;
19440 if (!used[range->color]) {
19443 for(edge = range->edges; edge; edge = edge->next) {
19444 if (edge->node->color != range->color) {
19447 warning(state, edge->node->defs->def, "edge: ");
19448 lrd = edge->node->defs;
19450 warning(state, lrd->def, " %p %s",
19451 lrd->def, tops(lrd->def->op));
19453 } while(lrd != edge->node->defs);
19456 warning(state, range->defs->def, "def: ");
19458 warning(state, lrd->def, " %p %s",
19459 lrd->def, tops(lrd->def->op));
19461 } while(lrd != range->defs);
19462 internal_error(state, range->defs->def,
19463 "live range with already used color %s",
19464 arch_reg_str(range->color));
19467 /* If I feed into an expression reuse it's color.
19468 * This should help remove copies in the case of 2 register instructions
19469 * and phi functions.
19472 lrd = live_range_end(state, range, 0);
19473 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
19474 entry = lrd->def->use;
19475 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
19476 struct live_range_def *insd;
19478 insd = &rstate->lrd[entry->member->id];
19479 if (insd->lr->defs == 0) {
19482 if (!phi && (insd->def->op == OP_PHI) &&
19483 !interfere(rstate, range, insd->lr)) {
19486 if (insd->lr->color == REG_UNSET) {
19489 regcm = insd->lr->classes;
19490 if (((regcm & range->classes) == 0) ||
19491 (used[insd->lr->color])) {
19494 if (interfere(rstate, range, insd->lr)) {
19497 range->color = insd->lr->color;
19500 /* If I feed into a phi function reuse it's color or the color
19501 * of something else that feeds into the phi function.
19504 if (phi->lr->color != REG_UNSET) {
19505 if (used[phi->lr->color]) {
19506 range->color = phi->lr->color;
19510 expr = triple_rhs(state, phi->def, 0);
19511 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
19512 struct live_range *lr;
19517 lr = rstate->lrd[(*expr)->id].lr;
19518 if (lr->color == REG_UNSET) {
19521 regcm = lr->classes;
19522 if (((regcm & range->classes) == 0) ||
19523 (used[lr->color])) {
19526 if (interfere(rstate, range, lr)) {
19529 range->color = lr->color;
19533 /* If I don't interfere with a rhs node reuse it's color */
19534 lrd = live_range_head(state, range, 0);
19535 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
19536 expr = triple_rhs(state, lrd->def, 0);
19537 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
19538 struct live_range *lr;
19543 lr = rstate->lrd[(*expr)->id].lr;
19544 if (lr->color == REG_UNSET) {
19547 regcm = lr->classes;
19548 if (((regcm & range->classes) == 0) ||
19549 (used[lr->color])) {
19552 if (interfere(rstate, range, lr)) {
19555 range->color = lr->color;
19559 /* If I have not opportunitically picked a useful color
19560 * pick the first color that is free.
19562 if (range->color == REG_UNSET) {
19564 arch_select_free_register(state, used, range->classes);
19566 if (range->color == REG_UNSET) {
19567 struct live_range_def *lrd;
19569 if (split_ranges(state, rstate, used, range)) {
19572 for(edge = range->edges; edge; edge = edge->next) {
19573 warning(state, edge->node->defs->def, "edge reg %s",
19574 arch_reg_str(edge->node->color));
19575 lrd = edge->node->defs;
19577 warning(state, lrd->def, " %s %p",
19578 tops(lrd->def->op), lrd->def);
19580 } while(lrd != edge->node->defs);
19582 warning(state, range->defs->def, "range: ");
19585 warning(state, lrd->def, " %s %p",
19586 tops(lrd->def->op), lrd->def);
19588 } while(lrd != range->defs);
19590 warning(state, range->defs->def, "classes: %x",
19592 for(i = 0; i < MAX_REGISTERS; i++) {
19594 warning(state, range->defs->def, "used: %s",
19598 error(state, range->defs->def, "too few registers");
19600 range->classes &= arch_reg_regcm(state, range->color);
19601 if ((range->color == REG_UNSET) || (range->classes == 0)) {
19602 internal_error(state, range->defs->def, "select_free_color did not?");
19607 static int color_graph(struct compile_state *state, struct reg_state *rstate)
19610 struct live_range_edge *edge;
19611 struct live_range *range;
19613 cgdebug_printf(state, "Lo: ");
19614 range = rstate->low;
19615 if (*range->group_prev != range) {
19616 internal_error(state, 0, "lo: *prev != range?");
19618 *range->group_prev = range->group_next;
19619 if (range->group_next) {
19620 range->group_next->group_prev = range->group_prev;
19622 if (&range->group_next == rstate->low_tail) {
19623 rstate->low_tail = range->group_prev;
19625 if (rstate->low == range) {
19626 internal_error(state, 0, "low: next != prev?");
19629 else if (rstate->high) {
19630 cgdebug_printf(state, "Hi: ");
19631 range = rstate->high;
19632 if (*range->group_prev != range) {
19633 internal_error(state, 0, "hi: *prev != range?");
19635 *range->group_prev = range->group_next;
19636 if (range->group_next) {
19637 range->group_next->group_prev = range->group_prev;
19639 if (&range->group_next == rstate->high_tail) {
19640 rstate->high_tail = range->group_prev;
19642 if (rstate->high == range) {
19643 internal_error(state, 0, "high: next != prev?");
19649 cgdebug_printf(state, " %d\n", range - rstate->lr);
19650 range->group_prev = 0;
19651 for(edge = range->edges; edge; edge = edge->next) {
19652 struct live_range *node;
19654 /* Move nodes from the high to the low list */
19655 if (node->group_prev && (node->color == REG_UNSET) &&
19656 (node->degree == regc_max_size(state, node->classes))) {
19657 if (*node->group_prev != node) {
19658 internal_error(state, 0, "move: *prev != node?");
19660 *node->group_prev = node->group_next;
19661 if (node->group_next) {
19662 node->group_next->group_prev = node->group_prev;
19664 if (&node->group_next == rstate->high_tail) {
19665 rstate->high_tail = node->group_prev;
19667 cgdebug_printf(state, "Moving...%d to low\n", node - rstate->lr);
19668 node->group_prev = rstate->low_tail;
19669 node->group_next = 0;
19670 *rstate->low_tail = node;
19671 rstate->low_tail = &node->group_next;
19672 if (*node->group_prev != node) {
19673 internal_error(state, 0, "move2: *prev != node?");
19678 colored = color_graph(state, rstate);
19680 cgdebug_printf(state, "Coloring %d @", range - rstate->lr);
19681 cgdebug_loc(state, range->defs->def);
19682 cgdebug_flush(state);
19683 colored = select_free_color(state, rstate, range);
19685 cgdebug_printf(state, " %s\n", arch_reg_str(range->color));
19691 static void verify_colors(struct compile_state *state, struct reg_state *rstate)
19693 struct live_range *lr;
19694 struct live_range_edge *edge;
19695 struct triple *ins, *first;
19696 char used[MAX_REGISTERS];
19697 first = state->first;
19700 if (triple_is_def(state, ins)) {
19701 if ((ins->id < 0) || (ins->id > rstate->defs)) {
19702 internal_error(state, ins,
19703 "triple without a live range def");
19705 lr = rstate->lrd[ins->id].lr;
19706 if (lr->color == REG_UNSET) {
19707 internal_error(state, ins,
19708 "triple without a color");
19710 /* Find the registers used by the edges */
19711 memset(used, 0, sizeof(used));
19712 for(edge = lr->edges; edge; edge = edge->next) {
19713 if (edge->node->color == REG_UNSET) {
19714 internal_error(state, 0,
19715 "live range without a color");
19717 reg_fill_used(state, used, edge->node->color);
19719 if (used[lr->color]) {
19720 internal_error(state, ins,
19721 "triple with already used color");
19725 } while(ins != first);
19728 static void color_triples(struct compile_state *state, struct reg_state *rstate)
19730 struct live_range_def *lrd;
19731 struct live_range *lr;
19732 struct triple *first, *ins;
19733 first = state->first;
19736 if ((ins->id < 0) || (ins->id > rstate->defs)) {
19737 internal_error(state, ins,
19738 "triple without a live range");
19740 lrd = &rstate->lrd[ins->id];
19742 ins->id = lrd->orig_id;
19743 SET_REG(ins->id, lr->color);
19745 } while (ins != first);
19748 static struct live_range *merge_sort_lr(
19749 struct live_range *first, struct live_range *last)
19751 struct live_range *mid, *join, **join_tail, *pick;
19753 size = (last - first) + 1;
19755 mid = first + size/2;
19756 first = merge_sort_lr(first, mid -1);
19757 mid = merge_sort_lr(mid, last);
19761 /* merge the two lists */
19762 while(first && mid) {
19763 if ((first->degree < mid->degree) ||
19764 ((first->degree == mid->degree) &&
19765 (first->length < mid->length))) {
19767 first = first->group_next;
19769 first->group_prev = 0;
19774 mid = mid->group_next;
19776 mid->group_prev = 0;
19779 pick->group_next = 0;
19780 pick->group_prev = join_tail;
19782 join_tail = &pick->group_next;
19784 /* Splice the remaining list */
19785 pick = (first)? first : mid;
19788 pick->group_prev = join_tail;
19792 if (!first->defs) {
19800 static void ids_from_rstate(struct compile_state *state,
19801 struct reg_state *rstate)
19803 struct triple *ins, *first;
19804 if (!rstate->defs) {
19807 /* Display the graph if desired */
19808 if (state->compiler->debug & DEBUG_INTERFERENCE) {
19809 FILE *fp = state->dbgout;
19810 print_interference_blocks(state, rstate, fp, 0);
19811 print_control_flow(state, fp, &state->bb);
19814 first = state->first;
19818 struct live_range_def *lrd;
19819 lrd = &rstate->lrd[ins->id];
19820 ins->id = lrd->orig_id;
19823 } while(ins != first);
19826 static void cleanup_live_edges(struct reg_state *rstate)
19829 /* Free the edges on each node */
19830 for(i = 1; i <= rstate->ranges; i++) {
19831 remove_live_edges(rstate, &rstate->lr[i]);
19835 static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
19837 cleanup_live_edges(rstate);
19838 xfree(rstate->lrd);
19841 /* Free the variable lifetime information */
19842 if (rstate->blocks) {
19843 free_variable_lifetimes(state, &state->bb, rstate->blocks);
19846 rstate->ranges = 0;
19849 rstate->blocks = 0;
19852 static void verify_consistency(struct compile_state *state);
19853 static void allocate_registers(struct compile_state *state)
19855 struct reg_state rstate;
19858 /* Clear out the reg_state */
19859 memset(&rstate, 0, sizeof(rstate));
19860 rstate.max_passes = state->compiler->max_allocation_passes;
19863 struct live_range **point, **next;
19868 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
19869 FILE *fp = state->errout;
19870 fprintf(fp, "pass: %d\n", rstate.passes);
19875 ids_from_rstate(state, &rstate);
19877 /* Cleanup the temporary data structures */
19878 cleanup_rstate(state, &rstate);
19880 /* Compute the variable lifetimes */
19881 rstate.blocks = compute_variable_lifetimes(state, &state->bb);
19883 /* Fix invalid mandatory live range coalesce conflicts */
19884 conflicts = correct_coalesce_conflicts(state, rstate.blocks);
19886 /* Fix two simultaneous uses of the same register.
19887 * In a few pathlogical cases a partial untangle moves
19888 * the tangle to a part of the graph we won't revisit.
19889 * So we keep looping until we have no more tangle fixes
19893 tangles = correct_tangles(state, rstate.blocks);
19897 print_blocks(state, "resolve_tangles", state->dbgout);
19898 verify_consistency(state);
19900 /* Allocate and initialize the live ranges */
19901 initialize_live_ranges(state, &rstate);
19903 /* Note currently doing coalescing in a loop appears to
19904 * buys me nothing. The code is left this way in case
19905 * there is some value in it. Or if a future bugfix
19906 * yields some benefit.
19909 if (state->compiler->debug & DEBUG_COALESCING) {
19910 fprintf(state->errout, "coalescing\n");
19913 /* Remove any previous live edge calculations */
19914 cleanup_live_edges(&rstate);
19916 /* Compute the interference graph */
19917 walk_variable_lifetimes(
19918 state, &state->bb, rstate.blocks,
19919 graph_ins, &rstate);
19921 /* Display the interference graph if desired */
19922 if (state->compiler->debug & DEBUG_INTERFERENCE) {
19923 print_interference_blocks(state, &rstate, state->dbgout, 1);
19924 fprintf(state->dbgout, "\nlive variables by instruction\n");
19925 walk_variable_lifetimes(
19926 state, &state->bb, rstate.blocks,
19927 print_interference_ins, &rstate);
19930 coalesced = coalesce_live_ranges(state, &rstate);
19932 if (state->compiler->debug & DEBUG_COALESCING) {
19933 fprintf(state->errout, "coalesced: %d\n", coalesced);
19935 } while(coalesced);
19937 #if DEBUG_CONSISTENCY > 1
19939 fprintf(state->errout, "verify_graph_ins...\n");
19941 /* Verify the interference graph */
19942 walk_variable_lifetimes(
19943 state, &state->bb, rstate.blocks,
19944 verify_graph_ins, &rstate);
19946 fprintf(state->errout, "verify_graph_ins done\n");
19950 /* Build the groups low and high. But with the nodes
19951 * first sorted by degree order.
19953 rstate.low_tail = &rstate.low;
19954 rstate.high_tail = &rstate.high;
19955 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
19957 rstate.high->group_prev = &rstate.high;
19959 for(point = &rstate.high; *point; point = &(*point)->group_next)
19961 rstate.high_tail = point;
19962 /* Walk through the high list and move everything that needs
19965 for(point = &rstate.high; *point; point = next) {
19966 struct live_range *range;
19967 next = &(*point)->group_next;
19970 /* If it has a low degree or it already has a color
19971 * place the node in low.
19973 if ((range->degree < regc_max_size(state, range->classes)) ||
19974 (range->color != REG_UNSET)) {
19975 cgdebug_printf(state, "Lo: %5d degree %5d%s\n",
19976 range - rstate.lr, range->degree,
19977 (range->color != REG_UNSET) ? " (colored)": "");
19978 *range->group_prev = range->group_next;
19979 if (range->group_next) {
19980 range->group_next->group_prev = range->group_prev;
19982 if (&range->group_next == rstate.high_tail) {
19983 rstate.high_tail = range->group_prev;
19985 range->group_prev = rstate.low_tail;
19986 range->group_next = 0;
19987 *rstate.low_tail = range;
19988 rstate.low_tail = &range->group_next;
19992 cgdebug_printf(state, "hi: %5d degree %5d%s\n",
19993 range - rstate.lr, range->degree,
19994 (range->color != REG_UNSET) ? " (colored)": "");
19997 /* Color the live_ranges */
19998 colored = color_graph(state, &rstate);
20000 } while (!colored);
20002 /* Verify the graph was properly colored */
20003 verify_colors(state, &rstate);
20005 /* Move the colors from the graph to the triples */
20006 color_triples(state, &rstate);
20008 /* Cleanup the temporary data structures */
20009 cleanup_rstate(state, &rstate);
20011 /* Display the new graph */
20012 print_blocks(state, __func__, state->dbgout);
20015 /* Sparce Conditional Constant Propogation
20016 * =========================================
20020 struct lattice_node {
20022 struct triple *def;
20023 struct ssa_edge *out;
20024 struct flow_block *fblock;
20025 struct triple *val;
20026 /* lattice high val == def
20027 * lattice const is_const(val)
20028 * lattice low other
20032 struct lattice_node *src;
20033 struct lattice_node *dst;
20034 struct ssa_edge *work_next;
20035 struct ssa_edge *work_prev;
20036 struct ssa_edge *out_next;
20039 struct flow_block *src;
20040 struct flow_block *dst;
20041 struct flow_edge *work_next;
20042 struct flow_edge *work_prev;
20043 struct flow_edge *in_next;
20044 struct flow_edge *out_next;
20047 #define MAX_FLOW_BLOCK_EDGES 3
20048 struct flow_block {
20049 struct block *block;
20050 struct flow_edge *in;
20051 struct flow_edge *out;
20052 struct flow_edge *edges;
20057 struct lattice_node *lattice;
20058 struct ssa_edge *ssa_edges;
20059 struct flow_block *flow_blocks;
20060 struct flow_edge *flow_work_list;
20061 struct ssa_edge *ssa_work_list;
20065 static int is_scc_const(struct compile_state *state, struct triple *ins)
20067 return ins && (triple_is_ubranch(state, ins) || is_const(ins));
20070 static int is_lattice_hi(struct compile_state *state, struct lattice_node *lnode)
20072 return !is_scc_const(state, lnode->val) && (lnode->val == lnode->def);
20075 static int is_lattice_const(struct compile_state *state, struct lattice_node *lnode)
20077 return is_scc_const(state, lnode->val);
20080 static int is_lattice_lo(struct compile_state *state, struct lattice_node *lnode)
20082 return (lnode->val != lnode->def) && !is_scc_const(state, lnode->val);
20085 static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
20086 struct flow_edge *fedge)
20088 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
20089 fprintf(state->errout, "adding fedge: %p (%4d -> %5d)\n",
20091 fedge->src->block?fedge->src->block->last->id: 0,
20092 fedge->dst->block?fedge->dst->block->first->id: 0);
20094 if ((fedge == scc->flow_work_list) ||
20095 (fedge->work_next != fedge) ||
20096 (fedge->work_prev != fedge)) {
20098 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
20099 fprintf(state->errout, "dupped fedge: %p\n",
20104 if (!scc->flow_work_list) {
20105 scc->flow_work_list = fedge;
20106 fedge->work_next = fedge->work_prev = fedge;
20109 struct flow_edge *ftail;
20110 ftail = scc->flow_work_list->work_prev;
20111 fedge->work_next = ftail->work_next;
20112 fedge->work_prev = ftail;
20113 fedge->work_next->work_prev = fedge;
20114 fedge->work_prev->work_next = fedge;
20118 static struct flow_edge *scc_next_fedge(
20119 struct compile_state *state, struct scc_state *scc)
20121 struct flow_edge *fedge;
20122 fedge = scc->flow_work_list;
20124 fedge->work_next->work_prev = fedge->work_prev;
20125 fedge->work_prev->work_next = fedge->work_next;
20126 if (fedge->work_next != fedge) {
20127 scc->flow_work_list = fedge->work_next;
20129 scc->flow_work_list = 0;
20131 fedge->work_next = fedge->work_prev = fedge;
20136 static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
20137 struct ssa_edge *sedge)
20139 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
20140 fprintf(state->errout, "adding sedge: %5d (%4d -> %5d)\n",
20141 sedge - scc->ssa_edges,
20142 sedge->src->def->id,
20143 sedge->dst->def->id);
20145 if ((sedge == scc->ssa_work_list) ||
20146 (sedge->work_next != sedge) ||
20147 (sedge->work_prev != sedge)) {
20149 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
20150 fprintf(state->errout, "dupped sedge: %5d\n",
20151 sedge - scc->ssa_edges);
20155 if (!scc->ssa_work_list) {
20156 scc->ssa_work_list = sedge;
20157 sedge->work_next = sedge->work_prev = sedge;
20160 struct ssa_edge *stail;
20161 stail = scc->ssa_work_list->work_prev;
20162 sedge->work_next = stail->work_next;
20163 sedge->work_prev = stail;
20164 sedge->work_next->work_prev = sedge;
20165 sedge->work_prev->work_next = sedge;
20169 static struct ssa_edge *scc_next_sedge(
20170 struct compile_state *state, struct scc_state *scc)
20172 struct ssa_edge *sedge;
20173 sedge = scc->ssa_work_list;
20175 sedge->work_next->work_prev = sedge->work_prev;
20176 sedge->work_prev->work_next = sedge->work_next;
20177 if (sedge->work_next != sedge) {
20178 scc->ssa_work_list = sedge->work_next;
20180 scc->ssa_work_list = 0;
20182 sedge->work_next = sedge->work_prev = sedge;
20187 static void initialize_scc_state(
20188 struct compile_state *state, struct scc_state *scc)
20190 int ins_count, ssa_edge_count;
20191 int ins_index, ssa_edge_index, fblock_index;
20192 struct triple *first, *ins;
20193 struct block *block;
20194 struct flow_block *fblock;
20196 memset(scc, 0, sizeof(*scc));
20198 /* Inialize pass zero find out how much memory we need */
20199 first = state->first;
20201 ins_count = ssa_edge_count = 0;
20203 struct triple_set *edge;
20205 for(edge = ins->use; edge; edge = edge->next) {
20209 } while(ins != first);
20210 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20211 fprintf(state->errout, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
20212 ins_count, ssa_edge_count, state->bb.last_vertex);
20214 scc->ins_count = ins_count;
20216 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
20218 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
20220 xcmalloc(sizeof(*scc->flow_blocks)*(state->bb.last_vertex + 1),
20223 /* Initialize pass one collect up the nodes */
20226 ins_index = ssa_edge_index = fblock_index = 0;
20229 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20230 block = ins->u.block;
20232 internal_error(state, ins, "label without block");
20235 block->vertex = fblock_index;
20236 fblock = &scc->flow_blocks[fblock_index];
20237 fblock->block = block;
20238 fblock->edges = xcmalloc(sizeof(*fblock->edges)*block->edge_count,
20242 struct lattice_node *lnode;
20244 lnode = &scc->lattice[ins_index];
20247 lnode->fblock = fblock;
20248 lnode->val = ins; /* LATTICE HIGH */
20249 if (lnode->val->op == OP_UNKNOWNVAL) {
20250 lnode->val = 0; /* LATTICE LOW by definition */
20252 lnode->old_id = ins->id;
20253 ins->id = ins_index;
20256 } while(ins != first);
20257 /* Initialize pass two collect up the edges */
20263 struct triple_set *edge;
20264 struct ssa_edge **stail;
20265 struct lattice_node *lnode;
20266 lnode = &scc->lattice[ins->id];
20268 stail = &lnode->out;
20269 for(edge = ins->use; edge; edge = edge->next) {
20270 struct ssa_edge *sedge;
20271 ssa_edge_index += 1;
20272 sedge = &scc->ssa_edges[ssa_edge_index];
20274 stail = &sedge->out_next;
20275 sedge->src = lnode;
20276 sedge->dst = &scc->lattice[edge->member->id];
20277 sedge->work_next = sedge->work_prev = sedge;
20278 sedge->out_next = 0;
20281 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20282 struct flow_edge *fedge, **ftail;
20283 struct block_set *bedge;
20284 block = ins->u.block;
20285 fblock = &scc->flow_blocks[block->vertex];
20288 ftail = &fblock->out;
20290 fedge = fblock->edges;
20291 bedge = block->edges;
20292 for(; bedge; bedge = bedge->next, fedge++) {
20293 fedge->dst = &scc->flow_blocks[bedge->member->vertex];
20294 if (fedge->dst->block != bedge->member) {
20295 internal_error(state, 0, "block mismatch");
20298 ftail = &fedge->out_next;
20299 fedge->out_next = 0;
20301 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
20302 fedge->src = fblock;
20303 fedge->work_next = fedge->work_prev = fedge;
20304 fedge->executable = 0;
20308 } while (ins != first);
20313 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20314 struct flow_edge **ftail;
20315 struct block_set *bedge;
20316 block = ins->u.block;
20317 fblock = &scc->flow_blocks[block->vertex];
20318 ftail = &fblock->in;
20319 for(bedge = block->use; bedge; bedge = bedge->next) {
20320 struct block *src_block;
20321 struct flow_block *sfblock;
20322 struct flow_edge *sfedge;
20323 src_block = bedge->member;
20324 sfblock = &scc->flow_blocks[src_block->vertex];
20325 for(sfedge = sfblock->out; sfedge; sfedge = sfedge->out_next) {
20326 if (sfedge->dst == fblock) {
20331 internal_error(state, 0, "edge mismatch");
20334 ftail = &sfedge->in_next;
20335 sfedge->in_next = 0;
20339 } while(ins != first);
20340 /* Setup a dummy block 0 as a node above the start node */
20342 struct flow_block *fblock, *dst;
20343 struct flow_edge *fedge;
20344 fblock = &scc->flow_blocks[0];
20346 fblock->edges = xcmalloc(sizeof(*fblock->edges)*1, "flow_edges");
20348 fblock->out = fblock->edges;
20349 dst = &scc->flow_blocks[state->bb.first_block->vertex];
20350 fedge = fblock->edges;
20351 fedge->src = fblock;
20353 fedge->work_next = fedge;
20354 fedge->work_prev = fedge;
20355 fedge->in_next = fedge->dst->in;
20356 fedge->out_next = 0;
20357 fedge->executable = 0;
20358 fedge->dst->in = fedge;
20360 /* Initialize the work lists */
20361 scc->flow_work_list = 0;
20362 scc->ssa_work_list = 0;
20363 scc_add_fedge(state, scc, fedge);
20365 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20366 fprintf(state->errout, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
20367 ins_index, ssa_edge_index, fblock_index);
20372 static void free_scc_state(
20373 struct compile_state *state, struct scc_state *scc)
20376 for(i = 0; i < state->bb.last_vertex + 1; i++) {
20377 struct flow_block *fblock;
20378 fblock = &scc->flow_blocks[i];
20379 if (fblock->edges) {
20380 xfree(fblock->edges);
20384 xfree(scc->flow_blocks);
20385 xfree(scc->ssa_edges);
20386 xfree(scc->lattice);
20390 static struct lattice_node *triple_to_lattice(
20391 struct compile_state *state, struct scc_state *scc, struct triple *ins)
20393 if (ins->id <= 0) {
20394 internal_error(state, ins, "bad id");
20396 return &scc->lattice[ins->id];
20399 static struct triple *preserve_lval(
20400 struct compile_state *state, struct lattice_node *lnode)
20402 struct triple *old;
20403 /* Preserve the original value */
20405 old = dup_triple(state, lnode->val);
20406 if (lnode->val != lnode->def) {
20416 static int lval_changed(struct compile_state *state,
20417 struct triple *old, struct lattice_node *lnode)
20420 /* See if the lattice value has changed */
20422 if (!old && !lnode->val) {
20426 lnode->val && old &&
20427 (memcmp(lnode->val->param, old->param,
20428 TRIPLE_SIZE(lnode->val) * sizeof(lnode->val->param[0])) == 0) &&
20429 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
20439 static void scc_debug_lnode(
20440 struct compile_state *state, struct scc_state *scc,
20441 struct lattice_node *lnode, int changed)
20443 if ((state->compiler->debug & DEBUG_SCC_TRANSFORM2) && lnode->val) {
20444 display_triple_changes(state->errout, lnode->val, lnode->def);
20446 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20447 FILE *fp = state->errout;
20448 struct triple *val, **expr;
20449 val = lnode->val? lnode->val : lnode->def;
20450 fprintf(fp, "%p %s %3d %10s (",
20452 ((lnode->def->op == OP_PHI)? "phi: ": "expr:"),
20454 tops(lnode->def->op));
20455 expr = triple_rhs(state, lnode->def, 0);
20456 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
20458 fprintf(fp, " %d", (*expr)->id);
20461 if (val->op == OP_INTCONST) {
20462 fprintf(fp, " <0x%08lx>", (unsigned long)(val->u.cval));
20464 fprintf(fp, " ) -> %s %s\n",
20465 (is_lattice_hi(state, lnode)? "hi":
20466 is_lattice_const(state, lnode)? "const" : "lo"),
20467 changed? "changed" : ""
20472 static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
20473 struct lattice_node *lnode)
20476 struct triple *old, *scratch;
20477 struct triple **dexpr, **vexpr;
20480 /* Store the original value */
20481 old = preserve_lval(state, lnode);
20483 /* Reinitialize the value */
20484 lnode->val = scratch = dup_triple(state, lnode->def);
20485 scratch->id = lnode->old_id;
20486 scratch->next = scratch;
20487 scratch->prev = scratch;
20490 count = TRIPLE_SIZE(scratch);
20491 for(i = 0; i < count; i++) {
20492 dexpr = &lnode->def->param[i];
20493 vexpr = &scratch->param[i];
20495 if (((i < TRIPLE_MISC_OFF(scratch)) ||
20496 (i >= TRIPLE_TARG_OFF(scratch))) &&
20498 struct lattice_node *tmp;
20499 tmp = triple_to_lattice(state, scc, *dexpr);
20500 *vexpr = (tmp->val)? tmp->val : tmp->def;
20503 if (triple_is_branch(state, scratch)) {
20504 scratch->next = lnode->def->next;
20506 /* Recompute the value */
20507 #warning "FIXME see if simplify does anything bad"
20508 /* So far it looks like only the strength reduction
20509 * optimization are things I need to worry about.
20511 simplify(state, scratch);
20512 /* Cleanup my value */
20513 if (scratch->use) {
20514 internal_error(state, lnode->def, "scratch used?");
20516 if ((scratch->prev != scratch) ||
20517 ((scratch->next != scratch) &&
20518 (!triple_is_branch(state, lnode->def) ||
20519 (scratch->next != lnode->def->next)))) {
20520 internal_error(state, lnode->def, "scratch in list?");
20522 /* undo any uses... */
20523 count = TRIPLE_SIZE(scratch);
20524 for(i = 0; i < count; i++) {
20525 vexpr = &scratch->param[i];
20527 unuse_triple(*vexpr, scratch);
20530 if (lnode->val->op == OP_UNKNOWNVAL) {
20531 lnode->val = 0; /* Lattice low by definition */
20533 /* Find the case when I am lattice high */
20535 (lnode->val->op == lnode->def->op) &&
20536 (memcmp(lnode->val->param, lnode->def->param,
20537 count * sizeof(lnode->val->param[0])) == 0) &&
20538 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
20539 lnode->val = lnode->def;
20541 /* Only allow lattice high when all of my inputs
20542 * are also lattice high. Occassionally I can
20543 * have constants with a lattice low input, so
20544 * I do not need to check that case.
20546 if (is_lattice_hi(state, lnode)) {
20547 struct lattice_node *tmp;
20549 rhs = lnode->val->rhs;
20550 for(i = 0; i < rhs; i++) {
20551 tmp = triple_to_lattice(state, scc, RHS(lnode->val, i));
20552 if (!is_lattice_hi(state, tmp)) {
20558 /* Find the cases that are always lattice lo */
20560 triple_is_def(state, lnode->val) &&
20561 !triple_is_pure(state, lnode->val, lnode->old_id)) {
20564 /* See if the lattice value has changed */
20565 changed = lval_changed(state, old, lnode);
20566 /* See if this value should not change */
20567 if ((lnode->val != lnode->def) &&
20568 (( !triple_is_def(state, lnode->def) &&
20569 !triple_is_cbranch(state, lnode->def)) ||
20570 (lnode->def->op == OP_PIECE))) {
20571 #warning "FIXME constant propogate through expressions with multiple left hand sides"
20573 internal_warning(state, lnode->def, "non def changes value?");
20578 /* See if we need to free the scratch value */
20579 if (lnode->val != scratch) {
20587 static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc,
20588 struct lattice_node *lnode)
20590 struct lattice_node *cond;
20591 struct flow_edge *left, *right;
20594 /* Update the branch value */
20595 changed = compute_lnode_val(state, scc, lnode);
20596 scc_debug_lnode(state, scc, lnode, changed);
20598 /* This only applies to conditional branches */
20599 if (!triple_is_cbranch(state, lnode->def)) {
20600 internal_error(state, lnode->def, "not a conditional branch");
20603 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20604 struct flow_edge *fedge;
20605 FILE *fp = state->errout;
20606 fprintf(fp, "%s: %d (",
20607 tops(lnode->def->op),
20610 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
20611 fprintf(fp, " %d", fedge->dst->block->vertex);
20614 if (lnode->def->rhs > 0) {
20615 fprintf(fp, " <- %d",
20616 RHS(lnode->def, 0)->id);
20620 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
20621 for(left = cond->fblock->out; left; left = left->out_next) {
20622 if (left->dst->block->first == lnode->def->next) {
20627 internal_error(state, lnode->def, "Cannot find left branch edge");
20629 for(right = cond->fblock->out; right; right = right->out_next) {
20630 if (right->dst->block->first == TARG(lnode->def, 0)) {
20635 internal_error(state, lnode->def, "Cannot find right branch edge");
20637 /* I should only come here if the controlling expressions value
20638 * has changed, which means it must be either a constant or lo.
20640 if (is_lattice_hi(state, cond)) {
20641 internal_error(state, cond->def, "condition high?");
20644 if (is_lattice_lo(state, cond)) {
20645 scc_add_fedge(state, scc, left);
20646 scc_add_fedge(state, scc, right);
20648 else if (cond->val->u.cval) {
20649 scc_add_fedge(state, scc, right);
20651 scc_add_fedge(state, scc, left);
20657 static void scc_add_sedge_dst(struct compile_state *state,
20658 struct scc_state *scc, struct ssa_edge *sedge)
20660 if (triple_is_cbranch(state, sedge->dst->def)) {
20661 scc_visit_cbranch(state, scc, sedge->dst);
20663 else if (triple_is_def(state, sedge->dst->def)) {
20664 scc_add_sedge(state, scc, sedge);
20668 static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
20669 struct lattice_node *lnode)
20671 struct lattice_node *tmp;
20672 struct triple **slot, *old;
20673 struct flow_edge *fedge;
20676 if (lnode->def->op != OP_PHI) {
20677 internal_error(state, lnode->def, "not phi");
20679 /* Store the original value */
20680 old = preserve_lval(state, lnode);
20682 /* default to lattice high */
20683 lnode->val = lnode->def;
20684 slot = &RHS(lnode->def, 0);
20686 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
20687 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20688 fprintf(state->errout, "Examining edge: %d vertex: %d executable: %d\n",
20690 fedge->dst->block->vertex,
20694 if (!fedge->executable) {
20697 if (!slot[index]) {
20698 internal_error(state, lnode->def, "no phi value");
20700 tmp = triple_to_lattice(state, scc, slot[index]);
20701 /* meet(X, lattice low) = lattice low */
20702 if (is_lattice_lo(state, tmp)) {
20705 /* meet(X, lattice high) = X */
20706 else if (is_lattice_hi(state, tmp)) {
20707 lnode->val = lnode->val;
20709 /* meet(lattice high, X) = X */
20710 else if (is_lattice_hi(state, lnode)) {
20711 lnode->val = dup_triple(state, tmp->val);
20712 /* Only change the type if necessary */
20713 if (!is_subset_type(lnode->def->type, tmp->val->type)) {
20714 lnode->val->type = lnode->def->type;
20717 /* meet(const, const) = const or lattice low */
20718 else if (!constants_equal(state, lnode->val, tmp->val)) {
20722 /* meet(lattice low, X) = lattice low */
20723 if (is_lattice_lo(state, lnode)) {
20728 changed = lval_changed(state, old, lnode);
20729 scc_debug_lnode(state, scc, lnode, changed);
20731 /* If the lattice value has changed update the work lists. */
20733 struct ssa_edge *sedge;
20734 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
20735 scc_add_sedge_dst(state, scc, sedge);
20741 static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
20742 struct lattice_node *lnode)
20746 if (!triple_is_def(state, lnode->def)) {
20747 internal_warning(state, lnode->def, "not visiting an expression?");
20749 changed = compute_lnode_val(state, scc, lnode);
20750 scc_debug_lnode(state, scc, lnode, changed);
20753 struct ssa_edge *sedge;
20754 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
20755 scc_add_sedge_dst(state, scc, sedge);
20760 static void scc_writeback_values(
20761 struct compile_state *state, struct scc_state *scc)
20763 struct triple *first, *ins;
20764 first = state->first;
20767 struct lattice_node *lnode;
20768 lnode = triple_to_lattice(state, scc, ins);
20769 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20770 if (is_lattice_hi(state, lnode) &&
20771 (lnode->val->op != OP_NOOP))
20773 struct flow_edge *fedge;
20776 for(fedge = lnode->fblock->in;
20777 !executable && fedge; fedge = fedge->in_next) {
20778 executable |= fedge->executable;
20781 internal_warning(state, lnode->def,
20782 "lattice node %d %s->%s still high?",
20784 tops(lnode->def->op),
20785 tops(lnode->val->op));
20791 ins->id = lnode->old_id;
20792 if (lnode->val && (lnode->val != ins)) {
20793 /* See if it something I know how to write back */
20794 switch(lnode->val->op) {
20796 mkconst(state, ins, lnode->val->u.cval);
20799 mkaddr_const(state, ins,
20800 MISC(lnode->val, 0), lnode->val->u.cval);
20803 /* By default don't copy the changes,
20804 * recompute them in place instead.
20806 simplify(state, ins);
20809 if (is_const(lnode->val) &&
20810 !constants_equal(state, lnode->val, ins)) {
20811 internal_error(state, 0, "constants not equal");
20813 /* Free the lattice nodes */
20818 } while(ins != first);
20821 static void scc_transform(struct compile_state *state)
20823 struct scc_state scc;
20824 if (!(state->compiler->flags & COMPILER_SCC_TRANSFORM)) {
20828 initialize_scc_state(state, &scc);
20830 while(scc.flow_work_list || scc.ssa_work_list) {
20831 struct flow_edge *fedge;
20832 struct ssa_edge *sedge;
20833 struct flow_edge *fptr;
20834 while((fedge = scc_next_fedge(state, &scc))) {
20835 struct block *block;
20836 struct triple *ptr;
20837 struct flow_block *fblock;
20840 if (fedge->executable) {
20844 internal_error(state, 0, "fedge without dst");
20847 internal_error(state, 0, "fedge without src");
20849 fedge->executable = 1;
20850 fblock = fedge->dst;
20851 block = fblock->block;
20853 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
20854 if (fptr->executable) {
20859 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20860 fprintf(state->errout, "vertex: %d reps: %d\n",
20861 block->vertex, reps);
20865 for(ptr = block->first; !done; ptr = ptr->next) {
20866 struct lattice_node *lnode;
20867 done = (ptr == block->last);
20868 lnode = &scc.lattice[ptr->id];
20869 if (ptr->op == OP_PHI) {
20870 scc_visit_phi(state, &scc, lnode);
20872 else if ((reps == 1) && triple_is_def(state, ptr))
20874 scc_visit_expr(state, &scc, lnode);
20877 /* Add unconditional branch edges */
20878 if (!triple_is_cbranch(state, fblock->block->last)) {
20879 struct flow_edge *out;
20880 for(out = fblock->out; out; out = out->out_next) {
20881 scc_add_fedge(state, &scc, out);
20885 while((sedge = scc_next_sedge(state, &scc))) {
20886 struct lattice_node *lnode;
20887 struct flow_block *fblock;
20888 lnode = sedge->dst;
20889 fblock = lnode->fblock;
20891 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20892 fprintf(state->errout, "sedge: %5d (%5d -> %5d)\n",
20893 sedge - scc.ssa_edges,
20894 sedge->src->def->id,
20895 sedge->dst->def->id);
20898 if (lnode->def->op == OP_PHI) {
20899 scc_visit_phi(state, &scc, lnode);
20902 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
20903 if (fptr->executable) {
20908 scc_visit_expr(state, &scc, lnode);
20914 scc_writeback_values(state, &scc);
20915 free_scc_state(state, &scc);
20916 rebuild_ssa_form(state);
20918 print_blocks(state, __func__, state->dbgout);
20922 static void transform_to_arch_instructions(struct compile_state *state)
20924 struct triple *ins, *first;
20925 first = state->first;
20928 ins = transform_to_arch_instruction(state, ins);
20929 } while(ins != first);
20931 print_blocks(state, __func__, state->dbgout);
20934 #if DEBUG_CONSISTENCY
20935 static void verify_uses(struct compile_state *state)
20937 struct triple *first, *ins;
20938 struct triple_set *set;
20939 first = state->first;
20942 struct triple **expr;
20943 expr = triple_rhs(state, ins, 0);
20944 for(; expr; expr = triple_rhs(state, ins, expr)) {
20945 struct triple *rhs;
20947 for(set = rhs?rhs->use:0; set; set = set->next) {
20948 if (set->member == ins) {
20953 internal_error(state, ins, "rhs not used");
20956 expr = triple_lhs(state, ins, 0);
20957 for(; expr; expr = triple_lhs(state, ins, expr)) {
20958 struct triple *lhs;
20960 for(set = lhs?lhs->use:0; set; set = set->next) {
20961 if (set->member == ins) {
20966 internal_error(state, ins, "lhs not used");
20969 expr = triple_misc(state, ins, 0);
20970 if (ins->op != OP_PHI) {
20971 for(; expr; expr = triple_targ(state, ins, expr)) {
20972 struct triple *misc;
20974 for(set = misc?misc->use:0; set; set = set->next) {
20975 if (set->member == ins) {
20980 internal_error(state, ins, "misc not used");
20984 if (!triple_is_ret(state, ins)) {
20985 expr = triple_targ(state, ins, 0);
20986 for(; expr; expr = triple_targ(state, ins, expr)) {
20987 struct triple *targ;
20989 for(set = targ?targ->use:0; set; set = set->next) {
20990 if (set->member == ins) {
20995 internal_error(state, ins, "targ not used");
21000 } while(ins != first);
21003 static void verify_blocks_present(struct compile_state *state)
21005 struct triple *first, *ins;
21006 if (!state->bb.first_block) {
21009 first = state->first;
21012 valid_ins(state, ins);
21013 if (triple_stores_block(state, ins)) {
21014 if (!ins->u.block) {
21015 internal_error(state, ins,
21016 "%p not in a block?", ins);
21020 } while(ins != first);
21025 static int edge_present(struct compile_state *state, struct block *block, struct triple *edge)
21027 struct block_set *bedge;
21028 struct block *targ;
21029 targ = block_of_triple(state, edge);
21030 for(bedge = block->edges; bedge; bedge = bedge->next) {
21031 if (bedge->member == targ) {
21038 static void verify_blocks(struct compile_state *state)
21040 struct triple *ins;
21041 struct block *block;
21043 block = state->bb.first_block;
21050 struct block_set *user, *edge;
21052 for(ins = block->first; ins != block->last->next; ins = ins->next) {
21053 if (triple_stores_block(state, ins) && (ins->u.block != block)) {
21054 internal_error(state, ins, "inconsitent block specified");
21056 valid_ins(state, ins);
21059 for(user = block->use; user; user = user->next) {
21061 if (!user->member->first) {
21062 internal_error(state, block->first, "user is empty");
21064 if ((block == state->bb.last_block) &&
21065 (user->member == state->bb.first_block)) {
21068 for(edge = user->member->edges; edge; edge = edge->next) {
21069 if (edge->member == block) {
21074 internal_error(state, user->member->first,
21075 "user does not use block");
21078 if (triple_is_branch(state, block->last)) {
21079 struct triple **expr;
21080 expr = triple_edge_targ(state, block->last, 0);
21081 for(;expr; expr = triple_edge_targ(state, block->last, expr)) {
21082 if (*expr && !edge_present(state, block, *expr)) {
21083 internal_error(state, block->last, "no edge to targ");
21087 if (!triple_is_ubranch(state, block->last) &&
21088 (block != state->bb.last_block) &&
21089 !edge_present(state, block, block->last->next)) {
21090 internal_error(state, block->last, "no edge to block->last->next");
21092 for(edge = block->edges; edge; edge = edge->next) {
21093 for(user = edge->member->use; user; user = user->next) {
21094 if (user->member == block) {
21098 if (!user || user->member != block) {
21099 internal_error(state, block->first,
21100 "block does not use edge");
21102 if (!edge->member->first) {
21103 internal_error(state, block->first, "edge block is empty");
21106 if (block->users != users) {
21107 internal_error(state, block->first,
21108 "computed users %d != stored users %d",
21109 users, block->users);
21111 if (!triple_stores_block(state, block->last->next)) {
21112 internal_error(state, block->last->next,
21113 "cannot find next block");
21115 block = block->last->next->u.block;
21117 internal_error(state, block->last->next,
21120 } while(block != state->bb.first_block);
21121 if (blocks != state->bb.last_vertex) {
21122 internal_error(state, 0, "computed blocks: %d != stored blocks %d",
21123 blocks, state->bb.last_vertex);
21127 static void verify_domination(struct compile_state *state)
21129 struct triple *first, *ins;
21130 struct triple_set *set;
21131 if (!state->bb.first_block) {
21135 first = state->first;
21138 for(set = ins->use; set; set = set->next) {
21139 struct triple **slot;
21140 struct triple *use_point;
21143 zrhs = set->member->rhs;
21144 slot = &RHS(set->member, 0);
21145 /* See if the use is on the right hand side */
21146 for(i = 0; i < zrhs; i++) {
21147 if (slot[i] == ins) {
21152 use_point = set->member;
21153 if (set->member->op == OP_PHI) {
21154 struct block_set *bset;
21156 bset = set->member->u.block->use;
21157 for(edge = 0; bset && (edge < i); edge++) {
21161 internal_error(state, set->member,
21162 "no edge for phi rhs %d", i);
21164 use_point = bset->member->last;
21168 !tdominates(state, ins, use_point)) {
21169 if (is_const(ins)) {
21170 internal_warning(state, ins,
21171 "non dominated rhs use point %p?", use_point);
21174 internal_error(state, ins,
21175 "non dominated rhs use point %p?", use_point);
21180 } while(ins != first);
21183 static void verify_rhs(struct compile_state *state)
21185 struct triple *first, *ins;
21186 first = state->first;
21189 struct triple **slot;
21192 slot = &RHS(ins, 0);
21193 for(i = 0; i < zrhs; i++) {
21194 if (slot[i] == 0) {
21195 internal_error(state, ins,
21196 "missing rhs %d on %s",
21199 if ((ins->op != OP_PHI) && (slot[i] == ins)) {
21200 internal_error(state, ins,
21201 "ins == rhs[%d] on %s",
21206 } while(ins != first);
21209 static void verify_piece(struct compile_state *state)
21211 struct triple *first, *ins;
21212 first = state->first;
21215 struct triple *ptr;
21218 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
21219 if (ptr != LHS(ins, i)) {
21220 internal_error(state, ins, "malformed lhs on %s",
21223 if (ptr->op != OP_PIECE) {
21224 internal_error(state, ins, "bad lhs op %s at %d on %s",
21225 tops(ptr->op), i, tops(ins->op));
21227 if (ptr->u.cval != i) {
21228 internal_error(state, ins, "bad u.cval of %d %d expected",
21233 } while(ins != first);
21236 static void verify_ins_colors(struct compile_state *state)
21238 struct triple *first, *ins;
21240 first = state->first;
21244 } while(ins != first);
21247 static void verify_unknown(struct compile_state *state)
21249 struct triple *first, *ins;
21250 if ( (unknown_triple.next != &unknown_triple) ||
21251 (unknown_triple.prev != &unknown_triple) ||
21253 (unknown_triple.use != 0) ||
21255 (unknown_triple.op != OP_UNKNOWNVAL) ||
21256 (unknown_triple.lhs != 0) ||
21257 (unknown_triple.rhs != 0) ||
21258 (unknown_triple.misc != 0) ||
21259 (unknown_triple.targ != 0) ||
21260 (unknown_triple.template_id != 0) ||
21261 (unknown_triple.id != -1) ||
21262 (unknown_triple.type != &unknown_type) ||
21263 (unknown_triple.occurance != &dummy_occurance) ||
21264 (unknown_triple.param[0] != 0) ||
21265 (unknown_triple.param[1] != 0)) {
21266 internal_error(state, &unknown_triple, "unknown_triple corrupted!");
21268 if ( (dummy_occurance.count != 2) ||
21269 (strcmp(dummy_occurance.filename, __FILE__) != 0) ||
21270 (strcmp(dummy_occurance.function, "") != 0) ||
21271 (dummy_occurance.col != 0) ||
21272 (dummy_occurance.parent != 0)) {
21273 internal_error(state, &unknown_triple, "dummy_occurance corrupted!");
21275 if ( (unknown_type.type != TYPE_UNKNOWN)) {
21276 internal_error(state, &unknown_triple, "unknown_type corrupted!");
21278 first = state->first;
21282 if (ins == &unknown_triple) {
21283 internal_error(state, ins, "unknown triple in list");
21285 params = TRIPLE_SIZE(ins);
21286 for(i = 0; i < params; i++) {
21287 if (ins->param[i] == &unknown_triple) {
21288 internal_error(state, ins, "unknown triple used!");
21292 } while(ins != first);
21295 static void verify_types(struct compile_state *state)
21297 struct triple *first, *ins;
21298 first = state->first;
21301 struct type *invalid;
21302 invalid = invalid_type(state, ins->type);
21304 FILE *fp = state->errout;
21305 fprintf(fp, "type: ");
21306 name_of(fp, ins->type);
21308 fprintf(fp, "invalid type: ");
21309 name_of(fp, invalid);
21311 internal_error(state, ins, "invalid ins type");
21313 } while(ins != first);
21316 static void verify_copy(struct compile_state *state)
21318 struct triple *first, *ins, *next;
21319 first = state->first;
21320 next = ins = first;
21324 if (ins->op != OP_COPY) {
21327 if (!equiv_types(ins->type, RHS(ins, 0)->type)) {
21328 FILE *fp = state->errout;
21329 fprintf(fp, "src type: ");
21330 name_of(fp, RHS(ins, 0)->type);
21332 fprintf(fp, "dst type: ");
21333 name_of(fp, ins->type);
21335 internal_error(state, ins, "type mismatch in copy");
21337 } while(next != first);
21340 static void verify_consistency(struct compile_state *state)
21342 verify_unknown(state);
21343 verify_uses(state);
21344 verify_blocks_present(state);
21345 verify_blocks(state);
21346 verify_domination(state);
21348 verify_piece(state);
21349 verify_ins_colors(state);
21350 verify_types(state);
21351 verify_copy(state);
21352 if (state->compiler->debug & DEBUG_VERIFICATION) {
21353 fprintf(state->dbgout, "consistency verified\n");
21357 static void verify_consistency(struct compile_state *state) {}
21358 #endif /* DEBUG_CONSISTENCY */
21360 static void optimize(struct compile_state *state)
21362 /* Join all of the functions into one giant function */
21363 join_functions(state);
21365 /* Dump what the instruction graph intially looks like */
21366 print_triples(state);
21368 /* Replace structures with simpler data types */
21369 decompose_compound_types(state);
21370 print_triples(state);
21372 verify_consistency(state);
21373 /* Analyze the intermediate code */
21374 state->bb.first = state->first;
21375 analyze_basic_blocks(state, &state->bb);
21377 /* Transform the code to ssa form. */
21379 * The transformation to ssa form puts a phi function
21380 * on each of edge of a dominance frontier where that
21381 * phi function might be needed. At -O2 if we don't
21382 * eleminate the excess phi functions we can get an
21383 * exponential code size growth. So I kill the extra
21384 * phi functions early and I kill them often.
21386 transform_to_ssa_form(state);
21387 verify_consistency(state);
21389 /* Remove dead code */
21390 eliminate_inefectual_code(state);
21391 verify_consistency(state);
21393 /* Do strength reduction and simple constant optimizations */
21394 simplify_all(state);
21395 verify_consistency(state);
21396 /* Propogate constants throughout the code */
21397 scc_transform(state);
21398 verify_consistency(state);
21399 #warning "WISHLIST implement single use constants (least possible register pressure)"
21400 #warning "WISHLIST implement induction variable elimination"
21401 /* Select architecture instructions and an initial partial
21402 * coloring based on architecture constraints.
21404 transform_to_arch_instructions(state);
21405 verify_consistency(state);
21407 /* Remove dead code */
21408 eliminate_inefectual_code(state);
21409 verify_consistency(state);
21411 /* Color all of the variables to see if they will fit in registers */
21412 insert_copies_to_phi(state);
21413 verify_consistency(state);
21415 insert_mandatory_copies(state);
21416 verify_consistency(state);
21418 allocate_registers(state);
21419 verify_consistency(state);
21421 /* Remove the optimization information.
21422 * This is more to check for memory consistency than to free memory.
21424 free_basic_blocks(state, &state->bb);
21427 static void print_op_asm(struct compile_state *state,
21428 struct triple *ins, FILE *fp)
21430 struct asm_info *info;
21432 unsigned lhs, rhs, i;
21433 info = ins->u.ainfo;
21436 /* Don't count the clobbers in lhs */
21437 for(i = 0; i < lhs; i++) {
21438 if (LHS(ins, i)->type == &void_type) {
21443 fprintf(fp, "#ASM\n");
21445 for(ptr = info->str; *ptr; ptr++) {
21447 unsigned long param;
21448 struct triple *piece;
21458 param = strtoul(ptr, &next, 10);
21460 error(state, ins, "Invalid asm template");
21462 if (param >= (lhs + rhs)) {
21463 error(state, ins, "Invalid param %%%u in asm template",
21466 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
21468 arch_reg_str(ID_REG(piece->id)));
21471 fprintf(fp, "\n#NOT ASM\n");
21475 /* Only use the low x86 byte registers. This allows me
21476 * allocate the entire register when a byte register is used.
21478 #define X86_4_8BIT_GPRS 1
21481 #define X86_MMX_REGS (1<<0)
21482 #define X86_XMM_REGS (1<<1)
21483 #define X86_NOOP_COPY (1<<2)
21485 /* The x86 register classes */
21486 #define REGC_FLAGS 0
21487 #define REGC_GPR8 1
21488 #define REGC_GPR16 2
21489 #define REGC_GPR32 3
21490 #define REGC_DIVIDEND64 4
21491 #define REGC_DIVIDEND32 5
21494 #define REGC_GPR32_8 8
21495 #define REGC_GPR16_8 9
21496 #define REGC_GPR8_LO 10
21497 #define REGC_IMM32 11
21498 #define REGC_IMM16 12
21499 #define REGC_IMM8 13
21500 #define LAST_REGC REGC_IMM8
21501 #if LAST_REGC >= MAX_REGC
21502 #error "MAX_REGC is to low"
21505 /* Register class masks */
21506 #define REGCM_FLAGS (1 << REGC_FLAGS)
21507 #define REGCM_GPR8 (1 << REGC_GPR8)
21508 #define REGCM_GPR16 (1 << REGC_GPR16)
21509 #define REGCM_GPR32 (1 << REGC_GPR32)
21510 #define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
21511 #define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
21512 #define REGCM_MMX (1 << REGC_MMX)
21513 #define REGCM_XMM (1 << REGC_XMM)
21514 #define REGCM_GPR32_8 (1 << REGC_GPR32_8)
21515 #define REGCM_GPR16_8 (1 << REGC_GPR16_8)
21516 #define REGCM_GPR8_LO (1 << REGC_GPR8_LO)
21517 #define REGCM_IMM32 (1 << REGC_IMM32)
21518 #define REGCM_IMM16 (1 << REGC_IMM16)
21519 #define REGCM_IMM8 (1 << REGC_IMM8)
21520 #define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
21521 #define REGCM_IMMALL (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)
21523 /* The x86 registers */
21524 #define REG_EFLAGS 2
21525 #define REGC_FLAGS_FIRST REG_EFLAGS
21526 #define REGC_FLAGS_LAST REG_EFLAGS
21535 #define REGC_GPR8_LO_FIRST REG_AL
21536 #define REGC_GPR8_LO_LAST REG_DL
21537 #define REGC_GPR8_FIRST REG_AL
21538 #define REGC_GPR8_LAST REG_DH
21547 #define REGC_GPR16_FIRST REG_AX
21548 #define REGC_GPR16_LAST REG_SP
21557 #define REGC_GPR32_FIRST REG_EAX
21558 #define REGC_GPR32_LAST REG_ESP
21559 #define REG_EDXEAX 27
21560 #define REGC_DIVIDEND64_FIRST REG_EDXEAX
21561 #define REGC_DIVIDEND64_LAST REG_EDXEAX
21562 #define REG_DXAX 28
21563 #define REGC_DIVIDEND32_FIRST REG_DXAX
21564 #define REGC_DIVIDEND32_LAST REG_DXAX
21565 #define REG_MMX0 29
21566 #define REG_MMX1 30
21567 #define REG_MMX2 31
21568 #define REG_MMX3 32
21569 #define REG_MMX4 33
21570 #define REG_MMX5 34
21571 #define REG_MMX6 35
21572 #define REG_MMX7 36
21573 #define REGC_MMX_FIRST REG_MMX0
21574 #define REGC_MMX_LAST REG_MMX7
21575 #define REG_XMM0 37
21576 #define REG_XMM1 38
21577 #define REG_XMM2 39
21578 #define REG_XMM3 40
21579 #define REG_XMM4 41
21580 #define REG_XMM5 42
21581 #define REG_XMM6 43
21582 #define REG_XMM7 44
21583 #define REGC_XMM_FIRST REG_XMM0
21584 #define REGC_XMM_LAST REG_XMM7
21585 #warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
21586 #define LAST_REG REG_XMM7
21588 #define REGC_GPR32_8_FIRST REG_EAX
21589 #define REGC_GPR32_8_LAST REG_EDX
21590 #define REGC_GPR16_8_FIRST REG_AX
21591 #define REGC_GPR16_8_LAST REG_DX
21593 #define REGC_IMM8_FIRST -1
21594 #define REGC_IMM8_LAST -1
21595 #define REGC_IMM16_FIRST -2
21596 #define REGC_IMM16_LAST -1
21597 #define REGC_IMM32_FIRST -4
21598 #define REGC_IMM32_LAST -1
21600 #if LAST_REG >= MAX_REGISTERS
21601 #error "MAX_REGISTERS to low"
21605 static unsigned regc_size[LAST_REGC +1] = {
21606 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
21607 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
21608 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
21609 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
21610 [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
21611 [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
21612 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
21613 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
21614 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
21615 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
21616 [REGC_GPR8_LO] = REGC_GPR8_LO_LAST - REGC_GPR8_LO_FIRST + 1,
21622 static const struct {
21624 } regcm_bound[LAST_REGC + 1] = {
21625 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
21626 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
21627 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
21628 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
21629 [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
21630 [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
21631 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
21632 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
21633 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
21634 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
21635 [REGC_GPR8_LO] = { REGC_GPR8_LO_FIRST, REGC_GPR8_LO_LAST },
21636 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
21637 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
21638 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
21641 #if ARCH_INPUT_REGS != 4
21642 #error ARCH_INPUT_REGS size mismatch
21644 static const struct reg_info arch_input_regs[ARCH_INPUT_REGS] = {
21645 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21646 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21647 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21648 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21651 #if ARCH_OUTPUT_REGS != 4
21652 #error ARCH_INPUT_REGS size mismatch
21654 static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS] = {
21655 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21656 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21657 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21658 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21661 static void init_arch_state(struct arch_state *arch)
21663 memset(arch, 0, sizeof(*arch));
21664 arch->features = 0;
21667 static const struct compiler_flag arch_flags[] = {
21668 { "mmx", X86_MMX_REGS },
21669 { "sse", X86_XMM_REGS },
21670 { "noop-copy", X86_NOOP_COPY },
21673 static const struct compiler_flag arch_cpus[] = {
21675 { "p2", X86_MMX_REGS },
21676 { "p3", X86_MMX_REGS | X86_XMM_REGS },
21677 { "p4", X86_MMX_REGS | X86_XMM_REGS },
21678 { "k7", X86_MMX_REGS },
21679 { "k8", X86_MMX_REGS | X86_XMM_REGS },
21680 { "c3", X86_MMX_REGS },
21681 { "c3-2", X86_MMX_REGS | X86_XMM_REGS }, /* Nehemiah */
21684 static int arch_encode_flag(struct arch_state *arch, const char *flag)
21691 if (strncmp(flag, "no-", 3) == 0) {
21695 if (act && strncmp(flag, "cpu=", 4) == 0) {
21697 result = set_flag(arch_cpus, &arch->features, 1, flag);
21700 result = set_flag(arch_flags, &arch->features, act, flag);
21705 static void arch_usage(FILE *fp)
21707 flag_usage(fp, arch_flags, "-m", "-mno-");
21708 flag_usage(fp, arch_cpus, "-mcpu=", 0);
21711 static unsigned arch_regc_size(struct compile_state *state, int class)
21713 if ((class < 0) || (class > LAST_REGC)) {
21716 return regc_size[class];
21719 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
21721 /* See if two register classes may have overlapping registers */
21722 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
21723 REGCM_GPR32_8 | REGCM_GPR32 |
21724 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
21726 /* Special case for the immediates */
21727 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21728 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
21729 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21730 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
21733 return (regcm1 & regcm2) ||
21734 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
21737 static void arch_reg_equivs(
21738 struct compile_state *state, unsigned *equiv, int reg)
21740 if ((reg < 0) || (reg > LAST_REG)) {
21741 internal_error(state, 0, "invalid register");
21746 #if X86_4_8BIT_GPRS
21750 *equiv++ = REG_EAX;
21751 *equiv++ = REG_DXAX;
21752 *equiv++ = REG_EDXEAX;
21755 #if X86_4_8BIT_GPRS
21759 *equiv++ = REG_EAX;
21760 *equiv++ = REG_DXAX;
21761 *equiv++ = REG_EDXEAX;
21764 #if X86_4_8BIT_GPRS
21768 *equiv++ = REG_EBX;
21772 #if X86_4_8BIT_GPRS
21776 *equiv++ = REG_EBX;
21779 #if X86_4_8BIT_GPRS
21783 *equiv++ = REG_ECX;
21787 #if X86_4_8BIT_GPRS
21791 *equiv++ = REG_ECX;
21794 #if X86_4_8BIT_GPRS
21798 *equiv++ = REG_EDX;
21799 *equiv++ = REG_DXAX;
21800 *equiv++ = REG_EDXEAX;
21803 #if X86_4_8BIT_GPRS
21807 *equiv++ = REG_EDX;
21808 *equiv++ = REG_DXAX;
21809 *equiv++ = REG_EDXEAX;
21814 *equiv++ = REG_EAX;
21815 *equiv++ = REG_DXAX;
21816 *equiv++ = REG_EDXEAX;
21821 *equiv++ = REG_EBX;
21826 *equiv++ = REG_ECX;
21831 *equiv++ = REG_EDX;
21832 *equiv++ = REG_DXAX;
21833 *equiv++ = REG_EDXEAX;
21836 *equiv++ = REG_ESI;
21839 *equiv++ = REG_EDI;
21842 *equiv++ = REG_EBP;
21845 *equiv++ = REG_ESP;
21851 *equiv++ = REG_DXAX;
21852 *equiv++ = REG_EDXEAX;
21868 *equiv++ = REG_DXAX;
21869 *equiv++ = REG_EDXEAX;
21890 *equiv++ = REG_EAX;
21891 *equiv++ = REG_EDX;
21892 *equiv++ = REG_EDXEAX;
21901 *equiv++ = REG_EAX;
21902 *equiv++ = REG_EDX;
21903 *equiv++ = REG_DXAX;
21906 *equiv++ = REG_UNSET;
21909 static unsigned arch_avail_mask(struct compile_state *state)
21911 unsigned avail_mask;
21912 /* REGCM_GPR8 is not available */
21913 avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
21914 REGCM_GPR32 | REGCM_GPR32_8 |
21915 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
21916 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
21917 if (state->arch->features & X86_MMX_REGS) {
21918 avail_mask |= REGCM_MMX;
21920 if (state->arch->features & X86_XMM_REGS) {
21921 avail_mask |= REGCM_XMM;
21926 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
21928 unsigned mask, result;
21932 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
21933 if ((result & mask) == 0) {
21936 if (class > LAST_REGC) {
21939 for(class2 = 0; class2 <= LAST_REGC; class2++) {
21940 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
21941 (regcm_bound[class2].last <= regcm_bound[class].last)) {
21942 result |= (1 << class2);
21946 result &= arch_avail_mask(state);
21950 static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
21952 /* Like arch_regcm_normalize except immediate register classes are excluded */
21953 regcm = arch_regcm_normalize(state, regcm);
21954 /* Remove the immediate register classes */
21955 regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
21960 static unsigned arch_reg_regcm(struct compile_state *state, int reg)
21965 for(class = 0; class <= LAST_REGC; class++) {
21966 if ((reg >= regcm_bound[class].first) &&
21967 (reg <= regcm_bound[class].last)) {
21968 mask |= (1 << class);
21972 internal_error(state, 0, "reg %d not in any class", reg);
21977 static struct reg_info arch_reg_constraint(
21978 struct compile_state *state, struct type *type, const char *constraint)
21980 static const struct {
21984 } constraints[] = {
21985 { 'r', REGCM_GPR32, REG_UNSET },
21986 { 'g', REGCM_GPR32, REG_UNSET },
21987 { 'p', REGCM_GPR32, REG_UNSET },
21988 { 'q', REGCM_GPR8_LO, REG_UNSET },
21989 { 'Q', REGCM_GPR32_8, REG_UNSET },
21990 { 'x', REGCM_XMM, REG_UNSET },
21991 { 'y', REGCM_MMX, REG_UNSET },
21992 { 'a', REGCM_GPR32, REG_EAX },
21993 { 'b', REGCM_GPR32, REG_EBX },
21994 { 'c', REGCM_GPR32, REG_ECX },
21995 { 'd', REGCM_GPR32, REG_EDX },
21996 { 'D', REGCM_GPR32, REG_EDI },
21997 { 'S', REGCM_GPR32, REG_ESI },
21998 { '\0', 0, REG_UNSET },
22000 unsigned int regcm;
22001 unsigned int mask, reg;
22002 struct reg_info result;
22004 regcm = arch_type_to_regcm(state, type);
22007 for(ptr = constraint; *ptr; ptr++) {
22012 for(i = 0; constraints[i].class != '\0'; i++) {
22013 if (constraints[i].class == *ptr) {
22017 if (constraints[i].class == '\0') {
22018 error(state, 0, "invalid register constraint ``%c''", *ptr);
22021 if ((constraints[i].mask & regcm) == 0) {
22022 error(state, 0, "invalid register class %c specified",
22025 mask |= constraints[i].mask;
22026 if (constraints[i].reg != REG_UNSET) {
22027 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
22028 error(state, 0, "Only one register may be specified");
22030 reg = constraints[i].reg;
22034 result.regcm = mask;
22038 static struct reg_info arch_reg_clobber(
22039 struct compile_state *state, const char *clobber)
22041 struct reg_info result;
22042 if (strcmp(clobber, "memory") == 0) {
22043 result.reg = REG_UNSET;
22046 else if (strcmp(clobber, "eax") == 0) {
22047 result.reg = REG_EAX;
22048 result.regcm = REGCM_GPR32;
22050 else if (strcmp(clobber, "ebx") == 0) {
22051 result.reg = REG_EBX;
22052 result.regcm = REGCM_GPR32;
22054 else if (strcmp(clobber, "ecx") == 0) {
22055 result.reg = REG_ECX;
22056 result.regcm = REGCM_GPR32;
22058 else if (strcmp(clobber, "edx") == 0) {
22059 result.reg = REG_EDX;
22060 result.regcm = REGCM_GPR32;
22062 else if (strcmp(clobber, "esi") == 0) {
22063 result.reg = REG_ESI;
22064 result.regcm = REGCM_GPR32;
22066 else if (strcmp(clobber, "edi") == 0) {
22067 result.reg = REG_EDI;
22068 result.regcm = REGCM_GPR32;
22070 else if (strcmp(clobber, "ebp") == 0) {
22071 result.reg = REG_EBP;
22072 result.regcm = REGCM_GPR32;
22074 else if (strcmp(clobber, "esp") == 0) {
22075 result.reg = REG_ESP;
22076 result.regcm = REGCM_GPR32;
22078 else if (strcmp(clobber, "cc") == 0) {
22079 result.reg = REG_EFLAGS;
22080 result.regcm = REGCM_FLAGS;
22082 else if ((strncmp(clobber, "xmm", 3) == 0) &&
22083 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22084 result.reg = REG_XMM0 + octdigval(clobber[3]);
22085 result.regcm = REGCM_XMM;
22087 else if ((strncmp(clobber, "mm", 2) == 0) &&
22088 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22089 result.reg = REG_MMX0 + octdigval(clobber[3]);
22090 result.regcm = REGCM_MMX;
22093 error(state, 0, "unknown register name `%s' in asm",
22095 result.reg = REG_UNSET;
22101 static int do_select_reg(struct compile_state *state,
22102 char *used, int reg, unsigned classes)
22108 mask = arch_reg_regcm(state, reg);
22109 return (classes & mask) ? reg : REG_UNSET;
22112 static int arch_select_free_register(
22113 struct compile_state *state, char *used, int classes)
22115 /* Live ranges with the most neighbors are colored first.
22117 * Generally it does not matter which colors are given
22118 * as the register allocator attempts to color live ranges
22119 * in an order where you are guaranteed not to run out of colors.
22121 * Occasionally the register allocator cannot find an order
22122 * of register selection that will find a free color. To
22123 * increase the odds the register allocator will work when
22124 * it guesses first give out registers from register classes
22125 * least likely to run out of registers.
22130 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
22131 reg = do_select_reg(state, used, i, classes);
22133 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
22134 reg = do_select_reg(state, used, i, classes);
22136 for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
22137 reg = do_select_reg(state, used, i, classes);
22139 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
22140 reg = do_select_reg(state, used, i, classes);
22142 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
22143 reg = do_select_reg(state, used, i, classes);
22145 for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
22146 reg = do_select_reg(state, used, i, classes);
22148 for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
22149 reg = do_select_reg(state, used, i, classes);
22151 for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
22152 reg = do_select_reg(state, used, i, classes);
22154 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
22155 reg = do_select_reg(state, used, i, classes);
22161 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
22163 #warning "FIXME force types smaller (if legal) before I get here"
22166 switch(type->type & TYPE_MASK) {
22173 mask = REGCM_GPR8 | REGCM_GPR8_LO |
22174 REGCM_GPR16 | REGCM_GPR16_8 |
22175 REGCM_GPR32 | REGCM_GPR32_8 |
22176 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22177 REGCM_MMX | REGCM_XMM |
22178 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
22182 mask = REGCM_GPR16 | REGCM_GPR16_8 |
22183 REGCM_GPR32 | REGCM_GPR32_8 |
22184 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22185 REGCM_MMX | REGCM_XMM |
22186 REGCM_IMM32 | REGCM_IMM16;
22194 mask = REGCM_GPR32 | REGCM_GPR32_8 |
22195 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22196 REGCM_MMX | REGCM_XMM |
22201 mask = arch_type_to_regcm(state, type->left);
22204 mask = arch_type_to_regcm(state, type->left) &
22205 arch_type_to_regcm(state, type->right);
22207 case TYPE_BITFIELD:
22208 mask = arch_type_to_regcm(state, type->left);
22211 fprintf(state->errout, "type: ");
22212 name_of(state->errout, type);
22213 fprintf(state->errout, "\n");
22214 internal_error(state, 0, "no register class for type");
22217 mask = arch_regcm_normalize(state, mask);
22221 static int is_imm32(struct triple *imm)
22223 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
22224 (imm->op == OP_ADDRCONST);
22227 static int is_imm16(struct triple *imm)
22229 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
22231 static int is_imm8(struct triple *imm)
22233 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
22236 static int get_imm32(struct triple *ins, struct triple **expr)
22238 struct triple *imm;
22240 while(imm->op == OP_COPY) {
22243 if (!is_imm32(imm)) {
22246 unuse_triple(*expr, ins);
22247 use_triple(imm, ins);
22252 static int get_imm8(struct triple *ins, struct triple **expr)
22254 struct triple *imm;
22256 while(imm->op == OP_COPY) {
22259 if (!is_imm8(imm)) {
22262 unuse_triple(*expr, ins);
22263 use_triple(imm, ins);
22268 #define TEMPLATE_NOP 0
22269 #define TEMPLATE_INTCONST8 1
22270 #define TEMPLATE_INTCONST32 2
22271 #define TEMPLATE_UNKNOWNVAL 3
22272 #define TEMPLATE_COPY8_REG 5
22273 #define TEMPLATE_COPY16_REG 6
22274 #define TEMPLATE_COPY32_REG 7
22275 #define TEMPLATE_COPY_IMM8 8
22276 #define TEMPLATE_COPY_IMM16 9
22277 #define TEMPLATE_COPY_IMM32 10
22278 #define TEMPLATE_PHI8 11
22279 #define TEMPLATE_PHI16 12
22280 #define TEMPLATE_PHI32 13
22281 #define TEMPLATE_STORE8 14
22282 #define TEMPLATE_STORE16 15
22283 #define TEMPLATE_STORE32 16
22284 #define TEMPLATE_LOAD8 17
22285 #define TEMPLATE_LOAD16 18
22286 #define TEMPLATE_LOAD32 19
22287 #define TEMPLATE_BINARY8_REG 20
22288 #define TEMPLATE_BINARY16_REG 21
22289 #define TEMPLATE_BINARY32_REG 22
22290 #define TEMPLATE_BINARY8_IMM 23
22291 #define TEMPLATE_BINARY16_IMM 24
22292 #define TEMPLATE_BINARY32_IMM 25
22293 #define TEMPLATE_SL8_CL 26
22294 #define TEMPLATE_SL16_CL 27
22295 #define TEMPLATE_SL32_CL 28
22296 #define TEMPLATE_SL8_IMM 29
22297 #define TEMPLATE_SL16_IMM 30
22298 #define TEMPLATE_SL32_IMM 31
22299 #define TEMPLATE_UNARY8 32
22300 #define TEMPLATE_UNARY16 33
22301 #define TEMPLATE_UNARY32 34
22302 #define TEMPLATE_CMP8_REG 35
22303 #define TEMPLATE_CMP16_REG 36
22304 #define TEMPLATE_CMP32_REG 37
22305 #define TEMPLATE_CMP8_IMM 38
22306 #define TEMPLATE_CMP16_IMM 39
22307 #define TEMPLATE_CMP32_IMM 40
22308 #define TEMPLATE_TEST8 41
22309 #define TEMPLATE_TEST16 42
22310 #define TEMPLATE_TEST32 43
22311 #define TEMPLATE_SET 44
22312 #define TEMPLATE_JMP 45
22313 #define TEMPLATE_RET 46
22314 #define TEMPLATE_INB_DX 47
22315 #define TEMPLATE_INB_IMM 48
22316 #define TEMPLATE_INW_DX 49
22317 #define TEMPLATE_INW_IMM 50
22318 #define TEMPLATE_INL_DX 51
22319 #define TEMPLATE_INL_IMM 52
22320 #define TEMPLATE_OUTB_DX 53
22321 #define TEMPLATE_OUTB_IMM 54
22322 #define TEMPLATE_OUTW_DX 55
22323 #define TEMPLATE_OUTW_IMM 56
22324 #define TEMPLATE_OUTL_DX 57
22325 #define TEMPLATE_OUTL_IMM 58
22326 #define TEMPLATE_BSF 59
22327 #define TEMPLATE_RDMSR 60
22328 #define TEMPLATE_WRMSR 61
22329 #define TEMPLATE_UMUL8 62
22330 #define TEMPLATE_UMUL16 63
22331 #define TEMPLATE_UMUL32 64
22332 #define TEMPLATE_DIV8 65
22333 #define TEMPLATE_DIV16 66
22334 #define TEMPLATE_DIV32 67
22335 #define LAST_TEMPLATE TEMPLATE_DIV32
22336 #if LAST_TEMPLATE >= MAX_TEMPLATES
22337 #error "MAX_TEMPLATES to low"
22340 #define COPY8_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO | REGCM_MMX | REGCM_XMM)
22341 #define COPY16_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)
22342 #define COPY32_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
22345 static struct ins_template templates[] = {
22348 [ 0] = { REG_UNNEEDED, REGCM_IMMALL },
22349 [ 1] = { REG_UNNEEDED, REGCM_IMMALL },
22350 [ 2] = { REG_UNNEEDED, REGCM_IMMALL },
22351 [ 3] = { REG_UNNEEDED, REGCM_IMMALL },
22352 [ 4] = { REG_UNNEEDED, REGCM_IMMALL },
22353 [ 5] = { REG_UNNEEDED, REGCM_IMMALL },
22354 [ 6] = { REG_UNNEEDED, REGCM_IMMALL },
22355 [ 7] = { REG_UNNEEDED, REGCM_IMMALL },
22356 [ 8] = { REG_UNNEEDED, REGCM_IMMALL },
22357 [ 9] = { REG_UNNEEDED, REGCM_IMMALL },
22358 [10] = { REG_UNNEEDED, REGCM_IMMALL },
22359 [11] = { REG_UNNEEDED, REGCM_IMMALL },
22360 [12] = { REG_UNNEEDED, REGCM_IMMALL },
22361 [13] = { REG_UNNEEDED, REGCM_IMMALL },
22362 [14] = { REG_UNNEEDED, REGCM_IMMALL },
22363 [15] = { REG_UNNEEDED, REGCM_IMMALL },
22364 [16] = { REG_UNNEEDED, REGCM_IMMALL },
22365 [17] = { REG_UNNEEDED, REGCM_IMMALL },
22366 [18] = { REG_UNNEEDED, REGCM_IMMALL },
22367 [19] = { REG_UNNEEDED, REGCM_IMMALL },
22368 [20] = { REG_UNNEEDED, REGCM_IMMALL },
22369 [21] = { REG_UNNEEDED, REGCM_IMMALL },
22370 [22] = { REG_UNNEEDED, REGCM_IMMALL },
22371 [23] = { REG_UNNEEDED, REGCM_IMMALL },
22372 [24] = { REG_UNNEEDED, REGCM_IMMALL },
22373 [25] = { REG_UNNEEDED, REGCM_IMMALL },
22374 [26] = { REG_UNNEEDED, REGCM_IMMALL },
22375 [27] = { REG_UNNEEDED, REGCM_IMMALL },
22376 [28] = { REG_UNNEEDED, REGCM_IMMALL },
22377 [29] = { REG_UNNEEDED, REGCM_IMMALL },
22378 [30] = { REG_UNNEEDED, REGCM_IMMALL },
22379 [31] = { REG_UNNEEDED, REGCM_IMMALL },
22380 [32] = { REG_UNNEEDED, REGCM_IMMALL },
22381 [33] = { REG_UNNEEDED, REGCM_IMMALL },
22382 [34] = { REG_UNNEEDED, REGCM_IMMALL },
22383 [35] = { REG_UNNEEDED, REGCM_IMMALL },
22384 [36] = { REG_UNNEEDED, REGCM_IMMALL },
22385 [37] = { REG_UNNEEDED, REGCM_IMMALL },
22386 [38] = { REG_UNNEEDED, REGCM_IMMALL },
22387 [39] = { REG_UNNEEDED, REGCM_IMMALL },
22388 [40] = { REG_UNNEEDED, REGCM_IMMALL },
22389 [41] = { REG_UNNEEDED, REGCM_IMMALL },
22390 [42] = { REG_UNNEEDED, REGCM_IMMALL },
22391 [43] = { REG_UNNEEDED, REGCM_IMMALL },
22392 [44] = { REG_UNNEEDED, REGCM_IMMALL },
22393 [45] = { REG_UNNEEDED, REGCM_IMMALL },
22394 [46] = { REG_UNNEEDED, REGCM_IMMALL },
22395 [47] = { REG_UNNEEDED, REGCM_IMMALL },
22396 [48] = { REG_UNNEEDED, REGCM_IMMALL },
22397 [49] = { REG_UNNEEDED, REGCM_IMMALL },
22398 [50] = { REG_UNNEEDED, REGCM_IMMALL },
22399 [51] = { REG_UNNEEDED, REGCM_IMMALL },
22400 [52] = { REG_UNNEEDED, REGCM_IMMALL },
22401 [53] = { REG_UNNEEDED, REGCM_IMMALL },
22402 [54] = { REG_UNNEEDED, REGCM_IMMALL },
22403 [55] = { REG_UNNEEDED, REGCM_IMMALL },
22404 [56] = { REG_UNNEEDED, REGCM_IMMALL },
22405 [57] = { REG_UNNEEDED, REGCM_IMMALL },
22406 [58] = { REG_UNNEEDED, REGCM_IMMALL },
22407 [59] = { REG_UNNEEDED, REGCM_IMMALL },
22408 [60] = { REG_UNNEEDED, REGCM_IMMALL },
22409 [61] = { REG_UNNEEDED, REGCM_IMMALL },
22410 [62] = { REG_UNNEEDED, REGCM_IMMALL },
22411 [63] = { REG_UNNEEDED, REGCM_IMMALL },
22414 [TEMPLATE_INTCONST8] = {
22415 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22417 [TEMPLATE_INTCONST32] = {
22418 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
22420 [TEMPLATE_UNKNOWNVAL] = {
22421 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22423 [TEMPLATE_COPY8_REG] = {
22424 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22425 .rhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22427 [TEMPLATE_COPY16_REG] = {
22428 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22429 .rhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22431 [TEMPLATE_COPY32_REG] = {
22432 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22433 .rhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22435 [TEMPLATE_COPY_IMM8] = {
22436 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22437 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22439 [TEMPLATE_COPY_IMM16] = {
22440 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22441 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
22443 [TEMPLATE_COPY_IMM32] = {
22444 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22445 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
22447 [TEMPLATE_PHI8] = {
22448 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
22449 .rhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
22451 [TEMPLATE_PHI16] = {
22452 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
22453 .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
22455 [TEMPLATE_PHI32] = {
22456 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
22457 .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
22459 [TEMPLATE_STORE8] = {
22461 [0] = { REG_UNSET, REGCM_GPR32 },
22462 [1] = { REG_UNSET, REGCM_GPR8_LO },
22465 [TEMPLATE_STORE16] = {
22467 [0] = { REG_UNSET, REGCM_GPR32 },
22468 [1] = { REG_UNSET, REGCM_GPR16 },
22471 [TEMPLATE_STORE32] = {
22473 [0] = { REG_UNSET, REGCM_GPR32 },
22474 [1] = { REG_UNSET, REGCM_GPR32 },
22477 [TEMPLATE_LOAD8] = {
22478 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22479 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22481 [TEMPLATE_LOAD16] = {
22482 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22483 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22485 [TEMPLATE_LOAD32] = {
22486 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22487 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22489 [TEMPLATE_BINARY8_REG] = {
22490 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22492 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22493 [1] = { REG_UNSET, REGCM_GPR8_LO },
22496 [TEMPLATE_BINARY16_REG] = {
22497 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22499 [0] = { REG_VIRT0, REGCM_GPR16 },
22500 [1] = { REG_UNSET, REGCM_GPR16 },
22503 [TEMPLATE_BINARY32_REG] = {
22504 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22506 [0] = { REG_VIRT0, REGCM_GPR32 },
22507 [1] = { REG_UNSET, REGCM_GPR32 },
22510 [TEMPLATE_BINARY8_IMM] = {
22511 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22513 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22514 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22517 [TEMPLATE_BINARY16_IMM] = {
22518 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22520 [0] = { REG_VIRT0, REGCM_GPR16 },
22521 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22524 [TEMPLATE_BINARY32_IMM] = {
22525 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22527 [0] = { REG_VIRT0, REGCM_GPR32 },
22528 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22531 [TEMPLATE_SL8_CL] = {
22532 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22534 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22535 [1] = { REG_CL, REGCM_GPR8_LO },
22538 [TEMPLATE_SL16_CL] = {
22539 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22541 [0] = { REG_VIRT0, REGCM_GPR16 },
22542 [1] = { REG_CL, REGCM_GPR8_LO },
22545 [TEMPLATE_SL32_CL] = {
22546 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22548 [0] = { REG_VIRT0, REGCM_GPR32 },
22549 [1] = { REG_CL, REGCM_GPR8_LO },
22552 [TEMPLATE_SL8_IMM] = {
22553 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22555 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22556 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22559 [TEMPLATE_SL16_IMM] = {
22560 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22562 [0] = { REG_VIRT0, REGCM_GPR16 },
22563 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22566 [TEMPLATE_SL32_IMM] = {
22567 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22569 [0] = { REG_VIRT0, REGCM_GPR32 },
22570 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22573 [TEMPLATE_UNARY8] = {
22574 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22575 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22577 [TEMPLATE_UNARY16] = {
22578 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22579 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22581 [TEMPLATE_UNARY32] = {
22582 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22583 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22585 [TEMPLATE_CMP8_REG] = {
22586 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22588 [0] = { REG_UNSET, REGCM_GPR8_LO },
22589 [1] = { REG_UNSET, REGCM_GPR8_LO },
22592 [TEMPLATE_CMP16_REG] = {
22593 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22595 [0] = { REG_UNSET, REGCM_GPR16 },
22596 [1] = { REG_UNSET, REGCM_GPR16 },
22599 [TEMPLATE_CMP32_REG] = {
22600 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22602 [0] = { REG_UNSET, REGCM_GPR32 },
22603 [1] = { REG_UNSET, REGCM_GPR32 },
22606 [TEMPLATE_CMP8_IMM] = {
22607 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22609 [0] = { REG_UNSET, REGCM_GPR8_LO },
22610 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22613 [TEMPLATE_CMP16_IMM] = {
22614 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22616 [0] = { REG_UNSET, REGCM_GPR16 },
22617 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22620 [TEMPLATE_CMP32_IMM] = {
22621 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22623 [0] = { REG_UNSET, REGCM_GPR32 },
22624 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22627 [TEMPLATE_TEST8] = {
22628 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22629 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22631 [TEMPLATE_TEST16] = {
22632 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22633 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22635 [TEMPLATE_TEST32] = {
22636 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22637 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22640 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22641 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22644 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22647 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22649 [TEMPLATE_INB_DX] = {
22650 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
22651 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22653 [TEMPLATE_INB_IMM] = {
22654 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
22655 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22657 [TEMPLATE_INW_DX] = {
22658 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22659 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22661 [TEMPLATE_INW_IMM] = {
22662 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22663 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22665 [TEMPLATE_INL_DX] = {
22666 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22667 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22669 [TEMPLATE_INL_IMM] = {
22670 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22671 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22673 [TEMPLATE_OUTB_DX] = {
22675 [0] = { REG_AL, REGCM_GPR8_LO },
22676 [1] = { REG_DX, REGCM_GPR16 },
22679 [TEMPLATE_OUTB_IMM] = {
22681 [0] = { REG_AL, REGCM_GPR8_LO },
22682 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22685 [TEMPLATE_OUTW_DX] = {
22687 [0] = { REG_AX, REGCM_GPR16 },
22688 [1] = { REG_DX, REGCM_GPR16 },
22691 [TEMPLATE_OUTW_IMM] = {
22693 [0] = { REG_AX, REGCM_GPR16 },
22694 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22697 [TEMPLATE_OUTL_DX] = {
22699 [0] = { REG_EAX, REGCM_GPR32 },
22700 [1] = { REG_DX, REGCM_GPR16 },
22703 [TEMPLATE_OUTL_IMM] = {
22705 [0] = { REG_EAX, REGCM_GPR32 },
22706 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22710 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22711 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22713 [TEMPLATE_RDMSR] = {
22715 [0] = { REG_EAX, REGCM_GPR32 },
22716 [1] = { REG_EDX, REGCM_GPR32 },
22718 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
22720 [TEMPLATE_WRMSR] = {
22722 [0] = { REG_ECX, REGCM_GPR32 },
22723 [1] = { REG_EAX, REGCM_GPR32 },
22724 [2] = { REG_EDX, REGCM_GPR32 },
22727 [TEMPLATE_UMUL8] = {
22728 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22730 [0] = { REG_AL, REGCM_GPR8_LO },
22731 [1] = { REG_UNSET, REGCM_GPR8_LO },
22734 [TEMPLATE_UMUL16] = {
22735 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
22737 [0] = { REG_AX, REGCM_GPR16 },
22738 [1] = { REG_UNSET, REGCM_GPR16 },
22741 [TEMPLATE_UMUL32] = {
22742 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
22744 [0] = { REG_EAX, REGCM_GPR32 },
22745 [1] = { REG_UNSET, REGCM_GPR32 },
22748 [TEMPLATE_DIV8] = {
22750 [0] = { REG_AL, REGCM_GPR8_LO },
22751 [1] = { REG_AH, REGCM_GPR8 },
22754 [0] = { REG_AX, REGCM_GPR16 },
22755 [1] = { REG_UNSET, REGCM_GPR8_LO },
22758 [TEMPLATE_DIV16] = {
22760 [0] = { REG_AX, REGCM_GPR16 },
22761 [1] = { REG_DX, REGCM_GPR16 },
22764 [0] = { REG_DXAX, REGCM_DIVIDEND32 },
22765 [1] = { REG_UNSET, REGCM_GPR16 },
22768 [TEMPLATE_DIV32] = {
22770 [0] = { REG_EAX, REGCM_GPR32 },
22771 [1] = { REG_EDX, REGCM_GPR32 },
22774 [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
22775 [1] = { REG_UNSET, REGCM_GPR32 },
22780 static void fixup_branch(struct compile_state *state,
22781 struct triple *branch, int jmp_op, int cmp_op, struct type *cmp_type,
22782 struct triple *left, struct triple *right)
22784 struct triple *test;
22786 internal_error(state, branch, "no branch test?");
22788 test = pre_triple(state, branch,
22789 cmp_op, cmp_type, left, right);
22790 test->template_id = TEMPLATE_TEST32;
22791 if (cmp_op == OP_CMP) {
22792 test->template_id = TEMPLATE_CMP32_REG;
22793 if (get_imm32(test, &RHS(test, 1))) {
22794 test->template_id = TEMPLATE_CMP32_IMM;
22797 use_triple(RHS(test, 0), test);
22798 use_triple(RHS(test, 1), test);
22799 unuse_triple(RHS(branch, 0), branch);
22800 RHS(branch, 0) = test;
22801 branch->op = jmp_op;
22802 branch->template_id = TEMPLATE_JMP;
22803 use_triple(RHS(branch, 0), branch);
22806 static void fixup_branches(struct compile_state *state,
22807 struct triple *cmp, struct triple *use, int jmp_op)
22809 struct triple_set *entry, *next;
22810 for(entry = use->use; entry; entry = next) {
22811 next = entry->next;
22812 if (entry->member->op == OP_COPY) {
22813 fixup_branches(state, cmp, entry->member, jmp_op);
22815 else if (entry->member->op == OP_CBRANCH) {
22816 struct triple *branch;
22817 struct triple *left, *right;
22819 left = RHS(cmp, 0);
22820 if (cmp->rhs > 1) {
22821 right = RHS(cmp, 1);
22823 branch = entry->member;
22824 fixup_branch(state, branch, jmp_op,
22825 cmp->op, cmp->type, left, right);
22830 static void bool_cmp(struct compile_state *state,
22831 struct triple *ins, int cmp_op, int jmp_op, int set_op)
22833 struct triple_set *entry, *next;
22834 struct triple *set, *convert;
22836 /* Put a barrier up before the cmp which preceeds the
22837 * copy instruction. If a set actually occurs this gives
22838 * us a chance to move variables in registers out of the way.
22841 /* Modify the comparison operator */
22843 ins->template_id = TEMPLATE_TEST32;
22844 if (cmp_op == OP_CMP) {
22845 ins->template_id = TEMPLATE_CMP32_REG;
22846 if (get_imm32(ins, &RHS(ins, 1))) {
22847 ins->template_id = TEMPLATE_CMP32_IMM;
22850 /* Generate the instruction sequence that will transform the
22851 * result of the comparison into a logical value.
22853 set = post_triple(state, ins, set_op, &uchar_type, ins, 0);
22854 use_triple(ins, set);
22855 set->template_id = TEMPLATE_SET;
22858 if (!equiv_types(ins->type, set->type)) {
22859 convert = post_triple(state, set, OP_CONVERT, ins->type, set, 0);
22860 use_triple(set, convert);
22861 convert->template_id = TEMPLATE_COPY32_REG;
22864 for(entry = ins->use; entry; entry = next) {
22865 next = entry->next;
22866 if (entry->member == set) {
22869 replace_rhs_use(state, ins, convert, entry->member);
22871 fixup_branches(state, ins, convert, jmp_op);
22874 struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
22876 struct ins_template *template;
22877 struct reg_info result;
22879 if (ins->op == OP_PIECE) {
22880 index = ins->u.cval;
22881 ins = MISC(ins, 0);
22884 if (triple_is_def(state, ins)) {
22887 if (index >= zlhs) {
22888 internal_error(state, ins, "index %d out of range for %s",
22889 index, tops(ins->op));
22893 template = &ins->u.ainfo->tmpl;
22896 if (ins->template_id > LAST_TEMPLATE) {
22897 internal_error(state, ins, "bad template number %d",
22900 template = &templates[ins->template_id];
22903 result = template->lhs[index];
22904 result.regcm = arch_regcm_normalize(state, result.regcm);
22905 if (result.reg != REG_UNNEEDED) {
22906 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
22908 if (result.regcm == 0) {
22909 internal_error(state, ins, "lhs %d regcm == 0", index);
22914 struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
22916 struct reg_info result;
22917 struct ins_template *template;
22918 if ((index > ins->rhs) ||
22919 (ins->op == OP_PIECE)) {
22920 internal_error(state, ins, "index %d out of range for %s\n",
22921 index, tops(ins->op));
22925 template = &ins->u.ainfo->tmpl;
22931 if (ins->template_id > LAST_TEMPLATE) {
22932 internal_error(state, ins, "bad template number %d",
22935 template = &templates[ins->template_id];
22938 result = template->rhs[index];
22939 result.regcm = arch_regcm_normalize(state, result.regcm);
22940 if (result.regcm == 0) {
22941 internal_error(state, ins, "rhs %d regcm == 0", index);
22946 static struct triple *mod_div(struct compile_state *state,
22947 struct triple *ins, int div_op, int index)
22949 struct triple *div, *piece0, *piece1;
22951 /* Generate the appropriate division instruction */
22952 div = post_triple(state, ins, div_op, ins->type, 0, 0);
22953 RHS(div, 0) = RHS(ins, 0);
22954 RHS(div, 1) = RHS(ins, 1);
22955 piece0 = LHS(div, 0);
22956 piece1 = LHS(div, 1);
22957 div->template_id = TEMPLATE_DIV32;
22958 use_triple(RHS(div, 0), div);
22959 use_triple(RHS(div, 1), div);
22960 use_triple(LHS(div, 0), div);
22961 use_triple(LHS(div, 1), div);
22963 /* Replate uses of ins with the appropriate piece of the div */
22964 propogate_use(state, ins, LHS(div, index));
22965 release_triple(state, ins);
22967 /* Return the address of the next instruction */
22968 return piece1->next;
22971 static int noop_adecl(struct triple *adecl)
22973 struct triple_set *use;
22974 /* It's a noop if it doesn't specify stoorage */
22975 if (adecl->lhs == 0) {
22978 /* Is the adecl used? If not it's a noop */
22979 for(use = adecl->use; use ; use = use->next) {
22980 if ((use->member->op != OP_PIECE) ||
22981 (MISC(use->member, 0) != adecl)) {
22988 static struct triple *x86_deposit(struct compile_state *state, struct triple *ins)
22990 struct triple *mask, *nmask, *shift;
22991 struct triple *val, *val_mask, *val_shift;
22992 struct triple *targ, *targ_mask;
22993 struct triple *new;
22994 ulong_t the_mask, the_nmask;
22996 targ = RHS(ins, 0);
22999 /* Get constant for the mask value */
23001 the_mask <<= ins->u.bitfield.size;
23003 the_mask <<= ins->u.bitfield.offset;
23004 mask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23005 mask->u.cval = the_mask;
23007 /* Get the inverted mask value */
23008 the_nmask = ~the_mask;
23009 nmask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23010 nmask->u.cval = the_nmask;
23012 /* Get constant for the shift value */
23013 shift = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23014 shift->u.cval = ins->u.bitfield.offset;
23016 /* Shift and mask the source value */
23018 if (shift->u.cval != 0) {
23019 val_shift = pre_triple(state, ins, OP_SL, val->type, val, shift);
23020 use_triple(val, val_shift);
23021 use_triple(shift, val_shift);
23023 val_mask = val_shift;
23024 if (is_signed(val->type)) {
23025 val_mask = pre_triple(state, ins, OP_AND, val->type, val_shift, mask);
23026 use_triple(val_shift, val_mask);
23027 use_triple(mask, val_mask);
23030 /* Mask the target value */
23031 targ_mask = pre_triple(state, ins, OP_AND, targ->type, targ, nmask);
23032 use_triple(targ, targ_mask);
23033 use_triple(nmask, targ_mask);
23035 /* Now combined them together */
23036 new = pre_triple(state, ins, OP_OR, targ->type, targ_mask, val_mask);
23037 use_triple(targ_mask, new);
23038 use_triple(val_mask, new);
23040 /* Move all of the users over to the new expression */
23041 propogate_use(state, ins, new);
23043 /* Delete the original triple */
23044 release_triple(state, ins);
23046 /* Restart the transformation at mask */
23050 static struct triple *x86_extract(struct compile_state *state, struct triple *ins)
23052 struct triple *mask, *shift;
23053 struct triple *val, *val_mask, *val_shift;
23058 /* Get constant for the mask value */
23060 the_mask <<= ins->u.bitfield.size;
23062 mask = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23063 mask->u.cval = the_mask;
23065 /* Get constant for the right shift value */
23066 shift = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23067 shift->u.cval = ins->u.bitfield.offset;
23069 /* Shift arithmetic right, to correct the sign */
23071 if (shift->u.cval != 0) {
23073 if (ins->op == OP_SEXTRACT) {
23078 val_shift = pre_triple(state, ins, op, val->type, val, shift);
23079 use_triple(val, val_shift);
23080 use_triple(shift, val_shift);
23083 /* Finally mask the value */
23084 val_mask = pre_triple(state, ins, OP_AND, ins->type, val_shift, mask);
23085 use_triple(val_shift, val_mask);
23086 use_triple(mask, val_mask);
23088 /* Move all of the users over to the new expression */
23089 propogate_use(state, ins, val_mask);
23091 /* Release the original instruction */
23092 release_triple(state, ins);
23098 static struct triple *transform_to_arch_instruction(
23099 struct compile_state *state, struct triple *ins)
23101 /* Transform from generic 3 address instructions
23102 * to archtecture specific instructions.
23103 * And apply architecture specific constraints to instructions.
23104 * Copies are inserted to preserve the register flexibility
23105 * of 3 address instructions.
23107 struct triple *next, *value;
23112 ins->template_id = TEMPLATE_INTCONST32;
23113 if (ins->u.cval < 256) {
23114 ins->template_id = TEMPLATE_INTCONST8;
23118 ins->template_id = TEMPLATE_INTCONST32;
23120 case OP_UNKNOWNVAL:
23121 ins->template_id = TEMPLATE_UNKNOWNVAL;
23127 ins->template_id = TEMPLATE_NOP;
23131 size = size_of(state, ins->type);
23132 value = RHS(ins, 0);
23133 if (is_imm8(value) && (size <= SIZEOF_I8)) {
23134 ins->template_id = TEMPLATE_COPY_IMM8;
23136 else if (is_imm16(value) && (size <= SIZEOF_I16)) {
23137 ins->template_id = TEMPLATE_COPY_IMM16;
23139 else if (is_imm32(value) && (size <= SIZEOF_I32)) {
23140 ins->template_id = TEMPLATE_COPY_IMM32;
23142 else if (is_const(value)) {
23143 internal_error(state, ins, "bad constant passed to copy");
23145 else if (size <= SIZEOF_I8) {
23146 ins->template_id = TEMPLATE_COPY8_REG;
23148 else if (size <= SIZEOF_I16) {
23149 ins->template_id = TEMPLATE_COPY16_REG;
23151 else if (size <= SIZEOF_I32) {
23152 ins->template_id = TEMPLATE_COPY32_REG;
23155 internal_error(state, ins, "bad type passed to copy");
23159 size = size_of(state, ins->type);
23160 if (size <= SIZEOF_I8) {
23161 ins->template_id = TEMPLATE_PHI8;
23163 else if (size <= SIZEOF_I16) {
23164 ins->template_id = TEMPLATE_PHI16;
23166 else if (size <= SIZEOF_I32) {
23167 ins->template_id = TEMPLATE_PHI32;
23170 internal_error(state, ins, "bad type passed to phi");
23174 /* Adecls should always be treated as dead code and
23175 * removed. If we are not optimizing they may linger.
23177 if (!noop_adecl(ins)) {
23178 internal_error(state, ins, "adecl remains?");
23180 ins->template_id = TEMPLATE_NOP;
23181 next = after_lhs(state, ins);
23184 switch(ins->type->type & TYPE_MASK) {
23185 case TYPE_CHAR: case TYPE_UCHAR:
23186 ins->template_id = TEMPLATE_STORE8;
23188 case TYPE_SHORT: case TYPE_USHORT:
23189 ins->template_id = TEMPLATE_STORE16;
23191 case TYPE_INT: case TYPE_UINT:
23192 case TYPE_LONG: case TYPE_ULONG:
23194 ins->template_id = TEMPLATE_STORE32;
23197 internal_error(state, ins, "unknown type in store");
23202 switch(ins->type->type & TYPE_MASK) {
23203 case TYPE_CHAR: case TYPE_UCHAR:
23204 case TYPE_SHORT: case TYPE_USHORT:
23205 case TYPE_INT: case TYPE_UINT:
23206 case TYPE_LONG: case TYPE_ULONG:
23210 internal_error(state, ins, "unknown type in load");
23213 ins->template_id = TEMPLATE_LOAD32;
23221 ins->template_id = TEMPLATE_BINARY32_REG;
23222 if (get_imm32(ins, &RHS(ins, 1))) {
23223 ins->template_id = TEMPLATE_BINARY32_IMM;
23228 ins->template_id = TEMPLATE_DIV32;
23229 next = after_lhs(state, ins);
23232 ins->template_id = TEMPLATE_UMUL32;
23235 next = mod_div(state, ins, OP_UDIVT, 0);
23238 next = mod_div(state, ins, OP_SDIVT, 0);
23241 next = mod_div(state, ins, OP_UDIVT, 1);
23244 next = mod_div(state, ins, OP_SDIVT, 1);
23249 ins->template_id = TEMPLATE_SL32_CL;
23250 if (get_imm8(ins, &RHS(ins, 1))) {
23251 ins->template_id = TEMPLATE_SL32_IMM;
23252 } else if (size_of(state, RHS(ins, 1)->type) > SIZEOF_CHAR) {
23253 typed_pre_copy(state, &uchar_type, ins, 1);
23258 ins->template_id = TEMPLATE_UNARY32;
23261 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
23264 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23267 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
23270 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
23273 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
23276 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
23279 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
23282 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
23285 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
23288 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
23291 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23294 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
23298 ins->template_id = TEMPLATE_NOP;
23301 fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST,
23302 RHS(ins, 0)->type, RHS(ins, 0), 0);
23305 ins->template_id = TEMPLATE_NOP;
23308 ins->template_id = TEMPLATE_RET;
23314 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
23315 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
23316 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
23318 if (get_imm8(ins, &RHS(ins, 0))) {
23319 ins->template_id += 1;
23326 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
23327 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
23328 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
23330 if (get_imm8(ins, &RHS(ins, 1))) {
23331 ins->template_id += 1;
23336 ins->template_id = TEMPLATE_BSF;
23339 ins->template_id = TEMPLATE_RDMSR;
23340 next = after_lhs(state, ins);
23343 ins->template_id = TEMPLATE_WRMSR;
23346 ins->template_id = TEMPLATE_NOP;
23349 ins->template_id = TEMPLATE_NOP;
23350 next = after_lhs(state, ins);
23352 /* Already transformed instructions */
23354 ins->template_id = TEMPLATE_TEST32;
23357 ins->template_id = TEMPLATE_CMP32_REG;
23358 if (get_imm32(ins, &RHS(ins, 1))) {
23359 ins->template_id = TEMPLATE_CMP32_IMM;
23363 ins->template_id = TEMPLATE_NOP;
23365 case OP_JMP_EQ: case OP_JMP_NOTEQ:
23366 case OP_JMP_SLESS: case OP_JMP_ULESS:
23367 case OP_JMP_SMORE: case OP_JMP_UMORE:
23368 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
23369 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
23370 ins->template_id = TEMPLATE_JMP;
23372 case OP_SET_EQ: case OP_SET_NOTEQ:
23373 case OP_SET_SLESS: case OP_SET_ULESS:
23374 case OP_SET_SMORE: case OP_SET_UMORE:
23375 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
23376 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
23377 ins->template_id = TEMPLATE_SET;
23380 next = x86_deposit(state, ins);
23384 next = x86_extract(state, ins);
23386 /* Unhandled instructions */
23389 internal_error(state, ins, "unhandled ins: %d %s",
23390 ins->op, tops(ins->op));
23396 static long next_label(struct compile_state *state)
23398 static long label_counter = 1000;
23399 return ++label_counter;
23401 static void generate_local_labels(struct compile_state *state)
23403 struct triple *first, *label;
23404 first = state->first;
23407 if ((label->op == OP_LABEL) ||
23408 (label->op == OP_SDECL)) {
23410 label->u.cval = next_label(state);
23416 label = label->next;
23417 } while(label != first);
23420 static int check_reg(struct compile_state *state,
23421 struct triple *triple, int classes)
23425 reg = ID_REG(triple->id);
23426 if (reg == REG_UNSET) {
23427 internal_error(state, triple, "register not set");
23429 mask = arch_reg_regcm(state, reg);
23430 if (!(classes & mask)) {
23431 internal_error(state, triple, "reg %d in wrong class",
23439 #error "Registers have renumberd fix arch_reg_str"
23441 static const char *arch_regs[] = {
23445 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
23446 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
23447 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
23450 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
23451 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
23452 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
23454 static const char *arch_reg_str(int reg)
23456 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
23459 return arch_regs[reg];
23462 static const char *reg(struct compile_state *state, struct triple *triple,
23466 reg = check_reg(state, triple, classes);
23467 return arch_reg_str(reg);
23470 static int arch_reg_size(int reg)
23474 if (reg == REG_EFLAGS) {
23477 else if ((reg >= REG_AL) && (reg <= REG_DH)) {
23480 else if ((reg >= REG_AX) && (reg <= REG_SP)) {
23483 else if ((reg >= REG_EAX) && (reg <= REG_ESP)) {
23486 else if (reg == REG_EDXEAX) {
23489 else if (reg == REG_DXAX) {
23492 else if ((reg >= REG_MMX0) && (reg <= REG_MMX7)) {
23495 else if ((reg >= REG_XMM0) && (reg <= REG_XMM7)) {
23501 static int reg_size(struct compile_state *state, struct triple *ins)
23504 reg = ID_REG(ins->id);
23505 if (reg == REG_UNSET) {
23506 internal_error(state, ins, "register not set");
23508 return arch_reg_size(reg);
23513 const char *type_suffix(struct compile_state *state, struct type *type)
23515 const char *suffix;
23516 switch(size_of(state, type)) {
23517 case SIZEOF_I8: suffix = "b"; break;
23518 case SIZEOF_I16: suffix = "w"; break;
23519 case SIZEOF_I32: suffix = "l"; break;
23521 internal_error(state, 0, "unknown suffix");
23528 static void print_const_val(
23529 struct compile_state *state, struct triple *ins, FILE *fp)
23533 fprintf(fp, " $%ld ",
23534 (long)(ins->u.cval));
23537 if ((MISC(ins, 0)->op != OP_SDECL) &&
23538 (MISC(ins, 0)->op != OP_LABEL))
23540 internal_error(state, ins, "bad base for addrconst");
23542 if (MISC(ins, 0)->u.cval <= 0) {
23543 internal_error(state, ins, "unlabeled constant");
23545 fprintf(fp, " $L%s%lu+%lu ",
23546 state->compiler->label_prefix,
23547 (unsigned long)(MISC(ins, 0)->u.cval),
23548 (unsigned long)(ins->u.cval));
23551 internal_error(state, ins, "unknown constant type");
23556 static void print_const(struct compile_state *state,
23557 struct triple *ins, FILE *fp)
23561 switch(ins->type->type & TYPE_MASK) {
23564 fprintf(fp, ".byte 0x%02lx\n",
23565 (unsigned long)(ins->u.cval));
23569 fprintf(fp, ".short 0x%04lx\n",
23570 (unsigned long)(ins->u.cval));
23577 fprintf(fp, ".int %lu\n",
23578 (unsigned long)(ins->u.cval));
23581 fprintf(state->errout, "type: ");
23582 name_of(state->errout, ins->type);
23583 fprintf(state->errout, "\n");
23584 internal_error(state, ins, "Unknown constant type. Val: %lu",
23585 (unsigned long)(ins->u.cval));
23590 if ((MISC(ins, 0)->op != OP_SDECL) &&
23591 (MISC(ins, 0)->op != OP_LABEL)) {
23592 internal_error(state, ins, "bad base for addrconst");
23594 if (MISC(ins, 0)->u.cval <= 0) {
23595 internal_error(state, ins, "unlabeled constant");
23597 fprintf(fp, ".int L%s%lu+%lu\n",
23598 state->compiler->label_prefix,
23599 (unsigned long)(MISC(ins, 0)->u.cval),
23600 (unsigned long)(ins->u.cval));
23604 unsigned char *blob;
23606 size = size_of_in_bytes(state, ins->type);
23607 blob = ins->u.blob;
23608 for(i = 0; i < size; i++) {
23609 fprintf(fp, ".byte 0x%02x\n",
23615 internal_error(state, ins, "Unknown constant type");
23620 #define TEXT_SECTION ".rom.text"
23621 #define DATA_SECTION ".rom.data"
23623 static long get_const_pool_ref(
23624 struct compile_state *state, struct triple *ins, size_t size, FILE *fp)
23628 ref = next_label(state);
23629 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
23630 fprintf(fp, ".balign %d\n", align_of_in_bytes(state, ins->type));
23631 fprintf(fp, "L%s%lu:\n", state->compiler->label_prefix, ref);
23632 print_const(state, ins, fp);
23633 fill_bytes = bits_to_bytes(size - size_of(state, ins->type));
23635 fprintf(fp, ".fill %d, 1, 0\n", fill_bytes);
23637 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
23641 static long get_mask_pool_ref(
23642 struct compile_state *state, struct triple *ins, unsigned long mask, FILE *fp)
23645 if (mask == 0xff) {
23648 else if (mask == 0xffff) {
23653 internal_error(state, ins, "unhandled mask value");
23658 static void print_binary_op(struct compile_state *state,
23659 const char *op, struct triple *ins, FILE *fp)
23662 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
23663 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
23664 internal_error(state, ins, "invalid register assignment");
23666 if (is_const(RHS(ins, 1))) {
23667 fprintf(fp, "\t%s ", op);
23668 print_const_val(state, RHS(ins, 1), fp);
23669 fprintf(fp, ", %s\n",
23670 reg(state, RHS(ins, 0), mask));
23673 unsigned lmask, rmask;
23675 lreg = check_reg(state, RHS(ins, 0), mask);
23676 rreg = check_reg(state, RHS(ins, 1), mask);
23677 lmask = arch_reg_regcm(state, lreg);
23678 rmask = arch_reg_regcm(state, rreg);
23679 mask = lmask & rmask;
23680 fprintf(fp, "\t%s %s, %s\n",
23682 reg(state, RHS(ins, 1), mask),
23683 reg(state, RHS(ins, 0), mask));
23686 static void print_unary_op(struct compile_state *state,
23687 const char *op, struct triple *ins, FILE *fp)
23690 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
23691 fprintf(fp, "\t%s %s\n",
23693 reg(state, RHS(ins, 0), mask));
23696 static void print_op_shift(struct compile_state *state,
23697 const char *op, struct triple *ins, FILE *fp)
23700 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
23701 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
23702 internal_error(state, ins, "invalid register assignment");
23704 if (is_const(RHS(ins, 1))) {
23705 fprintf(fp, "\t%s ", op);
23706 print_const_val(state, RHS(ins, 1), fp);
23707 fprintf(fp, ", %s\n",
23708 reg(state, RHS(ins, 0), mask));
23711 fprintf(fp, "\t%s %s, %s\n",
23713 reg(state, RHS(ins, 1), REGCM_GPR8_LO),
23714 reg(state, RHS(ins, 0), mask));
23718 static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
23725 case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
23726 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
23727 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
23729 internal_error(state, ins, "not an in operation");
23733 dreg = check_reg(state, ins, mask);
23734 if (!reg_is_reg(state, dreg, REG_EAX)) {
23735 internal_error(state, ins, "dst != %%eax");
23737 if (is_const(RHS(ins, 0))) {
23738 fprintf(fp, "\t%s ", op);
23739 print_const_val(state, RHS(ins, 0), fp);
23740 fprintf(fp, ", %s\n",
23741 reg(state, ins, mask));
23745 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
23746 if (!reg_is_reg(state, addr_reg, REG_DX)) {
23747 internal_error(state, ins, "src != %%dx");
23749 fprintf(fp, "\t%s %s, %s\n",
23751 reg(state, RHS(ins, 0), REGCM_GPR16),
23752 reg(state, ins, mask));
23756 static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
23763 case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
23764 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
23765 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
23767 internal_error(state, ins, "not an out operation");
23771 lreg = check_reg(state, RHS(ins, 0), mask);
23772 if (!reg_is_reg(state, lreg, REG_EAX)) {
23773 internal_error(state, ins, "src != %%eax");
23775 if (is_const(RHS(ins, 1))) {
23776 fprintf(fp, "\t%s %s,",
23777 op, reg(state, RHS(ins, 0), mask));
23778 print_const_val(state, RHS(ins, 1), fp);
23783 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
23784 if (!reg_is_reg(state, addr_reg, REG_DX)) {
23785 internal_error(state, ins, "dst != %%dx");
23787 fprintf(fp, "\t%s %s, %s\n",
23789 reg(state, RHS(ins, 0), mask),
23790 reg(state, RHS(ins, 1), REGCM_GPR16));
23794 static void print_op_move(struct compile_state *state,
23795 struct triple *ins, FILE *fp)
23797 /* op_move is complex because there are many types
23798 * of registers we can move between.
23799 * Because OP_COPY will be introduced in arbitrary locations
23800 * OP_COPY must not affect flags.
23801 * OP_CONVERT can change the flags and it is the only operation
23802 * where it is expected the types in the registers can change.
23804 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
23805 struct triple *dst, *src;
23806 if (state->arch->features & X86_NOOP_COPY) {
23809 if ((ins->op == OP_COPY) || (ins->op == OP_CONVERT)) {
23814 internal_error(state, ins, "unknown move operation");
23817 if (reg_size(state, dst) < size_of(state, dst->type)) {
23818 internal_error(state, ins, "Invalid destination register");
23820 if (!equiv_types(src->type, dst->type) && (dst->op == OP_COPY)) {
23821 fprintf(state->errout, "src type: ");
23822 name_of(state->errout, src->type);
23823 fprintf(state->errout, "\n");
23824 fprintf(state->errout, "dst type: ");
23825 name_of(state->errout, dst->type);
23826 fprintf(state->errout, "\n");
23827 internal_error(state, ins, "Type mismatch for OP_COPY");
23830 if (!is_const(src)) {
23831 int src_reg, dst_reg;
23832 int src_regcm, dst_regcm;
23833 src_reg = ID_REG(src->id);
23834 dst_reg = ID_REG(dst->id);
23835 src_regcm = arch_reg_regcm(state, src_reg);
23836 dst_regcm = arch_reg_regcm(state, dst_reg);
23837 /* If the class is the same just move the register */
23838 if (src_regcm & dst_regcm &
23839 (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
23840 if ((src_reg != dst_reg) || !omit_copy) {
23841 fprintf(fp, "\tmov %s, %s\n",
23842 reg(state, src, src_regcm),
23843 reg(state, dst, dst_regcm));
23846 /* Move 32bit to 16bit */
23847 else if ((src_regcm & REGCM_GPR32) &&
23848 (dst_regcm & REGCM_GPR16)) {
23849 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
23850 if ((src_reg != dst_reg) || !omit_copy) {
23851 fprintf(fp, "\tmovw %s, %s\n",
23852 arch_reg_str(src_reg),
23853 arch_reg_str(dst_reg));
23856 /* Move from 32bit gprs to 16bit gprs */
23857 else if ((src_regcm & REGCM_GPR32) &&
23858 (dst_regcm & REGCM_GPR16)) {
23859 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
23860 if ((src_reg != dst_reg) || !omit_copy) {
23861 fprintf(fp, "\tmov %s, %s\n",
23862 arch_reg_str(src_reg),
23863 arch_reg_str(dst_reg));
23866 /* Move 32bit to 8bit */
23867 else if ((src_regcm & REGCM_GPR32_8) &&
23868 (dst_regcm & REGCM_GPR8_LO))
23870 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
23871 if ((src_reg != dst_reg) || !omit_copy) {
23872 fprintf(fp, "\tmovb %s, %s\n",
23873 arch_reg_str(src_reg),
23874 arch_reg_str(dst_reg));
23877 /* Move 16bit to 8bit */
23878 else if ((src_regcm & REGCM_GPR16_8) &&
23879 (dst_regcm & REGCM_GPR8_LO))
23881 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
23882 if ((src_reg != dst_reg) || !omit_copy) {
23883 fprintf(fp, "\tmovb %s, %s\n",
23884 arch_reg_str(src_reg),
23885 arch_reg_str(dst_reg));
23888 /* Move 8/16bit to 16/32bit */
23889 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) &&
23890 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
23892 op = is_signed(src->type)? "movsx": "movzx";
23893 fprintf(fp, "\t%s %s, %s\n",
23895 reg(state, src, src_regcm),
23896 reg(state, dst, dst_regcm));
23898 /* Move between sse registers */
23899 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
23900 if ((src_reg != dst_reg) || !omit_copy) {
23901 fprintf(fp, "\tmovdqa %s, %s\n",
23902 reg(state, src, src_regcm),
23903 reg(state, dst, dst_regcm));
23906 /* Move between mmx registers */
23907 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
23908 if ((src_reg != dst_reg) || !omit_copy) {
23909 fprintf(fp, "\tmovq %s, %s\n",
23910 reg(state, src, src_regcm),
23911 reg(state, dst, dst_regcm));
23914 /* Move from sse to mmx registers */
23915 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
23916 fprintf(fp, "\tmovdq2q %s, %s\n",
23917 reg(state, src, src_regcm),
23918 reg(state, dst, dst_regcm));
23920 /* Move from mmx to sse registers */
23921 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
23922 fprintf(fp, "\tmovq2dq %s, %s\n",
23923 reg(state, src, src_regcm),
23924 reg(state, dst, dst_regcm));
23926 /* Move between 32bit gprs & mmx/sse registers */
23927 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
23928 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
23929 fprintf(fp, "\tmovd %s, %s\n",
23930 reg(state, src, src_regcm),
23931 reg(state, dst, dst_regcm));
23933 /* Move from 16bit gprs & mmx/sse registers */
23934 else if ((src_regcm & REGCM_GPR16) &&
23935 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
23938 op = is_signed(src->type)? "movsx":"movzx";
23939 mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
23940 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
23942 arch_reg_str(src_reg),
23943 arch_reg_str(mid_reg),
23944 arch_reg_str(mid_reg),
23945 arch_reg_str(dst_reg));
23947 /* Move from mmx/sse registers to 16bit gprs */
23948 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
23949 (dst_regcm & REGCM_GPR16)) {
23950 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
23951 fprintf(fp, "\tmovd %s, %s\n",
23952 arch_reg_str(src_reg),
23953 arch_reg_str(dst_reg));
23955 /* Move from gpr to 64bit dividend */
23956 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) &&
23957 (dst_regcm & REGCM_DIVIDEND64)) {
23958 const char *extend;
23959 extend = is_signed(src->type)? "cltd":"movl $0, %edx";
23960 fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
23961 arch_reg_str(src_reg),
23964 /* Move from 64bit gpr to gpr */
23965 else if ((src_regcm & REGCM_DIVIDEND64) &&
23966 (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
23967 if (dst_regcm & REGCM_GPR32) {
23970 else if (dst_regcm & REGCM_GPR16) {
23973 else if (dst_regcm & REGCM_GPR8_LO) {
23976 fprintf(fp, "\tmov %s, %s\n",
23977 arch_reg_str(src_reg),
23978 arch_reg_str(dst_reg));
23980 /* Move from mmx/sse registers to 64bit gpr */
23981 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
23982 (dst_regcm & REGCM_DIVIDEND64)) {
23983 const char *extend;
23984 extend = is_signed(src->type)? "cltd": "movl $0, %edx";
23985 fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
23986 arch_reg_str(src_reg),
23989 /* Move from 64bit gpr to mmx/sse register */
23990 else if ((src_regcm & REGCM_DIVIDEND64) &&
23991 (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
23992 fprintf(fp, "\tmovd %%eax, %s\n",
23993 arch_reg_str(dst_reg));
23995 #if X86_4_8BIT_GPRS
23996 /* Move from 8bit gprs to mmx/sse registers */
23997 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
23998 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24001 op = is_signed(src->type)? "movsx":"movzx";
24002 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24003 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24005 reg(state, src, src_regcm),
24006 arch_reg_str(mid_reg),
24007 arch_reg_str(mid_reg),
24008 reg(state, dst, dst_regcm));
24010 /* Move from mmx/sse registers and 8bit gprs */
24011 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24012 (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
24014 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24015 fprintf(fp, "\tmovd %s, %s\n",
24016 reg(state, src, src_regcm),
24017 arch_reg_str(mid_reg));
24019 /* Move from 32bit gprs to 8bit gprs */
24020 else if ((src_regcm & REGCM_GPR32) &&
24021 (dst_regcm & REGCM_GPR8_LO)) {
24022 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24023 if ((src_reg != dst_reg) || !omit_copy) {
24024 fprintf(fp, "\tmov %s, %s\n",
24025 arch_reg_str(src_reg),
24026 arch_reg_str(dst_reg));
24029 /* Move from 16bit gprs to 8bit gprs */
24030 else if ((src_regcm & REGCM_GPR16) &&
24031 (dst_regcm & REGCM_GPR8_LO)) {
24032 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
24033 if ((src_reg != dst_reg) || !omit_copy) {
24034 fprintf(fp, "\tmov %s, %s\n",
24035 arch_reg_str(src_reg),
24036 arch_reg_str(dst_reg));
24039 #endif /* X86_4_8BIT_GPRS */
24040 /* Move from %eax:%edx to %eax:%edx */
24041 else if ((src_regcm & REGCM_DIVIDEND64) &&
24042 (dst_regcm & REGCM_DIVIDEND64) &&
24043 (src_reg == dst_reg)) {
24045 fprintf(fp, "\t/*mov %s, %s*/\n",
24046 arch_reg_str(src_reg),
24047 arch_reg_str(dst_reg));
24051 if ((src_regcm & ~REGCM_FLAGS) == 0) {
24052 internal_error(state, ins, "attempt to copy from %%eflags!");
24054 internal_error(state, ins, "unknown copy type");
24061 dst_size = size_of(state, dst->type);
24062 dst_reg = ID_REG(dst->id);
24063 dst_regcm = arch_reg_regcm(state, dst_reg);
24064 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24065 fprintf(fp, "\tmov ");
24066 print_const_val(state, src, fp);
24067 fprintf(fp, ", %s\n",
24068 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24070 else if (dst_regcm & REGCM_DIVIDEND64) {
24071 if (dst_size > SIZEOF_I32) {
24072 internal_error(state, ins, "%dbit constant...", dst_size);
24074 fprintf(fp, "\tmov $0, %%edx\n");
24075 fprintf(fp, "\tmov ");
24076 print_const_val(state, src, fp);
24077 fprintf(fp, ", %%eax\n");
24079 else if (dst_regcm & REGCM_DIVIDEND32) {
24080 if (dst_size > SIZEOF_I16) {
24081 internal_error(state, ins, "%dbit constant...", dst_size);
24083 fprintf(fp, "\tmov $0, %%dx\n");
24084 fprintf(fp, "\tmov ");
24085 print_const_val(state, src, fp);
24086 fprintf(fp, ", %%ax");
24088 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
24090 if (dst_size > SIZEOF_I32) {
24091 internal_error(state, ins, "%d bit constant...", dst_size);
24093 ref = get_const_pool_ref(state, src, SIZEOF_I32, fp);
24094 fprintf(fp, "\tmovd L%s%lu, %s\n",
24095 state->compiler->label_prefix, ref,
24096 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
24099 internal_error(state, ins, "unknown copy immediate type");
24102 /* Leave now if this is not a type conversion */
24103 if (ins->op != OP_CONVERT) {
24106 /* Now make certain I have not logically overflowed the destination */
24107 if ((size_of(state, src->type) > size_of(state, dst->type)) &&
24108 (size_of(state, dst->type) < reg_size(state, dst)))
24110 unsigned long mask;
24113 if (size_of(state, dst->type) >= 32) {
24114 fprintf(state->errout, "dst type: ");
24115 name_of(state->errout, dst->type);
24116 fprintf(state->errout, "\n");
24117 internal_error(state, dst, "unhandled dst type size");
24120 mask <<= size_of(state, dst->type);
24123 dst_reg = ID_REG(dst->id);
24124 dst_regcm = arch_reg_regcm(state, dst_reg);
24126 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24127 fprintf(fp, "\tand $0x%lx, %s\n",
24128 mask, reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24130 else if (dst_regcm & REGCM_MMX) {
24132 ref = get_mask_pool_ref(state, dst, mask, fp);
24133 fprintf(fp, "\tpand L%s%lu, %s\n",
24134 state->compiler->label_prefix, ref,
24135 reg(state, dst, REGCM_MMX));
24137 else if (dst_regcm & REGCM_XMM) {
24139 ref = get_mask_pool_ref(state, dst, mask, fp);
24140 fprintf(fp, "\tpand L%s%lu, %s\n",
24141 state->compiler->label_prefix, ref,
24142 reg(state, dst, REGCM_XMM));
24145 fprintf(state->errout, "dst type: ");
24146 name_of(state->errout, dst->type);
24147 fprintf(state->errout, "\n");
24148 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24149 internal_error(state, dst, "failed to trunc value: mask %lx", mask);
24152 /* Make certain I am properly sign extended */
24153 if ((size_of(state, src->type) < size_of(state, dst->type)) &&
24154 (is_signed(src->type)))
24156 int bits, reg_bits, shift_bits;
24160 bits = size_of(state, src->type);
24161 reg_bits = reg_size(state, dst);
24162 if (reg_bits > 32) {
24165 shift_bits = reg_bits - size_of(state, src->type);
24166 dst_reg = ID_REG(dst->id);
24167 dst_regcm = arch_reg_regcm(state, dst_reg);
24169 if (shift_bits < 0) {
24170 internal_error(state, dst, "negative shift?");
24173 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24174 fprintf(fp, "\tshl $%d, %s\n",
24176 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24177 fprintf(fp, "\tsar $%d, %s\n",
24179 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24181 else if (dst_regcm & (REGCM_MMX | REGCM_XMM)) {
24182 fprintf(fp, "\tpslld $%d, %s\n",
24184 reg(state, dst, REGCM_MMX | REGCM_XMM));
24185 fprintf(fp, "\tpsrad $%d, %s\n",
24187 reg(state, dst, REGCM_MMX | REGCM_XMM));
24190 fprintf(state->errout, "dst type: ");
24191 name_of(state->errout, dst->type);
24192 fprintf(state->errout, "\n");
24193 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24194 internal_error(state, dst, "failed to signed extend value");
24199 static void print_op_load(struct compile_state *state,
24200 struct triple *ins, FILE *fp)
24202 struct triple *dst, *src;
24206 if (is_const(src) || is_const(dst)) {
24207 internal_error(state, ins, "unknown load operation");
24209 switch(ins->type->type & TYPE_MASK) {
24210 case TYPE_CHAR: op = "movsbl"; break;
24211 case TYPE_UCHAR: op = "movzbl"; break;
24212 case TYPE_SHORT: op = "movswl"; break;
24213 case TYPE_USHORT: op = "movzwl"; break;
24214 case TYPE_INT: case TYPE_UINT:
24215 case TYPE_LONG: case TYPE_ULONG:
24220 internal_error(state, ins, "unknown type in load");
24221 op = "<invalid opcode>";
24224 fprintf(fp, "\t%s (%s), %s\n",
24226 reg(state, src, REGCM_GPR32),
24227 reg(state, dst, REGCM_GPR32));
24231 static void print_op_store(struct compile_state *state,
24232 struct triple *ins, FILE *fp)
24234 struct triple *dst, *src;
24237 if (is_const(src) && (src->op == OP_INTCONST)) {
24239 value = (long_t)(src->u.cval);
24240 fprintf(fp, "\tmov%s $%ld, (%s)\n",
24241 type_suffix(state, src->type),
24243 reg(state, dst, REGCM_GPR32));
24245 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
24246 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
24247 type_suffix(state, src->type),
24248 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
24249 (unsigned long)(dst->u.cval));
24252 if (is_const(src) || is_const(dst)) {
24253 internal_error(state, ins, "unknown store operation");
24255 fprintf(fp, "\tmov%s %s, (%s)\n",
24256 type_suffix(state, src->type),
24257 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
24258 reg(state, dst, REGCM_GPR32));
24264 static void print_op_smul(struct compile_state *state,
24265 struct triple *ins, FILE *fp)
24267 if (!is_const(RHS(ins, 1))) {
24268 fprintf(fp, "\timul %s, %s\n",
24269 reg(state, RHS(ins, 1), REGCM_GPR32),
24270 reg(state, RHS(ins, 0), REGCM_GPR32));
24273 fprintf(fp, "\timul ");
24274 print_const_val(state, RHS(ins, 1), fp);
24275 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
24279 static void print_op_cmp(struct compile_state *state,
24280 struct triple *ins, FILE *fp)
24284 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
24285 dreg = check_reg(state, ins, REGCM_FLAGS);
24286 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
24287 internal_error(state, ins, "bad dest register for cmp");
24289 if (is_const(RHS(ins, 1))) {
24290 fprintf(fp, "\tcmp ");
24291 print_const_val(state, RHS(ins, 1), fp);
24292 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
24295 unsigned lmask, rmask;
24297 lreg = check_reg(state, RHS(ins, 0), mask);
24298 rreg = check_reg(state, RHS(ins, 1), mask);
24299 lmask = arch_reg_regcm(state, lreg);
24300 rmask = arch_reg_regcm(state, rreg);
24301 mask = lmask & rmask;
24302 fprintf(fp, "\tcmp %s, %s\n",
24303 reg(state, RHS(ins, 1), mask),
24304 reg(state, RHS(ins, 0), mask));
24308 static void print_op_test(struct compile_state *state,
24309 struct triple *ins, FILE *fp)
24312 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
24313 fprintf(fp, "\ttest %s, %s\n",
24314 reg(state, RHS(ins, 0), mask),
24315 reg(state, RHS(ins, 0), mask));
24318 static void print_op_branch(struct compile_state *state,
24319 struct triple *branch, FILE *fp)
24321 const char *bop = "j";
24322 if ((branch->op == OP_JMP) || (branch->op == OP_CALL)) {
24323 if (branch->rhs != 0) {
24324 internal_error(state, branch, "jmp with condition?");
24329 struct triple *ptr;
24330 if (branch->rhs != 1) {
24331 internal_error(state, branch, "jmpcc without condition?");
24333 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
24334 if ((RHS(branch, 0)->op != OP_CMP) &&
24335 (RHS(branch, 0)->op != OP_TEST)) {
24336 internal_error(state, branch, "bad branch test");
24338 #warning "FIXME I have observed instructions between the test and branch instructions"
24339 ptr = RHS(branch, 0);
24340 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
24341 if (ptr->op != OP_COPY) {
24342 internal_error(state, branch, "branch does not follow test");
24345 switch(branch->op) {
24346 case OP_JMP_EQ: bop = "jz"; break;
24347 case OP_JMP_NOTEQ: bop = "jnz"; break;
24348 case OP_JMP_SLESS: bop = "jl"; break;
24349 case OP_JMP_ULESS: bop = "jb"; break;
24350 case OP_JMP_SMORE: bop = "jg"; break;
24351 case OP_JMP_UMORE: bop = "ja"; break;
24352 case OP_JMP_SLESSEQ: bop = "jle"; break;
24353 case OP_JMP_ULESSEQ: bop = "jbe"; break;
24354 case OP_JMP_SMOREEQ: bop = "jge"; break;
24355 case OP_JMP_UMOREEQ: bop = "jae"; break;
24357 internal_error(state, branch, "Invalid branch op");
24363 if (branch->op == OP_CALL) {
24364 fprintf(fp, "\t/* call */\n");
24367 fprintf(fp, "\t%s L%s%lu\n",
24369 state->compiler->label_prefix,
24370 (unsigned long)(TARG(branch, 0)->u.cval));
24373 static void print_op_ret(struct compile_state *state,
24374 struct triple *branch, FILE *fp)
24376 fprintf(fp, "\tjmp *%s\n",
24377 reg(state, RHS(branch, 0), REGCM_GPR32));
24380 static void print_op_set(struct compile_state *state,
24381 struct triple *set, FILE *fp)
24383 const char *sop = "set";
24384 if (set->rhs != 1) {
24385 internal_error(state, set, "setcc without condition?");
24387 check_reg(state, RHS(set, 0), REGCM_FLAGS);
24388 if ((RHS(set, 0)->op != OP_CMP) &&
24389 (RHS(set, 0)->op != OP_TEST)) {
24390 internal_error(state, set, "bad set test");
24392 if (RHS(set, 0)->next != set) {
24393 internal_error(state, set, "set does not follow test");
24396 case OP_SET_EQ: sop = "setz"; break;
24397 case OP_SET_NOTEQ: sop = "setnz"; break;
24398 case OP_SET_SLESS: sop = "setl"; break;
24399 case OP_SET_ULESS: sop = "setb"; break;
24400 case OP_SET_SMORE: sop = "setg"; break;
24401 case OP_SET_UMORE: sop = "seta"; break;
24402 case OP_SET_SLESSEQ: sop = "setle"; break;
24403 case OP_SET_ULESSEQ: sop = "setbe"; break;
24404 case OP_SET_SMOREEQ: sop = "setge"; break;
24405 case OP_SET_UMOREEQ: sop = "setae"; break;
24407 internal_error(state, set, "Invalid set op");
24410 fprintf(fp, "\t%s %s\n",
24411 sop, reg(state, set, REGCM_GPR8_LO));
24414 static void print_op_bit_scan(struct compile_state *state,
24415 struct triple *ins, FILE *fp)
24419 case OP_BSF: op = "bsf"; break;
24420 case OP_BSR: op = "bsr"; break;
24422 internal_error(state, ins, "unknown bit scan");
24432 reg(state, RHS(ins, 0), REGCM_GPR32),
24433 reg(state, ins, REGCM_GPR32),
24434 reg(state, ins, REGCM_GPR32));
24438 static void print_sdecl(struct compile_state *state,
24439 struct triple *ins, FILE *fp)
24441 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
24442 fprintf(fp, ".balign %d\n", align_of_in_bytes(state, ins->type));
24443 fprintf(fp, "L%s%lu:\n",
24444 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
24445 print_const(state, MISC(ins, 0), fp);
24446 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
24450 static void print_instruction(struct compile_state *state,
24451 struct triple *ins, FILE *fp)
24453 /* Assumption: after I have exted the register allocator
24454 * everything is in a valid register.
24458 print_op_asm(state, ins, fp);
24460 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
24461 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
24462 case OP_AND: print_binary_op(state, "and", ins, fp); break;
24463 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
24464 case OP_OR: print_binary_op(state, "or", ins, fp); break;
24465 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
24466 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
24467 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
24468 case OP_POS: break;
24469 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
24470 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
24475 /* Don't generate anything here for constants */
24477 /* Don't generate anything for variable declarations. */
24479 case OP_UNKNOWNVAL:
24480 fprintf(fp, " /* unknown %s */\n",
24481 reg(state, ins, REGCM_ALL));
24484 print_sdecl(state, ins, fp);
24488 print_op_move(state, ins, fp);
24491 print_op_load(state, ins, fp);
24494 print_op_store(state, ins, fp);
24497 print_op_smul(state, ins, fp);
24499 case OP_CMP: print_op_cmp(state, ins, fp); break;
24500 case OP_TEST: print_op_test(state, ins, fp); break;
24502 case OP_JMP_EQ: case OP_JMP_NOTEQ:
24503 case OP_JMP_SLESS: case OP_JMP_ULESS:
24504 case OP_JMP_SMORE: case OP_JMP_UMORE:
24505 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
24506 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
24508 print_op_branch(state, ins, fp);
24511 print_op_ret(state, ins, fp);
24513 case OP_SET_EQ: case OP_SET_NOTEQ:
24514 case OP_SET_SLESS: case OP_SET_ULESS:
24515 case OP_SET_SMORE: case OP_SET_UMORE:
24516 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
24517 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
24518 print_op_set(state, ins, fp);
24520 case OP_INB: case OP_INW: case OP_INL:
24521 print_op_in(state, ins, fp);
24523 case OP_OUTB: case OP_OUTW: case OP_OUTL:
24524 print_op_out(state, ins, fp);
24528 print_op_bit_scan(state, ins, fp);
24531 after_lhs(state, ins);
24532 fprintf(fp, "\trdmsr\n");
24535 fprintf(fp, "\twrmsr\n");
24538 fprintf(fp, "\thlt\n");
24541 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24544 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24547 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24553 fprintf(fp, "L%s%lu:\n",
24554 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
24557 /* Ignore adecls with no registers error otherwise */
24558 if (!noop_adecl(ins)) {
24559 internal_error(state, ins, "adecl remains?");
24562 /* Ignore OP_PIECE */
24565 /* Operations that should never get here */
24566 case OP_SDIV: case OP_UDIV:
24567 case OP_SMOD: case OP_UMOD:
24568 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
24569 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
24570 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
24572 internal_error(state, ins, "unknown op: %d %s",
24573 ins->op, tops(ins->op));
24578 static void print_instructions(struct compile_state *state)
24580 struct triple *first, *ins;
24581 int print_location;
24582 struct occurance *last_occurance;
24584 int max_inline_depth;
24585 max_inline_depth = 0;
24586 print_location = 1;
24587 last_occurance = 0;
24588 fp = state->output;
24589 /* Masks for common sizes */
24590 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
24591 fprintf(fp, ".balign 16\n");
24592 fprintf(fp, "L%s1:\n", state->compiler->label_prefix);
24593 fprintf(fp, ".int 0xff, 0, 0, 0\n");
24594 fprintf(fp, "L%s2:\n", state->compiler->label_prefix);
24595 fprintf(fp, ".int 0xffff, 0, 0, 0\n");
24596 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
24597 first = state->first;
24600 if (print_location &&
24601 last_occurance != ins->occurance) {
24602 if (!ins->occurance->parent) {
24603 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
24604 ins->occurance->function,
24605 ins->occurance->filename,
24606 ins->occurance->line,
24607 ins->occurance->col);
24610 struct occurance *ptr;
24612 fprintf(fp, "\t/*\n");
24614 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
24616 fprintf(fp, "\t * %s,%s:%d.%d\n",
24622 fprintf(fp, "\t */\n");
24623 if (inline_depth > max_inline_depth) {
24624 max_inline_depth = inline_depth;
24627 if (last_occurance) {
24628 put_occurance(last_occurance);
24630 get_occurance(ins->occurance);
24631 last_occurance = ins->occurance;
24634 print_instruction(state, ins, fp);
24636 } while(ins != first);
24637 if (print_location) {
24638 fprintf(fp, "/* max inline depth %d */\n",
24643 static void generate_code(struct compile_state *state)
24645 generate_local_labels(state);
24646 print_instructions(state);
24650 static void print_preprocessed_tokens(struct compile_state *state)
24655 const char *filename;
24656 fp = state->output;
24661 const char *token_str;
24663 if (tok == TOK_EOF) {
24666 tk = eat(state, tok);
24668 tk->ident ? tk->ident->name :
24669 tk->str_len ? tk->val.str :
24672 if ((state->file->line != line) ||
24673 (state->file->basename != filename)) {
24675 if ((state->file->basename == filename) &&
24676 (line < state->file->line)) {
24677 while(line < state->file->line) {
24683 fprintf(fp, "\n#line %d \"%s\"\n",
24684 state->file->line, state->file->basename);
24686 line = state->file->line;
24687 filename = state->file->basename;
24688 col = get_col(state->file) - strlen(token_str);
24689 for(i = 0; i < col; i++) {
24694 fprintf(fp, "%s ", token_str);
24696 if (state->compiler->debug & DEBUG_TOKENS) {
24697 loc(state->dbgout, state, 0);
24698 fprintf(state->dbgout, "%s <- `%s'\n",
24699 tokens[tok], token_str);
24704 static void compile(const char *filename,
24705 struct compiler_state *compiler, struct arch_state *arch)
24708 struct compile_state state;
24709 struct triple *ptr;
24710 memset(&state, 0, sizeof(state));
24711 state.compiler = compiler;
24714 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
24715 memset(&state.token[i], 0, sizeof(state.token[i]));
24716 state.token[i].tok = -1;
24718 /* Remember the output descriptors */
24719 state.errout = stderr;
24720 state.dbgout = stdout;
24721 /* Remember the output filename */
24722 state.output = fopen(state.compiler->ofilename, "w");
24723 if (!state.output) {
24724 error(&state, 0, "Cannot open output file %s\n",
24725 state.compiler->ofilename);
24727 /* Make certain a good cleanup happens */
24728 exit_state = &state;
24729 atexit(exit_cleanup);
24731 /* Prep the preprocessor */
24732 state.if_depth = 0;
24733 memset(state.if_bytes, 0, sizeof(state.if_bytes));
24734 /* register the C keywords */
24735 register_keywords(&state);
24736 /* register the keywords the macro preprocessor knows */
24737 register_macro_keywords(&state);
24738 /* generate some builtin macros */
24739 register_builtin_macros(&state);
24740 /* Memorize where some special keywords are. */
24741 state.i_switch = lookup(&state, "switch", 6);
24742 state.i_case = lookup(&state, "case", 4);
24743 state.i_continue = lookup(&state, "continue", 8);
24744 state.i_break = lookup(&state, "break", 5);
24745 state.i_default = lookup(&state, "default", 7);
24746 state.i_return = lookup(&state, "return", 6);
24747 /* Memorize where predefined macros are. */
24748 state.i___VA_ARGS__ = lookup(&state, "__VA_ARGS__", 11);
24749 state.i___FILE__ = lookup(&state, "__FILE__", 8);
24750 state.i___LINE__ = lookup(&state, "__LINE__", 8);
24751 /* Memorize where predefined identifiers are. */
24752 state.i___func__ = lookup(&state, "__func__", 8);
24753 /* Memorize where some attribute keywords are. */
24754 state.i_noinline = lookup(&state, "noinline", 8);
24755 state.i_always_inline = lookup(&state, "always_inline", 13);
24757 /* Process the command line macros */
24758 process_cmdline_macros(&state);
24760 /* Allocate beginning bounding labels for the function list */
24761 state.first = label(&state);
24762 state.first->id |= TRIPLE_FLAG_VOLATILE;
24763 use_triple(state.first, state.first);
24764 ptr = label(&state);
24765 ptr->id |= TRIPLE_FLAG_VOLATILE;
24766 use_triple(ptr, ptr);
24767 flatten(&state, state.first, ptr);
24769 /* Allocate a label for the pool of global variables */
24770 state.global_pool = label(&state);
24771 state.global_pool->id |= TRIPLE_FLAG_VOLATILE;
24772 flatten(&state, state.first, state.global_pool);
24774 /* Enter the globl definition scope */
24775 start_scope(&state);
24776 register_builtins(&state);
24777 compile_file(&state, filename, 1);
24779 /* Stop if all we want is preprocessor output */
24780 if (state.compiler->flags & COMPILER_CPP_ONLY) {
24781 print_preprocessed_tokens(&state);
24787 /* Exit the global definition scope */
24790 /* Now that basic compilation has happened
24791 * optimize the intermediate code
24795 generate_code(&state);
24796 if (state.compiler->debug) {
24797 fprintf(state.errout, "done\n");
24802 static void version(FILE *fp)
24804 fprintf(fp, "romcc " VERSION " released " RELEASE_DATE "\n");
24807 static void usage(void)
24812 "\nUsage: romcc [options] <source>.c\n"
24813 "Compile a C source file generating a binary that does not implicilty use RAM\n"
24815 "-o <output file name>\n"
24816 "-f<option> Specify a generic compiler option\n"
24817 "-m<option> Specify a arch dependent option\n"
24818 "-- Specify this is the last option\n"
24819 "\nGeneric compiler options:\n"
24821 compiler_usage(fp);
24823 "\nArchitecture compiler options:\n"
24831 static void arg_error(char *fmt, ...)
24834 va_start(args, fmt);
24835 vfprintf(stderr, fmt, args);
24841 int main(int argc, char **argv)
24843 const char *filename;
24844 struct compiler_state compiler;
24845 struct arch_state arch;
24849 /* I don't want any surprises */
24850 setlocale(LC_ALL, "C");
24852 init_compiler_state(&compiler);
24853 init_arch_state(&arch);
24857 if (!all_opts && (strcmp(argv[1], "-o") == 0) && (argc > 2)) {
24858 compiler.ofilename = argv[2];
24862 else if (!all_opts && argv[1][0] == '-') {
24865 if (strcmp(argv[1], "--") == 0) {
24869 else if (strncmp(argv[1], "-E", 2) == 0) {
24870 result = compiler_encode_flag(&compiler, argv[1]);
24872 else if (strncmp(argv[1], "-O", 2) == 0) {
24873 result = compiler_encode_flag(&compiler, argv[1]);
24875 else if (strncmp(argv[1], "-I", 2) == 0) {
24876 result = compiler_encode_flag(&compiler, argv[1]);
24878 else if (strncmp(argv[1], "-D", 2) == 0) {
24879 result = compiler_encode_flag(&compiler, argv[1]);
24881 else if (strncmp(argv[1], "-U", 2) == 0) {
24882 result = compiler_encode_flag(&compiler, argv[1]);
24884 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
24885 result = compiler_encode_flag(&compiler, argv[1]+2);
24887 else if (strncmp(argv[1], "-f", 2) == 0) {
24888 result = compiler_encode_flag(&compiler, argv[1]+2);
24890 else if (strncmp(argv[1], "-m", 2) == 0) {
24891 result = arch_encode_flag(&arch, argv[1]+2);
24894 arg_error("Invalid option specified: %s\n",
24902 arg_error("Only one filename may be specified\n");
24904 filename = argv[1];
24910 arg_error("No filename specified\n");
24912 compile(filename, &compiler, &arch);