blob: db7d61131e484400b94f03ada872586e8bb76bc8 [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 Biederman2c018fb2003-07-21 20:13:45 +000017#define DEBUG_CONSISTENCY 1
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 Biederman83b991a2003-10-11 06:20:25 +000022#define DEBUG_SIMPLIFY 0
Eric Biedermanb138ac82003-04-22 18:44:01 +000023
Eric Biederman05f26fc2003-06-11 21:55:00 +000024#warning "FIXME boundary cases with small types in larger registers"
Eric Biederman8d9c1232003-06-17 08:42:17 +000025#warning "FIXME give clear error messages about unused variables"
Eric Biederman530b5192003-07-01 10:05:30 +000026#warning "FIXME properly handle multi dimensional arrays"
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
Eric Biederman83b991a2003-10-11 06:20:25 +0000211/* Types on the destination platform */
212#warning "FIXME this assumes 32bit x86 is the destination"
213typedef int8_t schar_t;
214typedef uint8_t uchar_t;
215typedef int8_t char_t;
216typedef int16_t short_t;
217typedef uint16_t ushort_t;
218typedef int32_t int_t;
219typedef uint32_t uint_t;
220typedef int32_t long_t;
221typedef uint32_t ulong_t;
222
223#define SCHAR_T_MIN (-128)
224#define SCHAR_T_MAX 127
225#define UCHAR_T_MAX 255
226#define CHAR_T_MIN SCHAR_T_MIN
227#define CHAR_T_MAX SCHAR_T_MAX
228#define SHRT_T_MIN (-32768)
229#define SHRT_T_MAX 32767
230#define USHRT_T_MAX 65535
231#define INT_T_MIN (-LONG_T_MAX - 1)
232#define INT_T_MAX 2147483647
233#define UINT_T_MAX 4294967295U
234#define LONG_T_MIN (-LONG_T_MAX - 1)
235#define LONG_T_MAX 2147483647
236#define ULONG_T_MAX 4294967295U
Eric Biedermanb138ac82003-04-22 18:44:01 +0000237
238struct file_state {
239 struct file_state *prev;
240 const char *basename;
241 char *dirname;
242 char *buf;
243 off_t size;
244 char *pos;
245 int line;
246 char *line_start;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000247 int report_line;
248 const char *report_name;
249 const char *report_dir;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000250};
251struct hash_entry;
252struct token {
253 int tok;
254 struct hash_entry *ident;
255 int str_len;
256 union {
257 ulong_t integer;
258 const char *str;
259 } val;
260};
261
262/* I have two classes of types:
263 * Operational types.
264 * Logical types. (The type the C standard says the operation is of)
265 *
266 * The operational types are:
267 * chars
268 * shorts
269 * ints
270 * longs
271 *
272 * floats
273 * doubles
274 * long doubles
275 *
276 * pointer
277 */
278
279
280/* Machine model.
281 * No memory is useable by the compiler.
282 * There is no floating point support.
283 * All operations take place in general purpose registers.
284 * There is one type of general purpose register.
285 * Unsigned longs are stored in that general purpose register.
286 */
287
288/* Operations on general purpose registers.
289 */
290
Eric Biederman530b5192003-07-01 10:05:30 +0000291#define OP_SDIVT 0
292#define OP_UDIVT 1
293#define OP_SMUL 2
294#define OP_UMUL 3
295#define OP_SDIV 4
296#define OP_UDIV 5
297#define OP_SMOD 6
298#define OP_UMOD 7
299#define OP_ADD 8
300#define OP_SUB 9
301#define OP_SL 10
302#define OP_USR 11
303#define OP_SSR 12
304#define OP_AND 13
305#define OP_XOR 14
306#define OP_OR 15
307#define OP_POS 16 /* Dummy positive operator don't use it */
308#define OP_NEG 17
309#define OP_INVERT 18
Eric Biedermanb138ac82003-04-22 18:44:01 +0000310
311#define OP_EQ 20
312#define OP_NOTEQ 21
313#define OP_SLESS 22
314#define OP_ULESS 23
315#define OP_SMORE 24
316#define OP_UMORE 25
317#define OP_SLESSEQ 26
318#define OP_ULESSEQ 27
319#define OP_SMOREEQ 28
320#define OP_UMOREEQ 29
321
322#define OP_LFALSE 30 /* Test if the expression is logically false */
323#define OP_LTRUE 31 /* Test if the expression is logcially true */
324
325#define OP_LOAD 32
326#define OP_STORE 33
Eric Biederman530b5192003-07-01 10:05:30 +0000327/* For OP_STORE ->type holds the type
328 * RHS(0) holds the destination address
329 * RHS(1) holds the value to store.
330 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000331
332#define OP_NOOP 34
333
334#define OP_MIN_CONST 50
335#define OP_MAX_CONST 59
336#define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
337#define OP_INTCONST 50
Eric Biedermand1ea5392003-06-28 06:49:45 +0000338/* For OP_INTCONST ->type holds the type.
339 * ->u.cval holds the constant value.
340 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000341#define OP_BLOBCONST 51
Eric Biederman0babc1c2003-05-09 02:39:00 +0000342/* For OP_BLOBCONST ->type holds the layout and size
Eric Biedermanb138ac82003-04-22 18:44:01 +0000343 * information. u.blob holds a pointer to the raw binary
344 * data for the constant initializer.
345 */
346#define OP_ADDRCONST 52
Eric Biederman0babc1c2003-05-09 02:39:00 +0000347/* For OP_ADDRCONST ->type holds the type.
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000348 * MISC(0) holds the reference to the static variable.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000349 * ->u.cval holds an offset from that value.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000350 */
351
352#define OP_WRITE 60
353/* OP_WRITE moves one pseudo register to another.
Eric Biederman530b5192003-07-01 10:05:30 +0000354 * RHS(0) holds the destination pseudo register, which must be an OP_DECL.
355 * RHS(1) holds the psuedo to move.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000356 */
357
358#define OP_READ 61
359/* OP_READ reads the value of a variable and makes
360 * it available for the pseudo operation.
361 * Useful for things like def-use chains.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000362 * RHS(0) holds points to the triple to read from.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000363 */
364#define OP_COPY 62
Eric Biederman0babc1c2003-05-09 02:39:00 +0000365/* OP_COPY makes a copy of the psedo register or constant in RHS(0).
366 */
367#define OP_PIECE 63
368/* OP_PIECE returns one piece of a instruction that returns a structure.
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000369 * MISC(0) is the instruction
Eric Biederman0babc1c2003-05-09 02:39:00 +0000370 * u.cval is the LHS piece of the instruction to return.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000371 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000372#define OP_ASM 64
373/* OP_ASM holds a sequence of assembly instructions, the result
374 * of a C asm directive.
375 * RHS(x) holds input value x to the assembly sequence.
376 * LHS(x) holds the output value x from the assembly sequence.
377 * u.blob holds the string of assembly instructions.
378 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000379
Eric Biedermanb138ac82003-04-22 18:44:01 +0000380#define OP_DEREF 65
381/* OP_DEREF generates an lvalue from a pointer.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000382 * RHS(0) holds the pointer value.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000383 * OP_DEREF serves as a place holder to indicate all necessary
384 * checks have been done to indicate a value is an lvalue.
385 */
386#define OP_DOT 66
Eric Biederman0babc1c2003-05-09 02:39:00 +0000387/* OP_DOT references a submember of a structure lvalue.
388 * RHS(0) holds the lvalue.
389 * ->u.field holds the name of the field we want.
390 *
391 * Not seen outside of expressions.
392 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000393#define OP_VAL 67
394/* OP_VAL returns the value of a subexpression of the current expression.
395 * Useful for operators that have side effects.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000396 * RHS(0) holds the expression.
397 * MISC(0) holds the subexpression of RHS(0) that is the
Eric Biedermanb138ac82003-04-22 18:44:01 +0000398 * value of the expression.
399 *
400 * Not seen outside of expressions.
401 */
402#define OP_LAND 68
Eric Biederman0babc1c2003-05-09 02:39:00 +0000403/* OP_LAND performs a C logical and between RHS(0) and RHS(1).
Eric Biedermanb138ac82003-04-22 18:44:01 +0000404 * Not seen outside of expressions.
405 */
406#define OP_LOR 69
Eric Biederman0babc1c2003-05-09 02:39:00 +0000407/* OP_LOR performs a C logical or between RHS(0) and RHS(1).
Eric Biedermanb138ac82003-04-22 18:44:01 +0000408 * Not seen outside of expressions.
409 */
410#define OP_COND 70
411/* OP_CODE performas a C ? : operation.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000412 * RHS(0) holds the test.
413 * RHS(1) holds the expression to evaluate if the test returns true.
414 * RHS(2) holds the expression to evaluate if the test returns false.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000415 * Not seen outside of expressions.
416 */
417#define OP_COMMA 71
418/* OP_COMMA performacs a C comma operation.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000419 * That is RHS(0) is evaluated, then RHS(1)
420 * and the value of RHS(1) is returned.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000421 * Not seen outside of expressions.
422 */
423
424#define OP_CALL 72
425/* OP_CALL performs a procedure call.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000426 * MISC(0) holds a pointer to the OP_LIST of a function
427 * RHS(x) holds argument x of a function
428 *
Eric Biedermanb138ac82003-04-22 18:44:01 +0000429 * Currently not seen outside of expressions.
430 */
Eric Biederman0babc1c2003-05-09 02:39:00 +0000431#define OP_VAL_VEC 74
432/* OP_VAL_VEC is an array of triples that are either variable
433 * or values for a structure or an array.
434 * RHS(x) holds element x of the vector.
435 * triple->type->elements holds the size of the vector.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000436 */
437
438/* statements */
439#define OP_LIST 80
440/* OP_LIST Holds a list of statements, and a result value.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000441 * RHS(0) holds the list of statements.
442 * MISC(0) holds the value of the statements.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000443 */
444
445#define OP_BRANCH 81 /* branch */
446/* For branch instructions
Eric Biederman0babc1c2003-05-09 02:39:00 +0000447 * TARG(0) holds the branch target.
448 * RHS(0) if present holds the branch condition.
449 * ->next holds where to branch to if the branch is not taken.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000450 * The branch target can only be a decl...
451 */
452
453#define OP_LABEL 83
454/* OP_LABEL is a triple that establishes an target for branches.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000455 * ->use is the list of all branches that use this label.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000456 */
457
458#define OP_ADECL 84
459/* OP_DECL is a triple that establishes an lvalue for assignments.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000460 * ->use is a list of statements that use the variable.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000461 */
462
463#define OP_SDECL 85
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000464/* OP_SDECL is a triple that establishes a variable of static
Eric Biedermanb138ac82003-04-22 18:44:01 +0000465 * storage duration.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000466 * ->use is a list of statements that use the variable.
467 * MISC(0) holds the initializer expression.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000468 */
469
470
471#define OP_PHI 86
472/* OP_PHI is a triple used in SSA form code.
473 * It is used when multiple code paths merge and a variable needs
474 * a single assignment from any of those code paths.
475 * The operation is a cross between OP_DECL and OP_WRITE, which
476 * is what OP_PHI is geneared from.
477 *
Eric Biederman0babc1c2003-05-09 02:39:00 +0000478 * RHS(x) points to the value from code path x
479 * The number of RHS entries is the number of control paths into the block
Eric Biedermanb138ac82003-04-22 18:44:01 +0000480 * in which OP_PHI resides. The elements of the array point to point
481 * to the variables OP_PHI is derived from.
482 *
Eric Biederman0babc1c2003-05-09 02:39:00 +0000483 * MISC(0) holds a pointer to the orginal OP_DECL node.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000484 */
485
486/* Architecture specific instructions */
487#define OP_CMP 100
488#define OP_TEST 101
489#define OP_SET_EQ 102
490#define OP_SET_NOTEQ 103
491#define OP_SET_SLESS 104
492#define OP_SET_ULESS 105
493#define OP_SET_SMORE 106
494#define OP_SET_UMORE 107
495#define OP_SET_SLESSEQ 108
496#define OP_SET_ULESSEQ 109
497#define OP_SET_SMOREEQ 110
498#define OP_SET_UMOREEQ 111
499
500#define OP_JMP 112
501#define OP_JMP_EQ 113
502#define OP_JMP_NOTEQ 114
503#define OP_JMP_SLESS 115
504#define OP_JMP_ULESS 116
505#define OP_JMP_SMORE 117
506#define OP_JMP_UMORE 118
507#define OP_JMP_SLESSEQ 119
508#define OP_JMP_ULESSEQ 120
509#define OP_JMP_SMOREEQ 121
510#define OP_JMP_UMOREEQ 122
511
512/* Builtin operators that it is just simpler to use the compiler for */
513#define OP_INB 130
514#define OP_INW 131
515#define OP_INL 132
516#define OP_OUTB 133
517#define OP_OUTW 134
518#define OP_OUTL 135
519#define OP_BSF 136
520#define OP_BSR 137
Eric Biedermanb138ac82003-04-22 18:44:01 +0000521#define OP_RDMSR 138
522#define OP_WRMSR 139
Eric Biedermanb138ac82003-04-22 18:44:01 +0000523#define OP_HLT 140
524
Eric Biederman0babc1c2003-05-09 02:39:00 +0000525struct op_info {
526 const char *name;
527 unsigned flags;
Eric Biederman83b991a2003-10-11 06:20:25 +0000528#define PURE 1 /* Triple has no side effects */
529#define IMPURE 2 /* Triple has side effects */
Eric Biederman0babc1c2003-05-09 02:39:00 +0000530#define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
Eric Biederman83b991a2003-10-11 06:20:25 +0000531#define DEF 4 /* Triple is a variable definition */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000532#define BLOCK 8 /* Triple stores the current block */
Eric Biederman83b991a2003-10-11 06:20:25 +0000533#define STRUCTURAL 16 /* Triple does not generate a machine instruction */
Eric Biederman0babc1c2003-05-09 02:39:00 +0000534 unsigned char lhs, rhs, misc, targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000535};
536
Eric Biederman0babc1c2003-05-09 02:39:00 +0000537#define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \
538 .name = (NAME), \
539 .flags = (FLAGS), \
540 .lhs = (LHS), \
541 .rhs = (RHS), \
542 .misc = (MISC), \
543 .targ = (TARG), \
544 }
545static const struct op_info table_ops[] = {
Eric Biederman530b5192003-07-01 10:05:30 +0000546[OP_SDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "sdivt"),
547[OP_UDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "udivt"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000548[OP_SMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smul"),
549[OP_UMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umul"),
550[OP_SDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sdiv"),
551[OP_UDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "udiv"),
552[OP_SMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smod"),
553[OP_UMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umod"),
554[OP_ADD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "add"),
555[OP_SUB ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sub"),
556[OP_SL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sl"),
557[OP_USR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "usr"),
558[OP_SSR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ssr"),
559[OP_AND ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "and"),
560[OP_XOR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "xor"),
561[OP_OR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "or"),
562[OP_POS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "pos"),
563[OP_NEG ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "neg"),
564[OP_INVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "invert"),
Eric Biedermanb138ac82003-04-22 18:44:01 +0000565
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000566[OP_EQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "eq"),
567[OP_NOTEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "noteq"),
568[OP_SLESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sless"),
569[OP_ULESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "uless"),
570[OP_SMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smore"),
571[OP_UMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umore"),
572[OP_SLESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "slesseq"),
573[OP_ULESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ulesseq"),
574[OP_SMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smoreeq"),
575[OP_UMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umoreeq"),
576[OP_LFALSE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "lfalse"),
577[OP_LTRUE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "ltrue"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000578
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000579[OP_LOAD ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "load"),
Eric Biederman530b5192003-07-01 10:05:30 +0000580[OP_STORE ] = OP( 0, 2, 0, 0, IMPURE | BLOCK , "store"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000581
Eric Biederman83b991a2003-10-11 06:20:25 +0000582[OP_NOOP ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "noop"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000583
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000584[OP_INTCONST ] = OP( 0, 0, 0, 0, PURE | DEF, "intconst"),
Eric Biederman83b991a2003-10-11 06:20:25 +0000585[OP_BLOBCONST ] = OP( 0, 0, 0, 0, PURE , "blobconst"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000586[OP_ADDRCONST ] = OP( 0, 0, 1, 0, PURE | DEF, "addrconst"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000587
Eric Biederman530b5192003-07-01 10:05:30 +0000588[OP_WRITE ] = OP( 0, 2, 0, 0, PURE | BLOCK, "write"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000589[OP_READ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "read"),
590[OP_COPY ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "copy"),
Eric Biederman83b991a2003-10-11 06:20:25 +0000591[OP_PIECE ] = OP( 0, 0, 1, 0, PURE | DEF | STRUCTURAL, "piece"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000592[OP_ASM ] = OP(-1, -1, 0, 0, IMPURE, "asm"),
593[OP_DEREF ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "deref"),
594[OP_DOT ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "dot"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000595
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000596[OP_VAL ] = OP( 0, 1, 1, 0, 0 | DEF | BLOCK, "val"),
597[OP_LAND ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "land"),
598[OP_LOR ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "lor"),
599[OP_COND ] = OP( 0, 3, 0, 0, 0 | DEF | BLOCK, "cond"),
600[OP_COMMA ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "comma"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000601/* Call is special most it can stand in for anything so it depends on context */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000602[OP_CALL ] = OP(-1, -1, 1, 0, 0 | BLOCK, "call"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000603/* The sizes of OP_CALL and OP_VAL_VEC depend upon context */
Eric Biederman83b991a2003-10-11 06:20:25 +0000604[OP_VAL_VEC ] = OP( 0, -1, 0, 0, 0 | BLOCK | STRUCTURAL, "valvec"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000605
Eric Biederman83b991a2003-10-11 06:20:25 +0000606[OP_LIST ] = OP( 0, 1, 1, 0, 0 | DEF | STRUCTURAL, "list"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000607/* The number of targets for OP_BRANCH depends on context */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000608[OP_BRANCH ] = OP( 0, -1, 0, 1, PURE | BLOCK, "branch"),
Eric Biederman83b991a2003-10-11 06:20:25 +0000609[OP_LABEL ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "label"),
610[OP_ADECL ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "adecl"),
611[OP_SDECL ] = OP( 0, 0, 1, 0, PURE | BLOCK | STRUCTURAL, "sdecl"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000612/* The number of RHS elements of OP_PHI depend upon context */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000613[OP_PHI ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000614
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000615[OP_CMP ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK, "cmp"),
616[OP_TEST ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "test"),
617[OP_SET_EQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_eq"),
618[OP_SET_NOTEQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_noteq"),
619[OP_SET_SLESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_sless"),
620[OP_SET_ULESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_uless"),
621[OP_SET_SMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smore"),
622[OP_SET_UMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umore"),
623[OP_SET_SLESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_slesseq"),
624[OP_SET_ULESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_ulesseq"),
625[OP_SET_SMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smoreq"),
626[OP_SET_UMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umoreq"),
627[OP_JMP ] = OP( 0, 0, 0, 1, PURE | BLOCK, "jmp"),
628[OP_JMP_EQ ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_eq"),
629[OP_JMP_NOTEQ ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_noteq"),
630[OP_JMP_SLESS ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_sless"),
631[OP_JMP_ULESS ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_uless"),
632[OP_JMP_SMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_smore"),
633[OP_JMP_UMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_umore"),
634[OP_JMP_SLESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_slesseq"),
635[OP_JMP_ULESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_ulesseq"),
636[OP_JMP_SMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_smoreq"),
637[OP_JMP_UMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_umoreq"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000638
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000639[OP_INB ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inb"),
640[OP_INW ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inw"),
641[OP_INL ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inl"),
642[OP_OUTB ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outb"),
643[OP_OUTW ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outw"),
644[OP_OUTL ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outl"),
645[OP_BSF ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsf"),
646[OP_BSR ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsr"),
647[OP_RDMSR ] = OP( 2, 1, 0, 0, IMPURE | BLOCK, "__rdmsr"),
648[OP_WRMSR ] = OP( 0, 3, 0, 0, IMPURE | BLOCK, "__wrmsr"),
649[OP_HLT ] = OP( 0, 0, 0, 0, IMPURE | BLOCK, "__hlt"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000650};
651#undef OP
652#define OP_MAX (sizeof(table_ops)/sizeof(table_ops[0]))
Eric Biedermanb138ac82003-04-22 18:44:01 +0000653
654static const char *tops(int index)
655{
656 static const char unknown[] = "unknown op";
657 if (index < 0) {
658 return unknown;
659 }
660 if (index > OP_MAX) {
661 return unknown;
662 }
Eric Biederman0babc1c2003-05-09 02:39:00 +0000663 return table_ops[index].name;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000664}
665
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000666struct asm_info;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000667struct triple;
668struct block;
669struct triple_set {
670 struct triple_set *next;
671 struct triple *member;
672};
673
Eric Biederman0babc1c2003-05-09 02:39:00 +0000674#define MAX_LHS 15
Eric Biederman678d8162003-07-03 03:59:38 +0000675#define MAX_RHS 250
676#define MAX_MISC 3
677#define MAX_TARG 3
Eric Biederman0babc1c2003-05-09 02:39:00 +0000678
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000679struct occurance {
680 int count;
681 const char *filename;
682 const char *function;
683 int line;
684 int col;
685 struct occurance *parent;
686};
Eric Biedermanb138ac82003-04-22 18:44:01 +0000687struct triple {
688 struct triple *next, *prev;
689 struct triple_set *use;
690 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000691 unsigned char op;
692 unsigned char template_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000693 unsigned short sizes;
694#define TRIPLE_LHS(SIZES) (((SIZES) >> 0) & 0x0f)
Eric Biederman678d8162003-07-03 03:59:38 +0000695#define TRIPLE_RHS(SIZES) (((SIZES) >> 4) & 0xff)
696#define TRIPLE_MISC(SIZES) (((SIZES) >> 12) & 0x03)
697#define TRIPLE_TARG(SIZES) (((SIZES) >> 14) & 0x03)
Eric Biederman0babc1c2003-05-09 02:39:00 +0000698#define TRIPLE_SIZE(SIZES) \
Eric Biederman678d8162003-07-03 03:59:38 +0000699 (TRIPLE_LHS(SIZES) + \
700 TRIPLE_RHS(SIZES) + \
701 TRIPLE_MISC(SIZES) + \
702 TRIPLE_TARG(SIZES))
Eric Biederman0babc1c2003-05-09 02:39:00 +0000703#define TRIPLE_SIZES(LHS, RHS, MISC, TARG) \
704 ((((LHS) & 0x0f) << 0) | \
Eric Biederman678d8162003-07-03 03:59:38 +0000705 (((RHS) & 0xff) << 4) | \
706 (((MISC) & 0x03) << 12) | \
707 (((TARG) & 0x03) << 14))
Eric Biederman0babc1c2003-05-09 02:39:00 +0000708#define TRIPLE_LHS_OFF(SIZES) (0)
709#define TRIPLE_RHS_OFF(SIZES) (TRIPLE_LHS_OFF(SIZES) + TRIPLE_LHS(SIZES))
710#define TRIPLE_MISC_OFF(SIZES) (TRIPLE_RHS_OFF(SIZES) + TRIPLE_RHS(SIZES))
711#define TRIPLE_TARG_OFF(SIZES) (TRIPLE_MISC_OFF(SIZES) + TRIPLE_MISC(SIZES))
712#define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF((PTR)->sizes) + (INDEX)])
713#define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF((PTR)->sizes) + (INDEX)])
714#define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF((PTR)->sizes) + (INDEX)])
715#define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF((PTR)->sizes) + (INDEX)])
Eric Biedermanb138ac82003-04-22 18:44:01 +0000716 unsigned id; /* A scratch value and finally the register */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000717#define TRIPLE_FLAG_FLATTENED (1 << 31)
718#define TRIPLE_FLAG_PRE_SPLIT (1 << 30)
719#define TRIPLE_FLAG_POST_SPLIT (1 << 29)
Eric Biederman83b991a2003-10-11 06:20:25 +0000720#define TRIPLE_FLAG_VOLATILE (1 << 28)
721#define TRIPLE_FLAG_LOCAL (1 << 27)
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000722 struct occurance *occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000723 union {
724 ulong_t cval;
725 struct block *block;
726 void *blob;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000727 struct hash_entry *field;
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000728 struct asm_info *ainfo;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000729 } u;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000730 struct triple *param[2];
Eric Biedermanb138ac82003-04-22 18:44:01 +0000731};
732
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000733struct reg_info {
734 unsigned reg;
735 unsigned regcm;
736};
737struct ins_template {
738 struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
739};
740
741struct asm_info {
742 struct ins_template tmpl;
743 char *str;
744};
745
Eric Biedermanb138ac82003-04-22 18:44:01 +0000746struct block_set {
747 struct block_set *next;
748 struct block *member;
749};
750struct block {
751 struct block *work_next;
752 struct block *left, *right;
753 struct triple *first, *last;
754 int users;
755 struct block_set *use;
756 struct block_set *idominates;
757 struct block_set *domfrontier;
758 struct block *idom;
759 struct block_set *ipdominates;
760 struct block_set *ipdomfrontier;
761 struct block *ipdom;
762 int vertex;
763
764};
765
766struct symbol {
767 struct symbol *next;
768 struct hash_entry *ident;
769 struct triple *def;
770 struct type *type;
771 int scope_depth;
772};
773
774struct macro {
775 struct hash_entry *ident;
776 char *buf;
777 int buf_len;
778};
779
780struct hash_entry {
781 struct hash_entry *next;
782 const char *name;
783 int name_len;
784 int tok;
785 struct macro *sym_define;
786 struct symbol *sym_label;
Eric Biederman83b991a2003-10-11 06:20:25 +0000787 struct symbol *sym_tag;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000788 struct symbol *sym_ident;
789};
790
791#define HASH_TABLE_SIZE 2048
792
793struct compile_state {
Eric Biederman05f26fc2003-06-11 21:55:00 +0000794 const char *label_prefix;
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000795 const char *ofilename;
796 FILE *output;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000797 struct file_state *file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000798 struct occurance *last_occurance;
799 const char *function;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000800 struct token token[4];
801 struct hash_entry *hash_table[HASH_TABLE_SIZE];
Eric Biederman83b991a2003-10-11 06:20:25 +0000802 struct hash_entry *i_switch;
803 struct hash_entry *i_case;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000804 struct hash_entry *i_continue;
805 struct hash_entry *i_break;
Eric Biederman83b991a2003-10-11 06:20:25 +0000806 struct hash_entry *i_default;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000807 int scope_depth;
808 int if_depth, if_value;
809 int macro_line;
810 struct file_state *macro_file;
811 struct triple *main_function;
Eric Biederman83b991a2003-10-11 06:20:25 +0000812 struct triple *first;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000813 struct block *first_block, *last_block;
814 int last_vertex;
Eric Biederman83b991a2003-10-11 06:20:25 +0000815 unsigned long features;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000816 int debug;
817 int optimize;
818};
819
Eric Biederman0babc1c2003-05-09 02:39:00 +0000820/* visibility global/local */
821/* static/auto duration */
822/* typedef, register, inline */
823#define STOR_SHIFT 0
824#define STOR_MASK 0x000f
825/* Visibility */
826#define STOR_GLOBAL 0x0001
827/* Duration */
828#define STOR_PERM 0x0002
829/* Storage specifiers */
830#define STOR_AUTO 0x0000
831#define STOR_STATIC 0x0002
832#define STOR_EXTERN 0x0003
833#define STOR_REGISTER 0x0004
834#define STOR_TYPEDEF 0x0008
835#define STOR_INLINE 0x000c
836
837#define QUAL_SHIFT 4
838#define QUAL_MASK 0x0070
839#define QUAL_NONE 0x0000
840#define QUAL_CONST 0x0010
841#define QUAL_VOLATILE 0x0020
842#define QUAL_RESTRICT 0x0040
843
844#define TYPE_SHIFT 8
845#define TYPE_MASK 0x1f00
Eric Biederman83b991a2003-10-11 06:20:25 +0000846#define TYPE_INTEGER(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG)) || ((TYPE) == TYPE_ENUM))
847#define TYPE_ARITHMETIC(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE)) || ((TYPE) == TYPE_ENUM))
Eric Biederman0babc1c2003-05-09 02:39:00 +0000848#define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
849#define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
Eric Biederman83b991a2003-10-11 06:20:25 +0000850#define TYPE_MKUNSIGNED(TYPE) (((TYPE) & ~0xF000) | 0x0100)
851#define TYPE_RANK(TYPE) ((TYPE) & ~0xF1FF)
Eric Biederman0babc1c2003-05-09 02:39:00 +0000852#define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER)
853#define TYPE_DEFAULT 0x0000
854#define TYPE_VOID 0x0100
855#define TYPE_CHAR 0x0200
856#define TYPE_UCHAR 0x0300
857#define TYPE_SHORT 0x0400
858#define TYPE_USHORT 0x0500
859#define TYPE_INT 0x0600
860#define TYPE_UINT 0x0700
861#define TYPE_LONG 0x0800
862#define TYPE_ULONG 0x0900
863#define TYPE_LLONG 0x0a00 /* long long */
864#define TYPE_ULLONG 0x0b00
865#define TYPE_FLOAT 0x0c00
866#define TYPE_DOUBLE 0x0d00
867#define TYPE_LDOUBLE 0x0e00 /* long double */
Eric Biederman83b991a2003-10-11 06:20:25 +0000868
869/* Note: TYPE_ENUM is chosen very carefully so TYPE_RANK works */
870#define TYPE_ENUM 0x1600
871#define TYPE_LIST 0x1700
872/* TYPE_LIST is a basic building block when defining enumerations
873 * type->field_ident holds the name of this enumeration entry.
874 * type->right holds the entry in the list.
875 */
876
Eric Biederman0babc1c2003-05-09 02:39:00 +0000877#define TYPE_STRUCT 0x1000
Eric Biederman83b991a2003-10-11 06:20:25 +0000878#define TYPE_UNION 0x1100
Eric Biederman0babc1c2003-05-09 02:39:00 +0000879#define TYPE_POINTER 0x1200
880/* For TYPE_POINTER:
881 * type->left holds the type pointed to.
882 */
883#define TYPE_FUNCTION 0x1300
884/* For TYPE_FUNCTION:
885 * type->left holds the return type.
886 * type->right holds the...
887 */
888#define TYPE_PRODUCT 0x1400
889/* TYPE_PRODUCT is a basic building block when defining structures
890 * type->left holds the type that appears first in memory.
891 * type->right holds the type that appears next in memory.
892 */
893#define TYPE_OVERLAP 0x1500
894/* TYPE_OVERLAP is a basic building block when defining unions
895 * type->left and type->right holds to types that overlap
896 * each other in memory.
897 */
Eric Biederman83b991a2003-10-11 06:20:25 +0000898#define TYPE_ARRAY 0x1800
Eric Biederman0babc1c2003-05-09 02:39:00 +0000899/* TYPE_ARRAY is a basic building block when definitng arrays.
900 * type->left holds the type we are an array of.
901 * type-> holds the number of elements.
902 */
903
Eric Biederman83b991a2003-10-11 06:20:25 +0000904#define ELEMENT_COUNT_UNSPECIFIED ULONG_T_MAX
Eric Biederman0babc1c2003-05-09 02:39:00 +0000905
906struct type {
907 unsigned int type;
908 struct type *left, *right;
909 ulong_t elements;
910 struct hash_entry *field_ident;
911 struct hash_entry *type_ident;
912};
913
Eric Biederman530b5192003-07-01 10:05:30 +0000914#define TEMPLATE_BITS 7
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000915#define MAX_TEMPLATES (1<<TEMPLATE_BITS)
Eric Biederman83b991a2003-10-11 06:20:25 +0000916#define MAX_REG_EQUIVS 16
Eric Biederman530b5192003-07-01 10:05:30 +0000917#define MAX_REGC 14
Eric Biederman83b991a2003-10-11 06:20:25 +0000918#define MAX_REGISTERS 75
919#define REGISTER_BITS 7
920#define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
Eric Biedermanb138ac82003-04-22 18:44:01 +0000921#define REG_UNSET 0
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000922#define REG_UNNEEDED 1
923#define REG_VIRT0 (MAX_REGISTERS + 0)
924#define REG_VIRT1 (MAX_REGISTERS + 1)
925#define REG_VIRT2 (MAX_REGISTERS + 2)
926#define REG_VIRT3 (MAX_REGISTERS + 3)
927#define REG_VIRT4 (MAX_REGISTERS + 4)
928#define REG_VIRT5 (MAX_REGISTERS + 5)
Eric Biederman83b991a2003-10-11 06:20:25 +0000929#define REG_VIRT6 (MAX_REGISTERS + 6)
930#define REG_VIRT7 (MAX_REGISTERS + 7)
931#define REG_VIRT8 (MAX_REGISTERS + 8)
932#define REG_VIRT9 (MAX_REGISTERS + 9)
933
934#if (MAX_REGISTERS + 9) > MAX_VIRT_REGISTERS
935#error "MAX_VIRT_REGISTERS to small"
936#endif
937#if (MAX_REGC + REGISTER_BITS) > 27
938#error "Too many id bits used"
939#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +0000940
941/* Provision for 8 register classes */
Eric Biedermanf96a8102003-06-16 16:57:34 +0000942#define REG_SHIFT 0
943#define REGC_SHIFT REGISTER_BITS
944#define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000945#define REG_MASK (MAX_VIRT_REGISTERS -1)
946#define ID_REG(ID) ((ID) & REG_MASK)
947#define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
Eric Biedermanf96a8102003-06-16 16:57:34 +0000948#define ID_REGCM(ID) (((ID) & REGC_MASK) >> REGC_SHIFT)
949#define SET_REGCM(ID, REGCM) ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
950#define SET_INFO(ID, INFO) ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
951 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
Eric Biedermanb138ac82003-04-22 18:44:01 +0000952
953static unsigned arch_reg_regcm(struct compile_state *state, int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000954static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermand1ea5392003-06-28 06:49:45 +0000955static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000956static void arch_reg_equivs(
957 struct compile_state *state, unsigned *equiv, int reg);
958static int arch_select_free_register(
959 struct compile_state *state, char *used, int classes);
960static unsigned arch_regc_size(struct compile_state *state, int class);
961static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
962static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
963static const char *arch_reg_str(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000964static struct reg_info arch_reg_constraint(
965 struct compile_state *state, struct type *type, const char *constraint);
966static struct reg_info arch_reg_clobber(
967 struct compile_state *state, const char *clobber);
968static struct reg_info arch_reg_lhs(struct compile_state *state,
969 struct triple *ins, int index);
970static struct reg_info arch_reg_rhs(struct compile_state *state,
971 struct triple *ins, int index);
972static struct triple *transform_to_arch_instruction(
973 struct compile_state *state, struct triple *ins);
974
975
Eric Biedermanb138ac82003-04-22 18:44:01 +0000976
Eric Biederman0babc1c2003-05-09 02:39:00 +0000977#define DEBUG_ABORT_ON_ERROR 0x0001
978#define DEBUG_INTERMEDIATE_CODE 0x0002
979#define DEBUG_CONTROL_FLOW 0x0004
980#define DEBUG_BASIC_BLOCKS 0x0008
981#define DEBUG_FDOMINATORS 0x0010
982#define DEBUG_RDOMINATORS 0x0020
983#define DEBUG_TRIPLES 0x0040
984#define DEBUG_INTERFERENCE 0x0080
985#define DEBUG_ARCH_CODE 0x0100
986#define DEBUG_CODE_ELIMINATION 0x0200
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000987#define DEBUG_INSERTED_COPIES 0x0400
Eric Biedermanb138ac82003-04-22 18:44:01 +0000988
Eric Biederman153ea352003-06-20 14:43:20 +0000989#define GLOBAL_SCOPE_DEPTH 1
990#define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
Eric Biedermanb138ac82003-04-22 18:44:01 +0000991
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000992static void compile_file(struct compile_state *old_state, const char *filename, int local);
993
994static void do_cleanup(struct compile_state *state)
995{
996 if (state->output) {
997 fclose(state->output);
998 unlink(state->ofilename);
999 }
1000}
Eric Biedermanb138ac82003-04-22 18:44:01 +00001001
1002static int get_col(struct file_state *file)
1003{
1004 int col;
1005 char *ptr, *end;
1006 ptr = file->line_start;
1007 end = file->pos;
1008 for(col = 0; ptr < end; ptr++) {
1009 if (*ptr != '\t') {
1010 col++;
1011 }
1012 else {
1013 col = (col & ~7) + 8;
1014 }
1015 }
1016 return col;
1017}
1018
1019static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
1020{
1021 int col;
Eric Biederman530b5192003-07-01 10:05:30 +00001022 if (triple && triple->occurance) {
Eric Biederman00443072003-06-24 12:34:45 +00001023 struct occurance *spot;
1024 spot = triple->occurance;
1025 while(spot->parent) {
1026 spot = spot->parent;
1027 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001028 fprintf(fp, "%s:%d.%d: ",
Eric Biederman00443072003-06-24 12:34:45 +00001029 spot->filename, spot->line, spot->col);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001030 return;
1031 }
1032 if (!state->file) {
1033 return;
1034 }
1035 col = get_col(state->file);
1036 fprintf(fp, "%s:%d.%d: ",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001037 state->file->report_name, state->file->report_line, col);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001038}
1039
Eric Biederman83b991a2003-10-11 06:20:25 +00001040static void romcc_internal_error(struct compile_state *state, struct triple *ptr,
Eric Biedermanb138ac82003-04-22 18:44:01 +00001041 char *fmt, ...)
1042{
1043 va_list args;
1044 va_start(args, fmt);
1045 loc(stderr, state, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001046 if (ptr) {
1047 fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
1048 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001049 fprintf(stderr, "Internal compiler error: ");
1050 vfprintf(stderr, fmt, args);
1051 fprintf(stderr, "\n");
1052 va_end(args);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001053 do_cleanup(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001054 abort();
1055}
1056
1057
Eric Biederman83b991a2003-10-11 06:20:25 +00001058static void romcc_internal_warning(struct compile_state *state, struct triple *ptr,
Eric Biedermanb138ac82003-04-22 18:44:01 +00001059 char *fmt, ...)
1060{
1061 va_list args;
1062 va_start(args, fmt);
1063 loc(stderr, state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00001064 if (ptr) {
1065 fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
1066 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001067 fprintf(stderr, "Internal compiler warning: ");
1068 vfprintf(stderr, fmt, args);
1069 fprintf(stderr, "\n");
1070 va_end(args);
1071}
1072
1073
1074
Eric Biederman83b991a2003-10-11 06:20:25 +00001075static void romcc_error(struct compile_state *state, struct triple *ptr,
Eric Biedermanb138ac82003-04-22 18:44:01 +00001076 char *fmt, ...)
1077{
1078 va_list args;
1079 va_start(args, fmt);
1080 loc(stderr, state, ptr);
Eric Biederman83b991a2003-10-11 06:20:25 +00001081 if (ptr && (state->debug & DEBUG_ABORT_ON_ERROR)) {
1082 fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
1083 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001084 vfprintf(stderr, fmt, args);
1085 va_end(args);
1086 fprintf(stderr, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001087 do_cleanup(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001088 if (state->debug & DEBUG_ABORT_ON_ERROR) {
1089 abort();
1090 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001091 exit(1);
1092}
1093
Eric Biederman83b991a2003-10-11 06:20:25 +00001094static void romcc_warning(struct compile_state *state, struct triple *ptr,
Eric Biedermanb138ac82003-04-22 18:44:01 +00001095 char *fmt, ...)
1096{
1097 va_list args;
1098 va_start(args, fmt);
1099 loc(stderr, state, ptr);
1100 fprintf(stderr, "warning: ");
1101 vfprintf(stderr, fmt, args);
1102 fprintf(stderr, "\n");
1103 va_end(args);
1104}
1105
1106#if DEBUG_ERROR_MESSAGES
Eric Biederman83b991a2003-10-11 06:20:25 +00001107# define internal_error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),romcc_internal_error
1108# define internal_warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),romcc_internal_warning
1109# define error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),romcc_error
1110# define warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),romcc_warning
Eric Biedermanb138ac82003-04-22 18:44:01 +00001111#else
Eric Biederman83b991a2003-10-11 06:20:25 +00001112# define internal_error romcc_internal_error
1113# define internal_warning romcc_internal_warning
1114# define error romcc_error
1115# define warning romcc_warning
Eric Biedermanb138ac82003-04-22 18:44:01 +00001116#endif
1117#define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1118
Eric Biederman0babc1c2003-05-09 02:39:00 +00001119static void valid_op(struct compile_state *state, int op)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001120{
1121 char *fmt = "invalid op: %d";
Eric Biederman0babc1c2003-05-09 02:39:00 +00001122 if (op >= OP_MAX) {
1123 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001124 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001125 if (op < 0) {
1126 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001127 }
1128}
1129
Eric Biederman0babc1c2003-05-09 02:39:00 +00001130static void valid_ins(struct compile_state *state, struct triple *ptr)
1131{
1132 valid_op(state, ptr->op);
1133}
1134
Eric Biedermanb138ac82003-04-22 18:44:01 +00001135static void process_trigraphs(struct compile_state *state)
1136{
1137 char *src, *dest, *end;
1138 struct file_state *file;
1139 file = state->file;
1140 src = dest = file->buf;
1141 end = file->buf + file->size;
1142 while((end - src) >= 3) {
1143 if ((src[0] == '?') && (src[1] == '?')) {
1144 int c = -1;
1145 switch(src[2]) {
1146 case '=': c = '#'; break;
1147 case '/': c = '\\'; break;
1148 case '\'': c = '^'; break;
1149 case '(': c = '['; break;
1150 case ')': c = ']'; break;
1151 case '!': c = '!'; break;
1152 case '<': c = '{'; break;
1153 case '>': c = '}'; break;
1154 case '-': c = '~'; break;
1155 }
1156 if (c != -1) {
1157 *dest++ = c;
1158 src += 3;
1159 }
1160 else {
1161 *dest++ = *src++;
1162 }
1163 }
1164 else {
1165 *dest++ = *src++;
1166 }
1167 }
1168 while(src != end) {
1169 *dest++ = *src++;
1170 }
1171 file->size = dest - file->buf;
1172}
1173
1174static void splice_lines(struct compile_state *state)
1175{
1176 char *src, *dest, *end;
1177 struct file_state *file;
1178 file = state->file;
1179 src = dest = file->buf;
1180 end = file->buf + file->size;
1181 while((end - src) >= 2) {
1182 if ((src[0] == '\\') && (src[1] == '\n')) {
1183 src += 2;
1184 }
1185 else {
1186 *dest++ = *src++;
1187 }
1188 }
1189 while(src != end) {
1190 *dest++ = *src++;
1191 }
1192 file->size = dest - file->buf;
1193}
1194
1195static struct type void_type;
1196static void use_triple(struct triple *used, struct triple *user)
1197{
1198 struct triple_set **ptr, *new;
1199 if (!used)
1200 return;
1201 if (!user)
1202 return;
1203 ptr = &used->use;
1204 while(*ptr) {
1205 if ((*ptr)->member == user) {
1206 return;
1207 }
1208 ptr = &(*ptr)->next;
1209 }
1210 /* Append new to the head of the list,
1211 * copy_func and rename_block_variables
1212 * depends on this.
1213 */
1214 new = xcmalloc(sizeof(*new), "triple_set");
1215 new->member = user;
1216 new->next = used->use;
1217 used->use = new;
1218}
1219
1220static void unuse_triple(struct triple *used, struct triple *unuser)
1221{
1222 struct triple_set *use, **ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001223 if (!used) {
1224 return;
1225 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001226 ptr = &used->use;
1227 while(*ptr) {
1228 use = *ptr;
1229 if (use->member == unuser) {
1230 *ptr = use->next;
1231 xfree(use);
1232 }
1233 else {
1234 ptr = &use->next;
1235 }
1236 }
1237}
1238
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001239static void put_occurance(struct occurance *occurance)
1240{
1241 occurance->count -= 1;
1242 if (occurance->count <= 0) {
1243 if (occurance->parent) {
1244 put_occurance(occurance->parent);
1245 }
1246 xfree(occurance);
1247 }
1248}
1249
1250static void get_occurance(struct occurance *occurance)
1251{
1252 occurance->count += 1;
1253}
1254
1255
1256static struct occurance *new_occurance(struct compile_state *state)
1257{
1258 struct occurance *result, *last;
1259 const char *filename;
1260 const char *function;
1261 int line, col;
1262
1263 function = "";
1264 filename = 0;
1265 line = 0;
1266 col = 0;
1267 if (state->file) {
1268 filename = state->file->report_name;
1269 line = state->file->report_line;
1270 col = get_col(state->file);
1271 }
1272 if (state->function) {
1273 function = state->function;
1274 }
1275 last = state->last_occurance;
1276 if (last &&
1277 (last->col == col) &&
1278 (last->line == line) &&
1279 (last->function == function) &&
Eric Biederman83b991a2003-10-11 06:20:25 +00001280 ((last->filename == filename) ||
1281 (strcmp(last->filename, filename) == 0)))
1282 {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001283 get_occurance(last);
1284 return last;
1285 }
1286 if (last) {
1287 state->last_occurance = 0;
1288 put_occurance(last);
1289 }
1290 result = xmalloc(sizeof(*result), "occurance");
1291 result->count = 2;
1292 result->filename = filename;
1293 result->function = function;
1294 result->line = line;
1295 result->col = col;
1296 result->parent = 0;
1297 state->last_occurance = result;
1298 return result;
1299}
1300
1301static struct occurance *inline_occurance(struct compile_state *state,
1302 struct occurance *new, struct occurance *orig)
1303{
1304 struct occurance *result, *last;
1305 last = state->last_occurance;
1306 if (last &&
1307 (last->parent == orig) &&
1308 (last->col == new->col) &&
1309 (last->line == new->line) &&
1310 (last->function == new->function) &&
1311 (last->filename == new->filename)) {
1312 get_occurance(last);
1313 return last;
1314 }
1315 if (last) {
1316 state->last_occurance = 0;
1317 put_occurance(last);
1318 }
1319 get_occurance(orig);
1320 result = xmalloc(sizeof(*result), "occurance");
1321 result->count = 2;
1322 result->filename = new->filename;
1323 result->function = new->function;
1324 result->line = new->line;
1325 result->col = new->col;
1326 result->parent = orig;
1327 state->last_occurance = result;
1328 return result;
1329}
1330
1331
1332static struct occurance dummy_occurance = {
1333 .count = 2,
1334 .filename = __FILE__,
1335 .function = "",
1336 .line = __LINE__,
1337 .col = 0,
1338 .parent = 0,
1339};
Eric Biedermanb138ac82003-04-22 18:44:01 +00001340
1341/* The zero triple is used as a place holder when we are removing pointers
1342 * from a triple. Having allows certain sanity checks to pass even
1343 * when the original triple that was pointed to is gone.
1344 */
1345static struct triple zero_triple = {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001346 .next = &zero_triple,
1347 .prev = &zero_triple,
1348 .use = 0,
1349 .op = OP_INTCONST,
1350 .sizes = TRIPLE_SIZES(0, 0, 0, 0),
1351 .id = -1, /* An invalid id */
Eric Biederman830c9882003-07-04 00:27:33 +00001352 .u = { .cval = 0, },
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001353 .occurance = &dummy_occurance,
Eric Biederman830c9882003-07-04 00:27:33 +00001354 .param = { [0] = 0, [1] = 0, },
Eric Biedermanb138ac82003-04-22 18:44:01 +00001355};
1356
Eric Biederman0babc1c2003-05-09 02:39:00 +00001357
1358static unsigned short triple_sizes(struct compile_state *state,
Eric Biederman678d8162003-07-03 03:59:38 +00001359 int op, struct type *type, int lhs_wanted, int rhs_wanted,
1360 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00001361{
1362 int lhs, rhs, misc, targ;
Eric Biederman678d8162003-07-03 03:59:38 +00001363 struct triple dummy;
1364 dummy.op = op;
1365 dummy.occurance = occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00001366 valid_op(state, op);
1367 lhs = table_ops[op].lhs;
1368 rhs = table_ops[op].rhs;
1369 misc = table_ops[op].misc;
1370 targ = table_ops[op].targ;
1371
1372
1373 if (op == OP_CALL) {
1374 struct type *param;
1375 rhs = 0;
1376 param = type->right;
1377 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
1378 rhs++;
1379 param = param->right;
1380 }
1381 if ((param->type & TYPE_MASK) != TYPE_VOID) {
1382 rhs++;
1383 }
1384 lhs = 0;
1385 if ((type->left->type & TYPE_MASK) == TYPE_STRUCT) {
1386 lhs = type->left->elements;
1387 }
1388 }
1389 else if (op == OP_VAL_VEC) {
1390 rhs = type->elements;
1391 }
1392 else if ((op == OP_BRANCH) || (op == OP_PHI)) {
1393 rhs = rhs_wanted;
1394 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001395 else if (op == OP_ASM) {
1396 rhs = rhs_wanted;
1397 lhs = lhs_wanted;
1398 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001399 if ((rhs < 0) || (rhs > MAX_RHS)) {
Eric Biederman678d8162003-07-03 03:59:38 +00001400 internal_error(state, &dummy, "bad rhs %d", rhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001401 }
1402 if ((lhs < 0) || (lhs > MAX_LHS)) {
Eric Biederman678d8162003-07-03 03:59:38 +00001403 internal_error(state, &dummy, "bad lhs");
Eric Biederman0babc1c2003-05-09 02:39:00 +00001404 }
1405 if ((misc < 0) || (misc > MAX_MISC)) {
Eric Biederman678d8162003-07-03 03:59:38 +00001406 internal_error(state, &dummy, "bad misc");
Eric Biederman0babc1c2003-05-09 02:39:00 +00001407 }
1408 if ((targ < 0) || (targ > MAX_TARG)) {
Eric Biederman678d8162003-07-03 03:59:38 +00001409 internal_error(state, &dummy, "bad targs");
Eric Biederman0babc1c2003-05-09 02:39:00 +00001410 }
1411 return TRIPLE_SIZES(lhs, rhs, misc, targ);
1412}
1413
1414static struct triple *alloc_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001415 int op, struct type *type, int lhs, int rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001416 struct occurance *occurance)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001417{
Eric Biederman0babc1c2003-05-09 02:39:00 +00001418 size_t size, sizes, extra_count, min_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001419 struct triple *ret;
Eric Biederman678d8162003-07-03 03:59:38 +00001420 sizes = triple_sizes(state, op, type, lhs, rhs, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001421
1422 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
1423 extra_count = TRIPLE_SIZE(sizes);
1424 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
1425
1426 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
1427 ret = xcmalloc(size, "tripple");
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001428 ret->op = op;
1429 ret->sizes = sizes;
1430 ret->type = type;
1431 ret->next = ret;
1432 ret->prev = ret;
1433 ret->occurance = occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001434 return ret;
1435}
1436
Eric Biederman0babc1c2003-05-09 02:39:00 +00001437struct triple *dup_triple(struct compile_state *state, struct triple *src)
1438{
1439 struct triple *dup;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001440 int src_lhs, src_rhs, src_size;
1441 src_lhs = TRIPLE_LHS(src->sizes);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001442 src_rhs = TRIPLE_RHS(src->sizes);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001443 src_size = TRIPLE_SIZE(src->sizes);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001444 get_occurance(src->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001445 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001446 src->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001447 memcpy(dup, src, sizeof(*src));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001448 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
Eric Biederman0babc1c2003-05-09 02:39:00 +00001449 return dup;
1450}
1451
1452static struct triple *new_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001453 int op, struct type *type, int lhs, int rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001454{
1455 struct triple *ret;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001456 struct occurance *occurance;
1457 occurance = new_occurance(state);
1458 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001459 return ret;
1460}
1461
1462static struct triple *build_triple(struct compile_state *state,
1463 int op, struct type *type, struct triple *left, struct triple *right,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001464 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00001465{
1466 struct triple *ret;
1467 size_t count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001468 ret = alloc_triple(state, op, type, -1, -1, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001469 count = TRIPLE_SIZE(ret->sizes);
1470 if (count > 0) {
1471 ret->param[0] = left;
1472 }
1473 if (count > 1) {
1474 ret->param[1] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001475 }
1476 return ret;
1477}
1478
Eric Biederman0babc1c2003-05-09 02:39:00 +00001479static struct triple *triple(struct compile_state *state,
1480 int op, struct type *type, struct triple *left, struct triple *right)
1481{
1482 struct triple *ret;
1483 size_t count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001484 ret = new_triple(state, op, type, -1, -1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001485 count = TRIPLE_SIZE(ret->sizes);
1486 if (count >= 1) {
1487 ret->param[0] = left;
1488 }
1489 if (count >= 2) {
1490 ret->param[1] = right;
1491 }
1492 return ret;
1493}
1494
1495static struct triple *branch(struct compile_state *state,
1496 struct triple *targ, struct triple *test)
1497{
1498 struct triple *ret;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001499 ret = new_triple(state, OP_BRANCH, &void_type, -1, test?1:0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001500 if (test) {
1501 RHS(ret, 0) = test;
1502 }
1503 TARG(ret, 0) = targ;
1504 /* record the branch target was used */
1505 if (!targ || (targ->op != OP_LABEL)) {
1506 internal_error(state, 0, "branch not to label");
1507 use_triple(targ, ret);
1508 }
1509 return ret;
1510}
1511
1512
Eric Biedermanb138ac82003-04-22 18:44:01 +00001513static void insert_triple(struct compile_state *state,
1514 struct triple *first, struct triple *ptr)
1515{
1516 if (ptr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001517 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00001518 internal_error(state, ptr, "expression already used");
1519 }
1520 ptr->next = first;
1521 ptr->prev = first->prev;
1522 ptr->prev->next = ptr;
1523 ptr->next->prev = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00001524 if ((ptr->prev->op == OP_BRANCH) &&
1525 TRIPLE_RHS(ptr->prev->sizes)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00001526 unuse_triple(first, ptr->prev);
1527 use_triple(ptr, ptr->prev);
1528 }
1529 }
1530}
1531
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001532static int triple_stores_block(struct compile_state *state, struct triple *ins)
1533{
1534 /* This function is used to determine if u.block
1535 * is utilized to store the current block number.
1536 */
1537 int stores_block;
1538 valid_ins(state, ins);
1539 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
1540 return stores_block;
1541}
1542
1543static struct block *block_of_triple(struct compile_state *state,
1544 struct triple *ins)
1545{
1546 struct triple *first;
Eric Biederman83b991a2003-10-11 06:20:25 +00001547 if (!ins) {
1548 return 0;
1549 }
1550 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001551 while(ins != first && !triple_stores_block(state, ins)) {
1552 if (ins == ins->prev) {
1553 internal_error(state, 0, "ins == ins->prev?");
1554 }
1555 ins = ins->prev;
1556 }
1557 if (!triple_stores_block(state, ins)) {
1558 internal_error(state, ins, "Cannot find block");
1559 }
1560 return ins->u.block;
1561}
1562
Eric Biedermanb138ac82003-04-22 18:44:01 +00001563static struct triple *pre_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 /* If I am an OP_PIECE jump to the real instruction */
1570 if (base->op == OP_PIECE) {
1571 base = MISC(base, 0);
1572 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001573 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001574 get_occurance(base->occurance);
1575 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001576 if (triple_stores_block(state, ret)) {
1577 ret->u.block = block;
1578 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001579 insert_triple(state, base, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001580 if (block->first == base) {
1581 block->first = ret;
1582 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001583 return ret;
1584}
1585
1586static struct triple *post_triple(struct compile_state *state,
1587 struct triple *base,
1588 int op, struct type *type, struct triple *left, struct triple *right)
1589{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001590 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001591 struct triple *ret;
Eric Biedermand3283ec2003-06-18 11:03:18 +00001592 int zlhs;
1593 /* If I am an OP_PIECE jump to the real instruction */
1594 if (base->op == OP_PIECE) {
1595 base = MISC(base, 0);
1596 }
1597 /* If I have a left hand side skip over it */
1598 zlhs = TRIPLE_LHS(base->sizes);
Eric Biederman530b5192003-07-01 10:05:30 +00001599 if (zlhs) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00001600 base = LHS(base, zlhs - 1);
1601 }
1602
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001603 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001604 get_occurance(base->occurance);
1605 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001606 if (triple_stores_block(state, ret)) {
1607 ret->u.block = block;
1608 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001609 insert_triple(state, base->next, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001610 if (block->last == base) {
1611 block->last = ret;
1612 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001613 return ret;
1614}
1615
1616static struct triple *label(struct compile_state *state)
1617{
1618 /* Labels don't get a type */
1619 struct triple *result;
1620 result = triple(state, OP_LABEL, &void_type, 0, 0);
1621 return result;
1622}
1623
Eric Biederman0babc1c2003-05-09 02:39:00 +00001624static void display_triple(FILE *fp, struct triple *ins)
1625{
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001626 struct occurance *ptr;
1627 const char *reg;
1628 char pre, post;
1629 pre = post = ' ';
1630 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
1631 pre = '^';
1632 }
1633 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
1634 post = 'v';
1635 }
1636 reg = arch_reg_str(ID_REG(ins->id));
Eric Biederman0babc1c2003-05-09 02:39:00 +00001637 if (ins->op == OP_INTCONST) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001638 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s <0x%08lx> ",
1639 ins, pre, post, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00001640 (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00001641 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001642 else if (ins->op == OP_ADDRCONST) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001643 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s %-10p <0x%08lx>",
1644 ins, pre, post, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00001645 MISC(ins, 0), (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00001646 }
1647 else {
1648 int i, count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001649 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s",
1650 ins, pre, post, reg, ins->template_id, tops(ins->op));
Eric Biederman0babc1c2003-05-09 02:39:00 +00001651 count = TRIPLE_SIZE(ins->sizes);
1652 for(i = 0; i < count; i++) {
1653 fprintf(fp, " %-10p", ins->param[i]);
1654 }
1655 for(; i < 2; i++) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00001656 fprintf(fp, " ");
Eric Biederman0babc1c2003-05-09 02:39:00 +00001657 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001658 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001659 fprintf(fp, " @");
1660 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
1661 fprintf(fp, " %s,%s:%d.%d",
1662 ptr->function,
1663 ptr->filename,
1664 ptr->line,
1665 ptr->col);
1666 }
1667 fprintf(fp, "\n");
Eric Biederman530b5192003-07-01 10:05:30 +00001668#if 0
1669 {
1670 struct triple_set *user;
1671 for(user = ptr->use; user; user = user->next) {
1672 fprintf(fp, "use: %p\n", user->member);
1673 }
1674 }
1675#endif
Eric Biederman0babc1c2003-05-09 02:39:00 +00001676 fflush(fp);
1677}
1678
Eric Biederman83b991a2003-10-11 06:20:25 +00001679static void display_func(FILE *fp, struct triple *func)
1680{
1681 struct triple *first, *ins;
1682 first = ins = RHS(func, 0);
1683 do {
1684 display_triple(fp, ins);
1685 ins = ins->next;
1686 } while(ins != first);
1687}
1688
1689static int triple_is_pure(struct compile_state *state, struct triple *ins, unsigned id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001690{
1691 /* Does the triple have no side effects.
1692 * I.e. Rexecuting the triple with the same arguments
1693 * gives the same value.
1694 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001695 unsigned pure;
1696 valid_ins(state, ins);
1697 pure = PURE_BITS(table_ops[ins->op].flags);
1698 if ((pure != PURE) && (pure != IMPURE)) {
1699 internal_error(state, 0, "Purity of %s not known\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +00001700 tops(ins->op));
Eric Biedermanb138ac82003-04-22 18:44:01 +00001701 }
Eric Biederman83b991a2003-10-11 06:20:25 +00001702 return (pure == PURE) && !(id & TRIPLE_FLAG_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001703}
1704
Eric Biederman0babc1c2003-05-09 02:39:00 +00001705static int triple_is_branch(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001706{
1707 /* This function is used to determine which triples need
1708 * a register.
1709 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001710 int is_branch;
1711 valid_ins(state, ins);
1712 is_branch = (table_ops[ins->op].targ != 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001713 return is_branch;
1714}
1715
Eric Biederman530b5192003-07-01 10:05:30 +00001716static int triple_is_cond_branch(struct compile_state *state, struct triple *ins)
1717{
1718 /* A conditional branch has the condition argument as a single
1719 * RHS parameter.
1720 */
1721 return triple_is_branch(state, ins) &&
1722 (TRIPLE_RHS(ins->sizes) == 1);
1723}
1724
1725static int triple_is_uncond_branch(struct compile_state *state, struct triple *ins)
1726{
1727 /* A unconditional branch has no RHS parameters.
1728 */
1729 return triple_is_branch(state, ins) &&
1730 (TRIPLE_RHS(ins->sizes) == 0);
1731}
1732
Eric Biederman0babc1c2003-05-09 02:39:00 +00001733static int triple_is_def(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001734{
1735 /* This function is used to determine which triples need
1736 * a register.
1737 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001738 int is_def;
1739 valid_ins(state, ins);
1740 is_def = (table_ops[ins->op].flags & DEF) == DEF;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001741 return is_def;
1742}
1743
Eric Biederman83b991a2003-10-11 06:20:25 +00001744static int triple_is_structural(struct compile_state *state, struct triple *ins)
1745{
1746 int is_structural;
1747 valid_ins(state, ins);
1748 is_structural = (table_ops[ins->op].flags & STRUCTURAL) == STRUCTURAL;
1749 return is_structural;
1750}
1751
Eric Biederman0babc1c2003-05-09 02:39:00 +00001752static struct triple **triple_iter(struct compile_state *state,
1753 size_t count, struct triple **vector,
1754 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001755{
1756 struct triple **ret;
1757 ret = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00001758 if (count) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00001759 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001760 ret = vector;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001761 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001762 else if ((last >= vector) && (last < (vector + count - 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00001763 ret = last + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001764 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001765 }
1766 return ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00001767
Eric Biedermanb138ac82003-04-22 18:44:01 +00001768}
1769
1770static struct triple **triple_lhs(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00001771 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001772{
Eric Biederman0babc1c2003-05-09 02:39:00 +00001773 return triple_iter(state, TRIPLE_LHS(ins->sizes), &LHS(ins,0),
1774 ins, last);
1775}
1776
1777static struct triple **triple_rhs(struct compile_state *state,
1778 struct triple *ins, struct triple **last)
1779{
1780 return triple_iter(state, TRIPLE_RHS(ins->sizes), &RHS(ins,0),
1781 ins, last);
1782}
1783
1784static struct triple **triple_misc(struct compile_state *state,
1785 struct triple *ins, struct triple **last)
1786{
1787 return triple_iter(state, TRIPLE_MISC(ins->sizes), &MISC(ins,0),
1788 ins, last);
1789}
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001790
Eric Biederman0babc1c2003-05-09 02:39:00 +00001791static struct triple **triple_targ(struct compile_state *state,
1792 struct triple *ins, struct triple **last)
1793{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001794 size_t count;
1795 struct triple **ret, **vector;
1796 ret = 0;
1797 count = TRIPLE_TARG(ins->sizes);
1798 vector = &TARG(ins, 0);
1799 if (count) {
1800 if (!last) {
1801 ret = vector;
1802 }
1803 else if ((last >= vector) && (last < (vector + count - 1))) {
1804 ret = last + 1;
1805 }
1806 else if ((last == (vector + count - 1)) &&
1807 TRIPLE_RHS(ins->sizes)) {
1808 ret = &ins->next;
1809 }
1810 }
1811 return ret;
1812}
1813
1814
1815static void verify_use(struct compile_state *state,
1816 struct triple *user, struct triple *used)
1817{
1818 int size, i;
1819 size = TRIPLE_SIZE(user->sizes);
1820 for(i = 0; i < size; i++) {
1821 if (user->param[i] == used) {
1822 break;
1823 }
1824 }
1825 if (triple_is_branch(state, user)) {
1826 if (user->next == used) {
1827 i = -1;
1828 }
1829 }
1830 if (i == size) {
1831 internal_error(state, user, "%s(%p) does not use %s(%p)",
1832 tops(user->op), user, tops(used->op), used);
1833 }
1834}
1835
1836static int find_rhs_use(struct compile_state *state,
1837 struct triple *user, struct triple *used)
1838{
1839 struct triple **param;
1840 int size, i;
1841 verify_use(state, user, used);
1842 size = TRIPLE_RHS(user->sizes);
1843 param = &RHS(user, 0);
1844 for(i = 0; i < size; i++) {
1845 if (param[i] == used) {
1846 return i;
1847 }
1848 }
1849 return -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001850}
1851
1852static void free_triple(struct compile_state *state, struct triple *ptr)
1853{
Eric Biederman0babc1c2003-05-09 02:39:00 +00001854 size_t size;
1855 size = sizeof(*ptr) - sizeof(ptr->param) +
1856 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr->sizes));
Eric Biedermanb138ac82003-04-22 18:44:01 +00001857 ptr->prev->next = ptr->next;
1858 ptr->next->prev = ptr->prev;
1859 if (ptr->use) {
1860 internal_error(state, ptr, "ptr->use != 0");
1861 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001862 put_occurance(ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001863 memset(ptr, -1, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001864 xfree(ptr);
1865}
1866
1867static void release_triple(struct compile_state *state, struct triple *ptr)
1868{
1869 struct triple_set *set, *next;
1870 struct triple **expr;
Eric Biederman66fe2222003-07-04 15:14:04 +00001871 struct block *block;
1872 /* Make certain the we are not the first or last element of a block */
1873 block = block_of_triple(state, ptr);
Eric Biederman83b991a2003-10-11 06:20:25 +00001874 if (block) {
1875 if ((block->last == ptr) && (block->first == ptr)) {
1876 block->last = block->first = 0;
1877 }
1878 else if (block->last == ptr) {
1879 block->last = ptr->prev;
1880 }
1881 else if (block->first == ptr) {
1882 block->first = ptr->next;
1883 }
Eric Biederman66fe2222003-07-04 15:14:04 +00001884 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001885 /* Remove ptr from use chains where it is the user */
1886 expr = triple_rhs(state, ptr, 0);
1887 for(; expr; expr = triple_rhs(state, ptr, expr)) {
1888 if (*expr) {
1889 unuse_triple(*expr, ptr);
1890 }
1891 }
1892 expr = triple_lhs(state, ptr, 0);
1893 for(; expr; expr = triple_lhs(state, ptr, expr)) {
1894 if (*expr) {
1895 unuse_triple(*expr, ptr);
1896 }
1897 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001898 expr = triple_misc(state, ptr, 0);
1899 for(; expr; expr = triple_misc(state, ptr, expr)) {
1900 if (*expr) {
1901 unuse_triple(*expr, ptr);
1902 }
1903 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001904 expr = triple_targ(state, ptr, 0);
1905 for(; expr; expr = triple_targ(state, ptr, expr)) {
1906 if (*expr) {
1907 unuse_triple(*expr, ptr);
1908 }
1909 }
1910 /* Reomve ptr from use chains where it is used */
1911 for(set = ptr->use; set; set = next) {
1912 next = set->next;
1913 expr = triple_rhs(state, set->member, 0);
1914 for(; expr; expr = triple_rhs(state, set->member, expr)) {
1915 if (*expr == ptr) {
1916 *expr = &zero_triple;
1917 }
1918 }
1919 expr = triple_lhs(state, set->member, 0);
1920 for(; expr; expr = triple_lhs(state, set->member, expr)) {
1921 if (*expr == ptr) {
1922 *expr = &zero_triple;
1923 }
1924 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001925 expr = triple_misc(state, set->member, 0);
1926 for(; expr; expr = triple_misc(state, set->member, expr)) {
1927 if (*expr == ptr) {
1928 *expr = &zero_triple;
1929 }
1930 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001931 expr = triple_targ(state, set->member, 0);
1932 for(; expr; expr = triple_targ(state, set->member, expr)) {
1933 if (*expr == ptr) {
1934 *expr = &zero_triple;
1935 }
1936 }
1937 unuse_triple(ptr, set->member);
1938 }
1939 free_triple(state, ptr);
1940}
1941
1942static void print_triple(struct compile_state *state, struct triple *ptr);
1943
1944#define TOK_UNKNOWN 0
1945#define TOK_SPACE 1
1946#define TOK_SEMI 2
1947#define TOK_LBRACE 3
1948#define TOK_RBRACE 4
1949#define TOK_COMMA 5
1950#define TOK_EQ 6
1951#define TOK_COLON 7
1952#define TOK_LBRACKET 8
1953#define TOK_RBRACKET 9
1954#define TOK_LPAREN 10
1955#define TOK_RPAREN 11
1956#define TOK_STAR 12
1957#define TOK_DOTS 13
1958#define TOK_MORE 14
1959#define TOK_LESS 15
1960#define TOK_TIMESEQ 16
1961#define TOK_DIVEQ 17
1962#define TOK_MODEQ 18
1963#define TOK_PLUSEQ 19
1964#define TOK_MINUSEQ 20
1965#define TOK_SLEQ 21
1966#define TOK_SREQ 22
1967#define TOK_ANDEQ 23
1968#define TOK_XOREQ 24
1969#define TOK_OREQ 25
1970#define TOK_EQEQ 26
1971#define TOK_NOTEQ 27
1972#define TOK_QUEST 28
1973#define TOK_LOGOR 29
1974#define TOK_LOGAND 30
1975#define TOK_OR 31
1976#define TOK_AND 32
1977#define TOK_XOR 33
1978#define TOK_LESSEQ 34
1979#define TOK_MOREEQ 35
1980#define TOK_SL 36
1981#define TOK_SR 37
1982#define TOK_PLUS 38
1983#define TOK_MINUS 39
1984#define TOK_DIV 40
1985#define TOK_MOD 41
1986#define TOK_PLUSPLUS 42
1987#define TOK_MINUSMINUS 43
1988#define TOK_BANG 44
1989#define TOK_ARROW 45
1990#define TOK_DOT 46
1991#define TOK_TILDE 47
1992#define TOK_LIT_STRING 48
1993#define TOK_LIT_CHAR 49
1994#define TOK_LIT_INT 50
1995#define TOK_LIT_FLOAT 51
1996#define TOK_MACRO 52
1997#define TOK_CONCATENATE 53
1998
1999#define TOK_IDENT 54
2000#define TOK_STRUCT_NAME 55
2001#define TOK_ENUM_CONST 56
2002#define TOK_TYPE_NAME 57
2003
2004#define TOK_AUTO 58
2005#define TOK_BREAK 59
2006#define TOK_CASE 60
2007#define TOK_CHAR 61
2008#define TOK_CONST 62
2009#define TOK_CONTINUE 63
2010#define TOK_DEFAULT 64
2011#define TOK_DO 65
2012#define TOK_DOUBLE 66
2013#define TOK_ELSE 67
2014#define TOK_ENUM 68
2015#define TOK_EXTERN 69
2016#define TOK_FLOAT 70
2017#define TOK_FOR 71
2018#define TOK_GOTO 72
2019#define TOK_IF 73
2020#define TOK_INLINE 74
2021#define TOK_INT 75
2022#define TOK_LONG 76
2023#define TOK_REGISTER 77
2024#define TOK_RESTRICT 78
2025#define TOK_RETURN 79
2026#define TOK_SHORT 80
2027#define TOK_SIGNED 81
2028#define TOK_SIZEOF 82
2029#define TOK_STATIC 83
2030#define TOK_STRUCT 84
2031#define TOK_SWITCH 85
2032#define TOK_TYPEDEF 86
2033#define TOK_UNION 87
2034#define TOK_UNSIGNED 88
2035#define TOK_VOID 89
2036#define TOK_VOLATILE 90
2037#define TOK_WHILE 91
2038#define TOK_ASM 92
2039#define TOK_ATTRIBUTE 93
2040#define TOK_ALIGNOF 94
2041#define TOK_FIRST_KEYWORD TOK_AUTO
2042#define TOK_LAST_KEYWORD TOK_ALIGNOF
2043
2044#define TOK_DEFINE 100
2045#define TOK_UNDEF 101
2046#define TOK_INCLUDE 102
2047#define TOK_LINE 103
2048#define TOK_ERROR 104
2049#define TOK_WARNING 105
2050#define TOK_PRAGMA 106
2051#define TOK_IFDEF 107
2052#define TOK_IFNDEF 108
2053#define TOK_ELIF 109
2054#define TOK_ENDIF 110
2055
2056#define TOK_FIRST_MACRO TOK_DEFINE
2057#define TOK_LAST_MACRO TOK_ENDIF
2058
2059#define TOK_EOF 111
2060
2061static const char *tokens[] = {
2062[TOK_UNKNOWN ] = "unknown",
2063[TOK_SPACE ] = ":space:",
2064[TOK_SEMI ] = ";",
2065[TOK_LBRACE ] = "{",
2066[TOK_RBRACE ] = "}",
2067[TOK_COMMA ] = ",",
2068[TOK_EQ ] = "=",
2069[TOK_COLON ] = ":",
2070[TOK_LBRACKET ] = "[",
2071[TOK_RBRACKET ] = "]",
2072[TOK_LPAREN ] = "(",
2073[TOK_RPAREN ] = ")",
2074[TOK_STAR ] = "*",
2075[TOK_DOTS ] = "...",
2076[TOK_MORE ] = ">",
2077[TOK_LESS ] = "<",
2078[TOK_TIMESEQ ] = "*=",
2079[TOK_DIVEQ ] = "/=",
2080[TOK_MODEQ ] = "%=",
2081[TOK_PLUSEQ ] = "+=",
2082[TOK_MINUSEQ ] = "-=",
2083[TOK_SLEQ ] = "<<=",
2084[TOK_SREQ ] = ">>=",
2085[TOK_ANDEQ ] = "&=",
2086[TOK_XOREQ ] = "^=",
2087[TOK_OREQ ] = "|=",
2088[TOK_EQEQ ] = "==",
2089[TOK_NOTEQ ] = "!=",
2090[TOK_QUEST ] = "?",
2091[TOK_LOGOR ] = "||",
2092[TOK_LOGAND ] = "&&",
2093[TOK_OR ] = "|",
2094[TOK_AND ] = "&",
2095[TOK_XOR ] = "^",
2096[TOK_LESSEQ ] = "<=",
2097[TOK_MOREEQ ] = ">=",
2098[TOK_SL ] = "<<",
2099[TOK_SR ] = ">>",
2100[TOK_PLUS ] = "+",
2101[TOK_MINUS ] = "-",
2102[TOK_DIV ] = "/",
2103[TOK_MOD ] = "%",
2104[TOK_PLUSPLUS ] = "++",
2105[TOK_MINUSMINUS ] = "--",
2106[TOK_BANG ] = "!",
2107[TOK_ARROW ] = "->",
2108[TOK_DOT ] = ".",
2109[TOK_TILDE ] = "~",
2110[TOK_LIT_STRING ] = ":string:",
2111[TOK_IDENT ] = ":ident:",
2112[TOK_TYPE_NAME ] = ":typename:",
2113[TOK_LIT_CHAR ] = ":char:",
2114[TOK_LIT_INT ] = ":integer:",
2115[TOK_LIT_FLOAT ] = ":float:",
2116[TOK_MACRO ] = "#",
2117[TOK_CONCATENATE ] = "##",
2118
2119[TOK_AUTO ] = "auto",
2120[TOK_BREAK ] = "break",
2121[TOK_CASE ] = "case",
2122[TOK_CHAR ] = "char",
2123[TOK_CONST ] = "const",
2124[TOK_CONTINUE ] = "continue",
2125[TOK_DEFAULT ] = "default",
2126[TOK_DO ] = "do",
2127[TOK_DOUBLE ] = "double",
2128[TOK_ELSE ] = "else",
2129[TOK_ENUM ] = "enum",
2130[TOK_EXTERN ] = "extern",
2131[TOK_FLOAT ] = "float",
2132[TOK_FOR ] = "for",
2133[TOK_GOTO ] = "goto",
2134[TOK_IF ] = "if",
2135[TOK_INLINE ] = "inline",
2136[TOK_INT ] = "int",
2137[TOK_LONG ] = "long",
2138[TOK_REGISTER ] = "register",
2139[TOK_RESTRICT ] = "restrict",
2140[TOK_RETURN ] = "return",
2141[TOK_SHORT ] = "short",
2142[TOK_SIGNED ] = "signed",
2143[TOK_SIZEOF ] = "sizeof",
2144[TOK_STATIC ] = "static",
2145[TOK_STRUCT ] = "struct",
2146[TOK_SWITCH ] = "switch",
2147[TOK_TYPEDEF ] = "typedef",
2148[TOK_UNION ] = "union",
2149[TOK_UNSIGNED ] = "unsigned",
2150[TOK_VOID ] = "void",
2151[TOK_VOLATILE ] = "volatile",
2152[TOK_WHILE ] = "while",
2153[TOK_ASM ] = "asm",
2154[TOK_ATTRIBUTE ] = "__attribute__",
2155[TOK_ALIGNOF ] = "__alignof__",
2156
2157[TOK_DEFINE ] = "define",
2158[TOK_UNDEF ] = "undef",
2159[TOK_INCLUDE ] = "include",
2160[TOK_LINE ] = "line",
2161[TOK_ERROR ] = "error",
2162[TOK_WARNING ] = "warning",
2163[TOK_PRAGMA ] = "pragma",
2164[TOK_IFDEF ] = "ifdef",
2165[TOK_IFNDEF ] = "ifndef",
2166[TOK_ELIF ] = "elif",
2167[TOK_ENDIF ] = "endif",
2168
2169[TOK_EOF ] = "EOF",
2170};
2171
2172static unsigned int hash(const char *str, int str_len)
2173{
2174 unsigned int hash;
2175 const char *end;
2176 end = str + str_len;
2177 hash = 0;
2178 for(; str < end; str++) {
2179 hash = (hash *263) + *str;
2180 }
2181 hash = hash & (HASH_TABLE_SIZE -1);
2182 return hash;
2183}
2184
2185static struct hash_entry *lookup(
2186 struct compile_state *state, const char *name, int name_len)
2187{
2188 struct hash_entry *entry;
2189 unsigned int index;
2190 index = hash(name, name_len);
2191 entry = state->hash_table[index];
2192 while(entry &&
2193 ((entry->name_len != name_len) ||
2194 (memcmp(entry->name, name, name_len) != 0))) {
2195 entry = entry->next;
2196 }
2197 if (!entry) {
2198 char *new_name;
2199 /* Get a private copy of the name */
2200 new_name = xmalloc(name_len + 1, "hash_name");
2201 memcpy(new_name, name, name_len);
2202 new_name[name_len] = '\0';
2203
2204 /* Create a new hash entry */
2205 entry = xcmalloc(sizeof(*entry), "hash_entry");
2206 entry->next = state->hash_table[index];
2207 entry->name = new_name;
2208 entry->name_len = name_len;
2209
2210 /* Place the new entry in the hash table */
2211 state->hash_table[index] = entry;
2212 }
2213 return entry;
2214}
2215
2216static void ident_to_keyword(struct compile_state *state, struct token *tk)
2217{
2218 struct hash_entry *entry;
2219 entry = tk->ident;
2220 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
2221 (entry->tok == TOK_ENUM_CONST) ||
2222 ((entry->tok >= TOK_FIRST_KEYWORD) &&
2223 (entry->tok <= TOK_LAST_KEYWORD)))) {
2224 tk->tok = entry->tok;
2225 }
2226}
2227
2228static void ident_to_macro(struct compile_state *state, struct token *tk)
2229{
2230 struct hash_entry *entry;
2231 entry = tk->ident;
2232 if (entry &&
2233 (entry->tok >= TOK_FIRST_MACRO) &&
2234 (entry->tok <= TOK_LAST_MACRO)) {
2235 tk->tok = entry->tok;
2236 }
2237}
2238
2239static void hash_keyword(
2240 struct compile_state *state, const char *keyword, int tok)
2241{
2242 struct hash_entry *entry;
2243 entry = lookup(state, keyword, strlen(keyword));
2244 if (entry && entry->tok != TOK_UNKNOWN) {
2245 die("keyword %s already hashed", keyword);
2246 }
2247 entry->tok = tok;
2248}
2249
2250static void symbol(
2251 struct compile_state *state, struct hash_entry *ident,
2252 struct symbol **chain, struct triple *def, struct type *type)
2253{
2254 struct symbol *sym;
2255 if (*chain && ((*chain)->scope_depth == state->scope_depth)) {
2256 error(state, 0, "%s already defined", ident->name);
2257 }
2258 sym = xcmalloc(sizeof(*sym), "symbol");
2259 sym->ident = ident;
2260 sym->def = def;
2261 sym->type = type;
2262 sym->scope_depth = state->scope_depth;
2263 sym->next = *chain;
2264 *chain = sym;
2265}
2266
Eric Biederman153ea352003-06-20 14:43:20 +00002267static void label_symbol(struct compile_state *state,
2268 struct hash_entry *ident, struct triple *label)
2269{
2270 struct symbol *sym;
2271 if (ident->sym_label) {
2272 error(state, 0, "label %s already defined", ident->name);
2273 }
2274 sym = xcmalloc(sizeof(*sym), "label");
2275 sym->ident = ident;
2276 sym->def = label;
2277 sym->type = &void_type;
2278 sym->scope_depth = FUNCTION_SCOPE_DEPTH;
2279 sym->next = 0;
2280 ident->sym_label = sym;
2281}
2282
Eric Biedermanb138ac82003-04-22 18:44:01 +00002283static void start_scope(struct compile_state *state)
2284{
2285 state->scope_depth++;
2286}
2287
2288static void end_scope_syms(struct symbol **chain, int depth)
2289{
2290 struct symbol *sym, *next;
2291 sym = *chain;
2292 while(sym && (sym->scope_depth == depth)) {
2293 next = sym->next;
2294 xfree(sym);
2295 sym = next;
2296 }
2297 *chain = sym;
2298}
2299
2300static void end_scope(struct compile_state *state)
2301{
2302 int i;
2303 int depth;
2304 /* Walk through the hash table and remove all symbols
2305 * in the current scope.
2306 */
2307 depth = state->scope_depth;
2308 for(i = 0; i < HASH_TABLE_SIZE; i++) {
2309 struct hash_entry *entry;
2310 entry = state->hash_table[i];
2311 while(entry) {
Eric Biederman83b991a2003-10-11 06:20:25 +00002312 end_scope_syms(&entry->sym_label, depth);
2313 end_scope_syms(&entry->sym_tag, depth);
2314 end_scope_syms(&entry->sym_ident, depth);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002315 entry = entry->next;
2316 }
2317 }
2318 state->scope_depth = depth - 1;
2319}
2320
2321static void register_keywords(struct compile_state *state)
2322{
2323 hash_keyword(state, "auto", TOK_AUTO);
2324 hash_keyword(state, "break", TOK_BREAK);
2325 hash_keyword(state, "case", TOK_CASE);
2326 hash_keyword(state, "char", TOK_CHAR);
2327 hash_keyword(state, "const", TOK_CONST);
2328 hash_keyword(state, "continue", TOK_CONTINUE);
2329 hash_keyword(state, "default", TOK_DEFAULT);
2330 hash_keyword(state, "do", TOK_DO);
2331 hash_keyword(state, "double", TOK_DOUBLE);
2332 hash_keyword(state, "else", TOK_ELSE);
2333 hash_keyword(state, "enum", TOK_ENUM);
2334 hash_keyword(state, "extern", TOK_EXTERN);
2335 hash_keyword(state, "float", TOK_FLOAT);
2336 hash_keyword(state, "for", TOK_FOR);
2337 hash_keyword(state, "goto", TOK_GOTO);
2338 hash_keyword(state, "if", TOK_IF);
2339 hash_keyword(state, "inline", TOK_INLINE);
2340 hash_keyword(state, "int", TOK_INT);
2341 hash_keyword(state, "long", TOK_LONG);
2342 hash_keyword(state, "register", TOK_REGISTER);
2343 hash_keyword(state, "restrict", TOK_RESTRICT);
2344 hash_keyword(state, "return", TOK_RETURN);
2345 hash_keyword(state, "short", TOK_SHORT);
2346 hash_keyword(state, "signed", TOK_SIGNED);
2347 hash_keyword(state, "sizeof", TOK_SIZEOF);
2348 hash_keyword(state, "static", TOK_STATIC);
2349 hash_keyword(state, "struct", TOK_STRUCT);
2350 hash_keyword(state, "switch", TOK_SWITCH);
2351 hash_keyword(state, "typedef", TOK_TYPEDEF);
2352 hash_keyword(state, "union", TOK_UNION);
2353 hash_keyword(state, "unsigned", TOK_UNSIGNED);
2354 hash_keyword(state, "void", TOK_VOID);
2355 hash_keyword(state, "volatile", TOK_VOLATILE);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002356 hash_keyword(state, "__volatile__", TOK_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002357 hash_keyword(state, "while", TOK_WHILE);
2358 hash_keyword(state, "asm", TOK_ASM);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002359 hash_keyword(state, "__asm__", TOK_ASM);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002360 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
2361 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
2362}
2363
2364static void register_macro_keywords(struct compile_state *state)
2365{
2366 hash_keyword(state, "define", TOK_DEFINE);
2367 hash_keyword(state, "undef", TOK_UNDEF);
2368 hash_keyword(state, "include", TOK_INCLUDE);
2369 hash_keyword(state, "line", TOK_LINE);
2370 hash_keyword(state, "error", TOK_ERROR);
2371 hash_keyword(state, "warning", TOK_WARNING);
2372 hash_keyword(state, "pragma", TOK_PRAGMA);
2373 hash_keyword(state, "ifdef", TOK_IFDEF);
2374 hash_keyword(state, "ifndef", TOK_IFNDEF);
2375 hash_keyword(state, "elif", TOK_ELIF);
2376 hash_keyword(state, "endif", TOK_ENDIF);
2377}
2378
2379static int spacep(int c)
2380{
2381 int ret = 0;
2382 switch(c) {
2383 case ' ':
2384 case '\t':
2385 case '\f':
2386 case '\v':
2387 case '\r':
2388 case '\n':
2389 ret = 1;
2390 break;
2391 }
2392 return ret;
2393}
2394
2395static int digitp(int c)
2396{
2397 int ret = 0;
2398 switch(c) {
2399 case '0': case '1': case '2': case '3': case '4':
2400 case '5': case '6': case '7': case '8': case '9':
2401 ret = 1;
2402 break;
2403 }
2404 return ret;
2405}
Eric Biederman8d9c1232003-06-17 08:42:17 +00002406static int digval(int c)
2407{
2408 int val = -1;
2409 if ((c >= '0') && (c <= '9')) {
2410 val = c - '0';
2411 }
2412 return val;
2413}
Eric Biedermanb138ac82003-04-22 18:44:01 +00002414
2415static int hexdigitp(int c)
2416{
2417 int ret = 0;
2418 switch(c) {
2419 case '0': case '1': case '2': case '3': case '4':
2420 case '5': case '6': case '7': case '8': case '9':
2421 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2422 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2423 ret = 1;
2424 break;
2425 }
2426 return ret;
2427}
2428static int hexdigval(int c)
2429{
2430 int val = -1;
2431 if ((c >= '0') && (c <= '9')) {
2432 val = c - '0';
2433 }
2434 else if ((c >= 'A') && (c <= 'F')) {
2435 val = 10 + (c - 'A');
2436 }
2437 else if ((c >= 'a') && (c <= 'f')) {
2438 val = 10 + (c - 'a');
2439 }
2440 return val;
2441}
2442
2443static int octdigitp(int c)
2444{
2445 int ret = 0;
2446 switch(c) {
2447 case '0': case '1': case '2': case '3':
2448 case '4': case '5': case '6': case '7':
2449 ret = 1;
2450 break;
2451 }
2452 return ret;
2453}
2454static int octdigval(int c)
2455{
2456 int val = -1;
2457 if ((c >= '0') && (c <= '7')) {
2458 val = c - '0';
2459 }
2460 return val;
2461}
2462
2463static int letterp(int c)
2464{
2465 int ret = 0;
2466 switch(c) {
2467 case 'a': case 'b': case 'c': case 'd': case 'e':
2468 case 'f': case 'g': case 'h': case 'i': case 'j':
2469 case 'k': case 'l': case 'm': case 'n': case 'o':
2470 case 'p': case 'q': case 'r': case 's': case 't':
2471 case 'u': case 'v': case 'w': case 'x': case 'y':
2472 case 'z':
2473 case 'A': case 'B': case 'C': case 'D': case 'E':
2474 case 'F': case 'G': case 'H': case 'I': case 'J':
2475 case 'K': case 'L': case 'M': case 'N': case 'O':
2476 case 'P': case 'Q': case 'R': case 'S': case 'T':
2477 case 'U': case 'V': case 'W': case 'X': case 'Y':
2478 case 'Z':
2479 case '_':
2480 ret = 1;
2481 break;
2482 }
2483 return ret;
2484}
2485
2486static int char_value(struct compile_state *state,
2487 const signed char **strp, const signed char *end)
2488{
2489 const signed char *str;
2490 int c;
2491 str = *strp;
2492 c = *str++;
2493 if ((c == '\\') && (str < end)) {
2494 switch(*str) {
2495 case 'n': c = '\n'; str++; break;
2496 case 't': c = '\t'; str++; break;
2497 case 'v': c = '\v'; str++; break;
2498 case 'b': c = '\b'; str++; break;
2499 case 'r': c = '\r'; str++; break;
2500 case 'f': c = '\f'; str++; break;
2501 case 'a': c = '\a'; str++; break;
2502 case '\\': c = '\\'; str++; break;
2503 case '?': c = '?'; str++; break;
2504 case '\'': c = '\''; str++; break;
2505 case '"': c = '"'; break;
2506 case 'x':
2507 c = 0;
2508 str++;
2509 while((str < end) && hexdigitp(*str)) {
2510 c <<= 4;
2511 c += hexdigval(*str);
2512 str++;
2513 }
2514 break;
2515 case '0': case '1': case '2': case '3':
2516 case '4': case '5': case '6': case '7':
2517 c = 0;
2518 while((str < end) && octdigitp(*str)) {
2519 c <<= 3;
2520 c += octdigval(*str);
2521 str++;
2522 }
2523 break;
2524 default:
2525 error(state, 0, "Invalid character constant");
2526 break;
2527 }
2528 }
2529 *strp = str;
2530 return c;
2531}
2532
2533static char *after_digits(char *ptr, char *end)
2534{
2535 while((ptr < end) && digitp(*ptr)) {
2536 ptr++;
2537 }
2538 return ptr;
2539}
2540
2541static char *after_octdigits(char *ptr, char *end)
2542{
2543 while((ptr < end) && octdigitp(*ptr)) {
2544 ptr++;
2545 }
2546 return ptr;
2547}
2548
2549static char *after_hexdigits(char *ptr, char *end)
2550{
2551 while((ptr < end) && hexdigitp(*ptr)) {
2552 ptr++;
2553 }
2554 return ptr;
2555}
2556
2557static void save_string(struct compile_state *state,
2558 struct token *tk, char *start, char *end, const char *id)
2559{
2560 char *str;
2561 int str_len;
2562 /* Create a private copy of the string */
2563 str_len = end - start + 1;
2564 str = xmalloc(str_len + 1, id);
2565 memcpy(str, start, str_len);
2566 str[str_len] = '\0';
2567
2568 /* Store the copy in the token */
2569 tk->val.str = str;
2570 tk->str_len = str_len;
2571}
2572static void next_token(struct compile_state *state, int index)
2573{
2574 struct file_state *file;
2575 struct token *tk;
2576 char *token;
2577 int c, c1, c2, c3;
2578 char *tokp, *end;
2579 int tok;
2580next_token:
2581 file = state->file;
2582 tk = &state->token[index];
2583 tk->str_len = 0;
2584 tk->ident = 0;
2585 token = tokp = file->pos;
2586 end = file->buf + file->size;
2587 tok = TOK_UNKNOWN;
2588 c = -1;
2589 if (tokp < end) {
2590 c = *tokp;
2591 }
2592 c1 = -1;
2593 if ((tokp + 1) < end) {
2594 c1 = tokp[1];
2595 }
2596 c2 = -1;
2597 if ((tokp + 2) < end) {
2598 c2 = tokp[2];
2599 }
2600 c3 = -1;
2601 if ((tokp + 3) < end) {
2602 c3 = tokp[3];
2603 }
2604 if (tokp >= end) {
2605 tok = TOK_EOF;
2606 tokp = end;
2607 }
2608 /* Whitespace */
2609 else if (spacep(c)) {
2610 tok = TOK_SPACE;
2611 while ((tokp < end) && spacep(c)) {
2612 if (c == '\n') {
2613 file->line++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002614 file->report_line++;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002615 file->line_start = tokp + 1;
2616 }
2617 c = *(++tokp);
2618 }
2619 if (!spacep(c)) {
2620 tokp--;
2621 }
2622 }
2623 /* EOL Comments */
2624 else if ((c == '/') && (c1 == '/')) {
2625 tok = TOK_SPACE;
2626 for(tokp += 2; tokp < end; tokp++) {
2627 c = *tokp;
2628 if (c == '\n') {
2629 file->line++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002630 file->report_line++;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002631 file->line_start = tokp +1;
2632 break;
2633 }
2634 }
2635 }
2636 /* Comments */
2637 else if ((c == '/') && (c1 == '*')) {
2638 int line;
2639 char *line_start;
2640 line = file->line;
2641 line_start = file->line_start;
2642 for(tokp += 2; (end - tokp) >= 2; tokp++) {
2643 c = *tokp;
2644 if (c == '\n') {
2645 line++;
2646 line_start = tokp +1;
2647 }
2648 else if ((c == '*') && (tokp[1] == '/')) {
2649 tok = TOK_SPACE;
2650 tokp += 1;
2651 break;
2652 }
2653 }
2654 if (tok == TOK_UNKNOWN) {
2655 error(state, 0, "unterminated comment");
2656 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002657 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002658 file->line = line;
2659 file->line_start = line_start;
2660 }
2661 /* string constants */
2662 else if ((c == '"') ||
2663 ((c == 'L') && (c1 == '"'))) {
2664 int line;
2665 char *line_start;
2666 int wchar;
2667 line = file->line;
2668 line_start = file->line_start;
2669 wchar = 0;
2670 if (c == 'L') {
2671 wchar = 1;
2672 tokp++;
2673 }
2674 for(tokp += 1; tokp < end; tokp++) {
2675 c = *tokp;
2676 if (c == '\n') {
2677 line++;
2678 line_start = tokp + 1;
2679 }
2680 else if ((c == '\\') && (tokp +1 < end)) {
2681 tokp++;
2682 }
2683 else if (c == '"') {
2684 tok = TOK_LIT_STRING;
2685 break;
2686 }
2687 }
2688 if (tok == TOK_UNKNOWN) {
2689 error(state, 0, "unterminated string constant");
2690 }
2691 if (line != file->line) {
2692 warning(state, 0, "multiline string constant");
2693 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002694 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002695 file->line = line;
2696 file->line_start = line_start;
2697
2698 /* Save the string value */
2699 save_string(state, tk, token, tokp, "literal string");
2700 }
2701 /* character constants */
2702 else if ((c == '\'') ||
2703 ((c == 'L') && (c1 == '\''))) {
2704 int line;
2705 char *line_start;
2706 int wchar;
2707 line = file->line;
2708 line_start = file->line_start;
2709 wchar = 0;
2710 if (c == 'L') {
2711 wchar = 1;
2712 tokp++;
2713 }
2714 for(tokp += 1; tokp < end; tokp++) {
2715 c = *tokp;
2716 if (c == '\n') {
2717 line++;
2718 line_start = tokp + 1;
2719 }
2720 else if ((c == '\\') && (tokp +1 < end)) {
2721 tokp++;
2722 }
2723 else if (c == '\'') {
2724 tok = TOK_LIT_CHAR;
2725 break;
2726 }
2727 }
2728 if (tok == TOK_UNKNOWN) {
2729 error(state, 0, "unterminated character constant");
2730 }
2731 if (line != file->line) {
2732 warning(state, 0, "multiline character constant");
2733 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002734 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002735 file->line = line;
2736 file->line_start = line_start;
2737
2738 /* Save the character value */
2739 save_string(state, tk, token, tokp, "literal character");
2740 }
2741 /* integer and floating constants
2742 * Integer Constants
2743 * {digits}
2744 * 0[Xx]{hexdigits}
2745 * 0{octdigit}+
2746 *
2747 * Floating constants
2748 * {digits}.{digits}[Ee][+-]?{digits}
2749 * {digits}.{digits}
2750 * {digits}[Ee][+-]?{digits}
2751 * .{digits}[Ee][+-]?{digits}
2752 * .{digits}
2753 */
2754
2755 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
2756 char *next, *new;
2757 int is_float;
2758 is_float = 0;
2759 if (c != '.') {
2760 next = after_digits(tokp, end);
2761 }
2762 else {
2763 next = tokp;
2764 }
2765 if (next[0] == '.') {
2766 new = after_digits(next, end);
2767 is_float = (new != next);
2768 next = new;
2769 }
2770 if ((next[0] == 'e') || (next[0] == 'E')) {
2771 if (((next + 1) < end) &&
2772 ((next[1] == '+') || (next[1] == '-'))) {
2773 next++;
2774 }
2775 new = after_digits(next, end);
2776 is_float = (new != next);
2777 next = new;
2778 }
2779 if (is_float) {
2780 tok = TOK_LIT_FLOAT;
2781 if ((next < end) && (
2782 (next[0] == 'f') ||
2783 (next[0] == 'F') ||
2784 (next[0] == 'l') ||
2785 (next[0] == 'L'))
2786 ) {
2787 next++;
2788 }
2789 }
2790 if (!is_float && digitp(c)) {
2791 tok = TOK_LIT_INT;
2792 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
2793 next = after_hexdigits(tokp + 2, end);
2794 }
2795 else if (c == '0') {
2796 next = after_octdigits(tokp, end);
2797 }
2798 else {
2799 next = after_digits(tokp, end);
2800 }
2801 /* crazy integer suffixes */
2802 if ((next < end) &&
2803 ((next[0] == 'u') || (next[0] == 'U'))) {
2804 next++;
2805 if ((next < end) &&
2806 ((next[0] == 'l') || (next[0] == 'L'))) {
2807 next++;
2808 }
2809 }
2810 else if ((next < end) &&
2811 ((next[0] == 'l') || (next[0] == 'L'))) {
2812 next++;
2813 if ((next < end) &&
2814 ((next[0] == 'u') || (next[0] == 'U'))) {
2815 next++;
2816 }
2817 }
2818 }
2819 tokp = next - 1;
2820
2821 /* Save the integer/floating point value */
2822 save_string(state, tk, token, tokp, "literal number");
2823 }
2824 /* identifiers */
2825 else if (letterp(c)) {
2826 tok = TOK_IDENT;
2827 for(tokp += 1; tokp < end; tokp++) {
2828 c = *tokp;
2829 if (!letterp(c) && !digitp(c)) {
2830 break;
2831 }
2832 }
2833 tokp -= 1;
2834 tk->ident = lookup(state, token, tokp +1 - token);
2835 }
2836 /* C99 alternate macro characters */
2837 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
2838 tokp += 3;
2839 tok = TOK_CONCATENATE;
2840 }
2841 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { tokp += 2; tok = TOK_DOTS; }
2842 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { tokp += 2; tok = TOK_SLEQ; }
2843 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { tokp += 2; tok = TOK_SREQ; }
2844 else if ((c == '*') && (c1 == '=')) { tokp += 1; tok = TOK_TIMESEQ; }
2845 else if ((c == '/') && (c1 == '=')) { tokp += 1; tok = TOK_DIVEQ; }
2846 else if ((c == '%') && (c1 == '=')) { tokp += 1; tok = TOK_MODEQ; }
2847 else if ((c == '+') && (c1 == '=')) { tokp += 1; tok = TOK_PLUSEQ; }
2848 else if ((c == '-') && (c1 == '=')) { tokp += 1; tok = TOK_MINUSEQ; }
2849 else if ((c == '&') && (c1 == '=')) { tokp += 1; tok = TOK_ANDEQ; }
2850 else if ((c == '^') && (c1 == '=')) { tokp += 1; tok = TOK_XOREQ; }
2851 else if ((c == '|') && (c1 == '=')) { tokp += 1; tok = TOK_OREQ; }
2852 else if ((c == '=') && (c1 == '=')) { tokp += 1; tok = TOK_EQEQ; }
2853 else if ((c == '!') && (c1 == '=')) { tokp += 1; tok = TOK_NOTEQ; }
2854 else if ((c == '|') && (c1 == '|')) { tokp += 1; tok = TOK_LOGOR; }
2855 else if ((c == '&') && (c1 == '&')) { tokp += 1; tok = TOK_LOGAND; }
2856 else if ((c == '<') && (c1 == '=')) { tokp += 1; tok = TOK_LESSEQ; }
2857 else if ((c == '>') && (c1 == '=')) { tokp += 1; tok = TOK_MOREEQ; }
2858 else if ((c == '<') && (c1 == '<')) { tokp += 1; tok = TOK_SL; }
2859 else if ((c == '>') && (c1 == '>')) { tokp += 1; tok = TOK_SR; }
2860 else if ((c == '+') && (c1 == '+')) { tokp += 1; tok = TOK_PLUSPLUS; }
2861 else if ((c == '-') && (c1 == '-')) { tokp += 1; tok = TOK_MINUSMINUS; }
2862 else if ((c == '-') && (c1 == '>')) { tokp += 1; tok = TOK_ARROW; }
2863 else if ((c == '<') && (c1 == ':')) { tokp += 1; tok = TOK_LBRACKET; }
2864 else if ((c == ':') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACKET; }
2865 else if ((c == '<') && (c1 == '%')) { tokp += 1; tok = TOK_LBRACE; }
2866 else if ((c == '%') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACE; }
2867 else if ((c == '%') && (c1 == ':')) { tokp += 1; tok = TOK_MACRO; }
2868 else if ((c == '#') && (c1 == '#')) { tokp += 1; tok = TOK_CONCATENATE; }
2869 else if (c == ';') { tok = TOK_SEMI; }
2870 else if (c == '{') { tok = TOK_LBRACE; }
2871 else if (c == '}') { tok = TOK_RBRACE; }
2872 else if (c == ',') { tok = TOK_COMMA; }
2873 else if (c == '=') { tok = TOK_EQ; }
2874 else if (c == ':') { tok = TOK_COLON; }
2875 else if (c == '[') { tok = TOK_LBRACKET; }
2876 else if (c == ']') { tok = TOK_RBRACKET; }
2877 else if (c == '(') { tok = TOK_LPAREN; }
2878 else if (c == ')') { tok = TOK_RPAREN; }
2879 else if (c == '*') { tok = TOK_STAR; }
2880 else if (c == '>') { tok = TOK_MORE; }
2881 else if (c == '<') { tok = TOK_LESS; }
2882 else if (c == '?') { tok = TOK_QUEST; }
2883 else if (c == '|') { tok = TOK_OR; }
2884 else if (c == '&') { tok = TOK_AND; }
2885 else if (c == '^') { tok = TOK_XOR; }
2886 else if (c == '+') { tok = TOK_PLUS; }
2887 else if (c == '-') { tok = TOK_MINUS; }
2888 else if (c == '/') { tok = TOK_DIV; }
2889 else if (c == '%') { tok = TOK_MOD; }
2890 else if (c == '!') { tok = TOK_BANG; }
2891 else if (c == '.') { tok = TOK_DOT; }
2892 else if (c == '~') { tok = TOK_TILDE; }
2893 else if (c == '#') { tok = TOK_MACRO; }
2894 if (tok == TOK_MACRO) {
2895 /* Only match preprocessor directives at the start of a line */
2896 char *ptr;
2897 for(ptr = file->line_start; spacep(*ptr); ptr++)
2898 ;
2899 if (ptr != tokp) {
2900 tok = TOK_UNKNOWN;
2901 }
2902 }
2903 if (tok == TOK_UNKNOWN) {
2904 error(state, 0, "unknown token");
2905 }
2906
2907 file->pos = tokp + 1;
2908 tk->tok = tok;
2909 if (tok == TOK_IDENT) {
2910 ident_to_keyword(state, tk);
2911 }
2912 /* Don't return space tokens. */
2913 if (tok == TOK_SPACE) {
2914 goto next_token;
2915 }
2916}
2917
2918static void compile_macro(struct compile_state *state, struct token *tk)
2919{
2920 struct file_state *file;
2921 struct hash_entry *ident;
2922 ident = tk->ident;
2923 file = xmalloc(sizeof(*file), "file_state");
2924 file->basename = xstrdup(tk->ident->name);
2925 file->dirname = xstrdup("");
2926 file->size = ident->sym_define->buf_len;
2927 file->buf = xmalloc(file->size +2, file->basename);
2928 memcpy(file->buf, ident->sym_define->buf, file->size);
2929 file->buf[file->size] = '\n';
2930 file->buf[file->size + 1] = '\0';
2931 file->pos = file->buf;
2932 file->line_start = file->pos;
2933 file->line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002934 file->report_line = 1;
2935 file->report_name = file->basename;
2936 file->report_dir = file->dirname;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002937 file->prev = state->file;
2938 state->file = file;
2939}
2940
2941
2942static int mpeek(struct compile_state *state, int index)
2943{
2944 struct token *tk;
2945 int rescan;
2946 tk = &state->token[index + 1];
2947 if (tk->tok == -1) {
2948 next_token(state, index + 1);
2949 }
2950 do {
2951 rescan = 0;
2952 if ((tk->tok == TOK_EOF) &&
2953 (state->file != state->macro_file) &&
2954 (state->file->prev)) {
2955 struct file_state *file = state->file;
2956 state->file = file->prev;
2957 /* file->basename is used keep it */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002958 if (file->report_dir != file->dirname) {
2959 xfree(file->report_dir);
2960 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002961 xfree(file->dirname);
2962 xfree(file->buf);
2963 xfree(file);
2964 next_token(state, index + 1);
2965 rescan = 1;
2966 }
2967 else if (tk->ident && tk->ident->sym_define) {
2968 compile_macro(state, tk);
2969 next_token(state, index + 1);
2970 rescan = 1;
2971 }
2972 } while(rescan);
2973 /* Don't show the token on the next line */
2974 if (state->macro_line < state->macro_file->line) {
2975 return TOK_EOF;
2976 }
2977 return state->token[index +1].tok;
2978}
2979
2980static void meat(struct compile_state *state, int index, int tok)
2981{
2982 int next_tok;
2983 int i;
2984 next_tok = mpeek(state, index);
2985 if (next_tok != tok) {
2986 const char *name1, *name2;
2987 name1 = tokens[next_tok];
2988 name2 = "";
2989 if (next_tok == TOK_IDENT) {
2990 name2 = state->token[index + 1].ident->name;
2991 }
2992 error(state, 0, "found %s %s expected %s",
2993 name1, name2, tokens[tok]);
2994 }
2995 /* Free the old token value */
2996 if (state->token[index].str_len) {
2997 memset((void *)(state->token[index].val.str), -1,
2998 state->token[index].str_len);
2999 xfree(state->token[index].val.str);
3000 }
3001 for(i = index; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
3002 state->token[i] = state->token[i + 1];
3003 }
3004 memset(&state->token[i], 0, sizeof(state->token[i]));
3005 state->token[i].tok = -1;
3006}
3007
3008static long_t mcexpr(struct compile_state *state, int index);
3009
3010static long_t mprimary_expr(struct compile_state *state, int index)
3011{
3012 long_t val;
3013 int tok;
3014 tok = mpeek(state, index);
3015 while(state->token[index + 1].ident &&
3016 state->token[index + 1].ident->sym_define) {
3017 meat(state, index, tok);
3018 compile_macro(state, &state->token[index]);
3019 tok = mpeek(state, index);
3020 }
3021 switch(tok) {
3022 case TOK_LPAREN:
3023 meat(state, index, TOK_LPAREN);
3024 val = mcexpr(state, index);
3025 meat(state, index, TOK_RPAREN);
3026 break;
3027 case TOK_LIT_INT:
3028 {
Eric Biederman83b991a2003-10-11 06:20:25 +00003029 long lval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003030 char *end;
3031 meat(state, index, TOK_LIT_INT);
3032 errno = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +00003033 lval = strtol(state->token[index].val.str, &end, 0);
3034 if ((lval > LONG_T_MAX) || (lval < LONG_T_MIN) ||
3035 (((lval == LONG_MIN) || (lval == LONG_MAX)) &&
3036 (errno == ERANGE))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003037 error(state, 0, "Integer constant to large");
3038 }
Eric Biederman83b991a2003-10-11 06:20:25 +00003039 val = lval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003040 break;
3041 }
3042 default:
3043 meat(state, index, TOK_LIT_INT);
3044 val = 0;
3045 }
3046 return val;
3047}
3048static long_t munary_expr(struct compile_state *state, int index)
3049{
3050 long_t val;
3051 switch(mpeek(state, index)) {
3052 case TOK_PLUS:
3053 meat(state, index, TOK_PLUS);
3054 val = munary_expr(state, index);
3055 val = + val;
3056 break;
3057 case TOK_MINUS:
3058 meat(state, index, TOK_MINUS);
3059 val = munary_expr(state, index);
3060 val = - val;
3061 break;
3062 case TOK_TILDE:
3063 meat(state, index, TOK_BANG);
3064 val = munary_expr(state, index);
3065 val = ~ val;
3066 break;
3067 case TOK_BANG:
3068 meat(state, index, TOK_BANG);
3069 val = munary_expr(state, index);
3070 val = ! val;
3071 break;
3072 default:
3073 val = mprimary_expr(state, index);
3074 break;
3075 }
3076 return val;
3077
3078}
3079static long_t mmul_expr(struct compile_state *state, int index)
3080{
3081 long_t val;
3082 int done;
3083 val = munary_expr(state, index);
3084 do {
3085 long_t right;
3086 done = 0;
3087 switch(mpeek(state, index)) {
3088 case TOK_STAR:
3089 meat(state, index, TOK_STAR);
3090 right = munary_expr(state, index);
3091 val = val * right;
3092 break;
3093 case TOK_DIV:
3094 meat(state, index, TOK_DIV);
3095 right = munary_expr(state, index);
3096 val = val / right;
3097 break;
3098 case TOK_MOD:
3099 meat(state, index, TOK_MOD);
3100 right = munary_expr(state, index);
3101 val = val % right;
3102 break;
3103 default:
3104 done = 1;
3105 break;
3106 }
3107 } while(!done);
3108
3109 return val;
3110}
3111
3112static long_t madd_expr(struct compile_state *state, int index)
3113{
3114 long_t val;
3115 int done;
3116 val = mmul_expr(state, index);
3117 do {
3118 long_t right;
3119 done = 0;
3120 switch(mpeek(state, index)) {
3121 case TOK_PLUS:
3122 meat(state, index, TOK_PLUS);
3123 right = mmul_expr(state, index);
3124 val = val + right;
3125 break;
3126 case TOK_MINUS:
3127 meat(state, index, TOK_MINUS);
3128 right = mmul_expr(state, index);
3129 val = val - right;
3130 break;
3131 default:
3132 done = 1;
3133 break;
3134 }
3135 } while(!done);
3136
3137 return val;
3138}
3139
3140static long_t mshift_expr(struct compile_state *state, int index)
3141{
3142 long_t val;
3143 int done;
3144 val = madd_expr(state, index);
3145 do {
3146 long_t right;
3147 done = 0;
3148 switch(mpeek(state, index)) {
3149 case TOK_SL:
3150 meat(state, index, TOK_SL);
3151 right = madd_expr(state, index);
3152 val = val << right;
3153 break;
3154 case TOK_SR:
3155 meat(state, index, TOK_SR);
3156 right = madd_expr(state, index);
3157 val = val >> right;
3158 break;
3159 default:
3160 done = 1;
3161 break;
3162 }
3163 } while(!done);
3164
3165 return val;
3166}
3167
3168static long_t mrel_expr(struct compile_state *state, int index)
3169{
3170 long_t val;
3171 int done;
3172 val = mshift_expr(state, index);
3173 do {
3174 long_t right;
3175 done = 0;
3176 switch(mpeek(state, index)) {
3177 case TOK_LESS:
3178 meat(state, index, TOK_LESS);
3179 right = mshift_expr(state, index);
3180 val = val < right;
3181 break;
3182 case TOK_MORE:
3183 meat(state, index, TOK_MORE);
3184 right = mshift_expr(state, index);
3185 val = val > right;
3186 break;
3187 case TOK_LESSEQ:
3188 meat(state, index, TOK_LESSEQ);
3189 right = mshift_expr(state, index);
3190 val = val <= right;
3191 break;
3192 case TOK_MOREEQ:
3193 meat(state, index, TOK_MOREEQ);
3194 right = mshift_expr(state, index);
3195 val = val >= right;
3196 break;
3197 default:
3198 done = 1;
3199 break;
3200 }
3201 } while(!done);
3202 return val;
3203}
3204
3205static long_t meq_expr(struct compile_state *state, int index)
3206{
3207 long_t val;
3208 int done;
3209 val = mrel_expr(state, index);
3210 do {
3211 long_t right;
3212 done = 0;
3213 switch(mpeek(state, index)) {
3214 case TOK_EQEQ:
3215 meat(state, index, TOK_EQEQ);
3216 right = mrel_expr(state, index);
3217 val = val == right;
3218 break;
3219 case TOK_NOTEQ:
3220 meat(state, index, TOK_NOTEQ);
3221 right = mrel_expr(state, index);
3222 val = val != right;
3223 break;
3224 default:
3225 done = 1;
3226 break;
3227 }
3228 } while(!done);
3229 return val;
3230}
3231
3232static long_t mand_expr(struct compile_state *state, int index)
3233{
3234 long_t val;
3235 val = meq_expr(state, index);
3236 if (mpeek(state, index) == TOK_AND) {
3237 long_t right;
3238 meat(state, index, TOK_AND);
3239 right = meq_expr(state, index);
3240 val = val & right;
3241 }
3242 return val;
3243}
3244
3245static long_t mxor_expr(struct compile_state *state, int index)
3246{
3247 long_t val;
3248 val = mand_expr(state, index);
3249 if (mpeek(state, index) == TOK_XOR) {
3250 long_t right;
3251 meat(state, index, TOK_XOR);
3252 right = mand_expr(state, index);
3253 val = val ^ right;
3254 }
3255 return val;
3256}
3257
3258static long_t mor_expr(struct compile_state *state, int index)
3259{
3260 long_t val;
3261 val = mxor_expr(state, index);
3262 if (mpeek(state, index) == TOK_OR) {
3263 long_t right;
3264 meat(state, index, TOK_OR);
3265 right = mxor_expr(state, index);
3266 val = val | right;
3267 }
3268 return val;
3269}
3270
3271static long_t mland_expr(struct compile_state *state, int index)
3272{
3273 long_t val;
3274 val = mor_expr(state, index);
3275 if (mpeek(state, index) == TOK_LOGAND) {
3276 long_t right;
3277 meat(state, index, TOK_LOGAND);
3278 right = mor_expr(state, index);
3279 val = val && right;
3280 }
3281 return val;
3282}
3283static long_t mlor_expr(struct compile_state *state, int index)
3284{
3285 long_t val;
3286 val = mland_expr(state, index);
3287 if (mpeek(state, index) == TOK_LOGOR) {
3288 long_t right;
3289 meat(state, index, TOK_LOGOR);
3290 right = mland_expr(state, index);
3291 val = val || right;
3292 }
3293 return val;
3294}
3295
3296static long_t mcexpr(struct compile_state *state, int index)
3297{
3298 return mlor_expr(state, index);
3299}
3300static void preprocess(struct compile_state *state, int index)
3301{
3302 /* Doing much more with the preprocessor would require
3303 * a parser and a major restructuring.
3304 * Postpone that for later.
3305 */
3306 struct file_state *file;
3307 struct token *tk;
3308 int line;
3309 int tok;
3310
3311 file = state->file;
3312 tk = &state->token[index];
3313 state->macro_line = line = file->line;
3314 state->macro_file = file;
3315
3316 next_token(state, index);
3317 ident_to_macro(state, tk);
3318 if (tk->tok == TOK_IDENT) {
3319 error(state, 0, "undefined preprocessing directive `%s'",
3320 tk->ident->name);
3321 }
3322 switch(tk->tok) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00003323 case TOK_LIT_INT:
3324 {
3325 int override_line;
3326 override_line = strtoul(tk->val.str, 0, 10);
3327 next_token(state, index);
3328 /* I have a cpp line marker parse it */
3329 if (tk->tok == TOK_LIT_STRING) {
3330 const char *token, *base;
3331 char *name, *dir;
3332 int name_len, dir_len;
3333 name = xmalloc(tk->str_len, "report_name");
3334 token = tk->val.str + 1;
3335 base = strrchr(token, '/');
3336 name_len = tk->str_len -2;
3337 if (base != 0) {
3338 dir_len = base - token;
3339 base++;
3340 name_len -= base - token;
3341 } else {
3342 dir_len = 0;
3343 base = token;
3344 }
3345 memcpy(name, base, name_len);
3346 name[name_len] = '\0';
3347 dir = xmalloc(dir_len + 1, "report_dir");
3348 memcpy(dir, token, dir_len);
3349 dir[dir_len] = '\0';
3350 file->report_line = override_line - 1;
3351 file->report_name = name;
3352 file->report_dir = dir;
3353 }
3354 }
3355 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003356 case TOK_LINE:
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00003357 meat(state, index, TOK_LINE);
3358 meat(state, index, TOK_LIT_INT);
3359 file->report_line = strtoul(tk->val.str, 0, 10) -1;
3360 if (mpeek(state, index) == TOK_LIT_STRING) {
3361 const char *token, *base;
3362 char *name, *dir;
3363 int name_len, dir_len;
3364 meat(state, index, TOK_LIT_STRING);
3365 name = xmalloc(tk->str_len, "report_name");
3366 token = tk->val.str + 1;
3367 name_len = tk->str_len - 2;
3368 if (base != 0) {
3369 dir_len = base - token;
3370 base++;
3371 name_len -= base - token;
3372 } else {
3373 dir_len = 0;
3374 base = token;
3375 }
3376 memcpy(name, base, name_len);
3377 name[name_len] = '\0';
3378 dir = xmalloc(dir_len + 1, "report_dir");
3379 memcpy(dir, token, dir_len);
3380 dir[dir_len] = '\0';
3381 file->report_name = name;
3382 file->report_dir = dir;
3383 }
3384 break;
3385 case TOK_UNDEF:
Eric Biedermanb138ac82003-04-22 18:44:01 +00003386 case TOK_PRAGMA:
3387 if (state->if_value < 0) {
3388 break;
3389 }
3390 warning(state, 0, "Ignoring preprocessor directive: %s",
3391 tk->ident->name);
3392 break;
3393 case TOK_ELIF:
3394 error(state, 0, "#elif not supported");
3395#warning "FIXME multiple #elif and #else in an #if do not work properly"
3396 if (state->if_depth == 0) {
3397 error(state, 0, "#elif without #if");
3398 }
3399 /* If the #if was taken the #elif just disables the following code */
3400 if (state->if_value >= 0) {
3401 state->if_value = - state->if_value;
3402 }
3403 /* If the previous #if was not taken see if the #elif enables the
3404 * trailing code.
3405 */
3406 else if ((state->if_value < 0) &&
3407 (state->if_depth == - state->if_value))
3408 {
3409 if (mcexpr(state, index) != 0) {
3410 state->if_value = state->if_depth;
3411 }
3412 else {
3413 state->if_value = - state->if_depth;
3414 }
3415 }
3416 break;
3417 case TOK_IF:
3418 state->if_depth++;
3419 if (state->if_value < 0) {
3420 break;
3421 }
3422 if (mcexpr(state, index) != 0) {
3423 state->if_value = state->if_depth;
3424 }
3425 else {
3426 state->if_value = - state->if_depth;
3427 }
3428 break;
3429 case TOK_IFNDEF:
3430 state->if_depth++;
3431 if (state->if_value < 0) {
3432 break;
3433 }
3434 next_token(state, index);
3435 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3436 error(state, 0, "Invalid macro name");
3437 }
3438 if (tk->ident->sym_define == 0) {
3439 state->if_value = state->if_depth;
3440 }
3441 else {
3442 state->if_value = - state->if_depth;
3443 }
3444 break;
3445 case TOK_IFDEF:
3446 state->if_depth++;
3447 if (state->if_value < 0) {
3448 break;
3449 }
3450 next_token(state, index);
3451 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3452 error(state, 0, "Invalid macro name");
3453 }
3454 if (tk->ident->sym_define != 0) {
3455 state->if_value = state->if_depth;
3456 }
3457 else {
3458 state->if_value = - state->if_depth;
3459 }
3460 break;
3461 case TOK_ELSE:
3462 if (state->if_depth == 0) {
3463 error(state, 0, "#else without #if");
3464 }
3465 if ((state->if_value >= 0) ||
3466 ((state->if_value < 0) &&
3467 (state->if_depth == -state->if_value)))
3468 {
3469 state->if_value = - state->if_value;
3470 }
3471 break;
3472 case TOK_ENDIF:
3473 if (state->if_depth == 0) {
3474 error(state, 0, "#endif without #if");
3475 }
3476 if ((state->if_value >= 0) ||
3477 ((state->if_value < 0) &&
3478 (state->if_depth == -state->if_value)))
3479 {
3480 state->if_value = state->if_depth - 1;
3481 }
3482 state->if_depth--;
3483 break;
3484 case TOK_DEFINE:
3485 {
3486 struct hash_entry *ident;
3487 struct macro *macro;
3488 char *ptr;
3489
3490 if (state->if_value < 0) /* quit early when #if'd out */
3491 break;
3492
3493 meat(state, index, TOK_IDENT);
3494 ident = tk->ident;
3495
3496
3497 if (*file->pos == '(') {
3498#warning "FIXME macros with arguments not supported"
3499 error(state, 0, "Macros with arguments not supported");
3500 }
3501
3502 /* Find the end of the line to get an estimate of
3503 * the macro's length.
3504 */
3505 for(ptr = file->pos; *ptr != '\n'; ptr++)
3506 ;
3507
3508 if (ident->sym_define != 0) {
3509 error(state, 0, "macro %s already defined\n", ident->name);
3510 }
3511 macro = xmalloc(sizeof(*macro), "macro");
3512 macro->ident = ident;
3513 macro->buf_len = ptr - file->pos +1;
3514 macro->buf = xmalloc(macro->buf_len +2, "macro buf");
3515
3516 memcpy(macro->buf, file->pos, macro->buf_len);
3517 macro->buf[macro->buf_len] = '\n';
3518 macro->buf[macro->buf_len +1] = '\0';
3519
3520 ident->sym_define = macro;
3521 break;
3522 }
3523 case TOK_ERROR:
3524 {
3525 char *end;
3526 int len;
3527 /* Find the end of the line */
3528 for(end = file->pos; *end != '\n'; end++)
3529 ;
3530 len = (end - file->pos);
3531 if (state->if_value >= 0) {
3532 error(state, 0, "%*.*s", len, len, file->pos);
3533 }
3534 file->pos = end;
3535 break;
3536 }
3537 case TOK_WARNING:
3538 {
3539 char *end;
3540 int len;
3541 /* Find the end of the line */
3542 for(end = file->pos; *end != '\n'; end++)
3543 ;
3544 len = (end - file->pos);
3545 if (state->if_value >= 0) {
3546 warning(state, 0, "%*.*s", len, len, file->pos);
3547 }
3548 file->pos = end;
3549 break;
3550 }
3551 case TOK_INCLUDE:
3552 {
3553 char *name;
3554 char *ptr;
3555 int local;
3556 local = 0;
3557 name = 0;
3558 next_token(state, index);
3559 if (tk->tok == TOK_LIT_STRING) {
3560 const char *token;
3561 int name_len;
3562 name = xmalloc(tk->str_len, "include");
3563 token = tk->val.str +1;
3564 name_len = tk->str_len -2;
3565 if (*token == '"') {
3566 token++;
3567 name_len--;
3568 }
3569 memcpy(name, token, name_len);
3570 name[name_len] = '\0';
3571 local = 1;
3572 }
3573 else if (tk->tok == TOK_LESS) {
3574 char *start, *end;
3575 start = file->pos;
3576 for(end = start; *end != '\n'; end++) {
3577 if (*end == '>') {
3578 break;
3579 }
3580 }
3581 if (*end == '\n') {
3582 error(state, 0, "Unterminated included directive");
3583 }
3584 name = xmalloc(end - start + 1, "include");
3585 memcpy(name, start, end - start);
3586 name[end - start] = '\0';
3587 file->pos = end +1;
3588 local = 0;
3589 }
3590 else {
3591 error(state, 0, "Invalid include directive");
3592 }
3593 /* Error if there are any characters after the include */
3594 for(ptr = file->pos; *ptr != '\n'; ptr++) {
Eric Biederman8d9c1232003-06-17 08:42:17 +00003595 switch(*ptr) {
3596 case ' ':
3597 case '\t':
3598 case '\v':
3599 break;
3600 default:
Eric Biedermanb138ac82003-04-22 18:44:01 +00003601 error(state, 0, "garbage after include directive");
3602 }
3603 }
3604 if (state->if_value >= 0) {
3605 compile_file(state, name, local);
3606 }
3607 xfree(name);
3608 next_token(state, index);
3609 return;
3610 }
3611 default:
3612 /* Ignore # without a following ident */
3613 if (tk->tok == TOK_IDENT) {
3614 error(state, 0, "Invalid preprocessor directive: %s",
3615 tk->ident->name);
3616 }
3617 break;
3618 }
3619 /* Consume the rest of the macro line */
3620 do {
3621 tok = mpeek(state, index);
3622 meat(state, index, tok);
3623 } while(tok != TOK_EOF);
3624 return;
3625}
3626
3627static void token(struct compile_state *state, int index)
3628{
3629 struct file_state *file;
3630 struct token *tk;
3631 int rescan;
3632
3633 tk = &state->token[index];
3634 next_token(state, index);
3635 do {
3636 rescan = 0;
3637 file = state->file;
3638 if (tk->tok == TOK_EOF && file->prev) {
3639 state->file = file->prev;
3640 /* file->basename is used keep it */
3641 xfree(file->dirname);
3642 xfree(file->buf);
3643 xfree(file);
3644 next_token(state, index);
3645 rescan = 1;
3646 }
3647 else if (tk->tok == TOK_MACRO) {
3648 preprocess(state, index);
3649 rescan = 1;
3650 }
3651 else if (tk->ident && tk->ident->sym_define) {
3652 compile_macro(state, tk);
3653 next_token(state, index);
3654 rescan = 1;
3655 }
3656 else if (state->if_value < 0) {
3657 next_token(state, index);
3658 rescan = 1;
3659 }
3660 } while(rescan);
3661}
3662
3663static int peek(struct compile_state *state)
3664{
3665 if (state->token[1].tok == -1) {
3666 token(state, 1);
3667 }
3668 return state->token[1].tok;
3669}
3670
3671static int peek2(struct compile_state *state)
3672{
3673 if (state->token[1].tok == -1) {
3674 token(state, 1);
3675 }
3676 if (state->token[2].tok == -1) {
3677 token(state, 2);
3678 }
3679 return state->token[2].tok;
3680}
3681
Eric Biederman0babc1c2003-05-09 02:39:00 +00003682static void eat(struct compile_state *state, int tok)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003683{
3684 int next_tok;
3685 int i;
3686 next_tok = peek(state);
3687 if (next_tok != tok) {
3688 const char *name1, *name2;
3689 name1 = tokens[next_tok];
3690 name2 = "";
3691 if (next_tok == TOK_IDENT) {
3692 name2 = state->token[1].ident->name;
3693 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00003694 error(state, 0, "\tfound %s %s expected %s",
3695 name1, name2 ,tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003696 }
3697 /* Free the old token value */
3698 if (state->token[0].str_len) {
3699 xfree((void *)(state->token[0].val.str));
3700 }
3701 for(i = 0; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
3702 state->token[i] = state->token[i + 1];
3703 }
3704 memset(&state->token[i], 0, sizeof(state->token[i]));
3705 state->token[i].tok = -1;
3706}
Eric Biedermanb138ac82003-04-22 18:44:01 +00003707
3708#warning "FIXME do not hardcode the include paths"
3709static char *include_paths[] = {
3710 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/include",
3711 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/arch/i386/include",
3712 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src",
3713 0
3714};
3715
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003716static void compile_file(struct compile_state *state, const char *filename, int local)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003717{
3718 char cwd[4096];
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003719 const char *subdir, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003720 int subdir_len;
3721 struct file_state *file;
3722 char *basename;
3723 file = xmalloc(sizeof(*file), "file_state");
3724
3725 base = strrchr(filename, '/');
3726 subdir = filename;
3727 if (base != 0) {
3728 subdir_len = base - filename;
3729 base++;
3730 }
3731 else {
3732 base = filename;
3733 subdir_len = 0;
3734 }
3735 basename = xmalloc(strlen(base) +1, "basename");
3736 strcpy(basename, base);
3737 file->basename = basename;
3738
3739 if (getcwd(cwd, sizeof(cwd)) == 0) {
3740 die("cwd buffer to small");
3741 }
3742
3743 if (subdir[0] == '/') {
3744 file->dirname = xmalloc(subdir_len + 1, "dirname");
3745 memcpy(file->dirname, subdir, subdir_len);
3746 file->dirname[subdir_len] = '\0';
3747 }
3748 else {
3749 char *dir;
3750 int dirlen;
3751 char **path;
3752 /* Find the appropriate directory... */
3753 dir = 0;
3754 if (!state->file && exists(cwd, filename)) {
3755 dir = cwd;
3756 }
3757 if (local && state->file && exists(state->file->dirname, filename)) {
3758 dir = state->file->dirname;
3759 }
3760 for(path = include_paths; !dir && *path; path++) {
3761 if (exists(*path, filename)) {
3762 dir = *path;
3763 }
3764 }
3765 if (!dir) {
3766 error(state, 0, "Cannot find `%s'\n", filename);
3767 }
3768 dirlen = strlen(dir);
3769 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
3770 memcpy(file->dirname, dir, dirlen);
3771 file->dirname[dirlen] = '/';
3772 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
3773 file->dirname[dirlen + 1 + subdir_len] = '\0';
3774 }
3775 file->buf = slurp_file(file->dirname, file->basename, &file->size);
3776 xchdir(cwd);
3777
3778 file->pos = file->buf;
3779 file->line_start = file->pos;
3780 file->line = 1;
3781
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00003782 file->report_line = 1;
3783 file->report_name = file->basename;
3784 file->report_dir = file->dirname;
3785
Eric Biedermanb138ac82003-04-22 18:44:01 +00003786 file->prev = state->file;
3787 state->file = file;
3788
3789 process_trigraphs(state);
3790 splice_lines(state);
3791}
3792
Eric Biederman0babc1c2003-05-09 02:39:00 +00003793/* Type helper functions */
Eric Biedermanb138ac82003-04-22 18:44:01 +00003794
3795static struct type *new_type(
3796 unsigned int type, struct type *left, struct type *right)
3797{
3798 struct type *result;
3799 result = xmalloc(sizeof(*result), "type");
3800 result->type = type;
3801 result->left = left;
3802 result->right = right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00003803 result->field_ident = 0;
3804 result->type_ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003805 return result;
3806}
3807
3808static struct type *clone_type(unsigned int specifiers, struct type *old)
3809{
3810 struct type *result;
3811 result = xmalloc(sizeof(*result), "type");
3812 memcpy(result, old, sizeof(*result));
3813 result->type &= TYPE_MASK;
3814 result->type |= specifiers;
3815 return result;
3816}
3817
3818#define SIZEOF_SHORT 2
3819#define SIZEOF_INT 4
3820#define SIZEOF_LONG (sizeof(long_t))
3821
3822#define ALIGNOF_SHORT 2
3823#define ALIGNOF_INT 4
3824#define ALIGNOF_LONG (sizeof(long_t))
3825
3826#define MASK_UCHAR(X) ((X) & ((ulong_t)0xff))
3827#define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT*8)) - 1))
3828static inline ulong_t mask_uint(ulong_t x)
3829{
3830 if (SIZEOF_INT < SIZEOF_LONG) {
3831 ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT*8))) -1;
3832 x &= mask;
3833 }
3834 return x;
3835}
3836#define MASK_UINT(X) (mask_uint(X))
3837#define MASK_ULONG(X) (X)
3838
Eric Biedermanb138ac82003-04-22 18:44:01 +00003839static struct type void_type = { .type = TYPE_VOID };
3840static struct type char_type = { .type = TYPE_CHAR };
3841static struct type uchar_type = { .type = TYPE_UCHAR };
3842static struct type short_type = { .type = TYPE_SHORT };
3843static struct type ushort_type = { .type = TYPE_USHORT };
3844static struct type int_type = { .type = TYPE_INT };
3845static struct type uint_type = { .type = TYPE_UINT };
3846static struct type long_type = { .type = TYPE_LONG };
3847static struct type ulong_type = { .type = TYPE_ULONG };
3848
Eric Biederman83b991a2003-10-11 06:20:25 +00003849static struct type void_func = {
3850 .type = TYPE_FUNCTION,
3851 .left = &void_type,
3852 .right = &void_type,
3853};
3854
Eric Biedermanb138ac82003-04-22 18:44:01 +00003855static struct triple *variable(struct compile_state *state, struct type *type)
3856{
3857 struct triple *result;
3858 if ((type->type & STOR_MASK) != STOR_PERM) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00003859 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3860 result = triple(state, OP_ADECL, type, 0, 0);
3861 } else {
3862 struct type *field;
3863 struct triple **vector;
3864 ulong_t index;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003865 result = new_triple(state, OP_VAL_VEC, type, -1, -1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00003866 vector = &result->param[0];
3867
3868 field = type->left;
3869 index = 0;
3870 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
3871 vector[index] = variable(state, field->left);
3872 field = field->right;
3873 index++;
3874 }
3875 vector[index] = variable(state, field);
3876 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003877 }
3878 else {
3879 result = triple(state, OP_SDECL, type, 0, 0);
3880 }
3881 return result;
3882}
3883
3884static void stor_of(FILE *fp, struct type *type)
3885{
3886 switch(type->type & STOR_MASK) {
3887 case STOR_AUTO:
3888 fprintf(fp, "auto ");
3889 break;
3890 case STOR_STATIC:
3891 fprintf(fp, "static ");
3892 break;
3893 case STOR_EXTERN:
3894 fprintf(fp, "extern ");
3895 break;
3896 case STOR_REGISTER:
3897 fprintf(fp, "register ");
3898 break;
3899 case STOR_TYPEDEF:
3900 fprintf(fp, "typedef ");
3901 break;
3902 case STOR_INLINE:
3903 fprintf(fp, "inline ");
3904 break;
3905 }
3906}
3907static void qual_of(FILE *fp, struct type *type)
3908{
3909 if (type->type & QUAL_CONST) {
3910 fprintf(fp, " const");
3911 }
3912 if (type->type & QUAL_VOLATILE) {
3913 fprintf(fp, " volatile");
3914 }
3915 if (type->type & QUAL_RESTRICT) {
3916 fprintf(fp, " restrict");
3917 }
3918}
Eric Biederman0babc1c2003-05-09 02:39:00 +00003919
Eric Biedermanb138ac82003-04-22 18:44:01 +00003920static void name_of(FILE *fp, struct type *type)
3921{
3922 stor_of(fp, type);
3923 switch(type->type & TYPE_MASK) {
3924 case TYPE_VOID:
3925 fprintf(fp, "void");
3926 qual_of(fp, type);
3927 break;
3928 case TYPE_CHAR:
3929 fprintf(fp, "signed char");
3930 qual_of(fp, type);
3931 break;
3932 case TYPE_UCHAR:
3933 fprintf(fp, "unsigned char");
3934 qual_of(fp, type);
3935 break;
3936 case TYPE_SHORT:
3937 fprintf(fp, "signed short");
3938 qual_of(fp, type);
3939 break;
3940 case TYPE_USHORT:
3941 fprintf(fp, "unsigned short");
3942 qual_of(fp, type);
3943 break;
3944 case TYPE_INT:
3945 fprintf(fp, "signed int");
3946 qual_of(fp, type);
3947 break;
3948 case TYPE_UINT:
3949 fprintf(fp, "unsigned int");
3950 qual_of(fp, type);
3951 break;
3952 case TYPE_LONG:
3953 fprintf(fp, "signed long");
3954 qual_of(fp, type);
3955 break;
3956 case TYPE_ULONG:
3957 fprintf(fp, "unsigned long");
3958 qual_of(fp, type);
3959 break;
3960 case TYPE_POINTER:
3961 name_of(fp, type->left);
3962 fprintf(fp, " * ");
3963 qual_of(fp, type);
3964 break;
3965 case TYPE_PRODUCT:
3966 case TYPE_OVERLAP:
3967 name_of(fp, type->left);
3968 fprintf(fp, ", ");
3969 name_of(fp, type->right);
3970 break;
3971 case TYPE_ENUM:
Eric Biederman0babc1c2003-05-09 02:39:00 +00003972 fprintf(fp, "enum %s", type->type_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003973 qual_of(fp, type);
3974 break;
3975 case TYPE_STRUCT:
Eric Biederman0babc1c2003-05-09 02:39:00 +00003976 fprintf(fp, "struct %s", type->type_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003977 qual_of(fp, type);
3978 break;
3979 case TYPE_FUNCTION:
3980 {
3981 name_of(fp, type->left);
3982 fprintf(fp, " (*)(");
3983 name_of(fp, type->right);
3984 fprintf(fp, ")");
3985 break;
3986 }
3987 case TYPE_ARRAY:
3988 name_of(fp, type->left);
Eric Biederman83b991a2003-10-11 06:20:25 +00003989 fprintf(fp, " [%ld]", (long)(type->elements));
Eric Biedermanb138ac82003-04-22 18:44:01 +00003990 break;
3991 default:
3992 fprintf(fp, "????: %x", type->type & TYPE_MASK);
3993 break;
3994 }
3995}
3996
3997static size_t align_of(struct compile_state *state, struct type *type)
3998{
3999 size_t align;
4000 align = 0;
4001 switch(type->type & TYPE_MASK) {
4002 case TYPE_VOID:
4003 align = 1;
4004 break;
4005 case TYPE_CHAR:
4006 case TYPE_UCHAR:
4007 align = 1;
4008 break;
4009 case TYPE_SHORT:
4010 case TYPE_USHORT:
4011 align = ALIGNOF_SHORT;
4012 break;
4013 case TYPE_INT:
4014 case TYPE_UINT:
4015 case TYPE_ENUM:
4016 align = ALIGNOF_INT;
4017 break;
4018 case TYPE_LONG:
4019 case TYPE_ULONG:
4020 case TYPE_POINTER:
4021 align = ALIGNOF_LONG;
4022 break;
4023 case TYPE_PRODUCT:
4024 case TYPE_OVERLAP:
4025 {
4026 size_t left_align, right_align;
4027 left_align = align_of(state, type->left);
4028 right_align = align_of(state, type->right);
4029 align = (left_align >= right_align) ? left_align : right_align;
4030 break;
4031 }
4032 case TYPE_ARRAY:
4033 align = align_of(state, type->left);
4034 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004035 case TYPE_STRUCT:
4036 align = align_of(state, type->left);
4037 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004038 default:
4039 error(state, 0, "alignof not yet defined for type\n");
4040 break;
4041 }
4042 return align;
4043}
4044
Eric Biederman03b59862003-06-24 14:27:37 +00004045static size_t needed_padding(size_t offset, size_t align)
4046{
4047 size_t padding;
4048 padding = 0;
4049 if (offset % align) {
4050 padding = align - (offset % align);
4051 }
4052 return padding;
4053}
Eric Biedermanb138ac82003-04-22 18:44:01 +00004054static size_t size_of(struct compile_state *state, struct type *type)
4055{
4056 size_t size;
4057 size = 0;
4058 switch(type->type & TYPE_MASK) {
4059 case TYPE_VOID:
4060 size = 0;
4061 break;
4062 case TYPE_CHAR:
4063 case TYPE_UCHAR:
4064 size = 1;
4065 break;
4066 case TYPE_SHORT:
4067 case TYPE_USHORT:
4068 size = SIZEOF_SHORT;
4069 break;
4070 case TYPE_INT:
4071 case TYPE_UINT:
4072 case TYPE_ENUM:
4073 size = SIZEOF_INT;
4074 break;
4075 case TYPE_LONG:
4076 case TYPE_ULONG:
4077 case TYPE_POINTER:
4078 size = SIZEOF_LONG;
4079 break;
4080 case TYPE_PRODUCT:
4081 {
4082 size_t align, pad;
Eric Biederman03b59862003-06-24 14:27:37 +00004083 size = 0;
4084 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004085 align = align_of(state, type->left);
Eric Biederman03b59862003-06-24 14:27:37 +00004086 pad = needed_padding(size, align);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004087 size = size + pad + size_of(state, type->left);
Eric Biederman03b59862003-06-24 14:27:37 +00004088 type = type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004089 }
Eric Biederman03b59862003-06-24 14:27:37 +00004090 align = align_of(state, type);
4091 pad = needed_padding(size, align);
Eric Biedermane058a1e2003-07-12 01:21:31 +00004092 size = size + pad + size_of(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004093 break;
4094 }
4095 case TYPE_OVERLAP:
4096 {
4097 size_t size_left, size_right;
4098 size_left = size_of(state, type->left);
4099 size_right = size_of(state, type->right);
4100 size = (size_left >= size_right)? size_left : size_right;
4101 break;
4102 }
4103 case TYPE_ARRAY:
4104 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4105 internal_error(state, 0, "Invalid array type");
4106 } else {
4107 size = size_of(state, type->left) * type->elements;
4108 }
4109 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004110 case TYPE_STRUCT:
Eric Biedermane058a1e2003-07-12 01:21:31 +00004111 {
4112 size_t align, pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004113 size = size_of(state, type->left);
Eric Biedermane058a1e2003-07-12 01:21:31 +00004114 /* Pad structures so their size is a multiples of their alignment */
4115 align = align_of(state, type);
4116 pad = needed_padding(size, align);
4117 size = size + pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004118 break;
Eric Biedermane058a1e2003-07-12 01:21:31 +00004119 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004120 default:
Eric Biedermand1ea5392003-06-28 06:49:45 +00004121 internal_error(state, 0, "sizeof not yet defined for type\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004122 break;
4123 }
4124 return size;
4125}
4126
Eric Biederman0babc1c2003-05-09 02:39:00 +00004127static size_t field_offset(struct compile_state *state,
4128 struct type *type, struct hash_entry *field)
4129{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004130 struct type *member;
Eric Biederman03b59862003-06-24 14:27:37 +00004131 size_t size, align;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004132 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4133 internal_error(state, 0, "field_offset only works on structures");
4134 }
4135 size = 0;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004136 member = type->left;
4137 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4138 align = align_of(state, member->left);
Eric Biederman03b59862003-06-24 14:27:37 +00004139 size += needed_padding(size, align);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004140 if (member->left->field_ident == field) {
4141 member = member->left;
Eric Biederman00443072003-06-24 12:34:45 +00004142 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004143 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004144 size += size_of(state, member->left);
4145 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004146 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004147 align = align_of(state, member);
Eric Biederman03b59862003-06-24 14:27:37 +00004148 size += needed_padding(size, align);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004149 if (member->field_ident != field) {
Eric Biederman03b59862003-06-24 14:27:37 +00004150 error(state, 0, "member %s not present", field->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00004151 }
4152 return size;
4153}
4154
4155static struct type *field_type(struct compile_state *state,
4156 struct type *type, struct hash_entry *field)
4157{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004158 struct type *member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004159 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4160 internal_error(state, 0, "field_type only works on structures");
4161 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004162 member = type->left;
4163 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4164 if (member->left->field_ident == field) {
4165 member = member->left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004166 break;
4167 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004168 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004169 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004170 if (member->field_ident != field) {
Eric Biederman03b59862003-06-24 14:27:37 +00004171 error(state, 0, "member %s not present", field->name);
4172 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004173 return member;
Eric Biederman03b59862003-06-24 14:27:37 +00004174}
4175
4176static struct type *next_field(struct compile_state *state,
4177 struct type *type, struct type *prev_member)
4178{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004179 struct type *member;
Eric Biederman03b59862003-06-24 14:27:37 +00004180 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4181 internal_error(state, 0, "next_field only works on structures");
4182 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004183 member = type->left;
4184 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman03b59862003-06-24 14:27:37 +00004185 if (!prev_member) {
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004186 member = member->left;
Eric Biederman03b59862003-06-24 14:27:37 +00004187 break;
4188 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004189 if (member->left == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00004190 prev_member = 0;
4191 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004192 member = member->right;
Eric Biederman03b59862003-06-24 14:27:37 +00004193 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004194 if (member == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00004195 prev_member = 0;
4196 }
4197 if (prev_member) {
4198 internal_error(state, 0, "prev_member %s not present",
4199 prev_member->field_ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00004200 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004201 return member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004202}
4203
4204static struct triple *struct_field(struct compile_state *state,
4205 struct triple *decl, struct hash_entry *field)
4206{
4207 struct triple **vector;
4208 struct type *type;
4209 ulong_t index;
4210 type = decl->type;
4211 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4212 return decl;
4213 }
4214 if (decl->op != OP_VAL_VEC) {
4215 internal_error(state, 0, "Invalid struct variable");
4216 }
4217 if (!field) {
4218 internal_error(state, 0, "Missing structure field");
4219 }
4220 type = type->left;
4221 vector = &RHS(decl, 0);
4222 index = 0;
4223 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4224 if (type->left->field_ident == field) {
4225 type = type->left;
4226 break;
4227 }
4228 index += 1;
4229 type = type->right;
4230 }
4231 if (type->field_ident != field) {
4232 internal_error(state, 0, "field %s not found?", field->name);
4233 }
4234 return vector[index];
4235}
4236
Eric Biedermanb138ac82003-04-22 18:44:01 +00004237static void arrays_complete(struct compile_state *state, struct type *type)
4238{
4239 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
4240 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4241 error(state, 0, "array size not specified");
4242 }
4243 arrays_complete(state, type->left);
4244 }
4245}
4246
4247static unsigned int do_integral_promotion(unsigned int type)
4248{
4249 type &= TYPE_MASK;
Eric Biederman83b991a2003-10-11 06:20:25 +00004250 if (type == TYPE_ENUM) type = TYPE_INT;
4251 if (TYPE_INTEGER(type) && (TYPE_RANK(type) < TYPE_RANK(TYPE_INT))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004252 type = TYPE_INT;
4253 }
4254 return type;
4255}
4256
4257static unsigned int do_arithmetic_conversion(
4258 unsigned int left, unsigned int right)
4259{
4260 left &= TYPE_MASK;
4261 right &= TYPE_MASK;
Eric Biederman83b991a2003-10-11 06:20:25 +00004262 /* Convert enums to ints */
4263 if (left == TYPE_ENUM) left = TYPE_INT;
4264 if (right == TYPE_ENUM) right = TYPE_INT;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004265 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
4266 return TYPE_LDOUBLE;
4267 }
4268 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
4269 return TYPE_DOUBLE;
4270 }
4271 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
4272 return TYPE_FLOAT;
4273 }
4274 left = do_integral_promotion(left);
4275 right = do_integral_promotion(right);
4276 /* If both operands have the same size done */
4277 if (left == right) {
4278 return left;
4279 }
4280 /* If both operands have the same signedness pick the larger */
4281 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
4282 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
4283 }
4284 /* If the signed type can hold everything use it */
4285 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
4286 return left;
4287 }
4288 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
4289 return right;
4290 }
4291 /* Convert to the unsigned type with the same rank as the signed type */
4292 else if (TYPE_SIGNED(left)) {
4293 return TYPE_MKUNSIGNED(left);
4294 }
4295 else {
4296 return TYPE_MKUNSIGNED(right);
4297 }
4298}
4299
4300/* see if two types are the same except for qualifiers */
4301static int equiv_types(struct type *left, struct type *right)
4302{
4303 unsigned int type;
4304 /* Error if the basic types do not match */
4305 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4306 return 0;
4307 }
4308 type = left->type & TYPE_MASK;
Eric Biederman530b5192003-07-01 10:05:30 +00004309 /* If the basic types match and it is a void type we are done */
4310 if (type == TYPE_VOID) {
4311 return 1;
4312 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004313 /* if the basic types match and it is an arithmetic type we are done */
4314 if (TYPE_ARITHMETIC(type)) {
4315 return 1;
4316 }
4317 /* If it is a pointer type recurse and keep testing */
4318 if (type == TYPE_POINTER) {
4319 return equiv_types(left->left, right->left);
4320 }
4321 else if (type == TYPE_ARRAY) {
4322 return (left->elements == right->elements) &&
4323 equiv_types(left->left, right->left);
4324 }
4325 /* test for struct/union equality */
4326 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004327 return left->type_ident == right->type_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004328 }
4329 /* Test for equivalent functions */
4330 else if (type == TYPE_FUNCTION) {
4331 return equiv_types(left->left, right->left) &&
4332 equiv_types(left->right, right->right);
4333 }
4334 /* We only see TYPE_PRODUCT as part of function equivalence matching */
4335 else if (type == TYPE_PRODUCT) {
4336 return equiv_types(left->left, right->left) &&
4337 equiv_types(left->right, right->right);
4338 }
4339 /* We should see TYPE_OVERLAP */
4340 else {
4341 return 0;
4342 }
4343}
4344
4345static int equiv_ptrs(struct type *left, struct type *right)
4346{
4347 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4348 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4349 return 0;
4350 }
4351 return equiv_types(left->left, right->left);
4352}
4353
4354static struct type *compatible_types(struct type *left, struct type *right)
4355{
4356 struct type *result;
4357 unsigned int type, qual_type;
4358 /* Error if the basic types do not match */
4359 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4360 return 0;
4361 }
4362 type = left->type & TYPE_MASK;
4363 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4364 result = 0;
4365 /* if the basic types match and it is an arithmetic type we are done */
4366 if (TYPE_ARITHMETIC(type)) {
4367 result = new_type(qual_type, 0, 0);
4368 }
4369 /* If it is a pointer type recurse and keep testing */
4370 else if (type == TYPE_POINTER) {
4371 result = compatible_types(left->left, right->left);
4372 if (result) {
4373 result = new_type(qual_type, result, 0);
4374 }
4375 }
4376 /* test for struct/union equality */
4377 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004378 if (left->type_ident == right->type_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004379 result = left;
4380 }
4381 }
4382 /* Test for equivalent functions */
4383 else if (type == TYPE_FUNCTION) {
4384 struct type *lf, *rf;
4385 lf = compatible_types(left->left, right->left);
4386 rf = compatible_types(left->right, right->right);
4387 if (lf && rf) {
4388 result = new_type(qual_type, lf, rf);
4389 }
4390 }
4391 /* We only see TYPE_PRODUCT as part of function equivalence matching */
4392 else if (type == TYPE_PRODUCT) {
4393 struct type *lf, *rf;
4394 lf = compatible_types(left->left, right->left);
4395 rf = compatible_types(left->right, right->right);
4396 if (lf && rf) {
4397 result = new_type(qual_type, lf, rf);
4398 }
4399 }
4400 else {
4401 /* Nothing else is compatible */
4402 }
4403 return result;
4404}
4405
4406static struct type *compatible_ptrs(struct type *left, struct type *right)
4407{
4408 struct type *result;
4409 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4410 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4411 return 0;
4412 }
4413 result = compatible_types(left->left, right->left);
4414 if (result) {
4415 unsigned int qual_type;
4416 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4417 result = new_type(qual_type, result, 0);
4418 }
4419 return result;
4420
4421}
4422static struct triple *integral_promotion(
4423 struct compile_state *state, struct triple *def)
4424{
4425 struct type *type;
4426 type = def->type;
4427 /* As all operations are carried out in registers
4428 * the values are converted on load I just convert
4429 * logical type of the operand.
4430 */
4431 if (TYPE_INTEGER(type->type)) {
4432 unsigned int int_type;
4433 int_type = type->type & ~TYPE_MASK;
4434 int_type |= do_integral_promotion(type->type);
4435 if (int_type != type->type) {
4436 def->type = new_type(int_type, 0, 0);
4437 }
4438 }
4439 return def;
4440}
4441
4442
4443static void arithmetic(struct compile_state *state, struct triple *def)
4444{
4445 if (!TYPE_ARITHMETIC(def->type->type)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004446 error(state, 0, "arithmetic type expexted");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004447 }
4448}
4449
4450static void ptr_arithmetic(struct compile_state *state, struct triple *def)
4451{
4452 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
4453 error(state, def, "pointer or arithmetic type expected");
4454 }
4455}
4456
4457static int is_integral(struct triple *ins)
4458{
4459 return TYPE_INTEGER(ins->type->type);
4460}
4461
4462static void integral(struct compile_state *state, struct triple *def)
4463{
4464 if (!is_integral(def)) {
4465 error(state, 0, "integral type expected");
4466 }
4467}
4468
4469
4470static void bool(struct compile_state *state, struct triple *def)
4471{
4472 if (!TYPE_ARITHMETIC(def->type->type) &&
4473 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
4474 error(state, 0, "arithmetic or pointer type expected");
4475 }
4476}
4477
4478static int is_signed(struct type *type)
4479{
4480 return !!TYPE_SIGNED(type->type);
4481}
4482
Eric Biederman0babc1c2003-05-09 02:39:00 +00004483/* Is this value located in a register otherwise it must be in memory */
4484static int is_in_reg(struct compile_state *state, struct triple *def)
4485{
4486 int in_reg;
4487 if (def->op == OP_ADECL) {
4488 in_reg = 1;
4489 }
4490 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
4491 in_reg = 0;
4492 }
4493 else if (def->op == OP_VAL_VEC) {
4494 in_reg = is_in_reg(state, RHS(def, 0));
4495 }
4496 else if (def->op == OP_DOT) {
4497 in_reg = is_in_reg(state, RHS(def, 0));
4498 }
4499 else {
4500 internal_error(state, 0, "unknown expr storage location");
4501 in_reg = -1;
4502 }
4503 return in_reg;
4504}
4505
Eric Biedermanb138ac82003-04-22 18:44:01 +00004506/* Is this a stable variable location otherwise it must be a temporary */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004507static int is_stable(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004508{
4509 int ret;
4510 ret = 0;
4511 if (!def) {
4512 return 0;
4513 }
4514 if ((def->op == OP_ADECL) ||
4515 (def->op == OP_SDECL) ||
4516 (def->op == OP_DEREF) ||
4517 (def->op == OP_BLOBCONST)) {
4518 ret = 1;
4519 }
4520 else if (def->op == OP_DOT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004521 ret = is_stable(state, RHS(def, 0));
4522 }
4523 else if (def->op == OP_VAL_VEC) {
4524 struct triple **vector;
4525 ulong_t i;
4526 ret = 1;
4527 vector = &RHS(def, 0);
4528 for(i = 0; i < def->type->elements; i++) {
4529 if (!is_stable(state, vector[i])) {
4530 ret = 0;
4531 break;
4532 }
4533 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004534 }
4535 return ret;
4536}
4537
Eric Biederman0babc1c2003-05-09 02:39:00 +00004538static int is_lvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004539{
4540 int ret;
4541 ret = 1;
4542 if (!def) {
4543 return 0;
4544 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004545 if (!is_stable(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004546 return 0;
4547 }
Eric Biederman00443072003-06-24 12:34:45 +00004548 if (def->op == OP_DOT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004549 ret = is_lvalue(state, RHS(def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00004550 }
4551 return ret;
4552}
4553
Eric Biederman00443072003-06-24 12:34:45 +00004554static void clvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004555{
4556 if (!def) {
4557 internal_error(state, def, "nothing where lvalue expected?");
4558 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004559 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004560 error(state, def, "lvalue expected");
4561 }
4562}
Eric Biederman00443072003-06-24 12:34:45 +00004563static void lvalue(struct compile_state *state, struct triple *def)
4564{
4565 clvalue(state, def);
4566 if (def->type->type & QUAL_CONST) {
4567 error(state, def, "modifable lvalue expected");
4568 }
4569}
Eric Biedermanb138ac82003-04-22 18:44:01 +00004570
4571static int is_pointer(struct triple *def)
4572{
4573 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
4574}
4575
4576static void pointer(struct compile_state *state, struct triple *def)
4577{
4578 if (!is_pointer(def)) {
4579 error(state, def, "pointer expected");
4580 }
4581}
4582
4583static struct triple *int_const(
4584 struct compile_state *state, struct type *type, ulong_t value)
4585{
4586 struct triple *result;
4587 switch(type->type & TYPE_MASK) {
4588 case TYPE_CHAR:
4589 case TYPE_INT: case TYPE_UINT:
4590 case TYPE_LONG: case TYPE_ULONG:
4591 break;
4592 default:
4593 internal_error(state, 0, "constant for unkown type");
4594 }
4595 result = triple(state, OP_INTCONST, type, 0, 0);
4596 result->u.cval = value;
4597 return result;
4598}
4599
4600
Eric Biederman83b991a2003-10-11 06:20:25 +00004601static struct triple *read_expr(struct compile_state *state, struct triple *def);
4602
Eric Biederman0babc1c2003-05-09 02:39:00 +00004603static struct triple *do_mk_addr_expr(struct compile_state *state,
4604 struct triple *expr, struct type *type, ulong_t offset)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004605{
4606 struct triple *result;
Eric Biederman00443072003-06-24 12:34:45 +00004607 clvalue(state, expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004608
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004609 type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
4610
Eric Biedermanb138ac82003-04-22 18:44:01 +00004611 result = 0;
4612 if (expr->op == OP_ADECL) {
4613 error(state, expr, "address of auto variables not supported");
4614 }
4615 else if (expr->op == OP_SDECL) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004616 result = triple(state, OP_ADDRCONST, type, 0, 0);
4617 MISC(result, 0) = expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004618 result->u.cval = offset;
4619 }
4620 else if (expr->op == OP_DEREF) {
4621 result = triple(state, OP_ADD, type,
Eric Biederman0babc1c2003-05-09 02:39:00 +00004622 RHS(expr, 0),
Eric Biedermanb138ac82003-04-22 18:44:01 +00004623 int_const(state, &ulong_type, offset));
4624 }
Eric Biederman83b991a2003-10-11 06:20:25 +00004625 if (!result) {
4626 internal_error(state, expr, "cannot take address of expression");
4627 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004628 return result;
4629}
4630
Eric Biederman0babc1c2003-05-09 02:39:00 +00004631static struct triple *mk_addr_expr(
4632 struct compile_state *state, struct triple *expr, ulong_t offset)
4633{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004634 return do_mk_addr_expr(state, expr, expr->type, offset);
Eric Biederman0babc1c2003-05-09 02:39:00 +00004635}
4636
Eric Biedermanb138ac82003-04-22 18:44:01 +00004637static struct triple *mk_deref_expr(
4638 struct compile_state *state, struct triple *expr)
4639{
4640 struct type *base_type;
4641 pointer(state, expr);
4642 base_type = expr->type->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004643 return triple(state, OP_DEREF, base_type, expr, 0);
4644}
4645
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004646static struct triple *array_to_pointer(struct compile_state *state, struct triple *def)
4647{
4648 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
4649 struct type *type;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004650 type = new_type(
4651 TYPE_POINTER | (def->type->type & QUAL_MASK),
4652 def->type->left, 0);
Eric Biederman66fe2222003-07-04 15:14:04 +00004653 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
Eric Biederman830c9882003-07-04 00:27:33 +00004654 struct triple *addrconst;
4655 if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
4656 internal_error(state, def, "bad array constant");
4657 }
4658 addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
4659 MISC(addrconst, 0) = def;
4660 def = addrconst;
4661 }
4662 else {
4663 def = triple(state, OP_COPY, type, def, 0);
4664 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004665 }
4666 return def;
4667}
4668
Eric Biederman0babc1c2003-05-09 02:39:00 +00004669static struct triple *deref_field(
4670 struct compile_state *state, struct triple *expr, struct hash_entry *field)
4671{
4672 struct triple *result;
4673 struct type *type, *member;
4674 if (!field) {
4675 internal_error(state, 0, "No field passed to deref_field");
4676 }
4677 result = 0;
4678 type = expr->type;
4679 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4680 error(state, 0, "request for member %s in something not a struct or union",
4681 field->name);
4682 }
Eric Biederman03b59862003-06-24 14:27:37 +00004683 member = field_type(state, type, field);
Eric Biederman0babc1c2003-05-09 02:39:00 +00004684 if ((type->type & STOR_MASK) == STOR_PERM) {
4685 /* Do the pointer arithmetic to get a deref the field */
4686 ulong_t offset;
4687 offset = field_offset(state, type, field);
4688 result = do_mk_addr_expr(state, expr, member, offset);
4689 result = mk_deref_expr(state, result);
4690 }
4691 else {
4692 /* Find the variable for the field I want. */
Eric Biederman03b59862003-06-24 14:27:37 +00004693 result = triple(state, OP_DOT, member, expr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00004694 result->u.field = field;
4695 }
4696 return result;
4697}
4698
Eric Biedermanb138ac82003-04-22 18:44:01 +00004699static struct triple *read_expr(struct compile_state *state, struct triple *def)
4700{
4701 int op;
4702 if (!def) {
4703 return 0;
4704 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004705 if (!is_stable(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004706 return def;
4707 }
4708 /* Tranform an array to a pointer to the first element */
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004709
Eric Biedermanb138ac82003-04-22 18:44:01 +00004710#warning "CHECK_ME is this the right place to transform arrays to pointers?"
4711 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
Eric Biederman3a51f3b2003-06-25 10:38:10 +00004712 return array_to_pointer(state, def);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004713 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004714 if (is_in_reg(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004715 op = OP_READ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004716 } else {
Eric Biederman83b991a2003-10-11 06:20:25 +00004717 if (def->op == OP_SDECL) {
4718 def = mk_addr_expr(state, def, 0);
4719 def = mk_deref_expr(state, def);
4720 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004721 op = OP_LOAD;
4722 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004723 return triple(state, op, def->type, def, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004724}
4725
Eric Biedermane058a1e2003-07-12 01:21:31 +00004726int is_write_compatible(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00004727 struct type *dest, struct type *rval)
4728{
4729 int compatible = 0;
4730 /* Both operands have arithmetic type */
4731 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
4732 compatible = 1;
4733 }
4734 /* One operand is a pointer and the other is a pointer to void */
4735 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
4736 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
4737 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
4738 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
4739 compatible = 1;
4740 }
4741 /* If both types are the same without qualifiers we are good */
4742 else if (equiv_ptrs(dest, rval)) {
4743 compatible = 1;
4744 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004745 /* test for struct/union equality */
4746 else if (((dest->type & TYPE_MASK) == TYPE_STRUCT) &&
4747 ((rval->type & TYPE_MASK) == TYPE_STRUCT) &&
4748 (dest->type_ident == rval->type_ident)) {
4749 compatible = 1;
4750 }
Eric Biedermane058a1e2003-07-12 01:21:31 +00004751 return compatible;
4752}
4753
4754
4755static void write_compatible(struct compile_state *state,
4756 struct type *dest, struct type *rval)
4757{
4758 if (!is_write_compatible(state, dest, rval)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004759 error(state, 0, "Incompatible types in assignment");
4760 }
4761}
4762
Eric Biedermane058a1e2003-07-12 01:21:31 +00004763static int is_init_compatible(struct compile_state *state,
4764 struct type *dest, struct type *rval)
4765{
4766 int compatible = 0;
4767 if (is_write_compatible(state, dest, rval)) {
4768 compatible = 1;
4769 }
4770 else if (equiv_types(dest, rval)) {
4771 compatible = 1;
4772 }
4773 return compatible;
4774}
4775
Eric Biedermanb138ac82003-04-22 18:44:01 +00004776static struct triple *write_expr(
4777 struct compile_state *state, struct triple *dest, struct triple *rval)
4778{
4779 struct triple *def;
4780 int op;
4781
4782 def = 0;
4783 if (!rval) {
4784 internal_error(state, 0, "missing rval");
4785 }
4786
4787 if (rval->op == OP_LIST) {
4788 internal_error(state, 0, "expression of type OP_LIST?");
4789 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004790 if (!is_lvalue(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004791 internal_error(state, 0, "writing to a non lvalue?");
4792 }
Eric Biederman00443072003-06-24 12:34:45 +00004793 if (dest->type->type & QUAL_CONST) {
4794 internal_error(state, 0, "modifable lvalue expexted");
4795 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004796
4797 write_compatible(state, dest->type, rval->type);
4798
4799 /* Now figure out which assignment operator to use */
4800 op = -1;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004801 if (is_in_reg(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004802 op = OP_WRITE;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004803 } else {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004804 op = OP_STORE;
4805 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004806 def = triple(state, op, dest->type, dest, rval);
4807 return def;
4808}
4809
4810static struct triple *init_expr(
4811 struct compile_state *state, struct triple *dest, struct triple *rval)
4812{
4813 struct triple *def;
4814
4815 def = 0;
4816 if (!rval) {
4817 internal_error(state, 0, "missing rval");
4818 }
4819 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
4820 rval = read_expr(state, rval);
4821 def = write_expr(state, dest, rval);
4822 }
4823 else {
4824 /* Fill in the array size if necessary */
4825 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
4826 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
4827 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4828 dest->type->elements = rval->type->elements;
4829 }
4830 }
4831 if (!equiv_types(dest->type, rval->type)) {
4832 error(state, 0, "Incompatible types in inializer");
4833 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004834 MISC(dest, 0) = rval;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004835 insert_triple(state, dest, rval);
4836 rval->id |= TRIPLE_FLAG_FLATTENED;
4837 use_triple(MISC(dest, 0), dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004838 }
4839 return def;
4840}
4841
4842struct type *arithmetic_result(
4843 struct compile_state *state, struct triple *left, struct triple *right)
4844{
4845 struct type *type;
4846 /* Sanity checks to ensure I am working with arithmetic types */
4847 arithmetic(state, left);
4848 arithmetic(state, right);
4849 type = new_type(
4850 do_arithmetic_conversion(
4851 left->type->type,
4852 right->type->type), 0, 0);
4853 return type;
4854}
4855
4856struct type *ptr_arithmetic_result(
4857 struct compile_state *state, struct triple *left, struct triple *right)
4858{
4859 struct type *type;
4860 /* Sanity checks to ensure I am working with the proper types */
4861 ptr_arithmetic(state, left);
4862 arithmetic(state, right);
4863 if (TYPE_ARITHMETIC(left->type->type) &&
4864 TYPE_ARITHMETIC(right->type->type)) {
4865 type = arithmetic_result(state, left, right);
4866 }
4867 else if (TYPE_PTR(left->type->type)) {
4868 type = left->type;
4869 }
4870 else {
4871 internal_error(state, 0, "huh?");
4872 type = 0;
4873 }
4874 return type;
4875}
4876
4877
4878/* boolean helper function */
4879
4880static struct triple *ltrue_expr(struct compile_state *state,
4881 struct triple *expr)
4882{
4883 switch(expr->op) {
4884 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
4885 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
4886 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
4887 /* If the expression is already boolean do nothing */
4888 break;
4889 default:
4890 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
4891 break;
4892 }
4893 return expr;
4894}
4895
4896static struct triple *lfalse_expr(struct compile_state *state,
4897 struct triple *expr)
4898{
4899 return triple(state, OP_LFALSE, &int_type, expr, 0);
4900}
4901
4902static struct triple *cond_expr(
4903 struct compile_state *state,
4904 struct triple *test, struct triple *left, struct triple *right)
4905{
4906 struct triple *def;
4907 struct type *result_type;
4908 unsigned int left_type, right_type;
4909 bool(state, test);
4910 left_type = left->type->type;
4911 right_type = right->type->type;
4912 result_type = 0;
4913 /* Both operands have arithmetic type */
4914 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
4915 result_type = arithmetic_result(state, left, right);
4916 }
4917 /* Both operands have void type */
4918 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
4919 ((right_type & TYPE_MASK) == TYPE_VOID)) {
4920 result_type = &void_type;
4921 }
4922 /* pointers to the same type... */
4923 else if ((result_type = compatible_ptrs(left->type, right->type))) {
4924 ;
4925 }
4926 /* Both operands are pointers and left is a pointer to void */
4927 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4928 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4929 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4930 result_type = right->type;
4931 }
4932 /* Both operands are pointers and right is a pointer to void */
4933 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4934 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4935 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4936 result_type = left->type;
4937 }
4938 if (!result_type) {
4939 error(state, 0, "Incompatible types in conditional expression");
4940 }
Eric Biederman30276382003-05-16 20:47:48 +00004941 /* Cleanup and invert the test */
4942 test = lfalse_expr(state, read_expr(state, test));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004943 def = new_triple(state, OP_COND, result_type, 0, 3);
Eric Biederman0babc1c2003-05-09 02:39:00 +00004944 def->param[0] = test;
4945 def->param[1] = left;
4946 def->param[2] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004947 return def;
4948}
4949
4950
Eric Biederman0babc1c2003-05-09 02:39:00 +00004951static int expr_depth(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004952{
4953 int count;
4954 count = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004955 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
4956 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004957 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004958 else if (ins->op == OP_DEREF) {
4959 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004960 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004961 else if (ins->op == OP_VAL) {
4962 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004963 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004964 else if (ins->op == OP_COMMA) {
4965 int ldepth, rdepth;
4966 ldepth = expr_depth(state, RHS(ins, 0));
4967 rdepth = expr_depth(state, RHS(ins, 1));
4968 count = (ldepth >= rdepth)? ldepth : rdepth;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004969 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004970 else if (ins->op == OP_CALL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004971 /* Don't figure the depth of a call just guess it is huge */
4972 count = 1000;
4973 }
4974 else {
4975 struct triple **expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004976 expr = triple_rhs(state, ins, 0);
4977 for(;expr; expr = triple_rhs(state, ins, expr)) {
4978 if (*expr) {
4979 int depth;
4980 depth = expr_depth(state, *expr);
4981 if (depth > count) {
4982 count = depth;
4983 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004984 }
4985 }
4986 }
4987 return count + 1;
4988}
4989
4990static struct triple *flatten(
4991 struct compile_state *state, struct triple *first, struct triple *ptr);
4992
Eric Biederman0babc1c2003-05-09 02:39:00 +00004993static struct triple *flatten_generic(
Eric Biedermanb138ac82003-04-22 18:44:01 +00004994 struct compile_state *state, struct triple *first, struct triple *ptr)
4995{
Eric Biederman0babc1c2003-05-09 02:39:00 +00004996 struct rhs_vector {
4997 int depth;
4998 struct triple **ins;
4999 } vector[MAX_RHS];
5000 int i, rhs, lhs;
5001 /* Only operations with just a rhs should come here */
5002 rhs = TRIPLE_RHS(ptr->sizes);
5003 lhs = TRIPLE_LHS(ptr->sizes);
5004 if (TRIPLE_SIZE(ptr->sizes) != lhs + rhs) {
5005 internal_error(state, ptr, "unexpected args for: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +00005006 ptr->op, tops(ptr->op));
5007 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005008 /* Find the depth of the rhs elements */
5009 for(i = 0; i < rhs; i++) {
5010 vector[i].ins = &RHS(ptr, i);
5011 vector[i].depth = expr_depth(state, *vector[i].ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005012 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005013 /* Selection sort the rhs */
5014 for(i = 0; i < rhs; i++) {
5015 int j, max = i;
5016 for(j = i + 1; j < rhs; j++ ) {
5017 if (vector[j].depth > vector[max].depth) {
5018 max = j;
5019 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005020 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005021 if (max != i) {
5022 struct rhs_vector tmp;
5023 tmp = vector[i];
5024 vector[i] = vector[max];
5025 vector[max] = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005026 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005027 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005028 /* Now flatten the rhs elements */
5029 for(i = 0; i < rhs; i++) {
5030 *vector[i].ins = flatten(state, first, *vector[i].ins);
5031 use_triple(*vector[i].ins, ptr);
5032 }
5033
5034 /* Now flatten the lhs elements */
5035 for(i = 0; i < lhs; i++) {
5036 struct triple **ins = &LHS(ptr, i);
5037 *ins = flatten(state, first, *ins);
5038 use_triple(*ins, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005039 }
5040 return ptr;
5041}
5042
5043static struct triple *flatten_land(
5044 struct compile_state *state, struct triple *first, struct triple *ptr)
5045{
5046 struct triple *left, *right;
5047 struct triple *val, *test, *jmp, *label1, *end;
5048
5049 /* Find the triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005050 left = RHS(ptr, 0);
5051 right = RHS(ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005052
5053 /* Generate the needed triples */
5054 end = label(state);
5055
5056 /* Thread the triples together */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005057 val = flatten(state, first, variable(state, ptr->type));
5058 left = flatten(state, first, write_expr(state, val, left));
5059 test = flatten(state, first,
Eric Biedermanb138ac82003-04-22 18:44:01 +00005060 lfalse_expr(state, read_expr(state, val)));
Eric Biederman0babc1c2003-05-09 02:39:00 +00005061 jmp = flatten(state, first, branch(state, end, test));
5062 label1 = flatten(state, first, label(state));
5063 right = flatten(state, first, write_expr(state, val, right));
5064 TARG(jmp, 0) = flatten(state, first, end);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005065
5066 /* Now give the caller something to chew on */
5067 return read_expr(state, val);
5068}
5069
5070static struct triple *flatten_lor(
5071 struct compile_state *state, struct triple *first, struct triple *ptr)
5072{
5073 struct triple *left, *right;
5074 struct triple *val, *jmp, *label1, *end;
5075
5076 /* Find the triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005077 left = RHS(ptr, 0);
5078 right = RHS(ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005079
5080 /* Generate the needed triples */
5081 end = label(state);
5082
5083 /* Thread the triples together */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005084 val = flatten(state, first, variable(state, ptr->type));
5085 left = flatten(state, first, write_expr(state, val, left));
5086 jmp = flatten(state, first, branch(state, end, left));
5087 label1 = flatten(state, first, label(state));
5088 right = flatten(state, first, write_expr(state, val, right));
5089 TARG(jmp, 0) = flatten(state, first, end);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005090
5091
5092 /* Now give the caller something to chew on */
5093 return read_expr(state, val);
5094}
5095
5096static struct triple *flatten_cond(
5097 struct compile_state *state, struct triple *first, struct triple *ptr)
5098{
5099 struct triple *test, *left, *right;
5100 struct triple *val, *mv1, *jmp1, *label1, *mv2, *middle, *jmp2, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005101
5102 /* Find the triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005103 test = RHS(ptr, 0);
5104 left = RHS(ptr, 1);
5105 right = RHS(ptr, 2);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005106
5107 /* Generate the needed triples */
5108 end = label(state);
5109 middle = label(state);
5110
5111 /* Thread the triples together */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005112 val = flatten(state, first, variable(state, ptr->type));
5113 test = flatten(state, first, test);
5114 jmp1 = flatten(state, first, branch(state, middle, test));
5115 label1 = flatten(state, first, label(state));
5116 left = flatten(state, first, left);
5117 mv1 = flatten(state, first, write_expr(state, val, left));
5118 jmp2 = flatten(state, first, branch(state, end, 0));
5119 TARG(jmp1, 0) = flatten(state, first, middle);
5120 right = flatten(state, first, right);
5121 mv2 = flatten(state, first, write_expr(state, val, right));
5122 TARG(jmp2, 0) = flatten(state, first, end);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005123
5124 /* Now give the caller something to chew on */
5125 return read_expr(state, val);
5126}
5127
Eric Biederman83b991a2003-10-11 06:20:25 +00005128static int local_triple(struct compile_state *state,
5129 struct triple *func, struct triple *ins)
5130{
5131 int local = (ins->id & TRIPLE_FLAG_LOCAL);
5132#if 0
5133 if (!local) {
5134 fprintf(stderr, "global: ");
5135 display_triple(stderr, ins);
5136 }
5137#endif
5138 return local;
5139}
5140
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005141struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
5142 struct occurance *base_occurance)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005143{
5144 struct triple *nfunc;
5145 struct triple *nfirst, *ofirst;
5146 struct triple *new, *old;
5147
5148#if 0
5149 fprintf(stdout, "\n");
5150 loc(stdout, state, 0);
5151 fprintf(stdout, "\n__________ copy_func _________\n");
Eric Biederman83b991a2003-10-11 06:20:25 +00005152 display_func(stdout, ofunc);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005153 fprintf(stdout, "__________ copy_func _________ done\n\n");
5154#endif
5155
5156 /* Make a new copy of the old function */
5157 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
5158 nfirst = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005159 ofirst = old = RHS(ofunc, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005160 do {
5161 struct triple *new;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005162 struct occurance *occurance;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005163 int old_lhs, old_rhs;
5164 old_lhs = TRIPLE_LHS(old->sizes);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005165 old_rhs = TRIPLE_RHS(old->sizes);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005166 occurance = inline_occurance(state, base_occurance, old->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005167 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005168 occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005169 if (!triple_stores_block(state, new)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005170 memcpy(&new->u, &old->u, sizeof(new->u));
5171 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005172 if (!nfirst) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00005173 RHS(nfunc, 0) = nfirst = new;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005174 }
5175 else {
5176 insert_triple(state, nfirst, new);
5177 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005178 new->id |= TRIPLE_FLAG_FLATTENED;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005179
5180 /* During the copy remember new as user of old */
5181 use_triple(old, new);
5182
5183 /* Populate the return type if present */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005184 if (old == MISC(ofunc, 0)) {
5185 MISC(nfunc, 0) = new;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005186 }
Eric Biederman83b991a2003-10-11 06:20:25 +00005187 /* Remember which instructions are local */
5188 old->id |= TRIPLE_FLAG_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005189 old = old->next;
5190 } while(old != ofirst);
5191
5192 /* Make a second pass to fix up any unresolved references */
5193 old = ofirst;
5194 new = nfirst;
5195 do {
Eric Biederman0babc1c2003-05-09 02:39:00 +00005196 struct triple **oexpr, **nexpr;
5197 int count, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005198 /* Lookup where the copy is, to join pointers */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005199 count = TRIPLE_SIZE(old->sizes);
5200 for(i = 0; i < count; i++) {
5201 oexpr = &old->param[i];
5202 nexpr = &new->param[i];
Eric Biederman83b991a2003-10-11 06:20:25 +00005203 if (*oexpr && !*nexpr) {
5204 if (!local_triple(state, ofunc, *oexpr)) {
5205 *nexpr = *oexpr;
5206 }
5207 else if ((*oexpr)->use) {
5208 *nexpr = (*oexpr)->use->member;
5209 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005210 if (*nexpr == old) {
5211 internal_error(state, 0, "new == old?");
5212 }
5213 use_triple(*nexpr, new);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005214 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005215 if (!*nexpr && *oexpr) {
5216 internal_error(state, 0, "Could not copy %d\n", i);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005217 }
5218 }
5219 old = old->next;
5220 new = new->next;
5221 } while((old != ofirst) && (new != nfirst));
5222
5223 /* Make a third pass to cleanup the extra useses */
5224 old = ofirst;
5225 new = nfirst;
5226 do {
5227 unuse_triple(old, new);
Eric Biederman83b991a2003-10-11 06:20:25 +00005228 /* Forget which instructions are local */
5229 old->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005230 old = old->next;
5231 new = new->next;
5232 } while ((old != ofirst) && (new != nfirst));
5233 return nfunc;
5234}
5235
5236static struct triple *flatten_call(
5237 struct compile_state *state, struct triple *first, struct triple *ptr)
5238{
5239 /* Inline the function call */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005240 struct type *ptype;
5241 struct triple *ofunc, *nfunc, *nfirst, *param, *result;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005242 struct triple *end, *nend;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005243 int pvals, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005244
5245 /* Find the triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005246 ofunc = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005247 if (ofunc->op != OP_LIST) {
5248 internal_error(state, 0, "improper function");
5249 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005250 nfunc = copy_func(state, ofunc, ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005251 nfirst = RHS(nfunc, 0)->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005252 /* Prepend the parameter reading into the new function list */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005253 ptype = nfunc->type->right;
5254 param = RHS(nfunc, 0)->next;
5255 pvals = TRIPLE_RHS(ptr->sizes);
5256 for(i = 0; i < pvals; i++) {
5257 struct type *atype;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005258 struct triple *arg;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005259 atype = ptype;
5260 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
5261 atype = ptype->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005262 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005263 while((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
5264 param = param->next;
5265 }
5266 arg = RHS(ptr, i);
5267 flatten(state, nfirst, write_expr(state, param, arg));
5268 ptype = ptype->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005269 param = param->next;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005270 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005271 result = 0;
5272 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00005273 result = read_expr(state, MISC(nfunc,0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005274 }
5275#if 0
5276 fprintf(stdout, "\n");
5277 loc(stdout, state, 0);
5278 fprintf(stdout, "\n__________ flatten_call _________\n");
Eric Biederman83b991a2003-10-11 06:20:25 +00005279 display_func(stdout, nfunc);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005280 fprintf(stdout, "__________ flatten_call _________ done\n\n");
5281#endif
5282
5283 /* Get rid of the extra triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005284 nfirst = RHS(nfunc, 0)->next;
5285 free_triple(state, RHS(nfunc, 0));
5286 RHS(nfunc, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005287 free_triple(state, nfunc);
5288
5289 /* Append the new function list onto the return list */
5290 end = first->prev;
5291 nend = nfirst->prev;
5292 end->next = nfirst;
5293 nfirst->prev = end;
5294 nend->next = first;
5295 first->prev = nend;
5296
5297 return result;
5298}
5299
5300static struct triple *flatten(
5301 struct compile_state *state, struct triple *first, struct triple *ptr)
5302{
5303 struct triple *orig_ptr;
5304 if (!ptr)
5305 return 0;
5306 do {
5307 orig_ptr = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005308 /* Only flatten triples once */
5309 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
5310 return ptr;
5311 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005312 switch(ptr->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005313 case OP_COMMA:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005314 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5315 ptr = RHS(ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005316 break;
5317 case OP_VAL:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005318 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5319 return MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005320 break;
5321 case OP_LAND:
5322 ptr = flatten_land(state, first, ptr);
5323 break;
5324 case OP_LOR:
5325 ptr = flatten_lor(state, first, ptr);
5326 break;
5327 case OP_COND:
5328 ptr = flatten_cond(state, first, ptr);
5329 break;
5330 case OP_CALL:
5331 ptr = flatten_call(state, first, ptr);
5332 break;
5333 case OP_READ:
5334 case OP_LOAD:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005335 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5336 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005337 break;
5338 case OP_BRANCH:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005339 use_triple(TARG(ptr, 0), ptr);
5340 if (TRIPLE_RHS(ptr->sizes)) {
5341 use_triple(RHS(ptr, 0), ptr);
5342 if (ptr->next != ptr) {
5343 use_triple(ptr->next, ptr);
5344 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005345 }
5346 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005347 case OP_BLOBCONST:
Eric Biederman83b991a2003-10-11 06:20:25 +00005348 insert_triple(state, state->first, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005349 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00005350 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005351 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005352 use_triple(MISC(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005353 break;
5354 case OP_DEREF:
5355 /* Since OP_DEREF is just a marker delete it when I flatten it */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005356 ptr = RHS(ptr, 0);
5357 RHS(orig_ptr, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005358 free_triple(state, orig_ptr);
5359 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005360 case OP_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005361 {
5362 struct triple *base;
5363 base = RHS(ptr, 0);
Eric Biederman00443072003-06-24 12:34:45 +00005364 if (base->op == OP_DEREF) {
Eric Biederman03b59862003-06-24 14:27:37 +00005365 struct triple *left;
Eric Biederman00443072003-06-24 12:34:45 +00005366 ulong_t offset;
5367 offset = field_offset(state, base->type, ptr->u.field);
Eric Biederman03b59862003-06-24 14:27:37 +00005368 left = RHS(base, 0);
5369 ptr = triple(state, OP_ADD, left->type,
5370 read_expr(state, left),
Eric Biederman00443072003-06-24 12:34:45 +00005371 int_const(state, &ulong_type, offset));
5372 free_triple(state, base);
5373 }
5374 else if (base->op == OP_VAL_VEC) {
5375 base = flatten(state, first, base);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005376 ptr = struct_field(state, base, ptr->u.field);
5377 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005378 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005379 }
Eric Biederman8d9c1232003-06-17 08:42:17 +00005380 case OP_PIECE:
5381 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5382 use_triple(MISC(ptr, 0), ptr);
5383 use_triple(ptr, MISC(ptr, 0));
5384 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005385 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005386 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5387 use_triple(MISC(ptr, 0), ptr);
5388 break;
Eric Biederman83b991a2003-10-11 06:20:25 +00005389 case OP_SDECL:
5390 first = state->first;
5391 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5392 use_triple(MISC(ptr, 0), ptr);
5393 insert_triple(state, first, ptr);
5394 ptr->id |= TRIPLE_FLAG_FLATTENED;
5395 ptr->id &= ~TRIPLE_FLAG_LOCAL;
5396 return ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005397 case OP_ADECL:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005398 break;
5399 default:
5400 /* Flatten the easy cases we don't override */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005401 ptr = flatten_generic(state, first, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005402 break;
5403 }
5404 } while(ptr && (ptr != orig_ptr));
Eric Biederman0babc1c2003-05-09 02:39:00 +00005405 if (ptr) {
5406 insert_triple(state, first, ptr);
5407 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00005408 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005409 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005410 return ptr;
5411}
5412
5413static void release_expr(struct compile_state *state, struct triple *expr)
5414{
5415 struct triple *head;
5416 head = label(state);
5417 flatten(state, head, expr);
5418 while(head->next != head) {
5419 release_triple(state, head->next);
5420 }
5421 free_triple(state, head);
5422}
5423
5424static int replace_rhs_use(struct compile_state *state,
5425 struct triple *orig, struct triple *new, struct triple *use)
5426{
5427 struct triple **expr;
5428 int found;
5429 found = 0;
5430 expr = triple_rhs(state, use, 0);
5431 for(;expr; expr = triple_rhs(state, use, expr)) {
5432 if (*expr == orig) {
5433 *expr = new;
5434 found = 1;
5435 }
5436 }
5437 if (found) {
5438 unuse_triple(orig, use);
5439 use_triple(new, use);
5440 }
5441 return found;
5442}
5443
5444static int replace_lhs_use(struct compile_state *state,
5445 struct triple *orig, struct triple *new, struct triple *use)
5446{
5447 struct triple **expr;
5448 int found;
5449 found = 0;
5450 expr = triple_lhs(state, use, 0);
5451 for(;expr; expr = triple_lhs(state, use, expr)) {
5452 if (*expr == orig) {
5453 *expr = new;
5454 found = 1;
5455 }
5456 }
5457 if (found) {
5458 unuse_triple(orig, use);
5459 use_triple(new, use);
5460 }
5461 return found;
5462}
5463
5464static void propogate_use(struct compile_state *state,
5465 struct triple *orig, struct triple *new)
5466{
5467 struct triple_set *user, *next;
5468 for(user = orig->use; user; user = next) {
5469 struct triple *use;
5470 int found;
5471 next = user->next;
5472 use = user->member;
5473 found = 0;
5474 found |= replace_rhs_use(state, orig, new, use);
5475 found |= replace_lhs_use(state, orig, new, use);
5476 if (!found) {
5477 internal_error(state, use, "use without use");
5478 }
5479 }
5480 if (orig->use) {
5481 internal_error(state, orig, "used after propogate_use");
5482 }
5483}
5484
5485/*
5486 * Code generators
5487 * ===========================
5488 */
5489
5490static struct triple *mk_add_expr(
5491 struct compile_state *state, struct triple *left, struct triple *right)
5492{
5493 struct type *result_type;
5494 /* Put pointer operands on the left */
5495 if (is_pointer(right)) {
5496 struct triple *tmp;
5497 tmp = left;
5498 left = right;
5499 right = tmp;
5500 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005501 left = read_expr(state, left);
5502 right = read_expr(state, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005503 result_type = ptr_arithmetic_result(state, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005504 if (is_pointer(left)) {
5505 right = triple(state,
5506 is_signed(right->type)? OP_SMUL : OP_UMUL,
5507 &ulong_type,
5508 right,
5509 int_const(state, &ulong_type,
5510 size_of(state, left->type->left)));
5511 }
5512 return triple(state, OP_ADD, result_type, left, right);
5513}
5514
5515static struct triple *mk_sub_expr(
5516 struct compile_state *state, struct triple *left, struct triple *right)
5517{
5518 struct type *result_type;
5519 result_type = ptr_arithmetic_result(state, left, right);
5520 left = read_expr(state, left);
5521 right = read_expr(state, right);
5522 if (is_pointer(left)) {
5523 right = triple(state,
5524 is_signed(right->type)? OP_SMUL : OP_UMUL,
5525 &ulong_type,
5526 right,
5527 int_const(state, &ulong_type,
5528 size_of(state, left->type->left)));
5529 }
5530 return triple(state, OP_SUB, result_type, left, right);
5531}
5532
5533static struct triple *mk_pre_inc_expr(
5534 struct compile_state *state, struct triple *def)
5535{
5536 struct triple *val;
5537 lvalue(state, def);
5538 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
5539 return triple(state, OP_VAL, def->type,
5540 write_expr(state, def, val),
5541 val);
5542}
5543
5544static struct triple *mk_pre_dec_expr(
5545 struct compile_state *state, struct triple *def)
5546{
5547 struct triple *val;
5548 lvalue(state, def);
5549 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
5550 return triple(state, OP_VAL, def->type,
5551 write_expr(state, def, val),
5552 val);
5553}
5554
5555static struct triple *mk_post_inc_expr(
5556 struct compile_state *state, struct triple *def)
5557{
5558 struct triple *val;
5559 lvalue(state, def);
5560 val = read_expr(state, def);
5561 return triple(state, OP_VAL, def->type,
5562 write_expr(state, def,
5563 mk_add_expr(state, val, int_const(state, &int_type, 1)))
5564 , val);
5565}
5566
5567static struct triple *mk_post_dec_expr(
5568 struct compile_state *state, struct triple *def)
5569{
5570 struct triple *val;
5571 lvalue(state, def);
5572 val = read_expr(state, def);
5573 return triple(state, OP_VAL, def->type,
5574 write_expr(state, def,
5575 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
5576 , val);
5577}
5578
5579static struct triple *mk_subscript_expr(
5580 struct compile_state *state, struct triple *left, struct triple *right)
5581{
5582 left = read_expr(state, left);
5583 right = read_expr(state, right);
5584 if (!is_pointer(left) && !is_pointer(right)) {
5585 error(state, left, "subscripted value is not a pointer");
5586 }
5587 return mk_deref_expr(state, mk_add_expr(state, left, right));
5588}
5589
Eric Biedermane058a1e2003-07-12 01:21:31 +00005590static struct triple *mk_cast_expr(
5591 struct compile_state *state, struct type *type, struct triple *expr)
5592{
5593 struct triple *def;
5594 def = read_expr(state, expr);
5595 def = triple(state, OP_COPY, type, def, 0);
5596 return def;
5597}
5598
Eric Biedermanb138ac82003-04-22 18:44:01 +00005599/*
5600 * Compile time evaluation
5601 * ===========================
5602 */
5603static int is_const(struct triple *ins)
5604{
5605 return IS_CONST_OP(ins->op);
5606}
5607
Eric Biederman83b991a2003-10-11 06:20:25 +00005608static int is_simple_const(struct triple *ins)
5609{
5610 return IS_CONST_OP(ins->op) && (ins->op != OP_ADDRCONST);
5611}
5612
Eric Biedermanb138ac82003-04-22 18:44:01 +00005613static int constants_equal(struct compile_state *state,
5614 struct triple *left, struct triple *right)
5615{
5616 int equal;
5617 if (!is_const(left) || !is_const(right)) {
5618 equal = 0;
5619 }
5620 else if (left->op != right->op) {
5621 equal = 0;
5622 }
5623 else if (!equiv_types(left->type, right->type)) {
5624 equal = 0;
5625 }
5626 else {
5627 equal = 0;
5628 switch(left->op) {
5629 case OP_INTCONST:
5630 if (left->u.cval == right->u.cval) {
5631 equal = 1;
5632 }
5633 break;
5634 case OP_BLOBCONST:
5635 {
5636 size_t lsize, rsize;
5637 lsize = size_of(state, left->type);
5638 rsize = size_of(state, right->type);
5639 if (lsize != rsize) {
5640 break;
5641 }
5642 if (memcmp(left->u.blob, right->u.blob, lsize) == 0) {
5643 equal = 1;
5644 }
5645 break;
5646 }
5647 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005648 if ((MISC(left, 0) == MISC(right, 0)) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00005649 (left->u.cval == right->u.cval)) {
5650 equal = 1;
5651 }
5652 break;
5653 default:
5654 internal_error(state, left, "uknown constant type");
5655 break;
5656 }
5657 }
5658 return equal;
5659}
5660
5661static int is_zero(struct triple *ins)
5662{
5663 return is_const(ins) && (ins->u.cval == 0);
5664}
5665
5666static int is_one(struct triple *ins)
5667{
5668 return is_const(ins) && (ins->u.cval == 1);
5669}
5670
Eric Biederman530b5192003-07-01 10:05:30 +00005671static long_t bit_count(ulong_t value)
5672{
5673 int count;
5674 int i;
5675 count = 0;
5676 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
5677 ulong_t mask;
5678 mask = 1;
5679 mask <<= i;
5680 if (value & mask) {
5681 count++;
5682 }
5683 }
5684 return count;
5685
5686}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005687static long_t bsr(ulong_t value)
5688{
5689 int i;
5690 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
5691 ulong_t mask;
5692 mask = 1;
5693 mask <<= i;
5694 if (value & mask) {
5695 return i;
5696 }
5697 }
5698 return -1;
5699}
5700
5701static long_t bsf(ulong_t value)
5702{
5703 int i;
5704 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
5705 ulong_t mask;
5706 mask = 1;
5707 mask <<= 1;
5708 if (value & mask) {
5709 return i;
5710 }
5711 }
5712 return -1;
5713}
5714
5715static long_t log2(ulong_t value)
5716{
5717 return bsr(value);
5718}
5719
5720static long_t tlog2(struct triple *ins)
5721{
5722 return log2(ins->u.cval);
5723}
5724
5725static int is_pow2(struct triple *ins)
5726{
5727 ulong_t value, mask;
5728 long_t log;
5729 if (!is_const(ins)) {
5730 return 0;
5731 }
5732 value = ins->u.cval;
5733 log = log2(value);
5734 if (log == -1) {
5735 return 0;
5736 }
5737 mask = 1;
5738 mask <<= log;
5739 return ((value & mask) == value);
5740}
5741
5742static ulong_t read_const(struct compile_state *state,
5743 struct triple *ins, struct triple **expr)
5744{
5745 struct triple *rhs;
5746 rhs = *expr;
5747 switch(rhs->type->type &TYPE_MASK) {
5748 case TYPE_CHAR:
5749 case TYPE_SHORT:
5750 case TYPE_INT:
5751 case TYPE_LONG:
5752 case TYPE_UCHAR:
5753 case TYPE_USHORT:
5754 case TYPE_UINT:
5755 case TYPE_ULONG:
5756 case TYPE_POINTER:
5757 break;
5758 default:
5759 internal_error(state, rhs, "bad type to read_const\n");
5760 break;
5761 }
Eric Biederman83b991a2003-10-11 06:20:25 +00005762 if (!is_simple_const(rhs)) {
5763 internal_error(state, rhs, "bad op to read_const\n");
5764 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005765 return rhs->u.cval;
5766}
5767
5768static long_t read_sconst(struct triple *ins, struct triple **expr)
5769{
5770 struct triple *rhs;
5771 rhs = *expr;
5772 return (long_t)(rhs->u.cval);
5773}
5774
5775static void unuse_rhs(struct compile_state *state, struct triple *ins)
5776{
5777 struct triple **expr;
5778 expr = triple_rhs(state, ins, 0);
5779 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00005780 if (*expr) {
5781 unuse_triple(*expr, ins);
5782 *expr = 0;
5783 }
5784 }
5785}
5786
5787static void unuse_lhs(struct compile_state *state, struct triple *ins)
5788{
5789 struct triple **expr;
5790 expr = triple_lhs(state, ins, 0);
5791 for(;expr;expr = triple_lhs(state, ins, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005792 unuse_triple(*expr, ins);
5793 *expr = 0;
5794 }
5795}
Eric Biederman0babc1c2003-05-09 02:39:00 +00005796
Eric Biedermanb138ac82003-04-22 18:44:01 +00005797static void check_lhs(struct compile_state *state, struct triple *ins)
5798{
5799 struct triple **expr;
5800 expr = triple_lhs(state, ins, 0);
5801 for(;expr;expr = triple_lhs(state, ins, expr)) {
5802 internal_error(state, ins, "unexpected lhs");
5803 }
5804
5805}
5806static void check_targ(struct compile_state *state, struct triple *ins)
5807{
5808 struct triple **expr;
5809 expr = triple_targ(state, ins, 0);
5810 for(;expr;expr = triple_targ(state, ins, expr)) {
5811 internal_error(state, ins, "unexpected targ");
5812 }
5813}
5814
5815static void wipe_ins(struct compile_state *state, struct triple *ins)
5816{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005817 /* Becareful which instructions you replace the wiped
5818 * instruction with, as there are not enough slots
5819 * in all instructions to hold all others.
5820 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005821 check_targ(state, ins);
5822 unuse_rhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005823 unuse_lhs(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005824}
5825
5826static void mkcopy(struct compile_state *state,
5827 struct triple *ins, struct triple *rhs)
5828{
Eric Biederman83b991a2003-10-11 06:20:25 +00005829 struct block *block;
5830 block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005831 wipe_ins(state, ins);
5832 ins->op = OP_COPY;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005833 ins->sizes = TRIPLE_SIZES(0, 1, 0, 0);
Eric Biederman83b991a2003-10-11 06:20:25 +00005834 ins->u.block = block;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005835 RHS(ins, 0) = rhs;
5836 use_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005837}
5838
5839static void mkconst(struct compile_state *state,
5840 struct triple *ins, ulong_t value)
5841{
5842 if (!is_integral(ins) && !is_pointer(ins)) {
5843 internal_error(state, ins, "unknown type to make constant\n");
5844 }
5845 wipe_ins(state, ins);
5846 ins->op = OP_INTCONST;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005847 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005848 ins->u.cval = value;
5849}
5850
5851static void mkaddr_const(struct compile_state *state,
5852 struct triple *ins, struct triple *sdecl, ulong_t value)
5853{
Eric Biederman830c9882003-07-04 00:27:33 +00005854 if (sdecl->op != OP_SDECL) {
5855 internal_error(state, ins, "bad base for addrconst");
5856 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005857 wipe_ins(state, ins);
5858 ins->op = OP_ADDRCONST;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005859 ins->sizes = TRIPLE_SIZES(0, 0, 1, 0);
5860 MISC(ins, 0) = sdecl;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005861 ins->u.cval = value;
5862 use_triple(sdecl, ins);
5863}
5864
Eric Biederman0babc1c2003-05-09 02:39:00 +00005865/* Transform multicomponent variables into simple register variables */
5866static void flatten_structures(struct compile_state *state)
5867{
5868 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +00005869 first = state->first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005870 ins = first;
5871 /* Pass one expand structure values into valvecs.
5872 */
5873 ins = first;
5874 do {
5875 struct triple *next;
5876 next = ins->next;
5877 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5878 if (ins->op == OP_VAL_VEC) {
5879 /* Do nothing */
5880 }
5881 else if ((ins->op == OP_LOAD) || (ins->op == OP_READ)) {
5882 struct triple *def, **vector;
5883 struct type *tptr;
5884 int op;
5885 ulong_t i;
5886
5887 op = ins->op;
5888 def = RHS(ins, 0);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005889 get_occurance(ins->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005890 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005891 ins->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005892
5893 vector = &RHS(next, 0);
5894 tptr = next->type->left;
5895 for(i = 0; i < next->type->elements; i++) {
5896 struct triple *sfield;
5897 struct type *mtype;
5898 mtype = tptr;
5899 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5900 mtype = mtype->left;
5901 }
5902 sfield = deref_field(state, def, mtype->field_ident);
5903
5904 vector[i] = triple(
5905 state, op, mtype, sfield, 0);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005906 put_occurance(vector[i]->occurance);
5907 get_occurance(next->occurance);
5908 vector[i]->occurance = next->occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005909 tptr = tptr->right;
5910 }
5911 propogate_use(state, ins, next);
5912 flatten(state, ins, next);
5913 free_triple(state, ins);
5914 }
5915 else if ((ins->op == OP_STORE) || (ins->op == OP_WRITE)) {
5916 struct triple *src, *dst, **vector;
5917 struct type *tptr;
5918 int op;
5919 ulong_t i;
5920
5921 op = ins->op;
Eric Biederman530b5192003-07-01 10:05:30 +00005922 src = RHS(ins, 1);
5923 dst = RHS(ins, 0);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005924 get_occurance(ins->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005925 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005926 ins->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005927
5928 vector = &RHS(next, 0);
5929 tptr = next->type->left;
5930 for(i = 0; i < ins->type->elements; i++) {
5931 struct triple *dfield, *sfield;
5932 struct type *mtype;
5933 mtype = tptr;
5934 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5935 mtype = mtype->left;
5936 }
5937 sfield = deref_field(state, src, mtype->field_ident);
5938 dfield = deref_field(state, dst, mtype->field_ident);
5939 vector[i] = triple(
5940 state, op, mtype, dfield, sfield);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005941 put_occurance(vector[i]->occurance);
5942 get_occurance(next->occurance);
5943 vector[i]->occurance = next->occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005944 tptr = tptr->right;
5945 }
5946 propogate_use(state, ins, next);
5947 flatten(state, ins, next);
5948 free_triple(state, ins);
5949 }
5950 }
5951 ins = next;
5952 } while(ins != first);
5953 /* Pass two flatten the valvecs.
5954 */
5955 ins = first;
5956 do {
5957 struct triple *next;
5958 next = ins->next;
5959 if (ins->op == OP_VAL_VEC) {
5960 release_triple(state, ins);
5961 }
5962 ins = next;
5963 } while(ins != first);
5964 /* Pass three verify the state and set ->id to 0.
5965 */
5966 ins = first;
5967 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005968 ins->id &= ~TRIPLE_FLAG_FLATTENED;
Eric Biederman830c9882003-07-04 00:27:33 +00005969 if ((ins->op != OP_BLOBCONST) && (ins->op != OP_SDECL) &&
5970 ((ins->type->type & TYPE_MASK) == TYPE_STRUCT)) {
Eric Biederman00443072003-06-24 12:34:45 +00005971 internal_error(state, ins, "STRUCT_TYPE remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00005972 }
5973 if (ins->op == OP_DOT) {
Eric Biederman00443072003-06-24 12:34:45 +00005974 internal_error(state, ins, "OP_DOT remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00005975 }
5976 if (ins->op == OP_VAL_VEC) {
Eric Biederman00443072003-06-24 12:34:45 +00005977 internal_error(state, ins, "OP_VAL_VEC remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00005978 }
5979 ins = ins->next;
5980 } while(ins != first);
5981}
5982
Eric Biedermanb138ac82003-04-22 18:44:01 +00005983/* For those operations that cannot be simplified */
5984static void simplify_noop(struct compile_state *state, struct triple *ins)
5985{
5986 return;
5987}
5988
5989static void simplify_smul(struct compile_state *state, struct triple *ins)
5990{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005991 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005992 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005993 tmp = RHS(ins, 0);
5994 RHS(ins, 0) = RHS(ins, 1);
5995 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005996 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005997 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005998 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005999 left = read_sconst(ins, &RHS(ins, 0));
6000 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006001 mkconst(state, ins, left * right);
6002 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006003 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006004 mkconst(state, ins, 0);
6005 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006006 else if (is_one(RHS(ins, 1))) {
6007 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006008 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006009 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006010 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006011 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006012 ins->op = OP_SL;
6013 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006014 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006015 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006016 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006017 }
6018}
6019
6020static void simplify_umul(struct compile_state *state, struct triple *ins)
6021{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006022 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006023 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006024 tmp = RHS(ins, 0);
6025 RHS(ins, 0) = RHS(ins, 1);
6026 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006027 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006028 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006029 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006030 left = read_const(state, ins, &RHS(ins, 0));
6031 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006032 mkconst(state, ins, left * right);
6033 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006034 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006035 mkconst(state, ins, 0);
6036 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006037 else if (is_one(RHS(ins, 1))) {
6038 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006039 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006040 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006041 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006042 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006043 ins->op = OP_SL;
6044 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006045 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006046 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006047 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006048 }
6049}
6050
6051static void simplify_sdiv(struct compile_state *state, struct triple *ins)
6052{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006053 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006054 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006055 left = read_sconst(ins, &RHS(ins, 0));
6056 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006057 mkconst(state, ins, left / right);
6058 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006059 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006060 mkconst(state, ins, 0);
6061 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006062 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006063 error(state, ins, "division by zero");
6064 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006065 else if (is_one(RHS(ins, 1))) {
6066 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006067 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006068 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006069 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006070 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006071 ins->op = OP_SSR;
6072 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006073 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006074 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006075 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006076 }
6077}
6078
6079static void simplify_udiv(struct compile_state *state, struct triple *ins)
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 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006087 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006088 mkconst(state, ins, 0);
6089 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006090 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006091 error(state, ins, "division by zero");
6092 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006093 else if (is_one(RHS(ins, 1))) {
6094 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006095 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006096 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006097 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006098 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006099 ins->op = OP_USR;
6100 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006101 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006102 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006103 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006104 }
6105}
6106
6107static void simplify_smod(struct compile_state *state, struct triple *ins)
6108{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006109 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006110 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006111 left = read_const(state, ins, &RHS(ins, 0));
6112 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006113 mkconst(state, ins, left % right);
6114 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006115 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006116 mkconst(state, ins, 0);
6117 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006118 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006119 error(state, ins, "division by zero");
6120 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006121 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006122 mkconst(state, ins, 0);
6123 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006124 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006125 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006126 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006127 ins->op = OP_AND;
6128 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006129 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006130 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006131 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006132 }
6133}
Eric Biederman83b991a2003-10-11 06:20:25 +00006134
Eric Biedermanb138ac82003-04-22 18:44:01 +00006135static void simplify_umod(struct compile_state *state, struct triple *ins)
6136{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006137 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006138 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006139 left = read_const(state, ins, &RHS(ins, 0));
6140 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006141 mkconst(state, ins, left % right);
6142 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006143 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006144 mkconst(state, ins, 0);
6145 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006146 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006147 error(state, ins, "division by zero");
6148 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006149 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006150 mkconst(state, ins, 0);
6151 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006152 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006153 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006154 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006155 ins->op = OP_AND;
6156 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006157 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006158 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006159 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006160 }
6161}
6162
6163static void simplify_add(struct compile_state *state, struct triple *ins)
6164{
6165 /* start with the pointer on the left */
Eric Biederman0babc1c2003-05-09 02:39:00 +00006166 if (is_pointer(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006167 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006168 tmp = RHS(ins, 0);
6169 RHS(ins, 0) = RHS(ins, 1);
6170 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006171 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006172 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00006173 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006174 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006175 left = read_const(state, ins, &RHS(ins, 0));
6176 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006177 mkconst(state, ins, left + right);
6178 }
Eric Biederman530b5192003-07-01 10:05:30 +00006179 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006180 struct triple *sdecl;
6181 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006182 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006183 left = RHS(ins, 0)->u.cval;
6184 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006185 mkaddr_const(state, ins, sdecl, left + right);
6186 }
Eric Biederman530b5192003-07-01 10:05:30 +00006187 else {
6188 internal_warning(state, ins, "Optimize me!");
6189 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006190 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006191 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006192 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006193 tmp = RHS(ins, 1);
6194 RHS(ins, 1) = RHS(ins, 0);
6195 RHS(ins, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006196 }
6197}
6198
6199static void simplify_sub(struct compile_state *state, struct triple *ins)
6200{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006201 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00006202 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006203 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006204 left = read_const(state, ins, &RHS(ins, 0));
6205 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006206 mkconst(state, ins, left - right);
6207 }
Eric Biederman530b5192003-07-01 10:05:30 +00006208 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006209 struct triple *sdecl;
6210 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006211 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006212 left = RHS(ins, 0)->u.cval;
6213 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006214 mkaddr_const(state, ins, sdecl, left - right);
6215 }
Eric Biederman530b5192003-07-01 10:05:30 +00006216 else {
6217 internal_warning(state, ins, "Optimize me!");
6218 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006219 }
6220}
6221
6222static void simplify_sl(struct compile_state *state, struct triple *ins)
6223{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006224 if (is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006225 ulong_t right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006226 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006227 if (right >= (size_of(state, ins->type)*8)) {
6228 warning(state, ins, "left shift count >= width of type");
6229 }
6230 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006231 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006232 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006233 left = read_const(state, ins, &RHS(ins, 0));
6234 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006235 mkconst(state, ins, left << right);
6236 }
6237}
6238
6239static void simplify_usr(struct compile_state *state, struct triple *ins)
6240{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006241 if (is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006242 ulong_t right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006243 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006244 if (right >= (size_of(state, ins->type)*8)) {
6245 warning(state, ins, "right shift count >= width of type");
6246 }
6247 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006248 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006249 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006250 left = read_const(state, ins, &RHS(ins, 0));
6251 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006252 mkconst(state, ins, left >> right);
6253 }
6254}
6255
6256static void simplify_ssr(struct compile_state *state, struct triple *ins)
6257{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006258 if (is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006259 ulong_t right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006260 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006261 if (right >= (size_of(state, ins->type)*8)) {
6262 warning(state, ins, "right shift count >= width of type");
6263 }
6264 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006265 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006266 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006267 left = read_sconst(ins, &RHS(ins, 0));
6268 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006269 mkconst(state, ins, left >> right);
6270 }
6271}
6272
6273static void simplify_and(struct compile_state *state, struct triple *ins)
6274{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006275 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006276 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006277 left = read_const(state, ins, &RHS(ins, 0));
6278 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006279 mkconst(state, ins, left & right);
6280 }
6281}
6282
6283static void simplify_or(struct compile_state *state, struct triple *ins)
6284{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006285 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006286 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006287 left = read_const(state, ins, &RHS(ins, 0));
6288 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006289 mkconst(state, ins, left | right);
6290 }
6291}
6292
6293static void simplify_xor(struct compile_state *state, struct triple *ins)
6294{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006295 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006296 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006297 left = read_const(state, ins, &RHS(ins, 0));
6298 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006299 mkconst(state, ins, left ^ right);
6300 }
6301}
6302
6303static void simplify_pos(struct compile_state *state, struct triple *ins)
6304{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006305 if (is_const(RHS(ins, 0))) {
6306 mkconst(state, ins, RHS(ins, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006307 }
6308 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006309 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006310 }
6311}
6312
6313static void simplify_neg(struct compile_state *state, struct triple *ins)
6314{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006315 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006316 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006317 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006318 mkconst(state, ins, -left);
6319 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006320 else if (RHS(ins, 0)->op == OP_NEG) {
6321 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006322 }
6323}
6324
6325static void simplify_invert(struct compile_state *state, struct triple *ins)
6326{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006327 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006328 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006329 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006330 mkconst(state, ins, ~left);
6331 }
6332}
6333
6334static void simplify_eq(struct compile_state *state, struct triple *ins)
6335{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006336 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006337 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006338 left = read_const(state, ins, &RHS(ins, 0));
6339 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006340 mkconst(state, ins, left == right);
6341 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006342 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006343 mkconst(state, ins, 1);
6344 }
6345}
6346
6347static void simplify_noteq(struct compile_state *state, struct triple *ins)
6348{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006349 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006350 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006351 left = read_const(state, ins, &RHS(ins, 0));
6352 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006353 mkconst(state, ins, left != right);
6354 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006355 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006356 mkconst(state, ins, 0);
6357 }
6358}
6359
6360static void simplify_sless(struct compile_state *state, struct triple *ins)
6361{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006362 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006363 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006364 left = read_sconst(ins, &RHS(ins, 0));
6365 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006366 mkconst(state, ins, left < right);
6367 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006368 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006369 mkconst(state, ins, 0);
6370 }
6371}
6372
6373static void simplify_uless(struct compile_state *state, struct triple *ins)
6374{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006375 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006376 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006377 left = read_const(state, ins, &RHS(ins, 0));
6378 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006379 mkconst(state, ins, left < right);
6380 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006381 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006382 mkconst(state, ins, 1);
6383 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006384 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006385 mkconst(state, ins, 0);
6386 }
6387}
6388
6389static void simplify_smore(struct compile_state *state, struct triple *ins)
6390{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006391 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006392 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006393 left = read_sconst(ins, &RHS(ins, 0));
6394 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006395 mkconst(state, ins, left > right);
6396 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006397 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006398 mkconst(state, ins, 0);
6399 }
6400}
6401
6402static void simplify_umore(struct compile_state *state, struct triple *ins)
6403{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006404 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006405 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006406 left = read_const(state, ins, &RHS(ins, 0));
6407 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006408 mkconst(state, ins, left > right);
6409 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006410 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006411 mkconst(state, ins, 1);
6412 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006413 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006414 mkconst(state, ins, 0);
6415 }
6416}
6417
6418
6419static void simplify_slesseq(struct compile_state *state, struct triple *ins)
6420{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006421 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006422 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006423 left = read_sconst(ins, &RHS(ins, 0));
6424 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006425 mkconst(state, ins, left <= right);
6426 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006427 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006428 mkconst(state, ins, 1);
6429 }
6430}
6431
6432static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
6433{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006434 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006435 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006436 left = read_const(state, ins, &RHS(ins, 0));
6437 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006438 mkconst(state, ins, left <= right);
6439 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006440 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006441 mkconst(state, ins, 1);
6442 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006443 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006444 mkconst(state, ins, 1);
6445 }
6446}
6447
6448static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
6449{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006450 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006451 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006452 left = read_sconst(ins, &RHS(ins, 0));
6453 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006454 mkconst(state, ins, left >= right);
6455 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006456 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006457 mkconst(state, ins, 1);
6458 }
6459}
6460
6461static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
6462{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006463 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006464 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006465 left = read_const(state, ins, &RHS(ins, 0));
6466 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006467 mkconst(state, ins, left >= right);
6468 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006469 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006470 mkconst(state, ins, 1);
6471 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006472 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006473 mkconst(state, ins, 1);
6474 }
6475}
6476
6477static void simplify_lfalse(struct compile_state *state, struct triple *ins)
6478{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006479 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006480 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006481 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006482 mkconst(state, ins, left == 0);
6483 }
6484 /* Otherwise if I am the only user... */
Eric Biederman83b991a2003-10-11 06:20:25 +00006485 else if ((RHS(ins, 0)->use) &&
6486 (RHS(ins, 0)->use->member == ins) && (RHS(ins, 0)->use->next == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006487 int need_copy = 1;
6488 /* Invert a boolean operation */
Eric Biederman0babc1c2003-05-09 02:39:00 +00006489 switch(RHS(ins, 0)->op) {
6490 case OP_LTRUE: RHS(ins, 0)->op = OP_LFALSE; break;
6491 case OP_LFALSE: RHS(ins, 0)->op = OP_LTRUE; break;
6492 case OP_EQ: RHS(ins, 0)->op = OP_NOTEQ; break;
6493 case OP_NOTEQ: RHS(ins, 0)->op = OP_EQ; break;
6494 case OP_SLESS: RHS(ins, 0)->op = OP_SMOREEQ; break;
6495 case OP_ULESS: RHS(ins, 0)->op = OP_UMOREEQ; break;
6496 case OP_SMORE: RHS(ins, 0)->op = OP_SLESSEQ; break;
6497 case OP_UMORE: RHS(ins, 0)->op = OP_ULESSEQ; break;
6498 case OP_SLESSEQ: RHS(ins, 0)->op = OP_SMORE; break;
6499 case OP_ULESSEQ: RHS(ins, 0)->op = OP_UMORE; break;
6500 case OP_SMOREEQ: RHS(ins, 0)->op = OP_SLESS; break;
6501 case OP_UMOREEQ: RHS(ins, 0)->op = OP_ULESS; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006502 default:
6503 need_copy = 0;
6504 break;
6505 }
6506 if (need_copy) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006507 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006508 }
6509 }
6510}
6511
6512static void simplify_ltrue (struct compile_state *state, struct triple *ins)
6513{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006514 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006515 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006516 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006517 mkconst(state, ins, left != 0);
6518 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006519 else switch(RHS(ins, 0)->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006520 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
6521 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
6522 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
Eric Biederman0babc1c2003-05-09 02:39:00 +00006523 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006524 }
6525
6526}
6527
6528static void simplify_copy(struct compile_state *state, struct triple *ins)
6529{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006530 if (is_const(RHS(ins, 0))) {
6531 switch(RHS(ins, 0)->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006532 case OP_INTCONST:
6533 {
6534 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006535 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006536 mkconst(state, ins, left);
6537 break;
6538 }
6539 case OP_ADDRCONST:
6540 {
6541 struct triple *sdecl;
6542 ulong_t offset;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006543 sdecl = MISC(RHS(ins, 0), 0);
6544 offset = RHS(ins, 0)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006545 mkaddr_const(state, ins, sdecl, offset);
6546 break;
6547 }
6548 default:
6549 internal_error(state, ins, "uknown constant");
6550 break;
6551 }
6552 }
6553}
6554
Eric Biederman83b991a2003-10-11 06:20:25 +00006555static int phi_present(struct block *block)
Eric Biederman530b5192003-07-01 10:05:30 +00006556{
6557 struct triple *ptr;
6558 if (!block) {
6559 return 0;
6560 }
6561 ptr = block->first;
6562 do {
6563 if (ptr->op == OP_PHI) {
6564 return 1;
6565 }
6566 ptr = ptr->next;
6567 } while(ptr != block->last);
6568 return 0;
6569}
6570
Eric Biederman83b991a2003-10-11 06:20:25 +00006571static int phi_dependency(struct block *block)
6572{
6573 /* A block has a phi dependency if a phi function
6574 * depends on that block to exist, and makes a block
6575 * that is otherwise useless unsafe to remove.
6576 */
6577 if (block && (
6578 phi_present(block->left) ||
6579 phi_present(block->right))) {
6580 return 1;
6581 }
6582 return 0;
6583}
6584
6585static struct triple *branch_target(struct compile_state *state, struct triple *ins)
6586{
6587 struct triple *targ;
6588 targ = TARG(ins, 0);
6589 /* During scc_transform temporary triples are allocated that
6590 * loop back onto themselves. If I see one don't advance the
6591 * target.
6592 */
6593 while(triple_is_structural(state, targ) && (targ->next != targ)) {
6594 targ = targ->next;
6595 }
6596 return targ;
6597}
6598
6599
6600static void simplify_branch(struct compile_state *state, struct triple *ins)
6601{
6602 int simplified;
6603 if (ins->op != OP_BRANCH) {
6604 internal_error(state, ins, "not branch");
6605 }
6606 if (ins->use != 0) {
6607 internal_error(state, ins, "branch use");
6608 }
6609 /* The challenge here with simplify branch is that I need to
6610 * make modifications to the control flow graph as well
6611 * as to the branch instruction itself. That is handled
6612 * by rebuilding the basic blocks after simplify all is called.
6613 */
6614
6615 /* If we have a branch to an unconditional branch update
6616 * our target. But watch out for dependencies from phi
6617 * functions.
6618 */
6619 do {
6620 struct triple *targ;
6621 simplified = 0;
6622 targ = branch_target(state, ins);
6623 if ((targ != ins) && triple_is_uncond_branch(state, targ)) {
6624 if (!phi_dependency(targ->u.block))
6625 {
6626 unuse_triple(TARG(ins, 0), ins);
6627 TARG(ins, 0) = TARG(targ, 0);
6628 use_triple(TARG(ins, 0), ins);
6629 simplified = 1;
6630 }
6631 }
6632 } while(simplified);
6633
6634 /* If we have a conditional branch with a constant condition
6635 * make it an unconditional branch.
6636 */
6637 if (TRIPLE_RHS(ins->sizes) && is_const(RHS(ins, 0))) {
6638 struct triple *targ;
6639 ulong_t value;
6640 value = read_const(state, ins, &RHS(ins, 0));
6641 unuse_triple(RHS(ins, 0), ins);
6642 targ = TARG(ins, 0);
6643 ins->sizes = TRIPLE_SIZES(0, 0, 0, 1);
6644 if (value) {
6645 unuse_triple(ins->next, ins);
6646 TARG(ins, 0) = targ;
6647 }
6648 else {
6649 unuse_triple(targ, ins);
6650 TARG(ins, 0) = ins->next;
6651 }
6652 }
6653
6654 /* If we have an unconditional branch to the next instruction
6655 * make it a noop.
6656 */
6657 if (TARG(ins, 0) == ins->next) {
6658 unuse_triple(ins->next, ins);
6659 if (TRIPLE_RHS(ins->sizes)) {
6660 unuse_triple(RHS(ins, 0), ins);
6661 unuse_triple(ins->next, ins);
6662 }
6663 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
6664 ins->op = OP_NOOP;
6665 if (ins->use) {
6666 internal_error(state, ins, "noop use != 0");
6667 }
6668 }
6669}
6670
Eric Biederman530b5192003-07-01 10:05:30 +00006671static void simplify_label(struct compile_state *state, struct triple *ins)
6672{
Eric Biederman83b991a2003-10-11 06:20:25 +00006673 struct triple *first;
6674 first = state->first;
6675 /* Ignore volatile labels */
6676 if (!triple_is_pure(state, ins, ins->id)) {
Eric Biederman530b5192003-07-01 10:05:30 +00006677 return;
6678 }
6679 if (ins->use == 0) {
6680 ins->op = OP_NOOP;
6681 }
6682 else if (ins->prev->op == OP_LABEL) {
Eric Biederman530b5192003-07-01 10:05:30 +00006683 /* In general it is not safe to merge one label that
6684 * imediately follows another. The problem is that the empty
6685 * looking block may have phi functions that depend on it.
6686 */
Eric Biederman83b991a2003-10-11 06:20:25 +00006687 if (!phi_dependency(ins->prev->u.block)) {
Eric Biederman530b5192003-07-01 10:05:30 +00006688 struct triple_set *user, *next;
6689 ins->op = OP_NOOP;
6690 for(user = ins->use; user; user = next) {
6691 struct triple *use;
6692 next = user->next;
6693 use = user->member;
6694 if (TARG(use, 0) == ins) {
6695 TARG(use, 0) = ins->prev;
6696 unuse_triple(ins, use);
6697 use_triple(ins->prev, use);
6698 }
6699 }
6700 if (ins->use) {
6701 internal_error(state, ins, "noop use != 0");
6702 }
6703 }
6704 }
6705}
6706
Eric Biedermanb138ac82003-04-22 18:44:01 +00006707static void simplify_phi(struct compile_state *state, struct triple *ins)
6708{
Eric Biederman83b991a2003-10-11 06:20:25 +00006709 struct triple **slot;
6710 struct triple *value;
6711 int zrhs, i;
6712 ulong_t cvalue;
6713 slot = &RHS(ins, 0);
6714 zrhs = TRIPLE_RHS(ins->sizes);
6715 if (zrhs == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006716 return;
6717 }
Eric Biederman83b991a2003-10-11 06:20:25 +00006718 /* See if all of the rhs members of a phi have the same value */
6719 if (slot[0] && is_simple_const(slot[0])) {
6720 cvalue = read_const(state, ins, &slot[0]);
6721 for(i = 1; i < zrhs; i++) {
6722 if ( !slot[i] ||
6723 !is_simple_const(slot[i]) ||
6724 (cvalue != read_const(state, ins, &slot[i]))) {
6725 break;
6726 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006727 }
Eric Biederman83b991a2003-10-11 06:20:25 +00006728 if (i == zrhs) {
6729 mkconst(state, ins, cvalue);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006730 return;
6731 }
6732 }
Eric Biederman83b991a2003-10-11 06:20:25 +00006733
6734 /* See if all of rhs members of a phi are the same */
6735 value = slot[0];
6736 for(i = 1; i < zrhs; i++) {
6737 if (slot[i] != value) {
6738 break;
6739 }
6740 }
6741 if (i == zrhs) {
6742 /* If the phi has a single value just copy it */
6743 mkcopy(state, ins, value);
6744 return;
6745 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006746}
6747
6748
6749static void simplify_bsf(struct compile_state *state, struct triple *ins)
6750{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006751 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006752 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006753 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006754 mkconst(state, ins, bsf(left));
6755 }
6756}
6757
6758static void simplify_bsr(struct compile_state *state, struct triple *ins)
6759{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006760 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006761 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006762 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006763 mkconst(state, ins, bsr(left));
6764 }
6765}
6766
6767
6768typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
6769static const simplify_t table_simplify[] = {
Eric Biederman530b5192003-07-01 10:05:30 +00006770#if 1
6771#define simplify_sdivt simplify_noop
6772#define simplify_udivt simplify_noop
6773#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00006774#if 0
6775#define simplify_smul simplify_noop
6776#define simplify_umul simplify_noop
6777#define simplify_sdiv simplify_noop
6778#define simplify_udiv simplify_noop
6779#define simplify_smod simplify_noop
6780#define simplify_umod simplify_noop
6781#endif
6782#if 0
6783#define simplify_add simplify_noop
6784#define simplify_sub simplify_noop
6785#endif
6786#if 0
6787#define simplify_sl simplify_noop
6788#define simplify_usr simplify_noop
6789#define simplify_ssr simplify_noop
6790#endif
6791#if 0
6792#define simplify_and simplify_noop
6793#define simplify_xor simplify_noop
6794#define simplify_or simplify_noop
6795#endif
6796#if 0
6797#define simplify_pos simplify_noop
6798#define simplify_neg simplify_noop
6799#define simplify_invert simplify_noop
6800#endif
6801
6802#if 0
6803#define simplify_eq simplify_noop
6804#define simplify_noteq simplify_noop
6805#endif
6806#if 0
6807#define simplify_sless simplify_noop
6808#define simplify_uless simplify_noop
6809#define simplify_smore simplify_noop
6810#define simplify_umore simplify_noop
6811#endif
6812#if 0
6813#define simplify_slesseq simplify_noop
6814#define simplify_ulesseq simplify_noop
6815#define simplify_smoreeq simplify_noop
6816#define simplify_umoreeq simplify_noop
6817#endif
6818#if 0
6819#define simplify_lfalse simplify_noop
6820#endif
6821#if 0
6822#define simplify_ltrue simplify_noop
6823#endif
6824
6825#if 0
6826#define simplify_copy simplify_noop
6827#endif
6828
6829#if 0
Eric Biedermanb138ac82003-04-22 18:44:01 +00006830#define simplify_branch simplify_noop
6831#endif
Eric Biederman83b991a2003-10-11 06:20:25 +00006832#if 0
Eric Biederman530b5192003-07-01 10:05:30 +00006833#define simplify_label simplify_noop
6834#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00006835
6836#if 0
6837#define simplify_phi simplify_noop
6838#endif
6839
6840#if 0
6841#define simplify_bsf simplify_noop
6842#define simplify_bsr simplify_noop
6843#endif
6844
Eric Biederman83b991a2003-10-11 06:20:25 +00006845#if 1
6846#define simplify_piece simplify_noop
6847#endif
6848
Eric Biederman530b5192003-07-01 10:05:30 +00006849[OP_SDIVT ] = simplify_sdivt,
6850[OP_UDIVT ] = simplify_udivt,
Eric Biedermanb138ac82003-04-22 18:44:01 +00006851[OP_SMUL ] = simplify_smul,
6852[OP_UMUL ] = simplify_umul,
6853[OP_SDIV ] = simplify_sdiv,
6854[OP_UDIV ] = simplify_udiv,
6855[OP_SMOD ] = simplify_smod,
6856[OP_UMOD ] = simplify_umod,
6857[OP_ADD ] = simplify_add,
6858[OP_SUB ] = simplify_sub,
6859[OP_SL ] = simplify_sl,
6860[OP_USR ] = simplify_usr,
6861[OP_SSR ] = simplify_ssr,
6862[OP_AND ] = simplify_and,
6863[OP_XOR ] = simplify_xor,
6864[OP_OR ] = simplify_or,
6865[OP_POS ] = simplify_pos,
6866[OP_NEG ] = simplify_neg,
6867[OP_INVERT ] = simplify_invert,
6868
6869[OP_EQ ] = simplify_eq,
6870[OP_NOTEQ ] = simplify_noteq,
6871[OP_SLESS ] = simplify_sless,
6872[OP_ULESS ] = simplify_uless,
6873[OP_SMORE ] = simplify_smore,
6874[OP_UMORE ] = simplify_umore,
6875[OP_SLESSEQ ] = simplify_slesseq,
6876[OP_ULESSEQ ] = simplify_ulesseq,
6877[OP_SMOREEQ ] = simplify_smoreeq,
6878[OP_UMOREEQ ] = simplify_umoreeq,
6879[OP_LFALSE ] = simplify_lfalse,
6880[OP_LTRUE ] = simplify_ltrue,
6881
6882[OP_LOAD ] = simplify_noop,
6883[OP_STORE ] = simplify_noop,
6884
6885[OP_NOOP ] = simplify_noop,
6886
6887[OP_INTCONST ] = simplify_noop,
6888[OP_BLOBCONST ] = simplify_noop,
6889[OP_ADDRCONST ] = simplify_noop,
6890
6891[OP_WRITE ] = simplify_noop,
6892[OP_READ ] = simplify_noop,
6893[OP_COPY ] = simplify_copy,
Eric Biederman83b991a2003-10-11 06:20:25 +00006894[OP_PIECE ] = simplify_piece,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006895[OP_ASM ] = simplify_noop,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006896
6897[OP_DOT ] = simplify_noop,
6898[OP_VAL_VEC ] = simplify_noop,
Eric Biedermanb138ac82003-04-22 18:44:01 +00006899
6900[OP_LIST ] = simplify_noop,
6901[OP_BRANCH ] = simplify_branch,
Eric Biederman530b5192003-07-01 10:05:30 +00006902[OP_LABEL ] = simplify_label,
Eric Biedermanb138ac82003-04-22 18:44:01 +00006903[OP_ADECL ] = simplify_noop,
6904[OP_SDECL ] = simplify_noop,
6905[OP_PHI ] = simplify_phi,
6906
6907[OP_INB ] = simplify_noop,
6908[OP_INW ] = simplify_noop,
6909[OP_INL ] = simplify_noop,
6910[OP_OUTB ] = simplify_noop,
6911[OP_OUTW ] = simplify_noop,
6912[OP_OUTL ] = simplify_noop,
6913[OP_BSF ] = simplify_bsf,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006914[OP_BSR ] = simplify_bsr,
6915[OP_RDMSR ] = simplify_noop,
6916[OP_WRMSR ] = simplify_noop,
6917[OP_HLT ] = simplify_noop,
Eric Biedermanb138ac82003-04-22 18:44:01 +00006918};
6919
6920static void simplify(struct compile_state *state, struct triple *ins)
6921{
6922 int op;
6923 simplify_t do_simplify;
6924 do {
6925 op = ins->op;
6926 do_simplify = 0;
6927 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
6928 do_simplify = 0;
6929 }
6930 else {
6931 do_simplify = table_simplify[op];
6932 }
6933 if (!do_simplify) {
6934 internal_error(state, ins, "cannot simplify op: %d %s\n",
6935 op, tops(op));
6936 return;
6937 }
Eric Biederman83b991a2003-10-11 06:20:25 +00006938#if !DEBUG_SIMPLIFY
Eric Biedermanb138ac82003-04-22 18:44:01 +00006939 do_simplify(state, ins);
Eric Biederman83b991a2003-10-11 06:20:25 +00006940#else
6941 {
6942 struct triple *dup;
6943 int ins_count, dup_count;
6944 dup = dup_triple(state, ins);
6945 do_simplify(state, ins);
6946 ins_count = TRIPLE_SIZE(ins->sizes);
6947 dup_count = TRIPLE_SIZE(dup->sizes);
6948 if ((dup->op != ins->op) ||
6949 (ins_count != dup_count) ||
6950 (memcmp(dup->param, ins->param,
6951 dup_count * sizeof(dup->param[0])) != 0) ||
6952 (memcmp(&dup->u, &ins->u, sizeof(ins->u)) != 0))
6953 {
6954 int i, min_count;
6955 fprintf(stderr, "simplify: %11p", ins);
6956 if (dup->op == ins->op) {
6957 fprintf(stderr, " %-11s", tops(ins->op));
6958 } else {
6959 fprintf(stderr, " [%-10s %-10s]",
6960 tops(dup->op), tops(ins->op));
6961 }
6962 min_count = dup_count;
6963 if (min_count > ins_count) {
6964 min_count = ins_count;
6965 }
6966 for(i = 0; i < min_count; i++) {
6967 if (dup->param[i] == ins->param[i]) {
6968 fprintf(stderr, " %-11p", ins->param[i]);
6969 } else {
6970 fprintf(stderr, " [%-10p %-10p]",
6971 dup->param[i], ins->param[i]);
6972 }
6973 }
6974 for(; i < ins_count; i++) {
6975 fprintf(stderr, " [%-9p]", ins->param[i]);
6976 }
6977 for(; i < dup_count; i++) {
6978 fprintf(stderr, " [%-9p]", dup->param[i]);
6979 }
6980 fprintf(stderr, "\n");
6981 fflush(stderr);
6982 }
6983 xfree(dup);
6984 }
6985#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00006986 } while(ins->op != op);
6987}
6988
6989static void simplify_all(struct compile_state *state)
6990{
6991 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +00006992 first = state->first;
6993 ins = first->prev;
6994 do {
6995 simplify(state, ins);
6996 ins = ins->prev;
6997 } while(ins != first->prev);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006998 ins = first;
6999 do {
7000 simplify(state, ins);
7001 ins = ins->next;
Eric Biederman530b5192003-07-01 10:05:30 +00007002 }while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007003}
7004
7005/*
7006 * Builtins....
7007 * ============================
7008 */
7009
Eric Biederman0babc1c2003-05-09 02:39:00 +00007010static void register_builtin_function(struct compile_state *state,
7011 const char *name, int op, struct type *rtype, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007012{
Eric Biederman0babc1c2003-05-09 02:39:00 +00007013 struct type *ftype, *atype, *param, **next;
7014 struct triple *def, *arg, *result, *work, *last, *first;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007015 struct hash_entry *ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007016 struct file_state file;
7017 int parameters;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007018 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007019 va_list args;
7020 int i;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007021
7022 /* Dummy file state to get debug handling right */
Eric Biedermanb138ac82003-04-22 18:44:01 +00007023 memset(&file, 0, sizeof(file));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00007024 file.basename = "<built-in>";
Eric Biedermanb138ac82003-04-22 18:44:01 +00007025 file.line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00007026 file.report_line = 1;
7027 file.report_name = file.basename;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007028 file.prev = state->file;
7029 state->file = &file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00007030 state->function = name;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007031
7032 /* Find the Parameter count */
7033 valid_op(state, op);
7034 parameters = table_ops[op].rhs;
7035 if (parameters < 0 ) {
7036 internal_error(state, 0, "Invalid builtin parameter count");
7037 }
7038
7039 /* Find the function type */
7040 ftype = new_type(TYPE_FUNCTION, rtype, 0);
7041 next = &ftype->right;
7042 va_start(args, rtype);
7043 for(i = 0; i < parameters; i++) {
7044 atype = va_arg(args, struct type *);
7045 if (!*next) {
7046 *next = atype;
7047 } else {
7048 *next = new_type(TYPE_PRODUCT, *next, atype);
7049 next = &((*next)->right);
7050 }
7051 }
7052 if (!*next) {
7053 *next = &void_type;
7054 }
7055 va_end(args);
7056
Eric Biedermanb138ac82003-04-22 18:44:01 +00007057 /* Generate the needed triples */
7058 def = triple(state, OP_LIST, ftype, 0, 0);
7059 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007060 RHS(def, 0) = first;
7061
7062 /* Now string them together */
7063 param = ftype->right;
7064 for(i = 0; i < parameters; i++) {
7065 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
7066 atype = param->left;
7067 } else {
7068 atype = param;
7069 }
7070 arg = flatten(state, first, variable(state, atype));
7071 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007072 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007073 result = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007074 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007075 result = flatten(state, first, variable(state, rtype));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007076 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007077 MISC(def, 0) = result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007078 work = new_triple(state, op, rtype, -1, parameters);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007079 for(i = 0, arg = first->next; i < parameters; i++, arg = arg->next) {
7080 RHS(work, i) = read_expr(state, arg);
7081 }
7082 if (result && ((rtype->type & TYPE_MASK) == TYPE_STRUCT)) {
7083 struct triple *val;
7084 /* Populate the LHS with the target registers */
7085 work = flatten(state, first, work);
7086 work->type = &void_type;
7087 param = rtype->left;
7088 if (rtype->elements != TRIPLE_LHS(work->sizes)) {
7089 internal_error(state, 0, "Invalid result type");
7090 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007091 val = new_triple(state, OP_VAL_VEC, rtype, -1, -1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007092 for(i = 0; i < rtype->elements; i++) {
7093 struct triple *piece;
7094 atype = param;
7095 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
7096 atype = param->left;
7097 }
7098 if (!TYPE_ARITHMETIC(atype->type) &&
7099 !TYPE_PTR(atype->type)) {
7100 internal_error(state, 0, "Invalid lhs type");
7101 }
7102 piece = triple(state, OP_PIECE, atype, work, 0);
7103 piece->u.cval = i;
7104 LHS(work, i) = piece;
7105 RHS(val, i) = piece;
7106 }
7107 work = val;
7108 }
7109 if (result) {
7110 work = write_expr(state, result, work);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007111 }
7112 work = flatten(state, first, work);
7113 last = flatten(state, first, label(state));
7114 name_len = strlen(name);
7115 ident = lookup(state, name, name_len);
7116 symbol(state, ident, &ident->sym_ident, def, ftype);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007117
Eric Biedermanb138ac82003-04-22 18:44:01 +00007118 state->file = file.prev;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00007119 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007120#if 0
7121 fprintf(stdout, "\n");
7122 loc(stdout, state, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007123 fprintf(stdout, "\n__________ builtin_function _________\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007124 print_triple(state, def);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007125 fprintf(stdout, "__________ builtin_function _________ done\n\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007126#endif
7127}
7128
Eric Biederman0babc1c2003-05-09 02:39:00 +00007129static struct type *partial_struct(struct compile_state *state,
7130 const char *field_name, struct type *type, struct type *rest)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007131{
Eric Biederman0babc1c2003-05-09 02:39:00 +00007132 struct hash_entry *field_ident;
7133 struct type *result;
7134 int field_name_len;
7135
7136 field_name_len = strlen(field_name);
7137 field_ident = lookup(state, field_name, field_name_len);
7138
7139 result = clone_type(0, type);
7140 result->field_ident = field_ident;
7141
7142 if (rest) {
7143 result = new_type(TYPE_PRODUCT, result, rest);
7144 }
7145 return result;
7146}
7147
7148static struct type *register_builtin_type(struct compile_state *state,
7149 const char *name, struct type *type)
7150{
Eric Biedermanb138ac82003-04-22 18:44:01 +00007151 struct hash_entry *ident;
7152 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007153
Eric Biedermanb138ac82003-04-22 18:44:01 +00007154 name_len = strlen(name);
7155 ident = lookup(state, name, name_len);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007156
7157 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
7158 ulong_t elements = 0;
7159 struct type *field;
7160 type = new_type(TYPE_STRUCT, type, 0);
7161 field = type->left;
7162 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
7163 elements++;
7164 field = field->right;
7165 }
7166 elements++;
Eric Biederman83b991a2003-10-11 06:20:25 +00007167 symbol(state, ident, &ident->sym_tag, 0, type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007168 type->type_ident = ident;
7169 type->elements = elements;
7170 }
7171 symbol(state, ident, &ident->sym_ident, 0, type);
7172 ident->tok = TOK_TYPE_NAME;
7173 return type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007174}
7175
Eric Biederman0babc1c2003-05-09 02:39:00 +00007176
Eric Biedermanb138ac82003-04-22 18:44:01 +00007177static void register_builtins(struct compile_state *state)
7178{
Eric Biederman530b5192003-07-01 10:05:30 +00007179 struct type *div_type, *ldiv_type;
7180 struct type *udiv_type, *uldiv_type;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007181 struct type *msr_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007182
Eric Biederman530b5192003-07-01 10:05:30 +00007183 div_type = register_builtin_type(state, "__builtin_div_t",
7184 partial_struct(state, "quot", &int_type,
7185 partial_struct(state, "rem", &int_type, 0)));
7186 ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
7187 partial_struct(state, "quot", &long_type,
7188 partial_struct(state, "rem", &long_type, 0)));
7189 udiv_type = register_builtin_type(state, "__builtin_udiv_t",
7190 partial_struct(state, "quot", &uint_type,
7191 partial_struct(state, "rem", &uint_type, 0)));
7192 uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
7193 partial_struct(state, "quot", &ulong_type,
7194 partial_struct(state, "rem", &ulong_type, 0)));
7195
7196 register_builtin_function(state, "__builtin_div", OP_SDIVT, div_type,
7197 &int_type, &int_type);
7198 register_builtin_function(state, "__builtin_ldiv", OP_SDIVT, ldiv_type,
7199 &long_type, &long_type);
7200 register_builtin_function(state, "__builtin_udiv", OP_UDIVT, udiv_type,
7201 &uint_type, &uint_type);
7202 register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
7203 &ulong_type, &ulong_type);
7204
Eric Biederman0babc1c2003-05-09 02:39:00 +00007205 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
7206 &ushort_type);
7207 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
7208 &ushort_type);
7209 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
7210 &ushort_type);
7211
7212 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
7213 &uchar_type, &ushort_type);
7214 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
7215 &ushort_type, &ushort_type);
7216 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
7217 &uint_type, &ushort_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007218
Eric Biederman0babc1c2003-05-09 02:39:00 +00007219 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
7220 &int_type);
7221 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
7222 &int_type);
7223
7224 msr_type = register_builtin_type(state, "__builtin_msr_t",
7225 partial_struct(state, "lo", &ulong_type,
7226 partial_struct(state, "hi", &ulong_type, 0)));
7227
7228 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
7229 &ulong_type);
7230 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
7231 &ulong_type, &ulong_type, &ulong_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007232
Eric Biederman0babc1c2003-05-09 02:39:00 +00007233 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
7234 &void_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007235}
7236
7237static struct type *declarator(
7238 struct compile_state *state, struct type *type,
7239 struct hash_entry **ident, int need_ident);
7240static void decl(struct compile_state *state, struct triple *first);
7241static struct type *specifier_qualifier_list(struct compile_state *state);
7242static int isdecl_specifier(int tok);
7243static struct type *decl_specifiers(struct compile_state *state);
7244static int istype(int tok);
7245static struct triple *expr(struct compile_state *state);
7246static struct triple *assignment_expr(struct compile_state *state);
7247static struct type *type_name(struct compile_state *state);
7248static void statement(struct compile_state *state, struct triple *fist);
7249
7250static struct triple *call_expr(
7251 struct compile_state *state, struct triple *func)
7252{
Eric Biederman0babc1c2003-05-09 02:39:00 +00007253 struct triple *def;
7254 struct type *param, *type;
7255 ulong_t pvals, index;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007256
7257 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
7258 error(state, 0, "Called object is not a function");
7259 }
7260 if (func->op != OP_LIST) {
7261 internal_error(state, 0, "improper function");
7262 }
7263 eat(state, TOK_LPAREN);
7264 /* Find the return type without any specifiers */
7265 type = clone_type(0, func->type->left);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007266 def = new_triple(state, OP_CALL, func->type, -1, -1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007267 def->type = type;
7268
7269 pvals = TRIPLE_RHS(def->sizes);
7270 MISC(def, 0) = func;
7271
7272 param = func->type->right;
7273 for(index = 0; index < pvals; index++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007274 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007275 struct type *arg_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007276 val = read_expr(state, assignment_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007277 arg_type = param;
7278 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
7279 arg_type = param->left;
7280 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007281 write_compatible(state, arg_type, val->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007282 RHS(def, index) = val;
7283 if (index != (pvals - 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007284 eat(state, TOK_COMMA);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007285 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007286 }
7287 }
7288 eat(state, TOK_RPAREN);
7289 return def;
7290}
7291
7292
7293static struct triple *character_constant(struct compile_state *state)
7294{
7295 struct triple *def;
7296 struct token *tk;
7297 const signed char *str, *end;
7298 int c;
7299 int str_len;
7300 eat(state, TOK_LIT_CHAR);
7301 tk = &state->token[0];
7302 str = tk->val.str + 1;
7303 str_len = tk->str_len - 2;
7304 if (str_len <= 0) {
7305 error(state, 0, "empty character constant");
7306 }
7307 end = str + str_len;
7308 c = char_value(state, &str, end);
7309 if (str != end) {
7310 error(state, 0, "multibyte character constant not supported");
7311 }
7312 def = int_const(state, &char_type, (ulong_t)((long_t)c));
7313 return def;
7314}
7315
7316static struct triple *string_constant(struct compile_state *state)
7317{
7318 struct triple *def;
7319 struct token *tk;
7320 struct type *type;
7321 const signed char *str, *end;
7322 signed char *buf, *ptr;
7323 int str_len;
7324
7325 buf = 0;
7326 type = new_type(TYPE_ARRAY, &char_type, 0);
7327 type->elements = 0;
7328 /* The while loop handles string concatenation */
7329 do {
7330 eat(state, TOK_LIT_STRING);
7331 tk = &state->token[0];
7332 str = tk->val.str + 1;
7333 str_len = tk->str_len - 2;
Eric Biedermanab2ea6b2003-04-26 03:20:53 +00007334 if (str_len < 0) {
7335 error(state, 0, "negative string constant length");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007336 }
7337 end = str + str_len;
7338 ptr = buf;
7339 buf = xmalloc(type->elements + str_len + 1, "string_constant");
7340 memcpy(buf, ptr, type->elements);
7341 ptr = buf + type->elements;
7342 do {
7343 *ptr++ = char_value(state, &str, end);
7344 } while(str < end);
7345 type->elements = ptr - buf;
7346 } while(peek(state) == TOK_LIT_STRING);
7347 *ptr = '\0';
7348 type->elements += 1;
7349 def = triple(state, OP_BLOBCONST, type, 0, 0);
7350 def->u.blob = buf;
7351 return def;
7352}
7353
7354
7355static struct triple *integer_constant(struct compile_state *state)
7356{
7357 struct triple *def;
7358 unsigned long val;
7359 struct token *tk;
7360 char *end;
7361 int u, l, decimal;
7362 struct type *type;
7363
7364 eat(state, TOK_LIT_INT);
7365 tk = &state->token[0];
7366 errno = 0;
7367 decimal = (tk->val.str[0] != '0');
7368 val = strtoul(tk->val.str, &end, 0);
Eric Biederman83b991a2003-10-11 06:20:25 +00007369 if ((val > ULONG_T_MAX) || ((val == ULONG_MAX) && (errno == ERANGE))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007370 error(state, 0, "Integer constant to large");
7371 }
7372 u = l = 0;
7373 if ((*end == 'u') || (*end == 'U')) {
7374 u = 1;
7375 end++;
7376 }
7377 if ((*end == 'l') || (*end == 'L')) {
7378 l = 1;
7379 end++;
7380 }
7381 if ((*end == 'u') || (*end == 'U')) {
7382 u = 1;
7383 end++;
7384 }
7385 if (*end) {
7386 error(state, 0, "Junk at end of integer constant");
7387 }
7388 if (u && l) {
7389 type = &ulong_type;
7390 }
7391 else if (l) {
7392 type = &long_type;
Eric Biederman83b991a2003-10-11 06:20:25 +00007393 if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007394 type = &ulong_type;
7395 }
7396 }
7397 else if (u) {
7398 type = &uint_type;
Eric Biederman83b991a2003-10-11 06:20:25 +00007399 if (val > UINT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007400 type = &ulong_type;
7401 }
7402 }
7403 else {
7404 type = &int_type;
Eric Biederman83b991a2003-10-11 06:20:25 +00007405 if (!decimal && (val > INT_T_MAX) && (val <= UINT_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007406 type = &uint_type;
7407 }
Eric Biederman83b991a2003-10-11 06:20:25 +00007408 else if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007409 type = &ulong_type;
7410 }
Eric Biederman83b991a2003-10-11 06:20:25 +00007411 else if (val > INT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007412 type = &long_type;
7413 }
7414 }
7415 def = int_const(state, type, val);
7416 return def;
7417}
7418
7419static struct triple *primary_expr(struct compile_state *state)
7420{
7421 struct triple *def;
7422 int tok;
7423 tok = peek(state);
7424 switch(tok) {
7425 case TOK_IDENT:
7426 {
7427 struct hash_entry *ident;
7428 /* Here ident is either:
7429 * a varable name
7430 * a function name
Eric Biedermanb138ac82003-04-22 18:44:01 +00007431 */
7432 eat(state, TOK_IDENT);
7433 ident = state->token[0].ident;
7434 if (!ident->sym_ident) {
7435 error(state, 0, "%s undeclared", ident->name);
7436 }
7437 def = ident->sym_ident->def;
7438 break;
7439 }
7440 case TOK_ENUM_CONST:
Eric Biederman83b991a2003-10-11 06:20:25 +00007441 {
7442 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007443 /* Here ident is an enumeration constant */
7444 eat(state, TOK_ENUM_CONST);
Eric Biederman83b991a2003-10-11 06:20:25 +00007445 ident = state->token[0].ident;
7446 if (!ident->sym_ident) {
7447 error(state, 0, "%s undeclared", ident->name);
7448 }
7449 def = ident->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007450 break;
Eric Biederman83b991a2003-10-11 06:20:25 +00007451 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007452 case TOK_LPAREN:
7453 eat(state, TOK_LPAREN);
7454 def = expr(state);
7455 eat(state, TOK_RPAREN);
7456 break;
7457 case TOK_LIT_INT:
7458 def = integer_constant(state);
7459 break;
7460 case TOK_LIT_FLOAT:
7461 eat(state, TOK_LIT_FLOAT);
7462 error(state, 0, "Floating point constants not supported");
7463 def = 0;
7464 FINISHME();
7465 break;
7466 case TOK_LIT_CHAR:
7467 def = character_constant(state);
7468 break;
7469 case TOK_LIT_STRING:
7470 def = string_constant(state);
7471 break;
7472 default:
7473 def = 0;
7474 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
7475 }
7476 return def;
7477}
7478
7479static struct triple *postfix_expr(struct compile_state *state)
7480{
7481 struct triple *def;
7482 int postfix;
7483 def = primary_expr(state);
7484 do {
7485 struct triple *left;
7486 int tok;
7487 postfix = 1;
7488 left = def;
7489 switch((tok = peek(state))) {
7490 case TOK_LBRACKET:
7491 eat(state, TOK_LBRACKET);
7492 def = mk_subscript_expr(state, left, expr(state));
7493 eat(state, TOK_RBRACKET);
7494 break;
7495 case TOK_LPAREN:
7496 def = call_expr(state, def);
7497 break;
7498 case TOK_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007499 {
7500 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007501 eat(state, TOK_DOT);
7502 eat(state, TOK_IDENT);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007503 field = state->token[0].ident;
7504 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007505 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007506 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007507 case TOK_ARROW:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007508 {
7509 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007510 eat(state, TOK_ARROW);
7511 eat(state, TOK_IDENT);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007512 field = state->token[0].ident;
7513 def = mk_deref_expr(state, read_expr(state, def));
7514 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007515 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007516 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007517 case TOK_PLUSPLUS:
7518 eat(state, TOK_PLUSPLUS);
7519 def = mk_post_inc_expr(state, left);
7520 break;
7521 case TOK_MINUSMINUS:
7522 eat(state, TOK_MINUSMINUS);
7523 def = mk_post_dec_expr(state, left);
7524 break;
7525 default:
7526 postfix = 0;
7527 break;
7528 }
7529 } while(postfix);
7530 return def;
7531}
7532
7533static struct triple *cast_expr(struct compile_state *state);
7534
7535static struct triple *unary_expr(struct compile_state *state)
7536{
7537 struct triple *def, *right;
7538 int tok;
7539 switch((tok = peek(state))) {
7540 case TOK_PLUSPLUS:
7541 eat(state, TOK_PLUSPLUS);
7542 def = mk_pre_inc_expr(state, unary_expr(state));
7543 break;
7544 case TOK_MINUSMINUS:
7545 eat(state, TOK_MINUSMINUS);
7546 def = mk_pre_dec_expr(state, unary_expr(state));
7547 break;
7548 case TOK_AND:
7549 eat(state, TOK_AND);
7550 def = mk_addr_expr(state, cast_expr(state), 0);
7551 break;
7552 case TOK_STAR:
7553 eat(state, TOK_STAR);
7554 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
7555 break;
7556 case TOK_PLUS:
7557 eat(state, TOK_PLUS);
7558 right = read_expr(state, cast_expr(state));
7559 arithmetic(state, right);
7560 def = integral_promotion(state, right);
7561 break;
7562 case TOK_MINUS:
7563 eat(state, TOK_MINUS);
7564 right = read_expr(state, cast_expr(state));
7565 arithmetic(state, right);
7566 def = integral_promotion(state, right);
7567 def = triple(state, OP_NEG, def->type, def, 0);
7568 break;
7569 case TOK_TILDE:
7570 eat(state, TOK_TILDE);
7571 right = read_expr(state, cast_expr(state));
7572 integral(state, right);
7573 def = integral_promotion(state, right);
7574 def = triple(state, OP_INVERT, def->type, def, 0);
7575 break;
7576 case TOK_BANG:
7577 eat(state, TOK_BANG);
7578 right = read_expr(state, cast_expr(state));
7579 bool(state, right);
7580 def = lfalse_expr(state, right);
7581 break;
7582 case TOK_SIZEOF:
7583 {
7584 struct type *type;
7585 int tok1, tok2;
7586 eat(state, TOK_SIZEOF);
7587 tok1 = peek(state);
7588 tok2 = peek2(state);
7589 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7590 eat(state, TOK_LPAREN);
7591 type = type_name(state);
7592 eat(state, TOK_RPAREN);
7593 }
7594 else {
7595 struct triple *expr;
7596 expr = unary_expr(state);
7597 type = expr->type;
7598 release_expr(state, expr);
7599 }
7600 def = int_const(state, &ulong_type, size_of(state, type));
7601 break;
7602 }
7603 case TOK_ALIGNOF:
7604 {
7605 struct type *type;
7606 int tok1, tok2;
7607 eat(state, TOK_ALIGNOF);
7608 tok1 = peek(state);
7609 tok2 = peek2(state);
7610 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7611 eat(state, TOK_LPAREN);
7612 type = type_name(state);
7613 eat(state, TOK_RPAREN);
7614 }
7615 else {
7616 struct triple *expr;
7617 expr = unary_expr(state);
7618 type = expr->type;
7619 release_expr(state, expr);
7620 }
7621 def = int_const(state, &ulong_type, align_of(state, type));
7622 break;
7623 }
7624 default:
7625 def = postfix_expr(state);
7626 break;
7627 }
7628 return def;
7629}
7630
7631static struct triple *cast_expr(struct compile_state *state)
7632{
7633 struct triple *def;
7634 int tok1, tok2;
7635 tok1 = peek(state);
7636 tok2 = peek2(state);
7637 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7638 struct type *type;
7639 eat(state, TOK_LPAREN);
7640 type = type_name(state);
7641 eat(state, TOK_RPAREN);
Eric Biedermane058a1e2003-07-12 01:21:31 +00007642 def = mk_cast_expr(state, type, cast_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007643 }
7644 else {
7645 def = unary_expr(state);
7646 }
7647 return def;
7648}
7649
7650static struct triple *mult_expr(struct compile_state *state)
7651{
7652 struct triple *def;
7653 int done;
7654 def = cast_expr(state);
7655 do {
7656 struct triple *left, *right;
7657 struct type *result_type;
7658 int tok, op, sign;
7659 done = 0;
7660 switch(tok = (peek(state))) {
7661 case TOK_STAR:
7662 case TOK_DIV:
7663 case TOK_MOD:
7664 left = read_expr(state, def);
7665 arithmetic(state, left);
7666
7667 eat(state, tok);
7668
7669 right = read_expr(state, cast_expr(state));
7670 arithmetic(state, right);
7671
7672 result_type = arithmetic_result(state, left, right);
7673 sign = is_signed(result_type);
7674 op = -1;
7675 switch(tok) {
7676 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
7677 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
7678 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
7679 }
7680 def = triple(state, op, result_type, left, right);
7681 break;
7682 default:
7683 done = 1;
7684 break;
7685 }
7686 } while(!done);
7687 return def;
7688}
7689
7690static struct triple *add_expr(struct compile_state *state)
7691{
7692 struct triple *def;
7693 int done;
7694 def = mult_expr(state);
7695 do {
7696 done = 0;
7697 switch( peek(state)) {
7698 case TOK_PLUS:
7699 eat(state, TOK_PLUS);
7700 def = mk_add_expr(state, def, mult_expr(state));
7701 break;
7702 case TOK_MINUS:
7703 eat(state, TOK_MINUS);
7704 def = mk_sub_expr(state, def, mult_expr(state));
7705 break;
7706 default:
7707 done = 1;
7708 break;
7709 }
7710 } while(!done);
7711 return def;
7712}
7713
7714static struct triple *shift_expr(struct compile_state *state)
7715{
7716 struct triple *def;
7717 int done;
7718 def = add_expr(state);
7719 do {
7720 struct triple *left, *right;
7721 int tok, op;
7722 done = 0;
7723 switch((tok = peek(state))) {
7724 case TOK_SL:
7725 case TOK_SR:
7726 left = read_expr(state, def);
7727 integral(state, left);
7728 left = integral_promotion(state, left);
7729
7730 eat(state, tok);
7731
7732 right = read_expr(state, add_expr(state));
7733 integral(state, right);
7734 right = integral_promotion(state, right);
7735
7736 op = (tok == TOK_SL)? OP_SL :
7737 is_signed(left->type)? OP_SSR: OP_USR;
7738
7739 def = triple(state, op, left->type, left, right);
7740 break;
7741 default:
7742 done = 1;
7743 break;
7744 }
7745 } while(!done);
7746 return def;
7747}
7748
7749static struct triple *relational_expr(struct compile_state *state)
7750{
7751#warning "Extend relational exprs to work on more than arithmetic types"
7752 struct triple *def;
7753 int done;
7754 def = shift_expr(state);
7755 do {
7756 struct triple *left, *right;
7757 struct type *arg_type;
7758 int tok, op, sign;
7759 done = 0;
7760 switch((tok = peek(state))) {
7761 case TOK_LESS:
7762 case TOK_MORE:
7763 case TOK_LESSEQ:
7764 case TOK_MOREEQ:
7765 left = read_expr(state, def);
7766 arithmetic(state, left);
7767
7768 eat(state, tok);
7769
7770 right = read_expr(state, shift_expr(state));
7771 arithmetic(state, right);
7772
7773 arg_type = arithmetic_result(state, left, right);
7774 sign = is_signed(arg_type);
7775 op = -1;
7776 switch(tok) {
7777 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
7778 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
7779 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
7780 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
7781 }
7782 def = triple(state, op, &int_type, left, right);
7783 break;
7784 default:
7785 done = 1;
7786 break;
7787 }
7788 } while(!done);
7789 return def;
7790}
7791
7792static struct triple *equality_expr(struct compile_state *state)
7793{
7794#warning "Extend equality exprs to work on more than arithmetic types"
7795 struct triple *def;
7796 int done;
7797 def = relational_expr(state);
7798 do {
7799 struct triple *left, *right;
7800 int tok, op;
7801 done = 0;
7802 switch((tok = peek(state))) {
7803 case TOK_EQEQ:
7804 case TOK_NOTEQ:
7805 left = read_expr(state, def);
7806 arithmetic(state, left);
7807 eat(state, tok);
7808 right = read_expr(state, relational_expr(state));
7809 arithmetic(state, right);
7810 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
7811 def = triple(state, op, &int_type, left, right);
7812 break;
7813 default:
7814 done = 1;
7815 break;
7816 }
7817 } while(!done);
7818 return def;
7819}
7820
7821static struct triple *and_expr(struct compile_state *state)
7822{
7823 struct triple *def;
7824 def = equality_expr(state);
7825 while(peek(state) == TOK_AND) {
7826 struct triple *left, *right;
7827 struct type *result_type;
7828 left = read_expr(state, def);
7829 integral(state, left);
7830 eat(state, TOK_AND);
7831 right = read_expr(state, equality_expr(state));
7832 integral(state, right);
7833 result_type = arithmetic_result(state, left, right);
7834 def = triple(state, OP_AND, result_type, left, right);
7835 }
7836 return def;
7837}
7838
7839static struct triple *xor_expr(struct compile_state *state)
7840{
7841 struct triple *def;
7842 def = and_expr(state);
7843 while(peek(state) == TOK_XOR) {
7844 struct triple *left, *right;
7845 struct type *result_type;
7846 left = read_expr(state, def);
7847 integral(state, left);
7848 eat(state, TOK_XOR);
7849 right = read_expr(state, and_expr(state));
7850 integral(state, right);
7851 result_type = arithmetic_result(state, left, right);
7852 def = triple(state, OP_XOR, result_type, left, right);
7853 }
7854 return def;
7855}
7856
7857static struct triple *or_expr(struct compile_state *state)
7858{
7859 struct triple *def;
7860 def = xor_expr(state);
7861 while(peek(state) == TOK_OR) {
7862 struct triple *left, *right;
7863 struct type *result_type;
7864 left = read_expr(state, def);
7865 integral(state, left);
7866 eat(state, TOK_OR);
7867 right = read_expr(state, xor_expr(state));
7868 integral(state, right);
7869 result_type = arithmetic_result(state, left, right);
7870 def = triple(state, OP_OR, result_type, left, right);
7871 }
7872 return def;
7873}
7874
7875static struct triple *land_expr(struct compile_state *state)
7876{
7877 struct triple *def;
7878 def = or_expr(state);
7879 while(peek(state) == TOK_LOGAND) {
7880 struct triple *left, *right;
7881 left = read_expr(state, def);
7882 bool(state, left);
7883 eat(state, TOK_LOGAND);
7884 right = read_expr(state, or_expr(state));
7885 bool(state, right);
7886
7887 def = triple(state, OP_LAND, &int_type,
7888 ltrue_expr(state, left),
7889 ltrue_expr(state, right));
7890 }
7891 return def;
7892}
7893
7894static struct triple *lor_expr(struct compile_state *state)
7895{
7896 struct triple *def;
7897 def = land_expr(state);
7898 while(peek(state) == TOK_LOGOR) {
7899 struct triple *left, *right;
7900 left = read_expr(state, def);
7901 bool(state, left);
7902 eat(state, TOK_LOGOR);
7903 right = read_expr(state, land_expr(state));
7904 bool(state, right);
7905
7906 def = triple(state, OP_LOR, &int_type,
7907 ltrue_expr(state, left),
7908 ltrue_expr(state, right));
7909 }
7910 return def;
7911}
7912
7913static struct triple *conditional_expr(struct compile_state *state)
7914{
7915 struct triple *def;
7916 def = lor_expr(state);
7917 if (peek(state) == TOK_QUEST) {
7918 struct triple *test, *left, *right;
7919 bool(state, def);
7920 test = ltrue_expr(state, read_expr(state, def));
7921 eat(state, TOK_QUEST);
7922 left = read_expr(state, expr(state));
7923 eat(state, TOK_COLON);
7924 right = read_expr(state, conditional_expr(state));
7925
7926 def = cond_expr(state, test, left, right);
7927 }
7928 return def;
7929}
7930
7931static struct triple *eval_const_expr(
7932 struct compile_state *state, struct triple *expr)
7933{
7934 struct triple *def;
Eric Biederman00443072003-06-24 12:34:45 +00007935 if (is_const(expr)) {
7936 def = expr;
7937 }
7938 else {
7939 /* If we don't start out as a constant simplify into one */
7940 struct triple *head, *ptr;
7941 head = label(state); /* dummy initial triple */
7942 flatten(state, head, expr);
7943 for(ptr = head->next; ptr != head; ptr = ptr->next) {
7944 simplify(state, ptr);
7945 }
7946 /* Remove the constant value the tail of the list */
7947 def = head->prev;
7948 def->prev->next = def->next;
7949 def->next->prev = def->prev;
7950 def->next = def->prev = def;
7951 if (!is_const(def)) {
7952 error(state, 0, "Not a constant expression");
7953 }
7954 /* Free the intermediate expressions */
7955 while(head->next != head) {
7956 release_triple(state, head->next);
7957 }
7958 free_triple(state, head);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007959 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007960 return def;
7961}
7962
7963static struct triple *constant_expr(struct compile_state *state)
7964{
7965 return eval_const_expr(state, conditional_expr(state));
7966}
7967
7968static struct triple *assignment_expr(struct compile_state *state)
7969{
7970 struct triple *def, *left, *right;
7971 int tok, op, sign;
7972 /* The C grammer in K&R shows assignment expressions
7973 * only taking unary expressions as input on their
7974 * left hand side. But specifies the precedence of
7975 * assignemnt as the lowest operator except for comma.
7976 *
7977 * Allowing conditional expressions on the left hand side
7978 * of an assignement results in a grammar that accepts
7979 * a larger set of statements than standard C. As long
7980 * as the subset of the grammar that is standard C behaves
7981 * correctly this should cause no problems.
7982 *
7983 * For the extra token strings accepted by the grammar
7984 * none of them should produce a valid lvalue, so they
7985 * should not produce functioning programs.
7986 *
7987 * GCC has this bug as well, so surprises should be minimal.
7988 */
7989 def = conditional_expr(state);
7990 left = def;
7991 switch((tok = peek(state))) {
7992 case TOK_EQ:
7993 lvalue(state, left);
7994 eat(state, TOK_EQ);
7995 def = write_expr(state, left,
7996 read_expr(state, assignment_expr(state)));
7997 break;
7998 case TOK_TIMESEQ:
7999 case TOK_DIVEQ:
8000 case TOK_MODEQ:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008001 lvalue(state, left);
8002 arithmetic(state, left);
8003 eat(state, tok);
8004 right = read_expr(state, assignment_expr(state));
8005 arithmetic(state, right);
8006
8007 sign = is_signed(left->type);
8008 op = -1;
8009 switch(tok) {
8010 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
8011 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
8012 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008013 }
8014 def = write_expr(state, left,
8015 triple(state, op, left->type,
8016 read_expr(state, left), right));
8017 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008018 case TOK_PLUSEQ:
8019 lvalue(state, left);
8020 eat(state, TOK_PLUSEQ);
8021 def = write_expr(state, left,
8022 mk_add_expr(state, left, assignment_expr(state)));
8023 break;
8024 case TOK_MINUSEQ:
8025 lvalue(state, left);
8026 eat(state, TOK_MINUSEQ);
8027 def = write_expr(state, left,
8028 mk_sub_expr(state, left, assignment_expr(state)));
8029 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008030 case TOK_SLEQ:
8031 case TOK_SREQ:
8032 case TOK_ANDEQ:
8033 case TOK_XOREQ:
8034 case TOK_OREQ:
8035 lvalue(state, left);
8036 integral(state, left);
8037 eat(state, tok);
8038 right = read_expr(state, assignment_expr(state));
8039 integral(state, right);
8040 right = integral_promotion(state, right);
8041 sign = is_signed(left->type);
8042 op = -1;
8043 switch(tok) {
8044 case TOK_SLEQ: op = OP_SL; break;
8045 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
8046 case TOK_ANDEQ: op = OP_AND; break;
8047 case TOK_XOREQ: op = OP_XOR; break;
8048 case TOK_OREQ: op = OP_OR; break;
8049 }
8050 def = write_expr(state, left,
8051 triple(state, op, left->type,
8052 read_expr(state, left), right));
8053 break;
8054 }
8055 return def;
8056}
8057
8058static struct triple *expr(struct compile_state *state)
8059{
8060 struct triple *def;
8061 def = assignment_expr(state);
8062 while(peek(state) == TOK_COMMA) {
8063 struct triple *left, *right;
8064 left = def;
8065 eat(state, TOK_COMMA);
8066 right = assignment_expr(state);
8067 def = triple(state, OP_COMMA, right->type, left, right);
8068 }
8069 return def;
8070}
8071
8072static void expr_statement(struct compile_state *state, struct triple *first)
8073{
8074 if (peek(state) != TOK_SEMI) {
8075 flatten(state, first, expr(state));
8076 }
8077 eat(state, TOK_SEMI);
8078}
8079
8080static void if_statement(struct compile_state *state, struct triple *first)
8081{
8082 struct triple *test, *jmp1, *jmp2, *middle, *end;
8083
8084 jmp1 = jmp2 = middle = 0;
8085 eat(state, TOK_IF);
8086 eat(state, TOK_LPAREN);
8087 test = expr(state);
8088 bool(state, test);
8089 /* Cleanup and invert the test */
8090 test = lfalse_expr(state, read_expr(state, test));
8091 eat(state, TOK_RPAREN);
8092 /* Generate the needed pieces */
8093 middle = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008094 jmp1 = branch(state, middle, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008095 /* Thread the pieces together */
8096 flatten(state, first, test);
8097 flatten(state, first, jmp1);
8098 flatten(state, first, label(state));
8099 statement(state, first);
8100 if (peek(state) == TOK_ELSE) {
8101 eat(state, TOK_ELSE);
8102 /* Generate the rest of the pieces */
8103 end = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008104 jmp2 = branch(state, end, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008105 /* Thread them together */
8106 flatten(state, first, jmp2);
8107 flatten(state, first, middle);
8108 statement(state, first);
8109 flatten(state, first, end);
8110 }
8111 else {
8112 flatten(state, first, middle);
8113 }
8114}
8115
8116static void for_statement(struct compile_state *state, struct triple *first)
8117{
8118 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
8119 struct triple *label1, *label2, *label3;
8120 struct hash_entry *ident;
8121
8122 eat(state, TOK_FOR);
8123 eat(state, TOK_LPAREN);
8124 head = test = tail = jmp1 = jmp2 = 0;
8125 if (peek(state) != TOK_SEMI) {
8126 head = expr(state);
8127 }
8128 eat(state, TOK_SEMI);
8129 if (peek(state) != TOK_SEMI) {
8130 test = expr(state);
8131 bool(state, test);
8132 test = ltrue_expr(state, read_expr(state, test));
8133 }
8134 eat(state, TOK_SEMI);
8135 if (peek(state) != TOK_RPAREN) {
8136 tail = expr(state);
8137 }
8138 eat(state, TOK_RPAREN);
8139 /* Generate the needed pieces */
8140 label1 = label(state);
8141 label2 = label(state);
8142 label3 = label(state);
8143 if (test) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008144 jmp1 = branch(state, label3, 0);
8145 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008146 }
8147 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008148 jmp2 = branch(state, label1, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008149 }
8150 end = label(state);
8151 /* Remember where break and continue go */
8152 start_scope(state);
8153 ident = state->i_break;
8154 symbol(state, ident, &ident->sym_ident, end, end->type);
8155 ident = state->i_continue;
8156 symbol(state, ident, &ident->sym_ident, label2, label2->type);
8157 /* Now include the body */
8158 flatten(state, first, head);
8159 flatten(state, first, jmp1);
8160 flatten(state, first, label1);
8161 statement(state, first);
8162 flatten(state, first, label2);
8163 flatten(state, first, tail);
8164 flatten(state, first, label3);
8165 flatten(state, first, test);
8166 flatten(state, first, jmp2);
8167 flatten(state, first, end);
8168 /* Cleanup the break/continue scope */
8169 end_scope(state);
8170}
8171
8172static void while_statement(struct compile_state *state, struct triple *first)
8173{
8174 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
8175 struct hash_entry *ident;
8176 eat(state, TOK_WHILE);
8177 eat(state, TOK_LPAREN);
8178 test = expr(state);
8179 bool(state, test);
8180 test = ltrue_expr(state, read_expr(state, test));
8181 eat(state, TOK_RPAREN);
8182 /* Generate the needed pieces */
8183 label1 = label(state);
8184 label2 = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008185 jmp1 = branch(state, label2, 0);
8186 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008187 end = label(state);
8188 /* Remember where break and continue go */
8189 start_scope(state);
8190 ident = state->i_break;
8191 symbol(state, ident, &ident->sym_ident, end, end->type);
8192 ident = state->i_continue;
8193 symbol(state, ident, &ident->sym_ident, label2, label2->type);
8194 /* Thread them together */
8195 flatten(state, first, jmp1);
8196 flatten(state, first, label1);
8197 statement(state, first);
8198 flatten(state, first, label2);
8199 flatten(state, first, test);
8200 flatten(state, first, jmp2);
8201 flatten(state, first, end);
8202 /* Cleanup the break/continue scope */
8203 end_scope(state);
8204}
8205
8206static void do_statement(struct compile_state *state, struct triple *first)
8207{
8208 struct triple *label1, *label2, *test, *end;
8209 struct hash_entry *ident;
8210 eat(state, TOK_DO);
8211 /* Generate the needed pieces */
8212 label1 = label(state);
8213 label2 = label(state);
8214 end = label(state);
8215 /* Remember where break and continue go */
8216 start_scope(state);
8217 ident = state->i_break;
8218 symbol(state, ident, &ident->sym_ident, end, end->type);
8219 ident = state->i_continue;
8220 symbol(state, ident, &ident->sym_ident, label2, label2->type);
8221 /* Now include the body */
8222 flatten(state, first, label1);
8223 statement(state, first);
8224 /* Cleanup the break/continue scope */
8225 end_scope(state);
8226 /* Eat the rest of the loop */
8227 eat(state, TOK_WHILE);
8228 eat(state, TOK_LPAREN);
8229 test = read_expr(state, expr(state));
8230 bool(state, test);
8231 eat(state, TOK_RPAREN);
8232 eat(state, TOK_SEMI);
8233 /* Thread the pieces together */
8234 test = ltrue_expr(state, test);
8235 flatten(state, first, label2);
8236 flatten(state, first, test);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008237 flatten(state, first, branch(state, label1, test));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008238 flatten(state, first, end);
8239}
8240
8241
8242static void return_statement(struct compile_state *state, struct triple *first)
8243{
8244 struct triple *jmp, *mv, *dest, *var, *val;
8245 int last;
8246 eat(state, TOK_RETURN);
8247
8248#warning "FIXME implement a more general excess branch elimination"
8249 val = 0;
8250 /* If we have a return value do some more work */
8251 if (peek(state) != TOK_SEMI) {
8252 val = read_expr(state, expr(state));
8253 }
8254 eat(state, TOK_SEMI);
8255
8256 /* See if this last statement in a function */
8257 last = ((peek(state) == TOK_RBRACE) &&
8258 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
8259
8260 /* Find the return variable */
Eric Biederman0babc1c2003-05-09 02:39:00 +00008261 var = MISC(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008262 /* Find the return destination */
Eric Biederman0babc1c2003-05-09 02:39:00 +00008263 dest = RHS(state->main_function, 0)->prev;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008264 mv = jmp = 0;
8265 /* If needed generate a jump instruction */
8266 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008267 jmp = branch(state, dest, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008268 }
8269 /* If needed generate an assignment instruction */
8270 if (val) {
8271 mv = write_expr(state, var, val);
8272 }
8273 /* Now put the code together */
8274 if (mv) {
8275 flatten(state, first, mv);
8276 flatten(state, first, jmp);
8277 }
8278 else if (jmp) {
8279 flatten(state, first, jmp);
8280 }
8281}
8282
8283static void break_statement(struct compile_state *state, struct triple *first)
8284{
8285 struct triple *dest;
8286 eat(state, TOK_BREAK);
8287 eat(state, TOK_SEMI);
8288 if (!state->i_break->sym_ident) {
8289 error(state, 0, "break statement not within loop or switch");
8290 }
8291 dest = state->i_break->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008292 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008293}
8294
8295static void continue_statement(struct compile_state *state, struct triple *first)
8296{
8297 struct triple *dest;
8298 eat(state, TOK_CONTINUE);
8299 eat(state, TOK_SEMI);
8300 if (!state->i_continue->sym_ident) {
8301 error(state, 0, "continue statement outside of a loop");
8302 }
8303 dest = state->i_continue->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008304 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008305}
8306
8307static void goto_statement(struct compile_state *state, struct triple *first)
8308{
Eric Biederman153ea352003-06-20 14:43:20 +00008309 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008310 eat(state, TOK_GOTO);
8311 eat(state, TOK_IDENT);
Eric Biederman153ea352003-06-20 14:43:20 +00008312 ident = state->token[0].ident;
8313 if (!ident->sym_label) {
8314 /* If this is a forward branch allocate the label now,
8315 * it will be flattend in the appropriate location later.
8316 */
8317 struct triple *ins;
8318 ins = label(state);
8319 label_symbol(state, ident, ins);
8320 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008321 eat(state, TOK_SEMI);
Eric Biederman153ea352003-06-20 14:43:20 +00008322
8323 flatten(state, first, branch(state, ident->sym_label->def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008324}
8325
8326static void labeled_statement(struct compile_state *state, struct triple *first)
8327{
Eric Biederman153ea352003-06-20 14:43:20 +00008328 struct triple *ins;
8329 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008330 eat(state, TOK_IDENT);
Eric Biederman153ea352003-06-20 14:43:20 +00008331
8332 ident = state->token[0].ident;
8333 if (ident->sym_label && ident->sym_label->def) {
8334 ins = ident->sym_label->def;
8335 put_occurance(ins->occurance);
8336 ins->occurance = new_occurance(state);
8337 }
8338 else {
8339 ins = label(state);
8340 label_symbol(state, ident, ins);
8341 }
8342 if (ins->id & TRIPLE_FLAG_FLATTENED) {
8343 error(state, 0, "label %s already defined", ident->name);
8344 }
8345 flatten(state, first, ins);
8346
Eric Biedermanb138ac82003-04-22 18:44:01 +00008347 eat(state, TOK_COLON);
8348 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008349}
8350
8351static void switch_statement(struct compile_state *state, struct triple *first)
8352{
Eric Biederman83b991a2003-10-11 06:20:25 +00008353 struct triple *value, *top, *end, *dbranch;
8354 struct hash_entry *ident;
8355
8356 /* See if we have a valid switch statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008357 eat(state, TOK_SWITCH);
8358 eat(state, TOK_LPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +00008359 value = expr(state);
8360 integral(state, value);
8361 value = read_expr(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008362 eat(state, TOK_RPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +00008363 /* Generate the needed pieces */
8364 top = label(state);
8365 end = label(state);
8366 dbranch = branch(state, end, 0);
8367 /* Remember where case branches and break goes */
8368 start_scope(state);
8369 ident = state->i_switch;
8370 symbol(state, ident, &ident->sym_ident, value, value->type);
8371 ident = state->i_case;
8372 symbol(state, ident, &ident->sym_ident, top, top->type);
8373 ident = state->i_break;
8374 symbol(state, ident, &ident->sym_ident, end, end->type);
8375 ident = state->i_default;
8376 symbol(state, ident, &ident->sym_ident, dbranch, dbranch->type);
8377 /* Thread them together */
8378 flatten(state, first, value);
8379 flatten(state, first, top);
8380 flatten(state, first, dbranch);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008381 statement(state, first);
Eric Biederman83b991a2003-10-11 06:20:25 +00008382 flatten(state, first, end);
8383 /* Cleanup the switch scope */
8384 end_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008385}
8386
8387static void case_statement(struct compile_state *state, struct triple *first)
8388{
Eric Biederman83b991a2003-10-11 06:20:25 +00008389 struct triple *cvalue, *dest, *test, *jmp;
8390 struct triple *ptr, *value, *top, *dbranch;
8391
8392 /* See if w have a valid case statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008393 eat(state, TOK_CASE);
Eric Biederman83b991a2003-10-11 06:20:25 +00008394 cvalue = constant_expr(state);
8395 integral(state, cvalue);
8396 if (cvalue->op != OP_INTCONST) {
8397 error(state, 0, "integer constant expected");
8398 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008399 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +00008400 if (!state->i_case->sym_ident) {
8401 error(state, 0, "case statement not within a switch");
8402 }
8403
8404 /* Lookup the interesting pieces */
8405 top = state->i_case->sym_ident->def;
8406 value = state->i_switch->sym_ident->def;
8407 dbranch = state->i_default->sym_ident->def;
8408
8409 /* See if this case label has already been used */
8410 for(ptr = top; ptr != dbranch; ptr = ptr->next) {
8411 if (ptr->op != OP_EQ) {
8412 continue;
8413 }
8414 if (RHS(ptr, 1)->u.cval == cvalue->u.cval) {
8415 error(state, 0, "duplicate case %d statement",
8416 cvalue->u.cval);
8417 }
8418 }
8419 /* Generate the needed pieces */
8420 dest = label(state);
8421 test = triple(state, OP_EQ, &int_type, value, cvalue);
8422 jmp = branch(state, dest, test);
8423 /* Thread the pieces together */
8424 flatten(state, dbranch, test);
8425 flatten(state, dbranch, jmp);
8426 flatten(state, dbranch, label(state));
8427 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008428 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008429}
8430
8431static void default_statement(struct compile_state *state, struct triple *first)
8432{
Eric Biederman83b991a2003-10-11 06:20:25 +00008433 struct triple *dest;
8434 struct triple *dbranch, *end;
8435
8436 /* See if we have a valid default statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008437 eat(state, TOK_DEFAULT);
8438 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +00008439
8440 if (!state->i_case->sym_ident) {
8441 error(state, 0, "default statement not within a switch");
8442 }
8443
8444 /* Lookup the interesting pieces */
8445 dbranch = state->i_default->sym_ident->def;
8446 end = state->i_break->sym_ident->def;
8447
8448 /* See if a default statement has already happened */
8449 if (TARG(dbranch, 0) != end) {
8450 error(state, 0, "duplicate default statement");
8451 }
8452
8453 /* Generate the needed pieces */
8454 dest = label(state);
8455
8456 /* Thread the pieces together */
8457 TARG(dbranch, 0) = dest;
8458 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008459 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008460}
8461
8462static void asm_statement(struct compile_state *state, struct triple *first)
8463{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008464 struct asm_info *info;
8465 struct {
8466 struct triple *constraint;
8467 struct triple *expr;
8468 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
8469 struct triple *def, *asm_str;
8470 int out, in, clobbers, more, colons, i;
8471
8472 eat(state, TOK_ASM);
8473 /* For now ignore the qualifiers */
8474 switch(peek(state)) {
8475 case TOK_CONST:
8476 eat(state, TOK_CONST);
8477 break;
8478 case TOK_VOLATILE:
8479 eat(state, TOK_VOLATILE);
8480 break;
8481 }
8482 eat(state, TOK_LPAREN);
8483 asm_str = string_constant(state);
8484
8485 colons = 0;
8486 out = in = clobbers = 0;
8487 /* Outputs */
8488 if ((colons == 0) && (peek(state) == TOK_COLON)) {
8489 eat(state, TOK_COLON);
8490 colons++;
8491 more = (peek(state) == TOK_LIT_STRING);
8492 while(more) {
8493 struct triple *var;
8494 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008495 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008496 more = 0;
8497 if (out > MAX_LHS) {
8498 error(state, 0, "Maximum output count exceeded.");
8499 }
8500 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +00008501 str = constraint->u.blob;
8502 if (str[0] != '=') {
8503 error(state, 0, "Output constraint does not start with =");
8504 }
8505 constraint->u.blob = str + 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008506 eat(state, TOK_LPAREN);
8507 var = conditional_expr(state);
8508 eat(state, TOK_RPAREN);
8509
8510 lvalue(state, var);
8511 out_param[out].constraint = constraint;
8512 out_param[out].expr = var;
8513 if (peek(state) == TOK_COMMA) {
8514 eat(state, TOK_COMMA);
8515 more = 1;
8516 }
8517 out++;
8518 }
8519 }
8520 /* Inputs */
8521 if ((colons == 1) && (peek(state) == TOK_COLON)) {
8522 eat(state, TOK_COLON);
8523 colons++;
8524 more = (peek(state) == TOK_LIT_STRING);
8525 while(more) {
8526 struct triple *val;
8527 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008528 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008529 more = 0;
8530 if (in > MAX_RHS) {
8531 error(state, 0, "Maximum input count exceeded.");
8532 }
8533 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +00008534 str = constraint->u.blob;
8535 if (digitp(str[0] && str[1] == '\0')) {
8536 int val;
8537 val = digval(str[0]);
8538 if ((val < 0) || (val >= out)) {
8539 error(state, 0, "Invalid input constraint %d", val);
8540 }
8541 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008542 eat(state, TOK_LPAREN);
8543 val = conditional_expr(state);
8544 eat(state, TOK_RPAREN);
8545
8546 in_param[in].constraint = constraint;
8547 in_param[in].expr = val;
8548 if (peek(state) == TOK_COMMA) {
8549 eat(state, TOK_COMMA);
8550 more = 1;
8551 }
8552 in++;
8553 }
8554 }
8555
8556 /* Clobber */
8557 if ((colons == 2) && (peek(state) == TOK_COLON)) {
8558 eat(state, TOK_COLON);
8559 colons++;
8560 more = (peek(state) == TOK_LIT_STRING);
8561 while(more) {
8562 struct triple *clobber;
8563 more = 0;
8564 if ((clobbers + out) > MAX_LHS) {
8565 error(state, 0, "Maximum clobber limit exceeded.");
8566 }
8567 clobber = string_constant(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008568
8569 clob_param[clobbers].constraint = clobber;
8570 if (peek(state) == TOK_COMMA) {
8571 eat(state, TOK_COMMA);
8572 more = 1;
8573 }
8574 clobbers++;
8575 }
8576 }
8577 eat(state, TOK_RPAREN);
8578 eat(state, TOK_SEMI);
8579
8580
8581 info = xcmalloc(sizeof(*info), "asm_info");
8582 info->str = asm_str->u.blob;
8583 free_triple(state, asm_str);
8584
8585 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
8586 def->u.ainfo = info;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008587
8588 /* Find the register constraints */
8589 for(i = 0; i < out; i++) {
8590 struct triple *constraint;
8591 constraint = out_param[i].constraint;
8592 info->tmpl.lhs[i] = arch_reg_constraint(state,
8593 out_param[i].expr->type, constraint->u.blob);
8594 free_triple(state, constraint);
8595 }
8596 for(; i - out < clobbers; i++) {
8597 struct triple *constraint;
8598 constraint = clob_param[i - out].constraint;
8599 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
8600 free_triple(state, constraint);
8601 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008602 for(i = 0; i < in; i++) {
8603 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008604 const char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008605 constraint = in_param[i].constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008606 str = constraint->u.blob;
8607 if (digitp(str[0]) && str[1] == '\0') {
8608 struct reg_info cinfo;
8609 int val;
8610 val = digval(str[0]);
8611 cinfo.reg = info->tmpl.lhs[val].reg;
8612 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
8613 cinfo.regcm &= info->tmpl.lhs[val].regcm;
8614 if (cinfo.reg == REG_UNSET) {
8615 cinfo.reg = REG_VIRT0 + val;
8616 }
8617 if (cinfo.regcm == 0) {
8618 error(state, 0, "No registers for %d", val);
8619 }
8620 info->tmpl.lhs[val] = cinfo;
8621 info->tmpl.rhs[i] = cinfo;
8622
8623 } else {
8624 info->tmpl.rhs[i] = arch_reg_constraint(state,
8625 in_param[i].expr->type, str);
8626 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008627 free_triple(state, constraint);
8628 }
Eric Biederman8d9c1232003-06-17 08:42:17 +00008629
8630 /* Now build the helper expressions */
8631 for(i = 0; i < in; i++) {
8632 RHS(def, i) = read_expr(state,in_param[i].expr);
8633 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008634 flatten(state, first, def);
Eric Biedermane058a1e2003-07-12 01:21:31 +00008635 for(i = 0; i < (out + clobbers); i++) {
8636 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008637 struct triple *piece;
Eric Biedermane058a1e2003-07-12 01:21:31 +00008638 type = (i < out)? out_param[i].expr->type : &void_type;
8639 piece = triple(state, OP_PIECE, type, def, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008640 piece->u.cval = i;
8641 LHS(def, i) = piece;
8642 flatten(state, first, piece);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008643 }
Eric Biedermane058a1e2003-07-12 01:21:31 +00008644 /* And write the helpers to their destinations */
8645 for(i = 0; i < out; i++) {
8646 struct triple *piece;
8647 piece = LHS(def, i);
8648 flatten(state, first,
8649 write_expr(state, out_param[i].expr, piece));
8650 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008651}
8652
8653
8654static int isdecl(int tok)
8655{
8656 switch(tok) {
8657 case TOK_AUTO:
8658 case TOK_REGISTER:
8659 case TOK_STATIC:
8660 case TOK_EXTERN:
8661 case TOK_TYPEDEF:
8662 case TOK_CONST:
8663 case TOK_RESTRICT:
8664 case TOK_VOLATILE:
8665 case TOK_VOID:
8666 case TOK_CHAR:
8667 case TOK_SHORT:
8668 case TOK_INT:
8669 case TOK_LONG:
8670 case TOK_FLOAT:
8671 case TOK_DOUBLE:
8672 case TOK_SIGNED:
8673 case TOK_UNSIGNED:
8674 case TOK_STRUCT:
8675 case TOK_UNION:
8676 case TOK_ENUM:
8677 case TOK_TYPE_NAME: /* typedef name */
8678 return 1;
8679 default:
8680 return 0;
8681 }
8682}
8683
8684static void compound_statement(struct compile_state *state, struct triple *first)
8685{
8686 eat(state, TOK_LBRACE);
8687 start_scope(state);
8688
8689 /* statement-list opt */
8690 while (peek(state) != TOK_RBRACE) {
8691 statement(state, first);
8692 }
8693 end_scope(state);
8694 eat(state, TOK_RBRACE);
8695}
8696
8697static void statement(struct compile_state *state, struct triple *first)
8698{
8699 int tok;
8700 tok = peek(state);
8701 if (tok == TOK_LBRACE) {
8702 compound_statement(state, first);
8703 }
8704 else if (tok == TOK_IF) {
8705 if_statement(state, first);
8706 }
8707 else if (tok == TOK_FOR) {
8708 for_statement(state, first);
8709 }
8710 else if (tok == TOK_WHILE) {
8711 while_statement(state, first);
8712 }
8713 else if (tok == TOK_DO) {
8714 do_statement(state, first);
8715 }
8716 else if (tok == TOK_RETURN) {
8717 return_statement(state, first);
8718 }
8719 else if (tok == TOK_BREAK) {
8720 break_statement(state, first);
8721 }
8722 else if (tok == TOK_CONTINUE) {
8723 continue_statement(state, first);
8724 }
8725 else if (tok == TOK_GOTO) {
8726 goto_statement(state, first);
8727 }
8728 else if (tok == TOK_SWITCH) {
8729 switch_statement(state, first);
8730 }
8731 else if (tok == TOK_ASM) {
8732 asm_statement(state, first);
8733 }
8734 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
8735 labeled_statement(state, first);
8736 }
8737 else if (tok == TOK_CASE) {
8738 case_statement(state, first);
8739 }
8740 else if (tok == TOK_DEFAULT) {
8741 default_statement(state, first);
8742 }
8743 else if (isdecl(tok)) {
8744 /* This handles C99 intermixing of statements and decls */
8745 decl(state, first);
8746 }
8747 else {
8748 expr_statement(state, first);
8749 }
8750}
8751
8752static struct type *param_decl(struct compile_state *state)
8753{
8754 struct type *type;
8755 struct hash_entry *ident;
8756 /* Cheat so the declarator will know we are not global */
8757 start_scope(state);
8758 ident = 0;
8759 type = decl_specifiers(state);
8760 type = declarator(state, type, &ident, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008761 type->field_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008762 end_scope(state);
8763 return type;
8764}
8765
8766static struct type *param_type_list(struct compile_state *state, struct type *type)
8767{
8768 struct type *ftype, **next;
8769 ftype = new_type(TYPE_FUNCTION, type, param_decl(state));
8770 next = &ftype->right;
8771 while(peek(state) == TOK_COMMA) {
8772 eat(state, TOK_COMMA);
8773 if (peek(state) == TOK_DOTS) {
8774 eat(state, TOK_DOTS);
8775 error(state, 0, "variadic functions not supported");
8776 }
8777 else {
8778 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
8779 next = &((*next)->right);
8780 }
8781 }
8782 return ftype;
8783}
8784
8785
8786static struct type *type_name(struct compile_state *state)
8787{
8788 struct type *type;
8789 type = specifier_qualifier_list(state);
8790 /* abstract-declarator (may consume no tokens) */
8791 type = declarator(state, type, 0, 0);
8792 return type;
8793}
8794
8795static struct type *direct_declarator(
8796 struct compile_state *state, struct type *type,
8797 struct hash_entry **ident, int need_ident)
8798{
8799 struct type *outer;
8800 int op;
8801 outer = 0;
8802 arrays_complete(state, type);
8803 switch(peek(state)) {
8804 case TOK_IDENT:
8805 eat(state, TOK_IDENT);
8806 if (!ident) {
8807 error(state, 0, "Unexpected identifier found");
8808 }
8809 /* The name of what we are declaring */
8810 *ident = state->token[0].ident;
8811 break;
8812 case TOK_LPAREN:
8813 eat(state, TOK_LPAREN);
8814 outer = declarator(state, type, ident, need_ident);
8815 eat(state, TOK_RPAREN);
8816 break;
8817 default:
8818 if (need_ident) {
8819 error(state, 0, "Identifier expected");
8820 }
8821 break;
8822 }
8823 do {
8824 op = 1;
8825 arrays_complete(state, type);
8826 switch(peek(state)) {
8827 case TOK_LPAREN:
8828 eat(state, TOK_LPAREN);
8829 type = param_type_list(state, type);
8830 eat(state, TOK_RPAREN);
8831 break;
8832 case TOK_LBRACKET:
8833 {
8834 unsigned int qualifiers;
8835 struct triple *value;
8836 value = 0;
8837 eat(state, TOK_LBRACKET);
8838 if (peek(state) != TOK_RBRACKET) {
8839 value = constant_expr(state);
8840 integral(state, value);
8841 }
8842 eat(state, TOK_RBRACKET);
8843
8844 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
8845 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
8846 if (value) {
8847 type->elements = value->u.cval;
8848 free_triple(state, value);
8849 } else {
8850 type->elements = ELEMENT_COUNT_UNSPECIFIED;
8851 op = 0;
8852 }
8853 }
8854 break;
8855 default:
8856 op = 0;
8857 break;
8858 }
8859 } while(op);
8860 if (outer) {
8861 struct type *inner;
8862 arrays_complete(state, type);
8863 FINISHME();
8864 for(inner = outer; inner->left; inner = inner->left)
8865 ;
8866 inner->left = type;
8867 type = outer;
8868 }
8869 return type;
8870}
8871
8872static struct type *declarator(
8873 struct compile_state *state, struct type *type,
8874 struct hash_entry **ident, int need_ident)
8875{
8876 while(peek(state) == TOK_STAR) {
8877 eat(state, TOK_STAR);
8878 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
8879 }
8880 type = direct_declarator(state, type, ident, need_ident);
8881 return type;
8882}
8883
8884
8885static struct type *typedef_name(
8886 struct compile_state *state, unsigned int specifiers)
8887{
8888 struct hash_entry *ident;
8889 struct type *type;
8890 eat(state, TOK_TYPE_NAME);
8891 ident = state->token[0].ident;
8892 type = ident->sym_ident->type;
8893 specifiers |= type->type & QUAL_MASK;
8894 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
8895 (type->type & (STOR_MASK | QUAL_MASK))) {
8896 type = clone_type(specifiers, type);
8897 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008898 return type;
8899}
8900
8901static struct type *enum_specifier(
Eric Biederman83b991a2003-10-11 06:20:25 +00008902 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008903{
Eric Biederman83b991a2003-10-11 06:20:25 +00008904 struct hash_entry *ident;
8905 ulong_t base;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008906 int tok;
Eric Biederman83b991a2003-10-11 06:20:25 +00008907 struct type *enum_type;
8908 enum_type = 0;
8909 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008910 eat(state, TOK_ENUM);
8911 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +00008912 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
8913 eat(state, tok);
8914 ident = state->token[0].ident;
8915
Eric Biedermanb138ac82003-04-22 18:44:01 +00008916 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008917 base = 0;
8918 if (!ident || (peek(state) == TOK_LBRACE)) {
8919 struct type **next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008920 eat(state, TOK_LBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +00008921 enum_type = new_type(TYPE_ENUM | spec, 0, 0);
8922 enum_type->type_ident = ident;
8923 next = &enum_type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008924 do {
Eric Biederman83b991a2003-10-11 06:20:25 +00008925 struct hash_entry *eident;
8926 struct triple *value;
8927 struct type *entry;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008928 eat(state, TOK_IDENT);
Eric Biederman83b991a2003-10-11 06:20:25 +00008929 eident = state->token[0].ident;
8930 if (eident->sym_ident) {
8931 error(state, 0, "%s already declared",
8932 eident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008933 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008934 eident->tok = TOK_ENUM_CONST;
8935 if (peek(state) == TOK_EQ) {
8936 struct triple *val;
8937 eat(state, TOK_EQ);
8938 val = constant_expr(state);
8939 integral(state, val);
8940 base = val->u.cval;
8941 }
8942 value = int_const(state, &int_type, base);
8943 symbol(state, eident, &eident->sym_ident, value, &int_type);
8944 entry = new_type(TYPE_LIST, 0, 0);
8945 entry->field_ident = eident;
8946 *next = entry;
8947 next = &entry->right;
8948 base += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008949 if (peek(state) == TOK_COMMA) {
8950 eat(state, TOK_COMMA);
8951 }
8952 } while(peek(state) != TOK_RBRACE);
8953 eat(state, TOK_RBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +00008954 if (ident) {
8955 symbol(state, ident, &ident->sym_tag, 0, enum_type);
8956 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008957 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008958 if (ident && ident->sym_tag &&
8959 ident->sym_tag->type &&
8960 ((ident->sym_tag->type->type & TYPE_MASK) == TYPE_ENUM)) {
8961 enum_type = clone_type(spec, ident->sym_tag->type);
8962 }
8963 else if (ident && !enum_type) {
8964 error(state, 0, "enum %s undeclared", ident->name);
8965 }
8966 return enum_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008967}
8968
Eric Biedermanb138ac82003-04-22 18:44:01 +00008969static struct type *struct_declarator(
8970 struct compile_state *state, struct type *type, struct hash_entry **ident)
8971{
8972 int tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008973 tok = peek(state);
8974 if (tok != TOK_COLON) {
8975 type = declarator(state, type, ident, 1);
8976 }
8977 if ((tok == TOK_COLON) || (peek(state) == TOK_COLON)) {
Eric Biederman530b5192003-07-01 10:05:30 +00008978 struct triple *value;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008979 eat(state, TOK_COLON);
Eric Biederman530b5192003-07-01 10:05:30 +00008980 value = constant_expr(state);
8981#warning "FIXME implement bitfields to reduce register usage"
8982 error(state, 0, "bitfields not yet implemented");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008983 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008984 return type;
8985}
Eric Biedermanb138ac82003-04-22 18:44:01 +00008986
8987static struct type *struct_or_union_specifier(
Eric Biederman00443072003-06-24 12:34:45 +00008988 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008989{
Eric Biederman0babc1c2003-05-09 02:39:00 +00008990 struct type *struct_type;
8991 struct hash_entry *ident;
8992 unsigned int type_join;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008993 int tok;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008994 struct_type = 0;
8995 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008996 switch(peek(state)) {
8997 case TOK_STRUCT:
8998 eat(state, TOK_STRUCT);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008999 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009000 break;
9001 case TOK_UNION:
9002 eat(state, TOK_UNION);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009003 type_join = TYPE_OVERLAP;
9004 error(state, 0, "unions not yet supported\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00009005 break;
9006 default:
9007 eat(state, TOK_STRUCT);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009008 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009009 break;
9010 }
9011 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +00009012 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009013 eat(state, tok);
9014 ident = state->token[0].ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009015 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009016 if (!ident || (peek(state) == TOK_LBRACE)) {
9017 ulong_t elements;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00009018 struct type **next;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009019 elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009020 eat(state, TOK_LBRACE);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00009021 next = &struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009022 do {
9023 struct type *base_type;
9024 int done;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009025 base_type = specifier_qualifier_list(state);
9026 do {
9027 struct type *type;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009028 struct hash_entry *fident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009029 done = 1;
Eric Biederman530b5192003-07-01 10:05:30 +00009030 type = struct_declarator(state, base_type, &fident);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009031 elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009032 if (peek(state) == TOK_COMMA) {
9033 done = 0;
9034 eat(state, TOK_COMMA);
9035 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009036 type = clone_type(0, type);
9037 type->field_ident = fident;
9038 if (*next) {
9039 *next = new_type(type_join, *next, type);
9040 next = &((*next)->right);
9041 } else {
9042 *next = type;
9043 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009044 } while(!done);
9045 eat(state, TOK_SEMI);
9046 } while(peek(state) != TOK_RBRACE);
9047 eat(state, TOK_RBRACE);
Eric Biederman00443072003-06-24 12:34:45 +00009048 struct_type = new_type(TYPE_STRUCT | spec, struct_type, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009049 struct_type->type_ident = ident;
9050 struct_type->elements = elements;
Eric Biedermane058a1e2003-07-12 01:21:31 +00009051 if (ident) {
Eric Biederman83b991a2003-10-11 06:20:25 +00009052 symbol(state, ident, &ident->sym_tag, 0, struct_type);
Eric Biedermane058a1e2003-07-12 01:21:31 +00009053 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009054 }
Eric Biederman83b991a2003-10-11 06:20:25 +00009055 if (ident && ident->sym_tag &&
9056 ident->sym_tag->type &&
9057 ((ident->sym_tag->type->type & TYPE_MASK) == TYPE_STRUCT)) {
9058 struct_type = clone_type(spec, ident->sym_tag->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009059 }
Eric Biederman83b991a2003-10-11 06:20:25 +00009060 else if (ident && !struct_type) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009061 error(state, 0, "struct %s undeclared", ident->name);
9062 }
9063 return struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009064}
9065
9066static unsigned int storage_class_specifier_opt(struct compile_state *state)
9067{
9068 unsigned int specifiers;
9069 switch(peek(state)) {
9070 case TOK_AUTO:
9071 eat(state, TOK_AUTO);
9072 specifiers = STOR_AUTO;
9073 break;
9074 case TOK_REGISTER:
9075 eat(state, TOK_REGISTER);
9076 specifiers = STOR_REGISTER;
9077 break;
9078 case TOK_STATIC:
9079 eat(state, TOK_STATIC);
9080 specifiers = STOR_STATIC;
9081 break;
9082 case TOK_EXTERN:
9083 eat(state, TOK_EXTERN);
9084 specifiers = STOR_EXTERN;
9085 break;
9086 case TOK_TYPEDEF:
9087 eat(state, TOK_TYPEDEF);
9088 specifiers = STOR_TYPEDEF;
9089 break;
9090 default:
9091 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
9092 specifiers = STOR_STATIC;
9093 }
9094 else {
9095 specifiers = STOR_AUTO;
9096 }
9097 }
9098 return specifiers;
9099}
9100
9101static unsigned int function_specifier_opt(struct compile_state *state)
9102{
9103 /* Ignore the inline keyword */
9104 unsigned int specifiers;
9105 specifiers = 0;
9106 switch(peek(state)) {
9107 case TOK_INLINE:
9108 eat(state, TOK_INLINE);
9109 specifiers = STOR_INLINE;
9110 }
9111 return specifiers;
9112}
9113
9114static unsigned int type_qualifiers(struct compile_state *state)
9115{
9116 unsigned int specifiers;
9117 int done;
9118 done = 0;
9119 specifiers = QUAL_NONE;
9120 do {
9121 switch(peek(state)) {
9122 case TOK_CONST:
9123 eat(state, TOK_CONST);
9124 specifiers = QUAL_CONST;
9125 break;
9126 case TOK_VOLATILE:
9127 eat(state, TOK_VOLATILE);
9128 specifiers = QUAL_VOLATILE;
9129 break;
9130 case TOK_RESTRICT:
9131 eat(state, TOK_RESTRICT);
9132 specifiers = QUAL_RESTRICT;
9133 break;
9134 default:
9135 done = 1;
9136 break;
9137 }
9138 } while(!done);
9139 return specifiers;
9140}
9141
9142static struct type *type_specifier(
9143 struct compile_state *state, unsigned int spec)
9144{
9145 struct type *type;
9146 type = 0;
9147 switch(peek(state)) {
9148 case TOK_VOID:
9149 eat(state, TOK_VOID);
9150 type = new_type(TYPE_VOID | spec, 0, 0);
9151 break;
9152 case TOK_CHAR:
9153 eat(state, TOK_CHAR);
9154 type = new_type(TYPE_CHAR | spec, 0, 0);
9155 break;
9156 case TOK_SHORT:
9157 eat(state, TOK_SHORT);
9158 if (peek(state) == TOK_INT) {
9159 eat(state, TOK_INT);
9160 }
9161 type = new_type(TYPE_SHORT | spec, 0, 0);
9162 break;
9163 case TOK_INT:
9164 eat(state, TOK_INT);
9165 type = new_type(TYPE_INT | spec, 0, 0);
9166 break;
9167 case TOK_LONG:
9168 eat(state, TOK_LONG);
9169 switch(peek(state)) {
9170 case TOK_LONG:
9171 eat(state, TOK_LONG);
9172 error(state, 0, "long long not supported");
9173 break;
9174 case TOK_DOUBLE:
9175 eat(state, TOK_DOUBLE);
9176 error(state, 0, "long double not supported");
9177 break;
9178 case TOK_INT:
9179 eat(state, TOK_INT);
9180 type = new_type(TYPE_LONG | spec, 0, 0);
9181 break;
9182 default:
9183 type = new_type(TYPE_LONG | spec, 0, 0);
9184 break;
9185 }
9186 break;
9187 case TOK_FLOAT:
9188 eat(state, TOK_FLOAT);
9189 error(state, 0, "type float not supported");
9190 break;
9191 case TOK_DOUBLE:
9192 eat(state, TOK_DOUBLE);
9193 error(state, 0, "type double not supported");
9194 break;
9195 case TOK_SIGNED:
9196 eat(state, TOK_SIGNED);
9197 switch(peek(state)) {
9198 case TOK_LONG:
9199 eat(state, TOK_LONG);
9200 switch(peek(state)) {
9201 case TOK_LONG:
9202 eat(state, TOK_LONG);
9203 error(state, 0, "type long long not supported");
9204 break;
9205 case TOK_INT:
9206 eat(state, TOK_INT);
9207 type = new_type(TYPE_LONG | spec, 0, 0);
9208 break;
9209 default:
9210 type = new_type(TYPE_LONG | spec, 0, 0);
9211 break;
9212 }
9213 break;
9214 case TOK_INT:
9215 eat(state, TOK_INT);
9216 type = new_type(TYPE_INT | spec, 0, 0);
9217 break;
9218 case TOK_SHORT:
9219 eat(state, TOK_SHORT);
9220 type = new_type(TYPE_SHORT | spec, 0, 0);
9221 break;
9222 case TOK_CHAR:
9223 eat(state, TOK_CHAR);
9224 type = new_type(TYPE_CHAR | spec, 0, 0);
9225 break;
9226 default:
9227 type = new_type(TYPE_INT | spec, 0, 0);
9228 break;
9229 }
9230 break;
9231 case TOK_UNSIGNED:
9232 eat(state, TOK_UNSIGNED);
9233 switch(peek(state)) {
9234 case TOK_LONG:
9235 eat(state, TOK_LONG);
9236 switch(peek(state)) {
9237 case TOK_LONG:
9238 eat(state, TOK_LONG);
9239 error(state, 0, "unsigned long long not supported");
9240 break;
9241 case TOK_INT:
9242 eat(state, TOK_INT);
9243 type = new_type(TYPE_ULONG | spec, 0, 0);
9244 break;
9245 default:
9246 type = new_type(TYPE_ULONG | spec, 0, 0);
9247 break;
9248 }
9249 break;
9250 case TOK_INT:
9251 eat(state, TOK_INT);
9252 type = new_type(TYPE_UINT | spec, 0, 0);
9253 break;
9254 case TOK_SHORT:
9255 eat(state, TOK_SHORT);
9256 type = new_type(TYPE_USHORT | spec, 0, 0);
9257 break;
9258 case TOK_CHAR:
9259 eat(state, TOK_CHAR);
9260 type = new_type(TYPE_UCHAR | spec, 0, 0);
9261 break;
9262 default:
9263 type = new_type(TYPE_UINT | spec, 0, 0);
9264 break;
9265 }
9266 break;
9267 /* struct or union specifier */
9268 case TOK_STRUCT:
9269 case TOK_UNION:
9270 type = struct_or_union_specifier(state, spec);
9271 break;
9272 /* enum-spefifier */
9273 case TOK_ENUM:
9274 type = enum_specifier(state, spec);
9275 break;
9276 /* typedef name */
9277 case TOK_TYPE_NAME:
9278 type = typedef_name(state, spec);
9279 break;
9280 default:
9281 error(state, 0, "bad type specifier %s",
9282 tokens[peek(state)]);
9283 break;
9284 }
9285 return type;
9286}
9287
9288static int istype(int tok)
9289{
9290 switch(tok) {
9291 case TOK_CONST:
9292 case TOK_RESTRICT:
9293 case TOK_VOLATILE:
9294 case TOK_VOID:
9295 case TOK_CHAR:
9296 case TOK_SHORT:
9297 case TOK_INT:
9298 case TOK_LONG:
9299 case TOK_FLOAT:
9300 case TOK_DOUBLE:
9301 case TOK_SIGNED:
9302 case TOK_UNSIGNED:
9303 case TOK_STRUCT:
9304 case TOK_UNION:
9305 case TOK_ENUM:
9306 case TOK_TYPE_NAME:
9307 return 1;
9308 default:
9309 return 0;
9310 }
9311}
9312
9313
9314static struct type *specifier_qualifier_list(struct compile_state *state)
9315{
9316 struct type *type;
9317 unsigned int specifiers = 0;
9318
9319 /* type qualifiers */
9320 specifiers |= type_qualifiers(state);
9321
9322 /* type specifier */
9323 type = type_specifier(state, specifiers);
9324
9325 return type;
9326}
9327
9328static int isdecl_specifier(int tok)
9329{
9330 switch(tok) {
9331 /* storage class specifier */
9332 case TOK_AUTO:
9333 case TOK_REGISTER:
9334 case TOK_STATIC:
9335 case TOK_EXTERN:
9336 case TOK_TYPEDEF:
9337 /* type qualifier */
9338 case TOK_CONST:
9339 case TOK_RESTRICT:
9340 case TOK_VOLATILE:
9341 /* type specifiers */
9342 case TOK_VOID:
9343 case TOK_CHAR:
9344 case TOK_SHORT:
9345 case TOK_INT:
9346 case TOK_LONG:
9347 case TOK_FLOAT:
9348 case TOK_DOUBLE:
9349 case TOK_SIGNED:
9350 case TOK_UNSIGNED:
9351 /* struct or union specifier */
9352 case TOK_STRUCT:
9353 case TOK_UNION:
9354 /* enum-spefifier */
9355 case TOK_ENUM:
9356 /* typedef name */
9357 case TOK_TYPE_NAME:
9358 /* function specifiers */
9359 case TOK_INLINE:
9360 return 1;
9361 default:
9362 return 0;
9363 }
9364}
9365
9366static struct type *decl_specifiers(struct compile_state *state)
9367{
9368 struct type *type;
9369 unsigned int specifiers;
9370 /* I am overly restrictive in the arragement of specifiers supported.
9371 * C is overly flexible in this department it makes interpreting
9372 * the parse tree difficult.
9373 */
9374 specifiers = 0;
9375
9376 /* storage class specifier */
9377 specifiers |= storage_class_specifier_opt(state);
9378
9379 /* function-specifier */
9380 specifiers |= function_specifier_opt(state);
9381
9382 /* type qualifier */
9383 specifiers |= type_qualifiers(state);
9384
9385 /* type specifier */
9386 type = type_specifier(state, specifiers);
9387 return type;
9388}
9389
Eric Biederman00443072003-06-24 12:34:45 +00009390struct field_info {
9391 struct type *type;
9392 size_t offset;
9393};
9394
9395static struct field_info designator(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +00009396{
9397 int tok;
Eric Biederman00443072003-06-24 12:34:45 +00009398 struct field_info info;
9399 info.offset = ~0U;
9400 info.type = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009401 do {
9402 switch(peek(state)) {
9403 case TOK_LBRACKET:
9404 {
9405 struct triple *value;
Eric Biederman00443072003-06-24 12:34:45 +00009406 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
9407 error(state, 0, "Array designator not in array initializer");
9408 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009409 eat(state, TOK_LBRACKET);
9410 value = constant_expr(state);
9411 eat(state, TOK_RBRACKET);
Eric Biederman00443072003-06-24 12:34:45 +00009412
9413 info.type = type->left;
9414 info.offset = value->u.cval * size_of(state, info.type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009415 break;
9416 }
9417 case TOK_DOT:
Eric Biederman00443072003-06-24 12:34:45 +00009418 {
9419 struct hash_entry *field;
Eric Biederman00443072003-06-24 12:34:45 +00009420 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
9421 error(state, 0, "Struct designator not in struct initializer");
9422 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009423 eat(state, TOK_DOT);
9424 eat(state, TOK_IDENT);
Eric Biederman00443072003-06-24 12:34:45 +00009425 field = state->token[0].ident;
Eric Biederman03b59862003-06-24 14:27:37 +00009426 info.offset = field_offset(state, type, field);
9427 info.type = field_type(state, type, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009428 break;
Eric Biederman00443072003-06-24 12:34:45 +00009429 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009430 default:
9431 error(state, 0, "Invalid designator");
9432 }
9433 tok = peek(state);
9434 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
9435 eat(state, TOK_EQ);
Eric Biederman00443072003-06-24 12:34:45 +00009436 return info;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009437}
9438
9439static struct triple *initializer(
9440 struct compile_state *state, struct type *type)
9441{
9442 struct triple *result;
Eric Biedermane058a1e2003-07-12 01:21:31 +00009443#warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
Eric Biedermanb138ac82003-04-22 18:44:01 +00009444 if (peek(state) != TOK_LBRACE) {
9445 result = assignment_expr(state);
Eric Biedermane058a1e2003-07-12 01:21:31 +00009446 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
9447 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
9448 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
9449 (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
9450 (equiv_types(type->left, result->type->left))) {
9451 type->elements = result->type->elements;
9452 }
Eric Biederman83b991a2003-10-11 06:20:25 +00009453 if (is_stable(state, result) &&
9454 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
9455 (type->type & TYPE_MASK) != TYPE_ARRAY)
9456 {
9457 result = array_to_pointer(state, result);
9458 }
Eric Biedermane058a1e2003-07-12 01:21:31 +00009459 if (!is_init_compatible(state, type, result->type)) {
9460 error(state, 0, "Incompatible types in initializer");
9461 }
9462 if (!equiv_types(type, result->type)) {
9463 result = mk_cast_expr(state, type, result);
9464 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009465 }
9466 else {
9467 int comma;
Eric Biederman00443072003-06-24 12:34:45 +00009468 size_t max_offset;
9469 struct field_info info;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009470 void *buf;
Eric Biederman00443072003-06-24 12:34:45 +00009471 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
9472 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
9473 internal_error(state, 0, "unknown initializer type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00009474 }
Eric Biederman00443072003-06-24 12:34:45 +00009475 info.offset = 0;
9476 info.type = type->left;
Eric Biederman03b59862003-06-24 14:27:37 +00009477 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
9478 info.type = next_field(state, type, 0);
9479 }
Eric Biederman00443072003-06-24 12:34:45 +00009480 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
9481 max_offset = 0;
9482 } else {
9483 max_offset = size_of(state, type);
9484 }
9485 buf = xcmalloc(max_offset, "initializer");
Eric Biedermanb138ac82003-04-22 18:44:01 +00009486 eat(state, TOK_LBRACE);
9487 do {
9488 struct triple *value;
9489 struct type *value_type;
9490 size_t value_size;
Eric Biederman00443072003-06-24 12:34:45 +00009491 void *dest;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009492 int tok;
9493 comma = 0;
9494 tok = peek(state);
9495 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
Eric Biederman00443072003-06-24 12:34:45 +00009496 info = designator(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009497 }
Eric Biederman00443072003-06-24 12:34:45 +00009498 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
9499 (info.offset >= max_offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009500 error(state, 0, "element beyond bounds");
9501 }
Eric Biederman00443072003-06-24 12:34:45 +00009502 value_type = info.type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009503 value = eval_const_expr(state, initializer(state, value_type));
9504 value_size = size_of(state, value_type);
9505 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
Eric Biederman00443072003-06-24 12:34:45 +00009506 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
9507 (max_offset <= info.offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009508 void *old_buf;
9509 size_t old_size;
9510 old_buf = buf;
Eric Biederman00443072003-06-24 12:34:45 +00009511 old_size = max_offset;
9512 max_offset = info.offset + value_size;
9513 buf = xmalloc(max_offset, "initializer");
Eric Biedermanb138ac82003-04-22 18:44:01 +00009514 memcpy(buf, old_buf, old_size);
9515 xfree(old_buf);
9516 }
Eric Biederman00443072003-06-24 12:34:45 +00009517 dest = ((char *)buf) + info.offset;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009518 if (value->op == OP_BLOBCONST) {
Eric Biederman00443072003-06-24 12:34:45 +00009519 memcpy(dest, value->u.blob, value_size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009520 }
9521 else if ((value->op == OP_INTCONST) && (value_size == 1)) {
Eric Biederman00443072003-06-24 12:34:45 +00009522 *((uint8_t *)dest) = value->u.cval & 0xff;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009523 }
9524 else if ((value->op == OP_INTCONST) && (value_size == 2)) {
Eric Biederman00443072003-06-24 12:34:45 +00009525 *((uint16_t *)dest) = value->u.cval & 0xffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009526 }
9527 else if ((value->op == OP_INTCONST) && (value_size == 4)) {
Eric Biederman00443072003-06-24 12:34:45 +00009528 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009529 }
9530 else {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009531 internal_error(state, 0, "unhandled constant initializer");
9532 }
Eric Biederman00443072003-06-24 12:34:45 +00009533 free_triple(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009534 if (peek(state) == TOK_COMMA) {
9535 eat(state, TOK_COMMA);
9536 comma = 1;
9537 }
Eric Biederman00443072003-06-24 12:34:45 +00009538 info.offset += value_size;
Eric Biederman03b59862003-06-24 14:27:37 +00009539 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
9540 info.type = next_field(state, type, info.type);
9541 info.offset = field_offset(state, type,
9542 info.type->field_ident);
Eric Biederman00443072003-06-24 12:34:45 +00009543 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009544 } while(comma && (peek(state) != TOK_RBRACE));
Eric Biederman00443072003-06-24 12:34:45 +00009545 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
9546 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
9547 type->elements = max_offset / size_of(state, type->left);
9548 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009549 eat(state, TOK_RBRACE);
9550 result = triple(state, OP_BLOBCONST, type, 0, 0);
9551 result->u.blob = buf;
9552 }
9553 return result;
9554}
9555
Eric Biederman153ea352003-06-20 14:43:20 +00009556static void resolve_branches(struct compile_state *state)
9557{
9558 /* Make a second pass and finish anything outstanding
9559 * with respect to branches. The only outstanding item
9560 * is to see if there are goto to labels that have not
9561 * been defined and to error about them.
9562 */
9563 int i;
9564 for(i = 0; i < HASH_TABLE_SIZE; i++) {
9565 struct hash_entry *entry;
9566 for(entry = state->hash_table[i]; entry; entry = entry->next) {
9567 struct triple *ins;
9568 if (!entry->sym_label) {
9569 continue;
9570 }
9571 ins = entry->sym_label->def;
9572 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
9573 error(state, ins, "label `%s' used but not defined",
9574 entry->name);
9575 }
9576 }
9577 }
9578}
9579
Eric Biedermanb138ac82003-04-22 18:44:01 +00009580static struct triple *function_definition(
9581 struct compile_state *state, struct type *type)
9582{
9583 struct triple *def, *tmp, *first, *end;
9584 struct hash_entry *ident;
9585 struct type *param;
9586 int i;
9587 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
9588 error(state, 0, "Invalid function header");
9589 }
9590
9591 /* Verify the function type */
9592 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
9593 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +00009594 (type->right->field_ident == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009595 error(state, 0, "Invalid function parameters");
9596 }
9597 param = type->right;
9598 i = 0;
9599 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
9600 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009601 if (!param->left->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009602 error(state, 0, "No identifier for parameter %d\n", i);
9603 }
9604 param = param->right;
9605 }
9606 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009607 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009608 error(state, 0, "No identifier for paramter %d\n", i);
9609 }
9610
9611 /* Get a list of statements for this function. */
9612 def = triple(state, OP_LIST, type, 0, 0);
9613
9614 /* Start a new scope for the passed parameters */
9615 start_scope(state);
9616
9617 /* Put a label at the very start of a function */
9618 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009619 RHS(def, 0) = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009620
9621 /* Put a label at the very end of a function */
9622 end = label(state);
9623 flatten(state, first, end);
9624
9625 /* Walk through the parameters and create symbol table entries
9626 * for them.
9627 */
9628 param = type->right;
9629 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009630 ident = param->left->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009631 tmp = variable(state, param->left);
9632 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
9633 flatten(state, end, tmp);
9634 param = param->right;
9635 }
9636 if ((param->type & TYPE_MASK) != TYPE_VOID) {
9637 /* And don't forget the last parameter */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009638 ident = param->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009639 tmp = variable(state, param);
9640 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
9641 flatten(state, end, tmp);
9642 }
9643 /* Add a variable for the return value */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009644 MISC(def, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009645 if ((type->left->type & TYPE_MASK) != TYPE_VOID) {
9646 /* Remove all type qualifiers from the return type */
9647 tmp = variable(state, clone_type(0, type->left));
9648 flatten(state, end, tmp);
9649 /* Remember where the return value is */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009650 MISC(def, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009651 }
9652
9653 /* Remember which function I am compiling.
9654 * Also assume the last defined function is the main function.
9655 */
9656 state->main_function = def;
9657
9658 /* Now get the actual function definition */
9659 compound_statement(state, end);
9660
Eric Biederman153ea352003-06-20 14:43:20 +00009661 /* Finish anything unfinished with branches */
9662 resolve_branches(state);
9663
Eric Biedermanb138ac82003-04-22 18:44:01 +00009664 /* Remove the parameter scope */
9665 end_scope(state);
Eric Biederman153ea352003-06-20 14:43:20 +00009666
Eric Biedermanb138ac82003-04-22 18:44:01 +00009667#if 0
9668 fprintf(stdout, "\n");
9669 loc(stdout, state, 0);
9670 fprintf(stdout, "\n__________ function_definition _________\n");
9671 print_triple(state, def);
9672 fprintf(stdout, "__________ function_definition _________ done\n\n");
9673#endif
9674
9675 return def;
9676}
9677
9678static struct triple *do_decl(struct compile_state *state,
9679 struct type *type, struct hash_entry *ident)
9680{
9681 struct triple *def;
9682 def = 0;
9683 /* Clean up the storage types used */
9684 switch (type->type & STOR_MASK) {
9685 case STOR_AUTO:
9686 case STOR_STATIC:
9687 /* These are the good types I am aiming for */
9688 break;
9689 case STOR_REGISTER:
9690 type->type &= ~STOR_MASK;
9691 type->type |= STOR_AUTO;
9692 break;
9693 case STOR_EXTERN:
9694 type->type &= ~STOR_MASK;
9695 type->type |= STOR_STATIC;
9696 break;
9697 case STOR_TYPEDEF:
Eric Biederman0babc1c2003-05-09 02:39:00 +00009698 if (!ident) {
9699 error(state, 0, "typedef without name");
9700 }
9701 symbol(state, ident, &ident->sym_ident, 0, type);
9702 ident->tok = TOK_TYPE_NAME;
9703 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009704 break;
9705 default:
9706 internal_error(state, 0, "Undefined storage class");
9707 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00009708 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
9709 error(state, 0, "Function prototypes not supported");
9710 }
Eric Biederman00443072003-06-24 12:34:45 +00009711 if (ident &&
9712 ((type->type & STOR_MASK) == STOR_STATIC) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00009713 ((type->type & QUAL_CONST) == 0)) {
9714 error(state, 0, "non const static variables not supported");
9715 }
9716 if (ident) {
9717 def = variable(state, type);
9718 symbol(state, ident, &ident->sym_ident, def, type);
9719 }
9720 return def;
9721}
9722
9723static void decl(struct compile_state *state, struct triple *first)
9724{
9725 struct type *base_type, *type;
9726 struct hash_entry *ident;
9727 struct triple *def;
9728 int global;
9729 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
9730 base_type = decl_specifiers(state);
9731 ident = 0;
9732 type = declarator(state, base_type, &ident, 0);
9733 if (global && ident && (peek(state) == TOK_LBRACE)) {
9734 /* function */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00009735 state->function = ident->name;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009736 def = function_definition(state, type);
9737 symbol(state, ident, &ident->sym_ident, def, type);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00009738 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009739 }
9740 else {
9741 int done;
9742 flatten(state, first, do_decl(state, type, ident));
9743 /* type or variable definition */
9744 do {
9745 done = 1;
9746 if (peek(state) == TOK_EQ) {
9747 if (!ident) {
9748 error(state, 0, "cannot assign to a type");
9749 }
9750 eat(state, TOK_EQ);
9751 flatten(state, first,
9752 init_expr(state,
9753 ident->sym_ident->def,
9754 initializer(state, type)));
9755 }
9756 arrays_complete(state, type);
9757 if (peek(state) == TOK_COMMA) {
9758 eat(state, TOK_COMMA);
9759 ident = 0;
9760 type = declarator(state, base_type, &ident, 0);
9761 flatten(state, first, do_decl(state, type, ident));
9762 done = 0;
9763 }
9764 } while(!done);
9765 eat(state, TOK_SEMI);
9766 }
9767}
9768
9769static void decls(struct compile_state *state)
9770{
9771 struct triple *list;
9772 int tok;
9773 list = label(state);
9774 while(1) {
9775 tok = peek(state);
9776 if (tok == TOK_EOF) {
9777 return;
9778 }
9779 if (tok == TOK_SPACE) {
9780 eat(state, TOK_SPACE);
9781 }
9782 decl(state, list);
9783 if (list->next != list) {
9784 error(state, 0, "global variables not supported");
9785 }
9786 }
9787}
9788
9789/*
9790 * Data structurs for optimation.
9791 */
9792
Eric Biederman83b991a2003-10-11 06:20:25 +00009793static int do_use_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +00009794 struct block *used, struct block_set **head, struct block *user,
9795 int front)
9796{
9797 struct block_set **ptr, *new;
9798 if (!used)
Eric Biederman83b991a2003-10-11 06:20:25 +00009799 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009800 if (!user)
Eric Biederman83b991a2003-10-11 06:20:25 +00009801 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009802 ptr = head;
9803 while(*ptr) {
9804 if ((*ptr)->member == user) {
Eric Biederman83b991a2003-10-11 06:20:25 +00009805 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009806 }
9807 ptr = &(*ptr)->next;
9808 }
9809 new = xcmalloc(sizeof(*new), "block_set");
9810 new->member = user;
9811 if (front) {
9812 new->next = *head;
9813 *head = new;
9814 }
9815 else {
9816 new->next = 0;
9817 *ptr = new;
9818 }
Eric Biederman83b991a2003-10-11 06:20:25 +00009819 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009820}
Eric Biederman83b991a2003-10-11 06:20:25 +00009821static int do_unuse_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +00009822 struct block *used, struct block_set **head, struct block *unuser)
9823{
9824 struct block_set *use, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +00009825 int count;
9826 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009827 ptr = head;
9828 while(*ptr) {
9829 use = *ptr;
9830 if (use->member == unuser) {
9831 *ptr = use->next;
9832 memset(use, -1, sizeof(*use));
9833 xfree(use);
Eric Biederman83b991a2003-10-11 06:20:25 +00009834 count += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009835 }
9836 else {
9837 ptr = &use->next;
9838 }
9839 }
Eric Biederman83b991a2003-10-11 06:20:25 +00009840 return count;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009841}
9842
9843static void use_block(struct block *used, struct block *user)
9844{
Eric Biederman83b991a2003-10-11 06:20:25 +00009845 int count;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009846 /* Append new to the head of the list, print_block
9847 * depends on this.
9848 */
Eric Biederman83b991a2003-10-11 06:20:25 +00009849 count = do_use_block(used, &used->use, user, 1);
9850 used->users += count;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009851}
9852static void unuse_block(struct block *used, struct block *unuser)
9853{
Eric Biederman83b991a2003-10-11 06:20:25 +00009854 int count;
9855 count = do_unuse_block(used, &used->use, unuser);
9856 used->users -= count;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009857}
9858
9859static void idom_block(struct block *idom, struct block *user)
9860{
9861 do_use_block(idom, &idom->idominates, user, 0);
9862}
9863
9864static void unidom_block(struct block *idom, struct block *unuser)
9865{
9866 do_unuse_block(idom, &idom->idominates, unuser);
9867}
9868
9869static void domf_block(struct block *block, struct block *domf)
9870{
9871 do_use_block(block, &block->domfrontier, domf, 0);
9872}
9873
9874static void undomf_block(struct block *block, struct block *undomf)
9875{
9876 do_unuse_block(block, &block->domfrontier, undomf);
9877}
9878
9879static void ipdom_block(struct block *ipdom, struct block *user)
9880{
9881 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
9882}
9883
9884static void unipdom_block(struct block *ipdom, struct block *unuser)
9885{
9886 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
9887}
9888
9889static void ipdomf_block(struct block *block, struct block *ipdomf)
9890{
9891 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
9892}
9893
9894static void unipdomf_block(struct block *block, struct block *unipdomf)
9895{
9896 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
9897}
9898
Eric Biederman83b991a2003-10-11 06:20:25 +00009899static int walk_triples(
9900 struct compile_state *state,
9901 int (*cb)(struct compile_state *state, struct triple *ptr))
Eric Biedermanb138ac82003-04-22 18:44:01 +00009902{
Eric Biederman83b991a2003-10-11 06:20:25 +00009903 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009904 int result;
Eric Biederman83b991a2003-10-11 06:20:25 +00009905 ptr = state->first;
9906 do {
9907 result = cb(state, ptr);
9908 if (ptr->next->prev != ptr) {
9909 internal_error(state, ptr->next, "bad prev");
9910 }
9911 ptr = ptr->next;
9912 } while((result == 0) && (ptr != state->first));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009913 return result;
9914}
9915
Eric Biedermanb138ac82003-04-22 18:44:01 +00009916#define PRINT_LIST 1
Eric Biederman83b991a2003-10-11 06:20:25 +00009917static int do_print_triple(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00009918{
9919 int op;
9920 op = ins->op;
9921 if (op == OP_LIST) {
9922#if !PRINT_LIST
9923 return 0;
9924#endif
9925 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009926 if ((op == OP_LABEL) && (ins->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009927 printf("\n%p:\n", ins);
9928 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009929 display_triple(stdout, ins);
9930
Eric Biedermanb138ac82003-04-22 18:44:01 +00009931 if ((ins->op == OP_BRANCH) && ins->use) {
9932 internal_error(state, ins, "branch used?");
9933 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009934 if (triple_is_branch(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009935 printf("\n");
9936 }
9937 return 0;
9938}
9939
Eric Biedermanb138ac82003-04-22 18:44:01 +00009940static void print_triples(struct compile_state *state)
9941{
Eric Biederman83b991a2003-10-11 06:20:25 +00009942 walk_triples(state, do_print_triple);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009943}
9944
9945struct cf_block {
9946 struct block *block;
9947};
9948static void find_cf_blocks(struct cf_block *cf, struct block *block)
9949{
9950 if (!block || (cf[block->vertex].block == block)) {
9951 return;
9952 }
9953 cf[block->vertex].block = block;
9954 find_cf_blocks(cf, block->left);
9955 find_cf_blocks(cf, block->right);
9956}
9957
9958static void print_control_flow(struct compile_state *state)
9959{
9960 struct cf_block *cf;
9961 int i;
9962 printf("\ncontrol flow\n");
9963 cf = xcmalloc(sizeof(*cf) * (state->last_vertex + 1), "cf_block");
9964 find_cf_blocks(cf, state->first_block);
9965
9966 for(i = 1; i <= state->last_vertex; i++) {
9967 struct block *block;
9968 block = cf[i].block;
9969 if (!block)
9970 continue;
9971 printf("(%p) %d:", block, block->vertex);
9972 if (block->left) {
9973 printf(" %d", block->left->vertex);
9974 }
9975 if (block->right && (block->right != block->left)) {
9976 printf(" %d", block->right->vertex);
9977 }
9978 printf("\n");
9979 }
9980
9981 xfree(cf);
9982}
9983
9984
9985static struct block *basic_block(struct compile_state *state,
9986 struct triple *first)
9987{
9988 struct block *block;
Eric Biederman83b991a2003-10-11 06:20:25 +00009989 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009990 if (first->op != OP_LABEL) {
9991 internal_error(state, 0, "block does not start with a label");
9992 }
9993 /* See if this basic block has already been setup */
9994 if (first->u.block != 0) {
9995 return first->u.block;
9996 }
9997 /* Allocate another basic block structure */
9998 state->last_vertex += 1;
9999 block = xcmalloc(sizeof(*block), "block");
10000 block->first = block->last = first;
10001 block->vertex = state->last_vertex;
10002 ptr = first;
10003 do {
Eric Biederman83b991a2003-10-11 06:20:25 +000010004 if ((ptr != first) && (ptr->op == OP_LABEL) && (ptr->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010005 break;
10006 }
10007 block->last = ptr;
10008 /* If ptr->u is not used remember where the baic block is */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010009 if (triple_stores_block(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010010 ptr->u.block = block;
10011 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010012 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010013 break;
10014 }
10015 ptr = ptr->next;
Eric Biederman83b991a2003-10-11 06:20:25 +000010016 } while (ptr != state->first);
10017 if (ptr == state->first) {
10018 /* The block has no outflowing edges */
10019 }
10020 else if (ptr->op == OP_LABEL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010021 block->left = basic_block(state, ptr);
10022 block->right = 0;
10023 use_block(block->left, block);
10024 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010025 else if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010026 block->left = 0;
10027 /* Trace the branch target */
Eric Biederman0babc1c2003-05-09 02:39:00 +000010028 block->right = basic_block(state, TARG(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010029 use_block(block->right, block);
10030 /* If there is a test trace the branch as well */
Eric Biederman0babc1c2003-05-09 02:39:00 +000010031 if (TRIPLE_RHS(ptr->sizes)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010032 block->left = basic_block(state, ptr->next);
10033 use_block(block->left, block);
10034 }
10035 }
10036 else {
10037 internal_error(state, 0, "Bad basic block split");
10038 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010039#if 0
10040 fprintf(stderr, "basic_block: %10p [%2d] ( %10p - %10p ) %10p [%2d] %10p [%2d] \n",
10041 block, block->vertex,
10042 block->first, block->last,
10043 block->left ? block->left->first : 0,
10044 block->left ? block->left->vertex : -1,
10045 block->left ? block->left->first : 0,
10046 block->left ? block->left->vertex : -1);
10047#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000010048 return block;
10049}
10050
10051
10052static void walk_blocks(struct compile_state *state,
10053 void (*cb)(struct compile_state *state, struct block *block, void *arg),
10054 void *arg)
10055{
10056 struct triple *ptr, *first;
10057 struct block *last_block;
10058 last_block = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010059 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010060 ptr = first;
10061 do {
10062 struct block *block;
Eric Biederman530b5192003-07-01 10:05:30 +000010063 if (triple_stores_block(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010064 block = ptr->u.block;
10065 if (block && (block != last_block)) {
10066 cb(state, block, arg);
10067 }
10068 last_block = block;
10069 }
Eric Biederman530b5192003-07-01 10:05:30 +000010070 if (block && (block->last == ptr)) {
10071 block = 0;
10072 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010073 ptr = ptr->next;
10074 } while(ptr != first);
10075}
10076
10077static void print_block(
10078 struct compile_state *state, struct block *block, void *arg)
10079{
Eric Biederman530b5192003-07-01 10:05:30 +000010080 struct block_set *user;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010081 struct triple *ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010082 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010083
Eric Biederman530b5192003-07-01 10:05:30 +000010084 fprintf(fp, "\nblock: %p (%d) %p<-%p %p<-%p\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000010085 block,
10086 block->vertex,
10087 block->left,
10088 block->left && block->left->use?block->left->use->member : 0,
10089 block->right,
10090 block->right && block->right->use?block->right->use->member : 0);
10091 if (block->first->op == OP_LABEL) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010092 fprintf(fp, "%p:\n", block->first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010093 }
10094 for(ptr = block->first; ; ptr = ptr->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010095 display_triple(fp, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010096 if (ptr == block->last)
10097 break;
10098 }
Eric Biederman530b5192003-07-01 10:05:30 +000010099 fprintf(fp, "users %d: ", block->users);
10100 for(user = block->use; user; user = user->next) {
10101 fprintf(fp, "%p (%d) ",
10102 user->member,
10103 user->member->vertex);
10104 }
10105 fprintf(fp,"\n\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010106}
10107
10108
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010109static void print_blocks(struct compile_state *state, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010110{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010111 fprintf(fp, "--------------- blocks ---------------\n");
10112 walk_blocks(state, print_block, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010113}
10114
10115static void prune_nonblock_triples(struct compile_state *state)
10116{
10117 struct block *block;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010118 struct triple *first, *ins, *next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010119 /* Delete the triples not in a basic block */
Eric Biederman83b991a2003-10-11 06:20:25 +000010120 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010121 block = 0;
10122 ins = first;
10123 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010124 next = ins->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010125 if (ins->op == OP_LABEL) {
10126 block = ins->u.block;
10127 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010128 if (!block) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010129 release_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010130 }
Eric Biederman530b5192003-07-01 10:05:30 +000010131 if (block && block->last == ins) {
10132 block = 0;
10133 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010134 ins = next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010135 } while(ins != first);
10136}
10137
10138static void setup_basic_blocks(struct compile_state *state)
10139{
Eric Biederman83b991a2003-10-11 06:20:25 +000010140 if (!triple_stores_block(state, state->first)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010141 internal_error(state, 0, "ins will not store block?");
10142 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010143 /* Find the basic blocks */
10144 state->last_vertex = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010145 state->first_block = basic_block(state, state->first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010146 /* Delete the triples not in a basic block */
10147 prune_nonblock_triples(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000010148
10149 /* Find the last basic block.
10150 *
10151 * For purposes of reverse flow computation it is
10152 * important that the last basic block is empty.
10153 * This allows the control flow graph to be modified to
10154 * have one unique starting block and one unique final block.
10155 * With the insertion of a few extra edges.
10156 *
10157 * If the final block contained instructions it could contain
10158 * phi functions from edges that would never contribute a
10159 * value. Which for now at least I consider a compile error.
10160 */
10161 state->last_block = block_of_triple(state, state->first->prev);
10162 if ((state->last_block->first != state->last_block->last) ||
10163 (state->last_block->last->op != OP_LABEL))
10164 {
10165 struct block *block, *prev_block;
10166 struct triple *final;
10167 prev_block = state->last_block;
10168 final = label(state);
10169 flatten(state, state->first, final);
10170 use_triple(final, final);
10171 block = basic_block(state, final);
10172 state->last_block = block;
10173 prev_block->left = block;
10174 use_block(prev_block->left, prev_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010175 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010176
Eric Biedermanb138ac82003-04-22 18:44:01 +000010177 /* If we are debugging print what I have just done */
10178 if (state->debug & DEBUG_BASIC_BLOCKS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010179 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010180 print_control_flow(state);
10181 }
10182}
10183
10184static void free_basic_block(struct compile_state *state, struct block *block)
10185{
10186 struct block_set *entry, *next;
10187 struct block *child;
10188 if (!block) {
10189 return;
10190 }
10191 if (block->vertex == -1) {
10192 return;
10193 }
10194 block->vertex = -1;
10195 if (block->left) {
10196 unuse_block(block->left, block);
10197 }
10198 if (block->right) {
10199 unuse_block(block->right, block);
10200 }
10201 if (block->idom) {
10202 unidom_block(block->idom, block);
10203 }
10204 block->idom = 0;
10205 if (block->ipdom) {
10206 unipdom_block(block->ipdom, block);
10207 }
10208 block->ipdom = 0;
10209 for(entry = block->use; entry; entry = next) {
10210 next = entry->next;
10211 child = entry->member;
10212 unuse_block(block, child);
10213 if (child->left == block) {
10214 child->left = 0;
10215 }
10216 if (child->right == block) {
10217 child->right = 0;
10218 }
10219 }
10220 for(entry = block->idominates; entry; entry = next) {
10221 next = entry->next;
10222 child = entry->member;
10223 unidom_block(block, child);
10224 child->idom = 0;
10225 }
10226 for(entry = block->domfrontier; entry; entry = next) {
10227 next = entry->next;
10228 child = entry->member;
10229 undomf_block(block, child);
10230 }
10231 for(entry = block->ipdominates; entry; entry = next) {
10232 next = entry->next;
10233 child = entry->member;
10234 unipdom_block(block, child);
10235 child->ipdom = 0;
10236 }
10237 for(entry = block->ipdomfrontier; entry; entry = next) {
10238 next = entry->next;
10239 child = entry->member;
10240 unipdomf_block(block, child);
10241 }
10242 if (block->users != 0) {
10243 internal_error(state, 0, "block still has users");
10244 }
10245 free_basic_block(state, block->left);
10246 block->left = 0;
10247 free_basic_block(state, block->right);
10248 block->right = 0;
10249 memset(block, -1, sizeof(*block));
10250 xfree(block);
10251}
10252
10253static void free_basic_blocks(struct compile_state *state)
10254{
10255 struct triple *first, *ins;
10256 free_basic_block(state, state->first_block);
10257 state->last_vertex = 0;
10258 state->first_block = state->last_block = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010259 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010260 ins = first;
10261 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010262 if (triple_stores_block(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010263 ins->u.block = 0;
10264 }
10265 ins = ins->next;
10266 } while(ins != first);
10267
10268}
10269
10270struct sdom_block {
10271 struct block *block;
10272 struct sdom_block *sdominates;
10273 struct sdom_block *sdom_next;
10274 struct sdom_block *sdom;
10275 struct sdom_block *label;
10276 struct sdom_block *parent;
10277 struct sdom_block *ancestor;
10278 int vertex;
10279};
10280
10281
10282static void unsdom_block(struct sdom_block *block)
10283{
10284 struct sdom_block **ptr;
10285 if (!block->sdom_next) {
10286 return;
10287 }
10288 ptr = &block->sdom->sdominates;
10289 while(*ptr) {
10290 if ((*ptr) == block) {
10291 *ptr = block->sdom_next;
10292 return;
10293 }
10294 ptr = &(*ptr)->sdom_next;
10295 }
10296}
10297
10298static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
10299{
10300 unsdom_block(block);
10301 block->sdom = sdom;
10302 block->sdom_next = sdom->sdominates;
10303 sdom->sdominates = block;
10304}
10305
10306
10307
10308static int initialize_sdblock(struct sdom_block *sd,
10309 struct block *parent, struct block *block, int vertex)
10310{
10311 if (!block || (sd[block->vertex].block == block)) {
10312 return vertex;
10313 }
10314 vertex += 1;
10315 /* Renumber the blocks in a convinient fashion */
10316 block->vertex = vertex;
10317 sd[vertex].block = block;
10318 sd[vertex].sdom = &sd[vertex];
10319 sd[vertex].label = &sd[vertex];
10320 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
10321 sd[vertex].ancestor = 0;
10322 sd[vertex].vertex = vertex;
10323 vertex = initialize_sdblock(sd, block, block->left, vertex);
10324 vertex = initialize_sdblock(sd, block, block->right, vertex);
10325 return vertex;
10326}
10327
Eric Biederman83b991a2003-10-11 06:20:25 +000010328static int initialize_spdblock(
Eric Biederman530b5192003-07-01 10:05:30 +000010329 struct compile_state *state, struct sdom_block *sd,
Eric Biedermanb138ac82003-04-22 18:44:01 +000010330 struct block *parent, struct block *block, int vertex)
10331{
10332 struct block_set *user;
10333 if (!block || (sd[block->vertex].block == block)) {
10334 return vertex;
10335 }
10336 vertex += 1;
10337 /* Renumber the blocks in a convinient fashion */
10338 block->vertex = vertex;
10339 sd[vertex].block = block;
10340 sd[vertex].sdom = &sd[vertex];
10341 sd[vertex].label = &sd[vertex];
10342 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
10343 sd[vertex].ancestor = 0;
10344 sd[vertex].vertex = vertex;
10345 for(user = block->use; user; user = user->next) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010346 vertex = initialize_spdblock(state, sd, block, user->member, vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000010347 }
10348 return vertex;
10349}
10350
Eric Biederman83b991a2003-10-11 06:20:25 +000010351static int setup_spdblocks(struct compile_state *state, struct sdom_block *sd)
Eric Biederman530b5192003-07-01 10:05:30 +000010352{
10353 struct block *block;
10354 int vertex;
10355 /* Setup as many sdpblocks as possible without using fake edges */
Eric Biederman83b991a2003-10-11 06:20:25 +000010356 vertex = initialize_spdblock(state, sd, 0, state->last_block, 0);
Eric Biederman530b5192003-07-01 10:05:30 +000010357
10358 /* Walk through the graph and find unconnected blocks. If
10359 * we can, add a fake edge from the unconnected blocks to the
10360 * end of the graph.
10361 */
10362 block = state->first_block->last->next->u.block;
Eric Biederman83b991a2003-10-11 06:20:25 +000010363 for(; block && block != state->first_block; block = block->last->next->u.block) {
Eric Biederman530b5192003-07-01 10:05:30 +000010364 if (sd[block->vertex].block == block) {
10365 continue;
10366 }
10367 if (block->left != 0) {
10368 continue;
10369 }
10370
10371#if DEBUG_SDP_BLOCKS
10372 fprintf(stderr, "Adding %d\n", vertex +1);
10373#endif
10374
10375 block->left = state->last_block;
10376 use_block(block->left, block);
Eric Biederman83b991a2003-10-11 06:20:25 +000010377 vertex = initialize_spdblock(state, sd, state->last_block, block, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010378 }
10379 return vertex;
10380}
10381
10382static void compress_ancestors(struct sdom_block *v)
10383{
10384 /* This procedure assumes ancestor(v) != 0 */
10385 /* if (ancestor(ancestor(v)) != 0) {
10386 * compress(ancestor(ancestor(v)));
10387 * if (semi(label(ancestor(v))) < semi(label(v))) {
10388 * label(v) = label(ancestor(v));
10389 * }
10390 * ancestor(v) = ancestor(ancestor(v));
10391 * }
10392 */
10393 if (!v->ancestor) {
10394 return;
10395 }
10396 if (v->ancestor->ancestor) {
10397 compress_ancestors(v->ancestor->ancestor);
10398 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
10399 v->label = v->ancestor->label;
10400 }
10401 v->ancestor = v->ancestor->ancestor;
10402 }
10403}
10404
10405static void compute_sdom(struct compile_state *state, struct sdom_block *sd)
10406{
10407 int i;
10408 /* // step 2
10409 * for each v <= pred(w) {
10410 * u = EVAL(v);
10411 * if (semi[u] < semi[w] {
10412 * semi[w] = semi[u];
10413 * }
10414 * }
10415 * add w to bucket(vertex(semi[w]));
10416 * LINK(parent(w), w);
10417 *
10418 * // step 3
10419 * for each v <= bucket(parent(w)) {
10420 * delete v from bucket(parent(w));
10421 * u = EVAL(v);
10422 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
10423 * }
10424 */
10425 for(i = state->last_vertex; i >= 2; i--) {
10426 struct sdom_block *v, *parent, *next;
10427 struct block_set *user;
10428 struct block *block;
10429 block = sd[i].block;
10430 parent = sd[i].parent;
10431 /* Step 2 */
10432 for(user = block->use; user; user = user->next) {
10433 struct sdom_block *v, *u;
10434 v = &sd[user->member->vertex];
10435 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
10436 if (u->sdom->vertex < sd[i].sdom->vertex) {
10437 sd[i].sdom = u->sdom;
10438 }
10439 }
10440 sdom_block(sd[i].sdom, &sd[i]);
10441 sd[i].ancestor = parent;
10442 /* Step 3 */
10443 for(v = parent->sdominates; v; v = next) {
10444 struct sdom_block *u;
10445 next = v->sdom_next;
10446 unsdom_block(v);
10447 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
10448 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
10449 u->block : parent->block;
10450 }
10451 }
10452}
10453
10454static void compute_spdom(struct compile_state *state, struct sdom_block *sd)
10455{
10456 int i;
10457 /* // step 2
10458 * for each v <= pred(w) {
10459 * u = EVAL(v);
10460 * if (semi[u] < semi[w] {
10461 * semi[w] = semi[u];
10462 * }
10463 * }
10464 * add w to bucket(vertex(semi[w]));
10465 * LINK(parent(w), w);
10466 *
10467 * // step 3
10468 * for each v <= bucket(parent(w)) {
10469 * delete v from bucket(parent(w));
10470 * u = EVAL(v);
10471 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
10472 * }
10473 */
10474 for(i = state->last_vertex; i >= 2; i--) {
10475 struct sdom_block *u, *v, *parent, *next;
10476 struct block *block;
10477 block = sd[i].block;
10478 parent = sd[i].parent;
10479 /* Step 2 */
10480 if (block->left) {
10481 v = &sd[block->left->vertex];
10482 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
10483 if (u->sdom->vertex < sd[i].sdom->vertex) {
10484 sd[i].sdom = u->sdom;
10485 }
10486 }
10487 if (block->right && (block->right != block->left)) {
10488 v = &sd[block->right->vertex];
10489 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
10490 if (u->sdom->vertex < sd[i].sdom->vertex) {
10491 sd[i].sdom = u->sdom;
10492 }
10493 }
10494 sdom_block(sd[i].sdom, &sd[i]);
10495 sd[i].ancestor = parent;
10496 /* Step 3 */
10497 for(v = parent->sdominates; v; v = next) {
10498 struct sdom_block *u;
10499 next = v->sdom_next;
10500 unsdom_block(v);
10501 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
10502 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
10503 u->block : parent->block;
10504 }
10505 }
10506}
10507
10508static void compute_idom(struct compile_state *state, struct sdom_block *sd)
10509{
10510 int i;
10511 for(i = 2; i <= state->last_vertex; i++) {
10512 struct block *block;
10513 block = sd[i].block;
10514 if (block->idom->vertex != sd[i].sdom->vertex) {
10515 block->idom = block->idom->idom;
10516 }
10517 idom_block(block->idom, block);
10518 }
10519 sd[1].block->idom = 0;
10520}
10521
10522static void compute_ipdom(struct compile_state *state, struct sdom_block *sd)
10523{
10524 int i;
10525 for(i = 2; i <= state->last_vertex; i++) {
10526 struct block *block;
10527 block = sd[i].block;
10528 if (block->ipdom->vertex != sd[i].sdom->vertex) {
10529 block->ipdom = block->ipdom->ipdom;
10530 }
10531 ipdom_block(block->ipdom, block);
10532 }
10533 sd[1].block->ipdom = 0;
10534}
10535
10536 /* Theorem 1:
10537 * Every vertex of a flowgraph G = (V, E, r) except r has
10538 * a unique immediate dominator.
10539 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
10540 * rooted at r, called the dominator tree of G, such that
10541 * v dominates w if and only if v is a proper ancestor of w in
10542 * the dominator tree.
10543 */
10544 /* Lemma 1:
10545 * If v and w are vertices of G such that v <= w,
10546 * than any path from v to w must contain a common ancestor
10547 * of v and w in T.
10548 */
10549 /* Lemma 2: For any vertex w != r, idom(w) -> w */
10550 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
10551 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
10552 /* Theorem 2:
10553 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
10554 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
10555 */
10556 /* Theorem 3:
10557 * Let w != r and let u be a vertex for which sdom(u) is
10558 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
10559 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
10560 */
10561 /* Lemma 5: Let vertices v,w satisfy v -> w.
10562 * Then v -> idom(w) or idom(w) -> idom(v)
10563 */
10564
10565static void find_immediate_dominators(struct compile_state *state)
10566{
10567 struct sdom_block *sd;
10568 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
10569 * vi > w for (1 <= i <= k - 1}
10570 */
10571 /* Theorem 4:
10572 * For any vertex w != r.
10573 * sdom(w) = min(
10574 * {v|(v,w) <= E and v < w } U
10575 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
10576 */
10577 /* Corollary 1:
10578 * Let w != r and let u be a vertex for which sdom(u) is
10579 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
10580 * Then:
10581 * { sdom(w) if sdom(w) = sdom(u),
10582 * idom(w) = {
10583 * { idom(u) otherwise
10584 */
10585 /* The algorithm consists of the following 4 steps.
10586 * Step 1. Carry out a depth-first search of the problem graph.
10587 * Number the vertices from 1 to N as they are reached during
10588 * the search. Initialize the variables used in succeeding steps.
10589 * Step 2. Compute the semidominators of all vertices by applying
10590 * theorem 4. Carry out the computation vertex by vertex in
10591 * decreasing order by number.
10592 * Step 3. Implicitly define the immediate dominator of each vertex
10593 * by applying Corollary 1.
10594 * Step 4. Explicitly define the immediate dominator of each vertex,
10595 * carrying out the computation vertex by vertex in increasing order
10596 * by number.
10597 */
10598 /* Step 1 initialize the basic block information */
10599 sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
10600 initialize_sdblock(sd, 0, state->first_block, 0);
10601#if 0
10602 sd[1].size = 0;
10603 sd[1].label = 0;
10604 sd[1].sdom = 0;
10605#endif
10606 /* Step 2 compute the semidominators */
10607 /* Step 3 implicitly define the immediate dominator of each vertex */
10608 compute_sdom(state, sd);
10609 /* Step 4 explicitly define the immediate dominator of each vertex */
10610 compute_idom(state, sd);
10611 xfree(sd);
10612}
10613
10614static void find_post_dominators(struct compile_state *state)
10615{
10616 struct sdom_block *sd;
Eric Biederman530b5192003-07-01 10:05:30 +000010617 int vertex;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010618 /* Step 1 initialize the basic block information */
10619 sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
10620
Eric Biederman83b991a2003-10-11 06:20:25 +000010621 vertex = setup_spdblocks(state, sd);
Eric Biederman530b5192003-07-01 10:05:30 +000010622 if (vertex != state->last_vertex) {
10623 internal_error(state, 0, "missing %d blocks\n",
10624 state->last_vertex - vertex);
10625 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010626
10627 /* Step 2 compute the semidominators */
10628 /* Step 3 implicitly define the immediate dominator of each vertex */
10629 compute_spdom(state, sd);
10630 /* Step 4 explicitly define the immediate dominator of each vertex */
10631 compute_ipdom(state, sd);
10632 xfree(sd);
10633}
10634
10635
10636
10637static void find_block_domf(struct compile_state *state, struct block *block)
10638{
10639 struct block *child;
10640 struct block_set *user;
10641 if (block->domfrontier != 0) {
10642 internal_error(state, block->first, "domfrontier present?");
10643 }
10644 for(user = block->idominates; user; user = user->next) {
10645 child = user->member;
10646 if (child->idom != block) {
10647 internal_error(state, block->first, "bad idom");
10648 }
10649 find_block_domf(state, child);
10650 }
10651 if (block->left && block->left->idom != block) {
10652 domf_block(block, block->left);
10653 }
10654 if (block->right && block->right->idom != block) {
10655 domf_block(block, block->right);
10656 }
10657 for(user = block->idominates; user; user = user->next) {
10658 struct block_set *frontier;
10659 child = user->member;
10660 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
10661 if (frontier->member->idom != block) {
10662 domf_block(block, frontier->member);
10663 }
10664 }
10665 }
10666}
10667
10668static void find_block_ipdomf(struct compile_state *state, struct block *block)
10669{
10670 struct block *child;
10671 struct block_set *user;
10672 if (block->ipdomfrontier != 0) {
10673 internal_error(state, block->first, "ipdomfrontier present?");
10674 }
10675 for(user = block->ipdominates; user; user = user->next) {
10676 child = user->member;
10677 if (child->ipdom != block) {
10678 internal_error(state, block->first, "bad ipdom");
10679 }
10680 find_block_ipdomf(state, child);
10681 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010682 for(user = block->use; user; user = user->next) {
10683 if (user->member->ipdom != block) {
10684 ipdomf_block(block, user->member);
10685 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010686 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010687 for(user = block->ipdominates; user; user = user->next) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010688 struct block_set *frontier;
10689 child = user->member;
10690 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
10691 if (frontier->member->ipdom != block) {
10692 ipdomf_block(block, frontier->member);
10693 }
10694 }
10695 }
10696}
10697
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010698static void print_dominated(
10699 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010700{
10701 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010702 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010703
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010704 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010705 for(user = block->idominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010706 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010707 if (user->member->idom != block) {
10708 internal_error(state, user->member->first, "bad idom");
10709 }
10710 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010711 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010712}
10713
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010714static void print_dominators(struct compile_state *state, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010715{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010716 fprintf(fp, "\ndominates\n");
10717 walk_blocks(state, print_dominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010718}
10719
10720
10721static int print_frontiers(
10722 struct compile_state *state, struct block *block, int vertex)
10723{
10724 struct block_set *user;
10725
10726 if (!block || (block->vertex != vertex + 1)) {
10727 return vertex;
10728 }
10729 vertex += 1;
10730
10731 printf("%d:", block->vertex);
10732 for(user = block->domfrontier; user; user = user->next) {
10733 printf(" %d", user->member->vertex);
10734 }
10735 printf("\n");
10736
10737 vertex = print_frontiers(state, block->left, vertex);
10738 vertex = print_frontiers(state, block->right, vertex);
10739 return vertex;
10740}
10741static void print_dominance_frontiers(struct compile_state *state)
10742{
10743 printf("\ndominance frontiers\n");
10744 print_frontiers(state, state->first_block, 0);
10745
10746}
10747
10748static void analyze_idominators(struct compile_state *state)
10749{
10750 /* Find the immediate dominators */
10751 find_immediate_dominators(state);
10752 /* Find the dominance frontiers */
10753 find_block_domf(state, state->first_block);
10754 /* If debuging print the print what I have just found */
10755 if (state->debug & DEBUG_FDOMINATORS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010756 print_dominators(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010757 print_dominance_frontiers(state);
10758 print_control_flow(state);
10759 }
10760}
10761
10762
10763
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010764static void print_ipdominated(
10765 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010766{
10767 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010768 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010769
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010770 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010771 for(user = block->ipdominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010772 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010773 if (user->member->ipdom != block) {
10774 internal_error(state, user->member->first, "bad ipdom");
10775 }
10776 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010777 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010778}
10779
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010780static void print_ipdominators(struct compile_state *state, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010781{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010782 fprintf(fp, "\nipdominates\n");
10783 walk_blocks(state, print_ipdominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010784}
10785
10786static int print_pfrontiers(
10787 struct compile_state *state, struct block *block, int vertex)
10788{
10789 struct block_set *user;
10790
10791 if (!block || (block->vertex != vertex + 1)) {
10792 return vertex;
10793 }
10794 vertex += 1;
10795
10796 printf("%d:", block->vertex);
10797 for(user = block->ipdomfrontier; user; user = user->next) {
10798 printf(" %d", user->member->vertex);
10799 }
10800 printf("\n");
10801 for(user = block->use; user; user = user->next) {
10802 vertex = print_pfrontiers(state, user->member, vertex);
10803 }
10804 return vertex;
10805}
10806static void print_ipdominance_frontiers(struct compile_state *state)
10807{
10808 printf("\nipdominance frontiers\n");
10809 print_pfrontiers(state, state->last_block, 0);
10810
10811}
10812
10813static void analyze_ipdominators(struct compile_state *state)
10814{
10815 /* Find the post dominators */
10816 find_post_dominators(state);
10817 /* Find the control dependencies (post dominance frontiers) */
10818 find_block_ipdomf(state, state->last_block);
10819 /* If debuging print the print what I have just found */
10820 if (state->debug & DEBUG_RDOMINATORS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010821 print_ipdominators(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010822 print_ipdominance_frontiers(state);
10823 print_control_flow(state);
10824 }
10825}
10826
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010827static int bdominates(struct compile_state *state,
10828 struct block *dom, struct block *sub)
10829{
10830 while(sub && (sub != dom)) {
10831 sub = sub->idom;
10832 }
10833 return sub == dom;
10834}
10835
10836static int tdominates(struct compile_state *state,
10837 struct triple *dom, struct triple *sub)
10838{
10839 struct block *bdom, *bsub;
10840 int result;
10841 bdom = block_of_triple(state, dom);
10842 bsub = block_of_triple(state, sub);
10843 if (bdom != bsub) {
10844 result = bdominates(state, bdom, bsub);
10845 }
10846 else {
10847 struct triple *ins;
10848 ins = sub;
10849 while((ins != bsub->first) && (ins != dom)) {
10850 ins = ins->prev;
10851 }
10852 result = (ins == dom);
10853 }
10854 return result;
10855}
10856
Eric Biederman83b991a2003-10-11 06:20:25 +000010857static void analyze_basic_blocks(struct compile_state *state)
10858{
10859 setup_basic_blocks(state);
10860 analyze_idominators(state);
10861 analyze_ipdominators(state);
10862}
10863
Eric Biedermanb138ac82003-04-22 18:44:01 +000010864static void insert_phi_operations(struct compile_state *state)
10865{
10866 size_t size;
10867 struct triple *first;
10868 int *has_already, *work;
10869 struct block *work_list, **work_list_tail;
10870 int iter;
Eric Biedermand1ea5392003-06-28 06:49:45 +000010871 struct triple *var, *vnext;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010872
10873 size = sizeof(int) * (state->last_vertex + 1);
10874 has_already = xcmalloc(size, "has_already");
10875 work = xcmalloc(size, "work");
10876 iter = 0;
10877
Eric Biederman83b991a2003-10-11 06:20:25 +000010878 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000010879 for(var = first->next; var != first ; var = vnext) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010880 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000010881 struct triple_set *user, *unext;
10882 vnext = var->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010883 if ((var->op != OP_ADECL) || !var->use) {
10884 continue;
10885 }
10886 iter += 1;
10887 work_list = 0;
10888 work_list_tail = &work_list;
Eric Biedermand1ea5392003-06-28 06:49:45 +000010889 for(user = var->use; user; user = unext) {
10890 unext = user->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010891 if (user->member->op == OP_READ) {
10892 continue;
10893 }
10894 if (user->member->op != OP_WRITE) {
10895 internal_error(state, user->member,
10896 "bad variable access");
10897 }
10898 block = user->member->u.block;
10899 if (!block) {
10900 warning(state, user->member, "dead code");
Eric Biedermand1ea5392003-06-28 06:49:45 +000010901 release_triple(state, user->member);
10902 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010903 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000010904 if (work[block->vertex] >= iter) {
10905 continue;
10906 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010907 work[block->vertex] = iter;
10908 *work_list_tail = block;
10909 block->work_next = 0;
10910 work_list_tail = &block->work_next;
10911 }
10912 for(block = work_list; block; block = block->work_next) {
10913 struct block_set *df;
10914 for(df = block->domfrontier; df; df = df->next) {
10915 struct triple *phi;
10916 struct block *front;
10917 int in_edges;
10918 front = df->member;
10919
10920 if (has_already[front->vertex] >= iter) {
10921 continue;
10922 }
10923 /* Count how many edges flow into this block */
10924 in_edges = front->users;
10925 /* Insert a phi function for this variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000010926 get_occurance(var->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010927 phi = alloc_triple(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010928 state, OP_PHI, var->type, -1, in_edges,
Eric Biederman66fe2222003-07-04 15:14:04 +000010929 var->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010930 phi->u.block = front;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010931 MISC(phi, 0) = var;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010932 use_triple(var, phi);
10933 /* Insert the phi functions immediately after the label */
10934 insert_triple(state, front->first->next, phi);
10935 if (front->first == front->last) {
10936 front->last = front->first->next;
10937 }
10938 has_already[front->vertex] = iter;
Eric Biederman83b991a2003-10-11 06:20:25 +000010939 transform_to_arch_instruction(state, phi);
Eric Biederman05f26fc2003-06-11 21:55:00 +000010940
Eric Biedermanb138ac82003-04-22 18:44:01 +000010941 /* If necessary plan to visit the basic block */
10942 if (work[front->vertex] >= iter) {
10943 continue;
10944 }
10945 work[front->vertex] = iter;
10946 *work_list_tail = front;
10947 front->work_next = 0;
10948 work_list_tail = &front->work_next;
10949 }
10950 }
10951 }
10952 xfree(has_already);
10953 xfree(work);
10954}
10955
Eric Biederman66fe2222003-07-04 15:14:04 +000010956
Eric Biederman83b991a2003-10-11 06:20:25 +000010957struct stack {
10958 struct triple_set *top;
10959 unsigned orig_id;
10960};
10961
10962static int count_adecls(struct compile_state *state)
Eric Biederman66fe2222003-07-04 15:14:04 +000010963{
10964 struct triple *first, *ins;
10965 int adecls = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010966 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000010967 ins = first;
10968 do {
10969 if (ins->op == OP_ADECL) {
10970 adecls += 1;
Eric Biederman66fe2222003-07-04 15:14:04 +000010971 }
10972 ins = ins->next;
10973 } while(ins != first);
10974 return adecls;
10975}
10976
Eric Biederman83b991a2003-10-11 06:20:25 +000010977static void number_adecls(struct compile_state *state, struct stack *stacks)
10978{
10979 struct triple *first, *ins;
10980 int adecls = 0;
10981 first = state->first;
10982 ins = first;
10983 do {
10984 if (ins->op == OP_ADECL) {
10985 adecls += 1;
10986 stacks[adecls].orig_id = ins->id;
10987 ins->id = adecls;
10988 }
10989 ins = ins->next;
10990 } while(ins != first);
10991}
10992
10993static void restore_adecls(struct compile_state *state, struct stack *stacks)
10994{
10995 struct triple *first, *ins;
10996 first = state->first;
10997 ins = first;
10998 do {
10999 if (ins->op == OP_ADECL) {
11000 ins->id = stacks[ins->id].orig_id;
11001 }
11002 ins = ins->next;
11003 } while(ins != first);
11004}
11005
11006static struct triple *peek_triple(struct stack *stacks, struct triple *var)
Eric Biederman66fe2222003-07-04 15:14:04 +000011007{
11008 struct triple_set *head;
11009 struct triple *top_val;
11010 top_val = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000011011 head = stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000011012 if (head) {
11013 top_val = head->member;
11014 }
11015 return top_val;
11016}
11017
Eric Biederman83b991a2003-10-11 06:20:25 +000011018static void push_triple(struct stack *stacks, struct triple *var, struct triple *val)
Eric Biederman66fe2222003-07-04 15:14:04 +000011019{
11020 struct triple_set *new;
11021 /* Append new to the head of the list,
11022 * it's the only sensible behavoir for a stack.
11023 */
11024 new = xcmalloc(sizeof(*new), "triple_set");
11025 new->member = val;
Eric Biederman83b991a2003-10-11 06:20:25 +000011026 new->next = stacks[var->id].top;
11027 stacks[var->id].top = new;
Eric Biederman66fe2222003-07-04 15:14:04 +000011028}
11029
Eric Biederman83b991a2003-10-11 06:20:25 +000011030static void pop_triple(struct stack *stacks, struct triple *var, struct triple *oldval)
Eric Biederman66fe2222003-07-04 15:14:04 +000011031{
11032 struct triple_set *set, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000011033 ptr = &stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000011034 while(*ptr) {
11035 set = *ptr;
11036 if (set->member == oldval) {
11037 *ptr = set->next;
11038 xfree(set);
11039 /* Only free one occurance from the stack */
11040 return;
11041 }
11042 else {
11043 ptr = &set->next;
11044 }
11045 }
11046}
11047
Eric Biedermanb138ac82003-04-22 18:44:01 +000011048/*
11049 * C(V)
11050 * S(V)
11051 */
11052static void fixup_block_phi_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000011053 struct compile_state *state, struct stack *stacks, struct block *parent, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000011054{
11055 struct block_set *set;
11056 struct triple *ptr;
11057 int edge;
11058 if (!parent || !block)
11059 return;
11060 /* Find the edge I am coming in on */
11061 edge = 0;
11062 for(set = block->use; set; set = set->next, edge++) {
11063 if (set->member == parent) {
11064 break;
11065 }
11066 }
11067 if (!set) {
11068 internal_error(state, 0, "phi input is not on a control predecessor");
11069 }
11070 for(ptr = block->first; ; ptr = ptr->next) {
11071 if (ptr->op == OP_PHI) {
11072 struct triple *var, *val, **slot;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011073 var = MISC(ptr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011074 if (!var) {
11075 internal_error(state, ptr, "no var???");
11076 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011077 /* Find the current value of the variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000011078 val = peek_triple(stacks, var);
11079 if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011080 internal_error(state, val, "bad value in phi");
11081 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000011082 if (edge >= TRIPLE_RHS(ptr->sizes)) {
11083 internal_error(state, ptr, "edges > phi rhs");
11084 }
11085 slot = &RHS(ptr, edge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011086 if ((*slot != 0) && (*slot != val)) {
11087 internal_error(state, ptr, "phi already bound on this edge");
11088 }
11089 *slot = val;
11090 use_triple(val, ptr);
11091 }
11092 if (ptr == block->last) {
11093 break;
11094 }
11095 }
11096}
11097
11098
11099static void rename_block_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000011100 struct compile_state *state, struct stack *stacks, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000011101{
11102 struct block_set *user;
11103 struct triple *ptr, *next, *last;
11104 int done;
11105 if (!block)
11106 return;
11107 last = block->first;
11108 done = 0;
11109 for(ptr = block->first; !done; ptr = next) {
11110 next = ptr->next;
11111 if (ptr == block->last) {
11112 done = 1;
11113 }
11114 /* RHS(A) */
11115 if (ptr->op == OP_READ) {
11116 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011117 var = RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011118 unuse_triple(var, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +000011119 /* Find the current value of the variable */
11120 val = peek_triple(stacks, var);
11121 if (!val) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011122 error(state, ptr, "variable used without being set");
11123 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011124 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
11125 internal_error(state, val, "bad value in read");
11126 }
11127 propogate_use(state, ptr, val);
11128 release_triple(state, ptr);
11129 continue;
11130 }
11131 /* LHS(A) */
11132 if (ptr->op == OP_WRITE) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000011133 struct triple *var, *val, *tval;
Eric Biederman530b5192003-07-01 10:05:30 +000011134 var = RHS(ptr, 0);
11135 tval = val = RHS(ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011136 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
Eric Biederman678d8162003-07-03 03:59:38 +000011137 internal_error(state, ptr, "bad value in write");
Eric Biedermanb138ac82003-04-22 18:44:01 +000011138 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000011139 /* Insert a copy if the types differ */
11140 if (!equiv_types(ptr->type, val->type)) {
11141 if (val->op == OP_INTCONST) {
11142 tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
11143 tval->u.cval = val->u.cval;
11144 }
11145 else {
11146 tval = pre_triple(state, ptr, OP_COPY, ptr->type, val, 0);
11147 use_triple(val, tval);
11148 }
Eric Biederman83b991a2003-10-11 06:20:25 +000011149 transform_to_arch_instruction(state, tval);
Eric Biedermand1ea5392003-06-28 06:49:45 +000011150 unuse_triple(val, ptr);
Eric Biederman530b5192003-07-01 10:05:30 +000011151 RHS(ptr, 1) = tval;
Eric Biedermand1ea5392003-06-28 06:49:45 +000011152 use_triple(tval, ptr);
11153 }
11154 propogate_use(state, ptr, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011155 unuse_triple(var, ptr);
11156 /* Push OP_WRITE ptr->right onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000011157 push_triple(stacks, var, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011158 }
11159 if (ptr->op == OP_PHI) {
11160 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011161 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011162 /* Push OP_PHI onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000011163 push_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011164 }
11165 last = ptr;
11166 }
11167 block->last = last;
11168
11169 /* Fixup PHI functions in the cf successors */
Eric Biederman66fe2222003-07-04 15:14:04 +000011170 fixup_block_phi_variables(state, stacks, block, block->left);
11171 fixup_block_phi_variables(state, stacks, block, block->right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011172 /* rename variables in the dominated nodes */
11173 for(user = block->idominates; user; user = user->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000011174 rename_block_variables(state, stacks, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011175 }
11176 /* pop the renamed variable stack */
11177 last = block->first;
11178 done = 0;
11179 for(ptr = block->first; !done ; ptr = next) {
11180 next = ptr->next;
11181 if (ptr == block->last) {
11182 done = 1;
11183 }
11184 if (ptr->op == OP_WRITE) {
11185 struct triple *var;
Eric Biederman530b5192003-07-01 10:05:30 +000011186 var = RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011187 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000011188 pop_triple(stacks, var, RHS(ptr, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011189 release_triple(state, ptr);
11190 continue;
11191 }
11192 if (ptr->op == OP_PHI) {
11193 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011194 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011195 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000011196 pop_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011197 }
11198 last = ptr;
11199 }
11200 block->last = last;
11201}
11202
Eric Biederman83b991a2003-10-11 06:20:25 +000011203static void rename_variables(struct compile_state *state)
11204{
11205 struct stack *stacks;
11206 int adecls;
11207
11208 /* Allocate stacks for the Variables */
11209 adecls = count_adecls(state);
11210 stacks = xcmalloc(sizeof(stacks[0])*(adecls + 1), "adecl stacks");
11211
11212 /* Give each adecl a stack */
11213 number_adecls(state, stacks);
11214
11215 /* Rename the variables */
11216 rename_block_variables(state, stacks, state->first_block);
11217
11218 /* Remove the stacks from the adecls */
11219 restore_adecls(state, stacks);
11220 xfree(stacks);
11221}
11222
Eric Biedermanb138ac82003-04-22 18:44:01 +000011223static void prune_block_variables(struct compile_state *state,
11224 struct block *block)
11225{
11226 struct block_set *user;
11227 struct triple *next, *last, *ptr;
11228 int done;
11229 last = block->first;
11230 done = 0;
11231 for(ptr = block->first; !done; ptr = next) {
11232 next = ptr->next;
11233 if (ptr == block->last) {
11234 done = 1;
11235 }
11236 if (ptr->op == OP_ADECL) {
11237 struct triple_set *user, *next;
11238 for(user = ptr->use; user; user = next) {
11239 struct triple *use;
11240 next = user->next;
11241 use = user->member;
11242 if (use->op != OP_PHI) {
11243 internal_error(state, use, "decl still used");
11244 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000011245 if (MISC(use, 0) != ptr) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011246 internal_error(state, use, "bad phi use of decl");
11247 }
11248 unuse_triple(ptr, use);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011249 MISC(use, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011250 }
11251 release_triple(state, ptr);
11252 continue;
11253 }
11254 last = ptr;
11255 }
11256 block->last = last;
11257 for(user = block->idominates; user; user = user->next) {
11258 prune_block_variables(state, user->member);
11259 }
11260}
11261
Eric Biederman66fe2222003-07-04 15:14:04 +000011262struct phi_triple {
11263 struct triple *phi;
11264 unsigned orig_id;
11265 int alive;
11266};
11267
11268static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi)
11269{
11270 struct triple **slot;
11271 int zrhs, i;
11272 if (live[phi->id].alive) {
11273 return;
11274 }
11275 live[phi->id].alive = 1;
11276 zrhs = TRIPLE_RHS(phi->sizes);
11277 slot = &RHS(phi, 0);
11278 for(i = 0; i < zrhs; i++) {
11279 struct triple *used;
11280 used = slot[i];
11281 if (used && (used->op == OP_PHI)) {
11282 keep_phi(state, live, used);
11283 }
11284 }
11285}
11286
11287static void prune_unused_phis(struct compile_state *state)
11288{
11289 struct triple *first, *phi;
11290 struct phi_triple *live;
11291 int phis, i;
11292
Eric Biederman66fe2222003-07-04 15:14:04 +000011293 /* Find the first instruction */
Eric Biederman83b991a2003-10-11 06:20:25 +000011294 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000011295
11296 /* Count how many phi functions I need to process */
11297 phis = 0;
11298 for(phi = first->next; phi != first; phi = phi->next) {
11299 if (phi->op == OP_PHI) {
11300 phis += 1;
11301 }
11302 }
11303
11304 /* Mark them all dead */
11305 live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple");
11306 phis = 0;
11307 for(phi = first->next; phi != first; phi = phi->next) {
11308 if (phi->op != OP_PHI) {
11309 continue;
11310 }
11311 live[phis].alive = 0;
11312 live[phis].orig_id = phi->id;
11313 live[phis].phi = phi;
11314 phi->id = phis;
11315 phis += 1;
11316 }
11317
11318 /* Mark phis alive that are used by non phis */
11319 for(i = 0; i < phis; i++) {
11320 struct triple_set *set;
11321 for(set = live[i].phi->use; !live[i].alive && set; set = set->next) {
11322 if (set->member->op != OP_PHI) {
11323 keep_phi(state, live, live[i].phi);
11324 break;
11325 }
11326 }
11327 }
11328
11329 /* Delete the extraneous phis */
11330 for(i = 0; i < phis; i++) {
11331 struct triple **slot;
11332 int zrhs, j;
11333 if (!live[i].alive) {
11334 release_triple(state, live[i].phi);
11335 continue;
11336 }
11337 phi = live[i].phi;
11338 slot = &RHS(phi, 0);
11339 zrhs = TRIPLE_RHS(phi->sizes);
11340 for(j = 0; j < zrhs; j++) {
11341 if(!slot[j]) {
11342 error(state, phi, "variable not set on all paths to use");
11343 }
11344 }
11345 }
11346 xfree(live);
11347}
11348
Eric Biedermanb138ac82003-04-22 18:44:01 +000011349static void transform_to_ssa_form(struct compile_state *state)
11350{
11351 insert_phi_operations(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000011352 rename_variables(state);
Eric Biederman66fe2222003-07-04 15:14:04 +000011353
Eric Biedermanb138ac82003-04-22 18:44:01 +000011354 prune_block_variables(state, state->first_block);
Eric Biederman66fe2222003-07-04 15:14:04 +000011355 prune_unused_phis(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011356}
11357
11358
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011359static void clear_vertex(
11360 struct compile_state *state, struct block *block, void *arg)
11361{
Eric Biederman83b991a2003-10-11 06:20:25 +000011362 /* Clear the current blocks vertex and the vertex of all
11363 * of the current blocks neighbors in case there are malformed
11364 * blocks with now instructions at this point.
11365 */
11366 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011367 block->vertex = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000011368 if (block->left) {
11369 block->left->vertex = 0;
11370 }
11371 if (block->right) {
11372 block->right->vertex = 0;
11373 }
11374 for(user = block->use; user; user = user->next) {
11375 user->member->vertex = 0;
11376 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011377}
11378
11379static void mark_live_block(
11380 struct compile_state *state, struct block *block, int *next_vertex)
11381{
11382 /* See if this is a block that has not been marked */
11383 if (block->vertex != 0) {
11384 return;
11385 }
11386 block->vertex = *next_vertex;
11387 *next_vertex += 1;
11388 if (triple_is_branch(state, block->last)) {
11389 struct triple **targ;
11390 targ = triple_targ(state, block->last, 0);
11391 for(; targ; targ = triple_targ(state, block->last, targ)) {
11392 if (!*targ) {
11393 continue;
11394 }
11395 if (!triple_stores_block(state, *targ)) {
11396 internal_error(state, 0, "bad targ");
11397 }
11398 mark_live_block(state, (*targ)->u.block, next_vertex);
11399 }
11400 }
Eric Biederman83b991a2003-10-11 06:20:25 +000011401 else if (block->last->next != state->first) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011402 struct triple *ins;
11403 ins = block->last->next;
11404 if (!triple_stores_block(state, ins)) {
11405 internal_error(state, 0, "bad block start");
11406 }
11407 mark_live_block(state, ins->u.block, next_vertex);
11408 }
11409}
11410
Eric Biedermanb138ac82003-04-22 18:44:01 +000011411static void transform_from_ssa_form(struct compile_state *state)
11412{
11413 /* To get out of ssa form we insert moves on the incoming
11414 * edges to blocks containting phi functions.
11415 */
11416 struct triple *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000011417 struct triple *phi, *var, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011418 int next_vertex;
11419
11420 /* Walk the control flow to see which blocks remain alive */
11421 walk_blocks(state, clear_vertex, 0);
11422 next_vertex = 1;
11423 mark_live_block(state, state->first_block, &next_vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011424
Eric Biederman83b991a2003-10-11 06:20:25 +000011425#if 0
11426 fprintf(stderr, "@ %s:%d\n", __FILE__, __LINE__);
11427 print_blocks(state, stderr);
11428#endif
11429
Eric Biedermanb138ac82003-04-22 18:44:01 +000011430 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000011431 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011432 for(phi = first->next; phi != first ; phi = next) {
11433 struct block_set *set;
11434 struct block *block;
11435 struct triple **slot;
Eric Biederman83b991a2003-10-11 06:20:25 +000011436 struct triple *var;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011437 struct triple_set *use, *use_next;
11438 int edge, used;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011439 next = phi->next;
11440 if (phi->op != OP_PHI) {
11441 continue;
11442 }
Eric Biederman83b991a2003-10-11 06:20:25 +000011443
Eric Biedermanb138ac82003-04-22 18:44:01 +000011444 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011445 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011446
Eric Biederman83b991a2003-10-11 06:20:25 +000011447 /* If this phi is in a dead block just forget it */
11448 if (block->vertex == 0) {
11449 release_triple(state, phi);
11450 continue;
11451 }
11452
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011453 /* Forget uses from code in dead blocks */
11454 for(use = phi->use; use; use = use_next) {
11455 struct block *ublock;
11456 struct triple **expr;
11457 use_next = use->next;
11458 ublock = block_of_triple(state, use->member);
11459 if ((use->member == phi) || (ublock->vertex != 0)) {
11460 continue;
11461 }
11462 expr = triple_rhs(state, use->member, 0);
11463 for(; expr; expr = triple_rhs(state, use->member, expr)) {
11464 if (*expr == phi) {
11465 *expr = 0;
11466 }
11467 }
11468 unuse_triple(phi, use->member);
11469 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011470 /* A variable to replace the phi function */
11471 var = post_triple(state, phi, OP_ADECL, phi->type, 0,0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011472
Eric Biederman83b991a2003-10-11 06:20:25 +000011473 /* Replaces use of phi with var */
11474 propogate_use(state, phi, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011475
11476 /* Walk all of the incoming edges/blocks and insert moves.
11477 */
Eric Biederman83b991a2003-10-11 06:20:25 +000011478 used = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011479 for(edge = 0, set = block->use; set; set = set->next, edge++) {
Eric Biederman83b991a2003-10-11 06:20:25 +000011480 struct block *eblock, *vblock;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011481 struct triple *move;
Eric Biederman530b5192003-07-01 10:05:30 +000011482 struct triple *val, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011483 eblock = set->member;
11484 val = slot[edge];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011485 slot[edge] = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011486 unuse_triple(val, phi);
Eric Biederman83b991a2003-10-11 06:20:25 +000011487 vblock = block_of_triple(state, val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011488
Eric Biederman83b991a2003-10-11 06:20:25 +000011489 /* If we don't have a value that belongs in an OP_WRITE
11490 * continue on.
11491 */
11492 if (!val || (val == &zero_triple) || (val == phi) ||
11493 (!vblock) || (vblock->vertex == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011494 continue;
11495 }
Eric Biederman83b991a2003-10-11 06:20:25 +000011496
11497 /* If the value occurs in a dead block see if a replacement
11498 * block can be found.
11499 */
11500 while(eblock && (eblock->vertex == 0)) {
11501 eblock = eblock->idom;
11502 }
11503 /* If not continue on with the next value. */
11504 if (!eblock || (eblock->vertex == 0)) {
11505 continue;
11506 }
11507
11508 /* If we have an empty incoming block ignore it. */
11509 if (!eblock->first) {
11510 internal_error(state, 0, "empty block?");
11511 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011512
Eric Biederman530b5192003-07-01 10:05:30 +000011513 /* Make certain the write is placed in the edge block... */
11514 base = eblock->first;
11515 if (block_of_triple(state, val) == eblock) {
11516 base = val;
11517 }
Eric Biederman83b991a2003-10-11 06:20:25 +000011518 move = post_triple(state, base, OP_WRITE, var->type, var, val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011519 use_triple(val, move);
11520 use_triple(var, move);
Eric Biederman83b991a2003-10-11 06:20:25 +000011521 used = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011522 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011523 /* If var is not used free it */
11524 if (!used) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011525 free_triple(state, var);
11526 }
11527
11528 /* Release the phi function */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011529 release_triple(state, phi);
11530 }
11531
Eric Biederman83b991a2003-10-11 06:20:25 +000011532 /* Walk all of the operations to find the adecls */
11533 for(var = first->next; var != first ; var = var->next) {
11534 struct triple_set *use, *use_next;
11535 if (var->op != OP_ADECL) {
11536 continue;
11537 }
11538
11539 /* Walk through all of the rhs uses of var and
11540 * replace them with read of var.
11541 */
11542 for(use = var->use; use; use = use_next) {
11543 struct triple *read, *user;
11544 struct triple **slot;
11545 int zrhs, i, used;
11546 use_next = use->next;
11547 user = use->member;
11548
11549 /* Generate a read of var */
11550 read = pre_triple(state, user, OP_READ, var->type, var, 0);
11551 use_triple(var, read);
11552
11553 /* Find the rhs uses and see if they need to be replaced */
11554 used = 0;
11555 zrhs = TRIPLE_RHS(user->sizes);
11556 slot = &RHS(user, 0);
11557 for(i = 0; i < zrhs; i++) {
11558 if ((slot[i] == var) &&
11559 ((i != 0) || (user->op != OP_WRITE)))
11560 {
11561 slot[i] = read;
11562 used = 1;
11563 }
11564 }
11565 /* If we did use it cleanup the uses */
11566 if (used) {
11567 unuse_triple(var, user);
11568 use_triple(read, user);
11569 }
11570 /* If we didn't use it release the extra triple */
11571 else {
11572 release_triple(state, read);
11573 }
11574 }
11575 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011576}
11577
Eric Biederman83b991a2003-10-11 06:20:25 +000011578#if 0
11579#define HI() do { fprintf(stderr, "@ %s:%d\n", __FILE__, __LINE__); print_blocks(state, stderr); } while (0)
11580#else
11581#define HI()
11582#endif
11583static void rebuild_ssa_form(struct compile_state *state)
11584{
11585HI();
11586 transform_from_ssa_form(state);
11587HI();
11588 free_basic_blocks(state);
11589 analyze_basic_blocks(state);
11590HI();
11591 insert_phi_operations(state);
11592HI();
11593 rename_variables(state);
11594HI();
11595
11596 prune_block_variables(state, state->first_block);
11597HI();
11598 prune_unused_phis(state);
11599HI();
11600}
11601#undef HI
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011602
11603/*
11604 * Register conflict resolution
11605 * =========================================================
11606 */
11607
11608static struct reg_info find_def_color(
11609 struct compile_state *state, struct triple *def)
11610{
11611 struct triple_set *set;
11612 struct reg_info info;
11613 info.reg = REG_UNSET;
11614 info.regcm = 0;
11615 if (!triple_is_def(state, def)) {
11616 return info;
11617 }
11618 info = arch_reg_lhs(state, def, 0);
11619 if (info.reg >= MAX_REGISTERS) {
11620 info.reg = REG_UNSET;
11621 }
11622 for(set = def->use; set; set = set->next) {
11623 struct reg_info tinfo;
11624 int i;
11625 i = find_rhs_use(state, set->member, def);
11626 if (i < 0) {
11627 continue;
11628 }
11629 tinfo = arch_reg_rhs(state, set->member, i);
11630 if (tinfo.reg >= MAX_REGISTERS) {
11631 tinfo.reg = REG_UNSET;
11632 }
11633 if ((tinfo.reg != REG_UNSET) &&
11634 (info.reg != REG_UNSET) &&
11635 (tinfo.reg != info.reg)) {
11636 internal_error(state, def, "register conflict");
11637 }
11638 if ((info.regcm & tinfo.regcm) == 0) {
11639 internal_error(state, def, "regcm conflict %x & %x == 0",
11640 info.regcm, tinfo.regcm);
11641 }
11642 if (info.reg == REG_UNSET) {
11643 info.reg = tinfo.reg;
11644 }
11645 info.regcm &= tinfo.regcm;
11646 }
11647 if (info.reg >= MAX_REGISTERS) {
11648 internal_error(state, def, "register out of range");
11649 }
11650 return info;
11651}
11652
11653static struct reg_info find_lhs_pre_color(
11654 struct compile_state *state, struct triple *ins, int index)
11655{
11656 struct reg_info info;
11657 int zlhs, zrhs, i;
11658 zrhs = TRIPLE_RHS(ins->sizes);
11659 zlhs = TRIPLE_LHS(ins->sizes);
11660 if (!zlhs && triple_is_def(state, ins)) {
11661 zlhs = 1;
11662 }
11663 if (index >= zlhs) {
11664 internal_error(state, ins, "Bad lhs %d", index);
11665 }
11666 info = arch_reg_lhs(state, ins, index);
11667 for(i = 0; i < zrhs; i++) {
11668 struct reg_info rinfo;
11669 rinfo = arch_reg_rhs(state, ins, i);
11670 if ((info.reg == rinfo.reg) &&
11671 (rinfo.reg >= MAX_REGISTERS)) {
11672 struct reg_info tinfo;
11673 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
11674 info.reg = tinfo.reg;
11675 info.regcm &= tinfo.regcm;
11676 break;
11677 }
11678 }
11679 if (info.reg >= MAX_REGISTERS) {
11680 info.reg = REG_UNSET;
11681 }
11682 return info;
11683}
11684
11685static struct reg_info find_rhs_post_color(
11686 struct compile_state *state, struct triple *ins, int index);
11687
11688static struct reg_info find_lhs_post_color(
11689 struct compile_state *state, struct triple *ins, int index)
11690{
11691 struct triple_set *set;
11692 struct reg_info info;
11693 struct triple *lhs;
Eric Biederman530b5192003-07-01 10:05:30 +000011694#if DEBUG_TRIPLE_COLOR
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011695 fprintf(stderr, "find_lhs_post_color(%p, %d)\n",
11696 ins, index);
11697#endif
11698 if ((index == 0) && triple_is_def(state, ins)) {
11699 lhs = ins;
11700 }
11701 else if (index < TRIPLE_LHS(ins->sizes)) {
11702 lhs = LHS(ins, index);
11703 }
11704 else {
11705 internal_error(state, ins, "Bad lhs %d", index);
11706 lhs = 0;
11707 }
11708 info = arch_reg_lhs(state, ins, index);
11709 if (info.reg >= MAX_REGISTERS) {
11710 info.reg = REG_UNSET;
11711 }
11712 for(set = lhs->use; set; set = set->next) {
11713 struct reg_info rinfo;
11714 struct triple *user;
11715 int zrhs, i;
11716 user = set->member;
11717 zrhs = TRIPLE_RHS(user->sizes);
11718 for(i = 0; i < zrhs; i++) {
11719 if (RHS(user, i) != lhs) {
11720 continue;
11721 }
11722 rinfo = find_rhs_post_color(state, user, i);
11723 if ((info.reg != REG_UNSET) &&
11724 (rinfo.reg != REG_UNSET) &&
11725 (info.reg != rinfo.reg)) {
11726 internal_error(state, ins, "register conflict");
11727 }
11728 if ((info.regcm & rinfo.regcm) == 0) {
11729 internal_error(state, ins, "regcm conflict %x & %x == 0",
11730 info.regcm, rinfo.regcm);
11731 }
11732 if (info.reg == REG_UNSET) {
11733 info.reg = rinfo.reg;
11734 }
11735 info.regcm &= rinfo.regcm;
11736 }
11737 }
Eric Biederman530b5192003-07-01 10:05:30 +000011738#if DEBUG_TRIPLE_COLOR
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011739 fprintf(stderr, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
11740 ins, index, info.reg, info.regcm);
11741#endif
11742 return info;
11743}
11744
11745static struct reg_info find_rhs_post_color(
11746 struct compile_state *state, struct triple *ins, int index)
11747{
11748 struct reg_info info, rinfo;
11749 int zlhs, i;
Eric Biederman530b5192003-07-01 10:05:30 +000011750#if DEBUG_TRIPLE_COLOR
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011751 fprintf(stderr, "find_rhs_post_color(%p, %d)\n",
11752 ins, index);
11753#endif
11754 rinfo = arch_reg_rhs(state, ins, index);
11755 zlhs = TRIPLE_LHS(ins->sizes);
11756 if (!zlhs && triple_is_def(state, ins)) {
11757 zlhs = 1;
11758 }
11759 info = rinfo;
11760 if (info.reg >= MAX_REGISTERS) {
11761 info.reg = REG_UNSET;
11762 }
11763 for(i = 0; i < zlhs; i++) {
11764 struct reg_info linfo;
11765 linfo = arch_reg_lhs(state, ins, i);
11766 if ((linfo.reg == rinfo.reg) &&
11767 (linfo.reg >= MAX_REGISTERS)) {
11768 struct reg_info tinfo;
11769 tinfo = find_lhs_post_color(state, ins, i);
11770 if (tinfo.reg >= MAX_REGISTERS) {
11771 tinfo.reg = REG_UNSET;
11772 }
Eric Biederman530b5192003-07-01 10:05:30 +000011773 info.regcm &= linfo.regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011774 info.regcm &= tinfo.regcm;
11775 if (info.reg != REG_UNSET) {
11776 internal_error(state, ins, "register conflict");
11777 }
11778 if (info.regcm == 0) {
11779 internal_error(state, ins, "regcm conflict");
11780 }
11781 info.reg = tinfo.reg;
11782 }
11783 }
Eric Biederman530b5192003-07-01 10:05:30 +000011784#if DEBUG_TRIPLE_COLOR
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011785 fprintf(stderr, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
11786 ins, index, info.reg, info.regcm);
11787#endif
11788 return info;
11789}
11790
11791static struct reg_info find_lhs_color(
11792 struct compile_state *state, struct triple *ins, int index)
11793{
11794 struct reg_info pre, post, info;
Eric Biederman530b5192003-07-01 10:05:30 +000011795#if DEBUG_TRIPLE_COLOR
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011796 fprintf(stderr, "find_lhs_color(%p, %d)\n",
11797 ins, index);
11798#endif
11799 pre = find_lhs_pre_color(state, ins, index);
11800 post = find_lhs_post_color(state, ins, index);
11801 if ((pre.reg != post.reg) &&
11802 (pre.reg != REG_UNSET) &&
11803 (post.reg != REG_UNSET)) {
11804 internal_error(state, ins, "register conflict");
11805 }
11806 info.regcm = pre.regcm & post.regcm;
11807 info.reg = pre.reg;
11808 if (info.reg == REG_UNSET) {
11809 info.reg = post.reg;
11810 }
Eric Biederman530b5192003-07-01 10:05:30 +000011811#if DEBUG_TRIPLE_COLOR
11812 fprintf(stderr, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
11813 ins, index, info.reg, info.regcm,
11814 pre.reg, pre.regcm, post.reg, post.regcm);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011815#endif
11816 return info;
11817}
11818
11819static struct triple *post_copy(struct compile_state *state, struct triple *ins)
11820{
11821 struct triple_set *entry, *next;
11822 struct triple *out;
11823 struct reg_info info, rinfo;
11824
11825 info = arch_reg_lhs(state, ins, 0);
11826 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
11827 use_triple(RHS(out, 0), out);
11828 /* Get the users of ins to use out instead */
11829 for(entry = ins->use; entry; entry = next) {
11830 int i;
11831 next = entry->next;
11832 if (entry->member == out) {
11833 continue;
11834 }
11835 i = find_rhs_use(state, entry->member, ins);
11836 if (i < 0) {
11837 continue;
11838 }
11839 rinfo = arch_reg_rhs(state, entry->member, i);
11840 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
11841 continue;
11842 }
11843 replace_rhs_use(state, ins, out, entry->member);
11844 }
11845 transform_to_arch_instruction(state, out);
11846 return out;
11847}
11848
Eric Biedermand1ea5392003-06-28 06:49:45 +000011849static struct triple *typed_pre_copy(
11850 struct compile_state *state, struct type *type, struct triple *ins, int index)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011851{
11852 /* Carefully insert enough operations so that I can
11853 * enter any operation with a GPR32.
11854 */
11855 struct triple *in;
11856 struct triple **expr;
Eric Biedermand1ea5392003-06-28 06:49:45 +000011857 unsigned classes;
11858 struct reg_info info;
Eric Biederman153ea352003-06-20 14:43:20 +000011859 if (ins->op == OP_PHI) {
11860 internal_error(state, ins, "pre_copy on a phi?");
11861 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000011862 classes = arch_type_to_regcm(state, type);
11863 info = arch_reg_rhs(state, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011864 expr = &RHS(ins, index);
Eric Biedermand1ea5392003-06-28 06:49:45 +000011865 if ((info.regcm & classes) == 0) {
11866 internal_error(state, ins, "pre_copy with no register classes");
11867 }
11868 in = pre_triple(state, ins, OP_COPY, type, *expr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011869 unuse_triple(*expr, ins);
11870 *expr = in;
11871 use_triple(RHS(in, 0), in);
11872 use_triple(in, ins);
11873 transform_to_arch_instruction(state, in);
11874 return in;
Eric Biedermand1ea5392003-06-28 06:49:45 +000011875
11876}
11877static struct triple *pre_copy(
11878 struct compile_state *state, struct triple *ins, int index)
11879{
11880 return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011881}
11882
11883
Eric Biedermanb138ac82003-04-22 18:44:01 +000011884static void insert_copies_to_phi(struct compile_state *state)
11885{
11886 /* To get out of ssa form we insert moves on the incoming
11887 * edges to blocks containting phi functions.
11888 */
11889 struct triple *first;
11890 struct triple *phi;
11891
11892 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000011893 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011894 for(phi = first->next; phi != first ; phi = phi->next) {
11895 struct block_set *set;
11896 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000011897 struct triple **slot, *copy;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011898 int edge;
11899 if (phi->op != OP_PHI) {
11900 continue;
11901 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011902 phi->id |= TRIPLE_FLAG_POST_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011903 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011904 slot = &RHS(phi, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000011905 /* Phi's that feed into mandatory live range joins
11906 * cause nasty complications. Insert a copy of
11907 * the phi value so I never have to deal with
11908 * that in the rest of the code.
11909 */
11910 copy = post_copy(state, phi);
11911 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011912 /* Walk all of the incoming edges/blocks and insert moves.
11913 */
11914 for(edge = 0, set = block->use; set; set = set->next, edge++) {
11915 struct block *eblock;
11916 struct triple *move;
11917 struct triple *val;
11918 struct triple *ptr;
11919 eblock = set->member;
11920 val = slot[edge];
11921
11922 if (val == phi) {
11923 continue;
11924 }
11925
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011926 get_occurance(val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011927 move = build_triple(state, OP_COPY, phi->type, val, 0,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011928 val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011929 move->u.block = eblock;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011930 move->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011931 use_triple(val, move);
11932
11933 slot[edge] = move;
11934 unuse_triple(val, phi);
11935 use_triple(move, phi);
11936
Eric Biederman66fe2222003-07-04 15:14:04 +000011937 /* Walk up the dominator tree until I have found the appropriate block */
11938 while(eblock && !tdominates(state, val, eblock->last)) {
11939 eblock = eblock->idom;
11940 }
11941 if (!eblock) {
11942 internal_error(state, phi, "Cannot find block dominated by %p",
11943 val);
11944 }
11945
Eric Biedermanb138ac82003-04-22 18:44:01 +000011946 /* Walk through the block backwards to find
11947 * an appropriate location for the OP_COPY.
11948 */
11949 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
11950 struct triple **expr;
11951 if ((ptr == phi) || (ptr == val)) {
11952 goto out;
11953 }
11954 expr = triple_rhs(state, ptr, 0);
11955 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11956 if ((*expr) == phi) {
11957 goto out;
11958 }
11959 }
11960 }
11961 out:
Eric Biederman0babc1c2003-05-09 02:39:00 +000011962 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011963 internal_error(state, ptr,
11964 "Could not insert write to phi");
11965 }
11966 insert_triple(state, ptr->next, move);
11967 if (eblock->last == ptr) {
11968 eblock->last = move;
11969 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011970 transform_to_arch_instruction(state, move);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011971 }
11972 }
11973}
11974
11975struct triple_reg_set {
11976 struct triple_reg_set *next;
11977 struct triple *member;
11978 struct triple *new;
11979};
11980
11981struct reg_block {
11982 struct block *block;
11983 struct triple_reg_set *in;
11984 struct triple_reg_set *out;
11985 int vertex;
11986};
11987
11988static int do_triple_set(struct triple_reg_set **head,
11989 struct triple *member, struct triple *new_member)
11990{
11991 struct triple_reg_set **ptr, *new;
11992 if (!member)
11993 return 0;
11994 ptr = head;
11995 while(*ptr) {
11996 if ((*ptr)->member == member) {
11997 return 0;
11998 }
11999 ptr = &(*ptr)->next;
12000 }
12001 new = xcmalloc(sizeof(*new), "triple_set");
12002 new->member = member;
12003 new->new = new_member;
12004 new->next = *head;
12005 *head = new;
12006 return 1;
12007}
12008
12009static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
12010{
12011 struct triple_reg_set *entry, **ptr;
12012 ptr = head;
12013 while(*ptr) {
12014 entry = *ptr;
12015 if (entry->member == member) {
12016 *ptr = entry->next;
12017 xfree(entry);
12018 return;
12019 }
12020 else {
12021 ptr = &entry->next;
12022 }
12023 }
12024}
12025
12026static int in_triple(struct reg_block *rb, struct triple *in)
12027{
12028 return do_triple_set(&rb->in, in, 0);
12029}
12030static void unin_triple(struct reg_block *rb, struct triple *unin)
12031{
12032 do_triple_unset(&rb->in, unin);
12033}
12034
12035static int out_triple(struct reg_block *rb, struct triple *out)
12036{
12037 return do_triple_set(&rb->out, out, 0);
12038}
12039static void unout_triple(struct reg_block *rb, struct triple *unout)
12040{
12041 do_triple_unset(&rb->out, unout);
12042}
12043
12044static int initialize_regblock(struct reg_block *blocks,
12045 struct block *block, int vertex)
12046{
12047 struct block_set *user;
12048 if (!block || (blocks[block->vertex].block == block)) {
12049 return vertex;
12050 }
12051 vertex += 1;
12052 /* Renumber the blocks in a convinient fashion */
12053 block->vertex = vertex;
12054 blocks[vertex].block = block;
12055 blocks[vertex].vertex = vertex;
12056 for(user = block->use; user; user = user->next) {
12057 vertex = initialize_regblock(blocks, user->member, vertex);
12058 }
12059 return vertex;
12060}
12061
12062static int phi_in(struct compile_state *state, struct reg_block *blocks,
12063 struct reg_block *rb, struct block *suc)
12064{
12065 /* Read the conditional input set of a successor block
12066 * (i.e. the input to the phi nodes) and place it in the
12067 * current blocks output set.
12068 */
12069 struct block_set *set;
12070 struct triple *ptr;
12071 int edge;
12072 int done, change;
12073 change = 0;
12074 /* Find the edge I am coming in on */
12075 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
12076 if (set->member == rb->block) {
12077 break;
12078 }
12079 }
12080 if (!set) {
12081 internal_error(state, 0, "Not coming on a control edge?");
12082 }
12083 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
12084 struct triple **slot, *expr, *ptr2;
12085 int out_change, done2;
12086 done = (ptr == suc->last);
12087 if (ptr->op != OP_PHI) {
12088 continue;
12089 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012090 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012091 expr = slot[edge];
12092 out_change = out_triple(rb, expr);
12093 if (!out_change) {
12094 continue;
12095 }
12096 /* If we don't define the variable also plast it
12097 * in the current blocks input set.
12098 */
12099 ptr2 = rb->block->first;
12100 for(done2 = 0; !done2; ptr2 = ptr2->next) {
12101 if (ptr2 == expr) {
12102 break;
12103 }
12104 done2 = (ptr2 == rb->block->last);
12105 }
12106 if (!done2) {
12107 continue;
12108 }
12109 change |= in_triple(rb, expr);
12110 }
12111 return change;
12112}
12113
12114static int reg_in(struct compile_state *state, struct reg_block *blocks,
12115 struct reg_block *rb, struct block *suc)
12116{
12117 struct triple_reg_set *in_set;
12118 int change;
12119 change = 0;
12120 /* Read the input set of a successor block
12121 * and place it in the current blocks output set.
12122 */
12123 in_set = blocks[suc->vertex].in;
12124 for(; in_set; in_set = in_set->next) {
12125 int out_change, done;
12126 struct triple *first, *last, *ptr;
12127 out_change = out_triple(rb, in_set->member);
12128 if (!out_change) {
12129 continue;
12130 }
12131 /* If we don't define the variable also place it
12132 * in the current blocks input set.
12133 */
12134 first = rb->block->first;
12135 last = rb->block->last;
12136 done = 0;
12137 for(ptr = first; !done; ptr = ptr->next) {
12138 if (ptr == in_set->member) {
12139 break;
12140 }
12141 done = (ptr == last);
12142 }
12143 if (!done) {
12144 continue;
12145 }
12146 change |= in_triple(rb, in_set->member);
12147 }
12148 change |= phi_in(state, blocks, rb, suc);
12149 return change;
12150}
12151
12152
12153static int use_in(struct compile_state *state, struct reg_block *rb)
12154{
12155 /* Find the variables we use but don't define and add
12156 * it to the current blocks input set.
12157 */
12158#warning "FIXME is this O(N^2) algorithm bad?"
12159 struct block *block;
12160 struct triple *ptr;
12161 int done;
12162 int change;
12163 block = rb->block;
12164 change = 0;
12165 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
12166 struct triple **expr;
12167 done = (ptr == block->first);
12168 /* The variable a phi function uses depends on the
12169 * control flow, and is handled in phi_in, not
12170 * here.
12171 */
12172 if (ptr->op == OP_PHI) {
12173 continue;
12174 }
12175 expr = triple_rhs(state, ptr, 0);
12176 for(;expr; expr = triple_rhs(state, ptr, expr)) {
12177 struct triple *rhs, *test;
12178 int tdone;
12179 rhs = *expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012180 if (!rhs) {
12181 continue;
12182 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012183 /* See if rhs is defined in this block */
12184 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
12185 tdone = (test == block->first);
12186 if (test == rhs) {
12187 rhs = 0;
12188 break;
12189 }
12190 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012191 /* If I still have a valid rhs add it to in */
12192 change |= in_triple(rb, rhs);
12193 }
12194 }
12195 return change;
12196}
12197
12198static struct reg_block *compute_variable_lifetimes(
12199 struct compile_state *state)
12200{
12201 struct reg_block *blocks;
12202 int change;
12203 blocks = xcmalloc(
12204 sizeof(*blocks)*(state->last_vertex + 1), "reg_block");
12205 initialize_regblock(blocks, state->last_block, 0);
12206 do {
12207 int i;
12208 change = 0;
12209 for(i = 1; i <= state->last_vertex; i++) {
12210 struct reg_block *rb;
12211 rb = &blocks[i];
12212 /* Add the left successor's input set to in */
12213 if (rb->block->left) {
12214 change |= reg_in(state, blocks, rb, rb->block->left);
12215 }
12216 /* Add the right successor's input set to in */
12217 if ((rb->block->right) &&
12218 (rb->block->right != rb->block->left)) {
12219 change |= reg_in(state, blocks, rb, rb->block->right);
12220 }
12221 /* Add use to in... */
12222 change |= use_in(state, rb);
12223 }
12224 } while(change);
12225 return blocks;
12226}
12227
12228static void free_variable_lifetimes(
12229 struct compile_state *state, struct reg_block *blocks)
12230{
12231 int i;
12232 /* free in_set && out_set on each block */
12233 for(i = 1; i <= state->last_vertex; i++) {
12234 struct triple_reg_set *entry, *next;
12235 struct reg_block *rb;
12236 rb = &blocks[i];
12237 for(entry = rb->in; entry ; entry = next) {
12238 next = entry->next;
12239 do_triple_unset(&rb->in, entry->member);
12240 }
12241 for(entry = rb->out; entry; entry = next) {
12242 next = entry->next;
12243 do_triple_unset(&rb->out, entry->member);
12244 }
12245 }
12246 xfree(blocks);
12247
12248}
12249
Eric Biedermanf96a8102003-06-16 16:57:34 +000012250typedef void (*wvl_cb_t)(
Eric Biedermanb138ac82003-04-22 18:44:01 +000012251 struct compile_state *state,
12252 struct reg_block *blocks, struct triple_reg_set *live,
12253 struct reg_block *rb, struct triple *ins, void *arg);
12254
12255static void walk_variable_lifetimes(struct compile_state *state,
12256 struct reg_block *blocks, wvl_cb_t cb, void *arg)
12257{
12258 int i;
12259
12260 for(i = 1; i <= state->last_vertex; i++) {
12261 struct triple_reg_set *live;
12262 struct triple_reg_set *entry, *next;
12263 struct triple *ptr, *prev;
12264 struct reg_block *rb;
12265 struct block *block;
12266 int done;
12267
12268 /* Get the blocks */
12269 rb = &blocks[i];
12270 block = rb->block;
12271
12272 /* Copy out into live */
12273 live = 0;
12274 for(entry = rb->out; entry; entry = next) {
12275 next = entry->next;
12276 do_triple_set(&live, entry->member, entry->new);
12277 }
12278 /* Walk through the basic block calculating live */
12279 for(done = 0, ptr = block->last; !done; ptr = prev) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000012280 struct triple **expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012281
12282 prev = ptr->prev;
12283 done = (ptr == block->first);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012284
12285 /* Ensure the current definition is in live */
12286 if (triple_is_def(state, ptr)) {
12287 do_triple_set(&live, ptr, 0);
12288 }
12289
12290 /* Inform the callback function of what is
12291 * going on.
12292 */
Eric Biedermanf96a8102003-06-16 16:57:34 +000012293 cb(state, blocks, live, rb, ptr, arg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012294
12295 /* Remove the current definition from live */
12296 do_triple_unset(&live, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012297
Eric Biedermanb138ac82003-04-22 18:44:01 +000012298 /* Add the current uses to live.
12299 *
12300 * It is safe to skip phi functions because they do
12301 * not have any block local uses, and the block
12302 * output sets already properly account for what
12303 * control flow depedent uses phi functions do have.
12304 */
12305 if (ptr->op == OP_PHI) {
12306 continue;
12307 }
12308 expr = triple_rhs(state, ptr, 0);
12309 for(;expr; expr = triple_rhs(state, ptr, expr)) {
12310 /* If the triple is not a definition skip it. */
Eric Biederman0babc1c2003-05-09 02:39:00 +000012311 if (!*expr || !triple_is_def(state, *expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012312 continue;
12313 }
12314 do_triple_set(&live, *expr, 0);
12315 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012316 }
12317 /* Free live */
12318 for(entry = live; entry; entry = next) {
12319 next = entry->next;
12320 do_triple_unset(&live, entry->member);
12321 }
12322 }
12323}
12324
12325static int count_triples(struct compile_state *state)
12326{
12327 struct triple *first, *ins;
12328 int triples = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000012329 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012330 ins = first;
12331 do {
12332 triples++;
12333 ins = ins->next;
12334 } while (ins != first);
12335 return triples;
12336}
Eric Biederman66fe2222003-07-04 15:14:04 +000012337
12338
Eric Biedermanb138ac82003-04-22 18:44:01 +000012339struct dead_triple {
12340 struct triple *triple;
12341 struct dead_triple *work_next;
12342 struct block *block;
Eric Biederman83b991a2003-10-11 06:20:25 +000012343 int old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012344 int flags;
12345#define TRIPLE_FLAG_ALIVE 1
12346};
12347
12348
12349static void awaken(
12350 struct compile_state *state,
12351 struct dead_triple *dtriple, struct triple **expr,
12352 struct dead_triple ***work_list_tail)
12353{
12354 struct triple *triple;
12355 struct dead_triple *dt;
12356 if (!expr) {
12357 return;
12358 }
12359 triple = *expr;
12360 if (!triple) {
12361 return;
12362 }
12363 if (triple->id <= 0) {
12364 internal_error(state, triple, "bad triple id: %d",
12365 triple->id);
12366 }
12367 if (triple->op == OP_NOOP) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012368 internal_error(state, triple, "awakening noop?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000012369 return;
12370 }
12371 dt = &dtriple[triple->id];
12372 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
12373 dt->flags |= TRIPLE_FLAG_ALIVE;
12374 if (!dt->work_next) {
12375 **work_list_tail = dt;
12376 *work_list_tail = &dt->work_next;
12377 }
12378 }
12379}
12380
12381static void eliminate_inefectual_code(struct compile_state *state)
12382{
12383 struct block *block;
12384 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
12385 int triples, i;
Eric Biederman83b991a2003-10-11 06:20:25 +000012386 struct triple *first, *final, *ins;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012387
12388 /* Setup the work list */
12389 work_list = 0;
12390 work_list_tail = &work_list;
12391
Eric Biederman83b991a2003-10-11 06:20:25 +000012392 first = state->first;
12393 final = state->first->prev;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012394
12395 /* Count how many triples I have */
12396 triples = count_triples(state);
12397
12398 /* Now put then in an array and mark all of the triples dead */
12399 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
12400
12401 ins = first;
12402 i = 1;
12403 block = 0;
12404 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012405 dtriple[i].triple = ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000012406 dtriple[i].block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012407 dtriple[i].flags = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000012408 dtriple[i].old_id = ins->id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012409 ins->id = i;
12410 /* See if it is an operation we always keep */
Eric Biederman83b991a2003-10-11 06:20:25 +000012411 if (!triple_is_pure(state, ins, dtriple[i].old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012412 awaken(state, dtriple, &ins, &work_list_tail);
12413 }
12414 i++;
12415 ins = ins->next;
12416 } while(ins != first);
12417 while(work_list) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012418 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012419 struct dead_triple *dt;
12420 struct block_set *user;
12421 struct triple **expr;
12422 dt = work_list;
12423 work_list = dt->work_next;
12424 if (!work_list) {
12425 work_list_tail = &work_list;
12426 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012427 /* Make certain the block the current instruction is in lives */
12428 block = block_of_triple(state, dt->triple);
12429 awaken(state, dtriple, &block->first, &work_list_tail);
12430 if (triple_is_branch(state, block->last)) {
12431 awaken(state, dtriple, &block->last, &work_list_tail);
12432 }
12433
Eric Biedermanb138ac82003-04-22 18:44:01 +000012434 /* Wake up the data depencencies of this triple */
12435 expr = 0;
12436 do {
12437 expr = triple_rhs(state, dt->triple, expr);
12438 awaken(state, dtriple, expr, &work_list_tail);
12439 } while(expr);
12440 do {
12441 expr = triple_lhs(state, dt->triple, expr);
12442 awaken(state, dtriple, expr, &work_list_tail);
12443 } while(expr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012444 do {
12445 expr = triple_misc(state, dt->triple, expr);
12446 awaken(state, dtriple, expr, &work_list_tail);
12447 } while(expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012448 /* Wake up the forward control dependencies */
12449 do {
12450 expr = triple_targ(state, dt->triple, expr);
12451 awaken(state, dtriple, expr, &work_list_tail);
12452 } while(expr);
12453 /* Wake up the reverse control dependencies of this triple */
12454 for(user = dt->block->ipdomfrontier; user; user = user->next) {
12455 awaken(state, dtriple, &user->member->last, &work_list_tail);
Eric Biederman83b991a2003-10-11 06:20:25 +000012456 if ((user->member->left != state->last_block) &&
12457 !triple_is_cond_branch(state, user->member->last)) {
12458 internal_error(state, dt->triple,
12459 "conditional branch missing");
12460 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012461 }
12462 }
12463 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
12464 if ((dt->triple->op == OP_NOOP) &&
12465 (dt->flags & TRIPLE_FLAG_ALIVE)) {
12466 internal_error(state, dt->triple, "noop effective?");
12467 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012468 dt->triple->id = dt->old_id; /* Restore the color */
Eric Biedermanb138ac82003-04-22 18:44:01 +000012469 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012470 release_triple(state, dt->triple);
12471 }
12472 }
12473 xfree(dtriple);
12474}
12475
12476
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012477static void insert_mandatory_copies(struct compile_state *state)
12478{
12479 struct triple *ins, *first;
12480
12481 /* The object is with a minimum of inserted copies,
12482 * to resolve in fundamental register conflicts between
12483 * register value producers and consumers.
12484 * Theoretically we may be greater than minimal when we
12485 * are inserting copies before instructions but that
12486 * case should be rare.
12487 */
Eric Biederman83b991a2003-10-11 06:20:25 +000012488 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012489 ins = first;
12490 do {
12491 struct triple_set *entry, *next;
12492 struct triple *tmp;
12493 struct reg_info info;
12494 unsigned reg, regcm;
12495 int do_post_copy, do_pre_copy;
12496 tmp = 0;
12497 if (!triple_is_def(state, ins)) {
12498 goto next;
12499 }
12500 /* Find the architecture specific color information */
12501 info = arch_reg_lhs(state, ins, 0);
12502 if (info.reg >= MAX_REGISTERS) {
12503 info.reg = REG_UNSET;
12504 }
12505
12506 reg = REG_UNSET;
12507 regcm = arch_type_to_regcm(state, ins->type);
12508 do_post_copy = do_pre_copy = 0;
12509
12510 /* Walk through the uses of ins and check for conflicts */
12511 for(entry = ins->use; entry; entry = next) {
12512 struct reg_info rinfo;
12513 int i;
12514 next = entry->next;
12515 i = find_rhs_use(state, entry->member, ins);
12516 if (i < 0) {
12517 continue;
12518 }
12519
12520 /* Find the users color requirements */
12521 rinfo = arch_reg_rhs(state, entry->member, i);
12522 if (rinfo.reg >= MAX_REGISTERS) {
12523 rinfo.reg = REG_UNSET;
12524 }
12525
12526 /* See if I need a pre_copy */
12527 if (rinfo.reg != REG_UNSET) {
12528 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
12529 do_pre_copy = 1;
12530 }
12531 reg = rinfo.reg;
12532 }
12533 regcm &= rinfo.regcm;
12534 regcm = arch_regcm_normalize(state, regcm);
12535 if (regcm == 0) {
12536 do_pre_copy = 1;
12537 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000012538 /* Always use pre_copies for constants.
12539 * They do not take up any registers until a
12540 * copy places them in one.
12541 */
12542 if ((info.reg == REG_UNNEEDED) &&
12543 (rinfo.reg != REG_UNNEEDED)) {
12544 do_pre_copy = 1;
12545 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012546 }
12547 do_post_copy =
12548 !do_pre_copy &&
12549 (((info.reg != REG_UNSET) &&
12550 (reg != REG_UNSET) &&
12551 (info.reg != reg)) ||
12552 ((info.regcm & regcm) == 0));
12553
12554 reg = info.reg;
12555 regcm = info.regcm;
Eric Biedermand1ea5392003-06-28 06:49:45 +000012556 /* 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 +000012557 for(entry = ins->use; entry; entry = next) {
12558 struct reg_info rinfo;
12559 int i;
12560 next = entry->next;
12561 i = find_rhs_use(state, entry->member, ins);
12562 if (i < 0) {
12563 continue;
12564 }
12565
12566 /* Find the users color requirements */
12567 rinfo = arch_reg_rhs(state, entry->member, i);
12568 if (rinfo.reg >= MAX_REGISTERS) {
12569 rinfo.reg = REG_UNSET;
12570 }
12571
12572 /* Now see if it is time to do the pre_copy */
12573 if (rinfo.reg != REG_UNSET) {
12574 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
12575 ((regcm & rinfo.regcm) == 0) ||
12576 /* Don't let a mandatory coalesce sneak
12577 * into a operation that is marked to prevent
12578 * coalescing.
12579 */
12580 ((reg != REG_UNNEEDED) &&
12581 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
12582 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
12583 ) {
12584 if (do_pre_copy) {
12585 struct triple *user;
12586 user = entry->member;
12587 if (RHS(user, i) != ins) {
12588 internal_error(state, user, "bad rhs");
12589 }
12590 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012591 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012592 continue;
12593 } else {
12594 do_post_copy = 1;
12595 }
12596 }
12597 reg = rinfo.reg;
12598 }
12599 if ((regcm & rinfo.regcm) == 0) {
12600 if (do_pre_copy) {
12601 struct triple *user;
12602 user = entry->member;
12603 if (RHS(user, i) != ins) {
12604 internal_error(state, user, "bad rhs");
12605 }
12606 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012607 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012608 continue;
12609 } else {
12610 do_post_copy = 1;
12611 }
12612 }
12613 regcm &= rinfo.regcm;
12614
12615 }
12616 if (do_post_copy) {
12617 struct reg_info pre, post;
12618 tmp = post_copy(state, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012619 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012620 pre = arch_reg_lhs(state, ins, 0);
12621 post = arch_reg_lhs(state, tmp, 0);
12622 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
12623 internal_error(state, tmp, "useless copy");
12624 }
12625 }
12626 next:
12627 ins = ins->next;
12628 } while(ins != first);
12629}
12630
12631
Eric Biedermanb138ac82003-04-22 18:44:01 +000012632struct live_range_edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012633struct live_range_def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012634struct live_range {
12635 struct live_range_edge *edges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012636 struct live_range_def *defs;
12637/* Note. The list pointed to by defs is kept in order.
12638 * That is baring splits in the flow control
12639 * defs dominates defs->next wich dominates defs->next->next
12640 * etc.
12641 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000012642 unsigned color;
12643 unsigned classes;
12644 unsigned degree;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012645 unsigned length;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012646 struct live_range *group_next, **group_prev;
12647};
12648
12649struct live_range_edge {
12650 struct live_range_edge *next;
12651 struct live_range *node;
12652};
12653
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012654struct live_range_def {
12655 struct live_range_def *next;
12656 struct live_range_def *prev;
12657 struct live_range *lr;
12658 struct triple *def;
12659 unsigned orig_id;
12660};
12661
Eric Biedermanb138ac82003-04-22 18:44:01 +000012662#define LRE_HASH_SIZE 2048
12663struct lre_hash {
12664 struct lre_hash *next;
12665 struct live_range *left;
12666 struct live_range *right;
12667};
12668
12669
12670struct reg_state {
12671 struct lre_hash *hash[LRE_HASH_SIZE];
12672 struct reg_block *blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012673 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012674 struct live_range *lr;
12675 struct live_range *low, **low_tail;
12676 struct live_range *high, **high_tail;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012677 unsigned defs;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012678 unsigned ranges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012679 int passes, max_passes;
12680#define MAX_ALLOCATION_PASSES 100
Eric Biedermanb138ac82003-04-22 18:44:01 +000012681};
12682
12683
Eric Biedermand1ea5392003-06-28 06:49:45 +000012684
12685struct print_interference_block_info {
12686 struct reg_state *rstate;
12687 FILE *fp;
12688 int need_edges;
12689};
12690static void print_interference_block(
12691 struct compile_state *state, struct block *block, void *arg)
12692
12693{
12694 struct print_interference_block_info *info = arg;
12695 struct reg_state *rstate = info->rstate;
12696 FILE *fp = info->fp;
12697 struct reg_block *rb;
12698 struct triple *ptr;
12699 int phi_present;
12700 int done;
12701 rb = &rstate->blocks[block->vertex];
12702
12703 fprintf(fp, "\nblock: %p (%d), %p<-%p %p<-%p\n",
12704 block,
12705 block->vertex,
12706 block->left,
12707 block->left && block->left->use?block->left->use->member : 0,
12708 block->right,
12709 block->right && block->right->use?block->right->use->member : 0);
12710 if (rb->in) {
12711 struct triple_reg_set *in_set;
12712 fprintf(fp, " in:");
12713 for(in_set = rb->in; in_set; in_set = in_set->next) {
12714 fprintf(fp, " %-10p", in_set->member);
12715 }
12716 fprintf(fp, "\n");
12717 }
12718 phi_present = 0;
12719 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
12720 done = (ptr == block->last);
12721 if (ptr->op == OP_PHI) {
12722 phi_present = 1;
12723 break;
12724 }
12725 }
12726 if (phi_present) {
12727 int edge;
12728 for(edge = 0; edge < block->users; edge++) {
12729 fprintf(fp, " in(%d):", edge);
12730 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
12731 struct triple **slot;
12732 done = (ptr == block->last);
12733 if (ptr->op != OP_PHI) {
12734 continue;
12735 }
12736 slot = &RHS(ptr, 0);
12737 fprintf(fp, " %-10p", slot[edge]);
12738 }
12739 fprintf(fp, "\n");
12740 }
12741 }
12742 if (block->first->op == OP_LABEL) {
12743 fprintf(fp, "%p:\n", block->first);
12744 }
12745 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000012746 struct live_range *lr;
12747 unsigned id;
12748 int op;
12749 op = ptr->op;
12750 done = (ptr == block->last);
12751 lr = rstate->lrd[ptr->id].lr;
12752
Eric Biedermand1ea5392003-06-28 06:49:45 +000012753 id = ptr->id;
12754 ptr->id = rstate->lrd[id].orig_id;
12755 SET_REG(ptr->id, lr->color);
12756 display_triple(fp, ptr);
12757 ptr->id = id;
12758
12759 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
12760 internal_error(state, ptr, "lr has no defs!");
12761 }
12762 if (info->need_edges) {
12763 if (lr->defs) {
12764 struct live_range_def *lrd;
12765 fprintf(fp, " range:");
12766 lrd = lr->defs;
12767 do {
12768 fprintf(fp, " %-10p", lrd->def);
12769 lrd = lrd->next;
12770 } while(lrd != lr->defs);
12771 fprintf(fp, "\n");
12772 }
12773 if (lr->edges > 0) {
12774 struct live_range_edge *edge;
12775 fprintf(fp, " edges:");
12776 for(edge = lr->edges; edge; edge = edge->next) {
12777 struct live_range_def *lrd;
12778 lrd = edge->node->defs;
12779 do {
12780 fprintf(fp, " %-10p", lrd->def);
12781 lrd = lrd->next;
12782 } while(lrd != edge->node->defs);
12783 fprintf(fp, "|");
12784 }
12785 fprintf(fp, "\n");
12786 }
12787 }
12788 /* Do a bunch of sanity checks */
12789 valid_ins(state, ptr);
12790 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
12791 internal_error(state, ptr, "Invalid triple id: %d",
12792 ptr->id);
12793 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000012794 }
12795 if (rb->out) {
12796 struct triple_reg_set *out_set;
12797 fprintf(fp, " out:");
12798 for(out_set = rb->out; out_set; out_set = out_set->next) {
12799 fprintf(fp, " %-10p", out_set->member);
12800 }
12801 fprintf(fp, "\n");
12802 }
12803 fprintf(fp, "\n");
12804}
12805
12806static void print_interference_blocks(
12807 struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
12808{
12809 struct print_interference_block_info info;
12810 info.rstate = rstate;
12811 info.fp = fp;
12812 info.need_edges = need_edges;
12813 fprintf(fp, "\nlive variables by block\n");
12814 walk_blocks(state, print_interference_block, &info);
12815
12816}
12817
Eric Biedermanb138ac82003-04-22 18:44:01 +000012818static unsigned regc_max_size(struct compile_state *state, int classes)
12819{
12820 unsigned max_size;
12821 int i;
12822 max_size = 0;
12823 for(i = 0; i < MAX_REGC; i++) {
12824 if (classes & (1 << i)) {
12825 unsigned size;
12826 size = arch_regc_size(state, i);
12827 if (size > max_size) {
12828 max_size = size;
12829 }
12830 }
12831 }
12832 return max_size;
12833}
12834
12835static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
12836{
12837 unsigned equivs[MAX_REG_EQUIVS];
12838 int i;
12839 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
12840 internal_error(state, 0, "invalid register");
12841 }
12842 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
12843 internal_error(state, 0, "invalid register");
12844 }
12845 arch_reg_equivs(state, equivs, reg1);
12846 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
12847 if (equivs[i] == reg2) {
12848 return 1;
12849 }
12850 }
12851 return 0;
12852}
12853
12854static void reg_fill_used(struct compile_state *state, char *used, int reg)
12855{
12856 unsigned equivs[MAX_REG_EQUIVS];
12857 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012858 if (reg == REG_UNNEEDED) {
12859 return;
12860 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012861 arch_reg_equivs(state, equivs, reg);
12862 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
12863 used[equivs[i]] = 1;
12864 }
12865 return;
12866}
12867
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012868static void reg_inc_used(struct compile_state *state, char *used, int reg)
12869{
12870 unsigned equivs[MAX_REG_EQUIVS];
12871 int i;
12872 if (reg == REG_UNNEEDED) {
12873 return;
12874 }
12875 arch_reg_equivs(state, equivs, reg);
12876 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
12877 used[equivs[i]] += 1;
12878 }
12879 return;
12880}
12881
Eric Biedermanb138ac82003-04-22 18:44:01 +000012882static unsigned int hash_live_edge(
12883 struct live_range *left, struct live_range *right)
12884{
12885 unsigned int hash, val;
12886 unsigned long lval, rval;
12887 lval = ((unsigned long)left)/sizeof(struct live_range);
12888 rval = ((unsigned long)right)/sizeof(struct live_range);
12889 hash = 0;
12890 while(lval) {
12891 val = lval & 0xff;
12892 lval >>= 8;
12893 hash = (hash *263) + val;
12894 }
12895 while(rval) {
12896 val = rval & 0xff;
12897 rval >>= 8;
12898 hash = (hash *263) + val;
12899 }
12900 hash = hash & (LRE_HASH_SIZE - 1);
12901 return hash;
12902}
12903
12904static struct lre_hash **lre_probe(struct reg_state *rstate,
12905 struct live_range *left, struct live_range *right)
12906{
12907 struct lre_hash **ptr;
12908 unsigned int index;
12909 /* Ensure left <= right */
12910 if (left > right) {
12911 struct live_range *tmp;
12912 tmp = left;
12913 left = right;
12914 right = tmp;
12915 }
12916 index = hash_live_edge(left, right);
12917
12918 ptr = &rstate->hash[index];
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012919 while(*ptr) {
12920 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
12921 break;
12922 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012923 ptr = &(*ptr)->next;
12924 }
12925 return ptr;
12926}
12927
12928static int interfere(struct reg_state *rstate,
12929 struct live_range *left, struct live_range *right)
12930{
12931 struct lre_hash **ptr;
12932 ptr = lre_probe(rstate, left, right);
12933 return ptr && *ptr;
12934}
12935
12936static void add_live_edge(struct reg_state *rstate,
12937 struct live_range *left, struct live_range *right)
12938{
12939 /* FIXME the memory allocation overhead is noticeable here... */
12940 struct lre_hash **ptr, *new_hash;
12941 struct live_range_edge *edge;
12942
12943 if (left == right) {
12944 return;
12945 }
12946 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
12947 return;
12948 }
12949 /* Ensure left <= right */
12950 if (left > right) {
12951 struct live_range *tmp;
12952 tmp = left;
12953 left = right;
12954 right = tmp;
12955 }
12956 ptr = lre_probe(rstate, left, right);
12957 if (*ptr) {
12958 return;
12959 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012960#if 0
12961 fprintf(stderr, "new_live_edge(%p, %p)\n",
12962 left, right);
12963#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000012964 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
12965 new_hash->next = *ptr;
12966 new_hash->left = left;
12967 new_hash->right = right;
12968 *ptr = new_hash;
12969
12970 edge = xmalloc(sizeof(*edge), "live_range_edge");
12971 edge->next = left->edges;
12972 edge->node = right;
12973 left->edges = edge;
12974 left->degree += 1;
12975
12976 edge = xmalloc(sizeof(*edge), "live_range_edge");
12977 edge->next = right->edges;
12978 edge->node = left;
12979 right->edges = edge;
12980 right->degree += 1;
12981}
12982
12983static void remove_live_edge(struct reg_state *rstate,
12984 struct live_range *left, struct live_range *right)
12985{
12986 struct live_range_edge *edge, **ptr;
12987 struct lre_hash **hptr, *entry;
12988 hptr = lre_probe(rstate, left, right);
12989 if (!hptr || !*hptr) {
12990 return;
12991 }
12992 entry = *hptr;
12993 *hptr = entry->next;
12994 xfree(entry);
12995
12996 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
12997 edge = *ptr;
12998 if (edge->node == right) {
12999 *ptr = edge->next;
13000 memset(edge, 0, sizeof(*edge));
13001 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013002 right->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013003 break;
13004 }
13005 }
13006 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
13007 edge = *ptr;
13008 if (edge->node == left) {
13009 *ptr = edge->next;
13010 memset(edge, 0, sizeof(*edge));
13011 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013012 left->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013013 break;
13014 }
13015 }
13016}
13017
13018static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
13019{
13020 struct live_range_edge *edge, *next;
13021 for(edge = range->edges; edge; edge = next) {
13022 next = edge->next;
13023 remove_live_edge(rstate, range, edge->node);
13024 }
13025}
13026
Eric Biederman153ea352003-06-20 14:43:20 +000013027static void transfer_live_edges(struct reg_state *rstate,
13028 struct live_range *dest, struct live_range *src)
13029{
13030 struct live_range_edge *edge, *next;
13031 for(edge = src->edges; edge; edge = next) {
13032 struct live_range *other;
13033 next = edge->next;
13034 other = edge->node;
13035 remove_live_edge(rstate, src, other);
13036 add_live_edge(rstate, dest, other);
13037 }
13038}
13039
Eric Biedermanb138ac82003-04-22 18:44:01 +000013040
13041/* Interference graph...
13042 *
13043 * new(n) --- Return a graph with n nodes but no edges.
13044 * add(g,x,y) --- Return a graph including g with an between x and y
13045 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
13046 * x and y in the graph g
13047 * degree(g, x) --- Return the degree of the node x in the graph g
13048 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
13049 *
13050 * Implement with a hash table && a set of adjcency vectors.
13051 * The hash table supports constant time implementations of add and interfere.
13052 * The adjacency vectors support an efficient implementation of neighbors.
13053 */
13054
13055/*
13056 * +---------------------------------------------------+
13057 * | +--------------+ |
13058 * v v | |
13059 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
13060 *
13061 * -- In simplify implment optimistic coloring... (No backtracking)
13062 * -- Implement Rematerialization it is the only form of spilling we can perform
13063 * Essentially this means dropping a constant from a register because
13064 * we can regenerate it later.
13065 *
13066 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
13067 * coalesce at phi points...
13068 * --- Bias coloring if at all possible do the coalesing a compile time.
13069 *
13070 *
13071 */
13072
13073static void different_colored(
13074 struct compile_state *state, struct reg_state *rstate,
13075 struct triple *parent, struct triple *ins)
13076{
13077 struct live_range *lr;
13078 struct triple **expr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013079 lr = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013080 expr = triple_rhs(state, ins, 0);
13081 for(;expr; expr = triple_rhs(state, ins, expr)) {
13082 struct live_range *lr2;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013083 if (!*expr || (*expr == parent) || (*expr == ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013084 continue;
13085 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013086 lr2 = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013087 if (lr->color == lr2->color) {
13088 internal_error(state, ins, "live range too big");
13089 }
13090 }
13091}
13092
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013093
13094static struct live_range *coalesce_ranges(
13095 struct compile_state *state, struct reg_state *rstate,
13096 struct live_range *lr1, struct live_range *lr2)
13097{
13098 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
13099 unsigned color;
13100 unsigned classes;
13101 if (lr1 == lr2) {
13102 return lr1;
13103 }
13104 if (!lr1->defs || !lr2->defs) {
13105 internal_error(state, 0,
13106 "cannot coalese dead live ranges");
13107 }
13108 if ((lr1->color == REG_UNNEEDED) ||
13109 (lr2->color == REG_UNNEEDED)) {
13110 internal_error(state, 0,
13111 "cannot coalesce live ranges without a possible color");
13112 }
13113 if ((lr1->color != lr2->color) &&
13114 (lr1->color != REG_UNSET) &&
13115 (lr2->color != REG_UNSET)) {
13116 internal_error(state, lr1->defs->def,
13117 "cannot coalesce live ranges of different colors");
13118 }
13119 color = lr1->color;
13120 if (color == REG_UNSET) {
13121 color = lr2->color;
13122 }
13123 classes = lr1->classes & lr2->classes;
13124 if (!classes) {
13125 internal_error(state, lr1->defs->def,
13126 "cannot coalesce live ranges with dissimilar register classes");
13127 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000013128#if DEBUG_COALESCING
13129 fprintf(stderr, "coalescing:");
13130 lrd = lr1->defs;
13131 do {
13132 fprintf(stderr, " %p", lrd->def);
13133 lrd = lrd->next;
13134 } while(lrd != lr1->defs);
13135 fprintf(stderr, " |");
13136 lrd = lr2->defs;
13137 do {
13138 fprintf(stderr, " %p", lrd->def);
13139 lrd = lrd->next;
13140 } while(lrd != lr2->defs);
13141 fprintf(stderr, "\n");
13142#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013143 /* If there is a clear dominate live range put it in lr1,
13144 * For purposes of this test phi functions are
13145 * considered dominated by the definitions that feed into
13146 * them.
13147 */
13148 if ((lr1->defs->prev->def->op == OP_PHI) ||
13149 ((lr2->defs->prev->def->op != OP_PHI) &&
13150 tdominates(state, lr2->defs->def, lr1->defs->def))) {
13151 struct live_range *tmp;
13152 tmp = lr1;
13153 lr1 = lr2;
13154 lr2 = tmp;
13155 }
13156#if 0
13157 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
13158 fprintf(stderr, "lr1 post\n");
13159 }
13160 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
13161 fprintf(stderr, "lr1 pre\n");
13162 }
13163 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
13164 fprintf(stderr, "lr2 post\n");
13165 }
13166 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
13167 fprintf(stderr, "lr2 pre\n");
13168 }
13169#endif
Eric Biederman153ea352003-06-20 14:43:20 +000013170#if 0
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013171 fprintf(stderr, "coalesce color1(%p): %3d color2(%p) %3d\n",
13172 lr1->defs->def,
13173 lr1->color,
13174 lr2->defs->def,
13175 lr2->color);
13176#endif
13177
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013178 /* Append lr2 onto lr1 */
13179#warning "FIXME should this be a merge instead of a splice?"
Eric Biederman153ea352003-06-20 14:43:20 +000013180 /* This FIXME item applies to the correctness of live_range_end
13181 * and to the necessity of making multiple passes of coalesce_live_ranges.
13182 * A failure to find some coalesce opportunities in coaleace_live_ranges
13183 * does not impact the correct of the compiler just the efficiency with
13184 * which registers are allocated.
13185 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013186 head = lr1->defs;
13187 mid1 = lr1->defs->prev;
13188 mid2 = lr2->defs;
13189 end = lr2->defs->prev;
13190
13191 head->prev = end;
13192 end->next = head;
13193
13194 mid1->next = mid2;
13195 mid2->prev = mid1;
13196
13197 /* Fixup the live range in the added live range defs */
13198 lrd = head;
13199 do {
13200 lrd->lr = lr1;
13201 lrd = lrd->next;
13202 } while(lrd != head);
13203
13204 /* Mark lr2 as free. */
13205 lr2->defs = 0;
13206 lr2->color = REG_UNNEEDED;
13207 lr2->classes = 0;
13208
13209 if (!lr1->defs) {
13210 internal_error(state, 0, "lr1->defs == 0 ?");
13211 }
13212
13213 lr1->color = color;
13214 lr1->classes = classes;
13215
Eric Biederman153ea352003-06-20 14:43:20 +000013216 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
13217 transfer_live_edges(rstate, lr1, lr2);
13218
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013219 return lr1;
13220}
13221
13222static struct live_range_def *live_range_head(
13223 struct compile_state *state, struct live_range *lr,
13224 struct live_range_def *last)
13225{
13226 struct live_range_def *result;
13227 result = 0;
13228 if (last == 0) {
13229 result = lr->defs;
13230 }
13231 else if (!tdominates(state, lr->defs->def, last->next->def)) {
13232 result = last->next;
13233 }
13234 return result;
13235}
13236
13237static struct live_range_def *live_range_end(
13238 struct compile_state *state, struct live_range *lr,
13239 struct live_range_def *last)
13240{
13241 struct live_range_def *result;
13242 result = 0;
13243 if (last == 0) {
13244 result = lr->defs->prev;
13245 }
13246 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
13247 result = last->prev;
13248 }
13249 return result;
13250}
13251
13252
Eric Biedermanb138ac82003-04-22 18:44:01 +000013253static void initialize_live_ranges(
13254 struct compile_state *state, struct reg_state *rstate)
13255{
13256 struct triple *ins, *first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013257 size_t count, size;
13258 int i, j;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013259
Eric Biederman83b991a2003-10-11 06:20:25 +000013260 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013261 /* First count how many instructions I have.
Eric Biedermanb138ac82003-04-22 18:44:01 +000013262 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013263 count = count_triples(state);
13264 /* Potentially I need one live range definitions for each
Eric Biedermand1ea5392003-06-28 06:49:45 +000013265 * instruction.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013266 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000013267 rstate->defs = count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013268 /* Potentially I need one live range for each instruction
13269 * plus an extra for the dummy live range.
13270 */
13271 rstate->ranges = count + 1;
13272 size = sizeof(rstate->lrd[0]) * rstate->defs;
13273 rstate->lrd = xcmalloc(size, "live_range_def");
13274 size = sizeof(rstate->lr[0]) * rstate->ranges;
13275 rstate->lr = xcmalloc(size, "live_range");
13276
Eric Biedermanb138ac82003-04-22 18:44:01 +000013277 /* Setup the dummy live range */
13278 rstate->lr[0].classes = 0;
13279 rstate->lr[0].color = REG_UNSET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013280 rstate->lr[0].defs = 0;
13281 i = j = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013282 ins = first;
13283 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013284 /* If the triple is a variable give it a live range */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013285 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013286 struct reg_info info;
13287 /* Find the architecture specific color information */
13288 info = find_def_color(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013289 i++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013290 rstate->lr[i].defs = &rstate->lrd[j];
13291 rstate->lr[i].color = info.reg;
13292 rstate->lr[i].classes = info.regcm;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013293 rstate->lr[i].degree = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013294 rstate->lrd[j].lr = &rstate->lr[i];
13295 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013296 /* Otherwise give the triple the dummy live range. */
13297 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013298 rstate->lrd[j].lr = &rstate->lr[0];
Eric Biedermanb138ac82003-04-22 18:44:01 +000013299 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013300
13301 /* Initalize the live_range_def */
13302 rstate->lrd[j].next = &rstate->lrd[j];
13303 rstate->lrd[j].prev = &rstate->lrd[j];
13304 rstate->lrd[j].def = ins;
13305 rstate->lrd[j].orig_id = ins->id;
13306 ins->id = j;
13307
13308 j++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013309 ins = ins->next;
13310 } while(ins != first);
13311 rstate->ranges = i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013312
Eric Biedermanb138ac82003-04-22 18:44:01 +000013313 /* Make a second pass to handle achitecture specific register
13314 * constraints.
13315 */
13316 ins = first;
13317 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013318 int zlhs, zrhs, i, j;
13319 if (ins->id > rstate->defs) {
13320 internal_error(state, ins, "bad id");
13321 }
13322
13323 /* Walk through the template of ins and coalesce live ranges */
13324 zlhs = TRIPLE_LHS(ins->sizes);
13325 if ((zlhs == 0) && triple_is_def(state, ins)) {
13326 zlhs = 1;
13327 }
13328 zrhs = TRIPLE_RHS(ins->sizes);
Eric Biedermand1ea5392003-06-28 06:49:45 +000013329
13330#if DEBUG_COALESCING > 1
13331 fprintf(stderr, "mandatory coalesce: %p %d %d\n",
13332 ins, zlhs, zrhs);
Eric Biedermand1ea5392003-06-28 06:49:45 +000013333#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013334 for(i = 0; i < zlhs; i++) {
13335 struct reg_info linfo;
13336 struct live_range_def *lhs;
13337 linfo = arch_reg_lhs(state, ins, i);
13338 if (linfo.reg < MAX_REGISTERS) {
13339 continue;
13340 }
13341 if (triple_is_def(state, ins)) {
13342 lhs = &rstate->lrd[ins->id];
13343 } else {
13344 lhs = &rstate->lrd[LHS(ins, i)->id];
13345 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000013346#if DEBUG_COALESCING > 1
13347 fprintf(stderr, "coalesce lhs(%d): %p %d\n",
13348 i, lhs, linfo.reg);
13349
13350#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013351 for(j = 0; j < zrhs; j++) {
13352 struct reg_info rinfo;
13353 struct live_range_def *rhs;
13354 rinfo = arch_reg_rhs(state, ins, j);
13355 if (rinfo.reg < MAX_REGISTERS) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013356 continue;
13357 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000013358 rhs = &rstate->lrd[RHS(ins, j)->id];
13359#if DEBUG_COALESCING > 1
13360 fprintf(stderr, "coalesce rhs(%d): %p %d\n",
13361 j, rhs, rinfo.reg);
13362
13363#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013364 if (rinfo.reg == linfo.reg) {
13365 coalesce_ranges(state, rstate,
13366 lhs->lr, rhs->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013367 }
13368 }
13369 }
13370 ins = ins->next;
13371 } while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013372}
13373
Eric Biedermanf96a8102003-06-16 16:57:34 +000013374static void graph_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000013375 struct compile_state *state,
13376 struct reg_block *blocks, struct triple_reg_set *live,
13377 struct reg_block *rb, struct triple *ins, void *arg)
13378{
13379 struct reg_state *rstate = arg;
13380 struct live_range *def;
13381 struct triple_reg_set *entry;
13382
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013383 /* If the triple is not a definition
Eric Biedermanb138ac82003-04-22 18:44:01 +000013384 * we do not have a definition to add to
13385 * the interference graph.
13386 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013387 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000013388 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013389 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013390 def = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013391
13392 /* Create an edge between ins and everything that is
13393 * alive, unless the live_range cannot share
13394 * a physical register with ins.
13395 */
13396 for(entry = live; entry; entry = entry->next) {
13397 struct live_range *lr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013398 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
13399 internal_error(state, 0, "bad entry?");
13400 }
13401 lr = rstate->lrd[entry->member->id].lr;
13402 if (def == lr) {
13403 continue;
13404 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013405 if (!arch_regcm_intersect(def->classes, lr->classes)) {
13406 continue;
13407 }
13408 add_live_edge(rstate, def, lr);
13409 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000013410 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013411}
13412
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013413static struct live_range *get_verify_live_range(
13414 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
13415{
13416 struct live_range *lr;
13417 struct live_range_def *lrd;
13418 int ins_found;
13419 if ((ins->id < 0) || (ins->id > rstate->defs)) {
13420 internal_error(state, ins, "bad ins?");
13421 }
13422 lr = rstate->lrd[ins->id].lr;
13423 ins_found = 0;
13424 lrd = lr->defs;
13425 do {
13426 if (lrd->def == ins) {
13427 ins_found = 1;
13428 }
13429 lrd = lrd->next;
13430 } while(lrd != lr->defs);
13431 if (!ins_found) {
13432 internal_error(state, ins, "ins not in live range");
13433 }
13434 return lr;
13435}
13436
13437static void verify_graph_ins(
13438 struct compile_state *state,
13439 struct reg_block *blocks, struct triple_reg_set *live,
13440 struct reg_block *rb, struct triple *ins, void *arg)
13441{
13442 struct reg_state *rstate = arg;
13443 struct triple_reg_set *entry1, *entry2;
13444
13445
13446 /* Compare live against edges and make certain the code is working */
13447 for(entry1 = live; entry1; entry1 = entry1->next) {
13448 struct live_range *lr1;
13449 lr1 = get_verify_live_range(state, rstate, entry1->member);
13450 for(entry2 = live; entry2; entry2 = entry2->next) {
13451 struct live_range *lr2;
13452 struct live_range_edge *edge2;
13453 int lr1_found;
13454 int lr2_degree;
13455 if (entry2 == entry1) {
13456 continue;
13457 }
13458 lr2 = get_verify_live_range(state, rstate, entry2->member);
13459 if (lr1 == lr2) {
13460 internal_error(state, entry2->member,
13461 "live range with 2 values simultaneously alive");
13462 }
13463 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
13464 continue;
13465 }
13466 if (!interfere(rstate, lr1, lr2)) {
13467 internal_error(state, entry2->member,
13468 "edges don't interfere?");
13469 }
13470
13471 lr1_found = 0;
13472 lr2_degree = 0;
13473 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
13474 lr2_degree++;
13475 if (edge2->node == lr1) {
13476 lr1_found = 1;
13477 }
13478 }
13479 if (lr2_degree != lr2->degree) {
13480 internal_error(state, entry2->member,
13481 "computed degree: %d does not match reported degree: %d\n",
13482 lr2_degree, lr2->degree);
13483 }
13484 if (!lr1_found) {
13485 internal_error(state, entry2->member, "missing edge");
13486 }
13487 }
13488 }
13489 return;
13490}
13491
Eric Biedermanb138ac82003-04-22 18:44:01 +000013492
Eric Biedermanf96a8102003-06-16 16:57:34 +000013493static void print_interference_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000013494 struct compile_state *state,
13495 struct reg_block *blocks, struct triple_reg_set *live,
13496 struct reg_block *rb, struct triple *ins, void *arg)
13497{
13498 struct reg_state *rstate = arg;
13499 struct live_range *lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013500 unsigned id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013501
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013502 lr = rstate->lrd[ins->id].lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013503 id = ins->id;
13504 ins->id = rstate->lrd[id].orig_id;
13505 SET_REG(ins->id, lr->color);
Eric Biederman0babc1c2003-05-09 02:39:00 +000013506 display_triple(stdout, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013507 ins->id = id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013508
13509 if (lr->defs) {
13510 struct live_range_def *lrd;
13511 printf(" range:");
13512 lrd = lr->defs;
13513 do {
13514 printf(" %-10p", lrd->def);
13515 lrd = lrd->next;
13516 } while(lrd != lr->defs);
13517 printf("\n");
13518 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013519 if (live) {
13520 struct triple_reg_set *entry;
13521 printf(" live:");
13522 for(entry = live; entry; entry = entry->next) {
13523 printf(" %-10p", entry->member);
13524 }
13525 printf("\n");
13526 }
13527 if (lr->edges) {
13528 struct live_range_edge *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013529 printf(" edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013530 for(entry = lr->edges; entry; entry = entry->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013531 struct live_range_def *lrd;
13532 lrd = entry->node->defs;
13533 do {
13534 printf(" %-10p", lrd->def);
13535 lrd = lrd->next;
13536 } while(lrd != entry->node->defs);
13537 printf("|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013538 }
13539 printf("\n");
13540 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000013541 if (triple_is_branch(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013542 printf("\n");
13543 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000013544 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013545}
13546
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013547static int coalesce_live_ranges(
13548 struct compile_state *state, struct reg_state *rstate)
13549{
13550 /* At the point where a value is moved from one
13551 * register to another that value requires two
13552 * registers, thus increasing register pressure.
13553 * Live range coaleescing reduces the register
13554 * pressure by keeping a value in one register
13555 * longer.
13556 *
13557 * In the case of a phi function all paths leading
13558 * into it must be allocated to the same register
13559 * otherwise the phi function may not be removed.
13560 *
13561 * Forcing a value to stay in a single register
13562 * for an extended period of time does have
13563 * limitations when applied to non homogenous
13564 * register pool.
13565 *
13566 * The two cases I have identified are:
13567 * 1) Two forced register assignments may
13568 * collide.
13569 * 2) Registers may go unused because they
13570 * are only good for storing the value
13571 * and not manipulating it.
13572 *
13573 * Because of this I need to split live ranges,
13574 * even outside of the context of coalesced live
13575 * ranges. The need to split live ranges does
13576 * impose some constraints on live range coalescing.
13577 *
13578 * - Live ranges may not be coalesced across phi
13579 * functions. This creates a 2 headed live
13580 * range that cannot be sanely split.
13581 *
13582 * - phi functions (coalesced in initialize_live_ranges)
13583 * are handled as pre split live ranges so we will
13584 * never attempt to split them.
13585 */
13586 int coalesced;
13587 int i;
13588
13589 coalesced = 0;
13590 for(i = 0; i <= rstate->ranges; i++) {
13591 struct live_range *lr1;
13592 struct live_range_def *lrd1;
13593 lr1 = &rstate->lr[i];
13594 if (!lr1->defs) {
13595 continue;
13596 }
13597 lrd1 = live_range_end(state, lr1, 0);
13598 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
13599 struct triple_set *set;
13600 if (lrd1->def->op != OP_COPY) {
13601 continue;
13602 }
13603 /* Skip copies that are the result of a live range split. */
13604 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
13605 continue;
13606 }
13607 for(set = lrd1->def->use; set; set = set->next) {
13608 struct live_range_def *lrd2;
13609 struct live_range *lr2, *res;
13610
13611 lrd2 = &rstate->lrd[set->member->id];
13612
13613 /* Don't coalesce with instructions
13614 * that are the result of a live range
13615 * split.
13616 */
13617 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
13618 continue;
13619 }
13620 lr2 = rstate->lrd[set->member->id].lr;
13621 if (lr1 == lr2) {
13622 continue;
13623 }
13624 if ((lr1->color != lr2->color) &&
13625 (lr1->color != REG_UNSET) &&
13626 (lr2->color != REG_UNSET)) {
13627 continue;
13628 }
13629 if ((lr1->classes & lr2->classes) == 0) {
13630 continue;
13631 }
13632
13633 if (interfere(rstate, lr1, lr2)) {
13634 continue;
13635 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013636
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013637 res = coalesce_ranges(state, rstate, lr1, lr2);
13638 coalesced += 1;
13639 if (res != lr1) {
13640 goto next;
13641 }
13642 }
13643 }
13644 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000013645 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013646 }
13647 return coalesced;
13648}
13649
13650
Eric Biedermanf96a8102003-06-16 16:57:34 +000013651static void fix_coalesce_conflicts(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013652 struct reg_block *blocks, struct triple_reg_set *live,
13653 struct reg_block *rb, struct triple *ins, void *arg)
13654{
Eric Biedermand1ea5392003-06-28 06:49:45 +000013655 int *conflicts = arg;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013656 int zlhs, zrhs, i, j;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013657
13658 /* See if we have a mandatory coalesce operation between
13659 * a lhs and a rhs value. If so and the rhs value is also
13660 * alive then this triple needs to be pre copied. Otherwise
13661 * we would have two definitions in the same live range simultaneously
13662 * alive.
13663 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013664 zlhs = TRIPLE_LHS(ins->sizes);
13665 if ((zlhs == 0) && triple_is_def(state, ins)) {
13666 zlhs = 1;
13667 }
13668 zrhs = TRIPLE_RHS(ins->sizes);
Eric Biedermanf96a8102003-06-16 16:57:34 +000013669 for(i = 0; i < zlhs; i++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013670 struct reg_info linfo;
13671 linfo = arch_reg_lhs(state, ins, i);
13672 if (linfo.reg < MAX_REGISTERS) {
13673 continue;
13674 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000013675 for(j = 0; j < zrhs; j++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013676 struct reg_info rinfo;
13677 struct triple *rhs;
13678 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000013679 int found;
13680 found = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013681 rinfo = arch_reg_rhs(state, ins, j);
13682 if (rinfo.reg != linfo.reg) {
13683 continue;
13684 }
13685 rhs = RHS(ins, j);
Eric Biedermanf96a8102003-06-16 16:57:34 +000013686 for(set = live; set && !found; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013687 if (set->member == rhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000013688 found = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013689 }
13690 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000013691 if (found) {
13692 struct triple *copy;
13693 copy = pre_copy(state, ins, j);
13694 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermand1ea5392003-06-28 06:49:45 +000013695 (*conflicts)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000013696 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013697 }
13698 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000013699 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013700}
13701
Eric Biedermand1ea5392003-06-28 06:49:45 +000013702static int correct_coalesce_conflicts(
13703 struct compile_state *state, struct reg_block *blocks)
13704{
13705 int conflicts;
13706 conflicts = 0;
13707 walk_variable_lifetimes(state, blocks, fix_coalesce_conflicts, &conflicts);
13708 return conflicts;
13709}
13710
Eric Biedermanf96a8102003-06-16 16:57:34 +000013711static void replace_set_use(struct compile_state *state,
13712 struct triple_reg_set *head, struct triple *orig, struct triple *new)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013713{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013714 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000013715 for(set = head; set; set = set->next) {
13716 if (set->member == orig) {
13717 set->member = new;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013718 }
13719 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013720}
13721
Eric Biedermanf96a8102003-06-16 16:57:34 +000013722static void replace_block_use(struct compile_state *state,
13723 struct reg_block *blocks, struct triple *orig, struct triple *new)
13724{
13725 int i;
13726#warning "WISHLIST visit just those blocks that need it *"
13727 for(i = 1; i <= state->last_vertex; i++) {
13728 struct reg_block *rb;
13729 rb = &blocks[i];
13730 replace_set_use(state, rb->in, orig, new);
13731 replace_set_use(state, rb->out, orig, new);
13732 }
13733}
13734
13735static void color_instructions(struct compile_state *state)
13736{
13737 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000013738 first = state->first;
Eric Biedermanf96a8102003-06-16 16:57:34 +000013739 ins = first;
13740 do {
13741 if (triple_is_def(state, ins)) {
13742 struct reg_info info;
13743 info = find_lhs_color(state, ins, 0);
13744 if (info.reg >= MAX_REGISTERS) {
13745 info.reg = REG_UNSET;
13746 }
13747 SET_INFO(ins->id, info);
13748 }
13749 ins = ins->next;
13750 } while(ins != first);
13751}
13752
13753static struct reg_info read_lhs_color(
13754 struct compile_state *state, struct triple *ins, int index)
13755{
13756 struct reg_info info;
13757 if ((index == 0) && triple_is_def(state, ins)) {
13758 info.reg = ID_REG(ins->id);
13759 info.regcm = ID_REGCM(ins->id);
13760 }
13761 else if (index < TRIPLE_LHS(ins->sizes)) {
13762 info = read_lhs_color(state, LHS(ins, index), 0);
13763 }
13764 else {
13765 internal_error(state, ins, "Bad lhs %d", index);
13766 info.reg = REG_UNSET;
13767 info.regcm = 0;
13768 }
13769 return info;
13770}
13771
13772static struct triple *resolve_tangle(
13773 struct compile_state *state, struct triple *tangle)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013774{
13775 struct reg_info info, uinfo;
13776 struct triple_set *set, *next;
13777 struct triple *copy;
13778
Eric Biedermanf96a8102003-06-16 16:57:34 +000013779#warning "WISHLIST recalculate all affected instructions colors"
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013780 info = find_lhs_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013781 for(set = tangle->use; set; set = next) {
13782 struct triple *user;
13783 int i, zrhs;
13784 next = set->next;
13785 user = set->member;
13786 zrhs = TRIPLE_RHS(user->sizes);
13787 for(i = 0; i < zrhs; i++) {
13788 if (RHS(user, i) != tangle) {
13789 continue;
13790 }
13791 uinfo = find_rhs_post_color(state, user, i);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013792 if (uinfo.reg == info.reg) {
13793 copy = pre_copy(state, user, i);
13794 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000013795 SET_INFO(copy->id, uinfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013796 }
13797 }
13798 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000013799 copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013800 uinfo = find_lhs_pre_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013801 if (uinfo.reg == info.reg) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000013802 struct reg_info linfo;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013803 copy = post_copy(state, tangle);
13804 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000013805 linfo = find_lhs_color(state, copy, 0);
13806 SET_INFO(copy->id, linfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013807 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000013808 info = find_lhs_color(state, tangle, 0);
13809 SET_INFO(tangle->id, info);
13810
13811 return copy;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013812}
13813
13814
Eric Biedermanf96a8102003-06-16 16:57:34 +000013815static void fix_tangles(struct compile_state *state,
13816 struct reg_block *blocks, struct triple_reg_set *live,
13817 struct reg_block *rb, struct triple *ins, void *arg)
13818{
Eric Biederman153ea352003-06-20 14:43:20 +000013819 int *tangles = arg;
Eric Biedermanf96a8102003-06-16 16:57:34 +000013820 struct triple *tangle;
13821 do {
13822 char used[MAX_REGISTERS];
13823 struct triple_reg_set *set;
13824 tangle = 0;
13825
13826 /* Find out which registers have multiple uses at this point */
13827 memset(used, 0, sizeof(used));
13828 for(set = live; set; set = set->next) {
13829 struct reg_info info;
13830 info = read_lhs_color(state, set->member, 0);
13831 if (info.reg == REG_UNSET) {
13832 continue;
13833 }
13834 reg_inc_used(state, used, info.reg);
13835 }
13836
13837 /* Now find the least dominated definition of a register in
13838 * conflict I have seen so far.
13839 */
13840 for(set = live; set; set = set->next) {
13841 struct reg_info info;
13842 info = read_lhs_color(state, set->member, 0);
13843 if (used[info.reg] < 2) {
13844 continue;
13845 }
Eric Biederman153ea352003-06-20 14:43:20 +000013846 /* Changing copies that feed into phi functions
13847 * is incorrect.
13848 */
13849 if (set->member->use &&
13850 (set->member->use->member->op == OP_PHI)) {
13851 continue;
13852 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000013853 if (!tangle || tdominates(state, set->member, tangle)) {
13854 tangle = set->member;
13855 }
13856 }
13857 /* If I have found a tangle resolve it */
13858 if (tangle) {
13859 struct triple *post_copy;
Eric Biederman153ea352003-06-20 14:43:20 +000013860 (*tangles)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000013861 post_copy = resolve_tangle(state, tangle);
13862 if (post_copy) {
13863 replace_block_use(state, blocks, tangle, post_copy);
13864 }
13865 if (post_copy && (tangle != ins)) {
13866 replace_set_use(state, live, tangle, post_copy);
13867 }
13868 }
13869 } while(tangle);
13870 return;
13871}
13872
Eric Biederman153ea352003-06-20 14:43:20 +000013873static int correct_tangles(
Eric Biedermanf96a8102003-06-16 16:57:34 +000013874 struct compile_state *state, struct reg_block *blocks)
13875{
Eric Biederman153ea352003-06-20 14:43:20 +000013876 int tangles;
13877 tangles = 0;
Eric Biedermanf96a8102003-06-16 16:57:34 +000013878 color_instructions(state);
Eric Biederman153ea352003-06-20 14:43:20 +000013879 walk_variable_lifetimes(state, blocks, fix_tangles, &tangles);
13880 return tangles;
Eric Biedermanf96a8102003-06-16 16:57:34 +000013881}
13882
Eric Biedermand1ea5392003-06-28 06:49:45 +000013883
13884static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
13885static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
13886
13887struct triple *find_constrained_def(
13888 struct compile_state *state, struct live_range *range, struct triple *constrained)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013889{
Eric Biedermand1ea5392003-06-28 06:49:45 +000013890 struct live_range_def *lrd;
13891 lrd = range->defs;
13892 do {
Eric Biedermand3283ec2003-06-18 11:03:18 +000013893 struct reg_info info;
Eric Biedermand1ea5392003-06-28 06:49:45 +000013894 unsigned regcm;
13895 int is_constrained;
13896 regcm = arch_type_to_regcm(state, lrd->def->type);
13897 info = find_lhs_color(state, lrd->def, 0);
13898 regcm = arch_regcm_reg_normalize(state, regcm);
13899 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
13900 /* If the 2 register class masks are not equal the
13901 * the current register class is constrained.
Eric Biedermand3283ec2003-06-18 11:03:18 +000013902 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000013903 is_constrained = regcm != info.regcm;
Eric Biedermand3283ec2003-06-18 11:03:18 +000013904
Eric Biedermand1ea5392003-06-28 06:49:45 +000013905 /* Of the constrained live ranges deal with the
13906 * least dominated one first.
Eric Biedermand3283ec2003-06-18 11:03:18 +000013907 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000013908 if (is_constrained) {
Eric Biederman530b5192003-07-01 10:05:30 +000013909#if DEBUG_RANGE_CONFLICTS
13910 fprintf(stderr, "canidate: %p %-8s regcm: %x %x\n",
13911 lrd->def, tops(lrd->def->op), regcm, info.regcm);
13912#endif
Eric Biedermand1ea5392003-06-28 06:49:45 +000013913 if (!constrained ||
13914 tdominates(state, lrd->def, constrained))
13915 {
13916 constrained = lrd->def;
Eric Biedermand3283ec2003-06-18 11:03:18 +000013917 }
13918 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000013919 lrd = lrd->next;
13920 } while(lrd != range->defs);
13921 return constrained;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013922}
13923
Eric Biedermand1ea5392003-06-28 06:49:45 +000013924static int split_constrained_ranges(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013925 struct compile_state *state, struct reg_state *rstate,
Eric Biedermand1ea5392003-06-28 06:49:45 +000013926 struct live_range *range)
13927{
13928 /* Walk through the edges in conflict and our current live
13929 * range, and find definitions that are more severly constrained
13930 * than they type of data they contain require.
13931 *
13932 * Then pick one of those ranges and relax the constraints.
13933 */
13934 struct live_range_edge *edge;
13935 struct triple *constrained;
13936
13937 constrained = 0;
13938 for(edge = range->edges; edge; edge = edge->next) {
13939 constrained = find_constrained_def(state, edge->node, constrained);
13940 }
13941 if (!constrained) {
13942 constrained = find_constrained_def(state, range, constrained);
13943 }
13944#if DEBUG_RANGE_CONFLICTS
Eric Biederman530b5192003-07-01 10:05:30 +000013945 fprintf(stderr, "constrained: %p %-8s\n",
13946 constrained, tops(constrained->op));
Eric Biedermand1ea5392003-06-28 06:49:45 +000013947#endif
13948 if (constrained) {
13949 ids_from_rstate(state, rstate);
13950 cleanup_rstate(state, rstate);
13951 resolve_tangle(state, constrained);
13952 }
13953 return !!constrained;
13954}
13955
13956static int split_ranges(
13957 struct compile_state *state, struct reg_state *rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013958 char *used, struct live_range *range)
13959{
Eric Biedermand1ea5392003-06-28 06:49:45 +000013960 int split;
13961#if DEBUG_RANGE_CONFLICTS
Eric Biedermand3283ec2003-06-18 11:03:18 +000013962 fprintf(stderr, "split_ranges %d %s %p\n",
13963 rstate->passes, tops(range->defs->def->op), range->defs->def);
13964#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013965 if ((range->color == REG_UNNEEDED) ||
13966 (rstate->passes >= rstate->max_passes)) {
13967 return 0;
13968 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000013969 split = split_constrained_ranges(state, rstate, range);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013970
Eric Biedermand1ea5392003-06-28 06:49:45 +000013971 /* Ideally I would split the live range that will not be used
13972 * for the longest period of time in hopes that this will
13973 * (a) allow me to spill a register or
13974 * (b) allow me to place a value in another register.
13975 *
13976 * So far I don't have a test case for this, the resolving
13977 * of mandatory constraints has solved all of my
13978 * know issues. So I have choosen not to write any
13979 * code until I cat get a better feel for cases where
13980 * it would be useful to have.
13981 *
13982 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013983#warning "WISHLIST implement live range splitting..."
Eric Biedermand1ea5392003-06-28 06:49:45 +000013984 if ((DEBUG_RANGE_CONFLICTS > 1) &&
13985 (!split || (DEBUG_RANGE_CONFLICTS > 2))) {
13986 print_interference_blocks(state, rstate, stderr, 0);
Eric Biedermand3283ec2003-06-18 11:03:18 +000013987 print_dominators(state, stderr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013988 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000013989 return split;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013990}
13991
Eric Biedermanb138ac82003-04-22 18:44:01 +000013992#if DEBUG_COLOR_GRAPH > 1
13993#define cgdebug_printf(...) fprintf(stdout, __VA_ARGS__)
13994#define cgdebug_flush() fflush(stdout)
Eric Biedermand1ea5392003-06-28 06:49:45 +000013995#define cgdebug_loc(STATE, TRIPLE) loc(stdout, STATE, TRIPLE)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013996#elif DEBUG_COLOR_GRAPH == 1
13997#define cgdebug_printf(...) fprintf(stderr, __VA_ARGS__)
13998#define cgdebug_flush() fflush(stderr)
Eric Biedermand1ea5392003-06-28 06:49:45 +000013999#define cgdebug_loc(STATE, TRIPLE) loc(stderr, STATE, TRIPLE)
Eric Biedermanb138ac82003-04-22 18:44:01 +000014000#else
14001#define cgdebug_printf(...)
14002#define cgdebug_flush()
Eric Biedermand1ea5392003-06-28 06:49:45 +000014003#define cgdebug_loc(STATE, TRIPLE)
Eric Biedermanb138ac82003-04-22 18:44:01 +000014004#endif
14005
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014006
14007static int select_free_color(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000014008 struct reg_state *rstate, struct live_range *range)
14009{
14010 struct triple_set *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014011 struct live_range_def *lrd;
14012 struct live_range_def *phi;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014013 struct live_range_edge *edge;
14014 char used[MAX_REGISTERS];
14015 struct triple **expr;
14016
Eric Biedermanb138ac82003-04-22 18:44:01 +000014017 /* Instead of doing just the trivial color select here I try
14018 * a few extra things because a good color selection will help reduce
14019 * copies.
14020 */
14021
14022 /* Find the registers currently in use */
14023 memset(used, 0, sizeof(used));
14024 for(edge = range->edges; edge; edge = edge->next) {
14025 if (edge->node->color == REG_UNSET) {
14026 continue;
14027 }
14028 reg_fill_used(state, used, edge->node->color);
14029 }
14030#if DEBUG_COLOR_GRAPH > 1
14031 {
14032 int i;
14033 i = 0;
14034 for(edge = range->edges; edge; edge = edge->next) {
14035 i++;
14036 }
14037 cgdebug_printf("\n%s edges: %d @%s:%d.%d\n",
14038 tops(range->def->op), i,
14039 range->def->filename, range->def->line, range->def->col);
14040 for(i = 0; i < MAX_REGISTERS; i++) {
14041 if (used[i]) {
14042 cgdebug_printf("used: %s\n",
14043 arch_reg_str(i));
14044 }
14045 }
14046 }
14047#endif
14048
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014049 /* If a color is already assigned see if it will work */
14050 if (range->color != REG_UNSET) {
14051 struct live_range_def *lrd;
14052 if (!used[range->color]) {
14053 return 1;
14054 }
14055 for(edge = range->edges; edge; edge = edge->next) {
14056 if (edge->node->color != range->color) {
14057 continue;
14058 }
14059 warning(state, edge->node->defs->def, "edge: ");
14060 lrd = edge->node->defs;
14061 do {
14062 warning(state, lrd->def, " %p %s",
14063 lrd->def, tops(lrd->def->op));
14064 lrd = lrd->next;
14065 } while(lrd != edge->node->defs);
14066 }
14067 lrd = range->defs;
14068 warning(state, range->defs->def, "def: ");
14069 do {
14070 warning(state, lrd->def, " %p %s",
14071 lrd->def, tops(lrd->def->op));
14072 lrd = lrd->next;
14073 } while(lrd != range->defs);
14074 internal_error(state, range->defs->def,
14075 "live range with already used color %s",
14076 arch_reg_str(range->color));
14077 }
14078
Eric Biedermanb138ac82003-04-22 18:44:01 +000014079 /* If I feed into an expression reuse it's color.
14080 * This should help remove copies in the case of 2 register instructions
14081 * and phi functions.
14082 */
14083 phi = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014084 lrd = live_range_end(state, range, 0);
14085 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
14086 entry = lrd->def->use;
14087 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
14088 struct live_range_def *insd;
Eric Biederman530b5192003-07-01 10:05:30 +000014089 unsigned regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014090 insd = &rstate->lrd[entry->member->id];
14091 if (insd->lr->defs == 0) {
14092 continue;
14093 }
14094 if (!phi && (insd->def->op == OP_PHI) &&
14095 !interfere(rstate, range, insd->lr)) {
14096 phi = insd;
14097 }
Eric Biederman530b5192003-07-01 10:05:30 +000014098 if (insd->lr->color == REG_UNSET) {
14099 continue;
14100 }
14101 regcm = insd->lr->classes;
14102 if (((regcm & range->classes) == 0) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014103 (used[insd->lr->color])) {
14104 continue;
14105 }
14106 if (interfere(rstate, range, insd->lr)) {
14107 continue;
14108 }
14109 range->color = insd->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014110 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014111 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014112 /* If I feed into a phi function reuse it's color or the color
Eric Biedermanb138ac82003-04-22 18:44:01 +000014113 * of something else that feeds into the phi function.
14114 */
14115 if (phi) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014116 if (phi->lr->color != REG_UNSET) {
14117 if (used[phi->lr->color]) {
14118 range->color = phi->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014119 }
14120 }
14121 else {
14122 expr = triple_rhs(state, phi->def, 0);
14123 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
14124 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000014125 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014126 if (!*expr) {
14127 continue;
14128 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014129 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000014130 if (lr->color == REG_UNSET) {
14131 continue;
14132 }
14133 regcm = lr->classes;
14134 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000014135 (used[lr->color])) {
14136 continue;
14137 }
14138 if (interfere(rstate, range, lr)) {
14139 continue;
14140 }
14141 range->color = lr->color;
14142 }
14143 }
14144 }
14145 /* If I don't interfere with a rhs node reuse it's color */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014146 lrd = live_range_head(state, range, 0);
14147 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
14148 expr = triple_rhs(state, lrd->def, 0);
14149 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014150 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000014151 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014152 if (!*expr) {
14153 continue;
14154 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014155 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000014156 if (lr->color == REG_UNSET) {
14157 continue;
14158 }
14159 regcm = lr->classes;
14160 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000014161 (used[lr->color])) {
14162 continue;
14163 }
14164 if (interfere(rstate, range, lr)) {
14165 continue;
14166 }
14167 range->color = lr->color;
14168 break;
14169 }
14170 }
14171 /* If I have not opportunitically picked a useful color
14172 * pick the first color that is free.
14173 */
14174 if (range->color == REG_UNSET) {
14175 range->color =
14176 arch_select_free_register(state, used, range->classes);
14177 }
14178 if (range->color == REG_UNSET) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000014179 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014180 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014181 if (split_ranges(state, rstate, used, range)) {
14182 return 0;
14183 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014184 for(edge = range->edges; edge; edge = edge->next) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000014185 warning(state, edge->node->defs->def, "edge reg %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000014186 arch_reg_str(edge->node->color));
Eric Biedermand3283ec2003-06-18 11:03:18 +000014187 lrd = edge->node->defs;
14188 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000014189 warning(state, lrd->def, " %s %p",
14190 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000014191 lrd = lrd->next;
14192 } while(lrd != edge->node->defs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014193 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000014194 warning(state, range->defs->def, "range: ");
14195 lrd = range->defs;
14196 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000014197 warning(state, lrd->def, " %s %p",
14198 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000014199 lrd = lrd->next;
14200 } while(lrd != range->defs);
14201
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014202 warning(state, range->defs->def, "classes: %x",
Eric Biedermanb138ac82003-04-22 18:44:01 +000014203 range->classes);
14204 for(i = 0; i < MAX_REGISTERS; i++) {
14205 if (used[i]) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014206 warning(state, range->defs->def, "used: %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000014207 arch_reg_str(i));
14208 }
14209 }
14210#if DEBUG_COLOR_GRAPH < 2
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014211 error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000014212#else
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014213 internal_error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000014214#endif
14215 }
Eric Biederman530b5192003-07-01 10:05:30 +000014216 range->classes &= arch_reg_regcm(state, range->color);
14217 if ((range->color == REG_UNSET) || (range->classes == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014218 internal_error(state, range->defs->def, "select_free_color did not?");
14219 }
14220 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014221}
14222
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014223static int color_graph(struct compile_state *state, struct reg_state *rstate)
Eric Biedermanb138ac82003-04-22 18:44:01 +000014224{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014225 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014226 struct live_range_edge *edge;
14227 struct live_range *range;
14228 if (rstate->low) {
14229 cgdebug_printf("Lo: ");
14230 range = rstate->low;
14231 if (*range->group_prev != range) {
14232 internal_error(state, 0, "lo: *prev != range?");
14233 }
14234 *range->group_prev = range->group_next;
14235 if (range->group_next) {
14236 range->group_next->group_prev = range->group_prev;
14237 }
14238 if (&range->group_next == rstate->low_tail) {
14239 rstate->low_tail = range->group_prev;
14240 }
14241 if (rstate->low == range) {
14242 internal_error(state, 0, "low: next != prev?");
14243 }
14244 }
14245 else if (rstate->high) {
14246 cgdebug_printf("Hi: ");
14247 range = rstate->high;
14248 if (*range->group_prev != range) {
14249 internal_error(state, 0, "hi: *prev != range?");
14250 }
14251 *range->group_prev = range->group_next;
14252 if (range->group_next) {
14253 range->group_next->group_prev = range->group_prev;
14254 }
14255 if (&range->group_next == rstate->high_tail) {
14256 rstate->high_tail = range->group_prev;
14257 }
14258 if (rstate->high == range) {
14259 internal_error(state, 0, "high: next != prev?");
14260 }
14261 }
14262 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014263 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014264 }
14265 cgdebug_printf(" %d\n", range - rstate->lr);
14266 range->group_prev = 0;
14267 for(edge = range->edges; edge; edge = edge->next) {
14268 struct live_range *node;
14269 node = edge->node;
14270 /* Move nodes from the high to the low list */
14271 if (node->group_prev && (node->color == REG_UNSET) &&
14272 (node->degree == regc_max_size(state, node->classes))) {
14273 if (*node->group_prev != node) {
14274 internal_error(state, 0, "move: *prev != node?");
14275 }
14276 *node->group_prev = node->group_next;
14277 if (node->group_next) {
14278 node->group_next->group_prev = node->group_prev;
14279 }
14280 if (&node->group_next == rstate->high_tail) {
14281 rstate->high_tail = node->group_prev;
14282 }
14283 cgdebug_printf("Moving...%d to low\n", node - rstate->lr);
14284 node->group_prev = rstate->low_tail;
14285 node->group_next = 0;
14286 *rstate->low_tail = node;
14287 rstate->low_tail = &node->group_next;
14288 if (*node->group_prev != node) {
14289 internal_error(state, 0, "move2: *prev != node?");
14290 }
14291 }
14292 node->degree -= 1;
14293 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014294 colored = color_graph(state, rstate);
14295 if (colored) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000014296 cgdebug_printf("Coloring %d @", range - rstate->lr);
14297 cgdebug_loc(state, range->defs->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014298 cgdebug_flush();
14299 colored = select_free_color(state, rstate, range);
14300 cgdebug_printf(" %s\n", arch_reg_str(range->color));
Eric Biedermanb138ac82003-04-22 18:44:01 +000014301 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014302 return colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014303}
14304
Eric Biedermana96d6a92003-05-13 20:45:19 +000014305static void verify_colors(struct compile_state *state, struct reg_state *rstate)
14306{
14307 struct live_range *lr;
14308 struct live_range_edge *edge;
14309 struct triple *ins, *first;
14310 char used[MAX_REGISTERS];
Eric Biederman83b991a2003-10-11 06:20:25 +000014311 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000014312 ins = first;
14313 do {
14314 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014315 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000014316 internal_error(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014317 "triple without a live range def");
Eric Biedermana96d6a92003-05-13 20:45:19 +000014318 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014319 lr = rstate->lrd[ins->id].lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000014320 if (lr->color == REG_UNSET) {
14321 internal_error(state, ins,
14322 "triple without a color");
14323 }
14324 /* Find the registers used by the edges */
14325 memset(used, 0, sizeof(used));
14326 for(edge = lr->edges; edge; edge = edge->next) {
14327 if (edge->node->color == REG_UNSET) {
14328 internal_error(state, 0,
14329 "live range without a color");
14330 }
14331 reg_fill_used(state, used, edge->node->color);
14332 }
14333 if (used[lr->color]) {
14334 internal_error(state, ins,
14335 "triple with already used color");
14336 }
14337 }
14338 ins = ins->next;
14339 } while(ins != first);
14340}
14341
Eric Biedermanb138ac82003-04-22 18:44:01 +000014342static void color_triples(struct compile_state *state, struct reg_state *rstate)
14343{
14344 struct live_range *lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000014345 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000014346 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000014347 ins = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014348 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014349 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000014350 internal_error(state, ins,
Eric Biedermanb138ac82003-04-22 18:44:01 +000014351 "triple without a live range");
14352 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014353 lr = rstate->lrd[ins->id].lr;
14354 SET_REG(ins->id, lr->color);
Eric Biedermana96d6a92003-05-13 20:45:19 +000014355 ins = ins->next;
14356 } while (ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014357}
14358
Eric Biedermanb138ac82003-04-22 18:44:01 +000014359static struct live_range *merge_sort_lr(
14360 struct live_range *first, struct live_range *last)
14361{
14362 struct live_range *mid, *join, **join_tail, *pick;
14363 size_t size;
14364 size = (last - first) + 1;
14365 if (size >= 2) {
14366 mid = first + size/2;
14367 first = merge_sort_lr(first, mid -1);
14368 mid = merge_sort_lr(mid, last);
14369
14370 join = 0;
14371 join_tail = &join;
14372 /* merge the two lists */
14373 while(first && mid) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014374 if ((first->degree < mid->degree) ||
14375 ((first->degree == mid->degree) &&
14376 (first->length < mid->length))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014377 pick = first;
14378 first = first->group_next;
14379 if (first) {
14380 first->group_prev = 0;
14381 }
14382 }
14383 else {
14384 pick = mid;
14385 mid = mid->group_next;
14386 if (mid) {
14387 mid->group_prev = 0;
14388 }
14389 }
14390 pick->group_next = 0;
14391 pick->group_prev = join_tail;
14392 *join_tail = pick;
14393 join_tail = &pick->group_next;
14394 }
14395 /* Splice the remaining list */
14396 pick = (first)? first : mid;
14397 *join_tail = pick;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014398 if (pick) {
14399 pick->group_prev = join_tail;
14400 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014401 }
14402 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014403 if (!first->defs) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014404 first = 0;
14405 }
14406 join = first;
14407 }
14408 return join;
14409}
14410
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014411static void ids_from_rstate(struct compile_state *state,
14412 struct reg_state *rstate)
14413{
14414 struct triple *ins, *first;
14415 if (!rstate->defs) {
14416 return;
14417 }
14418 /* Display the graph if desired */
14419 if (state->debug & DEBUG_INTERFERENCE) {
14420 print_blocks(state, stdout);
14421 print_control_flow(state);
14422 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014423 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014424 ins = first;
14425 do {
14426 if (ins->id) {
14427 struct live_range_def *lrd;
14428 lrd = &rstate->lrd[ins->id];
14429 ins->id = lrd->orig_id;
14430 }
14431 ins = ins->next;
14432 } while(ins != first);
14433}
14434
14435static void cleanup_live_edges(struct reg_state *rstate)
14436{
14437 int i;
14438 /* Free the edges on each node */
14439 for(i = 1; i <= rstate->ranges; i++) {
14440 remove_live_edges(rstate, &rstate->lr[i]);
14441 }
14442}
14443
14444static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
14445{
14446 cleanup_live_edges(rstate);
14447 xfree(rstate->lrd);
14448 xfree(rstate->lr);
14449
14450 /* Free the variable lifetime information */
14451 if (rstate->blocks) {
14452 free_variable_lifetimes(state, rstate->blocks);
14453 }
14454 rstate->defs = 0;
14455 rstate->ranges = 0;
14456 rstate->lrd = 0;
14457 rstate->lr = 0;
14458 rstate->blocks = 0;
14459}
14460
Eric Biederman153ea352003-06-20 14:43:20 +000014461static void verify_consistency(struct compile_state *state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014462static void allocate_registers(struct compile_state *state)
14463{
14464 struct reg_state rstate;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014465 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014466
14467 /* Clear out the reg_state */
14468 memset(&rstate, 0, sizeof(rstate));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014469 rstate.max_passes = MAX_ALLOCATION_PASSES;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014470
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014471 do {
14472 struct live_range **point, **next;
Eric Biedermand1ea5392003-06-28 06:49:45 +000014473 int conflicts;
Eric Biederman153ea352003-06-20 14:43:20 +000014474 int tangles;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014475 int coalesced;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014476
Eric Biedermand1ea5392003-06-28 06:49:45 +000014477#if DEBUG_RANGE_CONFLICTS
Eric Biederman153ea352003-06-20 14:43:20 +000014478 fprintf(stderr, "pass: %d\n", rstate.passes);
14479#endif
14480
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014481 /* Restore ids */
14482 ids_from_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014483
Eric Biedermanf96a8102003-06-16 16:57:34 +000014484 /* Cleanup the temporary data structures */
14485 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014486
Eric Biedermanf96a8102003-06-16 16:57:34 +000014487 /* Compute the variable lifetimes */
14488 rstate.blocks = compute_variable_lifetimes(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014489
Eric Biedermanf96a8102003-06-16 16:57:34 +000014490 /* Fix invalid mandatory live range coalesce conflicts */
Eric Biedermand1ea5392003-06-28 06:49:45 +000014491 conflicts = correct_coalesce_conflicts(state, rstate.blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014492
Eric Biederman153ea352003-06-20 14:43:20 +000014493 /* Fix two simultaneous uses of the same register.
14494 * In a few pathlogical cases a partial untangle moves
14495 * the tangle to a part of the graph we won't revisit.
14496 * So we keep looping until we have no more tangle fixes
14497 * to apply.
14498 */
14499 do {
14500 tangles = correct_tangles(state, rstate.blocks);
14501 } while(tangles);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014502
14503 if (state->debug & DEBUG_INSERTED_COPIES) {
14504 printf("After resolve_tangles\n");
14505 print_blocks(state, stdout);
14506 print_control_flow(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014507 }
Eric Biederman153ea352003-06-20 14:43:20 +000014508 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014509
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014510 /* Allocate and initialize the live ranges */
14511 initialize_live_ranges(state, &rstate);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014512
Eric Biederman153ea352003-06-20 14:43:20 +000014513 /* Note current doing coalescing in a loop appears to
14514 * buys me nothing. The code is left this way in case
14515 * there is some value in it. Or if a future bugfix
14516 * yields some benefit.
14517 */
14518 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000014519#if DEBUG_COALESCING
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000014520 fprintf(stderr, "coalescing\n");
14521#endif
Eric Biederman153ea352003-06-20 14:43:20 +000014522 /* Remove any previous live edge calculations */
14523 cleanup_live_edges(&rstate);
14524
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014525 /* Compute the interference graph */
14526 walk_variable_lifetimes(
14527 state, rstate.blocks, graph_ins, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000014528
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014529 /* Display the interference graph if desired */
14530 if (state->debug & DEBUG_INTERFERENCE) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000014531 print_interference_blocks(state, &rstate, stdout, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014532 printf("\nlive variables by instruction\n");
14533 walk_variable_lifetimes(
14534 state, rstate.blocks,
14535 print_interference_ins, &rstate);
14536 }
14537
14538 coalesced = coalesce_live_ranges(state, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000014539
Eric Biedermand1ea5392003-06-28 06:49:45 +000014540#if DEBUG_COALESCING
Eric Biederman153ea352003-06-20 14:43:20 +000014541 fprintf(stderr, "coalesced: %d\n", coalesced);
14542#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014543 } while(coalesced);
Eric Biederman153ea352003-06-20 14:43:20 +000014544
Eric Biederman83b991a2003-10-11 06:20:25 +000014545#if DEBUG_CONSISTENCY > 1
14546# if 0
14547 fprintf(stderr, "verify_graph_ins...\n");
14548# endif
Eric Biederman153ea352003-06-20 14:43:20 +000014549 /* Verify the interference graph */
Eric Biederman83b991a2003-10-11 06:20:25 +000014550 walk_variable_lifetimes(
14551 state, rstate.blocks, verify_graph_ins, &rstate);
14552# if 0
14553 fprintf(stderr, "verify_graph_ins done\n");
14554#endif
14555#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014556
14557 /* Build the groups low and high. But with the nodes
14558 * first sorted by degree order.
14559 */
14560 rstate.low_tail = &rstate.low;
14561 rstate.high_tail = &rstate.high;
14562 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
14563 if (rstate.high) {
14564 rstate.high->group_prev = &rstate.high;
14565 }
14566 for(point = &rstate.high; *point; point = &(*point)->group_next)
14567 ;
14568 rstate.high_tail = point;
14569 /* Walk through the high list and move everything that needs
14570 * to be onto low.
14571 */
14572 for(point = &rstate.high; *point; point = next) {
14573 struct live_range *range;
14574 next = &(*point)->group_next;
14575 range = *point;
14576
14577 /* If it has a low degree or it already has a color
14578 * place the node in low.
14579 */
14580 if ((range->degree < regc_max_size(state, range->classes)) ||
14581 (range->color != REG_UNSET)) {
14582 cgdebug_printf("Lo: %5d degree %5d%s\n",
14583 range - rstate.lr, range->degree,
14584 (range->color != REG_UNSET) ? " (colored)": "");
14585 *range->group_prev = range->group_next;
14586 if (range->group_next) {
14587 range->group_next->group_prev = range->group_prev;
14588 }
14589 if (&range->group_next == rstate.high_tail) {
14590 rstate.high_tail = range->group_prev;
14591 }
14592 range->group_prev = rstate.low_tail;
14593 range->group_next = 0;
14594 *rstate.low_tail = range;
14595 rstate.low_tail = &range->group_next;
14596 next = point;
14597 }
14598 else {
14599 cgdebug_printf("hi: %5d degree %5d%s\n",
14600 range - rstate.lr, range->degree,
14601 (range->color != REG_UNSET) ? " (colored)": "");
14602 }
14603 }
14604 /* Color the live_ranges */
14605 colored = color_graph(state, &rstate);
14606 rstate.passes++;
14607 } while (!colored);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014608
Eric Biedermana96d6a92003-05-13 20:45:19 +000014609 /* Verify the graph was properly colored */
14610 verify_colors(state, &rstate);
14611
Eric Biedermanb138ac82003-04-22 18:44:01 +000014612 /* Move the colors from the graph to the triples */
14613 color_triples(state, &rstate);
14614
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014615 /* Cleanup the temporary data structures */
14616 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014617}
14618
14619/* Sparce Conditional Constant Propogation
14620 * =========================================
14621 */
14622struct ssa_edge;
14623struct flow_block;
14624struct lattice_node {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014625 unsigned old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014626 struct triple *def;
14627 struct ssa_edge *out;
14628 struct flow_block *fblock;
14629 struct triple *val;
14630 /* lattice high val && !is_const(val)
14631 * lattice const is_const(val)
14632 * lattice low val == 0
14633 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000014634};
14635struct ssa_edge {
14636 struct lattice_node *src;
14637 struct lattice_node *dst;
14638 struct ssa_edge *work_next;
14639 struct ssa_edge *work_prev;
14640 struct ssa_edge *out_next;
14641};
14642struct flow_edge {
14643 struct flow_block *src;
14644 struct flow_block *dst;
14645 struct flow_edge *work_next;
14646 struct flow_edge *work_prev;
14647 struct flow_edge *in_next;
14648 struct flow_edge *out_next;
14649 int executable;
14650};
14651struct flow_block {
14652 struct block *block;
14653 struct flow_edge *in;
14654 struct flow_edge *out;
14655 struct flow_edge left, right;
14656};
14657
14658struct scc_state {
Eric Biederman0babc1c2003-05-09 02:39:00 +000014659 int ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014660 struct lattice_node *lattice;
14661 struct ssa_edge *ssa_edges;
14662 struct flow_block *flow_blocks;
14663 struct flow_edge *flow_work_list;
14664 struct ssa_edge *ssa_work_list;
14665};
14666
14667
14668static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
14669 struct flow_edge *fedge)
14670{
Eric Biederman83b991a2003-10-11 06:20:25 +000014671 if ((fedge == scc->flow_work_list) ||
14672 (fedge->work_next != fedge) ||
14673 (fedge->work_prev != fedge)) {
14674 return;
14675 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014676 if (!scc->flow_work_list) {
14677 scc->flow_work_list = fedge;
14678 fedge->work_next = fedge->work_prev = fedge;
14679 }
14680 else {
14681 struct flow_edge *ftail;
14682 ftail = scc->flow_work_list->work_prev;
14683 fedge->work_next = ftail->work_next;
14684 fedge->work_prev = ftail;
14685 fedge->work_next->work_prev = fedge;
14686 fedge->work_prev->work_next = fedge;
14687 }
14688}
14689
14690static struct flow_edge *scc_next_fedge(
14691 struct compile_state *state, struct scc_state *scc)
14692{
14693 struct flow_edge *fedge;
14694 fedge = scc->flow_work_list;
14695 if (fedge) {
14696 fedge->work_next->work_prev = fedge->work_prev;
14697 fedge->work_prev->work_next = fedge->work_next;
14698 if (fedge->work_next != fedge) {
14699 scc->flow_work_list = fedge->work_next;
14700 } else {
14701 scc->flow_work_list = 0;
14702 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014703 fedge->work_next = fedge->work_prev = fedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014704 }
14705 return fedge;
14706}
14707
14708static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
14709 struct ssa_edge *sedge)
14710{
Eric Biederman83b991a2003-10-11 06:20:25 +000014711#if DEBUG_SCC > 1
14712 fprintf(stderr, "adding sedge: %5d (%4d -> %5d)\n",
14713 sedge - scc->ssa_edges,
14714 sedge->src->def->id,
14715 sedge->dst->def->id);
14716#endif
14717 if ((sedge == scc->ssa_work_list) ||
14718 (sedge->work_next != sedge) ||
14719 (sedge->work_prev != sedge)) {
14720#if DEBUG_SCC > 1
14721 fprintf(stderr, "dupped sedge: %5d\n",
14722 sedge - scc->ssa_edges);
14723#endif
14724 return;
14725 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014726 if (!scc->ssa_work_list) {
14727 scc->ssa_work_list = sedge;
14728 sedge->work_next = sedge->work_prev = sedge;
14729 }
14730 else {
14731 struct ssa_edge *stail;
14732 stail = scc->ssa_work_list->work_prev;
14733 sedge->work_next = stail->work_next;
14734 sedge->work_prev = stail;
14735 sedge->work_next->work_prev = sedge;
14736 sedge->work_prev->work_next = sedge;
14737 }
14738}
14739
14740static struct ssa_edge *scc_next_sedge(
14741 struct compile_state *state, struct scc_state *scc)
14742{
14743 struct ssa_edge *sedge;
14744 sedge = scc->ssa_work_list;
14745 if (sedge) {
14746 sedge->work_next->work_prev = sedge->work_prev;
14747 sedge->work_prev->work_next = sedge->work_next;
14748 if (sedge->work_next != sedge) {
14749 scc->ssa_work_list = sedge->work_next;
14750 } else {
14751 scc->ssa_work_list = 0;
14752 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014753 sedge->work_next = sedge->work_prev = sedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014754 }
14755 return sedge;
14756}
14757
14758static void initialize_scc_state(
14759 struct compile_state *state, struct scc_state *scc)
14760{
14761 int ins_count, ssa_edge_count;
14762 int ins_index, ssa_edge_index, fblock_index;
14763 struct triple *first, *ins;
14764 struct block *block;
14765 struct flow_block *fblock;
14766
14767 memset(scc, 0, sizeof(*scc));
14768
14769 /* Inialize pass zero find out how much memory we need */
Eric Biederman83b991a2003-10-11 06:20:25 +000014770 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014771 ins = first;
14772 ins_count = ssa_edge_count = 0;
14773 do {
14774 struct triple_set *edge;
14775 ins_count += 1;
14776 for(edge = ins->use; edge; edge = edge->next) {
14777 ssa_edge_count++;
14778 }
14779 ins = ins->next;
14780 } while(ins != first);
14781#if DEBUG_SCC
14782 fprintf(stderr, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
14783 ins_count, ssa_edge_count, state->last_vertex);
14784#endif
Eric Biederman0babc1c2003-05-09 02:39:00 +000014785 scc->ins_count = ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014786 scc->lattice =
14787 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
14788 scc->ssa_edges =
14789 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
14790 scc->flow_blocks =
14791 xcmalloc(sizeof(*scc->flow_blocks)*(state->last_vertex + 1),
14792 "flow_blocks");
14793
14794 /* Initialize pass one collect up the nodes */
14795 fblock = 0;
14796 block = 0;
14797 ins_index = ssa_edge_index = fblock_index = 0;
14798 ins = first;
14799 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014800 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
14801 block = ins->u.block;
14802 if (!block) {
14803 internal_error(state, ins, "label without block");
14804 }
14805 fblock_index += 1;
14806 block->vertex = fblock_index;
14807 fblock = &scc->flow_blocks[fblock_index];
14808 fblock->block = block;
14809 }
14810 {
14811 struct lattice_node *lnode;
14812 ins_index += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014813 lnode = &scc->lattice[ins_index];
14814 lnode->def = ins;
14815 lnode->out = 0;
14816 lnode->fblock = fblock;
14817 lnode->val = ins; /* LATTICE HIGH */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014818 lnode->old_id = ins->id;
14819 ins->id = ins_index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014820 }
14821 ins = ins->next;
14822 } while(ins != first);
14823 /* Initialize pass two collect up the edges */
14824 block = 0;
14825 fblock = 0;
14826 ins = first;
14827 do {
14828 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
14829 struct flow_edge *fedge, **ftail;
14830 struct block_set *bedge;
14831 block = ins->u.block;
14832 fblock = &scc->flow_blocks[block->vertex];
14833 fblock->in = 0;
14834 fblock->out = 0;
14835 ftail = &fblock->out;
14836 if (block->left) {
14837 fblock->left.dst = &scc->flow_blocks[block->left->vertex];
14838 if (fblock->left.dst->block != block->left) {
14839 internal_error(state, 0, "block mismatch");
14840 }
14841 fblock->left.out_next = 0;
14842 *ftail = &fblock->left;
14843 ftail = &fblock->left.out_next;
14844 }
14845 if (block->right) {
14846 fblock->right.dst = &scc->flow_blocks[block->right->vertex];
14847 if (fblock->right.dst->block != block->right) {
14848 internal_error(state, 0, "block mismatch");
14849 }
14850 fblock->right.out_next = 0;
14851 *ftail = &fblock->right;
14852 ftail = &fblock->right.out_next;
14853 }
14854 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
14855 fedge->src = fblock;
14856 fedge->work_next = fedge->work_prev = fedge;
14857 fedge->executable = 0;
14858 }
14859 ftail = &fblock->in;
14860 for(bedge = block->use; bedge; bedge = bedge->next) {
14861 struct block *src_block;
14862 struct flow_block *sfblock;
14863 struct flow_edge *sfedge;
14864 src_block = bedge->member;
14865 sfblock = &scc->flow_blocks[src_block->vertex];
14866 sfedge = 0;
14867 if (src_block->left == block) {
14868 sfedge = &sfblock->left;
14869 } else {
14870 sfedge = &sfblock->right;
14871 }
14872 *ftail = sfedge;
14873 ftail = &sfedge->in_next;
14874 sfedge->in_next = 0;
14875 }
14876 }
14877 {
14878 struct triple_set *edge;
14879 struct ssa_edge **stail;
14880 struct lattice_node *lnode;
14881 lnode = &scc->lattice[ins->id];
14882 lnode->out = 0;
14883 stail = &lnode->out;
14884 for(edge = ins->use; edge; edge = edge->next) {
14885 struct ssa_edge *sedge;
14886 ssa_edge_index += 1;
14887 sedge = &scc->ssa_edges[ssa_edge_index];
14888 *stail = sedge;
14889 stail = &sedge->out_next;
14890 sedge->src = lnode;
14891 sedge->dst = &scc->lattice[edge->member->id];
14892 sedge->work_next = sedge->work_prev = sedge;
14893 sedge->out_next = 0;
14894 }
14895 }
14896 ins = ins->next;
14897 } while(ins != first);
14898 /* Setup a dummy block 0 as a node above the start node */
14899 {
14900 struct flow_block *fblock, *dst;
14901 struct flow_edge *fedge;
14902 fblock = &scc->flow_blocks[0];
14903 fblock->block = 0;
14904 fblock->in = 0;
14905 fblock->out = &fblock->left;
14906 dst = &scc->flow_blocks[state->first_block->vertex];
14907 fedge = &fblock->left;
14908 fedge->src = fblock;
14909 fedge->dst = dst;
14910 fedge->work_next = fedge;
14911 fedge->work_prev = fedge;
14912 fedge->in_next = fedge->dst->in;
14913 fedge->out_next = 0;
14914 fedge->executable = 0;
14915 fedge->dst->in = fedge;
14916
14917 /* Initialize the work lists */
14918 scc->flow_work_list = 0;
14919 scc->ssa_work_list = 0;
14920 scc_add_fedge(state, scc, fedge);
14921 }
14922#if DEBUG_SCC
14923 fprintf(stderr, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
14924 ins_index, ssa_edge_index, fblock_index);
14925#endif
14926}
14927
14928
14929static void free_scc_state(
14930 struct compile_state *state, struct scc_state *scc)
14931{
14932 xfree(scc->flow_blocks);
14933 xfree(scc->ssa_edges);
14934 xfree(scc->lattice);
Eric Biederman0babc1c2003-05-09 02:39:00 +000014935
Eric Biedermanb138ac82003-04-22 18:44:01 +000014936}
14937
14938static struct lattice_node *triple_to_lattice(
14939 struct compile_state *state, struct scc_state *scc, struct triple *ins)
14940{
14941 if (ins->id <= 0) {
14942 internal_error(state, ins, "bad id");
14943 }
14944 return &scc->lattice[ins->id];
14945}
14946
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014947static struct triple *preserve_lval(
14948 struct compile_state *state, struct lattice_node *lnode)
14949{
14950 struct triple *old;
14951 /* Preserve the original value */
14952 if (lnode->val) {
14953 old = dup_triple(state, lnode->val);
14954 if (lnode->val != lnode->def) {
14955 xfree(lnode->val);
14956 }
14957 lnode->val = 0;
14958 } else {
14959 old = 0;
14960 }
14961 return old;
14962}
14963
14964static int lval_changed(struct compile_state *state,
14965 struct triple *old, struct lattice_node *lnode)
14966{
14967 int changed;
14968 /* See if the lattice value has changed */
14969 changed = 1;
14970 if (!old && !lnode->val) {
14971 changed = 0;
14972 }
14973 if (changed && lnode->val && !is_const(lnode->val)) {
14974 changed = 0;
14975 }
14976 if (changed &&
14977 lnode->val && old &&
14978 (memcmp(lnode->val->param, old->param,
14979 TRIPLE_SIZE(lnode->val->sizes) * sizeof(lnode->val->param[0])) == 0) &&
14980 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
14981 changed = 0;
14982 }
14983 if (old) {
14984 xfree(old);
14985 }
14986 return changed;
14987
14988}
14989
Eric Biedermanb138ac82003-04-22 18:44:01 +000014990static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
14991 struct lattice_node *lnode)
14992{
14993 struct lattice_node *tmp;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014994 struct triple **slot, *old;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014995 struct flow_edge *fedge;
Eric Biederman83b991a2003-10-11 06:20:25 +000014996 int changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014997 int index;
14998 if (lnode->def->op != OP_PHI) {
14999 internal_error(state, lnode->def, "not phi");
15000 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015001 /* Store the original value */
15002 old = preserve_lval(state, lnode);
15003
Eric Biedermanb138ac82003-04-22 18:44:01 +000015004 /* default to lattice high */
15005 lnode->val = lnode->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000015006 slot = &RHS(lnode->def, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015007 index = 0;
15008 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
Eric Biederman83b991a2003-10-11 06:20:25 +000015009#if DEBUG_SCC
15010 fprintf(stderr, "Examining edge: %d vertex: %d executable: %d\n",
15011 index,
15012 fedge->dst->block->vertex,
15013 fedge->executable
15014 );
15015#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015016 if (!fedge->executable) {
15017 continue;
15018 }
15019 if (!slot[index]) {
15020 internal_error(state, lnode->def, "no phi value");
15021 }
15022 tmp = triple_to_lattice(state, scc, slot[index]);
15023 /* meet(X, lattice low) = lattice low */
15024 if (!tmp->val) {
15025 lnode->val = 0;
15026 }
15027 /* meet(X, lattice high) = X */
15028 else if (!tmp->val) {
15029 lnode->val = lnode->val;
15030 }
15031 /* meet(lattice high, X) = X */
15032 else if (!is_const(lnode->val)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015033 lnode->val = dup_triple(state, tmp->val);
15034 lnode->val->type = lnode->def->type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015035 }
15036 /* meet(const, const) = const or lattice low */
15037 else if (!constants_equal(state, lnode->val, tmp->val)) {
15038 lnode->val = 0;
15039 }
15040 if (!lnode->val) {
15041 break;
15042 }
15043 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015044 changed = lval_changed(state, old, lnode);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015045#if DEBUG_SCC
Eric Biederman83b991a2003-10-11 06:20:25 +000015046 fprintf(stderr, "%p phi: %d -> %s %s\n",
15047 lnode->def,
Eric Biedermanb138ac82003-04-22 18:44:01 +000015048 lnode->def->id,
Eric Biederman83b991a2003-10-11 06:20:25 +000015049 ((!lnode->val)? "lo": is_const(lnode->val)? "const": "hi"),
15050 changed? "changed" : ""
15051 );
Eric Biedermanb138ac82003-04-22 18:44:01 +000015052#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015053 /* If the lattice value has changed update the work lists. */
Eric Biederman83b991a2003-10-11 06:20:25 +000015054 if (changed) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015055 struct ssa_edge *sedge;
15056 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
15057 scc_add_sedge(state, scc, sedge);
15058 }
15059 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015060}
15061
15062static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
15063 struct lattice_node *lnode)
15064{
15065 int changed;
Eric Biederman0babc1c2003-05-09 02:39:00 +000015066 struct triple *old, *scratch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015067 struct triple **dexpr, **vexpr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000015068 int count, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015069
15070 /* Store the original value */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015071 old = preserve_lval(state, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000015072
Eric Biedermanb138ac82003-04-22 18:44:01 +000015073 /* Reinitialize the value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000015074 lnode->val = scratch = dup_triple(state, lnode->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015075 scratch->id = lnode->old_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +000015076 scratch->next = scratch;
15077 scratch->prev = scratch;
15078 scratch->use = 0;
15079
15080 count = TRIPLE_SIZE(scratch->sizes);
15081 for(i = 0; i < count; i++) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000015082 dexpr = &lnode->def->param[i];
15083 vexpr = &scratch->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015084 *vexpr = *dexpr;
15085 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
15086 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
15087 *dexpr) {
15088 struct lattice_node *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +000015089 tmp = triple_to_lattice(state, scc, *dexpr);
15090 *vexpr = (tmp->val)? tmp->val : tmp->def;
15091 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015092 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015093 if (scratch->op == OP_BRANCH) {
15094 scratch->next = lnode->def->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015095 }
15096 /* Recompute the value */
15097#warning "FIXME see if simplify does anything bad"
15098 /* So far it looks like only the strength reduction
15099 * optimization are things I need to worry about.
15100 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000015101 simplify(state, scratch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015102 /* Cleanup my value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000015103 if (scratch->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015104 internal_error(state, lnode->def, "scratch used?");
15105 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015106 if ((scratch->prev != scratch) ||
15107 ((scratch->next != scratch) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000015108 ((lnode->def->op != OP_BRANCH) ||
Eric Biederman0babc1c2003-05-09 02:39:00 +000015109 (scratch->next != lnode->def->next)))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015110 internal_error(state, lnode->def, "scratch in list?");
15111 }
15112 /* undo any uses... */
Eric Biederman0babc1c2003-05-09 02:39:00 +000015113 count = TRIPLE_SIZE(scratch->sizes);
15114 for(i = 0; i < count; i++) {
15115 vexpr = &scratch->param[i];
15116 if (*vexpr) {
15117 unuse_triple(*vexpr, scratch);
15118 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015119 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015120 if (!is_const(scratch)) {
15121 for(i = 0; i < count; i++) {
15122 dexpr = &lnode->def->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015123 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
15124 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
15125 *dexpr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000015126 struct lattice_node *tmp;
15127 tmp = triple_to_lattice(state, scc, *dexpr);
15128 if (!tmp->val) {
15129 lnode->val = 0;
15130 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015131 }
15132 }
15133 }
15134 if (lnode->val &&
15135 (lnode->val->op == lnode->def->op) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000015136 (memcmp(lnode->val->param, lnode->def->param,
15137 count * sizeof(lnode->val->param[0])) == 0) &&
15138 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015139 lnode->val = lnode->def;
15140 }
15141 /* Find the cases that are always lattice lo */
15142 if (lnode->val &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000015143 triple_is_def(state, lnode->val) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000015144 !triple_is_pure(state, lnode->val, lnode->old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015145 lnode->val = 0;
15146 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015147 /* See if the lattice value has changed */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015148 changed = lval_changed(state, old, lnode);
Eric Biederman83b991a2003-10-11 06:20:25 +000015149 /* See if this value should not change */
15150 if (lnode->val &&
15151 (( !triple_is_def(state, lnode->def) &&
15152 !triple_is_cond_branch(state, lnode->def)) ||
15153 (lnode->def->op == OP_PIECE))) {
15154#warning "FIXME constant propogate through expressions with multiple left hand sides"
15155 if (changed) {
15156 internal_warning(state, lnode->def, "non def changes value?");
15157 }
15158 lnode->val = 0;
15159 }
15160 /* See if we need to free the scratch value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000015161 if (lnode->val != scratch) {
15162 xfree(scratch);
15163 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015164 return changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015165}
Eric Biederman0babc1c2003-05-09 02:39:00 +000015166
Eric Biedermanb138ac82003-04-22 18:44:01 +000015167static void scc_visit_branch(struct compile_state *state, struct scc_state *scc,
15168 struct lattice_node *lnode)
15169{
15170 struct lattice_node *cond;
15171#if DEBUG_SCC
15172 {
15173 struct flow_edge *fedge;
15174 fprintf(stderr, "branch: %d (",
15175 lnode->def->id);
15176
15177 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
15178 fprintf(stderr, " %d", fedge->dst->block->vertex);
15179 }
15180 fprintf(stderr, " )");
Eric Biederman0babc1c2003-05-09 02:39:00 +000015181 if (TRIPLE_RHS(lnode->def->sizes) > 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015182 fprintf(stderr, " <- %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015183 RHS(lnode->def, 0)->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015184 }
15185 fprintf(stderr, "\n");
15186 }
15187#endif
15188 if (lnode->def->op != OP_BRANCH) {
15189 internal_error(state, lnode->def, "not branch");
15190 }
15191 /* This only applies to conditional branches */
Eric Biederman0babc1c2003-05-09 02:39:00 +000015192 if (TRIPLE_RHS(lnode->def->sizes) == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015193 return;
15194 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015195 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000015196 if (cond->val && !is_const(cond->val)) {
15197#warning "FIXME do I need to do something here?"
15198 warning(state, cond->def, "condition not constant?");
15199 return;
15200 }
15201 if (cond->val == 0) {
15202 scc_add_fedge(state, scc, cond->fblock->out);
15203 scc_add_fedge(state, scc, cond->fblock->out->out_next);
15204 }
15205 else if (cond->val->u.cval) {
15206 scc_add_fedge(state, scc, cond->fblock->out->out_next);
15207
15208 } else {
15209 scc_add_fedge(state, scc, cond->fblock->out);
15210 }
15211
15212}
15213
15214static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
15215 struct lattice_node *lnode)
15216{
15217 int changed;
15218
15219 changed = compute_lnode_val(state, scc, lnode);
15220#if DEBUG_SCC
15221 {
15222 struct triple **expr;
15223 fprintf(stderr, "expr: %3d %10s (",
15224 lnode->def->id, tops(lnode->def->op));
15225 expr = triple_rhs(state, lnode->def, 0);
15226 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000015227 if (*expr) {
15228 fprintf(stderr, " %d", (*expr)->id);
15229 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015230 }
15231 fprintf(stderr, " ) -> %s\n",
15232 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
15233 }
15234#endif
15235 if (lnode->def->op == OP_BRANCH) {
15236 scc_visit_branch(state, scc, lnode);
15237
15238 }
15239 else if (changed) {
15240 struct ssa_edge *sedge;
15241 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
15242 scc_add_sedge(state, scc, sedge);
15243 }
15244 }
15245}
15246
15247static void scc_writeback_values(
15248 struct compile_state *state, struct scc_state *scc)
15249{
15250 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000015251 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015252 ins = first;
15253 do {
15254 struct lattice_node *lnode;
15255 lnode = triple_to_lattice(state, scc, ins);
15256#if DEBUG_SCC
Eric Biederman83b991a2003-10-11 06:20:25 +000015257 if (lnode->val &&
15258 !is_const(lnode->val) &&
15259 !triple_is_uncond_branch(state, lnode->val) &&
15260 (lnode->val->op != OP_NOOP))
15261 {
15262 struct flow_edge *fedge;
15263 int executable;
15264 executable = 0;
15265 for(fedge = lnode->fblock->in;
15266 !executable && fedge; fedge = fedge->in_next) {
15267 executable |= fedge->executable;
15268 }
15269 if (executable) {
15270 internal_warning(state, lnode->val,
15271 "lattice node %d %s->%s still high?",
15272 ins->id,
15273 tops(lnode->def->op),
15274 tops(lnode->val->op));
15275 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015276 }
15277#endif
Eric Biederman83b991a2003-10-11 06:20:25 +000015278 /* Restore id */
15279 ins->id = lnode->old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015280 if (lnode->val && (lnode->val != ins)) {
15281 /* See if it something I know how to write back */
15282 switch(lnode->val->op) {
15283 case OP_INTCONST:
15284 mkconst(state, ins, lnode->val->u.cval);
15285 break;
15286 case OP_ADDRCONST:
15287 mkaddr_const(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015288 MISC(lnode->val, 0), lnode->val->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015289 break;
15290 default:
15291 /* By default don't copy the changes,
15292 * recompute them in place instead.
15293 */
15294 simplify(state, ins);
15295 break;
15296 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015297 if (is_const(lnode->val) &&
15298 !constants_equal(state, lnode->val, ins)) {
15299 internal_error(state, 0, "constants not equal");
15300 }
15301 /* Free the lattice nodes */
15302 xfree(lnode->val);
15303 lnode->val = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015304 }
15305 ins = ins->next;
15306 } while(ins != first);
15307}
15308
15309static void scc_transform(struct compile_state *state)
15310{
15311 struct scc_state scc;
15312
15313 initialize_scc_state(state, &scc);
15314
15315 while(scc.flow_work_list || scc.ssa_work_list) {
15316 struct flow_edge *fedge;
15317 struct ssa_edge *sedge;
15318 struct flow_edge *fptr;
15319 while((fedge = scc_next_fedge(state, &scc))) {
15320 struct block *block;
15321 struct triple *ptr;
15322 struct flow_block *fblock;
Eric Biederman83b991a2003-10-11 06:20:25 +000015323 int reps;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015324 int done;
15325 if (fedge->executable) {
15326 continue;
15327 }
15328 if (!fedge->dst) {
15329 internal_error(state, 0, "fedge without dst");
15330 }
15331 if (!fedge->src) {
15332 internal_error(state, 0, "fedge without src");
15333 }
15334 fedge->executable = 1;
15335 fblock = fedge->dst;
15336 block = fblock->block;
Eric Biederman83b991a2003-10-11 06:20:25 +000015337 reps = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015338 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
15339 if (fptr->executable) {
Eric Biederman83b991a2003-10-11 06:20:25 +000015340 reps++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015341 }
15342 }
15343#if DEBUG_SCC
Eric Biederman83b991a2003-10-11 06:20:25 +000015344 fprintf(stderr, "vertex: %d reps: %d\n",
15345 block->vertex, reps);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015346
15347#endif
15348 done = 0;
15349 for(ptr = block->first; !done; ptr = ptr->next) {
15350 struct lattice_node *lnode;
15351 done = (ptr == block->last);
15352 lnode = &scc.lattice[ptr->id];
15353 if (ptr->op == OP_PHI) {
15354 scc_visit_phi(state, &scc, lnode);
15355 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015356 else if (reps == 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015357 scc_visit_expr(state, &scc, lnode);
15358 }
15359 }
15360 if (fblock->out && !fblock->out->out_next) {
15361 scc_add_fedge(state, &scc, fblock->out);
15362 }
15363 }
15364 while((sedge = scc_next_sedge(state, &scc))) {
15365 struct lattice_node *lnode;
15366 struct flow_block *fblock;
15367 lnode = sedge->dst;
15368 fblock = lnode->fblock;
15369#if DEBUG_SCC
15370 fprintf(stderr, "sedge: %5d (%5d -> %5d)\n",
15371 sedge - scc.ssa_edges,
15372 sedge->src->def->id,
15373 sedge->dst->def->id);
15374#endif
15375 if (lnode->def->op == OP_PHI) {
15376 scc_visit_phi(state, &scc, lnode);
15377 }
15378 else {
15379 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
15380 if (fptr->executable) {
15381 break;
15382 }
15383 }
15384 if (fptr) {
15385 scc_visit_expr(state, &scc, lnode);
15386 }
15387 }
15388 }
15389 }
15390
15391 scc_writeback_values(state, &scc);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015392 free_scc_state(state, &scc);
15393}
15394
15395
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015396static void transform_to_arch_instructions(struct compile_state *state)
15397{
15398 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000015399 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015400 ins = first;
15401 do {
15402 ins = transform_to_arch_instruction(state, ins);
15403 } while(ins != first);
15404}
Eric Biedermanb138ac82003-04-22 18:44:01 +000015405
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015406#if DEBUG_CONSISTENCY
15407static void verify_uses(struct compile_state *state)
15408{
15409 struct triple *first, *ins;
15410 struct triple_set *set;
Eric Biederman83b991a2003-10-11 06:20:25 +000015411 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015412 ins = first;
15413 do {
15414 struct triple **expr;
15415 expr = triple_rhs(state, ins, 0);
15416 for(; expr; expr = triple_rhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000015417 struct triple *rhs;
15418 rhs = *expr;
15419 for(set = rhs?rhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015420 if (set->member == ins) {
15421 break;
15422 }
15423 }
15424 if (!set) {
15425 internal_error(state, ins, "rhs not used");
15426 }
15427 }
15428 expr = triple_lhs(state, ins, 0);
15429 for(; expr; expr = triple_lhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000015430 struct triple *lhs;
15431 lhs = *expr;
15432 for(set = lhs?lhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015433 if (set->member == ins) {
15434 break;
15435 }
15436 }
15437 if (!set) {
15438 internal_error(state, ins, "lhs not used");
15439 }
15440 }
15441 ins = ins->next;
15442 } while(ins != first);
15443
15444}
Eric Biedermand1ea5392003-06-28 06:49:45 +000015445static void verify_blocks_present(struct compile_state *state)
15446{
15447 struct triple *first, *ins;
15448 if (!state->first_block) {
15449 return;
15450 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015451 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000015452 ins = first;
15453 do {
Eric Biederman530b5192003-07-01 10:05:30 +000015454 valid_ins(state, ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000015455 if (triple_stores_block(state, ins)) {
15456 if (!ins->u.block) {
15457 internal_error(state, ins,
15458 "%p not in a block?\n", ins);
15459 }
15460 }
15461 ins = ins->next;
15462 } while(ins != first);
15463
15464
15465}
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015466static void verify_blocks(struct compile_state *state)
15467{
15468 struct triple *ins;
15469 struct block *block;
Eric Biederman530b5192003-07-01 10:05:30 +000015470 int blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015471 block = state->first_block;
15472 if (!block) {
15473 return;
15474 }
Eric Biederman530b5192003-07-01 10:05:30 +000015475 blocks = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015476 do {
Eric Biederman530b5192003-07-01 10:05:30 +000015477 int users;
15478 struct block_set *user;
15479 blocks++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015480 for(ins = block->first; ins != block->last->next; ins = ins->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000015481 if (triple_stores_block(state, ins) && (ins->u.block != block)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015482 internal_error(state, ins, "inconsitent block specified");
15483 }
Eric Biederman530b5192003-07-01 10:05:30 +000015484 valid_ins(state, ins);
15485 }
15486 users = 0;
15487 for(user = block->use; user; user = user->next) {
15488 users++;
Eric Biederman83b991a2003-10-11 06:20:25 +000015489 if (!user->member->first) {
15490 internal_error(state, block->first, "user is empty");
15491 }
Eric Biederman530b5192003-07-01 10:05:30 +000015492 if ((block == state->last_block) &&
15493 (user->member == state->first_block)) {
15494 continue;
15495 }
15496 if ((user->member->left != block) &&
15497 (user->member->right != block)) {
15498 internal_error(state, user->member->first,
15499 "user does not use block");
15500 }
15501 }
15502 if (triple_is_branch(state, block->last) &&
15503 (block->right != block_of_triple(state, TARG(block->last, 0))))
15504 {
15505 internal_error(state, block->last, "block->right != TARG(0)");
15506 }
15507 if (!triple_is_uncond_branch(state, block->last) &&
15508 (block != state->last_block) &&
15509 (block->left != block_of_triple(state, block->last->next)))
15510 {
15511 internal_error(state, block->last, "block->left != block->last->next");
15512 }
15513 if (block->left) {
15514 for(user = block->left->use; user; user = user->next) {
15515 if (user->member == block) {
15516 break;
15517 }
15518 }
15519 if (!user || user->member != block) {
15520 internal_error(state, block->first,
15521 "block does not use left");
15522 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015523 if (!block->left->first) {
15524 internal_error(state, block->first, "left block is empty");
15525 }
Eric Biederman530b5192003-07-01 10:05:30 +000015526 }
15527 if (block->right) {
15528 for(user = block->right->use; user; user = user->next) {
15529 if (user->member == block) {
15530 break;
15531 }
15532 }
15533 if (!user || user->member != block) {
15534 internal_error(state, block->first,
15535 "block does not use right");
15536 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015537 if (!block->right->first) {
15538 internal_error(state, block->first, "right block is empty");
15539 }
Eric Biederman530b5192003-07-01 10:05:30 +000015540 }
15541 if (block->users != users) {
15542 internal_error(state, block->first,
15543 "computed users %d != stored users %d\n",
15544 users, block->users);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015545 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015546 for(user = block->ipdomfrontier; user; user = user->next) {
15547 if ((user->member->left != state->last_block) &&
15548 !triple_is_cond_branch(state, user->member->last)) {
15549 internal_error(state, user->member->last,
15550 "conditional branch missing");
15551 }
15552 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015553 if (!triple_stores_block(state, block->last->next)) {
15554 internal_error(state, block->last->next,
15555 "cannot find next block");
15556 }
15557 block = block->last->next->u.block;
15558 if (!block) {
15559 internal_error(state, block->last->next,
15560 "bad next block");
15561 }
15562 } while(block != state->first_block);
Eric Biederman530b5192003-07-01 10:05:30 +000015563 if (blocks != state->last_vertex) {
15564 internal_error(state, 0, "computed blocks != stored blocks %d\n",
15565 blocks, state->last_vertex);
15566 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015567}
15568
15569static void verify_domination(struct compile_state *state)
15570{
15571 struct triple *first, *ins;
15572 struct triple_set *set;
15573 if (!state->first_block) {
15574 return;
15575 }
15576
Eric Biederman83b991a2003-10-11 06:20:25 +000015577 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015578 ins = first;
15579 do {
15580 for(set = ins->use; set; set = set->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000015581 struct triple **slot;
15582 struct triple *use_point;
15583 int i, zrhs;
15584 use_point = 0;
15585 zrhs = TRIPLE_RHS(ins->sizes);
15586 slot = &RHS(set->member, 0);
15587 /* See if the use is on the right hand side */
15588 for(i = 0; i < zrhs; i++) {
15589 if (slot[i] == ins) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015590 break;
15591 }
15592 }
Eric Biederman66fe2222003-07-04 15:14:04 +000015593 if (i < zrhs) {
15594 use_point = set->member;
15595 if (set->member->op == OP_PHI) {
15596 struct block_set *bset;
15597 int edge;
15598 bset = set->member->u.block->use;
15599 for(edge = 0; bset && (edge < i); edge++) {
15600 bset = bset->next;
15601 }
15602 if (!bset) {
15603 internal_error(state, set->member,
15604 "no edge for phi rhs %d\n", i);
15605 }
15606 use_point = bset->member->last;
15607 }
15608 }
15609 if (use_point &&
15610 !tdominates(state, ins, use_point)) {
15611 internal_warning(state, ins,
15612 "ins does not dominate rhs use");
15613 internal_error(state, use_point,
15614 "non dominated rhs use point?");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015615 }
15616 }
15617 ins = ins->next;
15618 } while(ins != first);
15619}
15620
Eric Biederman83b991a2003-10-11 06:20:25 +000015621static void verify_rhs(struct compile_state *state)
15622{
15623 struct triple *first, *ins;
15624 first = state->first;
15625 ins = first;
15626 do {
15627 struct triple **slot;
15628 int zrhs, i;
15629 zrhs = TRIPLE_RHS(ins->sizes);
15630 slot = &RHS(ins, 0);
15631 for(i = 0; i < zrhs; i++) {
15632 if (slot[i] == 0) {
15633 internal_error(state, ins,
15634 "missing rhs %d on %s",
15635 i, tops(ins->op));
15636 }
15637 if ((ins->op != OP_PHI) && (slot[i] == ins)) {
15638 internal_error(state, ins,
15639 "ins == rhs[%d] on %s",
15640 i, tops(ins->op));
15641 }
15642 }
15643 ins = ins->next;
15644 } while(ins != first);
15645}
15646
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015647static void verify_piece(struct compile_state *state)
15648{
15649 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000015650 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015651 ins = first;
15652 do {
15653 struct triple *ptr;
15654 int lhs, i;
15655 lhs = TRIPLE_LHS(ins->sizes);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015656 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
15657 if (ptr != LHS(ins, i)) {
15658 internal_error(state, ins, "malformed lhs on %s",
15659 tops(ins->op));
15660 }
15661 if (ptr->op != OP_PIECE) {
15662 internal_error(state, ins, "bad lhs op %s at %d on %s",
15663 tops(ptr->op), i, tops(ins->op));
15664 }
15665 if (ptr->u.cval != i) {
15666 internal_error(state, ins, "bad u.cval of %d %d expected",
15667 ptr->u.cval, i);
15668 }
15669 }
15670 ins = ins->next;
15671 } while(ins != first);
15672}
Eric Biederman83b991a2003-10-11 06:20:25 +000015673
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015674static void verify_ins_colors(struct compile_state *state)
15675{
15676 struct triple *first, *ins;
15677
Eric Biederman83b991a2003-10-11 06:20:25 +000015678 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015679 ins = first;
15680 do {
15681 ins = ins->next;
15682 } while(ins != first);
15683}
15684static void verify_consistency(struct compile_state *state)
15685{
15686 verify_uses(state);
Eric Biedermand1ea5392003-06-28 06:49:45 +000015687 verify_blocks_present(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015688 verify_blocks(state);
15689 verify_domination(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000015690 verify_rhs(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015691 verify_piece(state);
15692 verify_ins_colors(state);
15693}
15694#else
Eric Biederman153ea352003-06-20 14:43:20 +000015695static void verify_consistency(struct compile_state *state) {}
Eric Biederman83b991a2003-10-11 06:20:25 +000015696#endif /* DEBUG_CONSISTENCY */
Eric Biedermanb138ac82003-04-22 18:44:01 +000015697
15698static void optimize(struct compile_state *state)
15699{
15700 if (state->debug & DEBUG_TRIPLES) {
15701 print_triples(state);
15702 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015703 /* Replace structures with simpler data types */
15704 flatten_structures(state);
15705 if (state->debug & DEBUG_TRIPLES) {
15706 print_triples(state);
15707 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015708 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015709 /* Analize the intermediate code */
Eric Biederman83b991a2003-10-11 06:20:25 +000015710 analyze_basic_blocks(state);
Eric Biedermand1ea5392003-06-28 06:49:45 +000015711
Eric Biederman530b5192003-07-01 10:05:30 +000015712 /* Transform the code to ssa form. */
15713 /*
15714 * The transformation to ssa form puts a phi function
15715 * on each of edge of a dominance frontier where that
15716 * phi function might be needed. At -O2 if we don't
15717 * eleminate the excess phi functions we can get an
15718 * exponential code size growth. So I kill the extra
15719 * phi functions early and I kill them often.
15720 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000015721 transform_to_ssa_form(state);
Eric Biederman530b5192003-07-01 10:05:30 +000015722
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015723 verify_consistency(state);
Eric Biederman05f26fc2003-06-11 21:55:00 +000015724 if (state->debug & DEBUG_CODE_ELIMINATION) {
15725 fprintf(stdout, "After transform_to_ssa_form\n");
15726 print_blocks(state, stdout);
15727 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015728 /* Remove dead code */
15729 eliminate_inefectual_code(state);
15730 rebuild_ssa_form(state);
15731 verify_consistency(state);
15732
Eric Biedermanb138ac82003-04-22 18:44:01 +000015733 /* Do strength reduction and simple constant optimizations */
15734 if (state->optimize >= 1) {
15735 simplify_all(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000015736 rebuild_ssa_form(state);
Eric Biederman530b5192003-07-01 10:05:30 +000015737 }
15738 if (state->debug & DEBUG_CODE_ELIMINATION) {
15739 fprintf(stdout, "After simplify_all\n");
15740 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015741 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015742 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015743 /* Propogate constants throughout the code */
15744 if (state->optimize >= 2) {
15745 scc_transform(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000015746 rebuild_ssa_form(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015747 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015748 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015749#warning "WISHLIST implement single use constants (least possible register pressure)"
15750#warning "WISHLIST implement induction variable elimination"
Eric Biedermanb138ac82003-04-22 18:44:01 +000015751 /* Select architecture instructions and an initial partial
15752 * coloring based on architecture constraints.
15753 */
15754 transform_to_arch_instructions(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015755 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015756 if (state->debug & DEBUG_ARCH_CODE) {
15757 printf("After transform_to_arch_instructions\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015758 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015759 print_control_flow(state);
15760 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015761 /* Remove dead code */
Eric Biedermanb138ac82003-04-22 18:44:01 +000015762 eliminate_inefectual_code(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000015763 rebuild_ssa_form(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015764 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015765 if (state->debug & DEBUG_CODE_ELIMINATION) {
15766 printf("After eliminate_inefectual_code\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015767 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015768 print_control_flow(state);
15769 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015770 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015771 /* Color all of the variables to see if they will fit in registers */
15772 insert_copies_to_phi(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015773 if (state->debug & DEBUG_INSERTED_COPIES) {
15774 printf("After insert_copies_to_phi\n");
15775 print_blocks(state, stdout);
15776 print_control_flow(state);
15777 }
15778 verify_consistency(state);
15779 insert_mandatory_copies(state);
15780 if (state->debug & DEBUG_INSERTED_COPIES) {
15781 printf("After insert_mandatory_copies\n");
15782 print_blocks(state, stdout);
15783 print_control_flow(state);
15784 }
15785 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015786 allocate_registers(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015787 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015788 if (state->debug & DEBUG_INTERMEDIATE_CODE) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015789 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015790 }
15791 if (state->debug & DEBUG_CONTROL_FLOW) {
15792 print_control_flow(state);
15793 }
15794 /* Remove the optimization information.
15795 * This is more to check for memory consistency than to free memory.
15796 */
15797 free_basic_blocks(state);
15798}
15799
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015800static void print_op_asm(struct compile_state *state,
15801 struct triple *ins, FILE *fp)
15802{
15803 struct asm_info *info;
15804 const char *ptr;
15805 unsigned lhs, rhs, i;
15806 info = ins->u.ainfo;
15807 lhs = TRIPLE_LHS(ins->sizes);
15808 rhs = TRIPLE_RHS(ins->sizes);
15809 /* Don't count the clobbers in lhs */
15810 for(i = 0; i < lhs; i++) {
15811 if (LHS(ins, i)->type == &void_type) {
15812 break;
15813 }
15814 }
15815 lhs = i;
Eric Biederman8d9c1232003-06-17 08:42:17 +000015816 fprintf(fp, "#ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015817 fputc('\t', fp);
15818 for(ptr = info->str; *ptr; ptr++) {
15819 char *next;
15820 unsigned long param;
15821 struct triple *piece;
15822 if (*ptr != '%') {
15823 fputc(*ptr, fp);
15824 continue;
15825 }
15826 ptr++;
15827 if (*ptr == '%') {
15828 fputc('%', fp);
15829 continue;
15830 }
15831 param = strtoul(ptr, &next, 10);
15832 if (ptr == next) {
15833 error(state, ins, "Invalid asm template");
15834 }
15835 if (param >= (lhs + rhs)) {
15836 error(state, ins, "Invalid param %%%u in asm template",
15837 param);
15838 }
15839 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
15840 fprintf(fp, "%s",
15841 arch_reg_str(ID_REG(piece->id)));
Eric Biederman8d9c1232003-06-17 08:42:17 +000015842 ptr = next -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015843 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000015844 fprintf(fp, "\n#NOT ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015845}
15846
15847
15848/* Only use the low x86 byte registers. This allows me
15849 * allocate the entire register when a byte register is used.
15850 */
15851#define X86_4_8BIT_GPRS 1
15852
Eric Biederman83b991a2003-10-11 06:20:25 +000015853/* x86 featrues */
15854#define X86_MMX_REGS (1<<0)
15855#define X86_XMM_REGS (1<<1)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015856
Eric Biedermanb138ac82003-04-22 18:44:01 +000015857/* The x86 register classes */
Eric Biederman530b5192003-07-01 10:05:30 +000015858#define REGC_FLAGS 0
15859#define REGC_GPR8 1
15860#define REGC_GPR16 2
15861#define REGC_GPR32 3
15862#define REGC_DIVIDEND64 4
15863#define REGC_DIVIDEND32 5
15864#define REGC_MMX 6
15865#define REGC_XMM 7
15866#define REGC_GPR32_8 8
15867#define REGC_GPR16_8 9
15868#define REGC_GPR8_LO 10
15869#define REGC_IMM32 11
15870#define REGC_IMM16 12
15871#define REGC_IMM8 13
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015872#define LAST_REGC REGC_IMM8
Eric Biedermanb138ac82003-04-22 18:44:01 +000015873#if LAST_REGC >= MAX_REGC
15874#error "MAX_REGC is to low"
15875#endif
15876
15877/* Register class masks */
Eric Biederman530b5192003-07-01 10:05:30 +000015878#define REGCM_FLAGS (1 << REGC_FLAGS)
15879#define REGCM_GPR8 (1 << REGC_GPR8)
15880#define REGCM_GPR16 (1 << REGC_GPR16)
15881#define REGCM_GPR32 (1 << REGC_GPR32)
15882#define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
15883#define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
15884#define REGCM_MMX (1 << REGC_MMX)
15885#define REGCM_XMM (1 << REGC_XMM)
15886#define REGCM_GPR32_8 (1 << REGC_GPR32_8)
15887#define REGCM_GPR16_8 (1 << REGC_GPR16_8)
15888#define REGCM_GPR8_LO (1 << REGC_GPR8_LO)
15889#define REGCM_IMM32 (1 << REGC_IMM32)
15890#define REGCM_IMM16 (1 << REGC_IMM16)
15891#define REGCM_IMM8 (1 << REGC_IMM8)
15892#define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015893
15894/* The x86 registers */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015895#define REG_EFLAGS 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000015896#define REGC_FLAGS_FIRST REG_EFLAGS
15897#define REGC_FLAGS_LAST REG_EFLAGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015898#define REG_AL 3
15899#define REG_BL 4
15900#define REG_CL 5
15901#define REG_DL 6
15902#define REG_AH 7
15903#define REG_BH 8
15904#define REG_CH 9
15905#define REG_DH 10
Eric Biederman530b5192003-07-01 10:05:30 +000015906#define REGC_GPR8_LO_FIRST REG_AL
15907#define REGC_GPR8_LO_LAST REG_DL
Eric Biedermanb138ac82003-04-22 18:44:01 +000015908#define REGC_GPR8_FIRST REG_AL
Eric Biedermanb138ac82003-04-22 18:44:01 +000015909#define REGC_GPR8_LAST REG_DH
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015910#define REG_AX 11
15911#define REG_BX 12
15912#define REG_CX 13
15913#define REG_DX 14
15914#define REG_SI 15
15915#define REG_DI 16
15916#define REG_BP 17
15917#define REG_SP 18
Eric Biedermanb138ac82003-04-22 18:44:01 +000015918#define REGC_GPR16_FIRST REG_AX
15919#define REGC_GPR16_LAST REG_SP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015920#define REG_EAX 19
15921#define REG_EBX 20
15922#define REG_ECX 21
15923#define REG_EDX 22
15924#define REG_ESI 23
15925#define REG_EDI 24
15926#define REG_EBP 25
15927#define REG_ESP 26
Eric Biedermanb138ac82003-04-22 18:44:01 +000015928#define REGC_GPR32_FIRST REG_EAX
15929#define REGC_GPR32_LAST REG_ESP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015930#define REG_EDXEAX 27
Eric Biederman530b5192003-07-01 10:05:30 +000015931#define REGC_DIVIDEND64_FIRST REG_EDXEAX
15932#define REGC_DIVIDEND64_LAST REG_EDXEAX
15933#define REG_DXAX 28
15934#define REGC_DIVIDEND32_FIRST REG_DXAX
15935#define REGC_DIVIDEND32_LAST REG_DXAX
15936#define REG_MMX0 29
15937#define REG_MMX1 30
15938#define REG_MMX2 31
15939#define REG_MMX3 32
15940#define REG_MMX4 33
15941#define REG_MMX5 34
15942#define REG_MMX6 35
15943#define REG_MMX7 36
Eric Biedermanb138ac82003-04-22 18:44:01 +000015944#define REGC_MMX_FIRST REG_MMX0
15945#define REGC_MMX_LAST REG_MMX7
Eric Biederman530b5192003-07-01 10:05:30 +000015946#define REG_XMM0 37
15947#define REG_XMM1 38
15948#define REG_XMM2 39
15949#define REG_XMM3 40
15950#define REG_XMM4 41
15951#define REG_XMM5 42
15952#define REG_XMM6 43
15953#define REG_XMM7 44
Eric Biedermanb138ac82003-04-22 18:44:01 +000015954#define REGC_XMM_FIRST REG_XMM0
15955#define REGC_XMM_LAST REG_XMM7
15956#warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
15957#define LAST_REG REG_XMM7
15958
15959#define REGC_GPR32_8_FIRST REG_EAX
15960#define REGC_GPR32_8_LAST REG_EDX
15961#define REGC_GPR16_8_FIRST REG_AX
15962#define REGC_GPR16_8_LAST REG_DX
15963
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015964#define REGC_IMM8_FIRST -1
15965#define REGC_IMM8_LAST -1
15966#define REGC_IMM16_FIRST -2
15967#define REGC_IMM16_LAST -1
15968#define REGC_IMM32_FIRST -4
15969#define REGC_IMM32_LAST -1
15970
Eric Biedermanb138ac82003-04-22 18:44:01 +000015971#if LAST_REG >= MAX_REGISTERS
15972#error "MAX_REGISTERS to low"
15973#endif
15974
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015975
15976static unsigned regc_size[LAST_REGC +1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000015977 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
15978 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
15979 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
15980 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
15981 [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
15982 [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
15983 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
15984 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
15985 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
15986 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
15987 [REGC_GPR8_LO] = REGC_GPR8_LO_LAST - REGC_GPR8_LO_FIRST + 1,
15988 [REGC_IMM32] = 0,
15989 [REGC_IMM16] = 0,
15990 [REGC_IMM8] = 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015991};
15992
15993static const struct {
15994 int first, last;
15995} regcm_bound[LAST_REGC + 1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000015996 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
15997 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
15998 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
15999 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
16000 [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
16001 [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
16002 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
16003 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
16004 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
16005 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
16006 [REGC_GPR8_LO] = { REGC_GPR8_LO_FIRST, REGC_GPR8_LO_LAST },
16007 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
16008 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
16009 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016010};
16011
Eric Biederman83b991a2003-10-11 06:20:25 +000016012static int arch_encode_feature(const char *feature, unsigned long *features)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016013{
16014 struct cpu {
16015 const char *name;
16016 int cpu;
16017 } cpus[] = {
Eric Biederman83b991a2003-10-11 06:20:25 +000016018 { "i386", 0 },
16019 { "p2", X86_MMX_REGS },
16020 { "p3", X86_MMX_REGS | X86_XMM_REGS },
16021 { "p4", X86_MMX_REGS | X86_XMM_REGS },
16022 { "k7", X86_MMX_REGS },
16023 { "k8", X86_MMX_REGS | X86_XMM_REGS },
16024 { "c3", X86_MMX_REGS },
16025 { "c3-2", X86_MMX_REGS | X86_XMM_REGS }, /* Nehemiah */
16026 { 0, 0 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016027 };
16028 struct cpu *ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000016029 int result = 0;
16030 if (strcmp(feature, "mmx") == 0) {
16031 *features |= X86_MMX_REGS;
16032 }
16033 else if (strcmp(feature, "sse") == 0) {
16034 *features |= X86_XMM_REGS;
16035 }
16036 else if (strncmp(feature, "cpu=", 4) == 0) {
16037 const char *cpu = feature + 4;
16038 for(ptr = cpus; ptr->name; ptr++) {
16039 if (strcmp(ptr->name, cpu) == 0) {
16040 break;
16041 }
16042 }
16043 if (ptr->name) {
16044 *features |= ptr->cpu;
16045 }
16046 else {
16047 result = -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016048 }
16049 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016050 else {
16051 result = -1;
16052 }
16053 return result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016054}
16055
Eric Biedermanb138ac82003-04-22 18:44:01 +000016056static unsigned arch_regc_size(struct compile_state *state, int class)
16057{
Eric Biedermanb138ac82003-04-22 18:44:01 +000016058 if ((class < 0) || (class > LAST_REGC)) {
16059 return 0;
16060 }
16061 return regc_size[class];
16062}
Eric Biedermand1ea5392003-06-28 06:49:45 +000016063
Eric Biedermanb138ac82003-04-22 18:44:01 +000016064static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
16065{
16066 /* See if two register classes may have overlapping registers */
Eric Biederman530b5192003-07-01 10:05:30 +000016067 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
16068 REGCM_GPR32_8 | REGCM_GPR32 |
16069 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016070
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016071 /* Special case for the immediates */
16072 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
16073 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
16074 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
16075 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
16076 return 0;
16077 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016078 return (regcm1 & regcm2) ||
16079 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
16080}
16081
16082static void arch_reg_equivs(
16083 struct compile_state *state, unsigned *equiv, int reg)
16084{
16085 if ((reg < 0) || (reg > LAST_REG)) {
16086 internal_error(state, 0, "invalid register");
16087 }
16088 *equiv++ = reg;
16089 switch(reg) {
16090 case REG_AL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016091#if X86_4_8BIT_GPRS
16092 *equiv++ = REG_AH;
16093#endif
16094 *equiv++ = REG_AX;
16095 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000016096 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016097 *equiv++ = REG_EDXEAX;
16098 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016099 case REG_AH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016100#if X86_4_8BIT_GPRS
16101 *equiv++ = REG_AL;
16102#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016103 *equiv++ = REG_AX;
16104 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000016105 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016106 *equiv++ = REG_EDXEAX;
16107 break;
16108 case REG_BL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016109#if X86_4_8BIT_GPRS
16110 *equiv++ = REG_BH;
16111#endif
16112 *equiv++ = REG_BX;
16113 *equiv++ = REG_EBX;
16114 break;
16115
Eric Biedermanb138ac82003-04-22 18:44:01 +000016116 case REG_BH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016117#if X86_4_8BIT_GPRS
16118 *equiv++ = REG_BL;
16119#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016120 *equiv++ = REG_BX;
16121 *equiv++ = REG_EBX;
16122 break;
16123 case REG_CL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016124#if X86_4_8BIT_GPRS
16125 *equiv++ = REG_CH;
16126#endif
16127 *equiv++ = REG_CX;
16128 *equiv++ = REG_ECX;
16129 break;
16130
Eric Biedermanb138ac82003-04-22 18:44:01 +000016131 case REG_CH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016132#if X86_4_8BIT_GPRS
16133 *equiv++ = REG_CL;
16134#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016135 *equiv++ = REG_CX;
16136 *equiv++ = REG_ECX;
16137 break;
16138 case REG_DL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016139#if X86_4_8BIT_GPRS
16140 *equiv++ = REG_DH;
16141#endif
16142 *equiv++ = REG_DX;
16143 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000016144 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016145 *equiv++ = REG_EDXEAX;
16146 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016147 case REG_DH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016148#if X86_4_8BIT_GPRS
16149 *equiv++ = REG_DL;
16150#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016151 *equiv++ = REG_DX;
16152 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000016153 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016154 *equiv++ = REG_EDXEAX;
16155 break;
16156 case REG_AX:
16157 *equiv++ = REG_AL;
16158 *equiv++ = REG_AH;
16159 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000016160 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016161 *equiv++ = REG_EDXEAX;
16162 break;
16163 case REG_BX:
16164 *equiv++ = REG_BL;
16165 *equiv++ = REG_BH;
16166 *equiv++ = REG_EBX;
16167 break;
16168 case REG_CX:
16169 *equiv++ = REG_CL;
16170 *equiv++ = REG_CH;
16171 *equiv++ = REG_ECX;
16172 break;
16173 case REG_DX:
16174 *equiv++ = REG_DL;
16175 *equiv++ = REG_DH;
16176 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000016177 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016178 *equiv++ = REG_EDXEAX;
16179 break;
16180 case REG_SI:
16181 *equiv++ = REG_ESI;
16182 break;
16183 case REG_DI:
16184 *equiv++ = REG_EDI;
16185 break;
16186 case REG_BP:
16187 *equiv++ = REG_EBP;
16188 break;
16189 case REG_SP:
16190 *equiv++ = REG_ESP;
16191 break;
16192 case REG_EAX:
16193 *equiv++ = REG_AL;
16194 *equiv++ = REG_AH;
16195 *equiv++ = REG_AX;
Eric Biederman530b5192003-07-01 10:05:30 +000016196 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016197 *equiv++ = REG_EDXEAX;
16198 break;
16199 case REG_EBX:
16200 *equiv++ = REG_BL;
16201 *equiv++ = REG_BH;
16202 *equiv++ = REG_BX;
16203 break;
16204 case REG_ECX:
16205 *equiv++ = REG_CL;
16206 *equiv++ = REG_CH;
16207 *equiv++ = REG_CX;
16208 break;
16209 case REG_EDX:
16210 *equiv++ = REG_DL;
16211 *equiv++ = REG_DH;
16212 *equiv++ = REG_DX;
Eric Biederman530b5192003-07-01 10:05:30 +000016213 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016214 *equiv++ = REG_EDXEAX;
16215 break;
16216 case REG_ESI:
16217 *equiv++ = REG_SI;
16218 break;
16219 case REG_EDI:
16220 *equiv++ = REG_DI;
16221 break;
16222 case REG_EBP:
16223 *equiv++ = REG_BP;
16224 break;
16225 case REG_ESP:
16226 *equiv++ = REG_SP;
16227 break;
Eric Biederman530b5192003-07-01 10:05:30 +000016228 case REG_DXAX:
16229 *equiv++ = REG_AL;
16230 *equiv++ = REG_AH;
16231 *equiv++ = REG_DL;
16232 *equiv++ = REG_DH;
16233 *equiv++ = REG_AX;
16234 *equiv++ = REG_DX;
16235 *equiv++ = REG_EAX;
16236 *equiv++ = REG_EDX;
16237 *equiv++ = REG_EDXEAX;
16238 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016239 case REG_EDXEAX:
16240 *equiv++ = REG_AL;
16241 *equiv++ = REG_AH;
16242 *equiv++ = REG_DL;
16243 *equiv++ = REG_DH;
16244 *equiv++ = REG_AX;
16245 *equiv++ = REG_DX;
16246 *equiv++ = REG_EAX;
16247 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000016248 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016249 break;
16250 }
16251 *equiv++ = REG_UNSET;
16252}
16253
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016254static unsigned arch_avail_mask(struct compile_state *state)
16255{
16256 unsigned avail_mask;
Eric Biederman530b5192003-07-01 10:05:30 +000016257 /* REGCM_GPR8 is not available */
16258 avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
16259 REGCM_GPR32 | REGCM_GPR32_8 |
16260 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016261 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
Eric Biederman83b991a2003-10-11 06:20:25 +000016262 if (state->features & X86_MMX_REGS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016263 avail_mask |= REGCM_MMX;
Eric Biederman83b991a2003-10-11 06:20:25 +000016264 }
16265 if (state->features & X86_XMM_REGS) {
16266 avail_mask |= REGCM_XMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016267 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016268 return avail_mask;
16269}
16270
16271static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
16272{
16273 unsigned mask, result;
16274 int class, class2;
16275 result = regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016276
16277 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
16278 if ((result & mask) == 0) {
16279 continue;
16280 }
16281 if (class > LAST_REGC) {
16282 result &= ~mask;
16283 }
16284 for(class2 = 0; class2 <= LAST_REGC; class2++) {
16285 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
16286 (regcm_bound[class2].last <= regcm_bound[class].last)) {
16287 result |= (1 << class2);
16288 }
16289 }
16290 }
Eric Biederman530b5192003-07-01 10:05:30 +000016291 result &= arch_avail_mask(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016292 return result;
16293}
Eric Biedermanb138ac82003-04-22 18:44:01 +000016294
Eric Biedermand1ea5392003-06-28 06:49:45 +000016295static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
16296{
16297 /* Like arch_regcm_normalize except immediate register classes are excluded */
16298 regcm = arch_regcm_normalize(state, regcm);
16299 /* Remove the immediate register classes */
16300 regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
16301 return regcm;
16302
16303}
16304
Eric Biedermanb138ac82003-04-22 18:44:01 +000016305static unsigned arch_reg_regcm(struct compile_state *state, int reg)
16306{
Eric Biedermanb138ac82003-04-22 18:44:01 +000016307 unsigned mask;
16308 int class;
16309 mask = 0;
16310 for(class = 0; class <= LAST_REGC; class++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016311 if ((reg >= regcm_bound[class].first) &&
16312 (reg <= regcm_bound[class].last)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016313 mask |= (1 << class);
16314 }
16315 }
16316 if (!mask) {
16317 internal_error(state, 0, "reg %d not in any class", reg);
16318 }
16319 return mask;
16320}
16321
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016322static struct reg_info arch_reg_constraint(
16323 struct compile_state *state, struct type *type, const char *constraint)
16324{
16325 static const struct {
16326 char class;
16327 unsigned int mask;
16328 unsigned int reg;
16329 } constraints[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000016330 { 'r', REGCM_GPR32, REG_UNSET },
16331 { 'g', REGCM_GPR32, REG_UNSET },
16332 { 'p', REGCM_GPR32, REG_UNSET },
16333 { 'q', REGCM_GPR8_LO, REG_UNSET },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016334 { 'Q', REGCM_GPR32_8, REG_UNSET },
Eric Biederman530b5192003-07-01 10:05:30 +000016335 { 'x', REGCM_XMM, REG_UNSET },
16336 { 'y', REGCM_MMX, REG_UNSET },
16337 { 'a', REGCM_GPR32, REG_EAX },
16338 { 'b', REGCM_GPR32, REG_EBX },
16339 { 'c', REGCM_GPR32, REG_ECX },
16340 { 'd', REGCM_GPR32, REG_EDX },
16341 { 'D', REGCM_GPR32, REG_EDI },
16342 { 'S', REGCM_GPR32, REG_ESI },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016343 { '\0', 0, REG_UNSET },
16344 };
16345 unsigned int regcm;
16346 unsigned int mask, reg;
16347 struct reg_info result;
16348 const char *ptr;
16349 regcm = arch_type_to_regcm(state, type);
16350 reg = REG_UNSET;
16351 mask = 0;
16352 for(ptr = constraint; *ptr; ptr++) {
16353 int i;
16354 if (*ptr == ' ') {
16355 continue;
16356 }
16357 for(i = 0; constraints[i].class != '\0'; i++) {
16358 if (constraints[i].class == *ptr) {
16359 break;
16360 }
16361 }
16362 if (constraints[i].class == '\0') {
16363 error(state, 0, "invalid register constraint ``%c''", *ptr);
16364 break;
16365 }
16366 if ((constraints[i].mask & regcm) == 0) {
16367 error(state, 0, "invalid register class %c specified",
16368 *ptr);
16369 }
16370 mask |= constraints[i].mask;
16371 if (constraints[i].reg != REG_UNSET) {
16372 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
16373 error(state, 0, "Only one register may be specified");
16374 }
16375 reg = constraints[i].reg;
16376 }
16377 }
16378 result.reg = reg;
16379 result.regcm = mask;
16380 return result;
16381}
16382
16383static struct reg_info arch_reg_clobber(
16384 struct compile_state *state, const char *clobber)
16385{
16386 struct reg_info result;
16387 if (strcmp(clobber, "memory") == 0) {
16388 result.reg = REG_UNSET;
16389 result.regcm = 0;
16390 }
16391 else if (strcmp(clobber, "%eax") == 0) {
16392 result.reg = REG_EAX;
16393 result.regcm = REGCM_GPR32;
16394 }
16395 else if (strcmp(clobber, "%ebx") == 0) {
16396 result.reg = REG_EBX;
16397 result.regcm = REGCM_GPR32;
16398 }
16399 else if (strcmp(clobber, "%ecx") == 0) {
16400 result.reg = REG_ECX;
16401 result.regcm = REGCM_GPR32;
16402 }
16403 else if (strcmp(clobber, "%edx") == 0) {
16404 result.reg = REG_EDX;
16405 result.regcm = REGCM_GPR32;
16406 }
16407 else if (strcmp(clobber, "%esi") == 0) {
16408 result.reg = REG_ESI;
16409 result.regcm = REGCM_GPR32;
16410 }
16411 else if (strcmp(clobber, "%edi") == 0) {
16412 result.reg = REG_EDI;
16413 result.regcm = REGCM_GPR32;
16414 }
16415 else if (strcmp(clobber, "%ebp") == 0) {
16416 result.reg = REG_EBP;
16417 result.regcm = REGCM_GPR32;
16418 }
16419 else if (strcmp(clobber, "%esp") == 0) {
16420 result.reg = REG_ESP;
16421 result.regcm = REGCM_GPR32;
16422 }
16423 else if (strcmp(clobber, "cc") == 0) {
16424 result.reg = REG_EFLAGS;
16425 result.regcm = REGCM_FLAGS;
16426 }
16427 else if ((strncmp(clobber, "xmm", 3) == 0) &&
16428 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
16429 result.reg = REG_XMM0 + octdigval(clobber[3]);
16430 result.regcm = REGCM_XMM;
16431 }
16432 else if ((strncmp(clobber, "mmx", 3) == 0) &&
16433 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
16434 result.reg = REG_MMX0 + octdigval(clobber[3]);
16435 result.regcm = REGCM_MMX;
16436 }
16437 else {
16438 error(state, 0, "Invalid register clobber");
16439 result.reg = REG_UNSET;
16440 result.regcm = 0;
16441 }
16442 return result;
16443}
16444
Eric Biedermanb138ac82003-04-22 18:44:01 +000016445static int do_select_reg(struct compile_state *state,
16446 char *used, int reg, unsigned classes)
16447{
16448 unsigned mask;
16449 if (used[reg]) {
16450 return REG_UNSET;
16451 }
16452 mask = arch_reg_regcm(state, reg);
16453 return (classes & mask) ? reg : REG_UNSET;
16454}
16455
16456static int arch_select_free_register(
16457 struct compile_state *state, char *used, int classes)
16458{
Eric Biedermand1ea5392003-06-28 06:49:45 +000016459 /* Live ranges with the most neighbors are colored first.
16460 *
16461 * Generally it does not matter which colors are given
16462 * as the register allocator attempts to color live ranges
16463 * in an order where you are guaranteed not to run out of colors.
16464 *
16465 * Occasionally the register allocator cannot find an order
16466 * of register selection that will find a free color. To
16467 * increase the odds the register allocator will work when
16468 * it guesses first give out registers from register classes
16469 * least likely to run out of registers.
16470 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000016471 */
16472 int i, reg;
16473 reg = REG_UNSET;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016474 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016475 reg = do_select_reg(state, used, i, classes);
16476 }
16477 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
16478 reg = do_select_reg(state, used, i, classes);
16479 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000016480 for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016481 reg = do_select_reg(state, used, i, classes);
16482 }
16483 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
16484 reg = do_select_reg(state, used, i, classes);
16485 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016486 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
16487 reg = do_select_reg(state, used, i, classes);
16488 }
Eric Biederman530b5192003-07-01 10:05:30 +000016489 for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
16490 reg = do_select_reg(state, used, i, classes);
16491 }
16492 for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
16493 reg = do_select_reg(state, used, i, classes);
16494 }
16495 for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016496 reg = do_select_reg(state, used, i, classes);
16497 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000016498 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
16499 reg = do_select_reg(state, used, i, classes);
16500 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016501 return reg;
16502}
16503
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016504
Eric Biedermanb138ac82003-04-22 18:44:01 +000016505static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
16506{
16507#warning "FIXME force types smaller (if legal) before I get here"
Eric Biedermanb138ac82003-04-22 18:44:01 +000016508 unsigned mask;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016509 mask = 0;
16510 switch(type->type & TYPE_MASK) {
16511 case TYPE_ARRAY:
16512 case TYPE_VOID:
16513 mask = 0;
16514 break;
16515 case TYPE_CHAR:
16516 case TYPE_UCHAR:
Eric Biederman530b5192003-07-01 10:05:30 +000016517 mask = REGCM_GPR8 | REGCM_GPR8_LO |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016518 REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000016519 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000016520 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016521 REGCM_MMX | REGCM_XMM |
16522 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016523 break;
16524 case TYPE_SHORT:
16525 case TYPE_USHORT:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016526 mask = REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000016527 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000016528 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016529 REGCM_MMX | REGCM_XMM |
16530 REGCM_IMM32 | REGCM_IMM16;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016531 break;
16532 case TYPE_INT:
16533 case TYPE_UINT:
16534 case TYPE_LONG:
16535 case TYPE_ULONG:
16536 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016537 mask = REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000016538 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
16539 REGCM_MMX | REGCM_XMM |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016540 REGCM_IMM32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016541 break;
16542 default:
16543 internal_error(state, 0, "no register class for type");
16544 break;
16545 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000016546 mask = arch_regcm_normalize(state, mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016547 return mask;
16548}
16549
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016550static int is_imm32(struct triple *imm)
16551{
16552 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
16553 (imm->op == OP_ADDRCONST);
16554
16555}
16556static int is_imm16(struct triple *imm)
16557{
16558 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
16559}
16560static int is_imm8(struct triple *imm)
16561{
16562 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
16563}
16564
16565static int get_imm32(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016566{
16567 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016568 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016569 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016570 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016571 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016572 if (!is_imm32(imm)) {
16573 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016574 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016575 unuse_triple(*expr, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016576 use_triple(imm, ins);
16577 *expr = imm;
16578 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016579}
16580
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016581static int get_imm8(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016582{
16583 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016584 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016585 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016586 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016587 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016588 if (!is_imm8(imm)) {
16589 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016590 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016591 unuse_triple(*expr, ins);
16592 use_triple(imm, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016593 *expr = imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016594 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016595}
16596
Eric Biederman530b5192003-07-01 10:05:30 +000016597#define TEMPLATE_NOP 0
16598#define TEMPLATE_INTCONST8 1
16599#define TEMPLATE_INTCONST32 2
16600#define TEMPLATE_COPY8_REG 3
16601#define TEMPLATE_COPY16_REG 4
16602#define TEMPLATE_COPY32_REG 5
16603#define TEMPLATE_COPY_IMM8 6
16604#define TEMPLATE_COPY_IMM16 7
16605#define TEMPLATE_COPY_IMM32 8
16606#define TEMPLATE_PHI8 9
16607#define TEMPLATE_PHI16 10
16608#define TEMPLATE_PHI32 11
16609#define TEMPLATE_STORE8 12
16610#define TEMPLATE_STORE16 13
16611#define TEMPLATE_STORE32 14
16612#define TEMPLATE_LOAD8 15
16613#define TEMPLATE_LOAD16 16
16614#define TEMPLATE_LOAD32 17
16615#define TEMPLATE_BINARY8_REG 18
16616#define TEMPLATE_BINARY16_REG 19
16617#define TEMPLATE_BINARY32_REG 20
16618#define TEMPLATE_BINARY8_IMM 21
16619#define TEMPLATE_BINARY16_IMM 22
16620#define TEMPLATE_BINARY32_IMM 23
16621#define TEMPLATE_SL8_CL 24
16622#define TEMPLATE_SL16_CL 25
16623#define TEMPLATE_SL32_CL 26
16624#define TEMPLATE_SL8_IMM 27
16625#define TEMPLATE_SL16_IMM 28
16626#define TEMPLATE_SL32_IMM 29
16627#define TEMPLATE_UNARY8 30
16628#define TEMPLATE_UNARY16 31
16629#define TEMPLATE_UNARY32 32
16630#define TEMPLATE_CMP8_REG 33
16631#define TEMPLATE_CMP16_REG 34
16632#define TEMPLATE_CMP32_REG 35
16633#define TEMPLATE_CMP8_IMM 36
16634#define TEMPLATE_CMP16_IMM 37
16635#define TEMPLATE_CMP32_IMM 38
16636#define TEMPLATE_TEST8 39
16637#define TEMPLATE_TEST16 40
16638#define TEMPLATE_TEST32 41
16639#define TEMPLATE_SET 42
16640#define TEMPLATE_JMP 43
16641#define TEMPLATE_INB_DX 44
16642#define TEMPLATE_INB_IMM 45
16643#define TEMPLATE_INW_DX 46
16644#define TEMPLATE_INW_IMM 47
16645#define TEMPLATE_INL_DX 48
16646#define TEMPLATE_INL_IMM 49
16647#define TEMPLATE_OUTB_DX 50
16648#define TEMPLATE_OUTB_IMM 51
16649#define TEMPLATE_OUTW_DX 52
16650#define TEMPLATE_OUTW_IMM 53
16651#define TEMPLATE_OUTL_DX 54
16652#define TEMPLATE_OUTL_IMM 55
16653#define TEMPLATE_BSF 56
16654#define TEMPLATE_RDMSR 57
16655#define TEMPLATE_WRMSR 58
16656#define TEMPLATE_UMUL8 59
16657#define TEMPLATE_UMUL16 60
16658#define TEMPLATE_UMUL32 61
16659#define TEMPLATE_DIV8 62
16660#define TEMPLATE_DIV16 63
16661#define TEMPLATE_DIV32 64
16662#define LAST_TEMPLATE TEMPLATE_DIV32
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016663#if LAST_TEMPLATE >= MAX_TEMPLATES
16664#error "MAX_TEMPLATES to low"
16665#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016666
Eric Biederman530b5192003-07-01 10:05:30 +000016667#define COPY8_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO | REGCM_MMX | REGCM_XMM)
16668#define COPY16_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)
16669#define COPY32_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
Eric Biedermand1ea5392003-06-28 06:49:45 +000016670
Eric Biedermanb138ac82003-04-22 18:44:01 +000016671
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016672static struct ins_template templates[] = {
16673 [TEMPLATE_NOP] = {},
16674 [TEMPLATE_INTCONST8] = {
16675 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16676 },
16677 [TEMPLATE_INTCONST32] = {
16678 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
16679 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000016680 [TEMPLATE_COPY8_REG] = {
16681 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
16682 .rhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016683 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000016684 [TEMPLATE_COPY16_REG] = {
16685 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
16686 .rhs = { [0] = { REG_UNSET, COPY16_REGCM } },
16687 },
16688 [TEMPLATE_COPY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016689 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000016690 .rhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016691 },
16692 [TEMPLATE_COPY_IMM8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000016693 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016694 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16695 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000016696 [TEMPLATE_COPY_IMM16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000016697 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000016698 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
16699 },
16700 [TEMPLATE_COPY_IMM32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000016701 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000016702 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
16703 },
16704 [TEMPLATE_PHI8] = {
16705 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016706 .rhs = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016707 [ 0] = { REG_VIRT0, COPY8_REGCM },
16708 [ 1] = { REG_VIRT0, COPY8_REGCM },
16709 [ 2] = { REG_VIRT0, COPY8_REGCM },
16710 [ 3] = { REG_VIRT0, COPY8_REGCM },
16711 [ 4] = { REG_VIRT0, COPY8_REGCM },
16712 [ 5] = { REG_VIRT0, COPY8_REGCM },
16713 [ 6] = { REG_VIRT0, COPY8_REGCM },
16714 [ 7] = { REG_VIRT0, COPY8_REGCM },
16715 [ 8] = { REG_VIRT0, COPY8_REGCM },
16716 [ 9] = { REG_VIRT0, COPY8_REGCM },
16717 [10] = { REG_VIRT0, COPY8_REGCM },
16718 [11] = { REG_VIRT0, COPY8_REGCM },
16719 [12] = { REG_VIRT0, COPY8_REGCM },
16720 [13] = { REG_VIRT0, COPY8_REGCM },
16721 [14] = { REG_VIRT0, COPY8_REGCM },
16722 [15] = { REG_VIRT0, COPY8_REGCM },
16723 }, },
16724 [TEMPLATE_PHI16] = {
16725 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
16726 .rhs = {
16727 [ 0] = { REG_VIRT0, COPY16_REGCM },
16728 [ 1] = { REG_VIRT0, COPY16_REGCM },
16729 [ 2] = { REG_VIRT0, COPY16_REGCM },
16730 [ 3] = { REG_VIRT0, COPY16_REGCM },
16731 [ 4] = { REG_VIRT0, COPY16_REGCM },
16732 [ 5] = { REG_VIRT0, COPY16_REGCM },
16733 [ 6] = { REG_VIRT0, COPY16_REGCM },
16734 [ 7] = { REG_VIRT0, COPY16_REGCM },
16735 [ 8] = { REG_VIRT0, COPY16_REGCM },
16736 [ 9] = { REG_VIRT0, COPY16_REGCM },
16737 [10] = { REG_VIRT0, COPY16_REGCM },
16738 [11] = { REG_VIRT0, COPY16_REGCM },
16739 [12] = { REG_VIRT0, COPY16_REGCM },
16740 [13] = { REG_VIRT0, COPY16_REGCM },
16741 [14] = { REG_VIRT0, COPY16_REGCM },
16742 [15] = { REG_VIRT0, COPY16_REGCM },
16743 }, },
16744 [TEMPLATE_PHI32] = {
16745 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
16746 .rhs = {
16747 [ 0] = { REG_VIRT0, COPY32_REGCM },
16748 [ 1] = { REG_VIRT0, COPY32_REGCM },
16749 [ 2] = { REG_VIRT0, COPY32_REGCM },
16750 [ 3] = { REG_VIRT0, COPY32_REGCM },
16751 [ 4] = { REG_VIRT0, COPY32_REGCM },
16752 [ 5] = { REG_VIRT0, COPY32_REGCM },
16753 [ 6] = { REG_VIRT0, COPY32_REGCM },
16754 [ 7] = { REG_VIRT0, COPY32_REGCM },
16755 [ 8] = { REG_VIRT0, COPY32_REGCM },
16756 [ 9] = { REG_VIRT0, COPY32_REGCM },
16757 [10] = { REG_VIRT0, COPY32_REGCM },
16758 [11] = { REG_VIRT0, COPY32_REGCM },
16759 [12] = { REG_VIRT0, COPY32_REGCM },
16760 [13] = { REG_VIRT0, COPY32_REGCM },
16761 [14] = { REG_VIRT0, COPY32_REGCM },
16762 [15] = { REG_VIRT0, COPY32_REGCM },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016763 }, },
16764 [TEMPLATE_STORE8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000016765 .rhs = {
16766 [0] = { REG_UNSET, REGCM_GPR32 },
16767 [1] = { REG_UNSET, REGCM_GPR8_LO },
16768 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016769 },
16770 [TEMPLATE_STORE16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000016771 .rhs = {
16772 [0] = { REG_UNSET, REGCM_GPR32 },
16773 [1] = { REG_UNSET, REGCM_GPR16 },
16774 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016775 },
16776 [TEMPLATE_STORE32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000016777 .rhs = {
16778 [0] = { REG_UNSET, REGCM_GPR32 },
16779 [1] = { REG_UNSET, REGCM_GPR32 },
16780 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016781 },
16782 [TEMPLATE_LOAD8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000016783 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016784 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16785 },
16786 [TEMPLATE_LOAD16] = {
16787 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
16788 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16789 },
16790 [TEMPLATE_LOAD32] = {
16791 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16792 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16793 },
Eric Biederman530b5192003-07-01 10:05:30 +000016794 [TEMPLATE_BINARY8_REG] = {
16795 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16796 .rhs = {
16797 [0] = { REG_VIRT0, REGCM_GPR8_LO },
16798 [1] = { REG_UNSET, REGCM_GPR8_LO },
16799 },
16800 },
16801 [TEMPLATE_BINARY16_REG] = {
16802 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16803 .rhs = {
16804 [0] = { REG_VIRT0, REGCM_GPR16 },
16805 [1] = { REG_UNSET, REGCM_GPR16 },
16806 },
16807 },
16808 [TEMPLATE_BINARY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016809 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16810 .rhs = {
16811 [0] = { REG_VIRT0, REGCM_GPR32 },
16812 [1] = { REG_UNSET, REGCM_GPR32 },
16813 },
16814 },
Eric Biederman530b5192003-07-01 10:05:30 +000016815 [TEMPLATE_BINARY8_IMM] = {
16816 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16817 .rhs = {
16818 [0] = { REG_VIRT0, REGCM_GPR8_LO },
16819 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16820 },
16821 },
16822 [TEMPLATE_BINARY16_IMM] = {
16823 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16824 .rhs = {
16825 [0] = { REG_VIRT0, REGCM_GPR16 },
16826 [1] = { REG_UNNEEDED, REGCM_IMM16 },
16827 },
16828 },
16829 [TEMPLATE_BINARY32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016830 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16831 .rhs = {
16832 [0] = { REG_VIRT0, REGCM_GPR32 },
16833 [1] = { REG_UNNEEDED, REGCM_IMM32 },
16834 },
16835 },
Eric Biederman530b5192003-07-01 10:05:30 +000016836 [TEMPLATE_SL8_CL] = {
16837 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16838 .rhs = {
16839 [0] = { REG_VIRT0, REGCM_GPR8_LO },
16840 [1] = { REG_CL, REGCM_GPR8_LO },
16841 },
16842 },
16843 [TEMPLATE_SL16_CL] = {
16844 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16845 .rhs = {
16846 [0] = { REG_VIRT0, REGCM_GPR16 },
16847 [1] = { REG_CL, REGCM_GPR8_LO },
16848 },
16849 },
16850 [TEMPLATE_SL32_CL] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016851 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16852 .rhs = {
16853 [0] = { REG_VIRT0, REGCM_GPR32 },
Eric Biederman530b5192003-07-01 10:05:30 +000016854 [1] = { REG_CL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016855 },
16856 },
Eric Biederman530b5192003-07-01 10:05:30 +000016857 [TEMPLATE_SL8_IMM] = {
16858 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16859 .rhs = {
16860 [0] = { REG_VIRT0, REGCM_GPR8_LO },
16861 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16862 },
16863 },
16864 [TEMPLATE_SL16_IMM] = {
16865 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16866 .rhs = {
16867 [0] = { REG_VIRT0, REGCM_GPR16 },
16868 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16869 },
16870 },
16871 [TEMPLATE_SL32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016872 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16873 .rhs = {
16874 [0] = { REG_VIRT0, REGCM_GPR32 },
16875 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16876 },
16877 },
Eric Biederman530b5192003-07-01 10:05:30 +000016878 [TEMPLATE_UNARY8] = {
16879 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16880 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16881 },
16882 [TEMPLATE_UNARY16] = {
16883 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16884 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16885 },
16886 [TEMPLATE_UNARY32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016887 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16888 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16889 },
Eric Biederman530b5192003-07-01 10:05:30 +000016890 [TEMPLATE_CMP8_REG] = {
16891 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16892 .rhs = {
16893 [0] = { REG_UNSET, REGCM_GPR8_LO },
16894 [1] = { REG_UNSET, REGCM_GPR8_LO },
16895 },
16896 },
16897 [TEMPLATE_CMP16_REG] = {
16898 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16899 .rhs = {
16900 [0] = { REG_UNSET, REGCM_GPR16 },
16901 [1] = { REG_UNSET, REGCM_GPR16 },
16902 },
16903 },
16904 [TEMPLATE_CMP32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016905 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16906 .rhs = {
16907 [0] = { REG_UNSET, REGCM_GPR32 },
16908 [1] = { REG_UNSET, REGCM_GPR32 },
16909 },
16910 },
Eric Biederman530b5192003-07-01 10:05:30 +000016911 [TEMPLATE_CMP8_IMM] = {
16912 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16913 .rhs = {
16914 [0] = { REG_UNSET, REGCM_GPR8_LO },
16915 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16916 },
16917 },
16918 [TEMPLATE_CMP16_IMM] = {
16919 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16920 .rhs = {
16921 [0] = { REG_UNSET, REGCM_GPR16 },
16922 [1] = { REG_UNNEEDED, REGCM_IMM16 },
16923 },
16924 },
16925 [TEMPLATE_CMP32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016926 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16927 .rhs = {
16928 [0] = { REG_UNSET, REGCM_GPR32 },
16929 [1] = { REG_UNNEEDED, REGCM_IMM32 },
16930 },
16931 },
Eric Biederman530b5192003-07-01 10:05:30 +000016932 [TEMPLATE_TEST8] = {
16933 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16934 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
16935 },
16936 [TEMPLATE_TEST16] = {
16937 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16938 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
16939 },
16940 [TEMPLATE_TEST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016941 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16942 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16943 },
16944 [TEMPLATE_SET] = {
Eric Biederman530b5192003-07-01 10:05:30 +000016945 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016946 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16947 },
16948 [TEMPLATE_JMP] = {
16949 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16950 },
16951 [TEMPLATE_INB_DX] = {
Eric Biederman530b5192003-07-01 10:05:30 +000016952 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016953 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
16954 },
16955 [TEMPLATE_INB_IMM] = {
Eric Biederman530b5192003-07-01 10:05:30 +000016956 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016957 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16958 },
16959 [TEMPLATE_INW_DX] = {
16960 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
16961 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
16962 },
16963 [TEMPLATE_INW_IMM] = {
16964 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
16965 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16966 },
16967 [TEMPLATE_INL_DX] = {
16968 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
16969 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
16970 },
16971 [TEMPLATE_INL_IMM] = {
16972 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
16973 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16974 },
16975 [TEMPLATE_OUTB_DX] = {
16976 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000016977 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016978 [1] = { REG_DX, REGCM_GPR16 },
16979 },
16980 },
16981 [TEMPLATE_OUTB_IMM] = {
16982 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000016983 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016984 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16985 },
16986 },
16987 [TEMPLATE_OUTW_DX] = {
16988 .rhs = {
16989 [0] = { REG_AX, REGCM_GPR16 },
16990 [1] = { REG_DX, REGCM_GPR16 },
16991 },
16992 },
16993 [TEMPLATE_OUTW_IMM] = {
16994 .rhs = {
16995 [0] = { REG_AX, REGCM_GPR16 },
16996 [1] = { REG_UNNEEDED, REGCM_IMM8 },
16997 },
16998 },
16999 [TEMPLATE_OUTL_DX] = {
17000 .rhs = {
17001 [0] = { REG_EAX, REGCM_GPR32 },
17002 [1] = { REG_DX, REGCM_GPR16 },
17003 },
17004 },
17005 [TEMPLATE_OUTL_IMM] = {
17006 .rhs = {
17007 [0] = { REG_EAX, REGCM_GPR32 },
17008 [1] = { REG_UNNEEDED, REGCM_IMM8 },
17009 },
17010 },
17011 [TEMPLATE_BSF] = {
17012 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
17013 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
17014 },
17015 [TEMPLATE_RDMSR] = {
17016 .lhs = {
17017 [0] = { REG_EAX, REGCM_GPR32 },
17018 [1] = { REG_EDX, REGCM_GPR32 },
17019 },
17020 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
17021 },
17022 [TEMPLATE_WRMSR] = {
17023 .rhs = {
17024 [0] = { REG_ECX, REGCM_GPR32 },
17025 [1] = { REG_EAX, REGCM_GPR32 },
17026 [2] = { REG_EDX, REGCM_GPR32 },
17027 },
17028 },
Eric Biederman530b5192003-07-01 10:05:30 +000017029 [TEMPLATE_UMUL8] = {
17030 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
17031 .rhs = {
17032 [0] = { REG_AL, REGCM_GPR8_LO },
17033 [1] = { REG_UNSET, REGCM_GPR8_LO },
17034 },
17035 },
17036 [TEMPLATE_UMUL16] = {
17037 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
17038 .rhs = {
17039 [0] = { REG_AX, REGCM_GPR16 },
17040 [1] = { REG_UNSET, REGCM_GPR16 },
17041 },
17042 },
17043 [TEMPLATE_UMUL32] = {
17044 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000017045 .rhs = {
17046 [0] = { REG_EAX, REGCM_GPR32 },
17047 [1] = { REG_UNSET, REGCM_GPR32 },
17048 },
17049 },
Eric Biederman530b5192003-07-01 10:05:30 +000017050 [TEMPLATE_DIV8] = {
17051 .lhs = {
17052 [0] = { REG_AL, REGCM_GPR8_LO },
17053 [1] = { REG_AH, REGCM_GPR8 },
17054 },
17055 .rhs = {
17056 [0] = { REG_AX, REGCM_GPR16 },
17057 [1] = { REG_UNSET, REGCM_GPR8_LO },
17058 },
17059 },
17060 [TEMPLATE_DIV16] = {
17061 .lhs = {
17062 [0] = { REG_AX, REGCM_GPR16 },
17063 [1] = { REG_DX, REGCM_GPR16 },
17064 },
17065 .rhs = {
17066 [0] = { REG_DXAX, REGCM_DIVIDEND32 },
17067 [1] = { REG_UNSET, REGCM_GPR16 },
17068 },
17069 },
17070 [TEMPLATE_DIV32] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000017071 .lhs = {
17072 [0] = { REG_EAX, REGCM_GPR32 },
17073 [1] = { REG_EDX, REGCM_GPR32 },
17074 },
17075 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000017076 [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000017077 [1] = { REG_UNSET, REGCM_GPR32 },
17078 },
17079 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017080};
Eric Biedermanb138ac82003-04-22 18:44:01 +000017081
Eric Biederman83b991a2003-10-11 06:20:25 +000017082static void fixup_branch(struct compile_state *state,
17083 struct triple *branch, int jmp_op, int cmp_op, struct type *cmp_type,
17084 struct triple *left, struct triple *right)
17085{
17086 struct triple *test;
17087 if (!left) {
17088 internal_error(state, branch, "no branch test?");
17089 }
17090 test = pre_triple(state, branch,
17091 cmp_op, cmp_type, left, right);
17092 test->template_id = TEMPLATE_TEST32;
17093 if (cmp_op == OP_CMP) {
17094 test->template_id = TEMPLATE_CMP32_REG;
17095 if (get_imm32(test, &RHS(test, 1))) {
17096 test->template_id = TEMPLATE_CMP32_IMM;
17097 }
17098 }
17099 use_triple(RHS(test, 0), test);
17100 use_triple(RHS(test, 1), test);
17101 unuse_triple(RHS(branch, 0), branch);
17102 RHS(branch, 0) = test;
17103 branch->op = jmp_op;
17104 branch->template_id = TEMPLATE_JMP;
17105 use_triple(RHS(branch, 0), branch);
17106}
17107
Eric Biedermanb138ac82003-04-22 18:44:01 +000017108static void fixup_branches(struct compile_state *state,
17109 struct triple *cmp, struct triple *use, int jmp_op)
17110{
17111 struct triple_set *entry, *next;
17112 for(entry = use->use; entry; entry = next) {
17113 next = entry->next;
17114 if (entry->member->op == OP_COPY) {
17115 fixup_branches(state, cmp, entry->member, jmp_op);
17116 }
17117 else if (entry->member->op == OP_BRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017118 struct triple *branch;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017119 struct triple *left, *right;
17120 left = right = 0;
17121 left = RHS(cmp, 0);
17122 if (TRIPLE_RHS(cmp->sizes) > 1) {
17123 right = RHS(cmp, 1);
17124 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017125 branch = entry->member;
Eric Biederman83b991a2003-10-11 06:20:25 +000017126 fixup_branch(state, branch, jmp_op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000017127 cmp->op, cmp->type, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017128 }
17129 }
17130}
17131
17132static void bool_cmp(struct compile_state *state,
17133 struct triple *ins, int cmp_op, int jmp_op, int set_op)
17134{
Eric Biedermanb138ac82003-04-22 18:44:01 +000017135 struct triple_set *entry, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017136 struct triple *set;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017137
17138 /* Put a barrier up before the cmp which preceeds the
17139 * copy instruction. If a set actually occurs this gives
17140 * us a chance to move variables in registers out of the way.
17141 */
17142
17143 /* Modify the comparison operator */
17144 ins->op = cmp_op;
Eric Biederman530b5192003-07-01 10:05:30 +000017145 ins->template_id = TEMPLATE_TEST32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017146 if (cmp_op == OP_CMP) {
Eric Biederman530b5192003-07-01 10:05:30 +000017147 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017148 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000017149 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017150 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017151 }
17152 /* Generate the instruction sequence that will transform the
17153 * result of the comparison into a logical value.
17154 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000017155 set = post_triple(state, ins, set_op, &char_type, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017156 use_triple(ins, set);
17157 set->template_id = TEMPLATE_SET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017158
Eric Biedermanb138ac82003-04-22 18:44:01 +000017159 for(entry = ins->use; entry; entry = next) {
17160 next = entry->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017161 if (entry->member == set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017162 continue;
17163 }
17164 replace_rhs_use(state, ins, set, entry->member);
17165 }
17166 fixup_branches(state, ins, set, jmp_op);
17167}
17168
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017169static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
Eric Biederman0babc1c2003-05-09 02:39:00 +000017170{
17171 struct triple *next;
17172 int lhs, i;
17173 lhs = TRIPLE_LHS(ins->sizes);
17174 for(next = ins->next, i = 0; i < lhs; i++, next = next->next) {
17175 if (next != LHS(ins, i)) {
17176 internal_error(state, ins, "malformed lhs on %s",
17177 tops(ins->op));
17178 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017179 if (next->op != OP_PIECE) {
17180 internal_error(state, ins, "bad lhs op %s at %d on %s",
17181 tops(next->op), i, tops(ins->op));
17182 }
17183 if (next->u.cval != i) {
17184 internal_error(state, ins, "bad u.cval of %d %d expected",
17185 next->u.cval, i);
17186 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017187 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017188 return next;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017189}
Eric Biedermanb138ac82003-04-22 18:44:01 +000017190
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017191struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
17192{
17193 struct ins_template *template;
17194 struct reg_info result;
17195 int zlhs;
17196 if (ins->op == OP_PIECE) {
17197 index = ins->u.cval;
17198 ins = MISC(ins, 0);
17199 }
17200 zlhs = TRIPLE_LHS(ins->sizes);
17201 if (triple_is_def(state, ins)) {
17202 zlhs = 1;
17203 }
17204 if (index >= zlhs) {
17205 internal_error(state, ins, "index %d out of range for %s\n",
17206 index, tops(ins->op));
17207 }
17208 switch(ins->op) {
17209 case OP_ASM:
17210 template = &ins->u.ainfo->tmpl;
17211 break;
17212 default:
17213 if (ins->template_id > LAST_TEMPLATE) {
17214 internal_error(state, ins, "bad template number %d",
17215 ins->template_id);
17216 }
17217 template = &templates[ins->template_id];
17218 break;
17219 }
17220 result = template->lhs[index];
17221 result.regcm = arch_regcm_normalize(state, result.regcm);
17222 if (result.reg != REG_UNNEEDED) {
17223 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
17224 }
17225 if (result.regcm == 0) {
17226 internal_error(state, ins, "lhs %d regcm == 0", index);
17227 }
17228 return result;
17229}
17230
17231struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
17232{
17233 struct reg_info result;
17234 struct ins_template *template;
17235 if ((index > TRIPLE_RHS(ins->sizes)) ||
17236 (ins->op == OP_PIECE)) {
17237 internal_error(state, ins, "index %d out of range for %s\n",
17238 index, tops(ins->op));
17239 }
17240 switch(ins->op) {
17241 case OP_ASM:
17242 template = &ins->u.ainfo->tmpl;
17243 break;
17244 default:
17245 if (ins->template_id > LAST_TEMPLATE) {
17246 internal_error(state, ins, "bad template number %d",
17247 ins->template_id);
17248 }
17249 template = &templates[ins->template_id];
17250 break;
17251 }
17252 result = template->rhs[index];
17253 result.regcm = arch_regcm_normalize(state, result.regcm);
17254 if (result.regcm == 0) {
17255 internal_error(state, ins, "rhs %d regcm == 0", index);
17256 }
17257 return result;
17258}
17259
Eric Biederman530b5192003-07-01 10:05:30 +000017260static struct triple *mod_div(struct compile_state *state,
17261 struct triple *ins, int div_op, int index)
17262{
17263 struct triple *div, *piece0, *piece1;
17264
17265 /* Generate a piece to hold the remainder */
17266 piece1 = post_triple(state, ins, OP_PIECE, ins->type, 0, 0);
17267 piece1->u.cval = 1;
17268
17269 /* Generate a piece to hold the quotient */
17270 piece0 = post_triple(state, ins, OP_PIECE, ins->type, 0, 0);
17271 piece0->u.cval = 0;
17272
17273 /* Generate the appropriate division instruction */
17274 div = post_triple(state, ins, div_op, ins->type, 0, 0);
17275 RHS(div, 0) = RHS(ins, 0);
17276 RHS(div, 1) = RHS(ins, 1);
17277 LHS(div, 0) = piece0;
17278 LHS(div, 1) = piece1;
17279 div->template_id = TEMPLATE_DIV32;
17280 use_triple(RHS(div, 0), div);
17281 use_triple(RHS(div, 1), div);
17282 use_triple(LHS(div, 0), div);
17283 use_triple(LHS(div, 1), div);
17284
17285 /* Hook on piece0 */
17286 MISC(piece0, 0) = div;
17287 use_triple(div, piece0);
17288
17289 /* Hook on piece1 */
17290 MISC(piece1, 0) = div;
17291 use_triple(div, piece1);
17292
17293 /* Replate uses of ins with the appropriate piece of the div */
17294 propogate_use(state, ins, LHS(div, index));
17295 release_triple(state, ins);
17296
17297 /* Return the address of the next instruction */
17298 return piece1->next;
17299}
17300
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017301static struct triple *transform_to_arch_instruction(
17302 struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017303{
17304 /* Transform from generic 3 address instructions
17305 * to archtecture specific instructions.
Eric Biedermand1ea5392003-06-28 06:49:45 +000017306 * And apply architecture specific constraints to instructions.
Eric Biedermanb138ac82003-04-22 18:44:01 +000017307 * Copies are inserted to preserve the register flexibility
17308 * of 3 address instructions.
17309 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017310 struct triple *next;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017311 size_t size;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017312 next = ins->next;
17313 switch(ins->op) {
17314 case OP_INTCONST:
17315 ins->template_id = TEMPLATE_INTCONST32;
17316 if (ins->u.cval < 256) {
17317 ins->template_id = TEMPLATE_INTCONST8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017318 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017319 break;
17320 case OP_ADDRCONST:
17321 ins->template_id = TEMPLATE_INTCONST32;
17322 break;
17323 case OP_NOOP:
17324 case OP_SDECL:
17325 case OP_BLOBCONST:
17326 case OP_LABEL:
17327 ins->template_id = TEMPLATE_NOP;
17328 break;
17329 case OP_COPY:
Eric Biedermand1ea5392003-06-28 06:49:45 +000017330 size = size_of(state, ins->type);
17331 if (is_imm8(RHS(ins, 0)) && (size <= 1)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017332 ins->template_id = TEMPLATE_COPY_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017333 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017334 else if (is_imm16(RHS(ins, 0)) && (size <= 2)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017335 ins->template_id = TEMPLATE_COPY_IMM16;
17336 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017337 else if (is_imm32(RHS(ins, 0)) && (size <= 4)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017338 ins->template_id = TEMPLATE_COPY_IMM32;
17339 }
17340 else if (is_const(RHS(ins, 0))) {
17341 internal_error(state, ins, "bad constant passed to copy");
17342 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017343 else if (size <= 1) {
17344 ins->template_id = TEMPLATE_COPY8_REG;
17345 }
17346 else if (size <= 2) {
17347 ins->template_id = TEMPLATE_COPY16_REG;
17348 }
17349 else if (size <= 4) {
17350 ins->template_id = TEMPLATE_COPY32_REG;
17351 }
17352 else {
17353 internal_error(state, ins, "bad type passed to copy");
17354 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017355 break;
17356 case OP_PHI:
Eric Biedermand1ea5392003-06-28 06:49:45 +000017357 size = size_of(state, ins->type);
17358 if (size <= 1) {
17359 ins->template_id = TEMPLATE_PHI8;
17360 }
17361 else if (size <= 2) {
17362 ins->template_id = TEMPLATE_PHI16;
17363 }
17364 else if (size <= 4) {
17365 ins->template_id = TEMPLATE_PHI32;
17366 }
17367 else {
17368 internal_error(state, ins, "bad type passed to phi");
17369 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017370 break;
17371 case OP_STORE:
17372 switch(ins->type->type & TYPE_MASK) {
17373 case TYPE_CHAR: case TYPE_UCHAR:
17374 ins->template_id = TEMPLATE_STORE8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017375 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017376 case TYPE_SHORT: case TYPE_USHORT:
17377 ins->template_id = TEMPLATE_STORE16;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017378 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017379 case TYPE_INT: case TYPE_UINT:
17380 case TYPE_LONG: case TYPE_ULONG:
17381 case TYPE_POINTER:
17382 ins->template_id = TEMPLATE_STORE32;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017383 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017384 default:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017385 internal_error(state, ins, "unknown type in store");
Eric Biedermanb138ac82003-04-22 18:44:01 +000017386 break;
17387 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017388 break;
17389 case OP_LOAD:
17390 switch(ins->type->type & TYPE_MASK) {
17391 case TYPE_CHAR: case TYPE_UCHAR:
17392 ins->template_id = TEMPLATE_LOAD8;
17393 break;
17394 case TYPE_SHORT:
17395 case TYPE_USHORT:
17396 ins->template_id = TEMPLATE_LOAD16;
17397 break;
17398 case TYPE_INT:
17399 case TYPE_UINT:
17400 case TYPE_LONG:
17401 case TYPE_ULONG:
17402 case TYPE_POINTER:
17403 ins->template_id = TEMPLATE_LOAD32;
17404 break;
17405 default:
17406 internal_error(state, ins, "unknown type in load");
17407 break;
17408 }
17409 break;
17410 case OP_ADD:
17411 case OP_SUB:
17412 case OP_AND:
17413 case OP_XOR:
17414 case OP_OR:
17415 case OP_SMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000017416 ins->template_id = TEMPLATE_BINARY32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017417 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000017418 ins->template_id = TEMPLATE_BINARY32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017419 }
17420 break;
Eric Biederman530b5192003-07-01 10:05:30 +000017421 case OP_SDIVT:
17422 case OP_UDIVT:
17423 ins->template_id = TEMPLATE_DIV32;
17424 next = after_lhs(state, ins);
17425 break;
17426 /* FIXME UMUL does not work yet.. */
Eric Biedermand1ea5392003-06-28 06:49:45 +000017427 case OP_UMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000017428 ins->template_id = TEMPLATE_UMUL32;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017429 break;
17430 case OP_UDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000017431 next = mod_div(state, ins, OP_UDIVT, 0);
17432 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017433 case OP_SDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000017434 next = mod_div(state, ins, OP_SDIVT, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017435 break;
17436 case OP_UMOD:
Eric Biederman530b5192003-07-01 10:05:30 +000017437 next = mod_div(state, ins, OP_UDIVT, 1);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017438 break;
Eric Biederman530b5192003-07-01 10:05:30 +000017439 case OP_SMOD:
17440 next = mod_div(state, ins, OP_SDIVT, 1);
17441 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017442 case OP_SL:
17443 case OP_SSR:
17444 case OP_USR:
Eric Biederman530b5192003-07-01 10:05:30 +000017445 ins->template_id = TEMPLATE_SL32_CL;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017446 if (get_imm8(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000017447 ins->template_id = TEMPLATE_SL32_IMM;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017448 } else if (size_of(state, RHS(ins, 1)->type) > 1) {
17449 typed_pre_copy(state, &char_type, ins, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017450 }
17451 break;
17452 case OP_INVERT:
17453 case OP_NEG:
Eric Biederman530b5192003-07-01 10:05:30 +000017454 ins->template_id = TEMPLATE_UNARY32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017455 break;
17456 case OP_EQ:
17457 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
17458 break;
17459 case OP_NOTEQ:
17460 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
17461 break;
17462 case OP_SLESS:
17463 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
17464 break;
17465 case OP_ULESS:
17466 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
17467 break;
17468 case OP_SMORE:
17469 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
17470 break;
17471 case OP_UMORE:
17472 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
17473 break;
17474 case OP_SLESSEQ:
17475 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
17476 break;
17477 case OP_ULESSEQ:
17478 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
17479 break;
17480 case OP_SMOREEQ:
17481 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
17482 break;
17483 case OP_UMOREEQ:
17484 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
17485 break;
17486 case OP_LTRUE:
17487 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
17488 break;
17489 case OP_LFALSE:
17490 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
17491 break;
17492 case OP_BRANCH:
17493 if (TRIPLE_RHS(ins->sizes) > 0) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017494 struct triple *left = RHS(ins, 0);
17495 fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST,
17496 left->type, left, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017497 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017498 else {
17499 ins->op = OP_JMP;
17500 ins->template_id = TEMPLATE_NOP;
17501 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017502 break;
17503 case OP_INB:
17504 case OP_INW:
17505 case OP_INL:
17506 switch(ins->op) {
17507 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
17508 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
17509 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
17510 }
17511 if (get_imm8(ins, &RHS(ins, 0))) {
17512 ins->template_id += 1;
17513 }
17514 break;
17515 case OP_OUTB:
17516 case OP_OUTW:
17517 case OP_OUTL:
17518 switch(ins->op) {
17519 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
17520 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
17521 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
17522 }
17523 if (get_imm8(ins, &RHS(ins, 1))) {
17524 ins->template_id += 1;
17525 }
17526 break;
17527 case OP_BSF:
17528 case OP_BSR:
17529 ins->template_id = TEMPLATE_BSF;
17530 break;
17531 case OP_RDMSR:
17532 ins->template_id = TEMPLATE_RDMSR;
17533 next = after_lhs(state, ins);
17534 break;
17535 case OP_WRMSR:
17536 ins->template_id = TEMPLATE_WRMSR;
17537 break;
17538 case OP_HLT:
17539 ins->template_id = TEMPLATE_NOP;
17540 break;
17541 case OP_ASM:
17542 ins->template_id = TEMPLATE_NOP;
17543 next = after_lhs(state, ins);
17544 break;
17545 /* Already transformed instructions */
17546 case OP_TEST:
Eric Biederman530b5192003-07-01 10:05:30 +000017547 ins->template_id = TEMPLATE_TEST32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017548 break;
17549 case OP_CMP:
Eric Biederman530b5192003-07-01 10:05:30 +000017550 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017551 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000017552 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017553 }
17554 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000017555 case OP_JMP:
17556 ins->template_id = TEMPLATE_NOP;
17557 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017558 case OP_JMP_EQ: case OP_JMP_NOTEQ:
17559 case OP_JMP_SLESS: case OP_JMP_ULESS:
17560 case OP_JMP_SMORE: case OP_JMP_UMORE:
17561 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
17562 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
17563 ins->template_id = TEMPLATE_JMP;
17564 break;
17565 case OP_SET_EQ: case OP_SET_NOTEQ:
17566 case OP_SET_SLESS: case OP_SET_ULESS:
17567 case OP_SET_SMORE: case OP_SET_UMORE:
17568 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
17569 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
17570 ins->template_id = TEMPLATE_SET;
17571 break;
17572 /* Unhandled instructions */
17573 case OP_PIECE:
17574 default:
17575 internal_error(state, ins, "unhandled ins: %d %s\n",
17576 ins->op, tops(ins->op));
17577 break;
17578 }
17579 return next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017580}
17581
Eric Biederman530b5192003-07-01 10:05:30 +000017582static long next_label(struct compile_state *state)
17583{
17584 static long label_counter = 0;
17585 return ++label_counter;
17586}
Eric Biedermanb138ac82003-04-22 18:44:01 +000017587static void generate_local_labels(struct compile_state *state)
17588{
17589 struct triple *first, *label;
Eric Biederman83b991a2003-10-11 06:20:25 +000017590 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017591 label = first;
17592 do {
17593 if ((label->op == OP_LABEL) ||
17594 (label->op == OP_SDECL)) {
17595 if (label->use) {
Eric Biederman530b5192003-07-01 10:05:30 +000017596 label->u.cval = next_label(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017597 } else {
17598 label->u.cval = 0;
17599 }
17600
17601 }
17602 label = label->next;
17603 } while(label != first);
17604}
17605
17606static int check_reg(struct compile_state *state,
17607 struct triple *triple, int classes)
17608{
17609 unsigned mask;
17610 int reg;
17611 reg = ID_REG(triple->id);
17612 if (reg == REG_UNSET) {
17613 internal_error(state, triple, "register not set");
17614 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017615 mask = arch_reg_regcm(state, reg);
17616 if (!(classes & mask)) {
17617 internal_error(state, triple, "reg %d in wrong class",
17618 reg);
17619 }
17620 return reg;
17621}
17622
17623static const char *arch_reg_str(int reg)
17624{
Eric Biederman530b5192003-07-01 10:05:30 +000017625#if REG_XMM7 != 44
17626#error "Registers have renumberd fix arch_reg_str"
17627#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017628 static const char *regs[] = {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017629 "%unset",
17630 "%unneeded",
Eric Biedermanb138ac82003-04-22 18:44:01 +000017631 "%eflags",
17632 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
17633 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
17634 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
17635 "%edx:%eax",
Eric Biederman530b5192003-07-01 10:05:30 +000017636 "%dx:%ax",
Eric Biedermanb138ac82003-04-22 18:44:01 +000017637 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
17638 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
17639 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
17640 };
17641 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
17642 reg = 0;
17643 }
17644 return regs[reg];
17645}
17646
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017647
Eric Biedermanb138ac82003-04-22 18:44:01 +000017648static const char *reg(struct compile_state *state, struct triple *triple,
17649 int classes)
17650{
17651 int reg;
17652 reg = check_reg(state, triple, classes);
17653 return arch_reg_str(reg);
17654}
17655
17656const char *type_suffix(struct compile_state *state, struct type *type)
17657{
17658 const char *suffix;
17659 switch(size_of(state, type)) {
17660 case 1: suffix = "b"; break;
17661 case 2: suffix = "w"; break;
17662 case 4: suffix = "l"; break;
17663 default:
17664 internal_error(state, 0, "unknown suffix");
17665 suffix = 0;
17666 break;
17667 }
17668 return suffix;
17669}
17670
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017671static void print_const_val(
17672 struct compile_state *state, struct triple *ins, FILE *fp)
17673{
17674 switch(ins->op) {
17675 case OP_INTCONST:
17676 fprintf(fp, " $%ld ",
Eric Biederman83b991a2003-10-11 06:20:25 +000017677 (long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017678 break;
17679 case OP_ADDRCONST:
Eric Biederman830c9882003-07-04 00:27:33 +000017680 if (MISC(ins, 0)->op != OP_SDECL) {
17681 internal_error(state, ins, "bad base for addrconst");
17682 }
17683 if (MISC(ins, 0)->u.cval <= 0) {
17684 internal_error(state, ins, "unlabeled constant");
17685 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000017686 fprintf(fp, " $L%s%lu+%lu ",
17687 state->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000017688 (unsigned long)(MISC(ins, 0)->u.cval),
17689 (unsigned long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017690 break;
17691 default:
17692 internal_error(state, ins, "unknown constant type");
17693 break;
17694 }
17695}
17696
Eric Biederman530b5192003-07-01 10:05:30 +000017697static void print_const(struct compile_state *state,
17698 struct triple *ins, FILE *fp)
17699{
17700 switch(ins->op) {
17701 case OP_INTCONST:
17702 switch(ins->type->type & TYPE_MASK) {
17703 case TYPE_CHAR:
17704 case TYPE_UCHAR:
Eric Biederman83b991a2003-10-11 06:20:25 +000017705 fprintf(fp, ".byte 0x%02lx\n",
17706 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000017707 break;
17708 case TYPE_SHORT:
17709 case TYPE_USHORT:
Eric Biederman83b991a2003-10-11 06:20:25 +000017710 fprintf(fp, ".short 0x%04lx\n",
17711 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000017712 break;
17713 case TYPE_INT:
17714 case TYPE_UINT:
17715 case TYPE_LONG:
17716 case TYPE_ULONG:
Eric Biederman83b991a2003-10-11 06:20:25 +000017717 fprintf(fp, ".int %lu\n",
17718 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000017719 break;
17720 default:
17721 internal_error(state, ins, "Unknown constant type");
17722 }
17723 break;
17724 case OP_ADDRCONST:
Eric Biederman830c9882003-07-04 00:27:33 +000017725 if (MISC(ins, 0)->op != OP_SDECL) {
17726 internal_error(state, ins, "bad base for addrconst");
17727 }
17728 if (MISC(ins, 0)->u.cval <= 0) {
17729 internal_error(state, ins, "unlabeled constant");
17730 }
17731 fprintf(fp, ".int L%s%lu+%lu\n",
Eric Biederman530b5192003-07-01 10:05:30 +000017732 state->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000017733 (unsigned long)(MISC(ins, 0)->u.cval),
17734 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000017735 break;
17736 case OP_BLOBCONST:
17737 {
17738 unsigned char *blob;
17739 size_t size, i;
17740 size = size_of(state, ins->type);
17741 blob = ins->u.blob;
17742 for(i = 0; i < size; i++) {
17743 fprintf(fp, ".byte 0x%02x\n",
17744 blob[i]);
17745 }
17746 break;
17747 }
17748 default:
17749 internal_error(state, ins, "Unknown constant type");
17750 break;
17751 }
17752}
17753
17754#define TEXT_SECTION ".rom.text"
17755#define DATA_SECTION ".rom.data"
17756
17757static long get_const_pool_ref(
17758 struct compile_state *state, struct triple *ins, FILE *fp)
17759{
17760 long ref;
17761 ref = next_label(state);
17762 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
17763 fprintf(fp, ".balign %d\n", align_of(state, ins->type));
17764 fprintf(fp, "L%s%lu:\n", state->label_prefix, ref);
17765 print_const(state, ins, fp);
17766 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
17767 return ref;
17768}
17769
Eric Biedermanb138ac82003-04-22 18:44:01 +000017770static void print_binary_op(struct compile_state *state,
17771 const char *op, struct triple *ins, FILE *fp)
17772{
17773 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000017774 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000017775 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017776 internal_error(state, ins, "invalid register assignment");
17777 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017778 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017779 fprintf(fp, "\t%s ", op);
17780 print_const_val(state, RHS(ins, 1), fp);
17781 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000017782 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000017783 }
17784 else {
17785 unsigned lmask, rmask;
17786 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017787 lreg = check_reg(state, RHS(ins, 0), mask);
17788 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017789 lmask = arch_reg_regcm(state, lreg);
17790 rmask = arch_reg_regcm(state, rreg);
17791 mask = lmask & rmask;
17792 fprintf(fp, "\t%s %s, %s\n",
17793 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000017794 reg(state, RHS(ins, 1), mask),
17795 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000017796 }
17797}
17798static void print_unary_op(struct compile_state *state,
17799 const char *op, struct triple *ins, FILE *fp)
17800{
17801 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000017802 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017803 fprintf(fp, "\t%s %s\n",
17804 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000017805 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000017806}
17807
17808static void print_op_shift(struct compile_state *state,
17809 const char *op, struct triple *ins, FILE *fp)
17810{
17811 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000017812 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000017813 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017814 internal_error(state, ins, "invalid register assignment");
17815 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017816 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017817 fprintf(fp, "\t%s ", op);
17818 print_const_val(state, RHS(ins, 1), fp);
17819 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000017820 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000017821 }
17822 else {
17823 fprintf(fp, "\t%s %s, %s\n",
17824 op,
Eric Biederman530b5192003-07-01 10:05:30 +000017825 reg(state, RHS(ins, 1), REGCM_GPR8_LO),
Eric Biederman0babc1c2003-05-09 02:39:00 +000017826 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000017827 }
17828}
17829
17830static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
17831{
17832 const char *op;
17833 int mask;
17834 int dreg;
17835 mask = 0;
17836 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000017837 case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017838 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
17839 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
17840 default:
17841 internal_error(state, ins, "not an in operation");
17842 op = 0;
17843 break;
17844 }
17845 dreg = check_reg(state, ins, mask);
17846 if (!reg_is_reg(state, dreg, REG_EAX)) {
17847 internal_error(state, ins, "dst != %%eax");
17848 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017849 if (is_const(RHS(ins, 0))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017850 fprintf(fp, "\t%s ", op);
17851 print_const_val(state, RHS(ins, 0), fp);
17852 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000017853 reg(state, ins, mask));
17854 }
17855 else {
17856 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017857 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017858 if (!reg_is_reg(state, addr_reg, REG_DX)) {
17859 internal_error(state, ins, "src != %%dx");
17860 }
17861 fprintf(fp, "\t%s %s, %s\n",
17862 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000017863 reg(state, RHS(ins, 0), REGCM_GPR16),
Eric Biedermanb138ac82003-04-22 18:44:01 +000017864 reg(state, ins, mask));
17865 }
17866}
17867
17868static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
17869{
17870 const char *op;
17871 int mask;
17872 int lreg;
17873 mask = 0;
17874 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000017875 case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017876 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
17877 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
17878 default:
17879 internal_error(state, ins, "not an out operation");
17880 op = 0;
17881 break;
17882 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017883 lreg = check_reg(state, RHS(ins, 0), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017884 if (!reg_is_reg(state, lreg, REG_EAX)) {
17885 internal_error(state, ins, "src != %%eax");
17886 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017887 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017888 fprintf(fp, "\t%s %s,",
17889 op, reg(state, RHS(ins, 0), mask));
17890 print_const_val(state, RHS(ins, 1), fp);
17891 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000017892 }
17893 else {
17894 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017895 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017896 if (!reg_is_reg(state, addr_reg, REG_DX)) {
17897 internal_error(state, ins, "dst != %%dx");
17898 }
17899 fprintf(fp, "\t%s %s, %s\n",
17900 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000017901 reg(state, RHS(ins, 0), mask),
17902 reg(state, RHS(ins, 1), REGCM_GPR16));
Eric Biedermanb138ac82003-04-22 18:44:01 +000017903 }
17904}
17905
17906static void print_op_move(struct compile_state *state,
17907 struct triple *ins, FILE *fp)
17908{
17909 /* op_move is complex because there are many types
17910 * of registers we can move between.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017911 * Because OP_COPY will be introduced in arbitrary locations
17912 * OP_COPY must not affect flags.
Eric Biedermanb138ac82003-04-22 18:44:01 +000017913 */
17914 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
17915 struct triple *dst, *src;
17916 if (ins->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000017917 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017918 dst = ins;
17919 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017920 else {
17921 internal_error(state, ins, "unknown move operation");
17922 src = dst = 0;
17923 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017924 if (!is_const(src)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017925 int src_reg, dst_reg;
17926 int src_regcm, dst_regcm;
Eric Biederman530b5192003-07-01 10:05:30 +000017927 src_reg = ID_REG(src->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017928 dst_reg = ID_REG(dst->id);
17929 src_regcm = arch_reg_regcm(state, src_reg);
Eric Biederman530b5192003-07-01 10:05:30 +000017930 dst_regcm = arch_reg_regcm(state, dst_reg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017931 /* If the class is the same just move the register */
17932 if (src_regcm & dst_regcm &
Eric Biederman530b5192003-07-01 10:05:30 +000017933 (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017934 if ((src_reg != dst_reg) || !omit_copy) {
17935 fprintf(fp, "\tmov %s, %s\n",
17936 reg(state, src, src_regcm),
17937 reg(state, dst, dst_regcm));
17938 }
17939 }
17940 /* Move 32bit to 16bit */
17941 else if ((src_regcm & REGCM_GPR32) &&
17942 (dst_regcm & REGCM_GPR16)) {
17943 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
17944 if ((src_reg != dst_reg) || !omit_copy) {
17945 fprintf(fp, "\tmovw %s, %s\n",
17946 arch_reg_str(src_reg),
17947 arch_reg_str(dst_reg));
17948 }
17949 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017950 /* Move from 32bit gprs to 16bit gprs */
17951 else if ((src_regcm & REGCM_GPR32) &&
17952 (dst_regcm & REGCM_GPR16)) {
17953 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
17954 if ((src_reg != dst_reg) || !omit_copy) {
17955 fprintf(fp, "\tmov %s, %s\n",
17956 arch_reg_str(src_reg),
17957 arch_reg_str(dst_reg));
17958 }
17959 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017960 /* Move 32bit to 8bit */
17961 else if ((src_regcm & REGCM_GPR32_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000017962 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000017963 {
17964 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
17965 if ((src_reg != dst_reg) || !omit_copy) {
17966 fprintf(fp, "\tmovb %s, %s\n",
17967 arch_reg_str(src_reg),
17968 arch_reg_str(dst_reg));
17969 }
17970 }
17971 /* Move 16bit to 8bit */
17972 else if ((src_regcm & REGCM_GPR16_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000017973 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000017974 {
17975 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
17976 if ((src_reg != dst_reg) || !omit_copy) {
17977 fprintf(fp, "\tmovb %s, %s\n",
17978 arch_reg_str(src_reg),
17979 arch_reg_str(dst_reg));
17980 }
17981 }
17982 /* Move 8/16bit to 16/32bit */
Eric Biederman530b5192003-07-01 10:05:30 +000017983 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017984 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017985 const char *op;
17986 op = is_signed(src->type)? "movsx": "movzx";
17987 fprintf(fp, "\t%s %s, %s\n",
17988 op,
17989 reg(state, src, src_regcm),
17990 reg(state, dst, dst_regcm));
17991 }
17992 /* Move between sse registers */
17993 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
17994 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017995 fprintf(fp, "\tmovdqa %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000017996 reg(state, src, src_regcm),
17997 reg(state, dst, dst_regcm));
17998 }
17999 }
Eric Biederman530b5192003-07-01 10:05:30 +000018000 /* Move between mmx registers */
18001 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018002 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018003 fprintf(fp, "\tmovq %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000018004 reg(state, src, src_regcm),
18005 reg(state, dst, dst_regcm));
18006 }
18007 }
Eric Biederman530b5192003-07-01 10:05:30 +000018008 /* Move from sse to mmx registers */
18009 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
18010 fprintf(fp, "\tmovdq2q %s, %s\n",
18011 reg(state, src, src_regcm),
18012 reg(state, dst, dst_regcm));
18013 }
18014 /* Move from mmx to sse registers */
18015 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
18016 fprintf(fp, "\tmovq2dq %s, %s\n",
18017 reg(state, src, src_regcm),
18018 reg(state, dst, dst_regcm));
18019 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018020 /* Move between 32bit gprs & mmx/sse registers */
18021 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
18022 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
18023 fprintf(fp, "\tmovd %s, %s\n",
18024 reg(state, src, src_regcm),
18025 reg(state, dst, dst_regcm));
18026 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018027 /* Move from 16bit gprs & mmx/sse registers */
18028 else if ((src_regcm & REGCM_GPR16) &&
18029 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
18030 const char *op;
18031 int mid_reg;
Eric Biederman678d8162003-07-03 03:59:38 +000018032 op = is_signed(src->type)? "movsx":"movzx";
Eric Biedermand1ea5392003-06-28 06:49:45 +000018033 mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
18034 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
18035 op,
18036 arch_reg_str(src_reg),
18037 arch_reg_str(mid_reg),
18038 arch_reg_str(mid_reg),
18039 arch_reg_str(dst_reg));
18040 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018041 /* Move from mmx/sse registers to 16bit gprs */
18042 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
18043 (dst_regcm & REGCM_GPR16)) {
18044 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
18045 fprintf(fp, "\tmovd %s, %s\n",
18046 arch_reg_str(src_reg),
18047 arch_reg_str(dst_reg));
18048 }
Eric Biederman530b5192003-07-01 10:05:30 +000018049 /* Move from gpr to 64bit dividend */
18050 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) &&
18051 (dst_regcm & REGCM_DIVIDEND64)) {
18052 const char *extend;
18053 extend = is_signed(src->type)? "cltd":"movl $0, %edx";
18054 fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
18055 arch_reg_str(src_reg),
18056 extend);
18057 }
18058 /* Move from 64bit gpr to gpr */
18059 else if ((src_regcm & REGCM_DIVIDEND64) &&
18060 (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
18061 if (dst_regcm & REGCM_GPR32) {
18062 src_reg = REG_EAX;
18063 }
18064 else if (dst_regcm & REGCM_GPR16) {
18065 src_reg = REG_AX;
18066 }
18067 else if (dst_regcm & REGCM_GPR8_LO) {
18068 src_reg = REG_AL;
18069 }
18070 fprintf(fp, "\tmov %s, %s\n",
18071 arch_reg_str(src_reg),
18072 arch_reg_str(dst_reg));
18073 }
18074 /* Move from mmx/sse registers to 64bit gpr */
18075 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
18076 (dst_regcm & REGCM_DIVIDEND64)) {
18077 const char *extend;
18078 extend = is_signed(src->type)? "cltd": "movl $0, %edx";
18079 fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
18080 arch_reg_str(src_reg),
18081 extend);
18082 }
18083 /* Move from 64bit gpr to mmx/sse register */
18084 else if ((src_regcm & REGCM_DIVIDEND64) &&
18085 (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
18086 fprintf(fp, "\tmovd %%eax, %s\n",
18087 arch_reg_str(dst_reg));
18088 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018089#if X86_4_8BIT_GPRS
18090 /* Move from 8bit gprs to mmx/sse registers */
Eric Biederman530b5192003-07-01 10:05:30 +000018091 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018092 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
18093 const char *op;
18094 int mid_reg;
18095 op = is_signed(src->type)? "movsx":"movzx";
18096 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
18097 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
18098 op,
18099 reg(state, src, src_regcm),
18100 arch_reg_str(mid_reg),
18101 arch_reg_str(mid_reg),
18102 reg(state, dst, dst_regcm));
18103 }
18104 /* Move from mmx/sse registers and 8bit gprs */
18105 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
Eric Biederman530b5192003-07-01 10:05:30 +000018106 (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018107 int mid_reg;
18108 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
18109 fprintf(fp, "\tmovd %s, %s\n",
18110 reg(state, src, src_regcm),
18111 arch_reg_str(mid_reg));
18112 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018113 /* Move from 32bit gprs to 8bit gprs */
18114 else if ((src_regcm & REGCM_GPR32) &&
Eric Biederman530b5192003-07-01 10:05:30 +000018115 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018116 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
18117 if ((src_reg != dst_reg) || !omit_copy) {
18118 fprintf(fp, "\tmov %s, %s\n",
18119 arch_reg_str(src_reg),
18120 arch_reg_str(dst_reg));
18121 }
18122 }
18123 /* Move from 16bit gprs to 8bit gprs */
18124 else if ((src_regcm & REGCM_GPR16) &&
Eric Biederman530b5192003-07-01 10:05:30 +000018125 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018126 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
18127 if ((src_reg != dst_reg) || !omit_copy) {
18128 fprintf(fp, "\tmov %s, %s\n",
18129 arch_reg_str(src_reg),
18130 arch_reg_str(dst_reg));
18131 }
18132 }
18133#endif /* X86_4_8BIT_GPRS */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018134 else {
18135 internal_error(state, ins, "unknown copy type");
18136 }
18137 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018138 else {
Eric Biederman530b5192003-07-01 10:05:30 +000018139 int dst_reg;
18140 int dst_regcm;
18141 dst_reg = ID_REG(dst->id);
18142 dst_regcm = arch_reg_regcm(state, dst_reg);
18143 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
18144 fprintf(fp, "\tmov ");
18145 print_const_val(state, src, fp);
18146 fprintf(fp, ", %s\n",
18147 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
18148 }
18149 else if (dst_regcm & REGCM_DIVIDEND64) {
18150 if (size_of(state, dst->type) > 4) {
18151 internal_error(state, ins, "64bit constant...");
18152 }
18153 fprintf(fp, "\tmov $0, %%edx\n");
18154 fprintf(fp, "\tmov ");
18155 print_const_val(state, src, fp);
18156 fprintf(fp, ", %%eax\n");
18157 }
18158 else if (dst_regcm & REGCM_DIVIDEND32) {
18159 if (size_of(state, dst->type) > 2) {
18160 internal_error(state, ins, "32bit constant...");
18161 }
18162 fprintf(fp, "\tmov $0, %%dx\n");
18163 fprintf(fp, "\tmov ");
18164 print_const_val(state, src, fp);
18165 fprintf(fp, ", %%ax");
18166 }
18167 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
18168 long ref;
18169 ref = get_const_pool_ref(state, src, fp);
18170 fprintf(fp, "\tmovq L%s%lu, %s\n",
18171 state->label_prefix, ref,
18172 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
18173 }
18174 else {
18175 internal_error(state, ins, "unknown copy immediate type");
18176 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018177 }
18178}
18179
18180static void print_op_load(struct compile_state *state,
18181 struct triple *ins, FILE *fp)
18182{
18183 struct triple *dst, *src;
18184 dst = ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018185 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018186 if (is_const(src) || is_const(dst)) {
18187 internal_error(state, ins, "unknown load operation");
18188 }
18189 fprintf(fp, "\tmov (%s), %s\n",
18190 reg(state, src, REGCM_GPR32),
Eric Biederman530b5192003-07-01 10:05:30 +000018191 reg(state, dst, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000018192}
18193
18194
18195static void print_op_store(struct compile_state *state,
18196 struct triple *ins, FILE *fp)
18197{
18198 struct triple *dst, *src;
Eric Biederman530b5192003-07-01 10:05:30 +000018199 dst = RHS(ins, 0);
18200 src = RHS(ins, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018201 if (is_const(src) && (src->op == OP_INTCONST)) {
18202 long_t value;
18203 value = (long_t)(src->u.cval);
18204 fprintf(fp, "\tmov%s $%ld, (%s)\n",
18205 type_suffix(state, src->type),
Eric Biederman83b991a2003-10-11 06:20:25 +000018206 (long)(value),
Eric Biedermanb138ac82003-04-22 18:44:01 +000018207 reg(state, dst, REGCM_GPR32));
18208 }
18209 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
18210 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
18211 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000018212 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biederman83b991a2003-10-11 06:20:25 +000018213 (unsigned long)(dst->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000018214 }
18215 else {
18216 if (is_const(src) || is_const(dst)) {
18217 internal_error(state, ins, "unknown store operation");
18218 }
18219 fprintf(fp, "\tmov%s %s, (%s)\n",
18220 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000018221 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000018222 reg(state, dst, REGCM_GPR32));
18223 }
18224
18225
18226}
18227
18228static void print_op_smul(struct compile_state *state,
18229 struct triple *ins, FILE *fp)
18230{
Eric Biederman0babc1c2003-05-09 02:39:00 +000018231 if (!is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018232 fprintf(fp, "\timul %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000018233 reg(state, RHS(ins, 1), REGCM_GPR32),
18234 reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000018235 }
18236 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018237 fprintf(fp, "\timul ");
18238 print_const_val(state, RHS(ins, 1), fp);
18239 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000018240 }
18241}
18242
18243static void print_op_cmp(struct compile_state *state,
18244 struct triple *ins, FILE *fp)
18245{
18246 unsigned mask;
18247 int dreg;
Eric Biederman530b5192003-07-01 10:05:30 +000018248 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018249 dreg = check_reg(state, ins, REGCM_FLAGS);
18250 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
18251 internal_error(state, ins, "bad dest register for cmp");
18252 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000018253 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018254 fprintf(fp, "\tcmp ");
18255 print_const_val(state, RHS(ins, 1), fp);
18256 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000018257 }
18258 else {
18259 unsigned lmask, rmask;
18260 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018261 lreg = check_reg(state, RHS(ins, 0), mask);
18262 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018263 lmask = arch_reg_regcm(state, lreg);
18264 rmask = arch_reg_regcm(state, rreg);
18265 mask = lmask & rmask;
18266 fprintf(fp, "\tcmp %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000018267 reg(state, RHS(ins, 1), mask),
18268 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000018269 }
18270}
18271
18272static void print_op_test(struct compile_state *state,
18273 struct triple *ins, FILE *fp)
18274{
18275 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000018276 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018277 fprintf(fp, "\ttest %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000018278 reg(state, RHS(ins, 0), mask),
18279 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000018280}
18281
18282static void print_op_branch(struct compile_state *state,
18283 struct triple *branch, FILE *fp)
18284{
18285 const char *bop = "j";
18286 if (branch->op == OP_JMP) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000018287 if (TRIPLE_RHS(branch->sizes) != 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018288 internal_error(state, branch, "jmp with condition?");
18289 }
18290 bop = "jmp";
18291 }
18292 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018293 struct triple *ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018294 if (TRIPLE_RHS(branch->sizes) != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018295 internal_error(state, branch, "jmpcc without condition?");
18296 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000018297 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
18298 if ((RHS(branch, 0)->op != OP_CMP) &&
18299 (RHS(branch, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018300 internal_error(state, branch, "bad branch test");
18301 }
18302#warning "FIXME I have observed instructions between the test and branch instructions"
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018303 ptr = RHS(branch, 0);
18304 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
18305 if (ptr->op != OP_COPY) {
18306 internal_error(state, branch, "branch does not follow test");
18307 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018308 }
18309 switch(branch->op) {
18310 case OP_JMP_EQ: bop = "jz"; break;
18311 case OP_JMP_NOTEQ: bop = "jnz"; break;
18312 case OP_JMP_SLESS: bop = "jl"; break;
18313 case OP_JMP_ULESS: bop = "jb"; break;
18314 case OP_JMP_SMORE: bop = "jg"; break;
18315 case OP_JMP_UMORE: bop = "ja"; break;
18316 case OP_JMP_SLESSEQ: bop = "jle"; break;
18317 case OP_JMP_ULESSEQ: bop = "jbe"; break;
18318 case OP_JMP_SMOREEQ: bop = "jge"; break;
18319 case OP_JMP_UMOREEQ: bop = "jae"; break;
18320 default:
18321 internal_error(state, branch, "Invalid branch op");
18322 break;
18323 }
18324
18325 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000018326 fprintf(fp, "\t%s L%s%lu\n",
18327 bop,
18328 state->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000018329 (unsigned long)(TARG(branch, 0)->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000018330}
18331
18332static void print_op_set(struct compile_state *state,
18333 struct triple *set, FILE *fp)
18334{
18335 const char *sop = "set";
Eric Biederman0babc1c2003-05-09 02:39:00 +000018336 if (TRIPLE_RHS(set->sizes) != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018337 internal_error(state, set, "setcc without condition?");
18338 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000018339 check_reg(state, RHS(set, 0), REGCM_FLAGS);
18340 if ((RHS(set, 0)->op != OP_CMP) &&
18341 (RHS(set, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018342 internal_error(state, set, "bad set test");
18343 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000018344 if (RHS(set, 0)->next != set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018345 internal_error(state, set, "set does not follow test");
18346 }
18347 switch(set->op) {
18348 case OP_SET_EQ: sop = "setz"; break;
18349 case OP_SET_NOTEQ: sop = "setnz"; break;
18350 case OP_SET_SLESS: sop = "setl"; break;
18351 case OP_SET_ULESS: sop = "setb"; break;
18352 case OP_SET_SMORE: sop = "setg"; break;
18353 case OP_SET_UMORE: sop = "seta"; break;
18354 case OP_SET_SLESSEQ: sop = "setle"; break;
18355 case OP_SET_ULESSEQ: sop = "setbe"; break;
18356 case OP_SET_SMOREEQ: sop = "setge"; break;
18357 case OP_SET_UMOREEQ: sop = "setae"; break;
18358 default:
18359 internal_error(state, set, "Invalid set op");
18360 break;
18361 }
18362 fprintf(fp, "\t%s %s\n",
Eric Biederman530b5192003-07-01 10:05:30 +000018363 sop, reg(state, set, REGCM_GPR8_LO));
Eric Biedermanb138ac82003-04-22 18:44:01 +000018364}
18365
18366static void print_op_bit_scan(struct compile_state *state,
18367 struct triple *ins, FILE *fp)
18368{
18369 const char *op;
18370 switch(ins->op) {
18371 case OP_BSF: op = "bsf"; break;
18372 case OP_BSR: op = "bsr"; break;
18373 default:
18374 internal_error(state, ins, "unknown bit scan");
18375 op = 0;
18376 break;
18377 }
18378 fprintf(fp,
18379 "\t%s %s, %s\n"
18380 "\tjnz 1f\n"
18381 "\tmovl $-1, %s\n"
18382 "1:\n",
18383 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000018384 reg(state, RHS(ins, 0), REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000018385 reg(state, ins, REGCM_GPR32),
18386 reg(state, ins, REGCM_GPR32));
18387}
18388
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018389
Eric Biedermanb138ac82003-04-22 18:44:01 +000018390static void print_sdecl(struct compile_state *state,
18391 struct triple *ins, FILE *fp)
18392{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018393 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000018394 fprintf(fp, ".balign %d\n", align_of(state, ins->type));
Eric Biederman83b991a2003-10-11 06:20:25 +000018395 fprintf(fp, "L%s%lu:\n",
18396 state->label_prefix, (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +000018397 print_const(state, MISC(ins, 0), fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018398 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000018399
18400}
18401
18402static void print_instruction(struct compile_state *state,
18403 struct triple *ins, FILE *fp)
18404{
18405 /* Assumption: after I have exted the register allocator
18406 * everything is in a valid register.
18407 */
18408 switch(ins->op) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018409 case OP_ASM:
18410 print_op_asm(state, ins, fp);
18411 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018412 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
18413 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
18414 case OP_AND: print_binary_op(state, "and", ins, fp); break;
18415 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
18416 case OP_OR: print_binary_op(state, "or", ins, fp); break;
18417 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
18418 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
18419 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
18420 case OP_POS: break;
18421 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
18422 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
18423 case OP_INTCONST:
18424 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018425 case OP_BLOBCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +000018426 /* Don't generate anything here for constants */
18427 case OP_PHI:
18428 /* Don't generate anything for variable declarations. */
18429 break;
18430 case OP_SDECL:
18431 print_sdecl(state, ins, fp);
18432 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018433 case OP_COPY:
18434 print_op_move(state, ins, fp);
18435 break;
18436 case OP_LOAD:
18437 print_op_load(state, ins, fp);
18438 break;
18439 case OP_STORE:
18440 print_op_store(state, ins, fp);
18441 break;
18442 case OP_SMUL:
18443 print_op_smul(state, ins, fp);
18444 break;
18445 case OP_CMP: print_op_cmp(state, ins, fp); break;
18446 case OP_TEST: print_op_test(state, ins, fp); break;
18447 case OP_JMP:
18448 case OP_JMP_EQ: case OP_JMP_NOTEQ:
18449 case OP_JMP_SLESS: case OP_JMP_ULESS:
18450 case OP_JMP_SMORE: case OP_JMP_UMORE:
18451 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
18452 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
18453 print_op_branch(state, ins, fp);
18454 break;
18455 case OP_SET_EQ: case OP_SET_NOTEQ:
18456 case OP_SET_SLESS: case OP_SET_ULESS:
18457 case OP_SET_SMORE: case OP_SET_UMORE:
18458 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
18459 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
18460 print_op_set(state, ins, fp);
18461 break;
18462 case OP_INB: case OP_INW: case OP_INL:
18463 print_op_in(state, ins, fp);
18464 break;
18465 case OP_OUTB: case OP_OUTW: case OP_OUTL:
18466 print_op_out(state, ins, fp);
18467 break;
18468 case OP_BSF:
18469 case OP_BSR:
18470 print_op_bit_scan(state, ins, fp);
18471 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018472 case OP_RDMSR:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018473 after_lhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000018474 fprintf(fp, "\trdmsr\n");
18475 break;
18476 case OP_WRMSR:
18477 fprintf(fp, "\twrmsr\n");
18478 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018479 case OP_HLT:
18480 fprintf(fp, "\thlt\n");
18481 break;
Eric Biederman530b5192003-07-01 10:05:30 +000018482 case OP_SDIVT:
18483 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
18484 break;
18485 case OP_UDIVT:
18486 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
18487 break;
18488 case OP_UMUL:
18489 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
18490 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018491 case OP_LABEL:
18492 if (!ins->use) {
18493 return;
18494 }
Eric Biederman83b991a2003-10-11 06:20:25 +000018495 fprintf(fp, "L%s%lu:\n",
18496 state->label_prefix, (unsigned long)(ins->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000018497 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018498 /* Ignore OP_PIECE */
18499 case OP_PIECE:
18500 break;
Eric Biederman530b5192003-07-01 10:05:30 +000018501 /* Operations that should never get here */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018502 case OP_SDIV: case OP_UDIV:
18503 case OP_SMOD: case OP_UMOD:
Eric Biedermanb138ac82003-04-22 18:44:01 +000018504 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
18505 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
18506 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
18507 default:
18508 internal_error(state, ins, "unknown op: %d %s",
18509 ins->op, tops(ins->op));
18510 break;
18511 }
18512}
18513
18514static void print_instructions(struct compile_state *state)
18515{
18516 struct triple *first, *ins;
18517 int print_location;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018518 struct occurance *last_occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018519 FILE *fp;
Eric Biederman530b5192003-07-01 10:05:30 +000018520 int max_inline_depth;
18521 max_inline_depth = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018522 print_location = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018523 last_occurance = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018524 fp = state->output;
18525 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000018526 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018527 ins = first;
18528 do {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018529 if (print_location &&
18530 last_occurance != ins->occurance) {
18531 if (!ins->occurance->parent) {
18532 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
18533 ins->occurance->function,
18534 ins->occurance->filename,
18535 ins->occurance->line,
18536 ins->occurance->col);
18537 }
18538 else {
18539 struct occurance *ptr;
Eric Biederman530b5192003-07-01 10:05:30 +000018540 int inline_depth;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018541 fprintf(fp, "\t/*\n");
Eric Biederman530b5192003-07-01 10:05:30 +000018542 inline_depth = 0;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018543 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
Eric Biederman530b5192003-07-01 10:05:30 +000018544 inline_depth++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018545 fprintf(fp, "\t * %s,%s:%d.%d\n",
18546 ptr->function,
18547 ptr->filename,
18548 ptr->line,
18549 ptr->col);
18550 }
18551 fprintf(fp, "\t */\n");
Eric Biederman530b5192003-07-01 10:05:30 +000018552 if (inline_depth > max_inline_depth) {
18553 max_inline_depth = inline_depth;
18554 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018555 }
18556 if (last_occurance) {
18557 put_occurance(last_occurance);
18558 }
18559 get_occurance(ins->occurance);
18560 last_occurance = ins->occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018561 }
18562
18563 print_instruction(state, ins, fp);
18564 ins = ins->next;
18565 } while(ins != first);
Eric Biederman530b5192003-07-01 10:05:30 +000018566 if (print_location) {
18567 fprintf(fp, "/* max inline depth %d */\n",
18568 max_inline_depth);
18569 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018570}
Eric Biederman530b5192003-07-01 10:05:30 +000018571
Eric Biedermanb138ac82003-04-22 18:44:01 +000018572static void generate_code(struct compile_state *state)
18573{
18574 generate_local_labels(state);
18575 print_instructions(state);
18576
18577}
18578
18579static void print_tokens(struct compile_state *state)
18580{
18581 struct token *tk;
18582 tk = &state->token[0];
18583 do {
18584#if 1
18585 token(state, 0);
18586#else
18587 next_token(state, 0);
18588#endif
18589 loc(stdout, state, 0);
18590 printf("%s <- `%s'\n",
18591 tokens[tk->tok],
18592 tk->ident ? tk->ident->name :
18593 tk->str_len ? tk->val.str : "");
18594
18595 } while(tk->tok != TOK_EOF);
18596}
18597
Eric Biederman83b991a2003-10-11 06:20:25 +000018598static void call_main(struct compile_state *state)
18599{
18600 struct triple *call;
18601 call = new_triple(state, OP_CALL, &void_func, -1, -1);
18602 call->type = &void_type;
18603 MISC(call, 0) = state->main_function;
18604 flatten(state, state->first, call);
18605}
18606
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018607static void compile(const char *filename, const char *ofilename,
Eric Biederman83b991a2003-10-11 06:20:25 +000018608 unsigned long features, int debug, int opt, const char *label_prefix)
Eric Biedermanb138ac82003-04-22 18:44:01 +000018609{
18610 int i;
18611 struct compile_state state;
Eric Biederman83b991a2003-10-11 06:20:25 +000018612 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018613 memset(&state, 0, sizeof(state));
18614 state.file = 0;
18615 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
18616 memset(&state.token[i], 0, sizeof(state.token[i]));
18617 state.token[i].tok = -1;
18618 }
18619 /* Remember the debug settings */
Eric Biederman83b991a2003-10-11 06:20:25 +000018620 state.features = features;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018621 state.debug = debug;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018622 state.optimize = opt;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018623 /* Remember the output filename */
18624 state.ofilename = ofilename;
18625 state.output = fopen(state.ofilename, "w");
18626 if (!state.output) {
18627 error(&state, 0, "Cannot open output file %s\n",
18628 ofilename);
18629 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000018630 /* Remember the label prefix */
18631 state.label_prefix = label_prefix;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018632 /* Prep the preprocessor */
18633 state.if_depth = 0;
18634 state.if_value = 0;
18635 /* register the C keywords */
18636 register_keywords(&state);
18637 /* register the keywords the macro preprocessor knows */
18638 register_macro_keywords(&state);
18639 /* Memorize where some special keywords are. */
Eric Biederman83b991a2003-10-11 06:20:25 +000018640 state.i_switch = lookup(&state, "switch", 6);
18641 state.i_case = lookup(&state, "case", 4);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018642 state.i_continue = lookup(&state, "continue", 8);
18643 state.i_break = lookup(&state, "break", 5);
Eric Biederman83b991a2003-10-11 06:20:25 +000018644 state.i_default = lookup(&state, "default", 7);
18645
18646 /* Allocate beginning bounding labels for the function list */
18647 state.first = label(&state);
18648 state.first->id |= TRIPLE_FLAG_VOLATILE;
18649 use_triple(state.first, state.first);
18650 ptr = label(&state);
18651 ptr->id |= TRIPLE_FLAG_VOLATILE;
18652 use_triple(ptr, ptr);
18653 flatten(&state, state.first, ptr);
18654
Eric Biedermanb138ac82003-04-22 18:44:01 +000018655 /* Enter the globl definition scope */
18656 start_scope(&state);
18657 register_builtins(&state);
18658 compile_file(&state, filename, 1);
18659#if 0
18660 print_tokens(&state);
18661#endif
18662 decls(&state);
18663 /* Exit the global definition scope */
18664 end_scope(&state);
18665
Eric Biederman83b991a2003-10-11 06:20:25 +000018666 /* Call the main function */
18667 call_main(&state);
18668
Eric Biedermanb138ac82003-04-22 18:44:01 +000018669 /* Now that basic compilation has happened
18670 * optimize the intermediate code
18671 */
18672 optimize(&state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018673
Eric Biedermanb138ac82003-04-22 18:44:01 +000018674 generate_code(&state);
18675 if (state.debug) {
18676 fprintf(stderr, "done\n");
18677 }
18678}
18679
18680static void version(void)
18681{
18682 printf("romcc " VERSION " released " RELEASE_DATE "\n");
18683}
18684
18685static void usage(void)
18686{
18687 version();
18688 printf(
18689 "Usage: romcc <source>.c\n"
18690 "Compile a C source file without using ram\n"
18691 );
18692}
18693
18694static void arg_error(char *fmt, ...)
18695{
18696 va_list args;
18697 va_start(args, fmt);
18698 vfprintf(stderr, fmt, args);
18699 va_end(args);
18700 usage();
18701 exit(1);
18702}
18703
18704int main(int argc, char **argv)
18705{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018706 const char *filename;
18707 const char *ofilename;
Eric Biederman05f26fc2003-06-11 21:55:00 +000018708 const char *label_prefix;
Eric Biederman83b991a2003-10-11 06:20:25 +000018709 unsigned long features;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018710 int last_argc;
18711 int debug;
18712 int optimize;
Eric Biederman83b991a2003-10-11 06:20:25 +000018713 features = 0;
Eric Biederman05f26fc2003-06-11 21:55:00 +000018714 label_prefix = "";
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018715 ofilename = "auto.inc";
Eric Biedermanb138ac82003-04-22 18:44:01 +000018716 optimize = 0;
18717 debug = 0;
18718 last_argc = -1;
18719 while((argc > 1) && (argc != last_argc)) {
18720 last_argc = argc;
18721 if (strncmp(argv[1], "--debug=", 8) == 0) {
18722 debug = atoi(argv[1] + 8);
18723 argv++;
18724 argc--;
18725 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000018726 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
18727 label_prefix= argv[1] + 15;
18728 argv++;
18729 argc--;
18730 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018731 else if ((strcmp(argv[1],"-O") == 0) ||
18732 (strcmp(argv[1], "-O1") == 0)) {
18733 optimize = 1;
18734 argv++;
18735 argc--;
18736 }
18737 else if (strcmp(argv[1],"-O2") == 0) {
18738 optimize = 2;
18739 argv++;
18740 argc--;
18741 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018742 else if ((strcmp(argv[1], "-o") == 0) && (argc > 2)) {
18743 ofilename = argv[2];
18744 argv += 2;
18745 argc -= 2;
18746 }
Eric Biederman83b991a2003-10-11 06:20:25 +000018747 else if (strncmp(argv[1], "-m", 2) == 0) {
18748 int result;
18749 result = arch_encode_feature(argv[1] + 2, &features);
18750 if (result < 0) {
18751 arg_error("Invalid feature specified: %s\n",
18752 argv[1] + 2);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018753 }
18754 argv++;
18755 argc--;
18756 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018757 }
18758 if (argc != 2) {
18759 arg_error("Wrong argument count %d\n", argc);
18760 }
18761 filename = argv[1];
Eric Biederman83b991a2003-10-11 06:20:25 +000018762 compile(filename, ofilename, features, debug, optimize, label_prefix);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018763
18764 return 0;
18765}