X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=util%2Fromcc%2Fromcc.c;h=c7ef22366751613d40174a3170b1bd900d74b84e;hb=f31abe31f0dd47a9b99a4cabecd5f414a07418d4;hp=e03bb6b41b69ec1872b399c3a63fbfde5163b7e2;hpb=f8a2dddb573faef41ad43ee111d91d4c5259ad59;p=coreboot.git diff --git a/util/romcc/romcc.c b/util/romcc/romcc.c index e03bb6b41..c7ef22366 100644 --- a/util/romcc/romcc.c +++ b/util/romcc/romcc.c @@ -3,8 +3,8 @@ #undef RELEASE_DATE #undef VERSION #define VERSION_MAJOR "0" -#define VERSION_MINOR "64" -#define RELEASE_DATE "28 June 2004" +#define VERSION_MINOR "72" +#define RELEASE_DATE "10 February 2010" #define VERSION VERSION_MAJOR "." VERSION_MINOR #include @@ -25,6 +25,14 @@ #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 + #define DEBUG_CONSISTENCY 1 #define DEBUG_SDP_BLOCKS 0 #define DEBUG_TRIPLE_COLOR 0 @@ -46,12 +54,14 @@ #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 * +---/ * / @@ -64,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 } * @@ -107,7 +117,7 @@ * DDD EEE DDD: [ ] ( BBB ) EEE: [ JJJ ] () * | * FFF FFF: [ ] ( BBB ) - * / \ + * / \ * GGG HHH GGG: [ ] ( BBB ) HHH: [ BBB ] () * | * III III: [ BBB ] () @@ -115,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; @@ -209,12 +227,11 @@ static int exists(const char *dirname, const char *filename) static char *slurp_file(const char *dirname, const char *filename, off_t *r_size) { char cwd[MAX_CWD_SIZE]; - int fd; char *buf; off_t size, progress; ssize_t result; - struct stat stats; - + FILE* file; + if (!filename) { *r_size = 0; return 0; @@ -223,25 +240,22 @@ static char *slurp_file(const char *dirname, const char *filename, off_t *r_size die("cwd buffer to small"); } xchdir(dirname); - fd = open(filename, O_RDONLY); + file = fopen(filename, "rb"); xchdir(cwd); - if (fd < 0) { + 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; @@ -250,16 +264,14 @@ 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; } /* 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; @@ -268,7 +280,7 @@ typedef uint16_t ushort_t; typedef int32_t int_t; typedef uint32_t uint_t; typedef int32_t long_t; -typedef uint32_t ulong_t; +#define ulong_t uint32_t #define SCHAR_T_MIN (-128) #define SCHAR_T_MAX 127 @@ -328,7 +340,7 @@ struct file_state { struct file_state *prev; const char *basename; char *dirname; - char *buf; + const char *buf; off_t size; const char *pos; int line; @@ -336,11 +348,15 @@ struct file_state { 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; @@ -390,14 +406,14 @@ struct token { #define OP_SUB 9 #define OP_SL 10 #define OP_USR 11 -#define OP_SSR 12 -#define OP_AND 13 +#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 @@ -408,7 +424,7 @@ 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 */ @@ -467,7 +483,7 @@ struct token { * 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. * MISC(0) holds the destination pseudo register, which must be an OP_DECL. * RHS(0) holds the psuedo to move. @@ -536,10 +552,10 @@ struct token { * 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. + * components. * ???? registers ???? */ @@ -553,10 +569,10 @@ struct token { #define OP_FCALL 72 -/* OP_FCALL performs a procedure call. +/* 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_PROG 73 @@ -582,7 +598,7 @@ struct token { #define OP_CBRANCH 82 /* a conditional branch */ /* For conditional branch instructions * RHS(0) holds the branch condition. - * TARG(1) holds the branch target. + * 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 */ @@ -606,16 +622,16 @@ struct token { * ->use is the list of all branches that use this label. */ -#define OP_ADECL 87 +#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. + * components. */ #define OP_SDECL 88 @@ -627,12 +643,12 @@ struct token { #define OP_PHI 89 -/* OP_PHI is a triple used in SSA form code. +/* 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 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 @@ -645,12 +661,12 @@ struct token { /* continuation helpers */ #define OP_CPS_BRANCH 90 /* an unconditional branch */ -/* OP_CPS_BRANCH calls a continuation +/* 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 +/* 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 @@ -664,7 +680,7 @@ struct token { * ->next holds where the OP_CPS_RET will return to. */ #define OP_CPS_RET 93 -/* OP_CPS_RET conditionally calls one of two continuations +/* 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 @@ -796,14 +812,16 @@ static const struct op_info table_ops[] = { [OP_ADDRCONST ] = OP( 0, 0, 1, 0, PURE | DEF, "addrconst"), [OP_UNKNOWNVAL ] = OP( 0, 0, 0, 0, PURE | DEF, "unknown"), +#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_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_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"), @@ -874,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) { @@ -985,7 +1003,7 @@ struct block { struct block_set *ipdomfrontier; struct block *ipdom; int vertex; - + }; struct symbol { @@ -1002,9 +1020,8 @@ struct macro_arg { }; struct macro { struct hash_entry *ident; - char *buf; + const char *buf; int buf_len; - int buf_off; struct macro_arg *args; int argc; }; @@ -1047,7 +1064,7 @@ struct basic_blocks { struct block *first_block, *last_block; int last_vertex; }; -#define MAX_CPP_IF_DEPTH 63 +#define MAX_PP_IF_DEPTH 63 struct compile_state { struct compiler_state *compiler; struct arch_state *arch; @@ -1057,7 +1074,8 @@ struct compile_state { struct file_state *file; struct occurance *last_occurance; const char *function; - struct token token[4]; + 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; @@ -1065,6 +1083,7 @@ struct compile_state { 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__; @@ -1076,10 +1095,9 @@ struct compile_state { struct hash_entry *i_noinline; struct hash_entry *i_always_inline; int scope_depth; - unsigned char if_bytes[(MAX_CPP_IF_DEPTH + CHAR_BIT -1)/CHAR_BIT]; + unsigned char if_bytes[(MAX_PP_IF_DEPTH + CHAR_BIT -1)/CHAR_BIT]; int if_depth; int eat_depth, eat_targ; - int macro_line; struct file_state *macro_file; struct triple *functions; struct triple *main_function; @@ -1161,11 +1179,11 @@ struct compile_state { * make up the union. * type->elements hold the length of the linked list */ -#define TYPE_POINTER 0x1200 +#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 of the arguments @@ -1187,7 +1205,7 @@ struct compile_state { * type->elements holds the number of elements. */ #define TYPE_TUPLE 0x1900 -/* TYPE_TUPLE is a basic building block when defining +/* 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. @@ -1196,7 +1214,7 @@ struct compile_state { * type->elements hold the number of elements in the closure. */ #define TYPE_JOIN 0x1a00 -/* TYPE_JOIN is a basic building block when defining +/* 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. @@ -1289,16 +1307,19 @@ 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); @@ -1374,9 +1395,11 @@ static struct triple *flatten( #define COMPILER_SIMPLIFY_LOGICAL 0x00004000 #define COMPILER_SIMPLIFY_BITFIELD 0x00008000 -#define COMPILER_CPP_ONLY 0x80000000 +#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 | \ @@ -1467,9 +1490,9 @@ static int set_arg( } return result; } - -static void flag_usage(FILE *fp, const struct compiler_flag *ptr, + +static void flag_usage(FILE *fp, const struct compiler_flag *ptr, const char *prefix, const char *invert_prefix) { for(;ptr->name; ptr++) { @@ -1486,7 +1509,7 @@ static void arg_usage(FILE *fp, const struct compiler_arg *ptr, for(;ptr->name; ptr++) { const struct compiler_flag *flag; for(flag = &ptr->flags[0]; flag->name; flag++) { - fprintf(fp, "%s%s=%s\n", + fprintf(fp, "%s%s=%s\n", prefix, ptr->name, flag->name); } } @@ -1499,7 +1522,7 @@ static int append_string(size_t *max, const char ***vec, const char *str, count = ++(*max); *vec = xrealloc(*vec, sizeof(char *)*count, "name"); (*vec)[count -1] = 0; - (*vec)[count -2] = str; + (*vec)[count -2] = str; return 0; } @@ -1530,7 +1553,7 @@ static int append_define(struct compiler_state *compiler, const char *str) rest = identifier(str, end); if (rest != end) { int len = end - str - 1; - arg_error("Invalid name cannot define macro: `%*.*s'\n", + arg_error("Invalid name cannot define macro: `%*.*s'\n", len, len, str); } result = append_string(&compiler->define_count, @@ -1547,7 +1570,7 @@ static int append_undef(struct compiler_state *compiler, const char *str) rest = identifier(str, end); if (rest != end) { int len = end - str - 1; - arg_error("Invalid name cannot undefine macro: `%*.*s'\n", + arg_error("Invalid name cannot undefine macro: `%*.*s'\n", len, len, str); } result = append_string(&compiler->undef_count, @@ -1556,7 +1579,8 @@ static int append_undef(struct compiler_state *compiler, const char *str) } static const struct compiler_flag romcc_flags[] = { - { "cpp-only", COMPILER_CPP_ONLY }, + { "trigraphs", COMPILER_TRIGRAPHS }, + { "pp-only", COMPILER_PP_ONLY }, { "eliminate-inefectual-code", COMPILER_ELIMINATE_INEFECTUAL_CODE }, { "simplify", COMPILER_SIMPLIFY }, { "scc-transform", COMPILER_SCC_TRANSFORM }, @@ -1588,7 +1612,7 @@ static const struct compiler_arg romcc_args[] = { static const struct compiler_flag romcc_opt_flags[] = { { "-O", COMPILER_SIMPLIFY }, { "-O2", COMPILER_SIMPLIFY | COMPILER_SCC_TRANSFORM }, - { "-E", COMPILER_CPP_ONLY }, + { "-E", COMPILER_PP_ONLY }, { 0, 0, }, }; static const struct compiler_flag romcc_debug_flags[] = { @@ -1718,7 +1742,7 @@ static int get_col(struct file_state *file) for(col = 0; ptr < end; ptr++) { if (*ptr != '\t') { col++; - } + } else { col = (col & ~7) + 8; } @@ -1732,7 +1756,7 @@ static void loc(FILE *fp, struct compile_state *state, struct triple *triple) if (triple && triple->occurance) { struct occurance *spot; for(spot = triple->occurance; spot; spot = spot->parent) { - fprintf(fp, "%s:%d.%d: ", + fprintf(fp, "%s:%d.%d: ", spot->filename, spot->line, spot->col); } return; @@ -1741,11 +1765,11 @@ static void loc(FILE *fp, struct compile_state *state, struct triple *triple) return; } col = get_col(state->file); - fprintf(fp, "%s:%d.%d: ", + 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, +static void __attribute__ ((noreturn)) internal_error(struct compile_state *state, struct triple *ptr, const char *fmt, ...) { FILE *fp = state->errout; @@ -1765,7 +1789,7 @@ static void internal_error(struct compile_state *state, struct triple *ptr, } -static void internal_warning(struct compile_state *state, struct triple *ptr, +static void internal_warning(struct compile_state *state, struct triple *ptr, const char *fmt, ...) { FILE *fp = state->errout; @@ -1783,7 +1807,7 @@ static void internal_warning(struct compile_state *state, struct triple *ptr, -static void error(struct compile_state *state, struct triple *ptr, +static void __attribute__ ((noreturn)) error(struct compile_state *state, struct triple *ptr, const char *fmt, ...) { FILE *fp = state->errout; @@ -1804,14 +1828,14 @@ static void error(struct compile_state *state, struct triple *ptr, exit(1); } -static void warning(struct compile_state *state, struct triple *ptr, +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(fp, state, ptr); - fprintf(fp, "warning: "); + fprintf(fp, "warning: "); if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) { fprintf(fp, "%p %-10s ", ptr, tops(ptr->op)); } @@ -1838,6 +1862,7 @@ static void valid_ins(struct compile_state *state, struct triple *ptr) valid_op(state, ptr->op); } +#if DEBUG_ROMCC_WARNING static void valid_param_count(struct compile_state *state, struct triple *ins) { int lhs, rhs, misc, targ; @@ -1860,66 +1885,7 @@ static void valid_param_count(struct compile_state *state, struct triple *ins) internal_error(state, ins, "Bad targ count"); } } - -static void process_trigraphs(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) >= 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++; - } - } - while(src != end) { - *dest++ = *src++; - } - 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++; - } - } - while(src != end) { - *dest++ = *src++; - } - file->size = dest - file->buf; -} +#endif static struct type void_type; static struct type unknown_type; @@ -1937,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. */ @@ -2012,7 +1978,7 @@ static struct occurance *new_occurance(struct compile_state *state) (last->line == line) && (last->function == function) && ((last->filename == filename) || - (strcmp(last->filename, filename) == 0))) + (strcmp(last->filename, filename) == 0))) { get_occurance(last); return last; @@ -2108,7 +2074,7 @@ static struct triple unknown_triple = { static size_t registers_of(struct compile_state *state, struct type *type); -static struct triple *alloc_triple(struct compile_state *state, +static struct triple *alloc_triple(struct compile_state *state, int op, struct type *type, int lhs_wanted, int rhs_wanted, struct occurance *occurance) { @@ -2199,7 +2165,16 @@ struct triple *dup_triple(struct compile_state *state, struct triple *src) 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; @@ -2209,7 +2184,7 @@ static struct triple *new_triple(struct compile_state *state, 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, struct occurance *occurance) { @@ -2226,7 +2201,7 @@ 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; @@ -2242,7 +2217,7 @@ 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; @@ -2285,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; @@ -2295,7 +2270,7 @@ static int triple_stores_block(struct compile_state *state, struct triple *ins) } static int triple_is_branch(struct compile_state *state, struct triple *ins); -static struct block *block_of_triple(struct compile_state *state, +static struct block *block_of_triple(struct compile_state *state, struct triple *ins) { struct triple *first; @@ -2304,8 +2279,8 @@ static struct block *block_of_triple(struct compile_state *state, } first = state->first; while(ins != first && !triple_is_branch(state, ins->prev) && - !triple_stores_block(state, ins)) - { + !triple_stores_block(state, ins)) + { if (ins == ins->prev) { internal_error(state, ins, "ins == ins->prev?"); } @@ -2481,30 +2456,30 @@ static void display_triple(FILE *fp, struct triple *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), + 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) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>", - ins, pre, post, vol, reg, ins->template_id, tops(ins->op), + 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), + 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), + 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) %c%c%c %-7s %-2d %-10s", + 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>", + fprintf(fp, " <%2d-%2d:%2d>", ins->u.bitfield.offset, ins->u.bitfield.offset + ins->u.bitfield.size, ins->u.bitfield.size); @@ -2534,9 +2509,9 @@ static void display_triple(FILE *fp, struct triple *ins) fprintf(fp, " @"); for(ptr = ins->occurance; ptr; ptr = ptr->parent) { fprintf(fp, " %s,%s:%d.%d", - ptr->function, + ptr->function, ptr->filename, - ptr->line, + ptr->line, ptr->col); } if (ins->op == OP_ASM) { @@ -2557,9 +2532,9 @@ static void display_triple_changes( orig_count = TRIPLE_SIZE(orig); if ((new->op != orig->op) || (new_count != orig_count) || - (memcmp(orig->param, new->param, + (memcmp(orig->param, new->param, orig_count * sizeof(orig->param[0])) != 0) || - (memcmp(&orig->u, &new->u, sizeof(orig->u)) != 0)) + (memcmp(&orig->u, &new->u, sizeof(orig->u)) != 0)) { struct occurance *ptr; int i, min_count, indent; @@ -2567,7 +2542,7 @@ static void display_triple_changes( if (orig->op == new->op) { fprintf(fp, " %-11s", tops(orig->op)); } else { - fprintf(fp, " [%-10s %-10s]", + fprintf(fp, " [%-10s %-10s]", tops(new->op), tops(orig->op)); } min_count = new_count; @@ -2576,12 +2551,12 @@ static void display_triple_changes( } for(indent = i = 0; i < min_count; i++) { if (orig->param[i] == new->param[i]) { - fprintf(fp, " %-11p", + fprintf(fp, " %-11p", orig->param[i]); indent += 12; } else { fprintf(fp, " [%-10p %-10p]", - new->param[i], + new->param[i], orig->param[i]); indent += 24; } @@ -2596,7 +2571,7 @@ static void display_triple_changes( } if ((new->op == OP_INTCONST)|| (new->op == OP_ADDRCONST)) { - fprintf(fp, " <0x%08lx>", + fprintf(fp, " <0x%08lx>", (unsigned long)(new->u.cval)); indent += 13; } @@ -2617,11 +2592,11 @@ static void display_triple_changes( fprintf(fp, " @"); for(ptr = orig->occurance; ptr; ptr = ptr->parent) { fprintf(fp, " %s,%s:%d.%d", - ptr->function, + ptr->function, ptr->filename, - ptr->line, + ptr->line, ptr->col); - + } fprintf(fp, "\n"); fflush(fp); @@ -2631,7 +2606,7 @@ static void display_triple_changes( 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; @@ -2644,7 +2619,7 @@ static int triple_is_pure(struct compile_state *state, struct triple *ins, unsig return (pure == PURE) && !(id & TRIPLE_FLAG_VOLATILE); } -static int triple_is_branch_type(struct compile_state *state, +static int triple_is_branch_type(struct compile_state *state, struct triple *ins, unsigned type) { /* Is this one of the passed branch types? */ @@ -2686,12 +2661,14 @@ static int triple_is_ret(struct compile_state *state, struct triple *ins) return triple_is_branch_type(state, ins, RETBRANCH); } +#if DEBUG_ROMCC_WARNING static int triple_is_simple_ubranch(struct compile_state *state, struct triple *ins) { /* 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) { @@ -2764,27 +2741,27 @@ static struct triple **triple_iter(struct compile_state *state, } } 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), + 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), + 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), + return triple_iter(state, ins->misc, &MISC(ins,0), ins, last); } @@ -2852,7 +2829,7 @@ static struct triple **triple_targ(struct compile_state *state, static struct triple **triple_edge_targ(struct compile_state *state, struct triple *ins, struct triple **last) { - return do_triple_targ(state, ins, last, + return do_triple_targ(state, ins, last, state->functions_joined, !state->functions_joined); } @@ -2883,7 +2860,7 @@ static struct triple *after_lhs(struct compile_state *state, struct triple *ins) } /* Function piece accessor functions */ -static struct triple *do_farg(struct compile_state *state, +static struct triple *do_farg(struct compile_state *state, struct triple *func, unsigned index) { struct type *ftype; @@ -2912,7 +2889,7 @@ 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, +static struct triple *farg(struct compile_state *state, struct triple *func, unsigned index) { return do_farg(state, func, index + 2); @@ -2961,13 +2938,16 @@ 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); + +#if DEBUG_ROMCC_WARNINGS #warning "AUDIT ME ->rhs" +#endif size = user->rhs; param = &RHS(user, 0); for(i = 0; i < size; i++) { @@ -3076,126 +3056,131 @@ static void release_triple(struct compile_state *state, struct triple *ptr) 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_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_DEFINED 111 -#define TOK_EOF 112 +#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 ] = "{", @@ -3290,19 +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_DEFINED ] = "defined", +[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", }; @@ -3326,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; @@ -3356,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; } @@ -3366,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( @@ -3408,7 +3406,7 @@ static void symbol( romcc_symbol(state, ident, chain, def, type, state->scope_depth); } -static void var_symbol(struct compile_state *state, +static void var_symbol(struct compile_state *state, struct hash_entry *ident, struct triple *def) { if ((def->type->type & TYPE_MASK) == TYPE_PRODUCT) { @@ -3417,7 +3415,7 @@ static void var_symbol(struct compile_state *state, symbol(state, ident, &ident->sym_ident, def, def->type); } -static void label_symbol(struct compile_state *state, +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); @@ -3446,7 +3444,7 @@ 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++) { @@ -3507,17 +3505,18 @@ 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); } @@ -3528,7 +3527,7 @@ static void undef_macro(struct compile_state *state, struct hash_entry *ident) struct macro_arg *arg, *anext; macro = ident->sym_define; ident->sym_define = 0; - + /* Free the macro arguments... */ anext = macro->args; while(anext) { @@ -3545,49 +3544,65 @@ static void undef_macro(struct compile_state *state, struct hash_entry *ident) } } -static void define_macro( - struct compile_state *state, - struct hash_entry *ident, - const char *value, int value_len, int value_off, - struct macro_arg *args) +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) { - /* Explicitly allow identical redefinitions of the same macro */ - if ((macro->buf_len == value_len) && - (memcmp(macro->buf, value, value_len) == 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, "%s: `%*.*s'\n", - ident->name, - value_len - value_off, - value_len - value_off, - value + value_off); + 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_len = value_len; - macro->buf_off = value_off; + macro->ident = ident; + macro->buf = body; + macro->buf_len = body_len; macro->args = args; - macro->buf = xmalloc(macro->buf_len + 2, "macro buf"); - - macro->argc = 0; - for(arg = args; arg; arg = arg->next) { - macro->argc += 1; - } - - memcpy(macro->buf, value, macro->buf_len); - macro->buf[macro->buf_len] = '\n'; - macro->buf[macro->buf_len+1] = '\0'; + 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) { @@ -3597,7 +3612,7 @@ static void register_builtin_macro(struct compile_state *state, internal_error(state, 0, "Builtin macros with arguments not supported"); } ident = lookup(state, name, strlen(name)); - define_macro(state, ident, value, strlen(value), 0, 0); + define_macro(state, ident, value, strlen(value), -1, 0); } static void register_builtin_macros(struct compile_state *state) @@ -3626,7 +3641,7 @@ static void register_builtin_macros(struct compile_state *state) 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) @@ -3646,7 +3661,7 @@ static void process_cmdline_macros(struct compile_state *state) body++; } ident = lookup(state, name, name_len); - define_macro(state, ident, body, strlen(body), 0, 0); + define_macro(state, ident, body, strlen(body), -1, 0); } for(macro = state->compiler->undefs; (name = *macro); macro++) { ident = lookup(state, name, strlen(name)); @@ -3663,7 +3678,6 @@ static int spacep(int c) case '\f': case '\v': case '\r': - case '\n': ret = 1; break; } @@ -3674,7 +3688,7 @@ 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; @@ -3694,7 +3708,7 @@ 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': @@ -3703,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')) { @@ -3722,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; @@ -3795,7 +3809,7 @@ static int char_value(struct compile_state *state, case '?': c = '?'; str++; break; case '\'': c = '\''; str++; break; case '"': c = '"'; str++; break; - case 'x': + case 'x': c = 0; str++; while((str < end) && hexdigitp(*str)) { @@ -3804,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)) { @@ -3822,165 +3836,263 @@ static int char_value(struct compile_state *state, return c; } -static const char *after_digits(const char *ptr, const 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 pos; +} + +static int get_char(struct file_state *file, const char *pos) +{ + 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 const char *after_octdigits(const char *ptr, const char *end) +static const char *after_octdigits(struct file_state *file, const char *ptr) { - while((ptr < end) && octdigitp(*ptr)) { - ptr++; + while(octdigitp(get_char(file, ptr))) { + ptr = next_char(file, ptr, 1); } return ptr; } -static const char *after_hexdigits(const char *ptr, const char *end) +static const char *after_hexdigits(struct file_state *file, const char *ptr) { - while((ptr < end) && hexdigitp(*ptr)) { - 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, +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 int lparen_peek(struct compile_state *state, struct file_state *file) -{ - const char *tokp, *end; - /* Is the next token going to be an lparen? - * Whitespace tokens are significant for seeing if a macro - * should be expanded. - */ - tokp = file->pos; - end = file->buf + file->size; - return (tokp < end) && (*tokp == '('); -} - -static void raw_next_token(struct compile_state *state, +static void raw_next_token(struct compile_state *state, struct file_state *file, struct token *tk) { const char *token; int c, c1, c2, c3; - const char *tokp, *end; + const char *tokp; + int eat; int tok; 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->report_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->report_line++; - file->line_start = tokp +1; break; } + tokp = next_char(file, tokp, 1); } } /* Comments */ else if ((c == '/') && (c1 == '*')) { - int line; - const 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->report_line += line - file->line; - file->line = line; - file->line_start = line_start; } /* string constants */ - else if ((c == '"') || - ((c == 'L') && (c1 == '"'))) { - int line; - const 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; @@ -3990,37 +4102,28 @@ static void raw_next_token(struct compile_state *state, if (tok == TOK_UNKNOWN) { error(state, 0, "unterminated string constant"); } - if (line != file->line) { + if (multiline) { warning(state, 0, "multiline string constant"); } - file->report_line += line - file->line; - 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; - const 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; @@ -4030,22 +4133,19 @@ static void raw_next_token(struct compile_state *state, if (tok == TOK_UNKNOWN) { error(state, 0, "unterminated character constant"); } - if (line != file->line) { + if (multiline) { warning(state, 0, "multiline character constant"); } - file->report_line += line - file->line; - 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} @@ -4053,122 +4153,140 @@ static void raw_next_token(struct compile_state *state, * .{digits}[Ee][+-]?{digits} * .{digits} */ - else if (digitp(c) || ((c == '.') && (digitp(c1)))) { - const 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++; - } - new = after_digits(next, end); - is_float = (new != 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(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; - tokp = identifier(tokp, end); - tokp -= 1; - tk->ident = lookup(state, token, tokp +1 - token); + + /* 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; - if ((tokp < end) && (tokp[1] == '$')) { - tokp++; + c = get_char(file, tokp); + if (c == '$') { + tokp = next_char(file, tokp, 1); tk->val.notmacro = 1; } } /* 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; } @@ -4194,34 +4312,12 @@ static void raw_next_token(struct compile_state *state, else if (c == '.') { tok = TOK_DOT; } else if (c == '~') { tok = TOK_TILDE; } else if (c == '#') { tok = TOK_MACRO; } + 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); - } -} - -static void next_token(struct compile_state *state, struct token *tk) -{ - struct file_state *file; - file = state->file; - /* Don't return space tokens. */ - do { - raw_next_token(state, file, tk); - if (tk->tok == TOK_MACRO) { - /* Only match preprocessor directives at the start of a line */ - const char *ptr; - for(ptr = file->line_start; spacep(*ptr); ptr++) - ; - if (ptr != file->pos - 1) { - tk->tok = TOK_UNKNOWN; - } - } - if (tk->tok == TOK_UNKNOWN) { - error(state, 0, "unknown token"); - } - } while(tk->tok == TOK_SPACE); + tk->pos = token; } static void check_tok(struct compile_state *state, struct token *tk, int tok) @@ -4230,7 +4326,7 @@ static void check_tok(struct compile_state *state, struct token *tk, int tok) const char *name1, *name2; name1 = tokens[tk->tok]; name2 = ""; - if (tk->tok == TOK_IDENT) { + if ((tk->tok == TOK_IDENT) || (tk->tok == TOK_MIDENT)) { name2 = tk->ident->name; } error(state, 0, "\tfound %s %s expected %s", @@ -4240,11 +4336,11 @@ static void check_tok(struct compile_state *state, struct token *tk, int tok) struct macro_arg_value { struct hash_entry *ident; - unsigned char *value; + char *value; size_t len; }; static struct macro_arg_value *read_macro_args( - struct compile_state *state, struct macro *macro, + struct compile_state *state, struct macro *macro, struct file_state *file, struct token *tk) { struct macro_arg_value *argv; @@ -4256,7 +4352,7 @@ static struct macro_arg_value *read_macro_args( do { raw_next_token(state, file, tk); } while(tk->tok == TOK_SPACE); - return 0; + return NULL; } argv = xcmalloc(sizeof(*argv) * macro->argc, "macro args"); for(i = 0, arg = macro->args; arg; arg = arg->next, i++) { @@ -4266,15 +4362,15 @@ static struct macro_arg_value *read_macro_args( } 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__)) + (argv[i].ident != state->i___VA_ARGS__)) { i++; if (i >= macro->argc) { @@ -4283,11 +4379,11 @@ static struct macro_arg_value *read_macro_args( } continue; } - + if (tk->tok == TOK_LPAREN) { paren_depth++; } - + if (tk->tok == TOK_RPAREN) { if (paren_depth == 0) { break; @@ -4297,15 +4393,15 @@ static struct macro_arg_value *read_macro_args( if (tk->tok == TOK_EOF) { error(state, 0, "End of file encountered while parsing macro arguments"); } - - len = file->pos - start; + + len = char_strlen(file, start, file->pos); argv[i].value = xrealloc( argv[i].value, argv[i].len + len, "macro args"); - memcpy(argv[i].value + argv[i].len, start, len); + char_strcpy((char *)argv[i].value + argv[i].len, file, start, file->pos); argv[i].len += len; } if (i != macro->argc -1) { - error(state, 0, "missing %s arg %d\n", + error(state, 0, "missing %s arg %d\n", macro->ident->name, i +2); } return argv; @@ -4326,50 +4422,73 @@ struct macro_buf { size_t len, pos; }; +static void grow_macro_buf(struct compile_state *state, + const char *id, struct macro_buf *buf, + size_t grow) +{ + if ((buf->pos + grow) >= buf->len) { + buf->str = xrealloc(buf->str, buf->len + grow, id); + buf->len += grow; + } +} + static void append_macro_text(struct compile_state *state, - struct macro *macro, struct macro_buf *buf, + const char *id, struct macro_buf *buf, const char *fstart, size_t flen) { + 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, fstart); + flen, flen, buf->str + buf->pos); +#endif + buf->pos += flen; +} + + +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) +{ + 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 - if ((buf->pos + flen) < buf->len) { - memcpy(buf->str + buf->pos, fstart, flen); - } else { - buf->str = xrealloc(buf->str, buf->len + flen, macro->ident->name); - memcpy(buf->str + buf->pos, fstart, flen); - buf->len += flen; - } buf->pos += flen; } -static int compile_macro(struct compile_state *state, +static int compile_macro(struct compile_state *state, struct file_state **filep, struct token *tk); -static void macro_expand_args(struct compile_state *state, +static void macro_expand_args(struct compile_state *state, struct macro *macro, struct macro_arg_value *argv, struct token *tk) { - size_t i; - + int i; + for(i = 0; i < macro->argc; i++) { struct file_state fmacro, *file; struct macro_buf buf; - const char *fstart; - size_t flen; + fmacro.prev = 0; fmacro.basename = argv[i].ident->name; fmacro.dirname = ""; + fmacro.buf = (char *)argv[i].value; fmacro.size = argv[i].len; - fmacro.buf = argv[i].value; fmacro.pos = fmacro.buf; - fmacro.line_start = 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.prev = 0; + fmacro.macro = 1; + fmacro.trigraphs = 0; + fmacro.join_lines = 0; buf.len = argv[i].len; buf.str = xmalloc(buf.len, argv[i].ident->name); @@ -4377,10 +4496,11 @@ static void macro_expand_args(struct compile_state *state, file = &fmacro; for(;;) { - fstart = file->pos; raw_next_token(state, file, tk); - flen = file->pos - fstart; - + + /* If we have recursed into another macro body + * get out of it. + */ if (tk->tok == TOK_EOF) { struct file_state *old; old = file; @@ -4400,10 +4520,10 @@ static void macro_expand_args(struct compile_state *state, } } - append_macro_text(state, macro, &buf, - fstart, flen); + append_macro_chars(state, macro->ident->name, &buf, + file, tk->pos, file->pos); } - + xfree(argv[i].value); argv[i].value = buf.str; argv[i].len = buf.pos; @@ -4419,23 +4539,29 @@ static void expand_macro(struct compile_state *state, const char space[] = " "; const char *fstart; size_t flen; - size_t i, j; - fmacro.basename = macro->ident->name; - fmacro.dirname = ""; - fmacro.size = macro->buf_len - macro->buf_off;; - fmacro.buf = macro->buf + macro->buf_off; - fmacro.pos = fmacro.buf; - fmacro.line_start = fmacro.buf; - fmacro.line = 1; - fmacro.report_line = 1; + 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.prev = 0; - + 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) { @@ -4455,7 +4581,7 @@ static void expand_macro(struct compile_state *state, flen = argv[i].len; break; case TOK_MACRO: - if (!macro->buf_off) { + if (macro->argc < 0) { break; } do { @@ -4472,21 +4598,21 @@ static void expand_macro(struct compile_state *state, tk->ident->name); } /* Stringize token */ - append_macro_text(state, macro, buf, "\"", 1); + 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, buf, str, len); + append_macro_text(state, macro->ident->name, buf, str, len); } - append_macro_text(state, macro, buf, "\"", 1); + append_macro_text(state, macro->ident->name, buf, "\"", 1); fstart = 0; flen = 0; break; @@ -4520,8 +4646,8 @@ static void expand_macro(struct compile_state *state, break; } - append_macro_text(state, macro, buf, fstart, flen); - + append_macro_text(state, macro->ident->name, buf, fstart, flen); + fstart = fmacro.pos; raw_next_token(state, &fmacro, tk); } @@ -4537,43 +4663,50 @@ static void tag_macro_name(struct compile_state *state, struct file_state fmacro; const char *fstart; size_t flen; - fmacro.basename = macro->ident->name; - fmacro.dirname = ""; - fmacro.size = buf->pos; - fmacro.buf = buf->str; - fmacro.pos = fmacro.buf; - fmacro.line_start = fmacro.buf; - fmacro.line = 1; + + /* 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.prev = 0; - + 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, buf, fstart, flen); + (tk->val.notmacro == 0)) + { + append_macro_text(state, macro->ident->name, buf, fstart, flen); fstart = "$"; flen = 1; } - append_macro_text(state, macro, buf, fstart, flen); - + append_macro_text(state, macro->ident->name, buf, fstart, flen); + fstart = fmacro.pos; raw_next_token(state, &fmacro, tk); } xfree(fmacro.buf); } - -static int compile_macro(struct compile_state *state, + +static int compile_macro(struct compile_state *state, struct file_state **filep, struct token *tk) { struct file_state *file; @@ -4595,13 +4728,13 @@ static int compile_macro(struct compile_state *state, /* If I am a function like macro and the identifier is not followed * by a left parenthesis, do nothing. */ - if ((macro->buf_off != 0) && !lparen_peek(state, *filep)) { + if ((macro->argc >= 0) && (get_char(*filep, (*filep)->pos) != '(')) { return 0; } /* Read in the macro arguments */ argv = 0; - if (macro->buf_off) { + if (macro->argc >= 0) { raw_next_token(state, *filep, tk); check_tok(state, tk, TOK_LPAREN); @@ -4634,7 +4767,6 @@ static int compile_macro(struct compile_state *state, * be regonized as a canidate for macro expansion. */ tag_macro_name(state, macro, &buf, tk); - append_macro_text(state, macro, &buf, "\n\0", 2); #if 0 fprintf(state->errout, "%s: %d -> `%*.*s'\n", @@ -4644,558 +4776,405 @@ static int compile_macro(struct compile_state *state, free_macro_args(macro, argv); file = xmalloc(sizeof(*file), "file_state"); - file->basename = xstrdup(ident->name); - file->dirname = xstrdup(""); - file->buf = buf.str; - file->size = buf.pos - 2; - file->pos = file->buf; - file->line_start = file->pos; - file->line = 1; + 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->prev = *filep; + file->macro = 1; + file->trigraphs = 0; + file->join_lines = 0; *filep = file; return 1; } +static void eat_tokens(struct compile_state *state, int targ_tok) +{ + if (state->eat_depth > 0) { + internal_error(state, 0, "Already eating..."); + } + state->eat_depth = state->if_depth; + state->eat_targ = targ_tok; +} +static int if_eat(struct compile_state *state) +{ + return state->eat_depth > 0; +} +static int if_value(struct compile_state *state) +{ + int index, offset; + index = state->if_depth / CHAR_BIT; + offset = state->if_depth % CHAR_BIT; + return !!(state->if_bytes[index] & (1 << (offset))); +} +static void set_if_value(struct compile_state *state, int value) +{ + int index, offset; + index = state->if_depth / CHAR_BIT; + offset = state->if_depth % CHAR_BIT; -static int mpeek(struct compile_state *state, int index) + state->if_bytes[index] &= ~(1 << offset); + if (value) { + state->if_bytes[index] |= (1 << offset); + } +} +static void in_if(struct compile_state *state, const char *name) { - struct token *tk; - int rescan; - tk = &state->token[index + 1]; - if (tk->tok == -1) { - do { - raw_next_token(state, state->file, tk); - } while(tk->tok == TOK_SPACE); + if (state->if_depth <= 0) { + error(state, 0, "%s without #if", name); } - 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 */ - if (file->report_dir != file->dirname) { - xfree(file->report_dir); - } - xfree(file->dirname); - xfree(file->buf); - xfree(file); - next_token(state, tk); - rescan = 1; - } - else if (tk->ident && tk->ident->sym_define) { - rescan = compile_macro(state, &state->file, tk); - if (rescan) { - next_token(state, tk); - } - - } - } while(rescan); - /* Don't show the token on the next line */ - if (state->macro_line < state->macro_file->line) { - return TOK_EOF; +} +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"); } - return tk->tok; } - -static void meat(struct compile_state *state, int index, int tok) +static void reenter_if(struct compile_state *state, const char *name) { - int i; - int next_tok; - next_tok = mpeek(state, index); - if (next_tok != tok) { - check_tok(state, &state->token[index + 1], tok); + in_if(state, name); + if ((state->eat_depth == state->if_depth) && + (state->eat_targ == TOK_MELSE)) { + state->eat_depth = 0; + state->eat_targ = 0; } - - /* 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); +} +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; } - for(i = index; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) { - state->token[i] = state->token[i + 1]; +} +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; } - memset(&state->token[i], 0, sizeof(state->token[i])); - state->token[i].tok = -1; + state->if_depth -= 1; } -static int mpeek_raw(struct compile_state *state, int index) +static void raw_token(struct compile_state *state, struct token *tk) { - struct token *tk; + struct file_state *file; int rescan; - tk = &state->token[index + 1]; - if (tk->tok == -1) { - do { - raw_next_token(state, state->file, tk); - } while(tk->tok == TOK_SPACE); - } + + file = state->file; + raw_next_token(state, file, tk); do { rescan = 0; - if ((tk->tok == TOK_EOF) && - (state->file != state->macro_file) && - (state->file->prev)) { - struct file_state *file = state->file; + file = state->file; + /* 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 */ - if (file->report_dir != file->dirname) { - xfree(file->report_dir); - } xfree(file->dirname); xfree(file->buf); xfree(file); - next_token(state, tk); + file = 0; + raw_next_token(state, state->file, tk); rescan = 1; } } while(rescan); - /* Don't show the token on the next line */ - if (state->macro_line < state->macro_file->line) { - return TOK_EOF; - } - return tk->tok; } -static void meat_raw(struct compile_state *state, int index, int tok) +static void pp_token(struct compile_state *state, struct token *tk) { - int next_tok; - int i; - next_tok = mpeek_raw(state, index); - if (next_tok != tok) { - check_tok(state, &state->token[index + 1], tok); - } + int rescan; - /* 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]; - } - memset(&state->token[i], 0, sizeof(state->token[i])); - state->token[i].tok = -1; + 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 long_t mcexpr(struct compile_state *state, int index); +static void preprocess(struct compile_state *state, struct token *tk); -static long_t mprimary_expr(struct compile_state *state, int index) +static void token(struct compile_state *state, struct token *tk) { - long_t val; - int tok; - 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: - { - long lval; - char *end; - meat(state, index, TOK_LIT_INT); - errno = 0; - lval = strtol(state->token[index].val.str, &end, 0); - if ((lval > LONG_T_MAX) || (lval < LONG_T_MIN) || - (((lval == LONG_MIN) || (lval == LONG_MAX)) && - (errno == ERANGE))) { - error(state, 0, "Integer constant `%s' to large", state->token[index].val.str); + 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; + } } - val = lval; - break; - } - default: - meat(state, index, TOK_LIT_INT); - val = 0; - } - return val; -} -static long_t munary_expr(struct compile_state *state, int index) -{ - long_t val; - int tok; - tok = mpeek(state, index); - if ((tok == TOK_IDENT) && - (state->token[index + 1].ident == state->i_defined)) { - tok = TOK_DEFINED; - } - switch(tok) { - 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; - case TOK_DEFINED: - { - struct hash_entry *ident; - int parens; - meat(state, index, TOK_IDENT); - parens = 0; - if (mpeek_raw(state, index) == TOK_LPAREN) { - meat(state, index, TOK_LPAREN); - parens = 1; + /* Expand a macro call */ + else if (tk->ident && tk->ident->sym_define) { + rescan = compile_macro(state, &state->file, tk); + if (rescan) { + pp_token(state, tk); + } } - meat_raw(state, index, TOK_IDENT); - ident = state->token[index].ident; - val = ident->sym_define != 0; - if (parens) { - meat(state, index, TOK_RPAREN); + /* 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; } - break; - } - default: - val = mprimary_expr(state, index); - break; - } - return val; - -} -static long_t mmul_expr(struct compile_state *state, int index) -{ - 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; + /* 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; } - } while(!done); - - return val; + /* Error on unknown tokens */ + else if (tk->tok == TOK_UNKNOWN) { + error(state, 0, "unknown token"); + } + } while(rescan); } -static long_t madd_expr(struct compile_state *state, int index) -{ - 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; - break; - case TOK_MINUS: - meat(state, index, TOK_MINUS); - right = mmul_expr(state, index); - val = val - right; - break; - default: - done = 1; - break; - } - } while(!done); - return val; +static inline struct token *get_token(struct compile_state *state, int offset) +{ + 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[index]; } -static long_t mshift_expr(struct compile_state *state, int index) +static struct token *do_eat_token(struct compile_state *state, int tok) { - 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; - } - } while(!done); + struct token *tk; + int i; + check_tok(state, get_token(state, 1), tok); - return val; + /* Free the old token value */ + tk = get_token(state, 0); + if (tk->str_len) { + memset((void *)tk->val.str, -1, tk->str_len); + xfree(tk->val.str); + } + /* 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; + + /* Return the token */ + return tk; } -static long_t mrel_expr(struct compile_state *state, int index) +static int raw_peek(struct compile_state *state) { - 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; + struct token *tk1; + tk1 = get_token(state, 1); + if (tk1->tok == -1) { + raw_token(state, tk1); + } + return tk1->tok; } -static long_t meq_expr(struct compile_state *state, int index) +static struct token *raw_eat(struct compile_state *state, int tok) { - 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; + raw_peek(state); + return do_eat_token(state, tok); } -static long_t mand_expr(struct compile_state *state, int index) +static int pp_peek(struct compile_state *state) { - long_t val; - val = meq_expr(state, index); - while (mpeek(state, index) == TOK_AND) { - long_t right; - meat(state, index, TOK_AND); - right = meq_expr(state, index); - val = val & right; + struct token *tk1; + tk1 = get_token(state, 1); + if (tk1->tok == -1) { + pp_token(state, tk1); } - return val; + return tk1->tok; } -static long_t mxor_expr(struct compile_state *state, int index) +static struct token *pp_eat(struct compile_state *state, int tok) { - long_t val; - val = mand_expr(state, index); - while (mpeek(state, index) == TOK_XOR) { - long_t right; - meat(state, index, TOK_XOR); - right = mand_expr(state, index); - val = val ^ right; - } - return val; + pp_peek(state); + return do_eat_token(state, tok); } -static long_t mor_expr(struct compile_state *state, int index) +static int peek(struct compile_state *state) { - long_t val; - val = mxor_expr(state, index); - while (mpeek(state, index) == TOK_OR) { - long_t right; - meat(state, index, TOK_OR); - right = mxor_expr(state, index); - val = val | right; + struct token *tk1; + tk1 = get_token(state, 1); + if (tk1->tok == -1) { + token(state, tk1); } - return val; + return tk1->tok; } -static long_t mland_expr(struct compile_state *state, int index) +static int peek2(struct compile_state *state) { - long_t val; - val = mor_expr(state, index); - while (mpeek(state, index) == TOK_LOGAND) { - long_t right; - meat(state, index, TOK_LOGAND); - right = mor_expr(state, index); - val = val && right; + struct token *tk1, *tk2; + tk1 = get_token(state, 1); + tk2 = get_token(state, 2); + if (tk1->tok == -1) { + token(state, tk1); } - return val; -} -static long_t mlor_expr(struct compile_state *state, int index) -{ - long_t val; - val = mland_expr(state, index); - while (mpeek(state, index) == TOK_LOGOR) { - long_t right; - meat(state, index, TOK_LOGOR); - right = mland_expr(state, index); - val = val || right; + if (tk2->tok == -1) { + token(state, tk2); } - return val; + return tk2->tok; } -static long_t mcexpr(struct compile_state *state, int index) +static struct token *eat(struct compile_state *state, int tok) { - return mlor_expr(state, index); + peek(state); + return do_eat_token(state, tok); } -static void eat_tokens(struct compile_state *state, int targ_tok) -{ - if (state->eat_depth > 0) { - internal_error(state, 0, "Already eating..."); - } - state->eat_depth = state->if_depth; - state->eat_targ = targ_tok; -} -static int if_eat(struct compile_state *state) -{ - return state->eat_depth > 0; -} -static int if_value(struct compile_state *state) -{ - int index, offset; - index = state->if_depth / CHAR_BIT; - offset = state->if_depth % CHAR_BIT; - return !!(state->if_bytes[index] & (1 << (offset))); -} -static void set_if_value(struct compile_state *state, int value) +static void compile_file(struct compile_state *state, const char *filename, int local) { - int index, offset; - index = state->if_depth / CHAR_BIT; - offset = state->if_depth % CHAR_BIT; + char cwd[MAX_CWD_SIZE]; + const char *subdir, *base; + int subdir_len; + struct file_state *file; + char *basename; + file = xmalloc(sizeof(*file), "file_state"); - state->if_bytes[index] &= ~(1 << offset); - if (value) { - state->if_bytes[index] |= (1 << offset); + base = strrchr(filename, '/'); + subdir = filename; + if (base != 0) { + subdir_len = base - filename; + base++; } -} -static void in_if(struct compile_state *state, const char *name) -{ - if (state->if_depth <= 0) { - error(state, 0, "%s without #if", name); + else { + base = filename; + subdir_len = 0; } -} -static void enter_if(struct compile_state *state) -{ - state->if_depth += 1; - if (state->if_depth > MAX_CPP_IF_DEPTH) { - error(state, 0, "#if depth too great"); + basename = xmalloc(strlen(base) +1, "basename"); + strcpy(basename, base); + file->basename = basename; + + if (getcwd(cwd, sizeof(cwd)) == 0) { + die("cwd buffer to small"); } -} -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_ELSE)) { - state->eat_depth = 0; - state->eat_targ = 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'; } -} -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_ELSE)) { - state->eat_depth = 0; - state->eat_targ = 0; + else { + const char *dir; + int dirlen; + const char **path; + /* Find the appropriate directory... */ + dir = 0; + if (!state->file && exists(cwd, filename)) { + dir = cwd; + } + if (local && state->file && exists(state->file->dirname, filename)) { + dir = state->file->dirname; + } + for(path = state->compiler->include_paths; !dir && *path; path++) { + if (exists(*path, filename)) { + dir = *path; + } + } + if (!dir) { + error(state, 0, "Cannot open `%s'\n", filename); + } + dirlen = strlen(dir); + file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname"); + memcpy(file->dirname, dir, dirlen); + file->dirname[dirlen] = '/'; + memcpy(file->dirname + dirlen + 1, subdir, subdir_len); + file->dirname[dirlen + 1 + subdir_len] = '\0'; } + file->buf = slurp_file(file->dirname, file->basename, &file->size); + + 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; } -static void exit_if(struct compile_state *state, const char *name) + +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) { - in_if(state, name); - if (state->eat_depth == state->if_depth) { - state->eat_depth = 0; - state->eat_targ = 0; + struct triple *cvalue; + cvalue = constant_expr(state); + integral(state, cvalue); + if (cvalue->op != OP_INTCONST) { + error(state, 0, "integer constant expected"); } - state->if_depth -= 1; + return cvalue->u.cval != 0; } -static void preprocess(struct compile_state *state, int index) +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. */ - struct file_state *file; - struct token *tk; - int line; + int old_token_base; int tok; - - file = state->file; - tk = &state->token[index]; - state->macro_line = line = file->line; - state->macro_file = file; - next_token(state, tk); - ident_to_macro(state, tk); - if (tk->tok == TOK_IDENT) { - error(state, 0, "undefined preprocessing directive `%s'", - tk->ident->name); - } - switch(tk->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); - next_token(state, tk); - /* I have a cpp line marker parse it */ - if (tk->tok == TOK_LIT_STRING) { + /* 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, '/'); @@ -5213,20 +5192,24 @@ static void preprocess(struct compile_state *state, int index) dir = xmalloc(dir_len + 1, "report_dir"); memcpy(dir, token, dir_len); dir[dir_len] = '\0'; - file->report_line = override_line - 1; - file->report_name = name; - file->report_dir = dir; + state->file->report_line = override_line - 1; + state->file->report_name = name; + state->file->report_dir = dir; + state->file->macro = 0; } break; } - case TOK_LINE: - meat(state, index, TOK_LIT_INT); - file->report_line = strtoul(tk->val.str, 0, 10) -1; - if (mpeek(state, index) == TOK_LIT_STRING) { + 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; - meat(state, index, TOK_LIT_STRING); + tk = pp_eat(state, TOK_LIT_STRING); name = xmalloc(tk->str_len, "report_name"); token = tk->val.str + 1; base = strrchr(token, '/'); @@ -5244,145 +5227,136 @@ static void preprocess(struct compile_state *state, int index) dir = xmalloc(dir_len + 1, "report_dir"); memcpy(dir, token, dir_len); dir[dir_len] = '\0'; - file->report_name = name; - file->report_dir = dir; + state->file->report_name = name; + state->file->report_dir = dir; + state->file->macro = 0; } break; - case TOK_UNDEF: + } + case TOK_MUNDEF: { struct hash_entry *ident; + pp_eat(state, TOK_MUNDEF); if (if_eat(state)) /* quit early when #if'd out */ break; - meat_raw(state, index, TOK_IDENT); - ident = tk->ident; + ident = pp_eat(state, TOK_MIDENT)->ident; undef_macro(state, ident); break; } - case TOK_PRAGMA: + case TOK_MPRAGMA: + pp_eat(state, TOK_MPRAGMA); if (if_eat(state)) /* quit early when #if'd out */ break; - warning(state, 0, "Ignoring preprocessor directive: %s", - tk->ident->name); + warning(state, 0, "Ignoring pragma"); break; - case TOK_ELIF: + 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_ENDIF); + eat_tokens(state, TOK_MENDIF); } - /* If the previous #if was not taken see if the #elif enables the + /* If the previous #if was not taken see if the #elif enables the * trailing code. */ else { - set_if_value(state, mcexpr(state, index) != 0); + set_if_value(state, mcexpr(state)); if (!if_value(state)) { - eat_tokens(state, TOK_ELSE); + eat_tokens(state, TOK_MELSE); } } break; - case TOK_IF: + 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, index) != 0); + set_if_value(state, mcexpr(state)); if (!if_value(state)) { - eat_tokens(state, TOK_ELSE); + eat_tokens(state, TOK_MELSE); } break; - case TOK_IFNDEF: + 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; - next_token(state, tk); - if ((line != file->line) || (tk->tok != TOK_IDENT)) { - error(state, 0, "Invalid macro name"); - } - set_if_value(state, tk->ident->sym_define == 0); + ident = pp_eat(state, TOK_MIDENT)->ident; + set_if_value(state, ident->sym_define == 0); if (!if_value(state)) { - eat_tokens(state, TOK_ELSE); + eat_tokens(state, TOK_MELSE); } break; - case TOK_IFDEF: + } + 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; - next_token(state, tk); - if ((line != file->line) || (tk->tok != TOK_IDENT)) { - error(state, 0, "Invalid macro name"); - } - set_if_value(state, tk->ident->sym_define != 0); + ident = pp_eat(state, TOK_MIDENT)->ident; + set_if_value(state, ident->sym_define != 0); if (!if_value(state)) { - eat_tokens(state, TOK_ELSE); + eat_tokens(state, TOK_MELSE); } break; - case TOK_ELSE: + } + case TOK_MELSE: + pp_eat(state, TOK_MELSE); enter_else(state, "#else"); if (!if_eat(state) && if_value(state)) { - eat_tokens(state, TOK_ENDIF); + eat_tokens(state, TOK_MENDIF); } break; - case TOK_ENDIF: + case TOK_MENDIF: + pp_eat(state, TOK_MENDIF); exit_if(state, "#endif"); break; - case TOK_DEFINE: + case TOK_MDEFINE: { struct hash_entry *ident; struct macro_arg *args, **larg; - const char *start, *mstart, *ptr; + const char *mstart, *mend; + int argc; + pp_eat(state, TOK_MDEFINE); if (if_eat(state)) /* quit early when #if'd out */ break; - - meat_raw(state, index, TOK_IDENT); - ident = tk->ident; + ident = pp_eat(state, TOK_MIDENT)->ident; + argc = -1; args = 0; larg = &args; - /* Remember the start of the macro */ - start = file->pos; - - /* Find the end of the line. */ - for(ptr = start; *ptr != '\n'; ptr++) - ; - - /* remove the trailing whitespace */ - while(spacep(*ptr)) { - ptr--; - } - - /* Remove leading whitespace */ - while(spacep(*start) && (start < ptr)) { - start++; - } - /* Remember where the macro starts */ - mstart = start; - /* Parse macro parameters */ - if (lparen_peek(state, state->file)) { - meat_raw(state, index, TOK_LPAREN); - + 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 = mpeek_raw(state, index); + tok = pp_peek(state); if (!args && (tok == TOK_RPAREN)) { break; } else if (tok == TOK_DOTS) { - meat_raw(state, index, TOK_DOTS); + pp_eat(state, TOK_DOTS); aident = state->i___VA_ARGS__; - } + } else { - meat_raw(state, index, TOK_IDENT); - aident = tk->ident; + aident = pp_eat(state, TOK_MIDENT)->ident; } - + narg = xcmalloc(sizeof(*arg), "macro arg"); narg->ident = aident; @@ -5396,68 +5370,105 @@ static void preprocess(struct compile_state *state, int index) /* Add the new argument to the end of the list */ *larg = narg; larg = &narg->next; + argc += 1; if ((aident == state->i___VA_ARGS__) || - (mpeek(state, index) != TOK_COMMA)) { + (pp_peek(state) != TOK_COMMA)) { break; } - meat_raw(state, index, TOK_COMMA); + pp_eat(state, TOK_COMMA); } - meat_raw(state, index, TOK_RPAREN); + pp_eat(state, TOK_RPAREN); + } + /* Remove leading whitespace */ + while(raw_peek(state) == TOK_SPACE) { + raw_eat(state, TOK_SPACE); + } - /* Get the start of the macro body */ - mstart = file->pos; + /* Remember the start of the macro body */ + tok = raw_peek(state); + mend = mstart = get_token(state, 1)->pos; - /* Remove leading whitespace */ - while(spacep(*mstart) && (mstart < ptr)) { - mstart++; + /* 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; } } - define_macro(state, ident, start, ptr - start + 1, - mstart - start, args); + + /* 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_ERROR: + case TOK_MERROR: { - const char *end; + 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 */ - for(end = file->pos; *end != '\n'; end++) - ; - len = (end - file->pos); + 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, file->pos); + error(state, 0, "%*.*s", len, len, start); } - file->pos = end; break; } - case TOK_WARNING: + case TOK_MWARNING: { - const char *end; + 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 */ - for(end = file->pos; *end != '\n'; end++) - ; - len = (end - file->pos); + 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, file->pos); + warning(state, 0, "%*.*s", len, len, start); } - file->pos = end; break; } - case TOK_INCLUDE: + case TOK_MINCLUDE: { char *name; - const char *ptr; int local; local = 0; name = 0; - next_token(state, tk); - if (tk->tok == TOK_LIT_STRING) { + + 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; @@ -5469,208 +5480,71 @@ static void preprocess(struct compile_state *state, int index) name[name_len] = '\0'; local = 1; } - else if (tk->tok == TOK_LESS) { - const char *start, *end; - start = file->pos; - for(end = start; *end != '\n'; end++) { - if (*end == '>') { - break; - } + 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); } - if (*end == '\n') { - error(state, 0, "Unterminated included directive"); + append_macro_text(state, "include", &buf, "\0", 1); + if (peek(state) != TOK_MORE) { + error(state, 0, "Unterminated include directive"); } - name = xmalloc(end - start + 1, "include"); - memcpy(name, start, end - start); - name[end - start] = '\0'; - file->pos = end +1; + eat(state, TOK_MORE); local = 0; + name = buf.str; } else { error(state, 0, "Invalid include directive"); } - /* Error if there are any characters after the include */ - for(ptr = file->pos; *ptr != '\n'; ptr++) { - switch(*ptr) { - case ' ': - case '\t': - case '\v': - break; - default: - error(state, 0, "garbage after 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); - next_token(state, tk); - return; + break; } + case TOK_EOL: + /* Ignore # without a follwing ident */ + break; default: - /* Ignore # without a following ident */ - if (tk->tok == TOK_IDENT) { - error(state, 0, "Invalid preprocessor directive: %s", - tk->ident->name); + { + 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 = mpeek_raw(state, index); - meat_raw(state, index, tok); - } while(tok != TOK_EOF); + 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; } -static void token(struct compile_state *state, int index) -{ - struct file_state *file; - struct token *tk; - int rescan; - - tk = &state->token[index]; - next_token(state, tk); - do { - rescan = 0; - file = state->file; - if (tk->tok == TOK_EOF && file->prev) { - state->file = file->prev; - /* file->basename is used keep it */ - xfree(file->dirname); - xfree(file->buf); - xfree(file); - next_token(state, tk); - rescan = 1; - } - else if (tk->tok == TOK_MACRO) { - preprocess(state, index); - rescan = 1; - } - else if (tk->ident && tk->ident->sym_define) { - rescan = compile_macro(state, &state->file, tk); - if (rescan) { - next_token(state, tk); - } - } - else if (if_eat(state)) { - next_token(state, tk); - rescan = 1; - } - } 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) -{ - if (state->token[1].tok == -1) { - token(state, 1); - } - if (state->token[2].tok == -1) { - token(state, 2); - } - return state->token[2].tok; -} - -static void eat(struct compile_state *state, int tok) -{ - int i; - peek(state); - check_tok(state, &state->token[1], tok); - - /* Free the old token value */ - if (state->token[0].str_len) { - xfree((void *)(state->token[0].val.str)); - } - for(i = 0; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) { - state->token[i] = state->token[i + 1]; - } - memset(&state->token[i], 0, sizeof(state->token[i])); - state->token[i].tok = -1; -} - -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; - char *basename; - file = xmalloc(sizeof(*file), "file_state"); - - base = strrchr(filename, '/'); - subdir = filename; - if (base != 0) { - subdir_len = base - filename; - base++; - } - else { - base = filename; - subdir_len = 0; - } - basename = xmalloc(strlen(base) +1, "basename"); - strcpy(basename, base); - file->basename = basename; - - if (getcwd(cwd, sizeof(cwd)) == 0) { - die("cwd buffer to small"); - } - if (subdir[0] == '/') { - file->dirname = xmalloc(subdir_len + 1, "dirname"); - memcpy(file->dirname, subdir, subdir_len); - file->dirname[subdir_len] = '\0'; - } - else { - const char *dir; - int dirlen; - const char **path; - /* Find the appropriate directory... */ - dir = 0; - if (!state->file && exists(cwd, filename)) { - dir = cwd; - } - if (local && state->file && exists(state->file->dirname, filename)) { - dir = state->file->dirname; - } - 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); - } - dirlen = strlen(dir); - file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname"); - memcpy(file->dirname, dir, dirlen); - file->dirname[dirlen] = '/'; - memcpy(file->dirname + dirlen + 1, subdir, subdir_len); - file->dirname[dirlen + 1 + subdir_len] = '\0'; - } - file->buf = slurp_file(file->dirname, file->basename, &file->size); - - file->pos = file->buf; - file->line_start = file->pos; - file->line = 1; - - file->report_line = 1; - file->report_name = file->basename; - file->report_dir = file->dirname; - - file->prev = state->file; - state->file = file; - - process_trigraphs(state); - splice_lines(state); -} - /* Type helper functions */ static struct type *new_type( @@ -5741,7 +5615,7 @@ static struct type *invalid_type(struct compile_state *state, struct type *type) case TYPE_STRUCT: case TYPE_TUPLE: member = type->left; - while(member && (invalid == 0) && + while(member && (invalid == 0) && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) { invalid = invalid_type(state, member->left); member = member->right; @@ -5767,7 +5641,7 @@ static struct type *invalid_type(struct compile_state *state, struct type *type) break; } return invalid; - + } #define MASK_UCHAR(X) ((X) & ((ulong_t)0xff)) @@ -5775,7 +5649,7 @@ static struct type *invalid_type(struct compile_state *state, struct type *type) static inline ulong_t mask_uint(ulong_t x) { if (SIZEOF_INT < SIZEOF_LONG) { - ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT))) -1; + ulong_t mask = (1ULL << ((ulong_t)(SIZEOF_INT))) -1; x &= mask; } return x; @@ -5786,7 +5660,9 @@ static inline ulong_t mask_uint(ulong_t 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 }; +#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 }; @@ -5799,11 +5675,13 @@ static struct type void_ptr_type = { .left = &void_type, }; -static struct type void_func_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) { @@ -5931,19 +5809,19 @@ static void name_of(FILE *fp, struct type *type) name_of(fp, type->right); break; case TYPE_ENUM: - fprintf(fp, "enum %s", + fprintf(fp, "enum %s", (type->type_ident)? type->type_ident->name : ""); qual_of(fp, type); break; case TYPE_STRUCT: - fprintf(fp, "struct %s { ", + 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 { ", + fprintf(fp, "union %s { ", (type->type_ident)? type->type_ident->name : ""); name_of(fp, type->left); fprintf(fp, " } "); @@ -5960,7 +5838,7 @@ static void name_of(FILE *fp, struct type *type) fprintf(fp, " [%ld]", (long)(type->elements)); break; case TYPE_TUPLE: - fprintf(fp, "tuple { "); + fprintf(fp, "tuple { "); name_of(fp, type->left); fprintf(fp, " } "); qual_of(fp, type); @@ -6107,7 +5985,7 @@ static size_t align_of_in_bytes(struct compile_state *state, struct type *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, +static size_t needed_padding(struct compile_state *state, struct type *type, size_t offset) { size_t padding, align; @@ -6129,7 +6007,7 @@ static size_t needed_padding(struct compile_state *state, return padding; } -static size_t reg_needed_padding(struct compile_state *state, +static size_t reg_needed_padding(struct compile_state *state, struct type *type, size_t offset) { size_t padding, align; @@ -6138,7 +6016,7 @@ static size_t reg_needed_padding(struct compile_state *state, * fit into the current register. */ if (((type->type & TYPE_MASK) == TYPE_BITFIELD) && - (((offset + type->elements)/REG_SIZEOF_REG) != (offset/REG_SIZEOF_REG))) + (((offset + type->elements)/REG_SIZEOF_REG) != (offset/REG_SIZEOF_REG))) { align = REG_SIZEOF_REG; } @@ -6335,7 +6213,7 @@ 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, +static size_t field_offset(struct compile_state *state, struct type *type, struct hash_entry *field) { struct type *member; @@ -6376,7 +6254,7 @@ static size_t field_offset(struct compile_state *state, return size; } -static size_t field_reg_offset(struct compile_state *state, +static size_t field_reg_offset(struct compile_state *state, struct type *type, struct hash_entry *field) { struct type *member; @@ -6417,7 +6295,7 @@ static size_t field_reg_offset(struct compile_state *state, return size; } -static struct type *field_type(struct compile_state *state, +static struct type *field_type(struct compile_state *state, struct type *type, struct hash_entry *field) { struct type *member; @@ -6446,14 +6324,14 @@ static struct type *field_type(struct compile_state *state, else { internal_error(state, 0, "field_type only works on structures and unions"); } - + if (!member || (member->field_ident != field)) { error(state, 0, "member %s not present", field->name); } return member; } -static size_t index_offset(struct compile_state *state, +static size_t index_offset(struct compile_state *state, struct type *type, ulong_t index) { struct type *member; @@ -6499,14 +6377,14 @@ static size_t index_offset(struct compile_state *state, } } else { - internal_error(state, 0, + internal_error(state, 0, "request for index %u in something not an array, tuple or join", index); } return size; } -static size_t index_reg_offset(struct compile_state *state, +static size_t index_reg_offset(struct compile_state *state, struct type *type, ulong_t index) { struct type *member; @@ -6533,7 +6411,7 @@ static size_t index_reg_offset(struct compile_state *state, if (i != index) { internal_error(state, 0, "Missing member index: %u", index); } - + } else if ((type->type & TYPE_MASK) == TYPE_JOIN) { ulong_t i; @@ -6553,7 +6431,7 @@ static size_t index_reg_offset(struct compile_state *state, } } else { - internal_error(state, 0, + internal_error(state, 0, "request for index %u in something not an array, tuple or join", index); } @@ -6604,7 +6482,7 @@ static struct type *index_type(struct compile_state *state, } else { member = 0; - internal_error(state, 0, + internal_error(state, 0, "request for index %u in something not an array, tuple or join", index); } @@ -6829,7 +6707,7 @@ static struct type *reg_type( name_of(state->errout, type); fprintf(state->errout, "\n"); internal_error(state, 0, "reg_type not yet defined for type"); - + } } /* If I have a single register compound type not a bit-field @@ -6857,7 +6735,7 @@ static struct type *reg_type( } static struct type *next_field(struct compile_state *state, - struct type *type, struct type *prev_member) + struct type *type, struct type *prev_member) { struct type *member; if ((type->type & TYPE_MASK) != TYPE_STRUCT) { @@ -6878,13 +6756,13 @@ static struct type *next_field(struct compile_state *state, prev_member = 0; } if (prev_member) { - internal_error(state, 0, "prev_member %s not present", + internal_error(state, 0, "prev_member %s not present", prev_member->field_ident->name); } return member; } -typedef void (*walk_type_fields_cb_t)(struct compile_state *state, struct type *type, +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, @@ -6907,15 +6785,15 @@ static void walk_struct_fields(struct compile_state *state, if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) { mtype = mtype->left; } - walk_type_fields(state, mtype, - reg_offset + + walk_type_fields(state, mtype, + reg_offset + field_reg_offset(state, type, mtype->field_ident), - mem_offset + + mem_offset + field_offset(state, type, mtype->field_ident), cb, arg); tptr = tptr->right; } - + } static void walk_type_fields(struct compile_state *state, @@ -7202,7 +7080,7 @@ static struct type *compatible_ptrs(struct type *left, struct type *right) result = new_type(qual_type, result, 0); } return result; - + } static struct triple *integral_promotion( struct compile_state *state, struct triple *def) @@ -7222,7 +7100,7 @@ static struct triple *integral_promotion( def->type = new_type(int_type, 0, 0); } else { - def = triple(state, OP_CONVERT, + def = triple(state, OP_CONVERT, new_type(int_type, 0, 0), def, 0); } } @@ -7281,7 +7159,7 @@ static int is_compound_type(struct type *type) case TYPE_STRUCT: case TYPE_TUPLE: case TYPE_UNION: - case TYPE_JOIN: + case TYPE_JOIN: is_compound = 1; break; default: @@ -7321,8 +7199,8 @@ static int is_lvalue(struct compile_state *state, struct triple *def) 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_LIST)) { @@ -7339,7 +7217,7 @@ static void clvalue(struct compile_state *state, struct triple *def) if (!def) { internal_error(state, def, "nothing where lvalue expected?"); } - if (!is_lvalue(state, def)) { + if (!is_lvalue(state, def)) { error(state, def, "lvalue expected"); } } @@ -7383,7 +7261,7 @@ static struct triple *int_const( static struct triple *read_expr(struct compile_state *state, struct triple *def); -static struct triple *do_mk_addr_expr(struct compile_state *state, +static struct triple *do_mk_addr_expr(struct compile_state *state, struct triple *expr, struct type *type, ulong_t offset) { struct triple *result; @@ -7392,7 +7270,7 @@ static struct triple *do_mk_addr_expr(struct compile_state *state, 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"); @@ -7544,7 +7422,9 @@ static struct triple *read_expr(struct compile_state *state, struct triple *def) if (!def) { return 0; } +#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)) { @@ -7566,7 +7446,7 @@ static struct triple *read_expr(struct compile_state *state, struct triple *def) return def; } -int is_write_compatible(struct compile_state *state, +int is_write_compatible(struct compile_state *state, struct type *dest, struct type *rval) { int compatible = 0; @@ -7623,7 +7503,6 @@ static struct triple *write_expr( struct compile_state *state, struct triple *dest, struct triple *rval) { struct triple *def; - int op; def = 0; if (!rval) { @@ -7646,7 +7525,6 @@ static struct triple *write_expr( } /* Now figure out which assignment operator to use */ - op = -1; if (is_in_reg(state, dest)) { def = triple(state, OP_WRITE, dest->type, rval, dest); if (MISC(def, 0) != dest) { @@ -7718,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); } @@ -7734,7 +7612,7 @@ struct type *ptr_arithmetic_result( /* 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) { @@ -7750,7 +7628,7 @@ 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); @@ -7761,28 +7639,29 @@ static struct triple *mkland_expr( 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 */ - left = write_expr(state, var, left); + lstore = write_expr(state, var, left); /* Jump if the value is false */ - jmp = branch(state, end, + jmp = branch(state, end, lfalse_expr(state, read_expr(state, var))); mid = label(state); - + /* Store the right hand side value */ - right = write_expr(state, var, right); + 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, left, jmp, mid, right, end, val, 0); - + def = mkprog(state, var, lstore, jmp, mid, rstore, end, val, 0UL); + return def; } @@ -7795,28 +7674,28 @@ static struct triple *mklor_expr( /* 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, 0); + def = mkprog(state, var, left, jmp, mid, right, end, val, 0UL); return def; } static struct triple *mkcond_expr( - struct compile_state *state, + struct compile_state *state, struct triple *test, struct triple *left, struct triple *right) { struct triple *def, *val, *var, *jmp1, *jmp2, *top, *mid, *end; @@ -7871,12 +7750,12 @@ static struct triple *mkcond_expr( /* 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, 0); + def = mkprog(state, var, jmp1, top, left, jmp2, mid, right, end, val, 0UL); return def; } @@ -7884,7 +7763,9 @@ static struct triple *mkcond_expr( 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)) { @@ -7961,7 +7842,7 @@ static struct triple *flatten_generic( 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); @@ -7992,7 +7873,7 @@ static struct triple *flatten_prog( unuse_triple(first, body->prev); use_triple(body, body->prev); } - + if (!(val->id & TRIPLE_FLAG_FLATTENED)) { internal_error(state, val, "val not flattened?"); } @@ -8104,7 +7985,7 @@ static struct triple *flatten( 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, + ptr = triple(state, OP_ADD, left->type, read_expr(state, left), int_const(state, &ulong_type, offset)); free_triple(state, base); @@ -8325,8 +8206,8 @@ static struct triple *mk_add_expr( 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, + 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); @@ -8352,8 +8233,8 @@ static struct triple *mk_sub_expr( 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, + 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); @@ -8399,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); @@ -8431,12 +8312,12 @@ 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. + * So far only OP_INTCONST qualifies. */ return (ins->op == OP_INTCONST); } -static int constants_equal(struct compile_state *state, +static int constants_equal(struct compile_state *state, struct triple *left, struct triple *right) { int equal; @@ -8498,6 +8379,7 @@ static int is_one(struct triple *ins) return is_simple_const(ins) && (ins->u.cval == 1); } +#if DEBUG_ROMCC_WARNING static long_t bit_count(ulong_t value) { int count; @@ -8512,8 +8394,10 @@ static long_t bit_count(ulong_t value) } } return count; - + } +#endif + static long_t bsr(ulong_t value) { int i; @@ -8542,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) @@ -8560,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; } @@ -8573,12 +8457,12 @@ static ulong_t read_const(struct compile_state *state, struct triple *ins, struct triple *rhs) { 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: @@ -8628,7 +8512,7 @@ int const_eq(struct compile_state *state, struct triple *ins, rval = read_const(state, ins, right); result = (lval == rval); } - else if ((left->op == OP_ADDRCONST) && + else if ((left->op == OP_ADDRCONST) && (right->op == OP_ADDRCONST)) { result = (MISC(left, 0) == MISC(right, 0)) && (left->u.cval == right->u.cval); @@ -8638,7 +8522,7 @@ int const_eq(struct compile_state *state, struct triple *ins, result = -1; } return result; - + } int const_ucmp(struct compile_state *state, struct triple *ins, @@ -8663,7 +8547,7 @@ int const_ucmp(struct compile_state *state, struct triple *ins, result = -1; } } - else if ((left->op == OP_ADDRCONST) && + else if ((left->op == OP_ADDRCONST) && (right->op == OP_ADDRCONST) && (MISC(left, 0) == MISC(right, 0))) { result = 0; @@ -8731,6 +8615,7 @@ static void unuse_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; @@ -8759,8 +8644,9 @@ static void check_lhs(struct compile_state *state, struct triple *ins) 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) { @@ -8798,6 +8684,7 @@ static void wipe_ins(struct compile_state *state, struct triple *ins) 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 @@ -8813,8 +8700,9 @@ static void wipe_branch(struct compile_state *state, struct triple *ins) 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; @@ -8836,7 +8724,7 @@ static void mkcopy(struct compile_state *state, 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)) { @@ -8866,7 +8754,7 @@ static void mkaddr_const(struct compile_state *state, } #if DEBUG_DECOMPOSE_PRINT_TUPLES -static void print_tuple(struct compile_state *state, +static void print_tuple(struct compile_state *state, struct triple *ins, struct triple *tuple) { FILE *fp = state->dbgout; @@ -8877,11 +8765,11 @@ static void print_tuple(struct compile_state *state, name_of(fp, LHS(tuple, 0)->type); } fprintf(fp, "\n"); - + } #endif -static struct triple *decompose_with_tuple(struct compile_state *state, +static struct triple *decompose_with_tuple(struct compile_state *state, struct triple *ins, struct triple *tuple) { struct triple *next; @@ -8903,7 +8791,7 @@ static struct triple *decompose_with_tuple(struct compile_state *state, propogate_use(state, ins, tuple); release_triple(state, ins); - + return next; } @@ -8921,7 +8809,7 @@ static struct triple *decompose_unknownval(struct compile_state *state, #endif get_occurance(ins->occurance); - tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1, + tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1, ins->occurance); for(i = 0; i < tuple->lhs; i++) { @@ -8938,7 +8826,7 @@ static struct triple *decompose_unknownval(struct compile_state *state, } -static struct triple *decompose_read(struct compile_state *state, +static struct triple *decompose_read(struct compile_state *state, struct triple *ins) { struct triple *tuple, *lval; @@ -8954,7 +8842,7 @@ static struct triple *decompose_read(struct compile_state *state, ins->occurance); if ((tuple->lhs != lval->lhs) && - (!triple_is_def(state, lval) || (tuple->lhs != 1))) + (!triple_is_def(state, lval) || (tuple->lhs != 1))) { internal_error(state, ins, "lhs size inconsistency?"); } @@ -8974,7 +8862,7 @@ static struct triple *decompose_read(struct compile_state *state, } get_occurance(tuple->occurance); - read = alloc_triple(state, OP_READ, piece->type, -1, -1, + read = alloc_triple(state, OP_READ, piece->type, -1, -1, tuple->occurance); RHS(read, 0) = piece; @@ -9001,12 +8889,12 @@ static struct triple *decompose_read(struct compile_state *state, return decompose_with_tuple(state, ins, tuple); } -static struct triple *decompose_write(struct compile_state *state, +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); @@ -9014,7 +8902,7 @@ static struct triple *decompose_write(struct compile_state *state, ins->occurance); if ((tuple->lhs != lval->lhs) && - (!triple_is_def(state, lval) || tuple->lhs != 1)) + (!triple_is_def(state, lval) || tuple->lhs != 1)) { internal_error(state, ins, "lhs size inconsistency?"); } @@ -9034,7 +8922,7 @@ static struct triple *decompose_write(struct compile_state *state, } pval = LHS(val, i); } - + /* See if the piece is really a bitref */ bitref = 0; if (piece->op == OP_BITREF) { @@ -9062,7 +8950,7 @@ static struct triple *decompose_write(struct compile_state *state, } get_occurance(tuple->occurance); - write = alloc_triple(state, OP_WRITE, piece->type, -1, -1, + write = alloc_triple(state, OP_WRITE, piece->type, -1, -1, tuple->occurance); MISC(write, 0) = piece; RHS(write, 0) = pval; @@ -9081,7 +8969,7 @@ static void decompose_load_cb(struct compile_state *state, { struct decompose_load_info *info = arg; struct triple *load; - + if (reg_offset > info->tuple->lhs) { internal_error(state, info->tuple, "lhs to small?"); } @@ -9091,7 +8979,7 @@ static void decompose_load_cb(struct compile_state *state, LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = load; } -static struct triple *decompose_load(struct compile_state *state, +static struct triple *decompose_load(struct compile_state *state, struct triple *ins) { struct triple *tuple; @@ -9124,7 +9012,7 @@ static void decompose_store_cb(struct compile_state *state, { struct decompose_store_info *info = arg; struct triple *store; - + if (reg_offset > info->tuple->lhs) { internal_error(state, info->tuple, "lhs to small?"); } @@ -9135,7 +9023,7 @@ static void decompose_store_cb(struct compile_state *state, LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = store; } -static struct triple *decompose_store(struct compile_state *state, +static struct triple *decompose_store(struct compile_state *state, struct triple *ins) { struct triple *tuple; @@ -9157,7 +9045,7 @@ static struct triple *decompose_store(struct compile_state *state, return decompose_with_tuple(state, ins, tuple); } -static struct triple *decompose_dot(struct compile_state *state, +static struct triple *decompose_dot(struct compile_state *state, struct triple *ins) { struct triple *tuple, *lval; @@ -9179,7 +9067,7 @@ static struct triple *decompose_dot(struct compile_state *state, #endif get_occurance(ins->occurance); - tuple = alloc_triple(state, OP_TUPLE, type, -1, -1, + tuple = alloc_triple(state, OP_TUPLE, type, -1, -1, ins->occurance); if (((ins->type->type & TYPE_MASK) == TYPE_BITFIELD) && @@ -9214,7 +9102,7 @@ static struct triple *decompose_dot(struct compile_state *state, piece->u.bitfield.offset = reg_offset % REG_SIZEOF_REG; } else if ((reg_offset % REG_SIZEOF_REG) != 0) { - internal_error(state, ins, + internal_error(state, ins, "request for a nonbitfield sub register?"); } @@ -9224,7 +9112,7 @@ static struct triple *decompose_dot(struct compile_state *state, return decompose_with_tuple(state, ins, tuple); } -static struct triple *decompose_index(struct compile_state *state, +static struct triple *decompose_index(struct compile_state *state, struct triple *ins) { struct triple *tuple, *lval; @@ -9244,7 +9132,7 @@ static struct triple *decompose_index(struct compile_state *state, #endif get_occurance(ins->occurance); - tuple = alloc_triple(state, OP_TUPLE, type, -1, -1, + tuple = alloc_triple(state, OP_TUPLE, type, -1, -1, ins->occurance); for(i = 0; i < tuple->lhs; i++, idx++) { @@ -9272,8 +9160,10 @@ static struct triple *decompose_index(struct compile_state *state, 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; @@ -9314,7 +9204,7 @@ static void decompose_compound_types(struct compile_state *state) case OP_INDEX: next = decompose_index(state, ins); break; - + } #if DEBUG_DECOMPOSE_HIRES fprintf(fp, "decompose next: %p \n", next); @@ -9338,7 +9228,7 @@ static void decompose_compound_types(struct compile_state *state) else { release_triple(state, ins); } - } + } ins = next; } while(ins != first); ins = first; @@ -9347,7 +9237,7 @@ static void decompose_compound_types(struct compile_state *state) if (ins->op == OP_BITREF) { if (ins->use) { internal_error(state, ins, "bitref used"); - } + } else { release_triple(state, ins); } @@ -10077,7 +9967,7 @@ static void simplify_sextract(struct compile_state *state, struct triple *ins) val &= mask; val <<= (SIZEOF_LONG - ins->u.bitfield.size); sval = val; - sval >>= (SIZEOF_LONG - ins->u.bitfield.size); + sval >>= (SIZEOF_LONG - ins->u.bitfield.size); mkconst(state, ins, sval); } } @@ -10198,7 +10088,7 @@ static struct triple *branch_target(struct compile_state *state, struct triple * * loop back onto themselves. If I see one don't advance the * target. */ - while(triple_is_structural(state, targ) && + while(triple_is_structural(state, targ) && (targ->next != targ) && (targ->next != state->first)) { targ = targ->next; } @@ -10215,7 +10105,7 @@ static void simplify_branch(struct compile_state *state, struct triple *ins) if (ins->use != 0) { internal_error(state, ins, "branch use"); } - /* 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. That is handled * by rebuilding the basic blocks after simplify all is called. @@ -10232,7 +10122,7 @@ static void simplify_branch(struct compile_state *state, struct triple *ins) struct triple *targ; simplified = 0; targ = branch_target(state, ins); - if ((targ != ins) && (targ->op == OP_BRANCH) && + if ((targ != ins) && (targ->op == OP_BRANCH) && !phi_dependency(targ->u.block)) { unuse_triple(TARG(ins, 0), ins); @@ -10312,7 +10202,7 @@ static void simplify_label(struct compile_state *state, struct triple *ins) unuse_triple(ins, use); use_triple(ins->prev, use); } - + } } if (ins->use) { @@ -10349,7 +10239,7 @@ static void simplify_phi(struct compile_state *state, struct triple *ins) return; } } - + /* See if all of rhs members of a phi are the same */ value = slot[0]; for(i = 1; i < zrhs; i++) { @@ -10477,11 +10367,11 @@ static const struct simplify_table { [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_WRMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, [OP_HLT ] = { simplify_noop, COMPILER_SIMPLIFY_OP }, }; -static inline void debug_simplify(struct compile_state *state, +static inline void debug_simplify(struct compile_state *state, simplify_t do_simplify, struct triple *ins) { #if DEBUG_SIMPLIFY_HIRES @@ -10516,14 +10406,14 @@ static void simplify(struct compile_state *state, struct triple *ins) else { do_simplify = table_simplify[op].func; } - if (do_simplify && + 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", op, tops(op)); @@ -10566,7 +10456,7 @@ static void register_builtin_function(struct compile_state *state, const char *name, int op, struct type *rtype, ...) { struct type *ftype, *atype, *ctype, *crtype, *param, **next; - struct triple *def, *arg, *result, *work, *last, *first, *retvar, *ret; + struct triple *def, *result, *work, *first, *retvar, *ret; struct hash_entry *ident; struct file_state file; int parameters; @@ -10634,7 +10524,7 @@ 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; } work = new_triple(state, op, rtype, -1, parameters); @@ -10646,13 +10536,13 @@ static void register_builtin_function(struct compile_state *state, 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; state->function = 0; state->main_function = 0; @@ -10699,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; @@ -10748,23 +10638,23 @@ static void register_builtins(struct compile_state *state) register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type, &ulong_type, &ulong_type); - register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_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", @@ -10775,17 +10665,19 @@ 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); @@ -10850,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"); @@ -10880,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"); @@ -10915,8 +10808,7 @@ 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); @@ -10983,8 +10875,7 @@ static struct triple *primary_expr(struct compile_state *state) * a varable name * a function name */ - 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); } @@ -10995,14 +10886,22 @@ static struct triple *primary_expr(struct compile_state *state) { struct hash_entry *ident; /* Here ident is an enumeration constant */ - eat(state, TOK_ENUM_CONST); - ident = state->token[0].ident; + 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); @@ -11053,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; } @@ -11062,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; @@ -11175,6 +11072,24 @@ static struct triple *unary_expr(struct compile_state *state) 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: def = postfix_expr(state); break; @@ -11211,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: @@ -11286,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); @@ -11302,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); @@ -11345,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); @@ -11457,7 +11377,7 @@ static struct triple *lor_expr(struct compile_state *state) right = read_expr(state, land_expr(state)); bool(state, right); - def = mklor_expr(state, + def = mklor_expr(state, ltrue_expr(state, left), ltrue_expr(state, right)); } @@ -11482,35 +11402,141 @@ static struct triple *conditional_expr(struct compile_state *state) 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; if (is_const(expr)) { def = expr; - } + } 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) { - simplify(state, ptr); + 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++; } - /* Remove the constant value the tail of the list */ - def = head->prev; - def->prev->next = def->next; - def->next->prev = def->prev; - def->next = def->prev = def; - if (!is_const(def)) { - error(state, 0, "Not a constant expression"); + 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"); + } return def; } @@ -11533,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. @@ -11546,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: @@ -11566,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: @@ -11602,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; } @@ -11615,7 +11641,7 @@ static struct triple *expr(struct compile_state *state) def = assignment_expr(state); while(peek(state) == TOK_COMMA) { eat(state, TOK_COMMA); - def = mkprog(state, def, assignment_expr(state), 0); + def = mkprog(state, def, assignment_expr(state), 0UL); } return def; } @@ -11679,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); @@ -11800,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) { @@ -11809,7 +11837,7 @@ 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 */ @@ -11864,8 +11892,7 @@ static void goto_statement(struct compile_state *state, struct triple *first) { struct hash_entry *ident; eat(state, TOK_GOTO); - eat(state, TOK_IDENT); - ident = state->token[0].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. @@ -11883,9 +11910,8 @@ static void labeled_statement(struct compile_state *state, struct triple *first) { struct triple *ins; struct hash_entry *ident; - eat(state, TOK_IDENT); - ident = state->token[0].ident; + ident = eat(state, TOK_IDENT)->ident; if (ident->sym_label && ident->sym_label->def) { ins = ident->sym_label->def; put_occurance(ins->occurance); @@ -12154,7 +12180,7 @@ static void asm_statement(struct compile_state *state, struct triple *first) for(i = 0; i < out; i++) { struct triple *constraint; constraint = out_param[i].constraint; - info->tmpl.lhs[i] = arch_reg_constraint(state, + info->tmpl.lhs[i] = arch_reg_constraint(state, out_param[i].expr->type, constraint->u.blob); free_triple(state, constraint); } @@ -12184,9 +12210,9 @@ static void asm_statement(struct compile_state *state, struct triple *first) } info->tmpl.lhs[val] = cinfo; info->tmpl.rhs[i] = cinfo; - + } else { - info->tmpl.rhs[i] = arch_reg_constraint(state, + info->tmpl.rhs[i] = arch_reg_constraint(state, in_param[i].expr->type, str); } free_triple(state, constraint); @@ -12206,7 +12232,7 @@ static void asm_statement(struct compile_state *state, struct triple *first) 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; } @@ -12283,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); @@ -12313,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); @@ -12335,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); @@ -12375,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: @@ -12452,14 +12479,14 @@ 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; } @@ -12468,11 +12495,10 @@ static struct type *typedef_name( { 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); } @@ -12491,9 +12517,7 @@ static struct type *enum_specifier( eat(state, TOK_ENUM); tok = peek(state); if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) { - eat(state, tok); - ident = state->token[0].ident; - + ident = eat(state, tok)->ident; } base = 0; if (!ident || (peek(state) == TOK_LBRACE)) { @@ -12506,10 +12530,9 @@ static struct type *enum_specifier( struct hash_entry *eident; struct triple *value; struct type *entry; - eat(state, TOK_IDENT); - eident = state->token[0].ident; + eident = eat(state, TOK_IDENT)->ident; if (eident->sym_ident) { - error(state, 0, "%s already declared", + error(state, 0, "%s already declared", eident->name); } eident->tok = TOK_ENUM_CONST; @@ -12601,8 +12624,7 @@ static struct type *struct_or_union_specifier( } tok = peek(state); if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) { - eat(state, tok); - ident = state->token[0].ident; + ident = eat(state, tok)->ident; } if (!ident || (peek(state) == TOK_LBRACE)) { ulong_t elements; @@ -12643,13 +12665,13 @@ static struct type *struct_or_union_specifier( symbol(state, ident, &ident->sym_tag, 0, struct_type); } } - if (ident && ident->sym_tag && - ident->sym_tag->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 && !struct_type) { - error(state, 0, "%s %s undeclared", + error(state, 0, "%s %s undeclared", (type_main == TYPE_STRUCT)?"struct" : "union", ident->name); } @@ -12717,8 +12739,7 @@ static unsigned int attrib(struct compile_state *state, unsigned int attributes) case TOK_TYPE_NAME: { struct hash_entry *ident; - eat(state, TOK_IDENT); - ident = state->token[0].ident; + ident = eat(state, TOK_IDENT)->ident; if (ident == state->i_noinline) { if (attributes & ATTRIB_ALWAYS_INLINE) { @@ -12732,6 +12753,9 @@ static unsigned int attrib(struct compile_state *state, unsigned int attributes) } attributes |= ATTRIB_ALWAYS_INLINE; } + else if (ident == state->i_noreturn) { + // attribute((noreturn)) does nothing (yet?) + } else { error(state, 0, "Unknown attribute:%s", ident->name); } @@ -12799,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); @@ -12934,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; @@ -12981,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) { @@ -13018,6 +13044,7 @@ static int isdecl_specifier(int tok) return 0; } } +#endif static struct type *decl_specifiers(struct compile_state *state) { @@ -13082,8 +13109,7 @@ static struct field_info designator(struct compile_state *state, struct type *ty error(state, 0, "Struct designator not in struct initializer"); } eat(state, TOK_DOT); - eat(state, TOK_IDENT); - field = state->token[0].ident; + field = eat(state, TOK_IDENT)->ident; info.offset = field_offset(state, type, field); info.type = field_type(state, type, field); break; @@ -13101,7 +13127,9 @@ 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) && @@ -13111,7 +13139,7 @@ static struct triple *initializer( (equiv_types(type->left, result->type->left))) { type->elements = result->type->elements; } - if (is_lvalue(state, result) && + if (is_lvalue(state, result) && ((result->type->type & TYPE_MASK) == TYPE_ARRAY) && (type->type & TYPE_MASK) != TYPE_ARRAY) { @@ -13177,7 +13205,7 @@ static struct triple *initializer( } 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", + 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), @@ -13209,7 +13237,7 @@ static struct triple *initializer( 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.offset = field_offset(state, type, info.type->field_ident); } } while(comma && (peek(state) != TOK_RBRACE)); @@ -13275,7 +13303,7 @@ static void resolve_branches(struct compile_state *state, struct triple *first) static struct triple *function_definition( struct compile_state *state, struct type *type) { - struct triple *def, *tmp, *first, *end, *retvar, *result, *ret; + struct triple *def, *tmp, *first, *end, *retvar, *ret; struct triple *fname; struct type *fname_type; struct hash_entry *ident; @@ -13304,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); @@ -13327,11 +13355,11 @@ static struct triple *function_definition( ctype->elements = 1; /* Add a variable for the return value */ - crtype = new_type(TYPE_TUPLE, + 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; - result = flatten(state, end, variable(state, crtype)); + flatten(state, end, variable(state, crtype)); /* Allocate a variable for the return address */ retvar = flatten(state, end, variable(state, &void_ptr_type)); @@ -13360,7 +13388,7 @@ static struct triple *function_definition( } /* Add the declaration static const char __func__ [] = "func-name" */ - fname_type = new_type(TYPE_ARRAY, + 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; @@ -13405,7 +13433,7 @@ static struct triple *function_definition( return def; } -static struct triple *do_decl(struct compile_state *state, +static struct triple *do_decl(struct compile_state *state, struct type *type, struct hash_entry *ident) { struct triple *def; @@ -13439,7 +13467,11 @@ static struct triple *do_decl(struct compile_state *state, if ((type->type & TYPE_MASK) == TYPE_FUNCTION) { error(state, 0, "Function prototypes not supported"); } - if (ident && + 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"); @@ -13482,8 +13514,8 @@ static void decl(struct compile_state *state, struct triple *first) } eat(state, TOK_EQ); flatten(state, first, - init_expr(state, - ident->sym_ident->def, + init_expr(state, + ident->sym_ident->def, initializer(state, type))); } arrays_complete(state, type); @@ -13519,7 +13551,7 @@ static void decls(struct compile_state *state) } } -/* +/* * Function inlining */ struct triple_reg_set { @@ -13542,19 +13574,21 @@ static void walk_blocks(struct compile_state *state, struct basic_blocks *bb, 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, +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, +static void free_variable_lifetimes(struct compile_state *state, struct basic_blocks *bb, struct reg_block *blocks); -static void print_live_variables(struct compile_state *state, +#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 *retvar, struct triple *ret_addr, struct triple *targ, struct triple *ret) { struct triple *call; @@ -13633,19 +13667,19 @@ static void mark_live(struct compile_state *state, struct triple *func, void *ar static void mark_live_functions(struct compile_state *state) { - /* Ensure state->main_function is the last function in + /* 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, + 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, +static int local_triple(struct compile_state *state, struct triple *func, struct triple *ins) { int local = (ins->id & TRIPLE_FLAG_LOCAL); @@ -13659,7 +13693,7 @@ static int local_triple(struct compile_state *state, return local; } -struct triple *copy_func(struct compile_state *state, struct triple *ofunc, +struct triple *copy_func(struct compile_state *state, struct triple *ofunc, struct occurance *base_occurance) { struct triple *nfunc; @@ -13702,7 +13736,7 @@ struct triple *copy_func(struct compile_state *state, struct triple *ofunc, } 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); @@ -13741,7 +13775,7 @@ struct triple *copy_func(struct compile_state *state, struct triple *ofunc, old = old->next; new = new->next; } while((old != ofirst) && (new != nfirst)); - + /* Make a third pass to cleanup the extra useses */ old = ofirst; new = nfirst; @@ -13790,7 +13824,7 @@ static void expand_inline_call( } result = 0; if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) { - result = read_expr(state, + result = read_expr(state, deref_index(state, fresult(state, nfunc), 1)); } if (state->compiler->debug & DEBUG_INLINE) { @@ -13802,8 +13836,8 @@ static void expand_inline_call( fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__); } - /* - * Get rid of the extra triples + /* + * Get rid of the extra triples */ /* Remove the read of the return address */ ins = RHS(nfunc, 0)->prev->prev; @@ -13819,7 +13853,7 @@ static void expand_inline_call( release_triple(state, ins); /* Remove the retaddres variable */ retvar = fretaddr(state, nfunc); - if ((retvar->lhs != 1) || + if ((retvar->lhs != 1) || (retvar->op != OP_ADECL) || (retvar->next->op != OP_PIECE) || (MISC(retvar->next, 0) != retvar)) { @@ -13862,7 +13896,7 @@ static void expand_inline_call( /* * * Type of the result variable. - * + * * result * | * +----------+------------+ @@ -13872,7 +13906,7 @@ static void expand_inline_call( * +------------------+---------------+ * | | * closure1 ... closuerN - * | | + * | | * +----+--+-+--------+-----+ +----+----+---+-----+ * | | | | | | | | | * var1 var2 var3 ... varN result var1 var2 ... varN result @@ -13890,7 +13924,7 @@ static void expand_inline_call( * var1 var2 ... varN result var1 var2 ... varN result */ -static int add_closure_type(struct compile_state *state, +static int add_closure_type(struct compile_state *state, struct triple *func, struct type *closure_type) { struct type *type, *ctype, **next; @@ -13923,19 +13957,19 @@ static int add_closure_type(struct compile_state *state, fprintf(fp, "new_type: "); name_of(fp, type); fprintf(fp, "\n"); - fprintf(fp, "ctype: %p %d bits: %d ", + 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); @@ -13944,7 +13978,7 @@ static int add_closure_type(struct compile_state *state, release_triple(state, LHS(var, i)); } release_triple(state, var); - + /* Return the index of the added closure type */ return ctype->elements - 1; } @@ -14039,7 +14073,7 @@ static int lookup_closure_index(struct compile_state *state, for(index0 = ins->next->next; (index0->op == OP_INDEX) && (MISC(index0, 0) == result) && - (index0->u.cval == 0) ; + (index0->u.cval == 0) ; index0 = write->next) { index1 = index0->next; @@ -14083,12 +14117,12 @@ static void compute_closure_variables(struct compile_state *state, int i, max_index; #define MAX_INDICIES (sizeof(used_indicies)*CHAR_BIT) #define ID_BITS(X) ((X) & (TRIPLE_FLAG_LOCAL -1)) - struct { + struct { unsigned id; int index; } *info; - + /* Find the basic blocks of this function */ bb.func = me; bb.first = RHS(me, 0); @@ -14137,7 +14171,7 @@ static void compute_closure_variables(struct compile_state *state, ins = ins->next; } while(ins != first); - /* + /* * Build the list of variables to enclose. * * A target it to put the same variable in the @@ -14286,8 +14320,7 @@ static void expand_function_call( struct triple *func, *func_first, *func_last, *retvar; struct triple *first; struct type *ptype, *rtype; - struct triple *jmp; - struct triple *ret_addr, *ret_loc, *ret_set; + struct triple *ret_addr, *ret_loc; struct triple_reg_set *enclose, *set; int closure_idx, pvals, i; @@ -14322,7 +14355,7 @@ static void expand_function_call( if (!*closure_next) { *closure_next = type; } else { - *closure_next = new_type(TYPE_PRODUCT, *closure_next, + *closure_next = new_type(TYPE_PRODUCT, *closure_next, type); closure_next = &(*closure_next)->right; } @@ -14382,22 +14415,22 @@ static void expand_function_call( /* Initialize the return value */ if ((rtype->type & TYPE_MASK) != TYPE_VOID) { - flatten(state, ret_loc, - write_expr(state, + 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); - ret_set = flatten(state, ret_loc, write_expr(state, retvar, ret_addr)); - jmp = flatten(state, ret_loc, + 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, + result = flatten(state, first, + read_expr(state, deref_index(state, fresult(state, func), 1))); propogate_use(state, fcall, result); @@ -14416,7 +14449,7 @@ static void expand_function_call( } /* Generate an expression for the value */ new = flatten(state, first, - read_expr(state, + read_expr(state, closure_expr(state, func, closure_idx, i))); @@ -14429,7 +14462,7 @@ static void expand_function_call( /* * 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); @@ -14437,7 +14470,7 @@ static void expand_function_call( bb.func = 0; } analyze_basic_blocks(state, &bb); - + #if DEBUG_EXPLICIT_CLOSURES fprintf(state->errout, "Updating domindated uses: %p -> %p\n", @@ -14445,7 +14478,7 @@ static void expand_function_call( #endif /* If fcall dominates the use update the expression */ for(use = set->member->use; use; use = next) { - /* Replace use modifies the use chain and + /* Replace use modifies the use chain and * removes use, so I must take a copy of the * next entry early. */ @@ -14638,7 +14671,7 @@ static void insert_function(struct compile_state *state, if (state->compiler->debug & DEBUG_INLINE) { FILE *fp = state->errout; - fprintf(fp, "%s func count: %d\n", + fprintf(fp, "%s func count: %d\n", func->type->type_ident->name, func->u.cval); } if (func->u.cval == 0) { @@ -14663,7 +14696,7 @@ 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 = ""; @@ -14673,7 +14706,7 @@ struct triple *input_asm(struct compile_state *state) 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); @@ -14689,7 +14722,7 @@ struct triple *output_asm(struct compile_state *state) struct asm_info *info; struct triple *def; int in; - + info = xcmalloc(sizeof(*info), "asm_info"); info->str = ""; @@ -14699,13 +14732,13 @@ struct triple *output_asm(struct compile_state *state) def = new_triple(state, OP_ASM, &void_type, 0, in); def->u.ainfo = info; def->id |= TRIPLE_FLAG_VOLATILE; - + return def; } static void join_functions(struct compile_state *state) { - struct triple *jmp, *start, *end, *call, *in, *out, *func; + struct triple *start, *end, *call, *in, *out, *func; struct file_state file; struct type *pnext, *param; struct type *result_type, *args_type; @@ -14724,6 +14757,10 @@ static void join_functions(struct compile_state *state) state->file = &file; state->function = ""; + if (!state->main_function) { + error(state, 0, "No functions to compile\n"); + } + /* The type of arguments */ args_type = state->main_function->type->right; /* The return type without any specifiers */ @@ -14732,11 +14769,11 @@ static void join_functions(struct compile_state *state) /* Verify the external arguments */ if (registers_of(state, args_type) > ARCH_INPUT_REGS) { - error(state, state->main_function, + error(state, state->main_function, "Too many external input arguments"); } if (registers_of(state, result_type) > ARCH_OUTPUT_REGS) { - error(state, state->main_function, + error(state, state->main_function, "Too many external output arguments"); } @@ -14766,8 +14803,8 @@ static void join_functions(struct compile_state *state) param = param->left; } if (registers_of(state, param) != 1) { - error(state, state->main_function, - "Arg: %d %s requires multiple registers", + error(state, state->main_function, + "Arg: %d %s requires multiple registers", idx + 1, param->field_ident->name); } expr = read_expr(state, LHS(in, idx)); @@ -14775,7 +14812,7 @@ static void join_functions(struct compile_state *state) expr = flatten(state, call, expr); use_triple(expr, call); - idx++; + idx++; } @@ -14814,7 +14851,7 @@ static void join_functions(struct compile_state *state) } /* Allocate a dummy containing function */ - func = triple(state, OP_LIST, + 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; @@ -14826,7 +14863,7 @@ static void join_functions(struct compile_state *state) walk_functions(state, insert_function, end); if (start->next != end) { - jmp = flatten(state, start, branch(state, end, 0)); + flatten(state, start, branch(state, end, 0)); } /* OK now the functions have been joined. */ @@ -14843,7 +14880,7 @@ static void join_functions(struct compile_state *state) static int do_use_block( - struct block *used, struct block_set **head, struct block *user, + struct block *used, struct block_set **head, struct block *user, int front) { struct block_set **ptr, *new; @@ -14898,13 +14935,13 @@ static void use_block(struct block *used, struct block *user) /* Append new to the head of the list, print_block * depends on this. */ - count = do_use_block(used, &used->use, user, 1); + count = do_use_block(used, &used->use, user, 1); used->users += count; } static void unuse_block(struct block *used, struct block *unuser) { int count; - count = do_unuse_block(used, &used->use, unuser); + count = do_unuse_block(used, &used->use, unuser); used->users -= count; } @@ -14963,7 +15000,7 @@ static void unipdomf_block(struct block *block, struct block *unipdomf) } static int walk_triples( - struct compile_state *state, + struct compile_state *state, int (*cb)(struct compile_state *state, struct triple *ptr, void *arg), void *arg) { @@ -14996,7 +15033,7 @@ static int do_print_triple(struct compile_state *state, struct triple *ins, void } display_triple(fp, ins); - if (triple_is_branch(state, ins) && ins->use && + if (triple_is_branch(state, ins) && ins->use && (ins->op != OP_RET) && (ins->op != OP_FCALL)) { internal_error(state, ins, "branch used?"); } @@ -15117,16 +15154,18 @@ static void free_basic_block(struct compile_state *state, struct block *block) 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 free_basic_blocks(struct compile_state *state, +static void free_basic_blocks(struct compile_state *state, struct basic_blocks *bb) { struct triple *first, *ins; @@ -15141,10 +15180,10 @@ static void free_basic_blocks(struct compile_state *state, } ins = ins->next; } while(ins != first); - + } -static struct block *basic_block(struct compile_state *state, +static struct block *basic_block(struct compile_state *state, struct basic_blocks *bb, struct triple *first) { struct block *block; @@ -15163,7 +15202,7 @@ static struct block *basic_block(struct compile_state *state, block->vertex = bb->last_vertex; ptr = first; do { - if ((ptr != first) && triple_is_label(state, ptr) && (ptr->use)) { + if ((ptr != first) && triple_is_label(state, ptr) && (ptr->use)) { break; } block->last = ptr; @@ -15178,7 +15217,7 @@ static struct block *basic_block(struct compile_state *state, } while (ptr != bb->first); if ((ptr == bb->first) || ((ptr->next == bb->first) && ( - triple_is_end(state, ptr) || + triple_is_end(state, ptr) || triple_is_ret(state, ptr)))) { /* The block has no outflowing edges */ @@ -15243,7 +15282,7 @@ static struct block *basic_block(struct compile_state *state, struct block_set *edge; FILE *fp = state->errout; fprintf(fp, "basic_block: %10p [%2d] ( %10p - %10p )", - block, block->vertex, + block, block->vertex, block->first, block->last); for(edge = block->edges; edge; edge = edge->next) { fprintf(fp, " %10p [%2d]", @@ -15287,7 +15326,7 @@ static void print_block( FILE *fp = arg; fprintf(fp, "\nblock: %p (%d) ", - block, + block, block->vertex); for(edge = block->edges; edge; edge = edge->next) { @@ -15311,7 +15350,7 @@ static void print_block( } fprintf(fp, "users %d: ", block->users); for(user = block->use; user; user = user->next) { - fprintf(fp, "%p (%d) ", + fprintf(fp, "%p (%d) ", user->member, user->member->vertex); } @@ -15326,8 +15365,6 @@ static void romcc_print_blocks(struct compile_state *state, FILE *fp) } static void print_blocks(struct compile_state *state, const char *func, FILE *fp) { - 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); if (state->compiler->debug & DEBUG_BASIC_BLOCKS) { fprintf(fp, "After %s\n", func); romcc_print_blocks(state, fp); @@ -15339,7 +15376,7 @@ static void print_blocks(struct compile_state *state, const char *func, FILE *fp } } -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; @@ -15371,7 +15408,7 @@ static void prune_nonblock_triples(struct compile_state *state, } 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, bb->first)) { @@ -15386,8 +15423,8 @@ static void setup_basic_blocks(struct compile_state *state, bb->first_block = basic_block(state, bb, bb->first); /* 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 + * 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. */ @@ -15403,7 +15440,7 @@ static void setup_basic_blocks(struct compile_state *state, add_block_edge(bb->first_block, tail, 0); use_block(tail, bb->first_block); } - + /* Find the last basic block. */ bb->last_block = block_of_triple(state, bb->first->prev); @@ -15504,7 +15541,7 @@ static int initialize_spdblock( return vertex; } -static int setup_spdblocks(struct compile_state *state, +static int setup_spdblocks(struct compile_state *state, struct basic_blocks *bb, struct sdom_block *sd) { struct block *block; @@ -15514,7 +15551,7 @@ static int setup_spdblocks(struct compile_state *state, /* Walk through the graph and find unconnected blocks. Add a * fake edge from the unconnected blocks to the end of the - * graph. + * graph. */ block = bb->first_block->last->next->u.block; for(; block && block != bb->first_block; block = block->last->next->u.block) { @@ -15558,16 +15595,16 @@ static void compress_ancestors(struct sdom_block *v) } } -static void compute_sdom(struct compile_state *state, +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); @@ -15602,22 +15639,22 @@ static void compute_sdom(struct compile_state *state, 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, +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); @@ -15651,13 +15688,13 @@ static void compute_spdom(struct compile_state *state, 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, +static void compute_idom(struct compile_state *state, struct basic_blocks *bb, struct sdom_block *sd) { int i; @@ -15672,7 +15709,7 @@ static void compute_idom(struct compile_state *state, sd[1].block->idom = 0; } -static void compute_ipdom(struct compile_state *state, +static void compute_ipdom(struct compile_state *state, struct basic_blocks *bb, struct sdom_block *sd) { int i; @@ -15689,13 +15726,13 @@ static void compute_ipdom(struct compile_state *state, /* 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. @@ -15708,7 +15745,7 @@ static void compute_ipdom(struct compile_state *state, * 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). */ @@ -15726,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), @@ -15738,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 @@ -15878,7 +15915,7 @@ static void print_dominated2( for(i = 0; i < depth; i++) { fprintf(fp, " "); } - fprintf(fp, "%3d: %p (%p - %p) @", + fprintf(fp, "%3d: %p (%p - %p) @", block->vertex, block, block->first, block->last); ins = block->first; while(ins != block->last && (ins->occurance->line == 0)) { @@ -15929,7 +15966,7 @@ static int print_frontiers( fprintf(fp, " %d", user->member->vertex); } fprintf(fp, "\n"); - + for(edge = block->edges; edge; edge = edge->next) { vertex = print_frontiers(state, fp, edge->member, vertex); } @@ -15940,7 +15977,7 @@ static void print_dominance_frontiers(struct compile_state *state, { fprintf(fp, "\ndominance frontiers\n"); print_frontiers(state, fp, bb->first_block, 0); - + } static void analyze_idominators(struct compile_state *state, struct basic_blocks *bb) @@ -16006,7 +16043,7 @@ static void print_ipdominance_frontiers(struct compile_state *state, { fprintf(fp, "\nipdominance frontiers\n"); print_pfrontiers(state, fp, bb->last_block, 0); - + } static void analyze_ipdominators(struct compile_state *state, @@ -16042,7 +16079,7 @@ 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) { @@ -16088,7 +16125,7 @@ static void insert_phi_operations(struct compile_state *state) if (!triple_is_auto_var(state, var) || !var->use) { continue; } - + iter += 1; work_list = 0; work_list_tail = &work_list; @@ -16101,7 +16138,7 @@ static void insert_phi_operations(struct compile_state *state) 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; @@ -16134,7 +16171,7 @@ static void insert_phi_operations(struct compile_state *state) /* Insert a phi function for this variable */ get_occurance(var->occurance); phi = alloc_triple( - state, OP_PHI, var->type, -1, in_edges, + state, OP_PHI, var->type, -1, in_edges, var->occurance); phi->u.block = front; MISC(phi, 0) = var; @@ -16537,7 +16574,7 @@ 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; @@ -16548,7 +16585,7 @@ static void prune_unused_phis(struct compile_state *state) phis += 1; } } - + /* Mark them all dead */ live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple"); phis = 0; @@ -16562,7 +16599,7 @@ static void prune_unused_phis(struct compile_state *state) phi->id = phis; phis += 1; } - + /* Mark phis alive that are used by non phis */ for(i = 0; i < phis; i++) { struct triple_set *set; @@ -16595,7 +16632,7 @@ static void prune_unused_phis(struct compile_state *state) slot[j] = unknown; use_triple(unknown, phi); transform_to_arch_instruction(state, unknown); -#if 0 +#if 0 warning(state, phi, "variable not set at index %d on all paths to use", j); #endif } @@ -16729,7 +16766,7 @@ static void transform_from_ssa_form(struct compile_state *state) } 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); @@ -16782,7 +16819,7 @@ static void transform_from_ssa_form(struct compile_state *state) 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. @@ -16829,7 +16866,7 @@ static void transform_from_ssa_form(struct compile_state *state) /* 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; @@ -16846,7 +16883,7 @@ static void transform_from_ssa_form(struct compile_state *state) 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); @@ -16865,7 +16902,7 @@ static void transform_from_ssa_form(struct compile_state *state) 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); @@ -16877,7 +16914,7 @@ static void transform_from_ssa_form(struct compile_state *state) #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) { @@ -16892,7 +16929,7 @@ HI(); HI(); rename_variables(state); HI(); - + prune_block_variables(state, state->bb.first_block); HI(); prune_unused_phis(state); @@ -16900,7 +16937,7 @@ HI(); } #undef HI -/* +/* * Register conflict resolution * ========================================================= */ @@ -16930,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"); @@ -17183,7 +17220,7 @@ static struct triple *typed_pre_copy( 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) @@ -17240,7 +17277,7 @@ static void insert_copies_to_phi(struct compile_state *state) 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); @@ -17297,7 +17334,7 @@ struct triple_reg_set; struct reg_block; -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; @@ -17339,19 +17376,24 @@ 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) @@ -17410,7 +17452,7 @@ static struct triple *part_to_piece(struct compile_state *state, struct triple * return ins; } -static int this_def(struct compile_state *state, +static int this_def(struct compile_state *state, struct triple *ins, struct triple *other) { if (ins == other) { @@ -17517,7 +17559,9 @@ 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; @@ -17586,7 +17630,7 @@ static struct reg_block *compute_variable_lifetimes( return blocks; } -static void free_variable_lifetimes(struct compile_state *state, +static void free_variable_lifetimes(struct compile_state *state, struct basic_blocks *bb, struct reg_block *blocks) { int i; @@ -17609,16 +17653,16 @@ static void free_variable_lifetimes(struct compile_state *state, } typedef void (*wvl_cb_t)( - struct compile_state *state, - struct reg_block *blocks, struct triple_reg_set *live, + 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 basic_blocks *bb, struct reg_block *blocks, + struct basic_blocks *bb, struct reg_block *blocks, wvl_cb_t cb, void *arg) { int i; - + for(i = 1; i <= state->bb.last_vertex; i++) { struct triple_reg_set *live; struct triple_reg_set *entry, *next; @@ -17653,7 +17697,7 @@ static void walk_variable_lifetimes(struct compile_state *state, * going on. */ cb(state, blocks, live, rb, ptr, arg); - + /* Remove the current definition from live */ do_triple_unset(&live, ptr); @@ -17688,6 +17732,7 @@ 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) @@ -17705,7 +17750,7 @@ static void print_live_variables_block( block, block->vertex); for(edge = block->edges; edge; edge = edge->next) { fprintf(fp, " %p<-%p", - edge->member, + edge->member, edge->member && edge->member->use?edge->member->use->member : 0); } fprintf(fp, "\n"); @@ -17759,7 +17804,7 @@ static void print_live_variables_block( fprintf(fp, "\n"); } -static void print_live_variables(struct compile_state *state, +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; @@ -17769,7 +17814,7 @@ static void print_live_variables(struct compile_state *state, walk_blocks(state, bb, print_live_variables_block, &info); } - +#endif static int count_triples(struct compile_state *state) { @@ -17795,7 +17840,7 @@ struct dead_triple { #define TRIPLE_FLAG_FREE 1 }; -static void print_dead_triples(struct compile_state *state, +static void print_dead_triples(struct compile_state *state, struct dead_triple *dtriple) { struct triple *first, *ins; @@ -17813,7 +17858,7 @@ static void print_dead_triples(struct compile_state *state, if ((ins->op == OP_LABEL) && (ins->use)) { fprintf(fp, "\n%p:\n", ins); } - fprintf(fp, "%c", + fprintf(fp, "%c", (dt->flags & TRIPLE_FLAG_ALIVE)?' ': '-'); display_triple(fp, ins); if (triple_is_branch(state, ins)) { @@ -17859,10 +17904,9 @@ 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, *final, *ins; + struct triple *first, *ins; if (!(state->compiler->flags & COMPILER_ELIMINATE_INEFECTUAL_CODE)) { return; @@ -17873,17 +17917,15 @@ static void eliminate_inefectual_code(struct compile_state *state) work_list_tail = &work_list; first = state->first; - final = state->first->prev; /* 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 { dtriple[i].triple = ins; dtriple[i].block = block_of_triple(state, ins); @@ -17940,7 +17982,10 @@ static void eliminate_inefectual_code(struct compile_state *state) struct triple *last; last = user->member->last; while((last->op == OP_NOOP) && (last != user->member->first)) { - internal_warning(state, last, "awakening noop?"); +#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); @@ -17948,7 +17993,7 @@ static void eliminate_inefectual_code(struct compile_state *state) } 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?"); } @@ -18007,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)) { @@ -18030,14 +18075,14 @@ static void insert_mandatory_copies(struct compile_state *state) * They do not take up any registers until a * copy places them in one. */ - if ((info.reg == REG_UNNEEDED) && + 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)); @@ -18053,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) { @@ -18102,7 +18147,7 @@ static void insert_mandatory_copies(struct compile_state *state) } } regcm &= rinfo.regcm; - + } if (do_post_copy) { struct reg_info pre, post; @@ -18196,7 +18241,7 @@ static void print_interference_block( block, block->vertex); for(edge = block->edges; edge; edge = edge->next) { fprintf(fp, " %p<-%p", - edge->member, + edge->member, edge->member && edge->member->use?edge->member->use->member : 0); } fprintf(fp, "\n"); @@ -18238,11 +18283,9 @@ static void print_interference_block( for(done = 0, ptr = block->first; !done; ptr = ptr->next) { struct live_range *lr; unsigned id; - int op; - op = ptr->op; done = (ptr == block->last); lr = rstate->lrd[ptr->id].lr; - + id = ptr->id; ptr->id = rstate->lrd[id].orig_id; SET_REG(ptr->id, lr->color); @@ -18407,7 +18450,7 @@ static struct lre_hash **lre_probe(struct reg_state *rstate, right = tmp; } index = hash_live_edge(left, right); - + ptr = &rstate->hash[index]; while(*ptr) { if (((*ptr)->left == left) && ((*ptr)->right == right)) { @@ -18426,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... */ @@ -18465,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; @@ -18517,7 +18560,7 @@ static void remove_live_edges(struct reg_state *rstate, struct live_range *range } } -static void transfer_live_edges(struct reg_state *rstate, +static void transfer_live_edges(struct reg_state *rstate, struct live_range *dest, struct live_range *src) { struct live_range_edge *edge, *next; @@ -18532,7 +18575,7 @@ static void transfer_live_edges(struct reg_state *rstate, /* 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 @@ -18545,11 +18588,11 @@ static void transfer_live_edges(struct reg_state *rstate, * 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 @@ -18563,8 +18606,9 @@ static void transfer_live_edges(struct reg_state *rstate, * */ +#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; @@ -18582,7 +18626,7 @@ static void different_colored( } } } - +#endif static struct live_range *coalesce_ranges( struct compile_state *state, struct reg_state *rstate, @@ -18600,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; @@ -18637,7 +18681,7 @@ static struct live_range *coalesce_ranges( /* 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) && @@ -18668,10 +18712,12 @@ static struct live_range *coalesce_ranges( lr2->defs->def, lr2->color); #endif - + /* Append lr2 onto lr1 */ +#if DEBUG_ROMCC_WARNINGS #warning "FIXME should this be a merge instead of a splice?" - /* This FIXME item applies to the correctness of live_range_end +#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 @@ -18681,7 +18727,7 @@ static struct live_range *coalesce_ranges( mid1 = lr1->defs->prev; mid2 = lr2->defs; end = lr2->defs->prev; - + head->prev = end; end->next = head; @@ -18786,7 +18832,7 @@ static void initialize_live_ranges( 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]; @@ -18813,7 +18859,7 @@ 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 = ins->lhs; if ((zlhs == 0) && triple_is_def(state, ins)) { @@ -18859,7 +18905,7 @@ static void initialize_live_ranges( } if (rinfo.reg == linfo.reg) { - coalesce_ranges(state, rstate, + coalesce_ranges(state, rstate, lhs->lr, rhs->lr); } } @@ -18869,8 +18915,8 @@ static void initialize_live_ranges( } static void graph_ins( - struct compile_state *state, - struct reg_block *blocks, struct triple_reg_set *live, + 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; @@ -18885,7 +18931,7 @@ static void graph_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. @@ -18907,6 +18953,7 @@ static void graph_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) { @@ -18932,8 +18979,8 @@ static struct live_range *get_verify_live_range( } static void verify_graph_ins( - struct compile_state *state, - struct reg_block *blocks, struct triple_reg_set *live, + 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; @@ -18954,17 +19001,17 @@ static void verify_graph_ins( } lr2 = get_verify_live_range(state, rstate, entry2->member); if (lr1 == lr2) { - internal_error(state, entry2->member, + 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, + internal_error(state, entry2->member, "edges don't interfere?"); } - + lr1_found = 0; lr2_degree = 0; for(edge2 = lr2->edges; edge2; edge2 = edge2->next) { @@ -18985,11 +19032,11 @@ static void verify_graph_ins( } return; } - +#endif static void print_interference_ins( - struct compile_state *state, - struct reg_block *blocks, struct triple_reg_set *live, + 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; @@ -19059,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 @@ -19077,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. */ @@ -19127,7 +19174,7 @@ static int coalesce_live_ranges( if ((lr1->classes & lr2->classes) == 0) { continue; } - + if (interfere(rstate, lr1, lr2)) { continue; } @@ -19202,7 +19249,7 @@ static int correct_coalesce_conflicts( { int conflicts; conflicts = 0; - walk_variable_lifetimes(state, &state->bb, blocks, + walk_variable_lifetimes(state, &state->bb, blocks, fix_coalesce_conflicts, &conflicts); return conflicts; } @@ -19218,11 +19265,13 @@ static void replace_set_use(struct compile_state *state, } } -static void replace_block_use(struct compile_state *state, +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]; @@ -19275,7 +19324,9 @@ static struct triple *resolve_tangle( struct triple_set *set, *next; struct triple *copy; +#if DEBUG_ROMCC_WARNINGS #warning "WISHLIST recalculate all affected instructions colors" +#endif info = find_lhs_color(state, tangle, 0); for(set = tangle->use; set; set = next) { struct triple *user; @@ -19306,7 +19357,7 @@ static struct triple *resolve_tangle( } info = find_lhs_color(state, tangle, 0); SET_INFO(tangle->id, info); - + return copy; } @@ -19332,7 +19383,7 @@ static void fix_tangles(struct compile_state *state, } reg_inc_used(state, used, info.reg); } - + /* Now find the least dominated definition of a register in * conflict I have seen so far. */ @@ -19345,7 +19396,7 @@ static void fix_tangles(struct compile_state *state, /* Changing copies that feed into phi functions * is incorrect. */ - if (set->member->use && + if (set->member->use && (set->member->use->member->op == OP_PHI)) { continue; } @@ -19375,7 +19426,7 @@ static int correct_tangles( int tangles; tangles = 0; color_instructions(state); - walk_variable_lifetimes(state, &state->bb, blocks, + walk_variable_lifetimes(state, &state->bb, blocks, fix_tangles, &tangles); return tangles; } @@ -19406,7 +19457,7 @@ struct triple *find_constrained_def( if (regcm == info.regcm) { continue; } - + /* If there is just one use. * That use cannot accept a larger register class. * There are no intervening definitions except @@ -19414,8 +19465,10 @@ struct triple *find_constrained_def( * Then a triple is not constrained. * FIXME handle this case! */ +#if DEBUG_ROMCC_WARNINGS #warning "FIXME ignore cases that cannot be fixed (a definition followed by a use)" - +#endif + /* Of the constrained live ranges deal with the * least dominated one first. @@ -19424,7 +19477,7 @@ struct triple *find_constrained_def( fprintf(state->errout, "canidate: %p %-8s regcm: %x %x\n", lrd->def, tops(lrd->def->op), regcm, info.regcm); } - if (!constrained || + if (!constrained || tdominates(state, lrd->def, constrained)) { constrained = lrd->def; @@ -19434,13 +19487,13 @@ struct triple *find_constrained_def( } static int split_constrained_ranges( - struct compile_state *state, struct reg_state *rstate, + 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; @@ -19450,7 +19503,9 @@ static int split_constrained_ranges( 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); } @@ -19466,14 +19521,14 @@ static int split_constrained_ranges( } 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", + fprintf(state->errout, "split_ranges %d %s %p\n", rstate->passes, tops(range->defs->def->op), range->defs->def); } if ((range->color == REG_UNNEEDED) || @@ -19483,7 +19538,7 @@ static int split_ranges( 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 + * 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. * @@ -19494,8 +19549,10 @@ static int split_ranges( * 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); @@ -19547,7 +19604,7 @@ static void cgdebug_loc(struct compile_state *state, struct triple *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; @@ -19577,7 +19634,7 @@ static int select_free_color(struct compile_state *state, for(edge = range->edges; edge; edge = edge->next) { i++; } - cgdebug_printf(state, "\n%s edges: %d", + cgdebug_printf(state, "\n%s edges: %d", tops(range->defs->def->op), i); cgdebug_loc(state, range->defs->def); cgdebug_printf(state, "\n"); @@ -19587,7 +19644,7 @@ static int select_free_color(struct compile_state *state, arch_reg_str(i)); } } - } + } /* If a color is already assigned see if it will work */ if (range->color != REG_UNSET) { @@ -19715,7 +19772,7 @@ 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) { @@ -19741,7 +19798,7 @@ static int select_free_color(struct compile_state *state, 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++) { @@ -19854,7 +19911,7 @@ static void verify_colors(struct compile_state *state, struct reg_state *rstate) 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; @@ -19889,7 +19946,7 @@ static void color_triples(struct compile_state *state, struct reg_state *rstate) ins = first; do { if ((ins->id < 0) || (ins->id > rstate->defs)) { - internal_error(state, ins, + internal_error(state, ins, "triple without a live range"); } lrd = &rstate->lrd[ins->id]; @@ -19910,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 */ @@ -19939,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; } } @@ -19952,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; @@ -20016,7 +20073,6 @@ static void allocate_registers(struct compile_state *state) do { struct live_range **point, **next; - int conflicts; int tangles; int coalesced; @@ -20036,7 +20092,7 @@ static void allocate_registers(struct compile_state *state) rstate.blocks = compute_variable_lifetimes(state, &state->bb); /* Fix invalid mandatory live range coalesce conflicts */ - conflicts = correct_coalesce_conflicts(state, rstate.blocks); + correct_coalesce_conflicts(state, rstate.blocks); /* Fix two simultaneous uses of the same register. * In a few pathlogical cases a partial untangle moves @@ -20048,14 +20104,14 @@ static void allocate_registers(struct compile_state *state) tangles = correct_tangles(state, rstate.blocks); } while(tangles); - + 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 + /* 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. @@ -20070,18 +20126,18 @@ static void allocate_registers(struct compile_state *state) /* Compute the interference graph */ walk_variable_lifetimes( - state, &state->bb, rstate.blocks, + state, &state->bb, rstate.blocks, graph_ins, &rstate); - + /* Display the interference graph if desired */ 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, &state->bb, rstate.blocks, + state, &state->bb, rstate.blocks, print_interference_ins, &rstate); } - + coalesced = coalesce_live_ranges(state, &rstate); if (state->compiler->debug & DEBUG_COALESCING) { @@ -20095,13 +20151,13 @@ static void allocate_registers(struct compile_state *state) # endif /* Verify the interference graph */ walk_variable_lifetimes( - state, &state->bb, rstate.blocks, + 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. */ @@ -20121,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(state, "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; @@ -20144,7 +20200,7 @@ static void allocate_registers(struct compile_state *state) next = point; } else { - cgdebug_printf(state, "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)": ""); } @@ -20237,10 +20293,7 @@ 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, +static void scc_add_fedge(struct compile_state *state, struct scc_state *scc, struct flow_edge *fedge) { if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) { @@ -20295,8 +20348,8 @@ 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: %5d (%4d -> %5d)\n", - sedge - scc->ssa_edges, + fprintf(state->errout, "adding sedge: %5ld (%4d -> %5d)\n", + (long)(sedge - scc->ssa_edges), sedge->src->def->id, sedge->dst->def->id); } @@ -20305,8 +20358,8 @@ static void scc_add_sedge(struct compile_state *state, struct scc_state *scc, (sedge->work_prev != sedge)) { if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) { - fprintf(state->errout, "dupped sedge: %5d\n", - sedge - scc->ssa_edges); + fprintf(state->errout, "dupped sedge: %5ld\n", + (long)(sedge - scc->ssa_edges)); } return; } @@ -20342,7 +20395,6 @@ static struct ssa_edge *scc_next_sedge( return sedge; } - static void initialize_scc_state( struct compile_state *state, struct scc_state *scc) { @@ -20371,12 +20423,12 @@ static void initialize_scc_state( 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->bb.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 */ @@ -20515,7 +20567,7 @@ 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; @@ -20527,7 +20579,7 @@ static void initialize_scc_state( } } - + static void free_scc_state( struct compile_state *state, struct scc_state *scc) { @@ -20543,7 +20595,7 @@ static void free_scc_state( xfree(scc->flow_blocks); xfree(scc->ssa_edges); xfree(scc->lattice); - + } static struct lattice_node *triple_to_lattice( @@ -20572,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; @@ -20607,7 +20659,7 @@ static void scc_debug_lnode( struct triple *val, **expr; val = lnode->val? lnode->val : lnode->def; fprintf(fp, "%p %s %3d %10s (", - lnode->def, + lnode->def, ((lnode->def->op == OP_PHI)? "phi: ": "expr:"), lnode->def->id, tops(lnode->def->op)); @@ -20635,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); @@ -20663,7 +20715,9 @@ static int compute_lnode_val(struct compile_state *state, struct scc_state *scc, 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. */ @@ -20690,9 +20744,9 @@ static int compute_lnode_val(struct compile_state *state, struct scc_state *scc, lnode->val = 0; /* Lattice low by definition */ } /* Find the case when I am lattice high */ - if (lnode->val && + 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; @@ -20715,7 +20769,7 @@ static int compute_lnode_val(struct compile_state *state, struct scc_state *scc, } } /* 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, lnode->old_id)) { lnode->val = 0; @@ -20723,11 +20777,13 @@ static int compute_lnode_val(struct compile_state *state, struct scc_state *scc, /* 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) && + 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?"); } @@ -20738,7 +20794,7 @@ static int compute_lnode_val(struct compile_state *state, struct scc_state *scc, if (lnode->val != scratch) { xfree(scratch); } - + return changed; } @@ -20765,7 +20821,7 @@ static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc fprintf(fp, "%s: %d (", tops(lnode->def->op), lnode->def->id); - + for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) { fprintf(fp, " %d", fedge->dst->block->vertex); } @@ -20813,7 +20869,7 @@ static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc } -static void scc_add_sedge_dst(struct compile_state *state, +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)) { @@ -20824,7 +20880,7 @@ static void scc_add_sedge_dst(struct compile_state *state, } } -static void scc_visit_phi(struct compile_state *state, struct scc_state *scc, +static void scc_visit_phi(struct compile_state *state, struct scc_state *scc, struct lattice_node *lnode) { struct lattice_node *tmp; @@ -20844,7 +20900,7 @@ static void scc_visit_phi(struct compile_state *state, struct scc_state *scc, 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", + fprintf(state->errout, "Examining edge: %d vertex: %d executable: %d\n", index, fedge->dst->block->vertex, fedge->executable @@ -20932,14 +20988,14 @@ static void scc_writeback_values( struct flow_edge *fedge; int executable; executable = 0; - for(fedge = lnode->fblock->in; + 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, + ins->id, tops(lnode->def->op), tops(lnode->val->op)); } @@ -20955,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: @@ -21014,9 +21070,9 @@ static void scc_transform(struct compile_state *state) reps++; } } - + if (state->compiler->debug & DEBUG_SCC_TRANSFORM) { - fprintf(state->errout, "vertex: %d reps: %d\n", + fprintf(state->errout, "vertex: %d reps: %d\n", block->vertex, reps); } @@ -21048,8 +21104,8 @@ static void scc_transform(struct compile_state *state) fblock = lnode->fblock; if (state->compiler->debug & DEBUG_SCC_TRANSFORM) { - fprintf(state->errout, "sedge: %5d (%5d -> %5d)\n", - sedge - scc.ssa_edges, + fprintf(state->errout, "sedge: %5ld (%5d -> %5d)\n", + (unsigned long)sedge - (unsigned long)scc.ssa_edges, sedge->src->def->id, sedge->dst->def->id); } @@ -21069,11 +21125,11 @@ 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); } @@ -21086,7 +21142,7 @@ static void transform_to_arch_instructions(struct compile_state *state) do { ins = transform_to_arch_instruction(state, ins); } while(ins != first); - + print_blocks(state, __func__, state->dbgout); } @@ -21157,7 +21213,7 @@ static void verify_uses(struct compile_state *state) } ins = ins->next; } while(ins != first); - + } static void verify_blocks_present(struct compile_state *state) { @@ -21171,14 +21227,14 @@ static void verify_blocks_present(struct compile_state *state) valid_ins(state, ins); if (triple_stores_block(state, ins)) { if (!ins->u.block) { - internal_error(state, ins, + 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) @@ -21263,12 +21319,12 @@ static void verify_blocks(struct compile_state *state) } } if (block->users != users) { - internal_error(state, block->first, + 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; @@ -21290,7 +21346,7 @@ static void verify_domination(struct compile_state *state) if (!state->bb.first_block) { return; } - + first = state->first; ins = first; do { @@ -21317,7 +21373,7 @@ static void verify_domination(struct compile_state *state) bset = bset->next; } if (!bset) { - internal_error(state, set->member, + internal_error(state, set->member, "no edge for phi rhs %d", i); } use_point = bset->member->last; @@ -21326,11 +21382,11 @@ static void verify_domination(struct compile_state *state) if (use_point && !tdominates(state, ins, use_point)) { if (is_const(ins)) { - internal_warning(state, ins, + internal_warning(state, ins, "non dominated rhs use point %p?", use_point); } else { - internal_error(state, ins, + internal_error(state, ins, "non dominated rhs use point %p?", use_point); } } @@ -21395,7 +21451,7 @@ static void verify_piece(struct compile_state *state) static void verify_ins_colors(struct compile_state *state) { struct triple *first, *ins; - + first = state->first; ins = first; do { @@ -21512,7 +21568,7 @@ static void verify_consistency(struct compile_state *state) fprintf(state->dbgout, "consistency verified\n"); } } -#else +#else static void verify_consistency(struct compile_state *state) {} #endif /* DEBUG_CONSISTENCY */ @@ -21529,7 +21585,7 @@ static void optimize(struct compile_state *state) print_triples(state); verify_consistency(state); - /* Analize the intermediate code */ + /* Analyze the intermediate code */ state->bb.first = state->first; analyze_basic_blocks(state, &state->bb); @@ -21555,8 +21611,10 @@ static void optimize(struct compile_state *state) /* Propogate constants throughout the code */ 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. */ @@ -21623,7 +21681,7 @@ 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 -1; } @@ -21741,7 +21799,11 @@ static void print_op_asm(struct compile_state *state, #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 @@ -21879,14 +21941,14 @@ static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2) { /* See if two register classes may have overlapping registers */ unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 | - REGCM_GPR32_8 | REGCM_GPR32 | + 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) || @@ -21919,7 +21981,7 @@ static void arch_reg_equivs( *equiv++ = REG_DXAX; *equiv++ = REG_EDXEAX; break; - case REG_BL: + case REG_BL: #if X86_4_8BIT_GPRS *equiv++ = REG_BH; #endif @@ -21979,19 +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: @@ -22027,19 +22089,19 @@ static void arch_reg_equivs( *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_DXAX: + case REG_DXAX: *equiv++ = REG_AL; *equiv++ = REG_AH; *equiv++ = REG_DL; @@ -22050,7 +22112,7 @@ static void arch_reg_equivs( *equiv++ = REG_EDX; *equiv++ = REG_EDXEAX; break; - case REG_EDXEAX: + case REG_EDXEAX: *equiv++ = REG_AL; *equiv++ = REG_AH; *equiv++ = REG_DL; @@ -22062,15 +22124,15 @@ static void arch_reg_equivs( *equiv++ = REG_DXAX; break; } - *equiv++ = REG_UNSET; + *equiv++ = REG_UNSET; } static unsigned arch_avail_mask(struct compile_state *state) { unsigned avail_mask; /* REGCM_GPR8 is not available */ - avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 | - REGCM_GPR32 | REGCM_GPR32_8 | + 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; if (state->arch->features & X86_MMX_REGS) { @@ -22113,7 +22175,7 @@ static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned r /* 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) @@ -22257,7 +22319,7 @@ static struct reg_info arch_reg_clobber( 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; @@ -22282,7 +22344,7 @@ static int arch_select_free_register( * 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; @@ -22317,20 +22379,23 @@ static int arch_select_free_register( } -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" +#endif unsigned mask; mask = 0; 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_GPR8_LO | - REGCM_GPR16 | REGCM_GPR16_8 | + REGCM_GPR16 | REGCM_GPR16_8 | REGCM_GPR32 | REGCM_GPR32_8 | REGCM_DIVIDEND32 | REGCM_DIVIDEND64 | REGCM_MMX | REGCM_XMM | @@ -22379,9 +22444,12 @@ static unsigned arch_type_to_regcm(struct compile_state *state, struct type *typ 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) { @@ -22497,13 +22565,13 @@ static int get_imm8(struct triple *ins, struct triple **expr) #endif #define COPY8_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO | REGCM_MMX | REGCM_XMM) -#define COPY16_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM) +#define 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] = { - .lhs = { + .lhs = { [ 0] = { REG_UNNEEDED, REGCM_IMMALL }, [ 1] = { REG_UNNEEDED, REGCM_IMMALL }, [ 2] = { REG_UNNEEDED, REGCM_IMMALL }, @@ -22570,10 +22638,10 @@ static struct ins_template templates[] = { [63] = { REG_UNNEEDED, REGCM_IMMALL }, }, }, - [TEMPLATE_INTCONST8] = { + [TEMPLATE_INTCONST8] = { .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } }, }, - [TEMPLATE_INTCONST32] = { + [TEMPLATE_INTCONST32] = { .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } }, }, [TEMPLATE_UNKNOWNVAL] = { @@ -22603,32 +22671,32 @@ static struct ins_template templates[] = { .lhs = { [0] = { REG_UNSET, COPY32_REGCM } }, .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } }, }, - [TEMPLATE_PHI8] = { + [TEMPLATE_PHI8] = { .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } }, .rhs = { [0] = { REG_VIRT0, COPY8_REGCM } }, }, - [TEMPLATE_PHI16] = { + [TEMPLATE_PHI16] = { .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } }, - .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } }, + .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } }, }, - [TEMPLATE_PHI32] = { + [TEMPLATE_PHI32] = { .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } }, - .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } }, + .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } }, }, [TEMPLATE_STORE8] = { - .rhs = { + .rhs = { [0] = { REG_UNSET, REGCM_GPR32 }, [1] = { REG_UNSET, REGCM_GPR8_LO }, }, }, [TEMPLATE_STORE16] = { - .rhs = { + .rhs = { [0] = { REG_UNSET, REGCM_GPR32 }, [1] = { REG_UNSET, REGCM_GPR16 }, }, }, [TEMPLATE_STORE32] = { - .rhs = { + .rhs = { [0] = { REG_UNSET, REGCM_GPR32 }, [1] = { REG_UNSET, REGCM_GPR32 }, }, @@ -22647,84 +22715,84 @@ static struct ins_template templates[] = { }, [TEMPLATE_BINARY8_REG] = { .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } }, - .rhs = { + .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO }, [1] = { REG_UNSET, REGCM_GPR8_LO }, }, }, [TEMPLATE_BINARY16_REG] = { .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } }, - .rhs = { + .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_BINARY8_IMM] = { .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } }, - .rhs = { + .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO }, [1] = { REG_UNNEEDED, REGCM_IMM8 }, }, }, [TEMPLATE_BINARY16_IMM] = { .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } }, - .rhs = { + .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_SL8_CL] = { .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } }, - .rhs = { + .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO }, [1] = { REG_CL, REGCM_GPR8_LO }, }, }, [TEMPLATE_SL16_CL] = { .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } }, - .rhs = { + .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 }, [1] = { REG_CL, REGCM_GPR8_LO }, }, }, [TEMPLATE_SL32_CL] = { .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } }, - .rhs = { + .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 }, [1] = { REG_CL, REGCM_GPR8_LO }, }, }, [TEMPLATE_SL8_IMM] = { .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } }, - .rhs = { + .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO }, [1] = { REG_UNNEEDED, REGCM_IMM8 }, }, }, [TEMPLATE_SL16_IMM] = { .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } }, - .rhs = { + .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 }, [1] = { REG_UNNEEDED, REGCM_IMM8 }, }, }, [TEMPLATE_SL32_IMM] = { .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } }, - .rhs = { + .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 }, [1] = { REG_UNNEEDED, REGCM_IMM8 }, }, @@ -22806,19 +22874,19 @@ static struct ins_template templates[] = { .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } }, }, [TEMPLATE_INB_DX] = { - .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } }, + .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } }, .rhs = { [0] = { REG_DX, REGCM_GPR16 } }, }, [TEMPLATE_INB_IMM] = { - .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } }, + .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] = { @@ -22829,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_LO }, [1] = { REG_DX, REGCM_GPR16 }, }, }, - [TEMPLATE_OUTB_IMM] = { + [TEMPLATE_OUTB_IMM] = { .rhs = { - [0] = { REG_AL, REGCM_GPR8_LO }, + [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 }, @@ -22849,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 }, }, }, @@ -22870,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 }, }, @@ -22885,27 +22953,27 @@ static struct ins_template templates[] = { }, [TEMPLATE_UMUL8] = { .lhs = { [0] = { REG_AX, REGCM_GPR16 } }, - .rhs = { + .rhs = { [0] = { REG_AL, REGCM_GPR8_LO }, [1] = { REG_UNSET, REGCM_GPR8_LO }, }, }, [TEMPLATE_UMUL16] = { .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } }, - .rhs = { + .rhs = { [0] = { REG_AX, REGCM_GPR16 }, [1] = { REG_UNSET, REGCM_GPR16 }, }, }, [TEMPLATE_UMUL32] = { .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } }, - .rhs = { + .rhs = { [0] = { REG_EAX, REGCM_GPR32 }, [1] = { REG_UNSET, REGCM_GPR32 }, }, }, [TEMPLATE_DIV8] = { - .lhs = { + .lhs = { [0] = { REG_AL, REGCM_GPR8_LO }, [1] = { REG_AH, REGCM_GPR8 }, }, @@ -22915,7 +22983,7 @@ static struct ins_template templates[] = { }, }, [TEMPLATE_DIV16] = { - .lhs = { + .lhs = { [0] = { REG_AX, REGCM_GPR16 }, [1] = { REG_DX, REGCM_GPR16 }, }, @@ -22925,7 +22993,7 @@ static struct ins_template templates[] = { }, }, [TEMPLATE_DIV32] = { - .lhs = { + .lhs = { [0] = { REG_EAX, REGCM_GPR32 }, [1] = { REG_EDX, REGCM_GPR32 }, }, @@ -22946,7 +23014,7 @@ static void fixup_branch(struct compile_state *state, } test = pre_triple(state, branch, cmp_op, cmp_type, left, right); - test->template_id = TEMPLATE_TEST32; + test->template_id = TEMPLATE_TEST32; if (cmp_op == OP_CMP) { test->template_id = TEMPLATE_CMP32_REG; if (get_imm32(test, &RHS(test, 1))) { @@ -22980,13 +23048,13 @@ static void fixup_branches(struct compile_state *state, right = RHS(cmp, 1); } branch = entry->member; - fixup_branch(state, branch, jmp_op, + fixup_branch(state, branch, jmp_op, cmp->op, cmp->type, left, right); } } } -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; @@ -23053,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]; @@ -23088,7 +23156,7 @@ struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, in /* 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]; @@ -23105,13 +23173,12 @@ struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, in static struct triple *mod_div(struct compile_state *state, struct triple *ins, int div_op, int index) { - struct triple *div, *piece0, *piece1; - + 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); - piece0 = LHS(div, 0); piece1 = LHS(div, 1); div->template_id = TEMPLATE_DIV32; use_triple(RHS(div, 0), div); @@ -23416,8 +23483,8 @@ static struct triple *transform_to_arch_instruction( case OP_NEG: 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); @@ -23457,7 +23524,7 @@ static struct triple *transform_to_arch_instruction( ins->template_id = TEMPLATE_NOP; break; case OP_CBRANCH: - fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST, + fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST, RHS(ins, 0)->type, RHS(ins, 0), 0); break; case OP_CALL: @@ -23563,20 +23630,20 @@ static void generate_local_labels(struct compile_state *state) 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 = 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; @@ -23607,7 +23674,7 @@ static const char *arch_regs[] = { "%edx:%eax", "%dx:%ax", "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7", - "%xmm0", "%xmm1", "%xmm2", "%xmm3", + "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7", }; static const char *arch_reg_str(int reg) @@ -23666,7 +23733,7 @@ static int reg_size(struct compile_state *state, struct triple *ins) } return arch_reg_size(reg); } - + const char *type_suffix(struct compile_state *state, struct type *type) @@ -23689,7 +23756,7 @@ static void print_const_val( { switch(ins->op) { case OP_INTCONST: - fprintf(fp, " $%ld ", + fprintf(fp, " $%ld ", (long)(ins->u.cval)); break; case OP_ADDRCONST: @@ -23702,7 +23769,7 @@ static void print_const_val( internal_error(state, ins, "unlabeled constant"); } fprintf(fp, " $L%s%lu+%lu ", - state->compiler->label_prefix, + state->compiler->label_prefix, (unsigned long)(MISC(ins, 0)->u.cval), (unsigned long)(ins->u.cval)); break; @@ -23720,12 +23787,12 @@ static void print_const(struct compile_state *state, switch(ins->type->type & TYPE_MASK) { case TYPE_CHAR: case TYPE_UCHAR: - fprintf(fp, ".byte 0x%02lx\n", + fprintf(fp, ".byte 0x%02lx\n", (unsigned long)(ins->u.cval)); break; case TYPE_SHORT: case TYPE_USHORT: - fprintf(fp, ".short 0x%04lx\n", + fprintf(fp, ".short 0x%04lx\n", (unsigned long)(ins->u.cval)); break; case TYPE_INT: @@ -23733,7 +23800,7 @@ static void print_const(struct compile_state *state, case TYPE_LONG: case TYPE_ULONG: case TYPE_POINTER: - fprintf(fp, ".int %lu\n", + fprintf(fp, ".int %lu\n", (unsigned long)(ins->u.cval)); break; default: @@ -23743,7 +23810,7 @@ static void print_const(struct compile_state *state, 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) && @@ -23786,12 +23853,12 @@ static long get_const_pool_ref( long ref; ref = next_label(state); fprintf(fp, ".section \"" DATA_SECTION "\"\n"); - fprintf(fp, ".balign %d\n", align_of_in_bytes(state, ins->type)); + 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 %d, 1, 0\n", fill_bytes); + fprintf(fp, ".fill %ld, 1, 0\n", (long int)fill_bytes); } fprintf(fp, ".section \"" TEXT_SECTION "\"\n"); return ref; @@ -23815,7 +23882,7 @@ static long get_mask_pool_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_LO; @@ -23842,7 +23909,7 @@ 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; @@ -23906,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)); } @@ -23932,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"); @@ -23944,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)); } @@ -23994,7 +24061,7 @@ static void print_op_move(struct compile_state *state, src_regcm = arch_reg_regcm(state, src_reg); dst_regcm = arch_reg_regcm(state, dst_reg); /* If the class is the same just move the register */ - if (src_regcm & dst_regcm & + 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", @@ -24008,7 +24075,7 @@ 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)); } } @@ -24045,7 +24112,7 @@ static void print_op_move(struct compile_state *state, } } /* Move 8/16bit to 16/32bit */ - else if ((src_regcm & (REGCM_GPR8_LO | 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"; @@ -24117,7 +24184,7 @@ static void print_op_move(struct compile_state *state, 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), + arch_reg_str(src_reg), extend); } /* Move from 64bit gpr to gpr */ @@ -24125,7 +24192,7 @@ static void print_op_move(struct compile_state *state, (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; } @@ -24312,11 +24379,10 @@ static void print_op_move(struct compile_state *state, if ((size_of(state, src->type) < size_of(state, dst->type)) && (is_signed(src->type))) { - int bits, reg_bits, shift_bits; + int reg_bits, shift_bits; int dst_reg; int dst_regcm; - bits = size_of(state, src->type); reg_bits = reg_size(state, dst); if (reg_bits > 32) { reg_bits = 32; @@ -24330,19 +24396,19 @@ static void print_op_move(struct compile_state *state, } if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) { - fprintf(fp, "\tshl $%d, %s\n", - shift_bits, + 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, + 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, + shift_bits, reg(state, dst, REGCM_MMX | REGCM_XMM)); fprintf(fp, "\tpsrad $%d, %s\n", - shift_bits, + shift_bits, reg(state, dst, REGCM_MMX | REGCM_XMM)); } else { @@ -24373,7 +24439,7 @@ static void print_op_load(struct compile_state *state, case TYPE_INT: case TYPE_UINT: case TYPE_LONG: case TYPE_ULONG: case TYPE_POINTER: - op = "movl"; + op = "movl"; break; default: internal_error(state, ins, "unknown type in load"); @@ -24381,7 +24447,7 @@ static void print_op_load(struct compile_state *state, break; } fprintf(fp, "\t%s (%s), %s\n", - op, + op, reg(state, src, REGCM_GPR32), reg(state, dst, REGCM_GPR32)); } @@ -24416,8 +24482,8 @@ static void print_op_store(struct compile_state *state, reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32), reg(state, dst, REGCM_GPR32)); } - - + + } static void print_op_smul(struct compile_state *state, @@ -24494,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) { @@ -24516,7 +24584,7 @@ static void print_op_branch(struct compile_state *state, internal_error(state, branch, "Invalid branch op"); break; } - + } #if 1 if (branch->op == OP_CALL) { @@ -24524,7 +24592,7 @@ static void print_op_branch(struct compile_state *state, } #endif fprintf(fp, "\t%s L%s%lu\n", - bop, + bop, state->compiler->label_prefix, (unsigned long)(TARG(branch, 0)->u.cval)); } @@ -24570,19 +24638,19 @@ static void print_op_set(struct compile_state *state, 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" @@ -24598,19 +24666,19 @@ 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_in_bytes(state, ins->type)); - fprintf(fp, "L%s%lu:\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, (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: @@ -24642,7 +24710,7 @@ static void print_instruction(struct compile_state *state, case OP_SDECL: print_sdecl(state, ins, fp); break; - case OP_COPY: + case OP_COPY: case OP_CONVERT: print_op_move(state, ins, fp); break; @@ -24677,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: @@ -24709,7 +24777,7 @@ static void print_instruction(struct compile_state *state, if (!ins->use) { return; } - fprintf(fp, "L%s%lu:\n", + fprintf(fp, "L%s%lu:\n", state->compiler->label_prefix, (unsigned long)(ins->u.cval)); break; case OP_ADECL: @@ -24756,12 +24824,12 @@ static void print_instructions(struct compile_state *state) first = state->first; ins = first; do { - if (print_location && + if (print_location && last_occurance != ins->occurance) { if (!ins->occurance->parent) { fprintf(fp, "\t/* %s,%s:%d.%d */\n", - ins->occurance->function, - ins->occurance->filename, + ins->occurance->function?ins->occurance->function:"(null)", + ins->occurance->filename?ins->occurance->filename:"(null)", ins->occurance->line, ins->occurance->col); } @@ -24803,56 +24871,61 @@ static void generate_code(struct compile_state *state) { generate_local_labels(state); print_instructions(state); - + } static void print_preprocessed_tokens(struct compile_state *state) { - struct token *tk; int tok; FILE *fp; int line; const char *filename; fp = state->output; - tk = &state->token[0]; filename = 0; line = 0; for(;;) { + struct file_state *file; + struct token *tk; const char *token_str; tok = peek(state); if (tok == TOK_EOF) { break; } - eat(state, tok); - token_str = + tk = eat(state, tok); + token_str = tk->ident ? tk->ident->name : tk->str_len ? tk->val.str : tokens[tk->tok]; - - if ((state->file->line != line) || - (state->file->basename != filename)) { + + file = state->file; + while(file->macro && file->prev) { + file = file->prev; + } + if (!file->macro && + ((file->line != line) || (file->basename != filename))) + { int i, col; - if ((state->file->basename == filename) && - (line < state->file->line)) { - while(line < state->file->line) { + if ((file->basename == filename) && + (line < file->line)) { + while(line < file->line) { fprintf(fp, "\n"); line++; } } else { fprintf(fp, "\n#line %d \"%s\"\n", - state->file->line, state->file->basename); + file->line, file->basename); } - line = state->file->line; - filename = state->file->basename; - col = get_col(state->file) - strlen(token_str); + 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", @@ -24861,12 +24934,13 @@ static void print_preprocessed_tokens(struct compile_state *state) } } -static void compile(const char *filename, +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; @@ -24879,10 +24953,14 @@ static void compile(const char *filename, state.errout = stderr; state.dbgout = stdout; /* Remember the output filename */ - state.output = fopen(state.compiler->ofilename, "w"); - if (!state.output) { - error(&state, 0, "Cannot open output file %s\n", - state.compiler->ofilename); + 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; @@ -24905,7 +24983,6 @@ static void compile(const char *filename, state.i_default = lookup(&state, "default", 7); state.i_return = lookup(&state, "return", 6); /* Memorize where predefined macros are. */ - state.i_defined = lookup(&state, "defined", 7); state.i___VA_ARGS__ = lookup(&state, "__VA_ARGS__", 11); state.i___FILE__ = lookup(&state, "__FILE__", 8); state.i___LINE__ = lookup(&state, "__LINE__", 8); @@ -24914,6 +24991,7 @@ static void compile(const char *filename, /* 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); @@ -24935,10 +25013,16 @@ static void compile(const char *filename, /* Enter the globl definition scope */ start_scope(&state); register_builtins(&state); + compile_file(&state, filename, 1); + while (includes) { + compile_file(&state, includes->filename, 1); + includes=includes->next; + } + /* Stop if all we want is preprocessor output */ - if (state.compiler->flags & COMPILER_CPP_ONLY) { + if (state.compiler->flags & COMPILER_PP_ONLY) { print_preprocessed_tokens(&state); return; } @@ -24948,8 +25032,8 @@ static void compile(const char *filename, /* 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); @@ -25005,8 +25089,8 @@ int main(int argc, char **argv) struct compiler_state compiler; struct arch_state arch; int all_opts; - - + + /* I don't want any surprises */ setlocale(LC_ALL, "C"); @@ -25051,6 +25135,24 @@ int main(int argc, char **argv) else if (strncmp(argv[1], "-m", 2) == 0) { result = arch_encode_flag(&arch, argv[1]+2); } + else if (strncmp(argv[1], "-c", 2) == 0) { + result = 0; + } + else if (strncmp(argv[1], "-S", 2) == 0) { + result = 0; + } + else if (strncmp(argv[1], "-include", 10) == 0) { + struct filelist *old_head = include_filelist; + include_filelist = malloc(sizeof(struct filelist)); + if (!include_filelist) { + die("Out of memory.\n"); + } + argv++; + argc--; + include_filelist->filename = strdup(argv[1]); + include_filelist->next = old_head; + result = 0; + } if (result < 0) { arg_error("Invalid option specified: %s\n", argv[1]);