15 #define DEBUG_ERROR_MESSAGES 0
16 #define DEBUG_COLOR_GRAPH 0
18 #define DEBUG_CONSISTENCY 1
20 /* Control flow graph of a loop without goto.
31 * |\ GGG HHH | continue;
59 * DFlocal(X) = { Y <- Succ(X) | idom(Y) != X }
60 * DFup(Z) = { Y <- DF(Z) | idom(Y) != X }
63 * [] == DFlocal(X) U DF(X)
66 * Dominator graph of the same nodes.
70 * BBB JJJ BBB: [ JJJ ] ( JJJ ) JJJ: [ ] ()
72 * CCC CCC: [ ] ( BBB, JJJ )
74 * DDD EEE DDD: [ ] ( BBB ) EEE: [ JJJ ] ()
76 * FFF FFF: [ ] ( BBB )
78 * GGG HHH GGG: [ ] ( BBB ) HHH: [ BBB ] ()
83 * BBB and JJJ are definitely the dominance frontier.
84 * Where do I place phi functions and how do I make that decision.
87 static void die(char *fmt, ...)
92 vfprintf(stderr, fmt, args);
99 #define MALLOC_STRONG_DEBUG
100 static void *xmalloc(size_t size, const char *name)
105 die("Cannot malloc %ld bytes to hold %s: %s\n",
106 size + 0UL, name, strerror(errno));
111 static void *xcmalloc(size_t size, const char *name)
114 buf = xmalloc(size, name);
115 memset(buf, 0, size);
119 static void xfree(const void *ptr)
124 static char *xstrdup(const char *str)
129 new = xmalloc(len + 1, "xstrdup string");
130 memcpy(new, str, len);
135 static void xchdir(const char *path)
137 if (chdir(path) != 0) {
138 die("chdir to %s failed: %s\n",
139 path, strerror(errno));
143 static int exists(const char *dirname, const char *filename)
147 if (access(filename, O_RDONLY) < 0) {
148 if ((errno != EACCES) && (errno != EROFS)) {
156 static char *slurp_file(const char *dirname, const char *filename, off_t *r_size)
160 off_t size, progress;
169 fd = open(filename, O_RDONLY);
171 die("Cannot open '%s' : %s\n",
172 filename, strerror(errno));
174 result = fstat(fd, &stats);
176 die("Cannot stat: %s: %s\n",
177 filename, strerror(errno));
179 size = stats.st_size;
181 buf = xmalloc(size +2, filename);
182 buf[size] = '\n'; /* Make certain the file is newline terminated */
183 buf[size+1] = '\0'; /* Null terminate the file for good measure */
185 while(progress < size) {
186 result = read(fd, buf + progress, size - progress);
188 if ((errno == EINTR) || (errno == EAGAIN))
190 die("read on %s of %ld bytes failed: %s\n",
191 filename, (size - progress)+ 0UL, strerror(errno));
197 die("Close of %s failed: %s\n",
198 filename, strerror(errno));
203 /* Long on the destination platform */
204 typedef unsigned long ulong_t;
208 struct file_state *prev;
209 const char *basename;
220 struct hash_entry *ident;
228 /* I have two classes of types:
230 * Logical types. (The type the C standard says the operation is of)
232 * The operational types are:
247 * No memory is useable by the compiler.
248 * There is no floating point support.
249 * All operations take place in general purpose registers.
250 * There is one type of general purpose register.
251 * Unsigned longs are stored in that general purpose register.
254 /* Operations on general purpose registers.
271 #define OP_POS 14 /* Dummy positive operator don't use it */
281 #define OP_SLESSEQ 26
282 #define OP_ULESSEQ 27
283 #define OP_SMOREEQ 28
284 #define OP_UMOREEQ 29
286 #define OP_LFALSE 30 /* Test if the expression is logically false */
287 #define OP_LTRUE 31 /* Test if the expression is logcially true */
294 #define OP_MIN_CONST 50
295 #define OP_MAX_CONST 59
296 #define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
297 #define OP_INTCONST 50
298 #define OP_BLOBCONST 51
299 /* For OP_BLOBCONST ->type holds the layout and size
300 * information. u.blob holds a pointer to the raw binary
301 * data for the constant initializer.
303 #define OP_ADDRCONST 52
304 /* For OP_ADDRCONST ->type holds the type.
305 * MISC(0) holds the reference to the static variable.
306 * ->u.cval holds an offset from that value.
310 /* OP_WRITE moves one pseudo register to another.
311 * LHS(0) holds the destination pseudo register, which must be an OP_DECL.
312 * RHS(0) holds the psuedo to move.
316 /* OP_READ reads the value of a variable and makes
317 * it available for the pseudo operation.
318 * Useful for things like def-use chains.
319 * RHS(0) holds points to the triple to read from.
322 /* OP_COPY makes a copy of the psedo register or constant in RHS(0).
325 /* OP_PIECE returns one piece of a instruction that returns a structure.
326 * MISC(0) is the instruction
327 * u.cval is the LHS piece of the instruction to return.
330 /* OP_ASM holds a sequence of assembly instructions, the result
331 * of a C asm directive.
332 * RHS(x) holds input value x to the assembly sequence.
333 * LHS(x) holds the output value x from the assembly sequence.
334 * u.blob holds the string of assembly instructions.
338 /* OP_DEREF generates an lvalue from a pointer.
339 * RHS(0) holds the pointer value.
340 * OP_DEREF serves as a place holder to indicate all necessary
341 * checks have been done to indicate a value is an lvalue.
344 /* OP_DOT references a submember of a structure lvalue.
345 * RHS(0) holds the lvalue.
346 * ->u.field holds the name of the field we want.
348 * Not seen outside of expressions.
351 /* OP_VAL returns the value of a subexpression of the current expression.
352 * Useful for operators that have side effects.
353 * RHS(0) holds the expression.
354 * MISC(0) holds the subexpression of RHS(0) that is the
355 * value of the expression.
357 * Not seen outside of expressions.
360 /* OP_LAND performs a C logical and between RHS(0) and RHS(1).
361 * Not seen outside of expressions.
364 /* OP_LOR performs a C logical or between RHS(0) and RHS(1).
365 * Not seen outside of expressions.
368 /* OP_CODE performas a C ? : operation.
369 * RHS(0) holds the test.
370 * RHS(1) holds the expression to evaluate if the test returns true.
371 * RHS(2) holds the expression to evaluate if the test returns false.
372 * Not seen outside of expressions.
375 /* OP_COMMA performacs a C comma operation.
376 * That is RHS(0) is evaluated, then RHS(1)
377 * and the value of RHS(1) is returned.
378 * Not seen outside of expressions.
382 /* OP_CALL performs a procedure call.
383 * MISC(0) holds a pointer to the OP_LIST of a function
384 * RHS(x) holds argument x of a function
386 * Currently not seen outside of expressions.
388 #define OP_VAL_VEC 74
389 /* OP_VAL_VEC is an array of triples that are either variable
390 * or values for a structure or an array.
391 * RHS(x) holds element x of the vector.
392 * triple->type->elements holds the size of the vector.
397 /* OP_LIST Holds a list of statements, and a result value.
398 * RHS(0) holds the list of statements.
399 * MISC(0) holds the value of the statements.
402 #define OP_BRANCH 81 /* branch */
403 /* For branch instructions
404 * TARG(0) holds the branch target.
405 * RHS(0) if present holds the branch condition.
406 * ->next holds where to branch to if the branch is not taken.
407 * The branch target can only be a decl...
411 /* OP_LABEL is a triple that establishes an target for branches.
412 * ->use is the list of all branches that use this label.
416 /* OP_DECL is a triple that establishes an lvalue for assignments.
417 * ->use is a list of statements that use the variable.
421 /* OP_SDECL is a triple that establishes a variable of static
423 * ->use is a list of statements that use the variable.
424 * MISC(0) holds the initializer expression.
429 /* OP_PHI is a triple used in SSA form code.
430 * It is used when multiple code paths merge and a variable needs
431 * a single assignment from any of those code paths.
432 * The operation is a cross between OP_DECL and OP_WRITE, which
433 * is what OP_PHI is geneared from.
435 * RHS(x) points to the value from code path x
436 * The number of RHS entries is the number of control paths into the block
437 * in which OP_PHI resides. The elements of the array point to point
438 * to the variables OP_PHI is derived from.
440 * MISC(0) holds a pointer to the orginal OP_DECL node.
443 /* Architecture specific instructions */
446 #define OP_SET_EQ 102
447 #define OP_SET_NOTEQ 103
448 #define OP_SET_SLESS 104
449 #define OP_SET_ULESS 105
450 #define OP_SET_SMORE 106
451 #define OP_SET_UMORE 107
452 #define OP_SET_SLESSEQ 108
453 #define OP_SET_ULESSEQ 109
454 #define OP_SET_SMOREEQ 110
455 #define OP_SET_UMOREEQ 111
458 #define OP_JMP_EQ 113
459 #define OP_JMP_NOTEQ 114
460 #define OP_JMP_SLESS 115
461 #define OP_JMP_ULESS 116
462 #define OP_JMP_SMORE 117
463 #define OP_JMP_UMORE 118
464 #define OP_JMP_SLESSEQ 119
465 #define OP_JMP_ULESSEQ 120
466 #define OP_JMP_SMOREEQ 121
467 #define OP_JMP_UMOREEQ 122
469 /* Builtin operators that it is just simpler to use the compiler for */
487 #define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
489 #define BLOCK 8 /* Triple stores the current block */
490 unsigned char lhs, rhs, misc, targ;
493 #define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \
501 static const struct op_info table_ops[] = {
502 [OP_SMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smul"),
503 [OP_UMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umul"),
504 [OP_SDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sdiv"),
505 [OP_UDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "udiv"),
506 [OP_SMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smod"),
507 [OP_UMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umod"),
508 [OP_ADD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "add"),
509 [OP_SUB ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sub"),
510 [OP_SL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sl"),
511 [OP_USR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "usr"),
512 [OP_SSR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ssr"),
513 [OP_AND ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "and"),
514 [OP_XOR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "xor"),
515 [OP_OR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "or"),
516 [OP_POS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "pos"),
517 [OP_NEG ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "neg"),
518 [OP_INVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "invert"),
520 [OP_EQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "eq"),
521 [OP_NOTEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "noteq"),
522 [OP_SLESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sless"),
523 [OP_ULESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "uless"),
524 [OP_SMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smore"),
525 [OP_UMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umore"),
526 [OP_SLESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "slesseq"),
527 [OP_ULESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ulesseq"),
528 [OP_SMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smoreeq"),
529 [OP_UMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umoreeq"),
530 [OP_LFALSE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "lfalse"),
531 [OP_LTRUE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "ltrue"),
533 [OP_LOAD ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "load"),
534 [OP_STORE ] = OP( 1, 1, 0, 0, IMPURE | BLOCK , "store"),
536 [OP_NOOP ] = OP( 0, 0, 0, 0, PURE | BLOCK, "noop"),
538 [OP_INTCONST ] = OP( 0, 0, 0, 0, PURE | DEF, "intconst"),
539 [OP_BLOBCONST ] = OP( 0, 0, 0, 0, PURE, "blobconst"),
540 [OP_ADDRCONST ] = OP( 0, 0, 1, 0, PURE | DEF, "addrconst"),
542 [OP_WRITE ] = OP( 1, 1, 0, 0, PURE | BLOCK, "write"),
543 [OP_READ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "read"),
544 [OP_COPY ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "copy"),
545 [OP_PIECE ] = OP( 0, 0, 1, 0, PURE | DEF, "piece"),
546 [OP_ASM ] = OP(-1, -1, 0, 0, IMPURE, "asm"),
547 [OP_DEREF ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "deref"),
548 [OP_DOT ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "dot"),
550 [OP_VAL ] = OP( 0, 1, 1, 0, 0 | DEF | BLOCK, "val"),
551 [OP_LAND ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "land"),
552 [OP_LOR ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "lor"),
553 [OP_COND ] = OP( 0, 3, 0, 0, 0 | DEF | BLOCK, "cond"),
554 [OP_COMMA ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "comma"),
555 /* Call is special most it can stand in for anything so it depends on context */
556 [OP_CALL ] = OP(-1, -1, 1, 0, 0 | BLOCK, "call"),
557 /* The sizes of OP_CALL and OP_VAL_VEC depend upon context */
558 [OP_VAL_VEC ] = OP( 0, -1, 0, 0, 0 | BLOCK, "valvec"),
560 [OP_LIST ] = OP( 0, 1, 1, 0, 0 | DEF, "list"),
561 /* The number of targets for OP_BRANCH depends on context */
562 [OP_BRANCH ] = OP( 0, -1, 0, 1, PURE | BLOCK, "branch"),
563 [OP_LABEL ] = OP( 0, 0, 0, 0, PURE | BLOCK, "label"),
564 [OP_ADECL ] = OP( 0, 0, 0, 0, PURE | BLOCK, "adecl"),
565 [OP_SDECL ] = OP( 0, 0, 1, 0, PURE | BLOCK, "sdecl"),
566 /* The number of RHS elements of OP_PHI depend upon context */
567 [OP_PHI ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
569 [OP_CMP ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK, "cmp"),
570 [OP_TEST ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "test"),
571 [OP_SET_EQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_eq"),
572 [OP_SET_NOTEQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_noteq"),
573 [OP_SET_SLESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_sless"),
574 [OP_SET_ULESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_uless"),
575 [OP_SET_SMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smore"),
576 [OP_SET_UMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umore"),
577 [OP_SET_SLESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_slesseq"),
578 [OP_SET_ULESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_ulesseq"),
579 [OP_SET_SMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smoreq"),
580 [OP_SET_UMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umoreq"),
581 [OP_JMP ] = OP( 0, 0, 0, 1, PURE | BLOCK, "jmp"),
582 [OP_JMP_EQ ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_eq"),
583 [OP_JMP_NOTEQ ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_noteq"),
584 [OP_JMP_SLESS ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_sless"),
585 [OP_JMP_ULESS ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_uless"),
586 [OP_JMP_SMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_smore"),
587 [OP_JMP_UMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_umore"),
588 [OP_JMP_SLESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_slesseq"),
589 [OP_JMP_ULESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_ulesseq"),
590 [OP_JMP_SMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_smoreq"),
591 [OP_JMP_UMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_umoreq"),
593 [OP_INB ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inb"),
594 [OP_INW ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inw"),
595 [OP_INL ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inl"),
596 [OP_OUTB ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outb"),
597 [OP_OUTW ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outw"),
598 [OP_OUTL ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outl"),
599 [OP_BSF ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsf"),
600 [OP_BSR ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsr"),
601 [OP_RDMSR ] = OP( 2, 1, 0, 0, IMPURE | BLOCK, "__rdmsr"),
602 [OP_WRMSR ] = OP( 0, 3, 0, 0, IMPURE | BLOCK, "__wrmsr"),
603 [OP_HLT ] = OP( 0, 0, 0, 0, IMPURE | BLOCK, "__hlt"),
606 #define OP_MAX (sizeof(table_ops)/sizeof(table_ops[0]))
608 static const char *tops(int index)
610 static const char unknown[] = "unknown op";
614 if (index > OP_MAX) {
617 return table_ops[index].name;
624 struct triple_set *next;
625 struct triple *member;
634 struct triple *next, *prev;
635 struct triple_set *use;
638 unsigned char template_id;
639 unsigned short sizes;
640 #define TRIPLE_LHS(SIZES) (((SIZES) >> 0) & 0x0f)
641 #define TRIPLE_RHS(SIZES) (((SIZES) >> 4) & 0x0f)
642 #define TRIPLE_MISC(SIZES) (((SIZES) >> 8) & 0x0f)
643 #define TRIPLE_TARG(SIZES) (((SIZES) >> 12) & 0x0f)
644 #define TRIPLE_SIZE(SIZES) \
645 ((((SIZES) >> 0) & 0x0f) + \
646 (((SIZES) >> 4) & 0x0f) + \
647 (((SIZES) >> 8) & 0x0f) + \
648 (((SIZES) >> 12) & 0x0f))
649 #define TRIPLE_SIZES(LHS, RHS, MISC, TARG) \
650 ((((LHS) & 0x0f) << 0) | \
651 (((RHS) & 0x0f) << 4) | \
652 (((MISC) & 0x0f) << 8) | \
653 (((TARG) & 0x0f) << 12))
654 #define TRIPLE_LHS_OFF(SIZES) (0)
655 #define TRIPLE_RHS_OFF(SIZES) (TRIPLE_LHS_OFF(SIZES) + TRIPLE_LHS(SIZES))
656 #define TRIPLE_MISC_OFF(SIZES) (TRIPLE_RHS_OFF(SIZES) + TRIPLE_RHS(SIZES))
657 #define TRIPLE_TARG_OFF(SIZES) (TRIPLE_MISC_OFF(SIZES) + TRIPLE_MISC(SIZES))
658 #define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF((PTR)->sizes) + (INDEX)])
659 #define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF((PTR)->sizes) + (INDEX)])
660 #define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF((PTR)->sizes) + (INDEX)])
661 #define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF((PTR)->sizes) + (INDEX)])
662 unsigned id; /* A scratch value and finally the register */
663 #define TRIPLE_FLAG_FLATTENED (1 << 31)
664 #define TRIPLE_FLAG_PRE_SPLIT (1 << 30)
665 #define TRIPLE_FLAG_POST_SPLIT (1 << 29)
666 const char *filename;
673 struct hash_entry *field;
674 struct asm_info *ainfo;
676 struct triple *param[2];
683 struct ins_template {
684 struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
688 struct ins_template tmpl;
693 struct block_set *next;
694 struct block *member;
697 struct block *work_next;
698 struct block *left, *right;
699 struct triple *first, *last;
701 struct block_set *use;
702 struct block_set *idominates;
703 struct block_set *domfrontier;
705 struct block_set *ipdominates;
706 struct block_set *ipdomfrontier;
714 struct hash_entry *ident;
721 struct hash_entry *ident;
727 struct hash_entry *next;
731 struct macro *sym_define;
732 struct symbol *sym_label;
733 struct symbol *sym_struct;
734 struct symbol *sym_ident;
737 #define HASH_TABLE_SIZE 2048
739 struct compile_state {
740 const char *ofilename;
743 struct file_state *file;
744 struct token token[4];
745 struct hash_entry *hash_table[HASH_TABLE_SIZE];
746 struct hash_entry *i_continue;
747 struct hash_entry *i_break;
749 int if_depth, if_value;
751 struct file_state *macro_file;
752 struct triple *main_function;
753 struct block *first_block, *last_block;
760 /* visibility global/local */
761 /* static/auto duration */
762 /* typedef, register, inline */
764 #define STOR_MASK 0x000f
766 #define STOR_GLOBAL 0x0001
768 #define STOR_PERM 0x0002
769 /* Storage specifiers */
770 #define STOR_AUTO 0x0000
771 #define STOR_STATIC 0x0002
772 #define STOR_EXTERN 0x0003
773 #define STOR_REGISTER 0x0004
774 #define STOR_TYPEDEF 0x0008
775 #define STOR_INLINE 0x000c
778 #define QUAL_MASK 0x0070
779 #define QUAL_NONE 0x0000
780 #define QUAL_CONST 0x0010
781 #define QUAL_VOLATILE 0x0020
782 #define QUAL_RESTRICT 0x0040
785 #define TYPE_MASK 0x1f00
786 #define TYPE_INTEGER(TYPE) (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG))
787 #define TYPE_ARITHMETIC(TYPE) (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE))
788 #define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
789 #define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
790 #define TYPE_MKUNSIGNED(TYPE) ((TYPE) | 0x0100)
791 #define TYPE_RANK(TYPE) ((TYPE) & ~0x0100)
792 #define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER)
793 #define TYPE_DEFAULT 0x0000
794 #define TYPE_VOID 0x0100
795 #define TYPE_CHAR 0x0200
796 #define TYPE_UCHAR 0x0300
797 #define TYPE_SHORT 0x0400
798 #define TYPE_USHORT 0x0500
799 #define TYPE_INT 0x0600
800 #define TYPE_UINT 0x0700
801 #define TYPE_LONG 0x0800
802 #define TYPE_ULONG 0x0900
803 #define TYPE_LLONG 0x0a00 /* long long */
804 #define TYPE_ULLONG 0x0b00
805 #define TYPE_FLOAT 0x0c00
806 #define TYPE_DOUBLE 0x0d00
807 #define TYPE_LDOUBLE 0x0e00 /* long double */
808 #define TYPE_STRUCT 0x1000
809 #define TYPE_ENUM 0x1100
810 #define TYPE_POINTER 0x1200
812 * type->left holds the type pointed to.
814 #define TYPE_FUNCTION 0x1300
815 /* For TYPE_FUNCTION:
816 * type->left holds the return type.
817 * type->right holds the...
819 #define TYPE_PRODUCT 0x1400
820 /* TYPE_PRODUCT is a basic building block when defining structures
821 * type->left holds the type that appears first in memory.
822 * type->right holds the type that appears next in memory.
824 #define TYPE_OVERLAP 0x1500
825 /* TYPE_OVERLAP is a basic building block when defining unions
826 * type->left and type->right holds to types that overlap
827 * each other in memory.
829 #define TYPE_ARRAY 0x1600
830 /* TYPE_ARRAY is a basic building block when definitng arrays.
831 * type->left holds the type we are an array of.
832 * type-> holds the number of elements.
835 #define ELEMENT_COUNT_UNSPECIFIED (~0UL)
839 struct type *left, *right;
841 struct hash_entry *field_ident;
842 struct hash_entry *type_ident;
845 #define MAX_REGISTERS 75
846 #define MAX_REG_EQUIVS 16
847 #define REGISTER_BITS 28
848 #define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
849 #define TEMPLATE_BITS 6
850 #define MAX_TEMPLATES (1<<TEMPLATE_BITS)
853 #define REG_UNNEEDED 1
854 #define REG_VIRT0 (MAX_REGISTERS + 0)
855 #define REG_VIRT1 (MAX_REGISTERS + 1)
856 #define REG_VIRT2 (MAX_REGISTERS + 2)
857 #define REG_VIRT3 (MAX_REGISTERS + 3)
858 #define REG_VIRT4 (MAX_REGISTERS + 4)
859 #define REG_VIRT5 (MAX_REGISTERS + 5)
861 /* Provision for 8 register classes */
862 #define REG_MASK (MAX_VIRT_REGISTERS -1)
863 #define ID_REG(ID) ((ID) & REG_MASK)
864 #define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
866 static unsigned arch_reg_regcm(struct compile_state *state, int reg);
867 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
868 static void arch_reg_equivs(
869 struct compile_state *state, unsigned *equiv, int reg);
870 static int arch_select_free_register(
871 struct compile_state *state, char *used, int classes);
872 static unsigned arch_regc_size(struct compile_state *state, int class);
873 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
874 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
875 static const char *arch_reg_str(int reg);
876 static struct reg_info arch_reg_constraint(
877 struct compile_state *state, struct type *type, const char *constraint);
878 static struct reg_info arch_reg_clobber(
879 struct compile_state *state, const char *clobber);
880 static struct reg_info arch_reg_lhs(struct compile_state *state,
881 struct triple *ins, int index);
882 static struct reg_info arch_reg_rhs(struct compile_state *state,
883 struct triple *ins, int index);
884 static struct triple *transform_to_arch_instruction(
885 struct compile_state *state, struct triple *ins);
889 #define DEBUG_ABORT_ON_ERROR 0x0001
890 #define DEBUG_INTERMEDIATE_CODE 0x0002
891 #define DEBUG_CONTROL_FLOW 0x0004
892 #define DEBUG_BASIC_BLOCKS 0x0008
893 #define DEBUG_FDOMINATORS 0x0010
894 #define DEBUG_RDOMINATORS 0x0020
895 #define DEBUG_TRIPLES 0x0040
896 #define DEBUG_INTERFERENCE 0x0080
897 #define DEBUG_ARCH_CODE 0x0100
898 #define DEBUG_CODE_ELIMINATION 0x0200
899 #define DEBUG_INSERTED_COPIES 0x0400
901 #define GLOBAL_SCOPE_DEPTH 1
903 static void compile_file(struct compile_state *old_state, const char *filename, int local);
905 static void do_cleanup(struct compile_state *state)
908 fclose(state->output);
909 unlink(state->ofilename);
913 static int get_col(struct file_state *file)
917 ptr = file->line_start;
919 for(col = 0; ptr < end; ptr++) {
924 col = (col & ~7) + 8;
930 static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
934 fprintf(fp, "%s:%d.%d: ",
935 triple->filename, triple->line, triple->col);
941 col = get_col(state->file);
942 fprintf(fp, "%s:%d.%d: ",
943 state->file->basename, state->file->line, col);
946 static void __internal_error(struct compile_state *state, struct triple *ptr,
951 loc(stderr, state, ptr);
953 fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
955 fprintf(stderr, "Internal compiler error: ");
956 vfprintf(stderr, fmt, args);
957 fprintf(stderr, "\n");
964 static void __internal_warning(struct compile_state *state, struct triple *ptr,
969 loc(stderr, state, ptr);
970 fprintf(stderr, "Internal compiler warning: ");
971 vfprintf(stderr, fmt, args);
972 fprintf(stderr, "\n");
978 static void __error(struct compile_state *state, struct triple *ptr,
983 loc(stderr, state, ptr);
984 vfprintf(stderr, fmt, args);
986 fprintf(stderr, "\n");
988 if (state->debug & DEBUG_ABORT_ON_ERROR) {
994 static void __warning(struct compile_state *state, struct triple *ptr,
999 loc(stderr, state, ptr);
1000 fprintf(stderr, "warning: ");
1001 vfprintf(stderr, fmt, args);
1002 fprintf(stderr, "\n");
1006 #if DEBUG_ERROR_MESSAGES
1007 # define internal_error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_error
1008 # define internal_warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_warning
1009 # define error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__error
1010 # define warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__warning
1012 # define internal_error __internal_error
1013 # define internal_warning __internal_warning
1014 # define error __error
1015 # define warning __warning
1017 #define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1019 static void valid_op(struct compile_state *state, int op)
1021 char *fmt = "invalid op: %d";
1023 internal_error(state, 0, fmt, op);
1026 internal_error(state, 0, fmt, op);
1030 static void valid_ins(struct compile_state *state, struct triple *ptr)
1032 valid_op(state, ptr->op);
1035 static void process_trigraphs(struct compile_state *state)
1037 char *src, *dest, *end;
1038 struct file_state *file;
1040 src = dest = file->buf;
1041 end = file->buf + file->size;
1042 while((end - src) >= 3) {
1043 if ((src[0] == '?') && (src[1] == '?')) {
1046 case '=': c = '#'; break;
1047 case '/': c = '\\'; break;
1048 case '\'': c = '^'; break;
1049 case '(': c = '['; break;
1050 case ')': c = ']'; break;
1051 case '!': c = '!'; break;
1052 case '<': c = '{'; break;
1053 case '>': c = '}'; break;
1054 case '-': c = '~'; break;
1071 file->size = dest - file->buf;
1074 static void splice_lines(struct compile_state *state)
1076 char *src, *dest, *end;
1077 struct file_state *file;
1079 src = dest = file->buf;
1080 end = file->buf + file->size;
1081 while((end - src) >= 2) {
1082 if ((src[0] == '\\') && (src[1] == '\n')) {
1092 file->size = dest - file->buf;
1095 static struct type void_type;
1096 static void use_triple(struct triple *used, struct triple *user)
1098 struct triple_set **ptr, *new;
1105 if ((*ptr)->member == user) {
1108 ptr = &(*ptr)->next;
1110 /* Append new to the head of the list,
1111 * copy_func and rename_block_variables
1114 new = xcmalloc(sizeof(*new), "triple_set");
1116 new->next = used->use;
1120 static void unuse_triple(struct triple *used, struct triple *unuser)
1122 struct triple_set *use, **ptr;
1129 if (use->member == unuser) {
1139 static void push_triple(struct triple *used, struct triple *user)
1141 struct triple_set *new;
1146 /* Append new to the head of the list,
1147 * it's the only sensible behavoir for a stack.
1149 new = xcmalloc(sizeof(*new), "triple_set");
1151 new->next = used->use;
1155 static void pop_triple(struct triple *used, struct triple *unuser)
1157 struct triple_set *use, **ptr;
1161 if (use->member == unuser) {
1164 /* Only free one occurance from the stack */
1174 /* The zero triple is used as a place holder when we are removing pointers
1175 * from a triple. Having allows certain sanity checks to pass even
1176 * when the original triple that was pointed to is gone.
1178 static struct triple zero_triple = {
1179 .next = &zero_triple,
1180 .prev = &zero_triple,
1183 .sizes = TRIPLE_SIZES(0, 0, 0, 0),
1184 .id = -1, /* An invalid id */
1185 .u = { .cval = 0, },
1186 .filename = __FILE__,
1189 .param { [0] = 0, [1] = 0, },
1193 static unsigned short triple_sizes(struct compile_state *state,
1194 int op, struct type *type, int lhs_wanted, int rhs_wanted)
1196 int lhs, rhs, misc, targ;
1197 valid_op(state, op);
1198 lhs = table_ops[op].lhs;
1199 rhs = table_ops[op].rhs;
1200 misc = table_ops[op].misc;
1201 targ = table_ops[op].targ;
1204 if (op == OP_CALL) {
1207 param = type->right;
1208 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
1210 param = param->right;
1212 if ((param->type & TYPE_MASK) != TYPE_VOID) {
1216 if ((type->left->type & TYPE_MASK) == TYPE_STRUCT) {
1217 lhs = type->left->elements;
1220 else if (op == OP_VAL_VEC) {
1221 rhs = type->elements;
1223 else if ((op == OP_BRANCH) || (op == OP_PHI)) {
1226 else if (op == OP_ASM) {
1230 if ((rhs < 0) || (rhs > MAX_RHS)) {
1231 internal_error(state, 0, "bad rhs");
1233 if ((lhs < 0) || (lhs > MAX_LHS)) {
1234 internal_error(state, 0, "bad lhs");
1236 if ((misc < 0) || (misc > MAX_MISC)) {
1237 internal_error(state, 0, "bad misc");
1239 if ((targ < 0) || (targ > MAX_TARG)) {
1240 internal_error(state, 0, "bad targs");
1242 return TRIPLE_SIZES(lhs, rhs, misc, targ);
1245 static struct triple *alloc_triple(struct compile_state *state,
1246 int op, struct type *type, int lhs, int rhs,
1247 const char *filename, int line, int col)
1249 size_t size, sizes, extra_count, min_count;
1251 sizes = triple_sizes(state, op, type, lhs, rhs);
1253 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
1254 extra_count = TRIPLE_SIZE(sizes);
1255 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
1257 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
1258 ret = xcmalloc(size, "tripple");
1264 ret->filename = filename;
1270 struct triple *dup_triple(struct compile_state *state, struct triple *src)
1273 int src_lhs, src_rhs, src_size;
1274 src_lhs = TRIPLE_LHS(src->sizes);
1275 src_rhs = TRIPLE_RHS(src->sizes);
1276 src_size = TRIPLE_SIZE(src->sizes);
1277 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
1278 src->filename, src->line, src->col);
1279 memcpy(dup, src, sizeof(*src));
1280 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
1284 static struct triple *new_triple(struct compile_state *state,
1285 int op, struct type *type, int lhs, int rhs)
1288 const char *filename;
1294 filename = state->file->basename;
1295 line = state->file->line;
1296 col = get_col(state->file);
1298 ret = alloc_triple(state, op, type, lhs, rhs,
1299 filename, line, col);
1303 static struct triple *build_triple(struct compile_state *state,
1304 int op, struct type *type, struct triple *left, struct triple *right,
1305 const char *filename, int line, int col)
1309 ret = alloc_triple(state, op, type, -1, -1, filename, line, col);
1310 count = TRIPLE_SIZE(ret->sizes);
1312 ret->param[0] = left;
1315 ret->param[1] = right;
1320 static struct triple *triple(struct compile_state *state,
1321 int op, struct type *type, struct triple *left, struct triple *right)
1325 ret = new_triple(state, op, type, -1, -1);
1326 count = TRIPLE_SIZE(ret->sizes);
1328 ret->param[0] = left;
1331 ret->param[1] = right;
1336 static struct triple *branch(struct compile_state *state,
1337 struct triple *targ, struct triple *test)
1340 ret = new_triple(state, OP_BRANCH, &void_type, -1, test?1:0);
1344 TARG(ret, 0) = targ;
1345 /* record the branch target was used */
1346 if (!targ || (targ->op != OP_LABEL)) {
1347 internal_error(state, 0, "branch not to label");
1348 use_triple(targ, ret);
1354 static void insert_triple(struct compile_state *state,
1355 struct triple *first, struct triple *ptr)
1358 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
1359 internal_error(state, ptr, "expression already used");
1362 ptr->prev = first->prev;
1363 ptr->prev->next = ptr;
1364 ptr->next->prev = ptr;
1365 if ((ptr->prev->op == OP_BRANCH) &&
1366 TRIPLE_RHS(ptr->prev->sizes)) {
1367 unuse_triple(first, ptr->prev);
1368 use_triple(ptr, ptr->prev);
1373 static int triple_stores_block(struct compile_state *state, struct triple *ins)
1375 /* This function is used to determine if u.block
1376 * is utilized to store the current block number.
1379 valid_ins(state, ins);
1380 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
1381 return stores_block;
1384 static struct block *block_of_triple(struct compile_state *state,
1387 struct triple *first;
1388 first = RHS(state->main_function, 0);
1389 while(ins != first && !triple_stores_block(state, ins)) {
1390 if (ins == ins->prev) {
1391 internal_error(state, 0, "ins == ins->prev?");
1395 if (!triple_stores_block(state, ins)) {
1396 internal_error(state, ins, "Cannot find block");
1398 return ins->u.block;
1401 static struct triple *pre_triple(struct compile_state *state,
1402 struct triple *base,
1403 int op, struct type *type, struct triple *left, struct triple *right)
1405 struct block *block;
1407 block = block_of_triple(state, base);
1408 ret = build_triple(state, op, type, left, right,
1409 base->filename, base->line, base->col);
1410 if (triple_stores_block(state, ret)) {
1411 ret->u.block = block;
1413 insert_triple(state, base, ret);
1414 if (block->first == base) {
1420 static struct triple *post_triple(struct compile_state *state,
1421 struct triple *base,
1422 int op, struct type *type, struct triple *left, struct triple *right)
1424 struct block *block;
1426 block = block_of_triple(state, base);
1427 ret = build_triple(state, op, type, left, right,
1428 base->filename, base->line, base->col);
1429 if (triple_stores_block(state, ret)) {
1430 ret->u.block = block;
1432 insert_triple(state, base->next, ret);
1433 if (block->last == base) {
1439 static struct triple *label(struct compile_state *state)
1441 /* Labels don't get a type */
1442 struct triple *result;
1443 result = triple(state, OP_LABEL, &void_type, 0, 0);
1447 static void display_triple(FILE *fp, struct triple *ins)
1449 if (ins->op == OP_INTCONST) {
1450 fprintf(fp, "(%p) %3d %-2d %-10s <0x%08lx> @ %s:%d.%d\n",
1451 ins, ID_REG(ins->id), ins->template_id, tops(ins->op),
1453 ins->filename, ins->line, ins->col);
1455 else if (ins->op == OP_ADDRCONST) {
1456 fprintf(fp, "(%p) %3d %-2d %-10s %-10p <0x%08lx> @ %s:%d.%d\n",
1457 ins, ID_REG(ins->id), ins->template_id, tops(ins->op),
1458 MISC(ins, 0), ins->u.cval,
1459 ins->filename, ins->line, ins->col);
1463 fprintf(fp, "(%p) %3d %-2d %-10s",
1464 ins, ID_REG(ins->id), ins->template_id, tops(ins->op));
1465 count = TRIPLE_SIZE(ins->sizes);
1466 for(i = 0; i < count; i++) {
1467 fprintf(fp, " %-10p", ins->param[i]);
1472 fprintf(fp, " @ %s:%d.%d\n",
1473 ins->filename, ins->line, ins->col);
1478 static int triple_is_pure(struct compile_state *state, struct triple *ins)
1480 /* Does the triple have no side effects.
1481 * I.e. Rexecuting the triple with the same arguments
1482 * gives the same value.
1485 valid_ins(state, ins);
1486 pure = PURE_BITS(table_ops[ins->op].flags);
1487 if ((pure != PURE) && (pure != IMPURE)) {
1488 internal_error(state, 0, "Purity of %s not known\n",
1491 return pure == PURE;
1494 static int triple_is_branch(struct compile_state *state, struct triple *ins)
1496 /* This function is used to determine which triples need
1500 valid_ins(state, ins);
1501 is_branch = (table_ops[ins->op].targ != 0);
1505 static int triple_is_def(struct compile_state *state, struct triple *ins)
1507 /* This function is used to determine which triples need
1511 valid_ins(state, ins);
1512 is_def = (table_ops[ins->op].flags & DEF) == DEF;
1516 static struct triple **triple_iter(struct compile_state *state,
1517 size_t count, struct triple **vector,
1518 struct triple *ins, struct triple **last)
1520 struct triple **ret;
1526 else if ((last >= vector) && (last < (vector + count - 1))) {
1534 static struct triple **triple_lhs(struct compile_state *state,
1535 struct triple *ins, struct triple **last)
1537 return triple_iter(state, TRIPLE_LHS(ins->sizes), &LHS(ins,0),
1541 static struct triple **triple_rhs(struct compile_state *state,
1542 struct triple *ins, struct triple **last)
1544 return triple_iter(state, TRIPLE_RHS(ins->sizes), &RHS(ins,0),
1548 static struct triple **triple_misc(struct compile_state *state,
1549 struct triple *ins, struct triple **last)
1551 return triple_iter(state, TRIPLE_MISC(ins->sizes), &MISC(ins,0),
1555 static struct triple **triple_targ(struct compile_state *state,
1556 struct triple *ins, struct triple **last)
1559 struct triple **ret, **vector;
1561 count = TRIPLE_TARG(ins->sizes);
1562 vector = &TARG(ins, 0);
1567 else if ((last >= vector) && (last < (vector + count - 1))) {
1570 else if ((last == (vector + count - 1)) &&
1571 TRIPLE_RHS(ins->sizes)) {
1579 static void verify_use(struct compile_state *state,
1580 struct triple *user, struct triple *used)
1583 size = TRIPLE_SIZE(user->sizes);
1584 for(i = 0; i < size; i++) {
1585 if (user->param[i] == used) {
1589 if (triple_is_branch(state, user)) {
1590 if (user->next == used) {
1595 internal_error(state, user, "%s(%p) does not use %s(%p)",
1596 tops(user->op), user, tops(used->op), used);
1600 static int find_rhs_use(struct compile_state *state,
1601 struct triple *user, struct triple *used)
1603 struct triple **param;
1605 verify_use(state, user, used);
1606 size = TRIPLE_RHS(user->sizes);
1607 param = &RHS(user, 0);
1608 for(i = 0; i < size; i++) {
1609 if (param[i] == used) {
1616 static void free_triple(struct compile_state *state, struct triple *ptr)
1619 size = sizeof(*ptr) - sizeof(ptr->param) +
1620 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr->sizes));
1621 ptr->prev->next = ptr->next;
1622 ptr->next->prev = ptr->prev;
1624 internal_error(state, ptr, "ptr->use != 0");
1626 memset(ptr, -1, size);
1630 static void release_triple(struct compile_state *state, struct triple *ptr)
1632 struct triple_set *set, *next;
1633 struct triple **expr;
1634 /* Remove ptr from use chains where it is the user */
1635 expr = triple_rhs(state, ptr, 0);
1636 for(; expr; expr = triple_rhs(state, ptr, expr)) {
1638 unuse_triple(*expr, ptr);
1641 expr = triple_lhs(state, ptr, 0);
1642 for(; expr; expr = triple_lhs(state, ptr, expr)) {
1644 unuse_triple(*expr, ptr);
1647 expr = triple_misc(state, ptr, 0);
1648 for(; expr; expr = triple_misc(state, ptr, expr)) {
1650 unuse_triple(*expr, ptr);
1653 expr = triple_targ(state, ptr, 0);
1654 for(; expr; expr = triple_targ(state, ptr, expr)) {
1656 unuse_triple(*expr, ptr);
1659 /* Reomve ptr from use chains where it is used */
1660 for(set = ptr->use; set; set = next) {
1662 expr = triple_rhs(state, set->member, 0);
1663 for(; expr; expr = triple_rhs(state, set->member, expr)) {
1665 *expr = &zero_triple;
1668 expr = triple_lhs(state, set->member, 0);
1669 for(; expr; expr = triple_lhs(state, set->member, expr)) {
1671 *expr = &zero_triple;
1674 expr = triple_misc(state, set->member, 0);
1675 for(; expr; expr = triple_misc(state, set->member, expr)) {
1677 *expr = &zero_triple;
1680 expr = triple_targ(state, set->member, 0);
1681 for(; expr; expr = triple_targ(state, set->member, expr)) {
1683 *expr = &zero_triple;
1686 unuse_triple(ptr, set->member);
1688 free_triple(state, ptr);
1691 static void print_triple(struct compile_state *state, struct triple *ptr);
1693 #define TOK_UNKNOWN 0
1696 #define TOK_LBRACE 3
1697 #define TOK_RBRACE 4
1701 #define TOK_LBRACKET 8
1702 #define TOK_RBRACKET 9
1703 #define TOK_LPAREN 10
1704 #define TOK_RPAREN 11
1709 #define TOK_TIMESEQ 16
1710 #define TOK_DIVEQ 17
1711 #define TOK_MODEQ 18
1712 #define TOK_PLUSEQ 19
1713 #define TOK_MINUSEQ 20
1716 #define TOK_ANDEQ 23
1717 #define TOK_XOREQ 24
1720 #define TOK_NOTEQ 27
1721 #define TOK_QUEST 28
1722 #define TOK_LOGOR 29
1723 #define TOK_LOGAND 30
1727 #define TOK_LESSEQ 34
1728 #define TOK_MOREEQ 35
1732 #define TOK_MINUS 39
1735 #define TOK_PLUSPLUS 42
1736 #define TOK_MINUSMINUS 43
1738 #define TOK_ARROW 45
1740 #define TOK_TILDE 47
1741 #define TOK_LIT_STRING 48
1742 #define TOK_LIT_CHAR 49
1743 #define TOK_LIT_INT 50
1744 #define TOK_LIT_FLOAT 51
1745 #define TOK_MACRO 52
1746 #define TOK_CONCATENATE 53
1748 #define TOK_IDENT 54
1749 #define TOK_STRUCT_NAME 55
1750 #define TOK_ENUM_CONST 56
1751 #define TOK_TYPE_NAME 57
1754 #define TOK_BREAK 59
1757 #define TOK_CONST 62
1758 #define TOK_CONTINUE 63
1759 #define TOK_DEFAULT 64
1761 #define TOK_DOUBLE 66
1764 #define TOK_EXTERN 69
1765 #define TOK_FLOAT 70
1769 #define TOK_INLINE 74
1772 #define TOK_REGISTER 77
1773 #define TOK_RESTRICT 78
1774 #define TOK_RETURN 79
1775 #define TOK_SHORT 80
1776 #define TOK_SIGNED 81
1777 #define TOK_SIZEOF 82
1778 #define TOK_STATIC 83
1779 #define TOK_STRUCT 84
1780 #define TOK_SWITCH 85
1781 #define TOK_TYPEDEF 86
1782 #define TOK_UNION 87
1783 #define TOK_UNSIGNED 88
1785 #define TOK_VOLATILE 90
1786 #define TOK_WHILE 91
1788 #define TOK_ATTRIBUTE 93
1789 #define TOK_ALIGNOF 94
1790 #define TOK_FIRST_KEYWORD TOK_AUTO
1791 #define TOK_LAST_KEYWORD TOK_ALIGNOF
1793 #define TOK_DEFINE 100
1794 #define TOK_UNDEF 101
1795 #define TOK_INCLUDE 102
1796 #define TOK_LINE 103
1797 #define TOK_ERROR 104
1798 #define TOK_WARNING 105
1799 #define TOK_PRAGMA 106
1800 #define TOK_IFDEF 107
1801 #define TOK_IFNDEF 108
1802 #define TOK_ELIF 109
1803 #define TOK_ENDIF 110
1805 #define TOK_FIRST_MACRO TOK_DEFINE
1806 #define TOK_LAST_MACRO TOK_ENDIF
1810 static const char *tokens[] = {
1811 [TOK_UNKNOWN ] = "unknown",
1812 [TOK_SPACE ] = ":space:",
1814 [TOK_LBRACE ] = "{",
1815 [TOK_RBRACE ] = "}",
1819 [TOK_LBRACKET ] = "[",
1820 [TOK_RBRACKET ] = "]",
1821 [TOK_LPAREN ] = "(",
1822 [TOK_RPAREN ] = ")",
1824 [TOK_DOTS ] = "...",
1827 [TOK_TIMESEQ ] = "*=",
1828 [TOK_DIVEQ ] = "/=",
1829 [TOK_MODEQ ] = "%=",
1830 [TOK_PLUSEQ ] = "+=",
1831 [TOK_MINUSEQ ] = "-=",
1832 [TOK_SLEQ ] = "<<=",
1833 [TOK_SREQ ] = ">>=",
1834 [TOK_ANDEQ ] = "&=",
1835 [TOK_XOREQ ] = "^=",
1838 [TOK_NOTEQ ] = "!=",
1840 [TOK_LOGOR ] = "||",
1841 [TOK_LOGAND ] = "&&",
1845 [TOK_LESSEQ ] = "<=",
1846 [TOK_MOREEQ ] = ">=",
1853 [TOK_PLUSPLUS ] = "++",
1854 [TOK_MINUSMINUS ] = "--",
1856 [TOK_ARROW ] = "->",
1859 [TOK_LIT_STRING ] = ":string:",
1860 [TOK_IDENT ] = ":ident:",
1861 [TOK_TYPE_NAME ] = ":typename:",
1862 [TOK_LIT_CHAR ] = ":char:",
1863 [TOK_LIT_INT ] = ":integer:",
1864 [TOK_LIT_FLOAT ] = ":float:",
1866 [TOK_CONCATENATE ] = "##",
1868 [TOK_AUTO ] = "auto",
1869 [TOK_BREAK ] = "break",
1870 [TOK_CASE ] = "case",
1871 [TOK_CHAR ] = "char",
1872 [TOK_CONST ] = "const",
1873 [TOK_CONTINUE ] = "continue",
1874 [TOK_DEFAULT ] = "default",
1876 [TOK_DOUBLE ] = "double",
1877 [TOK_ELSE ] = "else",
1878 [TOK_ENUM ] = "enum",
1879 [TOK_EXTERN ] = "extern",
1880 [TOK_FLOAT ] = "float",
1882 [TOK_GOTO ] = "goto",
1884 [TOK_INLINE ] = "inline",
1886 [TOK_LONG ] = "long",
1887 [TOK_REGISTER ] = "register",
1888 [TOK_RESTRICT ] = "restrict",
1889 [TOK_RETURN ] = "return",
1890 [TOK_SHORT ] = "short",
1891 [TOK_SIGNED ] = "signed",
1892 [TOK_SIZEOF ] = "sizeof",
1893 [TOK_STATIC ] = "static",
1894 [TOK_STRUCT ] = "struct",
1895 [TOK_SWITCH ] = "switch",
1896 [TOK_TYPEDEF ] = "typedef",
1897 [TOK_UNION ] = "union",
1898 [TOK_UNSIGNED ] = "unsigned",
1899 [TOK_VOID ] = "void",
1900 [TOK_VOLATILE ] = "volatile",
1901 [TOK_WHILE ] = "while",
1903 [TOK_ATTRIBUTE ] = "__attribute__",
1904 [TOK_ALIGNOF ] = "__alignof__",
1906 [TOK_DEFINE ] = "define",
1907 [TOK_UNDEF ] = "undef",
1908 [TOK_INCLUDE ] = "include",
1909 [TOK_LINE ] = "line",
1910 [TOK_ERROR ] = "error",
1911 [TOK_WARNING ] = "warning",
1912 [TOK_PRAGMA ] = "pragma",
1913 [TOK_IFDEF ] = "ifdef",
1914 [TOK_IFNDEF ] = "ifndef",
1915 [TOK_ELIF ] = "elif",
1916 [TOK_ENDIF ] = "endif",
1921 static unsigned int hash(const char *str, int str_len)
1925 end = str + str_len;
1927 for(; str < end; str++) {
1928 hash = (hash *263) + *str;
1930 hash = hash & (HASH_TABLE_SIZE -1);
1934 static struct hash_entry *lookup(
1935 struct compile_state *state, const char *name, int name_len)
1937 struct hash_entry *entry;
1939 index = hash(name, name_len);
1940 entry = state->hash_table[index];
1942 ((entry->name_len != name_len) ||
1943 (memcmp(entry->name, name, name_len) != 0))) {
1944 entry = entry->next;
1948 /* Get a private copy of the name */
1949 new_name = xmalloc(name_len + 1, "hash_name");
1950 memcpy(new_name, name, name_len);
1951 new_name[name_len] = '\0';
1953 /* Create a new hash entry */
1954 entry = xcmalloc(sizeof(*entry), "hash_entry");
1955 entry->next = state->hash_table[index];
1956 entry->name = new_name;
1957 entry->name_len = name_len;
1959 /* Place the new entry in the hash table */
1960 state->hash_table[index] = entry;
1965 static void ident_to_keyword(struct compile_state *state, struct token *tk)
1967 struct hash_entry *entry;
1969 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
1970 (entry->tok == TOK_ENUM_CONST) ||
1971 ((entry->tok >= TOK_FIRST_KEYWORD) &&
1972 (entry->tok <= TOK_LAST_KEYWORD)))) {
1973 tk->tok = entry->tok;
1977 static void ident_to_macro(struct compile_state *state, struct token *tk)
1979 struct hash_entry *entry;
1982 (entry->tok >= TOK_FIRST_MACRO) &&
1983 (entry->tok <= TOK_LAST_MACRO)) {
1984 tk->tok = entry->tok;
1988 static void hash_keyword(
1989 struct compile_state *state, const char *keyword, int tok)
1991 struct hash_entry *entry;
1992 entry = lookup(state, keyword, strlen(keyword));
1993 if (entry && entry->tok != TOK_UNKNOWN) {
1994 die("keyword %s already hashed", keyword);
2000 struct compile_state *state, struct hash_entry *ident,
2001 struct symbol **chain, struct triple *def, struct type *type)
2004 if (*chain && ((*chain)->scope_depth == state->scope_depth)) {
2005 error(state, 0, "%s already defined", ident->name);
2007 sym = xcmalloc(sizeof(*sym), "symbol");
2011 sym->scope_depth = state->scope_depth;
2016 static void start_scope(struct compile_state *state)
2018 state->scope_depth++;
2021 static void end_scope_syms(struct symbol **chain, int depth)
2023 struct symbol *sym, *next;
2025 while(sym && (sym->scope_depth == depth)) {
2033 static void end_scope(struct compile_state *state)
2037 /* Walk through the hash table and remove all symbols
2038 * in the current scope.
2040 depth = state->scope_depth;
2041 for(i = 0; i < HASH_TABLE_SIZE; i++) {
2042 struct hash_entry *entry;
2043 entry = state->hash_table[i];
2045 end_scope_syms(&entry->sym_label, depth);
2046 end_scope_syms(&entry->sym_struct, depth);
2047 end_scope_syms(&entry->sym_ident, depth);
2048 entry = entry->next;
2051 state->scope_depth = depth - 1;
2054 static void register_keywords(struct compile_state *state)
2056 hash_keyword(state, "auto", TOK_AUTO);
2057 hash_keyword(state, "break", TOK_BREAK);
2058 hash_keyword(state, "case", TOK_CASE);
2059 hash_keyword(state, "char", TOK_CHAR);
2060 hash_keyword(state, "const", TOK_CONST);
2061 hash_keyword(state, "continue", TOK_CONTINUE);
2062 hash_keyword(state, "default", TOK_DEFAULT);
2063 hash_keyword(state, "do", TOK_DO);
2064 hash_keyword(state, "double", TOK_DOUBLE);
2065 hash_keyword(state, "else", TOK_ELSE);
2066 hash_keyword(state, "enum", TOK_ENUM);
2067 hash_keyword(state, "extern", TOK_EXTERN);
2068 hash_keyword(state, "float", TOK_FLOAT);
2069 hash_keyword(state, "for", TOK_FOR);
2070 hash_keyword(state, "goto", TOK_GOTO);
2071 hash_keyword(state, "if", TOK_IF);
2072 hash_keyword(state, "inline", TOK_INLINE);
2073 hash_keyword(state, "int", TOK_INT);
2074 hash_keyword(state, "long", TOK_LONG);
2075 hash_keyword(state, "register", TOK_REGISTER);
2076 hash_keyword(state, "restrict", TOK_RESTRICT);
2077 hash_keyword(state, "return", TOK_RETURN);
2078 hash_keyword(state, "short", TOK_SHORT);
2079 hash_keyword(state, "signed", TOK_SIGNED);
2080 hash_keyword(state, "sizeof", TOK_SIZEOF);
2081 hash_keyword(state, "static", TOK_STATIC);
2082 hash_keyword(state, "struct", TOK_STRUCT);
2083 hash_keyword(state, "switch", TOK_SWITCH);
2084 hash_keyword(state, "typedef", TOK_TYPEDEF);
2085 hash_keyword(state, "union", TOK_UNION);
2086 hash_keyword(state, "unsigned", TOK_UNSIGNED);
2087 hash_keyword(state, "void", TOK_VOID);
2088 hash_keyword(state, "volatile", TOK_VOLATILE);
2089 hash_keyword(state, "__volatile__", TOK_VOLATILE);
2090 hash_keyword(state, "while", TOK_WHILE);
2091 hash_keyword(state, "asm", TOK_ASM);
2092 hash_keyword(state, "__asm__", TOK_ASM);
2093 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
2094 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
2097 static void register_macro_keywords(struct compile_state *state)
2099 hash_keyword(state, "define", TOK_DEFINE);
2100 hash_keyword(state, "undef", TOK_UNDEF);
2101 hash_keyword(state, "include", TOK_INCLUDE);
2102 hash_keyword(state, "line", TOK_LINE);
2103 hash_keyword(state, "error", TOK_ERROR);
2104 hash_keyword(state, "warning", TOK_WARNING);
2105 hash_keyword(state, "pragma", TOK_PRAGMA);
2106 hash_keyword(state, "ifdef", TOK_IFDEF);
2107 hash_keyword(state, "ifndef", TOK_IFNDEF);
2108 hash_keyword(state, "elif", TOK_ELIF);
2109 hash_keyword(state, "endif", TOK_ENDIF);
2112 static int spacep(int c)
2128 static int digitp(int c)
2132 case '0': case '1': case '2': case '3': case '4':
2133 case '5': case '6': case '7': case '8': case '9':
2140 static int hexdigitp(int c)
2144 case '0': case '1': case '2': case '3': case '4':
2145 case '5': case '6': case '7': case '8': case '9':
2146 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2147 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2153 static int hexdigval(int c)
2156 if ((c >= '0') && (c <= '9')) {
2159 else if ((c >= 'A') && (c <= 'F')) {
2160 val = 10 + (c - 'A');
2162 else if ((c >= 'a') && (c <= 'f')) {
2163 val = 10 + (c - 'a');
2168 static int octdigitp(int c)
2172 case '0': case '1': case '2': case '3':
2173 case '4': case '5': case '6': case '7':
2179 static int octdigval(int c)
2182 if ((c >= '0') && (c <= '7')) {
2188 static int letterp(int c)
2192 case 'a': case 'b': case 'c': case 'd': case 'e':
2193 case 'f': case 'g': case 'h': case 'i': case 'j':
2194 case 'k': case 'l': case 'm': case 'n': case 'o':
2195 case 'p': case 'q': case 'r': case 's': case 't':
2196 case 'u': case 'v': case 'w': case 'x': case 'y':
2198 case 'A': case 'B': case 'C': case 'D': case 'E':
2199 case 'F': case 'G': case 'H': case 'I': case 'J':
2200 case 'K': case 'L': case 'M': case 'N': case 'O':
2201 case 'P': case 'Q': case 'R': case 'S': case 'T':
2202 case 'U': case 'V': case 'W': case 'X': case 'Y':
2211 static int char_value(struct compile_state *state,
2212 const signed char **strp, const signed char *end)
2214 const signed char *str;
2218 if ((c == '\\') && (str < end)) {
2220 case 'n': c = '\n'; str++; break;
2221 case 't': c = '\t'; str++; break;
2222 case 'v': c = '\v'; str++; break;
2223 case 'b': c = '\b'; str++; break;
2224 case 'r': c = '\r'; str++; break;
2225 case 'f': c = '\f'; str++; break;
2226 case 'a': c = '\a'; str++; break;
2227 case '\\': c = '\\'; str++; break;
2228 case '?': c = '?'; str++; break;
2229 case '\'': c = '\''; str++; break;
2230 case '"': c = '"'; break;
2234 while((str < end) && hexdigitp(*str)) {
2236 c += hexdigval(*str);
2240 case '0': case '1': case '2': case '3':
2241 case '4': case '5': case '6': case '7':
2243 while((str < end) && octdigitp(*str)) {
2245 c += octdigval(*str);
2250 error(state, 0, "Invalid character constant");
2258 static char *after_digits(char *ptr, char *end)
2260 while((ptr < end) && digitp(*ptr)) {
2266 static char *after_octdigits(char *ptr, char *end)
2268 while((ptr < end) && octdigitp(*ptr)) {
2274 static char *after_hexdigits(char *ptr, char *end)
2276 while((ptr < end) && hexdigitp(*ptr)) {
2282 static void save_string(struct compile_state *state,
2283 struct token *tk, char *start, char *end, const char *id)
2287 /* Create a private copy of the string */
2288 str_len = end - start + 1;
2289 str = xmalloc(str_len + 1, id);
2290 memcpy(str, start, str_len);
2291 str[str_len] = '\0';
2293 /* Store the copy in the token */
2295 tk->str_len = str_len;
2297 static void next_token(struct compile_state *state, int index)
2299 struct file_state *file;
2307 tk = &state->token[index];
2310 token = tokp = file->pos;
2311 end = file->buf + file->size;
2318 if ((tokp + 1) < end) {
2322 if ((tokp + 2) < end) {
2326 if ((tokp + 3) < end) {
2334 else if (spacep(c)) {
2336 while ((tokp < end) && spacep(c)) {
2339 file->line_start = tokp + 1;
2348 else if ((c == '/') && (c1 == '/')) {
2350 for(tokp += 2; tokp < end; tokp++) {
2354 file->line_start = tokp +1;
2360 else if ((c == '/') && (c1 == '*')) {
2364 line_start = file->line_start;
2365 for(tokp += 2; (end - tokp) >= 2; tokp++) {
2369 line_start = tokp +1;
2371 else if ((c == '*') && (tokp[1] == '/')) {
2377 if (tok == TOK_UNKNOWN) {
2378 error(state, 0, "unterminated comment");
2381 file->line_start = line_start;
2383 /* string constants */
2384 else if ((c == '"') ||
2385 ((c == 'L') && (c1 == '"'))) {
2390 line_start = file->line_start;
2396 for(tokp += 1; tokp < end; tokp++) {
2400 line_start = tokp + 1;
2402 else if ((c == '\\') && (tokp +1 < end)) {
2405 else if (c == '"') {
2406 tok = TOK_LIT_STRING;
2410 if (tok == TOK_UNKNOWN) {
2411 error(state, 0, "unterminated string constant");
2413 if (line != file->line) {
2414 warning(state, 0, "multiline string constant");
2417 file->line_start = line_start;
2419 /* Save the string value */
2420 save_string(state, tk, token, tokp, "literal string");
2422 /* character constants */
2423 else if ((c == '\'') ||
2424 ((c == 'L') && (c1 == '\''))) {
2429 line_start = file->line_start;
2435 for(tokp += 1; tokp < end; tokp++) {
2439 line_start = tokp + 1;
2441 else if ((c == '\\') && (tokp +1 < end)) {
2444 else if (c == '\'') {
2449 if (tok == TOK_UNKNOWN) {
2450 error(state, 0, "unterminated character constant");
2452 if (line != file->line) {
2453 warning(state, 0, "multiline character constant");
2456 file->line_start = line_start;
2458 /* Save the character value */
2459 save_string(state, tk, token, tokp, "literal character");
2461 /* integer and floating constants
2467 * Floating constants
2468 * {digits}.{digits}[Ee][+-]?{digits}
2470 * {digits}[Ee][+-]?{digits}
2471 * .{digits}[Ee][+-]?{digits}
2475 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
2480 next = after_digits(tokp, end);
2485 if (next[0] == '.') {
2486 new = after_digits(next, end);
2487 is_float = (new != next);
2490 if ((next[0] == 'e') || (next[0] == 'E')) {
2491 if (((next + 1) < end) &&
2492 ((next[1] == '+') || (next[1] == '-'))) {
2495 new = after_digits(next, end);
2496 is_float = (new != next);
2500 tok = TOK_LIT_FLOAT;
2501 if ((next < end) && (
2510 if (!is_float && digitp(c)) {
2512 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
2513 next = after_hexdigits(tokp + 2, end);
2515 else if (c == '0') {
2516 next = after_octdigits(tokp, end);
2519 next = after_digits(tokp, end);
2521 /* crazy integer suffixes */
2523 ((next[0] == 'u') || (next[0] == 'U'))) {
2526 ((next[0] == 'l') || (next[0] == 'L'))) {
2530 else if ((next < end) &&
2531 ((next[0] == 'l') || (next[0] == 'L'))) {
2534 ((next[0] == 'u') || (next[0] == 'U'))) {
2541 /* Save the integer/floating point value */
2542 save_string(state, tk, token, tokp, "literal number");
2545 else if (letterp(c)) {
2547 for(tokp += 1; tokp < end; tokp++) {
2549 if (!letterp(c) && !digitp(c)) {
2554 tk->ident = lookup(state, token, tokp +1 - token);
2556 /* C99 alternate macro characters */
2557 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
2559 tok = TOK_CONCATENATE;
2561 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { tokp += 2; tok = TOK_DOTS; }
2562 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { tokp += 2; tok = TOK_SLEQ; }
2563 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { tokp += 2; tok = TOK_SREQ; }
2564 else if ((c == '*') && (c1 == '=')) { tokp += 1; tok = TOK_TIMESEQ; }
2565 else if ((c == '/') && (c1 == '=')) { tokp += 1; tok = TOK_DIVEQ; }
2566 else if ((c == '%') && (c1 == '=')) { tokp += 1; tok = TOK_MODEQ; }
2567 else if ((c == '+') && (c1 == '=')) { tokp += 1; tok = TOK_PLUSEQ; }
2568 else if ((c == '-') && (c1 == '=')) { tokp += 1; tok = TOK_MINUSEQ; }
2569 else if ((c == '&') && (c1 == '=')) { tokp += 1; tok = TOK_ANDEQ; }
2570 else if ((c == '^') && (c1 == '=')) { tokp += 1; tok = TOK_XOREQ; }
2571 else if ((c == '|') && (c1 == '=')) { tokp += 1; tok = TOK_OREQ; }
2572 else if ((c == '=') && (c1 == '=')) { tokp += 1; tok = TOK_EQEQ; }
2573 else if ((c == '!') && (c1 == '=')) { tokp += 1; tok = TOK_NOTEQ; }
2574 else if ((c == '|') && (c1 == '|')) { tokp += 1; tok = TOK_LOGOR; }
2575 else if ((c == '&') && (c1 == '&')) { tokp += 1; tok = TOK_LOGAND; }
2576 else if ((c == '<') && (c1 == '=')) { tokp += 1; tok = TOK_LESSEQ; }
2577 else if ((c == '>') && (c1 == '=')) { tokp += 1; tok = TOK_MOREEQ; }
2578 else if ((c == '<') && (c1 == '<')) { tokp += 1; tok = TOK_SL; }
2579 else if ((c == '>') && (c1 == '>')) { tokp += 1; tok = TOK_SR; }
2580 else if ((c == '+') && (c1 == '+')) { tokp += 1; tok = TOK_PLUSPLUS; }
2581 else if ((c == '-') && (c1 == '-')) { tokp += 1; tok = TOK_MINUSMINUS; }
2582 else if ((c == '-') && (c1 == '>')) { tokp += 1; tok = TOK_ARROW; }
2583 else if ((c == '<') && (c1 == ':')) { tokp += 1; tok = TOK_LBRACKET; }
2584 else if ((c == ':') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACKET; }
2585 else if ((c == '<') && (c1 == '%')) { tokp += 1; tok = TOK_LBRACE; }
2586 else if ((c == '%') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACE; }
2587 else if ((c == '%') && (c1 == ':')) { tokp += 1; tok = TOK_MACRO; }
2588 else if ((c == '#') && (c1 == '#')) { tokp += 1; tok = TOK_CONCATENATE; }
2589 else if (c == ';') { tok = TOK_SEMI; }
2590 else if (c == '{') { tok = TOK_LBRACE; }
2591 else if (c == '}') { tok = TOK_RBRACE; }
2592 else if (c == ',') { tok = TOK_COMMA; }
2593 else if (c == '=') { tok = TOK_EQ; }
2594 else if (c == ':') { tok = TOK_COLON; }
2595 else if (c == '[') { tok = TOK_LBRACKET; }
2596 else if (c == ']') { tok = TOK_RBRACKET; }
2597 else if (c == '(') { tok = TOK_LPAREN; }
2598 else if (c == ')') { tok = TOK_RPAREN; }
2599 else if (c == '*') { tok = TOK_STAR; }
2600 else if (c == '>') { tok = TOK_MORE; }
2601 else if (c == '<') { tok = TOK_LESS; }
2602 else if (c == '?') { tok = TOK_QUEST; }
2603 else if (c == '|') { tok = TOK_OR; }
2604 else if (c == '&') { tok = TOK_AND; }
2605 else if (c == '^') { tok = TOK_XOR; }
2606 else if (c == '+') { tok = TOK_PLUS; }
2607 else if (c == '-') { tok = TOK_MINUS; }
2608 else if (c == '/') { tok = TOK_DIV; }
2609 else if (c == '%') { tok = TOK_MOD; }
2610 else if (c == '!') { tok = TOK_BANG; }
2611 else if (c == '.') { tok = TOK_DOT; }
2612 else if (c == '~') { tok = TOK_TILDE; }
2613 else if (c == '#') { tok = TOK_MACRO; }
2614 if (tok == TOK_MACRO) {
2615 /* Only match preprocessor directives at the start of a line */
2617 for(ptr = file->line_start; spacep(*ptr); ptr++)
2623 if (tok == TOK_UNKNOWN) {
2624 error(state, 0, "unknown token");
2627 file->pos = tokp + 1;
2629 if (tok == TOK_IDENT) {
2630 ident_to_keyword(state, tk);
2632 /* Don't return space tokens. */
2633 if (tok == TOK_SPACE) {
2638 static void compile_macro(struct compile_state *state, struct token *tk)
2640 struct file_state *file;
2641 struct hash_entry *ident;
2643 file = xmalloc(sizeof(*file), "file_state");
2644 file->basename = xstrdup(tk->ident->name);
2645 file->dirname = xstrdup("");
2646 file->size = ident->sym_define->buf_len;
2647 file->buf = xmalloc(file->size +2, file->basename);
2648 memcpy(file->buf, ident->sym_define->buf, file->size);
2649 file->buf[file->size] = '\n';
2650 file->buf[file->size + 1] = '\0';
2651 file->pos = file->buf;
2652 file->line_start = file->pos;
2654 file->prev = state->file;
2659 static int mpeek(struct compile_state *state, int index)
2663 tk = &state->token[index + 1];
2664 if (tk->tok == -1) {
2665 next_token(state, index + 1);
2669 if ((tk->tok == TOK_EOF) &&
2670 (state->file != state->macro_file) &&
2671 (state->file->prev)) {
2672 struct file_state *file = state->file;
2673 state->file = file->prev;
2674 /* file->basename is used keep it */
2675 xfree(file->dirname);
2678 next_token(state, index + 1);
2681 else if (tk->ident && tk->ident->sym_define) {
2682 compile_macro(state, tk);
2683 next_token(state, index + 1);
2687 /* Don't show the token on the next line */
2688 if (state->macro_line < state->macro_file->line) {
2691 return state->token[index +1].tok;
2694 static void meat(struct compile_state *state, int index, int tok)
2698 next_tok = mpeek(state, index);
2699 if (next_tok != tok) {
2700 const char *name1, *name2;
2701 name1 = tokens[next_tok];
2703 if (next_tok == TOK_IDENT) {
2704 name2 = state->token[index + 1].ident->name;
2706 error(state, 0, "found %s %s expected %s",
2707 name1, name2, tokens[tok]);
2709 /* Free the old token value */
2710 if (state->token[index].str_len) {
2711 memset((void *)(state->token[index].val.str), -1,
2712 state->token[index].str_len);
2713 xfree(state->token[index].val.str);
2715 for(i = index; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
2716 state->token[i] = state->token[i + 1];
2718 memset(&state->token[i], 0, sizeof(state->token[i]));
2719 state->token[i].tok = -1;
2722 static long_t mcexpr(struct compile_state *state, int index);
2724 static long_t mprimary_expr(struct compile_state *state, int index)
2728 tok = mpeek(state, index);
2729 while(state->token[index + 1].ident &&
2730 state->token[index + 1].ident->sym_define) {
2731 meat(state, index, tok);
2732 compile_macro(state, &state->token[index]);
2733 tok = mpeek(state, index);
2737 meat(state, index, TOK_LPAREN);
2738 val = mcexpr(state, index);
2739 meat(state, index, TOK_RPAREN);
2744 meat(state, index, TOK_LIT_INT);
2746 val = strtol(state->token[index].val.str, &end, 0);
2747 if (((val == LONG_MIN) || (val == LONG_MAX)) &&
2748 (errno == ERANGE)) {
2749 error(state, 0, "Integer constant to large");
2754 meat(state, index, TOK_LIT_INT);
2759 static long_t munary_expr(struct compile_state *state, int index)
2762 switch(mpeek(state, index)) {
2764 meat(state, index, TOK_PLUS);
2765 val = munary_expr(state, index);
2769 meat(state, index, TOK_MINUS);
2770 val = munary_expr(state, index);
2774 meat(state, index, TOK_BANG);
2775 val = munary_expr(state, index);
2779 meat(state, index, TOK_BANG);
2780 val = munary_expr(state, index);
2784 val = mprimary_expr(state, index);
2790 static long_t mmul_expr(struct compile_state *state, int index)
2794 val = munary_expr(state, index);
2798 switch(mpeek(state, index)) {
2800 meat(state, index, TOK_STAR);
2801 right = munary_expr(state, index);
2805 meat(state, index, TOK_DIV);
2806 right = munary_expr(state, index);
2810 meat(state, index, TOK_MOD);
2811 right = munary_expr(state, index);
2823 static long_t madd_expr(struct compile_state *state, int index)
2827 val = mmul_expr(state, index);
2831 switch(mpeek(state, index)) {
2833 meat(state, index, TOK_PLUS);
2834 right = mmul_expr(state, index);
2838 meat(state, index, TOK_MINUS);
2839 right = mmul_expr(state, index);
2851 static long_t mshift_expr(struct compile_state *state, int index)
2855 val = madd_expr(state, index);
2859 switch(mpeek(state, index)) {
2861 meat(state, index, TOK_SL);
2862 right = madd_expr(state, index);
2866 meat(state, index, TOK_SR);
2867 right = madd_expr(state, index);
2879 static long_t mrel_expr(struct compile_state *state, int index)
2883 val = mshift_expr(state, index);
2887 switch(mpeek(state, index)) {
2889 meat(state, index, TOK_LESS);
2890 right = mshift_expr(state, index);
2894 meat(state, index, TOK_MORE);
2895 right = mshift_expr(state, index);
2899 meat(state, index, TOK_LESSEQ);
2900 right = mshift_expr(state, index);
2904 meat(state, index, TOK_MOREEQ);
2905 right = mshift_expr(state, index);
2916 static long_t meq_expr(struct compile_state *state, int index)
2920 val = mrel_expr(state, index);
2924 switch(mpeek(state, index)) {
2926 meat(state, index, TOK_EQEQ);
2927 right = mrel_expr(state, index);
2931 meat(state, index, TOK_NOTEQ);
2932 right = mrel_expr(state, index);
2943 static long_t mand_expr(struct compile_state *state, int index)
2946 val = meq_expr(state, index);
2947 if (mpeek(state, index) == TOK_AND) {
2949 meat(state, index, TOK_AND);
2950 right = meq_expr(state, index);
2956 static long_t mxor_expr(struct compile_state *state, int index)
2959 val = mand_expr(state, index);
2960 if (mpeek(state, index) == TOK_XOR) {
2962 meat(state, index, TOK_XOR);
2963 right = mand_expr(state, index);
2969 static long_t mor_expr(struct compile_state *state, int index)
2972 val = mxor_expr(state, index);
2973 if (mpeek(state, index) == TOK_OR) {
2975 meat(state, index, TOK_OR);
2976 right = mxor_expr(state, index);
2982 static long_t mland_expr(struct compile_state *state, int index)
2985 val = mor_expr(state, index);
2986 if (mpeek(state, index) == TOK_LOGAND) {
2988 meat(state, index, TOK_LOGAND);
2989 right = mor_expr(state, index);
2994 static long_t mlor_expr(struct compile_state *state, int index)
2997 val = mland_expr(state, index);
2998 if (mpeek(state, index) == TOK_LOGOR) {
3000 meat(state, index, TOK_LOGOR);
3001 right = mland_expr(state, index);
3007 static long_t mcexpr(struct compile_state *state, int index)
3009 return mlor_expr(state, index);
3011 static void preprocess(struct compile_state *state, int index)
3013 /* Doing much more with the preprocessor would require
3014 * a parser and a major restructuring.
3015 * Postpone that for later.
3017 struct file_state *file;
3023 tk = &state->token[index];
3024 state->macro_line = line = file->line;
3025 state->macro_file = file;
3027 next_token(state, index);
3028 ident_to_macro(state, tk);
3029 if (tk->tok == TOK_IDENT) {
3030 error(state, 0, "undefined preprocessing directive `%s'",
3037 if (state->if_value < 0) {
3040 warning(state, 0, "Ignoring preprocessor directive: %s",
3044 error(state, 0, "#elif not supported");
3045 #warning "FIXME multiple #elif and #else in an #if do not work properly"
3046 if (state->if_depth == 0) {
3047 error(state, 0, "#elif without #if");
3049 /* If the #if was taken the #elif just disables the following code */
3050 if (state->if_value >= 0) {
3051 state->if_value = - state->if_value;
3053 /* If the previous #if was not taken see if the #elif enables the
3056 else if ((state->if_value < 0) &&
3057 (state->if_depth == - state->if_value))
3059 if (mcexpr(state, index) != 0) {
3060 state->if_value = state->if_depth;
3063 state->if_value = - state->if_depth;
3069 if (state->if_value < 0) {
3072 if (mcexpr(state, index) != 0) {
3073 state->if_value = state->if_depth;
3076 state->if_value = - state->if_depth;
3081 if (state->if_value < 0) {
3084 next_token(state, index);
3085 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3086 error(state, 0, "Invalid macro name");
3088 if (tk->ident->sym_define == 0) {
3089 state->if_value = state->if_depth;
3092 state->if_value = - state->if_depth;
3097 if (state->if_value < 0) {
3100 next_token(state, index);
3101 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3102 error(state, 0, "Invalid macro name");
3104 if (tk->ident->sym_define != 0) {
3105 state->if_value = state->if_depth;
3108 state->if_value = - state->if_depth;
3112 if (state->if_depth == 0) {
3113 error(state, 0, "#else without #if");
3115 if ((state->if_value >= 0) ||
3116 ((state->if_value < 0) &&
3117 (state->if_depth == -state->if_value)))
3119 state->if_value = - state->if_value;
3123 if (state->if_depth == 0) {
3124 error(state, 0, "#endif without #if");
3126 if ((state->if_value >= 0) ||
3127 ((state->if_value < 0) &&
3128 (state->if_depth == -state->if_value)))
3130 state->if_value = state->if_depth - 1;
3136 struct hash_entry *ident;
3137 struct macro *macro;
3140 if (state->if_value < 0) /* quit early when #if'd out */
3143 meat(state, index, TOK_IDENT);
3147 if (*file->pos == '(') {
3148 #warning "FIXME macros with arguments not supported"
3149 error(state, 0, "Macros with arguments not supported");
3152 /* Find the end of the line to get an estimate of
3153 * the macro's length.
3155 for(ptr = file->pos; *ptr != '\n'; ptr++)
3158 if (ident->sym_define != 0) {
3159 error(state, 0, "macro %s already defined\n", ident->name);
3161 macro = xmalloc(sizeof(*macro), "macro");
3162 macro->ident = ident;
3163 macro->buf_len = ptr - file->pos +1;
3164 macro->buf = xmalloc(macro->buf_len +2, "macro buf");
3166 memcpy(macro->buf, file->pos, macro->buf_len);
3167 macro->buf[macro->buf_len] = '\n';
3168 macro->buf[macro->buf_len +1] = '\0';
3170 ident->sym_define = macro;
3177 /* Find the end of the line */
3178 for(end = file->pos; *end != '\n'; end++)
3180 len = (end - file->pos);
3181 if (state->if_value >= 0) {
3182 error(state, 0, "%*.*s", len, len, file->pos);
3191 /* Find the end of the line */
3192 for(end = file->pos; *end != '\n'; end++)
3194 len = (end - file->pos);
3195 if (state->if_value >= 0) {
3196 warning(state, 0, "%*.*s", len, len, file->pos);
3208 next_token(state, index);
3209 if (tk->tok == TOK_LIT_STRING) {
3212 name = xmalloc(tk->str_len, "include");
3213 token = tk->val.str +1;
3214 name_len = tk->str_len -2;
3215 if (*token == '"') {
3219 memcpy(name, token, name_len);
3220 name[name_len] = '\0';
3223 else if (tk->tok == TOK_LESS) {
3226 for(end = start; *end != '\n'; end++) {
3232 error(state, 0, "Unterminated included directive");
3234 name = xmalloc(end - start + 1, "include");
3235 memcpy(name, start, end - start);
3236 name[end - start] = '\0';
3241 error(state, 0, "Invalid include directive");
3243 /* Error if there are any characters after the include */
3244 for(ptr = file->pos; *ptr != '\n'; ptr++) {
3245 if (!isspace(*ptr)) {
3246 error(state, 0, "garbage after include directive");
3249 if (state->if_value >= 0) {
3250 compile_file(state, name, local);
3253 next_token(state, index);
3257 /* Ignore # without a following ident */
3258 if (tk->tok == TOK_IDENT) {
3259 error(state, 0, "Invalid preprocessor directive: %s",
3264 /* Consume the rest of the macro line */
3266 tok = mpeek(state, index);
3267 meat(state, index, tok);
3268 } while(tok != TOK_EOF);
3272 static void token(struct compile_state *state, int index)
3274 struct file_state *file;
3278 tk = &state->token[index];
3279 next_token(state, index);
3283 if (tk->tok == TOK_EOF && file->prev) {
3284 state->file = file->prev;
3285 /* file->basename is used keep it */
3286 xfree(file->dirname);
3289 next_token(state, index);
3292 else if (tk->tok == TOK_MACRO) {
3293 preprocess(state, index);
3296 else if (tk->ident && tk->ident->sym_define) {
3297 compile_macro(state, tk);
3298 next_token(state, index);
3301 else if (state->if_value < 0) {
3302 next_token(state, index);
3308 static int peek(struct compile_state *state)
3310 if (state->token[1].tok == -1) {
3313 return state->token[1].tok;
3316 static int peek2(struct compile_state *state)
3318 if (state->token[1].tok == -1) {
3321 if (state->token[2].tok == -1) {
3324 return state->token[2].tok;
3327 static void eat(struct compile_state *state, int tok)
3331 next_tok = peek(state);
3332 if (next_tok != tok) {
3333 const char *name1, *name2;
3334 name1 = tokens[next_tok];
3336 if (next_tok == TOK_IDENT) {
3337 name2 = state->token[1].ident->name;
3339 error(state, 0, "\tfound %s %s expected %s",
3340 name1, name2 ,tokens[tok]);
3342 /* Free the old token value */
3343 if (state->token[0].str_len) {
3344 xfree((void *)(state->token[0].val.str));
3346 for(i = 0; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
3347 state->token[i] = state->token[i + 1];
3349 memset(&state->token[i], 0, sizeof(state->token[i]));
3350 state->token[i].tok = -1;
3353 #warning "FIXME do not hardcode the include paths"
3354 static char *include_paths[] = {
3355 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/include",
3356 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/arch/i386/include",
3357 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src",
3361 static void compile_file(struct compile_state *state, const char *filename, int local)
3364 const char *subdir, *base;
3366 struct file_state *file;
3368 file = xmalloc(sizeof(*file), "file_state");
3370 base = strrchr(filename, '/');
3373 subdir_len = base - filename;
3380 basename = xmalloc(strlen(base) +1, "basename");
3381 strcpy(basename, base);
3382 file->basename = basename;
3384 if (getcwd(cwd, sizeof(cwd)) == 0) {
3385 die("cwd buffer to small");
3388 if (subdir[0] == '/') {
3389 file->dirname = xmalloc(subdir_len + 1, "dirname");
3390 memcpy(file->dirname, subdir, subdir_len);
3391 file->dirname[subdir_len] = '\0';
3397 /* Find the appropriate directory... */
3399 if (!state->file && exists(cwd, filename)) {
3402 if (local && state->file && exists(state->file->dirname, filename)) {
3403 dir = state->file->dirname;
3405 for(path = include_paths; !dir && *path; path++) {
3406 if (exists(*path, filename)) {
3411 error(state, 0, "Cannot find `%s'\n", filename);
3413 dirlen = strlen(dir);
3414 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
3415 memcpy(file->dirname, dir, dirlen);
3416 file->dirname[dirlen] = '/';
3417 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
3418 file->dirname[dirlen + 1 + subdir_len] = '\0';
3420 file->buf = slurp_file(file->dirname, file->basename, &file->size);
3423 file->pos = file->buf;
3424 file->line_start = file->pos;
3427 file->prev = state->file;
3430 process_trigraphs(state);
3431 splice_lines(state);
3434 /* Type helper functions */
3436 static struct type *new_type(
3437 unsigned int type, struct type *left, struct type *right)
3439 struct type *result;
3440 result = xmalloc(sizeof(*result), "type");
3441 result->type = type;
3442 result->left = left;
3443 result->right = right;
3444 result->field_ident = 0;
3445 result->type_ident = 0;
3449 static struct type *clone_type(unsigned int specifiers, struct type *old)
3451 struct type *result;
3452 result = xmalloc(sizeof(*result), "type");
3453 memcpy(result, old, sizeof(*result));
3454 result->type &= TYPE_MASK;
3455 result->type |= specifiers;
3459 #define SIZEOF_SHORT 2
3460 #define SIZEOF_INT 4
3461 #define SIZEOF_LONG (sizeof(long_t))
3463 #define ALIGNOF_SHORT 2
3464 #define ALIGNOF_INT 4
3465 #define ALIGNOF_LONG (sizeof(long_t))
3467 #define MASK_UCHAR(X) ((X) & ((ulong_t)0xff))
3468 #define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT*8)) - 1))
3469 static inline ulong_t mask_uint(ulong_t x)
3471 if (SIZEOF_INT < SIZEOF_LONG) {
3472 ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT*8))) -1;
3477 #define MASK_UINT(X) (mask_uint(X))
3478 #define MASK_ULONG(X) (X)
3480 static struct type void_type = { .type = TYPE_VOID };
3481 static struct type char_type = { .type = TYPE_CHAR };
3482 static struct type uchar_type = { .type = TYPE_UCHAR };
3483 static struct type short_type = { .type = TYPE_SHORT };
3484 static struct type ushort_type = { .type = TYPE_USHORT };
3485 static struct type int_type = { .type = TYPE_INT };
3486 static struct type uint_type = { .type = TYPE_UINT };
3487 static struct type long_type = { .type = TYPE_LONG };
3488 static struct type ulong_type = { .type = TYPE_ULONG };
3490 static struct triple *variable(struct compile_state *state, struct type *type)
3492 struct triple *result;
3493 if ((type->type & STOR_MASK) != STOR_PERM) {
3494 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3495 result = triple(state, OP_ADECL, type, 0, 0);
3498 struct triple **vector;
3500 result = new_triple(state, OP_VAL_VEC, type, -1, -1);
3501 vector = &result->param[0];
3505 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
3506 vector[index] = variable(state, field->left);
3507 field = field->right;
3510 vector[index] = variable(state, field);
3514 result = triple(state, OP_SDECL, type, 0, 0);
3519 static void stor_of(FILE *fp, struct type *type)
3521 switch(type->type & STOR_MASK) {
3523 fprintf(fp, "auto ");
3526 fprintf(fp, "static ");
3529 fprintf(fp, "extern ");
3532 fprintf(fp, "register ");
3535 fprintf(fp, "typedef ");
3538 fprintf(fp, "inline ");
3542 static void qual_of(FILE *fp, struct type *type)
3544 if (type->type & QUAL_CONST) {
3545 fprintf(fp, " const");
3547 if (type->type & QUAL_VOLATILE) {
3548 fprintf(fp, " volatile");
3550 if (type->type & QUAL_RESTRICT) {
3551 fprintf(fp, " restrict");
3555 static void name_of(FILE *fp, struct type *type)
3558 switch(type->type & TYPE_MASK) {
3560 fprintf(fp, "void");
3564 fprintf(fp, "signed char");
3568 fprintf(fp, "unsigned char");
3572 fprintf(fp, "signed short");
3576 fprintf(fp, "unsigned short");
3580 fprintf(fp, "signed int");
3584 fprintf(fp, "unsigned int");
3588 fprintf(fp, "signed long");
3592 fprintf(fp, "unsigned long");
3596 name_of(fp, type->left);
3602 name_of(fp, type->left);
3604 name_of(fp, type->right);
3607 fprintf(fp, "enum %s", type->type_ident->name);
3611 fprintf(fp, "struct %s", type->type_ident->name);
3616 name_of(fp, type->left);
3617 fprintf(fp, " (*)(");
3618 name_of(fp, type->right);
3623 name_of(fp, type->left);
3624 fprintf(fp, " [%ld]", type->elements);
3627 fprintf(fp, "????: %x", type->type & TYPE_MASK);
3632 static size_t align_of(struct compile_state *state, struct type *type)
3636 switch(type->type & TYPE_MASK) {
3646 align = ALIGNOF_SHORT;
3651 align = ALIGNOF_INT;
3656 align = ALIGNOF_LONG;
3661 size_t left_align, right_align;
3662 left_align = align_of(state, type->left);
3663 right_align = align_of(state, type->right);
3664 align = (left_align >= right_align) ? left_align : right_align;
3668 align = align_of(state, type->left);
3671 align = align_of(state, type->left);
3674 error(state, 0, "alignof not yet defined for type\n");
3680 static size_t size_of(struct compile_state *state, struct type *type)
3684 switch(type->type & TYPE_MASK) {
3694 size = SIZEOF_SHORT;
3709 size = size_of(state, type->left);
3710 while((type->right->type & TYPE_MASK) == TYPE_PRODUCT) {
3712 align = align_of(state, type->left);
3713 pad = align - (size % align);
3714 size = size + pad + size_of(state, type->left);
3716 align = align_of(state, type->right);
3717 pad = align - (size % align);
3718 size = size + pad + sizeof(type->right);
3723 size_t size_left, size_right;
3724 size_left = size_of(state, type->left);
3725 size_right = size_of(state, type->right);
3726 size = (size_left >= size_right)? size_left : size_right;
3730 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
3731 internal_error(state, 0, "Invalid array type");
3733 size = size_of(state, type->left) * type->elements;
3737 size = size_of(state, type->left);
3740 error(state, 0, "sizeof not yet defined for type\n");
3746 static size_t field_offset(struct compile_state *state,
3747 struct type *type, struct hash_entry *field)
3749 size_t size, align, pad;
3750 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3751 internal_error(state, 0, "field_offset only works on structures");
3755 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
3756 if (type->left->field_ident == field) {
3759 size += size_of(state, type->left);
3761 align = align_of(state, type->left);
3762 pad = align - (size % align);
3765 if (type->field_ident != field) {
3766 internal_error(state, 0, "field_offset: member %s not present",
3772 static struct type *field_type(struct compile_state *state,
3773 struct type *type, struct hash_entry *field)
3775 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3776 internal_error(state, 0, "field_type only works on structures");
3779 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
3780 if (type->left->field_ident == field) {
3786 if (type->field_ident != field) {
3787 internal_error(state, 0, "field_type: member %s not present",
3793 static struct triple *struct_field(struct compile_state *state,
3794 struct triple *decl, struct hash_entry *field)
3796 struct triple **vector;
3800 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3803 if (decl->op != OP_VAL_VEC) {
3804 internal_error(state, 0, "Invalid struct variable");
3807 internal_error(state, 0, "Missing structure field");
3810 vector = &RHS(decl, 0);
3812 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
3813 if (type->left->field_ident == field) {
3820 if (type->field_ident != field) {
3821 internal_error(state, 0, "field %s not found?", field->name);
3823 return vector[index];
3826 static void arrays_complete(struct compile_state *state, struct type *type)
3828 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
3829 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
3830 error(state, 0, "array size not specified");
3832 arrays_complete(state, type->left);
3836 static unsigned int do_integral_promotion(unsigned int type)
3839 if (TYPE_INTEGER(type) &&
3840 TYPE_RANK(type) < TYPE_RANK(TYPE_INT)) {
3846 static unsigned int do_arithmetic_conversion(
3847 unsigned int left, unsigned int right)
3851 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
3852 return TYPE_LDOUBLE;
3854 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
3857 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
3860 left = do_integral_promotion(left);
3861 right = do_integral_promotion(right);
3862 /* If both operands have the same size done */
3863 if (left == right) {
3866 /* If both operands have the same signedness pick the larger */
3867 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
3868 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
3870 /* If the signed type can hold everything use it */
3871 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
3874 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
3877 /* Convert to the unsigned type with the same rank as the signed type */
3878 else if (TYPE_SIGNED(left)) {
3879 return TYPE_MKUNSIGNED(left);
3882 return TYPE_MKUNSIGNED(right);
3886 /* see if two types are the same except for qualifiers */
3887 static int equiv_types(struct type *left, struct type *right)
3890 /* Error if the basic types do not match */
3891 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
3894 type = left->type & TYPE_MASK;
3895 /* if the basic types match and it is an arithmetic type we are done */
3896 if (TYPE_ARITHMETIC(type)) {
3899 /* If it is a pointer type recurse and keep testing */
3900 if (type == TYPE_POINTER) {
3901 return equiv_types(left->left, right->left);
3903 else if (type == TYPE_ARRAY) {
3904 return (left->elements == right->elements) &&
3905 equiv_types(left->left, right->left);
3907 /* test for struct/union equality */
3908 else if (type == TYPE_STRUCT) {
3909 return left->type_ident == right->type_ident;
3911 /* Test for equivalent functions */
3912 else if (type == TYPE_FUNCTION) {
3913 return equiv_types(left->left, right->left) &&
3914 equiv_types(left->right, right->right);
3916 /* We only see TYPE_PRODUCT as part of function equivalence matching */
3917 else if (type == TYPE_PRODUCT) {
3918 return equiv_types(left->left, right->left) &&
3919 equiv_types(left->right, right->right);
3921 /* We should see TYPE_OVERLAP */
3927 static int equiv_ptrs(struct type *left, struct type *right)
3929 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
3930 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
3933 return equiv_types(left->left, right->left);
3936 static struct type *compatible_types(struct type *left, struct type *right)
3938 struct type *result;
3939 unsigned int type, qual_type;
3940 /* Error if the basic types do not match */
3941 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
3944 type = left->type & TYPE_MASK;
3945 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
3947 /* if the basic types match and it is an arithmetic type we are done */
3948 if (TYPE_ARITHMETIC(type)) {
3949 result = new_type(qual_type, 0, 0);
3951 /* If it is a pointer type recurse and keep testing */
3952 else if (type == TYPE_POINTER) {
3953 result = compatible_types(left->left, right->left);
3955 result = new_type(qual_type, result, 0);
3958 /* test for struct/union equality */
3959 else if (type == TYPE_STRUCT) {
3960 if (left->type_ident == right->type_ident) {
3964 /* Test for equivalent functions */
3965 else if (type == TYPE_FUNCTION) {
3966 struct type *lf, *rf;
3967 lf = compatible_types(left->left, right->left);
3968 rf = compatible_types(left->right, right->right);
3970 result = new_type(qual_type, lf, rf);
3973 /* We only see TYPE_PRODUCT as part of function equivalence matching */
3974 else if (type == TYPE_PRODUCT) {
3975 struct type *lf, *rf;
3976 lf = compatible_types(left->left, right->left);
3977 rf = compatible_types(left->right, right->right);
3979 result = new_type(qual_type, lf, rf);
3983 /* Nothing else is compatible */
3988 static struct type *compatible_ptrs(struct type *left, struct type *right)
3990 struct type *result;
3991 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
3992 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
3995 result = compatible_types(left->left, right->left);
3997 unsigned int qual_type;
3998 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
3999 result = new_type(qual_type, result, 0);
4004 static struct triple *integral_promotion(
4005 struct compile_state *state, struct triple *def)
4009 /* As all operations are carried out in registers
4010 * the values are converted on load I just convert
4011 * logical type of the operand.
4013 if (TYPE_INTEGER(type->type)) {
4014 unsigned int int_type;
4015 int_type = type->type & ~TYPE_MASK;
4016 int_type |= do_integral_promotion(type->type);
4017 if (int_type != type->type) {
4018 def->type = new_type(int_type, 0, 0);
4025 static void arithmetic(struct compile_state *state, struct triple *def)
4027 if (!TYPE_ARITHMETIC(def->type->type)) {
4028 error(state, 0, "arithmetic type expexted");
4032 static void ptr_arithmetic(struct compile_state *state, struct triple *def)
4034 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
4035 error(state, def, "pointer or arithmetic type expected");
4039 static int is_integral(struct triple *ins)
4041 return TYPE_INTEGER(ins->type->type);
4044 static void integral(struct compile_state *state, struct triple *def)
4046 if (!is_integral(def)) {
4047 error(state, 0, "integral type expected");
4052 static void bool(struct compile_state *state, struct triple *def)
4054 if (!TYPE_ARITHMETIC(def->type->type) &&
4055 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
4056 error(state, 0, "arithmetic or pointer type expected");
4060 static int is_signed(struct type *type)
4062 return !!TYPE_SIGNED(type->type);
4065 /* Is this value located in a register otherwise it must be in memory */
4066 static int is_in_reg(struct compile_state *state, struct triple *def)
4069 if (def->op == OP_ADECL) {
4072 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
4075 else if (def->op == OP_VAL_VEC) {
4076 in_reg = is_in_reg(state, RHS(def, 0));
4078 else if (def->op == OP_DOT) {
4079 in_reg = is_in_reg(state, RHS(def, 0));
4082 internal_error(state, 0, "unknown expr storage location");
4088 /* Is this a stable variable location otherwise it must be a temporary */
4089 static int is_stable(struct compile_state *state, struct triple *def)
4096 if ((def->op == OP_ADECL) ||
4097 (def->op == OP_SDECL) ||
4098 (def->op == OP_DEREF) ||
4099 (def->op == OP_BLOBCONST)) {
4102 else if (def->op == OP_DOT) {
4103 ret = is_stable(state, RHS(def, 0));
4105 else if (def->op == OP_VAL_VEC) {
4106 struct triple **vector;
4109 vector = &RHS(def, 0);
4110 for(i = 0; i < def->type->elements; i++) {
4111 if (!is_stable(state, vector[i])) {
4120 static int is_lvalue(struct compile_state *state, struct triple *def)
4127 if (!is_stable(state, def)) {
4130 if (def->type->type & QUAL_CONST) {
4133 else if (def->op == OP_DOT) {
4134 ret = is_lvalue(state, RHS(def, 0));
4139 static void lvalue(struct compile_state *state, struct triple *def)
4142 internal_error(state, def, "nothing where lvalue expected?");
4144 if (!is_lvalue(state, def)) {
4145 error(state, def, "lvalue expected");
4149 static int is_pointer(struct triple *def)
4151 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
4154 static void pointer(struct compile_state *state, struct triple *def)
4156 if (!is_pointer(def)) {
4157 error(state, def, "pointer expected");
4161 static struct triple *int_const(
4162 struct compile_state *state, struct type *type, ulong_t value)
4164 struct triple *result;
4165 switch(type->type & TYPE_MASK) {
4167 case TYPE_INT: case TYPE_UINT:
4168 case TYPE_LONG: case TYPE_ULONG:
4171 internal_error(state, 0, "constant for unkown type");
4173 result = triple(state, OP_INTCONST, type, 0, 0);
4174 result->u.cval = value;
4179 static struct triple *do_mk_addr_expr(struct compile_state *state,
4180 struct triple *expr, struct type *type, ulong_t offset)
4182 struct triple *result;
4183 lvalue(state, expr);
4186 if (expr->op == OP_ADECL) {
4187 error(state, expr, "address of auto variables not supported");
4189 else if (expr->op == OP_SDECL) {
4190 result = triple(state, OP_ADDRCONST, type, 0, 0);
4191 MISC(result, 0) = expr;
4192 result->u.cval = offset;
4194 else if (expr->op == OP_DEREF) {
4195 result = triple(state, OP_ADD, type,
4197 int_const(state, &ulong_type, offset));
4202 static struct triple *mk_addr_expr(
4203 struct compile_state *state, struct triple *expr, ulong_t offset)
4208 TYPE_POINTER | (expr->type->type & QUAL_MASK),
4211 return do_mk_addr_expr(state, expr, type, offset);
4214 static struct triple *mk_deref_expr(
4215 struct compile_state *state, struct triple *expr)
4217 struct type *base_type;
4218 pointer(state, expr);
4219 base_type = expr->type->left;
4220 if (!TYPE_PTR(base_type->type) && !TYPE_ARITHMETIC(base_type->type)) {
4222 "Only pointer and arithmetic values can be dereferenced");
4224 return triple(state, OP_DEREF, base_type, expr, 0);
4227 static struct triple *deref_field(
4228 struct compile_state *state, struct triple *expr, struct hash_entry *field)
4230 struct triple *result;
4231 struct type *type, *member;
4233 internal_error(state, 0, "No field passed to deref_field");
4237 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4238 error(state, 0, "request for member %s in something not a struct or union",
4241 member = type->left;
4242 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4243 if (member->left->field_ident == field) {
4244 member = member->left;
4247 member = member->right;
4249 if (member->field_ident != field) {
4250 error(state, 0, "%s is not a member", field->name);
4252 if ((type->type & STOR_MASK) == STOR_PERM) {
4253 /* Do the pointer arithmetic to get a deref the field */
4255 offset = field_offset(state, type, field);
4256 result = do_mk_addr_expr(state, expr, member, offset);
4257 result = mk_deref_expr(state, result);
4260 /* Find the variable for the field I want. */
4261 result = triple(state, OP_DOT,
4262 field_type(state, type, field), expr, 0);
4263 result->u.field = field;
4268 static struct triple *read_expr(struct compile_state *state, struct triple *def)
4274 if (!is_stable(state, def)) {
4277 /* Tranform an array to a pointer to the first element */
4278 #warning "CHECK_ME is this the right place to transform arrays to pointers?"
4279 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
4281 struct triple *result;
4283 TYPE_POINTER | (def->type->type & QUAL_MASK),
4284 def->type->left, 0);
4285 result = triple(state, OP_ADDRCONST, type, 0, 0);
4286 MISC(result, 0) = def;
4289 if (is_in_reg(state, def)) {
4294 return triple(state, op, def->type, def, 0);
4297 static void write_compatible(struct compile_state *state,
4298 struct type *dest, struct type *rval)
4301 /* Both operands have arithmetic type */
4302 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
4305 /* One operand is a pointer and the other is a pointer to void */
4306 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
4307 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
4308 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
4309 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
4312 /* If both types are the same without qualifiers we are good */
4313 else if (equiv_ptrs(dest, rval)) {
4316 /* test for struct/union equality */
4317 else if (((dest->type & TYPE_MASK) == TYPE_STRUCT) &&
4318 ((rval->type & TYPE_MASK) == TYPE_STRUCT) &&
4319 (dest->type_ident == rval->type_ident)) {
4323 error(state, 0, "Incompatible types in assignment");
4327 static struct triple *write_expr(
4328 struct compile_state *state, struct triple *dest, struct triple *rval)
4335 internal_error(state, 0, "missing rval");
4338 if (rval->op == OP_LIST) {
4339 internal_error(state, 0, "expression of type OP_LIST?");
4341 if (!is_lvalue(state, dest)) {
4342 internal_error(state, 0, "writing to a non lvalue?");
4345 write_compatible(state, dest->type, rval->type);
4347 /* Now figure out which assignment operator to use */
4349 if (is_in_reg(state, dest)) {
4354 def = triple(state, op, dest->type, dest, rval);
4358 static struct triple *init_expr(
4359 struct compile_state *state, struct triple *dest, struct triple *rval)
4365 internal_error(state, 0, "missing rval");
4367 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
4368 rval = read_expr(state, rval);
4369 def = write_expr(state, dest, rval);
4372 /* Fill in the array size if necessary */
4373 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
4374 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
4375 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4376 dest->type->elements = rval->type->elements;
4379 if (!equiv_types(dest->type, rval->type)) {
4380 error(state, 0, "Incompatible types in inializer");
4382 MISC(dest, 0) = rval;
4383 insert_triple(state, dest, rval);
4384 rval->id |= TRIPLE_FLAG_FLATTENED;
4385 use_triple(MISC(dest, 0), dest);
4390 struct type *arithmetic_result(
4391 struct compile_state *state, struct triple *left, struct triple *right)
4394 /* Sanity checks to ensure I am working with arithmetic types */
4395 arithmetic(state, left);
4396 arithmetic(state, right);
4398 do_arithmetic_conversion(
4400 right->type->type), 0, 0);
4404 struct type *ptr_arithmetic_result(
4405 struct compile_state *state, struct triple *left, struct triple *right)
4408 /* Sanity checks to ensure I am working with the proper types */
4409 ptr_arithmetic(state, left);
4410 arithmetic(state, right);
4411 if (TYPE_ARITHMETIC(left->type->type) &&
4412 TYPE_ARITHMETIC(right->type->type)) {
4413 type = arithmetic_result(state, left, right);
4415 else if (TYPE_PTR(left->type->type)) {
4419 internal_error(state, 0, "huh?");
4426 /* boolean helper function */
4428 static struct triple *ltrue_expr(struct compile_state *state,
4429 struct triple *expr)
4432 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
4433 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
4434 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
4435 /* If the expression is already boolean do nothing */
4438 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
4444 static struct triple *lfalse_expr(struct compile_state *state,
4445 struct triple *expr)
4447 return triple(state, OP_LFALSE, &int_type, expr, 0);
4450 static struct triple *cond_expr(
4451 struct compile_state *state,
4452 struct triple *test, struct triple *left, struct triple *right)
4455 struct type *result_type;
4456 unsigned int left_type, right_type;
4458 left_type = left->type->type;
4459 right_type = right->type->type;
4461 /* Both operands have arithmetic type */
4462 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
4463 result_type = arithmetic_result(state, left, right);
4465 /* Both operands have void type */
4466 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
4467 ((right_type & TYPE_MASK) == TYPE_VOID)) {
4468 result_type = &void_type;
4470 /* pointers to the same type... */
4471 else if ((result_type = compatible_ptrs(left->type, right->type))) {
4474 /* Both operands are pointers and left is a pointer to void */
4475 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4476 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4477 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4478 result_type = right->type;
4480 /* Both operands are pointers and right is a pointer to void */
4481 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4482 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4483 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4484 result_type = left->type;
4487 error(state, 0, "Incompatible types in conditional expression");
4489 /* Cleanup and invert the test */
4490 test = lfalse_expr(state, read_expr(state, test));
4491 def = new_triple(state, OP_COND, result_type, 0, 3);
4492 def->param[0] = test;
4493 def->param[1] = left;
4494 def->param[2] = right;
4499 static int expr_depth(struct compile_state *state, struct triple *ins)
4503 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
4506 else if (ins->op == OP_DEREF) {
4507 count = expr_depth(state, RHS(ins, 0)) - 1;
4509 else if (ins->op == OP_VAL) {
4510 count = expr_depth(state, RHS(ins, 0)) - 1;
4512 else if (ins->op == OP_COMMA) {
4514 ldepth = expr_depth(state, RHS(ins, 0));
4515 rdepth = expr_depth(state, RHS(ins, 1));
4516 count = (ldepth >= rdepth)? ldepth : rdepth;
4518 else if (ins->op == OP_CALL) {
4519 /* Don't figure the depth of a call just guess it is huge */
4523 struct triple **expr;
4524 expr = triple_rhs(state, ins, 0);
4525 for(;expr; expr = triple_rhs(state, ins, expr)) {
4528 depth = expr_depth(state, *expr);
4529 if (depth > count) {
4538 static struct triple *flatten(
4539 struct compile_state *state, struct triple *first, struct triple *ptr);
4541 static struct triple *flatten_generic(
4542 struct compile_state *state, struct triple *first, struct triple *ptr)
4546 struct triple **ins;
4549 /* Only operations with just a rhs should come here */
4550 rhs = TRIPLE_RHS(ptr->sizes);
4551 lhs = TRIPLE_LHS(ptr->sizes);
4552 if (TRIPLE_SIZE(ptr->sizes) != lhs + rhs) {
4553 internal_error(state, ptr, "unexpected args for: %d %s",
4554 ptr->op, tops(ptr->op));
4556 /* Find the depth of the rhs elements */
4557 for(i = 0; i < rhs; i++) {
4558 vector[i].ins = &RHS(ptr, i);
4559 vector[i].depth = expr_depth(state, *vector[i].ins);
4561 /* Selection sort the rhs */
4562 for(i = 0; i < rhs; i++) {
4564 for(j = i + 1; j < rhs; j++ ) {
4565 if (vector[j].depth > vector[max].depth) {
4570 struct rhs_vector tmp;
4572 vector[i] = vector[max];
4576 /* Now flatten the rhs elements */
4577 for(i = 0; i < rhs; i++) {
4578 *vector[i].ins = flatten(state, first, *vector[i].ins);
4579 use_triple(*vector[i].ins, ptr);
4582 /* Now flatten the lhs elements */
4583 for(i = 0; i < lhs; i++) {
4584 struct triple **ins = &LHS(ptr, i);
4585 *ins = flatten(state, first, *ins);
4586 use_triple(*ins, ptr);
4591 static struct triple *flatten_land(
4592 struct compile_state *state, struct triple *first, struct triple *ptr)
4594 struct triple *left, *right;
4595 struct triple *val, *test, *jmp, *label1, *end;
4597 /* Find the triples */
4599 right = RHS(ptr, 1);
4601 /* Generate the needed triples */
4604 /* Thread the triples together */
4605 val = flatten(state, first, variable(state, ptr->type));
4606 left = flatten(state, first, write_expr(state, val, left));
4607 test = flatten(state, first,
4608 lfalse_expr(state, read_expr(state, val)));
4609 jmp = flatten(state, first, branch(state, end, test));
4610 label1 = flatten(state, first, label(state));
4611 right = flatten(state, first, write_expr(state, val, right));
4612 TARG(jmp, 0) = flatten(state, first, end);
4614 /* Now give the caller something to chew on */
4615 return read_expr(state, val);
4618 static struct triple *flatten_lor(
4619 struct compile_state *state, struct triple *first, struct triple *ptr)
4621 struct triple *left, *right;
4622 struct triple *val, *jmp, *label1, *end;
4624 /* Find the triples */
4626 right = RHS(ptr, 1);
4628 /* Generate the needed triples */
4631 /* Thread the triples together */
4632 val = flatten(state, first, variable(state, ptr->type));
4633 left = flatten(state, first, write_expr(state, val, left));
4634 jmp = flatten(state, first, branch(state, end, left));
4635 label1 = flatten(state, first, label(state));
4636 right = flatten(state, first, write_expr(state, val, right));
4637 TARG(jmp, 0) = flatten(state, first, end);
4640 /* Now give the caller something to chew on */
4641 return read_expr(state, val);
4644 static struct triple *flatten_cond(
4645 struct compile_state *state, struct triple *first, struct triple *ptr)
4647 struct triple *test, *left, *right;
4648 struct triple *val, *mv1, *jmp1, *label1, *mv2, *middle, *jmp2, *end;
4650 /* Find the triples */
4653 right = RHS(ptr, 2);
4655 /* Generate the needed triples */
4657 middle = label(state);
4659 /* Thread the triples together */
4660 val = flatten(state, first, variable(state, ptr->type));
4661 test = flatten(state, first, test);
4662 jmp1 = flatten(state, first, branch(state, middle, test));
4663 label1 = flatten(state, first, label(state));
4664 left = flatten(state, first, left);
4665 mv1 = flatten(state, first, write_expr(state, val, left));
4666 jmp2 = flatten(state, first, branch(state, end, 0));
4667 TARG(jmp1, 0) = flatten(state, first, middle);
4668 right = flatten(state, first, right);
4669 mv2 = flatten(state, first, write_expr(state, val, right));
4670 TARG(jmp2, 0) = flatten(state, first, end);
4672 /* Now give the caller something to chew on */
4673 return read_expr(state, val);
4676 struct triple *copy_func(struct compile_state *state, struct triple *ofunc)
4678 struct triple *nfunc;
4679 struct triple *nfirst, *ofirst;
4680 struct triple *new, *old;
4683 fprintf(stdout, "\n");
4684 loc(stdout, state, 0);
4685 fprintf(stdout, "\n__________ copy_func _________\n");
4686 print_triple(state, ofunc);
4687 fprintf(stdout, "__________ copy_func _________ done\n\n");
4690 /* Make a new copy of the old function */
4691 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
4693 ofirst = old = RHS(ofunc, 0);
4696 int old_lhs, old_rhs;
4697 old_lhs = TRIPLE_LHS(old->sizes);
4698 old_rhs = TRIPLE_RHS(old->sizes);
4699 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
4700 old->filename, old->line, old->col);
4701 if (!triple_stores_block(state, new)) {
4702 memcpy(&new->u, &old->u, sizeof(new->u));
4705 RHS(nfunc, 0) = nfirst = new;
4708 insert_triple(state, nfirst, new);
4710 new->id |= TRIPLE_FLAG_FLATTENED;
4712 /* During the copy remember new as user of old */
4713 use_triple(old, new);
4715 /* Populate the return type if present */
4716 if (old == MISC(ofunc, 0)) {
4717 MISC(nfunc, 0) = new;
4720 } while(old != ofirst);
4722 /* Make a second pass to fix up any unresolved references */
4726 struct triple **oexpr, **nexpr;
4728 /* Lookup where the copy is, to join pointers */
4729 count = TRIPLE_SIZE(old->sizes);
4730 for(i = 0; i < count; i++) {
4731 oexpr = &old->param[i];
4732 nexpr = &new->param[i];
4733 if (!*nexpr && *oexpr && (*oexpr)->use) {
4734 *nexpr = (*oexpr)->use->member;
4735 if (*nexpr == old) {
4736 internal_error(state, 0, "new == old?");
4738 use_triple(*nexpr, new);
4740 if (!*nexpr && *oexpr) {
4741 internal_error(state, 0, "Could not copy %d\n", i);
4746 } while((old != ofirst) && (new != nfirst));
4748 /* Make a third pass to cleanup the extra useses */
4752 unuse_triple(old, new);
4755 } while ((old != ofirst) && (new != nfirst));
4759 static struct triple *flatten_call(
4760 struct compile_state *state, struct triple *first, struct triple *ptr)
4762 /* Inline the function call */
4764 struct triple *ofunc, *nfunc, *nfirst, *param, *result;
4765 struct triple *end, *nend;
4768 /* Find the triples */
4769 ofunc = MISC(ptr, 0);
4770 if (ofunc->op != OP_LIST) {
4771 internal_error(state, 0, "improper function");
4773 nfunc = copy_func(state, ofunc);
4774 nfirst = RHS(nfunc, 0)->next;
4775 /* Prepend the parameter reading into the new function list */
4776 ptype = nfunc->type->right;
4777 param = RHS(nfunc, 0)->next;
4778 pvals = TRIPLE_RHS(ptr->sizes);
4779 for(i = 0; i < pvals; i++) {
4783 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
4784 atype = ptype->left;
4786 while((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
4787 param = param->next;
4790 flatten(state, nfirst, write_expr(state, param, arg));
4791 ptype = ptype->right;
4792 param = param->next;
4795 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
4796 result = read_expr(state, MISC(nfunc,0));
4799 fprintf(stdout, "\n");
4800 loc(stdout, state, 0);
4801 fprintf(stdout, "\n__________ flatten_call _________\n");
4802 print_triple(state, nfunc);
4803 fprintf(stdout, "__________ flatten_call _________ done\n\n");
4806 /* Get rid of the extra triples */
4807 nfirst = RHS(nfunc, 0)->next;
4808 free_triple(state, RHS(nfunc, 0));
4810 free_triple(state, nfunc);
4812 /* Append the new function list onto the return list */
4814 nend = nfirst->prev;
4823 static struct triple *flatten(
4824 struct compile_state *state, struct triple *first, struct triple *ptr)
4826 struct triple *orig_ptr;
4831 /* Only flatten triples once */
4832 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
4838 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
4839 LHS(ptr, 0) = flatten(state, first, LHS(ptr, 0));
4840 use_triple(LHS(ptr, 0), ptr);
4841 use_triple(RHS(ptr, 0), ptr);
4844 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
4848 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
4849 return MISC(ptr, 0);
4852 ptr = flatten_land(state, first, ptr);
4855 ptr = flatten_lor(state, first, ptr);
4858 ptr = flatten_cond(state, first, ptr);
4861 ptr = flatten_call(state, first, ptr);
4865 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
4866 use_triple(RHS(ptr, 0), ptr);
4869 use_triple(TARG(ptr, 0), ptr);
4870 if (TRIPLE_RHS(ptr->sizes)) {
4871 use_triple(RHS(ptr, 0), ptr);
4872 if (ptr->next != ptr) {
4873 use_triple(ptr->next, ptr);
4878 insert_triple(state, first, ptr);
4879 ptr->id |= TRIPLE_FLAG_FLATTENED;
4880 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
4881 use_triple(MISC(ptr, 0), ptr);
4884 /* Since OP_DEREF is just a marker delete it when I flatten it */
4886 RHS(orig_ptr, 0) = 0;
4887 free_triple(state, orig_ptr);
4891 struct triple *base;
4893 base = flatten(state, first, base);
4894 if (base->op == OP_VAL_VEC) {
4895 ptr = struct_field(state, base, ptr->u.field);
4902 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
4903 use_triple(MISC(ptr, 0), ptr);
4908 /* Flatten the easy cases we don't override */
4909 ptr = flatten_generic(state, first, ptr);
4912 } while(ptr && (ptr != orig_ptr));
4914 insert_triple(state, first, ptr);
4915 ptr->id |= TRIPLE_FLAG_FLATTENED;
4920 static void release_expr(struct compile_state *state, struct triple *expr)
4922 struct triple *head;
4923 head = label(state);
4924 flatten(state, head, expr);
4925 while(head->next != head) {
4926 release_triple(state, head->next);
4928 free_triple(state, head);
4931 static int replace_rhs_use(struct compile_state *state,
4932 struct triple *orig, struct triple *new, struct triple *use)
4934 struct triple **expr;
4937 expr = triple_rhs(state, use, 0);
4938 for(;expr; expr = triple_rhs(state, use, expr)) {
4939 if (*expr == orig) {
4945 unuse_triple(orig, use);
4946 use_triple(new, use);
4951 static int replace_lhs_use(struct compile_state *state,
4952 struct triple *orig, struct triple *new, struct triple *use)
4954 struct triple **expr;
4957 expr = triple_lhs(state, use, 0);
4958 for(;expr; expr = triple_lhs(state, use, expr)) {
4959 if (*expr == orig) {
4965 unuse_triple(orig, use);
4966 use_triple(new, use);
4971 static void propogate_use(struct compile_state *state,
4972 struct triple *orig, struct triple *new)
4974 struct triple_set *user, *next;
4975 for(user = orig->use; user; user = next) {
4981 found |= replace_rhs_use(state, orig, new, use);
4982 found |= replace_lhs_use(state, orig, new, use);
4984 internal_error(state, use, "use without use");
4988 internal_error(state, orig, "used after propogate_use");
4994 * ===========================
4997 static struct triple *mk_add_expr(
4998 struct compile_state *state, struct triple *left, struct triple *right)
5000 struct type *result_type;
5001 /* Put pointer operands on the left */
5002 if (is_pointer(right)) {
5008 left = read_expr(state, left);
5009 right = read_expr(state, right);
5010 result_type = ptr_arithmetic_result(state, left, right);
5011 if (is_pointer(left)) {
5012 right = triple(state,
5013 is_signed(right->type)? OP_SMUL : OP_UMUL,
5016 int_const(state, &ulong_type,
5017 size_of(state, left->type->left)));
5019 return triple(state, OP_ADD, result_type, left, right);
5022 static struct triple *mk_sub_expr(
5023 struct compile_state *state, struct triple *left, struct triple *right)
5025 struct type *result_type;
5026 result_type = ptr_arithmetic_result(state, left, right);
5027 left = read_expr(state, left);
5028 right = read_expr(state, right);
5029 if (is_pointer(left)) {
5030 right = triple(state,
5031 is_signed(right->type)? OP_SMUL : OP_UMUL,
5034 int_const(state, &ulong_type,
5035 size_of(state, left->type->left)));
5037 return triple(state, OP_SUB, result_type, left, right);
5040 static struct triple *mk_pre_inc_expr(
5041 struct compile_state *state, struct triple *def)
5045 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
5046 return triple(state, OP_VAL, def->type,
5047 write_expr(state, def, val),
5051 static struct triple *mk_pre_dec_expr(
5052 struct compile_state *state, struct triple *def)
5056 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
5057 return triple(state, OP_VAL, def->type,
5058 write_expr(state, def, val),
5062 static struct triple *mk_post_inc_expr(
5063 struct compile_state *state, struct triple *def)
5067 val = read_expr(state, def);
5068 return triple(state, OP_VAL, def->type,
5069 write_expr(state, def,
5070 mk_add_expr(state, val, int_const(state, &int_type, 1)))
5074 static struct triple *mk_post_dec_expr(
5075 struct compile_state *state, struct triple *def)
5079 val = read_expr(state, def);
5080 return triple(state, OP_VAL, def->type,
5081 write_expr(state, def,
5082 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
5086 static struct triple *mk_subscript_expr(
5087 struct compile_state *state, struct triple *left, struct triple *right)
5089 left = read_expr(state, left);
5090 right = read_expr(state, right);
5091 if (!is_pointer(left) && !is_pointer(right)) {
5092 error(state, left, "subscripted value is not a pointer");
5094 return mk_deref_expr(state, mk_add_expr(state, left, right));
5098 * Compile time evaluation
5099 * ===========================
5101 static int is_const(struct triple *ins)
5103 return IS_CONST_OP(ins->op);
5106 static int constants_equal(struct compile_state *state,
5107 struct triple *left, struct triple *right)
5110 if (!is_const(left) || !is_const(right)) {
5113 else if (left->op != right->op) {
5116 else if (!equiv_types(left->type, right->type)) {
5123 if (left->u.cval == right->u.cval) {
5129 size_t lsize, rsize;
5130 lsize = size_of(state, left->type);
5131 rsize = size_of(state, right->type);
5132 if (lsize != rsize) {
5135 if (memcmp(left->u.blob, right->u.blob, lsize) == 0) {
5141 if ((MISC(left, 0) == MISC(right, 0)) &&
5142 (left->u.cval == right->u.cval)) {
5147 internal_error(state, left, "uknown constant type");
5154 static int is_zero(struct triple *ins)
5156 return is_const(ins) && (ins->u.cval == 0);
5159 static int is_one(struct triple *ins)
5161 return is_const(ins) && (ins->u.cval == 1);
5164 static long_t bsr(ulong_t value)
5167 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
5178 static long_t bsf(ulong_t value)
5181 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
5192 static long_t log2(ulong_t value)
5197 static long_t tlog2(struct triple *ins)
5199 return log2(ins->u.cval);
5202 static int is_pow2(struct triple *ins)
5204 ulong_t value, mask;
5206 if (!is_const(ins)) {
5209 value = ins->u.cval;
5216 return ((value & mask) == value);
5219 static ulong_t read_const(struct compile_state *state,
5220 struct triple *ins, struct triple **expr)
5224 switch(rhs->type->type &TYPE_MASK) {
5236 internal_error(state, rhs, "bad type to read_const\n");
5242 static long_t read_sconst(struct triple *ins, struct triple **expr)
5246 return (long_t)(rhs->u.cval);
5249 static void unuse_rhs(struct compile_state *state, struct triple *ins)
5251 struct triple **expr;
5252 expr = triple_rhs(state, ins, 0);
5253 for(;expr;expr = triple_rhs(state, ins, expr)) {
5255 unuse_triple(*expr, ins);
5261 static void unuse_lhs(struct compile_state *state, struct triple *ins)
5263 struct triple **expr;
5264 expr = triple_lhs(state, ins, 0);
5265 for(;expr;expr = triple_lhs(state, ins, expr)) {
5266 unuse_triple(*expr, ins);
5271 static void check_lhs(struct compile_state *state, struct triple *ins)
5273 struct triple **expr;
5274 expr = triple_lhs(state, ins, 0);
5275 for(;expr;expr = triple_lhs(state, ins, expr)) {
5276 internal_error(state, ins, "unexpected lhs");
5280 static void check_targ(struct compile_state *state, struct triple *ins)
5282 struct triple **expr;
5283 expr = triple_targ(state, ins, 0);
5284 for(;expr;expr = triple_targ(state, ins, expr)) {
5285 internal_error(state, ins, "unexpected targ");
5289 static void wipe_ins(struct compile_state *state, struct triple *ins)
5291 /* Becareful which instructions you replace the wiped
5292 * instruction with, as there are not enough slots
5293 * in all instructions to hold all others.
5295 check_targ(state, ins);
5296 unuse_rhs(state, ins);
5297 unuse_lhs(state, ins);
5300 static void mkcopy(struct compile_state *state,
5301 struct triple *ins, struct triple *rhs)
5303 wipe_ins(state, ins);
5305 ins->sizes = TRIPLE_SIZES(0, 1, 0, 0);
5307 use_triple(RHS(ins, 0), ins);
5310 static void mkconst(struct compile_state *state,
5311 struct triple *ins, ulong_t value)
5313 if (!is_integral(ins) && !is_pointer(ins)) {
5314 internal_error(state, ins, "unknown type to make constant\n");
5316 wipe_ins(state, ins);
5317 ins->op = OP_INTCONST;
5318 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
5319 ins->u.cval = value;
5322 static void mkaddr_const(struct compile_state *state,
5323 struct triple *ins, struct triple *sdecl, ulong_t value)
5325 wipe_ins(state, ins);
5326 ins->op = OP_ADDRCONST;
5327 ins->sizes = TRIPLE_SIZES(0, 0, 1, 0);
5328 MISC(ins, 0) = sdecl;
5329 ins->u.cval = value;
5330 use_triple(sdecl, ins);
5333 /* Transform multicomponent variables into simple register variables */
5334 static void flatten_structures(struct compile_state *state)
5336 struct triple *ins, *first;
5337 first = RHS(state->main_function, 0);
5339 /* Pass one expand structure values into valvecs.
5343 struct triple *next;
5345 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5346 if (ins->op == OP_VAL_VEC) {
5349 else if ((ins->op == OP_LOAD) || (ins->op == OP_READ)) {
5350 struct triple *def, **vector;
5357 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
5358 ins->filename, ins->line, ins->col);
5360 vector = &RHS(next, 0);
5361 tptr = next->type->left;
5362 for(i = 0; i < next->type->elements; i++) {
5363 struct triple *sfield;
5366 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5367 mtype = mtype->left;
5369 sfield = deref_field(state, def, mtype->field_ident);
5372 state, op, mtype, sfield, 0);
5373 vector[i]->filename = next->filename;
5374 vector[i]->line = next->line;
5375 vector[i]->col = next->col;
5378 propogate_use(state, ins, next);
5379 flatten(state, ins, next);
5380 free_triple(state, ins);
5382 else if ((ins->op == OP_STORE) || (ins->op == OP_WRITE)) {
5383 struct triple *src, *dst, **vector;
5391 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
5392 ins->filename, ins->line, ins->col);
5394 vector = &RHS(next, 0);
5395 tptr = next->type->left;
5396 for(i = 0; i < ins->type->elements; i++) {
5397 struct triple *dfield, *sfield;
5400 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5401 mtype = mtype->left;
5403 sfield = deref_field(state, src, mtype->field_ident);
5404 dfield = deref_field(state, dst, mtype->field_ident);
5406 state, op, mtype, dfield, sfield);
5407 vector[i]->filename = next->filename;
5408 vector[i]->line = next->line;
5409 vector[i]->col = next->col;
5412 propogate_use(state, ins, next);
5413 flatten(state, ins, next);
5414 free_triple(state, ins);
5418 } while(ins != first);
5419 /* Pass two flatten the valvecs.
5423 struct triple *next;
5425 if (ins->op == OP_VAL_VEC) {
5426 release_triple(state, ins);
5429 } while(ins != first);
5430 /* Pass three verify the state and set ->id to 0.
5434 ins->id &= ~TRIPLE_FLAG_FLATTENED;
5435 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5436 internal_error(state, 0, "STRUCT_TYPE remains?");
5438 if (ins->op == OP_DOT) {
5439 internal_error(state, 0, "OP_DOT remains?");
5441 if (ins->op == OP_VAL_VEC) {
5442 internal_error(state, 0, "OP_VAL_VEC remains?");
5445 } while(ins != first);
5448 /* For those operations that cannot be simplified */
5449 static void simplify_noop(struct compile_state *state, struct triple *ins)
5454 static void simplify_smul(struct compile_state *state, struct triple *ins)
5456 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5459 RHS(ins, 0) = RHS(ins, 1);
5462 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5464 left = read_sconst(ins, &RHS(ins, 0));
5465 right = read_sconst(ins, &RHS(ins, 1));
5466 mkconst(state, ins, left * right);
5468 else if (is_zero(RHS(ins, 1))) {
5469 mkconst(state, ins, 0);
5471 else if (is_one(RHS(ins, 1))) {
5472 mkcopy(state, ins, RHS(ins, 0));
5474 else if (is_pow2(RHS(ins, 1))) {
5476 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5478 insert_triple(state, ins, val);
5479 unuse_triple(RHS(ins, 1), ins);
5480 use_triple(val, ins);
5485 static void simplify_umul(struct compile_state *state, struct triple *ins)
5487 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5490 RHS(ins, 0) = RHS(ins, 1);
5493 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5494 ulong_t left, right;
5495 left = read_const(state, ins, &RHS(ins, 0));
5496 right = read_const(state, ins, &RHS(ins, 1));
5497 mkconst(state, ins, left * right);
5499 else if (is_zero(RHS(ins, 1))) {
5500 mkconst(state, ins, 0);
5502 else if (is_one(RHS(ins, 1))) {
5503 mkcopy(state, ins, RHS(ins, 0));
5505 else if (is_pow2(RHS(ins, 1))) {
5507 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5509 insert_triple(state, ins, val);
5510 unuse_triple(RHS(ins, 1), ins);
5511 use_triple(val, ins);
5516 static void simplify_sdiv(struct compile_state *state, struct triple *ins)
5518 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5520 left = read_sconst(ins, &RHS(ins, 0));
5521 right = read_sconst(ins, &RHS(ins, 1));
5522 mkconst(state, ins, left / right);
5524 else if (is_zero(RHS(ins, 0))) {
5525 mkconst(state, ins, 0);
5527 else if (is_zero(RHS(ins, 1))) {
5528 error(state, ins, "division by zero");
5530 else if (is_one(RHS(ins, 1))) {
5531 mkcopy(state, ins, RHS(ins, 0));
5533 else if (is_pow2(RHS(ins, 1))) {
5535 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5537 insert_triple(state, ins, val);
5538 unuse_triple(RHS(ins, 1), ins);
5539 use_triple(val, ins);
5544 static void simplify_udiv(struct compile_state *state, struct triple *ins)
5546 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5547 ulong_t left, right;
5548 left = read_const(state, ins, &RHS(ins, 0));
5549 right = read_const(state, ins, &RHS(ins, 1));
5550 mkconst(state, ins, left / right);
5552 else if (is_zero(RHS(ins, 0))) {
5553 mkconst(state, ins, 0);
5555 else if (is_zero(RHS(ins, 1))) {
5556 error(state, ins, "division by zero");
5558 else if (is_one(RHS(ins, 1))) {
5559 mkcopy(state, ins, RHS(ins, 0));
5561 else if (is_pow2(RHS(ins, 1))) {
5563 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5565 insert_triple(state, ins, val);
5566 unuse_triple(RHS(ins, 1), ins);
5567 use_triple(val, ins);
5572 static void simplify_smod(struct compile_state *state, struct triple *ins)
5574 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5576 left = read_const(state, ins, &RHS(ins, 0));
5577 right = read_const(state, ins, &RHS(ins, 1));
5578 mkconst(state, ins, left % right);
5580 else if (is_zero(RHS(ins, 0))) {
5581 mkconst(state, ins, 0);
5583 else if (is_zero(RHS(ins, 1))) {
5584 error(state, ins, "division by zero");
5586 else if (is_one(RHS(ins, 1))) {
5587 mkconst(state, ins, 0);
5589 else if (is_pow2(RHS(ins, 1))) {
5591 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
5593 insert_triple(state, ins, val);
5594 unuse_triple(RHS(ins, 1), ins);
5595 use_triple(val, ins);
5599 static void simplify_umod(struct compile_state *state, struct triple *ins)
5601 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5602 ulong_t left, right;
5603 left = read_const(state, ins, &RHS(ins, 0));
5604 right = read_const(state, ins, &RHS(ins, 1));
5605 mkconst(state, ins, left % right);
5607 else if (is_zero(RHS(ins, 0))) {
5608 mkconst(state, ins, 0);
5610 else if (is_zero(RHS(ins, 1))) {
5611 error(state, ins, "division by zero");
5613 else if (is_one(RHS(ins, 1))) {
5614 mkconst(state, ins, 0);
5616 else if (is_pow2(RHS(ins, 1))) {
5618 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
5620 insert_triple(state, ins, val);
5621 unuse_triple(RHS(ins, 1), ins);
5622 use_triple(val, ins);
5627 static void simplify_add(struct compile_state *state, struct triple *ins)
5629 /* start with the pointer on the left */
5630 if (is_pointer(RHS(ins, 1))) {
5633 RHS(ins, 0) = RHS(ins, 1);
5636 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5637 if (!is_pointer(RHS(ins, 0))) {
5638 ulong_t left, right;
5639 left = read_const(state, ins, &RHS(ins, 0));
5640 right = read_const(state, ins, &RHS(ins, 1));
5641 mkconst(state, ins, left + right);
5643 else /* op == OP_ADDRCONST */ {
5644 struct triple *sdecl;
5645 ulong_t left, right;
5646 sdecl = MISC(RHS(ins, 0), 0);
5647 left = RHS(ins, 0)->u.cval;
5648 right = RHS(ins, 1)->u.cval;
5649 mkaddr_const(state, ins, sdecl, left + right);
5652 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5655 RHS(ins, 1) = RHS(ins, 0);
5660 static void simplify_sub(struct compile_state *state, struct triple *ins)
5662 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5663 if (!is_pointer(RHS(ins, 0))) {
5664 ulong_t left, right;
5665 left = read_const(state, ins, &RHS(ins, 0));
5666 right = read_const(state, ins, &RHS(ins, 1));
5667 mkconst(state, ins, left - right);
5669 else /* op == OP_ADDRCONST */ {
5670 struct triple *sdecl;
5671 ulong_t left, right;
5672 sdecl = MISC(RHS(ins, 0), 0);
5673 left = RHS(ins, 0)->u.cval;
5674 right = RHS(ins, 1)->u.cval;
5675 mkaddr_const(state, ins, sdecl, left - right);
5680 static void simplify_sl(struct compile_state *state, struct triple *ins)
5682 if (is_const(RHS(ins, 1))) {
5684 right = read_const(state, ins, &RHS(ins, 1));
5685 if (right >= (size_of(state, ins->type)*8)) {
5686 warning(state, ins, "left shift count >= width of type");
5689 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5690 ulong_t left, right;
5691 left = read_const(state, ins, &RHS(ins, 0));
5692 right = read_const(state, ins, &RHS(ins, 1));
5693 mkconst(state, ins, left << right);
5697 static void simplify_usr(struct compile_state *state, struct triple *ins)
5699 if (is_const(RHS(ins, 1))) {
5701 right = read_const(state, ins, &RHS(ins, 1));
5702 if (right >= (size_of(state, ins->type)*8)) {
5703 warning(state, ins, "right shift count >= width of type");
5706 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5707 ulong_t left, right;
5708 left = read_const(state, ins, &RHS(ins, 0));
5709 right = read_const(state, ins, &RHS(ins, 1));
5710 mkconst(state, ins, left >> right);
5714 static void simplify_ssr(struct compile_state *state, struct triple *ins)
5716 if (is_const(RHS(ins, 1))) {
5718 right = read_const(state, ins, &RHS(ins, 1));
5719 if (right >= (size_of(state, ins->type)*8)) {
5720 warning(state, ins, "right shift count >= width of type");
5723 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5725 left = read_sconst(ins, &RHS(ins, 0));
5726 right = read_sconst(ins, &RHS(ins, 1));
5727 mkconst(state, ins, left >> right);
5731 static void simplify_and(struct compile_state *state, struct triple *ins)
5733 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5734 ulong_t left, right;
5735 left = read_const(state, ins, &RHS(ins, 0));
5736 right = read_const(state, ins, &RHS(ins, 1));
5737 mkconst(state, ins, left & right);
5741 static void simplify_or(struct compile_state *state, struct triple *ins)
5743 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5744 ulong_t left, right;
5745 left = read_const(state, ins, &RHS(ins, 0));
5746 right = read_const(state, ins, &RHS(ins, 1));
5747 mkconst(state, ins, left | right);
5751 static void simplify_xor(struct compile_state *state, struct triple *ins)
5753 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5754 ulong_t left, right;
5755 left = read_const(state, ins, &RHS(ins, 0));
5756 right = read_const(state, ins, &RHS(ins, 1));
5757 mkconst(state, ins, left ^ right);
5761 static void simplify_pos(struct compile_state *state, struct triple *ins)
5763 if (is_const(RHS(ins, 0))) {
5764 mkconst(state, ins, RHS(ins, 0)->u.cval);
5767 mkcopy(state, ins, RHS(ins, 0));
5771 static void simplify_neg(struct compile_state *state, struct triple *ins)
5773 if (is_const(RHS(ins, 0))) {
5775 left = read_const(state, ins, &RHS(ins, 0));
5776 mkconst(state, ins, -left);
5778 else if (RHS(ins, 0)->op == OP_NEG) {
5779 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
5783 static void simplify_invert(struct compile_state *state, struct triple *ins)
5785 if (is_const(RHS(ins, 0))) {
5787 left = read_const(state, ins, &RHS(ins, 0));
5788 mkconst(state, ins, ~left);
5792 static void simplify_eq(struct compile_state *state, struct triple *ins)
5794 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5795 ulong_t left, right;
5796 left = read_const(state, ins, &RHS(ins, 0));
5797 right = read_const(state, ins, &RHS(ins, 1));
5798 mkconst(state, ins, left == right);
5800 else if (RHS(ins, 0) == RHS(ins, 1)) {
5801 mkconst(state, ins, 1);
5805 static void simplify_noteq(struct compile_state *state, struct triple *ins)
5807 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5808 ulong_t left, right;
5809 left = read_const(state, ins, &RHS(ins, 0));
5810 right = read_const(state, ins, &RHS(ins, 1));
5811 mkconst(state, ins, left != right);
5813 else if (RHS(ins, 0) == RHS(ins, 1)) {
5814 mkconst(state, ins, 0);
5818 static void simplify_sless(struct compile_state *state, struct triple *ins)
5820 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5822 left = read_sconst(ins, &RHS(ins, 0));
5823 right = read_sconst(ins, &RHS(ins, 1));
5824 mkconst(state, ins, left < right);
5826 else if (RHS(ins, 0) == RHS(ins, 1)) {
5827 mkconst(state, ins, 0);
5831 static void simplify_uless(struct compile_state *state, struct triple *ins)
5833 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5834 ulong_t left, right;
5835 left = read_const(state, ins, &RHS(ins, 0));
5836 right = read_const(state, ins, &RHS(ins, 1));
5837 mkconst(state, ins, left < right);
5839 else if (is_zero(RHS(ins, 0))) {
5840 mkconst(state, ins, 1);
5842 else if (RHS(ins, 0) == RHS(ins, 1)) {
5843 mkconst(state, ins, 0);
5847 static void simplify_smore(struct compile_state *state, struct triple *ins)
5849 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5851 left = read_sconst(ins, &RHS(ins, 0));
5852 right = read_sconst(ins, &RHS(ins, 1));
5853 mkconst(state, ins, left > right);
5855 else if (RHS(ins, 0) == RHS(ins, 1)) {
5856 mkconst(state, ins, 0);
5860 static void simplify_umore(struct compile_state *state, struct triple *ins)
5862 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5863 ulong_t left, right;
5864 left = read_const(state, ins, &RHS(ins, 0));
5865 right = read_const(state, ins, &RHS(ins, 1));
5866 mkconst(state, ins, left > right);
5868 else if (is_zero(RHS(ins, 1))) {
5869 mkconst(state, ins, 1);
5871 else if (RHS(ins, 0) == RHS(ins, 1)) {
5872 mkconst(state, ins, 0);
5877 static void simplify_slesseq(struct compile_state *state, struct triple *ins)
5879 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5881 left = read_sconst(ins, &RHS(ins, 0));
5882 right = read_sconst(ins, &RHS(ins, 1));
5883 mkconst(state, ins, left <= right);
5885 else if (RHS(ins, 0) == RHS(ins, 1)) {
5886 mkconst(state, ins, 1);
5890 static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
5892 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5893 ulong_t left, right;
5894 left = read_const(state, ins, &RHS(ins, 0));
5895 right = read_const(state, ins, &RHS(ins, 1));
5896 mkconst(state, ins, left <= right);
5898 else if (is_zero(RHS(ins, 0))) {
5899 mkconst(state, ins, 1);
5901 else if (RHS(ins, 0) == RHS(ins, 1)) {
5902 mkconst(state, ins, 1);
5906 static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
5908 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 0))) {
5910 left = read_sconst(ins, &RHS(ins, 0));
5911 right = read_sconst(ins, &RHS(ins, 1));
5912 mkconst(state, ins, left >= right);
5914 else if (RHS(ins, 0) == RHS(ins, 1)) {
5915 mkconst(state, ins, 1);
5919 static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
5921 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5922 ulong_t left, right;
5923 left = read_const(state, ins, &RHS(ins, 0));
5924 right = read_const(state, ins, &RHS(ins, 1));
5925 mkconst(state, ins, left >= right);
5927 else if (is_zero(RHS(ins, 1))) {
5928 mkconst(state, ins, 1);
5930 else if (RHS(ins, 0) == RHS(ins, 1)) {
5931 mkconst(state, ins, 1);
5935 static void simplify_lfalse(struct compile_state *state, struct triple *ins)
5937 if (is_const(RHS(ins, 0))) {
5939 left = read_const(state, ins, &RHS(ins, 0));
5940 mkconst(state, ins, left == 0);
5942 /* Otherwise if I am the only user... */
5943 else if ((RHS(ins, 0)->use->member == ins) && (RHS(ins, 0)->use->next == 0)) {
5945 /* Invert a boolean operation */
5946 switch(RHS(ins, 0)->op) {
5947 case OP_LTRUE: RHS(ins, 0)->op = OP_LFALSE; break;
5948 case OP_LFALSE: RHS(ins, 0)->op = OP_LTRUE; break;
5949 case OP_EQ: RHS(ins, 0)->op = OP_NOTEQ; break;
5950 case OP_NOTEQ: RHS(ins, 0)->op = OP_EQ; break;
5951 case OP_SLESS: RHS(ins, 0)->op = OP_SMOREEQ; break;
5952 case OP_ULESS: RHS(ins, 0)->op = OP_UMOREEQ; break;
5953 case OP_SMORE: RHS(ins, 0)->op = OP_SLESSEQ; break;
5954 case OP_UMORE: RHS(ins, 0)->op = OP_ULESSEQ; break;
5955 case OP_SLESSEQ: RHS(ins, 0)->op = OP_SMORE; break;
5956 case OP_ULESSEQ: RHS(ins, 0)->op = OP_UMORE; break;
5957 case OP_SMOREEQ: RHS(ins, 0)->op = OP_SLESS; break;
5958 case OP_UMOREEQ: RHS(ins, 0)->op = OP_ULESS; break;
5964 mkcopy(state, ins, RHS(ins, 0));
5969 static void simplify_ltrue (struct compile_state *state, struct triple *ins)
5971 if (is_const(RHS(ins, 0))) {
5973 left = read_const(state, ins, &RHS(ins, 0));
5974 mkconst(state, ins, left != 0);
5976 else switch(RHS(ins, 0)->op) {
5977 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
5978 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
5979 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
5980 mkcopy(state, ins, RHS(ins, 0));
5985 static void simplify_copy(struct compile_state *state, struct triple *ins)
5987 if (is_const(RHS(ins, 0))) {
5988 switch(RHS(ins, 0)->op) {
5992 left = read_const(state, ins, &RHS(ins, 0));
5993 mkconst(state, ins, left);
5998 struct triple *sdecl;
6000 sdecl = MISC(RHS(ins, 0), 0);
6001 offset = RHS(ins, 0)->u.cval;
6002 mkaddr_const(state, ins, sdecl, offset);
6006 internal_error(state, ins, "uknown constant");
6012 static void simplify_branch(struct compile_state *state, struct triple *ins)
6014 struct block *block;
6015 if (ins->op != OP_BRANCH) {
6016 internal_error(state, ins, "not branch");
6018 if (ins->use != 0) {
6019 internal_error(state, ins, "branch use");
6021 #warning "FIXME implement simplify branch."
6022 /* The challenge here with simplify branch is that I need to
6023 * make modifications to the control flow graph as well
6024 * as to the branch instruction itself.
6026 block = ins->u.block;
6028 if (TRIPLE_RHS(ins->sizes) && is_const(RHS(ins, 0))) {
6029 struct triple *targ;
6031 value = read_const(state, ins, &RHS(ins, 0));
6032 unuse_triple(RHS(ins, 0), ins);
6033 targ = TARG(ins, 0);
6034 ins->sizes = TRIPLE_SIZES(0, 0, 0, 1);
6036 unuse_triple(ins->next, ins);
6037 TARG(ins, 0) = targ;
6040 unuse_triple(targ, ins);
6041 TARG(ins, 0) = ins->next;
6043 #warning "FIXME handle the case of making a branch unconditional"
6045 if (TARG(ins, 0) == ins->next) {
6046 unuse_triple(ins->next, ins);
6047 if (TRIPLE_RHS(ins->sizes)) {
6048 unuse_triple(RHS(ins, 0), ins);
6049 unuse_triple(ins->next, ins);
6051 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
6054 internal_error(state, ins, "noop use != 0");
6056 #warning "FIXME handle the case of killing a branch"
6060 static void simplify_phi(struct compile_state *state, struct triple *ins)
6062 struct triple **expr;
6064 expr = triple_rhs(state, ins, 0);
6065 if (!*expr || !is_const(*expr)) {
6068 value = read_const(state, ins, expr);
6069 for(;expr;expr = triple_rhs(state, ins, expr)) {
6070 if (!*expr || !is_const(*expr)) {
6073 if (value != read_const(state, ins, expr)) {
6077 mkconst(state, ins, value);
6081 static void simplify_bsf(struct compile_state *state, struct triple *ins)
6083 if (is_const(RHS(ins, 0))) {
6085 left = read_const(state, ins, &RHS(ins, 0));
6086 mkconst(state, ins, bsf(left));
6090 static void simplify_bsr(struct compile_state *state, struct triple *ins)
6092 if (is_const(RHS(ins, 0))) {
6094 left = read_const(state, ins, &RHS(ins, 0));
6095 mkconst(state, ins, bsr(left));
6100 typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
6101 static const simplify_t table_simplify[] = {
6103 #define simplify_smul simplify_noop
6104 #define simplify_umul simplify_noop
6105 #define simplify_sdiv simplify_noop
6106 #define simplify_udiv simplify_noop
6107 #define simplify_smod simplify_noop
6108 #define simplify_umod simplify_noop
6111 #define simplify_add simplify_noop
6112 #define simplify_sub simplify_noop
6115 #define simplify_sl simplify_noop
6116 #define simplify_usr simplify_noop
6117 #define simplify_ssr simplify_noop
6120 #define simplify_and simplify_noop
6121 #define simplify_xor simplify_noop
6122 #define simplify_or simplify_noop
6125 #define simplify_pos simplify_noop
6126 #define simplify_neg simplify_noop
6127 #define simplify_invert simplify_noop
6131 #define simplify_eq simplify_noop
6132 #define simplify_noteq simplify_noop
6135 #define simplify_sless simplify_noop
6136 #define simplify_uless simplify_noop
6137 #define simplify_smore simplify_noop
6138 #define simplify_umore simplify_noop
6141 #define simplify_slesseq simplify_noop
6142 #define simplify_ulesseq simplify_noop
6143 #define simplify_smoreeq simplify_noop
6144 #define simplify_umoreeq simplify_noop
6147 #define simplify_lfalse simplify_noop
6150 #define simplify_ltrue simplify_noop
6154 #define simplify_copy simplify_noop
6158 #define simplify_branch simplify_noop
6162 #define simplify_phi simplify_noop
6166 #define simplify_bsf simplify_noop
6167 #define simplify_bsr simplify_noop
6170 [OP_SMUL ] = simplify_smul,
6171 [OP_UMUL ] = simplify_umul,
6172 [OP_SDIV ] = simplify_sdiv,
6173 [OP_UDIV ] = simplify_udiv,
6174 [OP_SMOD ] = simplify_smod,
6175 [OP_UMOD ] = simplify_umod,
6176 [OP_ADD ] = simplify_add,
6177 [OP_SUB ] = simplify_sub,
6178 [OP_SL ] = simplify_sl,
6179 [OP_USR ] = simplify_usr,
6180 [OP_SSR ] = simplify_ssr,
6181 [OP_AND ] = simplify_and,
6182 [OP_XOR ] = simplify_xor,
6183 [OP_OR ] = simplify_or,
6184 [OP_POS ] = simplify_pos,
6185 [OP_NEG ] = simplify_neg,
6186 [OP_INVERT ] = simplify_invert,
6188 [OP_EQ ] = simplify_eq,
6189 [OP_NOTEQ ] = simplify_noteq,
6190 [OP_SLESS ] = simplify_sless,
6191 [OP_ULESS ] = simplify_uless,
6192 [OP_SMORE ] = simplify_smore,
6193 [OP_UMORE ] = simplify_umore,
6194 [OP_SLESSEQ ] = simplify_slesseq,
6195 [OP_ULESSEQ ] = simplify_ulesseq,
6196 [OP_SMOREEQ ] = simplify_smoreeq,
6197 [OP_UMOREEQ ] = simplify_umoreeq,
6198 [OP_LFALSE ] = simplify_lfalse,
6199 [OP_LTRUE ] = simplify_ltrue,
6201 [OP_LOAD ] = simplify_noop,
6202 [OP_STORE ] = simplify_noop,
6204 [OP_NOOP ] = simplify_noop,
6206 [OP_INTCONST ] = simplify_noop,
6207 [OP_BLOBCONST ] = simplify_noop,
6208 [OP_ADDRCONST ] = simplify_noop,
6210 [OP_WRITE ] = simplify_noop,
6211 [OP_READ ] = simplify_noop,
6212 [OP_COPY ] = simplify_copy,
6213 [OP_PIECE ] = simplify_noop,
6214 [OP_ASM ] = simplify_noop,
6216 [OP_DOT ] = simplify_noop,
6217 [OP_VAL_VEC ] = simplify_noop,
6219 [OP_LIST ] = simplify_noop,
6220 [OP_BRANCH ] = simplify_branch,
6221 [OP_LABEL ] = simplify_noop,
6222 [OP_ADECL ] = simplify_noop,
6223 [OP_SDECL ] = simplify_noop,
6224 [OP_PHI ] = simplify_phi,
6226 [OP_INB ] = simplify_noop,
6227 [OP_INW ] = simplify_noop,
6228 [OP_INL ] = simplify_noop,
6229 [OP_OUTB ] = simplify_noop,
6230 [OP_OUTW ] = simplify_noop,
6231 [OP_OUTL ] = simplify_noop,
6232 [OP_BSF ] = simplify_bsf,
6233 [OP_BSR ] = simplify_bsr,
6234 [OP_RDMSR ] = simplify_noop,
6235 [OP_WRMSR ] = simplify_noop,
6236 [OP_HLT ] = simplify_noop,
6239 static void simplify(struct compile_state *state, struct triple *ins)
6242 simplify_t do_simplify;
6246 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
6250 do_simplify = table_simplify[op];
6253 internal_error(state, ins, "cannot simplify op: %d %s\n",
6257 do_simplify(state, ins);
6258 } while(ins->op != op);
6261 static void simplify_all(struct compile_state *state)
6263 struct triple *ins, *first;
6264 first = RHS(state->main_function, 0);
6267 simplify(state, ins);
6269 } while(ins != first);
6274 * ============================
6277 static void register_builtin_function(struct compile_state *state,
6278 const char *name, int op, struct type *rtype, ...)
6280 struct type *ftype, *atype, *param, **next;
6281 struct triple *def, *arg, *result, *work, *last, *first;
6282 struct hash_entry *ident;
6283 struct file_state file;
6289 /* Dummy file state to get debug handling right */
6290 memset(&file, 0, sizeof(file));
6291 file.basename = name;
6293 file.prev = state->file;
6294 state->file = &file;
6296 /* Find the Parameter count */
6297 valid_op(state, op);
6298 parameters = table_ops[op].rhs;
6299 if (parameters < 0 ) {
6300 internal_error(state, 0, "Invalid builtin parameter count");
6303 /* Find the function type */
6304 ftype = new_type(TYPE_FUNCTION, rtype, 0);
6305 next = &ftype->right;
6306 va_start(args, rtype);
6307 for(i = 0; i < parameters; i++) {
6308 atype = va_arg(args, struct type *);
6312 *next = new_type(TYPE_PRODUCT, *next, atype);
6313 next = &((*next)->right);
6321 /* Generate the needed triples */
6322 def = triple(state, OP_LIST, ftype, 0, 0);
6323 first = label(state);
6324 RHS(def, 0) = first;
6326 /* Now string them together */
6327 param = ftype->right;
6328 for(i = 0; i < parameters; i++) {
6329 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6330 atype = param->left;
6334 arg = flatten(state, first, variable(state, atype));
6335 param = param->right;
6338 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
6339 result = flatten(state, first, variable(state, rtype));
6341 MISC(def, 0) = result;
6342 work = new_triple(state, op, rtype, -1, parameters);
6343 for(i = 0, arg = first->next; i < parameters; i++, arg = arg->next) {
6344 RHS(work, i) = read_expr(state, arg);
6346 if (result && ((rtype->type & TYPE_MASK) == TYPE_STRUCT)) {
6348 /* Populate the LHS with the target registers */
6349 work = flatten(state, first, work);
6350 work->type = &void_type;
6351 param = rtype->left;
6352 if (rtype->elements != TRIPLE_LHS(work->sizes)) {
6353 internal_error(state, 0, "Invalid result type");
6355 val = new_triple(state, OP_VAL_VEC, rtype, -1, -1);
6356 for(i = 0; i < rtype->elements; i++) {
6357 struct triple *piece;
6359 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6360 atype = param->left;
6362 if (!TYPE_ARITHMETIC(atype->type) &&
6363 !TYPE_PTR(atype->type)) {
6364 internal_error(state, 0, "Invalid lhs type");
6366 piece = triple(state, OP_PIECE, atype, work, 0);
6368 LHS(work, i) = piece;
6369 RHS(val, i) = piece;
6374 work = write_expr(state, result, work);
6376 work = flatten(state, first, work);
6377 last = flatten(state, first, label(state));
6378 name_len = strlen(name);
6379 ident = lookup(state, name, name_len);
6380 symbol(state, ident, &ident->sym_ident, def, ftype);
6382 state->file = file.prev;
6384 fprintf(stdout, "\n");
6385 loc(stdout, state, 0);
6386 fprintf(stdout, "\n__________ builtin_function _________\n");
6387 print_triple(state, def);
6388 fprintf(stdout, "__________ builtin_function _________ done\n\n");
6392 static struct type *partial_struct(struct compile_state *state,
6393 const char *field_name, struct type *type, struct type *rest)
6395 struct hash_entry *field_ident;
6396 struct type *result;
6399 field_name_len = strlen(field_name);
6400 field_ident = lookup(state, field_name, field_name_len);
6402 result = clone_type(0, type);
6403 result->field_ident = field_ident;
6406 result = new_type(TYPE_PRODUCT, result, rest);
6411 static struct type *register_builtin_type(struct compile_state *state,
6412 const char *name, struct type *type)
6414 struct hash_entry *ident;
6417 name_len = strlen(name);
6418 ident = lookup(state, name, name_len);
6420 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6421 ulong_t elements = 0;
6423 type = new_type(TYPE_STRUCT, type, 0);
6425 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
6427 field = field->right;
6430 symbol(state, ident, &ident->sym_struct, 0, type);
6431 type->type_ident = ident;
6432 type->elements = elements;
6434 symbol(state, ident, &ident->sym_ident, 0, type);
6435 ident->tok = TOK_TYPE_NAME;
6440 static void register_builtins(struct compile_state *state)
6442 struct type *msr_type;
6444 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
6446 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
6448 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
6451 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
6452 &uchar_type, &ushort_type);
6453 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
6454 &ushort_type, &ushort_type);
6455 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
6456 &uint_type, &ushort_type);
6458 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
6460 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
6463 msr_type = register_builtin_type(state, "__builtin_msr_t",
6464 partial_struct(state, "lo", &ulong_type,
6465 partial_struct(state, "hi", &ulong_type, 0)));
6467 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
6469 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
6470 &ulong_type, &ulong_type, &ulong_type);
6472 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
6476 static struct type *declarator(
6477 struct compile_state *state, struct type *type,
6478 struct hash_entry **ident, int need_ident);
6479 static void decl(struct compile_state *state, struct triple *first);
6480 static struct type *specifier_qualifier_list(struct compile_state *state);
6481 static int isdecl_specifier(int tok);
6482 static struct type *decl_specifiers(struct compile_state *state);
6483 static int istype(int tok);
6484 static struct triple *expr(struct compile_state *state);
6485 static struct triple *assignment_expr(struct compile_state *state);
6486 static struct type *type_name(struct compile_state *state);
6487 static void statement(struct compile_state *state, struct triple *fist);
6489 static struct triple *call_expr(
6490 struct compile_state *state, struct triple *func)
6493 struct type *param, *type;
6494 ulong_t pvals, index;
6496 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
6497 error(state, 0, "Called object is not a function");
6499 if (func->op != OP_LIST) {
6500 internal_error(state, 0, "improper function");
6502 eat(state, TOK_LPAREN);
6503 /* Find the return type without any specifiers */
6504 type = clone_type(0, func->type->left);
6505 def = new_triple(state, OP_CALL, func->type, -1, -1);
6508 pvals = TRIPLE_RHS(def->sizes);
6509 MISC(def, 0) = func;
6511 param = func->type->right;
6512 for(index = 0; index < pvals; index++) {
6514 struct type *arg_type;
6515 val = read_expr(state, assignment_expr(state));
6517 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6518 arg_type = param->left;
6520 write_compatible(state, arg_type, val->type);
6521 RHS(def, index) = val;
6522 if (index != (pvals - 1)) {
6523 eat(state, TOK_COMMA);
6524 param = param->right;
6527 eat(state, TOK_RPAREN);
6532 static struct triple *character_constant(struct compile_state *state)
6536 const signed char *str, *end;
6539 eat(state, TOK_LIT_CHAR);
6540 tk = &state->token[0];
6541 str = tk->val.str + 1;
6542 str_len = tk->str_len - 2;
6544 error(state, 0, "empty character constant");
6546 end = str + str_len;
6547 c = char_value(state, &str, end);
6549 error(state, 0, "multibyte character constant not supported");
6551 def = int_const(state, &char_type, (ulong_t)((long_t)c));
6555 static struct triple *string_constant(struct compile_state *state)
6560 const signed char *str, *end;
6561 signed char *buf, *ptr;
6565 type = new_type(TYPE_ARRAY, &char_type, 0);
6567 /* The while loop handles string concatenation */
6569 eat(state, TOK_LIT_STRING);
6570 tk = &state->token[0];
6571 str = tk->val.str + 1;
6572 str_len = tk->str_len - 2;
6574 error(state, 0, "negative string constant length");
6576 end = str + str_len;
6578 buf = xmalloc(type->elements + str_len + 1, "string_constant");
6579 memcpy(buf, ptr, type->elements);
6580 ptr = buf + type->elements;
6582 *ptr++ = char_value(state, &str, end);
6584 type->elements = ptr - buf;
6585 } while(peek(state) == TOK_LIT_STRING);
6587 type->elements += 1;
6588 def = triple(state, OP_BLOBCONST, type, 0, 0);
6594 static struct triple *integer_constant(struct compile_state *state)
6603 eat(state, TOK_LIT_INT);
6604 tk = &state->token[0];
6606 decimal = (tk->val.str[0] != '0');
6607 val = strtoul(tk->val.str, &end, 0);
6608 if ((val == ULONG_MAX) && (errno == ERANGE)) {
6609 error(state, 0, "Integer constant to large");
6612 if ((*end == 'u') || (*end == 'U')) {
6616 if ((*end == 'l') || (*end == 'L')) {
6620 if ((*end == 'u') || (*end == 'U')) {
6625 error(state, 0, "Junk at end of integer constant");
6632 if (!decimal && (val > LONG_MAX)) {
6638 if (val > UINT_MAX) {
6644 if (!decimal && (val > INT_MAX) && (val <= UINT_MAX)) {
6647 else if (!decimal && (val > LONG_MAX)) {
6650 else if (val > INT_MAX) {
6654 def = int_const(state, type, val);
6658 static struct triple *primary_expr(struct compile_state *state)
6666 struct hash_entry *ident;
6667 /* Here ident is either:
6670 * an enumeration constant.
6672 eat(state, TOK_IDENT);
6673 ident = state->token[0].ident;
6674 if (!ident->sym_ident) {
6675 error(state, 0, "%s undeclared", ident->name);
6677 def = ident->sym_ident->def;
6680 case TOK_ENUM_CONST:
6681 /* Here ident is an enumeration constant */
6682 eat(state, TOK_ENUM_CONST);
6687 eat(state, TOK_LPAREN);
6689 eat(state, TOK_RPAREN);
6692 def = integer_constant(state);
6695 eat(state, TOK_LIT_FLOAT);
6696 error(state, 0, "Floating point constants not supported");
6701 def = character_constant(state);
6703 case TOK_LIT_STRING:
6704 def = string_constant(state);
6708 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
6713 static struct triple *postfix_expr(struct compile_state *state)
6717 def = primary_expr(state);
6719 struct triple *left;
6723 switch((tok = peek(state))) {
6725 eat(state, TOK_LBRACKET);
6726 def = mk_subscript_expr(state, left, expr(state));
6727 eat(state, TOK_RBRACKET);
6730 def = call_expr(state, def);
6734 struct hash_entry *field;
6735 eat(state, TOK_DOT);
6736 eat(state, TOK_IDENT);
6737 field = state->token[0].ident;
6738 def = deref_field(state, def, field);
6743 struct hash_entry *field;
6744 eat(state, TOK_ARROW);
6745 eat(state, TOK_IDENT);
6746 field = state->token[0].ident;
6747 def = mk_deref_expr(state, read_expr(state, def));
6748 def = deref_field(state, def, field);
6752 eat(state, TOK_PLUSPLUS);
6753 def = mk_post_inc_expr(state, left);
6755 case TOK_MINUSMINUS:
6756 eat(state, TOK_MINUSMINUS);
6757 def = mk_post_dec_expr(state, left);
6767 static struct triple *cast_expr(struct compile_state *state);
6769 static struct triple *unary_expr(struct compile_state *state)
6771 struct triple *def, *right;
6773 switch((tok = peek(state))) {
6775 eat(state, TOK_PLUSPLUS);
6776 def = mk_pre_inc_expr(state, unary_expr(state));
6778 case TOK_MINUSMINUS:
6779 eat(state, TOK_MINUSMINUS);
6780 def = mk_pre_dec_expr(state, unary_expr(state));
6783 eat(state, TOK_AND);
6784 def = mk_addr_expr(state, cast_expr(state), 0);
6787 eat(state, TOK_STAR);
6788 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
6791 eat(state, TOK_PLUS);
6792 right = read_expr(state, cast_expr(state));
6793 arithmetic(state, right);
6794 def = integral_promotion(state, right);
6797 eat(state, TOK_MINUS);
6798 right = read_expr(state, cast_expr(state));
6799 arithmetic(state, right);
6800 def = integral_promotion(state, right);
6801 def = triple(state, OP_NEG, def->type, def, 0);
6804 eat(state, TOK_TILDE);
6805 right = read_expr(state, cast_expr(state));
6806 integral(state, right);
6807 def = integral_promotion(state, right);
6808 def = triple(state, OP_INVERT, def->type, def, 0);
6811 eat(state, TOK_BANG);
6812 right = read_expr(state, cast_expr(state));
6814 def = lfalse_expr(state, right);
6820 eat(state, TOK_SIZEOF);
6822 tok2 = peek2(state);
6823 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
6824 eat(state, TOK_LPAREN);
6825 type = type_name(state);
6826 eat(state, TOK_RPAREN);
6829 struct triple *expr;
6830 expr = unary_expr(state);
6832 release_expr(state, expr);
6834 def = int_const(state, &ulong_type, size_of(state, type));
6841 eat(state, TOK_ALIGNOF);
6843 tok2 = peek2(state);
6844 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
6845 eat(state, TOK_LPAREN);
6846 type = type_name(state);
6847 eat(state, TOK_RPAREN);
6850 struct triple *expr;
6851 expr = unary_expr(state);
6853 release_expr(state, expr);
6855 def = int_const(state, &ulong_type, align_of(state, type));
6859 def = postfix_expr(state);
6865 static struct triple *cast_expr(struct compile_state *state)
6870 tok2 = peek2(state);
6871 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
6873 eat(state, TOK_LPAREN);
6874 type = type_name(state);
6875 eat(state, TOK_RPAREN);
6876 def = read_expr(state, cast_expr(state));
6877 def = triple(state, OP_COPY, type, def, 0);
6880 def = unary_expr(state);
6885 static struct triple *mult_expr(struct compile_state *state)
6889 def = cast_expr(state);
6891 struct triple *left, *right;
6892 struct type *result_type;
6895 switch(tok = (peek(state))) {
6899 left = read_expr(state, def);
6900 arithmetic(state, left);
6904 right = read_expr(state, cast_expr(state));
6905 arithmetic(state, right);
6907 result_type = arithmetic_result(state, left, right);
6908 sign = is_signed(result_type);
6911 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
6912 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
6913 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
6915 def = triple(state, op, result_type, left, right);
6925 static struct triple *add_expr(struct compile_state *state)
6929 def = mult_expr(state);
6932 switch( peek(state)) {
6934 eat(state, TOK_PLUS);
6935 def = mk_add_expr(state, def, mult_expr(state));
6938 eat(state, TOK_MINUS);
6939 def = mk_sub_expr(state, def, mult_expr(state));
6949 static struct triple *shift_expr(struct compile_state *state)
6953 def = add_expr(state);
6955 struct triple *left, *right;
6958 switch((tok = peek(state))) {
6961 left = read_expr(state, def);
6962 integral(state, left);
6963 left = integral_promotion(state, left);
6967 right = read_expr(state, add_expr(state));
6968 integral(state, right);
6969 right = integral_promotion(state, right);
6971 op = (tok == TOK_SL)? OP_SL :
6972 is_signed(left->type)? OP_SSR: OP_USR;
6974 def = triple(state, op, left->type, left, right);
6984 static struct triple *relational_expr(struct compile_state *state)
6986 #warning "Extend relational exprs to work on more than arithmetic types"
6989 def = shift_expr(state);
6991 struct triple *left, *right;
6992 struct type *arg_type;
6995 switch((tok = peek(state))) {
7000 left = read_expr(state, def);
7001 arithmetic(state, left);
7005 right = read_expr(state, shift_expr(state));
7006 arithmetic(state, right);
7008 arg_type = arithmetic_result(state, left, right);
7009 sign = is_signed(arg_type);
7012 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
7013 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
7014 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
7015 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
7017 def = triple(state, op, &int_type, left, right);
7027 static struct triple *equality_expr(struct compile_state *state)
7029 #warning "Extend equality exprs to work on more than arithmetic types"
7032 def = relational_expr(state);
7034 struct triple *left, *right;
7037 switch((tok = peek(state))) {
7040 left = read_expr(state, def);
7041 arithmetic(state, left);
7043 right = read_expr(state, relational_expr(state));
7044 arithmetic(state, right);
7045 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
7046 def = triple(state, op, &int_type, left, right);
7056 static struct triple *and_expr(struct compile_state *state)
7059 def = equality_expr(state);
7060 while(peek(state) == TOK_AND) {
7061 struct triple *left, *right;
7062 struct type *result_type;
7063 left = read_expr(state, def);
7064 integral(state, left);
7065 eat(state, TOK_AND);
7066 right = read_expr(state, equality_expr(state));
7067 integral(state, right);
7068 result_type = arithmetic_result(state, left, right);
7069 def = triple(state, OP_AND, result_type, left, right);
7074 static struct triple *xor_expr(struct compile_state *state)
7077 def = and_expr(state);
7078 while(peek(state) == TOK_XOR) {
7079 struct triple *left, *right;
7080 struct type *result_type;
7081 left = read_expr(state, def);
7082 integral(state, left);
7083 eat(state, TOK_XOR);
7084 right = read_expr(state, and_expr(state));
7085 integral(state, right);
7086 result_type = arithmetic_result(state, left, right);
7087 def = triple(state, OP_XOR, result_type, left, right);
7092 static struct triple *or_expr(struct compile_state *state)
7095 def = xor_expr(state);
7096 while(peek(state) == TOK_OR) {
7097 struct triple *left, *right;
7098 struct type *result_type;
7099 left = read_expr(state, def);
7100 integral(state, left);
7102 right = read_expr(state, xor_expr(state));
7103 integral(state, right);
7104 result_type = arithmetic_result(state, left, right);
7105 def = triple(state, OP_OR, result_type, left, right);
7110 static struct triple *land_expr(struct compile_state *state)
7113 def = or_expr(state);
7114 while(peek(state) == TOK_LOGAND) {
7115 struct triple *left, *right;
7116 left = read_expr(state, def);
7118 eat(state, TOK_LOGAND);
7119 right = read_expr(state, or_expr(state));
7122 def = triple(state, OP_LAND, &int_type,
7123 ltrue_expr(state, left),
7124 ltrue_expr(state, right));
7129 static struct triple *lor_expr(struct compile_state *state)
7132 def = land_expr(state);
7133 while(peek(state) == TOK_LOGOR) {
7134 struct triple *left, *right;
7135 left = read_expr(state, def);
7137 eat(state, TOK_LOGOR);
7138 right = read_expr(state, land_expr(state));
7141 def = triple(state, OP_LOR, &int_type,
7142 ltrue_expr(state, left),
7143 ltrue_expr(state, right));
7148 static struct triple *conditional_expr(struct compile_state *state)
7151 def = lor_expr(state);
7152 if (peek(state) == TOK_QUEST) {
7153 struct triple *test, *left, *right;
7155 test = ltrue_expr(state, read_expr(state, def));
7156 eat(state, TOK_QUEST);
7157 left = read_expr(state, expr(state));
7158 eat(state, TOK_COLON);
7159 right = read_expr(state, conditional_expr(state));
7161 def = cond_expr(state, test, left, right);
7166 static struct triple *eval_const_expr(
7167 struct compile_state *state, struct triple *expr)
7170 struct triple *head, *ptr;
7171 head = label(state); /* dummy initial triple */
7172 flatten(state, head, expr);
7173 for(ptr = head->next; ptr != head; ptr = ptr->next) {
7174 simplify(state, ptr);
7176 /* Remove the constant value the tail of the list */
7178 def->prev->next = def->next;
7179 def->next->prev = def->prev;
7180 def->next = def->prev = def;
7181 if (!is_const(def)) {
7182 internal_error(state, 0, "Not a constant expression");
7184 /* Free the intermediate expressions */
7185 while(head->next != head) {
7186 release_triple(state, head->next);
7188 free_triple(state, head);
7192 static struct triple *constant_expr(struct compile_state *state)
7194 return eval_const_expr(state, conditional_expr(state));
7197 static struct triple *assignment_expr(struct compile_state *state)
7199 struct triple *def, *left, *right;
7201 /* The C grammer in K&R shows assignment expressions
7202 * only taking unary expressions as input on their
7203 * left hand side. But specifies the precedence of
7204 * assignemnt as the lowest operator except for comma.
7206 * Allowing conditional expressions on the left hand side
7207 * of an assignement results in a grammar that accepts
7208 * a larger set of statements than standard C. As long
7209 * as the subset of the grammar that is standard C behaves
7210 * correctly this should cause no problems.
7212 * For the extra token strings accepted by the grammar
7213 * none of them should produce a valid lvalue, so they
7214 * should not produce functioning programs.
7216 * GCC has this bug as well, so surprises should be minimal.
7218 def = conditional_expr(state);
7220 switch((tok = peek(state))) {
7222 lvalue(state, left);
7224 def = write_expr(state, left,
7225 read_expr(state, assignment_expr(state)));
7230 lvalue(state, left);
7231 arithmetic(state, left);
7233 right = read_expr(state, assignment_expr(state));
7234 arithmetic(state, right);
7236 sign = is_signed(left->type);
7239 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
7240 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
7241 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
7243 def = write_expr(state, left,
7244 triple(state, op, left->type,
7245 read_expr(state, left), right));
7248 lvalue(state, left);
7249 eat(state, TOK_PLUSEQ);
7250 def = write_expr(state, left,
7251 mk_add_expr(state, left, assignment_expr(state)));
7254 lvalue(state, left);
7255 eat(state, TOK_MINUSEQ);
7256 def = write_expr(state, left,
7257 mk_sub_expr(state, left, assignment_expr(state)));
7264 lvalue(state, left);
7265 integral(state, left);
7267 right = read_expr(state, assignment_expr(state));
7268 integral(state, right);
7269 right = integral_promotion(state, right);
7270 sign = is_signed(left->type);
7273 case TOK_SLEQ: op = OP_SL; break;
7274 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
7275 case TOK_ANDEQ: op = OP_AND; break;
7276 case TOK_XOREQ: op = OP_XOR; break;
7277 case TOK_OREQ: op = OP_OR; break;
7279 def = write_expr(state, left,
7280 triple(state, op, left->type,
7281 read_expr(state, left), right));
7287 static struct triple *expr(struct compile_state *state)
7290 def = assignment_expr(state);
7291 while(peek(state) == TOK_COMMA) {
7292 struct triple *left, *right;
7294 eat(state, TOK_COMMA);
7295 right = assignment_expr(state);
7296 def = triple(state, OP_COMMA, right->type, left, right);
7301 static void expr_statement(struct compile_state *state, struct triple *first)
7303 if (peek(state) != TOK_SEMI) {
7304 flatten(state, first, expr(state));
7306 eat(state, TOK_SEMI);
7309 static void if_statement(struct compile_state *state, struct triple *first)
7311 struct triple *test, *jmp1, *jmp2, *middle, *end;
7313 jmp1 = jmp2 = middle = 0;
7315 eat(state, TOK_LPAREN);
7318 /* Cleanup and invert the test */
7319 test = lfalse_expr(state, read_expr(state, test));
7320 eat(state, TOK_RPAREN);
7321 /* Generate the needed pieces */
7322 middle = label(state);
7323 jmp1 = branch(state, middle, test);
7324 /* Thread the pieces together */
7325 flatten(state, first, test);
7326 flatten(state, first, jmp1);
7327 flatten(state, first, label(state));
7328 statement(state, first);
7329 if (peek(state) == TOK_ELSE) {
7330 eat(state, TOK_ELSE);
7331 /* Generate the rest of the pieces */
7333 jmp2 = branch(state, end, 0);
7334 /* Thread them together */
7335 flatten(state, first, jmp2);
7336 flatten(state, first, middle);
7337 statement(state, first);
7338 flatten(state, first, end);
7341 flatten(state, first, middle);
7345 static void for_statement(struct compile_state *state, struct triple *first)
7347 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
7348 struct triple *label1, *label2, *label3;
7349 struct hash_entry *ident;
7351 eat(state, TOK_FOR);
7352 eat(state, TOK_LPAREN);
7353 head = test = tail = jmp1 = jmp2 = 0;
7354 if (peek(state) != TOK_SEMI) {
7357 eat(state, TOK_SEMI);
7358 if (peek(state) != TOK_SEMI) {
7361 test = ltrue_expr(state, read_expr(state, test));
7363 eat(state, TOK_SEMI);
7364 if (peek(state) != TOK_RPAREN) {
7367 eat(state, TOK_RPAREN);
7368 /* Generate the needed pieces */
7369 label1 = label(state);
7370 label2 = label(state);
7371 label3 = label(state);
7373 jmp1 = branch(state, label3, 0);
7374 jmp2 = branch(state, label1, test);
7377 jmp2 = branch(state, label1, 0);
7380 /* Remember where break and continue go */
7382 ident = state->i_break;
7383 symbol(state, ident, &ident->sym_ident, end, end->type);
7384 ident = state->i_continue;
7385 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7386 /* Now include the body */
7387 flatten(state, first, head);
7388 flatten(state, first, jmp1);
7389 flatten(state, first, label1);
7390 statement(state, first);
7391 flatten(state, first, label2);
7392 flatten(state, first, tail);
7393 flatten(state, first, label3);
7394 flatten(state, first, test);
7395 flatten(state, first, jmp2);
7396 flatten(state, first, end);
7397 /* Cleanup the break/continue scope */
7401 static void while_statement(struct compile_state *state, struct triple *first)
7403 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
7404 struct hash_entry *ident;
7405 eat(state, TOK_WHILE);
7406 eat(state, TOK_LPAREN);
7409 test = ltrue_expr(state, read_expr(state, test));
7410 eat(state, TOK_RPAREN);
7411 /* Generate the needed pieces */
7412 label1 = label(state);
7413 label2 = label(state);
7414 jmp1 = branch(state, label2, 0);
7415 jmp2 = branch(state, label1, test);
7417 /* Remember where break and continue go */
7419 ident = state->i_break;
7420 symbol(state, ident, &ident->sym_ident, end, end->type);
7421 ident = state->i_continue;
7422 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7423 /* Thread them together */
7424 flatten(state, first, jmp1);
7425 flatten(state, first, label1);
7426 statement(state, first);
7427 flatten(state, first, label2);
7428 flatten(state, first, test);
7429 flatten(state, first, jmp2);
7430 flatten(state, first, end);
7431 /* Cleanup the break/continue scope */
7435 static void do_statement(struct compile_state *state, struct triple *first)
7437 struct triple *label1, *label2, *test, *end;
7438 struct hash_entry *ident;
7440 /* Generate the needed pieces */
7441 label1 = label(state);
7442 label2 = label(state);
7444 /* Remember where break and continue go */
7446 ident = state->i_break;
7447 symbol(state, ident, &ident->sym_ident, end, end->type);
7448 ident = state->i_continue;
7449 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7450 /* Now include the body */
7451 flatten(state, first, label1);
7452 statement(state, first);
7453 /* Cleanup the break/continue scope */
7455 /* Eat the rest of the loop */
7456 eat(state, TOK_WHILE);
7457 eat(state, TOK_LPAREN);
7458 test = read_expr(state, expr(state));
7460 eat(state, TOK_RPAREN);
7461 eat(state, TOK_SEMI);
7462 /* Thread the pieces together */
7463 test = ltrue_expr(state, test);
7464 flatten(state, first, label2);
7465 flatten(state, first, test);
7466 flatten(state, first, branch(state, label1, test));
7467 flatten(state, first, end);
7471 static void return_statement(struct compile_state *state, struct triple *first)
7473 struct triple *jmp, *mv, *dest, *var, *val;
7475 eat(state, TOK_RETURN);
7477 #warning "FIXME implement a more general excess branch elimination"
7479 /* If we have a return value do some more work */
7480 if (peek(state) != TOK_SEMI) {
7481 val = read_expr(state, expr(state));
7483 eat(state, TOK_SEMI);
7485 /* See if this last statement in a function */
7486 last = ((peek(state) == TOK_RBRACE) &&
7487 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
7489 /* Find the return variable */
7490 var = MISC(state->main_function, 0);
7491 /* Find the return destination */
7492 dest = RHS(state->main_function, 0)->prev;
7494 /* If needed generate a jump instruction */
7496 jmp = branch(state, dest, 0);
7498 /* If needed generate an assignment instruction */
7500 mv = write_expr(state, var, val);
7502 /* Now put the code together */
7504 flatten(state, first, mv);
7505 flatten(state, first, jmp);
7508 flatten(state, first, jmp);
7512 static void break_statement(struct compile_state *state, struct triple *first)
7514 struct triple *dest;
7515 eat(state, TOK_BREAK);
7516 eat(state, TOK_SEMI);
7517 if (!state->i_break->sym_ident) {
7518 error(state, 0, "break statement not within loop or switch");
7520 dest = state->i_break->sym_ident->def;
7521 flatten(state, first, branch(state, dest, 0));
7524 static void continue_statement(struct compile_state *state, struct triple *first)
7526 struct triple *dest;
7527 eat(state, TOK_CONTINUE);
7528 eat(state, TOK_SEMI);
7529 if (!state->i_continue->sym_ident) {
7530 error(state, 0, "continue statement outside of a loop");
7532 dest = state->i_continue->sym_ident->def;
7533 flatten(state, first, branch(state, dest, 0));
7536 static void goto_statement(struct compile_state *state, struct triple *first)
7539 eat(state, TOK_GOTO);
7540 eat(state, TOK_IDENT);
7541 eat(state, TOK_SEMI);
7542 error(state, 0, "goto is not implemeted");
7546 static void labeled_statement(struct compile_state *state, struct triple *first)
7549 eat(state, TOK_IDENT);
7550 eat(state, TOK_COLON);
7551 statement(state, first);
7552 error(state, 0, "labeled statements are not implemented");
7556 static void switch_statement(struct compile_state *state, struct triple *first)
7559 eat(state, TOK_SWITCH);
7560 eat(state, TOK_LPAREN);
7562 eat(state, TOK_RPAREN);
7563 statement(state, first);
7564 error(state, 0, "switch statements are not implemented");
7568 static void case_statement(struct compile_state *state, struct triple *first)
7571 eat(state, TOK_CASE);
7572 constant_expr(state);
7573 eat(state, TOK_COLON);
7574 statement(state, first);
7575 error(state, 0, "case statements are not implemented");
7579 static void default_statement(struct compile_state *state, struct triple *first)
7582 eat(state, TOK_DEFAULT);
7583 eat(state, TOK_COLON);
7584 statement(state, first);
7585 error(state, 0, "default statements are not implemented");
7589 static void asm_statement(struct compile_state *state, struct triple *first)
7591 struct asm_info *info;
7593 struct triple *constraint;
7594 struct triple *expr;
7595 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
7596 struct triple *def, *asm_str;
7597 int out, in, clobbers, more, colons, i;
7599 eat(state, TOK_ASM);
7600 /* For now ignore the qualifiers */
7601 switch(peek(state)) {
7603 eat(state, TOK_CONST);
7606 eat(state, TOK_VOLATILE);
7609 eat(state, TOK_LPAREN);
7610 asm_str = string_constant(state);
7613 out = in = clobbers = 0;
7615 if ((colons == 0) && (peek(state) == TOK_COLON)) {
7616 eat(state, TOK_COLON);
7618 more = (peek(state) == TOK_LIT_STRING);
7621 struct triple *constraint;
7623 if (out > MAX_LHS) {
7624 error(state, 0, "Maximum output count exceeded.");
7626 constraint = string_constant(state);
7627 eat(state, TOK_LPAREN);
7628 var = conditional_expr(state);
7629 eat(state, TOK_RPAREN);
7632 out_param[out].constraint = constraint;
7633 out_param[out].expr = var;
7634 if (peek(state) == TOK_COMMA) {
7635 eat(state, TOK_COMMA);
7642 if ((colons == 1) && (peek(state) == TOK_COLON)) {
7643 eat(state, TOK_COLON);
7645 more = (peek(state) == TOK_LIT_STRING);
7648 struct triple *constraint;
7651 error(state, 0, "Maximum input count exceeded.");
7653 constraint = string_constant(state);
7654 eat(state, TOK_LPAREN);
7655 val = conditional_expr(state);
7656 eat(state, TOK_RPAREN);
7658 in_param[in].constraint = constraint;
7659 in_param[in].expr = val;
7660 if (peek(state) == TOK_COMMA) {
7661 eat(state, TOK_COMMA);
7669 if ((colons == 2) && (peek(state) == TOK_COLON)) {
7670 eat(state, TOK_COLON);
7672 more = (peek(state) == TOK_LIT_STRING);
7674 struct triple *clobber;
7676 if ((clobbers + out) > MAX_LHS) {
7677 error(state, 0, "Maximum clobber limit exceeded.");
7679 clobber = string_constant(state);
7680 eat(state, TOK_RPAREN);
7682 clob_param[clobbers].constraint = clobber;
7683 if (peek(state) == TOK_COMMA) {
7684 eat(state, TOK_COMMA);
7690 eat(state, TOK_RPAREN);
7691 eat(state, TOK_SEMI);
7694 info = xcmalloc(sizeof(*info), "asm_info");
7695 info->str = asm_str->u.blob;
7696 free_triple(state, asm_str);
7698 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
7699 def->u.ainfo = info;
7700 for(i = 0; i < in; i++) {
7701 struct triple *constraint;
7702 constraint = in_param[i].constraint;
7703 info->tmpl.rhs[i] = arch_reg_constraint(state,
7704 in_param[i].expr->type, constraint->u.blob);
7706 RHS(def, i) = read_expr(state,in_param[i].expr);
7707 free_triple(state, constraint);
7709 flatten(state, first, def);
7710 for(i = 0; i < out; i++) {
7711 struct triple *piece;
7712 struct triple *constraint;
7713 constraint = out_param[i].constraint;
7714 info->tmpl.lhs[i] = arch_reg_constraint(state,
7715 out_param[i].expr->type, constraint->u.blob);
7717 piece = triple(state, OP_PIECE, out_param[i].expr->type, def, 0);
7719 LHS(def, i) = piece;
7720 flatten(state, first,
7721 write_expr(state, out_param[i].expr, piece));
7722 free_triple(state, constraint);
7724 for(; i - out < clobbers; i++) {
7725 struct triple *piece;
7726 struct triple *constraint;
7727 constraint = clob_param[i - out].constraint;
7728 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
7730 piece = triple(state, OP_PIECE, &void_type, def, 0);
7732 LHS(def, i) = piece;
7733 flatten(state, first, piece);
7734 free_triple(state, constraint);
7739 static int isdecl(int tok)
7762 case TOK_TYPE_NAME: /* typedef name */
7769 static void compound_statement(struct compile_state *state, struct triple *first)
7771 eat(state, TOK_LBRACE);
7774 /* statement-list opt */
7775 while (peek(state) != TOK_RBRACE) {
7776 statement(state, first);
7779 eat(state, TOK_RBRACE);
7782 static void statement(struct compile_state *state, struct triple *first)
7786 if (tok == TOK_LBRACE) {
7787 compound_statement(state, first);
7789 else if (tok == TOK_IF) {
7790 if_statement(state, first);
7792 else if (tok == TOK_FOR) {
7793 for_statement(state, first);
7795 else if (tok == TOK_WHILE) {
7796 while_statement(state, first);
7798 else if (tok == TOK_DO) {
7799 do_statement(state, first);
7801 else if (tok == TOK_RETURN) {
7802 return_statement(state, first);
7804 else if (tok == TOK_BREAK) {
7805 break_statement(state, first);
7807 else if (tok == TOK_CONTINUE) {
7808 continue_statement(state, first);
7810 else if (tok == TOK_GOTO) {
7811 goto_statement(state, first);
7813 else if (tok == TOK_SWITCH) {
7814 switch_statement(state, first);
7816 else if (tok == TOK_ASM) {
7817 asm_statement(state, first);
7819 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
7820 labeled_statement(state, first);
7822 else if (tok == TOK_CASE) {
7823 case_statement(state, first);
7825 else if (tok == TOK_DEFAULT) {
7826 default_statement(state, first);
7828 else if (isdecl(tok)) {
7829 /* This handles C99 intermixing of statements and decls */
7833 expr_statement(state, first);
7837 static struct type *param_decl(struct compile_state *state)
7840 struct hash_entry *ident;
7841 /* Cheat so the declarator will know we are not global */
7844 type = decl_specifiers(state);
7845 type = declarator(state, type, &ident, 0);
7846 type->field_ident = ident;
7851 static struct type *param_type_list(struct compile_state *state, struct type *type)
7853 struct type *ftype, **next;
7854 ftype = new_type(TYPE_FUNCTION, type, param_decl(state));
7855 next = &ftype->right;
7856 while(peek(state) == TOK_COMMA) {
7857 eat(state, TOK_COMMA);
7858 if (peek(state) == TOK_DOTS) {
7859 eat(state, TOK_DOTS);
7860 error(state, 0, "variadic functions not supported");
7863 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
7864 next = &((*next)->right);
7871 static struct type *type_name(struct compile_state *state)
7874 type = specifier_qualifier_list(state);
7875 /* abstract-declarator (may consume no tokens) */
7876 type = declarator(state, type, 0, 0);
7880 static struct type *direct_declarator(
7881 struct compile_state *state, struct type *type,
7882 struct hash_entry **ident, int need_ident)
7887 arrays_complete(state, type);
7888 switch(peek(state)) {
7890 eat(state, TOK_IDENT);
7892 error(state, 0, "Unexpected identifier found");
7894 /* The name of what we are declaring */
7895 *ident = state->token[0].ident;
7898 eat(state, TOK_LPAREN);
7899 outer = declarator(state, type, ident, need_ident);
7900 eat(state, TOK_RPAREN);
7904 error(state, 0, "Identifier expected");
7910 arrays_complete(state, type);
7911 switch(peek(state)) {
7913 eat(state, TOK_LPAREN);
7914 type = param_type_list(state, type);
7915 eat(state, TOK_RPAREN);
7919 unsigned int qualifiers;
7920 struct triple *value;
7922 eat(state, TOK_LBRACKET);
7923 if (peek(state) != TOK_RBRACKET) {
7924 value = constant_expr(state);
7925 integral(state, value);
7927 eat(state, TOK_RBRACKET);
7929 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
7930 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
7932 type->elements = value->u.cval;
7933 free_triple(state, value);
7935 type->elements = ELEMENT_COUNT_UNSPECIFIED;
7947 arrays_complete(state, type);
7949 for(inner = outer; inner->left; inner = inner->left)
7957 static struct type *declarator(
7958 struct compile_state *state, struct type *type,
7959 struct hash_entry **ident, int need_ident)
7961 while(peek(state) == TOK_STAR) {
7962 eat(state, TOK_STAR);
7963 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
7965 type = direct_declarator(state, type, ident, need_ident);
7970 static struct type *typedef_name(
7971 struct compile_state *state, unsigned int specifiers)
7973 struct hash_entry *ident;
7975 eat(state, TOK_TYPE_NAME);
7976 ident = state->token[0].ident;
7977 type = ident->sym_ident->type;
7978 specifiers |= type->type & QUAL_MASK;
7979 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
7980 (type->type & (STOR_MASK | QUAL_MASK))) {
7981 type = clone_type(specifiers, type);
7986 static struct type *enum_specifier(
7987 struct compile_state *state, unsigned int specifiers)
7993 eat(state, TOK_ENUM);
7995 if (tok == TOK_IDENT) {
7996 eat(state, TOK_IDENT);
7998 if ((tok != TOK_IDENT) || (peek(state) == TOK_LBRACE)) {
7999 eat(state, TOK_LBRACE);
8001 eat(state, TOK_IDENT);
8002 if (peek(state) == TOK_EQ) {
8004 constant_expr(state);
8006 if (peek(state) == TOK_COMMA) {
8007 eat(state, TOK_COMMA);
8009 } while(peek(state) != TOK_RBRACE);
8010 eat(state, TOK_RBRACE);
8017 static struct type *struct_declarator(
8018 struct compile_state *state, struct type *type, struct hash_entry **ident)
8021 #warning "struct_declarator is complicated because of bitfields, kill them?"
8023 if (tok != TOK_COLON) {
8024 type = declarator(state, type, ident, 1);
8026 if ((tok == TOK_COLON) || (peek(state) == TOK_COLON)) {
8027 eat(state, TOK_COLON);
8028 constant_expr(state);
8035 static struct type *struct_or_union_specifier(
8036 struct compile_state *state, unsigned int specifiers)
8038 struct type *struct_type;
8039 struct hash_entry *ident;
8040 unsigned int type_join;
8044 switch(peek(state)) {
8046 eat(state, TOK_STRUCT);
8047 type_join = TYPE_PRODUCT;
8050 eat(state, TOK_UNION);
8051 type_join = TYPE_OVERLAP;
8052 error(state, 0, "unions not yet supported\n");
8055 eat(state, TOK_STRUCT);
8056 type_join = TYPE_PRODUCT;
8060 if ((tok == TOK_IDENT) || (tok == TOK_TYPE_NAME)) {
8062 ident = state->token[0].ident;
8064 if (!ident || (peek(state) == TOK_LBRACE)) {
8067 eat(state, TOK_LBRACE);
8069 struct type *base_type;
8072 base_type = specifier_qualifier_list(state);
8073 next = &struct_type;
8076 struct hash_entry *fident;
8078 type = declarator(state, base_type, &fident, 1);
8080 if (peek(state) == TOK_COMMA) {
8082 eat(state, TOK_COMMA);
8084 type = clone_type(0, type);
8085 type->field_ident = fident;
8087 *next = new_type(type_join, *next, type);
8088 next = &((*next)->right);
8093 eat(state, TOK_SEMI);
8094 } while(peek(state) != TOK_RBRACE);
8095 eat(state, TOK_RBRACE);
8096 struct_type = new_type(TYPE_STRUCT, struct_type, 0);
8097 struct_type->type_ident = ident;
8098 struct_type->elements = elements;
8099 symbol(state, ident, &ident->sym_struct, 0, struct_type);
8101 if (ident && ident->sym_struct) {
8102 struct_type = ident->sym_struct->type;
8104 else if (ident && !ident->sym_struct) {
8105 error(state, 0, "struct %s undeclared", ident->name);
8110 static unsigned int storage_class_specifier_opt(struct compile_state *state)
8112 unsigned int specifiers;
8113 switch(peek(state)) {
8115 eat(state, TOK_AUTO);
8116 specifiers = STOR_AUTO;
8119 eat(state, TOK_REGISTER);
8120 specifiers = STOR_REGISTER;
8123 eat(state, TOK_STATIC);
8124 specifiers = STOR_STATIC;
8127 eat(state, TOK_EXTERN);
8128 specifiers = STOR_EXTERN;
8131 eat(state, TOK_TYPEDEF);
8132 specifiers = STOR_TYPEDEF;
8135 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
8136 specifiers = STOR_STATIC;
8139 specifiers = STOR_AUTO;
8145 static unsigned int function_specifier_opt(struct compile_state *state)
8147 /* Ignore the inline keyword */
8148 unsigned int specifiers;
8150 switch(peek(state)) {
8152 eat(state, TOK_INLINE);
8153 specifiers = STOR_INLINE;
8158 static unsigned int type_qualifiers(struct compile_state *state)
8160 unsigned int specifiers;
8163 specifiers = QUAL_NONE;
8165 switch(peek(state)) {
8167 eat(state, TOK_CONST);
8168 specifiers = QUAL_CONST;
8171 eat(state, TOK_VOLATILE);
8172 specifiers = QUAL_VOLATILE;
8175 eat(state, TOK_RESTRICT);
8176 specifiers = QUAL_RESTRICT;
8186 static struct type *type_specifier(
8187 struct compile_state *state, unsigned int spec)
8191 switch(peek(state)) {
8193 eat(state, TOK_VOID);
8194 type = new_type(TYPE_VOID | spec, 0, 0);
8197 eat(state, TOK_CHAR);
8198 type = new_type(TYPE_CHAR | spec, 0, 0);
8201 eat(state, TOK_SHORT);
8202 if (peek(state) == TOK_INT) {
8203 eat(state, TOK_INT);
8205 type = new_type(TYPE_SHORT | spec, 0, 0);
8208 eat(state, TOK_INT);
8209 type = new_type(TYPE_INT | spec, 0, 0);
8212 eat(state, TOK_LONG);
8213 switch(peek(state)) {
8215 eat(state, TOK_LONG);
8216 error(state, 0, "long long not supported");
8219 eat(state, TOK_DOUBLE);
8220 error(state, 0, "long double not supported");
8223 eat(state, TOK_INT);
8224 type = new_type(TYPE_LONG | spec, 0, 0);
8227 type = new_type(TYPE_LONG | spec, 0, 0);
8232 eat(state, TOK_FLOAT);
8233 error(state, 0, "type float not supported");
8236 eat(state, TOK_DOUBLE);
8237 error(state, 0, "type double not supported");
8240 eat(state, TOK_SIGNED);
8241 switch(peek(state)) {
8243 eat(state, TOK_LONG);
8244 switch(peek(state)) {
8246 eat(state, TOK_LONG);
8247 error(state, 0, "type long long not supported");
8250 eat(state, TOK_INT);
8251 type = new_type(TYPE_LONG | spec, 0, 0);
8254 type = new_type(TYPE_LONG | spec, 0, 0);
8259 eat(state, TOK_INT);
8260 type = new_type(TYPE_INT | spec, 0, 0);
8263 eat(state, TOK_SHORT);
8264 type = new_type(TYPE_SHORT | spec, 0, 0);
8267 eat(state, TOK_CHAR);
8268 type = new_type(TYPE_CHAR | spec, 0, 0);
8271 type = new_type(TYPE_INT | spec, 0, 0);
8276 eat(state, TOK_UNSIGNED);
8277 switch(peek(state)) {
8279 eat(state, TOK_LONG);
8280 switch(peek(state)) {
8282 eat(state, TOK_LONG);
8283 error(state, 0, "unsigned long long not supported");
8286 eat(state, TOK_INT);
8287 type = new_type(TYPE_ULONG | spec, 0, 0);
8290 type = new_type(TYPE_ULONG | spec, 0, 0);
8295 eat(state, TOK_INT);
8296 type = new_type(TYPE_UINT | spec, 0, 0);
8299 eat(state, TOK_SHORT);
8300 type = new_type(TYPE_USHORT | spec, 0, 0);
8303 eat(state, TOK_CHAR);
8304 type = new_type(TYPE_UCHAR | spec, 0, 0);
8307 type = new_type(TYPE_UINT | spec, 0, 0);
8311 /* struct or union specifier */
8314 type = struct_or_union_specifier(state, spec);
8316 /* enum-spefifier */
8318 type = enum_specifier(state, spec);
8322 type = typedef_name(state, spec);
8325 error(state, 0, "bad type specifier %s",
8326 tokens[peek(state)]);
8332 static int istype(int tok)
8358 static struct type *specifier_qualifier_list(struct compile_state *state)
8361 unsigned int specifiers = 0;
8363 /* type qualifiers */
8364 specifiers |= type_qualifiers(state);
8366 /* type specifier */
8367 type = type_specifier(state, specifiers);
8372 static int isdecl_specifier(int tok)
8375 /* storage class specifier */
8381 /* type qualifier */
8385 /* type specifiers */
8395 /* struct or union specifier */
8398 /* enum-spefifier */
8402 /* function specifiers */
8410 static struct type *decl_specifiers(struct compile_state *state)
8413 unsigned int specifiers;
8414 /* I am overly restrictive in the arragement of specifiers supported.
8415 * C is overly flexible in this department it makes interpreting
8416 * the parse tree difficult.
8420 /* storage class specifier */
8421 specifiers |= storage_class_specifier_opt(state);
8423 /* function-specifier */
8424 specifiers |= function_specifier_opt(state);
8426 /* type qualifier */
8427 specifiers |= type_qualifiers(state);
8429 /* type specifier */
8430 type = type_specifier(state, specifiers);
8434 static unsigned designator(struct compile_state *state)
8440 switch(peek(state)) {
8443 struct triple *value;
8444 eat(state, TOK_LBRACKET);
8445 value = constant_expr(state);
8446 eat(state, TOK_RBRACKET);
8447 index = value->u.cval;
8451 eat(state, TOK_DOT);
8452 eat(state, TOK_IDENT);
8453 error(state, 0, "Struct Designators not currently supported");
8456 error(state, 0, "Invalid designator");
8459 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
8464 static struct triple *initializer(
8465 struct compile_state *state, struct type *type)
8467 struct triple *result;
8468 if (peek(state) != TOK_LBRACE) {
8469 result = assignment_expr(state);
8473 unsigned index, max_index;
8475 max_index = index = 0;
8476 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
8477 max_index = type->elements;
8478 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
8482 error(state, 0, "Struct initializers not currently supported");
8484 buf = xcmalloc(size_of(state, type), "initializer");
8485 eat(state, TOK_LBRACE);
8487 struct triple *value;
8488 struct type *value_type;
8493 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
8494 index = designator(state);
8496 if ((max_index != ELEMENT_COUNT_UNSPECIFIED) &&
8497 (index > max_index)) {
8498 error(state, 0, "element beyond bounds");
8501 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
8502 value_type = type->left;
8504 value = eval_const_expr(state, initializer(state, value_type));
8505 value_size = size_of(state, value_type);
8506 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
8507 (max_index == ELEMENT_COUNT_UNSPECIFIED) &&
8508 (type->elements <= index)) {
8512 old_size = size_of(state, type);
8513 type->elements = index + 1;
8514 buf = xmalloc(size_of(state, type), "initializer");
8515 memcpy(buf, old_buf, old_size);
8518 if (value->op == OP_BLOBCONST) {
8519 memcpy((char *)buf + index * value_size, value->u.blob, value_size);
8521 else if ((value->op == OP_INTCONST) && (value_size == 1)) {
8522 *(((uint8_t *)buf) + index) = value->u.cval & 0xff;
8524 else if ((value->op == OP_INTCONST) && (value_size == 2)) {
8525 *(((uint16_t *)buf) + index) = value->u.cval & 0xffff;
8527 else if ((value->op == OP_INTCONST) && (value_size == 4)) {
8528 *(((uint32_t *)buf) + index) = value->u.cval & 0xffffffff;
8531 fprintf(stderr, "%d %d\n",
8532 value->op, value_size);
8533 internal_error(state, 0, "unhandled constant initializer");
8535 if (peek(state) == TOK_COMMA) {
8536 eat(state, TOK_COMMA);
8540 } while(comma && (peek(state) != TOK_RBRACE));
8541 eat(state, TOK_RBRACE);
8542 result = triple(state, OP_BLOBCONST, type, 0, 0);
8543 result->u.blob = buf;
8548 static struct triple *function_definition(
8549 struct compile_state *state, struct type *type)
8551 struct triple *def, *tmp, *first, *end;
8552 struct hash_entry *ident;
8555 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
8556 error(state, 0, "Invalid function header");
8559 /* Verify the function type */
8560 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
8561 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
8562 (type->right->field_ident == 0)) {
8563 error(state, 0, "Invalid function parameters");
8565 param = type->right;
8567 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
8569 if (!param->left->field_ident) {
8570 error(state, 0, "No identifier for parameter %d\n", i);
8572 param = param->right;
8575 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
8576 error(state, 0, "No identifier for paramter %d\n", i);
8579 /* Get a list of statements for this function. */
8580 def = triple(state, OP_LIST, type, 0, 0);
8582 /* Start a new scope for the passed parameters */
8585 /* Put a label at the very start of a function */
8586 first = label(state);
8587 RHS(def, 0) = first;
8589 /* Put a label at the very end of a function */
8591 flatten(state, first, end);
8593 /* Walk through the parameters and create symbol table entries
8596 param = type->right;
8597 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
8598 ident = param->left->field_ident;
8599 tmp = variable(state, param->left);
8600 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
8601 flatten(state, end, tmp);
8602 param = param->right;
8604 if ((param->type & TYPE_MASK) != TYPE_VOID) {
8605 /* And don't forget the last parameter */
8606 ident = param->field_ident;
8607 tmp = variable(state, param);
8608 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
8609 flatten(state, end, tmp);
8611 /* Add a variable for the return value */
8613 if ((type->left->type & TYPE_MASK) != TYPE_VOID) {
8614 /* Remove all type qualifiers from the return type */
8615 tmp = variable(state, clone_type(0, type->left));
8616 flatten(state, end, tmp);
8617 /* Remember where the return value is */
8621 /* Remember which function I am compiling.
8622 * Also assume the last defined function is the main function.
8624 state->main_function = def;
8626 /* Now get the actual function definition */
8627 compound_statement(state, end);
8629 /* Remove the parameter scope */
8632 fprintf(stdout, "\n");
8633 loc(stdout, state, 0);
8634 fprintf(stdout, "\n__________ function_definition _________\n");
8635 print_triple(state, def);
8636 fprintf(stdout, "__________ function_definition _________ done\n\n");
8642 static struct triple *do_decl(struct compile_state *state,
8643 struct type *type, struct hash_entry *ident)
8647 /* Clean up the storage types used */
8648 switch (type->type & STOR_MASK) {
8651 /* These are the good types I am aiming for */
8654 type->type &= ~STOR_MASK;
8655 type->type |= STOR_AUTO;
8658 type->type &= ~STOR_MASK;
8659 type->type |= STOR_STATIC;
8663 error(state, 0, "typedef without name");
8665 symbol(state, ident, &ident->sym_ident, 0, type);
8666 ident->tok = TOK_TYPE_NAME;
8670 internal_error(state, 0, "Undefined storage class");
8672 if (((type->type & STOR_MASK) == STOR_STATIC) &&
8673 ((type->type & QUAL_CONST) == 0)) {
8674 error(state, 0, "non const static variables not supported");
8677 def = variable(state, type);
8678 symbol(state, ident, &ident->sym_ident, def, type);
8683 static void decl(struct compile_state *state, struct triple *first)
8685 struct type *base_type, *type;
8686 struct hash_entry *ident;
8689 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
8690 base_type = decl_specifiers(state);
8692 type = declarator(state, base_type, &ident, 0);
8693 if (global && ident && (peek(state) == TOK_LBRACE)) {
8695 def = function_definition(state, type);
8696 symbol(state, ident, &ident->sym_ident, def, type);
8700 flatten(state, first, do_decl(state, type, ident));
8701 /* type or variable definition */
8704 if (peek(state) == TOK_EQ) {
8706 error(state, 0, "cannot assign to a type");
8709 flatten(state, first,
8711 ident->sym_ident->def,
8712 initializer(state, type)));
8714 arrays_complete(state, type);
8715 if (peek(state) == TOK_COMMA) {
8716 eat(state, TOK_COMMA);
8718 type = declarator(state, base_type, &ident, 0);
8719 flatten(state, first, do_decl(state, type, ident));
8723 eat(state, TOK_SEMI);
8727 static void decls(struct compile_state *state)
8729 struct triple *list;
8731 list = label(state);
8734 if (tok == TOK_EOF) {
8737 if (tok == TOK_SPACE) {
8738 eat(state, TOK_SPACE);
8741 if (list->next != list) {
8742 error(state, 0, "global variables not supported");
8748 * Data structurs for optimation.
8751 static void do_use_block(
8752 struct block *used, struct block_set **head, struct block *user,
8755 struct block_set **ptr, *new;
8762 if ((*ptr)->member == user) {
8765 ptr = &(*ptr)->next;
8767 new = xcmalloc(sizeof(*new), "block_set");
8778 static void do_unuse_block(
8779 struct block *used, struct block_set **head, struct block *unuser)
8781 struct block_set *use, **ptr;
8785 if (use->member == unuser) {
8787 memset(use, -1, sizeof(*use));
8796 static void use_block(struct block *used, struct block *user)
8798 /* Append new to the head of the list, print_block
8801 do_use_block(used, &used->use, user, 1);
8804 static void unuse_block(struct block *used, struct block *unuser)
8806 do_unuse_block(used, &used->use, unuser);
8810 static void idom_block(struct block *idom, struct block *user)
8812 do_use_block(idom, &idom->idominates, user, 0);
8815 static void unidom_block(struct block *idom, struct block *unuser)
8817 do_unuse_block(idom, &idom->idominates, unuser);
8820 static void domf_block(struct block *block, struct block *domf)
8822 do_use_block(block, &block->domfrontier, domf, 0);
8825 static void undomf_block(struct block *block, struct block *undomf)
8827 do_unuse_block(block, &block->domfrontier, undomf);
8830 static void ipdom_block(struct block *ipdom, struct block *user)
8832 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
8835 static void unipdom_block(struct block *ipdom, struct block *unuser)
8837 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
8840 static void ipdomf_block(struct block *block, struct block *ipdomf)
8842 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
8845 static void unipdomf_block(struct block *block, struct block *unipdomf)
8847 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
8852 static int do_walk_triple(struct compile_state *state,
8853 struct triple *ptr, int depth,
8854 int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
8857 result = cb(state, ptr, depth);
8858 if ((result == 0) && (ptr->op == OP_LIST)) {
8859 struct triple *list;
8863 result = do_walk_triple(state, ptr, depth + 1, cb);
8864 if (ptr->next->prev != ptr) {
8865 internal_error(state, ptr->next, "bad prev");
8869 } while((result == 0) && (ptr != RHS(list, 0)));
8874 static int walk_triple(
8875 struct compile_state *state,
8877 int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
8879 return do_walk_triple(state, ptr, 0, cb);
8882 static void do_print_prefix(int depth)
8885 for(i = 0; i < depth; i++) {
8890 #define PRINT_LIST 1
8891 static int do_print_triple(struct compile_state *state, struct triple *ins, int depth)
8895 if (op == OP_LIST) {
8900 if ((op == OP_LABEL) && (ins->use)) {
8901 printf("\n%p:\n", ins);
8903 do_print_prefix(depth);
8904 display_triple(stdout, ins);
8906 if ((ins->op == OP_BRANCH) && ins->use) {
8907 internal_error(state, ins, "branch used?");
8911 struct triple_set *user;
8912 for(user = ins->use; user; user = user->next) {
8913 printf("use: %p\n", user->member);
8917 if (triple_is_branch(state, ins)) {
8923 static void print_triple(struct compile_state *state, struct triple *ins)
8925 walk_triple(state, ins, do_print_triple);
8928 static void print_triples(struct compile_state *state)
8930 print_triple(state, state->main_function);
8934 struct block *block;
8936 static void find_cf_blocks(struct cf_block *cf, struct block *block)
8938 if (!block || (cf[block->vertex].block == block)) {
8941 cf[block->vertex].block = block;
8942 find_cf_blocks(cf, block->left);
8943 find_cf_blocks(cf, block->right);
8946 static void print_control_flow(struct compile_state *state)
8948 struct cf_block *cf;
8950 printf("\ncontrol flow\n");
8951 cf = xcmalloc(sizeof(*cf) * (state->last_vertex + 1), "cf_block");
8952 find_cf_blocks(cf, state->first_block);
8954 for(i = 1; i <= state->last_vertex; i++) {
8955 struct block *block;
8956 block = cf[i].block;
8959 printf("(%p) %d:", block, block->vertex);
8961 printf(" %d", block->left->vertex);
8963 if (block->right && (block->right != block->left)) {
8964 printf(" %d", block->right->vertex);
8973 static struct block *basic_block(struct compile_state *state,
8974 struct triple *first)
8976 struct block *block;
8979 if (first->op != OP_LABEL) {
8980 internal_error(state, 0, "block does not start with a label");
8982 /* See if this basic block has already been setup */
8983 if (first->u.block != 0) {
8984 return first->u.block;
8986 /* Allocate another basic block structure */
8987 state->last_vertex += 1;
8988 block = xcmalloc(sizeof(*block), "block");
8989 block->first = block->last = first;
8990 block->vertex = state->last_vertex;
8993 if ((ptr != first) && (ptr->op == OP_LABEL) && ptr->use) {
8997 /* If ptr->u is not used remember where the baic block is */
8998 if (triple_stores_block(state, ptr)) {
8999 ptr->u.block = block;
9001 if (ptr->op == OP_BRANCH) {
9005 } while (ptr != RHS(state->main_function, 0));
9006 if (ptr == RHS(state->main_function, 0))
9009 if (op == OP_LABEL) {
9010 block->left = basic_block(state, ptr);
9012 use_block(block->left, block);
9014 else if (op == OP_BRANCH) {
9016 /* Trace the branch target */
9017 block->right = basic_block(state, TARG(ptr, 0));
9018 use_block(block->right, block);
9019 /* If there is a test trace the branch as well */
9020 if (TRIPLE_RHS(ptr->sizes)) {
9021 block->left = basic_block(state, ptr->next);
9022 use_block(block->left, block);
9026 internal_error(state, 0, "Bad basic block split");
9032 static void walk_blocks(struct compile_state *state,
9033 void (*cb)(struct compile_state *state, struct block *block, void *arg),
9036 struct triple *ptr, *first;
9037 struct block *last_block;
9039 first = RHS(state->main_function, 0);
9042 struct block *block;
9043 if (ptr->op == OP_LABEL) {
9044 block = ptr->u.block;
9045 if (block && (block != last_block)) {
9046 cb(state, block, arg);
9051 } while(ptr != first);
9054 static void print_block(
9055 struct compile_state *state, struct block *block, void *arg)
9060 fprintf(fp, "\nblock: %p (%d), %p<-%p %p<-%p\n",
9064 block->left && block->left->use?block->left->use->member : 0,
9066 block->right && block->right->use?block->right->use->member : 0);
9067 if (block->first->op == OP_LABEL) {
9068 fprintf(fp, "%p:\n", block->first);
9070 for(ptr = block->first; ; ptr = ptr->next) {
9071 struct triple_set *user;
9074 if (triple_stores_block(state, ptr)) {
9075 if (ptr->u.block != block) {
9076 internal_error(state, ptr,
9077 "Wrong block pointer: %p\n",
9081 if (op == OP_ADECL) {
9082 for(user = ptr->use; user; user = user->next) {
9083 if (!user->member->u.block) {
9084 internal_error(state, user->member,
9085 "Use %p not in a block?\n",
9090 display_triple(fp, ptr);
9093 for(user = ptr->use; user; user = user->next) {
9094 fprintf(fp, "use: %p\n", user->member);
9098 /* Sanity checks... */
9099 valid_ins(state, ptr);
9100 for(user = ptr->use; user; user = user->next) {
9103 valid_ins(state, use);
9104 if (triple_stores_block(state, user->member) &&
9105 !user->member->u.block) {
9106 internal_error(state, user->member,
9107 "Use %p not in a block?",
9112 if (ptr == block->last)
9119 static void print_blocks(struct compile_state *state, FILE *fp)
9121 fprintf(fp, "--------------- blocks ---------------\n");
9122 walk_blocks(state, print_block, fp);
9125 static void prune_nonblock_triples(struct compile_state *state)
9127 struct block *block;
9128 struct triple *first, *ins, *next;
9129 /* Delete the triples not in a basic block */
9130 first = RHS(state->main_function, 0);
9135 if (ins->op == OP_LABEL) {
9136 block = ins->u.block;
9139 release_triple(state, ins);
9142 } while(ins != first);
9145 static void setup_basic_blocks(struct compile_state *state)
9147 if (!triple_stores_block(state, RHS(state->main_function, 0)) ||
9148 !triple_stores_block(state, RHS(state->main_function,0)->prev)) {
9149 internal_error(state, 0, "ins will not store block?");
9151 /* Find the basic blocks */
9152 state->last_vertex = 0;
9153 state->first_block = basic_block(state, RHS(state->main_function,0));
9154 /* Delete the triples not in a basic block */
9155 prune_nonblock_triples(state);
9156 /* Find the last basic block */
9157 state->last_block = RHS(state->main_function, 0)->prev->u.block;
9158 if (!state->last_block) {
9159 internal_error(state, 0, "end not used?");
9161 /* Insert an extra unused edge from start to the end
9162 * This helps with reverse control flow calculations.
9164 use_block(state->first_block, state->last_block);
9165 /* If we are debugging print what I have just done */
9166 if (state->debug & DEBUG_BASIC_BLOCKS) {
9167 print_blocks(state, stdout);
9168 print_control_flow(state);
9172 static void free_basic_block(struct compile_state *state, struct block *block)
9174 struct block_set *entry, *next;
9175 struct block *child;
9179 if (block->vertex == -1) {
9184 unuse_block(block->left, block);
9187 unuse_block(block->right, block);
9190 unidom_block(block->idom, block);
9194 unipdom_block(block->ipdom, block);
9197 for(entry = block->use; entry; entry = next) {
9199 child = entry->member;
9200 unuse_block(block, child);
9201 if (child->left == block) {
9204 if (child->right == block) {
9208 for(entry = block->idominates; entry; entry = next) {
9210 child = entry->member;
9211 unidom_block(block, child);
9214 for(entry = block->domfrontier; entry; entry = next) {
9216 child = entry->member;
9217 undomf_block(block, child);
9219 for(entry = block->ipdominates; entry; entry = next) {
9221 child = entry->member;
9222 unipdom_block(block, child);
9225 for(entry = block->ipdomfrontier; entry; entry = next) {
9227 child = entry->member;
9228 unipdomf_block(block, child);
9230 if (block->users != 0) {
9231 internal_error(state, 0, "block still has users");
9233 free_basic_block(state, block->left);
9235 free_basic_block(state, block->right);
9237 memset(block, -1, sizeof(*block));
9241 static void free_basic_blocks(struct compile_state *state)
9243 struct triple *first, *ins;
9244 free_basic_block(state, state->first_block);
9245 state->last_vertex = 0;
9246 state->first_block = state->last_block = 0;
9247 first = RHS(state->main_function, 0);
9250 if (triple_stores_block(state, ins)) {
9254 } while(ins != first);
9259 struct block *block;
9260 struct sdom_block *sdominates;
9261 struct sdom_block *sdom_next;
9262 struct sdom_block *sdom;
9263 struct sdom_block *label;
9264 struct sdom_block *parent;
9265 struct sdom_block *ancestor;
9270 static void unsdom_block(struct sdom_block *block)
9272 struct sdom_block **ptr;
9273 if (!block->sdom_next) {
9276 ptr = &block->sdom->sdominates;
9278 if ((*ptr) == block) {
9279 *ptr = block->sdom_next;
9282 ptr = &(*ptr)->sdom_next;
9286 static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
9288 unsdom_block(block);
9290 block->sdom_next = sdom->sdominates;
9291 sdom->sdominates = block;
9296 static int initialize_sdblock(struct sdom_block *sd,
9297 struct block *parent, struct block *block, int vertex)
9299 if (!block || (sd[block->vertex].block == block)) {
9303 /* Renumber the blocks in a convinient fashion */
9304 block->vertex = vertex;
9305 sd[vertex].block = block;
9306 sd[vertex].sdom = &sd[vertex];
9307 sd[vertex].label = &sd[vertex];
9308 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
9309 sd[vertex].ancestor = 0;
9310 sd[vertex].vertex = vertex;
9311 vertex = initialize_sdblock(sd, block, block->left, vertex);
9312 vertex = initialize_sdblock(sd, block, block->right, vertex);
9316 static int initialize_sdpblock(struct sdom_block *sd,
9317 struct block *parent, struct block *block, int vertex)
9319 struct block_set *user;
9320 if (!block || (sd[block->vertex].block == block)) {
9324 /* Renumber the blocks in a convinient fashion */
9325 block->vertex = vertex;
9326 sd[vertex].block = block;
9327 sd[vertex].sdom = &sd[vertex];
9328 sd[vertex].label = &sd[vertex];
9329 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
9330 sd[vertex].ancestor = 0;
9331 sd[vertex].vertex = vertex;
9332 for(user = block->use; user; user = user->next) {
9333 vertex = initialize_sdpblock(sd, block, user->member, vertex);
9338 static void compress_ancestors(struct sdom_block *v)
9340 /* This procedure assumes ancestor(v) != 0 */
9341 /* if (ancestor(ancestor(v)) != 0) {
9342 * compress(ancestor(ancestor(v)));
9343 * if (semi(label(ancestor(v))) < semi(label(v))) {
9344 * label(v) = label(ancestor(v));
9346 * ancestor(v) = ancestor(ancestor(v));
9352 if (v->ancestor->ancestor) {
9353 compress_ancestors(v->ancestor->ancestor);
9354 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
9355 v->label = v->ancestor->label;
9357 v->ancestor = v->ancestor->ancestor;
9361 static void compute_sdom(struct compile_state *state, struct sdom_block *sd)
9365 * for each v <= pred(w) {
9367 * if (semi[u] < semi[w] {
9368 * semi[w] = semi[u];
9371 * add w to bucket(vertex(semi[w]));
9372 * LINK(parent(w), w);
9375 * for each v <= bucket(parent(w)) {
9376 * delete v from bucket(parent(w));
9378 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
9381 for(i = state->last_vertex; i >= 2; i--) {
9382 struct sdom_block *v, *parent, *next;
9383 struct block_set *user;
9384 struct block *block;
9385 block = sd[i].block;
9386 parent = sd[i].parent;
9388 for(user = block->use; user; user = user->next) {
9389 struct sdom_block *v, *u;
9390 v = &sd[user->member->vertex];
9391 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9392 if (u->sdom->vertex < sd[i].sdom->vertex) {
9393 sd[i].sdom = u->sdom;
9396 sdom_block(sd[i].sdom, &sd[i]);
9397 sd[i].ancestor = parent;
9399 for(v = parent->sdominates; v; v = next) {
9400 struct sdom_block *u;
9401 next = v->sdom_next;
9403 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
9404 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
9405 u->block : parent->block;
9410 static void compute_spdom(struct compile_state *state, struct sdom_block *sd)
9414 * for each v <= pred(w) {
9416 * if (semi[u] < semi[w] {
9417 * semi[w] = semi[u];
9420 * add w to bucket(vertex(semi[w]));
9421 * LINK(parent(w), w);
9424 * for each v <= bucket(parent(w)) {
9425 * delete v from bucket(parent(w));
9427 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
9430 for(i = state->last_vertex; i >= 2; i--) {
9431 struct sdom_block *u, *v, *parent, *next;
9432 struct block *block;
9433 block = sd[i].block;
9434 parent = sd[i].parent;
9437 v = &sd[block->left->vertex];
9438 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9439 if (u->sdom->vertex < sd[i].sdom->vertex) {
9440 sd[i].sdom = u->sdom;
9443 if (block->right && (block->right != block->left)) {
9444 v = &sd[block->right->vertex];
9445 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9446 if (u->sdom->vertex < sd[i].sdom->vertex) {
9447 sd[i].sdom = u->sdom;
9450 sdom_block(sd[i].sdom, &sd[i]);
9451 sd[i].ancestor = parent;
9453 for(v = parent->sdominates; v; v = next) {
9454 struct sdom_block *u;
9455 next = v->sdom_next;
9457 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
9458 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
9459 u->block : parent->block;
9464 static void compute_idom(struct compile_state *state, struct sdom_block *sd)
9467 for(i = 2; i <= state->last_vertex; i++) {
9468 struct block *block;
9469 block = sd[i].block;
9470 if (block->idom->vertex != sd[i].sdom->vertex) {
9471 block->idom = block->idom->idom;
9473 idom_block(block->idom, block);
9475 sd[1].block->idom = 0;
9478 static void compute_ipdom(struct compile_state *state, struct sdom_block *sd)
9481 for(i = 2; i <= state->last_vertex; i++) {
9482 struct block *block;
9483 block = sd[i].block;
9484 if (block->ipdom->vertex != sd[i].sdom->vertex) {
9485 block->ipdom = block->ipdom->ipdom;
9487 ipdom_block(block->ipdom, block);
9489 sd[1].block->ipdom = 0;
9493 * Every vertex of a flowgraph G = (V, E, r) except r has
9494 * a unique immediate dominator.
9495 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
9496 * rooted at r, called the dominator tree of G, such that
9497 * v dominates w if and only if v is a proper ancestor of w in
9498 * the dominator tree.
9501 * If v and w are vertices of G such that v <= w,
9502 * than any path from v to w must contain a common ancestor
9505 /* Lemma 2: For any vertex w != r, idom(w) -> w */
9506 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
9507 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
9509 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
9510 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
9513 * Let w != r and let u be a vertex for which sdom(u) is
9514 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
9515 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
9517 /* Lemma 5: Let vertices v,w satisfy v -> w.
9518 * Then v -> idom(w) or idom(w) -> idom(v)
9521 static void find_immediate_dominators(struct compile_state *state)
9523 struct sdom_block *sd;
9524 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
9525 * vi > w for (1 <= i <= k - 1}
9528 * For any vertex w != r.
9530 * {v|(v,w) <= E and v < w } U
9531 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
9534 * Let w != r and let u be a vertex for which sdom(u) is
9535 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
9537 * { sdom(w) if sdom(w) = sdom(u),
9539 * { idom(u) otherwise
9541 /* The algorithm consists of the following 4 steps.
9542 * Step 1. Carry out a depth-first search of the problem graph.
9543 * Number the vertices from 1 to N as they are reached during
9544 * the search. Initialize the variables used in succeeding steps.
9545 * Step 2. Compute the semidominators of all vertices by applying
9546 * theorem 4. Carry out the computation vertex by vertex in
9547 * decreasing order by number.
9548 * Step 3. Implicitly define the immediate dominator of each vertex
9549 * by applying Corollary 1.
9550 * Step 4. Explicitly define the immediate dominator of each vertex,
9551 * carrying out the computation vertex by vertex in increasing order
9554 /* Step 1 initialize the basic block information */
9555 sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
9556 initialize_sdblock(sd, 0, state->first_block, 0);
9562 /* Step 2 compute the semidominators */
9563 /* Step 3 implicitly define the immediate dominator of each vertex */
9564 compute_sdom(state, sd);
9565 /* Step 4 explicitly define the immediate dominator of each vertex */
9566 compute_idom(state, sd);
9570 static void find_post_dominators(struct compile_state *state)
9572 struct sdom_block *sd;
9573 /* Step 1 initialize the basic block information */
9574 sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
9576 initialize_sdpblock(sd, 0, state->last_block, 0);
9578 /* Step 2 compute the semidominators */
9579 /* Step 3 implicitly define the immediate dominator of each vertex */
9580 compute_spdom(state, sd);
9581 /* Step 4 explicitly define the immediate dominator of each vertex */
9582 compute_ipdom(state, sd);
9588 static void find_block_domf(struct compile_state *state, struct block *block)
9590 struct block *child;
9591 struct block_set *user;
9592 if (block->domfrontier != 0) {
9593 internal_error(state, block->first, "domfrontier present?");
9595 for(user = block->idominates; user; user = user->next) {
9596 child = user->member;
9597 if (child->idom != block) {
9598 internal_error(state, block->first, "bad idom");
9600 find_block_domf(state, child);
9602 if (block->left && block->left->idom != block) {
9603 domf_block(block, block->left);
9605 if (block->right && block->right->idom != block) {
9606 domf_block(block, block->right);
9608 for(user = block->idominates; user; user = user->next) {
9609 struct block_set *frontier;
9610 child = user->member;
9611 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
9612 if (frontier->member->idom != block) {
9613 domf_block(block, frontier->member);
9619 static void find_block_ipdomf(struct compile_state *state, struct block *block)
9621 struct block *child;
9622 struct block_set *user;
9623 if (block->ipdomfrontier != 0) {
9624 internal_error(state, block->first, "ipdomfrontier present?");
9626 for(user = block->ipdominates; user; user = user->next) {
9627 child = user->member;
9628 if (child->ipdom != block) {
9629 internal_error(state, block->first, "bad ipdom");
9631 find_block_ipdomf(state, child);
9633 if (block->left && block->left->ipdom != block) {
9634 ipdomf_block(block, block->left);
9636 if (block->right && block->right->ipdom != block) {
9637 ipdomf_block(block, block->right);
9639 for(user = block->idominates; user; user = user->next) {
9640 struct block_set *frontier;
9641 child = user->member;
9642 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
9643 if (frontier->member->ipdom != block) {
9644 ipdomf_block(block, frontier->member);
9650 static void print_dominated(
9651 struct compile_state *state, struct block *block, void *arg)
9653 struct block_set *user;
9656 fprintf(fp, "%d:", block->vertex);
9657 for(user = block->idominates; user; user = user->next) {
9658 fprintf(fp, " %d", user->member->vertex);
9659 if (user->member->idom != block) {
9660 internal_error(state, user->member->first, "bad idom");
9666 static void print_dominators(struct compile_state *state, FILE *fp)
9668 fprintf(fp, "\ndominates\n");
9669 walk_blocks(state, print_dominated, fp);
9673 static int print_frontiers(
9674 struct compile_state *state, struct block *block, int vertex)
9676 struct block_set *user;
9678 if (!block || (block->vertex != vertex + 1)) {
9683 printf("%d:", block->vertex);
9684 for(user = block->domfrontier; user; user = user->next) {
9685 printf(" %d", user->member->vertex);
9689 vertex = print_frontiers(state, block->left, vertex);
9690 vertex = print_frontiers(state, block->right, vertex);
9693 static void print_dominance_frontiers(struct compile_state *state)
9695 printf("\ndominance frontiers\n");
9696 print_frontiers(state, state->first_block, 0);
9700 static void analyze_idominators(struct compile_state *state)
9702 /* Find the immediate dominators */
9703 find_immediate_dominators(state);
9704 /* Find the dominance frontiers */
9705 find_block_domf(state, state->first_block);
9706 /* If debuging print the print what I have just found */
9707 if (state->debug & DEBUG_FDOMINATORS) {
9708 print_dominators(state, stdout);
9709 print_dominance_frontiers(state);
9710 print_control_flow(state);
9716 static void print_ipdominated(
9717 struct compile_state *state, struct block *block, void *arg)
9719 struct block_set *user;
9722 fprintf(fp, "%d:", block->vertex);
9723 for(user = block->ipdominates; user; user = user->next) {
9724 fprintf(fp, " %d", user->member->vertex);
9725 if (user->member->ipdom != block) {
9726 internal_error(state, user->member->first, "bad ipdom");
9732 static void print_ipdominators(struct compile_state *state, FILE *fp)
9734 fprintf(fp, "\nipdominates\n");
9735 walk_blocks(state, print_ipdominated, fp);
9738 static int print_pfrontiers(
9739 struct compile_state *state, struct block *block, int vertex)
9741 struct block_set *user;
9743 if (!block || (block->vertex != vertex + 1)) {
9748 printf("%d:", block->vertex);
9749 for(user = block->ipdomfrontier; user; user = user->next) {
9750 printf(" %d", user->member->vertex);
9753 for(user = block->use; user; user = user->next) {
9754 vertex = print_pfrontiers(state, user->member, vertex);
9758 static void print_ipdominance_frontiers(struct compile_state *state)
9760 printf("\nipdominance frontiers\n");
9761 print_pfrontiers(state, state->last_block, 0);
9765 static void analyze_ipdominators(struct compile_state *state)
9767 /* Find the post dominators */
9768 find_post_dominators(state);
9769 /* Find the control dependencies (post dominance frontiers) */
9770 find_block_ipdomf(state, state->last_block);
9771 /* If debuging print the print what I have just found */
9772 if (state->debug & DEBUG_RDOMINATORS) {
9773 print_ipdominators(state, stdout);
9774 print_ipdominance_frontiers(state);
9775 print_control_flow(state);
9779 static int bdominates(struct compile_state *state,
9780 struct block *dom, struct block *sub)
9782 while(sub && (sub != dom)) {
9788 static int tdominates(struct compile_state *state,
9789 struct triple *dom, struct triple *sub)
9791 struct block *bdom, *bsub;
9793 bdom = block_of_triple(state, dom);
9794 bsub = block_of_triple(state, sub);
9796 result = bdominates(state, bdom, bsub);
9801 while((ins != bsub->first) && (ins != dom)) {
9804 result = (ins == dom);
9809 static int tdistance(struct compile_state *state,
9810 struct triple *dom, struct triple *sub)
9813 struct block *bdom, *bsub;
9814 if (!tdominates(state, dom, sub)) {
9815 internal_error(state, 0, "dom does not dom sub");
9817 bdom = block_of_triple(state, dom);
9818 bsub = block_of_triple(state, sub);
9820 for(; bsub != bdom; (bsub = bsub->idom), sub = bsub->last) {
9821 for(; sub != bsub->first; sub = sub->prev) {
9825 for(; sub != dom; sub = sub->prev) {
9831 static void insert_phi_operations(struct compile_state *state)
9834 struct triple *first;
9835 int *has_already, *work;
9836 struct block *work_list, **work_list_tail;
9840 size = sizeof(int) * (state->last_vertex + 1);
9841 has_already = xcmalloc(size, "has_already");
9842 work = xcmalloc(size, "work");
9845 first = RHS(state->main_function, 0);
9846 for(var = first->next; var != first ; var = var->next) {
9847 struct block *block;
9848 struct triple_set *user;
9849 if ((var->op != OP_ADECL) || !var->use) {
9854 work_list_tail = &work_list;
9855 for(user = var->use; user; user = user->next) {
9856 if (user->member->op == OP_READ) {
9859 if (user->member->op != OP_WRITE) {
9860 internal_error(state, user->member,
9861 "bad variable access");
9863 block = user->member->u.block;
9865 warning(state, user->member, "dead code");
9867 work[block->vertex] = iter;
9868 *work_list_tail = block;
9869 block->work_next = 0;
9870 work_list_tail = &block->work_next;
9872 for(block = work_list; block; block = block->work_next) {
9873 struct block_set *df;
9874 for(df = block->domfrontier; df; df = df->next) {
9876 struct block *front;
9880 if (has_already[front->vertex] >= iter) {
9883 /* Count how many edges flow into this block */
9884 in_edges = front->users;
9885 /* Insert a phi function for this variable */
9887 state, OP_PHI, var->type, -1, in_edges,
9888 front->first->filename,
9891 phi->u.block = front;
9893 use_triple(var, phi);
9894 /* Insert the phi functions immediately after the label */
9895 insert_triple(state, front->first->next, phi);
9896 if (front->first == front->last) {
9897 front->last = front->first->next;
9899 has_already[front->vertex] = iter;
9901 /* If necessary plan to visit the basic block */
9902 if (work[front->vertex] >= iter) {
9905 work[front->vertex] = iter;
9906 *work_list_tail = front;
9907 front->work_next = 0;
9908 work_list_tail = &front->work_next;
9920 static void fixup_block_phi_variables(
9921 struct compile_state *state, struct block *parent, struct block *block)
9923 struct block_set *set;
9926 if (!parent || !block)
9928 /* Find the edge I am coming in on */
9930 for(set = block->use; set; set = set->next, edge++) {
9931 if (set->member == parent) {
9936 internal_error(state, 0, "phi input is not on a control predecessor");
9938 for(ptr = block->first; ; ptr = ptr->next) {
9939 if (ptr->op == OP_PHI) {
9940 struct triple *var, *val, **slot;
9943 internal_error(state, ptr, "no var???");
9945 /* Find the current value of the variable */
9946 val = var->use->member;
9947 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
9948 internal_error(state, val, "bad value in phi");
9950 if (edge >= TRIPLE_RHS(ptr->sizes)) {
9951 internal_error(state, ptr, "edges > phi rhs");
9953 slot = &RHS(ptr, edge);
9954 if ((*slot != 0) && (*slot != val)) {
9955 internal_error(state, ptr, "phi already bound on this edge");
9958 use_triple(val, ptr);
9960 if (ptr == block->last) {
9967 static void rename_block_variables(
9968 struct compile_state *state, struct block *block)
9970 struct block_set *user;
9971 struct triple *ptr, *next, *last;
9975 last = block->first;
9977 for(ptr = block->first; !done; ptr = next) {
9979 if (ptr == block->last) {
9983 if (ptr->op == OP_READ) {
9984 struct triple *var, *val;
9986 unuse_triple(var, ptr);
9988 error(state, ptr, "variable used without being set");
9990 /* Find the current value of the variable */
9991 val = var->use->member;
9992 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
9993 internal_error(state, val, "bad value in read");
9995 propogate_use(state, ptr, val);
9996 release_triple(state, ptr);
10000 if (ptr->op == OP_WRITE) {
10001 struct triple *var, *val;
10004 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10005 internal_error(state, val, "bad value in write");
10007 propogate_use(state, ptr, val);
10008 unuse_triple(var, ptr);
10009 /* Push OP_WRITE ptr->right onto a stack of variable uses */
10010 push_triple(var, val);
10012 if (ptr->op == OP_PHI) {
10013 struct triple *var;
10014 var = MISC(ptr, 0);
10015 /* Push OP_PHI onto a stack of variable uses */
10016 push_triple(var, ptr);
10020 block->last = last;
10022 /* Fixup PHI functions in the cf successors */
10023 fixup_block_phi_variables(state, block, block->left);
10024 fixup_block_phi_variables(state, block, block->right);
10025 /* rename variables in the dominated nodes */
10026 for(user = block->idominates; user; user = user->next) {
10027 rename_block_variables(state, user->member);
10029 /* pop the renamed variable stack */
10030 last = block->first;
10032 for(ptr = block->first; !done ; ptr = next) {
10034 if (ptr == block->last) {
10037 if (ptr->op == OP_WRITE) {
10038 struct triple *var;
10040 /* Pop OP_WRITE ptr->right from the stack of variable uses */
10041 pop_triple(var, RHS(ptr, 0));
10042 release_triple(state, ptr);
10045 if (ptr->op == OP_PHI) {
10046 struct triple *var;
10047 var = MISC(ptr, 0);
10048 /* Pop OP_WRITE ptr->right from the stack of variable uses */
10049 pop_triple(var, ptr);
10053 block->last = last;
10056 static void prune_block_variables(struct compile_state *state,
10057 struct block *block)
10059 struct block_set *user;
10060 struct triple *next, *last, *ptr;
10062 last = block->first;
10064 for(ptr = block->first; !done; ptr = next) {
10066 if (ptr == block->last) {
10069 if (ptr->op == OP_ADECL) {
10070 struct triple_set *user, *next;
10071 for(user = ptr->use; user; user = next) {
10072 struct triple *use;
10074 use = user->member;
10075 if (use->op != OP_PHI) {
10076 internal_error(state, use, "decl still used");
10078 if (MISC(use, 0) != ptr) {
10079 internal_error(state, use, "bad phi use of decl");
10081 unuse_triple(ptr, use);
10084 release_triple(state, ptr);
10089 block->last = last;
10090 for(user = block->idominates; user; user = user->next) {
10091 prune_block_variables(state, user->member);
10095 static void transform_to_ssa_form(struct compile_state *state)
10097 insert_phi_operations(state);
10099 printf("@%s:%d\n", __FILE__, __LINE__);
10100 print_blocks(state, stdout);
10102 rename_block_variables(state, state->first_block);
10103 prune_block_variables(state, state->first_block);
10107 static void clear_vertex(
10108 struct compile_state *state, struct block *block, void *arg)
10113 static void mark_live_block(
10114 struct compile_state *state, struct block *block, int *next_vertex)
10116 /* See if this is a block that has not been marked */
10117 if (block->vertex != 0) {
10120 block->vertex = *next_vertex;
10122 if (triple_is_branch(state, block->last)) {
10123 struct triple **targ;
10124 targ = triple_targ(state, block->last, 0);
10125 for(; targ; targ = triple_targ(state, block->last, targ)) {
10129 if (!triple_stores_block(state, *targ)) {
10130 internal_error(state, 0, "bad targ");
10132 mark_live_block(state, (*targ)->u.block, next_vertex);
10135 else if (block->last->next != RHS(state->main_function, 0)) {
10136 struct triple *ins;
10137 ins = block->last->next;
10138 if (!triple_stores_block(state, ins)) {
10139 internal_error(state, 0, "bad block start");
10141 mark_live_block(state, ins->u.block, next_vertex);
10145 static void transform_from_ssa_form(struct compile_state *state)
10147 /* To get out of ssa form we insert moves on the incoming
10148 * edges to blocks containting phi functions.
10150 struct triple *first;
10151 struct triple *phi, *next;
10154 /* Walk the control flow to see which blocks remain alive */
10155 walk_blocks(state, clear_vertex, 0);
10157 mark_live_block(state, state->first_block, &next_vertex);
10159 /* Walk all of the operations to find the phi functions */
10160 first = RHS(state->main_function, 0);
10161 for(phi = first->next; phi != first ; phi = next) {
10162 struct block_set *set;
10163 struct block *block;
10164 struct triple **slot;
10165 struct triple *var, *read;
10166 struct triple_set *use, *use_next;
10169 if (phi->op != OP_PHI) {
10172 block = phi->u.block;
10173 slot = &RHS(phi, 0);
10175 /* Forget uses from code in dead blocks */
10176 for(use = phi->use; use; use = use_next) {
10177 struct block *ublock;
10178 struct triple **expr;
10179 use_next = use->next;
10180 ublock = block_of_triple(state, use->member);
10181 if ((use->member == phi) || (ublock->vertex != 0)) {
10184 expr = triple_rhs(state, use->member, 0);
10185 for(; expr; expr = triple_rhs(state, use->member, expr)) {
10186 if (*expr == phi) {
10190 unuse_triple(phi, use->member);
10193 /* A variable to replace the phi function */
10194 var = post_triple(state, phi, OP_ADECL, phi->type, 0,0);
10195 /* A read of the single value that is set into the variable */
10196 read = post_triple(state, var, OP_READ, phi->type, var, 0);
10197 use_triple(var, read);
10199 /* Replaces uses of the phi with variable reads */
10200 propogate_use(state, phi, read);
10202 /* Walk all of the incoming edges/blocks and insert moves.
10204 for(edge = 0, set = block->use; set; set = set->next, edge++) {
10205 struct block *eblock;
10206 struct triple *move;
10207 struct triple *val;
10208 eblock = set->member;
10211 unuse_triple(val, phi);
10213 if (!val || (val == &zero_triple) ||
10214 (block->vertex == 0) || (eblock->vertex == 0) ||
10215 (val == phi) || (val == read)) {
10219 move = post_triple(state,
10220 val, OP_WRITE, phi->type, var, val);
10221 use_triple(val, move);
10222 use_triple(var, move);
10224 /* See if there are any writers of var */
10226 for(use = var->use; use; use = use->next) {
10227 struct triple **expr;
10228 expr = triple_lhs(state, use->member, 0);
10229 for(; expr; expr = triple_lhs(state, use->member, expr)) {
10230 if (*expr == var) {
10235 /* If var is not used free it */
10237 unuse_triple(var, read);
10238 free_triple(state, read);
10239 free_triple(state, var);
10242 /* Release the phi function */
10243 release_triple(state, phi);
10250 * Register conflict resolution
10251 * =========================================================
10254 static struct reg_info find_def_color(
10255 struct compile_state *state, struct triple *def)
10257 struct triple_set *set;
10258 struct reg_info info;
10259 info.reg = REG_UNSET;
10261 if (!triple_is_def(state, def)) {
10264 info = arch_reg_lhs(state, def, 0);
10265 if (info.reg >= MAX_REGISTERS) {
10266 info.reg = REG_UNSET;
10268 for(set = def->use; set; set = set->next) {
10269 struct reg_info tinfo;
10271 i = find_rhs_use(state, set->member, def);
10275 tinfo = arch_reg_rhs(state, set->member, i);
10276 if (tinfo.reg >= MAX_REGISTERS) {
10277 tinfo.reg = REG_UNSET;
10279 if ((tinfo.reg != REG_UNSET) &&
10280 (info.reg != REG_UNSET) &&
10281 (tinfo.reg != info.reg)) {
10282 internal_error(state, def, "register conflict");
10284 if ((info.regcm & tinfo.regcm) == 0) {
10285 internal_error(state, def, "regcm conflict %x & %x == 0",
10286 info.regcm, tinfo.regcm);
10288 if (info.reg == REG_UNSET) {
10289 info.reg = tinfo.reg;
10291 info.regcm &= tinfo.regcm;
10293 if (info.reg >= MAX_REGISTERS) {
10294 internal_error(state, def, "register out of range");
10299 static struct reg_info find_lhs_pre_color(
10300 struct compile_state *state, struct triple *ins, int index)
10302 struct reg_info info;
10304 zrhs = TRIPLE_RHS(ins->sizes);
10305 zlhs = TRIPLE_LHS(ins->sizes);
10306 if (!zlhs && triple_is_def(state, ins)) {
10309 if (index >= zlhs) {
10310 internal_error(state, ins, "Bad lhs %d", index);
10312 info = arch_reg_lhs(state, ins, index);
10313 for(i = 0; i < zrhs; i++) {
10314 struct reg_info rinfo;
10315 rinfo = arch_reg_rhs(state, ins, i);
10316 if ((info.reg == rinfo.reg) &&
10317 (rinfo.reg >= MAX_REGISTERS)) {
10318 struct reg_info tinfo;
10319 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
10320 info.reg = tinfo.reg;
10321 info.regcm &= tinfo.regcm;
10325 if (info.reg >= MAX_REGISTERS) {
10326 info.reg = REG_UNSET;
10331 static struct reg_info find_rhs_post_color(
10332 struct compile_state *state, struct triple *ins, int index);
10334 static struct reg_info find_lhs_post_color(
10335 struct compile_state *state, struct triple *ins, int index)
10337 struct triple_set *set;
10338 struct reg_info info;
10339 struct triple *lhs;
10341 fprintf(stderr, "find_lhs_post_color(%p, %d)\n",
10344 if ((index == 0) && triple_is_def(state, ins)) {
10347 else if (index < TRIPLE_LHS(ins->sizes)) {
10348 lhs = LHS(ins, index);
10351 internal_error(state, ins, "Bad lhs %d", index);
10354 info = arch_reg_lhs(state, ins, index);
10355 if (info.reg >= MAX_REGISTERS) {
10356 info.reg = REG_UNSET;
10358 for(set = lhs->use; set; set = set->next) {
10359 struct reg_info rinfo;
10360 struct triple *user;
10362 user = set->member;
10363 zrhs = TRIPLE_RHS(user->sizes);
10364 for(i = 0; i < zrhs; i++) {
10365 if (RHS(user, i) != lhs) {
10368 rinfo = find_rhs_post_color(state, user, i);
10369 if ((info.reg != REG_UNSET) &&
10370 (rinfo.reg != REG_UNSET) &&
10371 (info.reg != rinfo.reg)) {
10372 internal_error(state, ins, "register conflict");
10374 if ((info.regcm & rinfo.regcm) == 0) {
10375 internal_error(state, ins, "regcm conflict %x & %x == 0",
10376 info.regcm, rinfo.regcm);
10378 if (info.reg == REG_UNSET) {
10379 info.reg = rinfo.reg;
10381 info.regcm &= rinfo.regcm;
10385 fprintf(stderr, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
10386 ins, index, info.reg, info.regcm);
10391 static struct reg_info find_rhs_post_color(
10392 struct compile_state *state, struct triple *ins, int index)
10394 struct reg_info info, rinfo;
10397 fprintf(stderr, "find_rhs_post_color(%p, %d)\n",
10400 rinfo = arch_reg_rhs(state, ins, index);
10401 zlhs = TRIPLE_LHS(ins->sizes);
10402 if (!zlhs && triple_is_def(state, ins)) {
10406 if (info.reg >= MAX_REGISTERS) {
10407 info.reg = REG_UNSET;
10409 for(i = 0; i < zlhs; i++) {
10410 struct reg_info linfo;
10411 linfo = arch_reg_lhs(state, ins, i);
10412 if ((linfo.reg == rinfo.reg) &&
10413 (linfo.reg >= MAX_REGISTERS)) {
10414 struct reg_info tinfo;
10415 tinfo = find_lhs_post_color(state, ins, i);
10416 if (tinfo.reg >= MAX_REGISTERS) {
10417 tinfo.reg = REG_UNSET;
10419 info.regcm &= linfo.reg;
10420 info.regcm &= tinfo.regcm;
10421 if (info.reg != REG_UNSET) {
10422 internal_error(state, ins, "register conflict");
10424 if (info.regcm == 0) {
10425 internal_error(state, ins, "regcm conflict");
10427 info.reg = tinfo.reg;
10431 fprintf(stderr, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
10432 ins, index, info.reg, info.regcm);
10437 static struct reg_info find_lhs_color(
10438 struct compile_state *state, struct triple *ins, int index)
10440 struct reg_info pre, post, info;
10442 fprintf(stderr, "find_lhs_color(%p, %d)\n",
10445 pre = find_lhs_pre_color(state, ins, index);
10446 post = find_lhs_post_color(state, ins, index);
10447 if ((pre.reg != post.reg) &&
10448 (pre.reg != REG_UNSET) &&
10449 (post.reg != REG_UNSET)) {
10450 internal_error(state, ins, "register conflict");
10452 info.regcm = pre.regcm & post.regcm;
10453 info.reg = pre.reg;
10454 if (info.reg == REG_UNSET) {
10455 info.reg = post.reg;
10458 fprintf(stderr, "find_lhs_color(%p, %d) -> ( %d, %x)\n",
10459 ins, index, info.reg, info.regcm);
10464 static struct triple *post_copy(struct compile_state *state, struct triple *ins)
10466 struct triple_set *entry, *next;
10467 struct triple *out;
10468 struct reg_info info, rinfo;
10470 info = arch_reg_lhs(state, ins, 0);
10471 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
10472 use_triple(RHS(out, 0), out);
10473 /* Get the users of ins to use out instead */
10474 for(entry = ins->use; entry; entry = next) {
10476 next = entry->next;
10477 if (entry->member == out) {
10480 i = find_rhs_use(state, entry->member, ins);
10484 rinfo = arch_reg_rhs(state, entry->member, i);
10485 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
10488 replace_rhs_use(state, ins, out, entry->member);
10490 transform_to_arch_instruction(state, out);
10494 static struct triple *pre_copy(
10495 struct compile_state *state, struct triple *ins, int index)
10497 /* Carefully insert enough operations so that I can
10498 * enter any operation with a GPR32.
10501 struct triple **expr;
10502 expr = &RHS(ins, index);
10503 in = pre_triple(state, ins, OP_COPY, (*expr)->type, *expr, 0);
10504 unuse_triple(*expr, ins);
10506 use_triple(RHS(in, 0), in);
10507 use_triple(in, ins);
10508 transform_to_arch_instruction(state, in);
10513 static void insert_copies_to_phi(struct compile_state *state)
10515 /* To get out of ssa form we insert moves on the incoming
10516 * edges to blocks containting phi functions.
10518 struct triple *first;
10519 struct triple *phi;
10521 /* Walk all of the operations to find the phi functions */
10522 first = RHS(state->main_function, 0);
10523 for(phi = first->next; phi != first ; phi = phi->next) {
10524 struct block_set *set;
10525 struct block *block;
10526 struct triple **slot;
10528 if (phi->op != OP_PHI) {
10531 phi->id |= TRIPLE_FLAG_POST_SPLIT;
10532 block = phi->u.block;
10533 slot = &RHS(phi, 0);
10534 /* Walk all of the incoming edges/blocks and insert moves.
10536 for(edge = 0, set = block->use; set; set = set->next, edge++) {
10537 struct block *eblock;
10538 struct triple *move;
10539 struct triple *val;
10540 struct triple *ptr;
10541 eblock = set->member;
10548 move = build_triple(state, OP_COPY, phi->type, val, 0,
10549 val->filename, val->line, val->col);
10550 move->u.block = eblock;
10551 move->id |= TRIPLE_FLAG_PRE_SPLIT;
10552 use_triple(val, move);
10555 unuse_triple(val, phi);
10556 use_triple(move, phi);
10558 /* Walk through the block backwards to find
10559 * an appropriate location for the OP_COPY.
10561 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
10562 struct triple **expr;
10563 if ((ptr == phi) || (ptr == val)) {
10566 expr = triple_rhs(state, ptr, 0);
10567 for(;expr; expr = triple_rhs(state, ptr, expr)) {
10568 if ((*expr) == phi) {
10574 if (triple_is_branch(state, ptr)) {
10575 internal_error(state, ptr,
10576 "Could not insert write to phi");
10578 insert_triple(state, ptr->next, move);
10579 if (eblock->last == ptr) {
10580 eblock->last = move;
10582 transform_to_arch_instruction(state, move);
10587 struct triple_reg_set {
10588 struct triple_reg_set *next;
10589 struct triple *member;
10590 struct triple *new;
10594 struct block *block;
10595 struct triple_reg_set *in;
10596 struct triple_reg_set *out;
10600 static int do_triple_set(struct triple_reg_set **head,
10601 struct triple *member, struct triple *new_member)
10603 struct triple_reg_set **ptr, *new;
10608 if ((*ptr)->member == member) {
10611 ptr = &(*ptr)->next;
10613 new = xcmalloc(sizeof(*new), "triple_set");
10614 new->member = member;
10615 new->new = new_member;
10621 static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
10623 struct triple_reg_set *entry, **ptr;
10627 if (entry->member == member) {
10628 *ptr = entry->next;
10633 ptr = &entry->next;
10638 static int in_triple(struct reg_block *rb, struct triple *in)
10640 return do_triple_set(&rb->in, in, 0);
10642 static void unin_triple(struct reg_block *rb, struct triple *unin)
10644 do_triple_unset(&rb->in, unin);
10647 static int out_triple(struct reg_block *rb, struct triple *out)
10649 return do_triple_set(&rb->out, out, 0);
10651 static void unout_triple(struct reg_block *rb, struct triple *unout)
10653 do_triple_unset(&rb->out, unout);
10656 static int initialize_regblock(struct reg_block *blocks,
10657 struct block *block, int vertex)
10659 struct block_set *user;
10660 if (!block || (blocks[block->vertex].block == block)) {
10664 /* Renumber the blocks in a convinient fashion */
10665 block->vertex = vertex;
10666 blocks[vertex].block = block;
10667 blocks[vertex].vertex = vertex;
10668 for(user = block->use; user; user = user->next) {
10669 vertex = initialize_regblock(blocks, user->member, vertex);
10674 static int phi_in(struct compile_state *state, struct reg_block *blocks,
10675 struct reg_block *rb, struct block *suc)
10677 /* Read the conditional input set of a successor block
10678 * (i.e. the input to the phi nodes) and place it in the
10679 * current blocks output set.
10681 struct block_set *set;
10682 struct triple *ptr;
10686 /* Find the edge I am coming in on */
10687 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
10688 if (set->member == rb->block) {
10693 internal_error(state, 0, "Not coming on a control edge?");
10695 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
10696 struct triple **slot, *expr, *ptr2;
10697 int out_change, done2;
10698 done = (ptr == suc->last);
10699 if (ptr->op != OP_PHI) {
10702 slot = &RHS(ptr, 0);
10704 out_change = out_triple(rb, expr);
10708 /* If we don't define the variable also plast it
10709 * in the current blocks input set.
10711 ptr2 = rb->block->first;
10712 for(done2 = 0; !done2; ptr2 = ptr2->next) {
10713 if (ptr2 == expr) {
10716 done2 = (ptr2 == rb->block->last);
10721 change |= in_triple(rb, expr);
10726 static int reg_in(struct compile_state *state, struct reg_block *blocks,
10727 struct reg_block *rb, struct block *suc)
10729 struct triple_reg_set *in_set;
10732 /* Read the input set of a successor block
10733 * and place it in the current blocks output set.
10735 in_set = blocks[suc->vertex].in;
10736 for(; in_set; in_set = in_set->next) {
10737 int out_change, done;
10738 struct triple *first, *last, *ptr;
10739 out_change = out_triple(rb, in_set->member);
10743 /* If we don't define the variable also place it
10744 * in the current blocks input set.
10746 first = rb->block->first;
10747 last = rb->block->last;
10749 for(ptr = first; !done; ptr = ptr->next) {
10750 if (ptr == in_set->member) {
10753 done = (ptr == last);
10758 change |= in_triple(rb, in_set->member);
10760 change |= phi_in(state, blocks, rb, suc);
10765 static int use_in(struct compile_state *state, struct reg_block *rb)
10767 /* Find the variables we use but don't define and add
10768 * it to the current blocks input set.
10770 #warning "FIXME is this O(N^2) algorithm bad?"
10771 struct block *block;
10772 struct triple *ptr;
10777 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
10778 struct triple **expr;
10779 done = (ptr == block->first);
10780 /* The variable a phi function uses depends on the
10781 * control flow, and is handled in phi_in, not
10784 if (ptr->op == OP_PHI) {
10787 expr = triple_rhs(state, ptr, 0);
10788 for(;expr; expr = triple_rhs(state, ptr, expr)) {
10789 struct triple *rhs, *test;
10795 /* See if rhs is defined in this block */
10796 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
10797 tdone = (test == block->first);
10803 /* If I still have a valid rhs add it to in */
10804 change |= in_triple(rb, rhs);
10810 static struct reg_block *compute_variable_lifetimes(
10811 struct compile_state *state)
10813 struct reg_block *blocks;
10816 sizeof(*blocks)*(state->last_vertex + 1), "reg_block");
10817 initialize_regblock(blocks, state->last_block, 0);
10821 for(i = 1; i <= state->last_vertex; i++) {
10822 struct reg_block *rb;
10824 /* Add the left successor's input set to in */
10825 if (rb->block->left) {
10826 change |= reg_in(state, blocks, rb, rb->block->left);
10828 /* Add the right successor's input set to in */
10829 if ((rb->block->right) &&
10830 (rb->block->right != rb->block->left)) {
10831 change |= reg_in(state, blocks, rb, rb->block->right);
10833 /* Add use to in... */
10834 change |= use_in(state, rb);
10840 static void free_variable_lifetimes(
10841 struct compile_state *state, struct reg_block *blocks)
10844 /* free in_set && out_set on each block */
10845 for(i = 1; i <= state->last_vertex; i++) {
10846 struct triple_reg_set *entry, *next;
10847 struct reg_block *rb;
10849 for(entry = rb->in; entry ; entry = next) {
10850 next = entry->next;
10851 do_triple_unset(&rb->in, entry->member);
10853 for(entry = rb->out; entry; entry = next) {
10854 next = entry->next;
10855 do_triple_unset(&rb->out, entry->member);
10862 typedef struct triple *(*wvl_cb_t)(
10863 struct compile_state *state,
10864 struct reg_block *blocks, struct triple_reg_set *live,
10865 struct reg_block *rb, struct triple *ins, void *arg);
10867 static void walk_variable_lifetimes(struct compile_state *state,
10868 struct reg_block *blocks, wvl_cb_t cb, void *arg)
10872 for(i = 1; i <= state->last_vertex; i++) {
10873 struct triple_reg_set *live;
10874 struct triple_reg_set *entry, *next;
10875 struct triple *ptr, *prev;
10876 struct reg_block *rb;
10877 struct block *block;
10880 /* Get the blocks */
10884 /* Copy out into live */
10886 for(entry = rb->out; entry; entry = next) {
10887 next = entry->next;
10888 do_triple_set(&live, entry->member, entry->new);
10890 /* Walk through the basic block calculating live */
10891 for(done = 0, ptr = block->last; !done; ptr = prev) {
10892 struct triple **expr, *result;
10895 done = (ptr == block->first);
10897 /* Ensure the current definition is in live */
10898 if (triple_is_def(state, ptr)) {
10899 do_triple_set(&live, ptr, 0);
10902 /* Inform the callback function of what is
10905 result = cb(state, blocks, live, rb, ptr, arg);
10907 /* Remove the current definition from live */
10908 do_triple_unset(&live, ptr);
10910 /* If the current instruction was deleted continue */
10912 if (block->last == ptr) {
10913 block->last = prev;
10919 /* Add the current uses to live.
10921 * It is safe to skip phi functions because they do
10922 * not have any block local uses, and the block
10923 * output sets already properly account for what
10924 * control flow depedent uses phi functions do have.
10926 if (ptr->op == OP_PHI) {
10929 expr = triple_rhs(state, ptr, 0);
10930 for(;expr; expr = triple_rhs(state, ptr, expr)) {
10931 /* If the triple is not a definition skip it. */
10932 if (!*expr || !triple_is_def(state, *expr)) {
10935 do_triple_set(&live, *expr, 0);
10939 for(entry = live; entry; entry = next) {
10940 next = entry->next;
10941 do_triple_unset(&live, entry->member);
10946 static int count_triples(struct compile_state *state)
10948 struct triple *first, *ins;
10950 first = RHS(state->main_function, 0);
10955 } while (ins != first);
10958 struct dead_triple {
10959 struct triple *triple;
10960 struct dead_triple *work_next;
10961 struct block *block;
10964 #define TRIPLE_FLAG_ALIVE 1
10968 static void awaken(
10969 struct compile_state *state,
10970 struct dead_triple *dtriple, struct triple **expr,
10971 struct dead_triple ***work_list_tail)
10973 struct triple *triple;
10974 struct dead_triple *dt;
10982 if (triple->id <= 0) {
10983 internal_error(state, triple, "bad triple id: %d",
10986 if (triple->op == OP_NOOP) {
10987 internal_warning(state, triple, "awakening noop?");
10990 dt = &dtriple[triple->id];
10991 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
10992 dt->flags |= TRIPLE_FLAG_ALIVE;
10993 if (!dt->work_next) {
10994 **work_list_tail = dt;
10995 *work_list_tail = &dt->work_next;
11000 static void eliminate_inefectual_code(struct compile_state *state)
11002 struct block *block;
11003 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
11005 struct triple *first, *ins;
11007 /* Setup the work list */
11009 work_list_tail = &work_list;
11011 first = RHS(state->main_function, 0);
11013 /* Count how many triples I have */
11014 triples = count_triples(state);
11016 /* Now put then in an array and mark all of the triples dead */
11017 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
11023 if (ins->op == OP_LABEL) {
11024 block = ins->u.block;
11026 dtriple[i].triple = ins;
11027 dtriple[i].block = block;
11028 dtriple[i].flags = 0;
11029 dtriple[i].color = ins->id;
11031 /* See if it is an operation we always keep */
11032 #warning "FIXME handle the case of killing a branch instruction"
11033 if (!triple_is_pure(state, ins) || triple_is_branch(state, ins)) {
11034 awaken(state, dtriple, &ins, &work_list_tail);
11038 } while(ins != first);
11040 struct dead_triple *dt;
11041 struct block_set *user;
11042 struct triple **expr;
11044 work_list = dt->work_next;
11046 work_list_tail = &work_list;
11048 /* Wake up the data depencencies of this triple */
11051 expr = triple_rhs(state, dt->triple, expr);
11052 awaken(state, dtriple, expr, &work_list_tail);
11055 expr = triple_lhs(state, dt->triple, expr);
11056 awaken(state, dtriple, expr, &work_list_tail);
11059 expr = triple_misc(state, dt->triple, expr);
11060 awaken(state, dtriple, expr, &work_list_tail);
11062 /* Wake up the forward control dependencies */
11064 expr = triple_targ(state, dt->triple, expr);
11065 awaken(state, dtriple, expr, &work_list_tail);
11067 /* Wake up the reverse control dependencies of this triple */
11068 for(user = dt->block->ipdomfrontier; user; user = user->next) {
11069 awaken(state, dtriple, &user->member->last, &work_list_tail);
11072 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
11073 if ((dt->triple->op == OP_NOOP) &&
11074 (dt->flags & TRIPLE_FLAG_ALIVE)) {
11075 internal_error(state, dt->triple, "noop effective?");
11077 dt->triple->id = dt->color; /* Restore the color */
11078 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11079 #warning "FIXME handle the case of killing a basic block"
11080 if (dt->block->first == dt->triple) {
11083 if (dt->block->last == dt->triple) {
11084 dt->block->last = dt->triple->prev;
11086 release_triple(state, dt->triple);
11093 static void insert_mandatory_copies(struct compile_state *state)
11095 struct triple *ins, *first;
11097 /* The object is with a minimum of inserted copies,
11098 * to resolve in fundamental register conflicts between
11099 * register value producers and consumers.
11100 * Theoretically we may be greater than minimal when we
11101 * are inserting copies before instructions but that
11102 * case should be rare.
11104 first = RHS(state->main_function, 0);
11107 struct triple_set *entry, *next;
11108 struct triple *tmp;
11109 struct reg_info info;
11110 unsigned reg, regcm;
11111 int do_post_copy, do_pre_copy;
11113 if (!triple_is_def(state, ins)) {
11116 /* Find the architecture specific color information */
11117 info = arch_reg_lhs(state, ins, 0);
11118 if (info.reg >= MAX_REGISTERS) {
11119 info.reg = REG_UNSET;
11123 regcm = arch_type_to_regcm(state, ins->type);
11124 do_post_copy = do_pre_copy = 0;
11126 /* Walk through the uses of ins and check for conflicts */
11127 for(entry = ins->use; entry; entry = next) {
11128 struct reg_info rinfo;
11130 next = entry->next;
11131 i = find_rhs_use(state, entry->member, ins);
11136 /* Find the users color requirements */
11137 rinfo = arch_reg_rhs(state, entry->member, i);
11138 if (rinfo.reg >= MAX_REGISTERS) {
11139 rinfo.reg = REG_UNSET;
11142 /* See if I need a pre_copy */
11143 if (rinfo.reg != REG_UNSET) {
11144 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
11149 regcm &= rinfo.regcm;
11150 regcm = arch_regcm_normalize(state, regcm);
11157 (((info.reg != REG_UNSET) &&
11158 (reg != REG_UNSET) &&
11159 (info.reg != reg)) ||
11160 ((info.regcm & regcm) == 0));
11163 regcm = info.regcm;
11164 /* Walk through the uses of insert and do a pre_copy or see if a post_copy is warranted */
11165 for(entry = ins->use; entry; entry = next) {
11166 struct reg_info rinfo;
11168 next = entry->next;
11169 i = find_rhs_use(state, entry->member, ins);
11174 /* Find the users color requirements */
11175 rinfo = arch_reg_rhs(state, entry->member, i);
11176 if (rinfo.reg >= MAX_REGISTERS) {
11177 rinfo.reg = REG_UNSET;
11180 /* Now see if it is time to do the pre_copy */
11181 if (rinfo.reg != REG_UNSET) {
11182 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
11183 ((regcm & rinfo.regcm) == 0) ||
11184 /* Don't let a mandatory coalesce sneak
11185 * into a operation that is marked to prevent
11188 ((reg != REG_UNNEEDED) &&
11189 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
11190 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
11193 struct triple *user;
11194 user = entry->member;
11195 if (RHS(user, i) != ins) {
11196 internal_error(state, user, "bad rhs");
11198 tmp = pre_copy(state, user, i);
11206 if ((regcm & rinfo.regcm) == 0) {
11208 struct triple *user;
11209 user = entry->member;
11210 if (RHS(user, i) != ins) {
11211 internal_error(state, user, "bad rhs");
11213 tmp = pre_copy(state, user, i);
11219 regcm &= rinfo.regcm;
11222 if (do_post_copy) {
11223 struct reg_info pre, post;
11224 tmp = post_copy(state, ins);
11225 pre = arch_reg_lhs(state, ins, 0);
11226 post = arch_reg_lhs(state, tmp, 0);
11227 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
11228 internal_error(state, tmp, "useless copy");
11233 } while(ins != first);
11237 struct live_range_edge;
11238 struct live_range_def;
11239 struct live_range {
11240 struct live_range_edge *edges;
11241 struct live_range_def *defs;
11242 /* Note. The list pointed to by defs is kept in order.
11243 * That is baring splits in the flow control
11244 * defs dominates defs->next wich dominates defs->next->next
11251 struct live_range *group_next, **group_prev;
11254 struct live_range_edge {
11255 struct live_range_edge *next;
11256 struct live_range *node;
11259 struct live_range_def {
11260 struct live_range_def *next;
11261 struct live_range_def *prev;
11262 struct live_range *lr;
11263 struct triple *def;
11267 #define LRE_HASH_SIZE 2048
11269 struct lre_hash *next;
11270 struct live_range *left;
11271 struct live_range *right;
11276 struct lre_hash *hash[LRE_HASH_SIZE];
11277 struct reg_block *blocks;
11278 struct live_range_def *lrd;
11279 struct live_range *lr;
11280 struct live_range *low, **low_tail;
11281 struct live_range *high, **high_tail;
11284 int passes, max_passes;
11285 #define MAX_ALLOCATION_PASSES 100
11289 static unsigned regc_max_size(struct compile_state *state, int classes)
11294 for(i = 0; i < MAX_REGC; i++) {
11295 if (classes & (1 << i)) {
11297 size = arch_regc_size(state, i);
11298 if (size > max_size) {
11306 static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
11308 unsigned equivs[MAX_REG_EQUIVS];
11310 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
11311 internal_error(state, 0, "invalid register");
11313 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
11314 internal_error(state, 0, "invalid register");
11316 arch_reg_equivs(state, equivs, reg1);
11317 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11318 if (equivs[i] == reg2) {
11325 static void reg_fill_used(struct compile_state *state, char *used, int reg)
11327 unsigned equivs[MAX_REG_EQUIVS];
11329 if (reg == REG_UNNEEDED) {
11332 arch_reg_equivs(state, equivs, reg);
11333 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11334 used[equivs[i]] = 1;
11339 static void reg_inc_used(struct compile_state *state, char *used, int reg)
11341 unsigned equivs[MAX_REG_EQUIVS];
11343 if (reg == REG_UNNEEDED) {
11346 arch_reg_equivs(state, equivs, reg);
11347 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11348 used[equivs[i]] += 1;
11353 static unsigned int hash_live_edge(
11354 struct live_range *left, struct live_range *right)
11356 unsigned int hash, val;
11357 unsigned long lval, rval;
11358 lval = ((unsigned long)left)/sizeof(struct live_range);
11359 rval = ((unsigned long)right)/sizeof(struct live_range);
11364 hash = (hash *263) + val;
11369 hash = (hash *263) + val;
11371 hash = hash & (LRE_HASH_SIZE - 1);
11375 static struct lre_hash **lre_probe(struct reg_state *rstate,
11376 struct live_range *left, struct live_range *right)
11378 struct lre_hash **ptr;
11379 unsigned int index;
11380 /* Ensure left <= right */
11381 if (left > right) {
11382 struct live_range *tmp;
11387 index = hash_live_edge(left, right);
11389 ptr = &rstate->hash[index];
11390 while((*ptr) && ((*ptr)->left != left) && ((*ptr)->right != right)) {
11391 ptr = &(*ptr)->next;
11396 static int interfere(struct reg_state *rstate,
11397 struct live_range *left, struct live_range *right)
11399 struct lre_hash **ptr;
11400 ptr = lre_probe(rstate, left, right);
11401 return ptr && *ptr;
11404 static void add_live_edge(struct reg_state *rstate,
11405 struct live_range *left, struct live_range *right)
11407 /* FIXME the memory allocation overhead is noticeable here... */
11408 struct lre_hash **ptr, *new_hash;
11409 struct live_range_edge *edge;
11411 if (left == right) {
11414 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
11417 /* Ensure left <= right */
11418 if (left > right) {
11419 struct live_range *tmp;
11424 ptr = lre_probe(rstate, left, right);
11428 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
11429 new_hash->next = *ptr;
11430 new_hash->left = left;
11431 new_hash->right = right;
11434 edge = xmalloc(sizeof(*edge), "live_range_edge");
11435 edge->next = left->edges;
11436 edge->node = right;
11437 left->edges = edge;
11440 edge = xmalloc(sizeof(*edge), "live_range_edge");
11441 edge->next = right->edges;
11443 right->edges = edge;
11444 right->degree += 1;
11447 static void remove_live_edge(struct reg_state *rstate,
11448 struct live_range *left, struct live_range *right)
11450 struct live_range_edge *edge, **ptr;
11451 struct lre_hash **hptr, *entry;
11452 hptr = lre_probe(rstate, left, right);
11453 if (!hptr || !*hptr) {
11457 *hptr = entry->next;
11460 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
11462 if (edge->node == right) {
11464 memset(edge, 0, sizeof(*edge));
11469 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
11471 if (edge->node == left) {
11473 memset(edge, 0, sizeof(*edge));
11480 static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
11482 struct live_range_edge *edge, *next;
11483 for(edge = range->edges; edge; edge = next) {
11485 remove_live_edge(rstate, range, edge->node);
11490 /* Interference graph...
11492 * new(n) --- Return a graph with n nodes but no edges.
11493 * add(g,x,y) --- Return a graph including g with an between x and y
11494 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
11495 * x and y in the graph g
11496 * degree(g, x) --- Return the degree of the node x in the graph g
11497 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
11499 * Implement with a hash table && a set of adjcency vectors.
11500 * The hash table supports constant time implementations of add and interfere.
11501 * The adjacency vectors support an efficient implementation of neighbors.
11505 * +---------------------------------------------------+
11506 * | +--------------+ |
11508 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
11510 * -- In simplify implment optimistic coloring... (No backtracking)
11511 * -- Implement Rematerialization it is the only form of spilling we can perform
11512 * Essentially this means dropping a constant from a register because
11513 * we can regenerate it later.
11515 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
11516 * coalesce at phi points...
11517 * --- Bias coloring if at all possible do the coalesing a compile time.
11522 static void different_colored(
11523 struct compile_state *state, struct reg_state *rstate,
11524 struct triple *parent, struct triple *ins)
11526 struct live_range *lr;
11527 struct triple **expr;
11528 lr = rstate->lrd[ins->id].lr;
11529 expr = triple_rhs(state, ins, 0);
11530 for(;expr; expr = triple_rhs(state, ins, expr)) {
11531 struct live_range *lr2;
11532 if (!*expr || (*expr == parent) || (*expr == ins)) {
11535 lr2 = rstate->lrd[(*expr)->id].lr;
11536 if (lr->color == lr2->color) {
11537 internal_error(state, ins, "live range too big");
11543 static struct live_range *coalesce_ranges(
11544 struct compile_state *state, struct reg_state *rstate,
11545 struct live_range *lr1, struct live_range *lr2)
11547 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
11553 if (!lr1->defs || !lr2->defs) {
11554 internal_error(state, 0,
11555 "cannot coalese dead live ranges");
11557 if ((lr1->color == REG_UNNEEDED) ||
11558 (lr2->color == REG_UNNEEDED)) {
11559 internal_error(state, 0,
11560 "cannot coalesce live ranges without a possible color");
11562 if ((lr1->color != lr2->color) &&
11563 (lr1->color != REG_UNSET) &&
11564 (lr2->color != REG_UNSET)) {
11565 internal_error(state, lr1->defs->def,
11566 "cannot coalesce live ranges of different colors");
11568 color = lr1->color;
11569 if (color == REG_UNSET) {
11570 color = lr2->color;
11572 classes = lr1->classes & lr2->classes;
11574 internal_error(state, lr1->defs->def,
11575 "cannot coalesce live ranges with dissimilar register classes");
11577 /* If there is a clear dominate live range put it in lr1,
11578 * For purposes of this test phi functions are
11579 * considered dominated by the definitions that feed into
11582 if ((lr1->defs->prev->def->op == OP_PHI) ||
11583 ((lr2->defs->prev->def->op != OP_PHI) &&
11584 tdominates(state, lr2->defs->def, lr1->defs->def))) {
11585 struct live_range *tmp;
11591 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
11592 fprintf(stderr, "lr1 post\n");
11594 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
11595 fprintf(stderr, "lr1 pre\n");
11597 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
11598 fprintf(stderr, "lr2 post\n");
11600 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
11601 fprintf(stderr, "lr2 pre\n");
11605 fprintf(stderr, "coalesce color1(%p): %3d color2(%p) %3d\n",
11612 lr1->classes = classes;
11613 /* Append lr2 onto lr1 */
11614 #warning "FIXME should this be a merge instead of a splice?"
11616 mid1 = lr1->defs->prev;
11618 end = lr2->defs->prev;
11626 /* Fixup the live range in the added live range defs */
11631 } while(lrd != head);
11633 /* Mark lr2 as free. */
11635 lr2->color = REG_UNNEEDED;
11639 internal_error(state, 0, "lr1->defs == 0 ?");
11642 lr1->color = color;
11643 lr1->classes = classes;
11648 static struct live_range_def *live_range_head(
11649 struct compile_state *state, struct live_range *lr,
11650 struct live_range_def *last)
11652 struct live_range_def *result;
11657 else if (!tdominates(state, lr->defs->def, last->next->def)) {
11658 result = last->next;
11663 static struct live_range_def *live_range_end(
11664 struct compile_state *state, struct live_range *lr,
11665 struct live_range_def *last)
11667 struct live_range_def *result;
11670 result = lr->defs->prev;
11672 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
11673 result = last->prev;
11679 static void initialize_live_ranges(
11680 struct compile_state *state, struct reg_state *rstate)
11682 struct triple *ins, *first;
11683 size_t count, size;
11686 first = RHS(state->main_function, 0);
11687 /* First count how many instructions I have.
11689 count = count_triples(state);
11690 /* Potentially I need one live range definitions for each
11691 * instruction, plus an extra for the split routines.
11693 rstate->defs = count + 1;
11694 /* Potentially I need one live range for each instruction
11695 * plus an extra for the dummy live range.
11697 rstate->ranges = count + 1;
11698 size = sizeof(rstate->lrd[0]) * rstate->defs;
11699 rstate->lrd = xcmalloc(size, "live_range_def");
11700 size = sizeof(rstate->lr[0]) * rstate->ranges;
11701 rstate->lr = xcmalloc(size, "live_range");
11703 /* Setup the dummy live range */
11704 rstate->lr[0].classes = 0;
11705 rstate->lr[0].color = REG_UNSET;
11706 rstate->lr[0].defs = 0;
11710 /* If the triple is a variable give it a live range */
11711 if (triple_is_def(state, ins)) {
11712 struct reg_info info;
11713 /* Find the architecture specific color information */
11714 info = find_def_color(state, ins);
11717 rstate->lr[i].defs = &rstate->lrd[j];
11718 rstate->lr[i].color = info.reg;
11719 rstate->lr[i].classes = info.regcm;
11720 rstate->lr[i].degree = 0;
11721 rstate->lrd[j].lr = &rstate->lr[i];
11723 /* Otherwise give the triple the dummy live range. */
11725 rstate->lrd[j].lr = &rstate->lr[0];
11728 /* Initalize the live_range_def */
11729 rstate->lrd[j].next = &rstate->lrd[j];
11730 rstate->lrd[j].prev = &rstate->lrd[j];
11731 rstate->lrd[j].def = ins;
11732 rstate->lrd[j].orig_id = ins->id;
11737 } while(ins != first);
11738 rstate->ranges = i;
11741 /* Make a second pass to handle achitecture specific register
11746 int zlhs, zrhs, i, j;
11747 if (ins->id > rstate->defs) {
11748 internal_error(state, ins, "bad id");
11751 /* Walk through the template of ins and coalesce live ranges */
11752 zlhs = TRIPLE_LHS(ins->sizes);
11753 if ((zlhs == 0) && triple_is_def(state, ins)) {
11756 zrhs = TRIPLE_RHS(ins->sizes);
11758 for(i = 0; i < zlhs; i++) {
11759 struct reg_info linfo;
11760 struct live_range_def *lhs;
11761 linfo = arch_reg_lhs(state, ins, i);
11762 if (linfo.reg < MAX_REGISTERS) {
11765 if (triple_is_def(state, ins)) {
11766 lhs = &rstate->lrd[ins->id];
11768 lhs = &rstate->lrd[LHS(ins, i)->id];
11770 for(j = 0; j < zrhs; j++) {
11771 struct reg_info rinfo;
11772 struct live_range_def *rhs;
11773 rinfo = arch_reg_rhs(state, ins, j);
11774 if (rinfo.reg < MAX_REGISTERS) {
11777 rhs = &rstate->lrd[RHS(ins, i)->id];
11778 if (rinfo.reg == linfo.reg) {
11779 coalesce_ranges(state, rstate,
11785 } while(ins != first);
11788 static struct triple *graph_ins(
11789 struct compile_state *state,
11790 struct reg_block *blocks, struct triple_reg_set *live,
11791 struct reg_block *rb, struct triple *ins, void *arg)
11793 struct reg_state *rstate = arg;
11794 struct live_range *def;
11795 struct triple_reg_set *entry;
11797 /* If the triple is not a definition
11798 * we do not have a definition to add to
11799 * the interference graph.
11801 if (!triple_is_def(state, ins)) {
11804 def = rstate->lrd[ins->id].lr;
11806 /* Create an edge between ins and everything that is
11807 * alive, unless the live_range cannot share
11808 * a physical register with ins.
11810 for(entry = live; entry; entry = entry->next) {
11811 struct live_range *lr;
11812 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
11813 internal_error(state, 0, "bad entry?");
11815 lr = rstate->lrd[entry->member->id].lr;
11819 if (!arch_regcm_intersect(def->classes, lr->classes)) {
11822 add_live_edge(rstate, def, lr);
11828 static struct triple *print_interference_ins(
11829 struct compile_state *state,
11830 struct reg_block *blocks, struct triple_reg_set *live,
11831 struct reg_block *rb, struct triple *ins, void *arg)
11833 struct reg_state *rstate = arg;
11834 struct live_range *lr;
11836 lr = rstate->lrd[ins->id].lr;
11837 display_triple(stdout, ins);
11840 struct live_range_def *lrd;
11844 printf(" %-10p", lrd->def);
11846 } while(lrd != lr->defs);
11850 struct triple_reg_set *entry;
11852 for(entry = live; entry; entry = entry->next) {
11853 printf(" %-10p", entry->member);
11858 struct live_range_edge *entry;
11860 for(entry = lr->edges; entry; entry = entry->next) {
11861 struct live_range_def *lrd;
11862 lrd = entry->node->defs;
11864 printf(" %-10p", lrd->def);
11866 } while(lrd != entry->node->defs);
11871 if (triple_is_branch(state, ins)) {
11877 static int coalesce_live_ranges(
11878 struct compile_state *state, struct reg_state *rstate)
11880 /* At the point where a value is moved from one
11881 * register to another that value requires two
11882 * registers, thus increasing register pressure.
11883 * Live range coaleescing reduces the register
11884 * pressure by keeping a value in one register
11887 * In the case of a phi function all paths leading
11888 * into it must be allocated to the same register
11889 * otherwise the phi function may not be removed.
11891 * Forcing a value to stay in a single register
11892 * for an extended period of time does have
11893 * limitations when applied to non homogenous
11896 * The two cases I have identified are:
11897 * 1) Two forced register assignments may
11899 * 2) Registers may go unused because they
11900 * are only good for storing the value
11901 * and not manipulating it.
11903 * Because of this I need to split live ranges,
11904 * even outside of the context of coalesced live
11905 * ranges. The need to split live ranges does
11906 * impose some constraints on live range coalescing.
11908 * - Live ranges may not be coalesced across phi
11909 * functions. This creates a 2 headed live
11910 * range that cannot be sanely split.
11912 * - phi functions (coalesced in initialize_live_ranges)
11913 * are handled as pre split live ranges so we will
11914 * never attempt to split them.
11920 for(i = 0; i <= rstate->ranges; i++) {
11921 struct live_range *lr1;
11922 struct live_range_def *lrd1;
11923 lr1 = &rstate->lr[i];
11927 lrd1 = live_range_end(state, lr1, 0);
11928 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
11929 struct triple_set *set;
11930 if (lrd1->def->op != OP_COPY) {
11933 /* Skip copies that are the result of a live range split. */
11934 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
11937 for(set = lrd1->def->use; set; set = set->next) {
11938 struct live_range_def *lrd2;
11939 struct live_range *lr2, *res;
11941 lrd2 = &rstate->lrd[set->member->id];
11943 /* Don't coalesce with instructions
11944 * that are the result of a live range
11947 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
11950 lr2 = rstate->lrd[set->member->id].lr;
11954 if ((lr1->color != lr2->color) &&
11955 (lr1->color != REG_UNSET) &&
11956 (lr2->color != REG_UNSET)) {
11959 if ((lr1->classes & lr2->classes) == 0) {
11963 if (interfere(rstate, lr1, lr2)) {
11967 res = coalesce_ranges(state, rstate, lr1, lr2);
11980 struct coalesce_conflict {
11981 struct triple *ins;
11984 static struct triple *spot_coalesce_conflict(struct compile_state *state,
11985 struct reg_block *blocks, struct triple_reg_set *live,
11986 struct reg_block *rb, struct triple *ins, void *arg)
11988 struct coalesce_conflict *conflict = arg;
11989 int zlhs, zrhs, i, j;
11992 /* See if we have a mandatory coalesce operation between
11993 * a lhs and a rhs value. If so and the rhs value is also
11994 * alive then this triple needs to be pre copied. Otherwise
11995 * we would have two definitions in the same live range simultaneously
11999 zlhs = TRIPLE_LHS(ins->sizes);
12000 if ((zlhs == 0) && triple_is_def(state, ins)) {
12003 zrhs = TRIPLE_RHS(ins->sizes);
12004 for(i = 0; (i < zlhs) && (found == -1); i++) {
12005 struct reg_info linfo;
12006 linfo = arch_reg_lhs(state, ins, i);
12007 if (linfo.reg < MAX_REGISTERS) {
12010 for(j = 0; (j < zrhs) && (found == -1); j++) {
12011 struct reg_info rinfo;
12012 struct triple *rhs;
12013 struct triple_reg_set *set;
12014 rinfo = arch_reg_rhs(state, ins, j);
12015 if (rinfo.reg != linfo.reg) {
12019 for(set = live; set && (found == -1); set = set->next) {
12020 if (set->member == rhs) {
12026 /* Only update conflict if we are the least dominated conflict */
12027 if ((found != -1) &&
12028 (!conflict->ins || tdominates(state, ins, conflict->ins))) {
12029 conflict->ins = ins;
12030 conflict->index = found;
12035 static void resolve_coalesce_conflict(
12036 struct compile_state *state, struct coalesce_conflict *conflict)
12038 struct triple *copy;
12039 copy = pre_copy(state, conflict->ins, conflict->index);
12040 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12044 static struct triple *spot_tangle(struct compile_state *state,
12045 struct reg_block *blocks, struct triple_reg_set *live,
12046 struct reg_block *rb, struct triple *ins, void *arg)
12048 struct triple **tangle = arg;
12049 char used[MAX_REGISTERS];
12050 struct triple_reg_set *set;
12052 /* Find out which registers have multiple uses at this point */
12053 memset(used, 0, sizeof(used));
12054 for(set = live; set; set = set->next) {
12055 struct reg_info info;
12056 info = find_lhs_color(state, set->member, 0);
12057 if (info.reg == REG_UNSET) {
12060 reg_inc_used(state, used, info.reg);
12063 /* Now find the least dominated definition of a register in
12064 * conflict I have seen so far.
12066 for(set = live; set; set = set->next) {
12067 struct reg_info info;
12068 info = find_lhs_color(state, set->member, 0);
12069 if (used[info.reg] < 2) {
12072 if (!*tangle || tdominates(state, set->member, *tangle)) {
12073 *tangle = set->member;
12079 static void resolve_tangle(struct compile_state *state, struct triple *tangle)
12081 struct reg_info info, uinfo;
12082 struct triple_set *set, *next;
12083 struct triple *copy;
12086 fprintf(stderr, "Resolving tangle: %p\n", tangle);
12087 print_blocks(state, stderr);
12089 info = find_lhs_color(state, tangle, 0);
12091 fprintf(stderr, "color: %d\n", info.reg);
12093 for(set = tangle->use; set; set = next) {
12094 struct triple *user;
12097 user = set->member;
12098 zrhs = TRIPLE_RHS(user->sizes);
12099 for(i = 0; i < zrhs; i++) {
12100 if (RHS(user, i) != tangle) {
12103 uinfo = find_rhs_post_color(state, user, i);
12105 fprintf(stderr, "%p rhs %d color: %d\n",
12106 user, i, uinfo.reg);
12108 if (uinfo.reg == info.reg) {
12109 copy = pre_copy(state, user, i);
12110 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12114 uinfo = find_lhs_pre_color(state, tangle, 0);
12116 fprintf(stderr, "pre color: %d\n", uinfo.reg);
12118 if (uinfo.reg == info.reg) {
12119 copy = post_copy(state, tangle);
12120 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12125 struct least_conflict {
12126 struct reg_state *rstate;
12127 struct live_range *ref_range;
12128 struct triple *ins;
12129 struct triple_reg_set *live;
12132 static struct triple *least_conflict(struct compile_state *state,
12133 struct reg_block *blocks, struct triple_reg_set *live,
12134 struct reg_block *rb, struct triple *ins, void *arg)
12136 struct least_conflict *conflict = arg;
12137 struct live_range_edge *edge;
12138 struct triple_reg_set *set;
12141 #warning "FIXME handle instructions with left hand sides..."
12142 /* Only instructions that introduce a new definition
12143 * can be the conflict instruction.
12145 if (!triple_is_def(state, ins)) {
12149 /* See if live ranges at this instruction are a
12150 * strict subset of the live ranges that are in conflict.
12153 for(set = live; set; set = set->next) {
12154 struct live_range *lr;
12155 lr = conflict->rstate->lrd[set->member->id].lr;
12156 for(edge = conflict->ref_range->edges; edge; edge = edge->next) {
12157 if (edge->node == lr) {
12161 if (!edge && (lr != conflict->ref_range)) {
12170 /* See if there is an uncolored member in this subset.
12172 for(set = live; set; set = set->next) {
12173 struct live_range *lr;
12174 lr = conflict->rstate->lrd[set->member->id].lr;
12175 if (lr->color == REG_UNSET) {
12179 if (!set && (conflict->ref_range != REG_UNSET)) {
12184 /* Find the instruction with the largest possible subset of
12185 * conflict ranges and that dominates any other instruction
12186 * with an equal sized set of conflicting ranges.
12188 if ((count > conflict->count) ||
12189 ((count == conflict->count) &&
12190 tdominates(state, ins, conflict->ins))) {
12191 struct triple_reg_set *next;
12192 /* Remember the canidate instruction */
12193 conflict->ins = ins;
12194 conflict->count = count;
12195 /* Free the old collection of live registers */
12196 for(set = conflict->live; set; set = next) {
12198 do_triple_unset(&conflict->live, set->member);
12200 conflict->live = 0;
12201 /* Rember the registers that are alive but do not feed
12202 * into or out of conflict->ins.
12204 for(set = live; set; set = set->next) {
12205 struct triple **expr;
12206 if (set->member == ins) {
12209 expr = triple_rhs(state, ins, 0);
12210 for(;expr; expr = triple_rhs(state, ins, expr)) {
12211 if (*expr == set->member) {
12215 expr = triple_lhs(state, ins, 0);
12216 for(; expr; expr = triple_lhs(state, ins, expr)) {
12217 if (*expr == set->member) {
12221 do_triple_set(&conflict->live, set->member, set->new);
12228 static void find_range_conflict(struct compile_state *state,
12229 struct reg_state *rstate, char *used, struct live_range *ref_range,
12230 struct least_conflict *conflict)
12232 /* there are 3 kinds ways conflicts can occure.
12233 * 1) the life time of 2 values simply overlap.
12234 * 2) the 2 values feed into the same instruction.
12235 * 3) the 2 values feed into a phi function.
12238 /* find the instruction where the problematic conflict comes
12239 * into existance. that the instruction where all of
12240 * the values are alive, and among such instructions it is
12241 * the least dominated one.
12243 * a value is alive an an instruction if either;
12244 * 1) the value defintion dominates the instruction and there
12245 * is a use at or after that instrction
12246 * 2) the value definition feeds into a phi function in the
12247 * same block as the instruction. and the phi function
12248 * is at or after the instruction.
12250 memset(conflict, 0, sizeof(*conflict));
12251 conflict->rstate = rstate;
12252 conflict->ref_range = ref_range;
12254 conflict->count = 0;
12255 conflict->live = 0;
12256 walk_variable_lifetimes(state, rstate->blocks, least_conflict, conflict);
12258 if (!conflict->ins) {
12259 internal_error(state, 0, "No conflict ins?");
12261 if (!conflict->live) {
12262 internal_error(state, 0, "No conflict live?");
12267 static struct triple *split_constrained_range(struct compile_state *state,
12268 struct reg_state *rstate, char *used, struct least_conflict *conflict)
12270 unsigned constrained_size;
12271 struct triple *new, *constrained;
12272 struct triple_reg_set *cset;
12273 /* Find a range that is having problems because it is
12274 * artificially constrained.
12276 constrained_size = ~0;
12279 for(cset = conflict->live; cset; cset = cset->next) {
12280 struct triple_set *set;
12281 struct reg_info info;
12283 unsigned cur_size, size;
12284 /* Skip the live range that starts with conflict->ins */
12285 if (cset->member == conflict->ins) {
12288 /* Find how many registers this value can potentially
12291 classes = arch_type_to_regcm(state, cset->member->type);
12292 size = regc_max_size(state, classes);
12294 /* Find how many registers we allow this value to
12297 info = arch_reg_lhs(state, cset->member, 0);
12298 #warning "FIXME do I need a call to arch_reg_rhs around here somewhere?"
12299 if ((info.reg == REG_UNSET) || (info.reg >= MAX_REGISTERS)) {
12300 cur_size = regc_max_size(state, info.regcm);
12304 /* If this live_range feeds into conflict->ins
12305 * splitting it is unlikely to help.
12307 for(set = cset->member->use; set; set = set->next) {
12308 if (set->member == conflict->ins) {
12313 /* If there is no difference between potential and
12314 * actual register count there is nothing to do.
12316 if (cur_size >= size) {
12319 /* Of the constrained registers deal with the
12320 * most constrained one first.
12322 if (!constrained ||
12323 (size < constrained_size)) {
12324 constrained = cset->member;
12325 constrained_size = size;
12330 new = post_copy(state, constrained);
12331 new->id |= TRIPLE_FLAG_POST_SPLIT;
12336 static int split_ranges(
12337 struct compile_state *state, struct reg_state *rstate,
12338 char *used, struct live_range *range)
12340 struct triple *new;
12342 if ((range->color == REG_UNNEEDED) ||
12343 (rstate->passes >= rstate->max_passes)) {
12347 /* If I can't allocate a register something needs to be split */
12348 if (arch_select_free_register(state, used, range->classes) == REG_UNSET) {
12349 struct least_conflict conflict;
12351 /* Find where in the set of registers the conflict
12354 find_range_conflict(state, rstate, used, range, &conflict);
12356 /* If a range has been artifically constrained split it */
12357 new = split_constrained_range(state, rstate, used, &conflict);
12360 /* Ideally I would split the live range that will not be used
12361 * for the longest period of time in hopes that this will
12362 * (a) allow me to spill a register or
12363 * (b) allow me to place a value in another register.
12365 * So far I don't have a test case for this, the resolving
12366 * of mandatory constraints has solved all of my
12367 * know issues. So I have choosen not to write any
12368 * code until I cat get a better feel for cases where
12369 * it would be useful to have.
12372 #warning "WISHLIST implement live range splitting..."
12377 rstate->lrd[rstate->defs].orig_id = new->id;
12378 new->id = rstate->defs;
12381 fprintf(stderr, "new: %p\n", new);
12388 #if DEBUG_COLOR_GRAPH > 1
12389 #define cgdebug_printf(...) fprintf(stdout, __VA_ARGS__)
12390 #define cgdebug_flush() fflush(stdout)
12391 #elif DEBUG_COLOR_GRAPH == 1
12392 #define cgdebug_printf(...) fprintf(stderr, __VA_ARGS__)
12393 #define cgdebug_flush() fflush(stderr)
12395 #define cgdebug_printf(...)
12396 #define cgdebug_flush()
12400 static int select_free_color(struct compile_state *state,
12401 struct reg_state *rstate, struct live_range *range)
12403 struct triple_set *entry;
12404 struct live_range_def *lrd;
12405 struct live_range_def *phi;
12406 struct live_range_edge *edge;
12407 char used[MAX_REGISTERS];
12408 struct triple **expr;
12410 /* Instead of doing just the trivial color select here I try
12411 * a few extra things because a good color selection will help reduce
12415 /* Find the registers currently in use */
12416 memset(used, 0, sizeof(used));
12417 for(edge = range->edges; edge; edge = edge->next) {
12418 if (edge->node->color == REG_UNSET) {
12421 reg_fill_used(state, used, edge->node->color);
12423 #if DEBUG_COLOR_GRAPH > 1
12427 for(edge = range->edges; edge; edge = edge->next) {
12430 cgdebug_printf("\n%s edges: %d @%s:%d.%d\n",
12431 tops(range->def->op), i,
12432 range->def->filename, range->def->line, range->def->col);
12433 for(i = 0; i < MAX_REGISTERS; i++) {
12435 cgdebug_printf("used: %s\n",
12442 #warning "FIXME detect conflicts caused by the source and destination being the same register"
12444 /* If a color is already assigned see if it will work */
12445 if (range->color != REG_UNSET) {
12446 struct live_range_def *lrd;
12447 if (!used[range->color]) {
12450 for(edge = range->edges; edge; edge = edge->next) {
12451 if (edge->node->color != range->color) {
12454 warning(state, edge->node->defs->def, "edge: ");
12455 lrd = edge->node->defs;
12457 warning(state, lrd->def, " %p %s",
12458 lrd->def, tops(lrd->def->op));
12460 } while(lrd != edge->node->defs);
12463 warning(state, range->defs->def, "def: ");
12465 warning(state, lrd->def, " %p %s",
12466 lrd->def, tops(lrd->def->op));
12468 } while(lrd != range->defs);
12469 internal_error(state, range->defs->def,
12470 "live range with already used color %s",
12471 arch_reg_str(range->color));
12474 /* If I feed into an expression reuse it's color.
12475 * This should help remove copies in the case of 2 register instructions
12476 * and phi functions.
12479 lrd = live_range_end(state, range, 0);
12480 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
12481 entry = lrd->def->use;
12482 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
12483 struct live_range_def *insd;
12484 insd = &rstate->lrd[entry->member->id];
12485 if (insd->lr->defs == 0) {
12488 if (!phi && (insd->def->op == OP_PHI) &&
12489 !interfere(rstate, range, insd->lr)) {
12492 if ((insd->lr->color == REG_UNSET) ||
12493 ((insd->lr->classes & range->classes) == 0) ||
12494 (used[insd->lr->color])) {
12497 if (interfere(rstate, range, insd->lr)) {
12500 range->color = insd->lr->color;
12503 /* If I feed into a phi function reuse it's color or the color
12504 * of something else that feeds into the phi function.
12507 if (phi->lr->color != REG_UNSET) {
12508 if (used[phi->lr->color]) {
12509 range->color = phi->lr->color;
12513 expr = triple_rhs(state, phi->def, 0);
12514 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
12515 struct live_range *lr;
12519 lr = rstate->lrd[(*expr)->id].lr;
12520 if ((lr->color == REG_UNSET) ||
12521 ((lr->classes & range->classes) == 0) ||
12522 (used[lr->color])) {
12525 if (interfere(rstate, range, lr)) {
12528 range->color = lr->color;
12532 /* If I don't interfere with a rhs node reuse it's color */
12533 lrd = live_range_head(state, range, 0);
12534 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
12535 expr = triple_rhs(state, lrd->def, 0);
12536 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
12537 struct live_range *lr;
12541 lr = rstate->lrd[(*expr)->id].lr;
12542 if ((lr->color == -1) ||
12543 ((lr->classes & range->classes) == 0) ||
12544 (used[lr->color])) {
12547 if (interfere(rstate, range, lr)) {
12550 range->color = lr->color;
12554 /* If I have not opportunitically picked a useful color
12555 * pick the first color that is free.
12557 if (range->color == REG_UNSET) {
12559 arch_select_free_register(state, used, range->classes);
12561 if (range->color == REG_UNSET) {
12563 if (split_ranges(state, rstate, used, range)) {
12566 for(edge = range->edges; edge; edge = edge->next) {
12567 if (edge->node->color == REG_UNSET) {
12570 warning(state, edge->node->defs->def, "reg %s",
12571 arch_reg_str(edge->node->color));
12573 warning(state, range->defs->def, "classes: %x",
12575 for(i = 0; i < MAX_REGISTERS; i++) {
12577 warning(state, range->defs->def, "used: %s",
12581 #if DEBUG_COLOR_GRAPH < 2
12582 error(state, range->defs->def, "too few registers");
12584 internal_error(state, range->defs->def, "too few registers");
12587 range->classes = arch_reg_regcm(state, range->color);
12588 if (range->color == -1) {
12589 internal_error(state, range->defs->def, "select_free_color did not?");
12594 static int color_graph(struct compile_state *state, struct reg_state *rstate)
12597 struct live_range_edge *edge;
12598 struct live_range *range;
12600 cgdebug_printf("Lo: ");
12601 range = rstate->low;
12602 if (*range->group_prev != range) {
12603 internal_error(state, 0, "lo: *prev != range?");
12605 *range->group_prev = range->group_next;
12606 if (range->group_next) {
12607 range->group_next->group_prev = range->group_prev;
12609 if (&range->group_next == rstate->low_tail) {
12610 rstate->low_tail = range->group_prev;
12612 if (rstate->low == range) {
12613 internal_error(state, 0, "low: next != prev?");
12616 else if (rstate->high) {
12617 cgdebug_printf("Hi: ");
12618 range = rstate->high;
12619 if (*range->group_prev != range) {
12620 internal_error(state, 0, "hi: *prev != range?");
12622 *range->group_prev = range->group_next;
12623 if (range->group_next) {
12624 range->group_next->group_prev = range->group_prev;
12626 if (&range->group_next == rstate->high_tail) {
12627 rstate->high_tail = range->group_prev;
12629 if (rstate->high == range) {
12630 internal_error(state, 0, "high: next != prev?");
12636 cgdebug_printf(" %d\n", range - rstate->lr);
12637 range->group_prev = 0;
12638 for(edge = range->edges; edge; edge = edge->next) {
12639 struct live_range *node;
12641 /* Move nodes from the high to the low list */
12642 if (node->group_prev && (node->color == REG_UNSET) &&
12643 (node->degree == regc_max_size(state, node->classes))) {
12644 if (*node->group_prev != node) {
12645 internal_error(state, 0, "move: *prev != node?");
12647 *node->group_prev = node->group_next;
12648 if (node->group_next) {
12649 node->group_next->group_prev = node->group_prev;
12651 if (&node->group_next == rstate->high_tail) {
12652 rstate->high_tail = node->group_prev;
12654 cgdebug_printf("Moving...%d to low\n", node - rstate->lr);
12655 node->group_prev = rstate->low_tail;
12656 node->group_next = 0;
12657 *rstate->low_tail = node;
12658 rstate->low_tail = &node->group_next;
12659 if (*node->group_prev != node) {
12660 internal_error(state, 0, "move2: *prev != node?");
12665 colored = color_graph(state, rstate);
12667 cgdebug_printf("Coloring %d @%s:%d.%d:",
12668 range - rstate->lr,
12669 range->def->filename, range->def->line, range->def->col);
12671 colored = select_free_color(state, rstate, range);
12672 cgdebug_printf(" %s\n", arch_reg_str(range->color));
12677 static void verify_colors(struct compile_state *state, struct reg_state *rstate)
12679 struct live_range *lr;
12680 struct live_range_edge *edge;
12681 struct triple *ins, *first;
12682 char used[MAX_REGISTERS];
12683 first = RHS(state->main_function, 0);
12686 if (triple_is_def(state, ins)) {
12687 if ((ins->id < 0) || (ins->id > rstate->defs)) {
12688 internal_error(state, ins,
12689 "triple without a live range def");
12691 lr = rstate->lrd[ins->id].lr;
12692 if (lr->color == REG_UNSET) {
12693 internal_error(state, ins,
12694 "triple without a color");
12696 /* Find the registers used by the edges */
12697 memset(used, 0, sizeof(used));
12698 for(edge = lr->edges; edge; edge = edge->next) {
12699 if (edge->node->color == REG_UNSET) {
12700 internal_error(state, 0,
12701 "live range without a color");
12703 reg_fill_used(state, used, edge->node->color);
12705 if (used[lr->color]) {
12706 internal_error(state, ins,
12707 "triple with already used color");
12711 } while(ins != first);
12714 static void color_triples(struct compile_state *state, struct reg_state *rstate)
12716 struct live_range *lr;
12717 struct triple *first, *ins;
12718 first = RHS(state->main_function, 0);
12721 if ((ins->id < 0) || (ins->id > rstate->defs)) {
12722 internal_error(state, ins,
12723 "triple without a live range");
12725 lr = rstate->lrd[ins->id].lr;
12726 SET_REG(ins->id, lr->color);
12728 } while (ins != first);
12731 static void print_interference_block(
12732 struct compile_state *state, struct block *block, void *arg)
12735 struct reg_state *rstate = arg;
12736 struct reg_block *rb;
12737 struct triple *ptr;
12740 rb = &rstate->blocks[block->vertex];
12742 printf("\nblock: %p (%d), %p<-%p %p<-%p\n",
12746 block->left && block->left->use?block->left->use->member : 0,
12748 block->right && block->right->use?block->right->use->member : 0);
12750 struct triple_reg_set *in_set;
12752 for(in_set = rb->in; in_set; in_set = in_set->next) {
12753 printf(" %-10p", in_set->member);
12758 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
12759 done = (ptr == block->last);
12760 if (ptr->op == OP_PHI) {
12767 for(edge = 0; edge < block->users; edge++) {
12768 printf(" in(%d):", edge);
12769 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
12770 struct triple **slot;
12771 done = (ptr == block->last);
12772 if (ptr->op != OP_PHI) {
12775 slot = &RHS(ptr, 0);
12776 printf(" %-10p", slot[edge]);
12781 if (block->first->op == OP_LABEL) {
12782 printf("%p:\n", block->first);
12784 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
12785 struct triple_set *user;
12786 struct live_range *lr;
12790 done = (ptr == block->last);
12791 lr = rstate->lrd[ptr->id].lr;
12793 if (triple_stores_block(state, ptr)) {
12794 if (ptr->u.block != block) {
12795 internal_error(state, ptr,
12796 "Wrong block pointer: %p",
12800 if (op == OP_ADECL) {
12801 for(user = ptr->use; user; user = user->next) {
12802 if (!user->member->u.block) {
12803 internal_error(state, user->member,
12804 "Use %p not in a block?",
12811 SET_REG(ptr->id, lr->color);
12812 display_triple(stdout, ptr);
12815 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
12816 internal_error(state, ptr, "lr has no defs!");
12820 struct live_range_def *lrd;
12824 printf(" %-10p", lrd->def);
12826 } while(lrd != lr->defs);
12829 if (lr->edges > 0) {
12830 struct live_range_edge *edge;
12832 for(edge = lr->edges; edge; edge = edge->next) {
12833 struct live_range_def *lrd;
12834 lrd = edge->node->defs;
12836 printf(" %-10p", lrd->def);
12838 } while(lrd != edge->node->defs);
12843 /* Do a bunch of sanity checks */
12844 valid_ins(state, ptr);
12845 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
12846 internal_error(state, ptr, "Invalid triple id: %d",
12849 for(user = ptr->use; user; user = user->next) {
12850 struct triple *use;
12851 struct live_range *ulr;
12852 use = user->member;
12853 valid_ins(state, use);
12854 if ((use->id < 0) || (use->id > rstate->defs)) {
12855 internal_error(state, use, "Invalid triple id: %d",
12858 ulr = rstate->lrd[user->member->id].lr;
12859 if (triple_stores_block(state, user->member) &&
12860 !user->member->u.block) {
12861 internal_error(state, user->member,
12862 "Use %p not in a block?",
12868 struct triple_reg_set *out_set;
12870 for(out_set = rb->out; out_set; out_set = out_set->next) {
12871 printf(" %-10p", out_set->member);
12878 static struct live_range *merge_sort_lr(
12879 struct live_range *first, struct live_range *last)
12881 struct live_range *mid, *join, **join_tail, *pick;
12883 size = (last - first) + 1;
12885 mid = first + size/2;
12886 first = merge_sort_lr(first, mid -1);
12887 mid = merge_sort_lr(mid, last);
12891 /* merge the two lists */
12892 while(first && mid) {
12893 if ((first->degree < mid->degree) ||
12894 ((first->degree == mid->degree) &&
12895 (first->length < mid->length))) {
12897 first = first->group_next;
12899 first->group_prev = 0;
12904 mid = mid->group_next;
12906 mid->group_prev = 0;
12909 pick->group_next = 0;
12910 pick->group_prev = join_tail;
12912 join_tail = &pick->group_next;
12914 /* Splice the remaining list */
12915 pick = (first)? first : mid;
12918 pick->group_prev = join_tail;
12922 if (!first->defs) {
12930 static void ids_from_rstate(struct compile_state *state,
12931 struct reg_state *rstate)
12933 struct triple *ins, *first;
12934 if (!rstate->defs) {
12937 /* Display the graph if desired */
12938 if (state->debug & DEBUG_INTERFERENCE) {
12939 print_blocks(state, stdout);
12940 print_control_flow(state);
12942 first = RHS(state->main_function, 0);
12946 struct live_range_def *lrd;
12947 lrd = &rstate->lrd[ins->id];
12948 ins->id = lrd->orig_id;
12951 } while(ins != first);
12954 static void cleanup_live_edges(struct reg_state *rstate)
12957 /* Free the edges on each node */
12958 for(i = 1; i <= rstate->ranges; i++) {
12959 remove_live_edges(rstate, &rstate->lr[i]);
12963 static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
12965 cleanup_live_edges(rstate);
12966 xfree(rstate->lrd);
12969 /* Free the variable lifetime information */
12970 if (rstate->blocks) {
12971 free_variable_lifetimes(state, rstate->blocks);
12974 rstate->ranges = 0;
12977 rstate->blocks = 0;
12980 static void allocate_registers(struct compile_state *state)
12982 struct reg_state rstate;
12985 /* Clear out the reg_state */
12986 memset(&rstate, 0, sizeof(rstate));
12987 rstate.max_passes = MAX_ALLOCATION_PASSES;
12990 struct live_range **point, **next;
12991 struct triple *tangle;
12992 struct coalesce_conflict conflict;
12996 ids_from_rstate(state, &rstate);
12999 /* Cleanup the temporary data structures */
13000 cleanup_rstate(state, &rstate);
13002 /* Compute the variable lifetimes */
13003 rstate.blocks = compute_variable_lifetimes(state);
13005 /* Find an invalid mandatory live range coalesce */
13007 conflict.index = -1;
13008 walk_variable_lifetimes(
13009 state, rstate.blocks, spot_coalesce_conflict, &conflict);
13011 /* If a tangle was found resolve it */
13012 if (conflict.ins) {
13013 resolve_coalesce_conflict(state, &conflict);
13015 } while(conflict.ins);
13018 /* Cleanup the temporary data structures */
13019 cleanup_rstate(state, &rstate);
13021 /* Compute the variable lifetimes */
13022 rstate.blocks = compute_variable_lifetimes(state);
13024 /* Find two simultaneous uses of the same register */
13026 walk_variable_lifetimes(
13027 state, rstate.blocks, spot_tangle, &tangle);
13029 /* If a tangle was found resolve it */
13031 resolve_tangle(state, tangle);
13035 if (state->debug & DEBUG_INSERTED_COPIES) {
13036 printf("After resolve_tangles\n");
13037 print_blocks(state, stdout);
13038 print_control_flow(state);
13042 /* Allocate and initialize the live ranges */
13043 initialize_live_ranges(state, &rstate);
13046 /* Forget previous live range edge calculations */
13047 cleanup_live_edges(&rstate);
13049 /* Compute the interference graph */
13050 walk_variable_lifetimes(
13051 state, rstate.blocks, graph_ins, &rstate);
13053 /* Display the interference graph if desired */
13054 if (state->debug & DEBUG_INTERFERENCE) {
13055 printf("\nlive variables by block\n");
13056 walk_blocks(state, print_interference_block, &rstate);
13057 printf("\nlive variables by instruction\n");
13058 walk_variable_lifetimes(
13059 state, rstate.blocks,
13060 print_interference_ins, &rstate);
13063 coalesced = coalesce_live_ranges(state, &rstate);
13064 } while(coalesced);
13066 /* Build the groups low and high. But with the nodes
13067 * first sorted by degree order.
13069 rstate.low_tail = &rstate.low;
13070 rstate.high_tail = &rstate.high;
13071 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
13073 rstate.high->group_prev = &rstate.high;
13075 for(point = &rstate.high; *point; point = &(*point)->group_next)
13077 rstate.high_tail = point;
13078 /* Walk through the high list and move everything that needs
13081 for(point = &rstate.high; *point; point = next) {
13082 struct live_range *range;
13083 next = &(*point)->group_next;
13086 /* If it has a low degree or it already has a color
13087 * place the node in low.
13089 if ((range->degree < regc_max_size(state, range->classes)) ||
13090 (range->color != REG_UNSET)) {
13091 cgdebug_printf("Lo: %5d degree %5d%s\n",
13092 range - rstate.lr, range->degree,
13093 (range->color != REG_UNSET) ? " (colored)": "");
13094 *range->group_prev = range->group_next;
13095 if (range->group_next) {
13096 range->group_next->group_prev = range->group_prev;
13098 if (&range->group_next == rstate.high_tail) {
13099 rstate.high_tail = range->group_prev;
13101 range->group_prev = rstate.low_tail;
13102 range->group_next = 0;
13103 *rstate.low_tail = range;
13104 rstate.low_tail = &range->group_next;
13108 cgdebug_printf("hi: %5d degree %5d%s\n",
13109 range - rstate.lr, range->degree,
13110 (range->color != REG_UNSET) ? " (colored)": "");
13113 /* Color the live_ranges */
13114 colored = color_graph(state, &rstate);
13116 } while (!colored);
13118 /* Verify the graph was properly colored */
13119 verify_colors(state, &rstate);
13121 /* Move the colors from the graph to the triples */
13122 color_triples(state, &rstate);
13124 /* Cleanup the temporary data structures */
13125 cleanup_rstate(state, &rstate);
13128 /* Sparce Conditional Constant Propogation
13129 * =========================================
13133 struct lattice_node {
13135 struct triple *def;
13136 struct ssa_edge *out;
13137 struct flow_block *fblock;
13138 struct triple *val;
13139 /* lattice high val && !is_const(val)
13140 * lattice const is_const(val)
13141 * lattice low val == 0
13145 struct lattice_node *src;
13146 struct lattice_node *dst;
13147 struct ssa_edge *work_next;
13148 struct ssa_edge *work_prev;
13149 struct ssa_edge *out_next;
13152 struct flow_block *src;
13153 struct flow_block *dst;
13154 struct flow_edge *work_next;
13155 struct flow_edge *work_prev;
13156 struct flow_edge *in_next;
13157 struct flow_edge *out_next;
13160 struct flow_block {
13161 struct block *block;
13162 struct flow_edge *in;
13163 struct flow_edge *out;
13164 struct flow_edge left, right;
13169 struct lattice_node *lattice;
13170 struct ssa_edge *ssa_edges;
13171 struct flow_block *flow_blocks;
13172 struct flow_edge *flow_work_list;
13173 struct ssa_edge *ssa_work_list;
13177 static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
13178 struct flow_edge *fedge)
13180 if (!scc->flow_work_list) {
13181 scc->flow_work_list = fedge;
13182 fedge->work_next = fedge->work_prev = fedge;
13185 struct flow_edge *ftail;
13186 ftail = scc->flow_work_list->work_prev;
13187 fedge->work_next = ftail->work_next;
13188 fedge->work_prev = ftail;
13189 fedge->work_next->work_prev = fedge;
13190 fedge->work_prev->work_next = fedge;
13194 static struct flow_edge *scc_next_fedge(
13195 struct compile_state *state, struct scc_state *scc)
13197 struct flow_edge *fedge;
13198 fedge = scc->flow_work_list;
13200 fedge->work_next->work_prev = fedge->work_prev;
13201 fedge->work_prev->work_next = fedge->work_next;
13202 if (fedge->work_next != fedge) {
13203 scc->flow_work_list = fedge->work_next;
13205 scc->flow_work_list = 0;
13211 static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
13212 struct ssa_edge *sedge)
13214 if (!scc->ssa_work_list) {
13215 scc->ssa_work_list = sedge;
13216 sedge->work_next = sedge->work_prev = sedge;
13219 struct ssa_edge *stail;
13220 stail = scc->ssa_work_list->work_prev;
13221 sedge->work_next = stail->work_next;
13222 sedge->work_prev = stail;
13223 sedge->work_next->work_prev = sedge;
13224 sedge->work_prev->work_next = sedge;
13228 static struct ssa_edge *scc_next_sedge(
13229 struct compile_state *state, struct scc_state *scc)
13231 struct ssa_edge *sedge;
13232 sedge = scc->ssa_work_list;
13234 sedge->work_next->work_prev = sedge->work_prev;
13235 sedge->work_prev->work_next = sedge->work_next;
13236 if (sedge->work_next != sedge) {
13237 scc->ssa_work_list = sedge->work_next;
13239 scc->ssa_work_list = 0;
13245 static void initialize_scc_state(
13246 struct compile_state *state, struct scc_state *scc)
13248 int ins_count, ssa_edge_count;
13249 int ins_index, ssa_edge_index, fblock_index;
13250 struct triple *first, *ins;
13251 struct block *block;
13252 struct flow_block *fblock;
13254 memset(scc, 0, sizeof(*scc));
13256 /* Inialize pass zero find out how much memory we need */
13257 first = RHS(state->main_function, 0);
13259 ins_count = ssa_edge_count = 0;
13261 struct triple_set *edge;
13263 for(edge = ins->use; edge; edge = edge->next) {
13267 } while(ins != first);
13269 fprintf(stderr, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
13270 ins_count, ssa_edge_count, state->last_vertex);
13272 scc->ins_count = ins_count;
13274 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
13276 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
13278 xcmalloc(sizeof(*scc->flow_blocks)*(state->last_vertex + 1),
13281 /* Initialize pass one collect up the nodes */
13284 ins_index = ssa_edge_index = fblock_index = 0;
13287 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
13288 block = ins->u.block;
13290 internal_error(state, ins, "label without block");
13293 block->vertex = fblock_index;
13294 fblock = &scc->flow_blocks[fblock_index];
13295 fblock->block = block;
13298 struct lattice_node *lnode;
13300 lnode = &scc->lattice[ins_index];
13303 lnode->fblock = fblock;
13304 lnode->val = ins; /* LATTICE HIGH */
13305 lnode->old_id = ins->id;
13306 ins->id = ins_index;
13309 } while(ins != first);
13310 /* Initialize pass two collect up the edges */
13315 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
13316 struct flow_edge *fedge, **ftail;
13317 struct block_set *bedge;
13318 block = ins->u.block;
13319 fblock = &scc->flow_blocks[block->vertex];
13322 ftail = &fblock->out;
13324 fblock->left.dst = &scc->flow_blocks[block->left->vertex];
13325 if (fblock->left.dst->block != block->left) {
13326 internal_error(state, 0, "block mismatch");
13328 fblock->left.out_next = 0;
13329 *ftail = &fblock->left;
13330 ftail = &fblock->left.out_next;
13332 if (block->right) {
13333 fblock->right.dst = &scc->flow_blocks[block->right->vertex];
13334 if (fblock->right.dst->block != block->right) {
13335 internal_error(state, 0, "block mismatch");
13337 fblock->right.out_next = 0;
13338 *ftail = &fblock->right;
13339 ftail = &fblock->right.out_next;
13341 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
13342 fedge->src = fblock;
13343 fedge->work_next = fedge->work_prev = fedge;
13344 fedge->executable = 0;
13346 ftail = &fblock->in;
13347 for(bedge = block->use; bedge; bedge = bedge->next) {
13348 struct block *src_block;
13349 struct flow_block *sfblock;
13350 struct flow_edge *sfedge;
13351 src_block = bedge->member;
13352 sfblock = &scc->flow_blocks[src_block->vertex];
13354 if (src_block->left == block) {
13355 sfedge = &sfblock->left;
13357 sfedge = &sfblock->right;
13360 ftail = &sfedge->in_next;
13361 sfedge->in_next = 0;
13365 struct triple_set *edge;
13366 struct ssa_edge **stail;
13367 struct lattice_node *lnode;
13368 lnode = &scc->lattice[ins->id];
13370 stail = &lnode->out;
13371 for(edge = ins->use; edge; edge = edge->next) {
13372 struct ssa_edge *sedge;
13373 ssa_edge_index += 1;
13374 sedge = &scc->ssa_edges[ssa_edge_index];
13376 stail = &sedge->out_next;
13377 sedge->src = lnode;
13378 sedge->dst = &scc->lattice[edge->member->id];
13379 sedge->work_next = sedge->work_prev = sedge;
13380 sedge->out_next = 0;
13384 } while(ins != first);
13385 /* Setup a dummy block 0 as a node above the start node */
13387 struct flow_block *fblock, *dst;
13388 struct flow_edge *fedge;
13389 fblock = &scc->flow_blocks[0];
13392 fblock->out = &fblock->left;
13393 dst = &scc->flow_blocks[state->first_block->vertex];
13394 fedge = &fblock->left;
13395 fedge->src = fblock;
13397 fedge->work_next = fedge;
13398 fedge->work_prev = fedge;
13399 fedge->in_next = fedge->dst->in;
13400 fedge->out_next = 0;
13401 fedge->executable = 0;
13402 fedge->dst->in = fedge;
13404 /* Initialize the work lists */
13405 scc->flow_work_list = 0;
13406 scc->ssa_work_list = 0;
13407 scc_add_fedge(state, scc, fedge);
13410 fprintf(stderr, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
13411 ins_index, ssa_edge_index, fblock_index);
13416 static void free_scc_state(
13417 struct compile_state *state, struct scc_state *scc)
13419 xfree(scc->flow_blocks);
13420 xfree(scc->ssa_edges);
13421 xfree(scc->lattice);
13425 static struct lattice_node *triple_to_lattice(
13426 struct compile_state *state, struct scc_state *scc, struct triple *ins)
13428 if (ins->id <= 0) {
13429 internal_error(state, ins, "bad id");
13431 return &scc->lattice[ins->id];
13434 static struct triple *preserve_lval(
13435 struct compile_state *state, struct lattice_node *lnode)
13437 struct triple *old;
13438 /* Preserve the original value */
13440 old = dup_triple(state, lnode->val);
13441 if (lnode->val != lnode->def) {
13451 static int lval_changed(struct compile_state *state,
13452 struct triple *old, struct lattice_node *lnode)
13455 /* See if the lattice value has changed */
13457 if (!old && !lnode->val) {
13460 if (changed && lnode->val && !is_const(lnode->val)) {
13464 lnode->val && old &&
13465 (memcmp(lnode->val->param, old->param,
13466 TRIPLE_SIZE(lnode->val->sizes) * sizeof(lnode->val->param[0])) == 0) &&
13467 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
13477 static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
13478 struct lattice_node *lnode)
13480 struct lattice_node *tmp;
13481 struct triple **slot, *old;
13482 struct flow_edge *fedge;
13484 if (lnode->def->op != OP_PHI) {
13485 internal_error(state, lnode->def, "not phi");
13487 /* Store the original value */
13488 old = preserve_lval(state, lnode);
13490 /* default to lattice high */
13491 lnode->val = lnode->def;
13492 slot = &RHS(lnode->def, 0);
13494 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
13495 if (!fedge->executable) {
13498 if (!slot[index]) {
13499 internal_error(state, lnode->def, "no phi value");
13501 tmp = triple_to_lattice(state, scc, slot[index]);
13502 /* meet(X, lattice low) = lattice low */
13506 /* meet(X, lattice high) = X */
13507 else if (!tmp->val) {
13508 lnode->val = lnode->val;
13510 /* meet(lattice high, X) = X */
13511 else if (!is_const(lnode->val)) {
13512 lnode->val = dup_triple(state, tmp->val);
13513 lnode->val->type = lnode->def->type;
13515 /* meet(const, const) = const or lattice low */
13516 else if (!constants_equal(state, lnode->val, tmp->val)) {
13524 fprintf(stderr, "phi: %d -> %s\n",
13526 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
13528 /* If the lattice value has changed update the work lists. */
13529 if (lval_changed(state, old, lnode)) {
13530 struct ssa_edge *sedge;
13531 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
13532 scc_add_sedge(state, scc, sedge);
13537 static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
13538 struct lattice_node *lnode)
13541 struct triple *old, *scratch;
13542 struct triple **dexpr, **vexpr;
13545 /* Store the original value */
13546 old = preserve_lval(state, lnode);
13548 /* Reinitialize the value */
13549 lnode->val = scratch = dup_triple(state, lnode->def);
13550 scratch->id = lnode->old_id;
13551 scratch->next = scratch;
13552 scratch->prev = scratch;
13555 count = TRIPLE_SIZE(scratch->sizes);
13556 for(i = 0; i < count; i++) {
13557 dexpr = &lnode->def->param[i];
13558 vexpr = &scratch->param[i];
13560 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
13561 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
13563 struct lattice_node *tmp;
13564 tmp = triple_to_lattice(state, scc, *dexpr);
13565 *vexpr = (tmp->val)? tmp->val : tmp->def;
13568 if (scratch->op == OP_BRANCH) {
13569 scratch->next = lnode->def->next;
13571 /* Recompute the value */
13572 #warning "FIXME see if simplify does anything bad"
13573 /* So far it looks like only the strength reduction
13574 * optimization are things I need to worry about.
13576 simplify(state, scratch);
13577 /* Cleanup my value */
13578 if (scratch->use) {
13579 internal_error(state, lnode->def, "scratch used?");
13581 if ((scratch->prev != scratch) ||
13582 ((scratch->next != scratch) &&
13583 ((lnode->def->op != OP_BRANCH) ||
13584 (scratch->next != lnode->def->next)))) {
13585 internal_error(state, lnode->def, "scratch in list?");
13587 /* undo any uses... */
13588 count = TRIPLE_SIZE(scratch->sizes);
13589 for(i = 0; i < count; i++) {
13590 vexpr = &scratch->param[i];
13592 unuse_triple(*vexpr, scratch);
13595 if (!is_const(scratch)) {
13596 for(i = 0; i < count; i++) {
13597 dexpr = &lnode->def->param[i];
13598 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
13599 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
13601 struct lattice_node *tmp;
13602 tmp = triple_to_lattice(state, scc, *dexpr);
13610 (lnode->val->op == lnode->def->op) &&
13611 (memcmp(lnode->val->param, lnode->def->param,
13612 count * sizeof(lnode->val->param[0])) == 0) &&
13613 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
13614 lnode->val = lnode->def;
13616 /* Find the cases that are always lattice lo */
13618 triple_is_def(state, lnode->val) &&
13619 !triple_is_pure(state, lnode->val)) {
13623 (lnode->val->op == OP_SDECL) &&
13624 (lnode->val != lnode->def)) {
13625 internal_error(state, lnode->def, "bad sdecl");
13627 /* See if the lattice value has changed */
13628 changed = lval_changed(state, old, lnode);
13629 if (lnode->val != scratch) {
13635 static void scc_visit_branch(struct compile_state *state, struct scc_state *scc,
13636 struct lattice_node *lnode)
13638 struct lattice_node *cond;
13641 struct flow_edge *fedge;
13642 fprintf(stderr, "branch: %d (",
13645 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
13646 fprintf(stderr, " %d", fedge->dst->block->vertex);
13648 fprintf(stderr, " )");
13649 if (TRIPLE_RHS(lnode->def->sizes) > 0) {
13650 fprintf(stderr, " <- %d",
13651 RHS(lnode->def, 0)->id);
13653 fprintf(stderr, "\n");
13656 if (lnode->def->op != OP_BRANCH) {
13657 internal_error(state, lnode->def, "not branch");
13659 /* This only applies to conditional branches */
13660 if (TRIPLE_RHS(lnode->def->sizes) == 0) {
13663 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
13664 if (cond->val && !is_const(cond->val)) {
13665 #warning "FIXME do I need to do something here?"
13666 warning(state, cond->def, "condition not constant?");
13669 if (cond->val == 0) {
13670 scc_add_fedge(state, scc, cond->fblock->out);
13671 scc_add_fedge(state, scc, cond->fblock->out->out_next);
13673 else if (cond->val->u.cval) {
13674 scc_add_fedge(state, scc, cond->fblock->out->out_next);
13677 scc_add_fedge(state, scc, cond->fblock->out);
13682 static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
13683 struct lattice_node *lnode)
13687 changed = compute_lnode_val(state, scc, lnode);
13690 struct triple **expr;
13691 fprintf(stderr, "expr: %3d %10s (",
13692 lnode->def->id, tops(lnode->def->op));
13693 expr = triple_rhs(state, lnode->def, 0);
13694 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
13696 fprintf(stderr, " %d", (*expr)->id);
13699 fprintf(stderr, " ) -> %s\n",
13700 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
13703 if (lnode->def->op == OP_BRANCH) {
13704 scc_visit_branch(state, scc, lnode);
13707 else if (changed) {
13708 struct ssa_edge *sedge;
13709 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
13710 scc_add_sedge(state, scc, sedge);
13715 static void scc_writeback_values(
13716 struct compile_state *state, struct scc_state *scc)
13718 struct triple *first, *ins;
13719 first = RHS(state->main_function, 0);
13722 struct lattice_node *lnode;
13723 lnode = triple_to_lattice(state, scc, ins);
13725 ins->id = lnode->old_id;
13727 if (lnode->val && !is_const(lnode->val)) {
13728 warning(state, lnode->def,
13729 "lattice node still high?");
13732 if (lnode->val && (lnode->val != ins)) {
13733 /* See if it something I know how to write back */
13734 switch(lnode->val->op) {
13736 mkconst(state, ins, lnode->val->u.cval);
13739 mkaddr_const(state, ins,
13740 MISC(lnode->val, 0), lnode->val->u.cval);
13743 /* By default don't copy the changes,
13744 * recompute them in place instead.
13746 simplify(state, ins);
13749 if (is_const(lnode->val) &&
13750 !constants_equal(state, lnode->val, ins)) {
13751 internal_error(state, 0, "constants not equal");
13753 /* Free the lattice nodes */
13758 } while(ins != first);
13761 static void scc_transform(struct compile_state *state)
13763 struct scc_state scc;
13765 initialize_scc_state(state, &scc);
13767 while(scc.flow_work_list || scc.ssa_work_list) {
13768 struct flow_edge *fedge;
13769 struct ssa_edge *sedge;
13770 struct flow_edge *fptr;
13771 while((fedge = scc_next_fedge(state, &scc))) {
13772 struct block *block;
13773 struct triple *ptr;
13774 struct flow_block *fblock;
13777 if (fedge->executable) {
13781 internal_error(state, 0, "fedge without dst");
13784 internal_error(state, 0, "fedge without src");
13786 fedge->executable = 1;
13787 fblock = fedge->dst;
13788 block = fblock->block;
13790 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
13791 if (fptr->executable) {
13796 fprintf(stderr, "vertex: %d time: %d\n",
13797 block->vertex, time);
13801 for(ptr = block->first; !done; ptr = ptr->next) {
13802 struct lattice_node *lnode;
13803 done = (ptr == block->last);
13804 lnode = &scc.lattice[ptr->id];
13805 if (ptr->op == OP_PHI) {
13806 scc_visit_phi(state, &scc, lnode);
13808 else if (time == 1) {
13809 scc_visit_expr(state, &scc, lnode);
13812 if (fblock->out && !fblock->out->out_next) {
13813 scc_add_fedge(state, &scc, fblock->out);
13816 while((sedge = scc_next_sedge(state, &scc))) {
13817 struct lattice_node *lnode;
13818 struct flow_block *fblock;
13819 lnode = sedge->dst;
13820 fblock = lnode->fblock;
13822 fprintf(stderr, "sedge: %5d (%5d -> %5d)\n",
13823 sedge - scc.ssa_edges,
13824 sedge->src->def->id,
13825 sedge->dst->def->id);
13827 if (lnode->def->op == OP_PHI) {
13828 scc_visit_phi(state, &scc, lnode);
13831 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
13832 if (fptr->executable) {
13837 scc_visit_expr(state, &scc, lnode);
13843 scc_writeback_values(state, &scc);
13844 free_scc_state(state, &scc);
13848 static void transform_to_arch_instructions(struct compile_state *state)
13850 struct triple *ins, *first;
13851 first = RHS(state->main_function, 0);
13854 ins = transform_to_arch_instruction(state, ins);
13855 } while(ins != first);
13858 #if DEBUG_CONSISTENCY
13859 static void verify_uses(struct compile_state *state)
13861 struct triple *first, *ins;
13862 struct triple_set *set;
13863 first = RHS(state->main_function, 0);
13866 struct triple **expr;
13867 expr = triple_rhs(state, ins, 0);
13868 for(; expr; expr = triple_rhs(state, ins, expr)) {
13869 for(set = *expr?(*expr)->use:0; set; set = set->next) {
13870 if (set->member == ins) {
13875 internal_error(state, ins, "rhs not used");
13878 expr = triple_lhs(state, ins, 0);
13879 for(; expr; expr = triple_lhs(state, ins, expr)) {
13880 for(set = *expr?(*expr)->use:0; set; set = set->next) {
13881 if (set->member == ins) {
13886 internal_error(state, ins, "lhs not used");
13890 } while(ins != first);
13893 static void verify_blocks(struct compile_state *state)
13895 struct triple *ins;
13896 struct block *block;
13897 block = state->first_block;
13902 for(ins = block->first; ins != block->last->next; ins = ins->next) {
13903 if (!triple_stores_block(state, ins)) {
13906 if (ins->u.block != block) {
13907 internal_error(state, ins, "inconsitent block specified");
13910 if (!triple_stores_block(state, block->last->next)) {
13911 internal_error(state, block->last->next,
13912 "cannot find next block");
13914 block = block->last->next->u.block;
13916 internal_error(state, block->last->next,
13919 } while(block != state->first_block);
13922 static void verify_domination(struct compile_state *state)
13924 struct triple *first, *ins;
13925 struct triple_set *set;
13926 if (!state->first_block) {
13930 first = RHS(state->main_function, 0);
13933 for(set = ins->use; set; set = set->next) {
13934 struct triple **expr;
13935 if (set->member->op == OP_PHI) {
13938 /* See if the use is on the righ hand side */
13939 expr = triple_rhs(state, set->member, 0);
13940 for(; expr ; expr = triple_rhs(state, set->member, expr)) {
13941 if (*expr == ins) {
13946 !tdominates(state, ins, set->member)) {
13947 internal_error(state, set->member,
13948 "non dominated rhs use?");
13952 } while(ins != first);
13955 static void verify_piece(struct compile_state *state)
13957 struct triple *first, *ins;
13958 first = RHS(state->main_function, 0);
13961 struct triple *ptr;
13963 lhs = TRIPLE_LHS(ins->sizes);
13964 if ((ins->op == OP_WRITE) || (ins->op == OP_STORE)) {
13967 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
13968 if (ptr != LHS(ins, i)) {
13969 internal_error(state, ins, "malformed lhs on %s",
13972 if (ptr->op != OP_PIECE) {
13973 internal_error(state, ins, "bad lhs op %s at %d on %s",
13974 tops(ptr->op), i, tops(ins->op));
13976 if (ptr->u.cval != i) {
13977 internal_error(state, ins, "bad u.cval of %d %d expected",
13982 } while(ins != first);
13984 static void verify_ins_colors(struct compile_state *state)
13986 struct triple *first, *ins;
13988 first = RHS(state->main_function, 0);
13992 } while(ins != first);
13994 static void verify_consistency(struct compile_state *state)
13996 verify_uses(state);
13997 verify_blocks(state);
13998 verify_domination(state);
13999 verify_piece(state);
14000 verify_ins_colors(state);
14003 #define verify_consistency(state) do {} while(0)
14004 #endif /* DEBUG_USES */
14006 static void optimize(struct compile_state *state)
14008 if (state->debug & DEBUG_TRIPLES) {
14009 print_triples(state);
14011 /* Replace structures with simpler data types */
14012 flatten_structures(state);
14013 if (state->debug & DEBUG_TRIPLES) {
14014 print_triples(state);
14016 verify_consistency(state);
14017 /* Analize the intermediate code */
14018 setup_basic_blocks(state);
14019 analyze_idominators(state);
14020 analyze_ipdominators(state);
14021 /* Transform the code to ssa form */
14022 transform_to_ssa_form(state);
14023 verify_consistency(state);
14024 /* Do strength reduction and simple constant optimizations */
14025 if (state->optimize >= 1) {
14026 simplify_all(state);
14028 verify_consistency(state);
14029 /* Propogate constants throughout the code */
14030 if (state->optimize >= 2) {
14031 #warning "FIXME fix scc_transform"
14032 scc_transform(state);
14033 transform_from_ssa_form(state);
14034 free_basic_blocks(state);
14035 setup_basic_blocks(state);
14036 analyze_idominators(state);
14037 analyze_ipdominators(state);
14038 transform_to_ssa_form(state);
14040 verify_consistency(state);
14041 #warning "WISHLIST implement single use constants (least possible register pressure)"
14042 #warning "WISHLIST implement induction variable elimination"
14043 /* Select architecture instructions and an initial partial
14044 * coloring based on architecture constraints.
14046 transform_to_arch_instructions(state);
14047 verify_consistency(state);
14048 if (state->debug & DEBUG_ARCH_CODE) {
14049 printf("After transform_to_arch_instructions\n");
14050 print_blocks(state, stdout);
14051 print_control_flow(state);
14053 eliminate_inefectual_code(state);
14054 verify_consistency(state);
14055 if (state->debug & DEBUG_CODE_ELIMINATION) {
14056 printf("After eliminate_inefectual_code\n");
14057 print_blocks(state, stdout);
14058 print_control_flow(state);
14060 verify_consistency(state);
14061 /* Color all of the variables to see if they will fit in registers */
14062 insert_copies_to_phi(state);
14063 if (state->debug & DEBUG_INSERTED_COPIES) {
14064 printf("After insert_copies_to_phi\n");
14065 print_blocks(state, stdout);
14066 print_control_flow(state);
14068 verify_consistency(state);
14069 insert_mandatory_copies(state);
14070 if (state->debug & DEBUG_INSERTED_COPIES) {
14071 printf("After insert_mandatory_copies\n");
14072 print_blocks(state, stdout);
14073 print_control_flow(state);
14075 verify_consistency(state);
14076 allocate_registers(state);
14077 verify_consistency(state);
14078 if (state->debug & DEBUG_INTERMEDIATE_CODE) {
14079 print_blocks(state, stdout);
14081 if (state->debug & DEBUG_CONTROL_FLOW) {
14082 print_control_flow(state);
14084 /* Remove the optimization information.
14085 * This is more to check for memory consistency than to free memory.
14087 free_basic_blocks(state);
14090 static void print_op_asm(struct compile_state *state,
14091 struct triple *ins, FILE *fp)
14093 struct asm_info *info;
14095 unsigned lhs, rhs, i;
14096 info = ins->u.ainfo;
14097 lhs = TRIPLE_LHS(ins->sizes);
14098 rhs = TRIPLE_RHS(ins->sizes);
14099 /* Don't count the clobbers in lhs */
14100 for(i = 0; i < lhs; i++) {
14101 if (LHS(ins, i)->type == &void_type) {
14107 for(ptr = info->str; *ptr; ptr++) {
14109 unsigned long param;
14110 struct triple *piece;
14120 param = strtoul(ptr, &next, 10);
14122 error(state, ins, "Invalid asm template");
14124 if (param >= (lhs + rhs)) {
14125 error(state, ins, "Invalid param %%%u in asm template",
14128 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
14130 arch_reg_str(ID_REG(piece->id)));
14137 /* Only use the low x86 byte registers. This allows me
14138 * allocate the entire register when a byte register is used.
14140 #define X86_4_8BIT_GPRS 1
14142 /* Recognized x86 cpu variants */
14150 #define CPU_DEFAULT CPU_I386
14152 /* The x86 register classes */
14153 #define REGC_FLAGS 0
14154 #define REGC_GPR8 1
14155 #define REGC_GPR16 2
14156 #define REGC_GPR32 3
14157 #define REGC_GPR64 4
14160 #define REGC_GPR32_8 7
14161 #define REGC_GPR16_8 8
14162 #define REGC_IMM32 9
14163 #define REGC_IMM16 10
14164 #define REGC_IMM8 11
14165 #define LAST_REGC REGC_IMM8
14166 #if LAST_REGC >= MAX_REGC
14167 #error "MAX_REGC is to low"
14170 /* Register class masks */
14171 #define REGCM_FLAGS (1 << REGC_FLAGS)
14172 #define REGCM_GPR8 (1 << REGC_GPR8)
14173 #define REGCM_GPR16 (1 << REGC_GPR16)
14174 #define REGCM_GPR32 (1 << REGC_GPR32)
14175 #define REGCM_GPR64 (1 << REGC_GPR64)
14176 #define REGCM_MMX (1 << REGC_MMX)
14177 #define REGCM_XMM (1 << REGC_XMM)
14178 #define REGCM_GPR32_8 (1 << REGC_GPR32_8)
14179 #define REGCM_GPR16_8 (1 << REGC_GPR16_8)
14180 #define REGCM_IMM32 (1 << REGC_IMM32)
14181 #define REGCM_IMM16 (1 << REGC_IMM16)
14182 #define REGCM_IMM8 (1 << REGC_IMM8)
14183 #define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
14185 /* The x86 registers */
14186 #define REG_EFLAGS 2
14187 #define REGC_FLAGS_FIRST REG_EFLAGS
14188 #define REGC_FLAGS_LAST REG_EFLAGS
14197 #define REGC_GPR8_FIRST REG_AL
14198 #if X86_4_8BIT_GPRS
14199 #define REGC_GPR8_LAST REG_DL
14201 #define REGC_GPR8_LAST REG_DH
14211 #define REGC_GPR16_FIRST REG_AX
14212 #define REGC_GPR16_LAST REG_SP
14221 #define REGC_GPR32_FIRST REG_EAX
14222 #define REGC_GPR32_LAST REG_ESP
14223 #define REG_EDXEAX 27
14224 #define REGC_GPR64_FIRST REG_EDXEAX
14225 #define REGC_GPR64_LAST REG_EDXEAX
14226 #define REG_MMX0 28
14227 #define REG_MMX1 29
14228 #define REG_MMX2 30
14229 #define REG_MMX3 31
14230 #define REG_MMX4 32
14231 #define REG_MMX5 33
14232 #define REG_MMX6 34
14233 #define REG_MMX7 35
14234 #define REGC_MMX_FIRST REG_MMX0
14235 #define REGC_MMX_LAST REG_MMX7
14236 #define REG_XMM0 36
14237 #define REG_XMM1 37
14238 #define REG_XMM2 38
14239 #define REG_XMM3 39
14240 #define REG_XMM4 40
14241 #define REG_XMM5 41
14242 #define REG_XMM6 42
14243 #define REG_XMM7 43
14244 #define REGC_XMM_FIRST REG_XMM0
14245 #define REGC_XMM_LAST REG_XMM7
14246 #warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
14247 #define LAST_REG REG_XMM7
14249 #define REGC_GPR32_8_FIRST REG_EAX
14250 #define REGC_GPR32_8_LAST REG_EDX
14251 #define REGC_GPR16_8_FIRST REG_AX
14252 #define REGC_GPR16_8_LAST REG_DX
14254 #define REGC_IMM8_FIRST -1
14255 #define REGC_IMM8_LAST -1
14256 #define REGC_IMM16_FIRST -2
14257 #define REGC_IMM16_LAST -1
14258 #define REGC_IMM32_FIRST -4
14259 #define REGC_IMM32_LAST -1
14261 #if LAST_REG >= MAX_REGISTERS
14262 #error "MAX_REGISTERS to low"
14266 static unsigned regc_size[LAST_REGC +1] = {
14267 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
14268 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
14269 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
14270 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
14271 [REGC_GPR64] = REGC_GPR64_LAST - REGC_GPR64_FIRST + 1,
14272 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
14273 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
14274 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
14275 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
14281 static const struct {
14283 } regcm_bound[LAST_REGC + 1] = {
14284 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
14285 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
14286 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
14287 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
14288 [REGC_GPR64] = { REGC_GPR64_FIRST, REGC_GPR64_LAST },
14289 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
14290 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
14291 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
14292 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
14293 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
14294 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
14295 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
14298 static int arch_encode_cpu(const char *cpu)
14304 { "i386", CPU_I386 },
14312 for(ptr = cpus; ptr->name; ptr++) {
14313 if (strcmp(ptr->name, cpu) == 0) {
14320 static unsigned arch_regc_size(struct compile_state *state, int class)
14322 if ((class < 0) || (class > LAST_REGC)) {
14325 return regc_size[class];
14327 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
14329 /* See if two register classes may have overlapping registers */
14330 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 |
14331 REGCM_GPR32_8 | REGCM_GPR32 | REGCM_GPR64;
14333 /* Special case for the immediates */
14334 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
14335 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
14336 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
14337 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
14340 return (regcm1 & regcm2) ||
14341 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
14344 static void arch_reg_equivs(
14345 struct compile_state *state, unsigned *equiv, int reg)
14347 if ((reg < 0) || (reg > LAST_REG)) {
14348 internal_error(state, 0, "invalid register");
14353 #if X86_4_8BIT_GPRS
14357 *equiv++ = REG_EAX;
14358 *equiv++ = REG_EDXEAX;
14361 #if X86_4_8BIT_GPRS
14365 *equiv++ = REG_EAX;
14366 *equiv++ = REG_EDXEAX;
14369 #if X86_4_8BIT_GPRS
14373 *equiv++ = REG_EBX;
14377 #if X86_4_8BIT_GPRS
14381 *equiv++ = REG_EBX;
14384 #if X86_4_8BIT_GPRS
14388 *equiv++ = REG_ECX;
14392 #if X86_4_8BIT_GPRS
14396 *equiv++ = REG_ECX;
14399 #if X86_4_8BIT_GPRS
14403 *equiv++ = REG_EDX;
14404 *equiv++ = REG_EDXEAX;
14407 #if X86_4_8BIT_GPRS
14411 *equiv++ = REG_EDX;
14412 *equiv++ = REG_EDXEAX;
14417 *equiv++ = REG_EAX;
14418 *equiv++ = REG_EDXEAX;
14423 *equiv++ = REG_EBX;
14428 *equiv++ = REG_ECX;
14433 *equiv++ = REG_EDX;
14434 *equiv++ = REG_EDXEAX;
14437 *equiv++ = REG_ESI;
14440 *equiv++ = REG_EDI;
14443 *equiv++ = REG_EBP;
14446 *equiv++ = REG_ESP;
14452 *equiv++ = REG_EDXEAX;
14468 *equiv++ = REG_EDXEAX;
14489 *equiv++ = REG_EAX;
14490 *equiv++ = REG_EDX;
14493 *equiv++ = REG_UNSET;
14496 static unsigned arch_avail_mask(struct compile_state *state)
14498 unsigned avail_mask;
14499 avail_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 |
14500 REGCM_GPR32 | REGCM_GPR32_8 | REGCM_GPR64 |
14501 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
14502 switch(state->cpu) {
14505 avail_mask |= REGCM_MMX;
14509 avail_mask |= REGCM_MMX | REGCM_XMM;
14513 /* Don't enable 8 bit values until I can force both operands
14514 * to be 8bits simultaneously.
14516 avail_mask &= ~(REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16);
14521 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
14523 unsigned mask, result;
14526 result &= arch_avail_mask(state);
14528 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
14529 if ((result & mask) == 0) {
14532 if (class > LAST_REGC) {
14535 for(class2 = 0; class2 <= LAST_REGC; class2++) {
14536 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
14537 (regcm_bound[class2].last <= regcm_bound[class].last)) {
14538 result |= (1 << class2);
14545 static unsigned arch_reg_regcm(struct compile_state *state, int reg)
14550 for(class = 0; class <= LAST_REGC; class++) {
14551 if ((reg >= regcm_bound[class].first) &&
14552 (reg <= regcm_bound[class].last)) {
14553 mask |= (1 << class);
14557 internal_error(state, 0, "reg %d not in any class", reg);
14562 static struct reg_info arch_reg_constraint(
14563 struct compile_state *state, struct type *type, const char *constraint)
14565 static const struct {
14569 } constraints[] = {
14570 { 'r', REGCM_GPR32, REG_UNSET },
14571 { 'g', REGCM_GPR32, REG_UNSET },
14572 { 'p', REGCM_GPR32, REG_UNSET },
14573 { 'q', REGCM_GPR8, REG_UNSET },
14574 { 'Q', REGCM_GPR32_8, REG_UNSET },
14575 { 'x', REGCM_XMM, REG_UNSET },
14576 { 'y', REGCM_MMX, REG_UNSET },
14577 { 'a', REGCM_GPR32, REG_EAX },
14578 { 'b', REGCM_GPR32, REG_EBX },
14579 { 'c', REGCM_GPR32, REG_ECX },
14580 { 'd', REGCM_GPR32, REG_EDX },
14581 { 'D', REGCM_GPR32, REG_EDI },
14582 { 'S', REGCM_GPR32, REG_ESI },
14583 { '\0', 0, REG_UNSET },
14585 unsigned int regcm;
14586 unsigned int mask, reg;
14587 struct reg_info result;
14589 regcm = arch_type_to_regcm(state, type);
14592 for(ptr = constraint; *ptr; ptr++) {
14597 for(i = 0; constraints[i].class != '\0'; i++) {
14598 if (constraints[i].class == *ptr) {
14602 if (constraints[i].class == '\0') {
14603 error(state, 0, "invalid register constraint ``%c''", *ptr);
14606 if ((constraints[i].mask & regcm) == 0) {
14607 error(state, 0, "invalid register class %c specified",
14610 mask |= constraints[i].mask;
14611 if (constraints[i].reg != REG_UNSET) {
14612 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
14613 error(state, 0, "Only one register may be specified");
14615 reg = constraints[i].reg;
14619 result.regcm = mask;
14623 static struct reg_info arch_reg_clobber(
14624 struct compile_state *state, const char *clobber)
14626 struct reg_info result;
14627 if (strcmp(clobber, "memory") == 0) {
14628 result.reg = REG_UNSET;
14631 else if (strcmp(clobber, "%eax") == 0) {
14632 result.reg = REG_EAX;
14633 result.regcm = REGCM_GPR32;
14635 else if (strcmp(clobber, "%ebx") == 0) {
14636 result.reg = REG_EBX;
14637 result.regcm = REGCM_GPR32;
14639 else if (strcmp(clobber, "%ecx") == 0) {
14640 result.reg = REG_ECX;
14641 result.regcm = REGCM_GPR32;
14643 else if (strcmp(clobber, "%edx") == 0) {
14644 result.reg = REG_EDX;
14645 result.regcm = REGCM_GPR32;
14647 else if (strcmp(clobber, "%esi") == 0) {
14648 result.reg = REG_ESI;
14649 result.regcm = REGCM_GPR32;
14651 else if (strcmp(clobber, "%edi") == 0) {
14652 result.reg = REG_EDI;
14653 result.regcm = REGCM_GPR32;
14655 else if (strcmp(clobber, "%ebp") == 0) {
14656 result.reg = REG_EBP;
14657 result.regcm = REGCM_GPR32;
14659 else if (strcmp(clobber, "%esp") == 0) {
14660 result.reg = REG_ESP;
14661 result.regcm = REGCM_GPR32;
14663 else if (strcmp(clobber, "cc") == 0) {
14664 result.reg = REG_EFLAGS;
14665 result.regcm = REGCM_FLAGS;
14667 else if ((strncmp(clobber, "xmm", 3) == 0) &&
14668 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
14669 result.reg = REG_XMM0 + octdigval(clobber[3]);
14670 result.regcm = REGCM_XMM;
14672 else if ((strncmp(clobber, "mmx", 3) == 0) &&
14673 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
14674 result.reg = REG_MMX0 + octdigval(clobber[3]);
14675 result.regcm = REGCM_MMX;
14678 error(state, 0, "Invalid register clobber");
14679 result.reg = REG_UNSET;
14685 static int do_select_reg(struct compile_state *state,
14686 char *used, int reg, unsigned classes)
14692 mask = arch_reg_regcm(state, reg);
14693 return (classes & mask) ? reg : REG_UNSET;
14696 static int arch_select_free_register(
14697 struct compile_state *state, char *used, int classes)
14699 /* Preference: flags, 8bit gprs, 32bit gprs, other 32bit reg
14700 * other types of registers.
14704 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
14705 reg = do_select_reg(state, used, i, classes);
14707 for(i = REGC_GPR32_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR32_LAST); i++) {
14708 reg = do_select_reg(state, used, i, classes);
14710 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
14711 reg = do_select_reg(state, used, i, classes);
14713 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
14714 reg = do_select_reg(state, used, i, classes);
14716 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
14717 reg = do_select_reg(state, used, i, classes);
14719 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
14720 reg = do_select_reg(state, used, i, classes);
14722 for(i = REGC_GPR64_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR64_LAST); i++) {
14723 reg = do_select_reg(state, used, i, classes);
14729 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
14731 #warning "FIXME force types smaller (if legal) before I get here"
14732 unsigned avail_mask;
14735 avail_mask = arch_avail_mask(state);
14736 switch(type->type & TYPE_MASK) {
14743 mask = REGCM_GPR8 |
14744 REGCM_GPR16 | REGCM_GPR16_8 |
14745 REGCM_GPR32 | REGCM_GPR32_8 |
14747 REGCM_MMX | REGCM_XMM |
14748 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
14752 mask = REGCM_GPR16 | REGCM_GPR16_8 |
14753 REGCM_GPR32 | REGCM_GPR32_8 |
14755 REGCM_MMX | REGCM_XMM |
14756 REGCM_IMM32 | REGCM_IMM16;
14763 mask = REGCM_GPR32 | REGCM_GPR32_8 |
14764 REGCM_GPR64 | REGCM_MMX | REGCM_XMM |
14768 internal_error(state, 0, "no register class for type");
14771 mask &= avail_mask;
14775 static int is_imm32(struct triple *imm)
14777 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
14778 (imm->op == OP_ADDRCONST);
14781 static int is_imm16(struct triple *imm)
14783 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
14785 static int is_imm8(struct triple *imm)
14787 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
14790 static int get_imm32(struct triple *ins, struct triple **expr)
14792 struct triple *imm;
14794 while(imm->op == OP_COPY) {
14797 if (!is_imm32(imm)) {
14800 unuse_triple(*expr, ins);
14801 use_triple(imm, ins);
14806 static int get_imm8(struct triple *ins, struct triple **expr)
14808 struct triple *imm;
14810 while(imm->op == OP_COPY) {
14813 if (!is_imm8(imm)) {
14816 unuse_triple(*expr, ins);
14817 use_triple(imm, ins);
14822 #define TEMPLATE_NOP 0
14823 #define TEMPLATE_INTCONST8 1
14824 #define TEMPLATE_INTCONST32 2
14825 #define TEMPLATE_COPY_REG 3
14826 #define TEMPLATE_COPY_IMM32 4
14827 #define TEMPLATE_COPY_IMM16 5
14828 #define TEMPLATE_COPY_IMM8 6
14829 #define TEMPLATE_PHI 7
14830 #define TEMPLATE_STORE8 8
14831 #define TEMPLATE_STORE16 9
14832 #define TEMPLATE_STORE32 10
14833 #define TEMPLATE_LOAD8 11
14834 #define TEMPLATE_LOAD16 12
14835 #define TEMPLATE_LOAD32 13
14836 #define TEMPLATE_BINARY_REG 14
14837 #define TEMPLATE_BINARY_IMM 15
14838 #define TEMPLATE_SL_CL 16
14839 #define TEMPLATE_SL_IMM 17
14840 #define TEMPLATE_UNARY 18
14841 #define TEMPLATE_CMP_REG 19
14842 #define TEMPLATE_CMP_IMM 20
14843 #define TEMPLATE_TEST 21
14844 #define TEMPLATE_SET 22
14845 #define TEMPLATE_JMP 23
14846 #define TEMPLATE_INB_DX 24
14847 #define TEMPLATE_INB_IMM 25
14848 #define TEMPLATE_INW_DX 26
14849 #define TEMPLATE_INW_IMM 27
14850 #define TEMPLATE_INL_DX 28
14851 #define TEMPLATE_INL_IMM 29
14852 #define TEMPLATE_OUTB_DX 30
14853 #define TEMPLATE_OUTB_IMM 31
14854 #define TEMPLATE_OUTW_DX 32
14855 #define TEMPLATE_OUTW_IMM 33
14856 #define TEMPLATE_OUTL_DX 34
14857 #define TEMPLATE_OUTL_IMM 35
14858 #define TEMPLATE_BSF 36
14859 #define TEMPLATE_RDMSR 37
14860 #define TEMPLATE_WRMSR 38
14861 #define LAST_TEMPLATE TEMPLATE_WRMSR
14862 #if LAST_TEMPLATE >= MAX_TEMPLATES
14863 #error "MAX_TEMPLATES to low"
14866 #define COPY_REGCM (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8 | REGCM_MMX | REGCM_XMM)
14867 #define COPY32_REGCM (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
14869 static struct ins_template templates[] = {
14870 [TEMPLATE_NOP] = {},
14871 [TEMPLATE_INTCONST8] = {
14872 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
14874 [TEMPLATE_INTCONST32] = {
14875 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
14877 [TEMPLATE_COPY_REG] = {
14878 .lhs = { [0] = { REG_UNSET, COPY_REGCM } },
14879 .rhs = { [0] = { REG_UNSET, COPY_REGCM } },
14881 [TEMPLATE_COPY_IMM32] = {
14882 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
14883 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
14885 [TEMPLATE_COPY_IMM16] = {
14886 .lhs = { [0] = { REG_UNSET, COPY32_REGCM | REGCM_GPR16 } },
14887 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 } },
14889 [TEMPLATE_COPY_IMM8] = {
14890 .lhs = { [0] = { REG_UNSET, COPY_REGCM } },
14891 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
14894 .lhs = { [0] = { REG_VIRT0, COPY_REGCM } },
14896 [ 0] = { REG_VIRT0, COPY_REGCM },
14897 [ 1] = { REG_VIRT0, COPY_REGCM },
14898 [ 2] = { REG_VIRT0, COPY_REGCM },
14899 [ 3] = { REG_VIRT0, COPY_REGCM },
14900 [ 4] = { REG_VIRT0, COPY_REGCM },
14901 [ 5] = { REG_VIRT0, COPY_REGCM },
14902 [ 6] = { REG_VIRT0, COPY_REGCM },
14903 [ 7] = { REG_VIRT0, COPY_REGCM },
14904 [ 8] = { REG_VIRT0, COPY_REGCM },
14905 [ 9] = { REG_VIRT0, COPY_REGCM },
14906 [10] = { REG_VIRT0, COPY_REGCM },
14907 [11] = { REG_VIRT0, COPY_REGCM },
14908 [12] = { REG_VIRT0, COPY_REGCM },
14909 [13] = { REG_VIRT0, COPY_REGCM },
14910 [14] = { REG_VIRT0, COPY_REGCM },
14911 [15] = { REG_VIRT0, COPY_REGCM },
14913 [TEMPLATE_STORE8] = {
14914 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14915 .rhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
14917 [TEMPLATE_STORE16] = {
14918 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14919 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
14921 [TEMPLATE_STORE32] = {
14922 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14923 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14925 [TEMPLATE_LOAD8] = {
14926 .lhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
14927 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14929 [TEMPLATE_LOAD16] = {
14930 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
14931 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14933 [TEMPLATE_LOAD32] = {
14934 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14935 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14937 [TEMPLATE_BINARY_REG] = {
14938 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
14940 [0] = { REG_VIRT0, REGCM_GPR32 },
14941 [1] = { REG_UNSET, REGCM_GPR32 },
14944 [TEMPLATE_BINARY_IMM] = {
14945 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
14947 [0] = { REG_VIRT0, REGCM_GPR32 },
14948 [1] = { REG_UNNEEDED, REGCM_IMM32 },
14951 [TEMPLATE_SL_CL] = {
14952 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
14954 [0] = { REG_VIRT0, REGCM_GPR32 },
14955 [1] = { REG_CL, REGCM_GPR8 },
14958 [TEMPLATE_SL_IMM] = {
14959 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
14961 [0] = { REG_VIRT0, REGCM_GPR32 },
14962 [1] = { REG_UNNEEDED, REGCM_IMM8 },
14965 [TEMPLATE_UNARY] = {
14966 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
14967 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
14969 [TEMPLATE_CMP_REG] = {
14970 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
14972 [0] = { REG_UNSET, REGCM_GPR32 },
14973 [1] = { REG_UNSET, REGCM_GPR32 },
14976 [TEMPLATE_CMP_IMM] = {
14977 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
14979 [0] = { REG_UNSET, REGCM_GPR32 },
14980 [1] = { REG_UNNEEDED, REGCM_IMM32 },
14983 [TEMPLATE_TEST] = {
14984 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
14985 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14988 .lhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
14989 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
14992 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
14994 [TEMPLATE_INB_DX] = {
14995 .lhs = { [0] = { REG_AL, REGCM_GPR8 } },
14996 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
14998 [TEMPLATE_INB_IMM] = {
14999 .lhs = { [0] = { REG_AL, REGCM_GPR8 } },
15000 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15002 [TEMPLATE_INW_DX] = {
15003 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
15004 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15006 [TEMPLATE_INW_IMM] = {
15007 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
15008 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15010 [TEMPLATE_INL_DX] = {
15011 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
15012 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15014 [TEMPLATE_INL_IMM] = {
15015 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
15016 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15018 [TEMPLATE_OUTB_DX] = {
15020 [0] = { REG_AL, REGCM_GPR8 },
15021 [1] = { REG_DX, REGCM_GPR16 },
15024 [TEMPLATE_OUTB_IMM] = {
15026 [0] = { REG_AL, REGCM_GPR8 },
15027 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15030 [TEMPLATE_OUTW_DX] = {
15032 [0] = { REG_AX, REGCM_GPR16 },
15033 [1] = { REG_DX, REGCM_GPR16 },
15036 [TEMPLATE_OUTW_IMM] = {
15038 [0] = { REG_AX, REGCM_GPR16 },
15039 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15042 [TEMPLATE_OUTL_DX] = {
15044 [0] = { REG_EAX, REGCM_GPR32 },
15045 [1] = { REG_DX, REGCM_GPR16 },
15048 [TEMPLATE_OUTL_IMM] = {
15050 [0] = { REG_EAX, REGCM_GPR32 },
15051 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15055 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15056 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15058 [TEMPLATE_RDMSR] = {
15060 [0] = { REG_EAX, REGCM_GPR32 },
15061 [1] = { REG_EDX, REGCM_GPR32 },
15063 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
15065 [TEMPLATE_WRMSR] = {
15067 [0] = { REG_ECX, REGCM_GPR32 },
15068 [1] = { REG_EAX, REGCM_GPR32 },
15069 [2] = { REG_EDX, REGCM_GPR32 },
15074 static void fixup_branches(struct compile_state *state,
15075 struct triple *cmp, struct triple *use, int jmp_op)
15077 struct triple_set *entry, *next;
15078 for(entry = use->use; entry; entry = next) {
15079 next = entry->next;
15080 if (entry->member->op == OP_COPY) {
15081 fixup_branches(state, cmp, entry->member, jmp_op);
15083 else if (entry->member->op == OP_BRANCH) {
15084 struct triple *branch, *test;
15085 struct triple *left, *right;
15087 left = RHS(cmp, 0);
15088 if (TRIPLE_RHS(cmp->sizes) > 1) {
15089 right = RHS(cmp, 1);
15091 branch = entry->member;
15092 test = pre_triple(state, branch,
15093 cmp->op, cmp->type, left, right);
15094 test->template_id = TEMPLATE_TEST;
15095 if (cmp->op == OP_CMP) {
15096 test->template_id = TEMPLATE_CMP_REG;
15097 if (get_imm32(test, &RHS(test, 1))) {
15098 test->template_id = TEMPLATE_CMP_IMM;
15101 use_triple(RHS(test, 0), test);
15102 use_triple(RHS(test, 1), test);
15103 unuse_triple(RHS(branch, 0), branch);
15104 RHS(branch, 0) = test;
15105 branch->op = jmp_op;
15106 branch->template_id = TEMPLATE_JMP;
15107 use_triple(RHS(branch, 0), branch);
15112 static void bool_cmp(struct compile_state *state,
15113 struct triple *ins, int cmp_op, int jmp_op, int set_op)
15115 struct triple_set *entry, *next;
15116 struct triple *set;
15118 /* Put a barrier up before the cmp which preceeds the
15119 * copy instruction. If a set actually occurs this gives
15120 * us a chance to move variables in registers out of the way.
15123 /* Modify the comparison operator */
15125 ins->template_id = TEMPLATE_TEST;
15126 if (cmp_op == OP_CMP) {
15127 ins->template_id = TEMPLATE_CMP_REG;
15128 if (get_imm32(ins, &RHS(ins, 1))) {
15129 ins->template_id = TEMPLATE_CMP_IMM;
15132 /* Generate the instruction sequence that will transform the
15133 * result of the comparison into a logical value.
15135 set = post_triple(state, ins, set_op, ins->type, ins, 0);
15136 use_triple(ins, set);
15137 set->template_id = TEMPLATE_SET;
15139 for(entry = ins->use; entry; entry = next) {
15140 next = entry->next;
15141 if (entry->member == set) {
15144 replace_rhs_use(state, ins, set, entry->member);
15146 fixup_branches(state, ins, set, jmp_op);
15149 static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
15151 struct triple *next;
15153 lhs = TRIPLE_LHS(ins->sizes);
15154 for(next = ins->next, i = 0; i < lhs; i++, next = next->next) {
15155 if (next != LHS(ins, i)) {
15156 internal_error(state, ins, "malformed lhs on %s",
15159 if (next->op != OP_PIECE) {
15160 internal_error(state, ins, "bad lhs op %s at %d on %s",
15161 tops(next->op), i, tops(ins->op));
15163 if (next->u.cval != i) {
15164 internal_error(state, ins, "bad u.cval of %d %d expected",
15171 struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
15173 struct ins_template *template;
15174 struct reg_info result;
15176 if (ins->op == OP_PIECE) {
15177 index = ins->u.cval;
15178 ins = MISC(ins, 0);
15180 zlhs = TRIPLE_LHS(ins->sizes);
15181 if (triple_is_def(state, ins)) {
15184 if (index >= zlhs) {
15185 internal_error(state, ins, "index %d out of range for %s\n",
15186 index, tops(ins->op));
15190 template = &ins->u.ainfo->tmpl;
15193 if (ins->template_id > LAST_TEMPLATE) {
15194 internal_error(state, ins, "bad template number %d",
15197 template = &templates[ins->template_id];
15200 result = template->lhs[index];
15201 result.regcm = arch_regcm_normalize(state, result.regcm);
15202 if (result.reg != REG_UNNEEDED) {
15203 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
15205 if (result.regcm == 0) {
15206 internal_error(state, ins, "lhs %d regcm == 0", index);
15211 struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
15213 struct reg_info result;
15214 struct ins_template *template;
15215 if ((index > TRIPLE_RHS(ins->sizes)) ||
15216 (ins->op == OP_PIECE)) {
15217 internal_error(state, ins, "index %d out of range for %s\n",
15218 index, tops(ins->op));
15222 template = &ins->u.ainfo->tmpl;
15225 if (ins->template_id > LAST_TEMPLATE) {
15226 internal_error(state, ins, "bad template number %d",
15229 template = &templates[ins->template_id];
15232 result = template->rhs[index];
15233 result.regcm = arch_regcm_normalize(state, result.regcm);
15234 if (result.regcm == 0) {
15235 internal_error(state, ins, "rhs %d regcm == 0", index);
15240 static struct triple *transform_to_arch_instruction(
15241 struct compile_state *state, struct triple *ins)
15243 /* Transform from generic 3 address instructions
15244 * to archtecture specific instructions.
15245 * And apply architecture specific constrains to instructions.
15246 * Copies are inserted to preserve the register flexibility
15247 * of 3 address instructions.
15249 struct triple *next;
15253 ins->template_id = TEMPLATE_INTCONST32;
15254 if (ins->u.cval < 256) {
15255 ins->template_id = TEMPLATE_INTCONST8;
15259 ins->template_id = TEMPLATE_INTCONST32;
15265 ins->template_id = TEMPLATE_NOP;
15268 ins->template_id = TEMPLATE_COPY_REG;
15269 if (is_imm8(RHS(ins, 0))) {
15270 ins->template_id = TEMPLATE_COPY_IMM8;
15272 else if (is_imm16(RHS(ins, 0))) {
15273 ins->template_id = TEMPLATE_COPY_IMM16;
15275 else if (is_imm32(RHS(ins, 0))) {
15276 ins->template_id = TEMPLATE_COPY_IMM32;
15278 else if (is_const(RHS(ins, 0))) {
15279 internal_error(state, ins, "bad constant passed to copy");
15283 ins->template_id = TEMPLATE_PHI;
15286 switch(ins->type->type & TYPE_MASK) {
15287 case TYPE_CHAR: case TYPE_UCHAR:
15288 ins->template_id = TEMPLATE_STORE8;
15290 case TYPE_SHORT: case TYPE_USHORT:
15291 ins->template_id = TEMPLATE_STORE16;
15293 case TYPE_INT: case TYPE_UINT:
15294 case TYPE_LONG: case TYPE_ULONG:
15296 ins->template_id = TEMPLATE_STORE32;
15299 internal_error(state, ins, "unknown type in store");
15304 switch(ins->type->type & TYPE_MASK) {
15305 case TYPE_CHAR: case TYPE_UCHAR:
15306 ins->template_id = TEMPLATE_LOAD8;
15310 ins->template_id = TEMPLATE_LOAD16;
15317 ins->template_id = TEMPLATE_LOAD32;
15320 internal_error(state, ins, "unknown type in load");
15330 ins->template_id = TEMPLATE_BINARY_REG;
15331 if (get_imm32(ins, &RHS(ins, 1))) {
15332 ins->template_id = TEMPLATE_BINARY_IMM;
15338 ins->template_id = TEMPLATE_SL_CL;
15339 if (get_imm8(ins, &RHS(ins, 1))) {
15340 ins->template_id = TEMPLATE_SL_IMM;
15345 ins->template_id = TEMPLATE_UNARY;
15348 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
15351 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
15354 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
15357 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
15360 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
15363 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
15366 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
15369 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
15372 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
15375 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
15378 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
15381 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
15384 if (TRIPLE_RHS(ins->sizes) > 0) {
15385 internal_error(state, ins, "bad branch test");
15388 ins->template_id = TEMPLATE_NOP;
15394 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
15395 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
15396 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
15398 if (get_imm8(ins, &RHS(ins, 0))) {
15399 ins->template_id += 1;
15406 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
15407 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
15408 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
15410 if (get_imm8(ins, &RHS(ins, 1))) {
15411 ins->template_id += 1;
15416 ins->template_id = TEMPLATE_BSF;
15419 ins->template_id = TEMPLATE_RDMSR;
15420 next = after_lhs(state, ins);
15423 ins->template_id = TEMPLATE_WRMSR;
15426 ins->template_id = TEMPLATE_NOP;
15429 ins->template_id = TEMPLATE_NOP;
15430 next = after_lhs(state, ins);
15432 /* Already transformed instructions */
15434 ins->template_id = TEMPLATE_TEST;
15437 ins->template_id = TEMPLATE_CMP_REG;
15438 if (get_imm32(ins, &RHS(ins, 1))) {
15439 ins->template_id = TEMPLATE_CMP_IMM;
15442 case OP_JMP_EQ: case OP_JMP_NOTEQ:
15443 case OP_JMP_SLESS: case OP_JMP_ULESS:
15444 case OP_JMP_SMORE: case OP_JMP_UMORE:
15445 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
15446 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
15447 ins->template_id = TEMPLATE_JMP;
15449 case OP_SET_EQ: case OP_SET_NOTEQ:
15450 case OP_SET_SLESS: case OP_SET_ULESS:
15451 case OP_SET_SMORE: case OP_SET_UMORE:
15452 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
15453 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
15454 ins->template_id = TEMPLATE_SET;
15456 /* Unhandled instructions */
15459 internal_error(state, ins, "unhandled ins: %d %s\n",
15460 ins->op, tops(ins->op));
15466 static void generate_local_labels(struct compile_state *state)
15468 struct triple *first, *label;
15471 first = RHS(state->main_function, 0);
15474 if ((label->op == OP_LABEL) ||
15475 (label->op == OP_SDECL)) {
15477 label->u.cval = ++label_counter;
15483 label = label->next;
15484 } while(label != first);
15487 static int check_reg(struct compile_state *state,
15488 struct triple *triple, int classes)
15492 reg = ID_REG(triple->id);
15493 if (reg == REG_UNSET) {
15494 internal_error(state, triple, "register not set");
15496 mask = arch_reg_regcm(state, reg);
15497 if (!(classes & mask)) {
15498 internal_error(state, triple, "reg %d in wrong class",
15504 static const char *arch_reg_str(int reg)
15506 static const char *regs[] = {
15510 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
15511 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
15512 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
15514 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
15515 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
15516 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
15518 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
15525 static const char *reg(struct compile_state *state, struct triple *triple,
15529 reg = check_reg(state, triple, classes);
15530 return arch_reg_str(reg);
15533 const char *type_suffix(struct compile_state *state, struct type *type)
15535 const char *suffix;
15536 switch(size_of(state, type)) {
15537 case 1: suffix = "b"; break;
15538 case 2: suffix = "w"; break;
15539 case 4: suffix = "l"; break;
15541 internal_error(state, 0, "unknown suffix");
15548 static void print_const_val(
15549 struct compile_state *state, struct triple *ins, FILE *fp)
15553 fprintf(fp, " $%ld ",
15554 (long_t)(ins->u.cval));
15557 fprintf(fp, " $L%lu+%lu ",
15558 MISC(ins, 0)->u.cval,
15562 internal_error(state, ins, "unknown constant type");
15567 static void print_binary_op(struct compile_state *state,
15568 const char *op, struct triple *ins, FILE *fp)
15571 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
15572 if (RHS(ins, 0)->id != ins->id) {
15573 internal_error(state, ins, "invalid register assignment");
15575 if (is_const(RHS(ins, 1))) {
15576 fprintf(fp, "\t%s ", op);
15577 print_const_val(state, RHS(ins, 1), fp);
15578 fprintf(fp, ", %s\n",
15579 reg(state, RHS(ins, 0), mask));
15582 unsigned lmask, rmask;
15584 lreg = check_reg(state, RHS(ins, 0), mask);
15585 rreg = check_reg(state, RHS(ins, 1), mask);
15586 lmask = arch_reg_regcm(state, lreg);
15587 rmask = arch_reg_regcm(state, rreg);
15588 mask = lmask & rmask;
15589 fprintf(fp, "\t%s %s, %s\n",
15591 reg(state, RHS(ins, 1), mask),
15592 reg(state, RHS(ins, 0), mask));
15595 static void print_unary_op(struct compile_state *state,
15596 const char *op, struct triple *ins, FILE *fp)
15599 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
15600 fprintf(fp, "\t%s %s\n",
15602 reg(state, RHS(ins, 0), mask));
15605 static void print_op_shift(struct compile_state *state,
15606 const char *op, struct triple *ins, FILE *fp)
15609 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
15610 if (RHS(ins, 0)->id != ins->id) {
15611 internal_error(state, ins, "invalid register assignment");
15613 if (is_const(RHS(ins, 1))) {
15614 fprintf(fp, "\t%s ", op);
15615 print_const_val(state, RHS(ins, 1), fp);
15616 fprintf(fp, ", %s\n",
15617 reg(state, RHS(ins, 0), mask));
15620 fprintf(fp, "\t%s %s, %s\n",
15622 reg(state, RHS(ins, 1), REGCM_GPR8),
15623 reg(state, RHS(ins, 0), mask));
15627 static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
15634 case OP_INB: op = "inb", mask = REGCM_GPR8; break;
15635 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
15636 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
15638 internal_error(state, ins, "not an in operation");
15642 dreg = check_reg(state, ins, mask);
15643 if (!reg_is_reg(state, dreg, REG_EAX)) {
15644 internal_error(state, ins, "dst != %%eax");
15646 if (is_const(RHS(ins, 0))) {
15647 fprintf(fp, "\t%s ", op);
15648 print_const_val(state, RHS(ins, 0), fp);
15649 fprintf(fp, ", %s\n",
15650 reg(state, ins, mask));
15654 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
15655 if (!reg_is_reg(state, addr_reg, REG_DX)) {
15656 internal_error(state, ins, "src != %%dx");
15658 fprintf(fp, "\t%s %s, %s\n",
15660 reg(state, RHS(ins, 0), REGCM_GPR16),
15661 reg(state, ins, mask));
15665 static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
15672 case OP_OUTB: op = "outb", mask = REGCM_GPR8; break;
15673 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
15674 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
15676 internal_error(state, ins, "not an out operation");
15680 lreg = check_reg(state, RHS(ins, 0), mask);
15681 if (!reg_is_reg(state, lreg, REG_EAX)) {
15682 internal_error(state, ins, "src != %%eax");
15684 if (is_const(RHS(ins, 1))) {
15685 fprintf(fp, "\t%s %s,",
15686 op, reg(state, RHS(ins, 0), mask));
15687 print_const_val(state, RHS(ins, 1), fp);
15692 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
15693 if (!reg_is_reg(state, addr_reg, REG_DX)) {
15694 internal_error(state, ins, "dst != %%dx");
15696 fprintf(fp, "\t%s %s, %s\n",
15698 reg(state, RHS(ins, 0), mask),
15699 reg(state, RHS(ins, 1), REGCM_GPR16));
15703 static void print_op_move(struct compile_state *state,
15704 struct triple *ins, FILE *fp)
15706 /* op_move is complex because there are many types
15707 * of registers we can move between.
15708 * Because OP_COPY will be introduced in arbitrary locations
15709 * OP_COPY must not affect flags.
15711 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
15712 struct triple *dst, *src;
15713 if (ins->op == OP_COPY) {
15717 else if (ins->op == OP_WRITE) {
15722 internal_error(state, ins, "unknown move operation");
15725 if (!is_const(src)) {
15726 int src_reg, dst_reg;
15727 int src_regcm, dst_regcm;
15728 src_reg = ID_REG(src->id);
15729 dst_reg = ID_REG(dst->id);
15730 src_regcm = arch_reg_regcm(state, src_reg);
15731 dst_regcm = arch_reg_regcm(state, dst_reg);
15732 /* If the class is the same just move the register */
15733 if (src_regcm & dst_regcm &
15734 (REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32)) {
15735 if ((src_reg != dst_reg) || !omit_copy) {
15736 fprintf(fp, "\tmov %s, %s\n",
15737 reg(state, src, src_regcm),
15738 reg(state, dst, dst_regcm));
15741 /* Move 32bit to 16bit */
15742 else if ((src_regcm & REGCM_GPR32) &&
15743 (dst_regcm & REGCM_GPR16)) {
15744 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
15745 if ((src_reg != dst_reg) || !omit_copy) {
15746 fprintf(fp, "\tmovw %s, %s\n",
15747 arch_reg_str(src_reg),
15748 arch_reg_str(dst_reg));
15751 /* Move 32bit to 8bit */
15752 else if ((src_regcm & REGCM_GPR32_8) &&
15753 (dst_regcm & REGCM_GPR8))
15755 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
15756 if ((src_reg != dst_reg) || !omit_copy) {
15757 fprintf(fp, "\tmovb %s, %s\n",
15758 arch_reg_str(src_reg),
15759 arch_reg_str(dst_reg));
15762 /* Move 16bit to 8bit */
15763 else if ((src_regcm & REGCM_GPR16_8) &&
15764 (dst_regcm & REGCM_GPR8))
15766 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
15767 if ((src_reg != dst_reg) || !omit_copy) {
15768 fprintf(fp, "\tmovb %s, %s\n",
15769 arch_reg_str(src_reg),
15770 arch_reg_str(dst_reg));
15773 /* Move 8/16bit to 16/32bit */
15774 else if ((src_regcm & (REGCM_GPR8 | REGCM_GPR16)) &&
15775 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
15777 op = is_signed(src->type)? "movsx": "movzx";
15778 fprintf(fp, "\t%s %s, %s\n",
15780 reg(state, src, src_regcm),
15781 reg(state, dst, dst_regcm));
15783 /* Move between sse registers */
15784 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
15785 if ((src_reg != dst_reg) || !omit_copy) {
15786 fprintf(fp, "\tmovdqa %s, %s\n",
15787 reg(state, src, src_regcm),
15788 reg(state, dst, dst_regcm));
15791 /* Move between mmx registers or mmx & sse registers */
15792 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
15793 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
15794 if ((src_reg != dst_reg) || !omit_copy) {
15795 fprintf(fp, "\tmovq %s, %s\n",
15796 reg(state, src, src_regcm),
15797 reg(state, dst, dst_regcm));
15800 /* Move between 32bit gprs & mmx/sse registers */
15801 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
15802 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
15803 fprintf(fp, "\tmovd %s, %s\n",
15804 reg(state, src, src_regcm),
15805 reg(state, dst, dst_regcm));
15807 #if X86_4_8BIT_GPRS
15808 /* Move from 8bit gprs to mmx/sse registers */
15809 else if ((src_regcm & REGCM_GPR8) && (src_reg <= REG_DL) &&
15810 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
15813 op = is_signed(src->type)? "movsx":"movzx";
15814 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
15815 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
15817 reg(state, src, src_regcm),
15818 arch_reg_str(mid_reg),
15819 arch_reg_str(mid_reg),
15820 reg(state, dst, dst_regcm));
15822 /* Move from mmx/sse registers and 8bit gprs */
15823 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
15824 (dst_regcm & REGCM_GPR8) && (dst_reg <= REG_DL)) {
15826 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
15827 fprintf(fp, "\tmovd %s, %s\n",
15828 reg(state, src, src_regcm),
15829 arch_reg_str(mid_reg));
15831 /* Move from 32bit gprs to 16bit gprs */
15832 else if ((src_regcm & REGCM_GPR32) &&
15833 (dst_regcm & REGCM_GPR16)) {
15834 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
15835 if ((src_reg != dst_reg) || !omit_copy) {
15836 fprintf(fp, "\tmov %s, %s\n",
15837 arch_reg_str(src_reg),
15838 arch_reg_str(dst_reg));
15841 /* Move from 32bit gprs to 8bit gprs */
15842 else if ((src_regcm & REGCM_GPR32) &&
15843 (dst_regcm & REGCM_GPR8)) {
15844 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
15845 if ((src_reg != dst_reg) || !omit_copy) {
15846 fprintf(fp, "\tmov %s, %s\n",
15847 arch_reg_str(src_reg),
15848 arch_reg_str(dst_reg));
15851 /* Move from 16bit gprs to 8bit gprs */
15852 else if ((src_regcm & REGCM_GPR16) &&
15853 (dst_regcm & REGCM_GPR8)) {
15854 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
15855 if ((src_reg != dst_reg) || !omit_copy) {
15856 fprintf(fp, "\tmov %s, %s\n",
15857 arch_reg_str(src_reg),
15858 arch_reg_str(dst_reg));
15861 #endif /* X86_4_8BIT_GPRS */
15863 internal_error(state, ins, "unknown copy type");
15867 fprintf(fp, "\tmov ");
15868 print_const_val(state, src, fp);
15869 fprintf(fp, ", %s\n",
15870 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8));
15874 static void print_op_load(struct compile_state *state,
15875 struct triple *ins, FILE *fp)
15877 struct triple *dst, *src;
15880 if (is_const(src) || is_const(dst)) {
15881 internal_error(state, ins, "unknown load operation");
15883 fprintf(fp, "\tmov (%s), %s\n",
15884 reg(state, src, REGCM_GPR32),
15885 reg(state, dst, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32));
15889 static void print_op_store(struct compile_state *state,
15890 struct triple *ins, FILE *fp)
15892 struct triple *dst, *src;
15895 if (is_const(src) && (src->op == OP_INTCONST)) {
15897 value = (long_t)(src->u.cval);
15898 fprintf(fp, "\tmov%s $%ld, (%s)\n",
15899 type_suffix(state, src->type),
15901 reg(state, dst, REGCM_GPR32));
15903 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
15904 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
15905 type_suffix(state, src->type),
15906 reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
15910 if (is_const(src) || is_const(dst)) {
15911 internal_error(state, ins, "unknown store operation");
15913 fprintf(fp, "\tmov%s %s, (%s)\n",
15914 type_suffix(state, src->type),
15915 reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
15916 reg(state, dst, REGCM_GPR32));
15922 static void print_op_smul(struct compile_state *state,
15923 struct triple *ins, FILE *fp)
15925 if (!is_const(RHS(ins, 1))) {
15926 fprintf(fp, "\timul %s, %s\n",
15927 reg(state, RHS(ins, 1), REGCM_GPR32),
15928 reg(state, RHS(ins, 0), REGCM_GPR32));
15931 fprintf(fp, "\timul ");
15932 print_const_val(state, RHS(ins, 1), fp);
15933 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
15937 static void print_op_cmp(struct compile_state *state,
15938 struct triple *ins, FILE *fp)
15942 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
15943 dreg = check_reg(state, ins, REGCM_FLAGS);
15944 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
15945 internal_error(state, ins, "bad dest register for cmp");
15947 if (is_const(RHS(ins, 1))) {
15948 fprintf(fp, "\tcmp ");
15949 print_const_val(state, RHS(ins, 1), fp);
15950 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
15953 unsigned lmask, rmask;
15955 lreg = check_reg(state, RHS(ins, 0), mask);
15956 rreg = check_reg(state, RHS(ins, 1), mask);
15957 lmask = arch_reg_regcm(state, lreg);
15958 rmask = arch_reg_regcm(state, rreg);
15959 mask = lmask & rmask;
15960 fprintf(fp, "\tcmp %s, %s\n",
15961 reg(state, RHS(ins, 1), mask),
15962 reg(state, RHS(ins, 0), mask));
15966 static void print_op_test(struct compile_state *state,
15967 struct triple *ins, FILE *fp)
15970 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
15971 fprintf(fp, "\ttest %s, %s\n",
15972 reg(state, RHS(ins, 0), mask),
15973 reg(state, RHS(ins, 0), mask));
15976 static void print_op_branch(struct compile_state *state,
15977 struct triple *branch, FILE *fp)
15979 const char *bop = "j";
15980 if (branch->op == OP_JMP) {
15981 if (TRIPLE_RHS(branch->sizes) != 0) {
15982 internal_error(state, branch, "jmp with condition?");
15987 struct triple *ptr;
15988 if (TRIPLE_RHS(branch->sizes) != 1) {
15989 internal_error(state, branch, "jmpcc without condition?");
15991 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
15992 if ((RHS(branch, 0)->op != OP_CMP) &&
15993 (RHS(branch, 0)->op != OP_TEST)) {
15994 internal_error(state, branch, "bad branch test");
15996 #warning "FIXME I have observed instructions between the test and branch instructions"
15997 ptr = RHS(branch, 0);
15998 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
15999 if (ptr->op != OP_COPY) {
16000 internal_error(state, branch, "branch does not follow test");
16003 switch(branch->op) {
16004 case OP_JMP_EQ: bop = "jz"; break;
16005 case OP_JMP_NOTEQ: bop = "jnz"; break;
16006 case OP_JMP_SLESS: bop = "jl"; break;
16007 case OP_JMP_ULESS: bop = "jb"; break;
16008 case OP_JMP_SMORE: bop = "jg"; break;
16009 case OP_JMP_UMORE: bop = "ja"; break;
16010 case OP_JMP_SLESSEQ: bop = "jle"; break;
16011 case OP_JMP_ULESSEQ: bop = "jbe"; break;
16012 case OP_JMP_SMOREEQ: bop = "jge"; break;
16013 case OP_JMP_UMOREEQ: bop = "jae"; break;
16015 internal_error(state, branch, "Invalid branch op");
16020 fprintf(fp, "\t%s L%lu\n",
16021 bop, TARG(branch, 0)->u.cval);
16024 static void print_op_set(struct compile_state *state,
16025 struct triple *set, FILE *fp)
16027 const char *sop = "set";
16028 if (TRIPLE_RHS(set->sizes) != 1) {
16029 internal_error(state, set, "setcc without condition?");
16031 check_reg(state, RHS(set, 0), REGCM_FLAGS);
16032 if ((RHS(set, 0)->op != OP_CMP) &&
16033 (RHS(set, 0)->op != OP_TEST)) {
16034 internal_error(state, set, "bad set test");
16036 if (RHS(set, 0)->next != set) {
16037 internal_error(state, set, "set does not follow test");
16040 case OP_SET_EQ: sop = "setz"; break;
16041 case OP_SET_NOTEQ: sop = "setnz"; break;
16042 case OP_SET_SLESS: sop = "setl"; break;
16043 case OP_SET_ULESS: sop = "setb"; break;
16044 case OP_SET_SMORE: sop = "setg"; break;
16045 case OP_SET_UMORE: sop = "seta"; break;
16046 case OP_SET_SLESSEQ: sop = "setle"; break;
16047 case OP_SET_ULESSEQ: sop = "setbe"; break;
16048 case OP_SET_SMOREEQ: sop = "setge"; break;
16049 case OP_SET_UMOREEQ: sop = "setae"; break;
16051 internal_error(state, set, "Invalid set op");
16054 fprintf(fp, "\t%s %s\n",
16055 sop, reg(state, set, REGCM_GPR8));
16058 static void print_op_bit_scan(struct compile_state *state,
16059 struct triple *ins, FILE *fp)
16063 case OP_BSF: op = "bsf"; break;
16064 case OP_BSR: op = "bsr"; break;
16066 internal_error(state, ins, "unknown bit scan");
16076 reg(state, RHS(ins, 0), REGCM_GPR32),
16077 reg(state, ins, REGCM_GPR32),
16078 reg(state, ins, REGCM_GPR32));
16081 static void print_const(struct compile_state *state,
16082 struct triple *ins, FILE *fp)
16086 switch(ins->type->type & TYPE_MASK) {
16089 fprintf(fp, ".byte 0x%02lx\n", ins->u.cval);
16093 fprintf(fp, ".short 0x%04lx\n", ins->u.cval);
16099 fprintf(fp, ".int %lu\n", ins->u.cval);
16102 internal_error(state, ins, "Unknown constant type");
16107 unsigned char *blob;
16109 size = size_of(state, ins->type);
16110 blob = ins->u.blob;
16111 for(i = 0; i < size; i++) {
16112 fprintf(fp, ".byte 0x%02x\n",
16118 internal_error(state, ins, "Unknown constant type");
16123 #define TEXT_SECTION ".rom.text"
16124 #define DATA_SECTION ".rom.data"
16126 static void print_sdecl(struct compile_state *state,
16127 struct triple *ins, FILE *fp)
16129 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
16130 fprintf(fp, ".balign %d\n", align_of(state, ins->type));
16131 fprintf(fp, "L%lu:\n", ins->u.cval);
16132 print_const(state, MISC(ins, 0), fp);
16133 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
16137 static void print_instruction(struct compile_state *state,
16138 struct triple *ins, FILE *fp)
16140 /* Assumption: after I have exted the register allocator
16141 * everything is in a valid register.
16145 print_op_asm(state, ins, fp);
16147 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
16148 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
16149 case OP_AND: print_binary_op(state, "and", ins, fp); break;
16150 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
16151 case OP_OR: print_binary_op(state, "or", ins, fp); break;
16152 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
16153 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
16154 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
16155 case OP_POS: break;
16156 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
16157 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
16161 /* Don't generate anything here for constants */
16163 /* Don't generate anything for variable declarations. */
16166 print_sdecl(state, ins, fp);
16170 print_op_move(state, ins, fp);
16173 print_op_load(state, ins, fp);
16176 print_op_store(state, ins, fp);
16179 print_op_smul(state, ins, fp);
16181 case OP_CMP: print_op_cmp(state, ins, fp); break;
16182 case OP_TEST: print_op_test(state, ins, fp); break;
16184 case OP_JMP_EQ: case OP_JMP_NOTEQ:
16185 case OP_JMP_SLESS: case OP_JMP_ULESS:
16186 case OP_JMP_SMORE: case OP_JMP_UMORE:
16187 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
16188 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
16189 print_op_branch(state, ins, fp);
16191 case OP_SET_EQ: case OP_SET_NOTEQ:
16192 case OP_SET_SLESS: case OP_SET_ULESS:
16193 case OP_SET_SMORE: case OP_SET_UMORE:
16194 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
16195 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
16196 print_op_set(state, ins, fp);
16198 case OP_INB: case OP_INW: case OP_INL:
16199 print_op_in(state, ins, fp);
16201 case OP_OUTB: case OP_OUTW: case OP_OUTL:
16202 print_op_out(state, ins, fp);
16206 print_op_bit_scan(state, ins, fp);
16209 after_lhs(state, ins);
16210 fprintf(fp, "\trdmsr\n");
16213 fprintf(fp, "\twrmsr\n");
16216 fprintf(fp, "\thlt\n");
16222 fprintf(fp, "L%lu:\n", ins->u.cval);
16224 /* Ignore OP_PIECE */
16227 /* Operations I am not yet certain how to handle */
16229 case OP_SDIV: case OP_UDIV:
16230 case OP_SMOD: case OP_UMOD:
16231 /* Operations that should never get here */
16232 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
16233 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
16234 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
16236 internal_error(state, ins, "unknown op: %d %s",
16237 ins->op, tops(ins->op));
16242 static void print_instructions(struct compile_state *state)
16244 struct triple *first, *ins;
16245 int print_location;
16248 const char *last_filename;
16250 print_location = 1;
16254 fp = state->output;
16255 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
16256 first = RHS(state->main_function, 0);
16259 if (print_location &&
16260 ((last_filename != ins->filename) ||
16261 (last_line != ins->line) ||
16262 (last_col != ins->col))) {
16263 fprintf(fp, "\t/* %s:%d */\n",
16264 ins->filename, ins->line);
16265 last_filename = ins->filename;
16266 last_line = ins->line;
16267 last_col = ins->col;
16270 print_instruction(state, ins, fp);
16272 } while(ins != first);
16275 static void generate_code(struct compile_state *state)
16277 generate_local_labels(state);
16278 print_instructions(state);
16282 static void print_tokens(struct compile_state *state)
16285 tk = &state->token[0];
16290 next_token(state, 0);
16292 loc(stdout, state, 0);
16293 printf("%s <- `%s'\n",
16295 tk->ident ? tk->ident->name :
16296 tk->str_len ? tk->val.str : "");
16298 } while(tk->tok != TOK_EOF);
16301 static void compile(const char *filename, const char *ofilename,
16302 int cpu, int debug, int opt)
16305 struct compile_state state;
16306 memset(&state, 0, sizeof(state));
16308 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
16309 memset(&state.token[i], 0, sizeof(state.token[i]));
16310 state.token[i].tok = -1;
16312 /* Remember the debug settings */
16314 state.debug = debug;
16315 state.optimize = opt;
16316 /* Remember the output filename */
16317 state.ofilename = ofilename;
16318 state.output = fopen(state.ofilename, "w");
16319 if (!state.output) {
16320 error(&state, 0, "Cannot open output file %s\n",
16323 /* Prep the preprocessor */
16324 state.if_depth = 0;
16325 state.if_value = 0;
16326 /* register the C keywords */
16327 register_keywords(&state);
16328 /* register the keywords the macro preprocessor knows */
16329 register_macro_keywords(&state);
16330 /* Memorize where some special keywords are. */
16331 state.i_continue = lookup(&state, "continue", 8);
16332 state.i_break = lookup(&state, "break", 5);
16333 /* Enter the globl definition scope */
16334 start_scope(&state);
16335 register_builtins(&state);
16336 compile_file(&state, filename, 1);
16338 print_tokens(&state);
16341 /* Exit the global definition scope */
16344 /* Now that basic compilation has happened
16345 * optimize the intermediate code
16349 generate_code(&state);
16351 fprintf(stderr, "done\n");
16355 static void version(void)
16357 printf("romcc " VERSION " released " RELEASE_DATE "\n");
16360 static void usage(void)
16364 "Usage: romcc <source>.c\n"
16365 "Compile a C source file without using ram\n"
16369 static void arg_error(char *fmt, ...)
16372 va_start(args, fmt);
16373 vfprintf(stderr, fmt, args);
16379 int main(int argc, char **argv)
16381 const char *filename;
16382 const char *ofilename;
16388 ofilename = "auto.inc";
16392 while((argc > 1) && (argc != last_argc)) {
16394 if (strncmp(argv[1], "--debug=", 8) == 0) {
16395 debug = atoi(argv[1] + 8);
16399 else if ((strcmp(argv[1],"-O") == 0) ||
16400 (strcmp(argv[1], "-O1") == 0)) {
16405 else if (strcmp(argv[1],"-O2") == 0) {
16410 else if ((strcmp(argv[1], "-o") == 0) && (argc > 2)) {
16411 ofilename = argv[2];
16415 else if (strncmp(argv[1], "-mcpu=", 6) == 0) {
16416 cpu = arch_encode_cpu(argv[1] + 6);
16417 if (cpu == BAD_CPU) {
16418 arg_error("Invalid cpu specified: %s\n",
16426 arg_error("Wrong argument count %d\n", argc);
16428 filename = argv[1];
16429 compile(filename, ofilename, cpu, debug, optimize);