blob: d3cb322c8886a0710761607a3d00eaaf939993aa [file] [log] [blame]
Eric Biedermanb138ac82003-04-22 18:44:01 +00001#include <stdarg.h>
2#include <errno.h>
3#include <stdint.h>
4#include <stdlib.h>
5#include <stdio.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <fcntl.h>
9#include <unistd.h>
10#include <stdio.h>
11#include <string.h>
Eric Biedermanb138ac82003-04-22 18:44:01 +000012#include <limits.h>
13
14#define DEBUG_ERROR_MESSAGES 0
15#define DEBUG_COLOR_GRAPH 0
16#define DEBUG_SCC 0
Eric Biederman153ea352003-06-20 14:43:20 +000017#define DEBUG_CONSISTENCY 2
Eric Biedermand1ea5392003-06-28 06:49:45 +000018#define DEBUG_RANGE_CONFLICTS 0
19#define DEBUG_COALESCING 0
Eric Biederman530b5192003-07-01 10:05:30 +000020#define DEBUG_SDP_BLOCKS 0
21#define DEBUG_TRIPLE_COLOR 0
Eric Biedermanb138ac82003-04-22 18:44:01 +000022
Eric Biederman05f26fc2003-06-11 21:55:00 +000023#warning "FIXME boundary cases with small types in larger registers"
Eric Biederman8d9c1232003-06-17 08:42:17 +000024#warning "FIXME give clear error messages about unused variables"
Eric Biederman530b5192003-07-01 10:05:30 +000025#warning "FIXME properly handle multi dimensional arrays"
26#warning "FIXME fix scc_transform"
Eric Biederman05f26fc2003-06-11 21:55:00 +000027
Eric Biedermanb138ac82003-04-22 18:44:01 +000028/* Control flow graph of a loop without goto.
29 *
30 * AAA
31 * +---/
32 * /
33 * / +--->CCC
34 * | | / \
35 * | | DDD EEE break;
36 * | | \ \
37 * | | FFF \
38 * \| / \ \
39 * |\ GGG HHH | continue;
40 * | \ \ | |
41 * | \ III | /
42 * | \ | / /
43 * | vvv /
44 * +----BBB /
45 * | /
46 * vv
47 * JJJ
48 *
49 *
50 * AAA
51 * +-----+ | +----+
52 * | \ | / |
53 * | BBB +-+ |
54 * | / \ / | |
55 * | CCC JJJ / /
56 * | / \ / /
57 * | DDD EEE / /
58 * | | +-/ /
59 * | FFF /
60 * | / \ /
61 * | GGG HHH /
62 * | | +-/
63 * | III
64 * +--+
65 *
66 *
67 * DFlocal(X) = { Y <- Succ(X) | idom(Y) != X }
68 * DFup(Z) = { Y <- DF(Z) | idom(Y) != X }
69 *
70 *
71 * [] == DFlocal(X) U DF(X)
72 * () == DFup(X)
73 *
74 * Dominator graph of the same nodes.
75 *
76 * AAA AAA: [ ] ()
77 * / \
78 * BBB JJJ BBB: [ JJJ ] ( JJJ ) JJJ: [ ] ()
79 * |
80 * CCC CCC: [ ] ( BBB, JJJ )
81 * / \
82 * DDD EEE DDD: [ ] ( BBB ) EEE: [ JJJ ] ()
83 * |
84 * FFF FFF: [ ] ( BBB )
85 * / \
86 * GGG HHH GGG: [ ] ( BBB ) HHH: [ BBB ] ()
87 * |
88 * III III: [ BBB ] ()
89 *
90 *
91 * BBB and JJJ are definitely the dominance frontier.
92 * Where do I place phi functions and how do I make that decision.
93 *
94 */
95static void die(char *fmt, ...)
96{
97 va_list args;
98
99 va_start(args, fmt);
100 vfprintf(stderr, fmt, args);
101 va_end(args);
102 fflush(stdout);
103 fflush(stderr);
104 exit(1);
105}
106
107#define MALLOC_STRONG_DEBUG
108static void *xmalloc(size_t size, const char *name)
109{
110 void *buf;
111 buf = malloc(size);
112 if (!buf) {
113 die("Cannot malloc %ld bytes to hold %s: %s\n",
114 size + 0UL, name, strerror(errno));
115 }
116 return buf;
117}
118
119static void *xcmalloc(size_t size, const char *name)
120{
121 void *buf;
122 buf = xmalloc(size, name);
123 memset(buf, 0, size);
124 return buf;
125}
126
127static void xfree(const void *ptr)
128{
129 free((void *)ptr);
130}
131
132static char *xstrdup(const char *str)
133{
134 char *new;
135 int len;
136 len = strlen(str);
137 new = xmalloc(len + 1, "xstrdup string");
138 memcpy(new, str, len);
139 new[len] = '\0';
140 return new;
141}
142
143static void xchdir(const char *path)
144{
145 if (chdir(path) != 0) {
146 die("chdir to %s failed: %s\n",
147 path, strerror(errno));
148 }
149}
150
151static int exists(const char *dirname, const char *filename)
152{
153 int does_exist = 1;
154 xchdir(dirname);
155 if (access(filename, O_RDONLY) < 0) {
156 if ((errno != EACCES) && (errno != EROFS)) {
157 does_exist = 0;
158 }
159 }
160 return does_exist;
161}
162
163
164static char *slurp_file(const char *dirname, const char *filename, off_t *r_size)
165{
166 int fd;
167 char *buf;
168 off_t size, progress;
169 ssize_t result;
170 struct stat stats;
171
172 if (!filename) {
173 *r_size = 0;
174 return 0;
175 }
176 xchdir(dirname);
177 fd = open(filename, O_RDONLY);
178 if (fd < 0) {
179 die("Cannot open '%s' : %s\n",
180 filename, strerror(errno));
181 }
182 result = fstat(fd, &stats);
183 if (result < 0) {
184 die("Cannot stat: %s: %s\n",
185 filename, strerror(errno));
186 }
187 size = stats.st_size;
188 *r_size = size +1;
189 buf = xmalloc(size +2, filename);
190 buf[size] = '\n'; /* Make certain the file is newline terminated */
191 buf[size+1] = '\0'; /* Null terminate the file for good measure */
192 progress = 0;
193 while(progress < size) {
194 result = read(fd, buf + progress, size - progress);
195 if (result < 0) {
196 if ((errno == EINTR) || (errno == EAGAIN))
197 continue;
198 die("read on %s of %ld bytes failed: %s\n",
199 filename, (size - progress)+ 0UL, strerror(errno));
200 }
201 progress += result;
202 }
203 result = close(fd);
204 if (result < 0) {
205 die("Close of %s failed: %s\n",
206 filename, strerror(errno));
207 }
208 return buf;
209}
210
211/* Long on the destination platform */
212typedef unsigned long ulong_t;
213typedef long long_t;
214
215struct file_state {
216 struct file_state *prev;
217 const char *basename;
218 char *dirname;
219 char *buf;
220 off_t size;
221 char *pos;
222 int line;
223 char *line_start;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000224 int report_line;
225 const char *report_name;
226 const char *report_dir;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000227};
228struct hash_entry;
229struct token {
230 int tok;
231 struct hash_entry *ident;
232 int str_len;
233 union {
234 ulong_t integer;
235 const char *str;
236 } val;
237};
238
239/* I have two classes of types:
240 * Operational types.
241 * Logical types. (The type the C standard says the operation is of)
242 *
243 * The operational types are:
244 * chars
245 * shorts
246 * ints
247 * longs
248 *
249 * floats
250 * doubles
251 * long doubles
252 *
253 * pointer
254 */
255
256
257/* Machine model.
258 * No memory is useable by the compiler.
259 * There is no floating point support.
260 * All operations take place in general purpose registers.
261 * There is one type of general purpose register.
262 * Unsigned longs are stored in that general purpose register.
263 */
264
265/* Operations on general purpose registers.
266 */
267
Eric Biederman530b5192003-07-01 10:05:30 +0000268#define OP_SDIVT 0
269#define OP_UDIVT 1
270#define OP_SMUL 2
271#define OP_UMUL 3
272#define OP_SDIV 4
273#define OP_UDIV 5
274#define OP_SMOD 6
275#define OP_UMOD 7
276#define OP_ADD 8
277#define OP_SUB 9
278#define OP_SL 10
279#define OP_USR 11
280#define OP_SSR 12
281#define OP_AND 13
282#define OP_XOR 14
283#define OP_OR 15
284#define OP_POS 16 /* Dummy positive operator don't use it */
285#define OP_NEG 17
286#define OP_INVERT 18
Eric Biedermanb138ac82003-04-22 18:44:01 +0000287
288#define OP_EQ 20
289#define OP_NOTEQ 21
290#define OP_SLESS 22
291#define OP_ULESS 23
292#define OP_SMORE 24
293#define OP_UMORE 25
294#define OP_SLESSEQ 26
295#define OP_ULESSEQ 27
296#define OP_SMOREEQ 28
297#define OP_UMOREEQ 29
298
299#define OP_LFALSE 30 /* Test if the expression is logically false */
300#define OP_LTRUE 31 /* Test if the expression is logcially true */
301
302#define OP_LOAD 32
303#define OP_STORE 33
Eric Biederman530b5192003-07-01 10:05:30 +0000304/* For OP_STORE ->type holds the type
305 * RHS(0) holds the destination address
306 * RHS(1) holds the value to store.
307 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000308
309#define OP_NOOP 34
310
311#define OP_MIN_CONST 50
312#define OP_MAX_CONST 59
313#define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
314#define OP_INTCONST 50
Eric Biedermand1ea5392003-06-28 06:49:45 +0000315/* For OP_INTCONST ->type holds the type.
316 * ->u.cval holds the constant value.
317 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000318#define OP_BLOBCONST 51
Eric Biederman0babc1c2003-05-09 02:39:00 +0000319/* For OP_BLOBCONST ->type holds the layout and size
Eric Biedermanb138ac82003-04-22 18:44:01 +0000320 * information. u.blob holds a pointer to the raw binary
321 * data for the constant initializer.
322 */
323#define OP_ADDRCONST 52
Eric Biederman0babc1c2003-05-09 02:39:00 +0000324/* For OP_ADDRCONST ->type holds the type.
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000325 * MISC(0) holds the reference to the static variable.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000326 * ->u.cval holds an offset from that value.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000327 */
328
329#define OP_WRITE 60
330/* OP_WRITE moves one pseudo register to another.
Eric Biederman530b5192003-07-01 10:05:30 +0000331 * RHS(0) holds the destination pseudo register, which must be an OP_DECL.
332 * RHS(1) holds the psuedo to move.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000333 */
334
335#define OP_READ 61
336/* OP_READ reads the value of a variable and makes
337 * it available for the pseudo operation.
338 * Useful for things like def-use chains.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000339 * RHS(0) holds points to the triple to read from.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000340 */
341#define OP_COPY 62
Eric Biederman0babc1c2003-05-09 02:39:00 +0000342/* OP_COPY makes a copy of the psedo register or constant in RHS(0).
343 */
344#define OP_PIECE 63
345/* OP_PIECE returns one piece of a instruction that returns a structure.
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000346 * MISC(0) is the instruction
Eric Biederman0babc1c2003-05-09 02:39:00 +0000347 * u.cval is the LHS piece of the instruction to return.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000348 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000349#define OP_ASM 64
350/* OP_ASM holds a sequence of assembly instructions, the result
351 * of a C asm directive.
352 * RHS(x) holds input value x to the assembly sequence.
353 * LHS(x) holds the output value x from the assembly sequence.
354 * u.blob holds the string of assembly instructions.
355 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000356
Eric Biedermanb138ac82003-04-22 18:44:01 +0000357#define OP_DEREF 65
358/* OP_DEREF generates an lvalue from a pointer.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000359 * RHS(0) holds the pointer value.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000360 * OP_DEREF serves as a place holder to indicate all necessary
361 * checks have been done to indicate a value is an lvalue.
362 */
363#define OP_DOT 66
Eric Biederman0babc1c2003-05-09 02:39:00 +0000364/* OP_DOT references a submember of a structure lvalue.
365 * RHS(0) holds the lvalue.
366 * ->u.field holds the name of the field we want.
367 *
368 * Not seen outside of expressions.
369 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000370#define OP_VAL 67
371/* OP_VAL returns the value of a subexpression of the current expression.
372 * Useful for operators that have side effects.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000373 * RHS(0) holds the expression.
374 * MISC(0) holds the subexpression of RHS(0) that is the
Eric Biedermanb138ac82003-04-22 18:44:01 +0000375 * value of the expression.
376 *
377 * Not seen outside of expressions.
378 */
379#define OP_LAND 68
Eric Biederman0babc1c2003-05-09 02:39:00 +0000380/* OP_LAND performs a C logical and between RHS(0) and RHS(1).
Eric Biedermanb138ac82003-04-22 18:44:01 +0000381 * Not seen outside of expressions.
382 */
383#define OP_LOR 69
Eric Biederman0babc1c2003-05-09 02:39:00 +0000384/* OP_LOR performs a C logical or between RHS(0) and RHS(1).
Eric Biedermanb138ac82003-04-22 18:44:01 +0000385 * Not seen outside of expressions.
386 */
387#define OP_COND 70
388/* OP_CODE performas a C ? : operation.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000389 * RHS(0) holds the test.
390 * RHS(1) holds the expression to evaluate if the test returns true.
391 * RHS(2) holds the expression to evaluate if the test returns false.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000392 * Not seen outside of expressions.
393 */
394#define OP_COMMA 71
395/* OP_COMMA performacs a C comma operation.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000396 * That is RHS(0) is evaluated, then RHS(1)
397 * and the value of RHS(1) is returned.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000398 * Not seen outside of expressions.
399 */
400
401#define OP_CALL 72
402/* OP_CALL performs a procedure call.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000403 * MISC(0) holds a pointer to the OP_LIST of a function
404 * RHS(x) holds argument x of a function
405 *
Eric Biedermanb138ac82003-04-22 18:44:01 +0000406 * Currently not seen outside of expressions.
407 */
Eric Biederman0babc1c2003-05-09 02:39:00 +0000408#define OP_VAL_VEC 74
409/* OP_VAL_VEC is an array of triples that are either variable
410 * or values for a structure or an array.
411 * RHS(x) holds element x of the vector.
412 * triple->type->elements holds the size of the vector.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000413 */
414
415/* statements */
416#define OP_LIST 80
417/* OP_LIST Holds a list of statements, and a result value.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000418 * RHS(0) holds the list of statements.
419 * MISC(0) holds the value of the statements.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000420 */
421
422#define OP_BRANCH 81 /* branch */
423/* For branch instructions
Eric Biederman0babc1c2003-05-09 02:39:00 +0000424 * TARG(0) holds the branch target.
425 * RHS(0) if present holds the branch condition.
426 * ->next holds where to branch to if the branch is not taken.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000427 * The branch target can only be a decl...
428 */
429
430#define OP_LABEL 83
431/* OP_LABEL is a triple that establishes an target for branches.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000432 * ->use is the list of all branches that use this label.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000433 */
434
435#define OP_ADECL 84
436/* OP_DECL is a triple that establishes an lvalue for assignments.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000437 * ->use is a list of statements that use the variable.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000438 */
439
440#define OP_SDECL 85
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000441/* OP_SDECL is a triple that establishes a variable of static
Eric Biedermanb138ac82003-04-22 18:44:01 +0000442 * storage duration.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000443 * ->use is a list of statements that use the variable.
444 * MISC(0) holds the initializer expression.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000445 */
446
447
448#define OP_PHI 86
449/* OP_PHI is a triple used in SSA form code.
450 * It is used when multiple code paths merge and a variable needs
451 * a single assignment from any of those code paths.
452 * The operation is a cross between OP_DECL and OP_WRITE, which
453 * is what OP_PHI is geneared from.
454 *
Eric Biederman0babc1c2003-05-09 02:39:00 +0000455 * RHS(x) points to the value from code path x
456 * The number of RHS entries is the number of control paths into the block
Eric Biedermanb138ac82003-04-22 18:44:01 +0000457 * in which OP_PHI resides. The elements of the array point to point
458 * to the variables OP_PHI is derived from.
459 *
Eric Biederman0babc1c2003-05-09 02:39:00 +0000460 * MISC(0) holds a pointer to the orginal OP_DECL node.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000461 */
462
463/* Architecture specific instructions */
464#define OP_CMP 100
465#define OP_TEST 101
466#define OP_SET_EQ 102
467#define OP_SET_NOTEQ 103
468#define OP_SET_SLESS 104
469#define OP_SET_ULESS 105
470#define OP_SET_SMORE 106
471#define OP_SET_UMORE 107
472#define OP_SET_SLESSEQ 108
473#define OP_SET_ULESSEQ 109
474#define OP_SET_SMOREEQ 110
475#define OP_SET_UMOREEQ 111
476
477#define OP_JMP 112
478#define OP_JMP_EQ 113
479#define OP_JMP_NOTEQ 114
480#define OP_JMP_SLESS 115
481#define OP_JMP_ULESS 116
482#define OP_JMP_SMORE 117
483#define OP_JMP_UMORE 118
484#define OP_JMP_SLESSEQ 119
485#define OP_JMP_ULESSEQ 120
486#define OP_JMP_SMOREEQ 121
487#define OP_JMP_UMOREEQ 122
488
489/* Builtin operators that it is just simpler to use the compiler for */
490#define OP_INB 130
491#define OP_INW 131
492#define OP_INL 132
493#define OP_OUTB 133
494#define OP_OUTW 134
495#define OP_OUTL 135
496#define OP_BSF 136
497#define OP_BSR 137
Eric Biedermanb138ac82003-04-22 18:44:01 +0000498#define OP_RDMSR 138
499#define OP_WRMSR 139
Eric Biedermanb138ac82003-04-22 18:44:01 +0000500#define OP_HLT 140
501
Eric Biederman0babc1c2003-05-09 02:39:00 +0000502struct op_info {
503 const char *name;
504 unsigned flags;
505#define PURE 1
506#define IMPURE 2
507#define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
508#define DEF 4
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000509#define BLOCK 8 /* Triple stores the current block */
Eric Biederman0babc1c2003-05-09 02:39:00 +0000510 unsigned char lhs, rhs, misc, targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000511};
512
Eric Biederman0babc1c2003-05-09 02:39:00 +0000513#define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \
514 .name = (NAME), \
515 .flags = (FLAGS), \
516 .lhs = (LHS), \
517 .rhs = (RHS), \
518 .misc = (MISC), \
519 .targ = (TARG), \
520 }
521static const struct op_info table_ops[] = {
Eric Biederman530b5192003-07-01 10:05:30 +0000522[OP_SDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "sdivt"),
523[OP_UDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "udivt"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000524[OP_SMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smul"),
525[OP_UMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umul"),
526[OP_SDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sdiv"),
527[OP_UDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "udiv"),
528[OP_SMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smod"),
529[OP_UMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umod"),
530[OP_ADD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "add"),
531[OP_SUB ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sub"),
532[OP_SL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sl"),
533[OP_USR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "usr"),
534[OP_SSR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ssr"),
535[OP_AND ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "and"),
536[OP_XOR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "xor"),
537[OP_OR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "or"),
538[OP_POS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "pos"),
539[OP_NEG ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "neg"),
540[OP_INVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "invert"),
Eric Biedermanb138ac82003-04-22 18:44:01 +0000541
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000542[OP_EQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "eq"),
543[OP_NOTEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "noteq"),
544[OP_SLESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sless"),
545[OP_ULESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "uless"),
546[OP_SMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smore"),
547[OP_UMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umore"),
548[OP_SLESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "slesseq"),
549[OP_ULESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ulesseq"),
550[OP_SMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smoreeq"),
551[OP_UMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umoreeq"),
552[OP_LFALSE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "lfalse"),
553[OP_LTRUE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "ltrue"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000554
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000555[OP_LOAD ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "load"),
Eric Biederman530b5192003-07-01 10:05:30 +0000556[OP_STORE ] = OP( 0, 2, 0, 0, IMPURE | BLOCK , "store"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000557
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000558[OP_NOOP ] = OP( 0, 0, 0, 0, PURE | BLOCK, "noop"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000559
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000560[OP_INTCONST ] = OP( 0, 0, 0, 0, PURE | DEF, "intconst"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000561[OP_BLOBCONST ] = OP( 0, 0, 0, 0, PURE, "blobconst"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000562[OP_ADDRCONST ] = OP( 0, 0, 1, 0, PURE | DEF, "addrconst"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000563
Eric Biederman530b5192003-07-01 10:05:30 +0000564[OP_WRITE ] = OP( 0, 2, 0, 0, PURE | BLOCK, "write"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000565[OP_READ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "read"),
566[OP_COPY ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "copy"),
567[OP_PIECE ] = OP( 0, 0, 1, 0, PURE | DEF, "piece"),
568[OP_ASM ] = OP(-1, -1, 0, 0, IMPURE, "asm"),
569[OP_DEREF ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "deref"),
570[OP_DOT ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "dot"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000571
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000572[OP_VAL ] = OP( 0, 1, 1, 0, 0 | DEF | BLOCK, "val"),
573[OP_LAND ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "land"),
574[OP_LOR ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "lor"),
575[OP_COND ] = OP( 0, 3, 0, 0, 0 | DEF | BLOCK, "cond"),
576[OP_COMMA ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "comma"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000577/* Call is special most it can stand in for anything so it depends on context */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000578[OP_CALL ] = OP(-1, -1, 1, 0, 0 | BLOCK, "call"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000579/* The sizes of OP_CALL and OP_VAL_VEC depend upon context */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000580[OP_VAL_VEC ] = OP( 0, -1, 0, 0, 0 | BLOCK, "valvec"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000581
582[OP_LIST ] = OP( 0, 1, 1, 0, 0 | DEF, "list"),
583/* The number of targets for OP_BRANCH depends on context */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000584[OP_BRANCH ] = OP( 0, -1, 0, 1, PURE | BLOCK, "branch"),
585[OP_LABEL ] = OP( 0, 0, 0, 0, PURE | BLOCK, "label"),
586[OP_ADECL ] = OP( 0, 0, 0, 0, PURE | BLOCK, "adecl"),
587[OP_SDECL ] = OP( 0, 0, 1, 0, PURE | BLOCK, "sdecl"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000588/* The number of RHS elements of OP_PHI depend upon context */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000589[OP_PHI ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000590
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000591[OP_CMP ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK, "cmp"),
592[OP_TEST ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "test"),
593[OP_SET_EQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_eq"),
594[OP_SET_NOTEQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_noteq"),
595[OP_SET_SLESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_sless"),
596[OP_SET_ULESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_uless"),
597[OP_SET_SMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smore"),
598[OP_SET_UMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umore"),
599[OP_SET_SLESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_slesseq"),
600[OP_SET_ULESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_ulesseq"),
601[OP_SET_SMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smoreq"),
602[OP_SET_UMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umoreq"),
603[OP_JMP ] = OP( 0, 0, 0, 1, PURE | BLOCK, "jmp"),
604[OP_JMP_EQ ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_eq"),
605[OP_JMP_NOTEQ ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_noteq"),
606[OP_JMP_SLESS ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_sless"),
607[OP_JMP_ULESS ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_uless"),
608[OP_JMP_SMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_smore"),
609[OP_JMP_UMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_umore"),
610[OP_JMP_SLESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_slesseq"),
611[OP_JMP_ULESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_ulesseq"),
612[OP_JMP_SMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_smoreq"),
613[OP_JMP_UMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_umoreq"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000614
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000615[OP_INB ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inb"),
616[OP_INW ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inw"),
617[OP_INL ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inl"),
618[OP_OUTB ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outb"),
619[OP_OUTW ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outw"),
620[OP_OUTL ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outl"),
621[OP_BSF ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsf"),
622[OP_BSR ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsr"),
623[OP_RDMSR ] = OP( 2, 1, 0, 0, IMPURE | BLOCK, "__rdmsr"),
624[OP_WRMSR ] = OP( 0, 3, 0, 0, IMPURE | BLOCK, "__wrmsr"),
625[OP_HLT ] = OP( 0, 0, 0, 0, IMPURE | BLOCK, "__hlt"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000626};
627#undef OP
628#define OP_MAX (sizeof(table_ops)/sizeof(table_ops[0]))
Eric Biedermanb138ac82003-04-22 18:44:01 +0000629
630static const char *tops(int index)
631{
632 static const char unknown[] = "unknown op";
633 if (index < 0) {
634 return unknown;
635 }
636 if (index > OP_MAX) {
637 return unknown;
638 }
Eric Biederman0babc1c2003-05-09 02:39:00 +0000639 return table_ops[index].name;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000640}
641
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000642struct asm_info;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000643struct triple;
644struct block;
645struct triple_set {
646 struct triple_set *next;
647 struct triple *member;
648};
649
Eric Biederman0babc1c2003-05-09 02:39:00 +0000650#define MAX_LHS 15
Eric Biederman678d8162003-07-03 03:59:38 +0000651#define MAX_RHS 250
652#define MAX_MISC 3
653#define MAX_TARG 3
Eric Biederman0babc1c2003-05-09 02:39:00 +0000654
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000655struct occurance {
656 int count;
657 const char *filename;
658 const char *function;
659 int line;
660 int col;
661 struct occurance *parent;
662};
Eric Biedermanb138ac82003-04-22 18:44:01 +0000663struct triple {
664 struct triple *next, *prev;
665 struct triple_set *use;
666 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000667 unsigned char op;
668 unsigned char template_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000669 unsigned short sizes;
670#define TRIPLE_LHS(SIZES) (((SIZES) >> 0) & 0x0f)
Eric Biederman678d8162003-07-03 03:59:38 +0000671#define TRIPLE_RHS(SIZES) (((SIZES) >> 4) & 0xff)
672#define TRIPLE_MISC(SIZES) (((SIZES) >> 12) & 0x03)
673#define TRIPLE_TARG(SIZES) (((SIZES) >> 14) & 0x03)
Eric Biederman0babc1c2003-05-09 02:39:00 +0000674#define TRIPLE_SIZE(SIZES) \
Eric Biederman678d8162003-07-03 03:59:38 +0000675 (TRIPLE_LHS(SIZES) + \
676 TRIPLE_RHS(SIZES) + \
677 TRIPLE_MISC(SIZES) + \
678 TRIPLE_TARG(SIZES))
Eric Biederman0babc1c2003-05-09 02:39:00 +0000679#define TRIPLE_SIZES(LHS, RHS, MISC, TARG) \
680 ((((LHS) & 0x0f) << 0) | \
Eric Biederman678d8162003-07-03 03:59:38 +0000681 (((RHS) & 0xff) << 4) | \
682 (((MISC) & 0x03) << 12) | \
683 (((TARG) & 0x03) << 14))
Eric Biederman0babc1c2003-05-09 02:39:00 +0000684#define TRIPLE_LHS_OFF(SIZES) (0)
685#define TRIPLE_RHS_OFF(SIZES) (TRIPLE_LHS_OFF(SIZES) + TRIPLE_LHS(SIZES))
686#define TRIPLE_MISC_OFF(SIZES) (TRIPLE_RHS_OFF(SIZES) + TRIPLE_RHS(SIZES))
687#define TRIPLE_TARG_OFF(SIZES) (TRIPLE_MISC_OFF(SIZES) + TRIPLE_MISC(SIZES))
688#define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF((PTR)->sizes) + (INDEX)])
689#define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF((PTR)->sizes) + (INDEX)])
690#define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF((PTR)->sizes) + (INDEX)])
691#define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF((PTR)->sizes) + (INDEX)])
Eric Biedermanb138ac82003-04-22 18:44:01 +0000692 unsigned id; /* A scratch value and finally the register */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000693#define TRIPLE_FLAG_FLATTENED (1 << 31)
694#define TRIPLE_FLAG_PRE_SPLIT (1 << 30)
695#define TRIPLE_FLAG_POST_SPLIT (1 << 29)
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000696 struct occurance *occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000697 union {
698 ulong_t cval;
699 struct block *block;
700 void *blob;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000701 struct hash_entry *field;
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000702 struct asm_info *ainfo;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000703 } u;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000704 struct triple *param[2];
Eric Biedermanb138ac82003-04-22 18:44:01 +0000705};
706
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000707struct reg_info {
708 unsigned reg;
709 unsigned regcm;
710};
711struct ins_template {
712 struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
713};
714
715struct asm_info {
716 struct ins_template tmpl;
717 char *str;
718};
719
Eric Biedermanb138ac82003-04-22 18:44:01 +0000720struct block_set {
721 struct block_set *next;
722 struct block *member;
723};
724struct block {
725 struct block *work_next;
726 struct block *left, *right;
727 struct triple *first, *last;
728 int users;
729 struct block_set *use;
730 struct block_set *idominates;
731 struct block_set *domfrontier;
732 struct block *idom;
733 struct block_set *ipdominates;
734 struct block_set *ipdomfrontier;
735 struct block *ipdom;
736 int vertex;
737
738};
739
740struct symbol {
741 struct symbol *next;
742 struct hash_entry *ident;
743 struct triple *def;
744 struct type *type;
745 int scope_depth;
746};
747
748struct macro {
749 struct hash_entry *ident;
750 char *buf;
751 int buf_len;
752};
753
754struct hash_entry {
755 struct hash_entry *next;
756 const char *name;
757 int name_len;
758 int tok;
759 struct macro *sym_define;
760 struct symbol *sym_label;
761 struct symbol *sym_struct;
762 struct symbol *sym_ident;
763};
764
765#define HASH_TABLE_SIZE 2048
766
767struct compile_state {
Eric Biederman05f26fc2003-06-11 21:55:00 +0000768 const char *label_prefix;
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000769 const char *ofilename;
770 FILE *output;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000771 struct file_state *file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000772 struct occurance *last_occurance;
773 const char *function;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000774 struct token token[4];
775 struct hash_entry *hash_table[HASH_TABLE_SIZE];
776 struct hash_entry *i_continue;
777 struct hash_entry *i_break;
778 int scope_depth;
779 int if_depth, if_value;
780 int macro_line;
781 struct file_state *macro_file;
782 struct triple *main_function;
783 struct block *first_block, *last_block;
784 int last_vertex;
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000785 int cpu;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000786 int debug;
787 int optimize;
788};
789
Eric Biederman0babc1c2003-05-09 02:39:00 +0000790/* visibility global/local */
791/* static/auto duration */
792/* typedef, register, inline */
793#define STOR_SHIFT 0
794#define STOR_MASK 0x000f
795/* Visibility */
796#define STOR_GLOBAL 0x0001
797/* Duration */
798#define STOR_PERM 0x0002
799/* Storage specifiers */
800#define STOR_AUTO 0x0000
801#define STOR_STATIC 0x0002
802#define STOR_EXTERN 0x0003
803#define STOR_REGISTER 0x0004
804#define STOR_TYPEDEF 0x0008
805#define STOR_INLINE 0x000c
806
807#define QUAL_SHIFT 4
808#define QUAL_MASK 0x0070
809#define QUAL_NONE 0x0000
810#define QUAL_CONST 0x0010
811#define QUAL_VOLATILE 0x0020
812#define QUAL_RESTRICT 0x0040
813
814#define TYPE_SHIFT 8
815#define TYPE_MASK 0x1f00
816#define TYPE_INTEGER(TYPE) (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG))
817#define TYPE_ARITHMETIC(TYPE) (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE))
818#define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
819#define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
820#define TYPE_MKUNSIGNED(TYPE) ((TYPE) | 0x0100)
821#define TYPE_RANK(TYPE) ((TYPE) & ~0x0100)
822#define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER)
823#define TYPE_DEFAULT 0x0000
824#define TYPE_VOID 0x0100
825#define TYPE_CHAR 0x0200
826#define TYPE_UCHAR 0x0300
827#define TYPE_SHORT 0x0400
828#define TYPE_USHORT 0x0500
829#define TYPE_INT 0x0600
830#define TYPE_UINT 0x0700
831#define TYPE_LONG 0x0800
832#define TYPE_ULONG 0x0900
833#define TYPE_LLONG 0x0a00 /* long long */
834#define TYPE_ULLONG 0x0b00
835#define TYPE_FLOAT 0x0c00
836#define TYPE_DOUBLE 0x0d00
837#define TYPE_LDOUBLE 0x0e00 /* long double */
838#define TYPE_STRUCT 0x1000
839#define TYPE_ENUM 0x1100
840#define TYPE_POINTER 0x1200
841/* For TYPE_POINTER:
842 * type->left holds the type pointed to.
843 */
844#define TYPE_FUNCTION 0x1300
845/* For TYPE_FUNCTION:
846 * type->left holds the return type.
847 * type->right holds the...
848 */
849#define TYPE_PRODUCT 0x1400
850/* TYPE_PRODUCT is a basic building block when defining structures
851 * type->left holds the type that appears first in memory.
852 * type->right holds the type that appears next in memory.
853 */
854#define TYPE_OVERLAP 0x1500
855/* TYPE_OVERLAP is a basic building block when defining unions
856 * type->left and type->right holds to types that overlap
857 * each other in memory.
858 */
859#define TYPE_ARRAY 0x1600
860/* TYPE_ARRAY is a basic building block when definitng arrays.
861 * type->left holds the type we are an array of.
862 * type-> holds the number of elements.
863 */
864
865#define ELEMENT_COUNT_UNSPECIFIED (~0UL)
866
867struct type {
868 unsigned int type;
869 struct type *left, *right;
870 ulong_t elements;
871 struct hash_entry *field_ident;
872 struct hash_entry *type_ident;
873};
874
Eric Biedermanb138ac82003-04-22 18:44:01 +0000875#define MAX_REGISTERS 75
876#define MAX_REG_EQUIVS 16
Eric Biedermanf96a8102003-06-16 16:57:34 +0000877#define REGISTER_BITS 16
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000878#define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
Eric Biederman530b5192003-07-01 10:05:30 +0000879#define TEMPLATE_BITS 7
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000880#define MAX_TEMPLATES (1<<TEMPLATE_BITS)
Eric Biederman530b5192003-07-01 10:05:30 +0000881#define MAX_REGC 14
Eric Biedermanb138ac82003-04-22 18:44:01 +0000882#define REG_UNSET 0
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000883#define REG_UNNEEDED 1
884#define REG_VIRT0 (MAX_REGISTERS + 0)
885#define REG_VIRT1 (MAX_REGISTERS + 1)
886#define REG_VIRT2 (MAX_REGISTERS + 2)
887#define REG_VIRT3 (MAX_REGISTERS + 3)
888#define REG_VIRT4 (MAX_REGISTERS + 4)
889#define REG_VIRT5 (MAX_REGISTERS + 5)
Eric Biederman8d9c1232003-06-17 08:42:17 +0000890#define REG_VIRT6 (MAX_REGISTERS + 5)
891#define REG_VIRT7 (MAX_REGISTERS + 5)
892#define REG_VIRT8 (MAX_REGISTERS + 5)
893#define REG_VIRT9 (MAX_REGISTERS + 5)
Eric Biedermanb138ac82003-04-22 18:44:01 +0000894
895/* Provision for 8 register classes */
Eric Biedermanf96a8102003-06-16 16:57:34 +0000896#define REG_SHIFT 0
897#define REGC_SHIFT REGISTER_BITS
898#define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000899#define REG_MASK (MAX_VIRT_REGISTERS -1)
900#define ID_REG(ID) ((ID) & REG_MASK)
901#define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
Eric Biedermanf96a8102003-06-16 16:57:34 +0000902#define ID_REGCM(ID) (((ID) & REGC_MASK) >> REGC_SHIFT)
903#define SET_REGCM(ID, REGCM) ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
904#define SET_INFO(ID, INFO) ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
905 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
Eric Biedermanb138ac82003-04-22 18:44:01 +0000906
907static unsigned arch_reg_regcm(struct compile_state *state, int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000908static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermand1ea5392003-06-28 06:49:45 +0000909static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000910static void arch_reg_equivs(
911 struct compile_state *state, unsigned *equiv, int reg);
912static int arch_select_free_register(
913 struct compile_state *state, char *used, int classes);
914static unsigned arch_regc_size(struct compile_state *state, int class);
915static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
916static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
917static const char *arch_reg_str(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000918static struct reg_info arch_reg_constraint(
919 struct compile_state *state, struct type *type, const char *constraint);
920static struct reg_info arch_reg_clobber(
921 struct compile_state *state, const char *clobber);
922static struct reg_info arch_reg_lhs(struct compile_state *state,
923 struct triple *ins, int index);
924static struct reg_info arch_reg_rhs(struct compile_state *state,
925 struct triple *ins, int index);
926static struct triple *transform_to_arch_instruction(
927 struct compile_state *state, struct triple *ins);
928
929
Eric Biedermanb138ac82003-04-22 18:44:01 +0000930
Eric Biederman0babc1c2003-05-09 02:39:00 +0000931#define DEBUG_ABORT_ON_ERROR 0x0001
932#define DEBUG_INTERMEDIATE_CODE 0x0002
933#define DEBUG_CONTROL_FLOW 0x0004
934#define DEBUG_BASIC_BLOCKS 0x0008
935#define DEBUG_FDOMINATORS 0x0010
936#define DEBUG_RDOMINATORS 0x0020
937#define DEBUG_TRIPLES 0x0040
938#define DEBUG_INTERFERENCE 0x0080
939#define DEBUG_ARCH_CODE 0x0100
940#define DEBUG_CODE_ELIMINATION 0x0200
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000941#define DEBUG_INSERTED_COPIES 0x0400
Eric Biedermanb138ac82003-04-22 18:44:01 +0000942
Eric Biederman153ea352003-06-20 14:43:20 +0000943#define GLOBAL_SCOPE_DEPTH 1
944#define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
Eric Biedermanb138ac82003-04-22 18:44:01 +0000945
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000946static void compile_file(struct compile_state *old_state, const char *filename, int local);
947
948static void do_cleanup(struct compile_state *state)
949{
950 if (state->output) {
951 fclose(state->output);
952 unlink(state->ofilename);
953 }
954}
Eric Biedermanb138ac82003-04-22 18:44:01 +0000955
956static int get_col(struct file_state *file)
957{
958 int col;
959 char *ptr, *end;
960 ptr = file->line_start;
961 end = file->pos;
962 for(col = 0; ptr < end; ptr++) {
963 if (*ptr != '\t') {
964 col++;
965 }
966 else {
967 col = (col & ~7) + 8;
968 }
969 }
970 return col;
971}
972
973static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
974{
975 int col;
Eric Biederman530b5192003-07-01 10:05:30 +0000976 if (triple && triple->occurance) {
Eric Biederman00443072003-06-24 12:34:45 +0000977 struct occurance *spot;
978 spot = triple->occurance;
979 while(spot->parent) {
980 spot = spot->parent;
981 }
Eric Biedermanb138ac82003-04-22 18:44:01 +0000982 fprintf(fp, "%s:%d.%d: ",
Eric Biederman00443072003-06-24 12:34:45 +0000983 spot->filename, spot->line, spot->col);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000984 return;
985 }
986 if (!state->file) {
987 return;
988 }
989 col = get_col(state->file);
990 fprintf(fp, "%s:%d.%d: ",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000991 state->file->report_name, state->file->report_line, col);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000992}
993
994static void __internal_error(struct compile_state *state, struct triple *ptr,
995 char *fmt, ...)
996{
997 va_list args;
998 va_start(args, fmt);
999 loc(stderr, state, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001000 if (ptr) {
1001 fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
1002 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001003 fprintf(stderr, "Internal compiler error: ");
1004 vfprintf(stderr, fmt, args);
1005 fprintf(stderr, "\n");
1006 va_end(args);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001007 do_cleanup(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001008 abort();
1009}
1010
1011
1012static void __internal_warning(struct compile_state *state, struct triple *ptr,
1013 char *fmt, ...)
1014{
1015 va_list args;
1016 va_start(args, fmt);
1017 loc(stderr, state, ptr);
1018 fprintf(stderr, "Internal compiler warning: ");
1019 vfprintf(stderr, fmt, args);
1020 fprintf(stderr, "\n");
1021 va_end(args);
1022}
1023
1024
1025
1026static void __error(struct compile_state *state, struct triple *ptr,
1027 char *fmt, ...)
1028{
1029 va_list args;
1030 va_start(args, fmt);
1031 loc(stderr, state, ptr);
1032 vfprintf(stderr, fmt, args);
1033 va_end(args);
1034 fprintf(stderr, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001035 do_cleanup(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001036 if (state->debug & DEBUG_ABORT_ON_ERROR) {
1037 abort();
1038 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001039 exit(1);
1040}
1041
1042static void __warning(struct compile_state *state, struct triple *ptr,
1043 char *fmt, ...)
1044{
1045 va_list args;
1046 va_start(args, fmt);
1047 loc(stderr, state, ptr);
1048 fprintf(stderr, "warning: ");
1049 vfprintf(stderr, fmt, args);
1050 fprintf(stderr, "\n");
1051 va_end(args);
1052}
1053
1054#if DEBUG_ERROR_MESSAGES
1055# define internal_error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_error
1056# define internal_warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_warning
1057# define error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__error
1058# define warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__warning
1059#else
1060# define internal_error __internal_error
1061# define internal_warning __internal_warning
1062# define error __error
1063# define warning __warning
1064#endif
1065#define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1066
Eric Biederman0babc1c2003-05-09 02:39:00 +00001067static void valid_op(struct compile_state *state, int op)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001068{
1069 char *fmt = "invalid op: %d";
Eric Biederman0babc1c2003-05-09 02:39:00 +00001070 if (op >= OP_MAX) {
1071 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001072 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001073 if (op < 0) {
1074 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001075 }
1076}
1077
Eric Biederman0babc1c2003-05-09 02:39:00 +00001078static void valid_ins(struct compile_state *state, struct triple *ptr)
1079{
1080 valid_op(state, ptr->op);
1081}
1082
Eric Biedermanb138ac82003-04-22 18:44:01 +00001083static void process_trigraphs(struct compile_state *state)
1084{
1085 char *src, *dest, *end;
1086 struct file_state *file;
1087 file = state->file;
1088 src = dest = file->buf;
1089 end = file->buf + file->size;
1090 while((end - src) >= 3) {
1091 if ((src[0] == '?') && (src[1] == '?')) {
1092 int c = -1;
1093 switch(src[2]) {
1094 case '=': c = '#'; break;
1095 case '/': c = '\\'; break;
1096 case '\'': c = '^'; break;
1097 case '(': c = '['; break;
1098 case ')': c = ']'; break;
1099 case '!': c = '!'; break;
1100 case '<': c = '{'; break;
1101 case '>': c = '}'; break;
1102 case '-': c = '~'; break;
1103 }
1104 if (c != -1) {
1105 *dest++ = c;
1106 src += 3;
1107 }
1108 else {
1109 *dest++ = *src++;
1110 }
1111 }
1112 else {
1113 *dest++ = *src++;
1114 }
1115 }
1116 while(src != end) {
1117 *dest++ = *src++;
1118 }
1119 file->size = dest - file->buf;
1120}
1121
1122static void splice_lines(struct compile_state *state)
1123{
1124 char *src, *dest, *end;
1125 struct file_state *file;
1126 file = state->file;
1127 src = dest = file->buf;
1128 end = file->buf + file->size;
1129 while((end - src) >= 2) {
1130 if ((src[0] == '\\') && (src[1] == '\n')) {
1131 src += 2;
1132 }
1133 else {
1134 *dest++ = *src++;
1135 }
1136 }
1137 while(src != end) {
1138 *dest++ = *src++;
1139 }
1140 file->size = dest - file->buf;
1141}
1142
1143static struct type void_type;
1144static void use_triple(struct triple *used, struct triple *user)
1145{
1146 struct triple_set **ptr, *new;
1147 if (!used)
1148 return;
1149 if (!user)
1150 return;
1151 ptr = &used->use;
1152 while(*ptr) {
1153 if ((*ptr)->member == user) {
1154 return;
1155 }
1156 ptr = &(*ptr)->next;
1157 }
1158 /* Append new to the head of the list,
1159 * copy_func and rename_block_variables
1160 * depends on this.
1161 */
1162 new = xcmalloc(sizeof(*new), "triple_set");
1163 new->member = user;
1164 new->next = used->use;
1165 used->use = new;
1166}
1167
1168static void unuse_triple(struct triple *used, struct triple *unuser)
1169{
1170 struct triple_set *use, **ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001171 if (!used) {
1172 return;
1173 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001174 ptr = &used->use;
1175 while(*ptr) {
1176 use = *ptr;
1177 if (use->member == unuser) {
1178 *ptr = use->next;
1179 xfree(use);
1180 }
1181 else {
1182 ptr = &use->next;
1183 }
1184 }
1185}
1186
1187static void push_triple(struct triple *used, struct triple *user)
1188{
1189 struct triple_set *new;
1190 if (!used)
1191 return;
1192 if (!user)
1193 return;
1194 /* Append new to the head of the list,
1195 * it's the only sensible behavoir for a stack.
1196 */
1197 new = xcmalloc(sizeof(*new), "triple_set");
1198 new->member = user;
1199 new->next = used->use;
1200 used->use = new;
1201}
1202
1203static void pop_triple(struct triple *used, struct triple *unuser)
1204{
1205 struct triple_set *use, **ptr;
1206 ptr = &used->use;
1207 while(*ptr) {
1208 use = *ptr;
1209 if (use->member == unuser) {
1210 *ptr = use->next;
1211 xfree(use);
1212 /* Only free one occurance from the stack */
1213 return;
1214 }
1215 else {
1216 ptr = &use->next;
1217 }
1218 }
1219}
1220
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001221static void put_occurance(struct occurance *occurance)
1222{
1223 occurance->count -= 1;
1224 if (occurance->count <= 0) {
1225 if (occurance->parent) {
1226 put_occurance(occurance->parent);
1227 }
1228 xfree(occurance);
1229 }
1230}
1231
1232static void get_occurance(struct occurance *occurance)
1233{
1234 occurance->count += 1;
1235}
1236
1237
1238static struct occurance *new_occurance(struct compile_state *state)
1239{
1240 struct occurance *result, *last;
1241 const char *filename;
1242 const char *function;
1243 int line, col;
1244
1245 function = "";
1246 filename = 0;
1247 line = 0;
1248 col = 0;
1249 if (state->file) {
1250 filename = state->file->report_name;
1251 line = state->file->report_line;
1252 col = get_col(state->file);
1253 }
1254 if (state->function) {
1255 function = state->function;
1256 }
1257 last = state->last_occurance;
1258 if (last &&
1259 (last->col == col) &&
1260 (last->line == line) &&
1261 (last->function == function) &&
1262 (strcmp(last->filename, filename) == 0)) {
1263 get_occurance(last);
1264 return last;
1265 }
1266 if (last) {
1267 state->last_occurance = 0;
1268 put_occurance(last);
1269 }
1270 result = xmalloc(sizeof(*result), "occurance");
1271 result->count = 2;
1272 result->filename = filename;
1273 result->function = function;
1274 result->line = line;
1275 result->col = col;
1276 result->parent = 0;
1277 state->last_occurance = result;
1278 return result;
1279}
1280
1281static struct occurance *inline_occurance(struct compile_state *state,
1282 struct occurance *new, struct occurance *orig)
1283{
1284 struct occurance *result, *last;
1285 last = state->last_occurance;
1286 if (last &&
1287 (last->parent == orig) &&
1288 (last->col == new->col) &&
1289 (last->line == new->line) &&
1290 (last->function == new->function) &&
1291 (last->filename == new->filename)) {
1292 get_occurance(last);
1293 return last;
1294 }
1295 if (last) {
1296 state->last_occurance = 0;
1297 put_occurance(last);
1298 }
1299 get_occurance(orig);
1300 result = xmalloc(sizeof(*result), "occurance");
1301 result->count = 2;
1302 result->filename = new->filename;
1303 result->function = new->function;
1304 result->line = new->line;
1305 result->col = new->col;
1306 result->parent = orig;
1307 state->last_occurance = result;
1308 return result;
1309}
1310
1311
1312static struct occurance dummy_occurance = {
1313 .count = 2,
1314 .filename = __FILE__,
1315 .function = "",
1316 .line = __LINE__,
1317 .col = 0,
1318 .parent = 0,
1319};
Eric Biedermanb138ac82003-04-22 18:44:01 +00001320
1321/* The zero triple is used as a place holder when we are removing pointers
1322 * from a triple. Having allows certain sanity checks to pass even
1323 * when the original triple that was pointed to is gone.
1324 */
1325static struct triple zero_triple = {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001326 .next = &zero_triple,
1327 .prev = &zero_triple,
1328 .use = 0,
1329 .op = OP_INTCONST,
1330 .sizes = TRIPLE_SIZES(0, 0, 0, 0),
1331 .id = -1, /* An invalid id */
Eric Biedermanb138ac82003-04-22 18:44:01 +00001332 .u = { .cval = 0, },
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001333 .occurance = &dummy_occurance,
Eric Biederman0babc1c2003-05-09 02:39:00 +00001334 .param { [0] = 0, [1] = 0, },
Eric Biedermanb138ac82003-04-22 18:44:01 +00001335};
1336
Eric Biederman0babc1c2003-05-09 02:39:00 +00001337
1338static unsigned short triple_sizes(struct compile_state *state,
Eric Biederman678d8162003-07-03 03:59:38 +00001339 int op, struct type *type, int lhs_wanted, int rhs_wanted,
1340 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00001341{
1342 int lhs, rhs, misc, targ;
Eric Biederman678d8162003-07-03 03:59:38 +00001343 struct triple dummy;
1344 dummy.op = op;
1345 dummy.occurance = occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00001346 valid_op(state, op);
1347 lhs = table_ops[op].lhs;
1348 rhs = table_ops[op].rhs;
1349 misc = table_ops[op].misc;
1350 targ = table_ops[op].targ;
1351
1352
1353 if (op == OP_CALL) {
1354 struct type *param;
1355 rhs = 0;
1356 param = type->right;
1357 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
1358 rhs++;
1359 param = param->right;
1360 }
1361 if ((param->type & TYPE_MASK) != TYPE_VOID) {
1362 rhs++;
1363 }
1364 lhs = 0;
1365 if ((type->left->type & TYPE_MASK) == TYPE_STRUCT) {
1366 lhs = type->left->elements;
1367 }
1368 }
1369 else if (op == OP_VAL_VEC) {
1370 rhs = type->elements;
1371 }
1372 else if ((op == OP_BRANCH) || (op == OP_PHI)) {
1373 rhs = rhs_wanted;
1374 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001375 else if (op == OP_ASM) {
1376 rhs = rhs_wanted;
1377 lhs = lhs_wanted;
1378 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001379 if ((rhs < 0) || (rhs > MAX_RHS)) {
Eric Biederman678d8162003-07-03 03:59:38 +00001380 internal_error(state, &dummy, "bad rhs %d", rhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001381 }
1382 if ((lhs < 0) || (lhs > MAX_LHS)) {
Eric Biederman678d8162003-07-03 03:59:38 +00001383 internal_error(state, &dummy, "bad lhs");
Eric Biederman0babc1c2003-05-09 02:39:00 +00001384 }
1385 if ((misc < 0) || (misc > MAX_MISC)) {
Eric Biederman678d8162003-07-03 03:59:38 +00001386 internal_error(state, &dummy, "bad misc");
Eric Biederman0babc1c2003-05-09 02:39:00 +00001387 }
1388 if ((targ < 0) || (targ > MAX_TARG)) {
Eric Biederman678d8162003-07-03 03:59:38 +00001389 internal_error(state, &dummy, "bad targs");
Eric Biederman0babc1c2003-05-09 02:39:00 +00001390 }
1391 return TRIPLE_SIZES(lhs, rhs, misc, targ);
1392}
1393
1394static struct triple *alloc_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001395 int op, struct type *type, int lhs, int rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001396 struct occurance *occurance)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001397{
Eric Biederman0babc1c2003-05-09 02:39:00 +00001398 size_t size, sizes, extra_count, min_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001399 struct triple *ret;
Eric Biederman678d8162003-07-03 03:59:38 +00001400 sizes = triple_sizes(state, op, type, lhs, rhs, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001401
1402 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
1403 extra_count = TRIPLE_SIZE(sizes);
1404 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
1405
1406 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
1407 ret = xcmalloc(size, "tripple");
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001408 ret->op = op;
1409 ret->sizes = sizes;
1410 ret->type = type;
1411 ret->next = ret;
1412 ret->prev = ret;
1413 ret->occurance = occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001414 return ret;
1415}
1416
Eric Biederman0babc1c2003-05-09 02:39:00 +00001417struct triple *dup_triple(struct compile_state *state, struct triple *src)
1418{
1419 struct triple *dup;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001420 int src_lhs, src_rhs, src_size;
1421 src_lhs = TRIPLE_LHS(src->sizes);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001422 src_rhs = TRIPLE_RHS(src->sizes);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001423 src_size = TRIPLE_SIZE(src->sizes);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001424 get_occurance(src->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001425 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001426 src->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001427 memcpy(dup, src, sizeof(*src));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001428 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
Eric Biederman0babc1c2003-05-09 02:39:00 +00001429 return dup;
1430}
1431
1432static struct triple *new_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001433 int op, struct type *type, int lhs, int rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001434{
1435 struct triple *ret;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001436 struct occurance *occurance;
1437 occurance = new_occurance(state);
1438 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001439 return ret;
1440}
1441
1442static struct triple *build_triple(struct compile_state *state,
1443 int op, struct type *type, struct triple *left, struct triple *right,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001444 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00001445{
1446 struct triple *ret;
1447 size_t count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001448 ret = alloc_triple(state, op, type, -1, -1, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001449 count = TRIPLE_SIZE(ret->sizes);
1450 if (count > 0) {
1451 ret->param[0] = left;
1452 }
1453 if (count > 1) {
1454 ret->param[1] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001455 }
1456 return ret;
1457}
1458
Eric Biederman0babc1c2003-05-09 02:39:00 +00001459static struct triple *triple(struct compile_state *state,
1460 int op, struct type *type, struct triple *left, struct triple *right)
1461{
1462 struct triple *ret;
1463 size_t count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001464 ret = new_triple(state, op, type, -1, -1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001465 count = TRIPLE_SIZE(ret->sizes);
1466 if (count >= 1) {
1467 ret->param[0] = left;
1468 }
1469 if (count >= 2) {
1470 ret->param[1] = right;
1471 }
1472 return ret;
1473}
1474
1475static struct triple *branch(struct compile_state *state,
1476 struct triple *targ, struct triple *test)
1477{
1478 struct triple *ret;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001479 ret = new_triple(state, OP_BRANCH, &void_type, -1, test?1:0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001480 if (test) {
1481 RHS(ret, 0) = test;
1482 }
1483 TARG(ret, 0) = targ;
1484 /* record the branch target was used */
1485 if (!targ || (targ->op != OP_LABEL)) {
1486 internal_error(state, 0, "branch not to label");
1487 use_triple(targ, ret);
1488 }
1489 return ret;
1490}
1491
1492
Eric Biedermanb138ac82003-04-22 18:44:01 +00001493static void insert_triple(struct compile_state *state,
1494 struct triple *first, struct triple *ptr)
1495{
1496 if (ptr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001497 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00001498 internal_error(state, ptr, "expression already used");
1499 }
1500 ptr->next = first;
1501 ptr->prev = first->prev;
1502 ptr->prev->next = ptr;
1503 ptr->next->prev = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00001504 if ((ptr->prev->op == OP_BRANCH) &&
1505 TRIPLE_RHS(ptr->prev->sizes)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00001506 unuse_triple(first, ptr->prev);
1507 use_triple(ptr, ptr->prev);
1508 }
1509 }
1510}
1511
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001512static int triple_stores_block(struct compile_state *state, struct triple *ins)
1513{
1514 /* This function is used to determine if u.block
1515 * is utilized to store the current block number.
1516 */
1517 int stores_block;
1518 valid_ins(state, ins);
1519 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
1520 return stores_block;
1521}
1522
1523static struct block *block_of_triple(struct compile_state *state,
1524 struct triple *ins)
1525{
1526 struct triple *first;
1527 first = RHS(state->main_function, 0);
1528 while(ins != first && !triple_stores_block(state, ins)) {
1529 if (ins == ins->prev) {
1530 internal_error(state, 0, "ins == ins->prev?");
1531 }
1532 ins = ins->prev;
1533 }
1534 if (!triple_stores_block(state, ins)) {
1535 internal_error(state, ins, "Cannot find block");
1536 }
1537 return ins->u.block;
1538}
1539
Eric Biedermanb138ac82003-04-22 18:44:01 +00001540static struct triple *pre_triple(struct compile_state *state,
1541 struct triple *base,
1542 int op, struct type *type, struct triple *left, struct triple *right)
1543{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001544 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001545 struct triple *ret;
Eric Biedermand3283ec2003-06-18 11:03:18 +00001546 /* If I am an OP_PIECE jump to the real instruction */
1547 if (base->op == OP_PIECE) {
1548 base = MISC(base, 0);
1549 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001550 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001551 get_occurance(base->occurance);
1552 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001553 if (triple_stores_block(state, ret)) {
1554 ret->u.block = block;
1555 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001556 insert_triple(state, base, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001557 if (block->first == base) {
1558 block->first = ret;
1559 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001560 return ret;
1561}
1562
1563static struct triple *post_triple(struct compile_state *state,
1564 struct triple *base,
1565 int op, struct type *type, struct triple *left, struct triple *right)
1566{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001567 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001568 struct triple *ret;
Eric Biedermand3283ec2003-06-18 11:03:18 +00001569 int zlhs;
1570 /* If I am an OP_PIECE jump to the real instruction */
1571 if (base->op == OP_PIECE) {
1572 base = MISC(base, 0);
1573 }
1574 /* If I have a left hand side skip over it */
1575 zlhs = TRIPLE_LHS(base->sizes);
Eric Biederman530b5192003-07-01 10:05:30 +00001576 if (zlhs) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00001577 base = LHS(base, zlhs - 1);
1578 }
1579
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001580 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001581 get_occurance(base->occurance);
1582 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001583 if (triple_stores_block(state, ret)) {
1584 ret->u.block = block;
1585 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001586 insert_triple(state, base->next, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001587 if (block->last == base) {
1588 block->last = ret;
1589 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001590 return ret;
1591}
1592
1593static struct triple *label(struct compile_state *state)
1594{
1595 /* Labels don't get a type */
1596 struct triple *result;
1597 result = triple(state, OP_LABEL, &void_type, 0, 0);
1598 return result;
1599}
1600
Eric Biederman0babc1c2003-05-09 02:39:00 +00001601static void display_triple(FILE *fp, struct triple *ins)
1602{
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001603 struct occurance *ptr;
1604 const char *reg;
1605 char pre, post;
1606 pre = post = ' ';
1607 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
1608 pre = '^';
1609 }
1610 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
1611 post = 'v';
1612 }
1613 reg = arch_reg_str(ID_REG(ins->id));
Eric Biederman0babc1c2003-05-09 02:39:00 +00001614 if (ins->op == OP_INTCONST) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001615 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s <0x%08lx> ",
1616 ins, pre, post, reg, ins->template_id, tops(ins->op),
1617 ins->u.cval);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001618 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001619 else if (ins->op == OP_ADDRCONST) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001620 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s %-10p <0x%08lx>",
1621 ins, pre, post, reg, ins->template_id, tops(ins->op),
1622 MISC(ins, 0), ins->u.cval);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001623 }
1624 else {
1625 int i, count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001626 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s",
1627 ins, pre, post, reg, ins->template_id, tops(ins->op));
Eric Biederman0babc1c2003-05-09 02:39:00 +00001628 count = TRIPLE_SIZE(ins->sizes);
1629 for(i = 0; i < count; i++) {
1630 fprintf(fp, " %-10p", ins->param[i]);
1631 }
1632 for(; i < 2; i++) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00001633 fprintf(fp, " ");
Eric Biederman0babc1c2003-05-09 02:39:00 +00001634 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001635 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001636 fprintf(fp, " @");
1637 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
1638 fprintf(fp, " %s,%s:%d.%d",
1639 ptr->function,
1640 ptr->filename,
1641 ptr->line,
1642 ptr->col);
1643 }
1644 fprintf(fp, "\n");
Eric Biederman530b5192003-07-01 10:05:30 +00001645#if 0
1646 {
1647 struct triple_set *user;
1648 for(user = ptr->use; user; user = user->next) {
1649 fprintf(fp, "use: %p\n", user->member);
1650 }
1651 }
1652#endif
Eric Biederman0babc1c2003-05-09 02:39:00 +00001653 fflush(fp);
1654}
1655
Eric Biedermanb138ac82003-04-22 18:44:01 +00001656static int triple_is_pure(struct compile_state *state, struct triple *ins)
1657{
1658 /* Does the triple have no side effects.
1659 * I.e. Rexecuting the triple with the same arguments
1660 * gives the same value.
1661 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001662 unsigned pure;
1663 valid_ins(state, ins);
1664 pure = PURE_BITS(table_ops[ins->op].flags);
1665 if ((pure != PURE) && (pure != IMPURE)) {
1666 internal_error(state, 0, "Purity of %s not known\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +00001667 tops(ins->op));
Eric Biedermanb138ac82003-04-22 18:44:01 +00001668 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001669 return pure == PURE;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001670}
1671
Eric Biederman0babc1c2003-05-09 02:39:00 +00001672static int triple_is_branch(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001673{
1674 /* This function is used to determine which triples need
1675 * a register.
1676 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001677 int is_branch;
1678 valid_ins(state, ins);
1679 is_branch = (table_ops[ins->op].targ != 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001680 return is_branch;
1681}
1682
Eric Biederman530b5192003-07-01 10:05:30 +00001683static int triple_is_cond_branch(struct compile_state *state, struct triple *ins)
1684{
1685 /* A conditional branch has the condition argument as a single
1686 * RHS parameter.
1687 */
1688 return triple_is_branch(state, ins) &&
1689 (TRIPLE_RHS(ins->sizes) == 1);
1690}
1691
1692static int triple_is_uncond_branch(struct compile_state *state, struct triple *ins)
1693{
1694 /* A unconditional branch has no RHS parameters.
1695 */
1696 return triple_is_branch(state, ins) &&
1697 (TRIPLE_RHS(ins->sizes) == 0);
1698}
1699
Eric Biederman0babc1c2003-05-09 02:39:00 +00001700static int triple_is_def(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001701{
1702 /* This function is used to determine which triples need
1703 * a register.
1704 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001705 int is_def;
1706 valid_ins(state, ins);
1707 is_def = (table_ops[ins->op].flags & DEF) == DEF;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001708 return is_def;
1709}
1710
Eric Biederman0babc1c2003-05-09 02:39:00 +00001711static struct triple **triple_iter(struct compile_state *state,
1712 size_t count, struct triple **vector,
1713 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001714{
1715 struct triple **ret;
1716 ret = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00001717 if (count) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00001718 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001719 ret = vector;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001720 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001721 else if ((last >= vector) && (last < (vector + count - 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00001722 ret = last + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001723 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001724 }
1725 return ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00001726
Eric Biedermanb138ac82003-04-22 18:44:01 +00001727}
1728
1729static struct triple **triple_lhs(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00001730 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001731{
Eric Biederman0babc1c2003-05-09 02:39:00 +00001732 return triple_iter(state, TRIPLE_LHS(ins->sizes), &LHS(ins,0),
1733 ins, last);
1734}
1735
1736static struct triple **triple_rhs(struct compile_state *state,
1737 struct triple *ins, struct triple **last)
1738{
1739 return triple_iter(state, TRIPLE_RHS(ins->sizes), &RHS(ins,0),
1740 ins, last);
1741}
1742
1743static struct triple **triple_misc(struct compile_state *state,
1744 struct triple *ins, struct triple **last)
1745{
1746 return triple_iter(state, TRIPLE_MISC(ins->sizes), &MISC(ins,0),
1747 ins, last);
1748}
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001749
Eric Biederman0babc1c2003-05-09 02:39:00 +00001750static struct triple **triple_targ(struct compile_state *state,
1751 struct triple *ins, struct triple **last)
1752{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001753 size_t count;
1754 struct triple **ret, **vector;
1755 ret = 0;
1756 count = TRIPLE_TARG(ins->sizes);
1757 vector = &TARG(ins, 0);
1758 if (count) {
1759 if (!last) {
1760 ret = vector;
1761 }
1762 else if ((last >= vector) && (last < (vector + count - 1))) {
1763 ret = last + 1;
1764 }
1765 else if ((last == (vector + count - 1)) &&
1766 TRIPLE_RHS(ins->sizes)) {
1767 ret = &ins->next;
1768 }
1769 }
1770 return ret;
1771}
1772
1773
1774static void verify_use(struct compile_state *state,
1775 struct triple *user, struct triple *used)
1776{
1777 int size, i;
1778 size = TRIPLE_SIZE(user->sizes);
1779 for(i = 0; i < size; i++) {
1780 if (user->param[i] == used) {
1781 break;
1782 }
1783 }
1784 if (triple_is_branch(state, user)) {
1785 if (user->next == used) {
1786 i = -1;
1787 }
1788 }
1789 if (i == size) {
1790 internal_error(state, user, "%s(%p) does not use %s(%p)",
1791 tops(user->op), user, tops(used->op), used);
1792 }
1793}
1794
1795static int find_rhs_use(struct compile_state *state,
1796 struct triple *user, struct triple *used)
1797{
1798 struct triple **param;
1799 int size, i;
1800 verify_use(state, user, used);
1801 size = TRIPLE_RHS(user->sizes);
1802 param = &RHS(user, 0);
1803 for(i = 0; i < size; i++) {
1804 if (param[i] == used) {
1805 return i;
1806 }
1807 }
1808 return -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001809}
1810
1811static void free_triple(struct compile_state *state, struct triple *ptr)
1812{
Eric Biederman0babc1c2003-05-09 02:39:00 +00001813 size_t size;
1814 size = sizeof(*ptr) - sizeof(ptr->param) +
1815 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr->sizes));
Eric Biedermanb138ac82003-04-22 18:44:01 +00001816 ptr->prev->next = ptr->next;
1817 ptr->next->prev = ptr->prev;
1818 if (ptr->use) {
1819 internal_error(state, ptr, "ptr->use != 0");
1820 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001821 put_occurance(ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001822 memset(ptr, -1, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001823 xfree(ptr);
1824}
1825
1826static void release_triple(struct compile_state *state, struct triple *ptr)
1827{
1828 struct triple_set *set, *next;
1829 struct triple **expr;
1830 /* Remove ptr from use chains where it is the user */
1831 expr = triple_rhs(state, ptr, 0);
1832 for(; expr; expr = triple_rhs(state, ptr, expr)) {
1833 if (*expr) {
1834 unuse_triple(*expr, ptr);
1835 }
1836 }
1837 expr = triple_lhs(state, ptr, 0);
1838 for(; expr; expr = triple_lhs(state, ptr, expr)) {
1839 if (*expr) {
1840 unuse_triple(*expr, ptr);
1841 }
1842 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001843 expr = triple_misc(state, ptr, 0);
1844 for(; expr; expr = triple_misc(state, ptr, expr)) {
1845 if (*expr) {
1846 unuse_triple(*expr, ptr);
1847 }
1848 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001849 expr = triple_targ(state, ptr, 0);
1850 for(; expr; expr = triple_targ(state, ptr, expr)) {
1851 if (*expr) {
1852 unuse_triple(*expr, ptr);
1853 }
1854 }
1855 /* Reomve ptr from use chains where it is used */
1856 for(set = ptr->use; set; set = next) {
1857 next = set->next;
1858 expr = triple_rhs(state, set->member, 0);
1859 for(; expr; expr = triple_rhs(state, set->member, expr)) {
1860 if (*expr == ptr) {
1861 *expr = &zero_triple;
1862 }
1863 }
1864 expr = triple_lhs(state, set->member, 0);
1865 for(; expr; expr = triple_lhs(state, set->member, expr)) {
1866 if (*expr == ptr) {
1867 *expr = &zero_triple;
1868 }
1869 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001870 expr = triple_misc(state, set->member, 0);
1871 for(; expr; expr = triple_misc(state, set->member, expr)) {
1872 if (*expr == ptr) {
1873 *expr = &zero_triple;
1874 }
1875 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001876 expr = triple_targ(state, set->member, 0);
1877 for(; expr; expr = triple_targ(state, set->member, expr)) {
1878 if (*expr == ptr) {
1879 *expr = &zero_triple;
1880 }
1881 }
1882 unuse_triple(ptr, set->member);
1883 }
1884 free_triple(state, ptr);
1885}
1886
1887static void print_triple(struct compile_state *state, struct triple *ptr);
1888
1889#define TOK_UNKNOWN 0
1890#define TOK_SPACE 1
1891#define TOK_SEMI 2
1892#define TOK_LBRACE 3
1893#define TOK_RBRACE 4
1894#define TOK_COMMA 5
1895#define TOK_EQ 6
1896#define TOK_COLON 7
1897#define TOK_LBRACKET 8
1898#define TOK_RBRACKET 9
1899#define TOK_LPAREN 10
1900#define TOK_RPAREN 11
1901#define TOK_STAR 12
1902#define TOK_DOTS 13
1903#define TOK_MORE 14
1904#define TOK_LESS 15
1905#define TOK_TIMESEQ 16
1906#define TOK_DIVEQ 17
1907#define TOK_MODEQ 18
1908#define TOK_PLUSEQ 19
1909#define TOK_MINUSEQ 20
1910#define TOK_SLEQ 21
1911#define TOK_SREQ 22
1912#define TOK_ANDEQ 23
1913#define TOK_XOREQ 24
1914#define TOK_OREQ 25
1915#define TOK_EQEQ 26
1916#define TOK_NOTEQ 27
1917#define TOK_QUEST 28
1918#define TOK_LOGOR 29
1919#define TOK_LOGAND 30
1920#define TOK_OR 31
1921#define TOK_AND 32
1922#define TOK_XOR 33
1923#define TOK_LESSEQ 34
1924#define TOK_MOREEQ 35
1925#define TOK_SL 36
1926#define TOK_SR 37
1927#define TOK_PLUS 38
1928#define TOK_MINUS 39
1929#define TOK_DIV 40
1930#define TOK_MOD 41
1931#define TOK_PLUSPLUS 42
1932#define TOK_MINUSMINUS 43
1933#define TOK_BANG 44
1934#define TOK_ARROW 45
1935#define TOK_DOT 46
1936#define TOK_TILDE 47
1937#define TOK_LIT_STRING 48
1938#define TOK_LIT_CHAR 49
1939#define TOK_LIT_INT 50
1940#define TOK_LIT_FLOAT 51
1941#define TOK_MACRO 52
1942#define TOK_CONCATENATE 53
1943
1944#define TOK_IDENT 54
1945#define TOK_STRUCT_NAME 55
1946#define TOK_ENUM_CONST 56
1947#define TOK_TYPE_NAME 57
1948
1949#define TOK_AUTO 58
1950#define TOK_BREAK 59
1951#define TOK_CASE 60
1952#define TOK_CHAR 61
1953#define TOK_CONST 62
1954#define TOK_CONTINUE 63
1955#define TOK_DEFAULT 64
1956#define TOK_DO 65
1957#define TOK_DOUBLE 66
1958#define TOK_ELSE 67
1959#define TOK_ENUM 68
1960#define TOK_EXTERN 69
1961#define TOK_FLOAT 70
1962#define TOK_FOR 71
1963#define TOK_GOTO 72
1964#define TOK_IF 73
1965#define TOK_INLINE 74
1966#define TOK_INT 75
1967#define TOK_LONG 76
1968#define TOK_REGISTER 77
1969#define TOK_RESTRICT 78
1970#define TOK_RETURN 79
1971#define TOK_SHORT 80
1972#define TOK_SIGNED 81
1973#define TOK_SIZEOF 82
1974#define TOK_STATIC 83
1975#define TOK_STRUCT 84
1976#define TOK_SWITCH 85
1977#define TOK_TYPEDEF 86
1978#define TOK_UNION 87
1979#define TOK_UNSIGNED 88
1980#define TOK_VOID 89
1981#define TOK_VOLATILE 90
1982#define TOK_WHILE 91
1983#define TOK_ASM 92
1984#define TOK_ATTRIBUTE 93
1985#define TOK_ALIGNOF 94
1986#define TOK_FIRST_KEYWORD TOK_AUTO
1987#define TOK_LAST_KEYWORD TOK_ALIGNOF
1988
1989#define TOK_DEFINE 100
1990#define TOK_UNDEF 101
1991#define TOK_INCLUDE 102
1992#define TOK_LINE 103
1993#define TOK_ERROR 104
1994#define TOK_WARNING 105
1995#define TOK_PRAGMA 106
1996#define TOK_IFDEF 107
1997#define TOK_IFNDEF 108
1998#define TOK_ELIF 109
1999#define TOK_ENDIF 110
2000
2001#define TOK_FIRST_MACRO TOK_DEFINE
2002#define TOK_LAST_MACRO TOK_ENDIF
2003
2004#define TOK_EOF 111
2005
2006static const char *tokens[] = {
2007[TOK_UNKNOWN ] = "unknown",
2008[TOK_SPACE ] = ":space:",
2009[TOK_SEMI ] = ";",
2010[TOK_LBRACE ] = "{",
2011[TOK_RBRACE ] = "}",
2012[TOK_COMMA ] = ",",
2013[TOK_EQ ] = "=",
2014[TOK_COLON ] = ":",
2015[TOK_LBRACKET ] = "[",
2016[TOK_RBRACKET ] = "]",
2017[TOK_LPAREN ] = "(",
2018[TOK_RPAREN ] = ")",
2019[TOK_STAR ] = "*",
2020[TOK_DOTS ] = "...",
2021[TOK_MORE ] = ">",
2022[TOK_LESS ] = "<",
2023[TOK_TIMESEQ ] = "*=",
2024[TOK_DIVEQ ] = "/=",
2025[TOK_MODEQ ] = "%=",
2026[TOK_PLUSEQ ] = "+=",
2027[TOK_MINUSEQ ] = "-=",
2028[TOK_SLEQ ] = "<<=",
2029[TOK_SREQ ] = ">>=",
2030[TOK_ANDEQ ] = "&=",
2031[TOK_XOREQ ] = "^=",
2032[TOK_OREQ ] = "|=",
2033[TOK_EQEQ ] = "==",
2034[TOK_NOTEQ ] = "!=",
2035[TOK_QUEST ] = "?",
2036[TOK_LOGOR ] = "||",
2037[TOK_LOGAND ] = "&&",
2038[TOK_OR ] = "|",
2039[TOK_AND ] = "&",
2040[TOK_XOR ] = "^",
2041[TOK_LESSEQ ] = "<=",
2042[TOK_MOREEQ ] = ">=",
2043[TOK_SL ] = "<<",
2044[TOK_SR ] = ">>",
2045[TOK_PLUS ] = "+",
2046[TOK_MINUS ] = "-",
2047[TOK_DIV ] = "/",
2048[TOK_MOD ] = "%",
2049[TOK_PLUSPLUS ] = "++",
2050[TOK_MINUSMINUS ] = "--",
2051[TOK_BANG ] = "!",
2052[TOK_ARROW ] = "->",
2053[TOK_DOT ] = ".",
2054[TOK_TILDE ] = "~",
2055[TOK_LIT_STRING ] = ":string:",
2056[TOK_IDENT ] = ":ident:",
2057[TOK_TYPE_NAME ] = ":typename:",
2058[TOK_LIT_CHAR ] = ":char:",
2059[TOK_LIT_INT ] = ":integer:",
2060[TOK_LIT_FLOAT ] = ":float:",
2061[TOK_MACRO ] = "#",
2062[TOK_CONCATENATE ] = "##",
2063
2064[TOK_AUTO ] = "auto",
2065[TOK_BREAK ] = "break",
2066[TOK_CASE ] = "case",
2067[TOK_CHAR ] = "char",
2068[TOK_CONST ] = "const",
2069[TOK_CONTINUE ] = "continue",
2070[TOK_DEFAULT ] = "default",
2071[TOK_DO ] = "do",
2072[TOK_DOUBLE ] = "double",
2073[TOK_ELSE ] = "else",
2074[TOK_ENUM ] = "enum",
2075[TOK_EXTERN ] = "extern",
2076[TOK_FLOAT ] = "float",
2077[TOK_FOR ] = "for",
2078[TOK_GOTO ] = "goto",
2079[TOK_IF ] = "if",
2080[TOK_INLINE ] = "inline",
2081[TOK_INT ] = "int",
2082[TOK_LONG ] = "long",
2083[TOK_REGISTER ] = "register",
2084[TOK_RESTRICT ] = "restrict",
2085[TOK_RETURN ] = "return",
2086[TOK_SHORT ] = "short",
2087[TOK_SIGNED ] = "signed",
2088[TOK_SIZEOF ] = "sizeof",
2089[TOK_STATIC ] = "static",
2090[TOK_STRUCT ] = "struct",
2091[TOK_SWITCH ] = "switch",
2092[TOK_TYPEDEF ] = "typedef",
2093[TOK_UNION ] = "union",
2094[TOK_UNSIGNED ] = "unsigned",
2095[TOK_VOID ] = "void",
2096[TOK_VOLATILE ] = "volatile",
2097[TOK_WHILE ] = "while",
2098[TOK_ASM ] = "asm",
2099[TOK_ATTRIBUTE ] = "__attribute__",
2100[TOK_ALIGNOF ] = "__alignof__",
2101
2102[TOK_DEFINE ] = "define",
2103[TOK_UNDEF ] = "undef",
2104[TOK_INCLUDE ] = "include",
2105[TOK_LINE ] = "line",
2106[TOK_ERROR ] = "error",
2107[TOK_WARNING ] = "warning",
2108[TOK_PRAGMA ] = "pragma",
2109[TOK_IFDEF ] = "ifdef",
2110[TOK_IFNDEF ] = "ifndef",
2111[TOK_ELIF ] = "elif",
2112[TOK_ENDIF ] = "endif",
2113
2114[TOK_EOF ] = "EOF",
2115};
2116
2117static unsigned int hash(const char *str, int str_len)
2118{
2119 unsigned int hash;
2120 const char *end;
2121 end = str + str_len;
2122 hash = 0;
2123 for(; str < end; str++) {
2124 hash = (hash *263) + *str;
2125 }
2126 hash = hash & (HASH_TABLE_SIZE -1);
2127 return hash;
2128}
2129
2130static struct hash_entry *lookup(
2131 struct compile_state *state, const char *name, int name_len)
2132{
2133 struct hash_entry *entry;
2134 unsigned int index;
2135 index = hash(name, name_len);
2136 entry = state->hash_table[index];
2137 while(entry &&
2138 ((entry->name_len != name_len) ||
2139 (memcmp(entry->name, name, name_len) != 0))) {
2140 entry = entry->next;
2141 }
2142 if (!entry) {
2143 char *new_name;
2144 /* Get a private copy of the name */
2145 new_name = xmalloc(name_len + 1, "hash_name");
2146 memcpy(new_name, name, name_len);
2147 new_name[name_len] = '\0';
2148
2149 /* Create a new hash entry */
2150 entry = xcmalloc(sizeof(*entry), "hash_entry");
2151 entry->next = state->hash_table[index];
2152 entry->name = new_name;
2153 entry->name_len = name_len;
2154
2155 /* Place the new entry in the hash table */
2156 state->hash_table[index] = entry;
2157 }
2158 return entry;
2159}
2160
2161static void ident_to_keyword(struct compile_state *state, struct token *tk)
2162{
2163 struct hash_entry *entry;
2164 entry = tk->ident;
2165 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
2166 (entry->tok == TOK_ENUM_CONST) ||
2167 ((entry->tok >= TOK_FIRST_KEYWORD) &&
2168 (entry->tok <= TOK_LAST_KEYWORD)))) {
2169 tk->tok = entry->tok;
2170 }
2171}
2172
2173static void ident_to_macro(struct compile_state *state, struct token *tk)
2174{
2175 struct hash_entry *entry;
2176 entry = tk->ident;
2177 if (entry &&
2178 (entry->tok >= TOK_FIRST_MACRO) &&
2179 (entry->tok <= TOK_LAST_MACRO)) {
2180 tk->tok = entry->tok;
2181 }
2182}
2183
2184static void hash_keyword(
2185 struct compile_state *state, const char *keyword, int tok)
2186{
2187 struct hash_entry *entry;
2188 entry = lookup(state, keyword, strlen(keyword));
2189 if (entry && entry->tok != TOK_UNKNOWN) {
2190 die("keyword %s already hashed", keyword);
2191 }
2192 entry->tok = tok;
2193}
2194
2195static void symbol(
2196 struct compile_state *state, struct hash_entry *ident,
2197 struct symbol **chain, struct triple *def, struct type *type)
2198{
2199 struct symbol *sym;
2200 if (*chain && ((*chain)->scope_depth == state->scope_depth)) {
2201 error(state, 0, "%s already defined", ident->name);
2202 }
2203 sym = xcmalloc(sizeof(*sym), "symbol");
2204 sym->ident = ident;
2205 sym->def = def;
2206 sym->type = type;
2207 sym->scope_depth = state->scope_depth;
2208 sym->next = *chain;
2209 *chain = sym;
2210}
2211
Eric Biederman153ea352003-06-20 14:43:20 +00002212static void label_symbol(struct compile_state *state,
2213 struct hash_entry *ident, struct triple *label)
2214{
2215 struct symbol *sym;
2216 if (ident->sym_label) {
2217 error(state, 0, "label %s already defined", ident->name);
2218 }
2219 sym = xcmalloc(sizeof(*sym), "label");
2220 sym->ident = ident;
2221 sym->def = label;
2222 sym->type = &void_type;
2223 sym->scope_depth = FUNCTION_SCOPE_DEPTH;
2224 sym->next = 0;
2225 ident->sym_label = sym;
2226}
2227
Eric Biedermanb138ac82003-04-22 18:44:01 +00002228static void start_scope(struct compile_state *state)
2229{
2230 state->scope_depth++;
2231}
2232
2233static void end_scope_syms(struct symbol **chain, int depth)
2234{
2235 struct symbol *sym, *next;
2236 sym = *chain;
2237 while(sym && (sym->scope_depth == depth)) {
2238 next = sym->next;
2239 xfree(sym);
2240 sym = next;
2241 }
2242 *chain = sym;
2243}
2244
2245static void end_scope(struct compile_state *state)
2246{
2247 int i;
2248 int depth;
2249 /* Walk through the hash table and remove all symbols
2250 * in the current scope.
2251 */
2252 depth = state->scope_depth;
2253 for(i = 0; i < HASH_TABLE_SIZE; i++) {
2254 struct hash_entry *entry;
2255 entry = state->hash_table[i];
2256 while(entry) {
2257 end_scope_syms(&entry->sym_label, depth);
2258 end_scope_syms(&entry->sym_struct, depth);
2259 end_scope_syms(&entry->sym_ident, depth);
2260 entry = entry->next;
2261 }
2262 }
2263 state->scope_depth = depth - 1;
2264}
2265
2266static void register_keywords(struct compile_state *state)
2267{
2268 hash_keyword(state, "auto", TOK_AUTO);
2269 hash_keyword(state, "break", TOK_BREAK);
2270 hash_keyword(state, "case", TOK_CASE);
2271 hash_keyword(state, "char", TOK_CHAR);
2272 hash_keyword(state, "const", TOK_CONST);
2273 hash_keyword(state, "continue", TOK_CONTINUE);
2274 hash_keyword(state, "default", TOK_DEFAULT);
2275 hash_keyword(state, "do", TOK_DO);
2276 hash_keyword(state, "double", TOK_DOUBLE);
2277 hash_keyword(state, "else", TOK_ELSE);
2278 hash_keyword(state, "enum", TOK_ENUM);
2279 hash_keyword(state, "extern", TOK_EXTERN);
2280 hash_keyword(state, "float", TOK_FLOAT);
2281 hash_keyword(state, "for", TOK_FOR);
2282 hash_keyword(state, "goto", TOK_GOTO);
2283 hash_keyword(state, "if", TOK_IF);
2284 hash_keyword(state, "inline", TOK_INLINE);
2285 hash_keyword(state, "int", TOK_INT);
2286 hash_keyword(state, "long", TOK_LONG);
2287 hash_keyword(state, "register", TOK_REGISTER);
2288 hash_keyword(state, "restrict", TOK_RESTRICT);
2289 hash_keyword(state, "return", TOK_RETURN);
2290 hash_keyword(state, "short", TOK_SHORT);
2291 hash_keyword(state, "signed", TOK_SIGNED);
2292 hash_keyword(state, "sizeof", TOK_SIZEOF);
2293 hash_keyword(state, "static", TOK_STATIC);
2294 hash_keyword(state, "struct", TOK_STRUCT);
2295 hash_keyword(state, "switch", TOK_SWITCH);
2296 hash_keyword(state, "typedef", TOK_TYPEDEF);
2297 hash_keyword(state, "union", TOK_UNION);
2298 hash_keyword(state, "unsigned", TOK_UNSIGNED);
2299 hash_keyword(state, "void", TOK_VOID);
2300 hash_keyword(state, "volatile", TOK_VOLATILE);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002301 hash_keyword(state, "__volatile__", TOK_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002302 hash_keyword(state, "while", TOK_WHILE);
2303 hash_keyword(state, "asm", TOK_ASM);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002304 hash_keyword(state, "__asm__", TOK_ASM);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002305 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
2306 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
2307}
2308
2309static void register_macro_keywords(struct compile_state *state)
2310{
2311 hash_keyword(state, "define", TOK_DEFINE);
2312 hash_keyword(state, "undef", TOK_UNDEF);
2313 hash_keyword(state, "include", TOK_INCLUDE);
2314 hash_keyword(state, "line", TOK_LINE);
2315 hash_keyword(state, "error", TOK_ERROR);
2316 hash_keyword(state, "warning", TOK_WARNING);
2317 hash_keyword(state, "pragma", TOK_PRAGMA);
2318 hash_keyword(state, "ifdef", TOK_IFDEF);
2319 hash_keyword(state, "ifndef", TOK_IFNDEF);
2320 hash_keyword(state, "elif", TOK_ELIF);
2321 hash_keyword(state, "endif", TOK_ENDIF);
2322}
2323
2324static int spacep(int c)
2325{
2326 int ret = 0;
2327 switch(c) {
2328 case ' ':
2329 case '\t':
2330 case '\f':
2331 case '\v':
2332 case '\r':
2333 case '\n':
2334 ret = 1;
2335 break;
2336 }
2337 return ret;
2338}
2339
2340static int digitp(int c)
2341{
2342 int ret = 0;
2343 switch(c) {
2344 case '0': case '1': case '2': case '3': case '4':
2345 case '5': case '6': case '7': case '8': case '9':
2346 ret = 1;
2347 break;
2348 }
2349 return ret;
2350}
Eric Biederman8d9c1232003-06-17 08:42:17 +00002351static int digval(int c)
2352{
2353 int val = -1;
2354 if ((c >= '0') && (c <= '9')) {
2355 val = c - '0';
2356 }
2357 return val;
2358}
Eric Biedermanb138ac82003-04-22 18:44:01 +00002359
2360static int hexdigitp(int c)
2361{
2362 int ret = 0;
2363 switch(c) {
2364 case '0': case '1': case '2': case '3': case '4':
2365 case '5': case '6': case '7': case '8': case '9':
2366 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2367 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2368 ret = 1;
2369 break;
2370 }
2371 return ret;
2372}
2373static int hexdigval(int c)
2374{
2375 int val = -1;
2376 if ((c >= '0') && (c <= '9')) {
2377 val = c - '0';
2378 }
2379 else if ((c >= 'A') && (c <= 'F')) {
2380 val = 10 + (c - 'A');
2381 }
2382 else if ((c >= 'a') && (c <= 'f')) {
2383 val = 10 + (c - 'a');
2384 }
2385 return val;
2386}
2387
2388static int octdigitp(int c)
2389{
2390 int ret = 0;
2391 switch(c) {
2392 case '0': case '1': case '2': case '3':
2393 case '4': case '5': case '6': case '7':
2394 ret = 1;
2395 break;
2396 }
2397 return ret;
2398}
2399static int octdigval(int c)
2400{
2401 int val = -1;
2402 if ((c >= '0') && (c <= '7')) {
2403 val = c - '0';
2404 }
2405 return val;
2406}
2407
2408static int letterp(int c)
2409{
2410 int ret = 0;
2411 switch(c) {
2412 case 'a': case 'b': case 'c': case 'd': case 'e':
2413 case 'f': case 'g': case 'h': case 'i': case 'j':
2414 case 'k': case 'l': case 'm': case 'n': case 'o':
2415 case 'p': case 'q': case 'r': case 's': case 't':
2416 case 'u': case 'v': case 'w': case 'x': case 'y':
2417 case 'z':
2418 case 'A': case 'B': case 'C': case 'D': case 'E':
2419 case 'F': case 'G': case 'H': case 'I': case 'J':
2420 case 'K': case 'L': case 'M': case 'N': case 'O':
2421 case 'P': case 'Q': case 'R': case 'S': case 'T':
2422 case 'U': case 'V': case 'W': case 'X': case 'Y':
2423 case 'Z':
2424 case '_':
2425 ret = 1;
2426 break;
2427 }
2428 return ret;
2429}
2430
2431static int char_value(struct compile_state *state,
2432 const signed char **strp, const signed char *end)
2433{
2434 const signed char *str;
2435 int c;
2436 str = *strp;
2437 c = *str++;
2438 if ((c == '\\') && (str < end)) {
2439 switch(*str) {
2440 case 'n': c = '\n'; str++; break;
2441 case 't': c = '\t'; str++; break;
2442 case 'v': c = '\v'; str++; break;
2443 case 'b': c = '\b'; str++; break;
2444 case 'r': c = '\r'; str++; break;
2445 case 'f': c = '\f'; str++; break;
2446 case 'a': c = '\a'; str++; break;
2447 case '\\': c = '\\'; str++; break;
2448 case '?': c = '?'; str++; break;
2449 case '\'': c = '\''; str++; break;
2450 case '"': c = '"'; break;
2451 case 'x':
2452 c = 0;
2453 str++;
2454 while((str < end) && hexdigitp(*str)) {
2455 c <<= 4;
2456 c += hexdigval(*str);
2457 str++;
2458 }
2459 break;
2460 case '0': case '1': case '2': case '3':
2461 case '4': case '5': case '6': case '7':
2462 c = 0;
2463 while((str < end) && octdigitp(*str)) {
2464 c <<= 3;
2465 c += octdigval(*str);
2466 str++;
2467 }
2468 break;
2469 default:
2470 error(state, 0, "Invalid character constant");
2471 break;
2472 }
2473 }
2474 *strp = str;
2475 return c;
2476}
2477
2478static char *after_digits(char *ptr, char *end)
2479{
2480 while((ptr < end) && digitp(*ptr)) {
2481 ptr++;
2482 }
2483 return ptr;
2484}
2485
2486static char *after_octdigits(char *ptr, char *end)
2487{
2488 while((ptr < end) && octdigitp(*ptr)) {
2489 ptr++;
2490 }
2491 return ptr;
2492}
2493
2494static char *after_hexdigits(char *ptr, char *end)
2495{
2496 while((ptr < end) && hexdigitp(*ptr)) {
2497 ptr++;
2498 }
2499 return ptr;
2500}
2501
2502static void save_string(struct compile_state *state,
2503 struct token *tk, char *start, char *end, const char *id)
2504{
2505 char *str;
2506 int str_len;
2507 /* Create a private copy of the string */
2508 str_len = end - start + 1;
2509 str = xmalloc(str_len + 1, id);
2510 memcpy(str, start, str_len);
2511 str[str_len] = '\0';
2512
2513 /* Store the copy in the token */
2514 tk->val.str = str;
2515 tk->str_len = str_len;
2516}
2517static void next_token(struct compile_state *state, int index)
2518{
2519 struct file_state *file;
2520 struct token *tk;
2521 char *token;
2522 int c, c1, c2, c3;
2523 char *tokp, *end;
2524 int tok;
2525next_token:
2526 file = state->file;
2527 tk = &state->token[index];
2528 tk->str_len = 0;
2529 tk->ident = 0;
2530 token = tokp = file->pos;
2531 end = file->buf + file->size;
2532 tok = TOK_UNKNOWN;
2533 c = -1;
2534 if (tokp < end) {
2535 c = *tokp;
2536 }
2537 c1 = -1;
2538 if ((tokp + 1) < end) {
2539 c1 = tokp[1];
2540 }
2541 c2 = -1;
2542 if ((tokp + 2) < end) {
2543 c2 = tokp[2];
2544 }
2545 c3 = -1;
2546 if ((tokp + 3) < end) {
2547 c3 = tokp[3];
2548 }
2549 if (tokp >= end) {
2550 tok = TOK_EOF;
2551 tokp = end;
2552 }
2553 /* Whitespace */
2554 else if (spacep(c)) {
2555 tok = TOK_SPACE;
2556 while ((tokp < end) && spacep(c)) {
2557 if (c == '\n') {
2558 file->line++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002559 file->report_line++;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002560 file->line_start = tokp + 1;
2561 }
2562 c = *(++tokp);
2563 }
2564 if (!spacep(c)) {
2565 tokp--;
2566 }
2567 }
2568 /* EOL Comments */
2569 else if ((c == '/') && (c1 == '/')) {
2570 tok = TOK_SPACE;
2571 for(tokp += 2; tokp < end; tokp++) {
2572 c = *tokp;
2573 if (c == '\n') {
2574 file->line++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002575 file->report_line++;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002576 file->line_start = tokp +1;
2577 break;
2578 }
2579 }
2580 }
2581 /* Comments */
2582 else if ((c == '/') && (c1 == '*')) {
2583 int line;
2584 char *line_start;
2585 line = file->line;
2586 line_start = file->line_start;
2587 for(tokp += 2; (end - tokp) >= 2; tokp++) {
2588 c = *tokp;
2589 if (c == '\n') {
2590 line++;
2591 line_start = tokp +1;
2592 }
2593 else if ((c == '*') && (tokp[1] == '/')) {
2594 tok = TOK_SPACE;
2595 tokp += 1;
2596 break;
2597 }
2598 }
2599 if (tok == TOK_UNKNOWN) {
2600 error(state, 0, "unterminated comment");
2601 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002602 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002603 file->line = line;
2604 file->line_start = line_start;
2605 }
2606 /* string constants */
2607 else if ((c == '"') ||
2608 ((c == 'L') && (c1 == '"'))) {
2609 int line;
2610 char *line_start;
2611 int wchar;
2612 line = file->line;
2613 line_start = file->line_start;
2614 wchar = 0;
2615 if (c == 'L') {
2616 wchar = 1;
2617 tokp++;
2618 }
2619 for(tokp += 1; tokp < end; tokp++) {
2620 c = *tokp;
2621 if (c == '\n') {
2622 line++;
2623 line_start = tokp + 1;
2624 }
2625 else if ((c == '\\') && (tokp +1 < end)) {
2626 tokp++;
2627 }
2628 else if (c == '"') {
2629 tok = TOK_LIT_STRING;
2630 break;
2631 }
2632 }
2633 if (tok == TOK_UNKNOWN) {
2634 error(state, 0, "unterminated string constant");
2635 }
2636 if (line != file->line) {
2637 warning(state, 0, "multiline string constant");
2638 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002639 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002640 file->line = line;
2641 file->line_start = line_start;
2642
2643 /* Save the string value */
2644 save_string(state, tk, token, tokp, "literal string");
2645 }
2646 /* character constants */
2647 else if ((c == '\'') ||
2648 ((c == 'L') && (c1 == '\''))) {
2649 int line;
2650 char *line_start;
2651 int wchar;
2652 line = file->line;
2653 line_start = file->line_start;
2654 wchar = 0;
2655 if (c == 'L') {
2656 wchar = 1;
2657 tokp++;
2658 }
2659 for(tokp += 1; tokp < end; tokp++) {
2660 c = *tokp;
2661 if (c == '\n') {
2662 line++;
2663 line_start = tokp + 1;
2664 }
2665 else if ((c == '\\') && (tokp +1 < end)) {
2666 tokp++;
2667 }
2668 else if (c == '\'') {
2669 tok = TOK_LIT_CHAR;
2670 break;
2671 }
2672 }
2673 if (tok == TOK_UNKNOWN) {
2674 error(state, 0, "unterminated character constant");
2675 }
2676 if (line != file->line) {
2677 warning(state, 0, "multiline character constant");
2678 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002679 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002680 file->line = line;
2681 file->line_start = line_start;
2682
2683 /* Save the character value */
2684 save_string(state, tk, token, tokp, "literal character");
2685 }
2686 /* integer and floating constants
2687 * Integer Constants
2688 * {digits}
2689 * 0[Xx]{hexdigits}
2690 * 0{octdigit}+
2691 *
2692 * Floating constants
2693 * {digits}.{digits}[Ee][+-]?{digits}
2694 * {digits}.{digits}
2695 * {digits}[Ee][+-]?{digits}
2696 * .{digits}[Ee][+-]?{digits}
2697 * .{digits}
2698 */
2699
2700 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
2701 char *next, *new;
2702 int is_float;
2703 is_float = 0;
2704 if (c != '.') {
2705 next = after_digits(tokp, end);
2706 }
2707 else {
2708 next = tokp;
2709 }
2710 if (next[0] == '.') {
2711 new = after_digits(next, end);
2712 is_float = (new != next);
2713 next = new;
2714 }
2715 if ((next[0] == 'e') || (next[0] == 'E')) {
2716 if (((next + 1) < end) &&
2717 ((next[1] == '+') || (next[1] == '-'))) {
2718 next++;
2719 }
2720 new = after_digits(next, end);
2721 is_float = (new != next);
2722 next = new;
2723 }
2724 if (is_float) {
2725 tok = TOK_LIT_FLOAT;
2726 if ((next < end) && (
2727 (next[0] == 'f') ||
2728 (next[0] == 'F') ||
2729 (next[0] == 'l') ||
2730 (next[0] == 'L'))
2731 ) {
2732 next++;
2733 }
2734 }
2735 if (!is_float && digitp(c)) {
2736 tok = TOK_LIT_INT;
2737 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
2738 next = after_hexdigits(tokp + 2, end);
2739 }
2740 else if (c == '0') {
2741 next = after_octdigits(tokp, end);
2742 }
2743 else {
2744 next = after_digits(tokp, end);
2745 }
2746 /* crazy integer suffixes */
2747 if ((next < end) &&
2748 ((next[0] == 'u') || (next[0] == 'U'))) {
2749 next++;
2750 if ((next < end) &&
2751 ((next[0] == 'l') || (next[0] == 'L'))) {
2752 next++;
2753 }
2754 }
2755 else if ((next < end) &&
2756 ((next[0] == 'l') || (next[0] == 'L'))) {
2757 next++;
2758 if ((next < end) &&
2759 ((next[0] == 'u') || (next[0] == 'U'))) {
2760 next++;
2761 }
2762 }
2763 }
2764 tokp = next - 1;
2765
2766 /* Save the integer/floating point value */
2767 save_string(state, tk, token, tokp, "literal number");
2768 }
2769 /* identifiers */
2770 else if (letterp(c)) {
2771 tok = TOK_IDENT;
2772 for(tokp += 1; tokp < end; tokp++) {
2773 c = *tokp;
2774 if (!letterp(c) && !digitp(c)) {
2775 break;
2776 }
2777 }
2778 tokp -= 1;
2779 tk->ident = lookup(state, token, tokp +1 - token);
2780 }
2781 /* C99 alternate macro characters */
2782 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
2783 tokp += 3;
2784 tok = TOK_CONCATENATE;
2785 }
2786 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { tokp += 2; tok = TOK_DOTS; }
2787 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { tokp += 2; tok = TOK_SLEQ; }
2788 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { tokp += 2; tok = TOK_SREQ; }
2789 else if ((c == '*') && (c1 == '=')) { tokp += 1; tok = TOK_TIMESEQ; }
2790 else if ((c == '/') && (c1 == '=')) { tokp += 1; tok = TOK_DIVEQ; }
2791 else if ((c == '%') && (c1 == '=')) { tokp += 1; tok = TOK_MODEQ; }
2792 else if ((c == '+') && (c1 == '=')) { tokp += 1; tok = TOK_PLUSEQ; }
2793 else if ((c == '-') && (c1 == '=')) { tokp += 1; tok = TOK_MINUSEQ; }
2794 else if ((c == '&') && (c1 == '=')) { tokp += 1; tok = TOK_ANDEQ; }
2795 else if ((c == '^') && (c1 == '=')) { tokp += 1; tok = TOK_XOREQ; }
2796 else if ((c == '|') && (c1 == '=')) { tokp += 1; tok = TOK_OREQ; }
2797 else if ((c == '=') && (c1 == '=')) { tokp += 1; tok = TOK_EQEQ; }
2798 else if ((c == '!') && (c1 == '=')) { tokp += 1; tok = TOK_NOTEQ; }
2799 else if ((c == '|') && (c1 == '|')) { tokp += 1; tok = TOK_LOGOR; }
2800 else if ((c == '&') && (c1 == '&')) { tokp += 1; tok = TOK_LOGAND; }
2801 else if ((c == '<') && (c1 == '=')) { tokp += 1; tok = TOK_LESSEQ; }
2802 else if ((c == '>') && (c1 == '=')) { tokp += 1; tok = TOK_MOREEQ; }
2803 else if ((c == '<') && (c1 == '<')) { tokp += 1; tok = TOK_SL; }
2804 else if ((c == '>') && (c1 == '>')) { tokp += 1; tok = TOK_SR; }
2805 else if ((c == '+') && (c1 == '+')) { tokp += 1; tok = TOK_PLUSPLUS; }
2806 else if ((c == '-') && (c1 == '-')) { tokp += 1; tok = TOK_MINUSMINUS; }
2807 else if ((c == '-') && (c1 == '>')) { tokp += 1; tok = TOK_ARROW; }
2808 else if ((c == '<') && (c1 == ':')) { tokp += 1; tok = TOK_LBRACKET; }
2809 else if ((c == ':') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACKET; }
2810 else if ((c == '<') && (c1 == '%')) { tokp += 1; tok = TOK_LBRACE; }
2811 else if ((c == '%') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACE; }
2812 else if ((c == '%') && (c1 == ':')) { tokp += 1; tok = TOK_MACRO; }
2813 else if ((c == '#') && (c1 == '#')) { tokp += 1; tok = TOK_CONCATENATE; }
2814 else if (c == ';') { tok = TOK_SEMI; }
2815 else if (c == '{') { tok = TOK_LBRACE; }
2816 else if (c == '}') { tok = TOK_RBRACE; }
2817 else if (c == ',') { tok = TOK_COMMA; }
2818 else if (c == '=') { tok = TOK_EQ; }
2819 else if (c == ':') { tok = TOK_COLON; }
2820 else if (c == '[') { tok = TOK_LBRACKET; }
2821 else if (c == ']') { tok = TOK_RBRACKET; }
2822 else if (c == '(') { tok = TOK_LPAREN; }
2823 else if (c == ')') { tok = TOK_RPAREN; }
2824 else if (c == '*') { tok = TOK_STAR; }
2825 else if (c == '>') { tok = TOK_MORE; }
2826 else if (c == '<') { tok = TOK_LESS; }
2827 else if (c == '?') { tok = TOK_QUEST; }
2828 else if (c == '|') { tok = TOK_OR; }
2829 else if (c == '&') { tok = TOK_AND; }
2830 else if (c == '^') { tok = TOK_XOR; }
2831 else if (c == '+') { tok = TOK_PLUS; }
2832 else if (c == '-') { tok = TOK_MINUS; }
2833 else if (c == '/') { tok = TOK_DIV; }
2834 else if (c == '%') { tok = TOK_MOD; }
2835 else if (c == '!') { tok = TOK_BANG; }
2836 else if (c == '.') { tok = TOK_DOT; }
2837 else if (c == '~') { tok = TOK_TILDE; }
2838 else if (c == '#') { tok = TOK_MACRO; }
2839 if (tok == TOK_MACRO) {
2840 /* Only match preprocessor directives at the start of a line */
2841 char *ptr;
2842 for(ptr = file->line_start; spacep(*ptr); ptr++)
2843 ;
2844 if (ptr != tokp) {
2845 tok = TOK_UNKNOWN;
2846 }
2847 }
2848 if (tok == TOK_UNKNOWN) {
2849 error(state, 0, "unknown token");
2850 }
2851
2852 file->pos = tokp + 1;
2853 tk->tok = tok;
2854 if (tok == TOK_IDENT) {
2855 ident_to_keyword(state, tk);
2856 }
2857 /* Don't return space tokens. */
2858 if (tok == TOK_SPACE) {
2859 goto next_token;
2860 }
2861}
2862
2863static void compile_macro(struct compile_state *state, struct token *tk)
2864{
2865 struct file_state *file;
2866 struct hash_entry *ident;
2867 ident = tk->ident;
2868 file = xmalloc(sizeof(*file), "file_state");
2869 file->basename = xstrdup(tk->ident->name);
2870 file->dirname = xstrdup("");
2871 file->size = ident->sym_define->buf_len;
2872 file->buf = xmalloc(file->size +2, file->basename);
2873 memcpy(file->buf, ident->sym_define->buf, file->size);
2874 file->buf[file->size] = '\n';
2875 file->buf[file->size + 1] = '\0';
2876 file->pos = file->buf;
2877 file->line_start = file->pos;
2878 file->line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002879 file->report_line = 1;
2880 file->report_name = file->basename;
2881 file->report_dir = file->dirname;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002882 file->prev = state->file;
2883 state->file = file;
2884}
2885
2886
2887static int mpeek(struct compile_state *state, int index)
2888{
2889 struct token *tk;
2890 int rescan;
2891 tk = &state->token[index + 1];
2892 if (tk->tok == -1) {
2893 next_token(state, index + 1);
2894 }
2895 do {
2896 rescan = 0;
2897 if ((tk->tok == TOK_EOF) &&
2898 (state->file != state->macro_file) &&
2899 (state->file->prev)) {
2900 struct file_state *file = state->file;
2901 state->file = file->prev;
2902 /* file->basename is used keep it */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002903 if (file->report_dir != file->dirname) {
2904 xfree(file->report_dir);
2905 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002906 xfree(file->dirname);
2907 xfree(file->buf);
2908 xfree(file);
2909 next_token(state, index + 1);
2910 rescan = 1;
2911 }
2912 else if (tk->ident && tk->ident->sym_define) {
2913 compile_macro(state, tk);
2914 next_token(state, index + 1);
2915 rescan = 1;
2916 }
2917 } while(rescan);
2918 /* Don't show the token on the next line */
2919 if (state->macro_line < state->macro_file->line) {
2920 return TOK_EOF;
2921 }
2922 return state->token[index +1].tok;
2923}
2924
2925static void meat(struct compile_state *state, int index, int tok)
2926{
2927 int next_tok;
2928 int i;
2929 next_tok = mpeek(state, index);
2930 if (next_tok != tok) {
2931 const char *name1, *name2;
2932 name1 = tokens[next_tok];
2933 name2 = "";
2934 if (next_tok == TOK_IDENT) {
2935 name2 = state->token[index + 1].ident->name;
2936 }
2937 error(state, 0, "found %s %s expected %s",
2938 name1, name2, tokens[tok]);
2939 }
2940 /* Free the old token value */
2941 if (state->token[index].str_len) {
2942 memset((void *)(state->token[index].val.str), -1,
2943 state->token[index].str_len);
2944 xfree(state->token[index].val.str);
2945 }
2946 for(i = index; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
2947 state->token[i] = state->token[i + 1];
2948 }
2949 memset(&state->token[i], 0, sizeof(state->token[i]));
2950 state->token[i].tok = -1;
2951}
2952
2953static long_t mcexpr(struct compile_state *state, int index);
2954
2955static long_t mprimary_expr(struct compile_state *state, int index)
2956{
2957 long_t val;
2958 int tok;
2959 tok = mpeek(state, index);
2960 while(state->token[index + 1].ident &&
2961 state->token[index + 1].ident->sym_define) {
2962 meat(state, index, tok);
2963 compile_macro(state, &state->token[index]);
2964 tok = mpeek(state, index);
2965 }
2966 switch(tok) {
2967 case TOK_LPAREN:
2968 meat(state, index, TOK_LPAREN);
2969 val = mcexpr(state, index);
2970 meat(state, index, TOK_RPAREN);
2971 break;
2972 case TOK_LIT_INT:
2973 {
2974 char *end;
2975 meat(state, index, TOK_LIT_INT);
2976 errno = 0;
2977 val = strtol(state->token[index].val.str, &end, 0);
2978 if (((val == LONG_MIN) || (val == LONG_MAX)) &&
2979 (errno == ERANGE)) {
2980 error(state, 0, "Integer constant to large");
2981 }
2982 break;
2983 }
2984 default:
2985 meat(state, index, TOK_LIT_INT);
2986 val = 0;
2987 }
2988 return val;
2989}
2990static long_t munary_expr(struct compile_state *state, int index)
2991{
2992 long_t val;
2993 switch(mpeek(state, index)) {
2994 case TOK_PLUS:
2995 meat(state, index, TOK_PLUS);
2996 val = munary_expr(state, index);
2997 val = + val;
2998 break;
2999 case TOK_MINUS:
3000 meat(state, index, TOK_MINUS);
3001 val = munary_expr(state, index);
3002 val = - val;
3003 break;
3004 case TOK_TILDE:
3005 meat(state, index, TOK_BANG);
3006 val = munary_expr(state, index);
3007 val = ~ val;
3008 break;
3009 case TOK_BANG:
3010 meat(state, index, TOK_BANG);
3011 val = munary_expr(state, index);
3012 val = ! val;
3013 break;
3014 default:
3015 val = mprimary_expr(state, index);
3016 break;
3017 }
3018 return val;
3019
3020}
3021static long_t mmul_expr(struct compile_state *state, int index)
3022{
3023 long_t val;
3024 int done;
3025 val = munary_expr(state, index);
3026 do {
3027 long_t right;
3028 done = 0;
3029 switch(mpeek(state, index)) {
3030 case TOK_STAR:
3031 meat(state, index, TOK_STAR);
3032 right = munary_expr(state, index);
3033 val = val * right;
3034 break;
3035 case TOK_DIV:
3036 meat(state, index, TOK_DIV);
3037 right = munary_expr(state, index);
3038 val = val / right;
3039 break;
3040 case TOK_MOD:
3041 meat(state, index, TOK_MOD);
3042 right = munary_expr(state, index);
3043 val = val % right;
3044 break;
3045 default:
3046 done = 1;
3047 break;
3048 }
3049 } while(!done);
3050
3051 return val;
3052}
3053
3054static long_t madd_expr(struct compile_state *state, int index)
3055{
3056 long_t val;
3057 int done;
3058 val = mmul_expr(state, index);
3059 do {
3060 long_t right;
3061 done = 0;
3062 switch(mpeek(state, index)) {
3063 case TOK_PLUS:
3064 meat(state, index, TOK_PLUS);
3065 right = mmul_expr(state, index);
3066 val = val + right;
3067 break;
3068 case TOK_MINUS:
3069 meat(state, index, TOK_MINUS);
3070 right = mmul_expr(state, index);
3071 val = val - right;
3072 break;
3073 default:
3074 done = 1;
3075 break;
3076 }
3077 } while(!done);
3078
3079 return val;
3080}
3081
3082static long_t mshift_expr(struct compile_state *state, int index)
3083{
3084 long_t val;
3085 int done;
3086 val = madd_expr(state, index);
3087 do {
3088 long_t right;
3089 done = 0;
3090 switch(mpeek(state, index)) {
3091 case TOK_SL:
3092 meat(state, index, TOK_SL);
3093 right = madd_expr(state, index);
3094 val = val << right;
3095 break;
3096 case TOK_SR:
3097 meat(state, index, TOK_SR);
3098 right = madd_expr(state, index);
3099 val = val >> right;
3100 break;
3101 default:
3102 done = 1;
3103 break;
3104 }
3105 } while(!done);
3106
3107 return val;
3108}
3109
3110static long_t mrel_expr(struct compile_state *state, int index)
3111{
3112 long_t val;
3113 int done;
3114 val = mshift_expr(state, index);
3115 do {
3116 long_t right;
3117 done = 0;
3118 switch(mpeek(state, index)) {
3119 case TOK_LESS:
3120 meat(state, index, TOK_LESS);
3121 right = mshift_expr(state, index);
3122 val = val < right;
3123 break;
3124 case TOK_MORE:
3125 meat(state, index, TOK_MORE);
3126 right = mshift_expr(state, index);
3127 val = val > right;
3128 break;
3129 case TOK_LESSEQ:
3130 meat(state, index, TOK_LESSEQ);
3131 right = mshift_expr(state, index);
3132 val = val <= right;
3133 break;
3134 case TOK_MOREEQ:
3135 meat(state, index, TOK_MOREEQ);
3136 right = mshift_expr(state, index);
3137 val = val >= right;
3138 break;
3139 default:
3140 done = 1;
3141 break;
3142 }
3143 } while(!done);
3144 return val;
3145}
3146
3147static long_t meq_expr(struct compile_state *state, int index)
3148{
3149 long_t val;
3150 int done;
3151 val = mrel_expr(state, index);
3152 do {
3153 long_t right;
3154 done = 0;
3155 switch(mpeek(state, index)) {
3156 case TOK_EQEQ:
3157 meat(state, index, TOK_EQEQ);
3158 right = mrel_expr(state, index);
3159 val = val == right;
3160 break;
3161 case TOK_NOTEQ:
3162 meat(state, index, TOK_NOTEQ);
3163 right = mrel_expr(state, index);
3164 val = val != right;
3165 break;
3166 default:
3167 done = 1;
3168 break;
3169 }
3170 } while(!done);
3171 return val;
3172}
3173
3174static long_t mand_expr(struct compile_state *state, int index)
3175{
3176 long_t val;
3177 val = meq_expr(state, index);
3178 if (mpeek(state, index) == TOK_AND) {
3179 long_t right;
3180 meat(state, index, TOK_AND);
3181 right = meq_expr(state, index);
3182 val = val & right;
3183 }
3184 return val;
3185}
3186
3187static long_t mxor_expr(struct compile_state *state, int index)
3188{
3189 long_t val;
3190 val = mand_expr(state, index);
3191 if (mpeek(state, index) == TOK_XOR) {
3192 long_t right;
3193 meat(state, index, TOK_XOR);
3194 right = mand_expr(state, index);
3195 val = val ^ right;
3196 }
3197 return val;
3198}
3199
3200static long_t mor_expr(struct compile_state *state, int index)
3201{
3202 long_t val;
3203 val = mxor_expr(state, index);
3204 if (mpeek(state, index) == TOK_OR) {
3205 long_t right;
3206 meat(state, index, TOK_OR);
3207 right = mxor_expr(state, index);
3208 val = val | right;
3209 }
3210 return val;
3211}
3212
3213static long_t mland_expr(struct compile_state *state, int index)
3214{
3215 long_t val;
3216 val = mor_expr(state, index);
3217 if (mpeek(state, index) == TOK_LOGAND) {
3218 long_t right;
3219 meat(state, index, TOK_LOGAND);
3220 right = mor_expr(state, index);
3221 val = val && right;
3222 }
3223 return val;
3224}
3225static long_t mlor_expr(struct compile_state *state, int index)
3226{
3227 long_t val;
3228 val = mland_expr(state, index);
3229 if (mpeek(state, index) == TOK_LOGOR) {
3230 long_t right;
3231 meat(state, index, TOK_LOGOR);
3232 right = mland_expr(state, index);
3233 val = val || right;
3234 }
3235 return val;
3236}
3237
3238static long_t mcexpr(struct compile_state *state, int index)
3239{
3240 return mlor_expr(state, index);
3241}
3242static void preprocess(struct compile_state *state, int index)
3243{
3244 /* Doing much more with the preprocessor would require
3245 * a parser and a major restructuring.
3246 * Postpone that for later.
3247 */
3248 struct file_state *file;
3249 struct token *tk;
3250 int line;
3251 int tok;
3252
3253 file = state->file;
3254 tk = &state->token[index];
3255 state->macro_line = line = file->line;
3256 state->macro_file = file;
3257
3258 next_token(state, index);
3259 ident_to_macro(state, tk);
3260 if (tk->tok == TOK_IDENT) {
3261 error(state, 0, "undefined preprocessing directive `%s'",
3262 tk->ident->name);
3263 }
3264 switch(tk->tok) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00003265 case TOK_LIT_INT:
3266 {
3267 int override_line;
3268 override_line = strtoul(tk->val.str, 0, 10);
3269 next_token(state, index);
3270 /* I have a cpp line marker parse it */
3271 if (tk->tok == TOK_LIT_STRING) {
3272 const char *token, *base;
3273 char *name, *dir;
3274 int name_len, dir_len;
3275 name = xmalloc(tk->str_len, "report_name");
3276 token = tk->val.str + 1;
3277 base = strrchr(token, '/');
3278 name_len = tk->str_len -2;
3279 if (base != 0) {
3280 dir_len = base - token;
3281 base++;
3282 name_len -= base - token;
3283 } else {
3284 dir_len = 0;
3285 base = token;
3286 }
3287 memcpy(name, base, name_len);
3288 name[name_len] = '\0';
3289 dir = xmalloc(dir_len + 1, "report_dir");
3290 memcpy(dir, token, dir_len);
3291 dir[dir_len] = '\0';
3292 file->report_line = override_line - 1;
3293 file->report_name = name;
3294 file->report_dir = dir;
3295 }
3296 }
3297 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003298 case TOK_LINE:
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00003299 meat(state, index, TOK_LINE);
3300 meat(state, index, TOK_LIT_INT);
3301 file->report_line = strtoul(tk->val.str, 0, 10) -1;
3302 if (mpeek(state, index) == TOK_LIT_STRING) {
3303 const char *token, *base;
3304 char *name, *dir;
3305 int name_len, dir_len;
3306 meat(state, index, TOK_LIT_STRING);
3307 name = xmalloc(tk->str_len, "report_name");
3308 token = tk->val.str + 1;
3309 name_len = tk->str_len - 2;
3310 if (base != 0) {
3311 dir_len = base - token;
3312 base++;
3313 name_len -= base - token;
3314 } else {
3315 dir_len = 0;
3316 base = token;
3317 }
3318 memcpy(name, base, name_len);
3319 name[name_len] = '\0';
3320 dir = xmalloc(dir_len + 1, "report_dir");
3321 memcpy(dir, token, dir_len);
3322 dir[dir_len] = '\0';
3323 file->report_name = name;
3324 file->report_dir = dir;
3325 }
3326 break;
3327 case TOK_UNDEF:
Eric Biedermanb138ac82003-04-22 18:44:01 +00003328 case TOK_PRAGMA:
3329 if (state->if_value < 0) {
3330 break;
3331 }
3332 warning(state, 0, "Ignoring preprocessor directive: %s",
3333 tk->ident->name);
3334 break;
3335 case TOK_ELIF:
3336 error(state, 0, "#elif not supported");
3337#warning "FIXME multiple #elif and #else in an #if do not work properly"
3338 if (state->if_depth == 0) {
3339 error(state, 0, "#elif without #if");
3340 }
3341 /* If the #if was taken the #elif just disables the following code */
3342 if (state->if_value >= 0) {
3343 state->if_value = - state->if_value;
3344 }
3345 /* If the previous #if was not taken see if the #elif enables the
3346 * trailing code.
3347 */
3348 else if ((state->if_value < 0) &&
3349 (state->if_depth == - state->if_value))
3350 {
3351 if (mcexpr(state, index) != 0) {
3352 state->if_value = state->if_depth;
3353 }
3354 else {
3355 state->if_value = - state->if_depth;
3356 }
3357 }
3358 break;
3359 case TOK_IF:
3360 state->if_depth++;
3361 if (state->if_value < 0) {
3362 break;
3363 }
3364 if (mcexpr(state, index) != 0) {
3365 state->if_value = state->if_depth;
3366 }
3367 else {
3368 state->if_value = - state->if_depth;
3369 }
3370 break;
3371 case TOK_IFNDEF:
3372 state->if_depth++;
3373 if (state->if_value < 0) {
3374 break;
3375 }
3376 next_token(state, index);
3377 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3378 error(state, 0, "Invalid macro name");
3379 }
3380 if (tk->ident->sym_define == 0) {
3381 state->if_value = state->if_depth;
3382 }
3383 else {
3384 state->if_value = - state->if_depth;
3385 }
3386 break;
3387 case TOK_IFDEF:
3388 state->if_depth++;
3389 if (state->if_value < 0) {
3390 break;
3391 }
3392 next_token(state, index);
3393 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3394 error(state, 0, "Invalid macro name");
3395 }
3396 if (tk->ident->sym_define != 0) {
3397 state->if_value = state->if_depth;
3398 }
3399 else {
3400 state->if_value = - state->if_depth;
3401 }
3402 break;
3403 case TOK_ELSE:
3404 if (state->if_depth == 0) {
3405 error(state, 0, "#else without #if");
3406 }
3407 if ((state->if_value >= 0) ||
3408 ((state->if_value < 0) &&
3409 (state->if_depth == -state->if_value)))
3410 {
3411 state->if_value = - state->if_value;
3412 }
3413 break;
3414 case TOK_ENDIF:
3415 if (state->if_depth == 0) {
3416 error(state, 0, "#endif without #if");
3417 }
3418 if ((state->if_value >= 0) ||
3419 ((state->if_value < 0) &&
3420 (state->if_depth == -state->if_value)))
3421 {
3422 state->if_value = state->if_depth - 1;
3423 }
3424 state->if_depth--;
3425 break;
3426 case TOK_DEFINE:
3427 {
3428 struct hash_entry *ident;
3429 struct macro *macro;
3430 char *ptr;
3431
3432 if (state->if_value < 0) /* quit early when #if'd out */
3433 break;
3434
3435 meat(state, index, TOK_IDENT);
3436 ident = tk->ident;
3437
3438
3439 if (*file->pos == '(') {
3440#warning "FIXME macros with arguments not supported"
3441 error(state, 0, "Macros with arguments not supported");
3442 }
3443
3444 /* Find the end of the line to get an estimate of
3445 * the macro's length.
3446 */
3447 for(ptr = file->pos; *ptr != '\n'; ptr++)
3448 ;
3449
3450 if (ident->sym_define != 0) {
3451 error(state, 0, "macro %s already defined\n", ident->name);
3452 }
3453 macro = xmalloc(sizeof(*macro), "macro");
3454 macro->ident = ident;
3455 macro->buf_len = ptr - file->pos +1;
3456 macro->buf = xmalloc(macro->buf_len +2, "macro buf");
3457
3458 memcpy(macro->buf, file->pos, macro->buf_len);
3459 macro->buf[macro->buf_len] = '\n';
3460 macro->buf[macro->buf_len +1] = '\0';
3461
3462 ident->sym_define = macro;
3463 break;
3464 }
3465 case TOK_ERROR:
3466 {
3467 char *end;
3468 int len;
3469 /* Find the end of the line */
3470 for(end = file->pos; *end != '\n'; end++)
3471 ;
3472 len = (end - file->pos);
3473 if (state->if_value >= 0) {
3474 error(state, 0, "%*.*s", len, len, file->pos);
3475 }
3476 file->pos = end;
3477 break;
3478 }
3479 case TOK_WARNING:
3480 {
3481 char *end;
3482 int len;
3483 /* Find the end of the line */
3484 for(end = file->pos; *end != '\n'; end++)
3485 ;
3486 len = (end - file->pos);
3487 if (state->if_value >= 0) {
3488 warning(state, 0, "%*.*s", len, len, file->pos);
3489 }
3490 file->pos = end;
3491 break;
3492 }
3493 case TOK_INCLUDE:
3494 {
3495 char *name;
3496 char *ptr;
3497 int local;
3498 local = 0;
3499 name = 0;
3500 next_token(state, index);
3501 if (tk->tok == TOK_LIT_STRING) {
3502 const char *token;
3503 int name_len;
3504 name = xmalloc(tk->str_len, "include");
3505 token = tk->val.str +1;
3506 name_len = tk->str_len -2;
3507 if (*token == '"') {
3508 token++;
3509 name_len--;
3510 }
3511 memcpy(name, token, name_len);
3512 name[name_len] = '\0';
3513 local = 1;
3514 }
3515 else if (tk->tok == TOK_LESS) {
3516 char *start, *end;
3517 start = file->pos;
3518 for(end = start; *end != '\n'; end++) {
3519 if (*end == '>') {
3520 break;
3521 }
3522 }
3523 if (*end == '\n') {
3524 error(state, 0, "Unterminated included directive");
3525 }
3526 name = xmalloc(end - start + 1, "include");
3527 memcpy(name, start, end - start);
3528 name[end - start] = '\0';
3529 file->pos = end +1;
3530 local = 0;
3531 }
3532 else {
3533 error(state, 0, "Invalid include directive");
3534 }
3535 /* Error if there are any characters after the include */
3536 for(ptr = file->pos; *ptr != '\n'; ptr++) {
Eric Biederman8d9c1232003-06-17 08:42:17 +00003537 switch(*ptr) {
3538 case ' ':
3539 case '\t':
3540 case '\v':
3541 break;
3542 default:
Eric Biedermanb138ac82003-04-22 18:44:01 +00003543 error(state, 0, "garbage after include directive");
3544 }
3545 }
3546 if (state->if_value >= 0) {
3547 compile_file(state, name, local);
3548 }
3549 xfree(name);
3550 next_token(state, index);
3551 return;
3552 }
3553 default:
3554 /* Ignore # without a following ident */
3555 if (tk->tok == TOK_IDENT) {
3556 error(state, 0, "Invalid preprocessor directive: %s",
3557 tk->ident->name);
3558 }
3559 break;
3560 }
3561 /* Consume the rest of the macro line */
3562 do {
3563 tok = mpeek(state, index);
3564 meat(state, index, tok);
3565 } while(tok != TOK_EOF);
3566 return;
3567}
3568
3569static void token(struct compile_state *state, int index)
3570{
3571 struct file_state *file;
3572 struct token *tk;
3573 int rescan;
3574
3575 tk = &state->token[index];
3576 next_token(state, index);
3577 do {
3578 rescan = 0;
3579 file = state->file;
3580 if (tk->tok == TOK_EOF && file->prev) {
3581 state->file = file->prev;
3582 /* file->basename is used keep it */
3583 xfree(file->dirname);
3584 xfree(file->buf);
3585 xfree(file);
3586 next_token(state, index);
3587 rescan = 1;
3588 }
3589 else if (tk->tok == TOK_MACRO) {
3590 preprocess(state, index);
3591 rescan = 1;
3592 }
3593 else if (tk->ident && tk->ident->sym_define) {
3594 compile_macro(state, tk);
3595 next_token(state, index);
3596 rescan = 1;
3597 }
3598 else if (state->if_value < 0) {
3599 next_token(state, index);
3600 rescan = 1;
3601 }
3602 } while(rescan);
3603}
3604
3605static int peek(struct compile_state *state)
3606{
3607 if (state->token[1].tok == -1) {
3608 token(state, 1);
3609 }
3610 return state->token[1].tok;
3611}
3612
3613static int peek2(struct compile_state *state)
3614{
3615 if (state->token[1].tok == -1) {
3616 token(state, 1);
3617 }
3618 if (state->token[2].tok == -1) {
3619 token(state, 2);
3620 }
3621 return state->token[2].tok;
3622}
3623
Eric Biederman0babc1c2003-05-09 02:39:00 +00003624static void eat(struct compile_state *state, int tok)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003625{
3626 int next_tok;
3627 int i;
3628 next_tok = peek(state);
3629 if (next_tok != tok) {
3630 const char *name1, *name2;
3631 name1 = tokens[next_tok];
3632 name2 = "";
3633 if (next_tok == TOK_IDENT) {
3634 name2 = state->token[1].ident->name;
3635 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00003636 error(state, 0, "\tfound %s %s expected %s",
3637 name1, name2 ,tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003638 }
3639 /* Free the old token value */
3640 if (state->token[0].str_len) {
3641 xfree((void *)(state->token[0].val.str));
3642 }
3643 for(i = 0; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
3644 state->token[i] = state->token[i + 1];
3645 }
3646 memset(&state->token[i], 0, sizeof(state->token[i]));
3647 state->token[i].tok = -1;
3648}
Eric Biedermanb138ac82003-04-22 18:44:01 +00003649
3650#warning "FIXME do not hardcode the include paths"
3651static char *include_paths[] = {
3652 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/include",
3653 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/arch/i386/include",
3654 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src",
3655 0
3656};
3657
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003658static void compile_file(struct compile_state *state, const char *filename, int local)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003659{
3660 char cwd[4096];
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003661 const char *subdir, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003662 int subdir_len;
3663 struct file_state *file;
3664 char *basename;
3665 file = xmalloc(sizeof(*file), "file_state");
3666
3667 base = strrchr(filename, '/');
3668 subdir = filename;
3669 if (base != 0) {
3670 subdir_len = base - filename;
3671 base++;
3672 }
3673 else {
3674 base = filename;
3675 subdir_len = 0;
3676 }
3677 basename = xmalloc(strlen(base) +1, "basename");
3678 strcpy(basename, base);
3679 file->basename = basename;
3680
3681 if (getcwd(cwd, sizeof(cwd)) == 0) {
3682 die("cwd buffer to small");
3683 }
3684
3685 if (subdir[0] == '/') {
3686 file->dirname = xmalloc(subdir_len + 1, "dirname");
3687 memcpy(file->dirname, subdir, subdir_len);
3688 file->dirname[subdir_len] = '\0';
3689 }
3690 else {
3691 char *dir;
3692 int dirlen;
3693 char **path;
3694 /* Find the appropriate directory... */
3695 dir = 0;
3696 if (!state->file && exists(cwd, filename)) {
3697 dir = cwd;
3698 }
3699 if (local && state->file && exists(state->file->dirname, filename)) {
3700 dir = state->file->dirname;
3701 }
3702 for(path = include_paths; !dir && *path; path++) {
3703 if (exists(*path, filename)) {
3704 dir = *path;
3705 }
3706 }
3707 if (!dir) {
3708 error(state, 0, "Cannot find `%s'\n", filename);
3709 }
3710 dirlen = strlen(dir);
3711 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
3712 memcpy(file->dirname, dir, dirlen);
3713 file->dirname[dirlen] = '/';
3714 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
3715 file->dirname[dirlen + 1 + subdir_len] = '\0';
3716 }
3717 file->buf = slurp_file(file->dirname, file->basename, &file->size);
3718 xchdir(cwd);
3719
3720 file->pos = file->buf;
3721 file->line_start = file->pos;
3722 file->line = 1;
3723
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00003724 file->report_line = 1;
3725 file->report_name = file->basename;
3726 file->report_dir = file->dirname;
3727
Eric Biedermanb138ac82003-04-22 18:44:01 +00003728 file->prev = state->file;
3729 state->file = file;
3730
3731 process_trigraphs(state);
3732 splice_lines(state);
3733}
3734
Eric Biederman0babc1c2003-05-09 02:39:00 +00003735/* Type helper functions */
Eric Biedermanb138ac82003-04-22 18:44:01 +00003736
3737static struct type *new_type(
3738 unsigned int type, struct type *left, struct type *right)
3739{
3740 struct type *result;
3741 result = xmalloc(sizeof(*result), "type");
3742 result->type = type;
3743 result->left = left;
3744 result->right = right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00003745 result->field_ident = 0;
3746 result->type_ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003747 return result;
3748}
3749
3750static struct type *clone_type(unsigned int specifiers, struct type *old)
3751{
3752 struct type *result;
3753 result = xmalloc(sizeof(*result), "type");
3754 memcpy(result, old, sizeof(*result));
3755 result->type &= TYPE_MASK;
3756 result->type |= specifiers;
3757 return result;
3758}
3759
3760#define SIZEOF_SHORT 2
3761#define SIZEOF_INT 4
3762#define SIZEOF_LONG (sizeof(long_t))
3763
3764#define ALIGNOF_SHORT 2
3765#define ALIGNOF_INT 4
3766#define ALIGNOF_LONG (sizeof(long_t))
3767
3768#define MASK_UCHAR(X) ((X) & ((ulong_t)0xff))
3769#define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT*8)) - 1))
3770static inline ulong_t mask_uint(ulong_t x)
3771{
3772 if (SIZEOF_INT < SIZEOF_LONG) {
3773 ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT*8))) -1;
3774 x &= mask;
3775 }
3776 return x;
3777}
3778#define MASK_UINT(X) (mask_uint(X))
3779#define MASK_ULONG(X) (X)
3780
Eric Biedermanb138ac82003-04-22 18:44:01 +00003781static struct type void_type = { .type = TYPE_VOID };
3782static struct type char_type = { .type = TYPE_CHAR };
3783static struct type uchar_type = { .type = TYPE_UCHAR };
3784static struct type short_type = { .type = TYPE_SHORT };
3785static struct type ushort_type = { .type = TYPE_USHORT };
3786static struct type int_type = { .type = TYPE_INT };
3787static struct type uint_type = { .type = TYPE_UINT };
3788static struct type long_type = { .type = TYPE_LONG };
3789static struct type ulong_type = { .type = TYPE_ULONG };
3790
3791static struct triple *variable(struct compile_state *state, struct type *type)
3792{
3793 struct triple *result;
3794 if ((type->type & STOR_MASK) != STOR_PERM) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00003795 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3796 result = triple(state, OP_ADECL, type, 0, 0);
3797 } else {
3798 struct type *field;
3799 struct triple **vector;
3800 ulong_t index;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003801 result = new_triple(state, OP_VAL_VEC, type, -1, -1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00003802 vector = &result->param[0];
3803
3804 field = type->left;
3805 index = 0;
3806 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
3807 vector[index] = variable(state, field->left);
3808 field = field->right;
3809 index++;
3810 }
3811 vector[index] = variable(state, field);
3812 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003813 }
3814 else {
3815 result = triple(state, OP_SDECL, type, 0, 0);
3816 }
3817 return result;
3818}
3819
3820static void stor_of(FILE *fp, struct type *type)
3821{
3822 switch(type->type & STOR_MASK) {
3823 case STOR_AUTO:
3824 fprintf(fp, "auto ");
3825 break;
3826 case STOR_STATIC:
3827 fprintf(fp, "static ");
3828 break;
3829 case STOR_EXTERN:
3830 fprintf(fp, "extern ");
3831 break;
3832 case STOR_REGISTER:
3833 fprintf(fp, "register ");
3834 break;
3835 case STOR_TYPEDEF:
3836 fprintf(fp, "typedef ");
3837 break;
3838 case STOR_INLINE:
3839 fprintf(fp, "inline ");
3840 break;
3841 }
3842}
3843static void qual_of(FILE *fp, struct type *type)
3844{
3845 if (type->type & QUAL_CONST) {
3846 fprintf(fp, " const");
3847 }
3848 if (type->type & QUAL_VOLATILE) {
3849 fprintf(fp, " volatile");
3850 }
3851 if (type->type & QUAL_RESTRICT) {
3852 fprintf(fp, " restrict");
3853 }
3854}
Eric Biederman0babc1c2003-05-09 02:39:00 +00003855
Eric Biedermanb138ac82003-04-22 18:44:01 +00003856static void name_of(FILE *fp, struct type *type)
3857{
3858 stor_of(fp, type);
3859 switch(type->type & TYPE_MASK) {
3860 case TYPE_VOID:
3861 fprintf(fp, "void");
3862 qual_of(fp, type);
3863 break;
3864 case TYPE_CHAR:
3865 fprintf(fp, "signed char");
3866 qual_of(fp, type);
3867 break;
3868 case TYPE_UCHAR:
3869 fprintf(fp, "unsigned char");
3870 qual_of(fp, type);
3871 break;
3872 case TYPE_SHORT:
3873 fprintf(fp, "signed short");
3874 qual_of(fp, type);
3875 break;
3876 case TYPE_USHORT:
3877 fprintf(fp, "unsigned short");
3878 qual_of(fp, type);
3879 break;
3880 case TYPE_INT:
3881 fprintf(fp, "signed int");
3882 qual_of(fp, type);
3883 break;
3884 case TYPE_UINT:
3885 fprintf(fp, "unsigned int");
3886 qual_of(fp, type);
3887 break;
3888 case TYPE_LONG:
3889 fprintf(fp, "signed long");
3890 qual_of(fp, type);
3891 break;
3892 case TYPE_ULONG:
3893 fprintf(fp, "unsigned long");
3894 qual_of(fp, type);
3895 break;
3896 case TYPE_POINTER:
3897 name_of(fp, type->left);
3898 fprintf(fp, " * ");
3899 qual_of(fp, type);
3900 break;
3901 case TYPE_PRODUCT:
3902 case TYPE_OVERLAP:
3903 name_of(fp, type->left);
3904 fprintf(fp, ", ");
3905 name_of(fp, type->right);
3906 break;
3907 case TYPE_ENUM:
Eric Biederman0babc1c2003-05-09 02:39:00 +00003908 fprintf(fp, "enum %s", type->type_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003909 qual_of(fp, type);
3910 break;
3911 case TYPE_STRUCT:
Eric Biederman0babc1c2003-05-09 02:39:00 +00003912 fprintf(fp, "struct %s", type->type_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003913 qual_of(fp, type);
3914 break;
3915 case TYPE_FUNCTION:
3916 {
3917 name_of(fp, type->left);
3918 fprintf(fp, " (*)(");
3919 name_of(fp, type->right);
3920 fprintf(fp, ")");
3921 break;
3922 }
3923 case TYPE_ARRAY:
3924 name_of(fp, type->left);
3925 fprintf(fp, " [%ld]", type->elements);
3926 break;
3927 default:
3928 fprintf(fp, "????: %x", type->type & TYPE_MASK);
3929 break;
3930 }
3931}
3932
3933static size_t align_of(struct compile_state *state, struct type *type)
3934{
3935 size_t align;
3936 align = 0;
3937 switch(type->type & TYPE_MASK) {
3938 case TYPE_VOID:
3939 align = 1;
3940 break;
3941 case TYPE_CHAR:
3942 case TYPE_UCHAR:
3943 align = 1;
3944 break;
3945 case TYPE_SHORT:
3946 case TYPE_USHORT:
3947 align = ALIGNOF_SHORT;
3948 break;
3949 case TYPE_INT:
3950 case TYPE_UINT:
3951 case TYPE_ENUM:
3952 align = ALIGNOF_INT;
3953 break;
3954 case TYPE_LONG:
3955 case TYPE_ULONG:
3956 case TYPE_POINTER:
3957 align = ALIGNOF_LONG;
3958 break;
3959 case TYPE_PRODUCT:
3960 case TYPE_OVERLAP:
3961 {
3962 size_t left_align, right_align;
3963 left_align = align_of(state, type->left);
3964 right_align = align_of(state, type->right);
3965 align = (left_align >= right_align) ? left_align : right_align;
3966 break;
3967 }
3968 case TYPE_ARRAY:
3969 align = align_of(state, type->left);
3970 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00003971 case TYPE_STRUCT:
3972 align = align_of(state, type->left);
3973 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003974 default:
3975 error(state, 0, "alignof not yet defined for type\n");
3976 break;
3977 }
3978 return align;
3979}
3980
Eric Biederman03b59862003-06-24 14:27:37 +00003981static size_t needed_padding(size_t offset, size_t align)
3982{
3983 size_t padding;
3984 padding = 0;
3985 if (offset % align) {
3986 padding = align - (offset % align);
3987 }
3988 return padding;
3989}
Eric Biedermanb138ac82003-04-22 18:44:01 +00003990static size_t size_of(struct compile_state *state, struct type *type)
3991{
3992 size_t size;
3993 size = 0;
3994 switch(type->type & TYPE_MASK) {
3995 case TYPE_VOID:
3996 size = 0;
3997 break;
3998 case TYPE_CHAR:
3999 case TYPE_UCHAR:
4000 size = 1;
4001 break;
4002 case TYPE_SHORT:
4003 case TYPE_USHORT:
4004 size = SIZEOF_SHORT;
4005 break;
4006 case TYPE_INT:
4007 case TYPE_UINT:
4008 case TYPE_ENUM:
4009 size = SIZEOF_INT;
4010 break;
4011 case TYPE_LONG:
4012 case TYPE_ULONG:
4013 case TYPE_POINTER:
4014 size = SIZEOF_LONG;
4015 break;
4016 case TYPE_PRODUCT:
4017 {
4018 size_t align, pad;
Eric Biederman03b59862003-06-24 14:27:37 +00004019 size = 0;
4020 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004021 align = align_of(state, type->left);
Eric Biederman03b59862003-06-24 14:27:37 +00004022 pad = needed_padding(size, align);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004023 size = size + pad + size_of(state, type->left);
Eric Biederman03b59862003-06-24 14:27:37 +00004024 type = type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004025 }
Eric Biederman03b59862003-06-24 14:27:37 +00004026 align = align_of(state, type);
4027 pad = needed_padding(size, align);
4028 size = size + pad + sizeof(type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004029 break;
4030 }
4031 case TYPE_OVERLAP:
4032 {
4033 size_t size_left, size_right;
4034 size_left = size_of(state, type->left);
4035 size_right = size_of(state, type->right);
4036 size = (size_left >= size_right)? size_left : size_right;
4037 break;
4038 }
4039 case TYPE_ARRAY:
4040 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4041 internal_error(state, 0, "Invalid array type");
4042 } else {
4043 size = size_of(state, type->left) * type->elements;
4044 }
4045 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004046 case TYPE_STRUCT:
4047 size = size_of(state, type->left);
4048 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004049 default:
Eric Biedermand1ea5392003-06-28 06:49:45 +00004050 internal_error(state, 0, "sizeof not yet defined for type\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004051 break;
4052 }
4053 return size;
4054}
4055
Eric Biederman0babc1c2003-05-09 02:39:00 +00004056static size_t field_offset(struct compile_state *state,
4057 struct type *type, struct hash_entry *field)
4058{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004059 struct type *member;
Eric Biederman03b59862003-06-24 14:27:37 +00004060 size_t size, align;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004061 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4062 internal_error(state, 0, "field_offset only works on structures");
4063 }
4064 size = 0;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004065 member = type->left;
4066 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4067 align = align_of(state, member->left);
Eric Biederman03b59862003-06-24 14:27:37 +00004068 size += needed_padding(size, align);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004069 if (member->left->field_ident == field) {
4070 member = member->left;
Eric Biederman00443072003-06-24 12:34:45 +00004071 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004072 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004073 size += size_of(state, member->left);
4074 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004075 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004076 align = align_of(state, member);
Eric Biederman03b59862003-06-24 14:27:37 +00004077 size += needed_padding(size, align);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004078 if (member->field_ident != field) {
Eric Biederman03b59862003-06-24 14:27:37 +00004079 error(state, 0, "member %s not present", field->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00004080 }
4081 return size;
4082}
4083
4084static struct type *field_type(struct compile_state *state,
4085 struct type *type, struct hash_entry *field)
4086{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004087 struct type *member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004088 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4089 internal_error(state, 0, "field_type only works on structures");
4090 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004091 member = type->left;
4092 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4093 if (member->left->field_ident == field) {
4094 member = member->left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004095 break;
4096 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004097 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004098 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004099 if (member->field_ident != field) {
Eric Biederman03b59862003-06-24 14:27:37 +00004100 error(state, 0, "member %s not present", field->name);
4101 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004102 return member;
Eric Biederman03b59862003-06-24 14:27:37 +00004103}
4104
4105static struct type *next_field(struct compile_state *state,
4106 struct type *type, struct type *prev_member)
4107{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004108 struct type *member;
Eric Biederman03b59862003-06-24 14:27:37 +00004109 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4110 internal_error(state, 0, "next_field only works on structures");
4111 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004112 member = type->left;
4113 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman03b59862003-06-24 14:27:37 +00004114 if (!prev_member) {
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004115 member = member->left;
Eric Biederman03b59862003-06-24 14:27:37 +00004116 break;
4117 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004118 if (member->left == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00004119 prev_member = 0;
4120 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004121 member = member->right;
Eric Biederman03b59862003-06-24 14:27:37 +00004122 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004123 if (member == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00004124 prev_member = 0;
4125 }
4126 if (prev_member) {
4127 internal_error(state, 0, "prev_member %s not present",
4128 prev_member->field_ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00004129 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004130 return member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004131}
4132
4133static struct triple *struct_field(struct compile_state *state,
4134 struct triple *decl, struct hash_entry *field)
4135{
4136 struct triple **vector;
4137 struct type *type;
4138 ulong_t index;
4139 type = decl->type;
4140 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4141 return decl;
4142 }
4143 if (decl->op != OP_VAL_VEC) {
4144 internal_error(state, 0, "Invalid struct variable");
4145 }
4146 if (!field) {
4147 internal_error(state, 0, "Missing structure field");
4148 }
4149 type = type->left;
4150 vector = &RHS(decl, 0);
4151 index = 0;
4152 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4153 if (type->left->field_ident == field) {
4154 type = type->left;
4155 break;
4156 }
4157 index += 1;
4158 type = type->right;
4159 }
4160 if (type->field_ident != field) {
4161 internal_error(state, 0, "field %s not found?", field->name);
4162 }
4163 return vector[index];
4164}
4165
Eric Biedermanb138ac82003-04-22 18:44:01 +00004166static void arrays_complete(struct compile_state *state, struct type *type)
4167{
4168 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
4169 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4170 error(state, 0, "array size not specified");
4171 }
4172 arrays_complete(state, type->left);
4173 }
4174}
4175
4176static unsigned int do_integral_promotion(unsigned int type)
4177{
4178 type &= TYPE_MASK;
4179 if (TYPE_INTEGER(type) &&
4180 TYPE_RANK(type) < TYPE_RANK(TYPE_INT)) {
4181 type = TYPE_INT;
4182 }
4183 return type;
4184}
4185
4186static unsigned int do_arithmetic_conversion(
4187 unsigned int left, unsigned int right)
4188{
4189 left &= TYPE_MASK;
4190 right &= TYPE_MASK;
4191 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
4192 return TYPE_LDOUBLE;
4193 }
4194 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
4195 return TYPE_DOUBLE;
4196 }
4197 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
4198 return TYPE_FLOAT;
4199 }
4200 left = do_integral_promotion(left);
4201 right = do_integral_promotion(right);
4202 /* If both operands have the same size done */
4203 if (left == right) {
4204 return left;
4205 }
4206 /* If both operands have the same signedness pick the larger */
4207 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
4208 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
4209 }
4210 /* If the signed type can hold everything use it */
4211 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
4212 return left;
4213 }
4214 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
4215 return right;
4216 }
4217 /* Convert to the unsigned type with the same rank as the signed type */
4218 else if (TYPE_SIGNED(left)) {
4219 return TYPE_MKUNSIGNED(left);
4220 }
4221 else {
4222 return TYPE_MKUNSIGNED(right);
4223 }
4224}
4225
4226/* see if two types are the same except for qualifiers */
4227static int equiv_types(struct type *left, struct type *right)
4228{
4229 unsigned int type;
4230 /* Error if the basic types do not match */
4231 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4232 return 0;
4233 }
4234 type = left->type & TYPE_MASK;
Eric Biederman530b5192003-07-01 10:05:30 +00004235 /* If the basic types match and it is a void type we are done */
4236 if (type == TYPE_VOID) {
4237 return 1;
4238 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004239 /* if the basic types match and it is an arithmetic type we are done */
4240 if (TYPE_ARITHMETIC(type)) {
4241 return 1;
4242 }
4243 /* If it is a pointer type recurse and keep testing */
4244 if (type == TYPE_POINTER) {
4245 return equiv_types(left->left, right->left);
4246 }
4247 else if (type == TYPE_ARRAY) {
4248 return (left->elements == right->elements) &&
4249 equiv_types(left->left, right->left);
4250 }
4251 /* test for struct/union equality */
4252 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004253 return left->type_ident == right->type_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004254 }
4255 /* Test for equivalent functions */
4256 else if (type == TYPE_FUNCTION) {
4257 return equiv_types(left->left, right->left) &&
4258 equiv_types(left->right, right->right);
4259 }
4260 /* We only see TYPE_PRODUCT as part of function equivalence matching */
4261 else if (type == TYPE_PRODUCT) {
4262 return equiv_types(left->left, right->left) &&
4263 equiv_types(left->right, right->right);
4264 }
4265 /* We should see TYPE_OVERLAP */
4266 else {
4267 return 0;
4268 }
4269}
4270
4271static int equiv_ptrs(struct type *left, struct type *right)
4272{
4273 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4274 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4275 return 0;
4276 }
4277 return equiv_types(left->left, right->left);
4278}
4279
4280static struct type *compatible_types(struct type *left, struct type *right)
4281{
4282 struct type *result;
4283 unsigned int type, qual_type;
4284 /* Error if the basic types do not match */
4285 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4286 return 0;
4287 }
4288 type = left->type & TYPE_MASK;
4289 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4290 result = 0;
4291 /* if the basic types match and it is an arithmetic type we are done */
4292 if (TYPE_ARITHMETIC(type)) {
4293 result = new_type(qual_type, 0, 0);
4294 }
4295 /* If it is a pointer type recurse and keep testing */
4296 else if (type == TYPE_POINTER) {
4297 result = compatible_types(left->left, right->left);
4298 if (result) {
4299 result = new_type(qual_type, result, 0);
4300 }
4301 }
4302 /* test for struct/union equality */
4303 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004304 if (left->type_ident == right->type_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004305 result = left;
4306 }
4307 }
4308 /* Test for equivalent functions */
4309 else if (type == TYPE_FUNCTION) {
4310 struct type *lf, *rf;
4311 lf = compatible_types(left->left, right->left);
4312 rf = compatible_types(left->right, right->right);
4313 if (lf && rf) {
4314 result = new_type(qual_type, lf, rf);
4315 }
4316 }
4317 /* We only see TYPE_PRODUCT as part of function equivalence matching */
4318 else if (type == TYPE_PRODUCT) {
4319 struct type *lf, *rf;
4320 lf = compatible_types(left->left, right->left);
4321 rf = compatible_types(left->right, right->right);
4322 if (lf && rf) {
4323 result = new_type(qual_type, lf, rf);
4324 }
4325 }
4326 else {
4327 /* Nothing else is compatible */
4328 }
4329 return result;
4330}
4331
4332static struct type *compatible_ptrs(struct type *left, struct type *right)
4333{
4334 struct type *result;
4335 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4336 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4337 return 0;
4338 }
4339 result = compatible_types(left->left, right->left);
4340 if (result) {
4341 unsigned int qual_type;
4342 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4343 result = new_type(qual_type, result, 0);
4344 }
4345 return result;
4346
4347}
4348static struct triple *integral_promotion(
4349 struct compile_state *state, struct triple *def)
4350{
4351 struct type *type;
4352 type = def->type;
4353 /* As all operations are carried out in registers
4354 * the values are converted on load I just convert
4355 * logical type of the operand.
4356 */
4357 if (TYPE_INTEGER(type->type)) {
4358 unsigned int int_type;
4359 int_type = type->type & ~TYPE_MASK;
4360 int_type |= do_integral_promotion(type->type);
4361 if (int_type != type->type) {
4362 def->type = new_type(int_type, 0, 0);
4363 }
4364 }
4365 return def;
4366}
4367
4368
4369static void arithmetic(struct compile_state *state, struct triple *def)
4370{
4371 if (!TYPE_ARITHMETIC(def->type->type)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004372 error(state, 0, "arithmetic type expexted");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004373 }
4374}
4375
4376static void ptr_arithmetic(struct compile_state *state, struct triple *def)
4377{
4378 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
4379 error(state, def, "pointer or arithmetic type expected");
4380 }
4381}
4382
4383static int is_integral(struct triple *ins)
4384{
4385 return TYPE_INTEGER(ins->type->type);
4386}
4387
4388static void integral(struct compile_state *state, struct triple *def)
4389{
4390 if (!is_integral(def)) {
4391 error(state, 0, "integral type expected");
4392 }
4393}
4394
4395
4396static void bool(struct compile_state *state, struct triple *def)
4397{
4398 if (!TYPE_ARITHMETIC(def->type->type) &&
4399 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
4400 error(state, 0, "arithmetic or pointer type expected");
4401 }
4402}
4403
4404static int is_signed(struct type *type)
4405{
4406 return !!TYPE_SIGNED(type->type);
4407}
4408
Eric Biederman0babc1c2003-05-09 02:39:00 +00004409/* Is this value located in a register otherwise it must be in memory */
4410static int is_in_reg(struct compile_state *state, struct triple *def)
4411{
4412 int in_reg;
4413 if (def->op == OP_ADECL) {
4414 in_reg = 1;
4415 }
4416 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
4417 in_reg = 0;
4418 }
4419 else if (def->op == OP_VAL_VEC) {
4420 in_reg = is_in_reg(state, RHS(def, 0));
4421 }
4422 else if (def->op == OP_DOT) {
4423 in_reg = is_in_reg(state, RHS(def, 0));
4424 }
4425 else {
4426 internal_error(state, 0, "unknown expr storage location");
4427 in_reg = -1;
4428 }
4429 return in_reg;
4430}
4431
Eric Biedermanb138ac82003-04-22 18:44:01 +00004432/* Is this a stable variable location otherwise it must be a temporary */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004433static int is_stable(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004434{
4435 int ret;
4436 ret = 0;
4437 if (!def) {
4438 return 0;
4439 }
4440 if ((def->op == OP_ADECL) ||
4441 (def->op == OP_SDECL) ||
4442 (def->op == OP_DEREF) ||
4443 (def->op == OP_BLOBCONST)) {
4444 ret = 1;
4445 }
4446 else if (def->op == OP_DOT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004447 ret = is_stable(state, RHS(def, 0));
4448 }
4449 else if (def->op == OP_VAL_VEC) {
4450 struct triple **vector;
4451 ulong_t i;
4452 ret = 1;
4453 vector = &RHS(def, 0);
4454 for(i = 0; i < def->type->elements; i++) {
4455 if (!is_stable(state, vector[i])) {
4456 ret = 0;
4457 break;
4458 }
4459 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004460 }
4461 return ret;
4462}
4463
Eric Biederman0babc1c2003-05-09 02:39:00 +00004464static int is_lvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004465{
4466 int ret;
4467 ret = 1;
4468 if (!def) {
4469 return 0;
4470 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004471 if (!is_stable(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004472 return 0;
4473 }
Eric Biederman00443072003-06-24 12:34:45 +00004474 if (def->op == OP_DOT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004475 ret = is_lvalue(state, RHS(def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00004476 }
4477 return ret;
4478}
4479
Eric Biederman00443072003-06-24 12:34:45 +00004480static void clvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004481{
4482 if (!def) {
4483 internal_error(state, def, "nothing where lvalue expected?");
4484 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004485 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004486 error(state, def, "lvalue expected");
4487 }
4488}
Eric Biederman00443072003-06-24 12:34:45 +00004489static void lvalue(struct compile_state *state, struct triple *def)
4490{
4491 clvalue(state, def);
4492 if (def->type->type & QUAL_CONST) {
4493 error(state, def, "modifable lvalue expected");
4494 }
4495}
Eric Biedermanb138ac82003-04-22 18:44:01 +00004496
4497static int is_pointer(struct triple *def)
4498{
4499 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
4500}
4501
4502static void pointer(struct compile_state *state, struct triple *def)
4503{
4504 if (!is_pointer(def)) {
4505 error(state, def, "pointer expected");
4506 }
4507}
4508
4509static struct triple *int_const(
4510 struct compile_state *state, struct type *type, ulong_t value)
4511{
4512 struct triple *result;
4513 switch(type->type & TYPE_MASK) {
4514 case TYPE_CHAR:
4515 case TYPE_INT: case TYPE_UINT:
4516 case TYPE_LONG: case TYPE_ULONG:
4517 break;
4518 default:
4519 internal_error(state, 0, "constant for unkown type");
4520 }
4521 result = triple(state, OP_INTCONST, type, 0, 0);
4522 result->u.cval = value;
4523 return result;
4524}
4525
4526
Eric Biederman0babc1c2003-05-09 02:39:00 +00004527static struct triple *do_mk_addr_expr(struct compile_state *state,
4528 struct triple *expr, struct type *type, ulong_t offset)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004529{
4530 struct triple *result;
Eric Biederman00443072003-06-24 12:34:45 +00004531 clvalue(state, expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004532
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004533 type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
4534
Eric Biedermanb138ac82003-04-22 18:44:01 +00004535 result = 0;
4536 if (expr->op == OP_ADECL) {
4537 error(state, expr, "address of auto variables not supported");
4538 }
4539 else if (expr->op == OP_SDECL) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004540 result = triple(state, OP_ADDRCONST, type, 0, 0);
4541 MISC(result, 0) = expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004542 result->u.cval = offset;
4543 }
4544 else if (expr->op == OP_DEREF) {
4545 result = triple(state, OP_ADD, type,
Eric Biederman0babc1c2003-05-09 02:39:00 +00004546 RHS(expr, 0),
Eric Biedermanb138ac82003-04-22 18:44:01 +00004547 int_const(state, &ulong_type, offset));
4548 }
4549 return result;
4550}
4551
Eric Biederman0babc1c2003-05-09 02:39:00 +00004552static struct triple *mk_addr_expr(
4553 struct compile_state *state, struct triple *expr, ulong_t offset)
4554{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004555 return do_mk_addr_expr(state, expr, expr->type, offset);
Eric Biederman0babc1c2003-05-09 02:39:00 +00004556}
4557
Eric Biedermanb138ac82003-04-22 18:44:01 +00004558static struct triple *mk_deref_expr(
4559 struct compile_state *state, struct triple *expr)
4560{
4561 struct type *base_type;
4562 pointer(state, expr);
4563 base_type = expr->type->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004564 return triple(state, OP_DEREF, base_type, expr, 0);
4565}
4566
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004567static struct triple *array_to_pointer(struct compile_state *state, struct triple *def)
4568{
4569 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
4570 struct type *type;
4571 struct triple *addrconst;
4572 type = new_type(
4573 TYPE_POINTER | (def->type->type & QUAL_MASK),
4574 def->type->left, 0);
4575 addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
4576 MISC(addrconst, 0) = def;
4577 def = addrconst;
4578 }
4579 return def;
4580}
4581
Eric Biederman0babc1c2003-05-09 02:39:00 +00004582static struct triple *deref_field(
4583 struct compile_state *state, struct triple *expr, struct hash_entry *field)
4584{
4585 struct triple *result;
4586 struct type *type, *member;
4587 if (!field) {
4588 internal_error(state, 0, "No field passed to deref_field");
4589 }
4590 result = 0;
4591 type = expr->type;
4592 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4593 error(state, 0, "request for member %s in something not a struct or union",
4594 field->name);
4595 }
Eric Biederman03b59862003-06-24 14:27:37 +00004596 member = field_type(state, type, field);
Eric Biederman0babc1c2003-05-09 02:39:00 +00004597 if ((type->type & STOR_MASK) == STOR_PERM) {
4598 /* Do the pointer arithmetic to get a deref the field */
4599 ulong_t offset;
4600 offset = field_offset(state, type, field);
4601 result = do_mk_addr_expr(state, expr, member, offset);
4602 result = mk_deref_expr(state, result);
4603 }
4604 else {
4605 /* Find the variable for the field I want. */
Eric Biederman03b59862003-06-24 14:27:37 +00004606 result = triple(state, OP_DOT, member, expr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00004607 result->u.field = field;
4608 }
4609 return result;
4610}
4611
Eric Biedermanb138ac82003-04-22 18:44:01 +00004612static struct triple *read_expr(struct compile_state *state, struct triple *def)
4613{
4614 int op;
4615 if (!def) {
4616 return 0;
4617 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004618 if (!is_stable(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004619 return def;
4620 }
4621 /* Tranform an array to a pointer to the first element */
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004622
Eric Biedermanb138ac82003-04-22 18:44:01 +00004623#warning "CHECK_ME is this the right place to transform arrays to pointers?"
4624 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004625 return array_to_pointer(state, def);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004626 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004627 if (is_in_reg(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004628 op = OP_READ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004629 } else {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004630 op = OP_LOAD;
4631 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004632 return triple(state, op, def->type, def, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004633}
4634
4635static void write_compatible(struct compile_state *state,
4636 struct type *dest, struct type *rval)
4637{
4638 int compatible = 0;
4639 /* Both operands have arithmetic type */
4640 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
4641 compatible = 1;
4642 }
4643 /* One operand is a pointer and the other is a pointer to void */
4644 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
4645 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
4646 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
4647 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
4648 compatible = 1;
4649 }
4650 /* If both types are the same without qualifiers we are good */
4651 else if (equiv_ptrs(dest, rval)) {
4652 compatible = 1;
4653 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004654 /* test for struct/union equality */
4655 else if (((dest->type & TYPE_MASK) == TYPE_STRUCT) &&
4656 ((rval->type & TYPE_MASK) == TYPE_STRUCT) &&
4657 (dest->type_ident == rval->type_ident)) {
4658 compatible = 1;
4659 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004660 if (!compatible) {
4661 error(state, 0, "Incompatible types in assignment");
4662 }
4663}
4664
4665static struct triple *write_expr(
4666 struct compile_state *state, struct triple *dest, struct triple *rval)
4667{
4668 struct triple *def;
4669 int op;
4670
4671 def = 0;
4672 if (!rval) {
4673 internal_error(state, 0, "missing rval");
4674 }
4675
4676 if (rval->op == OP_LIST) {
4677 internal_error(state, 0, "expression of type OP_LIST?");
4678 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004679 if (!is_lvalue(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004680 internal_error(state, 0, "writing to a non lvalue?");
4681 }
Eric Biederman00443072003-06-24 12:34:45 +00004682 if (dest->type->type & QUAL_CONST) {
4683 internal_error(state, 0, "modifable lvalue expexted");
4684 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004685
4686 write_compatible(state, dest->type, rval->type);
4687
4688 /* Now figure out which assignment operator to use */
4689 op = -1;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004690 if (is_in_reg(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004691 op = OP_WRITE;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004692 } else {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004693 op = OP_STORE;
4694 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004695 def = triple(state, op, dest->type, dest, rval);
4696 return def;
4697}
4698
4699static struct triple *init_expr(
4700 struct compile_state *state, struct triple *dest, struct triple *rval)
4701{
4702 struct triple *def;
4703
4704 def = 0;
4705 if (!rval) {
4706 internal_error(state, 0, "missing rval");
4707 }
4708 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
4709 rval = read_expr(state, rval);
4710 def = write_expr(state, dest, rval);
4711 }
4712 else {
4713 /* Fill in the array size if necessary */
4714 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
4715 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
4716 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4717 dest->type->elements = rval->type->elements;
4718 }
4719 }
4720 if (!equiv_types(dest->type, rval->type)) {
4721 error(state, 0, "Incompatible types in inializer");
4722 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004723 MISC(dest, 0) = rval;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004724 insert_triple(state, dest, rval);
4725 rval->id |= TRIPLE_FLAG_FLATTENED;
4726 use_triple(MISC(dest, 0), dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004727 }
4728 return def;
4729}
4730
4731struct type *arithmetic_result(
4732 struct compile_state *state, struct triple *left, struct triple *right)
4733{
4734 struct type *type;
4735 /* Sanity checks to ensure I am working with arithmetic types */
4736 arithmetic(state, left);
4737 arithmetic(state, right);
4738 type = new_type(
4739 do_arithmetic_conversion(
4740 left->type->type,
4741 right->type->type), 0, 0);
4742 return type;
4743}
4744
4745struct type *ptr_arithmetic_result(
4746 struct compile_state *state, struct triple *left, struct triple *right)
4747{
4748 struct type *type;
4749 /* Sanity checks to ensure I am working with the proper types */
4750 ptr_arithmetic(state, left);
4751 arithmetic(state, right);
4752 if (TYPE_ARITHMETIC(left->type->type) &&
4753 TYPE_ARITHMETIC(right->type->type)) {
4754 type = arithmetic_result(state, left, right);
4755 }
4756 else if (TYPE_PTR(left->type->type)) {
4757 type = left->type;
4758 }
4759 else {
4760 internal_error(state, 0, "huh?");
4761 type = 0;
4762 }
4763 return type;
4764}
4765
4766
4767/* boolean helper function */
4768
4769static struct triple *ltrue_expr(struct compile_state *state,
4770 struct triple *expr)
4771{
4772 switch(expr->op) {
4773 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
4774 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
4775 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
4776 /* If the expression is already boolean do nothing */
4777 break;
4778 default:
4779 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
4780 break;
4781 }
4782 return expr;
4783}
4784
4785static struct triple *lfalse_expr(struct compile_state *state,
4786 struct triple *expr)
4787{
4788 return triple(state, OP_LFALSE, &int_type, expr, 0);
4789}
4790
4791static struct triple *cond_expr(
4792 struct compile_state *state,
4793 struct triple *test, struct triple *left, struct triple *right)
4794{
4795 struct triple *def;
4796 struct type *result_type;
4797 unsigned int left_type, right_type;
4798 bool(state, test);
4799 left_type = left->type->type;
4800 right_type = right->type->type;
4801 result_type = 0;
4802 /* Both operands have arithmetic type */
4803 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
4804 result_type = arithmetic_result(state, left, right);
4805 }
4806 /* Both operands have void type */
4807 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
4808 ((right_type & TYPE_MASK) == TYPE_VOID)) {
4809 result_type = &void_type;
4810 }
4811 /* pointers to the same type... */
4812 else if ((result_type = compatible_ptrs(left->type, right->type))) {
4813 ;
4814 }
4815 /* Both operands are pointers and left is a pointer to void */
4816 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4817 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4818 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4819 result_type = right->type;
4820 }
4821 /* Both operands are pointers and right is a pointer to void */
4822 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4823 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4824 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4825 result_type = left->type;
4826 }
4827 if (!result_type) {
4828 error(state, 0, "Incompatible types in conditional expression");
4829 }
Eric Biederman30276382003-05-16 20:47:48 +00004830 /* Cleanup and invert the test */
4831 test = lfalse_expr(state, read_expr(state, test));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004832 def = new_triple(state, OP_COND, result_type, 0, 3);
Eric Biederman0babc1c2003-05-09 02:39:00 +00004833 def->param[0] = test;
4834 def->param[1] = left;
4835 def->param[2] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004836 return def;
4837}
4838
4839
Eric Biederman0babc1c2003-05-09 02:39:00 +00004840static int expr_depth(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004841{
4842 int count;
4843 count = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004844 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
4845 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004846 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004847 else if (ins->op == OP_DEREF) {
4848 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004849 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004850 else if (ins->op == OP_VAL) {
4851 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004852 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004853 else if (ins->op == OP_COMMA) {
4854 int ldepth, rdepth;
4855 ldepth = expr_depth(state, RHS(ins, 0));
4856 rdepth = expr_depth(state, RHS(ins, 1));
4857 count = (ldepth >= rdepth)? ldepth : rdepth;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004858 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004859 else if (ins->op == OP_CALL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004860 /* Don't figure the depth of a call just guess it is huge */
4861 count = 1000;
4862 }
4863 else {
4864 struct triple **expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004865 expr = triple_rhs(state, ins, 0);
4866 for(;expr; expr = triple_rhs(state, ins, expr)) {
4867 if (*expr) {
4868 int depth;
4869 depth = expr_depth(state, *expr);
4870 if (depth > count) {
4871 count = depth;
4872 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004873 }
4874 }
4875 }
4876 return count + 1;
4877}
4878
4879static struct triple *flatten(
4880 struct compile_state *state, struct triple *first, struct triple *ptr);
4881
Eric Biederman0babc1c2003-05-09 02:39:00 +00004882static struct triple *flatten_generic(
Eric Biedermanb138ac82003-04-22 18:44:01 +00004883 struct compile_state *state, struct triple *first, struct triple *ptr)
4884{
Eric Biederman0babc1c2003-05-09 02:39:00 +00004885 struct rhs_vector {
4886 int depth;
4887 struct triple **ins;
4888 } vector[MAX_RHS];
4889 int i, rhs, lhs;
4890 /* Only operations with just a rhs should come here */
4891 rhs = TRIPLE_RHS(ptr->sizes);
4892 lhs = TRIPLE_LHS(ptr->sizes);
4893 if (TRIPLE_SIZE(ptr->sizes) != lhs + rhs) {
4894 internal_error(state, ptr, "unexpected args for: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +00004895 ptr->op, tops(ptr->op));
4896 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004897 /* Find the depth of the rhs elements */
4898 for(i = 0; i < rhs; i++) {
4899 vector[i].ins = &RHS(ptr, i);
4900 vector[i].depth = expr_depth(state, *vector[i].ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004901 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004902 /* Selection sort the rhs */
4903 for(i = 0; i < rhs; i++) {
4904 int j, max = i;
4905 for(j = i + 1; j < rhs; j++ ) {
4906 if (vector[j].depth > vector[max].depth) {
4907 max = j;
4908 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004909 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004910 if (max != i) {
4911 struct rhs_vector tmp;
4912 tmp = vector[i];
4913 vector[i] = vector[max];
4914 vector[max] = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004915 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004916 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004917 /* Now flatten the rhs elements */
4918 for(i = 0; i < rhs; i++) {
4919 *vector[i].ins = flatten(state, first, *vector[i].ins);
4920 use_triple(*vector[i].ins, ptr);
4921 }
4922
4923 /* Now flatten the lhs elements */
4924 for(i = 0; i < lhs; i++) {
4925 struct triple **ins = &LHS(ptr, i);
4926 *ins = flatten(state, first, *ins);
4927 use_triple(*ins, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004928 }
4929 return ptr;
4930}
4931
4932static struct triple *flatten_land(
4933 struct compile_state *state, struct triple *first, struct triple *ptr)
4934{
4935 struct triple *left, *right;
4936 struct triple *val, *test, *jmp, *label1, *end;
4937
4938 /* Find the triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004939 left = RHS(ptr, 0);
4940 right = RHS(ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004941
4942 /* Generate the needed triples */
4943 end = label(state);
4944
4945 /* Thread the triples together */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004946 val = flatten(state, first, variable(state, ptr->type));
4947 left = flatten(state, first, write_expr(state, val, left));
4948 test = flatten(state, first,
Eric Biedermanb138ac82003-04-22 18:44:01 +00004949 lfalse_expr(state, read_expr(state, val)));
Eric Biederman0babc1c2003-05-09 02:39:00 +00004950 jmp = flatten(state, first, branch(state, end, test));
4951 label1 = flatten(state, first, label(state));
4952 right = flatten(state, first, write_expr(state, val, right));
4953 TARG(jmp, 0) = flatten(state, first, end);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004954
4955 /* Now give the caller something to chew on */
4956 return read_expr(state, val);
4957}
4958
4959static struct triple *flatten_lor(
4960 struct compile_state *state, struct triple *first, struct triple *ptr)
4961{
4962 struct triple *left, *right;
4963 struct triple *val, *jmp, *label1, *end;
4964
4965 /* Find the triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004966 left = RHS(ptr, 0);
4967 right = RHS(ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004968
4969 /* Generate the needed triples */
4970 end = label(state);
4971
4972 /* Thread the triples together */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004973 val = flatten(state, first, variable(state, ptr->type));
4974 left = flatten(state, first, write_expr(state, val, left));
4975 jmp = flatten(state, first, branch(state, end, left));
4976 label1 = flatten(state, first, label(state));
4977 right = flatten(state, first, write_expr(state, val, right));
4978 TARG(jmp, 0) = flatten(state, first, end);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004979
4980
4981 /* Now give the caller something to chew on */
4982 return read_expr(state, val);
4983}
4984
4985static struct triple *flatten_cond(
4986 struct compile_state *state, struct triple *first, struct triple *ptr)
4987{
4988 struct triple *test, *left, *right;
4989 struct triple *val, *mv1, *jmp1, *label1, *mv2, *middle, *jmp2, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004990
4991 /* Find the triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004992 test = RHS(ptr, 0);
4993 left = RHS(ptr, 1);
4994 right = RHS(ptr, 2);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004995
4996 /* Generate the needed triples */
4997 end = label(state);
4998 middle = label(state);
4999
5000 /* Thread the triples together */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005001 val = flatten(state, first, variable(state, ptr->type));
5002 test = flatten(state, first, test);
5003 jmp1 = flatten(state, first, branch(state, middle, test));
5004 label1 = flatten(state, first, label(state));
5005 left = flatten(state, first, left);
5006 mv1 = flatten(state, first, write_expr(state, val, left));
5007 jmp2 = flatten(state, first, branch(state, end, 0));
5008 TARG(jmp1, 0) = flatten(state, first, middle);
5009 right = flatten(state, first, right);
5010 mv2 = flatten(state, first, write_expr(state, val, right));
5011 TARG(jmp2, 0) = flatten(state, first, end);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005012
5013 /* Now give the caller something to chew on */
5014 return read_expr(state, val);
5015}
5016
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005017struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
5018 struct occurance *base_occurance)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005019{
5020 struct triple *nfunc;
5021 struct triple *nfirst, *ofirst;
5022 struct triple *new, *old;
5023
5024#if 0
5025 fprintf(stdout, "\n");
5026 loc(stdout, state, 0);
5027 fprintf(stdout, "\n__________ copy_func _________\n");
5028 print_triple(state, ofunc);
5029 fprintf(stdout, "__________ copy_func _________ done\n\n");
5030#endif
5031
5032 /* Make a new copy of the old function */
5033 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
5034 nfirst = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005035 ofirst = old = RHS(ofunc, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005036 do {
5037 struct triple *new;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005038 struct occurance *occurance;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005039 int old_lhs, old_rhs;
5040 old_lhs = TRIPLE_LHS(old->sizes);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005041 old_rhs = TRIPLE_RHS(old->sizes);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005042 occurance = inline_occurance(state, base_occurance, old->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005043 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005044 occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005045 if (!triple_stores_block(state, new)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005046 memcpy(&new->u, &old->u, sizeof(new->u));
5047 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005048 if (!nfirst) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00005049 RHS(nfunc, 0) = nfirst = new;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005050 }
5051 else {
5052 insert_triple(state, nfirst, new);
5053 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005054 new->id |= TRIPLE_FLAG_FLATTENED;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005055
5056 /* During the copy remember new as user of old */
5057 use_triple(old, new);
5058
5059 /* Populate the return type if present */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005060 if (old == MISC(ofunc, 0)) {
5061 MISC(nfunc, 0) = new;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005062 }
5063 old = old->next;
5064 } while(old != ofirst);
5065
5066 /* Make a second pass to fix up any unresolved references */
5067 old = ofirst;
5068 new = nfirst;
5069 do {
Eric Biederman0babc1c2003-05-09 02:39:00 +00005070 struct triple **oexpr, **nexpr;
5071 int count, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005072 /* Lookup where the copy is, to join pointers */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005073 count = TRIPLE_SIZE(old->sizes);
5074 for(i = 0; i < count; i++) {
5075 oexpr = &old->param[i];
5076 nexpr = &new->param[i];
5077 if (!*nexpr && *oexpr && (*oexpr)->use) {
5078 *nexpr = (*oexpr)->use->member;
5079 if (*nexpr == old) {
5080 internal_error(state, 0, "new == old?");
5081 }
5082 use_triple(*nexpr, new);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005083 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005084 if (!*nexpr && *oexpr) {
5085 internal_error(state, 0, "Could not copy %d\n", i);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005086 }
5087 }
5088 old = old->next;
5089 new = new->next;
5090 } while((old != ofirst) && (new != nfirst));
5091
5092 /* Make a third pass to cleanup the extra useses */
5093 old = ofirst;
5094 new = nfirst;
5095 do {
5096 unuse_triple(old, new);
5097 old = old->next;
5098 new = new->next;
5099 } while ((old != ofirst) && (new != nfirst));
5100 return nfunc;
5101}
5102
5103static struct triple *flatten_call(
5104 struct compile_state *state, struct triple *first, struct triple *ptr)
5105{
5106 /* Inline the function call */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005107 struct type *ptype;
5108 struct triple *ofunc, *nfunc, *nfirst, *param, *result;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005109 struct triple *end, *nend;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005110 int pvals, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005111
5112 /* Find the triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005113 ofunc = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005114 if (ofunc->op != OP_LIST) {
5115 internal_error(state, 0, "improper function");
5116 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005117 nfunc = copy_func(state, ofunc, ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005118 nfirst = RHS(nfunc, 0)->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005119 /* Prepend the parameter reading into the new function list */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005120 ptype = nfunc->type->right;
5121 param = RHS(nfunc, 0)->next;
5122 pvals = TRIPLE_RHS(ptr->sizes);
5123 for(i = 0; i < pvals; i++) {
5124 struct type *atype;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005125 struct triple *arg;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005126 atype = ptype;
5127 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
5128 atype = ptype->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005129 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005130 while((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
5131 param = param->next;
5132 }
5133 arg = RHS(ptr, i);
5134 flatten(state, nfirst, write_expr(state, param, arg));
5135 ptype = ptype->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005136 param = param->next;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005137 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005138 result = 0;
5139 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00005140 result = read_expr(state, MISC(nfunc,0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005141 }
5142#if 0
5143 fprintf(stdout, "\n");
5144 loc(stdout, state, 0);
5145 fprintf(stdout, "\n__________ flatten_call _________\n");
5146 print_triple(state, nfunc);
5147 fprintf(stdout, "__________ flatten_call _________ done\n\n");
5148#endif
5149
5150 /* Get rid of the extra triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005151 nfirst = RHS(nfunc, 0)->next;
5152 free_triple(state, RHS(nfunc, 0));
5153 RHS(nfunc, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005154 free_triple(state, nfunc);
5155
5156 /* Append the new function list onto the return list */
5157 end = first->prev;
5158 nend = nfirst->prev;
5159 end->next = nfirst;
5160 nfirst->prev = end;
5161 nend->next = first;
5162 first->prev = nend;
5163
5164 return result;
5165}
5166
5167static struct triple *flatten(
5168 struct compile_state *state, struct triple *first, struct triple *ptr)
5169{
5170 struct triple *orig_ptr;
5171 if (!ptr)
5172 return 0;
5173 do {
5174 orig_ptr = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005175 /* Only flatten triples once */
5176 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
5177 return ptr;
5178 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005179 switch(ptr->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005180 case OP_COMMA:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005181 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5182 ptr = RHS(ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005183 break;
5184 case OP_VAL:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005185 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5186 return MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005187 break;
5188 case OP_LAND:
5189 ptr = flatten_land(state, first, ptr);
5190 break;
5191 case OP_LOR:
5192 ptr = flatten_lor(state, first, ptr);
5193 break;
5194 case OP_COND:
5195 ptr = flatten_cond(state, first, ptr);
5196 break;
5197 case OP_CALL:
5198 ptr = flatten_call(state, first, ptr);
5199 break;
5200 case OP_READ:
5201 case OP_LOAD:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005202 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5203 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005204 break;
5205 case OP_BRANCH:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005206 use_triple(TARG(ptr, 0), ptr);
5207 if (TRIPLE_RHS(ptr->sizes)) {
5208 use_triple(RHS(ptr, 0), ptr);
5209 if (ptr->next != ptr) {
5210 use_triple(ptr->next, ptr);
5211 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005212 }
5213 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005214 case OP_BLOBCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005215 insert_triple(state, first, ptr);
5216 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005217 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005218 use_triple(MISC(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005219 break;
5220 case OP_DEREF:
5221 /* Since OP_DEREF is just a marker delete it when I flatten it */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005222 ptr = RHS(ptr, 0);
5223 RHS(orig_ptr, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005224 free_triple(state, orig_ptr);
5225 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005226 case OP_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005227 {
5228 struct triple *base;
5229 base = RHS(ptr, 0);
Eric Biederman00443072003-06-24 12:34:45 +00005230 if (base->op == OP_DEREF) {
Eric Biederman03b59862003-06-24 14:27:37 +00005231 struct triple *left;
Eric Biederman00443072003-06-24 12:34:45 +00005232 ulong_t offset;
5233 offset = field_offset(state, base->type, ptr->u.field);
Eric Biederman03b59862003-06-24 14:27:37 +00005234 left = RHS(base, 0);
5235 ptr = triple(state, OP_ADD, left->type,
5236 read_expr(state, left),
Eric Biederman00443072003-06-24 12:34:45 +00005237 int_const(state, &ulong_type, offset));
5238 free_triple(state, base);
5239 }
5240 else if (base->op == OP_VAL_VEC) {
5241 base = flatten(state, first, base);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005242 ptr = struct_field(state, base, ptr->u.field);
5243 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005244 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005245 }
Eric Biederman8d9c1232003-06-17 08:42:17 +00005246 case OP_PIECE:
5247 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5248 use_triple(MISC(ptr, 0), ptr);
5249 use_triple(ptr, MISC(ptr, 0));
5250 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005251 case OP_ADDRCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005252 case OP_SDECL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005253 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5254 use_triple(MISC(ptr, 0), ptr);
5255 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005256 case OP_ADECL:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005257 break;
5258 default:
5259 /* Flatten the easy cases we don't override */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005260 ptr = flatten_generic(state, first, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005261 break;
5262 }
5263 } while(ptr && (ptr != orig_ptr));
Eric Biederman0babc1c2003-05-09 02:39:00 +00005264 if (ptr) {
5265 insert_triple(state, first, ptr);
5266 ptr->id |= TRIPLE_FLAG_FLATTENED;
5267 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005268 return ptr;
5269}
5270
5271static void release_expr(struct compile_state *state, struct triple *expr)
5272{
5273 struct triple *head;
5274 head = label(state);
5275 flatten(state, head, expr);
5276 while(head->next != head) {
5277 release_triple(state, head->next);
5278 }
5279 free_triple(state, head);
5280}
5281
5282static int replace_rhs_use(struct compile_state *state,
5283 struct triple *orig, struct triple *new, struct triple *use)
5284{
5285 struct triple **expr;
5286 int found;
5287 found = 0;
5288 expr = triple_rhs(state, use, 0);
5289 for(;expr; expr = triple_rhs(state, use, expr)) {
5290 if (*expr == orig) {
5291 *expr = new;
5292 found = 1;
5293 }
5294 }
5295 if (found) {
5296 unuse_triple(orig, use);
5297 use_triple(new, use);
5298 }
5299 return found;
5300}
5301
5302static int replace_lhs_use(struct compile_state *state,
5303 struct triple *orig, struct triple *new, struct triple *use)
5304{
5305 struct triple **expr;
5306 int found;
5307 found = 0;
5308 expr = triple_lhs(state, use, 0);
5309 for(;expr; expr = triple_lhs(state, use, expr)) {
5310 if (*expr == orig) {
5311 *expr = new;
5312 found = 1;
5313 }
5314 }
5315 if (found) {
5316 unuse_triple(orig, use);
5317 use_triple(new, use);
5318 }
5319 return found;
5320}
5321
5322static void propogate_use(struct compile_state *state,
5323 struct triple *orig, struct triple *new)
5324{
5325 struct triple_set *user, *next;
5326 for(user = orig->use; user; user = next) {
5327 struct triple *use;
5328 int found;
5329 next = user->next;
5330 use = user->member;
5331 found = 0;
5332 found |= replace_rhs_use(state, orig, new, use);
5333 found |= replace_lhs_use(state, orig, new, use);
5334 if (!found) {
5335 internal_error(state, use, "use without use");
5336 }
5337 }
5338 if (orig->use) {
5339 internal_error(state, orig, "used after propogate_use");
5340 }
5341}
5342
5343/*
5344 * Code generators
5345 * ===========================
5346 */
5347
5348static struct triple *mk_add_expr(
5349 struct compile_state *state, struct triple *left, struct triple *right)
5350{
5351 struct type *result_type;
5352 /* Put pointer operands on the left */
5353 if (is_pointer(right)) {
5354 struct triple *tmp;
5355 tmp = left;
5356 left = right;
5357 right = tmp;
5358 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005359 left = read_expr(state, left);
5360 right = read_expr(state, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005361 result_type = ptr_arithmetic_result(state, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005362 if (is_pointer(left)) {
5363 right = triple(state,
5364 is_signed(right->type)? OP_SMUL : OP_UMUL,
5365 &ulong_type,
5366 right,
5367 int_const(state, &ulong_type,
5368 size_of(state, left->type->left)));
5369 }
5370 return triple(state, OP_ADD, result_type, left, right);
5371}
5372
5373static struct triple *mk_sub_expr(
5374 struct compile_state *state, struct triple *left, struct triple *right)
5375{
5376 struct type *result_type;
5377 result_type = ptr_arithmetic_result(state, left, right);
5378 left = read_expr(state, left);
5379 right = read_expr(state, right);
5380 if (is_pointer(left)) {
5381 right = triple(state,
5382 is_signed(right->type)? OP_SMUL : OP_UMUL,
5383 &ulong_type,
5384 right,
5385 int_const(state, &ulong_type,
5386 size_of(state, left->type->left)));
5387 }
5388 return triple(state, OP_SUB, result_type, left, right);
5389}
5390
5391static struct triple *mk_pre_inc_expr(
5392 struct compile_state *state, struct triple *def)
5393{
5394 struct triple *val;
5395 lvalue(state, def);
5396 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
5397 return triple(state, OP_VAL, def->type,
5398 write_expr(state, def, val),
5399 val);
5400}
5401
5402static struct triple *mk_pre_dec_expr(
5403 struct compile_state *state, struct triple *def)
5404{
5405 struct triple *val;
5406 lvalue(state, def);
5407 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
5408 return triple(state, OP_VAL, def->type,
5409 write_expr(state, def, val),
5410 val);
5411}
5412
5413static struct triple *mk_post_inc_expr(
5414 struct compile_state *state, struct triple *def)
5415{
5416 struct triple *val;
5417 lvalue(state, def);
5418 val = read_expr(state, def);
5419 return triple(state, OP_VAL, def->type,
5420 write_expr(state, def,
5421 mk_add_expr(state, val, int_const(state, &int_type, 1)))
5422 , val);
5423}
5424
5425static struct triple *mk_post_dec_expr(
5426 struct compile_state *state, struct triple *def)
5427{
5428 struct triple *val;
5429 lvalue(state, def);
5430 val = read_expr(state, def);
5431 return triple(state, OP_VAL, def->type,
5432 write_expr(state, def,
5433 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
5434 , val);
5435}
5436
5437static struct triple *mk_subscript_expr(
5438 struct compile_state *state, struct triple *left, struct triple *right)
5439{
5440 left = read_expr(state, left);
5441 right = read_expr(state, right);
5442 if (!is_pointer(left) && !is_pointer(right)) {
5443 error(state, left, "subscripted value is not a pointer");
5444 }
5445 return mk_deref_expr(state, mk_add_expr(state, left, right));
5446}
5447
5448/*
5449 * Compile time evaluation
5450 * ===========================
5451 */
5452static int is_const(struct triple *ins)
5453{
5454 return IS_CONST_OP(ins->op);
5455}
5456
5457static int constants_equal(struct compile_state *state,
5458 struct triple *left, struct triple *right)
5459{
5460 int equal;
5461 if (!is_const(left) || !is_const(right)) {
5462 equal = 0;
5463 }
5464 else if (left->op != right->op) {
5465 equal = 0;
5466 }
5467 else if (!equiv_types(left->type, right->type)) {
5468 equal = 0;
5469 }
5470 else {
5471 equal = 0;
5472 switch(left->op) {
5473 case OP_INTCONST:
5474 if (left->u.cval == right->u.cval) {
5475 equal = 1;
5476 }
5477 break;
5478 case OP_BLOBCONST:
5479 {
5480 size_t lsize, rsize;
5481 lsize = size_of(state, left->type);
5482 rsize = size_of(state, right->type);
5483 if (lsize != rsize) {
5484 break;
5485 }
5486 if (memcmp(left->u.blob, right->u.blob, lsize) == 0) {
5487 equal = 1;
5488 }
5489 break;
5490 }
5491 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005492 if ((MISC(left, 0) == MISC(right, 0)) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00005493 (left->u.cval == right->u.cval)) {
5494 equal = 1;
5495 }
5496 break;
5497 default:
5498 internal_error(state, left, "uknown constant type");
5499 break;
5500 }
5501 }
5502 return equal;
5503}
5504
5505static int is_zero(struct triple *ins)
5506{
5507 return is_const(ins) && (ins->u.cval == 0);
5508}
5509
5510static int is_one(struct triple *ins)
5511{
5512 return is_const(ins) && (ins->u.cval == 1);
5513}
5514
Eric Biederman530b5192003-07-01 10:05:30 +00005515static long_t bit_count(ulong_t value)
5516{
5517 int count;
5518 int i;
5519 count = 0;
5520 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
5521 ulong_t mask;
5522 mask = 1;
5523 mask <<= i;
5524 if (value & mask) {
5525 count++;
5526 }
5527 }
5528 return count;
5529
5530}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005531static long_t bsr(ulong_t value)
5532{
5533 int i;
5534 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
5535 ulong_t mask;
5536 mask = 1;
5537 mask <<= i;
5538 if (value & mask) {
5539 return i;
5540 }
5541 }
5542 return -1;
5543}
5544
5545static long_t bsf(ulong_t value)
5546{
5547 int i;
5548 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
5549 ulong_t mask;
5550 mask = 1;
5551 mask <<= 1;
5552 if (value & mask) {
5553 return i;
5554 }
5555 }
5556 return -1;
5557}
5558
5559static long_t log2(ulong_t value)
5560{
5561 return bsr(value);
5562}
5563
5564static long_t tlog2(struct triple *ins)
5565{
5566 return log2(ins->u.cval);
5567}
5568
5569static int is_pow2(struct triple *ins)
5570{
5571 ulong_t value, mask;
5572 long_t log;
5573 if (!is_const(ins)) {
5574 return 0;
5575 }
5576 value = ins->u.cval;
5577 log = log2(value);
5578 if (log == -1) {
5579 return 0;
5580 }
5581 mask = 1;
5582 mask <<= log;
5583 return ((value & mask) == value);
5584}
5585
5586static ulong_t read_const(struct compile_state *state,
5587 struct triple *ins, struct triple **expr)
5588{
5589 struct triple *rhs;
5590 rhs = *expr;
5591 switch(rhs->type->type &TYPE_MASK) {
5592 case TYPE_CHAR:
5593 case TYPE_SHORT:
5594 case TYPE_INT:
5595 case TYPE_LONG:
5596 case TYPE_UCHAR:
5597 case TYPE_USHORT:
5598 case TYPE_UINT:
5599 case TYPE_ULONG:
5600 case TYPE_POINTER:
5601 break;
5602 default:
5603 internal_error(state, rhs, "bad type to read_const\n");
5604 break;
5605 }
5606 return rhs->u.cval;
5607}
5608
5609static long_t read_sconst(struct triple *ins, struct triple **expr)
5610{
5611 struct triple *rhs;
5612 rhs = *expr;
5613 return (long_t)(rhs->u.cval);
5614}
5615
5616static void unuse_rhs(struct compile_state *state, struct triple *ins)
5617{
5618 struct triple **expr;
5619 expr = triple_rhs(state, ins, 0);
5620 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00005621 if (*expr) {
5622 unuse_triple(*expr, ins);
5623 *expr = 0;
5624 }
5625 }
5626}
5627
5628static void unuse_lhs(struct compile_state *state, struct triple *ins)
5629{
5630 struct triple **expr;
5631 expr = triple_lhs(state, ins, 0);
5632 for(;expr;expr = triple_lhs(state, ins, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005633 unuse_triple(*expr, ins);
5634 *expr = 0;
5635 }
5636}
Eric Biederman0babc1c2003-05-09 02:39:00 +00005637
Eric Biedermanb138ac82003-04-22 18:44:01 +00005638static void check_lhs(struct compile_state *state, struct triple *ins)
5639{
5640 struct triple **expr;
5641 expr = triple_lhs(state, ins, 0);
5642 for(;expr;expr = triple_lhs(state, ins, expr)) {
5643 internal_error(state, ins, "unexpected lhs");
5644 }
5645
5646}
5647static void check_targ(struct compile_state *state, struct triple *ins)
5648{
5649 struct triple **expr;
5650 expr = triple_targ(state, ins, 0);
5651 for(;expr;expr = triple_targ(state, ins, expr)) {
5652 internal_error(state, ins, "unexpected targ");
5653 }
5654}
5655
5656static void wipe_ins(struct compile_state *state, struct triple *ins)
5657{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005658 /* Becareful which instructions you replace the wiped
5659 * instruction with, as there are not enough slots
5660 * in all instructions to hold all others.
5661 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005662 check_targ(state, ins);
5663 unuse_rhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005664 unuse_lhs(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005665}
5666
5667static void mkcopy(struct compile_state *state,
5668 struct triple *ins, struct triple *rhs)
5669{
5670 wipe_ins(state, ins);
5671 ins->op = OP_COPY;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005672 ins->sizes = TRIPLE_SIZES(0, 1, 0, 0);
5673 RHS(ins, 0) = rhs;
5674 use_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005675}
5676
5677static void mkconst(struct compile_state *state,
5678 struct triple *ins, ulong_t value)
5679{
5680 if (!is_integral(ins) && !is_pointer(ins)) {
5681 internal_error(state, ins, "unknown type to make constant\n");
5682 }
5683 wipe_ins(state, ins);
5684 ins->op = OP_INTCONST;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005685 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005686 ins->u.cval = value;
5687}
5688
5689static void mkaddr_const(struct compile_state *state,
5690 struct triple *ins, struct triple *sdecl, ulong_t value)
5691{
5692 wipe_ins(state, ins);
5693 ins->op = OP_ADDRCONST;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005694 ins->sizes = TRIPLE_SIZES(0, 0, 1, 0);
5695 MISC(ins, 0) = sdecl;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005696 ins->u.cval = value;
5697 use_triple(sdecl, ins);
5698}
5699
Eric Biederman0babc1c2003-05-09 02:39:00 +00005700/* Transform multicomponent variables into simple register variables */
5701static void flatten_structures(struct compile_state *state)
5702{
5703 struct triple *ins, *first;
5704 first = RHS(state->main_function, 0);
5705 ins = first;
5706 /* Pass one expand structure values into valvecs.
5707 */
5708 ins = first;
5709 do {
5710 struct triple *next;
5711 next = ins->next;
5712 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5713 if (ins->op == OP_VAL_VEC) {
5714 /* Do nothing */
5715 }
5716 else if ((ins->op == OP_LOAD) || (ins->op == OP_READ)) {
5717 struct triple *def, **vector;
5718 struct type *tptr;
5719 int op;
5720 ulong_t i;
5721
5722 op = ins->op;
5723 def = RHS(ins, 0);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005724 get_occurance(ins->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005725 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005726 ins->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005727
5728 vector = &RHS(next, 0);
5729 tptr = next->type->left;
5730 for(i = 0; i < next->type->elements; i++) {
5731 struct triple *sfield;
5732 struct type *mtype;
5733 mtype = tptr;
5734 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5735 mtype = mtype->left;
5736 }
5737 sfield = deref_field(state, def, mtype->field_ident);
5738
5739 vector[i] = triple(
5740 state, op, mtype, sfield, 0);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005741 put_occurance(vector[i]->occurance);
5742 get_occurance(next->occurance);
5743 vector[i]->occurance = next->occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005744 tptr = tptr->right;
5745 }
5746 propogate_use(state, ins, next);
5747 flatten(state, ins, next);
5748 free_triple(state, ins);
5749 }
5750 else if ((ins->op == OP_STORE) || (ins->op == OP_WRITE)) {
5751 struct triple *src, *dst, **vector;
5752 struct type *tptr;
5753 int op;
5754 ulong_t i;
5755
5756 op = ins->op;
Eric Biederman530b5192003-07-01 10:05:30 +00005757 src = RHS(ins, 1);
5758 dst = RHS(ins, 0);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005759 get_occurance(ins->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005760 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005761 ins->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005762
5763 vector = &RHS(next, 0);
5764 tptr = next->type->left;
5765 for(i = 0; i < ins->type->elements; i++) {
5766 struct triple *dfield, *sfield;
5767 struct type *mtype;
5768 mtype = tptr;
5769 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5770 mtype = mtype->left;
5771 }
5772 sfield = deref_field(state, src, mtype->field_ident);
5773 dfield = deref_field(state, dst, mtype->field_ident);
5774 vector[i] = triple(
5775 state, op, mtype, dfield, sfield);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005776 put_occurance(vector[i]->occurance);
5777 get_occurance(next->occurance);
5778 vector[i]->occurance = next->occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005779 tptr = tptr->right;
5780 }
5781 propogate_use(state, ins, next);
5782 flatten(state, ins, next);
5783 free_triple(state, ins);
5784 }
5785 }
5786 ins = next;
5787 } while(ins != first);
5788 /* Pass two flatten the valvecs.
5789 */
5790 ins = first;
5791 do {
5792 struct triple *next;
5793 next = ins->next;
5794 if (ins->op == OP_VAL_VEC) {
5795 release_triple(state, ins);
5796 }
5797 ins = next;
5798 } while(ins != first);
5799 /* Pass three verify the state and set ->id to 0.
5800 */
5801 ins = first;
5802 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005803 ins->id &= ~TRIPLE_FLAG_FLATTENED;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005804 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
Eric Biederman00443072003-06-24 12:34:45 +00005805 internal_error(state, ins, "STRUCT_TYPE remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00005806 }
5807 if (ins->op == OP_DOT) {
Eric Biederman00443072003-06-24 12:34:45 +00005808 internal_error(state, ins, "OP_DOT remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00005809 }
5810 if (ins->op == OP_VAL_VEC) {
Eric Biederman00443072003-06-24 12:34:45 +00005811 internal_error(state, ins, "OP_VAL_VEC remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00005812 }
5813 ins = ins->next;
5814 } while(ins != first);
5815}
5816
Eric Biedermanb138ac82003-04-22 18:44:01 +00005817/* For those operations that cannot be simplified */
5818static void simplify_noop(struct compile_state *state, struct triple *ins)
5819{
5820 return;
5821}
5822
5823static void simplify_smul(struct compile_state *state, struct triple *ins)
5824{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005825 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005826 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005827 tmp = RHS(ins, 0);
5828 RHS(ins, 0) = RHS(ins, 1);
5829 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005830 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005831 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005832 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005833 left = read_sconst(ins, &RHS(ins, 0));
5834 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005835 mkconst(state, ins, left * right);
5836 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005837 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005838 mkconst(state, ins, 0);
5839 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005840 else if (is_one(RHS(ins, 1))) {
5841 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005842 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005843 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005844 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005845 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005846 ins->op = OP_SL;
5847 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005848 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005849 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005850 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005851 }
5852}
5853
5854static void simplify_umul(struct compile_state *state, struct triple *ins)
5855{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005856 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005857 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005858 tmp = RHS(ins, 0);
5859 RHS(ins, 0) = RHS(ins, 1);
5860 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005861 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005862 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005863 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005864 left = read_const(state, ins, &RHS(ins, 0));
5865 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005866 mkconst(state, ins, left * right);
5867 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005868 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005869 mkconst(state, ins, 0);
5870 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005871 else if (is_one(RHS(ins, 1))) {
5872 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005873 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005874 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005875 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005876 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005877 ins->op = OP_SL;
5878 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005879 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005880 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005881 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005882 }
5883}
5884
5885static void simplify_sdiv(struct compile_state *state, struct triple *ins)
5886{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005887 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005888 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005889 left = read_sconst(ins, &RHS(ins, 0));
5890 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005891 mkconst(state, ins, left / right);
5892 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005893 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005894 mkconst(state, ins, 0);
5895 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005896 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005897 error(state, ins, "division by zero");
5898 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005899 else if (is_one(RHS(ins, 1))) {
5900 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005901 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005902 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005903 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005904 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005905 ins->op = OP_SSR;
5906 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005907 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005908 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005909 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005910 }
5911}
5912
5913static void simplify_udiv(struct compile_state *state, struct triple *ins)
5914{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005915 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005916 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005917 left = read_const(state, ins, &RHS(ins, 0));
5918 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005919 mkconst(state, ins, left / right);
5920 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005921 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005922 mkconst(state, ins, 0);
5923 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005924 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005925 error(state, ins, "division by zero");
5926 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005927 else if (is_one(RHS(ins, 1))) {
5928 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005929 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005930 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005931 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005932 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005933 ins->op = OP_USR;
5934 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005935 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005936 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005937 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005938 }
5939}
5940
5941static void simplify_smod(struct compile_state *state, struct triple *ins)
5942{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005943 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005944 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005945 left = read_const(state, ins, &RHS(ins, 0));
5946 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005947 mkconst(state, ins, left % right);
5948 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005949 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005950 mkconst(state, ins, 0);
5951 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005952 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005953 error(state, ins, "division by zero");
5954 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005955 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005956 mkconst(state, ins, 0);
5957 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005958 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005959 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005960 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005961 ins->op = OP_AND;
5962 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005963 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005964 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005965 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005966 }
5967}
5968static void simplify_umod(struct compile_state *state, struct triple *ins)
5969{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005970 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005971 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005972 left = read_const(state, ins, &RHS(ins, 0));
5973 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005974 mkconst(state, ins, left % right);
5975 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005976 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005977 mkconst(state, ins, 0);
5978 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005979 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005980 error(state, ins, "division by zero");
5981 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005982 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005983 mkconst(state, ins, 0);
5984 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005985 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005986 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005987 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005988 ins->op = OP_AND;
5989 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005990 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005991 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005992 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005993 }
5994}
5995
5996static void simplify_add(struct compile_state *state, struct triple *ins)
5997{
5998 /* start with the pointer on the left */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005999 if (is_pointer(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006000 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006001 tmp = RHS(ins, 0);
6002 RHS(ins, 0) = RHS(ins, 1);
6003 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006004 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006005 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00006006 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006007 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006008 left = read_const(state, ins, &RHS(ins, 0));
6009 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006010 mkconst(state, ins, left + right);
6011 }
Eric Biederman530b5192003-07-01 10:05:30 +00006012 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006013 struct triple *sdecl;
6014 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006015 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006016 left = RHS(ins, 0)->u.cval;
6017 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006018 mkaddr_const(state, ins, sdecl, left + right);
6019 }
Eric Biederman530b5192003-07-01 10:05:30 +00006020 else {
6021 internal_warning(state, ins, "Optimize me!");
6022 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006023 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006024 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006025 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006026 tmp = RHS(ins, 1);
6027 RHS(ins, 1) = RHS(ins, 0);
6028 RHS(ins, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006029 }
6030}
6031
6032static void simplify_sub(struct compile_state *state, struct triple *ins)
6033{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006034 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00006035 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006036 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006037 left = read_const(state, ins, &RHS(ins, 0));
6038 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006039 mkconst(state, ins, left - right);
6040 }
Eric Biederman530b5192003-07-01 10:05:30 +00006041 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006042 struct triple *sdecl;
6043 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006044 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006045 left = RHS(ins, 0)->u.cval;
6046 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006047 mkaddr_const(state, ins, sdecl, left - right);
6048 }
Eric Biederman530b5192003-07-01 10:05:30 +00006049 else {
6050 internal_warning(state, ins, "Optimize me!");
6051 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006052 }
6053}
6054
6055static void simplify_sl(struct compile_state *state, struct triple *ins)
6056{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006057 if (is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006058 ulong_t right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006059 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006060 if (right >= (size_of(state, ins->type)*8)) {
6061 warning(state, ins, "left shift count >= width of type");
6062 }
6063 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006064 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006065 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006066 left = read_const(state, ins, &RHS(ins, 0));
6067 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006068 mkconst(state, ins, left << right);
6069 }
6070}
6071
6072static void simplify_usr(struct compile_state *state, struct triple *ins)
6073{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006074 if (is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006075 ulong_t right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006076 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006077 if (right >= (size_of(state, ins->type)*8)) {
6078 warning(state, ins, "right shift count >= width of type");
6079 }
6080 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006081 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006082 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006083 left = read_const(state, ins, &RHS(ins, 0));
6084 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006085 mkconst(state, ins, left >> right);
6086 }
6087}
6088
6089static void simplify_ssr(struct compile_state *state, struct triple *ins)
6090{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006091 if (is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006092 ulong_t right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006093 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006094 if (right >= (size_of(state, ins->type)*8)) {
6095 warning(state, ins, "right shift count >= width of type");
6096 }
6097 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006098 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006099 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006100 left = read_sconst(ins, &RHS(ins, 0));
6101 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006102 mkconst(state, ins, left >> right);
6103 }
6104}
6105
6106static void simplify_and(struct compile_state *state, struct triple *ins)
6107{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006108 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006109 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006110 left = read_const(state, ins, &RHS(ins, 0));
6111 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006112 mkconst(state, ins, left & right);
6113 }
6114}
6115
6116static void simplify_or(struct compile_state *state, struct triple *ins)
6117{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006118 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006119 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006120 left = read_const(state, ins, &RHS(ins, 0));
6121 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006122 mkconst(state, ins, left | right);
6123 }
6124}
6125
6126static void simplify_xor(struct compile_state *state, struct triple *ins)
6127{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006128 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006129 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006130 left = read_const(state, ins, &RHS(ins, 0));
6131 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006132 mkconst(state, ins, left ^ right);
6133 }
6134}
6135
6136static void simplify_pos(struct compile_state *state, struct triple *ins)
6137{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006138 if (is_const(RHS(ins, 0))) {
6139 mkconst(state, ins, RHS(ins, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006140 }
6141 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006142 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006143 }
6144}
6145
6146static void simplify_neg(struct compile_state *state, struct triple *ins)
6147{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006148 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006149 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006150 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006151 mkconst(state, ins, -left);
6152 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006153 else if (RHS(ins, 0)->op == OP_NEG) {
6154 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006155 }
6156}
6157
6158static void simplify_invert(struct compile_state *state, struct triple *ins)
6159{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006160 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006161 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006162 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006163 mkconst(state, ins, ~left);
6164 }
6165}
6166
6167static void simplify_eq(struct compile_state *state, struct triple *ins)
6168{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006169 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006170 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006171 left = read_const(state, ins, &RHS(ins, 0));
6172 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006173 mkconst(state, ins, left == right);
6174 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006175 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006176 mkconst(state, ins, 1);
6177 }
6178}
6179
6180static void simplify_noteq(struct compile_state *state, struct triple *ins)
6181{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006182 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006183 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006184 left = read_const(state, ins, &RHS(ins, 0));
6185 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006186 mkconst(state, ins, left != right);
6187 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006188 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006189 mkconst(state, ins, 0);
6190 }
6191}
6192
6193static void simplify_sless(struct compile_state *state, struct triple *ins)
6194{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006195 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006196 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006197 left = read_sconst(ins, &RHS(ins, 0));
6198 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006199 mkconst(state, ins, left < right);
6200 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006201 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006202 mkconst(state, ins, 0);
6203 }
6204}
6205
6206static void simplify_uless(struct compile_state *state, struct triple *ins)
6207{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006208 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006209 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006210 left = read_const(state, ins, &RHS(ins, 0));
6211 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006212 mkconst(state, ins, left < right);
6213 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006214 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006215 mkconst(state, ins, 1);
6216 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006217 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006218 mkconst(state, ins, 0);
6219 }
6220}
6221
6222static void simplify_smore(struct compile_state *state, struct triple *ins)
6223{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006224 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006225 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006226 left = read_sconst(ins, &RHS(ins, 0));
6227 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006228 mkconst(state, ins, left > right);
6229 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006230 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006231 mkconst(state, ins, 0);
6232 }
6233}
6234
6235static void simplify_umore(struct compile_state *state, struct triple *ins)
6236{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006237 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006238 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006239 left = read_const(state, ins, &RHS(ins, 0));
6240 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006241 mkconst(state, ins, left > right);
6242 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006243 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006244 mkconst(state, ins, 1);
6245 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006246 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006247 mkconst(state, ins, 0);
6248 }
6249}
6250
6251
6252static void simplify_slesseq(struct compile_state *state, struct triple *ins)
6253{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006254 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006255 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006256 left = read_sconst(ins, &RHS(ins, 0));
6257 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006258 mkconst(state, ins, left <= right);
6259 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006260 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006261 mkconst(state, ins, 1);
6262 }
6263}
6264
6265static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
6266{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006267 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006268 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006269 left = read_const(state, ins, &RHS(ins, 0));
6270 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006271 mkconst(state, ins, left <= right);
6272 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006273 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006274 mkconst(state, ins, 1);
6275 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006276 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006277 mkconst(state, ins, 1);
6278 }
6279}
6280
6281static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
6282{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006283 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006284 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006285 left = read_sconst(ins, &RHS(ins, 0));
6286 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006287 mkconst(state, ins, left >= right);
6288 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006289 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006290 mkconst(state, ins, 1);
6291 }
6292}
6293
6294static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
6295{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006296 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006297 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006298 left = read_const(state, ins, &RHS(ins, 0));
6299 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006300 mkconst(state, ins, left >= right);
6301 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006302 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006303 mkconst(state, ins, 1);
6304 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006305 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006306 mkconst(state, ins, 1);
6307 }
6308}
6309
6310static void simplify_lfalse(struct compile_state *state, struct triple *ins)
6311{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006312 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006313 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006314 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006315 mkconst(state, ins, left == 0);
6316 }
6317 /* Otherwise if I am the only user... */
Eric Biederman0babc1c2003-05-09 02:39:00 +00006318 else if ((RHS(ins, 0)->use->member == ins) && (RHS(ins, 0)->use->next == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006319 int need_copy = 1;
6320 /* Invert a boolean operation */
Eric Biederman0babc1c2003-05-09 02:39:00 +00006321 switch(RHS(ins, 0)->op) {
6322 case OP_LTRUE: RHS(ins, 0)->op = OP_LFALSE; break;
6323 case OP_LFALSE: RHS(ins, 0)->op = OP_LTRUE; break;
6324 case OP_EQ: RHS(ins, 0)->op = OP_NOTEQ; break;
6325 case OP_NOTEQ: RHS(ins, 0)->op = OP_EQ; break;
6326 case OP_SLESS: RHS(ins, 0)->op = OP_SMOREEQ; break;
6327 case OP_ULESS: RHS(ins, 0)->op = OP_UMOREEQ; break;
6328 case OP_SMORE: RHS(ins, 0)->op = OP_SLESSEQ; break;
6329 case OP_UMORE: RHS(ins, 0)->op = OP_ULESSEQ; break;
6330 case OP_SLESSEQ: RHS(ins, 0)->op = OP_SMORE; break;
6331 case OP_ULESSEQ: RHS(ins, 0)->op = OP_UMORE; break;
6332 case OP_SMOREEQ: RHS(ins, 0)->op = OP_SLESS; break;
6333 case OP_UMOREEQ: RHS(ins, 0)->op = OP_ULESS; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006334 default:
6335 need_copy = 0;
6336 break;
6337 }
6338 if (need_copy) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006339 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006340 }
6341 }
6342}
6343
6344static void simplify_ltrue (struct compile_state *state, struct triple *ins)
6345{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006346 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006347 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006348 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006349 mkconst(state, ins, left != 0);
6350 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006351 else switch(RHS(ins, 0)->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006352 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
6353 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
6354 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
Eric Biederman0babc1c2003-05-09 02:39:00 +00006355 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006356 }
6357
6358}
6359
6360static void simplify_copy(struct compile_state *state, struct triple *ins)
6361{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006362 if (is_const(RHS(ins, 0))) {
6363 switch(RHS(ins, 0)->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006364 case OP_INTCONST:
6365 {
6366 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006367 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006368 mkconst(state, ins, left);
6369 break;
6370 }
6371 case OP_ADDRCONST:
6372 {
6373 struct triple *sdecl;
6374 ulong_t offset;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006375 sdecl = MISC(RHS(ins, 0), 0);
6376 offset = RHS(ins, 0)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006377 mkaddr_const(state, ins, sdecl, offset);
6378 break;
6379 }
6380 default:
6381 internal_error(state, ins, "uknown constant");
6382 break;
6383 }
6384 }
6385}
6386
Eric Biedermanb138ac82003-04-22 18:44:01 +00006387static void simplify_branch(struct compile_state *state, struct triple *ins)
6388{
6389 struct block *block;
6390 if (ins->op != OP_BRANCH) {
6391 internal_error(state, ins, "not branch");
6392 }
6393 if (ins->use != 0) {
6394 internal_error(state, ins, "branch use");
6395 }
6396#warning "FIXME implement simplify branch."
6397 /* The challenge here with simplify branch is that I need to
6398 * make modifications to the control flow graph as well
6399 * as to the branch instruction itself.
6400 */
6401 block = ins->u.block;
6402
Eric Biederman0babc1c2003-05-09 02:39:00 +00006403 if (TRIPLE_RHS(ins->sizes) && is_const(RHS(ins, 0))) {
6404 struct triple *targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006405 ulong_t value;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006406 value = read_const(state, ins, &RHS(ins, 0));
6407 unuse_triple(RHS(ins, 0), ins);
6408 targ = TARG(ins, 0);
6409 ins->sizes = TRIPLE_SIZES(0, 0, 0, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006410 if (value) {
6411 unuse_triple(ins->next, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006412 TARG(ins, 0) = targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006413 }
6414 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006415 unuse_triple(targ, ins);
6416 TARG(ins, 0) = ins->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006417 }
6418#warning "FIXME handle the case of making a branch unconditional"
6419 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006420 if (TARG(ins, 0) == ins->next) {
6421 unuse_triple(ins->next, ins);
6422 if (TRIPLE_RHS(ins->sizes)) {
6423 unuse_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006424 unuse_triple(ins->next, ins);
6425 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006426 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
6427 ins->op = OP_NOOP;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006428 if (ins->use) {
6429 internal_error(state, ins, "noop use != 0");
6430 }
6431#warning "FIXME handle the case of killing a branch"
6432 }
6433}
6434
Eric Biederman530b5192003-07-01 10:05:30 +00006435int phi_present(struct block *block)
6436{
6437 struct triple *ptr;
6438 if (!block) {
6439 return 0;
6440 }
6441 ptr = block->first;
6442 do {
6443 if (ptr->op == OP_PHI) {
6444 return 1;
6445 }
6446 ptr = ptr->next;
6447 } while(ptr != block->last);
6448 return 0;
6449}
6450
6451static void simplify_label(struct compile_state *state, struct triple *ins)
6452{
6453#warning "FIXME enable simplify_label"
6454 struct triple *first, *last;
6455 first = RHS(state->main_function, 0);
6456 last = first->prev;
6457 /* Ignore the first and last instructions */
6458 if ((ins == first) || (ins == last)) {
6459 return;
6460 }
6461 if (ins->use == 0) {
6462 ins->op = OP_NOOP;
6463 }
6464 else if (ins->prev->op == OP_LABEL) {
6465 struct block *block;
6466 block = ins->prev->u.block;
6467 /* In general it is not safe to merge one label that
6468 * imediately follows another. The problem is that the empty
6469 * looking block may have phi functions that depend on it.
6470 */
6471 if (!block ||
6472 (!phi_present(block->left) &&
6473 !phi_present(block->right)))
6474 {
6475 struct triple_set *user, *next;
6476 ins->op = OP_NOOP;
6477 for(user = ins->use; user; user = next) {
6478 struct triple *use;
6479 next = user->next;
6480 use = user->member;
6481 if (TARG(use, 0) == ins) {
6482 TARG(use, 0) = ins->prev;
6483 unuse_triple(ins, use);
6484 use_triple(ins->prev, use);
6485 }
6486 }
6487 if (ins->use) {
6488 internal_error(state, ins, "noop use != 0");
6489 }
6490 }
6491 }
6492}
6493
Eric Biedermanb138ac82003-04-22 18:44:01 +00006494static void simplify_phi(struct compile_state *state, struct triple *ins)
6495{
6496 struct triple **expr;
6497 ulong_t value;
6498 expr = triple_rhs(state, ins, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006499 if (!*expr || !is_const(*expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006500 return;
6501 }
6502 value = read_const(state, ins, expr);
6503 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006504 if (!*expr || !is_const(*expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006505 return;
6506 }
6507 if (value != read_const(state, ins, expr)) {
6508 return;
6509 }
6510 }
6511 mkconst(state, ins, value);
6512}
6513
6514
6515static void simplify_bsf(struct compile_state *state, struct triple *ins)
6516{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006517 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006518 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006519 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006520 mkconst(state, ins, bsf(left));
6521 }
6522}
6523
6524static void simplify_bsr(struct compile_state *state, struct triple *ins)
6525{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006526 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006527 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006528 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006529 mkconst(state, ins, bsr(left));
6530 }
6531}
6532
6533
6534typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
6535static const simplify_t table_simplify[] = {
Eric Biederman530b5192003-07-01 10:05:30 +00006536#if 1
6537#define simplify_sdivt simplify_noop
6538#define simplify_udivt simplify_noop
6539#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00006540#if 0
6541#define simplify_smul simplify_noop
6542#define simplify_umul simplify_noop
6543#define simplify_sdiv simplify_noop
6544#define simplify_udiv simplify_noop
6545#define simplify_smod simplify_noop
6546#define simplify_umod simplify_noop
6547#endif
6548#if 0
6549#define simplify_add simplify_noop
6550#define simplify_sub simplify_noop
6551#endif
6552#if 0
6553#define simplify_sl simplify_noop
6554#define simplify_usr simplify_noop
6555#define simplify_ssr simplify_noop
6556#endif
6557#if 0
6558#define simplify_and simplify_noop
6559#define simplify_xor simplify_noop
6560#define simplify_or simplify_noop
6561#endif
6562#if 0
6563#define simplify_pos simplify_noop
6564#define simplify_neg simplify_noop
6565#define simplify_invert simplify_noop
6566#endif
6567
6568#if 0
6569#define simplify_eq simplify_noop
6570#define simplify_noteq simplify_noop
6571#endif
6572#if 0
6573#define simplify_sless simplify_noop
6574#define simplify_uless simplify_noop
6575#define simplify_smore simplify_noop
6576#define simplify_umore simplify_noop
6577#endif
6578#if 0
6579#define simplify_slesseq simplify_noop
6580#define simplify_ulesseq simplify_noop
6581#define simplify_smoreeq simplify_noop
6582#define simplify_umoreeq simplify_noop
6583#endif
6584#if 0
6585#define simplify_lfalse simplify_noop
6586#endif
6587#if 0
6588#define simplify_ltrue simplify_noop
6589#endif
6590
6591#if 0
6592#define simplify_copy simplify_noop
6593#endif
6594
6595#if 0
Eric Biedermanb138ac82003-04-22 18:44:01 +00006596#define simplify_branch simplify_noop
6597#endif
Eric Biederman530b5192003-07-01 10:05:30 +00006598#if 1
6599#define simplify_label simplify_noop
6600#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00006601
6602#if 0
6603#define simplify_phi simplify_noop
6604#endif
6605
6606#if 0
6607#define simplify_bsf simplify_noop
6608#define simplify_bsr simplify_noop
6609#endif
6610
Eric Biederman530b5192003-07-01 10:05:30 +00006611[OP_SDIVT ] = simplify_sdivt,
6612[OP_UDIVT ] = simplify_udivt,
Eric Biedermanb138ac82003-04-22 18:44:01 +00006613[OP_SMUL ] = simplify_smul,
6614[OP_UMUL ] = simplify_umul,
6615[OP_SDIV ] = simplify_sdiv,
6616[OP_UDIV ] = simplify_udiv,
6617[OP_SMOD ] = simplify_smod,
6618[OP_UMOD ] = simplify_umod,
6619[OP_ADD ] = simplify_add,
6620[OP_SUB ] = simplify_sub,
6621[OP_SL ] = simplify_sl,
6622[OP_USR ] = simplify_usr,
6623[OP_SSR ] = simplify_ssr,
6624[OP_AND ] = simplify_and,
6625[OP_XOR ] = simplify_xor,
6626[OP_OR ] = simplify_or,
6627[OP_POS ] = simplify_pos,
6628[OP_NEG ] = simplify_neg,
6629[OP_INVERT ] = simplify_invert,
6630
6631[OP_EQ ] = simplify_eq,
6632[OP_NOTEQ ] = simplify_noteq,
6633[OP_SLESS ] = simplify_sless,
6634[OP_ULESS ] = simplify_uless,
6635[OP_SMORE ] = simplify_smore,
6636[OP_UMORE ] = simplify_umore,
6637[OP_SLESSEQ ] = simplify_slesseq,
6638[OP_ULESSEQ ] = simplify_ulesseq,
6639[OP_SMOREEQ ] = simplify_smoreeq,
6640[OP_UMOREEQ ] = simplify_umoreeq,
6641[OP_LFALSE ] = simplify_lfalse,
6642[OP_LTRUE ] = simplify_ltrue,
6643
6644[OP_LOAD ] = simplify_noop,
6645[OP_STORE ] = simplify_noop,
6646
6647[OP_NOOP ] = simplify_noop,
6648
6649[OP_INTCONST ] = simplify_noop,
6650[OP_BLOBCONST ] = simplify_noop,
6651[OP_ADDRCONST ] = simplify_noop,
6652
6653[OP_WRITE ] = simplify_noop,
6654[OP_READ ] = simplify_noop,
6655[OP_COPY ] = simplify_copy,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006656[OP_PIECE ] = simplify_noop,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006657[OP_ASM ] = simplify_noop,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006658
6659[OP_DOT ] = simplify_noop,
6660[OP_VAL_VEC ] = simplify_noop,
Eric Biedermanb138ac82003-04-22 18:44:01 +00006661
6662[OP_LIST ] = simplify_noop,
6663[OP_BRANCH ] = simplify_branch,
Eric Biederman530b5192003-07-01 10:05:30 +00006664[OP_LABEL ] = simplify_label,
Eric Biedermanb138ac82003-04-22 18:44:01 +00006665[OP_ADECL ] = simplify_noop,
6666[OP_SDECL ] = simplify_noop,
6667[OP_PHI ] = simplify_phi,
6668
6669[OP_INB ] = simplify_noop,
6670[OP_INW ] = simplify_noop,
6671[OP_INL ] = simplify_noop,
6672[OP_OUTB ] = simplify_noop,
6673[OP_OUTW ] = simplify_noop,
6674[OP_OUTL ] = simplify_noop,
6675[OP_BSF ] = simplify_bsf,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006676[OP_BSR ] = simplify_bsr,
6677[OP_RDMSR ] = simplify_noop,
6678[OP_WRMSR ] = simplify_noop,
6679[OP_HLT ] = simplify_noop,
Eric Biedermanb138ac82003-04-22 18:44:01 +00006680};
6681
6682static void simplify(struct compile_state *state, struct triple *ins)
6683{
6684 int op;
6685 simplify_t do_simplify;
6686 do {
6687 op = ins->op;
6688 do_simplify = 0;
6689 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
6690 do_simplify = 0;
6691 }
6692 else {
6693 do_simplify = table_simplify[op];
6694 }
6695 if (!do_simplify) {
6696 internal_error(state, ins, "cannot simplify op: %d %s\n",
6697 op, tops(op));
6698 return;
6699 }
6700 do_simplify(state, ins);
6701 } while(ins->op != op);
6702}
6703
6704static void simplify_all(struct compile_state *state)
6705{
6706 struct triple *ins, *first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006707 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006708 ins = first;
6709 do {
6710 simplify(state, ins);
6711 ins = ins->next;
Eric Biederman530b5192003-07-01 10:05:30 +00006712 }while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006713}
6714
6715/*
6716 * Builtins....
6717 * ============================
6718 */
6719
Eric Biederman0babc1c2003-05-09 02:39:00 +00006720static void register_builtin_function(struct compile_state *state,
6721 const char *name, int op, struct type *rtype, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00006722{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006723 struct type *ftype, *atype, *param, **next;
6724 struct triple *def, *arg, *result, *work, *last, *first;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006725 struct hash_entry *ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006726 struct file_state file;
6727 int parameters;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006728 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006729 va_list args;
6730 int i;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006731
6732 /* Dummy file state to get debug handling right */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006733 memset(&file, 0, sizeof(file));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00006734 file.basename = "<built-in>";
Eric Biedermanb138ac82003-04-22 18:44:01 +00006735 file.line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00006736 file.report_line = 1;
6737 file.report_name = file.basename;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006738 file.prev = state->file;
6739 state->file = &file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00006740 state->function = name;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006741
6742 /* Find the Parameter count */
6743 valid_op(state, op);
6744 parameters = table_ops[op].rhs;
6745 if (parameters < 0 ) {
6746 internal_error(state, 0, "Invalid builtin parameter count");
6747 }
6748
6749 /* Find the function type */
6750 ftype = new_type(TYPE_FUNCTION, rtype, 0);
6751 next = &ftype->right;
6752 va_start(args, rtype);
6753 for(i = 0; i < parameters; i++) {
6754 atype = va_arg(args, struct type *);
6755 if (!*next) {
6756 *next = atype;
6757 } else {
6758 *next = new_type(TYPE_PRODUCT, *next, atype);
6759 next = &((*next)->right);
6760 }
6761 }
6762 if (!*next) {
6763 *next = &void_type;
6764 }
6765 va_end(args);
6766
Eric Biedermanb138ac82003-04-22 18:44:01 +00006767 /* Generate the needed triples */
6768 def = triple(state, OP_LIST, ftype, 0, 0);
6769 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006770 RHS(def, 0) = first;
6771
6772 /* Now string them together */
6773 param = ftype->right;
6774 for(i = 0; i < parameters; i++) {
6775 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6776 atype = param->left;
6777 } else {
6778 atype = param;
6779 }
6780 arg = flatten(state, first, variable(state, atype));
6781 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006782 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006783 result = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006784 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006785 result = flatten(state, first, variable(state, rtype));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006786 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006787 MISC(def, 0) = result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006788 work = new_triple(state, op, rtype, -1, parameters);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006789 for(i = 0, arg = first->next; i < parameters; i++, arg = arg->next) {
6790 RHS(work, i) = read_expr(state, arg);
6791 }
6792 if (result && ((rtype->type & TYPE_MASK) == TYPE_STRUCT)) {
6793 struct triple *val;
6794 /* Populate the LHS with the target registers */
6795 work = flatten(state, first, work);
6796 work->type = &void_type;
6797 param = rtype->left;
6798 if (rtype->elements != TRIPLE_LHS(work->sizes)) {
6799 internal_error(state, 0, "Invalid result type");
6800 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006801 val = new_triple(state, OP_VAL_VEC, rtype, -1, -1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006802 for(i = 0; i < rtype->elements; i++) {
6803 struct triple *piece;
6804 atype = param;
6805 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6806 atype = param->left;
6807 }
6808 if (!TYPE_ARITHMETIC(atype->type) &&
6809 !TYPE_PTR(atype->type)) {
6810 internal_error(state, 0, "Invalid lhs type");
6811 }
6812 piece = triple(state, OP_PIECE, atype, work, 0);
6813 piece->u.cval = i;
6814 LHS(work, i) = piece;
6815 RHS(val, i) = piece;
6816 }
6817 work = val;
6818 }
6819 if (result) {
6820 work = write_expr(state, result, work);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006821 }
6822 work = flatten(state, first, work);
6823 last = flatten(state, first, label(state));
6824 name_len = strlen(name);
6825 ident = lookup(state, name, name_len);
6826 symbol(state, ident, &ident->sym_ident, def, ftype);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006827
Eric Biedermanb138ac82003-04-22 18:44:01 +00006828 state->file = file.prev;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00006829 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006830#if 0
6831 fprintf(stdout, "\n");
6832 loc(stdout, state, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006833 fprintf(stdout, "\n__________ builtin_function _________\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006834 print_triple(state, def);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006835 fprintf(stdout, "__________ builtin_function _________ done\n\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006836#endif
6837}
6838
Eric Biederman0babc1c2003-05-09 02:39:00 +00006839static struct type *partial_struct(struct compile_state *state,
6840 const char *field_name, struct type *type, struct type *rest)
Eric Biedermanb138ac82003-04-22 18:44:01 +00006841{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006842 struct hash_entry *field_ident;
6843 struct type *result;
6844 int field_name_len;
6845
6846 field_name_len = strlen(field_name);
6847 field_ident = lookup(state, field_name, field_name_len);
6848
6849 result = clone_type(0, type);
6850 result->field_ident = field_ident;
6851
6852 if (rest) {
6853 result = new_type(TYPE_PRODUCT, result, rest);
6854 }
6855 return result;
6856}
6857
6858static struct type *register_builtin_type(struct compile_state *state,
6859 const char *name, struct type *type)
6860{
Eric Biedermanb138ac82003-04-22 18:44:01 +00006861 struct hash_entry *ident;
6862 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006863
Eric Biedermanb138ac82003-04-22 18:44:01 +00006864 name_len = strlen(name);
6865 ident = lookup(state, name, name_len);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006866
6867 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6868 ulong_t elements = 0;
6869 struct type *field;
6870 type = new_type(TYPE_STRUCT, type, 0);
6871 field = type->left;
6872 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
6873 elements++;
6874 field = field->right;
6875 }
6876 elements++;
6877 symbol(state, ident, &ident->sym_struct, 0, type);
6878 type->type_ident = ident;
6879 type->elements = elements;
6880 }
6881 symbol(state, ident, &ident->sym_ident, 0, type);
6882 ident->tok = TOK_TYPE_NAME;
6883 return type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006884}
6885
Eric Biederman0babc1c2003-05-09 02:39:00 +00006886
Eric Biedermanb138ac82003-04-22 18:44:01 +00006887static void register_builtins(struct compile_state *state)
6888{
Eric Biederman530b5192003-07-01 10:05:30 +00006889 struct type *div_type, *ldiv_type;
6890 struct type *udiv_type, *uldiv_type;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006891 struct type *msr_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006892
Eric Biederman530b5192003-07-01 10:05:30 +00006893 div_type = register_builtin_type(state, "__builtin_div_t",
6894 partial_struct(state, "quot", &int_type,
6895 partial_struct(state, "rem", &int_type, 0)));
6896 ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
6897 partial_struct(state, "quot", &long_type,
6898 partial_struct(state, "rem", &long_type, 0)));
6899 udiv_type = register_builtin_type(state, "__builtin_udiv_t",
6900 partial_struct(state, "quot", &uint_type,
6901 partial_struct(state, "rem", &uint_type, 0)));
6902 uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
6903 partial_struct(state, "quot", &ulong_type,
6904 partial_struct(state, "rem", &ulong_type, 0)));
6905
6906 register_builtin_function(state, "__builtin_div", OP_SDIVT, div_type,
6907 &int_type, &int_type);
6908 register_builtin_function(state, "__builtin_ldiv", OP_SDIVT, ldiv_type,
6909 &long_type, &long_type);
6910 register_builtin_function(state, "__builtin_udiv", OP_UDIVT, udiv_type,
6911 &uint_type, &uint_type);
6912 register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
6913 &ulong_type, &ulong_type);
6914
Eric Biederman0babc1c2003-05-09 02:39:00 +00006915 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
6916 &ushort_type);
6917 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
6918 &ushort_type);
6919 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
6920 &ushort_type);
6921
6922 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
6923 &uchar_type, &ushort_type);
6924 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
6925 &ushort_type, &ushort_type);
6926 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
6927 &uint_type, &ushort_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006928
Eric Biederman0babc1c2003-05-09 02:39:00 +00006929 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
6930 &int_type);
6931 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
6932 &int_type);
6933
6934 msr_type = register_builtin_type(state, "__builtin_msr_t",
6935 partial_struct(state, "lo", &ulong_type,
6936 partial_struct(state, "hi", &ulong_type, 0)));
6937
6938 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
6939 &ulong_type);
6940 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
6941 &ulong_type, &ulong_type, &ulong_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006942
Eric Biederman0babc1c2003-05-09 02:39:00 +00006943 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
6944 &void_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006945}
6946
6947static struct type *declarator(
6948 struct compile_state *state, struct type *type,
6949 struct hash_entry **ident, int need_ident);
6950static void decl(struct compile_state *state, struct triple *first);
6951static struct type *specifier_qualifier_list(struct compile_state *state);
6952static int isdecl_specifier(int tok);
6953static struct type *decl_specifiers(struct compile_state *state);
6954static int istype(int tok);
6955static struct triple *expr(struct compile_state *state);
6956static struct triple *assignment_expr(struct compile_state *state);
6957static struct type *type_name(struct compile_state *state);
6958static void statement(struct compile_state *state, struct triple *fist);
6959
6960static struct triple *call_expr(
6961 struct compile_state *state, struct triple *func)
6962{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006963 struct triple *def;
6964 struct type *param, *type;
6965 ulong_t pvals, index;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006966
6967 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
6968 error(state, 0, "Called object is not a function");
6969 }
6970 if (func->op != OP_LIST) {
6971 internal_error(state, 0, "improper function");
6972 }
6973 eat(state, TOK_LPAREN);
6974 /* Find the return type without any specifiers */
6975 type = clone_type(0, func->type->left);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006976 def = new_triple(state, OP_CALL, func->type, -1, -1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006977 def->type = type;
6978
6979 pvals = TRIPLE_RHS(def->sizes);
6980 MISC(def, 0) = func;
6981
6982 param = func->type->right;
6983 for(index = 0; index < pvals; index++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006984 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006985 struct type *arg_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006986 val = read_expr(state, assignment_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006987 arg_type = param;
6988 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6989 arg_type = param->left;
6990 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006991 write_compatible(state, arg_type, val->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006992 RHS(def, index) = val;
6993 if (index != (pvals - 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006994 eat(state, TOK_COMMA);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006995 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006996 }
6997 }
6998 eat(state, TOK_RPAREN);
6999 return def;
7000}
7001
7002
7003static struct triple *character_constant(struct compile_state *state)
7004{
7005 struct triple *def;
7006 struct token *tk;
7007 const signed char *str, *end;
7008 int c;
7009 int str_len;
7010 eat(state, TOK_LIT_CHAR);
7011 tk = &state->token[0];
7012 str = tk->val.str + 1;
7013 str_len = tk->str_len - 2;
7014 if (str_len <= 0) {
7015 error(state, 0, "empty character constant");
7016 }
7017 end = str + str_len;
7018 c = char_value(state, &str, end);
7019 if (str != end) {
7020 error(state, 0, "multibyte character constant not supported");
7021 }
7022 def = int_const(state, &char_type, (ulong_t)((long_t)c));
7023 return def;
7024}
7025
7026static struct triple *string_constant(struct compile_state *state)
7027{
7028 struct triple *def;
7029 struct token *tk;
7030 struct type *type;
7031 const signed char *str, *end;
7032 signed char *buf, *ptr;
7033 int str_len;
7034
7035 buf = 0;
7036 type = new_type(TYPE_ARRAY, &char_type, 0);
7037 type->elements = 0;
7038 /* The while loop handles string concatenation */
7039 do {
7040 eat(state, TOK_LIT_STRING);
7041 tk = &state->token[0];
7042 str = tk->val.str + 1;
7043 str_len = tk->str_len - 2;
Eric Biedermanab2ea6b2003-04-26 03:20:53 +00007044 if (str_len < 0) {
7045 error(state, 0, "negative string constant length");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007046 }
7047 end = str + str_len;
7048 ptr = buf;
7049 buf = xmalloc(type->elements + str_len + 1, "string_constant");
7050 memcpy(buf, ptr, type->elements);
7051 ptr = buf + type->elements;
7052 do {
7053 *ptr++ = char_value(state, &str, end);
7054 } while(str < end);
7055 type->elements = ptr - buf;
7056 } while(peek(state) == TOK_LIT_STRING);
7057 *ptr = '\0';
7058 type->elements += 1;
7059 def = triple(state, OP_BLOBCONST, type, 0, 0);
7060 def->u.blob = buf;
7061 return def;
7062}
7063
7064
7065static struct triple *integer_constant(struct compile_state *state)
7066{
7067 struct triple *def;
7068 unsigned long val;
7069 struct token *tk;
7070 char *end;
7071 int u, l, decimal;
7072 struct type *type;
7073
7074 eat(state, TOK_LIT_INT);
7075 tk = &state->token[0];
7076 errno = 0;
7077 decimal = (tk->val.str[0] != '0');
7078 val = strtoul(tk->val.str, &end, 0);
7079 if ((val == ULONG_MAX) && (errno == ERANGE)) {
7080 error(state, 0, "Integer constant to large");
7081 }
7082 u = l = 0;
7083 if ((*end == 'u') || (*end == 'U')) {
7084 u = 1;
7085 end++;
7086 }
7087 if ((*end == 'l') || (*end == 'L')) {
7088 l = 1;
7089 end++;
7090 }
7091 if ((*end == 'u') || (*end == 'U')) {
7092 u = 1;
7093 end++;
7094 }
7095 if (*end) {
7096 error(state, 0, "Junk at end of integer constant");
7097 }
7098 if (u && l) {
7099 type = &ulong_type;
7100 }
7101 else if (l) {
7102 type = &long_type;
7103 if (!decimal && (val > LONG_MAX)) {
7104 type = &ulong_type;
7105 }
7106 }
7107 else if (u) {
7108 type = &uint_type;
7109 if (val > UINT_MAX) {
7110 type = &ulong_type;
7111 }
7112 }
7113 else {
7114 type = &int_type;
7115 if (!decimal && (val > INT_MAX) && (val <= UINT_MAX)) {
7116 type = &uint_type;
7117 }
7118 else if (!decimal && (val > LONG_MAX)) {
7119 type = &ulong_type;
7120 }
7121 else if (val > INT_MAX) {
7122 type = &long_type;
7123 }
7124 }
7125 def = int_const(state, type, val);
7126 return def;
7127}
7128
7129static struct triple *primary_expr(struct compile_state *state)
7130{
7131 struct triple *def;
7132 int tok;
7133 tok = peek(state);
7134 switch(tok) {
7135 case TOK_IDENT:
7136 {
7137 struct hash_entry *ident;
7138 /* Here ident is either:
7139 * a varable name
7140 * a function name
7141 * an enumeration constant.
7142 */
7143 eat(state, TOK_IDENT);
7144 ident = state->token[0].ident;
7145 if (!ident->sym_ident) {
7146 error(state, 0, "%s undeclared", ident->name);
7147 }
7148 def = ident->sym_ident->def;
7149 break;
7150 }
7151 case TOK_ENUM_CONST:
7152 /* Here ident is an enumeration constant */
7153 eat(state, TOK_ENUM_CONST);
7154 def = 0;
7155 FINISHME();
7156 break;
7157 case TOK_LPAREN:
7158 eat(state, TOK_LPAREN);
7159 def = expr(state);
7160 eat(state, TOK_RPAREN);
7161 break;
7162 case TOK_LIT_INT:
7163 def = integer_constant(state);
7164 break;
7165 case TOK_LIT_FLOAT:
7166 eat(state, TOK_LIT_FLOAT);
7167 error(state, 0, "Floating point constants not supported");
7168 def = 0;
7169 FINISHME();
7170 break;
7171 case TOK_LIT_CHAR:
7172 def = character_constant(state);
7173 break;
7174 case TOK_LIT_STRING:
7175 def = string_constant(state);
7176 break;
7177 default:
7178 def = 0;
7179 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
7180 }
7181 return def;
7182}
7183
7184static struct triple *postfix_expr(struct compile_state *state)
7185{
7186 struct triple *def;
7187 int postfix;
7188 def = primary_expr(state);
7189 do {
7190 struct triple *left;
7191 int tok;
7192 postfix = 1;
7193 left = def;
7194 switch((tok = peek(state))) {
7195 case TOK_LBRACKET:
7196 eat(state, TOK_LBRACKET);
7197 def = mk_subscript_expr(state, left, expr(state));
7198 eat(state, TOK_RBRACKET);
7199 break;
7200 case TOK_LPAREN:
7201 def = call_expr(state, def);
7202 break;
7203 case TOK_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007204 {
7205 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007206 eat(state, TOK_DOT);
7207 eat(state, TOK_IDENT);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007208 field = state->token[0].ident;
7209 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007210 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007211 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007212 case TOK_ARROW:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007213 {
7214 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007215 eat(state, TOK_ARROW);
7216 eat(state, TOK_IDENT);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007217 field = state->token[0].ident;
7218 def = mk_deref_expr(state, read_expr(state, def));
7219 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007220 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007221 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007222 case TOK_PLUSPLUS:
7223 eat(state, TOK_PLUSPLUS);
7224 def = mk_post_inc_expr(state, left);
7225 break;
7226 case TOK_MINUSMINUS:
7227 eat(state, TOK_MINUSMINUS);
7228 def = mk_post_dec_expr(state, left);
7229 break;
7230 default:
7231 postfix = 0;
7232 break;
7233 }
7234 } while(postfix);
7235 return def;
7236}
7237
7238static struct triple *cast_expr(struct compile_state *state);
7239
7240static struct triple *unary_expr(struct compile_state *state)
7241{
7242 struct triple *def, *right;
7243 int tok;
7244 switch((tok = peek(state))) {
7245 case TOK_PLUSPLUS:
7246 eat(state, TOK_PLUSPLUS);
7247 def = mk_pre_inc_expr(state, unary_expr(state));
7248 break;
7249 case TOK_MINUSMINUS:
7250 eat(state, TOK_MINUSMINUS);
7251 def = mk_pre_dec_expr(state, unary_expr(state));
7252 break;
7253 case TOK_AND:
7254 eat(state, TOK_AND);
7255 def = mk_addr_expr(state, cast_expr(state), 0);
7256 break;
7257 case TOK_STAR:
7258 eat(state, TOK_STAR);
7259 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
7260 break;
7261 case TOK_PLUS:
7262 eat(state, TOK_PLUS);
7263 right = read_expr(state, cast_expr(state));
7264 arithmetic(state, right);
7265 def = integral_promotion(state, right);
7266 break;
7267 case TOK_MINUS:
7268 eat(state, TOK_MINUS);
7269 right = read_expr(state, cast_expr(state));
7270 arithmetic(state, right);
7271 def = integral_promotion(state, right);
7272 def = triple(state, OP_NEG, def->type, def, 0);
7273 break;
7274 case TOK_TILDE:
7275 eat(state, TOK_TILDE);
7276 right = read_expr(state, cast_expr(state));
7277 integral(state, right);
7278 def = integral_promotion(state, right);
7279 def = triple(state, OP_INVERT, def->type, def, 0);
7280 break;
7281 case TOK_BANG:
7282 eat(state, TOK_BANG);
7283 right = read_expr(state, cast_expr(state));
7284 bool(state, right);
7285 def = lfalse_expr(state, right);
7286 break;
7287 case TOK_SIZEOF:
7288 {
7289 struct type *type;
7290 int tok1, tok2;
7291 eat(state, TOK_SIZEOF);
7292 tok1 = peek(state);
7293 tok2 = peek2(state);
7294 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7295 eat(state, TOK_LPAREN);
7296 type = type_name(state);
7297 eat(state, TOK_RPAREN);
7298 }
7299 else {
7300 struct triple *expr;
7301 expr = unary_expr(state);
7302 type = expr->type;
7303 release_expr(state, expr);
7304 }
7305 def = int_const(state, &ulong_type, size_of(state, type));
7306 break;
7307 }
7308 case TOK_ALIGNOF:
7309 {
7310 struct type *type;
7311 int tok1, tok2;
7312 eat(state, TOK_ALIGNOF);
7313 tok1 = peek(state);
7314 tok2 = peek2(state);
7315 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7316 eat(state, TOK_LPAREN);
7317 type = type_name(state);
7318 eat(state, TOK_RPAREN);
7319 }
7320 else {
7321 struct triple *expr;
7322 expr = unary_expr(state);
7323 type = expr->type;
7324 release_expr(state, expr);
7325 }
7326 def = int_const(state, &ulong_type, align_of(state, type));
7327 break;
7328 }
7329 default:
7330 def = postfix_expr(state);
7331 break;
7332 }
7333 return def;
7334}
7335
7336static struct triple *cast_expr(struct compile_state *state)
7337{
7338 struct triple *def;
7339 int tok1, tok2;
7340 tok1 = peek(state);
7341 tok2 = peek2(state);
7342 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7343 struct type *type;
7344 eat(state, TOK_LPAREN);
7345 type = type_name(state);
7346 eat(state, TOK_RPAREN);
7347 def = read_expr(state, cast_expr(state));
7348 def = triple(state, OP_COPY, type, def, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007349 }
7350 else {
7351 def = unary_expr(state);
7352 }
7353 return def;
7354}
7355
7356static struct triple *mult_expr(struct compile_state *state)
7357{
7358 struct triple *def;
7359 int done;
7360 def = cast_expr(state);
7361 do {
7362 struct triple *left, *right;
7363 struct type *result_type;
7364 int tok, op, sign;
7365 done = 0;
7366 switch(tok = (peek(state))) {
7367 case TOK_STAR:
7368 case TOK_DIV:
7369 case TOK_MOD:
7370 left = read_expr(state, def);
7371 arithmetic(state, left);
7372
7373 eat(state, tok);
7374
7375 right = read_expr(state, cast_expr(state));
7376 arithmetic(state, right);
7377
7378 result_type = arithmetic_result(state, left, right);
7379 sign = is_signed(result_type);
7380 op = -1;
7381 switch(tok) {
7382 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
7383 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
7384 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
7385 }
7386 def = triple(state, op, result_type, left, right);
7387 break;
7388 default:
7389 done = 1;
7390 break;
7391 }
7392 } while(!done);
7393 return def;
7394}
7395
7396static struct triple *add_expr(struct compile_state *state)
7397{
7398 struct triple *def;
7399 int done;
7400 def = mult_expr(state);
7401 do {
7402 done = 0;
7403 switch( peek(state)) {
7404 case TOK_PLUS:
7405 eat(state, TOK_PLUS);
7406 def = mk_add_expr(state, def, mult_expr(state));
7407 break;
7408 case TOK_MINUS:
7409 eat(state, TOK_MINUS);
7410 def = mk_sub_expr(state, def, mult_expr(state));
7411 break;
7412 default:
7413 done = 1;
7414 break;
7415 }
7416 } while(!done);
7417 return def;
7418}
7419
7420static struct triple *shift_expr(struct compile_state *state)
7421{
7422 struct triple *def;
7423 int done;
7424 def = add_expr(state);
7425 do {
7426 struct triple *left, *right;
7427 int tok, op;
7428 done = 0;
7429 switch((tok = peek(state))) {
7430 case TOK_SL:
7431 case TOK_SR:
7432 left = read_expr(state, def);
7433 integral(state, left);
7434 left = integral_promotion(state, left);
7435
7436 eat(state, tok);
7437
7438 right = read_expr(state, add_expr(state));
7439 integral(state, right);
7440 right = integral_promotion(state, right);
7441
7442 op = (tok == TOK_SL)? OP_SL :
7443 is_signed(left->type)? OP_SSR: OP_USR;
7444
7445 def = triple(state, op, left->type, left, right);
7446 break;
7447 default:
7448 done = 1;
7449 break;
7450 }
7451 } while(!done);
7452 return def;
7453}
7454
7455static struct triple *relational_expr(struct compile_state *state)
7456{
7457#warning "Extend relational exprs to work on more than arithmetic types"
7458 struct triple *def;
7459 int done;
7460 def = shift_expr(state);
7461 do {
7462 struct triple *left, *right;
7463 struct type *arg_type;
7464 int tok, op, sign;
7465 done = 0;
7466 switch((tok = peek(state))) {
7467 case TOK_LESS:
7468 case TOK_MORE:
7469 case TOK_LESSEQ:
7470 case TOK_MOREEQ:
7471 left = read_expr(state, def);
7472 arithmetic(state, left);
7473
7474 eat(state, tok);
7475
7476 right = read_expr(state, shift_expr(state));
7477 arithmetic(state, right);
7478
7479 arg_type = arithmetic_result(state, left, right);
7480 sign = is_signed(arg_type);
7481 op = -1;
7482 switch(tok) {
7483 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
7484 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
7485 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
7486 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
7487 }
7488 def = triple(state, op, &int_type, left, right);
7489 break;
7490 default:
7491 done = 1;
7492 break;
7493 }
7494 } while(!done);
7495 return def;
7496}
7497
7498static struct triple *equality_expr(struct compile_state *state)
7499{
7500#warning "Extend equality exprs to work on more than arithmetic types"
7501 struct triple *def;
7502 int done;
7503 def = relational_expr(state);
7504 do {
7505 struct triple *left, *right;
7506 int tok, op;
7507 done = 0;
7508 switch((tok = peek(state))) {
7509 case TOK_EQEQ:
7510 case TOK_NOTEQ:
7511 left = read_expr(state, def);
7512 arithmetic(state, left);
7513 eat(state, tok);
7514 right = read_expr(state, relational_expr(state));
7515 arithmetic(state, right);
7516 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
7517 def = triple(state, op, &int_type, left, right);
7518 break;
7519 default:
7520 done = 1;
7521 break;
7522 }
7523 } while(!done);
7524 return def;
7525}
7526
7527static struct triple *and_expr(struct compile_state *state)
7528{
7529 struct triple *def;
7530 def = equality_expr(state);
7531 while(peek(state) == TOK_AND) {
7532 struct triple *left, *right;
7533 struct type *result_type;
7534 left = read_expr(state, def);
7535 integral(state, left);
7536 eat(state, TOK_AND);
7537 right = read_expr(state, equality_expr(state));
7538 integral(state, right);
7539 result_type = arithmetic_result(state, left, right);
7540 def = triple(state, OP_AND, result_type, left, right);
7541 }
7542 return def;
7543}
7544
7545static struct triple *xor_expr(struct compile_state *state)
7546{
7547 struct triple *def;
7548 def = and_expr(state);
7549 while(peek(state) == TOK_XOR) {
7550 struct triple *left, *right;
7551 struct type *result_type;
7552 left = read_expr(state, def);
7553 integral(state, left);
7554 eat(state, TOK_XOR);
7555 right = read_expr(state, and_expr(state));
7556 integral(state, right);
7557 result_type = arithmetic_result(state, left, right);
7558 def = triple(state, OP_XOR, result_type, left, right);
7559 }
7560 return def;
7561}
7562
7563static struct triple *or_expr(struct compile_state *state)
7564{
7565 struct triple *def;
7566 def = xor_expr(state);
7567 while(peek(state) == TOK_OR) {
7568 struct triple *left, *right;
7569 struct type *result_type;
7570 left = read_expr(state, def);
7571 integral(state, left);
7572 eat(state, TOK_OR);
7573 right = read_expr(state, xor_expr(state));
7574 integral(state, right);
7575 result_type = arithmetic_result(state, left, right);
7576 def = triple(state, OP_OR, result_type, left, right);
7577 }
7578 return def;
7579}
7580
7581static struct triple *land_expr(struct compile_state *state)
7582{
7583 struct triple *def;
7584 def = or_expr(state);
7585 while(peek(state) == TOK_LOGAND) {
7586 struct triple *left, *right;
7587 left = read_expr(state, def);
7588 bool(state, left);
7589 eat(state, TOK_LOGAND);
7590 right = read_expr(state, or_expr(state));
7591 bool(state, right);
7592
7593 def = triple(state, OP_LAND, &int_type,
7594 ltrue_expr(state, left),
7595 ltrue_expr(state, right));
7596 }
7597 return def;
7598}
7599
7600static struct triple *lor_expr(struct compile_state *state)
7601{
7602 struct triple *def;
7603 def = land_expr(state);
7604 while(peek(state) == TOK_LOGOR) {
7605 struct triple *left, *right;
7606 left = read_expr(state, def);
7607 bool(state, left);
7608 eat(state, TOK_LOGOR);
7609 right = read_expr(state, land_expr(state));
7610 bool(state, right);
7611
7612 def = triple(state, OP_LOR, &int_type,
7613 ltrue_expr(state, left),
7614 ltrue_expr(state, right));
7615 }
7616 return def;
7617}
7618
7619static struct triple *conditional_expr(struct compile_state *state)
7620{
7621 struct triple *def;
7622 def = lor_expr(state);
7623 if (peek(state) == TOK_QUEST) {
7624 struct triple *test, *left, *right;
7625 bool(state, def);
7626 test = ltrue_expr(state, read_expr(state, def));
7627 eat(state, TOK_QUEST);
7628 left = read_expr(state, expr(state));
7629 eat(state, TOK_COLON);
7630 right = read_expr(state, conditional_expr(state));
7631
7632 def = cond_expr(state, test, left, right);
7633 }
7634 return def;
7635}
7636
7637static struct triple *eval_const_expr(
7638 struct compile_state *state, struct triple *expr)
7639{
7640 struct triple *def;
Eric Biederman00443072003-06-24 12:34:45 +00007641 if (is_const(expr)) {
7642 def = expr;
7643 }
7644 else {
7645 /* If we don't start out as a constant simplify into one */
7646 struct triple *head, *ptr;
7647 head = label(state); /* dummy initial triple */
7648 flatten(state, head, expr);
7649 for(ptr = head->next; ptr != head; ptr = ptr->next) {
7650 simplify(state, ptr);
7651 }
7652 /* Remove the constant value the tail of the list */
7653 def = head->prev;
7654 def->prev->next = def->next;
7655 def->next->prev = def->prev;
7656 def->next = def->prev = def;
7657 if (!is_const(def)) {
7658 error(state, 0, "Not a constant expression");
7659 }
7660 /* Free the intermediate expressions */
7661 while(head->next != head) {
7662 release_triple(state, head->next);
7663 }
7664 free_triple(state, head);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007665 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007666 return def;
7667}
7668
7669static struct triple *constant_expr(struct compile_state *state)
7670{
7671 return eval_const_expr(state, conditional_expr(state));
7672}
7673
7674static struct triple *assignment_expr(struct compile_state *state)
7675{
7676 struct triple *def, *left, *right;
7677 int tok, op, sign;
7678 /* The C grammer in K&R shows assignment expressions
7679 * only taking unary expressions as input on their
7680 * left hand side. But specifies the precedence of
7681 * assignemnt as the lowest operator except for comma.
7682 *
7683 * Allowing conditional expressions on the left hand side
7684 * of an assignement results in a grammar that accepts
7685 * a larger set of statements than standard C. As long
7686 * as the subset of the grammar that is standard C behaves
7687 * correctly this should cause no problems.
7688 *
7689 * For the extra token strings accepted by the grammar
7690 * none of them should produce a valid lvalue, so they
7691 * should not produce functioning programs.
7692 *
7693 * GCC has this bug as well, so surprises should be minimal.
7694 */
7695 def = conditional_expr(state);
7696 left = def;
7697 switch((tok = peek(state))) {
7698 case TOK_EQ:
7699 lvalue(state, left);
7700 eat(state, TOK_EQ);
7701 def = write_expr(state, left,
7702 read_expr(state, assignment_expr(state)));
7703 break;
7704 case TOK_TIMESEQ:
7705 case TOK_DIVEQ:
7706 case TOK_MODEQ:
Eric Biedermanb138ac82003-04-22 18:44:01 +00007707 lvalue(state, left);
7708 arithmetic(state, left);
7709 eat(state, tok);
7710 right = read_expr(state, assignment_expr(state));
7711 arithmetic(state, right);
7712
7713 sign = is_signed(left->type);
7714 op = -1;
7715 switch(tok) {
7716 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
7717 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
7718 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007719 }
7720 def = write_expr(state, left,
7721 triple(state, op, left->type,
7722 read_expr(state, left), right));
7723 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007724 case TOK_PLUSEQ:
7725 lvalue(state, left);
7726 eat(state, TOK_PLUSEQ);
7727 def = write_expr(state, left,
7728 mk_add_expr(state, left, assignment_expr(state)));
7729 break;
7730 case TOK_MINUSEQ:
7731 lvalue(state, left);
7732 eat(state, TOK_MINUSEQ);
7733 def = write_expr(state, left,
7734 mk_sub_expr(state, left, assignment_expr(state)));
7735 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007736 case TOK_SLEQ:
7737 case TOK_SREQ:
7738 case TOK_ANDEQ:
7739 case TOK_XOREQ:
7740 case TOK_OREQ:
7741 lvalue(state, left);
7742 integral(state, left);
7743 eat(state, tok);
7744 right = read_expr(state, assignment_expr(state));
7745 integral(state, right);
7746 right = integral_promotion(state, right);
7747 sign = is_signed(left->type);
7748 op = -1;
7749 switch(tok) {
7750 case TOK_SLEQ: op = OP_SL; break;
7751 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
7752 case TOK_ANDEQ: op = OP_AND; break;
7753 case TOK_XOREQ: op = OP_XOR; break;
7754 case TOK_OREQ: op = OP_OR; break;
7755 }
7756 def = write_expr(state, left,
7757 triple(state, op, left->type,
7758 read_expr(state, left), right));
7759 break;
7760 }
7761 return def;
7762}
7763
7764static struct triple *expr(struct compile_state *state)
7765{
7766 struct triple *def;
7767 def = assignment_expr(state);
7768 while(peek(state) == TOK_COMMA) {
7769 struct triple *left, *right;
7770 left = def;
7771 eat(state, TOK_COMMA);
7772 right = assignment_expr(state);
7773 def = triple(state, OP_COMMA, right->type, left, right);
7774 }
7775 return def;
7776}
7777
7778static void expr_statement(struct compile_state *state, struct triple *first)
7779{
7780 if (peek(state) != TOK_SEMI) {
7781 flatten(state, first, expr(state));
7782 }
7783 eat(state, TOK_SEMI);
7784}
7785
7786static void if_statement(struct compile_state *state, struct triple *first)
7787{
7788 struct triple *test, *jmp1, *jmp2, *middle, *end;
7789
7790 jmp1 = jmp2 = middle = 0;
7791 eat(state, TOK_IF);
7792 eat(state, TOK_LPAREN);
7793 test = expr(state);
7794 bool(state, test);
7795 /* Cleanup and invert the test */
7796 test = lfalse_expr(state, read_expr(state, test));
7797 eat(state, TOK_RPAREN);
7798 /* Generate the needed pieces */
7799 middle = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007800 jmp1 = branch(state, middle, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007801 /* Thread the pieces together */
7802 flatten(state, first, test);
7803 flatten(state, first, jmp1);
7804 flatten(state, first, label(state));
7805 statement(state, first);
7806 if (peek(state) == TOK_ELSE) {
7807 eat(state, TOK_ELSE);
7808 /* Generate the rest of the pieces */
7809 end = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007810 jmp2 = branch(state, end, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007811 /* Thread them together */
7812 flatten(state, first, jmp2);
7813 flatten(state, first, middle);
7814 statement(state, first);
7815 flatten(state, first, end);
7816 }
7817 else {
7818 flatten(state, first, middle);
7819 }
7820}
7821
7822static void for_statement(struct compile_state *state, struct triple *first)
7823{
7824 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
7825 struct triple *label1, *label2, *label3;
7826 struct hash_entry *ident;
7827
7828 eat(state, TOK_FOR);
7829 eat(state, TOK_LPAREN);
7830 head = test = tail = jmp1 = jmp2 = 0;
7831 if (peek(state) != TOK_SEMI) {
7832 head = expr(state);
7833 }
7834 eat(state, TOK_SEMI);
7835 if (peek(state) != TOK_SEMI) {
7836 test = expr(state);
7837 bool(state, test);
7838 test = ltrue_expr(state, read_expr(state, test));
7839 }
7840 eat(state, TOK_SEMI);
7841 if (peek(state) != TOK_RPAREN) {
7842 tail = expr(state);
7843 }
7844 eat(state, TOK_RPAREN);
7845 /* Generate the needed pieces */
7846 label1 = label(state);
7847 label2 = label(state);
7848 label3 = label(state);
7849 if (test) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007850 jmp1 = branch(state, label3, 0);
7851 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007852 }
7853 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007854 jmp2 = branch(state, label1, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007855 }
7856 end = label(state);
7857 /* Remember where break and continue go */
7858 start_scope(state);
7859 ident = state->i_break;
7860 symbol(state, ident, &ident->sym_ident, end, end->type);
7861 ident = state->i_continue;
7862 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7863 /* Now include the body */
7864 flatten(state, first, head);
7865 flatten(state, first, jmp1);
7866 flatten(state, first, label1);
7867 statement(state, first);
7868 flatten(state, first, label2);
7869 flatten(state, first, tail);
7870 flatten(state, first, label3);
7871 flatten(state, first, test);
7872 flatten(state, first, jmp2);
7873 flatten(state, first, end);
7874 /* Cleanup the break/continue scope */
7875 end_scope(state);
7876}
7877
7878static void while_statement(struct compile_state *state, struct triple *first)
7879{
7880 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
7881 struct hash_entry *ident;
7882 eat(state, TOK_WHILE);
7883 eat(state, TOK_LPAREN);
7884 test = expr(state);
7885 bool(state, test);
7886 test = ltrue_expr(state, read_expr(state, test));
7887 eat(state, TOK_RPAREN);
7888 /* Generate the needed pieces */
7889 label1 = label(state);
7890 label2 = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007891 jmp1 = branch(state, label2, 0);
7892 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007893 end = label(state);
7894 /* Remember where break and continue go */
7895 start_scope(state);
7896 ident = state->i_break;
7897 symbol(state, ident, &ident->sym_ident, end, end->type);
7898 ident = state->i_continue;
7899 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7900 /* Thread them together */
7901 flatten(state, first, jmp1);
7902 flatten(state, first, label1);
7903 statement(state, first);
7904 flatten(state, first, label2);
7905 flatten(state, first, test);
7906 flatten(state, first, jmp2);
7907 flatten(state, first, end);
7908 /* Cleanup the break/continue scope */
7909 end_scope(state);
7910}
7911
7912static void do_statement(struct compile_state *state, struct triple *first)
7913{
7914 struct triple *label1, *label2, *test, *end;
7915 struct hash_entry *ident;
7916 eat(state, TOK_DO);
7917 /* Generate the needed pieces */
7918 label1 = label(state);
7919 label2 = label(state);
7920 end = label(state);
7921 /* Remember where break and continue go */
7922 start_scope(state);
7923 ident = state->i_break;
7924 symbol(state, ident, &ident->sym_ident, end, end->type);
7925 ident = state->i_continue;
7926 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7927 /* Now include the body */
7928 flatten(state, first, label1);
7929 statement(state, first);
7930 /* Cleanup the break/continue scope */
7931 end_scope(state);
7932 /* Eat the rest of the loop */
7933 eat(state, TOK_WHILE);
7934 eat(state, TOK_LPAREN);
7935 test = read_expr(state, expr(state));
7936 bool(state, test);
7937 eat(state, TOK_RPAREN);
7938 eat(state, TOK_SEMI);
7939 /* Thread the pieces together */
7940 test = ltrue_expr(state, test);
7941 flatten(state, first, label2);
7942 flatten(state, first, test);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007943 flatten(state, first, branch(state, label1, test));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007944 flatten(state, first, end);
7945}
7946
7947
7948static void return_statement(struct compile_state *state, struct triple *first)
7949{
7950 struct triple *jmp, *mv, *dest, *var, *val;
7951 int last;
7952 eat(state, TOK_RETURN);
7953
7954#warning "FIXME implement a more general excess branch elimination"
7955 val = 0;
7956 /* If we have a return value do some more work */
7957 if (peek(state) != TOK_SEMI) {
7958 val = read_expr(state, expr(state));
7959 }
7960 eat(state, TOK_SEMI);
7961
7962 /* See if this last statement in a function */
7963 last = ((peek(state) == TOK_RBRACE) &&
7964 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
7965
7966 /* Find the return variable */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007967 var = MISC(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007968 /* Find the return destination */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007969 dest = RHS(state->main_function, 0)->prev;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007970 mv = jmp = 0;
7971 /* If needed generate a jump instruction */
7972 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007973 jmp = branch(state, dest, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007974 }
7975 /* If needed generate an assignment instruction */
7976 if (val) {
7977 mv = write_expr(state, var, val);
7978 }
7979 /* Now put the code together */
7980 if (mv) {
7981 flatten(state, first, mv);
7982 flatten(state, first, jmp);
7983 }
7984 else if (jmp) {
7985 flatten(state, first, jmp);
7986 }
7987}
7988
7989static void break_statement(struct compile_state *state, struct triple *first)
7990{
7991 struct triple *dest;
7992 eat(state, TOK_BREAK);
7993 eat(state, TOK_SEMI);
7994 if (!state->i_break->sym_ident) {
7995 error(state, 0, "break statement not within loop or switch");
7996 }
7997 dest = state->i_break->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007998 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007999}
8000
8001static void continue_statement(struct compile_state *state, struct triple *first)
8002{
8003 struct triple *dest;
8004 eat(state, TOK_CONTINUE);
8005 eat(state, TOK_SEMI);
8006 if (!state->i_continue->sym_ident) {
8007 error(state, 0, "continue statement outside of a loop");
8008 }
8009 dest = state->i_continue->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008010 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008011}
8012
8013static void goto_statement(struct compile_state *state, struct triple *first)
8014{
Eric Biederman153ea352003-06-20 14:43:20 +00008015 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008016 eat(state, TOK_GOTO);
8017 eat(state, TOK_IDENT);
Eric Biederman153ea352003-06-20 14:43:20 +00008018 ident = state->token[0].ident;
8019 if (!ident->sym_label) {
8020 /* If this is a forward branch allocate the label now,
8021 * it will be flattend in the appropriate location later.
8022 */
8023 struct triple *ins;
8024 ins = label(state);
8025 label_symbol(state, ident, ins);
8026 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008027 eat(state, TOK_SEMI);
Eric Biederman153ea352003-06-20 14:43:20 +00008028
8029 flatten(state, first, branch(state, ident->sym_label->def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008030}
8031
8032static void labeled_statement(struct compile_state *state, struct triple *first)
8033{
Eric Biederman153ea352003-06-20 14:43:20 +00008034 struct triple *ins;
8035 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008036 eat(state, TOK_IDENT);
Eric Biederman153ea352003-06-20 14:43:20 +00008037
8038 ident = state->token[0].ident;
8039 if (ident->sym_label && ident->sym_label->def) {
8040 ins = ident->sym_label->def;
8041 put_occurance(ins->occurance);
8042 ins->occurance = new_occurance(state);
8043 }
8044 else {
8045 ins = label(state);
8046 label_symbol(state, ident, ins);
8047 }
8048 if (ins->id & TRIPLE_FLAG_FLATTENED) {
8049 error(state, 0, "label %s already defined", ident->name);
8050 }
8051 flatten(state, first, ins);
8052
Eric Biedermanb138ac82003-04-22 18:44:01 +00008053 eat(state, TOK_COLON);
8054 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008055}
8056
8057static void switch_statement(struct compile_state *state, struct triple *first)
8058{
8059 FINISHME();
8060 eat(state, TOK_SWITCH);
8061 eat(state, TOK_LPAREN);
8062 expr(state);
8063 eat(state, TOK_RPAREN);
8064 statement(state, first);
8065 error(state, 0, "switch statements are not implemented");
8066 FINISHME();
8067}
8068
8069static void case_statement(struct compile_state *state, struct triple *first)
8070{
8071 FINISHME();
8072 eat(state, TOK_CASE);
8073 constant_expr(state);
8074 eat(state, TOK_COLON);
8075 statement(state, first);
8076 error(state, 0, "case statements are not implemented");
8077 FINISHME();
8078}
8079
8080static void default_statement(struct compile_state *state, struct triple *first)
8081{
8082 FINISHME();
8083 eat(state, TOK_DEFAULT);
8084 eat(state, TOK_COLON);
8085 statement(state, first);
8086 error(state, 0, "default statements are not implemented");
8087 FINISHME();
8088}
8089
8090static void asm_statement(struct compile_state *state, struct triple *first)
8091{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008092 struct asm_info *info;
8093 struct {
8094 struct triple *constraint;
8095 struct triple *expr;
8096 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
8097 struct triple *def, *asm_str;
8098 int out, in, clobbers, more, colons, i;
8099
8100 eat(state, TOK_ASM);
8101 /* For now ignore the qualifiers */
8102 switch(peek(state)) {
8103 case TOK_CONST:
8104 eat(state, TOK_CONST);
8105 break;
8106 case TOK_VOLATILE:
8107 eat(state, TOK_VOLATILE);
8108 break;
8109 }
8110 eat(state, TOK_LPAREN);
8111 asm_str = string_constant(state);
8112
8113 colons = 0;
8114 out = in = clobbers = 0;
8115 /* Outputs */
8116 if ((colons == 0) && (peek(state) == TOK_COLON)) {
8117 eat(state, TOK_COLON);
8118 colons++;
8119 more = (peek(state) == TOK_LIT_STRING);
8120 while(more) {
8121 struct triple *var;
8122 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008123 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008124 more = 0;
8125 if (out > MAX_LHS) {
8126 error(state, 0, "Maximum output count exceeded.");
8127 }
8128 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +00008129 str = constraint->u.blob;
8130 if (str[0] != '=') {
8131 error(state, 0, "Output constraint does not start with =");
8132 }
8133 constraint->u.blob = str + 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008134 eat(state, TOK_LPAREN);
8135 var = conditional_expr(state);
8136 eat(state, TOK_RPAREN);
8137
8138 lvalue(state, var);
8139 out_param[out].constraint = constraint;
8140 out_param[out].expr = var;
8141 if (peek(state) == TOK_COMMA) {
8142 eat(state, TOK_COMMA);
8143 more = 1;
8144 }
8145 out++;
8146 }
8147 }
8148 /* Inputs */
8149 if ((colons == 1) && (peek(state) == TOK_COLON)) {
8150 eat(state, TOK_COLON);
8151 colons++;
8152 more = (peek(state) == TOK_LIT_STRING);
8153 while(more) {
8154 struct triple *val;
8155 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008156 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008157 more = 0;
8158 if (in > MAX_RHS) {
8159 error(state, 0, "Maximum input count exceeded.");
8160 }
8161 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +00008162 str = constraint->u.blob;
8163 if (digitp(str[0] && str[1] == '\0')) {
8164 int val;
8165 val = digval(str[0]);
8166 if ((val < 0) || (val >= out)) {
8167 error(state, 0, "Invalid input constraint %d", val);
8168 }
8169 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008170 eat(state, TOK_LPAREN);
8171 val = conditional_expr(state);
8172 eat(state, TOK_RPAREN);
8173
8174 in_param[in].constraint = constraint;
8175 in_param[in].expr = val;
8176 if (peek(state) == TOK_COMMA) {
8177 eat(state, TOK_COMMA);
8178 more = 1;
8179 }
8180 in++;
8181 }
8182 }
8183
8184 /* Clobber */
8185 if ((colons == 2) && (peek(state) == TOK_COLON)) {
8186 eat(state, TOK_COLON);
8187 colons++;
8188 more = (peek(state) == TOK_LIT_STRING);
8189 while(more) {
8190 struct triple *clobber;
8191 more = 0;
8192 if ((clobbers + out) > MAX_LHS) {
8193 error(state, 0, "Maximum clobber limit exceeded.");
8194 }
8195 clobber = string_constant(state);
8196 eat(state, TOK_RPAREN);
8197
8198 clob_param[clobbers].constraint = clobber;
8199 if (peek(state) == TOK_COMMA) {
8200 eat(state, TOK_COMMA);
8201 more = 1;
8202 }
8203 clobbers++;
8204 }
8205 }
8206 eat(state, TOK_RPAREN);
8207 eat(state, TOK_SEMI);
8208
8209
8210 info = xcmalloc(sizeof(*info), "asm_info");
8211 info->str = asm_str->u.blob;
8212 free_triple(state, asm_str);
8213
8214 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
8215 def->u.ainfo = info;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008216
8217 /* Find the register constraints */
8218 for(i = 0; i < out; i++) {
8219 struct triple *constraint;
8220 constraint = out_param[i].constraint;
8221 info->tmpl.lhs[i] = arch_reg_constraint(state,
8222 out_param[i].expr->type, constraint->u.blob);
8223 free_triple(state, constraint);
8224 }
8225 for(; i - out < clobbers; i++) {
8226 struct triple *constraint;
8227 constraint = clob_param[i - out].constraint;
8228 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
8229 free_triple(state, constraint);
8230 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008231 for(i = 0; i < in; i++) {
8232 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008233 const char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008234 constraint = in_param[i].constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008235 str = constraint->u.blob;
8236 if (digitp(str[0]) && str[1] == '\0') {
8237 struct reg_info cinfo;
8238 int val;
8239 val = digval(str[0]);
8240 cinfo.reg = info->tmpl.lhs[val].reg;
8241 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
8242 cinfo.regcm &= info->tmpl.lhs[val].regcm;
8243 if (cinfo.reg == REG_UNSET) {
8244 cinfo.reg = REG_VIRT0 + val;
8245 }
8246 if (cinfo.regcm == 0) {
8247 error(state, 0, "No registers for %d", val);
8248 }
8249 info->tmpl.lhs[val] = cinfo;
8250 info->tmpl.rhs[i] = cinfo;
8251
8252 } else {
8253 info->tmpl.rhs[i] = arch_reg_constraint(state,
8254 in_param[i].expr->type, str);
8255 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008256 free_triple(state, constraint);
8257 }
Eric Biederman8d9c1232003-06-17 08:42:17 +00008258
8259 /* Now build the helper expressions */
8260 for(i = 0; i < in; i++) {
8261 RHS(def, i) = read_expr(state,in_param[i].expr);
8262 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008263 flatten(state, first, def);
8264 for(i = 0; i < out; i++) {
8265 struct triple *piece;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008266 piece = triple(state, OP_PIECE, out_param[i].expr->type, def, 0);
8267 piece->u.cval = i;
8268 LHS(def, i) = piece;
8269 flatten(state, first,
8270 write_expr(state, out_param[i].expr, piece));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008271 }
8272 for(; i - out < clobbers; i++) {
8273 struct triple *piece;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008274 piece = triple(state, OP_PIECE, &void_type, def, 0);
8275 piece->u.cval = i;
8276 LHS(def, i) = piece;
8277 flatten(state, first, piece);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008278 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008279}
8280
8281
8282static int isdecl(int tok)
8283{
8284 switch(tok) {
8285 case TOK_AUTO:
8286 case TOK_REGISTER:
8287 case TOK_STATIC:
8288 case TOK_EXTERN:
8289 case TOK_TYPEDEF:
8290 case TOK_CONST:
8291 case TOK_RESTRICT:
8292 case TOK_VOLATILE:
8293 case TOK_VOID:
8294 case TOK_CHAR:
8295 case TOK_SHORT:
8296 case TOK_INT:
8297 case TOK_LONG:
8298 case TOK_FLOAT:
8299 case TOK_DOUBLE:
8300 case TOK_SIGNED:
8301 case TOK_UNSIGNED:
8302 case TOK_STRUCT:
8303 case TOK_UNION:
8304 case TOK_ENUM:
8305 case TOK_TYPE_NAME: /* typedef name */
8306 return 1;
8307 default:
8308 return 0;
8309 }
8310}
8311
8312static void compound_statement(struct compile_state *state, struct triple *first)
8313{
8314 eat(state, TOK_LBRACE);
8315 start_scope(state);
8316
8317 /* statement-list opt */
8318 while (peek(state) != TOK_RBRACE) {
8319 statement(state, first);
8320 }
8321 end_scope(state);
8322 eat(state, TOK_RBRACE);
8323}
8324
8325static void statement(struct compile_state *state, struct triple *first)
8326{
8327 int tok;
8328 tok = peek(state);
8329 if (tok == TOK_LBRACE) {
8330 compound_statement(state, first);
8331 }
8332 else if (tok == TOK_IF) {
8333 if_statement(state, first);
8334 }
8335 else if (tok == TOK_FOR) {
8336 for_statement(state, first);
8337 }
8338 else if (tok == TOK_WHILE) {
8339 while_statement(state, first);
8340 }
8341 else if (tok == TOK_DO) {
8342 do_statement(state, first);
8343 }
8344 else if (tok == TOK_RETURN) {
8345 return_statement(state, first);
8346 }
8347 else if (tok == TOK_BREAK) {
8348 break_statement(state, first);
8349 }
8350 else if (tok == TOK_CONTINUE) {
8351 continue_statement(state, first);
8352 }
8353 else if (tok == TOK_GOTO) {
8354 goto_statement(state, first);
8355 }
8356 else if (tok == TOK_SWITCH) {
8357 switch_statement(state, first);
8358 }
8359 else if (tok == TOK_ASM) {
8360 asm_statement(state, first);
8361 }
8362 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
8363 labeled_statement(state, first);
8364 }
8365 else if (tok == TOK_CASE) {
8366 case_statement(state, first);
8367 }
8368 else if (tok == TOK_DEFAULT) {
8369 default_statement(state, first);
8370 }
8371 else if (isdecl(tok)) {
8372 /* This handles C99 intermixing of statements and decls */
8373 decl(state, first);
8374 }
8375 else {
8376 expr_statement(state, first);
8377 }
8378}
8379
8380static struct type *param_decl(struct compile_state *state)
8381{
8382 struct type *type;
8383 struct hash_entry *ident;
8384 /* Cheat so the declarator will know we are not global */
8385 start_scope(state);
8386 ident = 0;
8387 type = decl_specifiers(state);
8388 type = declarator(state, type, &ident, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008389 type->field_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008390 end_scope(state);
8391 return type;
8392}
8393
8394static struct type *param_type_list(struct compile_state *state, struct type *type)
8395{
8396 struct type *ftype, **next;
8397 ftype = new_type(TYPE_FUNCTION, type, param_decl(state));
8398 next = &ftype->right;
8399 while(peek(state) == TOK_COMMA) {
8400 eat(state, TOK_COMMA);
8401 if (peek(state) == TOK_DOTS) {
8402 eat(state, TOK_DOTS);
8403 error(state, 0, "variadic functions not supported");
8404 }
8405 else {
8406 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
8407 next = &((*next)->right);
8408 }
8409 }
8410 return ftype;
8411}
8412
8413
8414static struct type *type_name(struct compile_state *state)
8415{
8416 struct type *type;
8417 type = specifier_qualifier_list(state);
8418 /* abstract-declarator (may consume no tokens) */
8419 type = declarator(state, type, 0, 0);
8420 return type;
8421}
8422
8423static struct type *direct_declarator(
8424 struct compile_state *state, struct type *type,
8425 struct hash_entry **ident, int need_ident)
8426{
8427 struct type *outer;
8428 int op;
8429 outer = 0;
8430 arrays_complete(state, type);
8431 switch(peek(state)) {
8432 case TOK_IDENT:
8433 eat(state, TOK_IDENT);
8434 if (!ident) {
8435 error(state, 0, "Unexpected identifier found");
8436 }
8437 /* The name of what we are declaring */
8438 *ident = state->token[0].ident;
8439 break;
8440 case TOK_LPAREN:
8441 eat(state, TOK_LPAREN);
8442 outer = declarator(state, type, ident, need_ident);
8443 eat(state, TOK_RPAREN);
8444 break;
8445 default:
8446 if (need_ident) {
8447 error(state, 0, "Identifier expected");
8448 }
8449 break;
8450 }
8451 do {
8452 op = 1;
8453 arrays_complete(state, type);
8454 switch(peek(state)) {
8455 case TOK_LPAREN:
8456 eat(state, TOK_LPAREN);
8457 type = param_type_list(state, type);
8458 eat(state, TOK_RPAREN);
8459 break;
8460 case TOK_LBRACKET:
8461 {
8462 unsigned int qualifiers;
8463 struct triple *value;
8464 value = 0;
8465 eat(state, TOK_LBRACKET);
8466 if (peek(state) != TOK_RBRACKET) {
8467 value = constant_expr(state);
8468 integral(state, value);
8469 }
8470 eat(state, TOK_RBRACKET);
8471
8472 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
8473 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
8474 if (value) {
8475 type->elements = value->u.cval;
8476 free_triple(state, value);
8477 } else {
8478 type->elements = ELEMENT_COUNT_UNSPECIFIED;
8479 op = 0;
8480 }
8481 }
8482 break;
8483 default:
8484 op = 0;
8485 break;
8486 }
8487 } while(op);
8488 if (outer) {
8489 struct type *inner;
8490 arrays_complete(state, type);
8491 FINISHME();
8492 for(inner = outer; inner->left; inner = inner->left)
8493 ;
8494 inner->left = type;
8495 type = outer;
8496 }
8497 return type;
8498}
8499
8500static struct type *declarator(
8501 struct compile_state *state, struct type *type,
8502 struct hash_entry **ident, int need_ident)
8503{
8504 while(peek(state) == TOK_STAR) {
8505 eat(state, TOK_STAR);
8506 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
8507 }
8508 type = direct_declarator(state, type, ident, need_ident);
8509 return type;
8510}
8511
8512
8513static struct type *typedef_name(
8514 struct compile_state *state, unsigned int specifiers)
8515{
8516 struct hash_entry *ident;
8517 struct type *type;
8518 eat(state, TOK_TYPE_NAME);
8519 ident = state->token[0].ident;
8520 type = ident->sym_ident->type;
8521 specifiers |= type->type & QUAL_MASK;
8522 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
8523 (type->type & (STOR_MASK | QUAL_MASK))) {
8524 type = clone_type(specifiers, type);
8525 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008526 return type;
8527}
8528
8529static struct type *enum_specifier(
8530 struct compile_state *state, unsigned int specifiers)
8531{
8532 int tok;
8533 struct type *type;
8534 type = 0;
8535 FINISHME();
8536 eat(state, TOK_ENUM);
8537 tok = peek(state);
8538 if (tok == TOK_IDENT) {
8539 eat(state, TOK_IDENT);
8540 }
8541 if ((tok != TOK_IDENT) || (peek(state) == TOK_LBRACE)) {
8542 eat(state, TOK_LBRACE);
8543 do {
8544 eat(state, TOK_IDENT);
8545 if (peek(state) == TOK_EQ) {
8546 eat(state, TOK_EQ);
8547 constant_expr(state);
8548 }
8549 if (peek(state) == TOK_COMMA) {
8550 eat(state, TOK_COMMA);
8551 }
8552 } while(peek(state) != TOK_RBRACE);
8553 eat(state, TOK_RBRACE);
8554 }
8555 FINISHME();
8556 return type;
8557}
8558
Eric Biedermanb138ac82003-04-22 18:44:01 +00008559static struct type *struct_declarator(
8560 struct compile_state *state, struct type *type, struct hash_entry **ident)
8561{
8562 int tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008563 tok = peek(state);
8564 if (tok != TOK_COLON) {
8565 type = declarator(state, type, ident, 1);
8566 }
8567 if ((tok == TOK_COLON) || (peek(state) == TOK_COLON)) {
Eric Biederman530b5192003-07-01 10:05:30 +00008568 struct triple *value;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008569 eat(state, TOK_COLON);
Eric Biederman530b5192003-07-01 10:05:30 +00008570 value = constant_expr(state);
8571#warning "FIXME implement bitfields to reduce register usage"
8572 error(state, 0, "bitfields not yet implemented");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008573 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008574 return type;
8575}
Eric Biedermanb138ac82003-04-22 18:44:01 +00008576
8577static struct type *struct_or_union_specifier(
Eric Biederman00443072003-06-24 12:34:45 +00008578 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008579{
Eric Biederman0babc1c2003-05-09 02:39:00 +00008580 struct type *struct_type;
8581 struct hash_entry *ident;
8582 unsigned int type_join;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008583 int tok;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008584 struct_type = 0;
8585 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008586 switch(peek(state)) {
8587 case TOK_STRUCT:
8588 eat(state, TOK_STRUCT);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008589 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008590 break;
8591 case TOK_UNION:
8592 eat(state, TOK_UNION);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008593 type_join = TYPE_OVERLAP;
8594 error(state, 0, "unions not yet supported\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008595 break;
8596 default:
8597 eat(state, TOK_STRUCT);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008598 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008599 break;
8600 }
8601 tok = peek(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008602 if ((tok == TOK_IDENT) || (tok == TOK_TYPE_NAME)) {
8603 eat(state, tok);
8604 ident = state->token[0].ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008605 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00008606 if (!ident || (peek(state) == TOK_LBRACE)) {
8607 ulong_t elements;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00008608 struct type **next;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008609 elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008610 eat(state, TOK_LBRACE);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00008611 next = &struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008612 do {
8613 struct type *base_type;
8614 int done;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008615 base_type = specifier_qualifier_list(state);
8616 do {
8617 struct type *type;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008618 struct hash_entry *fident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008619 done = 1;
Eric Biederman530b5192003-07-01 10:05:30 +00008620 type = struct_declarator(state, base_type, &fident);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008621 elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008622 if (peek(state) == TOK_COMMA) {
8623 done = 0;
8624 eat(state, TOK_COMMA);
8625 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00008626 type = clone_type(0, type);
8627 type->field_ident = fident;
8628 if (*next) {
8629 *next = new_type(type_join, *next, type);
8630 next = &((*next)->right);
8631 } else {
8632 *next = type;
8633 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008634 } while(!done);
8635 eat(state, TOK_SEMI);
8636 } while(peek(state) != TOK_RBRACE);
8637 eat(state, TOK_RBRACE);
Eric Biederman00443072003-06-24 12:34:45 +00008638 struct_type = new_type(TYPE_STRUCT | spec, struct_type, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008639 struct_type->type_ident = ident;
8640 struct_type->elements = elements;
8641 symbol(state, ident, &ident->sym_struct, 0, struct_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008642 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00008643 if (ident && ident->sym_struct) {
Eric Biederman00443072003-06-24 12:34:45 +00008644 struct_type = clone_type(spec, ident->sym_struct->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008645 }
8646 else if (ident && !ident->sym_struct) {
8647 error(state, 0, "struct %s undeclared", ident->name);
8648 }
8649 return struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008650}
8651
8652static unsigned int storage_class_specifier_opt(struct compile_state *state)
8653{
8654 unsigned int specifiers;
8655 switch(peek(state)) {
8656 case TOK_AUTO:
8657 eat(state, TOK_AUTO);
8658 specifiers = STOR_AUTO;
8659 break;
8660 case TOK_REGISTER:
8661 eat(state, TOK_REGISTER);
8662 specifiers = STOR_REGISTER;
8663 break;
8664 case TOK_STATIC:
8665 eat(state, TOK_STATIC);
8666 specifiers = STOR_STATIC;
8667 break;
8668 case TOK_EXTERN:
8669 eat(state, TOK_EXTERN);
8670 specifiers = STOR_EXTERN;
8671 break;
8672 case TOK_TYPEDEF:
8673 eat(state, TOK_TYPEDEF);
8674 specifiers = STOR_TYPEDEF;
8675 break;
8676 default:
8677 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
8678 specifiers = STOR_STATIC;
8679 }
8680 else {
8681 specifiers = STOR_AUTO;
8682 }
8683 }
8684 return specifiers;
8685}
8686
8687static unsigned int function_specifier_opt(struct compile_state *state)
8688{
8689 /* Ignore the inline keyword */
8690 unsigned int specifiers;
8691 specifiers = 0;
8692 switch(peek(state)) {
8693 case TOK_INLINE:
8694 eat(state, TOK_INLINE);
8695 specifiers = STOR_INLINE;
8696 }
8697 return specifiers;
8698}
8699
8700static unsigned int type_qualifiers(struct compile_state *state)
8701{
8702 unsigned int specifiers;
8703 int done;
8704 done = 0;
8705 specifiers = QUAL_NONE;
8706 do {
8707 switch(peek(state)) {
8708 case TOK_CONST:
8709 eat(state, TOK_CONST);
8710 specifiers = QUAL_CONST;
8711 break;
8712 case TOK_VOLATILE:
8713 eat(state, TOK_VOLATILE);
8714 specifiers = QUAL_VOLATILE;
8715 break;
8716 case TOK_RESTRICT:
8717 eat(state, TOK_RESTRICT);
8718 specifiers = QUAL_RESTRICT;
8719 break;
8720 default:
8721 done = 1;
8722 break;
8723 }
8724 } while(!done);
8725 return specifiers;
8726}
8727
8728static struct type *type_specifier(
8729 struct compile_state *state, unsigned int spec)
8730{
8731 struct type *type;
8732 type = 0;
8733 switch(peek(state)) {
8734 case TOK_VOID:
8735 eat(state, TOK_VOID);
8736 type = new_type(TYPE_VOID | spec, 0, 0);
8737 break;
8738 case TOK_CHAR:
8739 eat(state, TOK_CHAR);
8740 type = new_type(TYPE_CHAR | spec, 0, 0);
8741 break;
8742 case TOK_SHORT:
8743 eat(state, TOK_SHORT);
8744 if (peek(state) == TOK_INT) {
8745 eat(state, TOK_INT);
8746 }
8747 type = new_type(TYPE_SHORT | spec, 0, 0);
8748 break;
8749 case TOK_INT:
8750 eat(state, TOK_INT);
8751 type = new_type(TYPE_INT | spec, 0, 0);
8752 break;
8753 case TOK_LONG:
8754 eat(state, TOK_LONG);
8755 switch(peek(state)) {
8756 case TOK_LONG:
8757 eat(state, TOK_LONG);
8758 error(state, 0, "long long not supported");
8759 break;
8760 case TOK_DOUBLE:
8761 eat(state, TOK_DOUBLE);
8762 error(state, 0, "long double not supported");
8763 break;
8764 case TOK_INT:
8765 eat(state, TOK_INT);
8766 type = new_type(TYPE_LONG | spec, 0, 0);
8767 break;
8768 default:
8769 type = new_type(TYPE_LONG | spec, 0, 0);
8770 break;
8771 }
8772 break;
8773 case TOK_FLOAT:
8774 eat(state, TOK_FLOAT);
8775 error(state, 0, "type float not supported");
8776 break;
8777 case TOK_DOUBLE:
8778 eat(state, TOK_DOUBLE);
8779 error(state, 0, "type double not supported");
8780 break;
8781 case TOK_SIGNED:
8782 eat(state, TOK_SIGNED);
8783 switch(peek(state)) {
8784 case TOK_LONG:
8785 eat(state, TOK_LONG);
8786 switch(peek(state)) {
8787 case TOK_LONG:
8788 eat(state, TOK_LONG);
8789 error(state, 0, "type long long not supported");
8790 break;
8791 case TOK_INT:
8792 eat(state, TOK_INT);
8793 type = new_type(TYPE_LONG | spec, 0, 0);
8794 break;
8795 default:
8796 type = new_type(TYPE_LONG | spec, 0, 0);
8797 break;
8798 }
8799 break;
8800 case TOK_INT:
8801 eat(state, TOK_INT);
8802 type = new_type(TYPE_INT | spec, 0, 0);
8803 break;
8804 case TOK_SHORT:
8805 eat(state, TOK_SHORT);
8806 type = new_type(TYPE_SHORT | spec, 0, 0);
8807 break;
8808 case TOK_CHAR:
8809 eat(state, TOK_CHAR);
8810 type = new_type(TYPE_CHAR | spec, 0, 0);
8811 break;
8812 default:
8813 type = new_type(TYPE_INT | spec, 0, 0);
8814 break;
8815 }
8816 break;
8817 case TOK_UNSIGNED:
8818 eat(state, TOK_UNSIGNED);
8819 switch(peek(state)) {
8820 case TOK_LONG:
8821 eat(state, TOK_LONG);
8822 switch(peek(state)) {
8823 case TOK_LONG:
8824 eat(state, TOK_LONG);
8825 error(state, 0, "unsigned long long not supported");
8826 break;
8827 case TOK_INT:
8828 eat(state, TOK_INT);
8829 type = new_type(TYPE_ULONG | spec, 0, 0);
8830 break;
8831 default:
8832 type = new_type(TYPE_ULONG | spec, 0, 0);
8833 break;
8834 }
8835 break;
8836 case TOK_INT:
8837 eat(state, TOK_INT);
8838 type = new_type(TYPE_UINT | spec, 0, 0);
8839 break;
8840 case TOK_SHORT:
8841 eat(state, TOK_SHORT);
8842 type = new_type(TYPE_USHORT | spec, 0, 0);
8843 break;
8844 case TOK_CHAR:
8845 eat(state, TOK_CHAR);
8846 type = new_type(TYPE_UCHAR | spec, 0, 0);
8847 break;
8848 default:
8849 type = new_type(TYPE_UINT | spec, 0, 0);
8850 break;
8851 }
8852 break;
8853 /* struct or union specifier */
8854 case TOK_STRUCT:
8855 case TOK_UNION:
8856 type = struct_or_union_specifier(state, spec);
8857 break;
8858 /* enum-spefifier */
8859 case TOK_ENUM:
8860 type = enum_specifier(state, spec);
8861 break;
8862 /* typedef name */
8863 case TOK_TYPE_NAME:
8864 type = typedef_name(state, spec);
8865 break;
8866 default:
8867 error(state, 0, "bad type specifier %s",
8868 tokens[peek(state)]);
8869 break;
8870 }
8871 return type;
8872}
8873
8874static int istype(int tok)
8875{
8876 switch(tok) {
8877 case TOK_CONST:
8878 case TOK_RESTRICT:
8879 case TOK_VOLATILE:
8880 case TOK_VOID:
8881 case TOK_CHAR:
8882 case TOK_SHORT:
8883 case TOK_INT:
8884 case TOK_LONG:
8885 case TOK_FLOAT:
8886 case TOK_DOUBLE:
8887 case TOK_SIGNED:
8888 case TOK_UNSIGNED:
8889 case TOK_STRUCT:
8890 case TOK_UNION:
8891 case TOK_ENUM:
8892 case TOK_TYPE_NAME:
8893 return 1;
8894 default:
8895 return 0;
8896 }
8897}
8898
8899
8900static struct type *specifier_qualifier_list(struct compile_state *state)
8901{
8902 struct type *type;
8903 unsigned int specifiers = 0;
8904
8905 /* type qualifiers */
8906 specifiers |= type_qualifiers(state);
8907
8908 /* type specifier */
8909 type = type_specifier(state, specifiers);
8910
8911 return type;
8912}
8913
8914static int isdecl_specifier(int tok)
8915{
8916 switch(tok) {
8917 /* storage class specifier */
8918 case TOK_AUTO:
8919 case TOK_REGISTER:
8920 case TOK_STATIC:
8921 case TOK_EXTERN:
8922 case TOK_TYPEDEF:
8923 /* type qualifier */
8924 case TOK_CONST:
8925 case TOK_RESTRICT:
8926 case TOK_VOLATILE:
8927 /* type specifiers */
8928 case TOK_VOID:
8929 case TOK_CHAR:
8930 case TOK_SHORT:
8931 case TOK_INT:
8932 case TOK_LONG:
8933 case TOK_FLOAT:
8934 case TOK_DOUBLE:
8935 case TOK_SIGNED:
8936 case TOK_UNSIGNED:
8937 /* struct or union specifier */
8938 case TOK_STRUCT:
8939 case TOK_UNION:
8940 /* enum-spefifier */
8941 case TOK_ENUM:
8942 /* typedef name */
8943 case TOK_TYPE_NAME:
8944 /* function specifiers */
8945 case TOK_INLINE:
8946 return 1;
8947 default:
8948 return 0;
8949 }
8950}
8951
8952static struct type *decl_specifiers(struct compile_state *state)
8953{
8954 struct type *type;
8955 unsigned int specifiers;
8956 /* I am overly restrictive in the arragement of specifiers supported.
8957 * C is overly flexible in this department it makes interpreting
8958 * the parse tree difficult.
8959 */
8960 specifiers = 0;
8961
8962 /* storage class specifier */
8963 specifiers |= storage_class_specifier_opt(state);
8964
8965 /* function-specifier */
8966 specifiers |= function_specifier_opt(state);
8967
8968 /* type qualifier */
8969 specifiers |= type_qualifiers(state);
8970
8971 /* type specifier */
8972 type = type_specifier(state, specifiers);
8973 return type;
8974}
8975
Eric Biederman00443072003-06-24 12:34:45 +00008976struct field_info {
8977 struct type *type;
8978 size_t offset;
8979};
8980
8981static struct field_info designator(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008982{
8983 int tok;
Eric Biederman00443072003-06-24 12:34:45 +00008984 struct field_info info;
8985 info.offset = ~0U;
8986 info.type = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008987 do {
8988 switch(peek(state)) {
8989 case TOK_LBRACKET:
8990 {
8991 struct triple *value;
Eric Biederman00443072003-06-24 12:34:45 +00008992 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
8993 error(state, 0, "Array designator not in array initializer");
8994 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008995 eat(state, TOK_LBRACKET);
8996 value = constant_expr(state);
8997 eat(state, TOK_RBRACKET);
Eric Biederman00443072003-06-24 12:34:45 +00008998
8999 info.type = type->left;
9000 info.offset = value->u.cval * size_of(state, info.type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009001 break;
9002 }
9003 case TOK_DOT:
Eric Biederman00443072003-06-24 12:34:45 +00009004 {
9005 struct hash_entry *field;
Eric Biederman00443072003-06-24 12:34:45 +00009006 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
9007 error(state, 0, "Struct designator not in struct initializer");
9008 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009009 eat(state, TOK_DOT);
9010 eat(state, TOK_IDENT);
Eric Biederman00443072003-06-24 12:34:45 +00009011 field = state->token[0].ident;
Eric Biederman03b59862003-06-24 14:27:37 +00009012 info.offset = field_offset(state, type, field);
9013 info.type = field_type(state, type, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009014 break;
Eric Biederman00443072003-06-24 12:34:45 +00009015 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009016 default:
9017 error(state, 0, "Invalid designator");
9018 }
9019 tok = peek(state);
9020 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
9021 eat(state, TOK_EQ);
Eric Biederman00443072003-06-24 12:34:45 +00009022 return info;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009023}
9024
9025static struct triple *initializer(
9026 struct compile_state *state, struct type *type)
9027{
9028 struct triple *result;
9029 if (peek(state) != TOK_LBRACE) {
9030 result = assignment_expr(state);
9031 }
9032 else {
9033 int comma;
Eric Biederman00443072003-06-24 12:34:45 +00009034 size_t max_offset;
9035 struct field_info info;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009036 void *buf;
Eric Biederman00443072003-06-24 12:34:45 +00009037 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
9038 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
9039 internal_error(state, 0, "unknown initializer type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00009040 }
Eric Biederman00443072003-06-24 12:34:45 +00009041 info.offset = 0;
9042 info.type = type->left;
Eric Biederman03b59862003-06-24 14:27:37 +00009043 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
9044 info.type = next_field(state, type, 0);
9045 }
Eric Biederman00443072003-06-24 12:34:45 +00009046 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
9047 max_offset = 0;
9048 } else {
9049 max_offset = size_of(state, type);
9050 }
9051 buf = xcmalloc(max_offset, "initializer");
Eric Biedermanb138ac82003-04-22 18:44:01 +00009052 eat(state, TOK_LBRACE);
9053 do {
9054 struct triple *value;
9055 struct type *value_type;
9056 size_t value_size;
Eric Biederman00443072003-06-24 12:34:45 +00009057 void *dest;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009058 int tok;
9059 comma = 0;
9060 tok = peek(state);
9061 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
Eric Biederman00443072003-06-24 12:34:45 +00009062 info = designator(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009063 }
Eric Biederman00443072003-06-24 12:34:45 +00009064 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
9065 (info.offset >= max_offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009066 error(state, 0, "element beyond bounds");
9067 }
Eric Biederman00443072003-06-24 12:34:45 +00009068 value_type = info.type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009069 value = eval_const_expr(state, initializer(state, value_type));
9070 value_size = size_of(state, value_type);
9071 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
Eric Biederman00443072003-06-24 12:34:45 +00009072 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
9073 (max_offset <= info.offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009074 void *old_buf;
9075 size_t old_size;
9076 old_buf = buf;
Eric Biederman00443072003-06-24 12:34:45 +00009077 old_size = max_offset;
9078 max_offset = info.offset + value_size;
9079 buf = xmalloc(max_offset, "initializer");
Eric Biedermanb138ac82003-04-22 18:44:01 +00009080 memcpy(buf, old_buf, old_size);
9081 xfree(old_buf);
9082 }
Eric Biederman00443072003-06-24 12:34:45 +00009083 dest = ((char *)buf) + info.offset;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009084 if (value->op == OP_BLOBCONST) {
Eric Biederman00443072003-06-24 12:34:45 +00009085 memcpy(dest, value->u.blob, value_size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009086 }
9087 else if ((value->op == OP_INTCONST) && (value_size == 1)) {
Eric Biederman00443072003-06-24 12:34:45 +00009088 *((uint8_t *)dest) = value->u.cval & 0xff;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009089 }
9090 else if ((value->op == OP_INTCONST) && (value_size == 2)) {
Eric Biederman00443072003-06-24 12:34:45 +00009091 *((uint16_t *)dest) = value->u.cval & 0xffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009092 }
9093 else if ((value->op == OP_INTCONST) && (value_size == 4)) {
Eric Biederman00443072003-06-24 12:34:45 +00009094 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009095 }
9096 else {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009097 internal_error(state, 0, "unhandled constant initializer");
9098 }
Eric Biederman00443072003-06-24 12:34:45 +00009099 free_triple(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009100 if (peek(state) == TOK_COMMA) {
9101 eat(state, TOK_COMMA);
9102 comma = 1;
9103 }
Eric Biederman00443072003-06-24 12:34:45 +00009104 info.offset += value_size;
Eric Biederman03b59862003-06-24 14:27:37 +00009105 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
9106 info.type = next_field(state, type, info.type);
9107 info.offset = field_offset(state, type,
9108 info.type->field_ident);
Eric Biederman00443072003-06-24 12:34:45 +00009109 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009110 } while(comma && (peek(state) != TOK_RBRACE));
Eric Biederman00443072003-06-24 12:34:45 +00009111 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
9112 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
9113 type->elements = max_offset / size_of(state, type->left);
9114 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009115 eat(state, TOK_RBRACE);
9116 result = triple(state, OP_BLOBCONST, type, 0, 0);
9117 result->u.blob = buf;
9118 }
9119 return result;
9120}
9121
Eric Biederman153ea352003-06-20 14:43:20 +00009122static void resolve_branches(struct compile_state *state)
9123{
9124 /* Make a second pass and finish anything outstanding
9125 * with respect to branches. The only outstanding item
9126 * is to see if there are goto to labels that have not
9127 * been defined and to error about them.
9128 */
9129 int i;
9130 for(i = 0; i < HASH_TABLE_SIZE; i++) {
9131 struct hash_entry *entry;
9132 for(entry = state->hash_table[i]; entry; entry = entry->next) {
9133 struct triple *ins;
9134 if (!entry->sym_label) {
9135 continue;
9136 }
9137 ins = entry->sym_label->def;
9138 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
9139 error(state, ins, "label `%s' used but not defined",
9140 entry->name);
9141 }
9142 }
9143 }
9144}
9145
Eric Biedermanb138ac82003-04-22 18:44:01 +00009146static struct triple *function_definition(
9147 struct compile_state *state, struct type *type)
9148{
9149 struct triple *def, *tmp, *first, *end;
9150 struct hash_entry *ident;
9151 struct type *param;
9152 int i;
9153 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
9154 error(state, 0, "Invalid function header");
9155 }
9156
9157 /* Verify the function type */
9158 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
9159 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +00009160 (type->right->field_ident == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009161 error(state, 0, "Invalid function parameters");
9162 }
9163 param = type->right;
9164 i = 0;
9165 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
9166 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009167 if (!param->left->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009168 error(state, 0, "No identifier for parameter %d\n", i);
9169 }
9170 param = param->right;
9171 }
9172 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009173 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009174 error(state, 0, "No identifier for paramter %d\n", i);
9175 }
9176
9177 /* Get a list of statements for this function. */
9178 def = triple(state, OP_LIST, type, 0, 0);
9179
9180 /* Start a new scope for the passed parameters */
9181 start_scope(state);
9182
9183 /* Put a label at the very start of a function */
9184 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009185 RHS(def, 0) = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009186
9187 /* Put a label at the very end of a function */
9188 end = label(state);
9189 flatten(state, first, end);
9190
9191 /* Walk through the parameters and create symbol table entries
9192 * for them.
9193 */
9194 param = type->right;
9195 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009196 ident = param->left->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009197 tmp = variable(state, param->left);
9198 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
9199 flatten(state, end, tmp);
9200 param = param->right;
9201 }
9202 if ((param->type & TYPE_MASK) != TYPE_VOID) {
9203 /* And don't forget the last parameter */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009204 ident = param->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009205 tmp = variable(state, param);
9206 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
9207 flatten(state, end, tmp);
9208 }
9209 /* Add a variable for the return value */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009210 MISC(def, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009211 if ((type->left->type & TYPE_MASK) != TYPE_VOID) {
9212 /* Remove all type qualifiers from the return type */
9213 tmp = variable(state, clone_type(0, type->left));
9214 flatten(state, end, tmp);
9215 /* Remember where the return value is */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009216 MISC(def, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009217 }
9218
9219 /* Remember which function I am compiling.
9220 * Also assume the last defined function is the main function.
9221 */
9222 state->main_function = def;
9223
9224 /* Now get the actual function definition */
9225 compound_statement(state, end);
9226
Eric Biederman153ea352003-06-20 14:43:20 +00009227 /* Finish anything unfinished with branches */
9228 resolve_branches(state);
9229
Eric Biedermanb138ac82003-04-22 18:44:01 +00009230 /* Remove the parameter scope */
9231 end_scope(state);
Eric Biederman153ea352003-06-20 14:43:20 +00009232
Eric Biedermanb138ac82003-04-22 18:44:01 +00009233#if 0
9234 fprintf(stdout, "\n");
9235 loc(stdout, state, 0);
9236 fprintf(stdout, "\n__________ function_definition _________\n");
9237 print_triple(state, def);
9238 fprintf(stdout, "__________ function_definition _________ done\n\n");
9239#endif
9240
9241 return def;
9242}
9243
9244static struct triple *do_decl(struct compile_state *state,
9245 struct type *type, struct hash_entry *ident)
9246{
9247 struct triple *def;
9248 def = 0;
9249 /* Clean up the storage types used */
9250 switch (type->type & STOR_MASK) {
9251 case STOR_AUTO:
9252 case STOR_STATIC:
9253 /* These are the good types I am aiming for */
9254 break;
9255 case STOR_REGISTER:
9256 type->type &= ~STOR_MASK;
9257 type->type |= STOR_AUTO;
9258 break;
9259 case STOR_EXTERN:
9260 type->type &= ~STOR_MASK;
9261 type->type |= STOR_STATIC;
9262 break;
9263 case STOR_TYPEDEF:
Eric Biederman0babc1c2003-05-09 02:39:00 +00009264 if (!ident) {
9265 error(state, 0, "typedef without name");
9266 }
9267 symbol(state, ident, &ident->sym_ident, 0, type);
9268 ident->tok = TOK_TYPE_NAME;
9269 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009270 break;
9271 default:
9272 internal_error(state, 0, "Undefined storage class");
9273 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00009274 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
9275 error(state, 0, "Function prototypes not supported");
9276 }
Eric Biederman00443072003-06-24 12:34:45 +00009277 if (ident &&
9278 ((type->type & STOR_MASK) == STOR_STATIC) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00009279 ((type->type & QUAL_CONST) == 0)) {
9280 error(state, 0, "non const static variables not supported");
9281 }
9282 if (ident) {
9283 def = variable(state, type);
9284 symbol(state, ident, &ident->sym_ident, def, type);
9285 }
9286 return def;
9287}
9288
9289static void decl(struct compile_state *state, struct triple *first)
9290{
9291 struct type *base_type, *type;
9292 struct hash_entry *ident;
9293 struct triple *def;
9294 int global;
9295 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
9296 base_type = decl_specifiers(state);
9297 ident = 0;
9298 type = declarator(state, base_type, &ident, 0);
9299 if (global && ident && (peek(state) == TOK_LBRACE)) {
9300 /* function */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00009301 state->function = ident->name;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009302 def = function_definition(state, type);
9303 symbol(state, ident, &ident->sym_ident, def, type);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00009304 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009305 }
9306 else {
9307 int done;
9308 flatten(state, first, do_decl(state, type, ident));
9309 /* type or variable definition */
9310 do {
9311 done = 1;
9312 if (peek(state) == TOK_EQ) {
9313 if (!ident) {
9314 error(state, 0, "cannot assign to a type");
9315 }
9316 eat(state, TOK_EQ);
9317 flatten(state, first,
9318 init_expr(state,
9319 ident->sym_ident->def,
9320 initializer(state, type)));
9321 }
9322 arrays_complete(state, type);
9323 if (peek(state) == TOK_COMMA) {
9324 eat(state, TOK_COMMA);
9325 ident = 0;
9326 type = declarator(state, base_type, &ident, 0);
9327 flatten(state, first, do_decl(state, type, ident));
9328 done = 0;
9329 }
9330 } while(!done);
9331 eat(state, TOK_SEMI);
9332 }
9333}
9334
9335static void decls(struct compile_state *state)
9336{
9337 struct triple *list;
9338 int tok;
9339 list = label(state);
9340 while(1) {
9341 tok = peek(state);
9342 if (tok == TOK_EOF) {
9343 return;
9344 }
9345 if (tok == TOK_SPACE) {
9346 eat(state, TOK_SPACE);
9347 }
9348 decl(state, list);
9349 if (list->next != list) {
9350 error(state, 0, "global variables not supported");
9351 }
9352 }
9353}
9354
9355/*
9356 * Data structurs for optimation.
9357 */
9358
9359static void do_use_block(
9360 struct block *used, struct block_set **head, struct block *user,
9361 int front)
9362{
9363 struct block_set **ptr, *new;
9364 if (!used)
9365 return;
9366 if (!user)
9367 return;
9368 ptr = head;
9369 while(*ptr) {
9370 if ((*ptr)->member == user) {
9371 return;
9372 }
9373 ptr = &(*ptr)->next;
9374 }
9375 new = xcmalloc(sizeof(*new), "block_set");
9376 new->member = user;
9377 if (front) {
9378 new->next = *head;
9379 *head = new;
9380 }
9381 else {
9382 new->next = 0;
9383 *ptr = new;
9384 }
9385}
9386static void do_unuse_block(
9387 struct block *used, struct block_set **head, struct block *unuser)
9388{
9389 struct block_set *use, **ptr;
9390 ptr = head;
9391 while(*ptr) {
9392 use = *ptr;
9393 if (use->member == unuser) {
9394 *ptr = use->next;
9395 memset(use, -1, sizeof(*use));
9396 xfree(use);
9397 }
9398 else {
9399 ptr = &use->next;
9400 }
9401 }
9402}
9403
9404static void use_block(struct block *used, struct block *user)
9405{
9406 /* Append new to the head of the list, print_block
9407 * depends on this.
9408 */
9409 do_use_block(used, &used->use, user, 1);
9410 used->users++;
9411}
9412static void unuse_block(struct block *used, struct block *unuser)
9413{
9414 do_unuse_block(used, &used->use, unuser);
9415 used->users--;
9416}
9417
9418static void idom_block(struct block *idom, struct block *user)
9419{
9420 do_use_block(idom, &idom->idominates, user, 0);
9421}
9422
9423static void unidom_block(struct block *idom, struct block *unuser)
9424{
9425 do_unuse_block(idom, &idom->idominates, unuser);
9426}
9427
9428static void domf_block(struct block *block, struct block *domf)
9429{
9430 do_use_block(block, &block->domfrontier, domf, 0);
9431}
9432
9433static void undomf_block(struct block *block, struct block *undomf)
9434{
9435 do_unuse_block(block, &block->domfrontier, undomf);
9436}
9437
9438static void ipdom_block(struct block *ipdom, struct block *user)
9439{
9440 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
9441}
9442
9443static void unipdom_block(struct block *ipdom, struct block *unuser)
9444{
9445 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
9446}
9447
9448static void ipdomf_block(struct block *block, struct block *ipdomf)
9449{
9450 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
9451}
9452
9453static void unipdomf_block(struct block *block, struct block *unipdomf)
9454{
9455 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
9456}
9457
9458
9459
9460static int do_walk_triple(struct compile_state *state,
9461 struct triple *ptr, int depth,
9462 int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
9463{
9464 int result;
9465 result = cb(state, ptr, depth);
9466 if ((result == 0) && (ptr->op == OP_LIST)) {
9467 struct triple *list;
9468 list = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009469 ptr = RHS(list, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009470 do {
9471 result = do_walk_triple(state, ptr, depth + 1, cb);
9472 if (ptr->next->prev != ptr) {
9473 internal_error(state, ptr->next, "bad prev");
9474 }
9475 ptr = ptr->next;
9476
Eric Biederman0babc1c2003-05-09 02:39:00 +00009477 } while((result == 0) && (ptr != RHS(list, 0)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009478 }
9479 return result;
9480}
9481
9482static int walk_triple(
9483 struct compile_state *state,
9484 struct triple *ptr,
9485 int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
9486{
9487 return do_walk_triple(state, ptr, 0, cb);
9488}
9489
9490static void do_print_prefix(int depth)
9491{
9492 int i;
9493 for(i = 0; i < depth; i++) {
9494 printf(" ");
9495 }
9496}
9497
9498#define PRINT_LIST 1
9499static int do_print_triple(struct compile_state *state, struct triple *ins, int depth)
9500{
9501 int op;
9502 op = ins->op;
9503 if (op == OP_LIST) {
9504#if !PRINT_LIST
9505 return 0;
9506#endif
9507 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009508 if ((op == OP_LABEL) && (ins->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009509 printf("\n%p:\n", ins);
9510 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009511 do_print_prefix(depth);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009512 display_triple(stdout, ins);
9513
Eric Biedermanb138ac82003-04-22 18:44:01 +00009514 if ((ins->op == OP_BRANCH) && ins->use) {
9515 internal_error(state, ins, "branch used?");
9516 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009517 if (triple_is_branch(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009518 printf("\n");
9519 }
9520 return 0;
9521}
9522
9523static void print_triple(struct compile_state *state, struct triple *ins)
9524{
9525 walk_triple(state, ins, do_print_triple);
9526}
9527
9528static void print_triples(struct compile_state *state)
9529{
9530 print_triple(state, state->main_function);
9531}
9532
9533struct cf_block {
9534 struct block *block;
9535};
9536static void find_cf_blocks(struct cf_block *cf, struct block *block)
9537{
9538 if (!block || (cf[block->vertex].block == block)) {
9539 return;
9540 }
9541 cf[block->vertex].block = block;
9542 find_cf_blocks(cf, block->left);
9543 find_cf_blocks(cf, block->right);
9544}
9545
9546static void print_control_flow(struct compile_state *state)
9547{
9548 struct cf_block *cf;
9549 int i;
9550 printf("\ncontrol flow\n");
9551 cf = xcmalloc(sizeof(*cf) * (state->last_vertex + 1), "cf_block");
9552 find_cf_blocks(cf, state->first_block);
9553
9554 for(i = 1; i <= state->last_vertex; i++) {
9555 struct block *block;
9556 block = cf[i].block;
9557 if (!block)
9558 continue;
9559 printf("(%p) %d:", block, block->vertex);
9560 if (block->left) {
9561 printf(" %d", block->left->vertex);
9562 }
9563 if (block->right && (block->right != block->left)) {
9564 printf(" %d", block->right->vertex);
9565 }
9566 printf("\n");
9567 }
9568
9569 xfree(cf);
9570}
9571
9572
9573static struct block *basic_block(struct compile_state *state,
9574 struct triple *first)
9575{
9576 struct block *block;
9577 struct triple *ptr;
9578 int op;
9579 if (first->op != OP_LABEL) {
9580 internal_error(state, 0, "block does not start with a label");
9581 }
9582 /* See if this basic block has already been setup */
9583 if (first->u.block != 0) {
9584 return first->u.block;
9585 }
9586 /* Allocate another basic block structure */
9587 state->last_vertex += 1;
9588 block = xcmalloc(sizeof(*block), "block");
9589 block->first = block->last = first;
9590 block->vertex = state->last_vertex;
9591 ptr = first;
9592 do {
9593 if ((ptr != first) && (ptr->op == OP_LABEL) && ptr->use) {
9594 break;
9595 }
9596 block->last = ptr;
9597 /* If ptr->u is not used remember where the baic block is */
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009598 if (triple_stores_block(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009599 ptr->u.block = block;
9600 }
9601 if (ptr->op == OP_BRANCH) {
9602 break;
9603 }
9604 ptr = ptr->next;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009605 } while (ptr != RHS(state->main_function, 0));
9606 if (ptr == RHS(state->main_function, 0))
Eric Biedermanb138ac82003-04-22 18:44:01 +00009607 return block;
9608 op = ptr->op;
9609 if (op == OP_LABEL) {
9610 block->left = basic_block(state, ptr);
9611 block->right = 0;
9612 use_block(block->left, block);
9613 }
9614 else if (op == OP_BRANCH) {
9615 block->left = 0;
9616 /* Trace the branch target */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009617 block->right = basic_block(state, TARG(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009618 use_block(block->right, block);
9619 /* If there is a test trace the branch as well */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009620 if (TRIPLE_RHS(ptr->sizes)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009621 block->left = basic_block(state, ptr->next);
9622 use_block(block->left, block);
9623 }
9624 }
9625 else {
9626 internal_error(state, 0, "Bad basic block split");
9627 }
9628 return block;
9629}
9630
9631
9632static void walk_blocks(struct compile_state *state,
9633 void (*cb)(struct compile_state *state, struct block *block, void *arg),
9634 void *arg)
9635{
9636 struct triple *ptr, *first;
9637 struct block *last_block;
9638 last_block = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009639 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009640 ptr = first;
9641 do {
9642 struct block *block;
Eric Biederman530b5192003-07-01 10:05:30 +00009643 if (triple_stores_block(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009644 block = ptr->u.block;
9645 if (block && (block != last_block)) {
9646 cb(state, block, arg);
9647 }
9648 last_block = block;
9649 }
Eric Biederman530b5192003-07-01 10:05:30 +00009650 if (block && (block->last == ptr)) {
9651 block = 0;
9652 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009653 ptr = ptr->next;
9654 } while(ptr != first);
9655}
9656
9657static void print_block(
9658 struct compile_state *state, struct block *block, void *arg)
9659{
Eric Biederman530b5192003-07-01 10:05:30 +00009660 struct block_set *user;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009661 struct triple *ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009662 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009663
Eric Biederman530b5192003-07-01 10:05:30 +00009664 fprintf(fp, "\nblock: %p (%d) %p<-%p %p<-%p\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +00009665 block,
9666 block->vertex,
9667 block->left,
9668 block->left && block->left->use?block->left->use->member : 0,
9669 block->right,
9670 block->right && block->right->use?block->right->use->member : 0);
9671 if (block->first->op == OP_LABEL) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009672 fprintf(fp, "%p:\n", block->first);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009673 }
9674 for(ptr = block->first; ; ptr = ptr->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009675 display_triple(fp, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009676 if (ptr == block->last)
9677 break;
9678 }
Eric Biederman530b5192003-07-01 10:05:30 +00009679 fprintf(fp, "users %d: ", block->users);
9680 for(user = block->use; user; user = user->next) {
9681 fprintf(fp, "%p (%d) ",
9682 user->member,
9683 user->member->vertex);
9684 }
9685 fprintf(fp,"\n\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00009686}
9687
9688
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009689static void print_blocks(struct compile_state *state, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +00009690{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009691 fprintf(fp, "--------------- blocks ---------------\n");
9692 walk_blocks(state, print_block, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009693}
9694
9695static void prune_nonblock_triples(struct compile_state *state)
9696{
9697 struct block *block;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009698 struct triple *first, *ins, *next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009699 /* Delete the triples not in a basic block */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009700 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009701 block = 0;
9702 ins = first;
9703 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009704 next = ins->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009705 if (ins->op == OP_LABEL) {
9706 block = ins->u.block;
9707 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009708 if (!block) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009709 release_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009710 }
Eric Biederman530b5192003-07-01 10:05:30 +00009711 if (block && block->last == ins) {
9712 block = 0;
9713 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009714 ins = next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009715 } while(ins != first);
9716}
9717
9718static void setup_basic_blocks(struct compile_state *state)
9719{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009720 if (!triple_stores_block(state, RHS(state->main_function, 0)) ||
9721 !triple_stores_block(state, RHS(state->main_function,0)->prev)) {
9722 internal_error(state, 0, "ins will not store block?");
9723 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009724 /* Find the basic blocks */
9725 state->last_vertex = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009726 state->first_block = basic_block(state, RHS(state->main_function,0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009727 /* Delete the triples not in a basic block */
9728 prune_nonblock_triples(state);
9729 /* Find the last basic block */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009730 state->last_block = RHS(state->main_function, 0)->prev->u.block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009731 if (!state->last_block) {
9732 internal_error(state, 0, "end not used?");
9733 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009734 /* If we are debugging print what I have just done */
9735 if (state->debug & DEBUG_BASIC_BLOCKS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009736 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009737 print_control_flow(state);
9738 }
9739}
9740
9741static void free_basic_block(struct compile_state *state, struct block *block)
9742{
9743 struct block_set *entry, *next;
9744 struct block *child;
9745 if (!block) {
9746 return;
9747 }
9748 if (block->vertex == -1) {
9749 return;
9750 }
9751 block->vertex = -1;
9752 if (block->left) {
9753 unuse_block(block->left, block);
9754 }
9755 if (block->right) {
9756 unuse_block(block->right, block);
9757 }
9758 if (block->idom) {
9759 unidom_block(block->idom, block);
9760 }
9761 block->idom = 0;
9762 if (block->ipdom) {
9763 unipdom_block(block->ipdom, block);
9764 }
9765 block->ipdom = 0;
9766 for(entry = block->use; entry; entry = next) {
9767 next = entry->next;
9768 child = entry->member;
9769 unuse_block(block, child);
9770 if (child->left == block) {
9771 child->left = 0;
9772 }
9773 if (child->right == block) {
9774 child->right = 0;
9775 }
9776 }
9777 for(entry = block->idominates; entry; entry = next) {
9778 next = entry->next;
9779 child = entry->member;
9780 unidom_block(block, child);
9781 child->idom = 0;
9782 }
9783 for(entry = block->domfrontier; entry; entry = next) {
9784 next = entry->next;
9785 child = entry->member;
9786 undomf_block(block, child);
9787 }
9788 for(entry = block->ipdominates; entry; entry = next) {
9789 next = entry->next;
9790 child = entry->member;
9791 unipdom_block(block, child);
9792 child->ipdom = 0;
9793 }
9794 for(entry = block->ipdomfrontier; entry; entry = next) {
9795 next = entry->next;
9796 child = entry->member;
9797 unipdomf_block(block, child);
9798 }
9799 if (block->users != 0) {
9800 internal_error(state, 0, "block still has users");
9801 }
9802 free_basic_block(state, block->left);
9803 block->left = 0;
9804 free_basic_block(state, block->right);
9805 block->right = 0;
9806 memset(block, -1, sizeof(*block));
9807 xfree(block);
9808}
9809
9810static void free_basic_blocks(struct compile_state *state)
9811{
9812 struct triple *first, *ins;
9813 free_basic_block(state, state->first_block);
9814 state->last_vertex = 0;
9815 state->first_block = state->last_block = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009816 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009817 ins = first;
9818 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009819 if (triple_stores_block(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009820 ins->u.block = 0;
9821 }
9822 ins = ins->next;
9823 } while(ins != first);
9824
9825}
9826
9827struct sdom_block {
9828 struct block *block;
9829 struct sdom_block *sdominates;
9830 struct sdom_block *sdom_next;
9831 struct sdom_block *sdom;
9832 struct sdom_block *label;
9833 struct sdom_block *parent;
9834 struct sdom_block *ancestor;
9835 int vertex;
9836};
9837
9838
9839static void unsdom_block(struct sdom_block *block)
9840{
9841 struct sdom_block **ptr;
9842 if (!block->sdom_next) {
9843 return;
9844 }
9845 ptr = &block->sdom->sdominates;
9846 while(*ptr) {
9847 if ((*ptr) == block) {
9848 *ptr = block->sdom_next;
9849 return;
9850 }
9851 ptr = &(*ptr)->sdom_next;
9852 }
9853}
9854
9855static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
9856{
9857 unsdom_block(block);
9858 block->sdom = sdom;
9859 block->sdom_next = sdom->sdominates;
9860 sdom->sdominates = block;
9861}
9862
9863
9864
9865static int initialize_sdblock(struct sdom_block *sd,
9866 struct block *parent, struct block *block, int vertex)
9867{
9868 if (!block || (sd[block->vertex].block == block)) {
9869 return vertex;
9870 }
9871 vertex += 1;
9872 /* Renumber the blocks in a convinient fashion */
9873 block->vertex = vertex;
9874 sd[vertex].block = block;
9875 sd[vertex].sdom = &sd[vertex];
9876 sd[vertex].label = &sd[vertex];
9877 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
9878 sd[vertex].ancestor = 0;
9879 sd[vertex].vertex = vertex;
9880 vertex = initialize_sdblock(sd, block, block->left, vertex);
9881 vertex = initialize_sdblock(sd, block, block->right, vertex);
9882 return vertex;
9883}
9884
Eric Biederman530b5192003-07-01 10:05:30 +00009885static int initialize_sdpblock(
9886 struct compile_state *state, struct sdom_block *sd,
Eric Biedermanb138ac82003-04-22 18:44:01 +00009887 struct block *parent, struct block *block, int vertex)
9888{
9889 struct block_set *user;
9890 if (!block || (sd[block->vertex].block == block)) {
9891 return vertex;
9892 }
9893 vertex += 1;
9894 /* Renumber the blocks in a convinient fashion */
9895 block->vertex = vertex;
9896 sd[vertex].block = block;
9897 sd[vertex].sdom = &sd[vertex];
9898 sd[vertex].label = &sd[vertex];
9899 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
9900 sd[vertex].ancestor = 0;
9901 sd[vertex].vertex = vertex;
9902 for(user = block->use; user; user = user->next) {
Eric Biederman530b5192003-07-01 10:05:30 +00009903 vertex = initialize_sdpblock(state, sd, block, user->member, vertex);
9904 }
9905 return vertex;
9906}
9907
9908static int setup_sdpblocks(struct compile_state *state, struct sdom_block *sd)
9909{
9910 struct block *block;
9911 int vertex;
9912 /* Setup as many sdpblocks as possible without using fake edges */
9913 vertex = initialize_sdpblock(state, sd, 0, state->last_block, 0);
9914
9915 /* Walk through the graph and find unconnected blocks. If
9916 * we can, add a fake edge from the unconnected blocks to the
9917 * end of the graph.
9918 */
9919 block = state->first_block->last->next->u.block;
9920 for(; block && block != state->first_block; block = block->last->next->u.block) {
9921 if (sd[block->vertex].block == block) {
9922 continue;
9923 }
9924 if (block->left != 0) {
9925 continue;
9926 }
9927
9928#if DEBUG_SDP_BLOCKS
9929 fprintf(stderr, "Adding %d\n", vertex +1);
9930#endif
9931
9932 block->left = state->last_block;
9933 use_block(block->left, block);
9934 vertex = initialize_sdpblock(state, sd, state->last_block, block, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009935 }
9936 return vertex;
9937}
9938
9939static void compress_ancestors(struct sdom_block *v)
9940{
9941 /* This procedure assumes ancestor(v) != 0 */
9942 /* if (ancestor(ancestor(v)) != 0) {
9943 * compress(ancestor(ancestor(v)));
9944 * if (semi(label(ancestor(v))) < semi(label(v))) {
9945 * label(v) = label(ancestor(v));
9946 * }
9947 * ancestor(v) = ancestor(ancestor(v));
9948 * }
9949 */
9950 if (!v->ancestor) {
9951 return;
9952 }
9953 if (v->ancestor->ancestor) {
9954 compress_ancestors(v->ancestor->ancestor);
9955 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
9956 v->label = v->ancestor->label;
9957 }
9958 v->ancestor = v->ancestor->ancestor;
9959 }
9960}
9961
9962static void compute_sdom(struct compile_state *state, struct sdom_block *sd)
9963{
9964 int i;
9965 /* // step 2
9966 * for each v <= pred(w) {
9967 * u = EVAL(v);
9968 * if (semi[u] < semi[w] {
9969 * semi[w] = semi[u];
9970 * }
9971 * }
9972 * add w to bucket(vertex(semi[w]));
9973 * LINK(parent(w), w);
9974 *
9975 * // step 3
9976 * for each v <= bucket(parent(w)) {
9977 * delete v from bucket(parent(w));
9978 * u = EVAL(v);
9979 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
9980 * }
9981 */
9982 for(i = state->last_vertex; i >= 2; i--) {
9983 struct sdom_block *v, *parent, *next;
9984 struct block_set *user;
9985 struct block *block;
9986 block = sd[i].block;
9987 parent = sd[i].parent;
9988 /* Step 2 */
9989 for(user = block->use; user; user = user->next) {
9990 struct sdom_block *v, *u;
9991 v = &sd[user->member->vertex];
9992 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9993 if (u->sdom->vertex < sd[i].sdom->vertex) {
9994 sd[i].sdom = u->sdom;
9995 }
9996 }
9997 sdom_block(sd[i].sdom, &sd[i]);
9998 sd[i].ancestor = parent;
9999 /* Step 3 */
10000 for(v = parent->sdominates; v; v = next) {
10001 struct sdom_block *u;
10002 next = v->sdom_next;
10003 unsdom_block(v);
10004 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
10005 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
10006 u->block : parent->block;
10007 }
10008 }
10009}
10010
10011static void compute_spdom(struct compile_state *state, struct sdom_block *sd)
10012{
10013 int i;
10014 /* // step 2
10015 * for each v <= pred(w) {
10016 * u = EVAL(v);
10017 * if (semi[u] < semi[w] {
10018 * semi[w] = semi[u];
10019 * }
10020 * }
10021 * add w to bucket(vertex(semi[w]));
10022 * LINK(parent(w), w);
10023 *
10024 * // step 3
10025 * for each v <= bucket(parent(w)) {
10026 * delete v from bucket(parent(w));
10027 * u = EVAL(v);
10028 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
10029 * }
10030 */
10031 for(i = state->last_vertex; i >= 2; i--) {
10032 struct sdom_block *u, *v, *parent, *next;
10033 struct block *block;
10034 block = sd[i].block;
10035 parent = sd[i].parent;
10036 /* Step 2 */
10037 if (block->left) {
10038 v = &sd[block->left->vertex];
10039 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
10040 if (u->sdom->vertex < sd[i].sdom->vertex) {
10041 sd[i].sdom = u->sdom;
10042 }
10043 }
10044 if (block->right && (block->right != block->left)) {
10045 v = &sd[block->right->vertex];
10046 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
10047 if (u->sdom->vertex < sd[i].sdom->vertex) {
10048 sd[i].sdom = u->sdom;
10049 }
10050 }
10051 sdom_block(sd[i].sdom, &sd[i]);
10052 sd[i].ancestor = parent;
10053 /* Step 3 */
10054 for(v = parent->sdominates; v; v = next) {
10055 struct sdom_block *u;
10056 next = v->sdom_next;
10057 unsdom_block(v);
10058 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
10059 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
10060 u->block : parent->block;
10061 }
10062 }
10063}
10064
10065static void compute_idom(struct compile_state *state, struct sdom_block *sd)
10066{
10067 int i;
10068 for(i = 2; i <= state->last_vertex; i++) {
10069 struct block *block;
10070 block = sd[i].block;
10071 if (block->idom->vertex != sd[i].sdom->vertex) {
10072 block->idom = block->idom->idom;
10073 }
10074 idom_block(block->idom, block);
10075 }
10076 sd[1].block->idom = 0;
10077}
10078
10079static void compute_ipdom(struct compile_state *state, struct sdom_block *sd)
10080{
10081 int i;
10082 for(i = 2; i <= state->last_vertex; i++) {
10083 struct block *block;
10084 block = sd[i].block;
10085 if (block->ipdom->vertex != sd[i].sdom->vertex) {
10086 block->ipdom = block->ipdom->ipdom;
10087 }
10088 ipdom_block(block->ipdom, block);
10089 }
10090 sd[1].block->ipdom = 0;
10091}
10092
10093 /* Theorem 1:
10094 * Every vertex of a flowgraph G = (V, E, r) except r has
10095 * a unique immediate dominator.
10096 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
10097 * rooted at r, called the dominator tree of G, such that
10098 * v dominates w if and only if v is a proper ancestor of w in
10099 * the dominator tree.
10100 */
10101 /* Lemma 1:
10102 * If v and w are vertices of G such that v <= w,
10103 * than any path from v to w must contain a common ancestor
10104 * of v and w in T.
10105 */
10106 /* Lemma 2: For any vertex w != r, idom(w) -> w */
10107 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
10108 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
10109 /* Theorem 2:
10110 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
10111 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
10112 */
10113 /* Theorem 3:
10114 * Let w != r and let u be a vertex for which sdom(u) is
10115 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
10116 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
10117 */
10118 /* Lemma 5: Let vertices v,w satisfy v -> w.
10119 * Then v -> idom(w) or idom(w) -> idom(v)
10120 */
10121
10122static void find_immediate_dominators(struct compile_state *state)
10123{
10124 struct sdom_block *sd;
10125 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
10126 * vi > w for (1 <= i <= k - 1}
10127 */
10128 /* Theorem 4:
10129 * For any vertex w != r.
10130 * sdom(w) = min(
10131 * {v|(v,w) <= E and v < w } U
10132 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
10133 */
10134 /* Corollary 1:
10135 * Let w != r and let u be a vertex for which sdom(u) is
10136 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
10137 * Then:
10138 * { sdom(w) if sdom(w) = sdom(u),
10139 * idom(w) = {
10140 * { idom(u) otherwise
10141 */
10142 /* The algorithm consists of the following 4 steps.
10143 * Step 1. Carry out a depth-first search of the problem graph.
10144 * Number the vertices from 1 to N as they are reached during
10145 * the search. Initialize the variables used in succeeding steps.
10146 * Step 2. Compute the semidominators of all vertices by applying
10147 * theorem 4. Carry out the computation vertex by vertex in
10148 * decreasing order by number.
10149 * Step 3. Implicitly define the immediate dominator of each vertex
10150 * by applying Corollary 1.
10151 * Step 4. Explicitly define the immediate dominator of each vertex,
10152 * carrying out the computation vertex by vertex in increasing order
10153 * by number.
10154 */
10155 /* Step 1 initialize the basic block information */
10156 sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
10157 initialize_sdblock(sd, 0, state->first_block, 0);
10158#if 0
10159 sd[1].size = 0;
10160 sd[1].label = 0;
10161 sd[1].sdom = 0;
10162#endif
10163 /* Step 2 compute the semidominators */
10164 /* Step 3 implicitly define the immediate dominator of each vertex */
10165 compute_sdom(state, sd);
10166 /* Step 4 explicitly define the immediate dominator of each vertex */
10167 compute_idom(state, sd);
10168 xfree(sd);
10169}
10170
10171static void find_post_dominators(struct compile_state *state)
10172{
10173 struct sdom_block *sd;
Eric Biederman530b5192003-07-01 10:05:30 +000010174 int vertex;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010175 /* Step 1 initialize the basic block information */
10176 sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
10177
Eric Biederman530b5192003-07-01 10:05:30 +000010178 vertex = setup_sdpblocks(state, sd);
10179 if (vertex != state->last_vertex) {
10180 internal_error(state, 0, "missing %d blocks\n",
10181 state->last_vertex - vertex);
10182 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010183
10184 /* Step 2 compute the semidominators */
10185 /* Step 3 implicitly define the immediate dominator of each vertex */
10186 compute_spdom(state, sd);
10187 /* Step 4 explicitly define the immediate dominator of each vertex */
10188 compute_ipdom(state, sd);
10189 xfree(sd);
10190}
10191
10192
10193
10194static void find_block_domf(struct compile_state *state, struct block *block)
10195{
10196 struct block *child;
10197 struct block_set *user;
10198 if (block->domfrontier != 0) {
10199 internal_error(state, block->first, "domfrontier present?");
10200 }
10201 for(user = block->idominates; user; user = user->next) {
10202 child = user->member;
10203 if (child->idom != block) {
10204 internal_error(state, block->first, "bad idom");
10205 }
10206 find_block_domf(state, child);
10207 }
10208 if (block->left && block->left->idom != block) {
10209 domf_block(block, block->left);
10210 }
10211 if (block->right && block->right->idom != block) {
10212 domf_block(block, block->right);
10213 }
10214 for(user = block->idominates; user; user = user->next) {
10215 struct block_set *frontier;
10216 child = user->member;
10217 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
10218 if (frontier->member->idom != block) {
10219 domf_block(block, frontier->member);
10220 }
10221 }
10222 }
10223}
10224
10225static void find_block_ipdomf(struct compile_state *state, struct block *block)
10226{
10227 struct block *child;
10228 struct block_set *user;
10229 if (block->ipdomfrontier != 0) {
10230 internal_error(state, block->first, "ipdomfrontier present?");
10231 }
10232 for(user = block->ipdominates; user; user = user->next) {
10233 child = user->member;
10234 if (child->ipdom != block) {
10235 internal_error(state, block->first, "bad ipdom");
10236 }
10237 find_block_ipdomf(state, child);
10238 }
10239 if (block->left && block->left->ipdom != block) {
10240 ipdomf_block(block, block->left);
10241 }
10242 if (block->right && block->right->ipdom != block) {
10243 ipdomf_block(block, block->right);
10244 }
10245 for(user = block->idominates; user; user = user->next) {
10246 struct block_set *frontier;
10247 child = user->member;
10248 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
10249 if (frontier->member->ipdom != block) {
10250 ipdomf_block(block, frontier->member);
10251 }
10252 }
10253 }
10254}
10255
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010256static void print_dominated(
10257 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010258{
10259 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010260 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010261
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010262 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010263 for(user = block->idominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010264 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010265 if (user->member->idom != block) {
10266 internal_error(state, user->member->first, "bad idom");
10267 }
10268 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010269 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010270}
10271
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010272static void print_dominators(struct compile_state *state, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010273{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010274 fprintf(fp, "\ndominates\n");
10275 walk_blocks(state, print_dominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010276}
10277
10278
10279static int print_frontiers(
10280 struct compile_state *state, struct block *block, int vertex)
10281{
10282 struct block_set *user;
10283
10284 if (!block || (block->vertex != vertex + 1)) {
10285 return vertex;
10286 }
10287 vertex += 1;
10288
10289 printf("%d:", block->vertex);
10290 for(user = block->domfrontier; user; user = user->next) {
10291 printf(" %d", user->member->vertex);
10292 }
10293 printf("\n");
10294
10295 vertex = print_frontiers(state, block->left, vertex);
10296 vertex = print_frontiers(state, block->right, vertex);
10297 return vertex;
10298}
10299static void print_dominance_frontiers(struct compile_state *state)
10300{
10301 printf("\ndominance frontiers\n");
10302 print_frontiers(state, state->first_block, 0);
10303
10304}
10305
10306static void analyze_idominators(struct compile_state *state)
10307{
10308 /* Find the immediate dominators */
10309 find_immediate_dominators(state);
10310 /* Find the dominance frontiers */
10311 find_block_domf(state, state->first_block);
10312 /* If debuging print the print what I have just found */
10313 if (state->debug & DEBUG_FDOMINATORS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010314 print_dominators(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010315 print_dominance_frontiers(state);
10316 print_control_flow(state);
10317 }
10318}
10319
10320
10321
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010322static void print_ipdominated(
10323 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010324{
10325 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010326 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010327
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010328 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010329 for(user = block->ipdominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010330 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010331 if (user->member->ipdom != block) {
10332 internal_error(state, user->member->first, "bad ipdom");
10333 }
10334 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010335 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010336}
10337
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010338static void print_ipdominators(struct compile_state *state, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010339{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010340 fprintf(fp, "\nipdominates\n");
10341 walk_blocks(state, print_ipdominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010342}
10343
10344static int print_pfrontiers(
10345 struct compile_state *state, struct block *block, int vertex)
10346{
10347 struct block_set *user;
10348
10349 if (!block || (block->vertex != vertex + 1)) {
10350 return vertex;
10351 }
10352 vertex += 1;
10353
10354 printf("%d:", block->vertex);
10355 for(user = block->ipdomfrontier; user; user = user->next) {
10356 printf(" %d", user->member->vertex);
10357 }
10358 printf("\n");
10359 for(user = block->use; user; user = user->next) {
10360 vertex = print_pfrontiers(state, user->member, vertex);
10361 }
10362 return vertex;
10363}
10364static void print_ipdominance_frontiers(struct compile_state *state)
10365{
10366 printf("\nipdominance frontiers\n");
10367 print_pfrontiers(state, state->last_block, 0);
10368
10369}
10370
10371static void analyze_ipdominators(struct compile_state *state)
10372{
10373 /* Find the post dominators */
10374 find_post_dominators(state);
10375 /* Find the control dependencies (post dominance frontiers) */
10376 find_block_ipdomf(state, state->last_block);
10377 /* If debuging print the print what I have just found */
10378 if (state->debug & DEBUG_RDOMINATORS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010379 print_ipdominators(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010380 print_ipdominance_frontiers(state);
10381 print_control_flow(state);
10382 }
10383}
10384
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010385static int bdominates(struct compile_state *state,
10386 struct block *dom, struct block *sub)
10387{
10388 while(sub && (sub != dom)) {
10389 sub = sub->idom;
10390 }
10391 return sub == dom;
10392}
10393
10394static int tdominates(struct compile_state *state,
10395 struct triple *dom, struct triple *sub)
10396{
10397 struct block *bdom, *bsub;
10398 int result;
10399 bdom = block_of_triple(state, dom);
10400 bsub = block_of_triple(state, sub);
10401 if (bdom != bsub) {
10402 result = bdominates(state, bdom, bsub);
10403 }
10404 else {
10405 struct triple *ins;
10406 ins = sub;
10407 while((ins != bsub->first) && (ins != dom)) {
10408 ins = ins->prev;
10409 }
10410 result = (ins == dom);
10411 }
10412 return result;
10413}
10414
Eric Biedermanb138ac82003-04-22 18:44:01 +000010415static void insert_phi_operations(struct compile_state *state)
10416{
10417 size_t size;
10418 struct triple *first;
10419 int *has_already, *work;
10420 struct block *work_list, **work_list_tail;
10421 int iter;
Eric Biedermand1ea5392003-06-28 06:49:45 +000010422 struct triple *var, *vnext;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010423
10424 size = sizeof(int) * (state->last_vertex + 1);
10425 has_already = xcmalloc(size, "has_already");
10426 work = xcmalloc(size, "work");
10427 iter = 0;
10428
Eric Biederman0babc1c2003-05-09 02:39:00 +000010429 first = RHS(state->main_function, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000010430 for(var = first->next; var != first ; var = vnext) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010431 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000010432 struct triple_set *user, *unext;
10433 vnext = var->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010434 if ((var->op != OP_ADECL) || !var->use) {
10435 continue;
10436 }
10437 iter += 1;
10438 work_list = 0;
10439 work_list_tail = &work_list;
Eric Biedermand1ea5392003-06-28 06:49:45 +000010440 for(user = var->use; user; user = unext) {
10441 unext = user->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010442 if (user->member->op == OP_READ) {
10443 continue;
10444 }
10445 if (user->member->op != OP_WRITE) {
10446 internal_error(state, user->member,
10447 "bad variable access");
10448 }
10449 block = user->member->u.block;
10450 if (!block) {
10451 warning(state, user->member, "dead code");
Eric Biedermand1ea5392003-06-28 06:49:45 +000010452 release_triple(state, user->member);
10453 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010454 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000010455 if (work[block->vertex] >= iter) {
10456 continue;
10457 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010458 work[block->vertex] = iter;
10459 *work_list_tail = block;
10460 block->work_next = 0;
10461 work_list_tail = &block->work_next;
10462 }
10463 for(block = work_list; block; block = block->work_next) {
10464 struct block_set *df;
10465 for(df = block->domfrontier; df; df = df->next) {
10466 struct triple *phi;
10467 struct block *front;
10468 int in_edges;
10469 front = df->member;
10470
10471 if (has_already[front->vertex] >= iter) {
10472 continue;
10473 }
10474 /* Count how many edges flow into this block */
10475 in_edges = front->users;
10476 /* Insert a phi function for this variable */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010477 get_occurance(front->first->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010478 phi = alloc_triple(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010479 state, OP_PHI, var->type, -1, in_edges,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010480 front->first->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010481 phi->u.block = front;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010482 MISC(phi, 0) = var;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010483 use_triple(var, phi);
10484 /* Insert the phi functions immediately after the label */
10485 insert_triple(state, front->first->next, phi);
10486 if (front->first == front->last) {
10487 front->last = front->first->next;
10488 }
10489 has_already[front->vertex] = iter;
Eric Biederman05f26fc2003-06-11 21:55:00 +000010490
Eric Biedermanb138ac82003-04-22 18:44:01 +000010491 /* If necessary plan to visit the basic block */
10492 if (work[front->vertex] >= iter) {
10493 continue;
10494 }
10495 work[front->vertex] = iter;
10496 *work_list_tail = front;
10497 front->work_next = 0;
10498 work_list_tail = &front->work_next;
10499 }
10500 }
10501 }
10502 xfree(has_already);
10503 xfree(work);
10504}
10505
10506/*
10507 * C(V)
10508 * S(V)
10509 */
10510static void fixup_block_phi_variables(
10511 struct compile_state *state, struct block *parent, struct block *block)
10512{
10513 struct block_set *set;
10514 struct triple *ptr;
10515 int edge;
10516 if (!parent || !block)
10517 return;
10518 /* Find the edge I am coming in on */
10519 edge = 0;
10520 for(set = block->use; set; set = set->next, edge++) {
10521 if (set->member == parent) {
10522 break;
10523 }
10524 }
10525 if (!set) {
10526 internal_error(state, 0, "phi input is not on a control predecessor");
10527 }
10528 for(ptr = block->first; ; ptr = ptr->next) {
10529 if (ptr->op == OP_PHI) {
10530 struct triple *var, *val, **slot;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010531 var = MISC(ptr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010532 if (!var) {
10533 internal_error(state, ptr, "no var???");
10534 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010535 /* Find the current value of the variable */
10536 val = var->use->member;
10537 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10538 internal_error(state, val, "bad value in phi");
10539 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000010540 if (edge >= TRIPLE_RHS(ptr->sizes)) {
10541 internal_error(state, ptr, "edges > phi rhs");
10542 }
10543 slot = &RHS(ptr, edge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010544 if ((*slot != 0) && (*slot != val)) {
10545 internal_error(state, ptr, "phi already bound on this edge");
10546 }
10547 *slot = val;
10548 use_triple(val, ptr);
10549 }
10550 if (ptr == block->last) {
10551 break;
10552 }
10553 }
10554}
10555
10556
10557static void rename_block_variables(
10558 struct compile_state *state, struct block *block)
10559{
10560 struct block_set *user;
10561 struct triple *ptr, *next, *last;
10562 int done;
10563 if (!block)
10564 return;
10565 last = block->first;
10566 done = 0;
10567 for(ptr = block->first; !done; ptr = next) {
10568 next = ptr->next;
10569 if (ptr == block->last) {
10570 done = 1;
10571 }
10572 /* RHS(A) */
10573 if (ptr->op == OP_READ) {
10574 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010575 var = RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010576 unuse_triple(var, ptr);
10577 if (!var->use) {
10578 error(state, ptr, "variable used without being set");
10579 }
10580 /* Find the current value of the variable */
10581 val = var->use->member;
10582 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10583 internal_error(state, val, "bad value in read");
10584 }
10585 propogate_use(state, ptr, val);
10586 release_triple(state, ptr);
10587 continue;
10588 }
10589 /* LHS(A) */
10590 if (ptr->op == OP_WRITE) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000010591 struct triple *var, *val, *tval;
Eric Biederman530b5192003-07-01 10:05:30 +000010592 var = RHS(ptr, 0);
10593 tval = val = RHS(ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010594 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
Eric Biederman678d8162003-07-03 03:59:38 +000010595 internal_error(state, ptr, "bad value in write");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010596 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000010597 /* Insert a copy if the types differ */
10598 if (!equiv_types(ptr->type, val->type)) {
10599 if (val->op == OP_INTCONST) {
10600 tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
10601 tval->u.cval = val->u.cval;
10602 }
10603 else {
10604 tval = pre_triple(state, ptr, OP_COPY, ptr->type, val, 0);
10605 use_triple(val, tval);
10606 }
10607 unuse_triple(val, ptr);
Eric Biederman530b5192003-07-01 10:05:30 +000010608 RHS(ptr, 1) = tval;
Eric Biedermand1ea5392003-06-28 06:49:45 +000010609 use_triple(tval, ptr);
10610 }
10611 propogate_use(state, ptr, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010612 unuse_triple(var, ptr);
10613 /* Push OP_WRITE ptr->right onto a stack of variable uses */
Eric Biedermand1ea5392003-06-28 06:49:45 +000010614 push_triple(var, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010615 }
10616 if (ptr->op == OP_PHI) {
10617 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010618 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010619 /* Push OP_PHI onto a stack of variable uses */
10620 push_triple(var, ptr);
10621 }
10622 last = ptr;
10623 }
10624 block->last = last;
10625
10626 /* Fixup PHI functions in the cf successors */
10627 fixup_block_phi_variables(state, block, block->left);
10628 fixup_block_phi_variables(state, block, block->right);
10629 /* rename variables in the dominated nodes */
10630 for(user = block->idominates; user; user = user->next) {
10631 rename_block_variables(state, user->member);
10632 }
10633 /* pop the renamed variable stack */
10634 last = block->first;
10635 done = 0;
10636 for(ptr = block->first; !done ; ptr = next) {
10637 next = ptr->next;
10638 if (ptr == block->last) {
10639 done = 1;
10640 }
10641 if (ptr->op == OP_WRITE) {
10642 struct triple *var;
Eric Biederman530b5192003-07-01 10:05:30 +000010643 var = RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010644 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman530b5192003-07-01 10:05:30 +000010645 pop_triple(var, RHS(ptr, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010646 release_triple(state, ptr);
10647 continue;
10648 }
10649 if (ptr->op == OP_PHI) {
10650 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010651 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010652 /* Pop OP_WRITE ptr->right from the stack of variable uses */
10653 pop_triple(var, ptr);
10654 }
10655 last = ptr;
10656 }
10657 block->last = last;
10658}
10659
10660static void prune_block_variables(struct compile_state *state,
10661 struct block *block)
10662{
10663 struct block_set *user;
10664 struct triple *next, *last, *ptr;
10665 int done;
10666 last = block->first;
10667 done = 0;
10668 for(ptr = block->first; !done; ptr = next) {
10669 next = ptr->next;
10670 if (ptr == block->last) {
10671 done = 1;
10672 }
10673 if (ptr->op == OP_ADECL) {
10674 struct triple_set *user, *next;
10675 for(user = ptr->use; user; user = next) {
10676 struct triple *use;
10677 next = user->next;
10678 use = user->member;
10679 if (use->op != OP_PHI) {
10680 internal_error(state, use, "decl still used");
10681 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000010682 if (MISC(use, 0) != ptr) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010683 internal_error(state, use, "bad phi use of decl");
10684 }
10685 unuse_triple(ptr, use);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010686 MISC(use, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010687 }
10688 release_triple(state, ptr);
10689 continue;
10690 }
10691 last = ptr;
10692 }
10693 block->last = last;
10694 for(user = block->idominates; user; user = user->next) {
10695 prune_block_variables(state, user->member);
10696 }
10697}
10698
10699static void transform_to_ssa_form(struct compile_state *state)
10700{
10701 insert_phi_operations(state);
10702#if 0
10703 printf("@%s:%d\n", __FILE__, __LINE__);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010704 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010705#endif
10706 rename_block_variables(state, state->first_block);
10707 prune_block_variables(state, state->first_block);
10708}
10709
10710
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010711static void clear_vertex(
10712 struct compile_state *state, struct block *block, void *arg)
10713{
10714 block->vertex = 0;
10715}
10716
10717static void mark_live_block(
10718 struct compile_state *state, struct block *block, int *next_vertex)
10719{
10720 /* See if this is a block that has not been marked */
10721 if (block->vertex != 0) {
10722 return;
10723 }
10724 block->vertex = *next_vertex;
10725 *next_vertex += 1;
10726 if (triple_is_branch(state, block->last)) {
10727 struct triple **targ;
10728 targ = triple_targ(state, block->last, 0);
10729 for(; targ; targ = triple_targ(state, block->last, targ)) {
10730 if (!*targ) {
10731 continue;
10732 }
10733 if (!triple_stores_block(state, *targ)) {
10734 internal_error(state, 0, "bad targ");
10735 }
10736 mark_live_block(state, (*targ)->u.block, next_vertex);
10737 }
10738 }
10739 else if (block->last->next != RHS(state->main_function, 0)) {
10740 struct triple *ins;
10741 ins = block->last->next;
10742 if (!triple_stores_block(state, ins)) {
10743 internal_error(state, 0, "bad block start");
10744 }
10745 mark_live_block(state, ins->u.block, next_vertex);
10746 }
10747}
10748
Eric Biedermanb138ac82003-04-22 18:44:01 +000010749static void transform_from_ssa_form(struct compile_state *state)
10750{
10751 /* To get out of ssa form we insert moves on the incoming
10752 * edges to blocks containting phi functions.
10753 */
10754 struct triple *first;
10755 struct triple *phi, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010756 int next_vertex;
10757
10758 /* Walk the control flow to see which blocks remain alive */
10759 walk_blocks(state, clear_vertex, 0);
10760 next_vertex = 1;
10761 mark_live_block(state, state->first_block, &next_vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010762
10763 /* Walk all of the operations to find the phi functions */
Eric Biederman0babc1c2003-05-09 02:39:00 +000010764 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010765 for(phi = first->next; phi != first ; phi = next) {
10766 struct block_set *set;
10767 struct block *block;
10768 struct triple **slot;
10769 struct triple *var, *read;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010770 struct triple_set *use, *use_next;
10771 int edge, used;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010772 next = phi->next;
10773 if (phi->op != OP_PHI) {
10774 continue;
10775 }
10776 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010777 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010778
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010779 /* Forget uses from code in dead blocks */
10780 for(use = phi->use; use; use = use_next) {
10781 struct block *ublock;
10782 struct triple **expr;
10783 use_next = use->next;
10784 ublock = block_of_triple(state, use->member);
10785 if ((use->member == phi) || (ublock->vertex != 0)) {
10786 continue;
10787 }
10788 expr = triple_rhs(state, use->member, 0);
10789 for(; expr; expr = triple_rhs(state, use->member, expr)) {
10790 if (*expr == phi) {
10791 *expr = 0;
10792 }
10793 }
10794 unuse_triple(phi, use->member);
10795 }
10796
Eric Biederman530b5192003-07-01 10:05:30 +000010797#warning "CHECK_ME does the OP_ADECL need to be placed somewhere that dominates all of the incoming phi edges?"
Eric Biedermanb138ac82003-04-22 18:44:01 +000010798 /* A variable to replace the phi function */
10799 var = post_triple(state, phi, OP_ADECL, phi->type, 0,0);
10800 /* A read of the single value that is set into the variable */
10801 read = post_triple(state, var, OP_READ, phi->type, var, 0);
10802 use_triple(var, read);
10803
10804 /* Replaces uses of the phi with variable reads */
10805 propogate_use(state, phi, read);
10806
10807 /* Walk all of the incoming edges/blocks and insert moves.
10808 */
10809 for(edge = 0, set = block->use; set; set = set->next, edge++) {
10810 struct block *eblock;
10811 struct triple *move;
Eric Biederman530b5192003-07-01 10:05:30 +000010812 struct triple *val, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010813 eblock = set->member;
10814 val = slot[edge];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010815 slot[edge] = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010816 unuse_triple(val, phi);
10817
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010818 if (!val || (val == &zero_triple) ||
10819 (block->vertex == 0) || (eblock->vertex == 0) ||
10820 (val == phi) || (val == read)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010821 continue;
10822 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010823
Eric Biederman530b5192003-07-01 10:05:30 +000010824 /* Make certain the write is placed in the edge block... */
10825 base = eblock->first;
10826 if (block_of_triple(state, val) == eblock) {
10827 base = val;
10828 }
10829 move = post_triple(state, base, OP_WRITE, phi->type, var, val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010830 use_triple(val, move);
10831 use_triple(var, move);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010832 }
10833 /* See if there are any writers of var */
10834 used = 0;
10835 for(use = var->use; use; use = use->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000010836 if ((use->member->op == OP_WRITE) &&
10837 (RHS(use->member, 0) == var)) {
10838 used = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010839 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010840 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010841 /* If var is not used free it */
10842 if (!used) {
10843 unuse_triple(var, read);
10844 free_triple(state, read);
10845 free_triple(state, var);
10846 }
10847
10848 /* Release the phi function */
Eric Biedermanb138ac82003-04-22 18:44:01 +000010849 release_triple(state, phi);
10850 }
10851
10852}
10853
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010854
10855/*
10856 * Register conflict resolution
10857 * =========================================================
10858 */
10859
10860static struct reg_info find_def_color(
10861 struct compile_state *state, struct triple *def)
10862{
10863 struct triple_set *set;
10864 struct reg_info info;
10865 info.reg = REG_UNSET;
10866 info.regcm = 0;
10867 if (!triple_is_def(state, def)) {
10868 return info;
10869 }
10870 info = arch_reg_lhs(state, def, 0);
10871 if (info.reg >= MAX_REGISTERS) {
10872 info.reg = REG_UNSET;
10873 }
10874 for(set = def->use; set; set = set->next) {
10875 struct reg_info tinfo;
10876 int i;
10877 i = find_rhs_use(state, set->member, def);
10878 if (i < 0) {
10879 continue;
10880 }
10881 tinfo = arch_reg_rhs(state, set->member, i);
10882 if (tinfo.reg >= MAX_REGISTERS) {
10883 tinfo.reg = REG_UNSET;
10884 }
10885 if ((tinfo.reg != REG_UNSET) &&
10886 (info.reg != REG_UNSET) &&
10887 (tinfo.reg != info.reg)) {
10888 internal_error(state, def, "register conflict");
10889 }
10890 if ((info.regcm & tinfo.regcm) == 0) {
10891 internal_error(state, def, "regcm conflict %x & %x == 0",
10892 info.regcm, tinfo.regcm);
10893 }
10894 if (info.reg == REG_UNSET) {
10895 info.reg = tinfo.reg;
10896 }
10897 info.regcm &= tinfo.regcm;
10898 }
10899 if (info.reg >= MAX_REGISTERS) {
10900 internal_error(state, def, "register out of range");
10901 }
10902 return info;
10903}
10904
10905static struct reg_info find_lhs_pre_color(
10906 struct compile_state *state, struct triple *ins, int index)
10907{
10908 struct reg_info info;
10909 int zlhs, zrhs, i;
10910 zrhs = TRIPLE_RHS(ins->sizes);
10911 zlhs = TRIPLE_LHS(ins->sizes);
10912 if (!zlhs && triple_is_def(state, ins)) {
10913 zlhs = 1;
10914 }
10915 if (index >= zlhs) {
10916 internal_error(state, ins, "Bad lhs %d", index);
10917 }
10918 info = arch_reg_lhs(state, ins, index);
10919 for(i = 0; i < zrhs; i++) {
10920 struct reg_info rinfo;
10921 rinfo = arch_reg_rhs(state, ins, i);
10922 if ((info.reg == rinfo.reg) &&
10923 (rinfo.reg >= MAX_REGISTERS)) {
10924 struct reg_info tinfo;
10925 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
10926 info.reg = tinfo.reg;
10927 info.regcm &= tinfo.regcm;
10928 break;
10929 }
10930 }
10931 if (info.reg >= MAX_REGISTERS) {
10932 info.reg = REG_UNSET;
10933 }
10934 return info;
10935}
10936
10937static struct reg_info find_rhs_post_color(
10938 struct compile_state *state, struct triple *ins, int index);
10939
10940static struct reg_info find_lhs_post_color(
10941 struct compile_state *state, struct triple *ins, int index)
10942{
10943 struct triple_set *set;
10944 struct reg_info info;
10945 struct triple *lhs;
Eric Biederman530b5192003-07-01 10:05:30 +000010946#if DEBUG_TRIPLE_COLOR
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010947 fprintf(stderr, "find_lhs_post_color(%p, %d)\n",
10948 ins, index);
10949#endif
10950 if ((index == 0) && triple_is_def(state, ins)) {
10951 lhs = ins;
10952 }
10953 else if (index < TRIPLE_LHS(ins->sizes)) {
10954 lhs = LHS(ins, index);
10955 }
10956 else {
10957 internal_error(state, ins, "Bad lhs %d", index);
10958 lhs = 0;
10959 }
10960 info = arch_reg_lhs(state, ins, index);
10961 if (info.reg >= MAX_REGISTERS) {
10962 info.reg = REG_UNSET;
10963 }
10964 for(set = lhs->use; set; set = set->next) {
10965 struct reg_info rinfo;
10966 struct triple *user;
10967 int zrhs, i;
10968 user = set->member;
10969 zrhs = TRIPLE_RHS(user->sizes);
10970 for(i = 0; i < zrhs; i++) {
10971 if (RHS(user, i) != lhs) {
10972 continue;
10973 }
10974 rinfo = find_rhs_post_color(state, user, i);
10975 if ((info.reg != REG_UNSET) &&
10976 (rinfo.reg != REG_UNSET) &&
10977 (info.reg != rinfo.reg)) {
10978 internal_error(state, ins, "register conflict");
10979 }
10980 if ((info.regcm & rinfo.regcm) == 0) {
10981 internal_error(state, ins, "regcm conflict %x & %x == 0",
10982 info.regcm, rinfo.regcm);
10983 }
10984 if (info.reg == REG_UNSET) {
10985 info.reg = rinfo.reg;
10986 }
10987 info.regcm &= rinfo.regcm;
10988 }
10989 }
Eric Biederman530b5192003-07-01 10:05:30 +000010990#if DEBUG_TRIPLE_COLOR
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010991 fprintf(stderr, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
10992 ins, index, info.reg, info.regcm);
10993#endif
10994 return info;
10995}
10996
10997static struct reg_info find_rhs_post_color(
10998 struct compile_state *state, struct triple *ins, int index)
10999{
11000 struct reg_info info, rinfo;
11001 int zlhs, i;
Eric Biederman530b5192003-07-01 10:05:30 +000011002#if DEBUG_TRIPLE_COLOR
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011003 fprintf(stderr, "find_rhs_post_color(%p, %d)\n",
11004 ins, index);
11005#endif
11006 rinfo = arch_reg_rhs(state, ins, index);
11007 zlhs = TRIPLE_LHS(ins->sizes);
11008 if (!zlhs && triple_is_def(state, ins)) {
11009 zlhs = 1;
11010 }
11011 info = rinfo;
11012 if (info.reg >= MAX_REGISTERS) {
11013 info.reg = REG_UNSET;
11014 }
11015 for(i = 0; i < zlhs; i++) {
11016 struct reg_info linfo;
11017 linfo = arch_reg_lhs(state, ins, i);
11018 if ((linfo.reg == rinfo.reg) &&
11019 (linfo.reg >= MAX_REGISTERS)) {
11020 struct reg_info tinfo;
11021 tinfo = find_lhs_post_color(state, ins, i);
11022 if (tinfo.reg >= MAX_REGISTERS) {
11023 tinfo.reg = REG_UNSET;
11024 }
Eric Biederman530b5192003-07-01 10:05:30 +000011025 info.regcm &= linfo.regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011026 info.regcm &= tinfo.regcm;
11027 if (info.reg != REG_UNSET) {
11028 internal_error(state, ins, "register conflict");
11029 }
11030 if (info.regcm == 0) {
11031 internal_error(state, ins, "regcm conflict");
11032 }
11033 info.reg = tinfo.reg;
11034 }
11035 }
Eric Biederman530b5192003-07-01 10:05:30 +000011036#if DEBUG_TRIPLE_COLOR
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011037 fprintf(stderr, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
11038 ins, index, info.reg, info.regcm);
11039#endif
11040 return info;
11041}
11042
11043static struct reg_info find_lhs_color(
11044 struct compile_state *state, struct triple *ins, int index)
11045{
11046 struct reg_info pre, post, info;
Eric Biederman530b5192003-07-01 10:05:30 +000011047#if DEBUG_TRIPLE_COLOR
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011048 fprintf(stderr, "find_lhs_color(%p, %d)\n",
11049 ins, index);
11050#endif
11051 pre = find_lhs_pre_color(state, ins, index);
11052 post = find_lhs_post_color(state, ins, index);
11053 if ((pre.reg != post.reg) &&
11054 (pre.reg != REG_UNSET) &&
11055 (post.reg != REG_UNSET)) {
11056 internal_error(state, ins, "register conflict");
11057 }
11058 info.regcm = pre.regcm & post.regcm;
11059 info.reg = pre.reg;
11060 if (info.reg == REG_UNSET) {
11061 info.reg = post.reg;
11062 }
Eric Biederman530b5192003-07-01 10:05:30 +000011063#if DEBUG_TRIPLE_COLOR
11064 fprintf(stderr, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
11065 ins, index, info.reg, info.regcm,
11066 pre.reg, pre.regcm, post.reg, post.regcm);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011067#endif
11068 return info;
11069}
11070
11071static struct triple *post_copy(struct compile_state *state, struct triple *ins)
11072{
11073 struct triple_set *entry, *next;
11074 struct triple *out;
11075 struct reg_info info, rinfo;
11076
11077 info = arch_reg_lhs(state, ins, 0);
11078 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
11079 use_triple(RHS(out, 0), out);
11080 /* Get the users of ins to use out instead */
11081 for(entry = ins->use; entry; entry = next) {
11082 int i;
11083 next = entry->next;
11084 if (entry->member == out) {
11085 continue;
11086 }
11087 i = find_rhs_use(state, entry->member, ins);
11088 if (i < 0) {
11089 continue;
11090 }
11091 rinfo = arch_reg_rhs(state, entry->member, i);
11092 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
11093 continue;
11094 }
11095 replace_rhs_use(state, ins, out, entry->member);
11096 }
11097 transform_to_arch_instruction(state, out);
11098 return out;
11099}
11100
Eric Biedermand1ea5392003-06-28 06:49:45 +000011101static struct triple *typed_pre_copy(
11102 struct compile_state *state, struct type *type, struct triple *ins, int index)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011103{
11104 /* Carefully insert enough operations so that I can
11105 * enter any operation with a GPR32.
11106 */
11107 struct triple *in;
11108 struct triple **expr;
Eric Biedermand1ea5392003-06-28 06:49:45 +000011109 unsigned classes;
11110 struct reg_info info;
Eric Biederman153ea352003-06-20 14:43:20 +000011111 if (ins->op == OP_PHI) {
11112 internal_error(state, ins, "pre_copy on a phi?");
11113 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000011114 classes = arch_type_to_regcm(state, type);
11115 info = arch_reg_rhs(state, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011116 expr = &RHS(ins, index);
Eric Biedermand1ea5392003-06-28 06:49:45 +000011117 if ((info.regcm & classes) == 0) {
11118 internal_error(state, ins, "pre_copy with no register classes");
11119 }
11120 in = pre_triple(state, ins, OP_COPY, type, *expr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011121 unuse_triple(*expr, ins);
11122 *expr = in;
11123 use_triple(RHS(in, 0), in);
11124 use_triple(in, ins);
11125 transform_to_arch_instruction(state, in);
11126 return in;
Eric Biedermand1ea5392003-06-28 06:49:45 +000011127
11128}
11129static struct triple *pre_copy(
11130 struct compile_state *state, struct triple *ins, int index)
11131{
11132 return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011133}
11134
11135
Eric Biedermanb138ac82003-04-22 18:44:01 +000011136static void insert_copies_to_phi(struct compile_state *state)
11137{
11138 /* To get out of ssa form we insert moves on the incoming
11139 * edges to blocks containting phi functions.
11140 */
11141 struct triple *first;
11142 struct triple *phi;
11143
11144 /* Walk all of the operations to find the phi functions */
Eric Biederman0babc1c2003-05-09 02:39:00 +000011145 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011146 for(phi = first->next; phi != first ; phi = phi->next) {
11147 struct block_set *set;
11148 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000011149 struct triple **slot, *copy;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011150 int edge;
11151 if (phi->op != OP_PHI) {
11152 continue;
11153 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011154 phi->id |= TRIPLE_FLAG_POST_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011155 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011156 slot = &RHS(phi, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000011157 /* Phi's that feed into mandatory live range joins
11158 * cause nasty complications. Insert a copy of
11159 * the phi value so I never have to deal with
11160 * that in the rest of the code.
11161 */
11162 copy = post_copy(state, phi);
11163 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011164 /* Walk all of the incoming edges/blocks and insert moves.
11165 */
11166 for(edge = 0, set = block->use; set; set = set->next, edge++) {
11167 struct block *eblock;
11168 struct triple *move;
11169 struct triple *val;
11170 struct triple *ptr;
11171 eblock = set->member;
11172 val = slot[edge];
11173
11174 if (val == phi) {
11175 continue;
11176 }
11177
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011178 get_occurance(val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011179 move = build_triple(state, OP_COPY, phi->type, val, 0,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011180 val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011181 move->u.block = eblock;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011182 move->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011183 use_triple(val, move);
11184
11185 slot[edge] = move;
11186 unuse_triple(val, phi);
11187 use_triple(move, phi);
11188
11189 /* Walk through the block backwards to find
11190 * an appropriate location for the OP_COPY.
11191 */
11192 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
11193 struct triple **expr;
11194 if ((ptr == phi) || (ptr == val)) {
11195 goto out;
11196 }
11197 expr = triple_rhs(state, ptr, 0);
11198 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11199 if ((*expr) == phi) {
11200 goto out;
11201 }
11202 }
11203 }
11204 out:
Eric Biederman0babc1c2003-05-09 02:39:00 +000011205 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011206 internal_error(state, ptr,
11207 "Could not insert write to phi");
11208 }
11209 insert_triple(state, ptr->next, move);
11210 if (eblock->last == ptr) {
11211 eblock->last = move;
11212 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011213 transform_to_arch_instruction(state, move);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011214 }
11215 }
11216}
11217
11218struct triple_reg_set {
11219 struct triple_reg_set *next;
11220 struct triple *member;
11221 struct triple *new;
11222};
11223
11224struct reg_block {
11225 struct block *block;
11226 struct triple_reg_set *in;
11227 struct triple_reg_set *out;
11228 int vertex;
11229};
11230
11231static int do_triple_set(struct triple_reg_set **head,
11232 struct triple *member, struct triple *new_member)
11233{
11234 struct triple_reg_set **ptr, *new;
11235 if (!member)
11236 return 0;
11237 ptr = head;
11238 while(*ptr) {
11239 if ((*ptr)->member == member) {
11240 return 0;
11241 }
11242 ptr = &(*ptr)->next;
11243 }
11244 new = xcmalloc(sizeof(*new), "triple_set");
11245 new->member = member;
11246 new->new = new_member;
11247 new->next = *head;
11248 *head = new;
11249 return 1;
11250}
11251
11252static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
11253{
11254 struct triple_reg_set *entry, **ptr;
11255 ptr = head;
11256 while(*ptr) {
11257 entry = *ptr;
11258 if (entry->member == member) {
11259 *ptr = entry->next;
11260 xfree(entry);
11261 return;
11262 }
11263 else {
11264 ptr = &entry->next;
11265 }
11266 }
11267}
11268
11269static int in_triple(struct reg_block *rb, struct triple *in)
11270{
11271 return do_triple_set(&rb->in, in, 0);
11272}
11273static void unin_triple(struct reg_block *rb, struct triple *unin)
11274{
11275 do_triple_unset(&rb->in, unin);
11276}
11277
11278static int out_triple(struct reg_block *rb, struct triple *out)
11279{
11280 return do_triple_set(&rb->out, out, 0);
11281}
11282static void unout_triple(struct reg_block *rb, struct triple *unout)
11283{
11284 do_triple_unset(&rb->out, unout);
11285}
11286
11287static int initialize_regblock(struct reg_block *blocks,
11288 struct block *block, int vertex)
11289{
11290 struct block_set *user;
11291 if (!block || (blocks[block->vertex].block == block)) {
11292 return vertex;
11293 }
11294 vertex += 1;
11295 /* Renumber the blocks in a convinient fashion */
11296 block->vertex = vertex;
11297 blocks[vertex].block = block;
11298 blocks[vertex].vertex = vertex;
11299 for(user = block->use; user; user = user->next) {
11300 vertex = initialize_regblock(blocks, user->member, vertex);
11301 }
11302 return vertex;
11303}
11304
11305static int phi_in(struct compile_state *state, struct reg_block *blocks,
11306 struct reg_block *rb, struct block *suc)
11307{
11308 /* Read the conditional input set of a successor block
11309 * (i.e. the input to the phi nodes) and place it in the
11310 * current blocks output set.
11311 */
11312 struct block_set *set;
11313 struct triple *ptr;
11314 int edge;
11315 int done, change;
11316 change = 0;
11317 /* Find the edge I am coming in on */
11318 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
11319 if (set->member == rb->block) {
11320 break;
11321 }
11322 }
11323 if (!set) {
11324 internal_error(state, 0, "Not coming on a control edge?");
11325 }
11326 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
11327 struct triple **slot, *expr, *ptr2;
11328 int out_change, done2;
11329 done = (ptr == suc->last);
11330 if (ptr->op != OP_PHI) {
11331 continue;
11332 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000011333 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011334 expr = slot[edge];
11335 out_change = out_triple(rb, expr);
11336 if (!out_change) {
11337 continue;
11338 }
11339 /* If we don't define the variable also plast it
11340 * in the current blocks input set.
11341 */
11342 ptr2 = rb->block->first;
11343 for(done2 = 0; !done2; ptr2 = ptr2->next) {
11344 if (ptr2 == expr) {
11345 break;
11346 }
11347 done2 = (ptr2 == rb->block->last);
11348 }
11349 if (!done2) {
11350 continue;
11351 }
11352 change |= in_triple(rb, expr);
11353 }
11354 return change;
11355}
11356
11357static int reg_in(struct compile_state *state, struct reg_block *blocks,
11358 struct reg_block *rb, struct block *suc)
11359{
11360 struct triple_reg_set *in_set;
11361 int change;
11362 change = 0;
11363 /* Read the input set of a successor block
11364 * and place it in the current blocks output set.
11365 */
11366 in_set = blocks[suc->vertex].in;
11367 for(; in_set; in_set = in_set->next) {
11368 int out_change, done;
11369 struct triple *first, *last, *ptr;
11370 out_change = out_triple(rb, in_set->member);
11371 if (!out_change) {
11372 continue;
11373 }
11374 /* If we don't define the variable also place it
11375 * in the current blocks input set.
11376 */
11377 first = rb->block->first;
11378 last = rb->block->last;
11379 done = 0;
11380 for(ptr = first; !done; ptr = ptr->next) {
11381 if (ptr == in_set->member) {
11382 break;
11383 }
11384 done = (ptr == last);
11385 }
11386 if (!done) {
11387 continue;
11388 }
11389 change |= in_triple(rb, in_set->member);
11390 }
11391 change |= phi_in(state, blocks, rb, suc);
11392 return change;
11393}
11394
11395
11396static int use_in(struct compile_state *state, struct reg_block *rb)
11397{
11398 /* Find the variables we use but don't define and add
11399 * it to the current blocks input set.
11400 */
11401#warning "FIXME is this O(N^2) algorithm bad?"
11402 struct block *block;
11403 struct triple *ptr;
11404 int done;
11405 int change;
11406 block = rb->block;
11407 change = 0;
11408 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
11409 struct triple **expr;
11410 done = (ptr == block->first);
11411 /* The variable a phi function uses depends on the
11412 * control flow, and is handled in phi_in, not
11413 * here.
11414 */
11415 if (ptr->op == OP_PHI) {
11416 continue;
11417 }
11418 expr = triple_rhs(state, ptr, 0);
11419 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11420 struct triple *rhs, *test;
11421 int tdone;
11422 rhs = *expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011423 if (!rhs) {
11424 continue;
11425 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011426 /* See if rhs is defined in this block */
11427 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
11428 tdone = (test == block->first);
11429 if (test == rhs) {
11430 rhs = 0;
11431 break;
11432 }
11433 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011434 /* If I still have a valid rhs add it to in */
11435 change |= in_triple(rb, rhs);
11436 }
11437 }
11438 return change;
11439}
11440
11441static struct reg_block *compute_variable_lifetimes(
11442 struct compile_state *state)
11443{
11444 struct reg_block *blocks;
11445 int change;
11446 blocks = xcmalloc(
11447 sizeof(*blocks)*(state->last_vertex + 1), "reg_block");
11448 initialize_regblock(blocks, state->last_block, 0);
11449 do {
11450 int i;
11451 change = 0;
11452 for(i = 1; i <= state->last_vertex; i++) {
11453 struct reg_block *rb;
11454 rb = &blocks[i];
11455 /* Add the left successor's input set to in */
11456 if (rb->block->left) {
11457 change |= reg_in(state, blocks, rb, rb->block->left);
11458 }
11459 /* Add the right successor's input set to in */
11460 if ((rb->block->right) &&
11461 (rb->block->right != rb->block->left)) {
11462 change |= reg_in(state, blocks, rb, rb->block->right);
11463 }
11464 /* Add use to in... */
11465 change |= use_in(state, rb);
11466 }
11467 } while(change);
11468 return blocks;
11469}
11470
11471static void free_variable_lifetimes(
11472 struct compile_state *state, struct reg_block *blocks)
11473{
11474 int i;
11475 /* free in_set && out_set on each block */
11476 for(i = 1; i <= state->last_vertex; i++) {
11477 struct triple_reg_set *entry, *next;
11478 struct reg_block *rb;
11479 rb = &blocks[i];
11480 for(entry = rb->in; entry ; entry = next) {
11481 next = entry->next;
11482 do_triple_unset(&rb->in, entry->member);
11483 }
11484 for(entry = rb->out; entry; entry = next) {
11485 next = entry->next;
11486 do_triple_unset(&rb->out, entry->member);
11487 }
11488 }
11489 xfree(blocks);
11490
11491}
11492
Eric Biedermanf96a8102003-06-16 16:57:34 +000011493typedef void (*wvl_cb_t)(
Eric Biedermanb138ac82003-04-22 18:44:01 +000011494 struct compile_state *state,
11495 struct reg_block *blocks, struct triple_reg_set *live,
11496 struct reg_block *rb, struct triple *ins, void *arg);
11497
11498static void walk_variable_lifetimes(struct compile_state *state,
11499 struct reg_block *blocks, wvl_cb_t cb, void *arg)
11500{
11501 int i;
11502
11503 for(i = 1; i <= state->last_vertex; i++) {
11504 struct triple_reg_set *live;
11505 struct triple_reg_set *entry, *next;
11506 struct triple *ptr, *prev;
11507 struct reg_block *rb;
11508 struct block *block;
11509 int done;
11510
11511 /* Get the blocks */
11512 rb = &blocks[i];
11513 block = rb->block;
11514
11515 /* Copy out into live */
11516 live = 0;
11517 for(entry = rb->out; entry; entry = next) {
11518 next = entry->next;
11519 do_triple_set(&live, entry->member, entry->new);
11520 }
11521 /* Walk through the basic block calculating live */
11522 for(done = 0, ptr = block->last; !done; ptr = prev) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000011523 struct triple **expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011524
11525 prev = ptr->prev;
11526 done = (ptr == block->first);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011527
11528 /* Ensure the current definition is in live */
11529 if (triple_is_def(state, ptr)) {
11530 do_triple_set(&live, ptr, 0);
11531 }
11532
11533 /* Inform the callback function of what is
11534 * going on.
11535 */
Eric Biedermanf96a8102003-06-16 16:57:34 +000011536 cb(state, blocks, live, rb, ptr, arg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011537
11538 /* Remove the current definition from live */
11539 do_triple_unset(&live, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011540
Eric Biedermanb138ac82003-04-22 18:44:01 +000011541 /* Add the current uses to live.
11542 *
11543 * It is safe to skip phi functions because they do
11544 * not have any block local uses, and the block
11545 * output sets already properly account for what
11546 * control flow depedent uses phi functions do have.
11547 */
11548 if (ptr->op == OP_PHI) {
11549 continue;
11550 }
11551 expr = triple_rhs(state, ptr, 0);
11552 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11553 /* If the triple is not a definition skip it. */
Eric Biederman0babc1c2003-05-09 02:39:00 +000011554 if (!*expr || !triple_is_def(state, *expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011555 continue;
11556 }
11557 do_triple_set(&live, *expr, 0);
11558 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011559 }
11560 /* Free live */
11561 for(entry = live; entry; entry = next) {
11562 next = entry->next;
11563 do_triple_unset(&live, entry->member);
11564 }
11565 }
11566}
11567
11568static int count_triples(struct compile_state *state)
11569{
11570 struct triple *first, *ins;
11571 int triples = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011572 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011573 ins = first;
11574 do {
11575 triples++;
11576 ins = ins->next;
11577 } while (ins != first);
11578 return triples;
11579}
11580struct dead_triple {
11581 struct triple *triple;
11582 struct dead_triple *work_next;
11583 struct block *block;
11584 int color;
11585 int flags;
11586#define TRIPLE_FLAG_ALIVE 1
11587};
11588
11589
11590static void awaken(
11591 struct compile_state *state,
11592 struct dead_triple *dtriple, struct triple **expr,
11593 struct dead_triple ***work_list_tail)
11594{
11595 struct triple *triple;
11596 struct dead_triple *dt;
11597 if (!expr) {
11598 return;
11599 }
11600 triple = *expr;
11601 if (!triple) {
11602 return;
11603 }
11604 if (triple->id <= 0) {
11605 internal_error(state, triple, "bad triple id: %d",
11606 triple->id);
11607 }
11608 if (triple->op == OP_NOOP) {
11609 internal_warning(state, triple, "awakening noop?");
11610 return;
11611 }
11612 dt = &dtriple[triple->id];
11613 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11614 dt->flags |= TRIPLE_FLAG_ALIVE;
11615 if (!dt->work_next) {
11616 **work_list_tail = dt;
11617 *work_list_tail = &dt->work_next;
11618 }
11619 }
11620}
11621
11622static void eliminate_inefectual_code(struct compile_state *state)
11623{
11624 struct block *block;
11625 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
11626 int triples, i;
11627 struct triple *first, *ins;
11628
11629 /* Setup the work list */
11630 work_list = 0;
11631 work_list_tail = &work_list;
11632
Eric Biederman0babc1c2003-05-09 02:39:00 +000011633 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011634
11635 /* Count how many triples I have */
11636 triples = count_triples(state);
11637
11638 /* Now put then in an array and mark all of the triples dead */
11639 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
11640
11641 ins = first;
11642 i = 1;
11643 block = 0;
11644 do {
11645 if (ins->op == OP_LABEL) {
11646 block = ins->u.block;
11647 }
11648 dtriple[i].triple = ins;
11649 dtriple[i].block = block;
11650 dtriple[i].flags = 0;
11651 dtriple[i].color = ins->id;
11652 ins->id = i;
11653 /* See if it is an operation we always keep */
11654#warning "FIXME handle the case of killing a branch instruction"
Eric Biederman0babc1c2003-05-09 02:39:00 +000011655 if (!triple_is_pure(state, ins) || triple_is_branch(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011656 awaken(state, dtriple, &ins, &work_list_tail);
11657 }
Eric Biederman530b5192003-07-01 10:05:30 +000011658#if 1
11659 /* Unconditionally keep the very last instruction */
11660 else if (ins->next == first) {
11661 awaken(state, dtriple, &ins, &work_list_tail);
11662 }
11663#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011664 i++;
11665 ins = ins->next;
11666 } while(ins != first);
11667 while(work_list) {
11668 struct dead_triple *dt;
11669 struct block_set *user;
11670 struct triple **expr;
11671 dt = work_list;
11672 work_list = dt->work_next;
11673 if (!work_list) {
11674 work_list_tail = &work_list;
11675 }
11676 /* Wake up the data depencencies of this triple */
11677 expr = 0;
11678 do {
11679 expr = triple_rhs(state, dt->triple, expr);
11680 awaken(state, dtriple, expr, &work_list_tail);
11681 } while(expr);
11682 do {
11683 expr = triple_lhs(state, dt->triple, expr);
11684 awaken(state, dtriple, expr, &work_list_tail);
11685 } while(expr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011686 do {
11687 expr = triple_misc(state, dt->triple, expr);
11688 awaken(state, dtriple, expr, &work_list_tail);
11689 } while(expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011690 /* Wake up the forward control dependencies */
11691 do {
11692 expr = triple_targ(state, dt->triple, expr);
11693 awaken(state, dtriple, expr, &work_list_tail);
11694 } while(expr);
11695 /* Wake up the reverse control dependencies of this triple */
11696 for(user = dt->block->ipdomfrontier; user; user = user->next) {
11697 awaken(state, dtriple, &user->member->last, &work_list_tail);
11698 }
11699 }
11700 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
11701 if ((dt->triple->op == OP_NOOP) &&
11702 (dt->flags & TRIPLE_FLAG_ALIVE)) {
11703 internal_error(state, dt->triple, "noop effective?");
11704 }
11705 dt->triple->id = dt->color; /* Restore the color */
11706 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11707#warning "FIXME handle the case of killing a basic block"
11708 if (dt->block->first == dt->triple) {
11709 continue;
11710 }
11711 if (dt->block->last == dt->triple) {
11712 dt->block->last = dt->triple->prev;
11713 }
11714 release_triple(state, dt->triple);
11715 }
11716 }
11717 xfree(dtriple);
11718}
11719
11720
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011721static void insert_mandatory_copies(struct compile_state *state)
11722{
11723 struct triple *ins, *first;
11724
11725 /* The object is with a minimum of inserted copies,
11726 * to resolve in fundamental register conflicts between
11727 * register value producers and consumers.
11728 * Theoretically we may be greater than minimal when we
11729 * are inserting copies before instructions but that
11730 * case should be rare.
11731 */
11732 first = RHS(state->main_function, 0);
11733 ins = first;
11734 do {
11735 struct triple_set *entry, *next;
11736 struct triple *tmp;
11737 struct reg_info info;
11738 unsigned reg, regcm;
11739 int do_post_copy, do_pre_copy;
11740 tmp = 0;
11741 if (!triple_is_def(state, ins)) {
11742 goto next;
11743 }
11744 /* Find the architecture specific color information */
11745 info = arch_reg_lhs(state, ins, 0);
11746 if (info.reg >= MAX_REGISTERS) {
11747 info.reg = REG_UNSET;
11748 }
11749
11750 reg = REG_UNSET;
11751 regcm = arch_type_to_regcm(state, ins->type);
11752 do_post_copy = do_pre_copy = 0;
11753
11754 /* Walk through the uses of ins and check for conflicts */
11755 for(entry = ins->use; entry; entry = next) {
11756 struct reg_info rinfo;
11757 int i;
11758 next = entry->next;
11759 i = find_rhs_use(state, entry->member, ins);
11760 if (i < 0) {
11761 continue;
11762 }
11763
11764 /* Find the users color requirements */
11765 rinfo = arch_reg_rhs(state, entry->member, i);
11766 if (rinfo.reg >= MAX_REGISTERS) {
11767 rinfo.reg = REG_UNSET;
11768 }
11769
11770 /* See if I need a pre_copy */
11771 if (rinfo.reg != REG_UNSET) {
11772 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
11773 do_pre_copy = 1;
11774 }
11775 reg = rinfo.reg;
11776 }
11777 regcm &= rinfo.regcm;
11778 regcm = arch_regcm_normalize(state, regcm);
11779 if (regcm == 0) {
11780 do_pre_copy = 1;
11781 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000011782 /* Always use pre_copies for constants.
11783 * They do not take up any registers until a
11784 * copy places them in one.
11785 */
11786 if ((info.reg == REG_UNNEEDED) &&
11787 (rinfo.reg != REG_UNNEEDED)) {
11788 do_pre_copy = 1;
11789 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011790 }
11791 do_post_copy =
11792 !do_pre_copy &&
11793 (((info.reg != REG_UNSET) &&
11794 (reg != REG_UNSET) &&
11795 (info.reg != reg)) ||
11796 ((info.regcm & regcm) == 0));
11797
11798 reg = info.reg;
11799 regcm = info.regcm;
Eric Biedermand1ea5392003-06-28 06:49:45 +000011800 /* Walk through the uses of ins and do a pre_copy or see if a post_copy is warranted */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011801 for(entry = ins->use; entry; entry = next) {
11802 struct reg_info rinfo;
11803 int i;
11804 next = entry->next;
11805 i = find_rhs_use(state, entry->member, ins);
11806 if (i < 0) {
11807 continue;
11808 }
11809
11810 /* Find the users color requirements */
11811 rinfo = arch_reg_rhs(state, entry->member, i);
11812 if (rinfo.reg >= MAX_REGISTERS) {
11813 rinfo.reg = REG_UNSET;
11814 }
11815
11816 /* Now see if it is time to do the pre_copy */
11817 if (rinfo.reg != REG_UNSET) {
11818 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
11819 ((regcm & rinfo.regcm) == 0) ||
11820 /* Don't let a mandatory coalesce sneak
11821 * into a operation that is marked to prevent
11822 * coalescing.
11823 */
11824 ((reg != REG_UNNEEDED) &&
11825 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
11826 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
11827 ) {
11828 if (do_pre_copy) {
11829 struct triple *user;
11830 user = entry->member;
11831 if (RHS(user, i) != ins) {
11832 internal_error(state, user, "bad rhs");
11833 }
11834 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011835 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011836 continue;
11837 } else {
11838 do_post_copy = 1;
11839 }
11840 }
11841 reg = rinfo.reg;
11842 }
11843 if ((regcm & rinfo.regcm) == 0) {
11844 if (do_pre_copy) {
11845 struct triple *user;
11846 user = entry->member;
11847 if (RHS(user, i) != ins) {
11848 internal_error(state, user, "bad rhs");
11849 }
11850 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011851 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011852 continue;
11853 } else {
11854 do_post_copy = 1;
11855 }
11856 }
11857 regcm &= rinfo.regcm;
11858
11859 }
11860 if (do_post_copy) {
11861 struct reg_info pre, post;
11862 tmp = post_copy(state, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011863 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011864 pre = arch_reg_lhs(state, ins, 0);
11865 post = arch_reg_lhs(state, tmp, 0);
11866 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
11867 internal_error(state, tmp, "useless copy");
11868 }
11869 }
11870 next:
11871 ins = ins->next;
11872 } while(ins != first);
11873}
11874
11875
Eric Biedermanb138ac82003-04-22 18:44:01 +000011876struct live_range_edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011877struct live_range_def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011878struct live_range {
11879 struct live_range_edge *edges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011880 struct live_range_def *defs;
11881/* Note. The list pointed to by defs is kept in order.
11882 * That is baring splits in the flow control
11883 * defs dominates defs->next wich dominates defs->next->next
11884 * etc.
11885 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011886 unsigned color;
11887 unsigned classes;
11888 unsigned degree;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011889 unsigned length;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011890 struct live_range *group_next, **group_prev;
11891};
11892
11893struct live_range_edge {
11894 struct live_range_edge *next;
11895 struct live_range *node;
11896};
11897
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011898struct live_range_def {
11899 struct live_range_def *next;
11900 struct live_range_def *prev;
11901 struct live_range *lr;
11902 struct triple *def;
11903 unsigned orig_id;
11904};
11905
Eric Biedermanb138ac82003-04-22 18:44:01 +000011906#define LRE_HASH_SIZE 2048
11907struct lre_hash {
11908 struct lre_hash *next;
11909 struct live_range *left;
11910 struct live_range *right;
11911};
11912
11913
11914struct reg_state {
11915 struct lre_hash *hash[LRE_HASH_SIZE];
11916 struct reg_block *blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011917 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011918 struct live_range *lr;
11919 struct live_range *low, **low_tail;
11920 struct live_range *high, **high_tail;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011921 unsigned defs;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011922 unsigned ranges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011923 int passes, max_passes;
11924#define MAX_ALLOCATION_PASSES 100
Eric Biedermanb138ac82003-04-22 18:44:01 +000011925};
11926
11927
Eric Biedermand1ea5392003-06-28 06:49:45 +000011928
11929struct print_interference_block_info {
11930 struct reg_state *rstate;
11931 FILE *fp;
11932 int need_edges;
11933};
11934static void print_interference_block(
11935 struct compile_state *state, struct block *block, void *arg)
11936
11937{
11938 struct print_interference_block_info *info = arg;
11939 struct reg_state *rstate = info->rstate;
11940 FILE *fp = info->fp;
11941 struct reg_block *rb;
11942 struct triple *ptr;
11943 int phi_present;
11944 int done;
11945 rb = &rstate->blocks[block->vertex];
11946
11947 fprintf(fp, "\nblock: %p (%d), %p<-%p %p<-%p\n",
11948 block,
11949 block->vertex,
11950 block->left,
11951 block->left && block->left->use?block->left->use->member : 0,
11952 block->right,
11953 block->right && block->right->use?block->right->use->member : 0);
11954 if (rb->in) {
11955 struct triple_reg_set *in_set;
11956 fprintf(fp, " in:");
11957 for(in_set = rb->in; in_set; in_set = in_set->next) {
11958 fprintf(fp, " %-10p", in_set->member);
11959 }
11960 fprintf(fp, "\n");
11961 }
11962 phi_present = 0;
11963 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
11964 done = (ptr == block->last);
11965 if (ptr->op == OP_PHI) {
11966 phi_present = 1;
11967 break;
11968 }
11969 }
11970 if (phi_present) {
11971 int edge;
11972 for(edge = 0; edge < block->users; edge++) {
11973 fprintf(fp, " in(%d):", edge);
11974 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
11975 struct triple **slot;
11976 done = (ptr == block->last);
11977 if (ptr->op != OP_PHI) {
11978 continue;
11979 }
11980 slot = &RHS(ptr, 0);
11981 fprintf(fp, " %-10p", slot[edge]);
11982 }
11983 fprintf(fp, "\n");
11984 }
11985 }
11986 if (block->first->op == OP_LABEL) {
11987 fprintf(fp, "%p:\n", block->first);
11988 }
11989 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000011990 struct live_range *lr;
11991 unsigned id;
11992 int op;
11993 op = ptr->op;
11994 done = (ptr == block->last);
11995 lr = rstate->lrd[ptr->id].lr;
11996
Eric Biedermand1ea5392003-06-28 06:49:45 +000011997 id = ptr->id;
11998 ptr->id = rstate->lrd[id].orig_id;
11999 SET_REG(ptr->id, lr->color);
12000 display_triple(fp, ptr);
12001 ptr->id = id;
12002
12003 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
12004 internal_error(state, ptr, "lr has no defs!");
12005 }
12006 if (info->need_edges) {
12007 if (lr->defs) {
12008 struct live_range_def *lrd;
12009 fprintf(fp, " range:");
12010 lrd = lr->defs;
12011 do {
12012 fprintf(fp, " %-10p", lrd->def);
12013 lrd = lrd->next;
12014 } while(lrd != lr->defs);
12015 fprintf(fp, "\n");
12016 }
12017 if (lr->edges > 0) {
12018 struct live_range_edge *edge;
12019 fprintf(fp, " edges:");
12020 for(edge = lr->edges; edge; edge = edge->next) {
12021 struct live_range_def *lrd;
12022 lrd = edge->node->defs;
12023 do {
12024 fprintf(fp, " %-10p", lrd->def);
12025 lrd = lrd->next;
12026 } while(lrd != edge->node->defs);
12027 fprintf(fp, "|");
12028 }
12029 fprintf(fp, "\n");
12030 }
12031 }
12032 /* Do a bunch of sanity checks */
12033 valid_ins(state, ptr);
12034 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
12035 internal_error(state, ptr, "Invalid triple id: %d",
12036 ptr->id);
12037 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000012038 }
12039 if (rb->out) {
12040 struct triple_reg_set *out_set;
12041 fprintf(fp, " out:");
12042 for(out_set = rb->out; out_set; out_set = out_set->next) {
12043 fprintf(fp, " %-10p", out_set->member);
12044 }
12045 fprintf(fp, "\n");
12046 }
12047 fprintf(fp, "\n");
12048}
12049
12050static void print_interference_blocks(
12051 struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
12052{
12053 struct print_interference_block_info info;
12054 info.rstate = rstate;
12055 info.fp = fp;
12056 info.need_edges = need_edges;
12057 fprintf(fp, "\nlive variables by block\n");
12058 walk_blocks(state, print_interference_block, &info);
12059
12060}
12061
Eric Biedermanb138ac82003-04-22 18:44:01 +000012062static unsigned regc_max_size(struct compile_state *state, int classes)
12063{
12064 unsigned max_size;
12065 int i;
12066 max_size = 0;
12067 for(i = 0; i < MAX_REGC; i++) {
12068 if (classes & (1 << i)) {
12069 unsigned size;
12070 size = arch_regc_size(state, i);
12071 if (size > max_size) {
12072 max_size = size;
12073 }
12074 }
12075 }
12076 return max_size;
12077}
12078
12079static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
12080{
12081 unsigned equivs[MAX_REG_EQUIVS];
12082 int i;
12083 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
12084 internal_error(state, 0, "invalid register");
12085 }
12086 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
12087 internal_error(state, 0, "invalid register");
12088 }
12089 arch_reg_equivs(state, equivs, reg1);
12090 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
12091 if (equivs[i] == reg2) {
12092 return 1;
12093 }
12094 }
12095 return 0;
12096}
12097
12098static void reg_fill_used(struct compile_state *state, char *used, int reg)
12099{
12100 unsigned equivs[MAX_REG_EQUIVS];
12101 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012102 if (reg == REG_UNNEEDED) {
12103 return;
12104 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012105 arch_reg_equivs(state, equivs, reg);
12106 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
12107 used[equivs[i]] = 1;
12108 }
12109 return;
12110}
12111
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012112static void reg_inc_used(struct compile_state *state, char *used, int reg)
12113{
12114 unsigned equivs[MAX_REG_EQUIVS];
12115 int i;
12116 if (reg == REG_UNNEEDED) {
12117 return;
12118 }
12119 arch_reg_equivs(state, equivs, reg);
12120 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
12121 used[equivs[i]] += 1;
12122 }
12123 return;
12124}
12125
Eric Biedermanb138ac82003-04-22 18:44:01 +000012126static unsigned int hash_live_edge(
12127 struct live_range *left, struct live_range *right)
12128{
12129 unsigned int hash, val;
12130 unsigned long lval, rval;
12131 lval = ((unsigned long)left)/sizeof(struct live_range);
12132 rval = ((unsigned long)right)/sizeof(struct live_range);
12133 hash = 0;
12134 while(lval) {
12135 val = lval & 0xff;
12136 lval >>= 8;
12137 hash = (hash *263) + val;
12138 }
12139 while(rval) {
12140 val = rval & 0xff;
12141 rval >>= 8;
12142 hash = (hash *263) + val;
12143 }
12144 hash = hash & (LRE_HASH_SIZE - 1);
12145 return hash;
12146}
12147
12148static struct lre_hash **lre_probe(struct reg_state *rstate,
12149 struct live_range *left, struct live_range *right)
12150{
12151 struct lre_hash **ptr;
12152 unsigned int index;
12153 /* Ensure left <= right */
12154 if (left > right) {
12155 struct live_range *tmp;
12156 tmp = left;
12157 left = right;
12158 right = tmp;
12159 }
12160 index = hash_live_edge(left, right);
12161
12162 ptr = &rstate->hash[index];
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012163 while(*ptr) {
12164 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
12165 break;
12166 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012167 ptr = &(*ptr)->next;
12168 }
12169 return ptr;
12170}
12171
12172static int interfere(struct reg_state *rstate,
12173 struct live_range *left, struct live_range *right)
12174{
12175 struct lre_hash **ptr;
12176 ptr = lre_probe(rstate, left, right);
12177 return ptr && *ptr;
12178}
12179
12180static void add_live_edge(struct reg_state *rstate,
12181 struct live_range *left, struct live_range *right)
12182{
12183 /* FIXME the memory allocation overhead is noticeable here... */
12184 struct lre_hash **ptr, *new_hash;
12185 struct live_range_edge *edge;
12186
12187 if (left == right) {
12188 return;
12189 }
12190 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
12191 return;
12192 }
12193 /* Ensure left <= right */
12194 if (left > right) {
12195 struct live_range *tmp;
12196 tmp = left;
12197 left = right;
12198 right = tmp;
12199 }
12200 ptr = lre_probe(rstate, left, right);
12201 if (*ptr) {
12202 return;
12203 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012204#if 0
12205 fprintf(stderr, "new_live_edge(%p, %p)\n",
12206 left, right);
12207#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000012208 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
12209 new_hash->next = *ptr;
12210 new_hash->left = left;
12211 new_hash->right = right;
12212 *ptr = new_hash;
12213
12214 edge = xmalloc(sizeof(*edge), "live_range_edge");
12215 edge->next = left->edges;
12216 edge->node = right;
12217 left->edges = edge;
12218 left->degree += 1;
12219
12220 edge = xmalloc(sizeof(*edge), "live_range_edge");
12221 edge->next = right->edges;
12222 edge->node = left;
12223 right->edges = edge;
12224 right->degree += 1;
12225}
12226
12227static void remove_live_edge(struct reg_state *rstate,
12228 struct live_range *left, struct live_range *right)
12229{
12230 struct live_range_edge *edge, **ptr;
12231 struct lre_hash **hptr, *entry;
12232 hptr = lre_probe(rstate, left, right);
12233 if (!hptr || !*hptr) {
12234 return;
12235 }
12236 entry = *hptr;
12237 *hptr = entry->next;
12238 xfree(entry);
12239
12240 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
12241 edge = *ptr;
12242 if (edge->node == right) {
12243 *ptr = edge->next;
12244 memset(edge, 0, sizeof(*edge));
12245 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012246 right->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012247 break;
12248 }
12249 }
12250 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
12251 edge = *ptr;
12252 if (edge->node == left) {
12253 *ptr = edge->next;
12254 memset(edge, 0, sizeof(*edge));
12255 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012256 left->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012257 break;
12258 }
12259 }
12260}
12261
12262static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
12263{
12264 struct live_range_edge *edge, *next;
12265 for(edge = range->edges; edge; edge = next) {
12266 next = edge->next;
12267 remove_live_edge(rstate, range, edge->node);
12268 }
12269}
12270
Eric Biederman153ea352003-06-20 14:43:20 +000012271static void transfer_live_edges(struct reg_state *rstate,
12272 struct live_range *dest, struct live_range *src)
12273{
12274 struct live_range_edge *edge, *next;
12275 for(edge = src->edges; edge; edge = next) {
12276 struct live_range *other;
12277 next = edge->next;
12278 other = edge->node;
12279 remove_live_edge(rstate, src, other);
12280 add_live_edge(rstate, dest, other);
12281 }
12282}
12283
Eric Biedermanb138ac82003-04-22 18:44:01 +000012284
12285/* Interference graph...
12286 *
12287 * new(n) --- Return a graph with n nodes but no edges.
12288 * add(g,x,y) --- Return a graph including g with an between x and y
12289 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
12290 * x and y in the graph g
12291 * degree(g, x) --- Return the degree of the node x in the graph g
12292 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
12293 *
12294 * Implement with a hash table && a set of adjcency vectors.
12295 * The hash table supports constant time implementations of add and interfere.
12296 * The adjacency vectors support an efficient implementation of neighbors.
12297 */
12298
12299/*
12300 * +---------------------------------------------------+
12301 * | +--------------+ |
12302 * v v | |
12303 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
12304 *
12305 * -- In simplify implment optimistic coloring... (No backtracking)
12306 * -- Implement Rematerialization it is the only form of spilling we can perform
12307 * Essentially this means dropping a constant from a register because
12308 * we can regenerate it later.
12309 *
12310 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
12311 * coalesce at phi points...
12312 * --- Bias coloring if at all possible do the coalesing a compile time.
12313 *
12314 *
12315 */
12316
12317static void different_colored(
12318 struct compile_state *state, struct reg_state *rstate,
12319 struct triple *parent, struct triple *ins)
12320{
12321 struct live_range *lr;
12322 struct triple **expr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012323 lr = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012324 expr = triple_rhs(state, ins, 0);
12325 for(;expr; expr = triple_rhs(state, ins, expr)) {
12326 struct live_range *lr2;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012327 if (!*expr || (*expr == parent) || (*expr == ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012328 continue;
12329 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012330 lr2 = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012331 if (lr->color == lr2->color) {
12332 internal_error(state, ins, "live range too big");
12333 }
12334 }
12335}
12336
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012337
12338static struct live_range *coalesce_ranges(
12339 struct compile_state *state, struct reg_state *rstate,
12340 struct live_range *lr1, struct live_range *lr2)
12341{
12342 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
12343 unsigned color;
12344 unsigned classes;
12345 if (lr1 == lr2) {
12346 return lr1;
12347 }
12348 if (!lr1->defs || !lr2->defs) {
12349 internal_error(state, 0,
12350 "cannot coalese dead live ranges");
12351 }
12352 if ((lr1->color == REG_UNNEEDED) ||
12353 (lr2->color == REG_UNNEEDED)) {
12354 internal_error(state, 0,
12355 "cannot coalesce live ranges without a possible color");
12356 }
12357 if ((lr1->color != lr2->color) &&
12358 (lr1->color != REG_UNSET) &&
12359 (lr2->color != REG_UNSET)) {
12360 internal_error(state, lr1->defs->def,
12361 "cannot coalesce live ranges of different colors");
12362 }
12363 color = lr1->color;
12364 if (color == REG_UNSET) {
12365 color = lr2->color;
12366 }
12367 classes = lr1->classes & lr2->classes;
12368 if (!classes) {
12369 internal_error(state, lr1->defs->def,
12370 "cannot coalesce live ranges with dissimilar register classes");
12371 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000012372#if DEBUG_COALESCING
12373 fprintf(stderr, "coalescing:");
12374 lrd = lr1->defs;
12375 do {
12376 fprintf(stderr, " %p", lrd->def);
12377 lrd = lrd->next;
12378 } while(lrd != lr1->defs);
12379 fprintf(stderr, " |");
12380 lrd = lr2->defs;
12381 do {
12382 fprintf(stderr, " %p", lrd->def);
12383 lrd = lrd->next;
12384 } while(lrd != lr2->defs);
12385 fprintf(stderr, "\n");
12386#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012387 /* If there is a clear dominate live range put it in lr1,
12388 * For purposes of this test phi functions are
12389 * considered dominated by the definitions that feed into
12390 * them.
12391 */
12392 if ((lr1->defs->prev->def->op == OP_PHI) ||
12393 ((lr2->defs->prev->def->op != OP_PHI) &&
12394 tdominates(state, lr2->defs->def, lr1->defs->def))) {
12395 struct live_range *tmp;
12396 tmp = lr1;
12397 lr1 = lr2;
12398 lr2 = tmp;
12399 }
12400#if 0
12401 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
12402 fprintf(stderr, "lr1 post\n");
12403 }
12404 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
12405 fprintf(stderr, "lr1 pre\n");
12406 }
12407 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
12408 fprintf(stderr, "lr2 post\n");
12409 }
12410 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
12411 fprintf(stderr, "lr2 pre\n");
12412 }
12413#endif
Eric Biederman153ea352003-06-20 14:43:20 +000012414#if 0
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012415 fprintf(stderr, "coalesce color1(%p): %3d color2(%p) %3d\n",
12416 lr1->defs->def,
12417 lr1->color,
12418 lr2->defs->def,
12419 lr2->color);
12420#endif
12421
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012422 /* Append lr2 onto lr1 */
12423#warning "FIXME should this be a merge instead of a splice?"
Eric Biederman153ea352003-06-20 14:43:20 +000012424 /* This FIXME item applies to the correctness of live_range_end
12425 * and to the necessity of making multiple passes of coalesce_live_ranges.
12426 * A failure to find some coalesce opportunities in coaleace_live_ranges
12427 * does not impact the correct of the compiler just the efficiency with
12428 * which registers are allocated.
12429 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012430 head = lr1->defs;
12431 mid1 = lr1->defs->prev;
12432 mid2 = lr2->defs;
12433 end = lr2->defs->prev;
12434
12435 head->prev = end;
12436 end->next = head;
12437
12438 mid1->next = mid2;
12439 mid2->prev = mid1;
12440
12441 /* Fixup the live range in the added live range defs */
12442 lrd = head;
12443 do {
12444 lrd->lr = lr1;
12445 lrd = lrd->next;
12446 } while(lrd != head);
12447
12448 /* Mark lr2 as free. */
12449 lr2->defs = 0;
12450 lr2->color = REG_UNNEEDED;
12451 lr2->classes = 0;
12452
12453 if (!lr1->defs) {
12454 internal_error(state, 0, "lr1->defs == 0 ?");
12455 }
12456
12457 lr1->color = color;
12458 lr1->classes = classes;
12459
Eric Biederman153ea352003-06-20 14:43:20 +000012460 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
12461 transfer_live_edges(rstate, lr1, lr2);
12462
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012463 return lr1;
12464}
12465
12466static struct live_range_def *live_range_head(
12467 struct compile_state *state, struct live_range *lr,
12468 struct live_range_def *last)
12469{
12470 struct live_range_def *result;
12471 result = 0;
12472 if (last == 0) {
12473 result = lr->defs;
12474 }
12475 else if (!tdominates(state, lr->defs->def, last->next->def)) {
12476 result = last->next;
12477 }
12478 return result;
12479}
12480
12481static struct live_range_def *live_range_end(
12482 struct compile_state *state, struct live_range *lr,
12483 struct live_range_def *last)
12484{
12485 struct live_range_def *result;
12486 result = 0;
12487 if (last == 0) {
12488 result = lr->defs->prev;
12489 }
12490 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
12491 result = last->prev;
12492 }
12493 return result;
12494}
12495
12496
Eric Biedermanb138ac82003-04-22 18:44:01 +000012497static void initialize_live_ranges(
12498 struct compile_state *state, struct reg_state *rstate)
12499{
12500 struct triple *ins, *first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012501 size_t count, size;
12502 int i, j;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012503
Eric Biederman0babc1c2003-05-09 02:39:00 +000012504 first = RHS(state->main_function, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012505 /* First count how many instructions I have.
Eric Biedermanb138ac82003-04-22 18:44:01 +000012506 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012507 count = count_triples(state);
12508 /* Potentially I need one live range definitions for each
Eric Biedermand1ea5392003-06-28 06:49:45 +000012509 * instruction.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012510 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000012511 rstate->defs = count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012512 /* Potentially I need one live range for each instruction
12513 * plus an extra for the dummy live range.
12514 */
12515 rstate->ranges = count + 1;
12516 size = sizeof(rstate->lrd[0]) * rstate->defs;
12517 rstate->lrd = xcmalloc(size, "live_range_def");
12518 size = sizeof(rstate->lr[0]) * rstate->ranges;
12519 rstate->lr = xcmalloc(size, "live_range");
12520
Eric Biedermanb138ac82003-04-22 18:44:01 +000012521 /* Setup the dummy live range */
12522 rstate->lr[0].classes = 0;
12523 rstate->lr[0].color = REG_UNSET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012524 rstate->lr[0].defs = 0;
12525 i = j = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012526 ins = first;
12527 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012528 /* If the triple is a variable give it a live range */
Eric Biederman0babc1c2003-05-09 02:39:00 +000012529 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012530 struct reg_info info;
12531 /* Find the architecture specific color information */
12532 info = find_def_color(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012533 i++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012534 rstate->lr[i].defs = &rstate->lrd[j];
12535 rstate->lr[i].color = info.reg;
12536 rstate->lr[i].classes = info.regcm;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012537 rstate->lr[i].degree = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012538 rstate->lrd[j].lr = &rstate->lr[i];
12539 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012540 /* Otherwise give the triple the dummy live range. */
12541 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012542 rstate->lrd[j].lr = &rstate->lr[0];
Eric Biedermanb138ac82003-04-22 18:44:01 +000012543 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012544
12545 /* Initalize the live_range_def */
12546 rstate->lrd[j].next = &rstate->lrd[j];
12547 rstate->lrd[j].prev = &rstate->lrd[j];
12548 rstate->lrd[j].def = ins;
12549 rstate->lrd[j].orig_id = ins->id;
12550 ins->id = j;
12551
12552 j++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012553 ins = ins->next;
12554 } while(ins != first);
12555 rstate->ranges = i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012556
Eric Biedermanb138ac82003-04-22 18:44:01 +000012557 /* Make a second pass to handle achitecture specific register
12558 * constraints.
12559 */
12560 ins = first;
12561 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012562 int zlhs, zrhs, i, j;
12563 if (ins->id > rstate->defs) {
12564 internal_error(state, ins, "bad id");
12565 }
12566
12567 /* Walk through the template of ins and coalesce live ranges */
12568 zlhs = TRIPLE_LHS(ins->sizes);
12569 if ((zlhs == 0) && triple_is_def(state, ins)) {
12570 zlhs = 1;
12571 }
12572 zrhs = TRIPLE_RHS(ins->sizes);
Eric Biedermand1ea5392003-06-28 06:49:45 +000012573
12574#if DEBUG_COALESCING > 1
12575 fprintf(stderr, "mandatory coalesce: %p %d %d\n",
12576 ins, zlhs, zrhs);
Eric Biedermand1ea5392003-06-28 06:49:45 +000012577#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012578 for(i = 0; i < zlhs; i++) {
12579 struct reg_info linfo;
12580 struct live_range_def *lhs;
12581 linfo = arch_reg_lhs(state, ins, i);
12582 if (linfo.reg < MAX_REGISTERS) {
12583 continue;
12584 }
12585 if (triple_is_def(state, ins)) {
12586 lhs = &rstate->lrd[ins->id];
12587 } else {
12588 lhs = &rstate->lrd[LHS(ins, i)->id];
12589 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000012590#if DEBUG_COALESCING > 1
12591 fprintf(stderr, "coalesce lhs(%d): %p %d\n",
12592 i, lhs, linfo.reg);
12593
12594#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012595 for(j = 0; j < zrhs; j++) {
12596 struct reg_info rinfo;
12597 struct live_range_def *rhs;
12598 rinfo = arch_reg_rhs(state, ins, j);
12599 if (rinfo.reg < MAX_REGISTERS) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012600 continue;
12601 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000012602 rhs = &rstate->lrd[RHS(ins, j)->id];
12603#if DEBUG_COALESCING > 1
12604 fprintf(stderr, "coalesce rhs(%d): %p %d\n",
12605 j, rhs, rinfo.reg);
12606
12607#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012608 if (rinfo.reg == linfo.reg) {
12609 coalesce_ranges(state, rstate,
12610 lhs->lr, rhs->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012611 }
12612 }
12613 }
12614 ins = ins->next;
12615 } while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012616}
12617
Eric Biedermanf96a8102003-06-16 16:57:34 +000012618static void graph_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000012619 struct compile_state *state,
12620 struct reg_block *blocks, struct triple_reg_set *live,
12621 struct reg_block *rb, struct triple *ins, void *arg)
12622{
12623 struct reg_state *rstate = arg;
12624 struct live_range *def;
12625 struct triple_reg_set *entry;
12626
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012627 /* If the triple is not a definition
Eric Biedermanb138ac82003-04-22 18:44:01 +000012628 * we do not have a definition to add to
12629 * the interference graph.
12630 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012631 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000012632 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012633 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012634 def = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012635
12636 /* Create an edge between ins and everything that is
12637 * alive, unless the live_range cannot share
12638 * a physical register with ins.
12639 */
12640 for(entry = live; entry; entry = entry->next) {
12641 struct live_range *lr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012642 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
12643 internal_error(state, 0, "bad entry?");
12644 }
12645 lr = rstate->lrd[entry->member->id].lr;
12646 if (def == lr) {
12647 continue;
12648 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012649 if (!arch_regcm_intersect(def->classes, lr->classes)) {
12650 continue;
12651 }
12652 add_live_edge(rstate, def, lr);
12653 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012654 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012655}
12656
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012657static struct live_range *get_verify_live_range(
12658 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
12659{
12660 struct live_range *lr;
12661 struct live_range_def *lrd;
12662 int ins_found;
12663 if ((ins->id < 0) || (ins->id > rstate->defs)) {
12664 internal_error(state, ins, "bad ins?");
12665 }
12666 lr = rstate->lrd[ins->id].lr;
12667 ins_found = 0;
12668 lrd = lr->defs;
12669 do {
12670 if (lrd->def == ins) {
12671 ins_found = 1;
12672 }
12673 lrd = lrd->next;
12674 } while(lrd != lr->defs);
12675 if (!ins_found) {
12676 internal_error(state, ins, "ins not in live range");
12677 }
12678 return lr;
12679}
12680
12681static void verify_graph_ins(
12682 struct compile_state *state,
12683 struct reg_block *blocks, struct triple_reg_set *live,
12684 struct reg_block *rb, struct triple *ins, void *arg)
12685{
12686 struct reg_state *rstate = arg;
12687 struct triple_reg_set *entry1, *entry2;
12688
12689
12690 /* Compare live against edges and make certain the code is working */
12691 for(entry1 = live; entry1; entry1 = entry1->next) {
12692 struct live_range *lr1;
12693 lr1 = get_verify_live_range(state, rstate, entry1->member);
12694 for(entry2 = live; entry2; entry2 = entry2->next) {
12695 struct live_range *lr2;
12696 struct live_range_edge *edge2;
12697 int lr1_found;
12698 int lr2_degree;
12699 if (entry2 == entry1) {
12700 continue;
12701 }
12702 lr2 = get_verify_live_range(state, rstate, entry2->member);
12703 if (lr1 == lr2) {
12704 internal_error(state, entry2->member,
12705 "live range with 2 values simultaneously alive");
12706 }
12707 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
12708 continue;
12709 }
12710 if (!interfere(rstate, lr1, lr2)) {
12711 internal_error(state, entry2->member,
12712 "edges don't interfere?");
12713 }
12714
12715 lr1_found = 0;
12716 lr2_degree = 0;
12717 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
12718 lr2_degree++;
12719 if (edge2->node == lr1) {
12720 lr1_found = 1;
12721 }
12722 }
12723 if (lr2_degree != lr2->degree) {
12724 internal_error(state, entry2->member,
12725 "computed degree: %d does not match reported degree: %d\n",
12726 lr2_degree, lr2->degree);
12727 }
12728 if (!lr1_found) {
12729 internal_error(state, entry2->member, "missing edge");
12730 }
12731 }
12732 }
12733 return;
12734}
12735
Eric Biedermanb138ac82003-04-22 18:44:01 +000012736
Eric Biedermanf96a8102003-06-16 16:57:34 +000012737static void print_interference_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000012738 struct compile_state *state,
12739 struct reg_block *blocks, struct triple_reg_set *live,
12740 struct reg_block *rb, struct triple *ins, void *arg)
12741{
12742 struct reg_state *rstate = arg;
12743 struct live_range *lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012744 unsigned id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012745
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012746 lr = rstate->lrd[ins->id].lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012747 id = ins->id;
12748 ins->id = rstate->lrd[id].orig_id;
12749 SET_REG(ins->id, lr->color);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012750 display_triple(stdout, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012751 ins->id = id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012752
12753 if (lr->defs) {
12754 struct live_range_def *lrd;
12755 printf(" range:");
12756 lrd = lr->defs;
12757 do {
12758 printf(" %-10p", lrd->def);
12759 lrd = lrd->next;
12760 } while(lrd != lr->defs);
12761 printf("\n");
12762 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012763 if (live) {
12764 struct triple_reg_set *entry;
12765 printf(" live:");
12766 for(entry = live; entry; entry = entry->next) {
12767 printf(" %-10p", entry->member);
12768 }
12769 printf("\n");
12770 }
12771 if (lr->edges) {
12772 struct live_range_edge *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012773 printf(" edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000012774 for(entry = lr->edges; entry; entry = entry->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012775 struct live_range_def *lrd;
12776 lrd = entry->node->defs;
12777 do {
12778 printf(" %-10p", lrd->def);
12779 lrd = lrd->next;
12780 } while(lrd != entry->node->defs);
12781 printf("|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000012782 }
12783 printf("\n");
12784 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012785 if (triple_is_branch(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012786 printf("\n");
12787 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012788 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012789}
12790
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012791static int coalesce_live_ranges(
12792 struct compile_state *state, struct reg_state *rstate)
12793{
12794 /* At the point where a value is moved from one
12795 * register to another that value requires two
12796 * registers, thus increasing register pressure.
12797 * Live range coaleescing reduces the register
12798 * pressure by keeping a value in one register
12799 * longer.
12800 *
12801 * In the case of a phi function all paths leading
12802 * into it must be allocated to the same register
12803 * otherwise the phi function may not be removed.
12804 *
12805 * Forcing a value to stay in a single register
12806 * for an extended period of time does have
12807 * limitations when applied to non homogenous
12808 * register pool.
12809 *
12810 * The two cases I have identified are:
12811 * 1) Two forced register assignments may
12812 * collide.
12813 * 2) Registers may go unused because they
12814 * are only good for storing the value
12815 * and not manipulating it.
12816 *
12817 * Because of this I need to split live ranges,
12818 * even outside of the context of coalesced live
12819 * ranges. The need to split live ranges does
12820 * impose some constraints on live range coalescing.
12821 *
12822 * - Live ranges may not be coalesced across phi
12823 * functions. This creates a 2 headed live
12824 * range that cannot be sanely split.
12825 *
12826 * - phi functions (coalesced in initialize_live_ranges)
12827 * are handled as pre split live ranges so we will
12828 * never attempt to split them.
12829 */
12830 int coalesced;
12831 int i;
12832
12833 coalesced = 0;
12834 for(i = 0; i <= rstate->ranges; i++) {
12835 struct live_range *lr1;
12836 struct live_range_def *lrd1;
12837 lr1 = &rstate->lr[i];
12838 if (!lr1->defs) {
12839 continue;
12840 }
12841 lrd1 = live_range_end(state, lr1, 0);
12842 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
12843 struct triple_set *set;
12844 if (lrd1->def->op != OP_COPY) {
12845 continue;
12846 }
12847 /* Skip copies that are the result of a live range split. */
12848 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
12849 continue;
12850 }
12851 for(set = lrd1->def->use; set; set = set->next) {
12852 struct live_range_def *lrd2;
12853 struct live_range *lr2, *res;
12854
12855 lrd2 = &rstate->lrd[set->member->id];
12856
12857 /* Don't coalesce with instructions
12858 * that are the result of a live range
12859 * split.
12860 */
12861 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
12862 continue;
12863 }
12864 lr2 = rstate->lrd[set->member->id].lr;
12865 if (lr1 == lr2) {
12866 continue;
12867 }
12868 if ((lr1->color != lr2->color) &&
12869 (lr1->color != REG_UNSET) &&
12870 (lr2->color != REG_UNSET)) {
12871 continue;
12872 }
12873 if ((lr1->classes & lr2->classes) == 0) {
12874 continue;
12875 }
12876
12877 if (interfere(rstate, lr1, lr2)) {
12878 continue;
12879 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012880
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012881 res = coalesce_ranges(state, rstate, lr1, lr2);
12882 coalesced += 1;
12883 if (res != lr1) {
12884 goto next;
12885 }
12886 }
12887 }
12888 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000012889 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012890 }
12891 return coalesced;
12892}
12893
12894
Eric Biedermanf96a8102003-06-16 16:57:34 +000012895static void fix_coalesce_conflicts(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012896 struct reg_block *blocks, struct triple_reg_set *live,
12897 struct reg_block *rb, struct triple *ins, void *arg)
12898{
Eric Biedermand1ea5392003-06-28 06:49:45 +000012899 int *conflicts = arg;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012900 int zlhs, zrhs, i, j;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012901
12902 /* See if we have a mandatory coalesce operation between
12903 * a lhs and a rhs value. If so and the rhs value is also
12904 * alive then this triple needs to be pre copied. Otherwise
12905 * we would have two definitions in the same live range simultaneously
12906 * alive.
12907 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012908 zlhs = TRIPLE_LHS(ins->sizes);
12909 if ((zlhs == 0) && triple_is_def(state, ins)) {
12910 zlhs = 1;
12911 }
12912 zrhs = TRIPLE_RHS(ins->sizes);
Eric Biedermanf96a8102003-06-16 16:57:34 +000012913 for(i = 0; i < zlhs; i++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012914 struct reg_info linfo;
12915 linfo = arch_reg_lhs(state, ins, i);
12916 if (linfo.reg < MAX_REGISTERS) {
12917 continue;
12918 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012919 for(j = 0; j < zrhs; j++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012920 struct reg_info rinfo;
12921 struct triple *rhs;
12922 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000012923 int found;
12924 found = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012925 rinfo = arch_reg_rhs(state, ins, j);
12926 if (rinfo.reg != linfo.reg) {
12927 continue;
12928 }
12929 rhs = RHS(ins, j);
Eric Biedermanf96a8102003-06-16 16:57:34 +000012930 for(set = live; set && !found; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012931 if (set->member == rhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000012932 found = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012933 }
12934 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012935 if (found) {
12936 struct triple *copy;
12937 copy = pre_copy(state, ins, j);
12938 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermand1ea5392003-06-28 06:49:45 +000012939 (*conflicts)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000012940 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012941 }
12942 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012943 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012944}
12945
Eric Biedermand1ea5392003-06-28 06:49:45 +000012946static int correct_coalesce_conflicts(
12947 struct compile_state *state, struct reg_block *blocks)
12948{
12949 int conflicts;
12950 conflicts = 0;
12951 walk_variable_lifetimes(state, blocks, fix_coalesce_conflicts, &conflicts);
12952 return conflicts;
12953}
12954
Eric Biedermanf96a8102003-06-16 16:57:34 +000012955static void replace_set_use(struct compile_state *state,
12956 struct triple_reg_set *head, struct triple *orig, struct triple *new)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012957{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012958 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000012959 for(set = head; set; set = set->next) {
12960 if (set->member == orig) {
12961 set->member = new;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012962 }
12963 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012964}
12965
Eric Biedermanf96a8102003-06-16 16:57:34 +000012966static void replace_block_use(struct compile_state *state,
12967 struct reg_block *blocks, struct triple *orig, struct triple *new)
12968{
12969 int i;
12970#warning "WISHLIST visit just those blocks that need it *"
12971 for(i = 1; i <= state->last_vertex; i++) {
12972 struct reg_block *rb;
12973 rb = &blocks[i];
12974 replace_set_use(state, rb->in, orig, new);
12975 replace_set_use(state, rb->out, orig, new);
12976 }
12977}
12978
12979static void color_instructions(struct compile_state *state)
12980{
12981 struct triple *ins, *first;
12982 first = RHS(state->main_function, 0);
12983 ins = first;
12984 do {
12985 if (triple_is_def(state, ins)) {
12986 struct reg_info info;
12987 info = find_lhs_color(state, ins, 0);
12988 if (info.reg >= MAX_REGISTERS) {
12989 info.reg = REG_UNSET;
12990 }
12991 SET_INFO(ins->id, info);
12992 }
12993 ins = ins->next;
12994 } while(ins != first);
12995}
12996
12997static struct reg_info read_lhs_color(
12998 struct compile_state *state, struct triple *ins, int index)
12999{
13000 struct reg_info info;
13001 if ((index == 0) && triple_is_def(state, ins)) {
13002 info.reg = ID_REG(ins->id);
13003 info.regcm = ID_REGCM(ins->id);
13004 }
13005 else if (index < TRIPLE_LHS(ins->sizes)) {
13006 info = read_lhs_color(state, LHS(ins, index), 0);
13007 }
13008 else {
13009 internal_error(state, ins, "Bad lhs %d", index);
13010 info.reg = REG_UNSET;
13011 info.regcm = 0;
13012 }
13013 return info;
13014}
13015
13016static struct triple *resolve_tangle(
13017 struct compile_state *state, struct triple *tangle)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013018{
13019 struct reg_info info, uinfo;
13020 struct triple_set *set, *next;
13021 struct triple *copy;
13022
Eric Biedermanf96a8102003-06-16 16:57:34 +000013023#warning "WISHLIST recalculate all affected instructions colors"
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013024 info = find_lhs_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013025 for(set = tangle->use; set; set = next) {
13026 struct triple *user;
13027 int i, zrhs;
13028 next = set->next;
13029 user = set->member;
13030 zrhs = TRIPLE_RHS(user->sizes);
13031 for(i = 0; i < zrhs; i++) {
13032 if (RHS(user, i) != tangle) {
13033 continue;
13034 }
13035 uinfo = find_rhs_post_color(state, user, i);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013036 if (uinfo.reg == info.reg) {
13037 copy = pre_copy(state, user, i);
13038 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000013039 SET_INFO(copy->id, uinfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013040 }
13041 }
13042 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000013043 copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013044 uinfo = find_lhs_pre_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013045 if (uinfo.reg == info.reg) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000013046 struct reg_info linfo;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013047 copy = post_copy(state, tangle);
13048 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000013049 linfo = find_lhs_color(state, copy, 0);
13050 SET_INFO(copy->id, linfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013051 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000013052 info = find_lhs_color(state, tangle, 0);
13053 SET_INFO(tangle->id, info);
13054
13055 return copy;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013056}
13057
13058
Eric Biedermanf96a8102003-06-16 16:57:34 +000013059static void fix_tangles(struct compile_state *state,
13060 struct reg_block *blocks, struct triple_reg_set *live,
13061 struct reg_block *rb, struct triple *ins, void *arg)
13062{
Eric Biederman153ea352003-06-20 14:43:20 +000013063 int *tangles = arg;
Eric Biedermanf96a8102003-06-16 16:57:34 +000013064 struct triple *tangle;
13065 do {
13066 char used[MAX_REGISTERS];
13067 struct triple_reg_set *set;
13068 tangle = 0;
13069
13070 /* Find out which registers have multiple uses at this point */
13071 memset(used, 0, sizeof(used));
13072 for(set = live; set; set = set->next) {
13073 struct reg_info info;
13074 info = read_lhs_color(state, set->member, 0);
13075 if (info.reg == REG_UNSET) {
13076 continue;
13077 }
13078 reg_inc_used(state, used, info.reg);
13079 }
13080
13081 /* Now find the least dominated definition of a register in
13082 * conflict I have seen so far.
13083 */
13084 for(set = live; set; set = set->next) {
13085 struct reg_info info;
13086 info = read_lhs_color(state, set->member, 0);
13087 if (used[info.reg] < 2) {
13088 continue;
13089 }
Eric Biederman153ea352003-06-20 14:43:20 +000013090 /* Changing copies that feed into phi functions
13091 * is incorrect.
13092 */
13093 if (set->member->use &&
13094 (set->member->use->member->op == OP_PHI)) {
13095 continue;
13096 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000013097 if (!tangle || tdominates(state, set->member, tangle)) {
13098 tangle = set->member;
13099 }
13100 }
13101 /* If I have found a tangle resolve it */
13102 if (tangle) {
13103 struct triple *post_copy;
Eric Biederman153ea352003-06-20 14:43:20 +000013104 (*tangles)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000013105 post_copy = resolve_tangle(state, tangle);
13106 if (post_copy) {
13107 replace_block_use(state, blocks, tangle, post_copy);
13108 }
13109 if (post_copy && (tangle != ins)) {
13110 replace_set_use(state, live, tangle, post_copy);
13111 }
13112 }
13113 } while(tangle);
13114 return;
13115}
13116
Eric Biederman153ea352003-06-20 14:43:20 +000013117static int correct_tangles(
Eric Biedermanf96a8102003-06-16 16:57:34 +000013118 struct compile_state *state, struct reg_block *blocks)
13119{
Eric Biederman153ea352003-06-20 14:43:20 +000013120 int tangles;
13121 tangles = 0;
Eric Biedermanf96a8102003-06-16 16:57:34 +000013122 color_instructions(state);
Eric Biederman153ea352003-06-20 14:43:20 +000013123 walk_variable_lifetimes(state, blocks, fix_tangles, &tangles);
13124 return tangles;
Eric Biedermanf96a8102003-06-16 16:57:34 +000013125}
13126
Eric Biedermand1ea5392003-06-28 06:49:45 +000013127
13128static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
13129static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
13130
13131struct triple *find_constrained_def(
13132 struct compile_state *state, struct live_range *range, struct triple *constrained)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013133{
Eric Biedermand1ea5392003-06-28 06:49:45 +000013134 struct live_range_def *lrd;
13135 lrd = range->defs;
13136 do {
Eric Biedermand3283ec2003-06-18 11:03:18 +000013137 struct reg_info info;
Eric Biedermand1ea5392003-06-28 06:49:45 +000013138 unsigned regcm;
13139 int is_constrained;
13140 regcm = arch_type_to_regcm(state, lrd->def->type);
13141 info = find_lhs_color(state, lrd->def, 0);
13142 regcm = arch_regcm_reg_normalize(state, regcm);
13143 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
13144 /* If the 2 register class masks are not equal the
13145 * the current register class is constrained.
Eric Biedermand3283ec2003-06-18 11:03:18 +000013146 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000013147 is_constrained = regcm != info.regcm;
Eric Biedermand3283ec2003-06-18 11:03:18 +000013148
Eric Biedermand1ea5392003-06-28 06:49:45 +000013149 /* Of the constrained live ranges deal with the
13150 * least dominated one first.
Eric Biedermand3283ec2003-06-18 11:03:18 +000013151 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000013152 if (is_constrained) {
Eric Biederman530b5192003-07-01 10:05:30 +000013153#if DEBUG_RANGE_CONFLICTS
13154 fprintf(stderr, "canidate: %p %-8s regcm: %x %x\n",
13155 lrd->def, tops(lrd->def->op), regcm, info.regcm);
13156#endif
Eric Biedermand1ea5392003-06-28 06:49:45 +000013157 if (!constrained ||
13158 tdominates(state, lrd->def, constrained))
13159 {
13160 constrained = lrd->def;
Eric Biedermand3283ec2003-06-18 11:03:18 +000013161 }
13162 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000013163 lrd = lrd->next;
13164 } while(lrd != range->defs);
13165 return constrained;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013166}
13167
Eric Biedermand1ea5392003-06-28 06:49:45 +000013168static int split_constrained_ranges(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013169 struct compile_state *state, struct reg_state *rstate,
Eric Biedermand1ea5392003-06-28 06:49:45 +000013170 struct live_range *range)
13171{
13172 /* Walk through the edges in conflict and our current live
13173 * range, and find definitions that are more severly constrained
13174 * than they type of data they contain require.
13175 *
13176 * Then pick one of those ranges and relax the constraints.
13177 */
13178 struct live_range_edge *edge;
13179 struct triple *constrained;
13180
13181 constrained = 0;
13182 for(edge = range->edges; edge; edge = edge->next) {
13183 constrained = find_constrained_def(state, edge->node, constrained);
13184 }
13185 if (!constrained) {
13186 constrained = find_constrained_def(state, range, constrained);
13187 }
13188#if DEBUG_RANGE_CONFLICTS
Eric Biederman530b5192003-07-01 10:05:30 +000013189 fprintf(stderr, "constrained: %p %-8s\n",
13190 constrained, tops(constrained->op));
Eric Biedermand1ea5392003-06-28 06:49:45 +000013191#endif
13192 if (constrained) {
13193 ids_from_rstate(state, rstate);
13194 cleanup_rstate(state, rstate);
13195 resolve_tangle(state, constrained);
13196 }
13197 return !!constrained;
13198}
13199
13200static int split_ranges(
13201 struct compile_state *state, struct reg_state *rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013202 char *used, struct live_range *range)
13203{
Eric Biedermand1ea5392003-06-28 06:49:45 +000013204 int split;
13205#if DEBUG_RANGE_CONFLICTS
Eric Biedermand3283ec2003-06-18 11:03:18 +000013206 fprintf(stderr, "split_ranges %d %s %p\n",
13207 rstate->passes, tops(range->defs->def->op), range->defs->def);
13208#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013209 if ((range->color == REG_UNNEEDED) ||
13210 (rstate->passes >= rstate->max_passes)) {
13211 return 0;
13212 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000013213 split = split_constrained_ranges(state, rstate, range);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013214
Eric Biedermand1ea5392003-06-28 06:49:45 +000013215 /* Ideally I would split the live range that will not be used
13216 * for the longest period of time in hopes that this will
13217 * (a) allow me to spill a register or
13218 * (b) allow me to place a value in another register.
13219 *
13220 * So far I don't have a test case for this, the resolving
13221 * of mandatory constraints has solved all of my
13222 * know issues. So I have choosen not to write any
13223 * code until I cat get a better feel for cases where
13224 * it would be useful to have.
13225 *
13226 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013227#warning "WISHLIST implement live range splitting..."
Eric Biedermand1ea5392003-06-28 06:49:45 +000013228 if ((DEBUG_RANGE_CONFLICTS > 1) &&
13229 (!split || (DEBUG_RANGE_CONFLICTS > 2))) {
13230 print_interference_blocks(state, rstate, stderr, 0);
Eric Biedermand3283ec2003-06-18 11:03:18 +000013231 print_dominators(state, stderr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013232 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000013233 return split;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013234}
13235
Eric Biedermanb138ac82003-04-22 18:44:01 +000013236#if DEBUG_COLOR_GRAPH > 1
13237#define cgdebug_printf(...) fprintf(stdout, __VA_ARGS__)
13238#define cgdebug_flush() fflush(stdout)
Eric Biedermand1ea5392003-06-28 06:49:45 +000013239#define cgdebug_loc(STATE, TRIPLE) loc(stdout, STATE, TRIPLE)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013240#elif DEBUG_COLOR_GRAPH == 1
13241#define cgdebug_printf(...) fprintf(stderr, __VA_ARGS__)
13242#define cgdebug_flush() fflush(stderr)
Eric Biedermand1ea5392003-06-28 06:49:45 +000013243#define cgdebug_loc(STATE, TRIPLE) loc(stderr, STATE, TRIPLE)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013244#else
13245#define cgdebug_printf(...)
13246#define cgdebug_flush()
Eric Biedermand1ea5392003-06-28 06:49:45 +000013247#define cgdebug_loc(STATE, TRIPLE)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013248#endif
13249
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013250
13251static int select_free_color(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013252 struct reg_state *rstate, struct live_range *range)
13253{
13254 struct triple_set *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013255 struct live_range_def *lrd;
13256 struct live_range_def *phi;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013257 struct live_range_edge *edge;
13258 char used[MAX_REGISTERS];
13259 struct triple **expr;
13260
Eric Biedermanb138ac82003-04-22 18:44:01 +000013261 /* Instead of doing just the trivial color select here I try
13262 * a few extra things because a good color selection will help reduce
13263 * copies.
13264 */
13265
13266 /* Find the registers currently in use */
13267 memset(used, 0, sizeof(used));
13268 for(edge = range->edges; edge; edge = edge->next) {
13269 if (edge->node->color == REG_UNSET) {
13270 continue;
13271 }
13272 reg_fill_used(state, used, edge->node->color);
13273 }
13274#if DEBUG_COLOR_GRAPH > 1
13275 {
13276 int i;
13277 i = 0;
13278 for(edge = range->edges; edge; edge = edge->next) {
13279 i++;
13280 }
13281 cgdebug_printf("\n%s edges: %d @%s:%d.%d\n",
13282 tops(range->def->op), i,
13283 range->def->filename, range->def->line, range->def->col);
13284 for(i = 0; i < MAX_REGISTERS; i++) {
13285 if (used[i]) {
13286 cgdebug_printf("used: %s\n",
13287 arch_reg_str(i));
13288 }
13289 }
13290 }
13291#endif
13292
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013293 /* If a color is already assigned see if it will work */
13294 if (range->color != REG_UNSET) {
13295 struct live_range_def *lrd;
13296 if (!used[range->color]) {
13297 return 1;
13298 }
13299 for(edge = range->edges; edge; edge = edge->next) {
13300 if (edge->node->color != range->color) {
13301 continue;
13302 }
13303 warning(state, edge->node->defs->def, "edge: ");
13304 lrd = edge->node->defs;
13305 do {
13306 warning(state, lrd->def, " %p %s",
13307 lrd->def, tops(lrd->def->op));
13308 lrd = lrd->next;
13309 } while(lrd != edge->node->defs);
13310 }
13311 lrd = range->defs;
13312 warning(state, range->defs->def, "def: ");
13313 do {
13314 warning(state, lrd->def, " %p %s",
13315 lrd->def, tops(lrd->def->op));
13316 lrd = lrd->next;
13317 } while(lrd != range->defs);
13318 internal_error(state, range->defs->def,
13319 "live range with already used color %s",
13320 arch_reg_str(range->color));
13321 }
13322
Eric Biedermanb138ac82003-04-22 18:44:01 +000013323 /* If I feed into an expression reuse it's color.
13324 * This should help remove copies in the case of 2 register instructions
13325 * and phi functions.
13326 */
13327 phi = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013328 lrd = live_range_end(state, range, 0);
13329 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
13330 entry = lrd->def->use;
13331 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
13332 struct live_range_def *insd;
Eric Biederman530b5192003-07-01 10:05:30 +000013333 unsigned regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013334 insd = &rstate->lrd[entry->member->id];
13335 if (insd->lr->defs == 0) {
13336 continue;
13337 }
13338 if (!phi && (insd->def->op == OP_PHI) &&
13339 !interfere(rstate, range, insd->lr)) {
13340 phi = insd;
13341 }
Eric Biederman530b5192003-07-01 10:05:30 +000013342 if (insd->lr->color == REG_UNSET) {
13343 continue;
13344 }
13345 regcm = insd->lr->classes;
13346 if (((regcm & range->classes) == 0) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013347 (used[insd->lr->color])) {
13348 continue;
13349 }
13350 if (interfere(rstate, range, insd->lr)) {
13351 continue;
13352 }
13353 range->color = insd->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013354 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013355 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013356 /* If I feed into a phi function reuse it's color or the color
Eric Biedermanb138ac82003-04-22 18:44:01 +000013357 * of something else that feeds into the phi function.
13358 */
13359 if (phi) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013360 if (phi->lr->color != REG_UNSET) {
13361 if (used[phi->lr->color]) {
13362 range->color = phi->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013363 }
13364 }
13365 else {
13366 expr = triple_rhs(state, phi->def, 0);
13367 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
13368 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000013369 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013370 if (!*expr) {
13371 continue;
13372 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013373 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000013374 if (lr->color == REG_UNSET) {
13375 continue;
13376 }
13377 regcm = lr->classes;
13378 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000013379 (used[lr->color])) {
13380 continue;
13381 }
13382 if (interfere(rstate, range, lr)) {
13383 continue;
13384 }
13385 range->color = lr->color;
13386 }
13387 }
13388 }
13389 /* If I don't interfere with a rhs node reuse it's color */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013390 lrd = live_range_head(state, range, 0);
13391 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
13392 expr = triple_rhs(state, lrd->def, 0);
13393 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013394 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000013395 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013396 if (!*expr) {
13397 continue;
13398 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013399 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000013400 if (lr->color == REG_UNSET) {
13401 continue;
13402 }
13403 regcm = lr->classes;
13404 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000013405 (used[lr->color])) {
13406 continue;
13407 }
13408 if (interfere(rstate, range, lr)) {
13409 continue;
13410 }
13411 range->color = lr->color;
13412 break;
13413 }
13414 }
13415 /* If I have not opportunitically picked a useful color
13416 * pick the first color that is free.
13417 */
13418 if (range->color == REG_UNSET) {
13419 range->color =
13420 arch_select_free_register(state, used, range->classes);
13421 }
13422 if (range->color == REG_UNSET) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000013423 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013424 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013425 if (split_ranges(state, rstate, used, range)) {
13426 return 0;
13427 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013428 for(edge = range->edges; edge; edge = edge->next) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000013429 warning(state, edge->node->defs->def, "edge reg %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000013430 arch_reg_str(edge->node->color));
Eric Biedermand3283ec2003-06-18 11:03:18 +000013431 lrd = edge->node->defs;
13432 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000013433 warning(state, lrd->def, " %s %p",
13434 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000013435 lrd = lrd->next;
13436 } while(lrd != edge->node->defs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013437 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000013438 warning(state, range->defs->def, "range: ");
13439 lrd = range->defs;
13440 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000013441 warning(state, lrd->def, " %s %p",
13442 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000013443 lrd = lrd->next;
13444 } while(lrd != range->defs);
13445
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013446 warning(state, range->defs->def, "classes: %x",
Eric Biedermanb138ac82003-04-22 18:44:01 +000013447 range->classes);
13448 for(i = 0; i < MAX_REGISTERS; i++) {
13449 if (used[i]) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013450 warning(state, range->defs->def, "used: %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000013451 arch_reg_str(i));
13452 }
13453 }
13454#if DEBUG_COLOR_GRAPH < 2
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013455 error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013456#else
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013457 internal_error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013458#endif
13459 }
Eric Biederman530b5192003-07-01 10:05:30 +000013460 range->classes &= arch_reg_regcm(state, range->color);
13461 if ((range->color == REG_UNSET) || (range->classes == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013462 internal_error(state, range->defs->def, "select_free_color did not?");
13463 }
13464 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013465}
13466
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013467static int color_graph(struct compile_state *state, struct reg_state *rstate)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013468{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013469 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013470 struct live_range_edge *edge;
13471 struct live_range *range;
13472 if (rstate->low) {
13473 cgdebug_printf("Lo: ");
13474 range = rstate->low;
13475 if (*range->group_prev != range) {
13476 internal_error(state, 0, "lo: *prev != range?");
13477 }
13478 *range->group_prev = range->group_next;
13479 if (range->group_next) {
13480 range->group_next->group_prev = range->group_prev;
13481 }
13482 if (&range->group_next == rstate->low_tail) {
13483 rstate->low_tail = range->group_prev;
13484 }
13485 if (rstate->low == range) {
13486 internal_error(state, 0, "low: next != prev?");
13487 }
13488 }
13489 else if (rstate->high) {
13490 cgdebug_printf("Hi: ");
13491 range = rstate->high;
13492 if (*range->group_prev != range) {
13493 internal_error(state, 0, "hi: *prev != range?");
13494 }
13495 *range->group_prev = range->group_next;
13496 if (range->group_next) {
13497 range->group_next->group_prev = range->group_prev;
13498 }
13499 if (&range->group_next == rstate->high_tail) {
13500 rstate->high_tail = range->group_prev;
13501 }
13502 if (rstate->high == range) {
13503 internal_error(state, 0, "high: next != prev?");
13504 }
13505 }
13506 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013507 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013508 }
13509 cgdebug_printf(" %d\n", range - rstate->lr);
13510 range->group_prev = 0;
13511 for(edge = range->edges; edge; edge = edge->next) {
13512 struct live_range *node;
13513 node = edge->node;
13514 /* Move nodes from the high to the low list */
13515 if (node->group_prev && (node->color == REG_UNSET) &&
13516 (node->degree == regc_max_size(state, node->classes))) {
13517 if (*node->group_prev != node) {
13518 internal_error(state, 0, "move: *prev != node?");
13519 }
13520 *node->group_prev = node->group_next;
13521 if (node->group_next) {
13522 node->group_next->group_prev = node->group_prev;
13523 }
13524 if (&node->group_next == rstate->high_tail) {
13525 rstate->high_tail = node->group_prev;
13526 }
13527 cgdebug_printf("Moving...%d to low\n", node - rstate->lr);
13528 node->group_prev = rstate->low_tail;
13529 node->group_next = 0;
13530 *rstate->low_tail = node;
13531 rstate->low_tail = &node->group_next;
13532 if (*node->group_prev != node) {
13533 internal_error(state, 0, "move2: *prev != node?");
13534 }
13535 }
13536 node->degree -= 1;
13537 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013538 colored = color_graph(state, rstate);
13539 if (colored) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000013540 cgdebug_printf("Coloring %d @", range - rstate->lr);
13541 cgdebug_loc(state, range->defs->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013542 cgdebug_flush();
13543 colored = select_free_color(state, rstate, range);
13544 cgdebug_printf(" %s\n", arch_reg_str(range->color));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013545 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013546 return colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013547}
13548
Eric Biedermana96d6a92003-05-13 20:45:19 +000013549static void verify_colors(struct compile_state *state, struct reg_state *rstate)
13550{
13551 struct live_range *lr;
13552 struct live_range_edge *edge;
13553 struct triple *ins, *first;
13554 char used[MAX_REGISTERS];
13555 first = RHS(state->main_function, 0);
13556 ins = first;
13557 do {
13558 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013559 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000013560 internal_error(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013561 "triple without a live range def");
Eric Biedermana96d6a92003-05-13 20:45:19 +000013562 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013563 lr = rstate->lrd[ins->id].lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000013564 if (lr->color == REG_UNSET) {
13565 internal_error(state, ins,
13566 "triple without a color");
13567 }
13568 /* Find the registers used by the edges */
13569 memset(used, 0, sizeof(used));
13570 for(edge = lr->edges; edge; edge = edge->next) {
13571 if (edge->node->color == REG_UNSET) {
13572 internal_error(state, 0,
13573 "live range without a color");
13574 }
13575 reg_fill_used(state, used, edge->node->color);
13576 }
13577 if (used[lr->color]) {
13578 internal_error(state, ins,
13579 "triple with already used color");
13580 }
13581 }
13582 ins = ins->next;
13583 } while(ins != first);
13584}
13585
Eric Biedermanb138ac82003-04-22 18:44:01 +000013586static void color_triples(struct compile_state *state, struct reg_state *rstate)
13587{
13588 struct live_range *lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000013589 struct triple *first, *ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013590 first = RHS(state->main_function, 0);
Eric Biedermana96d6a92003-05-13 20:45:19 +000013591 ins = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013592 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013593 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000013594 internal_error(state, ins,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013595 "triple without a live range");
13596 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013597 lr = rstate->lrd[ins->id].lr;
13598 SET_REG(ins->id, lr->color);
Eric Biedermana96d6a92003-05-13 20:45:19 +000013599 ins = ins->next;
13600 } while (ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013601}
13602
Eric Biedermanb138ac82003-04-22 18:44:01 +000013603static struct live_range *merge_sort_lr(
13604 struct live_range *first, struct live_range *last)
13605{
13606 struct live_range *mid, *join, **join_tail, *pick;
13607 size_t size;
13608 size = (last - first) + 1;
13609 if (size >= 2) {
13610 mid = first + size/2;
13611 first = merge_sort_lr(first, mid -1);
13612 mid = merge_sort_lr(mid, last);
13613
13614 join = 0;
13615 join_tail = &join;
13616 /* merge the two lists */
13617 while(first && mid) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013618 if ((first->degree < mid->degree) ||
13619 ((first->degree == mid->degree) &&
13620 (first->length < mid->length))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013621 pick = first;
13622 first = first->group_next;
13623 if (first) {
13624 first->group_prev = 0;
13625 }
13626 }
13627 else {
13628 pick = mid;
13629 mid = mid->group_next;
13630 if (mid) {
13631 mid->group_prev = 0;
13632 }
13633 }
13634 pick->group_next = 0;
13635 pick->group_prev = join_tail;
13636 *join_tail = pick;
13637 join_tail = &pick->group_next;
13638 }
13639 /* Splice the remaining list */
13640 pick = (first)? first : mid;
13641 *join_tail = pick;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013642 if (pick) {
13643 pick->group_prev = join_tail;
13644 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013645 }
13646 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013647 if (!first->defs) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013648 first = 0;
13649 }
13650 join = first;
13651 }
13652 return join;
13653}
13654
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013655static void ids_from_rstate(struct compile_state *state,
13656 struct reg_state *rstate)
13657{
13658 struct triple *ins, *first;
13659 if (!rstate->defs) {
13660 return;
13661 }
13662 /* Display the graph if desired */
13663 if (state->debug & DEBUG_INTERFERENCE) {
13664 print_blocks(state, stdout);
13665 print_control_flow(state);
13666 }
13667 first = RHS(state->main_function, 0);
13668 ins = first;
13669 do {
13670 if (ins->id) {
13671 struct live_range_def *lrd;
13672 lrd = &rstate->lrd[ins->id];
13673 ins->id = lrd->orig_id;
13674 }
13675 ins = ins->next;
13676 } while(ins != first);
13677}
13678
13679static void cleanup_live_edges(struct reg_state *rstate)
13680{
13681 int i;
13682 /* Free the edges on each node */
13683 for(i = 1; i <= rstate->ranges; i++) {
13684 remove_live_edges(rstate, &rstate->lr[i]);
13685 }
13686}
13687
13688static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
13689{
13690 cleanup_live_edges(rstate);
13691 xfree(rstate->lrd);
13692 xfree(rstate->lr);
13693
13694 /* Free the variable lifetime information */
13695 if (rstate->blocks) {
13696 free_variable_lifetimes(state, rstate->blocks);
13697 }
13698 rstate->defs = 0;
13699 rstate->ranges = 0;
13700 rstate->lrd = 0;
13701 rstate->lr = 0;
13702 rstate->blocks = 0;
13703}
13704
Eric Biederman153ea352003-06-20 14:43:20 +000013705static void verify_consistency(struct compile_state *state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013706static void allocate_registers(struct compile_state *state)
13707{
13708 struct reg_state rstate;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013709 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013710
13711 /* Clear out the reg_state */
13712 memset(&rstate, 0, sizeof(rstate));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013713 rstate.max_passes = MAX_ALLOCATION_PASSES;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013714
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013715 do {
13716 struct live_range **point, **next;
Eric Biedermand1ea5392003-06-28 06:49:45 +000013717 int conflicts;
Eric Biederman153ea352003-06-20 14:43:20 +000013718 int tangles;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013719 int coalesced;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013720
Eric Biedermand1ea5392003-06-28 06:49:45 +000013721#if DEBUG_RANGE_CONFLICTS
Eric Biederman153ea352003-06-20 14:43:20 +000013722 fprintf(stderr, "pass: %d\n", rstate.passes);
13723#endif
13724
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013725 /* Restore ids */
13726 ids_from_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013727
Eric Biedermanf96a8102003-06-16 16:57:34 +000013728 /* Cleanup the temporary data structures */
13729 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013730
Eric Biedermanf96a8102003-06-16 16:57:34 +000013731 /* Compute the variable lifetimes */
13732 rstate.blocks = compute_variable_lifetimes(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013733
Eric Biedermanf96a8102003-06-16 16:57:34 +000013734 /* Fix invalid mandatory live range coalesce conflicts */
Eric Biedermand1ea5392003-06-28 06:49:45 +000013735 conflicts = correct_coalesce_conflicts(state, rstate.blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013736
Eric Biederman153ea352003-06-20 14:43:20 +000013737 /* Fix two simultaneous uses of the same register.
13738 * In a few pathlogical cases a partial untangle moves
13739 * the tangle to a part of the graph we won't revisit.
13740 * So we keep looping until we have no more tangle fixes
13741 * to apply.
13742 */
13743 do {
13744 tangles = correct_tangles(state, rstate.blocks);
13745 } while(tangles);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013746
13747 if (state->debug & DEBUG_INSERTED_COPIES) {
13748 printf("After resolve_tangles\n");
13749 print_blocks(state, stdout);
13750 print_control_flow(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013751 }
Eric Biederman153ea352003-06-20 14:43:20 +000013752 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013753
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013754 /* Allocate and initialize the live ranges */
13755 initialize_live_ranges(state, &rstate);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013756
Eric Biederman153ea352003-06-20 14:43:20 +000013757 /* Note current doing coalescing in a loop appears to
13758 * buys me nothing. The code is left this way in case
13759 * there is some value in it. Or if a future bugfix
13760 * yields some benefit.
13761 */
13762 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000013763#if DEBUG_COALESCING
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013764 fprintf(stderr, "coalescing\n");
13765#endif
Eric Biederman153ea352003-06-20 14:43:20 +000013766 /* Remove any previous live edge calculations */
13767 cleanup_live_edges(&rstate);
13768
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013769 /* Compute the interference graph */
13770 walk_variable_lifetimes(
13771 state, rstate.blocks, graph_ins, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000013772
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013773 /* Display the interference graph if desired */
13774 if (state->debug & DEBUG_INTERFERENCE) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000013775 print_interference_blocks(state, &rstate, stdout, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013776 printf("\nlive variables by instruction\n");
13777 walk_variable_lifetimes(
13778 state, rstate.blocks,
13779 print_interference_ins, &rstate);
13780 }
13781
13782 coalesced = coalesce_live_ranges(state, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000013783
Eric Biedermand1ea5392003-06-28 06:49:45 +000013784#if DEBUG_COALESCING
Eric Biederman153ea352003-06-20 14:43:20 +000013785 fprintf(stderr, "coalesced: %d\n", coalesced);
13786#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013787 } while(coalesced);
Eric Biederman153ea352003-06-20 14:43:20 +000013788
13789#if DEBUG_CONSISTENCY > 1
13790# if 0
13791 fprintf(stderr, "verify_graph_ins...\n");
13792# endif
13793 /* Verify the interference graph */
13794 walk_variable_lifetimes(
13795 state, rstate.blocks, verify_graph_ins, &rstate);
13796# if 0
13797 fprintf(stderr, "verify_graph_ins done\n");
13798#endif
13799#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013800
13801 /* Build the groups low and high. But with the nodes
13802 * first sorted by degree order.
13803 */
13804 rstate.low_tail = &rstate.low;
13805 rstate.high_tail = &rstate.high;
13806 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
13807 if (rstate.high) {
13808 rstate.high->group_prev = &rstate.high;
13809 }
13810 for(point = &rstate.high; *point; point = &(*point)->group_next)
13811 ;
13812 rstate.high_tail = point;
13813 /* Walk through the high list and move everything that needs
13814 * to be onto low.
13815 */
13816 for(point = &rstate.high; *point; point = next) {
13817 struct live_range *range;
13818 next = &(*point)->group_next;
13819 range = *point;
13820
13821 /* If it has a low degree or it already has a color
13822 * place the node in low.
13823 */
13824 if ((range->degree < regc_max_size(state, range->classes)) ||
13825 (range->color != REG_UNSET)) {
13826 cgdebug_printf("Lo: %5d degree %5d%s\n",
13827 range - rstate.lr, range->degree,
13828 (range->color != REG_UNSET) ? " (colored)": "");
13829 *range->group_prev = range->group_next;
13830 if (range->group_next) {
13831 range->group_next->group_prev = range->group_prev;
13832 }
13833 if (&range->group_next == rstate.high_tail) {
13834 rstate.high_tail = range->group_prev;
13835 }
13836 range->group_prev = rstate.low_tail;
13837 range->group_next = 0;
13838 *rstate.low_tail = range;
13839 rstate.low_tail = &range->group_next;
13840 next = point;
13841 }
13842 else {
13843 cgdebug_printf("hi: %5d degree %5d%s\n",
13844 range - rstate.lr, range->degree,
13845 (range->color != REG_UNSET) ? " (colored)": "");
13846 }
13847 }
13848 /* Color the live_ranges */
13849 colored = color_graph(state, &rstate);
13850 rstate.passes++;
13851 } while (!colored);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013852
Eric Biedermana96d6a92003-05-13 20:45:19 +000013853 /* Verify the graph was properly colored */
13854 verify_colors(state, &rstate);
13855
Eric Biedermanb138ac82003-04-22 18:44:01 +000013856 /* Move the colors from the graph to the triples */
13857 color_triples(state, &rstate);
13858
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013859 /* Cleanup the temporary data structures */
13860 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013861}
13862
13863/* Sparce Conditional Constant Propogation
13864 * =========================================
13865 */
13866struct ssa_edge;
13867struct flow_block;
13868struct lattice_node {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013869 unsigned old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013870 struct triple *def;
13871 struct ssa_edge *out;
13872 struct flow_block *fblock;
13873 struct triple *val;
13874 /* lattice high val && !is_const(val)
13875 * lattice const is_const(val)
13876 * lattice low val == 0
13877 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000013878};
13879struct ssa_edge {
13880 struct lattice_node *src;
13881 struct lattice_node *dst;
13882 struct ssa_edge *work_next;
13883 struct ssa_edge *work_prev;
13884 struct ssa_edge *out_next;
13885};
13886struct flow_edge {
13887 struct flow_block *src;
13888 struct flow_block *dst;
13889 struct flow_edge *work_next;
13890 struct flow_edge *work_prev;
13891 struct flow_edge *in_next;
13892 struct flow_edge *out_next;
13893 int executable;
13894};
13895struct flow_block {
13896 struct block *block;
13897 struct flow_edge *in;
13898 struct flow_edge *out;
13899 struct flow_edge left, right;
13900};
13901
13902struct scc_state {
Eric Biederman0babc1c2003-05-09 02:39:00 +000013903 int ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013904 struct lattice_node *lattice;
13905 struct ssa_edge *ssa_edges;
13906 struct flow_block *flow_blocks;
13907 struct flow_edge *flow_work_list;
13908 struct ssa_edge *ssa_work_list;
13909};
13910
13911
13912static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
13913 struct flow_edge *fedge)
13914{
13915 if (!scc->flow_work_list) {
13916 scc->flow_work_list = fedge;
13917 fedge->work_next = fedge->work_prev = fedge;
13918 }
13919 else {
13920 struct flow_edge *ftail;
13921 ftail = scc->flow_work_list->work_prev;
13922 fedge->work_next = ftail->work_next;
13923 fedge->work_prev = ftail;
13924 fedge->work_next->work_prev = fedge;
13925 fedge->work_prev->work_next = fedge;
13926 }
13927}
13928
13929static struct flow_edge *scc_next_fedge(
13930 struct compile_state *state, struct scc_state *scc)
13931{
13932 struct flow_edge *fedge;
13933 fedge = scc->flow_work_list;
13934 if (fedge) {
13935 fedge->work_next->work_prev = fedge->work_prev;
13936 fedge->work_prev->work_next = fedge->work_next;
13937 if (fedge->work_next != fedge) {
13938 scc->flow_work_list = fedge->work_next;
13939 } else {
13940 scc->flow_work_list = 0;
13941 }
13942 }
13943 return fedge;
13944}
13945
13946static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
13947 struct ssa_edge *sedge)
13948{
13949 if (!scc->ssa_work_list) {
13950 scc->ssa_work_list = sedge;
13951 sedge->work_next = sedge->work_prev = sedge;
13952 }
13953 else {
13954 struct ssa_edge *stail;
13955 stail = scc->ssa_work_list->work_prev;
13956 sedge->work_next = stail->work_next;
13957 sedge->work_prev = stail;
13958 sedge->work_next->work_prev = sedge;
13959 sedge->work_prev->work_next = sedge;
13960 }
13961}
13962
13963static struct ssa_edge *scc_next_sedge(
13964 struct compile_state *state, struct scc_state *scc)
13965{
13966 struct ssa_edge *sedge;
13967 sedge = scc->ssa_work_list;
13968 if (sedge) {
13969 sedge->work_next->work_prev = sedge->work_prev;
13970 sedge->work_prev->work_next = sedge->work_next;
13971 if (sedge->work_next != sedge) {
13972 scc->ssa_work_list = sedge->work_next;
13973 } else {
13974 scc->ssa_work_list = 0;
13975 }
13976 }
13977 return sedge;
13978}
13979
13980static void initialize_scc_state(
13981 struct compile_state *state, struct scc_state *scc)
13982{
13983 int ins_count, ssa_edge_count;
13984 int ins_index, ssa_edge_index, fblock_index;
13985 struct triple *first, *ins;
13986 struct block *block;
13987 struct flow_block *fblock;
13988
13989 memset(scc, 0, sizeof(*scc));
13990
13991 /* Inialize pass zero find out how much memory we need */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013992 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013993 ins = first;
13994 ins_count = ssa_edge_count = 0;
13995 do {
13996 struct triple_set *edge;
13997 ins_count += 1;
13998 for(edge = ins->use; edge; edge = edge->next) {
13999 ssa_edge_count++;
14000 }
14001 ins = ins->next;
14002 } while(ins != first);
14003#if DEBUG_SCC
14004 fprintf(stderr, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
14005 ins_count, ssa_edge_count, state->last_vertex);
14006#endif
Eric Biederman0babc1c2003-05-09 02:39:00 +000014007 scc->ins_count = ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014008 scc->lattice =
14009 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
14010 scc->ssa_edges =
14011 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
14012 scc->flow_blocks =
14013 xcmalloc(sizeof(*scc->flow_blocks)*(state->last_vertex + 1),
14014 "flow_blocks");
14015
14016 /* Initialize pass one collect up the nodes */
14017 fblock = 0;
14018 block = 0;
14019 ins_index = ssa_edge_index = fblock_index = 0;
14020 ins = first;
14021 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014022 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
14023 block = ins->u.block;
14024 if (!block) {
14025 internal_error(state, ins, "label without block");
14026 }
14027 fblock_index += 1;
14028 block->vertex = fblock_index;
14029 fblock = &scc->flow_blocks[fblock_index];
14030 fblock->block = block;
14031 }
14032 {
14033 struct lattice_node *lnode;
14034 ins_index += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014035 lnode = &scc->lattice[ins_index];
14036 lnode->def = ins;
14037 lnode->out = 0;
14038 lnode->fblock = fblock;
14039 lnode->val = ins; /* LATTICE HIGH */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014040 lnode->old_id = ins->id;
14041 ins->id = ins_index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014042 }
14043 ins = ins->next;
14044 } while(ins != first);
14045 /* Initialize pass two collect up the edges */
14046 block = 0;
14047 fblock = 0;
14048 ins = first;
14049 do {
14050 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
14051 struct flow_edge *fedge, **ftail;
14052 struct block_set *bedge;
14053 block = ins->u.block;
14054 fblock = &scc->flow_blocks[block->vertex];
14055 fblock->in = 0;
14056 fblock->out = 0;
14057 ftail = &fblock->out;
14058 if (block->left) {
14059 fblock->left.dst = &scc->flow_blocks[block->left->vertex];
14060 if (fblock->left.dst->block != block->left) {
14061 internal_error(state, 0, "block mismatch");
14062 }
14063 fblock->left.out_next = 0;
14064 *ftail = &fblock->left;
14065 ftail = &fblock->left.out_next;
14066 }
14067 if (block->right) {
14068 fblock->right.dst = &scc->flow_blocks[block->right->vertex];
14069 if (fblock->right.dst->block != block->right) {
14070 internal_error(state, 0, "block mismatch");
14071 }
14072 fblock->right.out_next = 0;
14073 *ftail = &fblock->right;
14074 ftail = &fblock->right.out_next;
14075 }
14076 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
14077 fedge->src = fblock;
14078 fedge->work_next = fedge->work_prev = fedge;
14079 fedge->executable = 0;
14080 }
14081 ftail = &fblock->in;
14082 for(bedge = block->use; bedge; bedge = bedge->next) {
14083 struct block *src_block;
14084 struct flow_block *sfblock;
14085 struct flow_edge *sfedge;
14086 src_block = bedge->member;
14087 sfblock = &scc->flow_blocks[src_block->vertex];
14088 sfedge = 0;
14089 if (src_block->left == block) {
14090 sfedge = &sfblock->left;
14091 } else {
14092 sfedge = &sfblock->right;
14093 }
14094 *ftail = sfedge;
14095 ftail = &sfedge->in_next;
14096 sfedge->in_next = 0;
14097 }
14098 }
14099 {
14100 struct triple_set *edge;
14101 struct ssa_edge **stail;
14102 struct lattice_node *lnode;
14103 lnode = &scc->lattice[ins->id];
14104 lnode->out = 0;
14105 stail = &lnode->out;
14106 for(edge = ins->use; edge; edge = edge->next) {
14107 struct ssa_edge *sedge;
14108 ssa_edge_index += 1;
14109 sedge = &scc->ssa_edges[ssa_edge_index];
14110 *stail = sedge;
14111 stail = &sedge->out_next;
14112 sedge->src = lnode;
14113 sedge->dst = &scc->lattice[edge->member->id];
14114 sedge->work_next = sedge->work_prev = sedge;
14115 sedge->out_next = 0;
14116 }
14117 }
14118 ins = ins->next;
14119 } while(ins != first);
14120 /* Setup a dummy block 0 as a node above the start node */
14121 {
14122 struct flow_block *fblock, *dst;
14123 struct flow_edge *fedge;
14124 fblock = &scc->flow_blocks[0];
14125 fblock->block = 0;
14126 fblock->in = 0;
14127 fblock->out = &fblock->left;
14128 dst = &scc->flow_blocks[state->first_block->vertex];
14129 fedge = &fblock->left;
14130 fedge->src = fblock;
14131 fedge->dst = dst;
14132 fedge->work_next = fedge;
14133 fedge->work_prev = fedge;
14134 fedge->in_next = fedge->dst->in;
14135 fedge->out_next = 0;
14136 fedge->executable = 0;
14137 fedge->dst->in = fedge;
14138
14139 /* Initialize the work lists */
14140 scc->flow_work_list = 0;
14141 scc->ssa_work_list = 0;
14142 scc_add_fedge(state, scc, fedge);
14143 }
14144#if DEBUG_SCC
14145 fprintf(stderr, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
14146 ins_index, ssa_edge_index, fblock_index);
14147#endif
14148}
14149
14150
14151static void free_scc_state(
14152 struct compile_state *state, struct scc_state *scc)
14153{
14154 xfree(scc->flow_blocks);
14155 xfree(scc->ssa_edges);
14156 xfree(scc->lattice);
Eric Biederman0babc1c2003-05-09 02:39:00 +000014157
Eric Biedermanb138ac82003-04-22 18:44:01 +000014158}
14159
14160static struct lattice_node *triple_to_lattice(
14161 struct compile_state *state, struct scc_state *scc, struct triple *ins)
14162{
14163 if (ins->id <= 0) {
14164 internal_error(state, ins, "bad id");
14165 }
14166 return &scc->lattice[ins->id];
14167}
14168
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014169static struct triple *preserve_lval(
14170 struct compile_state *state, struct lattice_node *lnode)
14171{
14172 struct triple *old;
14173 /* Preserve the original value */
14174 if (lnode->val) {
14175 old = dup_triple(state, lnode->val);
14176 if (lnode->val != lnode->def) {
14177 xfree(lnode->val);
14178 }
14179 lnode->val = 0;
14180 } else {
14181 old = 0;
14182 }
14183 return old;
14184}
14185
14186static int lval_changed(struct compile_state *state,
14187 struct triple *old, struct lattice_node *lnode)
14188{
14189 int changed;
14190 /* See if the lattice value has changed */
14191 changed = 1;
14192 if (!old && !lnode->val) {
14193 changed = 0;
14194 }
14195 if (changed && lnode->val && !is_const(lnode->val)) {
14196 changed = 0;
14197 }
14198 if (changed &&
14199 lnode->val && old &&
14200 (memcmp(lnode->val->param, old->param,
14201 TRIPLE_SIZE(lnode->val->sizes) * sizeof(lnode->val->param[0])) == 0) &&
14202 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
14203 changed = 0;
14204 }
14205 if (old) {
14206 xfree(old);
14207 }
14208 return changed;
14209
14210}
14211
Eric Biedermanb138ac82003-04-22 18:44:01 +000014212static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
14213 struct lattice_node *lnode)
14214{
14215 struct lattice_node *tmp;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014216 struct triple **slot, *old;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014217 struct flow_edge *fedge;
14218 int index;
14219 if (lnode->def->op != OP_PHI) {
14220 internal_error(state, lnode->def, "not phi");
14221 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014222 /* Store the original value */
14223 old = preserve_lval(state, lnode);
14224
Eric Biedermanb138ac82003-04-22 18:44:01 +000014225 /* default to lattice high */
14226 lnode->val = lnode->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014227 slot = &RHS(lnode->def, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014228 index = 0;
14229 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
14230 if (!fedge->executable) {
14231 continue;
14232 }
14233 if (!slot[index]) {
14234 internal_error(state, lnode->def, "no phi value");
14235 }
14236 tmp = triple_to_lattice(state, scc, slot[index]);
14237 /* meet(X, lattice low) = lattice low */
14238 if (!tmp->val) {
14239 lnode->val = 0;
14240 }
14241 /* meet(X, lattice high) = X */
14242 else if (!tmp->val) {
14243 lnode->val = lnode->val;
14244 }
14245 /* meet(lattice high, X) = X */
14246 else if (!is_const(lnode->val)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014247 lnode->val = dup_triple(state, tmp->val);
14248 lnode->val->type = lnode->def->type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014249 }
14250 /* meet(const, const) = const or lattice low */
14251 else if (!constants_equal(state, lnode->val, tmp->val)) {
14252 lnode->val = 0;
14253 }
14254 if (!lnode->val) {
14255 break;
14256 }
14257 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014258#if DEBUG_SCC
14259 fprintf(stderr, "phi: %d -> %s\n",
14260 lnode->def->id,
14261 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14262#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014263 /* If the lattice value has changed update the work lists. */
14264 if (lval_changed(state, old, lnode)) {
14265 struct ssa_edge *sedge;
14266 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14267 scc_add_sedge(state, scc, sedge);
14268 }
14269 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014270}
14271
14272static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
14273 struct lattice_node *lnode)
14274{
14275 int changed;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014276 struct triple *old, *scratch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014277 struct triple **dexpr, **vexpr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014278 int count, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014279
14280 /* Store the original value */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014281 old = preserve_lval(state, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000014282
Eric Biedermanb138ac82003-04-22 18:44:01 +000014283 /* Reinitialize the value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000014284 lnode->val = scratch = dup_triple(state, lnode->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014285 scratch->id = lnode->old_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014286 scratch->next = scratch;
14287 scratch->prev = scratch;
14288 scratch->use = 0;
14289
14290 count = TRIPLE_SIZE(scratch->sizes);
14291 for(i = 0; i < count; i++) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000014292 dexpr = &lnode->def->param[i];
14293 vexpr = &scratch->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014294 *vexpr = *dexpr;
14295 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14296 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14297 *dexpr) {
14298 struct lattice_node *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014299 tmp = triple_to_lattice(state, scc, *dexpr);
14300 *vexpr = (tmp->val)? tmp->val : tmp->def;
14301 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014302 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014303 if (scratch->op == OP_BRANCH) {
14304 scratch->next = lnode->def->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014305 }
14306 /* Recompute the value */
14307#warning "FIXME see if simplify does anything bad"
14308 /* So far it looks like only the strength reduction
14309 * optimization are things I need to worry about.
14310 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000014311 simplify(state, scratch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014312 /* Cleanup my value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000014313 if (scratch->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014314 internal_error(state, lnode->def, "scratch used?");
14315 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014316 if ((scratch->prev != scratch) ||
14317 ((scratch->next != scratch) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000014318 ((lnode->def->op != OP_BRANCH) ||
Eric Biederman0babc1c2003-05-09 02:39:00 +000014319 (scratch->next != lnode->def->next)))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014320 internal_error(state, lnode->def, "scratch in list?");
14321 }
14322 /* undo any uses... */
Eric Biederman0babc1c2003-05-09 02:39:00 +000014323 count = TRIPLE_SIZE(scratch->sizes);
14324 for(i = 0; i < count; i++) {
14325 vexpr = &scratch->param[i];
14326 if (*vexpr) {
14327 unuse_triple(*vexpr, scratch);
14328 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014329 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014330 if (!is_const(scratch)) {
14331 for(i = 0; i < count; i++) {
14332 dexpr = &lnode->def->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014333 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14334 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14335 *dexpr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000014336 struct lattice_node *tmp;
14337 tmp = triple_to_lattice(state, scc, *dexpr);
14338 if (!tmp->val) {
14339 lnode->val = 0;
14340 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014341 }
14342 }
14343 }
14344 if (lnode->val &&
14345 (lnode->val->op == lnode->def->op) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000014346 (memcmp(lnode->val->param, lnode->def->param,
14347 count * sizeof(lnode->val->param[0])) == 0) &&
14348 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014349 lnode->val = lnode->def;
14350 }
14351 /* Find the cases that are always lattice lo */
14352 if (lnode->val &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000014353 triple_is_def(state, lnode->val) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000014354 !triple_is_pure(state, lnode->val)) {
14355 lnode->val = 0;
14356 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014357 if (lnode->val &&
14358 (lnode->val->op == OP_SDECL) &&
14359 (lnode->val != lnode->def)) {
14360 internal_error(state, lnode->def, "bad sdecl");
14361 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014362 /* See if the lattice value has changed */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014363 changed = lval_changed(state, old, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000014364 if (lnode->val != scratch) {
14365 xfree(scratch);
14366 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014367 return changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014368}
Eric Biederman0babc1c2003-05-09 02:39:00 +000014369
Eric Biedermanb138ac82003-04-22 18:44:01 +000014370static void scc_visit_branch(struct compile_state *state, struct scc_state *scc,
14371 struct lattice_node *lnode)
14372{
14373 struct lattice_node *cond;
14374#if DEBUG_SCC
14375 {
14376 struct flow_edge *fedge;
14377 fprintf(stderr, "branch: %d (",
14378 lnode->def->id);
14379
14380 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
14381 fprintf(stderr, " %d", fedge->dst->block->vertex);
14382 }
14383 fprintf(stderr, " )");
Eric Biederman0babc1c2003-05-09 02:39:00 +000014384 if (TRIPLE_RHS(lnode->def->sizes) > 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014385 fprintf(stderr, " <- %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014386 RHS(lnode->def, 0)->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014387 }
14388 fprintf(stderr, "\n");
14389 }
14390#endif
14391 if (lnode->def->op != OP_BRANCH) {
14392 internal_error(state, lnode->def, "not branch");
14393 }
14394 /* This only applies to conditional branches */
Eric Biederman0babc1c2003-05-09 02:39:00 +000014395 if (TRIPLE_RHS(lnode->def->sizes) == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014396 return;
14397 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014398 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000014399 if (cond->val && !is_const(cond->val)) {
14400#warning "FIXME do I need to do something here?"
14401 warning(state, cond->def, "condition not constant?");
14402 return;
14403 }
14404 if (cond->val == 0) {
14405 scc_add_fedge(state, scc, cond->fblock->out);
14406 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14407 }
14408 else if (cond->val->u.cval) {
14409 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14410
14411 } else {
14412 scc_add_fedge(state, scc, cond->fblock->out);
14413 }
14414
14415}
14416
14417static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
14418 struct lattice_node *lnode)
14419{
14420 int changed;
14421
14422 changed = compute_lnode_val(state, scc, lnode);
14423#if DEBUG_SCC
14424 {
14425 struct triple **expr;
14426 fprintf(stderr, "expr: %3d %10s (",
14427 lnode->def->id, tops(lnode->def->op));
14428 expr = triple_rhs(state, lnode->def, 0);
14429 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000014430 if (*expr) {
14431 fprintf(stderr, " %d", (*expr)->id);
14432 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014433 }
14434 fprintf(stderr, " ) -> %s\n",
14435 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14436 }
14437#endif
14438 if (lnode->def->op == OP_BRANCH) {
14439 scc_visit_branch(state, scc, lnode);
14440
14441 }
14442 else if (changed) {
14443 struct ssa_edge *sedge;
14444 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14445 scc_add_sedge(state, scc, sedge);
14446 }
14447 }
14448}
14449
14450static void scc_writeback_values(
14451 struct compile_state *state, struct scc_state *scc)
14452{
14453 struct triple *first, *ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014454 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014455 ins = first;
14456 do {
14457 struct lattice_node *lnode;
14458 lnode = triple_to_lattice(state, scc, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014459 /* Restore id */
14460 ins->id = lnode->old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014461#if DEBUG_SCC
14462 if (lnode->val && !is_const(lnode->val)) {
14463 warning(state, lnode->def,
14464 "lattice node still high?");
14465 }
14466#endif
14467 if (lnode->val && (lnode->val != ins)) {
14468 /* See if it something I know how to write back */
14469 switch(lnode->val->op) {
14470 case OP_INTCONST:
14471 mkconst(state, ins, lnode->val->u.cval);
14472 break;
14473 case OP_ADDRCONST:
14474 mkaddr_const(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014475 MISC(lnode->val, 0), lnode->val->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014476 break;
14477 default:
14478 /* By default don't copy the changes,
14479 * recompute them in place instead.
14480 */
14481 simplify(state, ins);
14482 break;
14483 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014484 if (is_const(lnode->val) &&
14485 !constants_equal(state, lnode->val, ins)) {
14486 internal_error(state, 0, "constants not equal");
14487 }
14488 /* Free the lattice nodes */
14489 xfree(lnode->val);
14490 lnode->val = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014491 }
14492 ins = ins->next;
14493 } while(ins != first);
14494}
14495
14496static void scc_transform(struct compile_state *state)
14497{
14498 struct scc_state scc;
14499
14500 initialize_scc_state(state, &scc);
14501
14502 while(scc.flow_work_list || scc.ssa_work_list) {
14503 struct flow_edge *fedge;
14504 struct ssa_edge *sedge;
14505 struct flow_edge *fptr;
14506 while((fedge = scc_next_fedge(state, &scc))) {
14507 struct block *block;
14508 struct triple *ptr;
14509 struct flow_block *fblock;
14510 int time;
14511 int done;
14512 if (fedge->executable) {
14513 continue;
14514 }
14515 if (!fedge->dst) {
14516 internal_error(state, 0, "fedge without dst");
14517 }
14518 if (!fedge->src) {
14519 internal_error(state, 0, "fedge without src");
14520 }
14521 fedge->executable = 1;
14522 fblock = fedge->dst;
14523 block = fblock->block;
14524 time = 0;
14525 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14526 if (fptr->executable) {
14527 time++;
14528 }
14529 }
14530#if DEBUG_SCC
14531 fprintf(stderr, "vertex: %d time: %d\n",
14532 block->vertex, time);
14533
14534#endif
14535 done = 0;
14536 for(ptr = block->first; !done; ptr = ptr->next) {
14537 struct lattice_node *lnode;
14538 done = (ptr == block->last);
14539 lnode = &scc.lattice[ptr->id];
14540 if (ptr->op == OP_PHI) {
14541 scc_visit_phi(state, &scc, lnode);
14542 }
14543 else if (time == 1) {
14544 scc_visit_expr(state, &scc, lnode);
14545 }
14546 }
14547 if (fblock->out && !fblock->out->out_next) {
14548 scc_add_fedge(state, &scc, fblock->out);
14549 }
14550 }
14551 while((sedge = scc_next_sedge(state, &scc))) {
14552 struct lattice_node *lnode;
14553 struct flow_block *fblock;
14554 lnode = sedge->dst;
14555 fblock = lnode->fblock;
14556#if DEBUG_SCC
14557 fprintf(stderr, "sedge: %5d (%5d -> %5d)\n",
14558 sedge - scc.ssa_edges,
14559 sedge->src->def->id,
14560 sedge->dst->def->id);
14561#endif
14562 if (lnode->def->op == OP_PHI) {
14563 scc_visit_phi(state, &scc, lnode);
14564 }
14565 else {
14566 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14567 if (fptr->executable) {
14568 break;
14569 }
14570 }
14571 if (fptr) {
14572 scc_visit_expr(state, &scc, lnode);
14573 }
14574 }
14575 }
14576 }
14577
14578 scc_writeback_values(state, &scc);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014579 free_scc_state(state, &scc);
14580}
14581
14582
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014583static void transform_to_arch_instructions(struct compile_state *state)
14584{
14585 struct triple *ins, *first;
14586 first = RHS(state->main_function, 0);
14587 ins = first;
14588 do {
14589 ins = transform_to_arch_instruction(state, ins);
14590 } while(ins != first);
14591}
Eric Biedermanb138ac82003-04-22 18:44:01 +000014592
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014593#if DEBUG_CONSISTENCY
14594static void verify_uses(struct compile_state *state)
14595{
14596 struct triple *first, *ins;
14597 struct triple_set *set;
14598 first = RHS(state->main_function, 0);
14599 ins = first;
14600 do {
14601 struct triple **expr;
14602 expr = triple_rhs(state, ins, 0);
14603 for(; expr; expr = triple_rhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000014604 struct triple *rhs;
14605 rhs = *expr;
14606 for(set = rhs?rhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014607 if (set->member == ins) {
14608 break;
14609 }
14610 }
14611 if (!set) {
14612 internal_error(state, ins, "rhs not used");
14613 }
14614 }
14615 expr = triple_lhs(state, ins, 0);
14616 for(; expr; expr = triple_lhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000014617 struct triple *lhs;
14618 lhs = *expr;
14619 for(set = lhs?lhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014620 if (set->member == ins) {
14621 break;
14622 }
14623 }
14624 if (!set) {
14625 internal_error(state, ins, "lhs not used");
14626 }
14627 }
14628 ins = ins->next;
14629 } while(ins != first);
14630
14631}
Eric Biedermand1ea5392003-06-28 06:49:45 +000014632static void verify_blocks_present(struct compile_state *state)
14633{
14634 struct triple *first, *ins;
14635 if (!state->first_block) {
14636 return;
14637 }
14638 first = RHS(state->main_function, 0);
14639 ins = first;
14640 do {
Eric Biederman530b5192003-07-01 10:05:30 +000014641 valid_ins(state, ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000014642 if (triple_stores_block(state, ins)) {
14643 if (!ins->u.block) {
14644 internal_error(state, ins,
14645 "%p not in a block?\n", ins);
14646 }
14647 }
14648 ins = ins->next;
14649 } while(ins != first);
14650
14651
14652}
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014653static void verify_blocks(struct compile_state *state)
14654{
14655 struct triple *ins;
14656 struct block *block;
Eric Biederman530b5192003-07-01 10:05:30 +000014657 int blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014658 block = state->first_block;
14659 if (!block) {
14660 return;
14661 }
Eric Biederman530b5192003-07-01 10:05:30 +000014662 blocks = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014663 do {
Eric Biederman530b5192003-07-01 10:05:30 +000014664 int users;
14665 struct block_set *user;
14666 blocks++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014667 for(ins = block->first; ins != block->last->next; ins = ins->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000014668 if (triple_stores_block(state, ins) && (ins->u.block != block)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014669 internal_error(state, ins, "inconsitent block specified");
14670 }
Eric Biederman530b5192003-07-01 10:05:30 +000014671 valid_ins(state, ins);
14672 }
14673 users = 0;
14674 for(user = block->use; user; user = user->next) {
14675 users++;
14676 if ((block == state->last_block) &&
14677 (user->member == state->first_block)) {
14678 continue;
14679 }
14680 if ((user->member->left != block) &&
14681 (user->member->right != block)) {
14682 internal_error(state, user->member->first,
14683 "user does not use block");
14684 }
14685 }
14686 if (triple_is_branch(state, block->last) &&
14687 (block->right != block_of_triple(state, TARG(block->last, 0))))
14688 {
14689 internal_error(state, block->last, "block->right != TARG(0)");
14690 }
14691 if (!triple_is_uncond_branch(state, block->last) &&
14692 (block != state->last_block) &&
14693 (block->left != block_of_triple(state, block->last->next)))
14694 {
14695 internal_error(state, block->last, "block->left != block->last->next");
14696 }
14697 if (block->left) {
14698 for(user = block->left->use; user; user = user->next) {
14699 if (user->member == block) {
14700 break;
14701 }
14702 }
14703 if (!user || user->member != block) {
14704 internal_error(state, block->first,
14705 "block does not use left");
14706 }
14707 }
14708 if (block->right) {
14709 for(user = block->right->use; user; user = user->next) {
14710 if (user->member == block) {
14711 break;
14712 }
14713 }
14714 if (!user || user->member != block) {
14715 internal_error(state, block->first,
14716 "block does not use right");
14717 }
14718 }
14719 if (block->users != users) {
14720 internal_error(state, block->first,
14721 "computed users %d != stored users %d\n",
14722 users, block->users);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014723 }
14724 if (!triple_stores_block(state, block->last->next)) {
14725 internal_error(state, block->last->next,
14726 "cannot find next block");
14727 }
14728 block = block->last->next->u.block;
14729 if (!block) {
14730 internal_error(state, block->last->next,
14731 "bad next block");
14732 }
14733 } while(block != state->first_block);
Eric Biederman530b5192003-07-01 10:05:30 +000014734 if (blocks != state->last_vertex) {
14735 internal_error(state, 0, "computed blocks != stored blocks %d\n",
14736 blocks, state->last_vertex);
14737 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014738}
14739
14740static void verify_domination(struct compile_state *state)
14741{
14742 struct triple *first, *ins;
14743 struct triple_set *set;
14744 if (!state->first_block) {
14745 return;
14746 }
14747
14748 first = RHS(state->main_function, 0);
14749 ins = first;
14750 do {
14751 for(set = ins->use; set; set = set->next) {
14752 struct triple **expr;
14753 if (set->member->op == OP_PHI) {
14754 continue;
14755 }
14756 /* See if the use is on the righ hand side */
14757 expr = triple_rhs(state, set->member, 0);
14758 for(; expr ; expr = triple_rhs(state, set->member, expr)) {
14759 if (*expr == ins) {
14760 break;
14761 }
14762 }
14763 if (expr &&
14764 !tdominates(state, ins, set->member)) {
14765 internal_error(state, set->member,
14766 "non dominated rhs use?");
14767 }
14768 }
14769 ins = ins->next;
14770 } while(ins != first);
14771}
14772
14773static void verify_piece(struct compile_state *state)
14774{
14775 struct triple *first, *ins;
14776 first = RHS(state->main_function, 0);
14777 ins = first;
14778 do {
14779 struct triple *ptr;
14780 int lhs, i;
14781 lhs = TRIPLE_LHS(ins->sizes);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014782 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
14783 if (ptr != LHS(ins, i)) {
14784 internal_error(state, ins, "malformed lhs on %s",
14785 tops(ins->op));
14786 }
14787 if (ptr->op != OP_PIECE) {
14788 internal_error(state, ins, "bad lhs op %s at %d on %s",
14789 tops(ptr->op), i, tops(ins->op));
14790 }
14791 if (ptr->u.cval != i) {
14792 internal_error(state, ins, "bad u.cval of %d %d expected",
14793 ptr->u.cval, i);
14794 }
14795 }
14796 ins = ins->next;
14797 } while(ins != first);
14798}
14799static void verify_ins_colors(struct compile_state *state)
14800{
14801 struct triple *first, *ins;
14802
14803 first = RHS(state->main_function, 0);
14804 ins = first;
14805 do {
14806 ins = ins->next;
14807 } while(ins != first);
14808}
14809static void verify_consistency(struct compile_state *state)
14810{
14811 verify_uses(state);
Eric Biedermand1ea5392003-06-28 06:49:45 +000014812 verify_blocks_present(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014813 verify_blocks(state);
14814 verify_domination(state);
14815 verify_piece(state);
14816 verify_ins_colors(state);
14817}
14818#else
Eric Biederman153ea352003-06-20 14:43:20 +000014819static void verify_consistency(struct compile_state *state) {}
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014820#endif /* DEBUG_USES */
Eric Biedermanb138ac82003-04-22 18:44:01 +000014821
14822static void optimize(struct compile_state *state)
14823{
14824 if (state->debug & DEBUG_TRIPLES) {
14825 print_triples(state);
14826 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014827 /* Replace structures with simpler data types */
14828 flatten_structures(state);
14829 if (state->debug & DEBUG_TRIPLES) {
14830 print_triples(state);
14831 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014832 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014833 /* Analize the intermediate code */
14834 setup_basic_blocks(state);
14835 analyze_idominators(state);
14836 analyze_ipdominators(state);
Eric Biedermand1ea5392003-06-28 06:49:45 +000014837
Eric Biederman530b5192003-07-01 10:05:30 +000014838 /* Transform the code to ssa form. */
14839 /*
14840 * The transformation to ssa form puts a phi function
14841 * on each of edge of a dominance frontier where that
14842 * phi function might be needed. At -O2 if we don't
14843 * eleminate the excess phi functions we can get an
14844 * exponential code size growth. So I kill the extra
14845 * phi functions early and I kill them often.
14846 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000014847 transform_to_ssa_form(state);
Eric Biederman530b5192003-07-01 10:05:30 +000014848 eliminate_inefectual_code(state);
14849
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014850 verify_consistency(state);
Eric Biederman05f26fc2003-06-11 21:55:00 +000014851 if (state->debug & DEBUG_CODE_ELIMINATION) {
14852 fprintf(stdout, "After transform_to_ssa_form\n");
14853 print_blocks(state, stdout);
14854 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014855 /* Do strength reduction and simple constant optimizations */
14856 if (state->optimize >= 1) {
14857 simplify_all(state);
Eric Biederman530b5192003-07-01 10:05:30 +000014858 transform_from_ssa_form(state);
14859 free_basic_blocks(state);
14860 setup_basic_blocks(state);
14861 analyze_idominators(state);
14862 analyze_ipdominators(state);
14863 transform_to_ssa_form(state);
14864 eliminate_inefectual_code(state);
14865 }
14866 if (state->debug & DEBUG_CODE_ELIMINATION) {
14867 fprintf(stdout, "After simplify_all\n");
14868 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014869 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014870 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014871 /* Propogate constants throughout the code */
14872 if (state->optimize >= 2) {
14873 scc_transform(state);
14874 transform_from_ssa_form(state);
14875 free_basic_blocks(state);
14876 setup_basic_blocks(state);
14877 analyze_idominators(state);
14878 analyze_ipdominators(state);
14879 transform_to_ssa_form(state);
Eric Biederman530b5192003-07-01 10:05:30 +000014880 eliminate_inefectual_code(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014881 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014882 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014883#warning "WISHLIST implement single use constants (least possible register pressure)"
14884#warning "WISHLIST implement induction variable elimination"
Eric Biedermanb138ac82003-04-22 18:44:01 +000014885 /* Select architecture instructions and an initial partial
14886 * coloring based on architecture constraints.
14887 */
14888 transform_to_arch_instructions(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014889 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014890 if (state->debug & DEBUG_ARCH_CODE) {
14891 printf("After transform_to_arch_instructions\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014892 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014893 print_control_flow(state);
14894 }
14895 eliminate_inefectual_code(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014896 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014897 if (state->debug & DEBUG_CODE_ELIMINATION) {
14898 printf("After eliminate_inefectual_code\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014899 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014900 print_control_flow(state);
14901 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014902 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014903 /* Color all of the variables to see if they will fit in registers */
14904 insert_copies_to_phi(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014905 if (state->debug & DEBUG_INSERTED_COPIES) {
14906 printf("After insert_copies_to_phi\n");
14907 print_blocks(state, stdout);
14908 print_control_flow(state);
14909 }
14910 verify_consistency(state);
14911 insert_mandatory_copies(state);
14912 if (state->debug & DEBUG_INSERTED_COPIES) {
14913 printf("After insert_mandatory_copies\n");
14914 print_blocks(state, stdout);
14915 print_control_flow(state);
14916 }
14917 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014918 allocate_registers(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014919 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014920 if (state->debug & DEBUG_INTERMEDIATE_CODE) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014921 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014922 }
14923 if (state->debug & DEBUG_CONTROL_FLOW) {
14924 print_control_flow(state);
14925 }
14926 /* Remove the optimization information.
14927 * This is more to check for memory consistency than to free memory.
14928 */
14929 free_basic_blocks(state);
14930}
14931
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014932static void print_op_asm(struct compile_state *state,
14933 struct triple *ins, FILE *fp)
14934{
14935 struct asm_info *info;
14936 const char *ptr;
14937 unsigned lhs, rhs, i;
14938 info = ins->u.ainfo;
14939 lhs = TRIPLE_LHS(ins->sizes);
14940 rhs = TRIPLE_RHS(ins->sizes);
14941 /* Don't count the clobbers in lhs */
14942 for(i = 0; i < lhs; i++) {
14943 if (LHS(ins, i)->type == &void_type) {
14944 break;
14945 }
14946 }
14947 lhs = i;
Eric Biederman8d9c1232003-06-17 08:42:17 +000014948 fprintf(fp, "#ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014949 fputc('\t', fp);
14950 for(ptr = info->str; *ptr; ptr++) {
14951 char *next;
14952 unsigned long param;
14953 struct triple *piece;
14954 if (*ptr != '%') {
14955 fputc(*ptr, fp);
14956 continue;
14957 }
14958 ptr++;
14959 if (*ptr == '%') {
14960 fputc('%', fp);
14961 continue;
14962 }
14963 param = strtoul(ptr, &next, 10);
14964 if (ptr == next) {
14965 error(state, ins, "Invalid asm template");
14966 }
14967 if (param >= (lhs + rhs)) {
14968 error(state, ins, "Invalid param %%%u in asm template",
14969 param);
14970 }
14971 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
14972 fprintf(fp, "%s",
14973 arch_reg_str(ID_REG(piece->id)));
Eric Biederman8d9c1232003-06-17 08:42:17 +000014974 ptr = next -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014975 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000014976 fprintf(fp, "\n#NOT ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014977}
14978
14979
14980/* Only use the low x86 byte registers. This allows me
14981 * allocate the entire register when a byte register is used.
14982 */
14983#define X86_4_8BIT_GPRS 1
14984
14985/* Recognized x86 cpu variants */
14986#define BAD_CPU 0
14987#define CPU_I386 1
14988#define CPU_P3 2
14989#define CPU_P4 3
14990#define CPU_K7 4
14991#define CPU_K8 5
14992
14993#define CPU_DEFAULT CPU_I386
14994
Eric Biedermanb138ac82003-04-22 18:44:01 +000014995/* The x86 register classes */
Eric Biederman530b5192003-07-01 10:05:30 +000014996#define REGC_FLAGS 0
14997#define REGC_GPR8 1
14998#define REGC_GPR16 2
14999#define REGC_GPR32 3
15000#define REGC_DIVIDEND64 4
15001#define REGC_DIVIDEND32 5
15002#define REGC_MMX 6
15003#define REGC_XMM 7
15004#define REGC_GPR32_8 8
15005#define REGC_GPR16_8 9
15006#define REGC_GPR8_LO 10
15007#define REGC_IMM32 11
15008#define REGC_IMM16 12
15009#define REGC_IMM8 13
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015010#define LAST_REGC REGC_IMM8
Eric Biedermanb138ac82003-04-22 18:44:01 +000015011#if LAST_REGC >= MAX_REGC
15012#error "MAX_REGC is to low"
15013#endif
15014
15015/* Register class masks */
Eric Biederman530b5192003-07-01 10:05:30 +000015016#define REGCM_FLAGS (1 << REGC_FLAGS)
15017#define REGCM_GPR8 (1 << REGC_GPR8)
15018#define REGCM_GPR16 (1 << REGC_GPR16)
15019#define REGCM_GPR32 (1 << REGC_GPR32)
15020#define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
15021#define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
15022#define REGCM_MMX (1 << REGC_MMX)
15023#define REGCM_XMM (1 << REGC_XMM)
15024#define REGCM_GPR32_8 (1 << REGC_GPR32_8)
15025#define REGCM_GPR16_8 (1 << REGC_GPR16_8)
15026#define REGCM_GPR8_LO (1 << REGC_GPR8_LO)
15027#define REGCM_IMM32 (1 << REGC_IMM32)
15028#define REGCM_IMM16 (1 << REGC_IMM16)
15029#define REGCM_IMM8 (1 << REGC_IMM8)
15030#define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015031
15032/* The x86 registers */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015033#define REG_EFLAGS 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000015034#define REGC_FLAGS_FIRST REG_EFLAGS
15035#define REGC_FLAGS_LAST REG_EFLAGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015036#define REG_AL 3
15037#define REG_BL 4
15038#define REG_CL 5
15039#define REG_DL 6
15040#define REG_AH 7
15041#define REG_BH 8
15042#define REG_CH 9
15043#define REG_DH 10
Eric Biederman530b5192003-07-01 10:05:30 +000015044#define REGC_GPR8_LO_FIRST REG_AL
15045#define REGC_GPR8_LO_LAST REG_DL
Eric Biedermanb138ac82003-04-22 18:44:01 +000015046#define REGC_GPR8_FIRST REG_AL
Eric Biedermanb138ac82003-04-22 18:44:01 +000015047#define REGC_GPR8_LAST REG_DH
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015048#define REG_AX 11
15049#define REG_BX 12
15050#define REG_CX 13
15051#define REG_DX 14
15052#define REG_SI 15
15053#define REG_DI 16
15054#define REG_BP 17
15055#define REG_SP 18
Eric Biedermanb138ac82003-04-22 18:44:01 +000015056#define REGC_GPR16_FIRST REG_AX
15057#define REGC_GPR16_LAST REG_SP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015058#define REG_EAX 19
15059#define REG_EBX 20
15060#define REG_ECX 21
15061#define REG_EDX 22
15062#define REG_ESI 23
15063#define REG_EDI 24
15064#define REG_EBP 25
15065#define REG_ESP 26
Eric Biedermanb138ac82003-04-22 18:44:01 +000015066#define REGC_GPR32_FIRST REG_EAX
15067#define REGC_GPR32_LAST REG_ESP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015068#define REG_EDXEAX 27
Eric Biederman530b5192003-07-01 10:05:30 +000015069#define REGC_DIVIDEND64_FIRST REG_EDXEAX
15070#define REGC_DIVIDEND64_LAST REG_EDXEAX
15071#define REG_DXAX 28
15072#define REGC_DIVIDEND32_FIRST REG_DXAX
15073#define REGC_DIVIDEND32_LAST REG_DXAX
15074#define REG_MMX0 29
15075#define REG_MMX1 30
15076#define REG_MMX2 31
15077#define REG_MMX3 32
15078#define REG_MMX4 33
15079#define REG_MMX5 34
15080#define REG_MMX6 35
15081#define REG_MMX7 36
Eric Biedermanb138ac82003-04-22 18:44:01 +000015082#define REGC_MMX_FIRST REG_MMX0
15083#define REGC_MMX_LAST REG_MMX7
Eric Biederman530b5192003-07-01 10:05:30 +000015084#define REG_XMM0 37
15085#define REG_XMM1 38
15086#define REG_XMM2 39
15087#define REG_XMM3 40
15088#define REG_XMM4 41
15089#define REG_XMM5 42
15090#define REG_XMM6 43
15091#define REG_XMM7 44
Eric Biedermanb138ac82003-04-22 18:44:01 +000015092#define REGC_XMM_FIRST REG_XMM0
15093#define REGC_XMM_LAST REG_XMM7
15094#warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
15095#define LAST_REG REG_XMM7
15096
15097#define REGC_GPR32_8_FIRST REG_EAX
15098#define REGC_GPR32_8_LAST REG_EDX
15099#define REGC_GPR16_8_FIRST REG_AX
15100#define REGC_GPR16_8_LAST REG_DX
15101
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015102#define REGC_IMM8_FIRST -1
15103#define REGC_IMM8_LAST -1
15104#define REGC_IMM16_FIRST -2
15105#define REGC_IMM16_LAST -1
15106#define REGC_IMM32_FIRST -4
15107#define REGC_IMM32_LAST -1
15108
Eric Biedermanb138ac82003-04-22 18:44:01 +000015109#if LAST_REG >= MAX_REGISTERS
15110#error "MAX_REGISTERS to low"
15111#endif
15112
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015113
15114static unsigned regc_size[LAST_REGC +1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000015115 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
15116 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
15117 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
15118 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
15119 [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
15120 [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
15121 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
15122 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
15123 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
15124 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
15125 [REGC_GPR8_LO] = REGC_GPR8_LO_LAST - REGC_GPR8_LO_FIRST + 1,
15126 [REGC_IMM32] = 0,
15127 [REGC_IMM16] = 0,
15128 [REGC_IMM8] = 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015129};
15130
15131static const struct {
15132 int first, last;
15133} regcm_bound[LAST_REGC + 1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000015134 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
15135 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
15136 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
15137 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
15138 [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
15139 [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
15140 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
15141 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
15142 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
15143 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
15144 [REGC_GPR8_LO] = { REGC_GPR8_LO_FIRST, REGC_GPR8_LO_LAST },
15145 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
15146 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
15147 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015148};
15149
15150static int arch_encode_cpu(const char *cpu)
15151{
15152 struct cpu {
15153 const char *name;
15154 int cpu;
15155 } cpus[] = {
15156 { "i386", CPU_I386 },
15157 { "p3", CPU_P3 },
15158 { "p4", CPU_P4 },
15159 { "k7", CPU_K7 },
15160 { "k8", CPU_K8 },
15161 { 0, BAD_CPU }
15162 };
15163 struct cpu *ptr;
15164 for(ptr = cpus; ptr->name; ptr++) {
15165 if (strcmp(ptr->name, cpu) == 0) {
15166 break;
15167 }
15168 }
15169 return ptr->cpu;
15170}
15171
Eric Biedermanb138ac82003-04-22 18:44:01 +000015172static unsigned arch_regc_size(struct compile_state *state, int class)
15173{
Eric Biedermanb138ac82003-04-22 18:44:01 +000015174 if ((class < 0) || (class > LAST_REGC)) {
15175 return 0;
15176 }
15177 return regc_size[class];
15178}
Eric Biedermand1ea5392003-06-28 06:49:45 +000015179
Eric Biedermanb138ac82003-04-22 18:44:01 +000015180static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
15181{
15182 /* See if two register classes may have overlapping registers */
Eric Biederman530b5192003-07-01 10:05:30 +000015183 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
15184 REGCM_GPR32_8 | REGCM_GPR32 |
15185 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015186
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015187 /* Special case for the immediates */
15188 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
15189 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
15190 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
15191 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
15192 return 0;
15193 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015194 return (regcm1 & regcm2) ||
15195 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
15196}
15197
15198static void arch_reg_equivs(
15199 struct compile_state *state, unsigned *equiv, int reg)
15200{
15201 if ((reg < 0) || (reg > LAST_REG)) {
15202 internal_error(state, 0, "invalid register");
15203 }
15204 *equiv++ = reg;
15205 switch(reg) {
15206 case REG_AL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015207#if X86_4_8BIT_GPRS
15208 *equiv++ = REG_AH;
15209#endif
15210 *equiv++ = REG_AX;
15211 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000015212 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015213 *equiv++ = REG_EDXEAX;
15214 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015215 case REG_AH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015216#if X86_4_8BIT_GPRS
15217 *equiv++ = REG_AL;
15218#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015219 *equiv++ = REG_AX;
15220 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000015221 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015222 *equiv++ = REG_EDXEAX;
15223 break;
15224 case REG_BL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015225#if X86_4_8BIT_GPRS
15226 *equiv++ = REG_BH;
15227#endif
15228 *equiv++ = REG_BX;
15229 *equiv++ = REG_EBX;
15230 break;
15231
Eric Biedermanb138ac82003-04-22 18:44:01 +000015232 case REG_BH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015233#if X86_4_8BIT_GPRS
15234 *equiv++ = REG_BL;
15235#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015236 *equiv++ = REG_BX;
15237 *equiv++ = REG_EBX;
15238 break;
15239 case REG_CL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015240#if X86_4_8BIT_GPRS
15241 *equiv++ = REG_CH;
15242#endif
15243 *equiv++ = REG_CX;
15244 *equiv++ = REG_ECX;
15245 break;
15246
Eric Biedermanb138ac82003-04-22 18:44:01 +000015247 case REG_CH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015248#if X86_4_8BIT_GPRS
15249 *equiv++ = REG_CL;
15250#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015251 *equiv++ = REG_CX;
15252 *equiv++ = REG_ECX;
15253 break;
15254 case REG_DL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015255#if X86_4_8BIT_GPRS
15256 *equiv++ = REG_DH;
15257#endif
15258 *equiv++ = REG_DX;
15259 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000015260 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015261 *equiv++ = REG_EDXEAX;
15262 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015263 case REG_DH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015264#if X86_4_8BIT_GPRS
15265 *equiv++ = REG_DL;
15266#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015267 *equiv++ = REG_DX;
15268 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000015269 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015270 *equiv++ = REG_EDXEAX;
15271 break;
15272 case REG_AX:
15273 *equiv++ = REG_AL;
15274 *equiv++ = REG_AH;
15275 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000015276 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015277 *equiv++ = REG_EDXEAX;
15278 break;
15279 case REG_BX:
15280 *equiv++ = REG_BL;
15281 *equiv++ = REG_BH;
15282 *equiv++ = REG_EBX;
15283 break;
15284 case REG_CX:
15285 *equiv++ = REG_CL;
15286 *equiv++ = REG_CH;
15287 *equiv++ = REG_ECX;
15288 break;
15289 case REG_DX:
15290 *equiv++ = REG_DL;
15291 *equiv++ = REG_DH;
15292 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000015293 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015294 *equiv++ = REG_EDXEAX;
15295 break;
15296 case REG_SI:
15297 *equiv++ = REG_ESI;
15298 break;
15299 case REG_DI:
15300 *equiv++ = REG_EDI;
15301 break;
15302 case REG_BP:
15303 *equiv++ = REG_EBP;
15304 break;
15305 case REG_SP:
15306 *equiv++ = REG_ESP;
15307 break;
15308 case REG_EAX:
15309 *equiv++ = REG_AL;
15310 *equiv++ = REG_AH;
15311 *equiv++ = REG_AX;
Eric Biederman530b5192003-07-01 10:05:30 +000015312 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015313 *equiv++ = REG_EDXEAX;
15314 break;
15315 case REG_EBX:
15316 *equiv++ = REG_BL;
15317 *equiv++ = REG_BH;
15318 *equiv++ = REG_BX;
15319 break;
15320 case REG_ECX:
15321 *equiv++ = REG_CL;
15322 *equiv++ = REG_CH;
15323 *equiv++ = REG_CX;
15324 break;
15325 case REG_EDX:
15326 *equiv++ = REG_DL;
15327 *equiv++ = REG_DH;
15328 *equiv++ = REG_DX;
Eric Biederman530b5192003-07-01 10:05:30 +000015329 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015330 *equiv++ = REG_EDXEAX;
15331 break;
15332 case REG_ESI:
15333 *equiv++ = REG_SI;
15334 break;
15335 case REG_EDI:
15336 *equiv++ = REG_DI;
15337 break;
15338 case REG_EBP:
15339 *equiv++ = REG_BP;
15340 break;
15341 case REG_ESP:
15342 *equiv++ = REG_SP;
15343 break;
Eric Biederman530b5192003-07-01 10:05:30 +000015344 case REG_DXAX:
15345 *equiv++ = REG_AL;
15346 *equiv++ = REG_AH;
15347 *equiv++ = REG_DL;
15348 *equiv++ = REG_DH;
15349 *equiv++ = REG_AX;
15350 *equiv++ = REG_DX;
15351 *equiv++ = REG_EAX;
15352 *equiv++ = REG_EDX;
15353 *equiv++ = REG_EDXEAX;
15354 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015355 case REG_EDXEAX:
15356 *equiv++ = REG_AL;
15357 *equiv++ = REG_AH;
15358 *equiv++ = REG_DL;
15359 *equiv++ = REG_DH;
15360 *equiv++ = REG_AX;
15361 *equiv++ = REG_DX;
15362 *equiv++ = REG_EAX;
15363 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000015364 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015365 break;
15366 }
15367 *equiv++ = REG_UNSET;
15368}
15369
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015370static unsigned arch_avail_mask(struct compile_state *state)
15371{
15372 unsigned avail_mask;
Eric Biederman530b5192003-07-01 10:05:30 +000015373 /* REGCM_GPR8 is not available */
15374 avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
15375 REGCM_GPR32 | REGCM_GPR32_8 |
15376 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015377 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
15378 switch(state->cpu) {
15379 case CPU_P3:
15380 case CPU_K7:
15381 avail_mask |= REGCM_MMX;
15382 break;
15383 case CPU_P4:
15384 case CPU_K8:
15385 avail_mask |= REGCM_MMX | REGCM_XMM;
15386 break;
15387 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015388 return avail_mask;
15389}
15390
15391static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
15392{
15393 unsigned mask, result;
15394 int class, class2;
15395 result = regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015396
15397 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
15398 if ((result & mask) == 0) {
15399 continue;
15400 }
15401 if (class > LAST_REGC) {
15402 result &= ~mask;
15403 }
15404 for(class2 = 0; class2 <= LAST_REGC; class2++) {
15405 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
15406 (regcm_bound[class2].last <= regcm_bound[class].last)) {
15407 result |= (1 << class2);
15408 }
15409 }
15410 }
Eric Biederman530b5192003-07-01 10:05:30 +000015411 result &= arch_avail_mask(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015412 return result;
15413}
Eric Biedermanb138ac82003-04-22 18:44:01 +000015414
Eric Biedermand1ea5392003-06-28 06:49:45 +000015415static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
15416{
15417 /* Like arch_regcm_normalize except immediate register classes are excluded */
15418 regcm = arch_regcm_normalize(state, regcm);
15419 /* Remove the immediate register classes */
15420 regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
15421 return regcm;
15422
15423}
15424
Eric Biedermanb138ac82003-04-22 18:44:01 +000015425static unsigned arch_reg_regcm(struct compile_state *state, int reg)
15426{
Eric Biedermanb138ac82003-04-22 18:44:01 +000015427 unsigned mask;
15428 int class;
15429 mask = 0;
15430 for(class = 0; class <= LAST_REGC; class++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015431 if ((reg >= regcm_bound[class].first) &&
15432 (reg <= regcm_bound[class].last)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015433 mask |= (1 << class);
15434 }
15435 }
15436 if (!mask) {
15437 internal_error(state, 0, "reg %d not in any class", reg);
15438 }
15439 return mask;
15440}
15441
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015442static struct reg_info arch_reg_constraint(
15443 struct compile_state *state, struct type *type, const char *constraint)
15444{
15445 static const struct {
15446 char class;
15447 unsigned int mask;
15448 unsigned int reg;
15449 } constraints[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000015450 { 'r', REGCM_GPR32, REG_UNSET },
15451 { 'g', REGCM_GPR32, REG_UNSET },
15452 { 'p', REGCM_GPR32, REG_UNSET },
15453 { 'q', REGCM_GPR8_LO, REG_UNSET },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015454 { 'Q', REGCM_GPR32_8, REG_UNSET },
Eric Biederman530b5192003-07-01 10:05:30 +000015455 { 'x', REGCM_XMM, REG_UNSET },
15456 { 'y', REGCM_MMX, REG_UNSET },
15457 { 'a', REGCM_GPR32, REG_EAX },
15458 { 'b', REGCM_GPR32, REG_EBX },
15459 { 'c', REGCM_GPR32, REG_ECX },
15460 { 'd', REGCM_GPR32, REG_EDX },
15461 { 'D', REGCM_GPR32, REG_EDI },
15462 { 'S', REGCM_GPR32, REG_ESI },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015463 { '\0', 0, REG_UNSET },
15464 };
15465 unsigned int regcm;
15466 unsigned int mask, reg;
15467 struct reg_info result;
15468 const char *ptr;
15469 regcm = arch_type_to_regcm(state, type);
15470 reg = REG_UNSET;
15471 mask = 0;
15472 for(ptr = constraint; *ptr; ptr++) {
15473 int i;
15474 if (*ptr == ' ') {
15475 continue;
15476 }
15477 for(i = 0; constraints[i].class != '\0'; i++) {
15478 if (constraints[i].class == *ptr) {
15479 break;
15480 }
15481 }
15482 if (constraints[i].class == '\0') {
15483 error(state, 0, "invalid register constraint ``%c''", *ptr);
15484 break;
15485 }
15486 if ((constraints[i].mask & regcm) == 0) {
15487 error(state, 0, "invalid register class %c specified",
15488 *ptr);
15489 }
15490 mask |= constraints[i].mask;
15491 if (constraints[i].reg != REG_UNSET) {
15492 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
15493 error(state, 0, "Only one register may be specified");
15494 }
15495 reg = constraints[i].reg;
15496 }
15497 }
15498 result.reg = reg;
15499 result.regcm = mask;
15500 return result;
15501}
15502
15503static struct reg_info arch_reg_clobber(
15504 struct compile_state *state, const char *clobber)
15505{
15506 struct reg_info result;
15507 if (strcmp(clobber, "memory") == 0) {
15508 result.reg = REG_UNSET;
15509 result.regcm = 0;
15510 }
15511 else if (strcmp(clobber, "%eax") == 0) {
15512 result.reg = REG_EAX;
15513 result.regcm = REGCM_GPR32;
15514 }
15515 else if (strcmp(clobber, "%ebx") == 0) {
15516 result.reg = REG_EBX;
15517 result.regcm = REGCM_GPR32;
15518 }
15519 else if (strcmp(clobber, "%ecx") == 0) {
15520 result.reg = REG_ECX;
15521 result.regcm = REGCM_GPR32;
15522 }
15523 else if (strcmp(clobber, "%edx") == 0) {
15524 result.reg = REG_EDX;
15525 result.regcm = REGCM_GPR32;
15526 }
15527 else if (strcmp(clobber, "%esi") == 0) {
15528 result.reg = REG_ESI;
15529 result.regcm = REGCM_GPR32;
15530 }
15531 else if (strcmp(clobber, "%edi") == 0) {
15532 result.reg = REG_EDI;
15533 result.regcm = REGCM_GPR32;
15534 }
15535 else if (strcmp(clobber, "%ebp") == 0) {
15536 result.reg = REG_EBP;
15537 result.regcm = REGCM_GPR32;
15538 }
15539 else if (strcmp(clobber, "%esp") == 0) {
15540 result.reg = REG_ESP;
15541 result.regcm = REGCM_GPR32;
15542 }
15543 else if (strcmp(clobber, "cc") == 0) {
15544 result.reg = REG_EFLAGS;
15545 result.regcm = REGCM_FLAGS;
15546 }
15547 else if ((strncmp(clobber, "xmm", 3) == 0) &&
15548 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15549 result.reg = REG_XMM0 + octdigval(clobber[3]);
15550 result.regcm = REGCM_XMM;
15551 }
15552 else if ((strncmp(clobber, "mmx", 3) == 0) &&
15553 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15554 result.reg = REG_MMX0 + octdigval(clobber[3]);
15555 result.regcm = REGCM_MMX;
15556 }
15557 else {
15558 error(state, 0, "Invalid register clobber");
15559 result.reg = REG_UNSET;
15560 result.regcm = 0;
15561 }
15562 return result;
15563}
15564
Eric Biedermanb138ac82003-04-22 18:44:01 +000015565static int do_select_reg(struct compile_state *state,
15566 char *used, int reg, unsigned classes)
15567{
15568 unsigned mask;
15569 if (used[reg]) {
15570 return REG_UNSET;
15571 }
15572 mask = arch_reg_regcm(state, reg);
15573 return (classes & mask) ? reg : REG_UNSET;
15574}
15575
15576static int arch_select_free_register(
15577 struct compile_state *state, char *used, int classes)
15578{
Eric Biedermand1ea5392003-06-28 06:49:45 +000015579 /* Live ranges with the most neighbors are colored first.
15580 *
15581 * Generally it does not matter which colors are given
15582 * as the register allocator attempts to color live ranges
15583 * in an order where you are guaranteed not to run out of colors.
15584 *
15585 * Occasionally the register allocator cannot find an order
15586 * of register selection that will find a free color. To
15587 * increase the odds the register allocator will work when
15588 * it guesses first give out registers from register classes
15589 * least likely to run out of registers.
15590 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000015591 */
15592 int i, reg;
15593 reg = REG_UNSET;
Eric Biedermand1ea5392003-06-28 06:49:45 +000015594 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015595 reg = do_select_reg(state, used, i, classes);
15596 }
15597 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
15598 reg = do_select_reg(state, used, i, classes);
15599 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000015600 for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015601 reg = do_select_reg(state, used, i, classes);
15602 }
15603 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
15604 reg = do_select_reg(state, used, i, classes);
15605 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015606 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
15607 reg = do_select_reg(state, used, i, classes);
15608 }
Eric Biederman530b5192003-07-01 10:05:30 +000015609 for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
15610 reg = do_select_reg(state, used, i, classes);
15611 }
15612 for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
15613 reg = do_select_reg(state, used, i, classes);
15614 }
15615 for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015616 reg = do_select_reg(state, used, i, classes);
15617 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000015618 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
15619 reg = do_select_reg(state, used, i, classes);
15620 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015621 return reg;
15622}
15623
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015624
Eric Biedermanb138ac82003-04-22 18:44:01 +000015625static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
15626{
15627#warning "FIXME force types smaller (if legal) before I get here"
Eric Biedermanb138ac82003-04-22 18:44:01 +000015628 unsigned mask;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015629 mask = 0;
15630 switch(type->type & TYPE_MASK) {
15631 case TYPE_ARRAY:
15632 case TYPE_VOID:
15633 mask = 0;
15634 break;
15635 case TYPE_CHAR:
15636 case TYPE_UCHAR:
Eric Biederman530b5192003-07-01 10:05:30 +000015637 mask = REGCM_GPR8 | REGCM_GPR8_LO |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015638 REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000015639 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000015640 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015641 REGCM_MMX | REGCM_XMM |
15642 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015643 break;
15644 case TYPE_SHORT:
15645 case TYPE_USHORT:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015646 mask = REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000015647 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000015648 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015649 REGCM_MMX | REGCM_XMM |
15650 REGCM_IMM32 | REGCM_IMM16;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015651 break;
15652 case TYPE_INT:
15653 case TYPE_UINT:
15654 case TYPE_LONG:
15655 case TYPE_ULONG:
15656 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015657 mask = REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000015658 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
15659 REGCM_MMX | REGCM_XMM |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015660 REGCM_IMM32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015661 break;
15662 default:
15663 internal_error(state, 0, "no register class for type");
15664 break;
15665 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000015666 mask = arch_regcm_normalize(state, mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015667 return mask;
15668}
15669
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015670static int is_imm32(struct triple *imm)
15671{
15672 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
15673 (imm->op == OP_ADDRCONST);
15674
15675}
15676static int is_imm16(struct triple *imm)
15677{
15678 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
15679}
15680static int is_imm8(struct triple *imm)
15681{
15682 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
15683}
15684
15685static int get_imm32(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015686{
15687 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015688 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015689 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000015690 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015691 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015692 if (!is_imm32(imm)) {
15693 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015694 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015695 unuse_triple(*expr, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015696 use_triple(imm, ins);
15697 *expr = imm;
15698 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015699}
15700
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015701static int get_imm8(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015702{
15703 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015704 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015705 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000015706 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015707 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015708 if (!is_imm8(imm)) {
15709 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015710 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015711 unuse_triple(*expr, ins);
15712 use_triple(imm, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015713 *expr = imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015714 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015715}
15716
Eric Biederman530b5192003-07-01 10:05:30 +000015717#define TEMPLATE_NOP 0
15718#define TEMPLATE_INTCONST8 1
15719#define TEMPLATE_INTCONST32 2
15720#define TEMPLATE_COPY8_REG 3
15721#define TEMPLATE_COPY16_REG 4
15722#define TEMPLATE_COPY32_REG 5
15723#define TEMPLATE_COPY_IMM8 6
15724#define TEMPLATE_COPY_IMM16 7
15725#define TEMPLATE_COPY_IMM32 8
15726#define TEMPLATE_PHI8 9
15727#define TEMPLATE_PHI16 10
15728#define TEMPLATE_PHI32 11
15729#define TEMPLATE_STORE8 12
15730#define TEMPLATE_STORE16 13
15731#define TEMPLATE_STORE32 14
15732#define TEMPLATE_LOAD8 15
15733#define TEMPLATE_LOAD16 16
15734#define TEMPLATE_LOAD32 17
15735#define TEMPLATE_BINARY8_REG 18
15736#define TEMPLATE_BINARY16_REG 19
15737#define TEMPLATE_BINARY32_REG 20
15738#define TEMPLATE_BINARY8_IMM 21
15739#define TEMPLATE_BINARY16_IMM 22
15740#define TEMPLATE_BINARY32_IMM 23
15741#define TEMPLATE_SL8_CL 24
15742#define TEMPLATE_SL16_CL 25
15743#define TEMPLATE_SL32_CL 26
15744#define TEMPLATE_SL8_IMM 27
15745#define TEMPLATE_SL16_IMM 28
15746#define TEMPLATE_SL32_IMM 29
15747#define TEMPLATE_UNARY8 30
15748#define TEMPLATE_UNARY16 31
15749#define TEMPLATE_UNARY32 32
15750#define TEMPLATE_CMP8_REG 33
15751#define TEMPLATE_CMP16_REG 34
15752#define TEMPLATE_CMP32_REG 35
15753#define TEMPLATE_CMP8_IMM 36
15754#define TEMPLATE_CMP16_IMM 37
15755#define TEMPLATE_CMP32_IMM 38
15756#define TEMPLATE_TEST8 39
15757#define TEMPLATE_TEST16 40
15758#define TEMPLATE_TEST32 41
15759#define TEMPLATE_SET 42
15760#define TEMPLATE_JMP 43
15761#define TEMPLATE_INB_DX 44
15762#define TEMPLATE_INB_IMM 45
15763#define TEMPLATE_INW_DX 46
15764#define TEMPLATE_INW_IMM 47
15765#define TEMPLATE_INL_DX 48
15766#define TEMPLATE_INL_IMM 49
15767#define TEMPLATE_OUTB_DX 50
15768#define TEMPLATE_OUTB_IMM 51
15769#define TEMPLATE_OUTW_DX 52
15770#define TEMPLATE_OUTW_IMM 53
15771#define TEMPLATE_OUTL_DX 54
15772#define TEMPLATE_OUTL_IMM 55
15773#define TEMPLATE_BSF 56
15774#define TEMPLATE_RDMSR 57
15775#define TEMPLATE_WRMSR 58
15776#define TEMPLATE_UMUL8 59
15777#define TEMPLATE_UMUL16 60
15778#define TEMPLATE_UMUL32 61
15779#define TEMPLATE_DIV8 62
15780#define TEMPLATE_DIV16 63
15781#define TEMPLATE_DIV32 64
15782#define LAST_TEMPLATE TEMPLATE_DIV32
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015783#if LAST_TEMPLATE >= MAX_TEMPLATES
15784#error "MAX_TEMPLATES to low"
15785#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015786
Eric Biederman530b5192003-07-01 10:05:30 +000015787#define COPY8_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO | REGCM_MMX | REGCM_XMM)
15788#define COPY16_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)
15789#define COPY32_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
Eric Biedermand1ea5392003-06-28 06:49:45 +000015790
Eric Biedermanb138ac82003-04-22 18:44:01 +000015791
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015792static struct ins_template templates[] = {
15793 [TEMPLATE_NOP] = {},
15794 [TEMPLATE_INTCONST8] = {
15795 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15796 },
15797 [TEMPLATE_INTCONST32] = {
15798 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
15799 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000015800 [TEMPLATE_COPY8_REG] = {
15801 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
15802 .rhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015803 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000015804 [TEMPLATE_COPY16_REG] = {
15805 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
15806 .rhs = { [0] = { REG_UNSET, COPY16_REGCM } },
15807 },
15808 [TEMPLATE_COPY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015809 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000015810 .rhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015811 },
15812 [TEMPLATE_COPY_IMM8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000015813 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015814 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15815 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000015816 [TEMPLATE_COPY_IMM16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000015817 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000015818 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
15819 },
15820 [TEMPLATE_COPY_IMM32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000015821 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000015822 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
15823 },
15824 [TEMPLATE_PHI8] = {
15825 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015826 .rhs = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000015827 [ 0] = { REG_VIRT0, COPY8_REGCM },
15828 [ 1] = { REG_VIRT0, COPY8_REGCM },
15829 [ 2] = { REG_VIRT0, COPY8_REGCM },
15830 [ 3] = { REG_VIRT0, COPY8_REGCM },
15831 [ 4] = { REG_VIRT0, COPY8_REGCM },
15832 [ 5] = { REG_VIRT0, COPY8_REGCM },
15833 [ 6] = { REG_VIRT0, COPY8_REGCM },
15834 [ 7] = { REG_VIRT0, COPY8_REGCM },
15835 [ 8] = { REG_VIRT0, COPY8_REGCM },
15836 [ 9] = { REG_VIRT0, COPY8_REGCM },
15837 [10] = { REG_VIRT0, COPY8_REGCM },
15838 [11] = { REG_VIRT0, COPY8_REGCM },
15839 [12] = { REG_VIRT0, COPY8_REGCM },
15840 [13] = { REG_VIRT0, COPY8_REGCM },
15841 [14] = { REG_VIRT0, COPY8_REGCM },
15842 [15] = { REG_VIRT0, COPY8_REGCM },
15843 }, },
15844 [TEMPLATE_PHI16] = {
15845 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
15846 .rhs = {
15847 [ 0] = { REG_VIRT0, COPY16_REGCM },
15848 [ 1] = { REG_VIRT0, COPY16_REGCM },
15849 [ 2] = { REG_VIRT0, COPY16_REGCM },
15850 [ 3] = { REG_VIRT0, COPY16_REGCM },
15851 [ 4] = { REG_VIRT0, COPY16_REGCM },
15852 [ 5] = { REG_VIRT0, COPY16_REGCM },
15853 [ 6] = { REG_VIRT0, COPY16_REGCM },
15854 [ 7] = { REG_VIRT0, COPY16_REGCM },
15855 [ 8] = { REG_VIRT0, COPY16_REGCM },
15856 [ 9] = { REG_VIRT0, COPY16_REGCM },
15857 [10] = { REG_VIRT0, COPY16_REGCM },
15858 [11] = { REG_VIRT0, COPY16_REGCM },
15859 [12] = { REG_VIRT0, COPY16_REGCM },
15860 [13] = { REG_VIRT0, COPY16_REGCM },
15861 [14] = { REG_VIRT0, COPY16_REGCM },
15862 [15] = { REG_VIRT0, COPY16_REGCM },
15863 }, },
15864 [TEMPLATE_PHI32] = {
15865 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
15866 .rhs = {
15867 [ 0] = { REG_VIRT0, COPY32_REGCM },
15868 [ 1] = { REG_VIRT0, COPY32_REGCM },
15869 [ 2] = { REG_VIRT0, COPY32_REGCM },
15870 [ 3] = { REG_VIRT0, COPY32_REGCM },
15871 [ 4] = { REG_VIRT0, COPY32_REGCM },
15872 [ 5] = { REG_VIRT0, COPY32_REGCM },
15873 [ 6] = { REG_VIRT0, COPY32_REGCM },
15874 [ 7] = { REG_VIRT0, COPY32_REGCM },
15875 [ 8] = { REG_VIRT0, COPY32_REGCM },
15876 [ 9] = { REG_VIRT0, COPY32_REGCM },
15877 [10] = { REG_VIRT0, COPY32_REGCM },
15878 [11] = { REG_VIRT0, COPY32_REGCM },
15879 [12] = { REG_VIRT0, COPY32_REGCM },
15880 [13] = { REG_VIRT0, COPY32_REGCM },
15881 [14] = { REG_VIRT0, COPY32_REGCM },
15882 [15] = { REG_VIRT0, COPY32_REGCM },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015883 }, },
15884 [TEMPLATE_STORE8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000015885 .rhs = {
15886 [0] = { REG_UNSET, REGCM_GPR32 },
15887 [1] = { REG_UNSET, REGCM_GPR8_LO },
15888 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015889 },
15890 [TEMPLATE_STORE16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000015891 .rhs = {
15892 [0] = { REG_UNSET, REGCM_GPR32 },
15893 [1] = { REG_UNSET, REGCM_GPR16 },
15894 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015895 },
15896 [TEMPLATE_STORE32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000015897 .rhs = {
15898 [0] = { REG_UNSET, REGCM_GPR32 },
15899 [1] = { REG_UNSET, REGCM_GPR32 },
15900 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015901 },
15902 [TEMPLATE_LOAD8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000015903 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015904 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15905 },
15906 [TEMPLATE_LOAD16] = {
15907 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
15908 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15909 },
15910 [TEMPLATE_LOAD32] = {
15911 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15912 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15913 },
Eric Biederman530b5192003-07-01 10:05:30 +000015914 [TEMPLATE_BINARY8_REG] = {
15915 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
15916 .rhs = {
15917 [0] = { REG_VIRT0, REGCM_GPR8_LO },
15918 [1] = { REG_UNSET, REGCM_GPR8_LO },
15919 },
15920 },
15921 [TEMPLATE_BINARY16_REG] = {
15922 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
15923 .rhs = {
15924 [0] = { REG_VIRT0, REGCM_GPR16 },
15925 [1] = { REG_UNSET, REGCM_GPR16 },
15926 },
15927 },
15928 [TEMPLATE_BINARY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015929 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15930 .rhs = {
15931 [0] = { REG_VIRT0, REGCM_GPR32 },
15932 [1] = { REG_UNSET, REGCM_GPR32 },
15933 },
15934 },
Eric Biederman530b5192003-07-01 10:05:30 +000015935 [TEMPLATE_BINARY8_IMM] = {
15936 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
15937 .rhs = {
15938 [0] = { REG_VIRT0, REGCM_GPR8_LO },
15939 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15940 },
15941 },
15942 [TEMPLATE_BINARY16_IMM] = {
15943 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
15944 .rhs = {
15945 [0] = { REG_VIRT0, REGCM_GPR16 },
15946 [1] = { REG_UNNEEDED, REGCM_IMM16 },
15947 },
15948 },
15949 [TEMPLATE_BINARY32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015950 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15951 .rhs = {
15952 [0] = { REG_VIRT0, REGCM_GPR32 },
15953 [1] = { REG_UNNEEDED, REGCM_IMM32 },
15954 },
15955 },
Eric Biederman530b5192003-07-01 10:05:30 +000015956 [TEMPLATE_SL8_CL] = {
15957 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
15958 .rhs = {
15959 [0] = { REG_VIRT0, REGCM_GPR8_LO },
15960 [1] = { REG_CL, REGCM_GPR8_LO },
15961 },
15962 },
15963 [TEMPLATE_SL16_CL] = {
15964 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
15965 .rhs = {
15966 [0] = { REG_VIRT0, REGCM_GPR16 },
15967 [1] = { REG_CL, REGCM_GPR8_LO },
15968 },
15969 },
15970 [TEMPLATE_SL32_CL] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015971 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15972 .rhs = {
15973 [0] = { REG_VIRT0, REGCM_GPR32 },
Eric Biederman530b5192003-07-01 10:05:30 +000015974 [1] = { REG_CL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015975 },
15976 },
Eric Biederman530b5192003-07-01 10:05:30 +000015977 [TEMPLATE_SL8_IMM] = {
15978 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
15979 .rhs = {
15980 [0] = { REG_VIRT0, REGCM_GPR8_LO },
15981 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15982 },
15983 },
15984 [TEMPLATE_SL16_IMM] = {
15985 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
15986 .rhs = {
15987 [0] = { REG_VIRT0, REGCM_GPR16 },
15988 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15989 },
15990 },
15991 [TEMPLATE_SL32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015992 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15993 .rhs = {
15994 [0] = { REG_VIRT0, REGCM_GPR32 },
15995 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15996 },
15997 },
Eric Biederman530b5192003-07-01 10:05:30 +000015998 [TEMPLATE_UNARY8] = {
15999 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16000 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16001 },
16002 [TEMPLATE_UNARY16] = {
16003 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16004 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16005 },
16006 [TEMPLATE_UNARY32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016007 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16008 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16009 },
Eric Biederman530b5192003-07-01 10:05:30 +000016010 [TEMPLATE_CMP8_REG] = {
16011 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16012 .rhs = {
16013 [0] = { REG_UNSET, REGCM_GPR8_LO },
16014 [1] = { REG_UNSET, REGCM_GPR8_LO },
16015 },
16016 },
16017 [TEMPLATE_CMP16_REG] = {
16018 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16019 .rhs = {
16020 [0] = { REG_UNSET, REGCM_GPR16 },
16021 [1] = { REG_UNSET, REGCM_GPR16 },
16022 },
16023 },
16024 [TEMPLATE_CMP32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016025 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16026 .rhs = {
16027 [0] = { REG_UNSET, REGCM_GPR32 },
16028 [1] = { REG_UNSET, REGCM_GPR32 },
16029 },
16030 },
Eric Biederman530b5192003-07-01 10:05:30 +000016031 [TEMPLATE_CMP8_IMM] = {
16032 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16033 .rhs = {
16034 [0] = { REG_UNSET, REGCM_GPR8_LO },
16035 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16036 },
16037 },
16038 [TEMPLATE_CMP16_IMM] = {
16039 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16040 .rhs = {
16041 [0] = { REG_UNSET, REGCM_GPR16 },
16042 [1] = { REG_UNNEEDED, REGCM_IMM16 },
16043 },
16044 },
16045 [TEMPLATE_CMP32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016046 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16047 .rhs = {
16048 [0] = { REG_UNSET, REGCM_GPR32 },
16049 [1] = { REG_UNNEEDED, REGCM_IMM32 },
16050 },
16051 },
Eric Biederman530b5192003-07-01 10:05:30 +000016052 [TEMPLATE_TEST8] = {
16053 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16054 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
16055 },
16056 [TEMPLATE_TEST16] = {
16057 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16058 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
16059 },
16060 [TEMPLATE_TEST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016061 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16062 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16063 },
16064 [TEMPLATE_SET] = {
Eric Biederman530b5192003-07-01 10:05:30 +000016065 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016066 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16067 },
16068 [TEMPLATE_JMP] = {
16069 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16070 },
16071 [TEMPLATE_INB_DX] = {
Eric Biederman530b5192003-07-01 10:05:30 +000016072 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016073 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
16074 },
16075 [TEMPLATE_INB_IMM] = {
Eric Biederman530b5192003-07-01 10:05:30 +000016076 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016077 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16078 },
16079 [TEMPLATE_INW_DX] = {
16080 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
16081 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
16082 },
16083 [TEMPLATE_INW_IMM] = {
16084 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
16085 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16086 },
16087 [TEMPLATE_INL_DX] = {
16088 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
16089 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
16090 },
16091 [TEMPLATE_INL_IMM] = {
16092 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
16093 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16094 },
16095 [TEMPLATE_OUTB_DX] = {
16096 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000016097 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016098 [1] = { REG_DX, REGCM_GPR16 },
16099 },
16100 },
16101 [TEMPLATE_OUTB_IMM] = {
16102 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000016103 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016104 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16105 },
16106 },
16107 [TEMPLATE_OUTW_DX] = {
16108 .rhs = {
16109 [0] = { REG_AX, REGCM_GPR16 },
16110 [1] = { REG_DX, REGCM_GPR16 },
16111 },
16112 },
16113 [TEMPLATE_OUTW_IMM] = {
16114 .rhs = {
16115 [0] = { REG_AX, REGCM_GPR16 },
16116 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16117 },
16118 },
16119 [TEMPLATE_OUTL_DX] = {
16120 .rhs = {
16121 [0] = { REG_EAX, REGCM_GPR32 },
16122 [1] = { REG_DX, REGCM_GPR16 },
16123 },
16124 },
16125 [TEMPLATE_OUTL_IMM] = {
16126 .rhs = {
16127 [0] = { REG_EAX, REGCM_GPR32 },
16128 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16129 },
16130 },
16131 [TEMPLATE_BSF] = {
16132 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16133 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16134 },
16135 [TEMPLATE_RDMSR] = {
16136 .lhs = {
16137 [0] = { REG_EAX, REGCM_GPR32 },
16138 [1] = { REG_EDX, REGCM_GPR32 },
16139 },
16140 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
16141 },
16142 [TEMPLATE_WRMSR] = {
16143 .rhs = {
16144 [0] = { REG_ECX, REGCM_GPR32 },
16145 [1] = { REG_EAX, REGCM_GPR32 },
16146 [2] = { REG_EDX, REGCM_GPR32 },
16147 },
16148 },
Eric Biederman530b5192003-07-01 10:05:30 +000016149 [TEMPLATE_UMUL8] = {
16150 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
16151 .rhs = {
16152 [0] = { REG_AL, REGCM_GPR8_LO },
16153 [1] = { REG_UNSET, REGCM_GPR8_LO },
16154 },
16155 },
16156 [TEMPLATE_UMUL16] = {
16157 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
16158 .rhs = {
16159 [0] = { REG_AX, REGCM_GPR16 },
16160 [1] = { REG_UNSET, REGCM_GPR16 },
16161 },
16162 },
16163 [TEMPLATE_UMUL32] = {
16164 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000016165 .rhs = {
16166 [0] = { REG_EAX, REGCM_GPR32 },
16167 [1] = { REG_UNSET, REGCM_GPR32 },
16168 },
16169 },
Eric Biederman530b5192003-07-01 10:05:30 +000016170 [TEMPLATE_DIV8] = {
16171 .lhs = {
16172 [0] = { REG_AL, REGCM_GPR8_LO },
16173 [1] = { REG_AH, REGCM_GPR8 },
16174 },
16175 .rhs = {
16176 [0] = { REG_AX, REGCM_GPR16 },
16177 [1] = { REG_UNSET, REGCM_GPR8_LO },
16178 },
16179 },
16180 [TEMPLATE_DIV16] = {
16181 .lhs = {
16182 [0] = { REG_AX, REGCM_GPR16 },
16183 [1] = { REG_DX, REGCM_GPR16 },
16184 },
16185 .rhs = {
16186 [0] = { REG_DXAX, REGCM_DIVIDEND32 },
16187 [1] = { REG_UNSET, REGCM_GPR16 },
16188 },
16189 },
16190 [TEMPLATE_DIV32] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016191 .lhs = {
16192 [0] = { REG_EAX, REGCM_GPR32 },
16193 [1] = { REG_EDX, REGCM_GPR32 },
16194 },
16195 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000016196 [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000016197 [1] = { REG_UNSET, REGCM_GPR32 },
16198 },
16199 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016200};
Eric Biedermanb138ac82003-04-22 18:44:01 +000016201
16202static void fixup_branches(struct compile_state *state,
16203 struct triple *cmp, struct triple *use, int jmp_op)
16204{
16205 struct triple_set *entry, *next;
16206 for(entry = use->use; entry; entry = next) {
16207 next = entry->next;
16208 if (entry->member->op == OP_COPY) {
16209 fixup_branches(state, cmp, entry->member, jmp_op);
16210 }
16211 else if (entry->member->op == OP_BRANCH) {
16212 struct triple *branch, *test;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016213 struct triple *left, *right;
16214 left = right = 0;
16215 left = RHS(cmp, 0);
16216 if (TRIPLE_RHS(cmp->sizes) > 1) {
16217 right = RHS(cmp, 1);
16218 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016219 branch = entry->member;
16220 test = pre_triple(state, branch,
Eric Biederman0babc1c2003-05-09 02:39:00 +000016221 cmp->op, cmp->type, left, right);
Eric Biederman530b5192003-07-01 10:05:30 +000016222 test->template_id = TEMPLATE_TEST32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016223 if (cmp->op == OP_CMP) {
Eric Biederman530b5192003-07-01 10:05:30 +000016224 test->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016225 if (get_imm32(test, &RHS(test, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000016226 test->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016227 }
16228 }
16229 use_triple(RHS(test, 0), test);
16230 use_triple(RHS(test, 1), test);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016231 unuse_triple(RHS(branch, 0), branch);
16232 RHS(branch, 0) = test;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016233 branch->op = jmp_op;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016234 branch->template_id = TEMPLATE_JMP;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016235 use_triple(RHS(branch, 0), branch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016236 }
16237 }
16238}
16239
16240static void bool_cmp(struct compile_state *state,
16241 struct triple *ins, int cmp_op, int jmp_op, int set_op)
16242{
Eric Biedermanb138ac82003-04-22 18:44:01 +000016243 struct triple_set *entry, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016244 struct triple *set;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016245
16246 /* Put a barrier up before the cmp which preceeds the
16247 * copy instruction. If a set actually occurs this gives
16248 * us a chance to move variables in registers out of the way.
16249 */
16250
16251 /* Modify the comparison operator */
16252 ins->op = cmp_op;
Eric Biederman530b5192003-07-01 10:05:30 +000016253 ins->template_id = TEMPLATE_TEST32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016254 if (cmp_op == OP_CMP) {
Eric Biederman530b5192003-07-01 10:05:30 +000016255 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016256 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000016257 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016258 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016259 }
16260 /* Generate the instruction sequence that will transform the
16261 * result of the comparison into a logical value.
16262 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000016263 set = post_triple(state, ins, set_op, &char_type, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016264 use_triple(ins, set);
16265 set->template_id = TEMPLATE_SET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016266
Eric Biedermanb138ac82003-04-22 18:44:01 +000016267 for(entry = ins->use; entry; entry = next) {
16268 next = entry->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016269 if (entry->member == set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016270 continue;
16271 }
16272 replace_rhs_use(state, ins, set, entry->member);
16273 }
16274 fixup_branches(state, ins, set, jmp_op);
16275}
16276
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016277static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
Eric Biederman0babc1c2003-05-09 02:39:00 +000016278{
16279 struct triple *next;
16280 int lhs, i;
16281 lhs = TRIPLE_LHS(ins->sizes);
16282 for(next = ins->next, i = 0; i < lhs; i++, next = next->next) {
16283 if (next != LHS(ins, i)) {
16284 internal_error(state, ins, "malformed lhs on %s",
16285 tops(ins->op));
16286 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016287 if (next->op != OP_PIECE) {
16288 internal_error(state, ins, "bad lhs op %s at %d on %s",
16289 tops(next->op), i, tops(ins->op));
16290 }
16291 if (next->u.cval != i) {
16292 internal_error(state, ins, "bad u.cval of %d %d expected",
16293 next->u.cval, i);
16294 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016295 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016296 return next;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016297}
Eric Biedermanb138ac82003-04-22 18:44:01 +000016298
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016299struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
16300{
16301 struct ins_template *template;
16302 struct reg_info result;
16303 int zlhs;
16304 if (ins->op == OP_PIECE) {
16305 index = ins->u.cval;
16306 ins = MISC(ins, 0);
16307 }
16308 zlhs = TRIPLE_LHS(ins->sizes);
16309 if (triple_is_def(state, ins)) {
16310 zlhs = 1;
16311 }
16312 if (index >= zlhs) {
16313 internal_error(state, ins, "index %d out of range for %s\n",
16314 index, tops(ins->op));
16315 }
16316 switch(ins->op) {
16317 case OP_ASM:
16318 template = &ins->u.ainfo->tmpl;
16319 break;
16320 default:
16321 if (ins->template_id > LAST_TEMPLATE) {
16322 internal_error(state, ins, "bad template number %d",
16323 ins->template_id);
16324 }
16325 template = &templates[ins->template_id];
16326 break;
16327 }
16328 result = template->lhs[index];
16329 result.regcm = arch_regcm_normalize(state, result.regcm);
16330 if (result.reg != REG_UNNEEDED) {
16331 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
16332 }
16333 if (result.regcm == 0) {
16334 internal_error(state, ins, "lhs %d regcm == 0", index);
16335 }
16336 return result;
16337}
16338
16339struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
16340{
16341 struct reg_info result;
16342 struct ins_template *template;
16343 if ((index > TRIPLE_RHS(ins->sizes)) ||
16344 (ins->op == OP_PIECE)) {
16345 internal_error(state, ins, "index %d out of range for %s\n",
16346 index, tops(ins->op));
16347 }
16348 switch(ins->op) {
16349 case OP_ASM:
16350 template = &ins->u.ainfo->tmpl;
16351 break;
16352 default:
16353 if (ins->template_id > LAST_TEMPLATE) {
16354 internal_error(state, ins, "bad template number %d",
16355 ins->template_id);
16356 }
16357 template = &templates[ins->template_id];
16358 break;
16359 }
16360 result = template->rhs[index];
16361 result.regcm = arch_regcm_normalize(state, result.regcm);
16362 if (result.regcm == 0) {
16363 internal_error(state, ins, "rhs %d regcm == 0", index);
16364 }
16365 return result;
16366}
16367
Eric Biederman530b5192003-07-01 10:05:30 +000016368static struct triple *mod_div(struct compile_state *state,
16369 struct triple *ins, int div_op, int index)
16370{
16371 struct triple *div, *piece0, *piece1;
16372
16373 /* Generate a piece to hold the remainder */
16374 piece1 = post_triple(state, ins, OP_PIECE, ins->type, 0, 0);
16375 piece1->u.cval = 1;
16376
16377 /* Generate a piece to hold the quotient */
16378 piece0 = post_triple(state, ins, OP_PIECE, ins->type, 0, 0);
16379 piece0->u.cval = 0;
16380
16381 /* Generate the appropriate division instruction */
16382 div = post_triple(state, ins, div_op, ins->type, 0, 0);
16383 RHS(div, 0) = RHS(ins, 0);
16384 RHS(div, 1) = RHS(ins, 1);
16385 LHS(div, 0) = piece0;
16386 LHS(div, 1) = piece1;
16387 div->template_id = TEMPLATE_DIV32;
16388 use_triple(RHS(div, 0), div);
16389 use_triple(RHS(div, 1), div);
16390 use_triple(LHS(div, 0), div);
16391 use_triple(LHS(div, 1), div);
16392
16393 /* Hook on piece0 */
16394 MISC(piece0, 0) = div;
16395 use_triple(div, piece0);
16396
16397 /* Hook on piece1 */
16398 MISC(piece1, 0) = div;
16399 use_triple(div, piece1);
16400
16401 /* Replate uses of ins with the appropriate piece of the div */
16402 propogate_use(state, ins, LHS(div, index));
16403 release_triple(state, ins);
16404
16405 /* Return the address of the next instruction */
16406 return piece1->next;
16407}
16408
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016409static struct triple *transform_to_arch_instruction(
16410 struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016411{
16412 /* Transform from generic 3 address instructions
16413 * to archtecture specific instructions.
Eric Biedermand1ea5392003-06-28 06:49:45 +000016414 * And apply architecture specific constraints to instructions.
Eric Biedermanb138ac82003-04-22 18:44:01 +000016415 * Copies are inserted to preserve the register flexibility
16416 * of 3 address instructions.
16417 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016418 struct triple *next;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016419 size_t size;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016420 next = ins->next;
16421 switch(ins->op) {
16422 case OP_INTCONST:
16423 ins->template_id = TEMPLATE_INTCONST32;
16424 if (ins->u.cval < 256) {
16425 ins->template_id = TEMPLATE_INTCONST8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016426 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016427 break;
16428 case OP_ADDRCONST:
16429 ins->template_id = TEMPLATE_INTCONST32;
16430 break;
16431 case OP_NOOP:
16432 case OP_SDECL:
16433 case OP_BLOBCONST:
16434 case OP_LABEL:
16435 ins->template_id = TEMPLATE_NOP;
16436 break;
16437 case OP_COPY:
Eric Biedermand1ea5392003-06-28 06:49:45 +000016438 size = size_of(state, ins->type);
16439 if (is_imm8(RHS(ins, 0)) && (size <= 1)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016440 ins->template_id = TEMPLATE_COPY_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016441 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000016442 else if (is_imm16(RHS(ins, 0)) && (size <= 2)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016443 ins->template_id = TEMPLATE_COPY_IMM16;
16444 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000016445 else if (is_imm32(RHS(ins, 0)) && (size <= 4)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016446 ins->template_id = TEMPLATE_COPY_IMM32;
16447 }
16448 else if (is_const(RHS(ins, 0))) {
16449 internal_error(state, ins, "bad constant passed to copy");
16450 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000016451 else if (size <= 1) {
16452 ins->template_id = TEMPLATE_COPY8_REG;
16453 }
16454 else if (size <= 2) {
16455 ins->template_id = TEMPLATE_COPY16_REG;
16456 }
16457 else if (size <= 4) {
16458 ins->template_id = TEMPLATE_COPY32_REG;
16459 }
16460 else {
16461 internal_error(state, ins, "bad type passed to copy");
16462 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016463 break;
16464 case OP_PHI:
Eric Biedermand1ea5392003-06-28 06:49:45 +000016465 size = size_of(state, ins->type);
16466 if (size <= 1) {
16467 ins->template_id = TEMPLATE_PHI8;
16468 }
16469 else if (size <= 2) {
16470 ins->template_id = TEMPLATE_PHI16;
16471 }
16472 else if (size <= 4) {
16473 ins->template_id = TEMPLATE_PHI32;
16474 }
16475 else {
16476 internal_error(state, ins, "bad type passed to phi");
16477 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016478 break;
16479 case OP_STORE:
16480 switch(ins->type->type & TYPE_MASK) {
16481 case TYPE_CHAR: case TYPE_UCHAR:
16482 ins->template_id = TEMPLATE_STORE8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016483 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016484 case TYPE_SHORT: case TYPE_USHORT:
16485 ins->template_id = TEMPLATE_STORE16;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016486 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016487 case TYPE_INT: case TYPE_UINT:
16488 case TYPE_LONG: case TYPE_ULONG:
16489 case TYPE_POINTER:
16490 ins->template_id = TEMPLATE_STORE32;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016491 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016492 default:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016493 internal_error(state, ins, "unknown type in store");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016494 break;
16495 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016496 break;
16497 case OP_LOAD:
16498 switch(ins->type->type & TYPE_MASK) {
16499 case TYPE_CHAR: case TYPE_UCHAR:
16500 ins->template_id = TEMPLATE_LOAD8;
16501 break;
16502 case TYPE_SHORT:
16503 case TYPE_USHORT:
16504 ins->template_id = TEMPLATE_LOAD16;
16505 break;
16506 case TYPE_INT:
16507 case TYPE_UINT:
16508 case TYPE_LONG:
16509 case TYPE_ULONG:
16510 case TYPE_POINTER:
16511 ins->template_id = TEMPLATE_LOAD32;
16512 break;
16513 default:
16514 internal_error(state, ins, "unknown type in load");
16515 break;
16516 }
16517 break;
16518 case OP_ADD:
16519 case OP_SUB:
16520 case OP_AND:
16521 case OP_XOR:
16522 case OP_OR:
16523 case OP_SMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000016524 ins->template_id = TEMPLATE_BINARY32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016525 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000016526 ins->template_id = TEMPLATE_BINARY32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016527 }
16528 break;
Eric Biederman530b5192003-07-01 10:05:30 +000016529 case OP_SDIVT:
16530 case OP_UDIVT:
16531 ins->template_id = TEMPLATE_DIV32;
16532 next = after_lhs(state, ins);
16533 break;
16534 /* FIXME UMUL does not work yet.. */
Eric Biedermand1ea5392003-06-28 06:49:45 +000016535 case OP_UMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000016536 ins->template_id = TEMPLATE_UMUL32;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016537 break;
16538 case OP_UDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000016539 next = mod_div(state, ins, OP_UDIVT, 0);
16540 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016541 case OP_SDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000016542 next = mod_div(state, ins, OP_SDIVT, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016543 break;
16544 case OP_UMOD:
Eric Biederman530b5192003-07-01 10:05:30 +000016545 next = mod_div(state, ins, OP_UDIVT, 1);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016546 break;
Eric Biederman530b5192003-07-01 10:05:30 +000016547 case OP_SMOD:
16548 next = mod_div(state, ins, OP_SDIVT, 1);
16549 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016550 case OP_SL:
16551 case OP_SSR:
16552 case OP_USR:
Eric Biederman530b5192003-07-01 10:05:30 +000016553 ins->template_id = TEMPLATE_SL32_CL;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016554 if (get_imm8(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000016555 ins->template_id = TEMPLATE_SL32_IMM;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016556 } else if (size_of(state, RHS(ins, 1)->type) > 1) {
16557 typed_pre_copy(state, &char_type, ins, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016558 }
16559 break;
16560 case OP_INVERT:
16561 case OP_NEG:
Eric Biederman530b5192003-07-01 10:05:30 +000016562 ins->template_id = TEMPLATE_UNARY32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016563 break;
16564 case OP_EQ:
16565 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
16566 break;
16567 case OP_NOTEQ:
16568 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
16569 break;
16570 case OP_SLESS:
16571 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
16572 break;
16573 case OP_ULESS:
16574 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
16575 break;
16576 case OP_SMORE:
16577 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
16578 break;
16579 case OP_UMORE:
16580 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
16581 break;
16582 case OP_SLESSEQ:
16583 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
16584 break;
16585 case OP_ULESSEQ:
16586 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
16587 break;
16588 case OP_SMOREEQ:
16589 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
16590 break;
16591 case OP_UMOREEQ:
16592 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
16593 break;
16594 case OP_LTRUE:
16595 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
16596 break;
16597 case OP_LFALSE:
16598 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
16599 break;
16600 case OP_BRANCH:
16601 if (TRIPLE_RHS(ins->sizes) > 0) {
16602 internal_error(state, ins, "bad branch test");
16603 }
16604 ins->op = OP_JMP;
16605 ins->template_id = TEMPLATE_NOP;
16606 break;
16607 case OP_INB:
16608 case OP_INW:
16609 case OP_INL:
16610 switch(ins->op) {
16611 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
16612 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
16613 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
16614 }
16615 if (get_imm8(ins, &RHS(ins, 0))) {
16616 ins->template_id += 1;
16617 }
16618 break;
16619 case OP_OUTB:
16620 case OP_OUTW:
16621 case OP_OUTL:
16622 switch(ins->op) {
16623 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
16624 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
16625 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
16626 }
16627 if (get_imm8(ins, &RHS(ins, 1))) {
16628 ins->template_id += 1;
16629 }
16630 break;
16631 case OP_BSF:
16632 case OP_BSR:
16633 ins->template_id = TEMPLATE_BSF;
16634 break;
16635 case OP_RDMSR:
16636 ins->template_id = TEMPLATE_RDMSR;
16637 next = after_lhs(state, ins);
16638 break;
16639 case OP_WRMSR:
16640 ins->template_id = TEMPLATE_WRMSR;
16641 break;
16642 case OP_HLT:
16643 ins->template_id = TEMPLATE_NOP;
16644 break;
16645 case OP_ASM:
16646 ins->template_id = TEMPLATE_NOP;
16647 next = after_lhs(state, ins);
16648 break;
16649 /* Already transformed instructions */
16650 case OP_TEST:
Eric Biederman530b5192003-07-01 10:05:30 +000016651 ins->template_id = TEMPLATE_TEST32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016652 break;
16653 case OP_CMP:
Eric Biederman530b5192003-07-01 10:05:30 +000016654 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016655 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000016656 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016657 }
16658 break;
16659 case OP_JMP_EQ: case OP_JMP_NOTEQ:
16660 case OP_JMP_SLESS: case OP_JMP_ULESS:
16661 case OP_JMP_SMORE: case OP_JMP_UMORE:
16662 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
16663 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
16664 ins->template_id = TEMPLATE_JMP;
16665 break;
16666 case OP_SET_EQ: case OP_SET_NOTEQ:
16667 case OP_SET_SLESS: case OP_SET_ULESS:
16668 case OP_SET_SMORE: case OP_SET_UMORE:
16669 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
16670 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
16671 ins->template_id = TEMPLATE_SET;
16672 break;
16673 /* Unhandled instructions */
16674 case OP_PIECE:
16675 default:
16676 internal_error(state, ins, "unhandled ins: %d %s\n",
16677 ins->op, tops(ins->op));
16678 break;
16679 }
16680 return next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016681}
16682
Eric Biederman530b5192003-07-01 10:05:30 +000016683static long next_label(struct compile_state *state)
16684{
16685 static long label_counter = 0;
16686 return ++label_counter;
16687}
Eric Biedermanb138ac82003-04-22 18:44:01 +000016688static void generate_local_labels(struct compile_state *state)
16689{
16690 struct triple *first, *label;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016691 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016692 label = first;
16693 do {
16694 if ((label->op == OP_LABEL) ||
16695 (label->op == OP_SDECL)) {
16696 if (label->use) {
Eric Biederman530b5192003-07-01 10:05:30 +000016697 label->u.cval = next_label(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016698 } else {
16699 label->u.cval = 0;
16700 }
16701
16702 }
16703 label = label->next;
16704 } while(label != first);
16705}
16706
16707static int check_reg(struct compile_state *state,
16708 struct triple *triple, int classes)
16709{
16710 unsigned mask;
16711 int reg;
16712 reg = ID_REG(triple->id);
16713 if (reg == REG_UNSET) {
16714 internal_error(state, triple, "register not set");
16715 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016716 mask = arch_reg_regcm(state, reg);
16717 if (!(classes & mask)) {
16718 internal_error(state, triple, "reg %d in wrong class",
16719 reg);
16720 }
16721 return reg;
16722}
16723
16724static const char *arch_reg_str(int reg)
16725{
Eric Biederman530b5192003-07-01 10:05:30 +000016726#if REG_XMM7 != 44
16727#error "Registers have renumberd fix arch_reg_str"
16728#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016729 static const char *regs[] = {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000016730 "%unset",
16731 "%unneeded",
Eric Biedermanb138ac82003-04-22 18:44:01 +000016732 "%eflags",
16733 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
16734 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
16735 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
16736 "%edx:%eax",
Eric Biederman530b5192003-07-01 10:05:30 +000016737 "%dx:%ax",
Eric Biedermanb138ac82003-04-22 18:44:01 +000016738 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
16739 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
16740 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
16741 };
16742 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
16743 reg = 0;
16744 }
16745 return regs[reg];
16746}
16747
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016748
Eric Biedermanb138ac82003-04-22 18:44:01 +000016749static const char *reg(struct compile_state *state, struct triple *triple,
16750 int classes)
16751{
16752 int reg;
16753 reg = check_reg(state, triple, classes);
16754 return arch_reg_str(reg);
16755}
16756
16757const char *type_suffix(struct compile_state *state, struct type *type)
16758{
16759 const char *suffix;
16760 switch(size_of(state, type)) {
16761 case 1: suffix = "b"; break;
16762 case 2: suffix = "w"; break;
16763 case 4: suffix = "l"; break;
16764 default:
16765 internal_error(state, 0, "unknown suffix");
16766 suffix = 0;
16767 break;
16768 }
16769 return suffix;
16770}
16771
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016772static void print_const_val(
16773 struct compile_state *state, struct triple *ins, FILE *fp)
16774{
16775 switch(ins->op) {
16776 case OP_INTCONST:
16777 fprintf(fp, " $%ld ",
16778 (long_t)(ins->u.cval));
16779 break;
16780 case OP_ADDRCONST:
Eric Biederman05f26fc2003-06-11 21:55:00 +000016781 fprintf(fp, " $L%s%lu+%lu ",
16782 state->label_prefix,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016783 MISC(ins, 0)->u.cval,
16784 ins->u.cval);
16785 break;
16786 default:
16787 internal_error(state, ins, "unknown constant type");
16788 break;
16789 }
16790}
16791
Eric Biederman530b5192003-07-01 10:05:30 +000016792static void print_const(struct compile_state *state,
16793 struct triple *ins, FILE *fp)
16794{
16795 switch(ins->op) {
16796 case OP_INTCONST:
16797 switch(ins->type->type & TYPE_MASK) {
16798 case TYPE_CHAR:
16799 case TYPE_UCHAR:
16800 fprintf(fp, ".byte 0x%02lx\n", ins->u.cval);
16801 break;
16802 case TYPE_SHORT:
16803 case TYPE_USHORT:
16804 fprintf(fp, ".short 0x%04lx\n", ins->u.cval);
16805 break;
16806 case TYPE_INT:
16807 case TYPE_UINT:
16808 case TYPE_LONG:
16809 case TYPE_ULONG:
16810 fprintf(fp, ".int %lu\n", ins->u.cval);
16811 break;
16812 default:
16813 internal_error(state, ins, "Unknown constant type");
16814 }
16815 break;
16816 case OP_ADDRCONST:
16817 fprintf(fp, " .int L%s%lu+%lu ",
16818 state->label_prefix,
16819 MISC(ins, 0)->u.cval,
16820 ins->u.cval);
16821 break;
16822 case OP_BLOBCONST:
16823 {
16824 unsigned char *blob;
16825 size_t size, i;
16826 size = size_of(state, ins->type);
16827 blob = ins->u.blob;
16828 for(i = 0; i < size; i++) {
16829 fprintf(fp, ".byte 0x%02x\n",
16830 blob[i]);
16831 }
16832 break;
16833 }
16834 default:
16835 internal_error(state, ins, "Unknown constant type");
16836 break;
16837 }
16838}
16839
16840#define TEXT_SECTION ".rom.text"
16841#define DATA_SECTION ".rom.data"
16842
16843static long get_const_pool_ref(
16844 struct compile_state *state, struct triple *ins, FILE *fp)
16845{
16846 long ref;
16847 ref = next_label(state);
16848 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
16849 fprintf(fp, ".balign %d\n", align_of(state, ins->type));
16850 fprintf(fp, "L%s%lu:\n", state->label_prefix, ref);
16851 print_const(state, ins, fp);
16852 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
16853 return ref;
16854}
16855
Eric Biedermanb138ac82003-04-22 18:44:01 +000016856static void print_binary_op(struct compile_state *state,
16857 const char *op, struct triple *ins, FILE *fp)
16858{
16859 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000016860 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016861 if (RHS(ins, 0)->id != ins->id) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016862 internal_error(state, ins, "invalid register assignment");
16863 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016864 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016865 fprintf(fp, "\t%s ", op);
16866 print_const_val(state, RHS(ins, 1), fp);
16867 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000016868 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016869 }
16870 else {
16871 unsigned lmask, rmask;
16872 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016873 lreg = check_reg(state, RHS(ins, 0), mask);
16874 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016875 lmask = arch_reg_regcm(state, lreg);
16876 rmask = arch_reg_regcm(state, rreg);
16877 mask = lmask & rmask;
16878 fprintf(fp, "\t%s %s, %s\n",
16879 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000016880 reg(state, RHS(ins, 1), mask),
16881 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016882 }
16883}
16884static void print_unary_op(struct compile_state *state,
16885 const char *op, struct triple *ins, FILE *fp)
16886{
16887 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000016888 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016889 fprintf(fp, "\t%s %s\n",
16890 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000016891 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016892}
16893
16894static void print_op_shift(struct compile_state *state,
16895 const char *op, struct triple *ins, FILE *fp)
16896{
16897 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000016898 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016899 if (RHS(ins, 0)->id != ins->id) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016900 internal_error(state, ins, "invalid register assignment");
16901 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016902 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016903 fprintf(fp, "\t%s ", op);
16904 print_const_val(state, RHS(ins, 1), fp);
16905 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000016906 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016907 }
16908 else {
16909 fprintf(fp, "\t%s %s, %s\n",
16910 op,
Eric Biederman530b5192003-07-01 10:05:30 +000016911 reg(state, RHS(ins, 1), REGCM_GPR8_LO),
Eric Biederman0babc1c2003-05-09 02:39:00 +000016912 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016913 }
16914}
16915
16916static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
16917{
16918 const char *op;
16919 int mask;
16920 int dreg;
16921 mask = 0;
16922 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000016923 case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016924 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
16925 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
16926 default:
16927 internal_error(state, ins, "not an in operation");
16928 op = 0;
16929 break;
16930 }
16931 dreg = check_reg(state, ins, mask);
16932 if (!reg_is_reg(state, dreg, REG_EAX)) {
16933 internal_error(state, ins, "dst != %%eax");
16934 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016935 if (is_const(RHS(ins, 0))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016936 fprintf(fp, "\t%s ", op);
16937 print_const_val(state, RHS(ins, 0), fp);
16938 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000016939 reg(state, ins, mask));
16940 }
16941 else {
16942 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016943 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016944 if (!reg_is_reg(state, addr_reg, REG_DX)) {
16945 internal_error(state, ins, "src != %%dx");
16946 }
16947 fprintf(fp, "\t%s %s, %s\n",
16948 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000016949 reg(state, RHS(ins, 0), REGCM_GPR16),
Eric Biedermanb138ac82003-04-22 18:44:01 +000016950 reg(state, ins, mask));
16951 }
16952}
16953
16954static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
16955{
16956 const char *op;
16957 int mask;
16958 int lreg;
16959 mask = 0;
16960 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000016961 case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016962 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
16963 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
16964 default:
16965 internal_error(state, ins, "not an out operation");
16966 op = 0;
16967 break;
16968 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016969 lreg = check_reg(state, RHS(ins, 0), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016970 if (!reg_is_reg(state, lreg, REG_EAX)) {
16971 internal_error(state, ins, "src != %%eax");
16972 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016973 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016974 fprintf(fp, "\t%s %s,",
16975 op, reg(state, RHS(ins, 0), mask));
16976 print_const_val(state, RHS(ins, 1), fp);
16977 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016978 }
16979 else {
16980 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016981 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016982 if (!reg_is_reg(state, addr_reg, REG_DX)) {
16983 internal_error(state, ins, "dst != %%dx");
16984 }
16985 fprintf(fp, "\t%s %s, %s\n",
16986 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000016987 reg(state, RHS(ins, 0), mask),
16988 reg(state, RHS(ins, 1), REGCM_GPR16));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016989 }
16990}
16991
16992static void print_op_move(struct compile_state *state,
16993 struct triple *ins, FILE *fp)
16994{
16995 /* op_move is complex because there are many types
16996 * of registers we can move between.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016997 * Because OP_COPY will be introduced in arbitrary locations
16998 * OP_COPY must not affect flags.
Eric Biedermanb138ac82003-04-22 18:44:01 +000016999 */
17000 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
17001 struct triple *dst, *src;
17002 if (ins->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000017003 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017004 dst = ins;
17005 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017006 else {
17007 internal_error(state, ins, "unknown move operation");
17008 src = dst = 0;
17009 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017010 if (!is_const(src)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017011 int src_reg, dst_reg;
17012 int src_regcm, dst_regcm;
Eric Biederman530b5192003-07-01 10:05:30 +000017013 src_reg = ID_REG(src->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017014 dst_reg = ID_REG(dst->id);
17015 src_regcm = arch_reg_regcm(state, src_reg);
Eric Biederman530b5192003-07-01 10:05:30 +000017016 dst_regcm = arch_reg_regcm(state, dst_reg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017017 /* If the class is the same just move the register */
17018 if (src_regcm & dst_regcm &
Eric Biederman530b5192003-07-01 10:05:30 +000017019 (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017020 if ((src_reg != dst_reg) || !omit_copy) {
17021 fprintf(fp, "\tmov %s, %s\n",
17022 reg(state, src, src_regcm),
17023 reg(state, dst, dst_regcm));
17024 }
17025 }
17026 /* Move 32bit to 16bit */
17027 else if ((src_regcm & REGCM_GPR32) &&
17028 (dst_regcm & REGCM_GPR16)) {
17029 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
17030 if ((src_reg != dst_reg) || !omit_copy) {
17031 fprintf(fp, "\tmovw %s, %s\n",
17032 arch_reg_str(src_reg),
17033 arch_reg_str(dst_reg));
17034 }
17035 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017036 /* Move from 32bit gprs to 16bit gprs */
17037 else if ((src_regcm & REGCM_GPR32) &&
17038 (dst_regcm & REGCM_GPR16)) {
17039 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
17040 if ((src_reg != dst_reg) || !omit_copy) {
17041 fprintf(fp, "\tmov %s, %s\n",
17042 arch_reg_str(src_reg),
17043 arch_reg_str(dst_reg));
17044 }
17045 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017046 /* Move 32bit to 8bit */
17047 else if ((src_regcm & REGCM_GPR32_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000017048 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000017049 {
17050 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
17051 if ((src_reg != dst_reg) || !omit_copy) {
17052 fprintf(fp, "\tmovb %s, %s\n",
17053 arch_reg_str(src_reg),
17054 arch_reg_str(dst_reg));
17055 }
17056 }
17057 /* Move 16bit to 8bit */
17058 else if ((src_regcm & REGCM_GPR16_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000017059 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000017060 {
17061 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
17062 if ((src_reg != dst_reg) || !omit_copy) {
17063 fprintf(fp, "\tmovb %s, %s\n",
17064 arch_reg_str(src_reg),
17065 arch_reg_str(dst_reg));
17066 }
17067 }
17068 /* Move 8/16bit to 16/32bit */
Eric Biederman530b5192003-07-01 10:05:30 +000017069 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017070 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017071 const char *op;
17072 op = is_signed(src->type)? "movsx": "movzx";
17073 fprintf(fp, "\t%s %s, %s\n",
17074 op,
17075 reg(state, src, src_regcm),
17076 reg(state, dst, dst_regcm));
17077 }
17078 /* Move between sse registers */
17079 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
17080 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017081 fprintf(fp, "\tmovdqa %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000017082 reg(state, src, src_regcm),
17083 reg(state, dst, dst_regcm));
17084 }
17085 }
Eric Biederman530b5192003-07-01 10:05:30 +000017086 /* Move between mmx registers */
17087 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017088 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017089 fprintf(fp, "\tmovq %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000017090 reg(state, src, src_regcm),
17091 reg(state, dst, dst_regcm));
17092 }
17093 }
Eric Biederman530b5192003-07-01 10:05:30 +000017094 /* Move from sse to mmx registers */
17095 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
17096 fprintf(fp, "\tmovdq2q %s, %s\n",
17097 reg(state, src, src_regcm),
17098 reg(state, dst, dst_regcm));
17099 }
17100 /* Move from mmx to sse registers */
17101 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
17102 fprintf(fp, "\tmovq2dq %s, %s\n",
17103 reg(state, src, src_regcm),
17104 reg(state, dst, dst_regcm));
17105 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017106 /* Move between 32bit gprs & mmx/sse registers */
17107 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
17108 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
17109 fprintf(fp, "\tmovd %s, %s\n",
17110 reg(state, src, src_regcm),
17111 reg(state, dst, dst_regcm));
17112 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017113 /* Move from 16bit gprs & mmx/sse registers */
17114 else if ((src_regcm & REGCM_GPR16) &&
17115 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
17116 const char *op;
17117 int mid_reg;
Eric Biederman678d8162003-07-03 03:59:38 +000017118 op = is_signed(src->type)? "movsx":"movzx";
Eric Biedermand1ea5392003-06-28 06:49:45 +000017119 mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
17120 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
17121 op,
17122 arch_reg_str(src_reg),
17123 arch_reg_str(mid_reg),
17124 arch_reg_str(mid_reg),
17125 arch_reg_str(dst_reg));
17126 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017127 /* Move from mmx/sse registers to 16bit gprs */
17128 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
17129 (dst_regcm & REGCM_GPR16)) {
17130 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
17131 fprintf(fp, "\tmovd %s, %s\n",
17132 arch_reg_str(src_reg),
17133 arch_reg_str(dst_reg));
17134 }
Eric Biederman530b5192003-07-01 10:05:30 +000017135 /* Move from gpr to 64bit dividend */
17136 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) &&
17137 (dst_regcm & REGCM_DIVIDEND64)) {
17138 const char *extend;
17139 extend = is_signed(src->type)? "cltd":"movl $0, %edx";
17140 fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
17141 arch_reg_str(src_reg),
17142 extend);
17143 }
17144 /* Move from 64bit gpr to gpr */
17145 else if ((src_regcm & REGCM_DIVIDEND64) &&
17146 (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
17147 if (dst_regcm & REGCM_GPR32) {
17148 src_reg = REG_EAX;
17149 }
17150 else if (dst_regcm & REGCM_GPR16) {
17151 src_reg = REG_AX;
17152 }
17153 else if (dst_regcm & REGCM_GPR8_LO) {
17154 src_reg = REG_AL;
17155 }
17156 fprintf(fp, "\tmov %s, %s\n",
17157 arch_reg_str(src_reg),
17158 arch_reg_str(dst_reg));
17159 }
17160 /* Move from mmx/sse registers to 64bit gpr */
17161 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
17162 (dst_regcm & REGCM_DIVIDEND64)) {
17163 const char *extend;
17164 extend = is_signed(src->type)? "cltd": "movl $0, %edx";
17165 fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
17166 arch_reg_str(src_reg),
17167 extend);
17168 }
17169 /* Move from 64bit gpr to mmx/sse register */
17170 else if ((src_regcm & REGCM_DIVIDEND64) &&
17171 (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
17172 fprintf(fp, "\tmovd %%eax, %s\n",
17173 arch_reg_str(dst_reg));
17174 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017175#if X86_4_8BIT_GPRS
17176 /* Move from 8bit gprs to mmx/sse registers */
Eric Biederman530b5192003-07-01 10:05:30 +000017177 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017178 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
17179 const char *op;
17180 int mid_reg;
17181 op = is_signed(src->type)? "movsx":"movzx";
17182 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
17183 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
17184 op,
17185 reg(state, src, src_regcm),
17186 arch_reg_str(mid_reg),
17187 arch_reg_str(mid_reg),
17188 reg(state, dst, dst_regcm));
17189 }
17190 /* Move from mmx/sse registers and 8bit gprs */
17191 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
Eric Biederman530b5192003-07-01 10:05:30 +000017192 (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017193 int mid_reg;
17194 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
17195 fprintf(fp, "\tmovd %s, %s\n",
17196 reg(state, src, src_regcm),
17197 arch_reg_str(mid_reg));
17198 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017199 /* Move from 32bit gprs to 8bit gprs */
17200 else if ((src_regcm & REGCM_GPR32) &&
Eric Biederman530b5192003-07-01 10:05:30 +000017201 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017202 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
17203 if ((src_reg != dst_reg) || !omit_copy) {
17204 fprintf(fp, "\tmov %s, %s\n",
17205 arch_reg_str(src_reg),
17206 arch_reg_str(dst_reg));
17207 }
17208 }
17209 /* Move from 16bit gprs to 8bit gprs */
17210 else if ((src_regcm & REGCM_GPR16) &&
Eric Biederman530b5192003-07-01 10:05:30 +000017211 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017212 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
17213 if ((src_reg != dst_reg) || !omit_copy) {
17214 fprintf(fp, "\tmov %s, %s\n",
17215 arch_reg_str(src_reg),
17216 arch_reg_str(dst_reg));
17217 }
17218 }
17219#endif /* X86_4_8BIT_GPRS */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017220 else {
17221 internal_error(state, ins, "unknown copy type");
17222 }
17223 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017224 else {
Eric Biederman530b5192003-07-01 10:05:30 +000017225 int dst_reg;
17226 int dst_regcm;
17227 dst_reg = ID_REG(dst->id);
17228 dst_regcm = arch_reg_regcm(state, dst_reg);
17229 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
17230 fprintf(fp, "\tmov ");
17231 print_const_val(state, src, fp);
17232 fprintf(fp, ", %s\n",
17233 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
17234 }
17235 else if (dst_regcm & REGCM_DIVIDEND64) {
17236 if (size_of(state, dst->type) > 4) {
17237 internal_error(state, ins, "64bit constant...");
17238 }
17239 fprintf(fp, "\tmov $0, %%edx\n");
17240 fprintf(fp, "\tmov ");
17241 print_const_val(state, src, fp);
17242 fprintf(fp, ", %%eax\n");
17243 }
17244 else if (dst_regcm & REGCM_DIVIDEND32) {
17245 if (size_of(state, dst->type) > 2) {
17246 internal_error(state, ins, "32bit constant...");
17247 }
17248 fprintf(fp, "\tmov $0, %%dx\n");
17249 fprintf(fp, "\tmov ");
17250 print_const_val(state, src, fp);
17251 fprintf(fp, ", %%ax");
17252 }
17253 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
17254 long ref;
17255 ref = get_const_pool_ref(state, src, fp);
17256 fprintf(fp, "\tmovq L%s%lu, %s\n",
17257 state->label_prefix, ref,
17258 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
17259 }
17260 else {
17261 internal_error(state, ins, "unknown copy immediate type");
17262 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017263 }
17264}
17265
17266static void print_op_load(struct compile_state *state,
17267 struct triple *ins, FILE *fp)
17268{
17269 struct triple *dst, *src;
17270 dst = ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017271 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017272 if (is_const(src) || is_const(dst)) {
17273 internal_error(state, ins, "unknown load operation");
17274 }
17275 fprintf(fp, "\tmov (%s), %s\n",
17276 reg(state, src, REGCM_GPR32),
Eric Biederman530b5192003-07-01 10:05:30 +000017277 reg(state, dst, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000017278}
17279
17280
17281static void print_op_store(struct compile_state *state,
17282 struct triple *ins, FILE *fp)
17283{
17284 struct triple *dst, *src;
Eric Biederman530b5192003-07-01 10:05:30 +000017285 dst = RHS(ins, 0);
17286 src = RHS(ins, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017287 if (is_const(src) && (src->op == OP_INTCONST)) {
17288 long_t value;
17289 value = (long_t)(src->u.cval);
17290 fprintf(fp, "\tmov%s $%ld, (%s)\n",
17291 type_suffix(state, src->type),
17292 value,
17293 reg(state, dst, REGCM_GPR32));
17294 }
17295 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
17296 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
17297 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000017298 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000017299 dst->u.cval);
17300 }
17301 else {
17302 if (is_const(src) || is_const(dst)) {
17303 internal_error(state, ins, "unknown store operation");
17304 }
17305 fprintf(fp, "\tmov%s %s, (%s)\n",
17306 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000017307 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000017308 reg(state, dst, REGCM_GPR32));
17309 }
17310
17311
17312}
17313
17314static void print_op_smul(struct compile_state *state,
17315 struct triple *ins, FILE *fp)
17316{
Eric Biederman0babc1c2003-05-09 02:39:00 +000017317 if (!is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017318 fprintf(fp, "\timul %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000017319 reg(state, RHS(ins, 1), REGCM_GPR32),
17320 reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000017321 }
17322 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017323 fprintf(fp, "\timul ");
17324 print_const_val(state, RHS(ins, 1), fp);
17325 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000017326 }
17327}
17328
17329static void print_op_cmp(struct compile_state *state,
17330 struct triple *ins, FILE *fp)
17331{
17332 unsigned mask;
17333 int dreg;
Eric Biederman530b5192003-07-01 10:05:30 +000017334 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017335 dreg = check_reg(state, ins, REGCM_FLAGS);
17336 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
17337 internal_error(state, ins, "bad dest register for cmp");
17338 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017339 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017340 fprintf(fp, "\tcmp ");
17341 print_const_val(state, RHS(ins, 1), fp);
17342 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000017343 }
17344 else {
17345 unsigned lmask, rmask;
17346 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017347 lreg = check_reg(state, RHS(ins, 0), mask);
17348 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017349 lmask = arch_reg_regcm(state, lreg);
17350 rmask = arch_reg_regcm(state, rreg);
17351 mask = lmask & rmask;
17352 fprintf(fp, "\tcmp %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000017353 reg(state, RHS(ins, 1), mask),
17354 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000017355 }
17356}
17357
17358static void print_op_test(struct compile_state *state,
17359 struct triple *ins, FILE *fp)
17360{
17361 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000017362 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017363 fprintf(fp, "\ttest %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000017364 reg(state, RHS(ins, 0), mask),
17365 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000017366}
17367
17368static void print_op_branch(struct compile_state *state,
17369 struct triple *branch, FILE *fp)
17370{
17371 const char *bop = "j";
17372 if (branch->op == OP_JMP) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000017373 if (TRIPLE_RHS(branch->sizes) != 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017374 internal_error(state, branch, "jmp with condition?");
17375 }
17376 bop = "jmp";
17377 }
17378 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017379 struct triple *ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017380 if (TRIPLE_RHS(branch->sizes) != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017381 internal_error(state, branch, "jmpcc without condition?");
17382 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017383 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
17384 if ((RHS(branch, 0)->op != OP_CMP) &&
17385 (RHS(branch, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017386 internal_error(state, branch, "bad branch test");
17387 }
17388#warning "FIXME I have observed instructions between the test and branch instructions"
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017389 ptr = RHS(branch, 0);
17390 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
17391 if (ptr->op != OP_COPY) {
17392 internal_error(state, branch, "branch does not follow test");
17393 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017394 }
17395 switch(branch->op) {
17396 case OP_JMP_EQ: bop = "jz"; break;
17397 case OP_JMP_NOTEQ: bop = "jnz"; break;
17398 case OP_JMP_SLESS: bop = "jl"; break;
17399 case OP_JMP_ULESS: bop = "jb"; break;
17400 case OP_JMP_SMORE: bop = "jg"; break;
17401 case OP_JMP_UMORE: bop = "ja"; break;
17402 case OP_JMP_SLESSEQ: bop = "jle"; break;
17403 case OP_JMP_ULESSEQ: bop = "jbe"; break;
17404 case OP_JMP_SMOREEQ: bop = "jge"; break;
17405 case OP_JMP_UMOREEQ: bop = "jae"; break;
17406 default:
17407 internal_error(state, branch, "Invalid branch op");
17408 break;
17409 }
17410
17411 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000017412 fprintf(fp, "\t%s L%s%lu\n",
17413 bop,
17414 state->label_prefix,
17415 TARG(branch, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017416}
17417
17418static void print_op_set(struct compile_state *state,
17419 struct triple *set, FILE *fp)
17420{
17421 const char *sop = "set";
Eric Biederman0babc1c2003-05-09 02:39:00 +000017422 if (TRIPLE_RHS(set->sizes) != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017423 internal_error(state, set, "setcc without condition?");
17424 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017425 check_reg(state, RHS(set, 0), REGCM_FLAGS);
17426 if ((RHS(set, 0)->op != OP_CMP) &&
17427 (RHS(set, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017428 internal_error(state, set, "bad set test");
17429 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017430 if (RHS(set, 0)->next != set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017431 internal_error(state, set, "set does not follow test");
17432 }
17433 switch(set->op) {
17434 case OP_SET_EQ: sop = "setz"; break;
17435 case OP_SET_NOTEQ: sop = "setnz"; break;
17436 case OP_SET_SLESS: sop = "setl"; break;
17437 case OP_SET_ULESS: sop = "setb"; break;
17438 case OP_SET_SMORE: sop = "setg"; break;
17439 case OP_SET_UMORE: sop = "seta"; break;
17440 case OP_SET_SLESSEQ: sop = "setle"; break;
17441 case OP_SET_ULESSEQ: sop = "setbe"; break;
17442 case OP_SET_SMOREEQ: sop = "setge"; break;
17443 case OP_SET_UMOREEQ: sop = "setae"; break;
17444 default:
17445 internal_error(state, set, "Invalid set op");
17446 break;
17447 }
17448 fprintf(fp, "\t%s %s\n",
Eric Biederman530b5192003-07-01 10:05:30 +000017449 sop, reg(state, set, REGCM_GPR8_LO));
Eric Biedermanb138ac82003-04-22 18:44:01 +000017450}
17451
17452static void print_op_bit_scan(struct compile_state *state,
17453 struct triple *ins, FILE *fp)
17454{
17455 const char *op;
17456 switch(ins->op) {
17457 case OP_BSF: op = "bsf"; break;
17458 case OP_BSR: op = "bsr"; break;
17459 default:
17460 internal_error(state, ins, "unknown bit scan");
17461 op = 0;
17462 break;
17463 }
17464 fprintf(fp,
17465 "\t%s %s, %s\n"
17466 "\tjnz 1f\n"
17467 "\tmovl $-1, %s\n"
17468 "1:\n",
17469 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000017470 reg(state, RHS(ins, 0), REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000017471 reg(state, ins, REGCM_GPR32),
17472 reg(state, ins, REGCM_GPR32));
17473}
17474
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017475
Eric Biedermanb138ac82003-04-22 18:44:01 +000017476static void print_sdecl(struct compile_state *state,
17477 struct triple *ins, FILE *fp)
17478{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017479 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000017480 fprintf(fp, ".balign %d\n", align_of(state, ins->type));
Eric Biederman05f26fc2003-06-11 21:55:00 +000017481 fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
Eric Biederman0babc1c2003-05-09 02:39:00 +000017482 print_const(state, MISC(ins, 0), fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017483 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000017484
17485}
17486
17487static void print_instruction(struct compile_state *state,
17488 struct triple *ins, FILE *fp)
17489{
17490 /* Assumption: after I have exted the register allocator
17491 * everything is in a valid register.
17492 */
17493 switch(ins->op) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017494 case OP_ASM:
17495 print_op_asm(state, ins, fp);
17496 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017497 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
17498 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
17499 case OP_AND: print_binary_op(state, "and", ins, fp); break;
17500 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
17501 case OP_OR: print_binary_op(state, "or", ins, fp); break;
17502 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
17503 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
17504 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
17505 case OP_POS: break;
17506 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
17507 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
17508 case OP_INTCONST:
17509 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017510 case OP_BLOBCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +000017511 /* Don't generate anything here for constants */
17512 case OP_PHI:
17513 /* Don't generate anything for variable declarations. */
17514 break;
17515 case OP_SDECL:
17516 print_sdecl(state, ins, fp);
17517 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017518 case OP_COPY:
17519 print_op_move(state, ins, fp);
17520 break;
17521 case OP_LOAD:
17522 print_op_load(state, ins, fp);
17523 break;
17524 case OP_STORE:
17525 print_op_store(state, ins, fp);
17526 break;
17527 case OP_SMUL:
17528 print_op_smul(state, ins, fp);
17529 break;
17530 case OP_CMP: print_op_cmp(state, ins, fp); break;
17531 case OP_TEST: print_op_test(state, ins, fp); break;
17532 case OP_JMP:
17533 case OP_JMP_EQ: case OP_JMP_NOTEQ:
17534 case OP_JMP_SLESS: case OP_JMP_ULESS:
17535 case OP_JMP_SMORE: case OP_JMP_UMORE:
17536 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
17537 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
17538 print_op_branch(state, ins, fp);
17539 break;
17540 case OP_SET_EQ: case OP_SET_NOTEQ:
17541 case OP_SET_SLESS: case OP_SET_ULESS:
17542 case OP_SET_SMORE: case OP_SET_UMORE:
17543 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
17544 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
17545 print_op_set(state, ins, fp);
17546 break;
17547 case OP_INB: case OP_INW: case OP_INL:
17548 print_op_in(state, ins, fp);
17549 break;
17550 case OP_OUTB: case OP_OUTW: case OP_OUTL:
17551 print_op_out(state, ins, fp);
17552 break;
17553 case OP_BSF:
17554 case OP_BSR:
17555 print_op_bit_scan(state, ins, fp);
17556 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017557 case OP_RDMSR:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017558 after_lhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000017559 fprintf(fp, "\trdmsr\n");
17560 break;
17561 case OP_WRMSR:
17562 fprintf(fp, "\twrmsr\n");
17563 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017564 case OP_HLT:
17565 fprintf(fp, "\thlt\n");
17566 break;
Eric Biederman530b5192003-07-01 10:05:30 +000017567 case OP_SDIVT:
17568 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
17569 break;
17570 case OP_UDIVT:
17571 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
17572 break;
17573 case OP_UMUL:
17574 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
17575 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017576 case OP_LABEL:
17577 if (!ins->use) {
17578 return;
17579 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000017580 fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017581 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017582 /* Ignore OP_PIECE */
17583 case OP_PIECE:
17584 break;
Eric Biederman530b5192003-07-01 10:05:30 +000017585 /* Operations that should never get here */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017586 case OP_SDIV: case OP_UDIV:
17587 case OP_SMOD: case OP_UMOD:
Eric Biedermanb138ac82003-04-22 18:44:01 +000017588 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
17589 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
17590 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
17591 default:
17592 internal_error(state, ins, "unknown op: %d %s",
17593 ins->op, tops(ins->op));
17594 break;
17595 }
17596}
17597
17598static void print_instructions(struct compile_state *state)
17599{
17600 struct triple *first, *ins;
17601 int print_location;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017602 struct occurance *last_occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017603 FILE *fp;
Eric Biederman530b5192003-07-01 10:05:30 +000017604 int max_inline_depth;
17605 max_inline_depth = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017606 print_location = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017607 last_occurance = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017608 fp = state->output;
17609 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biederman0babc1c2003-05-09 02:39:00 +000017610 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017611 ins = first;
17612 do {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017613 if (print_location &&
17614 last_occurance != ins->occurance) {
17615 if (!ins->occurance->parent) {
17616 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
17617 ins->occurance->function,
17618 ins->occurance->filename,
17619 ins->occurance->line,
17620 ins->occurance->col);
17621 }
17622 else {
17623 struct occurance *ptr;
Eric Biederman530b5192003-07-01 10:05:30 +000017624 int inline_depth;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017625 fprintf(fp, "\t/*\n");
Eric Biederman530b5192003-07-01 10:05:30 +000017626 inline_depth = 0;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017627 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
Eric Biederman530b5192003-07-01 10:05:30 +000017628 inline_depth++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017629 fprintf(fp, "\t * %s,%s:%d.%d\n",
17630 ptr->function,
17631 ptr->filename,
17632 ptr->line,
17633 ptr->col);
17634 }
17635 fprintf(fp, "\t */\n");
Eric Biederman530b5192003-07-01 10:05:30 +000017636 if (inline_depth > max_inline_depth) {
17637 max_inline_depth = inline_depth;
17638 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017639 }
17640 if (last_occurance) {
17641 put_occurance(last_occurance);
17642 }
17643 get_occurance(ins->occurance);
17644 last_occurance = ins->occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017645 }
17646
17647 print_instruction(state, ins, fp);
17648 ins = ins->next;
17649 } while(ins != first);
Eric Biederman530b5192003-07-01 10:05:30 +000017650 if (print_location) {
17651 fprintf(fp, "/* max inline depth %d */\n",
17652 max_inline_depth);
17653 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017654}
Eric Biederman530b5192003-07-01 10:05:30 +000017655
Eric Biedermanb138ac82003-04-22 18:44:01 +000017656static void generate_code(struct compile_state *state)
17657{
17658 generate_local_labels(state);
17659 print_instructions(state);
17660
17661}
17662
17663static void print_tokens(struct compile_state *state)
17664{
17665 struct token *tk;
17666 tk = &state->token[0];
17667 do {
17668#if 1
17669 token(state, 0);
17670#else
17671 next_token(state, 0);
17672#endif
17673 loc(stdout, state, 0);
17674 printf("%s <- `%s'\n",
17675 tokens[tk->tok],
17676 tk->ident ? tk->ident->name :
17677 tk->str_len ? tk->val.str : "");
17678
17679 } while(tk->tok != TOK_EOF);
17680}
17681
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017682static void compile(const char *filename, const char *ofilename,
Eric Biederman05f26fc2003-06-11 21:55:00 +000017683 int cpu, int debug, int opt, const char *label_prefix)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017684{
17685 int i;
17686 struct compile_state state;
17687 memset(&state, 0, sizeof(state));
17688 state.file = 0;
17689 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
17690 memset(&state.token[i], 0, sizeof(state.token[i]));
17691 state.token[i].tok = -1;
17692 }
17693 /* Remember the debug settings */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017694 state.cpu = cpu;
17695 state.debug = debug;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017696 state.optimize = opt;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017697 /* Remember the output filename */
17698 state.ofilename = ofilename;
17699 state.output = fopen(state.ofilename, "w");
17700 if (!state.output) {
17701 error(&state, 0, "Cannot open output file %s\n",
17702 ofilename);
17703 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000017704 /* Remember the label prefix */
17705 state.label_prefix = label_prefix;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017706 /* Prep the preprocessor */
17707 state.if_depth = 0;
17708 state.if_value = 0;
17709 /* register the C keywords */
17710 register_keywords(&state);
17711 /* register the keywords the macro preprocessor knows */
17712 register_macro_keywords(&state);
17713 /* Memorize where some special keywords are. */
17714 state.i_continue = lookup(&state, "continue", 8);
17715 state.i_break = lookup(&state, "break", 5);
17716 /* Enter the globl definition scope */
17717 start_scope(&state);
17718 register_builtins(&state);
17719 compile_file(&state, filename, 1);
17720#if 0
17721 print_tokens(&state);
17722#endif
17723 decls(&state);
17724 /* Exit the global definition scope */
17725 end_scope(&state);
17726
17727 /* Now that basic compilation has happened
17728 * optimize the intermediate code
17729 */
17730 optimize(&state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017731
Eric Biedermanb138ac82003-04-22 18:44:01 +000017732 generate_code(&state);
17733 if (state.debug) {
17734 fprintf(stderr, "done\n");
17735 }
17736}
17737
17738static void version(void)
17739{
17740 printf("romcc " VERSION " released " RELEASE_DATE "\n");
17741}
17742
17743static void usage(void)
17744{
17745 version();
17746 printf(
17747 "Usage: romcc <source>.c\n"
17748 "Compile a C source file without using ram\n"
17749 );
17750}
17751
17752static void arg_error(char *fmt, ...)
17753{
17754 va_list args;
17755 va_start(args, fmt);
17756 vfprintf(stderr, fmt, args);
17757 va_end(args);
17758 usage();
17759 exit(1);
17760}
17761
17762int main(int argc, char **argv)
17763{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017764 const char *filename;
17765 const char *ofilename;
Eric Biederman05f26fc2003-06-11 21:55:00 +000017766 const char *label_prefix;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017767 int cpu;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017768 int last_argc;
17769 int debug;
17770 int optimize;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017771 cpu = CPU_DEFAULT;
Eric Biederman05f26fc2003-06-11 21:55:00 +000017772 label_prefix = "";
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017773 ofilename = "auto.inc";
Eric Biedermanb138ac82003-04-22 18:44:01 +000017774 optimize = 0;
17775 debug = 0;
17776 last_argc = -1;
17777 while((argc > 1) && (argc != last_argc)) {
17778 last_argc = argc;
17779 if (strncmp(argv[1], "--debug=", 8) == 0) {
17780 debug = atoi(argv[1] + 8);
17781 argv++;
17782 argc--;
17783 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000017784 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
17785 label_prefix= argv[1] + 15;
17786 argv++;
17787 argc--;
17788 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017789 else if ((strcmp(argv[1],"-O") == 0) ||
17790 (strcmp(argv[1], "-O1") == 0)) {
17791 optimize = 1;
17792 argv++;
17793 argc--;
17794 }
17795 else if (strcmp(argv[1],"-O2") == 0) {
17796 optimize = 2;
17797 argv++;
17798 argc--;
17799 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017800 else if ((strcmp(argv[1], "-o") == 0) && (argc > 2)) {
17801 ofilename = argv[2];
17802 argv += 2;
17803 argc -= 2;
17804 }
17805 else if (strncmp(argv[1], "-mcpu=", 6) == 0) {
17806 cpu = arch_encode_cpu(argv[1] + 6);
17807 if (cpu == BAD_CPU) {
17808 arg_error("Invalid cpu specified: %s\n",
17809 argv[1] + 6);
17810 }
17811 argv++;
17812 argc--;
17813 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017814 }
17815 if (argc != 2) {
17816 arg_error("Wrong argument count %d\n", argc);
17817 }
17818 filename = argv[1];
Eric Biederman05f26fc2003-06-11 21:55:00 +000017819 compile(filename, ofilename, cpu, debug, optimize, label_prefix);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017820
17821 return 0;
17822}