blob: 5c784fc5e67c94c2536713a0d26b36585798fb51 [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 Biederman6aa31cc2003-06-10 21:22:07 +000017#define DEBUG_CONSISTENCY 1
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#if 1
862#define REGISTER_BITS 16
863#else
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000864#define REGISTER_BITS 28
Eric Biedermanf96a8102003-06-16 16:57:34 +0000865#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000866#define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
867#define TEMPLATE_BITS 6
868#define MAX_TEMPLATES (1<<TEMPLATE_BITS)
Eric Biedermanb138ac82003-04-22 18:44:01 +0000869#define MAX_REGC 12
870#define REG_UNSET 0
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000871#define REG_UNNEEDED 1
872#define REG_VIRT0 (MAX_REGISTERS + 0)
873#define REG_VIRT1 (MAX_REGISTERS + 1)
874#define REG_VIRT2 (MAX_REGISTERS + 2)
875#define REG_VIRT3 (MAX_REGISTERS + 3)
876#define REG_VIRT4 (MAX_REGISTERS + 4)
877#define REG_VIRT5 (MAX_REGISTERS + 5)
Eric Biederman8d9c1232003-06-17 08:42:17 +0000878#define REG_VIRT6 (MAX_REGISTERS + 5)
879#define REG_VIRT7 (MAX_REGISTERS + 5)
880#define REG_VIRT8 (MAX_REGISTERS + 5)
881#define REG_VIRT9 (MAX_REGISTERS + 5)
Eric Biedermanb138ac82003-04-22 18:44:01 +0000882
883/* Provision for 8 register classes */
Eric Biedermanf96a8102003-06-16 16:57:34 +0000884#if 1
885#define REG_SHIFT 0
886#define REGC_SHIFT REGISTER_BITS
887#define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000888#define REG_MASK (MAX_VIRT_REGISTERS -1)
889#define ID_REG(ID) ((ID) & REG_MASK)
890#define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
Eric Biedermanf96a8102003-06-16 16:57:34 +0000891#define ID_REGCM(ID) (((ID) & REGC_MASK) >> REGC_SHIFT)
892#define SET_REGCM(ID, REGCM) ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
893#define SET_INFO(ID, INFO) ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
894 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
895#else
896#define REG_MASK (MAX_VIRT_REGISTERS -1)
897#define ID_REG(ID) ((ID) & REG_MASK)
898#define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
899#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +0000900
901static unsigned arch_reg_regcm(struct compile_state *state, int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000902static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000903static void arch_reg_equivs(
904 struct compile_state *state, unsigned *equiv, int reg);
905static int arch_select_free_register(
906 struct compile_state *state, char *used, int classes);
907static unsigned arch_regc_size(struct compile_state *state, int class);
908static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
909static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
910static const char *arch_reg_str(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000911static struct reg_info arch_reg_constraint(
912 struct compile_state *state, struct type *type, const char *constraint);
913static struct reg_info arch_reg_clobber(
914 struct compile_state *state, const char *clobber);
915static struct reg_info arch_reg_lhs(struct compile_state *state,
916 struct triple *ins, int index);
917static struct reg_info arch_reg_rhs(struct compile_state *state,
918 struct triple *ins, int index);
919static struct triple *transform_to_arch_instruction(
920 struct compile_state *state, struct triple *ins);
921
922
Eric Biedermanb138ac82003-04-22 18:44:01 +0000923
Eric Biederman0babc1c2003-05-09 02:39:00 +0000924#define DEBUG_ABORT_ON_ERROR 0x0001
925#define DEBUG_INTERMEDIATE_CODE 0x0002
926#define DEBUG_CONTROL_FLOW 0x0004
927#define DEBUG_BASIC_BLOCKS 0x0008
928#define DEBUG_FDOMINATORS 0x0010
929#define DEBUG_RDOMINATORS 0x0020
930#define DEBUG_TRIPLES 0x0040
931#define DEBUG_INTERFERENCE 0x0080
932#define DEBUG_ARCH_CODE 0x0100
933#define DEBUG_CODE_ELIMINATION 0x0200
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000934#define DEBUG_INSERTED_COPIES 0x0400
Eric Biedermanb138ac82003-04-22 18:44:01 +0000935
936#define GLOBAL_SCOPE_DEPTH 1
937
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000938static void compile_file(struct compile_state *old_state, const char *filename, int local);
939
940static void do_cleanup(struct compile_state *state)
941{
942 if (state->output) {
943 fclose(state->output);
944 unlink(state->ofilename);
945 }
946}
Eric Biedermanb138ac82003-04-22 18:44:01 +0000947
948static int get_col(struct file_state *file)
949{
950 int col;
951 char *ptr, *end;
952 ptr = file->line_start;
953 end = file->pos;
954 for(col = 0; ptr < end; ptr++) {
955 if (*ptr != '\t') {
956 col++;
957 }
958 else {
959 col = (col & ~7) + 8;
960 }
961 }
962 return col;
963}
964
965static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
966{
967 int col;
968 if (triple) {
969 fprintf(fp, "%s:%d.%d: ",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000970 triple->occurance->filename,
971 triple->occurance->line,
972 triple->occurance->col);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000973 return;
974 }
975 if (!state->file) {
976 return;
977 }
978 col = get_col(state->file);
979 fprintf(fp, "%s:%d.%d: ",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000980 state->file->report_name, state->file->report_line, col);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000981}
982
983static void __internal_error(struct compile_state *state, struct triple *ptr,
984 char *fmt, ...)
985{
986 va_list args;
987 va_start(args, fmt);
988 loc(stderr, state, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000989 if (ptr) {
990 fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
991 }
Eric Biedermanb138ac82003-04-22 18:44:01 +0000992 fprintf(stderr, "Internal compiler error: ");
993 vfprintf(stderr, fmt, args);
994 fprintf(stderr, "\n");
995 va_end(args);
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000996 do_cleanup(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000997 abort();
998}
999
1000
1001static void __internal_warning(struct compile_state *state, struct triple *ptr,
1002 char *fmt, ...)
1003{
1004 va_list args;
1005 va_start(args, fmt);
1006 loc(stderr, state, ptr);
1007 fprintf(stderr, "Internal compiler warning: ");
1008 vfprintf(stderr, fmt, args);
1009 fprintf(stderr, "\n");
1010 va_end(args);
1011}
1012
1013
1014
1015static void __error(struct compile_state *state, struct triple *ptr,
1016 char *fmt, ...)
1017{
1018 va_list args;
1019 va_start(args, fmt);
1020 loc(stderr, state, ptr);
1021 vfprintf(stderr, fmt, args);
1022 va_end(args);
1023 fprintf(stderr, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001024 do_cleanup(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001025 if (state->debug & DEBUG_ABORT_ON_ERROR) {
1026 abort();
1027 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001028 exit(1);
1029}
1030
1031static void __warning(struct compile_state *state, struct triple *ptr,
1032 char *fmt, ...)
1033{
1034 va_list args;
1035 va_start(args, fmt);
1036 loc(stderr, state, ptr);
1037 fprintf(stderr, "warning: ");
1038 vfprintf(stderr, fmt, args);
1039 fprintf(stderr, "\n");
1040 va_end(args);
1041}
1042
1043#if DEBUG_ERROR_MESSAGES
1044# define internal_error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_error
1045# define internal_warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_warning
1046# define error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__error
1047# define warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__warning
1048#else
1049# define internal_error __internal_error
1050# define internal_warning __internal_warning
1051# define error __error
1052# define warning __warning
1053#endif
1054#define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1055
Eric Biederman0babc1c2003-05-09 02:39:00 +00001056static void valid_op(struct compile_state *state, int op)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001057{
1058 char *fmt = "invalid op: %d";
Eric Biederman0babc1c2003-05-09 02:39:00 +00001059 if (op >= OP_MAX) {
1060 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001061 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001062 if (op < 0) {
1063 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001064 }
1065}
1066
Eric Biederman0babc1c2003-05-09 02:39:00 +00001067static void valid_ins(struct compile_state *state, struct triple *ptr)
1068{
1069 valid_op(state, ptr->op);
1070}
1071
Eric Biedermanb138ac82003-04-22 18:44:01 +00001072static void process_trigraphs(struct compile_state *state)
1073{
1074 char *src, *dest, *end;
1075 struct file_state *file;
1076 file = state->file;
1077 src = dest = file->buf;
1078 end = file->buf + file->size;
1079 while((end - src) >= 3) {
1080 if ((src[0] == '?') && (src[1] == '?')) {
1081 int c = -1;
1082 switch(src[2]) {
1083 case '=': c = '#'; break;
1084 case '/': c = '\\'; break;
1085 case '\'': c = '^'; break;
1086 case '(': c = '['; break;
1087 case ')': c = ']'; break;
1088 case '!': c = '!'; break;
1089 case '<': c = '{'; break;
1090 case '>': c = '}'; break;
1091 case '-': c = '~'; break;
1092 }
1093 if (c != -1) {
1094 *dest++ = c;
1095 src += 3;
1096 }
1097 else {
1098 *dest++ = *src++;
1099 }
1100 }
1101 else {
1102 *dest++ = *src++;
1103 }
1104 }
1105 while(src != end) {
1106 *dest++ = *src++;
1107 }
1108 file->size = dest - file->buf;
1109}
1110
1111static void splice_lines(struct compile_state *state)
1112{
1113 char *src, *dest, *end;
1114 struct file_state *file;
1115 file = state->file;
1116 src = dest = file->buf;
1117 end = file->buf + file->size;
1118 while((end - src) >= 2) {
1119 if ((src[0] == '\\') && (src[1] == '\n')) {
1120 src += 2;
1121 }
1122 else {
1123 *dest++ = *src++;
1124 }
1125 }
1126 while(src != end) {
1127 *dest++ = *src++;
1128 }
1129 file->size = dest - file->buf;
1130}
1131
1132static struct type void_type;
1133static void use_triple(struct triple *used, struct triple *user)
1134{
1135 struct triple_set **ptr, *new;
1136 if (!used)
1137 return;
1138 if (!user)
1139 return;
1140 ptr = &used->use;
1141 while(*ptr) {
1142 if ((*ptr)->member == user) {
1143 return;
1144 }
1145 ptr = &(*ptr)->next;
1146 }
1147 /* Append new to the head of the list,
1148 * copy_func and rename_block_variables
1149 * depends on this.
1150 */
1151 new = xcmalloc(sizeof(*new), "triple_set");
1152 new->member = user;
1153 new->next = used->use;
1154 used->use = new;
1155}
1156
1157static void unuse_triple(struct triple *used, struct triple *unuser)
1158{
1159 struct triple_set *use, **ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001160 if (!used) {
1161 return;
1162 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001163 ptr = &used->use;
1164 while(*ptr) {
1165 use = *ptr;
1166 if (use->member == unuser) {
1167 *ptr = use->next;
1168 xfree(use);
1169 }
1170 else {
1171 ptr = &use->next;
1172 }
1173 }
1174}
1175
1176static void push_triple(struct triple *used, struct triple *user)
1177{
1178 struct triple_set *new;
1179 if (!used)
1180 return;
1181 if (!user)
1182 return;
1183 /* Append new to the head of the list,
1184 * it's the only sensible behavoir for a stack.
1185 */
1186 new = xcmalloc(sizeof(*new), "triple_set");
1187 new->member = user;
1188 new->next = used->use;
1189 used->use = new;
1190}
1191
1192static void pop_triple(struct triple *used, struct triple *unuser)
1193{
1194 struct triple_set *use, **ptr;
1195 ptr = &used->use;
1196 while(*ptr) {
1197 use = *ptr;
1198 if (use->member == unuser) {
1199 *ptr = use->next;
1200 xfree(use);
1201 /* Only free one occurance from the stack */
1202 return;
1203 }
1204 else {
1205 ptr = &use->next;
1206 }
1207 }
1208}
1209
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001210static void put_occurance(struct occurance *occurance)
1211{
1212 occurance->count -= 1;
1213 if (occurance->count <= 0) {
1214 if (occurance->parent) {
1215 put_occurance(occurance->parent);
1216 }
1217 xfree(occurance);
1218 }
1219}
1220
1221static void get_occurance(struct occurance *occurance)
1222{
1223 occurance->count += 1;
1224}
1225
1226
1227static struct occurance *new_occurance(struct compile_state *state)
1228{
1229 struct occurance *result, *last;
1230 const char *filename;
1231 const char *function;
1232 int line, col;
1233
1234 function = "";
1235 filename = 0;
1236 line = 0;
1237 col = 0;
1238 if (state->file) {
1239 filename = state->file->report_name;
1240 line = state->file->report_line;
1241 col = get_col(state->file);
1242 }
1243 if (state->function) {
1244 function = state->function;
1245 }
1246 last = state->last_occurance;
1247 if (last &&
1248 (last->col == col) &&
1249 (last->line == line) &&
1250 (last->function == function) &&
1251 (strcmp(last->filename, filename) == 0)) {
1252 get_occurance(last);
1253 return last;
1254 }
1255 if (last) {
1256 state->last_occurance = 0;
1257 put_occurance(last);
1258 }
1259 result = xmalloc(sizeof(*result), "occurance");
1260 result->count = 2;
1261 result->filename = filename;
1262 result->function = function;
1263 result->line = line;
1264 result->col = col;
1265 result->parent = 0;
1266 state->last_occurance = result;
1267 return result;
1268}
1269
1270static struct occurance *inline_occurance(struct compile_state *state,
1271 struct occurance *new, struct occurance *orig)
1272{
1273 struct occurance *result, *last;
1274 last = state->last_occurance;
1275 if (last &&
1276 (last->parent == orig) &&
1277 (last->col == new->col) &&
1278 (last->line == new->line) &&
1279 (last->function == new->function) &&
1280 (last->filename == new->filename)) {
1281 get_occurance(last);
1282 return last;
1283 }
1284 if (last) {
1285 state->last_occurance = 0;
1286 put_occurance(last);
1287 }
1288 get_occurance(orig);
1289 result = xmalloc(sizeof(*result), "occurance");
1290 result->count = 2;
1291 result->filename = new->filename;
1292 result->function = new->function;
1293 result->line = new->line;
1294 result->col = new->col;
1295 result->parent = orig;
1296 state->last_occurance = result;
1297 return result;
1298}
1299
1300
1301static struct occurance dummy_occurance = {
1302 .count = 2,
1303 .filename = __FILE__,
1304 .function = "",
1305 .line = __LINE__,
1306 .col = 0,
1307 .parent = 0,
1308};
Eric Biedermanb138ac82003-04-22 18:44:01 +00001309
1310/* The zero triple is used as a place holder when we are removing pointers
1311 * from a triple. Having allows certain sanity checks to pass even
1312 * when the original triple that was pointed to is gone.
1313 */
1314static struct triple zero_triple = {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001315 .next = &zero_triple,
1316 .prev = &zero_triple,
1317 .use = 0,
1318 .op = OP_INTCONST,
1319 .sizes = TRIPLE_SIZES(0, 0, 0, 0),
1320 .id = -1, /* An invalid id */
Eric Biedermanb138ac82003-04-22 18:44:01 +00001321 .u = { .cval = 0, },
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001322 .occurance = &dummy_occurance,
Eric Biederman0babc1c2003-05-09 02:39:00 +00001323 .param { [0] = 0, [1] = 0, },
Eric Biedermanb138ac82003-04-22 18:44:01 +00001324};
1325
Eric Biederman0babc1c2003-05-09 02:39:00 +00001326
1327static unsigned short triple_sizes(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001328 int op, struct type *type, int lhs_wanted, int rhs_wanted)
Eric Biederman0babc1c2003-05-09 02:39:00 +00001329{
1330 int lhs, rhs, misc, targ;
1331 valid_op(state, op);
1332 lhs = table_ops[op].lhs;
1333 rhs = table_ops[op].rhs;
1334 misc = table_ops[op].misc;
1335 targ = table_ops[op].targ;
1336
1337
1338 if (op == OP_CALL) {
1339 struct type *param;
1340 rhs = 0;
1341 param = type->right;
1342 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
1343 rhs++;
1344 param = param->right;
1345 }
1346 if ((param->type & TYPE_MASK) != TYPE_VOID) {
1347 rhs++;
1348 }
1349 lhs = 0;
1350 if ((type->left->type & TYPE_MASK) == TYPE_STRUCT) {
1351 lhs = type->left->elements;
1352 }
1353 }
1354 else if (op == OP_VAL_VEC) {
1355 rhs = type->elements;
1356 }
1357 else if ((op == OP_BRANCH) || (op == OP_PHI)) {
1358 rhs = rhs_wanted;
1359 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001360 else if (op == OP_ASM) {
1361 rhs = rhs_wanted;
1362 lhs = lhs_wanted;
1363 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001364 if ((rhs < 0) || (rhs > MAX_RHS)) {
1365 internal_error(state, 0, "bad rhs");
1366 }
1367 if ((lhs < 0) || (lhs > MAX_LHS)) {
1368 internal_error(state, 0, "bad lhs");
1369 }
1370 if ((misc < 0) || (misc > MAX_MISC)) {
1371 internal_error(state, 0, "bad misc");
1372 }
1373 if ((targ < 0) || (targ > MAX_TARG)) {
1374 internal_error(state, 0, "bad targs");
1375 }
1376 return TRIPLE_SIZES(lhs, rhs, misc, targ);
1377}
1378
1379static struct triple *alloc_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001380 int op, struct type *type, int lhs, int rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001381 struct occurance *occurance)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001382{
Eric Biederman0babc1c2003-05-09 02:39:00 +00001383 size_t size, sizes, extra_count, min_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001384 struct triple *ret;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001385 sizes = triple_sizes(state, op, type, lhs, rhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001386
1387 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
1388 extra_count = TRIPLE_SIZE(sizes);
1389 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
1390
1391 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
1392 ret = xcmalloc(size, "tripple");
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001393 ret->op = op;
1394 ret->sizes = sizes;
1395 ret->type = type;
1396 ret->next = ret;
1397 ret->prev = ret;
1398 ret->occurance = occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001399 return ret;
1400}
1401
Eric Biederman0babc1c2003-05-09 02:39:00 +00001402struct triple *dup_triple(struct compile_state *state, struct triple *src)
1403{
1404 struct triple *dup;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001405 int src_lhs, src_rhs, src_size;
1406 src_lhs = TRIPLE_LHS(src->sizes);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001407 src_rhs = TRIPLE_RHS(src->sizes);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001408 src_size = TRIPLE_SIZE(src->sizes);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001409 get_occurance(src->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001410 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001411 src->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001412 memcpy(dup, src, sizeof(*src));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001413 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
Eric Biederman0babc1c2003-05-09 02:39:00 +00001414 return dup;
1415}
1416
1417static struct triple *new_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001418 int op, struct type *type, int lhs, int rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001419{
1420 struct triple *ret;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001421 struct occurance *occurance;
1422 occurance = new_occurance(state);
1423 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001424 return ret;
1425}
1426
1427static struct triple *build_triple(struct compile_state *state,
1428 int op, struct type *type, struct triple *left, struct triple *right,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001429 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00001430{
1431 struct triple *ret;
1432 size_t count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001433 ret = alloc_triple(state, op, type, -1, -1, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001434 count = TRIPLE_SIZE(ret->sizes);
1435 if (count > 0) {
1436 ret->param[0] = left;
1437 }
1438 if (count > 1) {
1439 ret->param[1] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001440 }
1441 return ret;
1442}
1443
Eric Biederman0babc1c2003-05-09 02:39:00 +00001444static struct triple *triple(struct compile_state *state,
1445 int op, struct type *type, struct triple *left, struct triple *right)
1446{
1447 struct triple *ret;
1448 size_t count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001449 ret = new_triple(state, op, type, -1, -1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001450 count = TRIPLE_SIZE(ret->sizes);
1451 if (count >= 1) {
1452 ret->param[0] = left;
1453 }
1454 if (count >= 2) {
1455 ret->param[1] = right;
1456 }
1457 return ret;
1458}
1459
1460static struct triple *branch(struct compile_state *state,
1461 struct triple *targ, struct triple *test)
1462{
1463 struct triple *ret;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001464 ret = new_triple(state, OP_BRANCH, &void_type, -1, test?1:0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001465 if (test) {
1466 RHS(ret, 0) = test;
1467 }
1468 TARG(ret, 0) = targ;
1469 /* record the branch target was used */
1470 if (!targ || (targ->op != OP_LABEL)) {
1471 internal_error(state, 0, "branch not to label");
1472 use_triple(targ, ret);
1473 }
1474 return ret;
1475}
1476
1477
Eric Biedermanb138ac82003-04-22 18:44:01 +00001478static void insert_triple(struct compile_state *state,
1479 struct triple *first, struct triple *ptr)
1480{
1481 if (ptr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001482 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00001483 internal_error(state, ptr, "expression already used");
1484 }
1485 ptr->next = first;
1486 ptr->prev = first->prev;
1487 ptr->prev->next = ptr;
1488 ptr->next->prev = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00001489 if ((ptr->prev->op == OP_BRANCH) &&
1490 TRIPLE_RHS(ptr->prev->sizes)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00001491 unuse_triple(first, ptr->prev);
1492 use_triple(ptr, ptr->prev);
1493 }
1494 }
1495}
1496
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001497static int triple_stores_block(struct compile_state *state, struct triple *ins)
1498{
1499 /* This function is used to determine if u.block
1500 * is utilized to store the current block number.
1501 */
1502 int stores_block;
1503 valid_ins(state, ins);
1504 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
1505 return stores_block;
1506}
1507
1508static struct block *block_of_triple(struct compile_state *state,
1509 struct triple *ins)
1510{
1511 struct triple *first;
1512 first = RHS(state->main_function, 0);
1513 while(ins != first && !triple_stores_block(state, ins)) {
1514 if (ins == ins->prev) {
1515 internal_error(state, 0, "ins == ins->prev?");
1516 }
1517 ins = ins->prev;
1518 }
1519 if (!triple_stores_block(state, ins)) {
1520 internal_error(state, ins, "Cannot find block");
1521 }
1522 return ins->u.block;
1523}
1524
Eric Biedermanb138ac82003-04-22 18:44:01 +00001525static struct triple *pre_triple(struct compile_state *state,
1526 struct triple *base,
1527 int op, struct type *type, struct triple *left, struct triple *right)
1528{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001529 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001530 struct triple *ret;
Eric Biedermand3283ec2003-06-18 11:03:18 +00001531 /* If I am an OP_PIECE jump to the real instruction */
1532 if (base->op == OP_PIECE) {
1533 base = MISC(base, 0);
1534 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001535 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001536 get_occurance(base->occurance);
1537 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001538 if (triple_stores_block(state, ret)) {
1539 ret->u.block = block;
1540 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001541 insert_triple(state, base, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001542 if (block->first == base) {
1543 block->first = ret;
1544 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001545 return ret;
1546}
1547
1548static struct triple *post_triple(struct compile_state *state,
1549 struct triple *base,
1550 int op, struct type *type, struct triple *left, struct triple *right)
1551{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001552 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001553 struct triple *ret;
Eric Biedermand3283ec2003-06-18 11:03:18 +00001554 int zlhs;
1555 /* If I am an OP_PIECE jump to the real instruction */
1556 if (base->op == OP_PIECE) {
1557 base = MISC(base, 0);
1558 }
1559 /* If I have a left hand side skip over it */
1560 zlhs = TRIPLE_LHS(base->sizes);
1561 if (zlhs && (base->op != OP_WRITE) && (base->op != OP_STORE)) {
1562 base = LHS(base, zlhs - 1);
1563 }
1564
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001565 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001566 get_occurance(base->occurance);
1567 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001568 if (triple_stores_block(state, ret)) {
1569 ret->u.block = block;
1570 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001571 insert_triple(state, base->next, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001572 if (block->last == base) {
1573 block->last = ret;
1574 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001575 return ret;
1576}
1577
1578static struct triple *label(struct compile_state *state)
1579{
1580 /* Labels don't get a type */
1581 struct triple *result;
1582 result = triple(state, OP_LABEL, &void_type, 0, 0);
1583 return result;
1584}
1585
Eric Biederman0babc1c2003-05-09 02:39:00 +00001586static void display_triple(FILE *fp, struct triple *ins)
1587{
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001588 struct occurance *ptr;
1589 const char *reg;
1590 char pre, post;
1591 pre = post = ' ';
1592 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
1593 pre = '^';
1594 }
1595 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
1596 post = 'v';
1597 }
1598 reg = arch_reg_str(ID_REG(ins->id));
Eric Biederman0babc1c2003-05-09 02:39:00 +00001599 if (ins->op == OP_INTCONST) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001600 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s <0x%08lx> ",
1601 ins, pre, post, reg, ins->template_id, tops(ins->op),
1602 ins->u.cval);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001603 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001604 else if (ins->op == OP_ADDRCONST) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001605 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s %-10p <0x%08lx>",
1606 ins, pre, post, reg, ins->template_id, tops(ins->op),
1607 MISC(ins, 0), ins->u.cval);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001608 }
1609 else {
1610 int i, count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001611 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s",
1612 ins, pre, post, reg, ins->template_id, tops(ins->op));
Eric Biederman0babc1c2003-05-09 02:39:00 +00001613 count = TRIPLE_SIZE(ins->sizes);
1614 for(i = 0; i < count; i++) {
1615 fprintf(fp, " %-10p", ins->param[i]);
1616 }
1617 for(; i < 2; i++) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00001618 fprintf(fp, " ");
Eric Biederman0babc1c2003-05-09 02:39:00 +00001619 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001620 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001621 fprintf(fp, " @");
1622 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
1623 fprintf(fp, " %s,%s:%d.%d",
1624 ptr->function,
1625 ptr->filename,
1626 ptr->line,
1627 ptr->col);
1628 }
1629 fprintf(fp, "\n");
Eric Biederman0babc1c2003-05-09 02:39:00 +00001630 fflush(fp);
1631}
1632
Eric Biedermanb138ac82003-04-22 18:44:01 +00001633static int triple_is_pure(struct compile_state *state, struct triple *ins)
1634{
1635 /* Does the triple have no side effects.
1636 * I.e. Rexecuting the triple with the same arguments
1637 * gives the same value.
1638 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001639 unsigned pure;
1640 valid_ins(state, ins);
1641 pure = PURE_BITS(table_ops[ins->op].flags);
1642 if ((pure != PURE) && (pure != IMPURE)) {
1643 internal_error(state, 0, "Purity of %s not known\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +00001644 tops(ins->op));
Eric Biedermanb138ac82003-04-22 18:44:01 +00001645 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001646 return pure == PURE;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001647}
1648
Eric Biederman0babc1c2003-05-09 02:39:00 +00001649static int triple_is_branch(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001650{
1651 /* This function is used to determine which triples need
1652 * a register.
1653 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001654 int is_branch;
1655 valid_ins(state, ins);
1656 is_branch = (table_ops[ins->op].targ != 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001657 return is_branch;
1658}
1659
Eric Biederman0babc1c2003-05-09 02:39:00 +00001660static int triple_is_def(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001661{
1662 /* This function is used to determine which triples need
1663 * a register.
1664 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001665 int is_def;
1666 valid_ins(state, ins);
1667 is_def = (table_ops[ins->op].flags & DEF) == DEF;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001668 return is_def;
1669}
1670
Eric Biederman0babc1c2003-05-09 02:39:00 +00001671static struct triple **triple_iter(struct compile_state *state,
1672 size_t count, struct triple **vector,
1673 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001674{
1675 struct triple **ret;
1676 ret = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00001677 if (count) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00001678 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001679 ret = vector;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001680 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001681 else if ((last >= vector) && (last < (vector + count - 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00001682 ret = last + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001683 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001684 }
1685 return ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00001686
Eric Biedermanb138ac82003-04-22 18:44:01 +00001687}
1688
1689static struct triple **triple_lhs(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00001690 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001691{
Eric Biederman0babc1c2003-05-09 02:39:00 +00001692 return triple_iter(state, TRIPLE_LHS(ins->sizes), &LHS(ins,0),
1693 ins, last);
1694}
1695
1696static struct triple **triple_rhs(struct compile_state *state,
1697 struct triple *ins, struct triple **last)
1698{
1699 return triple_iter(state, TRIPLE_RHS(ins->sizes), &RHS(ins,0),
1700 ins, last);
1701}
1702
1703static struct triple **triple_misc(struct compile_state *state,
1704 struct triple *ins, struct triple **last)
1705{
1706 return triple_iter(state, TRIPLE_MISC(ins->sizes), &MISC(ins,0),
1707 ins, last);
1708}
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001709
Eric Biederman0babc1c2003-05-09 02:39:00 +00001710static struct triple **triple_targ(struct compile_state *state,
1711 struct triple *ins, struct triple **last)
1712{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001713 size_t count;
1714 struct triple **ret, **vector;
1715 ret = 0;
1716 count = TRIPLE_TARG(ins->sizes);
1717 vector = &TARG(ins, 0);
1718 if (count) {
1719 if (!last) {
1720 ret = vector;
1721 }
1722 else if ((last >= vector) && (last < (vector + count - 1))) {
1723 ret = last + 1;
1724 }
1725 else if ((last == (vector + count - 1)) &&
1726 TRIPLE_RHS(ins->sizes)) {
1727 ret = &ins->next;
1728 }
1729 }
1730 return ret;
1731}
1732
1733
1734static void verify_use(struct compile_state *state,
1735 struct triple *user, struct triple *used)
1736{
1737 int size, i;
1738 size = TRIPLE_SIZE(user->sizes);
1739 for(i = 0; i < size; i++) {
1740 if (user->param[i] == used) {
1741 break;
1742 }
1743 }
1744 if (triple_is_branch(state, user)) {
1745 if (user->next == used) {
1746 i = -1;
1747 }
1748 }
1749 if (i == size) {
1750 internal_error(state, user, "%s(%p) does not use %s(%p)",
1751 tops(user->op), user, tops(used->op), used);
1752 }
1753}
1754
1755static int find_rhs_use(struct compile_state *state,
1756 struct triple *user, struct triple *used)
1757{
1758 struct triple **param;
1759 int size, i;
1760 verify_use(state, user, used);
1761 size = TRIPLE_RHS(user->sizes);
1762 param = &RHS(user, 0);
1763 for(i = 0; i < size; i++) {
1764 if (param[i] == used) {
1765 return i;
1766 }
1767 }
1768 return -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001769}
1770
1771static void free_triple(struct compile_state *state, struct triple *ptr)
1772{
Eric Biederman0babc1c2003-05-09 02:39:00 +00001773 size_t size;
1774 size = sizeof(*ptr) - sizeof(ptr->param) +
1775 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr->sizes));
Eric Biedermanb138ac82003-04-22 18:44:01 +00001776 ptr->prev->next = ptr->next;
1777 ptr->next->prev = ptr->prev;
1778 if (ptr->use) {
1779 internal_error(state, ptr, "ptr->use != 0");
1780 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001781 put_occurance(ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00001782 memset(ptr, -1, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001783 xfree(ptr);
1784}
1785
1786static void release_triple(struct compile_state *state, struct triple *ptr)
1787{
1788 struct triple_set *set, *next;
1789 struct triple **expr;
1790 /* Remove ptr from use chains where it is the user */
1791 expr = triple_rhs(state, ptr, 0);
1792 for(; expr; expr = triple_rhs(state, ptr, expr)) {
1793 if (*expr) {
1794 unuse_triple(*expr, ptr);
1795 }
1796 }
1797 expr = triple_lhs(state, ptr, 0);
1798 for(; expr; expr = triple_lhs(state, ptr, expr)) {
1799 if (*expr) {
1800 unuse_triple(*expr, ptr);
1801 }
1802 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001803 expr = triple_misc(state, ptr, 0);
1804 for(; expr; expr = triple_misc(state, ptr, expr)) {
1805 if (*expr) {
1806 unuse_triple(*expr, ptr);
1807 }
1808 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001809 expr = triple_targ(state, ptr, 0);
1810 for(; expr; expr = triple_targ(state, ptr, expr)) {
1811 if (*expr) {
1812 unuse_triple(*expr, ptr);
1813 }
1814 }
1815 /* Reomve ptr from use chains where it is used */
1816 for(set = ptr->use; set; set = next) {
1817 next = set->next;
1818 expr = triple_rhs(state, set->member, 0);
1819 for(; expr; expr = triple_rhs(state, set->member, expr)) {
1820 if (*expr == ptr) {
1821 *expr = &zero_triple;
1822 }
1823 }
1824 expr = triple_lhs(state, set->member, 0);
1825 for(; expr; expr = triple_lhs(state, set->member, expr)) {
1826 if (*expr == ptr) {
1827 *expr = &zero_triple;
1828 }
1829 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001830 expr = triple_misc(state, set->member, 0);
1831 for(; expr; expr = triple_misc(state, set->member, expr)) {
1832 if (*expr == ptr) {
1833 *expr = &zero_triple;
1834 }
1835 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001836 expr = triple_targ(state, set->member, 0);
1837 for(; expr; expr = triple_targ(state, set->member, expr)) {
1838 if (*expr == ptr) {
1839 *expr = &zero_triple;
1840 }
1841 }
1842 unuse_triple(ptr, set->member);
1843 }
1844 free_triple(state, ptr);
1845}
1846
1847static void print_triple(struct compile_state *state, struct triple *ptr);
1848
1849#define TOK_UNKNOWN 0
1850#define TOK_SPACE 1
1851#define TOK_SEMI 2
1852#define TOK_LBRACE 3
1853#define TOK_RBRACE 4
1854#define TOK_COMMA 5
1855#define TOK_EQ 6
1856#define TOK_COLON 7
1857#define TOK_LBRACKET 8
1858#define TOK_RBRACKET 9
1859#define TOK_LPAREN 10
1860#define TOK_RPAREN 11
1861#define TOK_STAR 12
1862#define TOK_DOTS 13
1863#define TOK_MORE 14
1864#define TOK_LESS 15
1865#define TOK_TIMESEQ 16
1866#define TOK_DIVEQ 17
1867#define TOK_MODEQ 18
1868#define TOK_PLUSEQ 19
1869#define TOK_MINUSEQ 20
1870#define TOK_SLEQ 21
1871#define TOK_SREQ 22
1872#define TOK_ANDEQ 23
1873#define TOK_XOREQ 24
1874#define TOK_OREQ 25
1875#define TOK_EQEQ 26
1876#define TOK_NOTEQ 27
1877#define TOK_QUEST 28
1878#define TOK_LOGOR 29
1879#define TOK_LOGAND 30
1880#define TOK_OR 31
1881#define TOK_AND 32
1882#define TOK_XOR 33
1883#define TOK_LESSEQ 34
1884#define TOK_MOREEQ 35
1885#define TOK_SL 36
1886#define TOK_SR 37
1887#define TOK_PLUS 38
1888#define TOK_MINUS 39
1889#define TOK_DIV 40
1890#define TOK_MOD 41
1891#define TOK_PLUSPLUS 42
1892#define TOK_MINUSMINUS 43
1893#define TOK_BANG 44
1894#define TOK_ARROW 45
1895#define TOK_DOT 46
1896#define TOK_TILDE 47
1897#define TOK_LIT_STRING 48
1898#define TOK_LIT_CHAR 49
1899#define TOK_LIT_INT 50
1900#define TOK_LIT_FLOAT 51
1901#define TOK_MACRO 52
1902#define TOK_CONCATENATE 53
1903
1904#define TOK_IDENT 54
1905#define TOK_STRUCT_NAME 55
1906#define TOK_ENUM_CONST 56
1907#define TOK_TYPE_NAME 57
1908
1909#define TOK_AUTO 58
1910#define TOK_BREAK 59
1911#define TOK_CASE 60
1912#define TOK_CHAR 61
1913#define TOK_CONST 62
1914#define TOK_CONTINUE 63
1915#define TOK_DEFAULT 64
1916#define TOK_DO 65
1917#define TOK_DOUBLE 66
1918#define TOK_ELSE 67
1919#define TOK_ENUM 68
1920#define TOK_EXTERN 69
1921#define TOK_FLOAT 70
1922#define TOK_FOR 71
1923#define TOK_GOTO 72
1924#define TOK_IF 73
1925#define TOK_INLINE 74
1926#define TOK_INT 75
1927#define TOK_LONG 76
1928#define TOK_REGISTER 77
1929#define TOK_RESTRICT 78
1930#define TOK_RETURN 79
1931#define TOK_SHORT 80
1932#define TOK_SIGNED 81
1933#define TOK_SIZEOF 82
1934#define TOK_STATIC 83
1935#define TOK_STRUCT 84
1936#define TOK_SWITCH 85
1937#define TOK_TYPEDEF 86
1938#define TOK_UNION 87
1939#define TOK_UNSIGNED 88
1940#define TOK_VOID 89
1941#define TOK_VOLATILE 90
1942#define TOK_WHILE 91
1943#define TOK_ASM 92
1944#define TOK_ATTRIBUTE 93
1945#define TOK_ALIGNOF 94
1946#define TOK_FIRST_KEYWORD TOK_AUTO
1947#define TOK_LAST_KEYWORD TOK_ALIGNOF
1948
1949#define TOK_DEFINE 100
1950#define TOK_UNDEF 101
1951#define TOK_INCLUDE 102
1952#define TOK_LINE 103
1953#define TOK_ERROR 104
1954#define TOK_WARNING 105
1955#define TOK_PRAGMA 106
1956#define TOK_IFDEF 107
1957#define TOK_IFNDEF 108
1958#define TOK_ELIF 109
1959#define TOK_ENDIF 110
1960
1961#define TOK_FIRST_MACRO TOK_DEFINE
1962#define TOK_LAST_MACRO TOK_ENDIF
1963
1964#define TOK_EOF 111
1965
1966static const char *tokens[] = {
1967[TOK_UNKNOWN ] = "unknown",
1968[TOK_SPACE ] = ":space:",
1969[TOK_SEMI ] = ";",
1970[TOK_LBRACE ] = "{",
1971[TOK_RBRACE ] = "}",
1972[TOK_COMMA ] = ",",
1973[TOK_EQ ] = "=",
1974[TOK_COLON ] = ":",
1975[TOK_LBRACKET ] = "[",
1976[TOK_RBRACKET ] = "]",
1977[TOK_LPAREN ] = "(",
1978[TOK_RPAREN ] = ")",
1979[TOK_STAR ] = "*",
1980[TOK_DOTS ] = "...",
1981[TOK_MORE ] = ">",
1982[TOK_LESS ] = "<",
1983[TOK_TIMESEQ ] = "*=",
1984[TOK_DIVEQ ] = "/=",
1985[TOK_MODEQ ] = "%=",
1986[TOK_PLUSEQ ] = "+=",
1987[TOK_MINUSEQ ] = "-=",
1988[TOK_SLEQ ] = "<<=",
1989[TOK_SREQ ] = ">>=",
1990[TOK_ANDEQ ] = "&=",
1991[TOK_XOREQ ] = "^=",
1992[TOK_OREQ ] = "|=",
1993[TOK_EQEQ ] = "==",
1994[TOK_NOTEQ ] = "!=",
1995[TOK_QUEST ] = "?",
1996[TOK_LOGOR ] = "||",
1997[TOK_LOGAND ] = "&&",
1998[TOK_OR ] = "|",
1999[TOK_AND ] = "&",
2000[TOK_XOR ] = "^",
2001[TOK_LESSEQ ] = "<=",
2002[TOK_MOREEQ ] = ">=",
2003[TOK_SL ] = "<<",
2004[TOK_SR ] = ">>",
2005[TOK_PLUS ] = "+",
2006[TOK_MINUS ] = "-",
2007[TOK_DIV ] = "/",
2008[TOK_MOD ] = "%",
2009[TOK_PLUSPLUS ] = "++",
2010[TOK_MINUSMINUS ] = "--",
2011[TOK_BANG ] = "!",
2012[TOK_ARROW ] = "->",
2013[TOK_DOT ] = ".",
2014[TOK_TILDE ] = "~",
2015[TOK_LIT_STRING ] = ":string:",
2016[TOK_IDENT ] = ":ident:",
2017[TOK_TYPE_NAME ] = ":typename:",
2018[TOK_LIT_CHAR ] = ":char:",
2019[TOK_LIT_INT ] = ":integer:",
2020[TOK_LIT_FLOAT ] = ":float:",
2021[TOK_MACRO ] = "#",
2022[TOK_CONCATENATE ] = "##",
2023
2024[TOK_AUTO ] = "auto",
2025[TOK_BREAK ] = "break",
2026[TOK_CASE ] = "case",
2027[TOK_CHAR ] = "char",
2028[TOK_CONST ] = "const",
2029[TOK_CONTINUE ] = "continue",
2030[TOK_DEFAULT ] = "default",
2031[TOK_DO ] = "do",
2032[TOK_DOUBLE ] = "double",
2033[TOK_ELSE ] = "else",
2034[TOK_ENUM ] = "enum",
2035[TOK_EXTERN ] = "extern",
2036[TOK_FLOAT ] = "float",
2037[TOK_FOR ] = "for",
2038[TOK_GOTO ] = "goto",
2039[TOK_IF ] = "if",
2040[TOK_INLINE ] = "inline",
2041[TOK_INT ] = "int",
2042[TOK_LONG ] = "long",
2043[TOK_REGISTER ] = "register",
2044[TOK_RESTRICT ] = "restrict",
2045[TOK_RETURN ] = "return",
2046[TOK_SHORT ] = "short",
2047[TOK_SIGNED ] = "signed",
2048[TOK_SIZEOF ] = "sizeof",
2049[TOK_STATIC ] = "static",
2050[TOK_STRUCT ] = "struct",
2051[TOK_SWITCH ] = "switch",
2052[TOK_TYPEDEF ] = "typedef",
2053[TOK_UNION ] = "union",
2054[TOK_UNSIGNED ] = "unsigned",
2055[TOK_VOID ] = "void",
2056[TOK_VOLATILE ] = "volatile",
2057[TOK_WHILE ] = "while",
2058[TOK_ASM ] = "asm",
2059[TOK_ATTRIBUTE ] = "__attribute__",
2060[TOK_ALIGNOF ] = "__alignof__",
2061
2062[TOK_DEFINE ] = "define",
2063[TOK_UNDEF ] = "undef",
2064[TOK_INCLUDE ] = "include",
2065[TOK_LINE ] = "line",
2066[TOK_ERROR ] = "error",
2067[TOK_WARNING ] = "warning",
2068[TOK_PRAGMA ] = "pragma",
2069[TOK_IFDEF ] = "ifdef",
2070[TOK_IFNDEF ] = "ifndef",
2071[TOK_ELIF ] = "elif",
2072[TOK_ENDIF ] = "endif",
2073
2074[TOK_EOF ] = "EOF",
2075};
2076
2077static unsigned int hash(const char *str, int str_len)
2078{
2079 unsigned int hash;
2080 const char *end;
2081 end = str + str_len;
2082 hash = 0;
2083 for(; str < end; str++) {
2084 hash = (hash *263) + *str;
2085 }
2086 hash = hash & (HASH_TABLE_SIZE -1);
2087 return hash;
2088}
2089
2090static struct hash_entry *lookup(
2091 struct compile_state *state, const char *name, int name_len)
2092{
2093 struct hash_entry *entry;
2094 unsigned int index;
2095 index = hash(name, name_len);
2096 entry = state->hash_table[index];
2097 while(entry &&
2098 ((entry->name_len != name_len) ||
2099 (memcmp(entry->name, name, name_len) != 0))) {
2100 entry = entry->next;
2101 }
2102 if (!entry) {
2103 char *new_name;
2104 /* Get a private copy of the name */
2105 new_name = xmalloc(name_len + 1, "hash_name");
2106 memcpy(new_name, name, name_len);
2107 new_name[name_len] = '\0';
2108
2109 /* Create a new hash entry */
2110 entry = xcmalloc(sizeof(*entry), "hash_entry");
2111 entry->next = state->hash_table[index];
2112 entry->name = new_name;
2113 entry->name_len = name_len;
2114
2115 /* Place the new entry in the hash table */
2116 state->hash_table[index] = entry;
2117 }
2118 return entry;
2119}
2120
2121static void ident_to_keyword(struct compile_state *state, struct token *tk)
2122{
2123 struct hash_entry *entry;
2124 entry = tk->ident;
2125 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
2126 (entry->tok == TOK_ENUM_CONST) ||
2127 ((entry->tok >= TOK_FIRST_KEYWORD) &&
2128 (entry->tok <= TOK_LAST_KEYWORD)))) {
2129 tk->tok = entry->tok;
2130 }
2131}
2132
2133static void ident_to_macro(struct compile_state *state, struct token *tk)
2134{
2135 struct hash_entry *entry;
2136 entry = tk->ident;
2137 if (entry &&
2138 (entry->tok >= TOK_FIRST_MACRO) &&
2139 (entry->tok <= TOK_LAST_MACRO)) {
2140 tk->tok = entry->tok;
2141 }
2142}
2143
2144static void hash_keyword(
2145 struct compile_state *state, const char *keyword, int tok)
2146{
2147 struct hash_entry *entry;
2148 entry = lookup(state, keyword, strlen(keyword));
2149 if (entry && entry->tok != TOK_UNKNOWN) {
2150 die("keyword %s already hashed", keyword);
2151 }
2152 entry->tok = tok;
2153}
2154
2155static void symbol(
2156 struct compile_state *state, struct hash_entry *ident,
2157 struct symbol **chain, struct triple *def, struct type *type)
2158{
2159 struct symbol *sym;
2160 if (*chain && ((*chain)->scope_depth == state->scope_depth)) {
2161 error(state, 0, "%s already defined", ident->name);
2162 }
2163 sym = xcmalloc(sizeof(*sym), "symbol");
2164 sym->ident = ident;
2165 sym->def = def;
2166 sym->type = type;
2167 sym->scope_depth = state->scope_depth;
2168 sym->next = *chain;
2169 *chain = sym;
2170}
2171
2172static void start_scope(struct compile_state *state)
2173{
2174 state->scope_depth++;
2175}
2176
2177static void end_scope_syms(struct symbol **chain, int depth)
2178{
2179 struct symbol *sym, *next;
2180 sym = *chain;
2181 while(sym && (sym->scope_depth == depth)) {
2182 next = sym->next;
2183 xfree(sym);
2184 sym = next;
2185 }
2186 *chain = sym;
2187}
2188
2189static void end_scope(struct compile_state *state)
2190{
2191 int i;
2192 int depth;
2193 /* Walk through the hash table and remove all symbols
2194 * in the current scope.
2195 */
2196 depth = state->scope_depth;
2197 for(i = 0; i < HASH_TABLE_SIZE; i++) {
2198 struct hash_entry *entry;
2199 entry = state->hash_table[i];
2200 while(entry) {
2201 end_scope_syms(&entry->sym_label, depth);
2202 end_scope_syms(&entry->sym_struct, depth);
2203 end_scope_syms(&entry->sym_ident, depth);
2204 entry = entry->next;
2205 }
2206 }
2207 state->scope_depth = depth - 1;
2208}
2209
2210static void register_keywords(struct compile_state *state)
2211{
2212 hash_keyword(state, "auto", TOK_AUTO);
2213 hash_keyword(state, "break", TOK_BREAK);
2214 hash_keyword(state, "case", TOK_CASE);
2215 hash_keyword(state, "char", TOK_CHAR);
2216 hash_keyword(state, "const", TOK_CONST);
2217 hash_keyword(state, "continue", TOK_CONTINUE);
2218 hash_keyword(state, "default", TOK_DEFAULT);
2219 hash_keyword(state, "do", TOK_DO);
2220 hash_keyword(state, "double", TOK_DOUBLE);
2221 hash_keyword(state, "else", TOK_ELSE);
2222 hash_keyword(state, "enum", TOK_ENUM);
2223 hash_keyword(state, "extern", TOK_EXTERN);
2224 hash_keyword(state, "float", TOK_FLOAT);
2225 hash_keyword(state, "for", TOK_FOR);
2226 hash_keyword(state, "goto", TOK_GOTO);
2227 hash_keyword(state, "if", TOK_IF);
2228 hash_keyword(state, "inline", TOK_INLINE);
2229 hash_keyword(state, "int", TOK_INT);
2230 hash_keyword(state, "long", TOK_LONG);
2231 hash_keyword(state, "register", TOK_REGISTER);
2232 hash_keyword(state, "restrict", TOK_RESTRICT);
2233 hash_keyword(state, "return", TOK_RETURN);
2234 hash_keyword(state, "short", TOK_SHORT);
2235 hash_keyword(state, "signed", TOK_SIGNED);
2236 hash_keyword(state, "sizeof", TOK_SIZEOF);
2237 hash_keyword(state, "static", TOK_STATIC);
2238 hash_keyword(state, "struct", TOK_STRUCT);
2239 hash_keyword(state, "switch", TOK_SWITCH);
2240 hash_keyword(state, "typedef", TOK_TYPEDEF);
2241 hash_keyword(state, "union", TOK_UNION);
2242 hash_keyword(state, "unsigned", TOK_UNSIGNED);
2243 hash_keyword(state, "void", TOK_VOID);
2244 hash_keyword(state, "volatile", TOK_VOLATILE);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002245 hash_keyword(state, "__volatile__", TOK_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002246 hash_keyword(state, "while", TOK_WHILE);
2247 hash_keyword(state, "asm", TOK_ASM);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002248 hash_keyword(state, "__asm__", TOK_ASM);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002249 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
2250 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
2251}
2252
2253static void register_macro_keywords(struct compile_state *state)
2254{
2255 hash_keyword(state, "define", TOK_DEFINE);
2256 hash_keyword(state, "undef", TOK_UNDEF);
2257 hash_keyword(state, "include", TOK_INCLUDE);
2258 hash_keyword(state, "line", TOK_LINE);
2259 hash_keyword(state, "error", TOK_ERROR);
2260 hash_keyword(state, "warning", TOK_WARNING);
2261 hash_keyword(state, "pragma", TOK_PRAGMA);
2262 hash_keyword(state, "ifdef", TOK_IFDEF);
2263 hash_keyword(state, "ifndef", TOK_IFNDEF);
2264 hash_keyword(state, "elif", TOK_ELIF);
2265 hash_keyword(state, "endif", TOK_ENDIF);
2266}
2267
2268static int spacep(int c)
2269{
2270 int ret = 0;
2271 switch(c) {
2272 case ' ':
2273 case '\t':
2274 case '\f':
2275 case '\v':
2276 case '\r':
2277 case '\n':
2278 ret = 1;
2279 break;
2280 }
2281 return ret;
2282}
2283
2284static int digitp(int c)
2285{
2286 int ret = 0;
2287 switch(c) {
2288 case '0': case '1': case '2': case '3': case '4':
2289 case '5': case '6': case '7': case '8': case '9':
2290 ret = 1;
2291 break;
2292 }
2293 return ret;
2294}
Eric Biederman8d9c1232003-06-17 08:42:17 +00002295static int digval(int c)
2296{
2297 int val = -1;
2298 if ((c >= '0') && (c <= '9')) {
2299 val = c - '0';
2300 }
2301 return val;
2302}
Eric Biedermanb138ac82003-04-22 18:44:01 +00002303
2304static int hexdigitp(int c)
2305{
2306 int ret = 0;
2307 switch(c) {
2308 case '0': case '1': case '2': case '3': case '4':
2309 case '5': case '6': case '7': case '8': case '9':
2310 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2311 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2312 ret = 1;
2313 break;
2314 }
2315 return ret;
2316}
2317static int hexdigval(int c)
2318{
2319 int val = -1;
2320 if ((c >= '0') && (c <= '9')) {
2321 val = c - '0';
2322 }
2323 else if ((c >= 'A') && (c <= 'F')) {
2324 val = 10 + (c - 'A');
2325 }
2326 else if ((c >= 'a') && (c <= 'f')) {
2327 val = 10 + (c - 'a');
2328 }
2329 return val;
2330}
2331
2332static int octdigitp(int c)
2333{
2334 int ret = 0;
2335 switch(c) {
2336 case '0': case '1': case '2': case '3':
2337 case '4': case '5': case '6': case '7':
2338 ret = 1;
2339 break;
2340 }
2341 return ret;
2342}
2343static int octdigval(int c)
2344{
2345 int val = -1;
2346 if ((c >= '0') && (c <= '7')) {
2347 val = c - '0';
2348 }
2349 return val;
2350}
2351
2352static int letterp(int c)
2353{
2354 int ret = 0;
2355 switch(c) {
2356 case 'a': case 'b': case 'c': case 'd': case 'e':
2357 case 'f': case 'g': case 'h': case 'i': case 'j':
2358 case 'k': case 'l': case 'm': case 'n': case 'o':
2359 case 'p': case 'q': case 'r': case 's': case 't':
2360 case 'u': case 'v': case 'w': case 'x': case 'y':
2361 case 'z':
2362 case 'A': case 'B': case 'C': case 'D': case 'E':
2363 case 'F': case 'G': case 'H': case 'I': case 'J':
2364 case 'K': case 'L': case 'M': case 'N': case 'O':
2365 case 'P': case 'Q': case 'R': case 'S': case 'T':
2366 case 'U': case 'V': case 'W': case 'X': case 'Y':
2367 case 'Z':
2368 case '_':
2369 ret = 1;
2370 break;
2371 }
2372 return ret;
2373}
2374
2375static int char_value(struct compile_state *state,
2376 const signed char **strp, const signed char *end)
2377{
2378 const signed char *str;
2379 int c;
2380 str = *strp;
2381 c = *str++;
2382 if ((c == '\\') && (str < end)) {
2383 switch(*str) {
2384 case 'n': c = '\n'; str++; break;
2385 case 't': c = '\t'; str++; break;
2386 case 'v': c = '\v'; str++; break;
2387 case 'b': c = '\b'; str++; break;
2388 case 'r': c = '\r'; str++; break;
2389 case 'f': c = '\f'; str++; break;
2390 case 'a': c = '\a'; str++; break;
2391 case '\\': c = '\\'; str++; break;
2392 case '?': c = '?'; str++; break;
2393 case '\'': c = '\''; str++; break;
2394 case '"': c = '"'; break;
2395 case 'x':
2396 c = 0;
2397 str++;
2398 while((str < end) && hexdigitp(*str)) {
2399 c <<= 4;
2400 c += hexdigval(*str);
2401 str++;
2402 }
2403 break;
2404 case '0': case '1': case '2': case '3':
2405 case '4': case '5': case '6': case '7':
2406 c = 0;
2407 while((str < end) && octdigitp(*str)) {
2408 c <<= 3;
2409 c += octdigval(*str);
2410 str++;
2411 }
2412 break;
2413 default:
2414 error(state, 0, "Invalid character constant");
2415 break;
2416 }
2417 }
2418 *strp = str;
2419 return c;
2420}
2421
2422static char *after_digits(char *ptr, char *end)
2423{
2424 while((ptr < end) && digitp(*ptr)) {
2425 ptr++;
2426 }
2427 return ptr;
2428}
2429
2430static char *after_octdigits(char *ptr, char *end)
2431{
2432 while((ptr < end) && octdigitp(*ptr)) {
2433 ptr++;
2434 }
2435 return ptr;
2436}
2437
2438static char *after_hexdigits(char *ptr, char *end)
2439{
2440 while((ptr < end) && hexdigitp(*ptr)) {
2441 ptr++;
2442 }
2443 return ptr;
2444}
2445
2446static void save_string(struct compile_state *state,
2447 struct token *tk, char *start, char *end, const char *id)
2448{
2449 char *str;
2450 int str_len;
2451 /* Create a private copy of the string */
2452 str_len = end - start + 1;
2453 str = xmalloc(str_len + 1, id);
2454 memcpy(str, start, str_len);
2455 str[str_len] = '\0';
2456
2457 /* Store the copy in the token */
2458 tk->val.str = str;
2459 tk->str_len = str_len;
2460}
2461static void next_token(struct compile_state *state, int index)
2462{
2463 struct file_state *file;
2464 struct token *tk;
2465 char *token;
2466 int c, c1, c2, c3;
2467 char *tokp, *end;
2468 int tok;
2469next_token:
2470 file = state->file;
2471 tk = &state->token[index];
2472 tk->str_len = 0;
2473 tk->ident = 0;
2474 token = tokp = file->pos;
2475 end = file->buf + file->size;
2476 tok = TOK_UNKNOWN;
2477 c = -1;
2478 if (tokp < end) {
2479 c = *tokp;
2480 }
2481 c1 = -1;
2482 if ((tokp + 1) < end) {
2483 c1 = tokp[1];
2484 }
2485 c2 = -1;
2486 if ((tokp + 2) < end) {
2487 c2 = tokp[2];
2488 }
2489 c3 = -1;
2490 if ((tokp + 3) < end) {
2491 c3 = tokp[3];
2492 }
2493 if (tokp >= end) {
2494 tok = TOK_EOF;
2495 tokp = end;
2496 }
2497 /* Whitespace */
2498 else if (spacep(c)) {
2499 tok = TOK_SPACE;
2500 while ((tokp < end) && spacep(c)) {
2501 if (c == '\n') {
2502 file->line++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002503 file->report_line++;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002504 file->line_start = tokp + 1;
2505 }
2506 c = *(++tokp);
2507 }
2508 if (!spacep(c)) {
2509 tokp--;
2510 }
2511 }
2512 /* EOL Comments */
2513 else if ((c == '/') && (c1 == '/')) {
2514 tok = TOK_SPACE;
2515 for(tokp += 2; tokp < end; tokp++) {
2516 c = *tokp;
2517 if (c == '\n') {
2518 file->line++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002519 file->report_line++;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002520 file->line_start = tokp +1;
2521 break;
2522 }
2523 }
2524 }
2525 /* Comments */
2526 else if ((c == '/') && (c1 == '*')) {
2527 int line;
2528 char *line_start;
2529 line = file->line;
2530 line_start = file->line_start;
2531 for(tokp += 2; (end - tokp) >= 2; tokp++) {
2532 c = *tokp;
2533 if (c == '\n') {
2534 line++;
2535 line_start = tokp +1;
2536 }
2537 else if ((c == '*') && (tokp[1] == '/')) {
2538 tok = TOK_SPACE;
2539 tokp += 1;
2540 break;
2541 }
2542 }
2543 if (tok == TOK_UNKNOWN) {
2544 error(state, 0, "unterminated comment");
2545 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002546 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002547 file->line = line;
2548 file->line_start = line_start;
2549 }
2550 /* string constants */
2551 else if ((c == '"') ||
2552 ((c == 'L') && (c1 == '"'))) {
2553 int line;
2554 char *line_start;
2555 int wchar;
2556 line = file->line;
2557 line_start = file->line_start;
2558 wchar = 0;
2559 if (c == 'L') {
2560 wchar = 1;
2561 tokp++;
2562 }
2563 for(tokp += 1; tokp < end; tokp++) {
2564 c = *tokp;
2565 if (c == '\n') {
2566 line++;
2567 line_start = tokp + 1;
2568 }
2569 else if ((c == '\\') && (tokp +1 < end)) {
2570 tokp++;
2571 }
2572 else if (c == '"') {
2573 tok = TOK_LIT_STRING;
2574 break;
2575 }
2576 }
2577 if (tok == TOK_UNKNOWN) {
2578 error(state, 0, "unterminated string constant");
2579 }
2580 if (line != file->line) {
2581 warning(state, 0, "multiline string constant");
2582 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002583 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002584 file->line = line;
2585 file->line_start = line_start;
2586
2587 /* Save the string value */
2588 save_string(state, tk, token, tokp, "literal string");
2589 }
2590 /* character constants */
2591 else if ((c == '\'') ||
2592 ((c == 'L') && (c1 == '\''))) {
2593 int line;
2594 char *line_start;
2595 int wchar;
2596 line = file->line;
2597 line_start = file->line_start;
2598 wchar = 0;
2599 if (c == 'L') {
2600 wchar = 1;
2601 tokp++;
2602 }
2603 for(tokp += 1; tokp < end; tokp++) {
2604 c = *tokp;
2605 if (c == '\n') {
2606 line++;
2607 line_start = tokp + 1;
2608 }
2609 else if ((c == '\\') && (tokp +1 < end)) {
2610 tokp++;
2611 }
2612 else if (c == '\'') {
2613 tok = TOK_LIT_CHAR;
2614 break;
2615 }
2616 }
2617 if (tok == TOK_UNKNOWN) {
2618 error(state, 0, "unterminated character constant");
2619 }
2620 if (line != file->line) {
2621 warning(state, 0, "multiline character constant");
2622 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002623 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002624 file->line = line;
2625 file->line_start = line_start;
2626
2627 /* Save the character value */
2628 save_string(state, tk, token, tokp, "literal character");
2629 }
2630 /* integer and floating constants
2631 * Integer Constants
2632 * {digits}
2633 * 0[Xx]{hexdigits}
2634 * 0{octdigit}+
2635 *
2636 * Floating constants
2637 * {digits}.{digits}[Ee][+-]?{digits}
2638 * {digits}.{digits}
2639 * {digits}[Ee][+-]?{digits}
2640 * .{digits}[Ee][+-]?{digits}
2641 * .{digits}
2642 */
2643
2644 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
2645 char *next, *new;
2646 int is_float;
2647 is_float = 0;
2648 if (c != '.') {
2649 next = after_digits(tokp, end);
2650 }
2651 else {
2652 next = tokp;
2653 }
2654 if (next[0] == '.') {
2655 new = after_digits(next, end);
2656 is_float = (new != next);
2657 next = new;
2658 }
2659 if ((next[0] == 'e') || (next[0] == 'E')) {
2660 if (((next + 1) < end) &&
2661 ((next[1] == '+') || (next[1] == '-'))) {
2662 next++;
2663 }
2664 new = after_digits(next, end);
2665 is_float = (new != next);
2666 next = new;
2667 }
2668 if (is_float) {
2669 tok = TOK_LIT_FLOAT;
2670 if ((next < end) && (
2671 (next[0] == 'f') ||
2672 (next[0] == 'F') ||
2673 (next[0] == 'l') ||
2674 (next[0] == 'L'))
2675 ) {
2676 next++;
2677 }
2678 }
2679 if (!is_float && digitp(c)) {
2680 tok = TOK_LIT_INT;
2681 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
2682 next = after_hexdigits(tokp + 2, end);
2683 }
2684 else if (c == '0') {
2685 next = after_octdigits(tokp, end);
2686 }
2687 else {
2688 next = after_digits(tokp, end);
2689 }
2690 /* crazy integer suffixes */
2691 if ((next < end) &&
2692 ((next[0] == 'u') || (next[0] == 'U'))) {
2693 next++;
2694 if ((next < end) &&
2695 ((next[0] == 'l') || (next[0] == 'L'))) {
2696 next++;
2697 }
2698 }
2699 else if ((next < end) &&
2700 ((next[0] == 'l') || (next[0] == 'L'))) {
2701 next++;
2702 if ((next < end) &&
2703 ((next[0] == 'u') || (next[0] == 'U'))) {
2704 next++;
2705 }
2706 }
2707 }
2708 tokp = next - 1;
2709
2710 /* Save the integer/floating point value */
2711 save_string(state, tk, token, tokp, "literal number");
2712 }
2713 /* identifiers */
2714 else if (letterp(c)) {
2715 tok = TOK_IDENT;
2716 for(tokp += 1; tokp < end; tokp++) {
2717 c = *tokp;
2718 if (!letterp(c) && !digitp(c)) {
2719 break;
2720 }
2721 }
2722 tokp -= 1;
2723 tk->ident = lookup(state, token, tokp +1 - token);
2724 }
2725 /* C99 alternate macro characters */
2726 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
2727 tokp += 3;
2728 tok = TOK_CONCATENATE;
2729 }
2730 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { tokp += 2; tok = TOK_DOTS; }
2731 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { tokp += 2; tok = TOK_SLEQ; }
2732 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { tokp += 2; tok = TOK_SREQ; }
2733 else if ((c == '*') && (c1 == '=')) { tokp += 1; tok = TOK_TIMESEQ; }
2734 else if ((c == '/') && (c1 == '=')) { tokp += 1; tok = TOK_DIVEQ; }
2735 else if ((c == '%') && (c1 == '=')) { tokp += 1; tok = TOK_MODEQ; }
2736 else if ((c == '+') && (c1 == '=')) { tokp += 1; tok = TOK_PLUSEQ; }
2737 else if ((c == '-') && (c1 == '=')) { tokp += 1; tok = TOK_MINUSEQ; }
2738 else if ((c == '&') && (c1 == '=')) { tokp += 1; tok = TOK_ANDEQ; }
2739 else if ((c == '^') && (c1 == '=')) { tokp += 1; tok = TOK_XOREQ; }
2740 else if ((c == '|') && (c1 == '=')) { tokp += 1; tok = TOK_OREQ; }
2741 else if ((c == '=') && (c1 == '=')) { tokp += 1; tok = TOK_EQEQ; }
2742 else if ((c == '!') && (c1 == '=')) { tokp += 1; tok = TOK_NOTEQ; }
2743 else if ((c == '|') && (c1 == '|')) { tokp += 1; tok = TOK_LOGOR; }
2744 else if ((c == '&') && (c1 == '&')) { tokp += 1; tok = TOK_LOGAND; }
2745 else if ((c == '<') && (c1 == '=')) { tokp += 1; tok = TOK_LESSEQ; }
2746 else if ((c == '>') && (c1 == '=')) { tokp += 1; tok = TOK_MOREEQ; }
2747 else if ((c == '<') && (c1 == '<')) { tokp += 1; tok = TOK_SL; }
2748 else if ((c == '>') && (c1 == '>')) { tokp += 1; tok = TOK_SR; }
2749 else if ((c == '+') && (c1 == '+')) { tokp += 1; tok = TOK_PLUSPLUS; }
2750 else if ((c == '-') && (c1 == '-')) { tokp += 1; tok = TOK_MINUSMINUS; }
2751 else if ((c == '-') && (c1 == '>')) { tokp += 1; tok = TOK_ARROW; }
2752 else if ((c == '<') && (c1 == ':')) { tokp += 1; tok = TOK_LBRACKET; }
2753 else if ((c == ':') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACKET; }
2754 else if ((c == '<') && (c1 == '%')) { tokp += 1; tok = TOK_LBRACE; }
2755 else if ((c == '%') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACE; }
2756 else if ((c == '%') && (c1 == ':')) { tokp += 1; tok = TOK_MACRO; }
2757 else if ((c == '#') && (c1 == '#')) { tokp += 1; tok = TOK_CONCATENATE; }
2758 else if (c == ';') { tok = TOK_SEMI; }
2759 else if (c == '{') { tok = TOK_LBRACE; }
2760 else if (c == '}') { tok = TOK_RBRACE; }
2761 else if (c == ',') { tok = TOK_COMMA; }
2762 else if (c == '=') { tok = TOK_EQ; }
2763 else if (c == ':') { tok = TOK_COLON; }
2764 else if (c == '[') { tok = TOK_LBRACKET; }
2765 else if (c == ']') { tok = TOK_RBRACKET; }
2766 else if (c == '(') { tok = TOK_LPAREN; }
2767 else if (c == ')') { tok = TOK_RPAREN; }
2768 else if (c == '*') { tok = TOK_STAR; }
2769 else if (c == '>') { tok = TOK_MORE; }
2770 else if (c == '<') { tok = TOK_LESS; }
2771 else if (c == '?') { tok = TOK_QUEST; }
2772 else if (c == '|') { tok = TOK_OR; }
2773 else if (c == '&') { tok = TOK_AND; }
2774 else if (c == '^') { tok = TOK_XOR; }
2775 else if (c == '+') { tok = TOK_PLUS; }
2776 else if (c == '-') { tok = TOK_MINUS; }
2777 else if (c == '/') { tok = TOK_DIV; }
2778 else if (c == '%') { tok = TOK_MOD; }
2779 else if (c == '!') { tok = TOK_BANG; }
2780 else if (c == '.') { tok = TOK_DOT; }
2781 else if (c == '~') { tok = TOK_TILDE; }
2782 else if (c == '#') { tok = TOK_MACRO; }
2783 if (tok == TOK_MACRO) {
2784 /* Only match preprocessor directives at the start of a line */
2785 char *ptr;
2786 for(ptr = file->line_start; spacep(*ptr); ptr++)
2787 ;
2788 if (ptr != tokp) {
2789 tok = TOK_UNKNOWN;
2790 }
2791 }
2792 if (tok == TOK_UNKNOWN) {
2793 error(state, 0, "unknown token");
2794 }
2795
2796 file->pos = tokp + 1;
2797 tk->tok = tok;
2798 if (tok == TOK_IDENT) {
2799 ident_to_keyword(state, tk);
2800 }
2801 /* Don't return space tokens. */
2802 if (tok == TOK_SPACE) {
2803 goto next_token;
2804 }
2805}
2806
2807static void compile_macro(struct compile_state *state, struct token *tk)
2808{
2809 struct file_state *file;
2810 struct hash_entry *ident;
2811 ident = tk->ident;
2812 file = xmalloc(sizeof(*file), "file_state");
2813 file->basename = xstrdup(tk->ident->name);
2814 file->dirname = xstrdup("");
2815 file->size = ident->sym_define->buf_len;
2816 file->buf = xmalloc(file->size +2, file->basename);
2817 memcpy(file->buf, ident->sym_define->buf, file->size);
2818 file->buf[file->size] = '\n';
2819 file->buf[file->size + 1] = '\0';
2820 file->pos = file->buf;
2821 file->line_start = file->pos;
2822 file->line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002823 file->report_line = 1;
2824 file->report_name = file->basename;
2825 file->report_dir = file->dirname;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002826 file->prev = state->file;
2827 state->file = file;
2828}
2829
2830
2831static int mpeek(struct compile_state *state, int index)
2832{
2833 struct token *tk;
2834 int rescan;
2835 tk = &state->token[index + 1];
2836 if (tk->tok == -1) {
2837 next_token(state, index + 1);
2838 }
2839 do {
2840 rescan = 0;
2841 if ((tk->tok == TOK_EOF) &&
2842 (state->file != state->macro_file) &&
2843 (state->file->prev)) {
2844 struct file_state *file = state->file;
2845 state->file = file->prev;
2846 /* file->basename is used keep it */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002847 if (file->report_dir != file->dirname) {
2848 xfree(file->report_dir);
2849 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002850 xfree(file->dirname);
2851 xfree(file->buf);
2852 xfree(file);
2853 next_token(state, index + 1);
2854 rescan = 1;
2855 }
2856 else if (tk->ident && tk->ident->sym_define) {
2857 compile_macro(state, tk);
2858 next_token(state, index + 1);
2859 rescan = 1;
2860 }
2861 } while(rescan);
2862 /* Don't show the token on the next line */
2863 if (state->macro_line < state->macro_file->line) {
2864 return TOK_EOF;
2865 }
2866 return state->token[index +1].tok;
2867}
2868
2869static void meat(struct compile_state *state, int index, int tok)
2870{
2871 int next_tok;
2872 int i;
2873 next_tok = mpeek(state, index);
2874 if (next_tok != tok) {
2875 const char *name1, *name2;
2876 name1 = tokens[next_tok];
2877 name2 = "";
2878 if (next_tok == TOK_IDENT) {
2879 name2 = state->token[index + 1].ident->name;
2880 }
2881 error(state, 0, "found %s %s expected %s",
2882 name1, name2, tokens[tok]);
2883 }
2884 /* Free the old token value */
2885 if (state->token[index].str_len) {
2886 memset((void *)(state->token[index].val.str), -1,
2887 state->token[index].str_len);
2888 xfree(state->token[index].val.str);
2889 }
2890 for(i = index; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
2891 state->token[i] = state->token[i + 1];
2892 }
2893 memset(&state->token[i], 0, sizeof(state->token[i]));
2894 state->token[i].tok = -1;
2895}
2896
2897static long_t mcexpr(struct compile_state *state, int index);
2898
2899static long_t mprimary_expr(struct compile_state *state, int index)
2900{
2901 long_t val;
2902 int tok;
2903 tok = mpeek(state, index);
2904 while(state->token[index + 1].ident &&
2905 state->token[index + 1].ident->sym_define) {
2906 meat(state, index, tok);
2907 compile_macro(state, &state->token[index]);
2908 tok = mpeek(state, index);
2909 }
2910 switch(tok) {
2911 case TOK_LPAREN:
2912 meat(state, index, TOK_LPAREN);
2913 val = mcexpr(state, index);
2914 meat(state, index, TOK_RPAREN);
2915 break;
2916 case TOK_LIT_INT:
2917 {
2918 char *end;
2919 meat(state, index, TOK_LIT_INT);
2920 errno = 0;
2921 val = strtol(state->token[index].val.str, &end, 0);
2922 if (((val == LONG_MIN) || (val == LONG_MAX)) &&
2923 (errno == ERANGE)) {
2924 error(state, 0, "Integer constant to large");
2925 }
2926 break;
2927 }
2928 default:
2929 meat(state, index, TOK_LIT_INT);
2930 val = 0;
2931 }
2932 return val;
2933}
2934static long_t munary_expr(struct compile_state *state, int index)
2935{
2936 long_t val;
2937 switch(mpeek(state, index)) {
2938 case TOK_PLUS:
2939 meat(state, index, TOK_PLUS);
2940 val = munary_expr(state, index);
2941 val = + val;
2942 break;
2943 case TOK_MINUS:
2944 meat(state, index, TOK_MINUS);
2945 val = munary_expr(state, index);
2946 val = - val;
2947 break;
2948 case TOK_TILDE:
2949 meat(state, index, TOK_BANG);
2950 val = munary_expr(state, index);
2951 val = ~ val;
2952 break;
2953 case TOK_BANG:
2954 meat(state, index, TOK_BANG);
2955 val = munary_expr(state, index);
2956 val = ! val;
2957 break;
2958 default:
2959 val = mprimary_expr(state, index);
2960 break;
2961 }
2962 return val;
2963
2964}
2965static long_t mmul_expr(struct compile_state *state, int index)
2966{
2967 long_t val;
2968 int done;
2969 val = munary_expr(state, index);
2970 do {
2971 long_t right;
2972 done = 0;
2973 switch(mpeek(state, index)) {
2974 case TOK_STAR:
2975 meat(state, index, TOK_STAR);
2976 right = munary_expr(state, index);
2977 val = val * right;
2978 break;
2979 case TOK_DIV:
2980 meat(state, index, TOK_DIV);
2981 right = munary_expr(state, index);
2982 val = val / right;
2983 break;
2984 case TOK_MOD:
2985 meat(state, index, TOK_MOD);
2986 right = munary_expr(state, index);
2987 val = val % right;
2988 break;
2989 default:
2990 done = 1;
2991 break;
2992 }
2993 } while(!done);
2994
2995 return val;
2996}
2997
2998static long_t madd_expr(struct compile_state *state, int index)
2999{
3000 long_t val;
3001 int done;
3002 val = mmul_expr(state, index);
3003 do {
3004 long_t right;
3005 done = 0;
3006 switch(mpeek(state, index)) {
3007 case TOK_PLUS:
3008 meat(state, index, TOK_PLUS);
3009 right = mmul_expr(state, index);
3010 val = val + right;
3011 break;
3012 case TOK_MINUS:
3013 meat(state, index, TOK_MINUS);
3014 right = mmul_expr(state, index);
3015 val = val - right;
3016 break;
3017 default:
3018 done = 1;
3019 break;
3020 }
3021 } while(!done);
3022
3023 return val;
3024}
3025
3026static long_t mshift_expr(struct compile_state *state, int index)
3027{
3028 long_t val;
3029 int done;
3030 val = madd_expr(state, index);
3031 do {
3032 long_t right;
3033 done = 0;
3034 switch(mpeek(state, index)) {
3035 case TOK_SL:
3036 meat(state, index, TOK_SL);
3037 right = madd_expr(state, index);
3038 val = val << right;
3039 break;
3040 case TOK_SR:
3041 meat(state, index, TOK_SR);
3042 right = madd_expr(state, index);
3043 val = val >> right;
3044 break;
3045 default:
3046 done = 1;
3047 break;
3048 }
3049 } while(!done);
3050
3051 return val;
3052}
3053
3054static long_t mrel_expr(struct compile_state *state, int index)
3055{
3056 long_t val;
3057 int done;
3058 val = mshift_expr(state, index);
3059 do {
3060 long_t right;
3061 done = 0;
3062 switch(mpeek(state, index)) {
3063 case TOK_LESS:
3064 meat(state, index, TOK_LESS);
3065 right = mshift_expr(state, index);
3066 val = val < right;
3067 break;
3068 case TOK_MORE:
3069 meat(state, index, TOK_MORE);
3070 right = mshift_expr(state, index);
3071 val = val > right;
3072 break;
3073 case TOK_LESSEQ:
3074 meat(state, index, TOK_LESSEQ);
3075 right = mshift_expr(state, index);
3076 val = val <= right;
3077 break;
3078 case TOK_MOREEQ:
3079 meat(state, index, TOK_MOREEQ);
3080 right = mshift_expr(state, index);
3081 val = val >= right;
3082 break;
3083 default:
3084 done = 1;
3085 break;
3086 }
3087 } while(!done);
3088 return val;
3089}
3090
3091static long_t meq_expr(struct compile_state *state, int index)
3092{
3093 long_t val;
3094 int done;
3095 val = mrel_expr(state, index);
3096 do {
3097 long_t right;
3098 done = 0;
3099 switch(mpeek(state, index)) {
3100 case TOK_EQEQ:
3101 meat(state, index, TOK_EQEQ);
3102 right = mrel_expr(state, index);
3103 val = val == right;
3104 break;
3105 case TOK_NOTEQ:
3106 meat(state, index, TOK_NOTEQ);
3107 right = mrel_expr(state, index);
3108 val = val != right;
3109 break;
3110 default:
3111 done = 1;
3112 break;
3113 }
3114 } while(!done);
3115 return val;
3116}
3117
3118static long_t mand_expr(struct compile_state *state, int index)
3119{
3120 long_t val;
3121 val = meq_expr(state, index);
3122 if (mpeek(state, index) == TOK_AND) {
3123 long_t right;
3124 meat(state, index, TOK_AND);
3125 right = meq_expr(state, index);
3126 val = val & right;
3127 }
3128 return val;
3129}
3130
3131static long_t mxor_expr(struct compile_state *state, int index)
3132{
3133 long_t val;
3134 val = mand_expr(state, index);
3135 if (mpeek(state, index) == TOK_XOR) {
3136 long_t right;
3137 meat(state, index, TOK_XOR);
3138 right = mand_expr(state, index);
3139 val = val ^ right;
3140 }
3141 return val;
3142}
3143
3144static long_t mor_expr(struct compile_state *state, int index)
3145{
3146 long_t val;
3147 val = mxor_expr(state, index);
3148 if (mpeek(state, index) == TOK_OR) {
3149 long_t right;
3150 meat(state, index, TOK_OR);
3151 right = mxor_expr(state, index);
3152 val = val | right;
3153 }
3154 return val;
3155}
3156
3157static long_t mland_expr(struct compile_state *state, int index)
3158{
3159 long_t val;
3160 val = mor_expr(state, index);
3161 if (mpeek(state, index) == TOK_LOGAND) {
3162 long_t right;
3163 meat(state, index, TOK_LOGAND);
3164 right = mor_expr(state, index);
3165 val = val && right;
3166 }
3167 return val;
3168}
3169static long_t mlor_expr(struct compile_state *state, int index)
3170{
3171 long_t val;
3172 val = mland_expr(state, index);
3173 if (mpeek(state, index) == TOK_LOGOR) {
3174 long_t right;
3175 meat(state, index, TOK_LOGOR);
3176 right = mland_expr(state, index);
3177 val = val || right;
3178 }
3179 return val;
3180}
3181
3182static long_t mcexpr(struct compile_state *state, int index)
3183{
3184 return mlor_expr(state, index);
3185}
3186static void preprocess(struct compile_state *state, int index)
3187{
3188 /* Doing much more with the preprocessor would require
3189 * a parser and a major restructuring.
3190 * Postpone that for later.
3191 */
3192 struct file_state *file;
3193 struct token *tk;
3194 int line;
3195 int tok;
3196
3197 file = state->file;
3198 tk = &state->token[index];
3199 state->macro_line = line = file->line;
3200 state->macro_file = file;
3201
3202 next_token(state, index);
3203 ident_to_macro(state, tk);
3204 if (tk->tok == TOK_IDENT) {
3205 error(state, 0, "undefined preprocessing directive `%s'",
3206 tk->ident->name);
3207 }
3208 switch(tk->tok) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00003209 case TOK_LIT_INT:
3210 {
3211 int override_line;
3212 override_line = strtoul(tk->val.str, 0, 10);
3213 next_token(state, index);
3214 /* I have a cpp line marker parse it */
3215 if (tk->tok == TOK_LIT_STRING) {
3216 const char *token, *base;
3217 char *name, *dir;
3218 int name_len, dir_len;
3219 name = xmalloc(tk->str_len, "report_name");
3220 token = tk->val.str + 1;
3221 base = strrchr(token, '/');
3222 name_len = tk->str_len -2;
3223 if (base != 0) {
3224 dir_len = base - token;
3225 base++;
3226 name_len -= base - token;
3227 } else {
3228 dir_len = 0;
3229 base = token;
3230 }
3231 memcpy(name, base, name_len);
3232 name[name_len] = '\0';
3233 dir = xmalloc(dir_len + 1, "report_dir");
3234 memcpy(dir, token, dir_len);
3235 dir[dir_len] = '\0';
3236 file->report_line = override_line - 1;
3237 file->report_name = name;
3238 file->report_dir = dir;
3239 }
3240 }
3241 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003242 case TOK_LINE:
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00003243 meat(state, index, TOK_LINE);
3244 meat(state, index, TOK_LIT_INT);
3245 file->report_line = strtoul(tk->val.str, 0, 10) -1;
3246 if (mpeek(state, index) == TOK_LIT_STRING) {
3247 const char *token, *base;
3248 char *name, *dir;
3249 int name_len, dir_len;
3250 meat(state, index, TOK_LIT_STRING);
3251 name = xmalloc(tk->str_len, "report_name");
3252 token = tk->val.str + 1;
3253 name_len = tk->str_len - 2;
3254 if (base != 0) {
3255 dir_len = base - token;
3256 base++;
3257 name_len -= base - token;
3258 } else {
3259 dir_len = 0;
3260 base = token;
3261 }
3262 memcpy(name, base, name_len);
3263 name[name_len] = '\0';
3264 dir = xmalloc(dir_len + 1, "report_dir");
3265 memcpy(dir, token, dir_len);
3266 dir[dir_len] = '\0';
3267 file->report_name = name;
3268 file->report_dir = dir;
3269 }
3270 break;
3271 case TOK_UNDEF:
Eric Biedermanb138ac82003-04-22 18:44:01 +00003272 case TOK_PRAGMA:
3273 if (state->if_value < 0) {
3274 break;
3275 }
3276 warning(state, 0, "Ignoring preprocessor directive: %s",
3277 tk->ident->name);
3278 break;
3279 case TOK_ELIF:
3280 error(state, 0, "#elif not supported");
3281#warning "FIXME multiple #elif and #else in an #if do not work properly"
3282 if (state->if_depth == 0) {
3283 error(state, 0, "#elif without #if");
3284 }
3285 /* If the #if was taken the #elif just disables the following code */
3286 if (state->if_value >= 0) {
3287 state->if_value = - state->if_value;
3288 }
3289 /* If the previous #if was not taken see if the #elif enables the
3290 * trailing code.
3291 */
3292 else if ((state->if_value < 0) &&
3293 (state->if_depth == - state->if_value))
3294 {
3295 if (mcexpr(state, index) != 0) {
3296 state->if_value = state->if_depth;
3297 }
3298 else {
3299 state->if_value = - state->if_depth;
3300 }
3301 }
3302 break;
3303 case TOK_IF:
3304 state->if_depth++;
3305 if (state->if_value < 0) {
3306 break;
3307 }
3308 if (mcexpr(state, index) != 0) {
3309 state->if_value = state->if_depth;
3310 }
3311 else {
3312 state->if_value = - state->if_depth;
3313 }
3314 break;
3315 case TOK_IFNDEF:
3316 state->if_depth++;
3317 if (state->if_value < 0) {
3318 break;
3319 }
3320 next_token(state, index);
3321 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3322 error(state, 0, "Invalid macro name");
3323 }
3324 if (tk->ident->sym_define == 0) {
3325 state->if_value = state->if_depth;
3326 }
3327 else {
3328 state->if_value = - state->if_depth;
3329 }
3330 break;
3331 case TOK_IFDEF:
3332 state->if_depth++;
3333 if (state->if_value < 0) {
3334 break;
3335 }
3336 next_token(state, index);
3337 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3338 error(state, 0, "Invalid macro name");
3339 }
3340 if (tk->ident->sym_define != 0) {
3341 state->if_value = state->if_depth;
3342 }
3343 else {
3344 state->if_value = - state->if_depth;
3345 }
3346 break;
3347 case TOK_ELSE:
3348 if (state->if_depth == 0) {
3349 error(state, 0, "#else without #if");
3350 }
3351 if ((state->if_value >= 0) ||
3352 ((state->if_value < 0) &&
3353 (state->if_depth == -state->if_value)))
3354 {
3355 state->if_value = - state->if_value;
3356 }
3357 break;
3358 case TOK_ENDIF:
3359 if (state->if_depth == 0) {
3360 error(state, 0, "#endif without #if");
3361 }
3362 if ((state->if_value >= 0) ||
3363 ((state->if_value < 0) &&
3364 (state->if_depth == -state->if_value)))
3365 {
3366 state->if_value = state->if_depth - 1;
3367 }
3368 state->if_depth--;
3369 break;
3370 case TOK_DEFINE:
3371 {
3372 struct hash_entry *ident;
3373 struct macro *macro;
3374 char *ptr;
3375
3376 if (state->if_value < 0) /* quit early when #if'd out */
3377 break;
3378
3379 meat(state, index, TOK_IDENT);
3380 ident = tk->ident;
3381
3382
3383 if (*file->pos == '(') {
3384#warning "FIXME macros with arguments not supported"
3385 error(state, 0, "Macros with arguments not supported");
3386 }
3387
3388 /* Find the end of the line to get an estimate of
3389 * the macro's length.
3390 */
3391 for(ptr = file->pos; *ptr != '\n'; ptr++)
3392 ;
3393
3394 if (ident->sym_define != 0) {
3395 error(state, 0, "macro %s already defined\n", ident->name);
3396 }
3397 macro = xmalloc(sizeof(*macro), "macro");
3398 macro->ident = ident;
3399 macro->buf_len = ptr - file->pos +1;
3400 macro->buf = xmalloc(macro->buf_len +2, "macro buf");
3401
3402 memcpy(macro->buf, file->pos, macro->buf_len);
3403 macro->buf[macro->buf_len] = '\n';
3404 macro->buf[macro->buf_len +1] = '\0';
3405
3406 ident->sym_define = macro;
3407 break;
3408 }
3409 case TOK_ERROR:
3410 {
3411 char *end;
3412 int len;
3413 /* Find the end of the line */
3414 for(end = file->pos; *end != '\n'; end++)
3415 ;
3416 len = (end - file->pos);
3417 if (state->if_value >= 0) {
3418 error(state, 0, "%*.*s", len, len, file->pos);
3419 }
3420 file->pos = end;
3421 break;
3422 }
3423 case TOK_WARNING:
3424 {
3425 char *end;
3426 int len;
3427 /* Find the end of the line */
3428 for(end = file->pos; *end != '\n'; end++)
3429 ;
3430 len = (end - file->pos);
3431 if (state->if_value >= 0) {
3432 warning(state, 0, "%*.*s", len, len, file->pos);
3433 }
3434 file->pos = end;
3435 break;
3436 }
3437 case TOK_INCLUDE:
3438 {
3439 char *name;
3440 char *ptr;
3441 int local;
3442 local = 0;
3443 name = 0;
3444 next_token(state, index);
3445 if (tk->tok == TOK_LIT_STRING) {
3446 const char *token;
3447 int name_len;
3448 name = xmalloc(tk->str_len, "include");
3449 token = tk->val.str +1;
3450 name_len = tk->str_len -2;
3451 if (*token == '"') {
3452 token++;
3453 name_len--;
3454 }
3455 memcpy(name, token, name_len);
3456 name[name_len] = '\0';
3457 local = 1;
3458 }
3459 else if (tk->tok == TOK_LESS) {
3460 char *start, *end;
3461 start = file->pos;
3462 for(end = start; *end != '\n'; end++) {
3463 if (*end == '>') {
3464 break;
3465 }
3466 }
3467 if (*end == '\n') {
3468 error(state, 0, "Unterminated included directive");
3469 }
3470 name = xmalloc(end - start + 1, "include");
3471 memcpy(name, start, end - start);
3472 name[end - start] = '\0';
3473 file->pos = end +1;
3474 local = 0;
3475 }
3476 else {
3477 error(state, 0, "Invalid include directive");
3478 }
3479 /* Error if there are any characters after the include */
3480 for(ptr = file->pos; *ptr != '\n'; ptr++) {
Eric Biederman8d9c1232003-06-17 08:42:17 +00003481 switch(*ptr) {
3482 case ' ':
3483 case '\t':
3484 case '\v':
3485 break;
3486 default:
Eric Biedermanb138ac82003-04-22 18:44:01 +00003487 error(state, 0, "garbage after include directive");
3488 }
3489 }
3490 if (state->if_value >= 0) {
3491 compile_file(state, name, local);
3492 }
3493 xfree(name);
3494 next_token(state, index);
3495 return;
3496 }
3497 default:
3498 /* Ignore # without a following ident */
3499 if (tk->tok == TOK_IDENT) {
3500 error(state, 0, "Invalid preprocessor directive: %s",
3501 tk->ident->name);
3502 }
3503 break;
3504 }
3505 /* Consume the rest of the macro line */
3506 do {
3507 tok = mpeek(state, index);
3508 meat(state, index, tok);
3509 } while(tok != TOK_EOF);
3510 return;
3511}
3512
3513static void token(struct compile_state *state, int index)
3514{
3515 struct file_state *file;
3516 struct token *tk;
3517 int rescan;
3518
3519 tk = &state->token[index];
3520 next_token(state, index);
3521 do {
3522 rescan = 0;
3523 file = state->file;
3524 if (tk->tok == TOK_EOF && file->prev) {
3525 state->file = file->prev;
3526 /* file->basename is used keep it */
3527 xfree(file->dirname);
3528 xfree(file->buf);
3529 xfree(file);
3530 next_token(state, index);
3531 rescan = 1;
3532 }
3533 else if (tk->tok == TOK_MACRO) {
3534 preprocess(state, index);
3535 rescan = 1;
3536 }
3537 else if (tk->ident && tk->ident->sym_define) {
3538 compile_macro(state, tk);
3539 next_token(state, index);
3540 rescan = 1;
3541 }
3542 else if (state->if_value < 0) {
3543 next_token(state, index);
3544 rescan = 1;
3545 }
3546 } while(rescan);
3547}
3548
3549static int peek(struct compile_state *state)
3550{
3551 if (state->token[1].tok == -1) {
3552 token(state, 1);
3553 }
3554 return state->token[1].tok;
3555}
3556
3557static int peek2(struct compile_state *state)
3558{
3559 if (state->token[1].tok == -1) {
3560 token(state, 1);
3561 }
3562 if (state->token[2].tok == -1) {
3563 token(state, 2);
3564 }
3565 return state->token[2].tok;
3566}
3567
Eric Biederman0babc1c2003-05-09 02:39:00 +00003568static void eat(struct compile_state *state, int tok)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003569{
3570 int next_tok;
3571 int i;
3572 next_tok = peek(state);
3573 if (next_tok != tok) {
3574 const char *name1, *name2;
3575 name1 = tokens[next_tok];
3576 name2 = "";
3577 if (next_tok == TOK_IDENT) {
3578 name2 = state->token[1].ident->name;
3579 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00003580 error(state, 0, "\tfound %s %s expected %s",
3581 name1, name2 ,tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003582 }
3583 /* Free the old token value */
3584 if (state->token[0].str_len) {
3585 xfree((void *)(state->token[0].val.str));
3586 }
3587 for(i = 0; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
3588 state->token[i] = state->token[i + 1];
3589 }
3590 memset(&state->token[i], 0, sizeof(state->token[i]));
3591 state->token[i].tok = -1;
3592}
Eric Biedermanb138ac82003-04-22 18:44:01 +00003593
3594#warning "FIXME do not hardcode the include paths"
3595static char *include_paths[] = {
3596 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/include",
3597 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/arch/i386/include",
3598 "/home/eric/projects/linuxbios/checkin/solo/freebios2/src",
3599 0
3600};
3601
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003602static void compile_file(struct compile_state *state, const char *filename, int local)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003603{
3604 char cwd[4096];
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003605 const char *subdir, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003606 int subdir_len;
3607 struct file_state *file;
3608 char *basename;
3609 file = xmalloc(sizeof(*file), "file_state");
3610
3611 base = strrchr(filename, '/');
3612 subdir = filename;
3613 if (base != 0) {
3614 subdir_len = base - filename;
3615 base++;
3616 }
3617 else {
3618 base = filename;
3619 subdir_len = 0;
3620 }
3621 basename = xmalloc(strlen(base) +1, "basename");
3622 strcpy(basename, base);
3623 file->basename = basename;
3624
3625 if (getcwd(cwd, sizeof(cwd)) == 0) {
3626 die("cwd buffer to small");
3627 }
3628
3629 if (subdir[0] == '/') {
3630 file->dirname = xmalloc(subdir_len + 1, "dirname");
3631 memcpy(file->dirname, subdir, subdir_len);
3632 file->dirname[subdir_len] = '\0';
3633 }
3634 else {
3635 char *dir;
3636 int dirlen;
3637 char **path;
3638 /* Find the appropriate directory... */
3639 dir = 0;
3640 if (!state->file && exists(cwd, filename)) {
3641 dir = cwd;
3642 }
3643 if (local && state->file && exists(state->file->dirname, filename)) {
3644 dir = state->file->dirname;
3645 }
3646 for(path = include_paths; !dir && *path; path++) {
3647 if (exists(*path, filename)) {
3648 dir = *path;
3649 }
3650 }
3651 if (!dir) {
3652 error(state, 0, "Cannot find `%s'\n", filename);
3653 }
3654 dirlen = strlen(dir);
3655 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
3656 memcpy(file->dirname, dir, dirlen);
3657 file->dirname[dirlen] = '/';
3658 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
3659 file->dirname[dirlen + 1 + subdir_len] = '\0';
3660 }
3661 file->buf = slurp_file(file->dirname, file->basename, &file->size);
3662 xchdir(cwd);
3663
3664 file->pos = file->buf;
3665 file->line_start = file->pos;
3666 file->line = 1;
3667
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00003668 file->report_line = 1;
3669 file->report_name = file->basename;
3670 file->report_dir = file->dirname;
3671
Eric Biedermanb138ac82003-04-22 18:44:01 +00003672 file->prev = state->file;
3673 state->file = file;
3674
3675 process_trigraphs(state);
3676 splice_lines(state);
3677}
3678
Eric Biederman0babc1c2003-05-09 02:39:00 +00003679/* Type helper functions */
Eric Biedermanb138ac82003-04-22 18:44:01 +00003680
3681static struct type *new_type(
3682 unsigned int type, struct type *left, struct type *right)
3683{
3684 struct type *result;
3685 result = xmalloc(sizeof(*result), "type");
3686 result->type = type;
3687 result->left = left;
3688 result->right = right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00003689 result->field_ident = 0;
3690 result->type_ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003691 return result;
3692}
3693
3694static struct type *clone_type(unsigned int specifiers, struct type *old)
3695{
3696 struct type *result;
3697 result = xmalloc(sizeof(*result), "type");
3698 memcpy(result, old, sizeof(*result));
3699 result->type &= TYPE_MASK;
3700 result->type |= specifiers;
3701 return result;
3702}
3703
3704#define SIZEOF_SHORT 2
3705#define SIZEOF_INT 4
3706#define SIZEOF_LONG (sizeof(long_t))
3707
3708#define ALIGNOF_SHORT 2
3709#define ALIGNOF_INT 4
3710#define ALIGNOF_LONG (sizeof(long_t))
3711
3712#define MASK_UCHAR(X) ((X) & ((ulong_t)0xff))
3713#define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT*8)) - 1))
3714static inline ulong_t mask_uint(ulong_t x)
3715{
3716 if (SIZEOF_INT < SIZEOF_LONG) {
3717 ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT*8))) -1;
3718 x &= mask;
3719 }
3720 return x;
3721}
3722#define MASK_UINT(X) (mask_uint(X))
3723#define MASK_ULONG(X) (X)
3724
Eric Biedermanb138ac82003-04-22 18:44:01 +00003725static struct type void_type = { .type = TYPE_VOID };
3726static struct type char_type = { .type = TYPE_CHAR };
3727static struct type uchar_type = { .type = TYPE_UCHAR };
3728static struct type short_type = { .type = TYPE_SHORT };
3729static struct type ushort_type = { .type = TYPE_USHORT };
3730static struct type int_type = { .type = TYPE_INT };
3731static struct type uint_type = { .type = TYPE_UINT };
3732static struct type long_type = { .type = TYPE_LONG };
3733static struct type ulong_type = { .type = TYPE_ULONG };
3734
3735static struct triple *variable(struct compile_state *state, struct type *type)
3736{
3737 struct triple *result;
3738 if ((type->type & STOR_MASK) != STOR_PERM) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00003739 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3740 result = triple(state, OP_ADECL, type, 0, 0);
3741 } else {
3742 struct type *field;
3743 struct triple **vector;
3744 ulong_t index;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003745 result = new_triple(state, OP_VAL_VEC, type, -1, -1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00003746 vector = &result->param[0];
3747
3748 field = type->left;
3749 index = 0;
3750 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
3751 vector[index] = variable(state, field->left);
3752 field = field->right;
3753 index++;
3754 }
3755 vector[index] = variable(state, field);
3756 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003757 }
3758 else {
3759 result = triple(state, OP_SDECL, type, 0, 0);
3760 }
3761 return result;
3762}
3763
3764static void stor_of(FILE *fp, struct type *type)
3765{
3766 switch(type->type & STOR_MASK) {
3767 case STOR_AUTO:
3768 fprintf(fp, "auto ");
3769 break;
3770 case STOR_STATIC:
3771 fprintf(fp, "static ");
3772 break;
3773 case STOR_EXTERN:
3774 fprintf(fp, "extern ");
3775 break;
3776 case STOR_REGISTER:
3777 fprintf(fp, "register ");
3778 break;
3779 case STOR_TYPEDEF:
3780 fprintf(fp, "typedef ");
3781 break;
3782 case STOR_INLINE:
3783 fprintf(fp, "inline ");
3784 break;
3785 }
3786}
3787static void qual_of(FILE *fp, struct type *type)
3788{
3789 if (type->type & QUAL_CONST) {
3790 fprintf(fp, " const");
3791 }
3792 if (type->type & QUAL_VOLATILE) {
3793 fprintf(fp, " volatile");
3794 }
3795 if (type->type & QUAL_RESTRICT) {
3796 fprintf(fp, " restrict");
3797 }
3798}
Eric Biederman0babc1c2003-05-09 02:39:00 +00003799
Eric Biedermanb138ac82003-04-22 18:44:01 +00003800static void name_of(FILE *fp, struct type *type)
3801{
3802 stor_of(fp, type);
3803 switch(type->type & TYPE_MASK) {
3804 case TYPE_VOID:
3805 fprintf(fp, "void");
3806 qual_of(fp, type);
3807 break;
3808 case TYPE_CHAR:
3809 fprintf(fp, "signed char");
3810 qual_of(fp, type);
3811 break;
3812 case TYPE_UCHAR:
3813 fprintf(fp, "unsigned char");
3814 qual_of(fp, type);
3815 break;
3816 case TYPE_SHORT:
3817 fprintf(fp, "signed short");
3818 qual_of(fp, type);
3819 break;
3820 case TYPE_USHORT:
3821 fprintf(fp, "unsigned short");
3822 qual_of(fp, type);
3823 break;
3824 case TYPE_INT:
3825 fprintf(fp, "signed int");
3826 qual_of(fp, type);
3827 break;
3828 case TYPE_UINT:
3829 fprintf(fp, "unsigned int");
3830 qual_of(fp, type);
3831 break;
3832 case TYPE_LONG:
3833 fprintf(fp, "signed long");
3834 qual_of(fp, type);
3835 break;
3836 case TYPE_ULONG:
3837 fprintf(fp, "unsigned long");
3838 qual_of(fp, type);
3839 break;
3840 case TYPE_POINTER:
3841 name_of(fp, type->left);
3842 fprintf(fp, " * ");
3843 qual_of(fp, type);
3844 break;
3845 case TYPE_PRODUCT:
3846 case TYPE_OVERLAP:
3847 name_of(fp, type->left);
3848 fprintf(fp, ", ");
3849 name_of(fp, type->right);
3850 break;
3851 case TYPE_ENUM:
Eric Biederman0babc1c2003-05-09 02:39:00 +00003852 fprintf(fp, "enum %s", type->type_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003853 qual_of(fp, type);
3854 break;
3855 case TYPE_STRUCT:
Eric Biederman0babc1c2003-05-09 02:39:00 +00003856 fprintf(fp, "struct %s", type->type_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003857 qual_of(fp, type);
3858 break;
3859 case TYPE_FUNCTION:
3860 {
3861 name_of(fp, type->left);
3862 fprintf(fp, " (*)(");
3863 name_of(fp, type->right);
3864 fprintf(fp, ")");
3865 break;
3866 }
3867 case TYPE_ARRAY:
3868 name_of(fp, type->left);
3869 fprintf(fp, " [%ld]", type->elements);
3870 break;
3871 default:
3872 fprintf(fp, "????: %x", type->type & TYPE_MASK);
3873 break;
3874 }
3875}
3876
3877static size_t align_of(struct compile_state *state, struct type *type)
3878{
3879 size_t align;
3880 align = 0;
3881 switch(type->type & TYPE_MASK) {
3882 case TYPE_VOID:
3883 align = 1;
3884 break;
3885 case TYPE_CHAR:
3886 case TYPE_UCHAR:
3887 align = 1;
3888 break;
3889 case TYPE_SHORT:
3890 case TYPE_USHORT:
3891 align = ALIGNOF_SHORT;
3892 break;
3893 case TYPE_INT:
3894 case TYPE_UINT:
3895 case TYPE_ENUM:
3896 align = ALIGNOF_INT;
3897 break;
3898 case TYPE_LONG:
3899 case TYPE_ULONG:
3900 case TYPE_POINTER:
3901 align = ALIGNOF_LONG;
3902 break;
3903 case TYPE_PRODUCT:
3904 case TYPE_OVERLAP:
3905 {
3906 size_t left_align, right_align;
3907 left_align = align_of(state, type->left);
3908 right_align = align_of(state, type->right);
3909 align = (left_align >= right_align) ? left_align : right_align;
3910 break;
3911 }
3912 case TYPE_ARRAY:
3913 align = align_of(state, type->left);
3914 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00003915 case TYPE_STRUCT:
3916 align = align_of(state, type->left);
3917 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003918 default:
3919 error(state, 0, "alignof not yet defined for type\n");
3920 break;
3921 }
3922 return align;
3923}
3924
3925static size_t size_of(struct compile_state *state, struct type *type)
3926{
3927 size_t size;
3928 size = 0;
3929 switch(type->type & TYPE_MASK) {
3930 case TYPE_VOID:
3931 size = 0;
3932 break;
3933 case TYPE_CHAR:
3934 case TYPE_UCHAR:
3935 size = 1;
3936 break;
3937 case TYPE_SHORT:
3938 case TYPE_USHORT:
3939 size = SIZEOF_SHORT;
3940 break;
3941 case TYPE_INT:
3942 case TYPE_UINT:
3943 case TYPE_ENUM:
3944 size = SIZEOF_INT;
3945 break;
3946 case TYPE_LONG:
3947 case TYPE_ULONG:
3948 case TYPE_POINTER:
3949 size = SIZEOF_LONG;
3950 break;
3951 case TYPE_PRODUCT:
3952 {
3953 size_t align, pad;
3954 size = size_of(state, type->left);
3955 while((type->right->type & TYPE_MASK) == TYPE_PRODUCT) {
3956 type = type->right;
3957 align = align_of(state, type->left);
3958 pad = align - (size % align);
3959 size = size + pad + size_of(state, type->left);
3960 }
3961 align = align_of(state, type->right);
3962 pad = align - (size % align);
3963 size = size + pad + sizeof(type->right);
3964 break;
3965 }
3966 case TYPE_OVERLAP:
3967 {
3968 size_t size_left, size_right;
3969 size_left = size_of(state, type->left);
3970 size_right = size_of(state, type->right);
3971 size = (size_left >= size_right)? size_left : size_right;
3972 break;
3973 }
3974 case TYPE_ARRAY:
3975 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
3976 internal_error(state, 0, "Invalid array type");
3977 } else {
3978 size = size_of(state, type->left) * type->elements;
3979 }
3980 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00003981 case TYPE_STRUCT:
3982 size = size_of(state, type->left);
3983 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003984 default:
3985 error(state, 0, "sizeof not yet defined for type\n");
3986 break;
3987 }
3988 return size;
3989}
3990
Eric Biederman0babc1c2003-05-09 02:39:00 +00003991static size_t field_offset(struct compile_state *state,
3992 struct type *type, struct hash_entry *field)
3993{
3994 size_t size, align, pad;
3995 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3996 internal_error(state, 0, "field_offset only works on structures");
3997 }
3998 size = 0;
3999 type = type->left;
4000 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4001 if (type->left->field_ident == field) {
4002 type = type->left;
4003 }
4004 size += size_of(state, type->left);
4005 type = type->right;
4006 align = align_of(state, type->left);
4007 pad = align - (size % align);
4008 size += pad;
4009 }
4010 if (type->field_ident != field) {
4011 internal_error(state, 0, "field_offset: member %s not present",
4012 field->name);
4013 }
4014 return size;
4015}
4016
4017static struct type *field_type(struct compile_state *state,
4018 struct type *type, struct hash_entry *field)
4019{
4020 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4021 internal_error(state, 0, "field_type only works on structures");
4022 }
4023 type = type->left;
4024 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4025 if (type->left->field_ident == field) {
4026 type = type->left;
4027 break;
4028 }
4029 type = type->right;
4030 }
4031 if (type->field_ident != field) {
4032 internal_error(state, 0, "field_type: member %s not present",
4033 field->name);
4034 }
4035 return type;
4036}
4037
4038static struct triple *struct_field(struct compile_state *state,
4039 struct triple *decl, struct hash_entry *field)
4040{
4041 struct triple **vector;
4042 struct type *type;
4043 ulong_t index;
4044 type = decl->type;
4045 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4046 return decl;
4047 }
4048 if (decl->op != OP_VAL_VEC) {
4049 internal_error(state, 0, "Invalid struct variable");
4050 }
4051 if (!field) {
4052 internal_error(state, 0, "Missing structure field");
4053 }
4054 type = type->left;
4055 vector = &RHS(decl, 0);
4056 index = 0;
4057 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4058 if (type->left->field_ident == field) {
4059 type = type->left;
4060 break;
4061 }
4062 index += 1;
4063 type = type->right;
4064 }
4065 if (type->field_ident != field) {
4066 internal_error(state, 0, "field %s not found?", field->name);
4067 }
4068 return vector[index];
4069}
4070
Eric Biedermanb138ac82003-04-22 18:44:01 +00004071static void arrays_complete(struct compile_state *state, struct type *type)
4072{
4073 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
4074 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4075 error(state, 0, "array size not specified");
4076 }
4077 arrays_complete(state, type->left);
4078 }
4079}
4080
4081static unsigned int do_integral_promotion(unsigned int type)
4082{
4083 type &= TYPE_MASK;
4084 if (TYPE_INTEGER(type) &&
4085 TYPE_RANK(type) < TYPE_RANK(TYPE_INT)) {
4086 type = TYPE_INT;
4087 }
4088 return type;
4089}
4090
4091static unsigned int do_arithmetic_conversion(
4092 unsigned int left, unsigned int right)
4093{
4094 left &= TYPE_MASK;
4095 right &= TYPE_MASK;
4096 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
4097 return TYPE_LDOUBLE;
4098 }
4099 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
4100 return TYPE_DOUBLE;
4101 }
4102 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
4103 return TYPE_FLOAT;
4104 }
4105 left = do_integral_promotion(left);
4106 right = do_integral_promotion(right);
4107 /* If both operands have the same size done */
4108 if (left == right) {
4109 return left;
4110 }
4111 /* If both operands have the same signedness pick the larger */
4112 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
4113 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
4114 }
4115 /* If the signed type can hold everything use it */
4116 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
4117 return left;
4118 }
4119 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
4120 return right;
4121 }
4122 /* Convert to the unsigned type with the same rank as the signed type */
4123 else if (TYPE_SIGNED(left)) {
4124 return TYPE_MKUNSIGNED(left);
4125 }
4126 else {
4127 return TYPE_MKUNSIGNED(right);
4128 }
4129}
4130
4131/* see if two types are the same except for qualifiers */
4132static int equiv_types(struct type *left, struct type *right)
4133{
4134 unsigned int type;
4135 /* Error if the basic types do not match */
4136 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4137 return 0;
4138 }
4139 type = left->type & TYPE_MASK;
4140 /* if the basic types match and it is an arithmetic type we are done */
4141 if (TYPE_ARITHMETIC(type)) {
4142 return 1;
4143 }
4144 /* If it is a pointer type recurse and keep testing */
4145 if (type == TYPE_POINTER) {
4146 return equiv_types(left->left, right->left);
4147 }
4148 else if (type == TYPE_ARRAY) {
4149 return (left->elements == right->elements) &&
4150 equiv_types(left->left, right->left);
4151 }
4152 /* test for struct/union equality */
4153 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004154 return left->type_ident == right->type_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004155 }
4156 /* Test for equivalent functions */
4157 else if (type == TYPE_FUNCTION) {
4158 return equiv_types(left->left, right->left) &&
4159 equiv_types(left->right, right->right);
4160 }
4161 /* We only see TYPE_PRODUCT as part of function equivalence matching */
4162 else if (type == TYPE_PRODUCT) {
4163 return equiv_types(left->left, right->left) &&
4164 equiv_types(left->right, right->right);
4165 }
4166 /* We should see TYPE_OVERLAP */
4167 else {
4168 return 0;
4169 }
4170}
4171
4172static int equiv_ptrs(struct type *left, struct type *right)
4173{
4174 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4175 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4176 return 0;
4177 }
4178 return equiv_types(left->left, right->left);
4179}
4180
4181static struct type *compatible_types(struct type *left, struct type *right)
4182{
4183 struct type *result;
4184 unsigned int type, qual_type;
4185 /* Error if the basic types do not match */
4186 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4187 return 0;
4188 }
4189 type = left->type & TYPE_MASK;
4190 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4191 result = 0;
4192 /* if the basic types match and it is an arithmetic type we are done */
4193 if (TYPE_ARITHMETIC(type)) {
4194 result = new_type(qual_type, 0, 0);
4195 }
4196 /* If it is a pointer type recurse and keep testing */
4197 else if (type == TYPE_POINTER) {
4198 result = compatible_types(left->left, right->left);
4199 if (result) {
4200 result = new_type(qual_type, result, 0);
4201 }
4202 }
4203 /* test for struct/union equality */
4204 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004205 if (left->type_ident == right->type_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004206 result = left;
4207 }
4208 }
4209 /* Test for equivalent functions */
4210 else if (type == TYPE_FUNCTION) {
4211 struct type *lf, *rf;
4212 lf = compatible_types(left->left, right->left);
4213 rf = compatible_types(left->right, right->right);
4214 if (lf && rf) {
4215 result = new_type(qual_type, lf, rf);
4216 }
4217 }
4218 /* We only see TYPE_PRODUCT as part of function equivalence matching */
4219 else if (type == TYPE_PRODUCT) {
4220 struct type *lf, *rf;
4221 lf = compatible_types(left->left, right->left);
4222 rf = compatible_types(left->right, right->right);
4223 if (lf && rf) {
4224 result = new_type(qual_type, lf, rf);
4225 }
4226 }
4227 else {
4228 /* Nothing else is compatible */
4229 }
4230 return result;
4231}
4232
4233static struct type *compatible_ptrs(struct type *left, struct type *right)
4234{
4235 struct type *result;
4236 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4237 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4238 return 0;
4239 }
4240 result = compatible_types(left->left, right->left);
4241 if (result) {
4242 unsigned int qual_type;
4243 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4244 result = new_type(qual_type, result, 0);
4245 }
4246 return result;
4247
4248}
4249static struct triple *integral_promotion(
4250 struct compile_state *state, struct triple *def)
4251{
4252 struct type *type;
4253 type = def->type;
4254 /* As all operations are carried out in registers
4255 * the values are converted on load I just convert
4256 * logical type of the operand.
4257 */
4258 if (TYPE_INTEGER(type->type)) {
4259 unsigned int int_type;
4260 int_type = type->type & ~TYPE_MASK;
4261 int_type |= do_integral_promotion(type->type);
4262 if (int_type != type->type) {
4263 def->type = new_type(int_type, 0, 0);
4264 }
4265 }
4266 return def;
4267}
4268
4269
4270static void arithmetic(struct compile_state *state, struct triple *def)
4271{
4272 if (!TYPE_ARITHMETIC(def->type->type)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004273 error(state, 0, "arithmetic type expexted");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004274 }
4275}
4276
4277static void ptr_arithmetic(struct compile_state *state, struct triple *def)
4278{
4279 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
4280 error(state, def, "pointer or arithmetic type expected");
4281 }
4282}
4283
4284static int is_integral(struct triple *ins)
4285{
4286 return TYPE_INTEGER(ins->type->type);
4287}
4288
4289static void integral(struct compile_state *state, struct triple *def)
4290{
4291 if (!is_integral(def)) {
4292 error(state, 0, "integral type expected");
4293 }
4294}
4295
4296
4297static void bool(struct compile_state *state, struct triple *def)
4298{
4299 if (!TYPE_ARITHMETIC(def->type->type) &&
4300 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
4301 error(state, 0, "arithmetic or pointer type expected");
4302 }
4303}
4304
4305static int is_signed(struct type *type)
4306{
4307 return !!TYPE_SIGNED(type->type);
4308}
4309
Eric Biederman0babc1c2003-05-09 02:39:00 +00004310/* Is this value located in a register otherwise it must be in memory */
4311static int is_in_reg(struct compile_state *state, struct triple *def)
4312{
4313 int in_reg;
4314 if (def->op == OP_ADECL) {
4315 in_reg = 1;
4316 }
4317 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
4318 in_reg = 0;
4319 }
4320 else if (def->op == OP_VAL_VEC) {
4321 in_reg = is_in_reg(state, RHS(def, 0));
4322 }
4323 else if (def->op == OP_DOT) {
4324 in_reg = is_in_reg(state, RHS(def, 0));
4325 }
4326 else {
4327 internal_error(state, 0, "unknown expr storage location");
4328 in_reg = -1;
4329 }
4330 return in_reg;
4331}
4332
Eric Biedermanb138ac82003-04-22 18:44:01 +00004333/* Is this a stable variable location otherwise it must be a temporary */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004334static int is_stable(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004335{
4336 int ret;
4337 ret = 0;
4338 if (!def) {
4339 return 0;
4340 }
4341 if ((def->op == OP_ADECL) ||
4342 (def->op == OP_SDECL) ||
4343 (def->op == OP_DEREF) ||
4344 (def->op == OP_BLOBCONST)) {
4345 ret = 1;
4346 }
4347 else if (def->op == OP_DOT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004348 ret = is_stable(state, RHS(def, 0));
4349 }
4350 else if (def->op == OP_VAL_VEC) {
4351 struct triple **vector;
4352 ulong_t i;
4353 ret = 1;
4354 vector = &RHS(def, 0);
4355 for(i = 0; i < def->type->elements; i++) {
4356 if (!is_stable(state, vector[i])) {
4357 ret = 0;
4358 break;
4359 }
4360 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004361 }
4362 return ret;
4363}
4364
Eric Biederman0babc1c2003-05-09 02:39:00 +00004365static int is_lvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004366{
4367 int ret;
4368 ret = 1;
4369 if (!def) {
4370 return 0;
4371 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004372 if (!is_stable(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004373 return 0;
4374 }
4375 if (def->type->type & QUAL_CONST) {
4376 ret = 0;
4377 }
4378 else if (def->op == OP_DOT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004379 ret = is_lvalue(state, RHS(def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00004380 }
4381 return ret;
4382}
4383
4384static void lvalue(struct compile_state *state, struct triple *def)
4385{
4386 if (!def) {
4387 internal_error(state, def, "nothing where lvalue expected?");
4388 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004389 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004390 error(state, def, "lvalue expected");
4391 }
4392}
4393
4394static int is_pointer(struct triple *def)
4395{
4396 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
4397}
4398
4399static void pointer(struct compile_state *state, struct triple *def)
4400{
4401 if (!is_pointer(def)) {
4402 error(state, def, "pointer expected");
4403 }
4404}
4405
4406static struct triple *int_const(
4407 struct compile_state *state, struct type *type, ulong_t value)
4408{
4409 struct triple *result;
4410 switch(type->type & TYPE_MASK) {
4411 case TYPE_CHAR:
4412 case TYPE_INT: case TYPE_UINT:
4413 case TYPE_LONG: case TYPE_ULONG:
4414 break;
4415 default:
4416 internal_error(state, 0, "constant for unkown type");
4417 }
4418 result = triple(state, OP_INTCONST, type, 0, 0);
4419 result->u.cval = value;
4420 return result;
4421}
4422
4423
Eric Biederman0babc1c2003-05-09 02:39:00 +00004424static struct triple *do_mk_addr_expr(struct compile_state *state,
4425 struct triple *expr, struct type *type, ulong_t offset)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004426{
4427 struct triple *result;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004428 lvalue(state, expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004429
4430 result = 0;
4431 if (expr->op == OP_ADECL) {
4432 error(state, expr, "address of auto variables not supported");
4433 }
4434 else if (expr->op == OP_SDECL) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004435 result = triple(state, OP_ADDRCONST, type, 0, 0);
4436 MISC(result, 0) = expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004437 result->u.cval = offset;
4438 }
4439 else if (expr->op == OP_DEREF) {
4440 result = triple(state, OP_ADD, type,
Eric Biederman0babc1c2003-05-09 02:39:00 +00004441 RHS(expr, 0),
Eric Biedermanb138ac82003-04-22 18:44:01 +00004442 int_const(state, &ulong_type, offset));
4443 }
4444 return result;
4445}
4446
Eric Biederman0babc1c2003-05-09 02:39:00 +00004447static struct triple *mk_addr_expr(
4448 struct compile_state *state, struct triple *expr, ulong_t offset)
4449{
4450 struct type *type;
4451
4452 type = new_type(
4453 TYPE_POINTER | (expr->type->type & QUAL_MASK),
4454 expr->type, 0);
4455
4456 return do_mk_addr_expr(state, expr, type, offset);
4457}
4458
Eric Biedermanb138ac82003-04-22 18:44:01 +00004459static struct triple *mk_deref_expr(
4460 struct compile_state *state, struct triple *expr)
4461{
4462 struct type *base_type;
4463 pointer(state, expr);
4464 base_type = expr->type->left;
4465 if (!TYPE_PTR(base_type->type) && !TYPE_ARITHMETIC(base_type->type)) {
4466 error(state, 0,
4467 "Only pointer and arithmetic values can be dereferenced");
4468 }
4469 return triple(state, OP_DEREF, base_type, expr, 0);
4470}
4471
Eric Biederman0babc1c2003-05-09 02:39:00 +00004472static struct triple *deref_field(
4473 struct compile_state *state, struct triple *expr, struct hash_entry *field)
4474{
4475 struct triple *result;
4476 struct type *type, *member;
4477 if (!field) {
4478 internal_error(state, 0, "No field passed to deref_field");
4479 }
4480 result = 0;
4481 type = expr->type;
4482 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4483 error(state, 0, "request for member %s in something not a struct or union",
4484 field->name);
4485 }
4486 member = type->left;
4487 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4488 if (member->left->field_ident == field) {
4489 member = member->left;
4490 break;
4491 }
4492 member = member->right;
4493 }
4494 if (member->field_ident != field) {
4495 error(state, 0, "%s is not a member", field->name);
4496 }
4497 if ((type->type & STOR_MASK) == STOR_PERM) {
4498 /* Do the pointer arithmetic to get a deref the field */
4499 ulong_t offset;
4500 offset = field_offset(state, type, field);
4501 result = do_mk_addr_expr(state, expr, member, offset);
4502 result = mk_deref_expr(state, result);
4503 }
4504 else {
4505 /* Find the variable for the field I want. */
4506 result = triple(state, OP_DOT,
4507 field_type(state, type, field), expr, 0);
4508 result->u.field = field;
4509 }
4510 return result;
4511}
4512
Eric Biedermanb138ac82003-04-22 18:44:01 +00004513static struct triple *read_expr(struct compile_state *state, struct triple *def)
4514{
4515 int op;
4516 if (!def) {
4517 return 0;
4518 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004519 if (!is_stable(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004520 return def;
4521 }
4522 /* Tranform an array to a pointer to the first element */
4523#warning "CHECK_ME is this the right place to transform arrays to pointers?"
4524 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
4525 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004526 struct triple *result;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004527 type = new_type(
4528 TYPE_POINTER | (def->type->type & QUAL_MASK),
4529 def->type->left, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004530 result = triple(state, OP_ADDRCONST, type, 0, 0);
4531 MISC(result, 0) = def;
4532 return result;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004533 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004534 if (is_in_reg(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004535 op = OP_READ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004536 } else {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004537 op = OP_LOAD;
4538 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004539 return triple(state, op, def->type, def, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004540}
4541
4542static void write_compatible(struct compile_state *state,
4543 struct type *dest, struct type *rval)
4544{
4545 int compatible = 0;
4546 /* Both operands have arithmetic type */
4547 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
4548 compatible = 1;
4549 }
4550 /* One operand is a pointer and the other is a pointer to void */
4551 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
4552 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
4553 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
4554 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
4555 compatible = 1;
4556 }
4557 /* If both types are the same without qualifiers we are good */
4558 else if (equiv_ptrs(dest, rval)) {
4559 compatible = 1;
4560 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004561 /* test for struct/union equality */
4562 else if (((dest->type & TYPE_MASK) == TYPE_STRUCT) &&
4563 ((rval->type & TYPE_MASK) == TYPE_STRUCT) &&
4564 (dest->type_ident == rval->type_ident)) {
4565 compatible = 1;
4566 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004567 if (!compatible) {
4568 error(state, 0, "Incompatible types in assignment");
4569 }
4570}
4571
4572static struct triple *write_expr(
4573 struct compile_state *state, struct triple *dest, struct triple *rval)
4574{
4575 struct triple *def;
4576 int op;
4577
4578 def = 0;
4579 if (!rval) {
4580 internal_error(state, 0, "missing rval");
4581 }
4582
4583 if (rval->op == OP_LIST) {
4584 internal_error(state, 0, "expression of type OP_LIST?");
4585 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004586 if (!is_lvalue(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004587 internal_error(state, 0, "writing to a non lvalue?");
4588 }
4589
4590 write_compatible(state, dest->type, rval->type);
4591
4592 /* Now figure out which assignment operator to use */
4593 op = -1;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004594 if (is_in_reg(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004595 op = OP_WRITE;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004596 } else {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004597 op = OP_STORE;
4598 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004599 def = triple(state, op, dest->type, dest, rval);
4600 return def;
4601}
4602
4603static struct triple *init_expr(
4604 struct compile_state *state, struct triple *dest, struct triple *rval)
4605{
4606 struct triple *def;
4607
4608 def = 0;
4609 if (!rval) {
4610 internal_error(state, 0, "missing rval");
4611 }
4612 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
4613 rval = read_expr(state, rval);
4614 def = write_expr(state, dest, rval);
4615 }
4616 else {
4617 /* Fill in the array size if necessary */
4618 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
4619 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
4620 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4621 dest->type->elements = rval->type->elements;
4622 }
4623 }
4624 if (!equiv_types(dest->type, rval->type)) {
4625 error(state, 0, "Incompatible types in inializer");
4626 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004627 MISC(dest, 0) = rval;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004628 insert_triple(state, dest, rval);
4629 rval->id |= TRIPLE_FLAG_FLATTENED;
4630 use_triple(MISC(dest, 0), dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004631 }
4632 return def;
4633}
4634
4635struct type *arithmetic_result(
4636 struct compile_state *state, struct triple *left, struct triple *right)
4637{
4638 struct type *type;
4639 /* Sanity checks to ensure I am working with arithmetic types */
4640 arithmetic(state, left);
4641 arithmetic(state, right);
4642 type = new_type(
4643 do_arithmetic_conversion(
4644 left->type->type,
4645 right->type->type), 0, 0);
4646 return type;
4647}
4648
4649struct type *ptr_arithmetic_result(
4650 struct compile_state *state, struct triple *left, struct triple *right)
4651{
4652 struct type *type;
4653 /* Sanity checks to ensure I am working with the proper types */
4654 ptr_arithmetic(state, left);
4655 arithmetic(state, right);
4656 if (TYPE_ARITHMETIC(left->type->type) &&
4657 TYPE_ARITHMETIC(right->type->type)) {
4658 type = arithmetic_result(state, left, right);
4659 }
4660 else if (TYPE_PTR(left->type->type)) {
4661 type = left->type;
4662 }
4663 else {
4664 internal_error(state, 0, "huh?");
4665 type = 0;
4666 }
4667 return type;
4668}
4669
4670
4671/* boolean helper function */
4672
4673static struct triple *ltrue_expr(struct compile_state *state,
4674 struct triple *expr)
4675{
4676 switch(expr->op) {
4677 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
4678 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
4679 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
4680 /* If the expression is already boolean do nothing */
4681 break;
4682 default:
4683 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
4684 break;
4685 }
4686 return expr;
4687}
4688
4689static struct triple *lfalse_expr(struct compile_state *state,
4690 struct triple *expr)
4691{
4692 return triple(state, OP_LFALSE, &int_type, expr, 0);
4693}
4694
4695static struct triple *cond_expr(
4696 struct compile_state *state,
4697 struct triple *test, struct triple *left, struct triple *right)
4698{
4699 struct triple *def;
4700 struct type *result_type;
4701 unsigned int left_type, right_type;
4702 bool(state, test);
4703 left_type = left->type->type;
4704 right_type = right->type->type;
4705 result_type = 0;
4706 /* Both operands have arithmetic type */
4707 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
4708 result_type = arithmetic_result(state, left, right);
4709 }
4710 /* Both operands have void type */
4711 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
4712 ((right_type & TYPE_MASK) == TYPE_VOID)) {
4713 result_type = &void_type;
4714 }
4715 /* pointers to the same type... */
4716 else if ((result_type = compatible_ptrs(left->type, right->type))) {
4717 ;
4718 }
4719 /* Both operands are pointers and left is a pointer to void */
4720 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4721 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4722 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4723 result_type = right->type;
4724 }
4725 /* Both operands are pointers and right is a pointer to void */
4726 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4727 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4728 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4729 result_type = left->type;
4730 }
4731 if (!result_type) {
4732 error(state, 0, "Incompatible types in conditional expression");
4733 }
Eric Biederman30276382003-05-16 20:47:48 +00004734 /* Cleanup and invert the test */
4735 test = lfalse_expr(state, read_expr(state, test));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004736 def = new_triple(state, OP_COND, result_type, 0, 3);
Eric Biederman0babc1c2003-05-09 02:39:00 +00004737 def->param[0] = test;
4738 def->param[1] = left;
4739 def->param[2] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004740 return def;
4741}
4742
4743
Eric Biederman0babc1c2003-05-09 02:39:00 +00004744static int expr_depth(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004745{
4746 int count;
4747 count = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004748 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
4749 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004750 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004751 else if (ins->op == OP_DEREF) {
4752 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004753 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004754 else if (ins->op == OP_VAL) {
4755 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004756 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004757 else if (ins->op == OP_COMMA) {
4758 int ldepth, rdepth;
4759 ldepth = expr_depth(state, RHS(ins, 0));
4760 rdepth = expr_depth(state, RHS(ins, 1));
4761 count = (ldepth >= rdepth)? ldepth : rdepth;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004762 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004763 else if (ins->op == OP_CALL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004764 /* Don't figure the depth of a call just guess it is huge */
4765 count = 1000;
4766 }
4767 else {
4768 struct triple **expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004769 expr = triple_rhs(state, ins, 0);
4770 for(;expr; expr = triple_rhs(state, ins, expr)) {
4771 if (*expr) {
4772 int depth;
4773 depth = expr_depth(state, *expr);
4774 if (depth > count) {
4775 count = depth;
4776 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004777 }
4778 }
4779 }
4780 return count + 1;
4781}
4782
4783static struct triple *flatten(
4784 struct compile_state *state, struct triple *first, struct triple *ptr);
4785
Eric Biederman0babc1c2003-05-09 02:39:00 +00004786static struct triple *flatten_generic(
Eric Biedermanb138ac82003-04-22 18:44:01 +00004787 struct compile_state *state, struct triple *first, struct triple *ptr)
4788{
Eric Biederman0babc1c2003-05-09 02:39:00 +00004789 struct rhs_vector {
4790 int depth;
4791 struct triple **ins;
4792 } vector[MAX_RHS];
4793 int i, rhs, lhs;
4794 /* Only operations with just a rhs should come here */
4795 rhs = TRIPLE_RHS(ptr->sizes);
4796 lhs = TRIPLE_LHS(ptr->sizes);
4797 if (TRIPLE_SIZE(ptr->sizes) != lhs + rhs) {
4798 internal_error(state, ptr, "unexpected args for: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +00004799 ptr->op, tops(ptr->op));
4800 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004801 /* Find the depth of the rhs elements */
4802 for(i = 0; i < rhs; i++) {
4803 vector[i].ins = &RHS(ptr, i);
4804 vector[i].depth = expr_depth(state, *vector[i].ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004805 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004806 /* Selection sort the rhs */
4807 for(i = 0; i < rhs; i++) {
4808 int j, max = i;
4809 for(j = i + 1; j < rhs; j++ ) {
4810 if (vector[j].depth > vector[max].depth) {
4811 max = j;
4812 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004813 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004814 if (max != i) {
4815 struct rhs_vector tmp;
4816 tmp = vector[i];
4817 vector[i] = vector[max];
4818 vector[max] = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004819 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004820 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004821 /* Now flatten the rhs elements */
4822 for(i = 0; i < rhs; i++) {
4823 *vector[i].ins = flatten(state, first, *vector[i].ins);
4824 use_triple(*vector[i].ins, ptr);
4825 }
4826
4827 /* Now flatten the lhs elements */
4828 for(i = 0; i < lhs; i++) {
4829 struct triple **ins = &LHS(ptr, i);
4830 *ins = flatten(state, first, *ins);
4831 use_triple(*ins, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004832 }
4833 return ptr;
4834}
4835
4836static struct triple *flatten_land(
4837 struct compile_state *state, struct triple *first, struct triple *ptr)
4838{
4839 struct triple *left, *right;
4840 struct triple *val, *test, *jmp, *label1, *end;
4841
4842 /* Find the triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004843 left = RHS(ptr, 0);
4844 right = RHS(ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004845
4846 /* Generate the needed triples */
4847 end = label(state);
4848
4849 /* Thread the triples together */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004850 val = flatten(state, first, variable(state, ptr->type));
4851 left = flatten(state, first, write_expr(state, val, left));
4852 test = flatten(state, first,
Eric Biedermanb138ac82003-04-22 18:44:01 +00004853 lfalse_expr(state, read_expr(state, val)));
Eric Biederman0babc1c2003-05-09 02:39:00 +00004854 jmp = flatten(state, first, branch(state, end, test));
4855 label1 = flatten(state, first, label(state));
4856 right = flatten(state, first, write_expr(state, val, right));
4857 TARG(jmp, 0) = flatten(state, first, end);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004858
4859 /* Now give the caller something to chew on */
4860 return read_expr(state, val);
4861}
4862
4863static struct triple *flatten_lor(
4864 struct compile_state *state, struct triple *first, struct triple *ptr)
4865{
4866 struct triple *left, *right;
4867 struct triple *val, *jmp, *label1, *end;
4868
4869 /* Find the triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004870 left = RHS(ptr, 0);
4871 right = RHS(ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004872
4873 /* Generate the needed triples */
4874 end = label(state);
4875
4876 /* Thread the triples together */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004877 val = flatten(state, first, variable(state, ptr->type));
4878 left = flatten(state, first, write_expr(state, val, left));
4879 jmp = flatten(state, first, branch(state, end, left));
4880 label1 = flatten(state, first, label(state));
4881 right = flatten(state, first, write_expr(state, val, right));
4882 TARG(jmp, 0) = flatten(state, first, end);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004883
4884
4885 /* Now give the caller something to chew on */
4886 return read_expr(state, val);
4887}
4888
4889static struct triple *flatten_cond(
4890 struct compile_state *state, struct triple *first, struct triple *ptr)
4891{
4892 struct triple *test, *left, *right;
4893 struct triple *val, *mv1, *jmp1, *label1, *mv2, *middle, *jmp2, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004894
4895 /* Find the triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004896 test = RHS(ptr, 0);
4897 left = RHS(ptr, 1);
4898 right = RHS(ptr, 2);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004899
4900 /* Generate the needed triples */
4901 end = label(state);
4902 middle = label(state);
4903
4904 /* Thread the triples together */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004905 val = flatten(state, first, variable(state, ptr->type));
4906 test = flatten(state, first, test);
4907 jmp1 = flatten(state, first, branch(state, middle, test));
4908 label1 = flatten(state, first, label(state));
4909 left = flatten(state, first, left);
4910 mv1 = flatten(state, first, write_expr(state, val, left));
4911 jmp2 = flatten(state, first, branch(state, end, 0));
4912 TARG(jmp1, 0) = flatten(state, first, middle);
4913 right = flatten(state, first, right);
4914 mv2 = flatten(state, first, write_expr(state, val, right));
4915 TARG(jmp2, 0) = flatten(state, first, end);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004916
4917 /* Now give the caller something to chew on */
4918 return read_expr(state, val);
4919}
4920
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004921struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
4922 struct occurance *base_occurance)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004923{
4924 struct triple *nfunc;
4925 struct triple *nfirst, *ofirst;
4926 struct triple *new, *old;
4927
4928#if 0
4929 fprintf(stdout, "\n");
4930 loc(stdout, state, 0);
4931 fprintf(stdout, "\n__________ copy_func _________\n");
4932 print_triple(state, ofunc);
4933 fprintf(stdout, "__________ copy_func _________ done\n\n");
4934#endif
4935
4936 /* Make a new copy of the old function */
4937 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
4938 nfirst = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00004939 ofirst = old = RHS(ofunc, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004940 do {
4941 struct triple *new;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004942 struct occurance *occurance;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004943 int old_lhs, old_rhs;
4944 old_lhs = TRIPLE_LHS(old->sizes);
Eric Biederman0babc1c2003-05-09 02:39:00 +00004945 old_rhs = TRIPLE_RHS(old->sizes);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004946 occurance = inline_occurance(state, base_occurance, old->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004947 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004948 occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004949 if (!triple_stores_block(state, new)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004950 memcpy(&new->u, &old->u, sizeof(new->u));
4951 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004952 if (!nfirst) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004953 RHS(nfunc, 0) = nfirst = new;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004954 }
4955 else {
4956 insert_triple(state, nfirst, new);
4957 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004958 new->id |= TRIPLE_FLAG_FLATTENED;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004959
4960 /* During the copy remember new as user of old */
4961 use_triple(old, new);
4962
4963 /* Populate the return type if present */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004964 if (old == MISC(ofunc, 0)) {
4965 MISC(nfunc, 0) = new;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004966 }
4967 old = old->next;
4968 } while(old != ofirst);
4969
4970 /* Make a second pass to fix up any unresolved references */
4971 old = ofirst;
4972 new = nfirst;
4973 do {
Eric Biederman0babc1c2003-05-09 02:39:00 +00004974 struct triple **oexpr, **nexpr;
4975 int count, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004976 /* Lookup where the copy is, to join pointers */
Eric Biederman0babc1c2003-05-09 02:39:00 +00004977 count = TRIPLE_SIZE(old->sizes);
4978 for(i = 0; i < count; i++) {
4979 oexpr = &old->param[i];
4980 nexpr = &new->param[i];
4981 if (!*nexpr && *oexpr && (*oexpr)->use) {
4982 *nexpr = (*oexpr)->use->member;
4983 if (*nexpr == old) {
4984 internal_error(state, 0, "new == old?");
4985 }
4986 use_triple(*nexpr, new);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004987 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00004988 if (!*nexpr && *oexpr) {
4989 internal_error(state, 0, "Could not copy %d\n", i);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004990 }
4991 }
4992 old = old->next;
4993 new = new->next;
4994 } while((old != ofirst) && (new != nfirst));
4995
4996 /* Make a third pass to cleanup the extra useses */
4997 old = ofirst;
4998 new = nfirst;
4999 do {
5000 unuse_triple(old, new);
5001 old = old->next;
5002 new = new->next;
5003 } while ((old != ofirst) && (new != nfirst));
5004 return nfunc;
5005}
5006
5007static struct triple *flatten_call(
5008 struct compile_state *state, struct triple *first, struct triple *ptr)
5009{
5010 /* Inline the function call */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005011 struct type *ptype;
5012 struct triple *ofunc, *nfunc, *nfirst, *param, *result;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005013 struct triple *end, *nend;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005014 int pvals, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005015
5016 /* Find the triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005017 ofunc = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005018 if (ofunc->op != OP_LIST) {
5019 internal_error(state, 0, "improper function");
5020 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005021 nfunc = copy_func(state, ofunc, ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005022 nfirst = RHS(nfunc, 0)->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005023 /* Prepend the parameter reading into the new function list */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005024 ptype = nfunc->type->right;
5025 param = RHS(nfunc, 0)->next;
5026 pvals = TRIPLE_RHS(ptr->sizes);
5027 for(i = 0; i < pvals; i++) {
5028 struct type *atype;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005029 struct triple *arg;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005030 atype = ptype;
5031 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
5032 atype = ptype->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005033 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005034 while((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
5035 param = param->next;
5036 }
5037 arg = RHS(ptr, i);
5038 flatten(state, nfirst, write_expr(state, param, arg));
5039 ptype = ptype->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005040 param = param->next;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005041 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005042 result = 0;
5043 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00005044 result = read_expr(state, MISC(nfunc,0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005045 }
5046#if 0
5047 fprintf(stdout, "\n");
5048 loc(stdout, state, 0);
5049 fprintf(stdout, "\n__________ flatten_call _________\n");
5050 print_triple(state, nfunc);
5051 fprintf(stdout, "__________ flatten_call _________ done\n\n");
5052#endif
5053
5054 /* Get rid of the extra triples */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005055 nfirst = RHS(nfunc, 0)->next;
5056 free_triple(state, RHS(nfunc, 0));
5057 RHS(nfunc, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005058 free_triple(state, nfunc);
5059
5060 /* Append the new function list onto the return list */
5061 end = first->prev;
5062 nend = nfirst->prev;
5063 end->next = nfirst;
5064 nfirst->prev = end;
5065 nend->next = first;
5066 first->prev = nend;
5067
5068 return result;
5069}
5070
5071static struct triple *flatten(
5072 struct compile_state *state, struct triple *first, struct triple *ptr)
5073{
5074 struct triple *orig_ptr;
5075 if (!ptr)
5076 return 0;
5077 do {
5078 orig_ptr = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005079 /* Only flatten triples once */
5080 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
5081 return ptr;
5082 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005083 switch(ptr->op) {
5084 case OP_WRITE:
5085 case OP_STORE:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005086 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5087 LHS(ptr, 0) = flatten(state, first, LHS(ptr, 0));
5088 use_triple(LHS(ptr, 0), ptr);
5089 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005090 break;
5091 case OP_COMMA:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005092 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5093 ptr = RHS(ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005094 break;
5095 case OP_VAL:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005096 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5097 return MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005098 break;
5099 case OP_LAND:
5100 ptr = flatten_land(state, first, ptr);
5101 break;
5102 case OP_LOR:
5103 ptr = flatten_lor(state, first, ptr);
5104 break;
5105 case OP_COND:
5106 ptr = flatten_cond(state, first, ptr);
5107 break;
5108 case OP_CALL:
5109 ptr = flatten_call(state, first, ptr);
5110 break;
5111 case OP_READ:
5112 case OP_LOAD:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005113 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5114 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005115 break;
5116 case OP_BRANCH:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005117 use_triple(TARG(ptr, 0), ptr);
5118 if (TRIPLE_RHS(ptr->sizes)) {
5119 use_triple(RHS(ptr, 0), ptr);
5120 if (ptr->next != ptr) {
5121 use_triple(ptr->next, ptr);
5122 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005123 }
5124 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005125 case OP_BLOBCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005126 insert_triple(state, first, ptr);
5127 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005128 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005129 use_triple(MISC(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005130 break;
5131 case OP_DEREF:
5132 /* Since OP_DEREF is just a marker delete it when I flatten it */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005133 ptr = RHS(ptr, 0);
5134 RHS(orig_ptr, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005135 free_triple(state, orig_ptr);
5136 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005137 case OP_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005138 {
5139 struct triple *base;
5140 base = RHS(ptr, 0);
5141 base = flatten(state, first, base);
5142 if (base->op == OP_VAL_VEC) {
5143 ptr = struct_field(state, base, ptr->u.field);
5144 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005145 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005146 }
Eric Biederman8d9c1232003-06-17 08:42:17 +00005147 case OP_PIECE:
5148 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5149 use_triple(MISC(ptr, 0), ptr);
5150 use_triple(ptr, MISC(ptr, 0));
5151 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005152 case OP_ADDRCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005153 case OP_SDECL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005154 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5155 use_triple(MISC(ptr, 0), ptr);
5156 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005157 case OP_ADECL:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005158 break;
5159 default:
5160 /* Flatten the easy cases we don't override */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005161 ptr = flatten_generic(state, first, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005162 break;
5163 }
5164 } while(ptr && (ptr != orig_ptr));
Eric Biederman0babc1c2003-05-09 02:39:00 +00005165 if (ptr) {
5166 insert_triple(state, first, ptr);
5167 ptr->id |= TRIPLE_FLAG_FLATTENED;
5168 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005169 return ptr;
5170}
5171
5172static void release_expr(struct compile_state *state, struct triple *expr)
5173{
5174 struct triple *head;
5175 head = label(state);
5176 flatten(state, head, expr);
5177 while(head->next != head) {
5178 release_triple(state, head->next);
5179 }
5180 free_triple(state, head);
5181}
5182
5183static int replace_rhs_use(struct compile_state *state,
5184 struct triple *orig, struct triple *new, struct triple *use)
5185{
5186 struct triple **expr;
5187 int found;
5188 found = 0;
5189 expr = triple_rhs(state, use, 0);
5190 for(;expr; expr = triple_rhs(state, use, expr)) {
5191 if (*expr == orig) {
5192 *expr = new;
5193 found = 1;
5194 }
5195 }
5196 if (found) {
5197 unuse_triple(orig, use);
5198 use_triple(new, use);
5199 }
5200 return found;
5201}
5202
5203static int replace_lhs_use(struct compile_state *state,
5204 struct triple *orig, struct triple *new, struct triple *use)
5205{
5206 struct triple **expr;
5207 int found;
5208 found = 0;
5209 expr = triple_lhs(state, use, 0);
5210 for(;expr; expr = triple_lhs(state, use, expr)) {
5211 if (*expr == orig) {
5212 *expr = new;
5213 found = 1;
5214 }
5215 }
5216 if (found) {
5217 unuse_triple(orig, use);
5218 use_triple(new, use);
5219 }
5220 return found;
5221}
5222
5223static void propogate_use(struct compile_state *state,
5224 struct triple *orig, struct triple *new)
5225{
5226 struct triple_set *user, *next;
5227 for(user = orig->use; user; user = next) {
5228 struct triple *use;
5229 int found;
5230 next = user->next;
5231 use = user->member;
5232 found = 0;
5233 found |= replace_rhs_use(state, orig, new, use);
5234 found |= replace_lhs_use(state, orig, new, use);
5235 if (!found) {
5236 internal_error(state, use, "use without use");
5237 }
5238 }
5239 if (orig->use) {
5240 internal_error(state, orig, "used after propogate_use");
5241 }
5242}
5243
5244/*
5245 * Code generators
5246 * ===========================
5247 */
5248
5249static struct triple *mk_add_expr(
5250 struct compile_state *state, struct triple *left, struct triple *right)
5251{
5252 struct type *result_type;
5253 /* Put pointer operands on the left */
5254 if (is_pointer(right)) {
5255 struct triple *tmp;
5256 tmp = left;
5257 left = right;
5258 right = tmp;
5259 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005260 left = read_expr(state, left);
5261 right = read_expr(state, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005262 result_type = ptr_arithmetic_result(state, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005263 if (is_pointer(left)) {
5264 right = triple(state,
5265 is_signed(right->type)? OP_SMUL : OP_UMUL,
5266 &ulong_type,
5267 right,
5268 int_const(state, &ulong_type,
5269 size_of(state, left->type->left)));
5270 }
5271 return triple(state, OP_ADD, result_type, left, right);
5272}
5273
5274static struct triple *mk_sub_expr(
5275 struct compile_state *state, struct triple *left, struct triple *right)
5276{
5277 struct type *result_type;
5278 result_type = ptr_arithmetic_result(state, left, right);
5279 left = read_expr(state, left);
5280 right = read_expr(state, right);
5281 if (is_pointer(left)) {
5282 right = triple(state,
5283 is_signed(right->type)? OP_SMUL : OP_UMUL,
5284 &ulong_type,
5285 right,
5286 int_const(state, &ulong_type,
5287 size_of(state, left->type->left)));
5288 }
5289 return triple(state, OP_SUB, result_type, left, right);
5290}
5291
5292static struct triple *mk_pre_inc_expr(
5293 struct compile_state *state, struct triple *def)
5294{
5295 struct triple *val;
5296 lvalue(state, def);
5297 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
5298 return triple(state, OP_VAL, def->type,
5299 write_expr(state, def, val),
5300 val);
5301}
5302
5303static struct triple *mk_pre_dec_expr(
5304 struct compile_state *state, struct triple *def)
5305{
5306 struct triple *val;
5307 lvalue(state, def);
5308 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
5309 return triple(state, OP_VAL, def->type,
5310 write_expr(state, def, val),
5311 val);
5312}
5313
5314static struct triple *mk_post_inc_expr(
5315 struct compile_state *state, struct triple *def)
5316{
5317 struct triple *val;
5318 lvalue(state, def);
5319 val = read_expr(state, def);
5320 return triple(state, OP_VAL, def->type,
5321 write_expr(state, def,
5322 mk_add_expr(state, val, int_const(state, &int_type, 1)))
5323 , val);
5324}
5325
5326static struct triple *mk_post_dec_expr(
5327 struct compile_state *state, struct triple *def)
5328{
5329 struct triple *val;
5330 lvalue(state, def);
5331 val = read_expr(state, def);
5332 return triple(state, OP_VAL, def->type,
5333 write_expr(state, def,
5334 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
5335 , val);
5336}
5337
5338static struct triple *mk_subscript_expr(
5339 struct compile_state *state, struct triple *left, struct triple *right)
5340{
5341 left = read_expr(state, left);
5342 right = read_expr(state, right);
5343 if (!is_pointer(left) && !is_pointer(right)) {
5344 error(state, left, "subscripted value is not a pointer");
5345 }
5346 return mk_deref_expr(state, mk_add_expr(state, left, right));
5347}
5348
5349/*
5350 * Compile time evaluation
5351 * ===========================
5352 */
5353static int is_const(struct triple *ins)
5354{
5355 return IS_CONST_OP(ins->op);
5356}
5357
5358static int constants_equal(struct compile_state *state,
5359 struct triple *left, struct triple *right)
5360{
5361 int equal;
5362 if (!is_const(left) || !is_const(right)) {
5363 equal = 0;
5364 }
5365 else if (left->op != right->op) {
5366 equal = 0;
5367 }
5368 else if (!equiv_types(left->type, right->type)) {
5369 equal = 0;
5370 }
5371 else {
5372 equal = 0;
5373 switch(left->op) {
5374 case OP_INTCONST:
5375 if (left->u.cval == right->u.cval) {
5376 equal = 1;
5377 }
5378 break;
5379 case OP_BLOBCONST:
5380 {
5381 size_t lsize, rsize;
5382 lsize = size_of(state, left->type);
5383 rsize = size_of(state, right->type);
5384 if (lsize != rsize) {
5385 break;
5386 }
5387 if (memcmp(left->u.blob, right->u.blob, lsize) == 0) {
5388 equal = 1;
5389 }
5390 break;
5391 }
5392 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005393 if ((MISC(left, 0) == MISC(right, 0)) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00005394 (left->u.cval == right->u.cval)) {
5395 equal = 1;
5396 }
5397 break;
5398 default:
5399 internal_error(state, left, "uknown constant type");
5400 break;
5401 }
5402 }
5403 return equal;
5404}
5405
5406static int is_zero(struct triple *ins)
5407{
5408 return is_const(ins) && (ins->u.cval == 0);
5409}
5410
5411static int is_one(struct triple *ins)
5412{
5413 return is_const(ins) && (ins->u.cval == 1);
5414}
5415
5416static long_t bsr(ulong_t value)
5417{
5418 int i;
5419 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
5420 ulong_t mask;
5421 mask = 1;
5422 mask <<= i;
5423 if (value & mask) {
5424 return i;
5425 }
5426 }
5427 return -1;
5428}
5429
5430static long_t bsf(ulong_t value)
5431{
5432 int i;
5433 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
5434 ulong_t mask;
5435 mask = 1;
5436 mask <<= 1;
5437 if (value & mask) {
5438 return i;
5439 }
5440 }
5441 return -1;
5442}
5443
5444static long_t log2(ulong_t value)
5445{
5446 return bsr(value);
5447}
5448
5449static long_t tlog2(struct triple *ins)
5450{
5451 return log2(ins->u.cval);
5452}
5453
5454static int is_pow2(struct triple *ins)
5455{
5456 ulong_t value, mask;
5457 long_t log;
5458 if (!is_const(ins)) {
5459 return 0;
5460 }
5461 value = ins->u.cval;
5462 log = log2(value);
5463 if (log == -1) {
5464 return 0;
5465 }
5466 mask = 1;
5467 mask <<= log;
5468 return ((value & mask) == value);
5469}
5470
5471static ulong_t read_const(struct compile_state *state,
5472 struct triple *ins, struct triple **expr)
5473{
5474 struct triple *rhs;
5475 rhs = *expr;
5476 switch(rhs->type->type &TYPE_MASK) {
5477 case TYPE_CHAR:
5478 case TYPE_SHORT:
5479 case TYPE_INT:
5480 case TYPE_LONG:
5481 case TYPE_UCHAR:
5482 case TYPE_USHORT:
5483 case TYPE_UINT:
5484 case TYPE_ULONG:
5485 case TYPE_POINTER:
5486 break;
5487 default:
5488 internal_error(state, rhs, "bad type to read_const\n");
5489 break;
5490 }
5491 return rhs->u.cval;
5492}
5493
5494static long_t read_sconst(struct triple *ins, struct triple **expr)
5495{
5496 struct triple *rhs;
5497 rhs = *expr;
5498 return (long_t)(rhs->u.cval);
5499}
5500
5501static void unuse_rhs(struct compile_state *state, struct triple *ins)
5502{
5503 struct triple **expr;
5504 expr = triple_rhs(state, ins, 0);
5505 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00005506 if (*expr) {
5507 unuse_triple(*expr, ins);
5508 *expr = 0;
5509 }
5510 }
5511}
5512
5513static void unuse_lhs(struct compile_state *state, struct triple *ins)
5514{
5515 struct triple **expr;
5516 expr = triple_lhs(state, ins, 0);
5517 for(;expr;expr = triple_lhs(state, ins, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005518 unuse_triple(*expr, ins);
5519 *expr = 0;
5520 }
5521}
Eric Biederman0babc1c2003-05-09 02:39:00 +00005522
Eric Biedermanb138ac82003-04-22 18:44:01 +00005523static void check_lhs(struct compile_state *state, struct triple *ins)
5524{
5525 struct triple **expr;
5526 expr = triple_lhs(state, ins, 0);
5527 for(;expr;expr = triple_lhs(state, ins, expr)) {
5528 internal_error(state, ins, "unexpected lhs");
5529 }
5530
5531}
5532static void check_targ(struct compile_state *state, struct triple *ins)
5533{
5534 struct triple **expr;
5535 expr = triple_targ(state, ins, 0);
5536 for(;expr;expr = triple_targ(state, ins, expr)) {
5537 internal_error(state, ins, "unexpected targ");
5538 }
5539}
5540
5541static void wipe_ins(struct compile_state *state, struct triple *ins)
5542{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005543 /* Becareful which instructions you replace the wiped
5544 * instruction with, as there are not enough slots
5545 * in all instructions to hold all others.
5546 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005547 check_targ(state, ins);
5548 unuse_rhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005549 unuse_lhs(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005550}
5551
5552static void mkcopy(struct compile_state *state,
5553 struct triple *ins, struct triple *rhs)
5554{
5555 wipe_ins(state, ins);
5556 ins->op = OP_COPY;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005557 ins->sizes = TRIPLE_SIZES(0, 1, 0, 0);
5558 RHS(ins, 0) = rhs;
5559 use_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005560}
5561
5562static void mkconst(struct compile_state *state,
5563 struct triple *ins, ulong_t value)
5564{
5565 if (!is_integral(ins) && !is_pointer(ins)) {
5566 internal_error(state, ins, "unknown type to make constant\n");
5567 }
5568 wipe_ins(state, ins);
5569 ins->op = OP_INTCONST;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005570 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005571 ins->u.cval = value;
5572}
5573
5574static void mkaddr_const(struct compile_state *state,
5575 struct triple *ins, struct triple *sdecl, ulong_t value)
5576{
5577 wipe_ins(state, ins);
5578 ins->op = OP_ADDRCONST;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005579 ins->sizes = TRIPLE_SIZES(0, 0, 1, 0);
5580 MISC(ins, 0) = sdecl;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005581 ins->u.cval = value;
5582 use_triple(sdecl, ins);
5583}
5584
Eric Biederman0babc1c2003-05-09 02:39:00 +00005585/* Transform multicomponent variables into simple register variables */
5586static void flatten_structures(struct compile_state *state)
5587{
5588 struct triple *ins, *first;
5589 first = RHS(state->main_function, 0);
5590 ins = first;
5591 /* Pass one expand structure values into valvecs.
5592 */
5593 ins = first;
5594 do {
5595 struct triple *next;
5596 next = ins->next;
5597 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5598 if (ins->op == OP_VAL_VEC) {
5599 /* Do nothing */
5600 }
5601 else if ((ins->op == OP_LOAD) || (ins->op == OP_READ)) {
5602 struct triple *def, **vector;
5603 struct type *tptr;
5604 int op;
5605 ulong_t i;
5606
5607 op = ins->op;
5608 def = RHS(ins, 0);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005609 get_occurance(ins->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005610 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005611 ins->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005612
5613 vector = &RHS(next, 0);
5614 tptr = next->type->left;
5615 for(i = 0; i < next->type->elements; i++) {
5616 struct triple *sfield;
5617 struct type *mtype;
5618 mtype = tptr;
5619 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5620 mtype = mtype->left;
5621 }
5622 sfield = deref_field(state, def, mtype->field_ident);
5623
5624 vector[i] = triple(
5625 state, op, mtype, sfield, 0);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005626 put_occurance(vector[i]->occurance);
5627 get_occurance(next->occurance);
5628 vector[i]->occurance = next->occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005629 tptr = tptr->right;
5630 }
5631 propogate_use(state, ins, next);
5632 flatten(state, ins, next);
5633 free_triple(state, ins);
5634 }
5635 else if ((ins->op == OP_STORE) || (ins->op == OP_WRITE)) {
5636 struct triple *src, *dst, **vector;
5637 struct type *tptr;
5638 int op;
5639 ulong_t i;
5640
5641 op = ins->op;
5642 src = RHS(ins, 0);
5643 dst = LHS(ins, 0);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005644 get_occurance(ins->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005645 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005646 ins->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005647
5648 vector = &RHS(next, 0);
5649 tptr = next->type->left;
5650 for(i = 0; i < ins->type->elements; i++) {
5651 struct triple *dfield, *sfield;
5652 struct type *mtype;
5653 mtype = tptr;
5654 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5655 mtype = mtype->left;
5656 }
5657 sfield = deref_field(state, src, mtype->field_ident);
5658 dfield = deref_field(state, dst, mtype->field_ident);
5659 vector[i] = triple(
5660 state, op, mtype, dfield, sfield);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005661 put_occurance(vector[i]->occurance);
5662 get_occurance(next->occurance);
5663 vector[i]->occurance = next->occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005664 tptr = tptr->right;
5665 }
5666 propogate_use(state, ins, next);
5667 flatten(state, ins, next);
5668 free_triple(state, ins);
5669 }
5670 }
5671 ins = next;
5672 } while(ins != first);
5673 /* Pass two flatten the valvecs.
5674 */
5675 ins = first;
5676 do {
5677 struct triple *next;
5678 next = ins->next;
5679 if (ins->op == OP_VAL_VEC) {
5680 release_triple(state, ins);
5681 }
5682 ins = next;
5683 } while(ins != first);
5684 /* Pass three verify the state and set ->id to 0.
5685 */
5686 ins = first;
5687 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005688 ins->id &= ~TRIPLE_FLAG_FLATTENED;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005689 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5690 internal_error(state, 0, "STRUCT_TYPE remains?");
5691 }
5692 if (ins->op == OP_DOT) {
5693 internal_error(state, 0, "OP_DOT remains?");
5694 }
5695 if (ins->op == OP_VAL_VEC) {
5696 internal_error(state, 0, "OP_VAL_VEC remains?");
5697 }
5698 ins = ins->next;
5699 } while(ins != first);
5700}
5701
Eric Biedermanb138ac82003-04-22 18:44:01 +00005702/* For those operations that cannot be simplified */
5703static void simplify_noop(struct compile_state *state, struct triple *ins)
5704{
5705 return;
5706}
5707
5708static void simplify_smul(struct compile_state *state, struct triple *ins)
5709{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005710 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005711 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005712 tmp = RHS(ins, 0);
5713 RHS(ins, 0) = RHS(ins, 1);
5714 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005715 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005716 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005717 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005718 left = read_sconst(ins, &RHS(ins, 0));
5719 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005720 mkconst(state, ins, left * right);
5721 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005722 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005723 mkconst(state, ins, 0);
5724 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005725 else if (is_one(RHS(ins, 1))) {
5726 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005727 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005728 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005729 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005730 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005731 ins->op = OP_SL;
5732 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005733 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005734 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005735 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005736 }
5737}
5738
5739static void simplify_umul(struct compile_state *state, struct triple *ins)
5740{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005741 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005742 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005743 tmp = RHS(ins, 0);
5744 RHS(ins, 0) = RHS(ins, 1);
5745 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005746 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005747 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005748 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005749 left = read_const(state, ins, &RHS(ins, 0));
5750 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005751 mkconst(state, ins, left * right);
5752 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005753 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005754 mkconst(state, ins, 0);
5755 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005756 else if (is_one(RHS(ins, 1))) {
5757 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005758 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005759 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005760 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005761 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005762 ins->op = OP_SL;
5763 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005764 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005765 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005766 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005767 }
5768}
5769
5770static void simplify_sdiv(struct compile_state *state, struct triple *ins)
5771{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005772 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005773 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005774 left = read_sconst(ins, &RHS(ins, 0));
5775 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005776 mkconst(state, ins, left / right);
5777 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005778 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005779 mkconst(state, ins, 0);
5780 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005781 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005782 error(state, ins, "division by zero");
5783 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005784 else if (is_one(RHS(ins, 1))) {
5785 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005786 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005787 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005788 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005789 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005790 ins->op = OP_SSR;
5791 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005792 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005793 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005794 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005795 }
5796}
5797
5798static void simplify_udiv(struct compile_state *state, struct triple *ins)
5799{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005800 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005801 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005802 left = read_const(state, ins, &RHS(ins, 0));
5803 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005804 mkconst(state, ins, left / right);
5805 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005806 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005807 mkconst(state, ins, 0);
5808 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005809 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005810 error(state, ins, "division by zero");
5811 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005812 else if (is_one(RHS(ins, 1))) {
5813 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005814 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005815 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005816 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005817 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005818 ins->op = OP_USR;
5819 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005820 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005821 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005822 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005823 }
5824}
5825
5826static void simplify_smod(struct compile_state *state, struct triple *ins)
5827{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005828 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005829 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005830 left = read_const(state, ins, &RHS(ins, 0));
5831 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005832 mkconst(state, ins, left % right);
5833 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005834 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005835 mkconst(state, ins, 0);
5836 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005837 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005838 error(state, ins, "division by zero");
5839 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005840 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005841 mkconst(state, ins, 0);
5842 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005843 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005844 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005845 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005846 ins->op = OP_AND;
5847 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005848 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005849 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005850 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005851 }
5852}
5853static void simplify_umod(struct compile_state *state, struct triple *ins)
5854{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005855 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005856 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005857 left = read_const(state, ins, &RHS(ins, 0));
5858 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005859 mkconst(state, ins, left % right);
5860 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005861 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005862 mkconst(state, ins, 0);
5863 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005864 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005865 error(state, ins, "division by zero");
5866 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005867 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005868 mkconst(state, ins, 0);
5869 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005870 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005871 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005872 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005873 ins->op = OP_AND;
5874 insert_triple(state, ins, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005875 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005876 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005877 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005878 }
5879}
5880
5881static void simplify_add(struct compile_state *state, struct triple *ins)
5882{
5883 /* start with the pointer on the left */
Eric Biederman0babc1c2003-05-09 02:39:00 +00005884 if (is_pointer(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005885 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005886 tmp = RHS(ins, 0);
5887 RHS(ins, 0) = RHS(ins, 1);
5888 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005889 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005890 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5891 if (!is_pointer(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005892 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005893 left = read_const(state, ins, &RHS(ins, 0));
5894 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005895 mkconst(state, ins, left + right);
5896 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005897 else /* op == OP_ADDRCONST */ {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005898 struct triple *sdecl;
5899 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005900 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005901 left = RHS(ins, 0)->u.cval;
5902 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005903 mkaddr_const(state, ins, sdecl, left + right);
5904 }
5905 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005906 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005907 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005908 tmp = RHS(ins, 1);
5909 RHS(ins, 1) = RHS(ins, 0);
5910 RHS(ins, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005911 }
5912}
5913
5914static void simplify_sub(struct compile_state *state, struct triple *ins)
5915{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005916 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5917 if (!is_pointer(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005918 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005919 left = read_const(state, ins, &RHS(ins, 0));
5920 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005921 mkconst(state, ins, left - right);
5922 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005923 else /* op == OP_ADDRCONST */ {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005924 struct triple *sdecl;
5925 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005926 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00005927 left = RHS(ins, 0)->u.cval;
5928 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005929 mkaddr_const(state, ins, sdecl, left - right);
5930 }
5931 }
5932}
5933
5934static void simplify_sl(struct compile_state *state, struct triple *ins)
5935{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005936 if (is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005937 ulong_t right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005938 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005939 if (right >= (size_of(state, ins->type)*8)) {
5940 warning(state, ins, "left shift count >= width of type");
5941 }
5942 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005943 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005944 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005945 left = read_const(state, ins, &RHS(ins, 0));
5946 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005947 mkconst(state, ins, left << right);
5948 }
5949}
5950
5951static void simplify_usr(struct compile_state *state, struct triple *ins)
5952{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005953 if (is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005954 ulong_t right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005955 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005956 if (right >= (size_of(state, ins->type)*8)) {
5957 warning(state, ins, "right shift count >= width of type");
5958 }
5959 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005960 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005961 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005962 left = read_const(state, ins, &RHS(ins, 0));
5963 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005964 mkconst(state, ins, left >> right);
5965 }
5966}
5967
5968static void simplify_ssr(struct compile_state *state, struct triple *ins)
5969{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005970 if (is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005971 ulong_t right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005972 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005973 if (right >= (size_of(state, ins->type)*8)) {
5974 warning(state, ins, "right shift count >= width of type");
5975 }
5976 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00005977 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005978 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005979 left = read_sconst(ins, &RHS(ins, 0));
5980 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005981 mkconst(state, ins, left >> right);
5982 }
5983}
5984
5985static void simplify_and(struct compile_state *state, struct triple *ins)
5986{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005987 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005988 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005989 left = read_const(state, ins, &RHS(ins, 0));
5990 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005991 mkconst(state, ins, left & right);
5992 }
5993}
5994
5995static void simplify_or(struct compile_state *state, struct triple *ins)
5996{
Eric Biederman0babc1c2003-05-09 02:39:00 +00005997 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005998 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005999 left = read_const(state, ins, &RHS(ins, 0));
6000 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006001 mkconst(state, ins, left | right);
6002 }
6003}
6004
6005static void simplify_xor(struct compile_state *state, struct triple *ins)
6006{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006007 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006008 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006009 left = read_const(state, ins, &RHS(ins, 0));
6010 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006011 mkconst(state, ins, left ^ right);
6012 }
6013}
6014
6015static void simplify_pos(struct compile_state *state, struct triple *ins)
6016{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006017 if (is_const(RHS(ins, 0))) {
6018 mkconst(state, ins, RHS(ins, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006019 }
6020 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006021 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006022 }
6023}
6024
6025static void simplify_neg(struct compile_state *state, struct triple *ins)
6026{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006027 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006028 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006029 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006030 mkconst(state, ins, -left);
6031 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006032 else if (RHS(ins, 0)->op == OP_NEG) {
6033 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006034 }
6035}
6036
6037static void simplify_invert(struct compile_state *state, struct triple *ins)
6038{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006039 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006040 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006041 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006042 mkconst(state, ins, ~left);
6043 }
6044}
6045
6046static void simplify_eq(struct compile_state *state, struct triple *ins)
6047{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006048 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006049 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006050 left = read_const(state, ins, &RHS(ins, 0));
6051 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006052 mkconst(state, ins, left == right);
6053 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006054 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006055 mkconst(state, ins, 1);
6056 }
6057}
6058
6059static void simplify_noteq(struct compile_state *state, struct triple *ins)
6060{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006061 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006062 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006063 left = read_const(state, ins, &RHS(ins, 0));
6064 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006065 mkconst(state, ins, left != right);
6066 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006067 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006068 mkconst(state, ins, 0);
6069 }
6070}
6071
6072static void simplify_sless(struct compile_state *state, struct triple *ins)
6073{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006074 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006075 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006076 left = read_sconst(ins, &RHS(ins, 0));
6077 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006078 mkconst(state, ins, left < right);
6079 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006080 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006081 mkconst(state, ins, 0);
6082 }
6083}
6084
6085static void simplify_uless(struct compile_state *state, struct triple *ins)
6086{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006087 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006088 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006089 left = read_const(state, ins, &RHS(ins, 0));
6090 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006091 mkconst(state, ins, left < right);
6092 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006093 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006094 mkconst(state, ins, 1);
6095 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006096 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006097 mkconst(state, ins, 0);
6098 }
6099}
6100
6101static void simplify_smore(struct compile_state *state, struct triple *ins)
6102{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006103 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006104 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006105 left = read_sconst(ins, &RHS(ins, 0));
6106 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006107 mkconst(state, ins, left > right);
6108 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006109 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006110 mkconst(state, ins, 0);
6111 }
6112}
6113
6114static void simplify_umore(struct compile_state *state, struct triple *ins)
6115{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006116 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006117 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006118 left = read_const(state, ins, &RHS(ins, 0));
6119 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006120 mkconst(state, ins, left > right);
6121 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006122 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006123 mkconst(state, ins, 1);
6124 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006125 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006126 mkconst(state, ins, 0);
6127 }
6128}
6129
6130
6131static void simplify_slesseq(struct compile_state *state, struct triple *ins)
6132{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006133 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006134 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006135 left = read_sconst(ins, &RHS(ins, 0));
6136 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006137 mkconst(state, ins, left <= right);
6138 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006139 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006140 mkconst(state, ins, 1);
6141 }
6142}
6143
6144static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
6145{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006146 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006147 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006148 left = read_const(state, ins, &RHS(ins, 0));
6149 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006150 mkconst(state, ins, left <= right);
6151 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006152 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006153 mkconst(state, ins, 1);
6154 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006155 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006156 mkconst(state, ins, 1);
6157 }
6158}
6159
6160static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
6161{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006162 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006163 long_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006164 left = read_sconst(ins, &RHS(ins, 0));
6165 right = read_sconst(ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006166 mkconst(state, ins, left >= right);
6167 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006168 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006169 mkconst(state, ins, 1);
6170 }
6171}
6172
6173static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
6174{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006175 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006176 ulong_t left, right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006177 left = read_const(state, ins, &RHS(ins, 0));
6178 right = read_const(state, ins, &RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006179 mkconst(state, ins, left >= right);
6180 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006181 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006182 mkconst(state, ins, 1);
6183 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006184 else if (RHS(ins, 0) == RHS(ins, 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006185 mkconst(state, ins, 1);
6186 }
6187}
6188
6189static void simplify_lfalse(struct compile_state *state, struct triple *ins)
6190{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006191 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006192 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006193 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006194 mkconst(state, ins, left == 0);
6195 }
6196 /* Otherwise if I am the only user... */
Eric Biederman0babc1c2003-05-09 02:39:00 +00006197 else if ((RHS(ins, 0)->use->member == ins) && (RHS(ins, 0)->use->next == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006198 int need_copy = 1;
6199 /* Invert a boolean operation */
Eric Biederman0babc1c2003-05-09 02:39:00 +00006200 switch(RHS(ins, 0)->op) {
6201 case OP_LTRUE: RHS(ins, 0)->op = OP_LFALSE; break;
6202 case OP_LFALSE: RHS(ins, 0)->op = OP_LTRUE; break;
6203 case OP_EQ: RHS(ins, 0)->op = OP_NOTEQ; break;
6204 case OP_NOTEQ: RHS(ins, 0)->op = OP_EQ; break;
6205 case OP_SLESS: RHS(ins, 0)->op = OP_SMOREEQ; break;
6206 case OP_ULESS: RHS(ins, 0)->op = OP_UMOREEQ; break;
6207 case OP_SMORE: RHS(ins, 0)->op = OP_SLESSEQ; break;
6208 case OP_UMORE: RHS(ins, 0)->op = OP_ULESSEQ; break;
6209 case OP_SLESSEQ: RHS(ins, 0)->op = OP_SMORE; break;
6210 case OP_ULESSEQ: RHS(ins, 0)->op = OP_UMORE; break;
6211 case OP_SMOREEQ: RHS(ins, 0)->op = OP_SLESS; break;
6212 case OP_UMOREEQ: RHS(ins, 0)->op = OP_ULESS; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006213 default:
6214 need_copy = 0;
6215 break;
6216 }
6217 if (need_copy) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006218 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006219 }
6220 }
6221}
6222
6223static void simplify_ltrue (struct compile_state *state, struct triple *ins)
6224{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006225 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006226 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006227 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006228 mkconst(state, ins, left != 0);
6229 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006230 else switch(RHS(ins, 0)->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006231 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
6232 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
6233 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
Eric Biederman0babc1c2003-05-09 02:39:00 +00006234 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006235 }
6236
6237}
6238
6239static void simplify_copy(struct compile_state *state, struct triple *ins)
6240{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006241 if (is_const(RHS(ins, 0))) {
6242 switch(RHS(ins, 0)->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006243 case OP_INTCONST:
6244 {
6245 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006246 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006247 mkconst(state, ins, left);
6248 break;
6249 }
6250 case OP_ADDRCONST:
6251 {
6252 struct triple *sdecl;
6253 ulong_t offset;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006254 sdecl = MISC(RHS(ins, 0), 0);
6255 offset = RHS(ins, 0)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006256 mkaddr_const(state, ins, sdecl, offset);
6257 break;
6258 }
6259 default:
6260 internal_error(state, ins, "uknown constant");
6261 break;
6262 }
6263 }
6264}
6265
Eric Biedermanb138ac82003-04-22 18:44:01 +00006266static void simplify_branch(struct compile_state *state, struct triple *ins)
6267{
6268 struct block *block;
6269 if (ins->op != OP_BRANCH) {
6270 internal_error(state, ins, "not branch");
6271 }
6272 if (ins->use != 0) {
6273 internal_error(state, ins, "branch use");
6274 }
6275#warning "FIXME implement simplify branch."
6276 /* The challenge here with simplify branch is that I need to
6277 * make modifications to the control flow graph as well
6278 * as to the branch instruction itself.
6279 */
6280 block = ins->u.block;
6281
Eric Biederman0babc1c2003-05-09 02:39:00 +00006282 if (TRIPLE_RHS(ins->sizes) && is_const(RHS(ins, 0))) {
6283 struct triple *targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006284 ulong_t value;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006285 value = read_const(state, ins, &RHS(ins, 0));
6286 unuse_triple(RHS(ins, 0), ins);
6287 targ = TARG(ins, 0);
6288 ins->sizes = TRIPLE_SIZES(0, 0, 0, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006289 if (value) {
6290 unuse_triple(ins->next, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006291 TARG(ins, 0) = targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006292 }
6293 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006294 unuse_triple(targ, ins);
6295 TARG(ins, 0) = ins->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006296 }
6297#warning "FIXME handle the case of making a branch unconditional"
6298 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006299 if (TARG(ins, 0) == ins->next) {
6300 unuse_triple(ins->next, ins);
6301 if (TRIPLE_RHS(ins->sizes)) {
6302 unuse_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006303 unuse_triple(ins->next, ins);
6304 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006305 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
6306 ins->op = OP_NOOP;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006307 if (ins->use) {
6308 internal_error(state, ins, "noop use != 0");
6309 }
6310#warning "FIXME handle the case of killing a branch"
6311 }
6312}
6313
6314static void simplify_phi(struct compile_state *state, struct triple *ins)
6315{
6316 struct triple **expr;
6317 ulong_t value;
6318 expr = triple_rhs(state, ins, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006319 if (!*expr || !is_const(*expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006320 return;
6321 }
6322 value = read_const(state, ins, expr);
6323 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006324 if (!*expr || !is_const(*expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006325 return;
6326 }
6327 if (value != read_const(state, ins, expr)) {
6328 return;
6329 }
6330 }
6331 mkconst(state, ins, value);
6332}
6333
6334
6335static void simplify_bsf(struct compile_state *state, struct triple *ins)
6336{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006337 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006338 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006339 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006340 mkconst(state, ins, bsf(left));
6341 }
6342}
6343
6344static void simplify_bsr(struct compile_state *state, struct triple *ins)
6345{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006346 if (is_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006347 ulong_t left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006348 left = read_const(state, ins, &RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006349 mkconst(state, ins, bsr(left));
6350 }
6351}
6352
6353
6354typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
6355static const simplify_t table_simplify[] = {
6356#if 0
6357#define simplify_smul simplify_noop
6358#define simplify_umul simplify_noop
6359#define simplify_sdiv simplify_noop
6360#define simplify_udiv simplify_noop
6361#define simplify_smod simplify_noop
6362#define simplify_umod simplify_noop
6363#endif
6364#if 0
6365#define simplify_add simplify_noop
6366#define simplify_sub simplify_noop
6367#endif
6368#if 0
6369#define simplify_sl simplify_noop
6370#define simplify_usr simplify_noop
6371#define simplify_ssr simplify_noop
6372#endif
6373#if 0
6374#define simplify_and simplify_noop
6375#define simplify_xor simplify_noop
6376#define simplify_or simplify_noop
6377#endif
6378#if 0
6379#define simplify_pos simplify_noop
6380#define simplify_neg simplify_noop
6381#define simplify_invert simplify_noop
6382#endif
6383
6384#if 0
6385#define simplify_eq simplify_noop
6386#define simplify_noteq simplify_noop
6387#endif
6388#if 0
6389#define simplify_sless simplify_noop
6390#define simplify_uless simplify_noop
6391#define simplify_smore simplify_noop
6392#define simplify_umore simplify_noop
6393#endif
6394#if 0
6395#define simplify_slesseq simplify_noop
6396#define simplify_ulesseq simplify_noop
6397#define simplify_smoreeq simplify_noop
6398#define simplify_umoreeq simplify_noop
6399#endif
6400#if 0
6401#define simplify_lfalse simplify_noop
6402#endif
6403#if 0
6404#define simplify_ltrue simplify_noop
6405#endif
6406
6407#if 0
6408#define simplify_copy simplify_noop
6409#endif
6410
6411#if 0
Eric Biedermanb138ac82003-04-22 18:44:01 +00006412#define simplify_branch simplify_noop
6413#endif
6414
6415#if 0
6416#define simplify_phi simplify_noop
6417#endif
6418
6419#if 0
6420#define simplify_bsf simplify_noop
6421#define simplify_bsr simplify_noop
6422#endif
6423
6424[OP_SMUL ] = simplify_smul,
6425[OP_UMUL ] = simplify_umul,
6426[OP_SDIV ] = simplify_sdiv,
6427[OP_UDIV ] = simplify_udiv,
6428[OP_SMOD ] = simplify_smod,
6429[OP_UMOD ] = simplify_umod,
6430[OP_ADD ] = simplify_add,
6431[OP_SUB ] = simplify_sub,
6432[OP_SL ] = simplify_sl,
6433[OP_USR ] = simplify_usr,
6434[OP_SSR ] = simplify_ssr,
6435[OP_AND ] = simplify_and,
6436[OP_XOR ] = simplify_xor,
6437[OP_OR ] = simplify_or,
6438[OP_POS ] = simplify_pos,
6439[OP_NEG ] = simplify_neg,
6440[OP_INVERT ] = simplify_invert,
6441
6442[OP_EQ ] = simplify_eq,
6443[OP_NOTEQ ] = simplify_noteq,
6444[OP_SLESS ] = simplify_sless,
6445[OP_ULESS ] = simplify_uless,
6446[OP_SMORE ] = simplify_smore,
6447[OP_UMORE ] = simplify_umore,
6448[OP_SLESSEQ ] = simplify_slesseq,
6449[OP_ULESSEQ ] = simplify_ulesseq,
6450[OP_SMOREEQ ] = simplify_smoreeq,
6451[OP_UMOREEQ ] = simplify_umoreeq,
6452[OP_LFALSE ] = simplify_lfalse,
6453[OP_LTRUE ] = simplify_ltrue,
6454
6455[OP_LOAD ] = simplify_noop,
6456[OP_STORE ] = simplify_noop,
6457
6458[OP_NOOP ] = simplify_noop,
6459
6460[OP_INTCONST ] = simplify_noop,
6461[OP_BLOBCONST ] = simplify_noop,
6462[OP_ADDRCONST ] = simplify_noop,
6463
6464[OP_WRITE ] = simplify_noop,
6465[OP_READ ] = simplify_noop,
6466[OP_COPY ] = simplify_copy,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006467[OP_PIECE ] = simplify_noop,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006468[OP_ASM ] = simplify_noop,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006469
6470[OP_DOT ] = simplify_noop,
6471[OP_VAL_VEC ] = simplify_noop,
Eric Biedermanb138ac82003-04-22 18:44:01 +00006472
6473[OP_LIST ] = simplify_noop,
6474[OP_BRANCH ] = simplify_branch,
6475[OP_LABEL ] = simplify_noop,
6476[OP_ADECL ] = simplify_noop,
6477[OP_SDECL ] = simplify_noop,
6478[OP_PHI ] = simplify_phi,
6479
6480[OP_INB ] = simplify_noop,
6481[OP_INW ] = simplify_noop,
6482[OP_INL ] = simplify_noop,
6483[OP_OUTB ] = simplify_noop,
6484[OP_OUTW ] = simplify_noop,
6485[OP_OUTL ] = simplify_noop,
6486[OP_BSF ] = simplify_bsf,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006487[OP_BSR ] = simplify_bsr,
6488[OP_RDMSR ] = simplify_noop,
6489[OP_WRMSR ] = simplify_noop,
6490[OP_HLT ] = simplify_noop,
Eric Biedermanb138ac82003-04-22 18:44:01 +00006491};
6492
6493static void simplify(struct compile_state *state, struct triple *ins)
6494{
6495 int op;
6496 simplify_t do_simplify;
6497 do {
6498 op = ins->op;
6499 do_simplify = 0;
6500 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
6501 do_simplify = 0;
6502 }
6503 else {
6504 do_simplify = table_simplify[op];
6505 }
6506 if (!do_simplify) {
6507 internal_error(state, ins, "cannot simplify op: %d %s\n",
6508 op, tops(op));
6509 return;
6510 }
6511 do_simplify(state, ins);
6512 } while(ins->op != op);
6513}
6514
6515static void simplify_all(struct compile_state *state)
6516{
6517 struct triple *ins, *first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006518 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006519 ins = first;
6520 do {
6521 simplify(state, ins);
6522 ins = ins->next;
6523 } while(ins != first);
6524}
6525
6526/*
6527 * Builtins....
6528 * ============================
6529 */
6530
Eric Biederman0babc1c2003-05-09 02:39:00 +00006531static void register_builtin_function(struct compile_state *state,
6532 const char *name, int op, struct type *rtype, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00006533{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006534 struct type *ftype, *atype, *param, **next;
6535 struct triple *def, *arg, *result, *work, *last, *first;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006536 struct hash_entry *ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006537 struct file_state file;
6538 int parameters;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006539 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006540 va_list args;
6541 int i;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006542
6543 /* Dummy file state to get debug handling right */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006544 memset(&file, 0, sizeof(file));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00006545 file.basename = "<built-in>";
Eric Biedermanb138ac82003-04-22 18:44:01 +00006546 file.line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00006547 file.report_line = 1;
6548 file.report_name = file.basename;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006549 file.prev = state->file;
6550 state->file = &file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00006551 state->function = name;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006552
6553 /* Find the Parameter count */
6554 valid_op(state, op);
6555 parameters = table_ops[op].rhs;
6556 if (parameters < 0 ) {
6557 internal_error(state, 0, "Invalid builtin parameter count");
6558 }
6559
6560 /* Find the function type */
6561 ftype = new_type(TYPE_FUNCTION, rtype, 0);
6562 next = &ftype->right;
6563 va_start(args, rtype);
6564 for(i = 0; i < parameters; i++) {
6565 atype = va_arg(args, struct type *);
6566 if (!*next) {
6567 *next = atype;
6568 } else {
6569 *next = new_type(TYPE_PRODUCT, *next, atype);
6570 next = &((*next)->right);
6571 }
6572 }
6573 if (!*next) {
6574 *next = &void_type;
6575 }
6576 va_end(args);
6577
Eric Biedermanb138ac82003-04-22 18:44:01 +00006578 /* Generate the needed triples */
6579 def = triple(state, OP_LIST, ftype, 0, 0);
6580 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006581 RHS(def, 0) = first;
6582
6583 /* Now string them together */
6584 param = ftype->right;
6585 for(i = 0; i < parameters; i++) {
6586 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6587 atype = param->left;
6588 } else {
6589 atype = param;
6590 }
6591 arg = flatten(state, first, variable(state, atype));
6592 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006593 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006594 result = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006595 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006596 result = flatten(state, first, variable(state, rtype));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006597 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006598 MISC(def, 0) = result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006599 work = new_triple(state, op, rtype, -1, parameters);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006600 for(i = 0, arg = first->next; i < parameters; i++, arg = arg->next) {
6601 RHS(work, i) = read_expr(state, arg);
6602 }
6603 if (result && ((rtype->type & TYPE_MASK) == TYPE_STRUCT)) {
6604 struct triple *val;
6605 /* Populate the LHS with the target registers */
6606 work = flatten(state, first, work);
6607 work->type = &void_type;
6608 param = rtype->left;
6609 if (rtype->elements != TRIPLE_LHS(work->sizes)) {
6610 internal_error(state, 0, "Invalid result type");
6611 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006612 val = new_triple(state, OP_VAL_VEC, rtype, -1, -1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006613 for(i = 0; i < rtype->elements; i++) {
6614 struct triple *piece;
6615 atype = param;
6616 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6617 atype = param->left;
6618 }
6619 if (!TYPE_ARITHMETIC(atype->type) &&
6620 !TYPE_PTR(atype->type)) {
6621 internal_error(state, 0, "Invalid lhs type");
6622 }
6623 piece = triple(state, OP_PIECE, atype, work, 0);
6624 piece->u.cval = i;
6625 LHS(work, i) = piece;
6626 RHS(val, i) = piece;
6627 }
6628 work = val;
6629 }
6630 if (result) {
6631 work = write_expr(state, result, work);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006632 }
6633 work = flatten(state, first, work);
6634 last = flatten(state, first, label(state));
6635 name_len = strlen(name);
6636 ident = lookup(state, name, name_len);
6637 symbol(state, ident, &ident->sym_ident, def, ftype);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006638
Eric Biedermanb138ac82003-04-22 18:44:01 +00006639 state->file = file.prev;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00006640 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006641#if 0
6642 fprintf(stdout, "\n");
6643 loc(stdout, state, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006644 fprintf(stdout, "\n__________ builtin_function _________\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006645 print_triple(state, def);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006646 fprintf(stdout, "__________ builtin_function _________ done\n\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006647#endif
6648}
6649
Eric Biederman0babc1c2003-05-09 02:39:00 +00006650static struct type *partial_struct(struct compile_state *state,
6651 const char *field_name, struct type *type, struct type *rest)
Eric Biedermanb138ac82003-04-22 18:44:01 +00006652{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006653 struct hash_entry *field_ident;
6654 struct type *result;
6655 int field_name_len;
6656
6657 field_name_len = strlen(field_name);
6658 field_ident = lookup(state, field_name, field_name_len);
6659
6660 result = clone_type(0, type);
6661 result->field_ident = field_ident;
6662
6663 if (rest) {
6664 result = new_type(TYPE_PRODUCT, result, rest);
6665 }
6666 return result;
6667}
6668
6669static struct type *register_builtin_type(struct compile_state *state,
6670 const char *name, struct type *type)
6671{
Eric Biedermanb138ac82003-04-22 18:44:01 +00006672 struct hash_entry *ident;
6673 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006674
Eric Biedermanb138ac82003-04-22 18:44:01 +00006675 name_len = strlen(name);
6676 ident = lookup(state, name, name_len);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006677
6678 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6679 ulong_t elements = 0;
6680 struct type *field;
6681 type = new_type(TYPE_STRUCT, type, 0);
6682 field = type->left;
6683 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
6684 elements++;
6685 field = field->right;
6686 }
6687 elements++;
6688 symbol(state, ident, &ident->sym_struct, 0, type);
6689 type->type_ident = ident;
6690 type->elements = elements;
6691 }
6692 symbol(state, ident, &ident->sym_ident, 0, type);
6693 ident->tok = TOK_TYPE_NAME;
6694 return type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006695}
6696
Eric Biederman0babc1c2003-05-09 02:39:00 +00006697
Eric Biedermanb138ac82003-04-22 18:44:01 +00006698static void register_builtins(struct compile_state *state)
6699{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006700 struct type *msr_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006701
Eric Biederman0babc1c2003-05-09 02:39:00 +00006702 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
6703 &ushort_type);
6704 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
6705 &ushort_type);
6706 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
6707 &ushort_type);
6708
6709 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
6710 &uchar_type, &ushort_type);
6711 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
6712 &ushort_type, &ushort_type);
6713 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
6714 &uint_type, &ushort_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006715
Eric Biederman0babc1c2003-05-09 02:39:00 +00006716 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
6717 &int_type);
6718 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
6719 &int_type);
6720
6721 msr_type = register_builtin_type(state, "__builtin_msr_t",
6722 partial_struct(state, "lo", &ulong_type,
6723 partial_struct(state, "hi", &ulong_type, 0)));
6724
6725 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
6726 &ulong_type);
6727 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
6728 &ulong_type, &ulong_type, &ulong_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006729
Eric Biederman0babc1c2003-05-09 02:39:00 +00006730 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
6731 &void_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006732}
6733
6734static struct type *declarator(
6735 struct compile_state *state, struct type *type,
6736 struct hash_entry **ident, int need_ident);
6737static void decl(struct compile_state *state, struct triple *first);
6738static struct type *specifier_qualifier_list(struct compile_state *state);
6739static int isdecl_specifier(int tok);
6740static struct type *decl_specifiers(struct compile_state *state);
6741static int istype(int tok);
6742static struct triple *expr(struct compile_state *state);
6743static struct triple *assignment_expr(struct compile_state *state);
6744static struct type *type_name(struct compile_state *state);
6745static void statement(struct compile_state *state, struct triple *fist);
6746
6747static struct triple *call_expr(
6748 struct compile_state *state, struct triple *func)
6749{
Eric Biederman0babc1c2003-05-09 02:39:00 +00006750 struct triple *def;
6751 struct type *param, *type;
6752 ulong_t pvals, index;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006753
6754 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
6755 error(state, 0, "Called object is not a function");
6756 }
6757 if (func->op != OP_LIST) {
6758 internal_error(state, 0, "improper function");
6759 }
6760 eat(state, TOK_LPAREN);
6761 /* Find the return type without any specifiers */
6762 type = clone_type(0, func->type->left);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006763 def = new_triple(state, OP_CALL, func->type, -1, -1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006764 def->type = type;
6765
6766 pvals = TRIPLE_RHS(def->sizes);
6767 MISC(def, 0) = func;
6768
6769 param = func->type->right;
6770 for(index = 0; index < pvals; index++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006771 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006772 struct type *arg_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006773 val = read_expr(state, assignment_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006774 arg_type = param;
6775 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6776 arg_type = param->left;
6777 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006778 write_compatible(state, arg_type, val->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006779 RHS(def, index) = val;
6780 if (index != (pvals - 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006781 eat(state, TOK_COMMA);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006782 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006783 }
6784 }
6785 eat(state, TOK_RPAREN);
6786 return def;
6787}
6788
6789
6790static struct triple *character_constant(struct compile_state *state)
6791{
6792 struct triple *def;
6793 struct token *tk;
6794 const signed char *str, *end;
6795 int c;
6796 int str_len;
6797 eat(state, TOK_LIT_CHAR);
6798 tk = &state->token[0];
6799 str = tk->val.str + 1;
6800 str_len = tk->str_len - 2;
6801 if (str_len <= 0) {
6802 error(state, 0, "empty character constant");
6803 }
6804 end = str + str_len;
6805 c = char_value(state, &str, end);
6806 if (str != end) {
6807 error(state, 0, "multibyte character constant not supported");
6808 }
6809 def = int_const(state, &char_type, (ulong_t)((long_t)c));
6810 return def;
6811}
6812
6813static struct triple *string_constant(struct compile_state *state)
6814{
6815 struct triple *def;
6816 struct token *tk;
6817 struct type *type;
6818 const signed char *str, *end;
6819 signed char *buf, *ptr;
6820 int str_len;
6821
6822 buf = 0;
6823 type = new_type(TYPE_ARRAY, &char_type, 0);
6824 type->elements = 0;
6825 /* The while loop handles string concatenation */
6826 do {
6827 eat(state, TOK_LIT_STRING);
6828 tk = &state->token[0];
6829 str = tk->val.str + 1;
6830 str_len = tk->str_len - 2;
Eric Biedermanab2ea6b2003-04-26 03:20:53 +00006831 if (str_len < 0) {
6832 error(state, 0, "negative string constant length");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006833 }
6834 end = str + str_len;
6835 ptr = buf;
6836 buf = xmalloc(type->elements + str_len + 1, "string_constant");
6837 memcpy(buf, ptr, type->elements);
6838 ptr = buf + type->elements;
6839 do {
6840 *ptr++ = char_value(state, &str, end);
6841 } while(str < end);
6842 type->elements = ptr - buf;
6843 } while(peek(state) == TOK_LIT_STRING);
6844 *ptr = '\0';
6845 type->elements += 1;
6846 def = triple(state, OP_BLOBCONST, type, 0, 0);
6847 def->u.blob = buf;
6848 return def;
6849}
6850
6851
6852static struct triple *integer_constant(struct compile_state *state)
6853{
6854 struct triple *def;
6855 unsigned long val;
6856 struct token *tk;
6857 char *end;
6858 int u, l, decimal;
6859 struct type *type;
6860
6861 eat(state, TOK_LIT_INT);
6862 tk = &state->token[0];
6863 errno = 0;
6864 decimal = (tk->val.str[0] != '0');
6865 val = strtoul(tk->val.str, &end, 0);
6866 if ((val == ULONG_MAX) && (errno == ERANGE)) {
6867 error(state, 0, "Integer constant to large");
6868 }
6869 u = l = 0;
6870 if ((*end == 'u') || (*end == 'U')) {
6871 u = 1;
6872 end++;
6873 }
6874 if ((*end == 'l') || (*end == 'L')) {
6875 l = 1;
6876 end++;
6877 }
6878 if ((*end == 'u') || (*end == 'U')) {
6879 u = 1;
6880 end++;
6881 }
6882 if (*end) {
6883 error(state, 0, "Junk at end of integer constant");
6884 }
6885 if (u && l) {
6886 type = &ulong_type;
6887 }
6888 else if (l) {
6889 type = &long_type;
6890 if (!decimal && (val > LONG_MAX)) {
6891 type = &ulong_type;
6892 }
6893 }
6894 else if (u) {
6895 type = &uint_type;
6896 if (val > UINT_MAX) {
6897 type = &ulong_type;
6898 }
6899 }
6900 else {
6901 type = &int_type;
6902 if (!decimal && (val > INT_MAX) && (val <= UINT_MAX)) {
6903 type = &uint_type;
6904 }
6905 else if (!decimal && (val > LONG_MAX)) {
6906 type = &ulong_type;
6907 }
6908 else if (val > INT_MAX) {
6909 type = &long_type;
6910 }
6911 }
6912 def = int_const(state, type, val);
6913 return def;
6914}
6915
6916static struct triple *primary_expr(struct compile_state *state)
6917{
6918 struct triple *def;
6919 int tok;
6920 tok = peek(state);
6921 switch(tok) {
6922 case TOK_IDENT:
6923 {
6924 struct hash_entry *ident;
6925 /* Here ident is either:
6926 * a varable name
6927 * a function name
6928 * an enumeration constant.
6929 */
6930 eat(state, TOK_IDENT);
6931 ident = state->token[0].ident;
6932 if (!ident->sym_ident) {
6933 error(state, 0, "%s undeclared", ident->name);
6934 }
6935 def = ident->sym_ident->def;
6936 break;
6937 }
6938 case TOK_ENUM_CONST:
6939 /* Here ident is an enumeration constant */
6940 eat(state, TOK_ENUM_CONST);
6941 def = 0;
6942 FINISHME();
6943 break;
6944 case TOK_LPAREN:
6945 eat(state, TOK_LPAREN);
6946 def = expr(state);
6947 eat(state, TOK_RPAREN);
6948 break;
6949 case TOK_LIT_INT:
6950 def = integer_constant(state);
6951 break;
6952 case TOK_LIT_FLOAT:
6953 eat(state, TOK_LIT_FLOAT);
6954 error(state, 0, "Floating point constants not supported");
6955 def = 0;
6956 FINISHME();
6957 break;
6958 case TOK_LIT_CHAR:
6959 def = character_constant(state);
6960 break;
6961 case TOK_LIT_STRING:
6962 def = string_constant(state);
6963 break;
6964 default:
6965 def = 0;
6966 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
6967 }
6968 return def;
6969}
6970
6971static struct triple *postfix_expr(struct compile_state *state)
6972{
6973 struct triple *def;
6974 int postfix;
6975 def = primary_expr(state);
6976 do {
6977 struct triple *left;
6978 int tok;
6979 postfix = 1;
6980 left = def;
6981 switch((tok = peek(state))) {
6982 case TOK_LBRACKET:
6983 eat(state, TOK_LBRACKET);
6984 def = mk_subscript_expr(state, left, expr(state));
6985 eat(state, TOK_RBRACKET);
6986 break;
6987 case TOK_LPAREN:
6988 def = call_expr(state, def);
6989 break;
6990 case TOK_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +00006991 {
6992 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006993 eat(state, TOK_DOT);
6994 eat(state, TOK_IDENT);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006995 field = state->token[0].ident;
6996 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006997 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006998 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006999 case TOK_ARROW:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007000 {
7001 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007002 eat(state, TOK_ARROW);
7003 eat(state, TOK_IDENT);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007004 field = state->token[0].ident;
7005 def = mk_deref_expr(state, read_expr(state, def));
7006 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007007 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007008 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007009 case TOK_PLUSPLUS:
7010 eat(state, TOK_PLUSPLUS);
7011 def = mk_post_inc_expr(state, left);
7012 break;
7013 case TOK_MINUSMINUS:
7014 eat(state, TOK_MINUSMINUS);
7015 def = mk_post_dec_expr(state, left);
7016 break;
7017 default:
7018 postfix = 0;
7019 break;
7020 }
7021 } while(postfix);
7022 return def;
7023}
7024
7025static struct triple *cast_expr(struct compile_state *state);
7026
7027static struct triple *unary_expr(struct compile_state *state)
7028{
7029 struct triple *def, *right;
7030 int tok;
7031 switch((tok = peek(state))) {
7032 case TOK_PLUSPLUS:
7033 eat(state, TOK_PLUSPLUS);
7034 def = mk_pre_inc_expr(state, unary_expr(state));
7035 break;
7036 case TOK_MINUSMINUS:
7037 eat(state, TOK_MINUSMINUS);
7038 def = mk_pre_dec_expr(state, unary_expr(state));
7039 break;
7040 case TOK_AND:
7041 eat(state, TOK_AND);
7042 def = mk_addr_expr(state, cast_expr(state), 0);
7043 break;
7044 case TOK_STAR:
7045 eat(state, TOK_STAR);
7046 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
7047 break;
7048 case TOK_PLUS:
7049 eat(state, TOK_PLUS);
7050 right = read_expr(state, cast_expr(state));
7051 arithmetic(state, right);
7052 def = integral_promotion(state, right);
7053 break;
7054 case TOK_MINUS:
7055 eat(state, TOK_MINUS);
7056 right = read_expr(state, cast_expr(state));
7057 arithmetic(state, right);
7058 def = integral_promotion(state, right);
7059 def = triple(state, OP_NEG, def->type, def, 0);
7060 break;
7061 case TOK_TILDE:
7062 eat(state, TOK_TILDE);
7063 right = read_expr(state, cast_expr(state));
7064 integral(state, right);
7065 def = integral_promotion(state, right);
7066 def = triple(state, OP_INVERT, def->type, def, 0);
7067 break;
7068 case TOK_BANG:
7069 eat(state, TOK_BANG);
7070 right = read_expr(state, cast_expr(state));
7071 bool(state, right);
7072 def = lfalse_expr(state, right);
7073 break;
7074 case TOK_SIZEOF:
7075 {
7076 struct type *type;
7077 int tok1, tok2;
7078 eat(state, TOK_SIZEOF);
7079 tok1 = peek(state);
7080 tok2 = peek2(state);
7081 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7082 eat(state, TOK_LPAREN);
7083 type = type_name(state);
7084 eat(state, TOK_RPAREN);
7085 }
7086 else {
7087 struct triple *expr;
7088 expr = unary_expr(state);
7089 type = expr->type;
7090 release_expr(state, expr);
7091 }
7092 def = int_const(state, &ulong_type, size_of(state, type));
7093 break;
7094 }
7095 case TOK_ALIGNOF:
7096 {
7097 struct type *type;
7098 int tok1, tok2;
7099 eat(state, TOK_ALIGNOF);
7100 tok1 = peek(state);
7101 tok2 = peek2(state);
7102 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7103 eat(state, TOK_LPAREN);
7104 type = type_name(state);
7105 eat(state, TOK_RPAREN);
7106 }
7107 else {
7108 struct triple *expr;
7109 expr = unary_expr(state);
7110 type = expr->type;
7111 release_expr(state, expr);
7112 }
7113 def = int_const(state, &ulong_type, align_of(state, type));
7114 break;
7115 }
7116 default:
7117 def = postfix_expr(state);
7118 break;
7119 }
7120 return def;
7121}
7122
7123static struct triple *cast_expr(struct compile_state *state)
7124{
7125 struct triple *def;
7126 int tok1, tok2;
7127 tok1 = peek(state);
7128 tok2 = peek2(state);
7129 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7130 struct type *type;
7131 eat(state, TOK_LPAREN);
7132 type = type_name(state);
7133 eat(state, TOK_RPAREN);
7134 def = read_expr(state, cast_expr(state));
7135 def = triple(state, OP_COPY, type, def, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007136 }
7137 else {
7138 def = unary_expr(state);
7139 }
7140 return def;
7141}
7142
7143static struct triple *mult_expr(struct compile_state *state)
7144{
7145 struct triple *def;
7146 int done;
7147 def = cast_expr(state);
7148 do {
7149 struct triple *left, *right;
7150 struct type *result_type;
7151 int tok, op, sign;
7152 done = 0;
7153 switch(tok = (peek(state))) {
7154 case TOK_STAR:
7155 case TOK_DIV:
7156 case TOK_MOD:
7157 left = read_expr(state, def);
7158 arithmetic(state, left);
7159
7160 eat(state, tok);
7161
7162 right = read_expr(state, cast_expr(state));
7163 arithmetic(state, right);
7164
7165 result_type = arithmetic_result(state, left, right);
7166 sign = is_signed(result_type);
7167 op = -1;
7168 switch(tok) {
7169 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
7170 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
7171 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
7172 }
7173 def = triple(state, op, result_type, left, right);
7174 break;
7175 default:
7176 done = 1;
7177 break;
7178 }
7179 } while(!done);
7180 return def;
7181}
7182
7183static struct triple *add_expr(struct compile_state *state)
7184{
7185 struct triple *def;
7186 int done;
7187 def = mult_expr(state);
7188 do {
7189 done = 0;
7190 switch( peek(state)) {
7191 case TOK_PLUS:
7192 eat(state, TOK_PLUS);
7193 def = mk_add_expr(state, def, mult_expr(state));
7194 break;
7195 case TOK_MINUS:
7196 eat(state, TOK_MINUS);
7197 def = mk_sub_expr(state, def, mult_expr(state));
7198 break;
7199 default:
7200 done = 1;
7201 break;
7202 }
7203 } while(!done);
7204 return def;
7205}
7206
7207static struct triple *shift_expr(struct compile_state *state)
7208{
7209 struct triple *def;
7210 int done;
7211 def = add_expr(state);
7212 do {
7213 struct triple *left, *right;
7214 int tok, op;
7215 done = 0;
7216 switch((tok = peek(state))) {
7217 case TOK_SL:
7218 case TOK_SR:
7219 left = read_expr(state, def);
7220 integral(state, left);
7221 left = integral_promotion(state, left);
7222
7223 eat(state, tok);
7224
7225 right = read_expr(state, add_expr(state));
7226 integral(state, right);
7227 right = integral_promotion(state, right);
7228
7229 op = (tok == TOK_SL)? OP_SL :
7230 is_signed(left->type)? OP_SSR: OP_USR;
7231
7232 def = triple(state, op, left->type, left, right);
7233 break;
7234 default:
7235 done = 1;
7236 break;
7237 }
7238 } while(!done);
7239 return def;
7240}
7241
7242static struct triple *relational_expr(struct compile_state *state)
7243{
7244#warning "Extend relational exprs to work on more than arithmetic types"
7245 struct triple *def;
7246 int done;
7247 def = shift_expr(state);
7248 do {
7249 struct triple *left, *right;
7250 struct type *arg_type;
7251 int tok, op, sign;
7252 done = 0;
7253 switch((tok = peek(state))) {
7254 case TOK_LESS:
7255 case TOK_MORE:
7256 case TOK_LESSEQ:
7257 case TOK_MOREEQ:
7258 left = read_expr(state, def);
7259 arithmetic(state, left);
7260
7261 eat(state, tok);
7262
7263 right = read_expr(state, shift_expr(state));
7264 arithmetic(state, right);
7265
7266 arg_type = arithmetic_result(state, left, right);
7267 sign = is_signed(arg_type);
7268 op = -1;
7269 switch(tok) {
7270 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
7271 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
7272 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
7273 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
7274 }
7275 def = triple(state, op, &int_type, left, right);
7276 break;
7277 default:
7278 done = 1;
7279 break;
7280 }
7281 } while(!done);
7282 return def;
7283}
7284
7285static struct triple *equality_expr(struct compile_state *state)
7286{
7287#warning "Extend equality exprs to work on more than arithmetic types"
7288 struct triple *def;
7289 int done;
7290 def = relational_expr(state);
7291 do {
7292 struct triple *left, *right;
7293 int tok, op;
7294 done = 0;
7295 switch((tok = peek(state))) {
7296 case TOK_EQEQ:
7297 case TOK_NOTEQ:
7298 left = read_expr(state, def);
7299 arithmetic(state, left);
7300 eat(state, tok);
7301 right = read_expr(state, relational_expr(state));
7302 arithmetic(state, right);
7303 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
7304 def = triple(state, op, &int_type, left, right);
7305 break;
7306 default:
7307 done = 1;
7308 break;
7309 }
7310 } while(!done);
7311 return def;
7312}
7313
7314static struct triple *and_expr(struct compile_state *state)
7315{
7316 struct triple *def;
7317 def = equality_expr(state);
7318 while(peek(state) == TOK_AND) {
7319 struct triple *left, *right;
7320 struct type *result_type;
7321 left = read_expr(state, def);
7322 integral(state, left);
7323 eat(state, TOK_AND);
7324 right = read_expr(state, equality_expr(state));
7325 integral(state, right);
7326 result_type = arithmetic_result(state, left, right);
7327 def = triple(state, OP_AND, result_type, left, right);
7328 }
7329 return def;
7330}
7331
7332static struct triple *xor_expr(struct compile_state *state)
7333{
7334 struct triple *def;
7335 def = and_expr(state);
7336 while(peek(state) == TOK_XOR) {
7337 struct triple *left, *right;
7338 struct type *result_type;
7339 left = read_expr(state, def);
7340 integral(state, left);
7341 eat(state, TOK_XOR);
7342 right = read_expr(state, and_expr(state));
7343 integral(state, right);
7344 result_type = arithmetic_result(state, left, right);
7345 def = triple(state, OP_XOR, result_type, left, right);
7346 }
7347 return def;
7348}
7349
7350static struct triple *or_expr(struct compile_state *state)
7351{
7352 struct triple *def;
7353 def = xor_expr(state);
7354 while(peek(state) == TOK_OR) {
7355 struct triple *left, *right;
7356 struct type *result_type;
7357 left = read_expr(state, def);
7358 integral(state, left);
7359 eat(state, TOK_OR);
7360 right = read_expr(state, xor_expr(state));
7361 integral(state, right);
7362 result_type = arithmetic_result(state, left, right);
7363 def = triple(state, OP_OR, result_type, left, right);
7364 }
7365 return def;
7366}
7367
7368static struct triple *land_expr(struct compile_state *state)
7369{
7370 struct triple *def;
7371 def = or_expr(state);
7372 while(peek(state) == TOK_LOGAND) {
7373 struct triple *left, *right;
7374 left = read_expr(state, def);
7375 bool(state, left);
7376 eat(state, TOK_LOGAND);
7377 right = read_expr(state, or_expr(state));
7378 bool(state, right);
7379
7380 def = triple(state, OP_LAND, &int_type,
7381 ltrue_expr(state, left),
7382 ltrue_expr(state, right));
7383 }
7384 return def;
7385}
7386
7387static struct triple *lor_expr(struct compile_state *state)
7388{
7389 struct triple *def;
7390 def = land_expr(state);
7391 while(peek(state) == TOK_LOGOR) {
7392 struct triple *left, *right;
7393 left = read_expr(state, def);
7394 bool(state, left);
7395 eat(state, TOK_LOGOR);
7396 right = read_expr(state, land_expr(state));
7397 bool(state, right);
7398
7399 def = triple(state, OP_LOR, &int_type,
7400 ltrue_expr(state, left),
7401 ltrue_expr(state, right));
7402 }
7403 return def;
7404}
7405
7406static struct triple *conditional_expr(struct compile_state *state)
7407{
7408 struct triple *def;
7409 def = lor_expr(state);
7410 if (peek(state) == TOK_QUEST) {
7411 struct triple *test, *left, *right;
7412 bool(state, def);
7413 test = ltrue_expr(state, read_expr(state, def));
7414 eat(state, TOK_QUEST);
7415 left = read_expr(state, expr(state));
7416 eat(state, TOK_COLON);
7417 right = read_expr(state, conditional_expr(state));
7418
7419 def = cond_expr(state, test, left, right);
7420 }
7421 return def;
7422}
7423
7424static struct triple *eval_const_expr(
7425 struct compile_state *state, struct triple *expr)
7426{
7427 struct triple *def;
7428 struct triple *head, *ptr;
7429 head = label(state); /* dummy initial triple */
7430 flatten(state, head, expr);
7431 for(ptr = head->next; ptr != head; ptr = ptr->next) {
7432 simplify(state, ptr);
7433 }
7434 /* Remove the constant value the tail of the list */
7435 def = head->prev;
7436 def->prev->next = def->next;
7437 def->next->prev = def->prev;
7438 def->next = def->prev = def;
7439 if (!is_const(def)) {
7440 internal_error(state, 0, "Not a constant expression");
7441 }
7442 /* Free the intermediate expressions */
7443 while(head->next != head) {
7444 release_triple(state, head->next);
7445 }
7446 free_triple(state, head);
7447 return def;
7448}
7449
7450static struct triple *constant_expr(struct compile_state *state)
7451{
7452 return eval_const_expr(state, conditional_expr(state));
7453}
7454
7455static struct triple *assignment_expr(struct compile_state *state)
7456{
7457 struct triple *def, *left, *right;
7458 int tok, op, sign;
7459 /* The C grammer in K&R shows assignment expressions
7460 * only taking unary expressions as input on their
7461 * left hand side. But specifies the precedence of
7462 * assignemnt as the lowest operator except for comma.
7463 *
7464 * Allowing conditional expressions on the left hand side
7465 * of an assignement results in a grammar that accepts
7466 * a larger set of statements than standard C. As long
7467 * as the subset of the grammar that is standard C behaves
7468 * correctly this should cause no problems.
7469 *
7470 * For the extra token strings accepted by the grammar
7471 * none of them should produce a valid lvalue, so they
7472 * should not produce functioning programs.
7473 *
7474 * GCC has this bug as well, so surprises should be minimal.
7475 */
7476 def = conditional_expr(state);
7477 left = def;
7478 switch((tok = peek(state))) {
7479 case TOK_EQ:
7480 lvalue(state, left);
7481 eat(state, TOK_EQ);
7482 def = write_expr(state, left,
7483 read_expr(state, assignment_expr(state)));
7484 break;
7485 case TOK_TIMESEQ:
7486 case TOK_DIVEQ:
7487 case TOK_MODEQ:
Eric Biedermanb138ac82003-04-22 18:44:01 +00007488 lvalue(state, left);
7489 arithmetic(state, left);
7490 eat(state, tok);
7491 right = read_expr(state, assignment_expr(state));
7492 arithmetic(state, right);
7493
7494 sign = is_signed(left->type);
7495 op = -1;
7496 switch(tok) {
7497 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
7498 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
7499 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007500 }
7501 def = write_expr(state, left,
7502 triple(state, op, left->type,
7503 read_expr(state, left), right));
7504 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007505 case TOK_PLUSEQ:
7506 lvalue(state, left);
7507 eat(state, TOK_PLUSEQ);
7508 def = write_expr(state, left,
7509 mk_add_expr(state, left, assignment_expr(state)));
7510 break;
7511 case TOK_MINUSEQ:
7512 lvalue(state, left);
7513 eat(state, TOK_MINUSEQ);
7514 def = write_expr(state, left,
7515 mk_sub_expr(state, left, assignment_expr(state)));
7516 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007517 case TOK_SLEQ:
7518 case TOK_SREQ:
7519 case TOK_ANDEQ:
7520 case TOK_XOREQ:
7521 case TOK_OREQ:
7522 lvalue(state, left);
7523 integral(state, left);
7524 eat(state, tok);
7525 right = read_expr(state, assignment_expr(state));
7526 integral(state, right);
7527 right = integral_promotion(state, right);
7528 sign = is_signed(left->type);
7529 op = -1;
7530 switch(tok) {
7531 case TOK_SLEQ: op = OP_SL; break;
7532 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
7533 case TOK_ANDEQ: op = OP_AND; break;
7534 case TOK_XOREQ: op = OP_XOR; break;
7535 case TOK_OREQ: op = OP_OR; break;
7536 }
7537 def = write_expr(state, left,
7538 triple(state, op, left->type,
7539 read_expr(state, left), right));
7540 break;
7541 }
7542 return def;
7543}
7544
7545static struct triple *expr(struct compile_state *state)
7546{
7547 struct triple *def;
7548 def = assignment_expr(state);
7549 while(peek(state) == TOK_COMMA) {
7550 struct triple *left, *right;
7551 left = def;
7552 eat(state, TOK_COMMA);
7553 right = assignment_expr(state);
7554 def = triple(state, OP_COMMA, right->type, left, right);
7555 }
7556 return def;
7557}
7558
7559static void expr_statement(struct compile_state *state, struct triple *first)
7560{
7561 if (peek(state) != TOK_SEMI) {
7562 flatten(state, first, expr(state));
7563 }
7564 eat(state, TOK_SEMI);
7565}
7566
7567static void if_statement(struct compile_state *state, struct triple *first)
7568{
7569 struct triple *test, *jmp1, *jmp2, *middle, *end;
7570
7571 jmp1 = jmp2 = middle = 0;
7572 eat(state, TOK_IF);
7573 eat(state, TOK_LPAREN);
7574 test = expr(state);
7575 bool(state, test);
7576 /* Cleanup and invert the test */
7577 test = lfalse_expr(state, read_expr(state, test));
7578 eat(state, TOK_RPAREN);
7579 /* Generate the needed pieces */
7580 middle = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007581 jmp1 = branch(state, middle, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007582 /* Thread the pieces together */
7583 flatten(state, first, test);
7584 flatten(state, first, jmp1);
7585 flatten(state, first, label(state));
7586 statement(state, first);
7587 if (peek(state) == TOK_ELSE) {
7588 eat(state, TOK_ELSE);
7589 /* Generate the rest of the pieces */
7590 end = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007591 jmp2 = branch(state, end, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007592 /* Thread them together */
7593 flatten(state, first, jmp2);
7594 flatten(state, first, middle);
7595 statement(state, first);
7596 flatten(state, first, end);
7597 }
7598 else {
7599 flatten(state, first, middle);
7600 }
7601}
7602
7603static void for_statement(struct compile_state *state, struct triple *first)
7604{
7605 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
7606 struct triple *label1, *label2, *label3;
7607 struct hash_entry *ident;
7608
7609 eat(state, TOK_FOR);
7610 eat(state, TOK_LPAREN);
7611 head = test = tail = jmp1 = jmp2 = 0;
7612 if (peek(state) != TOK_SEMI) {
7613 head = expr(state);
7614 }
7615 eat(state, TOK_SEMI);
7616 if (peek(state) != TOK_SEMI) {
7617 test = expr(state);
7618 bool(state, test);
7619 test = ltrue_expr(state, read_expr(state, test));
7620 }
7621 eat(state, TOK_SEMI);
7622 if (peek(state) != TOK_RPAREN) {
7623 tail = expr(state);
7624 }
7625 eat(state, TOK_RPAREN);
7626 /* Generate the needed pieces */
7627 label1 = label(state);
7628 label2 = label(state);
7629 label3 = label(state);
7630 if (test) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007631 jmp1 = branch(state, label3, 0);
7632 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007633 }
7634 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007635 jmp2 = branch(state, label1, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007636 }
7637 end = label(state);
7638 /* Remember where break and continue go */
7639 start_scope(state);
7640 ident = state->i_break;
7641 symbol(state, ident, &ident->sym_ident, end, end->type);
7642 ident = state->i_continue;
7643 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7644 /* Now include the body */
7645 flatten(state, first, head);
7646 flatten(state, first, jmp1);
7647 flatten(state, first, label1);
7648 statement(state, first);
7649 flatten(state, first, label2);
7650 flatten(state, first, tail);
7651 flatten(state, first, label3);
7652 flatten(state, first, test);
7653 flatten(state, first, jmp2);
7654 flatten(state, first, end);
7655 /* Cleanup the break/continue scope */
7656 end_scope(state);
7657}
7658
7659static void while_statement(struct compile_state *state, struct triple *first)
7660{
7661 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
7662 struct hash_entry *ident;
7663 eat(state, TOK_WHILE);
7664 eat(state, TOK_LPAREN);
7665 test = expr(state);
7666 bool(state, test);
7667 test = ltrue_expr(state, read_expr(state, test));
7668 eat(state, TOK_RPAREN);
7669 /* Generate the needed pieces */
7670 label1 = label(state);
7671 label2 = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007672 jmp1 = branch(state, label2, 0);
7673 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007674 end = label(state);
7675 /* Remember where break and continue go */
7676 start_scope(state);
7677 ident = state->i_break;
7678 symbol(state, ident, &ident->sym_ident, end, end->type);
7679 ident = state->i_continue;
7680 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7681 /* Thread them together */
7682 flatten(state, first, jmp1);
7683 flatten(state, first, label1);
7684 statement(state, first);
7685 flatten(state, first, label2);
7686 flatten(state, first, test);
7687 flatten(state, first, jmp2);
7688 flatten(state, first, end);
7689 /* Cleanup the break/continue scope */
7690 end_scope(state);
7691}
7692
7693static void do_statement(struct compile_state *state, struct triple *first)
7694{
7695 struct triple *label1, *label2, *test, *end;
7696 struct hash_entry *ident;
7697 eat(state, TOK_DO);
7698 /* Generate the needed pieces */
7699 label1 = label(state);
7700 label2 = label(state);
7701 end = label(state);
7702 /* Remember where break and continue go */
7703 start_scope(state);
7704 ident = state->i_break;
7705 symbol(state, ident, &ident->sym_ident, end, end->type);
7706 ident = state->i_continue;
7707 symbol(state, ident, &ident->sym_ident, label2, label2->type);
7708 /* Now include the body */
7709 flatten(state, first, label1);
7710 statement(state, first);
7711 /* Cleanup the break/continue scope */
7712 end_scope(state);
7713 /* Eat the rest of the loop */
7714 eat(state, TOK_WHILE);
7715 eat(state, TOK_LPAREN);
7716 test = read_expr(state, expr(state));
7717 bool(state, test);
7718 eat(state, TOK_RPAREN);
7719 eat(state, TOK_SEMI);
7720 /* Thread the pieces together */
7721 test = ltrue_expr(state, test);
7722 flatten(state, first, label2);
7723 flatten(state, first, test);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007724 flatten(state, first, branch(state, label1, test));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007725 flatten(state, first, end);
7726}
7727
7728
7729static void return_statement(struct compile_state *state, struct triple *first)
7730{
7731 struct triple *jmp, *mv, *dest, *var, *val;
7732 int last;
7733 eat(state, TOK_RETURN);
7734
7735#warning "FIXME implement a more general excess branch elimination"
7736 val = 0;
7737 /* If we have a return value do some more work */
7738 if (peek(state) != TOK_SEMI) {
7739 val = read_expr(state, expr(state));
7740 }
7741 eat(state, TOK_SEMI);
7742
7743 /* See if this last statement in a function */
7744 last = ((peek(state) == TOK_RBRACE) &&
7745 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
7746
7747 /* Find the return variable */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007748 var = MISC(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007749 /* Find the return destination */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007750 dest = RHS(state->main_function, 0)->prev;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007751 mv = jmp = 0;
7752 /* If needed generate a jump instruction */
7753 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007754 jmp = branch(state, dest, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007755 }
7756 /* If needed generate an assignment instruction */
7757 if (val) {
7758 mv = write_expr(state, var, val);
7759 }
7760 /* Now put the code together */
7761 if (mv) {
7762 flatten(state, first, mv);
7763 flatten(state, first, jmp);
7764 }
7765 else if (jmp) {
7766 flatten(state, first, jmp);
7767 }
7768}
7769
7770static void break_statement(struct compile_state *state, struct triple *first)
7771{
7772 struct triple *dest;
7773 eat(state, TOK_BREAK);
7774 eat(state, TOK_SEMI);
7775 if (!state->i_break->sym_ident) {
7776 error(state, 0, "break statement not within loop or switch");
7777 }
7778 dest = state->i_break->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007779 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007780}
7781
7782static void continue_statement(struct compile_state *state, struct triple *first)
7783{
7784 struct triple *dest;
7785 eat(state, TOK_CONTINUE);
7786 eat(state, TOK_SEMI);
7787 if (!state->i_continue->sym_ident) {
7788 error(state, 0, "continue statement outside of a loop");
7789 }
7790 dest = state->i_continue->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007791 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007792}
7793
7794static void goto_statement(struct compile_state *state, struct triple *first)
7795{
7796 FINISHME();
7797 eat(state, TOK_GOTO);
7798 eat(state, TOK_IDENT);
7799 eat(state, TOK_SEMI);
7800 error(state, 0, "goto is not implemeted");
7801 FINISHME();
7802}
7803
7804static void labeled_statement(struct compile_state *state, struct triple *first)
7805{
7806 FINISHME();
7807 eat(state, TOK_IDENT);
7808 eat(state, TOK_COLON);
7809 statement(state, first);
7810 error(state, 0, "labeled statements are not implemented");
7811 FINISHME();
7812}
7813
7814static void switch_statement(struct compile_state *state, struct triple *first)
7815{
7816 FINISHME();
7817 eat(state, TOK_SWITCH);
7818 eat(state, TOK_LPAREN);
7819 expr(state);
7820 eat(state, TOK_RPAREN);
7821 statement(state, first);
7822 error(state, 0, "switch statements are not implemented");
7823 FINISHME();
7824}
7825
7826static void case_statement(struct compile_state *state, struct triple *first)
7827{
7828 FINISHME();
7829 eat(state, TOK_CASE);
7830 constant_expr(state);
7831 eat(state, TOK_COLON);
7832 statement(state, first);
7833 error(state, 0, "case statements are not implemented");
7834 FINISHME();
7835}
7836
7837static void default_statement(struct compile_state *state, struct triple *first)
7838{
7839 FINISHME();
7840 eat(state, TOK_DEFAULT);
7841 eat(state, TOK_COLON);
7842 statement(state, first);
7843 error(state, 0, "default statements are not implemented");
7844 FINISHME();
7845}
7846
7847static void asm_statement(struct compile_state *state, struct triple *first)
7848{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007849 struct asm_info *info;
7850 struct {
7851 struct triple *constraint;
7852 struct triple *expr;
7853 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
7854 struct triple *def, *asm_str;
7855 int out, in, clobbers, more, colons, i;
7856
7857 eat(state, TOK_ASM);
7858 /* For now ignore the qualifiers */
7859 switch(peek(state)) {
7860 case TOK_CONST:
7861 eat(state, TOK_CONST);
7862 break;
7863 case TOK_VOLATILE:
7864 eat(state, TOK_VOLATILE);
7865 break;
7866 }
7867 eat(state, TOK_LPAREN);
7868 asm_str = string_constant(state);
7869
7870 colons = 0;
7871 out = in = clobbers = 0;
7872 /* Outputs */
7873 if ((colons == 0) && (peek(state) == TOK_COLON)) {
7874 eat(state, TOK_COLON);
7875 colons++;
7876 more = (peek(state) == TOK_LIT_STRING);
7877 while(more) {
7878 struct triple *var;
7879 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +00007880 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007881 more = 0;
7882 if (out > MAX_LHS) {
7883 error(state, 0, "Maximum output count exceeded.");
7884 }
7885 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +00007886 str = constraint->u.blob;
7887 if (str[0] != '=') {
7888 error(state, 0, "Output constraint does not start with =");
7889 }
7890 constraint->u.blob = str + 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007891 eat(state, TOK_LPAREN);
7892 var = conditional_expr(state);
7893 eat(state, TOK_RPAREN);
7894
7895 lvalue(state, var);
7896 out_param[out].constraint = constraint;
7897 out_param[out].expr = var;
7898 if (peek(state) == TOK_COMMA) {
7899 eat(state, TOK_COMMA);
7900 more = 1;
7901 }
7902 out++;
7903 }
7904 }
7905 /* Inputs */
7906 if ((colons == 1) && (peek(state) == TOK_COLON)) {
7907 eat(state, TOK_COLON);
7908 colons++;
7909 more = (peek(state) == TOK_LIT_STRING);
7910 while(more) {
7911 struct triple *val;
7912 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +00007913 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007914 more = 0;
7915 if (in > MAX_RHS) {
7916 error(state, 0, "Maximum input count exceeded.");
7917 }
7918 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +00007919 str = constraint->u.blob;
7920 if (digitp(str[0] && str[1] == '\0')) {
7921 int val;
7922 val = digval(str[0]);
7923 if ((val < 0) || (val >= out)) {
7924 error(state, 0, "Invalid input constraint %d", val);
7925 }
7926 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007927 eat(state, TOK_LPAREN);
7928 val = conditional_expr(state);
7929 eat(state, TOK_RPAREN);
7930
7931 in_param[in].constraint = constraint;
7932 in_param[in].expr = val;
7933 if (peek(state) == TOK_COMMA) {
7934 eat(state, TOK_COMMA);
7935 more = 1;
7936 }
7937 in++;
7938 }
7939 }
7940
7941 /* Clobber */
7942 if ((colons == 2) && (peek(state) == TOK_COLON)) {
7943 eat(state, TOK_COLON);
7944 colons++;
7945 more = (peek(state) == TOK_LIT_STRING);
7946 while(more) {
7947 struct triple *clobber;
7948 more = 0;
7949 if ((clobbers + out) > MAX_LHS) {
7950 error(state, 0, "Maximum clobber limit exceeded.");
7951 }
7952 clobber = string_constant(state);
7953 eat(state, TOK_RPAREN);
7954
7955 clob_param[clobbers].constraint = clobber;
7956 if (peek(state) == TOK_COMMA) {
7957 eat(state, TOK_COMMA);
7958 more = 1;
7959 }
7960 clobbers++;
7961 }
7962 }
7963 eat(state, TOK_RPAREN);
7964 eat(state, TOK_SEMI);
7965
7966
7967 info = xcmalloc(sizeof(*info), "asm_info");
7968 info->str = asm_str->u.blob;
7969 free_triple(state, asm_str);
7970
7971 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
7972 def->u.ainfo = info;
Eric Biederman8d9c1232003-06-17 08:42:17 +00007973
7974 /* Find the register constraints */
7975 for(i = 0; i < out; i++) {
7976 struct triple *constraint;
7977 constraint = out_param[i].constraint;
7978 info->tmpl.lhs[i] = arch_reg_constraint(state,
7979 out_param[i].expr->type, constraint->u.blob);
7980 free_triple(state, constraint);
7981 }
7982 for(; i - out < clobbers; i++) {
7983 struct triple *constraint;
7984 constraint = clob_param[i - out].constraint;
7985 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
7986 free_triple(state, constraint);
7987 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007988 for(i = 0; i < in; i++) {
7989 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +00007990 const char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007991 constraint = in_param[i].constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +00007992 str = constraint->u.blob;
7993 if (digitp(str[0]) && str[1] == '\0') {
7994 struct reg_info cinfo;
7995 int val;
7996 val = digval(str[0]);
7997 cinfo.reg = info->tmpl.lhs[val].reg;
7998 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
7999 cinfo.regcm &= info->tmpl.lhs[val].regcm;
8000 if (cinfo.reg == REG_UNSET) {
8001 cinfo.reg = REG_VIRT0 + val;
8002 }
8003 if (cinfo.regcm == 0) {
8004 error(state, 0, "No registers for %d", val);
8005 }
8006 info->tmpl.lhs[val] = cinfo;
8007 info->tmpl.rhs[i] = cinfo;
8008
8009 } else {
8010 info->tmpl.rhs[i] = arch_reg_constraint(state,
8011 in_param[i].expr->type, str);
8012 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008013 free_triple(state, constraint);
8014 }
Eric Biederman8d9c1232003-06-17 08:42:17 +00008015
8016 /* Now build the helper expressions */
8017 for(i = 0; i < in; i++) {
8018 RHS(def, i) = read_expr(state,in_param[i].expr);
8019 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008020 flatten(state, first, def);
8021 for(i = 0; i < out; i++) {
8022 struct triple *piece;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008023 piece = triple(state, OP_PIECE, out_param[i].expr->type, def, 0);
8024 piece->u.cval = i;
8025 LHS(def, i) = piece;
8026 flatten(state, first,
8027 write_expr(state, out_param[i].expr, piece));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008028 }
8029 for(; i - out < clobbers; i++) {
8030 struct triple *piece;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008031 piece = triple(state, OP_PIECE, &void_type, def, 0);
8032 piece->u.cval = i;
8033 LHS(def, i) = piece;
8034 flatten(state, first, piece);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008035 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008036}
8037
8038
8039static int isdecl(int tok)
8040{
8041 switch(tok) {
8042 case TOK_AUTO:
8043 case TOK_REGISTER:
8044 case TOK_STATIC:
8045 case TOK_EXTERN:
8046 case TOK_TYPEDEF:
8047 case TOK_CONST:
8048 case TOK_RESTRICT:
8049 case TOK_VOLATILE:
8050 case TOK_VOID:
8051 case TOK_CHAR:
8052 case TOK_SHORT:
8053 case TOK_INT:
8054 case TOK_LONG:
8055 case TOK_FLOAT:
8056 case TOK_DOUBLE:
8057 case TOK_SIGNED:
8058 case TOK_UNSIGNED:
8059 case TOK_STRUCT:
8060 case TOK_UNION:
8061 case TOK_ENUM:
8062 case TOK_TYPE_NAME: /* typedef name */
8063 return 1;
8064 default:
8065 return 0;
8066 }
8067}
8068
8069static void compound_statement(struct compile_state *state, struct triple *first)
8070{
8071 eat(state, TOK_LBRACE);
8072 start_scope(state);
8073
8074 /* statement-list opt */
8075 while (peek(state) != TOK_RBRACE) {
8076 statement(state, first);
8077 }
8078 end_scope(state);
8079 eat(state, TOK_RBRACE);
8080}
8081
8082static void statement(struct compile_state *state, struct triple *first)
8083{
8084 int tok;
8085 tok = peek(state);
8086 if (tok == TOK_LBRACE) {
8087 compound_statement(state, first);
8088 }
8089 else if (tok == TOK_IF) {
8090 if_statement(state, first);
8091 }
8092 else if (tok == TOK_FOR) {
8093 for_statement(state, first);
8094 }
8095 else if (tok == TOK_WHILE) {
8096 while_statement(state, first);
8097 }
8098 else if (tok == TOK_DO) {
8099 do_statement(state, first);
8100 }
8101 else if (tok == TOK_RETURN) {
8102 return_statement(state, first);
8103 }
8104 else if (tok == TOK_BREAK) {
8105 break_statement(state, first);
8106 }
8107 else if (tok == TOK_CONTINUE) {
8108 continue_statement(state, first);
8109 }
8110 else if (tok == TOK_GOTO) {
8111 goto_statement(state, first);
8112 }
8113 else if (tok == TOK_SWITCH) {
8114 switch_statement(state, first);
8115 }
8116 else if (tok == TOK_ASM) {
8117 asm_statement(state, first);
8118 }
8119 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
8120 labeled_statement(state, first);
8121 }
8122 else if (tok == TOK_CASE) {
8123 case_statement(state, first);
8124 }
8125 else if (tok == TOK_DEFAULT) {
8126 default_statement(state, first);
8127 }
8128 else if (isdecl(tok)) {
8129 /* This handles C99 intermixing of statements and decls */
8130 decl(state, first);
8131 }
8132 else {
8133 expr_statement(state, first);
8134 }
8135}
8136
8137static struct type *param_decl(struct compile_state *state)
8138{
8139 struct type *type;
8140 struct hash_entry *ident;
8141 /* Cheat so the declarator will know we are not global */
8142 start_scope(state);
8143 ident = 0;
8144 type = decl_specifiers(state);
8145 type = declarator(state, type, &ident, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008146 type->field_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008147 end_scope(state);
8148 return type;
8149}
8150
8151static struct type *param_type_list(struct compile_state *state, struct type *type)
8152{
8153 struct type *ftype, **next;
8154 ftype = new_type(TYPE_FUNCTION, type, param_decl(state));
8155 next = &ftype->right;
8156 while(peek(state) == TOK_COMMA) {
8157 eat(state, TOK_COMMA);
8158 if (peek(state) == TOK_DOTS) {
8159 eat(state, TOK_DOTS);
8160 error(state, 0, "variadic functions not supported");
8161 }
8162 else {
8163 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
8164 next = &((*next)->right);
8165 }
8166 }
8167 return ftype;
8168}
8169
8170
8171static struct type *type_name(struct compile_state *state)
8172{
8173 struct type *type;
8174 type = specifier_qualifier_list(state);
8175 /* abstract-declarator (may consume no tokens) */
8176 type = declarator(state, type, 0, 0);
8177 return type;
8178}
8179
8180static struct type *direct_declarator(
8181 struct compile_state *state, struct type *type,
8182 struct hash_entry **ident, int need_ident)
8183{
8184 struct type *outer;
8185 int op;
8186 outer = 0;
8187 arrays_complete(state, type);
8188 switch(peek(state)) {
8189 case TOK_IDENT:
8190 eat(state, TOK_IDENT);
8191 if (!ident) {
8192 error(state, 0, "Unexpected identifier found");
8193 }
8194 /* The name of what we are declaring */
8195 *ident = state->token[0].ident;
8196 break;
8197 case TOK_LPAREN:
8198 eat(state, TOK_LPAREN);
8199 outer = declarator(state, type, ident, need_ident);
8200 eat(state, TOK_RPAREN);
8201 break;
8202 default:
8203 if (need_ident) {
8204 error(state, 0, "Identifier expected");
8205 }
8206 break;
8207 }
8208 do {
8209 op = 1;
8210 arrays_complete(state, type);
8211 switch(peek(state)) {
8212 case TOK_LPAREN:
8213 eat(state, TOK_LPAREN);
8214 type = param_type_list(state, type);
8215 eat(state, TOK_RPAREN);
8216 break;
8217 case TOK_LBRACKET:
8218 {
8219 unsigned int qualifiers;
8220 struct triple *value;
8221 value = 0;
8222 eat(state, TOK_LBRACKET);
8223 if (peek(state) != TOK_RBRACKET) {
8224 value = constant_expr(state);
8225 integral(state, value);
8226 }
8227 eat(state, TOK_RBRACKET);
8228
8229 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
8230 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
8231 if (value) {
8232 type->elements = value->u.cval;
8233 free_triple(state, value);
8234 } else {
8235 type->elements = ELEMENT_COUNT_UNSPECIFIED;
8236 op = 0;
8237 }
8238 }
8239 break;
8240 default:
8241 op = 0;
8242 break;
8243 }
8244 } while(op);
8245 if (outer) {
8246 struct type *inner;
8247 arrays_complete(state, type);
8248 FINISHME();
8249 for(inner = outer; inner->left; inner = inner->left)
8250 ;
8251 inner->left = type;
8252 type = outer;
8253 }
8254 return type;
8255}
8256
8257static struct type *declarator(
8258 struct compile_state *state, struct type *type,
8259 struct hash_entry **ident, int need_ident)
8260{
8261 while(peek(state) == TOK_STAR) {
8262 eat(state, TOK_STAR);
8263 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
8264 }
8265 type = direct_declarator(state, type, ident, need_ident);
8266 return type;
8267}
8268
8269
8270static struct type *typedef_name(
8271 struct compile_state *state, unsigned int specifiers)
8272{
8273 struct hash_entry *ident;
8274 struct type *type;
8275 eat(state, TOK_TYPE_NAME);
8276 ident = state->token[0].ident;
8277 type = ident->sym_ident->type;
8278 specifiers |= type->type & QUAL_MASK;
8279 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
8280 (type->type & (STOR_MASK | QUAL_MASK))) {
8281 type = clone_type(specifiers, type);
8282 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008283 return type;
8284}
8285
8286static struct type *enum_specifier(
8287 struct compile_state *state, unsigned int specifiers)
8288{
8289 int tok;
8290 struct type *type;
8291 type = 0;
8292 FINISHME();
8293 eat(state, TOK_ENUM);
8294 tok = peek(state);
8295 if (tok == TOK_IDENT) {
8296 eat(state, TOK_IDENT);
8297 }
8298 if ((tok != TOK_IDENT) || (peek(state) == TOK_LBRACE)) {
8299 eat(state, TOK_LBRACE);
8300 do {
8301 eat(state, TOK_IDENT);
8302 if (peek(state) == TOK_EQ) {
8303 eat(state, TOK_EQ);
8304 constant_expr(state);
8305 }
8306 if (peek(state) == TOK_COMMA) {
8307 eat(state, TOK_COMMA);
8308 }
8309 } while(peek(state) != TOK_RBRACE);
8310 eat(state, TOK_RBRACE);
8311 }
8312 FINISHME();
8313 return type;
8314}
8315
8316#if 0
8317static struct type *struct_declarator(
8318 struct compile_state *state, struct type *type, struct hash_entry **ident)
8319{
8320 int tok;
8321#warning "struct_declarator is complicated because of bitfields, kill them?"
8322 tok = peek(state);
8323 if (tok != TOK_COLON) {
8324 type = declarator(state, type, ident, 1);
8325 }
8326 if ((tok == TOK_COLON) || (peek(state) == TOK_COLON)) {
8327 eat(state, TOK_COLON);
8328 constant_expr(state);
8329 }
8330 FINISHME();
8331 return type;
8332}
8333#endif
8334
8335static struct type *struct_or_union_specifier(
8336 struct compile_state *state, unsigned int specifiers)
8337{
Eric Biederman0babc1c2003-05-09 02:39:00 +00008338 struct type *struct_type;
8339 struct hash_entry *ident;
8340 unsigned int type_join;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008341 int tok;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008342 struct_type = 0;
8343 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008344 switch(peek(state)) {
8345 case TOK_STRUCT:
8346 eat(state, TOK_STRUCT);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008347 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008348 break;
8349 case TOK_UNION:
8350 eat(state, TOK_UNION);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008351 type_join = TYPE_OVERLAP;
8352 error(state, 0, "unions not yet supported\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008353 break;
8354 default:
8355 eat(state, TOK_STRUCT);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008356 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008357 break;
8358 }
8359 tok = peek(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008360 if ((tok == TOK_IDENT) || (tok == TOK_TYPE_NAME)) {
8361 eat(state, tok);
8362 ident = state->token[0].ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008363 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00008364 if (!ident || (peek(state) == TOK_LBRACE)) {
8365 ulong_t elements;
8366 elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008367 eat(state, TOK_LBRACE);
8368 do {
8369 struct type *base_type;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008370 struct type **next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008371 int done;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008372 base_type = specifier_qualifier_list(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008373 next = &struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008374 do {
8375 struct type *type;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008376 struct hash_entry *fident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008377 done = 1;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008378 type = declarator(state, base_type, &fident, 1);
8379 elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008380 if (peek(state) == TOK_COMMA) {
8381 done = 0;
8382 eat(state, TOK_COMMA);
8383 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00008384 type = clone_type(0, type);
8385 type->field_ident = fident;
8386 if (*next) {
8387 *next = new_type(type_join, *next, type);
8388 next = &((*next)->right);
8389 } else {
8390 *next = type;
8391 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008392 } while(!done);
8393 eat(state, TOK_SEMI);
8394 } while(peek(state) != TOK_RBRACE);
8395 eat(state, TOK_RBRACE);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008396 struct_type = new_type(TYPE_STRUCT, struct_type, 0);
8397 struct_type->type_ident = ident;
8398 struct_type->elements = elements;
8399 symbol(state, ident, &ident->sym_struct, 0, struct_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008400 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00008401 if (ident && ident->sym_struct) {
8402 struct_type = ident->sym_struct->type;
8403 }
8404 else if (ident && !ident->sym_struct) {
8405 error(state, 0, "struct %s undeclared", ident->name);
8406 }
8407 return struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008408}
8409
8410static unsigned int storage_class_specifier_opt(struct compile_state *state)
8411{
8412 unsigned int specifiers;
8413 switch(peek(state)) {
8414 case TOK_AUTO:
8415 eat(state, TOK_AUTO);
8416 specifiers = STOR_AUTO;
8417 break;
8418 case TOK_REGISTER:
8419 eat(state, TOK_REGISTER);
8420 specifiers = STOR_REGISTER;
8421 break;
8422 case TOK_STATIC:
8423 eat(state, TOK_STATIC);
8424 specifiers = STOR_STATIC;
8425 break;
8426 case TOK_EXTERN:
8427 eat(state, TOK_EXTERN);
8428 specifiers = STOR_EXTERN;
8429 break;
8430 case TOK_TYPEDEF:
8431 eat(state, TOK_TYPEDEF);
8432 specifiers = STOR_TYPEDEF;
8433 break;
8434 default:
8435 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
8436 specifiers = STOR_STATIC;
8437 }
8438 else {
8439 specifiers = STOR_AUTO;
8440 }
8441 }
8442 return specifiers;
8443}
8444
8445static unsigned int function_specifier_opt(struct compile_state *state)
8446{
8447 /* Ignore the inline keyword */
8448 unsigned int specifiers;
8449 specifiers = 0;
8450 switch(peek(state)) {
8451 case TOK_INLINE:
8452 eat(state, TOK_INLINE);
8453 specifiers = STOR_INLINE;
8454 }
8455 return specifiers;
8456}
8457
8458static unsigned int type_qualifiers(struct compile_state *state)
8459{
8460 unsigned int specifiers;
8461 int done;
8462 done = 0;
8463 specifiers = QUAL_NONE;
8464 do {
8465 switch(peek(state)) {
8466 case TOK_CONST:
8467 eat(state, TOK_CONST);
8468 specifiers = QUAL_CONST;
8469 break;
8470 case TOK_VOLATILE:
8471 eat(state, TOK_VOLATILE);
8472 specifiers = QUAL_VOLATILE;
8473 break;
8474 case TOK_RESTRICT:
8475 eat(state, TOK_RESTRICT);
8476 specifiers = QUAL_RESTRICT;
8477 break;
8478 default:
8479 done = 1;
8480 break;
8481 }
8482 } while(!done);
8483 return specifiers;
8484}
8485
8486static struct type *type_specifier(
8487 struct compile_state *state, unsigned int spec)
8488{
8489 struct type *type;
8490 type = 0;
8491 switch(peek(state)) {
8492 case TOK_VOID:
8493 eat(state, TOK_VOID);
8494 type = new_type(TYPE_VOID | spec, 0, 0);
8495 break;
8496 case TOK_CHAR:
8497 eat(state, TOK_CHAR);
8498 type = new_type(TYPE_CHAR | spec, 0, 0);
8499 break;
8500 case TOK_SHORT:
8501 eat(state, TOK_SHORT);
8502 if (peek(state) == TOK_INT) {
8503 eat(state, TOK_INT);
8504 }
8505 type = new_type(TYPE_SHORT | spec, 0, 0);
8506 break;
8507 case TOK_INT:
8508 eat(state, TOK_INT);
8509 type = new_type(TYPE_INT | spec, 0, 0);
8510 break;
8511 case TOK_LONG:
8512 eat(state, TOK_LONG);
8513 switch(peek(state)) {
8514 case TOK_LONG:
8515 eat(state, TOK_LONG);
8516 error(state, 0, "long long not supported");
8517 break;
8518 case TOK_DOUBLE:
8519 eat(state, TOK_DOUBLE);
8520 error(state, 0, "long double not supported");
8521 break;
8522 case TOK_INT:
8523 eat(state, TOK_INT);
8524 type = new_type(TYPE_LONG | spec, 0, 0);
8525 break;
8526 default:
8527 type = new_type(TYPE_LONG | spec, 0, 0);
8528 break;
8529 }
8530 break;
8531 case TOK_FLOAT:
8532 eat(state, TOK_FLOAT);
8533 error(state, 0, "type float not supported");
8534 break;
8535 case TOK_DOUBLE:
8536 eat(state, TOK_DOUBLE);
8537 error(state, 0, "type double not supported");
8538 break;
8539 case TOK_SIGNED:
8540 eat(state, TOK_SIGNED);
8541 switch(peek(state)) {
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, "type long long not supported");
8548 break;
8549 case TOK_INT:
8550 eat(state, TOK_INT);
8551 type = new_type(TYPE_LONG | spec, 0, 0);
8552 break;
8553 default:
8554 type = new_type(TYPE_LONG | spec, 0, 0);
8555 break;
8556 }
8557 break;
8558 case TOK_INT:
8559 eat(state, TOK_INT);
8560 type = new_type(TYPE_INT | spec, 0, 0);
8561 break;
8562 case TOK_SHORT:
8563 eat(state, TOK_SHORT);
8564 type = new_type(TYPE_SHORT | spec, 0, 0);
8565 break;
8566 case TOK_CHAR:
8567 eat(state, TOK_CHAR);
8568 type = new_type(TYPE_CHAR | spec, 0, 0);
8569 break;
8570 default:
8571 type = new_type(TYPE_INT | spec, 0, 0);
8572 break;
8573 }
8574 break;
8575 case TOK_UNSIGNED:
8576 eat(state, TOK_UNSIGNED);
8577 switch(peek(state)) {
8578 case TOK_LONG:
8579 eat(state, TOK_LONG);
8580 switch(peek(state)) {
8581 case TOK_LONG:
8582 eat(state, TOK_LONG);
8583 error(state, 0, "unsigned long long not supported");
8584 break;
8585 case TOK_INT:
8586 eat(state, TOK_INT);
8587 type = new_type(TYPE_ULONG | spec, 0, 0);
8588 break;
8589 default:
8590 type = new_type(TYPE_ULONG | spec, 0, 0);
8591 break;
8592 }
8593 break;
8594 case TOK_INT:
8595 eat(state, TOK_INT);
8596 type = new_type(TYPE_UINT | spec, 0, 0);
8597 break;
8598 case TOK_SHORT:
8599 eat(state, TOK_SHORT);
8600 type = new_type(TYPE_USHORT | spec, 0, 0);
8601 break;
8602 case TOK_CHAR:
8603 eat(state, TOK_CHAR);
8604 type = new_type(TYPE_UCHAR | spec, 0, 0);
8605 break;
8606 default:
8607 type = new_type(TYPE_UINT | spec, 0, 0);
8608 break;
8609 }
8610 break;
8611 /* struct or union specifier */
8612 case TOK_STRUCT:
8613 case TOK_UNION:
8614 type = struct_or_union_specifier(state, spec);
8615 break;
8616 /* enum-spefifier */
8617 case TOK_ENUM:
8618 type = enum_specifier(state, spec);
8619 break;
8620 /* typedef name */
8621 case TOK_TYPE_NAME:
8622 type = typedef_name(state, spec);
8623 break;
8624 default:
8625 error(state, 0, "bad type specifier %s",
8626 tokens[peek(state)]);
8627 break;
8628 }
8629 return type;
8630}
8631
8632static int istype(int tok)
8633{
8634 switch(tok) {
8635 case TOK_CONST:
8636 case TOK_RESTRICT:
8637 case TOK_VOLATILE:
8638 case TOK_VOID:
8639 case TOK_CHAR:
8640 case TOK_SHORT:
8641 case TOK_INT:
8642 case TOK_LONG:
8643 case TOK_FLOAT:
8644 case TOK_DOUBLE:
8645 case TOK_SIGNED:
8646 case TOK_UNSIGNED:
8647 case TOK_STRUCT:
8648 case TOK_UNION:
8649 case TOK_ENUM:
8650 case TOK_TYPE_NAME:
8651 return 1;
8652 default:
8653 return 0;
8654 }
8655}
8656
8657
8658static struct type *specifier_qualifier_list(struct compile_state *state)
8659{
8660 struct type *type;
8661 unsigned int specifiers = 0;
8662
8663 /* type qualifiers */
8664 specifiers |= type_qualifiers(state);
8665
8666 /* type specifier */
8667 type = type_specifier(state, specifiers);
8668
8669 return type;
8670}
8671
8672static int isdecl_specifier(int tok)
8673{
8674 switch(tok) {
8675 /* storage class specifier */
8676 case TOK_AUTO:
8677 case TOK_REGISTER:
8678 case TOK_STATIC:
8679 case TOK_EXTERN:
8680 case TOK_TYPEDEF:
8681 /* type qualifier */
8682 case TOK_CONST:
8683 case TOK_RESTRICT:
8684 case TOK_VOLATILE:
8685 /* type specifiers */
8686 case TOK_VOID:
8687 case TOK_CHAR:
8688 case TOK_SHORT:
8689 case TOK_INT:
8690 case TOK_LONG:
8691 case TOK_FLOAT:
8692 case TOK_DOUBLE:
8693 case TOK_SIGNED:
8694 case TOK_UNSIGNED:
8695 /* struct or union specifier */
8696 case TOK_STRUCT:
8697 case TOK_UNION:
8698 /* enum-spefifier */
8699 case TOK_ENUM:
8700 /* typedef name */
8701 case TOK_TYPE_NAME:
8702 /* function specifiers */
8703 case TOK_INLINE:
8704 return 1;
8705 default:
8706 return 0;
8707 }
8708}
8709
8710static struct type *decl_specifiers(struct compile_state *state)
8711{
8712 struct type *type;
8713 unsigned int specifiers;
8714 /* I am overly restrictive in the arragement of specifiers supported.
8715 * C is overly flexible in this department it makes interpreting
8716 * the parse tree difficult.
8717 */
8718 specifiers = 0;
8719
8720 /* storage class specifier */
8721 specifiers |= storage_class_specifier_opt(state);
8722
8723 /* function-specifier */
8724 specifiers |= function_specifier_opt(state);
8725
8726 /* type qualifier */
8727 specifiers |= type_qualifiers(state);
8728
8729 /* type specifier */
8730 type = type_specifier(state, specifiers);
8731 return type;
8732}
8733
8734static unsigned designator(struct compile_state *state)
8735{
8736 int tok;
8737 unsigned index;
8738 index = -1U;
8739 do {
8740 switch(peek(state)) {
8741 case TOK_LBRACKET:
8742 {
8743 struct triple *value;
8744 eat(state, TOK_LBRACKET);
8745 value = constant_expr(state);
8746 eat(state, TOK_RBRACKET);
8747 index = value->u.cval;
8748 break;
8749 }
8750 case TOK_DOT:
8751 eat(state, TOK_DOT);
8752 eat(state, TOK_IDENT);
8753 error(state, 0, "Struct Designators not currently supported");
8754 break;
8755 default:
8756 error(state, 0, "Invalid designator");
8757 }
8758 tok = peek(state);
8759 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
8760 eat(state, TOK_EQ);
8761 return index;
8762}
8763
8764static struct triple *initializer(
8765 struct compile_state *state, struct type *type)
8766{
8767 struct triple *result;
8768 if (peek(state) != TOK_LBRACE) {
8769 result = assignment_expr(state);
8770 }
8771 else {
8772 int comma;
8773 unsigned index, max_index;
8774 void *buf;
8775 max_index = index = 0;
8776 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
8777 max_index = type->elements;
8778 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
8779 type->elements = 0;
8780 }
8781 } else {
8782 error(state, 0, "Struct initializers not currently supported");
8783 }
8784 buf = xcmalloc(size_of(state, type), "initializer");
8785 eat(state, TOK_LBRACE);
8786 do {
8787 struct triple *value;
8788 struct type *value_type;
8789 size_t value_size;
8790 int tok;
8791 comma = 0;
8792 tok = peek(state);
8793 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
8794 index = designator(state);
8795 }
8796 if ((max_index != ELEMENT_COUNT_UNSPECIFIED) &&
8797 (index > max_index)) {
8798 error(state, 0, "element beyond bounds");
8799 }
8800 value_type = 0;
8801 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
8802 value_type = type->left;
8803 }
8804 value = eval_const_expr(state, initializer(state, value_type));
8805 value_size = size_of(state, value_type);
8806 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
8807 (max_index == ELEMENT_COUNT_UNSPECIFIED) &&
8808 (type->elements <= index)) {
8809 void *old_buf;
8810 size_t old_size;
8811 old_buf = buf;
8812 old_size = size_of(state, type);
8813 type->elements = index + 1;
8814 buf = xmalloc(size_of(state, type), "initializer");
8815 memcpy(buf, old_buf, old_size);
8816 xfree(old_buf);
8817 }
8818 if (value->op == OP_BLOBCONST) {
8819 memcpy((char *)buf + index * value_size, value->u.blob, value_size);
8820 }
8821 else if ((value->op == OP_INTCONST) && (value_size == 1)) {
8822 *(((uint8_t *)buf) + index) = value->u.cval & 0xff;
8823 }
8824 else if ((value->op == OP_INTCONST) && (value_size == 2)) {
8825 *(((uint16_t *)buf) + index) = value->u.cval & 0xffff;
8826 }
8827 else if ((value->op == OP_INTCONST) && (value_size == 4)) {
8828 *(((uint32_t *)buf) + index) = value->u.cval & 0xffffffff;
8829 }
8830 else {
8831 fprintf(stderr, "%d %d\n",
8832 value->op, value_size);
8833 internal_error(state, 0, "unhandled constant initializer");
8834 }
8835 if (peek(state) == TOK_COMMA) {
8836 eat(state, TOK_COMMA);
8837 comma = 1;
8838 }
8839 index += 1;
8840 } while(comma && (peek(state) != TOK_RBRACE));
8841 eat(state, TOK_RBRACE);
8842 result = triple(state, OP_BLOBCONST, type, 0, 0);
8843 result->u.blob = buf;
8844 }
8845 return result;
8846}
8847
8848static struct triple *function_definition(
8849 struct compile_state *state, struct type *type)
8850{
8851 struct triple *def, *tmp, *first, *end;
8852 struct hash_entry *ident;
8853 struct type *param;
8854 int i;
8855 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
8856 error(state, 0, "Invalid function header");
8857 }
8858
8859 /* Verify the function type */
8860 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
8861 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +00008862 (type->right->field_ident == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008863 error(state, 0, "Invalid function parameters");
8864 }
8865 param = type->right;
8866 i = 0;
8867 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
8868 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008869 if (!param->left->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008870 error(state, 0, "No identifier for parameter %d\n", i);
8871 }
8872 param = param->right;
8873 }
8874 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008875 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008876 error(state, 0, "No identifier for paramter %d\n", i);
8877 }
8878
8879 /* Get a list of statements for this function. */
8880 def = triple(state, OP_LIST, type, 0, 0);
8881
8882 /* Start a new scope for the passed parameters */
8883 start_scope(state);
8884
8885 /* Put a label at the very start of a function */
8886 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008887 RHS(def, 0) = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008888
8889 /* Put a label at the very end of a function */
8890 end = label(state);
8891 flatten(state, first, end);
8892
8893 /* Walk through the parameters and create symbol table entries
8894 * for them.
8895 */
8896 param = type->right;
8897 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008898 ident = param->left->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008899 tmp = variable(state, param->left);
8900 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
8901 flatten(state, end, tmp);
8902 param = param->right;
8903 }
8904 if ((param->type & TYPE_MASK) != TYPE_VOID) {
8905 /* And don't forget the last parameter */
Eric Biederman0babc1c2003-05-09 02:39:00 +00008906 ident = param->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008907 tmp = variable(state, param);
8908 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
8909 flatten(state, end, tmp);
8910 }
8911 /* Add a variable for the return value */
Eric Biederman0babc1c2003-05-09 02:39:00 +00008912 MISC(def, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008913 if ((type->left->type & TYPE_MASK) != TYPE_VOID) {
8914 /* Remove all type qualifiers from the return type */
8915 tmp = variable(state, clone_type(0, type->left));
8916 flatten(state, end, tmp);
8917 /* Remember where the return value is */
Eric Biederman0babc1c2003-05-09 02:39:00 +00008918 MISC(def, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008919 }
8920
8921 /* Remember which function I am compiling.
8922 * Also assume the last defined function is the main function.
8923 */
8924 state->main_function = def;
8925
8926 /* Now get the actual function definition */
8927 compound_statement(state, end);
8928
8929 /* Remove the parameter scope */
8930 end_scope(state);
8931#if 0
8932 fprintf(stdout, "\n");
8933 loc(stdout, state, 0);
8934 fprintf(stdout, "\n__________ function_definition _________\n");
8935 print_triple(state, def);
8936 fprintf(stdout, "__________ function_definition _________ done\n\n");
8937#endif
8938
8939 return def;
8940}
8941
8942static struct triple *do_decl(struct compile_state *state,
8943 struct type *type, struct hash_entry *ident)
8944{
8945 struct triple *def;
8946 def = 0;
8947 /* Clean up the storage types used */
8948 switch (type->type & STOR_MASK) {
8949 case STOR_AUTO:
8950 case STOR_STATIC:
8951 /* These are the good types I am aiming for */
8952 break;
8953 case STOR_REGISTER:
8954 type->type &= ~STOR_MASK;
8955 type->type |= STOR_AUTO;
8956 break;
8957 case STOR_EXTERN:
8958 type->type &= ~STOR_MASK;
8959 type->type |= STOR_STATIC;
8960 break;
8961 case STOR_TYPEDEF:
Eric Biederman0babc1c2003-05-09 02:39:00 +00008962 if (!ident) {
8963 error(state, 0, "typedef without name");
8964 }
8965 symbol(state, ident, &ident->sym_ident, 0, type);
8966 ident->tok = TOK_TYPE_NAME;
8967 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008968 break;
8969 default:
8970 internal_error(state, 0, "Undefined storage class");
8971 }
8972 if (((type->type & STOR_MASK) == STOR_STATIC) &&
8973 ((type->type & QUAL_CONST) == 0)) {
8974 error(state, 0, "non const static variables not supported");
8975 }
8976 if (ident) {
8977 def = variable(state, type);
8978 symbol(state, ident, &ident->sym_ident, def, type);
8979 }
8980 return def;
8981}
8982
8983static void decl(struct compile_state *state, struct triple *first)
8984{
8985 struct type *base_type, *type;
8986 struct hash_entry *ident;
8987 struct triple *def;
8988 int global;
8989 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
8990 base_type = decl_specifiers(state);
8991 ident = 0;
8992 type = declarator(state, base_type, &ident, 0);
8993 if (global && ident && (peek(state) == TOK_LBRACE)) {
8994 /* function */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00008995 state->function = ident->name;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008996 def = function_definition(state, type);
8997 symbol(state, ident, &ident->sym_ident, def, type);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00008998 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008999 }
9000 else {
9001 int done;
9002 flatten(state, first, do_decl(state, type, ident));
9003 /* type or variable definition */
9004 do {
9005 done = 1;
9006 if (peek(state) == TOK_EQ) {
9007 if (!ident) {
9008 error(state, 0, "cannot assign to a type");
9009 }
9010 eat(state, TOK_EQ);
9011 flatten(state, first,
9012 init_expr(state,
9013 ident->sym_ident->def,
9014 initializer(state, type)));
9015 }
9016 arrays_complete(state, type);
9017 if (peek(state) == TOK_COMMA) {
9018 eat(state, TOK_COMMA);
9019 ident = 0;
9020 type = declarator(state, base_type, &ident, 0);
9021 flatten(state, first, do_decl(state, type, ident));
9022 done = 0;
9023 }
9024 } while(!done);
9025 eat(state, TOK_SEMI);
9026 }
9027}
9028
9029static void decls(struct compile_state *state)
9030{
9031 struct triple *list;
9032 int tok;
9033 list = label(state);
9034 while(1) {
9035 tok = peek(state);
9036 if (tok == TOK_EOF) {
9037 return;
9038 }
9039 if (tok == TOK_SPACE) {
9040 eat(state, TOK_SPACE);
9041 }
9042 decl(state, list);
9043 if (list->next != list) {
9044 error(state, 0, "global variables not supported");
9045 }
9046 }
9047}
9048
9049/*
9050 * Data structurs for optimation.
9051 */
9052
9053static void do_use_block(
9054 struct block *used, struct block_set **head, struct block *user,
9055 int front)
9056{
9057 struct block_set **ptr, *new;
9058 if (!used)
9059 return;
9060 if (!user)
9061 return;
9062 ptr = head;
9063 while(*ptr) {
9064 if ((*ptr)->member == user) {
9065 return;
9066 }
9067 ptr = &(*ptr)->next;
9068 }
9069 new = xcmalloc(sizeof(*new), "block_set");
9070 new->member = user;
9071 if (front) {
9072 new->next = *head;
9073 *head = new;
9074 }
9075 else {
9076 new->next = 0;
9077 *ptr = new;
9078 }
9079}
9080static void do_unuse_block(
9081 struct block *used, struct block_set **head, struct block *unuser)
9082{
9083 struct block_set *use, **ptr;
9084 ptr = head;
9085 while(*ptr) {
9086 use = *ptr;
9087 if (use->member == unuser) {
9088 *ptr = use->next;
9089 memset(use, -1, sizeof(*use));
9090 xfree(use);
9091 }
9092 else {
9093 ptr = &use->next;
9094 }
9095 }
9096}
9097
9098static void use_block(struct block *used, struct block *user)
9099{
9100 /* Append new to the head of the list, print_block
9101 * depends on this.
9102 */
9103 do_use_block(used, &used->use, user, 1);
9104 used->users++;
9105}
9106static void unuse_block(struct block *used, struct block *unuser)
9107{
9108 do_unuse_block(used, &used->use, unuser);
9109 used->users--;
9110}
9111
9112static void idom_block(struct block *idom, struct block *user)
9113{
9114 do_use_block(idom, &idom->idominates, user, 0);
9115}
9116
9117static void unidom_block(struct block *idom, struct block *unuser)
9118{
9119 do_unuse_block(idom, &idom->idominates, unuser);
9120}
9121
9122static void domf_block(struct block *block, struct block *domf)
9123{
9124 do_use_block(block, &block->domfrontier, domf, 0);
9125}
9126
9127static void undomf_block(struct block *block, struct block *undomf)
9128{
9129 do_unuse_block(block, &block->domfrontier, undomf);
9130}
9131
9132static void ipdom_block(struct block *ipdom, struct block *user)
9133{
9134 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
9135}
9136
9137static void unipdom_block(struct block *ipdom, struct block *unuser)
9138{
9139 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
9140}
9141
9142static void ipdomf_block(struct block *block, struct block *ipdomf)
9143{
9144 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
9145}
9146
9147static void unipdomf_block(struct block *block, struct block *unipdomf)
9148{
9149 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
9150}
9151
9152
9153
9154static int do_walk_triple(struct compile_state *state,
9155 struct triple *ptr, int depth,
9156 int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
9157{
9158 int result;
9159 result = cb(state, ptr, depth);
9160 if ((result == 0) && (ptr->op == OP_LIST)) {
9161 struct triple *list;
9162 list = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009163 ptr = RHS(list, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009164 do {
9165 result = do_walk_triple(state, ptr, depth + 1, cb);
9166 if (ptr->next->prev != ptr) {
9167 internal_error(state, ptr->next, "bad prev");
9168 }
9169 ptr = ptr->next;
9170
Eric Biederman0babc1c2003-05-09 02:39:00 +00009171 } while((result == 0) && (ptr != RHS(list, 0)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009172 }
9173 return result;
9174}
9175
9176static int walk_triple(
9177 struct compile_state *state,
9178 struct triple *ptr,
9179 int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
9180{
9181 return do_walk_triple(state, ptr, 0, cb);
9182}
9183
9184static void do_print_prefix(int depth)
9185{
9186 int i;
9187 for(i = 0; i < depth; i++) {
9188 printf(" ");
9189 }
9190}
9191
9192#define PRINT_LIST 1
9193static int do_print_triple(struct compile_state *state, struct triple *ins, int depth)
9194{
9195 int op;
9196 op = ins->op;
9197 if (op == OP_LIST) {
9198#if !PRINT_LIST
9199 return 0;
9200#endif
9201 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009202 if ((op == OP_LABEL) && (ins->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009203 printf("\n%p:\n", ins);
9204 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009205 do_print_prefix(depth);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009206 display_triple(stdout, ins);
9207
Eric Biedermanb138ac82003-04-22 18:44:01 +00009208 if ((ins->op == OP_BRANCH) && ins->use) {
9209 internal_error(state, ins, "branch used?");
9210 }
9211#if 0
9212 {
9213 struct triple_set *user;
9214 for(user = ins->use; user; user = user->next) {
9215 printf("use: %p\n", user->member);
9216 }
9217 }
9218#endif
Eric Biederman0babc1c2003-05-09 02:39:00 +00009219 if (triple_is_branch(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009220 printf("\n");
9221 }
9222 return 0;
9223}
9224
9225static void print_triple(struct compile_state *state, struct triple *ins)
9226{
9227 walk_triple(state, ins, do_print_triple);
9228}
9229
9230static void print_triples(struct compile_state *state)
9231{
9232 print_triple(state, state->main_function);
9233}
9234
9235struct cf_block {
9236 struct block *block;
9237};
9238static void find_cf_blocks(struct cf_block *cf, struct block *block)
9239{
9240 if (!block || (cf[block->vertex].block == block)) {
9241 return;
9242 }
9243 cf[block->vertex].block = block;
9244 find_cf_blocks(cf, block->left);
9245 find_cf_blocks(cf, block->right);
9246}
9247
9248static void print_control_flow(struct compile_state *state)
9249{
9250 struct cf_block *cf;
9251 int i;
9252 printf("\ncontrol flow\n");
9253 cf = xcmalloc(sizeof(*cf) * (state->last_vertex + 1), "cf_block");
9254 find_cf_blocks(cf, state->first_block);
9255
9256 for(i = 1; i <= state->last_vertex; i++) {
9257 struct block *block;
9258 block = cf[i].block;
9259 if (!block)
9260 continue;
9261 printf("(%p) %d:", block, block->vertex);
9262 if (block->left) {
9263 printf(" %d", block->left->vertex);
9264 }
9265 if (block->right && (block->right != block->left)) {
9266 printf(" %d", block->right->vertex);
9267 }
9268 printf("\n");
9269 }
9270
9271 xfree(cf);
9272}
9273
9274
9275static struct block *basic_block(struct compile_state *state,
9276 struct triple *first)
9277{
9278 struct block *block;
9279 struct triple *ptr;
9280 int op;
9281 if (first->op != OP_LABEL) {
9282 internal_error(state, 0, "block does not start with a label");
9283 }
9284 /* See if this basic block has already been setup */
9285 if (first->u.block != 0) {
9286 return first->u.block;
9287 }
9288 /* Allocate another basic block structure */
9289 state->last_vertex += 1;
9290 block = xcmalloc(sizeof(*block), "block");
9291 block->first = block->last = first;
9292 block->vertex = state->last_vertex;
9293 ptr = first;
9294 do {
9295 if ((ptr != first) && (ptr->op == OP_LABEL) && ptr->use) {
9296 break;
9297 }
9298 block->last = ptr;
9299 /* If ptr->u is not used remember where the baic block is */
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009300 if (triple_stores_block(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009301 ptr->u.block = block;
9302 }
9303 if (ptr->op == OP_BRANCH) {
9304 break;
9305 }
9306 ptr = ptr->next;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009307 } while (ptr != RHS(state->main_function, 0));
9308 if (ptr == RHS(state->main_function, 0))
Eric Biedermanb138ac82003-04-22 18:44:01 +00009309 return block;
9310 op = ptr->op;
9311 if (op == OP_LABEL) {
9312 block->left = basic_block(state, ptr);
9313 block->right = 0;
9314 use_block(block->left, block);
9315 }
9316 else if (op == OP_BRANCH) {
9317 block->left = 0;
9318 /* Trace the branch target */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009319 block->right = basic_block(state, TARG(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009320 use_block(block->right, block);
9321 /* If there is a test trace the branch as well */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009322 if (TRIPLE_RHS(ptr->sizes)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009323 block->left = basic_block(state, ptr->next);
9324 use_block(block->left, block);
9325 }
9326 }
9327 else {
9328 internal_error(state, 0, "Bad basic block split");
9329 }
9330 return block;
9331}
9332
9333
9334static void walk_blocks(struct compile_state *state,
9335 void (*cb)(struct compile_state *state, struct block *block, void *arg),
9336 void *arg)
9337{
9338 struct triple *ptr, *first;
9339 struct block *last_block;
9340 last_block = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009341 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009342 ptr = first;
9343 do {
9344 struct block *block;
9345 if (ptr->op == OP_LABEL) {
9346 block = ptr->u.block;
9347 if (block && (block != last_block)) {
9348 cb(state, block, arg);
9349 }
9350 last_block = block;
9351 }
9352 ptr = ptr->next;
9353 } while(ptr != first);
9354}
9355
9356static void print_block(
9357 struct compile_state *state, struct block *block, void *arg)
9358{
9359 struct triple *ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009360 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009361
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009362 fprintf(fp, "\nblock: %p (%d), %p<-%p %p<-%p\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +00009363 block,
9364 block->vertex,
9365 block->left,
9366 block->left && block->left->use?block->left->use->member : 0,
9367 block->right,
9368 block->right && block->right->use?block->right->use->member : 0);
9369 if (block->first->op == OP_LABEL) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009370 fprintf(fp, "%p:\n", block->first);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009371 }
9372 for(ptr = block->first; ; ptr = ptr->next) {
9373 struct triple_set *user;
9374 int op = ptr->op;
9375
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009376 if (triple_stores_block(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009377 if (ptr->u.block != block) {
9378 internal_error(state, ptr,
9379 "Wrong block pointer: %p\n",
9380 ptr->u.block);
9381 }
9382 }
9383 if (op == OP_ADECL) {
9384 for(user = ptr->use; user; user = user->next) {
9385 if (!user->member->u.block) {
9386 internal_error(state, user->member,
9387 "Use %p not in a block?\n",
9388 user->member);
9389 }
9390 }
9391 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009392 display_triple(fp, ptr);
9393
9394#if 0
9395 for(user = ptr->use; user; user = user->next) {
9396 fprintf(fp, "use: %p\n", user->member);
9397 }
9398#endif
Eric Biederman0babc1c2003-05-09 02:39:00 +00009399
Eric Biedermanb138ac82003-04-22 18:44:01 +00009400 /* Sanity checks... */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009401 valid_ins(state, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009402 for(user = ptr->use; user; user = user->next) {
9403 struct triple *use;
9404 use = user->member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009405 valid_ins(state, use);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009406 if (triple_stores_block(state, user->member) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00009407 !user->member->u.block) {
9408 internal_error(state, user->member,
9409 "Use %p not in a block?",
9410 user->member);
9411 }
9412 }
9413
9414 if (ptr == block->last)
9415 break;
9416 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009417 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00009418}
9419
9420
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009421static void print_blocks(struct compile_state *state, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +00009422{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009423 fprintf(fp, "--------------- blocks ---------------\n");
9424 walk_blocks(state, print_block, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009425}
9426
9427static void prune_nonblock_triples(struct compile_state *state)
9428{
9429 struct block *block;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009430 struct triple *first, *ins, *next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009431 /* Delete the triples not in a basic block */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009432 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009433 block = 0;
9434 ins = first;
9435 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009436 next = ins->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009437 if (ins->op == OP_LABEL) {
9438 block = ins->u.block;
9439 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009440 if (!block) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009441 release_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009442 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009443 ins = next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009444 } while(ins != first);
9445}
9446
9447static void setup_basic_blocks(struct compile_state *state)
9448{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009449 if (!triple_stores_block(state, RHS(state->main_function, 0)) ||
9450 !triple_stores_block(state, RHS(state->main_function,0)->prev)) {
9451 internal_error(state, 0, "ins will not store block?");
9452 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009453 /* Find the basic blocks */
9454 state->last_vertex = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009455 state->first_block = basic_block(state, RHS(state->main_function,0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009456 /* Delete the triples not in a basic block */
9457 prune_nonblock_triples(state);
9458 /* Find the last basic block */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009459 state->last_block = RHS(state->main_function, 0)->prev->u.block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009460 if (!state->last_block) {
9461 internal_error(state, 0, "end not used?");
9462 }
9463 /* Insert an extra unused edge from start to the end
9464 * This helps with reverse control flow calculations.
9465 */
9466 use_block(state->first_block, state->last_block);
9467 /* If we are debugging print what I have just done */
9468 if (state->debug & DEBUG_BASIC_BLOCKS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009469 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009470 print_control_flow(state);
9471 }
9472}
9473
9474static void free_basic_block(struct compile_state *state, struct block *block)
9475{
9476 struct block_set *entry, *next;
9477 struct block *child;
9478 if (!block) {
9479 return;
9480 }
9481 if (block->vertex == -1) {
9482 return;
9483 }
9484 block->vertex = -1;
9485 if (block->left) {
9486 unuse_block(block->left, block);
9487 }
9488 if (block->right) {
9489 unuse_block(block->right, block);
9490 }
9491 if (block->idom) {
9492 unidom_block(block->idom, block);
9493 }
9494 block->idom = 0;
9495 if (block->ipdom) {
9496 unipdom_block(block->ipdom, block);
9497 }
9498 block->ipdom = 0;
9499 for(entry = block->use; entry; entry = next) {
9500 next = entry->next;
9501 child = entry->member;
9502 unuse_block(block, child);
9503 if (child->left == block) {
9504 child->left = 0;
9505 }
9506 if (child->right == block) {
9507 child->right = 0;
9508 }
9509 }
9510 for(entry = block->idominates; entry; entry = next) {
9511 next = entry->next;
9512 child = entry->member;
9513 unidom_block(block, child);
9514 child->idom = 0;
9515 }
9516 for(entry = block->domfrontier; entry; entry = next) {
9517 next = entry->next;
9518 child = entry->member;
9519 undomf_block(block, child);
9520 }
9521 for(entry = block->ipdominates; entry; entry = next) {
9522 next = entry->next;
9523 child = entry->member;
9524 unipdom_block(block, child);
9525 child->ipdom = 0;
9526 }
9527 for(entry = block->ipdomfrontier; entry; entry = next) {
9528 next = entry->next;
9529 child = entry->member;
9530 unipdomf_block(block, child);
9531 }
9532 if (block->users != 0) {
9533 internal_error(state, 0, "block still has users");
9534 }
9535 free_basic_block(state, block->left);
9536 block->left = 0;
9537 free_basic_block(state, block->right);
9538 block->right = 0;
9539 memset(block, -1, sizeof(*block));
9540 xfree(block);
9541}
9542
9543static void free_basic_blocks(struct compile_state *state)
9544{
9545 struct triple *first, *ins;
9546 free_basic_block(state, state->first_block);
9547 state->last_vertex = 0;
9548 state->first_block = state->last_block = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009549 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009550 ins = first;
9551 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009552 if (triple_stores_block(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009553 ins->u.block = 0;
9554 }
9555 ins = ins->next;
9556 } while(ins != first);
9557
9558}
9559
9560struct sdom_block {
9561 struct block *block;
9562 struct sdom_block *sdominates;
9563 struct sdom_block *sdom_next;
9564 struct sdom_block *sdom;
9565 struct sdom_block *label;
9566 struct sdom_block *parent;
9567 struct sdom_block *ancestor;
9568 int vertex;
9569};
9570
9571
9572static void unsdom_block(struct sdom_block *block)
9573{
9574 struct sdom_block **ptr;
9575 if (!block->sdom_next) {
9576 return;
9577 }
9578 ptr = &block->sdom->sdominates;
9579 while(*ptr) {
9580 if ((*ptr) == block) {
9581 *ptr = block->sdom_next;
9582 return;
9583 }
9584 ptr = &(*ptr)->sdom_next;
9585 }
9586}
9587
9588static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
9589{
9590 unsdom_block(block);
9591 block->sdom = sdom;
9592 block->sdom_next = sdom->sdominates;
9593 sdom->sdominates = block;
9594}
9595
9596
9597
9598static int initialize_sdblock(struct sdom_block *sd,
9599 struct block *parent, struct block *block, int vertex)
9600{
9601 if (!block || (sd[block->vertex].block == block)) {
9602 return vertex;
9603 }
9604 vertex += 1;
9605 /* Renumber the blocks in a convinient fashion */
9606 block->vertex = vertex;
9607 sd[vertex].block = block;
9608 sd[vertex].sdom = &sd[vertex];
9609 sd[vertex].label = &sd[vertex];
9610 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
9611 sd[vertex].ancestor = 0;
9612 sd[vertex].vertex = vertex;
9613 vertex = initialize_sdblock(sd, block, block->left, vertex);
9614 vertex = initialize_sdblock(sd, block, block->right, vertex);
9615 return vertex;
9616}
9617
9618static int initialize_sdpblock(struct sdom_block *sd,
9619 struct block *parent, struct block *block, int vertex)
9620{
9621 struct block_set *user;
9622 if (!block || (sd[block->vertex].block == block)) {
9623 return vertex;
9624 }
9625 vertex += 1;
9626 /* Renumber the blocks in a convinient fashion */
9627 block->vertex = vertex;
9628 sd[vertex].block = block;
9629 sd[vertex].sdom = &sd[vertex];
9630 sd[vertex].label = &sd[vertex];
9631 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
9632 sd[vertex].ancestor = 0;
9633 sd[vertex].vertex = vertex;
9634 for(user = block->use; user; user = user->next) {
9635 vertex = initialize_sdpblock(sd, block, user->member, vertex);
9636 }
9637 return vertex;
9638}
9639
9640static void compress_ancestors(struct sdom_block *v)
9641{
9642 /* This procedure assumes ancestor(v) != 0 */
9643 /* if (ancestor(ancestor(v)) != 0) {
9644 * compress(ancestor(ancestor(v)));
9645 * if (semi(label(ancestor(v))) < semi(label(v))) {
9646 * label(v) = label(ancestor(v));
9647 * }
9648 * ancestor(v) = ancestor(ancestor(v));
9649 * }
9650 */
9651 if (!v->ancestor) {
9652 return;
9653 }
9654 if (v->ancestor->ancestor) {
9655 compress_ancestors(v->ancestor->ancestor);
9656 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
9657 v->label = v->ancestor->label;
9658 }
9659 v->ancestor = v->ancestor->ancestor;
9660 }
9661}
9662
9663static void compute_sdom(struct compile_state *state, struct sdom_block *sd)
9664{
9665 int i;
9666 /* // step 2
9667 * for each v <= pred(w) {
9668 * u = EVAL(v);
9669 * if (semi[u] < semi[w] {
9670 * semi[w] = semi[u];
9671 * }
9672 * }
9673 * add w to bucket(vertex(semi[w]));
9674 * LINK(parent(w), w);
9675 *
9676 * // step 3
9677 * for each v <= bucket(parent(w)) {
9678 * delete v from bucket(parent(w));
9679 * u = EVAL(v);
9680 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
9681 * }
9682 */
9683 for(i = state->last_vertex; i >= 2; i--) {
9684 struct sdom_block *v, *parent, *next;
9685 struct block_set *user;
9686 struct block *block;
9687 block = sd[i].block;
9688 parent = sd[i].parent;
9689 /* Step 2 */
9690 for(user = block->use; user; user = user->next) {
9691 struct sdom_block *v, *u;
9692 v = &sd[user->member->vertex];
9693 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9694 if (u->sdom->vertex < sd[i].sdom->vertex) {
9695 sd[i].sdom = u->sdom;
9696 }
9697 }
9698 sdom_block(sd[i].sdom, &sd[i]);
9699 sd[i].ancestor = parent;
9700 /* Step 3 */
9701 for(v = parent->sdominates; v; v = next) {
9702 struct sdom_block *u;
9703 next = v->sdom_next;
9704 unsdom_block(v);
9705 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
9706 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
9707 u->block : parent->block;
9708 }
9709 }
9710}
9711
9712static void compute_spdom(struct compile_state *state, struct sdom_block *sd)
9713{
9714 int i;
9715 /* // step 2
9716 * for each v <= pred(w) {
9717 * u = EVAL(v);
9718 * if (semi[u] < semi[w] {
9719 * semi[w] = semi[u];
9720 * }
9721 * }
9722 * add w to bucket(vertex(semi[w]));
9723 * LINK(parent(w), w);
9724 *
9725 * // step 3
9726 * for each v <= bucket(parent(w)) {
9727 * delete v from bucket(parent(w));
9728 * u = EVAL(v);
9729 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
9730 * }
9731 */
9732 for(i = state->last_vertex; i >= 2; i--) {
9733 struct sdom_block *u, *v, *parent, *next;
9734 struct block *block;
9735 block = sd[i].block;
9736 parent = sd[i].parent;
9737 /* Step 2 */
9738 if (block->left) {
9739 v = &sd[block->left->vertex];
9740 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9741 if (u->sdom->vertex < sd[i].sdom->vertex) {
9742 sd[i].sdom = u->sdom;
9743 }
9744 }
9745 if (block->right && (block->right != block->left)) {
9746 v = &sd[block->right->vertex];
9747 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9748 if (u->sdom->vertex < sd[i].sdom->vertex) {
9749 sd[i].sdom = u->sdom;
9750 }
9751 }
9752 sdom_block(sd[i].sdom, &sd[i]);
9753 sd[i].ancestor = parent;
9754 /* Step 3 */
9755 for(v = parent->sdominates; v; v = next) {
9756 struct sdom_block *u;
9757 next = v->sdom_next;
9758 unsdom_block(v);
9759 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
9760 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
9761 u->block : parent->block;
9762 }
9763 }
9764}
9765
9766static void compute_idom(struct compile_state *state, struct sdom_block *sd)
9767{
9768 int i;
9769 for(i = 2; i <= state->last_vertex; i++) {
9770 struct block *block;
9771 block = sd[i].block;
9772 if (block->idom->vertex != sd[i].sdom->vertex) {
9773 block->idom = block->idom->idom;
9774 }
9775 idom_block(block->idom, block);
9776 }
9777 sd[1].block->idom = 0;
9778}
9779
9780static void compute_ipdom(struct compile_state *state, struct sdom_block *sd)
9781{
9782 int i;
9783 for(i = 2; i <= state->last_vertex; i++) {
9784 struct block *block;
9785 block = sd[i].block;
9786 if (block->ipdom->vertex != sd[i].sdom->vertex) {
9787 block->ipdom = block->ipdom->ipdom;
9788 }
9789 ipdom_block(block->ipdom, block);
9790 }
9791 sd[1].block->ipdom = 0;
9792}
9793
9794 /* Theorem 1:
9795 * Every vertex of a flowgraph G = (V, E, r) except r has
9796 * a unique immediate dominator.
9797 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
9798 * rooted at r, called the dominator tree of G, such that
9799 * v dominates w if and only if v is a proper ancestor of w in
9800 * the dominator tree.
9801 */
9802 /* Lemma 1:
9803 * If v and w are vertices of G such that v <= w,
9804 * than any path from v to w must contain a common ancestor
9805 * of v and w in T.
9806 */
9807 /* Lemma 2: For any vertex w != r, idom(w) -> w */
9808 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
9809 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
9810 /* Theorem 2:
9811 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
9812 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
9813 */
9814 /* Theorem 3:
9815 * Let w != r and let u be a vertex for which sdom(u) is
9816 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
9817 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
9818 */
9819 /* Lemma 5: Let vertices v,w satisfy v -> w.
9820 * Then v -> idom(w) or idom(w) -> idom(v)
9821 */
9822
9823static void find_immediate_dominators(struct compile_state *state)
9824{
9825 struct sdom_block *sd;
9826 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
9827 * vi > w for (1 <= i <= k - 1}
9828 */
9829 /* Theorem 4:
9830 * For any vertex w != r.
9831 * sdom(w) = min(
9832 * {v|(v,w) <= E and v < w } U
9833 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
9834 */
9835 /* Corollary 1:
9836 * Let w != r and let u be a vertex for which sdom(u) is
9837 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
9838 * Then:
9839 * { sdom(w) if sdom(w) = sdom(u),
9840 * idom(w) = {
9841 * { idom(u) otherwise
9842 */
9843 /* The algorithm consists of the following 4 steps.
9844 * Step 1. Carry out a depth-first search of the problem graph.
9845 * Number the vertices from 1 to N as they are reached during
9846 * the search. Initialize the variables used in succeeding steps.
9847 * Step 2. Compute the semidominators of all vertices by applying
9848 * theorem 4. Carry out the computation vertex by vertex in
9849 * decreasing order by number.
9850 * Step 3. Implicitly define the immediate dominator of each vertex
9851 * by applying Corollary 1.
9852 * Step 4. Explicitly define the immediate dominator of each vertex,
9853 * carrying out the computation vertex by vertex in increasing order
9854 * by number.
9855 */
9856 /* Step 1 initialize the basic block information */
9857 sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
9858 initialize_sdblock(sd, 0, state->first_block, 0);
9859#if 0
9860 sd[1].size = 0;
9861 sd[1].label = 0;
9862 sd[1].sdom = 0;
9863#endif
9864 /* Step 2 compute the semidominators */
9865 /* Step 3 implicitly define the immediate dominator of each vertex */
9866 compute_sdom(state, sd);
9867 /* Step 4 explicitly define the immediate dominator of each vertex */
9868 compute_idom(state, sd);
9869 xfree(sd);
9870}
9871
9872static void find_post_dominators(struct compile_state *state)
9873{
9874 struct sdom_block *sd;
9875 /* Step 1 initialize the basic block information */
9876 sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
9877
9878 initialize_sdpblock(sd, 0, state->last_block, 0);
9879
9880 /* Step 2 compute the semidominators */
9881 /* Step 3 implicitly define the immediate dominator of each vertex */
9882 compute_spdom(state, sd);
9883 /* Step 4 explicitly define the immediate dominator of each vertex */
9884 compute_ipdom(state, sd);
9885 xfree(sd);
9886}
9887
9888
9889
9890static void find_block_domf(struct compile_state *state, struct block *block)
9891{
9892 struct block *child;
9893 struct block_set *user;
9894 if (block->domfrontier != 0) {
9895 internal_error(state, block->first, "domfrontier present?");
9896 }
9897 for(user = block->idominates; user; user = user->next) {
9898 child = user->member;
9899 if (child->idom != block) {
9900 internal_error(state, block->first, "bad idom");
9901 }
9902 find_block_domf(state, child);
9903 }
9904 if (block->left && block->left->idom != block) {
9905 domf_block(block, block->left);
9906 }
9907 if (block->right && block->right->idom != block) {
9908 domf_block(block, block->right);
9909 }
9910 for(user = block->idominates; user; user = user->next) {
9911 struct block_set *frontier;
9912 child = user->member;
9913 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
9914 if (frontier->member->idom != block) {
9915 domf_block(block, frontier->member);
9916 }
9917 }
9918 }
9919}
9920
9921static void find_block_ipdomf(struct compile_state *state, struct block *block)
9922{
9923 struct block *child;
9924 struct block_set *user;
9925 if (block->ipdomfrontier != 0) {
9926 internal_error(state, block->first, "ipdomfrontier present?");
9927 }
9928 for(user = block->ipdominates; user; user = user->next) {
9929 child = user->member;
9930 if (child->ipdom != block) {
9931 internal_error(state, block->first, "bad ipdom");
9932 }
9933 find_block_ipdomf(state, child);
9934 }
9935 if (block->left && block->left->ipdom != block) {
9936 ipdomf_block(block, block->left);
9937 }
9938 if (block->right && block->right->ipdom != block) {
9939 ipdomf_block(block, block->right);
9940 }
9941 for(user = block->idominates; user; user = user->next) {
9942 struct block_set *frontier;
9943 child = user->member;
9944 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
9945 if (frontier->member->ipdom != block) {
9946 ipdomf_block(block, frontier->member);
9947 }
9948 }
9949 }
9950}
9951
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009952static void print_dominated(
9953 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +00009954{
9955 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009956 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009957
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009958 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009959 for(user = block->idominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009960 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009961 if (user->member->idom != block) {
9962 internal_error(state, user->member->first, "bad idom");
9963 }
9964 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009965 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00009966}
9967
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009968static void print_dominators(struct compile_state *state, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +00009969{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009970 fprintf(fp, "\ndominates\n");
9971 walk_blocks(state, print_dominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009972}
9973
9974
9975static int print_frontiers(
9976 struct compile_state *state, struct block *block, int vertex)
9977{
9978 struct block_set *user;
9979
9980 if (!block || (block->vertex != vertex + 1)) {
9981 return vertex;
9982 }
9983 vertex += 1;
9984
9985 printf("%d:", block->vertex);
9986 for(user = block->domfrontier; user; user = user->next) {
9987 printf(" %d", user->member->vertex);
9988 }
9989 printf("\n");
9990
9991 vertex = print_frontiers(state, block->left, vertex);
9992 vertex = print_frontiers(state, block->right, vertex);
9993 return vertex;
9994}
9995static void print_dominance_frontiers(struct compile_state *state)
9996{
9997 printf("\ndominance frontiers\n");
9998 print_frontiers(state, state->first_block, 0);
9999
10000}
10001
10002static void analyze_idominators(struct compile_state *state)
10003{
10004 /* Find the immediate dominators */
10005 find_immediate_dominators(state);
10006 /* Find the dominance frontiers */
10007 find_block_domf(state, state->first_block);
10008 /* If debuging print the print what I have just found */
10009 if (state->debug & DEBUG_FDOMINATORS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010010 print_dominators(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010011 print_dominance_frontiers(state);
10012 print_control_flow(state);
10013 }
10014}
10015
10016
10017
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010018static void print_ipdominated(
10019 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010020{
10021 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010022 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010023
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010024 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010025 for(user = block->ipdominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010026 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010027 if (user->member->ipdom != block) {
10028 internal_error(state, user->member->first, "bad ipdom");
10029 }
10030 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010031 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010032}
10033
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010034static void print_ipdominators(struct compile_state *state, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010035{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010036 fprintf(fp, "\nipdominates\n");
10037 walk_blocks(state, print_ipdominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010038}
10039
10040static int print_pfrontiers(
10041 struct compile_state *state, struct block *block, int vertex)
10042{
10043 struct block_set *user;
10044
10045 if (!block || (block->vertex != vertex + 1)) {
10046 return vertex;
10047 }
10048 vertex += 1;
10049
10050 printf("%d:", block->vertex);
10051 for(user = block->ipdomfrontier; user; user = user->next) {
10052 printf(" %d", user->member->vertex);
10053 }
10054 printf("\n");
10055 for(user = block->use; user; user = user->next) {
10056 vertex = print_pfrontiers(state, user->member, vertex);
10057 }
10058 return vertex;
10059}
10060static void print_ipdominance_frontiers(struct compile_state *state)
10061{
10062 printf("\nipdominance frontiers\n");
10063 print_pfrontiers(state, state->last_block, 0);
10064
10065}
10066
10067static void analyze_ipdominators(struct compile_state *state)
10068{
10069 /* Find the post dominators */
10070 find_post_dominators(state);
10071 /* Find the control dependencies (post dominance frontiers) */
10072 find_block_ipdomf(state, state->last_block);
10073 /* If debuging print the print what I have just found */
10074 if (state->debug & DEBUG_RDOMINATORS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010075 print_ipdominators(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010076 print_ipdominance_frontiers(state);
10077 print_control_flow(state);
10078 }
10079}
10080
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010081static int bdominates(struct compile_state *state,
10082 struct block *dom, struct block *sub)
10083{
10084 while(sub && (sub != dom)) {
10085 sub = sub->idom;
10086 }
10087 return sub == dom;
10088}
10089
10090static int tdominates(struct compile_state *state,
10091 struct triple *dom, struct triple *sub)
10092{
10093 struct block *bdom, *bsub;
10094 int result;
10095 bdom = block_of_triple(state, dom);
10096 bsub = block_of_triple(state, sub);
10097 if (bdom != bsub) {
10098 result = bdominates(state, bdom, bsub);
10099 }
10100 else {
10101 struct triple *ins;
10102 ins = sub;
10103 while((ins != bsub->first) && (ins != dom)) {
10104 ins = ins->prev;
10105 }
10106 result = (ins == dom);
10107 }
10108 return result;
10109}
10110
Eric Biedermanb138ac82003-04-22 18:44:01 +000010111static void insert_phi_operations(struct compile_state *state)
10112{
10113 size_t size;
10114 struct triple *first;
10115 int *has_already, *work;
10116 struct block *work_list, **work_list_tail;
10117 int iter;
10118 struct triple *var;
10119
10120 size = sizeof(int) * (state->last_vertex + 1);
10121 has_already = xcmalloc(size, "has_already");
10122 work = xcmalloc(size, "work");
10123 iter = 0;
10124
Eric Biederman0babc1c2003-05-09 02:39:00 +000010125 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010126 for(var = first->next; var != first ; var = var->next) {
10127 struct block *block;
10128 struct triple_set *user;
10129 if ((var->op != OP_ADECL) || !var->use) {
10130 continue;
10131 }
10132 iter += 1;
10133 work_list = 0;
10134 work_list_tail = &work_list;
10135 for(user = var->use; user; user = user->next) {
10136 if (user->member->op == OP_READ) {
10137 continue;
10138 }
10139 if (user->member->op != OP_WRITE) {
10140 internal_error(state, user->member,
10141 "bad variable access");
10142 }
10143 block = user->member->u.block;
10144 if (!block) {
10145 warning(state, user->member, "dead code");
10146 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000010147 if (work[block->vertex] >= iter) {
10148 continue;
10149 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010150 work[block->vertex] = iter;
10151 *work_list_tail = block;
10152 block->work_next = 0;
10153 work_list_tail = &block->work_next;
10154 }
10155 for(block = work_list; block; block = block->work_next) {
10156 struct block_set *df;
10157 for(df = block->domfrontier; df; df = df->next) {
10158 struct triple *phi;
10159 struct block *front;
10160 int in_edges;
10161 front = df->member;
10162
10163 if (has_already[front->vertex] >= iter) {
10164 continue;
10165 }
10166 /* Count how many edges flow into this block */
10167 in_edges = front->users;
10168 /* Insert a phi function for this variable */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010169 get_occurance(front->first->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010170 phi = alloc_triple(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010171 state, OP_PHI, var->type, -1, in_edges,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010172 front->first->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010173 phi->u.block = front;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010174 MISC(phi, 0) = var;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010175 use_triple(var, phi);
10176 /* Insert the phi functions immediately after the label */
10177 insert_triple(state, front->first->next, phi);
10178 if (front->first == front->last) {
10179 front->last = front->first->next;
10180 }
10181 has_already[front->vertex] = iter;
Eric Biederman05f26fc2003-06-11 21:55:00 +000010182
Eric Biedermanb138ac82003-04-22 18:44:01 +000010183 /* If necessary plan to visit the basic block */
10184 if (work[front->vertex] >= iter) {
10185 continue;
10186 }
10187 work[front->vertex] = iter;
10188 *work_list_tail = front;
10189 front->work_next = 0;
10190 work_list_tail = &front->work_next;
10191 }
10192 }
10193 }
10194 xfree(has_already);
10195 xfree(work);
10196}
10197
10198/*
10199 * C(V)
10200 * S(V)
10201 */
10202static void fixup_block_phi_variables(
10203 struct compile_state *state, struct block *parent, struct block *block)
10204{
10205 struct block_set *set;
10206 struct triple *ptr;
10207 int edge;
10208 if (!parent || !block)
10209 return;
10210 /* Find the edge I am coming in on */
10211 edge = 0;
10212 for(set = block->use; set; set = set->next, edge++) {
10213 if (set->member == parent) {
10214 break;
10215 }
10216 }
10217 if (!set) {
10218 internal_error(state, 0, "phi input is not on a control predecessor");
10219 }
10220 for(ptr = block->first; ; ptr = ptr->next) {
10221 if (ptr->op == OP_PHI) {
10222 struct triple *var, *val, **slot;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010223 var = MISC(ptr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010224 if (!var) {
10225 internal_error(state, ptr, "no var???");
10226 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010227 /* Find the current value of the variable */
10228 val = var->use->member;
10229 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10230 internal_error(state, val, "bad value in phi");
10231 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000010232 if (edge >= TRIPLE_RHS(ptr->sizes)) {
10233 internal_error(state, ptr, "edges > phi rhs");
10234 }
10235 slot = &RHS(ptr, edge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010236 if ((*slot != 0) && (*slot != val)) {
10237 internal_error(state, ptr, "phi already bound on this edge");
10238 }
10239 *slot = val;
10240 use_triple(val, ptr);
10241 }
10242 if (ptr == block->last) {
10243 break;
10244 }
10245 }
10246}
10247
10248
10249static void rename_block_variables(
10250 struct compile_state *state, struct block *block)
10251{
10252 struct block_set *user;
10253 struct triple *ptr, *next, *last;
10254 int done;
10255 if (!block)
10256 return;
10257 last = block->first;
10258 done = 0;
10259 for(ptr = block->first; !done; ptr = next) {
10260 next = ptr->next;
10261 if (ptr == block->last) {
10262 done = 1;
10263 }
10264 /* RHS(A) */
10265 if (ptr->op == OP_READ) {
10266 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010267 var = RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010268 unuse_triple(var, ptr);
10269 if (!var->use) {
10270 error(state, ptr, "variable used without being set");
10271 }
10272 /* Find the current value of the variable */
10273 val = var->use->member;
10274 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10275 internal_error(state, val, "bad value in read");
10276 }
10277 propogate_use(state, ptr, val);
10278 release_triple(state, ptr);
10279 continue;
10280 }
10281 /* LHS(A) */
10282 if (ptr->op == OP_WRITE) {
10283 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010284 var = LHS(ptr, 0);
10285 val = RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010286 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10287 internal_error(state, val, "bad value in write");
10288 }
10289 propogate_use(state, ptr, val);
10290 unuse_triple(var, ptr);
10291 /* Push OP_WRITE ptr->right onto a stack of variable uses */
10292 push_triple(var, val);
10293 }
10294 if (ptr->op == OP_PHI) {
10295 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010296 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010297 /* Push OP_PHI onto a stack of variable uses */
10298 push_triple(var, ptr);
10299 }
10300 last = ptr;
10301 }
10302 block->last = last;
10303
10304 /* Fixup PHI functions in the cf successors */
10305 fixup_block_phi_variables(state, block, block->left);
10306 fixup_block_phi_variables(state, block, block->right);
10307 /* rename variables in the dominated nodes */
10308 for(user = block->idominates; user; user = user->next) {
10309 rename_block_variables(state, user->member);
10310 }
10311 /* pop the renamed variable stack */
10312 last = block->first;
10313 done = 0;
10314 for(ptr = block->first; !done ; ptr = next) {
10315 next = ptr->next;
10316 if (ptr == block->last) {
10317 done = 1;
10318 }
10319 if (ptr->op == OP_WRITE) {
10320 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010321 var = LHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010322 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman0babc1c2003-05-09 02:39:00 +000010323 pop_triple(var, RHS(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010324 release_triple(state, ptr);
10325 continue;
10326 }
10327 if (ptr->op == OP_PHI) {
10328 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010329 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010330 /* Pop OP_WRITE ptr->right from the stack of variable uses */
10331 pop_triple(var, ptr);
10332 }
10333 last = ptr;
10334 }
10335 block->last = last;
10336}
10337
10338static void prune_block_variables(struct compile_state *state,
10339 struct block *block)
10340{
10341 struct block_set *user;
10342 struct triple *next, *last, *ptr;
10343 int done;
10344 last = block->first;
10345 done = 0;
10346 for(ptr = block->first; !done; ptr = next) {
10347 next = ptr->next;
10348 if (ptr == block->last) {
10349 done = 1;
10350 }
10351 if (ptr->op == OP_ADECL) {
10352 struct triple_set *user, *next;
10353 for(user = ptr->use; user; user = next) {
10354 struct triple *use;
10355 next = user->next;
10356 use = user->member;
10357 if (use->op != OP_PHI) {
10358 internal_error(state, use, "decl still used");
10359 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000010360 if (MISC(use, 0) != ptr) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010361 internal_error(state, use, "bad phi use of decl");
10362 }
10363 unuse_triple(ptr, use);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010364 MISC(use, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010365 }
10366 release_triple(state, ptr);
10367 continue;
10368 }
10369 last = ptr;
10370 }
10371 block->last = last;
10372 for(user = block->idominates; user; user = user->next) {
10373 prune_block_variables(state, user->member);
10374 }
10375}
10376
10377static void transform_to_ssa_form(struct compile_state *state)
10378{
10379 insert_phi_operations(state);
10380#if 0
10381 printf("@%s:%d\n", __FILE__, __LINE__);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010382 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010383#endif
10384 rename_block_variables(state, state->first_block);
10385 prune_block_variables(state, state->first_block);
10386}
10387
10388
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010389static void clear_vertex(
10390 struct compile_state *state, struct block *block, void *arg)
10391{
10392 block->vertex = 0;
10393}
10394
10395static void mark_live_block(
10396 struct compile_state *state, struct block *block, int *next_vertex)
10397{
10398 /* See if this is a block that has not been marked */
10399 if (block->vertex != 0) {
10400 return;
10401 }
10402 block->vertex = *next_vertex;
10403 *next_vertex += 1;
10404 if (triple_is_branch(state, block->last)) {
10405 struct triple **targ;
10406 targ = triple_targ(state, block->last, 0);
10407 for(; targ; targ = triple_targ(state, block->last, targ)) {
10408 if (!*targ) {
10409 continue;
10410 }
10411 if (!triple_stores_block(state, *targ)) {
10412 internal_error(state, 0, "bad targ");
10413 }
10414 mark_live_block(state, (*targ)->u.block, next_vertex);
10415 }
10416 }
10417 else if (block->last->next != RHS(state->main_function, 0)) {
10418 struct triple *ins;
10419 ins = block->last->next;
10420 if (!triple_stores_block(state, ins)) {
10421 internal_error(state, 0, "bad block start");
10422 }
10423 mark_live_block(state, ins->u.block, next_vertex);
10424 }
10425}
10426
Eric Biedermanb138ac82003-04-22 18:44:01 +000010427static void transform_from_ssa_form(struct compile_state *state)
10428{
10429 /* To get out of ssa form we insert moves on the incoming
10430 * edges to blocks containting phi functions.
10431 */
10432 struct triple *first;
10433 struct triple *phi, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010434 int next_vertex;
10435
10436 /* Walk the control flow to see which blocks remain alive */
10437 walk_blocks(state, clear_vertex, 0);
10438 next_vertex = 1;
10439 mark_live_block(state, state->first_block, &next_vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010440
10441 /* Walk all of the operations to find the phi functions */
Eric Biederman0babc1c2003-05-09 02:39:00 +000010442 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010443 for(phi = first->next; phi != first ; phi = next) {
10444 struct block_set *set;
10445 struct block *block;
10446 struct triple **slot;
10447 struct triple *var, *read;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010448 struct triple_set *use, *use_next;
10449 int edge, used;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010450 next = phi->next;
10451 if (phi->op != OP_PHI) {
10452 continue;
10453 }
10454 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010455 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010456
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010457 /* Forget uses from code in dead blocks */
10458 for(use = phi->use; use; use = use_next) {
10459 struct block *ublock;
10460 struct triple **expr;
10461 use_next = use->next;
10462 ublock = block_of_triple(state, use->member);
10463 if ((use->member == phi) || (ublock->vertex != 0)) {
10464 continue;
10465 }
10466 expr = triple_rhs(state, use->member, 0);
10467 for(; expr; expr = triple_rhs(state, use->member, expr)) {
10468 if (*expr == phi) {
10469 *expr = 0;
10470 }
10471 }
10472 unuse_triple(phi, use->member);
10473 }
10474
Eric Biedermanb138ac82003-04-22 18:44:01 +000010475 /* A variable to replace the phi function */
10476 var = post_triple(state, phi, OP_ADECL, phi->type, 0,0);
10477 /* A read of the single value that is set into the variable */
10478 read = post_triple(state, var, OP_READ, phi->type, var, 0);
10479 use_triple(var, read);
10480
10481 /* Replaces uses of the phi with variable reads */
10482 propogate_use(state, phi, read);
10483
10484 /* Walk all of the incoming edges/blocks and insert moves.
10485 */
10486 for(edge = 0, set = block->use; set; set = set->next, edge++) {
10487 struct block *eblock;
10488 struct triple *move;
10489 struct triple *val;
10490 eblock = set->member;
10491 val = slot[edge];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010492 slot[edge] = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010493 unuse_triple(val, phi);
10494
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010495 if (!val || (val == &zero_triple) ||
10496 (block->vertex == 0) || (eblock->vertex == 0) ||
10497 (val == phi) || (val == read)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010498 continue;
10499 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010500
Eric Biedermanb138ac82003-04-22 18:44:01 +000010501 move = post_triple(state,
10502 val, OP_WRITE, phi->type, var, val);
10503 use_triple(val, move);
10504 use_triple(var, move);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010505 }
10506 /* See if there are any writers of var */
10507 used = 0;
10508 for(use = var->use; use; use = use->next) {
10509 struct triple **expr;
10510 expr = triple_lhs(state, use->member, 0);
10511 for(; expr; expr = triple_lhs(state, use->member, expr)) {
10512 if (*expr == var) {
10513 used = 1;
10514 }
10515 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010516 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010517 /* If var is not used free it */
10518 if (!used) {
10519 unuse_triple(var, read);
10520 free_triple(state, read);
10521 free_triple(state, var);
10522 }
10523
10524 /* Release the phi function */
Eric Biedermanb138ac82003-04-22 18:44:01 +000010525 release_triple(state, phi);
10526 }
10527
10528}
10529
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010530
10531/*
10532 * Register conflict resolution
10533 * =========================================================
10534 */
10535
10536static struct reg_info find_def_color(
10537 struct compile_state *state, struct triple *def)
10538{
10539 struct triple_set *set;
10540 struct reg_info info;
10541 info.reg = REG_UNSET;
10542 info.regcm = 0;
10543 if (!triple_is_def(state, def)) {
10544 return info;
10545 }
10546 info = arch_reg_lhs(state, def, 0);
10547 if (info.reg >= MAX_REGISTERS) {
10548 info.reg = REG_UNSET;
10549 }
10550 for(set = def->use; set; set = set->next) {
10551 struct reg_info tinfo;
10552 int i;
10553 i = find_rhs_use(state, set->member, def);
10554 if (i < 0) {
10555 continue;
10556 }
10557 tinfo = arch_reg_rhs(state, set->member, i);
10558 if (tinfo.reg >= MAX_REGISTERS) {
10559 tinfo.reg = REG_UNSET;
10560 }
10561 if ((tinfo.reg != REG_UNSET) &&
10562 (info.reg != REG_UNSET) &&
10563 (tinfo.reg != info.reg)) {
10564 internal_error(state, def, "register conflict");
10565 }
10566 if ((info.regcm & tinfo.regcm) == 0) {
10567 internal_error(state, def, "regcm conflict %x & %x == 0",
10568 info.regcm, tinfo.regcm);
10569 }
10570 if (info.reg == REG_UNSET) {
10571 info.reg = tinfo.reg;
10572 }
10573 info.regcm &= tinfo.regcm;
10574 }
10575 if (info.reg >= MAX_REGISTERS) {
10576 internal_error(state, def, "register out of range");
10577 }
10578 return info;
10579}
10580
10581static struct reg_info find_lhs_pre_color(
10582 struct compile_state *state, struct triple *ins, int index)
10583{
10584 struct reg_info info;
10585 int zlhs, zrhs, i;
10586 zrhs = TRIPLE_RHS(ins->sizes);
10587 zlhs = TRIPLE_LHS(ins->sizes);
10588 if (!zlhs && triple_is_def(state, ins)) {
10589 zlhs = 1;
10590 }
10591 if (index >= zlhs) {
10592 internal_error(state, ins, "Bad lhs %d", index);
10593 }
10594 info = arch_reg_lhs(state, ins, index);
10595 for(i = 0; i < zrhs; i++) {
10596 struct reg_info rinfo;
10597 rinfo = arch_reg_rhs(state, ins, i);
10598 if ((info.reg == rinfo.reg) &&
10599 (rinfo.reg >= MAX_REGISTERS)) {
10600 struct reg_info tinfo;
10601 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
10602 info.reg = tinfo.reg;
10603 info.regcm &= tinfo.regcm;
10604 break;
10605 }
10606 }
10607 if (info.reg >= MAX_REGISTERS) {
10608 info.reg = REG_UNSET;
10609 }
10610 return info;
10611}
10612
10613static struct reg_info find_rhs_post_color(
10614 struct compile_state *state, struct triple *ins, int index);
10615
10616static struct reg_info find_lhs_post_color(
10617 struct compile_state *state, struct triple *ins, int index)
10618{
10619 struct triple_set *set;
10620 struct reg_info info;
10621 struct triple *lhs;
10622#if 0
10623 fprintf(stderr, "find_lhs_post_color(%p, %d)\n",
10624 ins, index);
10625#endif
10626 if ((index == 0) && triple_is_def(state, ins)) {
10627 lhs = ins;
10628 }
10629 else if (index < TRIPLE_LHS(ins->sizes)) {
10630 lhs = LHS(ins, index);
10631 }
10632 else {
10633 internal_error(state, ins, "Bad lhs %d", index);
10634 lhs = 0;
10635 }
10636 info = arch_reg_lhs(state, ins, index);
10637 if (info.reg >= MAX_REGISTERS) {
10638 info.reg = REG_UNSET;
10639 }
10640 for(set = lhs->use; set; set = set->next) {
10641 struct reg_info rinfo;
10642 struct triple *user;
10643 int zrhs, i;
10644 user = set->member;
10645 zrhs = TRIPLE_RHS(user->sizes);
10646 for(i = 0; i < zrhs; i++) {
10647 if (RHS(user, i) != lhs) {
10648 continue;
10649 }
10650 rinfo = find_rhs_post_color(state, user, i);
10651 if ((info.reg != REG_UNSET) &&
10652 (rinfo.reg != REG_UNSET) &&
10653 (info.reg != rinfo.reg)) {
10654 internal_error(state, ins, "register conflict");
10655 }
10656 if ((info.regcm & rinfo.regcm) == 0) {
10657 internal_error(state, ins, "regcm conflict %x & %x == 0",
10658 info.regcm, rinfo.regcm);
10659 }
10660 if (info.reg == REG_UNSET) {
10661 info.reg = rinfo.reg;
10662 }
10663 info.regcm &= rinfo.regcm;
10664 }
10665 }
10666#if 0
10667 fprintf(stderr, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
10668 ins, index, info.reg, info.regcm);
10669#endif
10670 return info;
10671}
10672
10673static struct reg_info find_rhs_post_color(
10674 struct compile_state *state, struct triple *ins, int index)
10675{
10676 struct reg_info info, rinfo;
10677 int zlhs, i;
10678#if 0
10679 fprintf(stderr, "find_rhs_post_color(%p, %d)\n",
10680 ins, index);
10681#endif
10682 rinfo = arch_reg_rhs(state, ins, index);
10683 zlhs = TRIPLE_LHS(ins->sizes);
10684 if (!zlhs && triple_is_def(state, ins)) {
10685 zlhs = 1;
10686 }
10687 info = rinfo;
10688 if (info.reg >= MAX_REGISTERS) {
10689 info.reg = REG_UNSET;
10690 }
10691 for(i = 0; i < zlhs; i++) {
10692 struct reg_info linfo;
10693 linfo = arch_reg_lhs(state, ins, i);
10694 if ((linfo.reg == rinfo.reg) &&
10695 (linfo.reg >= MAX_REGISTERS)) {
10696 struct reg_info tinfo;
10697 tinfo = find_lhs_post_color(state, ins, i);
10698 if (tinfo.reg >= MAX_REGISTERS) {
10699 tinfo.reg = REG_UNSET;
10700 }
10701 info.regcm &= linfo.reg;
10702 info.regcm &= tinfo.regcm;
10703 if (info.reg != REG_UNSET) {
10704 internal_error(state, ins, "register conflict");
10705 }
10706 if (info.regcm == 0) {
10707 internal_error(state, ins, "regcm conflict");
10708 }
10709 info.reg = tinfo.reg;
10710 }
10711 }
10712#if 0
10713 fprintf(stderr, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
10714 ins, index, info.reg, info.regcm);
10715#endif
10716 return info;
10717}
10718
10719static struct reg_info find_lhs_color(
10720 struct compile_state *state, struct triple *ins, int index)
10721{
10722 struct reg_info pre, post, info;
10723#if 0
10724 fprintf(stderr, "find_lhs_color(%p, %d)\n",
10725 ins, index);
10726#endif
10727 pre = find_lhs_pre_color(state, ins, index);
10728 post = find_lhs_post_color(state, ins, index);
10729 if ((pre.reg != post.reg) &&
10730 (pre.reg != REG_UNSET) &&
10731 (post.reg != REG_UNSET)) {
10732 internal_error(state, ins, "register conflict");
10733 }
10734 info.regcm = pre.regcm & post.regcm;
10735 info.reg = pre.reg;
10736 if (info.reg == REG_UNSET) {
10737 info.reg = post.reg;
10738 }
10739#if 0
10740 fprintf(stderr, "find_lhs_color(%p, %d) -> ( %d, %x)\n",
10741 ins, index, info.reg, info.regcm);
10742#endif
10743 return info;
10744}
10745
10746static struct triple *post_copy(struct compile_state *state, struct triple *ins)
10747{
10748 struct triple_set *entry, *next;
10749 struct triple *out;
10750 struct reg_info info, rinfo;
10751
10752 info = arch_reg_lhs(state, ins, 0);
10753 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
10754 use_triple(RHS(out, 0), out);
10755 /* Get the users of ins to use out instead */
10756 for(entry = ins->use; entry; entry = next) {
10757 int i;
10758 next = entry->next;
10759 if (entry->member == out) {
10760 continue;
10761 }
10762 i = find_rhs_use(state, entry->member, ins);
10763 if (i < 0) {
10764 continue;
10765 }
10766 rinfo = arch_reg_rhs(state, entry->member, i);
10767 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
10768 continue;
10769 }
10770 replace_rhs_use(state, ins, out, entry->member);
10771 }
10772 transform_to_arch_instruction(state, out);
10773 return out;
10774}
10775
10776static struct triple *pre_copy(
10777 struct compile_state *state, struct triple *ins, int index)
10778{
10779 /* Carefully insert enough operations so that I can
10780 * enter any operation with a GPR32.
10781 */
10782 struct triple *in;
10783 struct triple **expr;
10784 expr = &RHS(ins, index);
10785 in = pre_triple(state, ins, OP_COPY, (*expr)->type, *expr, 0);
10786 unuse_triple(*expr, ins);
10787 *expr = in;
10788 use_triple(RHS(in, 0), in);
10789 use_triple(in, ins);
10790 transform_to_arch_instruction(state, in);
10791 return in;
10792}
10793
10794
Eric Biedermanb138ac82003-04-22 18:44:01 +000010795static void insert_copies_to_phi(struct compile_state *state)
10796{
10797 /* To get out of ssa form we insert moves on the incoming
10798 * edges to blocks containting phi functions.
10799 */
10800 struct triple *first;
10801 struct triple *phi;
10802
10803 /* Walk all of the operations to find the phi functions */
Eric Biederman0babc1c2003-05-09 02:39:00 +000010804 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010805 for(phi = first->next; phi != first ; phi = phi->next) {
10806 struct block_set *set;
10807 struct block *block;
10808 struct triple **slot;
10809 int edge;
10810 if (phi->op != OP_PHI) {
10811 continue;
10812 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010813 phi->id |= TRIPLE_FLAG_POST_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010814 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010815 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010816 /* Walk all of the incoming edges/blocks and insert moves.
10817 */
10818 for(edge = 0, set = block->use; set; set = set->next, edge++) {
10819 struct block *eblock;
10820 struct triple *move;
10821 struct triple *val;
10822 struct triple *ptr;
10823 eblock = set->member;
10824 val = slot[edge];
10825
10826 if (val == phi) {
10827 continue;
10828 }
10829
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010830 get_occurance(val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010831 move = build_triple(state, OP_COPY, phi->type, val, 0,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010832 val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010833 move->u.block = eblock;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010834 move->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010835 use_triple(val, move);
10836
10837 slot[edge] = move;
10838 unuse_triple(val, phi);
10839 use_triple(move, phi);
10840
10841 /* Walk through the block backwards to find
10842 * an appropriate location for the OP_COPY.
10843 */
10844 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
10845 struct triple **expr;
10846 if ((ptr == phi) || (ptr == val)) {
10847 goto out;
10848 }
10849 expr = triple_rhs(state, ptr, 0);
10850 for(;expr; expr = triple_rhs(state, ptr, expr)) {
10851 if ((*expr) == phi) {
10852 goto out;
10853 }
10854 }
10855 }
10856 out:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010857 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010858 internal_error(state, ptr,
10859 "Could not insert write to phi");
10860 }
10861 insert_triple(state, ptr->next, move);
10862 if (eblock->last == ptr) {
10863 eblock->last = move;
10864 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010865 transform_to_arch_instruction(state, move);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010866 }
10867 }
10868}
10869
10870struct triple_reg_set {
10871 struct triple_reg_set *next;
10872 struct triple *member;
10873 struct triple *new;
10874};
10875
10876struct reg_block {
10877 struct block *block;
10878 struct triple_reg_set *in;
10879 struct triple_reg_set *out;
10880 int vertex;
10881};
10882
10883static int do_triple_set(struct triple_reg_set **head,
10884 struct triple *member, struct triple *new_member)
10885{
10886 struct triple_reg_set **ptr, *new;
10887 if (!member)
10888 return 0;
10889 ptr = head;
10890 while(*ptr) {
10891 if ((*ptr)->member == member) {
10892 return 0;
10893 }
10894 ptr = &(*ptr)->next;
10895 }
10896 new = xcmalloc(sizeof(*new), "triple_set");
10897 new->member = member;
10898 new->new = new_member;
10899 new->next = *head;
10900 *head = new;
10901 return 1;
10902}
10903
10904static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
10905{
10906 struct triple_reg_set *entry, **ptr;
10907 ptr = head;
10908 while(*ptr) {
10909 entry = *ptr;
10910 if (entry->member == member) {
10911 *ptr = entry->next;
10912 xfree(entry);
10913 return;
10914 }
10915 else {
10916 ptr = &entry->next;
10917 }
10918 }
10919}
10920
10921static int in_triple(struct reg_block *rb, struct triple *in)
10922{
10923 return do_triple_set(&rb->in, in, 0);
10924}
10925static void unin_triple(struct reg_block *rb, struct triple *unin)
10926{
10927 do_triple_unset(&rb->in, unin);
10928}
10929
10930static int out_triple(struct reg_block *rb, struct triple *out)
10931{
10932 return do_triple_set(&rb->out, out, 0);
10933}
10934static void unout_triple(struct reg_block *rb, struct triple *unout)
10935{
10936 do_triple_unset(&rb->out, unout);
10937}
10938
10939static int initialize_regblock(struct reg_block *blocks,
10940 struct block *block, int vertex)
10941{
10942 struct block_set *user;
10943 if (!block || (blocks[block->vertex].block == block)) {
10944 return vertex;
10945 }
10946 vertex += 1;
10947 /* Renumber the blocks in a convinient fashion */
10948 block->vertex = vertex;
10949 blocks[vertex].block = block;
10950 blocks[vertex].vertex = vertex;
10951 for(user = block->use; user; user = user->next) {
10952 vertex = initialize_regblock(blocks, user->member, vertex);
10953 }
10954 return vertex;
10955}
10956
10957static int phi_in(struct compile_state *state, struct reg_block *blocks,
10958 struct reg_block *rb, struct block *suc)
10959{
10960 /* Read the conditional input set of a successor block
10961 * (i.e. the input to the phi nodes) and place it in the
10962 * current blocks output set.
10963 */
10964 struct block_set *set;
10965 struct triple *ptr;
10966 int edge;
10967 int done, change;
10968 change = 0;
10969 /* Find the edge I am coming in on */
10970 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
10971 if (set->member == rb->block) {
10972 break;
10973 }
10974 }
10975 if (!set) {
10976 internal_error(state, 0, "Not coming on a control edge?");
10977 }
10978 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
10979 struct triple **slot, *expr, *ptr2;
10980 int out_change, done2;
10981 done = (ptr == suc->last);
10982 if (ptr->op != OP_PHI) {
10983 continue;
10984 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000010985 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010986 expr = slot[edge];
10987 out_change = out_triple(rb, expr);
10988 if (!out_change) {
10989 continue;
10990 }
10991 /* If we don't define the variable also plast it
10992 * in the current blocks input set.
10993 */
10994 ptr2 = rb->block->first;
10995 for(done2 = 0; !done2; ptr2 = ptr2->next) {
10996 if (ptr2 == expr) {
10997 break;
10998 }
10999 done2 = (ptr2 == rb->block->last);
11000 }
11001 if (!done2) {
11002 continue;
11003 }
11004 change |= in_triple(rb, expr);
11005 }
11006 return change;
11007}
11008
11009static int reg_in(struct compile_state *state, struct reg_block *blocks,
11010 struct reg_block *rb, struct block *suc)
11011{
11012 struct triple_reg_set *in_set;
11013 int change;
11014 change = 0;
11015 /* Read the input set of a successor block
11016 * and place it in the current blocks output set.
11017 */
11018 in_set = blocks[suc->vertex].in;
11019 for(; in_set; in_set = in_set->next) {
11020 int out_change, done;
11021 struct triple *first, *last, *ptr;
11022 out_change = out_triple(rb, in_set->member);
11023 if (!out_change) {
11024 continue;
11025 }
11026 /* If we don't define the variable also place it
11027 * in the current blocks input set.
11028 */
11029 first = rb->block->first;
11030 last = rb->block->last;
11031 done = 0;
11032 for(ptr = first; !done; ptr = ptr->next) {
11033 if (ptr == in_set->member) {
11034 break;
11035 }
11036 done = (ptr == last);
11037 }
11038 if (!done) {
11039 continue;
11040 }
11041 change |= in_triple(rb, in_set->member);
11042 }
11043 change |= phi_in(state, blocks, rb, suc);
11044 return change;
11045}
11046
11047
11048static int use_in(struct compile_state *state, struct reg_block *rb)
11049{
11050 /* Find the variables we use but don't define and add
11051 * it to the current blocks input set.
11052 */
11053#warning "FIXME is this O(N^2) algorithm bad?"
11054 struct block *block;
11055 struct triple *ptr;
11056 int done;
11057 int change;
11058 block = rb->block;
11059 change = 0;
11060 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
11061 struct triple **expr;
11062 done = (ptr == block->first);
11063 /* The variable a phi function uses depends on the
11064 * control flow, and is handled in phi_in, not
11065 * here.
11066 */
11067 if (ptr->op == OP_PHI) {
11068 continue;
11069 }
11070 expr = triple_rhs(state, ptr, 0);
11071 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11072 struct triple *rhs, *test;
11073 int tdone;
11074 rhs = *expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011075 if (!rhs) {
11076 continue;
11077 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011078 /* See if rhs is defined in this block */
11079 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
11080 tdone = (test == block->first);
11081 if (test == rhs) {
11082 rhs = 0;
11083 break;
11084 }
11085 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011086 /* If I still have a valid rhs add it to in */
11087 change |= in_triple(rb, rhs);
11088 }
11089 }
11090 return change;
11091}
11092
11093static struct reg_block *compute_variable_lifetimes(
11094 struct compile_state *state)
11095{
11096 struct reg_block *blocks;
11097 int change;
11098 blocks = xcmalloc(
11099 sizeof(*blocks)*(state->last_vertex + 1), "reg_block");
11100 initialize_regblock(blocks, state->last_block, 0);
11101 do {
11102 int i;
11103 change = 0;
11104 for(i = 1; i <= state->last_vertex; i++) {
11105 struct reg_block *rb;
11106 rb = &blocks[i];
11107 /* Add the left successor's input set to in */
11108 if (rb->block->left) {
11109 change |= reg_in(state, blocks, rb, rb->block->left);
11110 }
11111 /* Add the right successor's input set to in */
11112 if ((rb->block->right) &&
11113 (rb->block->right != rb->block->left)) {
11114 change |= reg_in(state, blocks, rb, rb->block->right);
11115 }
11116 /* Add use to in... */
11117 change |= use_in(state, rb);
11118 }
11119 } while(change);
11120 return blocks;
11121}
11122
11123static void free_variable_lifetimes(
11124 struct compile_state *state, struct reg_block *blocks)
11125{
11126 int i;
11127 /* free in_set && out_set on each block */
11128 for(i = 1; i <= state->last_vertex; i++) {
11129 struct triple_reg_set *entry, *next;
11130 struct reg_block *rb;
11131 rb = &blocks[i];
11132 for(entry = rb->in; entry ; entry = next) {
11133 next = entry->next;
11134 do_triple_unset(&rb->in, entry->member);
11135 }
11136 for(entry = rb->out; entry; entry = next) {
11137 next = entry->next;
11138 do_triple_unset(&rb->out, entry->member);
11139 }
11140 }
11141 xfree(blocks);
11142
11143}
11144
Eric Biedermanf96a8102003-06-16 16:57:34 +000011145typedef void (*wvl_cb_t)(
Eric Biedermanb138ac82003-04-22 18:44:01 +000011146 struct compile_state *state,
11147 struct reg_block *blocks, struct triple_reg_set *live,
11148 struct reg_block *rb, struct triple *ins, void *arg);
11149
11150static void walk_variable_lifetimes(struct compile_state *state,
11151 struct reg_block *blocks, wvl_cb_t cb, void *arg)
11152{
11153 int i;
11154
11155 for(i = 1; i <= state->last_vertex; i++) {
11156 struct triple_reg_set *live;
11157 struct triple_reg_set *entry, *next;
11158 struct triple *ptr, *prev;
11159 struct reg_block *rb;
11160 struct block *block;
11161 int done;
11162
11163 /* Get the blocks */
11164 rb = &blocks[i];
11165 block = rb->block;
11166
11167 /* Copy out into live */
11168 live = 0;
11169 for(entry = rb->out; entry; entry = next) {
11170 next = entry->next;
11171 do_triple_set(&live, entry->member, entry->new);
11172 }
11173 /* Walk through the basic block calculating live */
11174 for(done = 0, ptr = block->last; !done; ptr = prev) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000011175 struct triple **expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011176
11177 prev = ptr->prev;
11178 done = (ptr == block->first);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011179
11180 /* Ensure the current definition is in live */
11181 if (triple_is_def(state, ptr)) {
11182 do_triple_set(&live, ptr, 0);
11183 }
11184
11185 /* Inform the callback function of what is
11186 * going on.
11187 */
Eric Biedermanf96a8102003-06-16 16:57:34 +000011188 cb(state, blocks, live, rb, ptr, arg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011189
11190 /* Remove the current definition from live */
11191 do_triple_unset(&live, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011192
Eric Biedermanb138ac82003-04-22 18:44:01 +000011193 /* Add the current uses to live.
11194 *
11195 * It is safe to skip phi functions because they do
11196 * not have any block local uses, and the block
11197 * output sets already properly account for what
11198 * control flow depedent uses phi functions do have.
11199 */
11200 if (ptr->op == OP_PHI) {
11201 continue;
11202 }
11203 expr = triple_rhs(state, ptr, 0);
11204 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11205 /* If the triple is not a definition skip it. */
Eric Biederman0babc1c2003-05-09 02:39:00 +000011206 if (!*expr || !triple_is_def(state, *expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011207 continue;
11208 }
11209 do_triple_set(&live, *expr, 0);
11210 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011211 }
11212 /* Free live */
11213 for(entry = live; entry; entry = next) {
11214 next = entry->next;
11215 do_triple_unset(&live, entry->member);
11216 }
11217 }
11218}
11219
11220static int count_triples(struct compile_state *state)
11221{
11222 struct triple *first, *ins;
11223 int triples = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011224 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011225 ins = first;
11226 do {
11227 triples++;
11228 ins = ins->next;
11229 } while (ins != first);
11230 return triples;
11231}
11232struct dead_triple {
11233 struct triple *triple;
11234 struct dead_triple *work_next;
11235 struct block *block;
11236 int color;
11237 int flags;
11238#define TRIPLE_FLAG_ALIVE 1
11239};
11240
11241
11242static void awaken(
11243 struct compile_state *state,
11244 struct dead_triple *dtriple, struct triple **expr,
11245 struct dead_triple ***work_list_tail)
11246{
11247 struct triple *triple;
11248 struct dead_triple *dt;
11249 if (!expr) {
11250 return;
11251 }
11252 triple = *expr;
11253 if (!triple) {
11254 return;
11255 }
11256 if (triple->id <= 0) {
11257 internal_error(state, triple, "bad triple id: %d",
11258 triple->id);
11259 }
11260 if (triple->op == OP_NOOP) {
11261 internal_warning(state, triple, "awakening noop?");
11262 return;
11263 }
11264 dt = &dtriple[triple->id];
11265 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11266 dt->flags |= TRIPLE_FLAG_ALIVE;
11267 if (!dt->work_next) {
11268 **work_list_tail = dt;
11269 *work_list_tail = &dt->work_next;
11270 }
11271 }
11272}
11273
11274static void eliminate_inefectual_code(struct compile_state *state)
11275{
11276 struct block *block;
11277 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
11278 int triples, i;
11279 struct triple *first, *ins;
11280
11281 /* Setup the work list */
11282 work_list = 0;
11283 work_list_tail = &work_list;
11284
Eric Biederman0babc1c2003-05-09 02:39:00 +000011285 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011286
11287 /* Count how many triples I have */
11288 triples = count_triples(state);
11289
11290 /* Now put then in an array and mark all of the triples dead */
11291 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
11292
11293 ins = first;
11294 i = 1;
11295 block = 0;
11296 do {
11297 if (ins->op == OP_LABEL) {
11298 block = ins->u.block;
11299 }
11300 dtriple[i].triple = ins;
11301 dtriple[i].block = block;
11302 dtriple[i].flags = 0;
11303 dtriple[i].color = ins->id;
11304 ins->id = i;
11305 /* See if it is an operation we always keep */
11306#warning "FIXME handle the case of killing a branch instruction"
Eric Biederman0babc1c2003-05-09 02:39:00 +000011307 if (!triple_is_pure(state, ins) || triple_is_branch(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011308 awaken(state, dtriple, &ins, &work_list_tail);
11309 }
11310 i++;
11311 ins = ins->next;
11312 } while(ins != first);
11313 while(work_list) {
11314 struct dead_triple *dt;
11315 struct block_set *user;
11316 struct triple **expr;
11317 dt = work_list;
11318 work_list = dt->work_next;
11319 if (!work_list) {
11320 work_list_tail = &work_list;
11321 }
11322 /* Wake up the data depencencies of this triple */
11323 expr = 0;
11324 do {
11325 expr = triple_rhs(state, dt->triple, expr);
11326 awaken(state, dtriple, expr, &work_list_tail);
11327 } while(expr);
11328 do {
11329 expr = triple_lhs(state, dt->triple, expr);
11330 awaken(state, dtriple, expr, &work_list_tail);
11331 } while(expr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011332 do {
11333 expr = triple_misc(state, dt->triple, expr);
11334 awaken(state, dtriple, expr, &work_list_tail);
11335 } while(expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011336 /* Wake up the forward control dependencies */
11337 do {
11338 expr = triple_targ(state, dt->triple, expr);
11339 awaken(state, dtriple, expr, &work_list_tail);
11340 } while(expr);
11341 /* Wake up the reverse control dependencies of this triple */
11342 for(user = dt->block->ipdomfrontier; user; user = user->next) {
11343 awaken(state, dtriple, &user->member->last, &work_list_tail);
11344 }
11345 }
11346 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
11347 if ((dt->triple->op == OP_NOOP) &&
11348 (dt->flags & TRIPLE_FLAG_ALIVE)) {
11349 internal_error(state, dt->triple, "noop effective?");
11350 }
11351 dt->triple->id = dt->color; /* Restore the color */
11352 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11353#warning "FIXME handle the case of killing a basic block"
11354 if (dt->block->first == dt->triple) {
11355 continue;
11356 }
11357 if (dt->block->last == dt->triple) {
11358 dt->block->last = dt->triple->prev;
11359 }
11360 release_triple(state, dt->triple);
11361 }
11362 }
11363 xfree(dtriple);
11364}
11365
11366
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011367static void insert_mandatory_copies(struct compile_state *state)
11368{
11369 struct triple *ins, *first;
11370
11371 /* The object is with a minimum of inserted copies,
11372 * to resolve in fundamental register conflicts between
11373 * register value producers and consumers.
11374 * Theoretically we may be greater than minimal when we
11375 * are inserting copies before instructions but that
11376 * case should be rare.
11377 */
11378 first = RHS(state->main_function, 0);
11379 ins = first;
11380 do {
11381 struct triple_set *entry, *next;
11382 struct triple *tmp;
11383 struct reg_info info;
11384 unsigned reg, regcm;
11385 int do_post_copy, do_pre_copy;
11386 tmp = 0;
11387 if (!triple_is_def(state, ins)) {
11388 goto next;
11389 }
11390 /* Find the architecture specific color information */
11391 info = arch_reg_lhs(state, ins, 0);
11392 if (info.reg >= MAX_REGISTERS) {
11393 info.reg = REG_UNSET;
11394 }
11395
11396 reg = REG_UNSET;
11397 regcm = arch_type_to_regcm(state, ins->type);
11398 do_post_copy = do_pre_copy = 0;
11399
11400 /* Walk through the uses of ins and check for conflicts */
11401 for(entry = ins->use; entry; entry = next) {
11402 struct reg_info rinfo;
11403 int i;
11404 next = entry->next;
11405 i = find_rhs_use(state, entry->member, ins);
11406 if (i < 0) {
11407 continue;
11408 }
11409
11410 /* Find the users color requirements */
11411 rinfo = arch_reg_rhs(state, entry->member, i);
11412 if (rinfo.reg >= MAX_REGISTERS) {
11413 rinfo.reg = REG_UNSET;
11414 }
11415
11416 /* See if I need a pre_copy */
11417 if (rinfo.reg != REG_UNSET) {
11418 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
11419 do_pre_copy = 1;
11420 }
11421 reg = rinfo.reg;
11422 }
11423 regcm &= rinfo.regcm;
11424 regcm = arch_regcm_normalize(state, regcm);
11425 if (regcm == 0) {
11426 do_pre_copy = 1;
11427 }
11428 }
11429 do_post_copy =
11430 !do_pre_copy &&
11431 (((info.reg != REG_UNSET) &&
11432 (reg != REG_UNSET) &&
11433 (info.reg != reg)) ||
11434 ((info.regcm & regcm) == 0));
11435
11436 reg = info.reg;
11437 regcm = info.regcm;
11438 /* Walk through the uses of insert and do a pre_copy or see if a post_copy is warranted */
11439 for(entry = ins->use; entry; entry = next) {
11440 struct reg_info rinfo;
11441 int i;
11442 next = entry->next;
11443 i = find_rhs_use(state, entry->member, ins);
11444 if (i < 0) {
11445 continue;
11446 }
11447
11448 /* Find the users color requirements */
11449 rinfo = arch_reg_rhs(state, entry->member, i);
11450 if (rinfo.reg >= MAX_REGISTERS) {
11451 rinfo.reg = REG_UNSET;
11452 }
11453
11454 /* Now see if it is time to do the pre_copy */
11455 if (rinfo.reg != REG_UNSET) {
11456 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
11457 ((regcm & rinfo.regcm) == 0) ||
11458 /* Don't let a mandatory coalesce sneak
11459 * into a operation that is marked to prevent
11460 * coalescing.
11461 */
11462 ((reg != REG_UNNEEDED) &&
11463 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
11464 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
11465 ) {
11466 if (do_pre_copy) {
11467 struct triple *user;
11468 user = entry->member;
11469 if (RHS(user, i) != ins) {
11470 internal_error(state, user, "bad rhs");
11471 }
11472 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011473 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011474 continue;
11475 } else {
11476 do_post_copy = 1;
11477 }
11478 }
11479 reg = rinfo.reg;
11480 }
11481 if ((regcm & rinfo.regcm) == 0) {
11482 if (do_pre_copy) {
11483 struct triple *user;
11484 user = entry->member;
11485 if (RHS(user, i) != ins) {
11486 internal_error(state, user, "bad rhs");
11487 }
11488 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011489 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011490 continue;
11491 } else {
11492 do_post_copy = 1;
11493 }
11494 }
11495 regcm &= rinfo.regcm;
11496
11497 }
11498 if (do_post_copy) {
11499 struct reg_info pre, post;
11500 tmp = post_copy(state, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011501 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011502 pre = arch_reg_lhs(state, ins, 0);
11503 post = arch_reg_lhs(state, tmp, 0);
11504 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
11505 internal_error(state, tmp, "useless copy");
11506 }
11507 }
11508 next:
11509 ins = ins->next;
11510 } while(ins != first);
11511}
11512
11513
Eric Biedermanb138ac82003-04-22 18:44:01 +000011514struct live_range_edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011515struct live_range_def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011516struct live_range {
11517 struct live_range_edge *edges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011518 struct live_range_def *defs;
11519/* Note. The list pointed to by defs is kept in order.
11520 * That is baring splits in the flow control
11521 * defs dominates defs->next wich dominates defs->next->next
11522 * etc.
11523 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011524 unsigned color;
11525 unsigned classes;
11526 unsigned degree;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011527 unsigned length;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011528 struct live_range *group_next, **group_prev;
11529};
11530
11531struct live_range_edge {
11532 struct live_range_edge *next;
11533 struct live_range *node;
11534};
11535
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011536struct live_range_def {
11537 struct live_range_def *next;
11538 struct live_range_def *prev;
11539 struct live_range *lr;
11540 struct triple *def;
11541 unsigned orig_id;
11542};
11543
Eric Biedermanb138ac82003-04-22 18:44:01 +000011544#define LRE_HASH_SIZE 2048
11545struct lre_hash {
11546 struct lre_hash *next;
11547 struct live_range *left;
11548 struct live_range *right;
11549};
11550
11551
11552struct reg_state {
11553 struct lre_hash *hash[LRE_HASH_SIZE];
11554 struct reg_block *blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011555 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011556 struct live_range *lr;
11557 struct live_range *low, **low_tail;
11558 struct live_range *high, **high_tail;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011559 unsigned defs;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011560 unsigned ranges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011561 int passes, max_passes;
11562#define MAX_ALLOCATION_PASSES 100
Eric Biedermanb138ac82003-04-22 18:44:01 +000011563};
11564
11565
11566static unsigned regc_max_size(struct compile_state *state, int classes)
11567{
11568 unsigned max_size;
11569 int i;
11570 max_size = 0;
11571 for(i = 0; i < MAX_REGC; i++) {
11572 if (classes & (1 << i)) {
11573 unsigned size;
11574 size = arch_regc_size(state, i);
11575 if (size > max_size) {
11576 max_size = size;
11577 }
11578 }
11579 }
11580 return max_size;
11581}
11582
11583static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
11584{
11585 unsigned equivs[MAX_REG_EQUIVS];
11586 int i;
11587 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
11588 internal_error(state, 0, "invalid register");
11589 }
11590 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
11591 internal_error(state, 0, "invalid register");
11592 }
11593 arch_reg_equivs(state, equivs, reg1);
11594 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11595 if (equivs[i] == reg2) {
11596 return 1;
11597 }
11598 }
11599 return 0;
11600}
11601
11602static void reg_fill_used(struct compile_state *state, char *used, int reg)
11603{
11604 unsigned equivs[MAX_REG_EQUIVS];
11605 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011606 if (reg == REG_UNNEEDED) {
11607 return;
11608 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011609 arch_reg_equivs(state, equivs, reg);
11610 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11611 used[equivs[i]] = 1;
11612 }
11613 return;
11614}
11615
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011616static void reg_inc_used(struct compile_state *state, char *used, int reg)
11617{
11618 unsigned equivs[MAX_REG_EQUIVS];
11619 int i;
11620 if (reg == REG_UNNEEDED) {
11621 return;
11622 }
11623 arch_reg_equivs(state, equivs, reg);
11624 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11625 used[equivs[i]] += 1;
11626 }
11627 return;
11628}
11629
Eric Biedermanb138ac82003-04-22 18:44:01 +000011630static unsigned int hash_live_edge(
11631 struct live_range *left, struct live_range *right)
11632{
11633 unsigned int hash, val;
11634 unsigned long lval, rval;
11635 lval = ((unsigned long)left)/sizeof(struct live_range);
11636 rval = ((unsigned long)right)/sizeof(struct live_range);
11637 hash = 0;
11638 while(lval) {
11639 val = lval & 0xff;
11640 lval >>= 8;
11641 hash = (hash *263) + val;
11642 }
11643 while(rval) {
11644 val = rval & 0xff;
11645 rval >>= 8;
11646 hash = (hash *263) + val;
11647 }
11648 hash = hash & (LRE_HASH_SIZE - 1);
11649 return hash;
11650}
11651
11652static struct lre_hash **lre_probe(struct reg_state *rstate,
11653 struct live_range *left, struct live_range *right)
11654{
11655 struct lre_hash **ptr;
11656 unsigned int index;
11657 /* Ensure left <= right */
11658 if (left > right) {
11659 struct live_range *tmp;
11660 tmp = left;
11661 left = right;
11662 right = tmp;
11663 }
11664 index = hash_live_edge(left, right);
11665
11666 ptr = &rstate->hash[index];
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011667 while(*ptr) {
11668 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
11669 break;
11670 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011671 ptr = &(*ptr)->next;
11672 }
11673 return ptr;
11674}
11675
11676static int interfere(struct reg_state *rstate,
11677 struct live_range *left, struct live_range *right)
11678{
11679 struct lre_hash **ptr;
11680 ptr = lre_probe(rstate, left, right);
11681 return ptr && *ptr;
11682}
11683
11684static void add_live_edge(struct reg_state *rstate,
11685 struct live_range *left, struct live_range *right)
11686{
11687 /* FIXME the memory allocation overhead is noticeable here... */
11688 struct lre_hash **ptr, *new_hash;
11689 struct live_range_edge *edge;
11690
11691 if (left == right) {
11692 return;
11693 }
11694 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
11695 return;
11696 }
11697 /* Ensure left <= right */
11698 if (left > right) {
11699 struct live_range *tmp;
11700 tmp = left;
11701 left = right;
11702 right = tmp;
11703 }
11704 ptr = lre_probe(rstate, left, right);
11705 if (*ptr) {
11706 return;
11707 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011708#if 0
11709 fprintf(stderr, "new_live_edge(%p, %p)\n",
11710 left, right);
11711#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011712 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
11713 new_hash->next = *ptr;
11714 new_hash->left = left;
11715 new_hash->right = right;
11716 *ptr = new_hash;
11717
11718 edge = xmalloc(sizeof(*edge), "live_range_edge");
11719 edge->next = left->edges;
11720 edge->node = right;
11721 left->edges = edge;
11722 left->degree += 1;
11723
11724 edge = xmalloc(sizeof(*edge), "live_range_edge");
11725 edge->next = right->edges;
11726 edge->node = left;
11727 right->edges = edge;
11728 right->degree += 1;
11729}
11730
11731static void remove_live_edge(struct reg_state *rstate,
11732 struct live_range *left, struct live_range *right)
11733{
11734 struct live_range_edge *edge, **ptr;
11735 struct lre_hash **hptr, *entry;
11736 hptr = lre_probe(rstate, left, right);
11737 if (!hptr || !*hptr) {
11738 return;
11739 }
11740 entry = *hptr;
11741 *hptr = entry->next;
11742 xfree(entry);
11743
11744 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
11745 edge = *ptr;
11746 if (edge->node == right) {
11747 *ptr = edge->next;
11748 memset(edge, 0, sizeof(*edge));
11749 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011750 right->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011751 break;
11752 }
11753 }
11754 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
11755 edge = *ptr;
11756 if (edge->node == left) {
11757 *ptr = edge->next;
11758 memset(edge, 0, sizeof(*edge));
11759 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011760 left->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011761 break;
11762 }
11763 }
11764}
11765
11766static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
11767{
11768 struct live_range_edge *edge, *next;
11769 for(edge = range->edges; edge; edge = next) {
11770 next = edge->next;
11771 remove_live_edge(rstate, range, edge->node);
11772 }
11773}
11774
11775
11776/* Interference graph...
11777 *
11778 * new(n) --- Return a graph with n nodes but no edges.
11779 * add(g,x,y) --- Return a graph including g with an between x and y
11780 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
11781 * x and y in the graph g
11782 * degree(g, x) --- Return the degree of the node x in the graph g
11783 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
11784 *
11785 * Implement with a hash table && a set of adjcency vectors.
11786 * The hash table supports constant time implementations of add and interfere.
11787 * The adjacency vectors support an efficient implementation of neighbors.
11788 */
11789
11790/*
11791 * +---------------------------------------------------+
11792 * | +--------------+ |
11793 * v v | |
11794 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
11795 *
11796 * -- In simplify implment optimistic coloring... (No backtracking)
11797 * -- Implement Rematerialization it is the only form of spilling we can perform
11798 * Essentially this means dropping a constant from a register because
11799 * we can regenerate it later.
11800 *
11801 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
11802 * coalesce at phi points...
11803 * --- Bias coloring if at all possible do the coalesing a compile time.
11804 *
11805 *
11806 */
11807
11808static void different_colored(
11809 struct compile_state *state, struct reg_state *rstate,
11810 struct triple *parent, struct triple *ins)
11811{
11812 struct live_range *lr;
11813 struct triple **expr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011814 lr = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011815 expr = triple_rhs(state, ins, 0);
11816 for(;expr; expr = triple_rhs(state, ins, expr)) {
11817 struct live_range *lr2;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011818 if (!*expr || (*expr == parent) || (*expr == ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011819 continue;
11820 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011821 lr2 = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011822 if (lr->color == lr2->color) {
11823 internal_error(state, ins, "live range too big");
11824 }
11825 }
11826}
11827
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011828
11829static struct live_range *coalesce_ranges(
11830 struct compile_state *state, struct reg_state *rstate,
11831 struct live_range *lr1, struct live_range *lr2)
11832{
11833 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
11834 unsigned color;
11835 unsigned classes;
11836 if (lr1 == lr2) {
11837 return lr1;
11838 }
11839 if (!lr1->defs || !lr2->defs) {
11840 internal_error(state, 0,
11841 "cannot coalese dead live ranges");
11842 }
11843 if ((lr1->color == REG_UNNEEDED) ||
11844 (lr2->color == REG_UNNEEDED)) {
11845 internal_error(state, 0,
11846 "cannot coalesce live ranges without a possible color");
11847 }
11848 if ((lr1->color != lr2->color) &&
11849 (lr1->color != REG_UNSET) &&
11850 (lr2->color != REG_UNSET)) {
11851 internal_error(state, lr1->defs->def,
11852 "cannot coalesce live ranges of different colors");
11853 }
11854 color = lr1->color;
11855 if (color == REG_UNSET) {
11856 color = lr2->color;
11857 }
11858 classes = lr1->classes & lr2->classes;
11859 if (!classes) {
11860 internal_error(state, lr1->defs->def,
11861 "cannot coalesce live ranges with dissimilar register classes");
11862 }
11863 /* If there is a clear dominate live range put it in lr1,
11864 * For purposes of this test phi functions are
11865 * considered dominated by the definitions that feed into
11866 * them.
11867 */
11868 if ((lr1->defs->prev->def->op == OP_PHI) ||
11869 ((lr2->defs->prev->def->op != OP_PHI) &&
11870 tdominates(state, lr2->defs->def, lr1->defs->def))) {
11871 struct live_range *tmp;
11872 tmp = lr1;
11873 lr1 = lr2;
11874 lr2 = tmp;
11875 }
11876#if 0
11877 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
11878 fprintf(stderr, "lr1 post\n");
11879 }
11880 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
11881 fprintf(stderr, "lr1 pre\n");
11882 }
11883 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
11884 fprintf(stderr, "lr2 post\n");
11885 }
11886 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
11887 fprintf(stderr, "lr2 pre\n");
11888 }
11889#endif
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000011890#if 1
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011891 fprintf(stderr, "coalesce color1(%p): %3d color2(%p) %3d\n",
11892 lr1->defs->def,
11893 lr1->color,
11894 lr2->defs->def,
11895 lr2->color);
11896#endif
11897
11898 lr1->classes = classes;
11899 /* Append lr2 onto lr1 */
11900#warning "FIXME should this be a merge instead of a splice?"
11901 head = lr1->defs;
11902 mid1 = lr1->defs->prev;
11903 mid2 = lr2->defs;
11904 end = lr2->defs->prev;
11905
11906 head->prev = end;
11907 end->next = head;
11908
11909 mid1->next = mid2;
11910 mid2->prev = mid1;
11911
11912 /* Fixup the live range in the added live range defs */
11913 lrd = head;
11914 do {
11915 lrd->lr = lr1;
11916 lrd = lrd->next;
11917 } while(lrd != head);
11918
11919 /* Mark lr2 as free. */
11920 lr2->defs = 0;
11921 lr2->color = REG_UNNEEDED;
11922 lr2->classes = 0;
11923
11924 if (!lr1->defs) {
11925 internal_error(state, 0, "lr1->defs == 0 ?");
11926 }
11927
11928 lr1->color = color;
11929 lr1->classes = classes;
11930
11931 return lr1;
11932}
11933
11934static struct live_range_def *live_range_head(
11935 struct compile_state *state, struct live_range *lr,
11936 struct live_range_def *last)
11937{
11938 struct live_range_def *result;
11939 result = 0;
11940 if (last == 0) {
11941 result = lr->defs;
11942 }
11943 else if (!tdominates(state, lr->defs->def, last->next->def)) {
11944 result = last->next;
11945 }
11946 return result;
11947}
11948
11949static struct live_range_def *live_range_end(
11950 struct compile_state *state, struct live_range *lr,
11951 struct live_range_def *last)
11952{
11953 struct live_range_def *result;
11954 result = 0;
11955 if (last == 0) {
11956 result = lr->defs->prev;
11957 }
11958 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
11959 result = last->prev;
11960 }
11961 return result;
11962}
11963
11964
Eric Biedermanb138ac82003-04-22 18:44:01 +000011965static void initialize_live_ranges(
11966 struct compile_state *state, struct reg_state *rstate)
11967{
11968 struct triple *ins, *first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011969 size_t count, size;
11970 int i, j;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011971
Eric Biederman0babc1c2003-05-09 02:39:00 +000011972 first = RHS(state->main_function, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011973 /* First count how many instructions I have.
Eric Biedermanb138ac82003-04-22 18:44:01 +000011974 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011975 count = count_triples(state);
11976 /* Potentially I need one live range definitions for each
11977 * instruction, plus an extra for the split routines.
11978 */
11979 rstate->defs = count + 1;
11980 /* Potentially I need one live range for each instruction
11981 * plus an extra for the dummy live range.
11982 */
11983 rstate->ranges = count + 1;
11984 size = sizeof(rstate->lrd[0]) * rstate->defs;
11985 rstate->lrd = xcmalloc(size, "live_range_def");
11986 size = sizeof(rstate->lr[0]) * rstate->ranges;
11987 rstate->lr = xcmalloc(size, "live_range");
11988
Eric Biedermanb138ac82003-04-22 18:44:01 +000011989 /* Setup the dummy live range */
11990 rstate->lr[0].classes = 0;
11991 rstate->lr[0].color = REG_UNSET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011992 rstate->lr[0].defs = 0;
11993 i = j = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011994 ins = first;
11995 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011996 /* If the triple is a variable give it a live range */
Eric Biederman0babc1c2003-05-09 02:39:00 +000011997 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011998 struct reg_info info;
11999 /* Find the architecture specific color information */
12000 info = find_def_color(state, ins);
12001
Eric Biedermanb138ac82003-04-22 18:44:01 +000012002 i++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012003 rstate->lr[i].defs = &rstate->lrd[j];
12004 rstate->lr[i].color = info.reg;
12005 rstate->lr[i].classes = info.regcm;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012006 rstate->lr[i].degree = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012007 rstate->lrd[j].lr = &rstate->lr[i];
12008 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012009 /* Otherwise give the triple the dummy live range. */
12010 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012011 rstate->lrd[j].lr = &rstate->lr[0];
Eric Biedermanb138ac82003-04-22 18:44:01 +000012012 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012013
12014 /* Initalize the live_range_def */
12015 rstate->lrd[j].next = &rstate->lrd[j];
12016 rstate->lrd[j].prev = &rstate->lrd[j];
12017 rstate->lrd[j].def = ins;
12018 rstate->lrd[j].orig_id = ins->id;
12019 ins->id = j;
12020
12021 j++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012022 ins = ins->next;
12023 } while(ins != first);
12024 rstate->ranges = i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012025 rstate->defs -= 1;
12026
Eric Biedermanb138ac82003-04-22 18:44:01 +000012027 /* Make a second pass to handle achitecture specific register
12028 * constraints.
12029 */
12030 ins = first;
12031 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012032 int zlhs, zrhs, i, j;
12033 if (ins->id > rstate->defs) {
12034 internal_error(state, ins, "bad id");
12035 }
12036
12037 /* Walk through the template of ins and coalesce live ranges */
12038 zlhs = TRIPLE_LHS(ins->sizes);
12039 if ((zlhs == 0) && triple_is_def(state, ins)) {
12040 zlhs = 1;
12041 }
12042 zrhs = TRIPLE_RHS(ins->sizes);
12043
12044 for(i = 0; i < zlhs; i++) {
12045 struct reg_info linfo;
12046 struct live_range_def *lhs;
12047 linfo = arch_reg_lhs(state, ins, i);
12048 if (linfo.reg < MAX_REGISTERS) {
12049 continue;
12050 }
12051 if (triple_is_def(state, ins)) {
12052 lhs = &rstate->lrd[ins->id];
12053 } else {
12054 lhs = &rstate->lrd[LHS(ins, i)->id];
12055 }
12056 for(j = 0; j < zrhs; j++) {
12057 struct reg_info rinfo;
12058 struct live_range_def *rhs;
12059 rinfo = arch_reg_rhs(state, ins, j);
12060 if (rinfo.reg < MAX_REGISTERS) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012061 continue;
12062 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012063 rhs = &rstate->lrd[RHS(ins, i)->id];
12064 if (rinfo.reg == linfo.reg) {
12065 coalesce_ranges(state, rstate,
12066 lhs->lr, rhs->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012067 }
12068 }
12069 }
12070 ins = ins->next;
12071 } while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012072}
12073
Eric Biedermanf96a8102003-06-16 16:57:34 +000012074static void graph_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000012075 struct compile_state *state,
12076 struct reg_block *blocks, struct triple_reg_set *live,
12077 struct reg_block *rb, struct triple *ins, void *arg)
12078{
12079 struct reg_state *rstate = arg;
12080 struct live_range *def;
12081 struct triple_reg_set *entry;
12082
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012083 /* If the triple is not a definition
Eric Biedermanb138ac82003-04-22 18:44:01 +000012084 * we do not have a definition to add to
12085 * the interference graph.
12086 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012087 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000012088 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012089 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012090 def = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012091
12092 /* Create an edge between ins and everything that is
12093 * alive, unless the live_range cannot share
12094 * a physical register with ins.
12095 */
12096 for(entry = live; entry; entry = entry->next) {
12097 struct live_range *lr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012098 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
12099 internal_error(state, 0, "bad entry?");
12100 }
12101 lr = rstate->lrd[entry->member->id].lr;
12102 if (def == lr) {
12103 continue;
12104 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012105 if (!arch_regcm_intersect(def->classes, lr->classes)) {
12106 continue;
12107 }
12108 add_live_edge(rstate, def, lr);
12109 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012110 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012111}
12112
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012113static struct live_range *get_verify_live_range(
12114 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
12115{
12116 struct live_range *lr;
12117 struct live_range_def *lrd;
12118 int ins_found;
12119 if ((ins->id < 0) || (ins->id > rstate->defs)) {
12120 internal_error(state, ins, "bad ins?");
12121 }
12122 lr = rstate->lrd[ins->id].lr;
12123 ins_found = 0;
12124 lrd = lr->defs;
12125 do {
12126 if (lrd->def == ins) {
12127 ins_found = 1;
12128 }
12129 lrd = lrd->next;
12130 } while(lrd != lr->defs);
12131 if (!ins_found) {
12132 internal_error(state, ins, "ins not in live range");
12133 }
12134 return lr;
12135}
12136
12137static void verify_graph_ins(
12138 struct compile_state *state,
12139 struct reg_block *blocks, struct triple_reg_set *live,
12140 struct reg_block *rb, struct triple *ins, void *arg)
12141{
12142 struct reg_state *rstate = arg;
12143 struct triple_reg_set *entry1, *entry2;
12144
12145
12146 /* Compare live against edges and make certain the code is working */
12147 for(entry1 = live; entry1; entry1 = entry1->next) {
12148 struct live_range *lr1;
12149 lr1 = get_verify_live_range(state, rstate, entry1->member);
12150 for(entry2 = live; entry2; entry2 = entry2->next) {
12151 struct live_range *lr2;
12152 struct live_range_edge *edge2;
12153 int lr1_found;
12154 int lr2_degree;
12155 if (entry2 == entry1) {
12156 continue;
12157 }
12158 lr2 = get_verify_live_range(state, rstate, entry2->member);
12159 if (lr1 == lr2) {
12160 internal_error(state, entry2->member,
12161 "live range with 2 values simultaneously alive");
12162 }
12163 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
12164 continue;
12165 }
12166 if (!interfere(rstate, lr1, lr2)) {
12167 internal_error(state, entry2->member,
12168 "edges don't interfere?");
12169 }
12170
12171 lr1_found = 0;
12172 lr2_degree = 0;
12173 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
12174 lr2_degree++;
12175 if (edge2->node == lr1) {
12176 lr1_found = 1;
12177 }
12178 }
12179 if (lr2_degree != lr2->degree) {
12180 internal_error(state, entry2->member,
12181 "computed degree: %d does not match reported degree: %d\n",
12182 lr2_degree, lr2->degree);
12183 }
12184 if (!lr1_found) {
12185 internal_error(state, entry2->member, "missing edge");
12186 }
12187 }
12188 }
12189 return;
12190}
12191
Eric Biedermanb138ac82003-04-22 18:44:01 +000012192
Eric Biedermanf96a8102003-06-16 16:57:34 +000012193static void print_interference_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000012194 struct compile_state *state,
12195 struct reg_block *blocks, struct triple_reg_set *live,
12196 struct reg_block *rb, struct triple *ins, void *arg)
12197{
12198 struct reg_state *rstate = arg;
12199 struct live_range *lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012200 unsigned id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012201
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012202 lr = rstate->lrd[ins->id].lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012203 id = ins->id;
12204 ins->id = rstate->lrd[id].orig_id;
12205 SET_REG(ins->id, lr->color);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012206 display_triple(stdout, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012207 ins->id = id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012208
12209 if (lr->defs) {
12210 struct live_range_def *lrd;
12211 printf(" range:");
12212 lrd = lr->defs;
12213 do {
12214 printf(" %-10p", lrd->def);
12215 lrd = lrd->next;
12216 } while(lrd != lr->defs);
12217 printf("\n");
12218 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012219 if (live) {
12220 struct triple_reg_set *entry;
12221 printf(" live:");
12222 for(entry = live; entry; entry = entry->next) {
12223 printf(" %-10p", entry->member);
12224 }
12225 printf("\n");
12226 }
12227 if (lr->edges) {
12228 struct live_range_edge *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012229 printf(" edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000012230 for(entry = lr->edges; entry; entry = entry->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012231 struct live_range_def *lrd;
12232 lrd = entry->node->defs;
12233 do {
12234 printf(" %-10p", lrd->def);
12235 lrd = lrd->next;
12236 } while(lrd != entry->node->defs);
12237 printf("|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000012238 }
12239 printf("\n");
12240 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012241 if (triple_is_branch(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012242 printf("\n");
12243 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012244 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012245}
12246
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012247static int coalesce_live_ranges(
12248 struct compile_state *state, struct reg_state *rstate)
12249{
12250 /* At the point where a value is moved from one
12251 * register to another that value requires two
12252 * registers, thus increasing register pressure.
12253 * Live range coaleescing reduces the register
12254 * pressure by keeping a value in one register
12255 * longer.
12256 *
12257 * In the case of a phi function all paths leading
12258 * into it must be allocated to the same register
12259 * otherwise the phi function may not be removed.
12260 *
12261 * Forcing a value to stay in a single register
12262 * for an extended period of time does have
12263 * limitations when applied to non homogenous
12264 * register pool.
12265 *
12266 * The two cases I have identified are:
12267 * 1) Two forced register assignments may
12268 * collide.
12269 * 2) Registers may go unused because they
12270 * are only good for storing the value
12271 * and not manipulating it.
12272 *
12273 * Because of this I need to split live ranges,
12274 * even outside of the context of coalesced live
12275 * ranges. The need to split live ranges does
12276 * impose some constraints on live range coalescing.
12277 *
12278 * - Live ranges may not be coalesced across phi
12279 * functions. This creates a 2 headed live
12280 * range that cannot be sanely split.
12281 *
12282 * - phi functions (coalesced in initialize_live_ranges)
12283 * are handled as pre split live ranges so we will
12284 * never attempt to split them.
12285 */
12286 int coalesced;
12287 int i;
12288
12289 coalesced = 0;
12290 for(i = 0; i <= rstate->ranges; i++) {
12291 struct live_range *lr1;
12292 struct live_range_def *lrd1;
12293 lr1 = &rstate->lr[i];
12294 if (!lr1->defs) {
12295 continue;
12296 }
12297 lrd1 = live_range_end(state, lr1, 0);
12298 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
12299 struct triple_set *set;
12300 if (lrd1->def->op != OP_COPY) {
12301 continue;
12302 }
12303 /* Skip copies that are the result of a live range split. */
12304 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
12305 continue;
12306 }
12307 for(set = lrd1->def->use; set; set = set->next) {
12308 struct live_range_def *lrd2;
12309 struct live_range *lr2, *res;
12310
12311 lrd2 = &rstate->lrd[set->member->id];
12312
12313 /* Don't coalesce with instructions
12314 * that are the result of a live range
12315 * split.
12316 */
12317 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
12318 continue;
12319 }
12320 lr2 = rstate->lrd[set->member->id].lr;
12321 if (lr1 == lr2) {
12322 continue;
12323 }
12324 if ((lr1->color != lr2->color) &&
12325 (lr1->color != REG_UNSET) &&
12326 (lr2->color != REG_UNSET)) {
12327 continue;
12328 }
12329 if ((lr1->classes & lr2->classes) == 0) {
12330 continue;
12331 }
12332
12333 if (interfere(rstate, lr1, lr2)) {
12334 continue;
12335 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000012336
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012337 res = coalesce_ranges(state, rstate, lr1, lr2);
12338 coalesced += 1;
12339 if (res != lr1) {
12340 goto next;
12341 }
12342 }
12343 }
12344 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000012345 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012346 }
12347 return coalesced;
12348}
12349
12350
Eric Biedermanf96a8102003-06-16 16:57:34 +000012351static void fix_coalesce_conflicts(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012352 struct reg_block *blocks, struct triple_reg_set *live,
12353 struct reg_block *rb, struct triple *ins, void *arg)
12354{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012355 int zlhs, zrhs, i, j;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012356
12357 /* See if we have a mandatory coalesce operation between
12358 * a lhs and a rhs value. If so and the rhs value is also
12359 * alive then this triple needs to be pre copied. Otherwise
12360 * we would have two definitions in the same live range simultaneously
12361 * alive.
12362 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012363 zlhs = TRIPLE_LHS(ins->sizes);
12364 if ((zlhs == 0) && triple_is_def(state, ins)) {
12365 zlhs = 1;
12366 }
12367 zrhs = TRIPLE_RHS(ins->sizes);
Eric Biedermanf96a8102003-06-16 16:57:34 +000012368 for(i = 0; i < zlhs; i++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012369 struct reg_info linfo;
12370 linfo = arch_reg_lhs(state, ins, i);
12371 if (linfo.reg < MAX_REGISTERS) {
12372 continue;
12373 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012374 for(j = 0; j < zrhs; j++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012375 struct reg_info rinfo;
12376 struct triple *rhs;
12377 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000012378 int found;
12379 found = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012380 rinfo = arch_reg_rhs(state, ins, j);
12381 if (rinfo.reg != linfo.reg) {
12382 continue;
12383 }
12384 rhs = RHS(ins, j);
Eric Biedermanf96a8102003-06-16 16:57:34 +000012385 for(set = live; set && !found; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012386 if (set->member == rhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000012387 found = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012388 }
12389 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012390 if (found) {
12391 struct triple *copy;
12392 copy = pre_copy(state, ins, j);
12393 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12394 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012395 }
12396 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012397 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012398}
12399
Eric Biedermanf96a8102003-06-16 16:57:34 +000012400static void replace_set_use(struct compile_state *state,
12401 struct triple_reg_set *head, struct triple *orig, struct triple *new)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012402{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012403 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000012404 for(set = head; set; set = set->next) {
12405 if (set->member == orig) {
12406 set->member = new;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012407 }
12408 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012409}
12410
Eric Biedermanf96a8102003-06-16 16:57:34 +000012411static void replace_block_use(struct compile_state *state,
12412 struct reg_block *blocks, struct triple *orig, struct triple *new)
12413{
12414 int i;
12415#warning "WISHLIST visit just those blocks that need it *"
12416 for(i = 1; i <= state->last_vertex; i++) {
12417 struct reg_block *rb;
12418 rb = &blocks[i];
12419 replace_set_use(state, rb->in, orig, new);
12420 replace_set_use(state, rb->out, orig, new);
12421 }
12422}
12423
12424static void color_instructions(struct compile_state *state)
12425{
12426 struct triple *ins, *first;
12427 first = RHS(state->main_function, 0);
12428 ins = first;
12429 do {
12430 if (triple_is_def(state, ins)) {
12431 struct reg_info info;
12432 info = find_lhs_color(state, ins, 0);
12433 if (info.reg >= MAX_REGISTERS) {
12434 info.reg = REG_UNSET;
12435 }
12436 SET_INFO(ins->id, info);
12437 }
12438 ins = ins->next;
12439 } while(ins != first);
12440}
12441
12442static struct reg_info read_lhs_color(
12443 struct compile_state *state, struct triple *ins, int index)
12444{
12445 struct reg_info info;
12446 if ((index == 0) && triple_is_def(state, ins)) {
12447 info.reg = ID_REG(ins->id);
12448 info.regcm = ID_REGCM(ins->id);
12449 }
12450 else if (index < TRIPLE_LHS(ins->sizes)) {
12451 info = read_lhs_color(state, LHS(ins, index), 0);
12452 }
12453 else {
12454 internal_error(state, ins, "Bad lhs %d", index);
12455 info.reg = REG_UNSET;
12456 info.regcm = 0;
12457 }
12458 return info;
12459}
12460
12461static struct triple *resolve_tangle(
12462 struct compile_state *state, struct triple *tangle)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012463{
12464 struct reg_info info, uinfo;
12465 struct triple_set *set, *next;
12466 struct triple *copy;
12467
Eric Biedermanf96a8102003-06-16 16:57:34 +000012468#warning "WISHLIST recalculate all affected instructions colors"
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012469 info = find_lhs_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012470 for(set = tangle->use; set; set = next) {
12471 struct triple *user;
12472 int i, zrhs;
12473 next = set->next;
12474 user = set->member;
12475 zrhs = TRIPLE_RHS(user->sizes);
12476 for(i = 0; i < zrhs; i++) {
12477 if (RHS(user, i) != tangle) {
12478 continue;
12479 }
12480 uinfo = find_rhs_post_color(state, user, i);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012481 if (uinfo.reg == info.reg) {
12482 copy = pre_copy(state, user, i);
12483 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000012484 SET_INFO(copy->id, uinfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012485 }
12486 }
12487 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012488 copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012489 uinfo = find_lhs_pre_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012490 if (uinfo.reg == info.reg) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000012491 struct reg_info linfo;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012492 copy = post_copy(state, tangle);
12493 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000012494 linfo = find_lhs_color(state, copy, 0);
12495 SET_INFO(copy->id, linfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012496 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012497 info = find_lhs_color(state, tangle, 0);
12498 SET_INFO(tangle->id, info);
12499
12500 return copy;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012501}
12502
12503
Eric Biedermanf96a8102003-06-16 16:57:34 +000012504static void fix_tangles(struct compile_state *state,
12505 struct reg_block *blocks, struct triple_reg_set *live,
12506 struct reg_block *rb, struct triple *ins, void *arg)
12507{
12508 struct triple *tangle;
12509 do {
12510 char used[MAX_REGISTERS];
12511 struct triple_reg_set *set;
12512 tangle = 0;
12513
12514 /* Find out which registers have multiple uses at this point */
12515 memset(used, 0, sizeof(used));
12516 for(set = live; set; set = set->next) {
12517 struct reg_info info;
12518 info = read_lhs_color(state, set->member, 0);
12519 if (info.reg == REG_UNSET) {
12520 continue;
12521 }
12522 reg_inc_used(state, used, info.reg);
12523 }
12524
12525 /* Now find the least dominated definition of a register in
12526 * conflict I have seen so far.
12527 */
12528 for(set = live; set; set = set->next) {
12529 struct reg_info info;
12530 info = read_lhs_color(state, set->member, 0);
12531 if (used[info.reg] < 2) {
12532 continue;
12533 }
12534 if (!tangle || tdominates(state, set->member, tangle)) {
12535 tangle = set->member;
12536 }
12537 }
12538 /* If I have found a tangle resolve it */
12539 if (tangle) {
12540 struct triple *post_copy;
12541 post_copy = resolve_tangle(state, tangle);
12542 if (post_copy) {
12543 replace_block_use(state, blocks, tangle, post_copy);
12544 }
12545 if (post_copy && (tangle != ins)) {
12546 replace_set_use(state, live, tangle, post_copy);
12547 }
12548 }
12549 } while(tangle);
12550 return;
12551}
12552
12553static void correct_tangles(
12554 struct compile_state *state, struct reg_block *blocks)
12555{
12556 color_instructions(state);
12557 walk_variable_lifetimes(state, blocks, fix_tangles, 0);
12558}
12559
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012560struct least_conflict {
12561 struct reg_state *rstate;
12562 struct live_range *ref_range;
12563 struct triple *ins;
12564 struct triple_reg_set *live;
12565 size_t count;
Eric Biedermand3283ec2003-06-18 11:03:18 +000012566 int constraints;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012567};
Eric Biedermanf96a8102003-06-16 16:57:34 +000012568static void least_conflict(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012569 struct reg_block *blocks, struct triple_reg_set *live,
12570 struct reg_block *rb, struct triple *ins, void *arg)
12571{
12572 struct least_conflict *conflict = arg;
12573 struct live_range_edge *edge;
12574 struct triple_reg_set *set;
12575 size_t count;
Eric Biedermand3283ec2003-06-18 11:03:18 +000012576 int constraints;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012577
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012578#warning "FIXME handle instructions with left hand sides..."
12579 /* Only instructions that introduce a new definition
12580 * can be the conflict instruction.
12581 */
12582 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000012583 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012584 }
12585
12586 /* See if live ranges at this instruction are a
12587 * strict subset of the live ranges that are in conflict.
12588 */
12589 count = 0;
12590 for(set = live; set; set = set->next) {
12591 struct live_range *lr;
12592 lr = conflict->rstate->lrd[set->member->id].lr;
Eric Biedermand3283ec2003-06-18 11:03:18 +000012593 /* Ignore it if there cannot be an edge between these two nodes */
12594 if (!arch_regcm_intersect(conflict->ref_range->classes, lr->classes)) {
12595 continue;
12596 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012597 for(edge = conflict->ref_range->edges; edge; edge = edge->next) {
12598 if (edge->node == lr) {
12599 break;
12600 }
12601 }
12602 if (!edge && (lr != conflict->ref_range)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000012603 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012604 }
12605 count++;
12606 }
12607 if (count <= 1) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000012608 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012609 }
12610
Eric Biedermand3283ec2003-06-18 11:03:18 +000012611#if 0
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012612 /* See if there is an uncolored member in this subset.
12613 */
12614 for(set = live; set; set = set->next) {
12615 struct live_range *lr;
12616 lr = conflict->rstate->lrd[set->member->id].lr;
12617 if (lr->color == REG_UNSET) {
12618 break;
12619 }
12620 }
12621 if (!set && (conflict->ref_range != REG_UNSET)) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000012622 return;
12623 }
12624#endif
12625
12626 /* See if any of the live registers are constrained,
12627 * if not it won't be productive to pick this as
12628 * a conflict instruction.
12629 */
12630 constraints = 0;
12631 for(set = live; set; set = set->next) {
12632 struct triple_set *uset;
12633 struct reg_info info;
12634 unsigned classes;
12635 unsigned cur_size, size;
12636 /* Skip this instruction */
12637 if (set->member == ins) {
12638 continue;
12639 }
12640 /* Find how many registers this value can potentially
12641 * be assigned to.
12642 */
12643 classes = arch_type_to_regcm(state, set->member->type);
12644 size = regc_max_size(state, classes);
12645
12646 /* Find how many registers we allow this value to
12647 * be assigned to.
12648 */
12649 info = arch_reg_lhs(state, set->member, 0);
12650
12651 /* If the value does not live in a register it
12652 * isn't constrained.
12653 */
12654 if (info.reg == REG_UNNEEDED) {
12655 continue;
12656 }
12657
12658 if ((info.reg == REG_UNSET) || (info.reg >= MAX_REGISTERS)) {
12659 cur_size = regc_max_size(state, info.regcm);
12660 } else {
12661 cur_size = 1;
12662 }
12663
12664 /* If there is no difference between potential and
12665 * actual register count there is not a constraint
12666 */
12667 if (cur_size >= size) {
12668 continue;
12669 }
12670
12671 /* If this live_range feeds into conflict->inds
12672 * it isn't a constraint we can relieve.
12673 */
12674 for(uset = set->member->use; uset; uset = uset->next) {
12675 if (uset->member == ins) {
12676 break;
12677 }
12678 }
12679 if (uset) {
12680 continue;
12681 }
12682 constraints = 1;
12683 break;
12684 }
12685 /* Don't drop canidates with constraints */
12686 if (conflict->constraints && !constraints) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000012687 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012688 }
12689
12690
Eric Biedermand3283ec2003-06-18 11:03:18 +000012691#if 0
12692 fprintf(stderr, "conflict ins? %p %s count: %d constraints: %d\n",
12693 ins, tops(ins->op), count, constraints);
12694#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012695 /* Find the instruction with the largest possible subset of
12696 * conflict ranges and that dominates any other instruction
12697 * with an equal sized set of conflicting ranges.
12698 */
12699 if ((count > conflict->count) ||
12700 ((count == conflict->count) &&
12701 tdominates(state, ins, conflict->ins))) {
12702 struct triple_reg_set *next;
12703 /* Remember the canidate instruction */
12704 conflict->ins = ins;
12705 conflict->count = count;
Eric Biedermand3283ec2003-06-18 11:03:18 +000012706 conflict->constraints = constraints;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012707 /* Free the old collection of live registers */
12708 for(set = conflict->live; set; set = next) {
12709 next = set->next;
12710 do_triple_unset(&conflict->live, set->member);
12711 }
12712 conflict->live = 0;
12713 /* Rember the registers that are alive but do not feed
12714 * into or out of conflict->ins.
12715 */
12716 for(set = live; set; set = set->next) {
12717 struct triple **expr;
12718 if (set->member == ins) {
12719 goto next;
12720 }
12721 expr = triple_rhs(state, ins, 0);
12722 for(;expr; expr = triple_rhs(state, ins, expr)) {
12723 if (*expr == set->member) {
12724 goto next;
12725 }
12726 }
12727 expr = triple_lhs(state, ins, 0);
12728 for(; expr; expr = triple_lhs(state, ins, expr)) {
12729 if (*expr == set->member) {
12730 goto next;
12731 }
12732 }
12733 do_triple_set(&conflict->live, set->member, set->new);
12734 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000012735 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012736 }
12737 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000012738 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012739}
12740
12741static void find_range_conflict(struct compile_state *state,
12742 struct reg_state *rstate, char *used, struct live_range *ref_range,
12743 struct least_conflict *conflict)
12744{
Eric Biederman8d9c1232003-06-17 08:42:17 +000012745
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012746 /* there are 3 kinds ways conflicts can occure.
12747 * 1) the life time of 2 values simply overlap.
12748 * 2) the 2 values feed into the same instruction.
12749 * 3) the 2 values feed into a phi function.
12750 */
12751
12752 /* find the instruction where the problematic conflict comes
12753 * into existance. that the instruction where all of
12754 * the values are alive, and among such instructions it is
12755 * the least dominated one.
12756 *
12757 * a value is alive an an instruction if either;
12758 * 1) the value defintion dominates the instruction and there
12759 * is a use at or after that instrction
12760 * 2) the value definition feeds into a phi function in the
12761 * same block as the instruction. and the phi function
12762 * is at or after the instruction.
12763 */
12764 memset(conflict, 0, sizeof(*conflict));
Eric Biedermand3283ec2003-06-18 11:03:18 +000012765 conflict->rstate = rstate;
12766 conflict->ref_range = ref_range;
12767 conflict->ins = 0;
12768 conflict->live = 0;
12769 conflict->count = 0;
12770 conflict->constraints = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012771 walk_variable_lifetimes(state, rstate->blocks, least_conflict, conflict);
12772
12773 if (!conflict->ins) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000012774 internal_error(state, ref_range->defs->def, "No conflict ins?");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012775 }
12776 if (!conflict->live) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000012777 internal_error(state, ref_range->defs->def, "No conflict live?");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012778 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000012779#if 0
12780 fprintf(stderr, "conflict ins: %p %s count: %d constraints: %d\n",
12781 conflict->ins, tops(conflict->ins->op),
12782 conflict->count, conflict->constraints);
12783#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012784 return;
12785}
12786
12787static struct triple *split_constrained_range(struct compile_state *state,
12788 struct reg_state *rstate, char *used, struct least_conflict *conflict)
12789{
12790 unsigned constrained_size;
12791 struct triple *new, *constrained;
12792 struct triple_reg_set *cset;
12793 /* Find a range that is having problems because it is
12794 * artificially constrained.
12795 */
12796 constrained_size = ~0;
12797 constrained = 0;
12798 new = 0;
12799 for(cset = conflict->live; cset; cset = cset->next) {
12800 struct triple_set *set;
12801 struct reg_info info;
12802 unsigned classes;
12803 unsigned cur_size, size;
12804 /* Skip the live range that starts with conflict->ins */
12805 if (cset->member == conflict->ins) {
12806 continue;
12807 }
12808 /* Find how many registers this value can potentially
12809 * be assigned to.
12810 */
12811 classes = arch_type_to_regcm(state, cset->member->type);
12812 size = regc_max_size(state, classes);
12813
12814 /* Find how many registers we allow this value to
12815 * be assigned to.
12816 */
12817 info = arch_reg_lhs(state, cset->member, 0);
Eric Biedermand3283ec2003-06-18 11:03:18 +000012818
12819 /* If the register doesn't need a register
12820 * splitting it can't help.
12821 */
12822 if (info.reg == REG_UNNEEDED) {
12823 continue;
12824 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012825#warning "FIXME do I need a call to arch_reg_rhs around here somewhere?"
12826 if ((info.reg == REG_UNSET) || (info.reg >= MAX_REGISTERS)) {
12827 cur_size = regc_max_size(state, info.regcm);
12828 } else {
12829 cur_size = 1;
12830 }
12831 /* If this live_range feeds into conflict->ins
12832 * splitting it is unlikely to help.
12833 */
12834 for(set = cset->member->use; set; set = set->next) {
12835 if (set->member == conflict->ins) {
12836 goto next;
12837 }
12838 }
12839
12840 /* If there is no difference between potential and
12841 * actual register count there is nothing to do.
12842 */
12843 if (cur_size >= size) {
12844 continue;
12845 }
12846 /* Of the constrained registers deal with the
12847 * most constrained one first.
12848 */
12849 if (!constrained ||
12850 (size < constrained_size)) {
12851 constrained = cset->member;
12852 constrained_size = size;
12853 }
12854 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000012855 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012856 }
12857 if (constrained) {
12858 new = post_copy(state, constrained);
12859 new->id |= TRIPLE_FLAG_POST_SPLIT;
12860 }
12861 return new;
12862}
12863
12864static int split_ranges(
12865 struct compile_state *state, struct reg_state *rstate,
12866 char *used, struct live_range *range)
12867{
12868 struct triple *new;
12869
Eric Biedermand3283ec2003-06-18 11:03:18 +000012870#if 0
12871 fprintf(stderr, "split_ranges %d %s %p\n",
12872 rstate->passes, tops(range->defs->def->op), range->defs->def);
12873#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012874 if ((range->color == REG_UNNEEDED) ||
12875 (rstate->passes >= rstate->max_passes)) {
12876 return 0;
12877 }
12878 new = 0;
12879 /* If I can't allocate a register something needs to be split */
12880 if (arch_select_free_register(state, used, range->classes) == REG_UNSET) {
12881 struct least_conflict conflict;
12882
Eric Biedermand3283ec2003-06-18 11:03:18 +000012883#if 0
12884 fprintf(stderr, "find_range_conflict\n");
12885#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012886 /* Find where in the set of registers the conflict
12887 * actually occurs.
12888 */
12889 find_range_conflict(state, rstate, used, range, &conflict);
12890
12891 /* If a range has been artifically constrained split it */
12892 new = split_constrained_range(state, rstate, used, &conflict);
12893
12894 if (!new) {
12895 /* Ideally I would split the live range that will not be used
12896 * for the longest period of time in hopes that this will
12897 * (a) allow me to spill a register or
12898 * (b) allow me to place a value in another register.
12899 *
12900 * So far I don't have a test case for this, the resolving
12901 * of mandatory constraints has solved all of my
12902 * know issues. So I have choosen not to write any
12903 * code until I cat get a better feel for cases where
12904 * it would be useful to have.
12905 *
12906 */
12907#warning "WISHLIST implement live range splitting..."
Eric Biedermand3283ec2003-06-18 11:03:18 +000012908#if 0
12909 print_blocks(state, stderr);
12910 print_dominators(state, stderr);
12911
12912#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012913 return 0;
12914 }
12915 }
12916 if (new) {
12917 rstate->lrd[rstate->defs].orig_id = new->id;
12918 new->id = rstate->defs;
12919 rstate->defs++;
12920#if 0
Eric Biedermand3283ec2003-06-18 11:03:18 +000012921 fprintf(stderr, "new: %p old: %s %p\n",
12922 new, tops(RHS(new, 0)->op), RHS(new, 0));
12923#endif
12924#if 0
12925 print_blocks(state, stderr);
12926 print_dominators(state, stderr);
12927
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012928#endif
12929 return 1;
12930 }
12931 return 0;
12932}
12933
Eric Biedermanb138ac82003-04-22 18:44:01 +000012934#if DEBUG_COLOR_GRAPH > 1
12935#define cgdebug_printf(...) fprintf(stdout, __VA_ARGS__)
12936#define cgdebug_flush() fflush(stdout)
12937#elif DEBUG_COLOR_GRAPH == 1
12938#define cgdebug_printf(...) fprintf(stderr, __VA_ARGS__)
12939#define cgdebug_flush() fflush(stderr)
12940#else
12941#define cgdebug_printf(...)
12942#define cgdebug_flush()
12943#endif
12944
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012945
12946static int select_free_color(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000012947 struct reg_state *rstate, struct live_range *range)
12948{
12949 struct triple_set *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012950 struct live_range_def *lrd;
12951 struct live_range_def *phi;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012952 struct live_range_edge *edge;
12953 char used[MAX_REGISTERS];
12954 struct triple **expr;
12955
Eric Biedermanb138ac82003-04-22 18:44:01 +000012956 /* Instead of doing just the trivial color select here I try
12957 * a few extra things because a good color selection will help reduce
12958 * copies.
12959 */
12960
12961 /* Find the registers currently in use */
12962 memset(used, 0, sizeof(used));
12963 for(edge = range->edges; edge; edge = edge->next) {
12964 if (edge->node->color == REG_UNSET) {
12965 continue;
12966 }
12967 reg_fill_used(state, used, edge->node->color);
12968 }
12969#if DEBUG_COLOR_GRAPH > 1
12970 {
12971 int i;
12972 i = 0;
12973 for(edge = range->edges; edge; edge = edge->next) {
12974 i++;
12975 }
12976 cgdebug_printf("\n%s edges: %d @%s:%d.%d\n",
12977 tops(range->def->op), i,
12978 range->def->filename, range->def->line, range->def->col);
12979 for(i = 0; i < MAX_REGISTERS; i++) {
12980 if (used[i]) {
12981 cgdebug_printf("used: %s\n",
12982 arch_reg_str(i));
12983 }
12984 }
12985 }
12986#endif
12987
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012988#warning "FIXME detect conflicts caused by the source and destination being the same register"
12989
12990 /* If a color is already assigned see if it will work */
12991 if (range->color != REG_UNSET) {
12992 struct live_range_def *lrd;
12993 if (!used[range->color]) {
12994 return 1;
12995 }
12996 for(edge = range->edges; edge; edge = edge->next) {
12997 if (edge->node->color != range->color) {
12998 continue;
12999 }
13000 warning(state, edge->node->defs->def, "edge: ");
13001 lrd = edge->node->defs;
13002 do {
13003 warning(state, lrd->def, " %p %s",
13004 lrd->def, tops(lrd->def->op));
13005 lrd = lrd->next;
13006 } while(lrd != edge->node->defs);
13007 }
13008 lrd = range->defs;
13009 warning(state, range->defs->def, "def: ");
13010 do {
13011 warning(state, lrd->def, " %p %s",
13012 lrd->def, tops(lrd->def->op));
13013 lrd = lrd->next;
13014 } while(lrd != range->defs);
13015 internal_error(state, range->defs->def,
13016 "live range with already used color %s",
13017 arch_reg_str(range->color));
13018 }
13019
Eric Biedermanb138ac82003-04-22 18:44:01 +000013020 /* If I feed into an expression reuse it's color.
13021 * This should help remove copies in the case of 2 register instructions
13022 * and phi functions.
13023 */
13024 phi = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013025 lrd = live_range_end(state, range, 0);
13026 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
13027 entry = lrd->def->use;
13028 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
13029 struct live_range_def *insd;
13030 insd = &rstate->lrd[entry->member->id];
13031 if (insd->lr->defs == 0) {
13032 continue;
13033 }
13034 if (!phi && (insd->def->op == OP_PHI) &&
13035 !interfere(rstate, range, insd->lr)) {
13036 phi = insd;
13037 }
13038 if ((insd->lr->color == REG_UNSET) ||
13039 ((insd->lr->classes & range->classes) == 0) ||
13040 (used[insd->lr->color])) {
13041 continue;
13042 }
13043 if (interfere(rstate, range, insd->lr)) {
13044 continue;
13045 }
13046 range->color = insd->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013047 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013048 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013049 /* If I feed into a phi function reuse it's color or the color
Eric Biedermanb138ac82003-04-22 18:44:01 +000013050 * of something else that feeds into the phi function.
13051 */
13052 if (phi) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013053 if (phi->lr->color != REG_UNSET) {
13054 if (used[phi->lr->color]) {
13055 range->color = phi->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013056 }
13057 }
13058 else {
13059 expr = triple_rhs(state, phi->def, 0);
13060 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
13061 struct live_range *lr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013062 if (!*expr) {
13063 continue;
13064 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013065 lr = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013066 if ((lr->color == REG_UNSET) ||
13067 ((lr->classes & range->classes) == 0) ||
13068 (used[lr->color])) {
13069 continue;
13070 }
13071 if (interfere(rstate, range, lr)) {
13072 continue;
13073 }
13074 range->color = lr->color;
13075 }
13076 }
13077 }
13078 /* If I don't interfere with a rhs node reuse it's color */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013079 lrd = live_range_head(state, range, 0);
13080 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
13081 expr = triple_rhs(state, lrd->def, 0);
13082 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013083 struct live_range *lr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013084 if (!*expr) {
13085 continue;
13086 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013087 lr = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013088 if ((lr->color == -1) ||
13089 ((lr->classes & range->classes) == 0) ||
13090 (used[lr->color])) {
13091 continue;
13092 }
13093 if (interfere(rstate, range, lr)) {
13094 continue;
13095 }
13096 range->color = lr->color;
13097 break;
13098 }
13099 }
13100 /* If I have not opportunitically picked a useful color
13101 * pick the first color that is free.
13102 */
13103 if (range->color == REG_UNSET) {
13104 range->color =
13105 arch_select_free_register(state, used, range->classes);
13106 }
13107 if (range->color == REG_UNSET) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000013108 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013109 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013110 if (split_ranges(state, rstate, used, range)) {
13111 return 0;
13112 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013113 for(edge = range->edges; edge; edge = edge->next) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000013114 warning(state, edge->node->defs->def, "edge reg %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000013115 arch_reg_str(edge->node->color));
Eric Biedermand3283ec2003-06-18 11:03:18 +000013116 lrd = edge->node->defs;
13117 do {
13118 warning(state, lrd->def, " %s",
13119 tops(lrd->def->op));
13120 lrd = lrd->next;
13121 } while(lrd != edge->node->defs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013122 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000013123 warning(state, range->defs->def, "range: ");
13124 lrd = range->defs;
13125 do {
13126 warning(state, lrd->def, " %s",
13127 tops(lrd->def->op));
13128 lrd = lrd->next;
13129 } while(lrd != range->defs);
13130
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013131 warning(state, range->defs->def, "classes: %x",
Eric Biedermanb138ac82003-04-22 18:44:01 +000013132 range->classes);
13133 for(i = 0; i < MAX_REGISTERS; i++) {
13134 if (used[i]) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013135 warning(state, range->defs->def, "used: %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000013136 arch_reg_str(i));
13137 }
13138 }
13139#if DEBUG_COLOR_GRAPH < 2
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013140 error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013141#else
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013142 internal_error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013143#endif
13144 }
13145 range->classes = arch_reg_regcm(state, range->color);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013146 if (range->color == -1) {
13147 internal_error(state, range->defs->def, "select_free_color did not?");
13148 }
13149 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013150}
13151
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013152static int color_graph(struct compile_state *state, struct reg_state *rstate)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013153{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013154 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013155 struct live_range_edge *edge;
13156 struct live_range *range;
13157 if (rstate->low) {
13158 cgdebug_printf("Lo: ");
13159 range = rstate->low;
13160 if (*range->group_prev != range) {
13161 internal_error(state, 0, "lo: *prev != range?");
13162 }
13163 *range->group_prev = range->group_next;
13164 if (range->group_next) {
13165 range->group_next->group_prev = range->group_prev;
13166 }
13167 if (&range->group_next == rstate->low_tail) {
13168 rstate->low_tail = range->group_prev;
13169 }
13170 if (rstate->low == range) {
13171 internal_error(state, 0, "low: next != prev?");
13172 }
13173 }
13174 else if (rstate->high) {
13175 cgdebug_printf("Hi: ");
13176 range = rstate->high;
13177 if (*range->group_prev != range) {
13178 internal_error(state, 0, "hi: *prev != range?");
13179 }
13180 *range->group_prev = range->group_next;
13181 if (range->group_next) {
13182 range->group_next->group_prev = range->group_prev;
13183 }
13184 if (&range->group_next == rstate->high_tail) {
13185 rstate->high_tail = range->group_prev;
13186 }
13187 if (rstate->high == range) {
13188 internal_error(state, 0, "high: next != prev?");
13189 }
13190 }
13191 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013192 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013193 }
13194 cgdebug_printf(" %d\n", range - rstate->lr);
13195 range->group_prev = 0;
13196 for(edge = range->edges; edge; edge = edge->next) {
13197 struct live_range *node;
13198 node = edge->node;
13199 /* Move nodes from the high to the low list */
13200 if (node->group_prev && (node->color == REG_UNSET) &&
13201 (node->degree == regc_max_size(state, node->classes))) {
13202 if (*node->group_prev != node) {
13203 internal_error(state, 0, "move: *prev != node?");
13204 }
13205 *node->group_prev = node->group_next;
13206 if (node->group_next) {
13207 node->group_next->group_prev = node->group_prev;
13208 }
13209 if (&node->group_next == rstate->high_tail) {
13210 rstate->high_tail = node->group_prev;
13211 }
13212 cgdebug_printf("Moving...%d to low\n", node - rstate->lr);
13213 node->group_prev = rstate->low_tail;
13214 node->group_next = 0;
13215 *rstate->low_tail = node;
13216 rstate->low_tail = &node->group_next;
13217 if (*node->group_prev != node) {
13218 internal_error(state, 0, "move2: *prev != node?");
13219 }
13220 }
13221 node->degree -= 1;
13222 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013223 colored = color_graph(state, rstate);
13224 if (colored) {
13225 cgdebug_printf("Coloring %d @%s:%d.%d:",
13226 range - rstate->lr,
13227 range->def->filename, range->def->line, range->def->col);
13228 cgdebug_flush();
13229 colored = select_free_color(state, rstate, range);
13230 cgdebug_printf(" %s\n", arch_reg_str(range->color));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013231 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013232 return colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013233}
13234
Eric Biedermana96d6a92003-05-13 20:45:19 +000013235static void verify_colors(struct compile_state *state, struct reg_state *rstate)
13236{
13237 struct live_range *lr;
13238 struct live_range_edge *edge;
13239 struct triple *ins, *first;
13240 char used[MAX_REGISTERS];
13241 first = RHS(state->main_function, 0);
13242 ins = first;
13243 do {
13244 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013245 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000013246 internal_error(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013247 "triple without a live range def");
Eric Biedermana96d6a92003-05-13 20:45:19 +000013248 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013249 lr = rstate->lrd[ins->id].lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000013250 if (lr->color == REG_UNSET) {
13251 internal_error(state, ins,
13252 "triple without a color");
13253 }
13254 /* Find the registers used by the edges */
13255 memset(used, 0, sizeof(used));
13256 for(edge = lr->edges; edge; edge = edge->next) {
13257 if (edge->node->color == REG_UNSET) {
13258 internal_error(state, 0,
13259 "live range without a color");
13260 }
13261 reg_fill_used(state, used, edge->node->color);
13262 }
13263 if (used[lr->color]) {
13264 internal_error(state, ins,
13265 "triple with already used color");
13266 }
13267 }
13268 ins = ins->next;
13269 } while(ins != first);
13270}
13271
Eric Biedermanb138ac82003-04-22 18:44:01 +000013272static void color_triples(struct compile_state *state, struct reg_state *rstate)
13273{
13274 struct live_range *lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000013275 struct triple *first, *ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013276 first = RHS(state->main_function, 0);
Eric Biedermana96d6a92003-05-13 20:45:19 +000013277 ins = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013278 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013279 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000013280 internal_error(state, ins,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013281 "triple without a live range");
13282 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013283 lr = rstate->lrd[ins->id].lr;
13284 SET_REG(ins->id, lr->color);
Eric Biedermana96d6a92003-05-13 20:45:19 +000013285 ins = ins->next;
13286 } while (ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013287}
13288
13289static void print_interference_block(
13290 struct compile_state *state, struct block *block, void *arg)
13291
13292{
13293 struct reg_state *rstate = arg;
13294 struct reg_block *rb;
13295 struct triple *ptr;
13296 int phi_present;
13297 int done;
13298 rb = &rstate->blocks[block->vertex];
13299
13300 printf("\nblock: %p (%d), %p<-%p %p<-%p\n",
13301 block,
13302 block->vertex,
13303 block->left,
13304 block->left && block->left->use?block->left->use->member : 0,
13305 block->right,
13306 block->right && block->right->use?block->right->use->member : 0);
13307 if (rb->in) {
13308 struct triple_reg_set *in_set;
13309 printf(" in:");
13310 for(in_set = rb->in; in_set; in_set = in_set->next) {
13311 printf(" %-10p", in_set->member);
13312 }
13313 printf("\n");
13314 }
13315 phi_present = 0;
13316 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13317 done = (ptr == block->last);
13318 if (ptr->op == OP_PHI) {
13319 phi_present = 1;
13320 break;
13321 }
13322 }
13323 if (phi_present) {
13324 int edge;
13325 for(edge = 0; edge < block->users; edge++) {
13326 printf(" in(%d):", edge);
13327 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13328 struct triple **slot;
13329 done = (ptr == block->last);
13330 if (ptr->op != OP_PHI) {
13331 continue;
13332 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000013333 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013334 printf(" %-10p", slot[edge]);
13335 }
13336 printf("\n");
13337 }
13338 }
13339 if (block->first->op == OP_LABEL) {
13340 printf("%p:\n", block->first);
13341 }
13342 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13343 struct triple_set *user;
13344 struct live_range *lr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013345 unsigned id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013346 int op;
13347 op = ptr->op;
13348 done = (ptr == block->last);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013349 lr = rstate->lrd[ptr->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013350
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013351 if (triple_stores_block(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013352 if (ptr->u.block != block) {
13353 internal_error(state, ptr,
13354 "Wrong block pointer: %p",
13355 ptr->u.block);
13356 }
13357 }
13358 if (op == OP_ADECL) {
13359 for(user = ptr->use; user; user = user->next) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013360 if (!user->member->u.block) {
13361 internal_error(state, user->member,
13362 "Use %p not in a block?",
13363 user->member);
13364 }
13365
13366 }
13367 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000013368 id = ptr->id;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013369 ptr->id = rstate->lrd[id].orig_id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013370 SET_REG(ptr->id, lr->color);
Eric Biederman0babc1c2003-05-09 02:39:00 +000013371 display_triple(stdout, ptr);
13372 ptr->id = id;
13373
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013374 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
13375 internal_error(state, ptr, "lr has no defs!");
13376 }
13377
13378 if (lr->defs) {
13379 struct live_range_def *lrd;
13380 printf(" range:");
13381 lrd = lr->defs;
13382 do {
13383 printf(" %-10p", lrd->def);
13384 lrd = lrd->next;
13385 } while(lrd != lr->defs);
13386 printf("\n");
13387 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013388 if (lr->edges > 0) {
13389 struct live_range_edge *edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013390 printf(" edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013391 for(edge = lr->edges; edge; edge = edge->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013392 struct live_range_def *lrd;
13393 lrd = edge->node->defs;
13394 do {
13395 printf(" %-10p", lrd->def);
13396 lrd = lrd->next;
13397 } while(lrd != edge->node->defs);
13398 printf("|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013399 }
13400 printf("\n");
13401 }
13402 /* Do a bunch of sanity checks */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013403 valid_ins(state, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013404 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013405 internal_error(state, ptr, "Invalid triple id: %d",
13406 ptr->id);
13407 }
13408 for(user = ptr->use; user; user = user->next) {
13409 struct triple *use;
13410 struct live_range *ulr;
13411 use = user->member;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013412 valid_ins(state, use);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013413 if ((use->id < 0) || (use->id > rstate->defs)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013414 internal_error(state, use, "Invalid triple id: %d",
13415 use->id);
13416 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013417 ulr = rstate->lrd[user->member->id].lr;
13418 if (triple_stores_block(state, user->member) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000013419 !user->member->u.block) {
13420 internal_error(state, user->member,
13421 "Use %p not in a block?",
13422 user->member);
13423 }
13424 }
13425 }
13426 if (rb->out) {
13427 struct triple_reg_set *out_set;
13428 printf(" out:");
13429 for(out_set = rb->out; out_set; out_set = out_set->next) {
13430 printf(" %-10p", out_set->member);
13431 }
13432 printf("\n");
13433 }
13434 printf("\n");
13435}
13436
13437static struct live_range *merge_sort_lr(
13438 struct live_range *first, struct live_range *last)
13439{
13440 struct live_range *mid, *join, **join_tail, *pick;
13441 size_t size;
13442 size = (last - first) + 1;
13443 if (size >= 2) {
13444 mid = first + size/2;
13445 first = merge_sort_lr(first, mid -1);
13446 mid = merge_sort_lr(mid, last);
13447
13448 join = 0;
13449 join_tail = &join;
13450 /* merge the two lists */
13451 while(first && mid) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013452 if ((first->degree < mid->degree) ||
13453 ((first->degree == mid->degree) &&
13454 (first->length < mid->length))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013455 pick = first;
13456 first = first->group_next;
13457 if (first) {
13458 first->group_prev = 0;
13459 }
13460 }
13461 else {
13462 pick = mid;
13463 mid = mid->group_next;
13464 if (mid) {
13465 mid->group_prev = 0;
13466 }
13467 }
13468 pick->group_next = 0;
13469 pick->group_prev = join_tail;
13470 *join_tail = pick;
13471 join_tail = &pick->group_next;
13472 }
13473 /* Splice the remaining list */
13474 pick = (first)? first : mid;
13475 *join_tail = pick;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013476 if (pick) {
13477 pick->group_prev = join_tail;
13478 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013479 }
13480 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013481 if (!first->defs) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013482 first = 0;
13483 }
13484 join = first;
13485 }
13486 return join;
13487}
13488
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013489static void ids_from_rstate(struct compile_state *state,
13490 struct reg_state *rstate)
13491{
13492 struct triple *ins, *first;
13493 if (!rstate->defs) {
13494 return;
13495 }
13496 /* Display the graph if desired */
13497 if (state->debug & DEBUG_INTERFERENCE) {
13498 print_blocks(state, stdout);
13499 print_control_flow(state);
13500 }
13501 first = RHS(state->main_function, 0);
13502 ins = first;
13503 do {
13504 if (ins->id) {
13505 struct live_range_def *lrd;
13506 lrd = &rstate->lrd[ins->id];
13507 ins->id = lrd->orig_id;
13508 }
13509 ins = ins->next;
13510 } while(ins != first);
13511}
13512
13513static void cleanup_live_edges(struct reg_state *rstate)
13514{
13515 int i;
13516 /* Free the edges on each node */
13517 for(i = 1; i <= rstate->ranges; i++) {
13518 remove_live_edges(rstate, &rstate->lr[i]);
13519 }
13520}
13521
13522static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
13523{
13524 cleanup_live_edges(rstate);
13525 xfree(rstate->lrd);
13526 xfree(rstate->lr);
13527
13528 /* Free the variable lifetime information */
13529 if (rstate->blocks) {
13530 free_variable_lifetimes(state, rstate->blocks);
13531 }
13532 rstate->defs = 0;
13533 rstate->ranges = 0;
13534 rstate->lrd = 0;
13535 rstate->lr = 0;
13536 rstate->blocks = 0;
13537}
13538
Eric Biedermanb138ac82003-04-22 18:44:01 +000013539static void allocate_registers(struct compile_state *state)
13540{
13541 struct reg_state rstate;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013542 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013543
13544 /* Clear out the reg_state */
13545 memset(&rstate, 0, sizeof(rstate));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013546 rstate.max_passes = MAX_ALLOCATION_PASSES;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013547
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013548 do {
13549 struct live_range **point, **next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013550 int coalesced;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013551
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013552 /* Restore ids */
13553 ids_from_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013554
Eric Biedermanf96a8102003-06-16 16:57:34 +000013555 /* Cleanup the temporary data structures */
13556 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013557
Eric Biedermanf96a8102003-06-16 16:57:34 +000013558 /* Compute the variable lifetimes */
13559 rstate.blocks = compute_variable_lifetimes(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013560
Eric Biedermanf96a8102003-06-16 16:57:34 +000013561 /* Fix invalid mandatory live range coalesce conflicts */
13562 walk_variable_lifetimes(
13563 state, rstate.blocks, fix_coalesce_conflicts, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013564
Eric Biedermanf96a8102003-06-16 16:57:34 +000013565 /* Fix two simultaneous uses of the same register */
13566 correct_tangles(state, rstate.blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013567
13568 if (state->debug & DEBUG_INSERTED_COPIES) {
13569 printf("After resolve_tangles\n");
13570 print_blocks(state, stdout);
13571 print_control_flow(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013572 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013573
Eric Biedermanb138ac82003-04-22 18:44:01 +000013574
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013575 /* Allocate and initialize the live ranges */
13576 initialize_live_ranges(state, &rstate);
13577
13578 do {
13579 /* Forget previous live range edge calculations */
13580 cleanup_live_edges(&rstate);
13581
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013582#if 0
13583 fprintf(stderr, "coalescing\n");
13584#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013585 /* Compute the interference graph */
13586 walk_variable_lifetimes(
13587 state, rstate.blocks, graph_ins, &rstate);
13588
13589 /* Display the interference graph if desired */
13590 if (state->debug & DEBUG_INTERFERENCE) {
13591 printf("\nlive variables by block\n");
13592 walk_blocks(state, print_interference_block, &rstate);
13593 printf("\nlive variables by instruction\n");
13594 walk_variable_lifetimes(
13595 state, rstate.blocks,
13596 print_interference_ins, &rstate);
13597 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013598#if DEBUG_CONSISTENCY
13599 /* Verify the interference graph */
13600 walk_variable_lifetimes(
13601 state, rstate.blocks, verify_graph_ins, &rstate);
13602#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013603
13604 coalesced = coalesce_live_ranges(state, &rstate);
13605 } while(coalesced);
13606
13607 /* Build the groups low and high. But with the nodes
13608 * first sorted by degree order.
13609 */
13610 rstate.low_tail = &rstate.low;
13611 rstate.high_tail = &rstate.high;
13612 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
13613 if (rstate.high) {
13614 rstate.high->group_prev = &rstate.high;
13615 }
13616 for(point = &rstate.high; *point; point = &(*point)->group_next)
13617 ;
13618 rstate.high_tail = point;
13619 /* Walk through the high list and move everything that needs
13620 * to be onto low.
13621 */
13622 for(point = &rstate.high; *point; point = next) {
13623 struct live_range *range;
13624 next = &(*point)->group_next;
13625 range = *point;
13626
13627 /* If it has a low degree or it already has a color
13628 * place the node in low.
13629 */
13630 if ((range->degree < regc_max_size(state, range->classes)) ||
13631 (range->color != REG_UNSET)) {
13632 cgdebug_printf("Lo: %5d degree %5d%s\n",
13633 range - rstate.lr, range->degree,
13634 (range->color != REG_UNSET) ? " (colored)": "");
13635 *range->group_prev = range->group_next;
13636 if (range->group_next) {
13637 range->group_next->group_prev = range->group_prev;
13638 }
13639 if (&range->group_next == rstate.high_tail) {
13640 rstate.high_tail = range->group_prev;
13641 }
13642 range->group_prev = rstate.low_tail;
13643 range->group_next = 0;
13644 *rstate.low_tail = range;
13645 rstate.low_tail = &range->group_next;
13646 next = point;
13647 }
13648 else {
13649 cgdebug_printf("hi: %5d degree %5d%s\n",
13650 range - rstate.lr, range->degree,
13651 (range->color != REG_UNSET) ? " (colored)": "");
13652 }
13653 }
13654 /* Color the live_ranges */
13655 colored = color_graph(state, &rstate);
13656 rstate.passes++;
13657 } while (!colored);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013658
Eric Biedermana96d6a92003-05-13 20:45:19 +000013659 /* Verify the graph was properly colored */
13660 verify_colors(state, &rstate);
13661
Eric Biedermanb138ac82003-04-22 18:44:01 +000013662 /* Move the colors from the graph to the triples */
13663 color_triples(state, &rstate);
13664
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013665 /* Cleanup the temporary data structures */
13666 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013667}
13668
13669/* Sparce Conditional Constant Propogation
13670 * =========================================
13671 */
13672struct ssa_edge;
13673struct flow_block;
13674struct lattice_node {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013675 unsigned old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013676 struct triple *def;
13677 struct ssa_edge *out;
13678 struct flow_block *fblock;
13679 struct triple *val;
13680 /* lattice high val && !is_const(val)
13681 * lattice const is_const(val)
13682 * lattice low val == 0
13683 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000013684};
13685struct ssa_edge {
13686 struct lattice_node *src;
13687 struct lattice_node *dst;
13688 struct ssa_edge *work_next;
13689 struct ssa_edge *work_prev;
13690 struct ssa_edge *out_next;
13691};
13692struct flow_edge {
13693 struct flow_block *src;
13694 struct flow_block *dst;
13695 struct flow_edge *work_next;
13696 struct flow_edge *work_prev;
13697 struct flow_edge *in_next;
13698 struct flow_edge *out_next;
13699 int executable;
13700};
13701struct flow_block {
13702 struct block *block;
13703 struct flow_edge *in;
13704 struct flow_edge *out;
13705 struct flow_edge left, right;
13706};
13707
13708struct scc_state {
Eric Biederman0babc1c2003-05-09 02:39:00 +000013709 int ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013710 struct lattice_node *lattice;
13711 struct ssa_edge *ssa_edges;
13712 struct flow_block *flow_blocks;
13713 struct flow_edge *flow_work_list;
13714 struct ssa_edge *ssa_work_list;
13715};
13716
13717
13718static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
13719 struct flow_edge *fedge)
13720{
13721 if (!scc->flow_work_list) {
13722 scc->flow_work_list = fedge;
13723 fedge->work_next = fedge->work_prev = fedge;
13724 }
13725 else {
13726 struct flow_edge *ftail;
13727 ftail = scc->flow_work_list->work_prev;
13728 fedge->work_next = ftail->work_next;
13729 fedge->work_prev = ftail;
13730 fedge->work_next->work_prev = fedge;
13731 fedge->work_prev->work_next = fedge;
13732 }
13733}
13734
13735static struct flow_edge *scc_next_fedge(
13736 struct compile_state *state, struct scc_state *scc)
13737{
13738 struct flow_edge *fedge;
13739 fedge = scc->flow_work_list;
13740 if (fedge) {
13741 fedge->work_next->work_prev = fedge->work_prev;
13742 fedge->work_prev->work_next = fedge->work_next;
13743 if (fedge->work_next != fedge) {
13744 scc->flow_work_list = fedge->work_next;
13745 } else {
13746 scc->flow_work_list = 0;
13747 }
13748 }
13749 return fedge;
13750}
13751
13752static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
13753 struct ssa_edge *sedge)
13754{
13755 if (!scc->ssa_work_list) {
13756 scc->ssa_work_list = sedge;
13757 sedge->work_next = sedge->work_prev = sedge;
13758 }
13759 else {
13760 struct ssa_edge *stail;
13761 stail = scc->ssa_work_list->work_prev;
13762 sedge->work_next = stail->work_next;
13763 sedge->work_prev = stail;
13764 sedge->work_next->work_prev = sedge;
13765 sedge->work_prev->work_next = sedge;
13766 }
13767}
13768
13769static struct ssa_edge *scc_next_sedge(
13770 struct compile_state *state, struct scc_state *scc)
13771{
13772 struct ssa_edge *sedge;
13773 sedge = scc->ssa_work_list;
13774 if (sedge) {
13775 sedge->work_next->work_prev = sedge->work_prev;
13776 sedge->work_prev->work_next = sedge->work_next;
13777 if (sedge->work_next != sedge) {
13778 scc->ssa_work_list = sedge->work_next;
13779 } else {
13780 scc->ssa_work_list = 0;
13781 }
13782 }
13783 return sedge;
13784}
13785
13786static void initialize_scc_state(
13787 struct compile_state *state, struct scc_state *scc)
13788{
13789 int ins_count, ssa_edge_count;
13790 int ins_index, ssa_edge_index, fblock_index;
13791 struct triple *first, *ins;
13792 struct block *block;
13793 struct flow_block *fblock;
13794
13795 memset(scc, 0, sizeof(*scc));
13796
13797 /* Inialize pass zero find out how much memory we need */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013798 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013799 ins = first;
13800 ins_count = ssa_edge_count = 0;
13801 do {
13802 struct triple_set *edge;
13803 ins_count += 1;
13804 for(edge = ins->use; edge; edge = edge->next) {
13805 ssa_edge_count++;
13806 }
13807 ins = ins->next;
13808 } while(ins != first);
13809#if DEBUG_SCC
13810 fprintf(stderr, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
13811 ins_count, ssa_edge_count, state->last_vertex);
13812#endif
Eric Biederman0babc1c2003-05-09 02:39:00 +000013813 scc->ins_count = ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013814 scc->lattice =
13815 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
13816 scc->ssa_edges =
13817 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
13818 scc->flow_blocks =
13819 xcmalloc(sizeof(*scc->flow_blocks)*(state->last_vertex + 1),
13820 "flow_blocks");
13821
13822 /* Initialize pass one collect up the nodes */
13823 fblock = 0;
13824 block = 0;
13825 ins_index = ssa_edge_index = fblock_index = 0;
13826 ins = first;
13827 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013828 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
13829 block = ins->u.block;
13830 if (!block) {
13831 internal_error(state, ins, "label without block");
13832 }
13833 fblock_index += 1;
13834 block->vertex = fblock_index;
13835 fblock = &scc->flow_blocks[fblock_index];
13836 fblock->block = block;
13837 }
13838 {
13839 struct lattice_node *lnode;
13840 ins_index += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013841 lnode = &scc->lattice[ins_index];
13842 lnode->def = ins;
13843 lnode->out = 0;
13844 lnode->fblock = fblock;
13845 lnode->val = ins; /* LATTICE HIGH */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013846 lnode->old_id = ins->id;
13847 ins->id = ins_index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013848 }
13849 ins = ins->next;
13850 } while(ins != first);
13851 /* Initialize pass two collect up the edges */
13852 block = 0;
13853 fblock = 0;
13854 ins = first;
13855 do {
13856 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
13857 struct flow_edge *fedge, **ftail;
13858 struct block_set *bedge;
13859 block = ins->u.block;
13860 fblock = &scc->flow_blocks[block->vertex];
13861 fblock->in = 0;
13862 fblock->out = 0;
13863 ftail = &fblock->out;
13864 if (block->left) {
13865 fblock->left.dst = &scc->flow_blocks[block->left->vertex];
13866 if (fblock->left.dst->block != block->left) {
13867 internal_error(state, 0, "block mismatch");
13868 }
13869 fblock->left.out_next = 0;
13870 *ftail = &fblock->left;
13871 ftail = &fblock->left.out_next;
13872 }
13873 if (block->right) {
13874 fblock->right.dst = &scc->flow_blocks[block->right->vertex];
13875 if (fblock->right.dst->block != block->right) {
13876 internal_error(state, 0, "block mismatch");
13877 }
13878 fblock->right.out_next = 0;
13879 *ftail = &fblock->right;
13880 ftail = &fblock->right.out_next;
13881 }
13882 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
13883 fedge->src = fblock;
13884 fedge->work_next = fedge->work_prev = fedge;
13885 fedge->executable = 0;
13886 }
13887 ftail = &fblock->in;
13888 for(bedge = block->use; bedge; bedge = bedge->next) {
13889 struct block *src_block;
13890 struct flow_block *sfblock;
13891 struct flow_edge *sfedge;
13892 src_block = bedge->member;
13893 sfblock = &scc->flow_blocks[src_block->vertex];
13894 sfedge = 0;
13895 if (src_block->left == block) {
13896 sfedge = &sfblock->left;
13897 } else {
13898 sfedge = &sfblock->right;
13899 }
13900 *ftail = sfedge;
13901 ftail = &sfedge->in_next;
13902 sfedge->in_next = 0;
13903 }
13904 }
13905 {
13906 struct triple_set *edge;
13907 struct ssa_edge **stail;
13908 struct lattice_node *lnode;
13909 lnode = &scc->lattice[ins->id];
13910 lnode->out = 0;
13911 stail = &lnode->out;
13912 for(edge = ins->use; edge; edge = edge->next) {
13913 struct ssa_edge *sedge;
13914 ssa_edge_index += 1;
13915 sedge = &scc->ssa_edges[ssa_edge_index];
13916 *stail = sedge;
13917 stail = &sedge->out_next;
13918 sedge->src = lnode;
13919 sedge->dst = &scc->lattice[edge->member->id];
13920 sedge->work_next = sedge->work_prev = sedge;
13921 sedge->out_next = 0;
13922 }
13923 }
13924 ins = ins->next;
13925 } while(ins != first);
13926 /* Setup a dummy block 0 as a node above the start node */
13927 {
13928 struct flow_block *fblock, *dst;
13929 struct flow_edge *fedge;
13930 fblock = &scc->flow_blocks[0];
13931 fblock->block = 0;
13932 fblock->in = 0;
13933 fblock->out = &fblock->left;
13934 dst = &scc->flow_blocks[state->first_block->vertex];
13935 fedge = &fblock->left;
13936 fedge->src = fblock;
13937 fedge->dst = dst;
13938 fedge->work_next = fedge;
13939 fedge->work_prev = fedge;
13940 fedge->in_next = fedge->dst->in;
13941 fedge->out_next = 0;
13942 fedge->executable = 0;
13943 fedge->dst->in = fedge;
13944
13945 /* Initialize the work lists */
13946 scc->flow_work_list = 0;
13947 scc->ssa_work_list = 0;
13948 scc_add_fedge(state, scc, fedge);
13949 }
13950#if DEBUG_SCC
13951 fprintf(stderr, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
13952 ins_index, ssa_edge_index, fblock_index);
13953#endif
13954}
13955
13956
13957static void free_scc_state(
13958 struct compile_state *state, struct scc_state *scc)
13959{
13960 xfree(scc->flow_blocks);
13961 xfree(scc->ssa_edges);
13962 xfree(scc->lattice);
Eric Biederman0babc1c2003-05-09 02:39:00 +000013963
Eric Biedermanb138ac82003-04-22 18:44:01 +000013964}
13965
13966static struct lattice_node *triple_to_lattice(
13967 struct compile_state *state, struct scc_state *scc, struct triple *ins)
13968{
13969 if (ins->id <= 0) {
13970 internal_error(state, ins, "bad id");
13971 }
13972 return &scc->lattice[ins->id];
13973}
13974
Eric Biederman6aa31cc2003-06-10 21:22:07 +000013975static struct triple *preserve_lval(
13976 struct compile_state *state, struct lattice_node *lnode)
13977{
13978 struct triple *old;
13979 /* Preserve the original value */
13980 if (lnode->val) {
13981 old = dup_triple(state, lnode->val);
13982 if (lnode->val != lnode->def) {
13983 xfree(lnode->val);
13984 }
13985 lnode->val = 0;
13986 } else {
13987 old = 0;
13988 }
13989 return old;
13990}
13991
13992static int lval_changed(struct compile_state *state,
13993 struct triple *old, struct lattice_node *lnode)
13994{
13995 int changed;
13996 /* See if the lattice value has changed */
13997 changed = 1;
13998 if (!old && !lnode->val) {
13999 changed = 0;
14000 }
14001 if (changed && lnode->val && !is_const(lnode->val)) {
14002 changed = 0;
14003 }
14004 if (changed &&
14005 lnode->val && old &&
14006 (memcmp(lnode->val->param, old->param,
14007 TRIPLE_SIZE(lnode->val->sizes) * sizeof(lnode->val->param[0])) == 0) &&
14008 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
14009 changed = 0;
14010 }
14011 if (old) {
14012 xfree(old);
14013 }
14014 return changed;
14015
14016}
14017
Eric Biedermanb138ac82003-04-22 18:44:01 +000014018static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
14019 struct lattice_node *lnode)
14020{
14021 struct lattice_node *tmp;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014022 struct triple **slot, *old;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014023 struct flow_edge *fedge;
14024 int index;
14025 if (lnode->def->op != OP_PHI) {
14026 internal_error(state, lnode->def, "not phi");
14027 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014028 /* Store the original value */
14029 old = preserve_lval(state, lnode);
14030
Eric Biedermanb138ac82003-04-22 18:44:01 +000014031 /* default to lattice high */
14032 lnode->val = lnode->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014033 slot = &RHS(lnode->def, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014034 index = 0;
14035 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
14036 if (!fedge->executable) {
14037 continue;
14038 }
14039 if (!slot[index]) {
14040 internal_error(state, lnode->def, "no phi value");
14041 }
14042 tmp = triple_to_lattice(state, scc, slot[index]);
14043 /* meet(X, lattice low) = lattice low */
14044 if (!tmp->val) {
14045 lnode->val = 0;
14046 }
14047 /* meet(X, lattice high) = X */
14048 else if (!tmp->val) {
14049 lnode->val = lnode->val;
14050 }
14051 /* meet(lattice high, X) = X */
14052 else if (!is_const(lnode->val)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014053 lnode->val = dup_triple(state, tmp->val);
14054 lnode->val->type = lnode->def->type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014055 }
14056 /* meet(const, const) = const or lattice low */
14057 else if (!constants_equal(state, lnode->val, tmp->val)) {
14058 lnode->val = 0;
14059 }
14060 if (!lnode->val) {
14061 break;
14062 }
14063 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014064#if DEBUG_SCC
14065 fprintf(stderr, "phi: %d -> %s\n",
14066 lnode->def->id,
14067 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14068#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014069 /* If the lattice value has changed update the work lists. */
14070 if (lval_changed(state, old, lnode)) {
14071 struct ssa_edge *sedge;
14072 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14073 scc_add_sedge(state, scc, sedge);
14074 }
14075 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014076}
14077
14078static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
14079 struct lattice_node *lnode)
14080{
14081 int changed;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014082 struct triple *old, *scratch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014083 struct triple **dexpr, **vexpr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014084 int count, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014085
14086 /* Store the original value */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014087 old = preserve_lval(state, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000014088
Eric Biedermanb138ac82003-04-22 18:44:01 +000014089 /* Reinitialize the value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000014090 lnode->val = scratch = dup_triple(state, lnode->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014091 scratch->id = lnode->old_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014092 scratch->next = scratch;
14093 scratch->prev = scratch;
14094 scratch->use = 0;
14095
14096 count = TRIPLE_SIZE(scratch->sizes);
14097 for(i = 0; i < count; i++) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000014098 dexpr = &lnode->def->param[i];
14099 vexpr = &scratch->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014100 *vexpr = *dexpr;
14101 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14102 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14103 *dexpr) {
14104 struct lattice_node *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014105 tmp = triple_to_lattice(state, scc, *dexpr);
14106 *vexpr = (tmp->val)? tmp->val : tmp->def;
14107 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014108 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014109 if (scratch->op == OP_BRANCH) {
14110 scratch->next = lnode->def->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014111 }
14112 /* Recompute the value */
14113#warning "FIXME see if simplify does anything bad"
14114 /* So far it looks like only the strength reduction
14115 * optimization are things I need to worry about.
14116 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000014117 simplify(state, scratch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014118 /* Cleanup my value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000014119 if (scratch->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014120 internal_error(state, lnode->def, "scratch used?");
14121 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014122 if ((scratch->prev != scratch) ||
14123 ((scratch->next != scratch) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000014124 ((lnode->def->op != OP_BRANCH) ||
Eric Biederman0babc1c2003-05-09 02:39:00 +000014125 (scratch->next != lnode->def->next)))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014126 internal_error(state, lnode->def, "scratch in list?");
14127 }
14128 /* undo any uses... */
Eric Biederman0babc1c2003-05-09 02:39:00 +000014129 count = TRIPLE_SIZE(scratch->sizes);
14130 for(i = 0; i < count; i++) {
14131 vexpr = &scratch->param[i];
14132 if (*vexpr) {
14133 unuse_triple(*vexpr, scratch);
14134 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014135 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014136 if (!is_const(scratch)) {
14137 for(i = 0; i < count; i++) {
14138 dexpr = &lnode->def->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014139 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14140 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14141 *dexpr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000014142 struct lattice_node *tmp;
14143 tmp = triple_to_lattice(state, scc, *dexpr);
14144 if (!tmp->val) {
14145 lnode->val = 0;
14146 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014147 }
14148 }
14149 }
14150 if (lnode->val &&
14151 (lnode->val->op == lnode->def->op) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000014152 (memcmp(lnode->val->param, lnode->def->param,
14153 count * sizeof(lnode->val->param[0])) == 0) &&
14154 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014155 lnode->val = lnode->def;
14156 }
14157 /* Find the cases that are always lattice lo */
14158 if (lnode->val &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000014159 triple_is_def(state, lnode->val) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000014160 !triple_is_pure(state, lnode->val)) {
14161 lnode->val = 0;
14162 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014163 if (lnode->val &&
14164 (lnode->val->op == OP_SDECL) &&
14165 (lnode->val != lnode->def)) {
14166 internal_error(state, lnode->def, "bad sdecl");
14167 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014168 /* See if the lattice value has changed */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014169 changed = lval_changed(state, old, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000014170 if (lnode->val != scratch) {
14171 xfree(scratch);
14172 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014173 return changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014174}
Eric Biederman0babc1c2003-05-09 02:39:00 +000014175
Eric Biedermanb138ac82003-04-22 18:44:01 +000014176static void scc_visit_branch(struct compile_state *state, struct scc_state *scc,
14177 struct lattice_node *lnode)
14178{
14179 struct lattice_node *cond;
14180#if DEBUG_SCC
14181 {
14182 struct flow_edge *fedge;
14183 fprintf(stderr, "branch: %d (",
14184 lnode->def->id);
14185
14186 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
14187 fprintf(stderr, " %d", fedge->dst->block->vertex);
14188 }
14189 fprintf(stderr, " )");
Eric Biederman0babc1c2003-05-09 02:39:00 +000014190 if (TRIPLE_RHS(lnode->def->sizes) > 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014191 fprintf(stderr, " <- %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014192 RHS(lnode->def, 0)->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014193 }
14194 fprintf(stderr, "\n");
14195 }
14196#endif
14197 if (lnode->def->op != OP_BRANCH) {
14198 internal_error(state, lnode->def, "not branch");
14199 }
14200 /* This only applies to conditional branches */
Eric Biederman0babc1c2003-05-09 02:39:00 +000014201 if (TRIPLE_RHS(lnode->def->sizes) == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014202 return;
14203 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014204 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000014205 if (cond->val && !is_const(cond->val)) {
14206#warning "FIXME do I need to do something here?"
14207 warning(state, cond->def, "condition not constant?");
14208 return;
14209 }
14210 if (cond->val == 0) {
14211 scc_add_fedge(state, scc, cond->fblock->out);
14212 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14213 }
14214 else if (cond->val->u.cval) {
14215 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14216
14217 } else {
14218 scc_add_fedge(state, scc, cond->fblock->out);
14219 }
14220
14221}
14222
14223static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
14224 struct lattice_node *lnode)
14225{
14226 int changed;
14227
14228 changed = compute_lnode_val(state, scc, lnode);
14229#if DEBUG_SCC
14230 {
14231 struct triple **expr;
14232 fprintf(stderr, "expr: %3d %10s (",
14233 lnode->def->id, tops(lnode->def->op));
14234 expr = triple_rhs(state, lnode->def, 0);
14235 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000014236 if (*expr) {
14237 fprintf(stderr, " %d", (*expr)->id);
14238 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014239 }
14240 fprintf(stderr, " ) -> %s\n",
14241 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14242 }
14243#endif
14244 if (lnode->def->op == OP_BRANCH) {
14245 scc_visit_branch(state, scc, lnode);
14246
14247 }
14248 else if (changed) {
14249 struct ssa_edge *sedge;
14250 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14251 scc_add_sedge(state, scc, sedge);
14252 }
14253 }
14254}
14255
14256static void scc_writeback_values(
14257 struct compile_state *state, struct scc_state *scc)
14258{
14259 struct triple *first, *ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000014260 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014261 ins = first;
14262 do {
14263 struct lattice_node *lnode;
14264 lnode = triple_to_lattice(state, scc, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014265 /* Restore id */
14266 ins->id = lnode->old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014267#if DEBUG_SCC
14268 if (lnode->val && !is_const(lnode->val)) {
14269 warning(state, lnode->def,
14270 "lattice node still high?");
14271 }
14272#endif
14273 if (lnode->val && (lnode->val != ins)) {
14274 /* See if it something I know how to write back */
14275 switch(lnode->val->op) {
14276 case OP_INTCONST:
14277 mkconst(state, ins, lnode->val->u.cval);
14278 break;
14279 case OP_ADDRCONST:
14280 mkaddr_const(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014281 MISC(lnode->val, 0), lnode->val->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014282 break;
14283 default:
14284 /* By default don't copy the changes,
14285 * recompute them in place instead.
14286 */
14287 simplify(state, ins);
14288 break;
14289 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014290 if (is_const(lnode->val) &&
14291 !constants_equal(state, lnode->val, ins)) {
14292 internal_error(state, 0, "constants not equal");
14293 }
14294 /* Free the lattice nodes */
14295 xfree(lnode->val);
14296 lnode->val = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014297 }
14298 ins = ins->next;
14299 } while(ins != first);
14300}
14301
14302static void scc_transform(struct compile_state *state)
14303{
14304 struct scc_state scc;
14305
14306 initialize_scc_state(state, &scc);
14307
14308 while(scc.flow_work_list || scc.ssa_work_list) {
14309 struct flow_edge *fedge;
14310 struct ssa_edge *sedge;
14311 struct flow_edge *fptr;
14312 while((fedge = scc_next_fedge(state, &scc))) {
14313 struct block *block;
14314 struct triple *ptr;
14315 struct flow_block *fblock;
14316 int time;
14317 int done;
14318 if (fedge->executable) {
14319 continue;
14320 }
14321 if (!fedge->dst) {
14322 internal_error(state, 0, "fedge without dst");
14323 }
14324 if (!fedge->src) {
14325 internal_error(state, 0, "fedge without src");
14326 }
14327 fedge->executable = 1;
14328 fblock = fedge->dst;
14329 block = fblock->block;
14330 time = 0;
14331 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14332 if (fptr->executable) {
14333 time++;
14334 }
14335 }
14336#if DEBUG_SCC
14337 fprintf(stderr, "vertex: %d time: %d\n",
14338 block->vertex, time);
14339
14340#endif
14341 done = 0;
14342 for(ptr = block->first; !done; ptr = ptr->next) {
14343 struct lattice_node *lnode;
14344 done = (ptr == block->last);
14345 lnode = &scc.lattice[ptr->id];
14346 if (ptr->op == OP_PHI) {
14347 scc_visit_phi(state, &scc, lnode);
14348 }
14349 else if (time == 1) {
14350 scc_visit_expr(state, &scc, lnode);
14351 }
14352 }
14353 if (fblock->out && !fblock->out->out_next) {
14354 scc_add_fedge(state, &scc, fblock->out);
14355 }
14356 }
14357 while((sedge = scc_next_sedge(state, &scc))) {
14358 struct lattice_node *lnode;
14359 struct flow_block *fblock;
14360 lnode = sedge->dst;
14361 fblock = lnode->fblock;
14362#if DEBUG_SCC
14363 fprintf(stderr, "sedge: %5d (%5d -> %5d)\n",
14364 sedge - scc.ssa_edges,
14365 sedge->src->def->id,
14366 sedge->dst->def->id);
14367#endif
14368 if (lnode->def->op == OP_PHI) {
14369 scc_visit_phi(state, &scc, lnode);
14370 }
14371 else {
14372 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14373 if (fptr->executable) {
14374 break;
14375 }
14376 }
14377 if (fptr) {
14378 scc_visit_expr(state, &scc, lnode);
14379 }
14380 }
14381 }
14382 }
14383
14384 scc_writeback_values(state, &scc);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014385 free_scc_state(state, &scc);
14386}
14387
14388
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014389static void transform_to_arch_instructions(struct compile_state *state)
14390{
14391 struct triple *ins, *first;
14392 first = RHS(state->main_function, 0);
14393 ins = first;
14394 do {
14395 ins = transform_to_arch_instruction(state, ins);
14396 } while(ins != first);
14397}
Eric Biedermanb138ac82003-04-22 18:44:01 +000014398
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014399#if DEBUG_CONSISTENCY
14400static void verify_uses(struct compile_state *state)
14401{
14402 struct triple *first, *ins;
14403 struct triple_set *set;
14404 first = RHS(state->main_function, 0);
14405 ins = first;
14406 do {
14407 struct triple **expr;
14408 expr = triple_rhs(state, ins, 0);
14409 for(; expr; expr = triple_rhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000014410 struct triple *rhs;
14411 rhs = *expr;
14412 for(set = rhs?rhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014413 if (set->member == ins) {
14414 break;
14415 }
14416 }
14417 if (!set) {
14418 internal_error(state, ins, "rhs not used");
14419 }
14420 }
14421 expr = triple_lhs(state, ins, 0);
14422 for(; expr; expr = triple_lhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000014423 struct triple *lhs;
14424 lhs = *expr;
14425 for(set = lhs?lhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014426 if (set->member == ins) {
14427 break;
14428 }
14429 }
14430 if (!set) {
14431 internal_error(state, ins, "lhs not used");
14432 }
14433 }
14434 ins = ins->next;
14435 } while(ins != first);
14436
14437}
14438static void verify_blocks(struct compile_state *state)
14439{
14440 struct triple *ins;
14441 struct block *block;
14442 block = state->first_block;
14443 if (!block) {
14444 return;
14445 }
14446 do {
14447 for(ins = block->first; ins != block->last->next; ins = ins->next) {
14448 if (!triple_stores_block(state, ins)) {
14449 continue;
14450 }
14451 if (ins->u.block != block) {
14452 internal_error(state, ins, "inconsitent block specified");
14453 }
14454 }
14455 if (!triple_stores_block(state, block->last->next)) {
14456 internal_error(state, block->last->next,
14457 "cannot find next block");
14458 }
14459 block = block->last->next->u.block;
14460 if (!block) {
14461 internal_error(state, block->last->next,
14462 "bad next block");
14463 }
14464 } while(block != state->first_block);
14465}
14466
14467static void verify_domination(struct compile_state *state)
14468{
14469 struct triple *first, *ins;
14470 struct triple_set *set;
14471 if (!state->first_block) {
14472 return;
14473 }
14474
14475 first = RHS(state->main_function, 0);
14476 ins = first;
14477 do {
14478 for(set = ins->use; set; set = set->next) {
14479 struct triple **expr;
14480 if (set->member->op == OP_PHI) {
14481 continue;
14482 }
14483 /* See if the use is on the righ hand side */
14484 expr = triple_rhs(state, set->member, 0);
14485 for(; expr ; expr = triple_rhs(state, set->member, expr)) {
14486 if (*expr == ins) {
14487 break;
14488 }
14489 }
14490 if (expr &&
14491 !tdominates(state, ins, set->member)) {
14492 internal_error(state, set->member,
14493 "non dominated rhs use?");
14494 }
14495 }
14496 ins = ins->next;
14497 } while(ins != first);
14498}
14499
14500static void verify_piece(struct compile_state *state)
14501{
14502 struct triple *first, *ins;
14503 first = RHS(state->main_function, 0);
14504 ins = first;
14505 do {
14506 struct triple *ptr;
14507 int lhs, i;
14508 lhs = TRIPLE_LHS(ins->sizes);
14509 if ((ins->op == OP_WRITE) || (ins->op == OP_STORE)) {
14510 lhs = 0;
14511 }
14512 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
14513 if (ptr != LHS(ins, i)) {
14514 internal_error(state, ins, "malformed lhs on %s",
14515 tops(ins->op));
14516 }
14517 if (ptr->op != OP_PIECE) {
14518 internal_error(state, ins, "bad lhs op %s at %d on %s",
14519 tops(ptr->op), i, tops(ins->op));
14520 }
14521 if (ptr->u.cval != i) {
14522 internal_error(state, ins, "bad u.cval of %d %d expected",
14523 ptr->u.cval, i);
14524 }
14525 }
14526 ins = ins->next;
14527 } while(ins != first);
14528}
14529static void verify_ins_colors(struct compile_state *state)
14530{
14531 struct triple *first, *ins;
14532
14533 first = RHS(state->main_function, 0);
14534 ins = first;
14535 do {
14536 ins = ins->next;
14537 } while(ins != first);
14538}
14539static void verify_consistency(struct compile_state *state)
14540{
14541 verify_uses(state);
14542 verify_blocks(state);
14543 verify_domination(state);
14544 verify_piece(state);
14545 verify_ins_colors(state);
14546}
14547#else
14548#define verify_consistency(state) do {} while(0)
14549#endif /* DEBUG_USES */
Eric Biedermanb138ac82003-04-22 18:44:01 +000014550
14551static void optimize(struct compile_state *state)
14552{
14553 if (state->debug & DEBUG_TRIPLES) {
14554 print_triples(state);
14555 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014556 /* Replace structures with simpler data types */
14557 flatten_structures(state);
14558 if (state->debug & DEBUG_TRIPLES) {
14559 print_triples(state);
14560 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014561 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014562 /* Analize the intermediate code */
14563 setup_basic_blocks(state);
14564 analyze_idominators(state);
14565 analyze_ipdominators(state);
14566 /* Transform the code to ssa form */
14567 transform_to_ssa_form(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014568 verify_consistency(state);
Eric Biederman05f26fc2003-06-11 21:55:00 +000014569 if (state->debug & DEBUG_CODE_ELIMINATION) {
14570 fprintf(stdout, "After transform_to_ssa_form\n");
14571 print_blocks(state, stdout);
14572 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014573 /* Do strength reduction and simple constant optimizations */
14574 if (state->optimize >= 1) {
14575 simplify_all(state);
14576 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014577 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014578 /* Propogate constants throughout the code */
14579 if (state->optimize >= 2) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014580#warning "FIXME fix scc_transform"
Eric Biedermanb138ac82003-04-22 18:44:01 +000014581 scc_transform(state);
14582 transform_from_ssa_form(state);
14583 free_basic_blocks(state);
14584 setup_basic_blocks(state);
14585 analyze_idominators(state);
14586 analyze_ipdominators(state);
14587 transform_to_ssa_form(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014588 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014589 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014590#warning "WISHLIST implement single use constants (least possible register pressure)"
14591#warning "WISHLIST implement induction variable elimination"
Eric Biedermanb138ac82003-04-22 18:44:01 +000014592 /* Select architecture instructions and an initial partial
14593 * coloring based on architecture constraints.
14594 */
14595 transform_to_arch_instructions(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014596 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014597 if (state->debug & DEBUG_ARCH_CODE) {
14598 printf("After transform_to_arch_instructions\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014599 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014600 print_control_flow(state);
14601 }
14602 eliminate_inefectual_code(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014603 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014604 if (state->debug & DEBUG_CODE_ELIMINATION) {
14605 printf("After eliminate_inefectual_code\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014606 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014607 print_control_flow(state);
14608 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014609 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014610 /* Color all of the variables to see if they will fit in registers */
14611 insert_copies_to_phi(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014612 if (state->debug & DEBUG_INSERTED_COPIES) {
14613 printf("After insert_copies_to_phi\n");
14614 print_blocks(state, stdout);
14615 print_control_flow(state);
14616 }
14617 verify_consistency(state);
14618 insert_mandatory_copies(state);
14619 if (state->debug & DEBUG_INSERTED_COPIES) {
14620 printf("After insert_mandatory_copies\n");
14621 print_blocks(state, stdout);
14622 print_control_flow(state);
14623 }
14624 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014625 allocate_registers(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014626 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014627 if (state->debug & DEBUG_INTERMEDIATE_CODE) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014628 print_blocks(state, stdout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014629 }
14630 if (state->debug & DEBUG_CONTROL_FLOW) {
14631 print_control_flow(state);
14632 }
14633 /* Remove the optimization information.
14634 * This is more to check for memory consistency than to free memory.
14635 */
14636 free_basic_blocks(state);
14637}
14638
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014639static void print_op_asm(struct compile_state *state,
14640 struct triple *ins, FILE *fp)
14641{
14642 struct asm_info *info;
14643 const char *ptr;
14644 unsigned lhs, rhs, i;
14645 info = ins->u.ainfo;
14646 lhs = TRIPLE_LHS(ins->sizes);
14647 rhs = TRIPLE_RHS(ins->sizes);
14648 /* Don't count the clobbers in lhs */
14649 for(i = 0; i < lhs; i++) {
14650 if (LHS(ins, i)->type == &void_type) {
14651 break;
14652 }
14653 }
14654 lhs = i;
Eric Biederman8d9c1232003-06-17 08:42:17 +000014655 fprintf(fp, "#ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014656 fputc('\t', fp);
14657 for(ptr = info->str; *ptr; ptr++) {
14658 char *next;
14659 unsigned long param;
14660 struct triple *piece;
14661 if (*ptr != '%') {
14662 fputc(*ptr, fp);
14663 continue;
14664 }
14665 ptr++;
14666 if (*ptr == '%') {
14667 fputc('%', fp);
14668 continue;
14669 }
14670 param = strtoul(ptr, &next, 10);
14671 if (ptr == next) {
14672 error(state, ins, "Invalid asm template");
14673 }
14674 if (param >= (lhs + rhs)) {
14675 error(state, ins, "Invalid param %%%u in asm template",
14676 param);
14677 }
14678 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
14679 fprintf(fp, "%s",
14680 arch_reg_str(ID_REG(piece->id)));
Eric Biederman8d9c1232003-06-17 08:42:17 +000014681 ptr = next -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014682 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000014683 fprintf(fp, "\n#NOT ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014684}
14685
14686
14687/* Only use the low x86 byte registers. This allows me
14688 * allocate the entire register when a byte register is used.
14689 */
14690#define X86_4_8BIT_GPRS 1
14691
14692/* Recognized x86 cpu variants */
14693#define BAD_CPU 0
14694#define CPU_I386 1
14695#define CPU_P3 2
14696#define CPU_P4 3
14697#define CPU_K7 4
14698#define CPU_K8 5
14699
14700#define CPU_DEFAULT CPU_I386
14701
Eric Biedermanb138ac82003-04-22 18:44:01 +000014702/* The x86 register classes */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014703#define REGC_FLAGS 0
14704#define REGC_GPR8 1
14705#define REGC_GPR16 2
14706#define REGC_GPR32 3
14707#define REGC_GPR64 4
14708#define REGC_MMX 5
14709#define REGC_XMM 6
14710#define REGC_GPR32_8 7
14711#define REGC_GPR16_8 8
14712#define REGC_IMM32 9
14713#define REGC_IMM16 10
14714#define REGC_IMM8 11
14715#define LAST_REGC REGC_IMM8
Eric Biedermanb138ac82003-04-22 18:44:01 +000014716#if LAST_REGC >= MAX_REGC
14717#error "MAX_REGC is to low"
14718#endif
14719
14720/* Register class masks */
14721#define REGCM_FLAGS (1 << REGC_FLAGS)
14722#define REGCM_GPR8 (1 << REGC_GPR8)
14723#define REGCM_GPR16 (1 << REGC_GPR16)
14724#define REGCM_GPR32 (1 << REGC_GPR32)
14725#define REGCM_GPR64 (1 << REGC_GPR64)
14726#define REGCM_MMX (1 << REGC_MMX)
14727#define REGCM_XMM (1 << REGC_XMM)
14728#define REGCM_GPR32_8 (1 << REGC_GPR32_8)
14729#define REGCM_GPR16_8 (1 << REGC_GPR16_8)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014730#define REGCM_IMM32 (1 << REGC_IMM32)
14731#define REGCM_IMM16 (1 << REGC_IMM16)
14732#define REGCM_IMM8 (1 << REGC_IMM8)
14733#define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
Eric Biedermanb138ac82003-04-22 18:44:01 +000014734
14735/* The x86 registers */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014736#define REG_EFLAGS 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000014737#define REGC_FLAGS_FIRST REG_EFLAGS
14738#define REGC_FLAGS_LAST REG_EFLAGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014739#define REG_AL 3
14740#define REG_BL 4
14741#define REG_CL 5
14742#define REG_DL 6
14743#define REG_AH 7
14744#define REG_BH 8
14745#define REG_CH 9
14746#define REG_DH 10
Eric Biedermanb138ac82003-04-22 18:44:01 +000014747#define REGC_GPR8_FIRST REG_AL
14748#if X86_4_8BIT_GPRS
14749#define REGC_GPR8_LAST REG_DL
14750#else
14751#define REGC_GPR8_LAST REG_DH
14752#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014753#define REG_AX 11
14754#define REG_BX 12
14755#define REG_CX 13
14756#define REG_DX 14
14757#define REG_SI 15
14758#define REG_DI 16
14759#define REG_BP 17
14760#define REG_SP 18
Eric Biedermanb138ac82003-04-22 18:44:01 +000014761#define REGC_GPR16_FIRST REG_AX
14762#define REGC_GPR16_LAST REG_SP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014763#define REG_EAX 19
14764#define REG_EBX 20
14765#define REG_ECX 21
14766#define REG_EDX 22
14767#define REG_ESI 23
14768#define REG_EDI 24
14769#define REG_EBP 25
14770#define REG_ESP 26
Eric Biedermanb138ac82003-04-22 18:44:01 +000014771#define REGC_GPR32_FIRST REG_EAX
14772#define REGC_GPR32_LAST REG_ESP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014773#define REG_EDXEAX 27
Eric Biedermanb138ac82003-04-22 18:44:01 +000014774#define REGC_GPR64_FIRST REG_EDXEAX
14775#define REGC_GPR64_LAST REG_EDXEAX
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014776#define REG_MMX0 28
14777#define REG_MMX1 29
14778#define REG_MMX2 30
14779#define REG_MMX3 31
14780#define REG_MMX4 32
14781#define REG_MMX5 33
14782#define REG_MMX6 34
14783#define REG_MMX7 35
Eric Biedermanb138ac82003-04-22 18:44:01 +000014784#define REGC_MMX_FIRST REG_MMX0
14785#define REGC_MMX_LAST REG_MMX7
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014786#define REG_XMM0 36
14787#define REG_XMM1 37
14788#define REG_XMM2 38
14789#define REG_XMM3 39
14790#define REG_XMM4 40
14791#define REG_XMM5 41
14792#define REG_XMM6 42
14793#define REG_XMM7 43
Eric Biedermanb138ac82003-04-22 18:44:01 +000014794#define REGC_XMM_FIRST REG_XMM0
14795#define REGC_XMM_LAST REG_XMM7
14796#warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
14797#define LAST_REG REG_XMM7
14798
14799#define REGC_GPR32_8_FIRST REG_EAX
14800#define REGC_GPR32_8_LAST REG_EDX
14801#define REGC_GPR16_8_FIRST REG_AX
14802#define REGC_GPR16_8_LAST REG_DX
14803
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014804#define REGC_IMM8_FIRST -1
14805#define REGC_IMM8_LAST -1
14806#define REGC_IMM16_FIRST -2
14807#define REGC_IMM16_LAST -1
14808#define REGC_IMM32_FIRST -4
14809#define REGC_IMM32_LAST -1
14810
Eric Biedermanb138ac82003-04-22 18:44:01 +000014811#if LAST_REG >= MAX_REGISTERS
14812#error "MAX_REGISTERS to low"
14813#endif
14814
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014815
14816static unsigned regc_size[LAST_REGC +1] = {
14817 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
14818 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
14819 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
14820 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
14821 [REGC_GPR64] = REGC_GPR64_LAST - REGC_GPR64_FIRST + 1,
14822 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
14823 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
14824 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
14825 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
14826 [REGC_IMM32] = 0,
14827 [REGC_IMM16] = 0,
14828 [REGC_IMM8] = 0,
14829};
14830
14831static const struct {
14832 int first, last;
14833} regcm_bound[LAST_REGC + 1] = {
14834 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
14835 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
14836 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
14837 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
14838 [REGC_GPR64] = { REGC_GPR64_FIRST, REGC_GPR64_LAST },
14839 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
14840 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
14841 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
14842 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
14843 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
14844 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
14845 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
14846};
14847
14848static int arch_encode_cpu(const char *cpu)
14849{
14850 struct cpu {
14851 const char *name;
14852 int cpu;
14853 } cpus[] = {
14854 { "i386", CPU_I386 },
14855 { "p3", CPU_P3 },
14856 { "p4", CPU_P4 },
14857 { "k7", CPU_K7 },
14858 { "k8", CPU_K8 },
14859 { 0, BAD_CPU }
14860 };
14861 struct cpu *ptr;
14862 for(ptr = cpus; ptr->name; ptr++) {
14863 if (strcmp(ptr->name, cpu) == 0) {
14864 break;
14865 }
14866 }
14867 return ptr->cpu;
14868}
14869
Eric Biedermanb138ac82003-04-22 18:44:01 +000014870static unsigned arch_regc_size(struct compile_state *state, int class)
14871{
Eric Biedermanb138ac82003-04-22 18:44:01 +000014872 if ((class < 0) || (class > LAST_REGC)) {
14873 return 0;
14874 }
14875 return regc_size[class];
14876}
14877static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
14878{
14879 /* See if two register classes may have overlapping registers */
14880 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 |
14881 REGCM_GPR32_8 | REGCM_GPR32 | REGCM_GPR64;
14882
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014883 /* Special case for the immediates */
14884 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
14885 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
14886 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
14887 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
14888 return 0;
14889 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014890 return (regcm1 & regcm2) ||
14891 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
14892}
14893
14894static void arch_reg_equivs(
14895 struct compile_state *state, unsigned *equiv, int reg)
14896{
14897 if ((reg < 0) || (reg > LAST_REG)) {
14898 internal_error(state, 0, "invalid register");
14899 }
14900 *equiv++ = reg;
14901 switch(reg) {
14902 case REG_AL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014903#if X86_4_8BIT_GPRS
14904 *equiv++ = REG_AH;
14905#endif
14906 *equiv++ = REG_AX;
14907 *equiv++ = REG_EAX;
14908 *equiv++ = REG_EDXEAX;
14909 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014910 case REG_AH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014911#if X86_4_8BIT_GPRS
14912 *equiv++ = REG_AL;
14913#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000014914 *equiv++ = REG_AX;
14915 *equiv++ = REG_EAX;
14916 *equiv++ = REG_EDXEAX;
14917 break;
14918 case REG_BL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014919#if X86_4_8BIT_GPRS
14920 *equiv++ = REG_BH;
14921#endif
14922 *equiv++ = REG_BX;
14923 *equiv++ = REG_EBX;
14924 break;
14925
Eric Biedermanb138ac82003-04-22 18:44:01 +000014926 case REG_BH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014927#if X86_4_8BIT_GPRS
14928 *equiv++ = REG_BL;
14929#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000014930 *equiv++ = REG_BX;
14931 *equiv++ = REG_EBX;
14932 break;
14933 case REG_CL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014934#if X86_4_8BIT_GPRS
14935 *equiv++ = REG_CH;
14936#endif
14937 *equiv++ = REG_CX;
14938 *equiv++ = REG_ECX;
14939 break;
14940
Eric Biedermanb138ac82003-04-22 18:44:01 +000014941 case REG_CH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014942#if X86_4_8BIT_GPRS
14943 *equiv++ = REG_CL;
14944#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000014945 *equiv++ = REG_CX;
14946 *equiv++ = REG_ECX;
14947 break;
14948 case REG_DL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014949#if X86_4_8BIT_GPRS
14950 *equiv++ = REG_DH;
14951#endif
14952 *equiv++ = REG_DX;
14953 *equiv++ = REG_EDX;
14954 *equiv++ = REG_EDXEAX;
14955 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014956 case REG_DH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014957#if X86_4_8BIT_GPRS
14958 *equiv++ = REG_DL;
14959#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000014960 *equiv++ = REG_DX;
14961 *equiv++ = REG_EDX;
14962 *equiv++ = REG_EDXEAX;
14963 break;
14964 case REG_AX:
14965 *equiv++ = REG_AL;
14966 *equiv++ = REG_AH;
14967 *equiv++ = REG_EAX;
14968 *equiv++ = REG_EDXEAX;
14969 break;
14970 case REG_BX:
14971 *equiv++ = REG_BL;
14972 *equiv++ = REG_BH;
14973 *equiv++ = REG_EBX;
14974 break;
14975 case REG_CX:
14976 *equiv++ = REG_CL;
14977 *equiv++ = REG_CH;
14978 *equiv++ = REG_ECX;
14979 break;
14980 case REG_DX:
14981 *equiv++ = REG_DL;
14982 *equiv++ = REG_DH;
14983 *equiv++ = REG_EDX;
14984 *equiv++ = REG_EDXEAX;
14985 break;
14986 case REG_SI:
14987 *equiv++ = REG_ESI;
14988 break;
14989 case REG_DI:
14990 *equiv++ = REG_EDI;
14991 break;
14992 case REG_BP:
14993 *equiv++ = REG_EBP;
14994 break;
14995 case REG_SP:
14996 *equiv++ = REG_ESP;
14997 break;
14998 case REG_EAX:
14999 *equiv++ = REG_AL;
15000 *equiv++ = REG_AH;
15001 *equiv++ = REG_AX;
15002 *equiv++ = REG_EDXEAX;
15003 break;
15004 case REG_EBX:
15005 *equiv++ = REG_BL;
15006 *equiv++ = REG_BH;
15007 *equiv++ = REG_BX;
15008 break;
15009 case REG_ECX:
15010 *equiv++ = REG_CL;
15011 *equiv++ = REG_CH;
15012 *equiv++ = REG_CX;
15013 break;
15014 case REG_EDX:
15015 *equiv++ = REG_DL;
15016 *equiv++ = REG_DH;
15017 *equiv++ = REG_DX;
15018 *equiv++ = REG_EDXEAX;
15019 break;
15020 case REG_ESI:
15021 *equiv++ = REG_SI;
15022 break;
15023 case REG_EDI:
15024 *equiv++ = REG_DI;
15025 break;
15026 case REG_EBP:
15027 *equiv++ = REG_BP;
15028 break;
15029 case REG_ESP:
15030 *equiv++ = REG_SP;
15031 break;
15032 case REG_EDXEAX:
15033 *equiv++ = REG_AL;
15034 *equiv++ = REG_AH;
15035 *equiv++ = REG_DL;
15036 *equiv++ = REG_DH;
15037 *equiv++ = REG_AX;
15038 *equiv++ = REG_DX;
15039 *equiv++ = REG_EAX;
15040 *equiv++ = REG_EDX;
15041 break;
15042 }
15043 *equiv++ = REG_UNSET;
15044}
15045
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015046static unsigned arch_avail_mask(struct compile_state *state)
15047{
15048 unsigned avail_mask;
15049 avail_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 |
15050 REGCM_GPR32 | REGCM_GPR32_8 | REGCM_GPR64 |
15051 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
15052 switch(state->cpu) {
15053 case CPU_P3:
15054 case CPU_K7:
15055 avail_mask |= REGCM_MMX;
15056 break;
15057 case CPU_P4:
15058 case CPU_K8:
15059 avail_mask |= REGCM_MMX | REGCM_XMM;
15060 break;
15061 }
15062#if 0
15063 /* Don't enable 8 bit values until I can force both operands
15064 * to be 8bits simultaneously.
15065 */
15066 avail_mask &= ~(REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16);
15067#endif
15068 return avail_mask;
15069}
15070
15071static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
15072{
15073 unsigned mask, result;
15074 int class, class2;
15075 result = regcm;
15076 result &= arch_avail_mask(state);
15077
15078 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
15079 if ((result & mask) == 0) {
15080 continue;
15081 }
15082 if (class > LAST_REGC) {
15083 result &= ~mask;
15084 }
15085 for(class2 = 0; class2 <= LAST_REGC; class2++) {
15086 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
15087 (regcm_bound[class2].last <= regcm_bound[class].last)) {
15088 result |= (1 << class2);
15089 }
15090 }
15091 }
15092 return result;
15093}
Eric Biedermanb138ac82003-04-22 18:44:01 +000015094
15095static unsigned arch_reg_regcm(struct compile_state *state, int reg)
15096{
Eric Biedermanb138ac82003-04-22 18:44:01 +000015097 unsigned mask;
15098 int class;
15099 mask = 0;
15100 for(class = 0; class <= LAST_REGC; class++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015101 if ((reg >= regcm_bound[class].first) &&
15102 (reg <= regcm_bound[class].last)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015103 mask |= (1 << class);
15104 }
15105 }
15106 if (!mask) {
15107 internal_error(state, 0, "reg %d not in any class", reg);
15108 }
15109 return mask;
15110}
15111
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015112static struct reg_info arch_reg_constraint(
15113 struct compile_state *state, struct type *type, const char *constraint)
15114{
15115 static const struct {
15116 char class;
15117 unsigned int mask;
15118 unsigned int reg;
15119 } constraints[] = {
15120 { 'r', REGCM_GPR32, REG_UNSET },
15121 { 'g', REGCM_GPR32, REG_UNSET },
15122 { 'p', REGCM_GPR32, REG_UNSET },
15123 { 'q', REGCM_GPR8, REG_UNSET },
15124 { 'Q', REGCM_GPR32_8, REG_UNSET },
15125 { 'x', REGCM_XMM, REG_UNSET },
15126 { 'y', REGCM_MMX, REG_UNSET },
15127 { 'a', REGCM_GPR32, REG_EAX },
15128 { 'b', REGCM_GPR32, REG_EBX },
15129 { 'c', REGCM_GPR32, REG_ECX },
15130 { 'd', REGCM_GPR32, REG_EDX },
15131 { 'D', REGCM_GPR32, REG_EDI },
15132 { 'S', REGCM_GPR32, REG_ESI },
15133 { '\0', 0, REG_UNSET },
15134 };
15135 unsigned int regcm;
15136 unsigned int mask, reg;
15137 struct reg_info result;
15138 const char *ptr;
15139 regcm = arch_type_to_regcm(state, type);
15140 reg = REG_UNSET;
15141 mask = 0;
15142 for(ptr = constraint; *ptr; ptr++) {
15143 int i;
15144 if (*ptr == ' ') {
15145 continue;
15146 }
15147 for(i = 0; constraints[i].class != '\0'; i++) {
15148 if (constraints[i].class == *ptr) {
15149 break;
15150 }
15151 }
15152 if (constraints[i].class == '\0') {
15153 error(state, 0, "invalid register constraint ``%c''", *ptr);
15154 break;
15155 }
15156 if ((constraints[i].mask & regcm) == 0) {
15157 error(state, 0, "invalid register class %c specified",
15158 *ptr);
15159 }
15160 mask |= constraints[i].mask;
15161 if (constraints[i].reg != REG_UNSET) {
15162 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
15163 error(state, 0, "Only one register may be specified");
15164 }
15165 reg = constraints[i].reg;
15166 }
15167 }
15168 result.reg = reg;
15169 result.regcm = mask;
15170 return result;
15171}
15172
15173static struct reg_info arch_reg_clobber(
15174 struct compile_state *state, const char *clobber)
15175{
15176 struct reg_info result;
15177 if (strcmp(clobber, "memory") == 0) {
15178 result.reg = REG_UNSET;
15179 result.regcm = 0;
15180 }
15181 else if (strcmp(clobber, "%eax") == 0) {
15182 result.reg = REG_EAX;
15183 result.regcm = REGCM_GPR32;
15184 }
15185 else if (strcmp(clobber, "%ebx") == 0) {
15186 result.reg = REG_EBX;
15187 result.regcm = REGCM_GPR32;
15188 }
15189 else if (strcmp(clobber, "%ecx") == 0) {
15190 result.reg = REG_ECX;
15191 result.regcm = REGCM_GPR32;
15192 }
15193 else if (strcmp(clobber, "%edx") == 0) {
15194 result.reg = REG_EDX;
15195 result.regcm = REGCM_GPR32;
15196 }
15197 else if (strcmp(clobber, "%esi") == 0) {
15198 result.reg = REG_ESI;
15199 result.regcm = REGCM_GPR32;
15200 }
15201 else if (strcmp(clobber, "%edi") == 0) {
15202 result.reg = REG_EDI;
15203 result.regcm = REGCM_GPR32;
15204 }
15205 else if (strcmp(clobber, "%ebp") == 0) {
15206 result.reg = REG_EBP;
15207 result.regcm = REGCM_GPR32;
15208 }
15209 else if (strcmp(clobber, "%esp") == 0) {
15210 result.reg = REG_ESP;
15211 result.regcm = REGCM_GPR32;
15212 }
15213 else if (strcmp(clobber, "cc") == 0) {
15214 result.reg = REG_EFLAGS;
15215 result.regcm = REGCM_FLAGS;
15216 }
15217 else if ((strncmp(clobber, "xmm", 3) == 0) &&
15218 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15219 result.reg = REG_XMM0 + octdigval(clobber[3]);
15220 result.regcm = REGCM_XMM;
15221 }
15222 else if ((strncmp(clobber, "mmx", 3) == 0) &&
15223 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15224 result.reg = REG_MMX0 + octdigval(clobber[3]);
15225 result.regcm = REGCM_MMX;
15226 }
15227 else {
15228 error(state, 0, "Invalid register clobber");
15229 result.reg = REG_UNSET;
15230 result.regcm = 0;
15231 }
15232 return result;
15233}
15234
Eric Biedermanb138ac82003-04-22 18:44:01 +000015235static int do_select_reg(struct compile_state *state,
15236 char *used, int reg, unsigned classes)
15237{
15238 unsigned mask;
15239 if (used[reg]) {
15240 return REG_UNSET;
15241 }
15242 mask = arch_reg_regcm(state, reg);
15243 return (classes & mask) ? reg : REG_UNSET;
15244}
15245
15246static int arch_select_free_register(
15247 struct compile_state *state, char *used, int classes)
15248{
15249 /* Preference: flags, 8bit gprs, 32bit gprs, other 32bit reg
15250 * other types of registers.
15251 */
15252 int i, reg;
15253 reg = REG_UNSET;
15254 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
15255 reg = do_select_reg(state, used, i, classes);
15256 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015257 for(i = REGC_GPR32_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR32_LAST); i++) {
15258 reg = do_select_reg(state, used, i, classes);
15259 }
15260 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
15261 reg = do_select_reg(state, used, i, classes);
15262 }
15263 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
15264 reg = do_select_reg(state, used, i, classes);
15265 }
15266 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
15267 reg = do_select_reg(state, used, i, classes);
15268 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015269 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
15270 reg = do_select_reg(state, used, i, classes);
15271 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015272 for(i = REGC_GPR64_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR64_LAST); i++) {
15273 reg = do_select_reg(state, used, i, classes);
15274 }
15275 return reg;
15276}
15277
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015278
Eric Biedermanb138ac82003-04-22 18:44:01 +000015279static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
15280{
15281#warning "FIXME force types smaller (if legal) before I get here"
Eric Biedermanb138ac82003-04-22 18:44:01 +000015282 unsigned avail_mask;
15283 unsigned mask;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015284 mask = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015285 avail_mask = arch_avail_mask(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015286 switch(type->type & TYPE_MASK) {
15287 case TYPE_ARRAY:
15288 case TYPE_VOID:
15289 mask = 0;
15290 break;
15291 case TYPE_CHAR:
15292 case TYPE_UCHAR:
15293 mask = REGCM_GPR8 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015294 REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000015295 REGCM_GPR32 | REGCM_GPR32_8 |
15296 REGCM_GPR64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015297 REGCM_MMX | REGCM_XMM |
15298 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015299 break;
15300 case TYPE_SHORT:
15301 case TYPE_USHORT:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015302 mask = REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000015303 REGCM_GPR32 | REGCM_GPR32_8 |
15304 REGCM_GPR64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015305 REGCM_MMX | REGCM_XMM |
15306 REGCM_IMM32 | REGCM_IMM16;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015307 break;
15308 case TYPE_INT:
15309 case TYPE_UINT:
15310 case TYPE_LONG:
15311 case TYPE_ULONG:
15312 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015313 mask = REGCM_GPR32 | REGCM_GPR32_8 |
15314 REGCM_GPR64 | REGCM_MMX | REGCM_XMM |
15315 REGCM_IMM32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015316 break;
15317 default:
15318 internal_error(state, 0, "no register class for type");
15319 break;
15320 }
15321 mask &= avail_mask;
15322 return mask;
15323}
15324
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015325static int is_imm32(struct triple *imm)
15326{
15327 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
15328 (imm->op == OP_ADDRCONST);
15329
15330}
15331static int is_imm16(struct triple *imm)
15332{
15333 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
15334}
15335static int is_imm8(struct triple *imm)
15336{
15337 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
15338}
15339
15340static int get_imm32(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015341{
15342 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015343 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015344 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000015345 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015346 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015347 if (!is_imm32(imm)) {
15348 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015349 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015350 unuse_triple(*expr, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015351 use_triple(imm, ins);
15352 *expr = imm;
15353 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015354}
15355
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015356static int get_imm8(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015357{
15358 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015359 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015360 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000015361 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015362 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015363 if (!is_imm8(imm)) {
15364 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015365 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015366 unuse_triple(*expr, ins);
15367 use_triple(imm, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015368 *expr = imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015369 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015370}
15371
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015372#define TEMPLATE_NOP 0
15373#define TEMPLATE_INTCONST8 1
15374#define TEMPLATE_INTCONST32 2
15375#define TEMPLATE_COPY_REG 3
15376#define TEMPLATE_COPY_IMM32 4
15377#define TEMPLATE_COPY_IMM16 5
15378#define TEMPLATE_COPY_IMM8 6
15379#define TEMPLATE_PHI 7
15380#define TEMPLATE_STORE8 8
15381#define TEMPLATE_STORE16 9
15382#define TEMPLATE_STORE32 10
15383#define TEMPLATE_LOAD8 11
15384#define TEMPLATE_LOAD16 12
15385#define TEMPLATE_LOAD32 13
15386#define TEMPLATE_BINARY_REG 14
15387#define TEMPLATE_BINARY_IMM 15
15388#define TEMPLATE_SL_CL 16
15389#define TEMPLATE_SL_IMM 17
15390#define TEMPLATE_UNARY 18
15391#define TEMPLATE_CMP_REG 19
15392#define TEMPLATE_CMP_IMM 20
15393#define TEMPLATE_TEST 21
15394#define TEMPLATE_SET 22
15395#define TEMPLATE_JMP 23
15396#define TEMPLATE_INB_DX 24
15397#define TEMPLATE_INB_IMM 25
15398#define TEMPLATE_INW_DX 26
15399#define TEMPLATE_INW_IMM 27
15400#define TEMPLATE_INL_DX 28
15401#define TEMPLATE_INL_IMM 29
15402#define TEMPLATE_OUTB_DX 30
15403#define TEMPLATE_OUTB_IMM 31
15404#define TEMPLATE_OUTW_DX 32
15405#define TEMPLATE_OUTW_IMM 33
15406#define TEMPLATE_OUTL_DX 34
15407#define TEMPLATE_OUTL_IMM 35
15408#define TEMPLATE_BSF 36
15409#define TEMPLATE_RDMSR 37
15410#define TEMPLATE_WRMSR 38
15411#define LAST_TEMPLATE TEMPLATE_WRMSR
15412#if LAST_TEMPLATE >= MAX_TEMPLATES
15413#error "MAX_TEMPLATES to low"
15414#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015415
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015416#define COPY_REGCM (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8 | REGCM_MMX | REGCM_XMM)
15417#define COPY32_REGCM (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015418
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015419static struct ins_template templates[] = {
15420 [TEMPLATE_NOP] = {},
15421 [TEMPLATE_INTCONST8] = {
15422 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15423 },
15424 [TEMPLATE_INTCONST32] = {
15425 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
15426 },
15427 [TEMPLATE_COPY_REG] = {
15428 .lhs = { [0] = { REG_UNSET, COPY_REGCM } },
15429 .rhs = { [0] = { REG_UNSET, COPY_REGCM } },
15430 },
15431 [TEMPLATE_COPY_IMM32] = {
15432 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
15433 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
15434 },
15435 [TEMPLATE_COPY_IMM16] = {
15436 .lhs = { [0] = { REG_UNSET, COPY32_REGCM | REGCM_GPR16 } },
15437 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 } },
15438 },
15439 [TEMPLATE_COPY_IMM8] = {
15440 .lhs = { [0] = { REG_UNSET, COPY_REGCM } },
15441 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15442 },
15443 [TEMPLATE_PHI] = {
15444 .lhs = { [0] = { REG_VIRT0, COPY_REGCM } },
15445 .rhs = {
15446 [ 0] = { REG_VIRT0, COPY_REGCM },
15447 [ 1] = { REG_VIRT0, COPY_REGCM },
15448 [ 2] = { REG_VIRT0, COPY_REGCM },
15449 [ 3] = { REG_VIRT0, COPY_REGCM },
15450 [ 4] = { REG_VIRT0, COPY_REGCM },
15451 [ 5] = { REG_VIRT0, COPY_REGCM },
15452 [ 6] = { REG_VIRT0, COPY_REGCM },
15453 [ 7] = { REG_VIRT0, COPY_REGCM },
15454 [ 8] = { REG_VIRT0, COPY_REGCM },
15455 [ 9] = { REG_VIRT0, COPY_REGCM },
15456 [10] = { REG_VIRT0, COPY_REGCM },
15457 [11] = { REG_VIRT0, COPY_REGCM },
15458 [12] = { REG_VIRT0, COPY_REGCM },
15459 [13] = { REG_VIRT0, COPY_REGCM },
15460 [14] = { REG_VIRT0, COPY_REGCM },
15461 [15] = { REG_VIRT0, COPY_REGCM },
15462 }, },
15463 [TEMPLATE_STORE8] = {
15464 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15465 .rhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15466 },
15467 [TEMPLATE_STORE16] = {
15468 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15469 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
15470 },
15471 [TEMPLATE_STORE32] = {
15472 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15473 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15474 },
15475 [TEMPLATE_LOAD8] = {
15476 .lhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15477 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15478 },
15479 [TEMPLATE_LOAD16] = {
15480 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
15481 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15482 },
15483 [TEMPLATE_LOAD32] = {
15484 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15485 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15486 },
15487 [TEMPLATE_BINARY_REG] = {
15488 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15489 .rhs = {
15490 [0] = { REG_VIRT0, REGCM_GPR32 },
15491 [1] = { REG_UNSET, REGCM_GPR32 },
15492 },
15493 },
15494 [TEMPLATE_BINARY_IMM] = {
15495 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15496 .rhs = {
15497 [0] = { REG_VIRT0, REGCM_GPR32 },
15498 [1] = { REG_UNNEEDED, REGCM_IMM32 },
15499 },
15500 },
15501 [TEMPLATE_SL_CL] = {
15502 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15503 .rhs = {
15504 [0] = { REG_VIRT0, REGCM_GPR32 },
15505 [1] = { REG_CL, REGCM_GPR8 },
15506 },
15507 },
15508 [TEMPLATE_SL_IMM] = {
15509 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15510 .rhs = {
15511 [0] = { REG_VIRT0, REGCM_GPR32 },
15512 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15513 },
15514 },
15515 [TEMPLATE_UNARY] = {
15516 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15517 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15518 },
15519 [TEMPLATE_CMP_REG] = {
15520 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15521 .rhs = {
15522 [0] = { REG_UNSET, REGCM_GPR32 },
15523 [1] = { REG_UNSET, REGCM_GPR32 },
15524 },
15525 },
15526 [TEMPLATE_CMP_IMM] = {
15527 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15528 .rhs = {
15529 [0] = { REG_UNSET, REGCM_GPR32 },
15530 [1] = { REG_UNNEEDED, REGCM_IMM32 },
15531 },
15532 },
15533 [TEMPLATE_TEST] = {
15534 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15535 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15536 },
15537 [TEMPLATE_SET] = {
15538 .lhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15539 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15540 },
15541 [TEMPLATE_JMP] = {
15542 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15543 },
15544 [TEMPLATE_INB_DX] = {
15545 .lhs = { [0] = { REG_AL, REGCM_GPR8 } },
15546 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15547 },
15548 [TEMPLATE_INB_IMM] = {
15549 .lhs = { [0] = { REG_AL, REGCM_GPR8 } },
15550 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15551 },
15552 [TEMPLATE_INW_DX] = {
15553 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
15554 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15555 },
15556 [TEMPLATE_INW_IMM] = {
15557 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
15558 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15559 },
15560 [TEMPLATE_INL_DX] = {
15561 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
15562 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15563 },
15564 [TEMPLATE_INL_IMM] = {
15565 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
15566 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15567 },
15568 [TEMPLATE_OUTB_DX] = {
15569 .rhs = {
15570 [0] = { REG_AL, REGCM_GPR8 },
15571 [1] = { REG_DX, REGCM_GPR16 },
15572 },
15573 },
15574 [TEMPLATE_OUTB_IMM] = {
15575 .rhs = {
15576 [0] = { REG_AL, REGCM_GPR8 },
15577 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15578 },
15579 },
15580 [TEMPLATE_OUTW_DX] = {
15581 .rhs = {
15582 [0] = { REG_AX, REGCM_GPR16 },
15583 [1] = { REG_DX, REGCM_GPR16 },
15584 },
15585 },
15586 [TEMPLATE_OUTW_IMM] = {
15587 .rhs = {
15588 [0] = { REG_AX, REGCM_GPR16 },
15589 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15590 },
15591 },
15592 [TEMPLATE_OUTL_DX] = {
15593 .rhs = {
15594 [0] = { REG_EAX, REGCM_GPR32 },
15595 [1] = { REG_DX, REGCM_GPR16 },
15596 },
15597 },
15598 [TEMPLATE_OUTL_IMM] = {
15599 .rhs = {
15600 [0] = { REG_EAX, REGCM_GPR32 },
15601 [1] = { REG_UNNEEDED, REGCM_IMM8 },
15602 },
15603 },
15604 [TEMPLATE_BSF] = {
15605 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15606 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15607 },
15608 [TEMPLATE_RDMSR] = {
15609 .lhs = {
15610 [0] = { REG_EAX, REGCM_GPR32 },
15611 [1] = { REG_EDX, REGCM_GPR32 },
15612 },
15613 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
15614 },
15615 [TEMPLATE_WRMSR] = {
15616 .rhs = {
15617 [0] = { REG_ECX, REGCM_GPR32 },
15618 [1] = { REG_EAX, REGCM_GPR32 },
15619 [2] = { REG_EDX, REGCM_GPR32 },
15620 },
15621 },
15622};
Eric Biedermanb138ac82003-04-22 18:44:01 +000015623
15624static void fixup_branches(struct compile_state *state,
15625 struct triple *cmp, struct triple *use, int jmp_op)
15626{
15627 struct triple_set *entry, *next;
15628 for(entry = use->use; entry; entry = next) {
15629 next = entry->next;
15630 if (entry->member->op == OP_COPY) {
15631 fixup_branches(state, cmp, entry->member, jmp_op);
15632 }
15633 else if (entry->member->op == OP_BRANCH) {
15634 struct triple *branch, *test;
Eric Biederman0babc1c2003-05-09 02:39:00 +000015635 struct triple *left, *right;
15636 left = right = 0;
15637 left = RHS(cmp, 0);
15638 if (TRIPLE_RHS(cmp->sizes) > 1) {
15639 right = RHS(cmp, 1);
15640 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015641 branch = entry->member;
15642 test = pre_triple(state, branch,
Eric Biederman0babc1c2003-05-09 02:39:00 +000015643 cmp->op, cmp->type, left, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015644 test->template_id = TEMPLATE_TEST;
15645 if (cmp->op == OP_CMP) {
15646 test->template_id = TEMPLATE_CMP_REG;
15647 if (get_imm32(test, &RHS(test, 1))) {
15648 test->template_id = TEMPLATE_CMP_IMM;
15649 }
15650 }
15651 use_triple(RHS(test, 0), test);
15652 use_triple(RHS(test, 1), test);
Eric Biederman0babc1c2003-05-09 02:39:00 +000015653 unuse_triple(RHS(branch, 0), branch);
15654 RHS(branch, 0) = test;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015655 branch->op = jmp_op;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015656 branch->template_id = TEMPLATE_JMP;
Eric Biederman0babc1c2003-05-09 02:39:00 +000015657 use_triple(RHS(branch, 0), branch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015658 }
15659 }
15660}
15661
15662static void bool_cmp(struct compile_state *state,
15663 struct triple *ins, int cmp_op, int jmp_op, int set_op)
15664{
Eric Biedermanb138ac82003-04-22 18:44:01 +000015665 struct triple_set *entry, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015666 struct triple *set;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015667
15668 /* Put a barrier up before the cmp which preceeds the
15669 * copy instruction. If a set actually occurs this gives
15670 * us a chance to move variables in registers out of the way.
15671 */
15672
15673 /* Modify the comparison operator */
15674 ins->op = cmp_op;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015675 ins->template_id = TEMPLATE_TEST;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015676 if (cmp_op == OP_CMP) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015677 ins->template_id = TEMPLATE_CMP_REG;
15678 if (get_imm32(ins, &RHS(ins, 1))) {
15679 ins->template_id = TEMPLATE_CMP_IMM;
15680 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015681 }
15682 /* Generate the instruction sequence that will transform the
15683 * result of the comparison into a logical value.
15684 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015685 set = post_triple(state, ins, set_op, ins->type, ins, 0);
15686 use_triple(ins, set);
15687 set->template_id = TEMPLATE_SET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015688
Eric Biedermanb138ac82003-04-22 18:44:01 +000015689 for(entry = ins->use; entry; entry = next) {
15690 next = entry->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015691 if (entry->member == set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015692 continue;
15693 }
15694 replace_rhs_use(state, ins, set, entry->member);
15695 }
15696 fixup_branches(state, ins, set, jmp_op);
15697}
15698
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015699static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
Eric Biederman0babc1c2003-05-09 02:39:00 +000015700{
15701 struct triple *next;
15702 int lhs, i;
15703 lhs = TRIPLE_LHS(ins->sizes);
15704 for(next = ins->next, i = 0; i < lhs; i++, next = next->next) {
15705 if (next != LHS(ins, i)) {
15706 internal_error(state, ins, "malformed lhs on %s",
15707 tops(ins->op));
15708 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015709 if (next->op != OP_PIECE) {
15710 internal_error(state, ins, "bad lhs op %s at %d on %s",
15711 tops(next->op), i, tops(ins->op));
15712 }
15713 if (next->u.cval != i) {
15714 internal_error(state, ins, "bad u.cval of %d %d expected",
15715 next->u.cval, i);
15716 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015717 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015718 return next;
Eric Biederman0babc1c2003-05-09 02:39:00 +000015719}
Eric Biedermanb138ac82003-04-22 18:44:01 +000015720
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015721struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
15722{
15723 struct ins_template *template;
15724 struct reg_info result;
15725 int zlhs;
15726 if (ins->op == OP_PIECE) {
15727 index = ins->u.cval;
15728 ins = MISC(ins, 0);
15729 }
15730 zlhs = TRIPLE_LHS(ins->sizes);
15731 if (triple_is_def(state, ins)) {
15732 zlhs = 1;
15733 }
15734 if (index >= zlhs) {
15735 internal_error(state, ins, "index %d out of range for %s\n",
15736 index, tops(ins->op));
15737 }
15738 switch(ins->op) {
15739 case OP_ASM:
15740 template = &ins->u.ainfo->tmpl;
15741 break;
15742 default:
15743 if (ins->template_id > LAST_TEMPLATE) {
15744 internal_error(state, ins, "bad template number %d",
15745 ins->template_id);
15746 }
15747 template = &templates[ins->template_id];
15748 break;
15749 }
15750 result = template->lhs[index];
15751 result.regcm = arch_regcm_normalize(state, result.regcm);
15752 if (result.reg != REG_UNNEEDED) {
15753 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
15754 }
15755 if (result.regcm == 0) {
15756 internal_error(state, ins, "lhs %d regcm == 0", index);
15757 }
15758 return result;
15759}
15760
15761struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
15762{
15763 struct reg_info result;
15764 struct ins_template *template;
15765 if ((index > TRIPLE_RHS(ins->sizes)) ||
15766 (ins->op == OP_PIECE)) {
15767 internal_error(state, ins, "index %d out of range for %s\n",
15768 index, tops(ins->op));
15769 }
15770 switch(ins->op) {
15771 case OP_ASM:
15772 template = &ins->u.ainfo->tmpl;
15773 break;
15774 default:
15775 if (ins->template_id > LAST_TEMPLATE) {
15776 internal_error(state, ins, "bad template number %d",
15777 ins->template_id);
15778 }
15779 template = &templates[ins->template_id];
15780 break;
15781 }
15782 result = template->rhs[index];
15783 result.regcm = arch_regcm_normalize(state, result.regcm);
15784 if (result.regcm == 0) {
15785 internal_error(state, ins, "rhs %d regcm == 0", index);
15786 }
15787 return result;
15788}
15789
15790static struct triple *transform_to_arch_instruction(
15791 struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015792{
15793 /* Transform from generic 3 address instructions
15794 * to archtecture specific instructions.
15795 * And apply architecture specific constrains to instructions.
15796 * Copies are inserted to preserve the register flexibility
15797 * of 3 address instructions.
15798 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015799 struct triple *next;
15800 next = ins->next;
15801 switch(ins->op) {
15802 case OP_INTCONST:
15803 ins->template_id = TEMPLATE_INTCONST32;
15804 if (ins->u.cval < 256) {
15805 ins->template_id = TEMPLATE_INTCONST8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015806 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015807 break;
15808 case OP_ADDRCONST:
15809 ins->template_id = TEMPLATE_INTCONST32;
15810 break;
15811 case OP_NOOP:
15812 case OP_SDECL:
15813 case OP_BLOBCONST:
15814 case OP_LABEL:
15815 ins->template_id = TEMPLATE_NOP;
15816 break;
15817 case OP_COPY:
15818 ins->template_id = TEMPLATE_COPY_REG;
15819 if (is_imm8(RHS(ins, 0))) {
15820 ins->template_id = TEMPLATE_COPY_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015821 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015822 else if (is_imm16(RHS(ins, 0))) {
15823 ins->template_id = TEMPLATE_COPY_IMM16;
15824 }
15825 else if (is_imm32(RHS(ins, 0))) {
15826 ins->template_id = TEMPLATE_COPY_IMM32;
15827 }
15828 else if (is_const(RHS(ins, 0))) {
15829 internal_error(state, ins, "bad constant passed to copy");
15830 }
15831 break;
15832 case OP_PHI:
15833 ins->template_id = TEMPLATE_PHI;
15834 break;
15835 case OP_STORE:
15836 switch(ins->type->type & TYPE_MASK) {
15837 case TYPE_CHAR: case TYPE_UCHAR:
15838 ins->template_id = TEMPLATE_STORE8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015839 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015840 case TYPE_SHORT: case TYPE_USHORT:
15841 ins->template_id = TEMPLATE_STORE16;
Eric Biederman0babc1c2003-05-09 02:39:00 +000015842 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015843 case TYPE_INT: case TYPE_UINT:
15844 case TYPE_LONG: case TYPE_ULONG:
15845 case TYPE_POINTER:
15846 ins->template_id = TEMPLATE_STORE32;
Eric Biederman0babc1c2003-05-09 02:39:00 +000015847 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015848 default:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015849 internal_error(state, ins, "unknown type in store");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015850 break;
15851 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015852 break;
15853 case OP_LOAD:
15854 switch(ins->type->type & TYPE_MASK) {
15855 case TYPE_CHAR: case TYPE_UCHAR:
15856 ins->template_id = TEMPLATE_LOAD8;
15857 break;
15858 case TYPE_SHORT:
15859 case TYPE_USHORT:
15860 ins->template_id = TEMPLATE_LOAD16;
15861 break;
15862 case TYPE_INT:
15863 case TYPE_UINT:
15864 case TYPE_LONG:
15865 case TYPE_ULONG:
15866 case TYPE_POINTER:
15867 ins->template_id = TEMPLATE_LOAD32;
15868 break;
15869 default:
15870 internal_error(state, ins, "unknown type in load");
15871 break;
15872 }
15873 break;
15874 case OP_ADD:
15875 case OP_SUB:
15876 case OP_AND:
15877 case OP_XOR:
15878 case OP_OR:
15879 case OP_SMUL:
15880 ins->template_id = TEMPLATE_BINARY_REG;
15881 if (get_imm32(ins, &RHS(ins, 1))) {
15882 ins->template_id = TEMPLATE_BINARY_IMM;
15883 }
15884 break;
15885 case OP_SL:
15886 case OP_SSR:
15887 case OP_USR:
15888 ins->template_id = TEMPLATE_SL_CL;
15889 if (get_imm8(ins, &RHS(ins, 1))) {
15890 ins->template_id = TEMPLATE_SL_IMM;
15891 }
15892 break;
15893 case OP_INVERT:
15894 case OP_NEG:
15895 ins->template_id = TEMPLATE_UNARY;
15896 break;
15897 case OP_EQ:
15898 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
15899 break;
15900 case OP_NOTEQ:
15901 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
15902 break;
15903 case OP_SLESS:
15904 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
15905 break;
15906 case OP_ULESS:
15907 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
15908 break;
15909 case OP_SMORE:
15910 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
15911 break;
15912 case OP_UMORE:
15913 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
15914 break;
15915 case OP_SLESSEQ:
15916 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
15917 break;
15918 case OP_ULESSEQ:
15919 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
15920 break;
15921 case OP_SMOREEQ:
15922 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
15923 break;
15924 case OP_UMOREEQ:
15925 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
15926 break;
15927 case OP_LTRUE:
15928 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
15929 break;
15930 case OP_LFALSE:
15931 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
15932 break;
15933 case OP_BRANCH:
15934 if (TRIPLE_RHS(ins->sizes) > 0) {
15935 internal_error(state, ins, "bad branch test");
15936 }
15937 ins->op = OP_JMP;
15938 ins->template_id = TEMPLATE_NOP;
15939 break;
15940 case OP_INB:
15941 case OP_INW:
15942 case OP_INL:
15943 switch(ins->op) {
15944 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
15945 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
15946 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
15947 }
15948 if (get_imm8(ins, &RHS(ins, 0))) {
15949 ins->template_id += 1;
15950 }
15951 break;
15952 case OP_OUTB:
15953 case OP_OUTW:
15954 case OP_OUTL:
15955 switch(ins->op) {
15956 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
15957 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
15958 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
15959 }
15960 if (get_imm8(ins, &RHS(ins, 1))) {
15961 ins->template_id += 1;
15962 }
15963 break;
15964 case OP_BSF:
15965 case OP_BSR:
15966 ins->template_id = TEMPLATE_BSF;
15967 break;
15968 case OP_RDMSR:
15969 ins->template_id = TEMPLATE_RDMSR;
15970 next = after_lhs(state, ins);
15971 break;
15972 case OP_WRMSR:
15973 ins->template_id = TEMPLATE_WRMSR;
15974 break;
15975 case OP_HLT:
15976 ins->template_id = TEMPLATE_NOP;
15977 break;
15978 case OP_ASM:
15979 ins->template_id = TEMPLATE_NOP;
15980 next = after_lhs(state, ins);
15981 break;
15982 /* Already transformed instructions */
15983 case OP_TEST:
15984 ins->template_id = TEMPLATE_TEST;
15985 break;
15986 case OP_CMP:
15987 ins->template_id = TEMPLATE_CMP_REG;
15988 if (get_imm32(ins, &RHS(ins, 1))) {
15989 ins->template_id = TEMPLATE_CMP_IMM;
15990 }
15991 break;
15992 case OP_JMP_EQ: case OP_JMP_NOTEQ:
15993 case OP_JMP_SLESS: case OP_JMP_ULESS:
15994 case OP_JMP_SMORE: case OP_JMP_UMORE:
15995 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
15996 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
15997 ins->template_id = TEMPLATE_JMP;
15998 break;
15999 case OP_SET_EQ: case OP_SET_NOTEQ:
16000 case OP_SET_SLESS: case OP_SET_ULESS:
16001 case OP_SET_SMORE: case OP_SET_UMORE:
16002 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
16003 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
16004 ins->template_id = TEMPLATE_SET;
16005 break;
16006 /* Unhandled instructions */
16007 case OP_PIECE:
16008 default:
16009 internal_error(state, ins, "unhandled ins: %d %s\n",
16010 ins->op, tops(ins->op));
16011 break;
16012 }
16013 return next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016014}
16015
Eric Biedermanb138ac82003-04-22 18:44:01 +000016016static void generate_local_labels(struct compile_state *state)
16017{
16018 struct triple *first, *label;
16019 int label_counter;
16020 label_counter = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016021 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016022 label = first;
16023 do {
16024 if ((label->op == OP_LABEL) ||
16025 (label->op == OP_SDECL)) {
16026 if (label->use) {
16027 label->u.cval = ++label_counter;
16028 } else {
16029 label->u.cval = 0;
16030 }
16031
16032 }
16033 label = label->next;
16034 } while(label != first);
16035}
16036
16037static int check_reg(struct compile_state *state,
16038 struct triple *triple, int classes)
16039{
16040 unsigned mask;
16041 int reg;
16042 reg = ID_REG(triple->id);
16043 if (reg == REG_UNSET) {
16044 internal_error(state, triple, "register not set");
16045 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016046 mask = arch_reg_regcm(state, reg);
16047 if (!(classes & mask)) {
16048 internal_error(state, triple, "reg %d in wrong class",
16049 reg);
16050 }
16051 return reg;
16052}
16053
16054static const char *arch_reg_str(int reg)
16055{
16056 static const char *regs[] = {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000016057 "%unset",
16058 "%unneeded",
Eric Biedermanb138ac82003-04-22 18:44:01 +000016059 "%eflags",
16060 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
16061 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
16062 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
16063 "%edx:%eax",
16064 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
16065 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
16066 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
16067 };
16068 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
16069 reg = 0;
16070 }
16071 return regs[reg];
16072}
16073
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016074
Eric Biedermanb138ac82003-04-22 18:44:01 +000016075static const char *reg(struct compile_state *state, struct triple *triple,
16076 int classes)
16077{
16078 int reg;
16079 reg = check_reg(state, triple, classes);
16080 return arch_reg_str(reg);
16081}
16082
16083const char *type_suffix(struct compile_state *state, struct type *type)
16084{
16085 const char *suffix;
16086 switch(size_of(state, type)) {
16087 case 1: suffix = "b"; break;
16088 case 2: suffix = "w"; break;
16089 case 4: suffix = "l"; break;
16090 default:
16091 internal_error(state, 0, "unknown suffix");
16092 suffix = 0;
16093 break;
16094 }
16095 return suffix;
16096}
16097
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016098static void print_const_val(
16099 struct compile_state *state, struct triple *ins, FILE *fp)
16100{
16101 switch(ins->op) {
16102 case OP_INTCONST:
16103 fprintf(fp, " $%ld ",
16104 (long_t)(ins->u.cval));
16105 break;
16106 case OP_ADDRCONST:
Eric Biederman05f26fc2003-06-11 21:55:00 +000016107 fprintf(fp, " $L%s%lu+%lu ",
16108 state->label_prefix,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016109 MISC(ins, 0)->u.cval,
16110 ins->u.cval);
16111 break;
16112 default:
16113 internal_error(state, ins, "unknown constant type");
16114 break;
16115 }
16116}
16117
Eric Biedermanb138ac82003-04-22 18:44:01 +000016118static void print_binary_op(struct compile_state *state,
16119 const char *op, struct triple *ins, FILE *fp)
16120{
16121 unsigned mask;
16122 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016123 if (RHS(ins, 0)->id != ins->id) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016124 internal_error(state, ins, "invalid register assignment");
16125 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016126 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016127 fprintf(fp, "\t%s ", op);
16128 print_const_val(state, RHS(ins, 1), fp);
16129 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000016130 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016131 }
16132 else {
16133 unsigned lmask, rmask;
16134 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016135 lreg = check_reg(state, RHS(ins, 0), mask);
16136 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016137 lmask = arch_reg_regcm(state, lreg);
16138 rmask = arch_reg_regcm(state, rreg);
16139 mask = lmask & rmask;
16140 fprintf(fp, "\t%s %s, %s\n",
16141 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000016142 reg(state, RHS(ins, 1), mask),
16143 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016144 }
16145}
16146static void print_unary_op(struct compile_state *state,
16147 const char *op, struct triple *ins, FILE *fp)
16148{
16149 unsigned mask;
16150 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16151 fprintf(fp, "\t%s %s\n",
16152 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000016153 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016154}
16155
16156static void print_op_shift(struct compile_state *state,
16157 const char *op, struct triple *ins, FILE *fp)
16158{
16159 unsigned mask;
16160 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016161 if (RHS(ins, 0)->id != ins->id) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016162 internal_error(state, ins, "invalid register assignment");
16163 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016164 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016165 fprintf(fp, "\t%s ", op);
16166 print_const_val(state, RHS(ins, 1), fp);
16167 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000016168 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016169 }
16170 else {
16171 fprintf(fp, "\t%s %s, %s\n",
16172 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000016173 reg(state, RHS(ins, 1), REGCM_GPR8),
16174 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016175 }
16176}
16177
16178static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
16179{
16180 const char *op;
16181 int mask;
16182 int dreg;
16183 mask = 0;
16184 switch(ins->op) {
16185 case OP_INB: op = "inb", mask = REGCM_GPR8; break;
16186 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
16187 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
16188 default:
16189 internal_error(state, ins, "not an in operation");
16190 op = 0;
16191 break;
16192 }
16193 dreg = check_reg(state, ins, mask);
16194 if (!reg_is_reg(state, dreg, REG_EAX)) {
16195 internal_error(state, ins, "dst != %%eax");
16196 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016197 if (is_const(RHS(ins, 0))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016198 fprintf(fp, "\t%s ", op);
16199 print_const_val(state, RHS(ins, 0), fp);
16200 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000016201 reg(state, ins, mask));
16202 }
16203 else {
16204 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016205 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016206 if (!reg_is_reg(state, addr_reg, REG_DX)) {
16207 internal_error(state, ins, "src != %%dx");
16208 }
16209 fprintf(fp, "\t%s %s, %s\n",
16210 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000016211 reg(state, RHS(ins, 0), REGCM_GPR16),
Eric Biedermanb138ac82003-04-22 18:44:01 +000016212 reg(state, ins, mask));
16213 }
16214}
16215
16216static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
16217{
16218 const char *op;
16219 int mask;
16220 int lreg;
16221 mask = 0;
16222 switch(ins->op) {
16223 case OP_OUTB: op = "outb", mask = REGCM_GPR8; break;
16224 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
16225 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
16226 default:
16227 internal_error(state, ins, "not an out operation");
16228 op = 0;
16229 break;
16230 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016231 lreg = check_reg(state, RHS(ins, 0), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016232 if (!reg_is_reg(state, lreg, REG_EAX)) {
16233 internal_error(state, ins, "src != %%eax");
16234 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016235 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016236 fprintf(fp, "\t%s %s,",
16237 op, reg(state, RHS(ins, 0), mask));
16238 print_const_val(state, RHS(ins, 1), fp);
16239 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016240 }
16241 else {
16242 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016243 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016244 if (!reg_is_reg(state, addr_reg, REG_DX)) {
16245 internal_error(state, ins, "dst != %%dx");
16246 }
16247 fprintf(fp, "\t%s %s, %s\n",
16248 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000016249 reg(state, RHS(ins, 0), mask),
16250 reg(state, RHS(ins, 1), REGCM_GPR16));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016251 }
16252}
16253
16254static void print_op_move(struct compile_state *state,
16255 struct triple *ins, FILE *fp)
16256{
16257 /* op_move is complex because there are many types
16258 * of registers we can move between.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016259 * Because OP_COPY will be introduced in arbitrary locations
16260 * OP_COPY must not affect flags.
Eric Biedermanb138ac82003-04-22 18:44:01 +000016261 */
16262 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
16263 struct triple *dst, *src;
16264 if (ins->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016265 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016266 dst = ins;
16267 }
16268 else if (ins->op == OP_WRITE) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016269 dst = LHS(ins, 0);
16270 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016271 }
16272 else {
16273 internal_error(state, ins, "unknown move operation");
16274 src = dst = 0;
16275 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016276 if (!is_const(src)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016277 int src_reg, dst_reg;
16278 int src_regcm, dst_regcm;
16279 src_reg = ID_REG(src->id);
16280 dst_reg = ID_REG(dst->id);
16281 src_regcm = arch_reg_regcm(state, src_reg);
16282 dst_regcm = arch_reg_regcm(state, dst_reg);
16283 /* If the class is the same just move the register */
16284 if (src_regcm & dst_regcm &
16285 (REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32)) {
16286 if ((src_reg != dst_reg) || !omit_copy) {
16287 fprintf(fp, "\tmov %s, %s\n",
16288 reg(state, src, src_regcm),
16289 reg(state, dst, dst_regcm));
16290 }
16291 }
16292 /* Move 32bit to 16bit */
16293 else if ((src_regcm & REGCM_GPR32) &&
16294 (dst_regcm & REGCM_GPR16)) {
16295 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
16296 if ((src_reg != dst_reg) || !omit_copy) {
16297 fprintf(fp, "\tmovw %s, %s\n",
16298 arch_reg_str(src_reg),
16299 arch_reg_str(dst_reg));
16300 }
16301 }
16302 /* Move 32bit to 8bit */
16303 else if ((src_regcm & REGCM_GPR32_8) &&
16304 (dst_regcm & REGCM_GPR8))
16305 {
16306 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
16307 if ((src_reg != dst_reg) || !omit_copy) {
16308 fprintf(fp, "\tmovb %s, %s\n",
16309 arch_reg_str(src_reg),
16310 arch_reg_str(dst_reg));
16311 }
16312 }
16313 /* Move 16bit to 8bit */
16314 else if ((src_regcm & REGCM_GPR16_8) &&
16315 (dst_regcm & REGCM_GPR8))
16316 {
16317 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
16318 if ((src_reg != dst_reg) || !omit_copy) {
16319 fprintf(fp, "\tmovb %s, %s\n",
16320 arch_reg_str(src_reg),
16321 arch_reg_str(dst_reg));
16322 }
16323 }
16324 /* Move 8/16bit to 16/32bit */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016325 else if ((src_regcm & (REGCM_GPR8 | REGCM_GPR16)) &&
16326 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016327 const char *op;
16328 op = is_signed(src->type)? "movsx": "movzx";
16329 fprintf(fp, "\t%s %s, %s\n",
16330 op,
16331 reg(state, src, src_regcm),
16332 reg(state, dst, dst_regcm));
16333 }
16334 /* Move between sse registers */
16335 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
16336 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016337 fprintf(fp, "\tmovdqa %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000016338 reg(state, src, src_regcm),
16339 reg(state, dst, dst_regcm));
16340 }
16341 }
16342 /* Move between mmx registers or mmx & sse registers */
16343 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
16344 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
16345 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016346 fprintf(fp, "\tmovq %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000016347 reg(state, src, src_regcm),
16348 reg(state, dst, dst_regcm));
16349 }
16350 }
16351 /* Move between 32bit gprs & mmx/sse registers */
16352 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
16353 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
16354 fprintf(fp, "\tmovd %s, %s\n",
16355 reg(state, src, src_regcm),
16356 reg(state, dst, dst_regcm));
16357 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016358#if X86_4_8BIT_GPRS
16359 /* Move from 8bit gprs to mmx/sse registers */
16360 else if ((src_regcm & REGCM_GPR8) && (src_reg <= REG_DL) &&
16361 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
16362 const char *op;
16363 int mid_reg;
16364 op = is_signed(src->type)? "movsx":"movzx";
16365 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16366 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
16367 op,
16368 reg(state, src, src_regcm),
16369 arch_reg_str(mid_reg),
16370 arch_reg_str(mid_reg),
16371 reg(state, dst, dst_regcm));
16372 }
16373 /* Move from mmx/sse registers and 8bit gprs */
16374 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
16375 (dst_regcm & REGCM_GPR8) && (dst_reg <= REG_DL)) {
16376 int mid_reg;
16377 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16378 fprintf(fp, "\tmovd %s, %s\n",
16379 reg(state, src, src_regcm),
16380 arch_reg_str(mid_reg));
16381 }
16382 /* Move from 32bit gprs to 16bit gprs */
16383 else if ((src_regcm & REGCM_GPR32) &&
16384 (dst_regcm & REGCM_GPR16)) {
16385 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
16386 if ((src_reg != dst_reg) || !omit_copy) {
16387 fprintf(fp, "\tmov %s, %s\n",
16388 arch_reg_str(src_reg),
16389 arch_reg_str(dst_reg));
16390 }
16391 }
16392 /* Move from 32bit gprs to 8bit gprs */
16393 else if ((src_regcm & REGCM_GPR32) &&
16394 (dst_regcm & REGCM_GPR8)) {
16395 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16396 if ((src_reg != dst_reg) || !omit_copy) {
16397 fprintf(fp, "\tmov %s, %s\n",
16398 arch_reg_str(src_reg),
16399 arch_reg_str(dst_reg));
16400 }
16401 }
16402 /* Move from 16bit gprs to 8bit gprs */
16403 else if ((src_regcm & REGCM_GPR16) &&
16404 (dst_regcm & REGCM_GPR8)) {
16405 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
16406 if ((src_reg != dst_reg) || !omit_copy) {
16407 fprintf(fp, "\tmov %s, %s\n",
16408 arch_reg_str(src_reg),
16409 arch_reg_str(dst_reg));
16410 }
16411 }
16412#endif /* X86_4_8BIT_GPRS */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016413 else {
16414 internal_error(state, ins, "unknown copy type");
16415 }
16416 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016417 else {
16418 fprintf(fp, "\tmov ");
16419 print_const_val(state, src, fp);
16420 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000016421 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016422 }
16423}
16424
16425static void print_op_load(struct compile_state *state,
16426 struct triple *ins, FILE *fp)
16427{
16428 struct triple *dst, *src;
16429 dst = ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016430 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016431 if (is_const(src) || is_const(dst)) {
16432 internal_error(state, ins, "unknown load operation");
16433 }
16434 fprintf(fp, "\tmov (%s), %s\n",
16435 reg(state, src, REGCM_GPR32),
16436 reg(state, dst, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32));
16437}
16438
16439
16440static void print_op_store(struct compile_state *state,
16441 struct triple *ins, FILE *fp)
16442{
16443 struct triple *dst, *src;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016444 dst = LHS(ins, 0);
16445 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016446 if (is_const(src) && (src->op == OP_INTCONST)) {
16447 long_t value;
16448 value = (long_t)(src->u.cval);
16449 fprintf(fp, "\tmov%s $%ld, (%s)\n",
16450 type_suffix(state, src->type),
16451 value,
16452 reg(state, dst, REGCM_GPR32));
16453 }
16454 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
16455 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
16456 type_suffix(state, src->type),
16457 reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
16458 dst->u.cval);
16459 }
16460 else {
16461 if (is_const(src) || is_const(dst)) {
16462 internal_error(state, ins, "unknown store operation");
16463 }
16464 fprintf(fp, "\tmov%s %s, (%s)\n",
16465 type_suffix(state, src->type),
16466 reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
16467 reg(state, dst, REGCM_GPR32));
16468 }
16469
16470
16471}
16472
16473static void print_op_smul(struct compile_state *state,
16474 struct triple *ins, FILE *fp)
16475{
Eric Biederman0babc1c2003-05-09 02:39:00 +000016476 if (!is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016477 fprintf(fp, "\timul %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000016478 reg(state, RHS(ins, 1), REGCM_GPR32),
16479 reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016480 }
16481 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016482 fprintf(fp, "\timul ");
16483 print_const_val(state, RHS(ins, 1), fp);
16484 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016485 }
16486}
16487
16488static void print_op_cmp(struct compile_state *state,
16489 struct triple *ins, FILE *fp)
16490{
16491 unsigned mask;
16492 int dreg;
16493 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16494 dreg = check_reg(state, ins, REGCM_FLAGS);
16495 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
16496 internal_error(state, ins, "bad dest register for cmp");
16497 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016498 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016499 fprintf(fp, "\tcmp ");
16500 print_const_val(state, RHS(ins, 1), fp);
16501 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016502 }
16503 else {
16504 unsigned lmask, rmask;
16505 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016506 lreg = check_reg(state, RHS(ins, 0), mask);
16507 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016508 lmask = arch_reg_regcm(state, lreg);
16509 rmask = arch_reg_regcm(state, rreg);
16510 mask = lmask & rmask;
16511 fprintf(fp, "\tcmp %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000016512 reg(state, RHS(ins, 1), mask),
16513 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016514 }
16515}
16516
16517static void print_op_test(struct compile_state *state,
16518 struct triple *ins, FILE *fp)
16519{
16520 unsigned mask;
16521 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16522 fprintf(fp, "\ttest %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000016523 reg(state, RHS(ins, 0), mask),
16524 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016525}
16526
16527static void print_op_branch(struct compile_state *state,
16528 struct triple *branch, FILE *fp)
16529{
16530 const char *bop = "j";
16531 if (branch->op == OP_JMP) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016532 if (TRIPLE_RHS(branch->sizes) != 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016533 internal_error(state, branch, "jmp with condition?");
16534 }
16535 bop = "jmp";
16536 }
16537 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016538 struct triple *ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016539 if (TRIPLE_RHS(branch->sizes) != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016540 internal_error(state, branch, "jmpcc without condition?");
16541 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016542 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
16543 if ((RHS(branch, 0)->op != OP_CMP) &&
16544 (RHS(branch, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016545 internal_error(state, branch, "bad branch test");
16546 }
16547#warning "FIXME I have observed instructions between the test and branch instructions"
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016548 ptr = RHS(branch, 0);
16549 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
16550 if (ptr->op != OP_COPY) {
16551 internal_error(state, branch, "branch does not follow test");
16552 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016553 }
16554 switch(branch->op) {
16555 case OP_JMP_EQ: bop = "jz"; break;
16556 case OP_JMP_NOTEQ: bop = "jnz"; break;
16557 case OP_JMP_SLESS: bop = "jl"; break;
16558 case OP_JMP_ULESS: bop = "jb"; break;
16559 case OP_JMP_SMORE: bop = "jg"; break;
16560 case OP_JMP_UMORE: bop = "ja"; break;
16561 case OP_JMP_SLESSEQ: bop = "jle"; break;
16562 case OP_JMP_ULESSEQ: bop = "jbe"; break;
16563 case OP_JMP_SMOREEQ: bop = "jge"; break;
16564 case OP_JMP_UMOREEQ: bop = "jae"; break;
16565 default:
16566 internal_error(state, branch, "Invalid branch op");
16567 break;
16568 }
16569
16570 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016571 fprintf(fp, "\t%s L%s%lu\n",
16572 bop,
16573 state->label_prefix,
16574 TARG(branch, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016575}
16576
16577static void print_op_set(struct compile_state *state,
16578 struct triple *set, FILE *fp)
16579{
16580 const char *sop = "set";
Eric Biederman0babc1c2003-05-09 02:39:00 +000016581 if (TRIPLE_RHS(set->sizes) != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016582 internal_error(state, set, "setcc without condition?");
16583 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016584 check_reg(state, RHS(set, 0), REGCM_FLAGS);
16585 if ((RHS(set, 0)->op != OP_CMP) &&
16586 (RHS(set, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016587 internal_error(state, set, "bad set test");
16588 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016589 if (RHS(set, 0)->next != set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016590 internal_error(state, set, "set does not follow test");
16591 }
16592 switch(set->op) {
16593 case OP_SET_EQ: sop = "setz"; break;
16594 case OP_SET_NOTEQ: sop = "setnz"; break;
16595 case OP_SET_SLESS: sop = "setl"; break;
16596 case OP_SET_ULESS: sop = "setb"; break;
16597 case OP_SET_SMORE: sop = "setg"; break;
16598 case OP_SET_UMORE: sop = "seta"; break;
16599 case OP_SET_SLESSEQ: sop = "setle"; break;
16600 case OP_SET_ULESSEQ: sop = "setbe"; break;
16601 case OP_SET_SMOREEQ: sop = "setge"; break;
16602 case OP_SET_UMOREEQ: sop = "setae"; break;
16603 default:
16604 internal_error(state, set, "Invalid set op");
16605 break;
16606 }
16607 fprintf(fp, "\t%s %s\n",
16608 sop, reg(state, set, REGCM_GPR8));
16609}
16610
16611static void print_op_bit_scan(struct compile_state *state,
16612 struct triple *ins, FILE *fp)
16613{
16614 const char *op;
16615 switch(ins->op) {
16616 case OP_BSF: op = "bsf"; break;
16617 case OP_BSR: op = "bsr"; break;
16618 default:
16619 internal_error(state, ins, "unknown bit scan");
16620 op = 0;
16621 break;
16622 }
16623 fprintf(fp,
16624 "\t%s %s, %s\n"
16625 "\tjnz 1f\n"
16626 "\tmovl $-1, %s\n"
16627 "1:\n",
16628 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000016629 reg(state, RHS(ins, 0), REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000016630 reg(state, ins, REGCM_GPR32),
16631 reg(state, ins, REGCM_GPR32));
16632}
16633
16634static void print_const(struct compile_state *state,
16635 struct triple *ins, FILE *fp)
16636{
16637 switch(ins->op) {
16638 case OP_INTCONST:
16639 switch(ins->type->type & TYPE_MASK) {
16640 case TYPE_CHAR:
16641 case TYPE_UCHAR:
16642 fprintf(fp, ".byte 0x%02lx\n", ins->u.cval);
16643 break;
16644 case TYPE_SHORT:
16645 case TYPE_USHORT:
16646 fprintf(fp, ".short 0x%04lx\n", ins->u.cval);
16647 break;
16648 case TYPE_INT:
16649 case TYPE_UINT:
16650 case TYPE_LONG:
16651 case TYPE_ULONG:
16652 fprintf(fp, ".int %lu\n", ins->u.cval);
16653 break;
16654 default:
16655 internal_error(state, ins, "Unknown constant type");
16656 }
16657 break;
16658 case OP_BLOBCONST:
16659 {
16660 unsigned char *blob;
16661 size_t size, i;
16662 size = size_of(state, ins->type);
16663 blob = ins->u.blob;
16664 for(i = 0; i < size; i++) {
16665 fprintf(fp, ".byte 0x%02x\n",
16666 blob[i]);
16667 }
16668 break;
16669 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016670 default:
16671 internal_error(state, ins, "Unknown constant type");
16672 break;
16673 }
16674}
16675
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016676#define TEXT_SECTION ".rom.text"
16677#define DATA_SECTION ".rom.data"
16678
Eric Biedermanb138ac82003-04-22 18:44:01 +000016679static void print_sdecl(struct compile_state *state,
16680 struct triple *ins, FILE *fp)
16681{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016682 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016683 fprintf(fp, ".balign %d\n", align_of(state, ins->type));
Eric Biederman05f26fc2003-06-11 21:55:00 +000016684 fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016685 print_const(state, MISC(ins, 0), fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016686 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016687
16688}
16689
16690static void print_instruction(struct compile_state *state,
16691 struct triple *ins, FILE *fp)
16692{
16693 /* Assumption: after I have exted the register allocator
16694 * everything is in a valid register.
16695 */
16696 switch(ins->op) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016697 case OP_ASM:
16698 print_op_asm(state, ins, fp);
16699 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016700 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
16701 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
16702 case OP_AND: print_binary_op(state, "and", ins, fp); break;
16703 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
16704 case OP_OR: print_binary_op(state, "or", ins, fp); break;
16705 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
16706 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
16707 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
16708 case OP_POS: break;
16709 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
16710 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
16711 case OP_INTCONST:
16712 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016713 case OP_BLOBCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +000016714 /* Don't generate anything here for constants */
16715 case OP_PHI:
16716 /* Don't generate anything for variable declarations. */
16717 break;
16718 case OP_SDECL:
16719 print_sdecl(state, ins, fp);
16720 break;
16721 case OP_WRITE:
16722 case OP_COPY:
16723 print_op_move(state, ins, fp);
16724 break;
16725 case OP_LOAD:
16726 print_op_load(state, ins, fp);
16727 break;
16728 case OP_STORE:
16729 print_op_store(state, ins, fp);
16730 break;
16731 case OP_SMUL:
16732 print_op_smul(state, ins, fp);
16733 break;
16734 case OP_CMP: print_op_cmp(state, ins, fp); break;
16735 case OP_TEST: print_op_test(state, ins, fp); break;
16736 case OP_JMP:
16737 case OP_JMP_EQ: case OP_JMP_NOTEQ:
16738 case OP_JMP_SLESS: case OP_JMP_ULESS:
16739 case OP_JMP_SMORE: case OP_JMP_UMORE:
16740 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
16741 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
16742 print_op_branch(state, ins, fp);
16743 break;
16744 case OP_SET_EQ: case OP_SET_NOTEQ:
16745 case OP_SET_SLESS: case OP_SET_ULESS:
16746 case OP_SET_SMORE: case OP_SET_UMORE:
16747 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
16748 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
16749 print_op_set(state, ins, fp);
16750 break;
16751 case OP_INB: case OP_INW: case OP_INL:
16752 print_op_in(state, ins, fp);
16753 break;
16754 case OP_OUTB: case OP_OUTW: case OP_OUTL:
16755 print_op_out(state, ins, fp);
16756 break;
16757 case OP_BSF:
16758 case OP_BSR:
16759 print_op_bit_scan(state, ins, fp);
16760 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016761 case OP_RDMSR:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016762 after_lhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016763 fprintf(fp, "\trdmsr\n");
16764 break;
16765 case OP_WRMSR:
16766 fprintf(fp, "\twrmsr\n");
16767 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016768 case OP_HLT:
16769 fprintf(fp, "\thlt\n");
16770 break;
16771 case OP_LABEL:
16772 if (!ins->use) {
16773 return;
16774 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016775 fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016776 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016777 /* Ignore OP_PIECE */
16778 case OP_PIECE:
16779 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016780 /* Operations I am not yet certain how to handle */
16781 case OP_UMUL:
16782 case OP_SDIV: case OP_UDIV:
16783 case OP_SMOD: case OP_UMOD:
16784 /* Operations that should never get here */
16785 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
16786 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
16787 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
16788 default:
16789 internal_error(state, ins, "unknown op: %d %s",
16790 ins->op, tops(ins->op));
16791 break;
16792 }
16793}
16794
16795static void print_instructions(struct compile_state *state)
16796{
16797 struct triple *first, *ins;
16798 int print_location;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000016799 struct occurance *last_occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016800 FILE *fp;
16801 print_location = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000016802 last_occurance = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016803 fp = state->output;
16804 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biederman0babc1c2003-05-09 02:39:00 +000016805 first = RHS(state->main_function, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016806 ins = first;
16807 do {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000016808 if (print_location &&
16809 last_occurance != ins->occurance) {
16810 if (!ins->occurance->parent) {
16811 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
16812 ins->occurance->function,
16813 ins->occurance->filename,
16814 ins->occurance->line,
16815 ins->occurance->col);
16816 }
16817 else {
16818 struct occurance *ptr;
16819 fprintf(fp, "\t/*\n");
16820 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
16821 fprintf(fp, "\t * %s,%s:%d.%d\n",
16822 ptr->function,
16823 ptr->filename,
16824 ptr->line,
16825 ptr->col);
16826 }
16827 fprintf(fp, "\t */\n");
16828
16829 }
16830 if (last_occurance) {
16831 put_occurance(last_occurance);
16832 }
16833 get_occurance(ins->occurance);
16834 last_occurance = ins->occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016835 }
16836
16837 print_instruction(state, ins, fp);
16838 ins = ins->next;
16839 } while(ins != first);
16840
16841}
16842static void generate_code(struct compile_state *state)
16843{
16844 generate_local_labels(state);
16845 print_instructions(state);
16846
16847}
16848
16849static void print_tokens(struct compile_state *state)
16850{
16851 struct token *tk;
16852 tk = &state->token[0];
16853 do {
16854#if 1
16855 token(state, 0);
16856#else
16857 next_token(state, 0);
16858#endif
16859 loc(stdout, state, 0);
16860 printf("%s <- `%s'\n",
16861 tokens[tk->tok],
16862 tk->ident ? tk->ident->name :
16863 tk->str_len ? tk->val.str : "");
16864
16865 } while(tk->tok != TOK_EOF);
16866}
16867
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016868static void compile(const char *filename, const char *ofilename,
Eric Biederman05f26fc2003-06-11 21:55:00 +000016869 int cpu, int debug, int opt, const char *label_prefix)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016870{
16871 int i;
16872 struct compile_state state;
16873 memset(&state, 0, sizeof(state));
16874 state.file = 0;
16875 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
16876 memset(&state.token[i], 0, sizeof(state.token[i]));
16877 state.token[i].tok = -1;
16878 }
16879 /* Remember the debug settings */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016880 state.cpu = cpu;
16881 state.debug = debug;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016882 state.optimize = opt;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016883 /* Remember the output filename */
16884 state.ofilename = ofilename;
16885 state.output = fopen(state.ofilename, "w");
16886 if (!state.output) {
16887 error(&state, 0, "Cannot open output file %s\n",
16888 ofilename);
16889 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016890 /* Remember the label prefix */
16891 state.label_prefix = label_prefix;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016892 /* Prep the preprocessor */
16893 state.if_depth = 0;
16894 state.if_value = 0;
16895 /* register the C keywords */
16896 register_keywords(&state);
16897 /* register the keywords the macro preprocessor knows */
16898 register_macro_keywords(&state);
16899 /* Memorize where some special keywords are. */
16900 state.i_continue = lookup(&state, "continue", 8);
16901 state.i_break = lookup(&state, "break", 5);
16902 /* Enter the globl definition scope */
16903 start_scope(&state);
16904 register_builtins(&state);
16905 compile_file(&state, filename, 1);
16906#if 0
16907 print_tokens(&state);
16908#endif
16909 decls(&state);
16910 /* Exit the global definition scope */
16911 end_scope(&state);
16912
16913 /* Now that basic compilation has happened
16914 * optimize the intermediate code
16915 */
16916 optimize(&state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016917
Eric Biedermanb138ac82003-04-22 18:44:01 +000016918 generate_code(&state);
16919 if (state.debug) {
16920 fprintf(stderr, "done\n");
16921 }
16922}
16923
16924static void version(void)
16925{
16926 printf("romcc " VERSION " released " RELEASE_DATE "\n");
16927}
16928
16929static void usage(void)
16930{
16931 version();
16932 printf(
16933 "Usage: romcc <source>.c\n"
16934 "Compile a C source file without using ram\n"
16935 );
16936}
16937
16938static void arg_error(char *fmt, ...)
16939{
16940 va_list args;
16941 va_start(args, fmt);
16942 vfprintf(stderr, fmt, args);
16943 va_end(args);
16944 usage();
16945 exit(1);
16946}
16947
16948int main(int argc, char **argv)
16949{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016950 const char *filename;
16951 const char *ofilename;
Eric Biederman05f26fc2003-06-11 21:55:00 +000016952 const char *label_prefix;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016953 int cpu;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016954 int last_argc;
16955 int debug;
16956 int optimize;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016957 cpu = CPU_DEFAULT;
Eric Biederman05f26fc2003-06-11 21:55:00 +000016958 label_prefix = "";
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016959 ofilename = "auto.inc";
Eric Biedermanb138ac82003-04-22 18:44:01 +000016960 optimize = 0;
16961 debug = 0;
16962 last_argc = -1;
16963 while((argc > 1) && (argc != last_argc)) {
16964 last_argc = argc;
16965 if (strncmp(argv[1], "--debug=", 8) == 0) {
16966 debug = atoi(argv[1] + 8);
16967 argv++;
16968 argc--;
16969 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016970 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
16971 label_prefix= argv[1] + 15;
16972 argv++;
16973 argc--;
16974 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016975 else if ((strcmp(argv[1],"-O") == 0) ||
16976 (strcmp(argv[1], "-O1") == 0)) {
16977 optimize = 1;
16978 argv++;
16979 argc--;
16980 }
16981 else if (strcmp(argv[1],"-O2") == 0) {
16982 optimize = 2;
16983 argv++;
16984 argc--;
16985 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016986 else if ((strcmp(argv[1], "-o") == 0) && (argc > 2)) {
16987 ofilename = argv[2];
16988 argv += 2;
16989 argc -= 2;
16990 }
16991 else if (strncmp(argv[1], "-mcpu=", 6) == 0) {
16992 cpu = arch_encode_cpu(argv[1] + 6);
16993 if (cpu == BAD_CPU) {
16994 arg_error("Invalid cpu specified: %s\n",
16995 argv[1] + 6);
16996 }
16997 argv++;
16998 argc--;
16999 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017000 }
17001 if (argc != 2) {
17002 arg_error("Wrong argument count %d\n", argc);
17003 }
17004 filename = argv[1];
Eric Biederman05f26fc2003-06-11 21:55:00 +000017005 compile(filename, ofilename, cpu, debug, optimize, label_prefix);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017006
17007 return 0;
17008}