blob: 1f5c91af821dd99a2242c85d5cbf9e1ac785c2f1 [file] [log] [blame]
Eric Biedermanb138ac82003-04-22 18:44:01 +00001#include <stdarg.h>
2#include <errno.h>
3#include <stdint.h>
4#include <stdlib.h>
5#include <stdio.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <fcntl.h>
9#include <unistd.h>
10#include <stdio.h>
11#include <string.h>
Eric Biedermanb138ac82003-04-22 18:44:01 +000012#include <limits.h>
13
14#define DEBUG_ERROR_MESSAGES 0
15#define DEBUG_COLOR_GRAPH 0
16#define DEBUG_SCC 0
Eric Biederman153ea352003-06-20 14:43:20 +000017#define DEBUG_CONSISTENCY 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000018
Eric Biederman05f26fc2003-06-11 21:55:00 +000019#warning "FIXME boundary cases with small types in larger registers"
Eric Biederman8d9c1232003-06-17 08:42:17 +000020#warning "FIXME give clear error messages about unused variables"
Eric Biederman05f26fc2003-06-11 21:55:00 +000021
Eric Biedermanb138ac82003-04-22 18:44:01 +000022/* Control flow graph of a loop without goto.
23 *
24 * AAA
25 * +---/
26 * /
27 * / +--->CCC
28 * | | / \
29 * | | DDD EEE break;
30 * | | \ \
31 * | | FFF \
32 * \| / \ \
33 * |\ GGG HHH | continue;
34 * | \ \ | |
35 * | \ III | /
36 * | \ | / /
37 * | vvv /
38 * +----BBB /
39 * | /
40 * vv
41 * JJJ
42 *
43 *
44 * AAA
45 * +-----+ | +----+
46 * | \ | / |
47 * | BBB +-+ |
48 * | / \ / | |
49 * | CCC JJJ / /
50 * | / \ / /
51 * | DDD EEE / /
52 * | | +-/ /
53 * | FFF /
54 * | / \ /
55 * | GGG HHH /
56 * | | +-/
57 * | III
58 * +--+
59 *
60 *
61 * DFlocal(X) = { Y <- Succ(X) | idom(Y) != X }
62 * DFup(Z) = { Y <- DF(Z) | idom(Y) != X }
63 *
64 *
65 * [] == DFlocal(X) U DF(X)
66 * () == DFup(X)
67 *
68 * Dominator graph of the same nodes.
69 *
70 * AAA AAA: [ ] ()
71 * / \
72 * BBB JJJ BBB: [ JJJ ] ( JJJ ) JJJ: [ ] ()
73 * |
74 * CCC CCC: [ ] ( BBB, JJJ )
75 * / \
76 * DDD EEE DDD: [ ] ( BBB ) EEE: [ JJJ ] ()
77 * |
78 * FFF FFF: [ ] ( BBB )
79 * / \
80 * GGG HHH GGG: [ ] ( BBB ) HHH: [ BBB ] ()
81 * |
82 * III III: [ BBB ] ()
83 *
84 *
85 * BBB and JJJ are definitely the dominance frontier.
86 * Where do I place phi functions and how do I make that decision.
87 *
88 */
89static void die(char *fmt, ...)
90{
91 va_list args;
92
93 va_start(args, fmt);
94 vfprintf(stderr, fmt, args);
95 va_end(args);
96 fflush(stdout);
97 fflush(stderr);
98 exit(1);
99}
100
101#define MALLOC_STRONG_DEBUG
102static void *xmalloc(size_t size, const char *name)
103{
104 void *buf;
105 buf = malloc(size);
106 if (!buf) {
107 die("Cannot malloc %ld bytes to hold %s: %s\n",
108 size + 0UL, name, strerror(errno));
109 }
110 return buf;
111}
112
113static void *xcmalloc(size_t size, const char *name)
114{
115 void *buf;
116 buf = xmalloc(size, name);
117 memset(buf, 0, size);
118 return buf;
119}
120
121static void xfree(const void *ptr)
122{
123 free((void *)ptr);
124}
125
126static char *xstrdup(const char *str)
127{
128 char *new;
129 int len;
130 len = strlen(str);
131 new = xmalloc(len + 1, "xstrdup string");
132 memcpy(new, str, len);
133 new[len] = '\0';
134 return new;
135}
136
137static void xchdir(const char *path)
138{
139 if (chdir(path) != 0) {
140 die("chdir to %s failed: %s\n",
141 path, strerror(errno));
142 }
143}
144
145static int exists(const char *dirname, const char *filename)
146{
147 int does_exist = 1;
148 xchdir(dirname);
149 if (access(filename, O_RDONLY) < 0) {
150 if ((errno != EACCES) && (errno != EROFS)) {
151 does_exist = 0;
152 }
153 }
154 return does_exist;
155}
156
157
158static char *slurp_file(const char *dirname, const char *filename, off_t *r_size)
159{
160 int fd;
161 char *buf;
162 off_t size, progress;
163 ssize_t result;
164 struct stat stats;
165
166 if (!filename) {
167 *r_size = 0;
168 return 0;
169 }
170 xchdir(dirname);
171 fd = open(filename, O_RDONLY);
172 if (fd < 0) {
173 die("Cannot open '%s' : %s\n",
174 filename, strerror(errno));
175 }
176 result = fstat(fd, &stats);
177 if (result < 0) {
178 die("Cannot stat: %s: %s\n",
179 filename, strerror(errno));
180 }
181 size = stats.st_size;
182 *r_size = size +1;
183 buf = xmalloc(size +2, filename);
184 buf[size] = '\n'; /* Make certain the file is newline terminated */
185 buf[size+1] = '\0'; /* Null terminate the file for good measure */
186 progress = 0;
187 while(progress < size) {
188 result = read(fd, buf + progress, size - progress);
189 if (result < 0) {
190 if ((errno == EINTR) || (errno == EAGAIN))
191 continue;
192 die("read on %s of %ld bytes failed: %s\n",
193 filename, (size - progress)+ 0UL, strerror(errno));
194 }
195 progress += result;
196 }
197 result = close(fd);
198 if (result < 0) {
199 die("Close of %s failed: %s\n",
200 filename, strerror(errno));
201 }
202 return buf;
203}
204
205/* Long on the destination platform */
206typedef unsigned long ulong_t;
207typedef long long_t;
208
209struct file_state {
210 struct file_state *prev;
211 const char *basename;
212 char *dirname;
213 char *buf;
214 off_t size;
215 char *pos;
216 int line;
217 char *line_start;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000218 int report_line;
219 const char *report_name;
220 const char *report_dir;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000221};
222struct hash_entry;
223struct token {
224 int tok;
225 struct hash_entry *ident;
226 int str_len;
227 union {
228 ulong_t integer;
229 const char *str;
230 } val;
231};
232
233/* I have two classes of types:
234 * Operational types.
235 * Logical types. (The type the C standard says the operation is of)
236 *
237 * The operational types are:
238 * chars
239 * shorts
240 * ints
241 * longs
242 *
243 * floats
244 * doubles
245 * long doubles
246 *
247 * pointer
248 */
249
250
251/* Machine model.
252 * No memory is useable by the compiler.
253 * There is no floating point support.
254 * All operations take place in general purpose registers.
255 * There is one type of general purpose register.
256 * Unsigned longs are stored in that general purpose register.
257 */
258
259/* Operations on general purpose registers.
260 */
261
262#define OP_SMUL 0
263#define OP_UMUL 1
264#define OP_SDIV 2
265#define OP_UDIV 3
266#define OP_SMOD 4
267#define OP_UMOD 5
268#define OP_ADD 6
269#define OP_SUB 7
Eric Biederman0babc1c2003-05-09 02:39:00 +0000270#define OP_SL 8
Eric Biedermanb138ac82003-04-22 18:44:01 +0000271#define OP_USR 9
272#define OP_SSR 10
273#define OP_AND 11
274#define OP_XOR 12
275#define OP_OR 13
276#define OP_POS 14 /* Dummy positive operator don't use it */
277#define OP_NEG 15
278#define OP_INVERT 16
279
280#define OP_EQ 20
281#define OP_NOTEQ 21
282#define OP_SLESS 22
283#define OP_ULESS 23
284#define OP_SMORE 24
285#define OP_UMORE 25
286#define OP_SLESSEQ 26
287#define OP_ULESSEQ 27
288#define OP_SMOREEQ 28
289#define OP_UMOREEQ 29
290
291#define OP_LFALSE 30 /* Test if the expression is logically false */
292#define OP_LTRUE 31 /* Test if the expression is logcially true */
293
294#define OP_LOAD 32
295#define OP_STORE 33
296
297#define OP_NOOP 34
298
299#define OP_MIN_CONST 50
300#define OP_MAX_CONST 59
301#define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
302#define OP_INTCONST 50
303#define OP_BLOBCONST 51
Eric Biederman0babc1c2003-05-09 02:39:00 +0000304/* For OP_BLOBCONST ->type holds the layout and size
Eric Biedermanb138ac82003-04-22 18:44:01 +0000305 * information. u.blob holds a pointer to the raw binary
306 * data for the constant initializer.
307 */
308#define OP_ADDRCONST 52
Eric Biederman0babc1c2003-05-09 02:39:00 +0000309/* For OP_ADDRCONST ->type holds the type.
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000310 * MISC(0) holds the reference to the static variable.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000311 * ->u.cval holds an offset from that value.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000312 */
313
314#define OP_WRITE 60
315/* OP_WRITE moves one pseudo register to another.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000316 * LHS(0) holds the destination pseudo register, which must be an OP_DECL.
317 * RHS(0) holds the psuedo to move.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000318 */
319
320#define OP_READ 61
321/* OP_READ reads the value of a variable and makes
322 * it available for the pseudo operation.
323 * Useful for things like def-use chains.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000324 * RHS(0) holds points to the triple to read from.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000325 */
326#define OP_COPY 62
Eric Biederman0babc1c2003-05-09 02:39:00 +0000327/* OP_COPY makes a copy of the psedo register or constant in RHS(0).
328 */
329#define OP_PIECE 63
330/* OP_PIECE returns one piece of a instruction that returns a structure.
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000331 * MISC(0) is the instruction
Eric Biederman0babc1c2003-05-09 02:39:00 +0000332 * u.cval is the LHS piece of the instruction to return.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000333 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000334#define OP_ASM 64
335/* OP_ASM holds a sequence of assembly instructions, the result
336 * of a C asm directive.
337 * RHS(x) holds input value x to the assembly sequence.
338 * LHS(x) holds the output value x from the assembly sequence.
339 * u.blob holds the string of assembly instructions.
340 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000341
Eric Biedermanb138ac82003-04-22 18:44:01 +0000342#define OP_DEREF 65
343/* OP_DEREF generates an lvalue from a pointer.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000344 * RHS(0) holds the pointer value.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000345 * OP_DEREF serves as a place holder to indicate all necessary
346 * checks have been done to indicate a value is an lvalue.
347 */
348#define OP_DOT 66
Eric Biederman0babc1c2003-05-09 02:39:00 +0000349/* OP_DOT references a submember of a structure lvalue.
350 * RHS(0) holds the lvalue.
351 * ->u.field holds the name of the field we want.
352 *
353 * Not seen outside of expressions.
354 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000355#define OP_VAL 67
356/* OP_VAL returns the value of a subexpression of the current expression.
357 * Useful for operators that have side effects.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000358 * RHS(0) holds the expression.
359 * MISC(0) holds the subexpression of RHS(0) that is the
Eric Biedermanb138ac82003-04-22 18:44:01 +0000360 * value of the expression.
361 *
362 * Not seen outside of expressions.
363 */
364#define OP_LAND 68
Eric Biederman0babc1c2003-05-09 02:39:00 +0000365/* OP_LAND performs a C logical and between RHS(0) and RHS(1).
Eric Biedermanb138ac82003-04-22 18:44:01 +0000366 * Not seen outside of expressions.
367 */
368#define OP_LOR 69
Eric Biederman0babc1c2003-05-09 02:39:00 +0000369/* OP_LOR performs a C logical or between RHS(0) and RHS(1).
Eric Biedermanb138ac82003-04-22 18:44:01 +0000370 * Not seen outside of expressions.
371 */
372#define OP_COND 70
373/* OP_CODE performas a C ? : operation.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000374 * RHS(0) holds the test.
375 * RHS(1) holds the expression to evaluate if the test returns true.
376 * RHS(2) holds the expression to evaluate if the test returns false.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000377 * Not seen outside of expressions.
378 */
379#define OP_COMMA 71
380/* OP_COMMA performacs a C comma operation.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000381 * That is RHS(0) is evaluated, then RHS(1)
382 * and the value of RHS(1) is returned.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000383 * Not seen outside of expressions.
384 */
385
386#define OP_CALL 72
387/* OP_CALL performs a procedure call.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000388 * MISC(0) holds a pointer to the OP_LIST of a function
389 * RHS(x) holds argument x of a function
390 *
Eric Biedermanb138ac82003-04-22 18:44:01 +0000391 * Currently not seen outside of expressions.
392 */
Eric Biederman0babc1c2003-05-09 02:39:00 +0000393#define OP_VAL_VEC 74
394/* OP_VAL_VEC is an array of triples that are either variable
395 * or values for a structure or an array.
396 * RHS(x) holds element x of the vector.
397 * triple->type->elements holds the size of the vector.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000398 */
399
400/* statements */
401#define OP_LIST 80
402/* OP_LIST Holds a list of statements, and a result value.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000403 * RHS(0) holds the list of statements.
404 * MISC(0) holds the value of the statements.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000405 */
406
407#define OP_BRANCH 81 /* branch */
408/* For branch instructions
Eric Biederman0babc1c2003-05-09 02:39:00 +0000409 * TARG(0) holds the branch target.
410 * RHS(0) if present holds the branch condition.
411 * ->next holds where to branch to if the branch is not taken.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000412 * The branch target can only be a decl...
413 */
414
415#define OP_LABEL 83
416/* OP_LABEL is a triple that establishes an target for branches.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000417 * ->use is the list of all branches that use this label.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000418 */
419
420#define OP_ADECL 84
421/* OP_DECL is a triple that establishes an lvalue for assignments.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000422 * ->use is a list of statements that use the variable.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000423 */
424
425#define OP_SDECL 85
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000426/* OP_SDECL is a triple that establishes a variable of static
Eric Biedermanb138ac82003-04-22 18:44:01 +0000427 * storage duration.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000428 * ->use is a list of statements that use the variable.
429 * MISC(0) holds the initializer expression.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000430 */
431
432
433#define OP_PHI 86
434/* OP_PHI is a triple used in SSA form code.
435 * It is used when multiple code paths merge and a variable needs
436 * a single assignment from any of those code paths.
437 * The operation is a cross between OP_DECL and OP_WRITE, which
438 * is what OP_PHI is geneared from.
439 *
Eric Biederman0babc1c2003-05-09 02:39:00 +0000440 * RHS(x) points to the value from code path x
441 * The number of RHS entries is the number of control paths into the block
Eric Biedermanb138ac82003-04-22 18:44:01 +0000442 * in which OP_PHI resides. The elements of the array point to point
443 * to the variables OP_PHI is derived from.
444 *
Eric Biederman0babc1c2003-05-09 02:39:00 +0000445 * MISC(0) holds a pointer to the orginal OP_DECL node.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000446 */
447
448/* Architecture specific instructions */
449#define OP_CMP 100
450#define OP_TEST 101
451#define OP_SET_EQ 102
452#define OP_SET_NOTEQ 103
453#define OP_SET_SLESS 104
454#define OP_SET_ULESS 105
455#define OP_SET_SMORE 106
456#define OP_SET_UMORE 107
457#define OP_SET_SLESSEQ 108
458#define OP_SET_ULESSEQ 109
459#define OP_SET_SMOREEQ 110
460#define OP_SET_UMOREEQ 111
461
462#define OP_JMP 112
463#define OP_JMP_EQ 113
464#define OP_JMP_NOTEQ 114
465#define OP_JMP_SLESS 115
466#define OP_JMP_ULESS 116
467#define OP_JMP_SMORE 117
468#define OP_JMP_UMORE 118
469#define OP_JMP_SLESSEQ 119
470#define OP_JMP_ULESSEQ 120
471#define OP_JMP_SMOREEQ 121
472#define OP_JMP_UMOREEQ 122
473
474/* Builtin operators that it is just simpler to use the compiler for */
475#define OP_INB 130
476#define OP_INW 131
477#define OP_INL 132
478#define OP_OUTB 133
479#define OP_OUTW 134
480#define OP_OUTL 135
481#define OP_BSF 136
482#define OP_BSR 137
Eric Biedermanb138ac82003-04-22 18:44:01 +0000483#define OP_RDMSR 138
484#define OP_WRMSR 139
Eric Biedermanb138ac82003-04-22 18:44:01 +0000485#define OP_HLT 140
486
Eric Biederman0babc1c2003-05-09 02:39:00 +0000487struct op_info {
488 const char *name;
489 unsigned flags;
490#define PURE 1
491#define IMPURE 2
492#define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
493#define DEF 4
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000494#define BLOCK 8 /* Triple stores the current block */
Eric Biederman0babc1c2003-05-09 02:39:00 +0000495 unsigned char lhs, rhs, misc, targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000496};
497
Eric Biederman0babc1c2003-05-09 02:39:00 +0000498#define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \
499 .name = (NAME), \
500 .flags = (FLAGS), \
501 .lhs = (LHS), \
502 .rhs = (RHS), \
503 .misc = (MISC), \
504 .targ = (TARG), \
505 }
506static const struct op_info table_ops[] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000507[OP_SMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smul"),
508[OP_UMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umul"),
509[OP_SDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sdiv"),
510[OP_UDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "udiv"),
511[OP_SMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smod"),
512[OP_UMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umod"),
513[OP_ADD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "add"),
514[OP_SUB ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sub"),
515[OP_SL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sl"),
516[OP_USR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "usr"),
517[OP_SSR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ssr"),
518[OP_AND ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "and"),
519[OP_XOR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "xor"),
520[OP_OR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "or"),
521[OP_POS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "pos"),
522[OP_NEG ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "neg"),
523[OP_INVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "invert"),
Eric Biedermanb138ac82003-04-22 18:44:01 +0000524
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000525[OP_EQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "eq"),
526[OP_NOTEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "noteq"),
527[OP_SLESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sless"),
528[OP_ULESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "uless"),
529[OP_SMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smore"),
530[OP_UMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umore"),
531[OP_SLESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "slesseq"),
532[OP_ULESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ulesseq"),
533[OP_SMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smoreeq"),
534[OP_UMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umoreeq"),
535[OP_LFALSE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "lfalse"),
536[OP_LTRUE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "ltrue"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000537
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000538[OP_LOAD ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "load"),
539[OP_STORE ] = OP( 1, 1, 0, 0, IMPURE | BLOCK , "store"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000540
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000541[OP_NOOP ] = OP( 0, 0, 0, 0, PURE | BLOCK, "noop"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000542
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000543[OP_INTCONST ] = OP( 0, 0, 0, 0, PURE | DEF, "intconst"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000544[OP_BLOBCONST ] = OP( 0, 0, 0, 0, PURE, "blobconst"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000545[OP_ADDRCONST ] = OP( 0, 0, 1, 0, PURE | DEF, "addrconst"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000546
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000547[OP_WRITE ] = OP( 1, 1, 0, 0, PURE | BLOCK, "write"),
548[OP_READ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "read"),
549[OP_COPY ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "copy"),
550[OP_PIECE ] = OP( 0, 0, 1, 0, PURE | DEF, "piece"),
551[OP_ASM ] = OP(-1, -1, 0, 0, IMPURE, "asm"),
552[OP_DEREF ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "deref"),
553[OP_DOT ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "dot"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000554
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000555[OP_VAL ] = OP( 0, 1, 1, 0, 0 | DEF | BLOCK, "val"),
556[OP_LAND ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "land"),
557[OP_LOR ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "lor"),
558[OP_COND ] = OP( 0, 3, 0, 0, 0 | DEF | BLOCK, "cond"),
559[OP_COMMA ] = OP( 0, 2, 0, 0, 0 | DEF | BLOCK, "comma"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000560/* Call is special most it can stand in for anything so it depends on context */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000561[OP_CALL ] = OP(-1, -1, 1, 0, 0 | BLOCK, "call"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000562/* The sizes of OP_CALL and OP_VAL_VEC depend upon context */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000563[OP_VAL_VEC ] = OP( 0, -1, 0, 0, 0 | BLOCK, "valvec"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000564
565[OP_LIST ] = OP( 0, 1, 1, 0, 0 | DEF, "list"),
566/* The number of targets for OP_BRANCH depends on context */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000567[OP_BRANCH ] = OP( 0, -1, 0, 1, PURE | BLOCK, "branch"),
568[OP_LABEL ] = OP( 0, 0, 0, 0, PURE | BLOCK, "label"),
569[OP_ADECL ] = OP( 0, 0, 0, 0, PURE | BLOCK, "adecl"),
570[OP_SDECL ] = OP( 0, 0, 1, 0, PURE | BLOCK, "sdecl"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000571/* The number of RHS elements of OP_PHI depend upon context */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000572[OP_PHI ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000573
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000574[OP_CMP ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK, "cmp"),
575[OP_TEST ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "test"),
576[OP_SET_EQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_eq"),
577[OP_SET_NOTEQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_noteq"),
578[OP_SET_SLESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_sless"),
579[OP_SET_ULESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_uless"),
580[OP_SET_SMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smore"),
581[OP_SET_UMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umore"),
582[OP_SET_SLESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_slesseq"),
583[OP_SET_ULESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_ulesseq"),
584[OP_SET_SMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smoreq"),
585[OP_SET_UMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umoreq"),
586[OP_JMP ] = OP( 0, 0, 0, 1, PURE | BLOCK, "jmp"),
587[OP_JMP_EQ ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_eq"),
588[OP_JMP_NOTEQ ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_noteq"),
589[OP_JMP_SLESS ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_sless"),
590[OP_JMP_ULESS ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_uless"),
591[OP_JMP_SMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_smore"),
592[OP_JMP_UMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_umore"),
593[OP_JMP_SLESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_slesseq"),
594[OP_JMP_ULESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_ulesseq"),
595[OP_JMP_SMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_smoreq"),
596[OP_JMP_UMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK, "jmp_umoreq"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000597
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000598[OP_INB ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inb"),
599[OP_INW ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inw"),
600[OP_INL ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inl"),
601[OP_OUTB ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outb"),
602[OP_OUTW ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outw"),
603[OP_OUTL ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outl"),
604[OP_BSF ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsf"),
605[OP_BSR ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsr"),
606[OP_RDMSR ] = OP( 2, 1, 0, 0, IMPURE | BLOCK, "__rdmsr"),
607[OP_WRMSR ] = OP( 0, 3, 0, 0, IMPURE | BLOCK, "__wrmsr"),
608[OP_HLT ] = OP( 0, 0, 0, 0, IMPURE | BLOCK, "__hlt"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000609};
610#undef OP
611#define OP_MAX (sizeof(table_ops)/sizeof(table_ops[0]))
Eric Biedermanb138ac82003-04-22 18:44:01 +0000612
613static const char *tops(int index)
614{
615 static const char unknown[] = "unknown op";
616 if (index < 0) {
617 return unknown;
618 }
619 if (index > OP_MAX) {
620 return unknown;
621 }
Eric Biederman0babc1c2003-05-09 02:39:00 +0000622 return table_ops[index].name;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000623}
624
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000625struct asm_info;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000626struct triple;
627struct block;
628struct triple_set {
629 struct triple_set *next;
630 struct triple *member;
631};
632
Eric Biederman0babc1c2003-05-09 02:39:00 +0000633#define MAX_LHS 15
634#define MAX_RHS 15
635#define MAX_MISC 15
636#define MAX_TARG 15
637
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000638struct occurance {
639 int count;
640 const char *filename;
641 const char *function;
642 int line;
643 int col;
644 struct occurance *parent;
645};
Eric Biedermanb138ac82003-04-22 18:44:01 +0000646struct triple {
647 struct triple *next, *prev;
648 struct triple_set *use;
649 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000650 unsigned char op;
651 unsigned char template_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000652 unsigned short sizes;
653#define TRIPLE_LHS(SIZES) (((SIZES) >> 0) & 0x0f)
654#define TRIPLE_RHS(SIZES) (((SIZES) >> 4) & 0x0f)
655#define TRIPLE_MISC(SIZES) (((SIZES) >> 8) & 0x0f)
656#define TRIPLE_TARG(SIZES) (((SIZES) >> 12) & 0x0f)
657#define TRIPLE_SIZE(SIZES) \
658 ((((SIZES) >> 0) & 0x0f) + \
659 (((SIZES) >> 4) & 0x0f) + \
660 (((SIZES) >> 8) & 0x0f) + \
661 (((SIZES) >> 12) & 0x0f))
662#define TRIPLE_SIZES(LHS, RHS, MISC, TARG) \
663 ((((LHS) & 0x0f) << 0) | \
664 (((RHS) & 0x0f) << 4) | \
665 (((MISC) & 0x0f) << 8) | \
666 (((TARG) & 0x0f) << 12))
667#define TRIPLE_LHS_OFF(SIZES) (0)
668#define TRIPLE_RHS_OFF(SIZES) (TRIPLE_LHS_OFF(SIZES) + TRIPLE_LHS(SIZES))
669#define TRIPLE_MISC_OFF(SIZES) (TRIPLE_RHS_OFF(SIZES) + TRIPLE_RHS(SIZES))
670#define TRIPLE_TARG_OFF(SIZES) (TRIPLE_MISC_OFF(SIZES) + TRIPLE_MISC(SIZES))
671#define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF((PTR)->sizes) + (INDEX)])
672#define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF((PTR)->sizes) + (INDEX)])
673#define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF((PTR)->sizes) + (INDEX)])
674#define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF((PTR)->sizes) + (INDEX)])
Eric Biedermanb138ac82003-04-22 18:44:01 +0000675 unsigned id; /* A scratch value and finally the register */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000676#define TRIPLE_FLAG_FLATTENED (1 << 31)
677#define TRIPLE_FLAG_PRE_SPLIT (1 << 30)
678#define TRIPLE_FLAG_POST_SPLIT (1 << 29)
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000679 struct occurance *occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000680 union {
681 ulong_t cval;
682 struct block *block;
683 void *blob;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000684 struct hash_entry *field;
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000685 struct asm_info *ainfo;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000686 } u;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000687 struct triple *param[2];
Eric Biedermanb138ac82003-04-22 18:44:01 +0000688};
689
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000690struct reg_info {
691 unsigned reg;
692 unsigned regcm;
693};
694struct ins_template {
695 struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
696};
697
698struct asm_info {
699 struct ins_template tmpl;
700 char *str;
701};
702
Eric Biedermanb138ac82003-04-22 18:44:01 +0000703struct block_set {
704 struct block_set *next;
705 struct block *member;
706};
707struct block {
708 struct block *work_next;
709 struct block *left, *right;
710 struct triple *first, *last;
711 int users;
712 struct block_set *use;
713 struct block_set *idominates;
714 struct block_set *domfrontier;
715 struct block *idom;
716 struct block_set *ipdominates;
717 struct block_set *ipdomfrontier;
718 struct block *ipdom;
719 int vertex;
720
721};
722
723struct symbol {
724 struct symbol *next;
725 struct hash_entry *ident;
726 struct triple *def;
727 struct type *type;
728 int scope_depth;
729};
730
731struct macro {
732 struct hash_entry *ident;
733 char *buf;
734 int buf_len;
735};
736
737struct hash_entry {
738 struct hash_entry *next;
739 const char *name;
740 int name_len;
741 int tok;
742 struct macro *sym_define;
743 struct symbol *sym_label;
744 struct symbol *sym_struct;
745 struct symbol *sym_ident;
746};
747
748#define HASH_TABLE_SIZE 2048
749
750struct compile_state {
Eric Biederman05f26fc2003-06-11 21:55:00 +0000751 const char *label_prefix;
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000752 const char *ofilename;
753 FILE *output;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000754 struct triple *vars;
755 struct file_state *file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000756 struct occurance *last_occurance;
757 const char *function;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000758 struct token token[4];
759 struct hash_entry *hash_table[HASH_TABLE_SIZE];
760 struct hash_entry *i_continue;
761 struct hash_entry *i_break;
762 int scope_depth;
763 int if_depth, if_value;
764 int macro_line;
765 struct file_state *macro_file;
766 struct triple *main_function;
767 struct block *first_block, *last_block;
768 int last_vertex;
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000769 int cpu;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000770 int debug;
771 int optimize;
772};
773
Eric Biederman0babc1c2003-05-09 02:39:00 +0000774/* visibility global/local */
775/* static/auto duration */
776/* typedef, register, inline */
777#define STOR_SHIFT 0
778#define STOR_MASK 0x000f
779/* Visibility */
780#define STOR_GLOBAL 0x0001
781/* Duration */
782#define STOR_PERM 0x0002
783/* Storage specifiers */
784#define STOR_AUTO 0x0000
785#define STOR_STATIC 0x0002
786#define STOR_EXTERN 0x0003
787#define STOR_REGISTER 0x0004
788#define STOR_TYPEDEF 0x0008
789#define STOR_INLINE 0x000c
790
791#define QUAL_SHIFT 4
792#define QUAL_MASK 0x0070
793#define QUAL_NONE 0x0000
794#define QUAL_CONST 0x0010
795#define QUAL_VOLATILE 0x0020
796#define QUAL_RESTRICT 0x0040
797
798#define TYPE_SHIFT 8
799#define TYPE_MASK 0x1f00
800#define TYPE_INTEGER(TYPE) (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG))
801#define TYPE_ARITHMETIC(TYPE) (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE))
802#define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
803#define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
804#define TYPE_MKUNSIGNED(TYPE) ((TYPE) | 0x0100)
805#define TYPE_RANK(TYPE) ((TYPE) & ~0x0100)
806#define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER)
807#define TYPE_DEFAULT 0x0000
808#define TYPE_VOID 0x0100
809#define TYPE_CHAR 0x0200
810#define TYPE_UCHAR 0x0300
811#define TYPE_SHORT 0x0400
812#define TYPE_USHORT 0x0500
813#define TYPE_INT 0x0600
814#define TYPE_UINT 0x0700
815#define TYPE_LONG 0x0800
816#define TYPE_ULONG 0x0900
817#define TYPE_LLONG 0x0a00 /* long long */
818#define TYPE_ULLONG 0x0b00
819#define TYPE_FLOAT 0x0c00
820#define TYPE_DOUBLE 0x0d00
821#define TYPE_LDOUBLE 0x0e00 /* long double */
822#define TYPE_STRUCT 0x1000
823#define TYPE_ENUM 0x1100
824#define TYPE_POINTER 0x1200
825/* For TYPE_POINTER:
826 * type->left holds the type pointed to.
827 */
828#define TYPE_FUNCTION 0x1300
829/* For TYPE_FUNCTION:
830 * type->left holds the return type.
831 * type->right holds the...
832 */
833#define TYPE_PRODUCT 0x1400
834/* TYPE_PRODUCT is a basic building block when defining structures
835 * type->left holds the type that appears first in memory.
836 * type->right holds the type that appears next in memory.
837 */
838#define TYPE_OVERLAP 0x1500
839/* TYPE_OVERLAP is a basic building block when defining unions
840 * type->left and type->right holds to types that overlap
841 * each other in memory.
842 */
843#define TYPE_ARRAY 0x1600
844/* TYPE_ARRAY is a basic building block when definitng arrays.
845 * type->left holds the type we are an array of.
846 * type-> holds the number of elements.
847 */
848
849#define ELEMENT_COUNT_UNSPECIFIED (~0UL)
850
851struct type {
852 unsigned int type;
853 struct type *left, *right;
854 ulong_t elements;
855 struct hash_entry *field_ident;
856 struct hash_entry *type_ident;
857};
858
Eric Biedermanb138ac82003-04-22 18:44:01 +0000859#define MAX_REGISTERS 75
860#define MAX_REG_EQUIVS 16
Eric Biedermanf96a8102003-06-16 16:57:34 +0000861#define REGISTER_BITS 16
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000862#define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
863#define TEMPLATE_BITS 6
864#define MAX_TEMPLATES (1<<TEMPLATE_BITS)
Eric Biedermanb138ac82003-04-22 18:44:01 +0000865#define MAX_REGC 12
866#define REG_UNSET 0
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000867#define REG_UNNEEDED 1
868#define REG_VIRT0 (MAX_REGISTERS + 0)
869#define REG_VIRT1 (MAX_REGISTERS + 1)
870#define REG_VIRT2 (MAX_REGISTERS + 2)
871#define REG_VIRT3 (MAX_REGISTERS + 3)
872#define REG_VIRT4 (MAX_REGISTERS + 4)
873#define REG_VIRT5 (MAX_REGISTERS + 5)
Eric Biederman8d9c1232003-06-17 08:42:17 +0000874#define REG_VIRT6 (MAX_REGISTERS + 5)
875#define REG_VIRT7 (MAX_REGISTERS + 5)
876#define REG_VIRT8 (MAX_REGISTERS + 5)
877#define REG_VIRT9 (MAX_REGISTERS + 5)
Eric Biedermanb138ac82003-04-22 18:44:01 +0000878
879/* Provision for 8 register classes */
Eric Biedermanf96a8102003-06-16 16:57:34 +0000880#define REG_SHIFT 0
881#define REGC_SHIFT REGISTER_BITS
882#define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000883#define REG_MASK (MAX_VIRT_REGISTERS -1)
884#define ID_REG(ID) ((ID) & REG_MASK)
885#define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
Eric Biedermanf96a8102003-06-16 16:57:34 +0000886#define ID_REGCM(ID) (((ID) & REGC_MASK) >> REGC_SHIFT)
887#define SET_REGCM(ID, REGCM) ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
888#define SET_INFO(ID, INFO) ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
889 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
Eric Biedermanb138ac82003-04-22 18:44:01 +0000890
891static unsigned arch_reg_regcm(struct compile_state *state, int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000892static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000893static void arch_reg_equivs(
894 struct compile_state *state, unsigned *equiv, int reg);
895static int arch_select_free_register(
896 struct compile_state *state, char *used, int classes);
897static unsigned arch_regc_size(struct compile_state *state, int class);
898static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
899static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
900static const char *arch_reg_str(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000901static struct reg_info arch_reg_constraint(
902 struct compile_state *state, struct type *type, const char *constraint);
903static struct reg_info arch_reg_clobber(
904 struct compile_state *state, const char *clobber);
905static struct reg_info arch_reg_lhs(struct compile_state *state,
906 struct triple *ins, int index);
907static struct reg_info arch_reg_rhs(struct compile_state *state,
908 struct triple *ins, int index);
909static struct triple *transform_to_arch_instruction(
910 struct compile_state *state, struct triple *ins);
911
912
Eric Biedermanb138ac82003-04-22 18:44:01 +0000913
Eric Biederman0babc1c2003-05-09 02:39:00 +0000914#define DEBUG_ABORT_ON_ERROR 0x0001
915#define DEBUG_INTERMEDIATE_CODE 0x0002
916#define DEBUG_CONTROL_FLOW 0x0004
917#define DEBUG_BASIC_BLOCKS 0x0008
918#define DEBUG_FDOMINATORS 0x0010
919#define DEBUG_RDOMINATORS 0x0020
920#define DEBUG_TRIPLES 0x0040
921#define DEBUG_INTERFERENCE 0x0080
922#define DEBUG_ARCH_CODE 0x0100
923#define DEBUG_CODE_ELIMINATION 0x0200
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000924#define DEBUG_INSERTED_COPIES 0x0400
Eric Biedermanb138ac82003-04-22 18:44:01 +0000925
Eric Biederman153ea352003-06-20 14:43:20 +0000926#define GLOBAL_SCOPE_DEPTH 1
927#define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
Eric Biedermanb138ac82003-04-22 18:44:01 +0000928
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000929static void compile_file(struct compile_state *old_state, const char *filename, int local);
930
931static void do_cleanup(struct compile_state *state)
932{
933 if (state->output) {
934 fclose(state->output);
935 unlink(state->ofilename);
936 }
937}
Eric Biedermanb138ac82003-04-22 18:44:01 +0000938
939static int get_col(struct file_state *file)
940{
941 int col;
942 char *ptr, *end;
943 ptr = file->line_start;
944 end = file->pos;
945 for(col = 0; ptr < end; ptr++) {
946 if (*ptr != '\t') {
947 col++;
948 }
949 else {
950 col = (col & ~7) + 8;
951 }
952 }
953 return col;
954}
955
956static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
957{
958 int col;
959 if (triple) {
960 fprintf(fp, "%s:%d.%d: ",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000961 triple->occurance->filename,
962 triple->occurance->line,
963 triple->occurance->col);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000964 return;
965 }
966 if (!state->file) {
967 return;
968 }
969 col = get_col(state->file);
970 fprintf(fp, "%s:%d.%d: ",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000971 state->file->report_name, state->file->report_line, col);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000972}
973
974static void __internal_error(struct compile_state *state, struct triple *ptr,
975 char *fmt, ...)
976{
977 va_list args;
978 va_start(args, fmt);
979 loc(stderr, state, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000980 if (ptr) {
981 fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
982 }
Eric Biedermanb138ac82003-04-22 18:44:01 +0000983 fprintf(stderr, "Internal compiler error: ");
984 vfprintf(stderr, fmt, args);
985 fprintf(stderr, "\n");
986 va_end(args);
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000987 do_cleanup(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000988 abort();
989}
990
991
992static void __internal_warning(struct compile_state *state, struct triple *ptr,
993 char *fmt, ...)
994{
995 va_list args;
996 va_start(args, fmt);
997 loc(stderr, state, ptr);
998 fprintf(stderr, "Internal compiler warning: ");
999 vfprintf(stderr, fmt, args);
1000 fprintf(stderr, "\n");
1001 va_end(args);
1002}
1003
1004
1005
1006static void __error(struct compile_state *state, struct triple *ptr,
1007 char *fmt, ...)
1008{
1009 va_list args;
1010 va_start(args, fmt);
1011 loc(stderr, state, ptr);
1012 vfprintf(stderr, fmt, args);
1013 va_end(args);
1014 fprintf(stderr, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001015 do_cleanup(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001016 if (state->debug & DEBUG_ABORT_ON_ERROR) {
1017 abort();
1018 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001019 exit(1);
1020}
1021
1022static void __warning(struct compile_state *state, struct triple *ptr,
1023 char *fmt, ...)
1024{
1025 va_list args;
1026 va_start(args, fmt);
1027 loc(stderr, state, ptr);
1028 fprintf(stderr, "warning: ");
1029 vfprintf(stderr, fmt, args);
1030 fprintf(stderr, "\n");
1031 va_end(args);
1032}
1033
1034#if DEBUG_ERROR_MESSAGES
1035# define internal_error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_error
1036# define internal_warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_warning
1037# define error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__error
1038# define warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__warning
1039#else
1040# define internal_error __internal_error
1041# define internal_warning __internal_warning
1042# define error __error
1043# define warning __warning
1044#endif
1045#define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1046
Eric Biederman0babc1c2003-05-09 02:39:00 +00001047static void valid_op(struct compile_state *state, int op)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001048{
1049 char *fmt = "invalid op: %d";
Eric Biederman0babc1c2003-05-09 02:39:00 +00001050 if (op >= OP_MAX) {
1051 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001052 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001053 if (op < 0) {
1054 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001055 }
1056}
1057
Eric Biederman0babc1c2003-05-09 02:39:00 +00001058static void valid_ins(struct compile_state *state, struct triple *ptr)
1059{
1060 valid_op(state, ptr->op);
1061}
1062
Eric Biedermanb138ac82003-04-22 18:44:01 +00001063static void process_trigraphs(struct compile_state *state)
1064{
1065 char *src, *dest, *end;
1066 struct file_state *file;
1067 file = state->file;
1068 src = dest = file->buf;
1069 end = file->buf + file->size;
1070 while((end - src) >= 3) {
1071 if ((src[0] == '?') && (src[1] == '?')) {
1072 int c = -1;
1073 switch(src[2]) {
1074 case '=': c = '#'; break;
1075 case '/': c = '\\'; break;
1076 case '\'': c = '^'; break;
1077 case '(': c = '['; break;
1078 case ')': c = ']'; break;
1079 case '!': c = '!'; break;
1080 case '<': c = '{'; break;
1081 case '>': c = '}'; break;
1082 case '-': c = '~'; break;
1083 }
1084 if (c != -1) {
1085 *dest++ = c;
1086 src += 3;
1087 }
1088 else {
1089 *dest++ = *src++;
1090 }
1091 }
1092 else {
1093 *dest++ = *src++;
1094 }
1095 }
1096 while(src != end) {
1097 *dest++ = *src++;
1098 }
1099 file->size = dest - file->buf;
1100}
1101
1102static void splice_lines(struct compile_state *state)
1103{
1104 char *src, *dest, *end;
1105 struct file_state *file;
1106 file = state->file;
1107 src = dest = file->buf;
1108 end = file->buf + file->size;
1109 while((end - src) >= 2) {
1110 if ((src[0] == '\\') && (src[1] == '\n')) {
1111 src += 2;
1112 }
1113 else {
1114 *dest++ = *src++;
1115 }
1116 }
1117 while(src != end) {
1118 *dest++ = *src++;
1119 }
1120 file->size = dest - file->buf;
1121}
1122
1123static struct type void_type;
1124static void use_triple(struct triple *used, struct triple *user)
1125{
1126 struct triple_set **ptr, *new;
1127 if (!used)
1128 return;
1129 if (!user)
1130 return;
1131 ptr = &used->use;
1132 while(*ptr) {
1133 if ((*ptr)->member == user) {
1134 return;
1135 }
1136 ptr = &(*ptr)->next;
1137 }
1138 /* Append new to the head of the list,
1139 * copy_func and rename_block_variables
1140 * depends on this.
1141 */
1142 new = xcmalloc(sizeof(*new), "triple_set");
1143 new->member = user;
1144 new->next = used->use;
1145 used->use = new;
1146}
1147
1148static void unuse_triple(struct triple *used, struct triple *unuser)
1149{
1150 struct triple_set *use, **ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001151 if (!used) {
1152 return;
1153 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001154 ptr = &used->use;
1155 while(*ptr) {
1156 use = *ptr;
1157 if (use->member == unuser) {
1158 *ptr = use->next;
1159 xfree(use);
1160 }
1161 else {
1162 ptr = &use->next;
1163 }
1164 }
1165}
1166
1167static void push_triple(struct triple *used, struct triple *user)
1168{
1169 struct triple_set *new;
1170 if (!used)
1171 return;
1172 if (!user)
1173 return;
1174 /* Append new to the head of the list,
1175 * it's the only sensible behavoir for a stack.
1176 */
1177 new = xcmalloc(sizeof(*new), "triple_set");
1178 new->member = user;
1179 new->next = used->use;
1180 used->use = new;
1181}
1182
1183static void pop_triple(struct triple *used, struct triple *unuser)
1184{
1185 struct triple_set *use, **ptr;
1186 ptr = &used->use;
1187 while(*ptr) {
1188 use = *ptr;
1189 if (use->member == unuser) {
1190 *ptr = use->next;
1191 xfree(use);
1192 /* Only free one occurance from the stack */
1193 return;
1194 }
1195 else {
1196 ptr = &use->next;
1197 }
1198 }
1199}
1200
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001201static void put_occurance(struct occurance *occurance)
1202{
1203 occurance->count -= 1;
1204 if (occurance->count <= 0) {
1205 if (occurance->parent) {
1206 put_occurance(occurance->parent);
1207 }
1208 xfree(occurance);
1209 }
1210}
1211
1212static void get_occurance(struct occurance *occurance)
1213{
1214 occurance->count += 1;
1215}
1216
1217
1218static struct occurance *new_occurance(struct compile_state *state)
1219{
1220 struct occurance *result, *last;
1221 const char *filename;
1222 const char *function;
1223 int line, col;
1224
1225 function = "";
1226 filename = 0;
1227 line = 0;
1228 col = 0;
1229 if (state->file) {
1230 filename = state->file->report_name;
1231 line = state->file->report_line;
1232 col = get_col(state->file);
1233 }
1234 if (state->function) {
1235 function = state->function;
1236 }
1237 last = state->last_occurance;
1238 if (last &&
1239 (last->col == col) &&
1240 (last->line == line) &&
1241 (last->function == function) &&
1242 (strcmp(last->filename, filename) == 0)) {
1243 get_occurance(last);
1244 return last;
1245 }
1246 if (last) {
1247 state->last_occurance = 0;
1248 put_occurance(last);
1249 }
1250 result = xmalloc(sizeof(*result), "occurance");
1251 result->count = 2;
1252 result->filename = filename;
1253 result->function = function;
1254 result->line = line;
1255 result->col = col;
1256 result->parent = 0;
1257 state->last_occurance = result;
1258 return result;
1259}
1260
1261static struct occurance *inline_occurance(struct compile_state *state,
1262 struct occurance *new, struct occurance *orig)
1263{
1264 struct occurance *result, *last;
1265 last = state->last_occurance;
1266 if (last &&
1267 (last->parent == orig) &&
1268 (last->col == new->col) &&
1269 (last->line == new->line) &&
1270 (last->function == new->function) &&
1271 (last->filename == new->filename)) {
1272 get_occurance(last);
1273 return last;
1274 }
1275 if (last) {
1276 state->last_occurance = 0;
1277 put_occurance(last);
1278 }
1279 get_occurance(orig);
1280 result = xmalloc(sizeof(*result), "occurance");
1281 result->count = 2;
1282 result->filename = new->filename;
1283 result->function = new->function;
1284 result->line = new->line;
1285 result->col = new->col;
1286 result->parent = orig;
1287 state->last_occurance = result;
1288 return result;
1289}
1290
1291
1292static struct occurance dummy_occurance = {
1293 .count = 2,
1294 .filename = __FILE__,
1295 .function = "",
1296 .line = __LINE__,
1297 .col = 0,
1298 .parent = 0,
1299};
Eric Biedermanb138ac82003-04-22 18:44:01 +00001300
1301/* The zero triple is used as a place holder when we are removing pointers
1302 * from a triple. Having allows certain sanity checks to pass even
1303 * when the original triple that was pointed to is gone.
1304 */
1305static struct triple zero_triple = {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001306 .next = &zero_triple,
1307 .prev = &zero_triple,
1308 .use = 0,
1309 .op = OP_INTCONST,
1310 .sizes = TRIPLE_SIZES(0, 0, 0, 0),
1311 .id = -1, /* An invalid id */
Eric Biedermanb138ac82003-04-22 18:44:01 +00001312 .u = { .cval = 0, },
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001313 .occurance = &dummy_occurance,
Eric Biederman0babc1c2003-05-09 02:39:00 +00001314 .param { [0] = 0, [1] = 0, },
Eric Biedermanb138ac82003-04-22 18:44:01 +00001315};
1316
Eric Biederman0babc1c2003-05-09 02:39:00 +00001317
1318static unsigned short triple_sizes(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001319 int op, struct type *type, int lhs_wanted, int rhs_wanted)
Eric Biederman0babc1c2003-05-09 02:39:00 +00001320{
1321 int lhs, rhs, misc, targ;
1322 valid_op(state, op);
1323 lhs = table_ops[op].lhs;
1324 rhs = table_ops[op].rhs;
1325 misc = table_ops[op].misc;
1326 targ = table_ops[op].targ;
1327
1328
1329 if (op == OP_CALL) {
1330 struct type *param;
1331 rhs = 0;
1332 param = type->right;
1333 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
1334 rhs++;
1335 param = param->right;
1336 }
1337 if ((param->type & TYPE_MASK) != TYPE_VOID) {
1338 rhs++;
1339 }
1340 lhs = 0;
1341 if ((type->left->type & TYPE_MASK) == TYPE_STRUCT) {
1342 lhs = type->left->elements;
1343 }
1344 }
1345 else if (op == OP_VAL_VEC) {
1346 rhs = type->elements;
1347 }
1348 else if ((op == OP_BRANCH) || (op == OP_PHI)) {
1349 rhs = rhs_wanted;
1350 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001351 else if (op == OP_ASM) {
1352 rhs = rhs_wanted;
1353 lhs = lhs_wanted;
1354 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001355 if ((rhs < 0) || (rhs > MAX_RHS)) {
1356 internal_error(state, 0, "bad rhs");
1357 }
1358 if ((lhs < 0) || (lhs > MAX_LHS)) {
1359 internal_error(state, 0, "bad lhs");
1360 }
1361 if ((misc < 0) || (misc > MAX_MISC)) {
1362 internal_error(state, 0, "bad misc");
1363 }
1364 if ((targ < 0) || (targ > MAX_TARG)) {
1365 internal_error(state, 0, "bad targs");
1366 }
1367 return TRIPLE_SIZES(lhs, rhs, misc, targ);
1368}
1369
1370static struct triple *alloc_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001371 int op, struct type *type, int lhs, int rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001372 struct occurance *occurance)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001373{
Eric Biederman0babc1c2003-05-09 02:39:00 +00001374 size_t size, sizes, extra_count, min_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001375 struct triple *ret;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001376 sizes = triple_sizes(state, op, type, lhs, rhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001377
1378 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
1379 extra_count = TRIPLE_SIZE(sizes);
1380 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
1381
1382 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
1383 ret = xcmalloc(size, "tripple");
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001384 ret->op = op;
1385 ret->sizes = sizes;
1386 ret->type = type;
1387 ret->next = ret;
1388 ret->prev = ret;
1389 ret->occurance = occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001390 return ret;
1391}
1392
Eric Biederman0babc1c2003-05-09 02:39:00 +00001393struct triple *dup_triple(struct compile_state *state, struct triple *src)
1394{
1395 struct triple *dup;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001396 int src_lhs, src_rhs, src_size;
1397 src_lhs = TRIPLE_LHS(src->sizes);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001398 src_rhs = TRIPLE_RHS(src->sizes);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001399 src_size = TRIPLE_SIZE(src->sizes);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001400 get_occurance(src->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001401 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001402 src->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001403 memcpy(dup, src, sizeof(*src));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001404 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
Eric Biederman0babc1c2003-05-09 02:39:00 +00001405 return dup;
1406}
1407
1408static struct triple *new_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001409 int op, struct type *type, int lhs, int rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001410{
1411 struct triple *ret;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001412 struct occurance *occurance;
1413 occurance = new_occurance(state);
1414 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001415 return ret;
1416}
1417
1418static struct triple *build_triple(struct compile_state *state,
1419 int op, struct type *type, struct triple *left, struct triple *right,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001420 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00001421{
1422 struct triple *ret;
1423 size_t count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001424 ret = alloc_triple(state, op, type, -1, -1, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001425 count = TRIPLE_SIZE(ret->sizes);
1426 if (count > 0) {
1427 ret->param[0] = left;
1428 }
1429 if (count > 1) {
1430 ret->param[1] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001431 }
1432 return ret;
1433}
1434
Eric Biederman0babc1c2003-05-09 02:39:00 +00001435static struct triple *triple(struct compile_state *state,
1436 int op, struct type *type, struct triple *left, struct triple *right)
1437{
1438 struct triple *ret;
1439 size_t count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001440 ret = new_triple(state, op, type, -1, -1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001441 count = TRIPLE_SIZE(ret->sizes);
1442 if (count >= 1) {
1443 ret->param[0] = left;
1444 }
1445 if (count >= 2) {
1446 ret->param[1] = right;
1447 }
1448 return ret;
1449}
1450
1451static struct triple *branch(struct compile_state *state,
1452 struct triple *targ, struct triple *test)
1453{
1454 struct triple *ret;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001455 ret = new_triple(state, OP_BRANCH, &void_type, -1, test?1:0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001456 if (test) {
1457 RHS(ret, 0) = test;
1458 }
1459 TARG(ret, 0) = targ;
1460 /* record the branch target was used */
1461 if (!targ || (targ->op != OP_LABEL)) {
1462 internal_error(state, 0, "branch not to label");
1463 use_triple(targ, ret);
1464 }
1465 return ret;
1466}
1467
1468
Eric Biedermanb138ac82003-04-22 18:44:01 +00001469static void insert_triple(struct compile_state *state,
1470 struct triple *first, struct triple *ptr)
1471{
1472 if (ptr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001473 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00001474 internal_error(state, ptr, "expression already used");
1475 }
1476 ptr->next = first;
1477 ptr->prev = first->prev;
1478 ptr->prev->next = ptr;
1479 ptr->next->prev = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00001480 if ((ptr->prev->op == OP_BRANCH) &&
1481 TRIPLE_RHS(ptr->prev->sizes)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00001482 unuse_triple(first, ptr->prev);
1483 use_triple(ptr, ptr->prev);
1484 }
1485 }
1486}
1487
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001488static int triple_stores_block(struct compile_state *state, struct triple *ins)
1489{
1490 /* This function is used to determine if u.block
1491 * is utilized to store the current block number.
1492 */
1493 int stores_block;
1494 valid_ins(state, ins);
1495 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
1496 return stores_block;
1497}
1498
1499static struct block *block_of_triple(struct compile_state *state,
1500 struct triple *ins)
1501{
1502 struct triple *first;
1503 first = RHS(state->main_function, 0);
1504 while(ins != first && !triple_stores_block(state, ins)) {
1505 if (ins == ins->prev) {
1506 internal_error(state, 0, "ins == ins->prev?");
1507 }
1508 ins = ins->prev;
1509 }
1510 if (!triple_stores_block(state, ins)) {
1511 internal_error(state, ins, "Cannot find block");
1512 }
1513 return ins->u.block;
1514}
1515
Eric Biedermanb138ac82003-04-22 18:44:01 +00001516static struct triple *pre_triple(struct compile_state *state,
1517 struct triple *base,
1518 int op, struct type *type, struct triple *left, struct triple *right)
1519{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001520 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001521 struct triple *ret;
Eric Biedermand3283ec2003-06-18 11:03:18 +00001522 /* If I am an OP_PIECE jump to the real instruction */
1523 if (base->op == OP_PIECE) {
1524 base = MISC(base, 0);
1525 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001526 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001527 get_occurance(base->occurance);
1528 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001529 if (triple_stores_block(state, ret)) {
1530 ret->u.block = block;
1531 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001532 insert_triple(state, base, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001533 if (block->first == base) {
1534 block->first = ret;
1535 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001536 return ret;
1537}
1538
1539static struct triple *post_triple(struct compile_state *state,
1540 struct triple *base,
1541 int op, struct type *type, struct triple *left, struct triple *right)
1542{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001543 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001544 struct triple *ret;
Eric Biedermand3283ec2003-06-18 11:03:18 +00001545 int zlhs;
1546 /* If I am an OP_PIECE jump to the real instruction */
1547 if (base->op == OP_PIECE) {
1548 base = MISC(base, 0);
1549 }
1550 /* If I have a left hand side skip over it */
1551 zlhs = TRIPLE_LHS(base->sizes);
1552 if (zlhs && (base->op != OP_WRITE) && (base->op != OP_STORE)) {
1553 base = LHS(base, zlhs - 1);
1554 }
1555
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001556 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001557 get_occurance(base->occurance);
1558 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001559 if (triple_stores_block(state, ret)) {
1560 ret->u.block = block;
1561 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001562 insert_triple(state, base->next, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001563 if (block->last == base) {
1564 block->last = ret;
1565 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001566 return ret;
1567}
1568
1569static struct triple *label(struct compile_state *state)
1570{
1571 /* Labels don't get a type */
1572 struct triple *result;
1573 result = triple(state, OP_LABEL, &void_type, 0, 0);
1574 return result;
1575}
1576
Eric Biederman0babc1c2003-05-09 02:39:00 +00001577static void display_triple(FILE *fp, struct triple *ins)
1578{
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001579 struct occurance *ptr;
1580 const char *reg;
1581 char pre, post;
1582 pre = post = ' ';
1583 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
1584 pre = '^';
1585 }
1586 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
1587 post = 'v';
1588 }
1589 reg = arch_reg_str(ID_REG(ins->id));
Eric Biederman0babc1c2003-05-09 02:39:00 +00001590 if (ins->op == OP_INTCONST) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001591 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s <0x%08lx> ",
1592 ins, pre, post, reg, ins->template_id, tops(ins->op),
1593 ins->u.cval);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001594 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001595 else if (ins->op == OP_ADDRCONST) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001596 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s %-10p <0x%08lx>",
1597 ins, pre, post, reg, ins->template_id, tops(ins->op),
1598 MISC(ins, 0), ins->u.cval);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001599 }
1600 else {
1601 int i, count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001602 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s",
1603 ins, pre, post, reg, ins->template_id, tops(ins->op));
Eric Biederman0babc1c2003-05-09 02:39:00 +00001604 count = TRIPLE_SIZE(ins->sizes);
1605 for(i = 0; i < count; i++) {
1606 fprintf(fp, " %-10p", ins->param[i]);
1607 }
1608 for(; i < 2; i++) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00001609 fprintf(fp, " ");
Eric Biederman0babc1c2003-05-09 02:39:00 +00001610 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001611 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001612 fprintf(fp, " @");
1613 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
1614 fprintf(fp, " %s,%s:%d.%d",
1615 ptr->function,
1616 ptr->filename,
1617 ptr->line,
1618 ptr->col);
1619 }
1620 fprintf(fp, "\n");
Eric Biederman0babc1c2003-05-09 02:39:00 +00001621 fflush(fp);
1622}
1623
Eric Biedermanb138ac82003-04-22 18:44:01 +00001624static int triple_is_pure(struct compile_state *state, struct triple *ins)
1625{
1626 /* Does the triple have no side effects.
1627 * I.e. Rexecuting the triple with the same arguments
1628 * gives the same value.
1629 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001630 unsigned pure;
1631 valid_ins(state, ins);
1632 pure = PURE_BITS(table_ops[ins->op].flags);
1633 if ((pure != PURE) && (pure != IMPURE)) {
1634 internal_error(state, 0, "Purity of %s not known\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +00001635 tops(ins->op));
Eric Biedermanb138ac82003-04-22 18:44:01 +00001636 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001637 return pure == PURE;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001638}
1639
Eric Biederman0babc1c2003-05-09 02:39:00 +00001640static int triple_is_branch(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001641{
1642 /* This function is used to determine which triples need
1643 * a register.
1644 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001645 int is_branch;
1646 valid_ins(state, ins);
1647 is_branch = (table_ops[ins->op].targ != 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001648 return is_branch;
1649}
1650
Eric Biederman0babc1c2003-05-09 02:39:00 +00001651static int triple_is_def(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001652{
1653 /* This function is used to determine which triples need
1654 * a register.
1655 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001656 int is_def;
1657 valid_ins(state, ins);
1658 is_def = (table_ops[ins->op].flags & DEF) == DEF;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001659 return is_def;
1660}
1661
Eric Biederman0babc1c2003-05-09 02:39:00 +00001662static struct triple **triple_iter(struct compile_state *state,
1663 size_t count, struct triple **vector,
1664 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001665{
1666 struct triple **ret;
1667 ret = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00001668 if (count) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00001669 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001670 ret = vector;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001671 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001672 else if ((last >= vector) && (last < (vector + count - 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00001673 ret = last + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001674 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001675 }
1676 return ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00001677
Eric Biedermanb138ac82003-04-22 18:44:01 +00001678}
1679
1680static struct triple **triple_lhs(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00001681 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001682{
Eric Biederman0babc1c2003-05-09 02:39:00 +00001683 return triple_iter(state, TRIPLE_LHS(ins->sizes), &LHS(ins,0),
1684 ins, last);
1685}
1686
1687static struct triple **triple_rhs(struct compile_state *state,
1688 struct triple *ins, struct triple **last)
1689{
1690 return triple_iter(state, TRIPLE_RHS(ins->sizes), &RHS(ins,0),
1691 ins, last);
1692}
1693
1694static struct triple **triple_misc(struct compile_state *state,
1695 struct triple *ins, struct triple **last)
1696{
1697 return triple_iter(state, TRIPLE_MISC(ins->sizes), &MISC(ins,0),
1698 ins, last);
1699}
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001700
Eric Biederman0babc1c2003-05-09 02:39:00 +00001701static struct triple **triple_targ(struct compile_state *state,
1702 struct triple *ins, struct triple **last)
1703{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001704 size_t count;
1705 struct triple **ret, **vector;
1706 ret = 0;
1707 count = TRIPLE_TARG(ins->sizes);
1708 vector = &TARG(ins, 0);
1709 if (count) {
1710 if (!last) {
1711 ret = vector;
1712 }
1713 else if ((last >= vector) && (last < (vector + count - 1))) {
1714 ret = last + 1;
1715 }
1716 else if ((last == (vector + count - 1)) &&
1717 TRIPLE_RHS(ins->sizes)) {
1718 ret = &ins->next;
1719 }
1720 }
1721 return ret;
1722}
1723
1724
1725static void verify_use(struct compile_state *state,
1726 struct triple *user, struct triple *used)
1727{
1728 int size, i;
1729 size = TRIPLE_SIZE(user->sizes);
1730 for(i = 0; i < size; i++) {
1731 if (user->param[i] == used) {
1732 break;
1733 }
1734 }
1735 if (triple_is_branch(state, user)) {
1736 if (user->next == used) {
1737 i = -1;
1738 }
1739 }
1740 if (i == size) {
1741 internal_error(state, user, "%s(%p) does not use %s(%p)",
1742 tops(user->op), user, tops(used->op), used);
1743 }
1744}
1745
1746static int find_rhs_use(struct compile_state *state,
1747 struct triple *user, struct triple *used)
1748{
1749 struct triple **param;
1750 int size, i;
1751 verify_use(state, user, used);
1752 size = TRIPLE_RHS(user->sizes);
1753 param = &RHS(user, 0);
1754 for(i = 0; i < size; i++) {
1755 if (param[i] == used) {
1756 return i;
1757 }
1758 }
1759 return -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001760}
1761
1762static void free_triple(struct compile_state *state, struct triple *ptr)
1763{
Eric Biederman0babc1c2003-05-09 02:39:00 +00001764 size_t size;
1765 size = sizeof(*ptr) - sizeof(ptr->param) +
1766 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr->sizes));
Eric Biedermanb138ac82003-04-22 18:44:01 +00001767 ptr->prev->next = ptr->next;
1768 ptr->next->prev = ptr->prev;
1769 if (ptr->use) {
1770 internal_error(state, ptr, "ptr->use != 0");
1771 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001772 put_occurance(ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001773 memset(ptr, -1, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001774 xfree(ptr);
1775}
1776
1777static void release_triple(struct compile_state *state, struct triple *ptr)
1778{
1779 struct triple_set *set, *next;
1780 struct triple **expr;
1781 /* Remove ptr from use chains where it is the user */
1782 expr = triple_rhs(state, ptr, 0);
1783 for(; expr; expr = triple_rhs(state, ptr, expr)) {
1784 if (*expr) {
1785 unuse_triple(*expr, ptr);
1786 }
1787 }
1788 expr = triple_lhs(state, ptr, 0);
1789 for(; expr; expr = triple_lhs(state, ptr, expr)) {
1790 if (*expr) {
1791 unuse_triple(*expr, ptr);
1792 }
1793 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001794 expr = triple_misc(state, ptr, 0);
1795 for(; expr; expr = triple_misc(state, ptr, expr)) {
1796 if (*expr) {
1797 unuse_triple(*expr, ptr);
1798 }
1799 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001800 expr = triple_targ(state, ptr, 0);
1801 for(; expr; expr = triple_targ(state, ptr, expr)) {
1802 if (*expr) {
1803 unuse_triple(*expr, ptr);
1804 }
1805 }
1806 /* Reomve ptr from use chains where it is used */
1807 for(set = ptr->use; set; set = next) {
1808 next = set->next;
1809 expr = triple_rhs(state, set->member, 0);
1810 for(; expr; expr = triple_rhs(state, set->member, expr)) {
1811 if (*expr == ptr) {
1812 *expr = &zero_triple;
1813 }
1814 }
1815 expr = triple_lhs(state, set->member, 0);
1816 for(; expr; expr = triple_lhs(state, set->member, expr)) {
1817 if (*expr == ptr) {
1818 *expr = &zero_triple;
1819 }
1820 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001821 expr = triple_misc(state, set->member, 0);
1822 for(; expr; expr = triple_misc(state, set->member, expr)) {
1823 if (*expr == ptr) {
1824 *expr = &zero_triple;
1825 }
1826 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001827 expr = triple_targ(state, set->member, 0);
1828 for(; expr; expr = triple_targ(state, set->member, expr)) {
1829 if (*expr == ptr) {
1830 *expr = &zero_triple;
1831 }
1832 }
1833 unuse_triple(ptr, set->member);
1834 }
1835 free_triple(state, ptr);
1836}
1837
1838static void print_triple(struct compile_state *state, struct triple *ptr);
1839
1840#define TOK_UNKNOWN 0
1841#define TOK_SPACE 1
1842#define TOK_SEMI 2
1843#define TOK_LBRACE 3
1844#define TOK_RBRACE 4
1845#define TOK_COMMA 5
1846#define TOK_EQ 6
1847#define TOK_COLON 7
1848#define TOK_LBRACKET 8
1849#define TOK_RBRACKET 9
1850#define TOK_LPAREN 10
1851#define TOK_RPAREN 11
1852#define TOK_STAR 12
1853#define TOK_DOTS 13
1854#define TOK_MORE 14
1855#define TOK_LESS 15
1856#define TOK_TIMESEQ 16
1857#define TOK_DIVEQ 17
1858#define TOK_MODEQ 18
1859#define TOK_PLUSEQ 19
1860#define TOK_MINUSEQ 20
1861#define TOK_SLEQ 21
1862#define TOK_SREQ 22
1863#define TOK_ANDEQ 23
1864#define TOK_XOREQ 24
1865#define TOK_OREQ 25
1866#define TOK_EQEQ 26
1867#define TOK_NOTEQ 27
1868#define TOK_QUEST 28
1869#define TOK_LOGOR 29
1870#define TOK_LOGAND 30
1871#define TOK_OR 31
1872#define TOK_AND 32
1873#define TOK_XOR 33
1874#define TOK_LESSEQ 34
1875#define TOK_MOREEQ 35
1876#define TOK_SL 36
1877#define TOK_SR 37
1878#define TOK_PLUS 38
1879#define TOK_MINUS 39
1880#define TOK_DIV 40
1881#define TOK_MOD 41
1882#define TOK_PLUSPLUS 42
1883#define TOK_MINUSMINUS 43
1884#define TOK_BANG 44
1885#define TOK_ARROW 45
1886#define TOK_DOT 46
1887#define TOK_TILDE 47
1888#define TOK_LIT_STRING 48
1889#define TOK_LIT_CHAR 49
1890#define TOK_LIT_INT 50
1891#define TOK_LIT_FLOAT 51
1892#define TOK_MACRO 52
1893#define TOK_CONCATENATE 53
1894
1895#define TOK_IDENT 54
1896#define TOK_STRUCT_NAME 55
1897#define TOK_ENUM_CONST 56
1898#define TOK_TYPE_NAME 57
1899
1900#define TOK_AUTO 58
1901#define TOK_BREAK 59
1902#define TOK_CASE 60
1903#define TOK_CHAR 61
1904#define TOK_CONST 62
1905#define TOK_CONTINUE 63
1906#define TOK_DEFAULT 64
1907#define TOK_DO 65
1908#define TOK_DOUBLE 66
1909#define TOK_ELSE 67
1910#define TOK_ENUM 68
1911#define TOK_EXTERN 69
1912#define TOK_FLOAT 70
1913#define TOK_FOR 71
1914#define TOK_GOTO 72
1915#define TOK_IF 73
1916#define TOK_INLINE 74
1917#define TOK_INT 75
1918#define TOK_LONG 76
1919#define TOK_REGISTER 77
1920#define TOK_RESTRICT 78
1921#define TOK_RETURN 79
1922#define TOK_SHORT 80
1923#define TOK_SIGNED 81
1924#define TOK_SIZEOF 82
1925#define TOK_STATIC 83
1926#define TOK_STRUCT 84
1927#define TOK_SWITCH 85
1928#define TOK_TYPEDEF 86
1929#define TOK_UNION 87
1930#define TOK_UNSIGNED 88
1931#define TOK_VOID 89
1932#define TOK_VOLATILE 90
1933#define TOK_WHILE 91
1934#define TOK_ASM 92
1935#define TOK_ATTRIBUTE 93
1936#define TOK_ALIGNOF 94
1937#define TOK_FIRST_KEYWORD TOK_AUTO
1938#define TOK_LAST_KEYWORD TOK_ALIGNOF
1939
1940#define TOK_DEFINE 100
1941#define TOK_UNDEF 101
1942#define TOK_INCLUDE 102
1943#define TOK_LINE 103
1944#define TOK_ERROR 104
1945#define TOK_WARNING 105
1946#define TOK_PRAGMA 106
1947#define TOK_IFDEF 107
1948#define TOK_IFNDEF 108
1949#define TOK_ELIF 109
1950#define TOK_ENDIF 110
1951
1952#define TOK_FIRST_MACRO TOK_DEFINE
1953#define TOK_LAST_MACRO TOK_ENDIF
1954
1955#define TOK_EOF 111
1956
1957static const char *tokens[] = {
1958[TOK_UNKNOWN ] = "unknown",
1959[TOK_SPACE ] = ":space:",
1960[TOK_SEMI ] = ";",
1961[TOK_LBRACE ] = "{",
1962[TOK_RBRACE ] = "}",
1963[TOK_COMMA ] = ",",
1964[TOK_EQ ] = "=",
1965[TOK_COLON ] = ":",
1966[TOK_LBRACKET ] = "[",
1967[TOK_RBRACKET ] = "]",
1968[TOK_LPAREN ] = "(",
1969[TOK_RPAREN ] = ")",
1970[TOK_STAR ] = "*",
1971[TOK_DOTS ] = "...",
1972[TOK_MORE ] = ">",
1973[TOK_LESS ] = "<",
1974[TOK_TIMESEQ ] = "*=",
1975[TOK_DIVEQ ] = "/=",
1976[TOK_MODEQ ] = "%=",
1977[TOK_PLUSEQ ] = "+=",
1978[TOK_MINUSEQ ] = "-=",
1979[TOK_SLEQ ] = "<<=",
1980[TOK_SREQ ] = ">>=",
1981[TOK_ANDEQ ] = "&=",
1982[TOK_XOREQ ] = "^=",
1983[TOK_OREQ ] = "|=",
1984[TOK_EQEQ ] = "==",
1985[TOK_NOTEQ ] = "!=",
1986[TOK_QUEST ] = "?",
1987[TOK_LOGOR ] = "||",
1988[TOK_LOGAND ] = "&&",
1989[TOK_OR ] = "|",
1990[TOK_AND ] = "&",
1991[TOK_XOR ] = "^",
1992[TOK_LESSEQ ] = "<=",
1993[TOK_MOREEQ ] = ">=",
1994[TOK_SL ] = "<<",
1995[TOK_SR ] = ">>",
1996[TOK_PLUS ] = "+",
1997[TOK_MINUS ] = "-",
1998[TOK_DIV ] = "/",
1999[TOK_MOD ] = "%",
2000[TOK_PLUSPLUS ] = "++",
2001[TOK_MINUSMINUS ] = "--",
2002[TOK_BANG ] = "!",
2003[TOK_ARROW ] = "->",
2004[TOK_DOT ] = ".",
2005[TOK_TILDE ] = "~",
2006[TOK_LIT_STRING ] = ":string:",
2007[TOK_IDENT ] = ":ident:",
2008[TOK_TYPE_NAME ] = ":typename:",
2009[TOK_LIT_CHAR ] = ":char:",
2010[TOK_LIT_INT ] = ":integer:",
2011[TOK_LIT_FLOAT ] = ":float:",
2012[TOK_MACRO ] = "#",
2013[TOK_CONCATENATE ] = "##",
2014
2015[TOK_AUTO ] = "auto",
2016[TOK_BREAK ] = "break",
2017[TOK_CASE ] = "case",
2018[TOK_CHAR ] = "char",
2019[TOK_CONST ] = "const",
2020[TOK_CONTINUE ] = "continue",
2021[TOK_DEFAULT ] = "default",
2022[TOK_DO ] = "do",
2023[TOK_DOUBLE ] = "double",
2024[TOK_ELSE ] = "else",
2025[TOK_ENUM ] = "enum",
2026[TOK_EXTERN ] = "extern",
2027[TOK_FLOAT ] = "float",
2028[TOK_FOR ] = "for",
2029[TOK_GOTO ] = "goto",
2030[TOK_IF ] = "if",
2031[TOK_INLINE ] = "inline",
2032[TOK_INT ] = "int",
2033[TOK_LONG ] = "long",
2034[TOK_REGISTER ] = "register",
2035[TOK_RESTRICT ] = "restrict",
2036[TOK_RETURN ] = "return",
2037[TOK_SHORT ] = "short",
2038[TOK_SIGNED ] = "signed",
2039[TOK_SIZEOF ] = "sizeof",
2040[TOK_STATIC ] = "static",
2041[TOK_STRUCT ] = "struct",
2042[TOK_SWITCH ] = "switch",
2043[TOK_TYPEDEF ] = "typedef",
2044[TOK_UNION ] = "union",
2045[TOK_UNSIGNED ] = "unsigned",
2046[TOK_VOID ] = "void",
2047[TOK_VOLATILE ] = "volatile",
2048[TOK_WHILE ] = "while",
2049[TOK_ASM ] = "asm",
2050[TOK_ATTRIBUTE ] = "__attribute__",
2051[TOK_ALIGNOF ] = "__alignof__",
2052
2053[TOK_DEFINE ] = "define",
2054[TOK_UNDEF ] = "undef",
2055[TOK_INCLUDE ] = "include",
2056[TOK_LINE ] = "line",
2057[TOK_ERROR ] = "error",
2058[TOK_WARNING ] = "warning",
2059[TOK_PRAGMA ] = "pragma",
2060[TOK_IFDEF ] = "ifdef",
2061[TOK_IFNDEF ] = "ifndef",
2062[TOK_ELIF ] = "elif",
2063[TOK_ENDIF ] = "endif",
2064
2065[TOK_EOF ] = "EOF",
2066};
2067
2068static unsigned int hash(const char *str, int str_len)
2069{
2070 unsigned int hash;
2071 const char *end;
2072 end = str + str_len;
2073 hash = 0;
2074 for(; str < end; str++) {
2075 hash = (hash *263) + *str;
2076 }
2077 hash = hash & (HASH_TABLE_SIZE -1);
2078 return hash;
2079}
2080
2081static struct hash_entry *lookup(
2082 struct compile_state *state, const char *name, int name_len)
2083{
2084 struct hash_entry *entry;
2085 unsigned int index;
2086 index = hash(name, name_len);
2087 entry = state->hash_table[index];
2088 while(entry &&
2089 ((entry->name_len != name_len) ||
2090 (memcmp(entry->name, name, name_len) != 0))) {
2091 entry = entry->next;
2092 }
2093 if (!entry) {
2094 char *new_name;
2095 /* Get a private copy of the name */
2096 new_name = xmalloc(name_len + 1, "hash_name");
2097 memcpy(new_name, name, name_len);
2098 new_name[name_len] = '\0';
2099
2100 /* Create a new hash entry */
2101 entry = xcmalloc(sizeof(*entry), "hash_entry");
2102 entry->next = state->hash_table[index];
2103 entry->name = new_name;
2104 entry->name_len = name_len;
2105
2106 /* Place the new entry in the hash table */
2107 state->hash_table[index] = entry;
2108 }
2109 return entry;
2110}
2111
2112static void ident_to_keyword(struct compile_state *state, struct token *tk)
2113{
2114 struct hash_entry *entry;
2115 entry = tk->ident;
2116 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
2117 (entry->tok == TOK_ENUM_CONST) ||
2118 ((entry->tok >= TOK_FIRST_KEYWORD) &&
2119 (entry->tok <= TOK_LAST_KEYWORD)))) {
2120 tk->tok = entry->tok;
2121 }
2122}
2123
2124static void ident_to_macro(struct compile_state *state, struct token *tk)
2125{
2126 struct hash_entry *entry;
2127 entry = tk->ident;
2128 if (entry &&
2129 (entry->tok >= TOK_FIRST_MACRO) &&
2130 (entry->tok <= TOK_LAST_MACRO)) {
2131 tk->tok = entry->tok;
2132 }
2133}
2134
2135static void hash_keyword(
2136 struct compile_state *state, const char *keyword, int tok)
2137{
2138 struct hash_entry *entry;
2139 entry = lookup(state, keyword, strlen(keyword));
2140 if (entry && entry->tok != TOK_UNKNOWN) {
2141 die("keyword %s already hashed", keyword);
2142 }
2143 entry->tok = tok;
2144}
2145
2146static void symbol(
2147 struct compile_state *state, struct hash_entry *ident,
2148 struct symbol **chain, struct triple *def, struct type *type)
2149{
2150 struct symbol *sym;
2151 if (*chain && ((*chain)->scope_depth == state->scope_depth)) {
2152 error(state, 0, "%s already defined", ident->name);
2153 }
2154 sym = xcmalloc(sizeof(*sym), "symbol");
2155 sym->ident = ident;
2156 sym->def = def;
2157 sym->type = type;
2158 sym->scope_depth = state->scope_depth;
2159 sym->next = *chain;
2160 *chain = sym;
2161}
2162
Eric Biederman153ea352003-06-20 14:43:20 +00002163static void label_symbol(struct compile_state *state,
2164 struct hash_entry *ident, struct triple *label)
2165{
2166 struct symbol *sym;
2167 if (ident->sym_label) {
2168 error(state, 0, "label %s already defined", ident->name);
2169 }
2170 sym = xcmalloc(sizeof(*sym), "label");
2171 sym->ident = ident;
2172 sym->def = label;
2173 sym->type = &void_type;
2174 sym->scope_depth = FUNCTION_SCOPE_DEPTH;
2175 sym->next = 0;
2176 ident->sym_label = sym;
2177}
2178
Eric Biedermanb138ac82003-04-22 18:44:01 +00002179static void start_scope(struct compile_state *state)
2180{
2181 state->scope_depth++;
2182}
2183
2184static void end_scope_syms(struct symbol **chain, int depth)
2185{
2186 struct symbol *sym, *next;
2187 sym = *chain;
2188 while(sym && (sym->scope_depth == depth)) {
2189 next = sym->next;
2190 xfree(sym);
2191 sym = next;
2192 }
2193 *chain = sym;
2194}
2195
2196static void end_scope(struct compile_state *state)
2197{
2198 int i;
2199 int depth;
2200 /* Walk through the hash table and remove all symbols
2201 * in the current scope.
2202 */
2203 depth = state->scope_depth;
2204 for(i = 0; i < HASH_TABLE_SIZE; i++) {
2205 struct hash_entry *entry;
2206 entry = state->hash_table[i];
2207 while(entry) {
2208 end_scope_syms(&entry->sym_label, depth);
2209 end_scope_syms(&entry->sym_struct, depth);
2210 end_scope_syms(&entry->sym_ident, depth);
2211 entry = entry->next;
2212 }
2213 }
2214 state->scope_depth = depth - 1;
2215}
2216
2217static void register_keywords(struct compile_state *state)
2218{
2219 hash_keyword(state, "auto", TOK_AUTO);
2220 hash_keyword(state, "break", TOK_BREAK);
2221 hash_keyword(state, "case", TOK_CASE);
2222 hash_keyword(state, "char", TOK_CHAR);
2223 hash_keyword(state, "const", TOK_CONST);
2224 hash_keyword(state, "continue", TOK_CONTINUE);
2225 hash_keyword(state, "default", TOK_DEFAULT);
2226 hash_keyword(state, "do", TOK_DO);
2227 hash_keyword(state, "double", TOK_DOUBLE);
2228 hash_keyword(state, "else", TOK_ELSE);
2229 hash_keyword(state, "enum", TOK_ENUM);
2230 hash_keyword(state, "extern", TOK_EXTERN);
2231 hash_keyword(state, "float", TOK_FLOAT);
2232 hash_keyword(state, "for", TOK_FOR);
2233 hash_keyword(state, "goto", TOK_GOTO);
2234 hash_keyword(state, "if", TOK_IF);
2235 hash_keyword(state, "inline", TOK_INLINE);
2236 hash_keyword(state, "int", TOK_INT);
2237 hash_keyword(state, "long", TOK_LONG);
2238 hash_keyword(state, "register", TOK_REGISTER);
2239 hash_keyword(state, "restrict", TOK_RESTRICT);
2240 hash_keyword(state, "return", TOK_RETURN);
2241 hash_keyword(state, "short", TOK_SHORT);
2242 hash_keyword(state, "signed", TOK_SIGNED);
2243 hash_keyword(state, "sizeof", TOK_SIZEOF);
2244 hash_keyword(state, "static", TOK_STATIC);
2245 hash_keyword(state, "struct", TOK_STRUCT);
2246 hash_keyword(state, "switch", TOK_SWITCH);
2247 hash_keyword(state, "typedef", TOK_TYPEDEF);
2248 hash_keyword(state, "union", TOK_UNION);
2249 hash_keyword(state, "unsigned", TOK_UNSIGNED);
2250 hash_keyword(state, "void", TOK_VOID);
2251 hash_keyword(state, "volatile", TOK_VOLATILE);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002252 hash_keyword(state, "__volatile__", TOK_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002253 hash_keyword(state, "while", TOK_WHILE);
2254 hash_keyword(state, "asm", TOK_ASM);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002255 hash_keyword(state, "__asm__", TOK_ASM);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002256 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
2257 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
2258}
2259
2260static void register_macro_keywords(struct compile_state *state)
2261{
2262 hash_keyword(state, "define", TOK_DEFINE);
2263 hash_keyword(state, "undef", TOK_UNDEF);
2264 hash_keyword(state, "include", TOK_INCLUDE);
2265 hash_keyword(state, "line", TOK_LINE);
2266 hash_keyword(state, "error", TOK_ERROR);
2267 hash_keyword(state, "warning", TOK_WARNING);
2268 hash_keyword(state, "pragma", TOK_PRAGMA);
2269 hash_keyword(state, "ifdef", TOK_IFDEF);
2270 hash_keyword(state, "ifndef", TOK_IFNDEF);
2271 hash_keyword(state, "elif", TOK_ELIF);
2272 hash_keyword(state, "endif", TOK_ENDIF);
2273}
2274
2275static int spacep(int c)
2276{
2277 int ret = 0;
2278 switch(c) {
2279 case ' ':
2280 case '\t':
2281 case '\f':
2282 case '\v':
2283 case '\r':
2284 case '\n':
2285 ret = 1;
2286 break;
2287 }
2288 return ret;
2289}
2290
2291static int digitp(int c)
2292{
2293 int ret = 0;
2294 switch(c) {
2295 case '0': case '1': case '2': case '3': case '4':
2296 case '5': case '6': case '7': case '8': case '9':
2297 ret = 1;
2298 break;
2299 }
2300 return ret;
2301}
Eric Biederman8d9c1232003-06-17 08:42:17 +00002302static int digval(int c)
2303{
2304 int val = -1;
2305 if ((c >= '0') && (c <= '9')) {
2306 val = c - '0';
2307 }
2308 return val;
2309}
Eric Biedermanb138ac82003-04-22 18:44:01 +00002310
2311static int hexdigitp(int c)
2312{
2313 int ret = 0;
2314 switch(c) {
2315 case '0': case '1': case '2': case '3': case '4':
2316 case '5': case '6': case '7': case '8': case '9':
2317 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2318 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2319 ret = 1;
2320 break;
2321 }
2322 return ret;
2323}
2324static int hexdigval(int c)
2325{
2326 int val = -1;
2327 if ((c >= '0') && (c <= '9')) {
2328 val = c - '0';
2329 }
2330 else if ((c >= 'A') && (c <= 'F')) {
2331 val = 10 + (c - 'A');
2332 }
2333 else if ((c >= 'a') && (c <= 'f')) {
2334 val = 10 + (c - 'a');
2335 }
2336 return val;
2337}
2338
2339static int octdigitp(int c)
2340{
2341 int ret = 0;
2342 switch(c) {
2343 case '0': case '1': case '2': case '3':
2344 case '4': case '5': case '6': case '7':
2345 ret = 1;
2346 break;
2347 }
2348 return ret;
2349}
2350static int octdigval(int c)
2351{
2352 int val = -1;
2353 if ((c >= '0') && (c <= '7')) {
2354 val = c - '0';
2355 }
2356 return val;
2357}
2358
2359static int letterp(int c)
2360{
2361 int ret = 0;
2362 switch(c) {
2363 case 'a': case 'b': case 'c': case 'd': case 'e':
2364 case 'f': case 'g': case 'h': case 'i': case 'j':
2365 case 'k': case 'l': case 'm': case 'n': case 'o':
2366 case 'p': case 'q': case 'r': case 's': case 't':
2367 case 'u': case 'v': case 'w': case 'x': case 'y':
2368 case 'z':
2369 case 'A': case 'B': case 'C': case 'D': case 'E':
2370 case 'F': case 'G': case 'H': case 'I': case 'J':
2371 case 'K': case 'L': case 'M': case 'N': case 'O':
2372 case 'P': case 'Q': case 'R': case 'S': case 'T':
2373 case 'U': case 'V': case 'W': case 'X': case 'Y':
2374 case 'Z':
2375 case '_':
2376 ret = 1;
2377 break;
2378 }
2379 return ret;
2380}
2381
2382static int char_value(struct compile_state *state,
2383 const signed char **strp, const signed char *end)
2384{
2385 const signed char *str;
2386 int c;
2387 str = *strp;
2388 c = *str++;
2389 if ((c == '\\') && (str < end)) {
2390 switch(*str) {
2391 case 'n': c = '\n'; str++; break;
2392 case 't': c = '\t'; str++; break;
2393 case 'v': c = '\v'; str++; break;
2394 case 'b': c = '\b'; str++; break;
2395 case 'r': c = '\r'; str++; break;
2396 case 'f': c = '\f'; str++; break;
2397 case 'a': c = '\a'; str++; break;
2398 case '\\': c = '\\'; str++; break;
2399 case '?': c = '?'; str++; break;
2400 case '\'': c = '\''; str++; break;
2401 case '"': c = '"'; break;
2402 case 'x':
2403 c = 0;
2404 str++;
2405 while((str < end) && hexdigitp(*str)) {
2406 c <<= 4;
2407 c += hexdigval(*str);
2408 str++;
2409 }
2410 break;
2411 case '0': case '1': case '2': case '3':
2412 case '4': case '5': case '6': case '7':
2413 c = 0;
2414 while((str < end) && octdigitp(*str)) {
2415 c <<= 3;
2416 c += octdigval(*str);
2417 str++;
2418 }
2419 break;
2420 default:
2421 error(state, 0, "Invalid character constant");
2422 break;
2423 }
2424 }
2425 *strp = str;
2426 return c;
2427}
2428
2429static char *after_digits(char *ptr, char *end)
2430{
2431 while((ptr < end) && digitp(*ptr)) {
2432 ptr++;
2433 }
2434 return ptr;
2435}
2436
2437static char *after_octdigits(char *ptr, char *end)
2438{
2439 while((ptr < end) && octdigitp(*ptr)) {
2440 ptr++;
2441 }
2442 return ptr;
2443}
2444
2445static char *after_hexdigits(char *ptr, char *end)
2446{
2447 while((ptr < end) && hexdigitp(*ptr)) {
2448 ptr++;
2449 }
2450 return ptr;
2451}
2452
2453static void save_string(struct compile_state *state,
2454 struct token *tk, char *start, char *end, const char *id)
2455{
2456 char *str;
2457 int str_len;
2458 /* Create a private copy of the string */
2459 str_len = end - start + 1;
2460 str = xmalloc(str_len + 1, id);
2461 memcpy(str, start, str_len);
2462 str[str_len] = '\0';
2463
2464 /* Store the copy in the token */
2465 tk->val.str = str;
2466 tk->str_len = str_len;
2467}
2468static void next_token(struct compile_state *state, int index)
2469{
2470 struct file_state *file;
2471 struct token *tk;
2472 char *token;
2473 int c, c1, c2, c3;
2474 char *tokp, *end;
2475 int tok;
2476next_token:
2477 file = state->file;
2478 tk = &state->token[index];
2479 tk->str_len = 0;
2480 tk->ident = 0;
2481 token = tokp = file->pos;
2482 end = file->buf + file->size;
2483 tok = TOK_UNKNOWN;
2484 c = -1;
2485 if (tokp < end) {
2486 c = *tokp;
2487 }
2488 c1 = -1;
2489 if ((tokp + 1) < end) {
2490 c1 = tokp[1];
2491 }
2492 c2 = -1;
2493 if ((tokp + 2) < end) {
2494 c2 = tokp[2];
2495 }
2496 c3 = -1;
2497 if ((tokp + 3) < end) {
2498 c3 = tokp[3];
2499 }
2500 if (tokp >= end) {
2501 tok = TOK_EOF;
2502 tokp = end;
2503 }
2504 /* Whitespace */
2505 else if (spacep(c)) {
2506 tok = TOK_SPACE;
2507 while ((tokp < end) && spacep(c)) {
2508 if (c == '\n') {
2509 file->line++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002510 file->report_line++;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002511 file->line_start = tokp + 1;
2512 }
2513 c = *(++tokp);
2514 }
2515 if (!spacep(c)) {
2516 tokp--;
2517 }
2518 }
2519 /* EOL Comments */
2520 else if ((c == '/') && (c1 == '/')) {
2521 tok = TOK_SPACE;
2522 for(tokp += 2; tokp < end; tokp++) {
2523 c = *tokp;
2524 if (c == '\n') {
2525 file->line++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002526 file->report_line++;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002527 file->line_start = tokp +1;
2528 break;
2529 }
2530 }
2531 }
2532 /* Comments */
2533 else if ((c == '/') && (c1 == '*')) {
2534 int line;
2535 char *line_start;
2536 line = file->line;
2537 line_start = file->line_start;
2538 for(tokp += 2; (end - tokp) >= 2; tokp++) {
2539 c = *tokp;
2540 if (c == '\n') {
2541 line++;
2542 line_start = tokp +1;
2543 }
2544 else if ((c == '*') && (tokp[1] == '/')) {
2545 tok = TOK_SPACE;
2546 tokp += 1;
2547 break;
2548 }
2549 }
2550 if (tok == TOK_UNKNOWN) {
2551 error(state, 0, "unterminated comment");
2552 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002553 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002554 file->line = line;
2555 file->line_start = line_start;
2556 }
2557 /* string constants */
2558 else if ((c == '"') ||
2559 ((c == 'L') && (c1 == '"'))) {
2560 int line;
2561 char *line_start;
2562 int wchar;
2563 line = file->line;
2564 line_start = file->line_start;
2565 wchar = 0;
2566 if (c == 'L') {
2567 wchar = 1;
2568 tokp++;
2569 }
2570 for(tokp += 1; tokp < end; tokp++) {
2571 c = *tokp;
2572 if (c == '\n') {
2573 line++;
2574 line_start = tokp + 1;
2575 }
2576 else if ((c == '\\') && (tokp +1 < end)) {
2577 tokp++;
2578 }
2579 else if (c == '"') {
2580 tok = TOK_LIT_STRING;
2581 break;
2582 }
2583 }
2584 if (tok == TOK_UNKNOWN) {
2585 error(state, 0, "unterminated string constant");
2586 }
2587 if (line != file->line) {
2588 warning(state, 0, "multiline string constant");
2589 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002590 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002591 file->line = line;
2592 file->line_start = line_start;
2593
2594 /* Save the string value */
2595 save_string(state, tk, token, tokp, "literal string");
2596 }
2597 /* character constants */
2598 else if ((c == '\'') ||
2599 ((c == 'L') && (c1 == '\''))) {
2600 int line;
2601 char *line_start;
2602 int wchar;
2603 line = file->line;
2604 line_start = file->line_start;
2605 wchar = 0;
2606 if (c == 'L') {
2607 wchar = 1;
2608 tokp++;
2609 }
2610 for(tokp += 1; tokp < end; tokp++) {
2611 c = *tokp;
2612 if (c == '\n') {
2613 line++;
2614 line_start = tokp + 1;
2615 }
2616 else if ((c == '\\') && (tokp +1 < end)) {
2617 tokp++;
2618 }
2619 else if (c == '\'') {
2620 tok = TOK_LIT_CHAR;
2621 break;
2622 }
2623 }
2624 if (tok == TOK_UNKNOWN) {
2625 error(state, 0, "unterminated character constant");
2626 }
2627 if (line != file->line) {
2628 warning(state, 0, "multiline character constant");
2629 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002630 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002631 file->line = line;
2632 file->line_start = line_start;
2633
2634 /* Save the character value */
2635 save_string(state, tk, token, tokp, "literal character");
2636 }
2637 /* integer and floating constants
2638 * Integer Constants
2639 * {digits}
2640 * 0[Xx]{hexdigits}
2641 * 0{octdigit}+
2642 *
2643 * Floating constants
2644 * {digits}.{digits}[Ee][+-]?{digits}
2645 * {digits}.{digits}
2646 * {digits}[Ee][+-]?{digits}
2647 * .{digits}[Ee][+-]?{digits}
2648 * .{digits}
2649 */
2650
2651 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
2652 char *next, *new;
2653 int is_float;
2654 is_float = 0;
2655 if (c != '.') {
2656 next = after_digits(tokp, end);
2657 }
2658 else {
2659 next = tokp;
2660 }
2661 if (next[0] == '.') {
2662 new = after_digits(next, end);
2663 is_float = (new != next);
2664 next = new;
2665 }
2666 if ((next[0] == 'e') || (next[0] == 'E')) {
2667 if (((next + 1) < end) &&
2668 ((next[1] == '+') || (next[1] == '-'))) {
2669 next++;
2670 }
2671 new = after_digits(next, end);
2672 is_float = (new != next);
2673 next = new;
2674 }
2675 if (is_float) {
2676 tok = TOK_LIT_FLOAT;
2677 if ((next < end) && (
2678 (next[0] == 'f') ||
2679 (next[0] == 'F') ||
2680 (next[0] == 'l') ||
2681 (next[0] == 'L'))
2682 ) {
2683 next++;
2684 }
2685 }
2686 if (!is_float && digitp(c)) {
2687 tok = TOK_LIT_INT;
2688 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
2689 next = after_hexdigits(tokp + 2, end);
2690 }
2691 else if (c == '0') {
2692 next = after_octdigits(tokp, end);
2693 }
2694 else {
2695 next = after_digits(tokp, end);
2696 }
2697 /* crazy integer suffixes */
2698 if ((next < end) &&
2699 ((next[0] == 'u') || (next[0] == 'U'))) {
2700 next++;
2701 if ((next < end) &&
2702 ((next[0] == 'l') || (next[0] == 'L'))) {
2703 next++;
2704 }
2705 }
2706 else if ((next < end) &&
2707 ((next[0] == 'l') || (next[0] == 'L'))) {
2708 next++;
2709 if ((next < end) &&
2710 ((next[0] == 'u') || (next[0] == 'U'))) {
2711 next++;
2712 }
2713 }
2714 }
2715 tokp = next - 1;
2716
2717 /* Save the integer/floating point value */
2718 save_string(state, tk, token, tokp, "literal number");
2719 }
2720 /* identifiers */
2721 else if (letterp(c)) {
2722 tok = TOK_IDENT;
2723 for(tokp += 1; tokp < end; tokp++) {
2724 c = *tokp;
2725 if (!letterp(c) && !digitp(c)) {
2726 break;
2727 }
2728 }
2729 tokp -= 1;
2730 tk->ident = lookup(state, token, tokp +1 - token);
2731 }
2732 /* C99 alternate macro characters */
2733 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
2734 tokp += 3;
2735 tok = TOK_CONCATENATE;
2736 }
2737 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { tokp += 2; tok = TOK_DOTS; }
2738 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { tokp += 2; tok = TOK_SLEQ; }
2739 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { tokp += 2; tok = TOK_SREQ; }
2740 else if ((c == '*') && (c1 == '=')) { tokp += 1; tok = TOK_TIMESEQ; }
2741 else if ((c == '/') && (c1 == '=')) { tokp += 1; tok = TOK_DIVEQ; }
2742 else if ((c == '%') && (c1 == '=')) { tokp += 1; tok = TOK_MODEQ; }
2743 else if ((c == '+') && (c1 == '=')) { tokp += 1; tok = TOK_PLUSEQ; }
2744 else if ((c == '-') && (c1 == '=')) { tokp += 1; tok = TOK_MINUSEQ; }
2745 else if ((c == '&') && (c1 == '=')) { tokp += 1; tok = TOK_ANDEQ; }
2746 else if ((c == '^') && (c1 == '=')) { tokp += 1; tok = TOK_XOREQ; }
2747 else if ((c == '|') && (c1 == '=')) { tokp += 1; tok = TOK_OREQ; }
2748 else if ((c == '=') && (c1 == '=')) { tokp += 1; tok = TOK_EQEQ; }
2749 else if ((c == '!') && (c1 == '=')) { tokp += 1; tok = TOK_NOTEQ; }
2750 else if ((c == '|') && (c1 == '|')) { tokp += 1; tok = TOK_LOGOR; }
2751 else if ((c == '&') && (c1 == '&')) { tokp += 1; tok = TOK_LOGAND; }
2752 else if ((c == '<') && (c1 == '=')) { tokp += 1; tok = TOK_LESSEQ; }
2753 else if ((c == '>') && (c1 == '=')) { tokp += 1; tok = TOK_MOREEQ; }
2754 else if ((c == '<') && (c1 == '<')) { tokp += 1; tok = TOK_SL; }
2755 else if ((c == '>') && (c1 == '>')) { tokp += 1; tok = TOK_SR; }
2756 else if ((c == '+') && (c1 == '+')) { tokp += 1; tok = TOK_PLUSPLUS; }
2757 else if ((c == '-') && (c1 == '-')) { tokp += 1; tok = TOK_MINUSMINUS; }
2758 else if ((c == '-') && (c1 == '>')) { tokp += 1; tok = TOK_ARROW; }
2759 else if ((c == '<') && (c1 == ':')) { tokp += 1; tok = TOK_LBRACKET; }
2760 else if ((c == ':') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACKET; }
2761 else if ((c == '<') && (c1 == '%')) { tokp += 1; tok = TOK_LBRACE; }
2762 else if ((c == '%') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACE; }
2763 else if ((c == '%') && (c1 == ':')) { tokp += 1; tok = TOK_MACRO; }
2764 else if ((c == '#') && (c1 == '#')) { tokp += 1; tok = TOK_CONCATENATE; }
2765 else if (c == ';') { tok = TOK_SEMI; }
2766 else if (c == '{') { tok = TOK_LBRACE; }
2767 else if (c == '}') { tok = TOK_RBRACE; }
2768 else if (c == ',') { tok = TOK_COMMA; }
2769 else if (c == '=') { tok = TOK_EQ; }
2770 else if (c == ':') { tok = TOK_COLON; }
2771 else if (c == '[') { tok = TOK_LBRACKET; }
2772 else if (c == ']') { tok = TOK_RBRACKET; }
2773 else if (c == '(') { tok = TOK_LPAREN; }
2774 else if (c == ')') { tok = TOK_RPAREN; }
2775 else if (c == '*') { tok = TOK_STAR; }
2776 else if (c == '>') { tok = TOK_MORE; }
2777 else if (c == '<') { tok = TOK_LESS; }
2778 else if (c == '?') { tok = TOK_QUEST; }
2779 else if (c == '|') { tok = TOK_OR; }
2780 else if (c == '&') { tok = TOK_AND; }
2781 else if (c == '^') { tok = TOK_XOR; }
2782 else if (c == '+') { tok = TOK_PLUS; }
2783 else if (c == '-') { tok = TOK_MINUS; }
2784 else if (c == '/') { tok = TOK_DIV; }
2785 else if (c == '%') { tok = TOK_MOD; }
2786 else if (c == '!') { tok = TOK_BANG; }
2787 else if (c == '.') { tok = TOK_DOT; }
2788 else if (c == '~') { tok = TOK_TILDE; }
2789 else if (c == '#') { tok = TOK_MACRO; }
2790 if (tok == TOK_MACRO) {
2791 /* Only match preprocessor directives at the start of a line */
2792 char *ptr;
2793 for(ptr = file->line_start; spacep(*ptr); ptr++)
2794 ;
2795 if (ptr != tokp) {
2796 tok = TOK_UNKNOWN;
2797 }
2798 }
2799 if (tok == TOK_UNKNOWN) {
2800 error(state, 0, "unknown token");
2801 }
2802
2803 file->pos = tokp + 1;
2804 tk->tok = tok;
2805 if (tok == TOK_IDENT) {
2806 ident_to_keyword(state, tk);
2807 }
2808 /* Don't return space tokens. */
2809 if (tok == TOK_SPACE) {
2810 goto next_token;
2811 }
2812}
2813
2814static void compile_macro(struct compile_state *state, struct token *tk)
2815{
2816 struct file_state *file;
2817 struct hash_entry *ident;
2818 ident = tk->ident;
2819 file = xmalloc(sizeof(*file), "file_state");
2820 file->basename = xstrdup(tk->ident->name);
2821 file->dirname = xstrdup("");
2822 file->size = ident->sym_define->buf_len;
2823 file->buf = xmalloc(file->size +2, file->basename);
2824 memcpy(file->buf, ident->sym_define->buf, file->size);
2825 file->buf[file->size] = '\n';
2826 file->buf[file->size + 1] = '\0';
2827 file->pos = file->buf;
2828 file->line_start = file->pos;
2829 file->line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002830 file->report_line = 1;
2831 file->report_name = file->basename;
2832 file->report_dir = file->dirname;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002833 file->prev = state->file;
2834 state->file = file;
2835}
2836
2837
2838static int mpeek(struct compile_state *state, int index)
2839{
2840 struct token *tk;
2841 int rescan;
2842 tk = &state->token[index + 1];
2843 if (tk->tok == -1) {
2844 next_token(state, index + 1);
2845 }
2846 do {
2847 rescan = 0;
2848 if ((tk->tok == TOK_EOF) &&
2849 (state->file != state->macro_file) &&
2850 (state->file->prev)) {
2851 struct file_state *file = state->file;
2852 state->file = file->prev;
2853 /* file->basename is used keep it */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002854 if (file->report_dir != file->dirname) {
2855 xfree(file->report_dir);
2856 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002857 xfree(file->dirname);
2858 xfree(file->buf);
2859 xfree(file);
2860 next_token(state, index + 1);
2861 rescan = 1;
2862 }
2863 else if (tk->ident && tk->ident->sym_define) {
2864 compile_macro(state, tk);
2865 next_token(state, index + 1);
2866 rescan = 1;
2867 }
2868 } while(rescan);
2869 /* Don't show the token on the next line */
2870 if (state->macro_line < state->macro_file->line) {
2871 return TOK_EOF;
2872 }
2873 return state->token[index +1].tok;
2874}
2875
2876static void meat(struct compile_state *state, int index, int tok)
2877{
2878 int next_tok;
2879 int i;
2880 next_tok = mpeek(state, index);
2881 if (next_tok != tok) {
2882 const char *name1, *name2;
2883 name1 = tokens[next_tok];
2884 name2 = "";
2885 if (next_tok == TOK_IDENT) {
2886 name2 = state->token[index + 1].ident->name;
2887 }
2888 error(state, 0, "found %s %s expected %s",
2889 name1, name2, tokens[tok]);
2890 }
2891 /* Free the old token value */
2892 if (state->token[index].str_len) {
2893 memset((void *)(state->token[index].val.str), -1,
2894 state->token[index].str_len);
2895 xfree(state->token[index].val.str);
2896 }
2897 for(i = index; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
2898 state->token[i] = state->token[i + 1];
2899 }
2900 memset(&state->token[i], 0, sizeof(state->token[i]));
2901 state->token[i].tok = -1;
2902}
2903
2904static long_t mcexpr(struct compile_state *state, int index);
2905
2906static long_t mprimary_expr(struct compile_state *state, int index)
2907{
2908 long_t val;
2909 int tok;
2910 tok = mpeek(state, index);
2911 while(state->token[index + 1].ident &&
2912 state->token[index + 1].ident->sym_define) {
2913 meat(state, index, tok);
2914 compile_macro(state, &state->token[index]);
2915 tok = mpeek(state, index);
2916 }
2917 switch(tok) {
2918 case TOK_LPAREN:
2919 meat(state, index, TOK_LPAREN);
2920 val = mcexpr(state, index);
2921 meat(state, index, TOK_RPAREN);
2922 break;
2923 case TOK_LIT_INT:
2924 {
2925 char *end;
2926 meat(state, index, TOK_LIT_INT);
2927 errno = 0;
2928 val = strtol(state->token[index].val.str, &end, 0);
2929 if (((val == LONG_MIN) || (val == LONG_MAX)) &&
2930 (errno == ERANGE)) {
2931 error(state, 0, "Integer constant to large");
2932 }
2933 break;
2934 }
2935 default:
2936 meat(state, index, TOK_LIT_INT);
2937 val = 0;
2938 }
2939 return val;
2940}
2941static long_t munary_expr(struct compile_state *state, int index)
2942{
2943 long_t val;
2944 switch(mpeek(state, index)) {
2945 case TOK_PLUS:
2946 meat(state, index, TOK_PLUS);
2947 val = munary_expr(state, index);
2948 val = + val;
2949 break;
2950 case TOK_MINUS:
2951 meat(state, index, TOK_MINUS);
2952 val = munary_expr(state, index);
2953 val = - val;
2954 break;
2955 case TOK_TILDE:
2956 meat(state, index, TOK_BANG);
2957 val = munary_expr(state, index);
2958 val = ~ val;
2959 break;
2960 case TOK_BANG:
2961 meat(state, index, TOK_BANG);
2962 val = munary_expr(state, index);
2963 val = ! val;
2964 break;
2965 default:
2966 val = mprimary_expr(state, index);
2967 break;
2968 }
2969 return val;
2970
2971}
2972static long_t mmul_expr(struct compile_state *state, int index)
2973{
2974 long_t val;
2975 int done;
2976 val = munary_expr(state, index);
2977 do {
2978 long_t right;
2979 done = 0;
2980 switch(mpeek(state, index)) {
2981 case TOK_STAR:
2982 meat(state, index, TOK_STAR);
2983 right = munary_expr(state, index);
2984 val = val * right;
2985 break;
2986 case TOK_DIV:
2987 meat(state, index, TOK_DIV);
2988 right = munary_expr(state, index);
2989 val = val / right;
2990 break;
2991 case TOK_MOD:
2992 meat(state, index, TOK_MOD);
2993 right = munary_expr(state, index);
2994 val = val % right;
2995 break;
2996 default:
2997 done = 1;
2998 break;
2999 }
3000 } while(!done);
3001
3002 return val;
3003}
3004
3005static long_t madd_expr(struct compile_state *state, int index)
3006{
3007 long_t val;
3008 int done;
3009 val = mmul_expr(state, index);
3010 do {
3011 long_t right;
3012 done = 0;
3013 switch(mpeek(state, index)) {
3014 case TOK_PLUS:
3015 meat(state, index, TOK_PLUS);
3016 right = mmul_expr(state, index);
3017 val = val + right;
3018 break;
3019 case TOK_MINUS:
3020 meat(state, index, TOK_MINUS);
3021 right = mmul_expr(state, index);
3022 val = val - right;
3023 break;
3024 default:
3025 done = 1;
3026 break;
3027 }
3028 } while(!done);
3029
3030 return val;
3031}
3032
3033static long_t mshift_expr(struct compile_state *state, int index)
3034{
3035 long_t val;
3036 int done;
3037 val = madd_expr(state, index);
3038 do {
3039 long_t right;
3040 done = 0;
3041 switch(mpeek(state, index)) {
3042 case TOK_SL:
3043 meat(state, index, TOK_SL);
3044 right = madd_expr(state, index);
3045 val = val << right;
3046 break;
3047 case TOK_SR:
3048 meat(state, index, TOK_SR);
3049 right = madd_expr(state, index);
3050 val = val >> right;
3051 break;
3052 default:
3053 done = 1;
3054 break;
3055 }
3056 } while(!done);
3057
3058 return val;
3059}
3060
3061static long_t mrel_expr(struct compile_state *state, int index)
3062{
3063 long_t val;
3064 int done;
3065 val = mshift_expr(state, index);
3066 do {
3067 long_t right;
3068 done = 0;
3069 switch(mpeek(state, index)) {
3070 case TOK_LESS:
3071 meat(state, index, TOK_LESS);
3072 right = mshift_expr(state, index);
3073 val = val < right;
3074 break;
3075 case TOK_MORE:
3076 meat(state, index, TOK_MORE);
3077 right = mshift_expr(state, index);
3078 val = val > right;
3079 break;
3080 case TOK_LESSEQ:
3081 meat(state, index, TOK_LESSEQ);
3082 right = mshift_expr(state, index);
3083 val = val <= right;
3084 break;
3085 case TOK_MOREEQ:
3086 meat(state, index, TOK_MOREEQ);
3087 right = mshift_expr(state, index);
3088 val = val >= right;
3089 break;
3090 default:
3091 done = 1;
3092 break;
3093 }
3094 } while(!done);
3095 return val;
3096}
3097
3098static long_t meq_expr(struct compile_state *state, int index)
3099{
3100 long_t val;
3101 int done;
3102 val = mrel_expr(state, index);
3103 do {
3104 long_t right;
3105 done = 0;
3106 switch(mpeek(state, index)) {
3107 case TOK_EQEQ:
3108 meat(state, index, TOK_EQEQ);
3109 right = mrel_expr(state, index);
3110 val = val == right;
3111 break;
3112 case TOK_NOTEQ:
3113 meat(state, index, TOK_NOTEQ);
3114 right = mrel_expr(state, index);
3115 val = val != right;
3116 break;
3117 default:
3118 done = 1;
3119 break;
3120 }
3121 } while(!done);
3122 return val;
3123}
3124
3125static long_t mand_expr(struct compile_state *state, int index)
3126{
3127 long_t val;
3128 val = meq_expr(state, index);
3129 if (mpeek(state, index) == TOK_AND) {
3130 long_t right;
3131 meat(state, index, TOK_AND);
3132 right = meq_expr(state, index);
3133 val = val & right;
3134 }
3135 return val;
3136}
3137
3138static long_t mxor_expr(struct compile_state *state, int index)
3139{
3140 long_t val;
3141 val = mand_expr(state, index);
3142 if (mpeek(state, index) == TOK_XOR) {
3143 long_t right;
3144 meat(state, index, TOK_XOR);
3145 right = mand_expr(state, index);
3146 val = val ^ right;
3147 }
3148 return val;
3149}
3150
3151static long_t mor_expr(struct compile_state *state, int index)
3152{
3153 long_t val;
3154 val = mxor_expr(state, index);
3155 if (mpeek(state, index) == TOK_OR) {
3156 long_t right;
3157 meat(state, index, TOK_OR);
3158 right = mxor_expr(state, index);
3159 val = val | right;
3160 }
3161 return val;
3162}
3163
3164static long_t mland_expr(struct compile_state *state, int index)
3165{
3166 long_t val;
3167 val = mor_expr(state, index);
3168 if (mpeek(state, index) == TOK_LOGAND) {
3169 long_t right;
3170 meat(state, index, TOK_LOGAND);
3171 right = mor_expr(state, index);
3172 val = val && right;
3173 }
3174 return val;
3175}
3176static long_t mlor_expr(struct compile_state *state, int index)
3177{
3178 long_t val;
3179 val = mland_expr(state, index);
3180 if (mpeek(state, index) == TOK_LOGOR) {
3181 long_t right;
3182 meat(state, index, TOK_LOGOR);
3183 right = mland_expr(state, index);
3184 val = val || right;
3185 }
3186 return val;
3187}
3188
3189static long_t mcexpr(struct compile_state *state, int index)
3190{
3191 return mlor_expr(state, index);
3192}
3193static void preprocess(struct compile_state *state, int index)
3194{
3195 /* Doing much more with the preprocessor would require
3196 * a parser and a major restructuring.
3197 * Postpone that for later.
3198 */
3199 struct file_state *file;
3200 struct token *tk;
3201 int line;
3202 int tok;
3203
3204 file = state->file;
3205 tk = &state->token[index];
3206 state->macro_line = line = file->line;
3207 state->macro_file = file;
3208
3209 next_token(state, index);
3210 ident_to_macro(state, tk);
3211 if (tk->tok == TOK_IDENT) {
3212 error(state, 0, "undefined preprocessing directive `%s'",
3213 tk->ident->name);
3214 }
3215 switch(tk->tok) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00003216 case TOK_LIT_INT:
3217 {
3218 int override_line;
3219 override_line = strtoul(tk->val.str, 0, 10);
3220 next_token(state, index);
3221 /* I have a cpp line marker parse it */
3222 if (tk->tok == TOK_LIT_STRING) {
3223 const char *token, *base;
3224 char *name, *dir;
3225 int name_len, dir_len;
3226 name = xmalloc(tk->str_len, "report_name");
3227 token = tk->val.str + 1;
3228 base = strrchr(token, '/');
3229 name_len = tk->str_len -2;
3230 if (base != 0) {
3231 dir_len = base - token;
3232 base++;
3233 name_len -= base - token;
3234 } else {
3235 dir_len = 0;
3236 base = token;
3237 }
3238 memcpy(name, base, name_len);
3239 name[name_len] = '\0';
3240 dir = xmalloc(dir_len + 1, "report_dir");
3241 memcpy(dir, token, dir_len);
3242 dir[dir_len] = '\0';
3243 file->report_line = override_line - 1;
3244 file->report_name = name;
3245 file->report_dir = dir;
3246 }
3247 }
3248 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003249 case TOK_LINE:
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00003250 meat(state, index, TOK_LINE);
3251 meat(state, index, TOK_LIT_INT);
3252 file->report_line = strtoul(tk->val.str, 0, 10) -1;
3253 if (mpeek(state, index) == TOK_LIT_STRING) {
3254 const char *token, *base;
3255 char *name, *dir;
3256 int name_len, dir_len;
3257 meat(state, index, TOK_LIT_STRING);
3258 name = xmalloc(tk->str_len, "report_name");
3259 token = tk->val.str + 1;
3260 name_len = tk->str_len - 2;
3261 if (base != 0) {
3262 dir_len = base - token;
3263 base++;
3264 name_len -= base - token;
3265 } else {
3266 dir_len = 0;
3267 base = token;
3268 }
3269 memcpy(name, base, name_len);
3270 name[name_len] = '\0';
3271 dir = xmalloc(dir_len + 1, "report_dir");
3272 memcpy(dir, token, dir_len);
3273 dir[dir_len] = '\0';
3274 file->report_name = name;
3275 file->report_dir = dir;
3276 }
3277 break;
3278 case TOK_UNDEF:
Eric Biedermanb138ac82003-04-22 18:44:01 +00003279 case TOK_PRAGMA:
3280 if (state->if_value < 0) {
3281 break;
3282 }
3283 warning(state, 0, "Ignoring preprocessor directive: %s",
3284 tk->ident->name);
3285 break;
3286 case TOK_ELIF:
3287 error(state, 0, "#elif not supported");
3288#warning "FIXME multiple #elif and #else in an #if do not work properly"
3289 if (state->if_depth == 0) {
3290 error(state, 0, "#elif without #if");
3291 }
3292 /* If the #if was taken the #elif just disables the following code */
3293 if (state->if_value >= 0) {
3294 state->if_value = - state->if_value;
3295 }
3296 /* If the previous #if was not taken see if the #elif enables the
3297 * trailing code.
3298 */
3299 else if ((state->if_value < 0) &&
3300 (state->if_depth == - state->if_value))
3301 {
3302 if (mcexpr(state, index) != 0) {
3303 state->if_value = state->if_depth;
3304 }
3305 else {
3306 state->if_value = - state->if_depth;
3307 }
3308 }
3309 break;
3310 case TOK_IF:
3311 state->if_depth++;
3312 if (state->if_value < 0) {
3313 break;
3314 }
3315 if (mcexpr(state, index) != 0) {
3316 state->if_value = state->if_depth;
3317 }
3318 else {
3319 state->if_value = - state->if_depth;
3320 }
3321 break;
3322 case TOK_IFNDEF:
3323 state->if_depth++;
3324 if (state->if_value < 0) {
3325 break;
3326 }
3327 next_token(state, index);
3328 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3329 error(state, 0, "Invalid macro name");
3330 }
3331 if (tk->ident->sym_define == 0) {
3332 state->if_value = state->if_depth;
3333 }
3334 else {
3335 state->if_value = - state->if_depth;
3336 }
3337 break;
3338 case TOK_IFDEF:
3339 state->if_depth++;
3340 if (state->if_value < 0) {
3341 break;
3342 }
3343 next_token(state, index);
3344 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3345 error(state, 0, "Invalid macro name");
3346 }
3347 if (tk->ident->sym_define != 0) {
3348 state->if_value = state->if_depth;
3349 }
3350 else {
3351 state->if_value = - state->if_depth;
3352 }
3353 break;
3354 case TOK_ELSE:
3355 if (state->if_depth == 0) {
3356 error(state, 0, "#else without #if");
3357 }
3358 if ((state->if_value >= 0) ||
3359 ((state->if_value < 0) &&
3360 (state->if_depth == -state->if_value)))
3361 {
3362 state->if_value = - state->if_value;
3363 }
3364 break;
3365 case TOK_ENDIF:
3366 if (state->if_depth == 0) {
3367 error(state, 0, "#endif without #if");
3368 }
3369 if ((state->if_value >= 0) ||
3370 ((state->if_value < 0) &&
3371 (state->if_depth == -state->if_value)))
3372 {
3373 state->if_value = state->if_depth - 1;
3374 }
3375 state->if_depth--;
3376 break;
3377 case TOK_DEFINE:
3378 {
3379 struct hash_entry *ident;
3380 struct macro *macro;
3381 char *ptr;
3382
3383 if (state->if_value < 0) /* quit early when #if'd out */
3384 break;
3385
3386 meat(state, index, TOK_IDENT);
3387 ident = tk->ident;
3388
3389
3390 if (*file->pos == '(') {
3391#warning "FIXME macros with arguments not supported"
3392 error(state, 0, "Macros with arguments not supported");
3393 }
3394
3395 /* Find the end of the line to get an estimate of
3396 * the macro's length.
3397 */
3398 for(ptr = file->pos; *ptr != '\n'; ptr++)
3399 ;
3400
3401 if (ident->sym_define != 0) {
3402 error(state, 0, "macro %s already defined\n", ident->name);
3403 }
3404 macro = xmalloc(sizeof(*macro), "macro");
3405 macro->ident = ident;
3406 macro->buf_len = ptr - file->pos +1;
3407 macro->buf = xmalloc(macro->buf_len +2, "macro buf");
3408
3409 memcpy(macro->buf, file->pos, macro->buf_len);
3410 macro->buf[macro->buf_len] = '\n';
3411 macro->buf[macro->buf_len +1] = '\0';
3412
3413 ident->sym_define = macro;
3414 break;
3415 }
3416 case TOK_ERROR:
3417 {
3418 char *end;
3419 int len;
3420 /* Find the end of the line */
3421 for(end = file->pos; *end != '\n'; end++)
3422 ;
3423 len = (end - file->pos);
3424 if (state->if_value >= 0) {
3425 error(state, 0, "%*.*s", len, len, file->pos);
3426 }
3427 file->pos = end;
3428 break;
3429 }
3430 case TOK_WARNING:
3431 {
3432 char *end;
3433 int len;
3434 /* Find the end of the line */
3435 for(end = file->pos; *end != '\n'; end++)
3436 ;
3437 len = (end - file->pos);
3438 if (state->if_value >= 0) {
3439 warning(state, 0, "%*.*s", len, len, file->pos);
3440 }
3441 file->pos = end;
3442 break;
3443 }
3444 case TOK_INCLUDE:
3445 {
3446 char *name;
3447 char *ptr;
3448 int local;
3449 local = 0;
3450 name = 0;
3451 next_token(state, index);
3452 if (tk->tok == TOK_LIT_STRING) {
3453 const char *token;
3454 int name_len;
3455 name = xmalloc(tk->str_len, "include");
3456 token = tk->val.str +1;
3457 name_len = tk->str_len -2;
3458 if (*token == '"') {
3459 token++;
3460 name_len--;
3461 }
3462 memcpy(name, token, name_len);
3463 name[name_len] = '\0';
3464 local = 1;
3465 }
3466 else if (tk->tok == TOK_LESS) {
3467 char *start, *end;
3468 start = file->pos;
3469 for(end = start; *end != '\n'; end++) {
3470 if (*end == '>') {
3471 break;
3472 }
3473 }
3474 if (*end == '\n') {
3475 error(state, 0, "Unterminated included directive");
3476 }
3477 name = xmalloc(end - start + 1, "include");
3478 memcpy(name, start, end - start);
3479 name[end - start] = '\0';
3480 file->pos = end +1;
3481 local = 0;
3482 }
3483 else {
3484 error(state, 0, "Invalid include directive");
3485 }
3486 /* Error if there are any characters after the include */
3487 for(ptr = file->pos; *ptr != '\n'; ptr++) {
Eric Biederman8d9c1232003-06-17 08:42:17 +00003488 switch(*ptr) {
3489 case ' ':
3490 case '\t':
3491 case '\v':
3492 break;
3493 default:
Eric Biedermanb138ac82003-04-22 18:44:01 +00003494 error(state, 0, "garbage after include directive");
3495 }
3496 }
3497 if (state->if_value >= 0) {
3498 compile_file(state, name, local);
3499 }
3500 xfree(name);
3501 next_token(state, index);
3502 return;
3503 }
3504 default:
3505 /* Ignore # without a following ident */
3506 if (tk->tok == TOK_IDENT) {
3507 error(state, 0, "Invalid preprocessor directive: %s",
3508 tk->ident->name);
3509 }
3510 break;
3511 }
3512 /* Consume the rest of the macro line */
3513 do {
3514 tok = mpeek(state, index);
3515 meat(state, index, tok);
3516 } while(tok != TOK_EOF);
3517 return;
3518}
3519
3520static void token(struct compile_state *state, int index)
3521{
3522 struct file_state *file;
3523 struct token *tk;
3524 int rescan;
3525
3526 tk = &state->token[index];
3527 next_token(state, index);
3528 do {
3529 rescan = 0;
3530 file = state->file;
3531 if (tk->tok == TOK_EOF && file->prev) {
3532 state->file = file->prev;
3533 /* file->basename is used keep it */
3534 xfree(file->dirname);
3535 xfree(file->buf);
3536 xfree(file);
3537 next_token(state, index);
3538 rescan = 1;
3539 }
3540 else if (tk->tok == TOK_MACRO) {
3541 preprocess(state, index);
3542 rescan = 1;
3543 }
3544 else if (tk->ident && tk->ident->sym_define) {
3545 compile_macro(state, tk);
3546 next_token(state, index);
3547 rescan = 1;
3548 }
3549 else if (state->if_value < 0) {
3550 next_token(state, index);
3551 rescan = 1;
3552 }
3553 } while(rescan);
3554}
3555
3556static int peek(struct compile_state *state)
3557{
3558 if (state->token[1].tok == -1) {
3559 token(state, 1);
3560 }
3561 return state->token[1].tok;
3562}
3563
3564static int peek2(struct compile_state *state)
3565{
3566 if (state->token[1].tok == -1) {
3567 token(state, 1);
3568 }
3569 if (state->token[2].tok == -1) {
3570 token(state, 2);
3571 }
3572 return state->token[2].tok;
3573}
3574
Eric Biederman0babc1c2003-05-09 02:39:00 +00003575static void eat(struct compile_state *state, int tok)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003576{
3577 int next_tok;
3578 int i;
3579 next_tok = peek(state);
3580 if (next_tok != tok) {
3581 const char *name1, *name2;
3582 name1 = tokens[next_tok];
3583 name2 = "";
3584 if (next_tok == TOK_IDENT) {
3585 name2 = state->token[1].ident->name;
3586 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00003587 error(state, 0, "\tfound %s %s expected %s",
3588 name1, name2 ,tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003589 }
3590 /* Free the old token value */
3591 if (state->token[0].str_len) {
3592 xfree((void *)(state->token[0].val.str));
3593 }
3594 for(i = 0; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
3595 state->token[i] = state->token[i + 1];
3596 }
3597 memset(&state->token[i], 0, sizeof(state->token[i]));
3598 state->token[i].tok = -1;
3599}
Eric Biedermanb138ac82003-04-22 18:44:01 +00003600
3601#warning "FIXME do not hardcode the include paths"
3602static char *include_paths[] = {
3603 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/include",
3604 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/arch/i386/include",
3605 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src",
3606 0
3607};
3608
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003609static void compile_file(struct compile_state *state, const char *filename, int local)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003610{
3611 char cwd[4096];
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003612 const char *subdir, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003613 int subdir_len;
3614 struct file_state *file;
3615 char *basename;
3616 file = xmalloc(sizeof(*file), "file_state");
3617
3618 base = strrchr(filename, '/');
3619 subdir = filename;
3620 if (base != 0) {
3621 subdir_len = base - filename;
3622 base++;
3623 }
3624 else {
3625 base = filename;
3626 subdir_len = 0;
3627 }
3628 basename = xmalloc(strlen(base) +1, "basename");
3629 strcpy(basename, base);
3630 file->basename = basename;
3631
3632 if (getcwd(cwd, sizeof(cwd)) == 0) {
3633 die("cwd buffer to small");
3634 }
3635
3636 if (subdir[0] == '/') {
3637 file->dirname = xmalloc(subdir_len + 1, "dirname");
3638 memcpy(file->dirname, subdir, subdir_len);
3639 file->dirname[subdir_len] = '\0';
3640 }
3641 else {
3642 char *dir;
3643 int dirlen;
3644 char **path;
3645 /* Find the appropriate directory... */
3646 dir = 0;
3647 if (!state->file && exists(cwd, filename)) {
3648 dir = cwd;
3649 }
3650 if (local && state->file && exists(state->file->dirname, filename)) {
3651 dir = state->file->dirname;
3652 }
3653 for(path = include_paths; !dir && *path; path++) {
3654 if (exists(*path, filename)) {
3655 dir = *path;
3656 }
3657 }
3658 if (!dir) {
3659 error(state, 0, "Cannot find `%s'\n", filename);
3660 }
3661 dirlen = strlen(dir);
3662 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
3663 memcpy(file->dirname, dir, dirlen);
3664 file->dirname[dirlen] = '/';
3665 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
3666 file->dirname[dirlen + 1 + subdir_len] = '\0';
3667 }
3668 file->buf = slurp_file(file->dirname, file->basename, &file->size);
3669 xchdir(cwd);
3670
3671 file->pos = file->buf;
3672 file->line_start = file->pos;
3673 file->line = 1;
3674
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00003675 file->report_line = 1;
3676 file->report_name = file->basename;
3677 file->report_dir = file->dirname;
3678
Eric Biedermanb138ac82003-04-22 18:44:01 +00003679 file->prev = state->file;
3680 state->file = file;
3681
3682 process_trigraphs(state);
3683 splice_lines(state);
3684}
3685
Eric Biederman0babc1c2003-05-09 02:39:00 +00003686/* Type helper functions */
Eric Biedermanb138ac82003-04-22 18:44:01 +00003687
3688static struct type *new_type(
3689 unsigned int type, struct type *left, struct type *right)
3690{
3691 struct type *result;
3692 result = xmalloc(sizeof(*result), "type");
3693 result->type = type;
3694 result->left = left;
3695 result->right = right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00003696 result->field_ident = 0;
3697 result->type_ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003698 return result;
3699}
3700
3701static struct type *clone_type(unsigned int specifiers, struct type *old)
3702{
3703 struct type *result;
3704 result = xmalloc(sizeof(*result), "type");
3705 memcpy(result, old, sizeof(*result));
3706 result->type &= TYPE_MASK;
3707 result->type |= specifiers;
3708 return result;
3709}
3710
3711#define SIZEOF_SHORT 2
3712#define SIZEOF_INT 4
3713#define SIZEOF_LONG (sizeof(long_t))
3714
3715#define ALIGNOF_SHORT 2
3716#define ALIGNOF_INT 4
3717#define ALIGNOF_LONG (sizeof(long_t))
3718
3719#define MASK_UCHAR(X) ((X) & ((ulong_t)0xff))
3720#define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT*8)) - 1))
3721static inline ulong_t mask_uint(ulong_t x)
3722{
3723 if (SIZEOF_INT < SIZEOF_LONG) {
3724 ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT*8))) -1;
3725 x &= mask;
3726 }
3727 return x;
3728}
3729#define MASK_UINT(X) (mask_uint(X))
3730#define MASK_ULONG(X) (X)
3731
Eric Biedermanb138ac82003-04-22 18:44:01 +00003732static struct type void_type = { .type = TYPE_VOID };
3733static struct type char_type = { .type = TYPE_CHAR };
3734static struct type uchar_type = { .type = TYPE_UCHAR };
3735static struct type short_type = { .type = TYPE_SHORT };
3736static struct type ushort_type = { .type = TYPE_USHORT };
3737static struct type int_type = { .type = TYPE_INT };
3738static struct type uint_type = { .type = TYPE_UINT };
3739static struct type long_type = { .type = TYPE_LONG };
3740static struct type ulong_type = { .type = TYPE_ULONG };
3741
3742static struct triple *variable(struct compile_state *state, struct type *type)
3743{
3744 struct triple *result;
3745 if ((type->type & STOR_MASK) != STOR_PERM) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00003746 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3747 result = triple(state, OP_ADECL, type, 0, 0);
3748 } else {
3749 struct type *field;
3750 struct triple **vector;
3751 ulong_t index;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003752 result = new_triple(state, OP_VAL_VEC, type, -1, -1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00003753 vector = &result->param[0];
3754
3755 field = type->left;
3756 index = 0;
3757 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
3758 vector[index] = variable(state, field->left);
3759 field = field->right;
3760 index++;
3761 }
3762 vector[index] = variable(state, field);
3763 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003764 }
3765 else {
3766 result = triple(state, OP_SDECL, type, 0, 0);
3767 }
3768 return result;
3769}
3770
3771static void stor_of(FILE *fp, struct type *type)
3772{
3773 switch(type->type & STOR_MASK) {
3774 case STOR_AUTO:
3775 fprintf(fp, "auto ");
3776 break;
3777 case STOR_STATIC:
3778 fprintf(fp, "static ");
3779 break;
3780 case STOR_EXTERN:
3781 fprintf(fp, "extern ");
3782 break;
3783 case STOR_REGISTER:
3784 fprintf(fp, "register ");
3785 break;
3786 case STOR_TYPEDEF:
3787 fprintf(fp, "typedef ");
3788 break;
3789 case STOR_INLINE:
3790 fprintf(fp, "inline ");
3791 break;
3792 }
3793}
3794static void qual_of(FILE *fp, struct type *type)
3795{
3796 if (type->type & QUAL_CONST) {
3797 fprintf(fp, " const");
3798 }
3799 if (type->type & QUAL_VOLATILE) {
3800 fprintf(fp, " volatile");
3801 }
3802 if (type->type & QUAL_RESTRICT) {
3803 fprintf(fp, " restrict");
3804 }
3805}
Eric Biederman0babc1c2003-05-09 02:39:00 +00003806
Eric Biedermanb138ac82003-04-22 18:44:01 +00003807static void name_of(FILE *fp, struct type *type)
3808{
3809 stor_of(fp, type);
3810 switch(type->type & TYPE_MASK) {
3811 case TYPE_VOID:
3812 fprintf(fp, "void");
3813 qual_of(fp, type);
3814 break;
3815 case TYPE_CHAR:
3816 fprintf(fp, "signed char");
3817 qual_of(fp, type);
3818 break;
3819 case TYPE_UCHAR:
3820 fprintf(fp, "unsigned char");
3821 qual_of(fp, type);
3822 break;
3823 case TYPE_SHORT:
3824 fprintf(fp, "signed short");
3825 qual_of(fp, type);
3826 break;
3827 case TYPE_USHORT:
3828 fprintf(fp, "unsigned short");
3829 qual_of(fp, type);
3830 break;
3831 case TYPE_INT:
3832 fprintf(fp, "signed int");
3833 qual_of(fp, type);
3834 break;
3835 case TYPE_UINT:
3836 fprintf(fp, "unsigned int");
3837 qual_of(fp, type);
3838 break;
3839 case TYPE_LONG:
3840 fprintf(fp, "signed long");
3841 qual_of(fp, type);
3842 break;
3843 case TYPE_ULONG:
3844 fprintf(fp, "unsigned long");
3845 qual_of(fp, type);
3846 break;
3847 case TYPE_POINTER:
3848 name_of(fp, type->left);
3849 fprintf(fp, " * ");
3850 qual_of(fp, type);
3851 break;
3852 case TYPE_PRODUCT:
3853 case TYPE_OVERLAP:
3854 name_of(fp, type->left);
3855 fprintf(fp, ", ");
3856 name_of(fp, type->right);
3857 break;
3858 case TYPE_ENUM:
Eric Biederman0babc1c2003-05-09 02:39:00 +00003859 fprintf(fp, "enum %s", type->type_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003860 qual_of(fp, type);
3861 break;
3862 case TYPE_STRUCT:
Eric Biederman0babc1c2003-05-09 02:39:00 +00003863 fprintf(fp, "struct %s", type->type_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003864 qual_of(fp, type);
3865 break;
3866 case TYPE_FUNCTION:
3867 {
3868 name_of(fp, type->left);
3869 fprintf(fp, " (*)(");
3870 name_of(fp, type->right);
3871 fprintf(fp, ")");
3872 break;
3873 }
3874 case TYPE_ARRAY:
3875 name_of(fp, type->left);
3876 fprintf(fp, " [%ld]", type->elements);
3877 break;
3878 default:
3879 fprintf(fp, "????: %x", type->type & TYPE_MASK);
3880 break;
3881 }
3882}
3883
3884static size_t align_of(struct compile_state *state, struct type *type)
3885{
3886 size_t align;
3887 align = 0;
3888 switch(type->type & TYPE_MASK) {
3889 case TYPE_VOID:
3890 align = 1;
3891 break;
3892 case TYPE_CHAR:
3893 case TYPE_UCHAR:
3894 align = 1;
3895 break;
3896 case TYPE_SHORT:
3897 case TYPE_USHORT:
3898 align = ALIGNOF_SHORT;
3899 break;
3900 case TYPE_INT:
3901 case TYPE_UINT:
3902 case TYPE_ENUM:
3903 align = ALIGNOF_INT;
3904 break;
3905 case TYPE_LONG:
3906 case TYPE_ULONG:
3907 case TYPE_POINTER:
3908 align = ALIGNOF_LONG;
3909 break;
3910 case TYPE_PRODUCT:
3911 case TYPE_OVERLAP:
3912 {
3913 size_t left_align, right_align;
3914 left_align = align_of(state, type->left);
3915 right_align = align_of(state, type->right);
3916 align = (left_align >= right_align) ? left_align : right_align;
3917 break;
3918 }
3919 case TYPE_ARRAY:
3920 align = align_of(state, type->left);
3921 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00003922 case TYPE_STRUCT:
3923 align = align_of(state, type->left);
3924 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003925 default:
3926 error(state, 0, "alignof not yet defined for type\n");
3927 break;
3928 }
3929 return align;
3930}
3931
3932static size_t size_of(struct compile_state *state, struct type *type)
3933{
3934 size_t size;
3935 size = 0;
3936 switch(type->type & TYPE_MASK) {
3937 case TYPE_VOID:
3938 size = 0;
3939 break;
3940 case TYPE_CHAR:
3941 case TYPE_UCHAR:
3942 size = 1;
3943 break;
3944 case TYPE_SHORT:
3945 case TYPE_USHORT:
3946 size = SIZEOF_SHORT;
3947 break;
3948 case TYPE_INT:
3949 case TYPE_UINT:
3950 case TYPE_ENUM:
3951 size = SIZEOF_INT;
3952 break;
3953 case TYPE_LONG:
3954 case TYPE_ULONG:
3955 case TYPE_POINTER:
3956 size = SIZEOF_LONG;
3957 break;
3958 case TYPE_PRODUCT:
3959 {
3960 size_t align, pad;
3961 size = size_of(state, type->left);
3962 while((type->right->type & TYPE_MASK) == TYPE_PRODUCT) {
3963 type = type->right;
3964 align = align_of(state, type->left);
3965 pad = align - (size % align);
3966 size = size + pad + size_of(state, type->left);
3967 }
3968 align = align_of(state, type->right);
3969 pad = align - (size % align);
3970 size = size + pad + sizeof(type->right);
3971 break;
3972 }
3973 case TYPE_OVERLAP:
3974 {
3975 size_t size_left, size_right;
3976 size_left = size_of(state, type->left);
3977 size_right = size_of(state, type->right);
3978 size = (size_left >= size_right)? size_left : size_right;
3979 break;
3980 }
3981 case TYPE_ARRAY:
3982 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
3983 internal_error(state, 0, "Invalid array type");
3984 } else {
3985 size = size_of(state, type->left) * type->elements;
3986 }
3987 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00003988 case TYPE_STRUCT:
3989 size = size_of(state, type->left);
3990 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003991 default:
3992 error(state, 0, "sizeof not yet defined for type\n");
3993 break;
3994 }
3995 return size;
3996}
3997
Eric Biederman0babc1c2003-05-09 02:39:00 +00003998static size_t field_offset(struct compile_state *state,
3999 struct type *type, struct hash_entry *field)
4000{
4001 size_t size, align, pad;
4002 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4003 internal_error(state, 0, "field_offset only works on structures");
4004 }
4005 size = 0;
4006 type = type->left;
4007 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4008 if (type->left->field_ident == field) {
4009 type = type->left;
4010 }
4011 size += size_of(state, type->left);
4012 type = type->right;
4013 align = align_of(state, type->left);
4014 pad = align - (size % align);
4015 size += pad;
4016 }
4017 if (type->field_ident != field) {
4018 internal_error(state, 0, "field_offset: member %s not present",
4019 field->name);
4020 }
4021 return size;
4022}
4023
4024static struct type *field_type(struct compile_state *state,
4025 struct type *type, struct hash_entry *field)
4026{
4027 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4028 internal_error(state, 0, "field_type only works on structures");
4029 }
4030 type = type->left;
4031 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4032 if (type->left->field_ident == field) {
4033 type = type->left;
4034 break;
4035 }
4036 type = type->right;
4037 }
4038 if (type->field_ident != field) {
4039 internal_error(state, 0, "field_type: member %s not present",
4040 field->name);
4041 }
4042 return type;
4043}
4044
4045static struct triple *struct_field(struct compile_state *state,
4046 struct triple *decl, struct hash_entry *field)
4047{
4048 struct triple **vector;
4049 struct type *type;
4050 ulong_t index;
4051 type = decl->type;
4052 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4053 return decl;
4054 }
4055 if (decl->op != OP_VAL_VEC) {
4056 internal_error(state, 0, "Invalid struct variable");
4057 }
4058 if (!field) {
4059 internal_error(state, 0, "Missing structure field");
4060 }
4061 type = type->left;
4062 vector = &RHS(decl, 0);
4063 index = 0;
4064 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4065 if (type->left->field_ident == field) {
4066 type = type->left;
4067 break;
4068 }
4069 index += 1;
4070 type = type->right;
4071 }
4072 if (type->field_ident != field) {
4073 internal_error(state, 0, "field %s not found?", field->name);
4074 }
4075 return vector[index];
4076}
4077
Eric Biedermanb138ac82003-04-22 18:44:01 +00004078static void arrays_complete(struct compile_state *state, struct type *type)
4079{
4080 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
4081 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4082 error(state, 0, "array size not specified");
4083 }
4084 arrays_complete(state, type->left);
4085 }
4086}
4087
4088static unsigned int do_integral_promotion(unsigned int type)
4089{
4090 type &= TYPE_MASK;
4091 if (TYPE_INTEGER(type) &&
4092 TYPE_RANK(type) < TYPE_RANK(TYPE_INT)) {
4093 type = TYPE_INT;
4094 }
4095 return type;
4096}
4097
4098static unsigned int do_arithmetic_conversion(
4099 unsigned int left, unsigned int right)
4100{
4101 left &= TYPE_MASK;
4102 right &= TYPE_MASK;
4103 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
4104 return TYPE_LDOUBLE;
4105 }
4106 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
4107 return TYPE_DOUBLE;
4108 }
4109 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
4110 return TYPE_FLOAT;
4111 }
4112 left = do_integral_promotion(left);
4113 right = do_integral_promotion(right);
4114 /* If both operands have the same size done */
4115 if (left == right) {
4116 return left;
4117 }
4118 /* If both operands have the same signedness pick the larger */
4119 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
4120 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
4121 }
4122 /* If the signed type can hold everything use it */
4123 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
4124 return left;
4125 }
4126 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
4127 return right;
4128 }
4129 /* Convert to the unsigned type with the same rank as the signed type */
4130 else if (TYPE_SIGNED(left)) {
4131 return TYPE_MKUNSIGNED(left);
4132 }
4133 else {
4134 return TYPE_MKUNSIGNED(right);
4135 }
4136}
4137
4138/* see if two types are the same except for qualifiers */
4139static int equiv_types(struct type *left, struct type *right)
4140{
4141 unsigned int type;
4142 /* Error if the basic types do not match */
4143 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4144 return 0;
4145 }
4146 type = left->type & TYPE_MASK;
4147 /* if the basic types match and it is an arithmetic type we are done */
4148 if (TYPE_ARITHMETIC(type)) {
4149 return 1;
4150 }
4151 /* If it is a pointer type recurse and keep testing */
4152 if (type == TYPE_POINTER) {
4153 return equiv_types(left->left, right->left);
4154 }
4155 else if (type == TYPE_ARRAY) {
4156 return (left->elements == right->elements) &&
4157 equiv_types(left->left, right->left);
4158 }
4159 /* test for struct/union equality */
4160 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004161 return left->type_ident == right->type_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004162 }
4163 /* Test for equivalent functions */
4164 else if (type == TYPE_FUNCTION) {
4165 return equiv_types(left->left, right->left) &&
4166 equiv_types(left->right, right->right);
4167 }
4168 /* We only see TYPE_PRODUCT as part of function equivalence matching */
4169 else if (type == TYPE_PRODUCT) {
4170 return equiv_types(left->left, right->left) &&
4171 equiv_types(left->right, right->right);
4172 }
4173 /* We should see TYPE_OVERLAP */
4174 else {
4175 return 0;
4176 }
4177}
4178
4179static int equiv_ptrs(struct type *left, struct type *right)
4180{
4181 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4182 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4183 return 0;
4184 }
4185 return equiv_types(left->left, right->left);
4186}
4187
4188static struct type *compatible_types(struct type *left, struct type *right)
4189{
4190 struct type *result;
4191 unsigned int type, qual_type;
4192 /* Error if the basic types do not match */
4193 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4194 return 0;
4195 }
4196 type = left->type & TYPE_MASK;
4197 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4198 result = 0;
4199 /* if the basic types match and it is an arithmetic type we are done */
4200 if (TYPE_ARITHMETIC(type)) {
4201 result = new_type(qual_type, 0, 0);
4202 }
4203 /* If it is a pointer type recurse and keep testing */
4204 else if (type == TYPE_POINTER) {
4205 result = compatible_types(left->left, right->left);
4206 if (result) {
4207 result = new_type(qual_type, result, 0);
4208 }
4209 }
4210 /* test for struct/union equality */
4211 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004212 if (left->type_ident == right->type_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004213 result = left;
4214 }
4215 }
4216 /* Test for equivalent functions */
4217 else if (type == TYPE_FUNCTION) {
4218 struct type *lf, *rf;
4219 lf = compatible_types(left->left, right->left);
4220 rf = compatible_types(left->right, right->right);
4221 if (lf && rf) {
4222 result = new_type(qual_type, lf, rf);
4223 }
4224 }
4225 /* We only see TYPE_PRODUCT as part of function equivalence matching */
4226 else if (type == TYPE_PRODUCT) {
4227 struct type *lf, *rf;
4228 lf = compatible_types(left->left, right->left);
4229 rf = compatible_types(left->right, right->right);
4230 if (lf && rf) {
4231 result = new_type(qual_type, lf, rf);
4232 }
4233 }
4234 else {
4235 /* Nothing else is compatible */
4236 }
4237 return result;
4238}
4239
4240static struct type *compatible_ptrs(struct type *left, struct type *right)
4241{
4242 struct type *result;
4243 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4244 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4245 return 0;
4246 }
4247 result = compatible_types(left->left, right->left);
4248 if (result) {
4249 unsigned int qual_type;
4250 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4251 result = new_type(qual_type, result, 0);
4252 }
4253 return result;
4254
4255}
4256static struct triple *integral_promotion(
4257 struct compile_state *state, struct triple *def)
4258{
4259 struct type *type;
4260 type = def->type;
4261 /* As all operations are carried out in registers
4262 * the values are converted on load I just convert
4263 * logical type of the operand.
4264 */
4265 if (TYPE_INTEGER(type->type)) {
4266 unsigned int int_type;
4267 int_type = type->type & ~TYPE_MASK;
4268 int_type |= do_integral_promotion(type->type);
4269 if (int_type != type->type) {
4270 def->type = new_type(int_type, 0, 0);
4271 }
4272 }
4273 return def;
4274}
4275
4276
4277static void arithmetic(struct compile_state *state, struct triple *def)
4278{
4279 if (!TYPE_ARITHMETIC(def->type->type)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004280 error(state, 0, "arithmetic type expexted");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004281 }
4282}
4283
4284static void ptr_arithmetic(struct compile_state *state, struct triple *def)
4285{
4286 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
4287 error(state, def, "pointer or arithmetic type expected");
4288 }
4289}
4290
4291static int is_integral(struct triple *ins)
4292{
4293 return TYPE_INTEGER(ins->type->type);
4294}
4295
4296static void integral(struct compile_state *state, struct triple *def)
4297{
4298 if (!is_integral(def)) {
4299 error(state, 0, "integral type expected");
4300 }
4301}
4302
4303
4304static void bool(struct compile_state *state, struct triple *def)
4305{
4306 if (!TYPE_ARITHMETIC(def->type->type) &&
4307 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
4308 error(state, 0, "arithmetic or pointer type expected");
4309 }
4310}
4311
4312static int is_signed(struct type *type)
4313{
4314 return !!TYPE_SIGNED(type->type);
4315}
4316
Eric Biederman0babc1c2003-05-09 02:39:00 +00004317/* Is this value located in a register otherwise it must be in memory */
4318static int is_in_reg(struct compile_state *state, struct triple *def)
4319{
4320 int in_reg;
4321 if (def->op == OP_ADECL) {
4322 in_reg = 1;
4323 }
4324 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
4325 in_reg = 0;
4326 }
4327 else if (def->op == OP_VAL_VEC) {
4328 in_reg = is_in_reg(state, RHS(def, 0));
4329 }
4330 else if (def->op == OP_DOT) {
4331 in_reg = is_in_reg(state, RHS(def, 0));
4332 }
4333 else {
4334 internal_error(state, 0, "unknown expr storage location");
4335 in_reg = -1;
4336 }
4337 return in_reg;
4338}
4339
Eric Biedermanb138ac82003-04-22 18:44:01 +00004340/* Is this a stable variable location otherwise it must be a temporary */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004341static int is_stable(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004342{
4343 int ret;
4344 ret = 0;
4345 if (!def) {
4346 return 0;
4347 }
4348 if ((def->op == OP_ADECL) ||
4349 (def->op == OP_SDECL) ||
4350 (def->op == OP_DEREF) ||
4351 (def->op == OP_BLOBCONST)) {
4352 ret = 1;
4353 }
4354 else if (def->op == OP_DOT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004355 ret = is_stable(state, RHS(def, 0));
4356 }
4357 else if (def->op == OP_VAL_VEC) {
4358 struct triple **vector;
4359 ulong_t i;
4360 ret = 1;
4361 vector = &RHS(def, 0);
4362 for(i = 0; i < def->type->elements; i++) {
4363 if (!is_stable(state, vector[i])) {
4364 ret = 0;
4365 break;
4366 }
4367 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004368 }
4369 return ret;
4370}
4371
Eric Biederman0babc1c2003-05-09 02:39:00 +00004372static int is_lvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004373{
4374 int ret;
4375 ret = 1;
4376 if (!def) {
4377 return 0;
4378 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004379 if (!is_stable(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004380 return 0;
4381 }
4382 if (def->type->type & QUAL_CONST) {
4383 ret = 0;
4384 }
4385 else if (def->op == OP_DOT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004386 ret = is_lvalue(state, RHS(def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00004387 }
4388 return ret;
4389}
4390
4391static void lvalue(struct compile_state *state, struct triple *def)
4392{
4393 if (!def) {
4394 internal_error(state, def, "nothing where lvalue expected?");
4395 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004396 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004397 error(state, def, "lvalue expected");
4398 }
4399}
4400
4401static int is_pointer(struct triple *def)
4402{
4403 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
4404}
4405
4406static void pointer(struct compile_state *state, struct triple *def)
4407{
4408 if (!is_pointer(def)) {
4409 error(state, def, "pointer expected");
4410 }
4411}
4412
4413static struct triple *int_const(
4414 struct compile_state *state, struct type *type, ulong_t value)
4415{
4416 struct triple *result;
4417 switch(type->type & TYPE_MASK) {
4418 case TYPE_CHAR:
4419 case TYPE_INT: case TYPE_UINT:
4420 case TYPE_LONG: case TYPE_ULONG:
4421 break;
4422 default:
4423 internal_error(state, 0, "constant for unkown type");
4424 }
4425 result = triple(state, OP_INTCONST, type, 0, 0);
4426 result->u.cval = value;
4427 return result;
4428}
4429
4430
Eric Biederman0babc1c2003-05-09 02:39:00 +00004431static struct triple *do_mk_addr_expr(struct compile_state *state,
4432 struct triple *expr, struct type *type, ulong_t offset)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004433{
4434 struct triple *result;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004435 lvalue(state, expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004436
4437 result = 0;
4438 if (expr->op == OP_ADECL) {
4439 error(state, expr, "address of auto variables not supported");
4440 }
4441 else if (expr->op == OP_SDECL) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004442 result = triple(state, OP_ADDRCONST, type, 0, 0);
4443 MISC(result, 0) = expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004444 result->u.cval = offset;
4445 }
4446 else if (expr->op == OP_DEREF) {
4447 result = triple(state, OP_ADD, type,
Eric Biederman0babc1c2003-05-09 02:39:00 +00004448 RHS(expr, 0),
Eric Biedermanb138ac82003-04-22 18:44:01 +00004449 int_const(state, &ulong_type, offset));
4450 }
4451 return result;
4452}
4453
Eric Biederman0babc1c2003-05-09 02:39:00 +00004454static struct triple *mk_addr_expr(
4455 struct compile_state *state, struct triple *expr, ulong_t offset)
4456{
4457 struct type *type;
4458
4459 type = new_type(
4460 TYPE_POINTER | (expr->type->type & QUAL_MASK),
4461 expr->type, 0);
4462
4463 return do_mk_addr_expr(state, expr, type, offset);
4464}
4465
Eric Biedermanb138ac82003-04-22 18:44:01 +00004466static struct triple *mk_deref_expr(
4467 struct compile_state *state, struct triple *expr)
4468{
4469 struct type *base_type;
4470 pointer(state, expr);
4471 base_type = expr->type->left;
4472 if (!TYPE_PTR(base_type->type) && !TYPE_ARITHMETIC(base_type->type)) {
4473 error(state, 0,
4474 "Only pointer and arithmetic values can be dereferenced");
4475 }
4476 return triple(state, OP_DEREF, base_type, expr, 0);
4477}
4478
Eric Biederman0babc1c2003-05-09 02:39:00 +00004479static struct triple *deref_field(
4480 struct compile_state *state, struct triple *expr, struct hash_entry *field)
4481{
4482 struct triple *result;
4483 struct type *type, *member;
4484 if (!field) {
4485 internal_error(state, 0, "No field passed to deref_field");
4486 }
4487 result = 0;
4488 type = expr->type;
4489 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4490 error(state, 0, "request for member %s in something not a struct or union",
4491 field->name);
4492 }
4493 member = type->left;
4494 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4495 if (member->left->field_ident == field) {
4496 member = member->left;
4497 break;
4498 }
4499 member = member->right;
4500 }
4501 if (member->field_ident != field) {
4502 error(state, 0, "%s is not a member", field->name);
4503 }
4504 if ((type->type & STOR_MASK) == STOR_PERM) {
4505 /* Do the pointer arithmetic to get a deref the field */
4506 ulong_t offset;
4507 offset = field_offset(state, type, field);
4508 result = do_mk_addr_expr(state, expr, member, offset);
4509 result = mk_deref_expr(state, result);
4510 }
4511 else {
4512 /* Find the variable for the field I want. */
4513 result = triple(state, OP_DOT,
4514 field_type(state, type, field), expr, 0);
4515 result->u.field = field;
4516 }
4517 return result;
4518}
4519
Eric Biedermanb138ac82003-04-22 18:44:01 +00004520static struct triple *read_expr(struct compile_state *state, struct triple *def)
4521{
4522 int op;
4523 if (!def) {
4524 return 0;
4525 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004526 if (!is_stable(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004527 return def;
4528 }
4529 /* Tranform an array to a pointer to the first element */
4530#warning "CHECK_ME is this the right place to transform arrays to pointers?"
4531 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
4532 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004533 struct triple *result;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004534 type = new_type(
4535 TYPE_POINTER | (def->type->type & QUAL_MASK),
4536 def->type->left, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004537 result = triple(state, OP_ADDRCONST, type, 0, 0);
4538 MISC(result, 0) = def;
4539 return result;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004540 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004541 if (is_in_reg(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004542 op = OP_READ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004543 } else {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004544 op = OP_LOAD;
4545 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004546 return triple(state, op, def->type, def, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004547}
4548
4549static void write_compatible(struct compile_state *state,
4550 struct type *dest, struct type *rval)
4551{
4552 int compatible = 0;
4553 /* Both operands have arithmetic type */
4554 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
4555 compatible = 1;
4556 }
4557 /* One operand is a pointer and the other is a pointer to void */
4558 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
4559 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
4560 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
4561 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
4562 compatible = 1;
4563 }
4564 /* If both types are the same without qualifiers we are good */
4565 else if (equiv_ptrs(dest, rval)) {
4566 compatible = 1;
4567 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004568 /* test for struct/union equality */
4569 else if (((dest->type & TYPE_MASK) == TYPE_STRUCT) &&
4570 ((rval->type & TYPE_MASK) == TYPE_STRUCT) &&
4571 (dest->type_ident == rval->type_ident)) {
4572 compatible = 1;
4573 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004574 if (!compatible) {
4575 error(state, 0, "Incompatible types in assignment");
4576 }
4577}
4578
4579static struct triple *write_expr(
4580 struct compile_state *state, struct triple *dest, struct triple *rval)
4581{
4582 struct triple *def;
4583 int op;
4584
4585 def = 0;
4586 if (!rval) {
4587 internal_error(state, 0, "missing rval");
4588 }
4589
4590 if (rval->op == OP_LIST) {
4591 internal_error(state, 0, "expression of type OP_LIST?");
4592 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004593 if (!is_lvalue(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004594 internal_error(state, 0, "writing to a non lvalue?");
4595 }
4596
4597 write_compatible(state, dest->type, rval->type);
4598
4599 /* Now figure out which assignment operator to use */
4600 op = -1;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004601 if (is_in_reg(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004602 op = OP_WRITE;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004603 } else {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004604 op = OP_STORE;
4605 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004606 def = triple(state, op, dest->type, dest, rval);
4607 return def;
4608}
4609
4610static struct triple *init_expr(
4611 struct compile_state *state, struct triple *dest, struct triple *rval)
4612{
4613 struct triple *def;
4614
4615 def = 0;
4616 if (!rval) {
4617 internal_error(state, 0, "missing rval");
4618 }
4619 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
4620 rval = read_expr(state, rval);
4621 def = write_expr(state, dest, rval);
4622 }
4623 else {
4624 /* Fill in the array size if necessary */
4625 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
4626 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
4627 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4628 dest->type->elements = rval->type->elements;
4629 }
4630 }
4631 if (!equiv_types(dest->type, rval->type)) {
4632 error(state, 0, "Incompatible types in inializer");
4633 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004634 MISC(dest, 0) = rval;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004635 insert_triple(state, dest, rval);
4636 rval->id |= TRIPLE_FLAG_FLATTENED;
4637 use_triple(MISC(dest, 0), dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004638 }
4639 return def;
4640}
4641
4642struct type *arithmetic_result(
4643 struct compile_state *state, struct triple *left, struct triple *right)
4644{
4645 struct type *type;
4646 /* Sanity checks to ensure I am working with arithmetic types */
4647 arithmetic(state, left);
4648 arithmetic(state, right);
4649 type = new_type(
4650 do_arithmetic_conversion(
4651 left->type->type,
4652 right->type->type), 0, 0);
4653 return type;
4654}
4655
4656struct type *ptr_arithmetic_result(
4657 struct compile_state *state, struct triple *left, struct triple *right)
4658{
4659 struct type *type;
4660 /* Sanity checks to ensure I am working with the proper types */
4661 ptr_arithmetic(state, left);
4662 arithmetic(state, right);
4663 if (TYPE_ARITHMETIC(left->type->type) &&
4664 TYPE_ARITHMETIC(right->type->type)) {
4665 type = arithmetic_result(state, left, right);
4666 }
4667 else if (TYPE_PTR(left->type->type)) {
4668 type = left->type;
4669 }
4670 else {
4671 internal_error(state, 0, "huh?");
4672 type = 0;
4673 }
4674 return type;
4675}
4676
4677
4678/* boolean helper function */
4679
4680static struct triple *ltrue_expr(struct compile_state *state,
4681 struct triple *expr)
4682{
4683 switch(expr->op) {
4684 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
4685 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
4686 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
4687 /* If the expression is already boolean do nothing */
4688 break;
4689 default:
4690 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
4691 break;
4692 }
4693 return expr;
4694}
4695
4696static struct triple *lfalse_expr(struct compile_state *state,
4697 struct triple *expr)
4698{
4699 return triple(state, OP_LFALSE, &int_type, expr, 0);
4700}
4701
4702static struct triple *cond_expr(
4703 struct compile_state *state,
4704 struct triple *test, struct triple *left, struct triple *right)
4705{
4706 struct triple *def;
4707 struct type *result_type;
4708 unsigned int left_type, right_type;
4709 bool(state, test);
4710 left_type = left->type->type;
4711 right_type = right->type->type;
4712 result_type = 0;
4713 /* Both operands have arithmetic type */
4714 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
4715 result_type = arithmetic_result(state, left, right);
4716 }
4717 /* Both operands have void type */
4718 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
4719 ((right_type & TYPE_MASK) == TYPE_VOID)) {
4720 result_type = &void_type;
4721 }
4722 /* pointers to the same type... */
4723 else if ((result_type = compatible_ptrs(left->type, right->type))) {
4724 ;
4725 }
4726 /* Both operands are pointers and left is a pointer to void */
4727 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4728 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4729 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4730 result_type = right->type;
4731 }
4732 /* Both operands are pointers and right is a pointer to void */
4733 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4734 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4735 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4736 result_type = left->type;
4737 }
4738 if (!result_type) {
4739 error(state, 0, "Incompatible types in conditional expression");
4740 }
Eric Biederman30276382003-05-16 20:47:48 +00004741 /* Cleanup and invert the test */
4742 test = lfalse_expr(state, read_expr(state, test));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004743 def = new_triple(state, OP_COND, result_type, 0, 3);
Eric Biederman0babc1c2003-05-09 02:39:00 +00004744 def->param[0] = test;
4745 def->param[1] = left;
4746 def->param[2] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004747 return def;
4748}
4749
4750
Eric Biederman0babc1c2003-05-09 02:39:00 +00004751static int expr_depth(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004752{
4753 int count;
4754 count = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004755 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
4756 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004757 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004758 else if (ins->op == OP_DEREF) {
4759 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004760 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004761 else if (ins->op == OP_VAL) {
4762 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004763 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004764 else if (ins->op == OP_COMMA) {
4765 int ldepth, rdepth;
4766 ldepth = expr_depth(state, RHS(ins, 0));
4767 rdepth = expr_depth(state, RHS(ins, 1));
4768 count = (ldepth >= rdepth)? ldepth : rdepth;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004769 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004770 else if (ins->op == OP_CALL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004771 /* Don't figure the depth of a call just guess it is huge */
4772 count = 1000;
4773 }
4774 else {
4775 struct triple **expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004776 expr = triple_rhs(state, ins, 0);
4777 for(;expr; expr = triple_rhs(state, ins, expr)) {
4778 if (*expr) {
4779 int depth;
4780 depth = expr_depth(state, *expr);
4781 if (depth > count) {
4782 count = depth;
4783 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004784 }
4785 }
4786 }
4787 return count + 1;
4788}
4789
4790static struct triple *flatten(
4791 struct compile_state *state, struct triple *first, struct triple *ptr);
4792
Eric Biederman0babc1c2003-05-09 02:39:00 +00004793static struct triple *flatten_generic(
Eric Biedermanb138ac82003-04-22 18:44:01 +00004794 struct compile_state *state, struct triple *first, struct triple *ptr)
4795{
Eric Biederman0babc1c2003-05-09 02:39:00 +00004796 struct rhs_vector {
4797 int depth;
4798 struct triple **ins;
4799 } vector[MAX_RHS];
4800 int i, rhs, lhs;
4801 /* Only operations with just a rhs should come here */
4802 rhs = TRIPLE_RHS(ptr->sizes);
4803 lhs = TRIPLE_LHS(ptr->sizes);
4804 if (TRIPLE_SIZE(ptr->sizes) != lhs + rhs) {
4805 internal_error(state, ptr, "unexpected args for: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +00004806 ptr->op, tops(ptr->op));
4807 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004808 /* Find the depth of the rhs elements */
4809 for(i = 0; i < rhs; i++) {
4810 vector[i].ins = &RHS(ptr, i);
4811 vector[i].depth = expr_depth(state, *vector[i].ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004812 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004813 /* Selection sort the rhs */
4814 for(i = 0; i < rhs; i++) {
4815 int j, max = i;
4816 for(j = i + 1; j < rhs; j++ ) {
4817 if (vector[j].depth > vector[max].depth) {
4818 max = j;
4819 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004820 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004821 if (max != i) {
4822 struct rhs_vector tmp;
4823 tmp = vector[i];
4824 vector[i] = vector[max];
4825 vector[max] = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004826 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004827 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004828 /* Now flatten the rhs elements */
4829 for(i = 0; i < rhs; i++) {
4830 *vector[i].ins = flatten(state, first, *vector[i].ins);
4831 use_triple(*vector[i].ins, ptr);
4832 }
4833
4834 /* Now flatten the lhs elements */
4835 for(i = 0; i < lhs; i++) {
4836 struct triple **ins = &LHS(ptr, i);
4837 *ins = flatten(state, first, *ins);
4838 use_triple(*ins, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004839 }
4840 return ptr;
4841}
4842
4843static struct triple *flatten_land(
4844 struct compile_state *state, struct triple *first, struct triple *ptr)
4845{
4846 struct triple *left, *right;
4847 struct triple *val, *test, *jmp, *label1, *end;
4848
4849 /* Find the triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004850 left = RHS(ptr, 0);
4851 right = RHS(ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004852
4853 /* Generate the needed triples */
4854 end = label(state);
4855
4856 /* Thread the triples together */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004857 val = flatten(state, first, variable(state, ptr->type));
4858 left = flatten(state, first, write_expr(state, val, left));
4859 test = flatten(state, first,
Eric Biedermanb138ac82003-04-22 18:44:01 +00004860 lfalse_expr(state, read_expr(state, val)));
Eric Biederman0babc1c2003-05-09 02:39:00 +00004861 jmp = flatten(state, first, branch(state, end, test));
4862 label1 = flatten(state, first, label(state));
4863 right = flatten(state, first, write_expr(state, val, right));
4864 TARG(jmp, 0) = flatten(state, first, end);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004865
4866 /* Now give the caller something to chew on */
4867 return read_expr(state, val);
4868}
4869
4870static struct triple *flatten_lor(
4871 struct compile_state *state, struct triple *first, struct triple *ptr)
4872{
4873 struct triple *left, *right;
4874 struct triple *val, *jmp, *label1, *end;
4875
4876 /* Find the triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004877 left = RHS(ptr, 0);
4878 right = RHS(ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004879
4880 /* Generate the needed triples */
4881 end = label(state);
4882
4883 /* Thread the triples together */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004884 val = flatten(state, first, variable(state, ptr->type));
4885 left = flatten(state, first, write_expr(state, val, left));
4886 jmp = flatten(state, first, branch(state, end, left));
4887 label1 = flatten(state, first, label(state));
4888 right = flatten(state, first, write_expr(state, val, right));
4889 TARG(jmp, 0) = flatten(state, first, end);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004890
4891
4892 /* Now give the caller something to chew on */
4893 return read_expr(state, val);
4894}
4895
4896static struct triple *flatten_cond(
4897 struct compile_state *state, struct triple *first, struct triple *ptr)
4898{
4899 struct triple *test, *left, *right;
4900 struct triple *val, *mv1, *jmp1, *label1, *mv2, *middle, *jmp2, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004901
4902 /* Find the triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004903 test = RHS(ptr, 0);
4904 left = RHS(ptr, 1);
4905 right = RHS(ptr, 2);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004906
4907 /* Generate the needed triples */
4908 end = label(state);
4909 middle = label(state);
4910
4911 /* Thread the triples together */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004912 val = flatten(state, first, variable(state, ptr->type));
4913 test = flatten(state, first, test);
4914 jmp1 = flatten(state, first, branch(state, middle, test));
4915 label1 = flatten(state, first, label(state));
4916 left = flatten(state, first, left);
4917 mv1 = flatten(state, first, write_expr(state, val, left));
4918 jmp2 = flatten(state, first, branch(state, end, 0));
4919 TARG(jmp1, 0) = flatten(state, first, middle);
4920 right = flatten(state, first, right);
4921 mv2 = flatten(state, first, write_expr(state, val, right));
4922 TARG(jmp2, 0) = flatten(state, first, end);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004923
4924 /* Now give the caller something to chew on */
4925 return read_expr(state, val);
4926}
4927
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004928struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
4929 struct occurance *base_occurance)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004930{
4931 struct triple *nfunc;
4932 struct triple *nfirst, *ofirst;
4933 struct triple *new, *old;
4934
4935#if 0
4936 fprintf(stdout, "\n");
4937 loc(stdout, state, 0);
4938 fprintf(stdout, "\n__________ copy_func _________\n");
4939 print_triple(state, ofunc);
4940 fprintf(stdout, "__________ copy_func _________ done\n\n");
4941#endif
4942
4943 /* Make a new copy of the old function */
4944 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
4945 nfirst = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004946 ofirst = old = RHS(ofunc, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004947 do {
4948 struct triple *new;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004949 struct occurance *occurance;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004950 int old_lhs, old_rhs;
4951 old_lhs = TRIPLE_LHS(old->sizes);
Eric Biederman0babc1c2003-05-09 02:39:00 +00004952 old_rhs = TRIPLE_RHS(old->sizes);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004953 occurance = inline_occurance(state, base_occurance, old->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004954 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004955 occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004956 if (!triple_stores_block(state, new)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004957 memcpy(&new->u, &old->u, sizeof(new->u));
4958 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004959 if (!nfirst) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004960 RHS(nfunc, 0) = nfirst = new;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004961 }
4962 else {
4963 insert_triple(state, nfirst, new);
4964 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004965 new->id |= TRIPLE_FLAG_FLATTENED;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004966
4967 /* During the copy remember new as user of old */
4968 use_triple(old, new);
4969
4970 /* Populate the return type if present */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004971 if (old == MISC(ofunc, 0)) {
4972 MISC(nfunc, 0) = new;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004973 }
4974 old = old->next;
4975 } while(old != ofirst);
4976
4977 /* Make a second pass to fix up any unresolved references */
4978 old = ofirst;
4979 new = nfirst;
4980 do {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004981 struct triple **oexpr, **nexpr;
4982 int count, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004983 /* Lookup where the copy is, to join pointers */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004984 count = TRIPLE_SIZE(old->sizes);
4985 for(i = 0; i < count; i++) {
4986 oexpr = &old->param[i];
4987 nexpr = &new->param[i];
4988 if (!*nexpr && *oexpr && (*oexpr)->use) {
4989 *nexpr = (*oexpr)->use->member;
4990 if (*nexpr == old) {
4991 internal_error(state, 0, "new == old?");
4992 }
4993 use_triple(*nexpr, new);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004994 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004995 if (!*nexpr && *oexpr) {
4996 internal_error(state, 0, "Could not copy %d\n", i);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004997 }
4998 }
4999 old = old->next;
5000 new = new->next;
5001 } while((old != ofirst) && (new != nfirst));
5002
5003 /* Make a third pass to cleanup the extra useses */
5004 old = ofirst;
5005 new = nfirst;
5006 do {
5007 unuse_triple(old, new);
5008 old = old->next;
5009 new = new->next;
5010 } while ((old != ofirst) && (new != nfirst));
5011 return nfunc;
5012}
5013
5014static struct triple *flatten_call(
5015 struct compile_state *state, struct triple *first, struct triple *ptr)
5016{
5017 /* Inline the function call */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005018 struct type *ptype;
5019 struct triple *ofunc, *nfunc, *nfirst, *param, *result;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005020 struct triple *end, *nend;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005021 int pvals, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005022
5023 /* Find the triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005024 ofunc = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005025 if (ofunc->op != OP_LIST) {
5026 internal_error(state, 0, "improper function");
5027 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005028 nfunc = copy_func(state, ofunc, ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005029 nfirst = RHS(nfunc, 0)->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005030 /* Prepend the parameter reading into the new function list */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005031 ptype = nfunc->type->right;
5032 param = RHS(nfunc, 0)->next;
5033 pvals = TRIPLE_RHS(ptr->sizes);
5034 for(i = 0; i < pvals; i++) {
5035 struct type *atype;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005036 struct triple *arg;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005037 atype = ptype;
5038 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
5039 atype = ptype->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005040 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005041 while((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
5042 param = param->next;
5043 }
5044 arg = RHS(ptr, i);
5045 flatten(state, nfirst, write_expr(state, param, arg));
5046 ptype = ptype->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005047 param = param->next;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005048 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005049 result = 0;
5050 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00005051 result = read_expr(state, MISC(nfunc,0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005052 }
5053#if 0
5054 fprintf(stdout, "\n");
5055 loc(stdout, state, 0);
5056 fprintf(stdout, "\n__________ flatten_call _________\n");
5057 print_triple(state, nfunc);
5058 fprintf(stdout, "__________ flatten_call _________ done\n\n");
5059#endif
5060
5061 /* Get rid of the extra triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005062 nfirst = RHS(nfunc, 0)->next;
5063 free_triple(state, RHS(nfunc, 0));
5064 RHS(nfunc, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005065 free_triple(state, nfunc);
5066
5067 /* Append the new function list onto the return list */
5068 end = first->prev;
5069 nend = nfirst->prev;
5070 end->next = nfirst;
5071 nfirst->prev = end;
5072 nend->next = first;
5073 first->prev = nend;
5074
5075 return result;
5076}
5077
5078static struct triple *flatten(
5079 struct compile_state *state, struct triple *first, struct triple *ptr)
5080{
5081 struct triple *orig_ptr;
5082 if (!ptr)
5083 return 0;
5084 do {
5085 orig_ptr = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005086 /* Only flatten triples once */
5087 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
5088 return ptr;
5089 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005090 switch(ptr->op) {
5091 case OP_WRITE:
5092 case OP_STORE:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005093 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5094 LHS(ptr, 0) = flatten(state, first, LHS(ptr, 0));
5095 use_triple(LHS(ptr, 0), ptr);
5096 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005097 break;
5098 case OP_COMMA:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005099 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5100 ptr = RHS(ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005101 break;
5102 case OP_VAL:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005103 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5104 return MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005105 break;
5106 case OP_LAND:
5107 ptr = flatten_land(state, first, ptr);
5108 break;
5109 case OP_LOR:
5110 ptr = flatten_lor(state, first, ptr);
5111 break;
5112 case OP_COND:
5113 ptr = flatten_cond(state, first, ptr);
5114 break;
5115 case OP_CALL:
5116 ptr = flatten_call(state, first, ptr);
5117 break;
5118 case OP_READ:
5119 case OP_LOAD:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005120 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5121 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005122 break;
5123 case OP_BRANCH:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005124 use_triple(TARG(ptr, 0), ptr);
5125 if (TRIPLE_RHS(ptr->sizes)) {
5126 use_triple(RHS(ptr, 0), ptr);
5127 if (ptr->next != ptr) {
5128 use_triple(ptr->next, ptr);
5129 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005130 }
5131 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005132 case OP_BLOBCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005133 insert_triple(state, first, ptr);
5134 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005135 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005136 use_triple(MISC(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005137 break;
5138 case OP_DEREF:
5139 /* Since OP_DEREF is just a marker delete it when I flatten it */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005140 ptr = RHS(ptr, 0);
5141 RHS(orig_ptr, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005142 free_triple(state, orig_ptr);
5143 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005144 case OP_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005145 {
5146 struct triple *base;
5147 base = RHS(ptr, 0);
5148 base = flatten(state, first, base);
5149 if (base->op == OP_VAL_VEC) {
5150 ptr = struct_field(state, base, ptr->u.field);
5151 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005152 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005153 }
Eric Biederman8d9c1232003-06-17 08:42:17 +00005154 case OP_PIECE:
5155 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5156 use_triple(MISC(ptr, 0), ptr);
5157 use_triple(ptr, MISC(ptr, 0));
5158 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005159 case OP_ADDRCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005160 case OP_SDECL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005161 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5162 use_triple(MISC(ptr, 0), ptr);
5163 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005164 case OP_ADECL:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005165 break;
5166 default:
5167 /* Flatten the easy cases we don't override */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005168 ptr = flatten_generic(state, first, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005169 break;
5170 }
5171 } while(ptr && (ptr != orig_ptr));
Eric Biederman0babc1c2003-05-09 02:39:00 +00005172 if (ptr) {
5173 insert_triple(state, first, ptr);
5174 ptr->id |= TRIPLE_FLAG_FLATTENED;
5175 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005176 return ptr;
5177}
5178
5179static void release_expr(struct compile_state *state, struct triple *expr)
5180{
5181 struct triple *head;
5182 head = label(state);
5183 flatten(state, head, expr);
5184 while(head->next != head) {
5185 release_triple(state, head->next);
5186 }
5187 free_triple(state, head);
5188}
5189
5190static int replace_rhs_use(struct compile_state *state,
5191 struct triple *orig, struct triple *new, struct triple *use)
5192{
5193 struct triple **expr;
5194 int found;
5195 found = 0;
5196 expr = triple_rhs(state, use, 0);
5197 for(;expr; expr = triple_rhs(state, use, expr)) {
5198 if (*expr == orig) {
5199 *expr = new;
5200 found = 1;
5201 }
5202 }
5203 if (found) {
5204 unuse_triple(orig, use);
5205 use_triple(new, use);
5206 }
5207 return found;
5208}
5209
5210static int replace_lhs_use(struct compile_state *state,
5211 struct triple *orig, struct triple *new, struct triple *use)
5212{
5213 struct triple **expr;
5214 int found;
5215 found = 0;
5216 expr = triple_lhs(state, use, 0);
5217 for(;expr; expr = triple_lhs(state, use, expr)) {
5218 if (*expr == orig) {
5219 *expr = new;
5220 found = 1;
5221 }
5222 }
5223 if (found) {
5224 unuse_triple(orig, use);
5225 use_triple(new, use);
5226 }
5227 return found;
5228}
5229
5230static void propogate_use(struct compile_state *state,
5231 struct triple *orig, struct triple *new)
5232{
5233 struct triple_set *user, *next;
5234 for(user = orig->use; user; user = next) {
5235 struct triple *use;
5236 int found;
5237 next = user->next;
5238 use = user->member;
5239 found = 0;
5240 found |= replace_rhs_use(state, orig, new, use);
5241 found |= replace_lhs_use(state, orig, new, use);
5242 if (!found) {
5243 internal_error(state, use, "use without use");
5244 }
5245 }
5246 if (orig->use) {
5247 internal_error(state, orig, "used after propogate_use");
5248 }
5249}
5250
5251/*
5252 * Code generators
5253 * ===========================
5254 */
5255
5256static struct triple *mk_add_expr(
5257 struct compile_state *state, struct triple *left, struct triple *right)
5258{
5259 struct type *result_type;
5260 /* Put pointer operands on the left */
5261 if (is_pointer(right)) {
5262 struct triple *tmp;
5263 tmp = left;
5264 left = right;
5265 right = tmp;
5266 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005267 left = read_expr(state, left);
5268 right = read_expr(state, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005269 result_type = ptr_arithmetic_result(state, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005270 if (is_pointer(left)) {
5271 right = triple(state,
5272 is_signed(right->type)? OP_SMUL : OP_UMUL,
5273 &ulong_type,
5274 right,
5275 int_const(state, &ulong_type,
5276 size_of(state, left->type->left)));
5277 }
5278 return triple(state, OP_ADD, result_type, left, right);
5279}
5280
5281static struct triple *mk_sub_expr(
5282 struct compile_state *state, struct triple *left, struct triple *right)
5283{
5284 struct type *result_type;
5285 result_type = ptr_arithmetic_result(state, left, right);
5286 left = read_expr(state, left);
5287 right = read_expr(state, right);
5288 if (is_pointer(left)) {
5289 right = triple(state,
5290 is_signed(right->type)? OP_SMUL : OP_UMUL,
5291 &ulong_type,
5292 right,
5293 int_const(state, &ulong_type,
5294 size_of(state, left->type->left)));
5295 }
5296 return triple(state, OP_SUB, result_type, left, right);
5297}
5298
5299static struct triple *mk_pre_inc_expr(
5300 struct compile_state *state, struct triple *def)
5301{
5302 struct triple *val;
5303 lvalue(state, def);
5304 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
5305 return triple(state, OP_VAL, def->type,
5306 write_expr(state, def, val),
5307 val);
5308}
5309
5310static struct triple *mk_pre_dec_expr(
5311 struct compile_state *state, struct triple *def)
5312{
5313 struct triple *val;
5314 lvalue(state, def);
5315 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
5316 return triple(state, OP_VAL, def->type,
5317 write_expr(state, def, val),
5318 val);
5319}
5320
5321static struct triple *mk_post_inc_expr(
5322 struct compile_state *state, struct triple *def)
5323{
5324 struct triple *val;
5325 lvalue(state, def);
5326 val = read_expr(state, def);
5327 return triple(state, OP_VAL, def->type,
5328 write_expr(state, def,
5329 mk_add_expr(state, val, int_const(state, &int_type, 1)))
5330 , val);
5331}
5332
5333static struct triple *mk_post_dec_expr(
5334 struct compile_state *state, struct triple *def)
5335{
5336 struct triple *val;
5337 lvalue(state, def);
5338 val = read_expr(state, def);
5339 return triple(state, OP_VAL, def->type,
5340 write_expr(state, def,
5341 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
5342 , val);
5343}
5344
5345static struct triple *mk_subscript_expr(
5346 struct compile_state *state, struct triple *left, struct triple *right)
5347{
5348 left = read_expr(state, left);
5349 right = read_expr(state, right);
5350 if (!is_pointer(left) && !is_pointer(right)) {
5351 error(state, left, "subscripted value is not a pointer");
5352 }
5353 return mk_deref_expr(state, mk_add_expr(state, left, right));
5354}
5355
5356/*
5357 * Compile time evaluation
5358 * ===========================
5359 */
5360static int is_const(struct triple *ins)
5361{
5362 return IS_CONST_OP(ins->op);
5363}
5364
5365static int constants_equal(struct compile_state *state,
5366 struct triple *left, struct triple *right)
5367{
5368 int equal;
5369 if (!is_const(left) || !is_const(right)) {
5370 equal = 0;
5371 }
5372 else if (left->op != right->op) {
5373 equal = 0;
5374 }
5375 else if (!equiv_types(left->type, right->type)) {
5376 equal = 0;
5377 }
5378 else {
5379 equal = 0;
5380 switch(left->op) {
5381 case OP_INTCONST:
5382 if (left->u.cval == right->u.cval) {
5383 equal = 1;
5384 }
5385 break;
5386 case OP_BLOBCONST:
5387 {
5388 size_t lsize, rsize;
5389 lsize = size_of(state, left->type);
5390 rsize = size_of(state, right->type);
5391 if (lsize != rsize) {
5392 break;
5393 }
5394 if (memcmp(left->u.blob, right->u.blob, lsize) == 0) {
5395 equal = 1;
5396 }
5397 break;
5398 }
5399 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005400 if ((MISC(left, 0) == MISC(right, 0)) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00005401 (left->u.cval == right->u.cval)) {
5402 equal = 1;
5403 }
5404 break;
5405 default:
5406 internal_error(state, left, "uknown constant type");
5407 break;
5408 }
5409 }
5410 return equal;
5411}
5412
5413static int is_zero(struct triple *ins)
5414{
5415 return is_const(ins) && (ins->u.cval == 0);
5416}
5417
5418static int is_one(struct triple *ins)
5419{
5420 return is_const(ins) && (ins->u.cval == 1);
5421}
5422
5423static long_t bsr(ulong_t value)
5424{
5425 int i;
5426 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
5427 ulong_t mask;
5428 mask = 1;
5429 mask <<= i;
5430 if (value & mask) {
5431 return i;
5432 }
5433 }
5434 return -1;
5435}
5436
5437static long_t bsf(ulong_t value)
5438{
5439 int i;
5440 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
5441 ulong_t mask;
5442 mask = 1;
5443 mask <<= 1;
5444 if (value & mask) {
5445 return i;
5446 }
5447 }
5448 return -1;
5449}
5450
5451static long_t log2(ulong_t value)
5452{
5453 return bsr(value);
5454}
5455
5456static long_t tlog2(struct triple *ins)
5457{
5458 return log2(ins->u.cval);
5459}
5460
5461static int is_pow2(struct triple *ins)
5462{
5463 ulong_t value, mask;
5464 long_t log;
5465 if (!is_const(ins)) {
5466 return 0;
5467 }
5468 value = ins->u.cval;
5469 log = log2(value);
5470 if (log == -1) {
5471 return 0;
5472 }
5473 mask = 1;
5474 mask <<= log;
5475 return ((value & mask) == value);
5476}
5477
5478static ulong_t read_const(struct compile_state *state,
5479 struct triple *ins, struct triple **expr)
5480{
5481 struct triple *rhs;
5482 rhs = *expr;
5483 switch(rhs->type->type &TYPE_MASK) {
5484 case TYPE_CHAR:
5485 case TYPE_SHORT:
5486 case TYPE_INT:
5487 case TYPE_LONG:
5488 case TYPE_UCHAR:
5489 case TYPE_USHORT:
5490 case TYPE_UINT:
5491 case TYPE_ULONG:
5492 case TYPE_POINTER:
5493 break;
5494 default:
5495 internal_error(state, rhs, "bad type to read_const\n");
5496 break;
5497 }
5498 return rhs->u.cval;
5499}
5500
5501static long_t read_sconst(struct triple *ins, struct triple **expr)
5502{
5503 struct triple *rhs;
5504 rhs = *expr;
5505 return (long_t)(rhs->u.cval);
5506}
5507
5508static void unuse_rhs(struct compile_state *state, struct triple *ins)
5509{
5510 struct triple **expr;
5511 expr = triple_rhs(state, ins, 0);
5512 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00005513 if (*expr) {
5514 unuse_triple(*expr, ins);
5515 *expr = 0;
5516 }
5517 }
5518}
5519
5520static void unuse_lhs(struct compile_state *state, struct triple *ins)
5521{
5522 struct triple **expr;
5523 expr = triple_lhs(state, ins, 0);
5524 for(;expr;expr = triple_lhs(state, ins, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005525 unuse_triple(*expr, ins);
5526 *expr = 0;
5527 }
5528}
Eric Biederman0babc1c2003-05-09 02:39:00 +00005529
Eric Biedermanb138ac82003-04-22 18:44:01 +00005530static void check_lhs(struct compile_state *state, struct triple *ins)
5531{
5532 struct triple **expr;
5533 expr = triple_lhs(state, ins, 0);
5534 for(;expr;expr = triple_lhs(state, ins, expr)) {
5535 internal_error(state, ins, "unexpected lhs");
5536 }
5537
5538}
5539static void check_targ(struct compile_state *state, struct triple *ins)
5540{
5541 struct triple **expr;
5542 expr = triple_targ(state, ins, 0);
5543 for(;expr;expr = triple_targ(state, ins, expr)) {
5544 internal_error(state, ins, "unexpected targ");
5545 }
5546}
5547
5548static void wipe_ins(struct compile_state *state, struct triple *ins)
5549{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005550 /* Becareful which instructions you replace the wiped
5551 * instruction with, as there are not enough slots
5552 * in all instructions to hold all others.
5553 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005554 check_targ(state, ins);
5555 unuse_rhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005556 unuse_lhs(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005557}
5558
5559static void mkcopy(struct compile_state *state,
5560 struct triple *ins, struct triple *rhs)
5561{
5562 wipe_ins(state, ins);
5563 ins->op = OP_COPY;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005564 ins->sizes = TRIPLE_SIZES(0, 1, 0, 0);
5565 RHS(ins, 0) = rhs;
5566 use_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005567}
5568
5569static void mkconst(struct compile_state *state,
5570 struct triple *ins, ulong_t value)
5571{
5572 if (!is_integral(ins) && !is_pointer(ins)) {
5573 internal_error(state, ins, "unknown type to make constant\n");
5574 }
5575 wipe_ins(state, ins);
5576 ins->op = OP_INTCONST;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005577 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005578 ins->u.cval = value;
5579}
5580
5581static void mkaddr_const(struct compile_state *state,
5582 struct triple *ins, struct triple *sdecl, ulong_t value)
5583{
5584 wipe_ins(state, ins);
5585 ins->op = OP_ADDRCONST;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005586 ins->sizes = TRIPLE_SIZES(0, 0, 1, 0);
5587 MISC(ins, 0) = sdecl;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005588 ins->u.cval = value;
5589 use_triple(sdecl, ins);
5590}
5591
Eric Biederman0babc1c2003-05-09 02:39:00 +00005592/* Transform multicomponent variables into simple register variables */
5593static void flatten_structures(struct compile_state *state)
5594{
5595 struct triple *ins, *first;
5596 first = RHS(state->main_function, 0);
5597 ins = first;
5598 /* Pass one expand structure values into valvecs.
5599 */
5600 ins = first;
5601 do {
5602 struct triple *next;
5603 next = ins->next;
5604 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5605 if (ins->op == OP_VAL_VEC) {
5606 /* Do nothing */
5607 }
5608 else if ((ins->op == OP_LOAD) || (ins->op == OP_READ)) {
5609 struct triple *def, **vector;
5610 struct type *tptr;
5611 int op;
5612 ulong_t i;
5613
5614 op = ins->op;
5615 def = RHS(ins, 0);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005616 get_occurance(ins->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005617 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005618 ins->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005619
5620 vector = &RHS(next, 0);
5621 tptr = next->type->left;
5622 for(i = 0; i < next->type->elements; i++) {
5623 struct triple *sfield;
5624 struct type *mtype;
5625 mtype = tptr;
5626 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5627 mtype = mtype->left;
5628 }
5629 sfield = deref_field(state, def, mtype->field_ident);
5630
5631 vector[i] = triple(
5632 state, op, mtype, sfield, 0);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005633 put_occurance(vector[i]->occurance);
5634 get_occurance(next->occurance);
5635 vector[i]->occurance = next->occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005636 tptr = tptr->right;
5637 }
5638 propogate_use(state, ins, next);
5639 flatten(state, ins, next);
5640 free_triple(state, ins);
5641 }
5642 else if ((ins->op == OP_STORE) || (ins->op == OP_WRITE)) {
5643 struct triple *src, *dst, **vector;
5644 struct type *tptr;
5645 int op;
5646 ulong_t i;
5647
5648 op = ins->op;
5649 src = RHS(ins, 0);
5650 dst = LHS(ins, 0);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005651 get_occurance(ins->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005652 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005653 ins->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005654
5655 vector = &RHS(next, 0);
5656 tptr = next->type->left;
5657 for(i = 0; i < ins->type->elements; i++) {
5658 struct triple *dfield, *sfield;
5659 struct type *mtype;
5660 mtype = tptr;
5661 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5662 mtype = mtype->left;
5663 }
5664 sfield = deref_field(state, src, mtype->field_ident);
5665 dfield = deref_field(state, dst, mtype->field_ident);
5666 vector[i] = triple(
5667 state, op, mtype, dfield, sfield);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005668 put_occurance(vector[i]->occurance);
5669 get_occurance(next->occurance);
5670 vector[i]->occurance = next->occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005671 tptr = tptr->right;
5672 }
5673 propogate_use(state, ins, next);
5674 flatten(state, ins, next);
5675 free_triple(state, ins);
5676 }
5677 }
5678 ins = next;
5679 } while(ins != first);
5680 /* Pass two flatten the valvecs.
5681 */
5682 ins = first;
5683 do {
5684 struct triple *next;
5685 next = ins->next;
5686 if (ins->op == OP_VAL_VEC) {
5687 release_triple(state, ins);
5688 }
5689 ins = next;
5690 } while(ins != first);
5691 /* Pass three verify the state and set ->id to 0.
5692 */
5693 ins = first;
5694 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005695 ins->id &= ~TRIPLE_FLAG_FLATTENED;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005696 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5697 internal_error(state, 0, "STRUCT_TYPE remains?");
5698 }
5699 if (ins->op == OP_DOT) {
5700 internal_error(state, 0, "OP_DOT remains?");
5701 }
5702 if (ins->op == OP_VAL_VEC) {
5703 internal_error(state, 0, "OP_VAL_VEC remains?");
5704 }
5705 ins = ins->next;
5706 } while(ins != first);
5707}
5708
Eric Biedermanb138ac82003-04-22 18:44:01 +00005709/* For those operations that cannot be simplified */
5710static void simplify_noop(struct compile_state *state, struct triple *ins)
5711{
5712 return;
5713}
5714
5715static void simplify_smul(struct compile_state *state, struct triple *ins)
5716{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005717 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005718 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005719 tmp = RHS(ins, 0);
5720 RHS(ins, 0) = RHS(ins, 1);
5721 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005722 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005723 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005724 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005725 left = read_sconst(ins, &RHS(ins, 0));
5726 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005727 mkconst(state, ins, left * right);
5728 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005729 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005730 mkconst(state, ins, 0);
5731 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005732 else if (is_one(RHS(ins, 1))) {
5733 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005734 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005735 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005736 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005737 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005738 ins->op = OP_SL;
5739 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005740 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005741 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005742 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005743 }
5744}
5745
5746static void simplify_umul(struct compile_state *state, struct triple *ins)
5747{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005748 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005749 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005750 tmp = RHS(ins, 0);
5751 RHS(ins, 0) = RHS(ins, 1);
5752 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005753 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005754 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005755 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005756 left = read_const(state, ins, &RHS(ins, 0));
5757 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005758 mkconst(state, ins, left * right);
5759 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005760 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005761 mkconst(state, ins, 0);
5762 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005763 else if (is_one(RHS(ins, 1))) {
5764 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005765 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005766 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005767 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005768 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005769 ins->op = OP_SL;
5770 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005771 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005772 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005773 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005774 }
5775}
5776
5777static void simplify_sdiv(struct compile_state *state, struct triple *ins)
5778{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005779 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005780 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005781 left = read_sconst(ins, &RHS(ins, 0));
5782 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005783 mkconst(state, ins, left / right);
5784 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005785 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005786 mkconst(state, ins, 0);
5787 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005788 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005789 error(state, ins, "division by zero");
5790 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005791 else if (is_one(RHS(ins, 1))) {
5792 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005793 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005794 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005795 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005796 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005797 ins->op = OP_SSR;
5798 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005799 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005800 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005801 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005802 }
5803}
5804
5805static void simplify_udiv(struct compile_state *state, struct triple *ins)
5806{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005807 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005808 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005809 left = read_const(state, ins, &RHS(ins, 0));
5810 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005811 mkconst(state, ins, left / right);
5812 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005813 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005814 mkconst(state, ins, 0);
5815 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005816 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005817 error(state, ins, "division by zero");
5818 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005819 else if (is_one(RHS(ins, 1))) {
5820 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005821 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005822 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005823 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005824 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005825 ins->op = OP_USR;
5826 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005827 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005828 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005829 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005830 }
5831}
5832
5833static void simplify_smod(struct compile_state *state, struct triple *ins)
5834{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005835 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005836 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005837 left = read_const(state, ins, &RHS(ins, 0));
5838 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005839 mkconst(state, ins, left % right);
5840 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005841 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005842 mkconst(state, ins, 0);
5843 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005844 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005845 error(state, ins, "division by zero");
5846 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005847 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005848 mkconst(state, ins, 0);
5849 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005850 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005851 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005852 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005853 ins->op = OP_AND;
5854 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005855 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005856 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005857 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005858 }
5859}
5860static void simplify_umod(struct compile_state *state, struct triple *ins)
5861{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005862 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005863 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005864 left = read_const(state, ins, &RHS(ins, 0));
5865 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005866 mkconst(state, ins, left % right);
5867 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005868 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005869 mkconst(state, ins, 0);
5870 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005871 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005872 error(state, ins, "division by zero");
5873 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005874 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005875 mkconst(state, ins, 0);
5876 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005877 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005878 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005879 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005880 ins->op = OP_AND;
5881 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005882 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005883 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005884 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005885 }
5886}
5887
5888static void simplify_add(struct compile_state *state, struct triple *ins)
5889{
5890 /* start with the pointer on the left */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005891 if (is_pointer(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005892 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005893 tmp = RHS(ins, 0);
5894 RHS(ins, 0) = RHS(ins, 1);
5895 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005896 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005897 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5898 if (!is_pointer(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005899 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005900 left = read_const(state, ins, &RHS(ins, 0));
5901 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005902 mkconst(state, ins, left + right);
5903 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005904 else /* op == OP_ADDRCONST */ {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005905 struct triple *sdecl;
5906 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005907 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005908 left = RHS(ins, 0)->u.cval;
5909 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005910 mkaddr_const(state, ins, sdecl, left + right);
5911 }
5912 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005913 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005914 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005915 tmp = RHS(ins, 1);
5916 RHS(ins, 1) = RHS(ins, 0);
5917 RHS(ins, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005918 }
5919}
5920
5921static void simplify_sub(struct compile_state *state, struct triple *ins)
5922{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005923 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5924 if (!is_pointer(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005925 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005926 left = read_const(state, ins, &RHS(ins, 0));
5927 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005928 mkconst(state, ins, left - right);
5929 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005930 else /* op == OP_ADDRCONST */ {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005931 struct triple *sdecl;
5932 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005933 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005934 left = RHS(ins, 0)->u.cval;
5935 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005936 mkaddr_const(state, ins, sdecl, left - right);
5937 }
5938 }
5939}
5940
5941static void simplify_sl(struct compile_state *state, struct triple *ins)
5942{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005943 if (is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005944 ulong_t right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005945 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005946 if (right >= (size_of(state, ins->type)*8)) {
5947 warning(state, ins, "left shift count >= width of type");
5948 }
5949 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005950 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005951 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005952 left = read_const(state, ins, &RHS(ins, 0));
5953 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005954 mkconst(state, ins, left << right);
5955 }
5956}
5957
5958static void simplify_usr(struct compile_state *state, struct triple *ins)
5959{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005960 if (is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005961 ulong_t right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005962 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005963 if (right >= (size_of(state, ins->type)*8)) {
5964 warning(state, ins, "right shift count >= width of type");
5965 }
5966 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005967 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005968 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005969 left = read_const(state, ins, &RHS(ins, 0));
5970 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005971 mkconst(state, ins, left >> right);
5972 }
5973}
5974
5975static void simplify_ssr(struct compile_state *state, struct triple *ins)
5976{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005977 if (is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005978 ulong_t right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005979 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005980 if (right >= (size_of(state, ins->type)*8)) {
5981 warning(state, ins, "right shift count >= width of type");
5982 }
5983 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005984 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005985 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005986 left = read_sconst(ins, &RHS(ins, 0));
5987 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005988 mkconst(state, ins, left >> right);
5989 }
5990}
5991
5992static void simplify_and(struct compile_state *state, struct triple *ins)
5993{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005994 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005995 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005996 left = read_const(state, ins, &RHS(ins, 0));
5997 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005998 mkconst(state, ins, left & right);
5999 }
6000}
6001
6002static void simplify_or(struct compile_state *state, struct triple *ins)
6003{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006004 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006005 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006006 left = read_const(state, ins, &RHS(ins, 0));
6007 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006008 mkconst(state, ins, left | right);
6009 }
6010}
6011
6012static void simplify_xor(struct compile_state *state, struct triple *ins)
6013{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006014 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006015 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006016 left = read_const(state, ins, &RHS(ins, 0));
6017 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006018 mkconst(state, ins, left ^ right);
6019 }
6020}
6021
6022static void simplify_pos(struct compile_state *state, struct triple *ins)
6023{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006024 if (is_const(RHS(ins, 0))) {
6025 mkconst(state, ins, RHS(ins, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006026 }
6027 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006028 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006029 }
6030}
6031
6032static void simplify_neg(struct compile_state *state, struct triple *ins)
6033{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006034 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006035 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006036 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006037 mkconst(state, ins, -left);
6038 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006039 else if (RHS(ins, 0)->op == OP_NEG) {
6040 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006041 }
6042}
6043
6044static void simplify_invert(struct compile_state *state, struct triple *ins)
6045{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006046 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006047 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006048 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006049 mkconst(state, ins, ~left);
6050 }
6051}
6052
6053static void simplify_eq(struct compile_state *state, struct triple *ins)
6054{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006055 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006056 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006057 left = read_const(state, ins, &RHS(ins, 0));
6058 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006059 mkconst(state, ins, left == right);
6060 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006061 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006062 mkconst(state, ins, 1);
6063 }
6064}
6065
6066static void simplify_noteq(struct compile_state *state, struct triple *ins)
6067{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006068 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006069 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006070 left = read_const(state, ins, &RHS(ins, 0));
6071 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006072 mkconst(state, ins, left != right);
6073 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006074 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006075 mkconst(state, ins, 0);
6076 }
6077}
6078
6079static void simplify_sless(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 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006083 left = read_sconst(ins, &RHS(ins, 0));
6084 right = read_sconst(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 (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006088 mkconst(state, ins, 0);
6089 }
6090}
6091
6092static void simplify_uless(struct compile_state *state, struct triple *ins)
6093{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006094 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006095 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006096 left = read_const(state, ins, &RHS(ins, 0));
6097 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006098 mkconst(state, ins, left < right);
6099 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006100 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006101 mkconst(state, ins, 1);
6102 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006103 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006104 mkconst(state, ins, 0);
6105 }
6106}
6107
6108static void simplify_smore(struct compile_state *state, struct triple *ins)
6109{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006110 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006111 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006112 left = read_sconst(ins, &RHS(ins, 0));
6113 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006114 mkconst(state, ins, left > right);
6115 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006116 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006117 mkconst(state, ins, 0);
6118 }
6119}
6120
6121static void simplify_umore(struct compile_state *state, struct triple *ins)
6122{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006123 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006124 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006125 left = read_const(state, ins, &RHS(ins, 0));
6126 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006127 mkconst(state, ins, left > right);
6128 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006129 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006130 mkconst(state, ins, 1);
6131 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006132 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006133 mkconst(state, ins, 0);
6134 }
6135}
6136
6137
6138static void simplify_slesseq(struct compile_state *state, struct triple *ins)
6139{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006140 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006141 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006142 left = read_sconst(ins, &RHS(ins, 0));
6143 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006144 mkconst(state, ins, left <= right);
6145 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006146 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006147 mkconst(state, ins, 1);
6148 }
6149}
6150
6151static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
6152{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006153 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006154 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006155 left = read_const(state, ins, &RHS(ins, 0));
6156 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006157 mkconst(state, ins, left <= right);
6158 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006159 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006160 mkconst(state, ins, 1);
6161 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006162 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006163 mkconst(state, ins, 1);
6164 }
6165}
6166
6167static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
6168{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006169 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006170 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006171 left = read_sconst(ins, &RHS(ins, 0));
6172 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006173 mkconst(state, ins, left >= right);
6174 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006175 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006176 mkconst(state, ins, 1);
6177 }
6178}
6179
6180static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
6181{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006182 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006183 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006184 left = read_const(state, ins, &RHS(ins, 0));
6185 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006186 mkconst(state, ins, left >= right);
6187 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006188 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006189 mkconst(state, ins, 1);
6190 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006191 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006192 mkconst(state, ins, 1);
6193 }
6194}
6195
6196static void simplify_lfalse(struct compile_state *state, struct triple *ins)
6197{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006198 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006199 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006200 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006201 mkconst(state, ins, left == 0);
6202 }
6203 /* Otherwise if I am the only user... */
Eric Biederman0babc1c2003-05-09 02:39:00 +00006204 else if ((RHS(ins, 0)->use->member == ins) && (RHS(ins, 0)->use->next == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006205 int need_copy = 1;
6206 /* Invert a boolean operation */
Eric Biederman0babc1c2003-05-09 02:39:00 +00006207 switch(RHS(ins, 0)->op) {
6208 case OP_LTRUE: RHS(ins, 0)->op = OP_LFALSE; break;
6209 case OP_LFALSE: RHS(ins, 0)->op = OP_LTRUE; break;
6210 case OP_EQ: RHS(ins, 0)->op = OP_NOTEQ; break;
6211 case OP_NOTEQ: RHS(ins, 0)->op = OP_EQ; break;
6212 case OP_SLESS: RHS(ins, 0)->op = OP_SMOREEQ; break;
6213 case OP_ULESS: RHS(ins, 0)->op = OP_UMOREEQ; break;
6214 case OP_SMORE: RHS(ins, 0)->op = OP_SLESSEQ; break;
6215 case OP_UMORE: RHS(ins, 0)->op = OP_ULESSEQ; break;
6216 case OP_SLESSEQ: RHS(ins, 0)->op = OP_SMORE; break;
6217 case OP_ULESSEQ: RHS(ins, 0)->op = OP_UMORE; break;
6218 case OP_SMOREEQ: RHS(ins, 0)->op = OP_SLESS; break;
6219 case OP_UMOREEQ: RHS(ins, 0)->op = OP_ULESS; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006220 default:
6221 need_copy = 0;
6222 break;
6223 }
6224 if (need_copy) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006225 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006226 }
6227 }
6228}
6229
6230static void simplify_ltrue (struct compile_state *state, struct triple *ins)
6231{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006232 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006233 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006234 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006235 mkconst(state, ins, left != 0);
6236 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006237 else switch(RHS(ins, 0)->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006238 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
6239 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
6240 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
Eric Biederman0babc1c2003-05-09 02:39:00 +00006241 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006242 }
6243
6244}
6245
6246static void simplify_copy(struct compile_state *state, struct triple *ins)
6247{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006248 if (is_const(RHS(ins, 0))) {
6249 switch(RHS(ins, 0)->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006250 case OP_INTCONST:
6251 {
6252 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006253 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006254 mkconst(state, ins, left);
6255 break;
6256 }
6257 case OP_ADDRCONST:
6258 {
6259 struct triple *sdecl;
6260 ulong_t offset;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006261 sdecl = MISC(RHS(ins, 0), 0);
6262 offset = RHS(ins, 0)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006263 mkaddr_const(state, ins, sdecl, offset);
6264 break;
6265 }
6266 default:
6267 internal_error(state, ins, "uknown constant");
6268 break;
6269 }
6270 }
6271}
6272
Eric Biedermanb138ac82003-04-22 18:44:01 +00006273static void simplify_branch(struct compile_state *state, struct triple *ins)
6274{
6275 struct block *block;
6276 if (ins->op != OP_BRANCH) {
6277 internal_error(state, ins, "not branch");
6278 }
6279 if (ins->use != 0) {
6280 internal_error(state, ins, "branch use");
6281 }
6282#warning "FIXME implement simplify branch."
6283 /* The challenge here with simplify branch is that I need to
6284 * make modifications to the control flow graph as well
6285 * as to the branch instruction itself.
6286 */
6287 block = ins->u.block;
6288
Eric Biederman0babc1c2003-05-09 02:39:00 +00006289 if (TRIPLE_RHS(ins->sizes) && is_const(RHS(ins, 0))) {
6290 struct triple *targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006291 ulong_t value;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006292 value = read_const(state, ins, &RHS(ins, 0));
6293 unuse_triple(RHS(ins, 0), ins);
6294 targ = TARG(ins, 0);
6295 ins->sizes = TRIPLE_SIZES(0, 0, 0, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006296 if (value) {
6297 unuse_triple(ins->next, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006298 TARG(ins, 0) = targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006299 }
6300 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006301 unuse_triple(targ, ins);
6302 TARG(ins, 0) = ins->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006303 }
6304#warning "FIXME handle the case of making a branch unconditional"
6305 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006306 if (TARG(ins, 0) == ins->next) {
6307 unuse_triple(ins->next, ins);
6308 if (TRIPLE_RHS(ins->sizes)) {
6309 unuse_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006310 unuse_triple(ins->next, ins);
6311 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006312 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
6313 ins->op = OP_NOOP;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006314 if (ins->use) {
6315 internal_error(state, ins, "noop use != 0");
6316 }
6317#warning "FIXME handle the case of killing a branch"
6318 }
6319}
6320
6321static void simplify_phi(struct compile_state *state, struct triple *ins)
6322{
6323 struct triple **expr;
6324 ulong_t value;
6325 expr = triple_rhs(state, ins, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006326 if (!*expr || !is_const(*expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006327 return;
6328 }
6329 value = read_const(state, ins, expr);
6330 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006331 if (!*expr || !is_const(*expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006332 return;
6333 }
6334 if (value != read_const(state, ins, expr)) {
6335 return;
6336 }
6337 }
6338 mkconst(state, ins, value);
6339}
6340
6341
6342static void simplify_bsf(struct compile_state *state, struct triple *ins)
6343{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006344 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006345 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006346 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006347 mkconst(state, ins, bsf(left));
6348 }
6349}
6350
6351static void simplify_bsr(struct compile_state *state, struct triple *ins)
6352{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006353 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006354 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006355 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006356 mkconst(state, ins, bsr(left));
6357 }
6358}
6359
6360
6361typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
6362static const simplify_t table_simplify[] = {
6363#if 0
6364#define simplify_smul simplify_noop
6365#define simplify_umul simplify_noop
6366#define simplify_sdiv simplify_noop
6367#define simplify_udiv simplify_noop
6368#define simplify_smod simplify_noop
6369#define simplify_umod simplify_noop
6370#endif
6371#if 0
6372#define simplify_add simplify_noop
6373#define simplify_sub simplify_noop
6374#endif
6375#if 0
6376#define simplify_sl simplify_noop
6377#define simplify_usr simplify_noop
6378#define simplify_ssr simplify_noop
6379#endif
6380#if 0
6381#define simplify_and simplify_noop
6382#define simplify_xor simplify_noop
6383#define simplify_or simplify_noop
6384#endif
6385#if 0
6386#define simplify_pos simplify_noop
6387#define simplify_neg simplify_noop
6388#define simplify_invert simplify_noop
6389#endif
6390
6391#if 0
6392#define simplify_eq simplify_noop
6393#define simplify_noteq simplify_noop
6394#endif
6395#if 0
6396#define simplify_sless simplify_noop
6397#define simplify_uless simplify_noop
6398#define simplify_smore simplify_noop
6399#define simplify_umore simplify_noop
6400#endif
6401#if 0
6402#define simplify_slesseq simplify_noop
6403#define simplify_ulesseq simplify_noop
6404#define simplify_smoreeq simplify_noop
6405#define simplify_umoreeq simplify_noop
6406#endif
6407#if 0
6408#define simplify_lfalse simplify_noop
6409#endif
6410#if 0
6411#define simplify_ltrue simplify_noop
6412#endif
6413
6414#if 0
6415#define simplify_copy simplify_noop
6416#endif
6417
6418#if 0
Eric Biedermanb138ac82003-04-22 18:44:01 +00006419#define simplify_branch simplify_noop
6420#endif
6421
6422#if 0
6423#define simplify_phi simplify_noop
6424#endif
6425
6426#if 0
6427#define simplify_bsf simplify_noop
6428#define simplify_bsr simplify_noop
6429#endif
6430
6431[OP_SMUL ] = simplify_smul,
6432[OP_UMUL ] = simplify_umul,
6433[OP_SDIV ] = simplify_sdiv,
6434[OP_UDIV ] = simplify_udiv,
6435[OP_SMOD ] = simplify_smod,
6436[OP_UMOD ] = simplify_umod,
6437[OP_ADD ] = simplify_add,
6438[OP_SUB ] = simplify_sub,
6439[OP_SL ] = simplify_sl,
6440[OP_USR ] = simplify_usr,
6441[OP_SSR ] = simplify_ssr,
6442[OP_AND ] = simplify_and,
6443[OP_XOR ] = simplify_xor,
6444[OP_OR ] = simplify_or,
6445[OP_POS ] = simplify_pos,
6446[OP_NEG ] = simplify_neg,
6447[OP_INVERT ] = simplify_invert,
6448
6449[OP_EQ ] = simplify_eq,
6450[OP_NOTEQ ] = simplify_noteq,
6451[OP_SLESS ] = simplify_sless,
6452[OP_ULESS ] = simplify_uless,
6453[OP_SMORE ] = simplify_smore,
6454[OP_UMORE ] = simplify_umore,
6455[OP_SLESSEQ ] = simplify_slesseq,
6456[OP_ULESSEQ ] = simplify_ulesseq,
6457[OP_SMOREEQ ] = simplify_smoreeq,
6458[OP_UMOREEQ ] = simplify_umoreeq,
6459[OP_LFALSE ] = simplify_lfalse,
6460[OP_LTRUE ] = simplify_ltrue,
6461
6462[OP_LOAD ] = simplify_noop,
6463[OP_STORE ] = simplify_noop,
6464
6465[OP_NOOP ] = simplify_noop,
6466
6467[OP_INTCONST ] = simplify_noop,
6468[OP_BLOBCONST ] = simplify_noop,
6469[OP_ADDRCONST ] = simplify_noop,
6470
6471[OP_WRITE ] = simplify_noop,
6472[OP_READ ] = simplify_noop,
6473[OP_COPY ] = simplify_copy,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006474[OP_PIECE ] = simplify_noop,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006475[OP_ASM ] = simplify_noop,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006476
6477[OP_DOT ] = simplify_noop,
6478[OP_VAL_VEC ] = simplify_noop,
Eric Biedermanb138ac82003-04-22 18:44:01 +00006479
6480[OP_LIST ] = simplify_noop,
6481[OP_BRANCH ] = simplify_branch,
6482[OP_LABEL ] = simplify_noop,
6483[OP_ADECL ] = simplify_noop,
6484[OP_SDECL ] = simplify_noop,
6485[OP_PHI ] = simplify_phi,
6486
6487[OP_INB ] = simplify_noop,
6488[OP_INW ] = simplify_noop,
6489[OP_INL ] = simplify_noop,
6490[OP_OUTB ] = simplify_noop,
6491[OP_OUTW ] = simplify_noop,
6492[OP_OUTL ] = simplify_noop,
6493[OP_BSF ] = simplify_bsf,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006494[OP_BSR ] = simplify_bsr,
6495[OP_RDMSR ] = simplify_noop,
6496[OP_WRMSR ] = simplify_noop,
6497[OP_HLT ] = simplify_noop,
Eric Biedermanb138ac82003-04-22 18:44:01 +00006498};
6499
6500static void simplify(struct compile_state *state, struct triple *ins)
6501{
6502 int op;
6503 simplify_t do_simplify;
6504 do {
6505 op = ins->op;
6506 do_simplify = 0;
6507 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
6508 do_simplify = 0;
6509 }
6510 else {
6511 do_simplify = table_simplify[op];
6512 }
6513 if (!do_simplify) {
6514 internal_error(state, ins, "cannot simplify op: %d %s\n",
6515 op, tops(op));
6516 return;
6517 }
6518 do_simplify(state, ins);
6519 } while(ins->op != op);
6520}
6521
6522static void simplify_all(struct compile_state *state)
6523{
6524 struct triple *ins, *first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006525 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006526 ins = first;
6527 do {
6528 simplify(state, ins);
6529 ins = ins->next;
6530 } while(ins != first);
6531}
6532
6533/*
6534 * Builtins....
6535 * ============================
6536 */
6537
Eric Biederman0babc1c2003-05-09 02:39:00 +00006538static void register_builtin_function(struct compile_state *state,
6539 const char *name, int op, struct type *rtype, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00006540{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006541 struct type *ftype, *atype, *param, **next;
6542 struct triple *def, *arg, *result, *work, *last, *first;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006543 struct hash_entry *ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006544 struct file_state file;
6545 int parameters;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006546 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006547 va_list args;
6548 int i;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006549
6550 /* Dummy file state to get debug handling right */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006551 memset(&file, 0, sizeof(file));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00006552 file.basename = "<built-in>";
Eric Biedermanb138ac82003-04-22 18:44:01 +00006553 file.line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00006554 file.report_line = 1;
6555 file.report_name = file.basename;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006556 file.prev = state->file;
6557 state->file = &file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00006558 state->function = name;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006559
6560 /* Find the Parameter count */
6561 valid_op(state, op);
6562 parameters = table_ops[op].rhs;
6563 if (parameters < 0 ) {
6564 internal_error(state, 0, "Invalid builtin parameter count");
6565 }
6566
6567 /* Find the function type */
6568 ftype = new_type(TYPE_FUNCTION, rtype, 0);
6569 next = &ftype->right;
6570 va_start(args, rtype);
6571 for(i = 0; i < parameters; i++) {
6572 atype = va_arg(args, struct type *);
6573 if (!*next) {
6574 *next = atype;
6575 } else {
6576 *next = new_type(TYPE_PRODUCT, *next, atype);
6577 next = &((*next)->right);
6578 }
6579 }
6580 if (!*next) {
6581 *next = &void_type;
6582 }
6583 va_end(args);
6584
Eric Biedermanb138ac82003-04-22 18:44:01 +00006585 /* Generate the needed triples */
6586 def = triple(state, OP_LIST, ftype, 0, 0);
6587 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006588 RHS(def, 0) = first;
6589
6590 /* Now string them together */
6591 param = ftype->right;
6592 for(i = 0; i < parameters; i++) {
6593 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6594 atype = param->left;
6595 } else {
6596 atype = param;
6597 }
6598 arg = flatten(state, first, variable(state, atype));
6599 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006600 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006601 result = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006602 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006603 result = flatten(state, first, variable(state, rtype));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006604 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006605 MISC(def, 0) = result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006606 work = new_triple(state, op, rtype, -1, parameters);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006607 for(i = 0, arg = first->next; i < parameters; i++, arg = arg->next) {
6608 RHS(work, i) = read_expr(state, arg);
6609 }
6610 if (result && ((rtype->type & TYPE_MASK) == TYPE_STRUCT)) {
6611 struct triple *val;
6612 /* Populate the LHS with the target registers */
6613 work = flatten(state, first, work);
6614 work->type = &void_type;
6615 param = rtype->left;
6616 if (rtype->elements != TRIPLE_LHS(work->sizes)) {
6617 internal_error(state, 0, "Invalid result type");
6618 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006619 val = new_triple(state, OP_VAL_VEC, rtype, -1, -1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006620 for(i = 0; i < rtype->elements; i++) {
6621 struct triple *piece;
6622 atype = param;
6623 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6624 atype = param->left;
6625 }
6626 if (!TYPE_ARITHMETIC(atype->type) &&
6627 !TYPE_PTR(atype->type)) {
6628 internal_error(state, 0, "Invalid lhs type");
6629 }
6630 piece = triple(state, OP_PIECE, atype, work, 0);
6631 piece->u.cval = i;
6632 LHS(work, i) = piece;
6633 RHS(val, i) = piece;
6634 }
6635 work = val;
6636 }
6637 if (result) {
6638 work = write_expr(state, result, work);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006639 }
6640 work = flatten(state, first, work);
6641 last = flatten(state, first, label(state));
6642 name_len = strlen(name);
6643 ident = lookup(state, name, name_len);
6644 symbol(state, ident, &ident->sym_ident, def, ftype);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006645
Eric Biedermanb138ac82003-04-22 18:44:01 +00006646 state->file = file.prev;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00006647 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006648#if 0
6649 fprintf(stdout, "\n");
6650 loc(stdout, state, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006651 fprintf(stdout, "\n__________ builtin_function _________\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006652 print_triple(state, def);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006653 fprintf(stdout, "__________ builtin_function _________ done\n\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006654#endif
6655}
6656
Eric Biederman0babc1c2003-05-09 02:39:00 +00006657static struct type *partial_struct(struct compile_state *state,
6658 const char *field_name, struct type *type, struct type *rest)
Eric Biedermanb138ac82003-04-22 18:44:01 +00006659{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006660 struct hash_entry *field_ident;
6661 struct type *result;
6662 int field_name_len;
6663
6664 field_name_len = strlen(field_name);
6665 field_ident = lookup(state, field_name, field_name_len);
6666
6667 result = clone_type(0, type);
6668 result->field_ident = field_ident;
6669
6670 if (rest) {
6671 result = new_type(TYPE_PRODUCT, result, rest);
6672 }
6673 return result;
6674}
6675
6676static struct type *register_builtin_type(struct compile_state *state,
6677 const char *name, struct type *type)
6678{
Eric Biedermanb138ac82003-04-22 18:44:01 +00006679 struct hash_entry *ident;
6680 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006681
Eric Biedermanb138ac82003-04-22 18:44:01 +00006682 name_len = strlen(name);
6683 ident = lookup(state, name, name_len);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006684
6685 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6686 ulong_t elements = 0;
6687 struct type *field;
6688 type = new_type(TYPE_STRUCT, type, 0);
6689 field = type->left;
6690 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
6691 elements++;
6692 field = field->right;
6693 }
6694 elements++;
6695 symbol(state, ident, &ident->sym_struct, 0, type);
6696 type->type_ident = ident;
6697 type->elements = elements;
6698 }
6699 symbol(state, ident, &ident->sym_ident, 0, type);
6700 ident->tok = TOK_TYPE_NAME;
6701 return type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006702}
6703
Eric Biederman0babc1c2003-05-09 02:39:00 +00006704
Eric Biedermanb138ac82003-04-22 18:44:01 +00006705static void register_builtins(struct compile_state *state)
6706{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006707 struct type *msr_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006708
Eric Biederman0babc1c2003-05-09 02:39:00 +00006709 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
6710 &ushort_type);
6711 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
6712 &ushort_type);
6713 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
6714 &ushort_type);
6715
6716 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
6717 &uchar_type, &ushort_type);
6718 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
6719 &ushort_type, &ushort_type);
6720 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
6721 &uint_type, &ushort_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006722
Eric Biederman0babc1c2003-05-09 02:39:00 +00006723 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
6724 &int_type);
6725 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
6726 &int_type);
6727
6728 msr_type = register_builtin_type(state, "__builtin_msr_t",
6729 partial_struct(state, "lo", &ulong_type,
6730 partial_struct(state, "hi", &ulong_type, 0)));
6731
6732 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
6733 &ulong_type);
6734 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
6735 &ulong_type, &ulong_type, &ulong_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006736
Eric Biederman0babc1c2003-05-09 02:39:00 +00006737 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
6738 &void_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006739}
6740
6741static struct type *declarator(
6742 struct compile_state *state, struct type *type,
6743 struct hash_entry **ident, int need_ident);
6744static void decl(struct compile_state *state, struct triple *first);
6745static struct type *specifier_qualifier_list(struct compile_state *state);
6746static int isdecl_specifier(int tok);
6747static struct type *decl_specifiers(struct compile_state *state);
6748static int istype(int tok);
6749static struct triple *expr(struct compile_state *state);
6750static struct triple *assignment_expr(struct compile_state *state);
6751static struct type *type_name(struct compile_state *state);
6752static void statement(struct compile_state *state, struct triple *fist);
6753
6754static struct triple *call_expr(
6755 struct compile_state *state, struct triple *func)
6756{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006757 struct triple *def;
6758 struct type *param, *type;
6759 ulong_t pvals, index;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006760
6761 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
6762 error(state, 0, "Called object is not a function");
6763 }
6764 if (func->op != OP_LIST) {
6765 internal_error(state, 0, "improper function");
6766 }
6767 eat(state, TOK_LPAREN);
6768 /* Find the return type without any specifiers */
6769 type = clone_type(0, func->type->left);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006770 def = new_triple(state, OP_CALL, func->type, -1, -1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006771 def->type = type;
6772
6773 pvals = TRIPLE_RHS(def->sizes);
6774 MISC(def, 0) = func;
6775
6776 param = func->type->right;
6777 for(index = 0; index < pvals; index++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006778 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006779 struct type *arg_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006780 val = read_expr(state, assignment_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006781 arg_type = param;
6782 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6783 arg_type = param->left;
6784 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006785 write_compatible(state, arg_type, val->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006786 RHS(def, index) = val;
6787 if (index != (pvals - 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006788 eat(state, TOK_COMMA);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006789 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006790 }
6791 }
6792 eat(state, TOK_RPAREN);
6793 return def;
6794}
6795
6796
6797static struct triple *character_constant(struct compile_state *state)
6798{
6799 struct triple *def;
6800 struct token *tk;
6801 const signed char *str, *end;
6802 int c;
6803 int str_len;
6804 eat(state, TOK_LIT_CHAR);
6805 tk = &state->token[0];
6806 str = tk->val.str + 1;
6807 str_len = tk->str_len - 2;
6808 if (str_len <= 0) {
6809 error(state, 0, "empty character constant");
6810 }
6811 end = str + str_len;
6812 c = char_value(state, &str, end);
6813 if (str != end) {
6814 error(state, 0, "multibyte character constant not supported");
6815 }
6816 def = int_const(state, &char_type, (ulong_t)((long_t)c));
6817 return def;
6818}
6819
6820static struct triple *string_constant(struct compile_state *state)
6821{
6822 struct triple *def;
6823 struct token *tk;
6824 struct type *type;
6825 const signed char *str, *end;
6826 signed char *buf, *ptr;
6827 int str_len;
6828
6829 buf = 0;
6830 type = new_type(TYPE_ARRAY, &char_type, 0);
6831 type->elements = 0;
6832 /* The while loop handles string concatenation */
6833 do {
6834 eat(state, TOK_LIT_STRING);
6835 tk = &state->token[0];
6836 str = tk->val.str + 1;
6837 str_len = tk->str_len - 2;
Eric Biedermanab2ea6b2003-04-26 03:20:53 +00006838 if (str_len < 0) {
6839 error(state, 0, "negative string constant length");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006840 }
6841 end = str + str_len;
6842 ptr = buf;
6843 buf = xmalloc(type->elements + str_len + 1, "string_constant");
6844 memcpy(buf, ptr, type->elements);
6845 ptr = buf + type->elements;
6846 do {
6847 *ptr++ = char_value(state, &str, end);
6848 } while(str < end);
6849 type->elements = ptr - buf;
6850 } while(peek(state) == TOK_LIT_STRING);
6851 *ptr = '\0';
6852 type->elements += 1;
6853 def = triple(state, OP_BLOBCONST, type, 0, 0);
6854 def->u.blob = buf;
6855 return def;
6856}
6857
6858
6859static struct triple *integer_constant(struct compile_state *state)
6860{
6861 struct triple *def;
6862 unsigned long val;
6863 struct token *tk;
6864 char *end;
6865 int u, l, decimal;
6866 struct type *type;
6867
6868 eat(state, TOK_LIT_INT);
6869 tk = &state->token[0];
6870 errno = 0;
6871 decimal = (tk->val.str[0] != '0');
6872 val = strtoul(tk->val.str, &end, 0);
6873 if ((val == ULONG_MAX) && (errno == ERANGE)) {
6874 error(state, 0, "Integer constant to large");
6875 }
6876 u = l = 0;
6877 if ((*end == 'u') || (*end == 'U')) {
6878 u = 1;
6879 end++;
6880 }
6881 if ((*end == 'l') || (*end == 'L')) {
6882 l = 1;
6883 end++;
6884 }
6885 if ((*end == 'u') || (*end == 'U')) {
6886 u = 1;
6887 end++;
6888 }
6889 if (*end) {
6890 error(state, 0, "Junk at end of integer constant");
6891 }
6892 if (u && l) {
6893 type = &ulong_type;
6894 }
6895 else if (l) {
6896 type = &long_type;
6897 if (!decimal && (val > LONG_MAX)) {
6898 type = &ulong_type;
6899 }
6900 }
6901 else if (u) {
6902 type = &uint_type;
6903 if (val > UINT_MAX) {
6904 type = &ulong_type;
6905 }
6906 }
6907 else {
6908 type = &int_type;
6909 if (!decimal && (val > INT_MAX) && (val <= UINT_MAX)) {
6910 type = &uint_type;
6911 }
6912 else if (!decimal && (val > LONG_MAX)) {
6913 type = &ulong_type;
6914 }
6915 else if (val > INT_MAX) {
6916 type = &long_type;
6917 }
6918 }
6919 def = int_const(state, type, val);
6920 return def;
6921}
6922
6923static struct triple *primary_expr(struct compile_state *state)
6924{
6925 struct triple *def;
6926 int tok;
6927 tok = peek(state);
6928 switch(tok) {
6929 case TOK_IDENT:
6930 {
6931 struct hash_entry *ident;
6932 /* Here ident is either:
6933 * a varable name
6934 * a function name
6935 * an enumeration constant.
6936 */
6937 eat(state, TOK_IDENT);
6938 ident = state->token[0].ident;
6939 if (!ident->sym_ident) {
6940 error(state, 0, "%s undeclared", ident->name);
6941 }
6942 def = ident->sym_ident->def;
6943 break;
6944 }
6945 case TOK_ENUM_CONST:
6946 /* Here ident is an enumeration constant */
6947 eat(state, TOK_ENUM_CONST);
6948 def = 0;
6949 FINISHME();
6950 break;
6951 case TOK_LPAREN:
6952 eat(state, TOK_LPAREN);
6953 def = expr(state);
6954 eat(state, TOK_RPAREN);
6955 break;
6956 case TOK_LIT_INT:
6957 def = integer_constant(state);
6958 break;
6959 case TOK_LIT_FLOAT:
6960 eat(state, TOK_LIT_FLOAT);
6961 error(state, 0, "Floating point constants not supported");
6962 def = 0;
6963 FINISHME();
6964 break;
6965 case TOK_LIT_CHAR:
6966 def = character_constant(state);
6967 break;
6968 case TOK_LIT_STRING:
6969 def = string_constant(state);
6970 break;
6971 default:
6972 def = 0;
6973 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
6974 }
6975 return def;
6976}
6977
6978static struct triple *postfix_expr(struct compile_state *state)
6979{
6980 struct triple *def;
6981 int postfix;
6982 def = primary_expr(state);
6983 do {
6984 struct triple *left;
6985 int tok;
6986 postfix = 1;
6987 left = def;
6988 switch((tok = peek(state))) {
6989 case TOK_LBRACKET:
6990 eat(state, TOK_LBRACKET);
6991 def = mk_subscript_expr(state, left, expr(state));
6992 eat(state, TOK_RBRACKET);
6993 break;
6994 case TOK_LPAREN:
6995 def = call_expr(state, def);
6996 break;
6997 case TOK_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +00006998 {
6999 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007000 eat(state, TOK_DOT);
7001 eat(state, TOK_IDENT);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007002 field = state->token[0].ident;
7003 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007004 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007005 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007006 case TOK_ARROW:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007007 {
7008 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007009 eat(state, TOK_ARROW);
7010 eat(state, TOK_IDENT);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007011 field = state->token[0].ident;
7012 def = mk_deref_expr(state, read_expr(state, def));
7013 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007014 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007015 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007016 case TOK_PLUSPLUS:
7017 eat(state, TOK_PLUSPLUS);
7018 def = mk_post_inc_expr(state, left);
7019 break;
7020 case TOK_MINUSMINUS:
7021 eat(state, TOK_MINUSMINUS);
7022 def = mk_post_dec_expr(state, left);
7023 break;
7024 default:
7025 postfix = 0;
7026 break;
7027 }
7028 } while(postfix);
7029 return def;
7030}
7031
7032static struct triple *cast_expr(struct compile_state *state);
7033
7034static struct triple *unary_expr(struct compile_state *state)
7035{
7036 struct triple *def, *right;
7037 int tok;
7038 switch((tok = peek(state))) {
7039 case TOK_PLUSPLUS:
7040 eat(state, TOK_PLUSPLUS);
7041 def = mk_pre_inc_expr(state, unary_expr(state));
7042 break;
7043 case TOK_MINUSMINUS:
7044 eat(state, TOK_MINUSMINUS);
7045 def = mk_pre_dec_expr(state, unary_expr(state));
7046 break;
7047 case TOK_AND:
7048 eat(state, TOK_AND);
7049 def = mk_addr_expr(state, cast_expr(state), 0);
7050 break;
7051 case TOK_STAR:
7052 eat(state, TOK_STAR);
7053 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
7054 break;
7055 case TOK_PLUS:
7056 eat(state, TOK_PLUS);
7057 right = read_expr(state, cast_expr(state));
7058 arithmetic(state, right);
7059 def = integral_promotion(state, right);
7060 break;
7061 case TOK_MINUS:
7062 eat(state, TOK_MINUS);
7063 right = read_expr(state, cast_expr(state));
7064 arithmetic(state, right);
7065 def = integral_promotion(state, right);
7066 def = triple(state, OP_NEG, def->type, def, 0);
7067 break;
7068 case TOK_TILDE:
7069 eat(state, TOK_TILDE);
7070 right = read_expr(state, cast_expr(state));
7071 integral(state, right);
7072 def = integral_promotion(state, right);
7073 def = triple(state, OP_INVERT, def->type, def, 0);
7074 break;
7075 case TOK_BANG:
7076 eat(state, TOK_BANG);
7077 right = read_expr(state, cast_expr(state));
7078 bool(state, right);
7079 def = lfalse_expr(state, right);
7080 break;
7081 case TOK_SIZEOF:
7082 {
7083 struct type *type;
7084 int tok1, tok2;
7085 eat(state, TOK_SIZEOF);
7086 tok1 = peek(state);
7087 tok2 = peek2(state);
7088 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7089 eat(state, TOK_LPAREN);
7090 type = type_name(state);
7091 eat(state, TOK_RPAREN);
7092 }
7093 else {
7094 struct triple *expr;
7095 expr = unary_expr(state);
7096 type = expr->type;
7097 release_expr(state, expr);
7098 }
7099 def = int_const(state, &ulong_type, size_of(state, type));
7100 break;
7101 }
7102 case TOK_ALIGNOF:
7103 {
7104 struct type *type;
7105 int tok1, tok2;
7106 eat(state, TOK_ALIGNOF);
7107 tok1 = peek(state);
7108 tok2 = peek2(state);
7109 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7110 eat(state, TOK_LPAREN);
7111 type = type_name(state);
7112 eat(state, TOK_RPAREN);
7113 }
7114 else {
7115 struct triple *expr;
7116 expr = unary_expr(state);
7117 type = expr->type;
7118 release_expr(state, expr);
7119 }
7120 def = int_const(state, &ulong_type, align_of(state, type));
7121 break;
7122 }
7123 default:
7124 def = postfix_expr(state);
7125 break;
7126 }
7127 return def;
7128}
7129
7130static struct triple *cast_expr(struct compile_state *state)
7131{
7132 struct triple *def;
7133 int tok1, tok2;
7134 tok1 = peek(state);
7135 tok2 = peek2(state);
7136 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7137 struct type *type;
7138 eat(state, TOK_LPAREN);
7139 type = type_name(state);
7140 eat(state, TOK_RPAREN);
7141 def = read_expr(state, cast_expr(state));
7142 def = triple(state, OP_COPY, type, def, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007143 }
7144 else {
7145 def = unary_expr(state);
7146 }
7147 return def;
7148}
7149
7150static struct triple *mult_expr(struct compile_state *state)
7151{
7152 struct triple *def;
7153 int done;
7154 def = cast_expr(state);
7155 do {
7156 struct triple *left, *right;
7157 struct type *result_type;
7158 int tok, op, sign;
7159 done = 0;
7160 switch(tok = (peek(state))) {
7161 case TOK_STAR:
7162 case TOK_DIV:
7163 case TOK_MOD:
7164 left = read_expr(state, def);
7165 arithmetic(state, left);
7166
7167 eat(state, tok);
7168
7169 right = read_expr(state, cast_expr(state));
7170 arithmetic(state, right);
7171
7172 result_type = arithmetic_result(state, left, right);
7173 sign = is_signed(result_type);
7174 op = -1;
7175 switch(tok) {
7176 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
7177 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
7178 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
7179 }
7180 def = triple(state, op, result_type, left, right);
7181 break;
7182 default:
7183 done = 1;
7184 break;
7185 }
7186 } while(!done);
7187 return def;
7188}
7189
7190static struct triple *add_expr(struct compile_state *state)
7191{
7192 struct triple *def;
7193 int done;
7194 def = mult_expr(state);
7195 do {
7196 done = 0;
7197 switch( peek(state)) {
7198 case TOK_PLUS:
7199 eat(state, TOK_PLUS);
7200 def = mk_add_expr(state, def, mult_expr(state));
7201 break;
7202 case TOK_MINUS:
7203 eat(state, TOK_MINUS);
7204 def = mk_sub_expr(state, def, mult_expr(state));
7205 break;
7206 default:
7207 done = 1;
7208 break;
7209 }
7210 } while(!done);
7211 return def;
7212}
7213
7214static struct triple *shift_expr(struct compile_state *state)
7215{
7216 struct triple *def;
7217 int done;
7218 def = add_expr(state);
7219 do {
7220 struct triple *left, *right;
7221 int tok, op;
7222 done = 0;
7223 switch((tok = peek(state))) {
7224 case TOK_SL:
7225 case TOK_SR:
7226 left = read_expr(state, def);
7227 integral(state, left);
7228 left = integral_promotion(state, left);
7229
7230 eat(state, tok);
7231
7232 right = read_expr(state, add_expr(state));
7233 integral(state, right);
7234 right = integral_promotion(state, right);
7235
7236 op = (tok == TOK_SL)? OP_SL :
7237 is_signed(left->type)? OP_SSR: OP_USR;
7238
7239 def = triple(state, op, left->type, left, right);
7240 break;
7241 default:
7242 done = 1;
7243 break;
7244 }
7245 } while(!done);
7246 return def;
7247}
7248
7249static struct triple *relational_expr(struct compile_state *state)
7250{
7251#warning "Extend relational exprs to work on more than arithmetic types"
7252 struct triple *def;
7253 int done;
7254 def = shift_expr(state);
7255 do {
7256 struct triple *left, *right;
7257 struct type *arg_type;
7258 int tok, op, sign;
7259 done = 0;
7260 switch((tok = peek(state))) {
7261 case TOK_LESS:
7262 case TOK_MORE:
7263 case TOK_LESSEQ:
7264 case TOK_MOREEQ:
7265 left = read_expr(state, def);
7266 arithmetic(state, left);
7267
7268 eat(state, tok);
7269
7270 right = read_expr(state, shift_expr(state));
7271 arithmetic(state, right);
7272
7273 arg_type = arithmetic_result(state, left, right);
7274 sign = is_signed(arg_type);
7275 op = -1;
7276 switch(tok) {
7277 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
7278 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
7279 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
7280 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
7281 }
7282 def = triple(state, op, &int_type, left, right);
7283 break;
7284 default:
7285 done = 1;
7286 break;
7287 }
7288 } while(!done);
7289 return def;
7290}
7291
7292static struct triple *equality_expr(struct compile_state *state)
7293{
7294#warning "Extend equality exprs to work on more than arithmetic types"
7295 struct triple *def;
7296 int done;
7297 def = relational_expr(state);
7298 do {
7299 struct triple *left, *right;
7300 int tok, op;
7301 done = 0;
7302 switch((tok = peek(state))) {
7303 case TOK_EQEQ:
7304 case TOK_NOTEQ:
7305 left = read_expr(state, def);
7306 arithmetic(state, left);
7307 eat(state, tok);
7308 right = read_expr(state, relational_expr(state));
7309 arithmetic(state, right);
7310 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
7311 def = triple(state, op, &int_type, left, right);
7312 break;
7313 default:
7314 done = 1;
7315 break;
7316 }
7317 } while(!done);
7318 return def;
7319}
7320
7321static struct triple *and_expr(struct compile_state *state)
7322{
7323 struct triple *def;
7324 def = equality_expr(state);
7325 while(peek(state) == TOK_AND) {
7326 struct triple *left, *right;
7327 struct type *result_type;
7328 left = read_expr(state, def);
7329 integral(state, left);
7330 eat(state, TOK_AND);
7331 right = read_expr(state, equality_expr(state));
7332 integral(state, right);
7333 result_type = arithmetic_result(state, left, right);
7334 def = triple(state, OP_AND, result_type, left, right);
7335 }
7336 return def;
7337}
7338
7339static struct triple *xor_expr(struct compile_state *state)
7340{
7341 struct triple *def;
7342 def = and_expr(state);
7343 while(peek(state) == TOK_XOR) {
7344 struct triple *left, *right;
7345 struct type *result_type;
7346 left = read_expr(state, def);
7347 integral(state, left);
7348 eat(state, TOK_XOR);
7349 right = read_expr(state, and_expr(state));
7350 integral(state, right);
7351 result_type = arithmetic_result(state, left, right);
7352 def = triple(state, OP_XOR, result_type, left, right);
7353 }
7354 return def;
7355}
7356
7357static struct triple *or_expr(struct compile_state *state)
7358{
7359 struct triple *def;
7360 def = xor_expr(state);
7361 while(peek(state) == TOK_OR) {
7362 struct triple *left, *right;
7363 struct type *result_type;
7364 left = read_expr(state, def);
7365 integral(state, left);
7366 eat(state, TOK_OR);
7367 right = read_expr(state, xor_expr(state));
7368 integral(state, right);
7369 result_type = arithmetic_result(state, left, right);
7370 def = triple(state, OP_OR, result_type, left, right);
7371 }
7372 return def;
7373}
7374
7375static struct triple *land_expr(struct compile_state *state)
7376{
7377 struct triple *def;
7378 def = or_expr(state);
7379 while(peek(state) == TOK_LOGAND) {
7380 struct triple *left, *right;
7381 left = read_expr(state, def);
7382 bool(state, left);
7383 eat(state, TOK_LOGAND);
7384 right = read_expr(state, or_expr(state));
7385 bool(state, right);
7386
7387 def = triple(state, OP_LAND, &int_type,
7388 ltrue_expr(state, left),
7389 ltrue_expr(state, right));
7390 }
7391 return def;
7392}
7393
7394static struct triple *lor_expr(struct compile_state *state)
7395{
7396 struct triple *def;
7397 def = land_expr(state);
7398 while(peek(state) == TOK_LOGOR) {
7399 struct triple *left, *right;
7400 left = read_expr(state, def);
7401 bool(state, left);
7402 eat(state, TOK_LOGOR);
7403 right = read_expr(state, land_expr(state));
7404 bool(state, right);
7405
7406 def = triple(state, OP_LOR, &int_type,
7407 ltrue_expr(state, left),
7408 ltrue_expr(state, right));
7409 }
7410 return def;
7411}
7412
7413static struct triple *conditional_expr(struct compile_state *state)
7414{
7415 struct triple *def;
7416 def = lor_expr(state);
7417 if (peek(state) == TOK_QUEST) {
7418 struct triple *test, *left, *right;
7419 bool(state, def);
7420 test = ltrue_expr(state, read_expr(state, def));
7421 eat(state, TOK_QUEST);
7422 left = read_expr(state, expr(state));
7423 eat(state, TOK_COLON);
7424 right = read_expr(state, conditional_expr(state));
7425
7426 def = cond_expr(state, test, left, right);
7427 }
7428 return def;
7429}
7430
7431static struct triple *eval_const_expr(
7432 struct compile_state *state, struct triple *expr)
7433{
7434 struct triple *def;
7435 struct triple *head, *ptr;
7436 head = label(state); /* dummy initial triple */
7437 flatten(state, head, expr);
7438 for(ptr = head->next; ptr != head; ptr = ptr->next) {
7439 simplify(state, ptr);
7440 }
7441 /* Remove the constant value the tail of the list */
7442 def = head->prev;
7443 def->prev->next = def->next;
7444 def->next->prev = def->prev;
7445 def->next = def->prev = def;
7446 if (!is_const(def)) {
7447 internal_error(state, 0, "Not a constant expression");
7448 }
7449 /* Free the intermediate expressions */
7450 while(head->next != head) {
7451 release_triple(state, head->next);
7452 }
7453 free_triple(state, head);
7454 return def;
7455}
7456
7457static struct triple *constant_expr(struct compile_state *state)
7458{
7459 return eval_const_expr(state, conditional_expr(state));
7460}
7461
7462static struct triple *assignment_expr(struct compile_state *state)
7463{
7464 struct triple *def, *left, *right;
7465 int tok, op, sign;
7466 /* The C grammer in K&R shows assignment expressions
7467 * only taking unary expressions as input on their
7468 * left hand side. But specifies the precedence of
7469 * assignemnt as the lowest operator except for comma.
7470 *
7471 * Allowing conditional expressions on the left hand side
7472 * of an assignement results in a grammar that accepts
7473 * a larger set of statements than standard C. As long
7474 * as the subset of the grammar that is standard C behaves
7475 * correctly this should cause no problems.
7476 *
7477 * For the extra token strings accepted by the grammar
7478 * none of them should produce a valid lvalue, so they
7479 * should not produce functioning programs.
7480 *
7481 * GCC has this bug as well, so surprises should be minimal.
7482 */
7483 def = conditional_expr(state);
7484 left = def;
7485 switch((tok = peek(state))) {
7486 case TOK_EQ:
7487 lvalue(state, left);
7488 eat(state, TOK_EQ);
7489 def = write_expr(state, left,
7490 read_expr(state, assignment_expr(state)));
7491 break;
7492 case TOK_TIMESEQ:
7493 case TOK_DIVEQ:
7494 case TOK_MODEQ:
Eric Biedermanb138ac82003-04-22 18:44:01 +00007495 lvalue(state, left);
7496 arithmetic(state, left);
7497 eat(state, tok);
7498 right = read_expr(state, assignment_expr(state));
7499 arithmetic(state, right);
7500
7501 sign = is_signed(left->type);
7502 op = -1;
7503 switch(tok) {
7504 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
7505 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
7506 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007507 }
7508 def = write_expr(state, left,
7509 triple(state, op, left->type,
7510 read_expr(state, left), right));
7511 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007512 case TOK_PLUSEQ:
7513 lvalue(state, left);
7514 eat(state, TOK_PLUSEQ);
7515 def = write_expr(state, left,
7516 mk_add_expr(state, left, assignment_expr(state)));
7517 break;
7518 case TOK_MINUSEQ:
7519 lvalue(state, left);
7520 eat(state, TOK_MINUSEQ);
7521 def = write_expr(state, left,
7522 mk_sub_expr(state, left, assignment_expr(state)));
7523 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007524 case TOK_SLEQ:
7525 case TOK_SREQ:
7526 case TOK_ANDEQ:
7527 case TOK_XOREQ:
7528 case TOK_OREQ:
7529 lvalue(state, left);
7530 integral(state, left);
7531 eat(state, tok);
7532 right = read_expr(state, assignment_expr(state));
7533 integral(state, right);
7534 right = integral_promotion(state, right);
7535 sign = is_signed(left->type);
7536 op = -1;
7537 switch(tok) {
7538 case TOK_SLEQ: op = OP_SL; break;
7539 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
7540 case TOK_ANDEQ: op = OP_AND; break;
7541 case TOK_XOREQ: op = OP_XOR; break;
7542 case TOK_OREQ: op = OP_OR; break;
7543 }
7544 def = write_expr(state, left,
7545 triple(state, op, left->type,
7546 read_expr(state, left), right));
7547 break;
7548 }
7549 return def;
7550}
7551
7552static struct triple *expr(struct compile_state *state)
7553{
7554 struct triple *def;
7555 def = assignment_expr(state);
7556 while(peek(state) == TOK_COMMA) {
7557 struct triple *left, *right;
7558 left = def;
7559 eat(state, TOK_COMMA);
7560 right = assignment_expr(state);
7561 def = triple(state, OP_COMMA, right->type, left, right);
7562 }
7563 return def;
7564}
7565
7566static void expr_statement(struct compile_state *state, struct triple *first)
7567{
7568 if (peek(state) != TOK_SEMI) {
7569 flatten(state, first, expr(state));
7570 }
7571 eat(state, TOK_SEMI);
7572}
7573
7574static void if_statement(struct compile_state *state, struct triple *first)
7575{
7576 struct triple *test, *jmp1, *jmp2, *middle, *end;
7577
7578 jmp1 = jmp2 = middle = 0;
7579 eat(state, TOK_IF);
7580 eat(state, TOK_LPAREN);
7581 test = expr(state);
7582 bool(state, test);
7583 /* Cleanup and invert the test */
7584 test = lfalse_expr(state, read_expr(state, test));
7585 eat(state, TOK_RPAREN);
7586 /* Generate the needed pieces */
7587 middle = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007588 jmp1 = branch(state, middle, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007589 /* Thread the pieces together */
7590 flatten(state, first, test);
7591 flatten(state, first, jmp1);
7592 flatten(state, first, label(state));
7593 statement(state, first);
7594 if (peek(state) == TOK_ELSE) {
7595 eat(state, TOK_ELSE);
7596 /* Generate the rest of the pieces */
7597 end = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007598 jmp2 = branch(state, end, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007599 /* Thread them together */
7600 flatten(state, first, jmp2);
7601 flatten(state, first, middle);
7602 statement(state, first);
7603 flatten(state, first, end);
7604 }
7605 else {
7606 flatten(state, first, middle);
7607 }
7608}
7609
7610static void for_statement(struct compile_state *state, struct triple *first)
7611{
7612 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
7613 struct triple *label1, *label2, *label3;
7614 struct hash_entry *ident;
7615
7616 eat(state, TOK_FOR);
7617 eat(state, TOK_LPAREN);
7618 head = test = tail = jmp1 = jmp2 = 0;
7619 if (peek(state) != TOK_SEMI) {
7620 head = expr(state);
7621 }
7622 eat(state, TOK_SEMI);
7623 if (peek(state) != TOK_SEMI) {
7624 test = expr(state);
7625 bool(state, test);
7626 test = ltrue_expr(state, read_expr(state, test));
7627 }
7628 eat(state, TOK_SEMI);
7629 if (peek(state) != TOK_RPAREN) {
7630 tail = expr(state);
7631 }
7632 eat(state, TOK_RPAREN);
7633 /* Generate the needed pieces */
7634 label1 = label(state);
7635 label2 = label(state);
7636 label3 = label(state);
7637 if (test) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007638 jmp1 = branch(state, label3, 0);
7639 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007640 }
7641 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007642 jmp2 = branch(state, label1, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007643 }
7644 end = label(state);
7645 /* Remember where break and continue go */
7646 start_scope(state);
7647 ident = state->i_break;
7648 symbol(state, ident, &ident->sym_ident, end, end->type);
7649 ident = state->i_continue;
7650 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7651 /* Now include the body */
7652 flatten(state, first, head);
7653 flatten(state, first, jmp1);
7654 flatten(state, first, label1);
7655 statement(state, first);
7656 flatten(state, first, label2);
7657 flatten(state, first, tail);
7658 flatten(state, first, label3);
7659 flatten(state, first, test);
7660 flatten(state, first, jmp2);
7661 flatten(state, first, end);
7662 /* Cleanup the break/continue scope */
7663 end_scope(state);
7664}
7665
7666static void while_statement(struct compile_state *state, struct triple *first)
7667{
7668 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
7669 struct hash_entry *ident;
7670 eat(state, TOK_WHILE);
7671 eat(state, TOK_LPAREN);
7672 test = expr(state);
7673 bool(state, test);
7674 test = ltrue_expr(state, read_expr(state, test));
7675 eat(state, TOK_RPAREN);
7676 /* Generate the needed pieces */
7677 label1 = label(state);
7678 label2 = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007679 jmp1 = branch(state, label2, 0);
7680 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007681 end = label(state);
7682 /* Remember where break and continue go */
7683 start_scope(state);
7684 ident = state->i_break;
7685 symbol(state, ident, &ident->sym_ident, end, end->type);
7686 ident = state->i_continue;
7687 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7688 /* Thread them together */
7689 flatten(state, first, jmp1);
7690 flatten(state, first, label1);
7691 statement(state, first);
7692 flatten(state, first, label2);
7693 flatten(state, first, test);
7694 flatten(state, first, jmp2);
7695 flatten(state, first, end);
7696 /* Cleanup the break/continue scope */
7697 end_scope(state);
7698}
7699
7700static void do_statement(struct compile_state *state, struct triple *first)
7701{
7702 struct triple *label1, *label2, *test, *end;
7703 struct hash_entry *ident;
7704 eat(state, TOK_DO);
7705 /* Generate the needed pieces */
7706 label1 = label(state);
7707 label2 = label(state);
7708 end = label(state);
7709 /* Remember where break and continue go */
7710 start_scope(state);
7711 ident = state->i_break;
7712 symbol(state, ident, &ident->sym_ident, end, end->type);
7713 ident = state->i_continue;
7714 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7715 /* Now include the body */
7716 flatten(state, first, label1);
7717 statement(state, first);
7718 /* Cleanup the break/continue scope */
7719 end_scope(state);
7720 /* Eat the rest of the loop */
7721 eat(state, TOK_WHILE);
7722 eat(state, TOK_LPAREN);
7723 test = read_expr(state, expr(state));
7724 bool(state, test);
7725 eat(state, TOK_RPAREN);
7726 eat(state, TOK_SEMI);
7727 /* Thread the pieces together */
7728 test = ltrue_expr(state, test);
7729 flatten(state, first, label2);
7730 flatten(state, first, test);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007731 flatten(state, first, branch(state, label1, test));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007732 flatten(state, first, end);
7733}
7734
7735
7736static void return_statement(struct compile_state *state, struct triple *first)
7737{
7738 struct triple *jmp, *mv, *dest, *var, *val;
7739 int last;
7740 eat(state, TOK_RETURN);
7741
7742#warning "FIXME implement a more general excess branch elimination"
7743 val = 0;
7744 /* If we have a return value do some more work */
7745 if (peek(state) != TOK_SEMI) {
7746 val = read_expr(state, expr(state));
7747 }
7748 eat(state, TOK_SEMI);
7749
7750 /* See if this last statement in a function */
7751 last = ((peek(state) == TOK_RBRACE) &&
7752 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
7753
7754 /* Find the return variable */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007755 var = MISC(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007756 /* Find the return destination */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007757 dest = RHS(state->main_function, 0)->prev;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007758 mv = jmp = 0;
7759 /* If needed generate a jump instruction */
7760 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007761 jmp = branch(state, dest, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007762 }
7763 /* If needed generate an assignment instruction */
7764 if (val) {
7765 mv = write_expr(state, var, val);
7766 }
7767 /* Now put the code together */
7768 if (mv) {
7769 flatten(state, first, mv);
7770 flatten(state, first, jmp);
7771 }
7772 else if (jmp) {
7773 flatten(state, first, jmp);
7774 }
7775}
7776
7777static void break_statement(struct compile_state *state, struct triple *first)
7778{
7779 struct triple *dest;
7780 eat(state, TOK_BREAK);
7781 eat(state, TOK_SEMI);
7782 if (!state->i_break->sym_ident) {
7783 error(state, 0, "break statement not within loop or switch");
7784 }
7785 dest = state->i_break->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007786 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007787}
7788
7789static void continue_statement(struct compile_state *state, struct triple *first)
7790{
7791 struct triple *dest;
7792 eat(state, TOK_CONTINUE);
7793 eat(state, TOK_SEMI);
7794 if (!state->i_continue->sym_ident) {
7795 error(state, 0, "continue statement outside of a loop");
7796 }
7797 dest = state->i_continue->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007798 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007799}
7800
7801static void goto_statement(struct compile_state *state, struct triple *first)
7802{
Eric Biederman153ea352003-06-20 14:43:20 +00007803 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007804 eat(state, TOK_GOTO);
7805 eat(state, TOK_IDENT);
Eric Biederman153ea352003-06-20 14:43:20 +00007806 ident = state->token[0].ident;
7807 if (!ident->sym_label) {
7808 /* If this is a forward branch allocate the label now,
7809 * it will be flattend in the appropriate location later.
7810 */
7811 struct triple *ins;
7812 ins = label(state);
7813 label_symbol(state, ident, ins);
7814 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007815 eat(state, TOK_SEMI);
Eric Biederman153ea352003-06-20 14:43:20 +00007816
7817 flatten(state, first, branch(state, ident->sym_label->def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007818}
7819
7820static void labeled_statement(struct compile_state *state, struct triple *first)
7821{
Eric Biederman153ea352003-06-20 14:43:20 +00007822 struct triple *ins;
7823 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007824 eat(state, TOK_IDENT);
Eric Biederman153ea352003-06-20 14:43:20 +00007825
7826 ident = state->token[0].ident;
7827 if (ident->sym_label && ident->sym_label->def) {
7828 ins = ident->sym_label->def;
7829 put_occurance(ins->occurance);
7830 ins->occurance = new_occurance(state);
7831 }
7832 else {
7833 ins = label(state);
7834 label_symbol(state, ident, ins);
7835 }
7836 if (ins->id & TRIPLE_FLAG_FLATTENED) {
7837 error(state, 0, "label %s already defined", ident->name);
7838 }
7839 flatten(state, first, ins);
7840
Eric Biedermanb138ac82003-04-22 18:44:01 +00007841 eat(state, TOK_COLON);
7842 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007843}
7844
7845static void switch_statement(struct compile_state *state, struct triple *first)
7846{
7847 FINISHME();
7848 eat(state, TOK_SWITCH);
7849 eat(state, TOK_LPAREN);
7850 expr(state);
7851 eat(state, TOK_RPAREN);
7852 statement(state, first);
7853 error(state, 0, "switch statements are not implemented");
7854 FINISHME();
7855}
7856
7857static void case_statement(struct compile_state *state, struct triple *first)
7858{
7859 FINISHME();
7860 eat(state, TOK_CASE);
7861 constant_expr(state);
7862 eat(state, TOK_COLON);
7863 statement(state, first);
7864 error(state, 0, "case statements are not implemented");
7865 FINISHME();
7866}
7867
7868static void default_statement(struct compile_state *state, struct triple *first)
7869{
7870 FINISHME();
7871 eat(state, TOK_DEFAULT);
7872 eat(state, TOK_COLON);
7873 statement(state, first);
7874 error(state, 0, "default statements are not implemented");
7875 FINISHME();
7876}
7877
7878static void asm_statement(struct compile_state *state, struct triple *first)
7879{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007880 struct asm_info *info;
7881 struct {
7882 struct triple *constraint;
7883 struct triple *expr;
7884 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
7885 struct triple *def, *asm_str;
7886 int out, in, clobbers, more, colons, i;
7887
7888 eat(state, TOK_ASM);
7889 /* For now ignore the qualifiers */
7890 switch(peek(state)) {
7891 case TOK_CONST:
7892 eat(state, TOK_CONST);
7893 break;
7894 case TOK_VOLATILE:
7895 eat(state, TOK_VOLATILE);
7896 break;
7897 }
7898 eat(state, TOK_LPAREN);
7899 asm_str = string_constant(state);
7900
7901 colons = 0;
7902 out = in = clobbers = 0;
7903 /* Outputs */
7904 if ((colons == 0) && (peek(state) == TOK_COLON)) {
7905 eat(state, TOK_COLON);
7906 colons++;
7907 more = (peek(state) == TOK_LIT_STRING);
7908 while(more) {
7909 struct triple *var;
7910 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +00007911 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007912 more = 0;
7913 if (out > MAX_LHS) {
7914 error(state, 0, "Maximum output count exceeded.");
7915 }
7916 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +00007917 str = constraint->u.blob;
7918 if (str[0] != '=') {
7919 error(state, 0, "Output constraint does not start with =");
7920 }
7921 constraint->u.blob = str + 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007922 eat(state, TOK_LPAREN);
7923 var = conditional_expr(state);
7924 eat(state, TOK_RPAREN);
7925
7926 lvalue(state, var);
7927 out_param[out].constraint = constraint;
7928 out_param[out].expr = var;
7929 if (peek(state) == TOK_COMMA) {
7930 eat(state, TOK_COMMA);
7931 more = 1;
7932 }
7933 out++;
7934 }
7935 }
7936 /* Inputs */
7937 if ((colons == 1) && (peek(state) == TOK_COLON)) {
7938 eat(state, TOK_COLON);
7939 colons++;
7940 more = (peek(state) == TOK_LIT_STRING);
7941 while(more) {
7942 struct triple *val;
7943 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +00007944 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007945 more = 0;
7946 if (in > MAX_RHS) {
7947 error(state, 0, "Maximum input count exceeded.");
7948 }
7949 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +00007950 str = constraint->u.blob;
7951 if (digitp(str[0] && str[1] == '\0')) {
7952 int val;
7953 val = digval(str[0]);
7954 if ((val < 0) || (val >= out)) {
7955 error(state, 0, "Invalid input constraint %d", val);
7956 }
7957 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007958 eat(state, TOK_LPAREN);
7959 val = conditional_expr(state);
7960 eat(state, TOK_RPAREN);
7961
7962 in_param[in].constraint = constraint;
7963 in_param[in].expr = val;
7964 if (peek(state) == TOK_COMMA) {
7965 eat(state, TOK_COMMA);
7966 more = 1;
7967 }
7968 in++;
7969 }
7970 }
7971
7972 /* Clobber */
7973 if ((colons == 2) && (peek(state) == TOK_COLON)) {
7974 eat(state, TOK_COLON);
7975 colons++;
7976 more = (peek(state) == TOK_LIT_STRING);
7977 while(more) {
7978 struct triple *clobber;
7979 more = 0;
7980 if ((clobbers + out) > MAX_LHS) {
7981 error(state, 0, "Maximum clobber limit exceeded.");
7982 }
7983 clobber = string_constant(state);
7984 eat(state, TOK_RPAREN);
7985
7986 clob_param[clobbers].constraint = clobber;
7987 if (peek(state) == TOK_COMMA) {
7988 eat(state, TOK_COMMA);
7989 more = 1;
7990 }
7991 clobbers++;
7992 }
7993 }
7994 eat(state, TOK_RPAREN);
7995 eat(state, TOK_SEMI);
7996
7997
7998 info = xcmalloc(sizeof(*info), "asm_info");
7999 info->str = asm_str->u.blob;
8000 free_triple(state, asm_str);
8001
8002 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
8003 def->u.ainfo = info;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008004
8005 /* Find the register constraints */
8006 for(i = 0; i < out; i++) {
8007 struct triple *constraint;
8008 constraint = out_param[i].constraint;
8009 info->tmpl.lhs[i] = arch_reg_constraint(state,
8010 out_param[i].expr->type, constraint->u.blob);
8011 free_triple(state, constraint);
8012 }
8013 for(; i - out < clobbers; i++) {
8014 struct triple *constraint;
8015 constraint = clob_param[i - out].constraint;
8016 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
8017 free_triple(state, constraint);
8018 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008019 for(i = 0; i < in; i++) {
8020 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008021 const char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008022 constraint = in_param[i].constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008023 str = constraint->u.blob;
8024 if (digitp(str[0]) && str[1] == '\0') {
8025 struct reg_info cinfo;
8026 int val;
8027 val = digval(str[0]);
8028 cinfo.reg = info->tmpl.lhs[val].reg;
8029 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
8030 cinfo.regcm &= info->tmpl.lhs[val].regcm;
8031 if (cinfo.reg == REG_UNSET) {
8032 cinfo.reg = REG_VIRT0 + val;
8033 }
8034 if (cinfo.regcm == 0) {
8035 error(state, 0, "No registers for %d", val);
8036 }
8037 info->tmpl.lhs[val] = cinfo;
8038 info->tmpl.rhs[i] = cinfo;
8039
8040 } else {
8041 info->tmpl.rhs[i] = arch_reg_constraint(state,
8042 in_param[i].expr->type, str);
8043 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008044 free_triple(state, constraint);
8045 }
Eric Biederman8d9c1232003-06-17 08:42:17 +00008046
8047 /* Now build the helper expressions */
8048 for(i = 0; i < in; i++) {
8049 RHS(def, i) = read_expr(state,in_param[i].expr);
8050 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008051 flatten(state, first, def);
8052 for(i = 0; i < out; i++) {
8053 struct triple *piece;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008054 piece = triple(state, OP_PIECE, out_param[i].expr->type, def, 0);
8055 piece->u.cval = i;
8056 LHS(def, i) = piece;
8057 flatten(state, first,
8058 write_expr(state, out_param[i].expr, piece));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008059 }
8060 for(; i - out < clobbers; i++) {
8061 struct triple *piece;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008062 piece = triple(state, OP_PIECE, &void_type, def, 0);
8063 piece->u.cval = i;
8064 LHS(def, i) = piece;
8065 flatten(state, first, piece);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008066 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008067}
8068
8069
8070static int isdecl(int tok)
8071{
8072 switch(tok) {
8073 case TOK_AUTO:
8074 case TOK_REGISTER:
8075 case TOK_STATIC:
8076 case TOK_EXTERN:
8077 case TOK_TYPEDEF:
8078 case TOK_CONST:
8079 case TOK_RESTRICT:
8080 case TOK_VOLATILE:
8081 case TOK_VOID:
8082 case TOK_CHAR:
8083 case TOK_SHORT:
8084 case TOK_INT:
8085 case TOK_LONG:
8086 case TOK_FLOAT:
8087 case TOK_DOUBLE:
8088 case TOK_SIGNED:
8089 case TOK_UNSIGNED:
8090 case TOK_STRUCT:
8091 case TOK_UNION:
8092 case TOK_ENUM:
8093 case TOK_TYPE_NAME: /* typedef name */
8094 return 1;
8095 default:
8096 return 0;
8097 }
8098}
8099
8100static void compound_statement(struct compile_state *state, struct triple *first)
8101{
8102 eat(state, TOK_LBRACE);
8103 start_scope(state);
8104
8105 /* statement-list opt */
8106 while (peek(state) != TOK_RBRACE) {
8107 statement(state, first);
8108 }
8109 end_scope(state);
8110 eat(state, TOK_RBRACE);
8111}
8112
8113static void statement(struct compile_state *state, struct triple *first)
8114{
8115 int tok;
8116 tok = peek(state);
8117 if (tok == TOK_LBRACE) {
8118 compound_statement(state, first);
8119 }
8120 else if (tok == TOK_IF) {
8121 if_statement(state, first);
8122 }
8123 else if (tok == TOK_FOR) {
8124 for_statement(state, first);
8125 }
8126 else if (tok == TOK_WHILE) {
8127 while_statement(state, first);
8128 }
8129 else if (tok == TOK_DO) {
8130 do_statement(state, first);
8131 }
8132 else if (tok == TOK_RETURN) {
8133 return_statement(state, first);
8134 }
8135 else if (tok == TOK_BREAK) {
8136 break_statement(state, first);
8137 }
8138 else if (tok == TOK_CONTINUE) {
8139 continue_statement(state, first);
8140 }
8141 else if (tok == TOK_GOTO) {
8142 goto_statement(state, first);
8143 }
8144 else if (tok == TOK_SWITCH) {
8145 switch_statement(state, first);
8146 }
8147 else if (tok == TOK_ASM) {
8148 asm_statement(state, first);
8149 }
8150 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
8151 labeled_statement(state, first);
8152 }
8153 else if (tok == TOK_CASE) {
8154 case_statement(state, first);
8155 }
8156 else if (tok == TOK_DEFAULT) {
8157 default_statement(state, first);
8158 }
8159 else if (isdecl(tok)) {
8160 /* This handles C99 intermixing of statements and decls */
8161 decl(state, first);
8162 }
8163 else {
8164 expr_statement(state, first);
8165 }
8166}
8167
8168static struct type *param_decl(struct compile_state *state)
8169{
8170 struct type *type;
8171 struct hash_entry *ident;
8172 /* Cheat so the declarator will know we are not global */
8173 start_scope(state);
8174 ident = 0;
8175 type = decl_specifiers(state);
8176 type = declarator(state, type, &ident, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008177 type->field_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008178 end_scope(state);
8179 return type;
8180}
8181
8182static struct type *param_type_list(struct compile_state *state, struct type *type)
8183{
8184 struct type *ftype, **next;
8185 ftype = new_type(TYPE_FUNCTION, type, param_decl(state));
8186 next = &ftype->right;
8187 while(peek(state) == TOK_COMMA) {
8188 eat(state, TOK_COMMA);
8189 if (peek(state) == TOK_DOTS) {
8190 eat(state, TOK_DOTS);
8191 error(state, 0, "variadic functions not supported");
8192 }
8193 else {
8194 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
8195 next = &((*next)->right);
8196 }
8197 }
8198 return ftype;
8199}
8200
8201
8202static struct type *type_name(struct compile_state *state)
8203{
8204 struct type *type;
8205 type = specifier_qualifier_list(state);
8206 /* abstract-declarator (may consume no tokens) */
8207 type = declarator(state, type, 0, 0);
8208 return type;
8209}
8210
8211static struct type *direct_declarator(
8212 struct compile_state *state, struct type *type,
8213 struct hash_entry **ident, int need_ident)
8214{
8215 struct type *outer;
8216 int op;
8217 outer = 0;
8218 arrays_complete(state, type);
8219 switch(peek(state)) {
8220 case TOK_IDENT:
8221 eat(state, TOK_IDENT);
8222 if (!ident) {
8223 error(state, 0, "Unexpected identifier found");
8224 }
8225 /* The name of what we are declaring */
8226 *ident = state->token[0].ident;
8227 break;
8228 case TOK_LPAREN:
8229 eat(state, TOK_LPAREN);
8230 outer = declarator(state, type, ident, need_ident);
8231 eat(state, TOK_RPAREN);
8232 break;
8233 default:
8234 if (need_ident) {
8235 error(state, 0, "Identifier expected");
8236 }
8237 break;
8238 }
8239 do {
8240 op = 1;
8241 arrays_complete(state, type);
8242 switch(peek(state)) {
8243 case TOK_LPAREN:
8244 eat(state, TOK_LPAREN);
8245 type = param_type_list(state, type);
8246 eat(state, TOK_RPAREN);
8247 break;
8248 case TOK_LBRACKET:
8249 {
8250 unsigned int qualifiers;
8251 struct triple *value;
8252 value = 0;
8253 eat(state, TOK_LBRACKET);
8254 if (peek(state) != TOK_RBRACKET) {
8255 value = constant_expr(state);
8256 integral(state, value);
8257 }
8258 eat(state, TOK_RBRACKET);
8259
8260 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
8261 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
8262 if (value) {
8263 type->elements = value->u.cval;
8264 free_triple(state, value);
8265 } else {
8266 type->elements = ELEMENT_COUNT_UNSPECIFIED;
8267 op = 0;
8268 }
8269 }
8270 break;
8271 default:
8272 op = 0;
8273 break;
8274 }
8275 } while(op);
8276 if (outer) {
8277 struct type *inner;
8278 arrays_complete(state, type);
8279 FINISHME();
8280 for(inner = outer; inner->left; inner = inner->left)
8281 ;
8282 inner->left = type;
8283 type = outer;
8284 }
8285 return type;
8286}
8287
8288static struct type *declarator(
8289 struct compile_state *state, struct type *type,
8290 struct hash_entry **ident, int need_ident)
8291{
8292 while(peek(state) == TOK_STAR) {
8293 eat(state, TOK_STAR);
8294 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
8295 }
8296 type = direct_declarator(state, type, ident, need_ident);
8297 return type;
8298}
8299
8300
8301static struct type *typedef_name(
8302 struct compile_state *state, unsigned int specifiers)
8303{
8304 struct hash_entry *ident;
8305 struct type *type;
8306 eat(state, TOK_TYPE_NAME);
8307 ident = state->token[0].ident;
8308 type = ident->sym_ident->type;
8309 specifiers |= type->type & QUAL_MASK;
8310 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
8311 (type->type & (STOR_MASK | QUAL_MASK))) {
8312 type = clone_type(specifiers, type);
8313 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008314 return type;
8315}
8316
8317static struct type *enum_specifier(
8318 struct compile_state *state, unsigned int specifiers)
8319{
8320 int tok;
8321 struct type *type;
8322 type = 0;
8323 FINISHME();
8324 eat(state, TOK_ENUM);
8325 tok = peek(state);
8326 if (tok == TOK_IDENT) {
8327 eat(state, TOK_IDENT);
8328 }
8329 if ((tok != TOK_IDENT) || (peek(state) == TOK_LBRACE)) {
8330 eat(state, TOK_LBRACE);
8331 do {
8332 eat(state, TOK_IDENT);
8333 if (peek(state) == TOK_EQ) {
8334 eat(state, TOK_EQ);
8335 constant_expr(state);
8336 }
8337 if (peek(state) == TOK_COMMA) {
8338 eat(state, TOK_COMMA);
8339 }
8340 } while(peek(state) != TOK_RBRACE);
8341 eat(state, TOK_RBRACE);
8342 }
8343 FINISHME();
8344 return type;
8345}
8346
8347#if 0
8348static struct type *struct_declarator(
8349 struct compile_state *state, struct type *type, struct hash_entry **ident)
8350{
8351 int tok;
8352#warning "struct_declarator is complicated because of bitfields, kill them?"
8353 tok = peek(state);
8354 if (tok != TOK_COLON) {
8355 type = declarator(state, type, ident, 1);
8356 }
8357 if ((tok == TOK_COLON) || (peek(state) == TOK_COLON)) {
8358 eat(state, TOK_COLON);
8359 constant_expr(state);
8360 }
8361 FINISHME();
8362 return type;
8363}
8364#endif
8365
8366static struct type *struct_or_union_specifier(
8367 struct compile_state *state, unsigned int specifiers)
8368{
Eric Biederman0babc1c2003-05-09 02:39:00 +00008369 struct type *struct_type;
8370 struct hash_entry *ident;
8371 unsigned int type_join;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008372 int tok;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008373 struct_type = 0;
8374 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008375 switch(peek(state)) {
8376 case TOK_STRUCT:
8377 eat(state, TOK_STRUCT);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008378 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008379 break;
8380 case TOK_UNION:
8381 eat(state, TOK_UNION);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008382 type_join = TYPE_OVERLAP;
8383 error(state, 0, "unions not yet supported\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008384 break;
8385 default:
8386 eat(state, TOK_STRUCT);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008387 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008388 break;
8389 }
8390 tok = peek(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008391 if ((tok == TOK_IDENT) || (tok == TOK_TYPE_NAME)) {
8392 eat(state, tok);
8393 ident = state->token[0].ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008394 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00008395 if (!ident || (peek(state) == TOK_LBRACE)) {
8396 ulong_t elements;
8397 elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008398 eat(state, TOK_LBRACE);
8399 do {
8400 struct type *base_type;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008401 struct type **next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008402 int done;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008403 base_type = specifier_qualifier_list(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008404 next = &struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008405 do {
8406 struct type *type;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008407 struct hash_entry *fident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008408 done = 1;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008409 type = declarator(state, base_type, &fident, 1);
8410 elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008411 if (peek(state) == TOK_COMMA) {
8412 done = 0;
8413 eat(state, TOK_COMMA);
8414 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00008415 type = clone_type(0, type);
8416 type->field_ident = fident;
8417 if (*next) {
8418 *next = new_type(type_join, *next, type);
8419 next = &((*next)->right);
8420 } else {
8421 *next = type;
8422 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008423 } while(!done);
8424 eat(state, TOK_SEMI);
8425 } while(peek(state) != TOK_RBRACE);
8426 eat(state, TOK_RBRACE);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008427 struct_type = new_type(TYPE_STRUCT, struct_type, 0);
8428 struct_type->type_ident = ident;
8429 struct_type->elements = elements;
8430 symbol(state, ident, &ident->sym_struct, 0, struct_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008431 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00008432 if (ident && ident->sym_struct) {
8433 struct_type = ident->sym_struct->type;
8434 }
8435 else if (ident && !ident->sym_struct) {
8436 error(state, 0, "struct %s undeclared", ident->name);
8437 }
8438 return struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008439}
8440
8441static unsigned int storage_class_specifier_opt(struct compile_state *state)
8442{
8443 unsigned int specifiers;
8444 switch(peek(state)) {
8445 case TOK_AUTO:
8446 eat(state, TOK_AUTO);
8447 specifiers = STOR_AUTO;
8448 break;
8449 case TOK_REGISTER:
8450 eat(state, TOK_REGISTER);
8451 specifiers = STOR_REGISTER;
8452 break;
8453 case TOK_STATIC:
8454 eat(state, TOK_STATIC);
8455 specifiers = STOR_STATIC;
8456 break;
8457 case TOK_EXTERN:
8458 eat(state, TOK_EXTERN);
8459 specifiers = STOR_EXTERN;
8460 break;
8461 case TOK_TYPEDEF:
8462 eat(state, TOK_TYPEDEF);
8463 specifiers = STOR_TYPEDEF;
8464 break;
8465 default:
8466 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
8467 specifiers = STOR_STATIC;
8468 }
8469 else {
8470 specifiers = STOR_AUTO;
8471 }
8472 }
8473 return specifiers;
8474}
8475
8476static unsigned int function_specifier_opt(struct compile_state *state)
8477{
8478 /* Ignore the inline keyword */
8479 unsigned int specifiers;
8480 specifiers = 0;
8481 switch(peek(state)) {
8482 case TOK_INLINE:
8483 eat(state, TOK_INLINE);
8484 specifiers = STOR_INLINE;
8485 }
8486 return specifiers;
8487}
8488
8489static unsigned int type_qualifiers(struct compile_state *state)
8490{
8491 unsigned int specifiers;
8492 int done;
8493 done = 0;
8494 specifiers = QUAL_NONE;
8495 do {
8496 switch(peek(state)) {
8497 case TOK_CONST:
8498 eat(state, TOK_CONST);
8499 specifiers = QUAL_CONST;
8500 break;
8501 case TOK_VOLATILE:
8502 eat(state, TOK_VOLATILE);
8503 specifiers = QUAL_VOLATILE;
8504 break;
8505 case TOK_RESTRICT:
8506 eat(state, TOK_RESTRICT);
8507 specifiers = QUAL_RESTRICT;
8508 break;
8509 default:
8510 done = 1;
8511 break;
8512 }
8513 } while(!done);
8514 return specifiers;
8515}
8516
8517static struct type *type_specifier(
8518 struct compile_state *state, unsigned int spec)
8519{
8520 struct type *type;
8521 type = 0;
8522 switch(peek(state)) {
8523 case TOK_VOID:
8524 eat(state, TOK_VOID);
8525 type = new_type(TYPE_VOID | spec, 0, 0);
8526 break;
8527 case TOK_CHAR:
8528 eat(state, TOK_CHAR);
8529 type = new_type(TYPE_CHAR | spec, 0, 0);
8530 break;
8531 case TOK_SHORT:
8532 eat(state, TOK_SHORT);
8533 if (peek(state) == TOK_INT) {
8534 eat(state, TOK_INT);
8535 }
8536 type = new_type(TYPE_SHORT | spec, 0, 0);
8537 break;
8538 case TOK_INT:
8539 eat(state, TOK_INT);
8540 type = new_type(TYPE_INT | spec, 0, 0);
8541 break;
8542 case TOK_LONG:
8543 eat(state, TOK_LONG);
8544 switch(peek(state)) {
8545 case TOK_LONG:
8546 eat(state, TOK_LONG);
8547 error(state, 0, "long long not supported");
8548 break;
8549 case TOK_DOUBLE:
8550 eat(state, TOK_DOUBLE);
8551 error(state, 0, "long double not supported");
8552 break;
8553 case TOK_INT:
8554 eat(state, TOK_INT);
8555 type = new_type(TYPE_LONG | spec, 0, 0);
8556 break;
8557 default:
8558 type = new_type(TYPE_LONG | spec, 0, 0);
8559 break;
8560 }
8561 break;
8562 case TOK_FLOAT:
8563 eat(state, TOK_FLOAT);
8564 error(state, 0, "type float not supported");
8565 break;
8566 case TOK_DOUBLE:
8567 eat(state, TOK_DOUBLE);
8568 error(state, 0, "type double not supported");
8569 break;
8570 case TOK_SIGNED:
8571 eat(state, TOK_SIGNED);
8572 switch(peek(state)) {
8573 case TOK_LONG:
8574 eat(state, TOK_LONG);
8575 switch(peek(state)) {
8576 case TOK_LONG:
8577 eat(state, TOK_LONG);
8578 error(state, 0, "type long long not supported");
8579 break;
8580 case TOK_INT:
8581 eat(state, TOK_INT);
8582 type = new_type(TYPE_LONG | spec, 0, 0);
8583 break;
8584 default:
8585 type = new_type(TYPE_LONG | spec, 0, 0);
8586 break;
8587 }
8588 break;
8589 case TOK_INT:
8590 eat(state, TOK_INT);
8591 type = new_type(TYPE_INT | spec, 0, 0);
8592 break;
8593 case TOK_SHORT:
8594 eat(state, TOK_SHORT);
8595 type = new_type(TYPE_SHORT | spec, 0, 0);
8596 break;
8597 case TOK_CHAR:
8598 eat(state, TOK_CHAR);
8599 type = new_type(TYPE_CHAR | spec, 0, 0);
8600 break;
8601 default:
8602 type = new_type(TYPE_INT | spec, 0, 0);
8603 break;
8604 }
8605 break;
8606 case TOK_UNSIGNED:
8607 eat(state, TOK_UNSIGNED);
8608 switch(peek(state)) {
8609 case TOK_LONG:
8610 eat(state, TOK_LONG);
8611 switch(peek(state)) {
8612 case TOK_LONG:
8613 eat(state, TOK_LONG);
8614 error(state, 0, "unsigned long long not supported");
8615 break;
8616 case TOK_INT:
8617 eat(state, TOK_INT);
8618 type = new_type(TYPE_ULONG | spec, 0, 0);
8619 break;
8620 default:
8621 type = new_type(TYPE_ULONG | spec, 0, 0);
8622 break;
8623 }
8624 break;
8625 case TOK_INT:
8626 eat(state, TOK_INT);
8627 type = new_type(TYPE_UINT | spec, 0, 0);
8628 break;
8629 case TOK_SHORT:
8630 eat(state, TOK_SHORT);
8631 type = new_type(TYPE_USHORT | spec, 0, 0);
8632 break;
8633 case TOK_CHAR:
8634 eat(state, TOK_CHAR);
8635 type = new_type(TYPE_UCHAR | spec, 0, 0);
8636 break;
8637 default:
8638 type = new_type(TYPE_UINT | spec, 0, 0);
8639 break;
8640 }
8641 break;
8642 /* struct or union specifier */
8643 case TOK_STRUCT:
8644 case TOK_UNION:
8645 type = struct_or_union_specifier(state, spec);
8646 break;
8647 /* enum-spefifier */
8648 case TOK_ENUM:
8649 type = enum_specifier(state, spec);
8650 break;
8651 /* typedef name */
8652 case TOK_TYPE_NAME:
8653 type = typedef_name(state, spec);
8654 break;
8655 default:
8656 error(state, 0, "bad type specifier %s",
8657 tokens[peek(state)]);
8658 break;
8659 }
8660 return type;
8661}
8662
8663static int istype(int tok)
8664{
8665 switch(tok) {
8666 case TOK_CONST:
8667 case TOK_RESTRICT:
8668 case TOK_VOLATILE:
8669 case TOK_VOID:
8670 case TOK_CHAR:
8671 case TOK_SHORT:
8672 case TOK_INT:
8673 case TOK_LONG:
8674 case TOK_FLOAT:
8675 case TOK_DOUBLE:
8676 case TOK_SIGNED:
8677 case TOK_UNSIGNED:
8678 case TOK_STRUCT:
8679 case TOK_UNION:
8680 case TOK_ENUM:
8681 case TOK_TYPE_NAME:
8682 return 1;
8683 default:
8684 return 0;
8685 }
8686}
8687
8688
8689static struct type *specifier_qualifier_list(struct compile_state *state)
8690{
8691 struct type *type;
8692 unsigned int specifiers = 0;
8693
8694 /* type qualifiers */
8695 specifiers |= type_qualifiers(state);
8696
8697 /* type specifier */
8698 type = type_specifier(state, specifiers);
8699
8700 return type;
8701}
8702
8703static int isdecl_specifier(int tok)
8704{
8705 switch(tok) {
8706 /* storage class specifier */
8707 case TOK_AUTO:
8708 case TOK_REGISTER:
8709 case TOK_STATIC:
8710 case TOK_EXTERN:
8711 case TOK_TYPEDEF:
8712 /* type qualifier */
8713 case TOK_CONST:
8714 case TOK_RESTRICT:
8715 case TOK_VOLATILE:
8716 /* type specifiers */
8717 case TOK_VOID:
8718 case TOK_CHAR:
8719 case TOK_SHORT:
8720 case TOK_INT:
8721 case TOK_LONG:
8722 case TOK_FLOAT:
8723 case TOK_DOUBLE:
8724 case TOK_SIGNED:
8725 case TOK_UNSIGNED:
8726 /* struct or union specifier */
8727 case TOK_STRUCT:
8728 case TOK_UNION:
8729 /* enum-spefifier */
8730 case TOK_ENUM:
8731 /* typedef name */
8732 case TOK_TYPE_NAME:
8733 /* function specifiers */
8734 case TOK_INLINE:
8735 return 1;
8736 default:
8737 return 0;
8738 }
8739}
8740
8741static struct type *decl_specifiers(struct compile_state *state)
8742{
8743 struct type *type;
8744 unsigned int specifiers;
8745 /* I am overly restrictive in the arragement of specifiers supported.
8746 * C is overly flexible in this department it makes interpreting
8747 * the parse tree difficult.
8748 */
8749 specifiers = 0;
8750
8751 /* storage class specifier */
8752 specifiers |= storage_class_specifier_opt(state);
8753
8754 /* function-specifier */
8755 specifiers |= function_specifier_opt(state);
8756
8757 /* type qualifier */
8758 specifiers |= type_qualifiers(state);
8759
8760 /* type specifier */
8761 type = type_specifier(state, specifiers);
8762 return type;
8763}
8764
8765static unsigned designator(struct compile_state *state)
8766{
8767 int tok;
8768 unsigned index;
8769 index = -1U;
8770 do {
8771 switch(peek(state)) {
8772 case TOK_LBRACKET:
8773 {
8774 struct triple *value;
8775 eat(state, TOK_LBRACKET);
8776 value = constant_expr(state);
8777 eat(state, TOK_RBRACKET);
8778 index = value->u.cval;
8779 break;
8780 }
8781 case TOK_DOT:
8782 eat(state, TOK_DOT);
8783 eat(state, TOK_IDENT);
8784 error(state, 0, "Struct Designators not currently supported");
8785 break;
8786 default:
8787 error(state, 0, "Invalid designator");
8788 }
8789 tok = peek(state);
8790 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
8791 eat(state, TOK_EQ);
8792 return index;
8793}
8794
8795static struct triple *initializer(
8796 struct compile_state *state, struct type *type)
8797{
8798 struct triple *result;
8799 if (peek(state) != TOK_LBRACE) {
8800 result = assignment_expr(state);
8801 }
8802 else {
8803 int comma;
8804 unsigned index, max_index;
8805 void *buf;
8806 max_index = index = 0;
8807 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
8808 max_index = type->elements;
8809 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
8810 type->elements = 0;
8811 }
8812 } else {
8813 error(state, 0, "Struct initializers not currently supported");
8814 }
8815 buf = xcmalloc(size_of(state, type), "initializer");
8816 eat(state, TOK_LBRACE);
8817 do {
8818 struct triple *value;
8819 struct type *value_type;
8820 size_t value_size;
8821 int tok;
8822 comma = 0;
8823 tok = peek(state);
8824 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
8825 index = designator(state);
8826 }
8827 if ((max_index != ELEMENT_COUNT_UNSPECIFIED) &&
8828 (index > max_index)) {
8829 error(state, 0, "element beyond bounds");
8830 }
8831 value_type = 0;
8832 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
8833 value_type = type->left;
8834 }
8835 value = eval_const_expr(state, initializer(state, value_type));
8836 value_size = size_of(state, value_type);
8837 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
8838 (max_index == ELEMENT_COUNT_UNSPECIFIED) &&
8839 (type->elements <= index)) {
8840 void *old_buf;
8841 size_t old_size;
8842 old_buf = buf;
8843 old_size = size_of(state, type);
8844 type->elements = index + 1;
8845 buf = xmalloc(size_of(state, type), "initializer");
8846 memcpy(buf, old_buf, old_size);
8847 xfree(old_buf);
8848 }
8849 if (value->op == OP_BLOBCONST) {
8850 memcpy((char *)buf + index * value_size, value->u.blob, value_size);
8851 }
8852 else if ((value->op == OP_INTCONST) && (value_size == 1)) {
8853 *(((uint8_t *)buf) + index) = value->u.cval & 0xff;
8854 }
8855 else if ((value->op == OP_INTCONST) && (value_size == 2)) {
8856 *(((uint16_t *)buf) + index) = value->u.cval & 0xffff;
8857 }
8858 else if ((value->op == OP_INTCONST) && (value_size == 4)) {
8859 *(((uint32_t *)buf) + index) = value->u.cval & 0xffffffff;
8860 }
8861 else {
8862 fprintf(stderr, "%d %d\n",
8863 value->op, value_size);
8864 internal_error(state, 0, "unhandled constant initializer");
8865 }
8866 if (peek(state) == TOK_COMMA) {
8867 eat(state, TOK_COMMA);
8868 comma = 1;
8869 }
8870 index += 1;
8871 } while(comma && (peek(state) != TOK_RBRACE));
8872 eat(state, TOK_RBRACE);
8873 result = triple(state, OP_BLOBCONST, type, 0, 0);
8874 result->u.blob = buf;
8875 }
8876 return result;
8877}
8878
Eric Biederman153ea352003-06-20 14:43:20 +00008879static void resolve_branches(struct compile_state *state)
8880{
8881 /* Make a second pass and finish anything outstanding
8882 * with respect to branches. The only outstanding item
8883 * is to see if there are goto to labels that have not
8884 * been defined and to error about them.
8885 */
8886 int i;
8887 for(i = 0; i < HASH_TABLE_SIZE; i++) {
8888 struct hash_entry *entry;
8889 for(entry = state->hash_table[i]; entry; entry = entry->next) {
8890 struct triple *ins;
8891 if (!entry->sym_label) {
8892 continue;
8893 }
8894 ins = entry->sym_label->def;
8895 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
8896 error(state, ins, "label `%s' used but not defined",
8897 entry->name);
8898 }
8899 }
8900 }
8901}
8902
Eric Biedermanb138ac82003-04-22 18:44:01 +00008903static struct triple *function_definition(
8904 struct compile_state *state, struct type *type)
8905{
8906 struct triple *def, *tmp, *first, *end;
8907 struct hash_entry *ident;
8908 struct type *param;
8909 int i;
8910 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
8911 error(state, 0, "Invalid function header");
8912 }
8913
8914 /* Verify the function type */
8915 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
8916 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +00008917 (type->right->field_ident == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008918 error(state, 0, "Invalid function parameters");
8919 }
8920 param = type->right;
8921 i = 0;
8922 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
8923 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008924 if (!param->left->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008925 error(state, 0, "No identifier for parameter %d\n", i);
8926 }
8927 param = param->right;
8928 }
8929 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008930 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008931 error(state, 0, "No identifier for paramter %d\n", i);
8932 }
8933
8934 /* Get a list of statements for this function. */
8935 def = triple(state, OP_LIST, type, 0, 0);
8936
8937 /* Start a new scope for the passed parameters */
8938 start_scope(state);
8939
8940 /* Put a label at the very start of a function */
8941 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008942 RHS(def, 0) = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008943
8944 /* Put a label at the very end of a function */
8945 end = label(state);
8946 flatten(state, first, end);
8947
8948 /* Walk through the parameters and create symbol table entries
8949 * for them.
8950 */
8951 param = type->right;
8952 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008953 ident = param->left->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008954 tmp = variable(state, param->left);
8955 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
8956 flatten(state, end, tmp);
8957 param = param->right;
8958 }
8959 if ((param->type & TYPE_MASK) != TYPE_VOID) {
8960 /* And don't forget the last parameter */
Eric Biederman0babc1c2003-05-09 02:39:00 +00008961 ident = param->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008962 tmp = variable(state, param);
8963 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
8964 flatten(state, end, tmp);
8965 }
8966 /* Add a variable for the return value */
Eric Biederman0babc1c2003-05-09 02:39:00 +00008967 MISC(def, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008968 if ((type->left->type & TYPE_MASK) != TYPE_VOID) {
8969 /* Remove all type qualifiers from the return type */
8970 tmp = variable(state, clone_type(0, type->left));
8971 flatten(state, end, tmp);
8972 /* Remember where the return value is */
Eric Biederman0babc1c2003-05-09 02:39:00 +00008973 MISC(def, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008974 }
8975
8976 /* Remember which function I am compiling.
8977 * Also assume the last defined function is the main function.
8978 */
8979 state->main_function = def;
8980
8981 /* Now get the actual function definition */
8982 compound_statement(state, end);
8983
Eric Biederman153ea352003-06-20 14:43:20 +00008984 /* Finish anything unfinished with branches */
8985 resolve_branches(state);
8986
Eric Biedermanb138ac82003-04-22 18:44:01 +00008987 /* Remove the parameter scope */
8988 end_scope(state);
Eric Biederman153ea352003-06-20 14:43:20 +00008989
Eric Biedermanb138ac82003-04-22 18:44:01 +00008990#if 0
8991 fprintf(stdout, "\n");
8992 loc(stdout, state, 0);
8993 fprintf(stdout, "\n__________ function_definition _________\n");
8994 print_triple(state, def);
8995 fprintf(stdout, "__________ function_definition _________ done\n\n");
8996#endif
8997
8998 return def;
8999}
9000
9001static struct triple *do_decl(struct compile_state *state,
9002 struct type *type, struct hash_entry *ident)
9003{
9004 struct triple *def;
9005 def = 0;
9006 /* Clean up the storage types used */
9007 switch (type->type & STOR_MASK) {
9008 case STOR_AUTO:
9009 case STOR_STATIC:
9010 /* These are the good types I am aiming for */
9011 break;
9012 case STOR_REGISTER:
9013 type->type &= ~STOR_MASK;
9014 type->type |= STOR_AUTO;
9015 break;
9016 case STOR_EXTERN:
9017 type->type &= ~STOR_MASK;
9018 type->type |= STOR_STATIC;
9019 break;
9020 case STOR_TYPEDEF:
Eric Biederman0babc1c2003-05-09 02:39:00 +00009021 if (!ident) {
9022 error(state, 0, "typedef without name");
9023 }
9024 symbol(state, ident, &ident->sym_ident, 0, type);
9025 ident->tok = TOK_TYPE_NAME;
9026 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009027 break;
9028 default:
9029 internal_error(state, 0, "Undefined storage class");
9030 }
9031 if (((type->type & STOR_MASK) == STOR_STATIC) &&
9032 ((type->type & QUAL_CONST) == 0)) {
9033 error(state, 0, "non const static variables not supported");
9034 }
9035 if (ident) {
9036 def = variable(state, type);
9037 symbol(state, ident, &ident->sym_ident, def, type);
9038 }
9039 return def;
9040}
9041
9042static void decl(struct compile_state *state, struct triple *first)
9043{
9044 struct type *base_type, *type;
9045 struct hash_entry *ident;
9046 struct triple *def;
9047 int global;
9048 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
9049 base_type = decl_specifiers(state);
9050 ident = 0;
9051 type = declarator(state, base_type, &ident, 0);
9052 if (global && ident && (peek(state) == TOK_LBRACE)) {
9053 /* function */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00009054 state->function = ident->name;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009055 def = function_definition(state, type);
9056 symbol(state, ident, &ident->sym_ident, def, type);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00009057 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009058 }
9059 else {
9060 int done;
9061 flatten(state, first, do_decl(state, type, ident));
9062 /* type or variable definition */
9063 do {
9064 done = 1;
9065 if (peek(state) == TOK_EQ) {
9066 if (!ident) {
9067 error(state, 0, "cannot assign to a type");
9068 }
9069 eat(state, TOK_EQ);
9070 flatten(state, first,
9071 init_expr(state,
9072 ident->sym_ident->def,
9073 initializer(state, type)));
9074 }
9075 arrays_complete(state, type);
9076 if (peek(state) == TOK_COMMA) {
9077 eat(state, TOK_COMMA);
9078 ident = 0;
9079 type = declarator(state, base_type, &ident, 0);
9080 flatten(state, first, do_decl(state, type, ident));
9081 done = 0;
9082 }
9083 } while(!done);
9084 eat(state, TOK_SEMI);
9085 }
9086}
9087
9088static void decls(struct compile_state *state)
9089{
9090 struct triple *list;
9091 int tok;
9092 list = label(state);
9093 while(1) {
9094 tok = peek(state);
9095 if (tok == TOK_EOF) {
9096 return;
9097 }
9098 if (tok == TOK_SPACE) {
9099 eat(state, TOK_SPACE);
9100 }
9101 decl(state, list);
9102 if (list->next != list) {
9103 error(state, 0, "global variables not supported");
9104 }
9105 }
9106}
9107
9108/*
9109 * Data structurs for optimation.
9110 */
9111
9112static void do_use_block(
9113 struct block *used, struct block_set **head, struct block *user,
9114 int front)
9115{
9116 struct block_set **ptr, *new;
9117 if (!used)
9118 return;
9119 if (!user)
9120 return;
9121 ptr = head;
9122 while(*ptr) {
9123 if ((*ptr)->member == user) {
9124 return;
9125 }
9126 ptr = &(*ptr)->next;
9127 }
9128 new = xcmalloc(sizeof(*new), "block_set");
9129 new->member = user;
9130 if (front) {
9131 new->next = *head;
9132 *head = new;
9133 }
9134 else {
9135 new->next = 0;
9136 *ptr = new;
9137 }
9138}
9139static void do_unuse_block(
9140 struct block *used, struct block_set **head, struct block *unuser)
9141{
9142 struct block_set *use, **ptr;
9143 ptr = head;
9144 while(*ptr) {
9145 use = *ptr;
9146 if (use->member == unuser) {
9147 *ptr = use->next;
9148 memset(use, -1, sizeof(*use));
9149 xfree(use);
9150 }
9151 else {
9152 ptr = &use->next;
9153 }
9154 }
9155}
9156
9157static void use_block(struct block *used, struct block *user)
9158{
9159 /* Append new to the head of the list, print_block
9160 * depends on this.
9161 */
9162 do_use_block(used, &used->use, user, 1);
9163 used->users++;
9164}
9165static void unuse_block(struct block *used, struct block *unuser)
9166{
9167 do_unuse_block(used, &used->use, unuser);
9168 used->users--;
9169}
9170
9171static void idom_block(struct block *idom, struct block *user)
9172{
9173 do_use_block(idom, &idom->idominates, user, 0);
9174}
9175
9176static void unidom_block(struct block *idom, struct block *unuser)
9177{
9178 do_unuse_block(idom, &idom->idominates, unuser);
9179}
9180
9181static void domf_block(struct block *block, struct block *domf)
9182{
9183 do_use_block(block, &block->domfrontier, domf, 0);
9184}
9185
9186static void undomf_block(struct block *block, struct block *undomf)
9187{
9188 do_unuse_block(block, &block->domfrontier, undomf);
9189}
9190
9191static void ipdom_block(struct block *ipdom, struct block *user)
9192{
9193 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
9194}
9195
9196static void unipdom_block(struct block *ipdom, struct block *unuser)
9197{
9198 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
9199}
9200
9201static void ipdomf_block(struct block *block, struct block *ipdomf)
9202{
9203 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
9204}
9205
9206static void unipdomf_block(struct block *block, struct block *unipdomf)
9207{
9208 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
9209}
9210
9211
9212
9213static int do_walk_triple(struct compile_state *state,
9214 struct triple *ptr, int depth,
9215 int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
9216{
9217 int result;
9218 result = cb(state, ptr, depth);
9219 if ((result == 0) && (ptr->op == OP_LIST)) {
9220 struct triple *list;
9221 list = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009222 ptr = RHS(list, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009223 do {
9224 result = do_walk_triple(state, ptr, depth + 1, cb);
9225 if (ptr->next->prev != ptr) {
9226 internal_error(state, ptr->next, "bad prev");
9227 }
9228 ptr = ptr->next;
9229
Eric Biederman0babc1c2003-05-09 02:39:00 +00009230 } while((result == 0) && (ptr != RHS(list, 0)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009231 }
9232 return result;
9233}
9234
9235static int walk_triple(
9236 struct compile_state *state,
9237 struct triple *ptr,
9238 int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
9239{
9240 return do_walk_triple(state, ptr, 0, cb);
9241}
9242
9243static void do_print_prefix(int depth)
9244{
9245 int i;
9246 for(i = 0; i < depth; i++) {
9247 printf(" ");
9248 }
9249}
9250
9251#define PRINT_LIST 1
9252static int do_print_triple(struct compile_state *state, struct triple *ins, int depth)
9253{
9254 int op;
9255 op = ins->op;
9256 if (op == OP_LIST) {
9257#if !PRINT_LIST
9258 return 0;
9259#endif
9260 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009261 if ((op == OP_LABEL) && (ins->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009262 printf("\n%p:\n", ins);
9263 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009264 do_print_prefix(depth);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009265 display_triple(stdout, ins);
9266
Eric Biedermanb138ac82003-04-22 18:44:01 +00009267 if ((ins->op == OP_BRANCH) && ins->use) {
9268 internal_error(state, ins, "branch used?");
9269 }
9270#if 0
9271 {
9272 struct triple_set *user;
9273 for(user = ins->use; user; user = user->next) {
9274 printf("use: %p\n", user->member);
9275 }
9276 }
9277#endif
Eric Biederman0babc1c2003-05-09 02:39:00 +00009278 if (triple_is_branch(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009279 printf("\n");
9280 }
9281 return 0;
9282}
9283
9284static void print_triple(struct compile_state *state, struct triple *ins)
9285{
9286 walk_triple(state, ins, do_print_triple);
9287}
9288
9289static void print_triples(struct compile_state *state)
9290{
9291 print_triple(state, state->main_function);
9292}
9293
9294struct cf_block {
9295 struct block *block;
9296};
9297static void find_cf_blocks(struct cf_block *cf, struct block *block)
9298{
9299 if (!block || (cf[block->vertex].block == block)) {
9300 return;
9301 }
9302 cf[block->vertex].block = block;
9303 find_cf_blocks(cf, block->left);
9304 find_cf_blocks(cf, block->right);
9305}
9306
9307static void print_control_flow(struct compile_state *state)
9308{
9309 struct cf_block *cf;
9310 int i;
9311 printf("\ncontrol flow\n");
9312 cf = xcmalloc(sizeof(*cf) * (state->last_vertex + 1), "cf_block");
9313 find_cf_blocks(cf, state->first_block);
9314
9315 for(i = 1; i <= state->last_vertex; i++) {
9316 struct block *block;
9317 block = cf[i].block;
9318 if (!block)
9319 continue;
9320 printf("(%p) %d:", block, block->vertex);
9321 if (block->left) {
9322 printf(" %d", block->left->vertex);
9323 }
9324 if (block->right && (block->right != block->left)) {
9325 printf(" %d", block->right->vertex);
9326 }
9327 printf("\n");
9328 }
9329
9330 xfree(cf);
9331}
9332
9333
9334static struct block *basic_block(struct compile_state *state,
9335 struct triple *first)
9336{
9337 struct block *block;
9338 struct triple *ptr;
9339 int op;
9340 if (first->op != OP_LABEL) {
9341 internal_error(state, 0, "block does not start with a label");
9342 }
9343 /* See if this basic block has already been setup */
9344 if (first->u.block != 0) {
9345 return first->u.block;
9346 }
9347 /* Allocate another basic block structure */
9348 state->last_vertex += 1;
9349 block = xcmalloc(sizeof(*block), "block");
9350 block->first = block->last = first;
9351 block->vertex = state->last_vertex;
9352 ptr = first;
9353 do {
9354 if ((ptr != first) && (ptr->op == OP_LABEL) && ptr->use) {
9355 break;
9356 }
9357 block->last = ptr;
9358 /* If ptr->u is not used remember where the baic block is */
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009359 if (triple_stores_block(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009360 ptr->u.block = block;
9361 }
9362 if (ptr->op == OP_BRANCH) {
9363 break;
9364 }
9365 ptr = ptr->next;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009366 } while (ptr != RHS(state->main_function, 0));
9367 if (ptr == RHS(state->main_function, 0))
Eric Biedermanb138ac82003-04-22 18:44:01 +00009368 return block;
9369 op = ptr->op;
9370 if (op == OP_LABEL) {
9371 block->left = basic_block(state, ptr);
9372 block->right = 0;
9373 use_block(block->left, block);
9374 }
9375 else if (op == OP_BRANCH) {
9376 block->left = 0;
9377 /* Trace the branch target */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009378 block->right = basic_block(state, TARG(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009379 use_block(block->right, block);
9380 /* If there is a test trace the branch as well */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009381 if (TRIPLE_RHS(ptr->sizes)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009382 block->left = basic_block(state, ptr->next);
9383 use_block(block->left, block);
9384 }
9385 }
9386 else {
9387 internal_error(state, 0, "Bad basic block split");
9388 }
9389 return block;
9390}
9391
9392
9393static void walk_blocks(struct compile_state *state,
9394 void (*cb)(struct compile_state *state, struct block *block, void *arg),
9395 void *arg)
9396{
9397 struct triple *ptr, *first;
9398 struct block *last_block;
9399 last_block = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009400 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009401 ptr = first;
9402 do {
9403 struct block *block;
9404 if (ptr->op == OP_LABEL) {
9405 block = ptr->u.block;
9406 if (block && (block != last_block)) {
9407 cb(state, block, arg);
9408 }
9409 last_block = block;
9410 }
9411 ptr = ptr->next;
9412 } while(ptr != first);
9413}
9414
9415static void print_block(
9416 struct compile_state *state, struct block *block, void *arg)
9417{
9418 struct triple *ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009419 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009420
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009421 fprintf(fp, "\nblock: %p (%d), %p<-%p %p<-%p\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +00009422 block,
9423 block->vertex,
9424 block->left,
9425 block->left && block->left->use?block->left->use->member : 0,
9426 block->right,
9427 block->right && block->right->use?block->right->use->member : 0);
9428 if (block->first->op == OP_LABEL) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009429 fprintf(fp, "%p:\n", block->first);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009430 }
9431 for(ptr = block->first; ; ptr = ptr->next) {
9432 struct triple_set *user;
9433 int op = ptr->op;
9434
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009435 if (triple_stores_block(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009436 if (ptr->u.block != block) {
9437 internal_error(state, ptr,
9438 "Wrong block pointer: %p\n",
9439 ptr->u.block);
9440 }
9441 }
9442 if (op == OP_ADECL) {
9443 for(user = ptr->use; user; user = user->next) {
9444 if (!user->member->u.block) {
9445 internal_error(state, user->member,
9446 "Use %p not in a block?\n",
9447 user->member);
9448 }
9449 }
9450 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009451 display_triple(fp, ptr);
9452
9453#if 0
9454 for(user = ptr->use; user; user = user->next) {
9455 fprintf(fp, "use: %p\n", user->member);
9456 }
9457#endif
Eric Biederman0babc1c2003-05-09 02:39:00 +00009458
Eric Biedermanb138ac82003-04-22 18:44:01 +00009459 /* Sanity checks... */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009460 valid_ins(state, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009461 for(user = ptr->use; user; user = user->next) {
9462 struct triple *use;
9463 use = user->member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009464 valid_ins(state, use);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009465 if (triple_stores_block(state, user->member) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00009466 !user->member->u.block) {
9467 internal_error(state, user->member,
9468 "Use %p not in a block?",
9469 user->member);
9470 }
9471 }
9472
9473 if (ptr == block->last)
9474 break;
9475 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009476 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00009477}
9478
9479
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009480static void print_blocks(struct compile_state *state, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +00009481{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009482 fprintf(fp, "--------------- blocks ---------------\n");
9483 walk_blocks(state, print_block, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009484}
9485
9486static void prune_nonblock_triples(struct compile_state *state)
9487{
9488 struct block *block;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009489 struct triple *first, *ins, *next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009490 /* Delete the triples not in a basic block */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009491 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009492 block = 0;
9493 ins = first;
9494 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009495 next = ins->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009496 if (ins->op == OP_LABEL) {
9497 block = ins->u.block;
9498 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009499 if (!block) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009500 release_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009501 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009502 ins = next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009503 } while(ins != first);
9504}
9505
9506static void setup_basic_blocks(struct compile_state *state)
9507{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009508 if (!triple_stores_block(state, RHS(state->main_function, 0)) ||
9509 !triple_stores_block(state, RHS(state->main_function,0)->prev)) {
9510 internal_error(state, 0, "ins will not store block?");
9511 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009512 /* Find the basic blocks */
9513 state->last_vertex = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009514 state->first_block = basic_block(state, RHS(state->main_function,0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009515 /* Delete the triples not in a basic block */
9516 prune_nonblock_triples(state);
9517 /* Find the last basic block */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009518 state->last_block = RHS(state->main_function, 0)->prev->u.block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009519 if (!state->last_block) {
9520 internal_error(state, 0, "end not used?");
9521 }
9522 /* Insert an extra unused edge from start to the end
9523 * This helps with reverse control flow calculations.
9524 */
9525 use_block(state->first_block, state->last_block);
9526 /* If we are debugging print what I have just done */
9527 if (state->debug & DEBUG_BASIC_BLOCKS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009528 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009529 print_control_flow(state);
9530 }
9531}
9532
9533static void free_basic_block(struct compile_state *state, struct block *block)
9534{
9535 struct block_set *entry, *next;
9536 struct block *child;
9537 if (!block) {
9538 return;
9539 }
9540 if (block->vertex == -1) {
9541 return;
9542 }
9543 block->vertex = -1;
9544 if (block->left) {
9545 unuse_block(block->left, block);
9546 }
9547 if (block->right) {
9548 unuse_block(block->right, block);
9549 }
9550 if (block->idom) {
9551 unidom_block(block->idom, block);
9552 }
9553 block->idom = 0;
9554 if (block->ipdom) {
9555 unipdom_block(block->ipdom, block);
9556 }
9557 block->ipdom = 0;
9558 for(entry = block->use; entry; entry = next) {
9559 next = entry->next;
9560 child = entry->member;
9561 unuse_block(block, child);
9562 if (child->left == block) {
9563 child->left = 0;
9564 }
9565 if (child->right == block) {
9566 child->right = 0;
9567 }
9568 }
9569 for(entry = block->idominates; entry; entry = next) {
9570 next = entry->next;
9571 child = entry->member;
9572 unidom_block(block, child);
9573 child->idom = 0;
9574 }
9575 for(entry = block->domfrontier; entry; entry = next) {
9576 next = entry->next;
9577 child = entry->member;
9578 undomf_block(block, child);
9579 }
9580 for(entry = block->ipdominates; entry; entry = next) {
9581 next = entry->next;
9582 child = entry->member;
9583 unipdom_block(block, child);
9584 child->ipdom = 0;
9585 }
9586 for(entry = block->ipdomfrontier; entry; entry = next) {
9587 next = entry->next;
9588 child = entry->member;
9589 unipdomf_block(block, child);
9590 }
9591 if (block->users != 0) {
9592 internal_error(state, 0, "block still has users");
9593 }
9594 free_basic_block(state, block->left);
9595 block->left = 0;
9596 free_basic_block(state, block->right);
9597 block->right = 0;
9598 memset(block, -1, sizeof(*block));
9599 xfree(block);
9600}
9601
9602static void free_basic_blocks(struct compile_state *state)
9603{
9604 struct triple *first, *ins;
9605 free_basic_block(state, state->first_block);
9606 state->last_vertex = 0;
9607 state->first_block = state->last_block = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009608 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009609 ins = first;
9610 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009611 if (triple_stores_block(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009612 ins->u.block = 0;
9613 }
9614 ins = ins->next;
9615 } while(ins != first);
9616
9617}
9618
9619struct sdom_block {
9620 struct block *block;
9621 struct sdom_block *sdominates;
9622 struct sdom_block *sdom_next;
9623 struct sdom_block *sdom;
9624 struct sdom_block *label;
9625 struct sdom_block *parent;
9626 struct sdom_block *ancestor;
9627 int vertex;
9628};
9629
9630
9631static void unsdom_block(struct sdom_block *block)
9632{
9633 struct sdom_block **ptr;
9634 if (!block->sdom_next) {
9635 return;
9636 }
9637 ptr = &block->sdom->sdominates;
9638 while(*ptr) {
9639 if ((*ptr) == block) {
9640 *ptr = block->sdom_next;
9641 return;
9642 }
9643 ptr = &(*ptr)->sdom_next;
9644 }
9645}
9646
9647static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
9648{
9649 unsdom_block(block);
9650 block->sdom = sdom;
9651 block->sdom_next = sdom->sdominates;
9652 sdom->sdominates = block;
9653}
9654
9655
9656
9657static int initialize_sdblock(struct sdom_block *sd,
9658 struct block *parent, struct block *block, int vertex)
9659{
9660 if (!block || (sd[block->vertex].block == block)) {
9661 return vertex;
9662 }
9663 vertex += 1;
9664 /* Renumber the blocks in a convinient fashion */
9665 block->vertex = vertex;
9666 sd[vertex].block = block;
9667 sd[vertex].sdom = &sd[vertex];
9668 sd[vertex].label = &sd[vertex];
9669 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
9670 sd[vertex].ancestor = 0;
9671 sd[vertex].vertex = vertex;
9672 vertex = initialize_sdblock(sd, block, block->left, vertex);
9673 vertex = initialize_sdblock(sd, block, block->right, vertex);
9674 return vertex;
9675}
9676
9677static int initialize_sdpblock(struct sdom_block *sd,
9678 struct block *parent, struct block *block, int vertex)
9679{
9680 struct block_set *user;
9681 if (!block || (sd[block->vertex].block == block)) {
9682 return vertex;
9683 }
9684 vertex += 1;
9685 /* Renumber the blocks in a convinient fashion */
9686 block->vertex = vertex;
9687 sd[vertex].block = block;
9688 sd[vertex].sdom = &sd[vertex];
9689 sd[vertex].label = &sd[vertex];
9690 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
9691 sd[vertex].ancestor = 0;
9692 sd[vertex].vertex = vertex;
9693 for(user = block->use; user; user = user->next) {
9694 vertex = initialize_sdpblock(sd, block, user->member, vertex);
9695 }
9696 return vertex;
9697}
9698
9699static void compress_ancestors(struct sdom_block *v)
9700{
9701 /* This procedure assumes ancestor(v) != 0 */
9702 /* if (ancestor(ancestor(v)) != 0) {
9703 * compress(ancestor(ancestor(v)));
9704 * if (semi(label(ancestor(v))) < semi(label(v))) {
9705 * label(v) = label(ancestor(v));
9706 * }
9707 * ancestor(v) = ancestor(ancestor(v));
9708 * }
9709 */
9710 if (!v->ancestor) {
9711 return;
9712 }
9713 if (v->ancestor->ancestor) {
9714 compress_ancestors(v->ancestor->ancestor);
9715 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
9716 v->label = v->ancestor->label;
9717 }
9718 v->ancestor = v->ancestor->ancestor;
9719 }
9720}
9721
9722static void compute_sdom(struct compile_state *state, struct sdom_block *sd)
9723{
9724 int i;
9725 /* // step 2
9726 * for each v <= pred(w) {
9727 * u = EVAL(v);
9728 * if (semi[u] < semi[w] {
9729 * semi[w] = semi[u];
9730 * }
9731 * }
9732 * add w to bucket(vertex(semi[w]));
9733 * LINK(parent(w), w);
9734 *
9735 * // step 3
9736 * for each v <= bucket(parent(w)) {
9737 * delete v from bucket(parent(w));
9738 * u = EVAL(v);
9739 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
9740 * }
9741 */
9742 for(i = state->last_vertex; i >= 2; i--) {
9743 struct sdom_block *v, *parent, *next;
9744 struct block_set *user;
9745 struct block *block;
9746 block = sd[i].block;
9747 parent = sd[i].parent;
9748 /* Step 2 */
9749 for(user = block->use; user; user = user->next) {
9750 struct sdom_block *v, *u;
9751 v = &sd[user->member->vertex];
9752 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9753 if (u->sdom->vertex < sd[i].sdom->vertex) {
9754 sd[i].sdom = u->sdom;
9755 }
9756 }
9757 sdom_block(sd[i].sdom, &sd[i]);
9758 sd[i].ancestor = parent;
9759 /* Step 3 */
9760 for(v = parent->sdominates; v; v = next) {
9761 struct sdom_block *u;
9762 next = v->sdom_next;
9763 unsdom_block(v);
9764 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
9765 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
9766 u->block : parent->block;
9767 }
9768 }
9769}
9770
9771static void compute_spdom(struct compile_state *state, struct sdom_block *sd)
9772{
9773 int i;
9774 /* // step 2
9775 * for each v <= pred(w) {
9776 * u = EVAL(v);
9777 * if (semi[u] < semi[w] {
9778 * semi[w] = semi[u];
9779 * }
9780 * }
9781 * add w to bucket(vertex(semi[w]));
9782 * LINK(parent(w), w);
9783 *
9784 * // step 3
9785 * for each v <= bucket(parent(w)) {
9786 * delete v from bucket(parent(w));
9787 * u = EVAL(v);
9788 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
9789 * }
9790 */
9791 for(i = state->last_vertex; i >= 2; i--) {
9792 struct sdom_block *u, *v, *parent, *next;
9793 struct block *block;
9794 block = sd[i].block;
9795 parent = sd[i].parent;
9796 /* Step 2 */
9797 if (block->left) {
9798 v = &sd[block->left->vertex];
9799 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9800 if (u->sdom->vertex < sd[i].sdom->vertex) {
9801 sd[i].sdom = u->sdom;
9802 }
9803 }
9804 if (block->right && (block->right != block->left)) {
9805 v = &sd[block->right->vertex];
9806 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9807 if (u->sdom->vertex < sd[i].sdom->vertex) {
9808 sd[i].sdom = u->sdom;
9809 }
9810 }
9811 sdom_block(sd[i].sdom, &sd[i]);
9812 sd[i].ancestor = parent;
9813 /* Step 3 */
9814 for(v = parent->sdominates; v; v = next) {
9815 struct sdom_block *u;
9816 next = v->sdom_next;
9817 unsdom_block(v);
9818 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
9819 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
9820 u->block : parent->block;
9821 }
9822 }
9823}
9824
9825static void compute_idom(struct compile_state *state, struct sdom_block *sd)
9826{
9827 int i;
9828 for(i = 2; i <= state->last_vertex; i++) {
9829 struct block *block;
9830 block = sd[i].block;
9831 if (block->idom->vertex != sd[i].sdom->vertex) {
9832 block->idom = block->idom->idom;
9833 }
9834 idom_block(block->idom, block);
9835 }
9836 sd[1].block->idom = 0;
9837}
9838
9839static void compute_ipdom(struct compile_state *state, struct sdom_block *sd)
9840{
9841 int i;
9842 for(i = 2; i <= state->last_vertex; i++) {
9843 struct block *block;
9844 block = sd[i].block;
9845 if (block->ipdom->vertex != sd[i].sdom->vertex) {
9846 block->ipdom = block->ipdom->ipdom;
9847 }
9848 ipdom_block(block->ipdom, block);
9849 }
9850 sd[1].block->ipdom = 0;
9851}
9852
9853 /* Theorem 1:
9854 * Every vertex of a flowgraph G = (V, E, r) except r has
9855 * a unique immediate dominator.
9856 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
9857 * rooted at r, called the dominator tree of G, such that
9858 * v dominates w if and only if v is a proper ancestor of w in
9859 * the dominator tree.
9860 */
9861 /* Lemma 1:
9862 * If v and w are vertices of G such that v <= w,
9863 * than any path from v to w must contain a common ancestor
9864 * of v and w in T.
9865 */
9866 /* Lemma 2: For any vertex w != r, idom(w) -> w */
9867 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
9868 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
9869 /* Theorem 2:
9870 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
9871 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
9872 */
9873 /* Theorem 3:
9874 * Let w != r and let u be a vertex for which sdom(u) is
9875 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
9876 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
9877 */
9878 /* Lemma 5: Let vertices v,w satisfy v -> w.
9879 * Then v -> idom(w) or idom(w) -> idom(v)
9880 */
9881
9882static void find_immediate_dominators(struct compile_state *state)
9883{
9884 struct sdom_block *sd;
9885 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
9886 * vi > w for (1 <= i <= k - 1}
9887 */
9888 /* Theorem 4:
9889 * For any vertex w != r.
9890 * sdom(w) = min(
9891 * {v|(v,w) <= E and v < w } U
9892 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
9893 */
9894 /* Corollary 1:
9895 * Let w != r and let u be a vertex for which sdom(u) is
9896 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
9897 * Then:
9898 * { sdom(w) if sdom(w) = sdom(u),
9899 * idom(w) = {
9900 * { idom(u) otherwise
9901 */
9902 /* The algorithm consists of the following 4 steps.
9903 * Step 1. Carry out a depth-first search of the problem graph.
9904 * Number the vertices from 1 to N as they are reached during
9905 * the search. Initialize the variables used in succeeding steps.
9906 * Step 2. Compute the semidominators of all vertices by applying
9907 * theorem 4. Carry out the computation vertex by vertex in
9908 * decreasing order by number.
9909 * Step 3. Implicitly define the immediate dominator of each vertex
9910 * by applying Corollary 1.
9911 * Step 4. Explicitly define the immediate dominator of each vertex,
9912 * carrying out the computation vertex by vertex in increasing order
9913 * by number.
9914 */
9915 /* Step 1 initialize the basic block information */
9916 sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
9917 initialize_sdblock(sd, 0, state->first_block, 0);
9918#if 0
9919 sd[1].size = 0;
9920 sd[1].label = 0;
9921 sd[1].sdom = 0;
9922#endif
9923 /* Step 2 compute the semidominators */
9924 /* Step 3 implicitly define the immediate dominator of each vertex */
9925 compute_sdom(state, sd);
9926 /* Step 4 explicitly define the immediate dominator of each vertex */
9927 compute_idom(state, sd);
9928 xfree(sd);
9929}
9930
9931static void find_post_dominators(struct compile_state *state)
9932{
9933 struct sdom_block *sd;
9934 /* Step 1 initialize the basic block information */
9935 sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
9936
9937 initialize_sdpblock(sd, 0, state->last_block, 0);
9938
9939 /* Step 2 compute the semidominators */
9940 /* Step 3 implicitly define the immediate dominator of each vertex */
9941 compute_spdom(state, sd);
9942 /* Step 4 explicitly define the immediate dominator of each vertex */
9943 compute_ipdom(state, sd);
9944 xfree(sd);
9945}
9946
9947
9948
9949static void find_block_domf(struct compile_state *state, struct block *block)
9950{
9951 struct block *child;
9952 struct block_set *user;
9953 if (block->domfrontier != 0) {
9954 internal_error(state, block->first, "domfrontier present?");
9955 }
9956 for(user = block->idominates; user; user = user->next) {
9957 child = user->member;
9958 if (child->idom != block) {
9959 internal_error(state, block->first, "bad idom");
9960 }
9961 find_block_domf(state, child);
9962 }
9963 if (block->left && block->left->idom != block) {
9964 domf_block(block, block->left);
9965 }
9966 if (block->right && block->right->idom != block) {
9967 domf_block(block, block->right);
9968 }
9969 for(user = block->idominates; user; user = user->next) {
9970 struct block_set *frontier;
9971 child = user->member;
9972 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
9973 if (frontier->member->idom != block) {
9974 domf_block(block, frontier->member);
9975 }
9976 }
9977 }
9978}
9979
9980static void find_block_ipdomf(struct compile_state *state, struct block *block)
9981{
9982 struct block *child;
9983 struct block_set *user;
9984 if (block->ipdomfrontier != 0) {
9985 internal_error(state, block->first, "ipdomfrontier present?");
9986 }
9987 for(user = block->ipdominates; user; user = user->next) {
9988 child = user->member;
9989 if (child->ipdom != block) {
9990 internal_error(state, block->first, "bad ipdom");
9991 }
9992 find_block_ipdomf(state, child);
9993 }
9994 if (block->left && block->left->ipdom != block) {
9995 ipdomf_block(block, block->left);
9996 }
9997 if (block->right && block->right->ipdom != block) {
9998 ipdomf_block(block, block->right);
9999 }
10000 for(user = block->idominates; user; user = user->next) {
10001 struct block_set *frontier;
10002 child = user->member;
10003 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
10004 if (frontier->member->ipdom != block) {
10005 ipdomf_block(block, frontier->member);
10006 }
10007 }
10008 }
10009}
10010
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010011static void print_dominated(
10012 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010013{
10014 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010015 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010016
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010017 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010018 for(user = block->idominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010019 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010020 if (user->member->idom != block) {
10021 internal_error(state, user->member->first, "bad idom");
10022 }
10023 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010024 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010025}
10026
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010027static void print_dominators(struct compile_state *state, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010028{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010029 fprintf(fp, "\ndominates\n");
10030 walk_blocks(state, print_dominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010031}
10032
10033
10034static int print_frontiers(
10035 struct compile_state *state, struct block *block, int vertex)
10036{
10037 struct block_set *user;
10038
10039 if (!block || (block->vertex != vertex + 1)) {
10040 return vertex;
10041 }
10042 vertex += 1;
10043
10044 printf("%d:", block->vertex);
10045 for(user = block->domfrontier; user; user = user->next) {
10046 printf(" %d", user->member->vertex);
10047 }
10048 printf("\n");
10049
10050 vertex = print_frontiers(state, block->left, vertex);
10051 vertex = print_frontiers(state, block->right, vertex);
10052 return vertex;
10053}
10054static void print_dominance_frontiers(struct compile_state *state)
10055{
10056 printf("\ndominance frontiers\n");
10057 print_frontiers(state, state->first_block, 0);
10058
10059}
10060
10061static void analyze_idominators(struct compile_state *state)
10062{
10063 /* Find the immediate dominators */
10064 find_immediate_dominators(state);
10065 /* Find the dominance frontiers */
10066 find_block_domf(state, state->first_block);
10067 /* If debuging print the print what I have just found */
10068 if (state->debug & DEBUG_FDOMINATORS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010069 print_dominators(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010070 print_dominance_frontiers(state);
10071 print_control_flow(state);
10072 }
10073}
10074
10075
10076
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010077static void print_ipdominated(
10078 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010079{
10080 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010081 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010082
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010083 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010084 for(user = block->ipdominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010085 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010086 if (user->member->ipdom != block) {
10087 internal_error(state, user->member->first, "bad ipdom");
10088 }
10089 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010090 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010091}
10092
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010093static void print_ipdominators(struct compile_state *state, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010094{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010095 fprintf(fp, "\nipdominates\n");
10096 walk_blocks(state, print_ipdominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010097}
10098
10099static int print_pfrontiers(
10100 struct compile_state *state, struct block *block, int vertex)
10101{
10102 struct block_set *user;
10103
10104 if (!block || (block->vertex != vertex + 1)) {
10105 return vertex;
10106 }
10107 vertex += 1;
10108
10109 printf("%d:", block->vertex);
10110 for(user = block->ipdomfrontier; user; user = user->next) {
10111 printf(" %d", user->member->vertex);
10112 }
10113 printf("\n");
10114 for(user = block->use; user; user = user->next) {
10115 vertex = print_pfrontiers(state, user->member, vertex);
10116 }
10117 return vertex;
10118}
10119static void print_ipdominance_frontiers(struct compile_state *state)
10120{
10121 printf("\nipdominance frontiers\n");
10122 print_pfrontiers(state, state->last_block, 0);
10123
10124}
10125
10126static void analyze_ipdominators(struct compile_state *state)
10127{
10128 /* Find the post dominators */
10129 find_post_dominators(state);
10130 /* Find the control dependencies (post dominance frontiers) */
10131 find_block_ipdomf(state, state->last_block);
10132 /* If debuging print the print what I have just found */
10133 if (state->debug & DEBUG_RDOMINATORS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010134 print_ipdominators(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010135 print_ipdominance_frontiers(state);
10136 print_control_flow(state);
10137 }
10138}
10139
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010140static int bdominates(struct compile_state *state,
10141 struct block *dom, struct block *sub)
10142{
10143 while(sub && (sub != dom)) {
10144 sub = sub->idom;
10145 }
10146 return sub == dom;
10147}
10148
10149static int tdominates(struct compile_state *state,
10150 struct triple *dom, struct triple *sub)
10151{
10152 struct block *bdom, *bsub;
10153 int result;
10154 bdom = block_of_triple(state, dom);
10155 bsub = block_of_triple(state, sub);
10156 if (bdom != bsub) {
10157 result = bdominates(state, bdom, bsub);
10158 }
10159 else {
10160 struct triple *ins;
10161 ins = sub;
10162 while((ins != bsub->first) && (ins != dom)) {
10163 ins = ins->prev;
10164 }
10165 result = (ins == dom);
10166 }
10167 return result;
10168}
10169
Eric Biedermanb138ac82003-04-22 18:44:01 +000010170static void insert_phi_operations(struct compile_state *state)
10171{
10172 size_t size;
10173 struct triple *first;
10174 int *has_already, *work;
10175 struct block *work_list, **work_list_tail;
10176 int iter;
10177 struct triple *var;
10178
10179 size = sizeof(int) * (state->last_vertex + 1);
10180 has_already = xcmalloc(size, "has_already");
10181 work = xcmalloc(size, "work");
10182 iter = 0;
10183
Eric Biederman0babc1c2003-05-09 02:39:00 +000010184 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010185 for(var = first->next; var != first ; var = var->next) {
10186 struct block *block;
10187 struct triple_set *user;
10188 if ((var->op != OP_ADECL) || !var->use) {
10189 continue;
10190 }
10191 iter += 1;
10192 work_list = 0;
10193 work_list_tail = &work_list;
10194 for(user = var->use; user; user = user->next) {
10195 if (user->member->op == OP_READ) {
10196 continue;
10197 }
10198 if (user->member->op != OP_WRITE) {
10199 internal_error(state, user->member,
10200 "bad variable access");
10201 }
10202 block = user->member->u.block;
10203 if (!block) {
10204 warning(state, user->member, "dead code");
10205 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000010206 if (work[block->vertex] >= iter) {
10207 continue;
10208 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010209 work[block->vertex] = iter;
10210 *work_list_tail = block;
10211 block->work_next = 0;
10212 work_list_tail = &block->work_next;
10213 }
10214 for(block = work_list; block; block = block->work_next) {
10215 struct block_set *df;
10216 for(df = block->domfrontier; df; df = df->next) {
10217 struct triple *phi;
10218 struct block *front;
10219 int in_edges;
10220 front = df->member;
10221
10222 if (has_already[front->vertex] >= iter) {
10223 continue;
10224 }
10225 /* Count how many edges flow into this block */
10226 in_edges = front->users;
10227 /* Insert a phi function for this variable */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010228 get_occurance(front->first->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010229 phi = alloc_triple(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010230 state, OP_PHI, var->type, -1, in_edges,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010231 front->first->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010232 phi->u.block = front;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010233 MISC(phi, 0) = var;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010234 use_triple(var, phi);
10235 /* Insert the phi functions immediately after the label */
10236 insert_triple(state, front->first->next, phi);
10237 if (front->first == front->last) {
10238 front->last = front->first->next;
10239 }
10240 has_already[front->vertex] = iter;
Eric Biederman05f26fc2003-06-11 21:55:00 +000010241
Eric Biedermanb138ac82003-04-22 18:44:01 +000010242 /* If necessary plan to visit the basic block */
10243 if (work[front->vertex] >= iter) {
10244 continue;
10245 }
10246 work[front->vertex] = iter;
10247 *work_list_tail = front;
10248 front->work_next = 0;
10249 work_list_tail = &front->work_next;
10250 }
10251 }
10252 }
10253 xfree(has_already);
10254 xfree(work);
10255}
10256
10257/*
10258 * C(V)
10259 * S(V)
10260 */
10261static void fixup_block_phi_variables(
10262 struct compile_state *state, struct block *parent, struct block *block)
10263{
10264 struct block_set *set;
10265 struct triple *ptr;
10266 int edge;
10267 if (!parent || !block)
10268 return;
10269 /* Find the edge I am coming in on */
10270 edge = 0;
10271 for(set = block->use; set; set = set->next, edge++) {
10272 if (set->member == parent) {
10273 break;
10274 }
10275 }
10276 if (!set) {
10277 internal_error(state, 0, "phi input is not on a control predecessor");
10278 }
10279 for(ptr = block->first; ; ptr = ptr->next) {
10280 if (ptr->op == OP_PHI) {
10281 struct triple *var, *val, **slot;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010282 var = MISC(ptr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010283 if (!var) {
10284 internal_error(state, ptr, "no var???");
10285 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010286 /* Find the current value of the variable */
10287 val = var->use->member;
10288 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10289 internal_error(state, val, "bad value in phi");
10290 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000010291 if (edge >= TRIPLE_RHS(ptr->sizes)) {
10292 internal_error(state, ptr, "edges > phi rhs");
10293 }
10294 slot = &RHS(ptr, edge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010295 if ((*slot != 0) && (*slot != val)) {
10296 internal_error(state, ptr, "phi already bound on this edge");
10297 }
10298 *slot = val;
10299 use_triple(val, ptr);
10300 }
10301 if (ptr == block->last) {
10302 break;
10303 }
10304 }
10305}
10306
10307
10308static void rename_block_variables(
10309 struct compile_state *state, struct block *block)
10310{
10311 struct block_set *user;
10312 struct triple *ptr, *next, *last;
10313 int done;
10314 if (!block)
10315 return;
10316 last = block->first;
10317 done = 0;
10318 for(ptr = block->first; !done; ptr = next) {
10319 next = ptr->next;
10320 if (ptr == block->last) {
10321 done = 1;
10322 }
10323 /* RHS(A) */
10324 if (ptr->op == OP_READ) {
10325 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010326 var = RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010327 unuse_triple(var, ptr);
10328 if (!var->use) {
10329 error(state, ptr, "variable used without being set");
10330 }
10331 /* Find the current value of the variable */
10332 val = var->use->member;
10333 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10334 internal_error(state, val, "bad value in read");
10335 }
10336 propogate_use(state, ptr, val);
10337 release_triple(state, ptr);
10338 continue;
10339 }
10340 /* LHS(A) */
10341 if (ptr->op == OP_WRITE) {
10342 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010343 var = LHS(ptr, 0);
10344 val = RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010345 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10346 internal_error(state, val, "bad value in write");
10347 }
10348 propogate_use(state, ptr, val);
10349 unuse_triple(var, ptr);
10350 /* Push OP_WRITE ptr->right onto a stack of variable uses */
10351 push_triple(var, val);
10352 }
10353 if (ptr->op == OP_PHI) {
10354 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010355 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010356 /* Push OP_PHI onto a stack of variable uses */
10357 push_triple(var, ptr);
10358 }
10359 last = ptr;
10360 }
10361 block->last = last;
10362
10363 /* Fixup PHI functions in the cf successors */
10364 fixup_block_phi_variables(state, block, block->left);
10365 fixup_block_phi_variables(state, block, block->right);
10366 /* rename variables in the dominated nodes */
10367 for(user = block->idominates; user; user = user->next) {
10368 rename_block_variables(state, user->member);
10369 }
10370 /* pop the renamed variable stack */
10371 last = block->first;
10372 done = 0;
10373 for(ptr = block->first; !done ; ptr = next) {
10374 next = ptr->next;
10375 if (ptr == block->last) {
10376 done = 1;
10377 }
10378 if (ptr->op == OP_WRITE) {
10379 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010380 var = LHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010381 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman0babc1c2003-05-09 02:39:00 +000010382 pop_triple(var, RHS(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010383 release_triple(state, ptr);
10384 continue;
10385 }
10386 if (ptr->op == OP_PHI) {
10387 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010388 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010389 /* Pop OP_WRITE ptr->right from the stack of variable uses */
10390 pop_triple(var, ptr);
10391 }
10392 last = ptr;
10393 }
10394 block->last = last;
10395}
10396
10397static void prune_block_variables(struct compile_state *state,
10398 struct block *block)
10399{
10400 struct block_set *user;
10401 struct triple *next, *last, *ptr;
10402 int done;
10403 last = block->first;
10404 done = 0;
10405 for(ptr = block->first; !done; ptr = next) {
10406 next = ptr->next;
10407 if (ptr == block->last) {
10408 done = 1;
10409 }
10410 if (ptr->op == OP_ADECL) {
10411 struct triple_set *user, *next;
10412 for(user = ptr->use; user; user = next) {
10413 struct triple *use;
10414 next = user->next;
10415 use = user->member;
10416 if (use->op != OP_PHI) {
10417 internal_error(state, use, "decl still used");
10418 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000010419 if (MISC(use, 0) != ptr) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010420 internal_error(state, use, "bad phi use of decl");
10421 }
10422 unuse_triple(ptr, use);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010423 MISC(use, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010424 }
10425 release_triple(state, ptr);
10426 continue;
10427 }
10428 last = ptr;
10429 }
10430 block->last = last;
10431 for(user = block->idominates; user; user = user->next) {
10432 prune_block_variables(state, user->member);
10433 }
10434}
10435
10436static void transform_to_ssa_form(struct compile_state *state)
10437{
10438 insert_phi_operations(state);
10439#if 0
10440 printf("@%s:%d\n", __FILE__, __LINE__);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010441 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010442#endif
10443 rename_block_variables(state, state->first_block);
10444 prune_block_variables(state, state->first_block);
10445}
10446
10447
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010448static void clear_vertex(
10449 struct compile_state *state, struct block *block, void *arg)
10450{
10451 block->vertex = 0;
10452}
10453
10454static void mark_live_block(
10455 struct compile_state *state, struct block *block, int *next_vertex)
10456{
10457 /* See if this is a block that has not been marked */
10458 if (block->vertex != 0) {
10459 return;
10460 }
10461 block->vertex = *next_vertex;
10462 *next_vertex += 1;
10463 if (triple_is_branch(state, block->last)) {
10464 struct triple **targ;
10465 targ = triple_targ(state, block->last, 0);
10466 for(; targ; targ = triple_targ(state, block->last, targ)) {
10467 if (!*targ) {
10468 continue;
10469 }
10470 if (!triple_stores_block(state, *targ)) {
10471 internal_error(state, 0, "bad targ");
10472 }
10473 mark_live_block(state, (*targ)->u.block, next_vertex);
10474 }
10475 }
10476 else if (block->last->next != RHS(state->main_function, 0)) {
10477 struct triple *ins;
10478 ins = block->last->next;
10479 if (!triple_stores_block(state, ins)) {
10480 internal_error(state, 0, "bad block start");
10481 }
10482 mark_live_block(state, ins->u.block, next_vertex);
10483 }
10484}
10485
Eric Biedermanb138ac82003-04-22 18:44:01 +000010486static void transform_from_ssa_form(struct compile_state *state)
10487{
10488 /* To get out of ssa form we insert moves on the incoming
10489 * edges to blocks containting phi functions.
10490 */
10491 struct triple *first;
10492 struct triple *phi, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010493 int next_vertex;
10494
10495 /* Walk the control flow to see which blocks remain alive */
10496 walk_blocks(state, clear_vertex, 0);
10497 next_vertex = 1;
10498 mark_live_block(state, state->first_block, &next_vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010499
10500 /* Walk all of the operations to find the phi functions */
Eric Biederman0babc1c2003-05-09 02:39:00 +000010501 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010502 for(phi = first->next; phi != first ; phi = next) {
10503 struct block_set *set;
10504 struct block *block;
10505 struct triple **slot;
10506 struct triple *var, *read;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010507 struct triple_set *use, *use_next;
10508 int edge, used;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010509 next = phi->next;
10510 if (phi->op != OP_PHI) {
10511 continue;
10512 }
10513 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010514 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010515
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010516 /* Forget uses from code in dead blocks */
10517 for(use = phi->use; use; use = use_next) {
10518 struct block *ublock;
10519 struct triple **expr;
10520 use_next = use->next;
10521 ublock = block_of_triple(state, use->member);
10522 if ((use->member == phi) || (ublock->vertex != 0)) {
10523 continue;
10524 }
10525 expr = triple_rhs(state, use->member, 0);
10526 for(; expr; expr = triple_rhs(state, use->member, expr)) {
10527 if (*expr == phi) {
10528 *expr = 0;
10529 }
10530 }
10531 unuse_triple(phi, use->member);
10532 }
10533
Eric Biedermanb138ac82003-04-22 18:44:01 +000010534 /* A variable to replace the phi function */
10535 var = post_triple(state, phi, OP_ADECL, phi->type, 0,0);
10536 /* A read of the single value that is set into the variable */
10537 read = post_triple(state, var, OP_READ, phi->type, var, 0);
10538 use_triple(var, read);
10539
10540 /* Replaces uses of the phi with variable reads */
10541 propogate_use(state, phi, read);
10542
10543 /* Walk all of the incoming edges/blocks and insert moves.
10544 */
10545 for(edge = 0, set = block->use; set; set = set->next, edge++) {
10546 struct block *eblock;
10547 struct triple *move;
10548 struct triple *val;
10549 eblock = set->member;
10550 val = slot[edge];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010551 slot[edge] = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010552 unuse_triple(val, phi);
10553
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010554 if (!val || (val == &zero_triple) ||
10555 (block->vertex == 0) || (eblock->vertex == 0) ||
10556 (val == phi) || (val == read)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010557 continue;
10558 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010559
Eric Biedermanb138ac82003-04-22 18:44:01 +000010560 move = post_triple(state,
10561 val, OP_WRITE, phi->type, var, val);
10562 use_triple(val, move);
10563 use_triple(var, move);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010564 }
10565 /* See if there are any writers of var */
10566 used = 0;
10567 for(use = var->use; use; use = use->next) {
10568 struct triple **expr;
10569 expr = triple_lhs(state, use->member, 0);
10570 for(; expr; expr = triple_lhs(state, use->member, expr)) {
10571 if (*expr == var) {
10572 used = 1;
10573 }
10574 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010575 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010576 /* If var is not used free it */
10577 if (!used) {
10578 unuse_triple(var, read);
10579 free_triple(state, read);
10580 free_triple(state, var);
10581 }
10582
10583 /* Release the phi function */
Eric Biedermanb138ac82003-04-22 18:44:01 +000010584 release_triple(state, phi);
10585 }
10586
10587}
10588
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010589
10590/*
10591 * Register conflict resolution
10592 * =========================================================
10593 */
10594
10595static struct reg_info find_def_color(
10596 struct compile_state *state, struct triple *def)
10597{
10598 struct triple_set *set;
10599 struct reg_info info;
10600 info.reg = REG_UNSET;
10601 info.regcm = 0;
10602 if (!triple_is_def(state, def)) {
10603 return info;
10604 }
10605 info = arch_reg_lhs(state, def, 0);
10606 if (info.reg >= MAX_REGISTERS) {
10607 info.reg = REG_UNSET;
10608 }
10609 for(set = def->use; set; set = set->next) {
10610 struct reg_info tinfo;
10611 int i;
10612 i = find_rhs_use(state, set->member, def);
10613 if (i < 0) {
10614 continue;
10615 }
10616 tinfo = arch_reg_rhs(state, set->member, i);
10617 if (tinfo.reg >= MAX_REGISTERS) {
10618 tinfo.reg = REG_UNSET;
10619 }
10620 if ((tinfo.reg != REG_UNSET) &&
10621 (info.reg != REG_UNSET) &&
10622 (tinfo.reg != info.reg)) {
10623 internal_error(state, def, "register conflict");
10624 }
10625 if ((info.regcm & tinfo.regcm) == 0) {
10626 internal_error(state, def, "regcm conflict %x & %x == 0",
10627 info.regcm, tinfo.regcm);
10628 }
10629 if (info.reg == REG_UNSET) {
10630 info.reg = tinfo.reg;
10631 }
10632 info.regcm &= tinfo.regcm;
10633 }
10634 if (info.reg >= MAX_REGISTERS) {
10635 internal_error(state, def, "register out of range");
10636 }
10637 return info;
10638}
10639
10640static struct reg_info find_lhs_pre_color(
10641 struct compile_state *state, struct triple *ins, int index)
10642{
10643 struct reg_info info;
10644 int zlhs, zrhs, i;
10645 zrhs = TRIPLE_RHS(ins->sizes);
10646 zlhs = TRIPLE_LHS(ins->sizes);
10647 if (!zlhs && triple_is_def(state, ins)) {
10648 zlhs = 1;
10649 }
10650 if (index >= zlhs) {
10651 internal_error(state, ins, "Bad lhs %d", index);
10652 }
10653 info = arch_reg_lhs(state, ins, index);
10654 for(i = 0; i < zrhs; i++) {
10655 struct reg_info rinfo;
10656 rinfo = arch_reg_rhs(state, ins, i);
10657 if ((info.reg == rinfo.reg) &&
10658 (rinfo.reg >= MAX_REGISTERS)) {
10659 struct reg_info tinfo;
10660 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
10661 info.reg = tinfo.reg;
10662 info.regcm &= tinfo.regcm;
10663 break;
10664 }
10665 }
10666 if (info.reg >= MAX_REGISTERS) {
10667 info.reg = REG_UNSET;
10668 }
10669 return info;
10670}
10671
10672static struct reg_info find_rhs_post_color(
10673 struct compile_state *state, struct triple *ins, int index);
10674
10675static struct reg_info find_lhs_post_color(
10676 struct compile_state *state, struct triple *ins, int index)
10677{
10678 struct triple_set *set;
10679 struct reg_info info;
10680 struct triple *lhs;
10681#if 0
10682 fprintf(stderr, "find_lhs_post_color(%p, %d)\n",
10683 ins, index);
10684#endif
10685 if ((index == 0) && triple_is_def(state, ins)) {
10686 lhs = ins;
10687 }
10688 else if (index < TRIPLE_LHS(ins->sizes)) {
10689 lhs = LHS(ins, index);
10690 }
10691 else {
10692 internal_error(state, ins, "Bad lhs %d", index);
10693 lhs = 0;
10694 }
10695 info = arch_reg_lhs(state, ins, index);
10696 if (info.reg >= MAX_REGISTERS) {
10697 info.reg = REG_UNSET;
10698 }
10699 for(set = lhs->use; set; set = set->next) {
10700 struct reg_info rinfo;
10701 struct triple *user;
10702 int zrhs, i;
10703 user = set->member;
10704 zrhs = TRIPLE_RHS(user->sizes);
10705 for(i = 0; i < zrhs; i++) {
10706 if (RHS(user, i) != lhs) {
10707 continue;
10708 }
10709 rinfo = find_rhs_post_color(state, user, i);
10710 if ((info.reg != REG_UNSET) &&
10711 (rinfo.reg != REG_UNSET) &&
10712 (info.reg != rinfo.reg)) {
10713 internal_error(state, ins, "register conflict");
10714 }
10715 if ((info.regcm & rinfo.regcm) == 0) {
10716 internal_error(state, ins, "regcm conflict %x & %x == 0",
10717 info.regcm, rinfo.regcm);
10718 }
10719 if (info.reg == REG_UNSET) {
10720 info.reg = rinfo.reg;
10721 }
10722 info.regcm &= rinfo.regcm;
10723 }
10724 }
10725#if 0
10726 fprintf(stderr, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
10727 ins, index, info.reg, info.regcm);
10728#endif
10729 return info;
10730}
10731
10732static struct reg_info find_rhs_post_color(
10733 struct compile_state *state, struct triple *ins, int index)
10734{
10735 struct reg_info info, rinfo;
10736 int zlhs, i;
10737#if 0
10738 fprintf(stderr, "find_rhs_post_color(%p, %d)\n",
10739 ins, index);
10740#endif
10741 rinfo = arch_reg_rhs(state, ins, index);
10742 zlhs = TRIPLE_LHS(ins->sizes);
10743 if (!zlhs && triple_is_def(state, ins)) {
10744 zlhs = 1;
10745 }
10746 info = rinfo;
10747 if (info.reg >= MAX_REGISTERS) {
10748 info.reg = REG_UNSET;
10749 }
10750 for(i = 0; i < zlhs; i++) {
10751 struct reg_info linfo;
10752 linfo = arch_reg_lhs(state, ins, i);
10753 if ((linfo.reg == rinfo.reg) &&
10754 (linfo.reg >= MAX_REGISTERS)) {
10755 struct reg_info tinfo;
10756 tinfo = find_lhs_post_color(state, ins, i);
10757 if (tinfo.reg >= MAX_REGISTERS) {
10758 tinfo.reg = REG_UNSET;
10759 }
10760 info.regcm &= linfo.reg;
10761 info.regcm &= tinfo.regcm;
10762 if (info.reg != REG_UNSET) {
10763 internal_error(state, ins, "register conflict");
10764 }
10765 if (info.regcm == 0) {
10766 internal_error(state, ins, "regcm conflict");
10767 }
10768 info.reg = tinfo.reg;
10769 }
10770 }
10771#if 0
10772 fprintf(stderr, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
10773 ins, index, info.reg, info.regcm);
10774#endif
10775 return info;
10776}
10777
10778static struct reg_info find_lhs_color(
10779 struct compile_state *state, struct triple *ins, int index)
10780{
10781 struct reg_info pre, post, info;
10782#if 0
10783 fprintf(stderr, "find_lhs_color(%p, %d)\n",
10784 ins, index);
10785#endif
10786 pre = find_lhs_pre_color(state, ins, index);
10787 post = find_lhs_post_color(state, ins, index);
10788 if ((pre.reg != post.reg) &&
10789 (pre.reg != REG_UNSET) &&
10790 (post.reg != REG_UNSET)) {
10791 internal_error(state, ins, "register conflict");
10792 }
10793 info.regcm = pre.regcm & post.regcm;
10794 info.reg = pre.reg;
10795 if (info.reg == REG_UNSET) {
10796 info.reg = post.reg;
10797 }
10798#if 0
10799 fprintf(stderr, "find_lhs_color(%p, %d) -> ( %d, %x)\n",
10800 ins, index, info.reg, info.regcm);
10801#endif
10802 return info;
10803}
10804
10805static struct triple *post_copy(struct compile_state *state, struct triple *ins)
10806{
10807 struct triple_set *entry, *next;
10808 struct triple *out;
10809 struct reg_info info, rinfo;
10810
10811 info = arch_reg_lhs(state, ins, 0);
10812 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
10813 use_triple(RHS(out, 0), out);
10814 /* Get the users of ins to use out instead */
10815 for(entry = ins->use; entry; entry = next) {
10816 int i;
10817 next = entry->next;
10818 if (entry->member == out) {
10819 continue;
10820 }
10821 i = find_rhs_use(state, entry->member, ins);
10822 if (i < 0) {
10823 continue;
10824 }
10825 rinfo = arch_reg_rhs(state, entry->member, i);
10826 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
10827 continue;
10828 }
10829 replace_rhs_use(state, ins, out, entry->member);
10830 }
10831 transform_to_arch_instruction(state, out);
10832 return out;
10833}
10834
10835static struct triple *pre_copy(
10836 struct compile_state *state, struct triple *ins, int index)
10837{
10838 /* Carefully insert enough operations so that I can
10839 * enter any operation with a GPR32.
10840 */
10841 struct triple *in;
10842 struct triple **expr;
Eric Biederman153ea352003-06-20 14:43:20 +000010843 if (ins->op == OP_PHI) {
10844 internal_error(state, ins, "pre_copy on a phi?");
10845 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010846 expr = &RHS(ins, index);
10847 in = pre_triple(state, ins, OP_COPY, (*expr)->type, *expr, 0);
10848 unuse_triple(*expr, ins);
10849 *expr = in;
10850 use_triple(RHS(in, 0), in);
10851 use_triple(in, ins);
10852 transform_to_arch_instruction(state, in);
10853 return in;
10854}
10855
10856
Eric Biedermanb138ac82003-04-22 18:44:01 +000010857static void insert_copies_to_phi(struct compile_state *state)
10858{
10859 /* To get out of ssa form we insert moves on the incoming
10860 * edges to blocks containting phi functions.
10861 */
10862 struct triple *first;
10863 struct triple *phi;
10864
10865 /* Walk all of the operations to find the phi functions */
Eric Biederman0babc1c2003-05-09 02:39:00 +000010866 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010867 for(phi = first->next; phi != first ; phi = phi->next) {
10868 struct block_set *set;
10869 struct block *block;
10870 struct triple **slot;
10871 int edge;
10872 if (phi->op != OP_PHI) {
10873 continue;
10874 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010875 phi->id |= TRIPLE_FLAG_POST_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010876 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010877 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010878 /* Walk all of the incoming edges/blocks and insert moves.
10879 */
10880 for(edge = 0, set = block->use; set; set = set->next, edge++) {
10881 struct block *eblock;
10882 struct triple *move;
10883 struct triple *val;
10884 struct triple *ptr;
10885 eblock = set->member;
10886 val = slot[edge];
10887
10888 if (val == phi) {
10889 continue;
10890 }
10891
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010892 get_occurance(val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010893 move = build_triple(state, OP_COPY, phi->type, val, 0,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010894 val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010895 move->u.block = eblock;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010896 move->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010897 use_triple(val, move);
10898
10899 slot[edge] = move;
10900 unuse_triple(val, phi);
10901 use_triple(move, phi);
10902
10903 /* Walk through the block backwards to find
10904 * an appropriate location for the OP_COPY.
10905 */
10906 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
10907 struct triple **expr;
10908 if ((ptr == phi) || (ptr == val)) {
10909 goto out;
10910 }
10911 expr = triple_rhs(state, ptr, 0);
10912 for(;expr; expr = triple_rhs(state, ptr, expr)) {
10913 if ((*expr) == phi) {
10914 goto out;
10915 }
10916 }
10917 }
10918 out:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010919 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010920 internal_error(state, ptr,
10921 "Could not insert write to phi");
10922 }
10923 insert_triple(state, ptr->next, move);
10924 if (eblock->last == ptr) {
10925 eblock->last = move;
10926 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010927 transform_to_arch_instruction(state, move);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010928 }
10929 }
10930}
10931
10932struct triple_reg_set {
10933 struct triple_reg_set *next;
10934 struct triple *member;
10935 struct triple *new;
10936};
10937
10938struct reg_block {
10939 struct block *block;
10940 struct triple_reg_set *in;
10941 struct triple_reg_set *out;
10942 int vertex;
10943};
10944
10945static int do_triple_set(struct triple_reg_set **head,
10946 struct triple *member, struct triple *new_member)
10947{
10948 struct triple_reg_set **ptr, *new;
10949 if (!member)
10950 return 0;
10951 ptr = head;
10952 while(*ptr) {
10953 if ((*ptr)->member == member) {
10954 return 0;
10955 }
10956 ptr = &(*ptr)->next;
10957 }
10958 new = xcmalloc(sizeof(*new), "triple_set");
10959 new->member = member;
10960 new->new = new_member;
10961 new->next = *head;
10962 *head = new;
10963 return 1;
10964}
10965
10966static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
10967{
10968 struct triple_reg_set *entry, **ptr;
10969 ptr = head;
10970 while(*ptr) {
10971 entry = *ptr;
10972 if (entry->member == member) {
10973 *ptr = entry->next;
10974 xfree(entry);
10975 return;
10976 }
10977 else {
10978 ptr = &entry->next;
10979 }
10980 }
10981}
10982
10983static int in_triple(struct reg_block *rb, struct triple *in)
10984{
10985 return do_triple_set(&rb->in, in, 0);
10986}
10987static void unin_triple(struct reg_block *rb, struct triple *unin)
10988{
10989 do_triple_unset(&rb->in, unin);
10990}
10991
10992static int out_triple(struct reg_block *rb, struct triple *out)
10993{
10994 return do_triple_set(&rb->out, out, 0);
10995}
10996static void unout_triple(struct reg_block *rb, struct triple *unout)
10997{
10998 do_triple_unset(&rb->out, unout);
10999}
11000
11001static int initialize_regblock(struct reg_block *blocks,
11002 struct block *block, int vertex)
11003{
11004 struct block_set *user;
11005 if (!block || (blocks[block->vertex].block == block)) {
11006 return vertex;
11007 }
11008 vertex += 1;
11009 /* Renumber the blocks in a convinient fashion */
11010 block->vertex = vertex;
11011 blocks[vertex].block = block;
11012 blocks[vertex].vertex = vertex;
11013 for(user = block->use; user; user = user->next) {
11014 vertex = initialize_regblock(blocks, user->member, vertex);
11015 }
11016 return vertex;
11017}
11018
11019static int phi_in(struct compile_state *state, struct reg_block *blocks,
11020 struct reg_block *rb, struct block *suc)
11021{
11022 /* Read the conditional input set of a successor block
11023 * (i.e. the input to the phi nodes) and place it in the
11024 * current blocks output set.
11025 */
11026 struct block_set *set;
11027 struct triple *ptr;
11028 int edge;
11029 int done, change;
11030 change = 0;
11031 /* Find the edge I am coming in on */
11032 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
11033 if (set->member == rb->block) {
11034 break;
11035 }
11036 }
11037 if (!set) {
11038 internal_error(state, 0, "Not coming on a control edge?");
11039 }
11040 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
11041 struct triple **slot, *expr, *ptr2;
11042 int out_change, done2;
11043 done = (ptr == suc->last);
11044 if (ptr->op != OP_PHI) {
11045 continue;
11046 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000011047 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011048 expr = slot[edge];
11049 out_change = out_triple(rb, expr);
11050 if (!out_change) {
11051 continue;
11052 }
11053 /* If we don't define the variable also plast it
11054 * in the current blocks input set.
11055 */
11056 ptr2 = rb->block->first;
11057 for(done2 = 0; !done2; ptr2 = ptr2->next) {
11058 if (ptr2 == expr) {
11059 break;
11060 }
11061 done2 = (ptr2 == rb->block->last);
11062 }
11063 if (!done2) {
11064 continue;
11065 }
11066 change |= in_triple(rb, expr);
11067 }
11068 return change;
11069}
11070
11071static int reg_in(struct compile_state *state, struct reg_block *blocks,
11072 struct reg_block *rb, struct block *suc)
11073{
11074 struct triple_reg_set *in_set;
11075 int change;
11076 change = 0;
11077 /* Read the input set of a successor block
11078 * and place it in the current blocks output set.
11079 */
11080 in_set = blocks[suc->vertex].in;
11081 for(; in_set; in_set = in_set->next) {
11082 int out_change, done;
11083 struct triple *first, *last, *ptr;
11084 out_change = out_triple(rb, in_set->member);
11085 if (!out_change) {
11086 continue;
11087 }
11088 /* If we don't define the variable also place it
11089 * in the current blocks input set.
11090 */
11091 first = rb->block->first;
11092 last = rb->block->last;
11093 done = 0;
11094 for(ptr = first; !done; ptr = ptr->next) {
11095 if (ptr == in_set->member) {
11096 break;
11097 }
11098 done = (ptr == last);
11099 }
11100 if (!done) {
11101 continue;
11102 }
11103 change |= in_triple(rb, in_set->member);
11104 }
11105 change |= phi_in(state, blocks, rb, suc);
11106 return change;
11107}
11108
11109
11110static int use_in(struct compile_state *state, struct reg_block *rb)
11111{
11112 /* Find the variables we use but don't define and add
11113 * it to the current blocks input set.
11114 */
11115#warning "FIXME is this O(N^2) algorithm bad?"
11116 struct block *block;
11117 struct triple *ptr;
11118 int done;
11119 int change;
11120 block = rb->block;
11121 change = 0;
11122 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
11123 struct triple **expr;
11124 done = (ptr == block->first);
11125 /* The variable a phi function uses depends on the
11126 * control flow, and is handled in phi_in, not
11127 * here.
11128 */
11129 if (ptr->op == OP_PHI) {
11130 continue;
11131 }
11132 expr = triple_rhs(state, ptr, 0);
11133 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11134 struct triple *rhs, *test;
11135 int tdone;
11136 rhs = *expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011137 if (!rhs) {
11138 continue;
11139 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011140 /* See if rhs is defined in this block */
11141 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
11142 tdone = (test == block->first);
11143 if (test == rhs) {
11144 rhs = 0;
11145 break;
11146 }
11147 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011148 /* If I still have a valid rhs add it to in */
11149 change |= in_triple(rb, rhs);
11150 }
11151 }
11152 return change;
11153}
11154
11155static struct reg_block *compute_variable_lifetimes(
11156 struct compile_state *state)
11157{
11158 struct reg_block *blocks;
11159 int change;
11160 blocks = xcmalloc(
11161 sizeof(*blocks)*(state->last_vertex + 1), "reg_block");
11162 initialize_regblock(blocks, state->last_block, 0);
11163 do {
11164 int i;
11165 change = 0;
11166 for(i = 1; i <= state->last_vertex; i++) {
11167 struct reg_block *rb;
11168 rb = &blocks[i];
11169 /* Add the left successor's input set to in */
11170 if (rb->block->left) {
11171 change |= reg_in(state, blocks, rb, rb->block->left);
11172 }
11173 /* Add the right successor's input set to in */
11174 if ((rb->block->right) &&
11175 (rb->block->right != rb->block->left)) {
11176 change |= reg_in(state, blocks, rb, rb->block->right);
11177 }
11178 /* Add use to in... */
11179 change |= use_in(state, rb);
11180 }
11181 } while(change);
11182 return blocks;
11183}
11184
11185static void free_variable_lifetimes(
11186 struct compile_state *state, struct reg_block *blocks)
11187{
11188 int i;
11189 /* free in_set && out_set on each block */
11190 for(i = 1; i <= state->last_vertex; i++) {
11191 struct triple_reg_set *entry, *next;
11192 struct reg_block *rb;
11193 rb = &blocks[i];
11194 for(entry = rb->in; entry ; entry = next) {
11195 next = entry->next;
11196 do_triple_unset(&rb->in, entry->member);
11197 }
11198 for(entry = rb->out; entry; entry = next) {
11199 next = entry->next;
11200 do_triple_unset(&rb->out, entry->member);
11201 }
11202 }
11203 xfree(blocks);
11204
11205}
11206
Eric Biedermanf96a8102003-06-16 16:57:34 +000011207typedef void (*wvl_cb_t)(
Eric Biedermanb138ac82003-04-22 18:44:01 +000011208 struct compile_state *state,
11209 struct reg_block *blocks, struct triple_reg_set *live,
11210 struct reg_block *rb, struct triple *ins, void *arg);
11211
11212static void walk_variable_lifetimes(struct compile_state *state,
11213 struct reg_block *blocks, wvl_cb_t cb, void *arg)
11214{
11215 int i;
11216
11217 for(i = 1; i <= state->last_vertex; i++) {
11218 struct triple_reg_set *live;
11219 struct triple_reg_set *entry, *next;
11220 struct triple *ptr, *prev;
11221 struct reg_block *rb;
11222 struct block *block;
11223 int done;
11224
11225 /* Get the blocks */
11226 rb = &blocks[i];
11227 block = rb->block;
11228
11229 /* Copy out into live */
11230 live = 0;
11231 for(entry = rb->out; entry; entry = next) {
11232 next = entry->next;
11233 do_triple_set(&live, entry->member, entry->new);
11234 }
11235 /* Walk through the basic block calculating live */
11236 for(done = 0, ptr = block->last; !done; ptr = prev) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000011237 struct triple **expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011238
11239 prev = ptr->prev;
11240 done = (ptr == block->first);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011241
11242 /* Ensure the current definition is in live */
11243 if (triple_is_def(state, ptr)) {
11244 do_triple_set(&live, ptr, 0);
11245 }
11246
11247 /* Inform the callback function of what is
11248 * going on.
11249 */
Eric Biedermanf96a8102003-06-16 16:57:34 +000011250 cb(state, blocks, live, rb, ptr, arg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011251
11252 /* Remove the current definition from live */
11253 do_triple_unset(&live, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011254
Eric Biedermanb138ac82003-04-22 18:44:01 +000011255 /* Add the current uses to live.
11256 *
11257 * It is safe to skip phi functions because they do
11258 * not have any block local uses, and the block
11259 * output sets already properly account for what
11260 * control flow depedent uses phi functions do have.
11261 */
11262 if (ptr->op == OP_PHI) {
11263 continue;
11264 }
11265 expr = triple_rhs(state, ptr, 0);
11266 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11267 /* If the triple is not a definition skip it. */
Eric Biederman0babc1c2003-05-09 02:39:00 +000011268 if (!*expr || !triple_is_def(state, *expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011269 continue;
11270 }
11271 do_triple_set(&live, *expr, 0);
11272 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011273 }
11274 /* Free live */
11275 for(entry = live; entry; entry = next) {
11276 next = entry->next;
11277 do_triple_unset(&live, entry->member);
11278 }
11279 }
11280}
11281
11282static int count_triples(struct compile_state *state)
11283{
11284 struct triple *first, *ins;
11285 int triples = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011286 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011287 ins = first;
11288 do {
11289 triples++;
11290 ins = ins->next;
11291 } while (ins != first);
11292 return triples;
11293}
11294struct dead_triple {
11295 struct triple *triple;
11296 struct dead_triple *work_next;
11297 struct block *block;
11298 int color;
11299 int flags;
11300#define TRIPLE_FLAG_ALIVE 1
11301};
11302
11303
11304static void awaken(
11305 struct compile_state *state,
11306 struct dead_triple *dtriple, struct triple **expr,
11307 struct dead_triple ***work_list_tail)
11308{
11309 struct triple *triple;
11310 struct dead_triple *dt;
11311 if (!expr) {
11312 return;
11313 }
11314 triple = *expr;
11315 if (!triple) {
11316 return;
11317 }
11318 if (triple->id <= 0) {
11319 internal_error(state, triple, "bad triple id: %d",
11320 triple->id);
11321 }
11322 if (triple->op == OP_NOOP) {
11323 internal_warning(state, triple, "awakening noop?");
11324 return;
11325 }
11326 dt = &dtriple[triple->id];
11327 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11328 dt->flags |= TRIPLE_FLAG_ALIVE;
11329 if (!dt->work_next) {
11330 **work_list_tail = dt;
11331 *work_list_tail = &dt->work_next;
11332 }
11333 }
11334}
11335
11336static void eliminate_inefectual_code(struct compile_state *state)
11337{
11338 struct block *block;
11339 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
11340 int triples, i;
11341 struct triple *first, *ins;
11342
11343 /* Setup the work list */
11344 work_list = 0;
11345 work_list_tail = &work_list;
11346
Eric Biederman0babc1c2003-05-09 02:39:00 +000011347 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011348
11349 /* Count how many triples I have */
11350 triples = count_triples(state);
11351
11352 /* Now put then in an array and mark all of the triples dead */
11353 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
11354
11355 ins = first;
11356 i = 1;
11357 block = 0;
11358 do {
11359 if (ins->op == OP_LABEL) {
11360 block = ins->u.block;
11361 }
11362 dtriple[i].triple = ins;
11363 dtriple[i].block = block;
11364 dtriple[i].flags = 0;
11365 dtriple[i].color = ins->id;
11366 ins->id = i;
11367 /* See if it is an operation we always keep */
11368#warning "FIXME handle the case of killing a branch instruction"
Eric Biederman0babc1c2003-05-09 02:39:00 +000011369 if (!triple_is_pure(state, ins) || triple_is_branch(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011370 awaken(state, dtriple, &ins, &work_list_tail);
11371 }
11372 i++;
11373 ins = ins->next;
11374 } while(ins != first);
11375 while(work_list) {
11376 struct dead_triple *dt;
11377 struct block_set *user;
11378 struct triple **expr;
11379 dt = work_list;
11380 work_list = dt->work_next;
11381 if (!work_list) {
11382 work_list_tail = &work_list;
11383 }
11384 /* Wake up the data depencencies of this triple */
11385 expr = 0;
11386 do {
11387 expr = triple_rhs(state, dt->triple, expr);
11388 awaken(state, dtriple, expr, &work_list_tail);
11389 } while(expr);
11390 do {
11391 expr = triple_lhs(state, dt->triple, expr);
11392 awaken(state, dtriple, expr, &work_list_tail);
11393 } while(expr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011394 do {
11395 expr = triple_misc(state, dt->triple, expr);
11396 awaken(state, dtriple, expr, &work_list_tail);
11397 } while(expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011398 /* Wake up the forward control dependencies */
11399 do {
11400 expr = triple_targ(state, dt->triple, expr);
11401 awaken(state, dtriple, expr, &work_list_tail);
11402 } while(expr);
11403 /* Wake up the reverse control dependencies of this triple */
11404 for(user = dt->block->ipdomfrontier; user; user = user->next) {
11405 awaken(state, dtriple, &user->member->last, &work_list_tail);
11406 }
11407 }
11408 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
11409 if ((dt->triple->op == OP_NOOP) &&
11410 (dt->flags & TRIPLE_FLAG_ALIVE)) {
11411 internal_error(state, dt->triple, "noop effective?");
11412 }
11413 dt->triple->id = dt->color; /* Restore the color */
11414 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11415#warning "FIXME handle the case of killing a basic block"
11416 if (dt->block->first == dt->triple) {
11417 continue;
11418 }
11419 if (dt->block->last == dt->triple) {
11420 dt->block->last = dt->triple->prev;
11421 }
11422 release_triple(state, dt->triple);
11423 }
11424 }
11425 xfree(dtriple);
11426}
11427
11428
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011429static void insert_mandatory_copies(struct compile_state *state)
11430{
11431 struct triple *ins, *first;
11432
11433 /* The object is with a minimum of inserted copies,
11434 * to resolve in fundamental register conflicts between
11435 * register value producers and consumers.
11436 * Theoretically we may be greater than minimal when we
11437 * are inserting copies before instructions but that
11438 * case should be rare.
11439 */
11440 first = RHS(state->main_function, 0);
11441 ins = first;
11442 do {
11443 struct triple_set *entry, *next;
11444 struct triple *tmp;
11445 struct reg_info info;
11446 unsigned reg, regcm;
11447 int do_post_copy, do_pre_copy;
11448 tmp = 0;
11449 if (!triple_is_def(state, ins)) {
11450 goto next;
11451 }
11452 /* Find the architecture specific color information */
11453 info = arch_reg_lhs(state, ins, 0);
11454 if (info.reg >= MAX_REGISTERS) {
11455 info.reg = REG_UNSET;
11456 }
11457
11458 reg = REG_UNSET;
11459 regcm = arch_type_to_regcm(state, ins->type);
11460 do_post_copy = do_pre_copy = 0;
11461
11462 /* Walk through the uses of ins and check for conflicts */
11463 for(entry = ins->use; entry; entry = next) {
11464 struct reg_info rinfo;
11465 int i;
11466 next = entry->next;
11467 i = find_rhs_use(state, entry->member, ins);
11468 if (i < 0) {
11469 continue;
11470 }
11471
11472 /* Find the users color requirements */
11473 rinfo = arch_reg_rhs(state, entry->member, i);
11474 if (rinfo.reg >= MAX_REGISTERS) {
11475 rinfo.reg = REG_UNSET;
11476 }
11477
11478 /* See if I need a pre_copy */
11479 if (rinfo.reg != REG_UNSET) {
11480 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
11481 do_pre_copy = 1;
11482 }
11483 reg = rinfo.reg;
11484 }
11485 regcm &= rinfo.regcm;
11486 regcm = arch_regcm_normalize(state, regcm);
11487 if (regcm == 0) {
11488 do_pre_copy = 1;
11489 }
11490 }
11491 do_post_copy =
11492 !do_pre_copy &&
11493 (((info.reg != REG_UNSET) &&
11494 (reg != REG_UNSET) &&
11495 (info.reg != reg)) ||
11496 ((info.regcm & regcm) == 0));
11497
11498 reg = info.reg;
11499 regcm = info.regcm;
11500 /* Walk through the uses of insert and do a pre_copy or see if a post_copy is warranted */
11501 for(entry = ins->use; entry; entry = next) {
11502 struct reg_info rinfo;
11503 int i;
11504 next = entry->next;
11505 i = find_rhs_use(state, entry->member, ins);
11506 if (i < 0) {
11507 continue;
11508 }
11509
11510 /* Find the users color requirements */
11511 rinfo = arch_reg_rhs(state, entry->member, i);
11512 if (rinfo.reg >= MAX_REGISTERS) {
11513 rinfo.reg = REG_UNSET;
11514 }
11515
11516 /* Now see if it is time to do the pre_copy */
11517 if (rinfo.reg != REG_UNSET) {
11518 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
11519 ((regcm & rinfo.regcm) == 0) ||
11520 /* Don't let a mandatory coalesce sneak
11521 * into a operation that is marked to prevent
11522 * coalescing.
11523 */
11524 ((reg != REG_UNNEEDED) &&
11525 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
11526 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
11527 ) {
11528 if (do_pre_copy) {
11529 struct triple *user;
11530 user = entry->member;
11531 if (RHS(user, i) != ins) {
11532 internal_error(state, user, "bad rhs");
11533 }
11534 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011535 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011536 continue;
11537 } else {
11538 do_post_copy = 1;
11539 }
11540 }
11541 reg = rinfo.reg;
11542 }
11543 if ((regcm & rinfo.regcm) == 0) {
11544 if (do_pre_copy) {
11545 struct triple *user;
11546 user = entry->member;
11547 if (RHS(user, i) != ins) {
11548 internal_error(state, user, "bad rhs");
11549 }
11550 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011551 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011552 continue;
11553 } else {
11554 do_post_copy = 1;
11555 }
11556 }
11557 regcm &= rinfo.regcm;
11558
11559 }
11560 if (do_post_copy) {
11561 struct reg_info pre, post;
11562 tmp = post_copy(state, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011563 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011564 pre = arch_reg_lhs(state, ins, 0);
11565 post = arch_reg_lhs(state, tmp, 0);
11566 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
11567 internal_error(state, tmp, "useless copy");
11568 }
11569 }
11570 next:
11571 ins = ins->next;
11572 } while(ins != first);
11573}
11574
11575
Eric Biedermanb138ac82003-04-22 18:44:01 +000011576struct live_range_edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011577struct live_range_def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011578struct live_range {
11579 struct live_range_edge *edges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011580 struct live_range_def *defs;
11581/* Note. The list pointed to by defs is kept in order.
11582 * That is baring splits in the flow control
11583 * defs dominates defs->next wich dominates defs->next->next
11584 * etc.
11585 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011586 unsigned color;
11587 unsigned classes;
11588 unsigned degree;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011589 unsigned length;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011590 struct live_range *group_next, **group_prev;
11591};
11592
11593struct live_range_edge {
11594 struct live_range_edge *next;
11595 struct live_range *node;
11596};
11597
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011598struct live_range_def {
11599 struct live_range_def *next;
11600 struct live_range_def *prev;
11601 struct live_range *lr;
11602 struct triple *def;
11603 unsigned orig_id;
11604};
11605
Eric Biedermanb138ac82003-04-22 18:44:01 +000011606#define LRE_HASH_SIZE 2048
11607struct lre_hash {
11608 struct lre_hash *next;
11609 struct live_range *left;
11610 struct live_range *right;
11611};
11612
11613
11614struct reg_state {
11615 struct lre_hash *hash[LRE_HASH_SIZE];
11616 struct reg_block *blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011617 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011618 struct live_range *lr;
11619 struct live_range *low, **low_tail;
11620 struct live_range *high, **high_tail;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011621 unsigned defs;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011622 unsigned ranges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011623 int passes, max_passes;
11624#define MAX_ALLOCATION_PASSES 100
Eric Biedermanb138ac82003-04-22 18:44:01 +000011625};
11626
11627
11628static unsigned regc_max_size(struct compile_state *state, int classes)
11629{
11630 unsigned max_size;
11631 int i;
11632 max_size = 0;
11633 for(i = 0; i < MAX_REGC; i++) {
11634 if (classes & (1 << i)) {
11635 unsigned size;
11636 size = arch_regc_size(state, i);
11637 if (size > max_size) {
11638 max_size = size;
11639 }
11640 }
11641 }
11642 return max_size;
11643}
11644
11645static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
11646{
11647 unsigned equivs[MAX_REG_EQUIVS];
11648 int i;
11649 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
11650 internal_error(state, 0, "invalid register");
11651 }
11652 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
11653 internal_error(state, 0, "invalid register");
11654 }
11655 arch_reg_equivs(state, equivs, reg1);
11656 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11657 if (equivs[i] == reg2) {
11658 return 1;
11659 }
11660 }
11661 return 0;
11662}
11663
11664static void reg_fill_used(struct compile_state *state, char *used, int reg)
11665{
11666 unsigned equivs[MAX_REG_EQUIVS];
11667 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011668 if (reg == REG_UNNEEDED) {
11669 return;
11670 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011671 arch_reg_equivs(state, equivs, reg);
11672 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11673 used[equivs[i]] = 1;
11674 }
11675 return;
11676}
11677
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011678static void reg_inc_used(struct compile_state *state, char *used, int reg)
11679{
11680 unsigned equivs[MAX_REG_EQUIVS];
11681 int i;
11682 if (reg == REG_UNNEEDED) {
11683 return;
11684 }
11685 arch_reg_equivs(state, equivs, reg);
11686 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11687 used[equivs[i]] += 1;
11688 }
11689 return;
11690}
11691
Eric Biedermanb138ac82003-04-22 18:44:01 +000011692static unsigned int hash_live_edge(
11693 struct live_range *left, struct live_range *right)
11694{
11695 unsigned int hash, val;
11696 unsigned long lval, rval;
11697 lval = ((unsigned long)left)/sizeof(struct live_range);
11698 rval = ((unsigned long)right)/sizeof(struct live_range);
11699 hash = 0;
11700 while(lval) {
11701 val = lval & 0xff;
11702 lval >>= 8;
11703 hash = (hash *263) + val;
11704 }
11705 while(rval) {
11706 val = rval & 0xff;
11707 rval >>= 8;
11708 hash = (hash *263) + val;
11709 }
11710 hash = hash & (LRE_HASH_SIZE - 1);
11711 return hash;
11712}
11713
11714static struct lre_hash **lre_probe(struct reg_state *rstate,
11715 struct live_range *left, struct live_range *right)
11716{
11717 struct lre_hash **ptr;
11718 unsigned int index;
11719 /* Ensure left <= right */
11720 if (left > right) {
11721 struct live_range *tmp;
11722 tmp = left;
11723 left = right;
11724 right = tmp;
11725 }
11726 index = hash_live_edge(left, right);
11727
11728 ptr = &rstate->hash[index];
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011729 while(*ptr) {
11730 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
11731 break;
11732 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011733 ptr = &(*ptr)->next;
11734 }
11735 return ptr;
11736}
11737
11738static int interfere(struct reg_state *rstate,
11739 struct live_range *left, struct live_range *right)
11740{
11741 struct lre_hash **ptr;
11742 ptr = lre_probe(rstate, left, right);
11743 return ptr && *ptr;
11744}
11745
11746static void add_live_edge(struct reg_state *rstate,
11747 struct live_range *left, struct live_range *right)
11748{
11749 /* FIXME the memory allocation overhead is noticeable here... */
11750 struct lre_hash **ptr, *new_hash;
11751 struct live_range_edge *edge;
11752
11753 if (left == right) {
11754 return;
11755 }
11756 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
11757 return;
11758 }
11759 /* Ensure left <= right */
11760 if (left > right) {
11761 struct live_range *tmp;
11762 tmp = left;
11763 left = right;
11764 right = tmp;
11765 }
11766 ptr = lre_probe(rstate, left, right);
11767 if (*ptr) {
11768 return;
11769 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011770#if 0
11771 fprintf(stderr, "new_live_edge(%p, %p)\n",
11772 left, right);
11773#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011774 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
11775 new_hash->next = *ptr;
11776 new_hash->left = left;
11777 new_hash->right = right;
11778 *ptr = new_hash;
11779
11780 edge = xmalloc(sizeof(*edge), "live_range_edge");
11781 edge->next = left->edges;
11782 edge->node = right;
11783 left->edges = edge;
11784 left->degree += 1;
11785
11786 edge = xmalloc(sizeof(*edge), "live_range_edge");
11787 edge->next = right->edges;
11788 edge->node = left;
11789 right->edges = edge;
11790 right->degree += 1;
11791}
11792
11793static void remove_live_edge(struct reg_state *rstate,
11794 struct live_range *left, struct live_range *right)
11795{
11796 struct live_range_edge *edge, **ptr;
11797 struct lre_hash **hptr, *entry;
11798 hptr = lre_probe(rstate, left, right);
11799 if (!hptr || !*hptr) {
11800 return;
11801 }
11802 entry = *hptr;
11803 *hptr = entry->next;
11804 xfree(entry);
11805
11806 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
11807 edge = *ptr;
11808 if (edge->node == right) {
11809 *ptr = edge->next;
11810 memset(edge, 0, sizeof(*edge));
11811 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011812 right->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011813 break;
11814 }
11815 }
11816 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
11817 edge = *ptr;
11818 if (edge->node == left) {
11819 *ptr = edge->next;
11820 memset(edge, 0, sizeof(*edge));
11821 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011822 left->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011823 break;
11824 }
11825 }
11826}
11827
11828static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
11829{
11830 struct live_range_edge *edge, *next;
11831 for(edge = range->edges; edge; edge = next) {
11832 next = edge->next;
11833 remove_live_edge(rstate, range, edge->node);
11834 }
11835}
11836
Eric Biederman153ea352003-06-20 14:43:20 +000011837static void transfer_live_edges(struct reg_state *rstate,
11838 struct live_range *dest, struct live_range *src)
11839{
11840 struct live_range_edge *edge, *next;
11841 for(edge = src->edges; edge; edge = next) {
11842 struct live_range *other;
11843 next = edge->next;
11844 other = edge->node;
11845 remove_live_edge(rstate, src, other);
11846 add_live_edge(rstate, dest, other);
11847 }
11848}
11849
Eric Biedermanb138ac82003-04-22 18:44:01 +000011850
11851/* Interference graph...
11852 *
11853 * new(n) --- Return a graph with n nodes but no edges.
11854 * add(g,x,y) --- Return a graph including g with an between x and y
11855 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
11856 * x and y in the graph g
11857 * degree(g, x) --- Return the degree of the node x in the graph g
11858 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
11859 *
11860 * Implement with a hash table && a set of adjcency vectors.
11861 * The hash table supports constant time implementations of add and interfere.
11862 * The adjacency vectors support an efficient implementation of neighbors.
11863 */
11864
11865/*
11866 * +---------------------------------------------------+
11867 * | +--------------+ |
11868 * v v | |
11869 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
11870 *
11871 * -- In simplify implment optimistic coloring... (No backtracking)
11872 * -- Implement Rematerialization it is the only form of spilling we can perform
11873 * Essentially this means dropping a constant from a register because
11874 * we can regenerate it later.
11875 *
11876 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
11877 * coalesce at phi points...
11878 * --- Bias coloring if at all possible do the coalesing a compile time.
11879 *
11880 *
11881 */
11882
11883static void different_colored(
11884 struct compile_state *state, struct reg_state *rstate,
11885 struct triple *parent, struct triple *ins)
11886{
11887 struct live_range *lr;
11888 struct triple **expr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011889 lr = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011890 expr = triple_rhs(state, ins, 0);
11891 for(;expr; expr = triple_rhs(state, ins, expr)) {
11892 struct live_range *lr2;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011893 if (!*expr || (*expr == parent) || (*expr == ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011894 continue;
11895 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011896 lr2 = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011897 if (lr->color == lr2->color) {
11898 internal_error(state, ins, "live range too big");
11899 }
11900 }
11901}
11902
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011903
11904static struct live_range *coalesce_ranges(
11905 struct compile_state *state, struct reg_state *rstate,
11906 struct live_range *lr1, struct live_range *lr2)
11907{
11908 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
11909 unsigned color;
11910 unsigned classes;
11911 if (lr1 == lr2) {
11912 return lr1;
11913 }
11914 if (!lr1->defs || !lr2->defs) {
11915 internal_error(state, 0,
11916 "cannot coalese dead live ranges");
11917 }
11918 if ((lr1->color == REG_UNNEEDED) ||
11919 (lr2->color == REG_UNNEEDED)) {
11920 internal_error(state, 0,
11921 "cannot coalesce live ranges without a possible color");
11922 }
11923 if ((lr1->color != lr2->color) &&
11924 (lr1->color != REG_UNSET) &&
11925 (lr2->color != REG_UNSET)) {
11926 internal_error(state, lr1->defs->def,
11927 "cannot coalesce live ranges of different colors");
11928 }
11929 color = lr1->color;
11930 if (color == REG_UNSET) {
11931 color = lr2->color;
11932 }
11933 classes = lr1->classes & lr2->classes;
11934 if (!classes) {
11935 internal_error(state, lr1->defs->def,
11936 "cannot coalesce live ranges with dissimilar register classes");
11937 }
11938 /* If there is a clear dominate live range put it in lr1,
11939 * For purposes of this test phi functions are
11940 * considered dominated by the definitions that feed into
11941 * them.
11942 */
11943 if ((lr1->defs->prev->def->op == OP_PHI) ||
11944 ((lr2->defs->prev->def->op != OP_PHI) &&
11945 tdominates(state, lr2->defs->def, lr1->defs->def))) {
11946 struct live_range *tmp;
11947 tmp = lr1;
11948 lr1 = lr2;
11949 lr2 = tmp;
11950 }
11951#if 0
11952 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
11953 fprintf(stderr, "lr1 post\n");
11954 }
11955 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
11956 fprintf(stderr, "lr1 pre\n");
11957 }
11958 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
11959 fprintf(stderr, "lr2 post\n");
11960 }
11961 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
11962 fprintf(stderr, "lr2 pre\n");
11963 }
11964#endif
Eric Biederman153ea352003-06-20 14:43:20 +000011965#if 0
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011966 fprintf(stderr, "coalesce color1(%p): %3d color2(%p) %3d\n",
11967 lr1->defs->def,
11968 lr1->color,
11969 lr2->defs->def,
11970 lr2->color);
11971#endif
11972
11973 lr1->classes = classes;
11974 /* Append lr2 onto lr1 */
11975#warning "FIXME should this be a merge instead of a splice?"
Eric Biederman153ea352003-06-20 14:43:20 +000011976 /* This FIXME item applies to the correctness of live_range_end
11977 * and to the necessity of making multiple passes of coalesce_live_ranges.
11978 * A failure to find some coalesce opportunities in coaleace_live_ranges
11979 * does not impact the correct of the compiler just the efficiency with
11980 * which registers are allocated.
11981 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011982 head = lr1->defs;
11983 mid1 = lr1->defs->prev;
11984 mid2 = lr2->defs;
11985 end = lr2->defs->prev;
11986
11987 head->prev = end;
11988 end->next = head;
11989
11990 mid1->next = mid2;
11991 mid2->prev = mid1;
11992
11993 /* Fixup the live range in the added live range defs */
11994 lrd = head;
11995 do {
11996 lrd->lr = lr1;
11997 lrd = lrd->next;
11998 } while(lrd != head);
11999
12000 /* Mark lr2 as free. */
12001 lr2->defs = 0;
12002 lr2->color = REG_UNNEEDED;
12003 lr2->classes = 0;
12004
12005 if (!lr1->defs) {
12006 internal_error(state, 0, "lr1->defs == 0 ?");
12007 }
12008
12009 lr1->color = color;
12010 lr1->classes = classes;
12011
Eric Biederman153ea352003-06-20 14:43:20 +000012012 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
12013 transfer_live_edges(rstate, lr1, lr2);
12014
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012015 return lr1;
12016}
12017
12018static struct live_range_def *live_range_head(
12019 struct compile_state *state, struct live_range *lr,
12020 struct live_range_def *last)
12021{
12022 struct live_range_def *result;
12023 result = 0;
12024 if (last == 0) {
12025 result = lr->defs;
12026 }
12027 else if (!tdominates(state, lr->defs->def, last->next->def)) {
12028 result = last->next;
12029 }
12030 return result;
12031}
12032
12033static struct live_range_def *live_range_end(
12034 struct compile_state *state, struct live_range *lr,
12035 struct live_range_def *last)
12036{
12037 struct live_range_def *result;
12038 result = 0;
12039 if (last == 0) {
12040 result = lr->defs->prev;
12041 }
12042 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
12043 result = last->prev;
12044 }
12045 return result;
12046}
12047
12048
Eric Biedermanb138ac82003-04-22 18:44:01 +000012049static void initialize_live_ranges(
12050 struct compile_state *state, struct reg_state *rstate)
12051{
12052 struct triple *ins, *first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012053 size_t count, size;
12054 int i, j;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012055
Eric Biederman0babc1c2003-05-09 02:39:00 +000012056 first = RHS(state->main_function, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012057 /* First count how many instructions I have.
Eric Biedermanb138ac82003-04-22 18:44:01 +000012058 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012059 count = count_triples(state);
12060 /* Potentially I need one live range definitions for each
12061 * instruction, plus an extra for the split routines.
12062 */
12063 rstate->defs = count + 1;
12064 /* Potentially I need one live range for each instruction
12065 * plus an extra for the dummy live range.
12066 */
12067 rstate->ranges = count + 1;
12068 size = sizeof(rstate->lrd[0]) * rstate->defs;
12069 rstate->lrd = xcmalloc(size, "live_range_def");
12070 size = sizeof(rstate->lr[0]) * rstate->ranges;
12071 rstate->lr = xcmalloc(size, "live_range");
12072
Eric Biedermanb138ac82003-04-22 18:44:01 +000012073 /* Setup the dummy live range */
12074 rstate->lr[0].classes = 0;
12075 rstate->lr[0].color = REG_UNSET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012076 rstate->lr[0].defs = 0;
12077 i = j = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012078 ins = first;
12079 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012080 /* If the triple is a variable give it a live range */
Eric Biederman0babc1c2003-05-09 02:39:00 +000012081 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012082 struct reg_info info;
12083 /* Find the architecture specific color information */
12084 info = find_def_color(state, ins);
12085
Eric Biedermanb138ac82003-04-22 18:44:01 +000012086 i++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012087 rstate->lr[i].defs = &rstate->lrd[j];
12088 rstate->lr[i].color = info.reg;
12089 rstate->lr[i].classes = info.regcm;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012090 rstate->lr[i].degree = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012091 rstate->lrd[j].lr = &rstate->lr[i];
12092 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012093 /* Otherwise give the triple the dummy live range. */
12094 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012095 rstate->lrd[j].lr = &rstate->lr[0];
Eric Biedermanb138ac82003-04-22 18:44:01 +000012096 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012097
12098 /* Initalize the live_range_def */
12099 rstate->lrd[j].next = &rstate->lrd[j];
12100 rstate->lrd[j].prev = &rstate->lrd[j];
12101 rstate->lrd[j].def = ins;
12102 rstate->lrd[j].orig_id = ins->id;
12103 ins->id = j;
12104
12105 j++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012106 ins = ins->next;
12107 } while(ins != first);
12108 rstate->ranges = i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012109 rstate->defs -= 1;
12110
Eric Biedermanb138ac82003-04-22 18:44:01 +000012111 /* Make a second pass to handle achitecture specific register
12112 * constraints.
12113 */
12114 ins = first;
12115 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012116 int zlhs, zrhs, i, j;
12117 if (ins->id > rstate->defs) {
12118 internal_error(state, ins, "bad id");
12119 }
12120
12121 /* Walk through the template of ins and coalesce live ranges */
12122 zlhs = TRIPLE_LHS(ins->sizes);
12123 if ((zlhs == 0) && triple_is_def(state, ins)) {
12124 zlhs = 1;
12125 }
12126 zrhs = TRIPLE_RHS(ins->sizes);
12127
12128 for(i = 0; i < zlhs; i++) {
12129 struct reg_info linfo;
12130 struct live_range_def *lhs;
12131 linfo = arch_reg_lhs(state, ins, i);
12132 if (linfo.reg < MAX_REGISTERS) {
12133 continue;
12134 }
12135 if (triple_is_def(state, ins)) {
12136 lhs = &rstate->lrd[ins->id];
12137 } else {
12138 lhs = &rstate->lrd[LHS(ins, i)->id];
12139 }
12140 for(j = 0; j < zrhs; j++) {
12141 struct reg_info rinfo;
12142 struct live_range_def *rhs;
12143 rinfo = arch_reg_rhs(state, ins, j);
12144 if (rinfo.reg < MAX_REGISTERS) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012145 continue;
12146 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012147 rhs = &rstate->lrd[RHS(ins, i)->id];
12148 if (rinfo.reg == linfo.reg) {
12149 coalesce_ranges(state, rstate,
12150 lhs->lr, rhs->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012151 }
12152 }
12153 }
12154 ins = ins->next;
12155 } while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012156}
12157
Eric Biedermanf96a8102003-06-16 16:57:34 +000012158static void graph_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000012159 struct compile_state *state,
12160 struct reg_block *blocks, struct triple_reg_set *live,
12161 struct reg_block *rb, struct triple *ins, void *arg)
12162{
12163 struct reg_state *rstate = arg;
12164 struct live_range *def;
12165 struct triple_reg_set *entry;
12166
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012167 /* If the triple is not a definition
Eric Biedermanb138ac82003-04-22 18:44:01 +000012168 * we do not have a definition to add to
12169 * the interference graph.
12170 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012171 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000012172 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012173 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012174 def = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012175
12176 /* Create an edge between ins and everything that is
12177 * alive, unless the live_range cannot share
12178 * a physical register with ins.
12179 */
12180 for(entry = live; entry; entry = entry->next) {
12181 struct live_range *lr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012182 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
12183 internal_error(state, 0, "bad entry?");
12184 }
12185 lr = rstate->lrd[entry->member->id].lr;
12186 if (def == lr) {
12187 continue;
12188 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012189 if (!arch_regcm_intersect(def->classes, lr->classes)) {
12190 continue;
12191 }
12192 add_live_edge(rstate, def, lr);
12193 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012194 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012195}
12196
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012197static struct live_range *get_verify_live_range(
12198 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
12199{
12200 struct live_range *lr;
12201 struct live_range_def *lrd;
12202 int ins_found;
12203 if ((ins->id < 0) || (ins->id > rstate->defs)) {
12204 internal_error(state, ins, "bad ins?");
12205 }
12206 lr = rstate->lrd[ins->id].lr;
12207 ins_found = 0;
12208 lrd = lr->defs;
12209 do {
12210 if (lrd->def == ins) {
12211 ins_found = 1;
12212 }
12213 lrd = lrd->next;
12214 } while(lrd != lr->defs);
12215 if (!ins_found) {
12216 internal_error(state, ins, "ins not in live range");
12217 }
12218 return lr;
12219}
12220
12221static void verify_graph_ins(
12222 struct compile_state *state,
12223 struct reg_block *blocks, struct triple_reg_set *live,
12224 struct reg_block *rb, struct triple *ins, void *arg)
12225{
12226 struct reg_state *rstate = arg;
12227 struct triple_reg_set *entry1, *entry2;
12228
12229
12230 /* Compare live against edges and make certain the code is working */
12231 for(entry1 = live; entry1; entry1 = entry1->next) {
12232 struct live_range *lr1;
12233 lr1 = get_verify_live_range(state, rstate, entry1->member);
12234 for(entry2 = live; entry2; entry2 = entry2->next) {
12235 struct live_range *lr2;
12236 struct live_range_edge *edge2;
12237 int lr1_found;
12238 int lr2_degree;
12239 if (entry2 == entry1) {
12240 continue;
12241 }
12242 lr2 = get_verify_live_range(state, rstate, entry2->member);
12243 if (lr1 == lr2) {
12244 internal_error(state, entry2->member,
12245 "live range with 2 values simultaneously alive");
12246 }
12247 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
12248 continue;
12249 }
12250 if (!interfere(rstate, lr1, lr2)) {
12251 internal_error(state, entry2->member,
12252 "edges don't interfere?");
12253 }
12254
12255 lr1_found = 0;
12256 lr2_degree = 0;
12257 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
12258 lr2_degree++;
12259 if (edge2->node == lr1) {
12260 lr1_found = 1;
12261 }
12262 }
12263 if (lr2_degree != lr2->degree) {
12264 internal_error(state, entry2->member,
12265 "computed degree: %d does not match reported degree: %d\n",
12266 lr2_degree, lr2->degree);
12267 }
12268 if (!lr1_found) {
12269 internal_error(state, entry2->member, "missing edge");
12270 }
12271 }
12272 }
12273 return;
12274}
12275
Eric Biedermanb138ac82003-04-22 18:44:01 +000012276
Eric Biedermanf96a8102003-06-16 16:57:34 +000012277static void print_interference_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000012278 struct compile_state *state,
12279 struct reg_block *blocks, struct triple_reg_set *live,
12280 struct reg_block *rb, struct triple *ins, void *arg)
12281{
12282 struct reg_state *rstate = arg;
12283 struct live_range *lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012284 unsigned id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012285
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012286 lr = rstate->lrd[ins->id].lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012287 id = ins->id;
12288 ins->id = rstate->lrd[id].orig_id;
12289 SET_REG(ins->id, lr->color);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012290 display_triple(stdout, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012291 ins->id = id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012292
12293 if (lr->defs) {
12294 struct live_range_def *lrd;
12295 printf(" range:");
12296 lrd = lr->defs;
12297 do {
12298 printf(" %-10p", lrd->def);
12299 lrd = lrd->next;
12300 } while(lrd != lr->defs);
12301 printf("\n");
12302 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012303 if (live) {
12304 struct triple_reg_set *entry;
12305 printf(" live:");
12306 for(entry = live; entry; entry = entry->next) {
12307 printf(" %-10p", entry->member);
12308 }
12309 printf("\n");
12310 }
12311 if (lr->edges) {
12312 struct live_range_edge *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012313 printf(" edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000012314 for(entry = lr->edges; entry; entry = entry->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012315 struct live_range_def *lrd;
12316 lrd = entry->node->defs;
12317 do {
12318 printf(" %-10p", lrd->def);
12319 lrd = lrd->next;
12320 } while(lrd != entry->node->defs);
12321 printf("|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000012322 }
12323 printf("\n");
12324 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012325 if (triple_is_branch(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012326 printf("\n");
12327 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012328 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012329}
12330
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012331static int coalesce_live_ranges(
12332 struct compile_state *state, struct reg_state *rstate)
12333{
12334 /* At the point where a value is moved from one
12335 * register to another that value requires two
12336 * registers, thus increasing register pressure.
12337 * Live range coaleescing reduces the register
12338 * pressure by keeping a value in one register
12339 * longer.
12340 *
12341 * In the case of a phi function all paths leading
12342 * into it must be allocated to the same register
12343 * otherwise the phi function may not be removed.
12344 *
12345 * Forcing a value to stay in a single register
12346 * for an extended period of time does have
12347 * limitations when applied to non homogenous
12348 * register pool.
12349 *
12350 * The two cases I have identified are:
12351 * 1) Two forced register assignments may
12352 * collide.
12353 * 2) Registers may go unused because they
12354 * are only good for storing the value
12355 * and not manipulating it.
12356 *
12357 * Because of this I need to split live ranges,
12358 * even outside of the context of coalesced live
12359 * ranges. The need to split live ranges does
12360 * impose some constraints on live range coalescing.
12361 *
12362 * - Live ranges may not be coalesced across phi
12363 * functions. This creates a 2 headed live
12364 * range that cannot be sanely split.
12365 *
12366 * - phi functions (coalesced in initialize_live_ranges)
12367 * are handled as pre split live ranges so we will
12368 * never attempt to split them.
12369 */
12370 int coalesced;
12371 int i;
12372
12373 coalesced = 0;
12374 for(i = 0; i <= rstate->ranges; i++) {
12375 struct live_range *lr1;
12376 struct live_range_def *lrd1;
12377 lr1 = &rstate->lr[i];
12378 if (!lr1->defs) {
12379 continue;
12380 }
12381 lrd1 = live_range_end(state, lr1, 0);
12382 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
12383 struct triple_set *set;
12384 if (lrd1->def->op != OP_COPY) {
12385 continue;
12386 }
12387 /* Skip copies that are the result of a live range split. */
12388 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
12389 continue;
12390 }
12391 for(set = lrd1->def->use; set; set = set->next) {
12392 struct live_range_def *lrd2;
12393 struct live_range *lr2, *res;
12394
12395 lrd2 = &rstate->lrd[set->member->id];
12396
12397 /* Don't coalesce with instructions
12398 * that are the result of a live range
12399 * split.
12400 */
12401 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
12402 continue;
12403 }
12404 lr2 = rstate->lrd[set->member->id].lr;
12405 if (lr1 == lr2) {
12406 continue;
12407 }
12408 if ((lr1->color != lr2->color) &&
12409 (lr1->color != REG_UNSET) &&
12410 (lr2->color != REG_UNSET)) {
12411 continue;
12412 }
12413 if ((lr1->classes & lr2->classes) == 0) {
12414 continue;
12415 }
12416
12417 if (interfere(rstate, lr1, lr2)) {
12418 continue;
12419 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012420
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012421 res = coalesce_ranges(state, rstate, lr1, lr2);
12422 coalesced += 1;
12423 if (res != lr1) {
12424 goto next;
12425 }
12426 }
12427 }
12428 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000012429 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012430 }
12431 return coalesced;
12432}
12433
12434
Eric Biedermanf96a8102003-06-16 16:57:34 +000012435static void fix_coalesce_conflicts(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012436 struct reg_block *blocks, struct triple_reg_set *live,
12437 struct reg_block *rb, struct triple *ins, void *arg)
12438{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012439 int zlhs, zrhs, i, j;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012440
12441 /* See if we have a mandatory coalesce operation between
12442 * a lhs and a rhs value. If so and the rhs value is also
12443 * alive then this triple needs to be pre copied. Otherwise
12444 * we would have two definitions in the same live range simultaneously
12445 * alive.
12446 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012447 zlhs = TRIPLE_LHS(ins->sizes);
12448 if ((zlhs == 0) && triple_is_def(state, ins)) {
12449 zlhs = 1;
12450 }
12451 zrhs = TRIPLE_RHS(ins->sizes);
Eric Biedermanf96a8102003-06-16 16:57:34 +000012452 for(i = 0; i < zlhs; i++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012453 struct reg_info linfo;
12454 linfo = arch_reg_lhs(state, ins, i);
12455 if (linfo.reg < MAX_REGISTERS) {
12456 continue;
12457 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012458 for(j = 0; j < zrhs; j++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012459 struct reg_info rinfo;
12460 struct triple *rhs;
12461 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000012462 int found;
12463 found = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012464 rinfo = arch_reg_rhs(state, ins, j);
12465 if (rinfo.reg != linfo.reg) {
12466 continue;
12467 }
12468 rhs = RHS(ins, j);
Eric Biedermanf96a8102003-06-16 16:57:34 +000012469 for(set = live; set && !found; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012470 if (set->member == rhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000012471 found = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012472 }
12473 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012474 if (found) {
12475 struct triple *copy;
12476 copy = pre_copy(state, ins, j);
12477 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12478 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012479 }
12480 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012481 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012482}
12483
Eric Biedermanf96a8102003-06-16 16:57:34 +000012484static void replace_set_use(struct compile_state *state,
12485 struct triple_reg_set *head, struct triple *orig, struct triple *new)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012486{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012487 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000012488 for(set = head; set; set = set->next) {
12489 if (set->member == orig) {
12490 set->member = new;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012491 }
12492 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012493}
12494
Eric Biedermanf96a8102003-06-16 16:57:34 +000012495static void replace_block_use(struct compile_state *state,
12496 struct reg_block *blocks, struct triple *orig, struct triple *new)
12497{
12498 int i;
12499#warning "WISHLIST visit just those blocks that need it *"
12500 for(i = 1; i <= state->last_vertex; i++) {
12501 struct reg_block *rb;
12502 rb = &blocks[i];
12503 replace_set_use(state, rb->in, orig, new);
12504 replace_set_use(state, rb->out, orig, new);
12505 }
12506}
12507
12508static void color_instructions(struct compile_state *state)
12509{
12510 struct triple *ins, *first;
12511 first = RHS(state->main_function, 0);
12512 ins = first;
12513 do {
12514 if (triple_is_def(state, ins)) {
12515 struct reg_info info;
12516 info = find_lhs_color(state, ins, 0);
12517 if (info.reg >= MAX_REGISTERS) {
12518 info.reg = REG_UNSET;
12519 }
12520 SET_INFO(ins->id, info);
12521 }
12522 ins = ins->next;
12523 } while(ins != first);
12524}
12525
12526static struct reg_info read_lhs_color(
12527 struct compile_state *state, struct triple *ins, int index)
12528{
12529 struct reg_info info;
12530 if ((index == 0) && triple_is_def(state, ins)) {
12531 info.reg = ID_REG(ins->id);
12532 info.regcm = ID_REGCM(ins->id);
12533 }
12534 else if (index < TRIPLE_LHS(ins->sizes)) {
12535 info = read_lhs_color(state, LHS(ins, index), 0);
12536 }
12537 else {
12538 internal_error(state, ins, "Bad lhs %d", index);
12539 info.reg = REG_UNSET;
12540 info.regcm = 0;
12541 }
12542 return info;
12543}
12544
12545static struct triple *resolve_tangle(
12546 struct compile_state *state, struct triple *tangle)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012547{
12548 struct reg_info info, uinfo;
12549 struct triple_set *set, *next;
12550 struct triple *copy;
12551
Eric Biedermanf96a8102003-06-16 16:57:34 +000012552#warning "WISHLIST recalculate all affected instructions colors"
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012553 info = find_lhs_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012554 for(set = tangle->use; set; set = next) {
12555 struct triple *user;
12556 int i, zrhs;
12557 next = set->next;
12558 user = set->member;
12559 zrhs = TRIPLE_RHS(user->sizes);
12560 for(i = 0; i < zrhs; i++) {
12561 if (RHS(user, i) != tangle) {
12562 continue;
12563 }
12564 uinfo = find_rhs_post_color(state, user, i);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012565 if (uinfo.reg == info.reg) {
12566 copy = pre_copy(state, user, i);
12567 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000012568 SET_INFO(copy->id, uinfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012569 }
12570 }
12571 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012572 copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012573 uinfo = find_lhs_pre_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012574 if (uinfo.reg == info.reg) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000012575 struct reg_info linfo;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012576 copy = post_copy(state, tangle);
12577 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000012578 linfo = find_lhs_color(state, copy, 0);
12579 SET_INFO(copy->id, linfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012580 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012581 info = find_lhs_color(state, tangle, 0);
12582 SET_INFO(tangle->id, info);
12583
12584 return copy;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012585}
12586
12587
Eric Biedermanf96a8102003-06-16 16:57:34 +000012588static void fix_tangles(struct compile_state *state,
12589 struct reg_block *blocks, struct triple_reg_set *live,
12590 struct reg_block *rb, struct triple *ins, void *arg)
12591{
Eric Biederman153ea352003-06-20 14:43:20 +000012592 int *tangles = arg;
Eric Biedermanf96a8102003-06-16 16:57:34 +000012593 struct triple *tangle;
12594 do {
12595 char used[MAX_REGISTERS];
12596 struct triple_reg_set *set;
12597 tangle = 0;
12598
12599 /* Find out which registers have multiple uses at this point */
12600 memset(used, 0, sizeof(used));
12601 for(set = live; set; set = set->next) {
12602 struct reg_info info;
12603 info = read_lhs_color(state, set->member, 0);
12604 if (info.reg == REG_UNSET) {
12605 continue;
12606 }
12607 reg_inc_used(state, used, info.reg);
12608 }
12609
12610 /* Now find the least dominated definition of a register in
12611 * conflict I have seen so far.
12612 */
12613 for(set = live; set; set = set->next) {
12614 struct reg_info info;
12615 info = read_lhs_color(state, set->member, 0);
12616 if (used[info.reg] < 2) {
12617 continue;
12618 }
Eric Biederman153ea352003-06-20 14:43:20 +000012619 /* Changing copies that feed into phi functions
12620 * is incorrect.
12621 */
12622 if (set->member->use &&
12623 (set->member->use->member->op == OP_PHI)) {
12624 continue;
12625 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012626 if (!tangle || tdominates(state, set->member, tangle)) {
12627 tangle = set->member;
12628 }
12629 }
12630 /* If I have found a tangle resolve it */
12631 if (tangle) {
12632 struct triple *post_copy;
Eric Biederman153ea352003-06-20 14:43:20 +000012633 (*tangles)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000012634 post_copy = resolve_tangle(state, tangle);
12635 if (post_copy) {
12636 replace_block_use(state, blocks, tangle, post_copy);
12637 }
12638 if (post_copy && (tangle != ins)) {
12639 replace_set_use(state, live, tangle, post_copy);
12640 }
12641 }
12642 } while(tangle);
12643 return;
12644}
12645
Eric Biederman153ea352003-06-20 14:43:20 +000012646static int correct_tangles(
Eric Biedermanf96a8102003-06-16 16:57:34 +000012647 struct compile_state *state, struct reg_block *blocks)
12648{
Eric Biederman153ea352003-06-20 14:43:20 +000012649 int tangles;
12650 tangles = 0;
Eric Biedermanf96a8102003-06-16 16:57:34 +000012651 color_instructions(state);
Eric Biederman153ea352003-06-20 14:43:20 +000012652 walk_variable_lifetimes(state, blocks, fix_tangles, &tangles);
12653 return tangles;
Eric Biedermanf96a8102003-06-16 16:57:34 +000012654}
12655
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012656struct least_conflict {
12657 struct reg_state *rstate;
12658 struct live_range *ref_range;
12659 struct triple *ins;
12660 struct triple_reg_set *live;
12661 size_t count;
Eric Biedermand3283ec2003-06-18 11:03:18 +000012662 int constraints;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012663};
Eric Biedermanf96a8102003-06-16 16:57:34 +000012664static void least_conflict(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012665 struct reg_block *blocks, struct triple_reg_set *live,
12666 struct reg_block *rb, struct triple *ins, void *arg)
12667{
12668 struct least_conflict *conflict = arg;
12669 struct live_range_edge *edge;
12670 struct triple_reg_set *set;
12671 size_t count;
Eric Biedermand3283ec2003-06-18 11:03:18 +000012672 int constraints;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012673
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012674#warning "FIXME handle instructions with left hand sides..."
12675 /* Only instructions that introduce a new definition
12676 * can be the conflict instruction.
12677 */
12678 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000012679 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012680 }
12681
12682 /* See if live ranges at this instruction are a
12683 * strict subset of the live ranges that are in conflict.
12684 */
12685 count = 0;
12686 for(set = live; set; set = set->next) {
12687 struct live_range *lr;
12688 lr = conflict->rstate->lrd[set->member->id].lr;
Eric Biedermand3283ec2003-06-18 11:03:18 +000012689 /* Ignore it if there cannot be an edge between these two nodes */
12690 if (!arch_regcm_intersect(conflict->ref_range->classes, lr->classes)) {
12691 continue;
12692 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012693 for(edge = conflict->ref_range->edges; edge; edge = edge->next) {
12694 if (edge->node == lr) {
12695 break;
12696 }
12697 }
12698 if (!edge && (lr != conflict->ref_range)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000012699 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012700 }
12701 count++;
12702 }
12703 if (count <= 1) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000012704 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012705 }
12706
Eric Biedermand3283ec2003-06-18 11:03:18 +000012707#if 0
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012708 /* See if there is an uncolored member in this subset.
12709 */
12710 for(set = live; set; set = set->next) {
12711 struct live_range *lr;
12712 lr = conflict->rstate->lrd[set->member->id].lr;
12713 if (lr->color == REG_UNSET) {
12714 break;
12715 }
12716 }
12717 if (!set && (conflict->ref_range != REG_UNSET)) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000012718 return;
12719 }
12720#endif
12721
12722 /* See if any of the live registers are constrained,
12723 * if not it won't be productive to pick this as
12724 * a conflict instruction.
12725 */
12726 constraints = 0;
12727 for(set = live; set; set = set->next) {
12728 struct triple_set *uset;
12729 struct reg_info info;
12730 unsigned classes;
12731 unsigned cur_size, size;
12732 /* Skip this instruction */
12733 if (set->member == ins) {
12734 continue;
12735 }
12736 /* Find how many registers this value can potentially
12737 * be assigned to.
12738 */
12739 classes = arch_type_to_regcm(state, set->member->type);
12740 size = regc_max_size(state, classes);
12741
12742 /* Find how many registers we allow this value to
12743 * be assigned to.
12744 */
12745 info = arch_reg_lhs(state, set->member, 0);
12746
12747 /* If the value does not live in a register it
12748 * isn't constrained.
12749 */
12750 if (info.reg == REG_UNNEEDED) {
12751 continue;
12752 }
12753
12754 if ((info.reg == REG_UNSET) || (info.reg >= MAX_REGISTERS)) {
12755 cur_size = regc_max_size(state, info.regcm);
12756 } else {
12757 cur_size = 1;
12758 }
12759
12760 /* If there is no difference between potential and
12761 * actual register count there is not a constraint
12762 */
12763 if (cur_size >= size) {
12764 continue;
12765 }
12766
12767 /* If this live_range feeds into conflict->inds
12768 * it isn't a constraint we can relieve.
12769 */
12770 for(uset = set->member->use; uset; uset = uset->next) {
12771 if (uset->member == ins) {
12772 break;
12773 }
12774 }
12775 if (uset) {
12776 continue;
12777 }
12778 constraints = 1;
12779 break;
12780 }
12781 /* Don't drop canidates with constraints */
12782 if (conflict->constraints && !constraints) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000012783 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012784 }
12785
12786
Eric Biedermand3283ec2003-06-18 11:03:18 +000012787#if 0
12788 fprintf(stderr, "conflict ins? %p %s count: %d constraints: %d\n",
12789 ins, tops(ins->op), count, constraints);
12790#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012791 /* Find the instruction with the largest possible subset of
12792 * conflict ranges and that dominates any other instruction
12793 * with an equal sized set of conflicting ranges.
12794 */
12795 if ((count > conflict->count) ||
12796 ((count == conflict->count) &&
12797 tdominates(state, ins, conflict->ins))) {
12798 struct triple_reg_set *next;
12799 /* Remember the canidate instruction */
12800 conflict->ins = ins;
12801 conflict->count = count;
Eric Biedermand3283ec2003-06-18 11:03:18 +000012802 conflict->constraints = constraints;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012803 /* Free the old collection of live registers */
12804 for(set = conflict->live; set; set = next) {
12805 next = set->next;
12806 do_triple_unset(&conflict->live, set->member);
12807 }
12808 conflict->live = 0;
12809 /* Rember the registers that are alive but do not feed
12810 * into or out of conflict->ins.
12811 */
12812 for(set = live; set; set = set->next) {
12813 struct triple **expr;
12814 if (set->member == ins) {
12815 goto next;
12816 }
12817 expr = triple_rhs(state, ins, 0);
12818 for(;expr; expr = triple_rhs(state, ins, expr)) {
12819 if (*expr == set->member) {
12820 goto next;
12821 }
12822 }
12823 expr = triple_lhs(state, ins, 0);
12824 for(; expr; expr = triple_lhs(state, ins, expr)) {
12825 if (*expr == set->member) {
12826 goto next;
12827 }
12828 }
12829 do_triple_set(&conflict->live, set->member, set->new);
12830 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000012831 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012832 }
12833 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012834 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012835}
12836
12837static void find_range_conflict(struct compile_state *state,
12838 struct reg_state *rstate, char *used, struct live_range *ref_range,
12839 struct least_conflict *conflict)
12840{
Eric Biederman8d9c1232003-06-17 08:42:17 +000012841
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012842 /* there are 3 kinds ways conflicts can occure.
12843 * 1) the life time of 2 values simply overlap.
12844 * 2) the 2 values feed into the same instruction.
12845 * 3) the 2 values feed into a phi function.
12846 */
12847
12848 /* find the instruction where the problematic conflict comes
12849 * into existance. that the instruction where all of
12850 * the values are alive, and among such instructions it is
12851 * the least dominated one.
12852 *
12853 * a value is alive an an instruction if either;
12854 * 1) the value defintion dominates the instruction and there
12855 * is a use at or after that instrction
12856 * 2) the value definition feeds into a phi function in the
12857 * same block as the instruction. and the phi function
12858 * is at or after the instruction.
12859 */
12860 memset(conflict, 0, sizeof(*conflict));
Eric Biedermand3283ec2003-06-18 11:03:18 +000012861 conflict->rstate = rstate;
12862 conflict->ref_range = ref_range;
12863 conflict->ins = 0;
12864 conflict->live = 0;
12865 conflict->count = 0;
12866 conflict->constraints = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012867 walk_variable_lifetimes(state, rstate->blocks, least_conflict, conflict);
12868
12869 if (!conflict->ins) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000012870 internal_error(state, ref_range->defs->def, "No conflict ins?");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012871 }
12872 if (!conflict->live) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000012873 internal_error(state, ref_range->defs->def, "No conflict live?");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012874 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000012875#if 0
12876 fprintf(stderr, "conflict ins: %p %s count: %d constraints: %d\n",
12877 conflict->ins, tops(conflict->ins->op),
12878 conflict->count, conflict->constraints);
12879#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012880 return;
12881}
12882
12883static struct triple *split_constrained_range(struct compile_state *state,
12884 struct reg_state *rstate, char *used, struct least_conflict *conflict)
12885{
12886 unsigned constrained_size;
12887 struct triple *new, *constrained;
12888 struct triple_reg_set *cset;
12889 /* Find a range that is having problems because it is
12890 * artificially constrained.
12891 */
12892 constrained_size = ~0;
12893 constrained = 0;
12894 new = 0;
12895 for(cset = conflict->live; cset; cset = cset->next) {
12896 struct triple_set *set;
12897 struct reg_info info;
12898 unsigned classes;
12899 unsigned cur_size, size;
12900 /* Skip the live range that starts with conflict->ins */
12901 if (cset->member == conflict->ins) {
12902 continue;
12903 }
12904 /* Find how many registers this value can potentially
12905 * be assigned to.
12906 */
12907 classes = arch_type_to_regcm(state, cset->member->type);
12908 size = regc_max_size(state, classes);
12909
12910 /* Find how many registers we allow this value to
12911 * be assigned to.
12912 */
12913 info = arch_reg_lhs(state, cset->member, 0);
Eric Biedermand3283ec2003-06-18 11:03:18 +000012914
12915 /* If the register doesn't need a register
12916 * splitting it can't help.
12917 */
12918 if (info.reg == REG_UNNEEDED) {
12919 continue;
12920 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012921#warning "FIXME do I need a call to arch_reg_rhs around here somewhere?"
12922 if ((info.reg == REG_UNSET) || (info.reg >= MAX_REGISTERS)) {
12923 cur_size = regc_max_size(state, info.regcm);
12924 } else {
12925 cur_size = 1;
12926 }
12927 /* If this live_range feeds into conflict->ins
12928 * splitting it is unlikely to help.
12929 */
12930 for(set = cset->member->use; set; set = set->next) {
12931 if (set->member == conflict->ins) {
12932 goto next;
12933 }
12934 }
12935
12936 /* If there is no difference between potential and
12937 * actual register count there is nothing to do.
12938 */
12939 if (cur_size >= size) {
12940 continue;
12941 }
12942 /* Of the constrained registers deal with the
12943 * most constrained one first.
12944 */
12945 if (!constrained ||
12946 (size < constrained_size)) {
12947 constrained = cset->member;
12948 constrained_size = size;
12949 }
12950 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000012951 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012952 }
12953 if (constrained) {
12954 new = post_copy(state, constrained);
12955 new->id |= TRIPLE_FLAG_POST_SPLIT;
12956 }
12957 return new;
12958}
12959
12960static int split_ranges(
12961 struct compile_state *state, struct reg_state *rstate,
12962 char *used, struct live_range *range)
12963{
12964 struct triple *new;
12965
Eric Biedermand3283ec2003-06-18 11:03:18 +000012966#if 0
12967 fprintf(stderr, "split_ranges %d %s %p\n",
12968 rstate->passes, tops(range->defs->def->op), range->defs->def);
12969#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012970 if ((range->color == REG_UNNEEDED) ||
12971 (rstate->passes >= rstate->max_passes)) {
12972 return 0;
12973 }
12974 new = 0;
12975 /* If I can't allocate a register something needs to be split */
12976 if (arch_select_free_register(state, used, range->classes) == REG_UNSET) {
12977 struct least_conflict conflict;
12978
Eric Biedermand3283ec2003-06-18 11:03:18 +000012979#if 0
12980 fprintf(stderr, "find_range_conflict\n");
12981#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012982 /* Find where in the set of registers the conflict
12983 * actually occurs.
12984 */
12985 find_range_conflict(state, rstate, used, range, &conflict);
12986
12987 /* If a range has been artifically constrained split it */
12988 new = split_constrained_range(state, rstate, used, &conflict);
12989
12990 if (!new) {
12991 /* Ideally I would split the live range that will not be used
12992 * for the longest period of time in hopes that this will
12993 * (a) allow me to spill a register or
12994 * (b) allow me to place a value in another register.
12995 *
12996 * So far I don't have a test case for this, the resolving
12997 * of mandatory constraints has solved all of my
12998 * know issues. So I have choosen not to write any
12999 * code until I cat get a better feel for cases where
13000 * it would be useful to have.
13001 *
13002 */
13003#warning "WISHLIST implement live range splitting..."
Eric Biedermand3283ec2003-06-18 11:03:18 +000013004#if 0
13005 print_blocks(state, stderr);
13006 print_dominators(state, stderr);
13007
13008#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013009 return 0;
13010 }
13011 }
13012 if (new) {
13013 rstate->lrd[rstate->defs].orig_id = new->id;
13014 new->id = rstate->defs;
13015 rstate->defs++;
13016#if 0
Eric Biedermand3283ec2003-06-18 11:03:18 +000013017 fprintf(stderr, "new: %p old: %s %p\n",
13018 new, tops(RHS(new, 0)->op), RHS(new, 0));
13019#endif
13020#if 0
13021 print_blocks(state, stderr);
13022 print_dominators(state, stderr);
13023
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013024#endif
13025 return 1;
13026 }
13027 return 0;
13028}
13029
Eric Biedermanb138ac82003-04-22 18:44:01 +000013030#if DEBUG_COLOR_GRAPH > 1
13031#define cgdebug_printf(...) fprintf(stdout, __VA_ARGS__)
13032#define cgdebug_flush() fflush(stdout)
13033#elif DEBUG_COLOR_GRAPH == 1
13034#define cgdebug_printf(...) fprintf(stderr, __VA_ARGS__)
13035#define cgdebug_flush() fflush(stderr)
13036#else
13037#define cgdebug_printf(...)
13038#define cgdebug_flush()
13039#endif
13040
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013041
13042static int select_free_color(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013043 struct reg_state *rstate, struct live_range *range)
13044{
13045 struct triple_set *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013046 struct live_range_def *lrd;
13047 struct live_range_def *phi;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013048 struct live_range_edge *edge;
13049 char used[MAX_REGISTERS];
13050 struct triple **expr;
13051
Eric Biedermanb138ac82003-04-22 18:44:01 +000013052 /* Instead of doing just the trivial color select here I try
13053 * a few extra things because a good color selection will help reduce
13054 * copies.
13055 */
13056
13057 /* Find the registers currently in use */
13058 memset(used, 0, sizeof(used));
13059 for(edge = range->edges; edge; edge = edge->next) {
13060 if (edge->node->color == REG_UNSET) {
13061 continue;
13062 }
13063 reg_fill_used(state, used, edge->node->color);
13064 }
13065#if DEBUG_COLOR_GRAPH > 1
13066 {
13067 int i;
13068 i = 0;
13069 for(edge = range->edges; edge; edge = edge->next) {
13070 i++;
13071 }
13072 cgdebug_printf("\n%s edges: %d @%s:%d.%d\n",
13073 tops(range->def->op), i,
13074 range->def->filename, range->def->line, range->def->col);
13075 for(i = 0; i < MAX_REGISTERS; i++) {
13076 if (used[i]) {
13077 cgdebug_printf("used: %s\n",
13078 arch_reg_str(i));
13079 }
13080 }
13081 }
13082#endif
13083
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013084#warning "FIXME detect conflicts caused by the source and destination being the same register"
13085
13086 /* If a color is already assigned see if it will work */
13087 if (range->color != REG_UNSET) {
13088 struct live_range_def *lrd;
13089 if (!used[range->color]) {
13090 return 1;
13091 }
13092 for(edge = range->edges; edge; edge = edge->next) {
13093 if (edge->node->color != range->color) {
13094 continue;
13095 }
13096 warning(state, edge->node->defs->def, "edge: ");
13097 lrd = edge->node->defs;
13098 do {
13099 warning(state, lrd->def, " %p %s",
13100 lrd->def, tops(lrd->def->op));
13101 lrd = lrd->next;
13102 } while(lrd != edge->node->defs);
13103 }
13104 lrd = range->defs;
13105 warning(state, range->defs->def, "def: ");
13106 do {
13107 warning(state, lrd->def, " %p %s",
13108 lrd->def, tops(lrd->def->op));
13109 lrd = lrd->next;
13110 } while(lrd != range->defs);
13111 internal_error(state, range->defs->def,
13112 "live range with already used color %s",
13113 arch_reg_str(range->color));
13114 }
13115
Eric Biedermanb138ac82003-04-22 18:44:01 +000013116 /* If I feed into an expression reuse it's color.
13117 * This should help remove copies in the case of 2 register instructions
13118 * and phi functions.
13119 */
13120 phi = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013121 lrd = live_range_end(state, range, 0);
13122 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
13123 entry = lrd->def->use;
13124 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
13125 struct live_range_def *insd;
13126 insd = &rstate->lrd[entry->member->id];
13127 if (insd->lr->defs == 0) {
13128 continue;
13129 }
13130 if (!phi && (insd->def->op == OP_PHI) &&
13131 !interfere(rstate, range, insd->lr)) {
13132 phi = insd;
13133 }
13134 if ((insd->lr->color == REG_UNSET) ||
13135 ((insd->lr->classes & range->classes) == 0) ||
13136 (used[insd->lr->color])) {
13137 continue;
13138 }
13139 if (interfere(rstate, range, insd->lr)) {
13140 continue;
13141 }
13142 range->color = insd->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013143 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013144 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013145 /* If I feed into a phi function reuse it's color or the color
Eric Biedermanb138ac82003-04-22 18:44:01 +000013146 * of something else that feeds into the phi function.
13147 */
13148 if (phi) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013149 if (phi->lr->color != REG_UNSET) {
13150 if (used[phi->lr->color]) {
13151 range->color = phi->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013152 }
13153 }
13154 else {
13155 expr = triple_rhs(state, phi->def, 0);
13156 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
13157 struct live_range *lr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013158 if (!*expr) {
13159 continue;
13160 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013161 lr = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013162 if ((lr->color == REG_UNSET) ||
13163 ((lr->classes & range->classes) == 0) ||
13164 (used[lr->color])) {
13165 continue;
13166 }
13167 if (interfere(rstate, range, lr)) {
13168 continue;
13169 }
13170 range->color = lr->color;
13171 }
13172 }
13173 }
13174 /* If I don't interfere with a rhs node reuse it's color */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013175 lrd = live_range_head(state, range, 0);
13176 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
13177 expr = triple_rhs(state, lrd->def, 0);
13178 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013179 struct live_range *lr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013180 if (!*expr) {
13181 continue;
13182 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013183 lr = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013184 if ((lr->color == -1) ||
13185 ((lr->classes & range->classes) == 0) ||
13186 (used[lr->color])) {
13187 continue;
13188 }
13189 if (interfere(rstate, range, lr)) {
13190 continue;
13191 }
13192 range->color = lr->color;
13193 break;
13194 }
13195 }
13196 /* If I have not opportunitically picked a useful color
13197 * pick the first color that is free.
13198 */
13199 if (range->color == REG_UNSET) {
13200 range->color =
13201 arch_select_free_register(state, used, range->classes);
13202 }
13203 if (range->color == REG_UNSET) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000013204 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013205 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013206 if (split_ranges(state, rstate, used, range)) {
13207 return 0;
13208 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013209 for(edge = range->edges; edge; edge = edge->next) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000013210 warning(state, edge->node->defs->def, "edge reg %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000013211 arch_reg_str(edge->node->color));
Eric Biedermand3283ec2003-06-18 11:03:18 +000013212 lrd = edge->node->defs;
13213 do {
13214 warning(state, lrd->def, " %s",
13215 tops(lrd->def->op));
13216 lrd = lrd->next;
13217 } while(lrd != edge->node->defs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013218 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000013219 warning(state, range->defs->def, "range: ");
13220 lrd = range->defs;
13221 do {
13222 warning(state, lrd->def, " %s",
13223 tops(lrd->def->op));
13224 lrd = lrd->next;
13225 } while(lrd != range->defs);
13226
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013227 warning(state, range->defs->def, "classes: %x",
Eric Biedermanb138ac82003-04-22 18:44:01 +000013228 range->classes);
13229 for(i = 0; i < MAX_REGISTERS; i++) {
13230 if (used[i]) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013231 warning(state, range->defs->def, "used: %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000013232 arch_reg_str(i));
13233 }
13234 }
13235#if DEBUG_COLOR_GRAPH < 2
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013236 error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013237#else
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013238 internal_error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013239#endif
13240 }
13241 range->classes = arch_reg_regcm(state, range->color);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013242 if (range->color == -1) {
13243 internal_error(state, range->defs->def, "select_free_color did not?");
13244 }
13245 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013246}
13247
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013248static int color_graph(struct compile_state *state, struct reg_state *rstate)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013249{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013250 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013251 struct live_range_edge *edge;
13252 struct live_range *range;
13253 if (rstate->low) {
13254 cgdebug_printf("Lo: ");
13255 range = rstate->low;
13256 if (*range->group_prev != range) {
13257 internal_error(state, 0, "lo: *prev != range?");
13258 }
13259 *range->group_prev = range->group_next;
13260 if (range->group_next) {
13261 range->group_next->group_prev = range->group_prev;
13262 }
13263 if (&range->group_next == rstate->low_tail) {
13264 rstate->low_tail = range->group_prev;
13265 }
13266 if (rstate->low == range) {
13267 internal_error(state, 0, "low: next != prev?");
13268 }
13269 }
13270 else if (rstate->high) {
13271 cgdebug_printf("Hi: ");
13272 range = rstate->high;
13273 if (*range->group_prev != range) {
13274 internal_error(state, 0, "hi: *prev != range?");
13275 }
13276 *range->group_prev = range->group_next;
13277 if (range->group_next) {
13278 range->group_next->group_prev = range->group_prev;
13279 }
13280 if (&range->group_next == rstate->high_tail) {
13281 rstate->high_tail = range->group_prev;
13282 }
13283 if (rstate->high == range) {
13284 internal_error(state, 0, "high: next != prev?");
13285 }
13286 }
13287 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013288 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013289 }
13290 cgdebug_printf(" %d\n", range - rstate->lr);
13291 range->group_prev = 0;
13292 for(edge = range->edges; edge; edge = edge->next) {
13293 struct live_range *node;
13294 node = edge->node;
13295 /* Move nodes from the high to the low list */
13296 if (node->group_prev && (node->color == REG_UNSET) &&
13297 (node->degree == regc_max_size(state, node->classes))) {
13298 if (*node->group_prev != node) {
13299 internal_error(state, 0, "move: *prev != node?");
13300 }
13301 *node->group_prev = node->group_next;
13302 if (node->group_next) {
13303 node->group_next->group_prev = node->group_prev;
13304 }
13305 if (&node->group_next == rstate->high_tail) {
13306 rstate->high_tail = node->group_prev;
13307 }
13308 cgdebug_printf("Moving...%d to low\n", node - rstate->lr);
13309 node->group_prev = rstate->low_tail;
13310 node->group_next = 0;
13311 *rstate->low_tail = node;
13312 rstate->low_tail = &node->group_next;
13313 if (*node->group_prev != node) {
13314 internal_error(state, 0, "move2: *prev != node?");
13315 }
13316 }
13317 node->degree -= 1;
13318 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013319 colored = color_graph(state, rstate);
13320 if (colored) {
13321 cgdebug_printf("Coloring %d @%s:%d.%d:",
13322 range - rstate->lr,
13323 range->def->filename, range->def->line, range->def->col);
13324 cgdebug_flush();
13325 colored = select_free_color(state, rstate, range);
13326 cgdebug_printf(" %s\n", arch_reg_str(range->color));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013327 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013328 return colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013329}
13330
Eric Biedermana96d6a92003-05-13 20:45:19 +000013331static void verify_colors(struct compile_state *state, struct reg_state *rstate)
13332{
13333 struct live_range *lr;
13334 struct live_range_edge *edge;
13335 struct triple *ins, *first;
13336 char used[MAX_REGISTERS];
13337 first = RHS(state->main_function, 0);
13338 ins = first;
13339 do {
13340 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013341 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000013342 internal_error(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013343 "triple without a live range def");
Eric Biedermana96d6a92003-05-13 20:45:19 +000013344 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013345 lr = rstate->lrd[ins->id].lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000013346 if (lr->color == REG_UNSET) {
13347 internal_error(state, ins,
13348 "triple without a color");
13349 }
13350 /* Find the registers used by the edges */
13351 memset(used, 0, sizeof(used));
13352 for(edge = lr->edges; edge; edge = edge->next) {
13353 if (edge->node->color == REG_UNSET) {
13354 internal_error(state, 0,
13355 "live range without a color");
13356 }
13357 reg_fill_used(state, used, edge->node->color);
13358 }
13359 if (used[lr->color]) {
13360 internal_error(state, ins,
13361 "triple with already used color");
13362 }
13363 }
13364 ins = ins->next;
13365 } while(ins != first);
13366}
13367
Eric Biedermanb138ac82003-04-22 18:44:01 +000013368static void color_triples(struct compile_state *state, struct reg_state *rstate)
13369{
13370 struct live_range *lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000013371 struct triple *first, *ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013372 first = RHS(state->main_function, 0);
Eric Biedermana96d6a92003-05-13 20:45:19 +000013373 ins = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013374 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013375 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000013376 internal_error(state, ins,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013377 "triple without a live range");
13378 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013379 lr = rstate->lrd[ins->id].lr;
13380 SET_REG(ins->id, lr->color);
Eric Biedermana96d6a92003-05-13 20:45:19 +000013381 ins = ins->next;
13382 } while (ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013383}
13384
13385static void print_interference_block(
13386 struct compile_state *state, struct block *block, void *arg)
13387
13388{
13389 struct reg_state *rstate = arg;
13390 struct reg_block *rb;
13391 struct triple *ptr;
13392 int phi_present;
13393 int done;
13394 rb = &rstate->blocks[block->vertex];
13395
13396 printf("\nblock: %p (%d), %p<-%p %p<-%p\n",
13397 block,
13398 block->vertex,
13399 block->left,
13400 block->left && block->left->use?block->left->use->member : 0,
13401 block->right,
13402 block->right && block->right->use?block->right->use->member : 0);
13403 if (rb->in) {
13404 struct triple_reg_set *in_set;
13405 printf(" in:");
13406 for(in_set = rb->in; in_set; in_set = in_set->next) {
13407 printf(" %-10p", in_set->member);
13408 }
13409 printf("\n");
13410 }
13411 phi_present = 0;
13412 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13413 done = (ptr == block->last);
13414 if (ptr->op == OP_PHI) {
13415 phi_present = 1;
13416 break;
13417 }
13418 }
13419 if (phi_present) {
13420 int edge;
13421 for(edge = 0; edge < block->users; edge++) {
13422 printf(" in(%d):", edge);
13423 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13424 struct triple **slot;
13425 done = (ptr == block->last);
13426 if (ptr->op != OP_PHI) {
13427 continue;
13428 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000013429 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013430 printf(" %-10p", slot[edge]);
13431 }
13432 printf("\n");
13433 }
13434 }
13435 if (block->first->op == OP_LABEL) {
13436 printf("%p:\n", block->first);
13437 }
13438 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13439 struct triple_set *user;
13440 struct live_range *lr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013441 unsigned id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013442 int op;
13443 op = ptr->op;
13444 done = (ptr == block->last);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013445 lr = rstate->lrd[ptr->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013446
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013447 if (triple_stores_block(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013448 if (ptr->u.block != block) {
13449 internal_error(state, ptr,
13450 "Wrong block pointer: %p",
13451 ptr->u.block);
13452 }
13453 }
13454 if (op == OP_ADECL) {
13455 for(user = ptr->use; user; user = user->next) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013456 if (!user->member->u.block) {
13457 internal_error(state, user->member,
13458 "Use %p not in a block?",
13459 user->member);
13460 }
13461
13462 }
13463 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000013464 id = ptr->id;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013465 ptr->id = rstate->lrd[id].orig_id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013466 SET_REG(ptr->id, lr->color);
Eric Biederman0babc1c2003-05-09 02:39:00 +000013467 display_triple(stdout, ptr);
13468 ptr->id = id;
13469
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013470 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
13471 internal_error(state, ptr, "lr has no defs!");
13472 }
13473
13474 if (lr->defs) {
13475 struct live_range_def *lrd;
13476 printf(" range:");
13477 lrd = lr->defs;
13478 do {
13479 printf(" %-10p", lrd->def);
13480 lrd = lrd->next;
13481 } while(lrd != lr->defs);
13482 printf("\n");
13483 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013484 if (lr->edges > 0) {
13485 struct live_range_edge *edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013486 printf(" edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013487 for(edge = lr->edges; edge; edge = edge->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013488 struct live_range_def *lrd;
13489 lrd = edge->node->defs;
13490 do {
13491 printf(" %-10p", lrd->def);
13492 lrd = lrd->next;
13493 } while(lrd != edge->node->defs);
13494 printf("|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013495 }
13496 printf("\n");
13497 }
13498 /* Do a bunch of sanity checks */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013499 valid_ins(state, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013500 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013501 internal_error(state, ptr, "Invalid triple id: %d",
13502 ptr->id);
13503 }
13504 for(user = ptr->use; user; user = user->next) {
13505 struct triple *use;
13506 struct live_range *ulr;
13507 use = user->member;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013508 valid_ins(state, use);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013509 if ((use->id < 0) || (use->id > rstate->defs)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013510 internal_error(state, use, "Invalid triple id: %d",
13511 use->id);
13512 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013513 ulr = rstate->lrd[user->member->id].lr;
13514 if (triple_stores_block(state, user->member) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000013515 !user->member->u.block) {
13516 internal_error(state, user->member,
13517 "Use %p not in a block?",
13518 user->member);
13519 }
13520 }
13521 }
13522 if (rb->out) {
13523 struct triple_reg_set *out_set;
13524 printf(" out:");
13525 for(out_set = rb->out; out_set; out_set = out_set->next) {
13526 printf(" %-10p", out_set->member);
13527 }
13528 printf("\n");
13529 }
13530 printf("\n");
13531}
13532
13533static struct live_range *merge_sort_lr(
13534 struct live_range *first, struct live_range *last)
13535{
13536 struct live_range *mid, *join, **join_tail, *pick;
13537 size_t size;
13538 size = (last - first) + 1;
13539 if (size >= 2) {
13540 mid = first + size/2;
13541 first = merge_sort_lr(first, mid -1);
13542 mid = merge_sort_lr(mid, last);
13543
13544 join = 0;
13545 join_tail = &join;
13546 /* merge the two lists */
13547 while(first && mid) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013548 if ((first->degree < mid->degree) ||
13549 ((first->degree == mid->degree) &&
13550 (first->length < mid->length))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013551 pick = first;
13552 first = first->group_next;
13553 if (first) {
13554 first->group_prev = 0;
13555 }
13556 }
13557 else {
13558 pick = mid;
13559 mid = mid->group_next;
13560 if (mid) {
13561 mid->group_prev = 0;
13562 }
13563 }
13564 pick->group_next = 0;
13565 pick->group_prev = join_tail;
13566 *join_tail = pick;
13567 join_tail = &pick->group_next;
13568 }
13569 /* Splice the remaining list */
13570 pick = (first)? first : mid;
13571 *join_tail = pick;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013572 if (pick) {
13573 pick->group_prev = join_tail;
13574 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013575 }
13576 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013577 if (!first->defs) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013578 first = 0;
13579 }
13580 join = first;
13581 }
13582 return join;
13583}
13584
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013585static void ids_from_rstate(struct compile_state *state,
13586 struct reg_state *rstate)
13587{
13588 struct triple *ins, *first;
13589 if (!rstate->defs) {
13590 return;
13591 }
13592 /* Display the graph if desired */
13593 if (state->debug & DEBUG_INTERFERENCE) {
13594 print_blocks(state, stdout);
13595 print_control_flow(state);
13596 }
13597 first = RHS(state->main_function, 0);
13598 ins = first;
13599 do {
13600 if (ins->id) {
13601 struct live_range_def *lrd;
13602 lrd = &rstate->lrd[ins->id];
13603 ins->id = lrd->orig_id;
13604 }
13605 ins = ins->next;
13606 } while(ins != first);
13607}
13608
13609static void cleanup_live_edges(struct reg_state *rstate)
13610{
13611 int i;
13612 /* Free the edges on each node */
13613 for(i = 1; i <= rstate->ranges; i++) {
13614 remove_live_edges(rstate, &rstate->lr[i]);
13615 }
13616}
13617
13618static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
13619{
13620 cleanup_live_edges(rstate);
13621 xfree(rstate->lrd);
13622 xfree(rstate->lr);
13623
13624 /* Free the variable lifetime information */
13625 if (rstate->blocks) {
13626 free_variable_lifetimes(state, rstate->blocks);
13627 }
13628 rstate->defs = 0;
13629 rstate->ranges = 0;
13630 rstate->lrd = 0;
13631 rstate->lr = 0;
13632 rstate->blocks = 0;
13633}
13634
Eric Biederman153ea352003-06-20 14:43:20 +000013635static void verify_consistency(struct compile_state *state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013636static void allocate_registers(struct compile_state *state)
13637{
13638 struct reg_state rstate;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013639 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013640
13641 /* Clear out the reg_state */
13642 memset(&rstate, 0, sizeof(rstate));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013643 rstate.max_passes = MAX_ALLOCATION_PASSES;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013644
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013645 do {
13646 struct live_range **point, **next;
Eric Biederman153ea352003-06-20 14:43:20 +000013647 int tangles;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013648 int coalesced;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013649
Eric Biederman153ea352003-06-20 14:43:20 +000013650#if 0
13651 fprintf(stderr, "pass: %d\n", rstate.passes);
13652#endif
13653
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013654 /* Restore ids */
13655 ids_from_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013656
Eric Biedermanf96a8102003-06-16 16:57:34 +000013657 /* Cleanup the temporary data structures */
13658 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013659
Eric Biedermanf96a8102003-06-16 16:57:34 +000013660 /* Compute the variable lifetimes */
13661 rstate.blocks = compute_variable_lifetimes(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013662
Eric Biedermanf96a8102003-06-16 16:57:34 +000013663 /* Fix invalid mandatory live range coalesce conflicts */
13664 walk_variable_lifetimes(
13665 state, rstate.blocks, fix_coalesce_conflicts, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013666
Eric Biederman153ea352003-06-20 14:43:20 +000013667 /* Fix two simultaneous uses of the same register.
13668 * In a few pathlogical cases a partial untangle moves
13669 * the tangle to a part of the graph we won't revisit.
13670 * So we keep looping until we have no more tangle fixes
13671 * to apply.
13672 */
13673 do {
13674 tangles = correct_tangles(state, rstate.blocks);
13675 } while(tangles);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013676
13677 if (state->debug & DEBUG_INSERTED_COPIES) {
13678 printf("After resolve_tangles\n");
13679 print_blocks(state, stdout);
13680 print_control_flow(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013681 }
Eric Biederman153ea352003-06-20 14:43:20 +000013682 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013683
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013684 /* Allocate and initialize the live ranges */
13685 initialize_live_ranges(state, &rstate);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013686
Eric Biederman153ea352003-06-20 14:43:20 +000013687 /* Note current doing coalescing in a loop appears to
13688 * buys me nothing. The code is left this way in case
13689 * there is some value in it. Or if a future bugfix
13690 * yields some benefit.
13691 */
13692 do {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013693#if 0
13694 fprintf(stderr, "coalescing\n");
13695#endif
Eric Biederman153ea352003-06-20 14:43:20 +000013696 /* Remove any previous live edge calculations */
13697 cleanup_live_edges(&rstate);
13698
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013699 /* Compute the interference graph */
13700 walk_variable_lifetimes(
13701 state, rstate.blocks, graph_ins, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000013702
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013703 /* Display the interference graph if desired */
13704 if (state->debug & DEBUG_INTERFERENCE) {
13705 printf("\nlive variables by block\n");
13706 walk_blocks(state, print_interference_block, &rstate);
13707 printf("\nlive variables by instruction\n");
13708 walk_variable_lifetimes(
13709 state, rstate.blocks,
13710 print_interference_ins, &rstate);
13711 }
13712
13713 coalesced = coalesce_live_ranges(state, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000013714
13715#if 0
13716 fprintf(stderr, "coalesced: %d\n", coalesced);
13717#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013718 } while(coalesced);
Eric Biederman153ea352003-06-20 14:43:20 +000013719
13720#if DEBUG_CONSISTENCY > 1
13721# if 0
13722 fprintf(stderr, "verify_graph_ins...\n");
13723# endif
13724 /* Verify the interference graph */
13725 walk_variable_lifetimes(
13726 state, rstate.blocks, verify_graph_ins, &rstate);
13727# if 0
13728 fprintf(stderr, "verify_graph_ins done\n");
13729#endif
13730#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013731
13732 /* Build the groups low and high. But with the nodes
13733 * first sorted by degree order.
13734 */
13735 rstate.low_tail = &rstate.low;
13736 rstate.high_tail = &rstate.high;
13737 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
13738 if (rstate.high) {
13739 rstate.high->group_prev = &rstate.high;
13740 }
13741 for(point = &rstate.high; *point; point = &(*point)->group_next)
13742 ;
13743 rstate.high_tail = point;
13744 /* Walk through the high list and move everything that needs
13745 * to be onto low.
13746 */
13747 for(point = &rstate.high; *point; point = next) {
13748 struct live_range *range;
13749 next = &(*point)->group_next;
13750 range = *point;
13751
13752 /* If it has a low degree or it already has a color
13753 * place the node in low.
13754 */
13755 if ((range->degree < regc_max_size(state, range->classes)) ||
13756 (range->color != REG_UNSET)) {
13757 cgdebug_printf("Lo: %5d degree %5d%s\n",
13758 range - rstate.lr, range->degree,
13759 (range->color != REG_UNSET) ? " (colored)": "");
13760 *range->group_prev = range->group_next;
13761 if (range->group_next) {
13762 range->group_next->group_prev = range->group_prev;
13763 }
13764 if (&range->group_next == rstate.high_tail) {
13765 rstate.high_tail = range->group_prev;
13766 }
13767 range->group_prev = rstate.low_tail;
13768 range->group_next = 0;
13769 *rstate.low_tail = range;
13770 rstate.low_tail = &range->group_next;
13771 next = point;
13772 }
13773 else {
13774 cgdebug_printf("hi: %5d degree %5d%s\n",
13775 range - rstate.lr, range->degree,
13776 (range->color != REG_UNSET) ? " (colored)": "");
13777 }
13778 }
13779 /* Color the live_ranges */
13780 colored = color_graph(state, &rstate);
13781 rstate.passes++;
13782 } while (!colored);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013783
Eric Biedermana96d6a92003-05-13 20:45:19 +000013784 /* Verify the graph was properly colored */
13785 verify_colors(state, &rstate);
13786
Eric Biedermanb138ac82003-04-22 18:44:01 +000013787 /* Move the colors from the graph to the triples */
13788 color_triples(state, &rstate);
13789
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013790 /* Cleanup the temporary data structures */
13791 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013792}
13793
13794/* Sparce Conditional Constant Propogation
13795 * =========================================
13796 */
13797struct ssa_edge;
13798struct flow_block;
13799struct lattice_node {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013800 unsigned old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013801 struct triple *def;
13802 struct ssa_edge *out;
13803 struct flow_block *fblock;
13804 struct triple *val;
13805 /* lattice high val && !is_const(val)
13806 * lattice const is_const(val)
13807 * lattice low val == 0
13808 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000013809};
13810struct ssa_edge {
13811 struct lattice_node *src;
13812 struct lattice_node *dst;
13813 struct ssa_edge *work_next;
13814 struct ssa_edge *work_prev;
13815 struct ssa_edge *out_next;
13816};
13817struct flow_edge {
13818 struct flow_block *src;
13819 struct flow_block *dst;
13820 struct flow_edge *work_next;
13821 struct flow_edge *work_prev;
13822 struct flow_edge *in_next;
13823 struct flow_edge *out_next;
13824 int executable;
13825};
13826struct flow_block {
13827 struct block *block;
13828 struct flow_edge *in;
13829 struct flow_edge *out;
13830 struct flow_edge left, right;
13831};
13832
13833struct scc_state {
Eric Biederman0babc1c2003-05-09 02:39:00 +000013834 int ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013835 struct lattice_node *lattice;
13836 struct ssa_edge *ssa_edges;
13837 struct flow_block *flow_blocks;
13838 struct flow_edge *flow_work_list;
13839 struct ssa_edge *ssa_work_list;
13840};
13841
13842
13843static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
13844 struct flow_edge *fedge)
13845{
13846 if (!scc->flow_work_list) {
13847 scc->flow_work_list = fedge;
13848 fedge->work_next = fedge->work_prev = fedge;
13849 }
13850 else {
13851 struct flow_edge *ftail;
13852 ftail = scc->flow_work_list->work_prev;
13853 fedge->work_next = ftail->work_next;
13854 fedge->work_prev = ftail;
13855 fedge->work_next->work_prev = fedge;
13856 fedge->work_prev->work_next = fedge;
13857 }
13858}
13859
13860static struct flow_edge *scc_next_fedge(
13861 struct compile_state *state, struct scc_state *scc)
13862{
13863 struct flow_edge *fedge;
13864 fedge = scc->flow_work_list;
13865 if (fedge) {
13866 fedge->work_next->work_prev = fedge->work_prev;
13867 fedge->work_prev->work_next = fedge->work_next;
13868 if (fedge->work_next != fedge) {
13869 scc->flow_work_list = fedge->work_next;
13870 } else {
13871 scc->flow_work_list = 0;
13872 }
13873 }
13874 return fedge;
13875}
13876
13877static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
13878 struct ssa_edge *sedge)
13879{
13880 if (!scc->ssa_work_list) {
13881 scc->ssa_work_list = sedge;
13882 sedge->work_next = sedge->work_prev = sedge;
13883 }
13884 else {
13885 struct ssa_edge *stail;
13886 stail = scc->ssa_work_list->work_prev;
13887 sedge->work_next = stail->work_next;
13888 sedge->work_prev = stail;
13889 sedge->work_next->work_prev = sedge;
13890 sedge->work_prev->work_next = sedge;
13891 }
13892}
13893
13894static struct ssa_edge *scc_next_sedge(
13895 struct compile_state *state, struct scc_state *scc)
13896{
13897 struct ssa_edge *sedge;
13898 sedge = scc->ssa_work_list;
13899 if (sedge) {
13900 sedge->work_next->work_prev = sedge->work_prev;
13901 sedge->work_prev->work_next = sedge->work_next;
13902 if (sedge->work_next != sedge) {
13903 scc->ssa_work_list = sedge->work_next;
13904 } else {
13905 scc->ssa_work_list = 0;
13906 }
13907 }
13908 return sedge;
13909}
13910
13911static void initialize_scc_state(
13912 struct compile_state *state, struct scc_state *scc)
13913{
13914 int ins_count, ssa_edge_count;
13915 int ins_index, ssa_edge_index, fblock_index;
13916 struct triple *first, *ins;
13917 struct block *block;
13918 struct flow_block *fblock;
13919
13920 memset(scc, 0, sizeof(*scc));
13921
13922 /* Inialize pass zero find out how much memory we need */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013923 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013924 ins = first;
13925 ins_count = ssa_edge_count = 0;
13926 do {
13927 struct triple_set *edge;
13928 ins_count += 1;
13929 for(edge = ins->use; edge; edge = edge->next) {
13930 ssa_edge_count++;
13931 }
13932 ins = ins->next;
13933 } while(ins != first);
13934#if DEBUG_SCC
13935 fprintf(stderr, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
13936 ins_count, ssa_edge_count, state->last_vertex);
13937#endif
Eric Biederman0babc1c2003-05-09 02:39:00 +000013938 scc->ins_count = ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013939 scc->lattice =
13940 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
13941 scc->ssa_edges =
13942 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
13943 scc->flow_blocks =
13944 xcmalloc(sizeof(*scc->flow_blocks)*(state->last_vertex + 1),
13945 "flow_blocks");
13946
13947 /* Initialize pass one collect up the nodes */
13948 fblock = 0;
13949 block = 0;
13950 ins_index = ssa_edge_index = fblock_index = 0;
13951 ins = first;
13952 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013953 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
13954 block = ins->u.block;
13955 if (!block) {
13956 internal_error(state, ins, "label without block");
13957 }
13958 fblock_index += 1;
13959 block->vertex = fblock_index;
13960 fblock = &scc->flow_blocks[fblock_index];
13961 fblock->block = block;
13962 }
13963 {
13964 struct lattice_node *lnode;
13965 ins_index += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013966 lnode = &scc->lattice[ins_index];
13967 lnode->def = ins;
13968 lnode->out = 0;
13969 lnode->fblock = fblock;
13970 lnode->val = ins; /* LATTICE HIGH */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013971 lnode->old_id = ins->id;
13972 ins->id = ins_index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013973 }
13974 ins = ins->next;
13975 } while(ins != first);
13976 /* Initialize pass two collect up the edges */
13977 block = 0;
13978 fblock = 0;
13979 ins = first;
13980 do {
13981 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
13982 struct flow_edge *fedge, **ftail;
13983 struct block_set *bedge;
13984 block = ins->u.block;
13985 fblock = &scc->flow_blocks[block->vertex];
13986 fblock->in = 0;
13987 fblock->out = 0;
13988 ftail = &fblock->out;
13989 if (block->left) {
13990 fblock->left.dst = &scc->flow_blocks[block->left->vertex];
13991 if (fblock->left.dst->block != block->left) {
13992 internal_error(state, 0, "block mismatch");
13993 }
13994 fblock->left.out_next = 0;
13995 *ftail = &fblock->left;
13996 ftail = &fblock->left.out_next;
13997 }
13998 if (block->right) {
13999 fblock->right.dst = &scc->flow_blocks[block->right->vertex];
14000 if (fblock->right.dst->block != block->right) {
14001 internal_error(state, 0, "block mismatch");
14002 }
14003 fblock->right.out_next = 0;
14004 *ftail = &fblock->right;
14005 ftail = &fblock->right.out_next;
14006 }
14007 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
14008 fedge->src = fblock;
14009 fedge->work_next = fedge->work_prev = fedge;
14010 fedge->executable = 0;
14011 }
14012 ftail = &fblock->in;
14013 for(bedge = block->use; bedge; bedge = bedge->next) {
14014 struct block *src_block;
14015 struct flow_block *sfblock;
14016 struct flow_edge *sfedge;
14017 src_block = bedge->member;
14018 sfblock = &scc->flow_blocks[src_block->vertex];
14019 sfedge = 0;
14020 if (src_block->left == block) {
14021 sfedge = &sfblock->left;
14022 } else {
14023 sfedge = &sfblock->right;
14024 }
14025 *ftail = sfedge;
14026 ftail = &sfedge->in_next;
14027 sfedge->in_next = 0;
14028 }
14029 }
14030 {
14031 struct triple_set *edge;
14032 struct ssa_edge **stail;
14033 struct lattice_node *lnode;
14034 lnode = &scc->lattice[ins->id];
14035 lnode->out = 0;
14036 stail = &lnode->out;
14037 for(edge = ins->use; edge; edge = edge->next) {
14038 struct ssa_edge *sedge;
14039 ssa_edge_index += 1;
14040 sedge = &scc->ssa_edges[ssa_edge_index];
14041 *stail = sedge;
14042 stail = &sedge->out_next;
14043 sedge->src = lnode;
14044 sedge->dst = &scc->lattice[edge->member->id];
14045 sedge->work_next = sedge->work_prev = sedge;
14046 sedge->out_next = 0;
14047 }
14048 }
14049 ins = ins->next;
14050 } while(ins != first);
14051 /* Setup a dummy block 0 as a node above the start node */
14052 {
14053 struct flow_block *fblock, *dst;
14054 struct flow_edge *fedge;
14055 fblock = &scc->flow_blocks[0];
14056 fblock->block = 0;
14057 fblock->in = 0;
14058 fblock->out = &fblock->left;
14059 dst = &scc->flow_blocks[state->first_block->vertex];
14060 fedge = &fblock->left;
14061 fedge->src = fblock;
14062 fedge->dst = dst;
14063 fedge->work_next = fedge;
14064 fedge->work_prev = fedge;
14065 fedge->in_next = fedge->dst->in;
14066 fedge->out_next = 0;
14067 fedge->executable = 0;
14068 fedge->dst->in = fedge;
14069
14070 /* Initialize the work lists */
14071 scc->flow_work_list = 0;
14072 scc->ssa_work_list = 0;
14073 scc_add_fedge(state, scc, fedge);
14074 }
14075#if DEBUG_SCC
14076 fprintf(stderr, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
14077 ins_index, ssa_edge_index, fblock_index);
14078#endif
14079}
14080
14081
14082static void free_scc_state(
14083 struct compile_state *state, struct scc_state *scc)
14084{
14085 xfree(scc->flow_blocks);
14086 xfree(scc->ssa_edges);
14087 xfree(scc->lattice);
Eric Biederman0babc1c2003-05-09 02:39:00 +000014088
Eric Biedermanb138ac82003-04-22 18:44:01 +000014089}
14090
14091static struct lattice_node *triple_to_lattice(
14092 struct compile_state *state, struct scc_state *scc, struct triple *ins)
14093{
14094 if (ins->id <= 0) {
14095 internal_error(state, ins, "bad id");
14096 }
14097 return &scc->lattice[ins->id];
14098}
14099
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014100static struct triple *preserve_lval(
14101 struct compile_state *state, struct lattice_node *lnode)
14102{
14103 struct triple *old;
14104 /* Preserve the original value */
14105 if (lnode->val) {
14106 old = dup_triple(state, lnode->val);
14107 if (lnode->val != lnode->def) {
14108 xfree(lnode->val);
14109 }
14110 lnode->val = 0;
14111 } else {
14112 old = 0;
14113 }
14114 return old;
14115}
14116
14117static int lval_changed(struct compile_state *state,
14118 struct triple *old, struct lattice_node *lnode)
14119{
14120 int changed;
14121 /* See if the lattice value has changed */
14122 changed = 1;
14123 if (!old && !lnode->val) {
14124 changed = 0;
14125 }
14126 if (changed && lnode->val && !is_const(lnode->val)) {
14127 changed = 0;
14128 }
14129 if (changed &&
14130 lnode->val && old &&
14131 (memcmp(lnode->val->param, old->param,
14132 TRIPLE_SIZE(lnode->val->sizes) * sizeof(lnode->val->param[0])) == 0) &&
14133 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
14134 changed = 0;
14135 }
14136 if (old) {
14137 xfree(old);
14138 }
14139 return changed;
14140
14141}
14142
Eric Biedermanb138ac82003-04-22 18:44:01 +000014143static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
14144 struct lattice_node *lnode)
14145{
14146 struct lattice_node *tmp;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014147 struct triple **slot, *old;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014148 struct flow_edge *fedge;
14149 int index;
14150 if (lnode->def->op != OP_PHI) {
14151 internal_error(state, lnode->def, "not phi");
14152 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014153 /* Store the original value */
14154 old = preserve_lval(state, lnode);
14155
Eric Biedermanb138ac82003-04-22 18:44:01 +000014156 /* default to lattice high */
14157 lnode->val = lnode->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014158 slot = &RHS(lnode->def, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014159 index = 0;
14160 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
14161 if (!fedge->executable) {
14162 continue;
14163 }
14164 if (!slot[index]) {
14165 internal_error(state, lnode->def, "no phi value");
14166 }
14167 tmp = triple_to_lattice(state, scc, slot[index]);
14168 /* meet(X, lattice low) = lattice low */
14169 if (!tmp->val) {
14170 lnode->val = 0;
14171 }
14172 /* meet(X, lattice high) = X */
14173 else if (!tmp->val) {
14174 lnode->val = lnode->val;
14175 }
14176 /* meet(lattice high, X) = X */
14177 else if (!is_const(lnode->val)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014178 lnode->val = dup_triple(state, tmp->val);
14179 lnode->val->type = lnode->def->type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014180 }
14181 /* meet(const, const) = const or lattice low */
14182 else if (!constants_equal(state, lnode->val, tmp->val)) {
14183 lnode->val = 0;
14184 }
14185 if (!lnode->val) {
14186 break;
14187 }
14188 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014189#if DEBUG_SCC
14190 fprintf(stderr, "phi: %d -> %s\n",
14191 lnode->def->id,
14192 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14193#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014194 /* If the lattice value has changed update the work lists. */
14195 if (lval_changed(state, old, lnode)) {
14196 struct ssa_edge *sedge;
14197 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14198 scc_add_sedge(state, scc, sedge);
14199 }
14200 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014201}
14202
14203static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
14204 struct lattice_node *lnode)
14205{
14206 int changed;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014207 struct triple *old, *scratch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014208 struct triple **dexpr, **vexpr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014209 int count, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014210
14211 /* Store the original value */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014212 old = preserve_lval(state, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000014213
Eric Biedermanb138ac82003-04-22 18:44:01 +000014214 /* Reinitialize the value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000014215 lnode->val = scratch = dup_triple(state, lnode->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014216 scratch->id = lnode->old_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014217 scratch->next = scratch;
14218 scratch->prev = scratch;
14219 scratch->use = 0;
14220
14221 count = TRIPLE_SIZE(scratch->sizes);
14222 for(i = 0; i < count; i++) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000014223 dexpr = &lnode->def->param[i];
14224 vexpr = &scratch->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014225 *vexpr = *dexpr;
14226 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14227 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14228 *dexpr) {
14229 struct lattice_node *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014230 tmp = triple_to_lattice(state, scc, *dexpr);
14231 *vexpr = (tmp->val)? tmp->val : tmp->def;
14232 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014233 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014234 if (scratch->op == OP_BRANCH) {
14235 scratch->next = lnode->def->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014236 }
14237 /* Recompute the value */
14238#warning "FIXME see if simplify does anything bad"
14239 /* So far it looks like only the strength reduction
14240 * optimization are things I need to worry about.
14241 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000014242 simplify(state, scratch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014243 /* Cleanup my value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000014244 if (scratch->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014245 internal_error(state, lnode->def, "scratch used?");
14246 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014247 if ((scratch->prev != scratch) ||
14248 ((scratch->next != scratch) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000014249 ((lnode->def->op != OP_BRANCH) ||
Eric Biederman0babc1c2003-05-09 02:39:00 +000014250 (scratch->next != lnode->def->next)))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014251 internal_error(state, lnode->def, "scratch in list?");
14252 }
14253 /* undo any uses... */
Eric Biederman0babc1c2003-05-09 02:39:00 +000014254 count = TRIPLE_SIZE(scratch->sizes);
14255 for(i = 0; i < count; i++) {
14256 vexpr = &scratch->param[i];
14257 if (*vexpr) {
14258 unuse_triple(*vexpr, scratch);
14259 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014260 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014261 if (!is_const(scratch)) {
14262 for(i = 0; i < count; i++) {
14263 dexpr = &lnode->def->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014264 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14265 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14266 *dexpr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000014267 struct lattice_node *tmp;
14268 tmp = triple_to_lattice(state, scc, *dexpr);
14269 if (!tmp->val) {
14270 lnode->val = 0;
14271 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014272 }
14273 }
14274 }
14275 if (lnode->val &&
14276 (lnode->val->op == lnode->def->op) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000014277 (memcmp(lnode->val->param, lnode->def->param,
14278 count * sizeof(lnode->val->param[0])) == 0) &&
14279 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014280 lnode->val = lnode->def;
14281 }
14282 /* Find the cases that are always lattice lo */
14283 if (lnode->val &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000014284 triple_is_def(state, lnode->val) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000014285 !triple_is_pure(state, lnode->val)) {
14286 lnode->val = 0;
14287 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014288 if (lnode->val &&
14289 (lnode->val->op == OP_SDECL) &&
14290 (lnode->val != lnode->def)) {
14291 internal_error(state, lnode->def, "bad sdecl");
14292 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014293 /* See if the lattice value has changed */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014294 changed = lval_changed(state, old, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000014295 if (lnode->val != scratch) {
14296 xfree(scratch);
14297 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014298 return changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014299}
Eric Biederman0babc1c2003-05-09 02:39:00 +000014300
Eric Biedermanb138ac82003-04-22 18:44:01 +000014301static void scc_visit_branch(struct compile_state *state, struct scc_state *scc,
14302 struct lattice_node *lnode)
14303{
14304 struct lattice_node *cond;
14305#if DEBUG_SCC
14306 {
14307 struct flow_edge *fedge;
14308 fprintf(stderr, "branch: %d (",
14309 lnode->def->id);
14310
14311 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
14312 fprintf(stderr, " %d", fedge->dst->block->vertex);
14313 }
14314 fprintf(stderr, " )");
Eric Biederman0babc1c2003-05-09 02:39:00 +000014315 if (TRIPLE_RHS(lnode->def->sizes) > 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014316 fprintf(stderr, " <- %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014317 RHS(lnode->def, 0)->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014318 }
14319 fprintf(stderr, "\n");
14320 }
14321#endif
14322 if (lnode->def->op != OP_BRANCH) {
14323 internal_error(state, lnode->def, "not branch");
14324 }
14325 /* This only applies to conditional branches */
Eric Biederman0babc1c2003-05-09 02:39:00 +000014326 if (TRIPLE_RHS(lnode->def->sizes) == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014327 return;
14328 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014329 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000014330 if (cond->val && !is_const(cond->val)) {
14331#warning "FIXME do I need to do something here?"
14332 warning(state, cond->def, "condition not constant?");
14333 return;
14334 }
14335 if (cond->val == 0) {
14336 scc_add_fedge(state, scc, cond->fblock->out);
14337 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14338 }
14339 else if (cond->val->u.cval) {
14340 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14341
14342 } else {
14343 scc_add_fedge(state, scc, cond->fblock->out);
14344 }
14345
14346}
14347
14348static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
14349 struct lattice_node *lnode)
14350{
14351 int changed;
14352
14353 changed = compute_lnode_val(state, scc, lnode);
14354#if DEBUG_SCC
14355 {
14356 struct triple **expr;
14357 fprintf(stderr, "expr: %3d %10s (",
14358 lnode->def->id, tops(lnode->def->op));
14359 expr = triple_rhs(state, lnode->def, 0);
14360 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000014361 if (*expr) {
14362 fprintf(stderr, " %d", (*expr)->id);
14363 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014364 }
14365 fprintf(stderr, " ) -> %s\n",
14366 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14367 }
14368#endif
14369 if (lnode->def->op == OP_BRANCH) {
14370 scc_visit_branch(state, scc, lnode);
14371
14372 }
14373 else if (changed) {
14374 struct ssa_edge *sedge;
14375 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14376 scc_add_sedge(state, scc, sedge);
14377 }
14378 }
14379}
14380
14381static void scc_writeback_values(
14382 struct compile_state *state, struct scc_state *scc)
14383{
14384 struct triple *first, *ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014385 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014386 ins = first;
14387 do {
14388 struct lattice_node *lnode;
14389 lnode = triple_to_lattice(state, scc, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014390 /* Restore id */
14391 ins->id = lnode->old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014392#if DEBUG_SCC
14393 if (lnode->val && !is_const(lnode->val)) {
14394 warning(state, lnode->def,
14395 "lattice node still high?");
14396 }
14397#endif
14398 if (lnode->val && (lnode->val != ins)) {
14399 /* See if it something I know how to write back */
14400 switch(lnode->val->op) {
14401 case OP_INTCONST:
14402 mkconst(state, ins, lnode->val->u.cval);
14403 break;
14404 case OP_ADDRCONST:
14405 mkaddr_const(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014406 MISC(lnode->val, 0), lnode->val->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014407 break;
14408 default:
14409 /* By default don't copy the changes,
14410 * recompute them in place instead.
14411 */
14412 simplify(state, ins);
14413 break;
14414 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014415 if (is_const(lnode->val) &&
14416 !constants_equal(state, lnode->val, ins)) {
14417 internal_error(state, 0, "constants not equal");
14418 }
14419 /* Free the lattice nodes */
14420 xfree(lnode->val);
14421 lnode->val = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014422 }
14423 ins = ins->next;
14424 } while(ins != first);
14425}
14426
14427static void scc_transform(struct compile_state *state)
14428{
14429 struct scc_state scc;
14430
14431 initialize_scc_state(state, &scc);
14432
14433 while(scc.flow_work_list || scc.ssa_work_list) {
14434 struct flow_edge *fedge;
14435 struct ssa_edge *sedge;
14436 struct flow_edge *fptr;
14437 while((fedge = scc_next_fedge(state, &scc))) {
14438 struct block *block;
14439 struct triple *ptr;
14440 struct flow_block *fblock;
14441 int time;
14442 int done;
14443 if (fedge->executable) {
14444 continue;
14445 }
14446 if (!fedge->dst) {
14447 internal_error(state, 0, "fedge without dst");
14448 }
14449 if (!fedge->src) {
14450 internal_error(state, 0, "fedge without src");
14451 }
14452 fedge->executable = 1;
14453 fblock = fedge->dst;
14454 block = fblock->block;
14455 time = 0;
14456 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14457 if (fptr->executable) {
14458 time++;
14459 }
14460 }
14461#if DEBUG_SCC
14462 fprintf(stderr, "vertex: %d time: %d\n",
14463 block->vertex, time);
14464
14465#endif
14466 done = 0;
14467 for(ptr = block->first; !done; ptr = ptr->next) {
14468 struct lattice_node *lnode;
14469 done = (ptr == block->last);
14470 lnode = &scc.lattice[ptr->id];
14471 if (ptr->op == OP_PHI) {
14472 scc_visit_phi(state, &scc, lnode);
14473 }
14474 else if (time == 1) {
14475 scc_visit_expr(state, &scc, lnode);
14476 }
14477 }
14478 if (fblock->out && !fblock->out->out_next) {
14479 scc_add_fedge(state, &scc, fblock->out);
14480 }
14481 }
14482 while((sedge = scc_next_sedge(state, &scc))) {
14483 struct lattice_node *lnode;
14484 struct flow_block *fblock;
14485 lnode = sedge->dst;
14486 fblock = lnode->fblock;
14487#if DEBUG_SCC
14488 fprintf(stderr, "sedge: %5d (%5d -> %5d)\n",
14489 sedge - scc.ssa_edges,
14490 sedge->src->def->id,
14491 sedge->dst->def->id);
14492#endif
14493 if (lnode->def->op == OP_PHI) {
14494 scc_visit_phi(state, &scc, lnode);
14495 }
14496 else {
14497 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14498 if (fptr->executable) {
14499 break;
14500 }
14501 }
14502 if (fptr) {
14503 scc_visit_expr(state, &scc, lnode);
14504 }
14505 }
14506 }
14507 }
14508
14509 scc_writeback_values(state, &scc);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014510 free_scc_state(state, &scc);
14511}
14512
14513
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014514static void transform_to_arch_instructions(struct compile_state *state)
14515{
14516 struct triple *ins, *first;
14517 first = RHS(state->main_function, 0);
14518 ins = first;
14519 do {
14520 ins = transform_to_arch_instruction(state, ins);
14521 } while(ins != first);
14522}
Eric Biedermanb138ac82003-04-22 18:44:01 +000014523
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014524#if DEBUG_CONSISTENCY
14525static void verify_uses(struct compile_state *state)
14526{
14527 struct triple *first, *ins;
14528 struct triple_set *set;
14529 first = RHS(state->main_function, 0);
14530 ins = first;
14531 do {
14532 struct triple **expr;
14533 expr = triple_rhs(state, ins, 0);
14534 for(; expr; expr = triple_rhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000014535 struct triple *rhs;
14536 rhs = *expr;
14537 for(set = rhs?rhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014538 if (set->member == ins) {
14539 break;
14540 }
14541 }
14542 if (!set) {
14543 internal_error(state, ins, "rhs not used");
14544 }
14545 }
14546 expr = triple_lhs(state, ins, 0);
14547 for(; expr; expr = triple_lhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000014548 struct triple *lhs;
14549 lhs = *expr;
14550 for(set = lhs?lhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014551 if (set->member == ins) {
14552 break;
14553 }
14554 }
14555 if (!set) {
14556 internal_error(state, ins, "lhs not used");
14557 }
14558 }
14559 ins = ins->next;
14560 } while(ins != first);
14561
14562}
14563static void verify_blocks(struct compile_state *state)
14564{
14565 struct triple *ins;
14566 struct block *block;
14567 block = state->first_block;
14568 if (!block) {
14569 return;
14570 }
14571 do {
14572 for(ins = block->first; ins != block->last->next; ins = ins->next) {
14573 if (!triple_stores_block(state, ins)) {
14574 continue;
14575 }
14576 if (ins->u.block != block) {
14577 internal_error(state, ins, "inconsitent block specified");
14578 }
14579 }
14580 if (!triple_stores_block(state, block->last->next)) {
14581 internal_error(state, block->last->next,
14582 "cannot find next block");
14583 }
14584 block = block->last->next->u.block;
14585 if (!block) {
14586 internal_error(state, block->last->next,
14587 "bad next block");
14588 }
14589 } while(block != state->first_block);
14590}
14591
14592static void verify_domination(struct compile_state *state)
14593{
14594 struct triple *first, *ins;
14595 struct triple_set *set;
14596 if (!state->first_block) {
14597 return;
14598 }
14599
14600 first = RHS(state->main_function, 0);
14601 ins = first;
14602 do {
14603 for(set = ins->use; set; set = set->next) {
14604 struct triple **expr;
14605 if (set->member->op == OP_PHI) {
14606 continue;
14607 }
14608 /* See if the use is on the righ hand side */
14609 expr = triple_rhs(state, set->member, 0);
14610 for(; expr ; expr = triple_rhs(state, set->member, expr)) {
14611 if (*expr == ins) {
14612 break;
14613 }
14614 }
14615 if (expr &&
14616 !tdominates(state, ins, set->member)) {
14617 internal_error(state, set->member,
14618 "non dominated rhs use?");
14619 }
14620 }
14621 ins = ins->next;
14622 } while(ins != first);
14623}
14624
14625static void verify_piece(struct compile_state *state)
14626{
14627 struct triple *first, *ins;
14628 first = RHS(state->main_function, 0);
14629 ins = first;
14630 do {
14631 struct triple *ptr;
14632 int lhs, i;
14633 lhs = TRIPLE_LHS(ins->sizes);
14634 if ((ins->op == OP_WRITE) || (ins->op == OP_STORE)) {
14635 lhs = 0;
14636 }
14637 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
14638 if (ptr != LHS(ins, i)) {
14639 internal_error(state, ins, "malformed lhs on %s",
14640 tops(ins->op));
14641 }
14642 if (ptr->op != OP_PIECE) {
14643 internal_error(state, ins, "bad lhs op %s at %d on %s",
14644 tops(ptr->op), i, tops(ins->op));
14645 }
14646 if (ptr->u.cval != i) {
14647 internal_error(state, ins, "bad u.cval of %d %d expected",
14648 ptr->u.cval, i);
14649 }
14650 }
14651 ins = ins->next;
14652 } while(ins != first);
14653}
14654static void verify_ins_colors(struct compile_state *state)
14655{
14656 struct triple *first, *ins;
14657
14658 first = RHS(state->main_function, 0);
14659 ins = first;
14660 do {
14661 ins = ins->next;
14662 } while(ins != first);
14663}
14664static void verify_consistency(struct compile_state *state)
14665{
14666 verify_uses(state);
14667 verify_blocks(state);
14668 verify_domination(state);
14669 verify_piece(state);
14670 verify_ins_colors(state);
14671}
14672#else
Eric Biederman153ea352003-06-20 14:43:20 +000014673static void verify_consistency(struct compile_state *state) {}
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014674#endif /* DEBUG_USES */
Eric Biedermanb138ac82003-04-22 18:44:01 +000014675
14676static void optimize(struct compile_state *state)
14677{
14678 if (state->debug & DEBUG_TRIPLES) {
14679 print_triples(state);
14680 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014681 /* Replace structures with simpler data types */
14682 flatten_structures(state);
14683 if (state->debug & DEBUG_TRIPLES) {
14684 print_triples(state);
14685 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014686 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014687 /* Analize the intermediate code */
14688 setup_basic_blocks(state);
14689 analyze_idominators(state);
14690 analyze_ipdominators(state);
14691 /* Transform the code to ssa form */
14692 transform_to_ssa_form(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014693 verify_consistency(state);
Eric Biederman05f26fc2003-06-11 21:55:00 +000014694 if (state->debug & DEBUG_CODE_ELIMINATION) {
14695 fprintf(stdout, "After transform_to_ssa_form\n");
14696 print_blocks(state, stdout);
14697 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014698 /* Do strength reduction and simple constant optimizations */
14699 if (state->optimize >= 1) {
14700 simplify_all(state);
14701 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014702 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014703 /* Propogate constants throughout the code */
14704 if (state->optimize >= 2) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014705#warning "FIXME fix scc_transform"
Eric Biedermanb138ac82003-04-22 18:44:01 +000014706 scc_transform(state);
14707 transform_from_ssa_form(state);
14708 free_basic_blocks(state);
14709 setup_basic_blocks(state);
14710 analyze_idominators(state);
14711 analyze_ipdominators(state);
14712 transform_to_ssa_form(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014713 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014714 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014715#warning "WISHLIST implement single use constants (least possible register pressure)"
14716#warning "WISHLIST implement induction variable elimination"
Eric Biedermanb138ac82003-04-22 18:44:01 +000014717 /* Select architecture instructions and an initial partial
14718 * coloring based on architecture constraints.
14719 */
14720 transform_to_arch_instructions(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014721 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014722 if (state->debug & DEBUG_ARCH_CODE) {
14723 printf("After transform_to_arch_instructions\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014724 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014725 print_control_flow(state);
14726 }
14727 eliminate_inefectual_code(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014728 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014729 if (state->debug & DEBUG_CODE_ELIMINATION) {
14730 printf("After eliminate_inefectual_code\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014731 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014732 print_control_flow(state);
14733 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014734 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014735 /* Color all of the variables to see if they will fit in registers */
14736 insert_copies_to_phi(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014737 if (state->debug & DEBUG_INSERTED_COPIES) {
14738 printf("After insert_copies_to_phi\n");
14739 print_blocks(state, stdout);
14740 print_control_flow(state);
14741 }
14742 verify_consistency(state);
14743 insert_mandatory_copies(state);
14744 if (state->debug & DEBUG_INSERTED_COPIES) {
14745 printf("After insert_mandatory_copies\n");
14746 print_blocks(state, stdout);
14747 print_control_flow(state);
14748 }
14749 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014750 allocate_registers(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014751 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014752 if (state->debug & DEBUG_INTERMEDIATE_CODE) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014753 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014754 }
14755 if (state->debug & DEBUG_CONTROL_FLOW) {
14756 print_control_flow(state);
14757 }
14758 /* Remove the optimization information.
14759 * This is more to check for memory consistency than to free memory.
14760 */
14761 free_basic_blocks(state);
14762}
14763
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014764static void print_op_asm(struct compile_state *state,
14765 struct triple *ins, FILE *fp)
14766{
14767 struct asm_info *info;
14768 const char *ptr;
14769 unsigned lhs, rhs, i;
14770 info = ins->u.ainfo;
14771 lhs = TRIPLE_LHS(ins->sizes);
14772 rhs = TRIPLE_RHS(ins->sizes);
14773 /* Don't count the clobbers in lhs */
14774 for(i = 0; i < lhs; i++) {
14775 if (LHS(ins, i)->type == &void_type) {
14776 break;
14777 }
14778 }
14779 lhs = i;
Eric Biederman8d9c1232003-06-17 08:42:17 +000014780 fprintf(fp, "#ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014781 fputc('\t', fp);
14782 for(ptr = info->str; *ptr; ptr++) {
14783 char *next;
14784 unsigned long param;
14785 struct triple *piece;
14786 if (*ptr != '%') {
14787 fputc(*ptr, fp);
14788 continue;
14789 }
14790 ptr++;
14791 if (*ptr == '%') {
14792 fputc('%', fp);
14793 continue;
14794 }
14795 param = strtoul(ptr, &next, 10);
14796 if (ptr == next) {
14797 error(state, ins, "Invalid asm template");
14798 }
14799 if (param >= (lhs + rhs)) {
14800 error(state, ins, "Invalid param %%%u in asm template",
14801 param);
14802 }
14803 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
14804 fprintf(fp, "%s",
14805 arch_reg_str(ID_REG(piece->id)));
Eric Biederman8d9c1232003-06-17 08:42:17 +000014806 ptr = next -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014807 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000014808 fprintf(fp, "\n#NOT ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014809}
14810
14811
14812/* Only use the low x86 byte registers. This allows me
14813 * allocate the entire register when a byte register is used.
14814 */
14815#define X86_4_8BIT_GPRS 1
14816
14817/* Recognized x86 cpu variants */
14818#define BAD_CPU 0
14819#define CPU_I386 1
14820#define CPU_P3 2
14821#define CPU_P4 3
14822#define CPU_K7 4
14823#define CPU_K8 5
14824
14825#define CPU_DEFAULT CPU_I386
14826
Eric Biedermanb138ac82003-04-22 18:44:01 +000014827/* The x86 register classes */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014828#define REGC_FLAGS 0
14829#define REGC_GPR8 1
14830#define REGC_GPR16 2
14831#define REGC_GPR32 3
14832#define REGC_GPR64 4
14833#define REGC_MMX 5
14834#define REGC_XMM 6
14835#define REGC_GPR32_8 7
14836#define REGC_GPR16_8 8
14837#define REGC_IMM32 9
14838#define REGC_IMM16 10
14839#define REGC_IMM8 11
14840#define LAST_REGC REGC_IMM8
Eric Biedermanb138ac82003-04-22 18:44:01 +000014841#if LAST_REGC >= MAX_REGC
14842#error "MAX_REGC is to low"
14843#endif
14844
14845/* Register class masks */
14846#define REGCM_FLAGS (1 << REGC_FLAGS)
14847#define REGCM_GPR8 (1 << REGC_GPR8)
14848#define REGCM_GPR16 (1 << REGC_GPR16)
14849#define REGCM_GPR32 (1 << REGC_GPR32)
14850#define REGCM_GPR64 (1 << REGC_GPR64)
14851#define REGCM_MMX (1 << REGC_MMX)
14852#define REGCM_XMM (1 << REGC_XMM)
14853#define REGCM_GPR32_8 (1 << REGC_GPR32_8)
14854#define REGCM_GPR16_8 (1 << REGC_GPR16_8)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014855#define REGCM_IMM32 (1 << REGC_IMM32)
14856#define REGCM_IMM16 (1 << REGC_IMM16)
14857#define REGCM_IMM8 (1 << REGC_IMM8)
14858#define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
Eric Biedermanb138ac82003-04-22 18:44:01 +000014859
14860/* The x86 registers */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014861#define REG_EFLAGS 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000014862#define REGC_FLAGS_FIRST REG_EFLAGS
14863#define REGC_FLAGS_LAST REG_EFLAGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014864#define REG_AL 3
14865#define REG_BL 4
14866#define REG_CL 5
14867#define REG_DL 6
14868#define REG_AH 7
14869#define REG_BH 8
14870#define REG_CH 9
14871#define REG_DH 10
Eric Biedermanb138ac82003-04-22 18:44:01 +000014872#define REGC_GPR8_FIRST REG_AL
14873#if X86_4_8BIT_GPRS
14874#define REGC_GPR8_LAST REG_DL
14875#else
14876#define REGC_GPR8_LAST REG_DH
14877#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014878#define REG_AX 11
14879#define REG_BX 12
14880#define REG_CX 13
14881#define REG_DX 14
14882#define REG_SI 15
14883#define REG_DI 16
14884#define REG_BP 17
14885#define REG_SP 18
Eric Biedermanb138ac82003-04-22 18:44:01 +000014886#define REGC_GPR16_FIRST REG_AX
14887#define REGC_GPR16_LAST REG_SP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014888#define REG_EAX 19
14889#define REG_EBX 20
14890#define REG_ECX 21
14891#define REG_EDX 22
14892#define REG_ESI 23
14893#define REG_EDI 24
14894#define REG_EBP 25
14895#define REG_ESP 26
Eric Biedermanb138ac82003-04-22 18:44:01 +000014896#define REGC_GPR32_FIRST REG_EAX
14897#define REGC_GPR32_LAST REG_ESP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014898#define REG_EDXEAX 27
Eric Biedermanb138ac82003-04-22 18:44:01 +000014899#define REGC_GPR64_FIRST REG_EDXEAX
14900#define REGC_GPR64_LAST REG_EDXEAX
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014901#define REG_MMX0 28
14902#define REG_MMX1 29
14903#define REG_MMX2 30
14904#define REG_MMX3 31
14905#define REG_MMX4 32
14906#define REG_MMX5 33
14907#define REG_MMX6 34
14908#define REG_MMX7 35
Eric Biedermanb138ac82003-04-22 18:44:01 +000014909#define REGC_MMX_FIRST REG_MMX0
14910#define REGC_MMX_LAST REG_MMX7
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014911#define REG_XMM0 36
14912#define REG_XMM1 37
14913#define REG_XMM2 38
14914#define REG_XMM3 39
14915#define REG_XMM4 40
14916#define REG_XMM5 41
14917#define REG_XMM6 42
14918#define REG_XMM7 43
Eric Biedermanb138ac82003-04-22 18:44:01 +000014919#define REGC_XMM_FIRST REG_XMM0
14920#define REGC_XMM_LAST REG_XMM7
14921#warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
14922#define LAST_REG REG_XMM7
14923
14924#define REGC_GPR32_8_FIRST REG_EAX
14925#define REGC_GPR32_8_LAST REG_EDX
14926#define REGC_GPR16_8_FIRST REG_AX
14927#define REGC_GPR16_8_LAST REG_DX
14928
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014929#define REGC_IMM8_FIRST -1
14930#define REGC_IMM8_LAST -1
14931#define REGC_IMM16_FIRST -2
14932#define REGC_IMM16_LAST -1
14933#define REGC_IMM32_FIRST -4
14934#define REGC_IMM32_LAST -1
14935
Eric Biedermanb138ac82003-04-22 18:44:01 +000014936#if LAST_REG >= MAX_REGISTERS
14937#error "MAX_REGISTERS to low"
14938#endif
14939
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014940
14941static unsigned regc_size[LAST_REGC +1] = {
14942 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
14943 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
14944 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
14945 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
14946 [REGC_GPR64] = REGC_GPR64_LAST - REGC_GPR64_FIRST + 1,
14947 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
14948 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
14949 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
14950 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
14951 [REGC_IMM32] = 0,
14952 [REGC_IMM16] = 0,
14953 [REGC_IMM8] = 0,
14954};
14955
14956static const struct {
14957 int first, last;
14958} regcm_bound[LAST_REGC + 1] = {
14959 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
14960 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
14961 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
14962 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
14963 [REGC_GPR64] = { REGC_GPR64_FIRST, REGC_GPR64_LAST },
14964 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
14965 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
14966 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
14967 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
14968 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
14969 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
14970 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
14971};
14972
14973static int arch_encode_cpu(const char *cpu)
14974{
14975 struct cpu {
14976 const char *name;
14977 int cpu;
14978 } cpus[] = {
14979 { "i386", CPU_I386 },
14980 { "p3", CPU_P3 },
14981 { "p4", CPU_P4 },
14982 { "k7", CPU_K7 },
14983 { "k8", CPU_K8 },
14984 { 0, BAD_CPU }
14985 };
14986 struct cpu *ptr;
14987 for(ptr = cpus; ptr->name; ptr++) {
14988 if (strcmp(ptr->name, cpu) == 0) {
14989 break;
14990 }
14991 }
14992 return ptr->cpu;
14993}
14994
Eric Biedermanb138ac82003-04-22 18:44:01 +000014995static unsigned arch_regc_size(struct compile_state *state, int class)
14996{
Eric Biedermanb138ac82003-04-22 18:44:01 +000014997 if ((class < 0) || (class > LAST_REGC)) {
14998 return 0;
14999 }
15000 return regc_size[class];
15001}
15002static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
15003{
15004 /* See if two register classes may have overlapping registers */
15005 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 |
15006 REGCM_GPR32_8 | REGCM_GPR32 | REGCM_GPR64;
15007
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015008 /* Special case for the immediates */
15009 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
15010 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
15011 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
15012 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
15013 return 0;
15014 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015015 return (regcm1 & regcm2) ||
15016 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
15017}
15018
15019static void arch_reg_equivs(
15020 struct compile_state *state, unsigned *equiv, int reg)
15021{
15022 if ((reg < 0) || (reg > LAST_REG)) {
15023 internal_error(state, 0, "invalid register");
15024 }
15025 *equiv++ = reg;
15026 switch(reg) {
15027 case REG_AL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015028#if X86_4_8BIT_GPRS
15029 *equiv++ = REG_AH;
15030#endif
15031 *equiv++ = REG_AX;
15032 *equiv++ = REG_EAX;
15033 *equiv++ = REG_EDXEAX;
15034 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015035 case REG_AH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015036#if X86_4_8BIT_GPRS
15037 *equiv++ = REG_AL;
15038#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015039 *equiv++ = REG_AX;
15040 *equiv++ = REG_EAX;
15041 *equiv++ = REG_EDXEAX;
15042 break;
15043 case REG_BL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015044#if X86_4_8BIT_GPRS
15045 *equiv++ = REG_BH;
15046#endif
15047 *equiv++ = REG_BX;
15048 *equiv++ = REG_EBX;
15049 break;
15050
Eric Biedermanb138ac82003-04-22 18:44:01 +000015051 case REG_BH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015052#if X86_4_8BIT_GPRS
15053 *equiv++ = REG_BL;
15054#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015055 *equiv++ = REG_BX;
15056 *equiv++ = REG_EBX;
15057 break;
15058 case REG_CL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015059#if X86_4_8BIT_GPRS
15060 *equiv++ = REG_CH;
15061#endif
15062 *equiv++ = REG_CX;
15063 *equiv++ = REG_ECX;
15064 break;
15065
Eric Biedermanb138ac82003-04-22 18:44:01 +000015066 case REG_CH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015067#if X86_4_8BIT_GPRS
15068 *equiv++ = REG_CL;
15069#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015070 *equiv++ = REG_CX;
15071 *equiv++ = REG_ECX;
15072 break;
15073 case REG_DL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015074#if X86_4_8BIT_GPRS
15075 *equiv++ = REG_DH;
15076#endif
15077 *equiv++ = REG_DX;
15078 *equiv++ = REG_EDX;
15079 *equiv++ = REG_EDXEAX;
15080 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015081 case REG_DH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015082#if X86_4_8BIT_GPRS
15083 *equiv++ = REG_DL;
15084#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015085 *equiv++ = REG_DX;
15086 *equiv++ = REG_EDX;
15087 *equiv++ = REG_EDXEAX;
15088 break;
15089 case REG_AX:
15090 *equiv++ = REG_AL;
15091 *equiv++ = REG_AH;
15092 *equiv++ = REG_EAX;
15093 *equiv++ = REG_EDXEAX;
15094 break;
15095 case REG_BX:
15096 *equiv++ = REG_BL;
15097 *equiv++ = REG_BH;
15098 *equiv++ = REG_EBX;
15099 break;
15100 case REG_CX:
15101 *equiv++ = REG_CL;
15102 *equiv++ = REG_CH;
15103 *equiv++ = REG_ECX;
15104 break;
15105 case REG_DX:
15106 *equiv++ = REG_DL;
15107 *equiv++ = REG_DH;
15108 *equiv++ = REG_EDX;
15109 *equiv++ = REG_EDXEAX;
15110 break;
15111 case REG_SI:
15112 *equiv++ = REG_ESI;
15113 break;
15114 case REG_DI:
15115 *equiv++ = REG_EDI;
15116 break;
15117 case REG_BP:
15118 *equiv++ = REG_EBP;
15119 break;
15120 case REG_SP:
15121 *equiv++ = REG_ESP;
15122 break;
15123 case REG_EAX:
15124 *equiv++ = REG_AL;
15125 *equiv++ = REG_AH;
15126 *equiv++ = REG_AX;
15127 *equiv++ = REG_EDXEAX;
15128 break;
15129 case REG_EBX:
15130 *equiv++ = REG_BL;
15131 *equiv++ = REG_BH;
15132 *equiv++ = REG_BX;
15133 break;
15134 case REG_ECX:
15135 *equiv++ = REG_CL;
15136 *equiv++ = REG_CH;
15137 *equiv++ = REG_CX;
15138 break;
15139 case REG_EDX:
15140 *equiv++ = REG_DL;
15141 *equiv++ = REG_DH;
15142 *equiv++ = REG_DX;
15143 *equiv++ = REG_EDXEAX;
15144 break;
15145 case REG_ESI:
15146 *equiv++ = REG_SI;
15147 break;
15148 case REG_EDI:
15149 *equiv++ = REG_DI;
15150 break;
15151 case REG_EBP:
15152 *equiv++ = REG_BP;
15153 break;
15154 case REG_ESP:
15155 *equiv++ = REG_SP;
15156 break;
15157 case REG_EDXEAX:
15158 *equiv++ = REG_AL;
15159 *equiv++ = REG_AH;
15160 *equiv++ = REG_DL;
15161 *equiv++ = REG_DH;
15162 *equiv++ = REG_AX;
15163 *equiv++ = REG_DX;
15164 *equiv++ = REG_EAX;
15165 *equiv++ = REG_EDX;
15166 break;
15167 }
15168 *equiv++ = REG_UNSET;
15169}
15170
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015171static unsigned arch_avail_mask(struct compile_state *state)
15172{
15173 unsigned avail_mask;
15174 avail_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 |
15175 REGCM_GPR32 | REGCM_GPR32_8 | REGCM_GPR64 |
15176 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
15177 switch(state->cpu) {
15178 case CPU_P3:
15179 case CPU_K7:
15180 avail_mask |= REGCM_MMX;
15181 break;
15182 case CPU_P4:
15183 case CPU_K8:
15184 avail_mask |= REGCM_MMX | REGCM_XMM;
15185 break;
15186 }
15187#if 0
15188 /* Don't enable 8 bit values until I can force both operands
15189 * to be 8bits simultaneously.
15190 */
15191 avail_mask &= ~(REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16);
15192#endif
15193 return avail_mask;
15194}
15195
15196static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
15197{
15198 unsigned mask, result;
15199 int class, class2;
15200 result = regcm;
15201 result &= arch_avail_mask(state);
15202
15203 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
15204 if ((result & mask) == 0) {
15205 continue;
15206 }
15207 if (class > LAST_REGC) {
15208 result &= ~mask;
15209 }
15210 for(class2 = 0; class2 <= LAST_REGC; class2++) {
15211 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
15212 (regcm_bound[class2].last <= regcm_bound[class].last)) {
15213 result |= (1 << class2);
15214 }
15215 }
15216 }
15217 return result;
15218}
Eric Biedermanb138ac82003-04-22 18:44:01 +000015219
15220static unsigned arch_reg_regcm(struct compile_state *state, int reg)
15221{
Eric Biedermanb138ac82003-04-22 18:44:01 +000015222 unsigned mask;
15223 int class;
15224 mask = 0;
15225 for(class = 0; class <= LAST_REGC; class++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015226 if ((reg >= regcm_bound[class].first) &&
15227 (reg <= regcm_bound[class].last)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015228 mask |= (1 << class);
15229 }
15230 }
15231 if (!mask) {
15232 internal_error(state, 0, "reg %d not in any class", reg);
15233 }
15234 return mask;
15235}
15236
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015237static struct reg_info arch_reg_constraint(
15238 struct compile_state *state, struct type *type, const char *constraint)
15239{
15240 static const struct {
15241 char class;
15242 unsigned int mask;
15243 unsigned int reg;
15244 } constraints[] = {
15245 { 'r', REGCM_GPR32, REG_UNSET },
15246 { 'g', REGCM_GPR32, REG_UNSET },
15247 { 'p', REGCM_GPR32, REG_UNSET },
15248 { 'q', REGCM_GPR8, REG_UNSET },
15249 { 'Q', REGCM_GPR32_8, REG_UNSET },
15250 { 'x', REGCM_XMM, REG_UNSET },
15251 { 'y', REGCM_MMX, REG_UNSET },
15252 { 'a', REGCM_GPR32, REG_EAX },
15253 { 'b', REGCM_GPR32, REG_EBX },
15254 { 'c', REGCM_GPR32, REG_ECX },
15255 { 'd', REGCM_GPR32, REG_EDX },
15256 { 'D', REGCM_GPR32, REG_EDI },
15257 { 'S', REGCM_GPR32, REG_ESI },
15258 { '\0', 0, REG_UNSET },
15259 };
15260 unsigned int regcm;
15261 unsigned int mask, reg;
15262 struct reg_info result;
15263 const char *ptr;
15264 regcm = arch_type_to_regcm(state, type);
15265 reg = REG_UNSET;
15266 mask = 0;
15267 for(ptr = constraint; *ptr; ptr++) {
15268 int i;
15269 if (*ptr == ' ') {
15270 continue;
15271 }
15272 for(i = 0; constraints[i].class != '\0'; i++) {
15273 if (constraints[i].class == *ptr) {
15274 break;
15275 }
15276 }
15277 if (constraints[i].class == '\0') {
15278 error(state, 0, "invalid register constraint ``%c''", *ptr);
15279 break;
15280 }
15281 if ((constraints[i].mask & regcm) == 0) {
15282 error(state, 0, "invalid register class %c specified",
15283 *ptr);
15284 }
15285 mask |= constraints[i].mask;
15286 if (constraints[i].reg != REG_UNSET) {
15287 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
15288 error(state, 0, "Only one register may be specified");
15289 }
15290 reg = constraints[i].reg;
15291 }
15292 }
15293 result.reg = reg;
15294 result.regcm = mask;
15295 return result;
15296}
15297
15298static struct reg_info arch_reg_clobber(
15299 struct compile_state *state, const char *clobber)
15300{
15301 struct reg_info result;
15302 if (strcmp(clobber, "memory") == 0) {
15303 result.reg = REG_UNSET;
15304 result.regcm = 0;
15305 }
15306 else if (strcmp(clobber, "%eax") == 0) {
15307 result.reg = REG_EAX;
15308 result.regcm = REGCM_GPR32;
15309 }
15310 else if (strcmp(clobber, "%ebx") == 0) {
15311 result.reg = REG_EBX;
15312 result.regcm = REGCM_GPR32;
15313 }
15314 else if (strcmp(clobber, "%ecx") == 0) {
15315 result.reg = REG_ECX;
15316 result.regcm = REGCM_GPR32;
15317 }
15318 else if (strcmp(clobber, "%edx") == 0) {
15319 result.reg = REG_EDX;
15320 result.regcm = REGCM_GPR32;
15321 }
15322 else if (strcmp(clobber, "%esi") == 0) {
15323 result.reg = REG_ESI;
15324 result.regcm = REGCM_GPR32;
15325 }
15326 else if (strcmp(clobber, "%edi") == 0) {
15327 result.reg = REG_EDI;
15328 result.regcm = REGCM_GPR32;
15329 }
15330 else if (strcmp(clobber, "%ebp") == 0) {
15331 result.reg = REG_EBP;
15332 result.regcm = REGCM_GPR32;
15333 }
15334 else if (strcmp(clobber, "%esp") == 0) {
15335 result.reg = REG_ESP;
15336 result.regcm = REGCM_GPR32;
15337 }
15338 else if (strcmp(clobber, "cc") == 0) {
15339 result.reg = REG_EFLAGS;
15340 result.regcm = REGCM_FLAGS;
15341 }
15342 else if ((strncmp(clobber, "xmm", 3) == 0) &&
15343 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15344 result.reg = REG_XMM0 + octdigval(clobber[3]);
15345 result.regcm = REGCM_XMM;
15346 }
15347 else if ((strncmp(clobber, "mmx", 3) == 0) &&
15348 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15349 result.reg = REG_MMX0 + octdigval(clobber[3]);
15350 result.regcm = REGCM_MMX;
15351 }
15352 else {
15353 error(state, 0, "Invalid register clobber");
15354 result.reg = REG_UNSET;
15355 result.regcm = 0;
15356 }
15357 return result;
15358}
15359
Eric Biedermanb138ac82003-04-22 18:44:01 +000015360static int do_select_reg(struct compile_state *state,
15361 char *used, int reg, unsigned classes)
15362{
15363 unsigned mask;
15364 if (used[reg]) {
15365 return REG_UNSET;
15366 }
15367 mask = arch_reg_regcm(state, reg);
15368 return (classes & mask) ? reg : REG_UNSET;
15369}
15370
15371static int arch_select_free_register(
15372 struct compile_state *state, char *used, int classes)
15373{
15374 /* Preference: flags, 8bit gprs, 32bit gprs, other 32bit reg
15375 * other types of registers.
15376 */
15377 int i, reg;
15378 reg = REG_UNSET;
15379 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
15380 reg = do_select_reg(state, used, i, classes);
15381 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015382 for(i = REGC_GPR32_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR32_LAST); i++) {
15383 reg = do_select_reg(state, used, i, classes);
15384 }
15385 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
15386 reg = do_select_reg(state, used, i, classes);
15387 }
15388 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
15389 reg = do_select_reg(state, used, i, classes);
15390 }
15391 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
15392 reg = do_select_reg(state, used, i, classes);
15393 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015394 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
15395 reg = do_select_reg(state, used, i, classes);
15396 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015397 for(i = REGC_GPR64_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR64_LAST); i++) {
15398 reg = do_select_reg(state, used, i, classes);
15399 }
15400 return reg;
15401}
15402
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015403
Eric Biedermanb138ac82003-04-22 18:44:01 +000015404static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
15405{
15406#warning "FIXME force types smaller (if legal) before I get here"
Eric Biedermanb138ac82003-04-22 18:44:01 +000015407 unsigned avail_mask;
15408 unsigned mask;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015409 mask = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015410 avail_mask = arch_avail_mask(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015411 switch(type->type & TYPE_MASK) {
15412 case TYPE_ARRAY:
15413 case TYPE_VOID:
15414 mask = 0;
15415 break;
15416 case TYPE_CHAR:
15417 case TYPE_UCHAR:
15418 mask = REGCM_GPR8 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015419 REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000015420 REGCM_GPR32 | REGCM_GPR32_8 |
15421 REGCM_GPR64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015422 REGCM_MMX | REGCM_XMM |
15423 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015424 break;
15425 case TYPE_SHORT:
15426 case TYPE_USHORT:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015427 mask = REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000015428 REGCM_GPR32 | REGCM_GPR32_8 |
15429 REGCM_GPR64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015430 REGCM_MMX | REGCM_XMM |
15431 REGCM_IMM32 | REGCM_IMM16;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015432 break;
15433 case TYPE_INT:
15434 case TYPE_UINT:
15435 case TYPE_LONG:
15436 case TYPE_ULONG:
15437 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015438 mask = REGCM_GPR32 | REGCM_GPR32_8 |
15439 REGCM_GPR64 | REGCM_MMX | REGCM_XMM |
15440 REGCM_IMM32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015441 break;
15442 default:
15443 internal_error(state, 0, "no register class for type");
15444 break;
15445 }
15446 mask &= avail_mask;
15447 return mask;
15448}
15449
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015450static int is_imm32(struct triple *imm)
15451{
15452 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
15453 (imm->op == OP_ADDRCONST);
15454
15455}
15456static int is_imm16(struct triple *imm)
15457{
15458 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
15459}
15460static int is_imm8(struct triple *imm)
15461{
15462 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
15463}
15464
15465static int get_imm32(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015466{
15467 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015468 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015469 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000015470 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015471 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015472 if (!is_imm32(imm)) {
15473 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015474 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015475 unuse_triple(*expr, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015476 use_triple(imm, ins);
15477 *expr = imm;
15478 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015479}
15480
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015481static int get_imm8(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015482{
15483 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015484 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015485 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000015486 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015487 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015488 if (!is_imm8(imm)) {
15489 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015490 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015491 unuse_triple(*expr, ins);
15492 use_triple(imm, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015493 *expr = imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015494 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015495}
15496
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015497#define TEMPLATE_NOP 0
15498#define TEMPLATE_INTCONST8 1
15499#define TEMPLATE_INTCONST32 2
15500#define TEMPLATE_COPY_REG 3
15501#define TEMPLATE_COPY_IMM32 4
15502#define TEMPLATE_COPY_IMM16 5
15503#define TEMPLATE_COPY_IMM8 6
15504#define TEMPLATE_PHI 7
15505#define TEMPLATE_STORE8 8
15506#define TEMPLATE_STORE16 9
15507#define TEMPLATE_STORE32 10
15508#define TEMPLATE_LOAD8 11
15509#define TEMPLATE_LOAD16 12
15510#define TEMPLATE_LOAD32 13
15511#define TEMPLATE_BINARY_REG 14
15512#define TEMPLATE_BINARY_IMM 15
15513#define TEMPLATE_SL_CL 16
15514#define TEMPLATE_SL_IMM 17
15515#define TEMPLATE_UNARY 18
15516#define TEMPLATE_CMP_REG 19
15517#define TEMPLATE_CMP_IMM 20
15518#define TEMPLATE_TEST 21
15519#define TEMPLATE_SET 22
15520#define TEMPLATE_JMP 23
15521#define TEMPLATE_INB_DX 24
15522#define TEMPLATE_INB_IMM 25
15523#define TEMPLATE_INW_DX 26
15524#define TEMPLATE_INW_IMM 27
15525#define TEMPLATE_INL_DX 28
15526#define TEMPLATE_INL_IMM 29
15527#define TEMPLATE_OUTB_DX 30
15528#define TEMPLATE_OUTB_IMM 31
15529#define TEMPLATE_OUTW_DX 32
15530#define TEMPLATE_OUTW_IMM 33
15531#define TEMPLATE_OUTL_DX 34
15532#define TEMPLATE_OUTL_IMM 35
15533#define TEMPLATE_BSF 36
15534#define TEMPLATE_RDMSR 37
15535#define TEMPLATE_WRMSR 38
15536#define LAST_TEMPLATE TEMPLATE_WRMSR
15537#if LAST_TEMPLATE >= MAX_TEMPLATES
15538#error "MAX_TEMPLATES to low"
15539#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015540
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015541#define COPY_REGCM (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8 | REGCM_MMX | REGCM_XMM)
15542#define COPY32_REGCM (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015543
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015544static struct ins_template templates[] = {
15545 [TEMPLATE_NOP] = {},
15546 [TEMPLATE_INTCONST8] = {
15547 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15548 },
15549 [TEMPLATE_INTCONST32] = {
15550 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
15551 },
15552 [TEMPLATE_COPY_REG] = {
15553 .lhs = { [0] = { REG_UNSET, COPY_REGCM } },
15554 .rhs = { [0] = { REG_UNSET, COPY_REGCM } },
15555 },
15556 [TEMPLATE_COPY_IMM32] = {
15557 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
15558 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
15559 },
15560 [TEMPLATE_COPY_IMM16] = {
15561 .lhs = { [0] = { REG_UNSET, COPY32_REGCM | REGCM_GPR16 } },
15562 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 } },
15563 },
15564 [TEMPLATE_COPY_IMM8] = {
15565 .lhs = { [0] = { REG_UNSET, COPY_REGCM } },
15566 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15567 },
15568 [TEMPLATE_PHI] = {
15569 .lhs = { [0] = { REG_VIRT0, COPY_REGCM } },
15570 .rhs = {
15571 [ 0] = { REG_VIRT0, COPY_REGCM },
15572 [ 1] = { REG_VIRT0, COPY_REGCM },
15573 [ 2] = { REG_VIRT0, COPY_REGCM },
15574 [ 3] = { REG_VIRT0, COPY_REGCM },
15575 [ 4] = { REG_VIRT0, COPY_REGCM },
15576 [ 5] = { REG_VIRT0, COPY_REGCM },
15577 [ 6] = { REG_VIRT0, COPY_REGCM },
15578 [ 7] = { REG_VIRT0, COPY_REGCM },
15579 [ 8] = { REG_VIRT0, COPY_REGCM },
15580 [ 9] = { REG_VIRT0, COPY_REGCM },
15581 [10] = { REG_VIRT0, COPY_REGCM },
15582 [11] = { REG_VIRT0, COPY_REGCM },
15583 [12] = { REG_VIRT0, COPY_REGCM },
15584 [13] = { REG_VIRT0, COPY_REGCM },
15585 [14] = { REG_VIRT0, COPY_REGCM },
15586 [15] = { REG_VIRT0, COPY_REGCM },
15587 }, },
15588 [TEMPLATE_STORE8] = {
15589 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15590 .rhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15591 },
15592 [TEMPLATE_STORE16] = {
15593 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15594 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
15595 },
15596 [TEMPLATE_STORE32] = {
15597 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15598 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15599 },
15600 [TEMPLATE_LOAD8] = {
15601 .lhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15602 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15603 },
15604 [TEMPLATE_LOAD16] = {
15605 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
15606 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15607 },
15608 [TEMPLATE_LOAD32] = {
15609 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15610 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15611 },
15612 [TEMPLATE_BINARY_REG] = {
15613 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15614 .rhs = {
15615 [0] = { REG_VIRT0, REGCM_GPR32 },
15616 [1] = { REG_UNSET, REGCM_GPR32 },
15617 },
15618 },
15619 [TEMPLATE_BINARY_IMM] = {
15620 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15621 .rhs = {
15622 [0] = { REG_VIRT0, REGCM_GPR32 },
15623 [1] = { REG_UNNEEDED, REGCM_IMM32 },
15624 },
15625 },
15626 [TEMPLATE_SL_CL] = {
15627 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15628 .rhs = {
15629 [0] = { REG_VIRT0, REGCM_GPR32 },
15630 [1] = { REG_CL, REGCM_GPR8 },
15631 },
15632 },
15633 [TEMPLATE_SL_IMM] = {
15634 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15635 .rhs = {
15636 [0] = { REG_VIRT0, REGCM_GPR32 },
15637 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15638 },
15639 },
15640 [TEMPLATE_UNARY] = {
15641 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15642 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15643 },
15644 [TEMPLATE_CMP_REG] = {
15645 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15646 .rhs = {
15647 [0] = { REG_UNSET, REGCM_GPR32 },
15648 [1] = { REG_UNSET, REGCM_GPR32 },
15649 },
15650 },
15651 [TEMPLATE_CMP_IMM] = {
15652 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15653 .rhs = {
15654 [0] = { REG_UNSET, REGCM_GPR32 },
15655 [1] = { REG_UNNEEDED, REGCM_IMM32 },
15656 },
15657 },
15658 [TEMPLATE_TEST] = {
15659 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15660 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15661 },
15662 [TEMPLATE_SET] = {
15663 .lhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15664 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15665 },
15666 [TEMPLATE_JMP] = {
15667 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15668 },
15669 [TEMPLATE_INB_DX] = {
15670 .lhs = { [0] = { REG_AL, REGCM_GPR8 } },
15671 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15672 },
15673 [TEMPLATE_INB_IMM] = {
15674 .lhs = { [0] = { REG_AL, REGCM_GPR8 } },
15675 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15676 },
15677 [TEMPLATE_INW_DX] = {
15678 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
15679 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15680 },
15681 [TEMPLATE_INW_IMM] = {
15682 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
15683 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15684 },
15685 [TEMPLATE_INL_DX] = {
15686 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
15687 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15688 },
15689 [TEMPLATE_INL_IMM] = {
15690 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
15691 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15692 },
15693 [TEMPLATE_OUTB_DX] = {
15694 .rhs = {
15695 [0] = { REG_AL, REGCM_GPR8 },
15696 [1] = { REG_DX, REGCM_GPR16 },
15697 },
15698 },
15699 [TEMPLATE_OUTB_IMM] = {
15700 .rhs = {
15701 [0] = { REG_AL, REGCM_GPR8 },
15702 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15703 },
15704 },
15705 [TEMPLATE_OUTW_DX] = {
15706 .rhs = {
15707 [0] = { REG_AX, REGCM_GPR16 },
15708 [1] = { REG_DX, REGCM_GPR16 },
15709 },
15710 },
15711 [TEMPLATE_OUTW_IMM] = {
15712 .rhs = {
15713 [0] = { REG_AX, REGCM_GPR16 },
15714 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15715 },
15716 },
15717 [TEMPLATE_OUTL_DX] = {
15718 .rhs = {
15719 [0] = { REG_EAX, REGCM_GPR32 },
15720 [1] = { REG_DX, REGCM_GPR16 },
15721 },
15722 },
15723 [TEMPLATE_OUTL_IMM] = {
15724 .rhs = {
15725 [0] = { REG_EAX, REGCM_GPR32 },
15726 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15727 },
15728 },
15729 [TEMPLATE_BSF] = {
15730 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15731 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15732 },
15733 [TEMPLATE_RDMSR] = {
15734 .lhs = {
15735 [0] = { REG_EAX, REGCM_GPR32 },
15736 [1] = { REG_EDX, REGCM_GPR32 },
15737 },
15738 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
15739 },
15740 [TEMPLATE_WRMSR] = {
15741 .rhs = {
15742 [0] = { REG_ECX, REGCM_GPR32 },
15743 [1] = { REG_EAX, REGCM_GPR32 },
15744 [2] = { REG_EDX, REGCM_GPR32 },
15745 },
15746 },
15747};
Eric Biedermanb138ac82003-04-22 18:44:01 +000015748
15749static void fixup_branches(struct compile_state *state,
15750 struct triple *cmp, struct triple *use, int jmp_op)
15751{
15752 struct triple_set *entry, *next;
15753 for(entry = use->use; entry; entry = next) {
15754 next = entry->next;
15755 if (entry->member->op == OP_COPY) {
15756 fixup_branches(state, cmp, entry->member, jmp_op);
15757 }
15758 else if (entry->member->op == OP_BRANCH) {
15759 struct triple *branch, *test;
Eric Biederman0babc1c2003-05-09 02:39:00 +000015760 struct triple *left, *right;
15761 left = right = 0;
15762 left = RHS(cmp, 0);
15763 if (TRIPLE_RHS(cmp->sizes) > 1) {
15764 right = RHS(cmp, 1);
15765 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015766 branch = entry->member;
15767 test = pre_triple(state, branch,
Eric Biederman0babc1c2003-05-09 02:39:00 +000015768 cmp->op, cmp->type, left, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015769 test->template_id = TEMPLATE_TEST;
15770 if (cmp->op == OP_CMP) {
15771 test->template_id = TEMPLATE_CMP_REG;
15772 if (get_imm32(test, &RHS(test, 1))) {
15773 test->template_id = TEMPLATE_CMP_IMM;
15774 }
15775 }
15776 use_triple(RHS(test, 0), test);
15777 use_triple(RHS(test, 1), test);
Eric Biederman0babc1c2003-05-09 02:39:00 +000015778 unuse_triple(RHS(branch, 0), branch);
15779 RHS(branch, 0) = test;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015780 branch->op = jmp_op;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015781 branch->template_id = TEMPLATE_JMP;
Eric Biederman0babc1c2003-05-09 02:39:00 +000015782 use_triple(RHS(branch, 0), branch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015783 }
15784 }
15785}
15786
15787static void bool_cmp(struct compile_state *state,
15788 struct triple *ins, int cmp_op, int jmp_op, int set_op)
15789{
Eric Biedermanb138ac82003-04-22 18:44:01 +000015790 struct triple_set *entry, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015791 struct triple *set;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015792
15793 /* Put a barrier up before the cmp which preceeds the
15794 * copy instruction. If a set actually occurs this gives
15795 * us a chance to move variables in registers out of the way.
15796 */
15797
15798 /* Modify the comparison operator */
15799 ins->op = cmp_op;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015800 ins->template_id = TEMPLATE_TEST;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015801 if (cmp_op == OP_CMP) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015802 ins->template_id = TEMPLATE_CMP_REG;
15803 if (get_imm32(ins, &RHS(ins, 1))) {
15804 ins->template_id = TEMPLATE_CMP_IMM;
15805 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015806 }
15807 /* Generate the instruction sequence that will transform the
15808 * result of the comparison into a logical value.
15809 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015810 set = post_triple(state, ins, set_op, ins->type, ins, 0);
15811 use_triple(ins, set);
15812 set->template_id = TEMPLATE_SET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015813
Eric Biedermanb138ac82003-04-22 18:44:01 +000015814 for(entry = ins->use; entry; entry = next) {
15815 next = entry->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015816 if (entry->member == set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015817 continue;
15818 }
15819 replace_rhs_use(state, ins, set, entry->member);
15820 }
15821 fixup_branches(state, ins, set, jmp_op);
15822}
15823
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015824static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
Eric Biederman0babc1c2003-05-09 02:39:00 +000015825{
15826 struct triple *next;
15827 int lhs, i;
15828 lhs = TRIPLE_LHS(ins->sizes);
15829 for(next = ins->next, i = 0; i < lhs; i++, next = next->next) {
15830 if (next != LHS(ins, i)) {
15831 internal_error(state, ins, "malformed lhs on %s",
15832 tops(ins->op));
15833 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015834 if (next->op != OP_PIECE) {
15835 internal_error(state, ins, "bad lhs op %s at %d on %s",
15836 tops(next->op), i, tops(ins->op));
15837 }
15838 if (next->u.cval != i) {
15839 internal_error(state, ins, "bad u.cval of %d %d expected",
15840 next->u.cval, i);
15841 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015842 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015843 return next;
Eric Biederman0babc1c2003-05-09 02:39:00 +000015844}
Eric Biedermanb138ac82003-04-22 18:44:01 +000015845
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015846struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
15847{
15848 struct ins_template *template;
15849 struct reg_info result;
15850 int zlhs;
15851 if (ins->op == OP_PIECE) {
15852 index = ins->u.cval;
15853 ins = MISC(ins, 0);
15854 }
15855 zlhs = TRIPLE_LHS(ins->sizes);
15856 if (triple_is_def(state, ins)) {
15857 zlhs = 1;
15858 }
15859 if (index >= zlhs) {
15860 internal_error(state, ins, "index %d out of range for %s\n",
15861 index, tops(ins->op));
15862 }
15863 switch(ins->op) {
15864 case OP_ASM:
15865 template = &ins->u.ainfo->tmpl;
15866 break;
15867 default:
15868 if (ins->template_id > LAST_TEMPLATE) {
15869 internal_error(state, ins, "bad template number %d",
15870 ins->template_id);
15871 }
15872 template = &templates[ins->template_id];
15873 break;
15874 }
15875 result = template->lhs[index];
15876 result.regcm = arch_regcm_normalize(state, result.regcm);
15877 if (result.reg != REG_UNNEEDED) {
15878 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
15879 }
15880 if (result.regcm == 0) {
15881 internal_error(state, ins, "lhs %d regcm == 0", index);
15882 }
15883 return result;
15884}
15885
15886struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
15887{
15888 struct reg_info result;
15889 struct ins_template *template;
15890 if ((index > TRIPLE_RHS(ins->sizes)) ||
15891 (ins->op == OP_PIECE)) {
15892 internal_error(state, ins, "index %d out of range for %s\n",
15893 index, tops(ins->op));
15894 }
15895 switch(ins->op) {
15896 case OP_ASM:
15897 template = &ins->u.ainfo->tmpl;
15898 break;
15899 default:
15900 if (ins->template_id > LAST_TEMPLATE) {
15901 internal_error(state, ins, "bad template number %d",
15902 ins->template_id);
15903 }
15904 template = &templates[ins->template_id];
15905 break;
15906 }
15907 result = template->rhs[index];
15908 result.regcm = arch_regcm_normalize(state, result.regcm);
15909 if (result.regcm == 0) {
15910 internal_error(state, ins, "rhs %d regcm == 0", index);
15911 }
15912 return result;
15913}
15914
15915static struct triple *transform_to_arch_instruction(
15916 struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015917{
15918 /* Transform from generic 3 address instructions
15919 * to archtecture specific instructions.
15920 * And apply architecture specific constrains to instructions.
15921 * Copies are inserted to preserve the register flexibility
15922 * of 3 address instructions.
15923 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015924 struct triple *next;
15925 next = ins->next;
15926 switch(ins->op) {
15927 case OP_INTCONST:
15928 ins->template_id = TEMPLATE_INTCONST32;
15929 if (ins->u.cval < 256) {
15930 ins->template_id = TEMPLATE_INTCONST8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015931 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015932 break;
15933 case OP_ADDRCONST:
15934 ins->template_id = TEMPLATE_INTCONST32;
15935 break;
15936 case OP_NOOP:
15937 case OP_SDECL:
15938 case OP_BLOBCONST:
15939 case OP_LABEL:
15940 ins->template_id = TEMPLATE_NOP;
15941 break;
15942 case OP_COPY:
15943 ins->template_id = TEMPLATE_COPY_REG;
15944 if (is_imm8(RHS(ins, 0))) {
15945 ins->template_id = TEMPLATE_COPY_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015946 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015947 else if (is_imm16(RHS(ins, 0))) {
15948 ins->template_id = TEMPLATE_COPY_IMM16;
15949 }
15950 else if (is_imm32(RHS(ins, 0))) {
15951 ins->template_id = TEMPLATE_COPY_IMM32;
15952 }
15953 else if (is_const(RHS(ins, 0))) {
15954 internal_error(state, ins, "bad constant passed to copy");
15955 }
15956 break;
15957 case OP_PHI:
15958 ins->template_id = TEMPLATE_PHI;
15959 break;
15960 case OP_STORE:
15961 switch(ins->type->type & TYPE_MASK) {
15962 case TYPE_CHAR: case TYPE_UCHAR:
15963 ins->template_id = TEMPLATE_STORE8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015964 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015965 case TYPE_SHORT: case TYPE_USHORT:
15966 ins->template_id = TEMPLATE_STORE16;
Eric Biederman0babc1c2003-05-09 02:39:00 +000015967 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015968 case TYPE_INT: case TYPE_UINT:
15969 case TYPE_LONG: case TYPE_ULONG:
15970 case TYPE_POINTER:
15971 ins->template_id = TEMPLATE_STORE32;
Eric Biederman0babc1c2003-05-09 02:39:00 +000015972 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015973 default:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015974 internal_error(state, ins, "unknown type in store");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015975 break;
15976 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015977 break;
15978 case OP_LOAD:
15979 switch(ins->type->type & TYPE_MASK) {
15980 case TYPE_CHAR: case TYPE_UCHAR:
15981 ins->template_id = TEMPLATE_LOAD8;
15982 break;
15983 case TYPE_SHORT:
15984 case TYPE_USHORT:
15985 ins->template_id = TEMPLATE_LOAD16;
15986 break;
15987 case TYPE_INT:
15988 case TYPE_UINT:
15989 case TYPE_LONG:
15990 case TYPE_ULONG:
15991 case TYPE_POINTER:
15992 ins->template_id = TEMPLATE_LOAD32;
15993 break;
15994 default:
15995 internal_error(state, ins, "unknown type in load");
15996 break;
15997 }
15998 break;
15999 case OP_ADD:
16000 case OP_SUB:
16001 case OP_AND:
16002 case OP_XOR:
16003 case OP_OR:
16004 case OP_SMUL:
16005 ins->template_id = TEMPLATE_BINARY_REG;
16006 if (get_imm32(ins, &RHS(ins, 1))) {
16007 ins->template_id = TEMPLATE_BINARY_IMM;
16008 }
16009 break;
16010 case OP_SL:
16011 case OP_SSR:
16012 case OP_USR:
16013 ins->template_id = TEMPLATE_SL_CL;
16014 if (get_imm8(ins, &RHS(ins, 1))) {
16015 ins->template_id = TEMPLATE_SL_IMM;
16016 }
16017 break;
16018 case OP_INVERT:
16019 case OP_NEG:
16020 ins->template_id = TEMPLATE_UNARY;
16021 break;
16022 case OP_EQ:
16023 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
16024 break;
16025 case OP_NOTEQ:
16026 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
16027 break;
16028 case OP_SLESS:
16029 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
16030 break;
16031 case OP_ULESS:
16032 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
16033 break;
16034 case OP_SMORE:
16035 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
16036 break;
16037 case OP_UMORE:
16038 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
16039 break;
16040 case OP_SLESSEQ:
16041 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
16042 break;
16043 case OP_ULESSEQ:
16044 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
16045 break;
16046 case OP_SMOREEQ:
16047 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
16048 break;
16049 case OP_UMOREEQ:
16050 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
16051 break;
16052 case OP_LTRUE:
16053 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
16054 break;
16055 case OP_LFALSE:
16056 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
16057 break;
16058 case OP_BRANCH:
16059 if (TRIPLE_RHS(ins->sizes) > 0) {
16060 internal_error(state, ins, "bad branch test");
16061 }
16062 ins->op = OP_JMP;
16063 ins->template_id = TEMPLATE_NOP;
16064 break;
16065 case OP_INB:
16066 case OP_INW:
16067 case OP_INL:
16068 switch(ins->op) {
16069 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
16070 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
16071 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
16072 }
16073 if (get_imm8(ins, &RHS(ins, 0))) {
16074 ins->template_id += 1;
16075 }
16076 break;
16077 case OP_OUTB:
16078 case OP_OUTW:
16079 case OP_OUTL:
16080 switch(ins->op) {
16081 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
16082 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
16083 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
16084 }
16085 if (get_imm8(ins, &RHS(ins, 1))) {
16086 ins->template_id += 1;
16087 }
16088 break;
16089 case OP_BSF:
16090 case OP_BSR:
16091 ins->template_id = TEMPLATE_BSF;
16092 break;
16093 case OP_RDMSR:
16094 ins->template_id = TEMPLATE_RDMSR;
16095 next = after_lhs(state, ins);
16096 break;
16097 case OP_WRMSR:
16098 ins->template_id = TEMPLATE_WRMSR;
16099 break;
16100 case OP_HLT:
16101 ins->template_id = TEMPLATE_NOP;
16102 break;
16103 case OP_ASM:
16104 ins->template_id = TEMPLATE_NOP;
16105 next = after_lhs(state, ins);
16106 break;
16107 /* Already transformed instructions */
16108 case OP_TEST:
16109 ins->template_id = TEMPLATE_TEST;
16110 break;
16111 case OP_CMP:
16112 ins->template_id = TEMPLATE_CMP_REG;
16113 if (get_imm32(ins, &RHS(ins, 1))) {
16114 ins->template_id = TEMPLATE_CMP_IMM;
16115 }
16116 break;
16117 case OP_JMP_EQ: case OP_JMP_NOTEQ:
16118 case OP_JMP_SLESS: case OP_JMP_ULESS:
16119 case OP_JMP_SMORE: case OP_JMP_UMORE:
16120 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
16121 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
16122 ins->template_id = TEMPLATE_JMP;
16123 break;
16124 case OP_SET_EQ: case OP_SET_NOTEQ:
16125 case OP_SET_SLESS: case OP_SET_ULESS:
16126 case OP_SET_SMORE: case OP_SET_UMORE:
16127 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
16128 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
16129 ins->template_id = TEMPLATE_SET;
16130 break;
16131 /* Unhandled instructions */
16132 case OP_PIECE:
16133 default:
16134 internal_error(state, ins, "unhandled ins: %d %s\n",
16135 ins->op, tops(ins->op));
16136 break;
16137 }
16138 return next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016139}
16140
Eric Biedermanb138ac82003-04-22 18:44:01 +000016141static void generate_local_labels(struct compile_state *state)
16142{
16143 struct triple *first, *label;
16144 int label_counter;
16145 label_counter = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016146 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016147 label = first;
16148 do {
16149 if ((label->op == OP_LABEL) ||
16150 (label->op == OP_SDECL)) {
16151 if (label->use) {
16152 label->u.cval = ++label_counter;
16153 } else {
16154 label->u.cval = 0;
16155 }
16156
16157 }
16158 label = label->next;
16159 } while(label != first);
16160}
16161
16162static int check_reg(struct compile_state *state,
16163 struct triple *triple, int classes)
16164{
16165 unsigned mask;
16166 int reg;
16167 reg = ID_REG(triple->id);
16168 if (reg == REG_UNSET) {
16169 internal_error(state, triple, "register not set");
16170 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016171 mask = arch_reg_regcm(state, reg);
16172 if (!(classes & mask)) {
16173 internal_error(state, triple, "reg %d in wrong class",
16174 reg);
16175 }
16176 return reg;
16177}
16178
16179static const char *arch_reg_str(int reg)
16180{
16181 static const char *regs[] = {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000016182 "%unset",
16183 "%unneeded",
Eric Biedermanb138ac82003-04-22 18:44:01 +000016184 "%eflags",
16185 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
16186 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
16187 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
16188 "%edx:%eax",
16189 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
16190 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
16191 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
16192 };
16193 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
16194 reg = 0;
16195 }
16196 return regs[reg];
16197}
16198
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016199
Eric Biedermanb138ac82003-04-22 18:44:01 +000016200static const char *reg(struct compile_state *state, struct triple *triple,
16201 int classes)
16202{
16203 int reg;
16204 reg = check_reg(state, triple, classes);
16205 return arch_reg_str(reg);
16206}
16207
16208const char *type_suffix(struct compile_state *state, struct type *type)
16209{
16210 const char *suffix;
16211 switch(size_of(state, type)) {
16212 case 1: suffix = "b"; break;
16213 case 2: suffix = "w"; break;
16214 case 4: suffix = "l"; break;
16215 default:
16216 internal_error(state, 0, "unknown suffix");
16217 suffix = 0;
16218 break;
16219 }
16220 return suffix;
16221}
16222
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016223static void print_const_val(
16224 struct compile_state *state, struct triple *ins, FILE *fp)
16225{
16226 switch(ins->op) {
16227 case OP_INTCONST:
16228 fprintf(fp, " $%ld ",
16229 (long_t)(ins->u.cval));
16230 break;
16231 case OP_ADDRCONST:
Eric Biederman05f26fc2003-06-11 21:55:00 +000016232 fprintf(fp, " $L%s%lu+%lu ",
16233 state->label_prefix,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016234 MISC(ins, 0)->u.cval,
16235 ins->u.cval);
16236 break;
16237 default:
16238 internal_error(state, ins, "unknown constant type");
16239 break;
16240 }
16241}
16242
Eric Biedermanb138ac82003-04-22 18:44:01 +000016243static void print_binary_op(struct compile_state *state,
16244 const char *op, struct triple *ins, FILE *fp)
16245{
16246 unsigned mask;
16247 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016248 if (RHS(ins, 0)->id != ins->id) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016249 internal_error(state, ins, "invalid register assignment");
16250 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016251 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016252 fprintf(fp, "\t%s ", op);
16253 print_const_val(state, RHS(ins, 1), fp);
16254 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000016255 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016256 }
16257 else {
16258 unsigned lmask, rmask;
16259 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016260 lreg = check_reg(state, RHS(ins, 0), mask);
16261 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016262 lmask = arch_reg_regcm(state, lreg);
16263 rmask = arch_reg_regcm(state, rreg);
16264 mask = lmask & rmask;
16265 fprintf(fp, "\t%s %s, %s\n",
16266 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000016267 reg(state, RHS(ins, 1), mask),
16268 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016269 }
16270}
16271static void print_unary_op(struct compile_state *state,
16272 const char *op, struct triple *ins, FILE *fp)
16273{
16274 unsigned mask;
16275 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16276 fprintf(fp, "\t%s %s\n",
16277 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000016278 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016279}
16280
16281static void print_op_shift(struct compile_state *state,
16282 const char *op, struct triple *ins, FILE *fp)
16283{
16284 unsigned mask;
16285 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016286 if (RHS(ins, 0)->id != ins->id) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016287 internal_error(state, ins, "invalid register assignment");
16288 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016289 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016290 fprintf(fp, "\t%s ", op);
16291 print_const_val(state, RHS(ins, 1), fp);
16292 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000016293 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016294 }
16295 else {
16296 fprintf(fp, "\t%s %s, %s\n",
16297 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000016298 reg(state, RHS(ins, 1), REGCM_GPR8),
16299 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016300 }
16301}
16302
16303static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
16304{
16305 const char *op;
16306 int mask;
16307 int dreg;
16308 mask = 0;
16309 switch(ins->op) {
16310 case OP_INB: op = "inb", mask = REGCM_GPR8; break;
16311 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
16312 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
16313 default:
16314 internal_error(state, ins, "not an in operation");
16315 op = 0;
16316 break;
16317 }
16318 dreg = check_reg(state, ins, mask);
16319 if (!reg_is_reg(state, dreg, REG_EAX)) {
16320 internal_error(state, ins, "dst != %%eax");
16321 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016322 if (is_const(RHS(ins, 0))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016323 fprintf(fp, "\t%s ", op);
16324 print_const_val(state, RHS(ins, 0), fp);
16325 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000016326 reg(state, ins, mask));
16327 }
16328 else {
16329 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016330 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016331 if (!reg_is_reg(state, addr_reg, REG_DX)) {
16332 internal_error(state, ins, "src != %%dx");
16333 }
16334 fprintf(fp, "\t%s %s, %s\n",
16335 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000016336 reg(state, RHS(ins, 0), REGCM_GPR16),
Eric Biedermanb138ac82003-04-22 18:44:01 +000016337 reg(state, ins, mask));
16338 }
16339}
16340
16341static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
16342{
16343 const char *op;
16344 int mask;
16345 int lreg;
16346 mask = 0;
16347 switch(ins->op) {
16348 case OP_OUTB: op = "outb", mask = REGCM_GPR8; break;
16349 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
16350 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
16351 default:
16352 internal_error(state, ins, "not an out operation");
16353 op = 0;
16354 break;
16355 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016356 lreg = check_reg(state, RHS(ins, 0), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016357 if (!reg_is_reg(state, lreg, REG_EAX)) {
16358 internal_error(state, ins, "src != %%eax");
16359 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016360 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016361 fprintf(fp, "\t%s %s,",
16362 op, reg(state, RHS(ins, 0), mask));
16363 print_const_val(state, RHS(ins, 1), fp);
16364 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016365 }
16366 else {
16367 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016368 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016369 if (!reg_is_reg(state, addr_reg, REG_DX)) {
16370 internal_error(state, ins, "dst != %%dx");
16371 }
16372 fprintf(fp, "\t%s %s, %s\n",
16373 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000016374 reg(state, RHS(ins, 0), mask),
16375 reg(state, RHS(ins, 1), REGCM_GPR16));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016376 }
16377}
16378
16379static void print_op_move(struct compile_state *state,
16380 struct triple *ins, FILE *fp)
16381{
16382 /* op_move is complex because there are many types
16383 * of registers we can move between.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016384 * Because OP_COPY will be introduced in arbitrary locations
16385 * OP_COPY must not affect flags.
Eric Biedermanb138ac82003-04-22 18:44:01 +000016386 */
16387 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
16388 struct triple *dst, *src;
16389 if (ins->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016390 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016391 dst = ins;
16392 }
16393 else if (ins->op == OP_WRITE) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016394 dst = LHS(ins, 0);
16395 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016396 }
16397 else {
16398 internal_error(state, ins, "unknown move operation");
16399 src = dst = 0;
16400 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016401 if (!is_const(src)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016402 int src_reg, dst_reg;
16403 int src_regcm, dst_regcm;
16404 src_reg = ID_REG(src->id);
16405 dst_reg = ID_REG(dst->id);
16406 src_regcm = arch_reg_regcm(state, src_reg);
16407 dst_regcm = arch_reg_regcm(state, dst_reg);
16408 /* If the class is the same just move the register */
16409 if (src_regcm & dst_regcm &
16410 (REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32)) {
16411 if ((src_reg != dst_reg) || !omit_copy) {
16412 fprintf(fp, "\tmov %s, %s\n",
16413 reg(state, src, src_regcm),
16414 reg(state, dst, dst_regcm));
16415 }
16416 }
16417 /* Move 32bit to 16bit */
16418 else if ((src_regcm & REGCM_GPR32) &&
16419 (dst_regcm & REGCM_GPR16)) {
16420 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
16421 if ((src_reg != dst_reg) || !omit_copy) {
16422 fprintf(fp, "\tmovw %s, %s\n",
16423 arch_reg_str(src_reg),
16424 arch_reg_str(dst_reg));
16425 }
16426 }
16427 /* Move 32bit to 8bit */
16428 else if ((src_regcm & REGCM_GPR32_8) &&
16429 (dst_regcm & REGCM_GPR8))
16430 {
16431 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
16432 if ((src_reg != dst_reg) || !omit_copy) {
16433 fprintf(fp, "\tmovb %s, %s\n",
16434 arch_reg_str(src_reg),
16435 arch_reg_str(dst_reg));
16436 }
16437 }
16438 /* Move 16bit to 8bit */
16439 else if ((src_regcm & REGCM_GPR16_8) &&
16440 (dst_regcm & REGCM_GPR8))
16441 {
16442 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
16443 if ((src_reg != dst_reg) || !omit_copy) {
16444 fprintf(fp, "\tmovb %s, %s\n",
16445 arch_reg_str(src_reg),
16446 arch_reg_str(dst_reg));
16447 }
16448 }
16449 /* Move 8/16bit to 16/32bit */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016450 else if ((src_regcm & (REGCM_GPR8 | REGCM_GPR16)) &&
16451 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016452 const char *op;
16453 op = is_signed(src->type)? "movsx": "movzx";
16454 fprintf(fp, "\t%s %s, %s\n",
16455 op,
16456 reg(state, src, src_regcm),
16457 reg(state, dst, dst_regcm));
16458 }
16459 /* Move between sse registers */
16460 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
16461 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016462 fprintf(fp, "\tmovdqa %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000016463 reg(state, src, src_regcm),
16464 reg(state, dst, dst_regcm));
16465 }
16466 }
16467 /* Move between mmx registers or mmx & sse registers */
16468 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
16469 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
16470 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016471 fprintf(fp, "\tmovq %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000016472 reg(state, src, src_regcm),
16473 reg(state, dst, dst_regcm));
16474 }
16475 }
16476 /* Move between 32bit gprs & mmx/sse registers */
16477 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
16478 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
16479 fprintf(fp, "\tmovd %s, %s\n",
16480 reg(state, src, src_regcm),
16481 reg(state, dst, dst_regcm));
16482 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016483#if X86_4_8BIT_GPRS
16484 /* Move from 8bit gprs to mmx/sse registers */
16485 else if ((src_regcm & REGCM_GPR8) && (src_reg <= REG_DL) &&
16486 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
16487 const char *op;
16488 int mid_reg;
16489 op = is_signed(src->type)? "movsx":"movzx";
16490 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16491 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
16492 op,
16493 reg(state, src, src_regcm),
16494 arch_reg_str(mid_reg),
16495 arch_reg_str(mid_reg),
16496 reg(state, dst, dst_regcm));
16497 }
16498 /* Move from mmx/sse registers and 8bit gprs */
16499 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
16500 (dst_regcm & REGCM_GPR8) && (dst_reg <= REG_DL)) {
16501 int mid_reg;
16502 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16503 fprintf(fp, "\tmovd %s, %s\n",
16504 reg(state, src, src_regcm),
16505 arch_reg_str(mid_reg));
16506 }
16507 /* Move from 32bit gprs to 16bit gprs */
16508 else if ((src_regcm & REGCM_GPR32) &&
16509 (dst_regcm & REGCM_GPR16)) {
16510 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
16511 if ((src_reg != dst_reg) || !omit_copy) {
16512 fprintf(fp, "\tmov %s, %s\n",
16513 arch_reg_str(src_reg),
16514 arch_reg_str(dst_reg));
16515 }
16516 }
16517 /* Move from 32bit gprs to 8bit gprs */
16518 else if ((src_regcm & REGCM_GPR32) &&
16519 (dst_regcm & REGCM_GPR8)) {
16520 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16521 if ((src_reg != dst_reg) || !omit_copy) {
16522 fprintf(fp, "\tmov %s, %s\n",
16523 arch_reg_str(src_reg),
16524 arch_reg_str(dst_reg));
16525 }
16526 }
16527 /* Move from 16bit gprs to 8bit gprs */
16528 else if ((src_regcm & REGCM_GPR16) &&
16529 (dst_regcm & REGCM_GPR8)) {
16530 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
16531 if ((src_reg != dst_reg) || !omit_copy) {
16532 fprintf(fp, "\tmov %s, %s\n",
16533 arch_reg_str(src_reg),
16534 arch_reg_str(dst_reg));
16535 }
16536 }
16537#endif /* X86_4_8BIT_GPRS */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016538 else {
16539 internal_error(state, ins, "unknown copy type");
16540 }
16541 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016542 else {
16543 fprintf(fp, "\tmov ");
16544 print_const_val(state, src, fp);
16545 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000016546 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016547 }
16548}
16549
16550static void print_op_load(struct compile_state *state,
16551 struct triple *ins, FILE *fp)
16552{
16553 struct triple *dst, *src;
16554 dst = ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016555 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016556 if (is_const(src) || is_const(dst)) {
16557 internal_error(state, ins, "unknown load operation");
16558 }
16559 fprintf(fp, "\tmov (%s), %s\n",
16560 reg(state, src, REGCM_GPR32),
16561 reg(state, dst, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32));
16562}
16563
16564
16565static void print_op_store(struct compile_state *state,
16566 struct triple *ins, FILE *fp)
16567{
16568 struct triple *dst, *src;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016569 dst = LHS(ins, 0);
16570 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016571 if (is_const(src) && (src->op == OP_INTCONST)) {
16572 long_t value;
16573 value = (long_t)(src->u.cval);
16574 fprintf(fp, "\tmov%s $%ld, (%s)\n",
16575 type_suffix(state, src->type),
16576 value,
16577 reg(state, dst, REGCM_GPR32));
16578 }
16579 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
16580 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
16581 type_suffix(state, src->type),
16582 reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
16583 dst->u.cval);
16584 }
16585 else {
16586 if (is_const(src) || is_const(dst)) {
16587 internal_error(state, ins, "unknown store operation");
16588 }
16589 fprintf(fp, "\tmov%s %s, (%s)\n",
16590 type_suffix(state, src->type),
16591 reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
16592 reg(state, dst, REGCM_GPR32));
16593 }
16594
16595
16596}
16597
16598static void print_op_smul(struct compile_state *state,
16599 struct triple *ins, FILE *fp)
16600{
Eric Biederman0babc1c2003-05-09 02:39:00 +000016601 if (!is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016602 fprintf(fp, "\timul %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000016603 reg(state, RHS(ins, 1), REGCM_GPR32),
16604 reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016605 }
16606 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016607 fprintf(fp, "\timul ");
16608 print_const_val(state, RHS(ins, 1), fp);
16609 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016610 }
16611}
16612
16613static void print_op_cmp(struct compile_state *state,
16614 struct triple *ins, FILE *fp)
16615{
16616 unsigned mask;
16617 int dreg;
16618 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16619 dreg = check_reg(state, ins, REGCM_FLAGS);
16620 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
16621 internal_error(state, ins, "bad dest register for cmp");
16622 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016623 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016624 fprintf(fp, "\tcmp ");
16625 print_const_val(state, RHS(ins, 1), fp);
16626 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016627 }
16628 else {
16629 unsigned lmask, rmask;
16630 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016631 lreg = check_reg(state, RHS(ins, 0), mask);
16632 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016633 lmask = arch_reg_regcm(state, lreg);
16634 rmask = arch_reg_regcm(state, rreg);
16635 mask = lmask & rmask;
16636 fprintf(fp, "\tcmp %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000016637 reg(state, RHS(ins, 1), mask),
16638 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016639 }
16640}
16641
16642static void print_op_test(struct compile_state *state,
16643 struct triple *ins, FILE *fp)
16644{
16645 unsigned mask;
16646 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16647 fprintf(fp, "\ttest %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000016648 reg(state, RHS(ins, 0), mask),
16649 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016650}
16651
16652static void print_op_branch(struct compile_state *state,
16653 struct triple *branch, FILE *fp)
16654{
16655 const char *bop = "j";
16656 if (branch->op == OP_JMP) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016657 if (TRIPLE_RHS(branch->sizes) != 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016658 internal_error(state, branch, "jmp with condition?");
16659 }
16660 bop = "jmp";
16661 }
16662 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016663 struct triple *ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016664 if (TRIPLE_RHS(branch->sizes) != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016665 internal_error(state, branch, "jmpcc without condition?");
16666 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016667 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
16668 if ((RHS(branch, 0)->op != OP_CMP) &&
16669 (RHS(branch, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016670 internal_error(state, branch, "bad branch test");
16671 }
16672#warning "FIXME I have observed instructions between the test and branch instructions"
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016673 ptr = RHS(branch, 0);
16674 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
16675 if (ptr->op != OP_COPY) {
16676 internal_error(state, branch, "branch does not follow test");
16677 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016678 }
16679 switch(branch->op) {
16680 case OP_JMP_EQ: bop = "jz"; break;
16681 case OP_JMP_NOTEQ: bop = "jnz"; break;
16682 case OP_JMP_SLESS: bop = "jl"; break;
16683 case OP_JMP_ULESS: bop = "jb"; break;
16684 case OP_JMP_SMORE: bop = "jg"; break;
16685 case OP_JMP_UMORE: bop = "ja"; break;
16686 case OP_JMP_SLESSEQ: bop = "jle"; break;
16687 case OP_JMP_ULESSEQ: bop = "jbe"; break;
16688 case OP_JMP_SMOREEQ: bop = "jge"; break;
16689 case OP_JMP_UMOREEQ: bop = "jae"; break;
16690 default:
16691 internal_error(state, branch, "Invalid branch op");
16692 break;
16693 }
16694
16695 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016696 fprintf(fp, "\t%s L%s%lu\n",
16697 bop,
16698 state->label_prefix,
16699 TARG(branch, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016700}
16701
16702static void print_op_set(struct compile_state *state,
16703 struct triple *set, FILE *fp)
16704{
16705 const char *sop = "set";
Eric Biederman0babc1c2003-05-09 02:39:00 +000016706 if (TRIPLE_RHS(set->sizes) != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016707 internal_error(state, set, "setcc without condition?");
16708 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016709 check_reg(state, RHS(set, 0), REGCM_FLAGS);
16710 if ((RHS(set, 0)->op != OP_CMP) &&
16711 (RHS(set, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016712 internal_error(state, set, "bad set test");
16713 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016714 if (RHS(set, 0)->next != set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016715 internal_error(state, set, "set does not follow test");
16716 }
16717 switch(set->op) {
16718 case OP_SET_EQ: sop = "setz"; break;
16719 case OP_SET_NOTEQ: sop = "setnz"; break;
16720 case OP_SET_SLESS: sop = "setl"; break;
16721 case OP_SET_ULESS: sop = "setb"; break;
16722 case OP_SET_SMORE: sop = "setg"; break;
16723 case OP_SET_UMORE: sop = "seta"; break;
16724 case OP_SET_SLESSEQ: sop = "setle"; break;
16725 case OP_SET_ULESSEQ: sop = "setbe"; break;
16726 case OP_SET_SMOREEQ: sop = "setge"; break;
16727 case OP_SET_UMOREEQ: sop = "setae"; break;
16728 default:
16729 internal_error(state, set, "Invalid set op");
16730 break;
16731 }
16732 fprintf(fp, "\t%s %s\n",
16733 sop, reg(state, set, REGCM_GPR8));
16734}
16735
16736static void print_op_bit_scan(struct compile_state *state,
16737 struct triple *ins, FILE *fp)
16738{
16739 const char *op;
16740 switch(ins->op) {
16741 case OP_BSF: op = "bsf"; break;
16742 case OP_BSR: op = "bsr"; break;
16743 default:
16744 internal_error(state, ins, "unknown bit scan");
16745 op = 0;
16746 break;
16747 }
16748 fprintf(fp,
16749 "\t%s %s, %s\n"
16750 "\tjnz 1f\n"
16751 "\tmovl $-1, %s\n"
16752 "1:\n",
16753 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000016754 reg(state, RHS(ins, 0), REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000016755 reg(state, ins, REGCM_GPR32),
16756 reg(state, ins, REGCM_GPR32));
16757}
16758
16759static void print_const(struct compile_state *state,
16760 struct triple *ins, FILE *fp)
16761{
16762 switch(ins->op) {
16763 case OP_INTCONST:
16764 switch(ins->type->type & TYPE_MASK) {
16765 case TYPE_CHAR:
16766 case TYPE_UCHAR:
16767 fprintf(fp, ".byte 0x%02lx\n", ins->u.cval);
16768 break;
16769 case TYPE_SHORT:
16770 case TYPE_USHORT:
16771 fprintf(fp, ".short 0x%04lx\n", ins->u.cval);
16772 break;
16773 case TYPE_INT:
16774 case TYPE_UINT:
16775 case TYPE_LONG:
16776 case TYPE_ULONG:
16777 fprintf(fp, ".int %lu\n", ins->u.cval);
16778 break;
16779 default:
16780 internal_error(state, ins, "Unknown constant type");
16781 }
16782 break;
16783 case OP_BLOBCONST:
16784 {
16785 unsigned char *blob;
16786 size_t size, i;
16787 size = size_of(state, ins->type);
16788 blob = ins->u.blob;
16789 for(i = 0; i < size; i++) {
16790 fprintf(fp, ".byte 0x%02x\n",
16791 blob[i]);
16792 }
16793 break;
16794 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016795 default:
16796 internal_error(state, ins, "Unknown constant type");
16797 break;
16798 }
16799}
16800
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016801#define TEXT_SECTION ".rom.text"
16802#define DATA_SECTION ".rom.data"
16803
Eric Biedermanb138ac82003-04-22 18:44:01 +000016804static void print_sdecl(struct compile_state *state,
16805 struct triple *ins, FILE *fp)
16806{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016807 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016808 fprintf(fp, ".balign %d\n", align_of(state, ins->type));
Eric Biederman05f26fc2003-06-11 21:55:00 +000016809 fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016810 print_const(state, MISC(ins, 0), fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016811 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016812
16813}
16814
16815static void print_instruction(struct compile_state *state,
16816 struct triple *ins, FILE *fp)
16817{
16818 /* Assumption: after I have exted the register allocator
16819 * everything is in a valid register.
16820 */
16821 switch(ins->op) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016822 case OP_ASM:
16823 print_op_asm(state, ins, fp);
16824 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016825 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
16826 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
16827 case OP_AND: print_binary_op(state, "and", ins, fp); break;
16828 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
16829 case OP_OR: print_binary_op(state, "or", ins, fp); break;
16830 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
16831 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
16832 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
16833 case OP_POS: break;
16834 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
16835 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
16836 case OP_INTCONST:
16837 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016838 case OP_BLOBCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +000016839 /* Don't generate anything here for constants */
16840 case OP_PHI:
16841 /* Don't generate anything for variable declarations. */
16842 break;
16843 case OP_SDECL:
16844 print_sdecl(state, ins, fp);
16845 break;
16846 case OP_WRITE:
16847 case OP_COPY:
16848 print_op_move(state, ins, fp);
16849 break;
16850 case OP_LOAD:
16851 print_op_load(state, ins, fp);
16852 break;
16853 case OP_STORE:
16854 print_op_store(state, ins, fp);
16855 break;
16856 case OP_SMUL:
16857 print_op_smul(state, ins, fp);
16858 break;
16859 case OP_CMP: print_op_cmp(state, ins, fp); break;
16860 case OP_TEST: print_op_test(state, ins, fp); break;
16861 case OP_JMP:
16862 case OP_JMP_EQ: case OP_JMP_NOTEQ:
16863 case OP_JMP_SLESS: case OP_JMP_ULESS:
16864 case OP_JMP_SMORE: case OP_JMP_UMORE:
16865 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
16866 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
16867 print_op_branch(state, ins, fp);
16868 break;
16869 case OP_SET_EQ: case OP_SET_NOTEQ:
16870 case OP_SET_SLESS: case OP_SET_ULESS:
16871 case OP_SET_SMORE: case OP_SET_UMORE:
16872 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
16873 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
16874 print_op_set(state, ins, fp);
16875 break;
16876 case OP_INB: case OP_INW: case OP_INL:
16877 print_op_in(state, ins, fp);
16878 break;
16879 case OP_OUTB: case OP_OUTW: case OP_OUTL:
16880 print_op_out(state, ins, fp);
16881 break;
16882 case OP_BSF:
16883 case OP_BSR:
16884 print_op_bit_scan(state, ins, fp);
16885 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016886 case OP_RDMSR:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016887 after_lhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016888 fprintf(fp, "\trdmsr\n");
16889 break;
16890 case OP_WRMSR:
16891 fprintf(fp, "\twrmsr\n");
16892 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016893 case OP_HLT:
16894 fprintf(fp, "\thlt\n");
16895 break;
16896 case OP_LABEL:
16897 if (!ins->use) {
16898 return;
16899 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016900 fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016901 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016902 /* Ignore OP_PIECE */
16903 case OP_PIECE:
16904 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016905 /* Operations I am not yet certain how to handle */
16906 case OP_UMUL:
16907 case OP_SDIV: case OP_UDIV:
16908 case OP_SMOD: case OP_UMOD:
16909 /* Operations that should never get here */
16910 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
16911 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
16912 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
16913 default:
16914 internal_error(state, ins, "unknown op: %d %s",
16915 ins->op, tops(ins->op));
16916 break;
16917 }
16918}
16919
16920static void print_instructions(struct compile_state *state)
16921{
16922 struct triple *first, *ins;
16923 int print_location;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000016924 struct occurance *last_occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016925 FILE *fp;
16926 print_location = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000016927 last_occurance = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016928 fp = state->output;
16929 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biederman0babc1c2003-05-09 02:39:00 +000016930 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016931 ins = first;
16932 do {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000016933 if (print_location &&
16934 last_occurance != ins->occurance) {
16935 if (!ins->occurance->parent) {
16936 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
16937 ins->occurance->function,
16938 ins->occurance->filename,
16939 ins->occurance->line,
16940 ins->occurance->col);
16941 }
16942 else {
16943 struct occurance *ptr;
16944 fprintf(fp, "\t/*\n");
16945 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
16946 fprintf(fp, "\t * %s,%s:%d.%d\n",
16947 ptr->function,
16948 ptr->filename,
16949 ptr->line,
16950 ptr->col);
16951 }
16952 fprintf(fp, "\t */\n");
16953
16954 }
16955 if (last_occurance) {
16956 put_occurance(last_occurance);
16957 }
16958 get_occurance(ins->occurance);
16959 last_occurance = ins->occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016960 }
16961
16962 print_instruction(state, ins, fp);
16963 ins = ins->next;
16964 } while(ins != first);
16965
16966}
16967static void generate_code(struct compile_state *state)
16968{
16969 generate_local_labels(state);
16970 print_instructions(state);
16971
16972}
16973
16974static void print_tokens(struct compile_state *state)
16975{
16976 struct token *tk;
16977 tk = &state->token[0];
16978 do {
16979#if 1
16980 token(state, 0);
16981#else
16982 next_token(state, 0);
16983#endif
16984 loc(stdout, state, 0);
16985 printf("%s <- `%s'\n",
16986 tokens[tk->tok],
16987 tk->ident ? tk->ident->name :
16988 tk->str_len ? tk->val.str : "");
16989
16990 } while(tk->tok != TOK_EOF);
16991}
16992
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016993static void compile(const char *filename, const char *ofilename,
Eric Biederman05f26fc2003-06-11 21:55:00 +000016994 int cpu, int debug, int opt, const char *label_prefix)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016995{
16996 int i;
16997 struct compile_state state;
16998 memset(&state, 0, sizeof(state));
16999 state.file = 0;
17000 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
17001 memset(&state.token[i], 0, sizeof(state.token[i]));
17002 state.token[i].tok = -1;
17003 }
17004 /* Remember the debug settings */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017005 state.cpu = cpu;
17006 state.debug = debug;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017007 state.optimize = opt;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017008 /* Remember the output filename */
17009 state.ofilename = ofilename;
17010 state.output = fopen(state.ofilename, "w");
17011 if (!state.output) {
17012 error(&state, 0, "Cannot open output file %s\n",
17013 ofilename);
17014 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000017015 /* Remember the label prefix */
17016 state.label_prefix = label_prefix;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017017 /* Prep the preprocessor */
17018 state.if_depth = 0;
17019 state.if_value = 0;
17020 /* register the C keywords */
17021 register_keywords(&state);
17022 /* register the keywords the macro preprocessor knows */
17023 register_macro_keywords(&state);
17024 /* Memorize where some special keywords are. */
17025 state.i_continue = lookup(&state, "continue", 8);
17026 state.i_break = lookup(&state, "break", 5);
17027 /* Enter the globl definition scope */
17028 start_scope(&state);
17029 register_builtins(&state);
17030 compile_file(&state, filename, 1);
17031#if 0
17032 print_tokens(&state);
17033#endif
17034 decls(&state);
17035 /* Exit the global definition scope */
17036 end_scope(&state);
17037
17038 /* Now that basic compilation has happened
17039 * optimize the intermediate code
17040 */
17041 optimize(&state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017042
Eric Biedermanb138ac82003-04-22 18:44:01 +000017043 generate_code(&state);
17044 if (state.debug) {
17045 fprintf(stderr, "done\n");
17046 }
17047}
17048
17049static void version(void)
17050{
17051 printf("romcc " VERSION " released " RELEASE_DATE "\n");
17052}
17053
17054static void usage(void)
17055{
17056 version();
17057 printf(
17058 "Usage: romcc <source>.c\n"
17059 "Compile a C source file without using ram\n"
17060 );
17061}
17062
17063static void arg_error(char *fmt, ...)
17064{
17065 va_list args;
17066 va_start(args, fmt);
17067 vfprintf(stderr, fmt, args);
17068 va_end(args);
17069 usage();
17070 exit(1);
17071}
17072
17073int main(int argc, char **argv)
17074{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017075 const char *filename;
17076 const char *ofilename;
Eric Biederman05f26fc2003-06-11 21:55:00 +000017077 const char *label_prefix;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017078 int cpu;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017079 int last_argc;
17080 int debug;
17081 int optimize;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017082 cpu = CPU_DEFAULT;
Eric Biederman05f26fc2003-06-11 21:55:00 +000017083 label_prefix = "";
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017084 ofilename = "auto.inc";
Eric Biedermanb138ac82003-04-22 18:44:01 +000017085 optimize = 0;
17086 debug = 0;
17087 last_argc = -1;
17088 while((argc > 1) && (argc != last_argc)) {
17089 last_argc = argc;
17090 if (strncmp(argv[1], "--debug=", 8) == 0) {
17091 debug = atoi(argv[1] + 8);
17092 argv++;
17093 argc--;
17094 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000017095 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
17096 label_prefix= argv[1] + 15;
17097 argv++;
17098 argc--;
17099 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017100 else if ((strcmp(argv[1],"-O") == 0) ||
17101 (strcmp(argv[1], "-O1") == 0)) {
17102 optimize = 1;
17103 argv++;
17104 argc--;
17105 }
17106 else if (strcmp(argv[1],"-O2") == 0) {
17107 optimize = 2;
17108 argv++;
17109 argc--;
17110 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017111 else if ((strcmp(argv[1], "-o") == 0) && (argc > 2)) {
17112 ofilename = argv[2];
17113 argv += 2;
17114 argc -= 2;
17115 }
17116 else if (strncmp(argv[1], "-mcpu=", 6) == 0) {
17117 cpu = arch_encode_cpu(argv[1] + 6);
17118 if (cpu == BAD_CPU) {
17119 arg_error("Invalid cpu specified: %s\n",
17120 argv[1] + 6);
17121 }
17122 argv++;
17123 argc--;
17124 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017125 }
17126 if (argc != 2) {
17127 arg_error("Wrong argument count %d\n", argc);
17128 }
17129 filename = argv[1];
Eric Biederman05f26fc2003-06-11 21:55:00 +000017130 compile(filename, ofilename, cpu, debug, optimize, label_prefix);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017131
17132 return 0;
17133}