blob: ff7aee669b12c295d893b5a790d5839f42913b8f [file] [log] [blame]
#include <stdarg.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#define MAX_ALLOCATION_PASSES 100
#define DEBUG_CONSISTENCY 1
#define DEBUG_SDP_BLOCKS 0
#define DEBUG_TRIPLE_COLOR 0
#warning "FIXME boundary cases with small types in larger registers"
#warning "FIXME give clear error messages about unused variables"
#warning "FIXME properly handle multi dimensional arrays"
/* Control flow graph of a loop without goto.
*
* AAA
* +---/
* /
* / +--->CCC
* | | / \
* | | DDD EEE break;
* | | \ \
* | | FFF \
* \| / \ \
* |\ GGG HHH | continue;
* | \ \ | |
* | \ III | /
* | \ | / /
* | vvv /
* +----BBB /
* | /
* vv
* JJJ
*
*
* AAA
* +-----+ | +----+
* | \ | / |
* | BBB +-+ |
* | / \ / | |
* | CCC JJJ / /
* | / \ / /
* | DDD EEE / /
* | | +-/ /
* | FFF /
* | / \ /
* | GGG HHH /
* | | +-/
* | III
* +--+
*
*
* DFlocal(X) = { Y <- Succ(X) | idom(Y) != X }
* DFup(Z) = { Y <- DF(Z) | idom(Y) != X }
*
*
* [] == DFlocal(X) U DF(X)
* () == DFup(X)
*
* Dominator graph of the same nodes.
*
* AAA AAA: [ ] ()
* / \
* BBB JJJ BBB: [ JJJ ] ( JJJ ) JJJ: [ ] ()
* |
* CCC CCC: [ ] ( BBB, JJJ )
* / \
* DDD EEE DDD: [ ] ( BBB ) EEE: [ JJJ ] ()
* |
* FFF FFF: [ ] ( BBB )
* / \
* GGG HHH GGG: [ ] ( BBB ) HHH: [ BBB ] ()
* |
* III III: [ BBB ] ()
*
*
* 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, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fflush(stdout);
fflush(stderr);
exit(1);
}
#define MALLOC_STRONG_DEBUG
static void *xmalloc(size_t size, const char *name)
{
void *buf;
buf = malloc(size);
if (!buf) {
die("Cannot malloc %ld bytes to hold %s: %s\n",
size + 0UL, name, strerror(errno));
}
return buf;
}
static void *xcmalloc(size_t size, const char *name)
{
void *buf;
buf = xmalloc(size, name);
memset(buf, 0, size);
return buf;
}
static void xfree(const void *ptr)
{
free((void *)ptr);
}
static char *xstrdup(const char *str)
{
char *new;
int len;
len = strlen(str);
new = xmalloc(len + 1, "xstrdup string");
memcpy(new, str, len);
new[len] = '\0';
return new;
}
static void xchdir(const char *path)
{
if (chdir(path) != 0) {
die("chdir to %s failed: %s\n",
path, strerror(errno));
}
}
static int exists(const char *dirname, const char *filename)
{
int does_exist = 1;
xchdir(dirname);
if (access(filename, O_RDONLY) < 0) {
if ((errno != EACCES) && (errno != EROFS)) {
does_exist = 0;
}
}
return does_exist;
}
static char *slurp_file(const char *dirname, const char *filename, off_t *r_size)
{
int fd;
char *buf;
off_t size, progress;
ssize_t result;
struct stat stats;
if (!filename) {
*r_size = 0;
return 0;
}
xchdir(dirname);
fd = open(filename, O_RDONLY);
if (fd < 0) {
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;
*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);
if (result < 0) {
if ((errno == EINTR) || (errno == EAGAIN))
continue;
die("read on %s of %ld bytes failed: %s\n",
filename, (size - progress)+ 0UL, strerror(errno));
}
progress += result;
}
result = close(fd);
if (result < 0) {
die("Close of %s failed: %s\n",
filename, strerror(errno));
}
return buf;
}
/* Types on the destination platform */
#warning "FIXME this assumes 32bit x86 is the destination"
typedef int8_t schar_t;
typedef uint8_t uchar_t;
typedef int8_t char_t;
typedef int16_t short_t;
typedef uint16_t ushort_t;
typedef int32_t int_t;
typedef uint32_t uint_t;
typedef int32_t long_t;
typedef uint32_t ulong_t;
#define SCHAR_T_MIN (-128)
#define SCHAR_T_MAX 127
#define UCHAR_T_MAX 255
#define CHAR_T_MIN SCHAR_T_MIN
#define CHAR_T_MAX SCHAR_T_MAX
#define SHRT_T_MIN (-32768)
#define SHRT_T_MAX 32767
#define USHRT_T_MAX 65535
#define INT_T_MIN (-LONG_T_MAX - 1)
#define INT_T_MAX 2147483647
#define UINT_T_MAX 4294967295U
#define LONG_T_MIN (-LONG_T_MAX - 1)
#define LONG_T_MAX 2147483647
#define ULONG_T_MAX 4294967295U
struct file_state {
struct file_state *prev;
const char *basename;
char *dirname;
char *buf;
off_t size;
char *pos;
int line;
char *line_start;
int report_line;
const char *report_name;
const char *report_dir;
};
struct hash_entry;
struct token {
int tok;
struct hash_entry *ident;
int str_len;
union {
ulong_t integer;
const char *str;
} val;
};
/* I have two classes of types:
* Operational types.
* Logical types. (The type the C standard says the operation is of)
*
* The operational types are:
* chars
* shorts
* ints
* longs
*
* floats
* doubles
* long doubles
*
* pointer
*/
/* Machine model.
* No memory is useable by the compiler.
* There is no floating point support.
* All operations take place in general purpose registers.
* There is one type of general purpose register.
* Unsigned longs are stored in that general purpose register.
*/
/* Operations on general purpose registers.
*/
#define OP_SDIVT 0
#define OP_UDIVT 1
#define OP_SMUL 2
#define OP_UMUL 3
#define OP_SDIV 4
#define OP_UDIV 5
#define OP_SMOD 6
#define OP_UMOD 7
#define OP_ADD 8
#define OP_SUB 9
#define OP_SL 10
#define OP_USR 11
#define OP_SSR 12
#define OP_AND 13
#define OP_XOR 14
#define OP_OR 15
#define OP_POS 16 /* Dummy positive operator don't use it */
#define OP_NEG 17
#define OP_INVERT 18
#define OP_EQ 20
#define OP_NOTEQ 21
#define OP_SLESS 22
#define OP_ULESS 23
#define OP_SMORE 24
#define OP_UMORE 25
#define OP_SLESSEQ 26
#define OP_ULESSEQ 27
#define OP_SMOREEQ 28
#define OP_UMOREEQ 29
#define OP_LFALSE 30 /* Test if the expression is logically false */
#define OP_LTRUE 31 /* Test if the expression is logcially true */
#define OP_LOAD 32
#define OP_STORE 33
/* For OP_STORE ->type holds the type
* RHS(0) holds the destination address
* RHS(1) holds the value to store.
*/
#define OP_NOOP 34
#define OP_MIN_CONST 50
#define OP_MAX_CONST 59
#define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
#define OP_INTCONST 50
/* For OP_INTCONST ->type holds the type.
* ->u.cval holds the constant value.
*/
#define OP_BLOBCONST 51
/* For OP_BLOBCONST ->type holds the layout and size
* information. u.blob holds a pointer to the raw binary
* data for the constant initializer.
*/
#define OP_ADDRCONST 52
/* For OP_ADDRCONST ->type holds the type.
* MISC(0) holds the reference to the static variable.
* ->u.cval holds an offset from that value.
*/
#define OP_WRITE 60
/* OP_WRITE moves one pseudo register to another.
* RHS(0) holds the destination pseudo register, which must be an OP_DECL.
* RHS(1) holds the psuedo to move.
*/
#define OP_READ 61
/* OP_READ reads the value of a variable and makes
* it available for the pseudo operation.
* Useful for things like def-use chains.
* RHS(0) holds points to the triple to read from.
*/
#define OP_COPY 62
/* OP_COPY makes a copy of the psedo register or constant in RHS(0).
*/
#define OP_PIECE 63
/* OP_PIECE returns one piece of a instruction that returns a structure.
* MISC(0) is the instruction
* u.cval is the LHS piece of the instruction to return.
*/
#define OP_ASM 64
/* OP_ASM holds a sequence of assembly instructions, the result
* of a C asm directive.
* RHS(x) holds input value x to the assembly sequence.
* LHS(x) holds the output value x from the assembly sequence.
* u.blob holds the string of assembly instructions.
*/
#define OP_DEREF 65
/* OP_DEREF generates an lvalue from a pointer.
* RHS(0) holds the pointer value.
* OP_DEREF serves as a place holder to indicate all necessary
* checks have been done to indicate a value is an lvalue.
*/
#define OP_DOT 66
/* OP_DOT references a submember of a structure lvalue.
* RHS(0) holds the lvalue.
* ->u.field holds the name of the field we want.
*
* Not seen outside of expressions.
*/
#define OP_VAL 67
/* OP_VAL returns the value of a subexpression of the current expression.
* Useful for operators that have side effects.
* RHS(0) holds the expression.
* MISC(0) holds the subexpression of RHS(0) that is the
* value of the expression.
*
* Not seen outside of expressions.
*/
#define OP_LAND 68
/* OP_LAND performs a C logical and between RHS(0) and RHS(1).
* Not seen outside of expressions.
*/
#define OP_LOR 69
/* OP_LOR performs a C logical or between RHS(0) and RHS(1).
* Not seen outside of expressions.
*/
#define OP_COND 70
/* OP_CODE performas a C ? : operation.
* RHS(0) holds the test.
* RHS(1) holds the expression to evaluate if the test returns true.
* RHS(2) holds the expression to evaluate if the test returns false.
* Not seen outside of expressions.
*/
#define OP_COMMA 71
/* OP_COMMA performacs a C comma operation.
* That is RHS(0) is evaluated, then RHS(1)
* and the value of RHS(1) is returned.
* Not seen outside of expressions.
*/
#define OP_FCALL 72
/* OP_FCALL performs a procedure call.
* MISC(0) holds a pointer to the OP_LIST of a function
* RHS(x) holds argument x of a function
*
* Currently not seen outside of expressions.
*/
#define OP_VAL_VEC 74
/* OP_VAL_VEC is an array of triples that are either variable
* or values for a structure or an array.
* RHS(x) holds element x of the vector.
* triple->type->elements holds the size of the vector.
*/
/* statements */
#define OP_LIST 80
/* OP_LIST Holds a list of statements that compose a function, and a result value.
* RHS(0) holds the list of statements.
* MISC(0) holds the value of the statements.
* A list of all functions is maintained.
*/
#define OP_BRANCH 81 /* an unconditional branch */
/* For branch instructions
* TARG(0) holds the branch target.
* ->next holds where to branch to if the branch is not taken.
* The branch target can only be a label
*/
#define OP_CBRANCH 82 /* a conditional branch */
/* For conditional branch instructions
* RHS(0) holds the branch condition.
* TARG(1) holds the branch target.
* ->next holds where to branch to if the branch is not taken.
* The branch target can only be a label
*/
#define OP_CALL 83 /* an uncontional branch that will return */
/* For call instructions
* MISC(0) holds the OP_RET that returns from the branch
* TARG(0) holds the branch target.
* ->next holds where to branch to if the branch is not taken.
* The branch target can only be a label
*/
#define OP_RET 84 /* an uncontinonal branch through a variable back to an OP_CALL */
/* For call instructions
* RHS(0) holds the variable with the return address
* The branch target can only be a label
*/
#define OP_LABEL 86
/* OP_LABEL is a triple that establishes an target for branches.
* ->use is the list of all branches that use this label.
*/
#define OP_ADECL 87
/* OP_ADECL is a triple that establishes an lvalue for assignments.
* ->use is a list of statements that use the variable.
*/
#define OP_SDECL 88
/* OP_SDECL is a triple that establishes a variable of static
* storage duration.
* ->use is a list of statements that use the variable.
* MISC(0) holds the initializer expression.
*/
#define OP_PHI 89
/* OP_PHI is a triple used in SSA form code.
* It is used when multiple code paths merge and a variable needs
* a single assignment from any of those code paths.
* The operation is a cross between OP_DECL and OP_WRITE, which
* is what OP_PHI is 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
* to the variables OP_PHI is derived from.
*
* MISC(0) holds a pointer to the orginal OP_DECL node.
*/
/* Architecture specific instructions */
#define OP_CMP 100
#define OP_TEST 101
#define OP_SET_EQ 102
#define OP_SET_NOTEQ 103
#define OP_SET_SLESS 104
#define OP_SET_ULESS 105
#define OP_SET_SMORE 106
#define OP_SET_UMORE 107
#define OP_SET_SLESSEQ 108
#define OP_SET_ULESSEQ 109
#define OP_SET_SMOREEQ 110
#define OP_SET_UMOREEQ 111
#define OP_JMP 112
#define OP_JMP_EQ 113
#define OP_JMP_NOTEQ 114
#define OP_JMP_SLESS 115
#define OP_JMP_ULESS 116
#define OP_JMP_SMORE 117
#define OP_JMP_UMORE 118
#define OP_JMP_SLESSEQ 119
#define OP_JMP_ULESSEQ 120
#define OP_JMP_SMOREEQ 121
#define OP_JMP_UMOREEQ 122
/* Builtin operators that it is just simpler to use the compiler for */
#define OP_INB 130
#define OP_INW 131
#define OP_INL 132
#define OP_OUTB 133
#define OP_OUTW 134
#define OP_OUTL 135
#define OP_BSF 136
#define OP_BSR 137
#define OP_RDMSR 138
#define OP_WRMSR 139
#define OP_HLT 140
struct op_info {
const char *name;
unsigned flags;
#define PURE 1 /* Triple has no side effects */
#define IMPURE 2 /* Triple has side effects */
#define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
#define DEF 4 /* Triple is a variable definition */
#define BLOCK 8 /* Triple stores the current block */
#define STRUCTURAL 16 /* Triple does not generate a machine instruction */
#define BRANCH 32 /* Triple is a branch instruction */
#define CBRANCH 64 /* Triple is a conditional branch instruction */
unsigned char lhs, rhs, misc, targ;
};
#define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \
.name = (NAME), \
.flags = (FLAGS), \
.lhs = (LHS), \
.rhs = (RHS), \
.misc = (MISC), \
.targ = (TARG), \
}
static const struct op_info table_ops[] = {
[OP_SDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "sdivt"),
[OP_UDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "udivt"),
[OP_SMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smul"),
[OP_UMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umul"),
[OP_SDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sdiv"),
[OP_UDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "udiv"),
[OP_SMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smod"),
[OP_UMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umod"),
[OP_ADD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "add"),
[OP_SUB ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sub"),
[OP_SL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sl"),
[OP_USR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "usr"),
[OP_SSR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ssr"),
[OP_AND ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "and"),
[OP_XOR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "xor"),
[OP_OR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "or"),
[OP_POS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "pos"),
[OP_NEG ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "neg"),
[OP_INVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "invert"),
[OP_EQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "eq"),
[OP_NOTEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "noteq"),
[OP_SLESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sless"),
[OP_ULESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "uless"),
[OP_SMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smore"),
[OP_UMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umore"),
[OP_SLESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "slesseq"),
[OP_ULESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ulesseq"),
[OP_SMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smoreeq"),
[OP_UMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umoreeq"),
[OP_LFALSE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "lfalse"),
[OP_LTRUE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "ltrue"),
[OP_LOAD ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "load"),
[OP_STORE ] = OP( 0, 2, 0, 0, IMPURE | BLOCK , "store"),
[OP_NOOP ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "noop"),
[OP_INTCONST ] = OP( 0, 0, 0, 0, PURE | DEF, "intconst"),
[OP_BLOBCONST ] = OP( 0, 0, 0, 0, PURE , "blobconst"),
[OP_ADDRCONST ] = OP( 0, 0, 1, 0, PURE | DEF, "addrconst"),
[OP_WRITE ] = OP( 0, 2, 0, 0, PURE | BLOCK, "write"),
[OP_READ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "read"),
[OP_COPY ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "copy"),
[OP_PIECE ] = OP( 0, 0, 1, 0, PURE | DEF | STRUCTURAL, "piece"),
[OP_ASM ] = OP(-1, -1, 0, 0, IMPURE, "asm"),
[OP_DEREF ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "deref"),
[OP_DOT ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "dot"),
[OP_VAL ] = OP( 0, 1, 1, 0, 0 | DEF | BLOCK, "val"),
[OP_LAND ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "land"),
[OP_LOR ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "lor"),
[OP_COND ] = OP( 0, 3, 0, 0, 0 | DEF | BLOCK, "cond"),
[OP_COMMA ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "comma"),
/* Call is special most it can stand in for anything so it depends on context */
[OP_FCALL ] = OP(-1, -1, 1, 0, 0 | BLOCK, "fcall"),
/* The sizes of OP_FCALL and OP_VAL_VEC depend upon context */
[OP_VAL_VEC ] = OP( 0, -1, 0, 0, 0 | BLOCK | STRUCTURAL, "valvec"),
[OP_LIST ] = OP( 0, 1, 1, 0, 0 | DEF | STRUCTURAL, "list"),
[OP_BRANCH ] = OP( 0, 0, 0, 1, PURE | BLOCK | BRANCH, "branch"),
[OP_CBRANCH ] = OP( 0, 1, 0, 1, PURE | BLOCK | BRANCH | CBRANCH, "cbranch"),
[OP_CALL ] = OP( 0, 0, 1, 1, PURE | BLOCK | BRANCH, "call"),
[OP_RET ] = OP( 0, 1, 0, 0, PURE | BLOCK | BRANCH, "ret"),
[OP_LABEL ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "label"),
[OP_ADECL ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "adecl"),
[OP_SDECL ] = OP( 0, 0, 1, 0, PURE | BLOCK | STRUCTURAL, "sdecl"),
/* The number of RHS elements of OP_PHI depend upon context */
[OP_PHI ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
[OP_CMP ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK, "cmp"),
[OP_TEST ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "test"),
[OP_SET_EQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_eq"),
[OP_SET_NOTEQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_noteq"),
[OP_SET_SLESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_sless"),
[OP_SET_ULESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_uless"),
[OP_SET_SMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smore"),
[OP_SET_UMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umore"),
[OP_SET_SLESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_slesseq"),
[OP_SET_ULESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_ulesseq"),
[OP_SET_SMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smoreq"),
[OP_SET_UMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umoreq"),
[OP_JMP ] = OP( 0, 0, 0, 1, PURE | BLOCK | BRANCH, "jmp"),
[OP_JMP_EQ ] = OP( 0, 1, 0, 1, PURE | BLOCK | BRANCH | CBRANCH, "jmp_eq"),
[OP_JMP_NOTEQ ] = OP( 0, 1, 0, 1, PURE | BLOCK | BRANCH | CBRANCH, "jmp_noteq"),
[OP_JMP_SLESS ] = OP( 0, 1, 0, 1, PURE | BLOCK | BRANCH | CBRANCH, "jmp_sless"),
[OP_JMP_ULESS ] = OP( 0, 1, 0, 1, PURE | BLOCK | BRANCH | CBRANCH, "jmp_uless"),
[OP_JMP_SMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK | BRANCH | CBRANCH, "jmp_smore"),
[OP_JMP_UMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK | BRANCH | CBRANCH, "jmp_umore"),
[OP_JMP_SLESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | BRANCH | CBRANCH, "jmp_slesseq"),
[OP_JMP_ULESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | BRANCH | CBRANCH, "jmp_ulesseq"),
[OP_JMP_SMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | BRANCH | CBRANCH, "jmp_smoreq"),
[OP_JMP_UMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | BRANCH | CBRANCH, "jmp_umoreq"),
[OP_INB ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inb"),
[OP_INW ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inw"),
[OP_INL ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inl"),
[OP_OUTB ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outb"),
[OP_OUTW ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outw"),
[OP_OUTL ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outl"),
[OP_BSF ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsf"),
[OP_BSR ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsr"),
[OP_RDMSR ] = OP( 2, 1, 0, 0, IMPURE | BLOCK, "__rdmsr"),
[OP_WRMSR ] = OP( 0, 3, 0, 0, IMPURE | BLOCK, "__wrmsr"),
[OP_HLT ] = OP( 0, 0, 0, 0, IMPURE | BLOCK, "__hlt"),
};
#undef OP
#define OP_MAX (sizeof(table_ops)/sizeof(table_ops[0]))
static const char *tops(int index)
{
static const char unknown[] = "unknown op";
if (index < 0) {
return unknown;
}
if (index > OP_MAX) {
return unknown;
}
return table_ops[index].name;
}
struct asm_info;
struct triple;
struct block;
struct triple_set {
struct triple_set *next;
struct triple *member;
};
#define MAX_LHS 15
#define MAX_RHS 250
#define MAX_MISC 3
#define MAX_TARG 3
struct occurance {
int count;
const char *filename;
const char *function;
int line;
int col;
struct occurance *parent;
};
struct triple {
struct triple *next, *prev;
struct triple_set *use;
struct type *type;
unsigned char op;
unsigned char template_id;
unsigned short sizes;
#define TRIPLE_LHS(SIZES) (((SIZES) >> 0) & 0x0f)
#define TRIPLE_RHS(SIZES) (((SIZES) >> 4) & 0xff)
#define TRIPLE_MISC(SIZES) (((SIZES) >> 12) & 0x03)
#define TRIPLE_TARG(SIZES) (((SIZES) >> 14) & 0x03)
#define TRIPLE_SIZE(SIZES) \
(TRIPLE_LHS(SIZES) + \
TRIPLE_RHS(SIZES) + \
TRIPLE_MISC(SIZES) + \
TRIPLE_TARG(SIZES))
#define TRIPLE_SIZES(LHS, RHS, MISC, TARG) \
((((LHS) & 0x0f) << 0) | \
(((RHS) & 0xff) << 4) | \
(((MISC) & 0x03) << 12) | \
(((TARG) & 0x03) << 14))
#define TRIPLE_LHS_OFF(SIZES) (0)
#define TRIPLE_RHS_OFF(SIZES) (TRIPLE_LHS_OFF(SIZES) + TRIPLE_LHS(SIZES))
#define TRIPLE_MISC_OFF(SIZES) (TRIPLE_RHS_OFF(SIZES) + TRIPLE_RHS(SIZES))
#define TRIPLE_TARG_OFF(SIZES) (TRIPLE_MISC_OFF(SIZES) + TRIPLE_MISC(SIZES))
#define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF((PTR)->sizes) + (INDEX)])
#define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF((PTR)->sizes) + (INDEX)])
#define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF((PTR)->sizes) + (INDEX)])
#define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF((PTR)->sizes) + (INDEX)])
unsigned id; /* A scratch value and finally the register */
#define TRIPLE_FLAG_FLATTENED (1 << 31)
#define TRIPLE_FLAG_PRE_SPLIT (1 << 30)
#define TRIPLE_FLAG_POST_SPLIT (1 << 29)
#define TRIPLE_FLAG_VOLATILE (1 << 28)
#define TRIPLE_FLAG_LOCAL (1 << 27)
struct occurance *occurance;
union {
ulong_t cval;
struct block *block;
void *blob;
struct hash_entry *field;
struct asm_info *ainfo;
} u;
struct triple *param[2];
};
struct reg_info {
unsigned reg;
unsigned regcm;
};
struct ins_template {
struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
};
struct asm_info {
struct ins_template tmpl;
char *str;
};
struct block_set {
struct block_set *next;
struct block *member;
};
struct block {
struct block *work_next;
struct triple *first, *last;
int edge_count;
struct block_set *edges;
int users;
struct block_set *use;
struct block_set *idominates;
struct block_set *domfrontier;
struct block *idom;
struct block_set *ipdominates;
struct block_set *ipdomfrontier;
struct block *ipdom;
int vertex;
};
struct symbol {
struct symbol *next;
struct hash_entry *ident;
struct triple *def;
struct type *type;
int scope_depth;
};
struct macro {
struct hash_entry *ident;
char *buf;
int buf_len;
};
struct hash_entry {
struct hash_entry *next;
const char *name;
int name_len;
int tok;
struct macro *sym_define;
struct symbol *sym_label;
struct symbol *sym_tag;
struct symbol *sym_ident;
};
#define HASH_TABLE_SIZE 2048
struct compiler_state {
const char *label_prefix;
const char *ofilename;
unsigned long flags;
unsigned long debug;
unsigned long max_allocation_passes;
};
struct arch_state {
unsigned long features;
};
struct compile_state {
struct compiler_state *compiler;
struct arch_state *arch;
FILE *output;
struct file_state *file;
struct occurance *last_occurance;
const char *function;
struct token token[4];
struct hash_entry *hash_table[HASH_TABLE_SIZE];
struct hash_entry *i_switch;
struct hash_entry *i_case;
struct hash_entry *i_continue;
struct hash_entry *i_break;
struct hash_entry *i_default;
struct hash_entry *i_return;
int scope_depth;
int if_depth, if_value;
int macro_line;
struct file_state *macro_file;
struct triple *functions;
struct triple *main_function;
struct triple *first;
struct triple *global_pool;
struct block *first_block, *last_block;
int last_vertex;
};
/* visibility global/local */
/* static/auto duration */
/* typedef, register, inline */
#define STOR_SHIFT 0
#define STOR_MASK 0x001f
/* Visibility */
#define STOR_GLOBAL 0x0001
/* Duration */
#define STOR_PERM 0x0002
/* Definition locality */
#define STOR_NONLOCAL 0x0004 /* The definition is not in this translation unit */
/* Storage specifiers */
#define STOR_AUTO 0x0000
#define STOR_STATIC 0x0002
#define STOR_LOCAL 0x0003
#define STOR_EXTERN 0x0007
#define STOR_INLINE 0x0008
#define STOR_REGISTER 0x0010
#define STOR_TYPEDEF 0x0018
#define QUAL_SHIFT 5
#define QUAL_MASK 0x00e0
#define QUAL_NONE 0x0000
#define QUAL_CONST 0x0020
#define QUAL_VOLATILE 0x0040
#define QUAL_RESTRICT 0x0080
#define TYPE_SHIFT 8
#define TYPE_MASK 0x1f00
#define TYPE_INTEGER(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG)) || ((TYPE) == TYPE_ENUM))
#define TYPE_ARITHMETIC(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE)) || ((TYPE) == TYPE_ENUM))
#define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
#define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
#define TYPE_MKUNSIGNED(TYPE) (((TYPE) & ~0xF000) | 0x0100)
#define TYPE_RANK(TYPE) ((TYPE) & ~0xF1FF)
#define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER)
#define TYPE_DEFAULT 0x0000
#define TYPE_VOID 0x0100
#define TYPE_CHAR 0x0200
#define TYPE_UCHAR 0x0300
#define TYPE_SHORT 0x0400
#define TYPE_USHORT 0x0500
#define TYPE_INT 0x0600
#define TYPE_UINT 0x0700
#define TYPE_LONG 0x0800
#define TYPE_ULONG 0x0900
#define TYPE_LLONG 0x0a00 /* long long */
#define TYPE_ULLONG 0x0b00
#define TYPE_FLOAT 0x0c00
#define TYPE_DOUBLE 0x0d00
#define TYPE_LDOUBLE 0x0e00 /* long double */
/* Note: TYPE_ENUM is chosen very carefully so TYPE_RANK works */
#define TYPE_ENUM 0x1600
#define TYPE_LIST 0x1700
/* TYPE_LIST is a basic building block when defining enumerations
* type->field_ident holds the name of this enumeration entry.
* type->right holds the entry in the list.
*/
#define TYPE_STRUCT 0x1000
#define TYPE_UNION 0x1100
#define TYPE_POINTER 0x1200
/* For TYPE_POINTER:
* type->left holds the type pointed to.
*/
#define TYPE_FUNCTION 0x1300
/* For TYPE_FUNCTION:
* type->left holds the return type.
* type->right holds the...
*/
#define TYPE_PRODUCT 0x1400
/* TYPE_PRODUCT is a basic building block when defining structures
* type->left holds the type that appears first in memory.
* type->right holds the type that appears next in memory.
*/
#define TYPE_OVERLAP 0x1500
/* TYPE_OVERLAP is a basic building block when defining unions
* type->left and type->right holds to types that overlap
* each other in memory.
*/
#define TYPE_ARRAY 0x1800
/* TYPE_ARRAY is a basic building block when definitng arrays.
* type->left holds the type we are an array of.
* type-> holds the number of elements.
*/
#define ELEMENT_COUNT_UNSPECIFIED ULONG_T_MAX
struct type {
unsigned int type;
struct type *left, *right;
ulong_t elements;
struct hash_entry *field_ident;
struct hash_entry *type_ident;
};
#define TEMPLATE_BITS 7
#define MAX_TEMPLATES (1<<TEMPLATE_BITS)
#define MAX_REG_EQUIVS 16
#define MAX_REGC 14
#define MAX_REGISTERS 75
#define REGISTER_BITS 7
#define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
#define REG_UNSET 0
#define REG_UNNEEDED 1
#define REG_VIRT0 (MAX_REGISTERS + 0)
#define REG_VIRT1 (MAX_REGISTERS + 1)
#define REG_VIRT2 (MAX_REGISTERS + 2)
#define REG_VIRT3 (MAX_REGISTERS + 3)
#define REG_VIRT4 (MAX_REGISTERS + 4)
#define REG_VIRT5 (MAX_REGISTERS + 5)
#define REG_VIRT6 (MAX_REGISTERS + 6)
#define REG_VIRT7 (MAX_REGISTERS + 7)
#define REG_VIRT8 (MAX_REGISTERS + 8)
#define REG_VIRT9 (MAX_REGISTERS + 9)
#if (MAX_REGISTERS + 9) > MAX_VIRT_REGISTERS
#error "MAX_VIRT_REGISTERS to small"
#endif
#if (MAX_REGC + REGISTER_BITS) > 27
#error "Too many id bits used"
#endif
/* Provision for 8 register classes */
#define REG_SHIFT 0
#define REGC_SHIFT REGISTER_BITS
#define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
#define REG_MASK (MAX_VIRT_REGISTERS -1)
#define ID_REG(ID) ((ID) & REG_MASK)
#define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
#define ID_REGCM(ID) (((ID) & REGC_MASK) >> REGC_SHIFT)
#define SET_REGCM(ID, REGCM) ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
#define SET_INFO(ID, INFO) ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
(((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
static unsigned arch_reg_regcm(struct compile_state *state, int reg);
static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm);
static void arch_reg_equivs(
struct compile_state *state, unsigned *equiv, int reg);
static int arch_select_free_register(
struct compile_state *state, char *used, int classes);
static unsigned arch_regc_size(struct compile_state *state, int class);
static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
static const char *arch_reg_str(int reg);
static struct reg_info arch_reg_constraint(
struct compile_state *state, struct type *type, const char *constraint);
static struct reg_info arch_reg_clobber(
struct compile_state *state, const char *clobber);
static struct reg_info arch_reg_lhs(struct compile_state *state,
struct triple *ins, int index);
static struct reg_info arch_reg_rhs(struct compile_state *state,
struct triple *ins, int index);
static struct triple *transform_to_arch_instruction(
struct compile_state *state, struct triple *ins);
#define DEBUG_ABORT_ON_ERROR 0x00000001
#define DEBUG_BASIC_BLOCKS 0x00000002
#define DEBUG_FDOMINATORS 0x00000004
#define DEBUG_RDOMINATORS 0x00000008
#define DEBUG_TRIPLES 0x00000010
#define DEBUG_INTERFERENCE 0x00000020
#define DEBUG_SCC_TRANSFORM 0x00000040
#define DEBUG_SCC_TRANSFORM2 0x00000080
#define DEBUG_REBUILD_SSA_FORM 0x00000100
#define DEBUG_INLINE 0x00000200
#define DEBUG_RANGE_CONFLICTS 0x00000400
#define DEBUG_RANGE_CONFLICTS2 0x00000800
#define DEBUG_COLOR_GRAPH 0x00001000
#define DEBUG_COLOR_GRAPH2 0x00002000
#define DEBUG_COALESCING 0x00004000
#define DEBUG_COALESCING2 0x00008000
#define DEBUG_DEFAULT ( \
DEBUG_ABORT_ON_ERROR | \
DEBUG_BASIC_BLOCKS | \
DEBUG_FDOMINATORS | \
DEBUG_RDOMINATORS | \
DEBUG_TRIPLES | \
0 )
#define COMPILER_ELIMINATE_INEFECTUAL_CODE 0x00000001
#define COMPILER_SIMPLIFY 0x00000002
#define COMPILER_SCC_TRANSFORM 0x00000004
#define COMPILER_INLINE 0x00000008
#define COMPILER_ALWAYS_INLINE 0x00000010
#define COMPILER_SIMPLIFY_OP 0x00000020
#define COMPILER_SIMPLIFY_PHI 0x00000040
#define COMPILER_SIMPLIFY_LABEL 0x00000080
#define COMPILER_SIMPLIFY_BRANCH 0x00000100
#define COMPILER_SIMPLIFY_COPY 0x00000200
#define COMPILER_SIMPLIFY_ARITH 0x00000400
#define COMPILER_SIMPLIFY_SHIFT 0x00000800
#define COMPILER_SIMPLIFY_BITWISE 0x00001000
#define COMPILER_SIMPLIFY_LOGICAL 0x00002000
#define COMPILER_DEFAULT_FLAGS ( \
COMPILER_ELIMINATE_INEFECTUAL_CODE | \
COMPILER_INLINE | \
COMPILER_ALWAYS_INLINE | \
COMPILER_SIMPLIFY_OP | \
COMPILER_SIMPLIFY_PHI | \
COMPILER_SIMPLIFY_LABEL | \
COMPILER_SIMPLIFY_BRANCH | \
COMPILER_SIMPLIFY_COPY | \
COMPILER_SIMPLIFY_ARITH | \
COMPILER_SIMPLIFY_SHIFT | \
COMPILER_SIMPLIFY_BITWISE | \
COMPILER_SIMPLIFY_LOGICAL | \
0 )
#define GLOBAL_SCOPE_DEPTH 1
#define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
static void compile_file(struct compile_state *old_state, const char *filename, int local);
static void init_compiler_state(struct compiler_state *compiler)
{
memset(compiler, 0, sizeof(*compiler));
compiler->label_prefix = "";
compiler->ofilename = "auto.inc";
compiler->flags = COMPILER_DEFAULT_FLAGS;
compiler->debug = 0;
compiler->max_allocation_passes = MAX_ALLOCATION_PASSES;
}
struct compiler_flag {
const char *name;
unsigned long flag;
};
static int set_flag(
const struct compiler_flag *ptr, unsigned long *flags,
int act, const char *flag)
{
int result = -1;
for(; ptr->name; ptr++) {
if (strcmp(ptr->name, flag) == 0) {
break;
}
}
if (ptr->name) {
result = 0;
*flags &= ~(ptr->flag);
if (act) {
*flags |= ptr->flag;
}
}
return result;
}
static int compiler_encode_flag(
struct compiler_state *compiler, const char *flag)
{
static const struct compiler_flag flags[] = {
{ "eliminate-inefectual-code", COMPILER_ELIMINATE_INEFECTUAL_CODE },
{ "simplify", COMPILER_SIMPLIFY },
{ "scc-transform", COMPILER_SCC_TRANSFORM },
{ "inline", COMPILER_INLINE },
{ "always-inline", COMPILER_ALWAYS_INLINE },
{ "simplify-op", COMPILER_SIMPLIFY_OP },
{ "simplify-phi", COMPILER_SIMPLIFY_PHI },
{ "simplify-label", COMPILER_SIMPLIFY_LABEL },
{ "simplify-branch", COMPILER_SIMPLIFY_BRANCH },
{ "simplify-copy", COMPILER_SIMPLIFY_COPY },
{ "simplify-arith", COMPILER_SIMPLIFY_ARITH },
{ "simplify-shift", COMPILER_SIMPLIFY_SHIFT },
{ "simplify-bitwise", COMPILER_SIMPLIFY_BITWISE },
{ "simplify-logical", COMPILER_SIMPLIFY_LOGICAL },
{ 0, 0 },
};
static const struct compiler_flag opt_flags[] = {
{ "-O", COMPILER_SIMPLIFY },
{ "-O2", COMPILER_SIMPLIFY | COMPILER_SCC_TRANSFORM },
{ 0, 0, },
};
static const struct compiler_flag debug_flags[] = {
{ "abort-on-error", DEBUG_ABORT_ON_ERROR },
{ "basic-blocks", DEBUG_BASIC_BLOCKS },
{ "fdominators", DEBUG_FDOMINATORS },
{ "rdominators", DEBUG_RDOMINATORS },
{ "triples", DEBUG_TRIPLES },
{ "interference", DEBUG_INTERFERENCE },
{ "scc-transform", DEBUG_SCC_TRANSFORM },
{ "scc-transform2", DEBUG_SCC_TRANSFORM2 },
{ "rebuild-ssa-form", DEBUG_REBUILD_SSA_FORM },
{ "inline", DEBUG_INLINE },
{ "live-range-conflicts", DEBUG_RANGE_CONFLICTS },
{ "live-range-conflicts2", DEBUG_RANGE_CONFLICTS2 },
{ "color-graph", DEBUG_COLOR_GRAPH },
{ "color-graph2", DEBUG_COLOR_GRAPH2 },
{ "coalescing", DEBUG_COALESCING },
{ "coalescing2", DEBUG_COALESCING2 },
{ 0, 0 },
};
int act;
int result;
act = 1;
result = -1;
if (strncmp(flag, "no-", 3) == 0) {
flag += 3;
act = 0;
}
if (strncmp(flag, "-O", 2) == 0) {
result = set_flag(opt_flags, &compiler->flags, act, flag);
}
else if (act && strncmp(flag, "label-prefix=", 13) == 0) {
result = 0;
compiler->label_prefix = flag + 13;
}
else if (act && strncmp(flag, "max-allocation-passes=", 22) == 0) {
unsigned long max_passes;
char *end;
max_passes = strtoul(flag + 22, &end, 10);
if (end[0] == '\0') {
result = 0;
compiler->max_allocation_passes = max_passes;
}
}
else if (act && strcmp(flag, "debug") == 0) {
result = 0;
compiler->debug |= DEBUG_DEFAULT;
}
else if (strncmp(flag, "debug-", 6) == 0) {
flag += 6;
result = set_flag(debug_flags, &compiler->debug, act, flag);
}
else {
result = set_flag(flags, &compiler->flags, act, flag);
}
return result;
}
static void do_cleanup(struct compile_state *state)
{
if (state->output) {
fclose(state->output);
unlink(state->compiler->ofilename);
}
}
static int get_col(struct file_state *file)
{
int col;
char *ptr, *end;
ptr = file->line_start;
end = file->pos;
for(col = 0; ptr < end; ptr++) {
if (*ptr != '\t') {
col++;
}
else {
col = (col & ~7) + 8;
}
}
return col;
}
static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
{
int col;
if (triple && triple->occurance) {
struct occurance *spot;
for(spot = triple->occurance; spot; spot = spot->parent) {
fprintf(fp, "%s:%d.%d: ",
spot->filename, spot->line, spot->col);
}
return;
}
if (!state->file) {
return;
}
col = get_col(state->file);
fprintf(fp, "%s:%d.%d: ",
state->file->report_name, state->file->report_line, col);
}
static void internal_error(struct compile_state *state, struct triple *ptr,
char *fmt, ...)
{
va_list args;
va_start(args, fmt);
loc(stderr, state, ptr);
fputc('\n', stderr);
if (ptr) {
fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
}
fprintf(stderr, "Internal compiler error: ");
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
do_cleanup(state);
abort();
}
static void internal_warning(struct compile_state *state, struct triple *ptr,
char *fmt, ...)
{
va_list args;
va_start(args, fmt);
loc(stderr, state, ptr);
if (ptr) {
fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
}
fprintf(stderr, "Internal compiler warning: ");
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
}
static void error(struct compile_state *state, struct triple *ptr,
char *fmt, ...)
{
va_list args;
va_start(args, fmt);
loc(stderr, state, ptr);
fputc('\n', stderr);
if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
}
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
do_cleanup(state);
if (state->compiler->debug & DEBUG_ABORT_ON_ERROR) {
abort();
}
exit(1);
}
static void warning(struct compile_state *state, struct triple *ptr,
char *fmt, ...)
{
va_list args;
va_start(args, fmt);
loc(stderr, state, ptr);
fprintf(stderr, "warning: ");
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
}
#define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
static void valid_op(struct compile_state *state, int op)
{
char *fmt = "invalid op: %d";
if (op >= OP_MAX) {
internal_error(state, 0, fmt, op);
}
if (op < 0) {
internal_error(state, 0, fmt, op);
}
}
static void valid_ins(struct compile_state *state, struct triple *ptr)
{
valid_op(state, ptr->op);
}
static void process_trigraphs(struct compile_state *state)
{
char *src, *dest, *end;
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;
}
static struct type void_type;
static void use_triple(struct triple *used, struct triple *user)
{
struct triple_set **ptr, *new;
if (!used)
return;
if (!user)
return;
ptr = &used->use;
while(*ptr) {
if ((*ptr)->member == user) {
return;
}
ptr = &(*ptr)->next;
}
/* Append new to the head of the list,
* copy_func and rename_block_variables
* depends on this.
*/
new = xcmalloc(sizeof(*new), "triple_set");
new->member = user;
new->next = used->use;
used->use = new;
}
static void unuse_triple(struct triple *used, struct triple *unuser)
{
struct triple_set *use, **ptr;
if (!used) {
return;
}
ptr = &used->use;
while(*ptr) {
use = *ptr;
if (use->member == unuser) {
*ptr = use->next;
xfree(use);
}
else {
ptr = &use->next;
}
}
}
static void put_occurance(struct occurance *occurance)
{
if (occurance) {
occurance->count -= 1;
if (occurance->count <= 0) {
if (occurance->parent) {
put_occurance(occurance->parent);
}
xfree(occurance);
}
}
}
static void get_occurance(struct occurance *occurance)
{
if (occurance) {
occurance->count += 1;
}
}
static struct occurance *new_occurance(struct compile_state *state)
{
struct occurance *result, *last;
const char *filename;
const char *function;
int line, col;
function = "";
filename = 0;
line = 0;
col = 0;
if (state->file) {
filename = state->file->report_name;
line = state->file->report_line;
col = get_col(state->file);
}
if (state->function) {
function = state->function;
}
last = state->last_occurance;
if (last &&
(last->col == col) &&
(last->line == line) &&
(last->function == function) &&
((last->filename == filename) ||
(strcmp(last->filename, filename) == 0)))
{
get_occurance(last);
return last;
}
if (last) {
state->last_occurance = 0;
put_occurance(last);
}
result = xmalloc(sizeof(*result), "occurance");
result->count = 2;
result->filename = filename;
result->function = function;
result->line = line;
result->col = col;
result->parent = 0;
state->last_occurance = result;
return result;
}
static struct occurance *inline_occurance(struct compile_state *state,
struct occurance *base, struct occurance *top)
{
struct occurance *result, *last;
if (top->parent) {
internal_error(state, 0, "inlining an already inlined function?");
}
/* If I have a null base treat it that way */
if ((base->parent == 0) &&
(base->col == 0) &&
(base->line == 0) &&
(base->function[0] == '\0') &&
(base->filename[0] == '\0')) {
base = 0;
}
/* See if I can reuse the last occurance I had */
last = state->last_occurance;
if (last &&
(last->parent == base) &&
(last->col == top->col) &&
(last->line == top->line) &&
(last->function == top->function) &&
(last->filename == top->filename)) {
get_occurance(last);
return last;
}
/* I can't reuse the last occurance so free it */
if (last) {
state->last_occurance = 0;
put_occurance(last);
}
/* Generate a new occurance structure */
get_occurance(base);
result = xmalloc(sizeof(*result), "occurance");
result->count = 2;
result->filename = top->filename;
result->function = top->function;
result->line = top->line;
result->col = top->col;
result->parent = base;
state->last_occurance = result;
return result;
}
static struct occurance dummy_occurance = {
.count = 2,
.filename = __FILE__,
.function = "",
.line = __LINE__,
.col = 0,
.parent = 0,
};
/* The zero triple is used as a place holder when we are removing pointers
* from a triple. Having allows certain sanity checks to pass even
* when the original triple that was pointed to is gone.
*/
static struct triple zero_triple = {
.next = &zero_triple,
.prev = &zero_triple,
.use = 0,
.op = OP_INTCONST,
.sizes = TRIPLE_SIZES(0, 0, 0, 0),
.id = -1, /* An invalid id */
.u = { .cval = 0, },
.occurance = &dummy_occurance,
.param = { [0] = 0, [1] = 0, },
};
static unsigned short triple_sizes(struct compile_state *state,
int op, struct type *type, int lhs_wanted, int rhs_wanted,
struct occurance *occurance)
{
int lhs, rhs, misc, targ;
struct triple dummy;
dummy.op = op;
dummy.occurance = occurance;
valid_op(state, op);
lhs = table_ops[op].lhs;
rhs = table_ops[op].rhs;
misc = table_ops[op].misc;
targ = table_ops[op].targ;
if (op == OP_FCALL) {
rhs = rhs_wanted;
lhs = 0;
if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
lhs = type->elements;
}
}
else if (op == OP_VAL_VEC) {
rhs = type->elements;
}
else if (op == OP_PHI) {
rhs = rhs_wanted;
}
else if (op == OP_ASM) {
rhs = rhs_wanted;
lhs = lhs_wanted;
}
if ((rhs < 0) || (rhs > MAX_RHS)) {
internal_error(state, &dummy, "bad rhs %d", rhs);
}
if ((lhs < 0) || (lhs > MAX_LHS)) {
internal_error(state, &dummy, "bad lhs");
}
if ((misc < 0) || (misc > MAX_MISC)) {
internal_error(state, &dummy, "bad misc");
}
if ((targ < 0) || (targ > MAX_TARG)) {
internal_error(state, &dummy, "bad targs");
}
return TRIPLE_SIZES(lhs, rhs, misc, targ);
}
static struct triple *alloc_triple(struct compile_state *state,
int op, struct type *type, int lhs, int rhs,
struct occurance *occurance)
{
size_t size, sizes, extra_count, min_count;
struct triple *ret;
sizes = triple_sizes(state, op, type, lhs, rhs, occurance);
min_count = sizeof(ret->param)/sizeof(ret->param[0]);
extra_count = TRIPLE_SIZE(sizes);
extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
ret = xcmalloc(size, "tripple");
ret->op = op;
ret->sizes = sizes;
ret->type = type;
ret->next = ret;
ret->prev = ret;
ret->occurance = occurance;
return ret;
}
struct triple *dup_triple(struct compile_state *state, struct triple *src)
{
struct triple *dup;
int src_lhs, src_rhs, src_size;
src_lhs = TRIPLE_LHS(src->sizes);
src_rhs = TRIPLE_RHS(src->sizes);
src_size = TRIPLE_SIZE(src->sizes);
get_occurance(src->occurance);
dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
src->occurance);
memcpy(dup, src, sizeof(*src));
memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
return dup;
}
static struct triple *new_triple(struct compile_state *state,
int op, struct type *type, int lhs, int rhs)
{
struct triple *ret;
struct occurance *occurance;
occurance = new_occurance(state);
ret = alloc_triple(state, op, type, lhs, rhs, occurance);
return ret;
}
static struct triple *build_triple(struct compile_state *state,
int op, struct type *type, struct triple *left, struct triple *right,
struct occurance *occurance)
{
struct triple *ret;
size_t count;
ret = alloc_triple(state, op, type, -1, -1, occurance);
count = TRIPLE_SIZE(ret->sizes);
if (count > 0) {
ret->param[0] = left;
}
if (count > 1) {
ret->param[1] = right;
}
return ret;
}
static struct triple *triple(struct compile_state *state,
int op, struct type *type, struct triple *left, struct triple *right)
{
struct triple *ret;
size_t count;
ret = new_triple(state, op, type, -1, -1);
count = TRIPLE_SIZE(ret->sizes);
if (count >= 1) {
ret->param[0] = left;
}
if (count >= 2) {
ret->param[1] = right;
}
return ret;
}
static struct triple *branch(struct compile_state *state,
struct triple *targ, struct triple *test)
{
struct triple *ret;
if (test) {
ret = new_triple(state, OP_CBRANCH, &void_type, -1, 1);
RHS(ret, 0) = test;
} else {
ret = new_triple(state, OP_BRANCH, &void_type, -1, 0);
}
TARG(ret, 0) = targ;
/* record the branch target was used */
if (!targ || (targ->op != OP_LABEL)) {
internal_error(state, 0, "branch not to label");
}
return ret;
}
static void insert_triple(struct compile_state *state,
struct triple *first, struct triple *ptr)
{
if (ptr) {
if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
internal_error(state, ptr, "expression already used");
}
ptr->next = first;
ptr->prev = first->prev;
ptr->prev->next = ptr;
ptr->next->prev = ptr;
if ((ptr->prev->op == OP_CBRANCH) || (ptr->prev->op == OP_CALL)) {
unuse_triple(first, ptr->prev);
use_triple(ptr, ptr->prev);
}
}
}
static int triple_stores_block(struct compile_state *state, struct triple *ins)
{
/* This function is used to determine if u.block
* is utilized to store the current block number.
*/
int stores_block;
valid_ins(state, ins);
stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
return stores_block;
}
static struct block *block_of_triple(struct compile_state *state,
struct triple *ins)
{
struct triple *first;
if (!ins || ins == &zero_triple) {
return 0;
}
first = state->first;
while(ins != first && !triple_stores_block(state, ins)) {
if (ins == ins->prev) {
internal_error(state, ins, "ins == ins->prev?");
}
ins = ins->prev;
}
if (!triple_stores_block(state, ins)) {
internal_error(state, ins, "Cannot find block");
}
return ins->u.block;
}
static struct triple *pre_triple(struct compile_state *state,
struct triple *base,
int op, struct type *type, struct triple *left, struct triple *right)
{
struct block *block;
struct triple *ret;
/* If I am an OP_PIECE jump to the real instruction */
if (base->op == OP_PIECE) {
base = MISC(base, 0);
}
block = block_of_triple(state, base);
get_occurance(base->occurance);
ret = build_triple(state, op, type, left, right, base->occurance);
if (triple_stores_block(state, ret)) {
ret->u.block = block;
}
insert_triple(state, base, ret);
if (block->first == base) {
block->first = ret;
}
return ret;
}
static struct triple *post_triple(struct compile_state *state,
struct triple *base,
int op, struct type *type, struct triple *left, struct triple *right)
{
struct block *block;
struct triple *ret;
int zlhs;
/* If I am an OP_PIECE jump to the real instruction */
if (base->op == OP_PIECE) {
base = MISC(base, 0);
}
/* If I have a left hand side skip over it */
zlhs = TRIPLE_LHS(base->sizes);
if (zlhs) {
base = LHS(base, zlhs - 1);
}
block = block_of_triple(state, base);
get_occurance(base->occurance);
ret = build_triple(state, op, type, left, right, base->occurance);
if (triple_stores_block(state, ret)) {
ret->u.block = block;
}
insert_triple(state, base->next, ret);
if (block->last == base) {
block->last = ret;
}
return ret;
}
static struct triple *label(struct compile_state *state)
{
/* Labels don't get a type */
struct triple *result;
result = triple(state, OP_LABEL, &void_type, 0, 0);
return result;
}
static void display_triple(FILE *fp, struct triple *ins)
{
struct occurance *ptr;
const char *reg;
char pre, post;
pre = post = ' ';
if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
pre = '^';
}
if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
post = 'v';
}
reg = arch_reg_str(ID_REG(ins->id));
if (ins->op == OP_INTCONST) {
fprintf(fp, "(%p) %c%c %-7s %-2d %-10s <0x%08lx> ",
ins, pre, post, reg, ins->template_id, tops(ins->op),
(unsigned long)(ins->u.cval));
}
else if (ins->op == OP_ADDRCONST) {
fprintf(fp, "(%p) %c%c %-7s %-2d %-10s %-10p <0x%08lx>",
ins, pre, post, reg, ins->template_id, tops(ins->op),
MISC(ins, 0), (unsigned long)(ins->u.cval));
}
else {
int i, count;
fprintf(fp, "(%p) %c%c %-7s %-2d %-10s",
ins, pre, post, reg, ins->template_id, tops(ins->op));
count = TRIPLE_SIZE(ins->sizes);
for(i = 0; i < count; i++) {
fprintf(fp, " %-10p", ins->param[i]);
}
for(; i < 2; i++) {
fprintf(fp, " ");
}
}
fprintf(fp, " @");
for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
fprintf(fp, " %s,%s:%d.%d",
ptr->function,
ptr->filename,
ptr->line,
ptr->col);
}
fprintf(fp, "\n");
#if 0
{
struct triple_set *user;
for(user = ptr->use; user; user = user->next) {
fprintf(fp, "use: %p\n", user->member);
}
}
#endif
fflush(fp);
}
static void display_triple_changes(
FILE *fp, const struct triple *new, const struct triple *orig)
{
int new_count, orig_count;
new_count = TRIPLE_SIZE(new->sizes);
orig_count = TRIPLE_SIZE(orig->sizes);
if ((new->op != orig->op) ||
(new_count != orig_count) ||
(memcmp(orig->param, new->param,
orig_count * sizeof(orig->param[0])) != 0) ||
(memcmp(&orig->u, &new->u, sizeof(orig->u)) != 0))
{
struct occurance *ptr;
int i, min_count, indent;
fprintf(fp, "(%p)", orig);
if (orig->op == new->op) {
fprintf(fp, " %-11s", tops(orig->op));
} else {
fprintf(fp, " [%-10s %-10s]",
tops(new->op), tops(orig->op));
}
min_count = new_count;
if (min_count > orig_count) {
min_count = orig_count;
}
for(indent = i = 0; i < min_count; i++) {
if (orig->param[i] == new->param[i]) {
fprintf(fp, " %-11p",
orig->param[i]);
indent += 12;
} else {
fprintf(fp, " [%-10p %-10p]",
new->param[i],
orig->param[i]);
indent += 24;
}
}
for(; i < orig_count; i++) {
fprintf(fp, " [%-9p]", orig->param[i]);
indent += 12;
}
for(; i < new_count; i++) {
fprintf(fp, " [%-9p]", new->param[i]);
indent += 12;
}
if ((new->op == OP_INTCONST)||
(new->op == OP_ADDRCONST)) {
fprintf(fp, " <0x%08lx>",
(unsigned long)(new->u.cval));
indent += 13;
}
for(;indent < 36; indent++) {
putc(' ', fp);
}
fprintf(fp, " @");
for(ptr = orig->occurance; ptr; ptr = ptr->parent) {
fprintf(fp, " %s,%s:%d.%d",
ptr->function,
ptr->filename,
ptr->line,
ptr->col);
}
fprintf(fp, "\n");
fflush(fp);
}
}
static void display_func(FILE *fp, struct triple *func)
{
struct triple *first, *ins;
fprintf(fp, "display_func %s\n", func->type->type_ident->name);
first = ins = RHS(func, 0);
do {
display_triple(fp, ins);
ins = ins->next;
} while(ins != first);
}
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
* gives the same value.
*/
unsigned pure;
valid_ins(state, ins);
pure = PURE_BITS(table_ops[ins->op].flags);
if ((pure != PURE) && (pure != IMPURE)) {
internal_error(state, 0, "Purity of %s not known\n",
tops(ins->op));
}
return (pure == PURE) && !(id & TRIPLE_FLAG_VOLATILE);
}
static int triple_is_branch(struct compile_state *state, struct triple *ins)
{
/* Is this triple a branch instruction? */
valid_ins(state, ins);
return (table_ops[ins->op].flags & BRANCH) != 0;
}
static int triple_is_cond_branch(struct compile_state *state, struct triple *ins)
{
/* Is this triple a conditional branch instruction? */
valid_ins(state, ins);
return (table_ops[ins->op].flags & CBRANCH) != 0;
}
static int triple_is_uncond_branch(struct compile_state *state, struct triple *ins)
{
/* Is this triple a unconditional branch instruction? */
valid_ins(state, ins);
return (table_ops[ins->op].flags & CBRANCH) == 0;
}
static int triple_is_def(struct compile_state *state, struct triple *ins)
{
/* This function is used to determine which triples need
* a register.
*/
int is_def;
valid_ins(state, ins);
is_def = (table_ops[ins->op].flags & DEF) == DEF;
return is_def;
}
static int triple_is_structural(struct compile_state *state, struct triple *ins)
{
int is_structural;
valid_ins(state, ins);
is_structural = (table_ops[ins->op].flags & STRUCTURAL) == STRUCTURAL;
return is_structural;
}
static struct triple **triple_iter(struct compile_state *state,
size_t count, struct triple **vector,
struct triple *ins, struct triple **last)
{
struct triple **ret;
ret = 0;
if (count) {
if (!last) {
ret = vector;
}
else if ((last >= vector) && (last < (vector + count - 1))) {
ret = last + 1;
}
}
return ret;
}
static struct triple **triple_lhs(struct compile_state *state,
struct triple *ins, struct triple **last)
{
return triple_iter(state, TRIPLE_LHS(ins->sizes), &LHS(ins,0),
ins, last);
}
static struct triple **triple_rhs(struct compile_state *state,
struct triple *ins, struct triple **last)
{
return triple_iter(state, TRIPLE_RHS(ins->sizes), &RHS(ins,0),
ins, last);
}
static struct triple **triple_misc(struct compile_state *state,
struct triple *ins, struct triple **last)
{
return triple_iter(state, TRIPLE_MISC(ins->sizes), &MISC(ins,0),
ins, last);
}
static struct triple **triple_targ(struct compile_state *state,
struct triple *ins, struct triple **last)
{
size_t count;
struct triple **ret, **vector;
ret = 0;
count = TRIPLE_TARG(ins->sizes);
vector = &TARG(ins, 0);
if (!ret &&
((ins->op == OP_CALL) || (table_ops[ins->op].flags & CBRANCH))) {
if (!last) {
ret = &ins->next;
} else if (last == &ins->next) {
last = 0;
}
}
if (!ret && count) {
if (!last) {
ret = vector;
}
else if ((last >= vector) && (last < (vector + count - 1))) {
ret = last + 1;
}
else if (last == vector + count - 1) {
last = 0;
}
}
if (!ret && (ins->op == OP_RET)) {
struct triple_set *use;
for(use = ins->use; use; use = use->next) {
if (use->member->op != OP_CALL) {
continue;
}
if (!last) {
ret = &use->member->next;
break;
}
else if (last == &use->member->next) {
last = 0;
}
}
}
return ret;
}
static void verify_use(struct compile_state *state,
struct triple *user, struct triple *used)
{
int size, i;
size = TRIPLE_SIZE(user->sizes);
for(i = 0; i < size; i++) {
if (user->param[i] == used) {
break;
}
}
if (triple_is_branch(state, user)) {
if (user->next == used) {
i = -1;
}
}
if (i == size) {
internal_error(state, user, "%s(%p) does not use %s(%p)",
tops(user->op), user, tops(used->op), used);
}
}
static int find_rhs_use(struct compile_state *state,
struct triple *user, struct triple *used)
{
struct triple **param;
int size, i;
verify_use(state, user, used);
size = TRIPLE_RHS(user->sizes);
param = &RHS(user, 0);
for(i = 0; i < size; i++) {
if (param[i] == used) {
return i;
}
}
return -1;
}
static void free_triple(struct compile_state *state, struct triple *ptr)
{
size_t size;
size = sizeof(*ptr) - sizeof(ptr->param) +
(sizeof(ptr->param[0])*TRIPLE_SIZE(ptr->sizes));
ptr->prev->next = ptr->next;
ptr->next->prev = ptr->prev;
if (ptr->use) {
internal_error(state, ptr, "ptr->use != 0");
}
put_occurance(ptr->occurance);
memset(ptr, -1, size);
xfree(ptr);
}
static void release_triple(struct compile_state *state, struct triple *ptr)
{
struct triple_set *set, *next;
struct triple **expr;
struct block *block;
valid_ins(state, ptr);
/* Make certain the we are not the first or last element of a block */
block = block_of_triple(state, ptr);
if (block) {
if ((block->last == ptr) && (block->first == ptr)) {
block->last = block->first = 0;
}
else if (block->last == ptr) {
block->last = ptr->prev;
}
else if (block->first == ptr) {
block->first = ptr->next;
}
}
/* Remove ptr from use chains where it is the user */
expr = triple_rhs(state, ptr, 0);
for(; expr; expr = triple_rhs(state, ptr, expr)) {
if (*expr) {
unuse_triple(*expr, ptr);
}
}
expr = triple_lhs(state, ptr, 0);
for(; expr; expr = triple_lhs(state, ptr, expr)) {
if (*expr) {
unuse_triple(*expr, ptr);
}
}
expr = triple_misc(state, ptr, 0);
for(; expr; expr = triple_misc(state, ptr, expr)) {
if (*expr) {
unuse_triple(*expr, ptr);
}
}
expr = triple_targ(state, ptr, 0);
for(; expr; expr = triple_targ(state, ptr, expr)) {
if (*expr){
unuse_triple(*expr, ptr);
}
}
/* Reomve ptr from use chains where it is used */
for(set = ptr->use; set; set = next) {
next = set->next;
valid_ins(state, set->member);
expr = triple_rhs(state, set->member, 0);
for(; expr; expr = triple_rhs(state, set->member, expr)) {
if (*expr == ptr) {
*expr = &zero_triple;
}
}
expr = triple_lhs(state, set->member, 0);
for(; expr; expr = triple_lhs(state, set->member, expr)) {
if (*expr == ptr) {
*expr = &zero_triple;
}
}
expr = triple_misc(state, set->member, 0);
for(; expr; expr = triple_misc(state, set->member, expr)) {
if (*expr == ptr) {
*expr = &zero_triple;
}
}
expr = triple_targ(state, set->member, 0);
for(; expr; expr = triple_targ(state, set->member, expr)) {
if (*expr == ptr) {
*expr = &zero_triple;
}
}
unuse_triple(ptr, set->member);
}
free_triple(state, 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_FIRST_KEYWORD TOK_AUTO
#define TOK_LAST_KEYWORD TOK_ALIGNOF
#define TOK_DEFINE 100
#define TOK_UNDEF 101
#define TOK_INCLUDE 102
#define TOK_LINE 103
#define TOK_ERROR 104
#define TOK_WARNING 105
#define TOK_PRAGMA 106
#define TOK_IFDEF 107
#define TOK_IFNDEF 108
#define TOK_ELIF 109
#define TOK_ENDIF 110
#define TOK_FIRST_MACRO TOK_DEFINE
#define TOK_LAST_MACRO TOK_ENDIF
#define TOK_EOF 111
static const char *tokens[] = {
[TOK_UNKNOWN ] = "unknown",
[TOK_SPACE ] = ":space:",
[TOK_SEMI ] = ";",
[TOK_LBRACE ] = "{",
[TOK_RBRACE ] = "}",
[TOK_COMMA ] = ",",
[TOK_EQ ] = "=",
[TOK_COLON ] = ":",
[TOK_LBRACKET ] = "[",
[TOK_RBRACKET ] = "]",
[TOK_LPAREN ] = "(",
[TOK_RPAREN ] = ")",
[TOK_STAR ] = "*",
[TOK_DOTS ] = "...",
[TOK_MORE ] = ">",
[TOK_LESS ] = "<",
[TOK_TIMESEQ ] = "*=",
[TOK_DIVEQ ] = "/=",
[TOK_MODEQ ] = "%=",
[TOK_PLUSEQ ] = "+=",
[TOK_MINUSEQ ] = "-=",
[TOK_SLEQ ] = "<<=",
[TOK_SREQ ] = ">>=",
[TOK_ANDEQ ] = "&=",
[TOK_XOREQ ] = "^=",
[TOK_OREQ ] = "|=",
[TOK_EQEQ ] = "==",
[TOK_NOTEQ ] = "!=",
[TOK_QUEST ] = "?",
[TOK_LOGOR ] = "||",
[TOK_LOGAND ] = "&&",
[TOK_OR ] = "|",
[TOK_AND ] = "&",
[TOK_XOR ] = "^",
[TOK_LESSEQ ] = "<=",
[TOK_MOREEQ ] = ">=",
[TOK_SL ] = "<<",
[TOK_SR ] = ">>",
[TOK_PLUS ] = "+",
[TOK_MINUS ] = "-",
[TOK_DIV ] = "/",
[TOK_MOD ] = "%",
[TOK_PLUSPLUS ] = "++",
[TOK_MINUSMINUS ] = "--",
[TOK_BANG ] = "!",
[TOK_ARROW ] = "->",
[TOK_DOT ] = ".",
[TOK_TILDE ] = "~",
[TOK_LIT_STRING ] = ":string:",
[TOK_IDENT ] = ":ident:",
[TOK_TYPE_NAME ] = ":typename:",
[TOK_LIT_CHAR ] = ":char:",
[TOK_LIT_INT ] = ":integer:",
[TOK_LIT_FLOAT ] = ":float:",
[TOK_MACRO ] = "#",
[TOK_CONCATENATE ] = "##",
[TOK_AUTO ] = "auto",
[TOK_BREAK ] = "break",
[TOK_CASE ] = "case",
[TOK_CHAR ] = "char",
[TOK_CONST ] = "const",
[TOK_CONTINUE ] = "continue",
[TOK_DEFAULT ] = "default",
[TOK_DO ] = "do",
[TOK_DOUBLE ] = "double",
[TOK_ELSE ] = "else",
[TOK_ENUM ] = "enum",
[TOK_EXTERN ] = "extern",
[TOK_FLOAT ] = "float",
[TOK_FOR ] = "for",
[TOK_GOTO ] = "goto",
[TOK_IF ] = "if",
[TOK_INLINE ] = "inline",
[TOK_INT ] = "int",
[TOK_LONG ] = "long",
[TOK_REGISTER ] = "register",
[TOK_RESTRICT ] = "restrict",
[TOK_RETURN ] = "return",
[TOK_SHORT ] = "short",
[TOK_SIGNED ] = "signed",
[TOK_SIZEOF ] = "sizeof",
[TOK_STATIC ] = "static",
[TOK_STRUCT ] = "struct",
[TOK_SWITCH ] = "switch",
[TOK_TYPEDEF ] = "typedef",
[TOK_UNION ] = "union",
[TOK_UNSIGNED ] = "unsigned",
[TOK_VOID ] = "void",
[TOK_VOLATILE ] = "volatile",
[TOK_WHILE ] = "while",
[TOK_ASM ] = "asm",
[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_EOF ] = "EOF",
};
static unsigned int hash(const char *str, int str_len)
{
unsigned int hash;
const char *end;
end = str + str_len;
hash = 0;
for(; str < end; str++) {
hash = (hash *263) + *str;
}
hash = hash & (HASH_TABLE_SIZE -1);
return hash;
}
static struct hash_entry *lookup(
struct compile_state *state, const char *name, int name_len)
{
struct hash_entry *entry;
unsigned int index;
index = hash(name, name_len);
entry = state->hash_table[index];
while(entry &&
((entry->name_len != name_len) ||
(memcmp(entry->name, name, name_len) != 0))) {
entry = entry->next;
}
if (!entry) {
char *new_name;
/* Get a private copy of the name */
new_name = xmalloc(name_len + 1, "hash_name");
memcpy(new_name, name, name_len);
new_name[name_len] = '\0';
/* Create a new hash entry */
entry = xcmalloc(sizeof(*entry), "hash_entry");
entry->next = state->hash_table[index];
entry->name = new_name;
entry->name_len = name_len;
/* Place the new entry in the hash table */
state->hash_table[index] = entry;
}
return entry;
}
static void ident_to_keyword(struct compile_state *state, struct token *tk)
{
struct hash_entry *entry;
entry = tk->ident;
if (entry && ((entry->tok == TOK_TYPE_NAME) ||
(entry->tok == TOK_ENUM_CONST) ||
((entry->tok >= TOK_FIRST_KEYWORD) &&
(entry->tok <= TOK_LAST_KEYWORD)))) {
tk->tok = entry->tok;
}
}
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)) {
tk->tok = entry->tok;
}
}
static void hash_keyword(
struct compile_state *state, const char *keyword, int tok)
{
struct hash_entry *entry;
entry = lookup(state, keyword, strlen(keyword));
if (entry && entry->tok != TOK_UNKNOWN) {
die("keyword %s already hashed", keyword);
}
entry->tok = tok;
}
static void symbol(
struct compile_state *state, struct hash_entry *ident,
struct symbol **chain, struct triple *def, struct type *type)
{
struct symbol *sym;
if (*chain && ((*chain)->scope_depth == state->scope_depth)) {
error(state, 0, "%s already defined", ident->name);
}
sym = xcmalloc(sizeof(*sym), "symbol");
sym->ident = ident;
sym->def = def;
sym->type = type;
sym->scope_depth = state->scope_depth;
sym->next = *chain;
*chain = sym;
}
static void label_symbol(struct compile_state *state,
struct hash_entry *ident, struct triple *label)
{
struct symbol *sym;
if (ident->sym_label) {
error(state, 0, "label %s already defined", ident->name);
}
sym = xcmalloc(sizeof(*sym), "label");
sym->ident = ident;
sym->def = label;
sym->type = &void_type;
sym->scope_depth = FUNCTION_SCOPE_DEPTH;
sym->next = 0;
ident->sym_label = sym;
}
static void start_scope(struct compile_state *state)
{
state->scope_depth++;
}
static void end_scope_syms(struct symbol **chain, int depth)
{
struct symbol *sym, *next;
sym = *chain;
while(sym && (sym->scope_depth == depth)) {
next = sym->next;
xfree(sym);
sym = next;
}
*chain = sym;
}
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.
*/
depth = state->scope_depth;
for(i = 0; i < HASH_TABLE_SIZE; i++) {
struct hash_entry *entry;
entry = state->hash_table[i];
while(entry) {
end_scope_syms(&entry->sym_label, depth);
end_scope_syms(&entry->sym_tag, depth);
end_scope_syms(&entry->sym_ident, depth);
entry = entry->next;
}
}
state->scope_depth = depth - 1;
}
static void register_keywords(struct compile_state *state)
{
hash_keyword(state, "auto", TOK_AUTO);
hash_keyword(state, "break", TOK_BREAK);
hash_keyword(state, "case", TOK_CASE);
hash_keyword(state, "char", TOK_CHAR);
hash_keyword(state, "const", TOK_CONST);
hash_keyword(state, "continue", TOK_CONTINUE);
hash_keyword(state, "default", TOK_DEFAULT);
hash_keyword(state, "do", TOK_DO);
hash_keyword(state, "double", TOK_DOUBLE);
hash_keyword(state, "else", TOK_ELSE);
hash_keyword(state, "enum", TOK_ENUM);
hash_keyword(state, "extern", TOK_EXTERN);
hash_keyword(state, "float", TOK_FLOAT);
hash_keyword(state, "for", TOK_FOR);
hash_keyword(state, "goto", TOK_GOTO);
hash_keyword(state, "if", TOK_IF);
hash_keyword(state, "inline", TOK_INLINE);
hash_keyword(state, "int", TOK_INT);
hash_keyword(state, "long", TOK_LONG);
hash_keyword(state, "register", TOK_REGISTER);
hash_keyword(state, "restrict", TOK_RESTRICT);
hash_keyword(state, "return", TOK_RETURN);
hash_keyword(state, "short", TOK_SHORT);
hash_keyword(state, "signed", TOK_SIGNED);
hash_keyword(state, "sizeof", TOK_SIZEOF);
hash_keyword(state, "static", TOK_STATIC);
hash_keyword(state, "struct", TOK_STRUCT);
hash_keyword(state, "switch", TOK_SWITCH);
hash_keyword(state, "typedef", TOK_TYPEDEF);
hash_keyword(state, "union", TOK_UNION);
hash_keyword(state, "unsigned", TOK_UNSIGNED);
hash_keyword(state, "void", TOK_VOID);
hash_keyword(state, "volatile", TOK_VOLATILE);
hash_keyword(state, "__volatile__", TOK_VOLATILE);
hash_keyword(state, "while", TOK_WHILE);
hash_keyword(state, "asm", TOK_ASM);
hash_keyword(state, "__asm__", TOK_ASM);
hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
hash_keyword(state, "__alignof__", TOK_ALIGNOF);
}
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);
}
static int spacep(int c)
{
int ret = 0;
switch(c) {
case ' ':
case '\t':
case '\f':
case '\v':
case '\r':
case '\n':
ret = 1;
break;
}
return ret;
}
static int digitp(int c)
{
int ret = 0;
switch(c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
ret = 1;
break;
}
return ret;
}
static int digval(int c)
{
int val = -1;
if ((c >= '0') && (c <= '9')) {
val = c - '0';
}
return val;
}
static int hexdigitp(int c)
{
int ret = 0;
switch(c) {
case '0': case '1': case '2': case '3': case '4':
case '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':
ret = 1;
break;
}
return ret;
}
static int hexdigval(int c)
{
int val = -1;
if ((c >= '0') && (c <= '9')) {
val = c - '0';
}
else if ((c >= 'A') && (c <= 'F')) {
val = 10 + (c - 'A');
}
else if ((c >= 'a') && (c <= 'f')) {
val = 10 + (c - 'a');
}
return val;
}
static int octdigitp(int c)
{
int ret = 0;
switch(c) {
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
ret = 1;
break;
}
return ret;
}
static int octdigval(int c)
{
int val = -1;
if ((c >= '0') && (c <= '7')) {
val = c - '0';
}
return val;
}
static int letterp(int c)
{
int ret = 0;
switch(c) {
case 'a': case 'b': case 'c': case 'd': case 'e':
case 'f': case 'g': case 'h': case 'i': case 'j':
case 'k': case 'l': case 'm': case 'n': case 'o':
case 'p': case 'q': case 'r': case 's': case 't':
case 'u': case 'v': case 'w': case 'x': case 'y':
case 'z'