- Minor fixes for handling structure constants and array values that are not sdecls
[coreboot.git] / util / romcc / romcc.c
index 29df259edd6c4acf2effb7c589f6208ef326bc6f..48632a8fc6cfab5199ebd7bcd4abf35cfd406056 100644 (file)
@@ -9,17 +9,21 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <string.h>
-#include <ctype.h>
 #include <limits.h>
 
 #define DEBUG_ERROR_MESSAGES 0
 #define DEBUG_COLOR_GRAPH 0
 #define DEBUG_SCC 0
-#define X86_4_8BIT_GPRS 1
+#define DEBUG_CONSISTENCY 2
+#define DEBUG_RANGE_CONFLICTS 0
+#define DEBUG_COALESCING 0
+#define DEBUG_SDP_BLOCKS 0
+#define DEBUG_TRIPLE_COLOR 0
 
-#warning "FIXME static constant variables"
-#warning "FIXME enable pointers"
-#warning "FIXME enable string constants"
+#warning "FIXME boundary cases with small types in larger registers"
+#warning "FIXME give clear error messages about unused variables"
+#warning "FIXME properly handle multi dimensional arrays"
+#warning "FIXME fix scc_transform"
 
 /*  Control flow graph of a loop without goto.
  * 
@@ -217,6 +221,9 @@ struct file_state {
        char *pos;
        int line;
        char *line_start;
+       int report_line;
+       const char *report_name;
+       const char *report_dir;
 };
 struct hash_entry;
 struct token {
@@ -258,23 +265,25 @@ struct token {
 /* Operations on general purpose registers.
  */
 
-#define OP_SMUL       0
-#define OP_UMUL       1
-#define OP_SDIV       2
-#define OP_UDIV       3
-#define OP_SMOD       4
-#define OP_UMOD       5
-#define OP_ADD        6
-#define OP_SUB        7
-#define OP_SL         8 
-#define OP_USR        9
-#define OP_SSR       10 
-#define OP_AND       11 
-#define OP_XOR       12
-#define OP_OR        13
-#define OP_POS       14 /* Dummy positive operator don't use it */
-#define OP_NEG       15
-#define OP_INVERT    16
+#define OP_SDIVT      0
+#define OP_UDIVT      1
+#define OP_SMUL       2
+#define OP_UMUL       3
+#define OP_SDIV       4
+#define OP_UDIV       5
+#define OP_SMOD       6
+#define OP_UMOD       7
+#define OP_ADD        8
+#define OP_SUB        9
+#define OP_SL        10
+#define OP_USR       11
+#define OP_SSR       12 
+#define OP_AND       13 
+#define OP_XOR       14
+#define OP_OR        15
+#define OP_POS       16 /* Dummy positive operator don't use it */
+#define OP_NEG       17
+#define OP_INVERT    18
                     
 #define OP_EQ        20
 #define OP_NOTEQ     21
@@ -292,6 +301,10 @@ struct token {
 
 #define OP_LOAD      32
 #define OP_STORE     33
+/* For OP_STORE ->type holds the type
+ * RHS(0) holds the destination address
+ * RHS(1) holds the value to store.
+ */
 
 #define OP_NOOP      34
 
@@ -299,123 +312,136 @@ struct token {
 #define OP_MAX_CONST 59
 #define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
 #define OP_INTCONST  50
+/* For OP_INTCONST ->type holds the type.
+ * ->u.cval holds the constant value.
+ */
 #define OP_BLOBCONST 51
-/* For OP_BLOBCONST triple->type holds the layout and size
+/* For OP_BLOBCONST ->type holds the layout and size
  * information.  u.blob holds a pointer to the raw binary
  * data for the constant initializer.
  */
 #define OP_ADDRCONST 52
-/* For OP_ADDRCONST triple->type holds the type.
- * triple->left holds the reference to the static variable.
- * triple->u.cval holds an offset from that value.
+/* For OP_ADDRCONST ->type holds the type.
+ * MISC(0) holds the reference to the static variable.
+ * ->u.cval holds an offset from that value.
  */
 
 #define OP_WRITE     60 
 /* OP_WRITE moves one pseudo register to another.
- * triple->left holds the destination pseudo register,
- * must be an OP_DECL.
- * triple->right holds the psuedo to move.
+ * RHS(0) holds the destination pseudo register, which must be an OP_DECL.
+ * RHS(1) holds the psuedo to move.
  */
 
 #define OP_READ      61
 /* OP_READ reads the value of a variable and makes
  * it available for the pseudo operation.
  * Useful for things like def-use chains.
- * triple->left holds points to the triple to read from.
+ * RHS(0) holds points to the triple to read from.
  */
 #define OP_COPY      62
-/* OP_COPY makes a copy of the psedo register or constant in op->left.
+/* OP_COPY makes a copy of the psedo register or constant in RHS(0).
+ */
+#define OP_PIECE     63
+/* OP_PIECE returns one piece of a instruction that returns a structure.
+ * MISC(0) is the instruction
+ * u.cval is the LHS piece of the instruction to return.
+ */
+#define OP_ASM       64
+/* OP_ASM holds a sequence of assembly instructions, the result
+ * of a C asm directive.
+ * RHS(x) holds input value x to the assembly sequence.
+ * LHS(x) holds the output value x from the assembly sequence.
+ * u.blob holds the string of assembly instructions.
  */
 
-/* Hard operations that I don't know if they are worth supporting */
 #define OP_DEREF     65
 /* OP_DEREF generates an lvalue from a pointer.
- * triple->left holds the pointer value.
+ * RHS(0) holds the pointer value.
  * OP_DEREF serves as a place holder to indicate all necessary
  * checks have been done to indicate a value is an lvalue.
  */
 #define OP_DOT       66
-
+/* OP_DOT references a submember of a structure lvalue.
+ * RHS(0) holds the lvalue.
+ * ->u.field holds the name of the field we want.
+ *
+ * Not seen outside of expressions.
+ */
 #define OP_VAL       67
 /* OP_VAL returns the value of a subexpression of the current expression.
  * Useful for operators that have side effects.
- * triple->left holds the expression.
- * triple->right holds the subexpression of triple->left that is the
+ * RHS(0) holds the expression.
+ * MISC(0) holds the subexpression of RHS(0) that is the
  * value of the expression.
  *
  * Not seen outside of expressions.
  */
 #define OP_LAND      68
-/* OP_LAND performs a C logical and between triple->left and triple->right.
+/* OP_LAND performs a C logical and between RHS(0) and RHS(1).
  * Not seen outside of expressions.
  */
 #define OP_LOR       69
-/* OP_LOR performs a C logical or between triple->left and triple->right.
+/* OP_LOR performs a C logical or between RHS(0) and RHS(1).
  * Not seen outside of expressions.
  */
 #define OP_COND      70
 /* OP_CODE performas a C ? : operation. 
- * triple->left holds the test.
- * triple->right holds an OP_PRODUCT triple.
- * triple->right->left holds the expression to evaluate if
- *     the test returns true.
- * triple->right->right holds the expression to evaluate if
- *     the test returns false.
+ * RHS(0) holds the test.
+ * RHS(1) holds the expression to evaluate if the test returns true.
+ * RHS(2) holds the expression to evaluate if the test returns false.
  * Not seen outside of expressions.
  */
 #define OP_COMMA     71
 /* OP_COMMA performacs a C comma operation.
- * That is triple->left is evaluated, then triple->right
- * and the value of triple->right is returned.
+ * That is RHS(0) is evaluated, then RHS(1)
+ * and the value of RHS(1) is returned.
  * Not seen outside of expressions.
  */
 
 #define OP_CALL      72
 /* OP_CALL performs a procedure call. 
- * triple->left holda a pointer to the OP_LIST of a function
- * triple->right holds a pointer either a single argument
- *    or a list of arguments.  The list is formed by inserting
- *    OP_PRODUCT triples inbetween the argument values.
+ * MISC(0) holds a pointer to the OP_LIST of a function
+ * RHS(x) holds argument x of a function
+ * 
  * Currently not seen outside of expressions.
  */
-#define OP_PRODUCT   73
-/* OP_PRODUCT is a utility triple, both triple->left and triple->right
- * are used.  Other opcodes OP_CALL, and OP_COND use it increase
- * the number of triple pointers in a triple.
- * Currently Not seen outside of expressions.
+#define OP_VAL_VEC   74
+/* OP_VAL_VEC is an array of triples that are either variable
+ * or values for a structure or an array.
+ * RHS(x) holds element x of the vector.
+ * triple->type->elements holds the size of the vector.
  */
 
 /* statements */
 #define OP_LIST      80
 /* OP_LIST Holds a list of statements, and a result value.
- * triple->left holds the list of statements.
- * triple->right holds the value of the statements.
- * triple->right must be the last statement in the list.
+ * RHS(0) holds the list of statements.
+ * MISC(0) holds the value of the statements.
  */
 
 #define OP_BRANCH    81 /* branch */
 /* For branch instructions
- * triple->left holds the branch target.
- * triple->right holds the branch condition.
- * triple->next holds where to branch to if the branch is not taken.
+ * TARG(0) holds the branch target.
+ * RHS(0) if present holds the branch condition.
+ * ->next holds where to branch to if the branch is not taken.
  * The branch target can only be a decl...
  */
 
 #define OP_LABEL     83
 /* OP_LABEL is a triple that establishes an target for branches.
- * triple->use is the list of all branches that use this label.
+ * ->use is the list of all branches that use this label.
  */
 
 #define OP_ADECL     84 
 /* OP_DECL is a triple that establishes an lvalue for assignments.
- * triple->use is a list of statements that use the variable.
+ * ->use is a list of statements that use the variable.
  */
 
 #define OP_SDECL     85
-/* OP_VAR is a triple that establishes a variable of static
+/* OP_SDECL is a triple that establishes a variable of static
  * storage duration.
- * triple->use is a list of statements that use the variable.
- * triple->left holds the initializer expression.
+ * ->use is a list of statements that use the variable.
+ * MISC(0) holds the initializer expression.
  */
 
 
@@ -426,12 +452,12 @@ struct token {
  * The operation is a cross between OP_DECL and OP_WRITE, which
  * is what OP_PHI is geneared from.
  * 
- * triple->left points to an array of pointers to triple.
- * The size of the array is the number of control paths into the block
+ * RHS(x) points to the value from code path x
+ * The number of RHS entries is the number of control paths into the block
  * in which OP_PHI resides.  The elements of the array point to point
  * to the variables OP_PHI is derived from.
  *
- * triple->right holds a pointer to the original OP_DECL node
+ * MISC(0) holds a pointer to the orginal OP_DECL node.
  */
 
 /* Architecture specific instructions */
@@ -469,117 +495,138 @@ struct token {
 #define OP_OUTL        135
 #define OP_BSF         136
 #define OP_BSR         137
-#warning "FIXME implement rdmsr wrmsr"
-#if 0
-/* I need to implement these but, I need to implment > 32bit return
- * values first.
- */
 #define OP_RDMSR       138
 #define OP_WRMSR       139
-#endif
 #define OP_HLT         140
 
-static const char *table_ops[] = {
-[OP_SMUL       ] = "smul",
-[OP_UMUL       ] = "umul",
-[OP_SDIV       ] = "sdiv",
-[OP_UDIV       ] = "udiv",
-[OP_SMOD       ] = "smod",
-[OP_UMOD       ] = "umod",
-[OP_ADD        ] = "add",
-[OP_SUB        ] = "sub",
-[OP_SL         ] = "sl", 
-[OP_USR        ] = "usr",
-[OP_SSR        ] = "ssr", 
-[OP_AND        ] = "and", 
-[OP_XOR        ] = "xor",
-[OP_OR         ] = "or",
-[OP_POS        ] = "pos",
-[OP_NEG        ] = "neg",
-[OP_INVERT     ] = "invert",
-
-[OP_EQ         ] = "eq",
-[OP_NOTEQ      ] = "noteq",
-[OP_SLESS      ] = "sless",
-[OP_ULESS      ] = "uless",
-[OP_SMORE      ] = "smore",
-[OP_UMORE      ] = "umore",
-[OP_SLESSEQ    ] = "slesseq",
-[OP_ULESSEQ    ] = "ulesseq",
-[OP_SMOREEQ    ] = "smoreeq",
-[OP_UMOREEQ    ] = "umoreeq",
-[OP_LFALSE     ] = "lfalse",
-[OP_LTRUE      ] = "ltrue",
-
-[OP_LOAD       ] = "load",
-[OP_STORE      ] = "store",
-
-[OP_NOOP       ] = "noop", 
-
-[OP_INTCONST   ] = "intconst",
-[OP_BLOBCONST  ] = "blobconst",
-[OP_ADDRCONST  ] = "addrconst",
-
-[OP_WRITE      ] = "write",
-[OP_READ       ] = "read",
-[OP_COPY       ] = "copy",
-[OP_DEREF      ] = "deref",
-[OP_DOT        ] = "dot",
-
-[OP_VAL        ] = "val",
-[OP_LAND       ] = "land",
-[OP_LOR        ] = "lor",
-[OP_COND       ] = "cond",
-[OP_COMMA      ] = "comma",
-[OP_CALL       ] = "call",
-[OP_PRODUCT    ] = "product",
-
-[OP_LIST       ] = "list",
-[OP_BRANCH     ] = "branch",
-[OP_LABEL      ] = "label",
-[OP_ADECL      ] = "adecl",
-[OP_SDECL      ] = "sdecl",
-[OP_PHI        ] = "phi",
-
-[OP_CMP        ] = "cmp",
-[OP_TEST       ] = "test",
-[OP_SET_EQ     ] = "set_eq",
-[OP_SET_NOTEQ  ] = "set_noteq",
-[OP_SET_SLESS  ] = "set_sless",
-[OP_SET_ULESS  ] = "set_uless",
-[OP_SET_SMORE  ] = "set_smore",
-[OP_SET_SMORE  ] = "set_umore",
-[OP_SET_SLESSEQ] = "set_slesseq",
-[OP_SET_ULESSEQ] = "set_ulesseq",
-[OP_SET_SMOREEQ] = "set_smoreq",
-[OP_SET_UMOREEQ] = "set_umoreq",
-[OP_JMP        ] = "jmp",
-[OP_JMP_EQ     ] = "jmp_eq",
-[OP_JMP_NOTEQ  ] = "jmp_noteq",
-[OP_JMP_SLESS  ] = "jmp_sless",
-[OP_JMP_ULESS  ] = "jmp_uless",
-[OP_JMP_SMORE  ] = "jmp_smore",
-[OP_JMP_SMORE  ] = "jmp_umore",
-[OP_JMP_SLESSEQ] = "jmp_slesseq",
-[OP_JMP_ULESSEQ] = "jmp_ulesseq",
-[OP_JMP_SMOREEQ] = "jmp_smoreq",
-[OP_JMP_UMOREEQ] = "jmp_umoreq",
-
-[OP_INB        ] = "__inb",
-[OP_INW        ] = "__inw",
-[OP_INL        ] = "__inl",
-[OP_OUTB       ] = "__outb",
-[OP_OUTW       ] = "__outw",
-[OP_OUTL       ] = "__outl",
-[OP_BSF        ] = "__bsf",
-[OP_BSR        ] = "__bsr",
-[OP_HLT        ] = "__hlt",
-
+struct op_info {
+       const char *name;
+       unsigned flags;
+#define PURE   1
+#define IMPURE 2
+#define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
+#define DEF    4
+#define BLOCK  8 /* Triple stores the current block */
+       unsigned char lhs, rhs, misc, targ;
 };
 
+#define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \
+       .name = (NAME), \
+       .flags = (FLAGS), \
+       .lhs = (LHS), \
+       .rhs = (RHS), \
+       .misc = (MISC), \
+       .targ = (TARG), \
+        }
+static const struct op_info table_ops[] = {
+[OP_SDIVT      ] = OP( 2,  2, 0, 0, PURE | BLOCK , "sdivt"),
+[OP_UDIVT      ] = OP( 2,  2, 0, 0, PURE | BLOCK , "udivt"),
+[OP_SMUL       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smul"),
+[OP_UMUL       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umul"),
+[OP_SDIV       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sdiv"),
+[OP_UDIV       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "udiv"),
+[OP_SMOD       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smod"),
+[OP_UMOD       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umod"),
+[OP_ADD        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "add"),
+[OP_SUB        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sub"),
+[OP_SL         ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sl"),
+[OP_USR        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "usr"),
+[OP_SSR        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "ssr"),
+[OP_AND        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "and"),
+[OP_XOR        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "xor"),
+[OP_OR         ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "or"),
+[OP_POS        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "pos"),
+[OP_NEG        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "neg"),
+[OP_INVERT     ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "invert"),
+
+[OP_EQ         ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "eq"),
+[OP_NOTEQ      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "noteq"),
+[OP_SLESS      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sless"),
+[OP_ULESS      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "uless"),
+[OP_SMORE      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smore"),
+[OP_UMORE      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umore"),
+[OP_SLESSEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "slesseq"),
+[OP_ULESSEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "ulesseq"),
+[OP_SMOREEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smoreeq"),
+[OP_UMOREEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umoreeq"),
+[OP_LFALSE     ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "lfalse"),
+[OP_LTRUE      ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "ltrue"),
+
+[OP_LOAD       ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "load"),
+[OP_STORE      ] = OP( 0,  2, 0, 0, IMPURE | BLOCK , "store"),
+
+[OP_NOOP       ] = OP( 0,  0, 0, 0, PURE | BLOCK, "noop"),
+
+[OP_INTCONST   ] = OP( 0,  0, 0, 0, PURE | DEF, "intconst"),
+[OP_BLOBCONST  ] = OP( 0,  0, 0, 0, PURE, "blobconst"),
+[OP_ADDRCONST  ] = OP( 0,  0, 1, 0, PURE | DEF, "addrconst"),
+
+[OP_WRITE      ] = OP( 0,  2, 0, 0, PURE | BLOCK, "write"),
+[OP_READ       ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "read"),
+[OP_COPY       ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "copy"),
+[OP_PIECE      ] = OP( 0,  0, 1, 0, PURE | DEF, "piece"),
+[OP_ASM        ] = OP(-1, -1, 0, 0, IMPURE, "asm"),
+[OP_DEREF      ] = OP( 0,  1, 0, 0, 0 | DEF | BLOCK, "deref"), 
+[OP_DOT        ] = OP( 0,  1, 0, 0, 0 | DEF | BLOCK, "dot"),
+
+[OP_VAL        ] = OP( 0,  1, 1, 0, 0 | DEF | BLOCK, "val"),
+[OP_LAND       ] = OP( 0,  2, 0, 0, 0 | DEF | BLOCK, "land"),
+[OP_LOR        ] = OP( 0,  2, 0, 0, 0 | DEF | BLOCK, "lor"),
+[OP_COND       ] = OP( 0,  3, 0, 0, 0 | DEF | BLOCK, "cond"),
+[OP_COMMA      ] = OP( 0,  2, 0, 0, 0 | DEF | BLOCK, "comma"),
+/* Call is special most it can stand in for anything so it depends on context */
+[OP_CALL       ] = OP(-1, -1, 1, 0, 0 | BLOCK, "call"),
+/* The sizes of OP_CALL and OP_VAL_VEC depend upon context */
+[OP_VAL_VEC    ] = OP( 0, -1, 0, 0, 0 | BLOCK, "valvec"),
+
+[OP_LIST       ] = OP( 0,  1, 1, 0, 0 | DEF, "list"),
+/* The number of targets for OP_BRANCH depends on context */
+[OP_BRANCH     ] = OP( 0, -1, 0, 1, PURE | BLOCK, "branch"),
+[OP_LABEL      ] = OP( 0,  0, 0, 0, PURE | BLOCK, "label"),
+[OP_ADECL      ] = OP( 0,  0, 0, 0, PURE | BLOCK, "adecl"),
+[OP_SDECL      ] = OP( 0,  0, 1, 0, PURE | BLOCK, "sdecl"),
+/* The number of RHS elements of OP_PHI depend upon context */
+[OP_PHI        ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
+
+[OP_CMP        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK, "cmp"),
+[OP_TEST       ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "test"),
+[OP_SET_EQ     ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_eq"),
+[OP_SET_NOTEQ  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_noteq"),
+[OP_SET_SLESS  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_sless"),
+[OP_SET_ULESS  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_uless"),
+[OP_SET_SMORE  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_smore"),
+[OP_SET_UMORE  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_umore"),
+[OP_SET_SLESSEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_slesseq"),
+[OP_SET_ULESSEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_ulesseq"),
+[OP_SET_SMOREEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_smoreq"),
+[OP_SET_UMOREEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_umoreq"),
+[OP_JMP        ] = OP( 0,  0, 0, 1, PURE | BLOCK, "jmp"),
+[OP_JMP_EQ     ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_eq"),
+[OP_JMP_NOTEQ  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_noteq"),
+[OP_JMP_SLESS  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_sless"),
+[OP_JMP_ULESS  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_uless"),
+[OP_JMP_SMORE  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_smore"),
+[OP_JMP_UMORE  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_umore"),
+[OP_JMP_SLESSEQ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_slesseq"),
+[OP_JMP_ULESSEQ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_ulesseq"),
+[OP_JMP_SMOREEQ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_smoreq"),
+[OP_JMP_UMOREEQ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_umoreq"),
+
+[OP_INB        ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "__inb"),
+[OP_INW        ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "__inw"),
+[OP_INL        ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "__inl"),
+[OP_OUTB       ] = OP( 0,  2, 0, 0, IMPURE| BLOCK, "__outb"),
+[OP_OUTW       ] = OP( 0,  2, 0, 0, IMPURE| BLOCK, "__outw"),
+[OP_OUTL       ] = OP( 0,  2, 0, 0, IMPURE| BLOCK, "__outl"),
+[OP_BSF        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "__bsf"),
+[OP_BSR        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "__bsr"),
+[OP_RDMSR      ] = OP( 2,  1, 0, 0, IMPURE | BLOCK, "__rdmsr"),
+[OP_WRMSR      ] = OP( 0,  3, 0, 0, IMPURE | BLOCK, "__wrmsr"),
+[OP_HLT        ] = OP( 0,  0, 0, 0, IMPURE | BLOCK, "__hlt"),
+};
+#undef OP
 #define OP_MAX      (sizeof(table_ops)/sizeof(table_ops[0]))
 
-
 static const char *tops(int index) 
 {
        static const char unknown[] = "unknown op";
@@ -589,11 +636,10 @@ static const char *tops(int index)
        if (index > OP_MAX) {
                return unknown;
        }
-       return table_ops[index];
+       return table_ops[index].name;
 }
 
-#warning "FIXME Finish defining struct type"
-
+struct asm_info;
 struct triple;
 struct block;
 struct triple_set {
@@ -601,22 +647,74 @@ struct triple_set {
        struct triple *member;
 };
 
+#define MAX_LHS  15
+#define MAX_RHS  250
+#define MAX_MISC 3
+#define MAX_TARG 3
+
+struct occurance {
+       int count;
+       const char *filename;
+       const char *function;
+       int line;
+       int col;
+       struct occurance *parent;
+};
 struct triple {
        struct triple *next, *prev;
        struct triple_set *use;
        struct type *type;
-       int op;
+       unsigned char op;
+       unsigned char template_id;
+       unsigned short sizes;
+#define TRIPLE_LHS(SIZES)  (((SIZES) >>  0) & 0x0f)
+#define TRIPLE_RHS(SIZES)  (((SIZES) >>  4) & 0xff)
+#define TRIPLE_MISC(SIZES) (((SIZES) >> 12) & 0x03)
+#define TRIPLE_TARG(SIZES) (((SIZES) >> 14) & 0x03)
+#define TRIPLE_SIZE(SIZES) \
+       (TRIPLE_LHS(SIZES)  + \
+        TRIPLE_RHS(SIZES)  + \
+        TRIPLE_MISC(SIZES) + \
+        TRIPLE_TARG(SIZES))
+#define TRIPLE_SIZES(LHS, RHS, MISC, TARG) \
+       ((((LHS) & 0x0f) <<  0) | \
+       (((RHS)  & 0xff) <<  4) | \
+       (((MISC) & 0x03) << 12) | \
+       (((TARG) & 0x03) << 14))
+#define TRIPLE_LHS_OFF(SIZES)  (0)
+#define TRIPLE_RHS_OFF(SIZES)  (TRIPLE_LHS_OFF(SIZES) + TRIPLE_LHS(SIZES))
+#define TRIPLE_MISC_OFF(SIZES) (TRIPLE_RHS_OFF(SIZES) + TRIPLE_RHS(SIZES))
+#define TRIPLE_TARG_OFF(SIZES) (TRIPLE_MISC_OFF(SIZES) + TRIPLE_MISC(SIZES))
+#define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF((PTR)->sizes) + (INDEX)])
+#define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF((PTR)->sizes) + (INDEX)])
+#define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF((PTR)->sizes) + (INDEX)])
+#define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF((PTR)->sizes) + (INDEX)])
        unsigned id; /* A scratch value and finally the register */
-       struct triple *left;
-       struct triple *right;
+#define TRIPLE_FLAG_FLATTENED   (1 << 31)
+#define TRIPLE_FLAG_PRE_SPLIT   (1 << 30)
+#define TRIPLE_FLAG_POST_SPLIT  (1 << 29)
+       struct occurance *occurance;
        union {
                ulong_t cval;
                struct block  *block;
                void *blob;
+               struct hash_entry *field;
+               struct asm_info *ainfo;
        } u;
-       const char *filename;
-       int line;
-       int col;
+       struct triple *param[2];
+};
+
+struct reg_info {
+       unsigned reg;
+       unsigned regcm;
+};
+struct ins_template {
+       struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
+};
+
+struct asm_info {
+       struct ins_template tmpl;
+       char *str;
 };
 
 struct block_set {
@@ -667,8 +765,12 @@ struct hash_entry {
 #define HASH_TABLE_SIZE 2048
 
 struct compile_state {
-       struct triple *vars;
+       const char *label_prefix;
+       const char *ofilename;
+       FILE *output;
        struct file_state *file;
+       struct occurance *last_occurance;
+       const char *function;
        struct token token[4];
        struct hash_entry *hash_table[HASH_TABLE_SIZE];
        struct hash_entry *i_continue;
@@ -680,29 +782,131 @@ struct compile_state {
        struct triple *main_function;
        struct block *first_block, *last_block;
        int last_vertex;
+       int cpu;
        int debug;
        int optimize;
 };
 
+/* visibility global/local */
+/* static/auto duration */
+/* typedef, register, inline */
+#define STOR_SHIFT         0
+#define STOR_MASK     0x000f
+/* Visibility */
+#define STOR_GLOBAL   0x0001
+/* Duration */
+#define STOR_PERM     0x0002
+/* Storage specifiers */
+#define STOR_AUTO     0x0000
+#define STOR_STATIC   0x0002
+#define STOR_EXTERN   0x0003
+#define STOR_REGISTER 0x0004
+#define STOR_TYPEDEF  0x0008
+#define STOR_INLINE   0x000c
+
+#define QUAL_SHIFT         4
+#define QUAL_MASK     0x0070
+#define QUAL_NONE     0x0000
+#define QUAL_CONST    0x0010
+#define QUAL_VOLATILE 0x0020
+#define QUAL_RESTRICT 0x0040
+
+#define TYPE_SHIFT         8
+#define TYPE_MASK     0x1f00
+#define TYPE_INTEGER(TYPE)    (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG))
+#define TYPE_ARITHMETIC(TYPE) (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE))
+#define TYPE_UNSIGNED(TYPE)   ((TYPE) & 0x0100)
+#define TYPE_SIGNED(TYPE)     (!TYPE_UNSIGNED(TYPE))
+#define TYPE_MKUNSIGNED(TYPE) ((TYPE) | 0x0100)
+#define TYPE_RANK(TYPE)       ((TYPE) & ~0x0100)
+#define TYPE_PTR(TYPE)        (((TYPE) & TYPE_MASK) == TYPE_POINTER)
+#define TYPE_DEFAULT  0x0000
+#define TYPE_VOID     0x0100
+#define TYPE_CHAR     0x0200
+#define TYPE_UCHAR    0x0300
+#define TYPE_SHORT    0x0400
+#define TYPE_USHORT   0x0500
+#define TYPE_INT      0x0600
+#define TYPE_UINT     0x0700
+#define TYPE_LONG     0x0800
+#define TYPE_ULONG    0x0900
+#define TYPE_LLONG    0x0a00 /* long long */
+#define TYPE_ULLONG   0x0b00
+#define TYPE_FLOAT    0x0c00
+#define TYPE_DOUBLE   0x0d00
+#define TYPE_LDOUBLE  0x0e00 /* long double */
+#define TYPE_STRUCT   0x1000
+#define TYPE_ENUM     0x1100
+#define TYPE_POINTER  0x1200 
+/* For TYPE_POINTER:
+ * type->left holds the type pointed to.
+ */
+#define TYPE_FUNCTION 0x1300 
+/* For TYPE_FUNCTION:
+ * type->left holds the return type.
+ * type->right holds the...
+ */
+#define TYPE_PRODUCT  0x1400
+/* TYPE_PRODUCT is a basic building block when defining structures
+ * type->left holds the type that appears first in memory.
+ * type->right holds the type that appears next in memory.
+ */
+#define TYPE_OVERLAP  0x1500
+/* TYPE_OVERLAP is a basic building block when defining unions
+ * type->left and type->right holds to types that overlap
+ * each other in memory.
+ */
+#define TYPE_ARRAY    0x1600
+/* TYPE_ARRAY is a basic building block when definitng arrays.
+ * type->left holds the type we are an array of.
+ * type-> holds the number of elements.
+ */
+
+#define ELEMENT_COUNT_UNSPECIFIED (~0UL)
+
+struct type {
+       unsigned int type;
+       struct type *left, *right;
+       ulong_t elements;
+       struct hash_entry *field_ident;
+       struct hash_entry *type_ident;
+};
+
 #define MAX_REGISTERS      75
 #define MAX_REG_EQUIVS     16
-#define MAX_REGC           12
+#define REGISTER_BITS      16
+#define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
+#define TEMPLATE_BITS      7
+#define MAX_TEMPLATES      (1<<TEMPLATE_BITS)
+#define MAX_REGC           14
 #define REG_UNSET          0
+#define REG_UNNEEDED       1
+#define REG_VIRT0          (MAX_REGISTERS + 0)
+#define REG_VIRT1          (MAX_REGISTERS + 1)
+#define REG_VIRT2          (MAX_REGISTERS + 2)
+#define REG_VIRT3          (MAX_REGISTERS + 3)
+#define REG_VIRT4          (MAX_REGISTERS + 4)
+#define REG_VIRT5          (MAX_REGISTERS + 5)
+#define REG_VIRT6          (MAX_REGISTERS + 5)
+#define REG_VIRT7          (MAX_REGISTERS + 5)
+#define REG_VIRT8          (MAX_REGISTERS + 5)
+#define REG_VIRT9          (MAX_REGISTERS + 5)
 
 /* Provision for 8 register classes */
-#define REGC_MASK ((1 << MAX_REGC) - 1)
-#define ID_REG_CLASSES(ID)      ((ID) & REGC_MASK)
-#define ID_REG(ID)              ((ID) >> MAX_REGC)
-#define MK_REG_ID(REG, CLASSES) (((REG) << MAX_REGC) | ((CLASSES) & REGC_MASK))
-
-static unsigned alloc_virtual_reg(void)
-{
-       static unsigned virtual_reg = MAX_REGISTERS;
-       virtual_reg += 1;
-       return virtual_reg;
-}
+#define REG_SHIFT  0
+#define REGC_SHIFT REGISTER_BITS
+#define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
+#define REG_MASK (MAX_VIRT_REGISTERS -1)
+#define ID_REG(ID)              ((ID) & REG_MASK)
+#define SET_REG(ID, REG)        ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
+#define ID_REGCM(ID)           (((ID) & REGC_MASK) >> REGC_SHIFT)
+#define SET_REGCM(ID, REGCM)   ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
+#define SET_INFO(ID, INFO)     ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
+               (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
 
 static unsigned arch_reg_regcm(struct compile_state *state, int reg);
+static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
+static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm);
 static void arch_reg_equivs(
        struct compile_state *state, unsigned *equiv, int reg);
 static int arch_select_free_register(
@@ -711,20 +915,43 @@ static unsigned arch_regc_size(struct compile_state *state, int class);
 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
 static const char *arch_reg_str(int reg);
+static struct reg_info arch_reg_constraint(
+       struct compile_state *state, struct type *type, const char *constraint);
+static struct reg_info arch_reg_clobber(
+       struct compile_state *state, const char *clobber);
+static struct reg_info arch_reg_lhs(struct compile_state *state, 
+       struct triple *ins, int index);
+static struct reg_info arch_reg_rhs(struct compile_state *state, 
+       struct triple *ins, int index);
+static struct triple *transform_to_arch_instruction(
+       struct compile_state *state, struct triple *ins);
 
-#define DEBUG_INTERMEDIATE_CODE 0x0001
-#define DEBUG_CONTROL_FLOW      0x0002
-#define DEBUG_BASIC_BLOCKS      0x0004
-#define DEBUG_FDOMINATORS       0x0008
-#define DEBUG_RDOMINATORS       0x0010
-#define DEBUG_TRIPLES           0x0020
-#define DEBUG_INTERFERENCE      0x0040
-#define DEBUG_ARCH_CODE         0x0080
-#define DEBUG_CODE_ELIMINATION  0x0100
 
-#define GLOBAL_SCOPE_DEPTH 1
 
-static void compile_file(struct compile_state *old_state, char *filename, int local);
+#define DEBUG_ABORT_ON_ERROR    0x0001
+#define DEBUG_INTERMEDIATE_CODE 0x0002
+#define DEBUG_CONTROL_FLOW      0x0004
+#define DEBUG_BASIC_BLOCKS      0x0008
+#define DEBUG_FDOMINATORS       0x0010
+#define DEBUG_RDOMINATORS       0x0020
+#define DEBUG_TRIPLES           0x0040
+#define DEBUG_INTERFERENCE      0x0080
+#define DEBUG_ARCH_CODE         0x0100
+#define DEBUG_CODE_ELIMINATION  0x0200
+#define DEBUG_INSERTED_COPIES   0x0400
+
+#define GLOBAL_SCOPE_DEPTH   1
+#define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
+
+static void compile_file(struct compile_state *old_state, const char *filename, int local);
+
+static void do_cleanup(struct compile_state *state)
+{
+       if (state->output) {
+               fclose(state->output);
+               unlink(state->ofilename);
+       }
+}
 
 static int get_col(struct file_state *file)
 {
@@ -746,9 +973,14 @@ static int get_col(struct file_state *file)
 static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
 {
        int col;
-       if (triple) {
+       if (triple && triple->occurance) {
+               struct occurance *spot;
+               spot = triple->occurance;
+               while(spot->parent) {
+                       spot = spot->parent;
+               }
                fprintf(fp, "%s:%d.%d: ", 
-                       triple->filename, triple->line, triple->col);
+                       spot->filename, spot->line, spot->col);
                return;
        }
        if (!state->file) {
@@ -756,7 +988,7 @@ static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
        }
        col = get_col(state->file);
        fprintf(fp, "%s:%d.%d: ", 
-               state->file->basename, state->file->line, col);
+               state->file->report_name, state->file->report_line, col);
 }
 
 static void __internal_error(struct compile_state *state, struct triple *ptr, 
@@ -765,10 +997,14 @@ static void __internal_error(struct compile_state *state, struct triple *ptr,
        va_list args;
        va_start(args, fmt);
        loc(stderr, state, ptr);
+       if (ptr) {
+               fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
+       }
        fprintf(stderr, "Internal compiler error: ");
        vfprintf(stderr, fmt, args);
        fprintf(stderr, "\n");
        va_end(args);
+       do_cleanup(state);
        abort();
 }
 
@@ -796,6 +1032,10 @@ static void __error(struct compile_state *state, struct triple *ptr,
        vfprintf(stderr, fmt, args);
        va_end(args);
        fprintf(stderr, "\n");
+       do_cleanup(state);
+       if (state->debug & DEBUG_ABORT_ON_ERROR) {
+               abort();
+       }
        exit(1);
 }
 
@@ -824,18 +1064,22 @@ static void __warning(struct compile_state *state, struct triple *ptr,
 #endif
 #define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
 
-
-static void valid_op(struct compile_state *state, struct triple *ptr)
+static void valid_op(struct compile_state *state, int op)
 {
        char *fmt = "invalid op: %d";
-       if (ptr->op >= OP_MAX) {
-               internal_error(state, 0, fmt,   ptr->op);
+       if (op >= OP_MAX) {
+               internal_error(state, 0, fmt, op);
        }
-       if (ptr->op < 0) {
-               internal_error(state, 0, fmt,   ptr->op);
+       if (op < 0) {
+               internal_error(state, 0, fmt, op);
        }
 }
 
+static void valid_ins(struct compile_state *state, struct triple *ptr)
+{
+       valid_op(state, ptr->op);
+}
+
 static void process_trigraphs(struct compile_state *state)
 {
        char *src, *dest, *end;
@@ -924,6 +1168,9 @@ static void use_triple(struct triple *used, struct triple *user)
 static void unuse_triple(struct triple *used, struct triple *unuser)
 {
        struct triple_set *use, **ptr;
+       if (!used) {
+               return;
+       }
        ptr = &used->use;
        while(*ptr) {
                use = *ptr;
@@ -971,39 +1218,241 @@ static void pop_triple(struct triple *used, struct triple *unuser)
        }
 }
 
+static void put_occurance(struct occurance *occurance)
+{
+       occurance->count -= 1;
+       if (occurance->count <= 0) {
+               if (occurance->parent) {
+                       put_occurance(occurance->parent);
+               }
+               xfree(occurance);
+       }
+}
+
+static void get_occurance(struct occurance *occurance)
+{
+       occurance->count += 1;
+}
+
+
+static struct occurance *new_occurance(struct compile_state *state)
+{
+       struct occurance *result, *last;
+       const char *filename;
+       const char *function;
+       int line, col;
+
+       function = "";
+       filename = 0;
+       line = 0;
+       col  = 0;
+       if (state->file) {
+               filename = state->file->report_name;
+               line     = state->file->report_line;
+               col      = get_col(state->file);
+       }
+       if (state->function) {
+               function = state->function;
+       }
+       last = state->last_occurance;
+       if (last &&
+               (last->col == col) &&
+               (last->line == line) &&
+               (last->function == function) &&
+               (strcmp(last->filename, filename) == 0)) {
+               get_occurance(last);
+               return last;
+       }
+       if (last) {
+               state->last_occurance = 0;
+               put_occurance(last);
+       }
+       result = xmalloc(sizeof(*result), "occurance");
+       result->count    = 2;
+       result->filename = filename;
+       result->function = function;
+       result->line     = line;
+       result->col      = col;
+       result->parent   = 0;
+       state->last_occurance = result;
+       return result;
+}
+
+static struct occurance *inline_occurance(struct compile_state *state,
+       struct occurance *new, struct occurance *orig)
+{
+       struct occurance *result, *last;
+       last = state->last_occurance;
+       if (last &&
+               (last->parent   == orig) &&
+               (last->col      == new->col) &&
+               (last->line     == new->line) &&
+               (last->function == new->function) &&
+               (last->filename == new->filename)) {
+               get_occurance(last);
+               return last;
+       }
+       if (last) {
+               state->last_occurance = 0;
+               put_occurance(last);
+       }
+       get_occurance(orig);
+       result = xmalloc(sizeof(*result), "occurance");
+       result->count    = 2;
+       result->filename = new->filename;
+       result->function = new->function;
+       result->line     = new->line;
+       result->col      = new->col;
+       result->parent   = orig;
+       state->last_occurance = result;
+       return result;
+}
+       
+
+static struct occurance dummy_occurance = {
+       .count    = 2,
+       .filename = __FILE__,
+       .function = "",
+       .line     = __LINE__,
+       .col      = 0,
+       .parent   = 0,
+};
 
 /* The zero triple is used as a place holder when we are removing pointers
  * from a triple.  Having allows certain sanity checks to pass even
  * when the original triple that was pointed to is gone.
  */
 static struct triple zero_triple = {
-       .next     = &zero_triple,
-       .prev     = &zero_triple,
-       .use      = 0,
-       .op       = OP_INTCONST,
-       .id       = -1, /* An invalid id */
-       .left     = 0,
-       .right    = 0,
-       .u = { .cval   = 0, },
-       .filename = __FILE__,
-       .line     = __LINE__,
+       .next      = &zero_triple,
+       .prev      = &zero_triple,
+       .use       = 0,
+       .op        = OP_INTCONST,
+       .sizes     = TRIPLE_SIZES(0, 0, 0, 0),
+       .id        = -1, /* An invalid id */
+       .u = { .cval = 0, },
+       .occurance = &dummy_occurance,
+       .param = { [0] = 0, [1] = 0, },
 };
 
+
+static unsigned short triple_sizes(struct compile_state *state,
+       int op, struct type *type, int lhs_wanted, int rhs_wanted,
+       struct occurance *occurance)
+{
+       int lhs, rhs, misc, targ;
+       struct triple dummy;
+       dummy.op = op;
+       dummy.occurance = occurance;
+       valid_op(state, op);
+       lhs = table_ops[op].lhs;
+       rhs = table_ops[op].rhs;
+       misc = table_ops[op].misc;
+       targ = table_ops[op].targ;
+       
+       
+       if (op == OP_CALL) {
+               struct type *param;
+               rhs = 0;
+               param = type->right;
+               while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
+                       rhs++;
+                       param = param->right;
+               }
+               if ((param->type & TYPE_MASK) != TYPE_VOID) {
+                       rhs++;
+               }
+               lhs = 0;
+               if ((type->left->type & TYPE_MASK) == TYPE_STRUCT) {
+                       lhs = type->left->elements;
+               }
+       }
+       else if (op == OP_VAL_VEC) {
+               rhs = type->elements;
+       }
+       else if ((op == OP_BRANCH) || (op == OP_PHI)) {
+               rhs = rhs_wanted;
+       }
+       else if (op == OP_ASM) {
+               rhs = rhs_wanted;
+               lhs = lhs_wanted;
+       }
+       if ((rhs < 0) || (rhs > MAX_RHS)) {
+               internal_error(state, &dummy, "bad rhs %d", rhs);
+       }
+       if ((lhs < 0) || (lhs > MAX_LHS)) {
+               internal_error(state, &dummy, "bad lhs");
+       }
+       if ((misc < 0) || (misc > MAX_MISC)) {
+               internal_error(state, &dummy, "bad misc");
+       }
+       if ((targ < 0) || (targ > MAX_TARG)) {
+               internal_error(state, &dummy, "bad targs");
+       }
+       return TRIPLE_SIZES(lhs, rhs, misc, targ);
+}
+
+static struct triple *alloc_triple(struct compile_state *state, 
+       int op, struct type *type, int lhs, int rhs,
+       struct occurance *occurance)
+{
+       size_t size, sizes, extra_count, min_count;
+       struct triple *ret;
+       sizes = triple_sizes(state, op, type, lhs, rhs, occurance);
+
+       min_count = sizeof(ret->param)/sizeof(ret->param[0]);
+       extra_count = TRIPLE_SIZE(sizes);
+       extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
+
+       size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
+       ret = xcmalloc(size, "tripple");
+       ret->op        = op;
+       ret->sizes     = sizes;
+       ret->type      = type;
+       ret->next      = ret;
+       ret->prev      = ret;
+       ret->occurance = occurance;
+       return ret;
+}
+
+struct triple *dup_triple(struct compile_state *state, struct triple *src)
+{
+       struct triple *dup;
+       int src_lhs, src_rhs, src_size;
+       src_lhs = TRIPLE_LHS(src->sizes);
+       src_rhs = TRIPLE_RHS(src->sizes);
+       src_size = TRIPLE_SIZE(src->sizes);
+       get_occurance(src->occurance);
+       dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
+               src->occurance);
+       memcpy(dup, src, sizeof(*src));
+       memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
+       return dup;
+}
+
+static struct triple *new_triple(struct compile_state *state, 
+       int op, struct type *type, int lhs, int rhs)
+{
+       struct triple *ret;
+       struct occurance *occurance;
+       occurance = new_occurance(state);
+       ret = alloc_triple(state, op, type, lhs, rhs, occurance);
+       return ret;
+}
+
 static struct triple *build_triple(struct compile_state *state, 
        int op, struct type *type, struct triple *left, struct triple *right,
-       const char *filename, int line, int col)
+       struct occurance *occurance)
 {
        struct triple *ret;
-       ret = xcmalloc(sizeof(*ret), "tripple");
-       ret->op       = op;
-       ret->type     = type;
-       ret->left     = left;
-       ret->right    = right;
-       ret->next     = ret;
-       ret->prev     = ret;
-       ret->filename = filename;
-       ret->line     = line;
-       ret->col      = col;
+       size_t count;
+       ret = alloc_triple(state, op, type, -1, -1, occurance);
+       count = TRIPLE_SIZE(ret->sizes);
+       if (count > 0) {
+               ret->param[0] = left;
+       }
+       if (count > 1) {
+               ret->param[1] = right;
+       }
        return ret;
 }
 
@@ -1011,55 +1460,103 @@ static struct triple *triple(struct compile_state *state,
        int op, struct type *type, struct triple *left, struct triple *right)
 {
        struct triple *ret;
-       const char *filename;
-       int line, col;
-       filename = 0;
-       line = 0;
-       col  = 0;
-       if (state->file) {
-               filename = state->file->basename;
-               line     = state->file->line;
-               col      = get_col(state->file);
+       size_t count;
+       ret = new_triple(state, op, type, -1, -1);
+       count = TRIPLE_SIZE(ret->sizes);
+       if (count >= 1) {
+               ret->param[0] = left;
+       }
+       if (count >= 2) {
+               ret->param[1] = right;
+       }
+       return ret;
+}
+
+static struct triple *branch(struct compile_state *state, 
+       struct triple *targ, struct triple *test)
+{
+       struct triple *ret;
+       ret = new_triple(state, OP_BRANCH, &void_type, -1, test?1:0);
+       if (test) {
+               RHS(ret, 0) = test;
        }
-       ret = build_triple(state, op, type, left, right, filename, line, col);
+       TARG(ret, 0) = targ;
        /* record the branch target was used */
-       if (ret->op == OP_BRANCH) {
-               if (!left || (left->op != OP_LABEL)) {
-                       internal_error(state, 0, "branch not to label");
-               }
-               use_triple(left, ret);
+       if (!targ || (targ->op != OP_LABEL)) {
+               internal_error(state, 0, "branch not to label");
+               use_triple(targ, ret);
        }
        return ret;
 }
 
+
 static void insert_triple(struct compile_state *state,
        struct triple *first, struct triple *ptr)
 {
        if (ptr) {
-               if (ptr->next != ptr) {
+               if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
                        internal_error(state, ptr, "expression already used");
                }
                ptr->next       = first;
                ptr->prev       = first->prev;
                ptr->prev->next = ptr;
                ptr->next->prev = ptr;
-               if ((ptr->prev->op == OP_BRANCH) && (ptr->prev->right)) {
+               if ((ptr->prev->op == OP_BRANCH) && 
+                       TRIPLE_RHS(ptr->prev->sizes)) {
                        unuse_triple(first, ptr->prev);
                        use_triple(ptr, ptr->prev);
                }
        }
 }
 
+static int triple_stores_block(struct compile_state *state, struct triple *ins)
+{
+       /* This function is used to determine if u.block 
+        * is utilized to store the current block number.
+        */
+       int stores_block;
+       valid_ins(state, ins);
+       stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
+       return stores_block;
+}
+
+static struct block *block_of_triple(struct compile_state *state, 
+       struct triple *ins)
+{
+       struct triple *first;
+       first = RHS(state->main_function, 0);
+       while(ins != first && !triple_stores_block(state, ins)) {
+               if (ins == ins->prev) {
+                       internal_error(state, 0, "ins == ins->prev?");
+               }
+               ins = ins->prev;
+       }
+       if (!triple_stores_block(state, ins)) {
+               internal_error(state, ins, "Cannot find block");
+       }
+       return ins->u.block;
+}
+
 static struct triple *pre_triple(struct compile_state *state,
        struct triple *base,
        int op, struct type *type, struct triple *left, struct triple *right)
 {
-       /* Careful this assumes it can do the easy thing to get the block */
+       struct block *block;
        struct triple *ret;
-       ret = build_triple(state, op, type, left, right, 
-               base->filename, base->line, base->col);
-       ret->u.block = base->u.block;
+       /* If I am an OP_PIECE jump to the real instruction */
+       if (base->op == OP_PIECE) {
+               base = MISC(base, 0);
+       }
+       block = block_of_triple(state, base);
+       get_occurance(base->occurance);
+       ret = build_triple(state, op, type, left, right, base->occurance);
+       if (triple_stores_block(state, ret)) {
+               ret->u.block = block;
+       }
        insert_triple(state, base, ret);
+       if (block->first == base) {
+               block->first = ret;
+       }
        return ret;
 }
 
@@ -1067,12 +1564,29 @@ static struct triple *post_triple(struct compile_state *state,
        struct triple *base,
        int op, struct type *type, struct triple *left, struct triple *right)
 {
-       /* Careful this assumes it can do the easy thing to get the block */
+       struct block *block;
        struct triple *ret;
-       ret = build_triple(state, op, type, left, right, 
-               base->filename, base->line, base->col);
-       ret->u.block = base->u.block;
+       int zlhs;
+       /* If I am an OP_PIECE jump to the real instruction */
+       if (base->op == OP_PIECE) {
+               base = MISC(base, 0);
+       }
+       /* If I have a left hand side skip over it */
+       zlhs = TRIPLE_LHS(base->sizes);
+       if (zlhs) {
+               base = LHS(base, zlhs - 1);
+       }
+
+       block = block_of_triple(state, base);
+       get_occurance(base->occurance);
+       ret = build_triple(state, op, type, left, right, base->occurance);
+       if (triple_stores_block(state, ret)) {
+               ret->u.block = block;
+       }
        insert_triple(state, base->next, ret);
+       if (block->last == base) {
+               block->last = ret;
+       }
        return ret;
 }
 
@@ -1084,351 +1598,228 @@ static struct triple *label(struct compile_state *state)
        return result;
 }
 
-static int triple_is_pure(struct compile_state *state, struct triple *ins)
+static void display_triple(FILE *fp, struct triple *ins)
 {
-       /* Does the triple have no side effects.
-        * I.e. Rexecuting the triple with the same arguments 
-        * gives the same value.
-        */
-       int pure;
-       switch(ins->op) {
-       case OP_SMUL:    case OP_UMUL:
-       case OP_SDIV:    case OP_UDIV:
-       case OP_SMOD:    case OP_UMOD:
-       case OP_ADD:     case OP_SUB:
-       case OP_SL:
-       case OP_USR:     case OP_SSR:
-       case OP_AND:
-       case OP_XOR:
-       case OP_OR:
-       case OP_POS:     case OP_NEG:
-       case OP_INVERT:
-       case OP_EQ:      case OP_NOTEQ:
-       case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
-       case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
-       case OP_LFALSE:  case OP_LTRUE:
-       case OP_NOOP:
-       case OP_INTCONST:
-       case OP_BLOBCONST:
-       case OP_ADDRCONST:
-
-       case OP_WRITE:   
-       case OP_READ:
-       case OP_COPY:
-       case OP_BRANCH:
-       case OP_LABEL:
-       case OP_ADECL:
-       case OP_SDECL:
-       case OP_PHI:
-
-
-       case OP_CMP:
-       case OP_TEST:
-       case OP_SET_EQ:      case OP_SET_NOTEQ:
-       case OP_SET_SLESS:   case OP_SET_ULESS:
-       case OP_SET_SMORE:   case OP_SET_UMORE:
-       case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
-       case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
-               
-       case OP_JMP:
-       case OP_JMP_EQ:      case OP_JMP_NOTEQ:
-       case OP_JMP_SLESS:   case OP_JMP_ULESS:
-       case OP_JMP_SMORE:   case OP_JMP_UMORE:
-       case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
-       case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
+       struct occurance *ptr;
+       const char *reg;
+       char pre, post;
+       pre = post = ' ';
+       if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
+               pre = '^';
+       }
+       if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
+               post = 'v';
+       }
+       reg = arch_reg_str(ID_REG(ins->id));
+       if (ins->op == OP_INTCONST) {
+               fprintf(fp, "(%p) %c%c %-7s %-2d %-10s <0x%08lx>         ",
+                       ins, pre, post, reg, ins->template_id, tops(ins->op), 
+                       ins->u.cval);
+       }
+       else if (ins->op == OP_ADDRCONST) {
+               fprintf(fp, "(%p) %c%c %-7s %-2d %-10s %-10p <0x%08lx>",
+                       ins, pre, post, reg, ins->template_id, tops(ins->op), 
+                       MISC(ins, 0), ins->u.cval);
+       }
+       else {
+               int i, count;
+               fprintf(fp, "(%p) %c%c %-7s %-2d %-10s", 
+                       ins, pre, post, reg, ins->template_id, tops(ins->op));
+               count = TRIPLE_SIZE(ins->sizes);
+               for(i = 0; i < count; i++) {
+                       fprintf(fp, " %-10p", ins->param[i]);
+               }
+               for(; i < 2; i++) {
+                       fprintf(fp, "           ");
+               }
+       }
+       fprintf(fp, " @");
+       for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
+               fprintf(fp, " %s,%s:%d.%d",
+                       ptr->function, 
+                       ptr->filename,
+                       ptr->line, 
+                       ptr->col);
+       }
+       fprintf(fp, "\n");
+#if 0
+       {
+               struct triple_set *user;
+               for(user = ptr->use; user; user = user->next) {
+                       fprintf(fp, "use: %p\n", user->member);
+               }
+       }
+#endif
+       fflush(fp);
+}
 
-       case OP_BSF:         case OP_BSR:
-               pure = 1;
-               break;
-       case OP_LOAD: case OP_STORE:
-       case OP_INB:  case OP_INW:   case OP_INL:
-       case OP_OUTB: case OP_OUTW:  case OP_OUTL:
-       case OP_HLT:
-               pure = 0;
-               break;
-       default:
-               internal_error(state, ins, "purity of %s not known",
+static int triple_is_pure(struct compile_state *state, struct triple *ins)
+{
+       /* Does the triple have no side effects.
+        * I.e. Rexecuting the triple with the same arguments 
+        * gives the same value.
+        */
+       unsigned pure;
+       valid_ins(state, ins);
+       pure = PURE_BITS(table_ops[ins->op].flags);
+       if ((pure != PURE) && (pure != IMPURE)) {
+               internal_error(state, 0, "Purity of %s not known\n",
                        tops(ins->op));
-               pure = 0;
-               break;
        }
-       return pure;
+       return pure == PURE;
 }
 
-static int triple_is_branch(struct triple *ins)
+static int triple_is_branch(struct compile_state *state, struct triple *ins)
 {
        /* This function is used to determine which triples need
         * a register.
         */
-       int is_branch = 0;
-       switch(ins->op) {
-       case OP_BRANCH:
-       case OP_JMP:
-       case OP_JMP_EQ:      case OP_JMP_NOTEQ: 
-       case OP_JMP_SLESS:   case OP_JMP_ULESS: 
-       case OP_JMP_SMORE:   case OP_JMP_UMORE:
-       case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
-       case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
-               is_branch = 1;
-               break;
-       }
+       int is_branch;
+       valid_ins(state, ins);
+       is_branch = (table_ops[ins->op].targ != 0);
        return is_branch;
 }
 
-static int triple_is_def(struct triple *ins)
+static int triple_is_cond_branch(struct compile_state *state, struct triple *ins)
+{
+       /* A conditional branch has the condition argument as a single
+        * RHS parameter.
+        */
+       return triple_is_branch(state, ins) &&
+               (TRIPLE_RHS(ins->sizes) == 1);
+}
+
+static int triple_is_uncond_branch(struct compile_state *state, struct triple *ins)
+{
+       /* A unconditional branch has no RHS parameters.
+        */
+       return triple_is_branch(state, ins) &&
+               (TRIPLE_RHS(ins->sizes) == 0);
+}
+
+static int triple_is_def(struct compile_state *state, struct triple *ins)
 {
        /* This function is used to determine which triples need
         * a register.
         */
-       int is_def = 1;
-       switch(ins->op) {
-       case OP_ADECL:
-       case OP_SDECL:
-       case OP_LABEL:
-       case OP_INTCONST:
-       case OP_BLOBCONST:
-       case OP_ADDRCONST:
-       case OP_STORE:
-       case OP_WRITE:
-       case OP_NOOP:
-       case OP_OUTB:    case OP_OUTW:    case OP_OUTL:
-       case OP_BRANCH:
-       case OP_JMP:
-       case OP_JMP_EQ:      case OP_JMP_NOTEQ: 
-       case OP_JMP_SLESS:   case OP_JMP_ULESS: 
-       case OP_JMP_SMORE:   case OP_JMP_UMORE:
-       case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
-       case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
-               is_def = 0;
-               break;
-       }
+       int is_def;
+       valid_ins(state, ins);
+       is_def = (table_ops[ins->op].flags & DEF) == DEF;
        return is_def;
 }
 
-static struct triple **triple_targ(struct compile_state *state,
-       struct triple *triple, struct triple **last)
+static struct triple **triple_iter(struct compile_state *state,
+       size_t count, struct triple **vector,
+       struct triple *ins, struct triple **last)
 {
        struct triple **ret;
        ret = 0;
-       switch(triple->op) {
-       case OP_BRANCH:
-       case OP_JMP:
-       case OP_JMP_EQ:      case OP_JMP_NOTEQ:
-       case OP_JMP_SLESS:   case OP_JMP_ULESS:
-       case OP_JMP_SMORE:   case OP_JMP_UMORE:
-       case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
-       case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
+       if (count) {
                if (!last) {
-                       ret = &triple->left;
+                       ret = vector;
                }
-               else if ((last == &triple->left) && triple->right) {
-                       ret = &triple->next;
+               else if ((last >= vector) && (last < (vector + count - 1))) {
+                       ret = last + 1;
                }
-               break;
        }
        return ret;
+       
+}
+
+static struct triple **triple_lhs(struct compile_state *state,
+       struct triple *ins, struct triple **last)
+{
+       return triple_iter(state, TRIPLE_LHS(ins->sizes), &LHS(ins,0), 
+               ins, last);
 }
 
 static struct triple **triple_rhs(struct compile_state *state,
-       struct triple *triple, struct triple **last)
+       struct triple *ins, struct triple **last)
 {
-       struct triple **ret;
+       return triple_iter(state, TRIPLE_RHS(ins->sizes), &RHS(ins,0), 
+               ins, last);
+}
+
+static struct triple **triple_misc(struct compile_state *state,
+       struct triple *ins, struct triple **last)
+{
+       return triple_iter(state, TRIPLE_MISC(ins->sizes), &MISC(ins,0), 
+               ins, last);
+}
+
+static struct triple **triple_targ(struct compile_state *state,
+       struct triple *ins, struct triple **last)
+{
+       size_t count;
+       struct triple **ret, **vector;
        ret = 0;
-       switch(triple->op) {
-               /* binary operations */
-       case OP_SMUL:    case OP_UMUL:    case OP_SDIV:    case OP_UDIV:
-       case OP_SMOD:    case OP_UMOD:    case OP_ADD:     case OP_SUB:
-       case OP_SL:      case OP_USR:     case OP_SSR:     case OP_AND:  
-       case OP_XOR:     case OP_OR:      case OP_EQ:      case OP_NOTEQ: 
-       case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
-       case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
-       case OP_CMP:
-       case OP_OUTB:    case OP_OUTW:    case OP_OUTL:
-#if 0
-               if (!triple->left) {
-                       internal_error(state, triple, "left arg missing");
-               }
-               if (!triple->right) {
-                       internal_error(state, triple, "right arg missing");
-               }
-#endif
-               if (!last) {
-                       ret = &triple->left;
-               }
-               else if (last == &triple->left){
-                       ret = &triple->right;
-               }
-               break;
-               /* unary operations */
-       case OP_POS:     case OP_NEG:
-       case OP_INVERT:  case OP_LFALSE:  case OP_LTRUE:
-       case OP_COPY:
-       case OP_TEST:
-       case OP_SET_EQ:      case OP_SET_NOTEQ:
-       case OP_SET_SLESS:   case OP_SET_ULESS:
-       case OP_SET_SMORE:   case OP_SET_UMORE:
-       case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
-       case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
-       case OP_INB:         case OP_INW:     case OP_INL:
-       case OP_BSF:         case OP_BSR:
-#if 0
-               if (!triple->left) {
-                       internal_error(state, triple, "left arg missing");
-               }
-               if (triple->right) {
-                       internal_error(state, triple, "right arg present");
-               }
-#endif
-               if (!last) {
-                       ret = &triple->left;
-               }
-               break;
-               /* Writes */
-       case OP_WRITE:
-       case OP_STORE:
-               if (!last) {
-                       ret = &triple->right;
-               }
-               break;
-               /* Reads */
-       case OP_READ:
+       count = TRIPLE_TARG(ins->sizes);
+       vector = &TARG(ins, 0);
+       if (count) {
                if (!last) {
-                       ret = &triple->left;
+                       ret = vector;
                }
-               break;
-               /* Branches */
-       case OP_BRANCH:
-       case OP_JMP:
-       case OP_JMP_EQ:      case OP_JMP_NOTEQ:
-       case OP_JMP_SLESS:   case OP_JMP_ULESS:
-       case OP_JMP_SMORE:   case OP_JMP_UMORE:
-       case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
-       case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
-               if (!last && triple->right) {
-                       ret = &triple->right;
+               else if ((last >= vector) && (last < (vector + count - 1))) {
+                       ret = last + 1;
                }
-               break;
-               /* Phi... */
-       case OP_PHI:
-       {
-               struct triple **slot;
-               struct block *block;
-               block = triple->u.block;
-               slot = (struct triple **)(triple->left);
-               if (!last) {
-                       ret = slot;
+               else if ((last == (vector + count - 1)) && 
+                       TRIPLE_RHS(ins->sizes)) {
+                       ret = &ins->next;
                }
-               else if ((last >= slot) && (last < (slot + block->users -1))) {
-                       ret = last + 1;
-                       
+       }
+       return ret;
+}
+
+
+static void verify_use(struct compile_state *state,
+       struct triple *user, struct triple *used)
+{
+       int size, i;
+       size = TRIPLE_SIZE(user->sizes);
+       for(i = 0; i < size; i++) {
+               if (user->param[i] == used) {
+                       break;
                }
-               break;
        }
-               /* Loads */
-       case OP_LOAD:
-               /* address constant.. */
-       case OP_ADDRCONST:
-               if (!last) {
-                       ret = &triple->left;
+       if (triple_is_branch(state, user)) {
+               if (user->next == used) {
+                       i = -1;
                }
-               break;
-               /* Stores */
-               break;
-               /* Variables and labels */
-       case OP_ADECL:    case OP_SDECL:  case OP_LABEL:
-               /* Constants */
-       case OP_INTCONST:
-       case OP_BLOBCONST:
-       case OP_NOOP:
-       case OP_HLT:
-               /* These operations that have no rhs expression */
-               break;
-       default:
-               internal_error(state, 0, "unknown expression type: %d %s",
-                       triple->op, tops(triple->op));
-               break;
-
        }
-       return ret;
+       if (i == size) {
+               internal_error(state, user, "%s(%p) does not use %s(%p)",
+                       tops(user->op), user, tops(used->op), used);
+       }
 }
 
-static struct triple **triple_lhs(struct compile_state *state,
-       struct triple *triple, struct triple **last)
+static int find_rhs_use(struct compile_state *state, 
+       struct triple *user, struct triple *used)
 {
-       struct triple **ret;
-       ret = 0;
-       switch(triple->op) {
-               /* binary operations */
-       case OP_SMUL:    case OP_UMUL:    case OP_SDIV:    case OP_UDIV:
-       case OP_SMOD:    case OP_UMOD:    case OP_ADD:     case OP_SUB:
-       case OP_SL:      case OP_USR:     case OP_SSR:     case OP_AND:  
-       case OP_XOR:     case OP_OR:      case OP_EQ:      case OP_NOTEQ: 
-       case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
-       case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
-       case OP_CMP:
-       case OP_OUTB:    case OP_OUTW:    case OP_OUTL:
-               /* unary operations */
-       case OP_POS:     case OP_NEG:
-       case OP_INVERT:  case OP_LFALSE:  case OP_LTRUE:
-       case OP_COPY:
-       case OP_TEST:
-       case OP_SET_EQ:      case OP_SET_NOTEQ:
-       case OP_SET_SLESS:   case OP_SET_ULESS:
-       case OP_SET_SMORE:   case OP_SET_UMORE:
-       case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
-       case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
-       case OP_INB:     case OP_INW:     case OP_INL:
-       case OP_BSF:     case OP_BSR:
-               /* Variable reads */
-       case OP_READ:    case OP_PHI:
-               /* Branches */
-       case OP_BRANCH:
-       case OP_JMP:
-       case OP_JMP_EQ:      case OP_JMP_NOTEQ:
-       case OP_JMP_SLESS:   case OP_JMP_ULESS:
-       case OP_JMP_SMORE:   case OP_JMP_UMORE:
-       case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
-       case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
-               /* Loads */
-       case OP_LOAD:
-               /* Address constants */
-       case OP_ADDRCONST:
-               /* Variables and labels */
-       case OP_ADECL:   case OP_SDECL:   case OP_LABEL:
-               /* Constants */
-       case OP_INTCONST:
-       case OP_BLOBCONST:
-       case OP_NOOP:
-       case OP_HLT:
-               /* These expressions have no lhs expression */
-               break;
-               /* Writes */
-       case OP_WRITE:
-               /* Stores */
-       case OP_STORE:
-               if (!last) {
-                       ret = &triple->left;
+       struct triple **param;
+       int size, i;
+       verify_use(state, user, used);
+       size = TRIPLE_RHS(user->sizes);
+       param = &RHS(user, 0);
+       for(i = 0; i < size; i++) {
+               if (param[i] == used) {
+                       return i;
                }
-               break;
-       default:
-               internal_error(state, 0, "unknown expression type: %d %s",
-                       triple->op, tops(triple->op));
-               break;
        }
-       return ret;
+       return -1;
 }
 
 static void free_triple(struct compile_state *state, struct triple *ptr)
 {
+       size_t size;
+       size = sizeof(*ptr) - sizeof(ptr->param) +
+               (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr->sizes));
        ptr->prev->next = ptr->next;
        ptr->next->prev = ptr->prev;
        if (ptr->use) {
                internal_error(state, ptr, "ptr->use != 0");
        }
-       if (ptr->op == OP_PHI) {
-               xfree(ptr->left);
-       }
-       memset(ptr, -1, sizeof(*ptr));
+       put_occurance(ptr->occurance);
+       memset(ptr, -1, size);
        xfree(ptr);
 }
 
@@ -1449,6 +1840,12 @@ static void release_triple(struct compile_state *state, struct triple *ptr)
                        unuse_triple(*expr, ptr);
                }
        }
+       expr = triple_misc(state, ptr, 0);
+       for(; expr; expr = triple_misc(state, ptr, expr)) {
+               if (*expr) {
+                       unuse_triple(*expr, ptr);
+               }
+       }
        expr = triple_targ(state, ptr, 0);
        for(; expr; expr = triple_targ(state, ptr, expr)) {
                if (*expr) {
@@ -1470,6 +1867,12 @@ static void release_triple(struct compile_state *state, struct triple *ptr)
                                *expr = &zero_triple;
                        }
                }
+               expr = triple_misc(state, set->member, 0);
+               for(; expr; expr = triple_misc(state, set->member, expr)) {
+                       if (*expr == ptr) {
+                               *expr = &zero_triple;
+                       }
+               }
                expr = triple_targ(state, set->member, 0);
                for(; expr; expr = triple_targ(state, set->member, expr)) {
                        if (*expr == ptr) {
@@ -1806,6 +2209,22 @@ static void symbol(
        *chain    = sym;
 }
 
+static void label_symbol(struct compile_state *state, 
+       struct hash_entry *ident, struct triple *label)
+{
+       struct symbol *sym;
+       if (ident->sym_label) {
+               error(state, 0, "label %s already defined", ident->name);
+       }
+       sym = xcmalloc(sizeof(*sym), "label");
+       sym->ident = ident;
+       sym->def   = label;
+       sym->type  = &void_type;
+       sym->scope_depth = FUNCTION_SCOPE_DEPTH;
+       sym->next  = 0;
+       ident->sym_label = sym;
+}
+
 static void start_scope(struct compile_state *state)
 {
        state->scope_depth++;
@@ -1879,8 +2298,10 @@ static void register_keywords(struct compile_state *state)
        hash_keyword(state, "unsigned",      TOK_UNSIGNED);
        hash_keyword(state, "void",          TOK_VOID);
        hash_keyword(state, "volatile",      TOK_VOLATILE);
+       hash_keyword(state, "__volatile__",  TOK_VOLATILE);
        hash_keyword(state, "while",         TOK_WHILE);
        hash_keyword(state, "asm",           TOK_ASM);
+       hash_keyword(state, "__asm__",       TOK_ASM);
        hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
        hash_keyword(state, "__alignof__",   TOK_ALIGNOF);
 }
@@ -1927,6 +2348,14 @@ static int digitp(int c)
        }
        return ret;
 }
+static int digval(int c)
+{
+       int val = -1;
+       if ((c >= '0') && (c <= '9')) {
+               val = c - '0';
+       }
+       return val;
+}
 
 static int hexdigitp(int c)
 {
@@ -2127,6 +2556,7 @@ next_token:
                while ((tokp < end) && spacep(c)) {
                        if (c == '\n') {
                                file->line++;
+                               file->report_line++;
                                file->line_start = tokp + 1;
                        }
                        c = *(++tokp);
@@ -2142,6 +2572,7 @@ next_token:
                        c = *tokp;
                        if (c == '\n') {
                                file->line++;
+                               file->report_line++;
                                file->line_start = tokp +1;
                                break;
                        }
@@ -2168,6 +2599,7 @@ next_token:
                if (tok == TOK_UNKNOWN) {
                        error(state, 0, "unterminated comment");
                }
+               file->report_line += line - file->line;
                file->line = line;
                file->line_start = line_start;
        }
@@ -2204,6 +2636,7 @@ next_token:
                if (line != file->line) {
                        warning(state, 0, "multiline string constant");
                }
+               file->report_line += line - file->line;
                file->line = line;
                file->line_start = line_start;
 
@@ -2243,6 +2676,7 @@ next_token:
                if (line != file->line) {
                        warning(state, 0, "multiline character constant");
                }
+               file->report_line += line - file->line;
                file->line = line;
                file->line_start = line_start;
 
@@ -2442,6 +2876,9 @@ static void compile_macro(struct compile_state *state, struct token *tk)
        file->pos = file->buf;
        file->line_start = file->pos;
        file->line = 1;
+       file->report_line = 1;
+       file->report_name = file->basename;
+       file->report_dir  = file->dirname;
        file->prev = state->file;
        state->file = file;
 }
@@ -2463,6 +2900,9 @@ static int mpeek(struct compile_state *state, int index)
                        struct file_state *file = state->file;
                        state->file = file->prev;
                        /* file->basename is used keep it */
+                       if (file->report_dir != file->dirname) {
+                               xfree(file->report_dir);
+                       }
                        xfree(file->dirname);
                        xfree(file->buf);
                        xfree(file);
@@ -2822,8 +3262,69 @@ static void preprocess(struct compile_state *state, int index)
                        tk->ident->name);
        }
        switch(tk->tok) {
-       case TOK_UNDEF:
+       case TOK_LIT_INT:
+       {
+               int override_line;
+               override_line = strtoul(tk->val.str, 0, 10);
+               next_token(state, index);
+               /* I have a cpp line marker parse it */
+               if (tk->tok == TOK_LIT_STRING) {
+                       const char *token, *base;
+                       char *name, *dir;
+                       int name_len, dir_len;
+                       name = xmalloc(tk->str_len, "report_name");
+                       token = tk->val.str + 1;
+                       base = strrchr(token, '/');
+                       name_len = tk->str_len -2;
+                       if (base != 0) {
+                               dir_len = base - token;
+                               base++;
+                               name_len -= base - token;
+                       } else {
+                               dir_len = 0;
+                               base = token;
+                       }
+                       memcpy(name, base, name_len);
+                       name[name_len] = '\0';
+                       dir = xmalloc(dir_len + 1, "report_dir");
+                       memcpy(dir, token, dir_len);
+                       dir[dir_len] = '\0';
+                       file->report_line = override_line - 1;
+                       file->report_name = name;
+                       file->report_dir = dir;
+               }
+       }
+               break;
        case TOK_LINE:
+               meat(state, index, TOK_LINE);
+               meat(state, index, TOK_LIT_INT);
+               file->report_line = strtoul(tk->val.str, 0, 10) -1;
+               if (mpeek(state, index) == TOK_LIT_STRING) {
+                       const char *token, *base;
+                       char *name, *dir;
+                       int name_len, dir_len;
+                       meat(state, index, TOK_LIT_STRING);
+                       name = xmalloc(tk->str_len, "report_name");
+                       token = tk->val.str + 1;
+                       name_len = tk->str_len - 2;
+                       if (base != 0) {
+                               dir_len = base - token;
+                               base++;
+                               name_len -= base - token;
+                       } else {
+                               dir_len = 0;
+                               base = token;
+                       }
+                       memcpy(name, base, name_len);
+                       name[name_len] = '\0';
+                       dir = xmalloc(dir_len + 1, "report_dir");
+                       memcpy(dir, token, dir_len);
+                       dir[dir_len] = '\0';
+                       file->report_name = name;
+                       file->report_dir = dir;
+               }
+               break;
+       case TOK_UNDEF:
        case TOK_PRAGMA:
                if (state->if_value < 0) {
                        break;
@@ -3033,7 +3534,12 @@ static void preprocess(struct compile_state *state, int index)
                }
                /* Error if there are any characters after the include */
                for(ptr = file->pos; *ptr != '\n'; ptr++) {
-                       if (!isspace(*ptr)) {
+                       switch(*ptr) {
+                       case ' ':
+                       case '\t':
+                       case '\v':
+                               break;
+                       default:
                                error(state, 0, "garbage after include directive");
                        }
                }
@@ -3115,9 +3621,7 @@ static int peek2(struct compile_state *state)
        return state->token[2].tok;
 }
 
-static void __eat(
-       const char *file, const char *func, int line,
-       struct compile_state *state, int tok)
+static void eat(struct compile_state *state, int tok)
 {
        int next_tok;
        int i;
@@ -3129,9 +3633,8 @@ static void __eat(
                if (next_tok == TOK_IDENT) {
                        name2 = state->token[1].ident->name;
                }
-               internal_error(state, 0, "@ %s.%s:%d \tfound %s %s expected %s", 
-                       file, func, line,
-                       name1, name2, tokens[tok]);
+               error(state, 0, "\tfound %s %s expected %s",
+                       name1, name2 ,tokens[tok]);
        }
        /* Free the old token value */
        if (state->token[0].str_len) {
@@ -3143,7 +3646,6 @@ static void __eat(
        memset(&state->token[i], 0, sizeof(state->token[i]));
        state->token[i].tok = -1;
 }
-#define eat(state, tok) __eat(__FILE__, __func__, __LINE__, state, tok)
 
 #warning "FIXME do not hardcode the include paths"
 static char *include_paths[] = {
@@ -3153,10 +3655,10 @@ static char *include_paths[] = {
        0
 };
 
-static void compile_file(struct compile_state *state, char *filename, int local)
+static void compile_file(struct compile_state *state, const char *filename, int local)
 {
        char cwd[4096];
-       char *subdir, *base;
+       const char *subdir, *base;
        int subdir_len;
        struct file_state *file;
        char *basename;
@@ -3219,6 +3721,10 @@ static void compile_file(struct compile_state *state, char *filename, int local)
        file->line_start = file->pos;
        file->line = 1;
 
+       file->report_line = 1;
+       file->report_name = file->basename;
+       file->report_dir  = file->dirname;
+
        file->prev = state->file;
        state->file = file;
        
@@ -3226,89 +3732,7 @@ static void compile_file(struct compile_state *state, char *filename, int local)
        splice_lines(state);
 }
 
-/* visibility global/local */
-/* static/auto duration */
-/* typedef, register, inline */
-#define STOR_SHIFT         0
-#define STOR_MASK     0x000f
-/* Visibility */
-#define STOR_GLOBAL   0x0001
-/* Duration */
-#define STOR_PERM     0x0002
-/* Storage specifiers */
-#define STOR_AUTO     0x0000
-#define STOR_STATIC   0x0002
-#define STOR_EXTERN   0x0003
-#define STOR_REGISTER 0x0004
-#define STOR_TYPEDEF  0x0008
-#define STOR_INLINE   0x000c
-
-#define QUAL_SHIFT         4
-#define QUAL_MASK     0x0070
-#define QUAL_NONE     0x0000
-#define QUAL_CONST    0x0010
-#define QUAL_VOLATILE 0x0020
-#define QUAL_RESTRICT 0x0040
-
-#define TYPE_SHIFT         8
-#define TYPE_MASK     0x1f00
-#define TYPE_INTEGER(TYPE)    (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG))
-#define TYPE_ARITHMETIC(TYPE) (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE))
-#define TYPE_UNSIGNED(TYPE)   ((TYPE) & 0x0100)
-#define TYPE_SIGNED(TYPE)     (!TYPE_UNSIGNED(TYPE))
-#define TYPE_MKUNSIGNED(TYPE) ((TYPE) | 0x0100)
-#define TYPE_RANK(TYPE)       ((TYPE) & ~0x0100)
-#define TYPE_PTR(TYPE)        (((TYPE) & TYPE_MASK) == TYPE_POINTER)
-#define TYPE_DEFAULT  0x0000
-#define TYPE_VOID     0x0100
-#define TYPE_CHAR     0x0200
-#define TYPE_UCHAR    0x0300
-#define TYPE_SHORT    0x0400
-#define TYPE_USHORT   0x0500
-#define TYPE_INT      0x0600
-#define TYPE_UINT     0x0700
-#define TYPE_LONG     0x0800
-#define TYPE_ULONG    0x0900
-#define TYPE_LLONG    0x0a00 /* long long */
-#define TYPE_ULLONG   0x0b00
-#define TYPE_FLOAT    0x0c00
-#define TYPE_DOUBLE   0x0d00
-#define TYPE_LDOUBLE  0x0e00 /* long double */
-#define TYPE_STRUCT   0x1000
-#define TYPE_ENUM     0x1100
-#define TYPE_POINTER  0x1200 
-/* For TYPE_POINTER:
- * type->left holds the type pointed to.
- */
-#define TYPE_FUNCTION 0x1300 
-/* For TYPE_FUNCTION:
- * type->left holds the return type.
- * type->right holds the...
- */
-#define TYPE_PRODUCT  0x1400
-/* TYPE_PRODUCT is a basic building block when defining structures
- * type->left holds the type that appears first in memory.
- * type->right holds the type that appears next in memory.
- */
-#define TYPE_OVERLAP  0x1500
-/* TYPE_OVERLAP is a basic building block when defining unions
- * type->left and type->right holds to types that overlap
- * each other in memory.
- */
-#define TYPE_ARRAY    0x1600
-/* TYPE_ARRAY is a basic building block when definitng arrays.
- * type->left holds the type we are an array of.
- * type-> holds the number of elements.
- */
-
-#define ELEMENT_COUNT_UNSPECIFIED (~0UL)
-
-struct type {
-       unsigned int type;
-       struct type *left, *right;
-       ulong_t elements;
-       struct hash_entry *ident;
-};
+/* Type helper functions */
 
 static struct type *new_type(
        unsigned int type, struct type *left, struct type *right)
@@ -3318,7 +3742,8 @@ static struct type *new_type(
        result->type = type;
        result->left = left;
        result->right = right;
-       result->ident = 0;
+       result->field_ident = 0;
+       result->type_ident = 0;
        return result;
 }
 
@@ -3353,7 +3778,6 @@ static inline ulong_t mask_uint(ulong_t x)
 #define MASK_UINT(X)      (mask_uint(X))
 #define MASK_ULONG(X)    (X)
 
-
 static struct type void_type   = { .type  = TYPE_VOID };
 static struct type char_type   = { .type  = TYPE_CHAR };
 static struct type uchar_type  = { .type  = TYPE_UCHAR };
@@ -3368,7 +3792,24 @@ static struct triple *variable(struct compile_state *state, struct type *type)
 {
        struct triple *result;
        if ((type->type & STOR_MASK) != STOR_PERM) {
-               result = triple(state, OP_ADECL, type, 0, 0);
+               if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
+                       result = triple(state, OP_ADECL, type, 0, 0);
+               } else {
+                       struct type *field;
+                       struct triple **vector;
+                       ulong_t index;
+                       result = new_triple(state, OP_VAL_VEC, type, -1, -1);
+                       vector = &result->param[0];
+
+                       field = type->left;
+                       index = 0;
+                       while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
+                               vector[index] = variable(state, field->left);
+                               field = field->right;
+                               index++;
+                       }
+                       vector[index] = variable(state, field);
+               }
        }
        else {
                result = triple(state, OP_SDECL, type, 0, 0);
@@ -3411,6 +3852,7 @@ static void qual_of(FILE *fp, struct type *type)
                fprintf(fp, " restrict");
        }
 }
+
 static void name_of(FILE *fp, struct type *type)
 {
        stor_of(fp, type);
@@ -3463,11 +3905,11 @@ static void name_of(FILE *fp, struct type *type)
                name_of(fp, type->right);
                break;
        case TYPE_ENUM:
-               fprintf(fp, "enum %s", type->ident->name);
+               fprintf(fp, "enum %s", type->type_ident->name);
                qual_of(fp, type);
                break;
        case TYPE_STRUCT:
-               fprintf(fp, "struct %s", type->ident->name);
+               fprintf(fp, "struct %s", type->type_ident->name);
                qual_of(fp, type);
                break;
        case TYPE_FUNCTION:
@@ -3526,6 +3968,9 @@ static size_t align_of(struct compile_state *state, struct type *type)
        case TYPE_ARRAY:
                align = align_of(state, type->left);
                break;
+       case TYPE_STRUCT:
+               align = align_of(state, type->left);
+               break;
        default:
                error(state, 0, "alignof not yet defined for type\n");
                break;
@@ -3533,6 +3978,15 @@ static size_t align_of(struct compile_state *state, struct type *type)
        return align;
 }
 
+static size_t needed_padding(size_t offset, size_t align)
+{
+        size_t padding;
+       padding = 0;
+       if (offset % align) {
+               padding = align - (offset % align);
+       }
+       return padding;
+}
 static size_t size_of(struct compile_state *state, struct type *type)
 {
        size_t size;
@@ -3562,16 +4016,16 @@ static size_t size_of(struct compile_state *state, struct type *type)
        case TYPE_PRODUCT:
        {
                size_t align, pad;
-               size = size_of(state, type->left);
-               while((type->right->type & TYPE_MASK) == TYPE_PRODUCT) {
-                       type = type->right;
+               size = 0;
+               while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
                        align = align_of(state, type->left);
-                       pad = align - (size % align);
+                       pad = needed_padding(size, align);
                        size = size + pad + size_of(state, type->left);
+                       type = type->right;
                }
-               align = align_of(state, type->right);
-               pad = align - (size % align);
-               size = size + pad + sizeof(type->right);
+               align = align_of(state, type);
+               pad = needed_padding(size, align);
+               size = size + pad + sizeof(type);
                break;
        }
        case TYPE_OVERLAP:
@@ -3589,13 +4043,126 @@ static size_t size_of(struct compile_state *state, struct type *type)
                        size = size_of(state, type->left) * type->elements;
                }
                break;
+       case TYPE_STRUCT:
+               size = size_of(state, type->left);
+               break;
        default:
-               error(state, 0, "sizeof not yet defined for type\n");
+               internal_error(state, 0, "sizeof not yet defined for type\n");
                break;
        }
        return size;
 }
 
+static size_t field_offset(struct compile_state *state, 
+       struct type *type, struct hash_entry *field)
+{
+       struct type *member;
+       size_t size, align;
+       if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
+               internal_error(state, 0, "field_offset only works on structures");
+       }
+       size = 0;
+       member = type->left;
+       while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
+               align = align_of(state, member->left);
+               size += needed_padding(size, align);
+               if (member->left->field_ident == field) {
+                       member = member->left;
+                       break;
+               }
+               size += size_of(state, member->left);
+               member = member->right;
+       }
+       align = align_of(state, member);
+       size += needed_padding(size, align);
+       if (member->field_ident != field) {
+               error(state, 0, "member %s not present", field->name);
+       }
+       return size;
+}
+
+static struct type *field_type(struct compile_state *state, 
+       struct type *type, struct hash_entry *field)
+{
+       struct type *member;
+       if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
+               internal_error(state, 0, "field_type only works on structures");
+       }
+       member = type->left;
+       while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
+               if (member->left->field_ident == field) {
+                       member = member->left;
+                       break;
+               }
+               member = member->right;
+       }
+       if (member->field_ident != field) {
+               error(state, 0, "member %s not present", field->name);
+       }
+       return member;
+}
+
+static struct type *next_field(struct compile_state *state,
+       struct type *type, struct type *prev_member) 
+{
+       struct type *member;
+       if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
+               internal_error(state, 0, "next_field only works on structures");
+       }
+       member = type->left;
+       while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
+               if (!prev_member) {
+                       member = member->left;
+                       break;
+               }
+               if (member->left == prev_member) {
+                       prev_member = 0;
+               }
+               member = member->right;
+       }
+       if (member == prev_member) {
+               prev_member = 0;
+       }
+       if (prev_member) {
+               internal_error(state, 0, "prev_member %s not present", 
+                       prev_member->field_ident->name);
+       }
+       return member;
+}
+
+static struct triple *struct_field(struct compile_state *state,
+       struct triple *decl, struct hash_entry *field)
+{
+       struct triple **vector;
+       struct type *type;
+       ulong_t index;
+       type = decl->type;
+       if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
+               return decl;
+       }
+       if (decl->op != OP_VAL_VEC) {
+               internal_error(state, 0, "Invalid struct variable");
+       }
+       if (!field) {
+               internal_error(state, 0, "Missing structure field");
+       }
+       type = type->left;
+       vector = &RHS(decl, 0);
+       index = 0;
+       while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
+               if (type->left->field_ident == field) {
+                       type = type->left;
+                       break;
+               }
+               index += 1;
+               type = type->right;
+       }
+       if (type->field_ident != field) {
+               internal_error(state, 0, "field %s not found?", field->name);
+       }
+       return vector[index];
+}
+
 static void arrays_complete(struct compile_state *state, struct type *type)
 {
        if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
@@ -3665,6 +4232,10 @@ static int equiv_types(struct type *left, struct type *right)
                return 0;
        }
        type = left->type & TYPE_MASK;
+       /* If the basic types match and it is a void type we are done */
+       if (type == TYPE_VOID) {
+               return 1;
+       }
        /* if the basic types match and it is an arithmetic type we are done */
        if (TYPE_ARITHMETIC(type)) {
                return 1;
@@ -3679,7 +4250,7 @@ static int equiv_types(struct type *left, struct type *right)
        }
        /* test for struct/union equality */
        else if (type == TYPE_STRUCT) {
-               return left->ident == right->ident;
+               return left->type_ident == right->type_ident;
        }
        /* Test for equivalent functions */
        else if (type == TYPE_FUNCTION) {
@@ -3730,7 +4301,7 @@ static struct type *compatible_types(struct type *left, struct type *right)
        }
        /* test for struct/union equality */
        else if (type == TYPE_STRUCT) {
-               if (left->ident == right->ident) {
+               if (left->type_ident == right->type_ident) {
                        result = left;
                }
        }
@@ -3798,7 +4369,7 @@ static struct triple *integral_promotion(
 static void arithmetic(struct compile_state *state, struct triple *def)
 {
        if (!TYPE_ARITHMETIC(def->type->type)) {
-               error(state, def, "arithmetic type expexted");
+               error(state, 0, "arithmetic type expexted");
        }
 }
 
@@ -3835,8 +4406,31 @@ static int is_signed(struct type *type)
        return !!TYPE_SIGNED(type->type);
 }
 
+/* Is this value located in a register otherwise it must be in memory */
+static int is_in_reg(struct compile_state *state, struct triple *def)
+{
+       int in_reg;
+       if (def->op == OP_ADECL) {
+               in_reg = 1;
+       }
+       else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
+               in_reg = 0;
+       }
+       else if (def->op == OP_VAL_VEC) {
+               in_reg = is_in_reg(state, RHS(def, 0));
+       }
+       else if (def->op == OP_DOT) {
+               in_reg = is_in_reg(state, RHS(def, 0));
+       }
+       else {
+               internal_error(state, 0, "unknown expr storage location");
+               in_reg = -1;
+       }
+       return in_reg;
+}
+
 /* Is this a stable variable location otherwise it must be a temporary */
-static int is_stable(struct triple *def)
+static int is_stable(struct compile_state *state, struct triple *def)
 {
        int ret;
        ret = 0;
@@ -3850,39 +4444,55 @@ static int is_stable(struct triple *def)
                ret = 1;
        }
        else if (def->op == OP_DOT) {
-               ret = is_stable(def->left);
+               ret = is_stable(state, RHS(def, 0));
+       }
+       else if (def->op == OP_VAL_VEC) {
+               struct triple **vector;
+               ulong_t i;
+               ret = 1;
+               vector = &RHS(def, 0);
+               for(i = 0; i < def->type->elements; i++) {
+                       if (!is_stable(state, vector[i])) {
+                               ret = 0;
+                               break;
+                       }
+               }
        }
        return ret;
 }
 
-static int is_lvalue(struct triple *def)
+static int is_lvalue(struct compile_state *state, struct triple *def)
 {
        int ret;
        ret = 1;
        if (!def) {
                return 0;
        }
-       if (!is_stable(def)) {
+       if (!is_stable(state, def)) {
                return 0;
        }
-       if (def->type->type & QUAL_CONST) {
-               ret = 0;
-       }
-       else if (def->op == OP_DOT) {
-               ret = is_lvalue(def->left);
+       if (def->op == OP_DOT) {
+               ret = is_lvalue(state, RHS(def, 0));
        }
        return ret;
 }
 
-static void lvalue(struct compile_state *state, struct triple *def)
+static void clvalue(struct compile_state *state, struct triple *def)
 {
        if (!def) {
                internal_error(state, def, "nothing where lvalue expected?");
        }
-       if (!is_lvalue(def)) { 
+       if (!is_lvalue(state, def)) { 
                error(state, def, "lvalue expected");
        }
 }
+static void lvalue(struct compile_state *state, struct triple *def)
+{
+       clvalue(state, def);
+       if (def->type->type & QUAL_CONST) {
+               error(state, def, "modifable lvalue expected");
+       }
+}
 
 static int is_pointer(struct triple *def)
 {
@@ -3914,76 +4524,120 @@ static struct triple *int_const(
 }
 
 
-static struct triple *mk_addr_expr(
-       struct compile_state *state, struct triple *expr, ulong_t offset)
+static struct triple *do_mk_addr_expr(struct compile_state *state, 
+       struct triple *expr, struct type *type, ulong_t offset)
 {
        struct triple *result;
-       struct type *type;
-       
-       lvalue(state, expr);
-       type = new_type(
-               TYPE_POINTER | (expr->type->type & QUAL_MASK),
-               expr->type, 0);
+       clvalue(state, expr);
+
+       type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
 
        result = 0;
        if (expr->op == OP_ADECL) {
                error(state, expr, "address of auto variables not supported");
        }
        else if (expr->op == OP_SDECL) {
-               result = triple(state, OP_ADDRCONST, type, expr, 0);
+               result = triple(state, OP_ADDRCONST, type, 0, 0);
+               MISC(result, 0) = expr;
                result->u.cval = offset;
        }
        else if (expr->op == OP_DEREF) {
                result = triple(state, OP_ADD, type,
-                       expr->left,
+                       RHS(expr, 0),
                        int_const(state, &ulong_type, offset));
        }
        return result;
 }
 
+static struct triple *mk_addr_expr(
+       struct compile_state *state, struct triple *expr, ulong_t offset)
+{
+       return do_mk_addr_expr(state, expr, expr->type, offset);
+}
+
 static struct triple *mk_deref_expr(
        struct compile_state *state, struct triple *expr)
 {
        struct type *base_type;
        pointer(state, expr);
        base_type = expr->type->left;
-       if (!TYPE_PTR(base_type->type) && !TYPE_ARITHMETIC(base_type->type)) {
-               error(state, 0, 
-                       "Only pointer and arithmetic values can be dereferenced");
-       }
        return triple(state, OP_DEREF, base_type, expr, 0);
 }
 
+static struct triple *array_to_pointer(struct compile_state *state, struct triple *def)
+{
+       if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
+               struct type *type;
+               type = new_type(
+                       TYPE_POINTER | (def->type->type & QUAL_MASK),
+                       def->type->left, 0);
+               if ((def->op == OP_SDECL) || is_const(def)) {
+                       struct triple *addrconst;
+                       if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
+                               internal_error(state, def, "bad array constant");
+                       }
+                       addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
+                       MISC(addrconst, 0) = def;
+                       def = addrconst;
+               }
+               else {
+                       def = triple(state, OP_COPY, type, def, 0);
+               }
+       }
+       return def;
+}
+
+static struct triple *deref_field(
+       struct compile_state *state, struct triple *expr, struct hash_entry *field)
+{
+       struct triple *result;
+       struct type *type, *member;
+       if (!field) {
+               internal_error(state, 0, "No field passed to deref_field");
+       }
+       result = 0;
+       type = expr->type;
+       if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
+               error(state, 0, "request for member %s in something not a struct or union",
+                       field->name);
+       }
+       member = field_type(state, type, field);
+       if ((type->type & STOR_MASK) == STOR_PERM) {
+               /* Do the pointer arithmetic to get a deref the field */
+               ulong_t offset;
+               offset = field_offset(state, type, field);
+               result = do_mk_addr_expr(state, expr, member, offset);
+               result = mk_deref_expr(state, result);
+       }
+       else {
+               /* Find the variable for the field I want. */
+               result = triple(state, OP_DOT, member, expr, 0);
+               result->u.field = field;
+       }
+       return result;
+}
+
 static struct triple *read_expr(struct compile_state *state, struct triple *def)
 {
        int op;
        if  (!def) {
                return 0;
        }
-       if (!is_stable(def)) {
+       if (!is_stable(state, def)) {
                return def;
        }
        /* Tranform an array to a pointer to the first element */
+       
 #warning "CHECK_ME is this the right place to transform arrays to pointers?"
        if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
-               struct type *type;
-               type = new_type(
-                       TYPE_POINTER | (def->type->type & QUAL_MASK),
-                       def->type->left, 0);
-               return triple(state, OP_ADDRCONST, type, def, 0);
+               return array_to_pointer(state, def);
        }
-       /* Only values in variables need to be read */
-       if (def->op == OP_ADECL) {
+       if (is_in_reg(state, def)) {
                op = OP_READ;
-       }
-       else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
+       } else {
                op = OP_LOAD;
        }
-       else {
-               internal_error(state, 0, "unhandled read expr type");
-               op = -1;
-       }
-       return  triple(state, op, def->type, def, 0);
+       return triple(state, op, def->type, def, 0);
 }
 
 static void write_compatible(struct compile_state *state,
@@ -4005,6 +4659,12 @@ static void write_compatible(struct compile_state *state,
        else if (equiv_ptrs(dest, rval)) {
                compatible = 1;
        }
+       /* test for struct/union equality  */
+       else if (((dest->type & TYPE_MASK) == TYPE_STRUCT) &&
+               ((rval->type & TYPE_MASK) == TYPE_STRUCT) &&
+               (dest->type_ident == rval->type_ident)) {
+               compatible = 1;
+       }
        if (!compatible) {
                error(state, 0, "Incompatible types in assignment");
        }
@@ -4024,24 +4684,22 @@ static struct triple *write_expr(
        if (rval->op == OP_LIST) {
                internal_error(state, 0, "expression of type OP_LIST?");
        }
-       if (!is_lvalue(dest)) {
+       if (!is_lvalue(state, dest)) {
                internal_error(state, 0, "writing to a non lvalue?");
        }
+       if (dest->type->type & QUAL_CONST) {
+               internal_error(state, 0, "modifable lvalue expexted");
+       }
 
        write_compatible(state, dest->type, rval->type);
 
        /* Now figure out which assignment operator to use */
        op = -1;
-       if (dest->op == OP_ADECL) {
+       if (is_in_reg(state, dest)) {
                op = OP_WRITE;
-       }
-       else if ((dest->op == OP_SDECL) || (dest->op == OP_DEREF)) {
+       } else {
                op = OP_STORE;
        }
-       else {
-               internal_error(state, 0, "unimplemented lvalue type");
-       }
-#warning "FIXME walk through a list of OP_DOT entries and generate a pointer addition"
        def = triple(state, op, dest->type, dest, rval);
        return def;
 }
@@ -4070,7 +4728,10 @@ static struct triple *init_expr(
                if (!equiv_types(dest->type, rval->type)) {
                        error(state, 0, "Incompatible types in inializer");
                }
-               dest->left = rval;
+               MISC(dest, 0) = rval;
+               insert_triple(state, dest, rval);
+               rval->id |= TRIPLE_FLAG_FLATTENED;
+               use_triple(MISC(dest, 0), dest);
        }
        return def;
 }
@@ -4174,49 +4835,49 @@ static struct triple *cond_expr(
        if (!result_type) {
                error(state, 0, "Incompatible types in conditional expression");
        }
-       def = triple(state, OP_COND, result_type, test,
-               triple(state, OP_PRODUCT, &void_type, left, right));
+       /* Cleanup and invert the test */
+       test = lfalse_expr(state, read_expr(state, test));
+       def = new_triple(state, OP_COND, result_type, 0, 3);
+       def->param[0] = test;
+       def->param[1] = left;
+       def->param[2] = right;
        return def;
 }
 
 
-static int expr_depth(struct compile_state *state, struct triple *triple)
+static int expr_depth(struct compile_state *state, struct triple *ins)
 {
        int count;
        count = 0;
-       if (!triple) {
-               return 0;
-       }
-       /* All of the internal helper ops that are not removed by
-        * flatten must be present here.
-        */
-       if (triple->op == OP_READ) {
-               ;
+       if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
+               count = 0;
        }
-       else if (triple->op == OP_DEREF) {
-               count = expr_depth(state, triple->left) - 1;
+       else if (ins->op == OP_DEREF) {
+               count = expr_depth(state, RHS(ins, 0)) - 1;
        }
-       else if (triple->op == OP_VAL) {
-               count = expr_depth(state, triple->left) - 1;
+       else if (ins->op == OP_VAL) {
+               count = expr_depth(state, RHS(ins, 0)) - 1;
        }
-       else if (triple->op == OP_COMMA) {
-               int left, right;
-               left = expr_depth(state, triple->left);
-               right = expr_depth(state, triple->right);
-               count = (left >= right)? left : right;
+       else if (ins->op == OP_COMMA) {
+               int ldepth, rdepth;
+               ldepth = expr_depth(state, RHS(ins, 0));
+               rdepth = expr_depth(state, RHS(ins, 1));
+               count = (ldepth >= rdepth)? ldepth : rdepth;
        }
-       else if (triple->op == OP_CALL) {
+       else if (ins->op == OP_CALL) {
                /* Don't figure the depth of a call just guess it is huge */
                count = 1000;
        }
        else {
                struct triple **expr;
-               expr = triple_rhs(state, triple, 0);
-               for(;expr; expr = triple_rhs(state, triple, expr)) {
-                       int depth;
-                       depth = expr_depth(state, *expr);
-                       if (depth > count) {
-                               count = depth;
+               expr = triple_rhs(state, ins, 0);
+               for(;expr; expr = triple_rhs(state, ins, expr)) {
+                       if (*expr) {
+                               int depth;
+                               depth = expr_depth(state, *expr);
+                               if (depth > count) {
+                                       count = depth;
+                               }
                        }
                }
        }
@@ -4226,44 +4887,52 @@ static int expr_depth(struct compile_state *state, struct triple *triple)
 static struct triple *flatten(
        struct compile_state *state, struct triple *first, struct triple *ptr);
 
-static struct triple *flatten_rhs(
+static struct triple *flatten_generic(
        struct compile_state *state, struct triple *first, struct triple *ptr)
 {
-       struct triple **left, **right, **last;
-       /* Only operations with a rhs should come here */
-       last = triple_lhs(state, ptr, 0);
-       if (last) {
-               internal_error(state, ptr, "unexpected rhs for: %d %s",
+       struct rhs_vector {
+               int depth;
+               struct triple **ins;
+       } vector[MAX_RHS];
+       int i, rhs, lhs;
+       /* Only operations with just a rhs should come here */
+       rhs = TRIPLE_RHS(ptr->sizes);
+       lhs = TRIPLE_LHS(ptr->sizes);
+       if (TRIPLE_SIZE(ptr->sizes) != lhs + rhs) {
+               internal_error(state, ptr, "unexpected args for: %d %s",
                        ptr->op, tops(ptr->op));
        }
-       /* Collect up the rhs */
-       left  = triple_rhs(state, ptr, 0);
-       right = last = 0;
-       if (left) {
-               right = triple_rhs(state, ptr, left);
-       }
-       if (right) {
-               last  = triple_rhs(state, ptr, right);
-       }
-       if (last) {
-               internal_error(state, ptr, "too many rhs arguments for: %d %s",
-                       ptr->op, tops(ptr->op));
-       }
-       if (left && right) {
-               if (expr_depth(state, *left) >= expr_depth(state, *right)) {
-                       *left  = flatten(state, first, *left);
-                       *right = flatten(state, first, *right);
+       /* Find the depth of the rhs elements */
+       for(i = 0; i < rhs; i++) {
+               vector[i].ins = &RHS(ptr, i);
+               vector[i].depth = expr_depth(state, *vector[i].ins);
+       }
+       /* Selection sort the rhs */
+       for(i = 0; i < rhs; i++) {
+               int j, max = i;
+               for(j = i + 1; j < rhs; j++ ) {
+                       if (vector[j].depth > vector[max].depth) {
+                               max = j;
+                       }
                }
-               else {
-                       *right = flatten(state, first, *right);
-                       *left  = flatten(state, first, *left);
+               if (max != i) {
+                       struct rhs_vector tmp;
+                       tmp = vector[i];
+                       vector[i] = vector[max];
+                       vector[max] = tmp;
                }
-               use_triple(*left, ptr);
-               use_triple(*right, ptr);
        }
-       else if (left) {
-               *left = flatten(state, first, *left);
-               use_triple(*left, ptr);
+       /* Now flatten the rhs elements */
+       for(i = 0; i < rhs; i++) {
+               *vector[i].ins = flatten(state, first, *vector[i].ins);
+               use_triple(*vector[i].ins, ptr);
+       }
+       
+       /* Now flatten the lhs elements */
+       for(i = 0; i < lhs; i++) {
+               struct triple **ins = &LHS(ptr, i);
+               *ins = flatten(state, first, *ins);
+               use_triple(*ins, ptr);
        }
        return ptr;
 }
@@ -4275,23 +4944,21 @@ static struct triple *flatten_land(
        struct triple *val, *test, *jmp, *label1, *end;
 
        /* Find the triples */
-       left = ptr->left;
-       right = ptr->right;
+       left = RHS(ptr, 0);
+       right = RHS(ptr, 1);
 
        /* Generate the needed triples */
        end = label(state);
 
        /* Thread the triples together */
-       val         = flatten(state, first, variable(state, ptr->type));
-       left        = flatten(state, first, write_expr(state, val, left));
-       test        = flatten(state, first, 
+       val          = flatten(state, first, variable(state, ptr->type));
+       left         = flatten(state, first, write_expr(state, val, left));
+       test         = flatten(state, first, 
                lfalse_expr(state, read_expr(state, val)));
-       jmp         = flatten(state, first, 
-               triple(state, OP_BRANCH, &void_type, end, test));
-       label1      = flatten(state, first, label(state));
-       right       = flatten(state, first, write_expr(state, val, right));
-       jmp->left   = flatten(state, first, end);
-       
+       jmp          = flatten(state, first, branch(state, end, test));
+       label1       = flatten(state, first, label(state));
+       right        = flatten(state, first, write_expr(state, val, right));
+       TARG(jmp, 0) = flatten(state, first, end); 
        
        /* Now give the caller something to chew on */
        return read_expr(state, val);
@@ -4304,20 +4971,19 @@ static struct triple *flatten_lor(
        struct triple *val, *jmp, *label1, *end;
 
        /* Find the triples */
-       left = ptr->left;
-       right = ptr->right;
+       left = RHS(ptr, 0);
+       right = RHS(ptr, 1);
 
        /* Generate the needed triples */
        end = label(state);
 
        /* Thread the triples together */
-       val         = flatten(state, first, variable(state, ptr->type));
-       left        = flatten(state, first, write_expr(state, val, left));
-       jmp         = flatten(state, first, 
-               triple(state, OP_BRANCH, &void_type, end, left));
-       label1      = flatten(state, first, label(state));
-       right       = flatten(state, first, write_expr(state, val, right));
-       jmp->left   = flatten(state, first, end);
+       val          = flatten(state, first, variable(state, ptr->type));
+       left         = flatten(state, first, write_expr(state, val, left));
+       jmp          = flatten(state, first, branch(state, end, left));
+       label1       = flatten(state, first, label(state));
+       right        = flatten(state, first, write_expr(state, val, right));
+       TARG(jmp, 0) = flatten(state, first, end);
        
        
        /* Now give the caller something to chew on */
@@ -4329,40 +4995,35 @@ static struct triple *flatten_cond(
 {
        struct triple *test, *left, *right;
        struct triple *val, *mv1, *jmp1, *label1, *mv2, *middle, *jmp2, *end;
-       if (ptr->right->op != OP_PRODUCT) {
-               internal_error(state, 0, "Improper conditional expression");
-       }
 
        /* Find the triples */
-       test = ptr->left;
-       left = ptr->right->left;
-       right = ptr->right->right;
+       test = RHS(ptr, 0);
+       left = RHS(ptr, 1);
+       right = RHS(ptr, 2);
 
        /* Generate the needed triples */
        end = label(state);
        middle = label(state);
 
        /* Thread the triples together */
-       val         = flatten(state, first, variable(state, ptr->type));
-       test        = flatten(state, first, test);
-       jmp1        = flatten(state, first, 
-               triple(state, OP_BRANCH, &void_type, middle, test));
-       label1      = flatten(state, first, label(state));
-       left        = flatten(state, first, left);
-       mv1         = flatten(state, first, write_expr(state, val, left));
-       jmp2        = flatten(state, first, 
-               triple(state, OP_BRANCH, &void_type, end, 0));
-       jmp1->left  = flatten(state, first, middle);
-       right       = flatten(state, first, right);
-       mv2         = flatten(state, first, write_expr(state, val, right));
-       jmp2->left  = flatten(state, first, end);
-       
+       val           = flatten(state, first, variable(state, ptr->type));
+       test          = flatten(state, first, test);
+       jmp1          = flatten(state, first, branch(state, middle, test));
+       label1        = flatten(state, first, label(state));
+       left          = flatten(state, first, left);
+       mv1           = flatten(state, first, write_expr(state, val, left));
+       jmp2          = flatten(state, first, branch(state, end, 0));
+       TARG(jmp1, 0) = flatten(state, first, middle);
+       right         = flatten(state, first, right);
+       mv2           = flatten(state, first, write_expr(state, val, right));
+       TARG(jmp2, 0) = flatten(state, first, end);
        
        /* Now give the caller something to chew on */
        return read_expr(state, val);
 }
 
-struct triple *copy_func(struct compile_state *state, struct triple *ofunc)
+struct triple *copy_func(struct compile_state *state, struct triple *ofunc, 
+       struct occurance *base_occurance)
 {
        struct triple *nfunc;
        struct triple *nfirst, *ofirst;
@@ -4379,31 +5040,33 @@ struct triple *copy_func(struct compile_state *state, struct triple *ofunc)
        /* Make a new copy of the old function */
        nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
        nfirst = 0;
-       ofirst = old = ofunc->left;
+       ofirst = old = RHS(ofunc, 0);
        do {
                struct triple *new;
-               new = build_triple(state, old->op, old->type, 0, 0, 
-                       old->filename, old->line, old->col);
-               if (IS_CONST_OP(new->op)) {
+               struct occurance *occurance;
+               int old_lhs, old_rhs;
+               old_lhs = TRIPLE_LHS(old->sizes);
+               old_rhs = TRIPLE_RHS(old->sizes);
+               occurance = inline_occurance(state, base_occurance, old->occurance);
+               new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
+                       occurance);
+               if (!triple_stores_block(state, new)) {
                        memcpy(&new->u, &old->u, sizeof(new->u));
                }
-#warning "WISHLIST find a way to handle SDECL without a special case..."
-               else if (new->op == OP_SDECL) {
-                       new->left = old->left;
-               }
                if (!nfirst) {
-                       nfunc->left = nfirst = new;
+                       RHS(nfunc, 0) = nfirst = new;
                }
                else {
                        insert_triple(state, nfirst, new);
                }
+               new->id |= TRIPLE_FLAG_FLATTENED;
                
                /* During the copy remember new as user of old */
                use_triple(old, new);
 
                /* Populate the return type if present */
-               if (old == ofunc->right) {
-                       nfunc->right = new;
+               if (old == MISC(ofunc, 0)) {
+                       MISC(nfunc, 0) = new;
                }
                old = old->next;
        } while(old != ofirst);
@@ -4412,36 +5075,22 @@ struct triple *copy_func(struct compile_state *state, struct triple *ofunc)
        old = ofirst;
        new = nfirst;
        do {
+               struct triple **oexpr, **nexpr;
+               int count, i;
                /* Lookup where the copy is, to join pointers */
-               if (!new->left && old->left && old->left->use) {
-                       new->left = old->left->use->member;
-                       if (new->left == old) {
-                               internal_error(state, 0, "new == old?");
-                       }
-               }
-               if (!new->right && old->right && old->right->use) {
-                       new->right = old->right->use->member;
-                       if (new->right == old) {
-                               internal_error(state, 0, "new == old?");
+               count = TRIPLE_SIZE(old->sizes);
+               for(i = 0; i < count; i++) {
+                       oexpr = &old->param[i];
+                       nexpr = &new->param[i];
+                       if (!*nexpr && *oexpr && (*oexpr)->use) {
+                               *nexpr = (*oexpr)->use->member;
+                               if (*nexpr == old) {
+                                       internal_error(state, 0, "new == old?");
+                               }
+                               use_triple(*nexpr, new);
                        }
-               }
-               if (!new->left && old->left) {
-                       internal_error(state, 0, "Could not copy left");
-               }
-               if (!new->right && old->right) {
-                       internal_error(state, 0, "Could not copy right");
-               }
-               if (new->op != old->op) {
-                       internal_error(state, 0, "Could not copy op?");
-               }
-               if (!new->next && old->next) {
-                       internal_error(state, 0, "Could not copy next");
-               }
-               use_triple(new->left, new);
-               use_triple(new->right, new);
-               if (new->op == OP_BRANCH) {
-                       if (new->right) {
-                               use_triple(new->next, new);
+                       if (!*nexpr && *oexpr) {
+                               internal_error(state, 0, "Could not copy %d\n", i);
                        }
                }
                old = old->next;
@@ -4463,35 +5112,40 @@ static struct triple *flatten_call(
        struct compile_state *state, struct triple *first, struct triple *ptr)
 {
        /* Inline the function call */
-       struct triple *ofunc, *nfunc, *nfirst, *args, *param, *result;
+       struct type *ptype;
+       struct triple *ofunc, *nfunc, *nfirst, *param, *result;
        struct triple *end, *nend;
-       int done;
+       int pvals, i;
 
        /* Find the triples */
-       ofunc = ptr->left;
-       args  = ptr->right;
+       ofunc = MISC(ptr, 0);
        if (ofunc->op != OP_LIST) {
                internal_error(state, 0, "improper function");
        }
-       nfunc = copy_func(state, ofunc);
-       nfirst = nfunc->left->next;
-       param = nfunc->left->next;
+       nfunc = copy_func(state, ofunc, ptr->occurance);
+       nfirst = RHS(nfunc, 0)->next;
        /* Prepend the parameter reading into the new function list */
-       while(args) {
+       ptype = nfunc->type->right;
+       param = RHS(nfunc, 0)->next;
+       pvals = TRIPLE_RHS(ptr->sizes);
+       for(i = 0; i < pvals; i++) {
+               struct type *atype;
                struct triple *arg;
-               arg = args;
-               done = 1;
-               if (args->op == OP_PRODUCT) {
-                       arg = args->left;
+               atype = ptype;
+               if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
+                       atype = ptype->left;
                }
-               flatten(state, nfirst,
-                       write_expr(state, param, arg));
+               while((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
+                       param = param->next;
+               }
+               arg = RHS(ptr, i);
+               flatten(state, nfirst, write_expr(state, param, arg));
+               ptype = ptype->right;
                param = param->next;
-               args = (args->op == OP_PRODUCT)? args->right : 0;
-       } 
+       }
        result = 0;
        if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
-               result = read_expr(state, nfunc->right);
+               result = read_expr(state, MISC(nfunc,0));
        }
 #if 0
        fprintf(stdout, "\n");
@@ -4502,9 +5156,9 @@ static struct triple *flatten_call(
 #endif
 
        /* Get rid of the extra triples */
-       nfirst = nfunc->left->next;
-       free_triple(state, nfunc->left); 
-       nfunc->left = 0;
+       nfirst = RHS(nfunc, 0)->next;
+       free_triple(state, RHS(nfunc, 0));
+       RHS(nfunc, 0) = 0;
        free_triple(state, nfunc);
 
        /* Append the new function list onto the return list */
@@ -4526,21 +5180,18 @@ static struct triple *flatten(
                return 0;
        do {
                orig_ptr = ptr;
+               /* Only flatten triples once */
+               if (ptr->id & TRIPLE_FLAG_FLATTENED) {
+                       return ptr;
+               }
                switch(ptr->op) {
-               case OP_WRITE:
-               case OP_STORE:
-                       ptr->right = flatten(state, first, ptr->right);
-                       ptr->left = flatten(state, first, ptr->left);
-                       use_triple(ptr->left, ptr);
-                       use_triple(ptr->right, ptr);
-                       break;
                case OP_COMMA:
-                       ptr->left = flatten(state, first, ptr->left);
-                       ptr = ptr->right;
+                       RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
+                       ptr = RHS(ptr, 1);
                        break;
                case OP_VAL:
-                       ptr->left = flatten(state, first, ptr->left);
-                       return ptr->right;
+                       RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
+                       return MISC(ptr, 0);
                        break;
                case OP_LAND:
                        ptr = flatten_land(state, first, ptr);
@@ -4556,49 +5207,72 @@ static struct triple *flatten(
                        break;
                case OP_READ:
                case OP_LOAD:
-                       ptr->left = flatten(state, first, ptr->left);
-                       use_triple(ptr->left, ptr);
+                       RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
+                       use_triple(RHS(ptr, 0), ptr);
                        break;
                case OP_BRANCH:
-                       use_triple(ptr->left, ptr);
-                       use_triple(ptr->right, ptr);
-                       if (ptr->next != ptr) {
-                               use_triple(ptr->next, ptr);
+                       use_triple(TARG(ptr, 0), ptr);
+                       if (TRIPLE_RHS(ptr->sizes)) {
+                               use_triple(RHS(ptr, 0), ptr);
+                               if (ptr->next != ptr) {
+                                       use_triple(ptr->next, ptr);
+                               }
                        }
                        break;
-               case OP_ADDRCONST:
-                       ptr->left = flatten(state, first, ptr->left);
-                       use_triple(ptr->left, ptr);
-                       break;
                case OP_BLOBCONST:
+                       insert_triple(state, first, ptr);
+                       ptr->id |= TRIPLE_FLAG_FLATTENED;
                        ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
-                       use_triple(ptr->left, ptr);
+                       use_triple(MISC(ptr, 0), ptr);
                        break;
                case OP_DEREF:
                        /* Since OP_DEREF is just a marker delete it when I flatten it */
-                       ptr = ptr->left;
-                       orig_ptr->left = 0;
+                       ptr = RHS(ptr, 0);
+                       RHS(orig_ptr, 0) = 0;
                        free_triple(state, orig_ptr);
                        break;
-               case OP_PRODUCT:
                case OP_DOT:
-                       internal_error(state, 0, "unknown expression type: %d %s",
-                               ptr->op, tops(ptr->op));
+               {
+                       struct triple *base;
+                       base = RHS(ptr, 0);
+                       if (base->op == OP_DEREF) {
+                               struct triple *left;
+                               ulong_t offset;
+                               offset = field_offset(state, base->type, ptr->u.field);
+                               left = RHS(base, 0);
+                               ptr = triple(state, OP_ADD, left->type, 
+                                       read_expr(state, left),
+                                       int_const(state, &ulong_type, offset));
+                               free_triple(state, base);
+                       }
+                       else if (base->op == OP_VAL_VEC) {
+                               base = flatten(state, first, base);
+                               ptr = struct_field(state, base, ptr->u.field);
+                       }
+                       break;
+               }
+               case OP_PIECE:
+                       MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
+                       use_triple(MISC(ptr, 0), ptr);
+                       use_triple(ptr, MISC(ptr, 0));
                        break;
+               case OP_ADDRCONST:
                case OP_SDECL:
+                       MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
+                       use_triple(MISC(ptr, 0), ptr);
+                       break;
                case OP_ADECL:
-                       /* Don't flatten already flattened decls */
-                       if ((ptr->next != ptr) || (ptr->prev != ptr)) {
-                               return ptr;
-                       }
                        break;
                default:
                        /* Flatten the easy cases we don't override */
-                       ptr = flatten_rhs(state, first, ptr);
+                       ptr = flatten_generic(state, first, ptr);
                        break;
                }
        } while(ptr && (ptr != orig_ptr));
-       insert_triple(state, first, ptr);
+       if (ptr) {
+               insert_triple(state, first, ptr);
+               ptr->id |= TRIPLE_FLAG_FLATTENED;
+       }
        return ptr;
 }
 
@@ -4690,9 +5364,9 @@ static struct triple *mk_add_expr(
                left = right;
                right = tmp;
        }
-       result_type = ptr_arithmetic_result(state, left, right);
        left  = read_expr(state, left);
        right = read_expr(state, right);
+       result_type = ptr_arithmetic_result(state, left, right);
        if (is_pointer(left)) {
                right = triple(state, 
                        is_signed(right->type)? OP_SMUL : OP_UMUL, 
@@ -4823,7 +5497,7 @@ static int constants_equal(struct compile_state *state,
                        break;
                }
                case OP_ADDRCONST:
-                       if ((left->left == right->left) &&
+                       if ((MISC(left, 0) == MISC(right, 0)) &&
                                (left->u.cval == right->u.cval)) {
                                equal = 1;
                        }
@@ -4846,6 +5520,22 @@ static int is_one(struct triple *ins)
        return is_const(ins) && (ins->u.cval == 1);
 }
 
+static long_t bit_count(ulong_t value)
+{
+       int count;
+       int i;
+       count = 0;
+       for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
+               ulong_t mask;
+               mask = 1;
+               mask <<= i;
+               if (value & mask) {
+                       count++;
+               }
+       }
+       return count;
+       
+}
 static long_t bsr(ulong_t value)
 {
        int i;
@@ -4936,10 +5626,23 @@ static void unuse_rhs(struct compile_state *state, struct triple *ins)
        struct triple **expr;
        expr = triple_rhs(state, ins, 0);
        for(;expr;expr = triple_rhs(state, ins, expr)) {
+               if (*expr) {
+                       unuse_triple(*expr, ins);
+                       *expr = 0;
+               }
+       }
+}
+
+static void unuse_lhs(struct compile_state *state, struct triple *ins)
+{
+       struct triple **expr;
+       expr = triple_lhs(state, ins, 0);
+       for(;expr;expr = triple_lhs(state, ins, expr)) {
                unuse_triple(*expr, ins);
                *expr = 0;
        }
 }
+
 static void check_lhs(struct compile_state *state, struct triple *ins)
 {
        struct triple **expr;
@@ -4960,13 +5663,13 @@ static void check_targ(struct compile_state *state, struct triple *ins)
 
 static void wipe_ins(struct compile_state *state, struct triple *ins)
 {
-       check_lhs(state, ins);
+       /* Becareful which instructions you replace the wiped
+        * instruction with, as there are not enough slots
+        * in all instructions to hold all others.
+        */
        check_targ(state, ins);
        unuse_rhs(state, ins);
-       if (ins->op == OP_PHI) {
-               xfree(ins->left);
-               ins->left = 0;
-       }
+       unuse_lhs(state, ins);
 }
 
 static void mkcopy(struct compile_state *state, 
@@ -4974,8 +5677,9 @@ static void mkcopy(struct compile_state *state,
 {
        wipe_ins(state, ins);
        ins->op = OP_COPY;
-       ins->left = rhs;
-       use_triple(ins->left, ins);
+       ins->sizes = TRIPLE_SIZES(0, 1, 0, 0);
+       RHS(ins, 0) = rhs;
+       use_triple(RHS(ins, 0), ins);
 }
 
 static void mkconst(struct compile_state *state, 
@@ -4986,19 +5690,142 @@ static void mkconst(struct compile_state *state,
        }
        wipe_ins(state, ins);
        ins->op = OP_INTCONST;
+       ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
        ins->u.cval = value;
 }
 
 static void mkaddr_const(struct compile_state *state,
        struct triple *ins, struct triple *sdecl, ulong_t value)
 {
+       if (sdecl->op != OP_SDECL) {
+               internal_error(state, ins, "bad base for addrconst");
+       }
        wipe_ins(state, ins);
        ins->op = OP_ADDRCONST;
-       ins->left = sdecl;
+       ins->sizes = TRIPLE_SIZES(0, 0, 1, 0);
+       MISC(ins, 0) = sdecl;
        ins->u.cval = value;
        use_triple(sdecl, ins);
 }
 
+/* Transform multicomponent variables into simple register variables */
+static void flatten_structures(struct compile_state *state)
+{
+       struct triple *ins, *first;
+       first = RHS(state->main_function, 0);
+       ins = first;
+       /* Pass one expand structure values into valvecs.
+        */
+       ins = first;
+       do {
+               struct triple *next;
+               next = ins->next;
+               if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
+                       if (ins->op == OP_VAL_VEC) {
+                               /* Do nothing */
+                       }
+                       else if ((ins->op == OP_LOAD) || (ins->op == OP_READ)) {
+                               struct triple *def, **vector;
+                               struct type *tptr;
+                               int op;
+                               ulong_t i;
+
+                               op = ins->op;
+                               def = RHS(ins, 0);
+                               get_occurance(ins->occurance);
+                               next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
+                                       ins->occurance);
+
+                               vector = &RHS(next, 0);
+                               tptr = next->type->left;
+                               for(i = 0; i < next->type->elements; i++) {
+                                       struct triple *sfield;
+                                       struct type *mtype;
+                                       mtype = tptr;
+                                       if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
+                                               mtype = mtype->left;
+                                       }
+                                       sfield = deref_field(state, def, mtype->field_ident);
+                                       
+                                       vector[i] = triple(
+                                               state, op, mtype, sfield, 0);
+                                       put_occurance(vector[i]->occurance);
+                                       get_occurance(next->occurance);
+                                       vector[i]->occurance = next->occurance;
+                                       tptr = tptr->right;
+                               }
+                               propogate_use(state, ins, next);
+                               flatten(state, ins, next);
+                               free_triple(state, ins);
+                       }
+                       else if ((ins->op == OP_STORE) || (ins->op == OP_WRITE)) {
+                               struct triple *src, *dst, **vector;
+                               struct type *tptr;
+                               int op;
+                               ulong_t i;
+
+                               op = ins->op;
+                               src = RHS(ins, 1);
+                               dst = RHS(ins, 0);
+                               get_occurance(ins->occurance);
+                               next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
+                                       ins->occurance);
+                               
+                               vector = &RHS(next, 0);
+                               tptr = next->type->left;
+                               for(i = 0; i < ins->type->elements; i++) {
+                                       struct triple *dfield, *sfield;
+                                       struct type *mtype;
+                                       mtype = tptr;
+                                       if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
+                                               mtype = mtype->left;
+                                       }
+                                       sfield = deref_field(state, src, mtype->field_ident);
+                                       dfield = deref_field(state, dst, mtype->field_ident);
+                                       vector[i] = triple(
+                                               state, op, mtype, dfield, sfield);
+                                       put_occurance(vector[i]->occurance);
+                                       get_occurance(next->occurance);
+                                       vector[i]->occurance = next->occurance;
+                                       tptr = tptr->right;
+                               }
+                               propogate_use(state, ins, next);
+                               flatten(state, ins, next);
+                               free_triple(state, ins);
+                       }
+               }
+               ins = next;
+       } while(ins != first);
+       /* Pass two flatten the valvecs.
+        */
+       ins = first;
+       do {
+               struct triple *next;
+               next = ins->next;
+               if (ins->op == OP_VAL_VEC) {
+                       release_triple(state, ins);
+               } 
+               ins = next;
+       } while(ins != first);
+       /* Pass three verify the state and set ->id to 0.
+        */
+       ins = first;
+       do {
+               ins->id &= ~TRIPLE_FLAG_FLATTENED;
+               if ((ins->op != OP_BLOBCONST) && (ins->op != OP_SDECL) &&
+                       ((ins->type->type & TYPE_MASK) == TYPE_STRUCT)) {
+                       internal_error(state, ins, "STRUCT_TYPE remains?");
+               }
+               if (ins->op == OP_DOT) {
+                       internal_error(state, ins, "OP_DOT remains?");
+               }
+               if (ins->op == OP_VAL_VEC) {
+                       internal_error(state, ins, "OP_VAL_VEC remains?");
+               }
+               ins = ins->next;
+       } while(ins != first);
+}
+
 /* For those operations that cannot be simplified */
 static void simplify_noop(struct compile_state *state, struct triple *ins)
 {
@@ -5007,422 +5834,428 @@ static void simplify_noop(struct compile_state *state, struct triple *ins)
 
 static void simplify_smul(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && !is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
                struct triple *tmp;
-               tmp = ins->left;
-               ins->left = ins->right;
-               ins->right = tmp;
+               tmp = RHS(ins, 0);
+               RHS(ins, 0) = RHS(ins, 1);
+               RHS(ins, 1) = tmp;
        }
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                long_t left, right;
-               left  = read_sconst(ins, &ins->left);
-               right = read_sconst(ins, &ins->right);
+               left  = read_sconst(ins, &RHS(ins, 0));
+               right = read_sconst(ins, &RHS(ins, 1));
                mkconst(state, ins, left * right);
        }
-       else if (is_zero(ins->right)) {
+       else if (is_zero(RHS(ins, 1))) {
                mkconst(state, ins, 0);
        }
-       else if (is_one(ins->right)) {
-               mkcopy(state, ins, ins->left);
+       else if (is_one(RHS(ins, 1))) {
+               mkcopy(state, ins, RHS(ins, 0));
        }
-       else if (is_pow2(ins->right)) {
+       else if (is_pow2(RHS(ins, 1))) {
                struct triple *val;
-               val = int_const(state, ins->type, tlog2(ins->right));
+               val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
                ins->op = OP_SL;
                insert_triple(state, ins, val);
-               unuse_triple(ins->right, ins);
+               unuse_triple(RHS(ins, 1), ins);
                use_triple(val, ins);
-               ins->right = val;
+               RHS(ins, 1) = val;
        }
 }
 
 static void simplify_umul(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && !is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
                struct triple *tmp;
-               tmp = ins->left;
-               ins->left = ins->right;
-               ins->right = tmp;
+               tmp = RHS(ins, 0);
+               RHS(ins, 0) = RHS(ins, 1);
+               RHS(ins, 1) = tmp;
        }
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                ulong_t left, right;
-               left  = read_const(state, ins, &ins->left);
-               right = read_const(state, ins, &ins->right);
+               left  = read_const(state, ins, &RHS(ins, 0));
+               right = read_const(state, ins, &RHS(ins, 1));
                mkconst(state, ins, left * right);
        }
-       else if (is_zero(ins->right)) {
+       else if (is_zero(RHS(ins, 1))) {
                mkconst(state, ins, 0);
        }
-       else if (is_one(ins->right)) {
-               mkcopy(state, ins, ins->left);
+       else if (is_one(RHS(ins, 1))) {
+               mkcopy(state, ins, RHS(ins, 0));
        }
-       else if (is_pow2(ins->right)) {
+       else if (is_pow2(RHS(ins, 1))) {
                struct triple *val;
-               val = int_const(state, ins->type, tlog2(ins->right));
+               val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
                ins->op = OP_SL;
                insert_triple(state, ins, val);
-               unuse_triple(ins->right, ins);
+               unuse_triple(RHS(ins, 1), ins);
                use_triple(val, ins);
-               ins->right = val;
+               RHS(ins, 1) = val;
        }
 }
 
 static void simplify_sdiv(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                long_t left, right;
-               left  = read_sconst(ins, &ins->left);
-               right = read_sconst(ins, &ins->right);
+               left  = read_sconst(ins, &RHS(ins, 0));
+               right = read_sconst(ins, &RHS(ins, 1));
                mkconst(state, ins, left / right);
        }
-       else if (is_zero(ins->left)) {
+       else if (is_zero(RHS(ins, 0))) {
                mkconst(state, ins, 0);
        }
-       else if (is_zero(ins->right)) {
+       else if (is_zero(RHS(ins, 1))) {
                error(state, ins, "division by zero");
        }
-       else if (is_one(ins->right)) {
-               mkcopy(state, ins, ins->left);
+       else if (is_one(RHS(ins, 1))) {
+               mkcopy(state, ins, RHS(ins, 0));
        }
-       else if (is_pow2(ins->right)) {
+       else if (is_pow2(RHS(ins, 1))) {
                struct triple *val;
-               val = int_const(state, ins->type, tlog2(ins->right));
+               val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
                ins->op = OP_SSR;
                insert_triple(state, ins, val);
-               unuse_triple(ins->right, ins);
+               unuse_triple(RHS(ins, 1), ins);
                use_triple(val, ins);
-               ins->right = val;
+               RHS(ins, 1) = val;
        }
 }
 
 static void simplify_udiv(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                ulong_t left, right;
-               left  = read_const(state, ins, &ins->left);
-               right = read_const(state, ins, &ins->right);
+               left  = read_const(state, ins, &RHS(ins, 0));
+               right = read_const(state, ins, &RHS(ins, 1));
                mkconst(state, ins, left / right);
        }
-       else if (is_zero(ins->left)) {
+       else if (is_zero(RHS(ins, 0))) {
                mkconst(state, ins, 0);
        }
-       else if (is_zero(ins->right)) {
+       else if (is_zero(RHS(ins, 1))) {
                error(state, ins, "division by zero");
        }
-       else if (is_one(ins->right)) {
-               mkcopy(state, ins, ins->left);
+       else if (is_one(RHS(ins, 1))) {
+               mkcopy(state, ins, RHS(ins, 0));
        }
-       else if (is_pow2(ins->right)) {
+       else if (is_pow2(RHS(ins, 1))) {
                struct triple *val;
-               val = int_const(state, ins->type, tlog2(ins->right));
+               val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
                ins->op = OP_USR;
                insert_triple(state, ins, val);
-               unuse_triple(ins->right, ins);
+               unuse_triple(RHS(ins, 1), ins);
                use_triple(val, ins);
-               ins->right = val;
+               RHS(ins, 1) = val;
        }
 }
 
 static void simplify_smod(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                long_t left, right;
-               left  = read_const(state, ins, &ins->left);
-               right = read_const(state, ins, &ins->right);
+               left  = read_const(state, ins, &RHS(ins, 0));
+               right = read_const(state, ins, &RHS(ins, 1));
                mkconst(state, ins, left % right);
        }
-       else if (is_zero(ins->left)) {
+       else if (is_zero(RHS(ins, 0))) {
                mkconst(state, ins, 0);
        }
-       else if (is_zero(ins->right)) {
+       else if (is_zero(RHS(ins, 1))) {
                error(state, ins, "division by zero");
        }
-       else if (is_one(ins->right)) {
+       else if (is_one(RHS(ins, 1))) {
                mkconst(state, ins, 0);
        }
-       else if (is_pow2(ins->right)) {
+       else if (is_pow2(RHS(ins, 1))) {
                struct triple *val;
-               val = int_const(state, ins->type, ins->right->u.cval - 1);
+               val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
                ins->op = OP_AND;
                insert_triple(state, ins, val);
-               unuse_triple(ins->right, ins);
+               unuse_triple(RHS(ins, 1), ins);
                use_triple(val, ins);
-               ins->right = val;
+               RHS(ins, 1) = val;
        }
 }
 static void simplify_umod(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                ulong_t left, right;
-               left  = read_const(state, ins, &ins->left);
-               right = read_const(state, ins, &ins->right);
+               left  = read_const(state, ins, &RHS(ins, 0));
+               right = read_const(state, ins, &RHS(ins, 1));
                mkconst(state, ins, left % right);
        }
-       else if (is_zero(ins->left)) {
+       else if (is_zero(RHS(ins, 0))) {
                mkconst(state, ins, 0);
        }
-       else if (is_zero(ins->right)) {
+       else if (is_zero(RHS(ins, 1))) {
                error(state, ins, "division by zero");
        }
-       else if (is_one(ins->right)) {
+       else if (is_one(RHS(ins, 1))) {
                mkconst(state, ins, 0);
        }
-       else if (is_pow2(ins->right)) {
+       else if (is_pow2(RHS(ins, 1))) {
                struct triple *val;
-               val = int_const(state, ins->type, ins->right->u.cval - 1);
+               val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
                ins->op = OP_AND;
                insert_triple(state, ins, val);
-               unuse_triple(ins->right, ins);
+               unuse_triple(RHS(ins, 1), ins);
                use_triple(val, ins);
-               ins->right = val;
+               RHS(ins, 1) = val;
        }
 }
 
 static void simplify_add(struct compile_state *state, struct triple *ins)
 {
        /* start with the pointer on the left */
-       if (is_pointer(ins->right)) {
+       if (is_pointer(RHS(ins, 1))) {
                struct triple *tmp;
-               tmp = ins->left;
-               ins->left = ins->right;
-               ins->right = tmp;
+               tmp = RHS(ins, 0);
+               RHS(ins, 0) = RHS(ins, 1);
+               RHS(ins, 1) = tmp;
        }
-       if (is_const(ins->left) && is_const(ins->right)) {
-               if (!is_pointer(ins->left)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
+               if (RHS(ins, 0)->op == OP_INTCONST) {
                        ulong_t left, right;
-                       left  = read_const(state, ins, &ins->left);
-                       right = read_const(state, ins, &ins->right);
+                       left  = read_const(state, ins, &RHS(ins, 0));
+                       right = read_const(state, ins, &RHS(ins, 1));
                        mkconst(state, ins, left + right);
                }
-               else {
+               else if (RHS(ins, 0)->op == OP_ADDRCONST) {
                        struct triple *sdecl;
                        ulong_t left, right;
-                       sdecl = ins->left->left;
-                       left  = ins->left->u.cval;
-                       right = ins->right->u.cval;
+                       sdecl = MISC(RHS(ins, 0), 0);
+                       left  = RHS(ins, 0)->u.cval;
+                       right = RHS(ins, 1)->u.cval;
                        mkaddr_const(state, ins, sdecl, left + right);
                }
+               else {
+                       internal_warning(state, ins, "Optimize me!");
+               }
        }
-       else if (is_const(ins->left) && !is_const(ins->right)) {
+       else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
                struct triple *tmp;
-               tmp = ins->right;
-               ins->right = ins->left;
-               ins->left  = tmp;
+               tmp = RHS(ins, 1);
+               RHS(ins, 1) = RHS(ins, 0);
+               RHS(ins, 0) = tmp;
        }
 }
 
 static void simplify_sub(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
-               if (!is_pointer(ins->left)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
+               if (RHS(ins, 0)->op == OP_INTCONST) {
                        ulong_t left, right;
-                       left  = read_const(state, ins, &ins->left);
-                       right = read_const(state, ins, &ins->right);
+                       left  = read_const(state, ins, &RHS(ins, 0));
+                       right = read_const(state, ins, &RHS(ins, 1));
                        mkconst(state, ins, left - right);
                }
-               else {
+               else if (RHS(ins, 0)->op == OP_ADDRCONST) {
                        struct triple *sdecl;
                        ulong_t left, right;
-                       sdecl = ins->left->left;
-                       left  = ins->left->u.cval;
-                       right = ins->right->u.cval;
+                       sdecl = MISC(RHS(ins, 0), 0);
+                       left  = RHS(ins, 0)->u.cval;
+                       right = RHS(ins, 1)->u.cval;
                        mkaddr_const(state, ins, sdecl, left - right);
                }
+               else {
+                       internal_warning(state, ins, "Optimize me!");
+               }
        }
 }
 
 static void simplify_sl(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->right)) {
+       if (is_const(RHS(ins, 1))) {
                ulong_t right;
-               right = read_const(state, ins, &ins->right);
+               right = read_const(state, ins, &RHS(ins, 1));
                if (right >= (size_of(state, ins->type)*8)) {
                        warning(state, ins, "left shift count >= width of type");
                }
        }
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                ulong_t left, right;
-               left  = read_const(state, ins, &ins->left);
-               right = read_const(state, ins, &ins->right);
+               left  = read_const(state, ins, &RHS(ins, 0));
+               right = read_const(state, ins, &RHS(ins, 1));
                mkconst(state, ins,  left << right);
        }
 }
 
 static void simplify_usr(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->right)) {
+       if (is_const(RHS(ins, 1))) {
                ulong_t right;
-               right = read_const(state, ins, &ins->right);
+               right = read_const(state, ins, &RHS(ins, 1));
                if (right >= (size_of(state, ins->type)*8)) {
                        warning(state, ins, "right shift count >= width of type");
                }
        }
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                ulong_t left, right;
-               left  = read_const(state, ins, &ins->left);
-               right = read_const(state, ins, &ins->right);
+               left  = read_const(state, ins, &RHS(ins, 0));
+               right = read_const(state, ins, &RHS(ins, 1));
                mkconst(state, ins, left >> right);
        }
 }
 
 static void simplify_ssr(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->right)) {
+       if (is_const(RHS(ins, 1))) {
                ulong_t right;
-               right = read_const(state, ins, &ins->right);
+               right = read_const(state, ins, &RHS(ins, 1));
                if (right >= (size_of(state, ins->type)*8)) {
                        warning(state, ins, "right shift count >= width of type");
                }
        }
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                long_t left, right;
-               left  = read_sconst(ins, &ins->left);
-               right = read_sconst(ins, &ins->right);
+               left  = read_sconst(ins, &RHS(ins, 0));
+               right = read_sconst(ins, &RHS(ins, 1));
                mkconst(state, ins, left >> right);
        }
 }
 
 static void simplify_and(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                ulong_t left, right;
-               left  = read_const(state, ins, &ins->left);
-               right = read_const(state, ins, &ins->right);
+               left  = read_const(state, ins, &RHS(ins, 0));
+               right = read_const(state, ins, &RHS(ins, 1));
                mkconst(state, ins, left & right);
        }
 }
 
 static void simplify_or(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                ulong_t left, right;
-               left  = read_const(state, ins, &ins->left);
-               right = read_const(state, ins, &ins->right);
+               left  = read_const(state, ins, &RHS(ins, 0));
+               right = read_const(state, ins, &RHS(ins, 1));
                mkconst(state, ins, left | right);
        }
 }
 
 static void simplify_xor(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                ulong_t left, right;
-               left  = read_const(state, ins, &ins->left);
-               right = read_const(state, ins, &ins->right);
+               left  = read_const(state, ins, &RHS(ins, 0));
+               right = read_const(state, ins, &RHS(ins, 1));
                mkconst(state, ins, left ^ right);
        }
 }
 
 static void simplify_pos(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left)) {
-               mkconst(state, ins, ins->left->u.cval);
+       if (is_const(RHS(ins, 0))) {
+               mkconst(state, ins, RHS(ins, 0)->u.cval);
        }
        else {
-               mkcopy(state, ins, ins->left);
+               mkcopy(state, ins, RHS(ins, 0));
        }
 }
 
 static void simplify_neg(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left)) {
+       if (is_const(RHS(ins, 0))) {
                ulong_t left;
-               left = read_const(state, ins, &ins->left);
+               left = read_const(state, ins, &RHS(ins, 0));
                mkconst(state, ins, -left);
        }
-       else if (ins->left->op == OP_NEG) {
-               mkcopy(state, ins, ins->left->left);
+       else if (RHS(ins, 0)->op == OP_NEG) {
+               mkcopy(state, ins, RHS(RHS(ins, 0), 0));
        }
 }
 
 static void simplify_invert(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left)) {
+       if (is_const(RHS(ins, 0))) {
                ulong_t left;
-               left = read_const(state, ins, &ins->left);
+               left = read_const(state, ins, &RHS(ins, 0));
                mkconst(state, ins, ~left);
        }
 }
 
 static void simplify_eq(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                ulong_t left, right;
-               left  = read_const(state, ins, &ins->left);
-               right = read_const(state, ins, &ins->right);
+               left  = read_const(state, ins, &RHS(ins, 0));
+               right = read_const(state, ins, &RHS(ins, 1));
                mkconst(state, ins, left == right);
        }
-       else if (ins->left == ins->right) {
+       else if (RHS(ins, 0) == RHS(ins, 1)) {
                mkconst(state, ins, 1);
        }
 }
 
 static void simplify_noteq(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                ulong_t left, right;
-               left  = read_const(state, ins, &ins->left);
-               right = read_const(state, ins, &ins->right);
+               left  = read_const(state, ins, &RHS(ins, 0));
+               right = read_const(state, ins, &RHS(ins, 1));
                mkconst(state, ins, left != right);
        }
-       else if (ins->left == ins->right) {
+       else if (RHS(ins, 0) == RHS(ins, 1)) {
                mkconst(state, ins, 0);
        }
 }
 
 static void simplify_sless(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                long_t left, right;
-               left  = read_sconst(ins, &ins->left);
-               right = read_sconst(ins, &ins->right);
+               left  = read_sconst(ins, &RHS(ins, 0));
+               right = read_sconst(ins, &RHS(ins, 1));
                mkconst(state, ins, left < right);
        }
-       else if (ins->left == ins->right) {
+       else if (RHS(ins, 0) == RHS(ins, 1)) {
                mkconst(state, ins, 0);
        }
 }
 
 static void simplify_uless(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                ulong_t left, right;
-               left  = read_const(state, ins, &ins->left);
-               right = read_const(state, ins, &ins->right);
+               left  = read_const(state, ins, &RHS(ins, 0));
+               right = read_const(state, ins, &RHS(ins, 1));
                mkconst(state, ins, left < right);
        }
-       else if (is_zero(ins->left)) {
+       else if (is_zero(RHS(ins, 0))) {
                mkconst(state, ins, 1);
        }
-       else if (ins->left == ins->right) {
+       else if (RHS(ins, 0) == RHS(ins, 1)) {
                mkconst(state, ins, 0);
        }
 }
 
 static void simplify_smore(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                long_t left, right;
-               left  = read_sconst(ins, &ins->left);
-               right = read_sconst(ins, &ins->right);
+               left  = read_sconst(ins, &RHS(ins, 0));
+               right = read_sconst(ins, &RHS(ins, 1));
                mkconst(state, ins, left > right);
        }
-       else if (ins->left == ins->right) {
+       else if (RHS(ins, 0) == RHS(ins, 1)) {
                mkconst(state, ins, 0);
        }
 }
 
 static void simplify_umore(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                ulong_t left, right;
-               left  = read_const(state, ins, &ins->left);
-               right = read_const(state, ins, &ins->right);
+               left  = read_const(state, ins, &RHS(ins, 0));
+               right = read_const(state, ins, &RHS(ins, 1));
                mkconst(state, ins, left > right);
        }
-       else if (is_zero(ins->right)) {
+       else if (is_zero(RHS(ins, 1))) {
                mkconst(state, ins, 1);
        }
-       else if (ins->left == ins->right) {
+       else if (RHS(ins, 0) == RHS(ins, 1)) {
                mkconst(state, ins, 0);
        }
 }
@@ -5430,120 +6263,120 @@ static void simplify_umore(struct compile_state *state, struct triple *ins)
 
 static void simplify_slesseq(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                long_t left, right;
-               left  = read_sconst(ins, &ins->left);
-               right = read_sconst(ins, &ins->right);
+               left  = read_sconst(ins, &RHS(ins, 0));
+               right = read_sconst(ins, &RHS(ins, 1));
                mkconst(state, ins, left <= right);
        }
-       else if (ins->left == ins->right) {
+       else if (RHS(ins, 0) == RHS(ins, 1)) {
                mkconst(state, ins, 1);
        }
 }
 
 static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                ulong_t left, right;
-               left  = read_const(state, ins, &ins->left);
-               right = read_const(state, ins, &ins->right);
+               left  = read_const(state, ins, &RHS(ins, 0));
+               right = read_const(state, ins, &RHS(ins, 1));
                mkconst(state, ins, left <= right);
        }
-       else if (is_zero(ins->left)) {
+       else if (is_zero(RHS(ins, 0))) {
                mkconst(state, ins, 1);
        }
-       else if (ins->left == ins->right) {
+       else if (RHS(ins, 0) == RHS(ins, 1)) {
                mkconst(state, ins, 1);
        }
 }
 
 static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 0))) {
                long_t left, right;
-               left  = read_sconst(ins, &ins->left);
-               right = read_sconst(ins, &ins->right);
+               left  = read_sconst(ins, &RHS(ins, 0));
+               right = read_sconst(ins, &RHS(ins, 1));
                mkconst(state, ins, left >= right);
        }
-       else if (ins->left == ins->right) {
+       else if (RHS(ins, 0) == RHS(ins, 1)) {
                mkconst(state, ins, 1);
        }
 }
 
 static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left) && is_const(ins->right)) {
+       if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
                ulong_t left, right;
-               left  = read_const(state, ins, &ins->left);
-               right = read_const(state, ins, &ins->right);
+               left  = read_const(state, ins, &RHS(ins, 0));
+               right = read_const(state, ins, &RHS(ins, 1));
                mkconst(state, ins, left >= right);
        }
-       else if (is_zero(ins->right)) {
+       else if (is_zero(RHS(ins, 1))) {
                mkconst(state, ins, 1);
        }
-       else if (ins->left == ins->right) {
+       else if (RHS(ins, 0) == RHS(ins, 1)) {
                mkconst(state, ins, 1);
        }
 }
 
 static void simplify_lfalse(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left)) {
+       if (is_const(RHS(ins, 0))) {
                ulong_t left;
-               left = read_const(state, ins, &ins->left);
+               left = read_const(state, ins, &RHS(ins, 0));
                mkconst(state, ins, left == 0);
        }
        /* Otherwise if I am the only user... */
-       else if ((ins->left->use->member == ins) && (ins->left->use->next == 0)) {
+       else if ((RHS(ins, 0)->use->member == ins) && (RHS(ins, 0)->use->next == 0)) {
                int need_copy = 1;
                /* Invert a boolean operation */
-               switch(ins->left->op) {
-               case OP_LTRUE:   ins->left->op = OP_LFALSE;  break;
-               case OP_LFALSE:  ins->left->op = OP_LTRUE;   break;
-               case OP_EQ:      ins->left->op = OP_NOTEQ;   break;
-               case OP_NOTEQ:   ins->left->op = OP_EQ;      break;
-               case OP_SLESS:   ins->left->op = OP_SMOREEQ; break;
-               case OP_ULESS:   ins->left->op = OP_UMOREEQ; break;
-               case OP_SMORE:   ins->left->op = OP_SLESSEQ; break;
-               case OP_UMORE:   ins->left->op = OP_ULESSEQ; break;
-               case OP_SLESSEQ: ins->left->op = OP_SMORE;   break;
-               case OP_ULESSEQ: ins->left->op = OP_UMORE;   break;
-               case OP_SMOREEQ: ins->left->op = OP_SLESS;   break;
-               case OP_UMOREEQ: ins->left->op = OP_ULESS;   break;
+               switch(RHS(ins, 0)->op) {
+               case OP_LTRUE:   RHS(ins, 0)->op = OP_LFALSE;  break;
+               case OP_LFALSE:  RHS(ins, 0)->op = OP_LTRUE;   break;
+               case OP_EQ:      RHS(ins, 0)->op = OP_NOTEQ;   break;
+               case OP_NOTEQ:   RHS(ins, 0)->op = OP_EQ;      break;
+               case OP_SLESS:   RHS(ins, 0)->op = OP_SMOREEQ; break;
+               case OP_ULESS:   RHS(ins, 0)->op = OP_UMOREEQ; break;
+               case OP_SMORE:   RHS(ins, 0)->op = OP_SLESSEQ; break;
+               case OP_UMORE:   RHS(ins, 0)->op = OP_ULESSEQ; break;
+               case OP_SLESSEQ: RHS(ins, 0)->op = OP_SMORE;   break;
+               case OP_ULESSEQ: RHS(ins, 0)->op = OP_UMORE;   break;
+               case OP_SMOREEQ: RHS(ins, 0)->op = OP_SLESS;   break;
+               case OP_UMOREEQ: RHS(ins, 0)->op = OP_ULESS;   break;
                default:
                        need_copy = 0;
                        break;
                }
                if (need_copy) {
-                       mkcopy(state, ins, ins->left);
+                       mkcopy(state, ins, RHS(ins, 0));
                }
        }
 }
 
 static void simplify_ltrue (struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left)) {
+       if (is_const(RHS(ins, 0))) {
                ulong_t left;
-               left = read_const(state, ins, &ins->left);
+               left = read_const(state, ins, &RHS(ins, 0));
                mkconst(state, ins, left != 0);
        }
-       else switch(ins->left->op) {
+       else switch(RHS(ins, 0)->op) {
        case OP_LTRUE:   case OP_LFALSE:  case OP_EQ:      case OP_NOTEQ:
        case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
        case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
-               mkcopy(state, ins, ins->left);
+               mkcopy(state, ins, RHS(ins, 0));
        }
 
 }
 
 static void simplify_copy(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left)) {
-               switch(ins->left->op) {
+       if (is_const(RHS(ins, 0))) {
+               switch(RHS(ins, 0)->op) {
                case OP_INTCONST:
                {
                        ulong_t left;
-                       left = read_const(state, ins, &ins->left);
+                       left = read_const(state, ins, &RHS(ins, 0));
                        mkconst(state, ins, left);
                        break;
                }
@@ -5551,8 +6384,8 @@ static void simplify_copy(struct compile_state *state, struct triple *ins)
                {
                        struct triple *sdecl;
                        ulong_t offset;
-                       sdecl  = ins->left;
-                       offset = ins->u.cval;
+                       sdecl  = MISC(RHS(ins, 0), 0);
+                       offset = RHS(ins, 0)->u.cval;
                        mkaddr_const(state, ins, sdecl, offset);
                        break;
                }
@@ -5563,11 +6396,6 @@ static void simplify_copy(struct compile_state *state, struct triple *ins)
        }
 }
 
-static void simplify_dot(struct compile_state *state, struct triple *ins)
-{
-       FINISHME();
-}
-
 static void simplify_branch(struct compile_state *state, struct triple *ins)
 {
        struct block *block;
@@ -5584,29 +6412,31 @@ static void simplify_branch(struct compile_state *state, struct triple *ins)
         */
        block = ins->u.block;
        
-       if (ins->right && is_const(ins->right)) {
+       if (TRIPLE_RHS(ins->sizes) && is_const(RHS(ins, 0))) {
+               struct triple *targ;
                ulong_t value;
-               value = read_const(state, ins, &ins->right);
-               unuse_triple(ins->right, ins);
-               ins->right = 0;
+               value = read_const(state, ins, &RHS(ins, 0));
+               unuse_triple(RHS(ins, 0), ins);
+               targ = TARG(ins, 0);
+               ins->sizes = TRIPLE_SIZES(0, 0, 0, 1);
                if (value) {
                        unuse_triple(ins->next, ins);
+                       TARG(ins, 0) = targ;
                }
                else {
-                       unuse_triple(ins->left, ins);
-                       ins->left = ins->next;
+                       unuse_triple(targ, ins);
+                       TARG(ins, 0) = ins->next;
                }
 #warning "FIXME handle the case of making a branch unconditional"
        }
-       if (ins->left == ins->next) {
-               unuse_triple(ins->left, ins);
-               if (ins->right) {
-                       unuse_triple(ins->right, ins);
+       if (TARG(ins, 0) == ins->next) {
+               unuse_triple(ins->next, ins);
+               if (TRIPLE_RHS(ins->sizes)) {
+                       unuse_triple(RHS(ins, 0), ins);
                        unuse_triple(ins->next, ins);
                }
-               ins->op    = OP_NOOP;
-               ins->left  = 0;
-               ins->right = 0;
+               ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
+               ins->op = OP_NOOP;
                if (ins->use) {
                        internal_error(state, ins, "noop use != 0");
                }
@@ -5614,17 +6444,76 @@ static void simplify_branch(struct compile_state *state, struct triple *ins)
        }
 }
 
+int phi_present(struct block *block)
+{
+       struct triple *ptr;
+       if (!block) {
+               return 0;
+       }
+       ptr = block->first;
+       do {
+               if (ptr->op == OP_PHI) {
+                       return 1;
+               }
+               ptr = ptr->next;
+       } while(ptr != block->last);
+       return 0;
+}
+
+static void simplify_label(struct compile_state *state, struct triple *ins)
+{
+#warning "FIXME enable simplify_label"
+       struct triple *first, *last;
+       first = RHS(state->main_function, 0);
+       last = first->prev;
+       /* Ignore the first and last instructions */
+       if ((ins == first) || (ins == last)) {
+               return;
+       }
+       if (ins->use == 0) {
+               ins->op = OP_NOOP;
+       }
+       else if (ins->prev->op == OP_LABEL) {
+               struct block *block;
+               block = ins->prev->u.block;
+               /* In general it is not safe to merge one label that
+                * imediately follows another.  The problem is that the empty
+                * looking block may have phi functions that depend on it.
+                */
+               if (!block || 
+                       (!phi_present(block->left) && 
+                       !phi_present(block->right))) 
+               {
+                       struct triple_set *user, *next;
+                       ins->op = OP_NOOP;
+                       for(user = ins->use; user; user = next) {
+                               struct triple *use;
+                               next = user->next;
+                               use = user->member;
+                               if (TARG(use, 0) == ins) {
+                                       TARG(use, 0) = ins->prev;
+                                       unuse_triple(ins, use);
+                                       use_triple(ins->prev, use);
+                               }
+                       }
+                       if (ins->use) {
+                               internal_error(state, ins, "noop use != 0");
+                       }
+               }
+       }
+}
+
 static void simplify_phi(struct compile_state *state, struct triple *ins)
 {
        struct triple **expr;
        ulong_t value;
        expr = triple_rhs(state, ins, 0);
-       if (!is_const(*expr)) {
+       if (!*expr || !is_const(*expr)) {
                return;
        }
        value = read_const(state, ins, expr);
        for(;expr;expr = triple_rhs(state, ins, expr)) {
-               if (!is_const(*expr)) {
+               if (!*expr || !is_const(*expr)) {
                        return;
                }
                if (value != read_const(state, ins, expr)) {
@@ -5637,18 +6526,18 @@ static void simplify_phi(struct compile_state *state, struct triple *ins)
 
 static void simplify_bsf(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left)) {
+       if (is_const(RHS(ins, 0))) {
                ulong_t left;
-               left = read_const(state, ins, &ins->left);
+               left = read_const(state, ins, &RHS(ins, 0));
                mkconst(state, ins, bsf(left));
        }
 }
 
 static void simplify_bsr(struct compile_state *state, struct triple *ins)
 {
-       if (is_const(ins->left)) {
+       if (is_const(RHS(ins, 0))) {
                ulong_t left;
-               left = read_const(state, ins, &ins->left);
+               left = read_const(state, ins, &RHS(ins, 0));
                mkconst(state, ins, bsr(left));
        }
 }
@@ -5656,6 +6545,10 @@ static void simplify_bsr(struct compile_state *state, struct triple *ins)
 
 typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
 static const simplify_t table_simplify[] = {
+#if 1
+#define simplify_sdivt    simplify_noop
+#define simplify_udivt    simplify_noop
+#endif
 #if 0
 #define simplify_smul     simplify_noop
 #define simplify_umul    simplify_noop
@@ -5711,13 +6604,12 @@ static const simplify_t table_simplify[] = {
 #define simplify_copy    simplify_noop
 #endif
 
-#if 0
-#define simplify_dot     simplify_noop
-#endif
-
 #if 0
 #define simplify_branch          simplify_noop
 #endif
+#if 1
+#define simplify_label   simplify_noop
+#endif
 
 #if 0
 #define simplify_phi     simplify_noop
@@ -5728,6 +6620,8 @@ static const simplify_t table_simplify[] = {
 #define simplify_bsr      simplify_noop
 #endif
 
+[OP_SDIVT      ] = simplify_sdivt,
+[OP_UDIVT      ] = simplify_udivt,
 [OP_SMUL       ] = simplify_smul,
 [OP_UMUL       ] = simplify_umul,
 [OP_SDIV       ] = simplify_sdiv,
@@ -5771,11 +6665,15 @@ static const simplify_t table_simplify[] = {
 [OP_WRITE      ] = simplify_noop,
 [OP_READ       ] = simplify_noop,
 [OP_COPY       ] = simplify_copy,
-[OP_DOT        ] = simplify_dot,
+[OP_PIECE      ] = simplify_noop,
+[OP_ASM        ] = simplify_noop,
+
+[OP_DOT        ] = simplify_noop,
+[OP_VAL_VEC    ] = simplify_noop,
 
 [OP_LIST       ] = simplify_noop,
 [OP_BRANCH     ] = simplify_branch,
-[OP_LABEL      ] = simplify_noop,
+[OP_LABEL      ] = simplify_label,
 [OP_ADECL      ] = simplify_noop,
 [OP_SDECL      ] = simplify_noop,
 [OP_PHI        ] = simplify_phi,
@@ -5787,7 +6685,10 @@ static const simplify_t table_simplify[] = {
 [OP_OUTW       ] = simplify_noop,
 [OP_OUTL       ] = simplify_noop,
 [OP_BSF        ] = simplify_bsf,
-[OP_BSR        ] = simplify_bsr,                    
+[OP_BSR        ] = simplify_bsr,
+[OP_RDMSR      ] = simplify_noop,
+[OP_WRMSR      ] = simplify_noop,                    
+[OP_HLT        ] = simplify_noop,
 };
 
 static void simplify(struct compile_state *state, struct triple *ins)
@@ -5815,12 +6716,12 @@ static void simplify(struct compile_state *state, struct triple *ins)
 static void simplify_all(struct compile_state *state)
 {
        struct triple *ins, *first;
-       first = state->main_function->left;
+       first = RHS(state->main_function, 0);
        ins = first;
        do {
                simplify(state, ins);
                ins = ins->next;
-       } while(ins != first);
+       }while(ins != first);
 }
 
 /*
@@ -5828,134 +6729,231 @@ static void simplify_all(struct compile_state *state)
  * ============================
  */
 
-static void register_builtin_unary(struct compile_state *state, 
-       const char *name, int op, struct type *result, struct type *a1type)
+static void register_builtin_function(struct compile_state *state,
+       const char *name, int op, struct type *rtype, ...)
 {
-       struct type *ftype, *rtype, *atype;
-       struct triple *def, *arg1, *work, *last, *first;
+       struct type *ftype, *atype, *param, **next;
+       struct triple *def, *arg, *result, *work, *last, *first;
        struct hash_entry *ident;
+       struct file_state file;
+       int parameters;
        int name_len;
+       va_list args;
+       int i;
 
        /* Dummy file state to get debug handling right */
-       struct file_state file;
        memset(&file, 0, sizeof(file));
-       file.basename = name;
+       file.basename = "<built-in>";
        file.line = 1;
+       file.report_line = 1;
+       file.report_name = file.basename;
        file.prev = state->file;
        state->file = &file;
-       
-       atype = a1type;
-       rtype = result;
-       ftype = new_type(TYPE_FUNCTION, rtype, atype);
+       state->function = name;
+
+       /* Find the Parameter count */
+       valid_op(state, op);
+       parameters = table_ops[op].rhs;
+       if (parameters < 0 ) {
+               internal_error(state, 0, "Invalid builtin parameter count");
+       }
+
+       /* Find the function type */
+       ftype = new_type(TYPE_FUNCTION, rtype, 0);
+       next = &ftype->right;
+       va_start(args, rtype);
+       for(i = 0; i < parameters; i++) {
+               atype = va_arg(args, struct type *);
+               if (!*next) {
+                       *next = atype;
+               } else {
+                       *next = new_type(TYPE_PRODUCT, *next, atype);
+                       next = &((*next)->right);
+               }
+       }
+       if (!*next) {
+               *next = &void_type;
+       }
+       va_end(args);
+
        /* Generate the needed triples */
        def = triple(state, OP_LIST, ftype, 0, 0);
        first = label(state);
-       def->left = first;
-       /* Now string them together into a list */
-       arg1 = 0;
-       if ((atype->type & TYPE_MASK) != TYPE_VOID) {
-               arg1 = flatten(state, first, variable(state, a1type));
+       RHS(def, 0) = first;
+
+       /* Now string them together */
+       param = ftype->right;
+       for(i = 0; i < parameters; i++) {
+               if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
+                       atype = param->left;
+               } else {
+                       atype = param;
+               }
+               arg = flatten(state, first, variable(state, atype));
+               param = param->right;
        }
-       def->right = 0;
+       result = 0;
        if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
-               def->right = flatten(state, first, variable(state, rtype));
+               result = flatten(state, first, variable(state, rtype));
+       }
+       MISC(def, 0) = result;
+       work = new_triple(state, op, rtype, -1, parameters);
+       for(i = 0, arg = first->next; i < parameters; i++, arg = arg->next) {
+               RHS(work, i) = read_expr(state, arg);
+       }
+       if (result && ((rtype->type & TYPE_MASK) == TYPE_STRUCT)) {
+               struct triple *val;
+               /* Populate the LHS with the target registers */
+               work = flatten(state, first, work);
+               work->type = &void_type;
+               param = rtype->left;
+               if (rtype->elements != TRIPLE_LHS(work->sizes)) {
+                       internal_error(state, 0, "Invalid result type");
+               }
+               val = new_triple(state, OP_VAL_VEC, rtype, -1, -1);
+               for(i = 0; i < rtype->elements; i++) {
+                       struct triple *piece;
+                       atype = param;
+                       if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
+                               atype = param->left;
+                       }
+                       if (!TYPE_ARITHMETIC(atype->type) &&
+                               !TYPE_PTR(atype->type)) {
+                               internal_error(state, 0, "Invalid lhs type");
+                       }
+                       piece = triple(state, OP_PIECE, atype, work, 0);
+                       piece->u.cval = i;
+                       LHS(work, i) = piece;
+                       RHS(val, i) = piece;
+               }
+               work = val;
        }
-       work = triple(state, op, rtype, read_expr(state, arg1), 0);
-       if (def->right) {
-               work = write_expr(state, def->right, work);
+       if (result) {
+               work = write_expr(state, result, work);
        }
        work = flatten(state, first, work);
        last = flatten(state, first, label(state));
        name_len = strlen(name);
        ident = lookup(state, name, name_len);
        symbol(state, ident, &ident->sym_ident, def, ftype);
-
+       
        state->file = file.prev;
-
+       state->function = 0;
 #if 0
        fprintf(stdout, "\n");
        loc(stdout, state, 0);
-       fprintf(stdout, "\n__________ builtin_unary _________\n");
+       fprintf(stdout, "\n__________ builtin_function _________\n");
        print_triple(state, def);
-       fprintf(stdout, "__________ builtin_unary _________ done\n\n");
+       fprintf(stdout, "__________ builtin_function _________ done\n\n");
 #endif
 }
 
-static void register_builtin_binary(struct compile_state *state, 
-       const char *name, int op, 
-       struct type *result, struct type *a1type, struct type *a2type)
+static struct type *partial_struct(struct compile_state *state,
+       const char *field_name, struct type *type, struct type *rest)
+{
+       struct hash_entry *field_ident;
+       struct type *result;
+       int field_name_len;
+
+       field_name_len = strlen(field_name);
+       field_ident = lookup(state, field_name, field_name_len);
+
+       result = clone_type(0, type);
+       result->field_ident = field_ident;
+
+       if (rest) {
+               result = new_type(TYPE_PRODUCT, result, rest);
+       }
+       return result;
+}
+
+static struct type *register_builtin_type(struct compile_state *state,
+       const char *name, struct type *type)
 {
-       struct type *ftype, *rtype, *atype;
-       struct triple *def, *arg1, *arg2, *work, *last, *first;
        struct hash_entry *ident;
        int name_len;
-       /* Dummy file state to get debug handling right */
-       struct file_state file;
-       memset(&file, 0, sizeof(file));
-       file.basename = name;
-       file.line = 1;
-       file.prev = state->file;
-       state->file = &file;
-       
-       atype = new_type(TYPE_PRODUCT, a1type, a2type);
-       rtype = result;
-       ftype = new_type(TYPE_FUNCTION, rtype, atype);
-       /* Generate the needed triples */
-       def = triple(state, OP_LIST, ftype, 0, 0);
-       first = label(state);
-       def->left = first;
-       /* String them togher */
-       arg1 = flatten(state, first, variable(state, a1type));
-       arg2 = flatten(state, first, variable(state, a2type));
-       def->right = 0;
-       if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
-               def->right = flatten(state, first, variable(state, rtype));
-       }
-       work = triple(state, op, rtype,
-                       read_expr(state, arg1), read_expr(state, arg2));
-       if (def->right) {
-               work = write_expr(state, def->right, work);
-       }
-       work = flatten(state, first, work);
-       last = flatten(state, first, label(state));
+
        name_len = strlen(name);
        ident = lookup(state, name, name_len);
-       symbol(state, ident, &ident->sym_ident, def, ftype);
-
-       state->file = file.prev;
-
-#if 0
-       fprintf(stdout, "\n");
-       loc(stdout, state, 0);
-       fprintf(stdout, "\n__________ builtin_binary _________\n");
-       print_triple(state, def);
-       fprintf(stdout, "__________ builtin_binary _________ done\n\n");
-#endif
+       
+       if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
+               ulong_t elements = 0;
+               struct type *field;
+               type = new_type(TYPE_STRUCT, type, 0);
+               field = type->left;
+               while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
+                       elements++;
+                       field = field->right;
+               }
+               elements++;
+               symbol(state, ident, &ident->sym_struct, 0, type);
+               type->type_ident = ident;
+               type->elements = elements;
+       }
+       symbol(state, ident, &ident->sym_ident, 0, type);
+       ident->tok = TOK_TYPE_NAME;
+       return type;
 }
 
+
 static void register_builtins(struct compile_state *state)
 {
-       register_builtin_unary(state, "__builtin_inb", OP_INB, 
+       struct type *div_type, *ldiv_type;
+       struct type *udiv_type, *uldiv_type;
+       struct type *msr_type;
+
+       div_type = register_builtin_type(state, "__builtin_div_t",
+               partial_struct(state, "quot", &int_type,
+               partial_struct(state, "rem",  &int_type, 0)));
+       ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
+               partial_struct(state, "quot", &long_type,
+               partial_struct(state, "rem",  &long_type, 0)));
+       udiv_type = register_builtin_type(state, "__builtin_udiv_t",
+               partial_struct(state, "quot", &uint_type,
+               partial_struct(state, "rem",  &uint_type, 0)));
+       uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
+               partial_struct(state, "quot", &ulong_type,
+               partial_struct(state, "rem",  &ulong_type, 0)));
+
+       register_builtin_function(state, "__builtin_div",   OP_SDIVT, div_type,
+               &int_type, &int_type);
+       register_builtin_function(state, "__builtin_ldiv",  OP_SDIVT, ldiv_type,
+               &long_type, &long_type);
+       register_builtin_function(state, "__builtin_udiv",  OP_UDIVT, udiv_type,
+               &uint_type, &uint_type);
+       register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
+               &ulong_type, &ulong_type);
+
+       register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type, 
+               &ushort_type);
+       register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
+               &ushort_type);
+       register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,   
+               &ushort_type);
+
+       register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type, 
                &uchar_type, &ushort_type);
-       register_builtin_unary(state, "__builtin_inw", OP_INW
+       register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type
                &ushort_type, &ushort_type);
-       register_builtin_unary( state, "__builtin_inl", OP_INL
+       register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type
                &uint_type, &ushort_type);
-
-       register_builtin_binary(state, "__builtin_outb", OP_OUTB,
-               &void_type, &uchar_type, &ushort_type);
-       register_builtin_binary(state, "__builtin_outw", OP_OUTW,
-               &void_type, &ushort_type, &ushort_type);
-       register_builtin_binary(state, "__builtin_outl", OP_OUTL,
-               &void_type, &uint_type, &ushort_type);
        
-       register_builtin_unary(state, "__builtin_bsf", OP_BSF,
-               &int_type, &int_type);
-       register_builtin_unary(state, "__builtin_bsr", OP_BSR,
-               &int_type, &int_type);
+       register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type, 
+               &int_type);
+       register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type, 
+               &int_type);
+
+       msr_type = register_builtin_type(state, "__builtin_msr_t",
+               partial_struct(state, "lo", &ulong_type,
+               partial_struct(state, "hi", &ulong_type, 0)));
+
+       register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
+               &ulong_type);
+       register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
+               &ulong_type, &ulong_type, &ulong_type);
        
-       register_builtin_unary(state, "__builtin_hlt", OP_HLT,
-               &void_type, &void_type);
+       register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type, 
+               &void_type);
 }
 
 static struct type *declarator(
@@ -5974,8 +6972,9 @@ static void statement(struct compile_state *state, struct triple *fist);
 static struct triple *call_expr(
        struct compile_state *state, struct triple *func)
 {
-       struct triple *def, **next;
-       struct type *type;
+       struct triple *def;
+       struct type *param, *type;
+       ulong_t pvals, index;
 
        if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
                error(state, 0, "Called object is not a function");
@@ -5986,35 +6985,26 @@ static struct triple *call_expr(
        eat(state, TOK_LPAREN);
        /* Find the return type without any specifiers */
        type = clone_type(0, func->type->left);
-       def = triple(state, OP_CALL, type, func, 0);
-       next = &def->right;
-       if (peek(state) != TOK_RPAREN) {
+       def = new_triple(state, OP_CALL, func->type, -1, -1);
+       def->type = type;
+
+       pvals = TRIPLE_RHS(def->sizes);
+       MISC(def, 0) = func;
+
+       param = func->type->right;
+       for(index = 0; index < pvals; index++) {
                struct triple *val;
-               struct type *param, *arg_type;
+               struct type *arg_type;
                val = read_expr(state, assignment_expr(state));
-               param = func->type->right;
                arg_type = param;
                if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
                        arg_type = param->left;
                }
-               else if ((param->type & TYPE_MASK) == TYPE_VOID) {
-                       error(state, 0, "Too many arguments");
-               }
                write_compatible(state, arg_type, val->type);
-               *next = val;
-               while(peek(state) == TOK_COMMA) {
+               RHS(def, index) = val;
+               if (index != (pvals - 1)) {
                        eat(state, TOK_COMMA);
-                       val = read_expr(state, assignment_expr(state));
-                       if (arg_type == param) {
-                               error(state, 0, "Too many arguments");
-                       }
-                       arg_type = param = param->right;
-                       if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
-                               arg_type = param->left;
-                       }
-                       write_compatible(state, arg_type, val->type);
-                       *next = triple(state, OP_PRODUCT, &void_type, *next, val);
-                       next = &((*next)->right);
+                       param = param->right;
                }
        }
        eat(state, TOK_RPAREN);
@@ -6063,8 +7053,8 @@ static struct triple *string_constant(struct compile_state *state)
                tk = &state->token[0];
                str = tk->val.str + 1;
                str_len = tk->str_len - 2;
-               if (str_len <= 0) {
-                       error(state, 0, "empty string constant");
+               if (str_len < 0) {
+                       error(state, 0, "negative string constant length");
                }
                end = str + str_len;
                ptr = buf;
@@ -6223,15 +7213,24 @@ static struct triple *postfix_expr(struct compile_state *state)
                        def = call_expr(state, def);
                        break;
                case TOK_DOT:
+               {
+                       struct hash_entry *field;
                        eat(state, TOK_DOT);
                        eat(state, TOK_IDENT);
-                       FINISHME();
+                       field = state->token[0].ident;
+                       def = deref_field(state, def, field);
                        break;
+               }
                case TOK_ARROW:
+               {
+                       struct hash_entry *field;
                        eat(state, TOK_ARROW);
                        eat(state, TOK_IDENT);
-                       FINISHME();
+                       field = state->token[0].ident;
+                       def = mk_deref_expr(state, read_expr(state, def));
+                       def = deref_field(state, def, field);
                        break;
+               }
                case TOK_PLUSPLUS:
                        eat(state, TOK_PLUSPLUS);
                        def = mk_post_inc_expr(state, left);
@@ -6359,7 +7358,6 @@ static struct triple *cast_expr(struct compile_state *state)
                eat(state, TOK_RPAREN);
                def = read_expr(state, cast_expr(state));
                def = triple(state, OP_COPY, type, def, 0);
-#warning "FIXME do I need an OP_CAST expr to be semantically correct here?"
        }
        else {
                def = unary_expr(state);
@@ -6652,25 +7650,31 @@ static struct triple *eval_const_expr(
        struct compile_state *state, struct triple *expr)
 {
        struct triple *def;
-       struct triple *head, *ptr;
-       head = label(state); /* dummy initial triple */
-       flatten(state, head, expr);
-       for(ptr = head->next; ptr != head; ptr = ptr->next) {
-               simplify(state, ptr);
-       }
-       /* Remove the constant value the tail of the list */
-       def = head->prev;
-       def->prev->next = def->next;
-       def->next->prev = def->prev;
-       def->next = def->prev = def;
-       if (!is_const(def)) {
-               internal_error(state, 0, "Not a constant expression");
-       }
-       /* Free the intermediate expressions */
-       while(head->next != head) {
-               release_triple(state, head->next);
+       if (is_const(expr)) {
+               def = expr;
+       } 
+       else {
+               /* If we don't start out as a constant simplify into one */
+               struct triple *head, *ptr;
+               head = label(state); /* dummy initial triple */
+               flatten(state, head, expr);
+               for(ptr = head->next; ptr != head; ptr = ptr->next) {
+                       simplify(state, ptr);
+               }
+               /* Remove the constant value the tail of the list */
+               def = head->prev;
+               def->prev->next = def->next;
+               def->next->prev = def->prev;
+               def->next = def->prev = def;
+               if (!is_const(def)) {
+                       error(state, 0, "Not a constant expression");
+               }
+               /* Free the intermediate expressions */
+               while(head->next != head) {
+                       release_triple(state, head->next);
+               }
+               free_triple(state, head);
        }
-       free_triple(state, head);
        return def;
 }
 
@@ -6712,8 +7716,6 @@ static struct triple *assignment_expr(struct compile_state *state)
        case TOK_TIMESEQ:
        case TOK_DIVEQ:
        case TOK_MODEQ:
-       case TOK_PLUSEQ:
-       case TOK_MINUSEQ:
                lvalue(state, left);
                arithmetic(state, left);
                eat(state, tok);
@@ -6726,13 +7728,23 @@ static struct triple *assignment_expr(struct compile_state *state)
                case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
                case TOK_DIVEQ:   op = sign? OP_SDIV : OP_UDIV; break;
                case TOK_MODEQ:   op = sign? OP_SMOD : OP_UMOD; break;
-               case TOK_PLUSEQ:  op = OP_ADD; break;
-               case TOK_MINUSEQ: op = OP_SUB; break;
                }
                def = write_expr(state, left,
                        triple(state, op, left->type, 
                                read_expr(state, left), right));
                break;
+       case TOK_PLUSEQ:
+               lvalue(state, left);
+               eat(state, TOK_PLUSEQ);
+               def = write_expr(state, left,
+                       mk_add_expr(state, left, assignment_expr(state)));
+               break;
+       case TOK_MINUSEQ:
+               lvalue(state, left);
+               eat(state, TOK_MINUSEQ);
+               def = write_expr(state, left,
+                       mk_sub_expr(state, left, assignment_expr(state)));
+               break;
        case TOK_SLEQ:
        case TOK_SREQ:
        case TOK_ANDEQ:
@@ -6797,7 +7809,7 @@ static void if_statement(struct compile_state *state, struct triple *first)
        eat(state, TOK_RPAREN);
        /* Generate the needed pieces */
        middle = label(state);
-       jmp1 = triple(state, OP_BRANCH, &void_type, middle, test);
+       jmp1 = branch(state, middle, test);
        /* Thread the pieces together */
        flatten(state, first, test);
        flatten(state, first, jmp1);
@@ -6807,7 +7819,7 @@ static void if_statement(struct compile_state *state, struct triple *first)
                eat(state, TOK_ELSE);
                /* Generate the rest of the pieces */
                end = label(state);
-               jmp2 = triple(state, OP_BRANCH, &void_type, end, 0);
+               jmp2 = branch(state, end, 0);
                /* Thread them together */
                flatten(state, first, jmp2);
                flatten(state, first, middle);
@@ -6847,11 +7859,11 @@ static void for_statement(struct compile_state *state, struct triple *first)
        label2 = label(state);
        label3 = label(state);
        if (test) {
-               jmp1 = triple(state, OP_BRANCH, &void_type, label3, 0);
-               jmp2 = triple(state, OP_BRANCH, &void_type, label1, test);
+               jmp1 = branch(state, label3, 0);
+               jmp2 = branch(state, label1, test);
        }
        else {
-               jmp2 = triple(state, OP_BRANCH, &void_type, label1, 0);
+               jmp2 = branch(state, label1, 0);
        }
        end = label(state);
        /* Remember where break and continue go */
@@ -6888,8 +7900,8 @@ static void while_statement(struct compile_state *state, struct triple *first)
        /* Generate the needed pieces */
        label1 = label(state);
        label2 = label(state);
-       jmp1 = triple(state, OP_BRANCH, &void_type, label2, 0);
-       jmp2 = triple(state, OP_BRANCH, &void_type, label1, test);
+       jmp1 = branch(state, label2, 0);
+       jmp2 = branch(state, label1, test);
        end = label(state);
        /* Remember where break and continue go */
        start_scope(state);
@@ -6940,8 +7952,7 @@ static void do_statement(struct compile_state *state, struct triple *first)
        test = ltrue_expr(state, test);
        flatten(state, first, label2);
        flatten(state, first, test);
-       flatten(state, first, 
-               triple(state, OP_BRANCH, &void_type, label1, test));
+       flatten(state, first, branch(state, label1, test));
        flatten(state, first, end);
 }
 
@@ -6965,13 +7976,13 @@ static void return_statement(struct compile_state *state, struct triple *first)
                (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
 
        /* Find the return variable */
-       var = state->main_function->right;
+       var = MISC(state->main_function, 0);
        /* Find the return destination */
-       dest = state->main_function->left->prev;
+       dest = RHS(state->main_function, 0)->prev;
        mv = jmp = 0;
        /* If needed generate a jump instruction */
        if (!last) {
-               jmp = triple(state, OP_BRANCH, &void_type, dest, 0);
+               jmp = branch(state, dest, 0);
        }
        /* If needed generate an assignment instruction */
        if (val) {
@@ -6996,7 +8007,7 @@ static void break_statement(struct compile_state *state, struct triple *first)
                error(state, 0, "break statement not within loop or switch");
        }
        dest = state->i_break->sym_ident->def;
-       flatten(state, first, triple(state, OP_BRANCH, &void_type, dest, 0));
+       flatten(state, first, branch(state, dest, 0));
 }
 
 static void continue_statement(struct compile_state *state, struct triple *first)
@@ -7008,27 +8019,51 @@ static void continue_statement(struct compile_state *state, struct triple *first
                error(state, 0, "continue statement outside of a loop");
        }
        dest = state->i_continue->sym_ident->def;
-       flatten(state, first, triple(state, OP_BRANCH, &void_type, dest, 0));
+       flatten(state, first, branch(state, dest, 0));
 }
 
 static void goto_statement(struct compile_state *state, struct triple *first)
 {
-       FINISHME();
+       struct hash_entry *ident;
        eat(state, TOK_GOTO);
        eat(state, TOK_IDENT);
+       ident = state->token[0].ident;
+       if (!ident->sym_label) {
+               /* If this is a forward branch allocate the label now,
+                * it will be flattend in the appropriate location later.
+                */
+               struct triple *ins;
+               ins = label(state);
+               label_symbol(state, ident, ins);
+       }
        eat(state, TOK_SEMI);
-       error(state, 0, "goto is not implemeted");
-       FINISHME();
+
+       flatten(state, first, branch(state, ident->sym_label->def, 0));
 }
 
 static void labeled_statement(struct compile_state *state, struct triple *first)
 {
-       FINISHME();
+       struct triple *ins;
+       struct hash_entry *ident;
        eat(state, TOK_IDENT);
+
+       ident = state->token[0].ident;
+       if (ident->sym_label && ident->sym_label->def) {
+               ins = ident->sym_label->def;
+               put_occurance(ins->occurance);
+               ins->occurance = new_occurance(state);
+       }
+       else {
+               ins = label(state);
+               label_symbol(state, ident, ins);
+       }
+       if (ins->id & TRIPLE_FLAG_FLATTENED) {
+               error(state, 0, "label %s already defined", ident->name);
+       }
+       flatten(state, first, ins);
+
        eat(state, TOK_COLON);
        statement(state, first);
-       error(state, 0, "labeled statements are not implemented");
-       FINISHME();
 }
 
 static void switch_statement(struct compile_state *state, struct triple *first)
@@ -7066,10 +8101,195 @@ static void default_statement(struct compile_state *state, struct triple *first)
 
 static void asm_statement(struct compile_state *state, struct triple *first)
 {
-       FINISHME();
-       error(state, 0, "FIXME finish asm_statement");
-}
-
+       struct asm_info *info;
+       struct {
+               struct triple *constraint;
+               struct triple *expr;
+       } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
+       struct triple *def, *asm_str;
+       int out, in, clobbers, more, colons, i;
+
+       eat(state, TOK_ASM);
+       /* For now ignore the qualifiers */
+       switch(peek(state)) {
+       case TOK_CONST:
+               eat(state, TOK_CONST);
+               break;
+       case TOK_VOLATILE:
+               eat(state, TOK_VOLATILE);
+               break;
+       }
+       eat(state, TOK_LPAREN);
+       asm_str = string_constant(state);
+
+       colons = 0;
+       out = in = clobbers = 0;
+       /* Outputs */
+       if ((colons == 0) && (peek(state) == TOK_COLON)) {
+               eat(state, TOK_COLON);
+               colons++;
+               more = (peek(state) == TOK_LIT_STRING);
+               while(more) {
+                       struct triple *var;
+                       struct triple *constraint;
+                       char *str;
+                       more = 0;
+                       if (out > MAX_LHS) {
+                               error(state, 0, "Maximum output count exceeded.");
+                       }
+                       constraint = string_constant(state);
+                       str = constraint->u.blob;
+                       if (str[0] != '=') {
+                               error(state, 0, "Output constraint does not start with =");
+                       }
+                       constraint->u.blob = str + 1;
+                       eat(state, TOK_LPAREN);
+                       var = conditional_expr(state);
+                       eat(state, TOK_RPAREN);
+
+                       lvalue(state, var);
+                       out_param[out].constraint = constraint;
+                       out_param[out].expr       = var;
+                       if (peek(state) == TOK_COMMA) {
+                               eat(state, TOK_COMMA);
+                               more = 1;
+                       }
+                       out++;
+               }
+       }
+       /* Inputs */
+       if ((colons == 1) && (peek(state) == TOK_COLON)) {
+               eat(state, TOK_COLON);
+               colons++;
+               more = (peek(state) == TOK_LIT_STRING);
+               while(more) {
+                       struct triple *val;
+                       struct triple *constraint;
+                       char *str;
+                       more = 0;
+                       if (in > MAX_RHS) {
+                               error(state, 0, "Maximum input count exceeded.");
+                       }
+                       constraint = string_constant(state);
+                       str = constraint->u.blob;
+                       if (digitp(str[0] && str[1] == '\0')) {
+                               int val;
+                               val = digval(str[0]);
+                               if ((val < 0) || (val >= out)) {
+                                       error(state, 0, "Invalid input constraint %d", val);
+                               }
+                       }
+                       eat(state, TOK_LPAREN);
+                       val = conditional_expr(state);
+                       eat(state, TOK_RPAREN);
+
+                       in_param[in].constraint = constraint;
+                       in_param[in].expr       = val;
+                       if (peek(state) == TOK_COMMA) {
+                               eat(state, TOK_COMMA);
+                               more = 1;
+                       }
+                       in++;
+               }
+       }
+
+       /* Clobber */
+       if ((colons == 2) && (peek(state) == TOK_COLON)) {
+               eat(state, TOK_COLON);
+               colons++;
+               more = (peek(state) == TOK_LIT_STRING);
+               while(more) {
+                       struct triple *clobber;
+                       more = 0;
+                       if ((clobbers + out) > MAX_LHS) {
+                               error(state, 0, "Maximum clobber limit exceeded.");
+                       }
+                       clobber = string_constant(state);
+                       eat(state, TOK_RPAREN);
+
+                       clob_param[clobbers].constraint = clobber;
+                       if (peek(state) == TOK_COMMA) {
+                               eat(state, TOK_COMMA);
+                               more = 1;
+                       }
+                       clobbers++;
+               }
+       }
+       eat(state, TOK_RPAREN);
+       eat(state, TOK_SEMI);
+
+
+       info = xcmalloc(sizeof(*info), "asm_info");
+       info->str = asm_str->u.blob;
+       free_triple(state, asm_str);
+
+       def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
+       def->u.ainfo = info;
+
+       /* Find the register constraints */
+       for(i = 0; i < out; i++) {
+               struct triple *constraint;
+               constraint = out_param[i].constraint;
+               info->tmpl.lhs[i] = arch_reg_constraint(state, 
+                       out_param[i].expr->type, constraint->u.blob);
+               free_triple(state, constraint);
+       }
+       for(; i - out < clobbers; i++) {
+               struct triple *constraint;
+               constraint = clob_param[i - out].constraint;
+               info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
+               free_triple(state, constraint);
+       }
+       for(i = 0; i < in; i++) {
+               struct triple *constraint;
+               const char *str;
+               constraint = in_param[i].constraint;
+               str = constraint->u.blob;
+               if (digitp(str[0]) && str[1] == '\0') {
+                       struct reg_info cinfo;
+                       int val;
+                       val = digval(str[0]);
+                       cinfo.reg = info->tmpl.lhs[val].reg;
+                       cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
+                       cinfo.regcm &= info->tmpl.lhs[val].regcm;
+                       if (cinfo.reg == REG_UNSET) {
+                               cinfo.reg = REG_VIRT0 + val;
+                       }
+                       if (cinfo.regcm == 0) {
+                               error(state, 0, "No registers for %d", val);
+                       }
+                       info->tmpl.lhs[val] = cinfo;
+                       info->tmpl.rhs[i]   = cinfo;
+                               
+               } else {
+                       info->tmpl.rhs[i] = arch_reg_constraint(state, 
+                               in_param[i].expr->type, str);
+               }
+               free_triple(state, constraint);
+       }
+
+       /* Now build the helper expressions */
+       for(i = 0; i < in; i++) {
+               RHS(def, i) = read_expr(state,in_param[i].expr);
+       }
+       flatten(state, first, def);
+       for(i = 0; i < out; i++) {
+               struct triple *piece;
+               piece = triple(state, OP_PIECE, out_param[i].expr->type, def, 0);
+               piece->u.cval = i;
+               LHS(def, i) = piece;
+               flatten(state, first,
+                       write_expr(state, out_param[i].expr, piece));
+       }
+       for(; i - out < clobbers; i++) {
+               struct triple *piece;
+               piece = triple(state, OP_PIECE, &void_type, def, 0);
+               piece->u.cval = i;
+               LHS(def, i) = piece;
+               flatten(state, first, piece);
+       }
+}
+
 
 static int isdecl(int tok)
 {
@@ -7178,7 +8398,7 @@ static struct type *param_decl(struct compile_state *state)
        ident = 0;
        type = decl_specifiers(state);
        type = declarator(state, type, &ident, 0);
-       type->ident = ident;
+       type->field_ident = ident;
        end_scope(state);
        return type;
 }
@@ -7315,7 +8535,6 @@ static struct type *typedef_name(
                (type->type & (STOR_MASK | QUAL_MASK))) {
                type = clone_type(specifiers, type);
        }
-       FINISHME();
        return type;
 }
 
@@ -7349,71 +8568,97 @@ static struct type *enum_specifier(
        return type;
 }
 
-#if 0
 static struct type *struct_declarator(
        struct compile_state *state, struct type *type, struct hash_entry **ident)
 {
        int tok;
-#warning "struct_declarator is complicated because of bitfields, kill them?"
        tok = peek(state);
        if (tok != TOK_COLON) {
                type = declarator(state, type, ident, 1);
        }
        if ((tok == TOK_COLON) || (peek(state) == TOK_COLON)) {
+               struct triple *value;
                eat(state, TOK_COLON);
-               constant_expr(state);
+               value = constant_expr(state);
+#warning "FIXME implement bitfields to reduce register usage"
+               error(state, 0, "bitfields not yet implemented");
        }
-       FINISHME();
        return type;
 }
-#endif
 
 static struct type *struct_or_union_specifier(
-       struct compile_state *state, unsigned int specifiers)
+       struct compile_state *state, unsigned int spec)
 {
-       struct type *type;
+       struct type *struct_type;
+       struct hash_entry *ident;
+       unsigned int type_join;
        int tok;
-       FINISHME();
-       type = 0;
+       struct_type = 0;
+       ident = 0;
        switch(peek(state)) {
        case TOK_STRUCT:
                eat(state, TOK_STRUCT);
+               type_join = TYPE_PRODUCT;
                break;
        case TOK_UNION:
                eat(state, TOK_UNION);
+               type_join = TYPE_OVERLAP;
+               error(state, 0, "unions not yet supported\n");
                break;
        default:
                eat(state, TOK_STRUCT);
+               type_join = TYPE_PRODUCT;
                break;
        }
        tok = peek(state);
-       if (tok == TOK_IDENT) {
-               eat(state, TOK_IDENT);
-               FINISHME();
+       if ((tok == TOK_IDENT) || (tok == TOK_TYPE_NAME)) {
+               eat(state, tok);
+               ident = state->token[0].ident;
        }
-       if ((tok != TOK_IDENT) || (peek(state) == TOK_LBRACE)) {
+       if (!ident || (peek(state) == TOK_LBRACE)) {
+               ulong_t elements;
+               struct type **next;
+               elements = 0;
                eat(state, TOK_LBRACE);
+               next = &struct_type;
                do {
                        struct type *base_type;
                        int done;
-                       FINISHME();
                        base_type = specifier_qualifier_list(state);
                        do {
                                struct type *type;
-                               struct hash_entry *ident;
+                               struct hash_entry *fident;
                                done = 1;
-                               type = declarator(state, base_type, &ident, 1);
+                               type = struct_declarator(state, base_type, &fident);
+                               elements++;
                                if (peek(state) == TOK_COMMA) {
                                        done = 0;
                                        eat(state, TOK_COMMA);
                                }
+                               type = clone_type(0, type);
+                               type->field_ident = fident;
+                               if (*next) {
+                                       *next = new_type(type_join, *next, type);
+                                       next = &((*next)->right);
+                               } else {
+                                       *next = type;
+                               }
                        } while(!done);
                        eat(state, TOK_SEMI);
                } while(peek(state) != TOK_RBRACE);
                eat(state, TOK_RBRACE);
+               struct_type = new_type(TYPE_STRUCT | spec, struct_type, 0);
+               struct_type->type_ident = ident;
+               struct_type->elements = elements;
+               symbol(state, ident, &ident->sym_struct, 0, struct_type);
        }
-       FINISHME();
-       return type;
+       if (ident && ident->sym_struct) {
+               struct_type = clone_type(spec,  ident->sym_struct->type);
+       }
+       else if (ident && !ident->sym_struct) {
+               error(state, 0, "struct %s undeclared", ident->name);
+       }
+       return struct_type;
 }
 
 static unsigned int storage_class_specifier_opt(struct compile_state *state)
@@ -7740,34 +8985,53 @@ static struct type *decl_specifiers(struct compile_state *state)
        return type;
 }
 
-static unsigned designator(struct compile_state *state)
+struct field_info {
+       struct type *type;
+       size_t offset;
+};
+
+static struct field_info designator(struct compile_state *state, struct type *type)
 {
        int tok;
-       unsigned index;
-       index = -1U;
+       struct field_info info;
+       info.offset = ~0U;
+       info.type = 0;
        do {
                switch(peek(state)) {
                case TOK_LBRACKET:
                {
                        struct triple *value;
+                       if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
+                               error(state, 0, "Array designator not in array initializer");
+                       }
                        eat(state, TOK_LBRACKET);
                        value = constant_expr(state);
                        eat(state, TOK_RBRACKET);
-                       index = value->u.cval;
+
+                       info.type = type->left;
+                       info.offset = value->u.cval * size_of(state, info.type);
                        break;
                }
                case TOK_DOT:
+               {
+                       struct hash_entry *field;
+                       if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
+                               error(state, 0, "Struct designator not in struct initializer");
+                       }
                        eat(state, TOK_DOT);
                        eat(state, TOK_IDENT);
-                       error(state, 0, "Struct Designators not currently supported");
+                       field = state->token[0].ident;
+                       info.offset = field_offset(state, type, field);
+                       info.type   = field_type(state, type, field);
                        break;
+               }
                default:
                        error(state, 0, "Invalid designator");
                }
                tok = peek(state);
        } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
        eat(state, TOK_EQ);
-       return index;
+       return info;
 }
 
 static struct triple *initializer(
@@ -7779,74 +9043,87 @@ static struct triple *initializer(
        }
        else {
                int comma;
-               unsigned index, max_index;
+               size_t max_offset;
+               struct field_info info;
                void *buf;
-               max_index = index = 0;
-               if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
-                       max_index = type->elements;
-                       if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
-                               type->elements = 0;
-                       }
+               if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
+                       ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
+                       internal_error(state, 0, "unknown initializer type");
+               }
+               info.offset = 0;
+               info.type = type->left;
+               if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
+                       info.type = next_field(state, type, 0);
+               }
+               if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
+                       max_offset = 0;
                } else {
-                       error(state, 0, "Struct initializers not currently supported");
+                       max_offset = size_of(state, type);
                }
-               buf = xcmalloc(size_of(state, type), "initializer");
+               buf = xcmalloc(max_offset, "initializer");
                eat(state, TOK_LBRACE);
                do {
                        struct triple *value;
                        struct type *value_type;
                        size_t value_size;
+                       void *dest;
                        int tok;
                        comma = 0;
                        tok = peek(state);
                        if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
-                               index = designator(state);
+                               info = designator(state, type);
                        }
-                       if ((max_index != ELEMENT_COUNT_UNSPECIFIED) &&
-                               (index > max_index)) {
+                       if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
+                               (info.offset >= max_offset)) {
                                error(state, 0, "element beyond bounds");
                        }
-                       value_type = 0;
-                       if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
-                               value_type = type->left;
-                       }
+                       value_type = info.type;
                        value = eval_const_expr(state, initializer(state, value_type));
                        value_size = size_of(state, value_type);
                        if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
-                               (max_index == ELEMENT_COUNT_UNSPECIFIED) &&
-                               (type->elements <= index)) {
+                               (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
+                               (max_offset <= info.offset)) {
                                void *old_buf;
                                size_t old_size;
                                old_buf = buf;
-                               old_size = size_of(state, type);
-                               type->elements = index + 1;
-                               buf = xmalloc(size_of(state, type), "initializer");
+                               old_size = max_offset;
+                               max_offset = info.offset + value_size;
+                               buf = xmalloc(max_offset, "initializer");
                                memcpy(buf, old_buf, old_size);
                                xfree(old_buf);
                        }
+                       dest = ((char *)buf) + info.offset;
                        if (value->op == OP_BLOBCONST) {
-                               memcpy((char *)buf + index * value_size, value->u.blob, value_size);
+                               memcpy(dest, value->u.blob, value_size);
                        }
                        else if ((value->op == OP_INTCONST) && (value_size == 1)) {
-                               *(((uint8_t *)buf) + index) = value->u.cval & 0xff;
+                               *((uint8_t *)dest) = value->u.cval & 0xff;
                        }
                        else if ((value->op == OP_INTCONST) && (value_size == 2)) {
-                               *(((uint16_t *)buf) + index) = value->u.cval & 0xffff;
+                               *((uint16_t *)dest) = value->u.cval & 0xffff;
                        }
                        else if ((value->op == OP_INTCONST) && (value_size == 4)) {
-                               *(((uint32_t *)buf) + index) = value->u.cval & 0xffffffff;
+                               *((uint32_t *)dest) = value->u.cval & 0xffffffff;
                        }
                        else {
-                               fprintf(stderr, "%d %d\n",
-                                       value->op, value_size);
                                internal_error(state, 0, "unhandled constant initializer");
                        }
+                       free_triple(state, value);
                        if (peek(state) == TOK_COMMA) {
                                eat(state, TOK_COMMA);
                                comma = 1;
                        }
-                       index += 1;
+                       info.offset += value_size;
+                       if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
+                               info.type = next_field(state, type, info.type);
+                               info.offset = field_offset(state, type, 
+                                       info.type->field_ident);
+                       }
                } while(comma && (peek(state) != TOK_RBRACE));
+               if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
+                       ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
+                       type->elements = max_offset / size_of(state, type->left);
+               }
                eat(state, TOK_RBRACE);
                result = triple(state, OP_BLOBCONST, type, 0, 0);
                result->u.blob = buf;
@@ -7854,6 +9131,30 @@ static struct triple *initializer(
        return result;
 }
 
+static void resolve_branches(struct compile_state *state)
+{
+       /* Make a second pass and finish anything outstanding
+        * with respect to branches.  The only outstanding item
+        * is to see if there are goto to labels that have not
+        * been defined and to error about them.
+        */
+       int i;
+       for(i = 0; i < HASH_TABLE_SIZE; i++) {
+               struct hash_entry *entry;
+               for(entry = state->hash_table[i]; entry; entry = entry->next) {
+                       struct triple *ins;
+                       if (!entry->sym_label) {
+                               continue;
+                       }
+                       ins = entry->sym_label->def;
+                       if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
+                               error(state, ins, "label `%s' used but not defined",
+                                       entry->name);
+                       }
+               }
+       }
+}
+
 static struct triple *function_definition(
        struct compile_state *state, struct type *type)
 {
@@ -7868,20 +9169,20 @@ static struct triple *function_definition(
        /* Verify the function type */
        if (((type->right->type & TYPE_MASK) != TYPE_VOID)  &&
                ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
-               (type->right->ident == 0)) {
+               (type->right->field_ident == 0)) {
                error(state, 0, "Invalid function parameters");
        }
        param = type->right;
        i = 0;
        while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
                i++;
-               if (!param->left->ident) {
+               if (!param->left->field_ident) {
                        error(state, 0, "No identifier for parameter %d\n", i);
                }
                param = param->right;
        }
        i++;
-       if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->ident) {
+       if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
                error(state, 0, "No identifier for paramter %d\n", i);
        }
        
@@ -7893,7 +9194,7 @@ static struct triple *function_definition(
 
        /* Put a label at the very start of a function */
        first = label(state);
-       def->left = first;
+       RHS(def, 0) = first;
 
        /* Put a label at the very end of a function */
        end = label(state);
@@ -7904,7 +9205,7 @@ static struct triple *function_definition(
         */
        param = type->right;
        while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
-               ident = param->left->ident;
+               ident = param->left->field_ident;
                tmp = variable(state, param->left);
                symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
                flatten(state, end, tmp);
@@ -7912,19 +9213,19 @@ static struct triple *function_definition(
        }
        if ((param->type & TYPE_MASK) != TYPE_VOID) {
                /* And don't forget the last parameter */
-               ident = param->ident;
+               ident = param->field_ident;
                tmp = variable(state, param);
                symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
                flatten(state, end, tmp);
        }
        /* Add a variable for the return value */
-       def->right = 0;
+       MISC(def, 0) = 0;
        if ((type->left->type & TYPE_MASK) != TYPE_VOID) {
                /* Remove all type qualifiers from the return type */
                tmp = variable(state, clone_type(0, type->left));
                flatten(state, end, tmp);
                /* Remember where the return value is */
-               def->right = tmp;
+               MISC(def, 0) = tmp;
        }
 
        /* Remember which function I am compiling.
@@ -7935,8 +9236,12 @@ static struct triple *function_definition(
        /* Now get the actual function definition */
        compound_statement(state, end);
 
+       /* Finish anything unfinished with branches */
+       resolve_branches(state);
+
        /* Remove the parameter scope */
        end_scope(state);
+
 #if 0
        fprintf(stdout, "\n");
        loc(stdout, state, 0);
@@ -7968,13 +9273,21 @@ static struct triple *do_decl(struct compile_state *state,
                type->type |= STOR_STATIC;
                break;
        case STOR_TYPEDEF:
-#warning "FIXME support typedefs"
-               error(state, 0, "typedefs not currently supported");
+               if (!ident) {
+                       error(state, 0, "typedef without name");
+               }
+               symbol(state, ident, &ident->sym_ident, 0, type);
+               ident->tok = TOK_TYPE_NAME;
+               return 0;
                break;
        default:
                internal_error(state, 0, "Undefined storage class");
        }
-       if (((type->type & STOR_MASK) == STOR_STATIC) &&
+       if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
+               error(state, 0, "Function prototypes not supported");
+       }
+       if (ident && 
+               ((type->type & STOR_MASK) == STOR_STATIC) &&
                ((type->type & QUAL_CONST) == 0)) {
                error(state, 0, "non const static variables not supported");
        }
@@ -7997,8 +9310,10 @@ static void decl(struct compile_state *state, struct triple *first)
        type = declarator(state, base_type, &ident, 0);
        if (global && ident && (peek(state) == TOK_LBRACE)) {
                /* function */
+               state->function = ident->name;
                def = function_definition(state, type);
                symbol(state, ident, &ident->sym_ident, def, type);
+               state->function = 0;
        }
        else {
                int done;
@@ -8163,7 +9478,7 @@ static int do_walk_triple(struct compile_state *state,
        if ((result == 0) && (ptr->op == OP_LIST)) {
                struct triple *list;
                list = ptr;
-               ptr = list->left;
+               ptr = RHS(list, 0);
                do {
                        result = do_walk_triple(state, ptr, depth + 1, cb);
                        if (ptr->next->prev != ptr) {
@@ -8171,7 +9486,7 @@ static int do_walk_triple(struct compile_state *state,
                        }
                        ptr = ptr->next;
                        
-               } while((result == 0) && (ptr != list->left));
+               } while((result == 0) && (ptr != RHS(list, 0)));
        }
        return result;
 }
@@ -8202,40 +9517,16 @@ static int do_print_triple(struct compile_state *state, struct triple *ins, int
                return 0;
 #endif
        }
-       else if ((op == OP_LABEL) && (ins->use)) {
+       if ((op == OP_LABEL) && (ins->use)) {
                printf("\n%p:\n", ins);
        }
-       else if (op == OP_INTCONST) {
-               do_print_prefix(depth);
-               printf("(%p) %-7s %08lx             @ %s:%d.%d\n",
-                       ins, tops(ins->op), ins->u.cval,
-                       ins->filename, ins->line, ins->col);
-               return 0;
-       }
-       else if (op == OP_SDECL) {
-               do_print_prefix(depth);
-               printf("(%p) %-7s %-10p            @ %s:%d.%d\n",
-                       ins, tops(ins->op), ins->left,
-                       ins->filename, ins->line, ins->col);
-               do_print_triple(state, ins->left, depth + 1);
-       }
        do_print_prefix(depth);
-       printf("%s(%p) %-7s %-10p %-10p @ %s:%d.%d\n",
-               (op == OP_LIST)? "list: ": "", 
-               ins, tops(ins->op), ins->left, ins->right,
-               ins->filename, ins->line, ins->col);
+       display_triple(stdout, ins);
+
        if ((ins->op == OP_BRANCH) && ins->use) {
                internal_error(state, ins, "branch used?");
        }
-#if 0
-       {
-               struct triple_set *user;
-               for(user = ins->use; user; user = user->next) {
-                       printf("use: %p\n", user->member);
-               }
-       }
-#endif
-       if (triple_is_branch(ins)) {
+       if (triple_is_branch(state, ins)) {
                printf("\n");
        }
        return 0;
@@ -8316,15 +9607,15 @@ static struct block *basic_block(struct compile_state *state,
                }
                block->last = ptr;
                /* If ptr->u is not used remember where the baic block is */
-               if (!is_const(ptr)) {
+               if (triple_stores_block(state, ptr)) {
                        ptr->u.block = block;
                }
                if (ptr->op == OP_BRANCH) {
                        break;
                }
                ptr = ptr->next;
-       } while (ptr != state->main_function->left);
-       if (ptr == state->main_function->left)
+       } while (ptr != RHS(state->main_function, 0));
+       if (ptr == RHS(state->main_function, 0))
                return block;
        op = ptr->op;
        if (op == OP_LABEL) {
@@ -8335,10 +9626,10 @@ static struct block *basic_block(struct compile_state *state,
        else if (op == OP_BRANCH) {
                block->left = 0;
                /* Trace the branch target */
-               block->right = basic_block(state, ptr->left);
+               block->right = basic_block(state, TARG(ptr, 0));
                use_block(block->right, block);
                /* If there is a test trace the branch as well */
-               if (ptr->right) {
+               if (TRIPLE_RHS(ptr->sizes)) {
                        block->left = basic_block(state, ptr->next);
                        use_block(block->left, block);
                }
@@ -8357,17 +9648,20 @@ static void walk_blocks(struct compile_state *state,
        struct triple *ptr, *first;
        struct block *last_block;
        last_block = 0;
-       first = state->main_function->left;
+       first = RHS(state->main_function, 0);
        ptr = first;
        do {
                struct block *block;
-               if (ptr->op == OP_LABEL) {
+               if (triple_stores_block(state, ptr)) {
                        block = ptr->u.block;
                        if (block && (block != last_block)) {
                                cb(state, block, arg);
                        }
                        last_block = block;
                }
+               if (block && (block->last == ptr)) {
+                       block = 0;
+               }
                ptr = ptr->next;
        } while(ptr != first);
 }
@@ -8375,9 +9669,11 @@ static void walk_blocks(struct compile_state *state,
 static void print_block(
        struct compile_state *state, struct block *block, void *arg)
 {
+       struct block_set *user;
        struct triple *ptr;
+       FILE *fp = arg;
 
-       printf("\nblock: %p (%d), %p<-%p %p<-%p\n", 
+       fprintf(fp, "\nblock: %p (%d)  %p<-%p %p<-%p\n", 
                block, 
                block->vertex,
                block->left, 
@@ -8385,119 +9681,71 @@ static void print_block(
                block->right, 
                block->right && block->right->use?block->right->use->member : 0);
        if (block->first->op == OP_LABEL) {
-               printf("%p:\n", block->first);
+               fprintf(fp, "%p:\n", block->first);
        }
        for(ptr = block->first; ; ptr = ptr->next) {
-               struct triple_set *user;
-               int op = ptr->op;
-               
-               if (!IS_CONST_OP(op)) {
-                       if (ptr->u.block != block) {
-                               internal_error(state, ptr, 
-                                       "Wrong block pointer: %p\n",
-                                       ptr->u.block);
-                       }
-               }
-               if (op == OP_ADECL) {
-                       for(user = ptr->use; user; user = user->next) {
-                               if (!user->member->u.block) {
-                                       internal_error(state, user->member, 
-                                               "Use %p not in a block?\n",
-                                               user->member);
-                               }
-                       }
-               }
-               if (op == OP_INTCONST) {
-                       printf("(%p) %3d %-7s %08lx              @ %s:%d.%d\n",
-                               ptr, ID_REG(ptr->id), tops(ptr->op), 
-                               ptr->u.cval,
-                               ptr->filename, ptr->line, ptr->col);
-               }
-               else if (op == OP_PHI) {
-                       struct triple **slot;
-                       struct block *block;
-                       int edge;
-                       block = ptr->u.block;
-                       slot  = (struct triple **)(ptr->left);
-                       printf("(%p) %3d %-7s",
-                               ptr, ID_REG(ptr->id), tops(ptr->op));
-                       for(edge = 0; edge < block->users; edge++) {
-                               printf(" %-10p", slot[edge]);
-                       }
-                       printf(" @%s:%d.%d\n", 
-                               ptr->filename, ptr->line, ptr->col);
-               }
-               else {
-                       printf("(%p) %3d %-7s %-10p %-10p @ %s:%d.%d\n",
-                               ptr, ID_REG(ptr->id), tops(ptr->op), 
-                               ptr->left, ptr->right,
-                               ptr->filename, ptr->line, ptr->col);
-               }
-               /* Sanity checks... */
-               valid_op(state, ptr);
-               for(user = ptr->use; user; user = user->next) {
-                       struct triple *use;
-                       use = user->member;
-                       valid_op(state, use);
-                       if (!IS_CONST_OP(user->member->op) &&
-                               !user->member->u.block) {
-                               internal_error(state, user->member,
-                                       "Use %p not in a block?",
-                                       user->member);
-                       }
-               }
-
+               display_triple(fp, ptr);
                if (ptr == block->last)
                        break;
        }
-       printf("\n");
+       fprintf(fp, "users %d: ", block->users);
+       for(user = block->use; user; user = user->next) {
+               fprintf(fp, "%p (%d) ", 
+                       user->member,
+                       user->member->vertex);
+       }
+       fprintf(fp,"\n\n");
 }
 
 
-static void print_blocks(struct compile_state *state)
+static void print_blocks(struct compile_state *state, FILE *fp)
 {
-       printf("--------------- blocks ---------------\n");
-       walk_blocks(state, print_block, 0);
+       fprintf(fp, "--------------- blocks ---------------\n");
+       walk_blocks(state, print_block, fp);
 }
 
 static void prune_nonblock_triples(struct compile_state *state)
 {
        struct block *block;
-       struct triple *first, *ins;
+       struct triple *first, *ins, *next;
        /* Delete the triples not in a basic block */
-       first = state->main_function->left;
+       first = RHS(state->main_function, 0);
        block = 0;
        ins = first;
        do {
+               next = ins->next;
                if (ins->op == OP_LABEL) {
                        block = ins->u.block;
                }
-               ins = ins->next;
                if (!block) {
-                       release_triple(state, ins->prev);
+                       release_triple(state, ins);
+               }
+               if (block && block->last == ins) {
+                       block = 0;
                }
+               ins = next;
        } while(ins != first);
 }
 
 static void setup_basic_blocks(struct compile_state *state)
 {
+       if (!triple_stores_block(state, RHS(state->main_function, 0)) ||
+               !triple_stores_block(state, RHS(state->main_function,0)->prev)) {
+               internal_error(state, 0, "ins will not store block?");
+       }
        /* Find the basic blocks */
        state->last_vertex = 0;
-       state->first_block = basic_block(state, state->main_function->left);
+       state->first_block = basic_block(state, RHS(state->main_function,0));
        /* Delete the triples not in a basic block */
        prune_nonblock_triples(state);
        /* Find the last basic block */
-       state->last_block = state->main_function->left->prev->u.block;
+       state->last_block = RHS(state->main_function, 0)->prev->u.block;
        if (!state->last_block) {
                internal_error(state, 0, "end not used?");
        }
-       /* Insert an extra unused edge from start to the end 
-        * This helps with reverse control flow calculations.
-        */
-       use_block(state->first_block, state->last_block);
        /* If we are debugging print what I have just done */
        if (state->debug & DEBUG_BASIC_BLOCKS) {
-               print_blocks(state);
+               print_blocks(state, stdout);
                print_control_flow(state);
        }
 }
@@ -8577,10 +9825,10 @@ static void free_basic_blocks(struct compile_state *state)
        free_basic_block(state, state->first_block);
        state->last_vertex = 0;
        state->first_block = state->last_block = 0;
-       first = state->main_function->left;
+       first = RHS(state->main_function, 0);
        ins = first;
        do {
-               if (!is_const(ins)) {
+               if (triple_stores_block(state, ins)) {
                        ins->u.block = 0;
                }
                ins = ins->next;
@@ -8646,7 +9894,8 @@ static int initialize_sdblock(struct sdom_block *sd,
        return vertex;
 }
 
-static int initialize_sdpblock(struct sdom_block *sd,
+static int initialize_sdpblock(
+       struct compile_state *state, struct sdom_block *sd,
        struct block *parent, struct block *block, int vertex)
 {
        struct block_set *user;
@@ -8663,7 +9912,38 @@ static int initialize_sdpblock(struct sdom_block *sd,
        sd[vertex].ancestor = 0;
        sd[vertex].vertex   = vertex;
        for(user = block->use; user; user = user->next) {
-               vertex = initialize_sdpblock(sd, block, user->member, vertex);
+               vertex = initialize_sdpblock(state, sd, block, user->member, vertex);
+       }
+       return vertex;
+}
+
+static int setup_sdpblocks(struct compile_state *state, struct sdom_block *sd)
+{
+       struct block *block;
+       int vertex;
+       /* Setup as many sdpblocks as possible without using fake edges */
+       vertex = initialize_sdpblock(state, sd, 0, state->last_block, 0);
+
+       /* Walk through the graph and find unconnected blocks.  If 
+        * we can, add a fake edge from the unconnected blocks to the
+        * end of the graph.
+        */
+       block = state->first_block->last->next->u.block;
+       for(; block && block != state->first_block; block =  block->last->next->u.block) {
+               if (sd[block->vertex].block == block) {
+                       continue;
+               }
+               if (block->left != 0) {
+                       continue;
+               }
+
+#if DEBUG_SDP_BLOCKS
+               fprintf(stderr, "Adding %d\n", vertex +1);
+#endif
+
+               block->left = state->last_block;
+               use_block(block->left, block);
+               vertex = initialize_sdpblock(state, sd, state->last_block, block, vertex);
        }
        return vertex;
 }
@@ -8903,10 +10183,15 @@ static void find_immediate_dominators(struct compile_state *state)
 static void find_post_dominators(struct compile_state *state)
 {
        struct sdom_block *sd;
+       int vertex;
        /* Step 1 initialize the basic block information */
        sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
 
-       initialize_sdpblock(sd, 0, state->last_block, 0);
+       vertex = setup_sdpblocks(state, sd);
+       if (vertex != state->last_vertex) {
+               internal_error(state, 0, "missing %d blocks\n",
+                       state->last_vertex - vertex);
+       }
 
        /* Step 2 compute the semidominators */
        /* Step 3 implicitly define the immediate dominator of each vertex */
@@ -8980,33 +10265,26 @@ static void find_block_ipdomf(struct compile_state *state, struct block *block)
        }
 }
 
-static int print_dominated(
-       struct compile_state *state, struct block *block, int vertex)
+static void print_dominated(
+       struct compile_state *state, struct block *block, void *arg)
 {
        struct block_set *user;
+       FILE *fp = arg;
 
-       if (!block || (block->vertex != vertex + 1)) {
-               return vertex;
-       }
-       vertex += 1;
-
-       printf("%d:", block->vertex);
+       fprintf(fp, "%d:", block->vertex);
        for(user = block->idominates; user; user = user->next) {
-               printf(" %d", user->member->vertex);
+               fprintf(fp, " %d", user->member->vertex);
                if (user->member->idom != block) {
                        internal_error(state, user->member->first, "bad idom");
                }
        }
-       printf("\n");
-       vertex = print_dominated(state, block->left, vertex);
-       vertex = print_dominated(state, block->right, vertex);
-       return vertex;
+       fprintf(fp,"\n");
 }
 
-static void print_dominators(struct compile_state *state)
+static void print_dominators(struct compile_state *state, FILE *fp)
 {
-       printf("\ndominates\n");
-       print_dominated(state, state->first_block, 0);
+       fprintf(fp, "\ndominates\n");
+       walk_blocks(state, print_dominated, fp);
 }
 
 
@@ -9045,7 +10323,7 @@ static void analyze_idominators(struct compile_state *state)
        find_block_domf(state, state->first_block);
        /* If debuging print the print what I have just found */
        if (state->debug & DEBUG_FDOMINATORS) {
-               print_dominators(state);
+               print_dominators(state, stdout);
                print_dominance_frontiers(state);
                print_control_flow(state);
        }
@@ -9053,34 +10331,26 @@ static void analyze_idominators(struct compile_state *state)
 
 
 
-static int print_ipdominated(
-       struct compile_state *state, struct block *block, int vertex)
+static void print_ipdominated(
+       struct compile_state *state, struct block *block, void *arg)
 {
        struct block_set *user;
+       FILE *fp = arg;
 
-       if (!block || (block->vertex != vertex + 1)) {
-               return vertex;
-       }
-       vertex += 1;
-
-       printf("%d:", block->vertex);
+       fprintf(fp, "%d:", block->vertex);
        for(user = block->ipdominates; user; user = user->next) {
-               printf(" %d", user->member->vertex);
+               fprintf(fp, " %d", user->member->vertex);
                if (user->member->ipdom != block) {
                        internal_error(state, user->member->first, "bad ipdom");
                }
        }
-       printf("\n");
-       for(user = block->use; user; user = user->next) {
-               vertex = print_ipdominated(state, user->member, vertex);
-       }
-       return vertex;
+       fprintf(fp, "\n");
 }
 
-static void print_ipdominators(struct compile_state *state)
+static void print_ipdominators(struct compile_state *state, FILE *fp)
 {
-       printf("\nipdominates\n");
-       print_ipdominated(state, state->last_block, 0);
+       fprintf(fp, "\nipdominates\n");
+       walk_blocks(state, print_ipdominated, fp);
 }
 
 static int print_pfrontiers(
@@ -9118,12 +10388,41 @@ static void analyze_ipdominators(struct compile_state *state)
        find_block_ipdomf(state, state->last_block);
        /* If debuging print the print what I have just found */
        if (state->debug & DEBUG_RDOMINATORS) {
-               print_ipdominators(state);
+               print_ipdominators(state, stdout);
                print_ipdominance_frontiers(state);
                print_control_flow(state);
        }
 }
 
+static int bdominates(struct compile_state *state,
+       struct block *dom, struct block *sub)
+{
+       while(sub && (sub != dom)) {
+               sub = sub->idom;
+       }
+       return sub == dom;
+}
+
+static int tdominates(struct compile_state *state,
+       struct triple *dom, struct triple *sub)
+{
+       struct block *bdom, *bsub;
+       int result;
+       bdom = block_of_triple(state, dom);
+       bsub = block_of_triple(state, sub);
+       if (bdom != bsub) {
+               result = bdominates(state, bdom, bsub);
+       } 
+       else {
+               struct triple *ins;
+               ins = sub;
+               while((ins != bsub->first) && (ins != dom)) {
+                       ins = ins->prev;
+               }
+               result = (ins == dom);
+       }
+       return result;
+}
 
 static void insert_phi_operations(struct compile_state *state)
 {
@@ -9132,24 +10431,26 @@ static void insert_phi_operations(struct compile_state *state)
        int *has_already, *work;
        struct block *work_list, **work_list_tail;
        int iter;
-       struct triple *var;
+       struct triple *var, *vnext;
 
        size = sizeof(int) * (state->last_vertex + 1);
        has_already = xcmalloc(size, "has_already");
        work =        xcmalloc(size, "work");
        iter = 0;
 
-       first = state->main_function->left;
-       for(var = first->next; var != first ; var = var->next) {
+       first = RHS(state->main_function, 0);
+       for(var = first->next; var != first ; var = vnext) {
                struct block *block;
-               struct triple_set *user;
+               struct triple_set *user, *unext;
+               vnext = var->next;
                if ((var->op != OP_ADECL) || !var->use) {
                        continue;
                }
                iter += 1;
                work_list = 0;
                work_list_tail = &work_list;
-               for(user = var->use; user; user = user->next) {
+               for(user = var->use; user; user = unext) {
+                       unext = user->next;
                        if (user->member->op == OP_READ) {
                                continue;
                        }
@@ -9160,6 +10461,11 @@ static void insert_phi_operations(struct compile_state *state)
                        block = user->member->u.block;
                        if (!block) {
                                warning(state, user->member, "dead code");
+                               release_triple(state, user->member);
+                               continue;
+                       }
+                       if (work[block->vertex] >= iter) {
+                               continue;
                        }
                        work[block->vertex] = iter;
                        *work_list_tail = block;
@@ -9180,13 +10486,12 @@ static void insert_phi_operations(struct compile_state *state)
                                /* Count how many edges flow into this block */
                                in_edges = front->users;
                                /* Insert a phi function for this variable */
-                               phi = xcmalloc(in_edges * sizeof(*phi),"phi");
-                               phi = triple(state, OP_PHI, var->type, 
-                                       phi, var);
-                               phi->filename = front->first->filename;
-                               phi->line = front->first->line;
-                               phi->col  = front->first->col;
+                               get_occurance(front->first->occurance);
+                               phi = alloc_triple(
+                                       state, OP_PHI, var->type, -1, in_edges, 
+                                       front->first->occurance);
                                phi->u.block = front;
+                               MISC(phi, 0) = var;
                                use_triple(var, phi);
                                /* Insert the phi functions immediately after the label */
                                insert_triple(state, front->first->next, phi);
@@ -9194,7 +10499,7 @@ static void insert_phi_operations(struct compile_state *state)
                                        front->last = front->first->next;
                                }
                                has_already[front->vertex] = iter;
-                               
+
                                /* If necessary plan to visit the basic block */
                                if (work[front->vertex] >= iter) {
                                        continue;
@@ -9235,14 +10540,19 @@ static void fixup_block_phi_variables(
        for(ptr = block->first; ; ptr = ptr->next) {
                if (ptr->op == OP_PHI) {
                        struct triple *var, *val, **slot;
-                       var = ptr->right;
+                       var = MISC(ptr, 0);
+                       if (!var) {
+                               internal_error(state, ptr, "no var???");
+                       }
                        /* Find the current value of the variable */
                        val = var->use->member;
                        if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
                                internal_error(state, val, "bad value in phi");
                        }
-                       slot = (struct triple **)(ptr->left);
-                       slot += edge;
+                       if (edge >= TRIPLE_RHS(ptr->sizes)) {
+                               internal_error(state, ptr, "edges > phi rhs");
+                       }
+                       slot = &RHS(ptr, edge);
                        if ((*slot != 0) && (*slot != val)) {
                                internal_error(state, ptr, "phi already bound on this edge");
                        }
@@ -9274,7 +10584,7 @@ static void rename_block_variables(
                /* RHS(A) */
                if (ptr->op == OP_READ) {
                        struct triple *var, *val;
-                       var = ptr->left;
+                       var = RHS(ptr, 0);
                        unuse_triple(var, ptr);
                        if (!var->use) {
                                error(state, ptr, "variable used without being set");
@@ -9290,20 +10600,34 @@ static void rename_block_variables(
                }
                /* LHS(A) */
                if (ptr->op == OP_WRITE) {
-                       struct triple *var, *val;
-                       var = ptr->left;
-                       val = ptr->right;
+                       struct triple *var, *val, *tval;
+                       var = RHS(ptr, 0);
+                       tval = val = RHS(ptr, 1);
                        if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
-                               internal_error(state, val, "bad value in write");
+                               internal_error(state, ptr, "bad value in write");
                        }
-                       propogate_use(state, ptr, val);
+                       /* Insert a copy if the types differ */
+                       if (!equiv_types(ptr->type, val->type)) {
+                               if (val->op == OP_INTCONST) {
+                                       tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
+                                       tval->u.cval = val->u.cval;
+                               }
+                               else {
+                                       tval = pre_triple(state, ptr, OP_COPY, ptr->type, val, 0);
+                                       use_triple(val, tval);
+                               }
+                               unuse_triple(val, ptr);
+                               RHS(ptr, 1) = tval;
+                               use_triple(tval, ptr);
+                       }
+                       propogate_use(state, ptr, tval);
                        unuse_triple(var, ptr);
                        /* Push OP_WRITE ptr->right onto a stack of variable uses */
-                       push_triple(var, val);
+                       push_triple(var, tval);
                }
                if (ptr->op == OP_PHI) {
                        struct triple *var;
-                       var = ptr->right;
+                       var = MISC(ptr, 0);
                        /* Push OP_PHI onto a stack of variable uses */
                        push_triple(var, ptr);
                }
@@ -9328,15 +10652,15 @@ static void rename_block_variables(
                }
                if (ptr->op == OP_WRITE) {
                        struct triple *var;
-                       var = ptr->left;
+                       var = RHS(ptr, 0);
                        /* Pop OP_WRITE ptr->right from the stack of variable uses */
-                       pop_triple(var, ptr->right);
+                       pop_triple(var, RHS(ptr, 1));
                        release_triple(state, ptr);
                        continue;
                }
                if (ptr->op == OP_PHI) {
                        struct triple *var;
-                       var = ptr->right;
+                       var = MISC(ptr, 0);
                        /* Pop OP_WRITE ptr->right from the stack of variable uses */
                        pop_triple(var, ptr);
                }
@@ -9367,11 +10691,11 @@ static void prune_block_variables(struct compile_state *state,
                                if (use->op != OP_PHI) {
                                        internal_error(state, use, "decl still used");
                                }
-                               if (use->right != ptr) {
+                               if (MISC(use, 0) != ptr) {
                                        internal_error(state, use, "bad phi use of decl");
                                }
                                unuse_triple(ptr, use);
-                               use->right = 0;
+                               MISC(use, 0) = 0;
                        }
                        release_triple(state, ptr);
                        continue;
@@ -9389,13 +10713,51 @@ static void transform_to_ssa_form(struct compile_state *state)
        insert_phi_operations(state);
 #if 0
        printf("@%s:%d\n", __FILE__, __LINE__);
-       print_blocks(state);
+       print_blocks(state, stdout);
 #endif
        rename_block_variables(state, state->first_block);
        prune_block_variables(state, state->first_block);
 }
 
 
+static void clear_vertex(
+       struct compile_state *state, struct block *block, void *arg)
+{
+       block->vertex = 0;
+}
+
+static void mark_live_block(
+       struct compile_state *state, struct block *block, int *next_vertex)
+{
+       /* See if this is a block that has not been marked */
+       if (block->vertex != 0) {
+               return;
+       }
+       block->vertex = *next_vertex;
+       *next_vertex += 1;
+       if (triple_is_branch(state, block->last)) {
+               struct triple **targ;
+               targ = triple_targ(state, block->last, 0);
+               for(; targ; targ = triple_targ(state, block->last, targ)) {
+                       if (!*targ) {
+                               continue;
+                       }
+                       if (!triple_stores_block(state, *targ)) {
+                               internal_error(state, 0, "bad targ");
+                       }
+                       mark_live_block(state, (*targ)->u.block, next_vertex);
+               }
+       }
+       else if (block->last->next != RHS(state->main_function, 0)) {
+               struct triple *ins;
+               ins = block->last->next;
+               if (!triple_stores_block(state, ins)) {
+                       internal_error(state, 0, "bad block start");
+               }
+               mark_live_block(state, ins->u.block, next_vertex);
+       }
+}
+
 static void transform_from_ssa_form(struct compile_state *state)
 {
        /* To get out of ssa form we insert moves on the incoming
@@ -9403,22 +10765,48 @@ static void transform_from_ssa_form(struct compile_state *state)
         */
        struct triple *first;
        struct triple *phi, *next;
+       int next_vertex;
+
+       /* Walk the control flow to see which blocks remain alive */
+       walk_blocks(state, clear_vertex, 0);
+       next_vertex = 1;
+       mark_live_block(state, state->first_block, &next_vertex);
 
        /* Walk all of the operations to find the phi functions */
-       first = state->main_function->left;
+       first = RHS(state->main_function, 0);
        for(phi = first->next; phi != first ; phi = next) {
                struct block_set *set;
                struct block *block;
                struct triple **slot;
                struct triple *var, *read;
-               int edge;
+               struct triple_set *use, *use_next;
+               int edge, used;
                next = phi->next;
                if (phi->op != OP_PHI) {
                        continue;
                }
                block = phi->u.block;
-               slot  = (struct triple **)(phi->left);
+               slot  = &RHS(phi, 0);
+
+               /* Forget uses from code in dead blocks */
+               for(use = phi->use; use; use = use_next) {
+                       struct block *ublock;
+                       struct triple **expr;
+                       use_next = use->next;
+                       ublock = block_of_triple(state, use->member);
+                       if ((use->member == phi) || (ublock->vertex != 0)) {
+                               continue;
+                       }
+                       expr = triple_rhs(state, use->member, 0);
+                       for(; expr; expr = triple_rhs(state, use->member, expr)) {
+                               if (*expr == phi) {
+                                       *expr = 0;
+                               }
+                       }
+                       unuse_triple(phi, use->member);
+               }
 
+#warning "CHECK_ME does the OP_ADECL need to be placed somewhere that dominates all of the incoming phi edges?"
                /* A variable to replace the phi function */
                var = post_triple(state, phi, OP_ADECL, phi->type, 0,0);
                /* A read of the single value that is set into the variable */
@@ -9433,25 +10821,330 @@ static void transform_from_ssa_form(struct compile_state *state)
                for(edge = 0, set = block->use; set; set = set->next, edge++) {
                        struct block *eblock;
                        struct triple *move;
-                       struct triple *val;
+                       struct triple *val, *base;
                        eblock = set->member;
                        val = slot[edge];
+                       slot[edge] = 0;
                        unuse_triple(val, phi);
 
-                       if (val == phi) {
+                       if (!val || (val == &zero_triple) ||
+                               (block->vertex == 0) || (eblock->vertex == 0) ||
+                               (val == phi) || (val == read)) {
                                continue;
                        }
-
-                       move = post_triple(state, 
-                               val, OP_WRITE, phi->type, var, val);
-                       use_triple(val, move);
+                       
+                       /* Make certain the write is placed in the edge block... */
+                       base = eblock->first;
+                       if (block_of_triple(state, val) == eblock) {
+                               base = val;
+                       }
+                       move = post_triple(state, base, OP_WRITE, phi->type, var, val);
+                       use_triple(val, move);
                        use_triple(var, move);
+               }               
+               /* See if there are any writers of var */
+               used = 0;
+               for(use = var->use; use; use = use->next) {
+                       if ((use->member->op == OP_WRITE) &&
+                               (RHS(use->member, 0) == var)) {
+                               used = 1;
+                       }
                }
+               /* If var is not used free it */
+               if (!used) {
+                       unuse_triple(var, read);
+                       free_triple(state, read);
+                       free_triple(state, var);
+               }
+
+               /* Release the phi function */
                release_triple(state, phi);
        }
        
 }
 
+
+/* 
+ * Register conflict resolution
+ * =========================================================
+ */
+
+static struct reg_info find_def_color(
+       struct compile_state *state, struct triple *def)
+{
+       struct triple_set *set;
+       struct reg_info info;
+       info.reg = REG_UNSET;
+       info.regcm = 0;
+       if (!triple_is_def(state, def)) {
+               return info;
+       }
+       info = arch_reg_lhs(state, def, 0);
+       if (info.reg >= MAX_REGISTERS) {
+               info.reg = REG_UNSET;
+       }
+       for(set = def->use; set; set = set->next) {
+               struct reg_info tinfo;
+               int i;
+               i = find_rhs_use(state, set->member, def);
+               if (i < 0) {
+                       continue;
+               }
+               tinfo = arch_reg_rhs(state, set->member, i);
+               if (tinfo.reg >= MAX_REGISTERS) {
+                       tinfo.reg = REG_UNSET;
+               }
+               if ((tinfo.reg != REG_UNSET) && 
+                       (info.reg != REG_UNSET) &&
+                       (tinfo.reg != info.reg)) {
+                       internal_error(state, def, "register conflict");
+               }
+               if ((info.regcm & tinfo.regcm) == 0) {
+                       internal_error(state, def, "regcm conflict %x & %x == 0",
+                               info.regcm, tinfo.regcm);
+               }
+               if (info.reg == REG_UNSET) {
+                       info.reg = tinfo.reg;
+               }
+               info.regcm &= tinfo.regcm;
+       }
+       if (info.reg >= MAX_REGISTERS) {
+               internal_error(state, def, "register out of range");
+       }
+       return info;
+}
+
+static struct reg_info find_lhs_pre_color(
+       struct compile_state *state, struct triple *ins, int index)
+{
+       struct reg_info info;
+       int zlhs, zrhs, i;
+       zrhs = TRIPLE_RHS(ins->sizes);
+       zlhs = TRIPLE_LHS(ins->sizes);
+       if (!zlhs && triple_is_def(state, ins)) {
+               zlhs = 1;
+       }
+       if (index >= zlhs) {
+               internal_error(state, ins, "Bad lhs %d", index);
+       }
+       info = arch_reg_lhs(state, ins, index);
+       for(i = 0; i < zrhs; i++) {
+               struct reg_info rinfo;
+               rinfo = arch_reg_rhs(state, ins, i);
+               if ((info.reg == rinfo.reg) &&
+                       (rinfo.reg >= MAX_REGISTERS)) {
+                       struct reg_info tinfo;
+                       tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
+                       info.reg = tinfo.reg;
+                       info.regcm &= tinfo.regcm;
+                       break;
+               }
+       }
+       if (info.reg >= MAX_REGISTERS) {
+               info.reg = REG_UNSET;
+       }
+       return info;
+}
+
+static struct reg_info find_rhs_post_color(
+       struct compile_state *state, struct triple *ins, int index);
+
+static struct reg_info find_lhs_post_color(
+       struct compile_state *state, struct triple *ins, int index)
+{
+       struct triple_set *set;
+       struct reg_info info;
+       struct triple *lhs;
+#if DEBUG_TRIPLE_COLOR
+       fprintf(stderr, "find_lhs_post_color(%p, %d)\n",
+               ins, index);
+#endif
+       if ((index == 0) && triple_is_def(state, ins)) {
+               lhs = ins;
+       }
+       else if (index < TRIPLE_LHS(ins->sizes)) {
+               lhs = LHS(ins, index);
+       }
+       else {
+               internal_error(state, ins, "Bad lhs %d", index);
+               lhs = 0;
+       }
+       info = arch_reg_lhs(state, ins, index);
+       if (info.reg >= MAX_REGISTERS) {
+               info.reg = REG_UNSET;
+       }
+       for(set = lhs->use; set; set = set->next) {
+               struct reg_info rinfo;
+               struct triple *user;
+               int zrhs, i;
+               user = set->member;
+               zrhs = TRIPLE_RHS(user->sizes);
+               for(i = 0; i < zrhs; i++) {
+                       if (RHS(user, i) != lhs) {
+                               continue;
+                       }
+                       rinfo = find_rhs_post_color(state, user, i);
+                       if ((info.reg != REG_UNSET) &&
+                               (rinfo.reg != REG_UNSET) &&
+                               (info.reg != rinfo.reg)) {
+                               internal_error(state, ins, "register conflict");
+                       }
+                       if ((info.regcm & rinfo.regcm) == 0) {
+                               internal_error(state, ins, "regcm conflict %x & %x == 0",
+                                       info.regcm, rinfo.regcm);
+                       }
+                       if (info.reg == REG_UNSET) {
+                               info.reg = rinfo.reg;
+                       }
+                       info.regcm &= rinfo.regcm;
+               }
+       }
+#if DEBUG_TRIPLE_COLOR
+       fprintf(stderr, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
+               ins, index, info.reg, info.regcm);
+#endif
+       return info;
+}
+
+static struct reg_info find_rhs_post_color(
+       struct compile_state *state, struct triple *ins, int index)
+{
+       struct reg_info info, rinfo;
+       int zlhs, i;
+#if DEBUG_TRIPLE_COLOR
+       fprintf(stderr, "find_rhs_post_color(%p, %d)\n",
+               ins, index);
+#endif
+       rinfo = arch_reg_rhs(state, ins, index);
+       zlhs = TRIPLE_LHS(ins->sizes);
+       if (!zlhs && triple_is_def(state, ins)) {
+               zlhs = 1;
+       }
+       info = rinfo;
+       if (info.reg >= MAX_REGISTERS) {
+               info.reg = REG_UNSET;
+       }
+       for(i = 0; i < zlhs; i++) {
+               struct reg_info linfo;
+               linfo = arch_reg_lhs(state, ins, i);
+               if ((linfo.reg == rinfo.reg) &&
+                       (linfo.reg >= MAX_REGISTERS)) {
+                       struct reg_info tinfo;
+                       tinfo = find_lhs_post_color(state, ins, i);
+                       if (tinfo.reg >= MAX_REGISTERS) {
+                               tinfo.reg = REG_UNSET;
+                       }
+                       info.regcm &= linfo.regcm;
+                       info.regcm &= tinfo.regcm;
+                       if (info.reg != REG_UNSET) {
+                               internal_error(state, ins, "register conflict");
+                       }
+                       if (info.regcm == 0) {
+                               internal_error(state, ins, "regcm conflict");
+                       }
+                       info.reg = tinfo.reg;
+               }
+       }
+#if DEBUG_TRIPLE_COLOR
+       fprintf(stderr, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
+               ins, index, info.reg, info.regcm);
+#endif
+       return info;
+}
+
+static struct reg_info find_lhs_color(
+       struct compile_state *state, struct triple *ins, int index)
+{
+       struct reg_info pre, post, info;
+#if DEBUG_TRIPLE_COLOR
+       fprintf(stderr, "find_lhs_color(%p, %d)\n",
+               ins, index);
+#endif
+       pre = find_lhs_pre_color(state, ins, index);
+       post = find_lhs_post_color(state, ins, index);
+       if ((pre.reg != post.reg) &&
+               (pre.reg != REG_UNSET) &&
+               (post.reg != REG_UNSET)) {
+               internal_error(state, ins, "register conflict");
+       }
+       info.regcm = pre.regcm & post.regcm;
+       info.reg = pre.reg;
+       if (info.reg == REG_UNSET) {
+               info.reg = post.reg;
+       }
+#if DEBUG_TRIPLE_COLOR
+       fprintf(stderr, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
+               ins, index, info.reg, info.regcm,
+               pre.reg, pre.regcm, post.reg, post.regcm);
+#endif
+       return info;
+}
+
+static struct triple *post_copy(struct compile_state *state, struct triple *ins)
+{
+       struct triple_set *entry, *next;
+       struct triple *out;
+       struct reg_info info, rinfo;
+
+       info = arch_reg_lhs(state, ins, 0);
+       out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
+       use_triple(RHS(out, 0), out);
+       /* Get the users of ins to use out instead */
+       for(entry = ins->use; entry; entry = next) {
+               int i;
+               next = entry->next;
+               if (entry->member == out) {
+                       continue;
+               }
+               i = find_rhs_use(state, entry->member, ins);
+               if (i < 0) {
+                       continue;
+               }
+               rinfo = arch_reg_rhs(state, entry->member, i);
+               if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
+                       continue;
+               }
+               replace_rhs_use(state, ins, out, entry->member);
+       }
+       transform_to_arch_instruction(state, out);
+       return out;
+}
+
+static struct triple *typed_pre_copy(
+       struct compile_state *state, struct type *type, struct triple *ins, int index)
+{
+       /* Carefully insert enough operations so that I can
+        * enter any operation with a GPR32.
+        */
+       struct triple *in;
+       struct triple **expr;
+       unsigned classes;
+       struct reg_info info;
+       if (ins->op == OP_PHI) {
+               internal_error(state, ins, "pre_copy on a phi?");
+       }
+       classes = arch_type_to_regcm(state, type);
+       info = arch_reg_rhs(state, ins, index);
+       expr = &RHS(ins, index);
+       if ((info.regcm & classes) == 0) {
+               internal_error(state, ins, "pre_copy with no register classes");
+       }
+       in = pre_triple(state, ins, OP_COPY, type, *expr, 0);
+       unuse_triple(*expr, ins);
+       *expr = in;
+       use_triple(RHS(in, 0), in);
+       use_triple(in, ins);
+       transform_to_arch_instruction(state, in);
+       return in;
+       
+}
+static struct triple *pre_copy(
+       struct compile_state *state, struct triple *ins, int index)
+{
+       return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
+}
+
+
 static void insert_copies_to_phi(struct compile_state *state)
 {
        /* To get out of ssa form we insert moves on the incoming
@@ -9461,21 +11154,25 @@ static void insert_copies_to_phi(struct compile_state *state)
        struct triple *phi;
 
        /* Walk all of the operations to find the phi functions */
-       first = state->main_function->left;
+       first = RHS(state->main_function, 0);
        for(phi = first->next; phi != first ; phi = phi->next) {
                struct block_set *set;
                struct block *block;
-               struct triple **slot;
+               struct triple **slot, *copy;
                int edge;
                if (phi->op != OP_PHI) {
                        continue;
                }
-               if (ID_REG(phi->id) == REG_UNSET) {
-                       phi->id = MK_REG_ID(alloc_virtual_reg(), 
-                               ID_REG_CLASSES(phi->id));
-               }
+               phi->id |= TRIPLE_FLAG_POST_SPLIT;
                block = phi->u.block;
-               slot  = (struct triple **)(phi->left);
+               slot  = &RHS(phi, 0);
+               /* Phi's that feed into mandatory live range joins
+                * cause nasty complications.  Insert a copy of
+                * the phi value so I never have to deal with
+                * that in the rest of the code.
+                */
+               copy = post_copy(state, phi);
+               copy->id |= TRIPLE_FLAG_PRE_SPLIT;
                /* Walk all of the incoming edges/blocks and insert moves.
                 */
                for(edge = 0, set = block->use; set; set = set->next, edge++) {
@@ -9490,10 +11187,11 @@ static void insert_copies_to_phi(struct compile_state *state)
                                continue;
                        }
 
+                       get_occurance(val->occurance);
                        move = build_triple(state, OP_COPY, phi->type, val, 0,
-                               val->filename, val->line, val->col);
+                               val->occurance);
                        move->u.block = eblock;
-                       move->id = phi->id;
+                       move->id |= TRIPLE_FLAG_PRE_SPLIT;
                        use_triple(val, move);
                        
                        slot[edge] = move;
@@ -9516,7 +11214,7 @@ static void insert_copies_to_phi(struct compile_state *state)
                                }
                        }
                out:
-                       if (triple_is_branch(ptr)) {
+                       if (triple_is_branch(state, ptr)) {
                                internal_error(state, ptr,
                                        "Could not insert write to phi");
                        }
@@ -9524,6 +11222,7 @@ static void insert_copies_to_phi(struct compile_state *state)
                        if (eblock->last == ptr) {
                                eblock->last = move;
                        }
+                       transform_to_arch_instruction(state, move);
                }
        }
 }
@@ -9643,7 +11342,7 @@ static int phi_in(struct compile_state *state, struct reg_block *blocks,
                if (ptr->op != OP_PHI) {
                        continue;
                }
-               slot = (struct triple **)(ptr->left);
+               slot = &RHS(ptr, 0);
                expr = slot[edge];
                out_change = out_triple(rb, expr);
                if (!out_change) {
@@ -9733,6 +11432,9 @@ static int use_in(struct compile_state *state, struct reg_block *rb)
                        struct triple *rhs, *test;
                        int tdone;
                        rhs = *expr;
+                       if (!rhs) {
+                               continue;
+                       }
                        /* See if rhs is defined in this block */
                        for(tdone = 0, test = ptr; !tdone; test = test->prev) {
                                tdone = (test == block->first);
@@ -9741,10 +11443,6 @@ static int use_in(struct compile_state *state, struct reg_block *rb)
                                        break;
                                }
                        }
-                       /* If the triple is not a definition skip it. */
-                       if (!triple_is_def(ptr)) {
-                               continue;
-                       }
                        /* If I still have a valid rhs add it to in */
                        change |= in_triple(rb, rhs);
                }
@@ -9804,7 +11502,7 @@ static void free_variable_lifetimes(
 
 }
 
-typedef struct triple *(*wvl_cb_t)(
+typedef void (*wvl_cb_t)(
        struct compile_state *state, 
        struct reg_block *blocks, struct triple_reg_set *live, 
        struct reg_block *rb, struct triple *ins, void *arg);
@@ -9838,18 +11536,20 @@ static void walk_variable_lifetimes(struct compile_state *state,
 
                        prev = ptr->prev;
                        done = (ptr == block->first);
+
+                       /* Ensure the current definition is in live */
+                       if (triple_is_def(state, ptr)) {
+                               do_triple_set(&live, ptr, 0);
+                       }
+
+                       /* Inform the callback function of what is
+                        * going on.
+                        */
+                        cb(state, blocks, live, rb, ptr, arg);
                        
                        /* Remove the current definition from live */
                        do_triple_unset(&live, ptr);
-                       
-                       /* If the current instruction was deleted continue */
-                       if (!cb(state, blocks, live, rb, ptr, arg)) {
-                               if (block->last == ptr) {
-                                       block->last = prev;
-                               }
-                               continue;
-                       }
-                       
+
                        /* Add the current uses to live.
                         *
                         * It is safe to skip phi functions because they do
@@ -9863,12 +11563,11 @@ static void walk_variable_lifetimes(struct compile_state *state,
                        expr = triple_rhs(state, ptr, 0);
                        for(;expr; expr = triple_rhs(state, ptr, expr)) {
                                /* If the triple is not a definition skip it. */
-                               if (!triple_is_def(*expr)) {
+                               if (!*expr || !triple_is_def(state, *expr)) {
                                        continue;
                                }
                                do_triple_set(&live, *expr, 0);
                        }
-
                }
                /* Free live */
                for(entry = live; entry; entry = next) {
@@ -9882,7 +11581,7 @@ static int count_triples(struct compile_state *state)
 {
        struct triple *first, *ins;
        int triples = 0;
-       first = state->main_function->left;
+       first = RHS(state->main_function, 0);
        ins = first;
        do {
                triples++;
@@ -9943,7 +11642,7 @@ static void eliminate_inefectual_code(struct compile_state *state)
        work_list = 0;
        work_list_tail = &work_list;
 
-       first = state->main_function->left;
+       first = RHS(state->main_function, 0);
 
        /* Count how many triples I have */
        triples = count_triples(state);
@@ -9965,9 +11664,15 @@ static void eliminate_inefectual_code(struct compile_state *state)
                ins->id = i;
                /* See if it is an operation we always keep */
 #warning "FIXME handle the case of killing a branch instruction"
-               if (!triple_is_pure(state, ins) || triple_is_branch(ins)) {
+               if (!triple_is_pure(state, ins) || triple_is_branch(state, ins)) {
+                       awaken(state, dtriple, &ins, &work_list_tail);
+               }
+#if 1
+               /* Unconditionally keep the very last instruction */
+               else if (ins->next == first) {
                        awaken(state, dtriple, &ins, &work_list_tail);
                }
+#endif
                i++;
                ins = ins->next;
        } while(ins != first);
@@ -9990,6 +11695,10 @@ static void eliminate_inefectual_code(struct compile_state *state)
                        expr = triple_lhs(state, dt->triple, expr);
                        awaken(state, dtriple, expr, &work_list_tail);
                } while(expr);
+               do {
+                       expr = triple_misc(state, dt->triple, expr);
+                       awaken(state, dtriple, expr, &work_list_tail);
+               } while(expr);
                /* Wake up the forward control dependencies */
                do {
                        expr = triple_targ(state, dt->triple, expr);
@@ -10021,13 +11730,175 @@ static void eliminate_inefectual_code(struct compile_state *state)
 }
 
 
+static void insert_mandatory_copies(struct compile_state *state)
+{
+       struct triple *ins, *first;
+
+       /* The object is with a minimum of inserted copies,
+        * to resolve in fundamental register conflicts between
+        * register value producers and consumers.
+        * Theoretically we may be greater than minimal when we
+        * are inserting copies before instructions but that
+        * case should be rare.
+        */
+       first = RHS(state->main_function, 0);
+       ins = first;
+       do {
+               struct triple_set *entry, *next;
+               struct triple *tmp;
+               struct reg_info info;
+               unsigned reg, regcm;
+               int do_post_copy, do_pre_copy;
+               tmp = 0;
+               if (!triple_is_def(state, ins)) {
+                       goto next;
+               }
+               /* Find the architecture specific color information */
+               info = arch_reg_lhs(state, ins, 0);
+               if (info.reg >= MAX_REGISTERS) {
+                       info.reg = REG_UNSET;
+               }
+               
+               reg = REG_UNSET;
+               regcm = arch_type_to_regcm(state, ins->type);
+               do_post_copy = do_pre_copy = 0;
+
+               /* Walk through the uses of ins and check for conflicts */
+               for(entry = ins->use; entry; entry = next) {
+                       struct reg_info rinfo;
+                       int i;
+                       next = entry->next;
+                       i = find_rhs_use(state, entry->member, ins);
+                       if (i < 0) {
+                               continue;
+                       }
+                       
+                       /* Find the users color requirements */
+                       rinfo = arch_reg_rhs(state, entry->member, i);
+                       if (rinfo.reg >= MAX_REGISTERS) {
+                               rinfo.reg = REG_UNSET;
+                       }
+                       
+                       /* See if I need a pre_copy */
+                       if (rinfo.reg != REG_UNSET) {
+                               if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
+                                       do_pre_copy = 1;
+                               }
+                               reg = rinfo.reg;
+                       }
+                       regcm &= rinfo.regcm;
+                       regcm = arch_regcm_normalize(state, regcm);
+                       if (regcm == 0) {
+                               do_pre_copy = 1;
+                       }
+                       /* Always use pre_copies for constants.
+                        * They do not take up any registers until a
+                        * copy places them in one.
+                        */
+                       if ((info.reg == REG_UNNEEDED) && 
+                               (rinfo.reg != REG_UNNEEDED)) {
+                               do_pre_copy = 1;
+                       }
+               }
+               do_post_copy =
+                       !do_pre_copy &&
+                       (((info.reg != REG_UNSET) && 
+                               (reg != REG_UNSET) &&
+                               (info.reg != reg)) ||
+                       ((info.regcm & regcm) == 0));
+
+               reg = info.reg;
+               regcm = info.regcm;
+               /* Walk through the uses of ins and do a pre_copy or see if a post_copy is warranted */
+               for(entry = ins->use; entry; entry = next) {
+                       struct reg_info rinfo;
+                       int i;
+                       next = entry->next;
+                       i = find_rhs_use(state, entry->member, ins);
+                       if (i < 0) {
+                               continue;
+                       }
+                       
+                       /* Find the users color requirements */
+                       rinfo = arch_reg_rhs(state, entry->member, i);
+                       if (rinfo.reg >= MAX_REGISTERS) {
+                               rinfo.reg = REG_UNSET;
+                       }
+
+                       /* Now see if it is time to do the pre_copy */
+                       if (rinfo.reg != REG_UNSET) {
+                               if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
+                                       ((regcm & rinfo.regcm) == 0) ||
+                                       /* Don't let a mandatory coalesce sneak
+                                        * into a operation that is marked to prevent
+                                        * coalescing.
+                                        */
+                                       ((reg != REG_UNNEEDED) &&
+                                       ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
+                                       (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
+                                       ) {
+                                       if (do_pre_copy) {
+                                               struct triple *user;
+                                               user = entry->member;
+                                               if (RHS(user, i) != ins) {
+                                                       internal_error(state, user, "bad rhs");
+                                               }
+                                               tmp = pre_copy(state, user, i);
+                                               tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
+                                               continue;
+                                       } else {
+                                               do_post_copy = 1;
+                                       }
+                               }
+                               reg = rinfo.reg;
+                       }
+                       if ((regcm & rinfo.regcm) == 0) {
+                               if (do_pre_copy) {
+                                       struct triple *user;
+                                       user = entry->member;
+                                       if (RHS(user, i) != ins) {
+                                               internal_error(state, user, "bad rhs");
+                                       }
+                                       tmp = pre_copy(state, user, i);
+                                       tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
+                                       continue;
+                               } else {
+                                       do_post_copy = 1;
+                               }
+                       }
+                       regcm &= rinfo.regcm;
+                       
+               }
+               if (do_post_copy) {
+                       struct reg_info pre, post;
+                       tmp = post_copy(state, ins);
+                       tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
+                       pre = arch_reg_lhs(state, ins, 0);
+                       post = arch_reg_lhs(state, tmp, 0);
+                       if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
+                               internal_error(state, tmp, "useless copy");
+                       }
+               }
+       next:
+               ins = ins->next;
+       } while(ins != first);
+}
+
+
 struct live_range_edge;
+struct live_range_def;
 struct live_range {
        struct live_range_edge *edges;
-       struct triple *def;
+       struct live_range_def *defs;
+/* Note. The list pointed to by defs is kept in order.
+ * That is baring splits in the flow control
+ * defs dominates defs->next wich dominates defs->next->next
+ * etc.
+ */
        unsigned color;
        unsigned classes;
        unsigned degree;
+       unsigned length;
        struct live_range *group_next, **group_prev;
 };
 
@@ -10036,6 +11907,14 @@ struct live_range_edge {
        struct live_range *node;
 };
 
+struct live_range_def {
+       struct live_range_def *next;
+       struct live_range_def *prev;
+       struct live_range *lr;
+       struct triple *def;
+       unsigned orig_id;
+};
+
 #define LRE_HASH_SIZE 2048
 struct lre_hash {
        struct lre_hash *next;
@@ -10047,12 +11926,150 @@ struct lre_hash {
 struct reg_state {
        struct lre_hash *hash[LRE_HASH_SIZE];
        struct reg_block *blocks;
+       struct live_range_def *lrd;
        struct live_range *lr;
        struct live_range *low, **low_tail;
        struct live_range *high, **high_tail;
+       unsigned defs;
        unsigned ranges;
+       int passes, max_passes;
+#define MAX_ALLOCATION_PASSES 100
+};
+
+
+
+struct print_interference_block_info {
+       struct reg_state *rstate;
+       FILE *fp;
+       int need_edges;
 };
+static void print_interference_block(
+       struct compile_state *state, struct block *block, void *arg)
+
+{
+       struct print_interference_block_info *info = arg;
+       struct reg_state *rstate = info->rstate;
+       FILE *fp = info->fp;
+       struct reg_block *rb;
+       struct triple *ptr;
+       int phi_present;
+       int done;
+       rb = &rstate->blocks[block->vertex];
+
+       fprintf(fp, "\nblock: %p (%d), %p<-%p %p<-%p\n", 
+               block, 
+               block->vertex,
+               block->left, 
+               block->left && block->left->use?block->left->use->member : 0,
+               block->right, 
+               block->right && block->right->use?block->right->use->member : 0);
+       if (rb->in) {
+               struct triple_reg_set *in_set;
+               fprintf(fp, "        in:");
+               for(in_set = rb->in; in_set; in_set = in_set->next) {
+                       fprintf(fp, " %-10p", in_set->member);
+               }
+               fprintf(fp, "\n");
+       }
+       phi_present = 0;
+       for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
+               done = (ptr == block->last);
+               if (ptr->op == OP_PHI) {
+                       phi_present = 1;
+                       break;
+               }
+       }
+       if (phi_present) {
+               int edge;
+               for(edge = 0; edge < block->users; edge++) {
+                       fprintf(fp, "     in(%d):", edge);
+                       for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
+                               struct triple **slot;
+                               done = (ptr == block->last);
+                               if (ptr->op != OP_PHI) {
+                                       continue;
+                               }
+                               slot = &RHS(ptr, 0);
+                               fprintf(fp, " %-10p", slot[edge]);
+                       }
+                       fprintf(fp, "\n");
+               }
+       }
+       if (block->first->op == OP_LABEL) {
+               fprintf(fp, "%p:\n", block->first);
+       }
+       for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
+               struct live_range *lr;
+               unsigned id;
+               int op;
+               op = ptr->op;
+               done = (ptr == block->last);
+               lr = rstate->lrd[ptr->id].lr;
+               
+               id = ptr->id;
+               ptr->id = rstate->lrd[id].orig_id;
+               SET_REG(ptr->id, lr->color);
+               display_triple(fp, ptr);
+               ptr->id = id;
+
+               if (triple_is_def(state, ptr) && (lr->defs == 0)) {
+                       internal_error(state, ptr, "lr has no defs!");
+               }
+               if (info->need_edges) {
+                       if (lr->defs) {
+                               struct live_range_def *lrd;
+                               fprintf(fp, "       range:");
+                               lrd = lr->defs;
+                               do {
+                                       fprintf(fp, " %-10p", lrd->def);
+                                       lrd = lrd->next;
+                               } while(lrd != lr->defs);
+                               fprintf(fp, "\n");
+                       }
+                       if (lr->edges > 0) {
+                               struct live_range_edge *edge;
+                               fprintf(fp, "       edges:");
+                               for(edge = lr->edges; edge; edge = edge->next) {
+                                       struct live_range_def *lrd;
+                                       lrd = edge->node->defs;
+                                       do {
+                                               fprintf(fp, " %-10p", lrd->def);
+                                               lrd = lrd->next;
+                                       } while(lrd != edge->node->defs);
+                                       fprintf(fp, "|");
+                               }
+                               fprintf(fp, "\n");
+                       }
+               }
+               /* Do a bunch of sanity checks */
+               valid_ins(state, ptr);
+               if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
+                       internal_error(state, ptr, "Invalid triple id: %d",
+                               ptr->id);
+               }
+       }
+       if (rb->out) {
+               struct triple_reg_set *out_set;
+               fprintf(fp, "       out:");
+               for(out_set = rb->out; out_set; out_set = out_set->next) {
+                       fprintf(fp, " %-10p", out_set->member);
+               }
+               fprintf(fp, "\n");
+       }
+       fprintf(fp, "\n");
+}
+
+static void print_interference_blocks(
+       struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
+{
+       struct print_interference_block_info info;
+       info.rstate = rstate;
+       info.fp = fp;
+       info.need_edges = need_edges;
+       fprintf(fp, "\nlive variables by block\n");
+       walk_blocks(state, print_interference_block, &info);
 
+}
 
 static unsigned regc_max_size(struct compile_state *state, int classes)
 {
@@ -10094,6 +12111,9 @@ static void reg_fill_used(struct compile_state *state, char *used, int reg)
 {
        unsigned equivs[MAX_REG_EQUIVS];
        int i;
+       if (reg == REG_UNNEEDED) {
+               return;
+       }
        arch_reg_equivs(state, equivs, reg);
        for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
                used[equivs[i]] = 1;
@@ -10101,6 +12121,20 @@ static void reg_fill_used(struct compile_state *state, char *used, int reg)
        return;
 }
 
+static void reg_inc_used(struct compile_state *state, char *used, int reg)
+{
+       unsigned equivs[MAX_REG_EQUIVS];
+       int i;
+       if (reg == REG_UNNEEDED) {
+               return;
+       }
+       arch_reg_equivs(state, equivs, reg);
+       for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
+               used[equivs[i]] += 1;
+       }
+       return;
+}
+
 static unsigned int hash_live_edge(
        struct live_range *left, struct live_range *right)
 {
@@ -10138,7 +12172,10 @@ static struct lre_hash **lre_probe(struct reg_state *rstate,
        index = hash_live_edge(left, right);
        
        ptr = &rstate->hash[index];
-       while((*ptr) && ((*ptr)->left != left) && ((*ptr)->right != right)) {
+       while(*ptr) {
+               if (((*ptr)->left == left) && ((*ptr)->right == right)) {
+                       break;
+               }
                ptr = &(*ptr)->next;
        }
        return ptr;
@@ -10176,6 +12213,10 @@ static void add_live_edge(struct reg_state *rstate,
        if (*ptr) {
                return;
        }
+#if 0
+       fprintf(stderr, "new_live_edge(%p, %p)\n",
+               left, right);
+#endif
        new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
        new_hash->next  = *ptr;
        new_hash->left  = left;
@@ -10214,6 +12255,7 @@ static void remove_live_edge(struct reg_state *rstate,
                        *ptr = edge->next;
                        memset(edge, 0, sizeof(*edge));
                        xfree(edge);
+                       right->degree--;
                        break;
                }
        }
@@ -10223,6 +12265,7 @@ static void remove_live_edge(struct reg_state *rstate,
                        *ptr = edge->next;
                        memset(edge, 0, sizeof(*edge));
                        xfree(edge);
+                       left->degree--;
                        break;
                }
        }
@@ -10237,6 +12280,19 @@ static void remove_live_edges(struct reg_state *rstate, struct live_range *range
        }
 }
 
+static void transfer_live_edges(struct reg_state *rstate, 
+       struct live_range *dest, struct live_range *src)
+{
+       struct live_range_edge *edge, *next;
+       for(edge = src->edges; edge; edge = next) {
+               struct live_range *other;
+               next = edge->next;
+               other = edge->node;
+               remove_live_edge(rstate, src, other);
+               add_live_edge(rstate, dest, other);
+       }
+}
+
 
 /* Interference graph...
  * 
@@ -10276,107 +12332,302 @@ static void different_colored(
 {
        struct live_range *lr;
        struct triple **expr;
-       lr = &rstate->lr[ins->id];
+       lr = rstate->lrd[ins->id].lr;
        expr = triple_rhs(state, ins, 0);
        for(;expr; expr = triple_rhs(state, ins, expr)) {
                struct live_range *lr2;
-               if ((*expr == parent) || (*expr == ins)) {
+               if (!*expr || (*expr == parent) || (*expr == ins)) {
                        continue;
                }
-               lr2 = &rstate->lr[(*expr)->id];
+               lr2 = rstate->lrd[(*expr)->id].lr;
                if (lr->color == lr2->color) {
                        internal_error(state, ins, "live range too big");
                }
        }
 }
 
+
+static struct live_range *coalesce_ranges(
+       struct compile_state *state, struct reg_state *rstate,
+       struct live_range *lr1, struct live_range *lr2)
+{
+       struct live_range_def *head, *mid1, *mid2, *end, *lrd;
+       unsigned color;
+       unsigned classes;
+       if (lr1 == lr2) {
+               return lr1;
+       }
+       if (!lr1->defs || !lr2->defs) {
+               internal_error(state, 0,
+                       "cannot coalese dead live ranges");
+       }
+       if ((lr1->color == REG_UNNEEDED) ||
+               (lr2->color == REG_UNNEEDED)) {
+               internal_error(state, 0, 
+                       "cannot coalesce live ranges without a possible color");
+       }
+       if ((lr1->color != lr2->color) &&
+               (lr1->color != REG_UNSET) &&
+               (lr2->color != REG_UNSET)) {
+               internal_error(state, lr1->defs->def, 
+                       "cannot coalesce live ranges of different colors");
+       }
+       color = lr1->color;
+       if (color == REG_UNSET) {
+               color = lr2->color;
+       }
+       classes = lr1->classes & lr2->classes;
+       if (!classes) {
+               internal_error(state, lr1->defs->def,
+                       "cannot coalesce live ranges with dissimilar register classes");
+       }
+#if DEBUG_COALESCING
+       fprintf(stderr, "coalescing:");
+       lrd = lr1->defs;
+       do {
+               fprintf(stderr, " %p", lrd->def);
+               lrd = lrd->next;
+       } while(lrd != lr1->defs);
+       fprintf(stderr, " |");
+       lrd = lr2->defs;
+       do {
+               fprintf(stderr, " %p", lrd->def);
+               lrd = lrd->next;
+       } while(lrd != lr2->defs);
+       fprintf(stderr, "\n");
+#endif
+       /* If there is a clear dominate live range put it in lr1,
+        * For purposes of this test phi functions are
+        * considered dominated by the definitions that feed into
+        * them. 
+        */
+       if ((lr1->defs->prev->def->op == OP_PHI) ||
+               ((lr2->defs->prev->def->op != OP_PHI) &&
+               tdominates(state, lr2->defs->def, lr1->defs->def))) {
+               struct live_range *tmp;
+               tmp = lr1;
+               lr1 = lr2;
+               lr2 = tmp;
+       }
+#if 0
+       if (lr1->defs->orig_id  & TRIPLE_FLAG_POST_SPLIT) {
+               fprintf(stderr, "lr1 post\n");
+       }
+       if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
+               fprintf(stderr, "lr1 pre\n");
+       }
+       if (lr2->defs->orig_id  & TRIPLE_FLAG_POST_SPLIT) {
+               fprintf(stderr, "lr2 post\n");
+       }
+       if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
+               fprintf(stderr, "lr2 pre\n");
+       }
+#endif
+#if 0
+       fprintf(stderr, "coalesce color1(%p): %3d color2(%p) %3d\n",
+               lr1->defs->def,
+               lr1->color,
+               lr2->defs->def,
+               lr2->color);
+#endif
+       
+       /* Append lr2 onto lr1 */
+#warning "FIXME should this be a merge instead of a splice?"
+       /* This FIXME item applies to the correctness of live_range_end 
+        * and to the necessity of making multiple passes of coalesce_live_ranges.
+        * A failure to find some coalesce opportunities in coaleace_live_ranges
+        * does not impact the correct of the compiler just the efficiency with
+        * which registers are allocated.
+        */
+       head = lr1->defs;
+       mid1 = lr1->defs->prev;
+       mid2 = lr2->defs;
+       end  = lr2->defs->prev;
+       
+       head->prev = end;
+       end->next  = head;
+
+       mid1->next = mid2;
+       mid2->prev = mid1;
+
+       /* Fixup the live range in the added live range defs */
+       lrd = head;
+       do {
+               lrd->lr = lr1;
+               lrd = lrd->next;
+       } while(lrd != head);
+
+       /* Mark lr2 as free. */
+       lr2->defs = 0;
+       lr2->color = REG_UNNEEDED;
+       lr2->classes = 0;
+
+       if (!lr1->defs) {
+               internal_error(state, 0, "lr1->defs == 0 ?");
+       }
+
+       lr1->color   = color;
+       lr1->classes = classes;
+
+       /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
+       transfer_live_edges(rstate, lr1, lr2);
+
+       return lr1;
+}
+
+static struct live_range_def *live_range_head(
+       struct compile_state *state, struct live_range *lr,
+       struct live_range_def *last)
+{
+       struct live_range_def *result;
+       result = 0;
+       if (last == 0) {
+               result = lr->defs;
+       }
+       else if (!tdominates(state, lr->defs->def, last->next->def)) {
+               result = last->next;
+       }
+       return result;
+}
+
+static struct live_range_def *live_range_end(
+       struct compile_state *state, struct live_range *lr,
+       struct live_range_def *last)
+{
+       struct live_range_def *result;
+       result = 0;
+       if (last == 0) {
+               result = lr->defs->prev;
+       }
+       else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
+               result = last->prev;
+       }
+       return result;
+}
+
+
 static void initialize_live_ranges(
        struct compile_state *state, struct reg_state *rstate)
 {
        struct triple *ins, *first;
-       size_t size;
-       int i;
+       size_t count, size;
+       int i, j;
 
-       first = state->main_function->left;
-       /* First count how many live ranges I will need.
+       first = RHS(state->main_function, 0);
+       /* First count how many instructions I have.
         */
-       rstate->ranges = count_triples(state);
-       size = sizeof(rstate->lr[0]) * (rstate->ranges + 1);
-       rstate->lr = xcmalloc(size, "live_range");
+       count = count_triples(state);
+       /* Potentially I need one live range definitions for each
+        * instruction.
+        */
+       rstate->defs = count;
+       /* Potentially I need one live range for each instruction
+        * plus an extra for the dummy live range.
+        */
+       rstate->ranges = count + 1;
+       size = sizeof(rstate->lrd[0]) * rstate->defs;
+       rstate->lrd = xcmalloc(size, "live_range_def");
+       size = sizeof(rstate->lr[0]) * rstate->ranges;
+       rstate->lr  = xcmalloc(size, "live_range");
+
        /* Setup the dummy live range */
        rstate->lr[0].classes = 0;
        rstate->lr[0].color = REG_UNSET;
-       rstate->lr[0].def = 0;
-       i = 0;
+       rstate->lr[0].defs = 0;
+       i = j = 0;
        ins = first;
        do {
-               unsigned color, classes;
-               /* Find the architecture specific color information */
-               color = ID_REG(ins->id);
-               classes = ID_REG_CLASSES(ins->id);
-               if ((color != REG_UNSET) && (color < MAX_REGISTERS)) {
-                       classes = arch_reg_regcm(state, color);
-               }
-
-               /* If the triple is a variable definition give it a live range. */
-               if (triple_is_def(ins)) {
+               /* If the triple is a variable give it a live range */
+               if (triple_is_def(state, ins)) {
+                       struct reg_info info;
+                       /* Find the architecture specific color information */
+                       info = find_def_color(state, ins);
                        i++;
-                       ins->id = i;
-                       rstate->lr[i].def     = ins;
-                       rstate->lr[i].color   = color;
-                       rstate->lr[i].classes = classes;
+                       rstate->lr[i].defs    = &rstate->lrd[j];
+                       rstate->lr[i].color   = info.reg;
+                       rstate->lr[i].classes = info.regcm;
                        rstate->lr[i].degree  = 0;
-                       if (!classes) {
-                               internal_error(state, ins, 
-                                       "live range without a class");
-                       }
-               }
+                       rstate->lrd[j].lr = &rstate->lr[i];
+               } 
                /* Otherwise give the triple the dummy live range. */
                else {
-                       ins->id = 0;
+                       rstate->lrd[j].lr = &rstate->lr[0];
                }
+
+               /* Initalize the live_range_def */
+               rstate->lrd[j].next    = &rstate->lrd[j];
+               rstate->lrd[j].prev    = &rstate->lrd[j];
+               rstate->lrd[j].def     = ins;
+               rstate->lrd[j].orig_id = ins->id;
+               ins->id = j;
+
+               j++;
                ins = ins->next;
        } while(ins != first);
        rstate->ranges = i;
+
        /* Make a second pass to handle achitecture specific register
         * constraints.
         */
        ins = first;
        do {
-               struct live_range *lr;
-               lr = &rstate->lr[ins->id];
-               if (lr->color != REG_UNSET) {
-                       struct triple **expr;
-                       /* This assumes the virtual register is only
-                        * used by one input operation.
-                        */
-                       expr = triple_rhs(state, ins, 0);
-                       for(;expr; expr = triple_rhs(state, ins, expr)) {
-                               struct live_range *lr2;
-                               if (ins == *expr) {
+               int zlhs, zrhs, i, j;
+               if (ins->id > rstate->defs) {
+                       internal_error(state, ins, "bad id");
+               }
+               
+               /* Walk through the template of ins and coalesce live ranges */
+               zlhs = TRIPLE_LHS(ins->sizes);
+               if ((zlhs == 0) && triple_is_def(state, ins)) {
+                       zlhs = 1;
+               }
+               zrhs = TRIPLE_RHS(ins->sizes);
+
+#if DEBUG_COALESCING > 1
+               fprintf(stderr, "mandatory coalesce: %p %d %d\n",
+                       ins, zlhs, zrhs);
+#endif         
+               for(i = 0; i < zlhs; i++) {
+                       struct reg_info linfo;
+                       struct live_range_def *lhs;
+                       linfo = arch_reg_lhs(state, ins, i);
+                       if (linfo.reg < MAX_REGISTERS) {
+                               continue;
+                       }
+                       if (triple_is_def(state, ins)) {
+                               lhs = &rstate->lrd[ins->id];
+                       } else {
+                               lhs = &rstate->lrd[LHS(ins, i)->id];
+                       }
+#if DEBUG_COALESCING > 1
+                       fprintf(stderr, "coalesce lhs(%d): %p %d\n",
+                               i, lhs, linfo.reg);
+               
+#endif         
+                       for(j = 0; j < zrhs; j++) {
+                               struct reg_info rinfo;
+                               struct live_range_def *rhs;
+                               rinfo = arch_reg_rhs(state, ins, j);
+                               if (rinfo.reg < MAX_REGISTERS) {
                                        continue;
                                }
-                               lr2 = &rstate->lr[(*expr)->id];
-                               if (lr->color == lr2->color) {
-                                       different_colored(state, rstate, 
-                                               ins, *expr);
-                                       (*expr)->id = ins->id;
-                                       
+                               rhs = &rstate->lrd[RHS(ins, j)->id];
+#if DEBUG_COALESCING > 1
+                               fprintf(stderr, "coalesce rhs(%d): %p %d\n",
+                                       j, rhs, rinfo.reg);
+               
+#endif         
+                               if (rinfo.reg == linfo.reg) {
+                                       coalesce_ranges(state, rstate, 
+                                               lhs->lr, rhs->lr);
                                }
                        }
                }
                ins = ins->next;
        } while(ins != first);
-
-       /* Make a third pass and forget the virtual registers */
-       for(i = 1; i <= rstate->ranges; i++) {
-               if (rstate->lr[i].color >= MAX_REGISTERS) {
-                       rstate->lr[i].color = REG_UNSET;
-               }
-       }
 }
 
-static struct triple *graph_ins(
+static void graph_ins(
        struct compile_state *state, 
        struct reg_block *blocks, struct triple_reg_set *live, 
        struct reg_block *rb, struct triple *ins, void *arg)
@@ -10385,14 +12636,14 @@ static struct triple *graph_ins(
        struct live_range *def;
        struct triple_reg_set *entry;
 
-       /* If the triple does not start a live range
+       /* If the triple is not a definition
         * we do not have a definition to add to
         * the interference graph.
         */
-       if (ins->id <= 0) {
-               return ins;
+       if (!triple_is_def(state, ins)) {
+               return;
        }
-       def = &rstate->lr[ins->id];
+       def = rstate->lrd[ins->id].lr;
        
        /* Create an edge between ins and everything that is
         * alive, unless the live_range cannot share
@@ -10400,37 +12651,126 @@ static struct triple *graph_ins(
         */
        for(entry = live; entry; entry = entry->next) {
                struct live_range *lr;
-               lr= &rstate->lr[entry->member->id];
+               if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
+                       internal_error(state, 0, "bad entry?");
+               }
+               lr = rstate->lrd[entry->member->id].lr;
+               if (def == lr) {
+                       continue;
+               }
                if (!arch_regcm_intersect(def->classes, lr->classes)) {
                        continue;
                }
                add_live_edge(rstate, def, lr);
        }
-       return ins;
+       return;
 }
 
+static struct live_range *get_verify_live_range(
+       struct compile_state *state, struct reg_state *rstate, struct triple *ins)
+{
+       struct live_range *lr;
+       struct live_range_def *lrd;
+       int ins_found;
+       if ((ins->id < 0) || (ins->id > rstate->defs)) {
+               internal_error(state, ins, "bad ins?");
+       }
+       lr = rstate->lrd[ins->id].lr;
+       ins_found = 0;
+       lrd = lr->defs;
+       do {
+               if (lrd->def == ins) {
+                       ins_found = 1;
+               }
+               lrd = lrd->next;
+       } while(lrd != lr->defs);
+       if (!ins_found) {
+               internal_error(state, ins, "ins not in live range");
+       }
+       return lr;
+}
 
-static struct triple *print_interference_ins(
+static void verify_graph_ins(
        struct compile_state *state, 
        struct reg_block *blocks, struct triple_reg_set *live, 
        struct reg_block *rb, struct triple *ins, void *arg)
 {
        struct reg_state *rstate = arg;
-       struct live_range *lr;
-
-       lr = &rstate->lr[ins->id];
-       if ((ins->op == OP_LABEL) && (ins->use)) {
-               printf("\n%p:\n", ins);
-       }
-       else if (ins->op == OP_INTCONST) {
-               printf("(%p) %-7s %08lx              @ %s:%d.%d\n",
-                       ins, tops(ins->op), ins->u.cval,
-                       ins->filename, ins->line, ins->col);
+       struct triple_reg_set *entry1, *entry2;
+
+
+       /* Compare live against edges and make certain the code is working */
+       for(entry1 = live; entry1; entry1 = entry1->next) {
+               struct live_range *lr1;
+               lr1 = get_verify_live_range(state, rstate, entry1->member);
+               for(entry2 = live; entry2; entry2 = entry2->next) {
+                       struct live_range *lr2;
+                       struct live_range_edge *edge2;
+                       int lr1_found;
+                       int lr2_degree;
+                       if (entry2 == entry1) {
+                               continue;
+                       }
+                       lr2 = get_verify_live_range(state, rstate, entry2->member);
+                       if (lr1 == lr2) {
+                               internal_error(state, entry2->member, 
+                                       "live range with 2 values simultaneously alive");
+                       }
+                       if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
+                               continue;
+                       }
+                       if (!interfere(rstate, lr1, lr2)) {
+                               internal_error(state, entry2->member, 
+                                       "edges don't interfere?");
+                       }
+                               
+                       lr1_found = 0;
+                       lr2_degree = 0;
+                       for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
+                               lr2_degree++;
+                               if (edge2->node == lr1) {
+                                       lr1_found = 1;
+                               }
+                       }
+                       if (lr2_degree != lr2->degree) {
+                               internal_error(state, entry2->member,
+                                       "computed degree: %d does not match reported degree: %d\n",
+                                       lr2_degree, lr2->degree);
+                       }
+                       if (!lr1_found) {
+                               internal_error(state, entry2->member, "missing edge");
+                       }
+               }
        }
-       else {
-               printf("(%p) %-7s %-10p %-10p @ %s:%d.%d\n",
-                       ins, tops(ins->op), ins->left, ins->right,
-                       ins->filename, ins->line, ins->col);
+       return;
+}
+
+
+static void print_interference_ins(
+       struct compile_state *state, 
+       struct reg_block *blocks, struct triple_reg_set *live, 
+       struct reg_block *rb, struct triple *ins, void *arg)
+{
+       struct reg_state *rstate = arg;
+       struct live_range *lr;
+       unsigned id;
+
+       lr = rstate->lrd[ins->id].lr;
+       id = ins->id;
+       ins->id = rstate->lrd[id].orig_id;
+       SET_REG(ins->id, lr->color);
+       display_triple(stdout, ins);
+       ins->id = id;
+
+       if (lr->defs) {
+               struct live_range_def *lrd;
+               printf("       range:");
+               lrd = lr->defs;
+               do {
+                       printf(" %-10p", lrd->def);
+                       lrd = lrd->next;
+               } while(lrd != lr->defs);
+               printf("\n");
        }
        if (live) {
                struct triple_reg_set *entry;
@@ -10442,139 +12782,643 @@ static struct triple *print_interference_ins(
        }
        if (lr->edges) {
                struct live_range_edge *entry;
-               printf("        edges:");
+               printf("       edges:");
                for(entry = lr->edges; entry; entry = entry->next) {
-                       printf(" %-10p", entry->node->def);
+                       struct live_range_def *lrd;
+                       lrd = entry->node->defs;
+                       do {
+                               printf(" %-10p", lrd->def);
+                               lrd = lrd->next;
+                       } while(lrd != entry->node->defs);
+                       printf("|");
                }
                printf("\n");
        }
-       if (triple_is_branch(ins)) {
+       if (triple_is_branch(state, ins)) {
                printf("\n");
        }
-       return ins;
+       return;
 }
 
-#if DEBUG_COLOR_GRAPH > 1
-#define cgdebug_printf(...) fprintf(stdout, __VA_ARGS__)
-#define cgdebug_flush() fflush(stdout)
-#elif DEBUG_COLOR_GRAPH == 1
-#define cgdebug_printf(...) fprintf(stderr, __VA_ARGS__)
-#define cgdebug_flush() fflush(stderr)
-#else
-#define cgdebug_printf(...)
-#define cgdebug_flush()
-#endif
-
-static void select_free_color(struct compile_state *state, 
-       struct reg_state *rstate, struct live_range *range)
+static int coalesce_live_ranges(
+       struct compile_state *state, struct reg_state *rstate)
 {
-       struct triple_set *entry;
-       struct live_range *phi;
-       struct live_range_edge *edge;
-       char used[MAX_REGISTERS];
-       struct triple **expr;
-
-       /* If a color is already assigned don't change it */
-       if (range->color != REG_UNSET) {
-               return;
-       }
-       /* Instead of doing just the trivial color select here I try
-        * a few extra things because a good color selection will help reduce
-        * copies.
+       /* At the point where a value is moved from one
+        * register to another that value requires two
+        * registers, thus increasing register pressure.
+        * Live range coaleescing reduces the register
+        * pressure by keeping a value in one register
+        * longer.
+        *
+        * In the case of a phi function all paths leading
+        * into it must be allocated to the same register
+        * otherwise the phi function may not be removed.
+        *
+        * Forcing a value to stay in a single register
+        * for an extended period of time does have
+        * limitations when applied to non homogenous
+        * register pool.  
+        *
+        * The two cases I have identified are:
+        * 1) Two forced register assignments may
+        *    collide.
+        * 2) Registers may go unused because they
+        *    are only good for storing the value
+        *    and not manipulating it.
+        *
+        * Because of this I need to split live ranges,
+        * even outside of the context of coalesced live
+        * ranges.  The need to split live ranges does
+        * impose some constraints on live range coalescing.
+        *
+        * - Live ranges may not be coalesced across phi
+        *   functions.  This creates a 2 headed live
+        *   range that cannot be sanely split.
+        *
+        * - phi functions (coalesced in initialize_live_ranges) 
+        *   are handled as pre split live ranges so we will
+        *   never attempt to split them.
         */
+       int coalesced;
+       int i;
 
-       /* Find the registers currently in use */
-       memset(used, 0, sizeof(used));
-       for(edge = range->edges; edge; edge = edge->next) {
-               if (edge->node->color == REG_UNSET) {
+       coalesced = 0;
+       for(i = 0; i <= rstate->ranges; i++) {
+               struct live_range *lr1;
+               struct live_range_def *lrd1;
+               lr1 = &rstate->lr[i];
+               if (!lr1->defs) {
                        continue;
                }
-               reg_fill_used(state, used, edge->node->color);
-       }
-#if DEBUG_COLOR_GRAPH > 1
-       {
-               int i;
-               i = 0;
-               for(edge = range->edges; edge; edge = edge->next) {
-                       i++;
-               }
-               cgdebug_printf("\n%s edges: %d @%s:%d.%d\n", 
-                       tops(range->def->op), i, 
-                       range->def->filename, range->def->line, range->def->col);
-               for(i = 0; i < MAX_REGISTERS; i++) {
-                       if (used[i]) {
-                               cgdebug_printf("used: %s\n",
-                                       arch_reg_str(i));
+               lrd1 = live_range_end(state, lr1, 0);
+               for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
+                       struct triple_set *set;
+                       if (lrd1->def->op != OP_COPY) {
+                               continue;
                        }
-               }
-       }       
-#endif
-
-       /* If I feed into an expression reuse it's color.
-        * This should help remove copies in the case of 2 register instructions
-        * and phi functions.
-        */
-       phi = 0;
-       entry = range->def->use;
-       for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
-               struct live_range *lr;
-               lr = &rstate->lr[entry->member->id];
-               if (entry->member->id == 0) {
-                       continue;
-               }
-               if (!phi && (lr->def->op == OP_PHI) && 
-                       !interfere(rstate, range, lr)) {
-                       phi = lr;
-               }
-               if ((lr->color == REG_UNSET) ||
-                       ((lr->classes & range->classes) == 0) ||
-                       (used[lr->color])) {
-                       continue;
-               }
-               if (interfere(rstate, range, lr)) {
-                       continue;
-               }
-               range->color = lr->color;
-       }
-       /* If I feed into a phi function reuse it's color of the color
-        * of something else that feeds into the phi function.
-        */
-       if (phi) {
-               if (phi->color != REG_UNSET) {
-                       if (used[phi->color]) {
-                               range->color = phi->color;
+                       /* Skip copies that are the result of a live range split. */
+                       if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
+                               continue;
                        }
-               }
-               else {
-                       expr = triple_rhs(state, phi->def, 0);
-                       for(; expr; expr = triple_rhs(state, phi->def, expr)) {
-                               struct live_range *lr;
-                               lr = &rstate->lr[(*expr)->id];
-                               if ((lr->color == REG_UNSET) || 
-                                       ((lr->classes & range->classes) == 0) ||
-                                       (used[lr->color])) {
+                       for(set = lrd1->def->use; set; set = set->next) {
+                               struct live_range_def *lrd2;
+                               struct live_range *lr2, *res;
+
+                               lrd2 = &rstate->lrd[set->member->id];
+
+                               /* Don't coalesce with instructions
+                                * that are the result of a live range
+                                * split.
+                                */
+                               if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
                                        continue;
                                }
-                               if (interfere(rstate, range, lr)) {
+                               lr2 = rstate->lrd[set->member->id].lr;
+                               if (lr1 == lr2) {
                                        continue;
                                }
-                               range->color = lr->color;
+                               if ((lr1->color != lr2->color) &&
+                                       (lr1->color != REG_UNSET) &&
+                                       (lr2->color != REG_UNSET)) {
+                                       continue;
+                               }
+                               if ((lr1->classes & lr2->classes) == 0) {
+                                       continue;
+                               }
+                               
+                               if (interfere(rstate, lr1, lr2)) {
+                                       continue;
+                               }
+
+                               res = coalesce_ranges(state, rstate, lr1, lr2);
+                               coalesced += 1;
+                               if (res != lr1) {
+                                       goto next;
+                               }
                        }
                }
+       next:
+               ;
        }
-       /* If I don't interfere with a rhs node reuse it's color */
-       if (range->color == REG_UNSET) {
-               expr = triple_rhs(state, range->def, 0);
-               for(; expr; expr = triple_rhs(state, range->def, expr)) {
-                       struct live_range *lr;
-                       lr = &rstate->lr[(*expr)->id];
-                       if ((lr->color == -1) || 
-                               ((lr->classes & range->classes) == 0) ||
-                               (used[lr->color])) {
+       return coalesced;
+}
+
+
+static void fix_coalesce_conflicts(struct compile_state *state,
+       struct reg_block *blocks, struct triple_reg_set *live,
+       struct reg_block *rb, struct triple *ins, void *arg)
+{
+       int *conflicts = arg;
+       int zlhs, zrhs, i, j;
+
+       /* See if we have a mandatory coalesce operation between
+        * a lhs and a rhs value.  If so and the rhs value is also
+        * alive then this triple needs to be pre copied.  Otherwise
+        * we would have two definitions in the same live range simultaneously
+        * alive.
+        */
+       zlhs = TRIPLE_LHS(ins->sizes);
+       if ((zlhs == 0) && triple_is_def(state, ins)) {
+               zlhs = 1;
+       }
+       zrhs = TRIPLE_RHS(ins->sizes);
+       for(i = 0; i < zlhs; i++) {
+               struct reg_info linfo;
+               linfo = arch_reg_lhs(state, ins, i);
+               if (linfo.reg < MAX_REGISTERS) {
+                       continue;
+               }
+               for(j = 0; j < zrhs; j++) {
+                       struct reg_info rinfo;
+                       struct triple *rhs;
+                       struct triple_reg_set *set;
+                       int found;
+                       found = 0;
+                       rinfo = arch_reg_rhs(state, ins, j);
+                       if (rinfo.reg != linfo.reg) {
                                continue;
                        }
-                       if (interfere(rstate, range, lr)) {
-                               continue;
+                       rhs = RHS(ins, j);
+                       for(set = live; set && !found; set = set->next) {
+                               if (set->member == rhs) {
+                                       found = 1;
+                               }
+                       }
+                       if (found) {
+                               struct triple *copy;
+                               copy = pre_copy(state, ins, j);
+                               copy->id |= TRIPLE_FLAG_PRE_SPLIT;
+                               (*conflicts)++;
+                       }
+               }
+       }
+       return;
+}
+
+static int correct_coalesce_conflicts(
+       struct compile_state *state, struct reg_block *blocks)
+{
+       int conflicts;
+       conflicts = 0;
+       walk_variable_lifetimes(state, blocks, fix_coalesce_conflicts, &conflicts);
+       return conflicts;
+}
+
+static void replace_set_use(struct compile_state *state,
+       struct triple_reg_set *head, struct triple *orig, struct triple *new)
+{
+       struct triple_reg_set *set;
+       for(set = head; set; set = set->next) {
+               if (set->member == orig) {
+                       set->member = new;
+               }
+       }
+}
+
+static void replace_block_use(struct compile_state *state, 
+       struct reg_block *blocks, struct triple *orig, struct triple *new)
+{
+       int i;
+#warning "WISHLIST visit just those blocks that need it *"
+       for(i = 1; i <= state->last_vertex; i++) {
+               struct reg_block *rb;
+               rb = &blocks[i];
+               replace_set_use(state, rb->in, orig, new);
+               replace_set_use(state, rb->out, orig, new);
+       }
+}
+
+static void color_instructions(struct compile_state *state)
+{
+       struct triple *ins, *first;
+       first = RHS(state->main_function, 0);
+       ins = first;
+       do {
+               if (triple_is_def(state, ins)) {
+                       struct reg_info info;
+                       info = find_lhs_color(state, ins, 0);
+                       if (info.reg >= MAX_REGISTERS) {
+                               info.reg = REG_UNSET;
+                       }
+                       SET_INFO(ins->id, info);
+               }
+               ins = ins->next;
+       } while(ins != first);
+}
+
+static struct reg_info read_lhs_color(
+       struct compile_state *state, struct triple *ins, int index)
+{
+       struct reg_info info;
+       if ((index == 0) && triple_is_def(state, ins)) {
+               info.reg   = ID_REG(ins->id);
+               info.regcm = ID_REGCM(ins->id);
+       }
+       else if (index < TRIPLE_LHS(ins->sizes)) {
+               info = read_lhs_color(state, LHS(ins, index), 0);
+       }
+       else {
+               internal_error(state, ins, "Bad lhs %d", index);
+               info.reg = REG_UNSET;
+               info.regcm = 0;
+       }
+       return info;
+}
+
+static struct triple *resolve_tangle(
+       struct compile_state *state, struct triple *tangle)
+{
+       struct reg_info info, uinfo;
+       struct triple_set *set, *next;
+       struct triple *copy;
+
+#warning "WISHLIST recalculate all affected instructions colors"
+       info = find_lhs_color(state, tangle, 0);
+       for(set = tangle->use; set; set = next) {
+               struct triple *user;
+               int i, zrhs;
+               next = set->next;
+               user = set->member;
+               zrhs = TRIPLE_RHS(user->sizes);
+               for(i = 0; i < zrhs; i++) {
+                       if (RHS(user, i) != tangle) {
+                               continue;
+                       }
+                       uinfo = find_rhs_post_color(state, user, i);
+                       if (uinfo.reg == info.reg) {
+                               copy = pre_copy(state, user, i);
+                               copy->id |= TRIPLE_FLAG_PRE_SPLIT;
+                               SET_INFO(copy->id, uinfo);
+                       }
+               }
+       }
+       copy = 0;
+       uinfo = find_lhs_pre_color(state, tangle, 0);
+       if (uinfo.reg == info.reg) {
+               struct reg_info linfo;
+               copy = post_copy(state, tangle);
+               copy->id |= TRIPLE_FLAG_PRE_SPLIT;
+               linfo = find_lhs_color(state, copy, 0);
+               SET_INFO(copy->id, linfo);
+       }
+       info = find_lhs_color(state, tangle, 0);
+       SET_INFO(tangle->id, info);
+       
+       return copy;
+}
+
+
+static void fix_tangles(struct compile_state *state,
+       struct reg_block *blocks, struct triple_reg_set *live,
+       struct reg_block *rb, struct triple *ins, void *arg)
+{
+       int *tangles = arg;
+       struct triple *tangle;
+       do {
+               char used[MAX_REGISTERS];
+               struct triple_reg_set *set;
+               tangle = 0;
+
+               /* Find out which registers have multiple uses at this point */
+               memset(used, 0, sizeof(used));
+               for(set = live; set; set = set->next) {
+                       struct reg_info info;
+                       info = read_lhs_color(state, set->member, 0);
+                       if (info.reg == REG_UNSET) {
+                               continue;
+                       }
+                       reg_inc_used(state, used, info.reg);
+               }
+               
+               /* Now find the least dominated definition of a register in
+                * conflict I have seen so far.
+                */
+               for(set = live; set; set = set->next) {
+                       struct reg_info info;
+                       info = read_lhs_color(state, set->member, 0);
+                       if (used[info.reg] < 2) {
+                               continue;
+                       }
+                       /* Changing copies that feed into phi functions
+                        * is incorrect.
+                        */
+                       if (set->member->use && 
+                               (set->member->use->member->op == OP_PHI)) {
+                               continue;
+                       }
+                       if (!tangle || tdominates(state, set->member, tangle)) {
+                               tangle = set->member;
+                       }
+               }
+               /* If I have found a tangle resolve it */
+               if (tangle) {
+                       struct triple *post_copy;
+                       (*tangles)++;
+                       post_copy = resolve_tangle(state, tangle);
+                       if (post_copy) {
+                               replace_block_use(state, blocks, tangle, post_copy);
+                       }
+                       if (post_copy && (tangle != ins)) {
+                               replace_set_use(state, live, tangle, post_copy);
+                       }
+               }
+       } while(tangle);
+       return;
+}
+
+static int correct_tangles(
+       struct compile_state *state, struct reg_block *blocks)
+{
+       int tangles;
+       tangles = 0;
+       color_instructions(state);
+       walk_variable_lifetimes(state, blocks, fix_tangles, &tangles);
+       return tangles;
+}
+
+
+static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
+static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
+
+struct triple *find_constrained_def(
+       struct compile_state *state, struct live_range *range, struct triple *constrained)
+{
+       struct live_range_def *lrd;
+       lrd = range->defs;
+       do {
+               struct reg_info info;
+               unsigned regcm;
+               int is_constrained;
+               regcm = arch_type_to_regcm(state, lrd->def->type);
+               info = find_lhs_color(state, lrd->def, 0);
+               regcm      = arch_regcm_reg_normalize(state, regcm);
+               info.regcm = arch_regcm_reg_normalize(state, info.regcm);
+               /* If the 2 register class masks are not equal the
+                * the current register class is constrained.
+                */
+               is_constrained = regcm != info.regcm;
+               
+               /* Of the constrained live ranges deal with the
+                * least dominated one first.
+                */
+               if (is_constrained) {
+#if DEBUG_RANGE_CONFLICTS
+                       fprintf(stderr, "canidate: %p %-8s regcm: %x %x\n",
+                               lrd->def, tops(lrd->def->op), regcm, info.regcm);
+#endif
+                       if (!constrained || 
+                               tdominates(state, lrd->def, constrained))
+                       {
+                               constrained = lrd->def;
+                       }
+               }
+               lrd = lrd->next;
+       } while(lrd != range->defs);
+       return constrained;
+}
+
+static int split_constrained_ranges(
+       struct compile_state *state, struct reg_state *rstate, 
+       struct live_range *range)
+{
+       /* Walk through the edges in conflict and our current live
+        * range, and find definitions that are more severly constrained
+        * than they type of data they contain require.
+        * 
+        * Then pick one of those ranges and relax the constraints.
+        */
+       struct live_range_edge *edge;
+       struct triple *constrained;
+
+       constrained = 0;
+       for(edge = range->edges; edge; edge = edge->next) {
+               constrained = find_constrained_def(state, edge->node, constrained);
+       }
+       if (!constrained) {
+               constrained = find_constrained_def(state, range, constrained);
+       }
+#if DEBUG_RANGE_CONFLICTS
+       fprintf(stderr, "constrained: %p %-8s\n",
+               constrained, tops(constrained->op));
+#endif
+       if (constrained) {
+               ids_from_rstate(state, rstate);
+               cleanup_rstate(state, rstate);
+               resolve_tangle(state, constrained);
+       }
+       return !!constrained;
+}
+       
+static int split_ranges(
+       struct compile_state *state, struct reg_state *rstate,
+       char *used, struct live_range *range)
+{
+       int split;
+#if DEBUG_RANGE_CONFLICTS
+       fprintf(stderr, "split_ranges %d %s %p\n", 
+               rstate->passes, tops(range->defs->def->op), range->defs->def);
+#endif
+       if ((range->color == REG_UNNEEDED) ||
+               (rstate->passes >= rstate->max_passes)) {
+               return 0;
+       }
+       split = split_constrained_ranges(state, rstate, range);
+
+       /* Ideally I would split the live range that will not be used
+        * for the longest period of time in hopes that this will 
+        * (a) allow me to spill a register or
+        * (b) allow me to place a value in another register.
+        *
+        * So far I don't have a test case for this, the resolving
+        * of mandatory constraints has solved all of my
+        * know issues.  So I have choosen not to write any
+        * code until I cat get a better feel for cases where
+        * it would be useful to have.
+        *
+        */
+#warning "WISHLIST implement live range splitting..."
+       if ((DEBUG_RANGE_CONFLICTS > 1) && 
+               (!split || (DEBUG_RANGE_CONFLICTS > 2))) {
+               print_interference_blocks(state, rstate, stderr, 0);
+               print_dominators(state, stderr);
+       }
+       return split;
+}
+
+#if DEBUG_COLOR_GRAPH > 1
+#define cgdebug_printf(...) fprintf(stdout, __VA_ARGS__)
+#define cgdebug_flush() fflush(stdout)
+#define cgdebug_loc(STATE, TRIPLE) loc(stdout, STATE, TRIPLE)
+#elif DEBUG_COLOR_GRAPH == 1
+#define cgdebug_printf(...) fprintf(stderr, __VA_ARGS__)
+#define cgdebug_flush() fflush(stderr)
+#define cgdebug_loc(STATE, TRIPLE) loc(stderr, STATE, TRIPLE)
+#else
+#define cgdebug_printf(...)
+#define cgdebug_flush()
+#define cgdebug_loc(STATE, TRIPLE)
+#endif
+
+       
+static int select_free_color(struct compile_state *state, 
+       struct reg_state *rstate, struct live_range *range)
+{
+       struct triple_set *entry;
+       struct live_range_def *lrd;
+       struct live_range_def *phi;
+       struct live_range_edge *edge;
+       char used[MAX_REGISTERS];
+       struct triple **expr;
+
+       /* Instead of doing just the trivial color select here I try
+        * a few extra things because a good color selection will help reduce
+        * copies.
+        */
+
+       /* Find the registers currently in use */
+       memset(used, 0, sizeof(used));
+       for(edge = range->edges; edge; edge = edge->next) {
+               if (edge->node->color == REG_UNSET) {
+                       continue;
+               }
+               reg_fill_used(state, used, edge->node->color);
+       }
+#if DEBUG_COLOR_GRAPH > 1
+       {
+               int i;
+               i = 0;
+               for(edge = range->edges; edge; edge = edge->next) {
+                       i++;
+               }
+               cgdebug_printf("\n%s edges: %d @%s:%d.%d\n", 
+                       tops(range->def->op), i, 
+                       range->def->filename, range->def->line, range->def->col);
+               for(i = 0; i < MAX_REGISTERS; i++) {
+                       if (used[i]) {
+                               cgdebug_printf("used: %s\n",
+                                       arch_reg_str(i));
+                       }
+               }
+       }       
+#endif
+
+       /* If a color is already assigned see if it will work */
+       if (range->color != REG_UNSET) {
+               struct live_range_def *lrd;
+               if (!used[range->color]) {
+                       return 1;
+               }
+               for(edge = range->edges; edge; edge = edge->next) {
+                       if (edge->node->color != range->color) {
+                               continue;
+                       }
+                       warning(state, edge->node->defs->def, "edge: ");
+                       lrd = edge->node->defs;
+                       do {
+                               warning(state, lrd->def, " %p %s",
+                                       lrd->def, tops(lrd->def->op));
+                               lrd = lrd->next;
+                       } while(lrd != edge->node->defs);
+               }
+               lrd = range->defs;
+               warning(state, range->defs->def, "def: ");
+               do {
+                       warning(state, lrd->def, " %p %s",
+                               lrd->def, tops(lrd->def->op));
+                       lrd = lrd->next;
+               } while(lrd != range->defs);
+               internal_error(state, range->defs->def,
+                       "live range with already used color %s",
+                       arch_reg_str(range->color));
+       }
+
+       /* If I feed into an expression reuse it's color.
+        * This should help remove copies in the case of 2 register instructions
+        * and phi functions.
+        */
+       phi = 0;
+       lrd = live_range_end(state, range, 0);
+       for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
+               entry = lrd->def->use;
+               for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
+                       struct live_range_def *insd;
+                       unsigned regcm;
+                       insd = &rstate->lrd[entry->member->id];
+                       if (insd->lr->defs == 0) {
+                               continue;
+                       }
+                       if (!phi && (insd->def->op == OP_PHI) &&
+                               !interfere(rstate, range, insd->lr)) {
+                               phi = insd;
+                       }
+                       if (insd->lr->color == REG_UNSET) {
+                               continue;
+                       }
+                       regcm = insd->lr->classes;
+                       if (((regcm & range->classes) == 0) ||
+                               (used[insd->lr->color])) {
+                               continue;
+                       }
+                       if (interfere(rstate, range, insd->lr)) {
+                               continue;
+                       }
+                       range->color = insd->lr->color;
+               }
+       }
+       /* If I feed into a phi function reuse it's color or the color
+        * of something else that feeds into the phi function.
+        */
+       if (phi) {
+               if (phi->lr->color != REG_UNSET) {
+                       if (used[phi->lr->color]) {
+                               range->color = phi->lr->color;
+                       }
+               }
+               else {
+                       expr = triple_rhs(state, phi->def, 0);
+                       for(; expr; expr = triple_rhs(state, phi->def, expr)) {
+                               struct live_range *lr;
+                               unsigned regcm;
+                               if (!*expr) {
+                                       continue;
+                               }
+                               lr = rstate->lrd[(*expr)->id].lr;
+                               if (lr->color == REG_UNSET) {
+                                       continue;
+                               }
+                               regcm = lr->classes;
+                               if (((regcm & range->classes) == 0) ||
+                                       (used[lr->color])) {
+                                       continue;
+                               }
+                               if (interfere(rstate, range, lr)) {
+                                       continue;
+                               }
+                               range->color = lr->color;
+                       }
+               }
+       }
+       /* If I don't interfere with a rhs node reuse it's color */
+       lrd = live_range_head(state, range, 0);
+       for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
+               expr = triple_rhs(state, lrd->def, 0);
+               for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
+                       struct live_range *lr;
+                       unsigned regcm;
+                       if (!*expr) {
+                               continue;
+                       }
+                       lr = rstate->lrd[(*expr)->id].lr;
+                       if (lr->color == REG_UNSET) {
+                               continue;
+                       }
+                       regcm = lr->classes;
+                       if (((regcm & range->classes) == 0) ||
+                               (used[lr->color])) {
+                               continue;
+                       }
+                       if (interfere(rstate, range, lr)) {
+                               continue;
                        }
                        range->color = lr->color;
                        break;
@@ -10588,34 +13432,53 @@ static void select_free_color(struct compile_state *state,
                        arch_select_free_register(state, used, range->classes);
        }
        if (range->color == REG_UNSET) {
+               struct live_range_def *lrd;
                int i;
+               if (split_ranges(state, rstate, used, range)) {
+                       return 0;
+               }
                for(edge = range->edges; edge; edge = edge->next) {
-                       if (edge->node->color == REG_UNSET) {
-                               continue;
-                       }
-                       warning(state, edge->node->def, "reg %s", 
+                       warning(state, edge->node->defs->def, "edge reg %s",
                                arch_reg_str(edge->node->color));
+                       lrd = edge->node->defs;
+                       do {
+                               warning(state, lrd->def, " %s %p",
+                                       tops(lrd->def->op), lrd->def);
+                               lrd = lrd->next;
+                       } while(lrd != edge->node->defs);
                }
-               warning(state, range->def, "classes: %x",
+               warning(state, range->defs->def, "range: ");
+               lrd = range->defs;
+               do {
+                       warning(state, lrd->def, " %s %p",
+                               tops(lrd->def->op), lrd->def);
+                       lrd = lrd->next;
+               } while(lrd != range->defs);
+                       
+               warning(state, range->defs->def, "classes: %x",
                        range->classes);
                for(i = 0; i < MAX_REGISTERS; i++) {
                        if (used[i]) {
-                               warning(state, range->def, "used: %s",
+                               warning(state, range->defs->def, "used: %s",
                                        arch_reg_str(i));
                        }
                }
 #if DEBUG_COLOR_GRAPH < 2
-               error(state, range->def, "too few registers");
+               error(state, range->defs->def, "too few registers");
 #else
-               internal_error(state, range->def, "too few registers");
+               internal_error(state, range->defs->def, "too few registers");
 #endif
        }
-       range->classes = arch_reg_regcm(state, range->color);
-       return;
+       range->classes &= arch_reg_regcm(state, range->color);
+       if ((range->color == REG_UNSET) || (range->classes == 0)) {
+               internal_error(state, range->defs->def, "select_free_color did not?");
+       }
+       return 1;
 }
 
-static void color_graph(struct compile_state *state, struct reg_state *rstate)
+static int color_graph(struct compile_state *state, struct reg_state *rstate)
 {
+       int colored;
        struct live_range_edge *edge;
        struct live_range *range;
        if (rstate->low) {
@@ -10653,7 +13516,7 @@ static void color_graph(struct compile_state *state, struct reg_state *rstate)
                }
        }
        else {
-               return;
+               return 1;
        }
        cgdebug_printf(" %d\n", range - rstate->lr);
        range->group_prev = 0;
@@ -10684,180 +13547,69 @@ static void color_graph(struct compile_state *state, struct reg_state *rstate)
                }
                node->degree -= 1;
        }
-       color_graph(state, rstate);
-       cgdebug_printf("Coloring %d @%s:%d.%d:", 
-               range - rstate->lr,
-               range->def->filename, range->def->line, range->def->col);
-       cgdebug_flush();
-       select_free_color(state, rstate, range);
-       if (range->color == -1) {
-               internal_error(state, range->def, "select_free_color did not?");
+       colored = color_graph(state, rstate);
+       if (colored) {
+               cgdebug_printf("Coloring %d @", range - rstate->lr);
+               cgdebug_loc(state, range->defs->def);
+               cgdebug_flush();
+               colored = select_free_color(state, rstate, range);
+               cgdebug_printf(" %s\n", arch_reg_str(range->color));
        }
-       cgdebug_printf(" %s\n", arch_reg_str(range->color));
+       return colored;
 }
 
-static void color_triples(struct compile_state *state, struct reg_state *rstate)
+static void verify_colors(struct compile_state *state, struct reg_state *rstate)
 {
        struct live_range *lr;
-       struct triple *first, *triple;
-       first = state->main_function->left;
-       triple = first;
+       struct live_range_edge *edge;
+       struct triple *ins, *first;
+       char used[MAX_REGISTERS];
+       first = RHS(state->main_function, 0);
+       ins = first;
        do {
-               if ((triple->id < 0) || (triple->id > rstate->ranges)) {
-                       internal_error(state, triple, 
-                               "triple without a live range");
-               }
-               lr = &rstate->lr[triple->id];
-               triple->id = MK_REG_ID(lr->color, 0);
-               triple = triple->next;
-       } while (triple != first);
-}
-
-static void print_interference_block(
-       struct compile_state *state, struct block *block, void *arg)
-
-{
-       struct reg_state *rstate = arg;
-       struct reg_block *rb;
-       struct triple *ptr;
-       int phi_present;
-       int done;
-       rb = &rstate->blocks[block->vertex];
-
-       printf("\nblock: %p (%d), %p<-%p %p<-%p\n", 
-               block, 
-               block->vertex,
-               block->left, 
-               block->left && block->left->use?block->left->use->member : 0,
-               block->right, 
-               block->right && block->right->use?block->right->use->member : 0);
-       if (rb->in) {
-               struct triple_reg_set *in_set;
-               printf("        in:");
-               for(in_set = rb->in; in_set; in_set = in_set->next) {
-                       printf(" %-10p", in_set->member);
-               }
-               printf("\n");
-       }
-       phi_present = 0;
-       for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
-               done = (ptr == block->last);
-               if (ptr->op == OP_PHI) {
-                       phi_present = 1;
-                       break;
-               }
-       }
-       if (phi_present) {
-               int edge;
-               for(edge = 0; edge < block->users; edge++) {
-                       printf("     in(%d):", edge);
-                       for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
-                               struct triple **slot;
-                               done = (ptr == block->last);
-                               if (ptr->op != OP_PHI) {
-                                       continue;
-                               }
-                               slot = (struct triple **)(ptr->left);
-                               printf(" %-10p", slot[edge]);
-                       }
-                       printf("\n");
-               }
-       }
-       if (block->first->op == OP_LABEL) {
-               printf("%p:\n", block->first);
-       }
-       for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
-               struct triple_set *user;
-               struct live_range *lr;
-               int op;
-               op = ptr->op;
-               done = (ptr == block->last);
-               lr = &rstate->lr[ptr->id];
-               
-               if (!IS_CONST_OP(op)) {
-                       if (ptr->u.block != block) {
-                               internal_error(state, ptr, 
-                                       "Wrong block pointer: %p",
-                                       ptr->u.block);
-                       }
-               }
-               if (op == OP_ADECL) {
-                       for(user = ptr->use; user; user = user->next) {
-                               struct live_range *lr;
-                               lr = &rstate->lr[user->member->id];
-                               if (!user->member->u.block) {
-                                       internal_error(state, user->member, 
-                                               "Use %p not in a block?",
-                                               user->member);
-                               }
-                               
+               if (triple_is_def(state, ins)) {
+                       if ((ins->id < 0) || (ins->id > rstate->defs)) {
+                               internal_error(state, ins, 
+                                       "triple without a live range def");
                        }
-               }
-               if (op == OP_INTCONST) {
-                       printf("(%p) %3d %-7s %08lx              @ %s:%d.%d\n",
-                               ptr, lr->color, tops(ptr->op), ptr->u.cval,
-                               ptr->filename, ptr->line, ptr->col);
-               }
-               else if (op == OP_PHI) {
-                       struct triple **slot;
-                       struct block *block;
-                       int edge;
-                       block = ptr->u.block;
-                       slot  = (struct triple **)(ptr->left);
-                       printf("(%p) %3d %-7s",
-                               ptr, lr->color, tops(ptr->op));
-                       for(edge = 0; edge < block->users; edge++) {
-                               printf(" %-10p", slot[edge]);
+                       lr = rstate->lrd[ins->id].lr;
+                       if (lr->color == REG_UNSET) {
+                               internal_error(state, ins,
+                                       "triple without a color");
                        }
-                       printf(" @%s:%d.%d\n", 
-                               ptr->filename, ptr->line, ptr->col);
-               }
-               else {
-                       printf("(%p) %3d %-7s %-10p %-10p @ %s:%d.%d\n",
-                               ptr, lr->color, tops(ptr->op), ptr->left, ptr->right,
-                               ptr->filename, ptr->line, ptr->col);
-               }
-               if (lr->edges > 0) {
-                       struct live_range_edge *edge;
-                       printf("           ");
+                       /* Find the registers used by the edges */
+                       memset(used, 0, sizeof(used));
                        for(edge = lr->edges; edge; edge = edge->next) {
-                               printf(" %-10p", edge->node->def);
+                               if (edge->node->color == REG_UNSET) {
+                                       internal_error(state, 0,
+                                               "live range without a color");
                        }
-                       printf("\n");
-               }
-               /* Do a bunch of sanity checks */
-               valid_op(state, ptr);
-               if ((ptr->id < 0) || (ptr->id > rstate->ranges)) {
-                       internal_error(state, ptr, "Invalid triple id: %d",
-                               ptr->id);
-               }
-               for(user = ptr->use; user; user = user->next) {
-                       struct triple *use;
-                       struct live_range *ulr;
-                       use = user->member;
-                       valid_op(state, use);
-                       if ((use->id < 0) || (use->id > rstate->ranges)) {
-                               internal_error(state, use, "Invalid triple id: %d",
-                                       use->id);
+                               reg_fill_used(state, used, edge->node->color);
                        }
-                       ulr = &rstate->lr[user->member->id];
-                       if (!IS_CONST_OP(user->member->op) &&
-                               !user->member->u.block) {
-                               internal_error(state, user->member,
-                                       "Use %p not in a block?",
-                                       user->member);
+                       if (used[lr->color]) {
+                               internal_error(state, ins,
+                                       "triple with already used color");
                        }
                }
-       }
-       if (rb->out) {
-               struct triple_reg_set *out_set;
-               printf("       out:");
-               for(out_set = rb->out; out_set; out_set = out_set->next) {
-                       printf(" %-10p", out_set->member);
+               ins = ins->next;
+       } while(ins != first);
+}
+
+static void color_triples(struct compile_state *state, struct reg_state *rstate)
+{
+       struct live_range *lr;
+       struct triple *first, *ins;
+       first = RHS(state->main_function, 0);
+       ins = first;
+       do {
+               if ((ins->id < 0) || (ins->id > rstate->defs)) {
+                       internal_error(state, ins, 
+                               "triple without a live range");
                }
-               printf("\n");
-       }
-       printf("\n");
+               lr = rstate->lrd[ins->id].lr;
+               SET_REG(ins->id, lr->color);
+               ins = ins->next;
+       } while (ins != first);
 }
 
 static struct live_range *merge_sort_lr(
@@ -10875,7 +13627,9 @@ static struct live_range *merge_sort_lr(
                join_tail = &join;
                /* merge the two lists */
                while(first && mid) {
-                       if (first->degree <= mid->degree) {
+                       if ((first->degree < mid->degree) ||
+                               ((first->degree == mid->degree) &&
+                                       (first->length < mid->length))) {
                                pick = first;
                                first = first->group_next;
                                if (first) {
@@ -10897,10 +13651,12 @@ static struct live_range *merge_sort_lr(
                /* Splice the remaining list */
                pick = (first)? first : mid;
                *join_tail = pick;
-               pick->group_prev = join_tail;
+               if (pick) { 
+                       pick->group_prev = join_tail;
+               }
        }
        else {
-               if (!first->def) {
+               if (!first->defs) {
                        first = 0;
                }
                join = first;
@@ -10908,104 +13664,212 @@ static struct live_range *merge_sort_lr(
        return join;
 }
 
+static void ids_from_rstate(struct compile_state *state, 
+       struct reg_state *rstate)
+{
+       struct triple *ins, *first;
+       if (!rstate->defs) {
+               return;
+       }
+       /* Display the graph if desired */
+       if (state->debug & DEBUG_INTERFERENCE) {
+               print_blocks(state, stdout);
+               print_control_flow(state);
+       }
+       first = RHS(state->main_function, 0);
+       ins = first;
+       do {
+               if (ins->id) {
+                       struct live_range_def *lrd;
+                       lrd = &rstate->lrd[ins->id];
+                       ins->id = lrd->orig_id;
+               }
+               ins = ins->next;
+       } while(ins != first);
+}
+
+static void cleanup_live_edges(struct reg_state *rstate)
+{
+       int i;
+       /* Free the edges on each node */
+       for(i = 1; i <= rstate->ranges; i++) {
+               remove_live_edges(rstate, &rstate->lr[i]);
+       }
+}
+
+static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
+{
+       cleanup_live_edges(rstate);
+       xfree(rstate->lrd);
+       xfree(rstate->lr);
+
+       /* Free the variable lifetime information */
+       if (rstate->blocks) {
+               free_variable_lifetimes(state, rstate->blocks);
+       }
+       rstate->defs = 0;
+       rstate->ranges = 0;
+       rstate->lrd = 0;
+       rstate->lr = 0;
+       rstate->blocks = 0;
+}
+
+static void verify_consistency(struct compile_state *state);
 static void allocate_registers(struct compile_state *state)
 {
        struct reg_state rstate;
-       struct live_range **point, **next;
-       int i;
+       int colored;
 
        /* Clear out the reg_state */
        memset(&rstate, 0, sizeof(rstate));
+       rstate.max_passes = MAX_ALLOCATION_PASSES;
 
-       /* Compute the variable lifetimes */
-       rstate.blocks = compute_variable_lifetimes(state);
+       do {
+               struct live_range **point, **next;
+               int conflicts;
+               int tangles;
+               int coalesced;
 
-       /* Allocate and initialize the live ranges */
-       initialize_live_ranges(state, &rstate);
+#if DEBUG_RANGE_CONFLICTS
+               fprintf(stderr, "pass: %d\n", rstate.passes);
+#endif
 
-       /* Compute the interference graph */
-       walk_variable_lifetimes(
-               state, rstate.blocks, graph_ins, &rstate);
+               /* Restore ids */
+               ids_from_rstate(state, &rstate);
 
-       /* Display the interference graph if desired */
-       if (state->debug & DEBUG_INTERFERENCE) {
-               printf("\nlive variables by block\n");
-               walk_blocks(state, print_interference_block, &rstate);
-               printf("\nlive variables by instruction\n");
-               walk_variable_lifetimes(
-                       state, rstate.blocks, 
-                       print_interference_ins, &rstate);
-       }
+               /* Cleanup the temporary data structures */
+               cleanup_rstate(state, &rstate);
 
-       /* Do not perform coalescing!  It is a neat idea but it limits what
-        * we can do later.  It has no benefits that decrease register pressure.
-        * It only decreases instruction count.
-        *
-        * It might be worth testing this reducing the number of
-        * live_ragnes as opposed to splitting them seems to help.
-        */
+               /* Compute the variable lifetimes */
+               rstate.blocks = compute_variable_lifetimes(state);
 
-       /* Build the groups low and high.  But with the nodes
-        * first sorted by degree order.
-        */
-       rstate.low_tail  = &rstate.low;
-       rstate.high_tail = &rstate.high;
-       rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
-       rstate.high->group_prev = &rstate.high;
-       for(point = &rstate.high; *point; point = &(*point)->group_next)
-               ;
-       rstate.high_tail = point;
-       /* Walk through the high list and move everything that needs
-        * to be onto low.
-        */
-       for(point = &rstate.high; *point; point = next) {
-               struct live_range *range;
-               next = &(*point)->group_next;
-               range = *point;
+               /* Fix invalid mandatory live range coalesce conflicts */
+               conflicts = correct_coalesce_conflicts(state, rstate.blocks);
 
-               /* If it has a low degree or it already has a color
-                * place the node in low.
+               /* Fix two simultaneous uses of the same register.
+                * In a few pathlogical cases a partial untangle moves
+                * the tangle to a part of the graph we won't revisit.
+                * So we keep looping until we have no more tangle fixes
+                * to apply.
                 */
-               if ((range->degree < regc_max_size(state, range->classes)) ||
-                       (range->color != REG_UNSET)) {
-                       cgdebug_printf("Lo: %5d degree %5d%s\n", 
-                               range - rstate.lr, range->degree,
-                               (range->color != REG_UNSET) ? " (colored)": "");
-                       *range->group_prev = range->group_next;
-                       if (range->group_next) {
-                               range->group_next->group_prev = range->group_prev;
-                       }
-                       if (&range->group_next == rstate.high_tail) {
-                               rstate.high_tail = range->group_prev;
-                       }
-                       range->group_prev  = rstate.low_tail;
-                       range->group_next  = 0;
-                       *rstate.low_tail   = range;
-                       rstate.low_tail    = &range->group_next;
-                       next = point;
-               }
-               else {
-                       cgdebug_printf("hi: %5d degree %5d%s\n", 
-                               range - rstate.lr, range->degree,
-                               (range->color != REG_UNSET) ? " (colored)": "");
+               do {
+                       tangles = correct_tangles(state, rstate.blocks);
+               } while(tangles);
+
+               if (state->debug & DEBUG_INSERTED_COPIES) {
+                       printf("After resolve_tangles\n");
+                       print_blocks(state, stdout);
+                       print_control_flow(state);
                }
+               verify_consistency(state);
                
-       }
-       /* Color the live_ranges */
-       color_graph(state, &rstate);
+               /* Allocate and initialize the live ranges */
+               initialize_live_ranges(state, &rstate);
 
-       /* Move the colors from the graph to the triples */
-       color_triples(state, &rstate);
+               /* Note current doing coalescing in a loop appears to 
+                * buys me nothing.  The code is left this way in case
+                * there is some value in it.  Or if a future bugfix
+                *  yields some benefit.
+                */
+               do {
+#if DEBUG_COALESCING
+                       fprintf(stderr, "coalescing\n");
+#endif                 
+                       /* Remove any previous live edge calculations */
+                       cleanup_live_edges(&rstate);
+
+                       /* Compute the interference graph */
+                       walk_variable_lifetimes(
+                               state, rstate.blocks, graph_ins, &rstate);
+                       
+                       /* Display the interference graph if desired */
+                       if (state->debug & DEBUG_INTERFERENCE) {
+                               print_interference_blocks(state, &rstate, stdout, 1);
+                               printf("\nlive variables by instruction\n");
+                               walk_variable_lifetimes(
+                                       state, rstate.blocks, 
+                                       print_interference_ins, &rstate);
+                       }
+                       
+                       coalesced = coalesce_live_ranges(state, &rstate);
 
-       /* Free the edges on each node */
-       for(i = 1; i <= rstate.ranges; i++) {
-               remove_live_edges(&rstate, &rstate.lr[i]);
-       }
-       xfree(rstate.lr);
+#if DEBUG_COALESCING
+                       fprintf(stderr, "coalesced: %d\n", coalesced);
+#endif
+               } while(coalesced);
 
-       /* Free the variable lifetime information */
-       free_variable_lifetimes(state, rstate.blocks);
+#if DEBUG_CONSISTENCY > 1
+# if 0
+               fprintf(stderr, "verify_graph_ins...\n");
+# endif
+               /* Verify the interference graph */
+               walk_variable_lifetimes(
+                       state, rstate.blocks, verify_graph_ins, &rstate);
+# if 0
+               fprintf(stderr, "verify_graph_ins done\n");
+#endif
+#endif
+                       
+               /* Build the groups low and high.  But with the nodes
+                * first sorted by degree order.
+                */
+               rstate.low_tail  = &rstate.low;
+               rstate.high_tail = &rstate.high;
+               rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
+               if (rstate.high) {
+                       rstate.high->group_prev = &rstate.high;
+               }
+               for(point = &rstate.high; *point; point = &(*point)->group_next)
+                       ;
+               rstate.high_tail = point;
+               /* Walk through the high list and move everything that needs
+                * to be onto low.
+                */
+               for(point = &rstate.high; *point; point = next) {
+                       struct live_range *range;
+                       next = &(*point)->group_next;
+                       range = *point;
+                       
+                       /* If it has a low degree or it already has a color
+                        * place the node in low.
+                        */
+                       if ((range->degree < regc_max_size(state, range->classes)) ||
+                               (range->color != REG_UNSET)) {
+                               cgdebug_printf("Lo: %5d degree %5d%s\n", 
+                                       range - rstate.lr, range->degree,
+                                       (range->color != REG_UNSET) ? " (colored)": "");
+                               *range->group_prev = range->group_next;
+                               if (range->group_next) {
+                                       range->group_next->group_prev = range->group_prev;
+                               }
+                               if (&range->group_next == rstate.high_tail) {
+                                       rstate.high_tail = range->group_prev;
+                               }
+                               range->group_prev  = rstate.low_tail;
+                               range->group_next  = 0;
+                               *rstate.low_tail   = range;
+                               rstate.low_tail    = &range->group_next;
+                               next = point;
+                       }
+                       else {
+                               cgdebug_printf("hi: %5d degree %5d%s\n", 
+                                       range - rstate.lr, range->degree,
+                                       (range->color != REG_UNSET) ? " (colored)": "");
+                       }
+               }
+               /* Color the live_ranges */
+               colored = color_graph(state, &rstate);
+               rstate.passes++;
+       } while (!colored);
+
+       /* Verify the graph was properly colored */
+       verify_colors(state, &rstate);
 
+       /* Move the colors from the graph to the triples */
+       color_triples(state, &rstate);
+
+       /* Cleanup the temporary data structures */
+       cleanup_rstate(state, &rstate);
 }
 
 /* Sparce Conditional Constant Propogation
@@ -11014,6 +13878,7 @@ static void allocate_registers(struct compile_state *state)
 struct ssa_edge;
 struct flow_block;
 struct lattice_node {
+       unsigned old_id;
        struct triple *def;
        struct ssa_edge *out;
        struct flow_block *fblock;
@@ -11022,7 +13887,6 @@ struct lattice_node {
         * lattice const  is_const(val)
         * lattice low    val == 0
         */
-       struct triple scratch;
 };
 struct ssa_edge {
        struct lattice_node *src;
@@ -11048,6 +13912,7 @@ struct flow_block {
 };
 
 struct scc_state {
+       int ins_count;
        struct lattice_node *lattice;
        struct ssa_edge     *ssa_edges;
        struct flow_block   *flow_blocks;
@@ -11136,7 +14001,7 @@ static void initialize_scc_state(
        memset(scc, 0, sizeof(*scc));
 
        /* Inialize pass zero find out how much memory we need */
-       first = state->main_function->left;
+       first = RHS(state->main_function, 0);
        ins = first;
        ins_count = ssa_edge_count = 0;
        do {
@@ -11151,6 +14016,7 @@ static void initialize_scc_state(
        fprintf(stderr, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
                ins_count, ssa_edge_count, state->last_vertex);
 #endif
+       scc->ins_count   = ins_count;
        scc->lattice     = 
                xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
        scc->ssa_edges   = 
@@ -11165,7 +14031,6 @@ static void initialize_scc_state(
        ins_index = ssa_edge_index = fblock_index = 0;
        ins = first;
        do {
-               ins->id = 0;
                if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
                        block = ins->u.block;
                        if (!block) {
@@ -11179,12 +14044,13 @@ static void initialize_scc_state(
                {
                        struct lattice_node *lnode;
                        ins_index += 1;
-                       ins->id = ins_index;
                        lnode = &scc->lattice[ins_index];
                        lnode->def = ins;
                        lnode->out = 0;
                        lnode->fblock = fblock;
                        lnode->val = ins; /* LATTICE HIGH */
+                       lnode->old_id = ins->id;
+                       ins->id = ins_index;
                }
                ins = ins->next;
        } while(ins != first);
@@ -11300,6 +14166,7 @@ static void free_scc_state(
        xfree(scc->flow_blocks);
        xfree(scc->ssa_edges);
        xfree(scc->lattice);
+       
 }
 
 static struct lattice_node *triple_to_lattice(
@@ -11311,19 +14178,65 @@ static struct lattice_node *triple_to_lattice(
        return &scc->lattice[ins->id];
 }
 
+static struct triple *preserve_lval(
+       struct compile_state *state, struct lattice_node *lnode)
+{
+       struct triple *old;
+       /* Preserve the original value */
+       if (lnode->val) {
+               old = dup_triple(state, lnode->val);
+               if (lnode->val != lnode->def) {
+                       xfree(lnode->val);
+               }
+               lnode->val = 0;
+       } else {
+               old = 0;
+       }
+       return old;
+}
+
+static int lval_changed(struct compile_state *state, 
+       struct triple *old, struct lattice_node *lnode)
+{
+       int changed;
+       /* See if the lattice value has changed */
+       changed = 1;
+       if (!old && !lnode->val) {
+               changed = 0;
+       }
+       if (changed && lnode->val && !is_const(lnode->val)) {
+               changed = 0;
+       }
+       if (changed &&
+               lnode->val && old &&
+               (memcmp(lnode->val->param, old->param,
+                       TRIPLE_SIZE(lnode->val->sizes) * sizeof(lnode->val->param[0])) == 0) &&
+               (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
+               changed = 0;
+       }
+       if (old) {
+               xfree(old);
+       }
+       return changed;
+
+}
+
 static void scc_visit_phi(struct compile_state *state, struct scc_state *scc, 
        struct lattice_node *lnode)
 {
        struct lattice_node *tmp;
-       struct triple **slot;
+       struct triple **slot, *old;
        struct flow_edge *fedge;
        int index;
        if (lnode->def->op != OP_PHI) {
                internal_error(state, lnode->def, "not phi");
        }
+       /* Store the original value */
+       old = preserve_lval(state, lnode);
+
        /* default to lattice high */
        lnode->val = lnode->def;
-       slot = (struct triple **)lnode->def->left;
+       slot = &RHS(lnode->def, 0);
        index = 0;
        for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
                if (!fedge->executable) {
@@ -11343,7 +14256,8 @@ static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
                }
                /* meet(lattice high, X) = X */
                else if (!is_const(lnode->val)) {
-                       lnode->val = tmp->val;
+                       lnode->val = dup_triple(state, tmp->val);
+                       lnode->val->type = lnode->def->type;
                }
                /* meet(const, const) = const or lattice low */
                else if (!constants_equal(state, lnode->val, tmp->val)) {
@@ -11353,134 +14267,118 @@ static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
                        break;
                }
        }
-       /* Do I need to update any work lists here? */
 #if DEBUG_SCC
        fprintf(stderr, "phi: %d -> %s\n",
                lnode->def->id,
                (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
 #endif
+       /* If the lattice value has changed update the work lists. */
+       if (lval_changed(state, old, lnode)) {
+               struct ssa_edge *sedge;
+               for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
+                       scc_add_sedge(state, scc, sedge);
+               }
+       }
 }
 
 static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
        struct lattice_node *lnode)
 {
        int changed;
-       struct triple old_buf, *old;
+       struct triple *old, *scratch;
        struct triple **dexpr, **vexpr;
+       int count, i;
        
        /* Store the original value */
-       if (lnode->val) {
-               old = &old_buf;
-               memcpy(old, lnode->val, sizeof(*old));
-       } else {
-               old = 0;
-       }
+       old = preserve_lval(state, lnode);
+
        /* Reinitialize the value */
-       memset(&lnode->scratch, 0, sizeof(lnode->scratch));
-       lnode->val = &lnode->scratch;
-       lnode->val->next     = &lnode->scratch;
-       lnode->val->prev     = &lnode->scratch;
-       lnode->val->use      = 0;
-       lnode->val->type     = lnode->def->type;
-       lnode->val->op       = lnode->def->op;
-       lnode->val->left     = 0;
-       lnode->val->right    = 0;
-       lnode->val->filename = lnode->def->filename;
-       lnode->val->line     = lnode->def->line;
-       lnode->val->col      = lnode->def->col;
-       if (lnode->def->op != OP_STORE) {
-               check_lhs(state,  lnode->def);
-       }
-       dexpr = triple_rhs(state, lnode->def, 0);
-       vexpr = triple_rhs(state, lnode->val, 0);
-       while(dexpr && vexpr) {
-               struct lattice_node *tmp;
-               tmp = triple_to_lattice(state, scc, *dexpr); 
-               *vexpr = (tmp->val)? tmp->val : tmp->def;
-               dexpr = triple_rhs(state, lnode->def, dexpr);
-               vexpr = triple_rhs(state, lnode->val, vexpr);
-       }
-       if (is_const(lnode->val)) {
-               memcpy(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u));
-       }
-       else if (lnode->val->op == OP_BRANCH) {
-               lnode->val->left = lnode->def->left;
-               lnode->val->next = lnode->def->next;
-       }
-       else if (lnode->val->op == OP_SDECL) {
-               lnode->val->left = lnode->def->left;
+       lnode->val = scratch = dup_triple(state, lnode->def);
+       scratch->id = lnode->old_id;
+       scratch->next     = scratch;
+       scratch->prev     = scratch;
+       scratch->use      = 0;
+
+       count = TRIPLE_SIZE(scratch->sizes);
+       for(i = 0; i < count; i++) {
+               dexpr = &lnode->def->param[i];
+               vexpr = &scratch->param[i];
+               *vexpr = *dexpr;
+               if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
+                       (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
+                       *dexpr) {
+                       struct lattice_node *tmp;
+                       tmp = triple_to_lattice(state, scc, *dexpr);
+                       *vexpr = (tmp->val)? tmp->val : tmp->def;
+               }
+       }
+       if (scratch->op == OP_BRANCH) {
+               scratch->next = lnode->def->next;
        }
        /* Recompute the value */
 #warning "FIXME see if simplify does anything bad"
        /* So far it looks like only the strength reduction
         * optimization are things I need to worry about.
         */
-       simplify(state, lnode->val);
+       simplify(state, scratch);
        /* Cleanup my value */
-       if (lnode->scratch.use) {
+       if (scratch->use) {
                internal_error(state, lnode->def, "scratch used?");
        }
-       if ((lnode->scratch.prev != &lnode->scratch) ||
-               ((lnode->scratch.next != &lnode->scratch) && 
+       if ((scratch->prev != scratch) ||
+               ((scratch->next != scratch) &&
                        ((lnode->def->op != OP_BRANCH) ||
-                               (lnode->scratch.next != lnode->def->next)))) {
+                               (scratch->next != lnode->def->next)))) {
                internal_error(state, lnode->def, "scratch in list?");
        }
        /* undo any uses... */
-       vexpr = triple_rhs(state, lnode->val, 0);
-       for(;vexpr;vexpr = triple_rhs(state, lnode->val, vexpr)) {
-               unuse_triple(*vexpr, lnode->val);
-       }
-       if (!is_const(lnode->val)) {
-               dexpr = triple_rhs(state, lnode->def, 0);
-               for(;dexpr;dexpr = triple_rhs(state, lnode->def, dexpr)) {
-                       struct lattice_node *tmp;
-                       tmp = triple_to_lattice(state, scc, *dexpr);
-                       if (!tmp->val) {
-                               lnode->val = 0;
+       count = TRIPLE_SIZE(scratch->sizes);
+       for(i = 0; i < count; i++) {
+               vexpr = &scratch->param[i];
+               if (*vexpr) {
+                       unuse_triple(*vexpr, scratch);
+               }
+       }
+       if (!is_const(scratch)) {
+               for(i = 0; i < count; i++) {
+                       dexpr = &lnode->def->param[i];
+                       if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
+                               (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
+                               *dexpr) {
+                               struct lattice_node *tmp;
+                               tmp = triple_to_lattice(state, scc, *dexpr);
+                               if (!tmp->val) {
+                                       lnode->val = 0;
+                               }
                        }
                }
        }
        if (lnode->val && 
                (lnode->val->op == lnode->def->op) &&
-               (lnode->val->left == lnode->def->left) &&
-               (lnode->val->right == lnode->def->right) &&
-               (((memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0) &&
-                       is_const(lnode->val)) || !is_const(lnode->val))) {
+               (memcmp(lnode->val->param, lnode->def->param, 
+                       count * sizeof(lnode->val->param[0])) == 0) &&
+               (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
                lnode->val = lnode->def;
        }
        /* Find the cases that are always lattice lo */
        if (lnode->val && 
-               triple_is_def(lnode->val) &&
+               triple_is_def(state, lnode->val) &&
                !triple_is_pure(state, lnode->val)) {
                lnode->val = 0;
        }
-#if 1
        if (lnode->val && 
                (lnode->val->op == OP_SDECL) && 
                (lnode->val != lnode->def)) {
                internal_error(state, lnode->def, "bad sdecl");
        }
-#endif
        /* See if the lattice value has changed */
-       changed = 1;
-       if (!old && !lnode->val) {
-               changed = 0;
-       }
-       if (changed && lnode->val && !is_const(lnode->val)) {
-               changed = 0;
-       }
-       if (changed &&
-               lnode->val && old &&
-               (lnode->val->op == old->op) &&
-               (lnode->val->left == old->left) &&
-               (lnode->val->right == old->right) &&
-               (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
-               changed = 0;
+       changed = lval_changed(state, old, lnode);
+       if (lnode->val != scratch) {
+               xfree(scratch);
        }
        return changed;
-
 }
+
 static void scc_visit_branch(struct compile_state *state, struct scc_state *scc,
        struct lattice_node *lnode)
 {
@@ -11495,9 +14393,9 @@ static void scc_visit_branch(struct compile_state *state, struct scc_state *scc,
                        fprintf(stderr, " %d", fedge->dst->block->vertex);
                }
                fprintf(stderr, " )");
-               if (lnode->def->right) {
+               if (TRIPLE_RHS(lnode->def->sizes) > 0) {
                        fprintf(stderr, " <- %d",
-                               lnode->def->right->id);
+                               RHS(lnode->def, 0)->id);
                }
                fprintf(stderr, "\n");
        }
@@ -11506,10 +14404,10 @@ static void scc_visit_branch(struct compile_state *state, struct scc_state *scc,
                internal_error(state, lnode->def, "not branch");
        }
        /* This only applies to conditional branches */
-       if (lnode->def->right == 0) {
+       if (TRIPLE_RHS(lnode->def->sizes) == 0) {
                return;
        }
-       cond = triple_to_lattice(state, scc, lnode->def->right);
+       cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
        if (cond->val && !is_const(cond->val)) {
 #warning "FIXME do I need to do something here?"
                warning(state, cond->def, "condition not constant?");
@@ -11541,7 +14439,9 @@ static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
                        lnode->def->id, tops(lnode->def->op));
                expr = triple_rhs(state, lnode->def, 0);
                for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
-                       fprintf(stderr, " %d", (*expr)->id);
+                       if (*expr) {
+                               fprintf(stderr, " %d", (*expr)->id);
+                       }
                }
                fprintf(stderr, " ) -> %s\n",
                        (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
@@ -11563,11 +14463,13 @@ static void scc_writeback_values(
        struct compile_state *state, struct scc_state *scc)
 {
        struct triple *first, *ins;
-       first = state->main_function->left;
+       first = RHS(state->main_function, 0);
        ins = first;
        do {
                struct lattice_node *lnode;
                lnode = triple_to_lattice(state, scc, ins);
+               /* Restore id */
+               ins->id = lnode->old_id;
 #if DEBUG_SCC
                if (lnode->val && !is_const(lnode->val)) {
                        warning(state, lnode->def, 
@@ -11582,7 +14484,7 @@ static void scc_writeback_values(
                                break;
                        case OP_ADDRCONST:
                                mkaddr_const(state, ins, 
-                                       lnode->val->left, lnode->val->u.cval);
+                                       MISC(lnode->val, 0), lnode->val->u.cval);
                                break;
                        default:
                                /* By default don't copy the changes,
@@ -11591,6 +14493,13 @@ static void scc_writeback_values(
                                simplify(state, ins);
                                break;
                        }
+                       if (is_const(lnode->val) &&
+                               !constants_equal(state, lnode->val, ins)) {
+                               internal_error(state, 0, "constants not equal");
+                       }
+                       /* Free the lattice nodes */
+                       xfree(lnode->val);
+                       lnode->val = 0;
                }
                ins = ins->next;
        } while(ins != first);
@@ -11630,78 +14539,347 @@ static void scc_transform(struct compile_state *state)
                                        time++;
                                }
                        }
-#if DEBUG_SCC
-                       fprintf(stderr, "vertex: %d time: %d\n", 
-                               block->vertex, time);
-                       
-#endif
-                       done = 0;
-                       for(ptr = block->first; !done; ptr = ptr->next) {
-                               struct lattice_node *lnode;
-                               done = (ptr == block->last);
-                               lnode = &scc.lattice[ptr->id];
-                               if (ptr->op == OP_PHI) {
-                                       scc_visit_phi(state, &scc, lnode);
-                               }
-                               else if (time == 1) {
-                                       scc_visit_expr(state, &scc, lnode);
+#if DEBUG_SCC
+                       fprintf(stderr, "vertex: %d time: %d\n", 
+                               block->vertex, time);
+                       
+#endif
+                       done = 0;
+                       for(ptr = block->first; !done; ptr = ptr->next) {
+                               struct lattice_node *lnode;
+                               done = (ptr == block->last);
+                               lnode = &scc.lattice[ptr->id];
+                               if (ptr->op == OP_PHI) {
+                                       scc_visit_phi(state, &scc, lnode);
+                               }
+                               else if (time == 1) {
+                                       scc_visit_expr(state, &scc, lnode);
+                               }
+                       }
+                       if (fblock->out && !fblock->out->out_next) {
+                               scc_add_fedge(state, &scc, fblock->out);
+                       }
+               }
+               while((sedge = scc_next_sedge(state, &scc))) {
+                       struct lattice_node *lnode;
+                       struct flow_block *fblock;
+                       lnode = sedge->dst;
+                       fblock = lnode->fblock;
+#if DEBUG_SCC
+                       fprintf(stderr, "sedge: %5d (%5d -> %5d)\n",
+                               sedge - scc.ssa_edges,
+                               sedge->src->def->id,
+                               sedge->dst->def->id);
+#endif
+                       if (lnode->def->op == OP_PHI) {
+                               scc_visit_phi(state, &scc, lnode);
+                       }
+                       else {
+                               for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
+                                       if (fptr->executable) {
+                                               break;
+                                       }
+                               }
+                               if (fptr) {
+                                       scc_visit_expr(state, &scc, lnode);
+                               }
+                       }
+               }
+       }
+       
+       scc_writeback_values(state, &scc);
+       free_scc_state(state, &scc);
+}
+
+
+static void transform_to_arch_instructions(struct compile_state *state)
+{
+       struct triple *ins, *first;
+       first = RHS(state->main_function, 0);
+       ins = first;
+       do {
+               ins = transform_to_arch_instruction(state, ins);
+       } while(ins != first);
+}
+
+#if DEBUG_CONSISTENCY
+static void verify_uses(struct compile_state *state)
+{
+       struct triple *first, *ins;
+       struct triple_set *set;
+       first = RHS(state->main_function, 0);
+       ins = first;
+       do {
+               struct triple **expr;
+               expr = triple_rhs(state, ins, 0);
+               for(; expr; expr = triple_rhs(state, ins, expr)) {
+                       struct triple *rhs;
+                       rhs = *expr;
+                       for(set = rhs?rhs->use:0; set; set = set->next) {
+                               if (set->member == ins) {
+                                       break;
+                               }
+                       }
+                       if (!set) {
+                               internal_error(state, ins, "rhs not used");
+                       }
+               }
+               expr = triple_lhs(state, ins, 0);
+               for(; expr; expr = triple_lhs(state, ins, expr)) {
+                       struct triple *lhs;
+                       lhs = *expr;
+                       for(set =  lhs?lhs->use:0; set; set = set->next) {
+                               if (set->member == ins) {
+                                       break;
+                               }
+                       }
+                       if (!set) {
+                               internal_error(state, ins, "lhs not used");
+                       }
+               }
+               ins = ins->next;
+       } while(ins != first);
+       
+}
+static void verify_blocks_present(struct compile_state *state)
+{
+       struct triple *first, *ins;
+       if (!state->first_block) {
+               return;
+       }
+       first = RHS(state->main_function, 0);
+       ins = first;
+       do {
+               valid_ins(state, ins);
+               if (triple_stores_block(state, ins)) {
+                       if (!ins->u.block) {
+                               internal_error(state, ins, 
+                                       "%p not in a block?\n", ins);
+                       }
+               }
+               ins = ins->next;
+       } while(ins != first);
+       
+       
+}
+static void verify_blocks(struct compile_state *state)
+{
+       struct triple *ins;
+       struct block *block;
+       int blocks;
+       block = state->first_block;
+       if (!block) {
+               return;
+       }
+       blocks = 0;
+       do {
+               int users;
+               struct block_set *user;
+               blocks++;
+               for(ins = block->first; ins != block->last->next; ins = ins->next) {
+                       if (triple_stores_block(state, ins) && (ins->u.block != block)) {
+                               internal_error(state, ins, "inconsitent block specified");
+                       }
+                       valid_ins(state, ins);
+               }
+               users = 0;
+               for(user = block->use; user; user = user->next) {
+                       users++;
+                       if ((block == state->last_block) &&
+                               (user->member == state->first_block)) {
+                               continue;
+                       }
+                       if ((user->member->left != block) &&
+                               (user->member->right != block)) {
+                               internal_error(state, user->member->first,
+                                       "user does not use block");
+                       }
+               }
+               if (triple_is_branch(state, block->last) &&
+                       (block->right != block_of_triple(state, TARG(block->last, 0))))
+               {
+                       internal_error(state, block->last, "block->right != TARG(0)");
+               }
+               if (!triple_is_uncond_branch(state, block->last) &&
+                       (block != state->last_block) &&
+                       (block->left != block_of_triple(state, block->last->next)))
+               {
+                       internal_error(state, block->last, "block->left != block->last->next");
+               }
+               if (block->left) {
+                       for(user = block->left->use; user; user = user->next) {
+                               if (user->member == block) {
+                                       break;
+                               }
+                       }
+                       if (!user || user->member != block) {
+                               internal_error(state, block->first,
+                                       "block does not use left");
+                       }
+               }
+               if (block->right) {
+                       for(user = block->right->use; user; user = user->next) {
+                               if (user->member == block) {
+                                       break;
+                               }
+                       }
+                       if (!user || user->member != block) {
+                               internal_error(state, block->first,
+                                       "block does not use right");
+                       }
+               }
+               if (block->users != users) {
+                       internal_error(state, block->first, 
+                               "computed users %d != stored users %d\n",
+                               users, block->users);
+               }
+               if (!triple_stores_block(state, block->last->next)) {
+                       internal_error(state, block->last->next, 
+                               "cannot find next block");
+               }
+               block = block->last->next->u.block;
+               if (!block) {
+                       internal_error(state, block->last->next,
+                               "bad next block");
+               }
+       } while(block != state->first_block);
+       if (blocks != state->last_vertex) {
+               internal_error(state, 0, "computed blocks != stored blocks %d\n",
+                       blocks, state->last_vertex);
+       }
+}
+
+static void verify_domination(struct compile_state *state)
+{
+       struct triple *first, *ins;
+       struct triple_set *set;
+       if (!state->first_block) {
+               return;
+       }
+       
+       first = RHS(state->main_function, 0);
+       ins = first;
+       do {
+               for(set = ins->use; set; set = set->next) {
+                       struct triple **expr;
+                       if (set->member->op == OP_PHI) {
+                               continue;
+                       }
+                       /* See if the use is on the righ hand side */
+                       expr = triple_rhs(state, set->member, 0);
+                       for(; expr ; expr = triple_rhs(state, set->member, expr)) {
+                               if (*expr == ins) {
+                                       break;
                                }
                        }
-                       if (fblock->out && !fblock->out->out_next) {
-                               scc_add_fedge(state, &scc, fblock->out);
+                       if (expr &&
+                               !tdominates(state, ins, set->member)) {
+                               internal_error(state, set->member, 
+                                       "non dominated rhs use?");
                        }
                }
-               while((sedge = scc_next_sedge(state, &scc))) {
-                       struct lattice_node *lnode;
-                       struct flow_block *fblock;
-                       lnode = sedge->dst;
-                       fblock = lnode->fblock;
-#if DEBUG_SCC
-                       fprintf(stderr, "sedge: %5d (%5d -> %5d)\n",
-                               sedge - scc.ssa_edges,
-                               sedge->src->def->id,
-                               sedge->dst->def->id);
-#endif
-                       if (lnode->def->op == OP_PHI) {
-                               scc_visit_phi(state, &scc, lnode);
+               ins = ins->next;
+       } while(ins != first);
+}
+
+static void verify_piece(struct compile_state *state)
+{
+       struct triple *first, *ins;
+       first = RHS(state->main_function, 0);
+       ins = first;
+       do {
+               struct triple *ptr;
+               int lhs, i;
+               lhs = TRIPLE_LHS(ins->sizes);
+               for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
+                       if (ptr != LHS(ins, i)) {
+                               internal_error(state, ins, "malformed lhs on %s",
+                                       tops(ins->op));
                        }
-                       else {
-                               for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
-                                       if (fptr->executable) {
-                                               break;
-                                       }
-                               }
-                               if (fptr) {
-                                       scc_visit_expr(state, &scc, lnode);
-                               }
+                       if (ptr->op != OP_PIECE) {
+                               internal_error(state, ins, "bad lhs op %s at %d on %s",
+                                       tops(ptr->op), i, tops(ins->op));
+                       }
+                       if (ptr->u.cval != i) {
+                               internal_error(state, ins, "bad u.cval of %d %d expected",
+                                       ptr->u.cval, i);
                        }
                }
-       }
+               ins = ins->next;
+       } while(ins != first);
+}
+static void verify_ins_colors(struct compile_state *state)
+{
+       struct triple *first, *ins;
        
-       scc_writeback_values(state, &scc);
-       /* FINISH ME move constants from scratch values into the tree */
-       free_scc_state(state, &scc);
+       first = RHS(state->main_function, 0);
+       ins = first;
+       do {
+               ins = ins->next;
+       } while(ins != first);
 }
-
-
-static void transform_to_arch_instructions(struct compile_state *state);
-
+static void verify_consistency(struct compile_state *state)
+{
+       verify_uses(state);
+       verify_blocks_present(state);
+       verify_blocks(state);
+       verify_domination(state);
+       verify_piece(state);
+       verify_ins_colors(state);
+}
+#else 
+static void verify_consistency(struct compile_state *state) {}
+#endif /* DEBUG_USES */
 
 static void optimize(struct compile_state *state)
 {
        if (state->debug & DEBUG_TRIPLES) {
                print_triples(state);
        }
+       /* Replace structures with simpler data types */
+       flatten_structures(state);
+       if (state->debug & DEBUG_TRIPLES) {
+               print_triples(state);
+       }
+       verify_consistency(state);
        /* Analize the intermediate code */
        setup_basic_blocks(state);
        analyze_idominators(state);
        analyze_ipdominators(state);
-       /* Transform the code to ssa form */
+
+       /* Transform the code to ssa form. */
+       /*
+        * The transformation to ssa form puts a phi function
+        * on each of edge of a dominance frontier where that
+        * phi function might be needed.  At -O2 if we don't
+        * eleminate the excess phi functions we can get an
+        * exponential code size growth.  So I kill the extra
+        * phi functions early and I kill them often.
+        */
        transform_to_ssa_form(state);
+       eliminate_inefectual_code(state);
+
+       verify_consistency(state);
+       if (state->debug & DEBUG_CODE_ELIMINATION) {
+               fprintf(stdout, "After transform_to_ssa_form\n");
+               print_blocks(state, stdout);
+       }
        /* Do strength reduction and simple constant optimizations */
        if (state->optimize >= 1) {
                simplify_all(state);
+               transform_from_ssa_form(state);
+               free_basic_blocks(state);
+               setup_basic_blocks(state);
+               analyze_idominators(state);
+               analyze_ipdominators(state);
+               transform_to_ssa_form(state);
+               eliminate_inefectual_code(state);
        }
+       if (state->debug & DEBUG_CODE_ELIMINATION) {
+               fprintf(stdout, "After simplify_all\n");
+               print_blocks(state, stdout);
+       }
+       verify_consistency(state);
        /* Propogate constants throughout the code */
        if (state->optimize >= 2) {
                scc_transform(state);
@@ -11711,31 +14889,48 @@ static void optimize(struct compile_state *state)
                analyze_idominators(state);
                analyze_ipdominators(state);
                transform_to_ssa_form(state);
-               
+               eliminate_inefectual_code(state);
        }
+       verify_consistency(state);
 #warning "WISHLIST implement single use constants (least possible register pressure)"
 #warning "WISHLIST implement induction variable elimination"
-#warning "WISHLIST implement strength reduction"
        /* Select architecture instructions and an initial partial
         * coloring based on architecture constraints.
         */
        transform_to_arch_instructions(state);
+       verify_consistency(state);
        if (state->debug & DEBUG_ARCH_CODE) {
                printf("After transform_to_arch_instructions\n");
-               print_blocks(state);
+               print_blocks(state, stdout);
                print_control_flow(state);
        }
        eliminate_inefectual_code(state);
+       verify_consistency(state);
        if (state->debug & DEBUG_CODE_ELIMINATION) {
                printf("After eliminate_inefectual_code\n");
-               print_blocks(state);
+               print_blocks(state, stdout);
                print_control_flow(state);
        }
+       verify_consistency(state);
        /* Color all of the variables to see if they will fit in registers */
        insert_copies_to_phi(state);
+       if (state->debug & DEBUG_INSERTED_COPIES) {
+               printf("After insert_copies_to_phi\n");
+               print_blocks(state, stdout);
+               print_control_flow(state);
+       }
+       verify_consistency(state);
+       insert_mandatory_copies(state);
+       if (state->debug & DEBUG_INSERTED_COPIES) {
+               printf("After insert_mandatory_copies\n");
+               print_blocks(state, stdout);
+               print_control_flow(state);
+       }
+       verify_consistency(state);
        allocate_registers(state);
+       verify_consistency(state);
        if (state->debug & DEBUG_INTERMEDIATE_CODE) {
-               print_blocks(state);
+               print_blocks(state, stdout);
        }
        if (state->debug & DEBUG_CONTROL_FLOW) {
                print_control_flow(state);
@@ -11746,91 +14941,166 @@ static void optimize(struct compile_state *state)
        free_basic_blocks(state);
 }
 
+static void print_op_asm(struct compile_state *state,
+       struct triple *ins, FILE *fp)
+{
+       struct asm_info *info;
+       const char *ptr;
+       unsigned lhs, rhs, i;
+       info = ins->u.ainfo;
+       lhs = TRIPLE_LHS(ins->sizes);
+       rhs = TRIPLE_RHS(ins->sizes);
+       /* Don't count the clobbers in lhs */
+       for(i = 0; i < lhs; i++) {
+               if (LHS(ins, i)->type == &void_type) {
+                       break;
+               }
+       }
+       lhs = i;
+       fprintf(fp, "#ASM\n");
+       fputc('\t', fp);
+       for(ptr = info->str; *ptr; ptr++) {
+               char *next;
+               unsigned long param;
+               struct triple *piece;
+               if (*ptr != '%') {
+                       fputc(*ptr, fp);
+                       continue;
+               }
+               ptr++;
+               if (*ptr == '%') {
+                       fputc('%', fp);
+                       continue;
+               }
+               param = strtoul(ptr, &next, 10);
+               if (ptr == next) {
+                       error(state, ins, "Invalid asm template");
+               }
+               if (param >= (lhs + rhs)) {
+                       error(state, ins, "Invalid param %%%u in asm template",
+                               param);
+               }
+               piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
+               fprintf(fp, "%s", 
+                       arch_reg_str(ID_REG(piece->id)));
+               ptr = next -1;
+       }
+       fprintf(fp, "\n#NOT ASM\n");
+}
+
+
+/* Only use the low x86 byte registers.  This allows me
+ * allocate the entire register when a byte register is used.
+ */
+#define X86_4_8BIT_GPRS 1
+
+/* Recognized x86 cpu variants */
+#define BAD_CPU      0
+#define CPU_I386     1
+#define CPU_P3       2
+#define CPU_P4       3
+#define CPU_K7       4
+#define CPU_K8       5
+
+#define CPU_DEFAULT  CPU_I386
+
 /* The x86 register classes */
-#define REGC_FLAGS   0
-#define REGC_GPR8    1
-#define REGC_GPR16   2
-#define REGC_GPR32   3
-#define REGC_GPR64   4
-#define REGC_MMX     5
-#define REGC_XMM     6
-#define REGC_GPR32_8 7
-#define REGC_GPR16_8 8
-#define LAST_REGC  REGC_GPR16_8
+#define REGC_FLAGS       0
+#define REGC_GPR8        1
+#define REGC_GPR16       2
+#define REGC_GPR32       3
+#define REGC_DIVIDEND64  4
+#define REGC_DIVIDEND32  5
+#define REGC_MMX         6
+#define REGC_XMM         7
+#define REGC_GPR32_8     8
+#define REGC_GPR16_8     9
+#define REGC_GPR8_LO    10
+#define REGC_IMM32      11
+#define REGC_IMM16      12
+#define REGC_IMM8       13
+#define LAST_REGC  REGC_IMM8
 #if LAST_REGC >= MAX_REGC
 #error "MAX_REGC is to low"
 #endif
 
 /* Register class masks */
-#define REGCM_FLAGS   (1 << REGC_FLAGS)
-#define REGCM_GPR8    (1 << REGC_GPR8)
-#define REGCM_GPR16   (1 << REGC_GPR16)
-#define REGCM_GPR32   (1 << REGC_GPR32)
-#define REGCM_GPR64   (1 << REGC_GPR64)
-#define REGCM_MMX     (1 << REGC_MMX)
-#define REGCM_XMM     (1 << REGC_XMM)
-#define REGCM_GPR32_8 (1 << REGC_GPR32_8)
-#define REGCM_GPR16_8 (1 << REGC_GPR16_8)
+#define REGCM_FLAGS      (1 << REGC_FLAGS)
+#define REGCM_GPR8       (1 << REGC_GPR8)
+#define REGCM_GPR16      (1 << REGC_GPR16)
+#define REGCM_GPR32      (1 << REGC_GPR32)
+#define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
+#define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
+#define REGCM_MMX        (1 << REGC_MMX)
+#define REGCM_XMM        (1 << REGC_XMM)
+#define REGCM_GPR32_8    (1 << REGC_GPR32_8)
+#define REGCM_GPR16_8    (1 << REGC_GPR16_8)
+#define REGCM_GPR8_LO    (1 << REGC_GPR8_LO)
+#define REGCM_IMM32      (1 << REGC_IMM32)
+#define REGCM_IMM16      (1 << REGC_IMM16)
+#define REGCM_IMM8       (1 << REGC_IMM8)
+#define REGCM_ALL        ((1 << (LAST_REGC + 1)) - 1)
 
 /* The x86 registers */
-#define REG_EFLAGS  1
+#define REG_EFLAGS  2
 #define REGC_FLAGS_FIRST REG_EFLAGS
 #define REGC_FLAGS_LAST  REG_EFLAGS
-#define REG_AL      2
-#define REG_BL      3
-#define REG_CL      4
-#define REG_DL      5
-#define REG_AH      6
-#define REG_BH      7
-#define REG_CH      8
-#define REG_DH      9
+#define REG_AL      3
+#define REG_BL      4
+#define REG_CL      5
+#define REG_DL      6
+#define REG_AH      7
+#define REG_BH      8
+#define REG_CH      9
+#define REG_DH      10
+#define REGC_GPR8_LO_FIRST REG_AL
+#define REGC_GPR8_LO_LAST  REG_DL
 #define REGC_GPR8_FIRST  REG_AL
-#if X86_4_8BIT_GPRS
-#define REGC_GPR8_LAST   REG_DL
-#else 
 #define REGC_GPR8_LAST   REG_DH
-#endif
-#define REG_AX     10
-#define REG_BX     11
-#define REG_CX     12
-#define REG_DX     13
-#define REG_SI     14
-#define REG_DI     15
-#define REG_BP     16
-#define REG_SP     17
+#define REG_AX     11
+#define REG_BX     12
+#define REG_CX     13
+#define REG_DX     14
+#define REG_SI     15
+#define REG_DI     16
+#define REG_BP     17
+#define REG_SP     18
 #define REGC_GPR16_FIRST REG_AX
 #define REGC_GPR16_LAST  REG_SP
-#define REG_EAX    18
-#define REG_EBX    19
-#define REG_ECX    20
-#define REG_EDX    21
-#define REG_ESI    22
-#define REG_EDI    23
-#define REG_EBP    24
-#define REG_ESP    25
+#define REG_EAX    19
+#define REG_EBX    20
+#define REG_ECX    21
+#define REG_EDX    22
+#define REG_ESI    23
+#define REG_EDI    24
+#define REG_EBP    25
+#define REG_ESP    26
 #define REGC_GPR32_FIRST REG_EAX
 #define REGC_GPR32_LAST  REG_ESP
-#define REG_EDXEAX 26
-#define REGC_GPR64_FIRST REG_EDXEAX
-#define REGC_GPR64_LAST  REG_EDXEAX
-#define REG_MMX0   27
-#define REG_MMX1   28
-#define REG_MMX2   29
-#define REG_MMX3   30
-#define REG_MMX4   31
-#define REG_MMX5   32
-#define REG_MMX6   33
-#define REG_MMX7   34
+#define REG_EDXEAX 27
+#define REGC_DIVIDEND64_FIRST REG_EDXEAX
+#define REGC_DIVIDEND64_LAST  REG_EDXEAX
+#define REG_DXAX   28
+#define REGC_DIVIDEND32_FIRST REG_DXAX
+#define REGC_DIVIDEND32_LAST  REG_DXAX
+#define REG_MMX0   29
+#define REG_MMX1   30
+#define REG_MMX2   31
+#define REG_MMX3   32
+#define REG_MMX4   33
+#define REG_MMX5   34
+#define REG_MMX6   35
+#define REG_MMX7   36
 #define REGC_MMX_FIRST REG_MMX0
 #define REGC_MMX_LAST  REG_MMX7
-#define REG_XMM0   35
-#define REG_XMM1   36
-#define REG_XMM2   37
-#define REG_XMM3   38
-#define REG_XMM4   39
-#define REG_XMM5   40
-#define REG_XMM6   41
-#define REG_XMM7   42
+#define REG_XMM0   37
+#define REG_XMM1   38
+#define REG_XMM2   39
+#define REG_XMM3   40
+#define REG_XMM4   41
+#define REG_XMM5   42
+#define REG_XMM6   43
+#define REG_XMM7   44
 #define REGC_XMM_FIRST REG_XMM0
 #define REGC_XMM_LAST  REG_XMM7
 #warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
@@ -11841,34 +15111,98 @@ static void optimize(struct compile_state *state)
 #define REGC_GPR16_8_FIRST REG_AX
 #define REGC_GPR16_8_LAST  REG_DX
 
+#define REGC_IMM8_FIRST    -1
+#define REGC_IMM8_LAST     -1
+#define REGC_IMM16_FIRST   -2
+#define REGC_IMM16_LAST    -1
+#define REGC_IMM32_FIRST   -4
+#define REGC_IMM32_LAST    -1
+
 #if LAST_REG >= MAX_REGISTERS
 #error "MAX_REGISTERS to low"
 #endif
 
+
+static unsigned regc_size[LAST_REGC +1] = {
+       [REGC_FLAGS]      = REGC_FLAGS_LAST      - REGC_FLAGS_FIRST + 1,
+       [REGC_GPR8]       = REGC_GPR8_LAST       - REGC_GPR8_FIRST + 1,
+       [REGC_GPR16]      = REGC_GPR16_LAST      - REGC_GPR16_FIRST + 1,
+       [REGC_GPR32]      = REGC_GPR32_LAST      - REGC_GPR32_FIRST + 1,
+       [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
+       [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
+       [REGC_MMX]        = REGC_MMX_LAST        - REGC_MMX_FIRST + 1,
+       [REGC_XMM]        = REGC_XMM_LAST        - REGC_XMM_FIRST + 1,
+       [REGC_GPR32_8]    = REGC_GPR32_8_LAST    - REGC_GPR32_8_FIRST + 1,
+       [REGC_GPR16_8]    = REGC_GPR16_8_LAST    - REGC_GPR16_8_FIRST + 1,
+       [REGC_GPR8_LO]    = REGC_GPR8_LO_LAST    - REGC_GPR8_LO_FIRST + 1,
+       [REGC_IMM32]      = 0,
+       [REGC_IMM16]      = 0,
+       [REGC_IMM8]       = 0,
+};
+
+static const struct {
+       int first, last;
+} regcm_bound[LAST_REGC + 1] = {
+       [REGC_FLAGS]      = { REGC_FLAGS_FIRST,      REGC_FLAGS_LAST },
+       [REGC_GPR8]       = { REGC_GPR8_FIRST,       REGC_GPR8_LAST },
+       [REGC_GPR16]      = { REGC_GPR16_FIRST,      REGC_GPR16_LAST },
+       [REGC_GPR32]      = { REGC_GPR32_FIRST,      REGC_GPR32_LAST },
+       [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
+       [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
+       [REGC_MMX]        = { REGC_MMX_FIRST,        REGC_MMX_LAST },
+       [REGC_XMM]        = { REGC_XMM_FIRST,        REGC_XMM_LAST },
+       [REGC_GPR32_8]    = { REGC_GPR32_8_FIRST,    REGC_GPR32_8_LAST },
+       [REGC_GPR16_8]    = { REGC_GPR16_8_FIRST,    REGC_GPR16_8_LAST },
+       [REGC_GPR8_LO]    = { REGC_GPR8_LO_FIRST,    REGC_GPR8_LO_LAST },
+       [REGC_IMM32]      = { REGC_IMM32_FIRST,      REGC_IMM32_LAST },
+       [REGC_IMM16]      = { REGC_IMM16_FIRST,      REGC_IMM16_LAST },
+       [REGC_IMM8]       = { REGC_IMM8_FIRST,       REGC_IMM8_LAST },
+};
+
+static int arch_encode_cpu(const char *cpu)
+{
+       struct cpu {
+               const char *name;
+               int cpu;
+       } cpus[] = {
+               { "i386", CPU_I386 },
+               { "p3",   CPU_P3 },
+               { "p4",   CPU_P4 },
+               { "k7",   CPU_K7 },
+               { "k8",   CPU_K8 },
+               {  0,     BAD_CPU }
+       };
+       struct cpu *ptr;
+       for(ptr = cpus; ptr->name; ptr++) {
+               if (strcmp(ptr->name, cpu) == 0) {
+                       break;
+               }
+       }
+       return ptr->cpu;
+}
+
 static unsigned arch_regc_size(struct compile_state *state, int class)
 {
-       static unsigned regc_size[LAST_REGC +1] = {
-               [REGC_FLAGS]   = REGC_FLAGS_LAST   - REGC_FLAGS_FIRST + 1,
-               [REGC_GPR8]    = REGC_GPR8_LAST    - REGC_GPR8_FIRST + 1,
-               [REGC_GPR16]   = REGC_GPR16_LAST   - REGC_GPR16_FIRST + 1,
-               [REGC_GPR32]   = REGC_GPR32_LAST   - REGC_GPR32_FIRST + 1,
-               [REGC_GPR64]   = REGC_GPR64_LAST   - REGC_GPR64_FIRST + 1,
-               [REGC_MMX]     = REGC_MMX_LAST     - REGC_MMX_FIRST + 1,
-               [REGC_XMM]     = REGC_XMM_LAST     - REGC_XMM_FIRST + 1,
-               [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
-               [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
-       };
        if ((class < 0) || (class > LAST_REGC)) {
                return 0;
        }
        return regc_size[class];
 }
+
 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
 {
        /* See if two register classes may have overlapping registers */
-       unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 |
-               REGCM_GPR32_8 | REGCM_GPR32 | REGCM_GPR64;
-
+       unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
+               REGCM_GPR32_8 | REGCM_GPR32 | 
+               REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
+
+       /* Special case for the immediates */
+       if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
+               ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
+               (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
+               ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) { 
+               return 0;
+       }
        return (regcm1 & regcm2) ||
                ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
 }
@@ -11882,31 +15216,76 @@ static void arch_reg_equivs(
        *equiv++ = reg;
        switch(reg) {
        case REG_AL:
+#if X86_4_8BIT_GPRS
+               *equiv++ = REG_AH;
+#endif
+               *equiv++ = REG_AX;
+               *equiv++ = REG_EAX;
+               *equiv++ = REG_DXAX;
+               *equiv++ = REG_EDXEAX;
+               break;
        case REG_AH:
+#if X86_4_8BIT_GPRS
+               *equiv++ = REG_AL;
+#endif
                *equiv++ = REG_AX;
                *equiv++ = REG_EAX;
+               *equiv++ = REG_DXAX;
                *equiv++ = REG_EDXEAX;
                break;
        case REG_BL:  
+#if X86_4_8BIT_GPRS
+               *equiv++ = REG_BH;
+#endif
+               *equiv++ = REG_BX;
+               *equiv++ = REG_EBX;
+               break;
+
        case REG_BH:
+#if X86_4_8BIT_GPRS
+               *equiv++ = REG_BL;
+#endif
                *equiv++ = REG_BX;
                *equiv++ = REG_EBX;
                break;
        case REG_CL:
+#if X86_4_8BIT_GPRS
+               *equiv++ = REG_CH;
+#endif
+               *equiv++ = REG_CX;
+               *equiv++ = REG_ECX;
+               break;
+
        case REG_CH:
+#if X86_4_8BIT_GPRS
+               *equiv++ = REG_CL;
+#endif
                *equiv++ = REG_CX;
                *equiv++ = REG_ECX;
                break;
        case REG_DL:
+#if X86_4_8BIT_GPRS
+               *equiv++ = REG_DH;
+#endif
+               *equiv++ = REG_DX;
+               *equiv++ = REG_EDX;
+               *equiv++ = REG_DXAX;
+               *equiv++ = REG_EDXEAX;
+               break;
        case REG_DH:
+#if X86_4_8BIT_GPRS
+               *equiv++ = REG_DL;
+#endif
                *equiv++ = REG_DX;
                *equiv++ = REG_EDX;
+               *equiv++ = REG_DXAX;
                *equiv++ = REG_EDXEAX;
                break;
        case REG_AX:
                *equiv++ = REG_AL;
                *equiv++ = REG_AH;
                *equiv++ = REG_EAX;
+               *equiv++ = REG_DXAX;
                *equiv++ = REG_EDXEAX;
                break;
        case REG_BX:
@@ -11923,6 +15302,7 @@ static void arch_reg_equivs(
                *equiv++ = REG_DL;
                *equiv++ = REG_DH;
                *equiv++ = REG_EDX;
+               *equiv++ = REG_DXAX;
                *equiv++ = REG_EDXEAX;
                break;
        case REG_SI:  
@@ -11941,6 +15321,7 @@ static void arch_reg_equivs(
                *equiv++ = REG_AL;
                *equiv++ = REG_AH;
                *equiv++ = REG_AX;
+               *equiv++ = REG_DXAX;
                *equiv++ = REG_EDXEAX;
                break;
        case REG_EBX:
@@ -11957,6 +15338,7 @@ static void arch_reg_equivs(
                *equiv++ = REG_DL;
                *equiv++ = REG_DH;
                *equiv++ = REG_DX;
+               *equiv++ = REG_DXAX;
                *equiv++ = REG_EDXEAX;
                break;
        case REG_ESI: 
@@ -11971,6 +15353,17 @@ static void arch_reg_equivs(
        case REG_ESP: 
                *equiv++ = REG_SP;
                break;
+       case REG_DXAX: 
+               *equiv++ = REG_AL;
+               *equiv++ = REG_AH;
+               *equiv++ = REG_DL;
+               *equiv++ = REG_DH;
+               *equiv++ = REG_AX;
+               *equiv++ = REG_DX;
+               *equiv++ = REG_EAX;
+               *equiv++ = REG_EDX;
+               *equiv++ = REG_EDXEAX;
+               break;
        case REG_EDXEAX: 
                *equiv++ = REG_AL;
                *equiv++ = REG_AH;
@@ -11980,33 +15373,75 @@ static void arch_reg_equivs(
                *equiv++ = REG_DX;
                *equiv++ = REG_EAX;
                *equiv++ = REG_EDX;
+               *equiv++ = REG_DXAX;
                break;
        }
        *equiv++ = REG_UNSET; 
 }
 
+static unsigned arch_avail_mask(struct compile_state *state)
+{
+       unsigned avail_mask;
+       /* REGCM_GPR8 is not available */
+       avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 | 
+               REGCM_GPR32 | REGCM_GPR32_8 | 
+               REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
+               REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
+       switch(state->cpu) {
+       case CPU_P3:
+       case CPU_K7:
+               avail_mask |= REGCM_MMX;
+               break;
+       case CPU_P4:
+       case CPU_K8:
+               avail_mask |= REGCM_MMX | REGCM_XMM;
+               break;
+       }
+       return avail_mask;
+}
+
+static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
+{
+       unsigned mask, result;
+       int class, class2;
+       result = regcm;
+
+       for(class = 0, mask = 1; mask; mask <<= 1, class++) {
+               if ((result & mask) == 0) {
+                       continue;
+               }
+               if (class > LAST_REGC) {
+                       result &= ~mask;
+               }
+               for(class2 = 0; class2 <= LAST_REGC; class2++) {
+                       if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
+                               (regcm_bound[class2].last <= regcm_bound[class].last)) {
+                               result |= (1 << class2);
+                       }
+               }
+       }
+       result &= arch_avail_mask(state);
+       return result;
+}
+
+static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
+{
+       /* Like arch_regcm_normalize except immediate register classes are excluded */
+       regcm = arch_regcm_normalize(state, regcm);
+       /* Remove the immediate register classes */
+       regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
+       return regcm;
+       
+}
 
 static unsigned arch_reg_regcm(struct compile_state *state, int reg)
 {
-       static const struct {
-               int first, last;
-       } bound[LAST_REGC + 1] = {
-               [REGC_FLAGS]   = { REGC_FLAGS_FIRST,   REGC_FLAGS_LAST },
-               [REGC_GPR8]    = { REGC_GPR8_FIRST,    REGC_GPR8_LAST },
-               [REGC_GPR16]   = { REGC_GPR16_FIRST,   REGC_GPR16_LAST },
-               [REGC_GPR32]   = { REGC_GPR32_FIRST,   REGC_GPR32_LAST },
-               [REGC_GPR64]   = { REGC_GPR64_FIRST,   REGC_GPR64_LAST },
-               [REGC_MMX]     = { REGC_MMX_FIRST,     REGC_MMX_LAST },
-               [REGC_XMM]     = { REGC_XMM_FIRST,     REGC_XMM_LAST },
-               [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
-               [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
-       };
        unsigned mask;
        int class;
        mask = 0;
        for(class = 0; class <= LAST_REGC; class++) {
-               if ((reg >= bound[class].first) &&
-                       (reg <= bound[class].last)) {
+               if ((reg >= regcm_bound[class].first) &&
+                       (reg <= regcm_bound[class].last)) {
                        mask |= (1 << class);
                }
        }
@@ -12016,6 +15451,129 @@ static unsigned arch_reg_regcm(struct compile_state *state, int reg)
        return mask;
 }
 
+static struct reg_info arch_reg_constraint(
+       struct compile_state *state, struct type *type, const char *constraint)
+{
+       static const struct {
+               char class;
+               unsigned int mask;
+               unsigned int reg;
+       } constraints[] = {
+               { 'r', REGCM_GPR32,   REG_UNSET },
+               { 'g', REGCM_GPR32,   REG_UNSET },
+               { 'p', REGCM_GPR32,   REG_UNSET },
+               { 'q', REGCM_GPR8_LO, REG_UNSET },
+               { 'Q', REGCM_GPR32_8, REG_UNSET },
+               { 'x', REGCM_XMM,     REG_UNSET },
+               { 'y', REGCM_MMX,     REG_UNSET },
+               { 'a', REGCM_GPR32,   REG_EAX },
+               { 'b', REGCM_GPR32,   REG_EBX },
+               { 'c', REGCM_GPR32,   REG_ECX },
+               { 'd', REGCM_GPR32,   REG_EDX },
+               { 'D', REGCM_GPR32,   REG_EDI },
+               { 'S', REGCM_GPR32,   REG_ESI },
+               { '\0', 0, REG_UNSET },
+       };
+       unsigned int regcm;
+       unsigned int mask, reg;
+       struct reg_info result;
+       const char *ptr;
+       regcm = arch_type_to_regcm(state, type);
+       reg = REG_UNSET;
+       mask = 0;
+       for(ptr = constraint; *ptr; ptr++) {
+               int i;
+               if (*ptr ==  ' ') {
+                       continue;
+               }
+               for(i = 0; constraints[i].class != '\0'; i++) {
+                       if (constraints[i].class == *ptr) {
+                               break;
+                       }
+               }
+               if (constraints[i].class == '\0') {
+                       error(state, 0, "invalid register constraint ``%c''", *ptr);
+                       break;
+               }
+               if ((constraints[i].mask & regcm) == 0) {
+                       error(state, 0, "invalid register class %c specified",
+                               *ptr);
+               }
+               mask |= constraints[i].mask;
+               if (constraints[i].reg != REG_UNSET) {
+                       if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
+                               error(state, 0, "Only one register may be specified");
+                       }
+                       reg = constraints[i].reg;
+               }
+       }
+       result.reg = reg;
+       result.regcm = mask;
+       return result;
+}
+
+static struct reg_info arch_reg_clobber(
+       struct compile_state *state, const char *clobber)
+{
+       struct reg_info result;
+       if (strcmp(clobber, "memory") == 0) {
+               result.reg = REG_UNSET;
+               result.regcm = 0;
+       }
+       else if (strcmp(clobber, "%eax") == 0) {
+               result.reg = REG_EAX;
+               result.regcm = REGCM_GPR32;
+       }
+       else if (strcmp(clobber, "%ebx") == 0) {
+               result.reg = REG_EBX;
+               result.regcm = REGCM_GPR32;
+       }
+       else if (strcmp(clobber, "%ecx") == 0) {
+               result.reg = REG_ECX;
+               result.regcm = REGCM_GPR32;
+       }
+       else if (strcmp(clobber, "%edx") == 0) {
+               result.reg = REG_EDX;
+               result.regcm = REGCM_GPR32;
+       }
+       else if (strcmp(clobber, "%esi") == 0) {
+               result.reg = REG_ESI;
+               result.regcm = REGCM_GPR32;
+       }
+       else if (strcmp(clobber, "%edi") == 0) {
+               result.reg = REG_EDI;
+               result.regcm = REGCM_GPR32;
+       }
+       else if (strcmp(clobber, "%ebp") == 0) {
+               result.reg = REG_EBP;
+               result.regcm = REGCM_GPR32;
+       }
+       else if (strcmp(clobber, "%esp") == 0) {
+               result.reg = REG_ESP;
+               result.regcm = REGCM_GPR32;
+       }
+       else if (strcmp(clobber, "cc") == 0) {
+               result.reg = REG_EFLAGS;
+               result.regcm = REGCM_FLAGS;
+       }
+       else if ((strncmp(clobber, "xmm", 3) == 0)  &&
+               octdigitp(clobber[3]) && (clobber[4] == '\0')) {
+               result.reg = REG_XMM0 + octdigval(clobber[3]);
+               result.regcm = REGCM_XMM;
+       }
+       else if ((strncmp(clobber, "mmx", 3) == 0) &&
+               octdigitp(clobber[3]) && (clobber[4] == '\0')) {
+               result.reg = REG_MMX0 + octdigval(clobber[3]);
+               result.regcm = REGCM_MMX;
+       }
+       else {
+               error(state, 0, "Invalid register clobber");
+               result.reg = REG_UNSET;
+               result.regcm = 0;
+       }
+       return result;
+}
+
 static int do_select_reg(struct compile_state *state, 
        char *used, int reg, unsigned classes)
 {
@@ -12030,56 +15588,56 @@ static int do_select_reg(struct compile_state *state,
 static int arch_select_free_register(
        struct compile_state *state, char *used, int classes)
 {
-       /* Preference: flags, 8bit gprs, 32bit gprs, other 32bit reg
-        * other types of registers.
+       /* Live ranges with the most neighbors are colored first.
+        *
+        * Generally it does not matter which colors are given
+        * as the register allocator attempts to color live ranges
+        * in an order where you are guaranteed not to run out of colors.
+        *
+        * Occasionally the register allocator cannot find an order
+        * of register selection that will find a free color.  To
+        * increase the odds the register allocator will work when
+        * it guesses first give out registers from register classes
+        * least likely to run out of registers.
+        * 
         */
        int i, reg;
        reg = REG_UNSET;
-       for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
+       for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
                reg = do_select_reg(state, used, i, classes);
        }
-       for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
+       for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
+               reg = do_select_reg(state, used, i, classes);
+       }
+       for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
+               reg = do_select_reg(state, used, i, classes);
+       }
+       for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
                reg = do_select_reg(state, used, i, classes);
        }
-       for(i = REGC_GPR32_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR32_LAST); i++) {
+       for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
                reg = do_select_reg(state, used, i, classes);
        }
-       for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
+       for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
                reg = do_select_reg(state, used, i, classes);
        }
-       for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
+       for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
                reg = do_select_reg(state, used, i, classes);
        }
-       for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
+       for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
                reg = do_select_reg(state, used, i, classes);
        }
-       for(i = REGC_GPR64_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR64_LAST); i++) {
+       for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
                reg = do_select_reg(state, used, i, classes);
        }
        return reg;
 }
 
+
 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type) 
 {
 #warning "FIXME force types smaller (if legal) before I get here"
-       int use_mmx = 0;
-       int use_sse = 0;
-       unsigned avail_mask;
        unsigned mask;
-       avail_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 | 
-               REGCM_GPR32 | REGCM_GPR32_8 | REGCM_GPR64;
-#if 1
-       /* Don't enable 8 bit values until I can force both operands
-        * to be 8bits simultaneously.
-        */
-       avail_mask &= ~(REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16);
-#endif
-       if (use_mmx) {
-               avail_mask |= REGCM_MMX;
-       }
-       if (use_sse) {
-               avail_mask |= REGCM_XMM;
-       }
        mask = 0;
        switch(type->type & TYPE_MASK) {
        case TYPE_ARRAY:
@@ -12088,150 +15646,570 @@ static unsigned arch_type_to_regcm(struct compile_state *state, struct type *typ
                break;
        case TYPE_CHAR:
        case TYPE_UCHAR:
-               mask = REGCM_GPR8 | 
-                       REGCM_GPR16_8 | REGCM_GPR16 | 
+               mask = REGCM_GPR8 | REGCM_GPR8_LO |
+                       REGCM_GPR16 | REGCM_GPR16_8 | 
                        REGCM_GPR32 | REGCM_GPR32_8 |
-                       REGCM_GPR64 |
-                       REGCM_MMX | REGCM_XMM;
+                       REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
+                       REGCM_MMX | REGCM_XMM |
+                       REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
                break;
        case TYPE_SHORT:
        case TYPE_USHORT:
-               mask = REGCM_GPR16 | REGCM_GPR16_8 |
+               mask =  REGCM_GPR16 | REGCM_GPR16_8 |
                        REGCM_GPR32 | REGCM_GPR32_8 |
-                       REGCM_GPR64 |
-                       REGCM_MMX | REGCM_XMM;
+                       REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
+                       REGCM_MMX | REGCM_XMM |
+                       REGCM_IMM32 | REGCM_IMM16;
                break;
        case TYPE_INT:
        case TYPE_UINT:
        case TYPE_LONG:
        case TYPE_ULONG:
        case TYPE_POINTER:
-               mask = REGCM_GPR32 | REGCM_GPR32_8 |
-                       REGCM_GPR64 | REGCM_MMX | REGCM_XMM;
+               mask =  REGCM_GPR32 | REGCM_GPR32_8 |
+                       REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
+                       REGCM_MMX | REGCM_XMM |
+                       REGCM_IMM32;
                break;
        default:
                internal_error(state, 0, "no register class for type");
                break;
        }
-       mask &= avail_mask;
+       mask = arch_regcm_normalize(state, mask);
        return mask;
 }
 
-static void get_imm32(struct triple *ins, struct triple **expr)
+static int is_imm32(struct triple *imm)
 {
-       struct triple *imm;
-       if ((*expr)->op != OP_COPY) {
-               return;
-       }
-       imm = (*expr)->left;
-       while(imm->op == OP_COPY) {
-               imm = imm->left;
-       }
-       if (imm->op != OP_INTCONST) {
-               return;
-       }
-       *expr = imm;
-       unuse_triple(*expr, ins);
-       use_triple(*expr, ins);
+       return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
+               (imm->op == OP_ADDRCONST);
+       
+}
+static int is_imm16(struct triple *imm)
+{
+       return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
+}
+static int is_imm8(struct triple *imm)
+{
+       return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
 }
 
-static void get_imm8(struct triple *ins, struct triple **expr)
+static int get_imm32(struct triple *ins, struct triple **expr)
 {
        struct triple *imm;
-       if ((*expr)->op != OP_COPY) {
-               return;
-       }
-       imm = (*expr)->left;
+       imm = *expr;
        while(imm->op == OP_COPY) {
-               imm = imm->left;
-       }
-       if (imm->op != OP_INTCONST) {
-               return;
+               imm = RHS(imm, 0);
        }
-       /* For imm8 only a sufficienlty small constant can be used */
-       if (imm->u.cval > 0xff) {
-               return;
+       if (!is_imm32(imm)) {
+               return 0;
        }
-       *expr = imm;
        unuse_triple(*expr, ins);
-       use_triple(*expr, ins);
+       use_triple(imm, ins);
+       *expr = imm;
+       return 1;
 }
 
-static struct triple *pre_copy(struct compile_state *state, 
-       struct triple *ins, struct triple **expr,
-       unsigned reg, unsigned mask)
+static int get_imm8(struct triple *ins, struct triple **expr)
 {
-       /* Carefully insert enough operations so that I can
-        * enter any operation with a GPR32.
-        */
-       struct triple *in;
-       /* See if I can directly reach the result from a GPR32 */
-       if (mask & (REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)) {
-               in = triple(state, OP_COPY, (*expr)->type, *expr,  0);
+       struct triple *imm;
+       imm = *expr;
+       while(imm->op == OP_COPY) {
+               imm = RHS(imm, 0);
        }
-       /* If it is a byte value force a earlier copy to a GPR32_8 */
-       else if (mask & REGCM_GPR8) {
-               struct triple *tmp;
-               tmp = triple(state, OP_COPY, (*expr)->type, *expr, 0);
-               tmp->filename = ins->filename;
-               tmp->line     = ins->line;
-               tmp->col      = ins->col;
-               tmp->u.block  = ins->u.block;
-               tmp->id = MK_REG_ID(REG_UNSET, REGCM_GPR32_8 | REGCM_GPR16_8);
-               use_triple(tmp->left, tmp);
-               insert_triple(state, ins, tmp);
-
-               in = triple(state, OP_COPY, tmp->type, tmp, 0);
+       if (!is_imm8(imm)) {
+               return 0;
        }
-       else {
-               internal_error(state, ins, "bad copy type");
-               in = 0;
-       }
-       in->filename  = ins->filename;
-       in->line      = ins->line;
-       in->col       = ins->col;
-       in->u.block   = ins->u.block;
-       in->id        = MK_REG_ID(reg, mask);
        unuse_triple(*expr, ins);
-       *expr = in;
-       use_triple(in->left, in);
-       use_triple(in, ins);
-       insert_triple(state, ins, in);
-       return in;
+       use_triple(imm, ins);
+       *expr = imm;
+       return 1;
 }
 
-static struct triple *post_copy(struct compile_state *state, struct triple *ins)
-{
-       struct triple_set *entry, *next;
-       struct triple *out, *label;
-       struct block *block;
-       label = ins;
-       while(label->op != OP_LABEL) {
-               label = label->prev;
-       }
-       block = label->u.block;
-       out = triple(state, OP_COPY, ins->type, ins, 0);
-       out->filename = ins->filename;
-       out->line     = ins->line;
-       out->col      = ins->col;
-       out->u.block  = block;
-       out->id       = MK_REG_ID(REG_UNSET, 
-               arch_type_to_regcm(state, ins->type));
-       use_triple(ins, out);
-       insert_triple(state, ins->next, out);
-       if (block->last == ins) {
-               block->last = out;
-       }
-       /* Get the users of ins to use out instead */
-       for(entry = ins->use; entry; entry = next) {
-               next = entry->next;
-               if (entry->member == out) {
-                       continue;
-               }
-               replace_rhs_use(state, ins, out, entry->member);
-       }
-       return out;
-}
+#define TEMPLATE_NOP           0
+#define TEMPLATE_INTCONST8     1
+#define TEMPLATE_INTCONST32    2
+#define TEMPLATE_COPY8_REG     3
+#define TEMPLATE_COPY16_REG    4
+#define TEMPLATE_COPY32_REG    5
+#define TEMPLATE_COPY_IMM8     6
+#define TEMPLATE_COPY_IMM16    7
+#define TEMPLATE_COPY_IMM32    8
+#define TEMPLATE_PHI8          9
+#define TEMPLATE_PHI16        10
+#define TEMPLATE_PHI32        11
+#define TEMPLATE_STORE8       12
+#define TEMPLATE_STORE16      13
+#define TEMPLATE_STORE32      14
+#define TEMPLATE_LOAD8        15
+#define TEMPLATE_LOAD16       16
+#define TEMPLATE_LOAD32       17
+#define TEMPLATE_BINARY8_REG  18
+#define TEMPLATE_BINARY16_REG 19
+#define TEMPLATE_BINARY32_REG 20
+#define TEMPLATE_BINARY8_IMM  21
+#define TEMPLATE_BINARY16_IMM 22
+#define TEMPLATE_BINARY32_IMM 23
+#define TEMPLATE_SL8_CL       24
+#define TEMPLATE_SL16_CL      25
+#define TEMPLATE_SL32_CL      26
+#define TEMPLATE_SL8_IMM      27
+#define TEMPLATE_SL16_IMM     28
+#define TEMPLATE_SL32_IMM     29
+#define TEMPLATE_UNARY8       30
+#define TEMPLATE_UNARY16      31
+#define TEMPLATE_UNARY32      32
+#define TEMPLATE_CMP8_REG     33
+#define TEMPLATE_CMP16_REG    34
+#define TEMPLATE_CMP32_REG    35
+#define TEMPLATE_CMP8_IMM     36
+#define TEMPLATE_CMP16_IMM    37
+#define TEMPLATE_CMP32_IMM    38
+#define TEMPLATE_TEST8        39
+#define TEMPLATE_TEST16       40
+#define TEMPLATE_TEST32       41
+#define TEMPLATE_SET          42
+#define TEMPLATE_JMP          43
+#define TEMPLATE_INB_DX       44
+#define TEMPLATE_INB_IMM      45
+#define TEMPLATE_INW_DX       46
+#define TEMPLATE_INW_IMM      47
+#define TEMPLATE_INL_DX       48
+#define TEMPLATE_INL_IMM      49
+#define TEMPLATE_OUTB_DX      50
+#define TEMPLATE_OUTB_IMM     51
+#define TEMPLATE_OUTW_DX      52
+#define TEMPLATE_OUTW_IMM     53
+#define TEMPLATE_OUTL_DX      54
+#define TEMPLATE_OUTL_IMM     55
+#define TEMPLATE_BSF          56
+#define TEMPLATE_RDMSR        57
+#define TEMPLATE_WRMSR        58
+#define TEMPLATE_UMUL8        59
+#define TEMPLATE_UMUL16       60
+#define TEMPLATE_UMUL32       61
+#define TEMPLATE_DIV8         62
+#define TEMPLATE_DIV16        63
+#define TEMPLATE_DIV32        64
+#define LAST_TEMPLATE       TEMPLATE_DIV32
+#if LAST_TEMPLATE >= MAX_TEMPLATES
+#error "MAX_TEMPLATES to low"
+#endif
+
+#define COPY8_REGCM     (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO | REGCM_MMX | REGCM_XMM)
+#define COPY16_REGCM    (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)  
+#define COPY32_REGCM    (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
+
+
+static struct ins_template templates[] = {
+       [TEMPLATE_NOP]      = {},
+       [TEMPLATE_INTCONST8] = { 
+               .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
+       },
+       [TEMPLATE_INTCONST32] = { 
+               .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
+       },
+       [TEMPLATE_COPY8_REG] = {
+               .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
+               .rhs = { [0] = { REG_UNSET, COPY8_REGCM }  },
+       },
+       [TEMPLATE_COPY16_REG] = {
+               .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
+               .rhs = { [0] = { REG_UNSET, COPY16_REGCM }  },
+       },
+       [TEMPLATE_COPY32_REG] = {
+               .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
+               .rhs = { [0] = { REG_UNSET, COPY32_REGCM }  },
+       },
+       [TEMPLATE_COPY_IMM8] = {
+               .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
+               .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
+       },
+       [TEMPLATE_COPY_IMM16] = {
+               .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
+               .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
+       },
+       [TEMPLATE_COPY_IMM32] = {
+               .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
+               .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
+       },
+       [TEMPLATE_PHI8] = { 
+               .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
+               .rhs = { 
+                       [ 0] = { REG_VIRT0, COPY8_REGCM },
+                       [ 1] = { REG_VIRT0, COPY8_REGCM },
+                       [ 2] = { REG_VIRT0, COPY8_REGCM },
+                       [ 3] = { REG_VIRT0, COPY8_REGCM },
+                       [ 4] = { REG_VIRT0, COPY8_REGCM },
+                       [ 5] = { REG_VIRT0, COPY8_REGCM },
+                       [ 6] = { REG_VIRT0, COPY8_REGCM },
+                       [ 7] = { REG_VIRT0, COPY8_REGCM },
+                       [ 8] = { REG_VIRT0, COPY8_REGCM },
+                       [ 9] = { REG_VIRT0, COPY8_REGCM },
+                       [10] = { REG_VIRT0, COPY8_REGCM },
+                       [11] = { REG_VIRT0, COPY8_REGCM },
+                       [12] = { REG_VIRT0, COPY8_REGCM },
+                       [13] = { REG_VIRT0, COPY8_REGCM },
+                       [14] = { REG_VIRT0, COPY8_REGCM },
+                       [15] = { REG_VIRT0, COPY8_REGCM },
+               }, },
+       [TEMPLATE_PHI16] = { 
+               .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
+               .rhs = { 
+                       [ 0] = { REG_VIRT0, COPY16_REGCM },
+                       [ 1] = { REG_VIRT0, COPY16_REGCM },
+                       [ 2] = { REG_VIRT0, COPY16_REGCM },
+                       [ 3] = { REG_VIRT0, COPY16_REGCM },
+                       [ 4] = { REG_VIRT0, COPY16_REGCM },
+                       [ 5] = { REG_VIRT0, COPY16_REGCM },
+                       [ 6] = { REG_VIRT0, COPY16_REGCM },
+                       [ 7] = { REG_VIRT0, COPY16_REGCM },
+                       [ 8] = { REG_VIRT0, COPY16_REGCM },
+                       [ 9] = { REG_VIRT0, COPY16_REGCM },
+                       [10] = { REG_VIRT0, COPY16_REGCM },
+                       [11] = { REG_VIRT0, COPY16_REGCM },
+                       [12] = { REG_VIRT0, COPY16_REGCM },
+                       [13] = { REG_VIRT0, COPY16_REGCM },
+                       [14] = { REG_VIRT0, COPY16_REGCM },
+                       [15] = { REG_VIRT0, COPY16_REGCM },
+               }, },
+       [TEMPLATE_PHI32] = { 
+               .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
+               .rhs = { 
+                       [ 0] = { REG_VIRT0, COPY32_REGCM },
+                       [ 1] = { REG_VIRT0, COPY32_REGCM },
+                       [ 2] = { REG_VIRT0, COPY32_REGCM },
+                       [ 3] = { REG_VIRT0, COPY32_REGCM },
+                       [ 4] = { REG_VIRT0, COPY32_REGCM },
+                       [ 5] = { REG_VIRT0, COPY32_REGCM },
+                       [ 6] = { REG_VIRT0, COPY32_REGCM },
+                       [ 7] = { REG_VIRT0, COPY32_REGCM },
+                       [ 8] = { REG_VIRT0, COPY32_REGCM },
+                       [ 9] = { REG_VIRT0, COPY32_REGCM },
+                       [10] = { REG_VIRT0, COPY32_REGCM },
+                       [11] = { REG_VIRT0, COPY32_REGCM },
+                       [12] = { REG_VIRT0, COPY32_REGCM },
+                       [13] = { REG_VIRT0, COPY32_REGCM },
+                       [14] = { REG_VIRT0, COPY32_REGCM },
+                       [15] = { REG_VIRT0, COPY32_REGCM },
+               }, },
+       [TEMPLATE_STORE8] = {
+               .rhs = { 
+                       [0] = { REG_UNSET, REGCM_GPR32 },
+                       [1] = { REG_UNSET, REGCM_GPR8_LO },
+               },
+       },
+       [TEMPLATE_STORE16] = {
+               .rhs = { 
+                       [0] = { REG_UNSET, REGCM_GPR32 },
+                       [1] = { REG_UNSET, REGCM_GPR16 },
+               },
+       },
+       [TEMPLATE_STORE32] = {
+               .rhs = { 
+                       [0] = { REG_UNSET, REGCM_GPR32 },
+                       [1] = { REG_UNSET, REGCM_GPR32 },
+               },
+       },
+       [TEMPLATE_LOAD8] = {
+               .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
+               .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
+       },
+       [TEMPLATE_LOAD16] = {
+               .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
+               .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
+       },
+       [TEMPLATE_LOAD32] = {
+               .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
+               .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
+       },
+       [TEMPLATE_BINARY8_REG] = {
+               .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
+               .rhs = { 
+                       [0] = { REG_VIRT0, REGCM_GPR8_LO },
+                       [1] = { REG_UNSET, REGCM_GPR8_LO },
+               },
+       },
+       [TEMPLATE_BINARY16_REG] = {
+               .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
+               .rhs = { 
+                       [0] = { REG_VIRT0, REGCM_GPR16 },
+                       [1] = { REG_UNSET, REGCM_GPR16 },
+               },
+       },
+       [TEMPLATE_BINARY32_REG] = {
+               .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
+               .rhs = { 
+                       [0] = { REG_VIRT0, REGCM_GPR32 },
+                       [1] = { REG_UNSET, REGCM_GPR32 },
+               },
+       },
+       [TEMPLATE_BINARY8_IMM] = {
+               .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
+               .rhs = { 
+                       [0] = { REG_VIRT0,    REGCM_GPR8_LO },
+                       [1] = { REG_UNNEEDED, REGCM_IMM8 },
+               },
+       },
+       [TEMPLATE_BINARY16_IMM] = {
+               .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
+               .rhs = { 
+                       [0] = { REG_VIRT0,    REGCM_GPR16 },
+                       [1] = { REG_UNNEEDED, REGCM_IMM16 },
+               },
+       },
+       [TEMPLATE_BINARY32_IMM] = {
+               .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
+               .rhs = { 
+                       [0] = { REG_VIRT0,    REGCM_GPR32 },
+                       [1] = { REG_UNNEEDED, REGCM_IMM32 },
+               },
+       },
+       [TEMPLATE_SL8_CL] = {
+               .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
+               .rhs = { 
+                       [0] = { REG_VIRT0, REGCM_GPR8_LO },
+                       [1] = { REG_CL, REGCM_GPR8_LO },
+               },
+       },
+       [TEMPLATE_SL16_CL] = {
+               .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
+               .rhs = { 
+                       [0] = { REG_VIRT0, REGCM_GPR16 },
+                       [1] = { REG_CL, REGCM_GPR8_LO },
+               },
+       },
+       [TEMPLATE_SL32_CL] = {
+               .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
+               .rhs = { 
+                       [0] = { REG_VIRT0, REGCM_GPR32 },
+                       [1] = { REG_CL, REGCM_GPR8_LO },
+               },
+       },
+       [TEMPLATE_SL8_IMM] = {
+               .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
+               .rhs = { 
+                       [0] = { REG_VIRT0,    REGCM_GPR8_LO },
+                       [1] = { REG_UNNEEDED, REGCM_IMM8 },
+               },
+       },
+       [TEMPLATE_SL16_IMM] = {
+               .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
+               .rhs = { 
+                       [0] = { REG_VIRT0,    REGCM_GPR16 },
+                       [1] = { REG_UNNEEDED, REGCM_IMM8 },
+               },
+       },
+       [TEMPLATE_SL32_IMM] = {
+               .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
+               .rhs = { 
+                       [0] = { REG_VIRT0,    REGCM_GPR32 },
+                       [1] = { REG_UNNEEDED, REGCM_IMM8 },
+               },
+       },
+       [TEMPLATE_UNARY8] = {
+               .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
+               .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
+       },
+       [TEMPLATE_UNARY16] = {
+               .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
+               .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
+       },
+       [TEMPLATE_UNARY32] = {
+               .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
+               .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
+       },
+       [TEMPLATE_CMP8_REG] = {
+               .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
+               .rhs = {
+                       [0] = { REG_UNSET, REGCM_GPR8_LO },
+                       [1] = { REG_UNSET, REGCM_GPR8_LO },
+               },
+       },
+       [TEMPLATE_CMP16_REG] = {
+               .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
+               .rhs = {
+                       [0] = { REG_UNSET, REGCM_GPR16 },
+                       [1] = { REG_UNSET, REGCM_GPR16 },
+               },
+       },
+       [TEMPLATE_CMP32_REG] = {
+               .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
+               .rhs = {
+                       [0] = { REG_UNSET, REGCM_GPR32 },
+                       [1] = { REG_UNSET, REGCM_GPR32 },
+               },
+       },
+       [TEMPLATE_CMP8_IMM] = {
+               .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
+               .rhs = {
+                       [0] = { REG_UNSET, REGCM_GPR8_LO },
+                       [1] = { REG_UNNEEDED, REGCM_IMM8 },
+               },
+       },
+       [TEMPLATE_CMP16_IMM] = {
+               .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
+               .rhs = {
+                       [0] = { REG_UNSET, REGCM_GPR16 },
+                       [1] = { REG_UNNEEDED, REGCM_IMM16 },
+               },
+       },
+       [TEMPLATE_CMP32_IMM] = {
+               .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
+               .rhs = {
+                       [0] = { REG_UNSET, REGCM_GPR32 },
+                       [1] = { REG_UNNEEDED, REGCM_IMM32 },
+               },
+       },
+       [TEMPLATE_TEST8] = {
+               .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
+               .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
+       },
+       [TEMPLATE_TEST16] = {
+               .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
+               .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
+       },
+       [TEMPLATE_TEST32] = {
+               .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
+               .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
+       },
+       [TEMPLATE_SET] = {
+               .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
+               .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
+       },
+       [TEMPLATE_JMP] = {
+               .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
+       },
+       [TEMPLATE_INB_DX] = {
+               .lhs = { [0] = { REG_AL,  REGCM_GPR8_LO } },  
+               .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
+       },
+       [TEMPLATE_INB_IMM] = {
+               .lhs = { [0] = { REG_AL,  REGCM_GPR8_LO } },  
+               .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
+       },
+       [TEMPLATE_INW_DX]  = { 
+               .lhs = { [0] = { REG_AX,  REGCM_GPR16 } }, 
+               .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
+       },
+       [TEMPLATE_INW_IMM] = { 
+               .lhs = { [0] = { REG_AX,  REGCM_GPR16 } }, 
+               .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
+       },
+       [TEMPLATE_INL_DX]  = {
+               .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
+               .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
+       },
+       [TEMPLATE_INL_IMM] = {
+               .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
+               .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
+       },
+       [TEMPLATE_OUTB_DX] = { 
+               .rhs = {
+                       [0] = { REG_AL,  REGCM_GPR8_LO },
+                       [1] = { REG_DX, REGCM_GPR16 },
+               },
+       },
+       [TEMPLATE_OUTB_IMM] = { 
+               .rhs = {
+                       [0] = { REG_AL,  REGCM_GPR8_LO },  
+                       [1] = { REG_UNNEEDED, REGCM_IMM8 },
+               },
+       },
+       [TEMPLATE_OUTW_DX] = { 
+               .rhs = {
+                       [0] = { REG_AX,  REGCM_GPR16 },
+                       [1] = { REG_DX, REGCM_GPR16 },
+               },
+       },
+       [TEMPLATE_OUTW_IMM] = {
+               .rhs = {
+                       [0] = { REG_AX,  REGCM_GPR16 }, 
+                       [1] = { REG_UNNEEDED, REGCM_IMM8 },
+               },
+       },
+       [TEMPLATE_OUTL_DX] = { 
+               .rhs = {
+                       [0] = { REG_EAX, REGCM_GPR32 },
+                       [1] = { REG_DX, REGCM_GPR16 },
+               },
+       },
+       [TEMPLATE_OUTL_IMM] = { 
+               .rhs = {
+                       [0] = { REG_EAX, REGCM_GPR32 }, 
+                       [1] = { REG_UNNEEDED, REGCM_IMM8 },
+               },
+       },
+       [TEMPLATE_BSF] = {
+               .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
+               .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
+       },
+       [TEMPLATE_RDMSR] = {
+               .lhs = { 
+                       [0] = { REG_EAX, REGCM_GPR32 },
+                       [1] = { REG_EDX, REGCM_GPR32 },
+               },
+               .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
+       },
+       [TEMPLATE_WRMSR] = {
+               .rhs = {
+                       [0] = { REG_ECX, REGCM_GPR32 },
+                       [1] = { REG_EAX, REGCM_GPR32 },
+                       [2] = { REG_EDX, REGCM_GPR32 },
+               },
+       },
+       [TEMPLATE_UMUL8] = {
+               .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
+               .rhs = { 
+                       [0] = { REG_AL, REGCM_GPR8_LO },
+                       [1] = { REG_UNSET, REGCM_GPR8_LO },
+               },
+       },
+       [TEMPLATE_UMUL16] = {
+               .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
+               .rhs = { 
+                       [0] = { REG_AX, REGCM_GPR16 },
+                       [1] = { REG_UNSET, REGCM_GPR16 },
+               },
+       },
+       [TEMPLATE_UMUL32] = {
+               .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
+               .rhs = { 
+                       [0] = { REG_EAX, REGCM_GPR32 },
+                       [1] = { REG_UNSET, REGCM_GPR32 },
+               },
+       },
+       [TEMPLATE_DIV8] = {
+               .lhs = { 
+                       [0] = { REG_AL, REGCM_GPR8_LO },
+                       [1] = { REG_AH, REGCM_GPR8 },
+               },
+               .rhs = {
+                       [0] = { REG_AX, REGCM_GPR16 },
+                       [1] = { REG_UNSET, REGCM_GPR8_LO },
+               },
+       },
+       [TEMPLATE_DIV16] = {
+               .lhs = { 
+                       [0] = { REG_AX, REGCM_GPR16 },
+                       [1] = { REG_DX, REGCM_GPR16 },
+               },
+               .rhs = {
+                       [0] = { REG_DXAX, REGCM_DIVIDEND32 },
+                       [1] = { REG_UNSET, REGCM_GPR16 },
+               },
+       },
+       [TEMPLATE_DIV32] = {
+               .lhs = { 
+                       [0] = { REG_EAX, REGCM_GPR32 },
+                       [1] = { REG_EDX, REGCM_GPR32 },
+               },
+               .rhs = {
+                       [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
+                       [1] = { REG_UNSET, REGCM_GPR32 },
+               },
+       },
+};
 
 static void fixup_branches(struct compile_state *state,
        struct triple *cmp, struct triple *use, int jmp_op)
@@ -12244,14 +16222,29 @@ static void fixup_branches(struct compile_state *state,
                }
                else if (entry->member->op == OP_BRANCH) {
                        struct triple *branch, *test;
+                       struct triple *left, *right;
+                       left = right = 0;
+                       left = RHS(cmp, 0);
+                       if (TRIPLE_RHS(cmp->sizes) > 1) {
+                               right = RHS(cmp, 1);
+                       }
                        branch = entry->member;
                        test = pre_triple(state, branch,
-                               cmp->op, cmp->type, cmp->left, cmp->right);
-                       test->id = MK_REG_ID(REG_EFLAGS, REGCM_FLAGS);
-                       unuse_triple(branch->right, branch);
-                       branch->right = test;
+                               cmp->op, cmp->type, left, right);
+                       test->template_id = TEMPLATE_TEST32; 
+                       if (cmp->op == OP_CMP) {
+                               test->template_id = TEMPLATE_CMP32_REG;
+                               if (get_imm32(test, &RHS(test, 1))) {
+                                       test->template_id = TEMPLATE_CMP32_IMM;
+                               }
+                       }
+                       use_triple(RHS(test, 0), test);
+                       use_triple(RHS(test, 1), test);
+                       unuse_triple(RHS(branch, 0), branch);
+                       RHS(branch, 0) = test;
                        branch->op = jmp_op;
-                       use_triple(branch->right, branch);
+                       branch->template_id = TEMPLATE_JMP;
+                       use_triple(RHS(branch, 0), branch);
                }
        }
 }
@@ -12259,13 +16252,8 @@ static void fixup_branches(struct compile_state *state,
 static void bool_cmp(struct compile_state *state, 
        struct triple *ins, int cmp_op, int jmp_op, int set_op)
 {
-       struct block *block;
        struct triple_set *entry, *next;
-       struct triple *set, *tmp1, *tmp2;
-
-#warning "WISHLIST implement an expression simplifier to reduce the use of set?"
-
-       block = ins->u.block;
+       struct triple *set;
 
        /* Put a barrier up before the cmp which preceeds the
         * copy instruction.  If a set actually occurs this gives
@@ -12274,40 +16262,23 @@ static void bool_cmp(struct compile_state *state,
 
        /* Modify the comparison operator */
        ins->op = cmp_op;
-       ins->id = MK_REG_ID(REG_EFLAGS, REGCM_FLAGS);
+       ins->template_id = TEMPLATE_TEST32;
        if (cmp_op == OP_CMP) {
-               get_imm32(ins, &ins->right);
+               ins->template_id = TEMPLATE_CMP32_REG;
+               if (get_imm32(ins, &RHS(ins, 1))) {
+                       ins->template_id =  TEMPLATE_CMP32_IMM;
+               }
        }
        /* Generate the instruction sequence that will transform the
         * result of the comparison into a logical value.
         */
-       tmp1 = triple(state, set_op, ins->type, ins, 0);
-       tmp1->filename = ins->filename;
-       tmp1->line     = ins->line;
-       tmp1->col      = ins->col;
-       tmp1->u.block  = block;
-       tmp1->id       = MK_REG_ID(REG_UNSET, REGCM_GPR8);
-       use_triple(ins, tmp1);
-       insert_triple(state, ins->next, tmp1);
-       
-       tmp2 = triple(state, OP_COPY, ins->type, tmp1, 0);
-       tmp2->filename = ins->filename;
-       tmp2->line     = ins->line;
-       tmp2->col      = ins->col;
-       tmp2->u.block  = block;
-       tmp2->id       = MK_REG_ID(REG_UNSET, 
-               REGCM_GPR32 | REGCM_GPR32_8 | REGCM_GPR16 | REGCM_GPR16_8 | REGCM_GPR8);
-       use_triple(tmp1, tmp2);
-       insert_triple(state, tmp1->next, tmp2);
-
-       if (block->last == ins) {
-               block->last = tmp2;
-       }
+       set = post_triple(state, ins, set_op, &char_type, ins, 0);
+       use_triple(ins, set);
+       set->template_id = TEMPLATE_SET;
 
-       set = tmp2;
        for(entry = ins->use; entry; entry = next) {
                next = entry->next;
-               if (entry->member == tmp1) {
+               if (entry->member == set) {
                        continue;
                }
                replace_rhs_use(state, ins, set, entry->member);
@@ -12315,242 +16286,427 @@ static void bool_cmp(struct compile_state *state,
        fixup_branches(state, ins, set, jmp_op);
 }
 
+static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
+{
+       struct triple *next;
+       int lhs, i;
+       lhs = TRIPLE_LHS(ins->sizes);
+       for(next = ins->next, i = 0; i < lhs; i++, next = next->next) {
+               if (next != LHS(ins, i)) {
+                       internal_error(state, ins, "malformed lhs on %s",
+                               tops(ins->op));
+               }
+               if (next->op != OP_PIECE) {
+                       internal_error(state, ins, "bad lhs op %s at %d on %s",
+                               tops(next->op), i, tops(ins->op));
+               }
+               if (next->u.cval != i) {
+                       internal_error(state, ins, "bad u.cval of %d %d expected",
+                               next->u.cval, i);
+               }
+       }
+       return next;
+}
 
-static void transform_to_arch_instructions(struct compile_state *state)
+struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
+{
+       struct ins_template *template;
+       struct reg_info result;
+       int zlhs;
+       if (ins->op == OP_PIECE) {
+               index = ins->u.cval;
+               ins = MISC(ins, 0);
+       }
+       zlhs = TRIPLE_LHS(ins->sizes);
+       if (triple_is_def(state, ins)) {
+               zlhs = 1;
+       }
+       if (index >= zlhs) {
+               internal_error(state, ins, "index %d out of range for %s\n",
+                       index, tops(ins->op));
+       }
+       switch(ins->op) {
+       case OP_ASM:
+               template = &ins->u.ainfo->tmpl;
+               break;
+       default:
+               if (ins->template_id > LAST_TEMPLATE) {
+                       internal_error(state, ins, "bad template number %d", 
+                               ins->template_id);
+               }
+               template = &templates[ins->template_id];
+               break;
+       }
+       result = template->lhs[index];
+       result.regcm = arch_regcm_normalize(state, result.regcm);
+       if (result.reg != REG_UNNEEDED) {
+               result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
+       }
+       if (result.regcm == 0) {
+               internal_error(state, ins, "lhs %d regcm == 0", index);
+       }
+       return result;
+}
+
+struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
+{
+       struct reg_info result;
+       struct ins_template *template;
+       if ((index > TRIPLE_RHS(ins->sizes)) ||
+               (ins->op == OP_PIECE)) {
+               internal_error(state, ins, "index %d out of range for %s\n",
+                       index, tops(ins->op));
+       }
+       switch(ins->op) {
+       case OP_ASM:
+               template = &ins->u.ainfo->tmpl;
+               break;
+       default:
+               if (ins->template_id > LAST_TEMPLATE) {
+                       internal_error(state, ins, "bad template number %d", 
+                               ins->template_id);
+               }
+               template = &templates[ins->template_id];
+               break;
+       }
+       result = template->rhs[index];
+       result.regcm = arch_regcm_normalize(state, result.regcm);
+       if (result.regcm == 0) {
+               internal_error(state, ins, "rhs %d regcm == 0", index);
+       }
+       return result;
+}
+
+static struct triple *mod_div(struct compile_state *state,
+       struct triple *ins, int div_op, int index)
+{
+       struct triple *div, *piece0, *piece1;
+       
+       /* Generate a piece to hold the remainder */
+       piece1 = post_triple(state, ins, OP_PIECE, ins->type, 0, 0);
+       piece1->u.cval = 1;
+
+       /* Generate a piece to hold the quotient */
+       piece0 = post_triple(state, ins, OP_PIECE, ins->type, 0, 0);
+       piece0->u.cval = 0;
+
+       /* Generate the appropriate division instruction */
+       div = post_triple(state, ins, div_op, ins->type, 0, 0);
+       RHS(div, 0) = RHS(ins, 0);
+       RHS(div, 1) = RHS(ins, 1);
+       LHS(div, 0) = piece0;
+       LHS(div, 1) = piece1;
+       div->template_id  = TEMPLATE_DIV32;
+       use_triple(RHS(div, 0), div);
+       use_triple(RHS(div, 1), div);
+       use_triple(LHS(div, 0), div);
+       use_triple(LHS(div, 1), div);
+
+       /* Hook on piece0 */
+       MISC(piece0, 0) = div;
+       use_triple(div, piece0);
+
+       /* Hook on piece1 */
+       MISC(piece1, 0) = div;
+       use_triple(div, piece1);
+       
+       /* Replate uses of ins with the appropriate piece of the div */
+       propogate_use(state, ins, LHS(div, index));
+       release_triple(state, ins);
+
+       /* Return the address of the next instruction */
+       return piece1->next;
+}
+
+static struct triple *transform_to_arch_instruction(
+       struct compile_state *state, struct triple *ins)
 {
        /* Transform from generic 3 address instructions
         * to archtecture specific instructions.
-        * And apply architecture specific constrains to instructions.
+        * And apply architecture specific constraints to instructions.
         * Copies are inserted to preserve the register flexibility
         * of 3 address instructions.
         */
-       struct triple *ins, *first, *next;
-       struct triple *in, *in2;
-       first = state->main_function->left;
-       ins = first;
-       do {
-               next = ins->next;
-               ins->id = MK_REG_ID(REG_UNSET, arch_type_to_regcm(state, ins->type));
-               switch(ins->op) {
-               case OP_INTCONST:
-               case OP_ADDRCONST:
-                       ins->id = 0;
-                       post_copy(state, ins);
-                       break;
-               case OP_NOOP:
-               case OP_SDECL:
-               case OP_BLOBCONST:
-               case OP_LABEL:
-                       ins->id = 0;
-                       break;
-                       /* instructions that can be used as is */
-               case OP_COPY:
-               case OP_PHI:
-                       break;
-               case OP_STORE:
-               {
-                       unsigned mask;
-                       ins->id = 0;
-                       switch(ins->type->type & TYPE_MASK) {
-                       case TYPE_CHAR:    case TYPE_UCHAR:
-                               mask = REGCM_GPR8;
-                               break;
-                       case TYPE_SHORT:   case TYPE_USHORT:
-                               mask = REGCM_GPR16;
-                               break;
-                       case TYPE_INT:     case TYPE_UINT:
-                       case TYPE_LONG:    case TYPE_ULONG:
-                       case TYPE_POINTER:
-                               mask  = REGCM_GPR32;
-                               break;
-                       default:
-                               internal_error(state, ins, "unknown type in store");
-                               mask = 0;
-                               break;
-                       }
-                       in = pre_copy(state, ins, &ins->right, REG_UNSET, mask);
-                       break;
+       struct triple *next;
+       size_t size;
+       next = ins->next;
+       switch(ins->op) {
+       case OP_INTCONST:
+               ins->template_id = TEMPLATE_INTCONST32;
+               if (ins->u.cval < 256) {
+                       ins->template_id = TEMPLATE_INTCONST8;
                }
-               case OP_LOAD:
-                       switch(ins->type->type & TYPE_MASK) {
-                       case TYPE_CHAR:   case TYPE_UCHAR:
-                               ins->id = MK_REG_ID(REG_UNSET, REGCM_GPR8);
-                               break;
-                       case TYPE_SHORT:
-                       case TYPE_USHORT:
-                               ins->id = MK_REG_ID(REG_UNSET, REGCM_GPR16);
-                               break;
-                       case TYPE_INT:
-                       case TYPE_UINT:
-                       case TYPE_LONG:
-                       case TYPE_ULONG:
-                       case TYPE_POINTER:
-                               ins->id = MK_REG_ID(REG_UNSET, REGCM_GPR32);
-                               break;
-                       default:
-                               internal_error(state, ins, "unknown type in load");
-                               break;
-                       }
-                       break;
-               case OP_ADD:
-               case OP_SUB:
-               case OP_AND:
-               case OP_XOR:
-               case OP_OR:
-                       get_imm32(ins, &ins->right);
-                       in = pre_copy(state, ins, &ins->left,
-                               alloc_virtual_reg(), ID_REG_CLASSES(ins->id));
-                       ins->id = in->id;
-                       break;
-               case OP_SL:
-               case OP_SSR:
-               case OP_USR:
-                       get_imm8(ins, &ins->right);
-                       in = pre_copy(state, ins, &ins->left,
-                               alloc_virtual_reg(), ID_REG_CLASSES(ins->id));
-                       ins->id = in->id;
-                       if (!IS_CONST_OP(ins->right->op)) {
-                               in2 = pre_copy(state, ins, &ins->right,
-                                       REG_CL, REGCM_GPR8);
-                       }
-                       break;
-               case OP_INVERT:
-               case OP_NEG:
-                       in = pre_copy(state, ins, &ins->left,
-                               alloc_virtual_reg(), ID_REG_CLASSES(ins->id));
-                       ins->id = in->id;
-                       break;
-               case OP_SMUL:
-                       get_imm32(ins, &ins->right);
-                       in = pre_copy(state, ins, &ins->left,
-                               alloc_virtual_reg(), ID_REG_CLASSES(ins->id));
-                       ins->id = in->id;
-                       if (!IS_CONST_OP(ins->right->op)) {
-                               in2 = pre_copy(state, ins, &ins->right,
-                                       REG_UNSET, REGCM_GPR32);
-                       }
-                       break;
-               case OP_EQ: 
-                       bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ); 
-                       break;
-               case OP_NOTEQ:
-                       bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
-                       break;
-               case OP_SLESS:
-                       bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
-                       break;
-               case OP_ULESS:
-                       bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
-                       break;
-               case OP_SMORE:
-                       bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
-                       break;
-               case OP_UMORE:
-                       bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
-                       break;
-               case OP_SLESSEQ:
-                       bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
-                       break;
-               case OP_ULESSEQ:
-                       bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
-                       break;
-               case OP_SMOREEQ:
-                       bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
-                       break;
-               case OP_UMOREEQ:
-                       bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
-                       break;
-               case OP_LTRUE:
-                       bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
-                       break;
-               case OP_LFALSE:
-                       bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
+               break;
+       case OP_ADDRCONST:
+               ins->template_id = TEMPLATE_INTCONST32;
+               break;
+       case OP_NOOP:
+       case OP_SDECL:
+       case OP_BLOBCONST:
+       case OP_LABEL:
+               ins->template_id = TEMPLATE_NOP;
+               break;
+       case OP_COPY:
+               size = size_of(state, ins->type);
+               if (is_imm8(RHS(ins, 0)) && (size <= 1)) {
+                       ins->template_id = TEMPLATE_COPY_IMM8;
+               }
+               else if (is_imm16(RHS(ins, 0)) && (size <= 2)) {
+                       ins->template_id = TEMPLATE_COPY_IMM16;
+               }
+               else if (is_imm32(RHS(ins, 0)) && (size <= 4)) {
+                       ins->template_id = TEMPLATE_COPY_IMM32;
+               }
+               else if (is_const(RHS(ins, 0))) {
+                       internal_error(state, ins, "bad constant passed to copy");
+               }
+               else if (size <= 1) {
+                       ins->template_id = TEMPLATE_COPY8_REG;
+               }
+               else if (size <= 2) {
+                       ins->template_id = TEMPLATE_COPY16_REG;
+               }
+               else if (size <= 4) {
+                       ins->template_id = TEMPLATE_COPY32_REG;
+               }
+               else {
+                       internal_error(state, ins, "bad type passed to copy");
+               }
+               break;
+       case OP_PHI:
+               size = size_of(state, ins->type);
+               if (size <= 1) {
+                       ins->template_id = TEMPLATE_PHI8;
+               }
+               else if (size <= 2) {
+                       ins->template_id = TEMPLATE_PHI16;
+               }
+               else if (size <= 4) {
+                       ins->template_id = TEMPLATE_PHI32;
+               }
+               else {
+                       internal_error(state, ins, "bad type passed to phi");
+               }
+               break;
+       case OP_STORE:
+               switch(ins->type->type & TYPE_MASK) {
+               case TYPE_CHAR:    case TYPE_UCHAR:
+                       ins->template_id = TEMPLATE_STORE8;
                        break;
-               case OP_BRANCH:
-                       if (ins->right) {
-                               internal_error(state, ins, "bad branch test");
-                       }
-                       ins->op = OP_JMP;
+               case TYPE_SHORT:   case TYPE_USHORT:
+                       ins->template_id = TEMPLATE_STORE16;
                        break;
-
-               case OP_INB:
-               case OP_INW:
-               case OP_INL:
-                       get_imm8(ins, &ins->left);
-                       switch(ins->op) {
-                       case OP_INB: ins->id = MK_REG_ID(REG_AL,  REGCM_GPR8); break;
-                       case OP_INW: ins->id = MK_REG_ID(REG_AX,  REGCM_GPR16); break;
-                       case OP_INL: ins->id = MK_REG_ID(REG_EAX, REGCM_GPR32); break;
-                       }
-                       if (!IS_CONST_OP(ins->left->op)) {
-                               in = pre_copy(state, ins, &ins->left,
-                                       REG_DX, REGCM_GPR16);
-                       }
+               case TYPE_INT:     case TYPE_UINT:
+               case TYPE_LONG:    case TYPE_ULONG:
+               case TYPE_POINTER:
+                       ins->template_id = TEMPLATE_STORE32;
                        break;
-               case OP_OUTB:
-               case OP_OUTW:
-               case OP_OUTL:
-               {
-                       unsigned reg, mask;
-                       get_imm8(ins, &ins->right);
-                       switch(ins->op) {
-                       case OP_OUTB: reg = REG_AL;  mask = REGCM_GPR8; break;
-                       case OP_OUTW: reg = REG_AX;  mask = REGCM_GPR16; break;
-                       case OP_OUTL: reg = REG_EAX; mask = REGCM_GPR32; break;
-                       default: reg = REG_UNSET; mask = 0; break;
-                       }
-                       in = pre_copy(state, ins, &ins->left, reg, mask);
-                       if (!IS_CONST_OP(ins->right->op)) {
-                               in2 = pre_copy(state, ins, &ins->right,
-                                       REG_DX, REGCM_GPR16);
-                       }
+               default:
+                       internal_error(state, ins, "unknown type in store");
                        break;
                }
-               case OP_BSF:
-               case OP_BSR:
-                       in = pre_copy(state, ins, &ins->left, 
-                               REG_UNSET, REGCM_GPR32);
-                       ins->id = MK_REG_ID(REG_UNSET, REGCM_GPR32 | REGCM_GPR32_8);
+               break;
+       case OP_LOAD:
+               switch(ins->type->type & TYPE_MASK) {
+               case TYPE_CHAR:   case TYPE_UCHAR:
+                       ins->template_id = TEMPLATE_LOAD8;
                        break;
-                       /* Already transformed instructions */
-               case OP_CMP:
-               case OP_TEST:
-                       ins->id = MK_REG_ID(REG_EFLAGS, REGCM_FLAGS);
+               case TYPE_SHORT:
+               case TYPE_USHORT:
+                       ins->template_id = TEMPLATE_LOAD16;
                        break;
-               case OP_JMP_EQ:      case OP_JMP_NOTEQ:
-               case OP_JMP_SLESS:   case OP_JMP_ULESS:
-               case OP_JMP_SMORE:   case OP_JMP_UMORE:
-               case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
-               case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
-               case OP_SET_EQ:      case OP_SET_NOTEQ:
-               case OP_SET_SLESS:   case OP_SET_ULESS:
-               case OP_SET_SMORE:   case OP_SET_UMORE:
-               case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
-               case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
+               case TYPE_INT:
+               case TYPE_UINT:
+               case TYPE_LONG:
+               case TYPE_ULONG:
+               case TYPE_POINTER:
+                       ins->template_id = TEMPLATE_LOAD32;
                        break;
                default:
-                       internal_error(state, ins, "unhandled ins: %d %s\n",
-                               ins->op, tops(ins->op));
+                       internal_error(state, ins, "unknown type in load");
                        break;
                }
-               ins = next;
-       } while(ins != first);
+               break;
+       case OP_ADD:
+       case OP_SUB:
+       case OP_AND:
+       case OP_XOR:
+       case OP_OR:
+       case OP_SMUL:
+               ins->template_id = TEMPLATE_BINARY32_REG;
+               if (get_imm32(ins, &RHS(ins, 1))) {
+                       ins->template_id = TEMPLATE_BINARY32_IMM;
+               }
+               break;
+       case OP_SDIVT:
+       case OP_UDIVT:
+               ins->template_id = TEMPLATE_DIV32;
+               next = after_lhs(state, ins);
+               break;
+               /* FIXME UMUL does not work yet.. */
+       case OP_UMUL:
+               ins->template_id = TEMPLATE_UMUL32;
+               break;
+       case OP_UDIV:
+               next = mod_div(state, ins, OP_UDIVT, 0);
+               break;
+       case OP_SDIV:
+               next = mod_div(state, ins, OP_SDIVT, 0);
+               break;
+       case OP_UMOD:
+               next = mod_div(state, ins, OP_UDIVT, 1);
+               break;
+       case OP_SMOD:
+               next = mod_div(state, ins, OP_SDIVT, 1);
+               break;
+       case OP_SL:
+       case OP_SSR:
+       case OP_USR:
+               ins->template_id = TEMPLATE_SL32_CL;
+               if (get_imm8(ins, &RHS(ins, 1))) {
+                       ins->template_id = TEMPLATE_SL32_IMM;
+               } else if (size_of(state, RHS(ins, 1)->type) > 1) {
+                       typed_pre_copy(state, &char_type, ins, 1);
+               }
+               break;
+       case OP_INVERT:
+       case OP_NEG:
+               ins->template_id = TEMPLATE_UNARY32;
+               break;
+       case OP_EQ: 
+               bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ); 
+               break;
+       case OP_NOTEQ:
+               bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
+               break;
+       case OP_SLESS:
+               bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
+               break;
+       case OP_ULESS:
+               bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
+               break;
+       case OP_SMORE:
+               bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
+               break;
+       case OP_UMORE:
+               bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
+               break;
+       case OP_SLESSEQ:
+               bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
+               break;
+       case OP_ULESSEQ:
+               bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
+               break;
+       case OP_SMOREEQ:
+               bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
+               break;
+       case OP_UMOREEQ:
+               bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
+               break;
+       case OP_LTRUE:
+               bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
+               break;
+       case OP_LFALSE:
+               bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
+               break;
+       case OP_BRANCH:
+               if (TRIPLE_RHS(ins->sizes) > 0) {
+                       internal_error(state, ins, "bad branch test");
+               }
+               ins->op = OP_JMP;
+               ins->template_id = TEMPLATE_NOP;
+               break;
+       case OP_INB:
+       case OP_INW:
+       case OP_INL:
+               switch(ins->op) {
+               case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
+               case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
+               case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
+               }
+               if (get_imm8(ins, &RHS(ins, 0))) {
+                       ins->template_id += 1;
+               }
+               break;
+       case OP_OUTB:
+       case OP_OUTW:
+       case OP_OUTL:
+               switch(ins->op) {
+               case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
+               case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
+               case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
+               }
+               if (get_imm8(ins, &RHS(ins, 1))) {
+                       ins->template_id += 1;
+               }
+               break;
+       case OP_BSF:
+       case OP_BSR:
+               ins->template_id = TEMPLATE_BSF;
+               break;
+       case OP_RDMSR:
+               ins->template_id = TEMPLATE_RDMSR;
+               next = after_lhs(state, ins);
+               break;
+       case OP_WRMSR:
+               ins->template_id = TEMPLATE_WRMSR;
+               break;
+       case OP_HLT:
+               ins->template_id = TEMPLATE_NOP;
+               break;
+       case OP_ASM:
+               ins->template_id = TEMPLATE_NOP;
+               next = after_lhs(state, ins);
+               break;
+               /* Already transformed instructions */
+       case OP_TEST:
+               ins->template_id = TEMPLATE_TEST32;
+               break;
+       case OP_CMP:
+               ins->template_id = TEMPLATE_CMP32_REG;
+               if (get_imm32(ins, &RHS(ins, 1))) {
+                       ins->template_id = TEMPLATE_CMP32_IMM;
+               }
+               break;
+       case OP_JMP_EQ:      case OP_JMP_NOTEQ:
+       case OP_JMP_SLESS:   case OP_JMP_ULESS:
+       case OP_JMP_SMORE:   case OP_JMP_UMORE:
+       case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
+       case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
+               ins->template_id = TEMPLATE_JMP;
+               break;
+       case OP_SET_EQ:      case OP_SET_NOTEQ:
+       case OP_SET_SLESS:   case OP_SET_ULESS:
+       case OP_SET_SMORE:   case OP_SET_UMORE:
+       case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
+       case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
+               ins->template_id = TEMPLATE_SET;
+               break;
+               /* Unhandled instructions */
+       case OP_PIECE:
+       default:
+               internal_error(state, ins, "unhandled ins: %d %s\n",
+                       ins->op, tops(ins->op));
+               break;
+       }
+       return next;
 }
 
-
-
+static long next_label(struct compile_state *state)
+{
+       static long label_counter = 0;
+       return ++label_counter;
+}
 static void generate_local_labels(struct compile_state *state)
 {
        struct triple *first, *label;
-       int label_counter;
-       label_counter = 0;
-       first = state->main_function->left;
+       first = RHS(state->main_function, 0);
        label = first;
        do {
                if ((label->op == OP_LABEL) || 
                        (label->op == OP_SDECL)) {
                        if (label->use) {
-                               label->u.cval = ++label_counter;
+                               label->u.cval = next_label(state);
                        } else {
                                label->u.cval = 0;
                        }
@@ -12569,9 +16725,6 @@ static int check_reg(struct compile_state *state,
        if (reg == REG_UNSET) {
                internal_error(state, triple, "register not set");
        }
-       if (ID_REG_CLASSES(triple->id)) {
-               internal_error(state, triple, "class specifier present");
-       }
        mask = arch_reg_regcm(state, reg);
        if (!(classes & mask)) {
                internal_error(state, triple, "reg %d in wrong class",
@@ -12582,13 +16735,18 @@ static int check_reg(struct compile_state *state,
 
 static const char *arch_reg_str(int reg)
 {
+#if REG_XMM7 != 44
+#error "Registers have renumberd fix arch_reg_str"
+#endif
        static const char *regs[] = {
-               "%bad_register",
+               "%unset",
+               "%unneeded",
                "%eflags",
                "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
                "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
                "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
                "%edx:%eax",
+               "%dx:%ax",
                "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
                "%xmm0", "%xmm1", "%xmm2", "%xmm3", 
                "%xmm4", "%xmm5", "%xmm6", "%xmm7",
@@ -12599,6 +16757,7 @@ static const char *arch_reg_str(int reg)
        return regs[reg];
 }
 
+
 static const char *reg(struct compile_state *state, struct triple *triple,
        int classes)
 {
@@ -12622,65 +16781,159 @@ const char *type_suffix(struct compile_state *state, struct type *type)
        return suffix;
 }
 
+static void print_const_val(
+       struct compile_state *state, struct triple *ins, FILE *fp)
+{
+       switch(ins->op) {
+       case OP_INTCONST:
+               fprintf(fp, " $%ld ", 
+                       (long_t)(ins->u.cval));
+               break;
+       case OP_ADDRCONST:
+               if (MISC(ins, 0)->op != OP_SDECL) {
+                       internal_error(state, ins, "bad base for addrconst");
+               }
+               if (MISC(ins, 0)->u.cval <= 0) {
+                       internal_error(state, ins, "unlabeled constant");
+               }
+               fprintf(fp, " $L%s%lu+%lu ",
+                       state->label_prefix, 
+                       MISC(ins, 0)->u.cval,
+                       ins->u.cval);
+               break;
+       default:
+               internal_error(state, ins, "unknown constant type");
+               break;
+       }
+}
+
+static void print_const(struct compile_state *state,
+       struct triple *ins, FILE *fp)
+{
+       switch(ins->op) {
+       case OP_INTCONST:
+               switch(ins->type->type & TYPE_MASK) {
+               case TYPE_CHAR:
+               case TYPE_UCHAR:
+                       fprintf(fp, ".byte 0x%02lx\n", ins->u.cval);
+                       break;
+               case TYPE_SHORT:
+               case TYPE_USHORT:
+                       fprintf(fp, ".short 0x%04lx\n", ins->u.cval);
+                       break;
+               case TYPE_INT:
+               case TYPE_UINT:
+               case TYPE_LONG:
+               case TYPE_ULONG:
+                       fprintf(fp, ".int %lu\n", ins->u.cval);
+                       break;
+               default:
+                       internal_error(state, ins, "Unknown constant type");
+               }
+               break;
+       case OP_ADDRCONST:
+               if (MISC(ins, 0)->op != OP_SDECL) {
+                       internal_error(state, ins, "bad base for addrconst");
+               }
+               if (MISC(ins, 0)->u.cval <= 0) {
+                       internal_error(state, ins, "unlabeled constant");
+               }
+               fprintf(fp, ".int L%s%lu+%lu\n",
+                       state->label_prefix,
+                       MISC(ins, 0)->u.cval,
+                       ins->u.cval);
+               break;
+       case OP_BLOBCONST:
+       {
+               unsigned char *blob;
+               size_t size, i;
+               size = size_of(state, ins->type);
+               blob = ins->u.blob;
+               for(i = 0; i < size; i++) {
+                       fprintf(fp, ".byte 0x%02x\n",
+                               blob[i]);
+               }
+               break;
+       }
+       default:
+               internal_error(state, ins, "Unknown constant type");
+               break;
+       }
+}
+
+#define TEXT_SECTION ".rom.text"
+#define DATA_SECTION ".rom.data"
+
+static long get_const_pool_ref(
+       struct compile_state *state, struct triple *ins, FILE *fp)
+{
+       long ref;
+       ref = next_label(state);
+       fprintf(fp, ".section \"" DATA_SECTION "\"\n");
+       fprintf(fp, ".balign %d\n", align_of(state, ins->type));
+       fprintf(fp, "L%s%lu:\n", state->label_prefix, ref);
+       print_const(state, ins, fp);
+       fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
+       return ref;
+}
+
 static void print_binary_op(struct compile_state *state,
        const char *op, struct triple *ins, FILE *fp) 
 {
        unsigned mask;
-       mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
-       if (ins->left->id != ins->id) {
+       mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
+       if (RHS(ins, 0)->id != ins->id) {
                internal_error(state, ins, "invalid register assignment");
        }
-       if (IS_CONST_OP(ins->right->op)) {
-               fprintf(fp, "\t%s $%lu, %s\n",
-                       op,
-                       ins->right->u.cval,
-                       reg(state, ins->left, mask));
-               
+       if (is_const(RHS(ins, 1))) {
+               fprintf(fp, "\t%s ", op);
+               print_const_val(state, RHS(ins, 1), fp);
+               fprintf(fp, ", %s\n",
+                       reg(state, RHS(ins, 0), mask));
        }
        else {
                unsigned lmask, rmask;
                int lreg, rreg;
-               lreg = check_reg(state, ins->left, mask);
-               rreg = check_reg(state, ins->right, mask);
+               lreg = check_reg(state, RHS(ins, 0), mask);
+               rreg = check_reg(state, RHS(ins, 1), mask);
                lmask = arch_reg_regcm(state, lreg);
                rmask = arch_reg_regcm(state, rreg);
                mask = lmask & rmask;
                fprintf(fp, "\t%s %s, %s\n",
                        op,
-                       reg(state, ins->right, mask),
-                       reg(state, ins->left, mask));
+                       reg(state, RHS(ins, 1), mask),
+                       reg(state, RHS(ins, 0), mask));
        }
 }
 static void print_unary_op(struct compile_state *state, 
        const char *op, struct triple *ins, FILE *fp)
 {
        unsigned mask;
-       mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
+       mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
        fprintf(fp, "\t%s %s\n",
                op,
-               reg(state, ins->left, mask));
+               reg(state, RHS(ins, 0), mask));
 }
 
 static void print_op_shift(struct compile_state *state,
        const char *op, struct triple *ins, FILE *fp)
 {
        unsigned mask;
-       mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
-       if (ins->left->id != ins->id) {
+       mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
+       if (RHS(ins, 0)->id != ins->id) {
                internal_error(state, ins, "invalid register assignment");
        }
-       if (IS_CONST_OP(ins->right->op)) {
-               fprintf(fp, "\t%s $%lu, %s\n",
-                       op,
-                       ins->right->u.cval,
-                       reg(state, ins->left, mask));
-               
+       if (is_const(RHS(ins, 1))) {
+               fprintf(fp, "\t%s ", op);
+               print_const_val(state, RHS(ins, 1), fp);
+               fprintf(fp, ", %s\n",
+                       reg(state, RHS(ins, 0), mask));
        }
        else {
                fprintf(fp, "\t%s %s, %s\n",
                        op,
-                       reg(state, ins->right, REGCM_GPR8),
-                       reg(state, ins->left, mask));
+                       reg(state, RHS(ins, 1), REGCM_GPR8_LO),
+                       reg(state, RHS(ins, 0), mask));
        }
 }
 
@@ -12691,7 +16944,7 @@ static void print_op_in(struct compile_state *state, struct triple *ins, FILE *f
        int dreg;
        mask = 0;
        switch(ins->op) {
-       case OP_INB: op = "inb", mask = REGCM_GPR8; break;
+       case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
        case OP_INW: op = "inw", mask = REGCM_GPR16; break;
        case OP_INL: op = "inl", mask = REGCM_GPR32; break;
        default:
@@ -12703,20 +16956,21 @@ static void print_op_in(struct compile_state *state, struct triple *ins, FILE *f
        if (!reg_is_reg(state, dreg, REG_EAX)) {
                internal_error(state, ins, "dst != %%eax");
        }
-       if (IS_CONST_OP(ins->left->op)) {
-               fprintf(fp, "\t%s $%lu, %s\n",
-                       op, ins->left->u.cval, 
+       if (is_const(RHS(ins, 0))) {
+               fprintf(fp, "\t%s ", op);
+               print_const_val(state, RHS(ins, 0), fp);
+               fprintf(fp, ", %s\n",
                        reg(state, ins, mask));
        }
        else {
                int addr_reg;
-               addr_reg = check_reg(state, ins->left, REGCM_GPR16);
+               addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
                if (!reg_is_reg(state, addr_reg, REG_DX)) {
                        internal_error(state, ins, "src != %%dx");
                }
                fprintf(fp, "\t%s %s, %s\n",
                        op, 
-                       reg(state, ins->left, REGCM_GPR16),
+                       reg(state, RHS(ins, 0), REGCM_GPR16),
                        reg(state, ins, mask));
        }
 }
@@ -12728,7 +16982,7 @@ static void print_op_out(struct compile_state *state, struct triple *ins, FILE *
        int lreg;
        mask = 0;
        switch(ins->op) {
-       case OP_OUTB: op = "outb", mask = REGCM_GPR8; break;
+       case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
        case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
        case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
        default:
@@ -12736,25 +16990,26 @@ static void print_op_out(struct compile_state *state, struct triple *ins, FILE *
                op = 0;
                break;
        }
-       lreg = check_reg(state, ins->left, mask);
+       lreg = check_reg(state, RHS(ins, 0), mask);
        if (!reg_is_reg(state, lreg, REG_EAX)) {
                internal_error(state, ins, "src != %%eax");
        }
-       if (IS_CONST_OP(ins->right->op)) {
-               fprintf(fp, "\t%s %s, $%lu\n",
-                       op, reg(state, ins->left, mask),
-                       ins->right->u.cval);
+       if (is_const(RHS(ins, 1))) {
+               fprintf(fp, "\t%s %s,", 
+                       op, reg(state, RHS(ins, 0), mask));
+               print_const_val(state, RHS(ins, 1), fp);
+               fprintf(fp, "\n");
        }
        else {
                int addr_reg;
-               addr_reg = check_reg(state, ins->right, REGCM_GPR16);
+               addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
                if (!reg_is_reg(state, addr_reg, REG_DX)) {
                        internal_error(state, ins, "dst != %%dx");
                }
                fprintf(fp, "\t%s %s, %s\n",
                        op, 
-                       reg(state, ins->left, mask),
-                       reg(state, ins->right, REGCM_GPR16));
+                       reg(state, RHS(ins, 0), mask),
+                       reg(state, RHS(ins, 1), REGCM_GPR16));
        }
 }
 
@@ -12763,31 +17018,29 @@ static void print_op_move(struct compile_state *state,
 {
        /* op_move is complex because there are many types
         * of registers we can move between.
+        * Because OP_COPY will be introduced in arbitrary locations
+        * OP_COPY must not affect flags.
         */
        int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
        struct triple *dst, *src;
        if (ins->op == OP_COPY) {
-               src = ins->left;
+               src = RHS(ins, 0);
                dst = ins;
        }
-       else if (ins->op == OP_WRITE) {
-               dst = ins->left;
-               src = ins->right;
-       }
        else {
                internal_error(state, ins, "unknown move operation");
                src = dst = 0;
        }
-       if (!IS_CONST_OP(src->op)) {
+       if (!is_const(src)) {
                int src_reg, dst_reg;
                int src_regcm, dst_regcm;
-               src_reg = ID_REG(src->id);
+               src_reg   = ID_REG(src->id);
                dst_reg   = ID_REG(dst->id);
                src_regcm = arch_reg_regcm(state, src_reg);
-               dst_regcm   = arch_reg_regcm(state, dst_reg);
+               dst_regcm = arch_reg_regcm(state, dst_reg);
                /* If the class is the same just move the register */
                if (src_regcm & dst_regcm & 
-                       (REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32)) {
+                       (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
                        if ((src_reg != dst_reg) || !omit_copy) {
                                fprintf(fp, "\tmov %s, %s\n",
                                        reg(state, src, src_regcm),
@@ -12804,9 +17057,19 @@ static void print_op_move(struct compile_state *state,
                                        arch_reg_str(dst_reg));
                        }
                }
+               /* Move from 32bit gprs to 16bit gprs */
+               else if ((src_regcm & REGCM_GPR32) &&
+                       (dst_regcm & REGCM_GPR16)) {
+                       dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
+                       if ((src_reg != dst_reg) || !omit_copy) {
+                               fprintf(fp, "\tmov %s, %s\n",
+                                       arch_reg_str(src_reg),
+                                       arch_reg_str(dst_reg));
+                       }
+               }
                /* Move 32bit to 8bit */
                else if ((src_regcm & REGCM_GPR32_8) &&
-                       (dst_regcm & REGCM_GPR8))
+                       (dst_regcm & REGCM_GPR8_LO))
                {
                        src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
                        if ((src_reg != dst_reg) || !omit_copy) {
@@ -12817,7 +17080,7 @@ static void print_op_move(struct compile_state *state,
                }
                /* Move 16bit to 8bit */
                else if ((src_regcm & REGCM_GPR16_8) &&
-                       (dst_regcm & REGCM_GPR8))
+                       (dst_regcm & REGCM_GPR8_LO))
                {
                        src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
                        if ((src_reg != dst_reg) || !omit_copy) {
@@ -12827,8 +17090,8 @@ static void print_op_move(struct compile_state *state,
                        }
                }
                /* Move 8/16bit to 16/32bit */
-               else if ((src_regcm & (REGCM_GPR8 | REGCM_GPR16)) &&
-                       (dst_regcm & (REGC_GPR16 | REGCM_GPR32))) {
+               else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) && 
+                       (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
                        const char *op;
                        op = is_signed(src->type)? "movsx": "movzx";
                        fprintf(fp, "\t%s %s, %s\n",
@@ -12839,20 +17102,31 @@ static void print_op_move(struct compile_state *state,
                /* Move between sse registers */
                else if ((src_regcm & dst_regcm & REGCM_XMM)) {
                        if ((src_reg != dst_reg) || !omit_copy) {
-                               fprintf(fp, "\tmovdqa %s %s\n",
+                               fprintf(fp, "\tmovdqa %s, %s\n",
                                        reg(state, src, src_regcm),
                                        reg(state, dst, dst_regcm));
                        }
                }
-               /* Move between mmx registers or mmx & sse  registers */
-               else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
-                       (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
+               /* Move between mmx registers */
+               else if ((src_regcm & dst_regcm & REGCM_MMX)) {
                        if ((src_reg != dst_reg) || !omit_copy) {
-                               fprintf(fp, "\tmovq %s %s\n",
+                               fprintf(fp, "\tmovq %s, %s\n",
                                        reg(state, src, src_regcm),
                                        reg(state, dst, dst_regcm));
                        }
                }
+               /* Move from sse to mmx registers */
+               else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
+                       fprintf(fp, "\tmovdq2q %s, %s\n",
+                               reg(state, src, src_regcm),
+                               reg(state, dst, dst_regcm));
+               }
+               /* Move from mmx to sse registers */
+               else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
+                       fprintf(fp, "\tmovq2dq %s, %s\n",
+                               reg(state, src, src_regcm),
+                               reg(state, dst, dst_regcm));
+               }
                /* Move between 32bit gprs & mmx/sse registers */
                else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
                        (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
@@ -12860,28 +17134,156 @@ static void print_op_move(struct compile_state *state,
                                reg(state, src, src_regcm),
                                reg(state, dst, dst_regcm));
                }
+               /* Move from 16bit gprs &  mmx/sse registers */
+               else if ((src_regcm & REGCM_GPR16) &&
+                       (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
+                       const char *op;
+                       int mid_reg;
+                       op = is_signed(src->type)? "movsx":"movzx";
+                       mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
+                       fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
+                               op,
+                               arch_reg_str(src_reg),
+                               arch_reg_str(mid_reg),
+                               arch_reg_str(mid_reg),
+                               arch_reg_str(dst_reg));
+               }
+               /* Move from mmx/sse registers to 16bit gprs */
+               else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
+                       (dst_regcm & REGCM_GPR16)) {
+                       dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
+                       fprintf(fp, "\tmovd %s, %s\n",
+                               arch_reg_str(src_reg),
+                               arch_reg_str(dst_reg));
+               }
+               /* Move from gpr to 64bit dividend */
+               else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))  &&
+                       (dst_regcm & REGCM_DIVIDEND64)) {
+                       const char *extend;
+                       extend = is_signed(src->type)? "cltd":"movl $0, %edx";
+                       fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
+                               arch_reg_str(src_reg), 
+                               extend);
+               }
+               /* Move from 64bit gpr to gpr */
+               else if ((src_regcm & REGCM_DIVIDEND64) &&
+                       (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
+                       if (dst_regcm & REGCM_GPR32) {
+                               src_reg = REG_EAX;
+                       } 
+                       else if (dst_regcm & REGCM_GPR16) {
+                               src_reg = REG_AX;
+                       }
+                       else if (dst_regcm & REGCM_GPR8_LO) {
+                               src_reg = REG_AL;
+                       }
+                       fprintf(fp, "\tmov %s, %s\n",
+                               arch_reg_str(src_reg),
+                               arch_reg_str(dst_reg));
+               }
+               /* Move from mmx/sse registers to 64bit gpr */
+               else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
+                       (dst_regcm & REGCM_DIVIDEND64)) {
+                       const char *extend;
+                       extend = is_signed(src->type)? "cltd": "movl $0, %edx";
+                       fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
+                               arch_reg_str(src_reg),
+                               extend);
+               }
+               /* Move from 64bit gpr to mmx/sse register */
+               else if ((src_regcm & REGCM_DIVIDEND64) &&
+                       (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
+                       fprintf(fp, "\tmovd %%eax, %s\n",
+                               arch_reg_str(dst_reg));
+               }
+#if X86_4_8BIT_GPRS
+               /* Move from 8bit gprs to  mmx/sse registers */
+               else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
+                       (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
+                       const char *op;
+                       int mid_reg;
+                       op = is_signed(src->type)? "movsx":"movzx";
+                       mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
+                       fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
+                               op,
+                               reg(state, src, src_regcm),
+                               arch_reg_str(mid_reg),
+                               arch_reg_str(mid_reg),
+                               reg(state, dst, dst_regcm));
+               }
+               /* Move from mmx/sse registers and 8bit gprs */
+               else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
+                       (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
+                       int mid_reg;
+                       mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
+                       fprintf(fp, "\tmovd %s, %s\n",
+                               reg(state, src, src_regcm),
+                               arch_reg_str(mid_reg));
+               }
+               /* Move from 32bit gprs to 8bit gprs */
+               else if ((src_regcm & REGCM_GPR32) &&
+                       (dst_regcm & REGCM_GPR8_LO)) {
+                       dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
+                       if ((src_reg != dst_reg) || !omit_copy) {
+                               fprintf(fp, "\tmov %s, %s\n",
+                                       arch_reg_str(src_reg),
+                                       arch_reg_str(dst_reg));
+                       }
+               }
+               /* Move from 16bit gprs to 8bit gprs */
+               else if ((src_regcm & REGCM_GPR16) &&
+                       (dst_regcm & REGCM_GPR8_LO)) {
+                       dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
+                       if ((src_reg != dst_reg) || !omit_copy) {
+                               fprintf(fp, "\tmov %s, %s\n",
+                                       arch_reg_str(src_reg),
+                                       arch_reg_str(dst_reg));
+                       }
+               }
+#endif /* X86_4_8BIT_GPRS */
                else {
                        internal_error(state, ins, "unknown copy type");
                }
        }
-       else switch(src->op) {
-       case OP_INTCONST:
-       {
-               long_t value;
-               value = (long_t)(src->u.cval);
-               fprintf(fp, "\tmov $%ld, %s\n",
-                       value,
-                       reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8));
-               break;
-       }
-       case OP_ADDRCONST:
-               fprintf(fp, "\tmov $L%lu+%lu, %s\n",
-                       src->left->u.cval,
-                       src->u.cval,
-                       reg(state, dst, REGCM_GPR32));
-               break;
-       default:
-               internal_error(state, ins, "uknown copy operation");
+       else {
+               int dst_reg;
+               int dst_regcm;
+               dst_reg = ID_REG(dst->id);
+               dst_regcm = arch_reg_regcm(state, dst_reg);
+               if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
+                       fprintf(fp, "\tmov ");
+                       print_const_val(state, src, fp);
+                       fprintf(fp, ", %s\n",
+                               reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
+               }
+               else if (dst_regcm & REGCM_DIVIDEND64) {
+                       if (size_of(state, dst->type) > 4) {
+                               internal_error(state, ins, "64bit constant...");
+                       }
+                       fprintf(fp, "\tmov $0, %%edx\n");
+                       fprintf(fp, "\tmov ");
+                       print_const_val(state, src, fp);
+                       fprintf(fp, ", %%eax\n");
+               }
+               else if (dst_regcm & REGCM_DIVIDEND32) {
+                       if (size_of(state, dst->type) > 2) {
+                               internal_error(state, ins, "32bit constant...");
+                       }
+                       fprintf(fp, "\tmov $0, %%dx\n");
+                       fprintf(fp, "\tmov ");
+                       print_const_val(state, src, fp);
+                       fprintf(fp, ", %%ax");
+               }
+               else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
+                       long ref;
+                       ref = get_const_pool_ref(state, src, fp);
+                       fprintf(fp, "\tmovq L%s%lu, %s\n",
+                               state->label_prefix, ref,
+                               reg(state, dst, (REGCM_XMM | REGCM_MMX)));
+               }
+               else {
+                       internal_error(state, ins, "unknown copy immediate type");
+               }
        }
 }
 
@@ -12890,13 +17292,13 @@ static void print_op_load(struct compile_state *state,
 {
        struct triple *dst, *src;
        dst = ins;
-       src = ins->left;
+       src = RHS(ins, 0);
        if (is_const(src) || is_const(dst)) {
                internal_error(state, ins, "unknown load operation");
        }
        fprintf(fp, "\tmov (%s), %s\n",
                reg(state, src, REGCM_GPR32),
-               reg(state, dst, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32));
+               reg(state, dst, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32));
 }
 
 
@@ -12904,8 +17306,8 @@ static void print_op_store(struct compile_state *state,
        struct triple *ins, FILE *fp)
 {
        struct triple *dst, *src;
-       dst = ins->left;
-       src = ins->right;
+       dst = RHS(ins, 0);
+       src = RHS(ins, 1);
        if (is_const(src) && (src->op == OP_INTCONST)) {
                long_t value;
                value = (long_t)(src->u.cval);
@@ -12917,7 +17319,7 @@ static void print_op_store(struct compile_state *state,
        else if (is_const(dst) && (dst->op == OP_INTCONST)) {
                fprintf(fp, "\tmov%s %s, 0x%08lx\n",
                        type_suffix(state, src->type),
-                       reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
+                       reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
                        dst->u.cval);
        }
        else {
@@ -12926,7 +17328,7 @@ static void print_op_store(struct compile_state *state,
                }
                fprintf(fp, "\tmov%s %s, (%s)\n",
                        type_suffix(state, src->type),
-                       reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
+                       reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
                        reg(state, dst, REGCM_GPR32));
        }
        
@@ -12936,15 +17338,15 @@ static void print_op_store(struct compile_state *state,
 static void print_op_smul(struct compile_state *state,
        struct triple *ins, FILE *fp)
 {
-       if (!IS_CONST_OP(ins->right->op)) {
+       if (!is_const(RHS(ins, 1))) {
                fprintf(fp, "\timul %s, %s\n",
-                       reg(state, ins->right, REGCM_GPR32),
-                       reg(state, ins->left, REGCM_GPR32));
+                       reg(state, RHS(ins, 1), REGCM_GPR32),
+                       reg(state, RHS(ins, 0), REGCM_GPR32));
        }
        else {
-               fprintf(fp, "\timul $%ld, %s\n",
-                       ins->right->u.cval,
-                       reg(state, ins->left, REGCM_GPR32));
+               fprintf(fp, "\timul ");
+               print_const_val(state, RHS(ins, 1), fp);
+               fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
        }
 }
 
@@ -12953,27 +17355,27 @@ static void print_op_cmp(struct compile_state *state,
 {
        unsigned mask;
        int dreg;
-       mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
+       mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
        dreg = check_reg(state, ins, REGCM_FLAGS);
        if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
                internal_error(state, ins, "bad dest register for cmp");
        }
-       if (IS_CONST_OP(ins->right->op)) {
-               fprintf(fp, "\tcmp $%lu, %s\n",
-                       ins->right->u.cval,
-                       reg(state, ins->left, mask));
+       if (is_const(RHS(ins, 1))) {
+               fprintf(fp, "\tcmp ");
+               print_const_val(state, RHS(ins, 1), fp);
+               fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
        }
        else {
                unsigned lmask, rmask;
                int lreg, rreg;
-               lreg = check_reg(state, ins->left, mask);
-               rreg = check_reg(state, ins->right, mask);
+               lreg = check_reg(state, RHS(ins, 0), mask);
+               rreg = check_reg(state, RHS(ins, 1), mask);
                lmask = arch_reg_regcm(state, lreg);
                rmask = arch_reg_regcm(state, rreg);
                mask = lmask & rmask;
                fprintf(fp, "\tcmp %s, %s\n",
-                       reg(state, ins->right, mask),
-                       reg(state, ins->left, mask));
+                       reg(state, RHS(ins, 1), mask),
+                       reg(state, RHS(ins, 0), mask));
        }
 }
 
@@ -12981,10 +17383,10 @@ static void print_op_test(struct compile_state *state,
        struct triple *ins, FILE *fp)
 {
        unsigned mask;
-       mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
+       mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
        fprintf(fp, "\ttest %s, %s\n",
-               reg(state, ins->left, mask),
-               reg(state, ins->left, mask));
+               reg(state, RHS(ins, 0), mask),
+               reg(state, RHS(ins, 0), mask));
 }
 
 static void print_op_branch(struct compile_state *state,
@@ -12992,23 +17394,27 @@ static void print_op_branch(struct compile_state *state,
 {
        const char *bop = "j";
        if (branch->op == OP_JMP) {
-               if (branch->right) {
+               if (TRIPLE_RHS(branch->sizes) != 0) {
                        internal_error(state, branch, "jmp with condition?");
                }
                bop = "jmp";
        }
        else {
-               if (!branch->right) {
+               struct triple *ptr;
+               if (TRIPLE_RHS(branch->sizes) != 1) {
                        internal_error(state, branch, "jmpcc without condition?");
                }
-               check_reg(state, branch->right, REGCM_FLAGS);
-               if ((branch->right->op != OP_CMP) &&
-                       (branch->right->op != OP_TEST)) {
+               check_reg(state, RHS(branch, 0), REGCM_FLAGS);
+               if ((RHS(branch, 0)->op != OP_CMP) &&
+                       (RHS(branch, 0)->op != OP_TEST)) {
                        internal_error(state, branch, "bad branch test");
                }
 #warning "FIXME I have observed instructions between the test and branch instructions"
-               if (branch->right->next != branch) {
-                       internal_error(state, branch, "branch does not follow test");
+               ptr = RHS(branch, 0);
+               for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
+                       if (ptr->op != OP_COPY) {
+                               internal_error(state, branch, "branch does not follow test");
+                       }
                }
                switch(branch->op) {
                case OP_JMP_EQ:       bop = "jz";  break;
@@ -13027,23 +17433,25 @@ static void print_op_branch(struct compile_state *state,
                }
                
        }
-       fprintf(fp, "\t%s L%lu\n",
-               bop, branch->left->u.cval);
+       fprintf(fp, "\t%s L%s%lu\n",
+               bop, 
+               state->label_prefix,
+               TARG(branch, 0)->u.cval);
 }
 
 static void print_op_set(struct compile_state *state,
        struct triple *set, FILE *fp)
 {
        const char *sop = "set";
-       if (!set->left) {
+       if (TRIPLE_RHS(set->sizes) != 1) {
                internal_error(state, set, "setcc without condition?");
        }
-       check_reg(state, set->left, REGCM_FLAGS);
-       if ((set->left->op != OP_CMP) &&
-               (set->left->op != OP_TEST)) {
+       check_reg(state, RHS(set, 0), REGCM_FLAGS);
+       if ((RHS(set, 0)->op != OP_CMP) &&
+               (RHS(set, 0)->op != OP_TEST)) {
                internal_error(state, set, "bad set test");
        }
-       if (set->left->next != set) {
+       if (RHS(set, 0)->next != set) {
                internal_error(state, set, "set does not follow test");
        }
        switch(set->op) {
@@ -13062,7 +17470,7 @@ static void print_op_set(struct compile_state *state,
                break;
        }
        fprintf(fp, "\t%s %s\n",
-               sop, reg(state, set, REGCM_GPR8));
+               sop, reg(state, set, REGCM_GPR8_LO));
 }
 
 static void print_op_bit_scan(struct compile_state *state, 
@@ -13083,68 +17491,20 @@ static void print_op_bit_scan(struct compile_state *state,
                "\tmovl $-1, %s\n"
                "1:\n",
                op,
-               reg(state, ins->left, REGCM_GPR32),
+               reg(state, RHS(ins, 0), REGCM_GPR32),
                reg(state, ins, REGCM_GPR32),
                reg(state, ins, REGCM_GPR32));
 }
 
-static void print_const(struct compile_state *state,
-       struct triple *ins, FILE *fp)
-{
-       switch(ins->op) {
-       case OP_INTCONST:
-               switch(ins->type->type & TYPE_MASK) {
-               case TYPE_CHAR:
-               case TYPE_UCHAR:
-                       fprintf(fp, ".byte 0x%02lx\n", ins->u.cval);
-                       break;
-               case TYPE_SHORT:
-               case TYPE_USHORT:
-                       fprintf(fp, ".short 0x%04lx\n", ins->u.cval);
-                       break;
-               case TYPE_INT:
-               case TYPE_UINT:
-               case TYPE_LONG:
-               case TYPE_ULONG:
-                       fprintf(fp, ".int %lu\n", ins->u.cval);
-                       break;
-               default:
-                       internal_error(state, ins, "Unknown constant type");
-               }
-               break;
-       case OP_BLOBCONST:
-       {
-               unsigned char *blob;
-               size_t size, i;
-               size = size_of(state, ins->type);
-               blob = ins->u.blob;
-               for(i = 0; i < size; i++) {
-                       fprintf(fp, ".byte 0x%02x\n",
-                               blob[i]);
-               }
-               break;
-       }
-#if 0
-       case OP_ADDRCONST:
-               fprintf(fp, ".int $L%lu+%lu",
-                       ins->left->u.cval,
-                       ins->u.cval);
-               break;
-#endif
-       default:
-               internal_error(state, ins, "Unknown constant type");
-               break;
-       }
-}
 
 static void print_sdecl(struct compile_state *state,
        struct triple *ins, FILE *fp)
 {
-       fprintf(fp, ".section \".rom.data\"\n");
+       fprintf(fp, ".section \"" DATA_SECTION "\"\n");
        fprintf(fp, ".balign %d\n", align_of(state, ins->type));
-       fprintf(fp, "L%lu:\n", ins->u.cval);
-       print_const(state, ins->left, fp);
-       fprintf(fp, ".section \".rom.text\"\n");
+       fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
+       print_const(state, MISC(ins, 0), fp);
+       fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
                
 }
 
@@ -13155,6 +17515,9 @@ static void print_instruction(struct compile_state *state,
         * everything is in a valid register. 
         */
        switch(ins->op) {
+       case OP_ASM:
+               print_op_asm(state, ins, fp);
+               break;
        case OP_ADD:    print_binary_op(state, "add", ins, fp); break;
        case OP_SUB:    print_binary_op(state, "sub", ins, fp); break;
        case OP_AND:    print_binary_op(state, "and", ins, fp); break;
@@ -13168,6 +17531,7 @@ static void print_instruction(struct compile_state *state,
        case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
        case OP_INTCONST:
        case OP_ADDRCONST:
+       case OP_BLOBCONST:
                /* Don't generate anything here for constants */
        case OP_PHI:
                /* Don't generate anything for variable declarations. */
@@ -13175,7 +17539,6 @@ static void print_instruction(struct compile_state *state,
        case OP_SDECL:
                print_sdecl(state, ins, fp);
                break;
-       case OP_WRITE: 
        case OP_COPY:   
                print_op_move(state, ins, fp);
                break;
@@ -13215,20 +17578,37 @@ static void print_instruction(struct compile_state *state,
        case OP_BSR:
                print_op_bit_scan(state, ins, fp);
                break;
+       case OP_RDMSR:
+               after_lhs(state, ins);
+               fprintf(fp, "\trdmsr\n");
+               break;
+       case OP_WRMSR:
+               fprintf(fp, "\twrmsr\n");
+               break;
        case OP_HLT:
                fprintf(fp, "\thlt\n");
                break;
+       case OP_SDIVT:
+               fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
+               break;
+       case OP_UDIVT:
+               fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
+               break;
+       case OP_UMUL:
+               fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
+               break;
        case OP_LABEL:
                if (!ins->use) {
                        return;
                }
-               fprintf(fp, "L%lu:\n", ins->u.cval);
+               fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
                break;
-               /* Operations I am not yet certain how to handle */
-       case OP_UMUL:
+               /* Ignore OP_PIECE */
+       case OP_PIECE:
+               break;
+               /* Operations that should never get here */
        case OP_SDIV: case OP_UDIV:
        case OP_SMOD: case OP_UMOD:
-               /* Operations that should never get here */
        case OP_LTRUE:   case OP_LFALSE:  case OP_EQ:      case OP_NOTEQ:
        case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
        case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
@@ -13243,35 +17623,60 @@ static void print_instructions(struct compile_state *state)
 {
        struct triple *first, *ins;
        int print_location;
-       int last_line;
-       int last_col;
-       const char *last_filename;
+       struct occurance *last_occurance;
        FILE *fp;
+       int max_inline_depth;
+       max_inline_depth = 0;
        print_location = 1;
-       last_line = -1;
-       last_col  = -1;
-       last_filename = 0;
-       fp = stdout;
-       fprintf(fp, ".section \".rom.text\"\n");
-       first = state->main_function->left;
+       last_occurance = 0;
+       fp = state->output;
+       fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
+       first = RHS(state->main_function, 0);
        ins = first;
        do {
-               if (print_location &&
-                       ((last_filename != ins->filename) ||
-                               (last_line != ins->line) ||
-                               (last_col  != ins->col))) {
-                       fprintf(fp, "\t/* %s:%d */\n",
-                               ins->filename, ins->line);
-                       last_filename = ins->filename;
-                       last_line = ins->line;
-                       last_col  = ins->col;
+               if (print_location && 
+                       last_occurance != ins->occurance) {
+                       if (!ins->occurance->parent) {
+                               fprintf(fp, "\t/* %s,%s:%d.%d */\n",
+                                       ins->occurance->function,
+                                       ins->occurance->filename,
+                                       ins->occurance->line,
+                                       ins->occurance->col);
+                       }
+                       else {
+                               struct occurance *ptr;
+                               int inline_depth;
+                               fprintf(fp, "\t/*\n");
+                               inline_depth = 0;
+                               for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
+                                       inline_depth++;
+                                       fprintf(fp, "\t * %s,%s:%d.%d\n",
+                                               ptr->function,
+                                               ptr->filename,
+                                               ptr->line,
+                                               ptr->col);
+                               }
+                               fprintf(fp, "\t */\n");
+                               if (inline_depth > max_inline_depth) {
+                                       max_inline_depth = inline_depth;
+                               }
+                       }
+                       if (last_occurance) {
+                               put_occurance(last_occurance);
+                       }
+                       get_occurance(ins->occurance);
+                       last_occurance = ins->occurance;
                }
 
                print_instruction(state, ins, fp);
                ins = ins->next;
        } while(ins != first);
-       
+       if (print_location) {
+               fprintf(fp, "/* max inline depth %d */\n",
+                       max_inline_depth);
+       }
 }
+
 static void generate_code(struct compile_state *state)
 {
        generate_local_labels(state);
@@ -13298,7 +17703,8 @@ static void print_tokens(struct compile_state *state)
        } while(tk->tok != TOK_EOF);
 }
 
-static void compile(char *filename, int debug, int opt)
+static void compile(const char *filename, const char *ofilename, 
+       int cpu, int debug, int opt, const char *label_prefix)
 {
        int i;
        struct compile_state state;
@@ -13309,8 +17715,18 @@ static void compile(char *filename, int debug, int opt)
                state.token[i].tok = -1;
        }
        /* Remember the debug settings */
-       state.debug = debug;
+       state.cpu      = cpu;
+       state.debug    = debug;
        state.optimize = opt;
+       /* Remember the output filename */
+       state.ofilename = ofilename;
+       state.output    = fopen(state.ofilename, "w");
+       if (!state.output) {
+               error(&state, 0, "Cannot open output file %s\n",
+                       ofilename);
+       }
+       /* Remember the label prefix */
+       state.label_prefix = label_prefix;
        /* Prep the preprocessor */
        state.if_depth = 0;
        state.if_value = 0;
@@ -13336,6 +17752,7 @@ static void compile(char *filename, int debug, int opt)
         * optimize the intermediate code 
         */
        optimize(&state);
+
        generate_code(&state);
        if (state.debug) {
                fprintf(stderr, "done\n");
@@ -13368,10 +17785,16 @@ static void arg_error(char *fmt, ...)
 
 int main(int argc, char **argv)
 {
-       char *filename;
+       const char *filename;
+       const char *ofilename;
+       const char *label_prefix;
+       int cpu;
        int last_argc;
        int debug;
        int optimize;
+       cpu = CPU_DEFAULT;
+       label_prefix = "";
+       ofilename = "auto.inc";
        optimize = 0;
        debug = 0;
        last_argc = -1;
@@ -13382,6 +17805,11 @@ int main(int argc, char **argv)
                        argv++;
                        argc--;
                }
+               else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
+                       label_prefix= argv[1] + 15;
+                       argv++;
+                       argc--;
+               }
                else if ((strcmp(argv[1],"-O") == 0) ||
                        (strcmp(argv[1], "-O1") == 0)) {
                        optimize = 1;
@@ -13393,12 +17821,26 @@ int main(int argc, char **argv)
                        argv++;
                        argc--;
                }
+               else if ((strcmp(argv[1], "-o") == 0) && (argc > 2)) {
+                       ofilename = argv[2];
+                       argv += 2;
+                       argc -= 2;
+               }
+               else if (strncmp(argv[1], "-mcpu=", 6) == 0) {
+                       cpu = arch_encode_cpu(argv[1] + 6);
+                       if (cpu == BAD_CPU) {
+                               arg_error("Invalid cpu specified: %s\n",
+                                       argv[1] + 6);
+                       }
+                       argv++;
+                       argc--;
+               }
        }
        if (argc != 2) {
                arg_error("Wrong argument count %d\n", argc);
        }
        filename = argv[1];
-       compile(filename, debug, optimize);
+       compile(filename, ofilename, cpu, debug, optimize, label_prefix);
 
        return 0;
 }