X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=util%2Fromcc%2Fromcc.c;h=c7ef22366751613d40174a3170b1bd900d74b84e;hb=f31abe31f0dd47a9b99a4cabecd5f414a07418d4;hp=561ba2411052d6e7961f955a6ec35992bc96af7c;hpb=05f26fcb571340b17beaca16939a025a9c0b4cdd;p=coreboot.git diff --git a/util/romcc/romcc.c b/util/romcc/romcc.c index 561ba2411..c7ef22366 100644 --- a/util/romcc/romcc.c +++ b/util/romcc/romcc.c @@ -1,3 +1,12 @@ +#undef VERSION_MAJOR +#undef VERSION_MINOR +#undef RELEASE_DATE +#undef VERSION +#define VERSION_MAJOR "0" +#define VERSION_MINOR "72" +#define RELEASE_DATE "10 February 2010" +#define VERSION VERSION_MAJOR "." VERSION_MINOR + #include #include #include @@ -9,18 +18,50 @@ #include #include #include -#include #include +#include +#include -#define DEBUG_ERROR_MESSAGES 0 -#define DEBUG_COLOR_GRAPH 0 -#define DEBUG_SCC 0 -#define DEBUG_CONSISTENCY 1 +#define MAX_CWD_SIZE 4096 +#define MAX_ALLOCATION_PASSES 100 + +/* NOTE: Before you even start thinking to touch anything + * in this code, set DEBUG_ROMCC_WARNINGS to 1 to get an + * insight on the original author's thoughts. We introduced + * this switch as romcc was about the only thing producing + * massive warnings in our code.. + */ +#define DEBUG_ROMCC_WARNINGS 0 -#warning "FIXME boundary cases with small types in larger registers" +#define DEBUG_CONSISTENCY 1 +#define DEBUG_SDP_BLOCKS 0 +#define DEBUG_TRIPLE_COLOR 0 + +#define DEBUG_DISPLAY_USES 1 +#define DEBUG_DISPLAY_TYPES 1 +#define DEBUG_REPLACE_CLOSURE_TYPE_HIRES 0 +#define DEBUG_DECOMPOSE_PRINT_TUPLES 0 +#define DEBUG_DECOMPOSE_HIRES 0 +#define DEBUG_INITIALIZER 0 +#define DEBUG_UPDATE_CLOSURE_TYPE 0 +#define DEBUG_LOCAL_TRIPLE 0 +#define DEBUG_BASIC_BLOCKS_VERBOSE 0 +#define DEBUG_CPS_RENAME_VARIABLES_HIRES 0 +#define DEBUG_SIMPLIFY_HIRES 0 +#define DEBUG_SHRINKING 0 +#define DEBUG_COALESCE_HITCHES 0 +#define DEBUG_CODE_ELIMINATION 0 + +#define DEBUG_EXPLICIT_CLOSURES 0 + +#if DEBUG_ROMCC_WARNINGS +#warning "FIXME give clear error messages about unused variables" +#warning "FIXME properly handle multi dimensional arrays" +#warning "FIXME handle multiple register sizes" +#endif /* Control flow graph of a loop without goto. - * + * * AAA * +---/ * / @@ -33,31 +74,31 @@ * |\ GGG HHH | continue; * | \ \ | | * | \ III | / - * | \ | / / - * | vvv / - * +----BBB / + * | \ | / / + * | vvv / + * +----BBB / * | / * vv * JJJ * - * + * * AAA * +-----+ | +----+ * | \ | / | * | BBB +-+ | * | / \ / | | * | CCC JJJ / / - * | / \ / / - * | DDD EEE / / + * | / \ / / + * | DDD EEE / / * | | +-/ / - * | FFF / - * | / \ / - * | GGG HHH / + * | FFF / + * | / \ / + * | GGG HHH / * | | +-/ * | III - * +--+ + * +--+ + * * - * * DFlocal(X) = { Y <- Succ(X) | idom(Y) != X } * DFup(Z) = { Y <- DF(Z) | idom(Y) != X } * @@ -76,7 +117,7 @@ * DDD EEE DDD: [ ] ( BBB ) EEE: [ JJJ ] () * | * FFF FFF: [ ] ( BBB ) - * / \ + * / \ * GGG HHH GGG: [ ] ( BBB ) HHH: [ BBB ] () * | * III III: [ BBB ] () @@ -84,9 +125,17 @@ * * BBB and JJJ are definitely the dominance frontier. * Where do I place phi functions and how do I make that decision. - * + * */ -static void die(char *fmt, ...) + +struct filelist { + const char *filename; + struct filelist *next; +}; + +struct filelist *include_filelist = NULL; + +static void __attribute__((noreturn)) die(char *fmt, ...) { va_list args; @@ -98,7 +147,6 @@ static void die(char *fmt, ...) exit(1); } -#define MALLOC_STRONG_DEBUG static void *xmalloc(size_t size, const char *name) { void *buf; @@ -118,6 +166,17 @@ static void *xcmalloc(size_t size, const char *name) return buf; } +static void *xrealloc(void *ptr, size_t size, const char *name) +{ + void *buf; + buf = realloc(ptr, size); + if (!buf) { + die("Cannot realloc %ld bytes to hold %s: %s\n", + size + 0UL, name, strerror(errno)); + } + return buf; +} + static void xfree(const void *ptr) { free((void *)ptr); @@ -137,55 +196,66 @@ static char *xstrdup(const char *str) static void xchdir(const char *path) { if (chdir(path) != 0) { - die("chdir to %s failed: %s\n", + die("chdir to `%s' failed: %s\n", path, strerror(errno)); } } static int exists(const char *dirname, const char *filename) { - int does_exist = 1; - xchdir(dirname); - if (access(filename, O_RDONLY) < 0) { + char cwd[MAX_CWD_SIZE]; + int does_exist; + + if (getcwd(cwd, sizeof(cwd)) == 0) { + die("cwd buffer to small"); + } + + does_exist = 1; + if (chdir(dirname) != 0) { + does_exist = 0; + } + if (does_exist && (access(filename, O_RDONLY) < 0)) { if ((errno != EACCES) && (errno != EROFS)) { does_exist = 0; } } + xchdir(cwd); return does_exist; } static char *slurp_file(const char *dirname, const char *filename, off_t *r_size) { - int fd; + char cwd[MAX_CWD_SIZE]; char *buf; off_t size, progress; ssize_t result; - struct stat stats; - + FILE* file; + if (!filename) { *r_size = 0; return 0; } + if (getcwd(cwd, sizeof(cwd)) == 0) { + die("cwd buffer to small"); + } xchdir(dirname); - fd = open(filename, O_RDONLY); - if (fd < 0) { + file = fopen(filename, "rb"); + xchdir(cwd); + if (file == NULL) { die("Cannot open '%s' : %s\n", filename, strerror(errno)); } - result = fstat(fd, &stats); - if (result < 0) { - die("Cannot stat: %s: %s\n", - filename, strerror(errno)); - } - size = stats.st_size; + fseek(file, 0, SEEK_END); + size = ftell(file); + fseek(file, 0, SEEK_SET); *r_size = size +1; buf = xmalloc(size +2, filename); buf[size] = '\n'; /* Make certain the file is newline terminated */ buf[size+1] = '\0'; /* Null terminate the file for good measure */ progress = 0; while(progress < size) { - result = read(fd, buf + progress, size - progress); + result = fread(buf + progress, 1, size - progress, file); if (result < 0) { if ((errno == EINTR) || (errno == EAGAIN)) continue; @@ -194,36 +264,104 @@ static char *slurp_file(const char *dirname, const char *filename, off_t *r_size } progress += result; } - result = close(fd); - if (result < 0) { - die("Close of %s failed: %s\n", - filename, strerror(errno)); - } + fclose(file); return buf; } -/* Long on the destination platform */ -typedef unsigned long ulong_t; -typedef long long_t; +/* Types on the destination platform */ +#if DEBUG_ROMCC_WARNINGS +#warning "FIXME this assumes 32bit x86 is the destination" +#endif +typedef int8_t schar_t; +typedef uint8_t uchar_t; +typedef int8_t char_t; +typedef int16_t short_t; +typedef uint16_t ushort_t; +typedef int32_t int_t; +typedef uint32_t uint_t; +typedef int32_t long_t; +#define ulong_t uint32_t + +#define SCHAR_T_MIN (-128) +#define SCHAR_T_MAX 127 +#define UCHAR_T_MAX 255 +#define CHAR_T_MIN SCHAR_T_MIN +#define CHAR_T_MAX SCHAR_T_MAX +#define SHRT_T_MIN (-32768) +#define SHRT_T_MAX 32767 +#define USHRT_T_MAX 65535 +#define INT_T_MIN (-LONG_T_MAX - 1) +#define INT_T_MAX 2147483647 +#define UINT_T_MAX 4294967295U +#define LONG_T_MIN (-LONG_T_MAX - 1) +#define LONG_T_MAX 2147483647 +#define ULONG_T_MAX 4294967295U + +#define SIZEOF_I8 8 +#define SIZEOF_I16 16 +#define SIZEOF_I32 32 +#define SIZEOF_I64 64 + +#define SIZEOF_CHAR 8 +#define SIZEOF_SHORT 16 +#define SIZEOF_INT 32 +#define SIZEOF_LONG (sizeof(long_t)*SIZEOF_CHAR) + + +#define ALIGNOF_CHAR 8 +#define ALIGNOF_SHORT 16 +#define ALIGNOF_INT 32 +#define ALIGNOF_LONG (sizeof(long_t)*SIZEOF_CHAR) + +#define REG_SIZEOF_REG 32 +#define REG_SIZEOF_CHAR REG_SIZEOF_REG +#define REG_SIZEOF_SHORT REG_SIZEOF_REG +#define REG_SIZEOF_INT REG_SIZEOF_REG +#define REG_SIZEOF_LONG REG_SIZEOF_REG + +#define REG_ALIGNOF_REG REG_SIZEOF_REG +#define REG_ALIGNOF_CHAR REG_SIZEOF_REG +#define REG_ALIGNOF_SHORT REG_SIZEOF_REG +#define REG_ALIGNOF_INT REG_SIZEOF_REG +#define REG_ALIGNOF_LONG REG_SIZEOF_REG + +/* Additional definitions for clarity. + * I currently assume a long is the largest native + * machine word and that a pointer fits into it. + */ +#define SIZEOF_WORD SIZEOF_LONG +#define SIZEOF_POINTER SIZEOF_LONG +#define ALIGNOF_WORD ALIGNOF_LONG +#define ALIGNOF_POINTER ALIGNOF_LONG +#define REG_SIZEOF_POINTER REG_SIZEOF_LONG +#define REG_ALIGNOF_POINTER REG_ALIGNOF_LONG struct file_state { struct file_state *prev; const char *basename; char *dirname; - char *buf; + const char *buf; off_t size; - char *pos; + const char *pos; int line; - char *line_start; + const char *line_start; + int report_line; + const char *report_name; + const char *report_dir; + int macro : 1; + int trigraphs : 1; + int join_lines : 1; }; struct hash_entry; struct token { int tok; struct hash_entry *ident; + const char *pos; int str_len; union { ulong_t integer; const char *str; + int notmacro; } val; }; @@ -256,24 +394,26 @@ 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 #define OP_SLESS 22 @@ -284,19 +424,48 @@ struct token { #define OP_ULESSEQ 27 #define OP_SMOREEQ 28 #define OP_UMOREEQ 29 - + #define OP_LFALSE 30 /* Test if the expression is logically false */ #define OP_LTRUE 31 /* Test if the expression is logcially true */ #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_UEXTRACT 34 +/* OP_UEXTRACT extracts an unsigned bitfield from a pseudo register + * RHS(0) holds the psuedo register to extract from + * ->type holds the size of the bitfield. + * ->u.bitfield.size holds the size of the bitfield. + * ->u.bitfield.offset holds the offset to extract from + */ +#define OP_SEXTRACT 35 +/* OP_SEXTRACT extracts a signed bitfield from a pseudo register + * RHS(0) holds the psuedo register to extract from + * ->type holds the size of the bitfield. + * ->u.bitfield.size holds the size of the bitfield. + * ->u.bitfield.offset holds the offset to extract from + */ +#define OP_DEPOSIT 36 +/* OP_DEPOSIT replaces a bitfield with a new value. + * RHS(0) holds the value to replace a bitifield in. + * RHS(1) holds the replacement value + * ->u.bitfield.size holds the size of the bitfield. + * ->u.bitfield.offset holds the deposit into + */ -#define OP_NOOP 34 +#define OP_NOOP 37 #define OP_MIN_CONST 50 -#define OP_MAX_CONST 59 +#define OP_MAX_CONST 58 #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 ->type holds the layout and size * information. u.blob holds a pointer to the raw binary @@ -307,10 +476,16 @@ struct token { * MISC(0) holds the reference to the static variable. * ->u.cval holds an offset from that value. */ +#define OP_UNKNOWNVAL 59 +/* For OP_UNKNOWNAL ->type holds the type. + * For some reason we don't know what value this type has. + * This allows for variables that have don't have values + * assigned yet, or variables whose value we simply do not know. + */ -#define OP_WRITE 60 +#define OP_WRITE 60 /* OP_WRITE moves one pseudo register to another. - * LHS(0) holds the destination pseudo register, which must be an OP_DECL. + * MISC(0) holds the destination pseudo register, which must be an OP_DECL. * RHS(0) holds the psuedo to move. */ @@ -321,14 +496,18 @@ struct token { * 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 RHS(0). +/* OP_COPY makes a copy of the pseudo register or constant in RHS(0). + */ +#define OP_CONVERT 63 +/* OP_CONVERT makes a copy of the pseudo register or constant in RHS(0). + * And then the type is converted appropriately. */ -#define OP_PIECE 63 +#define OP_PIECE 64 /* 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 +#define OP_ASM 65 /* 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. @@ -336,20 +515,27 @@ struct token { * u.blob holds the string of assembly instructions. */ -#define OP_DEREF 65 +#define OP_DEREF 66 /* OP_DEREF generates an lvalue from a pointer. * 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 +#define OP_DOT 67 /* OP_DOT references a submember of a structure lvalue. - * RHS(0) holds the lvalue. + * MISC(0) holds the lvalue. * ->u.field holds the name of the field we want. * - * Not seen outside of expressions. + * Not seen after structures are flattened. + */ +#define OP_INDEX 68 +/* OP_INDEX references a submember of a tuple or array lvalue. + * MISC(0) holds the lvalue. + * ->u.cval holds the index into the lvalue. + * + * Not seen after structures are flattened. */ -#define OP_VAL 67 +#define OP_VAL 69 /* OP_VAL returns the value of a subexpression of the current expression. * Useful for operators that have side effects. * RHS(0) holds the expression. @@ -358,68 +544,97 @@ struct token { * * Not seen outside of expressions. */ -#define OP_LAND 68 -/* 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 RHS(0) and RHS(1). - * Not seen outside of expressions. - */ -#define OP_COND 70 -/* OP_CODE performas a C ? : operation. - * 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_TUPLE 70 +/* OP_TUPLE is an array of triples that are either variable + * or values for a structure or an array. It is used as + * a place holder when flattening compound types. + * The value represented by an OP_TUPLE is held in N registers. + * LHS(0..N-1) refer to those registers. + * ->use is a list of statements that use the value. + * + * Although OP_TUPLE always has register sized pieces they are not + * used until structures are flattened/decomposed into their register + * components. + * ???? registers ???? */ -#define OP_COMMA 71 -/* OP_COMMA performacs a C comma operation. - * That is RHS(0) is evaluated, then RHS(1) - * and the value of RHS(1) is returned. - * Not seen outside of expressions. + +#define OP_BITREF 71 +/* OP_BITREF describes a bitfield as an lvalue. + * RHS(0) holds the register value. + * ->type holds the type of the bitfield. + * ->u.bitfield.size holds the size of the bitfield. + * ->u.bitfield.offset holds the offset of the bitfield in the register */ -#define OP_CALL 72 -/* OP_CALL performs a procedure call. + +#define OP_FCALL 72 +/* OP_FCALL performs a procedure call. * 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_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. +#define OP_PROG 73 +/* OP_PROG is an expression that holds a list of statements, or + * expressions. The final expression is the value of the expression. + * RHS(0) holds the start of the list. */ /* statements */ #define OP_LIST 80 -/* OP_LIST Holds a list of statements, and a result value. +/* OP_LIST Holds a list of statements that compose a function, and a result value. * RHS(0) holds the list of statements. - * MISC(0) holds the value of the statements. + * A list of all functions is maintained. */ -#define OP_BRANCH 81 /* branch */ +#define OP_BRANCH 81 /* an unconditional branch */ /* For branch instructions * 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... + * The branch target can only be a label + */ + +#define OP_CBRANCH 82 /* a conditional branch */ +/* For conditional branch instructions + * RHS(0) holds the branch condition. + * TARG(0) holds the branch target. + * ->next holds where to branch to if the branch is not taken. + * The branch target can only be a label */ -#define OP_LABEL 83 +#define OP_CALL 83 /* an uncontional branch that will return */ +/* For call instructions + * MISC(0) holds the OP_RET that returns from the branch + * TARG(0) holds the branch target. + * ->next holds where to branch to if the branch is not taken. + * The branch target can only be a label + */ + +#define OP_RET 84 /* an uncontinonal branch through a variable back to an OP_CALL */ +/* For call instructions + * RHS(0) holds the variable with the return address + * The branch target can only be a label + */ + +#define OP_LABEL 86 /* OP_LABEL is a triple that establishes an target for branches. * ->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. +#define OP_ADECL 87 +/* OP_ADECL is a triple that establishes an lvalue for assignments. + * A variable takes N registers to contain. + * LHS(0..N-1) refer to an OP_PIECE triple that represents + * the Xth register that the variable is stored in. * ->use is a list of statements that use the variable. + * + * Although OP_ADECL always has register sized pieces they are not + * used until structures are flattened/decomposed into their register + * components. */ -#define OP_SDECL 85 +#define OP_SDECL 88 /* OP_SDECL is a triple that establishes a variable of static * storage duration. * ->use is a list of statements that use the variable. @@ -427,13 +642,13 @@ struct token { */ -#define OP_PHI 86 -/* OP_PHI is a triple used in SSA form code. +#define OP_PHI 89 +/* OP_PHI is a triple used in SSA form code. * It is used when multiple code paths merge and a variable needs * a single assignment from any of those code paths. * The operation is a cross between OP_DECL and OP_WRITE, which - * is what OP_PHI is geneared from. - * + * is what OP_PHI is generated from. + * * 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 @@ -442,6 +657,46 @@ struct token { * MISC(0) holds a pointer to the orginal OP_DECL node. */ +#if 0 +/* continuation helpers + */ +#define OP_CPS_BRANCH 90 /* an unconditional branch */ +/* OP_CPS_BRANCH calls a continuation + * RHS(x) holds argument x of the function + * TARG(0) holds OP_CPS_START target + */ +#define OP_CPS_CBRANCH 91 /* a conditional branch */ +/* OP_CPS_CBRANCH conditionally calls one of two continuations + * RHS(0) holds the branch condition + * RHS(x + 1) holds argument x of the function + * TARG(0) holds the OP_CPS_START to jump to when true + * ->next holds the OP_CPS_START to jump to when false + */ +#define OP_CPS_CALL 92 /* an uncontional branch that will return */ +/* For OP_CPS_CALL instructions + * RHS(x) holds argument x of the function + * MISC(0) holds the OP_CPS_RET that returns from the branch + * TARG(0) holds the branch target. + * ->next holds where the OP_CPS_RET will return to. + */ +#define OP_CPS_RET 93 +/* OP_CPS_RET conditionally calls one of two continuations + * RHS(0) holds the variable with the return function address + * RHS(x + 1) holds argument x of the function + * The branch target may be any OP_CPS_START + */ +#define OP_CPS_END 94 +/* OP_CPS_END is the triple at the end of the program. + * For most practical purposes it is a branch. + */ +#define OP_CPS_START 95 +/* OP_CPS_START is a triple at the start of a continuation + * The arguments variables takes N registers to contain. + * LHS(0..N-1) refer to an OP_PIECE triple that represents + * the Xth register that the arguments are stored in. + */ +#endif + /* Architecture specific instructions */ #define OP_CMP 100 #define OP_TEST 101 @@ -484,12 +739,21 @@ struct token { struct op_info { const char *name; unsigned flags; -#define PURE 1 -#define IMPURE 2 +#define PURE 0x001 /* Triple has no side effects */ +#define IMPURE 0x002 /* Triple has side effects */ #define PURE_BITS(FLAGS) ((FLAGS) & 0x3) -#define DEF 4 -#define BLOCK 8 /* Triple stores the current block */ - unsigned char lhs, rhs, misc, targ; +#define DEF 0x004 /* Triple is a variable definition */ +#define BLOCK 0x008 /* Triple stores the current block */ +#define STRUCTURAL 0x010 /* Triple does not generate a machine instruction */ +#define BRANCH_BITS(FLAGS) ((FLAGS) & 0xe0 ) +#define UBRANCH 0x020 /* Triple is an unconditional branch instruction */ +#define CBRANCH 0x040 /* Triple is a conditional branch instruction */ +#define RETBRANCH 0x060 /* Triple is a return instruction */ +#define CALLBRANCH 0x080 /* Triple is a call instruction */ +#define ENDBRANCH 0x0a0 /* Triple is an end instruction */ +#define PART 0x100 /* Triple is really part of another triple */ +#define BITFIELD 0x200 /* Triple manipulates a bitfield */ + signed char lhs, rhs, misc, targ; }; #define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \ @@ -501,6 +765,8 @@ struct op_info { .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"), @@ -532,42 +798,61 @@ static const struct op_info table_ops[] = { [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( 1, 1, 0, 0, IMPURE | BLOCK , "store"), +[OP_LOAD ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "load"), +[OP_STORE ] = OP( 0, 2, 0, 0, PURE | BLOCK , "store"), + +[OP_UEXTRACT ] = OP( 0, 1, 0, 0, PURE | DEF | BITFIELD, "uextract"), +[OP_SEXTRACT ] = OP( 0, 1, 0, 0, PURE | DEF | BITFIELD, "sextract"), +[OP_DEPOSIT ] = OP( 0, 2, 0, 0, PURE | DEF | BITFIELD, "deposit"), -[OP_NOOP ] = OP( 0, 0, 0, 0, PURE | BLOCK, "noop"), +[OP_NOOP ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "noop"), [OP_INTCONST ] = OP( 0, 0, 0, 0, PURE | DEF, "intconst"), -[OP_BLOBCONST ] = OP( 0, 0, 0, 0, PURE, "blobconst"), +[OP_BLOBCONST ] = OP( 0, 0, 0, 0, PURE , "blobconst"), [OP_ADDRCONST ] = OP( 0, 0, 1, 0, PURE | DEF, "addrconst"), +[OP_UNKNOWNVAL ] = OP( 0, 0, 0, 0, PURE | DEF, "unknown"), -[OP_WRITE ] = OP( 1, 1, 0, 0, PURE | BLOCK, "write"), +#if DEBUG_ROMCC_WARNINGS +#warning "FIXME is it correct for OP_WRITE to be a def? I currently use it as one..." +#endif +[OP_WRITE ] = OP( 0, 1, 1, 0, PURE | DEF | 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_CONVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "convert"), +[OP_PIECE ] = OP( 0, 0, 1, 0, PURE | DEF | STRUCTURAL | PART, "piece"), +[OP_ASM ] = OP(-1, -1, 0, 0, PURE, "asm"), +[OP_DEREF ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "deref"), +[OP_DOT ] = OP( 0, 0, 1, 0, PURE | DEF | PART, "dot"), +[OP_INDEX ] = OP( 0, 0, 1, 0, PURE | DEF | PART, "index"), [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"), +[OP_TUPLE ] = OP(-1, 0, 0, 0, 0 | PURE | BLOCK | STRUCTURAL, "tuple"), +[OP_BITREF ] = OP( 0, 1, 0, 0, 0 | DEF | PURE | STRUCTURAL | BITFIELD, "bitref"), /* 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"), +[OP_FCALL ] = OP( 0, -1, 1, 0, 0 | BLOCK | CALLBRANCH, "fcall"), +[OP_PROG ] = OP( 0, 1, 0, 0, 0 | IMPURE | BLOCK | STRUCTURAL, "prog"), +/* The sizes of OP_FCALL depends upon context */ + +[OP_LIST ] = OP( 0, 1, 1, 0, 0 | DEF | STRUCTURAL, "list"), +[OP_BRANCH ] = OP( 0, 0, 0, 1, PURE | BLOCK | UBRANCH, "branch"), +[OP_CBRANCH ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "cbranch"), +[OP_CALL ] = OP( 0, 0, 1, 1, PURE | BLOCK | CALLBRANCH, "call"), +[OP_RET ] = OP( 0, 1, 0, 0, PURE | BLOCK | RETBRANCH, "ret"), +[OP_LABEL ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "label"), +[OP_ADECL ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "adecl"), +[OP_SDECL ] = OP( 0, 0, 1, 0, PURE | BLOCK | STRUCTURAL, "sdecl"), /* The number of RHS elements of OP_PHI depend upon context */ [OP_PHI ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"), +#if 0 +[OP_CPS_BRANCH ] = OP( 0, -1, 0, 1, PURE | BLOCK | UBRANCH, "cps_branch"), +[OP_CPS_CBRANCH] = OP( 0, -1, 0, 1, PURE | BLOCK | CBRANCH, "cps_cbranch"), +[OP_CPS_CALL ] = OP( 0, -1, 1, 1, PURE | BLOCK | CALLBRANCH, "cps_call"), +[OP_CPS_RET ] = OP( 0, -1, 0, 0, PURE | BLOCK | RETBRANCH, "cps_ret"), +[OP_CPS_END ] = OP( 0, -1, 0, 0, IMPURE | BLOCK | ENDBRANCH, "cps_end"), +[OP_CPS_START ] = OP( -1, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "cps_start"), +#endif + [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"), @@ -580,17 +865,17 @@ static const struct op_info table_ops[] = { [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_JMP ] = OP( 0, 0, 0, 1, PURE | BLOCK | UBRANCH, "jmp"), +[OP_JMP_EQ ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_eq"), +[OP_JMP_NOTEQ ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_noteq"), +[OP_JMP_SLESS ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_sless"), +[OP_JMP_ULESS ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_uless"), +[OP_JMP_SMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_smore"), +[OP_JMP_UMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_umore"), +[OP_JMP_SLESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_slesseq"), +[OP_JMP_ULESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_ulesseq"), +[OP_JMP_SMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_smoreq"), +[OP_JMP_UMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_umoreq"), [OP_INB ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inb"), [OP_INW ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inw"), @@ -607,7 +892,7 @@ static const struct op_info table_ops[] = { #undef OP #define OP_MAX (sizeof(table_ops)/sizeof(table_ops[0])) -static const char *tops(int index) +static const char *tops(int index) { static const char unknown[] = "unknown op"; if (index < 0) { @@ -627,53 +912,62 @@ struct triple_set { struct triple *member; }; -#define MAX_LHS 15 -#define MAX_RHS 15 -#define MAX_MISC 15 -#define MAX_TARG 15 +#define MAX_LHS 63 +#define MAX_RHS 127 +#define MAX_MISC 3 +#define MAX_TARG 1 +struct occurance { + int count; + const char *filename; + const char *function; + int line; + int col; + struct occurance *parent; +}; +struct bitfield { + ulong_t size : 8; + ulong_t offset : 24; +}; struct triple { struct triple *next, *prev; struct triple_set *use; struct type *type; - unsigned char op; - unsigned char template_id; - unsigned short sizes; -#define TRIPLE_LHS(SIZES) (((SIZES) >> 0) & 0x0f) -#define TRIPLE_RHS(SIZES) (((SIZES) >> 4) & 0x0f) -#define TRIPLE_MISC(SIZES) (((SIZES) >> 8) & 0x0f) -#define TRIPLE_TARG(SIZES) (((SIZES) >> 12) & 0x0f) -#define TRIPLE_SIZE(SIZES) \ - ((((SIZES) >> 0) & 0x0f) + \ - (((SIZES) >> 4) & 0x0f) + \ - (((SIZES) >> 8) & 0x0f) + \ - (((SIZES) >> 12) & 0x0f)) -#define TRIPLE_SIZES(LHS, RHS, MISC, TARG) \ - ((((LHS) & 0x0f) << 0) | \ - (((RHS) & 0x0f) << 4) | \ - (((MISC) & 0x0f) << 8) | \ - (((TARG) & 0x0f) << 12)) -#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 int op : 8; + unsigned int template_id : 7; + unsigned int lhs : 6; + unsigned int rhs : 7; + unsigned int misc : 2; + unsigned int targ : 1; +#define TRIPLE_SIZE(TRIPLE) \ + ((TRIPLE)->lhs + (TRIPLE)->rhs + (TRIPLE)->misc + (TRIPLE)->targ) +#define TRIPLE_LHS_OFF(PTR) (0) +#define TRIPLE_RHS_OFF(PTR) (TRIPLE_LHS_OFF(PTR) + (PTR)->lhs) +#define TRIPLE_MISC_OFF(PTR) (TRIPLE_RHS_OFF(PTR) + (PTR)->rhs) +#define TRIPLE_TARG_OFF(PTR) (TRIPLE_MISC_OFF(PTR) + (PTR)->misc) +#define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF(PTR) + (INDEX)]) +#define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF(PTR) + (INDEX)]) +#define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF(PTR) + (INDEX)]) +#define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF(PTR) + (INDEX)]) unsigned id; /* A scratch value and finally the register */ #define TRIPLE_FLAG_FLATTENED (1 << 31) #define TRIPLE_FLAG_PRE_SPLIT (1 << 30) #define TRIPLE_FLAG_POST_SPLIT (1 << 29) - const char *filename; - int line; - int col; +#define TRIPLE_FLAG_VOLATILE (1 << 28) +#define TRIPLE_FLAG_INLINE (1 << 27) /* ???? */ +#define TRIPLE_FLAG_LOCAL (1 << 26) + +#define TRIPLE_FLAG_COPY TRIPLE_FLAG_VOLATILE + struct occurance *occurance; union { ulong_t cval; + struct bitfield bitfield; struct block *block; void *blob; struct hash_entry *field; struct asm_info *ainfo; + struct triple *func; + struct symbol *symbol; } u; struct triple *param[2]; }; @@ -697,8 +991,9 @@ struct block_set { }; struct block { struct block *work_next; - struct block *left, *right; struct triple *first, *last; + int edge_count; + struct block_set *edges; int users; struct block_set *use; struct block_set *idominates; @@ -708,7 +1003,7 @@ struct block { struct block_set *ipdomfrontier; struct block *ipdom; int vertex; - + }; struct symbol { @@ -719,10 +1014,16 @@ struct symbol { int scope_depth; }; +struct macro_arg { + struct macro_arg *next; + struct hash_entry *ident; +}; struct macro { struct hash_entry *ident; - char *buf; + const char *buf; int buf_len; + struct macro_arg *args; + int argc; }; struct hash_entry { @@ -732,66 +1033,115 @@ struct hash_entry { int tok; struct macro *sym_define; struct symbol *sym_label; - struct symbol *sym_struct; + struct symbol *sym_tag; struct symbol *sym_ident; }; #define HASH_TABLE_SIZE 2048 -struct compile_state { +struct compiler_state { const char *label_prefix; const char *ofilename; + unsigned long flags; + unsigned long debug; + unsigned long max_allocation_passes; + + size_t include_path_count; + const char **include_paths; + + size_t define_count; + const char **defines; + + size_t undef_count; + const char **undefs; +}; +struct arch_state { + unsigned long features; +}; +struct basic_blocks { + struct triple *func; + struct triple *first; + struct block *first_block, *last_block; + int last_vertex; +}; +#define MAX_PP_IF_DEPTH 63 +struct compile_state { + struct compiler_state *compiler; + struct arch_state *arch; FILE *output; - struct triple *vars; + FILE *errout; + FILE *dbgout; struct file_state *file; - struct token token[4]; + struct occurance *last_occurance; + const char *function; + int token_base; + struct token token[6]; struct hash_entry *hash_table[HASH_TABLE_SIZE]; + struct hash_entry *i_switch; + struct hash_entry *i_case; struct hash_entry *i_continue; struct hash_entry *i_break; + struct hash_entry *i_default; + struct hash_entry *i_return; + struct hash_entry *i_noreturn; + /* Additional hash entries for predefined macros */ + struct hash_entry *i_defined; + struct hash_entry *i___VA_ARGS__; + struct hash_entry *i___FILE__; + struct hash_entry *i___LINE__; + /* Additional hash entries for predefined identifiers */ + struct hash_entry *i___func__; + /* Additional hash entries for attributes */ + struct hash_entry *i_noinline; + struct hash_entry *i_always_inline; int scope_depth; - int if_depth, if_value; - int macro_line; + unsigned char if_bytes[(MAX_PP_IF_DEPTH + CHAR_BIT -1)/CHAR_BIT]; + int if_depth; + int eat_depth, eat_targ; struct file_state *macro_file; + struct triple *functions; struct triple *main_function; - struct block *first_block, *last_block; - int last_vertex; - int cpu; - int debug; - int optimize; + struct triple *first; + struct triple *global_pool; + struct basic_blocks bb; + int functions_joined; }; /* visibility global/local */ /* static/auto duration */ /* typedef, register, inline */ #define STOR_SHIFT 0 -#define STOR_MASK 0x000f +#define STOR_MASK 0x001f /* Visibility */ #define STOR_GLOBAL 0x0001 /* Duration */ #define STOR_PERM 0x0002 +/* Definition locality */ +#define STOR_NONLOCAL 0x0004 /* The definition is not in this translation unit */ /* 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 STOR_LOCAL 0x0003 +#define STOR_EXTERN 0x0007 +#define STOR_INLINE 0x0008 +#define STOR_REGISTER 0x0010 +#define STOR_TYPEDEF 0x0018 + +#define QUAL_SHIFT 5 +#define QUAL_MASK 0x00e0 #define QUAL_NONE 0x0000 -#define QUAL_CONST 0x0010 -#define QUAL_VOLATILE 0x0020 -#define QUAL_RESTRICT 0x0040 +#define QUAL_CONST 0x0020 +#define QUAL_VOLATILE 0x0040 +#define QUAL_RESTRICT 0x0080 #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_INTEGER(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD)) +#define TYPE_ARITHMETIC(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD)) #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_MKUNSIGNED(TYPE) (((TYPE) & ~0xF000) | 0x0100) +#define TYPE_RANK(TYPE) ((TYPE) & ~0xF1FF) #define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER) #define TYPE_DEFAULT 0x0000 #define TYPE_VOID 0x0100 @@ -808,16 +1158,36 @@ struct compile_state { #define TYPE_FLOAT 0x0c00 #define TYPE_DOUBLE 0x0d00 #define TYPE_LDOUBLE 0x0e00 /* long double */ + +/* Note: TYPE_ENUM is chosen very carefully so TYPE_RANK works */ +#define TYPE_ENUM 0x1600 +#define TYPE_LIST 0x1700 +/* TYPE_LIST is a basic building block when defining enumerations + * type->field_ident holds the name of this enumeration entry. + * type->right holds the entry in the list. + */ + #define TYPE_STRUCT 0x1000 -#define TYPE_ENUM 0x1100 -#define TYPE_POINTER 0x1200 +/* For TYPE_STRUCT + * type->left holds the link list of TYPE_PRODUCT entries that + * make up the structure. + * type->elements hold the length of the linked list + */ +#define TYPE_UNION 0x1100 +/* For TYPE_UNION + * type->left holds the link list of TYPE_OVERLAP entries that + * make up the union. + * type->elements hold the length of the linked list + */ +#define TYPE_POINTER 0x1200 /* For TYPE_POINTER: * type->left holds the type pointed to. */ -#define TYPE_FUNCTION 0x1300 +#define TYPE_FUNCTION 0x1300 /* For TYPE_FUNCTION: * type->left holds the return type. - * type->right holds the... + * type->right holds the type of the arguments + * type->elements holds the count of the arguments */ #define TYPE_PRODUCT 0x1400 /* TYPE_PRODUCT is a basic building block when defining structures @@ -829,13 +1199,45 @@ struct compile_state { * type->left and type->right holds to types that overlap * each other in memory. */ -#define TYPE_ARRAY 0x1600 +#define TYPE_ARRAY 0x1800 /* 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. + * type->elements holds the number of elements. + */ +#define TYPE_TUPLE 0x1900 +/* TYPE_TUPLE is a basic building block when defining + * positionally reference type conglomerations. (i.e. closures) + * In essence it is a wrapper for TYPE_PRODUCT, like TYPE_STRUCT + * except it has no field names. + * type->left holds the liked list of TYPE_PRODUCT entries that + * make up the closure type. + * type->elements hold the number of elements in the closure. + */ +#define TYPE_JOIN 0x1a00 +/* TYPE_JOIN is a basic building block when defining + * positionally reference type conglomerations. (i.e. closures) + * In essence it is a wrapper for TYPE_OVERLAP, like TYPE_UNION + * except it has no field names. + * type->left holds the liked list of TYPE_OVERLAP entries that + * make up the closure type. + * type->elements hold the number of elements in the closure. + */ +#define TYPE_BITFIELD 0x1b00 +/* TYPE_BITFIED is the type of a bitfield. + * type->left holds the type basic type TYPE_BITFIELD is derived from. + * type->elements holds the number of bits in the bitfield. + */ +#define TYPE_UNKNOWN 0x1c00 +/* TYPE_UNKNOWN is the type of an unknown value. + * Used on unknown consts and other places where I don't know the type. */ -#define ELEMENT_COUNT_UNSPECIFIED (~0UL) +#define ATTRIB_SHIFT 16 +#define ATTRIB_MASK 0xffff0000 +#define ATTRIB_NOINLINE 0x00010000 +#define ATTRIB_ALWAYS_INLINE 0x00020000 + +#define ELEMENT_COUNT_UNSPECIFIED ULONG_T_MAX struct type { unsigned int type; @@ -845,29 +1247,54 @@ struct type { struct hash_entry *type_ident; }; -#define MAX_REGISTERS 75 +#define TEMPLATE_BITS 7 +#define MAX_TEMPLATES (1< MAX_VIRT_REGISTERS +#error "MAX_VIRT_REGISTERS to small" +#endif +#if (MAX_REGC + REGISTER_BITS) >= 26 +#error "Too many id bits used" +#endif /* Provision for 8 register classes */ +#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))) +#define ARCH_INPUT_REGS 4 +#define ARCH_OUTPUT_REGS 4 + +static const struct reg_info arch_input_regs[ARCH_INPUT_REGS]; +static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS]; 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( @@ -880,49 +1307,442 @@ 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, +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, +static struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index); +static int arch_reg_size(int reg); static struct triple *transform_to_arch_instruction( struct compile_state *state, struct triple *ins); +static struct triple *flatten( + struct compile_state *state, struct triple *first, struct triple *ptr); +static void print_dominators(struct compile_state *state, + FILE *fp, struct basic_blocks *bb); +static void print_dominance_frontiers(struct compile_state *state, + FILE *fp, struct basic_blocks *bb); + + + +#define DEBUG_ABORT_ON_ERROR 0x00000001 +#define DEBUG_BASIC_BLOCKS 0x00000002 +#define DEBUG_FDOMINATORS 0x00000004 +#define DEBUG_RDOMINATORS 0x00000008 +#define DEBUG_TRIPLES 0x00000010 +#define DEBUG_INTERFERENCE 0x00000020 +#define DEBUG_SCC_TRANSFORM 0x00000040 +#define DEBUG_SCC_TRANSFORM2 0x00000080 +#define DEBUG_REBUILD_SSA_FORM 0x00000100 +#define DEBUG_INLINE 0x00000200 +#define DEBUG_RANGE_CONFLICTS 0x00000400 +#define DEBUG_RANGE_CONFLICTS2 0x00000800 +#define DEBUG_COLOR_GRAPH 0x00001000 +#define DEBUG_COLOR_GRAPH2 0x00002000 +#define DEBUG_COALESCING 0x00004000 +#define DEBUG_COALESCING2 0x00008000 +#define DEBUG_VERIFICATION 0x00010000 +#define DEBUG_CALLS 0x00020000 +#define DEBUG_CALLS2 0x00040000 +#define DEBUG_TOKENS 0x80000000 + +#define DEBUG_DEFAULT ( \ + DEBUG_ABORT_ON_ERROR | \ + DEBUG_BASIC_BLOCKS | \ + DEBUG_FDOMINATORS | \ + DEBUG_RDOMINATORS | \ + DEBUG_TRIPLES | \ + 0 ) + +#define DEBUG_ALL ( \ + DEBUG_ABORT_ON_ERROR | \ + DEBUG_BASIC_BLOCKS | \ + DEBUG_FDOMINATORS | \ + DEBUG_RDOMINATORS | \ + DEBUG_TRIPLES | \ + DEBUG_INTERFERENCE | \ + DEBUG_SCC_TRANSFORM | \ + DEBUG_SCC_TRANSFORM2 | \ + DEBUG_REBUILD_SSA_FORM | \ + DEBUG_INLINE | \ + DEBUG_RANGE_CONFLICTS | \ + DEBUG_RANGE_CONFLICTS2 | \ + DEBUG_COLOR_GRAPH | \ + DEBUG_COLOR_GRAPH2 | \ + DEBUG_COALESCING | \ + DEBUG_COALESCING2 | \ + DEBUG_VERIFICATION | \ + DEBUG_CALLS | \ + DEBUG_CALLS2 | \ + DEBUG_TOKENS | \ + 0 ) + +#define COMPILER_INLINE_MASK 0x00000007 +#define COMPILER_INLINE_ALWAYS 0x00000000 +#define COMPILER_INLINE_NEVER 0x00000001 +#define COMPILER_INLINE_DEFAULTON 0x00000002 +#define COMPILER_INLINE_DEFAULTOFF 0x00000003 +#define COMPILER_INLINE_NOPENALTY 0x00000004 +#define COMPILER_ELIMINATE_INEFECTUAL_CODE 0x00000008 +#define COMPILER_SIMPLIFY 0x00000010 +#define COMPILER_SCC_TRANSFORM 0x00000020 +#define COMPILER_SIMPLIFY_OP 0x00000040 +#define COMPILER_SIMPLIFY_PHI 0x00000080 +#define COMPILER_SIMPLIFY_LABEL 0x00000100 +#define COMPILER_SIMPLIFY_BRANCH 0x00000200 +#define COMPILER_SIMPLIFY_COPY 0x00000400 +#define COMPILER_SIMPLIFY_ARITH 0x00000800 +#define COMPILER_SIMPLIFY_SHIFT 0x00001000 +#define COMPILER_SIMPLIFY_BITWISE 0x00002000 +#define COMPILER_SIMPLIFY_LOGICAL 0x00004000 +#define COMPILER_SIMPLIFY_BITFIELD 0x00008000 + +#define COMPILER_TRIGRAPHS 0x40000000 +#define COMPILER_PP_ONLY 0x80000000 + +#define COMPILER_DEFAULT_FLAGS ( \ + COMPILER_TRIGRAPHS | \ + COMPILER_ELIMINATE_INEFECTUAL_CODE | \ + COMPILER_INLINE_DEFAULTON | \ + COMPILER_SIMPLIFY_OP | \ + COMPILER_SIMPLIFY_PHI | \ + COMPILER_SIMPLIFY_LABEL | \ + COMPILER_SIMPLIFY_BRANCH | \ + COMPILER_SIMPLIFY_COPY | \ + COMPILER_SIMPLIFY_ARITH | \ + COMPILER_SIMPLIFY_SHIFT | \ + COMPILER_SIMPLIFY_BITWISE | \ + COMPILER_SIMPLIFY_LOGICAL | \ + COMPILER_SIMPLIFY_BITFIELD | \ + 0 ) + +#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); -#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 +static void init_compiler_state(struct compiler_state *compiler) +{ + memset(compiler, 0, sizeof(*compiler)); + compiler->label_prefix = ""; + compiler->ofilename = "auto.inc"; + compiler->flags = COMPILER_DEFAULT_FLAGS; + compiler->debug = 0; + compiler->max_allocation_passes = MAX_ALLOCATION_PASSES; + compiler->include_path_count = 1; + compiler->include_paths = xcmalloc(sizeof(char *), "include_paths"); + compiler->define_count = 1; + compiler->defines = xcmalloc(sizeof(char *), "defines"); + compiler->undef_count = 1; + compiler->undefs = xcmalloc(sizeof(char *), "undefs"); +} -#define GLOBAL_SCOPE_DEPTH 1 +struct compiler_flag { + const char *name; + unsigned long flag; +}; -static void compile_file(struct compile_state *old_state, const char *filename, int local); +struct compiler_arg { + const char *name; + unsigned long mask; + struct compiler_flag flags[16]; +}; + +static int set_flag( + const struct compiler_flag *ptr, unsigned long *flags, + int act, const char *flag) +{ + int result = -1; + for(; ptr->name; ptr++) { + if (strcmp(ptr->name, flag) == 0) { + break; + } + } + if (ptr->name) { + result = 0; + *flags &= ~(ptr->flag); + if (act) { + *flags |= ptr->flag; + } + } + return result; +} + +static int set_arg( + const struct compiler_arg *ptr, unsigned long *flags, const char *arg) +{ + const char *val; + int result = -1; + int len; + val = strchr(arg, '='); + if (val) { + len = val - arg; + val++; + for(; ptr->name; ptr++) { + if (strncmp(ptr->name, arg, len) == 0) { + break; + } + } + if (ptr->name) { + *flags &= ~ptr->mask; + result = set_flag(&ptr->flags[0], flags, 1, val); + } + } + return result; +} + + +static void flag_usage(FILE *fp, const struct compiler_flag *ptr, + const char *prefix, const char *invert_prefix) +{ + for(;ptr->name; ptr++) { + fprintf(fp, "%s%s\n", prefix, ptr->name); + if (invert_prefix) { + fprintf(fp, "%s%s\n", invert_prefix, ptr->name); + } + } +} + +static void arg_usage(FILE *fp, const struct compiler_arg *ptr, + const char *prefix) +{ + for(;ptr->name; ptr++) { + const struct compiler_flag *flag; + for(flag = &ptr->flags[0]; flag->name; flag++) { + fprintf(fp, "%s%s=%s\n", + prefix, ptr->name, flag->name); + } + } +} + +static int append_string(size_t *max, const char ***vec, const char *str, + const char *name) +{ + size_t count; + count = ++(*max); + *vec = xrealloc(*vec, sizeof(char *)*count, "name"); + (*vec)[count -1] = 0; + (*vec)[count -2] = str; + return 0; +} + +static void arg_error(char *fmt, ...); +static const char *identifier(const char *str, const char *end); + +static int append_include_path(struct compiler_state *compiler, const char *str) +{ + int result; + if (!exists(str, ".")) { + arg_error("Nonexistent include path: `%s'\n", + str); + } + result = append_string(&compiler->include_path_count, + &compiler->include_paths, str, "include_paths"); + return result; +} + +static int append_define(struct compiler_state *compiler, const char *str) +{ + const char *end, *rest; + int result; + + end = strchr(str, '='); + if (!end) { + end = str + strlen(str); + } + rest = identifier(str, end); + if (rest != end) { + int len = end - str - 1; + arg_error("Invalid name cannot define macro: `%*.*s'\n", + len, len, str); + } + result = append_string(&compiler->define_count, + &compiler->defines, str, "defines"); + return result; +} + +static int append_undef(struct compiler_state *compiler, const char *str) +{ + const char *end, *rest; + int result; + + end = str + strlen(str); + rest = identifier(str, end); + if (rest != end) { + int len = end - str - 1; + arg_error("Invalid name cannot undefine macro: `%*.*s'\n", + len, len, str); + } + result = append_string(&compiler->undef_count, + &compiler->undefs, str, "undefs"); + return result; +} + +static const struct compiler_flag romcc_flags[] = { + { "trigraphs", COMPILER_TRIGRAPHS }, + { "pp-only", COMPILER_PP_ONLY }, + { "eliminate-inefectual-code", COMPILER_ELIMINATE_INEFECTUAL_CODE }, + { "simplify", COMPILER_SIMPLIFY }, + { "scc-transform", COMPILER_SCC_TRANSFORM }, + { "simplify-op", COMPILER_SIMPLIFY_OP }, + { "simplify-phi", COMPILER_SIMPLIFY_PHI }, + { "simplify-label", COMPILER_SIMPLIFY_LABEL }, + { "simplify-branch", COMPILER_SIMPLIFY_BRANCH }, + { "simplify-copy", COMPILER_SIMPLIFY_COPY }, + { "simplify-arith", COMPILER_SIMPLIFY_ARITH }, + { "simplify-shift", COMPILER_SIMPLIFY_SHIFT }, + { "simplify-bitwise", COMPILER_SIMPLIFY_BITWISE }, + { "simplify-logical", COMPILER_SIMPLIFY_LOGICAL }, + { "simplify-bitfield", COMPILER_SIMPLIFY_BITFIELD }, + { 0, 0 }, +}; +static const struct compiler_arg romcc_args[] = { + { "inline-policy", COMPILER_INLINE_MASK, + { + { "always", COMPILER_INLINE_ALWAYS, }, + { "never", COMPILER_INLINE_NEVER, }, + { "defaulton", COMPILER_INLINE_DEFAULTON, }, + { "defaultoff", COMPILER_INLINE_DEFAULTOFF, }, + { "nopenalty", COMPILER_INLINE_NOPENALTY, }, + { 0, 0 }, + }, + }, + { 0, 0 }, +}; +static const struct compiler_flag romcc_opt_flags[] = { + { "-O", COMPILER_SIMPLIFY }, + { "-O2", COMPILER_SIMPLIFY | COMPILER_SCC_TRANSFORM }, + { "-E", COMPILER_PP_ONLY }, + { 0, 0, }, +}; +static const struct compiler_flag romcc_debug_flags[] = { + { "all", DEBUG_ALL }, + { "abort-on-error", DEBUG_ABORT_ON_ERROR }, + { "basic-blocks", DEBUG_BASIC_BLOCKS }, + { "fdominators", DEBUG_FDOMINATORS }, + { "rdominators", DEBUG_RDOMINATORS }, + { "triples", DEBUG_TRIPLES }, + { "interference", DEBUG_INTERFERENCE }, + { "scc-transform", DEBUG_SCC_TRANSFORM }, + { "scc-transform2", DEBUG_SCC_TRANSFORM2 }, + { "rebuild-ssa-form", DEBUG_REBUILD_SSA_FORM }, + { "inline", DEBUG_INLINE }, + { "live-range-conflicts", DEBUG_RANGE_CONFLICTS }, + { "live-range-conflicts2", DEBUG_RANGE_CONFLICTS2 }, + { "color-graph", DEBUG_COLOR_GRAPH }, + { "color-graph2", DEBUG_COLOR_GRAPH2 }, + { "coalescing", DEBUG_COALESCING }, + { "coalescing2", DEBUG_COALESCING2 }, + { "verification", DEBUG_VERIFICATION }, + { "calls", DEBUG_CALLS }, + { "calls2", DEBUG_CALLS2 }, + { "tokens", DEBUG_TOKENS }, + { 0, 0 }, +}; + +static int compiler_encode_flag( + struct compiler_state *compiler, const char *flag) +{ + int act; + int result; + + act = 1; + result = -1; + if (strncmp(flag, "no-", 3) == 0) { + flag += 3; + act = 0; + } + if (strncmp(flag, "-O", 2) == 0) { + result = set_flag(romcc_opt_flags, &compiler->flags, act, flag); + } + else if (strncmp(flag, "-E", 2) == 0) { + result = set_flag(romcc_opt_flags, &compiler->flags, act, flag); + } + else if (strncmp(flag, "-I", 2) == 0) { + result = append_include_path(compiler, flag + 2); + } + else if (strncmp(flag, "-D", 2) == 0) { + result = append_define(compiler, flag + 2); + } + else if (strncmp(flag, "-U", 2) == 0) { + result = append_undef(compiler, flag + 2); + } + else if (act && strncmp(flag, "label-prefix=", 13) == 0) { + result = 0; + compiler->label_prefix = flag + 13; + } + else if (act && strncmp(flag, "max-allocation-passes=", 22) == 0) { + unsigned long max_passes; + char *end; + max_passes = strtoul(flag + 22, &end, 10); + if (end[0] == '\0') { + result = 0; + compiler->max_allocation_passes = max_passes; + } + } + else if (act && strcmp(flag, "debug") == 0) { + result = 0; + compiler->debug |= DEBUG_DEFAULT; + } + else if (strncmp(flag, "debug-", 6) == 0) { + flag += 6; + result = set_flag(romcc_debug_flags, &compiler->debug, act, flag); + } + else { + result = set_flag(romcc_flags, &compiler->flags, act, flag); + if (result < 0) { + result = set_arg(romcc_args, &compiler->flags, flag); + } + } + return result; +} + +static void compiler_usage(FILE *fp) +{ + flag_usage(fp, romcc_opt_flags, "", 0); + flag_usage(fp, romcc_flags, "-f", "-fno-"); + arg_usage(fp, romcc_args, "-f"); + flag_usage(fp, romcc_debug_flags, "-fdebug-", "-fno-debug-"); + fprintf(fp, "-flabel-prefix=\n"); + fprintf(fp, "--label-prefix=\n"); + fprintf(fp, "-I\n"); + fprintf(fp, "-D[=defn]\n"); + fprintf(fp, "-U\n"); +} static void do_cleanup(struct compile_state *state) { if (state->output) { fclose(state->output); - unlink(state->ofilename); + unlink(state->compiler->ofilename); + state->output = 0; + } + if (state->dbgout) { + fflush(state->dbgout); + } + if (state->errout) { + fflush(state->errout); + } +} + +static struct compile_state *exit_state; +static void exit_cleanup(void) +{ + if (exit_state) { + do_cleanup(exit_state); } } static int get_col(struct file_state *file) { int col; - char *ptr, *end; + const char *ptr, *end; ptr = file->line_start; end = file->pos; for(col = 0; ptr < end; ptr++) { if (*ptr != '\t') { col++; - } + } else { col = (col & ~7) + 8; } @@ -933,90 +1753,97 @@ static int get_col(struct file_state *file) static void loc(FILE *fp, struct compile_state *state, struct triple *triple) { int col; - if (triple) { - fprintf(fp, "%s:%d.%d: ", - triple->filename, triple->line, triple->col); + if (triple && triple->occurance) { + struct occurance *spot; + for(spot = triple->occurance; spot; spot = spot->parent) { + fprintf(fp, "%s:%d.%d: ", + spot->filename, spot->line, spot->col); + } return; } if (!state->file) { return; } col = get_col(state->file); - fprintf(fp, "%s:%d.%d: ", - state->file->basename, state->file->line, col); + fprintf(fp, "%s:%d.%d: ", + state->file->report_name, state->file->report_line, col); } -static void __internal_error(struct compile_state *state, struct triple *ptr, - char *fmt, ...) +static void __attribute__ ((noreturn)) internal_error(struct compile_state *state, struct triple *ptr, + const char *fmt, ...) { + FILE *fp = state->errout; va_list args; va_start(args, fmt); - loc(stderr, state, ptr); + loc(fp, state, ptr); + fputc('\n', fp); if (ptr) { - fprintf(stderr, "%p %s ", ptr, tops(ptr->op)); + fprintf(fp, "%p %-10s ", ptr, tops(ptr->op)); } - fprintf(stderr, "Internal compiler error: "); - vfprintf(stderr, fmt, args); - fprintf(stderr, "\n"); + fprintf(fp, "Internal compiler error: "); + vfprintf(fp, fmt, args); + fprintf(fp, "\n"); va_end(args); do_cleanup(state); abort(); } -static void __internal_warning(struct compile_state *state, struct triple *ptr, - char *fmt, ...) +static void internal_warning(struct compile_state *state, struct triple *ptr, + const char *fmt, ...) { + FILE *fp = state->errout; va_list args; va_start(args, fmt); - loc(stderr, state, ptr); - fprintf(stderr, "Internal compiler warning: "); - vfprintf(stderr, fmt, args); - fprintf(stderr, "\n"); + loc(fp, state, ptr); + if (ptr) { + fprintf(fp, "%p %-10s ", ptr, tops(ptr->op)); + } + fprintf(fp, "Internal compiler warning: "); + vfprintf(fp, fmt, args); + fprintf(fp, "\n"); va_end(args); } -static void __error(struct compile_state *state, struct triple *ptr, - char *fmt, ...) +static void __attribute__ ((noreturn)) error(struct compile_state *state, struct triple *ptr, + const char *fmt, ...) { + FILE *fp = state->errout; va_list args; va_start(args, fmt); - loc(stderr, state, ptr); - vfprintf(stderr, fmt, args); + loc(fp, state, ptr); + fputc('\n', fp); + if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) { + fprintf(fp, "%p %-10s ", ptr, tops(ptr->op)); + } + vfprintf(fp, fmt, args); va_end(args); - fprintf(stderr, "\n"); + fprintf(fp, "\n"); do_cleanup(state); - if (state->debug & DEBUG_ABORT_ON_ERROR) { + if (state->compiler->debug & DEBUG_ABORT_ON_ERROR) { abort(); } exit(1); } -static void __warning(struct compile_state *state, struct triple *ptr, - char *fmt, ...) +static void warning(struct compile_state *state, struct triple *ptr, + const char *fmt, ...) { + FILE *fp = state->errout; va_list args; va_start(args, fmt); - loc(stderr, state, ptr); - fprintf(stderr, "warning: "); - vfprintf(stderr, fmt, args); - fprintf(stderr, "\n"); + loc(fp, state, ptr); + fprintf(fp, "warning: "); + if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) { + fprintf(fp, "%p %-10s ", ptr, tops(ptr->op)); + } + vfprintf(fp, fmt, args); + fprintf(fp, "\n"); va_end(args); } -#if DEBUG_ERROR_MESSAGES -# define internal_error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_error -# define internal_warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_warning -# define error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__error -# define warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__warning -#else -# define internal_error __internal_error -# define internal_warning __internal_warning -# define error __error -# define warning __warning -#endif #define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__) static void valid_op(struct compile_state *state, int op) @@ -1035,67 +1862,33 @@ static void valid_ins(struct compile_state *state, struct triple *ptr) valid_op(state, ptr->op); } -static void process_trigraphs(struct compile_state *state) +#if DEBUG_ROMCC_WARNING +static void valid_param_count(struct compile_state *state, struct triple *ins) { - char *src, *dest, *end; - struct file_state *file; - file = state->file; - src = dest = file->buf; - end = file->buf + file->size; - while((end - src) >= 3) { - if ((src[0] == '?') && (src[1] == '?')) { - int c = -1; - switch(src[2]) { - case '=': c = '#'; break; - case '/': c = '\\'; break; - case '\'': c = '^'; break; - case '(': c = '['; break; - case ')': c = ']'; break; - case '!': c = '!'; break; - case '<': c = '{'; break; - case '>': c = '}'; break; - case '-': c = '~'; break; - } - if (c != -1) { - *dest++ = c; - src += 3; - } - else { - *dest++ = *src++; - } - } - else { - *dest++ = *src++; - } + int lhs, rhs, misc, targ; + valid_ins(state, ins); + lhs = table_ops[ins->op].lhs; + rhs = table_ops[ins->op].rhs; + misc = table_ops[ins->op].misc; + targ = table_ops[ins->op].targ; + + if ((lhs >= 0) && (ins->lhs != lhs)) { + internal_error(state, ins, "Bad lhs count"); } - while(src != end) { - *dest++ = *src++; + if ((rhs >= 0) && (ins->rhs != rhs)) { + internal_error(state, ins, "Bad rhs count"); } - file->size = dest - file->buf; -} - -static void splice_lines(struct compile_state *state) -{ - char *src, *dest, *end; - struct file_state *file; - file = state->file; - src = dest = file->buf; - end = file->buf + file->size; - while((end - src) >= 2) { - if ((src[0] == '\\') && (src[1] == '\n')) { - src += 2; - } - else { - *dest++ = *src++; - } + if ((misc >= 0) && (ins->misc != misc)) { + internal_error(state, ins, "Bad misc count"); } - while(src != end) { - *dest++ = *src++; + if ((targ >= 0) && (ins->targ != targ)) { + internal_error(state, ins, "Bad targ count"); } - file->size = dest - file->buf; } +#endif static struct type void_type; +static struct type unknown_type; static void use_triple(struct triple *used, struct triple *user) { struct triple_set **ptr, *new; @@ -1110,7 +1903,7 @@ static void use_triple(struct triple *used, struct triple *user) } ptr = &(*ptr)->next; } - /* Append new to the head of the list, + /* Append new to the head of the list, * copy_func and rename_block_variables * depends on this. */ @@ -1139,134 +1932,221 @@ static void unuse_triple(struct triple *used, struct triple *unuser) } } -static void push_triple(struct triple *used, struct triple *user) +static void put_occurance(struct occurance *occurance) { - struct triple_set *new; - if (!used) - return; - if (!user) - return; - /* Append new to the head of the list, - * it's the only sensible behavoir for a stack. - */ - new = xcmalloc(sizeof(*new), "triple_set"); - new->member = user; - new->next = used->use; - used->use = new; + if (occurance) { + occurance->count -= 1; + if (occurance->count <= 0) { + if (occurance->parent) { + put_occurance(occurance->parent); + } + xfree(occurance); + } + } } -static void pop_triple(struct triple *used, struct triple *unuser) +static void get_occurance(struct occurance *occurance) { - struct triple_set *use, **ptr; - ptr = &used->use; - while(*ptr) { - use = *ptr; - if (use->member == unuser) { - *ptr = use->next; - xfree(use); - /* Only free one occurance from the stack */ - return; - } - else { - ptr = &use->next; - } + if (occurance) { + occurance->count += 1; } } -/* 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, - .sizes = TRIPLE_SIZES(0, 0, 0, 0), - .id = -1, /* An invalid id */ - .u = { .cval = 0, }, +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) && + ((last->filename == filename) || + (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 *base, struct occurance *top) +{ + struct occurance *result, *last; + if (top->parent) { + internal_error(state, 0, "inlining an already inlined function?"); + } + /* If I have a null base treat it that way */ + if ((base->parent == 0) && + (base->col == 0) && + (base->line == 0) && + (base->function[0] == '\0') && + (base->filename[0] == '\0')) { + base = 0; + } + /* See if I can reuse the last occurance I had */ + last = state->last_occurance; + if (last && + (last->parent == base) && + (last->col == top->col) && + (last->line == top->line) && + (last->function == top->function) && + (last->filename == top->filename)) { + get_occurance(last); + return last; + } + /* I can't reuse the last occurance so free it */ + if (last) { + state->last_occurance = 0; + put_occurance(last); + } + /* Generate a new occurance structure */ + get_occurance(base); + result = xmalloc(sizeof(*result), "occurance"); + result->count = 2; + result->filename = top->filename; + result->function = top->function; + result->line = top->line; + result->col = top->col; + result->parent = base; + state->last_occurance = result; + return result; +} + +static struct occurance dummy_occurance = { + .count = 2, .filename = __FILE__, + .function = "", .line = __LINE__, .col = 0, - .param { [0] = 0, [1] = 0, }, + .parent = 0, +}; + +/* The undef 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 unknown_triple = { + .next = &unknown_triple, + .prev = &unknown_triple, + .use = 0, + .op = OP_UNKNOWNVAL, + .lhs = 0, + .rhs = 0, + .misc = 0, + .targ = 0, + .type = &unknown_type, + .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) +static size_t registers_of(struct compile_state *state, struct type *type); + +static struct triple *alloc_triple(struct compile_state *state, + int op, struct type *type, int lhs_wanted, int rhs_wanted, + struct occurance *occurance) { + size_t size, extra_count, min_count; int lhs, rhs, misc, targ; + struct triple *ret, 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)) { + + switch(op) { + case OP_FCALL: rhs = rhs_wanted; - } - else if (op == OP_ASM) { + break; + case OP_PHI: + rhs = rhs_wanted; + break; + case OP_ADECL: + lhs = registers_of(state, type); + break; + case OP_TUPLE: + lhs = registers_of(state, type); + break; + case OP_ASM: rhs = rhs_wanted; lhs = lhs_wanted; + break; } if ((rhs < 0) || (rhs > MAX_RHS)) { - internal_error(state, 0, "bad rhs"); + internal_error(state, &dummy, "bad rhs count %d", rhs); } if ((lhs < 0) || (lhs > MAX_LHS)) { - internal_error(state, 0, "bad lhs"); + internal_error(state, &dummy, "bad lhs count %d", lhs); } if ((misc < 0) || (misc > MAX_MISC)) { - internal_error(state, 0, "bad misc"); + internal_error(state, &dummy, "bad misc count %d", misc); } if ((targ < 0) || (targ > MAX_TARG)) { - internal_error(state, 0, "bad targs"); + internal_error(state, &dummy, "bad targs count %d", targ); } - 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, - const char *filename, int line, int col) -{ - size_t size, sizes, extra_count, min_count; - struct triple *ret; - sizes = triple_sizes(state, op, type, lhs, rhs); min_count = sizeof(ret->param)/sizeof(ret->param[0]); - extra_count = TRIPLE_SIZE(sizes); + extra_count = lhs + rhs + misc + targ; 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->filename = filename; - ret->line = line; - ret->col = col; + ret->op = op; + ret->lhs = lhs; + ret->rhs = rhs; + ret->misc = misc; + ret->targ = targ; + ret->type = type; + ret->next = ret; + ret->prev = ret; + ret->occurance = occurance; + /* A simple sanity check */ + if ((ret->op != op) || + (ret->lhs != lhs) || + (ret->rhs != rhs) || + (ret->misc != misc) || + (ret->targ != targ) || + (ret->type != type) || + (ret->next != ret) || + (ret->prev != ret) || + (ret->occurance != occurance)) { + internal_error(state, ret, "huh?"); + } return ret; } @@ -1274,43 +2154,44 @@ 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); + src_lhs = src->lhs; + src_rhs = src->rhs; + src_size = TRIPLE_SIZE(src); + get_occurance(src->occurance); dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs, - src->filename, src->line, src->col); + 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, +static struct triple *copy_triple(struct compile_state *state, struct triple *src) +{ + struct triple *copy; + copy = dup_triple(state, src); + copy->use = 0; + copy->next = copy->prev = copy; + return copy; +} + +static struct triple *new_triple(struct compile_state *state, int op, struct type *type, int lhs, int rhs) { 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); - } - ret = alloc_triple(state, op, type, lhs, rhs, - filename, line, col); + 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, +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; size_t count; - ret = alloc_triple(state, op, type, -1, -1, filename, line, col); - count = TRIPLE_SIZE(ret->sizes); + ret = alloc_triple(state, op, type, -1, -1, occurance); + count = TRIPLE_SIZE(ret); if (count > 0) { ret->param[0] = left; } @@ -1320,13 +2201,13 @@ static struct triple *build_triple(struct compile_state *state, return ret; } -static struct triple *triple(struct compile_state *state, +static struct triple *triple(struct compile_state *state, int op, struct type *type, struct triple *left, struct triple *right) { struct triple *ret; size_t count; ret = new_triple(state, op, type, -1, -1); - count = TRIPLE_SIZE(ret->sizes); + count = TRIPLE_SIZE(ret); if (count >= 1) { ret->param[0] = left; } @@ -1336,24 +2217,27 @@ static struct triple *triple(struct compile_state *state, return ret; } -static struct triple *branch(struct compile_state *state, +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) { + ret = new_triple(state, OP_CBRANCH, &void_type, -1, 1); RHS(ret, 0) = test; + } else { + ret = new_triple(state, OP_BRANCH, &void_type, -1, 0); } TARG(ret, 0) = targ; /* record the branch target was used */ if (!targ || (targ->op != OP_LABEL)) { internal_error(state, 0, "branch not to label"); - use_triple(targ, ret); } return ret; } - +static int triple_is_label(struct compile_state *state, struct triple *ins); +static int triple_is_call(struct compile_state *state, struct triple *ins); +static int triple_is_cbranch(struct compile_state *state, struct triple *ins); static void insert_triple(struct compile_state *state, struct triple *first, struct triple *ptr) { @@ -1365,8 +2249,9 @@ static void insert_triple(struct compile_state *state, ptr->prev = first->prev; ptr->prev->next = ptr; ptr->next->prev = ptr; - if ((ptr->prev->op == OP_BRANCH) && - TRIPLE_RHS(ptr->prev->sizes)) { + + if (triple_is_cbranch(state, ptr->prev) || + triple_is_call(state, ptr->prev)) { unuse_triple(first, ptr->prev); use_triple(ptr, ptr->prev); } @@ -1375,7 +2260,7 @@ static void insert_triple(struct compile_state *state, static int triple_stores_block(struct compile_state *state, struct triple *ins) { - /* This function is used to determine if u.block + /* This function is used to determine if u.block * is utilized to store the current block number. */ int stores_block; @@ -1384,37 +2269,54 @@ static int triple_stores_block(struct compile_state *state, struct triple *ins) return stores_block; } -static struct block *block_of_triple(struct compile_state *state, +static int triple_is_branch(struct compile_state *state, struct triple *ins); +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 == &unknown_triple) { + return 0; + } + first = state->first; + while(ins != first && !triple_is_branch(state, ins->prev) && + !triple_stores_block(state, ins)) + { if (ins == ins->prev) { - internal_error(state, 0, "ins == ins->prev?"); + internal_error(state, ins, "ins == ins->prev?"); } ins = ins->prev; } - if (!triple_stores_block(state, ins)) { - internal_error(state, ins, "Cannot find block"); - } - return ins->u.block; + return triple_stores_block(state, ins)? ins->u.block: 0; } +static void generate_lhs_pieces(struct compile_state *state, struct triple *ins); static struct triple *pre_triple(struct compile_state *state, struct triple *base, int op, struct type *type, struct triple *left, struct triple *right) { struct block *block; struct triple *ret; + int i; + /* 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); - ret = build_triple(state, op, type, left, right, - base->filename, base->line, base->col); + get_occurance(base->occurance); + ret = build_triple(state, op, type, left, right, base->occurance); + generate_lhs_pieces(state, ret); if (triple_stores_block(state, ret)) { ret->u.block = block; } insert_triple(state, base, ret); - if (block->first == base) { + for(i = 0; i < ret->lhs; i++) { + struct triple *piece; + piece = LHS(ret, i); + insert_triple(state, base, piece); + use_triple(ret, piece); + use_triple(piece, ret); + } + if (block && (block->first == base)) { block->first = ret; } return ret; @@ -1425,20 +2327,82 @@ static struct triple *post_triple(struct compile_state *state, int op, struct type *type, struct triple *left, struct triple *right) { struct block *block; - struct triple *ret; + struct triple *ret, *next; + int zlhs, i; + /* 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 = base->lhs; + if (zlhs) { + base = LHS(base, zlhs - 1); + } + block = block_of_triple(state, base); - ret = build_triple(state, op, type, left, right, - base->filename, base->line, base->col); + get_occurance(base->occurance); + ret = build_triple(state, op, type, left, right, base->occurance); + generate_lhs_pieces(state, ret); if (triple_stores_block(state, ret)) { ret->u.block = block; } - insert_triple(state, base->next, ret); - if (block->last == base) { + next = base->next; + insert_triple(state, next, ret); + zlhs = ret->lhs; + for(i = 0; i < zlhs; i++) { + struct triple *piece; + piece = LHS(ret, i); + insert_triple(state, next, piece); + use_triple(ret, piece); + use_triple(piece, ret); + } + if (block && (block->last == base)) { block->last = ret; + if (zlhs) { + block->last = LHS(ret, zlhs - 1); + } } return ret; } +static struct type *reg_type( + struct compile_state *state, struct type *type, int reg); + +static void generate_lhs_piece( + struct compile_state *state, struct triple *ins, int index) +{ + struct type *piece_type; + struct triple *piece; + get_occurance(ins->occurance); + piece_type = reg_type(state, ins->type, index * REG_SIZEOF_REG); + + if ((piece_type->type & TYPE_MASK) == TYPE_BITFIELD) { + piece_type = piece_type->left; + } +#if 0 +{ + static void name_of(FILE *fp, struct type *type); + FILE * fp = state->errout; + fprintf(fp, "piece_type(%d): ", index); + name_of(fp, piece_type); + fprintf(fp, "\n"); +} +#endif + piece = alloc_triple(state, OP_PIECE, piece_type, -1, -1, ins->occurance); + piece->u.cval = index; + LHS(ins, piece->u.cval) = piece; + MISC(piece, 0) = ins; +} + +static void generate_lhs_pieces(struct compile_state *state, struct triple *ins) +{ + int i, zlhs; + zlhs = ins->lhs; + for(i = 0; i < zlhs; i++) { + generate_lhs_piece(state, ins, i); + } +} + static struct triple *label(struct compile_state *state) { /* Labels don't get a type */ @@ -1447,122 +2411,327 @@ static struct triple *label(struct compile_state *state) return result; } +static struct triple *mkprog(struct compile_state *state, ...) +{ + struct triple *prog, *head, *arg; + va_list args; + int i; + + head = label(state); + prog = new_triple(state, OP_PROG, &void_type, -1, -1); + RHS(prog, 0) = head; + va_start(args, state); + i = 0; + while((arg = va_arg(args, struct triple *)) != 0) { + if (++i >= 100) { + internal_error(state, 0, "too many arguments to mkprog"); + } + flatten(state, head, arg); + } + va_end(args); + prog->type = head->prev->type; + return prog; +} +static void name_of(FILE *fp, struct type *type); static void display_triple(FILE *fp, struct triple *ins) { - if (ins->op == OP_INTCONST) { - fprintf(fp, "(%p) %3d %-2d %-10s <0x%08lx> @ %s:%d.%d\n", - ins, ID_REG(ins->id), ins->template_id, tops(ins->op), - ins->u.cval, - ins->filename, ins->line, ins->col); + struct occurance *ptr; + const char *reg; + char pre, post, vol; + pre = post = vol = ' '; + if (ins) { + if (ins->id & TRIPLE_FLAG_PRE_SPLIT) { + pre = '^'; + } + if (ins->id & TRIPLE_FLAG_POST_SPLIT) { + post = ','; + } + if (ins->id & TRIPLE_FLAG_VOLATILE) { + vol = 'v'; + } + reg = arch_reg_str(ID_REG(ins->id)); + } + if (ins == 0) { + fprintf(fp, "(%p) ", ins); + } + else if (ins->op == OP_INTCONST) { + fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s <0x%08lx> ", + ins, pre, post, vol, reg, ins->template_id, tops(ins->op), + (unsigned long)(ins->u.cval)); } else if (ins->op == OP_ADDRCONST) { - fprintf(fp, "(%p) %3d %-2d %-10s %-10p <0x%08lx> @ %s:%d.%d\n", - ins, ID_REG(ins->id), ins->template_id, tops(ins->op), - MISC(ins, 0), ins->u.cval, - ins->filename, ins->line, ins->col); + fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>", + ins, pre, post, vol, reg, ins->template_id, tops(ins->op), + MISC(ins, 0), (unsigned long)(ins->u.cval)); + } + else if (ins->op == OP_INDEX) { + fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>", + ins, pre, post, vol, reg, ins->template_id, tops(ins->op), + RHS(ins, 0), (unsigned long)(ins->u.cval)); + } + else if (ins->op == OP_PIECE) { + fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>", + ins, pre, post, vol, reg, ins->template_id, tops(ins->op), + MISC(ins, 0), (unsigned long)(ins->u.cval)); } else { int i, count; - fprintf(fp, "(%p) %3d %-2d %-10s", - ins, ID_REG(ins->id), ins->template_id, tops(ins->op)); - count = TRIPLE_SIZE(ins->sizes); + fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s", + ins, pre, post, vol, reg, ins->template_id, tops(ins->op)); + if (table_ops[ins->op].flags & BITFIELD) { + fprintf(fp, " <%2d-%2d:%2d>", + ins->u.bitfield.offset, + ins->u.bitfield.offset + ins->u.bitfield.size, + ins->u.bitfield.size); + } + count = TRIPLE_SIZE(ins); for(i = 0; i < count; i++) { fprintf(fp, " %-10p", ins->param[i]); } for(; i < 2; i++) { - printf(" "); + fprintf(fp, " "); + } + } + if (ins) { + struct triple_set *user; +#if DEBUG_DISPLAY_TYPES + fprintf(fp, " <"); + name_of(fp, ins->type); + fprintf(fp, "> "); +#endif +#if DEBUG_DISPLAY_USES + fprintf(fp, " ["); + for(user = ins->use; user; user = user->next) { + fprintf(fp, " %-10p", user->member); + } + fprintf(fp, " ]"); +#endif + fprintf(fp, " @"); + for(ptr = ins->occurance; ptr; ptr = ptr->parent) { + fprintf(fp, " %s,%s:%d.%d", + ptr->function, + ptr->filename, + ptr->line, + ptr->col); + } + if (ins->op == OP_ASM) { + fprintf(fp, "\n\t%s", ins->u.ainfo->str); } - fprintf(fp, " @ %s:%d.%d\n", - ins->filename, ins->line, ins->col); } + fprintf(fp, "\n"); fflush(fp); } -static int triple_is_pure(struct compile_state *state, struct triple *ins) +static int equiv_types(struct type *left, struct type *right); +static void display_triple_changes( + FILE *fp, const struct triple *new, const struct triple *orig) +{ + + int new_count, orig_count; + new_count = TRIPLE_SIZE(new); + orig_count = TRIPLE_SIZE(orig); + if ((new->op != orig->op) || + (new_count != orig_count) || + (memcmp(orig->param, new->param, + orig_count * sizeof(orig->param[0])) != 0) || + (memcmp(&orig->u, &new->u, sizeof(orig->u)) != 0)) + { + struct occurance *ptr; + int i, min_count, indent; + fprintf(fp, "(%p %p)", new, orig); + if (orig->op == new->op) { + fprintf(fp, " %-11s", tops(orig->op)); + } else { + fprintf(fp, " [%-10s %-10s]", + tops(new->op), tops(orig->op)); + } + min_count = new_count; + if (min_count > orig_count) { + min_count = orig_count; + } + for(indent = i = 0; i < min_count; i++) { + if (orig->param[i] == new->param[i]) { + fprintf(fp, " %-11p", + orig->param[i]); + indent += 12; + } else { + fprintf(fp, " [%-10p %-10p]", + new->param[i], + orig->param[i]); + indent += 24; + } + } + for(; i < orig_count; i++) { + fprintf(fp, " [%-9p]", orig->param[i]); + indent += 12; + } + for(; i < new_count; i++) { + fprintf(fp, " [%-9p]", new->param[i]); + indent += 12; + } + if ((new->op == OP_INTCONST)|| + (new->op == OP_ADDRCONST)) { + fprintf(fp, " <0x%08lx>", + (unsigned long)(new->u.cval)); + indent += 13; + } + for(;indent < 36; indent++) { + putc(' ', fp); + } + +#if DEBUG_DISPLAY_TYPES + fprintf(fp, " <"); + name_of(fp, new->type); + if (!equiv_types(new->type, orig->type)) { + fprintf(fp, " -- "); + name_of(fp, orig->type); + } + fprintf(fp, "> "); +#endif + + fprintf(fp, " @"); + for(ptr = orig->occurance; ptr; ptr = ptr->parent) { + fprintf(fp, " %s,%s:%d.%d", + ptr->function, + ptr->filename, + ptr->line, + ptr->col); + + } + fprintf(fp, "\n"); + fflush(fp); + } +} + +static int triple_is_pure(struct compile_state *state, struct triple *ins, unsigned id) { /* Does the triple have no side effects. - * I.e. Rexecuting the triple with the same arguments + * 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", + internal_error(state, 0, "Purity of %s not known", tops(ins->op)); } - return pure == PURE; + return (pure == PURE) && !(id & TRIPLE_FLAG_VOLATILE); } -static int triple_is_branch(struct compile_state *state, struct triple *ins) +static int triple_is_branch_type(struct compile_state *state, + struct triple *ins, unsigned type) { - /* This function is used to determine which triples need - * a register. - */ - int is_branch; + /* Is this one of the passed branch types? */ valid_ins(state, ins); - is_branch = (table_ops[ins->op].targ != 0); - return is_branch; + return (BRANCH_BITS(table_ops[ins->op].flags) == type); } -static int triple_is_def(struct compile_state *state, 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_def; + /* Is this triple a branch instruction? */ valid_ins(state, ins); - is_def = (table_ops[ins->op].flags & DEF) == DEF; - return is_def; + return (BRANCH_BITS(table_ops[ins->op].flags) != 0); } -static struct triple **triple_iter(struct compile_state *state, - size_t count, struct triple **vector, - struct triple *ins, struct triple **last) +static int triple_is_cbranch(struct compile_state *state, struct triple *ins) { - struct triple **ret; - ret = 0; - if (count) { - if (!last) { - ret = vector; - } - else if ((last >= vector) && (last < (vector + count - 1))) { - ret = last + 1; - } - } - return ret; - + /* Is this triple a conditional branch instruction? */ + return triple_is_branch_type(state, ins, CBRANCH); } -static struct triple **triple_lhs(struct compile_state *state, - struct triple *ins, struct triple **last) +static int triple_is_ubranch(struct compile_state *state, struct triple *ins) { - return triple_iter(state, TRIPLE_LHS(ins->sizes), &LHS(ins,0), - ins, last); + /* Is this triple a unconditional branch instruction? */ + unsigned type; + valid_ins(state, ins); + type = BRANCH_BITS(table_ops[ins->op].flags); + return (type != 0) && (type != CBRANCH); } -static struct triple **triple_rhs(struct compile_state *state, - struct triple *ins, struct triple **last) +static int triple_is_call(struct compile_state *state, struct triple *ins) { - return triple_iter(state, TRIPLE_RHS(ins->sizes), &RHS(ins,0), - ins, last); + /* Is this triple a call instruction? */ + return triple_is_branch_type(state, ins, CALLBRANCH); } -static struct triple **triple_misc(struct compile_state *state, - struct triple *ins, struct triple **last) +static int triple_is_ret(struct compile_state *state, struct triple *ins) { - return triple_iter(state, TRIPLE_MISC(ins->sizes), &MISC(ins,0), - ins, last); + /* Is this triple a return instruction? */ + return triple_is_branch_type(state, ins, RETBRANCH); } -static struct triple **triple_targ(struct compile_state *state, - struct triple *ins, struct triple **last) +#if DEBUG_ROMCC_WARNING +static int triple_is_simple_ubranch(struct compile_state *state, struct triple *ins) { - size_t count; - struct triple **ret, **vector; + /* Is this triple an unconditional branch and not a call or a + * return? */ + return triple_is_branch_type(state, ins, UBRANCH); +} +#endif + +static int triple_is_end(struct compile_state *state, struct triple *ins) +{ + return triple_is_branch_type(state, ins, ENDBRANCH); +} + +static int triple_is_label(struct compile_state *state, struct triple *ins) +{ + valid_ins(state, ins); + return (ins->op == OP_LABEL); +} + +static struct triple *triple_to_block_start( + struct compile_state *state, struct triple *start) +{ + while(!triple_is_branch(state, start->prev) && + (!triple_is_label(state, start) || !start->use)) { + start = start->prev; + } + return start; +} + +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; + valid_ins(state, ins); + is_def = (table_ops[ins->op].flags & DEF) == DEF; + if (ins->lhs >= 1) { + is_def = 0; + } + return is_def; +} + +static int triple_is_structural(struct compile_state *state, struct triple *ins) +{ + int is_structural; + valid_ins(state, ins); + is_structural = (table_ops[ins->op].flags & STRUCTURAL) == STRUCTURAL; + return is_structural; +} + +static int triple_is_part(struct compile_state *state, struct triple *ins) +{ + int is_part; + valid_ins(state, ins); + is_part = (table_ops[ins->op].flags & PART) == PART; + return is_part; +} + +static int triple_is_auto_var(struct compile_state *state, struct triple *ins) +{ + return (ins->op == OP_PIECE) && (MISC(ins, 0)->op == OP_ADECL); +} + +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; - count = TRIPLE_TARG(ins->sizes); - vector = &TARG(ins, 0); if (count) { if (!last) { ret = vector; @@ -1570,20 +2739,189 @@ static struct triple **triple_targ(struct compile_state *state, else if ((last >= vector) && (last < (vector + count - 1))) { ret = last + 1; } - else if ((last == (vector + count - 1)) && - TRIPLE_RHS(ins->sizes)) { + } + return ret; + +} + +static struct triple **triple_lhs(struct compile_state *state, + struct triple *ins, struct triple **last) +{ + return triple_iter(state, ins->lhs, &LHS(ins,0), + ins, last); +} + +static struct triple **triple_rhs(struct compile_state *state, + struct triple *ins, struct triple **last) +{ + return triple_iter(state, ins->rhs, &RHS(ins,0), + ins, last); +} + +static struct triple **triple_misc(struct compile_state *state, + struct triple *ins, struct triple **last) +{ + return triple_iter(state, ins->misc, &MISC(ins,0), + ins, last); +} + +static struct triple **do_triple_targ(struct compile_state *state, + struct triple *ins, struct triple **last, int call_edges, int next_edges) +{ + size_t count; + struct triple **ret, **vector; + int next_is_targ; + ret = 0; + count = ins->targ; + next_is_targ = 0; + if (triple_is_cbranch(state, ins)) { + next_is_targ = 1; + } + if (!call_edges && triple_is_call(state, ins)) { + count = 0; + } + if (next_edges && triple_is_call(state, ins)) { + next_is_targ = 1; + } + vector = &TARG(ins, 0); + if (!ret && next_is_targ) { + if (!last) { ret = &ins->next; + } else if (last == &ins->next) { + last = 0; + } + } + if (!ret && count) { + if (!last) { + ret = vector; + } + else if ((last >= vector) && (last < (vector + count - 1))) { + ret = last + 1; + } + else if (last == vector + count - 1) { + last = 0; + } + } + if (!ret && triple_is_ret(state, ins) && call_edges) { + struct triple_set *use; + for(use = ins->use; use; use = use->next) { + if (!triple_is_call(state, use->member)) { + continue; + } + if (!last) { + ret = &use->member->next; + break; + } + else if (last == &use->member->next) { + last = 0; + } } } return ret; } +static struct triple **triple_targ(struct compile_state *state, + struct triple *ins, struct triple **last) +{ + return do_triple_targ(state, ins, last, 1, 1); +} + +static struct triple **triple_edge_targ(struct compile_state *state, + struct triple *ins, struct triple **last) +{ + return do_triple_targ(state, ins, last, + state->functions_joined, !state->functions_joined); +} + +static struct triple *after_lhs(struct compile_state *state, struct triple *ins) +{ + struct triple *next; + int lhs, i; + lhs = ins->lhs; + next = ins->next; + for(i = 0; i < lhs; i++) { + struct triple *piece; + piece = LHS(ins, i); + if (next != piece) { + 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); + } + next = next->next; + } + return next; +} + +/* Function piece accessor functions */ +static struct triple *do_farg(struct compile_state *state, + struct triple *func, unsigned index) +{ + struct type *ftype; + struct triple *first, *arg; + unsigned i; + + ftype = func->type; + if((index < 0) || (index >= (ftype->elements + 2))) { + internal_error(state, func, "bad argument index: %d", index); + } + first = RHS(func, 0); + arg = first->next; + for(i = 0; i < index; i++, arg = after_lhs(state, arg)) { + /* do nothing */ + } + if (arg->op != OP_ADECL) { + internal_error(state, 0, "arg not adecl?"); + } + return arg; +} +static struct triple *fresult(struct compile_state *state, struct triple *func) +{ + return do_farg(state, func, 0); +} +static struct triple *fretaddr(struct compile_state *state, struct triple *func) +{ + return do_farg(state, func, 1); +} +static struct triple *farg(struct compile_state *state, + struct triple *func, unsigned index) +{ + return do_farg(state, func, index + 2); +} + + +static void display_func(struct compile_state *state, FILE *fp, struct triple *func) +{ + struct triple *first, *ins; + fprintf(fp, "display_func %s\n", func->type->type_ident->name); + first = ins = RHS(func, 0); + do { + if (triple_is_label(state, ins) && ins->use) { + fprintf(fp, "%p:\n", ins); + } + display_triple(fp, ins); + + if (triple_is_branch(state, ins)) { + fprintf(fp, "\n"); + } + if (ins->next->prev != ins) { + internal_error(state, ins->next, "bad prev"); + } + ins = ins->next; + } while(ins != first); +} static void verify_use(struct compile_state *state, struct triple *user, struct triple *used) { int size, i; - size = TRIPLE_SIZE(user->sizes); + size = TRIPLE_SIZE(user); for(i = 0; i < size; i++) { if (user->param[i] == used) { break; @@ -1600,13 +2938,17 @@ static void verify_use(struct compile_state *state, } } -static int find_rhs_use(struct compile_state *state, +static int find_rhs_use(struct compile_state *state, struct triple *user, struct triple *used) { struct triple **param; int size, i; verify_use(state, user, used); - size = TRIPLE_RHS(user->sizes); + +#if DEBUG_ROMCC_WARNINGS +#warning "AUDIT ME ->rhs" +#endif + size = user->rhs; param = &RHS(user, 0); for(i = 0; i < size; i++) { if (param[i] == used) { @@ -1620,12 +2962,13 @@ 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)); + (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr)); ptr->prev->next = ptr->next; ptr->next->prev = ptr->prev; if (ptr->use) { internal_error(state, ptr, "ptr->use != 0"); } + put_occurance(ptr->occurance); memset(ptr, -1, size); xfree(ptr); } @@ -1634,6 +2977,24 @@ static void release_triple(struct compile_state *state, struct triple *ptr) { struct triple_set *set, *next; struct triple **expr; + struct block *block; + if (ptr == &unknown_triple) { + return; + } + valid_ins(state, ptr); + /* Make certain the we are not the first or last element of a block */ + block = block_of_triple(state, ptr); + if (block) { + if ((block->last == ptr) && (block->first == ptr)) { + block->last = block->first = 0; + } + else if (block->last == ptr) { + block->last = ptr->prev; + } + else if (block->first == ptr) { + block->first = ptr->next; + } + } /* Remove ptr from use chains where it is the user */ expr = triple_rhs(state, ptr, 0); for(; expr; expr = triple_rhs(state, ptr, expr)) { @@ -1655,35 +3016,36 @@ static void release_triple(struct compile_state *state, struct triple *ptr) } expr = triple_targ(state, ptr, 0); for(; expr; expr = triple_targ(state, ptr, expr)) { - if (*expr) { + if (*expr){ unuse_triple(*expr, ptr); } } /* Reomve ptr from use chains where it is used */ for(set = ptr->use; set; set = next) { next = set->next; + valid_ins(state, set->member); expr = triple_rhs(state, set->member, 0); for(; expr; expr = triple_rhs(state, set->member, expr)) { if (*expr == ptr) { - *expr = &zero_triple; + *expr = &unknown_triple; } } expr = triple_lhs(state, set->member, 0); for(; expr; expr = triple_lhs(state, set->member, expr)) { if (*expr == ptr) { - *expr = &zero_triple; + *expr = &unknown_triple; } } expr = triple_misc(state, set->member, 0); for(; expr; expr = triple_misc(state, set->member, expr)) { if (*expr == ptr) { - *expr = &zero_triple; + *expr = &unknown_triple; } } expr = triple_targ(state, set->member, 0); for(; expr; expr = triple_targ(state, set->member, expr)) { if (*expr == ptr) { - *expr = &zero_triple; + *expr = &unknown_triple; } } unuse_triple(ptr, set->member); @@ -1691,127 +3053,134 @@ static void release_triple(struct compile_state *state, struct triple *ptr) free_triple(state, ptr); } -static void print_triple(struct compile_state *state, struct triple *ptr); - -#define TOK_UNKNOWN 0 -#define TOK_SPACE 1 -#define TOK_SEMI 2 -#define TOK_LBRACE 3 -#define TOK_RBRACE 4 -#define TOK_COMMA 5 -#define TOK_EQ 6 -#define TOK_COLON 7 -#define TOK_LBRACKET 8 -#define TOK_RBRACKET 9 -#define TOK_LPAREN 10 -#define TOK_RPAREN 11 -#define TOK_STAR 12 -#define TOK_DOTS 13 -#define TOK_MORE 14 -#define TOK_LESS 15 -#define TOK_TIMESEQ 16 -#define TOK_DIVEQ 17 -#define TOK_MODEQ 18 -#define TOK_PLUSEQ 19 -#define TOK_MINUSEQ 20 -#define TOK_SLEQ 21 -#define TOK_SREQ 22 -#define TOK_ANDEQ 23 -#define TOK_XOREQ 24 -#define TOK_OREQ 25 -#define TOK_EQEQ 26 -#define TOK_NOTEQ 27 -#define TOK_QUEST 28 -#define TOK_LOGOR 29 -#define TOK_LOGAND 30 -#define TOK_OR 31 -#define TOK_AND 32 -#define TOK_XOR 33 -#define TOK_LESSEQ 34 -#define TOK_MOREEQ 35 -#define TOK_SL 36 -#define TOK_SR 37 -#define TOK_PLUS 38 -#define TOK_MINUS 39 -#define TOK_DIV 40 -#define TOK_MOD 41 -#define TOK_PLUSPLUS 42 -#define TOK_MINUSMINUS 43 -#define TOK_BANG 44 -#define TOK_ARROW 45 -#define TOK_DOT 46 -#define TOK_TILDE 47 -#define TOK_LIT_STRING 48 -#define TOK_LIT_CHAR 49 -#define TOK_LIT_INT 50 -#define TOK_LIT_FLOAT 51 -#define TOK_MACRO 52 -#define TOK_CONCATENATE 53 - -#define TOK_IDENT 54 -#define TOK_STRUCT_NAME 55 -#define TOK_ENUM_CONST 56 -#define TOK_TYPE_NAME 57 - -#define TOK_AUTO 58 -#define TOK_BREAK 59 -#define TOK_CASE 60 -#define TOK_CHAR 61 -#define TOK_CONST 62 -#define TOK_CONTINUE 63 -#define TOK_DEFAULT 64 -#define TOK_DO 65 -#define TOK_DOUBLE 66 -#define TOK_ELSE 67 -#define TOK_ENUM 68 -#define TOK_EXTERN 69 -#define TOK_FLOAT 70 -#define TOK_FOR 71 -#define TOK_GOTO 72 -#define TOK_IF 73 -#define TOK_INLINE 74 -#define TOK_INT 75 -#define TOK_LONG 76 -#define TOK_REGISTER 77 -#define TOK_RESTRICT 78 -#define TOK_RETURN 79 -#define TOK_SHORT 80 -#define TOK_SIGNED 81 -#define TOK_SIZEOF 82 -#define TOK_STATIC 83 -#define TOK_STRUCT 84 -#define TOK_SWITCH 85 -#define TOK_TYPEDEF 86 -#define TOK_UNION 87 -#define TOK_UNSIGNED 88 -#define TOK_VOID 89 -#define TOK_VOLATILE 90 -#define TOK_WHILE 91 -#define TOK_ASM 92 -#define TOK_ATTRIBUTE 93 -#define TOK_ALIGNOF 94 +static void print_triples(struct compile_state *state); +static void print_blocks(struct compile_state *state, const char *func, FILE *fp); + +#define TOK_UNKNOWN 0 +#define TOK_SPACE 1 +#define TOK_SEMI 2 +#define TOK_LBRACE 3 +#define TOK_RBRACE 4 +#define TOK_COMMA 5 +#define TOK_EQ 6 +#define TOK_COLON 7 +#define TOK_LBRACKET 8 +#define TOK_RBRACKET 9 +#define TOK_LPAREN 10 +#define TOK_RPAREN 11 +#define TOK_STAR 12 +#define TOK_DOTS 13 +#define TOK_MORE 14 +#define TOK_LESS 15 +#define TOK_TIMESEQ 16 +#define TOK_DIVEQ 17 +#define TOK_MODEQ 18 +#define TOK_PLUSEQ 19 +#define TOK_MINUSEQ 20 +#define TOK_SLEQ 21 +#define TOK_SREQ 22 +#define TOK_ANDEQ 23 +#define TOK_XOREQ 24 +#define TOK_OREQ 25 +#define TOK_EQEQ 26 +#define TOK_NOTEQ 27 +#define TOK_QUEST 28 +#define TOK_LOGOR 29 +#define TOK_LOGAND 30 +#define TOK_OR 31 +#define TOK_AND 32 +#define TOK_XOR 33 +#define TOK_LESSEQ 34 +#define TOK_MOREEQ 35 +#define TOK_SL 36 +#define TOK_SR 37 +#define TOK_PLUS 38 +#define TOK_MINUS 39 +#define TOK_DIV 40 +#define TOK_MOD 41 +#define TOK_PLUSPLUS 42 +#define TOK_MINUSMINUS 43 +#define TOK_BANG 44 +#define TOK_ARROW 45 +#define TOK_DOT 46 +#define TOK_TILDE 47 +#define TOK_LIT_STRING 48 +#define TOK_LIT_CHAR 49 +#define TOK_LIT_INT 50 +#define TOK_LIT_FLOAT 51 +#define TOK_MACRO 52 +#define TOK_CONCATENATE 53 + +#define TOK_IDENT 54 +#define TOK_STRUCT_NAME 55 +#define TOK_ENUM_CONST 56 +#define TOK_TYPE_NAME 57 + +#define TOK_AUTO 58 +#define TOK_BREAK 59 +#define TOK_CASE 60 +#define TOK_CHAR 61 +#define TOK_CONST 62 +#define TOK_CONTINUE 63 +#define TOK_DEFAULT 64 +#define TOK_DO 65 +#define TOK_DOUBLE 66 +#define TOK_ELSE 67 +#define TOK_ENUM 68 +#define TOK_EXTERN 69 +#define TOK_FLOAT 70 +#define TOK_FOR 71 +#define TOK_GOTO 72 +#define TOK_IF 73 +#define TOK_INLINE 74 +#define TOK_INT 75 +#define TOK_LONG 76 +#define TOK_REGISTER 77 +#define TOK_RESTRICT 78 +#define TOK_RETURN 79 +#define TOK_SHORT 80 +#define TOK_SIGNED 81 +#define TOK_SIZEOF 82 +#define TOK_STATIC 83 +#define TOK_STRUCT 84 +#define TOK_SWITCH 85 +#define TOK_TYPEDEF 86 +#define TOK_UNION 87 +#define TOK_UNSIGNED 88 +#define TOK_VOID 89 +#define TOK_VOLATILE 90 +#define TOK_WHILE 91 +#define TOK_ASM 92 +#define TOK_ATTRIBUTE 93 +#define TOK_ALIGNOF 94 #define TOK_FIRST_KEYWORD TOK_AUTO #define TOK_LAST_KEYWORD TOK_ALIGNOF -#define TOK_DEFINE 100 -#define TOK_UNDEF 101 -#define TOK_INCLUDE 102 -#define TOK_LINE 103 -#define TOK_ERROR 104 -#define TOK_WARNING 105 -#define TOK_PRAGMA 106 -#define TOK_IFDEF 107 -#define TOK_IFNDEF 108 -#define TOK_ELIF 109 -#define TOK_ENDIF 110 - -#define TOK_FIRST_MACRO TOK_DEFINE -#define TOK_LAST_MACRO TOK_ENDIF - -#define TOK_EOF 111 +#define TOK_MDEFINE 100 +#define TOK_MDEFINED 101 +#define TOK_MUNDEF 102 +#define TOK_MINCLUDE 103 +#define TOK_MLINE 104 +#define TOK_MERROR 105 +#define TOK_MWARNING 106 +#define TOK_MPRAGMA 107 +#define TOK_MIFDEF 108 +#define TOK_MIFNDEF 109 +#define TOK_MELIF 110 +#define TOK_MENDIF 111 + +#define TOK_FIRST_MACRO TOK_MDEFINE +#define TOK_LAST_MACRO TOK_MENDIF + +#define TOK_MIF 112 +#define TOK_MELSE 113 +#define TOK_MIDENT 114 + +#define TOK_EOL 115 +#define TOK_EOF 116 static const char *tokens[] = { -[TOK_UNKNOWN ] = "unknown", +[TOK_UNKNOWN ] = ":unknown:", [TOK_SPACE ] = ":space:", [TOK_SEMI ] = ";", [TOK_LBRACE ] = "{", @@ -1906,18 +3275,23 @@ static const char *tokens[] = { [TOK_ATTRIBUTE ] = "__attribute__", [TOK_ALIGNOF ] = "__alignof__", -[TOK_DEFINE ] = "define", -[TOK_UNDEF ] = "undef", -[TOK_INCLUDE ] = "include", -[TOK_LINE ] = "line", -[TOK_ERROR ] = "error", -[TOK_WARNING ] = "warning", -[TOK_PRAGMA ] = "pragma", -[TOK_IFDEF ] = "ifdef", -[TOK_IFNDEF ] = "ifndef", -[TOK_ELIF ] = "elif", -[TOK_ENDIF ] = "endif", - +[TOK_MDEFINE ] = "#define", +[TOK_MDEFINED ] = "#defined", +[TOK_MUNDEF ] = "#undef", +[TOK_MINCLUDE ] = "#include", +[TOK_MLINE ] = "#line", +[TOK_MERROR ] = "#error", +[TOK_MWARNING ] = "#warning", +[TOK_MPRAGMA ] = "#pragma", +[TOK_MIFDEF ] = "#ifdef", +[TOK_MIFNDEF ] = "#ifndef", +[TOK_MELIF ] = "#elif", +[TOK_MENDIF ] = "#endif", + +[TOK_MIF ] = "#if", +[TOK_MELSE ] = "#else", +[TOK_MIDENT ] = "#:ident:", +[TOK_EOL ] = "EOL", [TOK_EOF ] = "EOF", }; @@ -1941,7 +3315,7 @@ static struct hash_entry *lookup( unsigned int index; index = hash(name, name_len); entry = state->hash_table[index]; - while(entry && + while(entry && ((entry->name_len != name_len) || (memcmp(entry->name, name, name_len) != 0))) { entry = entry->next; @@ -1971,7 +3345,7 @@ static void ident_to_keyword(struct compile_state *state, struct token *tk) entry = tk->ident; if (entry && ((entry->tok == TOK_TYPE_NAME) || (entry->tok == TOK_ENUM_CONST) || - ((entry->tok >= TOK_FIRST_KEYWORD) && + ((entry->tok >= TOK_FIRST_KEYWORD) && (entry->tok <= TOK_LAST_KEYWORD)))) { tk->tok = entry->tok; } @@ -1981,11 +3355,20 @@ static void ident_to_macro(struct compile_state *state, struct token *tk) { struct hash_entry *entry; entry = tk->ident; - if (entry && - (entry->tok >= TOK_FIRST_MACRO) && - (entry->tok <= TOK_LAST_MACRO)) { + if (!entry) + return; + if ((entry->tok >= TOK_FIRST_MACRO) && (entry->tok <= TOK_LAST_MACRO)) { tk->tok = entry->tok; } + else if (entry->tok == TOK_IF) { + tk->tok = TOK_MIF; + } + else if (entry->tok == TOK_ELSE) { + tk->tok = TOK_MELSE; + } + else { + tk->tok = TOK_MIDENT; + } } static void hash_keyword( @@ -1999,29 +3382,52 @@ static void hash_keyword( entry->tok = tok; } -static void symbol( +static void romcc_symbol( struct compile_state *state, struct hash_entry *ident, - struct symbol **chain, struct triple *def, struct type *type) + struct symbol **chain, struct triple *def, struct type *type, int depth) { struct symbol *sym; - if (*chain && ((*chain)->scope_depth == state->scope_depth)) { + if (*chain && ((*chain)->scope_depth >= depth)) { error(state, 0, "%s already defined", ident->name); } sym = xcmalloc(sizeof(*sym), "symbol"); sym->ident = ident; sym->def = def; sym->type = type; - sym->scope_depth = state->scope_depth; + sym->scope_depth = depth; sym->next = *chain; *chain = sym; } +static void symbol( + struct compile_state *state, struct hash_entry *ident, + struct symbol **chain, struct triple *def, struct type *type) +{ + romcc_symbol(state, ident, chain, def, type, state->scope_depth); +} + +static void var_symbol(struct compile_state *state, + struct hash_entry *ident, struct triple *def) +{ + if ((def->type->type & TYPE_MASK) == TYPE_PRODUCT) { + internal_error(state, 0, "bad var type"); + } + symbol(state, ident, &ident->sym_ident, def, def->type); +} + +static void label_symbol(struct compile_state *state, + struct hash_entry *ident, struct triple *label, int depth) +{ + romcc_symbol(state, ident, &ident->sym_label, label, &void_type, depth); +} + static void start_scope(struct compile_state *state) { state->scope_depth++; } -static void end_scope_syms(struct symbol **chain, int depth) +static void end_scope_syms(struct compile_state *state, + struct symbol **chain, int depth) { struct symbol *sym, *next; sym = *chain; @@ -2038,16 +3444,16 @@ static void end_scope(struct compile_state *state) int i; int depth; /* Walk through the hash table and remove all symbols - * in the current scope. + * in the current scope. */ depth = state->scope_depth; for(i = 0; i < HASH_TABLE_SIZE; i++) { struct hash_entry *entry; entry = state->hash_table[i]; while(entry) { - end_scope_syms(&entry->sym_label, depth); - end_scope_syms(&entry->sym_struct, depth); - end_scope_syms(&entry->sym_ident, depth); + end_scope_syms(state, &entry->sym_label, depth); + end_scope_syms(state, &entry->sym_tag, depth); + end_scope_syms(state, &entry->sym_ident, depth); entry = entry->next; } } @@ -2099,17 +3505,168 @@ static void register_keywords(struct compile_state *state) static void register_macro_keywords(struct compile_state *state) { - hash_keyword(state, "define", TOK_DEFINE); - hash_keyword(state, "undef", TOK_UNDEF); - hash_keyword(state, "include", TOK_INCLUDE); - hash_keyword(state, "line", TOK_LINE); - hash_keyword(state, "error", TOK_ERROR); - hash_keyword(state, "warning", TOK_WARNING); - hash_keyword(state, "pragma", TOK_PRAGMA); - hash_keyword(state, "ifdef", TOK_IFDEF); - hash_keyword(state, "ifndef", TOK_IFNDEF); - hash_keyword(state, "elif", TOK_ELIF); - hash_keyword(state, "endif", TOK_ENDIF); + hash_keyword(state, "define", TOK_MDEFINE); + hash_keyword(state, "defined", TOK_MDEFINED); + hash_keyword(state, "undef", TOK_MUNDEF); + hash_keyword(state, "include", TOK_MINCLUDE); + hash_keyword(state, "line", TOK_MLINE); + hash_keyword(state, "error", TOK_MERROR); + hash_keyword(state, "warning", TOK_MWARNING); + hash_keyword(state, "pragma", TOK_MPRAGMA); + hash_keyword(state, "ifdef", TOK_MIFDEF); + hash_keyword(state, "ifndef", TOK_MIFNDEF); + hash_keyword(state, "elif", TOK_MELIF); + hash_keyword(state, "endif", TOK_MENDIF); +} + + +static void undef_macro(struct compile_state *state, struct hash_entry *ident) +{ + if (ident->sym_define != 0) { + struct macro *macro; + struct macro_arg *arg, *anext; + macro = ident->sym_define; + ident->sym_define = 0; + + /* Free the macro arguments... */ + anext = macro->args; + while(anext) { + arg = anext; + anext = arg->next; + xfree(arg); + } + + /* Free the macro buffer */ + xfree(macro->buf); + + /* Now free the macro itself */ + xfree(macro); + } +} + +static void do_define_macro(struct compile_state *state, + struct hash_entry *ident, const char *body, + int argc, struct macro_arg *args) +{ + struct macro *macro; + struct macro_arg *arg; + size_t body_len; + + /* Find the length of the body */ + body_len = strlen(body); + macro = ident->sym_define; + if (macro != 0) { + int identical_bodies, identical_args; + struct macro_arg *oarg; + /* Explicitly allow identical redfinitions of the same macro */ + identical_bodies = + (macro->buf_len == body_len) && + (memcmp(macro->buf, body, body_len) == 0); + identical_args = macro->argc == argc; + oarg = macro->args; + arg = args; + while(identical_args && arg) { + identical_args = oarg->ident == arg->ident; + arg = arg->next; + oarg = oarg->next; + } + if (identical_bodies && identical_args) { + xfree(body); + return; + } + error(state, 0, "macro %s already defined\n", ident->name); + } +#if 0 + fprintf(state->errout, "#define %s: `%*.*s'\n", + ident->name, body_len, body_len, body); +#endif + macro = xmalloc(sizeof(*macro), "macro"); + macro->ident = ident; + macro->buf = body; + macro->buf_len = body_len; + macro->args = args; + macro->argc = argc; + + ident->sym_define = macro; +} + +static void define_macro( + struct compile_state *state, + struct hash_entry *ident, + const char *body, int body_len, + int argc, struct macro_arg *args) +{ + char *buf; + buf = xmalloc(body_len + 1, "macro buf"); + memcpy(buf, body, body_len); + buf[body_len] = '\0'; + do_define_macro(state, ident, buf, argc, args); +} + +static void register_builtin_macro(struct compile_state *state, + const char *name, const char *value) +{ + struct hash_entry *ident; + + if (value[0] == '(') { + internal_error(state, 0, "Builtin macros with arguments not supported"); + } + ident = lookup(state, name, strlen(name)); + define_macro(state, ident, value, strlen(value), -1, 0); +} + +static void register_builtin_macros(struct compile_state *state) +{ + char buf[30]; + char scratch[30]; + time_t now; + struct tm *tm; + now = time(NULL); + tm = localtime(&now); + + register_builtin_macro(state, "__ROMCC__", VERSION_MAJOR); + register_builtin_macro(state, "__ROMCC_MINOR__", VERSION_MINOR); + register_builtin_macro(state, "__FILE__", "\"This should be the filename\""); + register_builtin_macro(state, "__LINE__", "54321"); + + strftime(scratch, sizeof(scratch), "%b %e %Y", tm); + sprintf(buf, "\"%s\"", scratch); + register_builtin_macro(state, "__DATE__", buf); + + strftime(scratch, sizeof(scratch), "%H:%M:%S", tm); + sprintf(buf, "\"%s\"", scratch); + register_builtin_macro(state, "__TIME__", buf); + + /* I can't be a conforming implementation of C :( */ + register_builtin_macro(state, "__STDC__", "0"); + /* In particular I don't conform to C99 */ + register_builtin_macro(state, "__STDC_VERSION__", "199901L"); + +} + +static void process_cmdline_macros(struct compile_state *state) +{ + const char **macro, *name; + struct hash_entry *ident; + for(macro = state->compiler->defines; (name = *macro); macro++) { + const char *body; + size_t name_len; + + name_len = strlen(name); + body = strchr(name, '='); + if (!body) { + body = "\0"; + } else { + name_len = body - name; + body++; + } + ident = lookup(state, name, name_len); + define_macro(state, ident, body, strlen(body), -1, 0); + } + for(macro = state->compiler->undefs; (name = *macro); macro++) { + ident = lookup(state, name, strlen(name)); + undef_macro(state, ident); + } } static int spacep(int c) @@ -2121,7 +3678,6 @@ static int spacep(int c) case '\f': case '\v': case '\r': - case '\n': ret = 1; break; } @@ -2132,19 +3688,27 @@ static int digitp(int c) { int ret = 0; switch(c) { - case '0': case '1': case '2': case '3': case '4': + case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': ret = 1; break; } 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) { int ret = 0; switch(c) { - case '0': case '1': case '2': case '3': case '4': + case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': @@ -2153,7 +3717,7 @@ static int hexdigitp(int c) } return ret; } -static int hexdigval(int c) +static int hexdigval(int c) { int val = -1; if ((c >= '0') && (c <= '9')) { @@ -2172,7 +3736,7 @@ static int octdigitp(int c) { int ret = 0; switch(c) { - case '0': case '1': case '2': case '3': + case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': ret = 1; break; @@ -2211,6 +3775,20 @@ static int letterp(int c) return ret; } +static const char *identifier(const char *str, const char *end) +{ + if (letterp(*str)) { + for(; str < end; str++) { + int c; + c = *str; + if (!letterp(c) && !digitp(c)) { + break; + } + } + } + return str; +} + static int char_value(struct compile_state *state, const signed char **strp, const signed char *end) { @@ -2230,8 +3808,8 @@ static int char_value(struct compile_state *state, case '\\': c = '\\'; str++; break; case '?': c = '?'; str++; break; case '\'': c = '\''; str++; break; - case '"': c = '"'; break; - case 'x': + case '"': c = '"'; str++; break; + case 'x': c = 0; str++; while((str < end) && hexdigitp(*str)) { @@ -2240,7 +3818,7 @@ static int char_value(struct compile_state *state, str++; } break; - case '0': case '1': case '2': case '3': + case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': c = 0; while((str < end) && octdigitp(*str)) { @@ -2258,152 +3836,263 @@ static int char_value(struct compile_state *state, return c; } -static char *after_digits(char *ptr, char *end) +static const char *next_char(struct file_state *file, const char *pos, int index) { - while((ptr < end) && digitp(*ptr)) { - ptr++; + const char *end = file->buf + file->size; + while(pos < end) { + /* Lookup the character */ + int size = 1; + int c = *pos; + /* Is this a trigraph? */ + if (file->trigraphs && + (c == '?') && ((end - pos) >= 3) && (pos[1] == '?')) + { + switch(pos[2]) { + case '=': c = '#'; break; + case '/': c = '\\'; break; + case '\'': c = '^'; break; + case '(': c = '['; break; + case ')': c = ']'; break; + case '!': c = '!'; break; + case '<': c = '{'; break; + case '>': c = '}'; break; + case '-': c = '~'; break; + } + if (c != '?') { + size = 3; + } + } + /* Is this an escaped newline? */ + if (file->join_lines && + (c == '\\') && (pos + size < end) && ((pos[1] == '\n') || ((pos[1] == '\r') && (pos[2] == '\n')))) + { + int cr_offset = ((pos[1] == '\r') && (pos[2] == '\n'))?1:0; + /* At the start of a line just eat it */ + if (pos == file->pos) { + file->line++; + file->report_line++; + file->line_start = pos + size + 1 + cr_offset; + } + pos += size + 1 + cr_offset; + } + /* Do I need to ga any farther? */ + else if (index == 0) { + break; + } + /* Process a normal character */ + else { + pos += size; + index -= 1; + } } - return ptr; + return pos; } -static char *after_octdigits(char *ptr, char *end) +static int get_char(struct file_state *file, const char *pos) { - while((ptr < end) && octdigitp(*ptr)) { - ptr++; - } + const char *end = file->buf + file->size; + int c; + c = -1; + pos = next_char(file, pos, 0); + if (pos < end) { + /* Lookup the character */ + c = *pos; + /* If it is a trigraph get the trigraph value */ + if (file->trigraphs && + (c == '?') && ((end - pos) >= 3) && (pos[1] == '?')) + { + switch(pos[2]) { + case '=': c = '#'; break; + case '/': c = '\\'; break; + case '\'': c = '^'; break; + case '(': c = '['; break; + case ')': c = ']'; break; + case '!': c = '!'; break; + case '<': c = '{'; break; + case '>': c = '}'; break; + case '-': c = '~'; break; + } + } + } + return c; +} + +static void eat_chars(struct file_state *file, const char *targ) +{ + const char *pos = file->pos; + while(pos < targ) { + /* Do we have a newline? */ + if (pos[0] == '\n') { + file->line++; + file->report_line++; + file->line_start = pos + 1; + } + pos++; + } + file->pos = pos; +} + + +static size_t char_strlen(struct file_state *file, const char *src, const char *end) +{ + size_t len; + len = 0; + while(src < end) { + src = next_char(file, src, 1); + len++; + } + return len; +} + +static void char_strcpy(char *dest, + struct file_state *file, const char *src, const char *end) +{ + while(src < end) { + int c; + c = get_char(file, src); + src = next_char(file, src, 1); + *dest++ = c; + } +} + +static char *char_strdup(struct file_state *file, + const char *start, const char *end, const char *id) +{ + char *str; + size_t str_len; + str_len = char_strlen(file, start, end); + str = xcmalloc(str_len + 1, id); + char_strcpy(str, file, start, end); + str[str_len] = '\0'; + return str; +} + +static const char *after_digits(struct file_state *file, const char *ptr) +{ + while(digitp(get_char(file, ptr))) { + ptr = next_char(file, ptr, 1); + } return ptr; } -static char *after_hexdigits(char *ptr, char *end) +static const char *after_octdigits(struct file_state *file, const char *ptr) { - while((ptr < end) && hexdigitp(*ptr)) { - ptr++; + while(octdigitp(get_char(file, ptr))) { + ptr = next_char(file, ptr, 1); + } + return ptr; +} + +static const char *after_hexdigits(struct file_state *file, const char *ptr) +{ + while(hexdigitp(get_char(file, ptr))) { + ptr = next_char(file, ptr, 1); + } + return ptr; +} + +static const char *after_alnums(struct file_state *file, const char *ptr) +{ + int c; + c = get_char(file, ptr); + while(letterp(c) || digitp(c)) { + ptr = next_char(file, ptr, 1); + c = get_char(file, ptr); } return ptr; } -static void save_string(struct compile_state *state, - struct token *tk, char *start, char *end, const char *id) +static void save_string(struct file_state *file, + struct token *tk, const char *start, const char *end, const char *id) { char *str; - int str_len; + /* Create a private copy of the string */ - str_len = end - start + 1; - str = xmalloc(str_len + 1, id); - memcpy(str, start, str_len); - str[str_len] = '\0'; + str = char_strdup(file, start, end, id); /* Store the copy in the token */ tk->val.str = str; - tk->str_len = str_len; + tk->str_len = strlen(str); } -static void next_token(struct compile_state *state, int index) + +static void raw_next_token(struct compile_state *state, + struct file_state *file, struct token *tk) { - struct file_state *file; - struct token *tk; - char *token; + const char *token; int c, c1, c2, c3; - char *tokp, *end; + const char *tokp; + int eat; int tok; -next_token: - file = state->file; - tk = &state->token[index]; + tk->str_len = 0; tk->ident = 0; - token = tokp = file->pos; - end = file->buf + file->size; + token = tokp = next_char(file, file->pos, 0); tok = TOK_UNKNOWN; - c = -1; - if (tokp < end) { - c = *tokp; - } - c1 = -1; - if ((tokp + 1) < end) { - c1 = tokp[1]; - } - c2 = -1; - if ((tokp + 2) < end) { - c2 = tokp[2]; - } - c3 = -1; - if ((tokp + 3) < end) { - c3 = tokp[3]; - } - if (tokp >= end) { + c = get_char(file, tokp); + tokp = next_char(file, tokp, 1); + eat = 0; + c1 = get_char(file, tokp); + c2 = get_char(file, next_char(file, tokp, 1)); + c3 = get_char(file, next_char(file, tokp, 2)); + + /* The end of the file */ + if (c == -1) { tok = TOK_EOF; - tokp = end; } /* Whitespace */ else if (spacep(c)) { tok = TOK_SPACE; - while ((tokp < end) && spacep(c)) { - if (c == '\n') { - file->line++; - file->line_start = tokp + 1; - } - c = *(++tokp); - } - if (!spacep(c)) { - tokp--; + while (spacep(get_char(file, tokp))) { + tokp = next_char(file, tokp, 1); } } /* EOL Comments */ else if ((c == '/') && (c1 == '/')) { tok = TOK_SPACE; - for(tokp += 2; tokp < end; tokp++) { - c = *tokp; + tokp = next_char(file, tokp, 1); + while((c = get_char(file, tokp)) != -1) { + /* Advance to the next character only after we verify + * the current character is not a newline. + * EOL is special to the preprocessor so we don't + * want to loose any. + */ if (c == '\n') { - file->line++; - file->line_start = tokp +1; break; } + tokp = next_char(file, tokp, 1); } } /* Comments */ else if ((c == '/') && (c1 == '*')) { - int line; - char *line_start; - line = file->line; - line_start = file->line_start; - for(tokp += 2; (end - tokp) >= 2; tokp++) { - c = *tokp; - if (c == '\n') { - line++; - line_start = tokp +1; - } - else if ((c == '*') && (tokp[1] == '/')) { + tokp = next_char(file, tokp, 2); + c = c2; + while((c1 = get_char(file, tokp)) != -1) { + tokp = next_char(file, tokp, 1); + if ((c == '*') && (c1 == '/')) { tok = TOK_SPACE; - tokp += 1; break; } + c = c1; } if (tok == TOK_UNKNOWN) { error(state, 0, "unterminated comment"); } - file->line = line; - file->line_start = line_start; } /* string constants */ - else if ((c == '"') || - ((c == 'L') && (c1 == '"'))) { - int line; - char *line_start; - int wchar; - line = file->line; - line_start = file->line_start; - wchar = 0; + else if ((c == '"') || ((c == 'L') && (c1 == '"'))) { + int multiline; + + multiline = 0; if (c == 'L') { - wchar = 1; - tokp++; + tokp = next_char(file, tokp, 1); } - for(tokp += 1; tokp < end; tokp++) { - c = *tokp; + while((c = get_char(file, tokp)) != -1) { + tokp = next_char(file, tokp, 1); if (c == '\n') { - line++; - line_start = tokp + 1; + multiline = 1; } - else if ((c == '\\') && (tokp +1 < end)) { - tokp++; + else if (c == '\\') { + tokp = next_char(file, tokp, 1); } else if (c == '"') { tok = TOK_LIT_STRING; @@ -2413,36 +4102,28 @@ next_token: if (tok == TOK_UNKNOWN) { error(state, 0, "unterminated string constant"); } - if (line != file->line) { + if (multiline) { warning(state, 0, "multiline string constant"); } - file->line = line; - file->line_start = line_start; /* Save the string value */ - save_string(state, tk, token, tokp, "literal string"); + save_string(file, tk, token, tokp, "literal string"); } /* character constants */ - else if ((c == '\'') || - ((c == 'L') && (c1 == '\''))) { - int line; - char *line_start; - int wchar; - line = file->line; - line_start = file->line_start; - wchar = 0; + else if ((c == '\'') || ((c == 'L') && (c1 == '\''))) { + int multiline; + + multiline = 0; if (c == 'L') { - wchar = 1; - tokp++; + tokp = next_char(file, tokp, 1); } - for(tokp += 1; tokp < end; tokp++) { - c = *tokp; + while((c = get_char(file, tokp)) != -1) { + tokp = next_char(file, tokp, 1); if (c == '\n') { - line++; - line_start = tokp + 1; + multiline = 1; } - else if ((c == '\\') && (tokp +1 < end)) { - tokp++; + else if (c == '\\') { + tokp = next_char(file, tokp, 1); } else if (c == '\'') { tok = TOK_LIT_CHAR; @@ -2452,21 +4133,19 @@ next_token: if (tok == TOK_UNKNOWN) { error(state, 0, "unterminated character constant"); } - if (line != file->line) { + if (multiline) { warning(state, 0, "multiline character constant"); } - file->line = line; - file->line_start = line_start; /* Save the character value */ - save_string(state, tk, token, tokp, "literal character"); + save_string(file, tk, token, tokp, "literal character"); } - /* integer and floating constants + /* integer and floating constants * Integer Constants * {digits} * 0[Xx]{hexdigits} * 0{octdigit}+ - * + * * Floating constants * {digits}.{digits}[Ee][+-]?{digits} * {digits}.{digits} @@ -2474,121 +4153,140 @@ next_token: * .{digits}[Ee][+-]?{digits} * .{digits} */ - else if (digitp(c) || ((c == '.') && (digitp(c1)))) { - char *next, *new; + const char *next; int is_float; + int cn; is_float = 0; if (c != '.') { - next = after_digits(tokp, end); + next = after_digits(file, tokp); } else { - next = tokp; - } - if (next[0] == '.') { - new = after_digits(next, end); - is_float = (new != next); - next = new; - } - if ((next[0] == 'e') || (next[0] == 'E')) { - if (((next + 1) < end) && - ((next[1] == '+') || (next[1] == '-'))) { - next++; + next = token; + } + cn = get_char(file, next); + if (cn == '.') { + next = next_char(file, next, 1); + next = after_digits(file, next); + is_float = 1; + } + cn = get_char(file, next); + if ((cn == 'e') || (cn == 'E')) { + const char *new; + next = next_char(file, next, 1); + cn = get_char(file, next); + if ((cn == '+') || (cn == '-')) { + next = next_char(file, next, 1); } - new = after_digits(next, end); - is_float = (new != next); + new = after_digits(file, next); + is_float |= (new != next); next = new; } if (is_float) { tok = TOK_LIT_FLOAT; - if ((next < end) && ( - (next[0] == 'f') || - (next[0] == 'F') || - (next[0] == 'l') || - (next[0] == 'L')) - ) { - next++; + cn = get_char(file, next); + if ((cn == 'f') || (cn == 'F') || (cn == 'l') || (cn == 'L')) { + next = next_char(file, next, 1); } } if (!is_float && digitp(c)) { tok = TOK_LIT_INT; if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) { - next = after_hexdigits(tokp + 2, end); + next = next_char(file, tokp, 1); + next = after_hexdigits(file, next); } else if (c == '0') { - next = after_octdigits(tokp, end); + next = after_octdigits(file, tokp); } else { - next = after_digits(tokp, end); + next = after_digits(file, tokp); } /* crazy integer suffixes */ - if ((next < end) && - ((next[0] == 'u') || (next[0] == 'U'))) { - next++; - if ((next < end) && - ((next[0] == 'l') || (next[0] == 'L'))) { - next++; + cn = get_char(file, next); + if ((cn == 'u') || (cn == 'U')) { + next = next_char(file, next, 1); + cn = get_char(file, next); + if ((cn == 'l') || (cn == 'L')) { + next = next_char(file, next, 1); + cn = get_char(file, next); + } + if ((cn == 'l') || (cn == 'L')) { + next = next_char(file, next, 1); } } - else if ((next < end) && - ((next[0] == 'l') || (next[0] == 'L'))) { - next++; - if ((next < end) && - ((next[0] == 'u') || (next[0] == 'U'))) { - next++; + else if ((cn == 'l') || (cn == 'L')) { + next = next_char(file, next, 1); + cn = get_char(file, next); + if ((cn == 'l') || (cn == 'L')) { + next = next_char(file, next, 1); + cn = get_char(file, next); + } + if ((cn == 'u') || (cn == 'U')) { + next = next_char(file, next, 1); } } } - tokp = next - 1; + tokp = next; /* Save the integer/floating point value */ - save_string(state, tk, token, tokp, "literal number"); + save_string(file, tk, token, tokp, "literal number"); } /* identifiers */ else if (letterp(c)) { tok = TOK_IDENT; - for(tokp += 1; tokp < end; tokp++) { - c = *tokp; - if (!letterp(c) && !digitp(c)) { - break; - } + + /* Find and save the identifier string */ + tokp = after_alnums(file, tokp); + save_string(file, tk, token, tokp, "identifier"); + + /* Look up to see which identifier it is */ + tk->ident = lookup(state, tk->val.str, tk->str_len); + + /* Free the identifier string */ + tk->str_len = 0; + xfree(tk->val.str); + + /* See if this identifier can be macro expanded */ + tk->val.notmacro = 0; + c = get_char(file, tokp); + if (c == '$') { + tokp = next_char(file, tokp, 1); + tk->val.notmacro = 1; } - tokp -= 1; - tk->ident = lookup(state, token, tokp +1 - token); } /* C99 alternate macro characters */ - else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) { - tokp += 3; - tok = TOK_CONCATENATE; - } - else if ((c == '.') && (c1 == '.') && (c2 == '.')) { tokp += 2; tok = TOK_DOTS; } - else if ((c == '<') && (c1 == '<') && (c2 == '=')) { tokp += 2; tok = TOK_SLEQ; } - else if ((c == '>') && (c1 == '>') && (c2 == '=')) { tokp += 2; tok = TOK_SREQ; } - else if ((c == '*') && (c1 == '=')) { tokp += 1; tok = TOK_TIMESEQ; } - else if ((c == '/') && (c1 == '=')) { tokp += 1; tok = TOK_DIVEQ; } - else if ((c == '%') && (c1 == '=')) { tokp += 1; tok = TOK_MODEQ; } - else if ((c == '+') && (c1 == '=')) { tokp += 1; tok = TOK_PLUSEQ; } - else if ((c == '-') && (c1 == '=')) { tokp += 1; tok = TOK_MINUSEQ; } - else if ((c == '&') && (c1 == '=')) { tokp += 1; tok = TOK_ANDEQ; } - else if ((c == '^') && (c1 == '=')) { tokp += 1; tok = TOK_XOREQ; } - else if ((c == '|') && (c1 == '=')) { tokp += 1; tok = TOK_OREQ; } - else if ((c == '=') && (c1 == '=')) { tokp += 1; tok = TOK_EQEQ; } - else if ((c == '!') && (c1 == '=')) { tokp += 1; tok = TOK_NOTEQ; } - else if ((c == '|') && (c1 == '|')) { tokp += 1; tok = TOK_LOGOR; } - else if ((c == '&') && (c1 == '&')) { tokp += 1; tok = TOK_LOGAND; } - else if ((c == '<') && (c1 == '=')) { tokp += 1; tok = TOK_LESSEQ; } - else if ((c == '>') && (c1 == '=')) { tokp += 1; tok = TOK_MOREEQ; } - else if ((c == '<') && (c1 == '<')) { tokp += 1; tok = TOK_SL; } - else if ((c == '>') && (c1 == '>')) { tokp += 1; tok = TOK_SR; } - else if ((c == '+') && (c1 == '+')) { tokp += 1; tok = TOK_PLUSPLUS; } - else if ((c == '-') && (c1 == '-')) { tokp += 1; tok = TOK_MINUSMINUS; } - else if ((c == '-') && (c1 == '>')) { tokp += 1; tok = TOK_ARROW; } - else if ((c == '<') && (c1 == ':')) { tokp += 1; tok = TOK_LBRACKET; } - else if ((c == ':') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACKET; } - else if ((c == '<') && (c1 == '%')) { tokp += 1; tok = TOK_LBRACE; } - else if ((c == '%') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACE; } - else if ((c == '%') && (c1 == ':')) { tokp += 1; tok = TOK_MACRO; } - else if ((c == '#') && (c1 == '#')) { tokp += 1; tok = TOK_CONCATENATE; } + else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) { + eat += 3; + tok = TOK_CONCATENATE; + } + else if ((c == '.') && (c1 == '.') && (c2 == '.')) { eat += 2; tok = TOK_DOTS; } + else if ((c == '<') && (c1 == '<') && (c2 == '=')) { eat += 2; tok = TOK_SLEQ; } + else if ((c == '>') && (c1 == '>') && (c2 == '=')) { eat += 2; tok = TOK_SREQ; } + else if ((c == '*') && (c1 == '=')) { eat += 1; tok = TOK_TIMESEQ; } + else if ((c == '/') && (c1 == '=')) { eat += 1; tok = TOK_DIVEQ; } + else if ((c == '%') && (c1 == '=')) { eat += 1; tok = TOK_MODEQ; } + else if ((c == '+') && (c1 == '=')) { eat += 1; tok = TOK_PLUSEQ; } + else if ((c == '-') && (c1 == '=')) { eat += 1; tok = TOK_MINUSEQ; } + else if ((c == '&') && (c1 == '=')) { eat += 1; tok = TOK_ANDEQ; } + else if ((c == '^') && (c1 == '=')) { eat += 1; tok = TOK_XOREQ; } + else if ((c == '|') && (c1 == '=')) { eat += 1; tok = TOK_OREQ; } + else if ((c == '=') && (c1 == '=')) { eat += 1; tok = TOK_EQEQ; } + else if ((c == '!') && (c1 == '=')) { eat += 1; tok = TOK_NOTEQ; } + else if ((c == '|') && (c1 == '|')) { eat += 1; tok = TOK_LOGOR; } + else if ((c == '&') && (c1 == '&')) { eat += 1; tok = TOK_LOGAND; } + else if ((c == '<') && (c1 == '=')) { eat += 1; tok = TOK_LESSEQ; } + else if ((c == '>') && (c1 == '=')) { eat += 1; tok = TOK_MOREEQ; } + else if ((c == '<') && (c1 == '<')) { eat += 1; tok = TOK_SL; } + else if ((c == '>') && (c1 == '>')) { eat += 1; tok = TOK_SR; } + else if ((c == '+') && (c1 == '+')) { eat += 1; tok = TOK_PLUSPLUS; } + else if ((c == '-') && (c1 == '-')) { eat += 1; tok = TOK_MINUSMINUS; } + else if ((c == '-') && (c1 == '>')) { eat += 1; tok = TOK_ARROW; } + else if ((c == '<') && (c1 == ':')) { eat += 1; tok = TOK_LBRACKET; } + else if ((c == ':') && (c1 == '>')) { eat += 1; tok = TOK_RBRACKET; } + else if ((c == '<') && (c1 == '%')) { eat += 1; tok = TOK_LBRACE; } + else if ((c == '%') && (c1 == '>')) { eat += 1; tok = TOK_RBRACE; } + else if ((c == '%') && (c1 == ':')) { eat += 1; tok = TOK_MACRO; } + else if ((c == '#') && (c1 == '#')) { eat += 1; tok = TOK_CONCATENATE; } else if (c == ';') { tok = TOK_SEMI; } else if (c == '{') { tok = TOK_LBRACE; } else if (c == '}') { tok = TOK_RBRACE; } @@ -2614,756 +4312,755 @@ next_token: else if (c == '.') { tok = TOK_DOT; } else if (c == '~') { tok = TOK_TILDE; } else if (c == '#') { tok = TOK_MACRO; } - if (tok == TOK_MACRO) { - /* Only match preprocessor directives at the start of a line */ - char *ptr; - for(ptr = file->line_start; spacep(*ptr); ptr++) - ; - if (ptr != tokp) { - tok = TOK_UNKNOWN; - } - } - if (tok == TOK_UNKNOWN) { - error(state, 0, "unknown token"); - } + else if (c == '\n') { tok = TOK_EOL; } - file->pos = tokp + 1; + tokp = next_char(file, tokp, eat); + eat_chars(file, tokp); tk->tok = tok; - if (tok == TOK_IDENT) { - ident_to_keyword(state, tk); - } - /* Don't return space tokens. */ - if (tok == TOK_SPACE) { - goto next_token; - } + tk->pos = token; } -static void compile_macro(struct compile_state *state, struct token *tk) +static void check_tok(struct compile_state *state, struct token *tk, int tok) { - struct file_state *file; - struct hash_entry *ident; - ident = tk->ident; - file = xmalloc(sizeof(*file), "file_state"); - file->basename = xstrdup(tk->ident->name); - file->dirname = xstrdup(""); - file->size = ident->sym_define->buf_len; - file->buf = xmalloc(file->size +2, file->basename); - memcpy(file->buf, ident->sym_define->buf, file->size); - file->buf[file->size] = '\n'; - file->buf[file->size + 1] = '\0'; - file->pos = file->buf; - file->line_start = file->pos; - file->line = 1; - file->prev = state->file; - state->file = file; + if (tk->tok != tok) { + const char *name1, *name2; + name1 = tokens[tk->tok]; + name2 = ""; + if ((tk->tok == TOK_IDENT) || (tk->tok == TOK_MIDENT)) { + name2 = tk->ident->name; + } + error(state, 0, "\tfound %s %s expected %s", + name1, name2, tokens[tok]); + } } - -static int mpeek(struct compile_state *state, int index) +struct macro_arg_value { + struct hash_entry *ident; + char *value; + size_t len; +}; +static struct macro_arg_value *read_macro_args( + struct compile_state *state, struct macro *macro, + struct file_state *file, struct token *tk) { - struct token *tk; - int rescan; - tk = &state->token[index + 1]; - if (tk->tok == -1) { - next_token(state, index + 1); + struct macro_arg_value *argv; + struct macro_arg *arg; + int paren_depth; + int i; + + if (macro->argc == 0) { + do { + raw_next_token(state, file, tk); + } while(tk->tok == TOK_SPACE); + return NULL; } - do { - rescan = 0; - if ((tk->tok == TOK_EOF) && - (state->file != state->macro_file) && - (state->file->prev)) { - struct file_state *file = state->file; - state->file = file->prev; - /* file->basename is used keep it */ - xfree(file->dirname); - xfree(file->buf); - xfree(file); - next_token(state, index + 1); - rescan = 1; + argv = xcmalloc(sizeof(*argv) * macro->argc, "macro args"); + for(i = 0, arg = macro->args; arg; arg = arg->next, i++) { + argv[i].value = 0; + argv[i].len = 0; + argv[i].ident = arg->ident; + } + paren_depth = 0; + i = 0; + + for(;;) { + const char *start; + size_t len; + start = file->pos; + raw_next_token(state, file, tk); + + if (!paren_depth && (tk->tok == TOK_COMMA) && + (argv[i].ident != state->i___VA_ARGS__)) + { + i++; + if (i >= macro->argc) { + error(state, 0, "too many args to %s\n", + macro->ident->name); + } + continue; } - else if (tk->ident && tk->ident->sym_define) { - compile_macro(state, tk); - next_token(state, index + 1); - rescan = 1; + + if (tk->tok == TOK_LPAREN) { + paren_depth++; } - } while(rescan); - /* Don't show the token on the next line */ - if (state->macro_line < state->macro_file->line) { - return TOK_EOF; + + if (tk->tok == TOK_RPAREN) { + if (paren_depth == 0) { + break; + } + paren_depth--; + } + if (tk->tok == TOK_EOF) { + error(state, 0, "End of file encountered while parsing macro arguments"); + } + + len = char_strlen(file, start, file->pos); + argv[i].value = xrealloc( + argv[i].value, argv[i].len + len, "macro args"); + char_strcpy((char *)argv[i].value + argv[i].len, file, start, file->pos); + argv[i].len += len; } - return state->token[index +1].tok; + if (i != macro->argc -1) { + error(state, 0, "missing %s arg %d\n", + macro->ident->name, i +2); + } + return argv; } -static void meat(struct compile_state *state, int index, int tok) + +static void free_macro_args(struct macro *macro, struct macro_arg_value *argv) { - int next_tok; int i; - next_tok = mpeek(state, index); - if (next_tok != tok) { - const char *name1, *name2; - name1 = tokens[next_tok]; - name2 = ""; - if (next_tok == TOK_IDENT) { - name2 = state->token[index + 1].ident->name; - } - error(state, 0, "found %s %s expected %s", - name1, name2, tokens[tok]); - } - /* Free the old token value */ - if (state->token[index].str_len) { - memset((void *)(state->token[index].val.str), -1, - state->token[index].str_len); - xfree(state->token[index].val.str); - } - for(i = index; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) { - state->token[i] = state->token[i + 1]; + for(i = 0; i < macro->argc; i++) { + xfree(argv[i].value); } - memset(&state->token[i], 0, sizeof(state->token[i])); - state->token[i].tok = -1; + xfree(argv); } -static long_t mcexpr(struct compile_state *state, int index); +struct macro_buf { + char *str; + size_t len, pos; +}; -static long_t mprimary_expr(struct compile_state *state, int index) +static void grow_macro_buf(struct compile_state *state, + const char *id, struct macro_buf *buf, + size_t grow) { - long_t val; - int tok; - tok = mpeek(state, index); - while(state->token[index + 1].ident && - state->token[index + 1].ident->sym_define) { - meat(state, index, tok); - compile_macro(state, &state->token[index]); - tok = mpeek(state, index); - } - switch(tok) { - case TOK_LPAREN: - meat(state, index, TOK_LPAREN); - val = mcexpr(state, index); - meat(state, index, TOK_RPAREN); - break; - case TOK_LIT_INT: - { - char *end; - meat(state, index, TOK_LIT_INT); - errno = 0; - val = strtol(state->token[index].val.str, &end, 0); - if (((val == LONG_MIN) || (val == LONG_MAX)) && - (errno == ERANGE)) { - error(state, 0, "Integer constant to large"); - } - break; - } - default: - meat(state, index, TOK_LIT_INT); - val = 0; + if ((buf->pos + grow) >= buf->len) { + buf->str = xrealloc(buf->str, buf->len + grow, id); + buf->len += grow; } - return val; } -static long_t munary_expr(struct compile_state *state, int index) + +static void append_macro_text(struct compile_state *state, + const char *id, struct macro_buf *buf, + const char *fstart, size_t flen) { - long_t val; - switch(mpeek(state, index)) { - case TOK_PLUS: - meat(state, index, TOK_PLUS); - val = munary_expr(state, index); - val = + val; - break; - case TOK_MINUS: - meat(state, index, TOK_MINUS); - val = munary_expr(state, index); - val = - val; - break; - case TOK_TILDE: - meat(state, index, TOK_BANG); - val = munary_expr(state, index); - val = ~ val; - break; - case TOK_BANG: - meat(state, index, TOK_BANG); - val = munary_expr(state, index); - val = ! val; - break; - default: - val = mprimary_expr(state, index); - break; - } - return val; - + grow_macro_buf(state, id, buf, flen); + memcpy(buf->str + buf->pos, fstart, flen); +#if 0 + fprintf(state->errout, "append: `%*.*s' `%*.*s'\n", + buf->pos, buf->pos, buf->str, + flen, flen, buf->str + buf->pos); +#endif + buf->pos += flen; } -static long_t mmul_expr(struct compile_state *state, int index) + + +static void append_macro_chars(struct compile_state *state, + const char *id, struct macro_buf *buf, + struct file_state *file, const char *start, const char *end) { - long_t val; - int done; - val = munary_expr(state, index); - do { - long_t right; - done = 0; - switch(mpeek(state, index)) { - case TOK_STAR: - meat(state, index, TOK_STAR); - right = munary_expr(state, index); - val = val * right; - break; - case TOK_DIV: - meat(state, index, TOK_DIV); - right = munary_expr(state, index); - val = val / right; - break; - case TOK_MOD: - meat(state, index, TOK_MOD); - right = munary_expr(state, index); - val = val % right; - break; - default: - done = 1; - break; + size_t flen; + flen = char_strlen(file, start, end); + grow_macro_buf(state, id, buf, flen); + char_strcpy(buf->str + buf->pos, file, start, end); +#if 0 + fprintf(state->errout, "append: `%*.*s' `%*.*s'\n", + buf->pos, buf->pos, buf->str, + flen, flen, buf->str + buf->pos); +#endif + buf->pos += flen; +} + +static int compile_macro(struct compile_state *state, + struct file_state **filep, struct token *tk); + +static void macro_expand_args(struct compile_state *state, + struct macro *macro, struct macro_arg_value *argv, struct token *tk) +{ + int i; + + for(i = 0; i < macro->argc; i++) { + struct file_state fmacro, *file; + struct macro_buf buf; + + fmacro.prev = 0; + fmacro.basename = argv[i].ident->name; + fmacro.dirname = ""; + fmacro.buf = (char *)argv[i].value; + fmacro.size = argv[i].len; + fmacro.pos = fmacro.buf; + fmacro.line = 1; + fmacro.line_start = fmacro.buf; + fmacro.report_line = 1; + fmacro.report_name = fmacro.basename; + fmacro.report_dir = fmacro.dirname; + fmacro.macro = 1; + fmacro.trigraphs = 0; + fmacro.join_lines = 0; + + buf.len = argv[i].len; + buf.str = xmalloc(buf.len, argv[i].ident->name); + buf.pos = 0; + + file = &fmacro; + for(;;) { + raw_next_token(state, file, tk); + + /* If we have recursed into another macro body + * get out of it. + */ + if (tk->tok == TOK_EOF) { + struct file_state *old; + old = file; + file = file->prev; + if (!file) { + break; + } + /* old->basename is used keep it */ + xfree(old->dirname); + xfree(old->buf); + xfree(old); + continue; + } + else if (tk->ident && tk->ident->sym_define) { + if (compile_macro(state, &file, tk)) { + continue; + } + } + + append_macro_chars(state, macro->ident->name, &buf, + file, tk->pos, file->pos); } - } while(!done); - return val; + xfree(argv[i].value); + argv[i].value = buf.str; + argv[i].len = buf.pos; + } + return; } -static long_t madd_expr(struct compile_state *state, int index) +static void expand_macro(struct compile_state *state, + struct macro *macro, struct macro_buf *buf, + struct macro_arg_value *argv, struct token *tk) { - long_t val; - int done; - val = mmul_expr(state, index); - do { - long_t right; - done = 0; - switch(mpeek(state, index)) { - case TOK_PLUS: - meat(state, index, TOK_PLUS); - right = mmul_expr(state, index); - val = val + right; + struct file_state fmacro; + const char space[] = " "; + const char *fstart; + size_t flen; + int i, j; + + /* Place the macro body in a dummy file */ + fmacro.prev = 0; + fmacro.basename = macro->ident->name; + fmacro.dirname = ""; + fmacro.buf = macro->buf; + fmacro.size = macro->buf_len; + fmacro.pos = fmacro.buf; + fmacro.line = 1; + fmacro.line_start = fmacro.buf; + fmacro.report_line = 1; + fmacro.report_name = fmacro.basename; + fmacro.report_dir = fmacro.dirname; + fmacro.macro = 1; + fmacro.trigraphs = 0; + fmacro.join_lines = 0; + + /* Allocate a buffer to hold the macro expansion */ + buf->len = macro->buf_len + 3; + buf->str = xmalloc(buf->len, macro->ident->name); + buf->pos = 0; + + fstart = fmacro.pos; + raw_next_token(state, &fmacro, tk); + while(tk->tok != TOK_EOF) { + flen = fmacro.pos - fstart; + switch(tk->tok) { + case TOK_IDENT: + for(i = 0; i < macro->argc; i++) { + if (argv[i].ident == tk->ident) { + break; + } + } + if (i >= macro->argc) { + break; + } + /* Substitute macro parameter */ + fstart = argv[i].value; + flen = argv[i].len; break; - case TOK_MINUS: - meat(state, index, TOK_MINUS); - right = mmul_expr(state, index); - val = val - right; + case TOK_MACRO: + if (macro->argc < 0) { + break; + } + do { + raw_next_token(state, &fmacro, tk); + } while(tk->tok == TOK_SPACE); + check_tok(state, tk, TOK_IDENT); + for(i = 0; i < macro->argc; i++) { + if (argv[i].ident == tk->ident) { + break; + } + } + if (i >= macro->argc) { + error(state, 0, "parameter `%s' not found", + tk->ident->name); + } + /* Stringize token */ + append_macro_text(state, macro->ident->name, buf, "\"", 1); + for(j = 0; j < argv[i].len; j++) { + char *str = argv[i].value + j; + size_t len = 1; + if (*str == '\\') { + str = "\\"; + len = 2; + } + else if (*str == '"') { + str = "\\\""; + len = 2; + } + append_macro_text(state, macro->ident->name, buf, str, len); + } + append_macro_text(state, macro->ident->name, buf, "\"", 1); + fstart = 0; + flen = 0; + break; + case TOK_CONCATENATE: + /* Concatenate tokens */ + /* Delete the previous whitespace token */ + if (buf->str[buf->pos - 1] == ' ') { + buf->pos -= 1; + } + /* Skip the next sequence of whitspace tokens */ + do { + fstart = fmacro.pos; + raw_next_token(state, &fmacro, tk); + } while(tk->tok == TOK_SPACE); + /* Restart at the top of the loop. + * I need to process the non white space token. + */ + continue; + break; + case TOK_SPACE: + /* Collapse multiple spaces into one */ + if (buf->str[buf->pos - 1] != ' ') { + fstart = space; + flen = 1; + } else { + fstart = 0; + flen = 0; + } break; default: - done = 1; break; } - } while(!done); - return val; + append_macro_text(state, macro->ident->name, buf, fstart, flen); + + fstart = fmacro.pos; + raw_next_token(state, &fmacro, tk); + } } -static long_t mshift_expr(struct compile_state *state, int index) +static void tag_macro_name(struct compile_state *state, + struct macro *macro, struct macro_buf *buf, + struct token *tk) { - long_t val; - int done; - val = madd_expr(state, index); - do { - long_t right; - done = 0; - switch(mpeek(state, index)) { - case TOK_SL: - meat(state, index, TOK_SL); - right = madd_expr(state, index); - val = val << right; - break; - case TOK_SR: - meat(state, index, TOK_SR); - right = madd_expr(state, index); - val = val >> right; - break; - default: - done = 1; - break; + /* Guard all instances of the macro name in the replacement + * text from further macro expansion. + */ + struct file_state fmacro; + const char *fstart; + size_t flen; + + /* Put the old macro expansion buffer in a file */ + fmacro.prev = 0; + fmacro.basename = macro->ident->name; + fmacro.dirname = ""; + fmacro.buf = buf->str; + fmacro.size = buf->pos; + fmacro.pos = fmacro.buf; + fmacro.line = 1; + fmacro.line_start = fmacro.buf; + fmacro.report_line = 1; + fmacro.report_name = fmacro.basename; + fmacro.report_dir = fmacro.dirname; + fmacro.macro = 1; + fmacro.trigraphs = 0; + fmacro.join_lines = 0; + + /* Allocate a new macro expansion buffer */ + buf->len = macro->buf_len + 3; + buf->str = xmalloc(buf->len, macro->ident->name); + buf->pos = 0; + + fstart = fmacro.pos; + raw_next_token(state, &fmacro, tk); + while(tk->tok != TOK_EOF) { + flen = fmacro.pos - fstart; + if ((tk->tok == TOK_IDENT) && + (tk->ident == macro->ident) && + (tk->val.notmacro == 0)) + { + append_macro_text(state, macro->ident->name, buf, fstart, flen); + fstart = "$"; + flen = 1; } - } while(!done); - return val; -} + append_macro_text(state, macro->ident->name, buf, fstart, flen); -static long_t mrel_expr(struct compile_state *state, int index) -{ - long_t val; - int done; - val = mshift_expr(state, index); - do { - long_t right; - done = 0; - switch(mpeek(state, index)) { - case TOK_LESS: - meat(state, index, TOK_LESS); - right = mshift_expr(state, index); - val = val < right; - break; - case TOK_MORE: - meat(state, index, TOK_MORE); - right = mshift_expr(state, index); - val = val > right; - break; - case TOK_LESSEQ: - meat(state, index, TOK_LESSEQ); - right = mshift_expr(state, index); - val = val <= right; - break; - case TOK_MOREEQ: - meat(state, index, TOK_MOREEQ); - right = mshift_expr(state, index); - val = val >= right; - break; - default: - done = 1; - break; - } - } while(!done); - return val; + fstart = fmacro.pos; + raw_next_token(state, &fmacro, tk); + } + xfree(fmacro.buf); } -static long_t meq_expr(struct compile_state *state, int index) +static int compile_macro(struct compile_state *state, + struct file_state **filep, struct token *tk) { - long_t val; - int done; - val = mrel_expr(state, index); - do { - long_t right; - done = 0; - switch(mpeek(state, index)) { - case TOK_EQEQ: - meat(state, index, TOK_EQEQ); - right = mrel_expr(state, index); - val = val == right; - break; - case TOK_NOTEQ: - meat(state, index, TOK_NOTEQ); - right = mrel_expr(state, index); - val = val != right; - break; - default: - done = 1; - break; - } - } while(!done); - return val; -} + struct file_state *file; + struct hash_entry *ident; + struct macro *macro; + struct macro_arg_value *argv; + struct macro_buf buf; -static long_t mand_expr(struct compile_state *state, int index) -{ - long_t val; - val = meq_expr(state, index); - if (mpeek(state, index) == TOK_AND) { - long_t right; - meat(state, index, TOK_AND); - right = meq_expr(state, index); - val = val & right; +#if 0 + fprintf(state->errout, "macro: %s\n", tk->ident->name); +#endif + ident = tk->ident; + macro = ident->sym_define; + + /* If this token comes from a macro expansion ignore it */ + if (tk->val.notmacro) { + return 0; + } + /* If I am a function like macro and the identifier is not followed + * by a left parenthesis, do nothing. + */ + if ((macro->argc >= 0) && (get_char(*filep, (*filep)->pos) != '(')) { + return 0; } - return val; -} -static long_t mxor_expr(struct compile_state *state, int index) -{ - long_t val; - val = mand_expr(state, index); - if (mpeek(state, index) == TOK_XOR) { - long_t right; - meat(state, index, TOK_XOR); - right = mand_expr(state, index); - val = val ^ right; + /* Read in the macro arguments */ + argv = 0; + if (macro->argc >= 0) { + raw_next_token(state, *filep, tk); + check_tok(state, tk, TOK_LPAREN); + + argv = read_macro_args(state, macro, *filep, tk); + + check_tok(state, tk, TOK_RPAREN); } - return val; -} + /* Macro expand the macro arguments */ + macro_expand_args(state, macro, argv, tk); -static long_t mor_expr(struct compile_state *state, int index) -{ - long_t val; - val = mxor_expr(state, index); - if (mpeek(state, index) == TOK_OR) { - long_t right; - meat(state, index, TOK_OR); - right = mxor_expr(state, index); - val = val | right; + buf.str = 0; + buf.len = 0; + buf.pos = 0; + if (ident == state->i___FILE__) { + buf.len = strlen(state->file->basename) + 1 + 2 + 3; + buf.str = xmalloc(buf.len, ident->name); + sprintf(buf.str, "\"%s\"", state->file->basename); + buf.pos = strlen(buf.str); } - return val; + else if (ident == state->i___LINE__) { + buf.len = 30; + buf.str = xmalloc(buf.len, ident->name); + sprintf(buf.str, "%d", state->file->line); + buf.pos = strlen(buf.str); + } + else { + expand_macro(state, macro, &buf, argv, tk); + } + /* Tag the macro name with a $ so it will no longer + * be regonized as a canidate for macro expansion. + */ + tag_macro_name(state, macro, &buf, tk); + +#if 0 + fprintf(state->errout, "%s: %d -> `%*.*s'\n", + ident->name, buf.pos, buf.pos, (int)(buf.pos), buf.str); +#endif + + free_macro_args(macro, argv); + + file = xmalloc(sizeof(*file), "file_state"); + file->prev = *filep; + file->basename = xstrdup(ident->name); + file->dirname = xstrdup(""); + file->buf = buf.str; + file->size = buf.pos; + file->pos = file->buf; + file->line = 1; + file->line_start = file->pos; + file->report_line = 1; + file->report_name = file->basename; + file->report_dir = file->dirname; + file->macro = 1; + file->trigraphs = 0; + file->join_lines = 0; + *filep = file; + return 1; } -static long_t mland_expr(struct compile_state *state, int index) +static void eat_tokens(struct compile_state *state, int targ_tok) { - long_t val; - val = mor_expr(state, index); - if (mpeek(state, index) == TOK_LOGAND) { - long_t right; - meat(state, index, TOK_LOGAND); - right = mor_expr(state, index); - val = val && right; + if (state->eat_depth > 0) { + internal_error(state, 0, "Already eating..."); } - return val; + state->eat_depth = state->if_depth; + state->eat_targ = targ_tok; } -static long_t mlor_expr(struct compile_state *state, int index) +static int if_eat(struct compile_state *state) { - long_t val; - val = mland_expr(state, index); - if (mpeek(state, index) == TOK_LOGOR) { - long_t right; - meat(state, index, TOK_LOGOR); - right = mland_expr(state, index); - val = val || right; - } - return val; + return state->eat_depth > 0; } - -static long_t mcexpr(struct compile_state *state, int index) +static int if_value(struct compile_state *state) { - return mlor_expr(state, index); + int index, offset; + index = state->if_depth / CHAR_BIT; + offset = state->if_depth % CHAR_BIT; + return !!(state->if_bytes[index] & (1 << (offset))); } -static void preprocess(struct compile_state *state, int index) +static void set_if_value(struct compile_state *state, int value) { - /* Doing much more with the preprocessor would require - * a parser and a major restructuring. - * Postpone that for later. - */ - struct file_state *file; - struct token *tk; - int line; - int tok; - - file = state->file; - tk = &state->token[index]; - state->macro_line = line = file->line; - state->macro_file = file; - - next_token(state, index); - ident_to_macro(state, tk); - if (tk->tok == TOK_IDENT) { - error(state, 0, "undefined preprocessing directive `%s'", - tk->ident->name); - } - switch(tk->tok) { - case TOK_UNDEF: - case TOK_LINE: - case TOK_PRAGMA: - if (state->if_value < 0) { - break; - } - warning(state, 0, "Ignoring preprocessor directive: %s", - tk->ident->name); - break; - case TOK_ELIF: - error(state, 0, "#elif not supported"); -#warning "FIXME multiple #elif and #else in an #if do not work properly" - if (state->if_depth == 0) { - error(state, 0, "#elif without #if"); - } - /* If the #if was taken the #elif just disables the following code */ - if (state->if_value >= 0) { - state->if_value = - state->if_value; - } - /* If the previous #if was not taken see if the #elif enables the - * trailing code. - */ - else if ((state->if_value < 0) && - (state->if_depth == - state->if_value)) - { - if (mcexpr(state, index) != 0) { - state->if_value = state->if_depth; - } - else { - state->if_value = - state->if_depth; - } - } - break; - case TOK_IF: - state->if_depth++; - if (state->if_value < 0) { - break; - } - if (mcexpr(state, index) != 0) { - state->if_value = state->if_depth; - } - else { - state->if_value = - state->if_depth; - } - break; - case TOK_IFNDEF: - state->if_depth++; - if (state->if_value < 0) { - break; - } - next_token(state, index); - if ((line != file->line) || (tk->tok != TOK_IDENT)) { - error(state, 0, "Invalid macro name"); - } - if (tk->ident->sym_define == 0) { - state->if_value = state->if_depth; - } - else { - state->if_value = - state->if_depth; - } - break; - case TOK_IFDEF: - state->if_depth++; - if (state->if_value < 0) { - break; - } - next_token(state, index); - if ((line != file->line) || (tk->tok != TOK_IDENT)) { - error(state, 0, "Invalid macro name"); - } - if (tk->ident->sym_define != 0) { - state->if_value = state->if_depth; - } - else { - state->if_value = - state->if_depth; - } - break; - case TOK_ELSE: - if (state->if_depth == 0) { - error(state, 0, "#else without #if"); - } - if ((state->if_value >= 0) || - ((state->if_value < 0) && - (state->if_depth == -state->if_value))) - { - state->if_value = - state->if_value; - } - break; - case TOK_ENDIF: - if (state->if_depth == 0) { - error(state, 0, "#endif without #if"); - } - if ((state->if_value >= 0) || - ((state->if_value < 0) && - (state->if_depth == -state->if_value))) - { - state->if_value = state->if_depth - 1; - } - state->if_depth--; - break; - case TOK_DEFINE: - { - struct hash_entry *ident; - struct macro *macro; - char *ptr; - - if (state->if_value < 0) /* quit early when #if'd out */ - break; - - meat(state, index, TOK_IDENT); - ident = tk->ident; - - - if (*file->pos == '(') { -#warning "FIXME macros with arguments not supported" - error(state, 0, "Macros with arguments not supported"); - } - - /* Find the end of the line to get an estimate of - * the macro's length. - */ - for(ptr = file->pos; *ptr != '\n'; ptr++) - ; - - if (ident->sym_define != 0) { - error(state, 0, "macro %s already defined\n", ident->name); - } - macro = xmalloc(sizeof(*macro), "macro"); - macro->ident = ident; - macro->buf_len = ptr - file->pos +1; - macro->buf = xmalloc(macro->buf_len +2, "macro buf"); - - memcpy(macro->buf, file->pos, macro->buf_len); - macro->buf[macro->buf_len] = '\n'; - macro->buf[macro->buf_len +1] = '\0'; + int index, offset; + index = state->if_depth / CHAR_BIT; + offset = state->if_depth % CHAR_BIT; - ident->sym_define = macro; - break; + state->if_bytes[index] &= ~(1 << offset); + if (value) { + state->if_bytes[index] |= (1 << offset); } - case TOK_ERROR: - { - char *end; - int len; - /* Find the end of the line */ - for(end = file->pos; *end != '\n'; end++) - ; - len = (end - file->pos); - if (state->if_value >= 0) { - error(state, 0, "%*.*s", len, len, file->pos); - } - file->pos = end; - break; +} +static void in_if(struct compile_state *state, const char *name) +{ + if (state->if_depth <= 0) { + error(state, 0, "%s without #if", name); } - case TOK_WARNING: - { - char *end; - int len; - /* Find the end of the line */ - for(end = file->pos; *end != '\n'; end++) - ; - len = (end - file->pos); - if (state->if_value >= 0) { - warning(state, 0, "%*.*s", len, len, file->pos); - } - file->pos = end; - break; +} +static void enter_if(struct compile_state *state) +{ + state->if_depth += 1; + if (state->if_depth > MAX_PP_IF_DEPTH) { + error(state, 0, "#if depth too great"); } - case TOK_INCLUDE: - { - char *name; - char *ptr; - int local; - local = 0; - name = 0; - next_token(state, index); - if (tk->tok == TOK_LIT_STRING) { - const char *token; - int name_len; - name = xmalloc(tk->str_len, "include"); - token = tk->val.str +1; - name_len = tk->str_len -2; - if (*token == '"') { - token++; - name_len--; - } - memcpy(name, token, name_len); - name[name_len] = '\0'; - local = 1; - } - else if (tk->tok == TOK_LESS) { - char *start, *end; - start = file->pos; - for(end = start; *end != '\n'; end++) { - if (*end == '>') { - break; - } - } - if (*end == '\n') { - error(state, 0, "Unterminated included directive"); - } - name = xmalloc(end - start + 1, "include"); - memcpy(name, start, end - start); - name[end - start] = '\0'; - file->pos = end +1; - local = 0; - } - else { - error(state, 0, "Invalid include directive"); - } - /* Error if there are any characters after the include */ - for(ptr = file->pos; *ptr != '\n'; ptr++) { - if (!isspace(*ptr)) { - error(state, 0, "garbage after include directive"); - } - } - if (state->if_value >= 0) { - compile_file(state, name, local); - } - xfree(name); - next_token(state, index); - return; +} +static void reenter_if(struct compile_state *state, const char *name) +{ + in_if(state, name); + if ((state->eat_depth == state->if_depth) && + (state->eat_targ == TOK_MELSE)) { + state->eat_depth = 0; + state->eat_targ = 0; } - default: - /* Ignore # without a following ident */ - if (tk->tok == TOK_IDENT) { - error(state, 0, "Invalid preprocessor directive: %s", - tk->ident->name); - } - break; +} +static void enter_else(struct compile_state *state, const char *name) +{ + in_if(state, name); + if ((state->eat_depth == state->if_depth) && + (state->eat_targ == TOK_MELSE)) { + state->eat_depth = 0; + state->eat_targ = 0; } - /* Consume the rest of the macro line */ - do { - tok = mpeek(state, index); - meat(state, index, tok); - } while(tok != TOK_EOF); - return; +} +static void exit_if(struct compile_state *state, const char *name) +{ + in_if(state, name); + if (state->eat_depth == state->if_depth) { + state->eat_depth = 0; + state->eat_targ = 0; + } + state->if_depth -= 1; } -static void token(struct compile_state *state, int index) +static void raw_token(struct compile_state *state, struct token *tk) { struct file_state *file; - struct token *tk; int rescan; - tk = &state->token[index]; - next_token(state, index); + file = state->file; + raw_next_token(state, file, tk); do { rescan = 0; file = state->file; - if (tk->tok == TOK_EOF && file->prev) { + /* Exit out of an include directive or macro call */ + if ((tk->tok == TOK_EOF) && + (file != state->macro_file) && file->prev) + { state->file = file->prev; /* file->basename is used keep it */ xfree(file->dirname); xfree(file->buf); xfree(file); - next_token(state, index); + file = 0; + raw_next_token(state, state->file, tk); rescan = 1; } - else if (tk->tok == TOK_MACRO) { - preprocess(state, index); + } while(rescan); +} + +static void pp_token(struct compile_state *state, struct token *tk) +{ + int rescan; + + raw_token(state, tk); + do { + rescan = 0; + if (tk->tok == TOK_SPACE) { + raw_token(state, tk); rescan = 1; } + else if (tk->tok == TOK_IDENT) { + if (state->token_base == 0) { + ident_to_keyword(state, tk); + } else { + ident_to_macro(state, tk); + } + } + } while(rescan); +} + +static void preprocess(struct compile_state *state, struct token *tk); + +static void token(struct compile_state *state, struct token *tk) +{ + int rescan; + pp_token(state, tk); + do { + rescan = 0; + /* Process a macro directive */ + if (tk->tok == TOK_MACRO) { + /* Only match preprocessor directives at the start of a line */ + const char *ptr; + ptr = state->file->line_start; + while((ptr < tk->pos) + && spacep(get_char(state->file, ptr))) + { + ptr = next_char(state->file, ptr, 1); + } + if (ptr == tk->pos) { + preprocess(state, tk); + rescan = 1; + } + } + /* Expand a macro call */ else if (tk->ident && tk->ident->sym_define) { - compile_macro(state, tk); - next_token(state, index); + rescan = compile_macro(state, &state->file, tk); + if (rescan) { + pp_token(state, tk); + } + } + /* Eat tokens disabled by the preprocessor + * (Unless we are parsing a preprocessor directive + */ + else if (if_eat(state) && (state->token_base == 0)) { + pp_token(state, tk); rescan = 1; } - else if (state->if_value < 0) { - next_token(state, index); + /* Make certain EOL only shows up in preprocessor directives */ + else if ((tk->tok == TOK_EOL) && (state->token_base == 0)) { + pp_token(state, tk); rescan = 1; } + /* Error on unknown tokens */ + else if (tk->tok == TOK_UNKNOWN) { + error(state, 0, "unknown token"); + } } while(rescan); } -static int peek(struct compile_state *state) -{ - if (state->token[1].tok == -1) { - token(state, 1); - } - return state->token[1].tok; -} -static int peek2(struct compile_state *state) +static inline struct token *get_token(struct compile_state *state, int offset) { - if (state->token[1].tok == -1) { - token(state, 1); - } - if (state->token[2].tok == -1) { - token(state, 2); + int index; + index = state->token_base + offset; + if (index >= sizeof(state->token)/sizeof(state->token[0])) { + internal_error(state, 0, "token array to small"); } - return state->token[2].tok; + return &state->token[index]; } -static void eat(struct compile_state *state, int tok) +static struct token *do_eat_token(struct compile_state *state, int tok) { - int next_tok; + struct token *tk; int i; - next_tok = peek(state); - if (next_tok != tok) { - const char *name1, *name2; - name1 = tokens[next_tok]; - name2 = ""; - if (next_tok == TOK_IDENT) { - name2 = state->token[1].ident->name; - } - error(state, 0, "\tfound %s %s expected %s", - name1, name2 ,tokens[tok]); - } + check_tok(state, get_token(state, 1), tok); + /* Free the old token value */ - if (state->token[0].str_len) { - xfree((void *)(state->token[0].val.str)); + tk = get_token(state, 0); + if (tk->str_len) { + memset((void *)tk->val.str, -1, tk->str_len); + xfree(tk->val.str); } - for(i = 0; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) { + /* Overwrite the old token with newer tokens */ + for(i = state->token_base; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) { state->token[i] = state->token[i + 1]; } + /* Clear the last token */ memset(&state->token[i], 0, sizeof(state->token[i])); state->token[i].tok = -1; -} -#warning "FIXME do not hardcode the include paths" -static char *include_paths[] = { - "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/include", - "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/arch/i386/include", - "/home/eric/projects/linuxbios/checkin/solo/freebios2/src", - 0 -}; + /* Return the token */ + return tk; +} -static void compile_file(struct compile_state *state, const char *filename, int local) +static int raw_peek(struct compile_state *state) { - char cwd[4096]; + struct token *tk1; + tk1 = get_token(state, 1); + if (tk1->tok == -1) { + raw_token(state, tk1); + } + return tk1->tok; +} + +static struct token *raw_eat(struct compile_state *state, int tok) +{ + raw_peek(state); + return do_eat_token(state, tok); +} + +static int pp_peek(struct compile_state *state) +{ + struct token *tk1; + tk1 = get_token(state, 1); + if (tk1->tok == -1) { + pp_token(state, tk1); + } + return tk1->tok; +} + +static struct token *pp_eat(struct compile_state *state, int tok) +{ + pp_peek(state); + return do_eat_token(state, tok); +} + +static int peek(struct compile_state *state) +{ + struct token *tk1; + tk1 = get_token(state, 1); + if (tk1->tok == -1) { + token(state, tk1); + } + return tk1->tok; +} + +static int peek2(struct compile_state *state) +{ + struct token *tk1, *tk2; + tk1 = get_token(state, 1); + tk2 = get_token(state, 2); + if (tk1->tok == -1) { + token(state, tk1); + } + if (tk2->tok == -1) { + token(state, tk2); + } + return tk2->tok; +} + +static struct token *eat(struct compile_state *state, int tok) +{ + peek(state); + return do_eat_token(state, tok); +} + +static void compile_file(struct compile_state *state, const char *filename, int local) +{ + char cwd[MAX_CWD_SIZE]; const char *subdir, *base; int subdir_len; struct file_state *file; @@ -3387,16 +5084,15 @@ static void compile_file(struct compile_state *state, const char *filename, int if (getcwd(cwd, sizeof(cwd)) == 0) { die("cwd buffer to small"); } - - if (subdir[0] == '/') { + if ((subdir[0] == '/') || ((subdir[1] == ':') && ((subdir[2] == '/') || (subdir[2] == '\\')))) { file->dirname = xmalloc(subdir_len + 1, "dirname"); memcpy(file->dirname, subdir, subdir_len); file->dirname[subdir_len] = '\0'; } else { - char *dir; + const char *dir; int dirlen; - char **path; + const char **path; /* Find the appropriate directory... */ dir = 0; if (!state->file && exists(cwd, filename)) { @@ -3405,13 +5101,13 @@ static void compile_file(struct compile_state *state, const char *filename, int if (local && state->file && exists(state->file->dirname, filename)) { dir = state->file->dirname; } - for(path = include_paths; !dir && *path; path++) { + for(path = state->compiler->include_paths; !dir && *path; path++) { if (exists(*path, filename)) { dir = *path; } } if (!dir) { - error(state, 0, "Cannot find `%s'\n", filename); + error(state, 0, "Cannot open `%s'\n", filename); } dirlen = strlen(dir); file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname"); @@ -3421,17 +5117,432 @@ static void compile_file(struct compile_state *state, const char *filename, int file->dirname[dirlen + 1 + subdir_len] = '\0'; } file->buf = slurp_file(file->dirname, file->basename, &file->size); - xchdir(cwd); 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->macro = 0; + file->trigraphs = (state->compiler->flags & COMPILER_TRIGRAPHS)? 1: 0; + file->join_lines = 1; + file->prev = state->file; state->file = file; - - process_trigraphs(state); - splice_lines(state); +} + +static struct triple *constant_expr(struct compile_state *state); +static void integral(struct compile_state *state, struct triple *def); + +static int mcexpr(struct compile_state *state) +{ + struct triple *cvalue; + cvalue = constant_expr(state); + integral(state, cvalue); + if (cvalue->op != OP_INTCONST) { + error(state, 0, "integer constant expected"); + } + return cvalue->u.cval != 0; +} + +static void preprocess(struct compile_state *state, struct token *current_token) +{ + /* Doing much more with the preprocessor would require + * a parser and a major restructuring. + * Postpone that for later. + */ + int old_token_base; + int tok; + + state->macro_file = state->file; + + old_token_base = state->token_base; + state->token_base = current_token - state->token; + + tok = pp_peek(state); + switch(tok) { + case TOK_LIT_INT: + { + struct token *tk; + int override_line; + tk = pp_eat(state, TOK_LIT_INT); + override_line = strtoul(tk->val.str, 0, 10); + /* I have a preprocessor line marker parse it */ + if (pp_peek(state) == TOK_LIT_STRING) { + const char *token, *base; + char *name, *dir; + int name_len, dir_len; + tk = pp_eat(state, TOK_LIT_STRING); + 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'; + state->file->report_line = override_line - 1; + state->file->report_name = name; + state->file->report_dir = dir; + state->file->macro = 0; + } + break; + } + case TOK_MLINE: + { + struct token *tk; + pp_eat(state, TOK_MLINE); + tk = eat(state, TOK_LIT_INT); + state->file->report_line = strtoul(tk->val.str, 0, 10) -1; + if (pp_peek(state) == TOK_LIT_STRING) { + const char *token, *base; + char *name, *dir; + int name_len, dir_len; + tk = pp_eat(state, TOK_LIT_STRING); + 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'; + state->file->report_name = name; + state->file->report_dir = dir; + state->file->macro = 0; + } + break; + } + case TOK_MUNDEF: + { + struct hash_entry *ident; + pp_eat(state, TOK_MUNDEF); + if (if_eat(state)) /* quit early when #if'd out */ + break; + + ident = pp_eat(state, TOK_MIDENT)->ident; + + undef_macro(state, ident); + break; + } + case TOK_MPRAGMA: + pp_eat(state, TOK_MPRAGMA); + if (if_eat(state)) /* quit early when #if'd out */ + break; + warning(state, 0, "Ignoring pragma"); + break; + case TOK_MELIF: + pp_eat(state, TOK_MELIF); + reenter_if(state, "#elif"); + if (if_eat(state)) /* quit early when #if'd out */ + break; + /* If the #if was taken the #elif just disables the following code */ + if (if_value(state)) { + eat_tokens(state, TOK_MENDIF); + } + /* If the previous #if was not taken see if the #elif enables the + * trailing code. + */ + else { + set_if_value(state, mcexpr(state)); + if (!if_value(state)) { + eat_tokens(state, TOK_MELSE); + } + } + break; + case TOK_MIF: + pp_eat(state, TOK_MIF); + enter_if(state); + if (if_eat(state)) /* quit early when #if'd out */ + break; + set_if_value(state, mcexpr(state)); + if (!if_value(state)) { + eat_tokens(state, TOK_MELSE); + } + break; + case TOK_MIFNDEF: + { + struct hash_entry *ident; + + pp_eat(state, TOK_MIFNDEF); + enter_if(state); + if (if_eat(state)) /* quit early when #if'd out */ + break; + ident = pp_eat(state, TOK_MIDENT)->ident; + set_if_value(state, ident->sym_define == 0); + if (!if_value(state)) { + eat_tokens(state, TOK_MELSE); + } + break; + } + case TOK_MIFDEF: + { + struct hash_entry *ident; + pp_eat(state, TOK_MIFDEF); + enter_if(state); + if (if_eat(state)) /* quit early when #if'd out */ + break; + ident = pp_eat(state, TOK_MIDENT)->ident; + set_if_value(state, ident->sym_define != 0); + if (!if_value(state)) { + eat_tokens(state, TOK_MELSE); + } + break; + } + case TOK_MELSE: + pp_eat(state, TOK_MELSE); + enter_else(state, "#else"); + if (!if_eat(state) && if_value(state)) { + eat_tokens(state, TOK_MENDIF); + } + break; + case TOK_MENDIF: + pp_eat(state, TOK_MENDIF); + exit_if(state, "#endif"); + break; + case TOK_MDEFINE: + { + struct hash_entry *ident; + struct macro_arg *args, **larg; + const char *mstart, *mend; + int argc; + + pp_eat(state, TOK_MDEFINE); + if (if_eat(state)) /* quit early when #if'd out */ + break; + ident = pp_eat(state, TOK_MIDENT)->ident; + argc = -1; + args = 0; + larg = &args; + + /* Parse macro parameters */ + if (raw_peek(state) == TOK_LPAREN) { + raw_eat(state, TOK_LPAREN); + argc += 1; + + for(;;) { + struct macro_arg *narg, *arg; + struct hash_entry *aident; + int tok; + + tok = pp_peek(state); + if (!args && (tok == TOK_RPAREN)) { + break; + } + else if (tok == TOK_DOTS) { + pp_eat(state, TOK_DOTS); + aident = state->i___VA_ARGS__; + } + else { + aident = pp_eat(state, TOK_MIDENT)->ident; + } + + narg = xcmalloc(sizeof(*arg), "macro arg"); + narg->ident = aident; + + /* Verify I don't have a duplicate identifier */ + for(arg = args; arg; arg = arg->next) { + if (arg->ident == narg->ident) { + error(state, 0, "Duplicate macro arg `%s'", + narg->ident->name); + } + } + /* Add the new argument to the end of the list */ + *larg = narg; + larg = &narg->next; + argc += 1; + + if ((aident == state->i___VA_ARGS__) || + (pp_peek(state) != TOK_COMMA)) { + break; + } + pp_eat(state, TOK_COMMA); + } + pp_eat(state, TOK_RPAREN); + } + /* Remove leading whitespace */ + while(raw_peek(state) == TOK_SPACE) { + raw_eat(state, TOK_SPACE); + } + + /* Remember the start of the macro body */ + tok = raw_peek(state); + mend = mstart = get_token(state, 1)->pos; + + /* Find the end of the macro */ + for(tok = raw_peek(state); tok != TOK_EOL; tok = raw_peek(state)) { + raw_eat(state, tok); + /* Remember the end of the last non space token */ + raw_peek(state); + if (tok != TOK_SPACE) { + mend = get_token(state, 1)->pos; + } + } + + /* Now that I have found the body defined the token */ + do_define_macro(state, ident, + char_strdup(state->file, mstart, mend, "macro buf"), + argc, args); + break; + } + case TOK_MERROR: + { + const char *start, *end; + int len; + + pp_eat(state, TOK_MERROR); + /* Find the start of the line */ + raw_peek(state); + start = get_token(state, 1)->pos; + + /* Find the end of the line */ + while((tok = raw_peek(state)) != TOK_EOL) { + raw_eat(state, tok); + } + end = get_token(state, 1)->pos; + len = end - start; + if (!if_eat(state)) { + error(state, 0, "%*.*s", len, len, start); + } + break; + } + case TOK_MWARNING: + { + const char *start, *end; + int len; + + pp_eat(state, TOK_MWARNING); + + /* Find the start of the line */ + raw_peek(state); + start = get_token(state, 1)->pos; + + /* Find the end of the line */ + while((tok = raw_peek(state)) != TOK_EOL) { + raw_eat(state, tok); + } + end = get_token(state, 1)->pos; + len = end - start; + if (!if_eat(state)) { + warning(state, 0, "%*.*s", len, len, start); + } + break; + } + case TOK_MINCLUDE: + { + char *name; + int local; + local = 0; + name = 0; + + pp_eat(state, TOK_MINCLUDE); + if (if_eat(state)) { + /* Find the end of the line */ + while((tok = raw_peek(state)) != TOK_EOL) { + raw_eat(state, tok); + } + break; + } + tok = peek(state); + if (tok == TOK_LIT_STRING) { + struct token *tk; + const char *token; + int name_len; + tk = eat(state, TOK_LIT_STRING); + name = xmalloc(tk->str_len, "include"); + token = tk->val.str +1; + name_len = tk->str_len -2; + if (*token == '"') { + token++; + name_len--; + } + memcpy(name, token, name_len); + name[name_len] = '\0'; + local = 1; + } + else if (tok == TOK_LESS) { + struct macro_buf buf; + eat(state, TOK_LESS); + + buf.len = 40; + buf.str = xmalloc(buf.len, "include"); + buf.pos = 0; + + tok = peek(state); + while((tok != TOK_MORE) && + (tok != TOK_EOL) && (tok != TOK_EOF)) + { + struct token *tk; + tk = eat(state, tok); + append_macro_chars(state, "include", &buf, + state->file, tk->pos, state->file->pos); + tok = peek(state); + } + append_macro_text(state, "include", &buf, "\0", 1); + if (peek(state) != TOK_MORE) { + error(state, 0, "Unterminated include directive"); + } + eat(state, TOK_MORE); + local = 0; + name = buf.str; + } + else { + error(state, 0, "Invalid include directive"); + } + /* Error if there are any tokens after the include */ + if (pp_peek(state) != TOK_EOL) { + error(state, 0, "garbage after include directive"); + } + if (!if_eat(state)) { + compile_file(state, name, local); + } + xfree(name); + break; + } + case TOK_EOL: + /* Ignore # without a follwing ident */ + break; + default: + { + const char *name1, *name2; + name1 = tokens[tok]; + name2 = ""; + if (tok == TOK_MIDENT) { + name2 = get_token(state, 1)->ident->name; + } + error(state, 0, "Invalid preprocessor directive: %s %s", + name1, name2); + break; + } + } + /* Consume the rest of the macro line */ + do { + tok = pp_peek(state); + pp_eat(state, tok); + } while((tok != TOK_EOF) && (tok != TOK_EOL)); + state->token_base = old_token_base; + state->macro_file = NULL; + return; } /* Type helper functions */ @@ -3446,6 +5557,7 @@ static struct type *new_type( result->right = right; result->field_ident = 0; result->type_ident = 0; + result->elements = 0; return result; } @@ -3459,20 +5571,85 @@ static struct type *clone_type(unsigned int specifiers, struct type *old) return result; } -#define SIZEOF_SHORT 2 -#define SIZEOF_INT 4 -#define SIZEOF_LONG (sizeof(long_t)) +static struct type *dup_type(struct compile_state *state, struct type *orig) +{ + struct type *new; + new = xcmalloc(sizeof(*new), "type"); + new->type = orig->type; + new->field_ident = orig->field_ident; + new->type_ident = orig->type_ident; + new->elements = orig->elements; + if (orig->left) { + new->left = dup_type(state, orig->left); + } + if (orig->right) { + new->right = dup_type(state, orig->right); + } + return new; +} + + +static struct type *invalid_type(struct compile_state *state, struct type *type) +{ + struct type *invalid, *member; + invalid = 0; + if (!type) { + internal_error(state, 0, "type missing?"); + } + switch(type->type & TYPE_MASK) { + case TYPE_VOID: + case TYPE_CHAR: case TYPE_UCHAR: + case TYPE_SHORT: case TYPE_USHORT: + case TYPE_INT: case TYPE_UINT: + case TYPE_LONG: case TYPE_ULONG: + case TYPE_LLONG: case TYPE_ULLONG: + case TYPE_POINTER: + case TYPE_ENUM: + break; + case TYPE_BITFIELD: + invalid = invalid_type(state, type->left); + break; + case TYPE_ARRAY: + invalid = invalid_type(state, type->left); + break; + case TYPE_STRUCT: + case TYPE_TUPLE: + member = type->left; + while(member && (invalid == 0) && + ((member->type & TYPE_MASK) == TYPE_PRODUCT)) { + invalid = invalid_type(state, member->left); + member = member->right; + } + if (!invalid) { + invalid = invalid_type(state, member); + } + break; + case TYPE_UNION: + case TYPE_JOIN: + member = type->left; + while(member && (invalid == 0) && + ((member->type & TYPE_MASK) == TYPE_OVERLAP)) { + invalid = invalid_type(state, member->left); + member = member->right; + } + if (!invalid) { + invalid = invalid_type(state, member); + } + break; + default: + invalid = type; + break; + } + return invalid; -#define ALIGNOF_SHORT 2 -#define ALIGNOF_INT 4 -#define ALIGNOF_LONG (sizeof(long_t)) +} #define MASK_UCHAR(X) ((X) & ((ulong_t)0xff)) -#define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT*8)) - 1)) +#define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT)) - 1)) static inline ulong_t mask_uint(ulong_t x) { if (SIZEOF_INT < SIZEOF_LONG) { - ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT*8))) -1; + ulong_t mask = (1ULL << ((ulong_t)(SIZEOF_INT))) -1; x &= mask; } return x; @@ -3480,38 +5657,43 @@ 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 }; -static struct type short_type = { .type = TYPE_SHORT }; -static struct type ushort_type = { .type = TYPE_USHORT }; -static struct type int_type = { .type = TYPE_INT }; -static struct type uint_type = { .type = TYPE_UINT }; -static struct type long_type = { .type = TYPE_LONG }; -static struct type ulong_type = { .type = TYPE_ULONG }; +static struct type void_type = { .type = TYPE_VOID }; +static struct type char_type = { .type = TYPE_CHAR }; +static struct type uchar_type = { .type = TYPE_UCHAR }; +#if DEBUG_ROMCC_WARNING +static struct type short_type = { .type = TYPE_SHORT }; +#endif +static struct type ushort_type = { .type = TYPE_USHORT }; +static struct type int_type = { .type = TYPE_INT }; +static struct type uint_type = { .type = TYPE_UINT }; +static struct type long_type = { .type = TYPE_LONG }; +static struct type ulong_type = { .type = TYPE_ULONG }; +static struct type unknown_type = { .type = TYPE_UNKNOWN }; + +static struct type void_ptr_type = { + .type = TYPE_POINTER, + .left = &void_type, +}; + +#if DEBUG_ROMCC_WARNING +static struct type void_func_type = { + .type = TYPE_FUNCTION, + .left = &void_type, + .right = &void_type, +}; +#endif + +static size_t bits_to_bytes(size_t size) +{ + return (size + SIZEOF_CHAR - 1)/SIZEOF_CHAR; +} static struct triple *variable(struct compile_state *state, struct type *type) { struct triple *result; if ((type->type & STOR_MASK) != STOR_PERM) { - 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); - } + result = triple(state, OP_ADECL, type, 0, 0); + generate_lhs_pieces(state, result); } else { result = triple(state, OP_SDECL, type, 0, 0); @@ -3528,6 +5710,9 @@ static void stor_of(FILE *fp, struct type *type) case STOR_STATIC: fprintf(fp, "static "); break; + case STOR_LOCAL: + fprintf(fp, "local "); + break; case STOR_EXTERN: fprintf(fp, "extern "); break; @@ -3537,9 +5722,18 @@ static void stor_of(FILE *fp, struct type *type) case STOR_TYPEDEF: fprintf(fp, "typedef "); break; - case STOR_INLINE: + case STOR_INLINE | STOR_LOCAL: fprintf(fp, "inline "); break; + case STOR_INLINE | STOR_STATIC: + fprintf(fp, "static inline"); + break; + case STOR_INLINE | STOR_EXTERN: + fprintf(fp, "extern inline"); + break; + default: + fprintf(fp, "stor:%x", type->type & STOR_MASK); + break; } } static void qual_of(FILE *fp, struct type *type) @@ -3557,8 +5751,12 @@ static void qual_of(FILE *fp, struct type *type) static void name_of(FILE *fp, struct type *type) { - stor_of(fp, type); - switch(type->type & TYPE_MASK) { + unsigned int base_type; + base_type = type->type & TYPE_MASK; + if ((base_type != TYPE_PRODUCT) && (base_type != TYPE_OVERLAP)) { + stor_of(fp, type); + } + switch(base_type) { case TYPE_VOID: fprintf(fp, "void"); qual_of(fp, type); @@ -3601,34 +5799,70 @@ static void name_of(FILE *fp, struct type *type) qual_of(fp, type); break; case TYPE_PRODUCT: - case TYPE_OVERLAP: name_of(fp, type->left); fprintf(fp, ", "); name_of(fp, type->right); break; + case TYPE_OVERLAP: + name_of(fp, type->left); + fprintf(fp, ",| "); + name_of(fp, type->right); + break; case TYPE_ENUM: - fprintf(fp, "enum %s", type->type_ident->name); + fprintf(fp, "enum %s", + (type->type_ident)? type->type_ident->name : ""); qual_of(fp, type); break; case TYPE_STRUCT: - fprintf(fp, "struct %s", type->type_ident->name); + fprintf(fp, "struct %s { ", + (type->type_ident)? type->type_ident->name : ""); + name_of(fp, type->left); + fprintf(fp, " } "); + qual_of(fp, type); + break; + case TYPE_UNION: + fprintf(fp, "union %s { ", + (type->type_ident)? type->type_ident->name : ""); + name_of(fp, type->left); + fprintf(fp, " } "); qual_of(fp, type); break; case TYPE_FUNCTION: - { name_of(fp, type->left); fprintf(fp, " (*)("); name_of(fp, type->right); fprintf(fp, ")"); break; - } case TYPE_ARRAY: name_of(fp, type->left); - fprintf(fp, " [%ld]", type->elements); + fprintf(fp, " [%ld]", (long)(type->elements)); break; - default: - fprintf(fp, "????: %x", type->type & TYPE_MASK); + case TYPE_TUPLE: + fprintf(fp, "tuple { "); + name_of(fp, type->left); + fprintf(fp, " } "); + qual_of(fp, type); break; + case TYPE_JOIN: + fprintf(fp, "join { "); + name_of(fp, type->left); + fprintf(fp, " } "); + qual_of(fp, type); + break; + case TYPE_BITFIELD: + name_of(fp, type->left); + fprintf(fp, " : %d ", type->elements); + qual_of(fp, type); + break; + case TYPE_UNKNOWN: + fprintf(fp, "unknown_t"); + break; + default: + fprintf(fp, "????: %x", base_type); + break; + } + if (type->field_ident && type->field_ident->name) { + fprintf(fp, " .%s", type->field_ident->name); } } @@ -3640,9 +5874,12 @@ static size_t align_of(struct compile_state *state, struct type *type) case TYPE_VOID: align = 1; break; + case TYPE_BITFIELD: + align = 1; + break; case TYPE_CHAR: case TYPE_UCHAR: - align = 1; + align = ALIGNOF_CHAR; break; case TYPE_SHORT: case TYPE_USHORT: @@ -3655,9 +5892,11 @@ static size_t align_of(struct compile_state *state, struct type *type) break; case TYPE_LONG: case TYPE_ULONG: - case TYPE_POINTER: align = ALIGNOF_LONG; break; + case TYPE_POINTER: + align = ALIGNOF_POINTER; + break; case TYPE_PRODUCT: case TYPE_OVERLAP: { @@ -3671,6 +5910,9 @@ static size_t align_of(struct compile_state *state, struct type *type) align = align_of(state, type->left); break; case TYPE_STRUCT: + case TYPE_TUPLE: + case TYPE_UNION: + case TYPE_JOIN: align = align_of(state, type->left); break; default: @@ -3680,6 +5922,111 @@ static size_t align_of(struct compile_state *state, struct type *type) return align; } +static size_t reg_align_of(struct compile_state *state, struct type *type) +{ + size_t align; + align = 0; + switch(type->type & TYPE_MASK) { + case TYPE_VOID: + align = 1; + break; + case TYPE_BITFIELD: + align = 1; + break; + case TYPE_CHAR: + case TYPE_UCHAR: + align = REG_ALIGNOF_CHAR; + break; + case TYPE_SHORT: + case TYPE_USHORT: + align = REG_ALIGNOF_SHORT; + break; + case TYPE_INT: + case TYPE_UINT: + case TYPE_ENUM: + align = REG_ALIGNOF_INT; + break; + case TYPE_LONG: + case TYPE_ULONG: + align = REG_ALIGNOF_LONG; + break; + case TYPE_POINTER: + align = REG_ALIGNOF_POINTER; + break; + case TYPE_PRODUCT: + case TYPE_OVERLAP: + { + size_t left_align, right_align; + left_align = reg_align_of(state, type->left); + right_align = reg_align_of(state, type->right); + align = (left_align >= right_align) ? left_align : right_align; + break; + } + case TYPE_ARRAY: + align = reg_align_of(state, type->left); + break; + case TYPE_STRUCT: + case TYPE_UNION: + case TYPE_TUPLE: + case TYPE_JOIN: + align = reg_align_of(state, type->left); + break; + default: + error(state, 0, "alignof not yet defined for type\n"); + break; + } + return align; +} + +static size_t align_of_in_bytes(struct compile_state *state, struct type *type) +{ + return bits_to_bytes(align_of(state, type)); +} +static size_t size_of(struct compile_state *state, struct type *type); +static size_t reg_size_of(struct compile_state *state, struct type *type); + +static size_t needed_padding(struct compile_state *state, + struct type *type, size_t offset) +{ + size_t padding, align; + align = align_of(state, type); + /* Align to the next machine word if the bitfield does completely + * fit into the current word. + */ + if ((type->type & TYPE_MASK) == TYPE_BITFIELD) { + size_t size; + size = size_of(state, type); + if ((offset + type->elements)/size != offset/size) { + align = size; + } + } + padding = 0; + if (offset % align) { + padding = align - (offset % align); + } + return padding; +} + +static size_t reg_needed_padding(struct compile_state *state, + struct type *type, size_t offset) +{ + size_t padding, align; + align = reg_align_of(state, type); + /* Align to the next register word if the bitfield does completely + * fit into the current register. + */ + if (((type->type & TYPE_MASK) == TYPE_BITFIELD) && + (((offset + type->elements)/REG_SIZEOF_REG) != (offset/REG_SIZEOF_REG))) + { + align = REG_SIZEOF_REG; + } + 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; @@ -3688,9 +6035,12 @@ static size_t size_of(struct compile_state *state, struct type *type) case TYPE_VOID: size = 0; break; + case TYPE_BITFIELD: + size = type->elements; + break; case TYPE_CHAR: case TYPE_UCHAR: - size = 1; + size = SIZEOF_CHAR; break; case TYPE_SHORT: case TYPE_USHORT: @@ -3703,22 +6053,22 @@ static size_t size_of(struct compile_state *state, struct type *type) break; case TYPE_LONG: case TYPE_ULONG: - case TYPE_POINTER: size = SIZEOF_LONG; break; + case TYPE_POINTER: + size = SIZEOF_POINTER; + break; case TYPE_PRODUCT: { - size_t align, pad; - size = size_of(state, type->left); - while((type->right->type & TYPE_MASK) == TYPE_PRODUCT) { - type = type->right; - align = align_of(state, type->left); - pad = align - (size % align); + size_t pad; + size = 0; + while((type->type & TYPE_MASK) == TYPE_PRODUCT) { + pad = needed_padding(state, type->left, size); 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); + pad = needed_padding(state, type, size); + size = size + pad + size_of(state, type); break; } case TYPE_OVERLAP: @@ -3737,317 +6087,1051 @@ static size_t size_of(struct compile_state *state, struct type *type) } break; case TYPE_STRUCT: + case TYPE_TUPLE: + { + size_t pad; + size = size_of(state, type->left); + /* Pad structures so their size is a multiples of their alignment */ + pad = needed_padding(state, type, size); + size = size + pad; + break; + } + case TYPE_UNION: + case TYPE_JOIN: + { + size_t pad; size = size_of(state, type->left); + /* Pad unions so their size is a multiple of their alignment */ + pad = needed_padding(state, type, size); + size = size + pad; break; + } default: - error(state, 0, "sizeof not yet defined for type\n"); + internal_error(state, 0, "sizeof not yet defined for type"); break; } return size; } -static size_t field_offset(struct compile_state *state, - struct type *type, struct hash_entry *field) +static size_t reg_size_of(struct compile_state *state, struct type *type) { - size_t size, align, pad; - if ((type->type & TYPE_MASK) != TYPE_STRUCT) { - internal_error(state, 0, "field_offset only works on structures"); - } + size_t size; size = 0; - type = type->left; - while((type->type & TYPE_MASK) == TYPE_PRODUCT) { - if (type->left->field_ident == field) { - type = type->left; + switch(type->type & TYPE_MASK) { + case TYPE_VOID: + size = 0; + break; + case TYPE_BITFIELD: + size = type->elements; + break; + case TYPE_CHAR: + case TYPE_UCHAR: + size = REG_SIZEOF_CHAR; + break; + case TYPE_SHORT: + case TYPE_USHORT: + size = REG_SIZEOF_SHORT; + break; + case TYPE_INT: + case TYPE_UINT: + case TYPE_ENUM: + size = REG_SIZEOF_INT; + break; + case TYPE_LONG: + case TYPE_ULONG: + size = REG_SIZEOF_LONG; + break; + case TYPE_POINTER: + size = REG_SIZEOF_POINTER; + break; + case TYPE_PRODUCT: + { + size_t pad; + size = 0; + while((type->type & TYPE_MASK) == TYPE_PRODUCT) { + pad = reg_needed_padding(state, type->left, size); + size = size + pad + reg_size_of(state, type->left); + type = type->right; } - size += size_of(state, type->left); - type = type->right; - align = align_of(state, type->left); - pad = align - (size % align); - size += pad; + pad = reg_needed_padding(state, type, size); + size = size + pad + reg_size_of(state, type); + break; } - if (type->field_ident != field) { - internal_error(state, 0, "field_offset: member %s not present", - field->name); + case TYPE_OVERLAP: + { + size_t size_left, size_right; + size_left = reg_size_of(state, type->left); + size_right = reg_size_of(state, type->right); + size = (size_left >= size_right)? size_left : size_right; + break; + } + case TYPE_ARRAY: + if (type->elements == ELEMENT_COUNT_UNSPECIFIED) { + internal_error(state, 0, "Invalid array type"); + } else { + size = reg_size_of(state, type->left) * type->elements; + } + break; + case TYPE_STRUCT: + case TYPE_TUPLE: + { + size_t pad; + size = reg_size_of(state, type->left); + /* Pad structures so their size is a multiples of their alignment */ + pad = reg_needed_padding(state, type, size); + size = size + pad; + break; + } + case TYPE_UNION: + case TYPE_JOIN: + { + size_t pad; + size = reg_size_of(state, type->left); + /* Pad unions so their size is a multiple of their alignment */ + pad = reg_needed_padding(state, type, size); + size = size + pad; + break; + } + default: + internal_error(state, 0, "sizeof not yet defined for type"); + break; } return size; } -static struct type *field_type(struct compile_state *state, +static size_t registers_of(struct compile_state *state, struct type *type) +{ + size_t registers; + registers = reg_size_of(state, type); + registers += REG_SIZEOF_REG - 1; + registers /= REG_SIZEOF_REG; + return registers; +} + +static size_t size_of_in_bytes(struct compile_state *state, struct type *type) +{ + return bits_to_bytes(size_of(state, type)); +} + +static size_t field_offset(struct compile_state *state, struct type *type, struct hash_entry *field) { - if ((type->type & TYPE_MASK) != TYPE_STRUCT) { - internal_error(state, 0, "field_type only works on structures"); + struct type *member; + size_t size; + + size = 0; + member = 0; + if ((type->type & TYPE_MASK) == TYPE_STRUCT) { + member = type->left; + while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) { + size += needed_padding(state, member->left, size); + if (member->left->field_ident == field) { + member = member->left; + break; + } + size += size_of(state, member->left); + member = member->right; + } + size += needed_padding(state, member, size); } - type = type->left; - while((type->type & TYPE_MASK) == TYPE_PRODUCT) { - if (type->left->field_ident == field) { - type = type->left; - break; + else if ((type->type & TYPE_MASK) == TYPE_UNION) { + member = type->left; + while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) { + if (member->left->field_ident == field) { + member = member->left; + break; + } + member = member->right; } - type = type->right; } - if (type->field_ident != field) { - internal_error(state, 0, "field_type: member %s not present", - field->name); + else { + internal_error(state, 0, "field_offset only works on structures and unions"); } - return type; + + if (!member || (member->field_ident != field)) { + error(state, 0, "member %s not present", field->name); + } + return size; } -static struct triple *struct_field(struct compile_state *state, - struct triple *decl, struct hash_entry *field) +static size_t field_reg_offset(struct compile_state *state, + struct type *type, 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"); + struct type *member; + size_t size; + + size = 0; + member = 0; + if ((type->type & TYPE_MASK) == TYPE_STRUCT) { + member = type->left; + while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) { + size += reg_needed_padding(state, member->left, size); + if (member->left->field_ident == field) { + member = member->left; + break; + } + size += reg_size_of(state, member->left); + member = member->right; + } } - 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; + else if ((type->type & TYPE_MASK) == TYPE_UNION) { + member = type->left; + while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) { + if (member->left->field_ident == field) { + member = member->left; + break; + } + member = member->right; } - index += 1; - type = type->right; } - if (type->field_ident != field) { - internal_error(state, 0, "field %s not found?", field->name); + else { + internal_error(state, 0, "field_reg_offset only works on structures and unions"); } - return vector[index]; -} -static void arrays_complete(struct compile_state *state, struct type *type) -{ - if ((type->type & TYPE_MASK) == TYPE_ARRAY) { - if (type->elements == ELEMENT_COUNT_UNSPECIFIED) { - error(state, 0, "array size not specified"); - } - arrays_complete(state, type->left); + size += reg_needed_padding(state, member, size); + if (!member || (member->field_ident != field)) { + error(state, 0, "member %s not present", field->name); } + return size; } -static unsigned int do_integral_promotion(unsigned int type) +static struct type *field_type(struct compile_state *state, + struct type *type, struct hash_entry *field) { - type &= TYPE_MASK; - if (TYPE_INTEGER(type) && - TYPE_RANK(type) < TYPE_RANK(TYPE_INT)) { - type = TYPE_INT; - } - return type; -} + struct type *member; -static unsigned int do_arithmetic_conversion( - unsigned int left, unsigned int right) -{ - left &= TYPE_MASK; - right &= TYPE_MASK; - if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) { - return TYPE_LDOUBLE; - } - else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) { - return TYPE_DOUBLE; + member = 0; + if ((type->type & TYPE_MASK) == TYPE_STRUCT) { + member = type->left; + while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) { + if (member->left->field_ident == field) { + member = member->left; + break; + } + member = member->right; + } } - else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) { - return TYPE_FLOAT; + else if ((type->type & TYPE_MASK) == TYPE_UNION) { + member = type->left; + while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) { + if (member->left->field_ident == field) { + member = member->left; + break; + } + member = member->right; + } } - left = do_integral_promotion(left); - right = do_integral_promotion(right); - /* If both operands have the same size done */ - if (left == right) { - return left; + else { + internal_error(state, 0, "field_type only works on structures and unions"); } - /* If both operands have the same signedness pick the larger */ - else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) { - return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right; + + if (!member || (member->field_ident != field)) { + error(state, 0, "member %s not present", field->name); } - /* If the signed type can hold everything use it */ - else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) { - return left; + return member; +} + +static size_t index_offset(struct compile_state *state, + struct type *type, ulong_t index) +{ + struct type *member; + size_t size; + size = 0; + if ((type->type & TYPE_MASK) == TYPE_ARRAY) { + size = size_of(state, type->left) * index; } - else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) { - return right; + else if ((type->type & TYPE_MASK) == TYPE_TUPLE) { + ulong_t i; + member = type->left; + i = 0; + while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) { + size += needed_padding(state, member->left, size); + if (i == index) { + member = member->left; + break; + } + size += size_of(state, member->left); + i++; + member = member->right; + } + size += needed_padding(state, member, size); + if (i != index) { + internal_error(state, 0, "Missing member index: %u", index); + } } - /* Convert to the unsigned type with the same rank as the signed type */ - else if (TYPE_SIGNED(left)) { - return TYPE_MKUNSIGNED(left); + else if ((type->type & TYPE_MASK) == TYPE_JOIN) { + ulong_t i; + size = 0; + member = type->left; + i = 0; + while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) { + if (i == index) { + member = member->left; + break; + } + i++; + member = member->right; + } + if (i != index) { + internal_error(state, 0, "Missing member index: %u", index); + } } else { - return TYPE_MKUNSIGNED(right); + internal_error(state, 0, + "request for index %u in something not an array, tuple or join", + index); } + return size; } -/* see if two types are the same except for qualifiers */ -static int equiv_types(struct type *left, struct type *right) +static size_t index_reg_offset(struct compile_state *state, + struct type *type, ulong_t index) { - unsigned int type; - /* Error if the basic types do not match */ - if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) { - return 0; + struct type *member; + size_t size; + size = 0; + if ((type->type & TYPE_MASK) == TYPE_ARRAY) { + size = reg_size_of(state, type->left) * index; } - type = left->type & TYPE_MASK; - /* if the basic types match and it is an arithmetic type we are done */ - if (TYPE_ARITHMETIC(type)) { - return 1; + else if ((type->type & TYPE_MASK) == TYPE_TUPLE) { + ulong_t i; + member = type->left; + i = 0; + while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) { + size += reg_needed_padding(state, member->left, size); + if (i == index) { + member = member->left; + break; + } + size += reg_size_of(state, member->left); + i++; + member = member->right; + } + size += reg_needed_padding(state, member, size); + if (i != index) { + internal_error(state, 0, "Missing member index: %u", index); + } + } - /* If it is a pointer type recurse and keep testing */ - if (type == TYPE_POINTER) { - return equiv_types(left->left, right->left); + else if ((type->type & TYPE_MASK) == TYPE_JOIN) { + ulong_t i; + size = 0; + member = type->left; + i = 0; + while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) { + if (i == index) { + member = member->left; + break; + } + i++; + member = member->right; + } + if (i != index) { + internal_error(state, 0, "Missing member index: %u", index); + } } - else if (type == TYPE_ARRAY) { - return (left->elements == right->elements) && - equiv_types(left->left, right->left); + else { + internal_error(state, 0, + "request for index %u in something not an array, tuple or join", + index); } - /* test for struct/union equality */ - else if (type == TYPE_STRUCT) { - return left->type_ident == right->type_ident; + return size; +} + +static struct type *index_type(struct compile_state *state, + struct type *type, ulong_t index) +{ + struct type *member; + if (index >= type->elements) { + internal_error(state, 0, "Invalid element %u requested", index); } - /* Test for equivalent functions */ - else if (type == TYPE_FUNCTION) { - return equiv_types(left->left, right->left) && - equiv_types(left->right, right->right); + if ((type->type & TYPE_MASK) == TYPE_ARRAY) { + member = type->left; } - /* We only see TYPE_PRODUCT as part of function equivalence matching */ - else if (type == TYPE_PRODUCT) { - return equiv_types(left->left, right->left) && - equiv_types(left->right, right->right); + else if ((type->type & TYPE_MASK) == TYPE_TUPLE) { + ulong_t i; + member = type->left; + i = 0; + while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) { + if (i == index) { + member = member->left; + break; + } + i++; + member = member->right; + } + if (i != index) { + internal_error(state, 0, "Missing member index: %u", index); + } + } + else if ((type->type & TYPE_MASK) == TYPE_JOIN) { + ulong_t i; + member = type->left; + i = 0; + while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) { + if (i == index) { + member = member->left; + break; + } + i++; + member = member->right; + } + if (i != index) { + internal_error(state, 0, "Missing member index: %u", index); + } } - /* We should see TYPE_OVERLAP */ else { - return 0; + member = 0; + internal_error(state, 0, + "request for index %u in something not an array, tuple or join", + index); } + return member; } -static int equiv_ptrs(struct type *left, struct type *right) +static struct type *unpack_type(struct compile_state *state, struct type *type) { - if (((left->type & TYPE_MASK) != TYPE_POINTER) || - ((right->type & TYPE_MASK) != TYPE_POINTER)) { - return 0; + /* If I have a single register compound type not a bit-field + * find the real type. + */ + struct type *start_type; + size_t size; + /* Get out early if I need multiple registers for this type */ + size = reg_size_of(state, type); + if (size > REG_SIZEOF_REG) { + return type; } - return equiv_types(left->left, right->left); + /* Get out early if I don't need any registers for this type */ + if (size == 0) { + return &void_type; + } + /* Loop until I have no more layers I can remove */ + do { + start_type = type; + switch(type->type & TYPE_MASK) { + case TYPE_ARRAY: + /* If I have a single element the unpacked type + * is that element. + */ + if (type->elements == 1) { + type = type->left; + } + break; + case TYPE_STRUCT: + case TYPE_TUPLE: + /* If I have a single element the unpacked type + * is that element. + */ + if (type->elements == 1) { + type = type->left; + } + /* If I have multiple elements the unpacked + * type is the non-void element. + */ + else { + struct type *next, *member; + struct type *sub_type; + sub_type = 0; + next = type->left; + while(next) { + member = next; + next = 0; + if ((member->type & TYPE_MASK) == TYPE_PRODUCT) { + next = member->right; + member = member->left; + } + if (reg_size_of(state, member) > 0) { + if (sub_type) { + internal_error(state, 0, "true compound type in a register"); + } + sub_type = member; + } + } + if (sub_type) { + type = sub_type; + } + } + break; + + case TYPE_UNION: + case TYPE_JOIN: + /* If I have a single element the unpacked type + * is that element. + */ + if (type->elements == 1) { + type = type->left; + } + /* I can't in general unpack union types */ + break; + default: + /* If I'm not a compound type I can't unpack it */ + break; + } + } while(start_type != type); + switch(type->type & TYPE_MASK) { + case TYPE_STRUCT: + case TYPE_ARRAY: + case TYPE_TUPLE: + internal_error(state, 0, "irredicible type?"); + break; + } + return type; } -static struct type *compatible_types(struct type *left, struct type *right) +static int equiv_types(struct type *left, struct type *right); +static int is_compound_type(struct type *type); + +static struct type *reg_type( + struct compile_state *state, struct type *type, int reg_offset) { - struct type *result; - unsigned int type, qual_type; - /* Error if the basic types do not match */ - if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) { - return 0; + struct type *member; + size_t size; +#if 1 + struct type *invalid; + invalid = invalid_type(state, type); + if (invalid) { + fprintf(state->errout, "type: "); + name_of(state->errout, type); + fprintf(state->errout, "\n"); + fprintf(state->errout, "invalid: "); + name_of(state->errout, invalid); + fprintf(state->errout, "\n"); + internal_error(state, 0, "bad input type?"); } - type = left->type & TYPE_MASK; - qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK); - result = 0; - /* if the basic types match and it is an arithmetic type we are done */ - if (TYPE_ARITHMETIC(type)) { - result = new_type(qual_type, 0, 0); +#endif + + size = reg_size_of(state, type); + if (reg_offset > size) { + member = 0; + fprintf(state->errout, "type: "); + name_of(state->errout, type); + fprintf(state->errout, "\n"); + internal_error(state, 0, "offset outside of type"); } - /* If it is a pointer type recurse and keep testing */ - else if (type == TYPE_POINTER) { - result = compatible_types(left->left, right->left); - if (result) { - result = new_type(qual_type, result, 0); + else { + switch(type->type & TYPE_MASK) { + /* Don't do anything with the basic types */ + case TYPE_VOID: + case TYPE_CHAR: case TYPE_UCHAR: + case TYPE_SHORT: case TYPE_USHORT: + case TYPE_INT: case TYPE_UINT: + case TYPE_LONG: case TYPE_ULONG: + case TYPE_LLONG: case TYPE_ULLONG: + case TYPE_FLOAT: case TYPE_DOUBLE: + case TYPE_LDOUBLE: + case TYPE_POINTER: + case TYPE_ENUM: + case TYPE_BITFIELD: + member = type; + break; + case TYPE_ARRAY: + member = type->left; + size = reg_size_of(state, member); + if (size > REG_SIZEOF_REG) { + member = reg_type(state, member, reg_offset % size); + } + break; + case TYPE_STRUCT: + case TYPE_TUPLE: + { + size_t offset; + offset = 0; + member = type->left; + while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) { + size = reg_size_of(state, member->left); + offset += reg_needed_padding(state, member->left, offset); + if ((offset + size) > reg_offset) { + member = member->left; + break; + } + offset += size; + member = member->right; + } + offset += reg_needed_padding(state, member, offset); + member = reg_type(state, member, reg_offset - offset); + break; } - } - /* test for struct/union equality */ - else if (type == TYPE_STRUCT) { - if (left->type_ident == right->type_ident) { - result = left; + case TYPE_UNION: + case TYPE_JOIN: + { + struct type *join, **jnext, *mnext; + join = new_type(TYPE_JOIN, 0, 0); + jnext = &join->left; + mnext = type->left; + while(mnext) { + size_t size; + member = mnext; + mnext = 0; + if ((member->type & TYPE_MASK) == TYPE_OVERLAP) { + mnext = member->right; + member = member->left; + } + size = reg_size_of(state, member); + if (size > reg_offset) { + struct type *part, *hunt; + part = reg_type(state, member, reg_offset); + /* See if this type is already in the union */ + hunt = join->left; + while(hunt) { + struct type *test = hunt; + hunt = 0; + if ((test->type & TYPE_MASK) == TYPE_OVERLAP) { + hunt = test->right; + test = test->left; + } + if (equiv_types(part, test)) { + goto next; + } + } + /* Nope add it */ + if (!*jnext) { + *jnext = part; + } else { + *jnext = new_type(TYPE_OVERLAP, *jnext, part); + jnext = &(*jnext)->right; + } + join->elements++; + } + next: + ; + } + if (join->elements == 0) { + internal_error(state, 0, "No elements?"); + } + member = join; + break; } - } - /* Test for equivalent functions */ - else if (type == TYPE_FUNCTION) { - struct type *lf, *rf; - lf = compatible_types(left->left, right->left); - rf = compatible_types(left->right, right->right); - if (lf && rf) { - result = new_type(qual_type, lf, rf); + default: + member = 0; + fprintf(state->errout, "type: "); + name_of(state->errout, type); + fprintf(state->errout, "\n"); + internal_error(state, 0, "reg_type not yet defined for type"); + } } - /* We only see TYPE_PRODUCT as part of function equivalence matching */ - else if (type == TYPE_PRODUCT) { - struct type *lf, *rf; - lf = compatible_types(left->left, right->left); - rf = compatible_types(left->right, right->right); - if (lf && rf) { - result = new_type(qual_type, lf, rf); - } + /* If I have a single register compound type not a bit-field + * find the real type. + */ + member = unpack_type(state, member); + ; + size = reg_size_of(state, member); + if (size > REG_SIZEOF_REG) { + internal_error(state, 0, "Cannot find type of single register"); } - else { - /* Nothing else is compatible */ +#if 1 + invalid = invalid_type(state, member); + if (invalid) { + fprintf(state->errout, "type: "); + name_of(state->errout, member); + fprintf(state->errout, "\n"); + fprintf(state->errout, "invalid: "); + name_of(state->errout, invalid); + fprintf(state->errout, "\n"); + internal_error(state, 0, "returning bad type?"); } - return result; +#endif + return member; } -static struct type *compatible_ptrs(struct type *left, struct type *right) +static struct type *next_field(struct compile_state *state, + struct type *type, struct type *prev_member) { - struct type *result; - if (((left->type & TYPE_MASK) != TYPE_POINTER) || - ((right->type & TYPE_MASK) != TYPE_POINTER)) { - return 0; + struct type *member; + if ((type->type & TYPE_MASK) != TYPE_STRUCT) { + internal_error(state, 0, "next_field only works on structures"); } - result = compatible_types(left->left, right->left); - if (result) { - unsigned int qual_type; - qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK); - result = new_type(qual_type, result, 0); + 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; } - return result; - + 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 *integral_promotion( - struct compile_state *state, struct triple *def) + +typedef void (*walk_type_fields_cb_t)(struct compile_state *state, struct type *type, + size_t ret_offset, size_t mem_offset, void *arg); + +static void walk_type_fields(struct compile_state *state, + struct type *type, size_t reg_offset, size_t mem_offset, + walk_type_fields_cb_t cb, void *arg); + +static void walk_struct_fields(struct compile_state *state, + struct type *type, size_t reg_offset, size_t mem_offset, + walk_type_fields_cb_t cb, void *arg) { - struct type *type; - type = def->type; - /* As all operations are carried out in registers - * the values are converted on load I just convert - * logical type of the operand. - */ - if (TYPE_INTEGER(type->type)) { - unsigned int int_type; - int_type = type->type & ~TYPE_MASK; - int_type |= do_integral_promotion(type->type); - if (int_type != type->type) { - def->type = new_type(int_type, 0, 0); + struct type *tptr; + ulong_t i; + if ((type->type & TYPE_MASK) != TYPE_STRUCT) { + internal_error(state, 0, "walk_struct_fields only works on structures"); + } + tptr = type->left; + for(i = 0; i < type->elements; i++) { + struct type *mtype; + mtype = tptr; + if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) { + mtype = mtype->left; } + walk_type_fields(state, mtype, + reg_offset + + field_reg_offset(state, type, mtype->field_ident), + mem_offset + + field_offset(state, type, mtype->field_ident), + cb, arg); + tptr = tptr->right; } - return def; -} +} -static void arithmetic(struct compile_state *state, struct triple *def) +static void walk_type_fields(struct compile_state *state, + struct type *type, size_t reg_offset, size_t mem_offset, + walk_type_fields_cb_t cb, void *arg) { - if (!TYPE_ARITHMETIC(def->type->type)) { - error(state, 0, "arithmetic type expexted"); + switch(type->type & TYPE_MASK) { + case TYPE_STRUCT: + walk_struct_fields(state, type, reg_offset, mem_offset, cb, arg); + break; + case TYPE_CHAR: + case TYPE_UCHAR: + case TYPE_SHORT: + case TYPE_USHORT: + case TYPE_INT: + case TYPE_UINT: + case TYPE_LONG: + case TYPE_ULONG: + cb(state, type, reg_offset, mem_offset, arg); + break; + case TYPE_VOID: + break; + default: + internal_error(state, 0, "walk_type_fields not yet implemented for type"); } } -static void ptr_arithmetic(struct compile_state *state, struct triple *def) +static void arrays_complete(struct compile_state *state, struct type *type) { - if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) { - error(state, def, "pointer or arithmetic type expected"); + if ((type->type & TYPE_MASK) == TYPE_ARRAY) { + if (type->elements == ELEMENT_COUNT_UNSPECIFIED) { + error(state, 0, "array size not specified"); + } + arrays_complete(state, type->left); } } -static int is_integral(struct triple *ins) +static unsigned int get_basic_type(struct type *type) { - return TYPE_INTEGER(ins->type->type); + unsigned int basic; + basic = type->type & TYPE_MASK; + /* Convert enums to ints */ + if (basic == TYPE_ENUM) { + basic = TYPE_INT; + } + /* Convert bitfields to standard types */ + else if (basic == TYPE_BITFIELD) { + if (type->elements <= SIZEOF_CHAR) { + basic = TYPE_CHAR; + } + else if (type->elements <= SIZEOF_SHORT) { + basic = TYPE_SHORT; + } + else if (type->elements <= SIZEOF_INT) { + basic = TYPE_INT; + } + else if (type->elements <= SIZEOF_LONG) { + basic = TYPE_LONG; + } + if (!TYPE_SIGNED(type->left->type)) { + basic += 1; + } + } + return basic; } -static void integral(struct compile_state *state, struct triple *def) +static unsigned int do_integral_promotion(unsigned int type) { - if (!is_integral(def)) { - error(state, 0, "integral type expected"); + if (TYPE_INTEGER(type) && (TYPE_RANK(type) < TYPE_RANK(TYPE_INT))) { + type = TYPE_INT; + } + return type; +} + +static unsigned int do_arithmetic_conversion( + unsigned int left, unsigned int right) +{ + if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) { + return TYPE_LDOUBLE; + } + else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) { + return TYPE_DOUBLE; + } + else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) { + return TYPE_FLOAT; + } + left = do_integral_promotion(left); + right = do_integral_promotion(right); + /* If both operands have the same size done */ + if (left == right) { + return left; + } + /* If both operands have the same signedness pick the larger */ + else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) { + return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right; + } + /* If the signed type can hold everything use it */ + else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) { + return left; + } + else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) { + return right; + } + /* Convert to the unsigned type with the same rank as the signed type */ + else if (TYPE_SIGNED(left)) { + return TYPE_MKUNSIGNED(left); + } + else { + return TYPE_MKUNSIGNED(right); + } +} + +/* see if two types are the same except for qualifiers */ +static int equiv_types(struct type *left, struct type *right) +{ + unsigned int type; + /* Error if the basic types do not match */ + if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) { + 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; + } + /* For bitfields we need to compare the sizes */ + else if (type == TYPE_BITFIELD) { + return (left->elements == right->elements) && + (TYPE_SIGNED(left->left->type) == TYPE_SIGNED(right->left->type)); + } + /* if the basic types match and it is an arithmetic type we are done */ + else if (TYPE_ARITHMETIC(type)) { + return 1; + } + /* If it is a pointer type recurse and keep testing */ + else if (type == TYPE_POINTER) { + return equiv_types(left->left, right->left); + } + else if (type == TYPE_ARRAY) { + return (left->elements == right->elements) && + equiv_types(left->left, right->left); + } + /* test for struct equality */ + else if (type == TYPE_STRUCT) { + return left->type_ident == right->type_ident; + } + /* test for union equality */ + else if (type == TYPE_UNION) { + return left->type_ident == right->type_ident; + } + /* Test for equivalent functions */ + else if (type == TYPE_FUNCTION) { + return equiv_types(left->left, right->left) && + equiv_types(left->right, right->right); + } + /* We only see TYPE_PRODUCT as part of function equivalence matching */ + /* We also see TYPE_PRODUCT as part of of tuple equivalence matchin */ + else if (type == TYPE_PRODUCT) { + return equiv_types(left->left, right->left) && + equiv_types(left->right, right->right); + } + /* We should see TYPE_OVERLAP when comparing joins */ + else if (type == TYPE_OVERLAP) { + return equiv_types(left->left, right->left) && + equiv_types(left->right, right->right); + } + /* Test for equivalence of tuples */ + else if (type == TYPE_TUPLE) { + return (left->elements == right->elements) && + equiv_types(left->left, right->left); + } + /* Test for equivalence of joins */ + else if (type == TYPE_JOIN) { + return (left->elements == right->elements) && + equiv_types(left->left, right->left); + } + else { + return 0; + } +} + +static int equiv_ptrs(struct type *left, struct type *right) +{ + if (((left->type & TYPE_MASK) != TYPE_POINTER) || + ((right->type & TYPE_MASK) != TYPE_POINTER)) { + return 0; + } + return equiv_types(left->left, right->left); +} + +static struct type *compatible_types(struct type *left, struct type *right) +{ + struct type *result; + unsigned int type, qual_type; + /* Error if the basic types do not match */ + if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) { + return 0; + } + type = left->type & TYPE_MASK; + qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK); + result = 0; + /* if the basic types match and it is an arithmetic type we are done */ + if (TYPE_ARITHMETIC(type)) { + result = new_type(qual_type, 0, 0); + } + /* If it is a pointer type recurse and keep testing */ + else if (type == TYPE_POINTER) { + result = compatible_types(left->left, right->left); + if (result) { + result = new_type(qual_type, result, 0); + } + } + /* test for struct equality */ + else if (type == TYPE_STRUCT) { + if (left->type_ident == right->type_ident) { + result = left; + } + } + /* test for union equality */ + else if (type == TYPE_UNION) { + if (left->type_ident == right->type_ident) { + result = left; + } + } + /* Test for equivalent functions */ + else if (type == TYPE_FUNCTION) { + struct type *lf, *rf; + lf = compatible_types(left->left, right->left); + rf = compatible_types(left->right, right->right); + if (lf && rf) { + result = new_type(qual_type, lf, rf); + } + } + /* We only see TYPE_PRODUCT as part of function equivalence matching */ + else if (type == TYPE_PRODUCT) { + struct type *lf, *rf; + lf = compatible_types(left->left, right->left); + rf = compatible_types(left->right, right->right); + if (lf && rf) { + result = new_type(qual_type, lf, rf); + } + } + else { + /* Nothing else is compatible */ + } + return result; +} + +/* See if left is a equivalent to right or right is a union member of left */ +static int is_subset_type(struct type *left, struct type *right) +{ + if (equiv_types(left, right)) { + return 1; + } + if ((left->type & TYPE_MASK) == TYPE_JOIN) { + struct type *member, *mnext; + mnext = left->left; + while(mnext) { + member = mnext; + mnext = 0; + if ((member->type & TYPE_MASK) == TYPE_OVERLAP) { + mnext = member->right; + member = member->left; + } + if (is_subset_type( member, right)) { + return 1; + } + } + } + return 0; +} + +static struct type *compatible_ptrs(struct type *left, struct type *right) +{ + struct type *result; + if (((left->type & TYPE_MASK) != TYPE_POINTER) || + ((right->type & TYPE_MASK) != TYPE_POINTER)) { + return 0; + } + result = compatible_types(left->left, right->left); + if (result) { + unsigned int qual_type; + qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK); + result = new_type(qual_type, result, 0); + } + return result; + +} +static struct triple *integral_promotion( + struct compile_state *state, struct triple *def) +{ + struct type *type; + type = def->type; + /* As all operations are carried out in registers + * the values are converted on load I just convert + * logical type of the operand. + */ + if (TYPE_INTEGER(type->type)) { + unsigned int int_type; + int_type = type->type & ~TYPE_MASK; + int_type |= do_integral_promotion(get_basic_type(type)); + if (int_type != type->type) { + if (def->op != OP_LOAD) { + def->type = new_type(int_type, 0, 0); + } + else { + def = triple(state, OP_CONVERT, + new_type(int_type, 0, 0), def, 0); + } + } + } + return def; +} + + +static void arithmetic(struct compile_state *state, struct triple *def) +{ + if (!TYPE_ARITHMETIC(def->type->type)) { + error(state, 0, "arithmetic type expexted"); + } +} + +static void ptr_arithmetic(struct compile_state *state, struct triple *def) +{ + if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) { + error(state, def, "pointer or arithmetic type expected"); + } +} + +static int is_integral(struct triple *ins) +{ + return TYPE_INTEGER(ins->type->type); +} + +static void integral(struct compile_state *state, struct triple *def) +{ + if (!is_integral(def)) { + error(state, 0, "integral type expected"); } } @@ -4062,8 +7146,28 @@ static void bool(struct compile_state *state, struct triple *def) static int is_signed(struct type *type) { + if ((type->type & TYPE_MASK) == TYPE_BITFIELD) { + type = type->left; + } return !!TYPE_SIGNED(type->type); } +static int is_compound_type(struct type *type) +{ + int is_compound; + switch((type->type & TYPE_MASK)) { + case TYPE_ARRAY: + case TYPE_STRUCT: + case TYPE_TUPLE: + case TYPE_UNION: + case TYPE_JOIN: + is_compound = 1; + break; + default: + is_compound = 0; + break; + } + return is_compound; +} /* 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) @@ -4075,77 +7179,53 @@ static int is_in_reg(struct compile_state *state, struct triple *def) 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 if (triple_is_part(state, def)) { + in_reg = is_in_reg(state, MISC(def, 0)); } else { - internal_error(state, 0, "unknown expr storage location"); + internal_error(state, def, "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 compile_state *state, struct triple *def) +/* Is this an auto or static variable location? Something that can + * be assigned to. Otherwise it must must be a pure value, a temporary. + */ +static int is_lvalue(struct compile_state *state, struct triple *def) { int ret; ret = 0; if (!def) { return 0; } - if ((def->op == OP_ADECL) || - (def->op == OP_SDECL) || + if ((def->op == OP_ADECL) || + (def->op == OP_SDECL) || (def->op == OP_DEREF) || - (def->op == OP_BLOBCONST)) { + (def->op == OP_BLOBCONST) || + (def->op == OP_LIST)) { ret = 1; } - else if (def->op == OP_DOT) { - 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; - } - } + else if (triple_is_part(state, def)) { + ret = is_lvalue(state, MISC(def, 0)); } return ret; } -static int is_lvalue(struct compile_state *state, struct triple *def) +static void clvalue(struct compile_state *state, struct triple *def) { - int ret; - ret = 1; if (!def) { - return 0; + internal_error(state, def, "nothing where lvalue expected?"); } - if (!is_stable(state, def)) { - return 0; + 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) { - ret = 0; - } - else if (def->op == OP_DOT) { - ret = is_lvalue(state, RHS(def, 0)); - } - return ret; -} - -static void lvalue(struct compile_state *state, struct triple *def) -{ - if (!def) { - internal_error(state, def, "nothing where lvalue expected?"); - } - if (!is_lvalue(state, def)) { - error(state, def, "lvalue expected"); + error(state, def, "modifable lvalue expected"); } } @@ -4171,7 +7251,7 @@ static struct triple *int_const( case TYPE_LONG: case TYPE_ULONG: break; default: - internal_error(state, 0, "constant for unkown type"); + internal_error(state, 0, "constant for unknown type"); } result = triple(state, OP_INTCONST, type, 0, 0); result->u.cval = value; @@ -4179,39 +7259,66 @@ static struct triple *int_const( } -static struct triple *do_mk_addr_expr(struct compile_state *state, +static struct triple *read_expr(struct compile_state *state, struct triple *def); + +static struct triple *do_mk_addr_expr(struct compile_state *state, struct triple *expr, struct type *type, ulong_t offset) { struct triple *result; - lvalue(state, expr); + struct type *ptr_type; + clvalue(state, expr); + + ptr_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, 0, 0); + result = triple(state, OP_ADDRCONST, ptr_type, 0, 0); MISC(result, 0) = expr; result->u.cval = offset; } else if (expr->op == OP_DEREF) { - result = triple(state, OP_ADD, type, + result = triple(state, OP_ADD, ptr_type, RHS(expr, 0), int_const(state, &ulong_type, offset)); } + else if (expr->op == OP_BLOBCONST) { + FINISHME(); + internal_error(state, expr, "not yet implemented"); + } + else if (expr->op == OP_LIST) { + error(state, 0, "Function addresses not supported"); + } + else if (triple_is_part(state, expr)) { + struct triple *part; + part = expr; + expr = MISC(expr, 0); + if (part->op == OP_DOT) { + offset += bits_to_bytes( + field_offset(state, expr->type, part->u.field)); + } + else if (part->op == OP_INDEX) { + offset += bits_to_bytes( + index_offset(state, expr->type, part->u.cval)); + } + else { + internal_error(state, part, "unhandled part type"); + } + result = do_mk_addr_expr(state, expr, type, offset); + } + if (!result) { + internal_error(state, expr, "cannot take address of expression"); + } return result; } static struct triple *mk_addr_expr( struct compile_state *state, struct triple *expr, ulong_t offset) { - struct type *type; - - type = new_type( - TYPE_POINTER | (expr->type->type & QUAL_MASK), - expr->type, 0); - - return do_mk_addr_expr(state, expr, type, offset); + return do_mk_addr_expr(state, expr, expr->type, offset); } static struct triple *mk_deref_expr( @@ -4220,84 +7327,126 @@ static struct triple *mk_deref_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); } +/* lvalue conversions always apply except when certain operators + * are applied. So I apply apply it when I know no more + * operators will be applied. + */ +static struct triple *lvalue_conversion(struct compile_state *state, struct triple *def) +{ + /* Tranform an array to a pointer to the first element */ + 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_OP(def->op)) { + 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_CONVERT, type, def, 0); + } + } + /* Transform a function to a pointer to it */ + else if ((def->type->type & TYPE_MASK) == TYPE_FUNCTION) { + def = mk_addr_expr(state, 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; + ulong_t offset; if (!field) { internal_error(state, 0, "No field passed to deref_field"); } result = 0; type = expr->type; - if ((type->type & TYPE_MASK) != TYPE_STRUCT) { + if (((type->type & TYPE_MASK) != TYPE_STRUCT) && + ((type->type & TYPE_MASK) != TYPE_UNION)) { error(state, 0, "request for member %s in something not a struct or union", field->name); } - 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, "%s is not a member", 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); + offset = bits_to_bytes(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, - field_type(state, type, field), expr, 0); + result = triple(state, OP_DOT, member, expr, 0); result->u.field = field; } return result; } +static struct triple *deref_index( + struct compile_state *state, struct triple *expr, size_t index) +{ + struct triple *result; + struct type *type, *member; + ulong_t offset; + + result = 0; + type = expr->type; + member = index_type(state, type, index); + + if ((type->type & STOR_MASK) == STOR_PERM) { + offset = bits_to_bytes(index_offset(state, type, index)); + result = do_mk_addr_expr(state, expr, member, offset); + result = mk_deref_expr(state, result); + } + else { + result = triple(state, OP_INDEX, member, expr, 0); + result->u.cval = index; + } + return result; +} + static struct triple *read_expr(struct compile_state *state, struct triple *def) { int op; if (!def) { return 0; } - if (!is_stable(state, def)) { +#if DEBUG_ROMCC_WARNINGS +#warning "CHECK_ME is this the only place I need to do lvalue conversions?" +#endif + /* Transform lvalues into something we can read */ + def = lvalue_conversion(state, def); + if (!is_lvalue(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; - struct triple *result; - type = new_type( - TYPE_POINTER | (def->type->type & QUAL_MASK), - def->type->left, 0); - result = triple(state, OP_ADDRCONST, type, 0, 0); - MISC(result, 0) = def; - return result; - } if (is_in_reg(state, def)) { op = OP_READ; } else { + if (def->op == OP_SDECL) { + def = mk_addr_expr(state, def, 0); + def = mk_deref_expr(state, def); + } op = OP_LOAD; } - return triple(state, op, def->type, def, 0); + def = triple(state, op, def->type, def, 0); + if (def->type->type & QUAL_VOLATILE) { + def->id |= TRIPLE_FLAG_VOLATILE; + } + return def; } -static void write_compatible(struct compile_state *state, +int is_write_compatible(struct compile_state *state, struct type *dest, struct type *rval) { int compatible = 0; @@ -4317,21 +7466,43 @@ static void write_compatible(struct compile_state *state, 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)) { + else if (equiv_types(dest, rval)) { compatible = 1; } - if (!compatible) { + return compatible; +} + +static void write_compatible(struct compile_state *state, + struct type *dest, struct type *rval) +{ + if (!is_write_compatible(state, dest, rval)) { + FILE *fp = state->errout; + fprintf(fp, "dest: "); + name_of(fp, dest); + fprintf(fp,"\nrval: "); + name_of(fp, rval); + fprintf(fp, "\n"); error(state, 0, "Incompatible types in assignment"); } } +static int is_init_compatible(struct compile_state *state, + struct type *dest, struct type *rval) +{ + int compatible = 0; + if (is_write_compatible(state, dest, rval)) { + compatible = 1; + } + else if (equiv_types(dest, rval)) { + compatible = 1; + } + return compatible; +} + static struct triple *write_expr( struct compile_state *state, struct triple *dest, struct triple *rval) { struct triple *def; - int op; def = 0; if (!rval) { @@ -4344,17 +7515,30 @@ static struct triple *write_expr( 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); + if (!equiv_types(dest->type, rval->type)) { + rval = triple(state, OP_CONVERT, dest->type, rval, 0); + } /* Now figure out which assignment operator to use */ - op = -1; if (is_in_reg(state, dest)) { - op = OP_WRITE; + def = triple(state, OP_WRITE, dest->type, rval, dest); + if (MISC(def, 0) != dest) { + internal_error(state, def, "huh?"); + } + if (RHS(def, 0) != rval) { + internal_error(state, def, "huh?"); + } } else { - op = OP_STORE; + def = triple(state, OP_STORE, dest->type, dest, rval); + } + if (def->type->type & QUAL_VOLATILE) { + def->id |= TRIPLE_FLAG_VOLATILE; } - def = triple(state, op, dest->type, dest, rval); return def; } @@ -4399,8 +7583,9 @@ struct type *arithmetic_result( arithmetic(state, right); type = new_type( do_arithmetic_conversion( - left->type->type, - right->type->type), 0, 0); + get_basic_type(left->type), + get_basic_type(right->type)), + 0, 0); return type; } @@ -4411,7 +7596,7 @@ struct type *ptr_arithmetic_result( /* Sanity checks to ensure I am working with the proper types */ ptr_arithmetic(state, left); arithmetic(state, right); - if (TYPE_ARITHMETIC(left->type->type) && + if (TYPE_ARITHMETIC(left->type->type) && TYPE_ARITHMETIC(right->type->type)) { type = arithmetic_result(state, left, right); } @@ -4425,10 +7610,9 @@ struct type *ptr_arithmetic_result( return type; } - /* boolean helper function */ -static struct triple *ltrue_expr(struct compile_state *state, +static struct triple *ltrue_expr(struct compile_state *state, struct triple *expr) { switch(expr->op) { @@ -4444,17 +7628,77 @@ static struct triple *ltrue_expr(struct compile_state *state, return expr; } -static struct triple *lfalse_expr(struct compile_state *state, +static struct triple *lfalse_expr(struct compile_state *state, struct triple *expr) { return triple(state, OP_LFALSE, &int_type, expr, 0); } -static struct triple *cond_expr( - struct compile_state *state, +static struct triple *mkland_expr( + struct compile_state *state, + struct triple *left, struct triple *right) +{ + struct triple *def, *val, *var, *jmp, *mid, *end; + struct triple *lstore, *rstore; + + /* Generate some intermediate triples */ + end = label(state); + var = variable(state, &int_type); + + /* Store the left hand side value */ + lstore = write_expr(state, var, left); + + /* Jump if the value is false */ + jmp = branch(state, end, + lfalse_expr(state, read_expr(state, var))); + mid = label(state); + + /* Store the right hand side value */ + rstore = write_expr(state, var, right); + + /* An expression for the computed value */ + val = read_expr(state, var); + + /* Generate the prog for a logical and */ + def = mkprog(state, var, lstore, jmp, mid, rstore, end, val, 0UL); + + return def; +} + +static struct triple *mklor_expr( + struct compile_state *state, + struct triple *left, struct triple *right) +{ + struct triple *def, *val, *var, *jmp, *mid, *end; + + /* Generate some intermediate triples */ + end = label(state); + var = variable(state, &int_type); + + /* Store the left hand side value */ + left = write_expr(state, var, left); + + /* Jump if the value is true */ + jmp = branch(state, end, read_expr(state, var)); + mid = label(state); + + /* Store the right hand side value */ + right = write_expr(state, var, right); + + /* An expression for the computed value*/ + val = read_expr(state, var); + + /* Generate the prog for a logical or */ + def = mkprog(state, var, left, jmp, mid, right, end, val, 0UL); + + return def; +} + +static struct triple *mkcond_expr( + struct compile_state *state, struct triple *test, struct triple *left, struct triple *right) { - struct triple *def; + struct triple *def, *val, *var, *jmp1, *jmp2, *top, *mid, *end; struct type *result_type; unsigned int left_type, right_type; bool(state, test); @@ -4489,18 +7733,39 @@ static struct triple *cond_expr( if (!result_type) { error(state, 0, "Incompatible types in conditional expression"); } - /* 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; + /* Generate some intermediate triples */ + mid = label(state); + end = label(state); + var = variable(state, result_type); + + /* Branch if the test is false */ + jmp1 = branch(state, mid, lfalse_expr(state, read_expr(state, test))); + top = label(state); + + /* Store the left hand side value */ + left = write_expr(state, var, left); + + /* Branch to the end */ + jmp2 = branch(state, end, 0); + + /* Store the right hand side value */ + right = write_expr(state, var, right); + + /* An expression for the computed value */ + val = read_expr(state, var); + + /* Generate the prog for a conditional expression */ + def = mkprog(state, var, jmp1, top, left, jmp2, mid, right, end, val, 0UL); + return def; } static int expr_depth(struct compile_state *state, struct triple *ins) { +#if DEBUG_ROMCC_WARNINGS +#warning "FIXME move optimal ordering of subexpressions into the optimizer" +#endif int count; count = 0; if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) { @@ -4512,13 +7777,7 @@ static int expr_depth(struct compile_state *state, struct triple *ins) else if (ins->op == OP_VAL) { count = expr_depth(state, RHS(ins, 0)) - 1; } - 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 (ins->op == OP_CALL) { + else if (ins->op == OP_FCALL) { /* Don't figure the depth of a call just guess it is huge */ count = 1000; } @@ -4538,21 +7797,19 @@ static int expr_depth(struct compile_state *state, struct triple *ins) return count + 1; } -static struct triple *flatten( - struct compile_state *state, struct triple *first, struct triple *ptr); - static struct triple *flatten_generic( - struct compile_state *state, struct triple *first, struct triple *ptr) + struct compile_state *state, struct triple *first, struct triple *ptr, + int ignored) { 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) { + /* Only operations with just a rhs and a lhs should come here */ + rhs = ptr->rhs; + lhs = ptr->lhs; + if (TRIPLE_SIZE(ptr) != lhs + rhs + ignored) { internal_error(state, ptr, "unexpected args for: %d %s", ptr->op, tops(ptr->op)); } @@ -4581,305 +7838,137 @@ static struct triple *flatten_generic( *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); + if (lhs) { + insert_triple(state, first, ptr); + ptr->id |= TRIPLE_FLAG_FLATTENED; + ptr->id &= ~TRIPLE_FLAG_LOCAL; + + /* 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; } -static struct triple *flatten_land( +static struct triple *flatten_prog( struct compile_state *state, struct triple *first, struct triple *ptr) { - struct triple *left, *right; - struct triple *val, *test, *jmp, *label1, *end; - - /* Find the triples */ - left = RHS(ptr, 0); - right = RHS(ptr, 1); + struct triple *head, *body, *val; + head = RHS(ptr, 0); + RHS(ptr, 0) = 0; + val = head->prev; + body = head->next; + release_triple(state, head); + release_triple(state, ptr); + val->next = first; + body->prev = first->prev; + body->prev->next = body; + val->next->prev = val; - /* Generate the needed triples */ - end = label(state); + if (triple_is_cbranch(state, body->prev) || + triple_is_call(state, body->prev)) { + unuse_triple(first, body->prev); + use_triple(body, body->prev); + } - /* 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, - lfalse_expr(state, read_expr(state, val))); - 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); -} - -static struct triple *flatten_lor( - struct compile_state *state, struct triple *first, struct triple *ptr) -{ - struct triple *left, *right; - struct triple *val, *jmp, *label1, *end; + if (!(val->id & TRIPLE_FLAG_FLATTENED)) { + internal_error(state, val, "val not flattened?"); + } - /* Find the triples */ - left = RHS(ptr, 0); - right = RHS(ptr, 1); + return val; +} - /* 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, 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 */ - return read_expr(state, val); -} - -static struct triple *flatten_cond( +static struct triple *flatten_part( struct compile_state *state, struct triple *first, struct triple *ptr) { - struct triple *test, *left, *right; - struct triple *val, *mv1, *jmp1, *label1, *mv2, *middle, *jmp2, *end; - - /* Find the triples */ - test = RHS(ptr, 0); - left = RHS(ptr, 1); - right = RHS(ptr, 2); - - /* Generate the needed triples */ - end = label(state); - middle = label(state); + if (!triple_is_part(state, ptr)) { + internal_error(state, ptr, "not a part"); + } + if (ptr->rhs || ptr->lhs || ptr->targ || (ptr->misc != 1)) { + internal_error(state, ptr, "unexpected args for: %d %s", + ptr->op, tops(ptr->op)); + } + MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0)); + use_triple(MISC(ptr, 0), ptr); + return flatten_generic(state, first, ptr, 1); +} - /* Thread the triples together */ - 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) +static struct triple *flatten( + struct compile_state *state, struct triple *first, struct triple *ptr) { - struct triple *nfunc; - struct triple *nfirst, *ofirst; - struct triple *new, *old; - -#if 0 - fprintf(stdout, "\n"); - loc(stdout, state, 0); - fprintf(stdout, "\n__________ copy_func _________\n"); - print_triple(state, ofunc); - fprintf(stdout, "__________ copy_func _________ done\n\n"); -#endif - - /* Make a new copy of the old function */ - nfunc = triple(state, OP_LIST, ofunc->type, 0, 0); - nfirst = 0; - ofirst = old = RHS(ofunc, 0); + struct triple *orig_ptr; + if (!ptr) + return 0; do { - struct triple *new; - int old_lhs, old_rhs; - old_lhs = TRIPLE_LHS(old->sizes); - old_rhs = TRIPLE_RHS(old->sizes); - new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs, - old->filename, old->line, old->col); - if (!triple_stores_block(state, new)) { - memcpy(&new->u, &old->u, sizeof(new->u)); - } - if (!nfirst) { - 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 == MISC(ofunc, 0)) { - MISC(nfunc, 0) = new; + orig_ptr = ptr; + /* Only flatten triples once */ + if (ptr->id & TRIPLE_FLAG_FLATTENED) { + return ptr; } - old = old->next; - } while(old != ofirst); - - /* Make a second pass to fix up any unresolved references */ - old = ofirst; - new = nfirst; - do { - struct triple **oexpr, **nexpr; - int count, i; - /* Lookup where the copy is, to join pointers */ - 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 (!*nexpr && *oexpr) { - internal_error(state, 0, "Could not copy %d\n", i); + switch(ptr->op) { + case OP_VAL: + RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0)); + return MISC(ptr, 0); + break; + case OP_PROG: + ptr = flatten_prog(state, first, ptr); + break; + case OP_FCALL: + ptr = flatten_generic(state, first, ptr, 1); + insert_triple(state, first, ptr); + ptr->id |= TRIPLE_FLAG_FLATTENED; + ptr->id &= ~TRIPLE_FLAG_LOCAL; + if (ptr->next != ptr) { + use_triple(ptr->next, ptr); } - } - old = old->next; - new = new->next; - } while((old != ofirst) && (new != nfirst)); - - /* Make a third pass to cleanup the extra useses */ - old = ofirst; - new = nfirst; - do { - unuse_triple(old, new); - old = old->next; - new = new->next; - } while ((old != ofirst) && (new != nfirst)); - return nfunc; -} - -static struct triple *flatten_call( - struct compile_state *state, struct triple *first, struct triple *ptr) -{ - /* Inline the function call */ - struct type *ptype; - struct triple *ofunc, *nfunc, *nfirst, *param, *result; - struct triple *end, *nend; - int pvals, i; - - /* Find the triples */ - ofunc = MISC(ptr, 0); - if (ofunc->op != OP_LIST) { - internal_error(state, 0, "improper function"); - } - nfunc = copy_func(state, ofunc); - nfirst = RHS(nfunc, 0)->next; - /* Prepend the parameter reading into the new function list */ - 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; - atype = ptype; - if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) { - atype = ptype->left; - } - 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; - } - result = 0; - if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) { - result = read_expr(state, MISC(nfunc,0)); - } -#if 0 - fprintf(stdout, "\n"); - loc(stdout, state, 0); - fprintf(stdout, "\n__________ flatten_call _________\n"); - print_triple(state, nfunc); - fprintf(stdout, "__________ flatten_call _________ done\n\n"); -#endif - - /* Get rid of the extra triples */ - 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 */ - end = first->prev; - nend = nfirst->prev; - end->next = nfirst; - nfirst->prev = end; - nend->next = first; - first->prev = nend; - - return result; -} - -static struct triple *flatten( - struct compile_state *state, struct triple *first, struct triple *ptr) -{ - struct triple *orig_ptr; - if (!ptr) - 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: - RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0)); - LHS(ptr, 0) = flatten(state, first, LHS(ptr, 0)); - use_triple(LHS(ptr, 0), ptr); - use_triple(RHS(ptr, 0), ptr); - break; - case OP_COMMA: - RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0)); - ptr = RHS(ptr, 1); break; - case OP_VAL: + case OP_READ: + case OP_LOAD: RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0)); - return MISC(ptr, 0); - break; - case OP_LAND: - ptr = flatten_land(state, first, ptr); - break; - case OP_LOR: - ptr = flatten_lor(state, first, ptr); + use_triple(RHS(ptr, 0), ptr); break; - case OP_COND: - ptr = flatten_cond(state, first, ptr); + case OP_WRITE: + ptr = flatten_generic(state, first, ptr, 1); + MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0)); + use_triple(MISC(ptr, 0), ptr); break; - case OP_CALL: - ptr = flatten_call(state, first, ptr); + case OP_BRANCH: + use_triple(TARG(ptr, 0), ptr); break; - case OP_READ: - case OP_LOAD: + case OP_CBRANCH: RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0)); use_triple(RHS(ptr, 0), ptr); + use_triple(TARG(ptr, 0), ptr); + insert_triple(state, first, ptr); + ptr->id |= TRIPLE_FLAG_FLATTENED; + ptr->id &= ~TRIPLE_FLAG_LOCAL; + if (ptr->next != ptr) { + use_triple(ptr->next, ptr); + } break; - case OP_BRANCH: + case OP_CALL: + MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0)); + use_triple(MISC(ptr, 0), 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); - } + insert_triple(state, first, ptr); + ptr->id |= TRIPLE_FLAG_FLATTENED; + ptr->id &= ~TRIPLE_FLAG_LOCAL; + if (ptr->next != ptr) { + use_triple(ptr->next, ptr); } break; + case OP_RET: + RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0)); + use_triple(RHS(ptr, 0), ptr); + break; case OP_BLOBCONST: - insert_triple(state, first, ptr); + insert_triple(state, state->global_pool, ptr); ptr->id |= TRIPLE_FLAG_FLATTENED; + ptr->id &= ~TRIPLE_FLAG_LOCAL; ptr = triple(state, OP_SDECL, ptr->type, ptr, 0); use_triple(MISC(ptr, 0), ptr); break; @@ -4890,32 +7979,66 @@ static struct triple *flatten( free_triple(state, orig_ptr); break; case OP_DOT: - { - struct triple *base; - base = RHS(ptr, 0); - base = flatten(state, first, base); - if (base->op == OP_VAL_VEC) { - ptr = struct_field(state, base, ptr->u.field); + if (RHS(ptr, 0)->op == OP_DEREF) { + struct triple *base, *left; + ulong_t offset; + base = MISC(ptr, 0); + offset = bits_to_bytes(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 { + ptr = flatten_part(state, first, ptr); + } + break; + case OP_INDEX: + if (RHS(ptr, 0)->op == OP_DEREF) { + struct triple *base, *left; + ulong_t offset; + base = MISC(ptr, 0); + offset = bits_to_bytes(index_offset(state, base->type, ptr->u.cval)); + left = RHS(base, 0); + ptr = triple(state, OP_ADD, left->type, + read_expr(state, left), + int_const(state, &long_type, offset)); + free_triple(state, base); + } + else { + ptr = flatten_part(state, first, ptr); } break; - } - case OP_ADDRCONST: - case OP_SDECL: case OP_PIECE: + ptr = flatten_part(state, first, ptr); + use_triple(ptr, MISC(ptr, 0)); + break; + case OP_ADDRCONST: MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0)); use_triple(MISC(ptr, 0), ptr); break; + case OP_SDECL: + first = state->global_pool; + MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0)); + use_triple(MISC(ptr, 0), ptr); + insert_triple(state, first, ptr); + ptr->id |= TRIPLE_FLAG_FLATTENED; + ptr->id &= ~TRIPLE_FLAG_LOCAL; + return ptr; case OP_ADECL: + ptr = flatten_generic(state, first, ptr, 0); break; default: /* Flatten the easy cases we don't override */ - ptr = flatten_generic(state, first, ptr); + ptr = flatten_generic(state, first, ptr, 0); break; } } while(ptr && (ptr != orig_ptr)); - if (ptr) { + if (ptr && !(ptr->id & TRIPLE_FLAG_FLATTENED)) { insert_triple(state, first, ptr); ptr->id |= TRIPLE_FLAG_FLATTENED; + ptr->id &= ~TRIPLE_FLAG_LOCAL; } return ptr; } @@ -4971,21 +8094,71 @@ static int replace_lhs_use(struct compile_state *state, return found; } +static int replace_misc_use(struct compile_state *state, + struct triple *orig, struct triple *new, struct triple *use) +{ + struct triple **expr; + int found; + found = 0; + expr = triple_misc(state, use, 0); + for(;expr; expr = triple_misc(state, use, expr)) { + if (*expr == orig) { + *expr = new; + found = 1; + } + } + if (found) { + unuse_triple(orig, use); + use_triple(new, use); + } + return found; +} + +static int replace_targ_use(struct compile_state *state, + struct triple *orig, struct triple *new, struct triple *use) +{ + struct triple **expr; + int found; + found = 0; + expr = triple_targ(state, use, 0); + for(;expr; expr = triple_targ(state, use, expr)) { + if (*expr == orig) { + *expr = new; + found = 1; + } + } + if (found) { + unuse_triple(orig, use); + use_triple(new, use); + } + return found; +} + +static void replace_use(struct compile_state *state, + struct triple *orig, struct triple *new, struct triple *use) +{ + int found; + found = 0; + found |= replace_rhs_use(state, orig, new, use); + found |= replace_lhs_use(state, orig, new, use); + found |= replace_misc_use(state, orig, new, use); + found |= replace_targ_use(state, orig, new, use); + if (!found) { + internal_error(state, use, "use without use"); + } +} + static void propogate_use(struct compile_state *state, struct triple *orig, struct triple *new) { struct triple_set *user, *next; for(user = orig->use; user; user = next) { - struct triple *use; - int found; + /* Careful replace_use modifies the use chain and + * removes use. So we must get a copy of the next + * entry early. + */ next = user->next; - use = user->member; - found = 0; - found |= replace_rhs_use(state, orig, new, use); - found |= replace_lhs_use(state, orig, new, use); - if (!found) { - internal_error(state, use, "use without use"); - } + replace_use(state, orig, new, user->member); } if (orig->use) { internal_error(state, orig, "used after propogate_use"); @@ -4997,6 +8170,15 @@ static void propogate_use(struct compile_state *state, * =========================== */ +static struct triple *mk_cast_expr( + struct compile_state *state, struct type *type, struct triple *expr) +{ + struct triple *def; + def = read_expr(state, expr); + def = triple(state, OP_CONVERT, type, def, 0); + return def; +} + static struct triple *mk_add_expr( struct compile_state *state, struct triple *left, struct triple *right) { @@ -5012,12 +8194,21 @@ static struct triple *mk_add_expr( 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, - &ulong_type, - right, - int_const(state, &ulong_type, - size_of(state, left->type->left))); + struct type *ptr_math; + int op; + if (is_signed(right->type)) { + ptr_math = &long_type; + op = OP_SMUL; + } else { + ptr_math = &ulong_type; + op = OP_UMUL; + } + if (!equiv_types(right->type, ptr_math)) { + right = mk_cast_expr(state, ptr_math, right); + } + right = triple(state, op, ptr_math, right, + int_const(state, ptr_math, + size_of_in_bytes(state, left->type->left))); } return triple(state, OP_ADD, result_type, left, right); } @@ -5030,12 +8221,21 @@ static struct triple *mk_sub_expr( left = read_expr(state, left); right = read_expr(state, right); if (is_pointer(left)) { - right = triple(state, - is_signed(right->type)? OP_SMUL : OP_UMUL, - &ulong_type, - right, - int_const(state, &ulong_type, - size_of(state, left->type->left))); + struct type *ptr_math; + int op; + if (is_signed(right->type)) { + ptr_math = &long_type; + op = OP_SMUL; + } else { + ptr_math = &ulong_type; + op = OP_UMUL; + } + if (!equiv_types(right->type, ptr_math)) { + right = mk_cast_expr(state, ptr_math, right); + } + right = triple(state, op, ptr_math, right, + int_const(state, ptr_math, + size_of_in_bytes(state, left->type->left))); } return triple(state, OP_SUB, result_type, left, right); } @@ -5080,7 +8280,7 @@ static struct triple *mk_post_dec_expr( struct triple *val; lvalue(state, def); val = read_expr(state, def); - return triple(state, OP_VAL, def->type, + return triple(state, OP_VAL, def->type, write_expr(state, def, mk_sub_expr(state, val, int_const(state, &int_type, 1))) , val); @@ -5097,6 +8297,7 @@ static struct triple *mk_subscript_expr( return mk_deref_expr(state, mk_add_expr(state, left, right)); } + /* * Compile time evaluation * =========================== @@ -5106,11 +8307,24 @@ static int is_const(struct triple *ins) return IS_CONST_OP(ins->op); } -static int constants_equal(struct compile_state *state, +static int is_simple_const(struct triple *ins) +{ + /* Is this a constant that u.cval has the value. + * Or equivalently is this a constant that read_const + * works on. + * So far only OP_INTCONST qualifies. + */ + return (ins->op == OP_INTCONST); +} + +static int constants_equal(struct compile_state *state, struct triple *left, struct triple *right) { int equal; - if (!is_const(left) || !is_const(right)) { + if ((left->op == OP_UNKNOWNVAL) || (right->op == OP_UNKNOWNVAL)) { + equal = 0; + } + else if (!is_const(left) || !is_const(right)) { equal = 0; } else if (left->op != right->op) { @@ -5129,13 +8343,14 @@ static int constants_equal(struct compile_state *state, break; case OP_BLOBCONST: { - size_t lsize, rsize; + size_t lsize, rsize, bytes; lsize = size_of(state, left->type); rsize = size_of(state, right->type); if (lsize != rsize) { break; } - if (memcmp(left->u.blob, right->u.blob, lsize) == 0) { + bytes = bits_to_bytes(lsize); + if (memcmp(left->u.blob, right->u.blob, bytes) == 0) { equal = 1; } break; @@ -5156,13 +8371,32 @@ static int constants_equal(struct compile_state *state, static int is_zero(struct triple *ins) { - return is_const(ins) && (ins->u.cval == 0); + return is_simple_const(ins) && (ins->u.cval == 0); } static int is_one(struct triple *ins) { - return is_const(ins) && (ins->u.cval == 1); + return is_simple_const(ins) && (ins->u.cval == 1); +} + +#if DEBUG_ROMCC_WARNING +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; + } +#endif static long_t bsr(ulong_t value) { @@ -5192,14 +8426,14 @@ static long_t bsf(ulong_t value) return -1; } -static long_t log2(ulong_t value) +static long_t ilog2(ulong_t value) { return bsr(value); } static long_t tlog2(struct triple *ins) { - return log2(ins->u.cval); + return ilog2(ins->u.cval); } static int is_pow2(struct triple *ins) @@ -5210,7 +8444,7 @@ static int is_pow2(struct triple *ins) return 0; } value = ins->u.cval; - log = log2(value); + log = ilog2(value); if (log == -1) { return 0; } @@ -5220,35 +8454,145 @@ static int is_pow2(struct triple *ins) } static ulong_t read_const(struct compile_state *state, - struct triple *ins, struct triple **expr) + struct triple *ins, struct triple *rhs) { - struct triple *rhs; - rhs = *expr; switch(rhs->type->type &TYPE_MASK) { - case TYPE_CHAR: + case TYPE_CHAR: case TYPE_SHORT: case TYPE_INT: case TYPE_LONG: - case TYPE_UCHAR: - case TYPE_USHORT: + case TYPE_UCHAR: + case TYPE_USHORT: case TYPE_UINT: case TYPE_ULONG: case TYPE_POINTER: + case TYPE_BITFIELD: break; default: - internal_error(state, rhs, "bad type to read_const\n"); + fprintf(state->errout, "type: "); + name_of(state->errout, rhs->type); + fprintf(state->errout, "\n"); + internal_warning(state, rhs, "bad type to read_const"); break; } + if (!is_simple_const(rhs)) { + internal_error(state, rhs, "bad op to read_const"); + } return rhs->u.cval; } -static long_t read_sconst(struct triple *ins, struct triple **expr) +static long_t read_sconst(struct compile_state *state, + struct triple *ins, struct triple *rhs) { - struct triple *rhs; - rhs = *expr; return (long_t)(rhs->u.cval); } +int const_ltrue(struct compile_state *state, struct triple *ins, struct triple *rhs) +{ + if (!is_const(rhs)) { + internal_error(state, 0, "non const passed to const_true"); + } + return !is_zero(rhs); +} + +int const_eq(struct compile_state *state, struct triple *ins, + struct triple *left, struct triple *right) +{ + int result; + if (!is_const(left) || !is_const(right)) { + internal_warning(state, ins, "non const passed to const_eq"); + result = -1; + } + else if (left == right) { + result = 1; + } + else if (is_simple_const(left) && is_simple_const(right)) { + ulong_t lval, rval; + lval = read_const(state, ins, left); + rval = read_const(state, ins, right); + result = (lval == rval); + } + else if ((left->op == OP_ADDRCONST) && + (right->op == OP_ADDRCONST)) { + result = (MISC(left, 0) == MISC(right, 0)) && + (left->u.cval == right->u.cval); + } + else { + internal_warning(state, ins, "incomparable constants passed to const_eq"); + result = -1; + } + return result; + +} + +int const_ucmp(struct compile_state *state, struct triple *ins, + struct triple *left, struct triple *right) +{ + int result; + if (!is_const(left) || !is_const(right)) { + internal_warning(state, ins, "non const past to const_ucmp"); + result = -2; + } + else if (left == right) { + result = 0; + } + else if (is_simple_const(left) && is_simple_const(right)) { + ulong_t lval, rval; + lval = read_const(state, ins, left); + rval = read_const(state, ins, right); + result = 0; + if (lval > rval) { + result = 1; + } else if (rval > lval) { + result = -1; + } + } + else if ((left->op == OP_ADDRCONST) && + (right->op == OP_ADDRCONST) && + (MISC(left, 0) == MISC(right, 0))) { + result = 0; + if (left->u.cval > right->u.cval) { + result = 1; + } else if (left->u.cval < right->u.cval) { + result = -1; + } + } + else { + internal_warning(state, ins, "incomparable constants passed to const_ucmp"); + result = -2; + } + return result; +} + +int const_scmp(struct compile_state *state, struct triple *ins, + struct triple *left, struct triple *right) +{ + int result; + if (!is_const(left) || !is_const(right)) { + internal_warning(state, ins, "non const past to ucmp_const"); + result = -2; + } + else if (left == right) { + result = 0; + } + else if (is_simple_const(left) && is_simple_const(right)) { + long_t lval, rval; + lval = read_sconst(state, ins, left); + rval = read_sconst(state, ins, right); + result = 0; + if (lval > rval) { + result = 1; + } else if (rval > lval) { + result = -1; + } + } + else { + internal_warning(state, ins, "incomparable constants passed to const_scmp"); + result = -2; + } + return result; +} + static void unuse_rhs(struct compile_state *state, struct triple *ins) { struct triple **expr; @@ -5271,16 +8615,51 @@ static void unuse_lhs(struct compile_state *state, struct triple *ins) } } -static void check_lhs(struct compile_state *state, struct triple *ins) +#if DEBUG_ROMCC_WARNING +static void unuse_misc(struct compile_state *state, struct triple *ins) { struct triple **expr; - expr = triple_lhs(state, ins, 0); - for(;expr;expr = triple_lhs(state, ins, expr)) { - internal_error(state, ins, "unexpected lhs"); + expr = triple_misc(state, ins, 0); + for(;expr;expr = triple_misc(state, ins, expr)) { + unuse_triple(*expr, ins); + *expr = 0; } - } -static void check_targ(struct compile_state *state, struct triple *ins) + +static void unuse_targ(struct compile_state *state, struct triple *ins) +{ + int i; + struct triple **slot; + slot = &TARG(ins, 0); + for(i = 0; i < ins->targ; i++) { + unuse_triple(slot[i], ins); + slot[i] = 0; + } +} + +static void check_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)) { + internal_error(state, ins, "unexpected lhs"); + } + +} +#endif + +static void check_misc(struct compile_state *state, struct triple *ins) +{ + struct triple **expr; + expr = triple_misc(state, ins, 0); + for(;expr;expr = triple_misc(state, ins, expr)) { + if (*expr) { + internal_error(state, ins, "unexpected misc"); + } + } +} + +static void check_targ(struct compile_state *state, struct triple *ins) { struct triple **expr; expr = triple_targ(state, ins, 0); @@ -5296,156 +8675,604 @@ static void wipe_ins(struct compile_state *state, struct triple *ins) * in all instructions to hold all others. */ check_targ(state, ins); + check_misc(state, ins); + unuse_rhs(state, ins); + unuse_lhs(state, ins); + ins->lhs = 0; + ins->rhs = 0; + ins->misc = 0; + ins->targ = 0; +} + +#if DEBUG_ROMCC_WARNING +static void wipe_branch(struct compile_state *state, struct triple *ins) +{ + /* Becareful which instructions you replace the wiped + * instruction with, as there are not enough slots + * in all instructions to hold all others. + */ unuse_rhs(state, ins); unuse_lhs(state, ins); + unuse_misc(state, ins); + unuse_targ(state, ins); + ins->lhs = 0; + ins->rhs = 0; + ins->misc = 0; + ins->targ = 0; } +#endif -static void mkcopy(struct compile_state *state, +static void mkcopy(struct compile_state *state, struct triple *ins, struct triple *rhs) { + struct block *block; + if (!equiv_types(ins->type, rhs->type)) { + FILE *fp = state->errout; + fprintf(fp, "src type: "); + name_of(fp, rhs->type); + fprintf(fp, "\ndst type: "); + name_of(fp, ins->type); + fprintf(fp, "\n"); + internal_error(state, ins, "mkcopy type mismatch"); + } + block = block_of_triple(state, ins); wipe_ins(state, ins); ins->op = OP_COPY; - ins->sizes = TRIPLE_SIZES(0, 1, 0, 0); + ins->rhs = 1; + ins->u.block = block; RHS(ins, 0) = rhs; use_triple(RHS(ins, 0), ins); } -static void mkconst(struct compile_state *state, +static void mkconst(struct compile_state *state, struct triple *ins, ulong_t value) { if (!is_integral(ins) && !is_pointer(ins)) { - internal_error(state, ins, "unknown type to make constant\n"); + fprintf(state->errout, "type: "); + name_of(state->errout, ins->type); + fprintf(state->errout, "\n"); + internal_error(state, ins, "unknown type to make constant value: %ld", + value); } 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) && (sdecl->op != OP_LABEL)) { + internal_error(state, ins, "bad base for addrconst"); + } wipe_ins(state, ins); ins->op = OP_ADDRCONST; - ins->sizes = TRIPLE_SIZES(0, 0, 1, 0); + ins->misc = 1; 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) +#if DEBUG_DECOMPOSE_PRINT_TUPLES +static void print_tuple(struct compile_state *state, + struct triple *ins, struct triple *tuple) { - struct triple *ins, *first; - first = RHS(state->main_function, 0); + FILE *fp = state->dbgout; + fprintf(fp, "%5s %p tuple: %p ", tops(ins->op), ins, tuple); + name_of(fp, tuple->type); + if (tuple->lhs > 0) { + fprintf(fp, " lhs: "); + name_of(fp, LHS(tuple, 0)->type); + } + fprintf(fp, "\n"); + +} +#endif + +static struct triple *decompose_with_tuple(struct compile_state *state, + struct triple *ins, struct triple *tuple) +{ + struct triple *next; + next = ins->next; + flatten(state, next, tuple); +#if DEBUG_DECOMPOSE_PRINT_TUPLES + print_tuple(state, ins, tuple); +#endif + + if (!is_compound_type(tuple->type) && (tuple->lhs > 0)) { + struct triple *tmp; + if (tuple->lhs != 1) { + internal_error(state, tuple, "plain type in multiple registers?"); + } + tmp = LHS(tuple, 0); + release_triple(state, tuple); + tuple = tmp; + } + + propogate_use(state, ins, tuple); + release_triple(state, ins); + + return next; +} + +static struct triple *decompose_unknownval(struct compile_state *state, + struct triple *ins) +{ + struct triple *tuple; + ulong_t i; + +#if DEBUG_DECOMPOSE_HIRES + FILE *fp = state->dbgout; + fprintf(fp, "unknown type: "); + name_of(fp, ins->type); + fprintf(fp, "\n"); +#endif + + get_occurance(ins->occurance); + tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1, + ins->occurance); + + for(i = 0; i < tuple->lhs; i++) { + struct type *piece_type; + struct triple *unknown; + + piece_type = reg_type(state, ins->type, i * REG_SIZEOF_REG); + get_occurance(tuple->occurance); + unknown = alloc_triple(state, OP_UNKNOWNVAL, piece_type, 0, 0, + tuple->occurance); + LHS(tuple, i) = unknown; + } + return decompose_with_tuple(state, ins, tuple); +} + + +static struct triple *decompose_read(struct compile_state *state, + struct triple *ins) +{ + struct triple *tuple, *lval; + ulong_t i; + + lval = RHS(ins, 0); + + if (lval->op == OP_PIECE) { + return ins->next; + } + get_occurance(ins->occurance); + tuple = alloc_triple(state, OP_TUPLE, lval->type, -1, -1, + ins->occurance); + + if ((tuple->lhs != lval->lhs) && + (!triple_is_def(state, lval) || (tuple->lhs != 1))) + { + internal_error(state, ins, "lhs size inconsistency?"); + } + for(i = 0; i < tuple->lhs; i++) { + struct triple *piece, *read, *bitref; + if ((i != 0) || !triple_is_def(state, lval)) { + piece = LHS(lval, i); + } else { + piece = lval; + } + + /* See if the piece is really a bitref */ + bitref = 0; + if (piece->op == OP_BITREF) { + bitref = piece; + piece = RHS(bitref, 0); + } + + get_occurance(tuple->occurance); + read = alloc_triple(state, OP_READ, piece->type, -1, -1, + tuple->occurance); + RHS(read, 0) = piece; + + if (bitref) { + struct triple *extract; + int op; + if (is_signed(bitref->type->left)) { + op = OP_SEXTRACT; + } else { + op = OP_UEXTRACT; + } + get_occurance(tuple->occurance); + extract = alloc_triple(state, op, bitref->type, -1, -1, + tuple->occurance); + RHS(extract, 0) = read; + extract->u.bitfield.size = bitref->u.bitfield.size; + extract->u.bitfield.offset = bitref->u.bitfield.offset; + + read = extract; + } + + LHS(tuple, i) = read; + } + return decompose_with_tuple(state, ins, tuple); +} + +static struct triple *decompose_write(struct compile_state *state, + struct triple *ins) +{ + struct triple *tuple, *lval, *val; + ulong_t i; + + lval = MISC(ins, 0); + val = RHS(ins, 0); + get_occurance(ins->occurance); + tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1, + ins->occurance); + + if ((tuple->lhs != lval->lhs) && + (!triple_is_def(state, lval) || tuple->lhs != 1)) + { + internal_error(state, ins, "lhs size inconsistency?"); + } + for(i = 0; i < tuple->lhs; i++) { + struct triple *piece, *write, *pval, *bitref; + if ((i != 0) || !triple_is_def(state, lval)) { + piece = LHS(lval, i); + } else { + piece = lval; + } + if ((i == 0) && (tuple->lhs == 1) && (val->lhs == 0)) { + pval = val; + } + else { + if (i > val->lhs) { + internal_error(state, ins, "lhs size inconsistency?"); + } + pval = LHS(val, i); + } + + /* See if the piece is really a bitref */ + bitref = 0; + if (piece->op == OP_BITREF) { + struct triple *read, *deposit; + bitref = piece; + piece = RHS(bitref, 0); + + /* Read the destination register */ + get_occurance(tuple->occurance); + read = alloc_triple(state, OP_READ, piece->type, -1, -1, + tuple->occurance); + RHS(read, 0) = piece; + + /* Deposit the new bitfield value */ + get_occurance(tuple->occurance); + deposit = alloc_triple(state, OP_DEPOSIT, piece->type, -1, -1, + tuple->occurance); + RHS(deposit, 0) = read; + RHS(deposit, 1) = pval; + deposit->u.bitfield.size = bitref->u.bitfield.size; + deposit->u.bitfield.offset = bitref->u.bitfield.offset; + + /* Now write the newly generated value */ + pval = deposit; + } + + get_occurance(tuple->occurance); + write = alloc_triple(state, OP_WRITE, piece->type, -1, -1, + tuple->occurance); + MISC(write, 0) = piece; + RHS(write, 0) = pval; + LHS(tuple, i) = write; + } + return decompose_with_tuple(state, ins, tuple); +} + +struct decompose_load_info { + struct occurance *occurance; + struct triple *lval; + struct triple *tuple; +}; +static void decompose_load_cb(struct compile_state *state, + struct type *type, size_t reg_offset, size_t mem_offset, void *arg) +{ + struct decompose_load_info *info = arg; + struct triple *load; + + if (reg_offset > info->tuple->lhs) { + internal_error(state, info->tuple, "lhs to small?"); + } + get_occurance(info->occurance); + load = alloc_triple(state, OP_LOAD, type, -1, -1, info->occurance); + RHS(load, 0) = mk_addr_expr(state, info->lval, mem_offset); + LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = load; +} + +static struct triple *decompose_load(struct compile_state *state, + struct triple *ins) +{ + struct triple *tuple; + struct decompose_load_info info; + + if (!is_compound_type(ins->type)) { + return ins->next; + } + get_occurance(ins->occurance); + tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1, + ins->occurance); + + info.occurance = ins->occurance; + info.lval = RHS(ins, 0); + info.tuple = tuple; + walk_type_fields(state, ins->type, 0, 0, decompose_load_cb, &info); + + return decompose_with_tuple(state, ins, tuple); +} + + +struct decompose_store_info { + struct occurance *occurance; + struct triple *lval; + struct triple *val; + struct triple *tuple; +}; +static void decompose_store_cb(struct compile_state *state, + struct type *type, size_t reg_offset, size_t mem_offset, void *arg) +{ + struct decompose_store_info *info = arg; + struct triple *store; + + if (reg_offset > info->tuple->lhs) { + internal_error(state, info->tuple, "lhs to small?"); + } + get_occurance(info->occurance); + store = alloc_triple(state, OP_STORE, type, -1, -1, info->occurance); + RHS(store, 0) = mk_addr_expr(state, info->lval, mem_offset); + RHS(store, 1) = LHS(info->val, reg_offset); + LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = store; +} + +static struct triple *decompose_store(struct compile_state *state, + struct triple *ins) +{ + struct triple *tuple; + struct decompose_store_info info; + + if (!is_compound_type(ins->type)) { + return ins->next; + } + get_occurance(ins->occurance); + tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1, + ins->occurance); + + info.occurance = ins->occurance; + info.lval = RHS(ins, 0); + info.val = RHS(ins, 1); + info.tuple = tuple; + walk_type_fields(state, ins->type, 0, 0, decompose_store_cb, &info); + + return decompose_with_tuple(state, ins, tuple); +} + +static struct triple *decompose_dot(struct compile_state *state, + struct triple *ins) +{ + struct triple *tuple, *lval; + struct type *type; + size_t reg_offset; + int i, idx; + + lval = MISC(ins, 0); + reg_offset = field_reg_offset(state, lval->type, ins->u.field); + idx = reg_offset/REG_SIZEOF_REG; + type = field_type(state, lval->type, ins->u.field); +#if DEBUG_DECOMPOSE_HIRES + { + FILE *fp = state->dbgout; + fprintf(fp, "field type: "); + name_of(fp, type); + fprintf(fp, "\n"); + } +#endif + + get_occurance(ins->occurance); + tuple = alloc_triple(state, OP_TUPLE, type, -1, -1, + ins->occurance); + + if (((ins->type->type & TYPE_MASK) == TYPE_BITFIELD) && + (tuple->lhs != 1)) + { + internal_error(state, ins, "multi register bitfield?"); + } + + for(i = 0; i < tuple->lhs; i++, idx++) { + struct triple *piece; + if (!triple_is_def(state, lval)) { + if (idx > lval->lhs) { + internal_error(state, ins, "inconsistent lhs count"); + } + piece = LHS(lval, idx); + } else { + if (idx != 0) { + internal_error(state, ins, "bad reg_offset into def"); + } + if (i != 0) { + internal_error(state, ins, "bad reg count from def"); + } + piece = lval; + } + + /* Remember the offset of the bitfield */ + if ((type->type & TYPE_MASK) == TYPE_BITFIELD) { + get_occurance(ins->occurance); + piece = build_triple(state, OP_BITREF, type, piece, 0, + ins->occurance); + piece->u.bitfield.size = size_of(state, type); + piece->u.bitfield.offset = reg_offset % REG_SIZEOF_REG; + } + else if ((reg_offset % REG_SIZEOF_REG) != 0) { + internal_error(state, ins, + "request for a nonbitfield sub register?"); + } + + LHS(tuple, i) = piece; + } + + return decompose_with_tuple(state, ins, tuple); +} + +static struct triple *decompose_index(struct compile_state *state, + struct triple *ins) +{ + struct triple *tuple, *lval; + struct type *type; + int i, idx; + + lval = MISC(ins, 0); + idx = index_reg_offset(state, lval->type, ins->u.cval)/REG_SIZEOF_REG; + type = index_type(state, lval->type, ins->u.cval); +#if DEBUG_DECOMPOSE_HIRES +{ + FILE *fp = state->dbgout; + fprintf(fp, "index type: "); + name_of(fp, type); + fprintf(fp, "\n"); +} +#endif + + get_occurance(ins->occurance); + tuple = alloc_triple(state, OP_TUPLE, type, -1, -1, + ins->occurance); + + for(i = 0; i < tuple->lhs; i++, idx++) { + struct triple *piece; + if (!triple_is_def(state, lval)) { + if (idx > lval->lhs) { + internal_error(state, ins, "inconsistent lhs count"); + } + piece = LHS(lval, idx); + } else { + if (idx != 0) { + internal_error(state, ins, "bad reg_offset into def"); + } + if (i != 0) { + internal_error(state, ins, "bad reg count from def"); + } + piece = lval; + } + LHS(tuple, i) = piece; + } + + return decompose_with_tuple(state, ins, tuple); +} + +static void decompose_compound_types(struct compile_state *state) +{ + struct triple *ins, *next, *first; +#if DEBUG_DECOMPOSE_HIRES + FILE *fp; + fp = state->dbgout; +#endif + first = state->first; ins = first; - /* Pass one expand structure values into valvecs. + + /* Pass one expand compound values into pseudo registers. + */ + next = first; + do { + ins = next; + next = ins->next; + switch(ins->op) { + case OP_UNKNOWNVAL: + next = decompose_unknownval(state, ins); + break; + + case OP_READ: + next = decompose_read(state, ins); + break; + + case OP_WRITE: + next = decompose_write(state, ins); + break; + + + /* Be very careful with the load/store logic. These + * operations must convert from the in register layout + * to the in memory layout, which is nontrivial. + */ + case OP_LOAD: + next = decompose_load(state, ins); + break; + case OP_STORE: + next = decompose_store(state, ins); + break; + + case OP_DOT: + next = decompose_dot(state, ins); + break; + case OP_INDEX: + next = decompose_index(state, ins); + break; + + } +#if DEBUG_DECOMPOSE_HIRES + fprintf(fp, "decompose next: %p \n", next); + fflush(fp); + fprintf(fp, "next->op: %d %s\n", + next->op, tops(next->op)); + /* High resolution debugging mode */ + print_triples(state); +#endif + } while (next != first); + + /* Pass two remove the tuples. */ 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); - next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1, - ins->filename, ins->line, ins->col); - - 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); - vector[i]->filename = next->filename; - vector[i]->line = next->line; - vector[i]->col = next->col; - 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, 0); - dst = LHS(ins, 0); - next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1, - ins->filename, ins->line, ins->col); - - 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); - vector[i]->filename = next->filename; - vector[i]->line = next->line; - vector[i]->col = next->col; - tptr = tptr->right; - } - propogate_use(state, ins, next); - flatten(state, ins, next); - free_triple(state, ins); + if (ins->op == OP_TUPLE) { + if (ins->use) { + internal_error(state, ins, "tuple used"); + } + else { + release_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); - } + if (ins->op == OP_BITREF) { + if (ins->use) { + internal_error(state, ins, "bitref used"); + } + else { + release_triple(state, ins); + } + } ins = next; } while(ins != first); + /* Pass three verify the state and set ->id to 0. */ - ins = first; + next = first; do { + ins = next; + next = ins->next; ins->id &= ~TRIPLE_FLAG_FLATTENED; - if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) { - internal_error(state, 0, "STRUCT_TYPE remains?"); + if (triple_stores_block(state, ins)) { + ins->u.block = 0; + } + if (triple_is_def(state, ins)) { + if (reg_size_of(state, ins->type) > REG_SIZEOF_REG) { + internal_error(state, ins, "multi register value remains?"); + } } if (ins->op == OP_DOT) { - internal_error(state, 0, "OP_DOT remains?"); + internal_error(state, ins, "OP_DOT remains?"); } - if (ins->op == OP_VAL_VEC) { - internal_error(state, 0, "OP_VAL_VEC remains?"); + if (ins->op == OP_INDEX) { + internal_error(state, ins, "OP_INDEX remains?"); } - ins = ins->next; - } while(ins != first); + if (ins->op == OP_BITREF) { + internal_error(state, ins, "OP_BITREF remains?"); + } + if (ins->op == OP_TUPLE) { + internal_error(state, ins, "OP_TUPLE remains?"); + } + } while(next != first); } /* For those operations that cannot be simplified */ @@ -5464,8 +9291,8 @@ static void simplify_smul(struct compile_state *state, struct triple *ins) } if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { long_t left, right; - left = read_sconst(ins, &RHS(ins, 0)); - right = read_sconst(ins, &RHS(ins, 1)); + left = read_sconst(state, ins, RHS(ins, 0)); + right = read_sconst(state, ins, RHS(ins, 1)); mkconst(state, ins, left * right); } else if (is_zero(RHS(ins, 1))) { @@ -5478,7 +9305,7 @@ static void simplify_smul(struct compile_state *state, struct triple *ins) struct triple *val; val = int_const(state, ins->type, tlog2(RHS(ins, 1))); ins->op = OP_SL; - insert_triple(state, ins, val); + insert_triple(state, state->global_pool, val); unuse_triple(RHS(ins, 1), ins); use_triple(val, ins); RHS(ins, 1) = val; @@ -5493,10 +9320,10 @@ static void simplify_umul(struct compile_state *state, struct triple *ins) RHS(ins, 0) = RHS(ins, 1); RHS(ins, 1) = tmp; } - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { + if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) { ulong_t left, right; - left = read_const(state, ins, &RHS(ins, 0)); - right = read_const(state, ins, &RHS(ins, 1)); + 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(RHS(ins, 1))) { @@ -5509,7 +9336,7 @@ static void simplify_umul(struct compile_state *state, struct triple *ins) struct triple *val; val = int_const(state, ins->type, tlog2(RHS(ins, 1))); ins->op = OP_SL; - insert_triple(state, ins, val); + insert_triple(state, state->global_pool, val); unuse_triple(RHS(ins, 1), ins); use_triple(val, ins); RHS(ins, 1) = val; @@ -5520,8 +9347,8 @@ static void simplify_sdiv(struct compile_state *state, struct triple *ins) { if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { long_t left, right; - left = read_sconst(ins, &RHS(ins, 0)); - right = read_sconst(ins, &RHS(ins, 1)); + left = read_sconst(state, ins, RHS(ins, 0)); + right = read_sconst(state, ins, RHS(ins, 1)); mkconst(state, ins, left / right); } else if (is_zero(RHS(ins, 0))) { @@ -5537,7 +9364,7 @@ static void simplify_sdiv(struct compile_state *state, struct triple *ins) struct triple *val; val = int_const(state, ins->type, tlog2(RHS(ins, 1))); ins->op = OP_SSR; - insert_triple(state, ins, val); + insert_triple(state, state->global_pool, val); unuse_triple(RHS(ins, 1), ins); use_triple(val, ins); RHS(ins, 1) = val; @@ -5546,10 +9373,10 @@ static void simplify_sdiv(struct compile_state *state, struct triple *ins) static void simplify_udiv(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { + if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) { ulong_t left, right; - left = read_const(state, ins, &RHS(ins, 0)); - right = read_const(state, ins, &RHS(ins, 1)); + 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(RHS(ins, 0))) { @@ -5565,7 +9392,7 @@ static void simplify_udiv(struct compile_state *state, struct triple *ins) struct triple *val; val = int_const(state, ins->type, tlog2(RHS(ins, 1))); ins->op = OP_USR; - insert_triple(state, ins, val); + insert_triple(state, state->global_pool, val); unuse_triple(RHS(ins, 1), ins); use_triple(val, ins); RHS(ins, 1) = val; @@ -5574,10 +9401,10 @@ static void simplify_udiv(struct compile_state *state, struct triple *ins) static void simplify_smod(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { + if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) { long_t left, right; - left = read_const(state, ins, &RHS(ins, 0)); - right = read_const(state, ins, &RHS(ins, 1)); + 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(RHS(ins, 0))) { @@ -5593,18 +9420,19 @@ static void simplify_smod(struct compile_state *state, struct triple *ins) struct triple *val; val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1); ins->op = OP_AND; - insert_triple(state, ins, val); + insert_triple(state, state->global_pool, val); unuse_triple(RHS(ins, 1), ins); use_triple(val, ins); RHS(ins, 1) = val; } } + static void simplify_umod(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { + if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) { ulong_t left, right; - left = read_const(state, ins, &RHS(ins, 0)); - right = read_const(state, ins, &RHS(ins, 1)); + 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(RHS(ins, 0))) { @@ -5620,7 +9448,7 @@ static void simplify_umod(struct compile_state *state, struct triple *ins) struct triple *val; val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1); ins->op = OP_AND; - insert_triple(state, ins, val); + insert_triple(state, state->global_pool, val); unuse_triple(RHS(ins, 1), ins); use_triple(val, ins); RHS(ins, 1) = val; @@ -5636,14 +9464,14 @@ static void simplify_add(struct compile_state *state, struct triple *ins) RHS(ins, 0) = RHS(ins, 1); RHS(ins, 1) = tmp; } - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { - if (!is_pointer(RHS(ins, 0))) { + if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) { + if (RHS(ins, 0)->op == OP_INTCONST) { ulong_t left, right; - left = read_const(state, ins, &RHS(ins, 0)); - right = read_const(state, ins, &RHS(ins, 1)); + left = read_const(state, ins, RHS(ins, 0)); + right = read_const(state, ins, RHS(ins, 1)); mkconst(state, ins, left + right); } - else /* op == OP_ADDRCONST */ { + else if (RHS(ins, 0)->op == OP_ADDRCONST) { struct triple *sdecl; ulong_t left, right; sdecl = MISC(RHS(ins, 0), 0); @@ -5651,6 +9479,9 @@ static void simplify_add(struct compile_state *state, struct triple *ins) right = RHS(ins, 1)->u.cval; mkaddr_const(state, ins, sdecl, left + right); } + else { + internal_warning(state, ins, "Optimize me!"); + } } else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) { struct triple *tmp; @@ -5662,14 +9493,14 @@ static void simplify_add(struct compile_state *state, struct triple *ins) static void simplify_sub(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { - if (!is_pointer(RHS(ins, 0))) { + if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) { + if (RHS(ins, 0)->op == OP_INTCONST) { ulong_t left, right; - left = read_const(state, ins, &RHS(ins, 0)); - right = read_const(state, ins, &RHS(ins, 1)); + left = read_const(state, ins, RHS(ins, 0)); + right = read_const(state, ins, RHS(ins, 1)); mkconst(state, ins, left - right); } - else /* op == OP_ADDRCONST */ { + else if (RHS(ins, 0)->op == OP_ADDRCONST) { struct triple *sdecl; ulong_t left, right; sdecl = MISC(RHS(ins, 0), 0); @@ -5677,86 +9508,108 @@ static void simplify_sub(struct compile_state *state, struct triple *ins) 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(RHS(ins, 1))) { + if (is_simple_const(RHS(ins, 1))) { ulong_t right; - right = read_const(state, ins, &RHS(ins, 1)); - if (right >= (size_of(state, ins->type)*8)) { + right = read_const(state, ins, RHS(ins, 1)); + if (right >= (size_of(state, ins->type))) { warning(state, ins, "left shift count >= width of type"); } } - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { + if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) { ulong_t left, right; - left = read_const(state, ins, &RHS(ins, 0)); - right = read_const(state, ins, &RHS(ins, 1)); + 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(RHS(ins, 1))) { + if (is_simple_const(RHS(ins, 1))) { ulong_t right; - right = read_const(state, ins, &RHS(ins, 1)); - if (right >= (size_of(state, ins->type)*8)) { + right = read_const(state, ins, RHS(ins, 1)); + if (right >= (size_of(state, ins->type))) { warning(state, ins, "right shift count >= width of type"); } } - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { + if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) { ulong_t left, right; - left = read_const(state, ins, &RHS(ins, 0)); - right = read_const(state, ins, &RHS(ins, 1)); + 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(RHS(ins, 1))) { + if (is_simple_const(RHS(ins, 1))) { ulong_t right; - right = read_const(state, ins, &RHS(ins, 1)); - if (right >= (size_of(state, ins->type)*8)) { + right = read_const(state, ins, RHS(ins, 1)); + if (right >= (size_of(state, ins->type))) { warning(state, ins, "right shift count >= width of type"); } } - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { + if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) { long_t left, right; - left = read_sconst(ins, &RHS(ins, 0)); - right = read_sconst(ins, &RHS(ins, 1)); + left = read_sconst(state, ins, RHS(ins, 0)); + right = read_sconst(state, ins, RHS(ins, 1)); mkconst(state, ins, left >> right); } } static void simplify_and(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { - ulong_t left, right; - left = read_const(state, ins, &RHS(ins, 0)); - right = read_const(state, ins, &RHS(ins, 1)); - mkconst(state, ins, left & right); + struct triple *left, *right; + left = RHS(ins, 0); + right = RHS(ins, 1); + + if (is_simple_const(left) && is_simple_const(right)) { + ulong_t lval, rval; + lval = read_const(state, ins, left); + rval = read_const(state, ins, right); + mkconst(state, ins, lval & rval); + } + else if (is_zero(right) || is_zero(left)) { + mkconst(state, ins, 0); } } static void simplify_or(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { - ulong_t left, right; - left = read_const(state, ins, &RHS(ins, 0)); - right = read_const(state, ins, &RHS(ins, 1)); - mkconst(state, ins, left | right); + struct triple *left, *right; + left = RHS(ins, 0); + right = RHS(ins, 1); + + if (is_simple_const(left) && is_simple_const(right)) { + ulong_t lval, rval; + lval = read_const(state, ins, left); + rval = read_const(state, ins, right); + mkconst(state, ins, lval | rval); + } +#if 0 /* I need to handle type mismatches here... */ + else if (is_zero(right)) { + mkcopy(state, ins, left); } + else if (is_zero(left)) { + mkcopy(state, ins, right); + } +#endif } static void simplify_xor(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { + if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) { ulong_t left, right; - left = read_const(state, ins, &RHS(ins, 0)); - right = read_const(state, ins, &RHS(ins, 1)); + left = read_const(state, ins, RHS(ins, 0)); + right = read_const(state, ins, RHS(ins, 1)); mkconst(state, ins, left ^ right); } } @@ -5773,9 +9626,9 @@ static void simplify_pos(struct compile_state *state, struct triple *ins) static void simplify_neg(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0))) { + if (is_simple_const(RHS(ins, 0))) { ulong_t left; - left = read_const(state, ins, &RHS(ins, 0)); + left = read_const(state, ins, RHS(ins, 0)); mkconst(state, ins, -left); } else if (RHS(ins, 0)->op == OP_NEG) { @@ -5785,93 +9638,123 @@ static void simplify_neg(struct compile_state *state, struct triple *ins) static void simplify_invert(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0))) { + if (is_simple_const(RHS(ins, 0))) { ulong_t left; - left = read_const(state, ins, &RHS(ins, 0)); + 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(RHS(ins, 0)) && is_const(RHS(ins, 1))) { - ulong_t left, right; - left = read_const(state, ins, &RHS(ins, 0)); - right = read_const(state, ins, &RHS(ins, 1)); - mkconst(state, ins, left == right); + struct triple *left, *right; + left = RHS(ins, 0); + right = RHS(ins, 1); + + if (is_const(left) && is_const(right)) { + int val; + val = const_eq(state, ins, left, right); + if (val >= 0) { + mkconst(state, ins, val == 1); + } } - else if (RHS(ins, 0) == RHS(ins, 1)) { + else if (left == right) { mkconst(state, ins, 1); } } static void simplify_noteq(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { - ulong_t left, right; - left = read_const(state, ins, &RHS(ins, 0)); - right = read_const(state, ins, &RHS(ins, 1)); - mkconst(state, ins, left != right); + struct triple *left, *right; + left = RHS(ins, 0); + right = RHS(ins, 1); + + if (is_const(left) && is_const(right)) { + int val; + val = const_eq(state, ins, left, right); + if (val >= 0) { + mkconst(state, ins, val != 1); + } } - else if (RHS(ins, 0) == RHS(ins, 1)) { + if (left == right) { mkconst(state, ins, 0); } } static void simplify_sless(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { - long_t left, right; - left = read_sconst(ins, &RHS(ins, 0)); - right = read_sconst(ins, &RHS(ins, 1)); - mkconst(state, ins, left < right); + struct triple *left, *right; + left = RHS(ins, 0); + right = RHS(ins, 1); + + if (is_const(left) && is_const(right)) { + int val; + val = const_scmp(state, ins, left, right); + if ((val >= -1) && (val <= 1)) { + mkconst(state, ins, val < 0); + } } - else if (RHS(ins, 0) == RHS(ins, 1)) { + else if (left == right) { mkconst(state, ins, 0); } } static void simplify_uless(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { - ulong_t left, right; - left = read_const(state, ins, &RHS(ins, 0)); - right = read_const(state, ins, &RHS(ins, 1)); - mkconst(state, ins, left < right); + struct triple *left, *right; + left = RHS(ins, 0); + right = RHS(ins, 1); + + if (is_const(left) && is_const(right)) { + int val; + val = const_ucmp(state, ins, left, right); + if ((val >= -1) && (val <= 1)) { + mkconst(state, ins, val < 0); + } } - else if (is_zero(RHS(ins, 0))) { - mkconst(state, ins, 1); + else if (is_zero(right)) { + mkconst(state, ins, 0); } - else if (RHS(ins, 0) == RHS(ins, 1)) { + else if (left == right) { mkconst(state, ins, 0); } } static void simplify_smore(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { - long_t left, right; - left = read_sconst(ins, &RHS(ins, 0)); - right = read_sconst(ins, &RHS(ins, 1)); - mkconst(state, ins, left > right); + struct triple *left, *right; + left = RHS(ins, 0); + right = RHS(ins, 1); + + if (is_const(left) && is_const(right)) { + int val; + val = const_scmp(state, ins, left, right); + if ((val >= -1) && (val <= 1)) { + mkconst(state, ins, val > 0); + } } - else if (RHS(ins, 0) == RHS(ins, 1)) { + else if (left == right) { mkconst(state, ins, 0); } } static void simplify_umore(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { - ulong_t left, right; - left = read_const(state, ins, &RHS(ins, 0)); - right = read_const(state, ins, &RHS(ins, 1)); - mkconst(state, ins, left > right); + struct triple *left, *right; + left = RHS(ins, 0); + right = RHS(ins, 1); + + if (is_const(left) && is_const(right)) { + int val; + val = const_ucmp(state, ins, left, right); + if ((val >= -1) && (val <= 1)) { + mkconst(state, ins, val > 0); + } } - else if (is_zero(RHS(ins, 1))) { - mkconst(state, ins, 1); + else if (is_zero(left)) { + mkconst(state, ins, 0); } - else if (RHS(ins, 0) == RHS(ins, 1)) { + else if (left == right) { mkconst(state, ins, 0); } } @@ -5879,132 +9762,284 @@ 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(RHS(ins, 0)) && is_const(RHS(ins, 1))) { - long_t left, right; - left = read_sconst(ins, &RHS(ins, 0)); - right = read_sconst(ins, &RHS(ins, 1)); - mkconst(state, ins, left <= right); + struct triple *left, *right; + left = RHS(ins, 0); + right = RHS(ins, 1); + + if (is_const(left) && is_const(right)) { + int val; + val = const_scmp(state, ins, left, right); + if ((val >= -1) && (val <= 1)) { + mkconst(state, ins, val <= 0); + } } - else if (RHS(ins, 0) == RHS(ins, 1)) { + else if (left == right) { mkconst(state, ins, 1); } } static void simplify_ulesseq(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { - ulong_t left, right; - left = read_const(state, ins, &RHS(ins, 0)); - right = read_const(state, ins, &RHS(ins, 1)); - mkconst(state, ins, left <= right); + struct triple *left, *right; + left = RHS(ins, 0); + right = RHS(ins, 1); + + if (is_const(left) && is_const(right)) { + int val; + val = const_ucmp(state, ins, left, right); + if ((val >= -1) && (val <= 1)) { + mkconst(state, ins, val <= 0); + } } - else if (is_zero(RHS(ins, 0))) { + else if (is_zero(left)) { mkconst(state, ins, 1); } - else if (RHS(ins, 0) == RHS(ins, 1)) { + else if (left == right) { mkconst(state, ins, 1); } } static void simplify_smoreeq(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 0))) { - long_t left, right; - left = read_sconst(ins, &RHS(ins, 0)); - right = read_sconst(ins, &RHS(ins, 1)); - mkconst(state, ins, left >= right); + struct triple *left, *right; + left = RHS(ins, 0); + right = RHS(ins, 1); + + if (is_const(left) && is_const(right)) { + int val; + val = const_scmp(state, ins, left, right); + if ((val >= -1) && (val <= 1)) { + mkconst(state, ins, val >= 0); + } } - else if (RHS(ins, 0) == RHS(ins, 1)) { + else if (left == right) { mkconst(state, ins, 1); } } static void simplify_umoreeq(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) { - ulong_t left, right; - left = read_const(state, ins, &RHS(ins, 0)); - right = read_const(state, ins, &RHS(ins, 1)); - mkconst(state, ins, left >= right); + struct triple *left, *right; + left = RHS(ins, 0); + right = RHS(ins, 1); + + if (is_const(left) && is_const(right)) { + int val; + val = const_ucmp(state, ins, left, right); + if ((val >= -1) && (val <= 1)) { + mkconst(state, ins, val >= 0); + } } - else if (is_zero(RHS(ins, 1))) { + else if (is_zero(right)) { mkconst(state, ins, 1); } - else if (RHS(ins, 0) == RHS(ins, 1)) { + else if (left == right) { mkconst(state, ins, 1); } } static void simplify_lfalse(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0))) { - ulong_t left; - left = read_const(state, ins, &RHS(ins, 0)); - mkconst(state, ins, left == 0); + struct triple *rhs; + rhs = RHS(ins, 0); + + if (is_const(rhs)) { + mkconst(state, ins, !const_ltrue(state, ins, rhs)); } /* Otherwise if I am the only user... */ - else if ((RHS(ins, 0)->use->member == ins) && (RHS(ins, 0)->use->next == 0)) { + else if ((rhs->use) && + (rhs->use->member == ins) && (rhs->use->next == 0)) { int need_copy = 1; /* Invert a boolean operation */ - 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; + switch(rhs->op) { + case OP_LTRUE: rhs->op = OP_LFALSE; break; + case OP_LFALSE: rhs->op = OP_LTRUE; break; + case OP_EQ: rhs->op = OP_NOTEQ; break; + case OP_NOTEQ: rhs->op = OP_EQ; break; + case OP_SLESS: rhs->op = OP_SMOREEQ; break; + case OP_ULESS: rhs->op = OP_UMOREEQ; break; + case OP_SMORE: rhs->op = OP_SLESSEQ; break; + case OP_UMORE: rhs->op = OP_ULESSEQ; break; + case OP_SLESSEQ: rhs->op = OP_SMORE; break; + case OP_ULESSEQ: rhs->op = OP_UMORE; break; + case OP_SMOREEQ: rhs->op = OP_SLESS; break; + case OP_UMOREEQ: rhs->op = OP_ULESS; break; default: need_copy = 0; break; } if (need_copy) { - mkcopy(state, ins, RHS(ins, 0)); + mkcopy(state, ins, rhs); } } } static void simplify_ltrue (struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0))) { - ulong_t left; - left = read_const(state, ins, &RHS(ins, 0)); - mkconst(state, ins, left != 0); + struct triple *rhs; + rhs = RHS(ins, 0); + + if (is_const(rhs)) { + mkconst(state, ins, const_ltrue(state, ins, rhs)); } - else switch(RHS(ins, 0)->op) { + else switch(rhs->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, RHS(ins, 0)); + mkcopy(state, ins, rhs); + } + +} + +static void simplify_load(struct compile_state *state, struct triple *ins) +{ + struct triple *addr, *sdecl, *blob; + + /* If I am doing a load with a constant pointer from a constant + * table get the value. + */ + addr = RHS(ins, 0); + if ((addr->op == OP_ADDRCONST) && (sdecl = MISC(addr, 0)) && + (sdecl->op == OP_SDECL) && (blob = MISC(sdecl, 0)) && + (blob->op == OP_BLOBCONST)) { + unsigned char buffer[SIZEOF_WORD]; + size_t reg_size, mem_size; + const char *src, *end; + ulong_t val; + reg_size = reg_size_of(state, ins->type); + if (reg_size > REG_SIZEOF_REG) { + internal_error(state, ins, "load size greater than register"); + } + mem_size = size_of(state, ins->type); + end = blob->u.blob; + end += bits_to_bytes(size_of(state, sdecl->type)); + src = blob->u.blob; + src += addr->u.cval; + + if (src > end) { + error(state, ins, "Load address out of bounds"); + } + + memset(buffer, 0, sizeof(buffer)); + memcpy(buffer, src, bits_to_bytes(mem_size)); + + switch(mem_size) { + case SIZEOF_I8: val = *((uint8_t *) buffer); break; + case SIZEOF_I16: val = *((uint16_t *)buffer); break; + case SIZEOF_I32: val = *((uint32_t *)buffer); break; + case SIZEOF_I64: val = *((uint64_t *)buffer); break; + default: + internal_error(state, ins, "mem_size: %d not handled", + mem_size); + val = 0; + break; + } + mkconst(state, ins, val); + } +} + +static void simplify_uextract(struct compile_state *state, struct triple *ins) +{ + if (is_simple_const(RHS(ins, 0))) { + ulong_t val; + ulong_t mask; + val = read_const(state, ins, RHS(ins, 0)); + mask = 1; + mask <<= ins->u.bitfield.size; + mask -= 1; + val >>= ins->u.bitfield.offset; + val &= mask; + mkconst(state, ins, val); + } +} + +static void simplify_sextract(struct compile_state *state, struct triple *ins) +{ + if (is_simple_const(RHS(ins, 0))) { + ulong_t val; + ulong_t mask; + long_t sval; + val = read_const(state, ins, RHS(ins, 0)); + mask = 1; + mask <<= ins->u.bitfield.size; + mask -= 1; + val >>= ins->u.bitfield.offset; + val &= mask; + val <<= (SIZEOF_LONG - ins->u.bitfield.size); + sval = val; + sval >>= (SIZEOF_LONG - ins->u.bitfield.size); + mkconst(state, ins, sval); } +} +static void simplify_deposit(struct compile_state *state, struct triple *ins) +{ + if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) { + ulong_t targ, val; + ulong_t mask; + targ = read_const(state, ins, RHS(ins, 0)); + val = read_const(state, ins, RHS(ins, 1)); + mask = 1; + mask <<= ins->u.bitfield.size; + mask -= 1; + mask <<= ins->u.bitfield.offset; + targ &= ~mask; + val <<= ins->u.bitfield.offset; + val &= mask; + targ |= val; + mkconst(state, ins, targ); + } } static void simplify_copy(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0))) { - switch(RHS(ins, 0)->op) { + struct triple *right; + right = RHS(ins, 0); + if (is_subset_type(ins->type, right->type)) { + ins->type = right->type; + } + if (equiv_types(ins->type, right->type)) { + ins->op = OP_COPY;/* I don't need to convert if the types match */ + } else { + if (ins->op == OP_COPY) { + internal_error(state, ins, "type mismatch on copy"); + } + } + if (is_const(right) && (right->op == OP_ADDRCONST) && is_pointer(ins)) { + struct triple *sdecl; + ulong_t offset; + sdecl = MISC(right, 0); + offset = right->u.cval; + mkaddr_const(state, ins, sdecl, offset); + } + else if (is_const(right) && is_write_compatible(state, ins->type, right->type)) { + switch(right->op) { case OP_INTCONST: { ulong_t left; - left = read_const(state, ins, &RHS(ins, 0)); + left = read_const(state, ins, right); + /* Ensure I have not overflowed the destination. */ + if (size_of(state, right->type) > size_of(state, ins->type)) { + ulong_t mask; + mask = 1; + mask <<= size_of(state, ins->type); + mask -= 1; + left &= mask; + } + /* Ensure I am properly sign extended */ + if (size_of(state, right->type) < size_of(state, ins->type) && + is_signed(right->type)) { + long_t val; + int shift; + shift = SIZEOF_LONG - size_of(state, right->type); + val = left; + val <<= shift; + val >>= shift; + left = val; + } mkconst(state, ins, left); break; } - case OP_ADDRCONST: - { - struct triple *sdecl; - ulong_t offset; - sdecl = MISC(RHS(ins, 0), 0); - offset = RHS(ins, 0)->u.cval; - mkaddr_const(state, ins, sdecl, offset); - break; - } default: internal_error(state, ins, "uknown constant"); break; @@ -6012,29 +10047,103 @@ static void simplify_copy(struct compile_state *state, struct triple *ins) } } +static 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 int phi_dependency(struct block *block) +{ + /* A block has a phi dependency if a phi function + * depends on that block to exist, and makes a block + * that is otherwise useless unsafe to remove. + */ + if (block) { + struct block_set *edge; + for(edge = block->edges; edge; edge = edge->next) { + if (phi_present(edge->member)) { + return 1; + } + } + } + return 0; +} + +static struct triple *branch_target(struct compile_state *state, struct triple *ins) +{ + struct triple *targ; + targ = TARG(ins, 0); + /* During scc_transform temporary triples are allocated that + * loop back onto themselves. If I see one don't advance the + * target. + */ + while(triple_is_structural(state, targ) && + (targ->next != targ) && (targ->next != state->first)) { + targ = targ->next; + } + return targ; +} + + static void simplify_branch(struct compile_state *state, struct triple *ins) { - struct block *block; - if (ins->op != OP_BRANCH) { + int simplified, loops; + if ((ins->op != OP_BRANCH) && (ins->op != OP_CBRANCH)) { internal_error(state, ins, "not branch"); } if (ins->use != 0) { internal_error(state, ins, "branch use"); } -#warning "FIXME implement simplify branch." - /* The challenge here with simplify branch is that I need to + /* The challenge here with simplify branch is that I need to * make modifications to the control flow graph as well - * as to the branch instruction itself. + * as to the branch instruction itself. That is handled + * by rebuilding the basic blocks after simplify all is called. + */ + + /* If we have a branch to an unconditional branch update + * our target. But watch out for dependencies from phi + * functions. + * Also only do this a limited number of times so + * we don't get into an infinite loop. + */ + loops = 0; + do { + struct triple *targ; + simplified = 0; + targ = branch_target(state, ins); + if ((targ != ins) && (targ->op == OP_BRANCH) && + !phi_dependency(targ->u.block)) + { + unuse_triple(TARG(ins, 0), ins); + TARG(ins, 0) = TARG(targ, 0); + use_triple(TARG(ins, 0), ins); + simplified = 1; + } + } while(simplified && (++loops < 20)); + + /* If we have a conditional branch with a constant condition + * make it an unconditional branch. */ - block = ins->u.block; - - if (TRIPLE_RHS(ins->sizes) && is_const(RHS(ins, 0))) { + if ((ins->op == OP_CBRANCH) && is_simple_const(RHS(ins, 0))) { struct triple *targ; ulong_t value; - value = read_const(state, ins, &RHS(ins, 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); + ins->rhs = 0; + ins->targ = 1; + ins->op = OP_BRANCH; if (value) { unuse_triple(ins->next, ins); TARG(ins, 0) = targ; @@ -6043,206 +10152,251 @@ static void simplify_branch(struct compile_state *state, struct triple *ins) unuse_triple(targ, ins); TARG(ins, 0) = ins->next; } -#warning "FIXME handle the case of making a branch unconditional" } + + /* If we have a branch to the next instruction, + * make it a noop. + */ if (TARG(ins, 0) == ins->next) { - unuse_triple(ins->next, ins); - if (TRIPLE_RHS(ins->sizes)) { + unuse_triple(TARG(ins, 0), ins); + if (ins->op == OP_CBRANCH) { unuse_triple(RHS(ins, 0), ins); unuse_triple(ins->next, ins); } - ins->sizes = TRIPLE_SIZES(0, 0, 0, 0); + ins->lhs = 0; + ins->rhs = 0; + ins->misc = 0; + ins->targ = 0; ins->op = OP_NOOP; if (ins->use) { internal_error(state, ins, "noop use != 0"); } -#warning "FIXME handle the case of killing a branch" + } +} + +static void simplify_label(struct compile_state *state, struct triple *ins) +{ + /* Ignore volatile labels */ + if (!triple_is_pure(state, ins, ins->id)) { + return; + } + if (ins->use == 0) { + ins->op = OP_NOOP; + } + else if (ins->prev->op == OP_LABEL) { + /* 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 (!phi_dependency(ins->prev->u.block)) { + struct triple_set *user, *next; + ins->op = OP_NOOP; + for(user = ins->use; user; user = next) { + struct triple *use, **expr; + next = user->next; + use = user->member; + expr = triple_targ(state, use, 0); + for(;expr; expr = triple_targ(state, use, expr)) { + if (*expr == ins) { + *expr = 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 (!*expr || !is_const(*expr)) { + struct triple **slot; + struct triple *value; + int zrhs, i; + ulong_t cvalue; + slot = &RHS(ins, 0); + zrhs = ins->rhs; + if (zrhs == 0) { return; } - value = read_const(state, ins, expr); - for(;expr;expr = triple_rhs(state, ins, expr)) { - if (!*expr || !is_const(*expr)) { - return; + /* See if all of the rhs members of a phi have the same value */ + if (slot[0] && is_simple_const(slot[0])) { + cvalue = read_const(state, ins, slot[0]); + for(i = 1; i < zrhs; i++) { + if ( !slot[i] || + !is_simple_const(slot[i]) || + !equiv_types(slot[0]->type, slot[i]->type) || + (cvalue != read_const(state, ins, slot[i]))) { + break; + } } - if (value != read_const(state, ins, expr)) { + if (i == zrhs) { + mkconst(state, ins, cvalue); return; } } - mkconst(state, ins, value); + + /* See if all of rhs members of a phi are the same */ + value = slot[0]; + for(i = 1; i < zrhs; i++) { + if (slot[i] != value) { + break; + } + } + if (i == zrhs) { + /* If the phi has a single value just copy it */ + if (!is_subset_type(ins->type, value->type)) { + internal_error(state, ins, "bad input type to phi"); + } + /* Make the types match */ + if (!equiv_types(ins->type, value->type)) { + ins->type = value->type; + } + /* Now make the actual copy */ + mkcopy(state, ins, value); + return; + } } static void simplify_bsf(struct compile_state *state, struct triple *ins) { - if (is_const(RHS(ins, 0))) { + if (is_simple_const(RHS(ins, 0))) { ulong_t left; - left = read_const(state, ins, &RHS(ins, 0)); + 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(RHS(ins, 0))) { + if (is_simple_const(RHS(ins, 0))) { ulong_t left; - left = read_const(state, ins, &RHS(ins, 0)); + left = read_const(state, ins, RHS(ins, 0)); mkconst(state, ins, bsr(left)); } } typedef void (*simplify_t)(struct compile_state *state, struct triple *ins); -static const simplify_t table_simplify[] = { -#if 0 -#define simplify_smul simplify_noop -#define simplify_umul simplify_noop -#define simplify_sdiv simplify_noop -#define simplify_udiv simplify_noop -#define simplify_smod simplify_noop -#define simplify_umod simplify_noop -#endif -#if 0 -#define simplify_add simplify_noop -#define simplify_sub simplify_noop -#endif -#if 0 -#define simplify_sl simplify_noop -#define simplify_usr simplify_noop -#define simplify_ssr simplify_noop -#endif -#if 0 -#define simplify_and simplify_noop -#define simplify_xor simplify_noop -#define simplify_or simplify_noop -#endif -#if 0 -#define simplify_pos simplify_noop -#define simplify_neg simplify_noop -#define simplify_invert simplify_noop -#endif - -#if 0 -#define simplify_eq simplify_noop -#define simplify_noteq simplify_noop -#endif -#if 0 -#define simplify_sless simplify_noop -#define simplify_uless simplify_noop -#define simplify_smore simplify_noop -#define simplify_umore simplify_noop -#endif -#if 0 -#define simplify_slesseq simplify_noop -#define simplify_ulesseq simplify_noop -#define simplify_smoreeq simplify_noop -#define simplify_umoreeq simplify_noop -#endif -#if 0 -#define simplify_lfalse simplify_noop -#endif -#if 0 -#define simplify_ltrue simplify_noop -#endif - -#if 0 -#define simplify_copy simplify_noop -#endif - -#if 0 -#define simplify_branch simplify_noop -#endif +static const struct simplify_table { + simplify_t func; + unsigned long flag; +} table_simplify[] = { +#define simplify_sdivt simplify_noop +#define simplify_udivt simplify_noop +#define simplify_piece simplify_noop + +[OP_SDIVT ] = { simplify_sdivt, COMPILER_SIMPLIFY_ARITH }, +[OP_UDIVT ] = { simplify_udivt, COMPILER_SIMPLIFY_ARITH }, +[OP_SMUL ] = { simplify_smul, COMPILER_SIMPLIFY_ARITH }, +[OP_UMUL ] = { simplify_umul, COMPILER_SIMPLIFY_ARITH }, +[OP_SDIV ] = { simplify_sdiv, COMPILER_SIMPLIFY_ARITH }, +[OP_UDIV ] = { simplify_udiv, COMPILER_SIMPLIFY_ARITH }, +[OP_SMOD ] = { simplify_smod, COMPILER_SIMPLIFY_ARITH }, +[OP_UMOD ] = { simplify_umod, COMPILER_SIMPLIFY_ARITH }, +[OP_ADD ] = { simplify_add, COMPILER_SIMPLIFY_ARITH }, +[OP_SUB ] = { simplify_sub, COMPILER_SIMPLIFY_ARITH }, +[OP_SL ] = { simplify_sl, COMPILER_SIMPLIFY_SHIFT }, +[OP_USR ] = { simplify_usr, COMPILER_SIMPLIFY_SHIFT }, +[OP_SSR ] = { simplify_ssr, COMPILER_SIMPLIFY_SHIFT }, +[OP_AND ] = { simplify_and, COMPILER_SIMPLIFY_BITWISE }, +[OP_XOR ] = { simplify_xor, COMPILER_SIMPLIFY_BITWISE }, +[OP_OR ] = { simplify_or, COMPILER_SIMPLIFY_BITWISE }, +[OP_POS ] = { simplify_pos, COMPILER_SIMPLIFY_ARITH }, +[OP_NEG ] = { simplify_neg, COMPILER_SIMPLIFY_ARITH }, +[OP_INVERT ] = { simplify_invert, COMPILER_SIMPLIFY_BITWISE }, + +[OP_EQ ] = { simplify_eq, COMPILER_SIMPLIFY_LOGICAL }, +[OP_NOTEQ ] = { simplify_noteq, COMPILER_SIMPLIFY_LOGICAL }, +[OP_SLESS ] = { simplify_sless, COMPILER_SIMPLIFY_LOGICAL }, +[OP_ULESS ] = { simplify_uless, COMPILER_SIMPLIFY_LOGICAL }, +[OP_SMORE ] = { simplify_smore, COMPILER_SIMPLIFY_LOGICAL }, +[OP_UMORE ] = { simplify_umore, COMPILER_SIMPLIFY_LOGICAL }, +[OP_SLESSEQ ] = { simplify_slesseq, COMPILER_SIMPLIFY_LOGICAL }, +[OP_ULESSEQ ] = { simplify_ulesseq, COMPILER_SIMPLIFY_LOGICAL }, +[OP_SMOREEQ ] = { simplify_smoreeq, COMPILER_SIMPLIFY_LOGICAL }, +[OP_UMOREEQ ] = { simplify_umoreeq, COMPILER_SIMPLIFY_LOGICAL }, +[OP_LFALSE ] = { simplify_lfalse, COMPILER_SIMPLIFY_LOGICAL }, +[OP_LTRUE ] = { simplify_ltrue, COMPILER_SIMPLIFY_LOGICAL }, + +[OP_LOAD ] = { simplify_load, COMPILER_SIMPLIFY_OP }, +[OP_STORE ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, + +[OP_UEXTRACT ] = { simplify_uextract, COMPILER_SIMPLIFY_BITFIELD }, +[OP_SEXTRACT ] = { simplify_sextract, COMPILER_SIMPLIFY_BITFIELD }, +[OP_DEPOSIT ] = { simplify_deposit, COMPILER_SIMPLIFY_BITFIELD }, + +[OP_NOOP ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, + +[OP_INTCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +[OP_BLOBCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +[OP_ADDRCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +[OP_UNKNOWNVAL ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, + +[OP_WRITE ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +[OP_READ ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +[OP_COPY ] = { simplify_copy, COMPILER_SIMPLIFY_COPY }, +[OP_CONVERT ] = { simplify_copy, COMPILER_SIMPLIFY_COPY }, +[OP_PIECE ] = { simplify_piece, COMPILER_SIMPLIFY_OP }, +[OP_ASM ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, + +[OP_DOT ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +[OP_INDEX ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, + +[OP_LIST ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +[OP_BRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH }, +[OP_CBRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH }, +[OP_CALL ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH }, +[OP_RET ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH }, +[OP_LABEL ] = { simplify_label, COMPILER_SIMPLIFY_LABEL }, +[OP_ADECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +[OP_SDECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +[OP_PHI ] = { simplify_phi, COMPILER_SIMPLIFY_PHI }, + +[OP_INB ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +[OP_INW ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +[OP_INL ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +[OP_OUTB ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +[OP_OUTW ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +[OP_OUTL ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +[OP_BSF ] = { simplify_bsf, COMPILER_SIMPLIFY_OP }, +[OP_BSR ] = { simplify_bsr, COMPILER_SIMPLIFY_OP }, +[OP_RDMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +[OP_WRMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +[OP_HLT ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, +}; -#if 0 -#define simplify_phi simplify_noop +static inline void debug_simplify(struct compile_state *state, + simplify_t do_simplify, struct triple *ins) +{ +#if DEBUG_SIMPLIFY_HIRES + if (state->functions_joined && (do_simplify != simplify_noop)) { + /* High resolution debugging mode */ + fprintf(state->dbgout, "simplifing: "); + display_triple(state->dbgout, ins); + } #endif - -#if 0 -#define simplify_bsf simplify_noop -#define simplify_bsr simplify_noop + do_simplify(state, ins); +#if DEBUG_SIMPLIFY_HIRES + if (state->functions_joined && (do_simplify != simplify_noop)) { + /* High resolution debugging mode */ + fprintf(state->dbgout, "simplified: "); + display_triple(state->dbgout, ins); + } #endif - -[OP_SMUL ] = simplify_smul, -[OP_UMUL ] = simplify_umul, -[OP_SDIV ] = simplify_sdiv, -[OP_UDIV ] = simplify_udiv, -[OP_SMOD ] = simplify_smod, -[OP_UMOD ] = simplify_umod, -[OP_ADD ] = simplify_add, -[OP_SUB ] = simplify_sub, -[OP_SL ] = simplify_sl, -[OP_USR ] = simplify_usr, -[OP_SSR ] = simplify_ssr, -[OP_AND ] = simplify_and, -[OP_XOR ] = simplify_xor, -[OP_OR ] = simplify_or, -[OP_POS ] = simplify_pos, -[OP_NEG ] = simplify_neg, -[OP_INVERT ] = simplify_invert, - -[OP_EQ ] = simplify_eq, -[OP_NOTEQ ] = simplify_noteq, -[OP_SLESS ] = simplify_sless, -[OP_ULESS ] = simplify_uless, -[OP_SMORE ] = simplify_smore, -[OP_UMORE ] = simplify_umore, -[OP_SLESSEQ ] = simplify_slesseq, -[OP_ULESSEQ ] = simplify_ulesseq, -[OP_SMOREEQ ] = simplify_smoreeq, -[OP_UMOREEQ ] = simplify_umoreeq, -[OP_LFALSE ] = simplify_lfalse, -[OP_LTRUE ] = simplify_ltrue, - -[OP_LOAD ] = simplify_noop, -[OP_STORE ] = simplify_noop, - -[OP_NOOP ] = simplify_noop, - -[OP_INTCONST ] = simplify_noop, -[OP_BLOBCONST ] = simplify_noop, -[OP_ADDRCONST ] = simplify_noop, - -[OP_WRITE ] = simplify_noop, -[OP_READ ] = simplify_noop, -[OP_COPY ] = simplify_copy, -[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_ADECL ] = simplify_noop, -[OP_SDECL ] = simplify_noop, -[OP_PHI ] = simplify_phi, - -[OP_INB ] = simplify_noop, -[OP_INW ] = simplify_noop, -[OP_INL ] = simplify_noop, -[OP_OUTB ] = simplify_noop, -[OP_OUTW ] = simplify_noop, -[OP_OUTL ] = simplify_noop, -[OP_BSF ] = simplify_bsf, -[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) { int op; simplify_t do_simplify; + if (ins == &unknown_triple) { + internal_error(state, ins, "simplifying the unknown triple?"); + } do { op = ins->op; do_simplify = 0; @@ -6250,26 +10404,47 @@ static void simplify(struct compile_state *state, struct triple *ins) do_simplify = 0; } else { - do_simplify = table_simplify[op]; + do_simplify = table_simplify[op].func; + } + if (do_simplify && + !(state->compiler->flags & table_simplify[op].flag)) { + do_simplify = simplify_noop; } + if (do_simplify && (ins->id & TRIPLE_FLAG_VOLATILE)) { + do_simplify = simplify_noop; + } + if (!do_simplify) { - internal_error(state, ins, "cannot simplify op: %d %s\n", + internal_error(state, ins, "cannot simplify op: %d %s", op, tops(op)); return; } - do_simplify(state, ins); + debug_simplify(state, do_simplify, ins); } while(ins->op != op); } +static void rebuild_ssa_form(struct compile_state *state); + static void simplify_all(struct compile_state *state) { struct triple *ins, *first; - first = RHS(state->main_function, 0); + if (!(state->compiler->flags & COMPILER_SIMPLIFY)) { + return; + } + first = state->first; + ins = first->prev; + do { + simplify(state, ins); + ins = ins->prev; + } while(ins != first->prev); ins = first; do { simplify(state, ins); ins = ins->next; - } while(ins != first); + }while(ins != first); + rebuild_ssa_form(state); + + print_blocks(state, __func__, state->dbgout); } /* @@ -6280,8 +10455,8 @@ static void simplify_all(struct compile_state *state) static void register_builtin_function(struct compile_state *state, const char *name, int op, struct type *rtype, ...) { - struct type *ftype, *atype, *param, **next; - struct triple *def, *arg, *result, *work, *last, *first; + struct type *ftype, *atype, *ctype, *crtype, *param, **next; + struct triple *def, *result, *work, *first, *retvar, *ret; struct hash_entry *ident; struct file_state file; int parameters; @@ -6291,10 +10466,13 @@ static void register_builtin_function(struct compile_state *state, /* Dummy file state to get debug handling right */ memset(&file, 0, sizeof(file)); - file.basename = name; + file.basename = ""; file.line = 1; + file.report_line = 1; + file.report_name = file.basename; file.prev = state->file; state->file = &file; + state->function = name; /* Find the Parameter count */ valid_op(state, op); @@ -6304,7 +10482,8 @@ static void register_builtin_function(struct compile_state *state, } /* Find the function type */ - ftype = new_type(TYPE_FUNCTION, rtype, 0); + ftype = new_type(TYPE_FUNCTION | STOR_INLINE | STOR_STATIC, rtype, 0); + ftype->elements = parameters; next = &ftype->right; va_start(args, rtype); for(i = 0; i < parameters; i++) { @@ -6321,10 +10500,21 @@ static void register_builtin_function(struct compile_state *state, } va_end(args); + /* Get the initial closure type */ + ctype = new_type(TYPE_JOIN, &void_type, 0); + ctype->elements = 1; + + /* Get the return type */ + crtype = new_type(TYPE_TUPLE, new_type(TYPE_PRODUCT, ctype, rtype), 0); + crtype->elements = 2; + /* Generate the needed triples */ def = triple(state, OP_LIST, ftype, 0, 0); first = label(state); RHS(def, 0) = first; + result = flatten(state, first, variable(state, crtype)); + retvar = flatten(state, first, variable(state, &void_ptr_type)); + ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0); /* Now string them together */ param = ftype->right; @@ -6334,62 +10524,42 @@ static void register_builtin_function(struct compile_state *state, } else { atype = param; } - arg = flatten(state, first, variable(state, atype)); + flatten(state, first, variable(state, atype)); param = param->right; } - result = 0; - if ((rtype->type & TYPE_MASK) != TYPE_VOID) { - 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; + generate_lhs_pieces(state, work); + for(i = 0; i < parameters; i++) { + RHS(work, i) = read_expr(state, farg(state, def, i)); } - if (result) { - work = write_expr(state, result, work); + if ((rtype->type & TYPE_MASK) != TYPE_VOID) { + work = write_expr(state, deref_index(state, result, 1), work); } work = flatten(state, first, work); - last = flatten(state, first, label(state)); + flatten(state, first, label(state)); + ret = flatten(state, first, ret); name_len = strlen(name); ident = lookup(state, name, name_len); + ftype->type_ident = ident; 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_function _________\n"); - print_triple(state, def); - fprintf(stdout, "__________ builtin_function _________ done\n\n"); -#endif + state->function = 0; + state->main_function = 0; + + if (!state->functions) { + state->functions = def; + } else { + insert_triple(state, state->functions, def); + } + if (state->compiler->debug & DEBUG_INLINE) { + FILE *fp = state->dbgout; + fprintf(fp, "\n"); + loc(fp, state, 0); + fprintf(fp, "\n__________ %s _________\n", __FUNCTION__); + display_func(state, fp, def); + fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__); + } } static struct type *partial_struct(struct compile_state *state, @@ -6419,7 +10589,7 @@ static struct type *register_builtin_type(struct compile_state *state, name_len = strlen(name); ident = lookup(state, name, name_len); - + if ((type->type & TYPE_MASK) == TYPE_PRODUCT) { ulong_t elements = 0; struct type *field; @@ -6430,7 +10600,7 @@ static struct type *register_builtin_type(struct compile_state *state, field = field->right; } elements++; - symbol(state, ident, &ident->sym_struct, 0, type); + symbol(state, ident, &ident->sym_tag, 0, type); type->type_ident = ident; type->elements = elements; } @@ -6442,25 +10612,49 @@ static struct type *register_builtin_type(struct compile_state *state, static void register_builtins(struct compile_state *state) { + struct type *div_type, *ldiv_type; + struct type *udiv_type, *uldiv_type; struct type *msr_type; - register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_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, + register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type, &ushort_type); - register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type, + register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type, &uchar_type, &ushort_type); - register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type, + register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type, &ushort_type, &ushort_type); - register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type, + register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type, &uint_type, &ushort_type); - - register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type, + + register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type, &int_type); - register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type, + register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type, &int_type); msr_type = register_builtin_type(state, "__builtin_msr_t", @@ -6471,23 +10665,25 @@ static void register_builtins(struct compile_state *state) &ulong_type); register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type, &ulong_type, &ulong_type, &ulong_type); - - register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type, + + register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type, &void_type); } static struct type *declarator( - struct compile_state *state, struct type *type, + struct compile_state *state, struct type *type, struct hash_entry **ident, int need_ident); static void decl(struct compile_state *state, struct triple *first); static struct type *specifier_qualifier_list(struct compile_state *state); +#if DEBUG_ROMCC_WARNING static int isdecl_specifier(int tok); +#endif static struct type *decl_specifiers(struct compile_state *state); static int istype(int tok); static struct triple *expr(struct compile_state *state); static struct triple *assignment_expr(struct compile_state *state); static struct type *type_name(struct compile_state *state); -static void statement(struct compile_state *state, struct triple *fist); +static void statement(struct compile_state *state, struct triple *first); static struct triple *call_expr( struct compile_state *state, struct triple *func) @@ -6505,10 +10701,17 @@ static struct triple *call_expr( eat(state, TOK_LPAREN); /* Find the return type without any specifiers */ type = clone_type(0, func->type->left); - def = new_triple(state, OP_CALL, func->type, -1, -1); - def->type = type; - - pvals = TRIPLE_RHS(def->sizes); + /* Count the number of rhs entries for OP_FCALL */ + param = func->type->right; + pvals = 0; + while((param->type & TYPE_MASK) == TYPE_PRODUCT) { + pvals++; + param = param->right; + } + if ((param->type & TYPE_MASK) != TYPE_VOID) { + pvals++; + } + def = new_triple(state, OP_FCALL, type, -1, pvals); MISC(def, 0) = func; param = func->type->right; @@ -6539,9 +10742,8 @@ static struct triple *character_constant(struct compile_state *state) const signed char *str, *end; int c; int str_len; - eat(state, TOK_LIT_CHAR); - tk = &state->token[0]; - str = tk->val.str + 1; + tk = eat(state, TOK_LIT_CHAR); + str = (signed char *)tk->val.str + 1; str_len = tk->str_len - 2; if (str_len <= 0) { error(state, 0, "empty character constant"); @@ -6569,13 +10771,15 @@ static struct triple *string_constant(struct compile_state *state) type->elements = 0; /* The while loop handles string concatenation */ do { - eat(state, TOK_LIT_STRING); - tk = &state->token[0]; - str = tk->val.str + 1; + tk = eat(state, TOK_LIT_STRING); + str = (signed char *)tk->val.str + 1; str_len = tk->str_len - 2; if (str_len < 0) { error(state, 0, "negative string constant length"); } + /* ignore empty string tokens */ + if ('"' == *str && 0 == str[1]) + continue; end = str + str_len; ptr = buf; buf = xmalloc(type->elements + str_len + 1, "string_constant"); @@ -6590,6 +10794,7 @@ static struct triple *string_constant(struct compile_state *state) type->elements += 1; def = triple(state, OP_BLOBCONST, type, 0, 0); def->u.blob = buf; + return def; } @@ -6603,12 +10808,11 @@ static struct triple *integer_constant(struct compile_state *state) int u, l, decimal; struct type *type; - eat(state, TOK_LIT_INT); - tk = &state->token[0]; + tk = eat(state, TOK_LIT_INT); errno = 0; decimal = (tk->val.str[0] != '0'); val = strtoul(tk->val.str, &end, 0); - if ((val == ULONG_MAX) && (errno == ERANGE)) { + if ((val > ULONG_T_MAX) || ((val == ULONG_MAX) && (errno == ERANGE))) { error(state, 0, "Integer constant to large"); } u = l = 0; @@ -6632,25 +10836,25 @@ static struct triple *integer_constant(struct compile_state *state) } else if (l) { type = &long_type; - if (!decimal && (val > LONG_MAX)) { + if (!decimal && (val > LONG_T_MAX)) { type = &ulong_type; } } else if (u) { type = &uint_type; - if (val > UINT_MAX) { + if (val > UINT_T_MAX) { type = &ulong_type; } } else { type = &int_type; - if (!decimal && (val > INT_MAX) && (val <= UINT_MAX)) { + if (!decimal && (val > INT_T_MAX) && (val <= UINT_T_MAX)) { type = &uint_type; } - else if (!decimal && (val > LONG_MAX)) { + else if (!decimal && (val > LONG_T_MAX)) { type = &ulong_type; } - else if (val > INT_MAX) { + else if (val > INT_T_MAX) { type = &long_type; } } @@ -6670,10 +10874,8 @@ static struct triple *primary_expr(struct compile_state *state) /* Here ident is either: * a varable name * a function name - * an enumeration constant. */ - eat(state, TOK_IDENT); - ident = state->token[0].ident; + ident = eat(state, TOK_IDENT)->ident; if (!ident->sym_ident) { error(state, 0, "%s undeclared", ident->name); } @@ -6681,11 +10883,25 @@ static struct triple *primary_expr(struct compile_state *state) break; } case TOK_ENUM_CONST: + { + struct hash_entry *ident; /* Here ident is an enumeration constant */ - eat(state, TOK_ENUM_CONST); - def = 0; - FINISHME(); + ident = eat(state, TOK_ENUM_CONST)->ident; + if (!ident->sym_ident) { + error(state, 0, "%s undeclared", ident->name); + } + def = ident->sym_ident->def; + break; + } + case TOK_MIDENT: + { + struct hash_entry *ident; + ident = eat(state, TOK_MIDENT)->ident; + warning(state, 0, "Replacing undefined macro: %s with 0", + ident->name); + def = int_const(state, &int_type, 0); break; + } case TOK_LPAREN: eat(state, TOK_LPAREN); def = expr(state); @@ -6736,8 +10952,7 @@ static struct triple *postfix_expr(struct compile_state *state) { struct hash_entry *field; eat(state, TOK_DOT); - eat(state, TOK_IDENT); - field = state->token[0].ident; + field = eat(state, TOK_IDENT)->ident; def = deref_field(state, def, field); break; } @@ -6745,8 +10960,7 @@ static struct triple *postfix_expr(struct compile_state *state) { struct hash_entry *field; eat(state, TOK_ARROW); - eat(state, TOK_IDENT); - field = state->token[0].ident; + field = eat(state, TOK_IDENT)->ident; def = mk_deref_expr(state, read_expr(state, def)); def = deref_field(state, def, field); break; @@ -6834,7 +11048,7 @@ static struct triple *unary_expr(struct compile_state *state) type = expr->type; release_expr(state, expr); } - def = int_const(state, &ulong_type, size_of(state, type)); + def = int_const(state, &ulong_type, size_of_in_bytes(state, type)); break; } case TOK_ALIGNOF: @@ -6855,7 +11069,25 @@ static struct triple *unary_expr(struct compile_state *state) type = expr->type; release_expr(state, expr); } - def = int_const(state, &ulong_type, align_of(state, type)); + def = int_const(state, &ulong_type, align_of_in_bytes(state, type)); + break; + } + case TOK_MDEFINED: + { + /* We only come here if we are called from the preprocessor */ + struct hash_entry *ident; + int parens; + eat(state, TOK_MDEFINED); + parens = 0; + if (pp_peek(state) == TOK_LPAREN) { + pp_eat(state, TOK_LPAREN); + parens = 1; + } + ident = pp_eat(state, TOK_MIDENT)->ident; + if (parens) { + eat(state, TOK_RPAREN); + } + def = int_const(state, &int_type, ident->sym_define != 0); break; } default: @@ -6876,8 +11108,7 @@ static struct triple *cast_expr(struct compile_state *state) eat(state, TOK_LPAREN); type = type_name(state); eat(state, TOK_RPAREN); - def = read_expr(state, cast_expr(state)); - def = triple(state, OP_COPY, type, def, 0); + def = mk_cast_expr(state, type, cast_expr(state)); } else { def = unary_expr(state); @@ -6895,7 +11126,8 @@ static struct triple *mult_expr(struct compile_state *state) struct type *result_type; int tok, op, sign; done = 0; - switch(tok = (peek(state))) { + tok = peek(state); + switch(tok) { case TOK_STAR: case TOK_DIV: case TOK_MOD: @@ -6970,8 +11202,8 @@ static struct triple *shift_expr(struct compile_state *state) right = read_expr(state, add_expr(state)); integral(state, right); right = integral_promotion(state, right); - - op = (tok == TOK_SL)? OP_SL : + + op = (tok == TOK_SL)? OP_SL : is_signed(left->type)? OP_SSR: OP_USR; def = triple(state, op, left->type, left, right); @@ -6986,7 +11218,9 @@ static struct triple *shift_expr(struct compile_state *state) static struct triple *relational_expr(struct compile_state *state) { +#if DEBUG_ROMCC_WARNINGS #warning "Extend relational exprs to work on more than arithmetic types" +#endif struct triple *def; int done; def = shift_expr(state); @@ -7029,7 +11263,9 @@ static struct triple *relational_expr(struct compile_state *state) static struct triple *equality_expr(struct compile_state *state) { +#if DEBUG_ROMCC_WARNINGS #warning "Extend equality exprs to work on more than arithmetic types" +#endif struct triple *def; int done; def = relational_expr(state); @@ -7122,7 +11358,7 @@ static struct triple *land_expr(struct compile_state *state) right = read_expr(state, or_expr(state)); bool(state, right); - def = triple(state, OP_LAND, &int_type, + def = mkland_expr(state, ltrue_expr(state, left), ltrue_expr(state, right)); } @@ -7140,8 +11376,8 @@ static struct triple *lor_expr(struct compile_state *state) eat(state, TOK_LOGOR); right = read_expr(state, land_expr(state)); bool(state, right); - - def = triple(state, OP_LOR, &int_type, + + def = mklor_expr(state, ltrue_expr(state, left), ltrue_expr(state, right)); } @@ -7161,34 +11397,146 @@ static struct triple *conditional_expr(struct compile_state *state) eat(state, TOK_COLON); right = read_expr(state, conditional_expr(state)); - def = cond_expr(state, test, left, right); + def = mkcond_expr(state, test, left, right); } return def; } +struct cv_triple { + struct triple *val; + int id; +}; + +static void set_cv(struct compile_state *state, struct cv_triple *cv, + struct triple *dest, struct triple *val) +{ + if (cv[dest->id].val) { + free_triple(state, cv[dest->id].val); + } + cv[dest->id].val = val; +} +static struct triple *get_cv(struct compile_state *state, struct cv_triple *cv, + struct triple *src) +{ + return cv[src->id].val; +} + 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"); + if (is_const(expr)) { + def = expr; } - /* Free the intermediate expressions */ - while(head->next != head) { - release_triple(state, head->next); + else { + /* If we don't start out as a constant simplify into one */ + struct triple *head, *ptr; + struct cv_triple *cv; + int i, count; + head = label(state); /* dummy initial triple */ + flatten(state, head, expr); + count = 1; + for(ptr = head->next; ptr != head; ptr = ptr->next) { + count++; + } + cv = xcmalloc(sizeof(struct cv_triple)*count, "const value vector"); + i = 1; + for(ptr = head->next; ptr != head; ptr = ptr->next) { + cv[i].val = 0; + cv[i].id = ptr->id; + ptr->id = i; + i++; + } + ptr = head->next; + do { + valid_ins(state, ptr); + if ((ptr->op == OP_PHI) || (ptr->op == OP_LIST)) { + internal_error(state, ptr, + "unexpected %s in constant expression", + tops(ptr->op)); + } + else if (ptr->op == OP_LIST) { + } + else if (triple_is_structural(state, ptr)) { + ptr = ptr->next; + } + else if (triple_is_ubranch(state, ptr)) { + ptr = TARG(ptr, 0); + } + else if (triple_is_cbranch(state, ptr)) { + struct triple *cond_val; + cond_val = get_cv(state, cv, RHS(ptr, 0)); + if (!cond_val || !is_const(cond_val) || + (cond_val->op != OP_INTCONST)) + { + internal_error(state, ptr, "bad branch condition"); + } + if (cond_val->u.cval == 0) { + ptr = ptr->next; + } else { + ptr = TARG(ptr, 0); + } + } + else if (triple_is_branch(state, ptr)) { + error(state, ptr, "bad branch type in constant expression"); + } + else if (ptr->op == OP_WRITE) { + struct triple *val; + val = get_cv(state, cv, RHS(ptr, 0)); + + set_cv(state, cv, MISC(ptr, 0), + copy_triple(state, val)); + set_cv(state, cv, ptr, + copy_triple(state, val)); + ptr = ptr->next; + } + else if (ptr->op == OP_READ) { + set_cv(state, cv, ptr, + copy_triple(state, + get_cv(state, cv, RHS(ptr, 0)))); + ptr = ptr->next; + } + else if (triple_is_pure(state, ptr, cv[ptr->id].id)) { + struct triple *val, **rhs; + val = copy_triple(state, ptr); + rhs = triple_rhs(state, val, 0); + for(; rhs; rhs = triple_rhs(state, val, rhs)) { + if (!*rhs) { + internal_error(state, ptr, "Missing rhs"); + } + *rhs = get_cv(state, cv, *rhs); + } + simplify(state, val); + set_cv(state, cv, ptr, val); + ptr = ptr->next; + } + else { + error(state, ptr, "impure operation in constant expression"); + } + + } while(ptr != head); + + /* Get the result value */ + def = get_cv(state, cv, head->prev); + cv[head->prev->id].val = 0; + + /* Free the temporary values */ + for(i = 0; i < count; i++) { + if (cv[i].val) { + free_triple(state, cv[i].val); + cv[i].val = 0; + } + } + xfree(cv); + /* Free the intermediate expressions */ + while(head->next != head) { + release_triple(state, head->next); + } + free_triple(state, head); + } + if (!is_const(def)) { + error(state, expr, "Not a constant expression"); } - free_triple(state, head); return def; } @@ -7211,7 +11559,7 @@ static struct triple *assignment_expr(struct compile_state *state) * a larger set of statements than standard C. As long * as the subset of the grammar that is standard C behaves * correctly this should cause no problems. - * + * * For the extra token strings accepted by the grammar * none of them should produce a valid lvalue, so they * should not produce functioning programs. @@ -7224,7 +11572,7 @@ static struct triple *assignment_expr(struct compile_state *state) case TOK_EQ: lvalue(state, left); eat(state, TOK_EQ); - def = write_expr(state, left, + def = write_expr(state, left, read_expr(state, assignment_expr(state))); break; case TOK_TIMESEQ: @@ -7244,7 +11592,7 @@ static struct triple *assignment_expr(struct compile_state *state) case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break; } def = write_expr(state, left, - triple(state, op, left->type, + triple(state, op, left->type, read_expr(state, left), right)); break; case TOK_PLUSEQ: @@ -7280,7 +11628,7 @@ static struct triple *assignment_expr(struct compile_state *state) case TOK_OREQ: op = OP_OR; break; } def = write_expr(state, left, - triple(state, op, left->type, + triple(state, op, left->type, read_expr(state, left), right)); break; } @@ -7292,11 +11640,8 @@ static struct triple *expr(struct compile_state *state) struct triple *def; def = assignment_expr(state); while(peek(state) == TOK_COMMA) { - struct triple *left, *right; - left = def; eat(state, TOK_COMMA); - right = assignment_expr(state); - def = triple(state, OP_COMMA, right->type, left, right); + def = mkprog(state, def, assignment_expr(state), 0UL); } return def; } @@ -7304,7 +11649,11 @@ static struct triple *expr(struct compile_state *state) static void expr_statement(struct compile_state *state, struct triple *first) { if (peek(state) != TOK_SEMI) { - flatten(state, first, expr(state)); + /* lvalue conversions always apply except when certian operators + * are applied. I apply the lvalue conversions here + * as I know no more operators will be applied. + */ + flatten(state, first, lvalue_conversion(state, expr(state))); } eat(state, TOK_SEMI); } @@ -7356,7 +11705,7 @@ static void for_statement(struct compile_state *state, struct triple *first) head = test = tail = jmp1 = jmp2 = 0; if (peek(state) != TOK_SEMI) { head = expr(state); - } + } eat(state, TOK_SEMI); if (peek(state) != TOK_SEMI) { test = expr(state); @@ -7477,7 +11826,9 @@ static void return_statement(struct compile_state *state, struct triple *first) int last; eat(state, TOK_RETURN); +#if DEBUG_ROMCC_WARNINGS #warning "FIXME implement a more general excess branch elimination" +#endif val = 0; /* If we have a return value do some more work */ if (peek(state) != TOK_SEMI) { @@ -7486,13 +11837,14 @@ static void return_statement(struct compile_state *state, struct triple *first) eat(state, TOK_SEMI); /* See if this last statement in a function */ - last = ((peek(state) == TOK_RBRACE) && + last = ((peek(state) == TOK_RBRACE) && (state->scope_depth == GLOBAL_SCOPE_DEPTH +2)); /* Find the return variable */ - var = MISC(state->main_function, 0); + var = fresult(state, state->main_function); + /* Find the return destination */ - dest = RHS(state->main_function, 0)->prev; + dest = state->i_return->sym_ident->def; mv = jmp = 0; /* If needed generate a jump instruction */ if (!last) { @@ -7500,7 +11852,7 @@ static void return_statement(struct compile_state *state, struct triple *first) } /* If needed generate an assignment instruction */ if (val) { - mv = write_expr(state, var, val); + mv = write_expr(state, deref_index(state, var, 1), val); } /* Now put the code together */ if (mv) { @@ -7538,55 +11890,160 @@ static void continue_statement(struct compile_state *state, struct triple *first 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 = eat(state, TOK_IDENT)->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, FUNCTION_SCOPE_DEPTH); + } 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(); - eat(state, TOK_IDENT); + struct triple *ins; + struct hash_entry *ident; + + ident = eat(state, TOK_IDENT)->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, FUNCTION_SCOPE_DEPTH); + } + 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) { - FINISHME(); + struct triple *value, *top, *end, *dbranch; + struct hash_entry *ident; + + /* See if we have a valid switch statement */ eat(state, TOK_SWITCH); eat(state, TOK_LPAREN); - expr(state); + value = expr(state); + integral(state, value); + value = read_expr(state, value); eat(state, TOK_RPAREN); + /* Generate the needed pieces */ + top = label(state); + end = label(state); + dbranch = branch(state, end, 0); + /* Remember where case branches and break goes */ + start_scope(state); + ident = state->i_switch; + symbol(state, ident, &ident->sym_ident, value, value->type); + ident = state->i_case; + symbol(state, ident, &ident->sym_ident, top, top->type); + ident = state->i_break; + symbol(state, ident, &ident->sym_ident, end, end->type); + ident = state->i_default; + symbol(state, ident, &ident->sym_ident, dbranch, dbranch->type); + /* Thread them together */ + flatten(state, first, value); + flatten(state, first, top); + flatten(state, first, dbranch); statement(state, first); - error(state, 0, "switch statements are not implemented"); - FINISHME(); + flatten(state, first, end); + /* Cleanup the switch scope */ + end_scope(state); } static void case_statement(struct compile_state *state, struct triple *first) { - FINISHME(); + struct triple *cvalue, *dest, *test, *jmp; + struct triple *ptr, *value, *top, *dbranch; + + /* See if w have a valid case statement */ eat(state, TOK_CASE); - constant_expr(state); + cvalue = constant_expr(state); + integral(state, cvalue); + if (cvalue->op != OP_INTCONST) { + error(state, 0, "integer constant expected"); + } eat(state, TOK_COLON); + if (!state->i_case->sym_ident) { + error(state, 0, "case statement not within a switch"); + } + + /* Lookup the interesting pieces */ + top = state->i_case->sym_ident->def; + value = state->i_switch->sym_ident->def; + dbranch = state->i_default->sym_ident->def; + + /* See if this case label has already been used */ + for(ptr = top; ptr != dbranch; ptr = ptr->next) { + if (ptr->op != OP_EQ) { + continue; + } + if (RHS(ptr, 1)->u.cval == cvalue->u.cval) { + error(state, 0, "duplicate case %d statement", + cvalue->u.cval); + } + } + /* Generate the needed pieces */ + dest = label(state); + test = triple(state, OP_EQ, &int_type, value, cvalue); + jmp = branch(state, dest, test); + /* Thread the pieces together */ + flatten(state, dbranch, test); + flatten(state, dbranch, jmp); + flatten(state, dbranch, label(state)); + flatten(state, first, dest); statement(state, first); - error(state, 0, "case statements are not implemented"); - FINISHME(); } static void default_statement(struct compile_state *state, struct triple *first) { - FINISHME(); + struct triple *dest; + struct triple *dbranch, *end; + + /* See if we have a valid default statement */ eat(state, TOK_DEFAULT); eat(state, TOK_COLON); + + if (!state->i_case->sym_ident) { + error(state, 0, "default statement not within a switch"); + } + + /* Lookup the interesting pieces */ + dbranch = state->i_default->sym_ident->def; + end = state->i_break->sym_ident->def; + + /* See if a default statement has already happened */ + if (TARG(dbranch, 0) != end) { + error(state, 0, "duplicate default statement"); + } + + /* Generate the needed pieces */ + dest = label(state); + + /* Blame the branch on the default statement */ + put_occurance(dbranch->occurance); + dbranch->occurance = new_occurance(state); + + /* Thread the pieces together */ + TARG(dbranch, 0) = dest; + use_triple(dest, dbranch); + flatten(state, first, dest); statement(state, first); - error(state, 0, "default statements are not implemented"); - FINISHME(); } static void asm_statement(struct compile_state *state, struct triple *first) @@ -7598,7 +12055,9 @@ static void asm_statement(struct compile_state *state, struct triple *first) } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS]; struct triple *def, *asm_str; int out, in, clobbers, more, colons, i; + int flags; + flags = 0; eat(state, TOK_ASM); /* For now ignore the qualifiers */ switch(peek(state)) { @@ -7607,6 +12066,7 @@ static void asm_statement(struct compile_state *state, struct triple *first) break; case TOK_VOLATILE: eat(state, TOK_VOLATILE); + flags |= TRIPLE_FLAG_VOLATILE; break; } eat(state, TOK_LPAREN); @@ -7622,11 +12082,17 @@ static void asm_statement(struct compile_state *state, struct triple *first) 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); @@ -7649,11 +12115,20 @@ static void asm_statement(struct compile_state *state, struct triple *first) 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); @@ -7680,7 +12155,6 @@ static void asm_statement(struct compile_state *state, struct triple *first) 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) { @@ -7700,41 +12174,86 @@ static void asm_statement(struct compile_state *state, struct triple *first) def = new_triple(state, OP_ASM, &void_type, clobbers + out, in); def->u.ainfo = info; - for(i = 0; i < in; i++) { - struct triple *constraint; - constraint = in_param[i].constraint; - info->tmpl.rhs[i] = arch_reg_constraint(state, - in_param[i].expr->type, constraint->u.blob); + def->id |= flags; - RHS(def, i) = read_expr(state,in_param[i].expr); - free_triple(state, constraint); - } - flatten(state, first, def); + /* Find the register constraints */ for(i = 0; i < out; i++) { - struct triple *piece; struct triple *constraint; constraint = out_param[i].constraint; info->tmpl.lhs[i] = arch_reg_constraint(state, out_param[i].expr->type, constraint->u.blob); - - 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)); free_triple(state, constraint); } for(; i - out < clobbers; i++) { - struct triple *piece; 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); + } - piece = triple(state, OP_PIECE, &void_type, def, 0); + /* 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 + clobbers); i++) { + struct type *type; + struct triple *piece; + if (i < out) { + type = out_param[i].expr->type; + } else { + size_t size = arch_reg_size(info->tmpl.lhs[i].reg); + if (size >= SIZEOF_LONG) { + type = &ulong_type; + } + else if (size >= SIZEOF_INT) { + type = &uint_type; + } + else if (size >= SIZEOF_SHORT) { + type = &ushort_type; + } + else { + type = &uchar_type; + } + } + piece = triple(state, OP_PIECE, type, def, 0); piece->u.cval = i; LHS(def, i) = piece; flatten(state, first, piece); - free_triple(state, constraint); + } + /* And write the helpers to their destinations */ + for(i = 0; i < out; i++) { + struct triple *piece; + piece = LHS(def, i); + flatten(state, first, + write_expr(state, out_param[i].expr, piece)); } } @@ -7790,7 +12309,7 @@ static void statement(struct compile_state *state, struct triple *first) compound_statement(state, first); } else if (tok == TOK_IF) { - if_statement(state, first); + if_statement(state, first); } else if (tok == TOK_FOR) { for_statement(state, first); @@ -7820,7 +12339,7 @@ static void statement(struct compile_state *state, struct triple *first) asm_statement(state, first); } else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) { - labeled_statement(state, first); + labeled_statement(state, first); } else if (tok == TOK_CASE) { case_statement(state, first); @@ -7842,7 +12361,7 @@ static struct type *param_decl(struct compile_state *state) struct type *type; struct hash_entry *ident; /* Cheat so the declarator will know we are not global */ - start_scope(state); + start_scope(state); ident = 0; type = decl_specifiers(state); type = declarator(state, type, &ident, 0); @@ -7854,8 +12373,9 @@ static struct type *param_decl(struct compile_state *state) static struct type *param_type_list(struct compile_state *state, struct type *type) { struct type *ftype, **next; - ftype = new_type(TYPE_FUNCTION, type, param_decl(state)); + ftype = new_type(TYPE_FUNCTION | (type->type & STOR_MASK), type, param_decl(state)); next = &ftype->right; + ftype->elements = 1; while(peek(state) == TOK_COMMA) { eat(state, TOK_COMMA); if (peek(state) == TOK_DOTS) { @@ -7865,12 +12385,12 @@ static struct type *param_type_list(struct compile_state *state, struct type *ty else { *next = new_type(TYPE_PRODUCT, *next, param_decl(state)); next = &((*next)->right); + ftype->elements++; } } return ftype; } - static struct type *type_name(struct compile_state *state) { struct type *type; @@ -7881,25 +12401,26 @@ static struct type *type_name(struct compile_state *state) } static struct type *direct_declarator( - struct compile_state *state, struct type *type, - struct hash_entry **ident, int need_ident) + struct compile_state *state, struct type *type, + struct hash_entry **pident, int need_ident) { + struct hash_entry *ident; struct type *outer; int op; outer = 0; arrays_complete(state, type); switch(peek(state)) { case TOK_IDENT: - eat(state, TOK_IDENT); + ident = eat(state, TOK_IDENT)->ident; if (!ident) { error(state, 0, "Unexpected identifier found"); } /* The name of what we are declaring */ - *ident = state->token[0].ident; + *pident = ident; break; case TOK_LPAREN: eat(state, TOK_LPAREN); - outer = declarator(state, type, ident, need_ident); + outer = declarator(state, type, pident, need_ident); eat(state, TOK_RPAREN); break; default: @@ -7958,28 +12479,26 @@ static struct type *direct_declarator( } static struct type *declarator( - struct compile_state *state, struct type *type, - struct hash_entry **ident, int need_ident) + struct compile_state *state, struct type *type, + struct hash_entry **pident, int need_ident) { while(peek(state) == TOK_STAR) { eat(state, TOK_STAR); type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0); } - type = direct_declarator(state, type, ident, need_ident); + type = direct_declarator(state, type, pident, need_ident); return type; } - static struct type *typedef_name( struct compile_state *state, unsigned int specifiers) { struct hash_entry *ident; struct type *type; - eat(state, TOK_TYPE_NAME); - ident = state->token[0].ident; + ident = eat(state, TOK_TYPE_NAME)->ident; type = ident->sym_ident->type; specifiers |= type->type & QUAL_MASK; - if ((specifiers & (STOR_MASK | QUAL_MASK)) != + if ((specifiers & (STOR_MASK | QUAL_MASK)) != (type->type & (STOR_MASK | QUAL_MASK))) { type = clone_type(specifiers, type); } @@ -7987,59 +12506,101 @@ static struct type *typedef_name( } static struct type *enum_specifier( - struct compile_state *state, unsigned int specifiers) + struct compile_state *state, unsigned int spec) { + struct hash_entry *ident; + ulong_t base; int tok; - struct type *type; - type = 0; - FINISHME(); + struct type *enum_type; + enum_type = 0; + ident = 0; eat(state, TOK_ENUM); tok = peek(state); - if (tok == TOK_IDENT) { - eat(state, TOK_IDENT); + if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) { + ident = eat(state, tok)->ident; } - if ((tok != TOK_IDENT) || (peek(state) == TOK_LBRACE)) { + base = 0; + if (!ident || (peek(state) == TOK_LBRACE)) { + struct type **next; eat(state, TOK_LBRACE); + enum_type = new_type(TYPE_ENUM | spec, 0, 0); + enum_type->type_ident = ident; + next = &enum_type->right; do { - eat(state, TOK_IDENT); + struct hash_entry *eident; + struct triple *value; + struct type *entry; + eident = eat(state, TOK_IDENT)->ident; + if (eident->sym_ident) { + error(state, 0, "%s already declared", + eident->name); + } + eident->tok = TOK_ENUM_CONST; if (peek(state) == TOK_EQ) { + struct triple *val; eat(state, TOK_EQ); - constant_expr(state); + val = constant_expr(state); + integral(state, val); + base = val->u.cval; } + value = int_const(state, &int_type, base); + symbol(state, eident, &eident->sym_ident, value, &int_type); + entry = new_type(TYPE_LIST, 0, 0); + entry->field_ident = eident; + *next = entry; + next = &entry->right; + base += 1; if (peek(state) == TOK_COMMA) { eat(state, TOK_COMMA); } } while(peek(state) != TOK_RBRACE); eat(state, TOK_RBRACE); + if (ident) { + symbol(state, ident, &ident->sym_tag, 0, enum_type); + } } - FINISHME(); - return type; + if (ident && ident->sym_tag && + ident->sym_tag->type && + ((ident->sym_tag->type->type & TYPE_MASK) == TYPE_ENUM)) { + enum_type = clone_type(spec, ident->sym_tag->type); + } + else if (ident && !enum_type) { + error(state, 0, "enum %s undeclared", ident->name); + } + return enum_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) { + if (peek(state) != TOK_COLON) { type = declarator(state, type, ident, 1); } - if ((tok == TOK_COLON) || (peek(state) == TOK_COLON)) { + if (peek(state) == TOK_COLON) { + struct triple *value; eat(state, TOK_COLON); - constant_expr(state); + value = constant_expr(state); + if (value->op != OP_INTCONST) { + error(state, 0, "Invalid constant expression"); + } + if (value->u.cval > size_of(state, type)) { + error(state, 0, "bitfield larger than base type"); + } + if (!TYPE_INTEGER(type->type) || ((type->type & TYPE_MASK) == TYPE_BITFIELD)) { + error(state, 0, "bitfield base not an integer type"); + } + type = new_type(TYPE_BITFIELD, type, 0); + type->elements = value->u.cval; } - 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 *struct_type; struct hash_entry *ident; + unsigned int type_main; unsigned int type_join; int tok; struct_type = 0; @@ -8047,38 +12608,39 @@ static struct type *struct_or_union_specifier( switch(peek(state)) { case TOK_STRUCT: eat(state, TOK_STRUCT); + type_main = TYPE_STRUCT; type_join = TYPE_PRODUCT; break; case TOK_UNION: eat(state, TOK_UNION); + type_main = TYPE_UNION; type_join = TYPE_OVERLAP; - error(state, 0, "unions not yet supported\n"); break; default: eat(state, TOK_STRUCT); + type_main = TYPE_STRUCT; type_join = TYPE_PRODUCT; break; } tok = peek(state); - if ((tok == TOK_IDENT) || (tok == TOK_TYPE_NAME)) { - eat(state, tok); - ident = state->token[0].ident; + if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) { + ident = eat(state, tok)->ident; } 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; - struct type **next; int done; base_type = specifier_qualifier_list(state); - next = &struct_type; do { struct type *type; struct hash_entry *fident; done = 1; - type = declarator(state, base_type, &fident, 1); + type = struct_declarator(state, base_type, &fident); elements++; if (peek(state) == TOK_COMMA) { done = 0; @@ -8096,16 +12658,22 @@ static struct type *struct_or_union_specifier( eat(state, TOK_SEMI); } while(peek(state) != TOK_RBRACE); eat(state, TOK_RBRACE); - struct_type = new_type(TYPE_STRUCT, struct_type, 0); + struct_type = new_type(type_main | spec, struct_type, 0); struct_type->type_ident = ident; struct_type->elements = elements; - symbol(state, ident, &ident->sym_struct, 0, struct_type); + if (ident) { + symbol(state, ident, &ident->sym_tag, 0, struct_type); + } } - if (ident && ident->sym_struct) { - struct_type = ident->sym_struct->type; + if (ident && ident->sym_tag && + ident->sym_tag->type && + ((ident->sym_tag->type->type & TYPE_MASK) == type_main)) { + struct_type = clone_type(spec, ident->sym_tag->type); } - else if (ident && !ident->sym_struct) { - error(state, 0, "struct %s undeclared", ident->name); + else if (ident && !struct_type) { + error(state, 0, "%s %s undeclared", + (type_main == TYPE_STRUCT)?"struct" : "union", + ident->name); } return struct_type; } @@ -8136,7 +12704,7 @@ static unsigned int storage_class_specifier_opt(struct compile_state *state) break; default: if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) { - specifiers = STOR_STATIC; + specifiers = STOR_LOCAL; } else { specifiers = STOR_AUTO; @@ -8158,29 +12726,94 @@ static unsigned int function_specifier_opt(struct compile_state *state) return specifiers; } -static unsigned int type_qualifiers(struct compile_state *state) +static unsigned int attrib(struct compile_state *state, unsigned int attributes) { - unsigned int specifiers; - int done; - done = 0; - specifiers = QUAL_NONE; - do { - switch(peek(state)) { - case TOK_CONST: - eat(state, TOK_CONST); - specifiers = QUAL_CONST; - break; - case TOK_VOLATILE: - eat(state, TOK_VOLATILE); - specifiers = QUAL_VOLATILE; - break; - case TOK_RESTRICT: - eat(state, TOK_RESTRICT); - specifiers = QUAL_RESTRICT; - break; - default: - done = 1; - break; + int tok = peek(state); + switch(tok) { + case TOK_COMMA: + case TOK_LPAREN: + /* The empty attribute ignore it */ + break; + case TOK_IDENT: + case TOK_ENUM_CONST: + case TOK_TYPE_NAME: + { + struct hash_entry *ident; + ident = eat(state, TOK_IDENT)->ident; + + if (ident == state->i_noinline) { + if (attributes & ATTRIB_ALWAYS_INLINE) { + error(state, 0, "both always_inline and noinline attribtes"); + } + attributes |= ATTRIB_NOINLINE; + } + else if (ident == state->i_always_inline) { + if (attributes & ATTRIB_NOINLINE) { + error(state, 0, "both noinline and always_inline attribtes"); + } + attributes |= ATTRIB_ALWAYS_INLINE; + } + else if (ident == state->i_noreturn) { + // attribute((noreturn)) does nothing (yet?) + } + else { + error(state, 0, "Unknown attribute:%s", ident->name); + } + break; + } + default: + error(state, 0, "Unexpected token: %s\n", tokens[tok]); + break; + } + return attributes; +} + +static unsigned int attribute_list(struct compile_state *state, unsigned type) +{ + type = attrib(state, type); + while(peek(state) == TOK_COMMA) { + eat(state, TOK_COMMA); + type = attrib(state, type); + } + return type; +} + +static unsigned int attributes_opt(struct compile_state *state, unsigned type) +{ + if (peek(state) == TOK_ATTRIBUTE) { + eat(state, TOK_ATTRIBUTE); + eat(state, TOK_LPAREN); + eat(state, TOK_LPAREN); + type = attribute_list(state, type); + eat(state, TOK_RPAREN); + eat(state, TOK_RPAREN); + } + return type; +} + +static unsigned int type_qualifiers(struct compile_state *state) +{ + unsigned int specifiers; + int done; + done = 0; + specifiers = QUAL_NONE; + do { + switch(peek(state)) { + case TOK_CONST: + eat(state, TOK_CONST); + specifiers |= QUAL_CONST; + break; + case TOK_VOLATILE: + eat(state, TOK_VOLATILE); + specifiers |= QUAL_VOLATILE; + break; + case TOK_RESTRICT: + eat(state, TOK_RESTRICT); + specifiers |= QUAL_RESTRICT; + break; + default: + done = 1; + break; } } while(!done); return specifiers; @@ -8190,8 +12823,9 @@ static struct type *type_specifier( struct compile_state *state, unsigned int spec) { struct type *type; + int tok; type = 0; - switch(peek(state)) { + switch((tok = peek(state))) { case TOK_VOID: eat(state, TOK_VOID); type = new_type(TYPE_VOID | spec, 0, 0); @@ -8325,8 +12959,8 @@ static struct type *type_specifier( type = typedef_name(state, spec); break; default: - error(state, 0, "bad type specifier %s", - tokens[peek(state)]); + error(state, 0, "bad type specifier %s", + tokens[tok]); break; } return type; @@ -8372,6 +13006,7 @@ static struct type *specifier_qualifier_list(struct compile_state *state) return type; } +#if DEBUG_ROMCC_WARNING static int isdecl_specifier(int tok) { switch(tok) { @@ -8409,6 +13044,7 @@ static int isdecl_specifier(int tok) return 0; } } +#endif static struct type *decl_specifiers(struct compile_state *state) { @@ -8426,6 +13062,9 @@ static struct type *decl_specifiers(struct compile_state *state) /* function-specifier */ specifiers |= function_specifier_opt(state); + /* attributes */ + specifiers |= attributes_opt(state, 0); + /* type qualifier */ specifiers |= type_qualifiers(state); @@ -8434,113 +13073,178 @@ 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) && + ((type->type & TYPE_MASK) != TYPE_UNION)) + { + 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 = eat(state, TOK_IDENT)->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( struct compile_state *state, struct type *type) { struct triple *result; +#if DEBUG_ROMCC_WARNINGS +#warning "FIXME more consistent initializer handling (where should eval_const_expr go?" +#endif if (peek(state) != TOK_LBRACE) { result = assignment_expr(state); + if (((type->type & TYPE_MASK) == TYPE_ARRAY) && + (type->elements == ELEMENT_COUNT_UNSPECIFIED) && + ((result->type->type & TYPE_MASK) == TYPE_ARRAY) && + (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) && + (equiv_types(type->left, result->type->left))) { + type->elements = result->type->elements; + } + if (is_lvalue(state, result) && + ((result->type->type & TYPE_MASK) == TYPE_ARRAY) && + (type->type & TYPE_MASK) != TYPE_ARRAY) + { + result = lvalue_conversion(state, result); + } + if (!is_init_compatible(state, type, result->type)) { + error(state, 0, "Incompatible types in initializer"); + } + if (!equiv_types(type, result->type)) { + result = mk_cast_expr(state, type, result); + } } 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(bits_to_bytes(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"); - memcpy(buf, old_buf, old_size); + old_size = max_offset; + max_offset = info.offset + value_size; + buf = xmalloc(bits_to_bytes(max_offset), "initializer"); + memcpy(buf, old_buf, bits_to_bytes(old_size)); xfree(old_buf); } + dest = ((char *)buf) + bits_to_bytes(info.offset); +#if DEBUG_INITIALIZER + fprintf(state->errout, "dest = buf + %d max_offset: %d value_size: %d op: %d\n", + dest - buf, + bits_to_bytes(max_offset), + bits_to_bytes(value_size), + value->op); +#endif if (value->op == OP_BLOBCONST) { - memcpy((char *)buf + index * value_size, value->u.blob, value_size); + memcpy(dest, value->u.blob, bits_to_bytes(value_size)); } - else if ((value->op == OP_INTCONST) && (value_size == 1)) { - *(((uint8_t *)buf) + index) = value->u.cval & 0xff; + else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I8)) { +#if DEBUG_INITIALIZER + fprintf(state->errout, "byte: %02x\n", value->u.cval & 0xff); +#endif + *((uint8_t *)dest) = value->u.cval & 0xff; } - else if ((value->op == OP_INTCONST) && (value_size == 2)) { - *(((uint16_t *)buf) + index) = value->u.cval & 0xffff; + else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I16)) { + *((uint16_t *)dest) = value->u.cval & 0xffff; } - else if ((value->op == OP_INTCONST) && (value_size == 4)) { - *(((uint32_t *)buf) + index) = value->u.cval & 0xffffffff; + else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I32)) { + *((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; @@ -8548,12 +13252,62 @@ static struct triple *initializer( return result; } +static void resolve_branches(struct compile_state *state, struct triple *first) +{ + /* 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; + struct triple *ins; + /* Also error on branches that do not use their targets */ + ins = first; + do { + if (!triple_is_ret(state, ins)) { + struct triple **expr ; + struct triple_set *set; + expr = triple_targ(state, ins, 0); + for(; expr; expr = triple_targ(state, ins, expr)) { + struct triple *targ; + targ = *expr; + for(set = targ?targ->use:0; set; set = set->next) { + if (set->member == ins) { + break; + } + } + if (!set) { + internal_error(state, ins, "targ not used"); + } + } + } + ins = ins->next; + } while(ins != first); + /* See if there are goto to labels that have not been defined */ + 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) { - struct triple *def, *tmp, *first, *end; + struct triple *def, *tmp, *first, *end, *retvar, *ret; + struct triple *fname; + struct type *fname_type; struct hash_entry *ident; - struct type *param; + struct type *param, *crtype, *ctype; int i; if ((type->type &TYPE_MASK) != TYPE_FUNCTION) { error(state, 0, "Invalid function header"); @@ -8578,7 +13332,7 @@ static struct triple *function_definition( if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) { error(state, 0, "No identifier for paramter %d\n", i); } - + /* Get a list of statements for this function. */ def = triple(state, OP_LIST, type, 0, 0); @@ -8592,6 +13346,27 @@ static struct triple *function_definition( /* Put a label at the very end of a function */ end = label(state); flatten(state, first, end); + /* Remember where return goes */ + ident = state->i_return; + symbol(state, ident, &ident->sym_ident, end, end->type); + + /* Get the initial closure type */ + ctype = new_type(TYPE_JOIN, &void_type, 0); + ctype->elements = 1; + + /* Add a variable for the return value */ + crtype = new_type(TYPE_TUPLE, + /* Remove all type qualifiers from the return type */ + new_type(TYPE_PRODUCT, ctype, clone_type(0, type->left)), 0); + crtype->elements = 2; + flatten(state, end, variable(state, crtype)); + + /* Allocate a variable for the return address */ + retvar = flatten(state, end, variable(state, &void_ptr_type)); + + /* Add in the return instruction */ + ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0); + ret = flatten(state, first, ret); /* Walk through the parameters and create symbol table entries * for them. @@ -8600,7 +13375,7 @@ static struct triple *function_definition( while((param->type & TYPE_MASK) == TYPE_PRODUCT) { ident = param->left->field_ident; tmp = variable(state, param->left); - symbol(state, ident, &ident->sym_ident, tmp, tmp->type); + var_symbol(state, ident, tmp); flatten(state, end, tmp); param = param->right; } @@ -8611,159 +13386,1512 @@ static struct triple *function_definition( symbol(state, ident, &ident->sym_ident, tmp, tmp->type); flatten(state, end, tmp); } - /* Add a variable for the return value */ - 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 */ - MISC(def, 0) = tmp; - } + + /* Add the declaration static const char __func__ [] = "func-name" */ + fname_type = new_type(TYPE_ARRAY, + clone_type(QUAL_CONST | STOR_STATIC, &char_type), 0); + fname_type->type |= QUAL_CONST | STOR_STATIC; + fname_type->elements = strlen(state->function) + 1; + + fname = triple(state, OP_BLOBCONST, fname_type, 0, 0); + fname->u.blob = (void *)state->function; + fname = flatten(state, end, fname); + + ident = state->i___func__; + symbol(state, ident, &ident->sym_ident, fname, fname_type); /* Remember which function I am compiling. * Also assume the last defined function is the main function. */ state->main_function = def; - /* Now get the actual function definition */ - compound_statement(state, end); + /* Now get the actual function definition */ + compound_statement(state, end); + + /* Finish anything unfinished with branches */ + resolve_branches(state, first); + + /* Remove the parameter scope */ + end_scope(state); + + + /* Remember I have defined a function */ + if (!state->functions) { + state->functions = def; + } else { + insert_triple(state, state->functions, def); + } + if (state->compiler->debug & DEBUG_INLINE) { + FILE *fp = state->dbgout; + fprintf(fp, "\n"); + loc(fp, state, 0); + fprintf(fp, "\n__________ %s _________\n", __FUNCTION__); + display_func(state, fp, def); + fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__); + } + + return def; +} + +static struct triple *do_decl(struct compile_state *state, + struct type *type, struct hash_entry *ident) +{ + struct triple *def; + def = 0; + /* Clean up the storage types used */ + switch (type->type & STOR_MASK) { + case STOR_AUTO: + case STOR_STATIC: + /* These are the good types I am aiming for */ + break; + case STOR_REGISTER: + type->type &= ~STOR_MASK; + type->type |= STOR_AUTO; + break; + case STOR_LOCAL: + case STOR_EXTERN: + type->type &= ~STOR_MASK; + type->type |= STOR_STATIC; + break; + case STOR_TYPEDEF: + 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 & TYPE_MASK) == TYPE_FUNCTION) { + error(state, 0, "Function prototypes not supported"); + } + if (ident && + ((type->type & TYPE_MASK) == TYPE_ARRAY) && + ((type->type & STOR_MASK) != STOR_STATIC)) + error(state, 0, "non static arrays not supported"); + if (ident && + ((type->type & STOR_MASK) == STOR_STATIC) && + ((type->type & QUAL_CONST) == 0)) { + error(state, 0, "non const static variables not supported"); + } + if (ident) { + def = variable(state, type); + var_symbol(state, ident, def); + } + return def; +} + +static void decl(struct compile_state *state, struct triple *first) +{ + struct type *base_type, *type; + struct hash_entry *ident; + struct triple *def; + int global; + global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH); + base_type = decl_specifiers(state); + ident = 0; + type = declarator(state, base_type, &ident, 0); + type->type = attributes_opt(state, type->type); + if (global && ident && (peek(state) == TOK_LBRACE)) { + /* function */ + type->type_ident = ident; + state->function = ident->name; + def = function_definition(state, type); + symbol(state, ident, &ident->sym_ident, def, type); + state->function = 0; + } + else { + int done; + flatten(state, first, do_decl(state, type, ident)); + /* type or variable definition */ + do { + done = 1; + if (peek(state) == TOK_EQ) { + if (!ident) { + error(state, 0, "cannot assign to a type"); + } + eat(state, TOK_EQ); + flatten(state, first, + init_expr(state, + ident->sym_ident->def, + initializer(state, type))); + } + arrays_complete(state, type); + if (peek(state) == TOK_COMMA) { + eat(state, TOK_COMMA); + ident = 0; + type = declarator(state, base_type, &ident, 0); + flatten(state, first, do_decl(state, type, ident)); + done = 0; + } + } while(!done); + eat(state, TOK_SEMI); + } +} + +static void decls(struct compile_state *state) +{ + struct triple *list; + int tok; + list = label(state); + while(1) { + tok = peek(state); + if (tok == TOK_EOF) { + return; + } + if (tok == TOK_SPACE) { + eat(state, TOK_SPACE); + } + decl(state, list); + if (list->next != list) { + error(state, 0, "global variables not supported"); + } + } +} + +/* + * Function inlining + */ +struct triple_reg_set { + struct triple_reg_set *next; + struct triple *member; + struct triple *new; +}; +struct reg_block { + struct block *block; + struct triple_reg_set *in; + struct triple_reg_set *out; + int vertex; +}; +static void setup_basic_blocks(struct compile_state *, struct basic_blocks *bb); +static void analyze_basic_blocks(struct compile_state *state, struct basic_blocks *bb); +static void free_basic_blocks(struct compile_state *, struct basic_blocks *bb); +static int tdominates(struct compile_state *state, struct triple *dom, struct triple *sub); +static void walk_blocks(struct compile_state *state, struct basic_blocks *bb, + void (*cb)(struct compile_state *state, struct block *block, void *arg), + void *arg); +static void print_block( + struct compile_state *state, struct block *block, void *arg); +static int do_triple_set(struct triple_reg_set **head, + struct triple *member, struct triple *new_member); +static void do_triple_unset(struct triple_reg_set **head, struct triple *member); +static struct reg_block *compute_variable_lifetimes( + struct compile_state *state, struct basic_blocks *bb); +static void free_variable_lifetimes(struct compile_state *state, + struct basic_blocks *bb, struct reg_block *blocks); +#if DEBUG_EXPLICIT_CLOSURES +static void print_live_variables(struct compile_state *state, + struct basic_blocks *bb, struct reg_block *rb, FILE *fp); +#endif + + +static struct triple *call(struct compile_state *state, + struct triple *retvar, struct triple *ret_addr, + struct triple *targ, struct triple *ret) +{ + struct triple *call; + + if (!retvar || !is_lvalue(state, retvar)) { + internal_error(state, 0, "writing to a non lvalue?"); + } + write_compatible(state, retvar->type, &void_ptr_type); + + call = new_triple(state, OP_CALL, &void_type, 1, 0); + TARG(call, 0) = targ; + MISC(call, 0) = ret; + if (!targ || (targ->op != OP_LABEL)) { + internal_error(state, 0, "call not to a label"); + } + if (!ret || (ret->op != OP_RET)) { + internal_error(state, 0, "call not matched with return"); + } + return call; +} + +static void walk_functions(struct compile_state *state, + void (*cb)(struct compile_state *state, struct triple *func, void *arg), + void *arg) +{ + struct triple *func, *first; + func = first = state->functions; + do { + cb(state, func, arg); + func = func->next; + } while(func != first); +} + +static void reverse_walk_functions(struct compile_state *state, + void (*cb)(struct compile_state *state, struct triple *func, void *arg), + void *arg) +{ + struct triple *func, *first; + func = first = state->functions; + do { + func = func->prev; + cb(state, func, arg); + } while(func != first); +} + + +static void mark_live(struct compile_state *state, struct triple *func, void *arg) +{ + struct triple *ptr, *first; + if (func->u.cval == 0) { + return; + } + ptr = first = RHS(func, 0); + do { + if (ptr->op == OP_FCALL) { + struct triple *called_func; + called_func = MISC(ptr, 0); + /* Mark the called function as used */ + if (!(func->id & TRIPLE_FLAG_FLATTENED)) { + called_func->u.cval++; + } + /* Remove the called function from the list */ + called_func->prev->next = called_func->next; + called_func->next->prev = called_func->prev; + + /* Place the called function before me on the list */ + called_func->next = func; + called_func->prev = func->prev; + called_func->prev->next = called_func; + called_func->next->prev = called_func; + } + ptr = ptr->next; + } while(ptr != first); + func->id |= TRIPLE_FLAG_FLATTENED; +} + +static void mark_live_functions(struct compile_state *state) +{ + /* Ensure state->main_function is the last function in + * the list of functions. + */ + if ((state->main_function->next != state->functions) || + (state->functions->prev != state->main_function)) { + internal_error(state, 0, + "state->main_function is not at the end of the function list "); + } + state->main_function->u.cval = 1; + reverse_walk_functions(state, mark_live, 0); +} + +static int local_triple(struct compile_state *state, + struct triple *func, struct triple *ins) +{ + int local = (ins->id & TRIPLE_FLAG_LOCAL); +#if 0 + if (!local) { + FILE *fp = state->errout; + fprintf(fp, "global: "); + display_triple(fp, ins); + } +#endif + return local; +} + +struct triple *copy_func(struct compile_state *state, struct triple *ofunc, + struct occurance *base_occurance) +{ + struct triple *nfunc; + struct triple *nfirst, *ofirst; + struct triple *new, *old; + + if (state->compiler->debug & DEBUG_INLINE) { + FILE *fp = state->dbgout; + fprintf(fp, "\n"); + loc(fp, state, 0); + fprintf(fp, "\n__________ %s _________\n", __FUNCTION__); + display_func(state, fp, ofunc); + fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__); + } + + /* Make a new copy of the old function */ + nfunc = triple(state, OP_LIST, ofunc->type, 0, 0); + nfirst = 0; + ofirst = old = RHS(ofunc, 0); + do { + struct triple *new; + struct occurance *occurance; + int old_lhs, old_rhs; + old_lhs = old->lhs; + old_rhs = old->rhs; + occurance = inline_occurance(state, base_occurance, old->occurance); + if (ofunc->u.cval && (old->op == OP_FCALL)) { + MISC(old, 0)->u.cval += 1; + } + 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)); + } + if (!nfirst) { + RHS(nfunc, 0) = nfirst = new; + } + else { + insert_triple(state, nfirst, new); + } + new->id |= TRIPLE_FLAG_FLATTENED; + new->id |= old->id & TRIPLE_FLAG_COPY; + + /* During the copy remember new as user of old */ + use_triple(old, new); + + /* Remember which instructions are local */ + old->id |= TRIPLE_FLAG_LOCAL; + old = old->next; + } while(old != ofirst); + + /* Make a second pass to fix up any unresolved references */ + old = ofirst; + new = nfirst; + do { + struct triple **oexpr, **nexpr; + int count, i; + /* Lookup where the copy is, to join pointers */ + count = TRIPLE_SIZE(old); + for(i = 0; i < count; i++) { + oexpr = &old->param[i]; + nexpr = &new->param[i]; + if (*oexpr && !*nexpr) { + if (!local_triple(state, ofunc, *oexpr)) { + *nexpr = *oexpr; + } + else if ((*oexpr)->use) { + *nexpr = (*oexpr)->use->member; + } + if (*nexpr == old) { + internal_error(state, 0, "new == old?"); + } + use_triple(*nexpr, new); + } + if (!*nexpr && *oexpr) { + internal_error(state, 0, "Could not copy %d", i); + } + } + old = old->next; + new = new->next; + } while((old != ofirst) && (new != nfirst)); + + /* Make a third pass to cleanup the extra useses */ + old = ofirst; + new = nfirst; + do { + unuse_triple(old, new); + /* Forget which instructions are local */ + old->id &= ~TRIPLE_FLAG_LOCAL; + old = old->next; + new = new->next; + } while ((old != ofirst) && (new != nfirst)); + return nfunc; +} + +static void expand_inline_call( + struct compile_state *state, struct triple *me, struct triple *fcall) +{ + /* Inline the function call */ + struct type *ptype; + struct triple *ofunc, *nfunc, *nfirst, *result, *retvar, *ins; + struct triple *end, *nend; + int pvals, i; + + /* Find the triples */ + ofunc = MISC(fcall, 0); + if (ofunc->op != OP_LIST) { + internal_error(state, 0, "improper function"); + } + nfunc = copy_func(state, ofunc, fcall->occurance); + /* Prepend the parameter reading into the new function list */ + ptype = nfunc->type->right; + pvals = fcall->rhs; + for(i = 0; i < pvals; i++) { + struct type *atype; + struct triple *arg, *param; + atype = ptype; + if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) { + atype = ptype->left; + } + param = farg(state, nfunc, i); + if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) { + internal_error(state, fcall, "param %d type mismatch", i); + } + arg = RHS(fcall, i); + flatten(state, fcall, write_expr(state, param, arg)); + ptype = ptype->right; + } + result = 0; + if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) { + result = read_expr(state, + deref_index(state, fresult(state, nfunc), 1)); + } + if (state->compiler->debug & DEBUG_INLINE) { + FILE *fp = state->dbgout; + fprintf(fp, "\n"); + loc(fp, state, 0); + fprintf(fp, "\n__________ %s _________\n", __FUNCTION__); + display_func(state, fp, nfunc); + fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__); + } + + /* + * Get rid of the extra triples + */ + /* Remove the read of the return address */ + ins = RHS(nfunc, 0)->prev->prev; + if ((ins->op != OP_READ) || (RHS(ins, 0) != fretaddr(state, nfunc))) { + internal_error(state, ins, "Not return addres read?"); + } + release_triple(state, ins); + /* Remove the return instruction */ + ins = RHS(nfunc, 0)->prev; + if (ins->op != OP_RET) { + internal_error(state, ins, "Not return?"); + } + release_triple(state, ins); + /* Remove the retaddres variable */ + retvar = fretaddr(state, nfunc); + if ((retvar->lhs != 1) || + (retvar->op != OP_ADECL) || + (retvar->next->op != OP_PIECE) || + (MISC(retvar->next, 0) != retvar)) { + internal_error(state, retvar, "Not the return address?"); + } + release_triple(state, retvar->next); + release_triple(state, retvar); + + /* Remove the label at the start of the function */ + ins = RHS(nfunc, 0); + if (ins->op != OP_LABEL) { + internal_error(state, ins, "Not label?"); + } + nfirst = ins->next; + free_triple(state, ins); + /* Release the new function header */ + RHS(nfunc, 0) = 0; + free_triple(state, nfunc); + + /* Append the new function list onto the return list */ + end = fcall->prev; + nend = nfirst->prev; + end->next = nfirst; + nfirst->prev = end; + nend->next = fcall; + fcall->prev = nend; + + /* Now the result reading code */ + if (result) { + result = flatten(state, fcall, result); + propogate_use(state, fcall, result); + } + + /* Release the original fcall instruction */ + release_triple(state, fcall); + + return; +} + +/* + * + * Type of the result variable. + * + * result + * | + * +----------+------------+ + * | | + * union of closures result_type + * | + * +------------------+---------------+ + * | | + * closure1 ... closuerN + * | | + * +----+--+-+--------+-----+ +----+----+---+-----+ + * | | | | | | | | | + * var1 var2 var3 ... varN result var1 var2 ... varN result + * | + * +--------+---------+ + * | | + * union of closures result_type + * | + * +-----+-------------------+ + * | | + * closure1 ... closureN + * | | + * +-----+---+----+----+ +----+---+----+-----+ + * | | | | | | | | + * var1 var2 ... varN result var1 var2 ... varN result + */ + +static int add_closure_type(struct compile_state *state, + struct triple *func, struct type *closure_type) +{ + struct type *type, *ctype, **next; + struct triple *var, *new_var; + int i; + +#if 0 + FILE *fp = state->errout; + fprintf(fp, "original_type: "); + name_of(fp, fresult(state, func)->type); + fprintf(fp, "\n"); +#endif + /* find the original type */ + var = fresult(state, func); + type = var->type; + if (type->elements != 2) { + internal_error(state, var, "bad return type"); + } + + /* Find the complete closure type and update it */ + ctype = type->left->left; + next = &ctype->left; + while(((*next)->type & TYPE_MASK) == TYPE_OVERLAP) { + next = &(*next)->right; + } + *next = new_type(TYPE_OVERLAP, *next, dup_type(state, closure_type)); + ctype->elements += 1; + +#if 0 + fprintf(fp, "new_type: "); + name_of(fp, type); + fprintf(fp, "\n"); + fprintf(fp, "ctype: %p %d bits: %d ", + ctype, ctype->elements, reg_size_of(state, ctype)); + name_of(fp, ctype); + fprintf(fp, "\n"); +#endif + + /* Regenerate the variable with the new type definition */ + new_var = pre_triple(state, var, OP_ADECL, type, 0, 0); + new_var->id |= TRIPLE_FLAG_FLATTENED; + for(i = 0; i < new_var->lhs; i++) { + LHS(new_var, i)->id |= TRIPLE_FLAG_FLATTENED; + } + + /* Point everyone at the new variable */ + propogate_use(state, var, new_var); + + /* Release the original variable */ + for(i = 0; i < var->lhs; i++) { + release_triple(state, LHS(var, i)); + } + release_triple(state, var); + + /* Return the index of the added closure type */ + return ctype->elements - 1; +} + +static struct triple *closure_expr(struct compile_state *state, + struct triple *func, int closure_idx, int var_idx) +{ + return deref_index(state, + deref_index(state, + deref_index(state, fresult(state, func), 0), + closure_idx), + var_idx); +} + + +static void insert_triple_set( + struct triple_reg_set **head, struct triple *member) +{ + struct triple_reg_set *new; + new = xcmalloc(sizeof(*new), "triple_set"); + new->member = member; + new->new = 0; + new->next = *head; + *head = new; +} + +static int ordered_triple_set( + struct triple_reg_set **head, struct triple *member) +{ + struct triple_reg_set **ptr; + if (!member) + return 0; + ptr = head; + while(*ptr) { + if (member == (*ptr)->member) { + return 0; + } + /* keep the list ordered */ + if (member->id < (*ptr)->member->id) { + break; + } + ptr = &(*ptr)->next; + } + insert_triple_set(ptr, member); + return 1; +} + + +static void free_closure_variables(struct compile_state *state, + struct triple_reg_set **enclose) +{ + struct triple_reg_set *entry, *next; + for(entry = *enclose; entry; entry = next) { + next = entry->next; + do_triple_unset(enclose, entry->member); + } +} + +static int lookup_closure_index(struct compile_state *state, + struct triple *me, struct triple *val) +{ + struct triple *first, *ins, *next; + first = RHS(me, 0); + ins = next = first; + do { + struct triple *result; + struct triple *index0, *index1, *index2, *read, *write; + ins = next; + next = ins->next; + if (ins->op != OP_CALL) { + continue; + } + /* I am at a previous call point examine it closely */ + if (ins->next->op != OP_LABEL) { + internal_error(state, ins, "call not followed by label"); + } + /* Does this call does not enclose any variables? */ + if ((ins->next->next->op != OP_INDEX) || + (ins->next->next->u.cval != 0) || + (result = MISC(ins->next->next, 0)) || + (result->id & TRIPLE_FLAG_LOCAL)) { + continue; + } + index0 = ins->next->next; + /* The pattern is: + * 0 index result < 0 > + * 1 index 0 < ? > + * 2 index 1 < ? > + * 3 read 2 + * 4 write 3 var + */ + for(index0 = ins->next->next; + (index0->op == OP_INDEX) && + (MISC(index0, 0) == result) && + (index0->u.cval == 0) ; + index0 = write->next) + { + index1 = index0->next; + index2 = index1->next; + read = index2->next; + write = read->next; + if ((index0->op != OP_INDEX) || + (index1->op != OP_INDEX) || + (index2->op != OP_INDEX) || + (read->op != OP_READ) || + (write->op != OP_WRITE) || + (MISC(index1, 0) != index0) || + (MISC(index2, 0) != index1) || + (RHS(read, 0) != index2) || + (RHS(write, 0) != read)) { + internal_error(state, index0, "bad var read"); + } + if (MISC(write, 0) == val) { + return index2->u.cval; + } + } + } while(next != first); + return -1; +} + +static inline int enclose_triple(struct triple *ins) +{ + return (ins && ((ins->type->type & TYPE_MASK) != TYPE_VOID)); +} + +static void compute_closure_variables(struct compile_state *state, + struct triple *me, struct triple *fcall, struct triple_reg_set **enclose) +{ + struct triple_reg_set *set, *vars, **last_var; + struct basic_blocks bb; + struct reg_block *rb; + struct block *block; + struct triple *old_result, *first, *ins; + size_t count, idx; + unsigned long used_indicies; + int i, max_index; +#define MAX_INDICIES (sizeof(used_indicies)*CHAR_BIT) +#define ID_BITS(X) ((X) & (TRIPLE_FLAG_LOCAL -1)) + struct { + unsigned id; + int index; + } *info; + + + /* Find the basic blocks of this function */ + bb.func = me; + bb.first = RHS(me, 0); + old_result = 0; + if (!triple_is_ret(state, bb.first->prev)) { + bb.func = 0; + } else { + old_result = fresult(state, me); + } + analyze_basic_blocks(state, &bb); + + /* Find which variables are currently alive in a given block */ + rb = compute_variable_lifetimes(state, &bb); + + /* Find the variables that are currently alive */ + block = block_of_triple(state, fcall); + if (!block || (block->vertex <= 0) || (block->vertex > bb.last_vertex)) { + internal_error(state, fcall, "No reg block? block: %p", block); + } + +#if DEBUG_EXPLICIT_CLOSURES + print_live_variables(state, &bb, rb, state->dbgout); + fflush(state->dbgout); +#endif + + /* Count the number of triples in the function */ + first = RHS(me, 0); + ins = first; + count = 0; + do { + count++; + ins = ins->next; + } while(ins != first); + + /* Allocate some memory to temorary hold the id info */ + info = xcmalloc(sizeof(*info) * (count +1), "info"); + + /* Mark the local function */ + first = RHS(me, 0); + ins = first; + idx = 1; + do { + info[idx].id = ins->id; + ins->id = TRIPLE_FLAG_LOCAL | idx; + idx++; + ins = ins->next; + } while(ins != first); + + /* + * Build the list of variables to enclose. + * + * A target it to put the same variable in the + * same slot for ever call of a given function. + * After coloring this removes all of the variable + * manipulation code. + * + * The list of variables to enclose is built ordered + * program order because except in corner cases this + * gives me the stability of assignment I need. + * + * To gurantee that stability I lookup the variables + * to see where they have been used before and + * I build my final list with the assigned indicies. + */ + vars = 0; + if (enclose_triple(old_result)) { + ordered_triple_set(&vars, old_result); + } + for(set = rb[block->vertex].out; set; set = set->next) { + if (!enclose_triple(set->member)) { + continue; + } + if ((set->member == fcall) || (set->member == old_result)) { + continue; + } + if (!local_triple(state, me, set->member)) { + internal_error(state, set->member, "not local?"); + } + ordered_triple_set(&vars, set->member); + } + + /* Lookup the current indicies of the live varialbe */ + used_indicies = 0; + max_index = -1; + for(set = vars; set ; set = set->next) { + struct triple *ins; + int index; + ins = set->member; + index = lookup_closure_index(state, me, ins); + info[ID_BITS(ins->id)].index = index; + if (index < 0) { + continue; + } + if (index >= MAX_INDICIES) { + internal_error(state, ins, "index unexpectedly large"); + } + if (used_indicies & (1 << index)) { + internal_error(state, ins, "index previously used?"); + } + /* Remember which indicies have been used */ + used_indicies |= (1 << index); + if (index > max_index) { + max_index = index; + } + } + + /* Walk through the live variables and make certain + * everything is assigned an index. + */ + for(set = vars; set; set = set->next) { + struct triple *ins; + int index; + ins = set->member; + index = info[ID_BITS(ins->id)].index; + if (index >= 0) { + continue; + } + /* Find the lowest unused index value */ + for(index = 0; index < MAX_INDICIES; index++) { + if (!(used_indicies & (1 << index))) { + break; + } + } + if (index == MAX_INDICIES) { + internal_error(state, ins, "no free indicies?"); + } + info[ID_BITS(ins->id)].index = index; + /* Remember which indicies have been used */ + used_indicies |= (1 << index); + if (index > max_index) { + max_index = index; + } + } + + /* Build the return list of variables with positions matching + * their indicies. + */ + *enclose = 0; + last_var = enclose; + for(i = 0; i <= max_index; i++) { + struct triple *var; + var = 0; + if (used_indicies & (1 << i)) { + for(set = vars; set; set = set->next) { + int index; + index = info[ID_BITS(set->member->id)].index; + if (index == i) { + var = set->member; + break; + } + } + if (!var) { + internal_error(state, me, "missing variable"); + } + } + insert_triple_set(last_var, var); + last_var = &(*last_var)->next; + } + +#if DEBUG_EXPLICIT_CLOSURES + /* Print out the variables to be enclosed */ + loc(state->dbgout, state, fcall); + fprintf(state->dbgout, "Alive: \n"); + for(set = *enclose; set; set = set->next) { + display_triple(state->dbgout, set->member); + } + fflush(state->dbgout); +#endif + + /* Clear the marks */ + ins = first; + do { + ins->id = info[ID_BITS(ins->id)].id; + ins = ins->next; + } while(ins != first); + + /* Release the ordered list of live variables */ + free_closure_variables(state, &vars); + + /* Release the storage of the old ids */ + xfree(info); + + /* Release the variable lifetime information */ + free_variable_lifetimes(state, &bb, rb); + + /* Release the basic blocks of this function */ + free_basic_blocks(state, &bb); +} + +static void expand_function_call( + struct compile_state *state, struct triple *me, struct triple *fcall) +{ + /* Generate an ordinary function call */ + struct type *closure_type, **closure_next; + struct triple *func, *func_first, *func_last, *retvar; + struct triple *first; + struct type *ptype, *rtype; + struct triple *ret_addr, *ret_loc; + struct triple_reg_set *enclose, *set; + int closure_idx, pvals, i; + +#if DEBUG_EXPLICIT_CLOSURES + FILE *fp = state->dbgout; + fprintf(fp, "\ndisplay_func(me) ptr: %p\n", fcall); + display_func(state, fp, MISC(fcall, 0)); + display_func(state, fp, me); + fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__); +#endif + + /* Find the triples */ + func = MISC(fcall, 0); + func_first = RHS(func, 0); + retvar = fretaddr(state, func); + func_last = func_first->prev; + first = fcall->next; + + /* Find what I need to enclose */ + compute_closure_variables(state, me, fcall, &enclose); + + /* Compute the closure type */ + closure_type = new_type(TYPE_TUPLE, 0, 0); + closure_type->elements = 0; + closure_next = &closure_type->left; + for(set = enclose; set ; set = set->next) { + struct type *type; + type = &void_type; + if (set->member) { + type = set->member->type; + } + if (!*closure_next) { + *closure_next = type; + } else { + *closure_next = new_type(TYPE_PRODUCT, *closure_next, + type); + closure_next = &(*closure_next)->right; + } + closure_type->elements += 1; + } + if (closure_type->elements == 0) { + closure_type->type = TYPE_VOID; + } + + +#if DEBUG_EXPLICIT_CLOSURES + fprintf(state->dbgout, "closure type: "); + name_of(state->dbgout, closure_type); + fprintf(state->dbgout, "\n"); +#endif + + /* Update the called functions closure variable */ + closure_idx = add_closure_type(state, func, closure_type); + + /* Generate some needed triples */ + ret_loc = label(state); + ret_addr = triple(state, OP_ADDRCONST, &void_ptr_type, ret_loc, 0); + + /* Pass the parameters to the new function */ + ptype = func->type->right; + pvals = fcall->rhs; + for(i = 0; i < pvals; i++) { + struct type *atype; + struct triple *arg, *param; + atype = ptype; + if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) { + atype = ptype->left; + } + param = farg(state, func, i); + if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) { + internal_error(state, fcall, "param type mismatch"); + } + arg = RHS(fcall, i); + flatten(state, first, write_expr(state, param, arg)); + ptype = ptype->right; + } + rtype = func->type->left; + + /* Thread the triples together */ + ret_loc = flatten(state, first, ret_loc); + + /* Save the active variables in the result variable */ + for(i = 0, set = enclose; set ; set = set->next, i++) { + if (!set->member) { + continue; + } + flatten(state, ret_loc, + write_expr(state, + closure_expr(state, func, closure_idx, i), + read_expr(state, set->member))); + } + + /* Initialize the return value */ + if ((rtype->type & TYPE_MASK) != TYPE_VOID) { + flatten(state, ret_loc, + write_expr(state, + deref_index(state, fresult(state, func), 1), + new_triple(state, OP_UNKNOWNVAL, rtype, 0, 0))); + } + + ret_addr = flatten(state, ret_loc, ret_addr); + flatten(state, ret_loc, write_expr(state, retvar, ret_addr)); + flatten(state, ret_loc, + call(state, retvar, ret_addr, func_first, func_last)); + + /* Find the result */ + if ((rtype->type & TYPE_MASK) != TYPE_VOID) { + struct triple * result; + result = flatten(state, first, + read_expr(state, + deref_index(state, fresult(state, func), 1))); + + propogate_use(state, fcall, result); + } + + /* Release the original fcall instruction */ + release_triple(state, fcall); + + /* Restore the active variables from the result variable */ + for(i = 0, set = enclose; set ; set = set->next, i++) { + struct triple_set *use, *next; + struct triple *new; + struct basic_blocks bb; + if (!set->member || (set->member == fcall)) { + continue; + } + /* Generate an expression for the value */ + new = flatten(state, first, + read_expr(state, + closure_expr(state, func, closure_idx, i))); + + + /* If the original is an lvalue restore the preserved value */ + if (is_lvalue(state, set->member)) { + flatten(state, first, + write_expr(state, set->member, new)); + continue; + } + /* + * If the original is a value update the dominated uses. + */ + + /* Analyze the basic blocks so I can see who dominates whom */ + bb.func = me; + bb.first = RHS(me, 0); + if (!triple_is_ret(state, bb.first->prev)) { + bb.func = 0; + } + analyze_basic_blocks(state, &bb); + + +#if DEBUG_EXPLICIT_CLOSURES + fprintf(state->errout, "Updating domindated uses: %p -> %p\n", + set->member, new); +#endif + /* If fcall dominates the use update the expression */ + for(use = set->member->use; use; use = next) { + /* Replace use modifies the use chain and + * removes use, so I must take a copy of the + * next entry early. + */ + next = use->next; + if (!tdominates(state, fcall, use->member)) { + continue; + } + replace_use(state, set->member, new, use->member); + } + + /* Release the basic blocks, the instructions will be + * different next time, and flatten/insert_triple does + * not update the block values so I can't cache the analysis. + */ + free_basic_blocks(state, &bb); + } + + /* Release the closure variable list */ + free_closure_variables(state, &enclose); + + if (state->compiler->debug & DEBUG_INLINE) { + FILE *fp = state->dbgout; + fprintf(fp, "\n"); + loc(fp, state, 0); + fprintf(fp, "\n__________ %s _________\n", __FUNCTION__); + display_func(state, fp, func); + display_func(state, fp, me); + fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__); + } + + return; +} + +static int do_inline(struct compile_state *state, struct triple *func) +{ + int do_inline; + int policy; + + policy = state->compiler->flags & COMPILER_INLINE_MASK; + switch(policy) { + case COMPILER_INLINE_ALWAYS: + do_inline = 1; + if (func->type->type & ATTRIB_NOINLINE) { + error(state, func, "noinline with always_inline compiler option"); + } + break; + case COMPILER_INLINE_NEVER: + do_inline = 0; + if (func->type->type & ATTRIB_ALWAYS_INLINE) { + error(state, func, "always_inline with noinline compiler option"); + } + break; + case COMPILER_INLINE_DEFAULTON: + switch(func->type->type & STOR_MASK) { + case STOR_STATIC | STOR_INLINE: + case STOR_LOCAL | STOR_INLINE: + case STOR_EXTERN | STOR_INLINE: + do_inline = 1; + break; + default: + do_inline = 1; + break; + } + break; + case COMPILER_INLINE_DEFAULTOFF: + switch(func->type->type & STOR_MASK) { + case STOR_STATIC | STOR_INLINE: + case STOR_LOCAL | STOR_INLINE: + case STOR_EXTERN | STOR_INLINE: + do_inline = 1; + break; + default: + do_inline = 0; + break; + } + break; + case COMPILER_INLINE_NOPENALTY: + switch(func->type->type & STOR_MASK) { + case STOR_STATIC | STOR_INLINE: + case STOR_LOCAL | STOR_INLINE: + case STOR_EXTERN | STOR_INLINE: + do_inline = 1; + break; + default: + do_inline = (func->u.cval == 1); + break; + } + break; + default: + do_inline = 0; + internal_error(state, 0, "Unimplemented inline policy"); + break; + } + /* Force inlining */ + if (func->type->type & ATTRIB_NOINLINE) { + do_inline = 0; + } + if (func->type->type & ATTRIB_ALWAYS_INLINE) { + do_inline = 1; + } + return do_inline; +} + +static void inline_function(struct compile_state *state, struct triple *me, void *arg) +{ + struct triple *first, *ptr, *next; + /* If the function is not used don't bother */ + if (me->u.cval <= 0) { + return; + } + if (state->compiler->debug & DEBUG_CALLS2) { + FILE *fp = state->dbgout; + fprintf(fp, "in: %s\n", + me->type->type_ident->name); + } + + first = RHS(me, 0); + ptr = next = first; + do { + struct triple *func, *prev; + ptr = next; + prev = ptr->prev; + next = ptr->next; + if (ptr->op != OP_FCALL) { + continue; + } + func = MISC(ptr, 0); + /* See if the function should be inlined */ + if (!do_inline(state, func)) { + /* Put a label after the fcall */ + post_triple(state, ptr, OP_LABEL, &void_type, 0, 0); + continue; + } + if (state->compiler->debug & DEBUG_CALLS) { + FILE *fp = state->dbgout; + if (state->compiler->debug & DEBUG_CALLS2) { + loc(fp, state, ptr); + } + fprintf(fp, "inlining %s\n", + func->type->type_ident->name); + fflush(fp); + } + + /* Update the function use counts */ + func->u.cval -= 1; + + /* Replace the fcall with the called function */ + expand_inline_call(state, me, ptr); + + next = prev->next; + } while (next != first); + + ptr = next = first; + do { + struct triple *prev, *func; + ptr = next; + prev = ptr->prev; + next = ptr->next; + if (ptr->op != OP_FCALL) { + continue; + } + func = MISC(ptr, 0); + if (state->compiler->debug & DEBUG_CALLS) { + FILE *fp = state->dbgout; + if (state->compiler->debug & DEBUG_CALLS2) { + loc(fp, state, ptr); + } + fprintf(fp, "calling %s\n", + func->type->type_ident->name); + fflush(fp); + } + /* Replace the fcall with the instruction sequence + * needed to make the call. + */ + expand_function_call(state, me, ptr); + next = prev->next; + } while(next != first); +} + +static void inline_functions(struct compile_state *state, struct triple *func) +{ + inline_function(state, func, 0); + reverse_walk_functions(state, inline_function, 0); +} + +static void insert_function(struct compile_state *state, + struct triple *func, void *arg) +{ + struct triple *first, *end, *ffirst, *fend; + + if (state->compiler->debug & DEBUG_INLINE) { + FILE *fp = state->errout; + fprintf(fp, "%s func count: %d\n", + func->type->type_ident->name, func->u.cval); + } + if (func->u.cval == 0) { + return; + } + + /* Find the end points of the lists */ + first = arg; + end = first->prev; + ffirst = RHS(func, 0); + fend = ffirst->prev; + + /* splice the lists together */ + end->next = ffirst; + ffirst->prev = end; + fend->next = first; + first->prev = fend; +} + +struct triple *input_asm(struct compile_state *state) +{ + struct asm_info *info; + struct triple *def; + int i, out; + + info = xcmalloc(sizeof(*info), "asm_info"); + info->str = ""; + + out = sizeof(arch_input_regs)/sizeof(arch_input_regs[0]); + memcpy(&info->tmpl.lhs, arch_input_regs, sizeof(arch_input_regs)); - /* Remove the parameter scope */ - end_scope(state); -#if 0 - fprintf(stdout, "\n"); - loc(stdout, state, 0); - fprintf(stdout, "\n__________ function_definition _________\n"); - print_triple(state, def); - fprintf(stdout, "__________ function_definition _________ done\n\n"); -#endif + def = new_triple(state, OP_ASM, &void_type, out, 0); + def->u.ainfo = info; + def->id |= TRIPLE_FLAG_VOLATILE; + + for(i = 0; i < out; i++) { + struct triple *piece; + piece = triple(state, OP_PIECE, &int_type, def, 0); + piece->u.cval = i; + LHS(def, i) = piece; + } return def; } -static struct triple *do_decl(struct compile_state *state, - struct type *type, struct hash_entry *ident) +struct triple *output_asm(struct compile_state *state) { + struct asm_info *info; struct triple *def; - def = 0; - /* Clean up the storage types used */ - switch (type->type & STOR_MASK) { - case STOR_AUTO: - case STOR_STATIC: - /* These are the good types I am aiming for */ - break; - case STOR_REGISTER: - type->type &= ~STOR_MASK; - type->type |= STOR_AUTO; - break; - case STOR_EXTERN: - type->type &= ~STOR_MASK; - type->type |= STOR_STATIC; - break; - case STOR_TYPEDEF: - 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) && - ((type->type & QUAL_CONST) == 0)) { - error(state, 0, "non const static variables not supported"); - } - if (ident) { - def = variable(state, type); - symbol(state, ident, &ident->sym_ident, def, type); - } + int in; + + info = xcmalloc(sizeof(*info), "asm_info"); + info->str = ""; + + in = sizeof(arch_output_regs)/sizeof(arch_output_regs[0]); + memcpy(&info->tmpl.rhs, arch_output_regs, sizeof(arch_output_regs)); + + def = new_triple(state, OP_ASM, &void_type, 0, in); + def->u.ainfo = info; + def->id |= TRIPLE_FLAG_VOLATILE; + return def; } -static void decl(struct compile_state *state, struct triple *first) +static void join_functions(struct compile_state *state) { - struct type *base_type, *type; - struct hash_entry *ident; - struct triple *def; - int global; - global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH); - base_type = decl_specifiers(state); - ident = 0; - type = declarator(state, base_type, &ident, 0); - if (global && ident && (peek(state) == TOK_LBRACE)) { - /* function */ - def = function_definition(state, type); - symbol(state, ident, &ident->sym_ident, def, type); + struct triple *start, *end, *call, *in, *out, *func; + struct file_state file; + struct type *pnext, *param; + struct type *result_type, *args_type; + int idx; + + /* Be clear the functions have not been joined yet */ + state->functions_joined = 0; + + /* Dummy file state to get debug handing right */ + memset(&file, 0, sizeof(file)); + file.basename = ""; + file.line = 0; + file.report_line = 0; + file.report_name = file.basename; + file.prev = state->file; + state->file = &file; + state->function = ""; + + if (!state->main_function) { + error(state, 0, "No functions to compile\n"); } - else { - int done; - flatten(state, first, do_decl(state, type, ident)); - /* type or variable definition */ - do { - done = 1; - if (peek(state) == TOK_EQ) { - if (!ident) { - error(state, 0, "cannot assign to a type"); - } - eat(state, TOK_EQ); - flatten(state, first, - init_expr(state, - ident->sym_ident->def, - initializer(state, type))); - } - arrays_complete(state, type); - if (peek(state) == TOK_COMMA) { - eat(state, TOK_COMMA); - ident = 0; - type = declarator(state, base_type, &ident, 0); - flatten(state, first, do_decl(state, type, ident)); - done = 0; - } - } while(!done); - eat(state, TOK_SEMI); + + /* The type of arguments */ + args_type = state->main_function->type->right; + /* The return type without any specifiers */ + result_type = clone_type(0, state->main_function->type->left); + + + /* Verify the external arguments */ + if (registers_of(state, args_type) > ARCH_INPUT_REGS) { + error(state, state->main_function, + "Too many external input arguments"); + } + if (registers_of(state, result_type) > ARCH_OUTPUT_REGS) { + error(state, state->main_function, + "Too many external output arguments"); } -} -static void decls(struct compile_state *state) -{ - struct triple *list; - int tok; - list = label(state); - while(1) { - tok = peek(state); - if (tok == TOK_EOF) { - return; + /* Lay down the basic program structure */ + end = label(state); + start = label(state); + start = flatten(state, state->first, start); + end = flatten(state, state->first, end); + in = input_asm(state); + out = output_asm(state); + call = new_triple(state, OP_FCALL, result_type, -1, registers_of(state, args_type)); + MISC(call, 0) = state->main_function; + in = flatten(state, state->first, in); + call = flatten(state, state->first, call); + out = flatten(state, state->first, out); + + + /* Read the external input arguments */ + pnext = args_type; + idx = 0; + while(pnext && ((pnext->type & TYPE_MASK) != TYPE_VOID)) { + struct triple *expr; + param = pnext; + pnext = 0; + if ((param->type & TYPE_MASK) == TYPE_PRODUCT) { + pnext = param->right; + param = param->left; } - if (tok == TOK_SPACE) { - eat(state, TOK_SPACE); + if (registers_of(state, param) != 1) { + error(state, state->main_function, + "Arg: %d %s requires multiple registers", + idx + 1, param->field_ident->name); } - decl(state, list); - if (list->next != list) { - error(state, 0, "global variables not supported"); + expr = read_expr(state, LHS(in, idx)); + RHS(call, idx) = expr; + expr = flatten(state, call, expr); + use_triple(expr, call); + + idx++; + } + + + /* Write the external output arguments */ + pnext = result_type; + if ((pnext->type & TYPE_MASK) == TYPE_STRUCT) { + pnext = result_type->left; + } + for(idx = 0; idx < out->rhs; idx++) { + struct triple *expr; + param = pnext; + pnext = 0; + if (param && ((param->type & TYPE_MASK) == TYPE_PRODUCT)) { + pnext = param->right; + param = param->left; + } + if (param && ((param->type & TYPE_MASK) == TYPE_VOID)) { + param = 0; + } + if (param) { + if (registers_of(state, param) != 1) { + error(state, state->main_function, + "Result: %d %s requires multiple registers", + idx, param->field_ident->name); + } + expr = read_expr(state, call); + if ((result_type->type & TYPE_MASK) == TYPE_STRUCT) { + expr = deref_field(state, expr, param->field_ident); + } + } else { + expr = triple(state, OP_UNKNOWNVAL, &int_type, 0, 0); } + flatten(state, out, expr); + RHS(out, idx) = expr; + use_triple(expr, out); + } + + /* Allocate a dummy containing function */ + func = triple(state, OP_LIST, + new_type(TYPE_FUNCTION, &void_type, &void_type), 0, 0); + func->type->type_ident = lookup(state, "", 0); + RHS(func, 0) = state->first; + func->u.cval = 1; + + /* See which functions are called, and how often */ + mark_live_functions(state); + inline_functions(state, func); + walk_functions(state, insert_function, end); + + if (start->next != end) { + flatten(state, start, branch(state, end, 0)); } + + /* OK now the functions have been joined. */ + state->functions_joined = 1; + + /* Done now cleanup */ + state->file = file.prev; + state->function = 0; } /* * Data structurs for optimation. */ -static void do_use_block( - struct block *used, struct block_set **head, struct block *user, + +static int do_use_block( + struct block *used, struct block_set **head, struct block *user, int front) { struct block_set **ptr, *new; if (!used) - return; + return 0; if (!user) - return; + return 0; ptr = head; while(*ptr) { if ((*ptr)->member == user) { - return; + return 0; } ptr = &(*ptr)->next; } @@ -8777,11 +14905,14 @@ static void do_use_block( new->next = 0; *ptr = new; } + return 1; } -static void do_unuse_block( +static int do_unuse_block( struct block *used, struct block_set **head, struct block *unuser) { struct block_set *use, **ptr; + int count; + count = 0; ptr = head; while(*ptr) { use = *ptr; @@ -8789,25 +14920,43 @@ static void do_unuse_block( *ptr = use->next; memset(use, -1, sizeof(*use)); xfree(use); + count += 1; } else { ptr = &use->next; } } + return count; } static void use_block(struct block *used, struct block *user) { + int count; /* Append new to the head of the list, print_block * depends on this. */ - do_use_block(used, &used->use, user, 1); - used->users++; + count = do_use_block(used, &used->use, user, 1); + used->users += count; } static void unuse_block(struct block *used, struct block *unuser) { - do_unuse_block(used, &used->use, unuser); - used->users--; + int count; + count = do_unuse_block(used, &used->use, unuser); + used->users -= count; +} + +static void add_block_edge(struct block *block, struct block *edge, int front) +{ + int count; + count = do_use_block(block, &block->edges, edge, front); + block->edge_count += count; +} + +static void remove_block_edge(struct block *block, struct block *edge) +{ + int count; + count = do_unuse_block(block, &block->edges, edge); + block->edge_count -= count; } static void idom_block(struct block *idom, struct block *user) @@ -8850,49 +14999,28 @@ static void unipdomf_block(struct block *block, struct block *unipdomf) do_unuse_block(block, &block->ipdomfrontier, unipdomf); } - - -static int do_walk_triple(struct compile_state *state, - struct triple *ptr, int depth, - int (*cb)(struct compile_state *state, struct triple *ptr, int depth)) +static int walk_triples( + struct compile_state *state, + int (*cb)(struct compile_state *state, struct triple *ptr, void *arg), + void *arg) { + struct triple *ptr; int result; - result = cb(state, ptr, depth); - if ((result == 0) && (ptr->op == OP_LIST)) { - struct triple *list; - list = ptr; - ptr = RHS(list, 0); - do { - result = do_walk_triple(state, ptr, depth + 1, cb); - if (ptr->next->prev != ptr) { - internal_error(state, ptr->next, "bad prev"); - } - ptr = ptr->next; - - } while((result == 0) && (ptr != RHS(list, 0))); - } + ptr = state->first; + do { + result = cb(state, ptr, arg); + if (ptr->next->prev != ptr) { + internal_error(state, ptr->next, "bad prev"); + } + ptr = ptr->next; + } while((result == 0) && (ptr != state->first)); return result; } -static int walk_triple( - struct compile_state *state, - struct triple *ptr, - int (*cb)(struct compile_state *state, struct triple *ptr, int depth)) -{ - return do_walk_triple(state, ptr, 0, cb); -} - -static void do_print_prefix(int depth) -{ - int i; - for(i = 0; i < depth; i++) { - printf(" "); - } -} - #define PRINT_LIST 1 -static int do_print_triple(struct compile_state *state, struct triple *ins, int depth) +static int do_print_triple(struct compile_state *state, struct triple *ins, void *arg) { + FILE *fp = arg; int op; op = ins->op; if (op == OP_LIST) { @@ -8901,36 +15029,28 @@ static int do_print_triple(struct compile_state *state, struct triple *ins, int #endif } if ((op == OP_LABEL) && (ins->use)) { - printf("\n%p:\n", ins); + fprintf(fp, "\n%p:\n", ins); } - do_print_prefix(depth); - display_triple(stdout, ins); + display_triple(fp, ins); - if ((ins->op == OP_BRANCH) && ins->use) { + if (triple_is_branch(state, ins) && ins->use && + (ins->op != OP_RET) && (ins->op != OP_FCALL)) { 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(state, ins)) { - printf("\n"); + fprintf(fp, "\n"); } return 0; } -static void print_triple(struct compile_state *state, struct triple *ins) -{ - walk_triple(state, ins, do_print_triple); -} - static void print_triples(struct compile_state *state) { - print_triple(state, state->main_function); + if (state->compiler->debug & DEBUG_TRIPLES) { + FILE *fp = state->dbgout; + fprintf(fp, "--------------- triples ---------------\n"); + walk_triples(state, do_print_triple, fp); + fprintf(fp, "\n"); + } } struct cf_block { @@ -8938,62 +15058,151 @@ struct cf_block { }; static void find_cf_blocks(struct cf_block *cf, struct block *block) { + struct block_set *edge; if (!block || (cf[block->vertex].block == block)) { return; } cf[block->vertex].block = block; - find_cf_blocks(cf, block->left); - find_cf_blocks(cf, block->right); + for(edge = block->edges; edge; edge = edge->next) { + find_cf_blocks(cf, edge->member); + } +} + +static void print_control_flow(struct compile_state *state, + FILE *fp, struct basic_blocks *bb) +{ + struct cf_block *cf; + int i; + fprintf(fp, "\ncontrol flow\n"); + cf = xcmalloc(sizeof(*cf) * (bb->last_vertex + 1), "cf_block"); + find_cf_blocks(cf, bb->first_block); + + for(i = 1; i <= bb->last_vertex; i++) { + struct block *block; + struct block_set *edge; + block = cf[i].block; + if (!block) + continue; + fprintf(fp, "(%p) %d:", block, block->vertex); + for(edge = block->edges; edge; edge = edge->next) { + fprintf(fp, " %d", edge->member->vertex); + } + fprintf(fp, "\n"); + } + + xfree(cf); +} + +static void free_basic_block(struct compile_state *state, struct block *block) +{ + struct block_set *edge, *entry; + struct block *child; + if (!block) { + return; + } + if (block->vertex == -1) { + return; + } + block->vertex = -1; + for(edge = block->edges; edge; edge = edge->next) { + if (edge->member) { + unuse_block(edge->member, block); + } + } + if (block->idom) { + unidom_block(block->idom, block); + } + block->idom = 0; + if (block->ipdom) { + unipdom_block(block->ipdom, block); + } + block->ipdom = 0; + while((entry = block->use)) { + child = entry->member; + unuse_block(block, child); + if (child && (child->vertex != -1)) { + for(edge = child->edges; edge; edge = edge->next) { + edge->member = 0; + } + } + } + while((entry = block->idominates)) { + child = entry->member; + unidom_block(block, child); + if (child && (child->vertex != -1)) { + child->idom = 0; + } + } + while((entry = block->domfrontier)) { + child = entry->member; + undomf_block(block, child); + } + while((entry = block->ipdominates)) { + child = entry->member; + unipdom_block(block, child); + if (child && (child->vertex != -1)) { + child->ipdom = 0; + } + } + while((entry = block->ipdomfrontier)) { + child = entry->member; + unipdomf_block(block, child); + } + if (block->users != 0) { + internal_error(state, 0, "block still has users"); + } + while((edge = block->edges)) { + child = edge->member; + remove_block_edge(block, child); + + if (child && (child->vertex != -1)) { + free_basic_block(state, child); + } + } + memset(block, -1, sizeof(*block)); +#ifndef WIN32 + xfree(block); +#endif } -static void print_control_flow(struct compile_state *state) +static void free_basic_blocks(struct compile_state *state, + struct basic_blocks *bb) { - struct cf_block *cf; - int i; - printf("\ncontrol flow\n"); - cf = xcmalloc(sizeof(*cf) * (state->last_vertex + 1), "cf_block"); - find_cf_blocks(cf, state->first_block); - - for(i = 1; i <= state->last_vertex; i++) { - struct block *block; - block = cf[i].block; - if (!block) - continue; - printf("(%p) %d:", block, block->vertex); - if (block->left) { - printf(" %d", block->left->vertex); - } - if (block->right && (block->right != block->left)) { - printf(" %d", block->right->vertex); + struct triple *first, *ins; + free_basic_block(state, bb->first_block); + bb->last_vertex = 0; + bb->first_block = bb->last_block = 0; + first = bb->first; + ins = first; + do { + if (triple_stores_block(state, ins)) { + ins->u.block = 0; } - printf("\n"); - } + ins = ins->next; + } while(ins != first); - xfree(cf); } - static struct block *basic_block(struct compile_state *state, - struct triple *first) + struct basic_blocks *bb, struct triple *first) { struct block *block; struct triple *ptr; - int op; - if (first->op != OP_LABEL) { - internal_error(state, 0, "block does not start with a label"); + if (!triple_is_label(state, first)) { + internal_error(state, first, "block does not start with a label"); } /* See if this basic block has already been setup */ if (first->u.block != 0) { return first->u.block; } /* Allocate another basic block structure */ - state->last_vertex += 1; + bb->last_vertex += 1; block = xcmalloc(sizeof(*block), "block"); block->first = block->last = first; - block->vertex = state->last_vertex; + block->vertex = bb->last_vertex; ptr = first; do { - if ((ptr != first) && (ptr->op == OP_LABEL) && ptr->use) { + if ((ptr != first) && triple_is_label(state, ptr) && (ptr->use)) { break; } block->last = ptr; @@ -9001,49 +15210,104 @@ static struct block *basic_block(struct compile_state *state, if (triple_stores_block(state, ptr)) { ptr->u.block = block; } - if (ptr->op == OP_BRANCH) { + if (triple_is_branch(state, ptr)) { break; } ptr = ptr->next; - } while (ptr != RHS(state->main_function, 0)); - if (ptr == RHS(state->main_function, 0)) - return block; - op = ptr->op; - if (op == OP_LABEL) { - block->left = basic_block(state, ptr); - block->right = 0; - use_block(block->left, block); - } - else if (op == OP_BRANCH) { - block->left = 0; - /* Trace the branch target */ - block->right = basic_block(state, TARG(ptr, 0)); - use_block(block->right, block); - /* If there is a test trace the branch as well */ - if (TRIPLE_RHS(ptr->sizes)) { - block->left = basic_block(state, ptr->next); - use_block(block->left, block); + } while (ptr != bb->first); + if ((ptr == bb->first) || + ((ptr->next == bb->first) && ( + triple_is_end(state, ptr) || + triple_is_ret(state, ptr)))) + { + /* The block has no outflowing edges */ + } + else if (triple_is_label(state, ptr)) { + struct block *next; + next = basic_block(state, bb, ptr); + add_block_edge(block, next, 0); + use_block(next, block); + } + else if (triple_is_branch(state, ptr)) { + struct triple **expr, *first; + struct block *child; + /* Find the branch targets. + * I special case the first branch as that magically + * avoids some difficult cases for the register allocator. + */ + expr = triple_edge_targ(state, ptr, 0); + if (!expr) { + internal_error(state, ptr, "branch without targets"); + } + first = *expr; + expr = triple_edge_targ(state, ptr, expr); + for(; expr; expr = triple_edge_targ(state, ptr, expr)) { + if (!*expr) continue; + child = basic_block(state, bb, *expr); + use_block(child, block); + add_block_edge(block, child, 0); + } + if (first) { + child = basic_block(state, bb, first); + use_block(child, block); + add_block_edge(block, child, 1); + + /* Be certain the return block of a call is + * in a basic block. When it is not find + * start of the block, insert a label if + * necessary and build the basic block. + * Then add a fake edge from the start block + * to the return block of the function. + */ + if (state->functions_joined && triple_is_call(state, ptr) + && !block_of_triple(state, MISC(ptr, 0))) { + struct block *tail; + struct triple *start; + start = triple_to_block_start(state, MISC(ptr, 0)); + if (!triple_is_label(state, start)) { + start = pre_triple(state, + start, OP_LABEL, &void_type, 0, 0); + } + tail = basic_block(state, bb, start); + add_block_edge(child, tail, 0); + use_block(tail, child); + } } } else { internal_error(state, 0, "Bad basic block split"); } +#if 0 +{ + struct block_set *edge; + FILE *fp = state->errout; + fprintf(fp, "basic_block: %10p [%2d] ( %10p - %10p )", + block, block->vertex, + block->first, block->last); + for(edge = block->edges; edge; edge = edge->next) { + fprintf(fp, " %10p [%2d]", + edge->member ? edge->member->first : 0, + edge->member ? edge->member->vertex : -1); + } + fprintf(fp, "\n"); +} +#endif return block; } -static void walk_blocks(struct compile_state *state, +static void walk_blocks(struct compile_state *state, struct basic_blocks *bb, void (*cb)(struct compile_state *state, struct block *block, void *arg), void *arg) { struct triple *ptr, *first; struct block *last_block; last_block = 0; - first = RHS(state->main_function, 0); + first = bb->first; ptr = first; do { - struct block *block; - if (ptr->op == OP_LABEL) { + if (triple_stores_block(state, ptr)) { + struct block *block; block = ptr->u.block; if (block && (block != last_block)) { cb(state, block, arg); @@ -9057,81 +15321,69 @@ 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, *edge; struct triple *ptr; FILE *fp = arg; - 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); + fprintf(fp, "\nblock: %p (%d) ", + block, + block->vertex); + + for(edge = block->edges; edge; edge = edge->next) { + fprintf(fp, " %p<-%p", + edge->member, + (edge->member && edge->member->use)? + edge->member->use->member : 0); + } + fprintf(fp, "\n"); if (block->first->op == OP_LABEL) { fprintf(fp, "%p:\n", block->first); } - for(ptr = block->first; ; ptr = ptr->next) { - struct triple_set *user; - int op = ptr->op; - - if (triple_stores_block(state, ptr)) { - 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); - } - } - } + for(ptr = block->first; ; ) { display_triple(fp, ptr); - -#if 0 - for(user = ptr->use; user; user = user->next) { - fprintf(fp, "use: %p\n", user->member); - } -#endif - - /* Sanity checks... */ - valid_ins(state, ptr); - for(user = ptr->use; user; user = user->next) { - struct triple *use; - use = user->member; - valid_ins(state, use); - if (triple_stores_block(state, user->member) && - !user->member->u.block) { - internal_error(state, user->member, - "Use %p not in a block?", - user->member); - } - } - if (ptr == block->last) break; + ptr = ptr->next; + if (ptr == block->first) { + internal_error(state, 0, "missing block last?"); + } } - fprintf(fp,"\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, FILE *fp) +static void romcc_print_blocks(struct compile_state *state, FILE *fp) { fprintf(fp, "--------------- blocks ---------------\n"); - walk_blocks(state, print_block, fp); + walk_blocks(state, &state->bb, print_block, fp); +} +static void print_blocks(struct compile_state *state, const char *func, FILE *fp) +{ + if (state->compiler->debug & DEBUG_BASIC_BLOCKS) { + fprintf(fp, "After %s\n", func); + romcc_print_blocks(state, fp); + if (state->compiler->debug & DEBUG_FDOMINATORS) { + print_dominators(state, fp, &state->bb); + print_dominance_frontiers(state, fp, &state->bb); + } + print_control_flow(state, fp, &state->bb); + } } -static void prune_nonblock_triples(struct compile_state *state) +static void prune_nonblock_triples(struct compile_state *state, + struct basic_blocks *bb) { struct block *block; struct triple *first, *ins, *next; /* Delete the triples not in a basic block */ - first = RHS(state->main_function, 0); block = 0; + first = bb->first; ins = first; do { next = ins->next; @@ -9139,124 +15391,72 @@ static void prune_nonblock_triples(struct compile_state *state) block = ins->u.block; } if (!block) { + struct triple_set *use; + for(use = ins->use; use; use = use->next) { + struct block *block; + block = block_of_triple(state, use->member); + if (block != 0) { + internal_error(state, ins, "pruning used ins?"); + } + } 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) +static void setup_basic_blocks(struct compile_state *state, + struct basic_blocks *bb) { - if (!triple_stores_block(state, RHS(state->main_function, 0)) || - !triple_stores_block(state, RHS(state->main_function,0)->prev)) { + if (!triple_stores_block(state, bb->first)) { internal_error(state, 0, "ins will not store block?"); } + /* Initialize the state */ + bb->first_block = bb->last_block = 0; + bb->last_vertex = 0; + free_basic_blocks(state, bb); + /* Find the basic blocks */ - state->last_vertex = 0; - 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 = 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, stdout); - print_control_flow(state); - } -} + bb->first_block = basic_block(state, bb, bb->first); -static void free_basic_block(struct compile_state *state, struct block *block) -{ - struct block_set *entry, *next; - struct block *child; - if (!block) { - return; - } - if (block->vertex == -1) { - return; - } - block->vertex = -1; - if (block->left) { - unuse_block(block->left, block); - } - if (block->right) { - unuse_block(block->right, block); - } - if (block->idom) { - unidom_block(block->idom, block); - } - block->idom = 0; - if (block->ipdom) { - unipdom_block(block->ipdom, block); - } - block->ipdom = 0; - for(entry = block->use; entry; entry = next) { - next = entry->next; - child = entry->member; - unuse_block(block, child); - if (child->left == block) { - child->left = 0; - } - if (child->right == block) { - child->right = 0; + /* Be certain the last instruction of a function, or the + * entire program is in a basic block. When it is not find + * the start of the block, insert a label if necessary and build + * basic block. Then add a fake edge from the start block + * to the final block. + */ + if (!block_of_triple(state, bb->first->prev)) { + struct triple *start; + struct block *tail; + start = triple_to_block_start(state, bb->first->prev); + if (!triple_is_label(state, start)) { + start = pre_triple(state, + start, OP_LABEL, &void_type, 0, 0); } + tail = basic_block(state, bb, start); + add_block_edge(bb->first_block, tail, 0); + use_block(tail, bb->first_block); } - for(entry = block->idominates; entry; entry = next) { - next = entry->next; - child = entry->member; - unidom_block(block, child); - child->idom = 0; - } - for(entry = block->domfrontier; entry; entry = next) { - next = entry->next; - child = entry->member; - undomf_block(block, child); - } - for(entry = block->ipdominates; entry; entry = next) { - next = entry->next; - child = entry->member; - unipdom_block(block, child); - child->ipdom = 0; - } - for(entry = block->ipdomfrontier; entry; entry = next) { - next = entry->next; - child = entry->member; - unipdomf_block(block, child); - } - if (block->users != 0) { - internal_error(state, 0, "block still has users"); + + /* Find the last basic block. + */ + bb->last_block = block_of_triple(state, bb->first->prev); + + /* Delete the triples not in a basic block */ + prune_nonblock_triples(state, bb); + +#if 0 + /* If we are debugging print what I have just done */ + if (state->compiler->debug & DEBUG_BASIC_BLOCKS) { + print_blocks(state, state->dbgout); + print_control_flow(state, bb); } - free_basic_block(state, block->left); - block->left = 0; - free_basic_block(state, block->right); - block->right = 0; - memset(block, -1, sizeof(*block)); - xfree(block); +#endif } -static void free_basic_blocks(struct compile_state *state) -{ - struct triple *first, *ins; - free_basic_block(state, state->first_block); - state->last_vertex = 0; - state->first_block = state->last_block = 0; - first = RHS(state->main_function, 0); - ins = first; - do { - if (triple_stores_block(state, ins)) { - ins->u.block = 0; - } - ins = ins->next; - } while(ins != first); - -} struct sdom_block { struct block *block; @@ -9299,6 +15499,7 @@ static void sdom_block(struct sdom_block *sdom, struct sdom_block *block) static int initialize_sdblock(struct sdom_block *sd, struct block *parent, struct block *block, int vertex) { + struct block_set *edge; if (!block || (sd[block->vertex].block == block)) { return vertex; } @@ -9311,12 +15512,14 @@ static int initialize_sdblock(struct sdom_block *sd, sd[vertex].parent = parent? &sd[parent->vertex] : 0; sd[vertex].ancestor = 0; sd[vertex].vertex = vertex; - vertex = initialize_sdblock(sd, block, block->left, vertex); - vertex = initialize_sdblock(sd, block, block->right, vertex); + for(edge = block->edges; edge; edge = edge->next) { + vertex = initialize_sdblock(sd, block, edge->member, vertex); + } return vertex; } -static int initialize_sdpblock(struct sdom_block *sd, +static int initialize_spdblock( + struct compile_state *state, struct sdom_block *sd, struct block *parent, struct block *block, int vertex) { struct block_set *user; @@ -9333,7 +15536,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_spdblock(state, sd, block, user->member, vertex); + } + return vertex; +} + +static int setup_spdblocks(struct compile_state *state, + struct basic_blocks *bb, struct sdom_block *sd) +{ + struct block *block; + int vertex; + /* Setup as many sdpblocks as possible without using fake edges */ + vertex = initialize_spdblock(state, sd, 0, bb->last_block, 0); + + /* Walk through the graph and find unconnected blocks. Add a + * fake edge from the unconnected blocks to the end of the + * graph. + */ + block = bb->first_block->last->next->u.block; + for(; block && block != bb->first_block; block = block->last->next->u.block) { + if (sd[block->vertex].block == block) { + continue; + } +#if DEBUG_SDP_BLOCKS + { + FILE *fp = state->errout; + fprintf(fp, "Adding %d\n", vertex +1); + } +#endif + add_block_edge(block, bb->last_block, 0); + use_block(bb->last_block, block); + + vertex = initialize_spdblock(state, sd, bb->last_block, block, vertex); } return vertex; } @@ -9361,15 +15595,16 @@ static void compress_ancestors(struct sdom_block *v) } } -static void compute_sdom(struct compile_state *state, struct sdom_block *sd) +static void compute_sdom(struct compile_state *state, + struct basic_blocks *bb, struct sdom_block *sd) { int i; - /* // step 2 + /* // step 2 * for each v <= pred(w) { * u = EVAL(v); - * if (semi[u] < semi[w] { - * semi[w] = semi[u]; - * } + * if (semi[u] < semi[w] { + * semi[w] = semi[u]; + * } * } * add w to bucket(vertex(semi[w])); * LINK(parent(w), w); @@ -9381,7 +15616,7 @@ static void compute_sdom(struct compile_state *state, struct sdom_block *sd) * dom(v) = (semi[u] < semi[v]) ? u : parent(w); * } */ - for(i = state->last_vertex; i >= 2; i--) { + for(i = bb->last_vertex; i >= 2; i--) { struct sdom_block *v, *parent, *next; struct block_set *user; struct block *block; @@ -9404,21 +15639,22 @@ static void compute_sdom(struct compile_state *state, struct sdom_block *sd) next = v->sdom_next; unsdom_block(v); u = (!v->ancestor) ? v : (compress_ancestors(v), v->label); - v->block->idom = (u->sdom->vertex < v->sdom->vertex)? + v->block->idom = (u->sdom->vertex < v->sdom->vertex)? u->block : parent->block; } } } -static void compute_spdom(struct compile_state *state, struct sdom_block *sd) +static void compute_spdom(struct compile_state *state, + struct basic_blocks *bb, struct sdom_block *sd) { int i; - /* // step 2 + /* // step 2 * for each v <= pred(w) { * u = EVAL(v); - * if (semi[u] < semi[w] { - * semi[w] = semi[u]; - * } + * if (semi[u] < semi[w] { + * semi[w] = semi[u]; + * } * } * add w to bucket(vertex(semi[w])); * LINK(parent(w), w); @@ -9430,21 +15666,15 @@ static void compute_spdom(struct compile_state *state, struct sdom_block *sd) * dom(v) = (semi[u] < semi[v]) ? u : parent(w); * } */ - for(i = state->last_vertex; i >= 2; i--) { + for(i = bb->last_vertex; i >= 2; i--) { struct sdom_block *u, *v, *parent, *next; + struct block_set *edge; struct block *block; block = sd[i].block; parent = sd[i].parent; /* Step 2 */ - if (block->left) { - v = &sd[block->left->vertex]; - u = !(v->ancestor)? v : (compress_ancestors(v), v->label); - if (u->sdom->vertex < sd[i].sdom->vertex) { - sd[i].sdom = u->sdom; - } - } - if (block->right && (block->right != block->left)) { - v = &sd[block->right->vertex]; + for(edge = block->edges; edge; edge = edge->next) { + v = &sd[edge->member->vertex]; u = !(v->ancestor)? v : (compress_ancestors(v), v->label); if (u->sdom->vertex < sd[i].sdom->vertex) { sd[i].sdom = u->sdom; @@ -9458,16 +15688,17 @@ static void compute_spdom(struct compile_state *state, struct sdom_block *sd) next = v->sdom_next; unsdom_block(v); u = (!v->ancestor) ? v : (compress_ancestors(v), v->label); - v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)? + v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)? u->block : parent->block; } } } -static void compute_idom(struct compile_state *state, struct sdom_block *sd) +static void compute_idom(struct compile_state *state, + struct basic_blocks *bb, struct sdom_block *sd) { int i; - for(i = 2; i <= state->last_vertex; i++) { + for(i = 2; i <= bb->last_vertex; i++) { struct block *block; block = sd[i].block; if (block->idom->vertex != sd[i].sdom->vertex) { @@ -9478,10 +15709,11 @@ static void compute_idom(struct compile_state *state, struct sdom_block *sd) sd[1].block->idom = 0; } -static void compute_ipdom(struct compile_state *state, struct sdom_block *sd) +static void compute_ipdom(struct compile_state *state, + struct basic_blocks *bb, struct sdom_block *sd) { int i; - for(i = 2; i <= state->last_vertex; i++) { + for(i = 2; i <= bb->last_vertex; i++) { struct block *block; block = sd[i].block; if (block->ipdom->vertex != sd[i].sdom->vertex) { @@ -9494,13 +15726,13 @@ static void compute_ipdom(struct compile_state *state, struct sdom_block *sd) /* Theorem 1: * Every vertex of a flowgraph G = (V, E, r) except r has - * a unique immediate dominator. + * a unique immediate dominator. * The edges {(idom(w), w) |w <= V - {r}} form a directed tree - * rooted at r, called the dominator tree of G, such that + * rooted at r, called the dominator tree of G, such that * v dominates w if and only if v is a proper ancestor of w in * the dominator tree. */ - /* Lemma 1: + /* Lemma 1: * If v and w are vertices of G such that v <= w, * than any path from v to w must contain a common ancestor * of v and w in T. @@ -9513,7 +15745,7 @@ static void compute_ipdom(struct compile_state *state, struct sdom_block *sd) * sdom(u) >= sdom(w). Then idom(w) = sdom(w). */ /* Theorem 3: - * Let w != r and let u be a vertex for which sdom(u) is + * Let w != r and let u be a vertex for which sdom(u) is * minimum amoung vertices u satisfying sdom(w) -> u -> w. * Then sdom(u) <= sdom(w) and idom(u) = idom(w). */ @@ -9521,7 +15753,8 @@ static void compute_ipdom(struct compile_state *state, struct sdom_block *sd) * Then v -> idom(w) or idom(w) -> idom(v) */ -static void find_immediate_dominators(struct compile_state *state) +static void find_immediate_dominators(struct compile_state *state, + struct basic_blocks *bb) { struct sdom_block *sd; /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that: @@ -9530,11 +15763,11 @@ static void find_immediate_dominators(struct compile_state *state) /* Theorem 4: * For any vertex w != r. * sdom(w) = min( - * {v|(v,w) <= E and v < w } U + * {v|(v,w) <= E and v < w } U * {sdom(u) | u > w and there is an edge (v, w) such that u -> v}) */ /* Corollary 1: - * Let w != r and let u be a vertex for which sdom(u) is + * Let w != r and let u be a vertex for which sdom(u) is * minimum amoung vertices u satisfying sdom(w) -> u -> w. * Then: * { sdom(w) if sdom(w) = sdom(u), @@ -9542,7 +15775,7 @@ static void find_immediate_dominators(struct compile_state *state) * { idom(u) otherwise */ /* The algorithm consists of the following 4 steps. - * Step 1. Carry out a depth-first search of the problem graph. + * Step 1. Carry out a depth-first search of the problem graph. * Number the vertices from 1 to N as they are reached during * the search. Initialize the variables used in succeeding steps. * Step 2. Compute the semidominators of all vertices by applying @@ -9555,8 +15788,8 @@ static void find_immediate_dominators(struct compile_state *state) * by number. */ /* Step 1 initialize the basic block information */ - sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state"); - initialize_sdblock(sd, 0, state->first_block, 0); + sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state"); + initialize_sdblock(sd, 0, bb->first_block, 0); #if 0 sd[1].size = 0; sd[1].label = 0; @@ -9564,25 +15797,31 @@ static void find_immediate_dominators(struct compile_state *state) #endif /* Step 2 compute the semidominators */ /* Step 3 implicitly define the immediate dominator of each vertex */ - compute_sdom(state, sd); + compute_sdom(state, bb, sd); /* Step 4 explicitly define the immediate dominator of each vertex */ - compute_idom(state, sd); + compute_idom(state, bb, sd); xfree(sd); } -static void find_post_dominators(struct compile_state *state) +static void find_post_dominators(struct compile_state *state, + struct basic_blocks *bb) { struct sdom_block *sd; + int vertex; /* Step 1 initialize the basic block information */ - sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state"); + sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state"); - initialize_sdpblock(sd, 0, state->last_block, 0); + vertex = setup_spdblocks(state, bb, sd); + if (vertex != bb->last_vertex) { + internal_error(state, 0, "missing %d blocks", + bb->last_vertex - vertex); + } /* Step 2 compute the semidominators */ /* Step 3 implicitly define the immediate dominator of each vertex */ - compute_spdom(state, sd); + compute_spdom(state, bb, sd); /* Step 4 explicitly define the immediate dominator of each vertex */ - compute_ipdom(state, sd); + compute_ipdom(state, bb, sd); xfree(sd); } @@ -9591,7 +15830,7 @@ static void find_post_dominators(struct compile_state *state) static void find_block_domf(struct compile_state *state, struct block *block) { struct block *child; - struct block_set *user; + struct block_set *user, *edge; if (block->domfrontier != 0) { internal_error(state, block->first, "domfrontier present?"); } @@ -9602,11 +15841,10 @@ static void find_block_domf(struct compile_state *state, struct block *block) } find_block_domf(state, child); } - if (block->left && block->left->idom != block) { - domf_block(block, block->left); - } - if (block->right && block->right->idom != block) { - domf_block(block, block->right); + for(edge = block->edges; edge; edge = edge->next) { + if (edge->member->idom != block) { + domf_block(block, edge->member); + } } for(user = block->idominates; user; user = user->next) { struct block_set *frontier; @@ -9633,13 +15871,12 @@ static void find_block_ipdomf(struct compile_state *state, struct block *block) } find_block_ipdomf(state, child); } - if (block->left && block->left->ipdom != block) { - ipdomf_block(block, block->left); - } - if (block->right && block->right->ipdom != block) { - ipdomf_block(block, block->right); + for(user = block->use; user; user = user->next) { + if (user->member->ipdom != block) { + ipdomf_block(block, user->member); + } } - for(user = block->idominates; user; user = user->next) { + for(user = block->ipdominates; user; user = user->next) { struct block_set *frontier; child = user->member; for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) { @@ -9666,56 +15903,98 @@ static void print_dominated( fprintf(fp,"\n"); } -static void print_dominators(struct compile_state *state, FILE *fp) +static void print_dominated2( + struct compile_state *state, FILE *fp, int depth, struct block *block) +{ + struct block_set *user; + struct triple *ins; + struct occurance *ptr, *ptr2; + const char *filename1, *filename2; + int equal_filenames; + int i; + for(i = 0; i < depth; i++) { + fprintf(fp, " "); + } + fprintf(fp, "%3d: %p (%p - %p) @", + block->vertex, block, block->first, block->last); + ins = block->first; + while(ins != block->last && (ins->occurance->line == 0)) { + ins = ins->next; + } + ptr = ins->occurance; + ptr2 = block->last->occurance; + filename1 = ptr->filename? ptr->filename : ""; + filename2 = ptr2->filename? ptr2->filename : ""; + equal_filenames = (strcmp(filename1, filename2) == 0); + if ((ptr == ptr2) || (equal_filenames && ptr->line == ptr2->line)) { + fprintf(fp, " %s:%d", ptr->filename, ptr->line); + } else if (equal_filenames) { + fprintf(fp, " %s:(%d - %d)", + ptr->filename, ptr->line, ptr2->line); + } else { + fprintf(fp, " (%s:%d - %s:%d)", + ptr->filename, ptr->line, + ptr2->filename, ptr2->line); + } + fprintf(fp, "\n"); + for(user = block->idominates; user; user = user->next) { + print_dominated2(state, fp, depth + 1, user->member); + } +} + +static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb) { fprintf(fp, "\ndominates\n"); - walk_blocks(state, print_dominated, fp); + walk_blocks(state, bb, print_dominated, fp); + fprintf(fp, "dominates\n"); + print_dominated2(state, fp, 0, bb->first_block); } static int print_frontiers( - struct compile_state *state, struct block *block, int vertex) + struct compile_state *state, FILE *fp, struct block *block, int vertex) { - struct block_set *user; + struct block_set *user, *edge; if (!block || (block->vertex != vertex + 1)) { return vertex; } vertex += 1; - printf("%d:", block->vertex); + fprintf(fp, "%d:", block->vertex); for(user = block->domfrontier; user; user = user->next) { - printf(" %d", user->member->vertex); + fprintf(fp, " %d", user->member->vertex); } - printf("\n"); + fprintf(fp, "\n"); - vertex = print_frontiers(state, block->left, vertex); - vertex = print_frontiers(state, block->right, vertex); + for(edge = block->edges; edge; edge = edge->next) { + vertex = print_frontiers(state, fp, edge->member, vertex); + } return vertex; } -static void print_dominance_frontiers(struct compile_state *state) +static void print_dominance_frontiers(struct compile_state *state, + FILE *fp, struct basic_blocks *bb) { - printf("\ndominance frontiers\n"); - print_frontiers(state, state->first_block, 0); - + fprintf(fp, "\ndominance frontiers\n"); + print_frontiers(state, fp, bb->first_block, 0); + } -static void analyze_idominators(struct compile_state *state) +static void analyze_idominators(struct compile_state *state, struct basic_blocks *bb) { /* Find the immediate dominators */ - find_immediate_dominators(state); + find_immediate_dominators(state, bb); /* Find the dominance frontiers */ - find_block_domf(state, state->first_block); + find_block_domf(state, bb->first_block); /* If debuging print the print what I have just found */ - if (state->debug & DEBUG_FDOMINATORS) { - print_dominators(state, stdout); - print_dominance_frontiers(state); - print_control_flow(state); + if (state->compiler->debug & DEBUG_FDOMINATORS) { + print_dominators(state, state->dbgout, bb); + print_dominance_frontiers(state, state->dbgout, bb); + print_control_flow(state, state->dbgout, bb); } } - static void print_ipdominated( struct compile_state *state, struct block *block, void *arg) { @@ -9732,14 +16011,15 @@ static void print_ipdominated( fprintf(fp, "\n"); } -static void print_ipdominators(struct compile_state *state, FILE *fp) +static void print_ipdominators(struct compile_state *state, FILE *fp, + struct basic_blocks *bb) { fprintf(fp, "\nipdominates\n"); - walk_blocks(state, print_ipdominated, fp); + walk_blocks(state, bb, print_ipdominated, fp); } static int print_pfrontiers( - struct compile_state *state, struct block *block, int vertex) + struct compile_state *state, FILE *fp, struct block *block, int vertex) { struct block_set *user; @@ -9748,34 +16028,36 @@ static int print_pfrontiers( } vertex += 1; - printf("%d:", block->vertex); + fprintf(fp, "%d:", block->vertex); for(user = block->ipdomfrontier; user; user = user->next) { - printf(" %d", user->member->vertex); + fprintf(fp, " %d", user->member->vertex); } - printf("\n"); + fprintf(fp, "\n"); for(user = block->use; user; user = user->next) { - vertex = print_pfrontiers(state, user->member, vertex); + vertex = print_pfrontiers(state, fp, user->member, vertex); } return vertex; } -static void print_ipdominance_frontiers(struct compile_state *state) +static void print_ipdominance_frontiers(struct compile_state *state, + FILE *fp, struct basic_blocks *bb) { - printf("\nipdominance frontiers\n"); - print_pfrontiers(state, state->last_block, 0); - + fprintf(fp, "\nipdominance frontiers\n"); + print_pfrontiers(state, fp, bb->last_block, 0); + } -static void analyze_ipdominators(struct compile_state *state) +static void analyze_ipdominators(struct compile_state *state, + struct basic_blocks *bb) { /* Find the post dominators */ - find_post_dominators(state); + find_post_dominators(state, bb); /* Find the control dependencies (post dominance frontiers) */ - find_block_ipdomf(state, state->last_block); + find_block_ipdomf(state, bb->last_block); /* If debuging print the print what I have just found */ - if (state->debug & DEBUG_RDOMINATORS) { - print_ipdominators(state, stdout); - print_ipdominance_frontiers(state); - print_control_flow(state); + if (state->compiler->debug & DEBUG_RDOMINATORS) { + print_ipdominators(state, state->dbgout, bb); + print_ipdominance_frontiers(state, state->dbgout, bb); + print_control_flow(state, state->dbgout, bb); } } @@ -9797,9 +16079,12 @@ static int tdominates(struct compile_state *state, bsub = block_of_triple(state, sub); if (bdom != bsub) { result = bdominates(state, bdom, bsub); - } + } else { struct triple *ins; + if (!bdom || !bsub) { + internal_error(state, dom, "huh?"); + } ins = sub; while((ins != bsub->first) && (ins != dom)) { ins = ins->prev; @@ -9809,6 +16094,14 @@ static int tdominates(struct compile_state *state, return result; } +static void analyze_basic_blocks( + struct compile_state *state, struct basic_blocks *bb) +{ + setup_basic_blocks(state, bb); + analyze_idominators(state, bb); + analyze_ipdominators(state, bb); +} + static void insert_phi_operations(struct compile_state *state) { size_t size; @@ -9816,34 +16109,43 @@ 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); + size = sizeof(int) * (state->bb.last_vertex + 1); has_already = xcmalloc(size, "has_already"); work = xcmalloc(size, "work"); iter = 0; - first = RHS(state->main_function, 0); - for(var = first->next; var != first ; var = var->next) { + first = state->first; + for(var = first->next; var != first ; var = vnext) { struct block *block; - struct triple_set *user; - if ((var->op != OP_ADECL) || !var->use) { + struct triple_set *user, *unext; + vnext = var->next; + + if (!triple_is_auto_var(state, var) || !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 (MISC(var, 0) == user->member) { + continue; + } if (user->member->op == OP_READ) { continue; } if (user->member->op != OP_WRITE) { - internal_error(state, user->member, + internal_error(state, user->member, "bad variable access"); } 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; @@ -9867,34 +16169,132 @@ 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 */ + get_occurance(var->occurance); phi = alloc_triple( - state, OP_PHI, var->type, -1, in_edges, - front->first->filename, - front->first->line, - front->first->col); + state, OP_PHI, var->type, -1, in_edges, + var->occurance); phi->u.block = front; MISC(phi, 0) = var; use_triple(var, phi); +#if 1 + if (phi->rhs != in_edges) { + internal_error(state, phi, "phi->rhs: %d != in_edges: %d", + phi->rhs, in_edges); + } +#endif /* Insert the phi functions immediately after the label */ insert_triple(state, front->first->next, phi); if (front->first == front->last) { front->last = front->first->next; } has_already[front->vertex] = iter; + transform_to_arch_instruction(state, phi); + + /* If necessary plan to visit the basic block */ + if (work[front->vertex] >= iter) { + continue; + } + work[front->vertex] = iter; + *work_list_tail = front; + front->work_next = 0; + work_list_tail = &front->work_next; + } + } + } + xfree(has_already); + xfree(work); +} + + +struct stack { + struct triple_set *top; + unsigned orig_id; +}; + +static int count_auto_vars(struct compile_state *state) +{ + struct triple *first, *ins; + int auto_vars = 0; + first = state->first; + ins = first; + do { + if (triple_is_auto_var(state, ins)) { + auto_vars += 1; + } + ins = ins->next; + } while(ins != first); + return auto_vars; +} + +static void number_auto_vars(struct compile_state *state, struct stack *stacks) +{ + struct triple *first, *ins; + int auto_vars = 0; + first = state->first; + ins = first; + do { + if (triple_is_auto_var(state, ins)) { + auto_vars += 1; + stacks[auto_vars].orig_id = ins->id; + ins->id = auto_vars; + } + ins = ins->next; + } while(ins != first); +} + +static void restore_auto_vars(struct compile_state *state, struct stack *stacks) +{ + struct triple *first, *ins; + first = state->first; + ins = first; + do { + if (triple_is_auto_var(state, ins)) { + ins->id = stacks[ins->id].orig_id; + } + ins = ins->next; + } while(ins != first); +} - /* If necessary plan to visit the basic block */ - if (work[front->vertex] >= iter) { - continue; - } - work[front->vertex] = iter; - *work_list_tail = front; - front->work_next = 0; - work_list_tail = &front->work_next; - } +static struct triple *peek_triple(struct stack *stacks, struct triple *var) +{ + struct triple_set *head; + struct triple *top_val; + top_val = 0; + head = stacks[var->id].top; + if (head) { + top_val = head->member; + } + return top_val; +} + +static void push_triple(struct stack *stacks, struct triple *var, struct triple *val) +{ + struct triple_set *new; + /* Append new to the head of the list, + * it's the only sensible behavoir for a stack. + */ + new = xcmalloc(sizeof(*new), "triple_set"); + new->member = val; + new->next = stacks[var->id].top; + stacks[var->id].top = new; +} + +static void pop_triple(struct stack *stacks, struct triple *var, struct triple *oldval) +{ + struct triple_set *set, **ptr; + ptr = &stacks[var->id].top; + while(*ptr) { + set = *ptr; + if (set->member == oldval) { + *ptr = set->next; + xfree(set); + /* Only free one occurance from the stack */ + return; + } + else { + ptr = &set->next; } } - xfree(has_already); - xfree(work); } /* @@ -9902,7 +16302,7 @@ static void insert_phi_operations(struct compile_state *state) * S(V) */ static void fixup_block_phi_variables( - struct compile_state *state, struct block *parent, struct block *block) + struct compile_state *state, struct stack *stacks, struct block *parent, struct block *block) { struct block_set *set; struct triple *ptr; @@ -9927,11 +16327,11 @@ static void fixup_block_phi_variables( 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)) { + val = peek_triple(stacks, var); + if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) { internal_error(state, val, "bad value in phi"); } - if (edge >= TRIPLE_RHS(ptr->sizes)) { + if (edge >= ptr->rhs) { internal_error(state, ptr, "edges > phi rhs"); } slot = &RHS(ptr, edge); @@ -9949,9 +16349,9 @@ static void fixup_block_phi_variables( static void rename_block_variables( - struct compile_state *state, struct block *block) + struct compile_state *state, struct stack *stacks, struct block *block) { - struct block_set *user; + struct block_set *user, *edge; struct triple *ptr, *next, *last; int done; if (!block) @@ -9967,12 +16367,28 @@ static void rename_block_variables( if (ptr->op == OP_READ) { struct triple *var, *val; var = RHS(ptr, 0); + if (!triple_is_auto_var(state, var)) { + internal_error(state, ptr, "read of non auto var!"); + } unuse_triple(var, ptr); - if (!var->use) { + /* Find the current value of the variable */ + val = peek_triple(stacks, var); + if (!val) { + /* Let the optimizer at variables that are not initially + * set. But give it a bogus value so things seem to + * work by accident. This is useful for bitfields because + * setting them always involves a read-modify-write. + */ + if (TYPE_ARITHMETIC(ptr->type->type)) { + val = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0); + val->u.cval = 0xdeadbeaf; + } else { + val = pre_triple(state, ptr, OP_UNKNOWNVAL, ptr->type, 0, 0); + } + } + if (!val) { error(state, ptr, "variable used without being set"); } - /* 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 read"); } @@ -9982,33 +16398,56 @@ static void rename_block_variables( } /* LHS(A) */ if (ptr->op == OP_WRITE) { - struct triple *var, *val; - var = LHS(ptr, 0); - val = RHS(ptr, 0); - if ((val->op == OP_WRITE) || (val->op == OP_READ)) { - internal_error(state, val, "bad value in write"); + struct triple *var, *val, *tval; + var = MISC(ptr, 0); + if (!triple_is_auto_var(state, var)) { + internal_error(state, ptr, "write to non auto var!"); } - propogate_use(state, ptr, val); + tval = val = RHS(ptr, 0); + if ((val->op == OP_WRITE) || (val->op == OP_READ) || + triple_is_auto_var(state, val)) { + internal_error(state, ptr, "bad value in write"); + } + /* Insert a cast if the types differ */ + if (!is_subset_type(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_CONVERT, ptr->type, val, 0); + use_triple(val, tval); + } + transform_to_arch_instruction(state, tval); + unuse_triple(val, ptr); + RHS(ptr, 0) = 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(stacks, var, tval); } if (ptr->op == OP_PHI) { struct triple *var; var = MISC(ptr, 0); + if (!triple_is_auto_var(state, var)) { + internal_error(state, ptr, "phi references non auto var!"); + } /* Push OP_PHI onto a stack of variable uses */ - push_triple(var, ptr); + push_triple(stacks, var, ptr); } last = ptr; } block->last = last; /* Fixup PHI functions in the cf successors */ - fixup_block_phi_variables(state, block, block->left); - fixup_block_phi_variables(state, block, block->right); + for(edge = block->edges; edge; edge = edge->next) { + fixup_block_phi_variables(state, stacks, block, edge->member); + } /* rename variables in the dominated nodes */ for(user = block->idominates; user; user = user->next) { - rename_block_variables(state, user->member); + rename_block_variables(state, stacks, user->member); } /* pop the renamed variable stack */ last = block->first; @@ -10020,9 +16459,9 @@ static void rename_block_variables( } if (ptr->op == OP_WRITE) { struct triple *var; - var = LHS(ptr, 0); + var = MISC(ptr, 0); /* Pop OP_WRITE ptr->right from the stack of variable uses */ - pop_triple(var, RHS(ptr, 0)); + pop_triple(stacks, var, RHS(ptr, 0)); release_triple(state, ptr); continue; } @@ -10030,32 +16469,58 @@ static void rename_block_variables( struct triple *var; var = MISC(ptr, 0); /* Pop OP_WRITE ptr->right from the stack of variable uses */ - pop_triple(var, ptr); + pop_triple(stacks, var, ptr); } last = ptr; } block->last = last; } +static void rename_variables(struct compile_state *state) +{ + struct stack *stacks; + int auto_vars; + + /* Allocate stacks for the Variables */ + auto_vars = count_auto_vars(state); + stacks = xcmalloc(sizeof(stacks[0])*(auto_vars + 1), "auto var stacks"); + + /* Give each auto_var a stack */ + number_auto_vars(state, stacks); + + /* Rename the variables */ + rename_block_variables(state, stacks, state->bb.first_block); + + /* Remove the stacks from the auto_vars */ + restore_auto_vars(state, stacks); + xfree(stacks); +} + static void prune_block_variables(struct compile_state *state, struct block *block) { struct block_set *user; - struct triple *next, *last, *ptr; + struct triple *next, *ptr; int done; - last = block->first; + done = 0; for(ptr = block->first; !done; ptr = next) { + /* Be extremely careful I am deleting the list + * as I walk trhough it. + */ next = ptr->next; if (ptr == block->last) { done = 1; } - if (ptr->op == OP_ADECL) { + if (triple_is_auto_var(state, ptr)) { struct triple_set *user, *next; for(user = ptr->use; user; user = next) { struct triple *use; next = user->next; use = user->member; + if (MISC(ptr, 0) == user->member) { + continue; + } if (use->op != OP_PHI) { internal_error(state, use, "decl still used"); } @@ -10065,33 +16530,144 @@ static void prune_block_variables(struct compile_state *state, unuse_triple(ptr, use); MISC(use, 0) = 0; } - release_triple(state, ptr); + if ((ptr->u.cval == 0) && (MISC(ptr, 0)->lhs == 1)) { + /* Delete the adecl */ + release_triple(state, MISC(ptr, 0)); + /* And the piece */ + release_triple(state, ptr); + } continue; } - last = ptr; } - block->last = last; for(user = block->idominates; user; user = user->next) { prune_block_variables(state, user->member); } } -static void transform_to_ssa_form(struct compile_state *state) +struct phi_triple { + struct triple *phi; + unsigned orig_id; + int alive; +}; + +static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi) { - insert_phi_operations(state); + struct triple **slot; + int zrhs, i; + if (live[phi->id].alive) { + return; + } + live[phi->id].alive = 1; + zrhs = phi->rhs; + slot = &RHS(phi, 0); + for(i = 0; i < zrhs; i++) { + struct triple *used; + used = slot[i]; + if (used && (used->op == OP_PHI)) { + keep_phi(state, live, used); + } + } +} + +static void prune_unused_phis(struct compile_state *state) +{ + struct triple *first, *phi; + struct phi_triple *live; + int phis, i; + + /* Find the first instruction */ + first = state->first; + + /* Count how many phi functions I need to process */ + phis = 0; + for(phi = first->next; phi != first; phi = phi->next) { + if (phi->op == OP_PHI) { + phis += 1; + } + } + + /* Mark them all dead */ + live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple"); + phis = 0; + for(phi = first->next; phi != first; phi = phi->next) { + if (phi->op != OP_PHI) { + continue; + } + live[phis].alive = 0; + live[phis].orig_id = phi->id; + live[phis].phi = phi; + phi->id = phis; + phis += 1; + } + + /* Mark phis alive that are used by non phis */ + for(i = 0; i < phis; i++) { + struct triple_set *set; + for(set = live[i].phi->use; !live[i].alive && set; set = set->next) { + if (set->member->op != OP_PHI) { + keep_phi(state, live, live[i].phi); + break; + } + } + } + + /* Delete the extraneous phis */ + for(i = 0; i < phis; i++) { + struct triple **slot; + int zrhs, j; + if (!live[i].alive) { + release_triple(state, live[i].phi); + continue; + } + phi = live[i].phi; + slot = &RHS(phi, 0); + zrhs = phi->rhs; + for(j = 0; j < zrhs; j++) { + if(!slot[j]) { + struct triple *unknown; + get_occurance(phi->occurance); + unknown = flatten(state, state->global_pool, + alloc_triple(state, OP_UNKNOWNVAL, + phi->type, 0, 0, phi->occurance)); + slot[j] = unknown; + use_triple(unknown, phi); + transform_to_arch_instruction(state, unknown); #if 0 - printf("@%s:%d\n", __FILE__, __LINE__); - print_blocks(state, stdout); + warning(state, phi, "variable not set at index %d on all paths to use", j); #endif - rename_block_variables(state, state->first_block); - prune_block_variables(state, state->first_block); + } + } + } + xfree(live); +} + +static void transform_to_ssa_form(struct compile_state *state) +{ + insert_phi_operations(state); + rename_variables(state); + + prune_block_variables(state, state->bb.first_block); + prune_unused_phis(state); + + print_blocks(state, __func__, state->dbgout); } static void clear_vertex( struct compile_state *state, struct block *block, void *arg) { + /* Clear the current blocks vertex and the vertex of all + * of the current blocks neighbors in case there are malformed + * blocks with now instructions at this point. + */ + struct block_set *user, *edge; block->vertex = 0; + for(edge = block->edges; edge; edge = edge->next) { + edge->member->vertex = 0; + } + for(user = block->use; user; user = user->next) { + user->member->vertex = 0; + } } static void mark_live_block( @@ -10105,8 +16681,8 @@ static void mark_live_block( *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)) { + targ = triple_edge_targ(state, block->last, 0); + for(; targ; targ = triple_edge_targ(state, block->last, targ)) { if (!*targ) { continue; } @@ -10115,8 +16691,12 @@ static void mark_live_block( } mark_live_block(state, (*targ)->u.block, next_vertex); } + /* Ensure the last block of a function remains alive */ + if (triple_is_call(state, block->last)) { + mark_live_block(state, MISC(block->last, 0)->u.block, next_vertex); + } } - else if (block->last->next != RHS(state->main_function, 0)) { + else if (block->last->next != state->first) { struct triple *ins; ins = block->last->next; if (!triple_stores_block(state, ins)) { @@ -10132,30 +16712,37 @@ static void transform_from_ssa_form(struct compile_state *state) * edges to blocks containting phi functions. */ struct triple *first; - struct triple *phi, *next; + struct triple *phi, *var, *next; int next_vertex; /* Walk the control flow to see which blocks remain alive */ - walk_blocks(state, clear_vertex, 0); + walk_blocks(state, &state->bb, clear_vertex, 0); next_vertex = 1; - mark_live_block(state, state->first_block, &next_vertex); + mark_live_block(state, state->bb.first_block, &next_vertex); /* Walk all of the operations to find the phi functions */ - first = RHS(state->main_function, 0); + first = state->first; for(phi = first->next; phi != first ; phi = next) { struct block_set *set; struct block *block; struct triple **slot; - struct triple *var, *read; + struct triple *var; struct triple_set *use, *use_next; - int edge, used; + int edge, writers, readers; next = phi->next; if (phi->op != OP_PHI) { continue; } + block = phi->u.block; slot = &RHS(phi, 0); + /* If this phi is in a dead block just forget it */ + if (block->vertex == 0) { + release_triple(state, phi); + continue; + } + /* Forget uses from code in dead blocks */ for(use = phi->use; use; use = use_next) { struct block *ublock; @@ -10173,64 +16760,184 @@ static void transform_from_ssa_form(struct compile_state *state) } unuse_triple(phi, use->member); } - /* 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 */ - read = post_triple(state, var, OP_READ, phi->type, var, 0); - use_triple(var, read); + if (registers_of(state, phi->type) != 1) { + internal_error(state, phi, "phi->type does not fit in a single register!"); + } + var = post_triple(state, phi, OP_ADECL, phi->type, 0, 0); + var = var->next; /* point at the var */ + + /* Replaces use of phi with var */ + propogate_use(state, phi, var); - /* Replaces uses of the phi with variable reads */ - propogate_use(state, phi, read); + /* Count the readers */ + readers = 0; + for(use = var->use; use; use = use->next) { + if (use->member != MISC(var, 0)) { + readers++; + } + } /* Walk all of the incoming edges/blocks and insert moves. */ + writers = 0; for(edge = 0, set = block->use; set; set = set->next, edge++) { - struct block *eblock; + struct block *eblock, *vblock; struct triple *move; - struct triple *val; + struct triple *val, *base; eblock = set->member; val = slot[edge]; slot[edge] = 0; unuse_triple(val, phi); + vblock = block_of_triple(state, val); - if (!val || (val == &zero_triple) || - (block->vertex == 0) || (eblock->vertex == 0) || - (val == phi) || (val == read)) { + /* If we don't have a value that belongs in an OP_WRITE + * continue on. + */ + if (!val || (val == &unknown_triple) || (val == phi) + || (vblock && (vblock->vertex == 0))) { continue; } - - move = post_triple(state, - val, 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) { - struct triple **expr; - expr = triple_lhs(state, use->member, 0); - for(; expr; expr = triple_lhs(state, use->member, expr)) { - if (*expr == var) { - used = 1; + /* If the value should never occur error */ + if (!vblock) { + internal_error(state, val, "no vblock?"); + continue; + } + + /* If the value occurs in a dead block see if a replacement + * block can be found. + */ + while(eblock && (eblock->vertex == 0)) { + eblock = eblock->idom; + } + /* If not continue on with the next value. */ + if (!eblock || (eblock->vertex == 0)) { + continue; + } + + /* If we have an empty incoming block ignore it. */ + if (!eblock->first) { + internal_error(state, 0, "empty block?"); + } + + /* Make certain the write is placed in the edge block... */ + /* Walk through the edge block backwards to find an + * appropriate location for the OP_WRITE. + */ + for(base = eblock->last; base != eblock->first; base = base->prev) { + struct triple **expr; + if (base->op == OP_PIECE) { + base = MISC(base, 0); + } + if ((base == var) || (base == val)) { + goto out; + } + expr = triple_lhs(state, base, 0); + for(; expr; expr = triple_lhs(state, base, expr)) { + if ((*expr) == val) { + goto out; + } } + expr = triple_rhs(state, base, 0); + for(; expr; expr = triple_rhs(state, base, expr)) { + if ((*expr) == var) { + goto out; + } + } + } + out: + if (triple_is_branch(state, base)) { + internal_error(state, base, + "Could not insert write to phi"); } + move = post_triple(state, base, OP_WRITE, var->type, val, var); + use_triple(val, move); + use_triple(var, move); + writers++; + } + if (!writers && readers) { + internal_error(state, var, "no value written to in use phi?"); } /* If var is not used free it */ - if (!used) { - unuse_triple(var, read); - free_triple(state, read); - free_triple(state, var); + if (!writers) { + release_triple(state, MISC(var, 0)); + release_triple(state, var); } - /* Release the phi function */ release_triple(state, phi); } - + + /* Walk all of the operations to find the adecls */ + for(var = first->next; var != first ; var = var->next) { + struct triple_set *use, *use_next; + if (!triple_is_auto_var(state, var)) { + continue; + } + + /* Walk through all of the rhs uses of var and + * replace them with read of var. + */ + for(use = var->use; use; use = use_next) { + struct triple *read, *user; + struct triple **slot; + int zrhs, i, used; + use_next = use->next; + user = use->member; + + /* Generate a read of var */ + read = pre_triple(state, user, OP_READ, var->type, var, 0); + use_triple(var, read); + + /* Find the rhs uses and see if they need to be replaced */ + used = 0; + zrhs = user->rhs; + slot = &RHS(user, 0); + for(i = 0; i < zrhs; i++) { + if (slot[i] == var) { + slot[i] = read; + used = 1; + } + } + /* If we did use it cleanup the uses */ + if (used) { + unuse_triple(var, user); + use_triple(read, user); + } + /* If we didn't use it release the extra triple */ + else { + release_triple(state, read); + } + } + } } +#define HI() if (state->compiler->debug & DEBUG_REBUILD_SSA_FORM) { \ + FILE *fp = state->dbgout; \ + fprintf(fp, "@ %s:%d\n", __FILE__, __LINE__); romcc_print_blocks(state, fp); \ + } + +static void rebuild_ssa_form(struct compile_state *state) +{ +HI(); + transform_from_ssa_form(state); +HI(); + state->bb.first = state->first; + free_basic_blocks(state, &state->bb); + analyze_basic_blocks(state, &state->bb); +HI(); + insert_phi_operations(state); +HI(); + rename_variables(state); +HI(); + + prune_block_variables(state, state->bb.first_block); +HI(); + prune_unused_phis(state); +HI(); +} +#undef HI -/* +/* * Register conflict resolution * ========================================================= */ @@ -10260,7 +16967,7 @@ static struct reg_info find_def_color( if (tinfo.reg >= MAX_REGISTERS) { tinfo.reg = REG_UNSET; } - if ((tinfo.reg != REG_UNSET) && + if ((tinfo.reg != REG_UNSET) && (info.reg != REG_UNSET) && (tinfo.reg != info.reg)) { internal_error(state, def, "register conflict"); @@ -10285,8 +16992,8 @@ static struct reg_info find_lhs_pre_color( { struct reg_info info; int zlhs, zrhs, i; - zrhs = TRIPLE_RHS(ins->sizes); - zlhs = TRIPLE_LHS(ins->sizes); + zrhs = ins->rhs; + zlhs = ins->lhs; if (!zlhs && triple_is_def(state, ins)) { zlhs = 1; } @@ -10321,14 +17028,14 @@ static struct reg_info find_lhs_post_color( struct triple_set *set; struct reg_info info; struct triple *lhs; -#if 0 - fprintf(stderr, "find_lhs_post_color(%p, %d)\n", +#if DEBUG_TRIPLE_COLOR + fprintf(state->errout, "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)) { + else if (index < ins->lhs) { lhs = LHS(ins, index); } else { @@ -10344,7 +17051,7 @@ static struct reg_info find_lhs_post_color( struct triple *user; int zrhs, i; user = set->member; - zrhs = TRIPLE_RHS(user->sizes); + zrhs = user->rhs; for(i = 0; i < zrhs; i++) { if (RHS(user, i) != lhs) { continue; @@ -10365,8 +17072,8 @@ static struct reg_info find_lhs_post_color( info.regcm &= rinfo.regcm; } } -#if 0 - fprintf(stderr, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n", +#if DEBUG_TRIPLE_COLOR + fprintf(state->errout, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n", ins, index, info.reg, info.regcm); #endif return info; @@ -10377,12 +17084,12 @@ static struct reg_info find_rhs_post_color( { struct reg_info info, rinfo; int zlhs, i; -#if 0 - fprintf(stderr, "find_rhs_post_color(%p, %d)\n", +#if DEBUG_TRIPLE_COLOR + fprintf(state->errout, "find_rhs_post_color(%p, %d)\n", ins, index); #endif rinfo = arch_reg_rhs(state, ins, index); - zlhs = TRIPLE_LHS(ins->sizes); + zlhs = ins->lhs; if (!zlhs && triple_is_def(state, ins)) { zlhs = 1; } @@ -10400,7 +17107,7 @@ static struct reg_info find_rhs_post_color( if (tinfo.reg >= MAX_REGISTERS) { tinfo.reg = REG_UNSET; } - info.regcm &= linfo.reg; + info.regcm &= linfo.regcm; info.regcm &= tinfo.regcm; if (info.reg != REG_UNSET) { internal_error(state, ins, "register conflict"); @@ -10411,8 +17118,8 @@ static struct reg_info find_rhs_post_color( info.reg = tinfo.reg; } } -#if 0 - fprintf(stderr, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n", +#if DEBUG_TRIPLE_COLOR + fprintf(state->errout, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n", ins, index, info.reg, info.regcm); #endif return info; @@ -10422,8 +17129,8 @@ static struct reg_info find_lhs_color( struct compile_state *state, struct triple *ins, int index) { struct reg_info pre, post, info; -#if 0 - fprintf(stderr, "find_lhs_color(%p, %d)\n", +#if DEBUG_TRIPLE_COLOR + fprintf(state->errout, "find_lhs_color(%p, %d)\n", ins, index); #endif pre = find_lhs_pre_color(state, ins, index); @@ -10438,9 +17145,10 @@ static struct reg_info find_lhs_color( if (info.reg == REG_UNSET) { info.reg = post.reg; } -#if 0 - fprintf(stderr, "find_lhs_color(%p, %d) -> ( %d, %x)\n", - ins, index, info.reg, info.regcm); +#if DEBUG_TRIPLE_COLOR + fprintf(state->errout, "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; } @@ -10475,22 +17183,49 @@ static struct triple *post_copy(struct compile_state *state, struct triple *ins) return out; } -static struct triple *pre_copy( - struct compile_state *state, struct triple *ins, int index) +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; + int op; + 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); - in = pre_triple(state, ins, OP_COPY, (*expr)->type, *expr, 0); + if ((info.regcm & classes) == 0) { + FILE *fp = state->errout; + fprintf(fp, "src_type: "); + name_of(fp, ins->type); + fprintf(fp, "\ndst_type: "); + name_of(fp, type); + fprintf(fp, "\n"); + internal_error(state, ins, "pre_copy with no register classes"); + } + op = OP_COPY; + if (!equiv_types(type, (*expr)->type)) { + op = OP_CONVERT; + } + in = pre_triple(state, ins, op, 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); } @@ -10503,11 +17238,11 @@ static void insert_copies_to_phi(struct compile_state *state) struct triple *phi; /* Walk all of the operations to find the phi functions */ - first = RHS(state->main_function, 0); + first = state->first; 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; @@ -10515,6 +17250,13 @@ static void insert_copies_to_phi(struct compile_state *state) phi->id |= TRIPLE_FLAG_POST_SPLIT; block = phi->u.block; 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++) { @@ -10529,24 +17271,43 @@ static void insert_copies_to_phi(struct compile_state *state) continue; } - move = build_triple(state, OP_COPY, phi->type, val, 0, - val->filename, val->line, val->col); + get_occurance(val->occurance); + move = build_triple(state, OP_COPY, val->type, val, 0, + val->occurance); move->u.block = eblock; move->id |= TRIPLE_FLAG_PRE_SPLIT; use_triple(val, move); - + slot[edge] = move; unuse_triple(val, phi); use_triple(move, phi); + /* Walk up the dominator tree until I have found the appropriate block */ + while(eblock && !tdominates(state, val, eblock->last)) { + eblock = eblock->idom; + } + if (!eblock) { + internal_error(state, phi, "Cannot find block dominated by %p", + val); + } + /* Walk through the block backwards to find * an appropriate location for the OP_COPY. */ for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) { struct triple **expr; + if (ptr->op == OP_PIECE) { + ptr = MISC(ptr, 0); + } if ((ptr == phi) || (ptr == val)) { goto out; } + expr = triple_lhs(state, ptr, 0); + for(;expr; expr = triple_lhs(state, ptr, expr)) { + if ((*expr) == val) { + goto out; + } + } expr = triple_rhs(state, ptr, 0); for(;expr; expr = triple_rhs(state, ptr, expr)) { if ((*expr) == phi) { @@ -10559,29 +17320,21 @@ static void insert_copies_to_phi(struct compile_state *state) internal_error(state, ptr, "Could not insert write to phi"); } - insert_triple(state, ptr->next, move); - if (eblock->last == ptr) { + insert_triple(state, after_lhs(state, ptr), move); + if (eblock->last == after_lhs(state, ptr)->prev) { eblock->last = move; } transform_to_arch_instruction(state, move); } } + print_blocks(state, __func__, state->dbgout); } -struct triple_reg_set { - struct triple_reg_set *next; - struct triple *member; - struct triple *new; -}; +struct triple_reg_set; +struct reg_block; -struct reg_block { - struct block *block; - struct triple_reg_set *in; - struct triple_reg_set *out; - int vertex; -}; -static int do_triple_set(struct triple_reg_set **head, +static int do_triple_set(struct triple_reg_set **head, struct triple *member, struct triple *new_member) { struct triple_reg_set **ptr, *new; @@ -10623,36 +17376,92 @@ static int in_triple(struct reg_block *rb, struct triple *in) { return do_triple_set(&rb->in, in, 0); } + +#if DEBUG_ROMCC_WARNING static void unin_triple(struct reg_block *rb, struct triple *unin) { do_triple_unset(&rb->in, unin); } +#endif static int out_triple(struct reg_block *rb, struct triple *out) { return do_triple_set(&rb->out, out, 0); } +#if DEBUG_ROMCC_WARNING static void unout_triple(struct reg_block *rb, struct triple *unout) { do_triple_unset(&rb->out, unout); } +#endif + +static int initialize_regblock(struct reg_block *blocks, + struct block *block, int vertex) +{ + struct block_set *user; + if (!block || (blocks[block->vertex].block == block)) { + return vertex; + } + vertex += 1; + /* Renumber the blocks in a convinient fashion */ + block->vertex = vertex; + blocks[vertex].block = block; + blocks[vertex].vertex = vertex; + for(user = block->use; user; user = user->next) { + vertex = initialize_regblock(blocks, user->member, vertex); + } + return vertex; +} + +static struct triple *part_to_piece(struct compile_state *state, struct triple *ins) +{ +/* Part to piece is a best attempt and it cannot be correct all by + * itself. If various values are read as different sizes in different + * parts of the code this function cannot work. Or rather it cannot + * work in conjunction with compute_variable_liftimes. As the + * analysis will get confused. + */ + struct triple *base; + unsigned reg; + if (!is_lvalue(state, ins)) { + return ins; + } + base = 0; + reg = 0; + while(ins && triple_is_part(state, ins) && (ins->op != OP_PIECE)) { + base = MISC(ins, 0); + switch(ins->op) { + case OP_INDEX: + reg += index_reg_offset(state, base->type, ins->u.cval)/REG_SIZEOF_REG; + break; + case OP_DOT: + reg += field_reg_offset(state, base->type, ins->u.field)/REG_SIZEOF_REG; + break; + default: + internal_error(state, ins, "unhandled part"); + break; + } + ins = base; + } + if (base) { + if (reg > base->lhs) { + internal_error(state, base, "part out of range?"); + } + ins = LHS(base, reg); + } + return ins; +} -static int initialize_regblock(struct reg_block *blocks, - struct block *block, int vertex) +static int this_def(struct compile_state *state, + struct triple *ins, struct triple *other) { - struct block_set *user; - if (!block || (blocks[block->vertex].block == block)) { - return vertex; + if (ins == other) { + return 1; } - vertex += 1; - /* Renumber the blocks in a convinient fashion */ - block->vertex = vertex; - blocks[vertex].block = block; - blocks[vertex].vertex = vertex; - for(user = block->use; user; user = user->next) { - vertex = initialize_regblock(blocks, user->member, vertex); + if (ins->op == OP_WRITE) { + ins = part_to_piece(state, MISC(ins, 0)); } - return vertex; + return ins == other; } static int phi_in(struct compile_state *state, struct reg_block *blocks, @@ -10694,7 +17503,7 @@ static int phi_in(struct compile_state *state, struct reg_block *blocks, */ ptr2 = rb->block->first; for(done2 = 0; !done2; ptr2 = ptr2->next) { - if (ptr2 == expr) { + if (this_def(state, ptr2, expr)) { break; } done2 = (ptr2 == rb->block->last); @@ -10731,7 +17540,7 @@ static int reg_in(struct compile_state *state, struct reg_block *blocks, last = rb->block->last; done = 0; for(ptr = first; !done; ptr = ptr->next) { - if (ptr == in_set->member) { + if (this_def(state, ptr, in_set->member)) { break; } done = (ptr == last); @@ -10745,13 +17554,14 @@ static int reg_in(struct compile_state *state, struct reg_block *blocks, return change; } - static int use_in(struct compile_state *state, struct reg_block *rb) { /* Find the variables we use but don't define and add * it to the current blocks input set. */ +#if DEBUG_ROMCC_WARNINGS #warning "FIXME is this O(N^2) algorithm bad?" +#endif struct block *block; struct triple *ptr; int done; @@ -10772,14 +17582,17 @@ static int use_in(struct compile_state *state, struct reg_block *rb) for(;expr; expr = triple_rhs(state, ptr, expr)) { struct triple *rhs, *test; int tdone; - rhs = *expr; + rhs = part_to_piece(state, *expr); if (!rhs) { continue; } - /* See if rhs is defined in this block */ + + /* See if rhs is defined in this block. + * A write counts as a definition. + */ for(tdone = 0, test = ptr; !tdone; test = test->prev) { tdone = (test == block->first); - if (test == rhs) { + if (this_def(state, test, rhs)) { rhs = 0; break; } @@ -10792,27 +17605,23 @@ static int use_in(struct compile_state *state, struct reg_block *rb) } static struct reg_block *compute_variable_lifetimes( - struct compile_state *state) + struct compile_state *state, struct basic_blocks *bb) { struct reg_block *blocks; int change; blocks = xcmalloc( - sizeof(*blocks)*(state->last_vertex + 1), "reg_block"); - initialize_regblock(blocks, state->last_block, 0); + sizeof(*blocks)*(bb->last_vertex + 1), "reg_block"); + initialize_regblock(blocks, bb->last_block, 0); do { int i; change = 0; - for(i = 1; i <= state->last_vertex; i++) { + for(i = 1; i <= bb->last_vertex; i++) { + struct block_set *edge; struct reg_block *rb; rb = &blocks[i]; - /* Add the left successor's input set to in */ - if (rb->block->left) { - change |= reg_in(state, blocks, rb, rb->block->left); - } - /* Add the right successor's input set to in */ - if ((rb->block->right) && - (rb->block->right != rb->block->left)) { - change |= reg_in(state, blocks, rb, rb->block->right); + /* Add the all successor's input set to in */ + for(edge = rb->block->edges; edge; edge = edge->next) { + change |= reg_in(state, blocks, rb, edge->member); } /* Add use to in... */ change |= use_in(state, rb); @@ -10821,12 +17630,12 @@ static struct reg_block *compute_variable_lifetimes( return blocks; } -static void free_variable_lifetimes( - struct compile_state *state, struct reg_block *blocks) +static void free_variable_lifetimes(struct compile_state *state, + struct basic_blocks *bb, struct reg_block *blocks) { int i; /* free in_set && out_set on each block */ - for(i = 1; i <= state->last_vertex; i++) { + for(i = 1; i <= bb->last_vertex; i++) { struct triple_reg_set *entry, *next; struct reg_block *rb; rb = &blocks[i]; @@ -10843,17 +17652,18 @@ static void free_variable_lifetimes( } -typedef struct triple *(*wvl_cb_t)( - struct compile_state *state, - struct reg_block *blocks, struct triple_reg_set *live, +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); static void walk_variable_lifetimes(struct compile_state *state, - struct reg_block *blocks, wvl_cb_t cb, void *arg) + struct basic_blocks *bb, struct reg_block *blocks, + wvl_cb_t cb, void *arg) { int i; - - for(i = 1; i <= state->last_vertex; i++) { + + for(i = 1; i <= state->bb.last_vertex; i++) { struct triple_reg_set *live; struct triple_reg_set *entry, *next; struct triple *ptr, *prev; @@ -10873,7 +17683,7 @@ static void walk_variable_lifetimes(struct compile_state *state, } /* Walk through the basic block calculating live */ for(done = 0, ptr = block->last; !done; ptr = prev) { - struct triple **expr, *result; + struct triple **expr; prev = ptr->prev; done = (ptr == block->first); @@ -10886,20 +17696,11 @@ static void walk_variable_lifetimes(struct compile_state *state, /* Inform the callback function of what is * going on. */ - result = cb(state, blocks, live, rb, ptr, arg); - + 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 (!result) { - if (block->last == ptr) { - block->last = prev; - } - continue; - } - - /* Add the current uses to live. * * It is safe to skip phi functions because they do @@ -10927,11 +17728,99 @@ static void walk_variable_lifetimes(struct compile_state *state, } } +struct print_live_variable_info { + struct reg_block *rb; + FILE *fp; +}; +#if DEBUG_EXPLICIT_CLOSURES +static void print_live_variables_block( + struct compile_state *state, struct block *block, void *arg) + +{ + struct print_live_variable_info *info = arg; + struct block_set *edge; + FILE *fp = info->fp; + struct reg_block *rb; + struct triple *ptr; + int phi_present; + int done; + rb = &info->rb[block->vertex]; + + fprintf(fp, "\nblock: %p (%d),", + block, block->vertex); + for(edge = block->edges; edge; edge = edge->next) { + fprintf(fp, " %p<-%p", + edge->member, + edge->member && edge->member->use?edge->member->use->member : 0); + } + fprintf(fp, "\n"); + 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) { + done = (ptr == block->last); + display_triple(fp, ptr); + } + 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_live_variables(struct compile_state *state, + struct basic_blocks *bb, struct reg_block *rb, FILE *fp) +{ + struct print_live_variable_info info; + info.rb = rb; + info.fp = fp; + fprintf(fp, "\nlive variables by block\n"); + walk_blocks(state, bb, print_live_variables_block, &info); + +} +#endif + static int count_triples(struct compile_state *state) { struct triple *first, *ins; int triples = 0; - first = RHS(state->main_function, 0); + first = state->first; ins = first; do { triples++; @@ -10939,15 +17828,47 @@ static int count_triples(struct compile_state *state) } while (ins != first); return triples; } + + struct dead_triple { struct triple *triple; struct dead_triple *work_next; struct block *block; - int color; + int old_id; int flags; #define TRIPLE_FLAG_ALIVE 1 +#define TRIPLE_FLAG_FREE 1 }; +static void print_dead_triples(struct compile_state *state, + struct dead_triple *dtriple) +{ + struct triple *first, *ins; + struct dead_triple *dt; + FILE *fp; + if (!(state->compiler->debug & DEBUG_TRIPLES)) { + return; + } + fp = state->dbgout; + fprintf(fp, "--------------- dtriples ---------------\n"); + first = state->first; + ins = first; + do { + dt = &dtriple[ins->id]; + if ((ins->op == OP_LABEL) && (ins->use)) { + fprintf(fp, "\n%p:\n", ins); + } + fprintf(fp, "%c", + (dt->flags & TRIPLE_FLAG_ALIVE)?' ': '-'); + display_triple(fp, ins); + if (triple_is_branch(state, ins)) { + fprintf(fp, "\n"); + } + ins = ins->next; + } while(ins != first); + fprintf(fp, "\n"); +} + static void awaken( struct compile_state *state, @@ -10968,7 +17889,7 @@ static void awaken( triple->id); } if (triple->op == OP_NOOP) { - internal_warning(state, triple, "awakening noop?"); + internal_error(state, triple, "awakening noop?"); return; } dt = &dtriple[triple->id]; @@ -10983,44 +17904,43 @@ static void awaken( static void eliminate_inefectual_code(struct compile_state *state) { - struct block *block; struct dead_triple *dtriple, *work_list, **work_list_tail, *dt; int triples, i; struct triple *first, *ins; + if (!(state->compiler->flags & COMPILER_ELIMINATE_INEFECTUAL_CODE)) { + return; + } + /* Setup the work list */ work_list = 0; work_list_tail = &work_list; - first = RHS(state->main_function, 0); + first = state->first; /* Count how many triples I have */ triples = count_triples(state); /* Now put then in an array and mark all of the triples dead */ dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples"); - + ins = first; i = 1; - block = 0; do { - if (ins->op == OP_LABEL) { - block = ins->u.block; - } dtriple[i].triple = ins; - dtriple[i].block = block; + dtriple[i].block = block_of_triple(state, ins); dtriple[i].flags = 0; - dtriple[i].color = ins->id; + dtriple[i].old_id = ins->id; 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(state, ins)) { + if (!triple_is_pure(state, ins, dtriple[i].old_id)) { awaken(state, dtriple, &ins, &work_list_tail); } i++; ins = ins->next; } while(ins != first); while(work_list) { + struct block *block; struct dead_triple *dt; struct block_set *user; struct triple **expr; @@ -11029,6 +17949,15 @@ static void eliminate_inefectual_code(struct compile_state *state) if (!work_list) { work_list_tail = &work_list; } + /* Make certain the block the current instruction is in lives */ + block = block_of_triple(state, dt->triple); + awaken(state, dtriple, &block->first, &work_list_tail); + if (triple_is_branch(state, block->last)) { + awaken(state, dtriple, &block->last, &work_list_tail); + } else { + awaken(state, dtriple, &block->last->next, &work_list_tail); + } + /* Wake up the data depencencies of this triple */ expr = 0; do { @@ -11050,27 +17979,34 @@ static void eliminate_inefectual_code(struct compile_state *state) } while(expr); /* Wake up the reverse control dependencies of this triple */ for(user = dt->block->ipdomfrontier; user; user = user->next) { - awaken(state, dtriple, &user->member->last, &work_list_tail); + struct triple *last; + last = user->member->last; + while((last->op == OP_NOOP) && (last != user->member->first)) { +#if DEBUG_ROMCC_WARNINGS +#warning "Should we bring the awakening noops back?" +#endif + // internal_warning(state, last, "awakening noop?"); + last = last->prev; + } + awaken(state, dtriple, &last, &work_list_tail); } } + print_dead_triples(state, dtriple); for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) { - if ((dt->triple->op == OP_NOOP) && + if ((dt->triple->op == OP_NOOP) && (dt->flags & TRIPLE_FLAG_ALIVE)) { internal_error(state, dt->triple, "noop effective?"); } - dt->triple->id = dt->color; /* Restore the color */ + dt->triple->id = dt->old_id; /* Restore the color */ if (!(dt->flags & TRIPLE_FLAG_ALIVE)) { -#warning "FIXME handle the case of killing a basic block" - if (dt->block->first == dt->triple) { - continue; - } - if (dt->block->last == dt->triple) { - dt->block->last = dt->triple->prev; - } release_triple(state, dt->triple); } } xfree(dtriple); + + rebuild_ssa_form(state); + + print_blocks(state, __func__, state->dbgout); } @@ -11085,7 +18021,7 @@ static void insert_mandatory_copies(struct compile_state *state) * are inserting copies before instructions but that * case should be rare. */ - first = RHS(state->main_function, 0); + first = state->first; ins = first; do { struct triple_set *entry, *next; @@ -11098,11 +18034,11 @@ static void insert_mandatory_copies(struct compile_state *state) goto next; } /* Find the architecture specific color information */ - info = arch_reg_lhs(state, ins, 0); + info = find_lhs_pre_color(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; @@ -11116,13 +18052,13 @@ static void insert_mandatory_copies(struct compile_state *state) 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)) { @@ -11135,17 +18071,25 @@ static void insert_mandatory_copies(struct compile_state *state) 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) && + (((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 insert and do a pre_copy or see if a post_copy is warranted */ + /* 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; @@ -11154,7 +18098,7 @@ static void insert_mandatory_copies(struct compile_state *state) if (i < 0) { continue; } - + /* Find the users color requirements */ rinfo = arch_reg_rhs(state, entry->member, i); if (rinfo.reg >= MAX_REGISTERS) { @@ -11180,6 +18124,7 @@ static void insert_mandatory_copies(struct compile_state *state) internal_error(state, user, "bad rhs"); } tmp = pre_copy(state, user, i); + tmp->id |= TRIPLE_FLAG_PRE_SPLIT; continue; } else { do_post_copy = 1; @@ -11195,17 +18140,19 @@ static void insert_mandatory_copies(struct compile_state *state) 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)) { @@ -11215,6 +18162,8 @@ static void insert_mandatory_copies(struct compile_state *state) next: ins = ins->next; } while(ins != first); + + print_blocks(state, __func__, state->dbgout); } @@ -11266,10 +18215,142 @@ struct reg_state { 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; + struct block_set *edge; + 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),", + block, block->vertex); + for(edge = block->edges; edge; edge = edge->next) { + fprintf(fp, " %p<-%p", + edge->member, + edge->member && edge->member->use?edge->member->use->member : 0); + } + fprintf(fp, "\n"); + 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; + 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, &state->bb, print_interference_block, &info); + +} + static unsigned regc_max_size(struct compile_state *state, int classes) { unsigned max_size; @@ -11369,9 +18450,12 @@ static struct lre_hash **lre_probe(struct reg_state *rstate, right = tmp; } 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; @@ -11385,7 +18469,7 @@ static int interfere(struct reg_state *rstate, return ptr && *ptr; } -static void add_live_edge(struct reg_state *rstate, +static void add_live_edge(struct reg_state *rstate, struct live_range *left, struct live_range *right) { /* FIXME the memory allocation overhead is noticeable here... */ @@ -11409,6 +18493,10 @@ static void add_live_edge(struct reg_state *rstate, if (*ptr) { return; } +#if 0 + fprintf(state->errout, "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; @@ -11420,7 +18508,7 @@ static void add_live_edge(struct reg_state *rstate, edge->node = right; left->edges = edge; left->degree += 1; - + edge = xmalloc(sizeof(*edge), "live_range_edge"); edge->next = right->edges; edge->node = left; @@ -11447,6 +18535,7 @@ static void remove_live_edge(struct reg_state *rstate, *ptr = edge->next; memset(edge, 0, sizeof(*edge)); xfree(edge); + right->degree--; break; } } @@ -11456,6 +18545,7 @@ static void remove_live_edge(struct reg_state *rstate, *ptr = edge->next; memset(edge, 0, sizeof(*edge)); xfree(edge); + left->degree--; break; } } @@ -11470,9 +18560,22 @@ 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... - * + * * new(n) --- Return a graph with n nodes but no edges. * add(g,x,y) --- Return a graph including g with an between x and y * interfere(g, x, y) --- Return true if there exists an edge between the nodes @@ -11485,11 +18588,11 @@ static void remove_live_edges(struct reg_state *rstate, struct live_range *range * The adjacency vectors support an efficient implementation of neighbors. */ -/* +/* * +---------------------------------------------------+ * | +--------------+ | * v v | | - * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select + * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select * * -- In simplify implment optimistic coloring... (No backtracking) * -- Implement Rematerialization it is the only form of spilling we can perform @@ -11503,8 +18606,9 @@ static void remove_live_edges(struct reg_state *rstate, struct live_range *range * */ +#if DEBUG_ROMCC_WARNING static void different_colored( - struct compile_state *state, struct reg_state *rstate, + struct compile_state *state, struct reg_state *rstate, struct triple *parent, struct triple *ins) { struct live_range *lr; @@ -11522,7 +18626,7 @@ static void different_colored( } } } - +#endif static struct live_range *coalesce_ranges( struct compile_state *state, struct reg_state *rstate, @@ -11540,13 +18644,13 @@ static struct live_range *coalesce_ranges( } if ((lr1->color == REG_UNNEEDED) || (lr2->color == REG_UNNEEDED)) { - internal_error(state, 0, + 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, + internal_error(state, lr1->defs->def, "cannot coalesce live ranges of different colors"); } color = lr1->color; @@ -11558,10 +18662,26 @@ static struct live_range *coalesce_ranges( internal_error(state, lr1->defs->def, "cannot coalesce live ranges with dissimilar register classes"); } + if (state->compiler->debug & DEBUG_COALESCING) { + FILE *fp = state->errout; + fprintf(fp, "coalescing:"); + lrd = lr1->defs; + do { + fprintf(fp, " %p", lrd->def); + lrd = lrd->next; + } while(lrd != lr1->defs); + fprintf(fp, " |"); + lrd = lr2->defs; + do { + fprintf(fp, " %p", lrd->def); + lrd = lrd->next; + } while(lrd != lr2->defs); + fprintf(fp, "\n"); + } /* 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. + * them. */ if ((lr1->defs->prev->def->op == OP_PHI) || ((lr2->defs->prev->def->op != OP_PHI) && @@ -11573,34 +18693,41 @@ static struct live_range *coalesce_ranges( } #if 0 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) { - fprintf(stderr, "lr1 post\n"); + fprintf(state->errout, "lr1 post\n"); } if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) { - fprintf(stderr, "lr1 pre\n"); + fprintf(state->errout, "lr1 pre\n"); } if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) { - fprintf(stderr, "lr2 post\n"); + fprintf(state->errout, "lr2 post\n"); } if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) { - fprintf(stderr, "lr2 pre\n"); + fprintf(state->errout, "lr2 pre\n"); } #endif #if 0 - fprintf(stderr, "coalesce color1(%p): %3d color2(%p) %3d\n", + fprintf(state->errout, "coalesce color1(%p): %3d color2(%p) %3d\n", lr1->defs->def, lr1->color, lr2->defs->def, lr2->color); #endif - - lr1->classes = classes; + /* Append lr2 onto lr1 */ +#if DEBUG_ROMCC_WARNINGS #warning "FIXME should this be a merge instead of a splice?" +#endif + /* 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; @@ -11626,6 +18753,9 @@ static struct live_range *coalesce_ranges( 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; } @@ -11667,14 +18797,14 @@ static void initialize_live_ranges( size_t count, size; int i, j; - first = RHS(state->main_function, 0); + first = state->first; /* First count how many instructions I have. */ count = count_triples(state); /* Potentially I need one live range definitions for each - * instruction, plus an extra for the split routines. + * instruction. */ - rstate->defs = count + 1; + rstate->defs = count; /* Potentially I need one live range for each instruction * plus an extra for the dummy live range. */ @@ -11696,14 +18826,13 @@ static void initialize_live_ranges( struct reg_info info; /* Find the architecture specific color information */ info = find_def_color(state, ins); - i++; rstate->lr[i].defs = &rstate->lrd[j]; rstate->lr[i].color = info.reg; rstate->lr[i].classes = info.regcm; rstate->lr[i].degree = 0; rstate->lrd[j].lr = &rstate->lr[i]; - } + } /* Otherwise give the triple the dummy live range. */ else { rstate->lrd[j].lr = &rstate->lr[0]; @@ -11720,7 +18849,6 @@ static void initialize_live_ranges( ins = ins->next; } while(ins != first); rstate->ranges = i; - rstate->defs -= 1; /* Make a second pass to handle achitecture specific register * constraints. @@ -11731,14 +18859,19 @@ static void initialize_live_ranges( 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); + zlhs = ins->lhs; if ((zlhs == 0) && triple_is_def(state, ins)) { zlhs = 1; } - zrhs = TRIPLE_RHS(ins->sizes); - + zrhs = ins->rhs; + + if (state->compiler->debug & DEBUG_COALESCING2) { + fprintf(state->errout, "mandatory coalesce: %p %d %d\n", + ins, zlhs, zrhs); + } + for(i = 0; i < zlhs; i++) { struct reg_info linfo; struct live_range_def *lhs; @@ -11751,6 +18884,12 @@ static void initialize_live_ranges( } else { lhs = &rstate->lrd[LHS(ins, i)->id]; } + + if (state->compiler->debug & DEBUG_COALESCING2) { + fprintf(state->errout, "coalesce lhs(%d): %p %d\n", + i, lhs, linfo.reg); + } + for(j = 0; j < zrhs; j++) { struct reg_info rinfo; struct live_range_def *rhs; @@ -11758,9 +18897,15 @@ static void initialize_live_ranges( if (rinfo.reg < MAX_REGISTERS) { continue; } - rhs = &rstate->lrd[RHS(ins, i)->id]; + rhs = &rstate->lrd[RHS(ins, j)->id]; + + if (state->compiler->debug & DEBUG_COALESCING2) { + fprintf(state->errout, "coalesce rhs(%d): %p %d\n", + j, rhs, rinfo.reg); + } + if (rinfo.reg == linfo.reg) { - coalesce_ranges(state, rstate, + coalesce_ranges(state, rstate, lhs->lr, rhs->lr); } } @@ -11769,9 +18914,9 @@ static void initialize_live_ranges( } while(ins != first); } -static struct triple *graph_ins( - struct compile_state *state, - struct reg_block *blocks, struct triple_reg_set *live, +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) { struct reg_state *rstate = arg; @@ -11783,10 +18928,10 @@ static struct triple *graph_ins( * the interference graph. */ if (!triple_is_def(state, ins)) { - return ins; + return; } def = rstate->lrd[ins->id].lr; - + /* Create an edge between ins and everything that is * alive, unless the live_range cannot share * a physical register with ins. @@ -11805,57 +18950,143 @@ static struct triple *graph_ins( } add_live_edge(rstate, def, lr); } - return ins; + return; +} + +#if DEBUG_CONSISTENCY > 1 +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 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 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"); + } + } + } + return; +} +#endif -static struct triple *print_interference_ins( - struct compile_state *state, - struct reg_block *blocks, struct triple_reg_set *live, +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; + FILE *fp = state->dbgout; lr = rstate->lrd[ins->id].lr; - display_triple(stdout, ins); + id = ins->id; + ins->id = rstate->lrd[id].orig_id; + SET_REG(ins->id, lr->color); + display_triple(state->dbgout, ins); + ins->id = id; if (lr->defs) { struct live_range_def *lrd; - printf(" range:"); + fprintf(fp, " range:"); lrd = lr->defs; do { - printf(" %-10p", lrd->def); + fprintf(fp, " %-10p", lrd->def); lrd = lrd->next; } while(lrd != lr->defs); - printf("\n"); + fprintf(fp, "\n"); } if (live) { struct triple_reg_set *entry; - printf(" live:"); + fprintf(fp, " live:"); for(entry = live; entry; entry = entry->next) { - printf(" %-10p", entry->member); + fprintf(fp, " %-10p", entry->member); } - printf("\n"); + fprintf(fp, "\n"); } if (lr->edges) { struct live_range_edge *entry; - printf(" edges:"); + fprintf(fp, " edges:"); for(entry = lr->edges; entry; entry = entry->next) { struct live_range_def *lrd; lrd = entry->node->defs; do { - printf(" %-10p", lrd->def); + fprintf(fp, " %-10p", lrd->def); lrd = lrd->next; } while(lrd != entry->node->defs); - printf("|"); + fprintf(fp, "|"); } - printf("\n"); + fprintf(fp, "\n"); } if (triple_is_branch(state, ins)) { - printf("\n"); + fprintf(fp, "\n"); } - return ins; + return; } static int coalesce_live_ranges( @@ -11875,7 +19106,7 @@ static int coalesce_live_ranges( * 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. + * register pool. * * The two cases I have identified are: * 1) Two forced register assignments may @@ -11893,7 +19124,7 @@ static int coalesce_live_ranges( * functions. This creates a 2 headed live * range that cannot be sanely split. * - * - phi functions (coalesced in initialize_live_ranges) + * - phi functions (coalesced in initialize_live_ranges) * are handled as pre split live ranges so we will * never attempt to split them. */ @@ -11943,11 +19174,11 @@ static int coalesce_live_ranges( 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) { @@ -11962,17 +19193,12 @@ static int coalesce_live_ranges( } -struct coalesce_conflict { - struct triple *ins; - int index; -}; -static struct triple *spot_coalesce_conflict(struct compile_state *state, +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) { - struct coalesce_conflict *conflict = arg; + int *conflicts = arg; int zlhs, zrhs, i, j; - int found; /* See if we have a mandatory coalesce operation between * a lhs and a rhs value. If so and the rhs value is also @@ -11980,411 +19206,405 @@ static struct triple *spot_coalesce_conflict(struct compile_state *state, * we would have two definitions in the same live range simultaneously * alive. */ - found = -1; - zlhs = TRIPLE_LHS(ins->sizes); + zlhs = ins->lhs; if ((zlhs == 0) && triple_is_def(state, ins)) { zlhs = 1; } - zrhs = TRIPLE_RHS(ins->sizes); - for(i = 0; (i < zlhs) && (found == -1); i++) { + zrhs = ins->rhs; + 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) && (found == -1); j++) { + 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; } rhs = RHS(ins, j); - for(set = live; set && (found == -1); set = set->next) { + for(set = live; set && !found; set = set->next) { if (set->member == rhs) { - found = j; + found = 1; } } + if (found) { + struct triple *copy; + copy = pre_copy(state, ins, j); + copy->id |= TRIPLE_FLAG_PRE_SPLIT; + (*conflicts)++; + } } } - /* Only update conflict if we are the least dominated conflict */ - if ((found != -1) && - (!conflict->ins || tdominates(state, ins, conflict->ins))) { - conflict->ins = ins; - conflict->index = found; - } - return ins; + return; } -static void resolve_coalesce_conflict( - struct compile_state *state, struct coalesce_conflict *conflict) +static int correct_coalesce_conflicts( + struct compile_state *state, struct reg_block *blocks) { - struct triple *copy; - copy = pre_copy(state, conflict->ins, conflict->index); - copy->id |= TRIPLE_FLAG_PRE_SPLIT; + int conflicts; + conflicts = 0; + walk_variable_lifetimes(state, &state->bb, blocks, + fix_coalesce_conflicts, &conflicts); + return conflicts; } - -static struct triple *spot_tangle(struct compile_state *state, - struct reg_block *blocks, struct triple_reg_set *live, - struct reg_block *rb, struct triple *ins, void *arg) +static void replace_set_use(struct compile_state *state, + struct triple_reg_set *head, struct triple *orig, struct triple *new) { - struct triple **tangle = arg; - char used[MAX_REGISTERS]; struct triple_reg_set *set; - - /* 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 = find_lhs_color(state, set->member, 0); - if (info.reg == REG_UNSET) { - continue; + for(set = head; set; set = set->next) { + if (set->member == orig) { + set->member = new; } - 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 = find_lhs_color(state, set->member, 0); - if (used[info.reg] < 2) { - continue; - } - if (!*tangle || tdominates(state, set->member, *tangle)) { - *tangle = set->member; +static void replace_block_use(struct compile_state *state, + struct reg_block *blocks, struct triple *orig, struct triple *new) +{ + int i; +#if DEBUG_ROMCC_WARNINGS +#warning "WISHLIST visit just those blocks that need it *" +#endif + for(i = 1; i <= state->bb.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 = state->first; + 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); } - return ins; + else if (index < ins->lhs) { + 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 void resolve_tangle(struct compile_state *state, struct triple *tangle) +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; -#if 0 - fprintf(stderr, "Resolving tangle: %p\n", tangle); - print_blocks(state, stderr); +#if DEBUG_ROMCC_WARNINGS +#warning "WISHLIST recalculate all affected instructions colors" #endif info = find_lhs_color(state, tangle, 0); -#if 0 - fprintf(stderr, "color: %d\n", info.reg); -#endif for(set = tangle->use; set; set = next) { struct triple *user; int i, zrhs; next = set->next; user = set->member; - zrhs = TRIPLE_RHS(user->sizes); + zrhs = user->rhs; for(i = 0; i < zrhs; i++) { if (RHS(user, i) != tangle) { continue; } uinfo = find_rhs_post_color(state, user, i); -#if 0 - fprintf(stderr, "%p rhs %d color: %d\n", - user, i, uinfo.reg); -#endif 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 0 - fprintf(stderr, "pre color: %d\n", uinfo.reg); -#endif 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; } -struct least_conflict { - struct reg_state *rstate; - struct live_range *ref_range; - struct triple *ins; - struct triple_reg_set *live; - size_t count; -}; -static struct triple *least_conflict(struct compile_state *state, +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) { - struct least_conflict *conflict = arg; - struct live_range_edge *edge; - struct triple_reg_set *set; - size_t count; - -#warning "FIXME handle instructions with left hand sides..." - /* Only instructions that introduce a new definition - * can be the conflict instruction. - */ - if (!triple_is_def(state, ins)) { - return ins; - } + int *tangles = arg; + struct triple *tangle; + do { + char used[MAX_REGISTERS]; + struct triple_reg_set *set; + tangle = 0; - /* See if live ranges at this instruction are a - * strict subset of the live ranges that are in conflict. - */ - count = 0; - for(set = live; set; set = set->next) { - struct live_range *lr; - lr = conflict->rstate->lrd[set->member->id].lr; - for(edge = conflict->ref_range->edges; edge; edge = edge->next) { - if (edge->node == lr) { - break; + /* 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); } - if (!edge && (lr != conflict->ref_range)) { - return ins; - } - count++; - } - if (count <= 1) { - return ins; - } - - /* See if there is an uncolored member in this subset. - */ - for(set = live; set; set = set->next) { - struct live_range *lr; - lr = conflict->rstate->lrd[set->member->id].lr; - if (lr->color == REG_UNSET) { - break; - } - } - if (!set && (conflict->ref_range != REG_UNSET)) { - return ins; - } - - /* Find the instruction with the largest possible subset of - * conflict ranges and that dominates any other instruction - * with an equal sized set of conflicting ranges. - */ - if ((count > conflict->count) || - ((count == conflict->count) && - tdominates(state, ins, conflict->ins))) { - struct triple_reg_set *next; - /* Remember the canidate instruction */ - conflict->ins = ins; - conflict->count = count; - /* Free the old collection of live registers */ - for(set = conflict->live; set; set = next) { - next = set->next; - do_triple_unset(&conflict->live, set->member); - } - conflict->live = 0; - /* Rember the registers that are alive but do not feed - * into or out of conflict->ins. + /* Now find the least dominated definition of a register in + * conflict I have seen so far. */ for(set = live; set; set = set->next) { - struct triple **expr; - if (set->member == ins) { - goto next; + struct reg_info info; + info = read_lhs_color(state, set->member, 0); + if (used[info.reg] < 2) { + continue; } - expr = triple_rhs(state, ins, 0); - for(;expr; expr = triple_rhs(state, ins, expr)) { - if (*expr == set->member) { - goto next; - } + /* Changing copies that feed into phi functions + * is incorrect. + */ + if (set->member->use && + (set->member->use->member->op == OP_PHI)) { + continue; } - expr = triple_lhs(state, ins, 0); - for(; expr; expr = triple_lhs(state, ins, expr)) { - if (*expr == set->member) { - goto next; - } + if (!tangle || tdominates(state, set->member, tangle)) { + tangle = set->member; } - do_triple_set(&conflict->live, set->member, set->new); - next: - ; } - } - return ins; + /* 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 void find_range_conflict(struct compile_state *state, - struct reg_state *rstate, char *used, struct live_range *ref_range, - struct least_conflict *conflict) +static int correct_tangles( + struct compile_state *state, struct reg_block *blocks) { - /* there are 3 kinds ways conflicts can occure. - * 1) the life time of 2 values simply overlap. - * 2) the 2 values feed into the same instruction. - * 3) the 2 values feed into a phi function. - */ + int tangles; + tangles = 0; + color_instructions(state); + walk_variable_lifetimes(state, &state->bb, blocks, + fix_tangles, &tangles); + return tangles; +} - /* find the instruction where the problematic conflict comes - * into existance. that the instruction where all of - * the values are alive, and among such instructions it is - * the least dominated one. - * - * a value is alive an an instruction if either; - * 1) the value defintion dominates the instruction and there - * is a use at or after that instrction - * 2) the value definition feeds into a phi function in the - * same block as the instruction. and the phi function - * is at or after the instruction. - */ - memset(conflict, 0, sizeof(*conflict)); - conflict->rstate = rstate; - conflict->ref_range = ref_range; - conflict->ins = 0; - conflict->count = 0; - conflict->live = 0; - walk_variable_lifetimes(state, rstate->blocks, least_conflict, conflict); - if (!conflict->ins) { - internal_error(state, 0, "No conflict ins?"); - } - if (!conflict->live) { - internal_error(state, 0, "No conflict live?"); - } - return; -} +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); -static struct triple *split_constrained_range(struct compile_state *state, - struct reg_state *rstate, char *used, struct least_conflict *conflict) +struct triple *find_constrained_def( + struct compile_state *state, struct live_range *range, struct triple *constrained) { - unsigned constrained_size; - struct triple *new, *constrained; - struct triple_reg_set *cset; - /* Find a range that is having problems because it is - * artificially constrained. - */ - constrained_size = ~0; - constrained = 0; - new = 0; - for(cset = conflict->live; cset; cset = cset->next) { - struct triple_set *set; + struct live_range_def *lrd, *lrd_next; + lrd_next = range->defs; + do { struct reg_info info; - unsigned classes; - unsigned cur_size, size; - /* Skip the live range that starts with conflict->ins */ - if (cset->member == conflict->ins) { - continue; - } - /* Find how many registers this value can potentially - * be assigned to. - */ - classes = arch_type_to_regcm(state, cset->member->type); - size = regc_max_size(state, classes); + unsigned regcm; - /* Find how many registers we allow this value to - * be assigned to. + lrd = lrd_next; + lrd_next = lrd->next; + + 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 equal then + * the current register class is not constrained. */ - info = arch_reg_lhs(state, cset->member, 0); -#warning "FIXME do I need a call to arch_reg_rhs around here somewhere?" - if ((info.reg == REG_UNSET) || (info.reg >= MAX_REGISTERS)) { - cur_size = regc_max_size(state, info.regcm); - } else { - cur_size = 1; + if (regcm == info.regcm) { + continue; } - /* If this live_range feeds into conflict->ins - * splitting it is unlikely to help. + + /* If there is just one use. + * That use cannot accept a larger register class. + * There are no intervening definitions except + * definitions that feed into that use. + * Then a triple is not constrained. + * FIXME handle this case! */ - for(set = cset->member->use; set; set = set->next) { - if (set->member == conflict->ins) { - goto next; - } - } +#if DEBUG_ROMCC_WARNINGS +#warning "FIXME ignore cases that cannot be fixed (a definition followed by a use)" +#endif + - /* If there is no difference between potential and - * actual register count there is nothing to do. + /* Of the constrained live ranges deal with the + * least dominated one first. */ - if (cur_size >= size) { - continue; + if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) { + fprintf(state->errout, "canidate: %p %-8s regcm: %x %x\n", + lrd->def, tops(lrd->def->op), regcm, info.regcm); } - /* Of the constrained registers deal with the - * most constrained one first. - */ if (!constrained || - (size < constrained_size)) { - constrained = cset->member; - constrained_size = size; + tdominates(state, lrd->def, constrained)) + { + constrained = lrd->def; } - next: - ; + } while(lrd_next != 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 DEBUG_ROMCC_WARNINGS +#warning "FIXME should I call find_constrained_def here only if no previous constrained def was found?" +#endif + if (!constrained) { + constrained = find_constrained_def(state, range, constrained); + } + + if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) { + fprintf(state->errout, "constrained: "); + display_triple(state->errout, constrained); } if (constrained) { - new = post_copy(state, constrained); - new->id |= TRIPLE_FLAG_POST_SPLIT; + 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 (state->compiler->debug & DEBUG_RANGE_CONFLICTS) { + fprintf(state->errout, "split_ranges %d %s %p\n", + rstate->passes, tops(range->defs->def->op), range->defs->def); + } + 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. + * + */ +#if DEBUG_ROMCC_WARNINGS +#warning "WISHLIST implement live range splitting..." +#endif + + if (!split && (state->compiler->debug & DEBUG_RANGE_CONFLICTS2)) { + FILE *fp = state->errout; + print_interference_blocks(state, rstate, fp, 0); + print_dominators(state, fp, &state->bb); + } + return split; +} + +static FILE *cgdebug_fp(struct compile_state *state) +{ + FILE *fp; + fp = 0; + if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH2)) { + fp = state->errout; + } + if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH)) { + fp = state->dbgout; } - return new; + return fp; } -static int split_ranges( - struct compile_state *state, struct reg_state *rstate, - char *used, struct live_range *range) +static void cgdebug_printf(struct compile_state *state, const char *fmt, ...) { - struct triple *new; - - if ((range->color == REG_UNNEEDED) || - (rstate->passes >= rstate->max_passes)) { - return 0; + FILE *fp; + fp = cgdebug_fp(state); + if (fp) { + va_list args; + va_start(args, fmt); + vfprintf(fp, fmt, args); + va_end(args); } - new = 0; - /* If I can't allocate a register something needs to be split */ - if (arch_select_free_register(state, used, range->classes) == REG_UNSET) { - struct least_conflict conflict; +} - /* Find where in the set of registers the conflict - * actually occurs. - */ - find_range_conflict(state, rstate, used, range, &conflict); - - /* If a range has been artifically constrained split it */ - new = split_constrained_range(state, rstate, used, &conflict); - - if (!new) { - /* 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..." - return 0; - } - } - if (new) { - rstate->lrd[rstate->defs].orig_id = new->id; - new->id = rstate->defs; - rstate->defs++; -#if 0 - fprintf(stderr, "new: %p\n", new); -#endif - return 1; +static void cgdebug_flush(struct compile_state *state) +{ + FILE *fp; + fp = cgdebug_fp(state); + if (fp) { + fflush(fp); } - return 0; } -#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 cgdebug_loc(struct compile_state *state, struct triple *ins) +{ + FILE *fp; + fp = cgdebug_fp(state); + if (fp) { + loc(fp, state, ins); + } +} - -static int select_free_color(struct compile_state *state, +static int select_free_color(struct compile_state *state, struct reg_state *rstate, struct live_range *range) { struct triple_set *entry; @@ -12407,26 +19627,24 @@ static int select_free_color(struct compile_state *state, } reg_fill_used(state, used, edge->node->color); } -#if DEBUG_COLOR_GRAPH > 1 - { + + if (state->compiler->debug & DEBUG_COLOR_GRAPH2) { 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); + cgdebug_printf(state, "\n%s edges: %d", + tops(range->defs->def->op), i); + cgdebug_loc(state, range->defs->def); + cgdebug_printf(state, "\n"); for(i = 0; i < MAX_REGISTERS; i++) { if (used[i]) { - cgdebug_printf("used: %s\n", + cgdebug_printf(state, "used: %s\n", arch_reg_str(i)); } } - } -#endif - -#warning "FIXME detect conflicts caused by the source and destination being the same register" + } /* If a color is already assigned see if it will work */ if (range->color != REG_UNSET) { @@ -12468,6 +19686,7 @@ static int select_free_color(struct compile_state *state, 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; @@ -12476,8 +19695,11 @@ static int select_free_color(struct compile_state *state, !interfere(rstate, range, insd->lr)) { phi = insd; } - if ((insd->lr->color == REG_UNSET) || - ((insd->lr->classes & range->classes) == 0) || + if (insd->lr->color == REG_UNSET) { + continue; + } + regcm = insd->lr->classes; + if (((regcm & range->classes) == 0) || (used[insd->lr->color])) { continue; } @@ -12500,12 +19722,16 @@ static int select_free_color(struct compile_state *state, 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) || - ((lr->classes & range->classes) == 0) || + if (lr->color == REG_UNSET) { + continue; + } + regcm = lr->classes; + if (((regcm & range->classes) == 0) || (used[lr->color])) { continue; } @@ -12522,12 +19748,16 @@ static int select_free_color(struct compile_state *state, 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 == -1) || - ((lr->classes & range->classes) == 0) || + if (lr->color == REG_UNSET) { + continue; + } + regcm = lr->classes; + if (((regcm & range->classes) == 0) || (used[lr->color])) { continue; } @@ -12542,21 +19772,33 @@ static int select_free_color(struct compile_state *state, * pick the first color that is free. */ if (range->color == REG_UNSET) { - range->color = + range->color = 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->defs->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->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++) { @@ -12565,14 +19807,10 @@ static int select_free_color(struct compile_state *state, arch_reg_str(i)); } } -#if DEBUG_COLOR_GRAPH < 2 error(state, range->defs->def, "too few registers"); -#else - internal_error(state, range->defs->def, "too few registers"); -#endif } - range->classes = arch_reg_regcm(state, range->color); - if (range->color == -1) { + 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; @@ -12584,7 +19822,7 @@ static int color_graph(struct compile_state *state, struct reg_state *rstate) struct live_range_edge *edge; struct live_range *range; if (rstate->low) { - cgdebug_printf("Lo: "); + cgdebug_printf(state, "Lo: "); range = rstate->low; if (*range->group_prev != range) { internal_error(state, 0, "lo: *prev != range?"); @@ -12601,7 +19839,7 @@ static int color_graph(struct compile_state *state, struct reg_state *rstate) } } else if (rstate->high) { - cgdebug_printf("Hi: "); + cgdebug_printf(state, "Hi: "); range = rstate->high; if (*range->group_prev != range) { internal_error(state, 0, "hi: *prev != range?"); @@ -12620,7 +19858,7 @@ static int color_graph(struct compile_state *state, struct reg_state *rstate) else { return 1; } - cgdebug_printf(" %d\n", range - rstate->lr); + cgdebug_printf(state, " %d\n", range - rstate->lr); range->group_prev = 0; for(edge = range->edges; edge; edge = edge->next) { struct live_range *node; @@ -12638,7 +19876,7 @@ static int color_graph(struct compile_state *state, struct reg_state *rstate) if (&node->group_next == rstate->high_tail) { rstate->high_tail = node->group_prev; } - cgdebug_printf("Moving...%d to low\n", node - rstate->lr); + cgdebug_printf(state, "Moving...%d to low\n", node - rstate->lr); node->group_prev = rstate->low_tail; node->group_next = 0; *rstate->low_tail = node; @@ -12651,12 +19889,13 @@ static int color_graph(struct compile_state *state, struct reg_state *rstate) } colored = color_graph(state, rstate); if (colored) { - cgdebug_printf("Coloring %d @%s:%d.%d:", - range - rstate->lr, - range->def->filename, range->def->line, range->def->col); - cgdebug_flush(); + cgdebug_printf(state, "Coloring %d @", range - rstate->lr); + cgdebug_loc(state, range->defs->def); + cgdebug_flush(state); colored = select_free_color(state, rstate, range); - cgdebug_printf(" %s\n", arch_reg_str(range->color)); + if (colored) { + cgdebug_printf(state, " %s\n", arch_reg_str(range->color)); + } } return colored; } @@ -12667,12 +19906,12 @@ static void verify_colors(struct compile_state *state, struct reg_state *rstate) struct live_range_edge *edge; struct triple *ins, *first; char used[MAX_REGISTERS]; - first = RHS(state->main_function, 0); + first = state->first; ins = first; do { if (triple_is_def(state, ins)) { if ((ins->id < 0) || (ins->id > rstate->defs)) { - internal_error(state, ins, + internal_error(state, ins, "triple without a live range def"); } lr = rstate->lrd[ins->id].lr; @@ -12700,168 +19939,24 @@ static void verify_colors(struct compile_state *state, struct reg_state *rstate) static void color_triples(struct compile_state *state, struct reg_state *rstate) { + struct live_range_def *lrd; struct live_range *lr; struct triple *first, *ins; - first = RHS(state->main_function, 0); + first = state->first; ins = first; do { if ((ins->id < 0) || (ins->id > rstate->defs)) { - internal_error(state, ins, + internal_error(state, ins, "triple without a live range"); } - lr = rstate->lrd[ins->id].lr; + lrd = &rstate->lrd[ins->id]; + lr = lrd->lr; + ins->id = lrd->orig_id; SET_REG(ins->id, lr->color); ins = ins->next; } while (ins != 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 = &RHS(ptr, 0); - 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; - unsigned id; - int op; - op = ptr->op; - done = (ptr == block->last); - lr = rstate->lrd[ptr->id].lr; - - if (triple_stores_block(state, ptr)) { - 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) { - if (!user->member->u.block) { - internal_error(state, user->member, - "Use %p not in a block?", - user->member); - } - - } - } - id = ptr->id; - SET_REG(ptr->id, lr->color); - display_triple(stdout, ptr); - ptr->id = id; - - if (triple_is_def(state, ptr) && (lr->defs == 0)) { - internal_error(state, ptr, "lr has no defs!"); - } - - 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 (lr->edges > 0) { - struct live_range_edge *edge; - printf(" edges:"); - for(edge = lr->edges; edge; edge = edge->next) { - struct live_range_def *lrd; - lrd = edge->node->defs; - do { - printf(" %-10p", lrd->def); - lrd = lrd->next; - } while(lrd != edge->node->defs); - printf("|"); - } - printf("\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); - } - for(user = ptr->use; user; user = user->next) { - struct triple *use; - struct live_range *ulr; - use = user->member; - valid_ins(state, use); - if ((use->id < 0) || (use->id > rstate->defs)) { - internal_error(state, use, "Invalid triple id: %d", - use->id); - } - ulr = rstate->lrd[user->member->id].lr; - if (triple_stores_block(state, user->member) && - !user->member->u.block) { - internal_error(state, user->member, - "Use %p not in a block?", - user->member); - } - } - } - 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); - } - printf("\n"); - } - printf("\n"); -} - static struct live_range *merge_sort_lr( struct live_range *first, struct live_range *last) { @@ -12872,7 +19967,7 @@ static struct live_range *merge_sort_lr( mid = first + size/2; first = merge_sort_lr(first, mid -1); mid = merge_sort_lr(mid, last); - + join = 0; join_tail = &join; /* merge the two lists */ @@ -12901,7 +19996,7 @@ static struct live_range *merge_sort_lr( /* Splice the remaining list */ pick = (first)? first : mid; *join_tail = pick; - if (pick) { + if (pick) { pick->group_prev = join_tail; } } @@ -12914,7 +20009,7 @@ static struct live_range *merge_sort_lr( return join; } -static void ids_from_rstate(struct compile_state *state, +static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate) { struct triple *ins, *first; @@ -12922,11 +20017,13 @@ static void ids_from_rstate(struct compile_state *state, return; } /* Display the graph if desired */ - if (state->debug & DEBUG_INTERFERENCE) { - print_blocks(state, stdout); - print_control_flow(state); + if (state->compiler->debug & DEBUG_INTERFERENCE) { + FILE *fp = state->dbgout; + print_interference_blocks(state, rstate, fp, 0); + print_control_flow(state, fp, &state->bb); + fflush(fp); } - first = RHS(state->main_function, 0); + first = state->first; ins = first; do { if (ins->id) { @@ -12955,7 +20052,7 @@ static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate /* Free the variable lifetime information */ if (rstate->blocks) { - free_variable_lifetimes(state, rstate->blocks); + free_variable_lifetimes(state, &state->bb, rstate->blocks); } rstate->defs = 0; rstate->ranges = 0; @@ -12964,6 +20061,7 @@ static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate rstate->blocks = 0; } +static void verify_consistency(struct compile_state *state); static void allocate_registers(struct compile_state *state) { struct reg_state rstate; @@ -12971,85 +20069,95 @@ static void allocate_registers(struct compile_state *state) /* Clear out the reg_state */ memset(&rstate, 0, sizeof(rstate)); - rstate.max_passes = MAX_ALLOCATION_PASSES; + rstate.max_passes = state->compiler->max_allocation_passes; do { struct live_range **point, **next; - struct triple *tangle; - struct coalesce_conflict conflict; + int tangles; int coalesced; + if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) { + FILE *fp = state->errout; + fprintf(fp, "pass: %d\n", rstate.passes); + fflush(fp); + } + /* Restore ids */ ids_from_rstate(state, &rstate); - do { - /* Cleanup the temporary data structures */ - cleanup_rstate(state, &rstate); + /* Cleanup the temporary data structures */ + cleanup_rstate(state, &rstate); - /* Compute the variable lifetimes */ - rstate.blocks = compute_variable_lifetimes(state); + /* Compute the variable lifetimes */ + rstate.blocks = compute_variable_lifetimes(state, &state->bb); - /* Find an invalid mandatory live range coalesce */ - conflict.ins = 0; - conflict.index = -1; - walk_variable_lifetimes( - state, rstate.blocks, spot_coalesce_conflict, &conflict); - - /* If a tangle was found resolve it */ - if (conflict.ins) { - resolve_coalesce_conflict(state, &conflict); - } - } while(conflict.ins); + /* Fix invalid mandatory live range coalesce conflicts */ + correct_coalesce_conflicts(state, rstate.blocks); + /* 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. + */ do { - /* Cleanup the temporary data structures */ - cleanup_rstate(state, &rstate); - - /* Compute the variable lifetimes */ - rstate.blocks = compute_variable_lifetimes(state); + tangles = correct_tangles(state, rstate.blocks); + } while(tangles); - /* Find two simultaneous uses of the same register */ - tangle = 0; - walk_variable_lifetimes( - state, rstate.blocks, spot_tangle, &tangle); - - /* If a tangle was found resolve it */ - if (tangle) { - resolve_tangle(state, tangle); - } - } while(tangle); - if (state->debug & DEBUG_INSERTED_COPIES) { - printf("After resolve_tangles\n"); - print_blocks(state, stdout); - print_control_flow(state); - } + print_blocks(state, "resolve_tangles", state->dbgout); + verify_consistency(state); - /* Allocate and initialize the live ranges */ initialize_live_ranges(state, &rstate); - + + /* Note currently 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 { - /* Forget previous live range edge calculations */ + if (state->compiler->debug & DEBUG_COALESCING) { + fprintf(state->errout, "coalescing\n"); + } + + /* Remove any previous live edge calculations */ cleanup_live_edges(&rstate); /* Compute the interference graph */ walk_variable_lifetimes( - state, rstate.blocks, graph_ins, &rstate); - + state, &state->bb, rstate.blocks, + graph_ins, &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"); + if (state->compiler->debug & DEBUG_INTERFERENCE) { + print_interference_blocks(state, &rstate, state->dbgout, 1); + fprintf(state->dbgout, "\nlive variables by instruction\n"); walk_variable_lifetimes( - state, rstate.blocks, + state, &state->bb, rstate.blocks, print_interference_ins, &rstate); } - + coalesced = coalesce_live_ranges(state, &rstate); + + if (state->compiler->debug & DEBUG_COALESCING) { + fprintf(state->errout, "coalesced: %d\n", coalesced); + } } while(coalesced); - + +#if DEBUG_CONSISTENCY > 1 +# if 0 + fprintf(state->errout, "verify_graph_ins...\n"); +# endif + /* Verify the interference graph */ + walk_variable_lifetimes( + state, &state->bb, rstate.blocks, + verify_graph_ins, &rstate); +# if 0 + fprintf(state->errout, "verify_graph_ins done\n"); +#endif +#endif + /* Build the groups low and high. But with the nodes * first sorted by degree order. */ @@ -13069,13 +20177,13 @@ static void allocate_registers(struct compile_state *state) 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", + cgdebug_printf(state, "Lo: %5d degree %5d%s\n", range - rstate.lr, range->degree, (range->color != REG_UNSET) ? " (colored)": ""); *range->group_prev = range->group_next; @@ -13092,7 +20200,7 @@ static void allocate_registers(struct compile_state *state) next = point; } else { - cgdebug_printf("hi: %5d degree %5d%s\n", + cgdebug_printf(state, "hi: %5d degree %5d%s\n", range - rstate.lr, range->degree, (range->color != REG_UNSET) ? " (colored)": ""); } @@ -13110,6 +20218,9 @@ static void allocate_registers(struct compile_state *state) /* Cleanup the temporary data structures */ cleanup_rstate(state, &rstate); + + /* Display the new graph */ + print_blocks(state, __func__, state->dbgout); } /* Sparce Conditional Constant Propogation @@ -13123,9 +20234,9 @@ struct lattice_node { struct ssa_edge *out; struct flow_block *fblock; struct triple *val; - /* lattice high val && !is_const(val) + /* lattice high val == def * lattice const is_const(val) - * lattice low val == 0 + * lattice low other */ }; struct ssa_edge { @@ -13144,11 +20255,12 @@ struct flow_edge { struct flow_edge *out_next; int executable; }; +#define MAX_FLOW_BLOCK_EDGES 3 struct flow_block { struct block *block; struct flow_edge *in; struct flow_edge *out; - struct flow_edge left, right; + struct flow_edge *edges; }; struct scc_state { @@ -13161,9 +20273,45 @@ struct scc_state { }; -static void scc_add_fedge(struct compile_state *state, struct scc_state *scc, +static int is_scc_const(struct compile_state *state, struct triple *ins) +{ + return ins && (triple_is_ubranch(state, ins) || is_const(ins)); +} + +static int is_lattice_hi(struct compile_state *state, struct lattice_node *lnode) +{ + return !is_scc_const(state, lnode->val) && (lnode->val == lnode->def); +} + +static int is_lattice_const(struct compile_state *state, struct lattice_node *lnode) +{ + return is_scc_const(state, lnode->val); +} + +static int is_lattice_lo(struct compile_state *state, struct lattice_node *lnode) +{ + return (lnode->val != lnode->def) && !is_scc_const(state, lnode->val); +} + +static void scc_add_fedge(struct compile_state *state, struct scc_state *scc, struct flow_edge *fedge) { + if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) { + fprintf(state->errout, "adding fedge: %p (%4d -> %5d)\n", + fedge, + fedge->src->block?fedge->src->block->last->id: 0, + fedge->dst->block?fedge->dst->block->first->id: 0); + } + if ((fedge == scc->flow_work_list) || + (fedge->work_next != fedge) || + (fedge->work_prev != fedge)) { + + if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) { + fprintf(state->errout, "dupped fedge: %p\n", + fedge); + } + return; + } if (!scc->flow_work_list) { scc->flow_work_list = fedge; fedge->work_next = fedge->work_prev = fedge; @@ -13191,6 +20339,7 @@ static struct flow_edge *scc_next_fedge( } else { scc->flow_work_list = 0; } + fedge->work_next = fedge->work_prev = fedge; } return fedge; } @@ -13198,6 +20347,22 @@ static struct flow_edge *scc_next_fedge( static void scc_add_sedge(struct compile_state *state, struct scc_state *scc, struct ssa_edge *sedge) { + if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) { + fprintf(state->errout, "adding sedge: %5ld (%4d -> %5d)\n", + (long)(sedge - scc->ssa_edges), + sedge->src->def->id, + sedge->dst->def->id); + } + if ((sedge == scc->ssa_work_list) || + (sedge->work_next != sedge) || + (sedge->work_prev != sedge)) { + + if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) { + fprintf(state->errout, "dupped sedge: %5ld\n", + (long)(sedge - scc->ssa_edges)); + } + return; + } if (!scc->ssa_work_list) { scc->ssa_work_list = sedge; sedge->work_next = sedge->work_prev = sedge; @@ -13225,6 +20390,7 @@ static struct ssa_edge *scc_next_sedge( } else { scc->ssa_work_list = 0; } + sedge->work_next = sedge->work_prev = sedge; } return sedge; } @@ -13241,7 +20407,7 @@ static void initialize_scc_state( memset(scc, 0, sizeof(*scc)); /* Inialize pass zero find out how much memory we need */ - first = RHS(state->main_function, 0); + first = state->first; ins = first; ins_count = ssa_edge_count = 0; do { @@ -13252,17 +20418,17 @@ static void initialize_scc_state( } ins = ins->next; } while(ins != first); -#if DEBUG_SCC - fprintf(stderr, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n", - ins_count, ssa_edge_count, state->last_vertex); -#endif + if (state->compiler->debug & DEBUG_SCC_TRANSFORM) { + fprintf(state->errout, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n", + ins_count, ssa_edge_count, state->bb.last_vertex); + } scc->ins_count = ins_count; - scc->lattice = + scc->lattice = xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice"); - scc->ssa_edges = + scc->ssa_edges = xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges"); - scc->flow_blocks = - xcmalloc(sizeof(*scc->flow_blocks)*(state->last_vertex + 1), + scc->flow_blocks = + xcmalloc(sizeof(*scc->flow_blocks)*(state->bb.last_vertex + 1), "flow_blocks"); /* Initialize pass one collect up the nodes */ @@ -13280,6 +20446,8 @@ static void initialize_scc_state( block->vertex = fblock_index; fblock = &scc->flow_blocks[fblock_index]; fblock->block = block; + fblock->edges = xcmalloc(sizeof(*fblock->edges)*block->edge_count, + "flow_edges"); } { struct lattice_node *lnode; @@ -13289,6 +20457,9 @@ static void initialize_scc_state( lnode->out = 0; lnode->fblock = fblock; lnode->val = ins; /* LATTICE HIGH */ + if (lnode->val->op == OP_UNKNOWNVAL) { + lnode->val = 0; /* LATTICE LOW by definition */ + } lnode->old_id = ins->id; ins->id = ins_index; } @@ -13299,6 +20470,25 @@ static void initialize_scc_state( fblock = 0; ins = first; do { + { + struct triple_set *edge; + struct ssa_edge **stail; + struct lattice_node *lnode; + lnode = &scc->lattice[ins->id]; + lnode->out = 0; + stail = &lnode->out; + for(edge = ins->use; edge; edge = edge->next) { + struct ssa_edge *sedge; + ssa_edge_index += 1; + sedge = &scc->ssa_edges[ssa_edge_index]; + *stail = sedge; + stail = &sedge->out_next; + sedge->src = lnode; + sedge->dst = &scc->lattice[edge->member->id]; + sedge->work_next = sedge->work_prev = sedge; + sedge->out_next = 0; + } + } if ((ins->op == OP_LABEL) && (block != ins->u.block)) { struct flow_edge *fedge, **ftail; struct block_set *bedge; @@ -13307,64 +20497,53 @@ static void initialize_scc_state( fblock->in = 0; fblock->out = 0; ftail = &fblock->out; - if (block->left) { - fblock->left.dst = &scc->flow_blocks[block->left->vertex]; - if (fblock->left.dst->block != block->left) { - internal_error(state, 0, "block mismatch"); - } - fblock->left.out_next = 0; - *ftail = &fblock->left; - ftail = &fblock->left.out_next; - } - if (block->right) { - fblock->right.dst = &scc->flow_blocks[block->right->vertex]; - if (fblock->right.dst->block != block->right) { + + fedge = fblock->edges; + bedge = block->edges; + for(; bedge; bedge = bedge->next, fedge++) { + fedge->dst = &scc->flow_blocks[bedge->member->vertex]; + if (fedge->dst->block != bedge->member) { internal_error(state, 0, "block mismatch"); } - fblock->right.out_next = 0; - *ftail = &fblock->right; - ftail = &fblock->right.out_next; + *ftail = fedge; + ftail = &fedge->out_next; + fedge->out_next = 0; } for(fedge = fblock->out; fedge; fedge = fedge->out_next) { fedge->src = fblock; fedge->work_next = fedge->work_prev = fedge; fedge->executable = 0; } + } + ins = ins->next; + } while (ins != first); + block = 0; + fblock = 0; + ins = first; + do { + if ((ins->op == OP_LABEL) && (block != ins->u.block)) { + struct flow_edge **ftail; + struct block_set *bedge; + block = ins->u.block; + fblock = &scc->flow_blocks[block->vertex]; ftail = &fblock->in; for(bedge = block->use; bedge; bedge = bedge->next) { - struct block *src_block; - struct flow_block *sfblock; - struct flow_edge *sfedge; - src_block = bedge->member; - sfblock = &scc->flow_blocks[src_block->vertex]; - sfedge = 0; - if (src_block->left == block) { - sfedge = &sfblock->left; - } else { - sfedge = &sfblock->right; - } - *ftail = sfedge; - ftail = &sfedge->in_next; - sfedge->in_next = 0; - } - } - { - struct triple_set *edge; - struct ssa_edge **stail; - struct lattice_node *lnode; - lnode = &scc->lattice[ins->id]; - lnode->out = 0; - stail = &lnode->out; - for(edge = ins->use; edge; edge = edge->next) { - struct ssa_edge *sedge; - ssa_edge_index += 1; - sedge = &scc->ssa_edges[ssa_edge_index]; - *stail = sedge; - stail = &sedge->out_next; - sedge->src = lnode; - sedge->dst = &scc->lattice[edge->member->id]; - sedge->work_next = sedge->work_prev = sedge; - sedge->out_next = 0; + struct block *src_block; + struct flow_block *sfblock; + struct flow_edge *sfedge; + src_block = bedge->member; + sfblock = &scc->flow_blocks[src_block->vertex]; + for(sfedge = sfblock->out; sfedge; sfedge = sfedge->out_next) { + if (sfedge->dst == fblock) { + break; + } + } + if (!sfedge) { + internal_error(state, 0, "edge mismatch"); + } + *ftail = sfedge; + ftail = &sfedge->in_next; + sfedge->in_next = 0; } } ins = ins->next; @@ -13375,10 +20554,11 @@ static void initialize_scc_state( struct flow_edge *fedge; fblock = &scc->flow_blocks[0]; fblock->block = 0; + fblock->edges = xcmalloc(sizeof(*fblock->edges)*1, "flow_edges"); fblock->in = 0; - fblock->out = &fblock->left; - dst = &scc->flow_blocks[state->first_block->vertex]; - fedge = &fblock->left; + fblock->out = fblock->edges; + dst = &scc->flow_blocks[state->bb.first_block->vertex]; + fedge = fblock->edges; fedge->src = fblock; fedge->dst = dst; fedge->work_next = fedge; @@ -13387,26 +20567,35 @@ static void initialize_scc_state( fedge->out_next = 0; fedge->executable = 0; fedge->dst->in = fedge; - + /* Initialize the work lists */ scc->flow_work_list = 0; scc->ssa_work_list = 0; scc_add_fedge(state, scc, fedge); } -#if DEBUG_SCC - fprintf(stderr, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n", - ins_index, ssa_edge_index, fblock_index); -#endif + if (state->compiler->debug & DEBUG_SCC_TRANSFORM) { + fprintf(state->errout, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n", + ins_index, ssa_edge_index, fblock_index); + } } - + static void free_scc_state( struct compile_state *state, struct scc_state *scc) { + int i; + for(i = 0; i < state->bb.last_vertex + 1; i++) { + struct flow_block *fblock; + fblock = &scc->flow_blocks[i]; + if (fblock->edges) { + xfree(fblock->edges); + fblock->edges = 0; + } + } xfree(scc->flow_blocks); xfree(scc->ssa_edges); xfree(scc->lattice); - + } static struct lattice_node *triple_to_lattice( @@ -13435,7 +20624,7 @@ static struct triple *preserve_lval( return old; } -static int lval_changed(struct compile_state *state, +static int lval_changed(struct compile_state *state, struct triple *old, struct lattice_node *lnode) { int changed; @@ -13444,13 +20633,10 @@ static int lval_changed(struct compile_state *state, 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) && + TRIPLE_SIZE(lnode->val) * sizeof(lnode->val->param[0])) == 0) && (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) { changed = 0; } @@ -13461,63 +20647,36 @@ static int lval_changed(struct compile_state *state, } -static void scc_visit_phi(struct compile_state *state, struct scc_state *scc, - struct lattice_node *lnode) +static void scc_debug_lnode( + struct compile_state *state, struct scc_state *scc, + struct lattice_node *lnode, int changed) { - struct lattice_node *tmp; - struct triple **slot, *old; - struct flow_edge *fedge; - int index; - if (lnode->def->op != OP_PHI) { - internal_error(state, lnode->def, "not phi"); + if ((state->compiler->debug & DEBUG_SCC_TRANSFORM2) && lnode->val) { + display_triple_changes(state->errout, lnode->val, lnode->def); } - /* Store the original value */ - old = preserve_lval(state, lnode); - - /* default to lattice high */ - lnode->val = lnode->def; - slot = &RHS(lnode->def, 0); - index = 0; - for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) { - if (!fedge->executable) { - continue; - } - if (!slot[index]) { - internal_error(state, lnode->def, "no phi value"); - } - tmp = triple_to_lattice(state, scc, slot[index]); - /* meet(X, lattice low) = lattice low */ - if (!tmp->val) { - lnode->val = 0; - } - /* meet(X, lattice high) = X */ - else if (!tmp->val) { - lnode->val = lnode->val; - } - /* meet(lattice high, X) = X */ - else if (!is_const(lnode->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)) { - lnode->val = 0; - } - if (!lnode->val) { - break; + if (state->compiler->debug & DEBUG_SCC_TRANSFORM) { + FILE *fp = state->errout; + struct triple *val, **expr; + val = lnode->val? lnode->val : lnode->def; + fprintf(fp, "%p %s %3d %10s (", + lnode->def, + ((lnode->def->op == OP_PHI)? "phi: ": "expr:"), + lnode->def->id, + tops(lnode->def->op)); + expr = triple_rhs(state, lnode->def, 0); + for(;expr;expr = triple_rhs(state, lnode->def, expr)) { + if (*expr) { + fprintf(fp, " %d", (*expr)->id); + } } - } -#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); + if (val->op == OP_INTCONST) { + fprintf(fp, " <0x%08lx>", (unsigned long)(val->u.cval)); } + fprintf(fp, " ) -> %s %s\n", + (is_lattice_hi(state, lnode)? "hi": + is_lattice_const(state, lnode)? "const" : "lo"), + changed? "changed" : "" + ); } } @@ -13528,7 +20687,7 @@ static int compute_lnode_val(struct compile_state *state, struct scc_state *scc, struct triple *old, *scratch; struct triple **dexpr, **vexpr; int count, i; - + /* Store the original value */ old = preserve_lval(state, lnode); @@ -13539,24 +20698,26 @@ static int compute_lnode_val(struct compile_state *state, struct scc_state *scc, scratch->prev = scratch; scratch->use = 0; - count = TRIPLE_SIZE(scratch->sizes); + count = TRIPLE_SIZE(scratch); 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))) && + if (((i < TRIPLE_MISC_OFF(scratch)) || + (i >= TRIPLE_TARG_OFF(scratch))) && *dexpr) { struct lattice_node *tmp; tmp = triple_to_lattice(state, scc, *dexpr); *vexpr = (tmp->val)? tmp->val : tmp->def; } } - if (scratch->op == OP_BRANCH) { + if (triple_is_branch(state, scratch)) { scratch->next = lnode->def->next; } /* Recompute the value */ +#if DEBUG_ROMCC_WARNINGS #warning "FIXME see if simplify does anything bad" +#endif /* So far it looks like only the strength reduction * optimization are things I need to worry about. */ @@ -13567,134 +20728,246 @@ static int compute_lnode_val(struct compile_state *state, struct scc_state *scc, } if ((scratch->prev != scratch) || ((scratch->next != scratch) && - ((lnode->def->op != OP_BRANCH) || + (!triple_is_branch(state, lnode->def) || (scratch->next != lnode->def->next)))) { internal_error(state, lnode->def, "scratch in list?"); } /* undo any uses... */ - count = TRIPLE_SIZE(scratch->sizes); + count = TRIPLE_SIZE(scratch); 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->op == OP_UNKNOWNVAL) { + lnode->val = 0; /* Lattice low by definition */ } - if (lnode->val && + /* Find the case when I am lattice high */ + if (lnode->val && (lnode->val->op == lnode->def->op) && - (memcmp(lnode->val->param, lnode->def->param, + (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; } + /* Only allow lattice high when all of my inputs + * are also lattice high. Occassionally I can + * have constants with a lattice low input, so + * I do not need to check that case. + */ + if (is_lattice_hi(state, lnode)) { + struct lattice_node *tmp; + int rhs; + rhs = lnode->val->rhs; + for(i = 0; i < rhs; i++) { + tmp = triple_to_lattice(state, scc, RHS(lnode->val, i)); + if (!is_lattice_hi(state, tmp)) { + lnode->val = 0; + break; + } + } + } /* Find the cases that are always lattice lo */ - if (lnode->val && + if (lnode->val && triple_is_def(state, lnode->val) && - !triple_is_pure(state, lnode->val)) { + !triple_is_pure(state, lnode->val, lnode->old_id)) { lnode->val = 0; } - if (lnode->val && - (lnode->val->op == OP_SDECL) && - (lnode->val != lnode->def)) { - internal_error(state, lnode->def, "bad sdecl"); - } /* See if the lattice value has changed */ changed = lval_changed(state, old, lnode); + /* See if this value should not change */ + if ((lnode->val != lnode->def) && + (( !triple_is_def(state, lnode->def) && + !triple_is_cbranch(state, lnode->def)) || + (lnode->def->op == OP_PIECE))) { +#if DEBUG_ROMCC_WARNINGS +#warning "FIXME constant propogate through expressions with multiple left hand sides" +#endif + if (changed) { + internal_warning(state, lnode->def, "non def changes value?"); + } + lnode->val = 0; + } + + /* See if we need to free the scratch value */ if (lnode->val != scratch) { xfree(scratch); } + return changed; } -static void scc_visit_branch(struct compile_state *state, struct scc_state *scc, + +static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc, struct lattice_node *lnode) { struct lattice_node *cond; -#if DEBUG_SCC - { + struct flow_edge *left, *right; + int changed; + + /* Update the branch value */ + changed = compute_lnode_val(state, scc, lnode); + scc_debug_lnode(state, scc, lnode, changed); + + /* This only applies to conditional branches */ + if (!triple_is_cbranch(state, lnode->def)) { + internal_error(state, lnode->def, "not a conditional branch"); + } + + if (state->compiler->debug & DEBUG_SCC_TRANSFORM) { struct flow_edge *fedge; - fprintf(stderr, "branch: %d (", + FILE *fp = state->errout; + fprintf(fp, "%s: %d (", + tops(lnode->def->op), lnode->def->id); - + for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) { - fprintf(stderr, " %d", fedge->dst->block->vertex); + fprintf(fp, " %d", fedge->dst->block->vertex); } - fprintf(stderr, " )"); - if (TRIPLE_RHS(lnode->def->sizes) > 0) { - fprintf(stderr, " <- %d", + fprintf(fp, " )"); + if (lnode->def->rhs > 0) { + fprintf(fp, " <- %d", RHS(lnode->def, 0)->id); } - fprintf(stderr, "\n"); + fprintf(fp, "\n"); + } + cond = triple_to_lattice(state, scc, RHS(lnode->def,0)); + for(left = cond->fblock->out; left; left = left->out_next) { + if (left->dst->block->first == lnode->def->next) { + break; + } } -#endif - if (lnode->def->op != OP_BRANCH) { - internal_error(state, lnode->def, "not branch"); + if (!left) { + internal_error(state, lnode->def, "Cannot find left branch edge"); } - /* This only applies to conditional branches */ - if (TRIPLE_RHS(lnode->def->sizes) == 0) { - return; + for(right = cond->fblock->out; right; right = right->out_next) { + if (right->dst->block->first == TARG(lnode->def, 0)) { + break; + } } - 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?"); + if (!right) { + internal_error(state, lnode->def, "Cannot find right branch edge"); + } + /* I should only come here if the controlling expressions value + * has changed, which means it must be either a constant or lo. + */ + if (is_lattice_hi(state, cond)) { + internal_error(state, cond->def, "condition high?"); return; } - if (cond->val == 0) { - scc_add_fedge(state, scc, cond->fblock->out); - scc_add_fedge(state, scc, cond->fblock->out->out_next); + if (is_lattice_lo(state, cond)) { + scc_add_fedge(state, scc, left); + scc_add_fedge(state, scc, right); } else if (cond->val->u.cval) { - scc_add_fedge(state, scc, cond->fblock->out->out_next); - + scc_add_fedge(state, scc, right); } else { - scc_add_fedge(state, scc, cond->fblock->out); + scc_add_fedge(state, scc, left); } } -static void scc_visit_expr(struct compile_state *state, struct scc_state *scc, + +static void scc_add_sedge_dst(struct compile_state *state, + struct scc_state *scc, struct ssa_edge *sedge) +{ + if (triple_is_cbranch(state, sedge->dst->def)) { + scc_visit_cbranch(state, scc, sedge->dst); + } + else if (triple_is_def(state, sedge->dst->def)) { + scc_add_sedge(state, scc, sedge); + } +} + +static void scc_visit_phi(struct compile_state *state, struct scc_state *scc, struct lattice_node *lnode) { + struct lattice_node *tmp; + struct triple **slot, *old; + struct flow_edge *fedge; int changed; + int index; + if (lnode->def->op != OP_PHI) { + internal_error(state, lnode->def, "not phi"); + } + /* Store the original value */ + old = preserve_lval(state, lnode); - changed = compute_lnode_val(state, scc, lnode); -#if DEBUG_SCC - { - struct triple **expr; - fprintf(stderr, "expr: %3d %10s (", - lnode->def->id, tops(lnode->def->op)); - expr = triple_rhs(state, lnode->def, 0); - for(;expr;expr = triple_rhs(state, lnode->def, expr)) { - if (*expr) { - fprintf(stderr, " %d", (*expr)->id); + /* default to lattice high */ + lnode->val = lnode->def; + slot = &RHS(lnode->def, 0); + index = 0; + for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) { + if (state->compiler->debug & DEBUG_SCC_TRANSFORM) { + fprintf(state->errout, "Examining edge: %d vertex: %d executable: %d\n", + index, + fedge->dst->block->vertex, + fedge->executable + ); + } + if (!fedge->executable) { + continue; + } + if (!slot[index]) { + internal_error(state, lnode->def, "no phi value"); + } + tmp = triple_to_lattice(state, scc, slot[index]); + /* meet(X, lattice low) = lattice low */ + if (is_lattice_lo(state, tmp)) { + lnode->val = 0; + } + /* meet(X, lattice high) = X */ + else if (is_lattice_hi(state, tmp)) { + lnode->val = lnode->val; + } + /* meet(lattice high, X) = X */ + else if (is_lattice_hi(state, lnode)) { + lnode->val = dup_triple(state, tmp->val); + /* Only change the type if necessary */ + if (!is_subset_type(lnode->def->type, tmp->val->type)) { + lnode->val->type = lnode->def->type; } } - fprintf(stderr, " ) -> %s\n", - (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi"); + /* meet(const, const) = const or lattice low */ + else if (!constants_equal(state, lnode->val, tmp->val)) { + lnode->val = 0; + } + + /* meet(lattice low, X) = lattice low */ + if (is_lattice_lo(state, lnode)) { + lnode->val = 0; + break; + } } -#endif - if (lnode->def->op == OP_BRANCH) { - scc_visit_branch(state, scc, lnode); + changed = lval_changed(state, old, lnode); + scc_debug_lnode(state, scc, lnode, changed); + + /* If the lattice value has changed update the work lists. */ + if (changed) { + struct ssa_edge *sedge; + for(sedge = lnode->out; sedge; sedge = sedge->out_next) { + scc_add_sedge_dst(state, scc, sedge); + } + } +} + +static void scc_visit_expr(struct compile_state *state, struct scc_state *scc, + struct lattice_node *lnode) +{ + int changed; + + if (!triple_is_def(state, lnode->def)) { + internal_warning(state, lnode->def, "not visiting an expression?"); } - else if (changed) { + changed = compute_lnode_val(state, scc, lnode); + scc_debug_lnode(state, scc, lnode, changed); + + if (changed) { struct ssa_edge *sedge; for(sedge = lnode->out; sedge; sedge = sedge->out_next) { - scc_add_sedge(state, scc, sedge); + scc_add_sedge_dst(state, scc, sedge); } } } @@ -13703,19 +20976,34 @@ static void scc_writeback_values( struct compile_state *state, struct scc_state *scc) { struct triple *first, *ins; - first = RHS(state->main_function, 0); + first = state->first; ins = first; do { struct lattice_node *lnode; lnode = triple_to_lattice(state, scc, ins); + if (state->compiler->debug & DEBUG_SCC_TRANSFORM) { + if (is_lattice_hi(state, lnode) && + (lnode->val->op != OP_NOOP)) + { + struct flow_edge *fedge; + int executable; + executable = 0; + for(fedge = lnode->fblock->in; + !executable && fedge; fedge = fedge->in_next) { + executable |= fedge->executable; + } + if (executable) { + internal_warning(state, lnode->def, + "lattice node %d %s->%s still high?", + ins->id, + tops(lnode->def->op), + tops(lnode->val->op)); + } + } + } + /* Restore id */ ins->id = lnode->old_id; -#if DEBUG_SCC - if (lnode->val && !is_const(lnode->val)) { - warning(state, lnode->def, - "lattice node still high?"); - } -#endif if (lnode->val && (lnode->val != ins)) { /* See if it something I know how to write back */ switch(lnode->val->op) { @@ -13723,7 +21011,7 @@ static void scc_writeback_values( mkconst(state, ins, lnode->val->u.cval); break; case OP_ADDRCONST: - mkaddr_const(state, ins, + mkaddr_const(state, ins, MISC(lnode->val, 0), lnode->val->u.cval); break; default: @@ -13748,6 +21036,9 @@ static void scc_writeback_values( static void scc_transform(struct compile_state *state) { struct scc_state scc; + if (!(state->compiler->flags & COMPILER_SCC_TRANSFORM)) { + return; + } initialize_scc_state(state, &scc); @@ -13759,7 +21050,7 @@ static void scc_transform(struct compile_state *state) struct block *block; struct triple *ptr; struct flow_block *fblock; - int time; + int reps; int done; if (fedge->executable) { continue; @@ -13773,17 +21064,18 @@ static void scc_transform(struct compile_state *state) fedge->executable = 1; fblock = fedge->dst; block = fblock->block; - time = 0; + reps = 0; for(fptr = fblock->in; fptr; fptr = fptr->in_next) { if (fptr->executable) { - time++; + reps++; } } -#if DEBUG_SCC - fprintf(stderr, "vertex: %d time: %d\n", - block->vertex, time); - -#endif + + if (state->compiler->debug & DEBUG_SCC_TRANSFORM) { + fprintf(state->errout, "vertex: %d reps: %d\n", + block->vertex, reps); + } + done = 0; for(ptr = block->first; !done; ptr = ptr->next) { struct lattice_node *lnode; @@ -13792,12 +21084,17 @@ static void scc_transform(struct compile_state *state) if (ptr->op == OP_PHI) { scc_visit_phi(state, &scc, lnode); } - else if (time == 1) { + else if ((reps == 1) && triple_is_def(state, ptr)) + { scc_visit_expr(state, &scc, lnode); } } - if (fblock->out && !fblock->out->out_next) { - scc_add_fedge(state, &scc, fblock->out); + /* Add unconditional branch edges */ + if (!triple_is_cbranch(state, fblock->block->last)) { + struct flow_edge *out; + for(out = fblock->out; out; out = out->out_next) { + scc_add_fedge(state, &scc, out); + } } } while((sedge = scc_next_sedge(state, &scc))) { @@ -13805,12 +21102,14 @@ static void scc_transform(struct compile_state *state) 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 (state->compiler->debug & DEBUG_SCC_TRANSFORM) { + fprintf(state->errout, "sedge: %5ld (%5d -> %5d)\n", + (unsigned long)sedge - (unsigned long)scc.ssa_edges, + sedge->src->def->id, + sedge->dst->def->id); + } + if (lnode->def->op == OP_PHI) { scc_visit_phi(state, &scc, lnode); } @@ -13826,20 +21125,25 @@ static void scc_transform(struct compile_state *state) } } } - + scc_writeback_values(state, &scc); free_scc_state(state, &scc); + rebuild_ssa_form(state); + + print_blocks(state, __func__, state->dbgout); } static void transform_to_arch_instructions(struct compile_state *state) { struct triple *ins, *first; - first = RHS(state->main_function, 0); + first = state->first; ins = first; do { ins = transform_to_arch_instruction(state, ins); } while(ins != first); + + print_blocks(state, __func__, state->dbgout); } #if DEBUG_CONSISTENCY @@ -13847,13 +21151,15 @@ static void verify_uses(struct compile_state *state) { struct triple *first, *ins; struct triple_set *set; - first = RHS(state->main_function, 0); + first = state->first; ins = first; do { struct triple **expr; expr = triple_rhs(state, ins, 0); for(; expr; expr = triple_rhs(state, ins, expr)) { - for(set = *expr?(*expr)->use:0; set; set = set->next) { + struct triple *rhs; + rhs = *expr; + for(set = rhs?rhs->use:0; set; set = set->next) { if (set->member == ins) { break; } @@ -13864,7 +21170,9 @@ static void verify_uses(struct compile_state *state) } expr = triple_lhs(state, ins, 0); for(; expr; expr = triple_lhs(state, ins, expr)) { - for(set = *expr?(*expr)->use:0; set; set = set->next) { + struct triple *lhs; + lhs = *expr; + for(set = lhs?lhs->use:0; set; set = set->next) { if (set->member == ins) { break; } @@ -13873,29 +21181,150 @@ static void verify_uses(struct compile_state *state) internal_error(state, ins, "lhs not used"); } } + expr = triple_misc(state, ins, 0); + if (ins->op != OP_PHI) { + for(; expr; expr = triple_targ(state, ins, expr)) { + struct triple *misc; + misc = *expr; + for(set = misc?misc->use:0; set; set = set->next) { + if (set->member == ins) { + break; + } + } + if (!set) { + internal_error(state, ins, "misc not used"); + } + } + } + if (!triple_is_ret(state, ins)) { + expr = triple_targ(state, ins, 0); + for(; expr; expr = triple_targ(state, ins, expr)) { + struct triple *targ; + targ = *expr; + for(set = targ?targ->use:0; set; set = set->next) { + if (set->member == ins) { + break; + } + } + if (!set) { + internal_error(state, ins, "targ not used"); + } + } + } + ins = ins->next; + } while(ins != first); + +} +static void verify_blocks_present(struct compile_state *state) +{ + struct triple *first, *ins; + if (!state->bb.first_block) { + return; + } + first = state->first; + 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?", ins); + } + } ins = ins->next; } while(ins != first); - + + +} + +static int edge_present(struct compile_state *state, struct block *block, struct triple *edge) +{ + struct block_set *bedge; + struct block *targ; + targ = block_of_triple(state, edge); + for(bedge = block->edges; bedge; bedge = bedge->next) { + if (bedge->member == targ) { + return 1; + } + } + return 0; } + static void verify_blocks(struct compile_state *state) { struct triple *ins; struct block *block; - block = state->first_block; + int blocks; + block = state->bb.first_block; if (!block) { return; } + blocks = 0; do { + int users; + struct block_set *user, *edge; + blocks++; for(ins = block->first; ins != block->last->next; ins = ins->next) { - if (!triple_stores_block(state, ins)) { + 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 (!user->member->first) { + internal_error(state, block->first, "user is empty"); + } + if ((block == state->bb.last_block) && + (user->member == state->bb.first_block)) { continue; } - if (ins->u.block != block) { - internal_error(state, ins, "inconsitent block specified"); + for(edge = user->member->edges; edge; edge = edge->next) { + if (edge->member == block) { + break; + } + } + if (!edge) { + internal_error(state, user->member->first, + "user does not use block"); + } + } + if (triple_is_branch(state, block->last)) { + struct triple **expr; + expr = triple_edge_targ(state, block->last, 0); + for(;expr; expr = triple_edge_targ(state, block->last, expr)) { + if (*expr && !edge_present(state, block, *expr)) { + internal_error(state, block->last, "no edge to targ"); + } } } + if (!triple_is_ubranch(state, block->last) && + (block != state->bb.last_block) && + !edge_present(state, block, block->last->next)) { + internal_error(state, block->last, "no edge to block->last->next"); + } + for(edge = block->edges; edge; edge = edge->next) { + for(user = edge->member->use; user; user = user->next) { + if (user->member == block) { + break; + } + } + if (!user || user->member != block) { + internal_error(state, block->first, + "block does not use edge"); + } + if (!edge->member->first) { + internal_error(state, block->first, "edge block is empty"); + } + } + if (block->users != users) { + internal_error(state, block->first, + "computed users %d != stored users %d", + users, block->users); + } if (!triple_stores_block(state, block->last->next)) { - internal_error(state, block->last->next, + internal_error(state, block->last->next, "cannot find next block"); } block = block->last->next->u.block; @@ -13903,36 +21332,89 @@ static void verify_blocks(struct compile_state *state) internal_error(state, block->last->next, "bad next block"); } - } while(block != state->first_block); + } while(block != state->bb.first_block); + if (blocks != state->bb.last_vertex) { + internal_error(state, 0, "computed blocks: %d != stored blocks %d", + blocks, state->bb.last_vertex); + } } static void verify_domination(struct compile_state *state) { struct triple *first, *ins; struct triple_set *set; - if (!state->first_block) { + if (!state->bb.first_block) { return; } - - first = RHS(state->main_function, 0); + + first = state->first; 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) { + struct triple **slot; + struct triple *use_point; + int i, zrhs; + use_point = 0; + zrhs = set->member->rhs; + slot = &RHS(set->member, 0); + /* See if the use is on the right hand side */ + for(i = 0; i < zrhs; i++) { + if (slot[i] == ins) { break; } } - if (expr && - !tdominates(state, ins, set->member)) { - internal_error(state, set->member, - "non dominated rhs use?"); + if (i < zrhs) { + use_point = set->member; + if (set->member->op == OP_PHI) { + struct block_set *bset; + int edge; + bset = set->member->u.block->use; + for(edge = 0; bset && (edge < i); edge++) { + bset = bset->next; + } + if (!bset) { + internal_error(state, set->member, + "no edge for phi rhs %d", i); + } + use_point = bset->member->last; + } + } + if (use_point && + !tdominates(state, ins, use_point)) { + if (is_const(ins)) { + internal_warning(state, ins, + "non dominated rhs use point %p?", use_point); + } + else { + internal_error(state, ins, + "non dominated rhs use point %p?", use_point); + } + } + } + ins = ins->next; + } while(ins != first); +} + +static void verify_rhs(struct compile_state *state) +{ + struct triple *first, *ins; + first = state->first; + ins = first; + do { + struct triple **slot; + int zrhs, i; + zrhs = ins->rhs; + slot = &RHS(ins, 0); + for(i = 0; i < zrhs; i++) { + if (slot[i] == 0) { + internal_error(state, ins, + "missing rhs %d on %s", + i, tops(ins->op)); + } + if ((ins->op != OP_PHI) && (slot[i] == ins)) { + internal_error(state, ins, + "ins == rhs[%d] on %s", + i, tops(ins->op)); } } ins = ins->next; @@ -13942,15 +21424,12 @@ static void verify_domination(struct compile_state *state) static void verify_piece(struct compile_state *state) { struct triple *first, *ins; - first = RHS(state->main_function, 0); + first = state->first; ins = first; do { struct triple *ptr; int lhs, i; - lhs = TRIPLE_LHS(ins->sizes); - if ((ins->op == OP_WRITE) || (ins->op == OP_STORE)) { - lhs = 0; - } + lhs = ins->lhs; 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", @@ -13968,114 +21447,198 @@ static void verify_piece(struct compile_state *state) ins = ins->next; } while(ins != first); } + static void verify_ins_colors(struct compile_state *state) { struct triple *first, *ins; - - first = RHS(state->main_function, 0); + + first = state->first; + ins = first; + do { + ins = ins->next; + } while(ins != first); +} + +static void verify_unknown(struct compile_state *state) +{ + struct triple *first, *ins; + if ( (unknown_triple.next != &unknown_triple) || + (unknown_triple.prev != &unknown_triple) || +#if 0 + (unknown_triple.use != 0) || +#endif + (unknown_triple.op != OP_UNKNOWNVAL) || + (unknown_triple.lhs != 0) || + (unknown_triple.rhs != 0) || + (unknown_triple.misc != 0) || + (unknown_triple.targ != 0) || + (unknown_triple.template_id != 0) || + (unknown_triple.id != -1) || + (unknown_triple.type != &unknown_type) || + (unknown_triple.occurance != &dummy_occurance) || + (unknown_triple.param[0] != 0) || + (unknown_triple.param[1] != 0)) { + internal_error(state, &unknown_triple, "unknown_triple corrupted!"); + } + if ( (dummy_occurance.count != 2) || + (strcmp(dummy_occurance.filename, __FILE__) != 0) || + (strcmp(dummy_occurance.function, "") != 0) || + (dummy_occurance.col != 0) || + (dummy_occurance.parent != 0)) { + internal_error(state, &unknown_triple, "dummy_occurance corrupted!"); + } + if ( (unknown_type.type != TYPE_UNKNOWN)) { + internal_error(state, &unknown_triple, "unknown_type corrupted!"); + } + first = state->first; ins = first; do { + int params, i; + if (ins == &unknown_triple) { + internal_error(state, ins, "unknown triple in list"); + } + params = TRIPLE_SIZE(ins); + for(i = 0; i < params; i++) { + if (ins->param[i] == &unknown_triple) { + internal_error(state, ins, "unknown triple used!"); + } + } ins = ins->next; } while(ins != first); } + +static void verify_types(struct compile_state *state) +{ + struct triple *first, *ins; + first = state->first; + ins = first; + do { + struct type *invalid; + invalid = invalid_type(state, ins->type); + if (invalid) { + FILE *fp = state->errout; + fprintf(fp, "type: "); + name_of(fp, ins->type); + fprintf(fp, "\n"); + fprintf(fp, "invalid type: "); + name_of(fp, invalid); + fprintf(fp, "\n"); + internal_error(state, ins, "invalid ins type"); + } + } while(ins != first); +} + +static void verify_copy(struct compile_state *state) +{ + struct triple *first, *ins, *next; + first = state->first; + next = ins = first; + do { + ins = next; + next = ins->next; + if (ins->op != OP_COPY) { + continue; + } + if (!equiv_types(ins->type, RHS(ins, 0)->type)) { + FILE *fp = state->errout; + fprintf(fp, "src type: "); + name_of(fp, RHS(ins, 0)->type); + fprintf(fp, "\n"); + fprintf(fp, "dst type: "); + name_of(fp, ins->type); + fprintf(fp, "\n"); + internal_error(state, ins, "type mismatch in copy"); + } + } while(next != first); +} + static void verify_consistency(struct compile_state *state) { + verify_unknown(state); verify_uses(state); + verify_blocks_present(state); verify_blocks(state); verify_domination(state); + verify_rhs(state); verify_piece(state); verify_ins_colors(state); + verify_types(state); + verify_copy(state); + if (state->compiler->debug & DEBUG_VERIFICATION) { + fprintf(state->dbgout, "consistency verified\n"); + } } -#else -#define verify_consistency(state) do {} while(0) -#endif /* DEBUG_USES */ +#else +static void verify_consistency(struct compile_state *state) {} +#endif /* DEBUG_CONSISTENCY */ static void optimize(struct compile_state *state) { - if (state->debug & DEBUG_TRIPLES) { - print_triples(state); - } + /* Join all of the functions into one giant function */ + join_functions(state); + + /* Dump what the instruction graph intially looks like */ + print_triples(state); + /* Replace structures with simpler data types */ - flatten_structures(state); - if (state->debug & DEBUG_TRIPLES) { - print_triples(state); - } + decompose_compound_types(state); + 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 */ + /* Analyze the intermediate code */ + state->bb.first = state->first; + analyze_basic_blocks(state, &state->bb); + + /* 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); verify_consistency(state); - if (state->debug & DEBUG_CODE_ELIMINATION) { - fprintf(stdout, "After transform_to_ssa_form\n"); - print_blocks(state, stdout); - } + + /* Remove dead code */ + eliminate_inefectual_code(state); + verify_consistency(state); + /* Do strength reduction and simple constant optimizations */ - if (state->optimize >= 1) { - simplify_all(state); - } + simplify_all(state); verify_consistency(state); /* Propogate constants throughout the code */ - if (state->optimize >= 2) { -#warning "FIXME fix scc_transform" - scc_transform(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); - } + scc_transform(state); verify_consistency(state); +#if DEBUG_ROMCC_WARNINGS #warning "WISHLIST implement single use constants (least possible register pressure)" #warning "WISHLIST implement induction variable elimination" +#endif /* 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, stdout); - print_control_flow(state); - } + + /* Remove dead code */ eliminate_inefectual_code(state); verify_consistency(state); - if (state->debug & DEBUG_CODE_ELIMINATION) { - printf("After eliminate_inefectual_code\n"); - 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, stdout); - } - if (state->debug & DEBUG_CONTROL_FLOW) { - print_control_flow(state); - } + /* Remove the optimization information. * This is more to check for memory consistency than to free memory. */ - free_basic_blocks(state); + free_basic_blocks(state, &state->bb); } static void print_op_asm(struct compile_state *state, @@ -14085,8 +21648,8 @@ static void print_op_asm(struct compile_state *state, const char *ptr; unsigned lhs, rhs, i; info = ins->u.ainfo; - lhs = TRIPLE_LHS(ins->sizes); - rhs = TRIPLE_RHS(ins->sizes); + lhs = ins->lhs; + rhs = ins->rhs; /* Don't count the clobbers in lhs */ for(i = 0; i < lhs; i++) { if (LHS(ins, i)->type == &void_type) { @@ -14094,6 +21657,7 @@ static void print_op_asm(struct compile_state *state, } } lhs = i; + fprintf(fp, "#ASM\n"); fputc('\t', fp); for(ptr = info->str; *ptr; ptr++) { char *next; @@ -14117,11 +21681,11 @@ static void print_op_asm(struct compile_state *state, param); } piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs); - fprintf(fp, "%s", + fprintf(fp, "%s", arch_reg_str(ID_REG(piece->id))); - ptr = next; + ptr = next -1; } - fputc('\n', fp); + fprintf(fp, "\n#NOT ASM\n"); } @@ -14130,48 +21694,48 @@ static void print_op_asm(struct compile_state *state, */ #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 +/* x86 featrues */ +#define X86_MMX_REGS (1<<0) +#define X86_XMM_REGS (1<<1) +#define X86_NOOP_COPY (1<<2) /* 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 REGC_IMM32 9 -#define REGC_IMM16 10 -#define REGC_IMM8 11 +#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_IMM32 (1 << REGC_IMM32) -#define REGCM_IMM16 (1 << REGC_IMM16) -#define REGCM_IMM8 (1 << REGC_IMM8) -#define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1) +#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) +#define REGCM_IMMALL (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8) /* The x86 registers */ #define REG_EFLAGS 2 @@ -14185,12 +21749,10 @@ static void print_op_asm(struct compile_state *state, #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 11 #define REG_BX 12 #define REG_CX 13 @@ -14212,29 +21774,36 @@ static void print_op_asm(struct compile_state *state, #define REGC_GPR32_FIRST REG_EAX #define REGC_GPR32_LAST REG_ESP #define REG_EDXEAX 27 -#define REGC_GPR64_FIRST REG_EDXEAX -#define REGC_GPR64_LAST REG_EDXEAX -#define REG_MMX0 28 -#define REG_MMX1 29 -#define REG_MMX2 30 -#define REG_MMX3 31 -#define REG_MMX4 32 -#define REG_MMX5 33 -#define REG_MMX6 34 -#define REG_MMX7 35 +#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 36 -#define REG_XMM1 37 -#define REG_XMM2 38 -#define REG_XMM3 39 -#define REG_XMM4 40 -#define REG_XMM5 41 -#define REG_XMM6 42 -#define REG_XMM7 43 +#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 + +#if DEBUG_ROMCC_WARNINGS #warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs" +#endif + #define LAST_REG REG_XMM7 #define REGC_GPR32_8_FIRST REG_EAX @@ -14255,57 +21824,109 @@ static void print_op_asm(struct compile_state *state, 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, - [REGC_IMM32] = 0, - [REGC_IMM16] = 0, - [REGC_IMM8] = 0, + [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_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 }, - [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST }, - [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST }, - [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST }, + [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; - } +#if ARCH_INPUT_REGS != 4 +#error ARCH_INPUT_REGS size mismatch +#endif +static const struct reg_info arch_input_regs[ARCH_INPUT_REGS] = { + { .reg = REG_EAX, .regcm = REGCM_GPR32 }, + { .reg = REG_EBX, .regcm = REGCM_GPR32 }, + { .reg = REG_ECX, .regcm = REGCM_GPR32 }, + { .reg = REG_EDX, .regcm = REGCM_GPR32 }, +}; + +#if ARCH_OUTPUT_REGS != 4 +#error ARCH_INPUT_REGS size mismatch +#endif +static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS] = { + { .reg = REG_EAX, .regcm = REGCM_GPR32 }, + { .reg = REG_EBX, .regcm = REGCM_GPR32 }, + { .reg = REG_ECX, .regcm = REGCM_GPR32 }, + { .reg = REG_EDX, .regcm = REGCM_GPR32 }, +}; + +static void init_arch_state(struct arch_state *arch) +{ + memset(arch, 0, sizeof(*arch)); + arch->features = 0; +} + +static const struct compiler_flag arch_flags[] = { + { "mmx", X86_MMX_REGS }, + { "sse", X86_XMM_REGS }, + { "noop-copy", X86_NOOP_COPY }, + { 0, 0 }, +}; +static const struct compiler_flag arch_cpus[] = { + { "i386", 0 }, + { "p2", X86_MMX_REGS }, + { "p3", X86_MMX_REGS | X86_XMM_REGS }, + { "p4", X86_MMX_REGS | X86_XMM_REGS }, + { "k7", X86_MMX_REGS }, + { "k8", X86_MMX_REGS | X86_XMM_REGS }, + { "c3", X86_MMX_REGS }, + { "c3-2", X86_MMX_REGS | X86_XMM_REGS }, /* Nehemiah */ + { 0, 0 } +}; +static int arch_encode_flag(struct arch_state *arch, const char *flag) +{ + int result; + int act; + + act = 1; + result = -1; + if (strncmp(flag, "no-", 3) == 0) { + flag += 3; + act = 0; + } + if (act && strncmp(flag, "cpu=", 4) == 0) { + flag += 4; + result = set_flag(arch_cpus, &arch->features, 1, flag); + } + else { + result = set_flag(arch_flags, &arch->features, act, flag); } - return ptr->cpu; + return result; +} + +static void arch_usage(FILE *fp) +{ + flag_usage(fp, arch_flags, "-m", "-mno-"); + flag_usage(fp, arch_cpus, "-mcpu=", 0); } static unsigned arch_regc_size(struct compile_state *state, int class) @@ -14315,17 +21936,19 @@ static unsigned arch_regc_size(struct compile_state *state, int class) } 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)) { + ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) { return 0; } return (regcm1 & regcm2) || @@ -14346,6 +21969,7 @@ static void arch_reg_equivs( #endif *equiv++ = REG_AX; *equiv++ = REG_EAX; + *equiv++ = REG_DXAX; *equiv++ = REG_EDXEAX; break; case REG_AH: @@ -14354,9 +21978,10 @@ static void arch_reg_equivs( #endif *equiv++ = REG_AX; *equiv++ = REG_EAX; + *equiv++ = REG_DXAX; *equiv++ = REG_EDXEAX; break; - case REG_BL: + case REG_BL: #if X86_4_8BIT_GPRS *equiv++ = REG_BH; #endif @@ -14392,6 +22017,7 @@ static void arch_reg_equivs( #endif *equiv++ = REG_DX; *equiv++ = REG_EDX; + *equiv++ = REG_DXAX; *equiv++ = REG_EDXEAX; break; case REG_DH: @@ -14400,12 +22026,14 @@ static void arch_reg_equivs( #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: @@ -14413,18 +22041,19 @@ static void arch_reg_equivs( *equiv++ = REG_BH; *equiv++ = REG_EBX; break; - case REG_CX: + case REG_CX: *equiv++ = REG_CL; *equiv++ = REG_CH; *equiv++ = REG_ECX; break; - case REG_DX: + case REG_DX: *equiv++ = REG_DL; *equiv++ = REG_DH; *equiv++ = REG_EDX; + *equiv++ = REG_DXAX; *equiv++ = REG_EDXEAX; break; - case REG_SI: + case REG_SI: *equiv++ = REG_ESI; break; case REG_DI: @@ -14440,6 +22069,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: @@ -14456,21 +22086,33 @@ static void arch_reg_equivs( *equiv++ = REG_DL; *equiv++ = REG_DH; *equiv++ = REG_DX; + *equiv++ = REG_DXAX; *equiv++ = REG_EDXEAX; break; - case REG_ESI: + case REG_ESI: *equiv++ = REG_SI; break; - case REG_EDI: + case REG_EDI: *equiv++ = REG_DI; break; - case REG_EBP: + case REG_EBP: *equiv++ = REG_BP; break; - case REG_ESP: + case REG_ESP: *equiv++ = REG_SP; break; - case REG_EDXEAX: + 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; *equiv++ = REG_DL; @@ -14479,33 +22121,26 @@ static void arch_reg_equivs( *equiv++ = REG_DX; *equiv++ = REG_EAX; *equiv++ = REG_EDX; + *equiv++ = REG_DXAX; break; } - *equiv++ = REG_UNSET; + *equiv++ = REG_UNSET; } static unsigned arch_avail_mask(struct compile_state *state) { unsigned avail_mask; - avail_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 | - REGCM_GPR32 | REGCM_GPR32_8 | REGCM_GPR64 | + /* 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: + if (state->arch->features & X86_MMX_REGS) { avail_mask |= REGCM_MMX; - break; - case CPU_P4: - case CPU_K8: - avail_mask |= REGCM_MMX | REGCM_XMM; - break; } -#if 0 - /* 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 (state->arch->features & X86_XMM_REGS) { + avail_mask |= REGCM_XMM; + } return avail_mask; } @@ -14514,7 +22149,6 @@ static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm unsigned mask, result; int class, class2; result = regcm; - result &= arch_avail_mask(state); for(class = 0, mask = 1; mask; mask <<= 1, class++) { if ((result & mask) == 0) { @@ -14530,9 +22164,20 @@ static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm } } } + 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) { unsigned mask; @@ -14558,19 +22203,19 @@ static struct reg_info arch_reg_constraint( 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, REG_UNSET }, + { '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 }, + { '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; @@ -14619,35 +22264,35 @@ static struct reg_info arch_reg_clobber( result.reg = REG_UNSET; result.regcm = 0; } - else if (strcmp(clobber, "%eax") == 0) { + else if (strcmp(clobber, "eax") == 0) { result.reg = REG_EAX; result.regcm = REGCM_GPR32; } - else if (strcmp(clobber, "%ebx") == 0) { + else if (strcmp(clobber, "ebx") == 0) { result.reg = REG_EBX; result.regcm = REGCM_GPR32; } - else if (strcmp(clobber, "%ecx") == 0) { + else if (strcmp(clobber, "ecx") == 0) { result.reg = REG_ECX; result.regcm = REGCM_GPR32; } - else if (strcmp(clobber, "%edx") == 0) { + else if (strcmp(clobber, "edx") == 0) { result.reg = REG_EDX; result.regcm = REGCM_GPR32; } - else if (strcmp(clobber, "%esi") == 0) { + else if (strcmp(clobber, "esi") == 0) { result.reg = REG_ESI; result.regcm = REGCM_GPR32; } - else if (strcmp(clobber, "%edi") == 0) { + else if (strcmp(clobber, "edi") == 0) { result.reg = REG_EDI; result.regcm = REGCM_GPR32; } - else if (strcmp(clobber, "%ebp") == 0) { + else if (strcmp(clobber, "ebp") == 0) { result.reg = REG_EBP; result.regcm = REGCM_GPR32; } - else if (strcmp(clobber, "%esp") == 0) { + else if (strcmp(clobber, "esp") == 0) { result.reg = REG_ESP; result.regcm = REGCM_GPR32; } @@ -14660,20 +22305,21 @@ static struct reg_info arch_reg_clobber( result.reg = REG_XMM0 + octdigval(clobber[3]); result.regcm = REGCM_XMM; } - else if ((strncmp(clobber, "mmx", 3) == 0) && + else if ((strncmp(clobber, "mm", 2) == 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"); + error(state, 0, "unknown register name `%s' in asm", + clobber); result.reg = REG_UNSET; result.regcm = 0; } return result; } -static int do_select_reg(struct compile_state *state, +static int do_select_reg(struct compile_state *state, char *used, int reg, unsigned classes) { unsigned mask; @@ -14687,21 +22333,28 @@ 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++) { - reg = do_select_reg(state, used, i, classes); - } - for(i = REGC_GPR32_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR32_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_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_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_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++) { @@ -14710,31 +22363,41 @@ static int arch_select_free_register( for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_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_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) { + reg = do_select_reg(state, used, i, classes); + } + for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) { + reg = do_select_reg(state, used, i, classes); + } + for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) { + reg = do_select_reg(state, used, i, classes); + } + 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) +static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type) { + +#if DEBUG_ROMCC_WARNINGS #warning "FIXME force types smaller (if legal) before I get here" - unsigned avail_mask; +#endif unsigned mask; mask = 0; - avail_mask = arch_avail_mask(state); switch(type->type & TYPE_MASK) { case TYPE_ARRAY: - case TYPE_VOID: - mask = 0; + case TYPE_VOID: + mask = 0; break; case TYPE_CHAR: case TYPE_UCHAR: - mask = REGCM_GPR8 | - REGCM_GPR16 | REGCM_GPR16_8 | + mask = REGCM_GPR8 | REGCM_GPR8_LO | + REGCM_GPR16 | REGCM_GPR16_8 | REGCM_GPR32 | REGCM_GPR32_8 | - REGCM_GPR64 | + REGCM_DIVIDEND32 | REGCM_DIVIDEND64 | REGCM_MMX | REGCM_XMM | REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8; break; @@ -14742,32 +22405,51 @@ static unsigned arch_type_to_regcm(struct compile_state *state, struct type *typ case TYPE_USHORT: mask = REGCM_GPR16 | REGCM_GPR16_8 | REGCM_GPR32 | REGCM_GPR32_8 | - REGCM_GPR64 | + REGCM_DIVIDEND32 | REGCM_DIVIDEND64 | REGCM_MMX | REGCM_XMM | REGCM_IMM32 | REGCM_IMM16; break; + case TYPE_ENUM: 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 | + REGCM_DIVIDEND32 | REGCM_DIVIDEND64 | + REGCM_MMX | REGCM_XMM | REGCM_IMM32; break; + case TYPE_JOIN: + case TYPE_UNION: + mask = arch_type_to_regcm(state, type->left); + break; + case TYPE_OVERLAP: + mask = arch_type_to_regcm(state, type->left) & + arch_type_to_regcm(state, type->right); + break; + case TYPE_BITFIELD: + mask = arch_type_to_regcm(state, type->left); + break; default: + fprintf(state->errout, "type: "); + name_of(state->errout, type); + fprintf(state->errout, "\n"); internal_error(state, 0, "no register class for type"); break; } - mask &= avail_mask; + mask = arch_regcm_normalize(state, mask); return mask; } static int is_imm32(struct triple *imm) { - return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) || + // second condition commented out to prevent compiler warning: + // imm->u.cval is always 32bit unsigned, so the comparison is + // always true. + return ((imm->op == OP_INTCONST) /* && (imm->u.cval <= 0xffffffffUL) */ ) || (imm->op == OP_ADDRCONST); - + } static int is_imm16(struct triple *imm) { @@ -14810,111 +22492,217 @@ static int get_imm8(struct triple *ins, struct triple **expr) return 1; } -#define TEMPLATE_NOP 0 -#define TEMPLATE_INTCONST8 1 -#define TEMPLATE_INTCONST32 2 -#define TEMPLATE_COPY_REG 3 -#define TEMPLATE_COPY_IMM32 4 -#define TEMPLATE_COPY_IMM16 5 -#define TEMPLATE_COPY_IMM8 6 -#define TEMPLATE_PHI 7 -#define TEMPLATE_STORE8 8 -#define TEMPLATE_STORE16 9 -#define TEMPLATE_STORE32 10 -#define TEMPLATE_LOAD8 11 -#define TEMPLATE_LOAD16 12 -#define TEMPLATE_LOAD32 13 -#define TEMPLATE_BINARY_REG 14 -#define TEMPLATE_BINARY_IMM 15 -#define TEMPLATE_SL_CL 16 -#define TEMPLATE_SL_IMM 17 -#define TEMPLATE_UNARY 18 -#define TEMPLATE_CMP_REG 19 -#define TEMPLATE_CMP_IMM 20 -#define TEMPLATE_TEST 21 -#define TEMPLATE_SET 22 -#define TEMPLATE_JMP 23 -#define TEMPLATE_INB_DX 24 -#define TEMPLATE_INB_IMM 25 -#define TEMPLATE_INW_DX 26 -#define TEMPLATE_INW_IMM 27 -#define TEMPLATE_INL_DX 28 -#define TEMPLATE_INL_IMM 29 -#define TEMPLATE_OUTB_DX 30 -#define TEMPLATE_OUTB_IMM 31 -#define TEMPLATE_OUTW_DX 32 -#define TEMPLATE_OUTW_IMM 33 -#define TEMPLATE_OUTL_DX 34 -#define TEMPLATE_OUTL_IMM 35 -#define TEMPLATE_BSF 36 -#define TEMPLATE_RDMSR 37 -#define TEMPLATE_WRMSR 38 -#define LAST_TEMPLATE TEMPLATE_WRMSR +#define TEMPLATE_NOP 0 +#define TEMPLATE_INTCONST8 1 +#define TEMPLATE_INTCONST32 2 +#define TEMPLATE_UNKNOWNVAL 3 +#define TEMPLATE_COPY8_REG 5 +#define TEMPLATE_COPY16_REG 6 +#define TEMPLATE_COPY32_REG 7 +#define TEMPLATE_COPY_IMM8 8 +#define TEMPLATE_COPY_IMM16 9 +#define TEMPLATE_COPY_IMM32 10 +#define TEMPLATE_PHI8 11 +#define TEMPLATE_PHI16 12 +#define TEMPLATE_PHI32 13 +#define TEMPLATE_STORE8 14 +#define TEMPLATE_STORE16 15 +#define TEMPLATE_STORE32 16 +#define TEMPLATE_LOAD8 17 +#define TEMPLATE_LOAD16 18 +#define TEMPLATE_LOAD32 19 +#define TEMPLATE_BINARY8_REG 20 +#define TEMPLATE_BINARY16_REG 21 +#define TEMPLATE_BINARY32_REG 22 +#define TEMPLATE_BINARY8_IMM 23 +#define TEMPLATE_BINARY16_IMM 24 +#define TEMPLATE_BINARY32_IMM 25 +#define TEMPLATE_SL8_CL 26 +#define TEMPLATE_SL16_CL 27 +#define TEMPLATE_SL32_CL 28 +#define TEMPLATE_SL8_IMM 29 +#define TEMPLATE_SL16_IMM 30 +#define TEMPLATE_SL32_IMM 31 +#define TEMPLATE_UNARY8 32 +#define TEMPLATE_UNARY16 33 +#define TEMPLATE_UNARY32 34 +#define TEMPLATE_CMP8_REG 35 +#define TEMPLATE_CMP16_REG 36 +#define TEMPLATE_CMP32_REG 37 +#define TEMPLATE_CMP8_IMM 38 +#define TEMPLATE_CMP16_IMM 39 +#define TEMPLATE_CMP32_IMM 40 +#define TEMPLATE_TEST8 41 +#define TEMPLATE_TEST16 42 +#define TEMPLATE_TEST32 43 +#define TEMPLATE_SET 44 +#define TEMPLATE_JMP 45 +#define TEMPLATE_RET 46 +#define TEMPLATE_INB_DX 47 +#define TEMPLATE_INB_IMM 48 +#define TEMPLATE_INW_DX 49 +#define TEMPLATE_INW_IMM 50 +#define TEMPLATE_INL_DX 51 +#define TEMPLATE_INL_IMM 52 +#define TEMPLATE_OUTB_DX 53 +#define TEMPLATE_OUTB_IMM 54 +#define TEMPLATE_OUTW_DX 55 +#define TEMPLATE_OUTW_IMM 56 +#define TEMPLATE_OUTL_DX 57 +#define TEMPLATE_OUTL_IMM 58 +#define TEMPLATE_BSF 59 +#define TEMPLATE_RDMSR 60 +#define TEMPLATE_WRMSR 61 +#define TEMPLATE_UMUL8 62 +#define TEMPLATE_UMUL16 63 +#define TEMPLATE_UMUL32 64 +#define TEMPLATE_DIV8 65 +#define TEMPLATE_DIV16 66 +#define TEMPLATE_DIV32 67 +#define LAST_TEMPLATE TEMPLATE_DIV32 #if LAST_TEMPLATE >= MAX_TEMPLATES #error "MAX_TEMPLATES to low" #endif -#define COPY_REGCM (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8 | REGCM_MMX | REGCM_XMM) -#define COPY32_REGCM (REGCM_GPR32 | REGCM_MMX | REGCM_XMM) +#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] = { + [TEMPLATE_NOP] = { + .lhs = { + [ 0] = { REG_UNNEEDED, REGCM_IMMALL }, + [ 1] = { REG_UNNEEDED, REGCM_IMMALL }, + [ 2] = { REG_UNNEEDED, REGCM_IMMALL }, + [ 3] = { REG_UNNEEDED, REGCM_IMMALL }, + [ 4] = { REG_UNNEEDED, REGCM_IMMALL }, + [ 5] = { REG_UNNEEDED, REGCM_IMMALL }, + [ 6] = { REG_UNNEEDED, REGCM_IMMALL }, + [ 7] = { REG_UNNEEDED, REGCM_IMMALL }, + [ 8] = { REG_UNNEEDED, REGCM_IMMALL }, + [ 9] = { REG_UNNEEDED, REGCM_IMMALL }, + [10] = { REG_UNNEEDED, REGCM_IMMALL }, + [11] = { REG_UNNEEDED, REGCM_IMMALL }, + [12] = { REG_UNNEEDED, REGCM_IMMALL }, + [13] = { REG_UNNEEDED, REGCM_IMMALL }, + [14] = { REG_UNNEEDED, REGCM_IMMALL }, + [15] = { REG_UNNEEDED, REGCM_IMMALL }, + [16] = { REG_UNNEEDED, REGCM_IMMALL }, + [17] = { REG_UNNEEDED, REGCM_IMMALL }, + [18] = { REG_UNNEEDED, REGCM_IMMALL }, + [19] = { REG_UNNEEDED, REGCM_IMMALL }, + [20] = { REG_UNNEEDED, REGCM_IMMALL }, + [21] = { REG_UNNEEDED, REGCM_IMMALL }, + [22] = { REG_UNNEEDED, REGCM_IMMALL }, + [23] = { REG_UNNEEDED, REGCM_IMMALL }, + [24] = { REG_UNNEEDED, REGCM_IMMALL }, + [25] = { REG_UNNEEDED, REGCM_IMMALL }, + [26] = { REG_UNNEEDED, REGCM_IMMALL }, + [27] = { REG_UNNEEDED, REGCM_IMMALL }, + [28] = { REG_UNNEEDED, REGCM_IMMALL }, + [29] = { REG_UNNEEDED, REGCM_IMMALL }, + [30] = { REG_UNNEEDED, REGCM_IMMALL }, + [31] = { REG_UNNEEDED, REGCM_IMMALL }, + [32] = { REG_UNNEEDED, REGCM_IMMALL }, + [33] = { REG_UNNEEDED, REGCM_IMMALL }, + [34] = { REG_UNNEEDED, REGCM_IMMALL }, + [35] = { REG_UNNEEDED, REGCM_IMMALL }, + [36] = { REG_UNNEEDED, REGCM_IMMALL }, + [37] = { REG_UNNEEDED, REGCM_IMMALL }, + [38] = { REG_UNNEEDED, REGCM_IMMALL }, + [39] = { REG_UNNEEDED, REGCM_IMMALL }, + [40] = { REG_UNNEEDED, REGCM_IMMALL }, + [41] = { REG_UNNEEDED, REGCM_IMMALL }, + [42] = { REG_UNNEEDED, REGCM_IMMALL }, + [43] = { REG_UNNEEDED, REGCM_IMMALL }, + [44] = { REG_UNNEEDED, REGCM_IMMALL }, + [45] = { REG_UNNEEDED, REGCM_IMMALL }, + [46] = { REG_UNNEEDED, REGCM_IMMALL }, + [47] = { REG_UNNEEDED, REGCM_IMMALL }, + [48] = { REG_UNNEEDED, REGCM_IMMALL }, + [49] = { REG_UNNEEDED, REGCM_IMMALL }, + [50] = { REG_UNNEEDED, REGCM_IMMALL }, + [51] = { REG_UNNEEDED, REGCM_IMMALL }, + [52] = { REG_UNNEEDED, REGCM_IMMALL }, + [53] = { REG_UNNEEDED, REGCM_IMMALL }, + [54] = { REG_UNNEEDED, REGCM_IMMALL }, + [55] = { REG_UNNEEDED, REGCM_IMMALL }, + [56] = { REG_UNNEEDED, REGCM_IMMALL }, + [57] = { REG_UNNEEDED, REGCM_IMMALL }, + [58] = { REG_UNNEEDED, REGCM_IMMALL }, + [59] = { REG_UNNEEDED, REGCM_IMMALL }, + [60] = { REG_UNNEEDED, REGCM_IMMALL }, + [61] = { REG_UNNEEDED, REGCM_IMMALL }, + [62] = { REG_UNNEEDED, REGCM_IMMALL }, + [63] = { REG_UNNEEDED, REGCM_IMMALL }, + }, + }, + [TEMPLATE_INTCONST8] = { .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } }, }, - [TEMPLATE_INTCONST32] = { + [TEMPLATE_INTCONST32] = { .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } }, }, - [TEMPLATE_COPY_REG] = { - .lhs = { [0] = { REG_UNSET, COPY_REGCM } }, - .rhs = { [0] = { REG_UNSET, COPY_REGCM } }, - }, - [TEMPLATE_COPY_IMM32] = { + [TEMPLATE_UNKNOWNVAL] = { .lhs = { [0] = { REG_UNSET, COPY32_REGCM } }, - .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } }, }, - [TEMPLATE_COPY_IMM16] = { - .lhs = { [0] = { REG_UNSET, COPY32_REGCM | REGCM_GPR16 } }, - .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 } }, + [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, COPY_REGCM } }, + .lhs = { [0] = { REG_UNSET, COPY8_REGCM } }, .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } }, }, - [TEMPLATE_PHI] = { - .lhs = { [0] = { REG_VIRT0, COPY_REGCM } }, - .rhs = { - [ 0] = { REG_VIRT0, COPY_REGCM }, - [ 1] = { REG_VIRT0, COPY_REGCM }, - [ 2] = { REG_VIRT0, COPY_REGCM }, - [ 3] = { REG_VIRT0, COPY_REGCM }, - [ 4] = { REG_VIRT0, COPY_REGCM }, - [ 5] = { REG_VIRT0, COPY_REGCM }, - [ 6] = { REG_VIRT0, COPY_REGCM }, - [ 7] = { REG_VIRT0, COPY_REGCM }, - [ 8] = { REG_VIRT0, COPY_REGCM }, - [ 9] = { REG_VIRT0, COPY_REGCM }, - [10] = { REG_VIRT0, COPY_REGCM }, - [11] = { REG_VIRT0, COPY_REGCM }, - [12] = { REG_VIRT0, COPY_REGCM }, - [13] = { REG_VIRT0, COPY_REGCM }, - [14] = { REG_VIRT0, COPY_REGCM }, - [15] = { REG_VIRT0, COPY_REGCM }, - }, }, + [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 } }, + }, + [TEMPLATE_PHI16] = { + .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } }, + .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } }, + }, + [TEMPLATE_PHI32] = { + .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } }, + .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } }, + }, [TEMPLATE_STORE8] = { - .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } }, - .rhs = { [0] = { REG_UNSET, REGCM_GPR8 } }, + .rhs = { + [0] = { REG_UNSET, REGCM_GPR32 }, + [1] = { REG_UNSET, REGCM_GPR8_LO }, + }, }, [TEMPLATE_STORE16] = { - .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } }, - .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } }, + .rhs = { + [0] = { REG_UNSET, REGCM_GPR32 }, + [1] = { REG_UNSET, REGCM_GPR16 }, + }, }, [TEMPLATE_STORE32] = { - .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } }, - .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } }, + .rhs = { + [0] = { REG_UNSET, REGCM_GPR32 }, + [1] = { REG_UNSET, REGCM_GPR32 }, + }, }, [TEMPLATE_LOAD8] = { - .lhs = { [0] = { REG_UNSET, REGCM_GPR8 } }, + .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } }, .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } }, }, [TEMPLATE_LOAD16] = { @@ -14925,77 +22713,180 @@ static struct ins_template templates[] = { .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } }, .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } }, }, - [TEMPLATE_BINARY_REG] = { + [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 = { + .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 }, [1] = { REG_UNSET, REGCM_GPR32 }, }, }, - [TEMPLATE_BINARY_IMM] = { + [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 = { + .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 }, [1] = { REG_UNNEEDED, REGCM_IMM32 }, }, }, - [TEMPLATE_SL_CL] = { - .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } }, - .rhs = { - [0] = { REG_VIRT0, REGCM_GPR32 }, - [1] = { REG_CL, REGCM_GPR8 }, + [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_SL_IMM] = { - .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } }, - .rhs = { - [0] = { REG_VIRT0, REGCM_GPR32 }, + [TEMPLATE_CMP8_IMM] = { + .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } }, + .rhs = { + [0] = { REG_UNSET, REGCM_GPR8_LO }, [1] = { REG_UNNEEDED, REGCM_IMM8 }, }, }, - [TEMPLATE_UNARY] = { - .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } }, - .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } }, - }, - [TEMPLATE_CMP_REG] = { + [TEMPLATE_CMP16_IMM] = { .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } }, .rhs = { - [0] = { REG_UNSET, REGCM_GPR32 }, - [1] = { REG_UNSET, REGCM_GPR32 }, + [0] = { REG_UNSET, REGCM_GPR16 }, + [1] = { REG_UNNEEDED, REGCM_IMM16 }, }, }, - [TEMPLATE_CMP_IMM] = { + [TEMPLATE_CMP32_IMM] = { .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } }, .rhs = { [0] = { REG_UNSET, REGCM_GPR32 }, [1] = { REG_UNNEEDED, REGCM_IMM32 }, }, }, - [TEMPLATE_TEST] = { + [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 } }, + .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } }, .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } }, }, [TEMPLATE_JMP] = { .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } }, }, + [TEMPLATE_RET] = { + .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } }, + }, [TEMPLATE_INB_DX] = { - .lhs = { [0] = { REG_AL, REGCM_GPR8 } }, + .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } }, .rhs = { [0] = { REG_DX, REGCM_GPR16 } }, }, [TEMPLATE_INB_IMM] = { - .lhs = { [0] = { REG_AL, REGCM_GPR8 } }, + .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } }, .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } }, }, - [TEMPLATE_INW_DX] = { - .lhs = { [0] = { REG_AX, REGCM_GPR16 } }, + [TEMPLATE_INW_DX] = { + .lhs = { [0] = { REG_AX, REGCM_GPR16 } }, .rhs = { [0] = { REG_DX, REGCM_GPR16 } }, }, - [TEMPLATE_INW_IMM] = { - .lhs = { [0] = { REG_AX, REGCM_GPR16 } }, + [TEMPLATE_INW_IMM] = { + .lhs = { [0] = { REG_AX, REGCM_GPR16 } }, .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } }, }, [TEMPLATE_INL_DX] = { @@ -15006,19 +22897,19 @@ static struct ins_template templates[] = { .lhs = { [0] = { REG_EAX, REGCM_GPR32 } }, .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } }, }, - [TEMPLATE_OUTB_DX] = { + [TEMPLATE_OUTB_DX] = { .rhs = { - [0] = { REG_AL, REGCM_GPR8 }, + [0] = { REG_AL, REGCM_GPR8_LO }, [1] = { REG_DX, REGCM_GPR16 }, }, }, - [TEMPLATE_OUTB_IMM] = { + [TEMPLATE_OUTB_IMM] = { .rhs = { - [0] = { REG_AL, REGCM_GPR8 }, + [0] = { REG_AL, REGCM_GPR8_LO }, [1] = { REG_UNNEEDED, REGCM_IMM8 }, }, }, - [TEMPLATE_OUTW_DX] = { + [TEMPLATE_OUTW_DX] = { .rhs = { [0] = { REG_AX, REGCM_GPR16 }, [1] = { REG_DX, REGCM_GPR16 }, @@ -15026,19 +22917,19 @@ static struct ins_template templates[] = { }, [TEMPLATE_OUTW_IMM] = { .rhs = { - [0] = { REG_AX, REGCM_GPR16 }, + [0] = { REG_AX, REGCM_GPR16 }, [1] = { REG_UNNEEDED, REGCM_IMM8 }, }, }, - [TEMPLATE_OUTL_DX] = { + [TEMPLATE_OUTL_DX] = { .rhs = { [0] = { REG_EAX, REGCM_GPR32 }, [1] = { REG_DX, REGCM_GPR16 }, }, }, - [TEMPLATE_OUTL_IMM] = { + [TEMPLATE_OUTL_IMM] = { .rhs = { - [0] = { REG_EAX, REGCM_GPR32 }, + [0] = { REG_EAX, REGCM_GPR32 }, [1] = { REG_UNNEEDED, REGCM_IMM8 }, }, }, @@ -15047,7 +22938,7 @@ static struct ins_template templates[] = { .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } }, }, [TEMPLATE_RDMSR] = { - .lhs = { + .lhs = { [0] = { REG_EAX, REGCM_GPR32 }, [1] = { REG_EDX, REGCM_GPR32 }, }, @@ -15060,8 +22951,85 @@ static struct ins_template templates[] = { [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_branch(struct compile_state *state, + struct triple *branch, int jmp_op, int cmp_op, struct type *cmp_type, + struct triple *left, struct triple *right) +{ + struct triple *test; + if (!left) { + internal_error(state, branch, "no branch test?"); + } + test = pre_triple(state, branch, + 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; + branch->template_id = TEMPLATE_JMP; + use_triple(RHS(branch, 0), branch); +} + static void fixup_branches(struct compile_state *state, struct triple *cmp, struct triple *use, int jmp_op) { @@ -15071,40 +23039,26 @@ static void fixup_branches(struct compile_state *state, if (entry->member->op == OP_COPY) { fixup_branches(state, cmp, entry->member, jmp_op); } - else if (entry->member->op == OP_BRANCH) { - struct triple *branch, *test; + else if (entry->member->op == OP_CBRANCH) { + struct triple *branch; struct triple *left, *right; left = right = 0; left = RHS(cmp, 0); - if (TRIPLE_RHS(cmp->sizes) > 1) { + if (cmp->rhs > 1) { right = RHS(cmp, 1); } branch = entry->member; - test = pre_triple(state, branch, + fixup_branch(state, branch, jmp_op, cmp->op, cmp->type, left, right); - test->template_id = TEMPLATE_TEST; - if (cmp->op == OP_CMP) { - test->template_id = TEMPLATE_CMP_REG; - if (get_imm32(test, &RHS(test, 1))) { - test->template_id = TEMPLATE_CMP_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; - branch->template_id = TEMPLATE_JMP; - use_triple(RHS(branch, 0), branch); } } } -static void bool_cmp(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 triple_set *entry, *next; - struct triple *set; + struct triple *set, *convert; /* Put a barrier up before the cmp which preceeds the * copy instruction. If a set actually occurs this gives @@ -15113,50 +23067,35 @@ static void bool_cmp(struct compile_state *state, /* Modify the comparison operator */ ins->op = cmp_op; - ins->template_id = TEMPLATE_TEST; + ins->template_id = TEMPLATE_TEST32; if (cmp_op == OP_CMP) { - ins->template_id = TEMPLATE_CMP_REG; + ins->template_id = TEMPLATE_CMP32_REG; if (get_imm32(ins, &RHS(ins, 1))) { - ins->template_id = TEMPLATE_CMP_IMM; + ins->template_id = TEMPLATE_CMP32_IMM; } } /* Generate the instruction sequence that will transform the * result of the comparison into a logical value. */ - set = post_triple(state, ins, set_op, ins->type, ins, 0); + set = post_triple(state, ins, set_op, &uchar_type, ins, 0); use_triple(ins, set); set->template_id = TEMPLATE_SET; + convert = set; + if (!equiv_types(ins->type, set->type)) { + convert = post_triple(state, set, OP_CONVERT, ins->type, set, 0); + use_triple(set, convert); + convert->template_id = TEMPLATE_COPY32_REG; + } + for(entry = ins->use; entry; entry = next) { next = entry->next; if (entry->member == set) { continue; } - replace_rhs_use(state, ins, set, entry->member); - } - 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); - } + replace_rhs_use(state, ins, convert, entry->member); } - return next; + fixup_branches(state, ins, convert, jmp_op); } struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index) @@ -15168,12 +23107,12 @@ struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, in index = ins->u.cval; ins = MISC(ins, 0); } - zlhs = TRIPLE_LHS(ins->sizes); + zlhs = ins->lhs; if (triple_is_def(state, ins)) { zlhs = 1; } if (index >= zlhs) { - internal_error(state, ins, "index %d out of range for %s\n", + internal_error(state, ins, "index %d out of range for %s", index, tops(ins->op)); } switch(ins->op) { @@ -15182,7 +23121,7 @@ struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, in break; default: if (ins->template_id > LAST_TEMPLATE) { - internal_error(state, ins, "bad template number %d", + internal_error(state, ins, "bad template number %d", ins->template_id); } template = &templates[ins->template_id]; @@ -15203,7 +23142,7 @@ struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, in { struct reg_info result; struct ins_template *template; - if ((index > TRIPLE_RHS(ins->sizes)) || + if ((index > ins->rhs) || (ins->op == OP_PIECE)) { internal_error(state, ins, "index %d out of range for %s\n", index, tops(ins->op)); @@ -15212,9 +23151,12 @@ struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, in case OP_ASM: template = &ins->u.ainfo->tmpl; break; + case OP_PHI: + index = 0; + /* Fall through */ default: if (ins->template_id > LAST_TEMPLATE) { - internal_error(state, ins, "bad template number %d", + internal_error(state, ins, "bad template number %d", ins->template_id); } template = &templates[ins->template_id]; @@ -15228,16 +23170,168 @@ struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, in return result; } +static struct triple *mod_div(struct compile_state *state, + struct triple *ins, int div_op, int index) +{ + struct triple *div, *piece1; + + /* 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); + piece1 = LHS(div, 1); + 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); + + /* 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 int noop_adecl(struct triple *adecl) +{ + struct triple_set *use; + /* It's a noop if it doesn't specify stoorage */ + if (adecl->lhs == 0) { + return 1; + } + /* Is the adecl used? If not it's a noop */ + for(use = adecl->use; use ; use = use->next) { + if ((use->member->op != OP_PIECE) || + (MISC(use->member, 0) != adecl)) { + return 0; + } + } + return 1; +} + +static struct triple *x86_deposit(struct compile_state *state, struct triple *ins) +{ + struct triple *mask, *nmask, *shift; + struct triple *val, *val_mask, *val_shift; + struct triple *targ, *targ_mask; + struct triple *new; + ulong_t the_mask, the_nmask; + + targ = RHS(ins, 0); + val = RHS(ins, 1); + + /* Get constant for the mask value */ + the_mask = 1; + the_mask <<= ins->u.bitfield.size; + the_mask -= 1; + the_mask <<= ins->u.bitfield.offset; + mask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0); + mask->u.cval = the_mask; + + /* Get the inverted mask value */ + the_nmask = ~the_mask; + nmask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0); + nmask->u.cval = the_nmask; + + /* Get constant for the shift value */ + shift = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0); + shift->u.cval = ins->u.bitfield.offset; + + /* Shift and mask the source value */ + val_shift = val; + if (shift->u.cval != 0) { + val_shift = pre_triple(state, ins, OP_SL, val->type, val, shift); + use_triple(val, val_shift); + use_triple(shift, val_shift); + } + val_mask = val_shift; + if (is_signed(val->type)) { + val_mask = pre_triple(state, ins, OP_AND, val->type, val_shift, mask); + use_triple(val_shift, val_mask); + use_triple(mask, val_mask); + } + + /* Mask the target value */ + targ_mask = pre_triple(state, ins, OP_AND, targ->type, targ, nmask); + use_triple(targ, targ_mask); + use_triple(nmask, targ_mask); + + /* Now combined them together */ + new = pre_triple(state, ins, OP_OR, targ->type, targ_mask, val_mask); + use_triple(targ_mask, new); + use_triple(val_mask, new); + + /* Move all of the users over to the new expression */ + propogate_use(state, ins, new); + + /* Delete the original triple */ + release_triple(state, ins); + + /* Restart the transformation at mask */ + return mask; +} + +static struct triple *x86_extract(struct compile_state *state, struct triple *ins) +{ + struct triple *mask, *shift; + struct triple *val, *val_mask, *val_shift; + ulong_t the_mask; + + val = RHS(ins, 0); + + /* Get constant for the mask value */ + the_mask = 1; + the_mask <<= ins->u.bitfield.size; + the_mask -= 1; + mask = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0); + mask->u.cval = the_mask; + + /* Get constant for the right shift value */ + shift = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0); + shift->u.cval = ins->u.bitfield.offset; + + /* Shift arithmetic right, to correct the sign */ + val_shift = val; + if (shift->u.cval != 0) { + int op; + if (ins->op == OP_SEXTRACT) { + op = OP_SSR; + } else { + op = OP_USR; + } + val_shift = pre_triple(state, ins, op, val->type, val, shift); + use_triple(val, val_shift); + use_triple(shift, val_shift); + } + + /* Finally mask the value */ + val_mask = pre_triple(state, ins, OP_AND, ins->type, val_shift, mask); + use_triple(val_shift, val_mask); + use_triple(mask, val_mask); + + /* Move all of the users over to the new expression */ + propogate_use(state, ins, val_mask); + + /* Release the original instruction */ + release_triple(state, ins); + + return mask; + +} + 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 *next; + struct triple *next, *value; + size_t size; next = ins->next; switch(ins->op) { case OP_INTCONST: @@ -15249,6 +23343,9 @@ static struct triple *transform_to_arch_instruction( case OP_ADDRCONST: ins->template_id = TEMPLATE_INTCONST32; break; + case OP_UNKNOWNVAL: + ins->template_id = TEMPLATE_UNKNOWNVAL; + break; case OP_NOOP: case OP_SDECL: case OP_BLOBCONST: @@ -15256,22 +23353,58 @@ static struct triple *transform_to_arch_instruction( ins->template_id = TEMPLATE_NOP; break; case OP_COPY: - ins->template_id = TEMPLATE_COPY_REG; - if (is_imm8(RHS(ins, 0))) { + case OP_CONVERT: + size = size_of(state, ins->type); + value = RHS(ins, 0); + if (is_imm8(value) && (size <= SIZEOF_I8)) { ins->template_id = TEMPLATE_COPY_IMM8; } - else if (is_imm16(RHS(ins, 0))) { + else if (is_imm16(value) && (size <= SIZEOF_I16)) { ins->template_id = TEMPLATE_COPY_IMM16; } - else if (is_imm32(RHS(ins, 0))) { + else if (is_imm32(value) && (size <= SIZEOF_I32)) { ins->template_id = TEMPLATE_COPY_IMM32; } - else if (is_const(RHS(ins, 0))) { + else if (is_const(value)) { internal_error(state, ins, "bad constant passed to copy"); } + else if (size <= SIZEOF_I8) { + ins->template_id = TEMPLATE_COPY8_REG; + } + else if (size <= SIZEOF_I16) { + ins->template_id = TEMPLATE_COPY16_REG; + } + else if (size <= SIZEOF_I32) { + ins->template_id = TEMPLATE_COPY32_REG; + } + else { + internal_error(state, ins, "bad type passed to copy"); + } break; case OP_PHI: - ins->template_id = TEMPLATE_PHI; + size = size_of(state, ins->type); + if (size <= SIZEOF_I8) { + ins->template_id = TEMPLATE_PHI8; + } + else if (size <= SIZEOF_I16) { + ins->template_id = TEMPLATE_PHI16; + } + else if (size <= SIZEOF_I32) { + ins->template_id = TEMPLATE_PHI32; + } + else { + internal_error(state, ins, "bad type passed to phi"); + } + break; + case OP_ADECL: + /* Adecls should always be treated as dead code and + * removed. If we are not optimizing they may linger. + */ + if (!noop_adecl(ins)) { + internal_error(state, ins, "adecl remains?"); + } + ins->template_id = TEMPLATE_NOP; + next = after_lhs(state, ins); break; case OP_STORE: switch(ins->type->type & TYPE_MASK) { @@ -15294,23 +23427,16 @@ static struct triple *transform_to_arch_instruction( case OP_LOAD: switch(ins->type->type & TYPE_MASK) { case TYPE_CHAR: case TYPE_UCHAR: - ins->template_id = TEMPLATE_LOAD8; - break; - case TYPE_SHORT: - case TYPE_USHORT: - ins->template_id = TEMPLATE_LOAD16; - break; - case TYPE_INT: - case TYPE_UINT: - case TYPE_LONG: - case TYPE_ULONG: + case TYPE_SHORT: case TYPE_USHORT: + 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, "unknown type in load"); break; } + ins->template_id = TEMPLATE_LOAD32; break; case OP_ADD: case OP_SUB: @@ -15318,25 +23444,47 @@ static struct triple *transform_to_arch_instruction( case OP_XOR: case OP_OR: case OP_SMUL: - ins->template_id = TEMPLATE_BINARY_REG; + ins->template_id = TEMPLATE_BINARY32_REG; if (get_imm32(ins, &RHS(ins, 1))) { - ins->template_id = TEMPLATE_BINARY_IMM; + ins->template_id = TEMPLATE_BINARY32_IMM; } break; + case OP_SDIVT: + case OP_UDIVT: + ins->template_id = TEMPLATE_DIV32; + next = after_lhs(state, ins); + break; + 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_SL_CL; + ins->template_id = TEMPLATE_SL32_CL; if (get_imm8(ins, &RHS(ins, 1))) { - ins->template_id = TEMPLATE_SL_IMM; + ins->template_id = TEMPLATE_SL32_IMM; + } else if (size_of(state, RHS(ins, 1)->type) > SIZEOF_CHAR) { + typed_pre_copy(state, &uchar_type, ins, 1); } break; case OP_INVERT: case OP_NEG: - ins->template_id = TEMPLATE_UNARY; + ins->template_id = TEMPLATE_UNARY32; break; - case OP_EQ: - bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ); + 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); @@ -15372,12 +23520,19 @@ static struct triple *transform_to_arch_instruction( 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_CBRANCH: + fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST, + RHS(ins, 0)->type, RHS(ins, 0), 0); + break; + case OP_CALL: + ins->template_id = TEMPLATE_NOP; + break; + case OP_RET: + ins->template_id = TEMPLATE_RET; + break; case OP_INB: case OP_INW: case OP_INL: @@ -15422,14 +23577,17 @@ static struct triple *transform_to_arch_instruction( break; /* Already transformed instructions */ case OP_TEST: - ins->template_id = TEMPLATE_TEST; + ins->template_id = TEMPLATE_TEST32; break; case OP_CMP: - ins->template_id = TEMPLATE_CMP_REG; + ins->template_id = TEMPLATE_CMP32_REG; if (get_imm32(ins, &RHS(ins, 1))) { - ins->template_id = TEMPLATE_CMP_IMM; + ins->template_id = TEMPLATE_CMP32_IMM; } break; + case OP_JMP: + ins->template_id = TEMPLATE_NOP; + 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: @@ -15444,38 +23602,48 @@ static struct triple *transform_to_arch_instruction( case OP_SET_SMOREEQ: case OP_SET_UMOREEQ: ins->template_id = TEMPLATE_SET; break; + case OP_DEPOSIT: + next = x86_deposit(state, ins); + break; + case OP_SEXTRACT: + case OP_UEXTRACT: + next = x86_extract(state, ins); + break; /* Unhandled instructions */ case OP_PIECE: default: - internal_error(state, ins, "unhandled ins: %d %s\n", + internal_error(state, ins, "unhandled ins: %d %s", ins->op, tops(ins->op)); break; } return next; } +static long next_label(struct compile_state *state) +{ + static long label_counter = 1000; + return ++label_counter; +} static void generate_local_labels(struct compile_state *state) { struct triple *first, *label; - int label_counter; - label_counter = 0; - first = RHS(state->main_function, 0); + first = state->first; label = first; do { - if ((label->op == OP_LABEL) || + 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; } - + } label = label->next; } while(label != first); } -static int check_reg(struct compile_state *state, +static int check_reg(struct compile_state *state, struct triple *triple, int classes) { unsigned mask; @@ -15492,27 +23660,31 @@ static int check_reg(struct compile_state *state, return reg; } + +#if REG_XMM7 != 44 +#error "Registers have renumberd fix arch_reg_str" +#endif +static const char *arch_regs[] = { + "%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", +}; static const char *arch_reg_str(int reg) { - static const char *regs[] = { - "%bad_register", - "%bad_register2", - "%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", - "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7", - "%xmm0", "%xmm1", "%xmm2", "%xmm3", - "%xmm4", "%xmm5", "%xmm6", "%xmm7", - }; if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) { reg = 0; } - return regs[reg]; + return arch_regs[reg]; } - static const char *reg(struct compile_state *state, struct triple *triple, int classes) { @@ -15521,13 +23693,56 @@ static const char *reg(struct compile_state *state, struct triple *triple, return arch_reg_str(reg); } +static int arch_reg_size(int reg) +{ + int size; + size = 0; + if (reg == REG_EFLAGS) { + size = 32; + } + else if ((reg >= REG_AL) && (reg <= REG_DH)) { + size = 8; + } + else if ((reg >= REG_AX) && (reg <= REG_SP)) { + size = 16; + } + else if ((reg >= REG_EAX) && (reg <= REG_ESP)) { + size = 32; + } + else if (reg == REG_EDXEAX) { + size = 64; + } + else if (reg == REG_DXAX) { + size = 32; + } + else if ((reg >= REG_MMX0) && (reg <= REG_MMX7)) { + size = 64; + } + else if ((reg >= REG_XMM0) && (reg <= REG_XMM7)) { + size = 128; + } + return size; +} + +static int reg_size(struct compile_state *state, struct triple *ins) +{ + int reg; + reg = ID_REG(ins->id); + if (reg == REG_UNSET) { + internal_error(state, ins, "register not set"); + } + return arch_reg_size(reg); +} + + + const char *type_suffix(struct compile_state *state, struct type *type) { const char *suffix; switch(size_of(state, type)) { - case 1: suffix = "b"; break; - case 2: suffix = "w"; break; - case 4: suffix = "l"; break; + case SIZEOF_I8: suffix = "b"; break; + case SIZEOF_I16: suffix = "w"; break; + case SIZEOF_I32: suffix = "l"; break; default: internal_error(state, 0, "unknown suffix"); suffix = 0; @@ -15541,14 +23756,22 @@ static void print_const_val( { switch(ins->op) { case OP_INTCONST: - fprintf(fp, " $%ld ", - (long_t)(ins->u.cval)); + fprintf(fp, " $%ld ", + (long)(ins->u.cval)); break; case OP_ADDRCONST: + if ((MISC(ins, 0)->op != OP_SDECL) && + (MISC(ins, 0)->op != OP_LABEL)) + { + 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); + state->compiler->label_prefix, + (unsigned long)(MISC(ins, 0)->u.cval), + (unsigned long)(ins->u.cval)); break; default: internal_error(state, ins, "unknown constant type"); @@ -15556,12 +23779,114 @@ static void print_const_val( } } +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", + (unsigned long)(ins->u.cval)); + break; + case TYPE_SHORT: + case TYPE_USHORT: + fprintf(fp, ".short 0x%04lx\n", + (unsigned long)(ins->u.cval)); + break; + case TYPE_INT: + case TYPE_UINT: + case TYPE_LONG: + case TYPE_ULONG: + case TYPE_POINTER: + fprintf(fp, ".int %lu\n", + (unsigned long)(ins->u.cval)); + break; + default: + fprintf(state->errout, "type: "); + name_of(state->errout, ins->type); + fprintf(state->errout, "\n"); + internal_error(state, ins, "Unknown constant type. Val: %lu", + (unsigned long)(ins->u.cval)); + } + + break; + case OP_ADDRCONST: + if ((MISC(ins, 0)->op != OP_SDECL) && + (MISC(ins, 0)->op != OP_LABEL)) { + 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->compiler->label_prefix, + (unsigned long)(MISC(ins, 0)->u.cval), + (unsigned long)(ins->u.cval)); + break; + case OP_BLOBCONST: + { + unsigned char *blob; + size_t size, i; + size = size_of_in_bytes(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, size_t size, FILE *fp) +{ + size_t fill_bytes; + long ref; + ref = next_label(state); + fprintf(fp, ".section \"" DATA_SECTION "\"\n"); + fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type)); + fprintf(fp, "L%s%lu:\n", state->compiler->label_prefix, ref); + print_const(state, ins, fp); + fill_bytes = bits_to_bytes(size - size_of(state, ins->type)); + if (fill_bytes) { + fprintf(fp, ".fill %ld, 1, 0\n", (long int)fill_bytes); + } + fprintf(fp, ".section \"" TEXT_SECTION "\"\n"); + return ref; +} + +static long get_mask_pool_ref( + struct compile_state *state, struct triple *ins, unsigned long mask, FILE *fp) +{ + long ref; + if (mask == 0xff) { + ref = 1; + } + else if (mask == 0xffff) { + ref = 2; + } + else { + ref = 0; + internal_error(state, ins, "unhandled mask value"); + } + return ref; +} + static void print_binary_op(struct compile_state *state, - const char *op, struct triple *ins, FILE *fp) + const char *op, struct triple *ins, FILE *fp) { unsigned mask; - mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8; - if (RHS(ins, 0)->id != ins->id) { + mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO; + if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) { internal_error(state, ins, "invalid register assignment"); } if (is_const(RHS(ins, 1))) { @@ -15584,11 +23909,11 @@ static void print_binary_op(struct compile_state *state, reg(state, RHS(ins, 0), mask)); } } -static void print_unary_op(struct compile_state *state, +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, RHS(ins, 0), mask)); @@ -15598,8 +23923,8 @@ 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 (RHS(ins, 0)->id != ins->id) { + mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO; + if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) { internal_error(state, ins, "invalid register assignment"); } if (is_const(RHS(ins, 1))) { @@ -15611,7 +23936,7 @@ static void print_op_shift(struct compile_state *state, else { fprintf(fp, "\t%s %s, %s\n", op, - reg(state, RHS(ins, 1), REGCM_GPR8), + reg(state, RHS(ins, 1), REGCM_GPR8_LO), reg(state, RHS(ins, 0), mask)); } } @@ -15623,7 +23948,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: @@ -15648,7 +23973,7 @@ static void print_op_in(struct compile_state *state, struct triple *ins, FILE *f internal_error(state, ins, "src != %%dx"); } fprintf(fp, "\t%s %s, %s\n", - op, + op, reg(state, RHS(ins, 0), REGCM_GPR16), reg(state, ins, mask)); } @@ -15661,7 +23986,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: @@ -15674,7 +23999,7 @@ static void print_op_out(struct compile_state *state, struct triple *ins, FILE * internal_error(state, ins, "src != %%eax"); } if (is_const(RHS(ins, 1))) { - fprintf(fp, "\t%s %s,", + fprintf(fp, "\t%s %s,", op, reg(state, RHS(ins, 0), mask)); print_const_val(state, RHS(ins, 1), fp); fprintf(fp, "\n"); @@ -15686,7 +24011,7 @@ static void print_op_out(struct compile_state *state, struct triple *ins, FILE * internal_error(state, ins, "dst != %%dx"); } fprintf(fp, "\t%s %s, %s\n", - op, + op, reg(state, RHS(ins, 0), mask), reg(state, RHS(ins, 1), REGCM_GPR16)); } @@ -15699,31 +24024,45 @@ static void print_op_move(struct compile_state *state, * of registers we can move between. * Because OP_COPY will be introduced in arbitrary locations * OP_COPY must not affect flags. + * OP_CONVERT can change the flags and it is the only operation + * where it is expected the types in the registers can change. */ int omit_copy = 1; /* Is it o.k. to omit a noop copy? */ struct triple *dst, *src; - if (ins->op == OP_COPY) { - src = RHS(ins, 0); - dst = ins; + if (state->arch->features & X86_NOOP_COPY) { + omit_copy = 0; } - else if (ins->op == OP_WRITE) { - dst = LHS(ins, 0); + if ((ins->op == OP_COPY) || (ins->op == OP_CONVERT)) { src = RHS(ins, 0); + dst = ins; } else { internal_error(state, ins, "unknown move operation"); src = dst = 0; } + if (reg_size(state, dst) < size_of(state, dst->type)) { + internal_error(state, ins, "Invalid destination register"); + } + if (!equiv_types(src->type, dst->type) && (dst->op == OP_COPY)) { + fprintf(state->errout, "src type: "); + name_of(state->errout, src->type); + fprintf(state->errout, "\n"); + fprintf(state->errout, "dst type: "); + name_of(state->errout, dst->type); + fprintf(state->errout, "\n"); + internal_error(state, ins, "Type mismatch for OP_COPY"); + } + 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)) { + if (src_regcm & dst_regcm & + (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), @@ -15736,13 +24075,23 @@ static void print_op_move(struct compile_state *state, src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST; if ((src_reg != dst_reg) || !omit_copy) { fprintf(fp, "\tmovw %s, %s\n", - arch_reg_str(src_reg), + arch_reg_str(src_reg), + 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) { @@ -15753,7 +24102,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) { @@ -15763,7 +24112,7 @@ static void print_op_move(struct compile_state *state, } } /* Move 8/16bit to 16/32bit */ - else if ((src_regcm & (REGCM_GPR8 | REGCM_GPR16)) && + 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"; @@ -15780,15 +24129,26 @@ static void print_op_move(struct compile_state *state, 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", 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))) { @@ -15796,9 +24156,71 @@ 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) && (src_reg <= REG_DL) && + else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) && (dst_regcm & (REGCM_MMX | REGCM_XMM))) { const char *op; int mid_reg; @@ -15813,26 +24235,16 @@ static void print_op_move(struct compile_state *state, } /* Move from mmx/sse registers and 8bit gprs */ else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) && - (dst_regcm & REGCM_GPR8) && (dst_reg <= REG_DL)) { + (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 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 from 32bit gprs to 8bit gprs */ else if ((src_regcm & REGCM_GPR32) && - (dst_regcm & REGCM_GPR8)) { + (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", @@ -15842,7 +24254,7 @@ static void print_op_move(struct compile_state *state, } /* Move from 16bit gprs to 8bit gprs */ else if ((src_regcm & REGCM_GPR16) && - (dst_regcm & REGCM_GPR8)) { + (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", @@ -15851,15 +24263,161 @@ static void print_op_move(struct compile_state *state, } } #endif /* X86_4_8BIT_GPRS */ + /* Move from %eax:%edx to %eax:%edx */ + else if ((src_regcm & REGCM_DIVIDEND64) && + (dst_regcm & REGCM_DIVIDEND64) && + (src_reg == dst_reg)) { + if (!omit_copy) { + fprintf(fp, "\t/*mov %s, %s*/\n", + arch_reg_str(src_reg), + arch_reg_str(dst_reg)); + } + } else { + if ((src_regcm & ~REGCM_FLAGS) == 0) { + internal_error(state, ins, "attempt to copy from %%eflags!"); + } internal_error(state, ins, "unknown copy type"); } } else { - fprintf(fp, "\tmov "); - print_const_val(state, src, fp); - fprintf(fp, ", %s\n", - reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8)); + size_t dst_size; + int dst_reg; + int dst_regcm; + dst_size = size_of(state, dst->type); + 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 (dst_size > SIZEOF_I32) { + internal_error(state, ins, "%dbit constant...", dst_size); + } + 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 (dst_size > SIZEOF_I16) { + internal_error(state, ins, "%dbit constant...", dst_size); + } + 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; + if (dst_size > SIZEOF_I32) { + internal_error(state, ins, "%d bit constant...", dst_size); + } + ref = get_const_pool_ref(state, src, SIZEOF_I32, fp); + fprintf(fp, "\tmovd L%s%lu, %s\n", + state->compiler->label_prefix, ref, + reg(state, dst, (REGCM_XMM | REGCM_MMX))); + } + else { + internal_error(state, ins, "unknown copy immediate type"); + } + } + /* Leave now if this is not a type conversion */ + if (ins->op != OP_CONVERT) { + return; + } + /* Now make certain I have not logically overflowed the destination */ + if ((size_of(state, src->type) > size_of(state, dst->type)) && + (size_of(state, dst->type) < reg_size(state, dst))) + { + unsigned long mask; + int dst_reg; + int dst_regcm; + if (size_of(state, dst->type) >= 32) { + fprintf(state->errout, "dst type: "); + name_of(state->errout, dst->type); + fprintf(state->errout, "\n"); + internal_error(state, dst, "unhandled dst type size"); + } + mask = 1; + mask <<= size_of(state, dst->type); + mask -= 1; + + 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, "\tand $0x%lx, %s\n", + mask, reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)); + } + else if (dst_regcm & REGCM_MMX) { + long ref; + ref = get_mask_pool_ref(state, dst, mask, fp); + fprintf(fp, "\tpand L%s%lu, %s\n", + state->compiler->label_prefix, ref, + reg(state, dst, REGCM_MMX)); + } + else if (dst_regcm & REGCM_XMM) { + long ref; + ref = get_mask_pool_ref(state, dst, mask, fp); + fprintf(fp, "\tpand L%s%lu, %s\n", + state->compiler->label_prefix, ref, + reg(state, dst, REGCM_XMM)); + } + else { + fprintf(state->errout, "dst type: "); + name_of(state->errout, dst->type); + fprintf(state->errout, "\n"); + fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL)); + internal_error(state, dst, "failed to trunc value: mask %lx", mask); + } + } + /* Make certain I am properly sign extended */ + if ((size_of(state, src->type) < size_of(state, dst->type)) && + (is_signed(src->type))) + { + int reg_bits, shift_bits; + int dst_reg; + int dst_regcm; + + reg_bits = reg_size(state, dst); + if (reg_bits > 32) { + reg_bits = 32; + } + shift_bits = reg_bits - size_of(state, src->type); + dst_reg = ID_REG(dst->id); + dst_regcm = arch_reg_regcm(state, dst_reg); + + if (shift_bits < 0) { + internal_error(state, dst, "negative shift?"); + } + + if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) { + fprintf(fp, "\tshl $%d, %s\n", + shift_bits, + reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)); + fprintf(fp, "\tsar $%d, %s\n", + shift_bits, + reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)); + } + else if (dst_regcm & (REGCM_MMX | REGCM_XMM)) { + fprintf(fp, "\tpslld $%d, %s\n", + shift_bits, + reg(state, dst, REGCM_MMX | REGCM_XMM)); + fprintf(fp, "\tpsrad $%d, %s\n", + shift_bits, + reg(state, dst, REGCM_MMX | REGCM_XMM)); + } + else { + fprintf(state->errout, "dst type: "); + name_of(state->errout, dst->type); + fprintf(state->errout, "\n"); + fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL)); + internal_error(state, dst, "failed to signed extend value"); + } } } @@ -15867,14 +24425,31 @@ static void print_op_load(struct compile_state *state, struct triple *ins, FILE *fp) { struct triple *dst, *src; + const char *op; dst = ins; src = RHS(ins, 0); if (is_const(src) || is_const(dst)) { internal_error(state, ins, "unknown load operation"); } - fprintf(fp, "\tmov (%s), %s\n", + switch(ins->type->type & TYPE_MASK) { + case TYPE_CHAR: op = "movsbl"; break; + case TYPE_UCHAR: op = "movzbl"; break; + case TYPE_SHORT: op = "movswl"; break; + case TYPE_USHORT: op = "movzwl"; break; + case TYPE_INT: case TYPE_UINT: + case TYPE_LONG: case TYPE_ULONG: + case TYPE_POINTER: + op = "movl"; + break; + default: + internal_error(state, ins, "unknown type in load"); + op = ""; + break; + } + fprintf(fp, "\t%s (%s), %s\n", + op, reg(state, src, REGCM_GPR32), - reg(state, dst, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32)); + reg(state, dst, REGCM_GPR32)); } @@ -15882,21 +24457,21 @@ static void print_op_store(struct compile_state *state, struct triple *ins, FILE *fp) { struct triple *dst, *src; - dst = LHS(ins, 0); - src = RHS(ins, 0); + 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); fprintf(fp, "\tmov%s $%ld, (%s)\n", type_suffix(state, src->type), - value, + (long)(value), reg(state, dst, REGCM_GPR32)); } 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), - dst->u.cval); + reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32), + (unsigned long)(dst->u.cval)); } else { if (is_const(src) || is_const(dst)) { @@ -15904,11 +24479,11 @@ 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)); } - - + + } static void print_op_smul(struct compile_state *state, @@ -15931,7 +24506,7 @@ 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"); @@ -15959,7 +24534,7 @@ 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, RHS(ins, 0), mask), reg(state, RHS(ins, 0), mask)); @@ -15969,15 +24544,15 @@ static void print_op_branch(struct compile_state *state, struct triple *branch, FILE *fp) { const char *bop = "j"; - if (branch->op == OP_JMP) { - if (TRIPLE_RHS(branch->sizes) != 0) { + if ((branch->op == OP_JMP) || (branch->op == OP_CALL)) { + if (branch->rhs != 0) { internal_error(state, branch, "jmp with condition?"); } bop = "jmp"; } else { struct triple *ptr; - if (TRIPLE_RHS(branch->sizes) != 1) { + if (branch->rhs != 1) { internal_error(state, branch, "jmpcc without condition?"); } check_reg(state, RHS(branch, 0), REGCM_FLAGS); @@ -15985,7 +24560,9 @@ static void print_op_branch(struct compile_state *state, (RHS(branch, 0)->op != OP_TEST)) { internal_error(state, branch, "bad branch test"); } +#if DEBUG_ROMCC_WARNINGS #warning "FIXME I have observed instructions between the test and branch instructions" +#endif ptr = RHS(branch, 0); for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) { if (ptr->op != OP_COPY) { @@ -16007,19 +24584,31 @@ static void print_op_branch(struct compile_state *state, internal_error(state, branch, "Invalid branch op"); break; } - + + } +#if 1 + if (branch->op == OP_CALL) { + fprintf(fp, "\t/* call */\n"); } +#endif fprintf(fp, "\t%s L%s%lu\n", - bop, - state->label_prefix, - TARG(branch, 0)->u.cval); + bop, + state->compiler->label_prefix, + (unsigned long)(TARG(branch, 0)->u.cval)); +} + +static void print_op_ret(struct compile_state *state, + struct triple *branch, FILE *fp) +{ + fprintf(fp, "\tjmp *%s\n", + reg(state, RHS(branch, 0), REGCM_GPR32)); } static void print_op_set(struct compile_state *state, struct triple *set, FILE *fp) { const char *sop = "set"; - if (TRIPLE_RHS(set->sizes) != 1) { + if (set->rhs != 1) { internal_error(state, set, "setcc without condition?"); } check_reg(state, RHS(set, 0), REGCM_FLAGS); @@ -16046,22 +24635,22 @@ 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, - struct triple *ins, FILE *fp) +static void print_op_bit_scan(struct compile_state *state, + struct triple *ins, FILE *fp) { const char *op; switch(ins->op) { case OP_BSF: op = "bsf"; break; case OP_BSR: op = "bsr"; break; - default: + default: internal_error(state, ins, "unknown bit scan"); op = 0; break; } - fprintf(fp, + fprintf(fp, "\t%s %s, %s\n" "\tjnz 1f\n" "\tmovl $-1, %s\n" @@ -16072,67 +24661,24 @@ static void print_op_bit_scan(struct compile_state *state, 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; - } - default: - internal_error(state, ins, "Unknown constant type"); - break; - } -} - -#define TEXT_SECTION ".rom.text" -#define DATA_SECTION ".rom.data" static void print_sdecl(struct compile_state *state, struct triple *ins, FILE *fp) { 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, ins->u.cval); + fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type)); + fprintf(fp, "L%s%lu:\n", + state->compiler->label_prefix, (unsigned long)(ins->u.cval)); print_const(state, MISC(ins, 0), fp); fprintf(fp, ".section \"" TEXT_SECTION "\"\n"); - + } static void print_instruction(struct compile_state *state, struct triple *ins, FILE *fp) { /* Assumption: after I have exted the register allocator - * everything is in a valid register. + * everything is in a valid register. */ switch(ins->op) { case OP_ASM: @@ -16149,6 +24695,7 @@ static void print_instruction(struct compile_state *state, case OP_POS: break; case OP_NEG: print_unary_op(state, "neg", ins, fp); break; case OP_INVERT: print_unary_op(state, "not", ins, fp); break; + case OP_NOOP: case OP_INTCONST: case OP_ADDRCONST: case OP_BLOBCONST: @@ -16156,11 +24703,15 @@ static void print_instruction(struct compile_state *state, case OP_PHI: /* Don't generate anything for variable declarations. */ break; + case OP_UNKNOWNVAL: + fprintf(fp, " /* unknown %s */\n", + reg(state, ins, REGCM_ALL)); + break; case OP_SDECL: print_sdecl(state, ins, fp); break; - case OP_WRITE: - case OP_COPY: + case OP_COPY: + case OP_CONVERT: print_op_move(state, ins, fp); break; case OP_LOAD: @@ -16180,8 +24731,12 @@ static void print_instruction(struct compile_state *state, 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_CALL: print_op_branch(state, ins, fp); break; + case OP_RET: + print_op_ret(state, ins, fp); + 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: @@ -16190,10 +24745,10 @@ static void print_instruction(struct compile_state *state, print_op_set(state, ins, fp); break; case OP_INB: case OP_INW: case OP_INL: - print_op_in(state, ins, fp); + print_op_in(state, ins, fp); break; case OP_OUTB: case OP_OUTW: case OP_OUTL: - print_op_out(state, ins, fp); + print_op_out(state, ins, fp); break; case OP_BSF: case OP_BSR: @@ -16209,20 +24764,34 @@ static void print_instruction(struct compile_state *state, 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%s%lu:\n", state->label_prefix, ins->u.cval); + fprintf(fp, "L%s%lu:\n", + state->compiler->label_prefix, (unsigned long)(ins->u.cval)); + break; + case OP_ADECL: + /* Ignore adecls with no registers error otherwise */ + if (!noop_adecl(ins)) { + internal_error(state, ins, "adecl remains?"); + } break; /* Ignore OP_PIECE */ case OP_PIECE: break; - /* Operations I am not yet certain how to handle */ - case OP_UMUL: + /* 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: @@ -16237,128 +24806,270 @@ 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; + last_occurance = 0; fp = state->output; + /* Masks for common sizes */ + fprintf(fp, ".section \"" DATA_SECTION "\"\n"); + fprintf(fp, ".balign 16\n"); + fprintf(fp, "L%s1:\n", state->compiler->label_prefix); + fprintf(fp, ".int 0xff, 0, 0, 0\n"); + fprintf(fp, "L%s2:\n", state->compiler->label_prefix); + fprintf(fp, ".int 0xffff, 0, 0, 0\n"); fprintf(fp, ".section \"" TEXT_SECTION "\"\n"); - first = RHS(state->main_function, 0); + first = state->first; 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; + last_occurance != ins->occurance) { + if (!ins->occurance->parent) { + fprintf(fp, "\t/* %s,%s:%d.%d */\n", + ins->occurance->function?ins->occurance->function:"(null)", + ins->occurance->filename?ins->occurance->filename:"(null)", + 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); print_instructions(state); - + } -static void print_tokens(struct compile_state *state) +static void print_preprocessed_tokens(struct compile_state *state) { - struct token *tk; - tk = &state->token[0]; - do { -#if 1 - token(state, 0); -#else - next_token(state, 0); -#endif - loc(stdout, state, 0); - printf("%s <- `%s'\n", - tokens[tk->tok], + int tok; + FILE *fp; + int line; + const char *filename; + fp = state->output; + filename = 0; + line = 0; + for(;;) { + struct file_state *file; + struct token *tk; + const char *token_str; + tok = peek(state); + if (tok == TOK_EOF) { + break; + } + tk = eat(state, tok); + token_str = tk->ident ? tk->ident->name : - tk->str_len ? tk->val.str : ""); - - } while(tk->tok != TOK_EOF); + tk->str_len ? tk->val.str : + tokens[tk->tok]; + + file = state->file; + while(file->macro && file->prev) { + file = file->prev; + } + if (!file->macro && + ((file->line != line) || (file->basename != filename))) + { + int i, col; + if ((file->basename == filename) && + (line < file->line)) { + while(line < file->line) { + fprintf(fp, "\n"); + line++; + } + } + else { + fprintf(fp, "\n#line %d \"%s\"\n", + file->line, file->basename); + } + line = file->line; + filename = file->basename; + col = get_col(file) - strlen(token_str); + for(i = 0; i < col; i++) { + fprintf(fp, " "); + } + } + + fprintf(fp, "%s ", token_str); + + if (state->compiler->debug & DEBUG_TOKENS) { + loc(state->dbgout, state, 0); + fprintf(state->dbgout, "%s <- `%s'\n", + tokens[tok], token_str); + } + } } -static void compile(const char *filename, const char *ofilename, - int cpu, int debug, int opt, const char *label_prefix) +static void compile(const char *filename, + struct compiler_state *compiler, struct arch_state *arch) { int i; struct compile_state state; + struct triple *ptr; + struct filelist *includes = include_filelist; memset(&state, 0, sizeof(state)); + state.compiler = compiler; + state.arch = arch; state.file = 0; for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) { memset(&state.token[i], 0, sizeof(state.token[i])); state.token[i].tok = -1; } - /* Remember the debug settings */ - state.cpu = cpu; - state.debug = debug; - state.optimize = opt; + /* Remember the output descriptors */ + state.errout = stderr; + state.dbgout = stdout; /* 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; + if ((state.compiler->flags & COMPILER_PP_ONLY) && (strcmp("auto.inc",state.compiler->ofilename) == 0)) { + state.output = stdout; + } else { + state.output = fopen(state.compiler->ofilename, "w"); + if (!state.output) { + error(&state, 0, "Cannot open output file %s\n", + state.compiler->ofilename); + } + } + /* Make certain a good cleanup happens */ + exit_state = &state; + atexit(exit_cleanup); + /* Prep the preprocessor */ state.if_depth = 0; - state.if_value = 0; + memset(state.if_bytes, 0, sizeof(state.if_bytes)); /* register the C keywords */ register_keywords(&state); /* register the keywords the macro preprocessor knows */ register_macro_keywords(&state); + /* generate some builtin macros */ + register_builtin_macros(&state); /* Memorize where some special keywords are. */ - state.i_continue = lookup(&state, "continue", 8); - state.i_break = lookup(&state, "break", 5); + state.i_switch = lookup(&state, "switch", 6); + state.i_case = lookup(&state, "case", 4); + state.i_continue = lookup(&state, "continue", 8); + state.i_break = lookup(&state, "break", 5); + state.i_default = lookup(&state, "default", 7); + state.i_return = lookup(&state, "return", 6); + /* Memorize where predefined macros are. */ + state.i___VA_ARGS__ = lookup(&state, "__VA_ARGS__", 11); + state.i___FILE__ = lookup(&state, "__FILE__", 8); + state.i___LINE__ = lookup(&state, "__LINE__", 8); + /* Memorize where predefined identifiers are. */ + state.i___func__ = lookup(&state, "__func__", 8); + /* Memorize where some attribute keywords are. */ + state.i_noinline = lookup(&state, "noinline", 8); + state.i_always_inline = lookup(&state, "always_inline", 13); + state.i_noreturn = lookup(&state, "noreturn", 8); + + /* Process the command line macros */ + process_cmdline_macros(&state); + + /* Allocate beginning bounding labels for the function list */ + state.first = label(&state); + state.first->id |= TRIPLE_FLAG_VOLATILE; + use_triple(state.first, state.first); + ptr = label(&state); + ptr->id |= TRIPLE_FLAG_VOLATILE; + use_triple(ptr, ptr); + flatten(&state, state.first, ptr); + + /* Allocate a label for the pool of global variables */ + state.global_pool = label(&state); + state.global_pool->id |= TRIPLE_FLAG_VOLATILE; + flatten(&state, state.first, state.global_pool); + /* Enter the globl definition scope */ start_scope(&state); register_builtins(&state); + compile_file(&state, filename, 1); -#if 0 - print_tokens(&state); -#endif + + while (includes) { + compile_file(&state, includes->filename, 1); + includes=includes->next; + } + + /* Stop if all we want is preprocessor output */ + if (state.compiler->flags & COMPILER_PP_ONLY) { + print_preprocessed_tokens(&state); + return; + } + decls(&state); + /* Exit the global definition scope */ end_scope(&state); - /* Now that basic compilation has happened - * optimize the intermediate code + /* Now that basic compilation has happened + * optimize the intermediate code */ optimize(&state); generate_code(&state); - if (state.debug) { - fprintf(stderr, "done\n"); + if (state.compiler->debug) { + fprintf(state.errout, "done\n"); } + exit_state = 0; } -static void version(void) +static void version(FILE *fp) { - printf("romcc " VERSION " released " RELEASE_DATE "\n"); + fprintf(fp, "romcc " VERSION " released " RELEASE_DATE "\n"); } static void usage(void) { - version(); - printf( - "Usage: romcc .c\n" - "Compile a C source file without using ram\n" + FILE *fp = stdout; + version(fp); + fprintf(fp, + "\nUsage: romcc [options] .c\n" + "Compile a C source file generating a binary that does not implicilty use RAM\n" + "Options: \n" + "-o \n" + "-f