- Factoring of auto.c
[coreboot.git] / util / romcc / romcc.c
1 #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>
12 #include <ctype.h>
13 #include <limits.h>
14
15 #define DEBUG_ERROR_MESSAGES 0
16 #define DEBUG_COLOR_GRAPH 0
17 #define DEBUG_SCC 0
18 #define DEBUG_CONSISTENCY 1
19
20 #warning "FIXME boundary cases with small types in larger registers"
21
22 /*  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  */
89 static 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
102 static 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
113 static 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
121 static void xfree(const void *ptr)
122 {
123         free((void *)ptr);
124 }
125
126 static 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
137 static 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
145 static 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
158 static 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 */
206 typedef unsigned long ulong_t;
207 typedef long long_t;
208
209 struct 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;
218 };
219 struct hash_entry;
220 struct token {
221         int tok;
222         struct hash_entry *ident;
223         int str_len;
224         union {
225                 ulong_t integer;
226                 const char *str;
227         } val;
228 };
229
230 /* I have two classes of types:
231  * Operational types.
232  * Logical types.  (The type the C standard says the operation is of)
233  *
234  * The operational types are:
235  * chars
236  * shorts
237  * ints
238  * longs
239  *
240  * floats
241  * doubles
242  * long doubles
243  *
244  * pointer
245  */
246
247
248 /* Machine model.
249  * No memory is useable by the compiler.
250  * There is no floating point support.
251  * All operations take place in general purpose registers.
252  * There is one type of general purpose register.
253  * Unsigned longs are stored in that general purpose register.
254  */
255
256 /* Operations on general purpose registers.
257  */
258
259 #define OP_SMUL       0
260 #define OP_UMUL       1
261 #define OP_SDIV       2
262 #define OP_UDIV       3
263 #define OP_SMOD       4
264 #define OP_UMOD       5
265 #define OP_ADD        6
266 #define OP_SUB        7
267 #define OP_SL         8
268 #define OP_USR        9
269 #define OP_SSR       10 
270 #define OP_AND       11 
271 #define OP_XOR       12
272 #define OP_OR        13
273 #define OP_POS       14 /* Dummy positive operator don't use it */
274 #define OP_NEG       15
275 #define OP_INVERT    16
276                      
277 #define OP_EQ        20
278 #define OP_NOTEQ     21
279 #define OP_SLESS     22
280 #define OP_ULESS     23
281 #define OP_SMORE     24
282 #define OP_UMORE     25
283 #define OP_SLESSEQ   26
284 #define OP_ULESSEQ   27
285 #define OP_SMOREEQ   28
286 #define OP_UMOREEQ   29
287                      
288 #define OP_LFALSE    30  /* Test if the expression is logically false */
289 #define OP_LTRUE     31  /* Test if the expression is logcially true */
290
291 #define OP_LOAD      32
292 #define OP_STORE     33
293
294 #define OP_NOOP      34
295
296 #define OP_MIN_CONST 50
297 #define OP_MAX_CONST 59
298 #define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
299 #define OP_INTCONST  50
300 #define OP_BLOBCONST 51
301 /* For OP_BLOBCONST ->type holds the layout and size
302  * information.  u.blob holds a pointer to the raw binary
303  * data for the constant initializer.
304  */
305 #define OP_ADDRCONST 52
306 /* For OP_ADDRCONST ->type holds the type.
307  * MISC(0) holds the reference to the static variable.
308  * ->u.cval holds an offset from that value.
309  */
310
311 #define OP_WRITE     60 
312 /* OP_WRITE moves one pseudo register to another.
313  * LHS(0) holds the destination pseudo register, which must be an OP_DECL.
314  * RHS(0) holds the psuedo to move.
315  */
316
317 #define OP_READ      61
318 /* OP_READ reads the value of a variable and makes
319  * it available for the pseudo operation.
320  * Useful for things like def-use chains.
321  * RHS(0) holds points to the triple to read from.
322  */
323 #define OP_COPY      62
324 /* OP_COPY makes a copy of the psedo register or constant in RHS(0).
325  */
326 #define OP_PIECE     63
327 /* OP_PIECE returns one piece of a instruction that returns a structure.
328  * MISC(0) is the instruction
329  * u.cval is the LHS piece of the instruction to return.
330  */
331 #define OP_ASM       64
332 /* OP_ASM holds a sequence of assembly instructions, the result
333  * of a C asm directive.
334  * RHS(x) holds input value x to the assembly sequence.
335  * LHS(x) holds the output value x from the assembly sequence.
336  * u.blob holds the string of assembly instructions.
337  */
338
339 #define OP_DEREF     65
340 /* OP_DEREF generates an lvalue from a pointer.
341  * RHS(0) holds the pointer value.
342  * OP_DEREF serves as a place holder to indicate all necessary
343  * checks have been done to indicate a value is an lvalue.
344  */
345 #define OP_DOT       66
346 /* OP_DOT references a submember of a structure lvalue.
347  * RHS(0) holds the lvalue.
348  * ->u.field holds the name of the field we want.
349  *
350  * Not seen outside of expressions.
351  */
352 #define OP_VAL       67
353 /* OP_VAL returns the value of a subexpression of the current expression.
354  * Useful for operators that have side effects.
355  * RHS(0) holds the expression.
356  * MISC(0) holds the subexpression of RHS(0) that is the
357  * value of the expression.
358  *
359  * Not seen outside of expressions.
360  */
361 #define OP_LAND      68
362 /* OP_LAND performs a C logical and between RHS(0) and RHS(1).
363  * Not seen outside of expressions.
364  */
365 #define OP_LOR       69
366 /* OP_LOR performs a C logical or between RHS(0) and RHS(1).
367  * Not seen outside of expressions.
368  */
369 #define OP_COND      70
370 /* OP_CODE performas a C ? : operation. 
371  * RHS(0) holds the test.
372  * RHS(1) holds the expression to evaluate if the test returns true.
373  * RHS(2) holds the expression to evaluate if the test returns false.
374  * Not seen outside of expressions.
375  */
376 #define OP_COMMA     71
377 /* OP_COMMA performacs a C comma operation.
378  * That is RHS(0) is evaluated, then RHS(1)
379  * and the value of RHS(1) is returned.
380  * Not seen outside of expressions.
381  */
382
383 #define OP_CALL      72
384 /* OP_CALL performs a procedure call. 
385  * MISC(0) holds a pointer to the OP_LIST of a function
386  * RHS(x) holds argument x of a function
387  * 
388  * Currently not seen outside of expressions.
389  */
390 #define OP_VAL_VEC   74
391 /* OP_VAL_VEC is an array of triples that are either variable
392  * or values for a structure or an array.
393  * RHS(x) holds element x of the vector.
394  * triple->type->elements holds the size of the vector.
395  */
396
397 /* statements */
398 #define OP_LIST      80
399 /* OP_LIST Holds a list of statements, and a result value.
400  * RHS(0) holds the list of statements.
401  * MISC(0) holds the value of the statements.
402  */
403
404 #define OP_BRANCH    81 /* branch */
405 /* For branch instructions
406  * TARG(0) holds the branch target.
407  * RHS(0) if present holds the branch condition.
408  * ->next holds where to branch to if the branch is not taken.
409  * The branch target can only be a decl...
410  */
411
412 #define OP_LABEL     83
413 /* OP_LABEL is a triple that establishes an target for branches.
414  * ->use is the list of all branches that use this label.
415  */
416
417 #define OP_ADECL     84 
418 /* OP_DECL is a triple that establishes an lvalue for assignments.
419  * ->use is a list of statements that use the variable.
420  */
421
422 #define OP_SDECL     85
423 /* OP_SDECL is a triple that establishes a variable of static
424  * storage duration.
425  * ->use is a list of statements that use the variable.
426  * MISC(0) holds the initializer expression.
427  */
428
429
430 #define OP_PHI       86
431 /* OP_PHI is a triple used in SSA form code.  
432  * It is used when multiple code paths merge and a variable needs
433  * a single assignment from any of those code paths.
434  * The operation is a cross between OP_DECL and OP_WRITE, which
435  * is what OP_PHI is geneared from.
436  * 
437  * RHS(x) points to the value from code path x
438  * The number of RHS entries is the number of control paths into the block
439  * in which OP_PHI resides.  The elements of the array point to point
440  * to the variables OP_PHI is derived from.
441  *
442  * MISC(0) holds a pointer to the orginal OP_DECL node.
443  */
444
445 /* Architecture specific instructions */
446 #define OP_CMP         100
447 #define OP_TEST        101
448 #define OP_SET_EQ      102
449 #define OP_SET_NOTEQ   103
450 #define OP_SET_SLESS   104
451 #define OP_SET_ULESS   105
452 #define OP_SET_SMORE   106
453 #define OP_SET_UMORE   107
454 #define OP_SET_SLESSEQ 108
455 #define OP_SET_ULESSEQ 109
456 #define OP_SET_SMOREEQ 110
457 #define OP_SET_UMOREEQ 111
458
459 #define OP_JMP         112
460 #define OP_JMP_EQ      113
461 #define OP_JMP_NOTEQ   114
462 #define OP_JMP_SLESS   115
463 #define OP_JMP_ULESS   116
464 #define OP_JMP_SMORE   117
465 #define OP_JMP_UMORE   118
466 #define OP_JMP_SLESSEQ 119
467 #define OP_JMP_ULESSEQ 120
468 #define OP_JMP_SMOREEQ 121
469 #define OP_JMP_UMOREEQ 122
470
471 /* Builtin operators that it is just simpler to use the compiler for */
472 #define OP_INB         130
473 #define OP_INW         131
474 #define OP_INL         132
475 #define OP_OUTB        133
476 #define OP_OUTW        134
477 #define OP_OUTL        135
478 #define OP_BSF         136
479 #define OP_BSR         137
480 #define OP_RDMSR       138
481 #define OP_WRMSR       139
482 #define OP_HLT         140
483
484 struct op_info {
485         const char *name;
486         unsigned flags;
487 #define PURE   1
488 #define IMPURE 2
489 #define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
490 #define DEF    4
491 #define BLOCK  8 /* Triple stores the current block */
492         unsigned char lhs, rhs, misc, targ;
493 };
494
495 #define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \
496         .name = (NAME), \
497         .flags = (FLAGS), \
498         .lhs = (LHS), \
499         .rhs = (RHS), \
500         .misc = (MISC), \
501         .targ = (TARG), \
502          }
503 static const struct op_info table_ops[] = {
504 [OP_SMUL       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smul"),
505 [OP_UMUL       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umul"),
506 [OP_SDIV       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sdiv"),
507 [OP_UDIV       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "udiv"),
508 [OP_SMOD       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smod"),
509 [OP_UMOD       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umod"),
510 [OP_ADD        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "add"),
511 [OP_SUB        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sub"),
512 [OP_SL         ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sl"),
513 [OP_USR        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "usr"),
514 [OP_SSR        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "ssr"),
515 [OP_AND        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "and"),
516 [OP_XOR        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "xor"),
517 [OP_OR         ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "or"),
518 [OP_POS        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "pos"),
519 [OP_NEG        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "neg"),
520 [OP_INVERT     ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "invert"),
521
522 [OP_EQ         ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "eq"),
523 [OP_NOTEQ      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "noteq"),
524 [OP_SLESS      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sless"),
525 [OP_ULESS      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "uless"),
526 [OP_SMORE      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smore"),
527 [OP_UMORE      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umore"),
528 [OP_SLESSEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "slesseq"),
529 [OP_ULESSEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "ulesseq"),
530 [OP_SMOREEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smoreeq"),
531 [OP_UMOREEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umoreeq"),
532 [OP_LFALSE     ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "lfalse"),
533 [OP_LTRUE      ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "ltrue"),
534
535 [OP_LOAD       ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "load"),
536 [OP_STORE      ] = OP( 1,  1, 0, 0, IMPURE | BLOCK , "store"),
537
538 [OP_NOOP       ] = OP( 0,  0, 0, 0, PURE | BLOCK, "noop"),
539
540 [OP_INTCONST   ] = OP( 0,  0, 0, 0, PURE | DEF, "intconst"),
541 [OP_BLOBCONST  ] = OP( 0,  0, 0, 0, PURE, "blobconst"),
542 [OP_ADDRCONST  ] = OP( 0,  0, 1, 0, PURE | DEF, "addrconst"),
543
544 [OP_WRITE      ] = OP( 1,  1, 0, 0, PURE | BLOCK, "write"),
545 [OP_READ       ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "read"),
546 [OP_COPY       ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "copy"),
547 [OP_PIECE      ] = OP( 0,  0, 1, 0, PURE | DEF, "piece"),
548 [OP_ASM        ] = OP(-1, -1, 0, 0, IMPURE, "asm"),
549 [OP_DEREF      ] = OP( 0,  1, 0, 0, 0 | DEF | BLOCK, "deref"), 
550 [OP_DOT        ] = OP( 0,  1, 0, 0, 0 | DEF | BLOCK, "dot"),
551
552 [OP_VAL        ] = OP( 0,  1, 1, 0, 0 | DEF | BLOCK, "val"),
553 [OP_LAND       ] = OP( 0,  2, 0, 0, 0 | DEF | BLOCK, "land"),
554 [OP_LOR        ] = OP( 0,  2, 0, 0, 0 | DEF | BLOCK, "lor"),
555 [OP_COND       ] = OP( 0,  3, 0, 0, 0 | DEF | BLOCK, "cond"),
556 [OP_COMMA      ] = OP( 0,  2, 0, 0, 0 | DEF | BLOCK, "comma"),
557 /* Call is special most it can stand in for anything so it depends on context */
558 [OP_CALL       ] = OP(-1, -1, 1, 0, 0 | BLOCK, "call"),
559 /* The sizes of OP_CALL and OP_VAL_VEC depend upon context */
560 [OP_VAL_VEC    ] = OP( 0, -1, 0, 0, 0 | BLOCK, "valvec"),
561
562 [OP_LIST       ] = OP( 0,  1, 1, 0, 0 | DEF, "list"),
563 /* The number of targets for OP_BRANCH depends on context */
564 [OP_BRANCH     ] = OP( 0, -1, 0, 1, PURE | BLOCK, "branch"),
565 [OP_LABEL      ] = OP( 0,  0, 0, 0, PURE | BLOCK, "label"),
566 [OP_ADECL      ] = OP( 0,  0, 0, 0, PURE | BLOCK, "adecl"),
567 [OP_SDECL      ] = OP( 0,  0, 1, 0, PURE | BLOCK, "sdecl"),
568 /* The number of RHS elements of OP_PHI depend upon context */
569 [OP_PHI        ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
570
571 [OP_CMP        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK, "cmp"),
572 [OP_TEST       ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "test"),
573 [OP_SET_EQ     ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_eq"),
574 [OP_SET_NOTEQ  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_noteq"),
575 [OP_SET_SLESS  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_sless"),
576 [OP_SET_ULESS  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_uless"),
577 [OP_SET_SMORE  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_smore"),
578 [OP_SET_UMORE  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_umore"),
579 [OP_SET_SLESSEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_slesseq"),
580 [OP_SET_ULESSEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_ulesseq"),
581 [OP_SET_SMOREEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_smoreq"),
582 [OP_SET_UMOREEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_umoreq"),
583 [OP_JMP        ] = OP( 0,  0, 0, 1, PURE | BLOCK, "jmp"),
584 [OP_JMP_EQ     ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_eq"),
585 [OP_JMP_NOTEQ  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_noteq"),
586 [OP_JMP_SLESS  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_sless"),
587 [OP_JMP_ULESS  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_uless"),
588 [OP_JMP_SMORE  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_smore"),
589 [OP_JMP_UMORE  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_umore"),
590 [OP_JMP_SLESSEQ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_slesseq"),
591 [OP_JMP_ULESSEQ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_ulesseq"),
592 [OP_JMP_SMOREEQ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_smoreq"),
593 [OP_JMP_UMOREEQ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_umoreq"),
594
595 [OP_INB        ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "__inb"),
596 [OP_INW        ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "__inw"),
597 [OP_INL        ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "__inl"),
598 [OP_OUTB       ] = OP( 0,  2, 0, 0, IMPURE| BLOCK, "__outb"),
599 [OP_OUTW       ] = OP( 0,  2, 0, 0, IMPURE| BLOCK, "__outw"),
600 [OP_OUTL       ] = OP( 0,  2, 0, 0, IMPURE| BLOCK, "__outl"),
601 [OP_BSF        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "__bsf"),
602 [OP_BSR        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "__bsr"),
603 [OP_RDMSR      ] = OP( 2,  1, 0, 0, IMPURE | BLOCK, "__rdmsr"),
604 [OP_WRMSR      ] = OP( 0,  3, 0, 0, IMPURE | BLOCK, "__wrmsr"),
605 [OP_HLT        ] = OP( 0,  0, 0, 0, IMPURE | BLOCK, "__hlt"),
606 };
607 #undef OP
608 #define OP_MAX      (sizeof(table_ops)/sizeof(table_ops[0]))
609
610 static const char *tops(int index) 
611 {
612         static const char unknown[] = "unknown op";
613         if (index < 0) {
614                 return unknown;
615         }
616         if (index > OP_MAX) {
617                 return unknown;
618         }
619         return table_ops[index].name;
620 }
621
622 struct asm_info;
623 struct triple;
624 struct block;
625 struct triple_set {
626         struct triple_set *next;
627         struct triple *member;
628 };
629
630 #define MAX_LHS  15
631 #define MAX_RHS  15
632 #define MAX_MISC 15
633 #define MAX_TARG 15
634
635 struct triple {
636         struct triple *next, *prev;
637         struct triple_set *use;
638         struct type *type;
639         unsigned char op;
640         unsigned char template_id;
641         unsigned short sizes;
642 #define TRIPLE_LHS(SIZES)  (((SIZES) >>  0) & 0x0f)
643 #define TRIPLE_RHS(SIZES)  (((SIZES) >>  4) & 0x0f)
644 #define TRIPLE_MISC(SIZES) (((SIZES) >>  8) & 0x0f)
645 #define TRIPLE_TARG(SIZES) (((SIZES) >> 12) & 0x0f)
646 #define TRIPLE_SIZE(SIZES) \
647         ((((SIZES) >> 0) & 0x0f) + \
648         (((SIZES) >>  4) & 0x0f) + \
649         (((SIZES) >>  8) & 0x0f) + \
650         (((SIZES) >> 12) & 0x0f))
651 #define TRIPLE_SIZES(LHS, RHS, MISC, TARG) \
652         ((((LHS) & 0x0f) <<  0) | \
653         (((RHS) & 0x0f)  <<  4) | \
654         (((MISC) & 0x0f) <<  8) | \
655         (((TARG) & 0x0f) << 12))
656 #define TRIPLE_LHS_OFF(SIZES)  (0)
657 #define TRIPLE_RHS_OFF(SIZES)  (TRIPLE_LHS_OFF(SIZES) + TRIPLE_LHS(SIZES))
658 #define TRIPLE_MISC_OFF(SIZES) (TRIPLE_RHS_OFF(SIZES) + TRIPLE_RHS(SIZES))
659 #define TRIPLE_TARG_OFF(SIZES) (TRIPLE_MISC_OFF(SIZES) + TRIPLE_MISC(SIZES))
660 #define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF((PTR)->sizes) + (INDEX)])
661 #define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF((PTR)->sizes) + (INDEX)])
662 #define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF((PTR)->sizes) + (INDEX)])
663 #define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF((PTR)->sizes) + (INDEX)])
664         unsigned id; /* A scratch value and finally the register */
665 #define TRIPLE_FLAG_FLATTENED   (1 << 31)
666 #define TRIPLE_FLAG_PRE_SPLIT   (1 << 30)
667 #define TRIPLE_FLAG_POST_SPLIT  (1 << 29)
668         const char *filename;
669         int line;
670         int col;
671         union {
672                 ulong_t cval;
673                 struct block  *block;
674                 void *blob;
675                 struct hash_entry *field;
676                 struct asm_info *ainfo;
677         } u;
678         struct triple *param[2];
679 };
680
681 struct reg_info {
682         unsigned reg;
683         unsigned regcm;
684 };
685 struct ins_template {
686         struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
687 };
688
689 struct asm_info {
690         struct ins_template tmpl;
691         char *str;
692 };
693
694 struct block_set {
695         struct block_set *next;
696         struct block *member;
697 };
698 struct block {
699         struct block *work_next;
700         struct block *left, *right;
701         struct triple *first, *last;
702         int users;
703         struct block_set *use;
704         struct block_set *idominates;
705         struct block_set *domfrontier;
706         struct block *idom;
707         struct block_set *ipdominates;
708         struct block_set *ipdomfrontier;
709         struct block *ipdom;
710         int vertex;
711         
712 };
713
714 struct symbol {
715         struct symbol *next;
716         struct hash_entry *ident;
717         struct triple *def;
718         struct type *type;
719         int scope_depth;
720 };
721
722 struct macro {
723         struct hash_entry *ident;
724         char *buf;
725         int buf_len;
726 };
727
728 struct hash_entry {
729         struct hash_entry *next;
730         const char *name;
731         int name_len;
732         int tok;
733         struct macro *sym_define;
734         struct symbol *sym_label;
735         struct symbol *sym_struct;
736         struct symbol *sym_ident;
737 };
738
739 #define HASH_TABLE_SIZE 2048
740
741 struct compile_state {
742         const char *label_prefix;
743         const char *ofilename;
744         FILE *output;
745         struct triple *vars;
746         struct file_state *file;
747         struct token token[4];
748         struct hash_entry *hash_table[HASH_TABLE_SIZE];
749         struct hash_entry *i_continue;
750         struct hash_entry *i_break;
751         int scope_depth;
752         int if_depth, if_value;
753         int macro_line;
754         struct file_state *macro_file;
755         struct triple *main_function;
756         struct block *first_block, *last_block;
757         int last_vertex;
758         int cpu;
759         int debug;
760         int optimize;
761 };
762
763 /* visibility global/local */
764 /* static/auto duration */
765 /* typedef, register, inline */
766 #define STOR_SHIFT         0
767 #define STOR_MASK     0x000f
768 /* Visibility */
769 #define STOR_GLOBAL   0x0001
770 /* Duration */
771 #define STOR_PERM     0x0002
772 /* Storage specifiers */
773 #define STOR_AUTO     0x0000
774 #define STOR_STATIC   0x0002
775 #define STOR_EXTERN   0x0003
776 #define STOR_REGISTER 0x0004
777 #define STOR_TYPEDEF  0x0008
778 #define STOR_INLINE   0x000c
779
780 #define QUAL_SHIFT         4
781 #define QUAL_MASK     0x0070
782 #define QUAL_NONE     0x0000
783 #define QUAL_CONST    0x0010
784 #define QUAL_VOLATILE 0x0020
785 #define QUAL_RESTRICT 0x0040
786
787 #define TYPE_SHIFT         8
788 #define TYPE_MASK     0x1f00
789 #define TYPE_INTEGER(TYPE)    (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG))
790 #define TYPE_ARITHMETIC(TYPE) (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE))
791 #define TYPE_UNSIGNED(TYPE)   ((TYPE) & 0x0100)
792 #define TYPE_SIGNED(TYPE)     (!TYPE_UNSIGNED(TYPE))
793 #define TYPE_MKUNSIGNED(TYPE) ((TYPE) | 0x0100)
794 #define TYPE_RANK(TYPE)       ((TYPE) & ~0x0100)
795 #define TYPE_PTR(TYPE)        (((TYPE) & TYPE_MASK) == TYPE_POINTER)
796 #define TYPE_DEFAULT  0x0000
797 #define TYPE_VOID     0x0100
798 #define TYPE_CHAR     0x0200
799 #define TYPE_UCHAR    0x0300
800 #define TYPE_SHORT    0x0400
801 #define TYPE_USHORT   0x0500
802 #define TYPE_INT      0x0600
803 #define TYPE_UINT     0x0700
804 #define TYPE_LONG     0x0800
805 #define TYPE_ULONG    0x0900
806 #define TYPE_LLONG    0x0a00 /* long long */
807 #define TYPE_ULLONG   0x0b00
808 #define TYPE_FLOAT    0x0c00
809 #define TYPE_DOUBLE   0x0d00
810 #define TYPE_LDOUBLE  0x0e00 /* long double */
811 #define TYPE_STRUCT   0x1000
812 #define TYPE_ENUM     0x1100
813 #define TYPE_POINTER  0x1200 
814 /* For TYPE_POINTER:
815  * type->left holds the type pointed to.
816  */
817 #define TYPE_FUNCTION 0x1300 
818 /* For TYPE_FUNCTION:
819  * type->left holds the return type.
820  * type->right holds the...
821  */
822 #define TYPE_PRODUCT  0x1400
823 /* TYPE_PRODUCT is a basic building block when defining structures
824  * type->left holds the type that appears first in memory.
825  * type->right holds the type that appears next in memory.
826  */
827 #define TYPE_OVERLAP  0x1500
828 /* TYPE_OVERLAP is a basic building block when defining unions
829  * type->left and type->right holds to types that overlap
830  * each other in memory.
831  */
832 #define TYPE_ARRAY    0x1600
833 /* TYPE_ARRAY is a basic building block when definitng arrays.
834  * type->left holds the type we are an array of.
835  * type-> holds the number of elements.
836  */
837
838 #define ELEMENT_COUNT_UNSPECIFIED (~0UL)
839
840 struct type {
841         unsigned int type;
842         struct type *left, *right;
843         ulong_t elements;
844         struct hash_entry *field_ident;
845         struct hash_entry *type_ident;
846 };
847
848 #define MAX_REGISTERS      75
849 #define MAX_REG_EQUIVS     16
850 #define REGISTER_BITS      28
851 #define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
852 #define TEMPLATE_BITS      6
853 #define MAX_TEMPLATES      (1<<TEMPLATE_BITS)
854 #define MAX_REGC           12
855 #define REG_UNSET          0
856 #define REG_UNNEEDED       1
857 #define REG_VIRT0          (MAX_REGISTERS + 0)
858 #define REG_VIRT1          (MAX_REGISTERS + 1)
859 #define REG_VIRT2          (MAX_REGISTERS + 2)
860 #define REG_VIRT3          (MAX_REGISTERS + 3)
861 #define REG_VIRT4          (MAX_REGISTERS + 4)
862 #define REG_VIRT5          (MAX_REGISTERS + 5)
863
864 /* Provision for 8 register classes */
865 #define REG_MASK (MAX_VIRT_REGISTERS -1)
866 #define ID_REG(ID)              ((ID) & REG_MASK)
867 #define SET_REG(ID, REG)        ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
868
869 static unsigned arch_reg_regcm(struct compile_state *state, int reg);
870 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
871 static void arch_reg_equivs(
872         struct compile_state *state, unsigned *equiv, int reg);
873 static int arch_select_free_register(
874         struct compile_state *state, char *used, int classes);
875 static unsigned arch_regc_size(struct compile_state *state, int class);
876 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
877 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
878 static const char *arch_reg_str(int reg);
879 static struct reg_info arch_reg_constraint(
880         struct compile_state *state, struct type *type, const char *constraint);
881 static struct reg_info arch_reg_clobber(
882         struct compile_state *state, const char *clobber);
883 static struct reg_info arch_reg_lhs(struct compile_state *state, 
884         struct triple *ins, int index);
885 static struct reg_info arch_reg_rhs(struct compile_state *state, 
886         struct triple *ins, int index);
887 static struct triple *transform_to_arch_instruction(
888         struct compile_state *state, struct triple *ins);
889
890
891
892 #define DEBUG_ABORT_ON_ERROR    0x0001
893 #define DEBUG_INTERMEDIATE_CODE 0x0002
894 #define DEBUG_CONTROL_FLOW      0x0004
895 #define DEBUG_BASIC_BLOCKS      0x0008
896 #define DEBUG_FDOMINATORS       0x0010
897 #define DEBUG_RDOMINATORS       0x0020
898 #define DEBUG_TRIPLES           0x0040
899 #define DEBUG_INTERFERENCE      0x0080
900 #define DEBUG_ARCH_CODE         0x0100
901 #define DEBUG_CODE_ELIMINATION  0x0200
902 #define DEBUG_INSERTED_COPIES   0x0400
903
904 #define GLOBAL_SCOPE_DEPTH 1
905
906 static void compile_file(struct compile_state *old_state, const char *filename, int local);
907
908 static void do_cleanup(struct compile_state *state)
909 {
910         if (state->output) {
911                 fclose(state->output);
912                 unlink(state->ofilename);
913         }
914 }
915
916 static int get_col(struct file_state *file)
917 {
918         int col;
919         char *ptr, *end;
920         ptr = file->line_start;
921         end = file->pos;
922         for(col = 0; ptr < end; ptr++) {
923                 if (*ptr != '\t') {
924                         col++;
925                 } 
926                 else {
927                         col = (col & ~7) + 8;
928                 }
929         }
930         return col;
931 }
932
933 static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
934 {
935         int col;
936         if (triple) {
937                 fprintf(fp, "%s:%d.%d: ", 
938                         triple->filename, triple->line, triple->col);
939                 return;
940         }
941         if (!state->file) {
942                 return;
943         }
944         col = get_col(state->file);
945         fprintf(fp, "%s:%d.%d: ", 
946                 state->file->basename, state->file->line, col);
947 }
948
949 static void __internal_error(struct compile_state *state, struct triple *ptr, 
950         char *fmt, ...)
951 {
952         va_list args;
953         va_start(args, fmt);
954         loc(stderr, state, ptr);
955         if (ptr) {
956                 fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
957         }
958         fprintf(stderr, "Internal compiler error: ");
959         vfprintf(stderr, fmt, args);
960         fprintf(stderr, "\n");
961         va_end(args);
962         do_cleanup(state);
963         abort();
964 }
965
966
967 static void __internal_warning(struct compile_state *state, struct triple *ptr, 
968         char *fmt, ...)
969 {
970         va_list args;
971         va_start(args, fmt);
972         loc(stderr, state, ptr);
973         fprintf(stderr, "Internal compiler warning: ");
974         vfprintf(stderr, fmt, args);
975         fprintf(stderr, "\n");
976         va_end(args);
977 }
978
979
980
981 static void __error(struct compile_state *state, struct triple *ptr, 
982         char *fmt, ...)
983 {
984         va_list args;
985         va_start(args, fmt);
986         loc(stderr, state, ptr);
987         vfprintf(stderr, fmt, args);
988         va_end(args);
989         fprintf(stderr, "\n");
990         do_cleanup(state);
991         if (state->debug & DEBUG_ABORT_ON_ERROR) {
992                 abort();
993         }
994         exit(1);
995 }
996
997 static void __warning(struct compile_state *state, struct triple *ptr, 
998         char *fmt, ...)
999 {
1000         va_list args;
1001         va_start(args, fmt);
1002         loc(stderr, state, ptr);
1003         fprintf(stderr, "warning: "); 
1004         vfprintf(stderr, fmt, args);
1005         fprintf(stderr, "\n");
1006         va_end(args);
1007 }
1008
1009 #if DEBUG_ERROR_MESSAGES 
1010 #  define internal_error fprintf(stderr,  "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_error
1011 #  define internal_warning fprintf(stderr,  "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_warning
1012 #  define error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__error
1013 #  define warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__warning
1014 #else
1015 #  define internal_error __internal_error
1016 #  define internal_warning __internal_warning
1017 #  define error __error
1018 #  define warning __warning
1019 #endif
1020 #define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1021
1022 static void valid_op(struct compile_state *state, int op)
1023 {
1024         char *fmt = "invalid op: %d";
1025         if (op >= OP_MAX) {
1026                 internal_error(state, 0, fmt, op);
1027         }
1028         if (op < 0) {
1029                 internal_error(state, 0, fmt, op);
1030         }
1031 }
1032
1033 static void valid_ins(struct compile_state *state, struct triple *ptr)
1034 {
1035         valid_op(state, ptr->op);
1036 }
1037
1038 static void process_trigraphs(struct compile_state *state)
1039 {
1040         char *src, *dest, *end;
1041         struct file_state *file;
1042         file = state->file;
1043         src = dest = file->buf;
1044         end = file->buf + file->size;
1045         while((end - src) >= 3) {
1046                 if ((src[0] == '?') && (src[1] == '?')) {
1047                         int c = -1;
1048                         switch(src[2]) {
1049                         case '=': c = '#'; break;
1050                         case '/': c = '\\'; break;
1051                         case '\'': c = '^'; break;
1052                         case '(': c = '['; break;
1053                         case ')': c = ']'; break;
1054                         case '!': c = '!'; break;
1055                         case '<': c = '{'; break;
1056                         case '>': c = '}'; break;
1057                         case '-': c = '~'; break;
1058                         }
1059                         if (c != -1) {
1060                                 *dest++ = c;
1061                                 src += 3;
1062                         }
1063                         else {
1064                                 *dest++ = *src++;
1065                         }
1066                 }
1067                 else {
1068                         *dest++ = *src++;
1069                 }
1070         }
1071         while(src != end) {
1072                 *dest++ = *src++;
1073         }
1074         file->size = dest - file->buf;
1075 }
1076
1077 static void splice_lines(struct compile_state *state)
1078 {
1079         char *src, *dest, *end;
1080         struct file_state *file;
1081         file = state->file;
1082         src = dest = file->buf;
1083         end = file->buf + file->size;
1084         while((end - src) >= 2) {
1085                 if ((src[0] == '\\') && (src[1] == '\n')) {
1086                         src += 2;
1087                 }
1088                 else {
1089                         *dest++ = *src++;
1090                 }
1091         }
1092         while(src != end) {
1093                 *dest++ = *src++;
1094         }
1095         file->size = dest - file->buf;
1096 }
1097
1098 static struct type void_type;
1099 static void use_triple(struct triple *used, struct triple *user)
1100 {
1101         struct triple_set **ptr, *new;
1102         if (!used)
1103                 return;
1104         if (!user)
1105                 return;
1106         ptr = &used->use;
1107         while(*ptr) {
1108                 if ((*ptr)->member == user) {
1109                         return;
1110                 }
1111                 ptr = &(*ptr)->next;
1112         }
1113         /* Append new to the head of the list, 
1114          * copy_func and rename_block_variables
1115          * depends on this.
1116          */
1117         new = xcmalloc(sizeof(*new), "triple_set");
1118         new->member = user;
1119         new->next   = used->use;
1120         used->use   = new;
1121 }
1122
1123 static void unuse_triple(struct triple *used, struct triple *unuser)
1124 {
1125         struct triple_set *use, **ptr;
1126         if (!used) {
1127                 return;
1128         }
1129         ptr = &used->use;
1130         while(*ptr) {
1131                 use = *ptr;
1132                 if (use->member == unuser) {
1133                         *ptr = use->next;
1134                         xfree(use);
1135                 }
1136                 else {
1137                         ptr = &use->next;
1138                 }
1139         }
1140 }
1141
1142 static void push_triple(struct triple *used, struct triple *user)
1143 {
1144         struct triple_set *new;
1145         if (!used)
1146                 return;
1147         if (!user)
1148                 return;
1149         /* Append new to the head of the list,
1150          * it's the only sensible behavoir for a stack.
1151          */
1152         new = xcmalloc(sizeof(*new), "triple_set");
1153         new->member = user;
1154         new->next   = used->use;
1155         used->use   = new;
1156 }
1157
1158 static void pop_triple(struct triple *used, struct triple *unuser)
1159 {
1160         struct triple_set *use, **ptr;
1161         ptr = &used->use;
1162         while(*ptr) {
1163                 use = *ptr;
1164                 if (use->member == unuser) {
1165                         *ptr = use->next;
1166                         xfree(use);
1167                         /* Only free one occurance from the stack */
1168                         return;
1169                 }
1170                 else {
1171                         ptr = &use->next;
1172                 }
1173         }
1174 }
1175
1176
1177 /* The zero triple is used as a place holder when we are removing pointers
1178  * from a triple.  Having allows certain sanity checks to pass even
1179  * when the original triple that was pointed to is gone.
1180  */
1181 static struct triple zero_triple = {
1182         .next     = &zero_triple,
1183         .prev     = &zero_triple,
1184         .use      = 0,
1185         .op       = OP_INTCONST,
1186         .sizes    = TRIPLE_SIZES(0, 0, 0, 0),
1187         .id       = -1, /* An invalid id */
1188         .u = { .cval   = 0, },
1189         .filename = __FILE__,
1190         .line     = __LINE__,
1191         .col      = 0,
1192         .param { [0] = 0, [1] = 0, },
1193 };
1194
1195
1196 static unsigned short triple_sizes(struct compile_state *state,
1197         int op, struct type *type, int lhs_wanted, int rhs_wanted)
1198 {
1199         int lhs, rhs, misc, targ;
1200         valid_op(state, op);
1201         lhs = table_ops[op].lhs;
1202         rhs = table_ops[op].rhs;
1203         misc = table_ops[op].misc;
1204         targ = table_ops[op].targ;
1205         
1206         
1207         if (op == OP_CALL) {
1208                 struct type *param;
1209                 rhs = 0;
1210                 param = type->right;
1211                 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
1212                         rhs++;
1213                         param = param->right;
1214                 }
1215                 if ((param->type & TYPE_MASK) != TYPE_VOID) {
1216                         rhs++;
1217                 }
1218                 lhs = 0;
1219                 if ((type->left->type & TYPE_MASK) == TYPE_STRUCT) {
1220                         lhs = type->left->elements;
1221                 }
1222         }
1223         else if (op == OP_VAL_VEC) {
1224                 rhs = type->elements;
1225         }
1226         else if ((op == OP_BRANCH) || (op == OP_PHI)) {
1227                 rhs = rhs_wanted;
1228         }
1229         else if (op == OP_ASM) {
1230                 rhs = rhs_wanted;
1231                 lhs = lhs_wanted;
1232         }
1233         if ((rhs < 0) || (rhs > MAX_RHS)) {
1234                 internal_error(state, 0, "bad rhs");
1235         }
1236         if ((lhs < 0) || (lhs > MAX_LHS)) {
1237                 internal_error(state, 0, "bad lhs");
1238         }
1239         if ((misc < 0) || (misc > MAX_MISC)) {
1240                 internal_error(state, 0, "bad misc");
1241         }
1242         if ((targ < 0) || (targ > MAX_TARG)) {
1243                 internal_error(state, 0, "bad targs");
1244         }
1245         return TRIPLE_SIZES(lhs, rhs, misc, targ);
1246 }
1247
1248 static struct triple *alloc_triple(struct compile_state *state, 
1249         int op, struct type *type, int lhs, int rhs,
1250         const char *filename, int line, int col)
1251 {
1252         size_t size, sizes, extra_count, min_count;
1253         struct triple *ret;
1254         sizes = triple_sizes(state, op, type, lhs, rhs);
1255
1256         min_count = sizeof(ret->param)/sizeof(ret->param[0]);
1257         extra_count = TRIPLE_SIZE(sizes);
1258         extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
1259
1260         size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
1261         ret = xcmalloc(size, "tripple");
1262         ret->op       = op;
1263         ret->sizes    = sizes;
1264         ret->type     = type;
1265         ret->next     = ret;
1266         ret->prev     = ret;
1267         ret->filename = filename;
1268         ret->line     = line;
1269         ret->col      = col;
1270         return ret;
1271 }
1272
1273 struct triple *dup_triple(struct compile_state *state, struct triple *src)
1274 {
1275         struct triple *dup;
1276         int src_lhs, src_rhs, src_size;
1277         src_lhs = TRIPLE_LHS(src->sizes);
1278         src_rhs = TRIPLE_RHS(src->sizes);
1279         src_size = TRIPLE_SIZE(src->sizes);
1280         dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
1281                 src->filename, src->line, src->col);
1282         memcpy(dup, src, sizeof(*src));
1283         memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
1284         return dup;
1285 }
1286
1287 static struct triple *new_triple(struct compile_state *state, 
1288         int op, struct type *type, int lhs, int rhs)
1289 {
1290         struct triple *ret;
1291         const char *filename;
1292         int line, col;
1293         filename = 0;
1294         line = 0;
1295         col  = 0;
1296         if (state->file) {
1297                 filename = state->file->basename;
1298                 line     = state->file->line;
1299                 col      = get_col(state->file);
1300         }
1301         ret = alloc_triple(state, op, type, lhs, rhs,
1302                 filename, line, col);
1303         return ret;
1304 }
1305
1306 static struct triple *build_triple(struct compile_state *state, 
1307         int op, struct type *type, struct triple *left, struct triple *right,
1308         const char *filename, int line, int col)
1309 {
1310         struct triple *ret;
1311         size_t count;
1312         ret = alloc_triple(state, op, type, -1, -1, filename, line, col);
1313         count = TRIPLE_SIZE(ret->sizes);
1314         if (count > 0) {
1315                 ret->param[0] = left;
1316         }
1317         if (count > 1) {
1318                 ret->param[1] = right;
1319         }
1320         return ret;
1321 }
1322
1323 static struct triple *triple(struct compile_state *state, 
1324         int op, struct type *type, struct triple *left, struct triple *right)
1325 {
1326         struct triple *ret;
1327         size_t count;
1328         ret = new_triple(state, op, type, -1, -1);
1329         count = TRIPLE_SIZE(ret->sizes);
1330         if (count >= 1) {
1331                 ret->param[0] = left;
1332         }
1333         if (count >= 2) {
1334                 ret->param[1] = right;
1335         }
1336         return ret;
1337 }
1338
1339 static struct triple *branch(struct compile_state *state, 
1340         struct triple *targ, struct triple *test)
1341 {
1342         struct triple *ret;
1343         ret = new_triple(state, OP_BRANCH, &void_type, -1, test?1:0);
1344         if (test) {
1345                 RHS(ret, 0) = test;
1346         }
1347         TARG(ret, 0) = targ;
1348         /* record the branch target was used */
1349         if (!targ || (targ->op != OP_LABEL)) {
1350                 internal_error(state, 0, "branch not to label");
1351                 use_triple(targ, ret);
1352         }
1353         return ret;
1354 }
1355
1356
1357 static void insert_triple(struct compile_state *state,
1358         struct triple *first, struct triple *ptr)
1359 {
1360         if (ptr) {
1361                 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
1362                         internal_error(state, ptr, "expression already used");
1363                 }
1364                 ptr->next       = first;
1365                 ptr->prev       = first->prev;
1366                 ptr->prev->next = ptr;
1367                 ptr->next->prev = ptr;
1368                 if ((ptr->prev->op == OP_BRANCH) && 
1369                         TRIPLE_RHS(ptr->prev->sizes)) {
1370                         unuse_triple(first, ptr->prev);
1371                         use_triple(ptr, ptr->prev);
1372                 }
1373         }
1374 }
1375
1376 static int triple_stores_block(struct compile_state *state, struct triple *ins)
1377 {
1378         /* This function is used to determine if u.block 
1379          * is utilized to store the current block number.
1380          */
1381         int stores_block;
1382         valid_ins(state, ins);
1383         stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
1384         return stores_block;
1385 }
1386
1387 static struct block *block_of_triple(struct compile_state *state, 
1388         struct triple *ins)
1389 {
1390         struct triple *first;
1391         first = RHS(state->main_function, 0);
1392         while(ins != first && !triple_stores_block(state, ins)) {
1393                 if (ins == ins->prev) {
1394                         internal_error(state, 0, "ins == ins->prev?");
1395                 }
1396                 ins = ins->prev;
1397         }
1398         if (!triple_stores_block(state, ins)) {
1399                 internal_error(state, ins, "Cannot find block");
1400         }
1401         return ins->u.block;
1402 }
1403
1404 static struct triple *pre_triple(struct compile_state *state,
1405         struct triple *base,
1406         int op, struct type *type, struct triple *left, struct triple *right)
1407 {
1408         struct block *block;
1409         struct triple *ret;
1410         block = block_of_triple(state, base);
1411         ret = build_triple(state, op, type, left, right, 
1412                 base->filename, base->line, base->col);
1413         if (triple_stores_block(state, ret)) {
1414                 ret->u.block = block;
1415         }
1416         insert_triple(state, base, ret);
1417         if (block->first == base) {
1418                 block->first = ret;
1419         }
1420         return ret;
1421 }
1422
1423 static struct triple *post_triple(struct compile_state *state,
1424         struct triple *base,
1425         int op, struct type *type, struct triple *left, struct triple *right)
1426 {
1427         struct block *block;
1428         struct triple *ret;
1429         block = block_of_triple(state, base);
1430         ret = build_triple(state, op, type, left, right, 
1431                 base->filename, base->line, base->col);
1432         if (triple_stores_block(state, ret)) {
1433                 ret->u.block = block;
1434         }
1435         insert_triple(state, base->next, ret);
1436         if (block->last == base) {
1437                 block->last = ret;
1438         }
1439         return ret;
1440 }
1441
1442 static struct triple *label(struct compile_state *state)
1443 {
1444         /* Labels don't get a type */
1445         struct triple *result;
1446         result = triple(state, OP_LABEL, &void_type, 0, 0);
1447         return result;
1448 }
1449
1450 static void display_triple(FILE *fp, struct triple *ins)
1451 {
1452         if (ins->op == OP_INTCONST) {
1453                 fprintf(fp, "(%p) %3d %-2d %-10s <0x%08lx>          @ %s:%d.%d\n",
1454                         ins, ID_REG(ins->id), ins->template_id, tops(ins->op), 
1455                         ins->u.cval,
1456                         ins->filename, ins->line, ins->col);
1457         }
1458         else if (ins->op == OP_ADDRCONST) {
1459                 fprintf(fp, "(%p) %3d %-2d %-10s %-10p <0x%08lx> @ %s:%d.%d\n",
1460                         ins, ID_REG(ins->id), ins->template_id, tops(ins->op), 
1461                         MISC(ins, 0), ins->u.cval,
1462                         ins->filename, ins->line, ins->col);
1463         }
1464         else {
1465                 int i, count;
1466                 fprintf(fp, "(%p) %3d %-2d %-10s", 
1467                         ins, ID_REG(ins->id), ins->template_id, tops(ins->op));
1468                 count = TRIPLE_SIZE(ins->sizes);
1469                 for(i = 0; i < count; i++) {
1470                         fprintf(fp, " %-10p", ins->param[i]);
1471                 }
1472                 for(; i < 2; i++) {
1473                         printf("           ");
1474                 }
1475                 fprintf(fp, " @ %s:%d.%d\n", 
1476                         ins->filename, ins->line, ins->col);
1477         }
1478         fflush(fp);
1479 }
1480
1481 static int triple_is_pure(struct compile_state *state, struct triple *ins)
1482 {
1483         /* Does the triple have no side effects.
1484          * I.e. Rexecuting the triple with the same arguments 
1485          * gives the same value.
1486          */
1487         unsigned pure;
1488         valid_ins(state, ins);
1489         pure = PURE_BITS(table_ops[ins->op].flags);
1490         if ((pure != PURE) && (pure != IMPURE)) {
1491                 internal_error(state, 0, "Purity of %s not known\n",
1492                         tops(ins->op));
1493         }
1494         return pure == PURE;
1495 }
1496
1497 static int triple_is_branch(struct compile_state *state, struct triple *ins)
1498 {
1499         /* This function is used to determine which triples need
1500          * a register.
1501          */
1502         int is_branch;
1503         valid_ins(state, ins);
1504         is_branch = (table_ops[ins->op].targ != 0);
1505         return is_branch;
1506 }
1507
1508 static int triple_is_def(struct compile_state *state, struct triple *ins)
1509 {
1510         /* This function is used to determine which triples need
1511          * a register.
1512          */
1513         int is_def;
1514         valid_ins(state, ins);
1515         is_def = (table_ops[ins->op].flags & DEF) == DEF;
1516         return is_def;
1517 }
1518
1519 static struct triple **triple_iter(struct compile_state *state,
1520         size_t count, struct triple **vector,
1521         struct triple *ins, struct triple **last)
1522 {
1523         struct triple **ret;
1524         ret = 0;
1525         if (count) {
1526                 if (!last) {
1527                         ret = vector;
1528                 }
1529                 else if ((last >= vector) && (last < (vector + count - 1))) {
1530                         ret = last + 1;
1531                 }
1532         }
1533         return ret;
1534         
1535 }
1536
1537 static struct triple **triple_lhs(struct compile_state *state,
1538         struct triple *ins, struct triple **last)
1539 {
1540         return triple_iter(state, TRIPLE_LHS(ins->sizes), &LHS(ins,0), 
1541                 ins, last);
1542 }
1543
1544 static struct triple **triple_rhs(struct compile_state *state,
1545         struct triple *ins, struct triple **last)
1546 {
1547         return triple_iter(state, TRIPLE_RHS(ins->sizes), &RHS(ins,0), 
1548                 ins, last);
1549 }
1550
1551 static struct triple **triple_misc(struct compile_state *state,
1552         struct triple *ins, struct triple **last)
1553 {
1554         return triple_iter(state, TRIPLE_MISC(ins->sizes), &MISC(ins,0), 
1555                 ins, last);
1556 }
1557
1558 static struct triple **triple_targ(struct compile_state *state,
1559         struct triple *ins, struct triple **last)
1560 {
1561         size_t count;
1562         struct triple **ret, **vector;
1563         ret = 0;
1564         count = TRIPLE_TARG(ins->sizes);
1565         vector = &TARG(ins, 0);
1566         if (count) {
1567                 if (!last) {
1568                         ret = vector;
1569                 }
1570                 else if ((last >= vector) && (last < (vector + count - 1))) {
1571                         ret = last + 1;
1572                 }
1573                 else if ((last == (vector + count - 1)) && 
1574                         TRIPLE_RHS(ins->sizes)) {
1575                         ret = &ins->next;
1576                 }
1577         }
1578         return ret;
1579 }
1580
1581
1582 static void verify_use(struct compile_state *state,
1583         struct triple *user, struct triple *used)
1584 {
1585         int size, i;
1586         size = TRIPLE_SIZE(user->sizes);
1587         for(i = 0; i < size; i++) {
1588                 if (user->param[i] == used) {
1589                         break;
1590                 }
1591         }
1592         if (triple_is_branch(state, user)) {
1593                 if (user->next == used) {
1594                         i = -1;
1595                 }
1596         }
1597         if (i == size) {
1598                 internal_error(state, user, "%s(%p) does not use %s(%p)",
1599                         tops(user->op), user, tops(used->op), used);
1600         }
1601 }
1602
1603 static int find_rhs_use(struct compile_state *state, 
1604         struct triple *user, struct triple *used)
1605 {
1606         struct triple **param;
1607         int size, i;
1608         verify_use(state, user, used);
1609         size = TRIPLE_RHS(user->sizes);
1610         param = &RHS(user, 0);
1611         for(i = 0; i < size; i++) {
1612                 if (param[i] == used) {
1613                         return i;
1614                 }
1615         }
1616         return -1;
1617 }
1618
1619 static void free_triple(struct compile_state *state, struct triple *ptr)
1620 {
1621         size_t size;
1622         size = sizeof(*ptr) - sizeof(ptr->param) +
1623                 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr->sizes));
1624         ptr->prev->next = ptr->next;
1625         ptr->next->prev = ptr->prev;
1626         if (ptr->use) {
1627                 internal_error(state, ptr, "ptr->use != 0");
1628         }
1629         memset(ptr, -1, size);
1630         xfree(ptr);
1631 }
1632
1633 static void release_triple(struct compile_state *state, struct triple *ptr)
1634 {
1635         struct triple_set *set, *next;
1636         struct triple **expr;
1637         /* Remove ptr from use chains where it is the user */
1638         expr = triple_rhs(state, ptr, 0);
1639         for(; expr; expr = triple_rhs(state, ptr, expr)) {
1640                 if (*expr) {
1641                         unuse_triple(*expr, ptr);
1642                 }
1643         }
1644         expr = triple_lhs(state, ptr, 0);
1645         for(; expr; expr = triple_lhs(state, ptr, expr)) {
1646                 if (*expr) {
1647                         unuse_triple(*expr, ptr);
1648                 }
1649         }
1650         expr = triple_misc(state, ptr, 0);
1651         for(; expr; expr = triple_misc(state, ptr, expr)) {
1652                 if (*expr) {
1653                         unuse_triple(*expr, ptr);
1654                 }
1655         }
1656         expr = triple_targ(state, ptr, 0);
1657         for(; expr; expr = triple_targ(state, ptr, expr)) {
1658                 if (*expr) {
1659                         unuse_triple(*expr, ptr);
1660                 }
1661         }
1662         /* Reomve ptr from use chains where it is used */
1663         for(set = ptr->use; set; set = next) {
1664                 next = set->next;
1665                 expr = triple_rhs(state, set->member, 0);
1666                 for(; expr; expr = triple_rhs(state, set->member, expr)) {
1667                         if (*expr == ptr) {
1668                                 *expr = &zero_triple;
1669                         }
1670                 }
1671                 expr = triple_lhs(state, set->member, 0);
1672                 for(; expr; expr = triple_lhs(state, set->member, expr)) {
1673                         if (*expr == ptr) {
1674                                 *expr = &zero_triple;
1675                         }
1676                 }
1677                 expr = triple_misc(state, set->member, 0);
1678                 for(; expr; expr = triple_misc(state, set->member, expr)) {
1679                         if (*expr == ptr) {
1680                                 *expr = &zero_triple;
1681                         }
1682                 }
1683                 expr = triple_targ(state, set->member, 0);
1684                 for(; expr; expr = triple_targ(state, set->member, expr)) {
1685                         if (*expr == ptr) {
1686                                 *expr = &zero_triple;
1687                         }
1688                 }
1689                 unuse_triple(ptr, set->member);
1690         }
1691         free_triple(state, ptr);
1692 }
1693
1694 static void print_triple(struct compile_state *state, struct triple *ptr);
1695
1696 #define TOK_UNKNOWN     0
1697 #define TOK_SPACE       1
1698 #define TOK_SEMI        2
1699 #define TOK_LBRACE      3
1700 #define TOK_RBRACE      4
1701 #define TOK_COMMA       5
1702 #define TOK_EQ          6
1703 #define TOK_COLON       7
1704 #define TOK_LBRACKET    8
1705 #define TOK_RBRACKET    9
1706 #define TOK_LPAREN      10
1707 #define TOK_RPAREN      11
1708 #define TOK_STAR        12
1709 #define TOK_DOTS        13
1710 #define TOK_MORE        14
1711 #define TOK_LESS        15
1712 #define TOK_TIMESEQ     16
1713 #define TOK_DIVEQ       17
1714 #define TOK_MODEQ       18
1715 #define TOK_PLUSEQ      19
1716 #define TOK_MINUSEQ     20
1717 #define TOK_SLEQ        21
1718 #define TOK_SREQ        22
1719 #define TOK_ANDEQ       23
1720 #define TOK_XOREQ       24
1721 #define TOK_OREQ        25
1722 #define TOK_EQEQ        26
1723 #define TOK_NOTEQ       27
1724 #define TOK_QUEST       28
1725 #define TOK_LOGOR       29
1726 #define TOK_LOGAND      30
1727 #define TOK_OR          31
1728 #define TOK_AND         32
1729 #define TOK_XOR         33
1730 #define TOK_LESSEQ      34
1731 #define TOK_MOREEQ      35
1732 #define TOK_SL          36
1733 #define TOK_SR          37
1734 #define TOK_PLUS        38
1735 #define TOK_MINUS       39
1736 #define TOK_DIV         40
1737 #define TOK_MOD         41
1738 #define TOK_PLUSPLUS    42
1739 #define TOK_MINUSMINUS  43
1740 #define TOK_BANG        44
1741 #define TOK_ARROW       45
1742 #define TOK_DOT         46
1743 #define TOK_TILDE       47
1744 #define TOK_LIT_STRING  48
1745 #define TOK_LIT_CHAR    49
1746 #define TOK_LIT_INT     50
1747 #define TOK_LIT_FLOAT   51
1748 #define TOK_MACRO       52
1749 #define TOK_CONCATENATE 53
1750
1751 #define TOK_IDENT       54
1752 #define TOK_STRUCT_NAME 55
1753 #define TOK_ENUM_CONST  56
1754 #define TOK_TYPE_NAME   57
1755
1756 #define TOK_AUTO        58
1757 #define TOK_BREAK       59
1758 #define TOK_CASE        60
1759 #define TOK_CHAR        61
1760 #define TOK_CONST       62
1761 #define TOK_CONTINUE    63
1762 #define TOK_DEFAULT     64
1763 #define TOK_DO          65
1764 #define TOK_DOUBLE      66
1765 #define TOK_ELSE        67
1766 #define TOK_ENUM        68
1767 #define TOK_EXTERN      69
1768 #define TOK_FLOAT       70
1769 #define TOK_FOR         71
1770 #define TOK_GOTO        72
1771 #define TOK_IF          73
1772 #define TOK_INLINE      74
1773 #define TOK_INT         75
1774 #define TOK_LONG        76
1775 #define TOK_REGISTER    77
1776 #define TOK_RESTRICT    78
1777 #define TOK_RETURN      79
1778 #define TOK_SHORT       80
1779 #define TOK_SIGNED      81
1780 #define TOK_SIZEOF      82
1781 #define TOK_STATIC      83
1782 #define TOK_STRUCT      84
1783 #define TOK_SWITCH      85
1784 #define TOK_TYPEDEF     86
1785 #define TOK_UNION       87
1786 #define TOK_UNSIGNED    88
1787 #define TOK_VOID        89
1788 #define TOK_VOLATILE    90
1789 #define TOK_WHILE       91
1790 #define TOK_ASM         92
1791 #define TOK_ATTRIBUTE   93
1792 #define TOK_ALIGNOF     94
1793 #define TOK_FIRST_KEYWORD TOK_AUTO
1794 #define TOK_LAST_KEYWORD  TOK_ALIGNOF
1795
1796 #define TOK_DEFINE      100
1797 #define TOK_UNDEF       101
1798 #define TOK_INCLUDE     102
1799 #define TOK_LINE        103
1800 #define TOK_ERROR       104
1801 #define TOK_WARNING     105
1802 #define TOK_PRAGMA      106
1803 #define TOK_IFDEF       107
1804 #define TOK_IFNDEF      108
1805 #define TOK_ELIF        109
1806 #define TOK_ENDIF       110
1807
1808 #define TOK_FIRST_MACRO TOK_DEFINE
1809 #define TOK_LAST_MACRO  TOK_ENDIF
1810          
1811 #define TOK_EOF         111
1812
1813 static const char *tokens[] = {
1814 [TOK_UNKNOWN     ] = "unknown",
1815 [TOK_SPACE       ] = ":space:",
1816 [TOK_SEMI        ] = ";",
1817 [TOK_LBRACE      ] = "{",
1818 [TOK_RBRACE      ] = "}",
1819 [TOK_COMMA       ] = ",",
1820 [TOK_EQ          ] = "=",
1821 [TOK_COLON       ] = ":",
1822 [TOK_LBRACKET    ] = "[",
1823 [TOK_RBRACKET    ] = "]",
1824 [TOK_LPAREN      ] = "(",
1825 [TOK_RPAREN      ] = ")",
1826 [TOK_STAR        ] = "*",
1827 [TOK_DOTS        ] = "...",
1828 [TOK_MORE        ] = ">",
1829 [TOK_LESS        ] = "<",
1830 [TOK_TIMESEQ     ] = "*=",
1831 [TOK_DIVEQ       ] = "/=",
1832 [TOK_MODEQ       ] = "%=",
1833 [TOK_PLUSEQ      ] = "+=",
1834 [TOK_MINUSEQ     ] = "-=",
1835 [TOK_SLEQ        ] = "<<=",
1836 [TOK_SREQ        ] = ">>=",
1837 [TOK_ANDEQ       ] = "&=",
1838 [TOK_XOREQ       ] = "^=",
1839 [TOK_OREQ        ] = "|=",
1840 [TOK_EQEQ        ] = "==",
1841 [TOK_NOTEQ       ] = "!=",
1842 [TOK_QUEST       ] = "?",
1843 [TOK_LOGOR       ] = "||",
1844 [TOK_LOGAND      ] = "&&",
1845 [TOK_OR          ] = "|",
1846 [TOK_AND         ] = "&",
1847 [TOK_XOR         ] = "^",
1848 [TOK_LESSEQ      ] = "<=",
1849 [TOK_MOREEQ      ] = ">=",
1850 [TOK_SL          ] = "<<",
1851 [TOK_SR          ] = ">>",
1852 [TOK_PLUS        ] = "+",
1853 [TOK_MINUS       ] = "-",
1854 [TOK_DIV         ] = "/",
1855 [TOK_MOD         ] = "%",
1856 [TOK_PLUSPLUS    ] = "++",
1857 [TOK_MINUSMINUS  ] = "--",
1858 [TOK_BANG        ] = "!",
1859 [TOK_ARROW       ] = "->",
1860 [TOK_DOT         ] = ".",
1861 [TOK_TILDE       ] = "~",
1862 [TOK_LIT_STRING  ] = ":string:",
1863 [TOK_IDENT       ] = ":ident:",
1864 [TOK_TYPE_NAME   ] = ":typename:",
1865 [TOK_LIT_CHAR    ] = ":char:",
1866 [TOK_LIT_INT     ] = ":integer:",
1867 [TOK_LIT_FLOAT   ] = ":float:",
1868 [TOK_MACRO       ] = "#",
1869 [TOK_CONCATENATE ] = "##",
1870
1871 [TOK_AUTO        ] = "auto",
1872 [TOK_BREAK       ] = "break",
1873 [TOK_CASE        ] = "case",
1874 [TOK_CHAR        ] = "char",
1875 [TOK_CONST       ] = "const",
1876 [TOK_CONTINUE    ] = "continue",
1877 [TOK_DEFAULT     ] = "default",
1878 [TOK_DO          ] = "do",
1879 [TOK_DOUBLE      ] = "double",
1880 [TOK_ELSE        ] = "else",
1881 [TOK_ENUM        ] = "enum",
1882 [TOK_EXTERN      ] = "extern",
1883 [TOK_FLOAT       ] = "float",
1884 [TOK_FOR         ] = "for",
1885 [TOK_GOTO        ] = "goto",
1886 [TOK_IF          ] = "if",
1887 [TOK_INLINE      ] = "inline",
1888 [TOK_INT         ] = "int",
1889 [TOK_LONG        ] = "long",
1890 [TOK_REGISTER    ] = "register",
1891 [TOK_RESTRICT    ] = "restrict",
1892 [TOK_RETURN      ] = "return",
1893 [TOK_SHORT       ] = "short",
1894 [TOK_SIGNED      ] = "signed",
1895 [TOK_SIZEOF      ] = "sizeof",
1896 [TOK_STATIC      ] = "static",
1897 [TOK_STRUCT      ] = "struct",
1898 [TOK_SWITCH      ] = "switch",
1899 [TOK_TYPEDEF     ] = "typedef",
1900 [TOK_UNION       ] = "union",
1901 [TOK_UNSIGNED    ] = "unsigned",
1902 [TOK_VOID        ] = "void",
1903 [TOK_VOLATILE    ] = "volatile",
1904 [TOK_WHILE       ] = "while",
1905 [TOK_ASM         ] = "asm",
1906 [TOK_ATTRIBUTE   ] = "__attribute__",
1907 [TOK_ALIGNOF     ] = "__alignof__",
1908
1909 [TOK_DEFINE      ] = "define",
1910 [TOK_UNDEF       ] = "undef",
1911 [TOK_INCLUDE     ] = "include",
1912 [TOK_LINE        ] = "line",
1913 [TOK_ERROR       ] = "error",
1914 [TOK_WARNING     ] = "warning",
1915 [TOK_PRAGMA      ] = "pragma",
1916 [TOK_IFDEF       ] = "ifdef",
1917 [TOK_IFNDEF      ] = "ifndef",
1918 [TOK_ELIF        ] = "elif",
1919 [TOK_ENDIF       ] = "endif",
1920
1921 [TOK_EOF         ] = "EOF",
1922 };
1923
1924 static unsigned int hash(const char *str, int str_len)
1925 {
1926         unsigned int hash;
1927         const char *end;
1928         end = str + str_len;
1929         hash = 0;
1930         for(; str < end; str++) {
1931                 hash = (hash *263) + *str;
1932         }
1933         hash = hash & (HASH_TABLE_SIZE -1);
1934         return hash;
1935 }
1936
1937 static struct hash_entry *lookup(
1938         struct compile_state *state, const char *name, int name_len)
1939 {
1940         struct hash_entry *entry;
1941         unsigned int index;
1942         index = hash(name, name_len);
1943         entry = state->hash_table[index];
1944         while(entry && 
1945                 ((entry->name_len != name_len) ||
1946                         (memcmp(entry->name, name, name_len) != 0))) {
1947                 entry = entry->next;
1948         }
1949         if (!entry) {
1950                 char *new_name;
1951                 /* Get a private copy of the name */
1952                 new_name = xmalloc(name_len + 1, "hash_name");
1953                 memcpy(new_name, name, name_len);
1954                 new_name[name_len] = '\0';
1955
1956                 /* Create a new hash entry */
1957                 entry = xcmalloc(sizeof(*entry), "hash_entry");
1958                 entry->next = state->hash_table[index];
1959                 entry->name = new_name;
1960                 entry->name_len = name_len;
1961
1962                 /* Place the new entry in the hash table */
1963                 state->hash_table[index] = entry;
1964         }
1965         return entry;
1966 }
1967
1968 static void ident_to_keyword(struct compile_state *state, struct token *tk)
1969 {
1970         struct hash_entry *entry;
1971         entry = tk->ident;
1972         if (entry && ((entry->tok == TOK_TYPE_NAME) ||
1973                 (entry->tok == TOK_ENUM_CONST) ||
1974                 ((entry->tok >= TOK_FIRST_KEYWORD) && 
1975                         (entry->tok <= TOK_LAST_KEYWORD)))) {
1976                 tk->tok = entry->tok;
1977         }
1978 }
1979
1980 static void ident_to_macro(struct compile_state *state, struct token *tk)
1981 {
1982         struct hash_entry *entry;
1983         entry = tk->ident;
1984         if (entry && 
1985                 (entry->tok >= TOK_FIRST_MACRO) &&
1986                 (entry->tok <= TOK_LAST_MACRO)) {
1987                 tk->tok = entry->tok;
1988         }
1989 }
1990
1991 static void hash_keyword(
1992         struct compile_state *state, const char *keyword, int tok)
1993 {
1994         struct hash_entry *entry;
1995         entry = lookup(state, keyword, strlen(keyword));
1996         if (entry && entry->tok != TOK_UNKNOWN) {
1997                 die("keyword %s already hashed", keyword);
1998         }
1999         entry->tok  = tok;
2000 }
2001
2002 static void symbol(
2003         struct compile_state *state, struct hash_entry *ident,
2004         struct symbol **chain, struct triple *def, struct type *type)
2005 {
2006         struct symbol *sym;
2007         if (*chain && ((*chain)->scope_depth == state->scope_depth)) {
2008                 error(state, 0, "%s already defined", ident->name);
2009         }
2010         sym = xcmalloc(sizeof(*sym), "symbol");
2011         sym->ident = ident;
2012         sym->def   = def;
2013         sym->type  = type;
2014         sym->scope_depth = state->scope_depth;
2015         sym->next = *chain;
2016         *chain    = sym;
2017 }
2018
2019 static void start_scope(struct compile_state *state)
2020 {
2021         state->scope_depth++;
2022 }
2023
2024 static void end_scope_syms(struct symbol **chain, int depth)
2025 {
2026         struct symbol *sym, *next;
2027         sym = *chain;
2028         while(sym && (sym->scope_depth == depth)) {
2029                 next = sym->next;
2030                 xfree(sym);
2031                 sym = next;
2032         }
2033         *chain = sym;
2034 }
2035
2036 static void end_scope(struct compile_state *state)
2037 {
2038         int i;
2039         int depth;
2040         /* Walk through the hash table and remove all symbols
2041          * in the current scope. 
2042          */
2043         depth = state->scope_depth;
2044         for(i = 0; i < HASH_TABLE_SIZE; i++) {
2045                 struct hash_entry *entry;
2046                 entry = state->hash_table[i];
2047                 while(entry) {
2048                         end_scope_syms(&entry->sym_label,  depth);
2049                         end_scope_syms(&entry->sym_struct, depth);
2050                         end_scope_syms(&entry->sym_ident,  depth);
2051                         entry = entry->next;
2052                 }
2053         }
2054         state->scope_depth = depth - 1;
2055 }
2056
2057 static void register_keywords(struct compile_state *state)
2058 {
2059         hash_keyword(state, "auto",          TOK_AUTO);
2060         hash_keyword(state, "break",         TOK_BREAK);
2061         hash_keyword(state, "case",          TOK_CASE);
2062         hash_keyword(state, "char",          TOK_CHAR);
2063         hash_keyword(state, "const",         TOK_CONST);
2064         hash_keyword(state, "continue",      TOK_CONTINUE);
2065         hash_keyword(state, "default",       TOK_DEFAULT);
2066         hash_keyword(state, "do",            TOK_DO);
2067         hash_keyword(state, "double",        TOK_DOUBLE);
2068         hash_keyword(state, "else",          TOK_ELSE);
2069         hash_keyword(state, "enum",          TOK_ENUM);
2070         hash_keyword(state, "extern",        TOK_EXTERN);
2071         hash_keyword(state, "float",         TOK_FLOAT);
2072         hash_keyword(state, "for",           TOK_FOR);
2073         hash_keyword(state, "goto",          TOK_GOTO);
2074         hash_keyword(state, "if",            TOK_IF);
2075         hash_keyword(state, "inline",        TOK_INLINE);
2076         hash_keyword(state, "int",           TOK_INT);
2077         hash_keyword(state, "long",          TOK_LONG);
2078         hash_keyword(state, "register",      TOK_REGISTER);
2079         hash_keyword(state, "restrict",      TOK_RESTRICT);
2080         hash_keyword(state, "return",        TOK_RETURN);
2081         hash_keyword(state, "short",         TOK_SHORT);
2082         hash_keyword(state, "signed",        TOK_SIGNED);
2083         hash_keyword(state, "sizeof",        TOK_SIZEOF);
2084         hash_keyword(state, "static",        TOK_STATIC);
2085         hash_keyword(state, "struct",        TOK_STRUCT);
2086         hash_keyword(state, "switch",        TOK_SWITCH);
2087         hash_keyword(state, "typedef",       TOK_TYPEDEF);
2088         hash_keyword(state, "union",         TOK_UNION);
2089         hash_keyword(state, "unsigned",      TOK_UNSIGNED);
2090         hash_keyword(state, "void",          TOK_VOID);
2091         hash_keyword(state, "volatile",      TOK_VOLATILE);
2092         hash_keyword(state, "__volatile__",  TOK_VOLATILE);
2093         hash_keyword(state, "while",         TOK_WHILE);
2094         hash_keyword(state, "asm",           TOK_ASM);
2095         hash_keyword(state, "__asm__",       TOK_ASM);
2096         hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
2097         hash_keyword(state, "__alignof__",   TOK_ALIGNOF);
2098 }
2099
2100 static void register_macro_keywords(struct compile_state *state)
2101 {
2102         hash_keyword(state, "define",        TOK_DEFINE);
2103         hash_keyword(state, "undef",         TOK_UNDEF);
2104         hash_keyword(state, "include",       TOK_INCLUDE);
2105         hash_keyword(state, "line",          TOK_LINE);
2106         hash_keyword(state, "error",         TOK_ERROR);
2107         hash_keyword(state, "warning",       TOK_WARNING);
2108         hash_keyword(state, "pragma",        TOK_PRAGMA);
2109         hash_keyword(state, "ifdef",         TOK_IFDEF);
2110         hash_keyword(state, "ifndef",        TOK_IFNDEF);
2111         hash_keyword(state, "elif",          TOK_ELIF);
2112         hash_keyword(state, "endif",         TOK_ENDIF);
2113 }
2114
2115 static int spacep(int c)
2116 {
2117         int ret = 0;
2118         switch(c) {
2119         case ' ':
2120         case '\t':
2121         case '\f':
2122         case '\v':
2123         case '\r':
2124         case '\n':
2125                 ret = 1;
2126                 break;
2127         }
2128         return ret;
2129 }
2130
2131 static int digitp(int c)
2132 {
2133         int ret = 0;
2134         switch(c) {
2135         case '0': case '1': case '2': case '3': case '4': 
2136         case '5': case '6': case '7': case '8': case '9':
2137                 ret = 1;
2138                 break;
2139         }
2140         return ret;
2141 }
2142
2143 static int hexdigitp(int c)
2144 {
2145         int ret = 0;
2146         switch(c) {
2147         case '0': case '1': case '2': case '3': case '4': 
2148         case '5': case '6': case '7': case '8': case '9':
2149         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2150         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2151                 ret = 1;
2152                 break;
2153         }
2154         return ret;
2155 }
2156 static int hexdigval(int c) 
2157 {
2158         int val = -1;
2159         if ((c >= '0') && (c <= '9')) {
2160                 val = c - '0';
2161         }
2162         else if ((c >= 'A') && (c <= 'F')) {
2163                 val = 10 + (c - 'A');
2164         }
2165         else if ((c >= 'a') && (c <= 'f')) {
2166                 val = 10 + (c - 'a');
2167         }
2168         return val;
2169 }
2170
2171 static int octdigitp(int c)
2172 {
2173         int ret = 0;
2174         switch(c) {
2175         case '0': case '1': case '2': case '3': 
2176         case '4': case '5': case '6': case '7':
2177                 ret = 1;
2178                 break;
2179         }
2180         return ret;
2181 }
2182 static int octdigval(int c)
2183 {
2184         int val = -1;
2185         if ((c >= '0') && (c <= '7')) {
2186                 val = c - '0';
2187         }
2188         return val;
2189 }
2190
2191 static int letterp(int c)
2192 {
2193         int ret = 0;
2194         switch(c) {
2195         case 'a': case 'b': case 'c': case 'd': case 'e':
2196         case 'f': case 'g': case 'h': case 'i': case 'j':
2197         case 'k': case 'l': case 'm': case 'n': case 'o':
2198         case 'p': case 'q': case 'r': case 's': case 't':
2199         case 'u': case 'v': case 'w': case 'x': case 'y':
2200         case 'z':
2201         case 'A': case 'B': case 'C': case 'D': case 'E':
2202         case 'F': case 'G': case 'H': case 'I': case 'J':
2203         case 'K': case 'L': case 'M': case 'N': case 'O':
2204         case 'P': case 'Q': case 'R': case 'S': case 'T':
2205         case 'U': case 'V': case 'W': case 'X': case 'Y':
2206         case 'Z':
2207         case '_':
2208                 ret = 1;
2209                 break;
2210         }
2211         return ret;
2212 }
2213
2214 static int char_value(struct compile_state *state,
2215         const signed char **strp, const signed char *end)
2216 {
2217         const signed char *str;
2218         int c;
2219         str = *strp;
2220         c = *str++;
2221         if ((c == '\\') && (str < end)) {
2222                 switch(*str) {
2223                 case 'n':  c = '\n'; str++; break;
2224                 case 't':  c = '\t'; str++; break;
2225                 case 'v':  c = '\v'; str++; break;
2226                 case 'b':  c = '\b'; str++; break;
2227                 case 'r':  c = '\r'; str++; break;
2228                 case 'f':  c = '\f'; str++; break;
2229                 case 'a':  c = '\a'; str++; break;
2230                 case '\\': c = '\\'; str++; break;
2231                 case '?':  c = '?';  str++; break;
2232                 case '\'': c = '\''; str++; break;
2233                 case '"':  c = '"';  break;
2234                 case 'x': 
2235                         c = 0;
2236                         str++;
2237                         while((str < end) && hexdigitp(*str)) {
2238                                 c <<= 4;
2239                                 c += hexdigval(*str);
2240                                 str++;
2241                         }
2242                         break;
2243                 case '0': case '1': case '2': case '3': 
2244                 case '4': case '5': case '6': case '7':
2245                         c = 0;
2246                         while((str < end) && octdigitp(*str)) {
2247                                 c <<= 3;
2248                                 c += octdigval(*str);
2249                                 str++;
2250                         }
2251                         break;
2252                 default:
2253                         error(state, 0, "Invalid character constant");
2254                         break;
2255                 }
2256         }
2257         *strp = str;
2258         return c;
2259 }
2260
2261 static char *after_digits(char *ptr, char *end)
2262 {
2263         while((ptr < end) && digitp(*ptr)) {
2264                 ptr++;
2265         }
2266         return ptr;
2267 }
2268
2269 static char *after_octdigits(char *ptr, char *end)
2270 {
2271         while((ptr < end) && octdigitp(*ptr)) {
2272                 ptr++;
2273         }
2274         return ptr;
2275 }
2276
2277 static char *after_hexdigits(char *ptr, char *end)
2278 {
2279         while((ptr < end) && hexdigitp(*ptr)) {
2280                 ptr++;
2281         }
2282         return ptr;
2283 }
2284
2285 static void save_string(struct compile_state *state, 
2286         struct token *tk, char *start, char *end, const char *id)
2287 {
2288         char *str;
2289         int str_len;
2290         /* Create a private copy of the string */
2291         str_len = end - start + 1;
2292         str = xmalloc(str_len + 1, id);
2293         memcpy(str, start, str_len);
2294         str[str_len] = '\0';
2295
2296         /* Store the copy in the token */
2297         tk->val.str = str;
2298         tk->str_len = str_len;
2299 }
2300 static void next_token(struct compile_state *state, int index)
2301 {
2302         struct file_state *file;
2303         struct token *tk;
2304         char *token;
2305         int c, c1, c2, c3;
2306         char *tokp, *end;
2307         int tok;
2308 next_token:
2309         file = state->file;
2310         tk = &state->token[index];
2311         tk->str_len = 0;
2312         tk->ident = 0;
2313         token = tokp = file->pos;
2314         end = file->buf + file->size;
2315         tok = TOK_UNKNOWN;
2316         c = -1;
2317         if (tokp < end) {
2318                 c = *tokp;
2319         }
2320         c1 = -1;
2321         if ((tokp + 1) < end) {
2322                 c1 = tokp[1];
2323         }
2324         c2 = -1;
2325         if ((tokp + 2) < end) {
2326                 c2 = tokp[2];
2327         }
2328         c3 = -1;
2329         if ((tokp + 3) < end) {
2330                 c3 = tokp[3];
2331         }
2332         if (tokp >= end) {
2333                 tok = TOK_EOF;
2334                 tokp = end;
2335         }
2336         /* Whitespace */
2337         else if (spacep(c)) {
2338                 tok = TOK_SPACE;
2339                 while ((tokp < end) && spacep(c)) {
2340                         if (c == '\n') {
2341                                 file->line++;
2342                                 file->line_start = tokp + 1;
2343                         }
2344                         c = *(++tokp);
2345                 }
2346                 if (!spacep(c)) {
2347                         tokp--;
2348                 }
2349         }
2350         /* EOL Comments */
2351         else if ((c == '/') && (c1 == '/')) {
2352                 tok = TOK_SPACE;
2353                 for(tokp += 2; tokp < end; tokp++) {
2354                         c = *tokp;
2355                         if (c == '\n') {
2356                                 file->line++;
2357                                 file->line_start = tokp +1;
2358                                 break;
2359                         }
2360                 }
2361         }
2362         /* Comments */
2363         else if ((c == '/') && (c1 == '*')) {
2364                 int line;
2365                 char *line_start;
2366                 line = file->line;
2367                 line_start = file->line_start;
2368                 for(tokp += 2; (end - tokp) >= 2; tokp++) {
2369                         c = *tokp;
2370                         if (c == '\n') {
2371                                 line++;
2372                                 line_start = tokp +1;
2373                         }
2374                         else if ((c == '*') && (tokp[1] == '/')) {
2375                                 tok = TOK_SPACE;
2376                                 tokp += 1;
2377                                 break;
2378                         }
2379                 }
2380                 if (tok == TOK_UNKNOWN) {
2381                         error(state, 0, "unterminated comment");
2382                 }
2383                 file->line = line;
2384                 file->line_start = line_start;
2385         }
2386         /* string constants */
2387         else if ((c == '"') ||
2388                 ((c == 'L') && (c1 == '"'))) {
2389                 int line;
2390                 char *line_start;
2391                 int wchar;
2392                 line = file->line;
2393                 line_start = file->line_start;
2394                 wchar = 0;
2395                 if (c == 'L') {
2396                         wchar = 1;
2397                         tokp++;
2398                 }
2399                 for(tokp += 1; tokp < end; tokp++) {
2400                         c = *tokp;
2401                         if (c == '\n') {
2402                                 line++;
2403                                 line_start = tokp + 1;
2404                         }
2405                         else if ((c == '\\') && (tokp +1 < end)) {
2406                                 tokp++;
2407                         }
2408                         else if (c == '"') {
2409                                 tok = TOK_LIT_STRING;
2410                                 break;
2411                         }
2412                 }
2413                 if (tok == TOK_UNKNOWN) {
2414                         error(state, 0, "unterminated string constant");
2415                 }
2416                 if (line != file->line) {
2417                         warning(state, 0, "multiline string constant");
2418                 }
2419                 file->line = line;
2420                 file->line_start = line_start;
2421
2422                 /* Save the string value */
2423                 save_string(state, tk, token, tokp, "literal string");
2424         }
2425         /* character constants */
2426         else if ((c == '\'') ||
2427                 ((c == 'L') && (c1 == '\''))) {
2428                 int line;
2429                 char *line_start;
2430                 int wchar;
2431                 line = file->line;
2432                 line_start = file->line_start;
2433                 wchar = 0;
2434                 if (c == 'L') {
2435                         wchar = 1;
2436                         tokp++;
2437                 }
2438                 for(tokp += 1; tokp < end; tokp++) {
2439                         c = *tokp;
2440                         if (c == '\n') {
2441                                 line++;
2442                                 line_start = tokp + 1;
2443                         }
2444                         else if ((c == '\\') && (tokp +1 < end)) {
2445                                 tokp++;
2446                         }
2447                         else if (c == '\'') {
2448                                 tok = TOK_LIT_CHAR;
2449                                 break;
2450                         }
2451                 }
2452                 if (tok == TOK_UNKNOWN) {
2453                         error(state, 0, "unterminated character constant");
2454                 }
2455                 if (line != file->line) {
2456                         warning(state, 0, "multiline character constant");
2457                 }
2458                 file->line = line;
2459                 file->line_start = line_start;
2460
2461                 /* Save the character value */
2462                 save_string(state, tk, token, tokp, "literal character");
2463         }
2464         /* integer and floating constants 
2465          * Integer Constants
2466          * {digits}
2467          * 0[Xx]{hexdigits}
2468          * 0{octdigit}+
2469          * 
2470          * Floating constants
2471          * {digits}.{digits}[Ee][+-]?{digits}
2472          * {digits}.{digits}
2473          * {digits}[Ee][+-]?{digits}
2474          * .{digits}[Ee][+-]?{digits}
2475          * .{digits}
2476          */
2477         
2478         else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
2479                 char *next, *new;
2480                 int is_float;
2481                 is_float = 0;
2482                 if (c != '.') {
2483                         next = after_digits(tokp, end);
2484                 }
2485                 else {
2486                         next = tokp;
2487                 }
2488                 if (next[0] == '.') {
2489                         new = after_digits(next, end);
2490                         is_float = (new != next);
2491                         next = new;
2492                 }
2493                 if ((next[0] == 'e') || (next[0] == 'E')) {
2494                         if (((next + 1) < end) && 
2495                                 ((next[1] == '+') || (next[1] == '-'))) {
2496                                 next++;
2497                         }
2498                         new = after_digits(next, end);
2499                         is_float = (new != next);
2500                         next = new;
2501                 }
2502                 if (is_float) {
2503                         tok = TOK_LIT_FLOAT;
2504                         if ((next < end) && (
2505                                 (next[0] == 'f') ||
2506                                 (next[0] == 'F') ||
2507                                 (next[0] == 'l') ||
2508                                 (next[0] == 'L'))
2509                                 ) {
2510                                 next++;
2511                         }
2512                 }
2513                 if (!is_float && digitp(c)) {
2514                         tok = TOK_LIT_INT;
2515                         if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
2516                                 next = after_hexdigits(tokp + 2, end);
2517                         }
2518                         else if (c == '0') {
2519                                 next = after_octdigits(tokp, end);
2520                         }
2521                         else {
2522                                 next = after_digits(tokp, end);
2523                         }
2524                         /* crazy integer suffixes */
2525                         if ((next < end) && 
2526                                 ((next[0] == 'u') || (next[0] == 'U'))) { 
2527                                 next++;
2528                                 if ((next < end) &&
2529                                         ((next[0] == 'l') || (next[0] == 'L'))) {
2530                                         next++;
2531                                 }
2532                         }
2533                         else if ((next < end) &&
2534                                 ((next[0] == 'l') || (next[0] == 'L'))) {
2535                                 next++;
2536                                 if ((next < end) && 
2537                                         ((next[0] == 'u') || (next[0] == 'U'))) { 
2538                                         next++;
2539                                 }
2540                         }
2541                 }
2542                 tokp = next - 1;
2543
2544                 /* Save the integer/floating point value */
2545                 save_string(state, tk, token, tokp, "literal number");
2546         }
2547         /* identifiers */
2548         else if (letterp(c)) {
2549                 tok = TOK_IDENT;
2550                 for(tokp += 1; tokp < end; tokp++) {
2551                         c = *tokp;
2552                         if (!letterp(c) && !digitp(c)) {
2553                                 break;
2554                         }
2555                 }
2556                 tokp -= 1;
2557                 tk->ident = lookup(state, token, tokp +1 - token);
2558         }
2559         /* C99 alternate macro characters */
2560         else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) { 
2561                 tokp += 3; 
2562                 tok = TOK_CONCATENATE; 
2563         }
2564         else if ((c == '.') && (c1 == '.') && (c2 == '.')) { tokp += 2; tok = TOK_DOTS; }
2565         else if ((c == '<') && (c1 == '<') && (c2 == '=')) { tokp += 2; tok = TOK_SLEQ; }
2566         else if ((c == '>') && (c1 == '>') && (c2 == '=')) { tokp += 2; tok = TOK_SREQ; }
2567         else if ((c == '*') && (c1 == '=')) { tokp += 1; tok = TOK_TIMESEQ; }
2568         else if ((c == '/') && (c1 == '=')) { tokp += 1; tok = TOK_DIVEQ; }
2569         else if ((c == '%') && (c1 == '=')) { tokp += 1; tok = TOK_MODEQ; }
2570         else if ((c == '+') && (c1 == '=')) { tokp += 1; tok = TOK_PLUSEQ; }
2571         else if ((c == '-') && (c1 == '=')) { tokp += 1; tok = TOK_MINUSEQ; }
2572         else if ((c == '&') && (c1 == '=')) { tokp += 1; tok = TOK_ANDEQ; }
2573         else if ((c == '^') && (c1 == '=')) { tokp += 1; tok = TOK_XOREQ; }
2574         else if ((c == '|') && (c1 == '=')) { tokp += 1; tok = TOK_OREQ; }
2575         else if ((c == '=') && (c1 == '=')) { tokp += 1; tok = TOK_EQEQ; }
2576         else if ((c == '!') && (c1 == '=')) { tokp += 1; tok = TOK_NOTEQ; }
2577         else if ((c == '|') && (c1 == '|')) { tokp += 1; tok = TOK_LOGOR; }
2578         else if ((c == '&') && (c1 == '&')) { tokp += 1; tok = TOK_LOGAND; }
2579         else if ((c == '<') && (c1 == '=')) { tokp += 1; tok = TOK_LESSEQ; }
2580         else if ((c == '>') && (c1 == '=')) { tokp += 1; tok = TOK_MOREEQ; }
2581         else if ((c == '<') && (c1 == '<')) { tokp += 1; tok = TOK_SL; }
2582         else if ((c == '>') && (c1 == '>')) { tokp += 1; tok = TOK_SR; }
2583         else if ((c == '+') && (c1 == '+')) { tokp += 1; tok = TOK_PLUSPLUS; }
2584         else if ((c == '-') && (c1 == '-')) { tokp += 1; tok = TOK_MINUSMINUS; }
2585         else if ((c == '-') && (c1 == '>')) { tokp += 1; tok = TOK_ARROW; }
2586         else if ((c == '<') && (c1 == ':')) { tokp += 1; tok = TOK_LBRACKET; }
2587         else if ((c == ':') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACKET; }
2588         else if ((c == '<') && (c1 == '%')) { tokp += 1; tok = TOK_LBRACE; }
2589         else if ((c == '%') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACE; }
2590         else if ((c == '%') && (c1 == ':')) { tokp += 1; tok = TOK_MACRO; }
2591         else if ((c == '#') && (c1 == '#')) { tokp += 1; tok = TOK_CONCATENATE; }
2592         else if (c == ';') { tok = TOK_SEMI; }
2593         else if (c == '{') { tok = TOK_LBRACE; }
2594         else if (c == '}') { tok = TOK_RBRACE; }
2595         else if (c == ',') { tok = TOK_COMMA; }
2596         else if (c == '=') { tok = TOK_EQ; }
2597         else if (c == ':') { tok = TOK_COLON; }
2598         else if (c == '[') { tok = TOK_LBRACKET; }
2599         else if (c == ']') { tok = TOK_RBRACKET; }
2600         else if (c == '(') { tok = TOK_LPAREN; }
2601         else if (c == ')') { tok = TOK_RPAREN; }
2602         else if (c == '*') { tok = TOK_STAR; }
2603         else if (c == '>') { tok = TOK_MORE; }
2604         else if (c == '<') { tok = TOK_LESS; }
2605         else if (c == '?') { tok = TOK_QUEST; }
2606         else if (c == '|') { tok = TOK_OR; }
2607         else if (c == '&') { tok = TOK_AND; }
2608         else if (c == '^') { tok = TOK_XOR; }
2609         else if (c == '+') { tok = TOK_PLUS; }
2610         else if (c == '-') { tok = TOK_MINUS; }
2611         else if (c == '/') { tok = TOK_DIV; }
2612         else if (c == '%') { tok = TOK_MOD; }
2613         else if (c == '!') { tok = TOK_BANG; }
2614         else if (c == '.') { tok = TOK_DOT; }
2615         else if (c == '~') { tok = TOK_TILDE; }
2616         else if (c == '#') { tok = TOK_MACRO; }
2617         if (tok == TOK_MACRO) {
2618                 /* Only match preprocessor directives at the start of a line */
2619                 char *ptr;
2620                 for(ptr = file->line_start; spacep(*ptr); ptr++)
2621                         ;
2622                 if (ptr != tokp) {
2623                         tok = TOK_UNKNOWN;
2624                 }
2625         }
2626         if (tok == TOK_UNKNOWN) {
2627                 error(state, 0, "unknown token");
2628         }
2629
2630         file->pos = tokp + 1;
2631         tk->tok = tok;
2632         if (tok == TOK_IDENT) {
2633                 ident_to_keyword(state, tk);
2634         }
2635         /* Don't return space tokens. */
2636         if (tok == TOK_SPACE) {
2637                 goto next_token;
2638         }
2639 }
2640
2641 static void compile_macro(struct compile_state *state, struct token *tk)
2642 {
2643         struct file_state *file;
2644         struct hash_entry *ident;
2645         ident = tk->ident;
2646         file = xmalloc(sizeof(*file), "file_state");
2647         file->basename = xstrdup(tk->ident->name);
2648         file->dirname = xstrdup("");
2649         file->size = ident->sym_define->buf_len;
2650         file->buf = xmalloc(file->size +2,  file->basename);
2651         memcpy(file->buf, ident->sym_define->buf, file->size);
2652         file->buf[file->size] = '\n';
2653         file->buf[file->size + 1] = '\0';
2654         file->pos = file->buf;
2655         file->line_start = file->pos;
2656         file->line = 1;
2657         file->prev = state->file;
2658         state->file = file;
2659 }
2660
2661
2662 static int mpeek(struct compile_state *state, int index)
2663 {
2664         struct token *tk;
2665         int rescan;
2666         tk = &state->token[index + 1];
2667         if (tk->tok == -1) {
2668                 next_token(state, index + 1);
2669         }
2670         do {
2671                 rescan = 0;
2672                 if ((tk->tok == TOK_EOF) && 
2673                         (state->file != state->macro_file) &&
2674                         (state->file->prev)) {
2675                         struct file_state *file = state->file;
2676                         state->file = file->prev;
2677                         /* file->basename is used keep it */
2678                         xfree(file->dirname);
2679                         xfree(file->buf);
2680                         xfree(file);
2681                         next_token(state, index + 1);
2682                         rescan = 1;
2683                 }
2684                 else if (tk->ident && tk->ident->sym_define) {
2685                         compile_macro(state, tk);
2686                         next_token(state, index + 1);
2687                         rescan = 1;
2688                 }
2689         } while(rescan);
2690         /* Don't show the token on the next line */
2691         if (state->macro_line < state->macro_file->line) {
2692                 return TOK_EOF;
2693         }
2694         return state->token[index +1].tok;
2695 }
2696
2697 static void meat(struct compile_state *state, int index, int tok)
2698 {
2699         int next_tok;
2700         int i;
2701         next_tok = mpeek(state, index);
2702         if (next_tok != tok) {
2703                 const char *name1, *name2;
2704                 name1 = tokens[next_tok];
2705                 name2 = "";
2706                 if (next_tok == TOK_IDENT) {
2707                         name2 = state->token[index + 1].ident->name;
2708                 }
2709                 error(state, 0, "found %s %s expected %s", 
2710                         name1, name2, tokens[tok]);
2711         }
2712         /* Free the old token value */
2713         if (state->token[index].str_len) {
2714                 memset((void *)(state->token[index].val.str), -1, 
2715                         state->token[index].str_len);
2716                 xfree(state->token[index].val.str);
2717         }
2718         for(i = index; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
2719                 state->token[i] = state->token[i + 1];
2720         }
2721         memset(&state->token[i], 0, sizeof(state->token[i]));
2722         state->token[i].tok = -1;
2723 }
2724
2725 static long_t mcexpr(struct compile_state *state, int index);
2726
2727 static long_t mprimary_expr(struct compile_state *state, int index)
2728 {
2729         long_t val;
2730         int tok;
2731         tok = mpeek(state, index);
2732         while(state->token[index + 1].ident && 
2733                 state->token[index + 1].ident->sym_define) {
2734                 meat(state, index, tok);
2735                 compile_macro(state, &state->token[index]);
2736                 tok = mpeek(state, index);
2737         }
2738         switch(tok) {
2739         case TOK_LPAREN:
2740                 meat(state, index, TOK_LPAREN);
2741                 val = mcexpr(state, index);
2742                 meat(state, index, TOK_RPAREN);
2743                 break;
2744         case TOK_LIT_INT:
2745         {
2746                 char *end;
2747                 meat(state, index, TOK_LIT_INT);
2748                 errno = 0;
2749                 val = strtol(state->token[index].val.str, &end, 0);
2750                 if (((val == LONG_MIN) || (val == LONG_MAX)) &&
2751                         (errno == ERANGE)) {
2752                         error(state, 0, "Integer constant to large");
2753                 }
2754                 break;
2755         }
2756         default:
2757                 meat(state, index, TOK_LIT_INT);
2758                 val = 0;
2759         }
2760         return val;
2761 }
2762 static long_t munary_expr(struct compile_state *state, int index)
2763 {
2764         long_t val;
2765         switch(mpeek(state, index)) {
2766         case TOK_PLUS:
2767                 meat(state, index, TOK_PLUS);
2768                 val = munary_expr(state, index);
2769                 val = + val;
2770                 break;
2771         case TOK_MINUS:
2772                 meat(state, index, TOK_MINUS);
2773                 val = munary_expr(state, index);
2774                 val = - val;
2775                 break;
2776         case TOK_TILDE:
2777                 meat(state, index, TOK_BANG);
2778                 val = munary_expr(state, index);
2779                 val = ~ val;
2780                 break;
2781         case TOK_BANG:
2782                 meat(state, index, TOK_BANG);
2783                 val = munary_expr(state, index);
2784                 val = ! val;
2785                 break;
2786         default:
2787                 val = mprimary_expr(state, index);
2788                 break;
2789         }
2790         return val;
2791         
2792 }
2793 static long_t mmul_expr(struct compile_state *state, int index)
2794 {
2795         long_t val;
2796         int done;
2797         val = munary_expr(state, index);
2798         do {
2799                 long_t right;
2800                 done = 0;
2801                 switch(mpeek(state, index)) {
2802                 case TOK_STAR:
2803                         meat(state, index, TOK_STAR);
2804                         right = munary_expr(state, index);
2805                         val = val * right;
2806                         break;
2807                 case TOK_DIV:
2808                         meat(state, index, TOK_DIV);
2809                         right = munary_expr(state, index);
2810                         val = val / right;
2811                         break;
2812                 case TOK_MOD:
2813                         meat(state, index, TOK_MOD);
2814                         right = munary_expr(state, index);
2815                         val = val % right;
2816                         break;
2817                 default:
2818                         done = 1;
2819                         break;
2820                 }
2821         } while(!done);
2822
2823         return val;
2824 }
2825
2826 static long_t madd_expr(struct compile_state *state, int index)
2827 {
2828         long_t val;
2829         int done;
2830         val = mmul_expr(state, index);
2831         do {
2832                 long_t right;
2833                 done = 0;
2834                 switch(mpeek(state, index)) {
2835                 case TOK_PLUS:
2836                         meat(state, index, TOK_PLUS);
2837                         right = mmul_expr(state, index);
2838                         val = val + right;
2839                         break;
2840                 case TOK_MINUS:
2841                         meat(state, index, TOK_MINUS);
2842                         right = mmul_expr(state, index);
2843                         val = val - right;
2844                         break;
2845                 default:
2846                         done = 1;
2847                         break;
2848                 }
2849         } while(!done);
2850
2851         return val;
2852 }
2853
2854 static long_t mshift_expr(struct compile_state *state, int index)
2855 {
2856         long_t val;
2857         int done;
2858         val = madd_expr(state, index);
2859         do {
2860                 long_t right;
2861                 done = 0;
2862                 switch(mpeek(state, index)) {
2863                 case TOK_SL:
2864                         meat(state, index, TOK_SL);
2865                         right = madd_expr(state, index);
2866                         val = val << right;
2867                         break;
2868                 case TOK_SR:
2869                         meat(state, index, TOK_SR);
2870                         right = madd_expr(state, index);
2871                         val = val >> right;
2872                         break;
2873                 default:
2874                         done = 1;
2875                         break;
2876                 }
2877         } while(!done);
2878
2879         return val;
2880 }
2881
2882 static long_t mrel_expr(struct compile_state *state, int index)
2883 {
2884         long_t val;
2885         int done;
2886         val = mshift_expr(state, index);
2887         do {
2888                 long_t right;
2889                 done = 0;
2890                 switch(mpeek(state, index)) {
2891                 case TOK_LESS:
2892                         meat(state, index, TOK_LESS);
2893                         right = mshift_expr(state, index);
2894                         val = val < right;
2895                         break;
2896                 case TOK_MORE:
2897                         meat(state, index, TOK_MORE);
2898                         right = mshift_expr(state, index);
2899                         val = val > right;
2900                         break;
2901                 case TOK_LESSEQ:
2902                         meat(state, index, TOK_LESSEQ);
2903                         right = mshift_expr(state, index);
2904                         val = val <= right;
2905                         break;
2906                 case TOK_MOREEQ:
2907                         meat(state, index, TOK_MOREEQ);
2908                         right = mshift_expr(state, index);
2909                         val = val >= right;
2910                         break;
2911                 default:
2912                         done = 1;
2913                         break;
2914                 }
2915         } while(!done);
2916         return val;
2917 }
2918
2919 static long_t meq_expr(struct compile_state *state, int index)
2920 {
2921         long_t val;
2922         int done;
2923         val = mrel_expr(state, index);
2924         do {
2925                 long_t right;
2926                 done = 0;
2927                 switch(mpeek(state, index)) {
2928                 case TOK_EQEQ:
2929                         meat(state, index, TOK_EQEQ);
2930                         right = mrel_expr(state, index);
2931                         val = val == right;
2932                         break;
2933                 case TOK_NOTEQ:
2934                         meat(state, index, TOK_NOTEQ);
2935                         right = mrel_expr(state, index);
2936                         val = val != right;
2937                         break;
2938                 default:
2939                         done = 1;
2940                         break;
2941                 }
2942         } while(!done);
2943         return val;
2944 }
2945
2946 static long_t mand_expr(struct compile_state *state, int index)
2947 {
2948         long_t val;
2949         val = meq_expr(state, index);
2950         if (mpeek(state, index) == TOK_AND) {
2951                 long_t right;
2952                 meat(state, index, TOK_AND);
2953                 right = meq_expr(state, index);
2954                 val = val & right;
2955         }
2956         return val;
2957 }
2958
2959 static long_t mxor_expr(struct compile_state *state, int index)
2960 {
2961         long_t val;
2962         val = mand_expr(state, index);
2963         if (mpeek(state, index) == TOK_XOR) {
2964                 long_t right;
2965                 meat(state, index, TOK_XOR);
2966                 right = mand_expr(state, index);
2967                 val = val ^ right;
2968         }
2969         return val;
2970 }
2971
2972 static long_t mor_expr(struct compile_state *state, int index)
2973 {
2974         long_t val;
2975         val = mxor_expr(state, index);
2976         if (mpeek(state, index) == TOK_OR) {
2977                 long_t right;
2978                 meat(state, index, TOK_OR);
2979                 right = mxor_expr(state, index);
2980                 val = val | right;
2981         }
2982         return val;
2983 }
2984
2985 static long_t mland_expr(struct compile_state *state, int index)
2986 {
2987         long_t val;
2988         val = mor_expr(state, index);
2989         if (mpeek(state, index) == TOK_LOGAND) {
2990                 long_t right;
2991                 meat(state, index, TOK_LOGAND);
2992                 right = mor_expr(state, index);
2993                 val = val && right;
2994         }
2995         return val;
2996 }
2997 static long_t mlor_expr(struct compile_state *state, int index)
2998 {
2999         long_t val;
3000         val = mland_expr(state, index);
3001         if (mpeek(state, index) == TOK_LOGOR) {
3002                 long_t right;
3003                 meat(state, index, TOK_LOGOR);
3004                 right = mland_expr(state, index);
3005                 val = val || right;
3006         }
3007         return val;
3008 }
3009
3010 static long_t mcexpr(struct compile_state *state, int index)
3011 {
3012         return mlor_expr(state, index);
3013 }
3014 static void preprocess(struct compile_state *state, int index)
3015 {
3016         /* Doing much more with the preprocessor would require
3017          * a parser and a major restructuring.
3018          * Postpone that for later.
3019          */
3020         struct file_state *file;
3021         struct token *tk;
3022         int line;
3023         int tok;
3024         
3025         file = state->file;
3026         tk = &state->token[index];
3027         state->macro_line = line = file->line;
3028         state->macro_file = file;
3029
3030         next_token(state, index);
3031         ident_to_macro(state, tk);
3032         if (tk->tok == TOK_IDENT) {
3033                 error(state, 0, "undefined preprocessing directive `%s'",
3034                         tk->ident->name);
3035         }
3036         switch(tk->tok) {
3037         case TOK_UNDEF:
3038         case TOK_LINE:
3039         case TOK_PRAGMA:
3040                 if (state->if_value < 0) {
3041                         break;
3042                 }
3043                 warning(state, 0, "Ignoring preprocessor directive: %s", 
3044                         tk->ident->name);
3045                 break;
3046         case TOK_ELIF:
3047                 error(state, 0, "#elif not supported");
3048 #warning "FIXME multiple #elif and #else in an #if do not work properly"
3049                 if (state->if_depth == 0) {
3050                         error(state, 0, "#elif without #if");
3051                 }
3052                 /* If the #if was taken the #elif just disables the following code */
3053                 if (state->if_value >= 0) {
3054                         state->if_value = - state->if_value;
3055                 }
3056                 /* If the previous #if was not taken see if the #elif enables the 
3057                  * trailing code.
3058                  */
3059                 else if ((state->if_value < 0) && 
3060                         (state->if_depth == - state->if_value))
3061                 {
3062                         if (mcexpr(state, index) != 0) {
3063                                 state->if_value = state->if_depth;
3064                         }
3065                         else {
3066                                 state->if_value = - state->if_depth;
3067                         }
3068                 }
3069                 break;
3070         case TOK_IF:
3071                 state->if_depth++;
3072                 if (state->if_value < 0) {
3073                         break;
3074                 }
3075                 if (mcexpr(state, index) != 0) {
3076                         state->if_value = state->if_depth;
3077                 }
3078                 else {
3079                         state->if_value = - state->if_depth;
3080                 }
3081                 break;
3082         case TOK_IFNDEF:
3083                 state->if_depth++;
3084                 if (state->if_value < 0) {
3085                         break;
3086                 }
3087                 next_token(state, index);
3088                 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3089                         error(state, 0, "Invalid macro name");
3090                 }
3091                 if (tk->ident->sym_define == 0) {
3092                         state->if_value = state->if_depth;
3093                 } 
3094                 else {
3095                         state->if_value = - state->if_depth;
3096                 }
3097                 break;
3098         case TOK_IFDEF:
3099                 state->if_depth++;
3100                 if (state->if_value < 0) {
3101                         break;
3102                 }
3103                 next_token(state, index);
3104                 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3105                         error(state, 0, "Invalid macro name");
3106                 }
3107                 if (tk->ident->sym_define != 0) {
3108                         state->if_value = state->if_depth;
3109                 }
3110                 else {
3111                         state->if_value = - state->if_depth;
3112                 }
3113                 break;
3114         case TOK_ELSE:
3115                 if (state->if_depth == 0) {
3116                         error(state, 0, "#else without #if");
3117                 }
3118                 if ((state->if_value >= 0) ||
3119                         ((state->if_value < 0) && 
3120                                 (state->if_depth == -state->if_value)))
3121                 {
3122                         state->if_value = - state->if_value;
3123                 }
3124                 break;
3125         case TOK_ENDIF:
3126                 if (state->if_depth == 0) {
3127                         error(state, 0, "#endif without #if");
3128                 }
3129                 if ((state->if_value >= 0) ||
3130                         ((state->if_value < 0) &&
3131                                 (state->if_depth == -state->if_value))) 
3132                 {
3133                         state->if_value = state->if_depth - 1;
3134                 }
3135                 state->if_depth--;
3136                 break;
3137         case TOK_DEFINE:
3138         {
3139                 struct hash_entry *ident;
3140                 struct macro *macro;
3141                 char *ptr;
3142                 
3143                 if (state->if_value < 0) /* quit early when #if'd out */
3144                         break;
3145
3146                 meat(state, index, TOK_IDENT);
3147                 ident = tk->ident;
3148                 
3149
3150                 if (*file->pos == '(') {
3151 #warning "FIXME macros with arguments not supported"
3152                         error(state, 0, "Macros with arguments not supported");
3153                 }
3154
3155                 /* Find the end of the line to get an estimate of
3156                  * the macro's length.
3157                  */
3158                 for(ptr = file->pos; *ptr != '\n'; ptr++)  
3159                         ;
3160
3161                 if (ident->sym_define != 0) {
3162                         error(state, 0, "macro %s already defined\n", ident->name);
3163                 }
3164                 macro = xmalloc(sizeof(*macro), "macro");
3165                 macro->ident = ident;
3166                 macro->buf_len = ptr - file->pos +1;
3167                 macro->buf = xmalloc(macro->buf_len +2, "macro buf");
3168
3169                 memcpy(macro->buf, file->pos, macro->buf_len);
3170                 macro->buf[macro->buf_len] = '\n';
3171                 macro->buf[macro->buf_len +1] = '\0';
3172
3173                 ident->sym_define = macro;
3174                 break;
3175         }
3176         case TOK_ERROR:
3177         {
3178                 char *end;
3179                 int len;
3180                 /* Find the end of the line */
3181                 for(end = file->pos; *end != '\n'; end++)
3182                         ;
3183                 len = (end - file->pos);
3184                 if (state->if_value >= 0) {
3185                         error(state, 0, "%*.*s", len, len, file->pos);
3186                 }
3187                 file->pos = end;
3188                 break;
3189         }
3190         case TOK_WARNING:
3191         {
3192                 char *end;
3193                 int len;
3194                 /* Find the end of the line */
3195                 for(end = file->pos; *end != '\n'; end++)
3196                         ;
3197                 len = (end - file->pos);
3198                 if (state->if_value >= 0) {
3199                         warning(state, 0, "%*.*s", len, len, file->pos);
3200                 }
3201                 file->pos = end;
3202                 break;
3203         }
3204         case TOK_INCLUDE:
3205         {
3206                 char *name;
3207                 char *ptr;
3208                 int local;
3209                 local = 0;
3210                 name = 0;
3211                 next_token(state, index);
3212                 if (tk->tok == TOK_LIT_STRING) {
3213                         const char *token;
3214                         int name_len;
3215                         name = xmalloc(tk->str_len, "include");
3216                         token = tk->val.str +1;
3217                         name_len = tk->str_len -2;
3218                         if (*token == '"') {
3219                                 token++;
3220                                 name_len--;
3221                         }
3222                         memcpy(name, token, name_len);
3223                         name[name_len] = '\0';
3224                         local = 1;
3225                 }
3226                 else if (tk->tok == TOK_LESS) {
3227                         char *start, *end;
3228                         start = file->pos;
3229                         for(end = start; *end != '\n'; end++) {
3230                                 if (*end == '>') {
3231                                         break;
3232                                 }
3233                         }
3234                         if (*end == '\n') {
3235                                 error(state, 0, "Unterminated included directive");
3236                         }
3237                         name = xmalloc(end - start + 1, "include");
3238                         memcpy(name, start, end - start);
3239                         name[end - start] = '\0';
3240                         file->pos = end +1;
3241                         local = 0;
3242                 }
3243                 else {
3244                         error(state, 0, "Invalid include directive");
3245                 }
3246                 /* Error if there are any characters after the include */
3247                 for(ptr = file->pos; *ptr != '\n'; ptr++) {
3248                         if (!isspace(*ptr)) {
3249                                 error(state, 0, "garbage after include directive");
3250                         }
3251                 }
3252                 if (state->if_value >= 0) {
3253                         compile_file(state, name, local);
3254                 }
3255                 xfree(name);
3256                 next_token(state, index);
3257                 return;
3258         }
3259         default:
3260                 /* Ignore # without a following ident */
3261                 if (tk->tok == TOK_IDENT) {
3262                         error(state, 0, "Invalid preprocessor directive: %s", 
3263                                 tk->ident->name);
3264                 }
3265                 break;
3266         }
3267         /* Consume the rest of the macro line */
3268         do {
3269                 tok = mpeek(state, index);
3270                 meat(state, index, tok);
3271         } while(tok != TOK_EOF);
3272         return;
3273 }
3274
3275 static void token(struct compile_state *state, int index)
3276 {
3277         struct file_state *file;
3278         struct token *tk;
3279         int rescan;
3280
3281         tk = &state->token[index];
3282         next_token(state, index);
3283         do {
3284                 rescan = 0;
3285                 file = state->file;
3286                 if (tk->tok == TOK_EOF && file->prev) {
3287                         state->file = file->prev;
3288                         /* file->basename is used keep it */
3289                         xfree(file->dirname);
3290                         xfree(file->buf);
3291                         xfree(file);
3292                         next_token(state, index);
3293                         rescan = 1;
3294                 }
3295                 else if (tk->tok == TOK_MACRO) {
3296                         preprocess(state, index);
3297                         rescan = 1;
3298                 }
3299                 else if (tk->ident && tk->ident->sym_define) {
3300                         compile_macro(state, tk);
3301                         next_token(state, index);
3302                         rescan = 1;
3303                 }
3304                 else if (state->if_value < 0) {
3305                         next_token(state, index);
3306                         rescan = 1;
3307                 }
3308         } while(rescan);
3309 }
3310
3311 static int peek(struct compile_state *state)
3312 {
3313         if (state->token[1].tok == -1) {
3314                 token(state, 1);
3315         }
3316         return state->token[1].tok;
3317 }
3318
3319 static int peek2(struct compile_state *state)
3320 {
3321         if (state->token[1].tok == -1) {
3322                 token(state, 1);
3323         }
3324         if (state->token[2].tok == -1) {
3325                 token(state, 2);
3326         }
3327         return state->token[2].tok;
3328 }
3329
3330 static void eat(struct compile_state *state, int tok)
3331 {
3332         int next_tok;
3333         int i;
3334         next_tok = peek(state);
3335         if (next_tok != tok) {
3336                 const char *name1, *name2;
3337                 name1 = tokens[next_tok];
3338                 name2 = "";
3339                 if (next_tok == TOK_IDENT) {
3340                         name2 = state->token[1].ident->name;
3341                 }
3342                 error(state, 0, "\tfound %s %s expected %s",
3343                         name1, name2 ,tokens[tok]);
3344         }
3345         /* Free the old token value */
3346         if (state->token[0].str_len) {
3347                 xfree((void *)(state->token[0].val.str));
3348         }
3349         for(i = 0; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
3350                 state->token[i] = state->token[i + 1];
3351         }
3352         memset(&state->token[i], 0, sizeof(state->token[i]));
3353         state->token[i].tok = -1;
3354 }
3355
3356 #warning "FIXME do not hardcode the include paths"
3357 static char *include_paths[] = {
3358         "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/include",
3359         "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/arch/i386/include",
3360         "/home/eric/projects/linuxbios/checkin/solo/freebios2/src",
3361         0
3362 };
3363
3364 static void compile_file(struct compile_state *state, const char *filename, int local)
3365 {
3366         char cwd[4096];
3367         const char *subdir, *base;
3368         int subdir_len;
3369         struct file_state *file;
3370         char *basename;
3371         file = xmalloc(sizeof(*file), "file_state");
3372
3373         base = strrchr(filename, '/');
3374         subdir = filename;
3375         if (base != 0) {
3376                 subdir_len = base - filename;
3377                 base++;
3378         }
3379         else {
3380                 base = filename;
3381                 subdir_len = 0;
3382         }
3383         basename = xmalloc(strlen(base) +1, "basename");
3384         strcpy(basename, base);
3385         file->basename = basename;
3386
3387         if (getcwd(cwd, sizeof(cwd)) == 0) {
3388                 die("cwd buffer to small");
3389         }
3390         
3391         if (subdir[0] == '/') {
3392                 file->dirname = xmalloc(subdir_len + 1, "dirname");
3393                 memcpy(file->dirname, subdir, subdir_len);
3394                 file->dirname[subdir_len] = '\0';
3395         }
3396         else {
3397                 char *dir;
3398                 int dirlen;
3399                 char **path;
3400                 /* Find the appropriate directory... */
3401                 dir = 0;
3402                 if (!state->file && exists(cwd, filename)) {
3403                         dir = cwd;
3404                 }
3405                 if (local && state->file && exists(state->file->dirname, filename)) {
3406                         dir = state->file->dirname;
3407                 }
3408                 for(path = include_paths; !dir && *path; path++) {
3409                         if (exists(*path, filename)) {
3410                                 dir = *path;
3411                         }
3412                 }
3413                 if (!dir) {
3414                         error(state, 0, "Cannot find `%s'\n", filename);
3415                 }
3416                 dirlen = strlen(dir);
3417                 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
3418                 memcpy(file->dirname, dir, dirlen);
3419                 file->dirname[dirlen] = '/';
3420                 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
3421                 file->dirname[dirlen + 1 + subdir_len] = '\0';
3422         }
3423         file->buf = slurp_file(file->dirname, file->basename, &file->size);
3424         xchdir(cwd);
3425
3426         file->pos = file->buf;
3427         file->line_start = file->pos;
3428         file->line = 1;
3429
3430         file->prev = state->file;
3431         state->file = file;
3432         
3433         process_trigraphs(state);
3434         splice_lines(state);
3435 }
3436
3437 /* Type helper functions */
3438
3439 static struct type *new_type(
3440         unsigned int type, struct type *left, struct type *right)
3441 {
3442         struct type *result;
3443         result = xmalloc(sizeof(*result), "type");
3444         result->type = type;
3445         result->left = left;
3446         result->right = right;
3447         result->field_ident = 0;
3448         result->type_ident = 0;
3449         return result;
3450 }
3451
3452 static struct type *clone_type(unsigned int specifiers, struct type *old)
3453 {
3454         struct type *result;
3455         result = xmalloc(sizeof(*result), "type");
3456         memcpy(result, old, sizeof(*result));
3457         result->type &= TYPE_MASK;
3458         result->type |= specifiers;
3459         return result;
3460 }
3461
3462 #define SIZEOF_SHORT 2
3463 #define SIZEOF_INT   4
3464 #define SIZEOF_LONG  (sizeof(long_t))
3465
3466 #define ALIGNOF_SHORT 2
3467 #define ALIGNOF_INT   4
3468 #define ALIGNOF_LONG  (sizeof(long_t))
3469
3470 #define MASK_UCHAR(X)    ((X) & ((ulong_t)0xff))
3471 #define MASK_USHORT(X)   ((X) & (((ulong_t)1 << (SIZEOF_SHORT*8)) - 1))
3472 static inline ulong_t mask_uint(ulong_t x)
3473 {
3474         if (SIZEOF_INT < SIZEOF_LONG) {
3475                 ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT*8))) -1;
3476                 x &= mask;
3477         }
3478         return x;
3479 }
3480 #define MASK_UINT(X)      (mask_uint(X))
3481 #define MASK_ULONG(X)    (X)
3482
3483 static struct type void_type   = { .type  = TYPE_VOID };
3484 static struct type char_type   = { .type  = TYPE_CHAR };
3485 static struct type uchar_type  = { .type  = TYPE_UCHAR };
3486 static struct type short_type  = { .type  = TYPE_SHORT };
3487 static struct type ushort_type = { .type  = TYPE_USHORT };
3488 static struct type int_type    = { .type  = TYPE_INT };
3489 static struct type uint_type   = { .type  = TYPE_UINT };
3490 static struct type long_type   = { .type  = TYPE_LONG };
3491 static struct type ulong_type  = { .type  = TYPE_ULONG };
3492
3493 static struct triple *variable(struct compile_state *state, struct type *type)
3494 {
3495         struct triple *result;
3496         if ((type->type & STOR_MASK) != STOR_PERM) {
3497                 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3498                         result = triple(state, OP_ADECL, type, 0, 0);
3499                 } else {
3500                         struct type *field;
3501                         struct triple **vector;
3502                         ulong_t index;
3503                         result = new_triple(state, OP_VAL_VEC, type, -1, -1);
3504                         vector = &result->param[0];
3505
3506                         field = type->left;
3507                         index = 0;
3508                         while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
3509                                 vector[index] = variable(state, field->left);
3510                                 field = field->right;
3511                                 index++;
3512                         }
3513                         vector[index] = variable(state, field);
3514                 }
3515         }
3516         else {
3517                 result = triple(state, OP_SDECL, type, 0, 0);
3518         }
3519         return result;
3520 }
3521
3522 static void stor_of(FILE *fp, struct type *type)
3523 {
3524         switch(type->type & STOR_MASK) {
3525         case STOR_AUTO:
3526                 fprintf(fp, "auto ");
3527                 break;
3528         case STOR_STATIC:
3529                 fprintf(fp, "static ");
3530                 break;
3531         case STOR_EXTERN:
3532                 fprintf(fp, "extern ");
3533                 break;
3534         case STOR_REGISTER:
3535                 fprintf(fp, "register ");
3536                 break;
3537         case STOR_TYPEDEF:
3538                 fprintf(fp, "typedef ");
3539                 break;
3540         case STOR_INLINE:
3541                 fprintf(fp, "inline ");
3542                 break;
3543         }
3544 }
3545 static void qual_of(FILE *fp, struct type *type)
3546 {
3547         if (type->type & QUAL_CONST) {
3548                 fprintf(fp, " const");
3549         }
3550         if (type->type & QUAL_VOLATILE) {
3551                 fprintf(fp, " volatile");
3552         }
3553         if (type->type & QUAL_RESTRICT) {
3554                 fprintf(fp, " restrict");
3555         }
3556 }
3557
3558 static void name_of(FILE *fp, struct type *type)
3559 {
3560         stor_of(fp, type);
3561         switch(type->type & TYPE_MASK) {
3562         case TYPE_VOID:
3563                 fprintf(fp, "void");
3564                 qual_of(fp, type);
3565                 break;
3566         case TYPE_CHAR:
3567                 fprintf(fp, "signed char");
3568                 qual_of(fp, type);
3569                 break;
3570         case TYPE_UCHAR:
3571                 fprintf(fp, "unsigned char");
3572                 qual_of(fp, type);
3573                 break;
3574         case TYPE_SHORT:
3575                 fprintf(fp, "signed short");
3576                 qual_of(fp, type);
3577                 break;
3578         case TYPE_USHORT:
3579                 fprintf(fp, "unsigned short");
3580                 qual_of(fp, type);
3581                 break;
3582         case TYPE_INT:
3583                 fprintf(fp, "signed int");
3584                 qual_of(fp, type);
3585                 break;
3586         case TYPE_UINT:
3587                 fprintf(fp, "unsigned int");
3588                 qual_of(fp, type);
3589                 break;
3590         case TYPE_LONG:
3591                 fprintf(fp, "signed long");
3592                 qual_of(fp, type);
3593                 break;
3594         case TYPE_ULONG:
3595                 fprintf(fp, "unsigned long");
3596                 qual_of(fp, type);
3597                 break;
3598         case TYPE_POINTER:
3599                 name_of(fp, type->left);
3600                 fprintf(fp, " * ");
3601                 qual_of(fp, type);
3602                 break;
3603         case TYPE_PRODUCT:
3604         case TYPE_OVERLAP:
3605                 name_of(fp, type->left);
3606                 fprintf(fp, ", ");
3607                 name_of(fp, type->right);
3608                 break;
3609         case TYPE_ENUM:
3610                 fprintf(fp, "enum %s", type->type_ident->name);
3611                 qual_of(fp, type);
3612                 break;
3613         case TYPE_STRUCT:
3614                 fprintf(fp, "struct %s", type->type_ident->name);
3615                 qual_of(fp, type);
3616                 break;
3617         case TYPE_FUNCTION:
3618         {
3619                 name_of(fp, type->left);
3620                 fprintf(fp, " (*)(");
3621                 name_of(fp, type->right);
3622                 fprintf(fp, ")");
3623                 break;
3624         }
3625         case TYPE_ARRAY:
3626                 name_of(fp, type->left);
3627                 fprintf(fp, " [%ld]", type->elements);
3628                 break;
3629         default:
3630                 fprintf(fp, "????: %x", type->type & TYPE_MASK);
3631                 break;
3632         }
3633 }
3634
3635 static size_t align_of(struct compile_state *state, struct type *type)
3636 {
3637         size_t align;
3638         align = 0;
3639         switch(type->type & TYPE_MASK) {
3640         case TYPE_VOID:
3641                 align = 1;
3642                 break;
3643         case TYPE_CHAR:
3644         case TYPE_UCHAR:
3645                 align = 1;
3646                 break;
3647         case TYPE_SHORT:
3648         case TYPE_USHORT:
3649                 align = ALIGNOF_SHORT;
3650                 break;
3651         case TYPE_INT:
3652         case TYPE_UINT:
3653         case TYPE_ENUM:
3654                 align = ALIGNOF_INT;
3655                 break;
3656         case TYPE_LONG:
3657         case TYPE_ULONG:
3658         case TYPE_POINTER:
3659                 align = ALIGNOF_LONG;
3660                 break;
3661         case TYPE_PRODUCT:
3662         case TYPE_OVERLAP:
3663         {
3664                 size_t left_align, right_align;
3665                 left_align  = align_of(state, type->left);
3666                 right_align = align_of(state, type->right);
3667                 align = (left_align >= right_align) ? left_align : right_align;
3668                 break;
3669         }
3670         case TYPE_ARRAY:
3671                 align = align_of(state, type->left);
3672                 break;
3673         case TYPE_STRUCT:
3674                 align = align_of(state, type->left);
3675                 break;
3676         default:
3677                 error(state, 0, "alignof not yet defined for type\n");
3678                 break;
3679         }
3680         return align;
3681 }
3682
3683 static size_t size_of(struct compile_state *state, struct type *type)
3684 {
3685         size_t size;
3686         size = 0;
3687         switch(type->type & TYPE_MASK) {
3688         case TYPE_VOID:
3689                 size = 0;
3690                 break;
3691         case TYPE_CHAR:
3692         case TYPE_UCHAR:
3693                 size = 1;
3694                 break;
3695         case TYPE_SHORT:
3696         case TYPE_USHORT:
3697                 size = SIZEOF_SHORT;
3698                 break;
3699         case TYPE_INT:
3700         case TYPE_UINT:
3701         case TYPE_ENUM:
3702                 size = SIZEOF_INT;
3703                 break;
3704         case TYPE_LONG:
3705         case TYPE_ULONG:
3706         case TYPE_POINTER:
3707                 size = SIZEOF_LONG;
3708                 break;
3709         case TYPE_PRODUCT:
3710         {
3711                 size_t align, pad;
3712                 size = size_of(state, type->left);
3713                 while((type->right->type & TYPE_MASK) == TYPE_PRODUCT) {
3714                         type = type->right;
3715                         align = align_of(state, type->left);
3716                         pad = align - (size % align);
3717                         size = size + pad + size_of(state, type->left);
3718                 }
3719                 align = align_of(state, type->right);
3720                 pad = align - (size % align);
3721                 size = size + pad + sizeof(type->right);
3722                 break;
3723         }
3724         case TYPE_OVERLAP:
3725         {
3726                 size_t size_left, size_right;
3727                 size_left = size_of(state, type->left);
3728                 size_right = size_of(state, type->right);
3729                 size = (size_left >= size_right)? size_left : size_right;
3730                 break;
3731         }
3732         case TYPE_ARRAY:
3733                 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
3734                         internal_error(state, 0, "Invalid array type");
3735                 } else {
3736                         size = size_of(state, type->left) * type->elements;
3737                 }
3738                 break;
3739         case TYPE_STRUCT:
3740                 size = size_of(state, type->left);
3741                 break;
3742         default:
3743                 error(state, 0, "sizeof not yet defined for type\n");
3744                 break;
3745         }
3746         return size;
3747 }
3748
3749 static size_t field_offset(struct compile_state *state, 
3750         struct type *type, struct hash_entry *field)
3751 {
3752         size_t size, align, pad;
3753         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3754                 internal_error(state, 0, "field_offset only works on structures");
3755         }
3756         size = 0;
3757         type = type->left;
3758         while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
3759                 if (type->left->field_ident == field) {
3760                         type = type->left;
3761                 }
3762                 size += size_of(state, type->left);
3763                 type = type->right;
3764                 align = align_of(state, type->left);
3765                 pad = align - (size % align);
3766                 size += pad;
3767         }
3768         if (type->field_ident != field) {
3769                 internal_error(state, 0, "field_offset: member %s not present",
3770                         field->name);
3771         }
3772         return size;
3773 }
3774
3775 static struct type *field_type(struct compile_state *state, 
3776         struct type *type, struct hash_entry *field)
3777 {
3778         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3779                 internal_error(state, 0, "field_type only works on structures");
3780         }
3781         type = type->left;
3782         while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
3783                 if (type->left->field_ident == field) {
3784                         type = type->left;
3785                         break;
3786                 }
3787                 type = type->right;
3788         }
3789         if (type->field_ident != field) {
3790                 internal_error(state, 0, "field_type: member %s not present", 
3791                         field->name);
3792         }
3793         return type;
3794 }
3795
3796 static struct triple *struct_field(struct compile_state *state,
3797         struct triple *decl, struct hash_entry *field)
3798 {
3799         struct triple **vector;
3800         struct type *type;
3801         ulong_t index;
3802         type = decl->type;
3803         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3804                 return decl;
3805         }
3806         if (decl->op != OP_VAL_VEC) {
3807                 internal_error(state, 0, "Invalid struct variable");
3808         }
3809         if (!field) {
3810                 internal_error(state, 0, "Missing structure field");
3811         }
3812         type = type->left;
3813         vector = &RHS(decl, 0);
3814         index = 0;
3815         while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
3816                 if (type->left->field_ident == field) {
3817                         type = type->left;
3818                         break;
3819                 }
3820                 index += 1;
3821                 type = type->right;
3822         }
3823         if (type->field_ident != field) {
3824                 internal_error(state, 0, "field %s not found?", field->name);
3825         }
3826         return vector[index];
3827 }
3828
3829 static void arrays_complete(struct compile_state *state, struct type *type)
3830 {
3831         if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
3832                 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
3833                         error(state, 0, "array size not specified");
3834                 }
3835                 arrays_complete(state, type->left);
3836         }
3837 }
3838
3839 static unsigned int do_integral_promotion(unsigned int type)
3840 {
3841         type &= TYPE_MASK;
3842         if (TYPE_INTEGER(type) && 
3843                 TYPE_RANK(type) < TYPE_RANK(TYPE_INT)) {
3844                 type = TYPE_INT;
3845         }
3846         return type;
3847 }
3848
3849 static unsigned int do_arithmetic_conversion(
3850         unsigned int left, unsigned int right)
3851 {
3852         left &= TYPE_MASK;
3853         right &= TYPE_MASK;
3854         if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
3855                 return TYPE_LDOUBLE;
3856         }
3857         else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
3858                 return TYPE_DOUBLE;
3859         }
3860         else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
3861                 return TYPE_FLOAT;
3862         }
3863         left = do_integral_promotion(left);
3864         right = do_integral_promotion(right);
3865         /* If both operands have the same size done */
3866         if (left == right) {
3867                 return left;
3868         }
3869         /* If both operands have the same signedness pick the larger */
3870         else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
3871                 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
3872         }
3873         /* If the signed type can hold everything use it */
3874         else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
3875                 return left;
3876         }
3877         else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
3878                 return right;
3879         }
3880         /* Convert to the unsigned type with the same rank as the signed type */
3881         else if (TYPE_SIGNED(left)) {
3882                 return TYPE_MKUNSIGNED(left);
3883         }
3884         else {
3885                 return TYPE_MKUNSIGNED(right);
3886         }
3887 }
3888
3889 /* see if two types are the same except for qualifiers */
3890 static int equiv_types(struct type *left, struct type *right)
3891 {
3892         unsigned int type;
3893         /* Error if the basic types do not match */
3894         if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
3895                 return 0;
3896         }
3897         type = left->type & TYPE_MASK;
3898         /* if the basic types match and it is an arithmetic type we are done */
3899         if (TYPE_ARITHMETIC(type)) {
3900                 return 1;
3901         }
3902         /* If it is a pointer type recurse and keep testing */
3903         if (type == TYPE_POINTER) {
3904                 return equiv_types(left->left, right->left);
3905         }
3906         else if (type == TYPE_ARRAY) {
3907                 return (left->elements == right->elements) &&
3908                         equiv_types(left->left, right->left);
3909         }
3910         /* test for struct/union equality */
3911         else if (type == TYPE_STRUCT) {
3912                 return left->type_ident == right->type_ident;
3913         }
3914         /* Test for equivalent functions */
3915         else if (type == TYPE_FUNCTION) {
3916                 return equiv_types(left->left, right->left) &&
3917                         equiv_types(left->right, right->right);
3918         }
3919         /* We only see TYPE_PRODUCT as part of function equivalence matching */
3920         else if (type == TYPE_PRODUCT) {
3921                 return equiv_types(left->left, right->left) &&
3922                         equiv_types(left->right, right->right);
3923         }
3924         /* We should see TYPE_OVERLAP */
3925         else {
3926                 return 0;
3927         }
3928 }
3929
3930 static int equiv_ptrs(struct type *left, struct type *right)
3931 {
3932         if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
3933                 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
3934                 return 0;
3935         }
3936         return equiv_types(left->left, right->left);
3937 }
3938
3939 static struct type *compatible_types(struct type *left, struct type *right)
3940 {
3941         struct type *result;
3942         unsigned int type, qual_type;
3943         /* Error if the basic types do not match */
3944         if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
3945                 return 0;
3946         }
3947         type = left->type & TYPE_MASK;
3948         qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
3949         result = 0;
3950         /* if the basic types match and it is an arithmetic type we are done */
3951         if (TYPE_ARITHMETIC(type)) {
3952                 result = new_type(qual_type, 0, 0);
3953         }
3954         /* If it is a pointer type recurse and keep testing */
3955         else if (type == TYPE_POINTER) {
3956                 result = compatible_types(left->left, right->left);
3957                 if (result) {
3958                         result = new_type(qual_type, result, 0);
3959                 }
3960         }
3961         /* test for struct/union equality */
3962         else if (type == TYPE_STRUCT) {
3963                 if (left->type_ident == right->type_ident) {
3964                         result = left;
3965                 }
3966         }
3967         /* Test for equivalent functions */
3968         else if (type == TYPE_FUNCTION) {
3969                 struct type *lf, *rf;
3970                 lf = compatible_types(left->left, right->left);
3971                 rf = compatible_types(left->right, right->right);
3972                 if (lf && rf) {
3973                         result = new_type(qual_type, lf, rf);
3974                 }
3975         }
3976         /* We only see TYPE_PRODUCT as part of function equivalence matching */
3977         else if (type == TYPE_PRODUCT) {
3978                 struct type *lf, *rf;
3979                 lf = compatible_types(left->left, right->left);
3980                 rf = compatible_types(left->right, right->right);
3981                 if (lf && rf) {
3982                         result = new_type(qual_type, lf, rf);
3983                 }
3984         }
3985         else {
3986                 /* Nothing else is compatible */
3987         }
3988         return result;
3989 }
3990
3991 static struct type *compatible_ptrs(struct type *left, struct type *right)
3992 {
3993         struct type *result;
3994         if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
3995                 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
3996                 return 0;
3997         }
3998         result = compatible_types(left->left, right->left);
3999         if (result) {
4000                 unsigned int qual_type;
4001                 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4002                 result = new_type(qual_type, result, 0);
4003         }
4004         return result;
4005         
4006 }
4007 static struct triple *integral_promotion(
4008         struct compile_state *state, struct triple *def)
4009 {
4010         struct type *type;
4011         type = def->type;
4012         /* As all operations are carried out in registers
4013          * the values are converted on load I just convert
4014          * logical type of the operand.
4015          */
4016         if (TYPE_INTEGER(type->type)) {
4017                 unsigned int int_type;
4018                 int_type = type->type & ~TYPE_MASK;
4019                 int_type |= do_integral_promotion(type->type);
4020                 if (int_type != type->type) {
4021                         def->type = new_type(int_type, 0, 0);
4022                 }
4023         }
4024         return def;
4025 }
4026
4027
4028 static void arithmetic(struct compile_state *state, struct triple *def)
4029 {
4030         if (!TYPE_ARITHMETIC(def->type->type)) {
4031                 error(state, 0, "arithmetic type expexted");
4032         }
4033 }
4034
4035 static void ptr_arithmetic(struct compile_state *state, struct triple *def)
4036 {
4037         if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
4038                 error(state, def, "pointer or arithmetic type expected");
4039         }
4040 }
4041
4042 static int is_integral(struct triple *ins)
4043 {
4044         return TYPE_INTEGER(ins->type->type);
4045 }
4046
4047 static void integral(struct compile_state *state, struct triple *def)
4048 {
4049         if (!is_integral(def)) {
4050                 error(state, 0, "integral type expected");
4051         }
4052 }
4053
4054
4055 static void bool(struct compile_state *state, struct triple *def)
4056 {
4057         if (!TYPE_ARITHMETIC(def->type->type) &&
4058                 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
4059                 error(state, 0, "arithmetic or pointer type expected");
4060         }
4061 }
4062
4063 static int is_signed(struct type *type)
4064 {
4065         return !!TYPE_SIGNED(type->type);
4066 }
4067
4068 /* Is this value located in a register otherwise it must be in memory */
4069 static int is_in_reg(struct compile_state *state, struct triple *def)
4070 {
4071         int in_reg;
4072         if (def->op == OP_ADECL) {
4073                 in_reg = 1;
4074         }
4075         else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
4076                 in_reg = 0;
4077         }
4078         else if (def->op == OP_VAL_VEC) {
4079                 in_reg = is_in_reg(state, RHS(def, 0));
4080         }
4081         else if (def->op == OP_DOT) {
4082                 in_reg = is_in_reg(state, RHS(def, 0));
4083         }
4084         else {
4085                 internal_error(state, 0, "unknown expr storage location");
4086                 in_reg = -1;
4087         }
4088         return in_reg;
4089 }
4090
4091 /* Is this a stable variable location otherwise it must be a temporary */
4092 static int is_stable(struct compile_state *state, struct triple *def)
4093 {
4094         int ret;
4095         ret = 0;
4096         if (!def) {
4097                 return 0;
4098         }
4099         if ((def->op == OP_ADECL) || 
4100                 (def->op == OP_SDECL) || 
4101                 (def->op == OP_DEREF) ||
4102                 (def->op == OP_BLOBCONST)) {
4103                 ret = 1;
4104         }
4105         else if (def->op == OP_DOT) {
4106                 ret = is_stable(state, RHS(def, 0));
4107         }
4108         else if (def->op == OP_VAL_VEC) {
4109                 struct triple **vector;
4110                 ulong_t i;
4111                 ret = 1;
4112                 vector = &RHS(def, 0);
4113                 for(i = 0; i < def->type->elements; i++) {
4114                         if (!is_stable(state, vector[i])) {
4115                                 ret = 0;
4116                                 break;
4117                         }
4118                 }
4119         }
4120         return ret;
4121 }
4122
4123 static int is_lvalue(struct compile_state *state, struct triple *def)
4124 {
4125         int ret;
4126         ret = 1;
4127         if (!def) {
4128                 return 0;
4129         }
4130         if (!is_stable(state, def)) {
4131                 return 0;
4132         }
4133         if (def->type->type & QUAL_CONST) {
4134                 ret = 0;
4135         }
4136         else if (def->op == OP_DOT) {
4137                 ret = is_lvalue(state, RHS(def, 0));
4138         }
4139         return ret;
4140 }
4141
4142 static void lvalue(struct compile_state *state, struct triple *def)
4143 {
4144         if (!def) {
4145                 internal_error(state, def, "nothing where lvalue expected?");
4146         }
4147         if (!is_lvalue(state, def)) { 
4148                 error(state, def, "lvalue expected");
4149         }
4150 }
4151
4152 static int is_pointer(struct triple *def)
4153 {
4154         return (def->type->type & TYPE_MASK) == TYPE_POINTER;
4155 }
4156
4157 static void pointer(struct compile_state *state, struct triple *def)
4158 {
4159         if (!is_pointer(def)) {
4160                 error(state, def, "pointer expected");
4161         }
4162 }
4163
4164 static struct triple *int_const(
4165         struct compile_state *state, struct type *type, ulong_t value)
4166 {
4167         struct triple *result;
4168         switch(type->type & TYPE_MASK) {
4169         case TYPE_CHAR:
4170         case TYPE_INT:   case TYPE_UINT:
4171         case TYPE_LONG:  case TYPE_ULONG:
4172                 break;
4173         default:
4174                 internal_error(state, 0, "constant for unkown type");
4175         }
4176         result = triple(state, OP_INTCONST, type, 0, 0);
4177         result->u.cval = value;
4178         return result;
4179 }
4180
4181
4182 static struct triple *do_mk_addr_expr(struct compile_state *state, 
4183         struct triple *expr, struct type *type, ulong_t offset)
4184 {
4185         struct triple *result;
4186         lvalue(state, expr);
4187
4188         result = 0;
4189         if (expr->op == OP_ADECL) {
4190                 error(state, expr, "address of auto variables not supported");
4191         }
4192         else if (expr->op == OP_SDECL) {
4193                 result = triple(state, OP_ADDRCONST, type, 0, 0);
4194                 MISC(result, 0) = expr;
4195                 result->u.cval = offset;
4196         }
4197         else if (expr->op == OP_DEREF) {
4198                 result = triple(state, OP_ADD, type,
4199                         RHS(expr, 0),
4200                         int_const(state, &ulong_type, offset));
4201         }
4202         return result;
4203 }
4204
4205 static struct triple *mk_addr_expr(
4206         struct compile_state *state, struct triple *expr, ulong_t offset)
4207 {
4208         struct type *type;
4209         
4210         type = new_type(
4211                 TYPE_POINTER | (expr->type->type & QUAL_MASK),
4212                 expr->type, 0);
4213
4214         return do_mk_addr_expr(state, expr, type, offset);
4215 }
4216
4217 static struct triple *mk_deref_expr(
4218         struct compile_state *state, struct triple *expr)
4219 {
4220         struct type *base_type;
4221         pointer(state, expr);
4222         base_type = expr->type->left;
4223         if (!TYPE_PTR(base_type->type) && !TYPE_ARITHMETIC(base_type->type)) {
4224                 error(state, 0, 
4225                         "Only pointer and arithmetic values can be dereferenced");
4226         }
4227         return triple(state, OP_DEREF, base_type, expr, 0);
4228 }
4229
4230 static struct triple *deref_field(
4231         struct compile_state *state, struct triple *expr, struct hash_entry *field)
4232 {
4233         struct triple *result;
4234         struct type *type, *member;
4235         if (!field) {
4236                 internal_error(state, 0, "No field passed to deref_field");
4237         }
4238         result = 0;
4239         type = expr->type;
4240         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4241                 error(state, 0, "request for member %s in something not a struct or union",
4242                         field->name);
4243         }
4244         member = type->left;
4245         while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4246                 if (member->left->field_ident == field) {
4247                         member = member->left;
4248                         break;
4249                 }
4250                 member = member->right;
4251         }
4252         if (member->field_ident != field) {
4253                 error(state, 0, "%s is not a member", field->name);
4254         }
4255         if ((type->type & STOR_MASK) == STOR_PERM) {
4256                 /* Do the pointer arithmetic to get a deref the field */
4257                 ulong_t offset;
4258                 offset = field_offset(state, type, field);
4259                 result = do_mk_addr_expr(state, expr, member, offset);
4260                 result = mk_deref_expr(state, result);
4261         }
4262         else {
4263                 /* Find the variable for the field I want. */
4264                 result = triple(state, OP_DOT, 
4265                         field_type(state, type, field), expr, 0);
4266                 result->u.field = field;
4267         }
4268         return result;
4269 }
4270
4271 static struct triple *read_expr(struct compile_state *state, struct triple *def)
4272 {
4273         int op;
4274         if  (!def) {
4275                 return 0;
4276         }
4277         if (!is_stable(state, def)) {
4278                 return def;
4279         }
4280         /* Tranform an array to a pointer to the first element */
4281 #warning "CHECK_ME is this the right place to transform arrays to pointers?"
4282         if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
4283                 struct type *type;
4284                 struct triple *result;
4285                 type = new_type(
4286                         TYPE_POINTER | (def->type->type & QUAL_MASK),
4287                         def->type->left, 0);
4288                 result = triple(state, OP_ADDRCONST, type, 0, 0);
4289                 MISC(result, 0) = def;
4290                 return result;
4291         }
4292         if (is_in_reg(state, def)) {
4293                 op = OP_READ;
4294         } else {
4295                 op = OP_LOAD;
4296         }
4297         return triple(state, op, def->type, def, 0);
4298 }
4299
4300 static void write_compatible(struct compile_state *state,
4301         struct type *dest, struct type *rval)
4302 {
4303         int compatible = 0;
4304         /* Both operands have arithmetic type */
4305         if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
4306                 compatible = 1;
4307         }
4308         /* One operand is a pointer and the other is a pointer to void */
4309         else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
4310                 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
4311                 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
4312                         ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
4313                 compatible = 1;
4314         }
4315         /* If both types are the same without qualifiers we are good */
4316         else if (equiv_ptrs(dest, rval)) {
4317                 compatible = 1;
4318         }
4319         /* test for struct/union equality  */
4320         else if (((dest->type & TYPE_MASK) == TYPE_STRUCT) &&
4321                 ((rval->type & TYPE_MASK) == TYPE_STRUCT) &&
4322                 (dest->type_ident == rval->type_ident)) {
4323                 compatible = 1;
4324         }
4325         if (!compatible) {
4326                 error(state, 0, "Incompatible types in assignment");
4327         }
4328 }
4329
4330 static struct triple *write_expr(
4331         struct compile_state *state, struct triple *dest, struct triple *rval)
4332 {
4333         struct triple *def;
4334         int op;
4335
4336         def = 0;
4337         if (!rval) {
4338                 internal_error(state, 0, "missing rval");
4339         }
4340
4341         if (rval->op == OP_LIST) {
4342                 internal_error(state, 0, "expression of type OP_LIST?");
4343         }
4344         if (!is_lvalue(state, dest)) {
4345                 internal_error(state, 0, "writing to a non lvalue?");
4346         }
4347
4348         write_compatible(state, dest->type, rval->type);
4349
4350         /* Now figure out which assignment operator to use */
4351         op = -1;
4352         if (is_in_reg(state, dest)) {
4353                 op = OP_WRITE;
4354         } else {
4355                 op = OP_STORE;
4356         }
4357         def = triple(state, op, dest->type, dest, rval);
4358         return def;
4359 }
4360
4361 static struct triple *init_expr(
4362         struct compile_state *state, struct triple *dest, struct triple *rval)
4363 {
4364         struct triple *def;
4365
4366         def = 0;
4367         if (!rval) {
4368                 internal_error(state, 0, "missing rval");
4369         }
4370         if ((dest->type->type & STOR_MASK) != STOR_PERM) {
4371                 rval = read_expr(state, rval);
4372                 def = write_expr(state, dest, rval);
4373         }
4374         else {
4375                 /* Fill in the array size if necessary */
4376                 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
4377                         ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
4378                         if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4379                                 dest->type->elements = rval->type->elements;
4380                         }
4381                 }
4382                 if (!equiv_types(dest->type, rval->type)) {
4383                         error(state, 0, "Incompatible types in inializer");
4384                 }
4385                 MISC(dest, 0) = rval;
4386                 insert_triple(state, dest, rval);
4387                 rval->id |= TRIPLE_FLAG_FLATTENED;
4388                 use_triple(MISC(dest, 0), dest);
4389         }
4390         return def;
4391 }
4392
4393 struct type *arithmetic_result(
4394         struct compile_state *state, struct triple *left, struct triple *right)
4395 {
4396         struct type *type;
4397         /* Sanity checks to ensure I am working with arithmetic types */
4398         arithmetic(state, left);
4399         arithmetic(state, right);
4400         type = new_type(
4401                 do_arithmetic_conversion(
4402                         left->type->type, 
4403                         right->type->type), 0, 0);
4404         return type;
4405 }
4406
4407 struct type *ptr_arithmetic_result(
4408         struct compile_state *state, struct triple *left, struct triple *right)
4409 {
4410         struct type *type;
4411         /* Sanity checks to ensure I am working with the proper types */
4412         ptr_arithmetic(state, left);
4413         arithmetic(state, right);
4414         if (TYPE_ARITHMETIC(left->type->type) && 
4415                 TYPE_ARITHMETIC(right->type->type)) {
4416                 type = arithmetic_result(state, left, right);
4417         }
4418         else if (TYPE_PTR(left->type->type)) {
4419                 type = left->type;
4420         }
4421         else {
4422                 internal_error(state, 0, "huh?");
4423                 type = 0;
4424         }
4425         return type;
4426 }
4427
4428
4429 /* boolean helper function */
4430
4431 static struct triple *ltrue_expr(struct compile_state *state, 
4432         struct triple *expr)
4433 {
4434         switch(expr->op) {
4435         case OP_LTRUE:   case OP_LFALSE:  case OP_EQ:      case OP_NOTEQ:
4436         case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
4437         case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
4438                 /* If the expression is already boolean do nothing */
4439                 break;
4440         default:
4441                 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
4442                 break;
4443         }
4444         return expr;
4445 }
4446
4447 static struct triple *lfalse_expr(struct compile_state *state, 
4448         struct triple *expr)
4449 {
4450         return triple(state, OP_LFALSE, &int_type, expr, 0);
4451 }
4452
4453 static struct triple *cond_expr(
4454         struct compile_state *state, 
4455         struct triple *test, struct triple *left, struct triple *right)
4456 {
4457         struct triple *def;
4458         struct type *result_type;
4459         unsigned int left_type, right_type;
4460         bool(state, test);
4461         left_type = left->type->type;
4462         right_type = right->type->type;
4463         result_type = 0;
4464         /* Both operands have arithmetic type */
4465         if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
4466                 result_type = arithmetic_result(state, left, right);
4467         }
4468         /* Both operands have void type */
4469         else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
4470                 ((right_type & TYPE_MASK) == TYPE_VOID)) {
4471                 result_type = &void_type;
4472         }
4473         /* pointers to the same type... */
4474         else if ((result_type = compatible_ptrs(left->type, right->type))) {
4475                 ;
4476         }
4477         /* Both operands are pointers and left is a pointer to void */
4478         else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4479                 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4480                 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4481                 result_type = right->type;
4482         }
4483         /* Both operands are pointers and right is a pointer to void */
4484         else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4485                 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4486                 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4487                 result_type = left->type;
4488         }
4489         if (!result_type) {
4490                 error(state, 0, "Incompatible types in conditional expression");
4491         }
4492         /* Cleanup and invert the test */
4493         test = lfalse_expr(state, read_expr(state, test));
4494         def = new_triple(state, OP_COND, result_type, 0, 3);
4495         def->param[0] = test;
4496         def->param[1] = left;
4497         def->param[2] = right;
4498         return def;
4499 }
4500
4501
4502 static int expr_depth(struct compile_state *state, struct triple *ins)
4503 {
4504         int count;
4505         count = 0;
4506         if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
4507                 count = 0;
4508         }
4509         else if (ins->op == OP_DEREF) {
4510                 count = expr_depth(state, RHS(ins, 0)) - 1;
4511         }
4512         else if (ins->op == OP_VAL) {
4513                 count = expr_depth(state, RHS(ins, 0)) - 1;
4514         }
4515         else if (ins->op == OP_COMMA) {
4516                 int ldepth, rdepth;
4517                 ldepth = expr_depth(state, RHS(ins, 0));
4518                 rdepth = expr_depth(state, RHS(ins, 1));
4519                 count = (ldepth >= rdepth)? ldepth : rdepth;
4520         }
4521         else if (ins->op == OP_CALL) {
4522                 /* Don't figure the depth of a call just guess it is huge */
4523                 count = 1000;
4524         }
4525         else {
4526                 struct triple **expr;
4527                 expr = triple_rhs(state, ins, 0);
4528                 for(;expr; expr = triple_rhs(state, ins, expr)) {
4529                         if (*expr) {
4530                                 int depth;
4531                                 depth = expr_depth(state, *expr);
4532                                 if (depth > count) {
4533                                         count = depth;
4534                                 }
4535                         }
4536                 }
4537         }
4538         return count + 1;
4539 }
4540
4541 static struct triple *flatten(
4542         struct compile_state *state, struct triple *first, struct triple *ptr);
4543
4544 static struct triple *flatten_generic(
4545         struct compile_state *state, struct triple *first, struct triple *ptr)
4546 {
4547         struct rhs_vector {
4548                 int depth;
4549                 struct triple **ins;
4550         } vector[MAX_RHS];
4551         int i, rhs, lhs;
4552         /* Only operations with just a rhs should come here */
4553         rhs = TRIPLE_RHS(ptr->sizes);
4554         lhs = TRIPLE_LHS(ptr->sizes);
4555         if (TRIPLE_SIZE(ptr->sizes) != lhs + rhs) {
4556                 internal_error(state, ptr, "unexpected args for: %d %s",
4557                         ptr->op, tops(ptr->op));
4558         }
4559         /* Find the depth of the rhs elements */
4560         for(i = 0; i < rhs; i++) {
4561                 vector[i].ins = &RHS(ptr, i);
4562                 vector[i].depth = expr_depth(state, *vector[i].ins);
4563         }
4564         /* Selection sort the rhs */
4565         for(i = 0; i < rhs; i++) {
4566                 int j, max = i;
4567                 for(j = i + 1; j < rhs; j++ ) {
4568                         if (vector[j].depth > vector[max].depth) {
4569                                 max = j;
4570                         }
4571                 }
4572                 if (max != i) {
4573                         struct rhs_vector tmp;
4574                         tmp = vector[i];
4575                         vector[i] = vector[max];
4576                         vector[max] = tmp;
4577                 }
4578         }
4579         /* Now flatten the rhs elements */
4580         for(i = 0; i < rhs; i++) {
4581                 *vector[i].ins = flatten(state, first, *vector[i].ins);
4582                 use_triple(*vector[i].ins, ptr);
4583         }
4584         
4585         /* Now flatten the lhs elements */
4586         for(i = 0; i < lhs; i++) {
4587                 struct triple **ins = &LHS(ptr, i);
4588                 *ins = flatten(state, first, *ins);
4589                 use_triple(*ins, ptr);
4590         }
4591         return ptr;
4592 }
4593
4594 static struct triple *flatten_land(
4595         struct compile_state *state, struct triple *first, struct triple *ptr)
4596 {
4597         struct triple *left, *right;
4598         struct triple *val, *test, *jmp, *label1, *end;
4599
4600         /* Find the triples */
4601         left = RHS(ptr, 0);
4602         right = RHS(ptr, 1);
4603
4604         /* Generate the needed triples */
4605         end = label(state);
4606
4607         /* Thread the triples together */
4608         val          = flatten(state, first, variable(state, ptr->type));
4609         left         = flatten(state, first, write_expr(state, val, left));
4610         test         = flatten(state, first, 
4611                 lfalse_expr(state, read_expr(state, val)));
4612         jmp          = flatten(state, first, branch(state, end, test));
4613         label1       = flatten(state, first, label(state));
4614         right        = flatten(state, first, write_expr(state, val, right));
4615         TARG(jmp, 0) = flatten(state, first, end); 
4616         
4617         /* Now give the caller something to chew on */
4618         return read_expr(state, val);
4619 }
4620
4621 static struct triple *flatten_lor(
4622         struct compile_state *state, struct triple *first, struct triple *ptr)
4623 {
4624         struct triple *left, *right;
4625         struct triple *val, *jmp, *label1, *end;
4626
4627         /* Find the triples */
4628         left = RHS(ptr, 0);
4629         right = RHS(ptr, 1);
4630
4631         /* Generate the needed triples */
4632         end = label(state);
4633
4634         /* Thread the triples together */
4635         val          = flatten(state, first, variable(state, ptr->type));
4636         left         = flatten(state, first, write_expr(state, val, left));
4637         jmp          = flatten(state, first, branch(state, end, left));
4638         label1       = flatten(state, first, label(state));
4639         right        = flatten(state, first, write_expr(state, val, right));
4640         TARG(jmp, 0) = flatten(state, first, end);
4641        
4642         
4643         /* Now give the caller something to chew on */
4644         return read_expr(state, val);
4645 }
4646
4647 static struct triple *flatten_cond(
4648         struct compile_state *state, struct triple *first, struct triple *ptr)
4649 {
4650         struct triple *test, *left, *right;
4651         struct triple *val, *mv1, *jmp1, *label1, *mv2, *middle, *jmp2, *end;
4652
4653         /* Find the triples */
4654         test = RHS(ptr, 0);
4655         left = RHS(ptr, 1);
4656         right = RHS(ptr, 2);
4657
4658         /* Generate the needed triples */
4659         end = label(state);
4660         middle = label(state);
4661
4662         /* Thread the triples together */
4663         val           = flatten(state, first, variable(state, ptr->type));
4664         test          = flatten(state, first, test);
4665         jmp1          = flatten(state, first, branch(state, middle, test));
4666         label1        = flatten(state, first, label(state));
4667         left          = flatten(state, first, left);
4668         mv1           = flatten(state, first, write_expr(state, val, left));
4669         jmp2          = flatten(state, first, branch(state, end, 0));
4670         TARG(jmp1, 0) = flatten(state, first, middle);
4671         right         = flatten(state, first, right);
4672         mv2           = flatten(state, first, write_expr(state, val, right));
4673         TARG(jmp2, 0) = flatten(state, first, end);
4674         
4675         /* Now give the caller something to chew on */
4676         return read_expr(state, val);
4677 }
4678
4679 struct triple *copy_func(struct compile_state *state, struct triple *ofunc)
4680 {
4681         struct triple *nfunc;
4682         struct triple *nfirst, *ofirst;
4683         struct triple *new, *old;
4684
4685 #if 0
4686         fprintf(stdout, "\n");
4687         loc(stdout, state, 0);
4688         fprintf(stdout, "\n__________ copy_func _________\n");
4689         print_triple(state, ofunc);
4690         fprintf(stdout, "__________ copy_func _________ done\n\n");
4691 #endif
4692
4693         /* Make a new copy of the old function */
4694         nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
4695         nfirst = 0;
4696         ofirst = old = RHS(ofunc, 0);
4697         do {
4698                 struct triple *new;
4699                 int old_lhs, old_rhs;
4700                 old_lhs = TRIPLE_LHS(old->sizes);
4701                 old_rhs = TRIPLE_RHS(old->sizes);
4702                 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
4703                         old->filename, old->line, old->col);
4704                 if (!triple_stores_block(state, new)) {
4705                         memcpy(&new->u, &old->u, sizeof(new->u));
4706                 }
4707                 if (!nfirst) {
4708                         RHS(nfunc, 0) = nfirst = new;
4709                 }
4710                 else {
4711                         insert_triple(state, nfirst, new);
4712                 }
4713                 new->id |= TRIPLE_FLAG_FLATTENED;
4714                 
4715                 /* During the copy remember new as user of old */
4716                 use_triple(old, new);
4717
4718                 /* Populate the return type if present */
4719                 if (old == MISC(ofunc, 0)) {
4720                         MISC(nfunc, 0) = new;
4721                 }
4722                 old = old->next;
4723         } while(old != ofirst);
4724
4725         /* Make a second pass to fix up any unresolved references */
4726         old = ofirst;
4727         new = nfirst;
4728         do {
4729                 struct triple **oexpr, **nexpr;
4730                 int count, i;
4731                 /* Lookup where the copy is, to join pointers */
4732                 count = TRIPLE_SIZE(old->sizes);
4733                 for(i = 0; i < count; i++) {
4734                         oexpr = &old->param[i];
4735                         nexpr = &new->param[i];
4736                         if (!*nexpr && *oexpr && (*oexpr)->use) {
4737                                 *nexpr = (*oexpr)->use->member;
4738                                 if (*nexpr == old) {
4739                                         internal_error(state, 0, "new == old?");
4740                                 }
4741                                 use_triple(*nexpr, new);
4742                         }
4743                         if (!*nexpr && *oexpr) {
4744                                 internal_error(state, 0, "Could not copy %d\n", i);
4745                         }
4746                 }
4747                 old = old->next;
4748                 new = new->next;
4749         } while((old != ofirst) && (new != nfirst));
4750         
4751         /* Make a third pass to cleanup the extra useses */
4752         old = ofirst;
4753         new = nfirst;
4754         do {
4755                 unuse_triple(old, new);
4756                 old = old->next;
4757                 new = new->next;
4758         } while ((old != ofirst) && (new != nfirst));
4759         return nfunc;
4760 }
4761
4762 static struct triple *flatten_call(
4763         struct compile_state *state, struct triple *first, struct triple *ptr)
4764 {
4765         /* Inline the function call */
4766         struct type *ptype;
4767         struct triple *ofunc, *nfunc, *nfirst, *param, *result;
4768         struct triple *end, *nend;
4769         int pvals, i;
4770
4771         /* Find the triples */
4772         ofunc = MISC(ptr, 0);
4773         if (ofunc->op != OP_LIST) {
4774                 internal_error(state, 0, "improper function");
4775         }
4776         nfunc = copy_func(state, ofunc);
4777         nfirst = RHS(nfunc, 0)->next;
4778         /* Prepend the parameter reading into the new function list */
4779         ptype = nfunc->type->right;
4780         param = RHS(nfunc, 0)->next;
4781         pvals = TRIPLE_RHS(ptr->sizes);
4782         for(i = 0; i < pvals; i++) {
4783                 struct type *atype;
4784                 struct triple *arg;
4785                 atype = ptype;
4786                 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
4787                         atype = ptype->left;
4788                 }
4789                 while((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
4790                         param = param->next;
4791                 }
4792                 arg = RHS(ptr, i);
4793                 flatten(state, nfirst, write_expr(state, param, arg));
4794                 ptype = ptype->right;
4795                 param = param->next;
4796         }
4797         result = 0;
4798         if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
4799                 result = read_expr(state, MISC(nfunc,0));
4800         }
4801 #if 0
4802         fprintf(stdout, "\n");
4803         loc(stdout, state, 0);
4804         fprintf(stdout, "\n__________ flatten_call _________\n");
4805         print_triple(state, nfunc);
4806         fprintf(stdout, "__________ flatten_call _________ done\n\n");
4807 #endif
4808
4809         /* Get rid of the extra triples */
4810         nfirst = RHS(nfunc, 0)->next;
4811         free_triple(state, RHS(nfunc, 0));
4812         RHS(nfunc, 0) = 0;
4813         free_triple(state, nfunc);
4814
4815         /* Append the new function list onto the return list */
4816         end = first->prev;
4817         nend = nfirst->prev;
4818         end->next    = nfirst;
4819         nfirst->prev = end;
4820         nend->next   = first;
4821         first->prev  = nend;
4822
4823         return result;
4824 }
4825
4826 static struct triple *flatten(
4827         struct compile_state *state, struct triple *first, struct triple *ptr)
4828 {
4829         struct triple *orig_ptr;
4830         if (!ptr)
4831                 return 0;
4832         do {
4833                 orig_ptr = ptr;
4834                 /* Only flatten triples once */
4835                 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
4836                         return ptr;
4837                 }
4838                 switch(ptr->op) {
4839                 case OP_WRITE:
4840                 case OP_STORE:
4841                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
4842                         LHS(ptr, 0) = flatten(state, first, LHS(ptr, 0));
4843                         use_triple(LHS(ptr, 0), ptr);
4844                         use_triple(RHS(ptr, 0), ptr);
4845                         break;
4846                 case OP_COMMA:
4847                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
4848                         ptr = RHS(ptr, 1);
4849                         break;
4850                 case OP_VAL:
4851                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
4852                         return MISC(ptr, 0);
4853                         break;
4854                 case OP_LAND:
4855                         ptr = flatten_land(state, first, ptr);
4856                         break;
4857                 case OP_LOR:
4858                         ptr = flatten_lor(state, first, ptr);
4859                         break;
4860                 case OP_COND:
4861                         ptr = flatten_cond(state, first, ptr);
4862                         break;
4863                 case OP_CALL:
4864                         ptr = flatten_call(state, first, ptr);
4865                         break;
4866                 case OP_READ:
4867                 case OP_LOAD:
4868                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
4869                         use_triple(RHS(ptr, 0), ptr);
4870                         break;
4871                 case OP_BRANCH:
4872                         use_triple(TARG(ptr, 0), ptr);
4873                         if (TRIPLE_RHS(ptr->sizes)) {
4874                                 use_triple(RHS(ptr, 0), ptr);
4875                                 if (ptr->next != ptr) {
4876                                         use_triple(ptr->next, ptr);
4877                                 }
4878                         }
4879                         break;
4880                 case OP_BLOBCONST:
4881                         insert_triple(state, first, ptr);
4882                         ptr->id |= TRIPLE_FLAG_FLATTENED;
4883                         ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
4884                         use_triple(MISC(ptr, 0), ptr);
4885                         break;
4886                 case OP_DEREF:
4887                         /* Since OP_DEREF is just a marker delete it when I flatten it */
4888                         ptr = RHS(ptr, 0);
4889                         RHS(orig_ptr, 0) = 0;
4890                         free_triple(state, orig_ptr);
4891                         break;
4892                 case OP_DOT:
4893                 {
4894                         struct triple *base;
4895                         base = RHS(ptr, 0);
4896                         base = flatten(state, first, base);
4897                         if (base->op == OP_VAL_VEC) {
4898                                 ptr = struct_field(state, base, ptr->u.field);
4899                         }
4900                         break;
4901                 }
4902                 case OP_ADDRCONST:
4903                 case OP_SDECL:
4904                 case OP_PIECE:
4905                         MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
4906                         use_triple(MISC(ptr, 0), ptr);
4907                         break;
4908                 case OP_ADECL:
4909                         break;
4910                 default:
4911                         /* Flatten the easy cases we don't override */
4912                         ptr = flatten_generic(state, first, ptr);
4913                         break;
4914                 }
4915         } while(ptr && (ptr != orig_ptr));
4916         if (ptr) {
4917                 insert_triple(state, first, ptr);
4918                 ptr->id |= TRIPLE_FLAG_FLATTENED;
4919         }
4920         return ptr;
4921 }
4922
4923 static void release_expr(struct compile_state *state, struct triple *expr)
4924 {
4925         struct triple *head;
4926         head = label(state);
4927         flatten(state, head, expr);
4928         while(head->next != head) {
4929                 release_triple(state, head->next);
4930         }
4931         free_triple(state, head);
4932 }
4933
4934 static int replace_rhs_use(struct compile_state *state,
4935         struct triple *orig, struct triple *new, struct triple *use)
4936 {
4937         struct triple **expr;
4938         int found;
4939         found = 0;
4940         expr = triple_rhs(state, use, 0);
4941         for(;expr; expr = triple_rhs(state, use, expr)) {
4942                 if (*expr == orig) {
4943                         *expr = new;
4944                         found = 1;
4945                 }
4946         }
4947         if (found) {
4948                 unuse_triple(orig, use);
4949                 use_triple(new, use);
4950         }
4951         return found;
4952 }
4953
4954 static int replace_lhs_use(struct compile_state *state,
4955         struct triple *orig, struct triple *new, struct triple *use)
4956 {
4957         struct triple **expr;
4958         int found;
4959         found = 0;
4960         expr = triple_lhs(state, use, 0);
4961         for(;expr; expr = triple_lhs(state, use, expr)) {
4962                 if (*expr == orig) {
4963                         *expr = new;
4964                         found = 1;
4965                 }
4966         }
4967         if (found) {
4968                 unuse_triple(orig, use);
4969                 use_triple(new, use);
4970         }
4971         return found;
4972 }
4973
4974 static void propogate_use(struct compile_state *state,
4975         struct triple *orig, struct triple *new)
4976 {
4977         struct triple_set *user, *next;
4978         for(user = orig->use; user; user = next) {
4979                 struct triple *use;
4980                 int found;
4981                 next = user->next;
4982                 use = user->member;
4983                 found = 0;
4984                 found |= replace_rhs_use(state, orig, new, use);
4985                 found |= replace_lhs_use(state, orig, new, use);
4986                 if (!found) {
4987                         internal_error(state, use, "use without use");
4988                 }
4989         }
4990         if (orig->use) {
4991                 internal_error(state, orig, "used after propogate_use");
4992         }
4993 }
4994
4995 /*
4996  * Code generators
4997  * ===========================
4998  */
4999
5000 static struct triple *mk_add_expr(
5001         struct compile_state *state, struct triple *left, struct triple *right)
5002 {
5003         struct type *result_type;
5004         /* Put pointer operands on the left */
5005         if (is_pointer(right)) {
5006                 struct triple *tmp;
5007                 tmp = left;
5008                 left = right;
5009                 right = tmp;
5010         }
5011         left  = read_expr(state, left);
5012         right = read_expr(state, right);
5013         result_type = ptr_arithmetic_result(state, left, right);
5014         if (is_pointer(left)) {
5015                 right = triple(state, 
5016                         is_signed(right->type)? OP_SMUL : OP_UMUL, 
5017                         &ulong_type, 
5018                         right, 
5019                         int_const(state, &ulong_type, 
5020                                 size_of(state, left->type->left)));
5021         }
5022         return triple(state, OP_ADD, result_type, left, right);
5023 }
5024
5025 static struct triple *mk_sub_expr(
5026         struct compile_state *state, struct triple *left, struct triple *right)
5027 {
5028         struct type *result_type;
5029         result_type = ptr_arithmetic_result(state, left, right);
5030         left  = read_expr(state, left);
5031         right = read_expr(state, right);
5032         if (is_pointer(left)) {
5033                 right = triple(state, 
5034                         is_signed(right->type)? OP_SMUL : OP_UMUL, 
5035                         &ulong_type, 
5036                         right, 
5037                         int_const(state, &ulong_type, 
5038                                 size_of(state, left->type->left)));
5039         }
5040         return triple(state, OP_SUB, result_type, left, right);
5041 }
5042
5043 static struct triple *mk_pre_inc_expr(
5044         struct compile_state *state, struct triple *def)
5045 {
5046         struct triple *val;
5047         lvalue(state, def);
5048         val = mk_add_expr(state, def, int_const(state, &int_type, 1));
5049         return triple(state, OP_VAL, def->type,
5050                 write_expr(state, def, val),
5051                 val);
5052 }
5053
5054 static struct triple *mk_pre_dec_expr(
5055         struct compile_state *state, struct triple *def)
5056 {
5057         struct triple *val;
5058         lvalue(state, def);
5059         val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
5060         return triple(state, OP_VAL, def->type,
5061                 write_expr(state, def, val),
5062                 val);
5063 }
5064
5065 static struct triple *mk_post_inc_expr(
5066         struct compile_state *state, struct triple *def)
5067 {
5068         struct triple *val;
5069         lvalue(state, def);
5070         val = read_expr(state, def);
5071         return triple(state, OP_VAL, def->type,
5072                 write_expr(state, def,
5073                         mk_add_expr(state, val, int_const(state, &int_type, 1)))
5074                 , val);
5075 }
5076
5077 static struct triple *mk_post_dec_expr(
5078         struct compile_state *state, struct triple *def)
5079 {
5080         struct triple *val;
5081         lvalue(state, def);
5082         val = read_expr(state, def);
5083         return triple(state, OP_VAL, def->type, 
5084                 write_expr(state, def,
5085                         mk_sub_expr(state, val, int_const(state, &int_type, 1)))
5086                 , val);
5087 }
5088
5089 static struct triple *mk_subscript_expr(
5090         struct compile_state *state, struct triple *left, struct triple *right)
5091 {
5092         left  = read_expr(state, left);
5093         right = read_expr(state, right);
5094         if (!is_pointer(left) && !is_pointer(right)) {
5095                 error(state, left, "subscripted value is not a pointer");
5096         }
5097         return mk_deref_expr(state, mk_add_expr(state, left, right));
5098 }
5099
5100 /*
5101  * Compile time evaluation
5102  * ===========================
5103  */
5104 static int is_const(struct triple *ins)
5105 {
5106         return IS_CONST_OP(ins->op);
5107 }
5108
5109 static int constants_equal(struct compile_state *state, 
5110         struct triple *left, struct triple *right)
5111 {
5112         int equal;
5113         if (!is_const(left) || !is_const(right)) {
5114                 equal = 0;
5115         }
5116         else if (left->op != right->op) {
5117                 equal = 0;
5118         }
5119         else if (!equiv_types(left->type, right->type)) {
5120                 equal = 0;
5121         }
5122         else {
5123                 equal = 0;
5124                 switch(left->op) {
5125                 case OP_INTCONST:
5126                         if (left->u.cval == right->u.cval) {
5127                                 equal = 1;
5128                         }
5129                         break;
5130                 case OP_BLOBCONST:
5131                 {
5132                         size_t lsize, rsize;
5133                         lsize = size_of(state, left->type);
5134                         rsize = size_of(state, right->type);
5135                         if (lsize != rsize) {
5136                                 break;
5137                         }
5138                         if (memcmp(left->u.blob, right->u.blob, lsize) == 0) {
5139                                 equal = 1;
5140                         }
5141                         break;
5142                 }
5143                 case OP_ADDRCONST:
5144                         if ((MISC(left, 0) == MISC(right, 0)) &&
5145                                 (left->u.cval == right->u.cval)) {
5146                                 equal = 1;
5147                         }
5148                         break;
5149                 default:
5150                         internal_error(state, left, "uknown constant type");
5151                         break;
5152                 }
5153         }
5154         return equal;
5155 }
5156
5157 static int is_zero(struct triple *ins)
5158 {
5159         return is_const(ins) && (ins->u.cval == 0);
5160 }
5161
5162 static int is_one(struct triple *ins)
5163 {
5164         return is_const(ins) && (ins->u.cval == 1);
5165 }
5166
5167 static long_t bsr(ulong_t value)
5168 {
5169         int i;
5170         for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
5171                 ulong_t mask;
5172                 mask = 1;
5173                 mask <<= i;
5174                 if (value & mask) {
5175                         return i;
5176                 }
5177         }
5178         return -1;
5179 }
5180
5181 static long_t bsf(ulong_t value)
5182 {
5183         int i;
5184         for(i = 0; i < (sizeof(ulong_t)*8); i++) {
5185                 ulong_t mask;
5186                 mask = 1;
5187                 mask <<= 1;
5188                 if (value & mask) {
5189                         return i;
5190                 }
5191         }
5192         return -1;
5193 }
5194
5195 static long_t log2(ulong_t value)
5196 {
5197         return bsr(value);
5198 }
5199
5200 static long_t tlog2(struct triple *ins)
5201 {
5202         return log2(ins->u.cval);
5203 }
5204
5205 static int is_pow2(struct triple *ins)
5206 {
5207         ulong_t value, mask;
5208         long_t log;
5209         if (!is_const(ins)) {
5210                 return 0;
5211         }
5212         value = ins->u.cval;
5213         log = log2(value);
5214         if (log == -1) {
5215                 return 0;
5216         }
5217         mask = 1;
5218         mask <<= log;
5219         return  ((value & mask) == value);
5220 }
5221
5222 static ulong_t read_const(struct compile_state *state,
5223         struct triple *ins, struct triple **expr)
5224 {
5225         struct triple *rhs;
5226         rhs = *expr;
5227         switch(rhs->type->type &TYPE_MASK) {
5228         case TYPE_CHAR:   
5229         case TYPE_SHORT:
5230         case TYPE_INT:
5231         case TYPE_LONG:
5232         case TYPE_UCHAR:   
5233         case TYPE_USHORT:  
5234         case TYPE_UINT:
5235         case TYPE_ULONG:
5236         case TYPE_POINTER:
5237                 break;
5238         default:
5239                 internal_error(state, rhs, "bad type to read_const\n");
5240                 break;
5241         }
5242         return rhs->u.cval;
5243 }
5244
5245 static long_t read_sconst(struct triple *ins, struct triple **expr)
5246 {
5247         struct triple *rhs;
5248         rhs = *expr;
5249         return (long_t)(rhs->u.cval);
5250 }
5251
5252 static void unuse_rhs(struct compile_state *state, struct triple *ins)
5253 {
5254         struct triple **expr;
5255         expr = triple_rhs(state, ins, 0);
5256         for(;expr;expr = triple_rhs(state, ins, expr)) {
5257                 if (*expr) {
5258                         unuse_triple(*expr, ins);
5259                         *expr = 0;
5260                 }
5261         }
5262 }
5263
5264 static void unuse_lhs(struct compile_state *state, struct triple *ins)
5265 {
5266         struct triple **expr;
5267         expr = triple_lhs(state, ins, 0);
5268         for(;expr;expr = triple_lhs(state, ins, expr)) {
5269                 unuse_triple(*expr, ins);
5270                 *expr = 0;
5271         }
5272 }
5273
5274 static void check_lhs(struct compile_state *state, struct triple *ins)
5275 {
5276         struct triple **expr;
5277         expr = triple_lhs(state, ins, 0);
5278         for(;expr;expr = triple_lhs(state, ins, expr)) {
5279                 internal_error(state, ins, "unexpected lhs");
5280         }
5281         
5282 }
5283 static void check_targ(struct compile_state *state, struct triple *ins)
5284 {
5285         struct triple **expr;
5286         expr = triple_targ(state, ins, 0);
5287         for(;expr;expr = triple_targ(state, ins, expr)) {
5288                 internal_error(state, ins, "unexpected targ");
5289         }
5290 }
5291
5292 static void wipe_ins(struct compile_state *state, struct triple *ins)
5293 {
5294         /* Becareful which instructions you replace the wiped
5295          * instruction with, as there are not enough slots
5296          * in all instructions to hold all others.
5297          */
5298         check_targ(state, ins);
5299         unuse_rhs(state, ins);
5300         unuse_lhs(state, ins);
5301 }
5302
5303 static void mkcopy(struct compile_state *state, 
5304         struct triple *ins, struct triple *rhs)
5305 {
5306         wipe_ins(state, ins);
5307         ins->op = OP_COPY;
5308         ins->sizes = TRIPLE_SIZES(0, 1, 0, 0);
5309         RHS(ins, 0) = rhs;
5310         use_triple(RHS(ins, 0), ins);
5311 }
5312
5313 static void mkconst(struct compile_state *state, 
5314         struct triple *ins, ulong_t value)
5315 {
5316         if (!is_integral(ins) && !is_pointer(ins)) {
5317                 internal_error(state, ins, "unknown type to make constant\n");
5318         }
5319         wipe_ins(state, ins);
5320         ins->op = OP_INTCONST;
5321         ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
5322         ins->u.cval = value;
5323 }
5324
5325 static void mkaddr_const(struct compile_state *state,
5326         struct triple *ins, struct triple *sdecl, ulong_t value)
5327 {
5328         wipe_ins(state, ins);
5329         ins->op = OP_ADDRCONST;
5330         ins->sizes = TRIPLE_SIZES(0, 0, 1, 0);
5331         MISC(ins, 0) = sdecl;
5332         ins->u.cval = value;
5333         use_triple(sdecl, ins);
5334 }
5335
5336 /* Transform multicomponent variables into simple register variables */
5337 static void flatten_structures(struct compile_state *state)
5338 {
5339         struct triple *ins, *first;
5340         first = RHS(state->main_function, 0);
5341         ins = first;
5342         /* Pass one expand structure values into valvecs.
5343          */
5344         ins = first;
5345         do {
5346                 struct triple *next;
5347                 next = ins->next;
5348                 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5349                         if (ins->op == OP_VAL_VEC) {
5350                                 /* Do nothing */
5351                         }
5352                         else if ((ins->op == OP_LOAD) || (ins->op == OP_READ)) {
5353                                 struct triple *def, **vector;
5354                                 struct type *tptr;
5355                                 int op;
5356                                 ulong_t i;
5357
5358                                 op = ins->op;
5359                                 def = RHS(ins, 0);
5360                                 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
5361                                         ins->filename, ins->line, ins->col);
5362
5363                                 vector = &RHS(next, 0);
5364                                 tptr = next->type->left;
5365                                 for(i = 0; i < next->type->elements; i++) {
5366                                         struct triple *sfield;
5367                                         struct type *mtype;
5368                                         mtype = tptr;
5369                                         if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5370                                                 mtype = mtype->left;
5371                                         }
5372                                         sfield = deref_field(state, def, mtype->field_ident);
5373                                         
5374                                         vector[i] = triple(
5375                                                 state, op, mtype, sfield, 0);
5376                                         vector[i]->filename = next->filename;
5377                                         vector[i]->line = next->line;
5378                                         vector[i]->col = next->col;
5379                                         tptr = tptr->right;
5380                                 }
5381                                 propogate_use(state, ins, next);
5382                                 flatten(state, ins, next);
5383                                 free_triple(state, ins);
5384                         }
5385                         else if ((ins->op == OP_STORE) || (ins->op == OP_WRITE)) {
5386                                 struct triple *src, *dst, **vector;
5387                                 struct type *tptr;
5388                                 int op;
5389                                 ulong_t i;
5390
5391                                 op = ins->op;
5392                                 src = RHS(ins, 0);
5393                                 dst = LHS(ins, 0);
5394                                 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
5395                                         ins->filename, ins->line, ins->col);
5396                                 
5397                                 vector = &RHS(next, 0);
5398                                 tptr = next->type->left;
5399                                 for(i = 0; i < ins->type->elements; i++) {
5400                                         struct triple *dfield, *sfield;
5401                                         struct type *mtype;
5402                                         mtype = tptr;
5403                                         if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5404                                                 mtype = mtype->left;
5405                                         }
5406                                         sfield = deref_field(state, src, mtype->field_ident);
5407                                         dfield = deref_field(state, dst, mtype->field_ident);
5408                                         vector[i] = triple(
5409                                                 state, op, mtype, dfield, sfield);
5410                                         vector[i]->filename = next->filename;
5411                                         vector[i]->line = next->line;
5412                                         vector[i]->col = next->col;
5413                                         tptr = tptr->right;
5414                                 }
5415                                 propogate_use(state, ins, next);
5416                                 flatten(state, ins, next);
5417                                 free_triple(state, ins);
5418                         }
5419                 }
5420                 ins = next;
5421         } while(ins != first);
5422         /* Pass two flatten the valvecs.
5423          */
5424         ins = first;
5425         do {
5426                 struct triple *next;
5427                 next = ins->next;
5428                 if (ins->op == OP_VAL_VEC) {
5429                         release_triple(state, ins);
5430                 } 
5431                 ins = next;
5432         } while(ins != first);
5433         /* Pass three verify the state and set ->id to 0.
5434          */
5435         ins = first;
5436         do {
5437                 ins->id &= ~TRIPLE_FLAG_FLATTENED;
5438                 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5439                         internal_error(state, 0, "STRUCT_TYPE remains?");
5440                 }
5441                 if (ins->op == OP_DOT) {
5442                         internal_error(state, 0, "OP_DOT remains?");
5443                 }
5444                 if (ins->op == OP_VAL_VEC) {
5445                         internal_error(state, 0, "OP_VAL_VEC remains?");
5446                 }
5447                 ins = ins->next;
5448         } while(ins != first);
5449 }
5450
5451 /* For those operations that cannot be simplified */
5452 static void simplify_noop(struct compile_state *state, struct triple *ins)
5453 {
5454         return;
5455 }
5456
5457 static void simplify_smul(struct compile_state *state, struct triple *ins)
5458 {
5459         if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5460                 struct triple *tmp;
5461                 tmp = RHS(ins, 0);
5462                 RHS(ins, 0) = RHS(ins, 1);
5463                 RHS(ins, 1) = tmp;
5464         }
5465         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5466                 long_t left, right;
5467                 left  = read_sconst(ins, &RHS(ins, 0));
5468                 right = read_sconst(ins, &RHS(ins, 1));
5469                 mkconst(state, ins, left * right);
5470         }
5471         else if (is_zero(RHS(ins, 1))) {
5472                 mkconst(state, ins, 0);
5473         }
5474         else if (is_one(RHS(ins, 1))) {
5475                 mkcopy(state, ins, RHS(ins, 0));
5476         }
5477         else if (is_pow2(RHS(ins, 1))) {
5478                 struct triple *val;
5479                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5480                 ins->op = OP_SL;
5481                 insert_triple(state, ins, val);
5482                 unuse_triple(RHS(ins, 1), ins);
5483                 use_triple(val, ins);
5484                 RHS(ins, 1) = val;
5485         }
5486 }
5487
5488 static void simplify_umul(struct compile_state *state, struct triple *ins)
5489 {
5490         if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5491                 struct triple *tmp;
5492                 tmp = RHS(ins, 0);
5493                 RHS(ins, 0) = RHS(ins, 1);
5494                 RHS(ins, 1) = tmp;
5495         }
5496         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5497                 ulong_t left, right;
5498                 left  = read_const(state, ins, &RHS(ins, 0));
5499                 right = read_const(state, ins, &RHS(ins, 1));
5500                 mkconst(state, ins, left * right);
5501         }
5502         else if (is_zero(RHS(ins, 1))) {
5503                 mkconst(state, ins, 0);
5504         }
5505         else if (is_one(RHS(ins, 1))) {
5506                 mkcopy(state, ins, RHS(ins, 0));
5507         }
5508         else if (is_pow2(RHS(ins, 1))) {
5509                 struct triple *val;
5510                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5511                 ins->op = OP_SL;
5512                 insert_triple(state, ins, val);
5513                 unuse_triple(RHS(ins, 1), ins);
5514                 use_triple(val, ins);
5515                 RHS(ins, 1) = val;
5516         }
5517 }
5518
5519 static void simplify_sdiv(struct compile_state *state, struct triple *ins)
5520 {
5521         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5522                 long_t left, right;
5523                 left  = read_sconst(ins, &RHS(ins, 0));
5524                 right = read_sconst(ins, &RHS(ins, 1));
5525                 mkconst(state, ins, left / right);
5526         }
5527         else if (is_zero(RHS(ins, 0))) {
5528                 mkconst(state, ins, 0);
5529         }
5530         else if (is_zero(RHS(ins, 1))) {
5531                 error(state, ins, "division by zero");
5532         }
5533         else if (is_one(RHS(ins, 1))) {
5534                 mkcopy(state, ins, RHS(ins, 0));
5535         }
5536         else if (is_pow2(RHS(ins, 1))) {
5537                 struct triple *val;
5538                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5539                 ins->op = OP_SSR;
5540                 insert_triple(state, ins, val);
5541                 unuse_triple(RHS(ins, 1), ins);
5542                 use_triple(val, ins);
5543                 RHS(ins, 1) = val;
5544         }
5545 }
5546
5547 static void simplify_udiv(struct compile_state *state, struct triple *ins)
5548 {
5549         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5550                 ulong_t left, right;
5551                 left  = read_const(state, ins, &RHS(ins, 0));
5552                 right = read_const(state, ins, &RHS(ins, 1));
5553                 mkconst(state, ins, left / right);
5554         }
5555         else if (is_zero(RHS(ins, 0))) {
5556                 mkconst(state, ins, 0);
5557         }
5558         else if (is_zero(RHS(ins, 1))) {
5559                 error(state, ins, "division by zero");
5560         }
5561         else if (is_one(RHS(ins, 1))) {
5562                 mkcopy(state, ins, RHS(ins, 0));
5563         }
5564         else if (is_pow2(RHS(ins, 1))) {
5565                 struct triple *val;
5566                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5567                 ins->op = OP_USR;
5568                 insert_triple(state, ins, val);
5569                 unuse_triple(RHS(ins, 1), ins);
5570                 use_triple(val, ins);
5571                 RHS(ins, 1) = val;
5572         }
5573 }
5574
5575 static void simplify_smod(struct compile_state *state, struct triple *ins)
5576 {
5577         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5578                 long_t left, right;
5579                 left  = read_const(state, ins, &RHS(ins, 0));
5580                 right = read_const(state, ins, &RHS(ins, 1));
5581                 mkconst(state, ins, left % right);
5582         }
5583         else if (is_zero(RHS(ins, 0))) {
5584                 mkconst(state, ins, 0);
5585         }
5586         else if (is_zero(RHS(ins, 1))) {
5587                 error(state, ins, "division by zero");
5588         }
5589         else if (is_one(RHS(ins, 1))) {
5590                 mkconst(state, ins, 0);
5591         }
5592         else if (is_pow2(RHS(ins, 1))) {
5593                 struct triple *val;
5594                 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
5595                 ins->op = OP_AND;
5596                 insert_triple(state, ins, val);
5597                 unuse_triple(RHS(ins, 1), ins);
5598                 use_triple(val, ins);
5599                 RHS(ins, 1) = val;
5600         }
5601 }
5602 static void simplify_umod(struct compile_state *state, struct triple *ins)
5603 {
5604         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5605                 ulong_t left, right;
5606                 left  = read_const(state, ins, &RHS(ins, 0));
5607                 right = read_const(state, ins, &RHS(ins, 1));
5608                 mkconst(state, ins, left % right);
5609         }
5610         else if (is_zero(RHS(ins, 0))) {
5611                 mkconst(state, ins, 0);
5612         }
5613         else if (is_zero(RHS(ins, 1))) {
5614                 error(state, ins, "division by zero");
5615         }
5616         else if (is_one(RHS(ins, 1))) {
5617                 mkconst(state, ins, 0);
5618         }
5619         else if (is_pow2(RHS(ins, 1))) {
5620                 struct triple *val;
5621                 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
5622                 ins->op = OP_AND;
5623                 insert_triple(state, ins, val);
5624                 unuse_triple(RHS(ins, 1), ins);
5625                 use_triple(val, ins);
5626                 RHS(ins, 1) = val;
5627         }
5628 }
5629
5630 static void simplify_add(struct compile_state *state, struct triple *ins)
5631 {
5632         /* start with the pointer on the left */
5633         if (is_pointer(RHS(ins, 1))) {
5634                 struct triple *tmp;
5635                 tmp = RHS(ins, 0);
5636                 RHS(ins, 0) = RHS(ins, 1);
5637                 RHS(ins, 1) = tmp;
5638         }
5639         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5640                 if (!is_pointer(RHS(ins, 0))) {
5641                         ulong_t left, right;
5642                         left  = read_const(state, ins, &RHS(ins, 0));
5643                         right = read_const(state, ins, &RHS(ins, 1));
5644                         mkconst(state, ins, left + right);
5645                 }
5646                 else /* op == OP_ADDRCONST */ {
5647                         struct triple *sdecl;
5648                         ulong_t left, right;
5649                         sdecl = MISC(RHS(ins, 0), 0);
5650                         left  = RHS(ins, 0)->u.cval;
5651                         right = RHS(ins, 1)->u.cval;
5652                         mkaddr_const(state, ins, sdecl, left + right);
5653                 }
5654         }
5655         else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5656                 struct triple *tmp;
5657                 tmp = RHS(ins, 1);
5658                 RHS(ins, 1) = RHS(ins, 0);
5659                 RHS(ins, 0) = tmp;
5660         }
5661 }
5662
5663 static void simplify_sub(struct compile_state *state, struct triple *ins)
5664 {
5665         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5666                 if (!is_pointer(RHS(ins, 0))) {
5667                         ulong_t left, right;
5668                         left  = read_const(state, ins, &RHS(ins, 0));
5669                         right = read_const(state, ins, &RHS(ins, 1));
5670                         mkconst(state, ins, left - right);
5671                 }
5672                 else /* op == OP_ADDRCONST */ {
5673                         struct triple *sdecl;
5674                         ulong_t left, right;
5675                         sdecl = MISC(RHS(ins, 0), 0);
5676                         left  = RHS(ins, 0)->u.cval;
5677                         right = RHS(ins, 1)->u.cval;
5678                         mkaddr_const(state, ins, sdecl, left - right);
5679                 }
5680         }
5681 }
5682
5683 static void simplify_sl(struct compile_state *state, struct triple *ins)
5684 {
5685         if (is_const(RHS(ins, 1))) {
5686                 ulong_t right;
5687                 right = read_const(state, ins, &RHS(ins, 1));
5688                 if (right >= (size_of(state, ins->type)*8)) {
5689                         warning(state, ins, "left shift count >= width of type");
5690                 }
5691         }
5692         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5693                 ulong_t left, right;
5694                 left  = read_const(state, ins, &RHS(ins, 0));
5695                 right = read_const(state, ins, &RHS(ins, 1));
5696                 mkconst(state, ins,  left << right);
5697         }
5698 }
5699
5700 static void simplify_usr(struct compile_state *state, struct triple *ins)
5701 {
5702         if (is_const(RHS(ins, 1))) {
5703                 ulong_t right;
5704                 right = read_const(state, ins, &RHS(ins, 1));
5705                 if (right >= (size_of(state, ins->type)*8)) {
5706                         warning(state, ins, "right shift count >= width of type");
5707                 }
5708         }
5709         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5710                 ulong_t left, right;
5711                 left  = read_const(state, ins, &RHS(ins, 0));
5712                 right = read_const(state, ins, &RHS(ins, 1));
5713                 mkconst(state, ins, left >> right);
5714         }
5715 }
5716
5717 static void simplify_ssr(struct compile_state *state, struct triple *ins)
5718 {
5719         if (is_const(RHS(ins, 1))) {
5720                 ulong_t right;
5721                 right = read_const(state, ins, &RHS(ins, 1));
5722                 if (right >= (size_of(state, ins->type)*8)) {
5723                         warning(state, ins, "right shift count >= width of type");
5724                 }
5725         }
5726         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5727                 long_t left, right;
5728                 left  = read_sconst(ins, &RHS(ins, 0));
5729                 right = read_sconst(ins, &RHS(ins, 1));
5730                 mkconst(state, ins, left >> right);
5731         }
5732 }
5733
5734 static void simplify_and(struct compile_state *state, struct triple *ins)
5735 {
5736         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5737                 ulong_t left, right;
5738                 left  = read_const(state, ins, &RHS(ins, 0));
5739                 right = read_const(state, ins, &RHS(ins, 1));
5740                 mkconst(state, ins, left & right);
5741         }
5742 }
5743
5744 static void simplify_or(struct compile_state *state, struct triple *ins)
5745 {
5746         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5747                 ulong_t left, right;
5748                 left  = read_const(state, ins, &RHS(ins, 0));
5749                 right = read_const(state, ins, &RHS(ins, 1));
5750                 mkconst(state, ins, left | right);
5751         }
5752 }
5753
5754 static void simplify_xor(struct compile_state *state, struct triple *ins)
5755 {
5756         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5757                 ulong_t left, right;
5758                 left  = read_const(state, ins, &RHS(ins, 0));
5759                 right = read_const(state, ins, &RHS(ins, 1));
5760                 mkconst(state, ins, left ^ right);
5761         }
5762 }
5763
5764 static void simplify_pos(struct compile_state *state, struct triple *ins)
5765 {
5766         if (is_const(RHS(ins, 0))) {
5767                 mkconst(state, ins, RHS(ins, 0)->u.cval);
5768         }
5769         else {
5770                 mkcopy(state, ins, RHS(ins, 0));
5771         }
5772 }
5773
5774 static void simplify_neg(struct compile_state *state, struct triple *ins)
5775 {
5776         if (is_const(RHS(ins, 0))) {
5777                 ulong_t left;
5778                 left = read_const(state, ins, &RHS(ins, 0));
5779                 mkconst(state, ins, -left);
5780         }
5781         else if (RHS(ins, 0)->op == OP_NEG) {
5782                 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
5783         }
5784 }
5785
5786 static void simplify_invert(struct compile_state *state, struct triple *ins)
5787 {
5788         if (is_const(RHS(ins, 0))) {
5789                 ulong_t left;
5790                 left = read_const(state, ins, &RHS(ins, 0));
5791                 mkconst(state, ins, ~left);
5792         }
5793 }
5794
5795 static void simplify_eq(struct compile_state *state, struct triple *ins)
5796 {
5797         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5798                 ulong_t left, right;
5799                 left  = read_const(state, ins, &RHS(ins, 0));
5800                 right = read_const(state, ins, &RHS(ins, 1));
5801                 mkconst(state, ins, left == right);
5802         }
5803         else if (RHS(ins, 0) == RHS(ins, 1)) {
5804                 mkconst(state, ins, 1);
5805         }
5806 }
5807
5808 static void simplify_noteq(struct compile_state *state, struct triple *ins)
5809 {
5810         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5811                 ulong_t left, right;
5812                 left  = read_const(state, ins, &RHS(ins, 0));
5813                 right = read_const(state, ins, &RHS(ins, 1));
5814                 mkconst(state, ins, left != right);
5815         }
5816         else if (RHS(ins, 0) == RHS(ins, 1)) {
5817                 mkconst(state, ins, 0);
5818         }
5819 }
5820
5821 static void simplify_sless(struct compile_state *state, struct triple *ins)
5822 {
5823         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5824                 long_t left, right;
5825                 left  = read_sconst(ins, &RHS(ins, 0));
5826                 right = read_sconst(ins, &RHS(ins, 1));
5827                 mkconst(state, ins, left < right);
5828         }
5829         else if (RHS(ins, 0) == RHS(ins, 1)) {
5830                 mkconst(state, ins, 0);
5831         }
5832 }
5833
5834 static void simplify_uless(struct compile_state *state, struct triple *ins)
5835 {
5836         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5837                 ulong_t left, right;
5838                 left  = read_const(state, ins, &RHS(ins, 0));
5839                 right = read_const(state, ins, &RHS(ins, 1));
5840                 mkconst(state, ins, left < right);
5841         }
5842         else if (is_zero(RHS(ins, 0))) {
5843                 mkconst(state, ins, 1);
5844         }
5845         else if (RHS(ins, 0) == RHS(ins, 1)) {
5846                 mkconst(state, ins, 0);
5847         }
5848 }
5849
5850 static void simplify_smore(struct compile_state *state, struct triple *ins)
5851 {
5852         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5853                 long_t left, right;
5854                 left  = read_sconst(ins, &RHS(ins, 0));
5855                 right = read_sconst(ins, &RHS(ins, 1));
5856                 mkconst(state, ins, left > right);
5857         }
5858         else if (RHS(ins, 0) == RHS(ins, 1)) {
5859                 mkconst(state, ins, 0);
5860         }
5861 }
5862
5863 static void simplify_umore(struct compile_state *state, struct triple *ins)
5864 {
5865         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5866                 ulong_t left, right;
5867                 left  = read_const(state, ins, &RHS(ins, 0));
5868                 right = read_const(state, ins, &RHS(ins, 1));
5869                 mkconst(state, ins, left > right);
5870         }
5871         else if (is_zero(RHS(ins, 1))) {
5872                 mkconst(state, ins, 1);
5873         }
5874         else if (RHS(ins, 0) == RHS(ins, 1)) {
5875                 mkconst(state, ins, 0);
5876         }
5877 }
5878
5879
5880 static void simplify_slesseq(struct compile_state *state, struct triple *ins)
5881 {
5882         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5883                 long_t left, right;
5884                 left  = read_sconst(ins, &RHS(ins, 0));
5885                 right = read_sconst(ins, &RHS(ins, 1));
5886                 mkconst(state, ins, left <= right);
5887         }
5888         else if (RHS(ins, 0) == RHS(ins, 1)) {
5889                 mkconst(state, ins, 1);
5890         }
5891 }
5892
5893 static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
5894 {
5895         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5896                 ulong_t left, right;
5897                 left  = read_const(state, ins, &RHS(ins, 0));
5898                 right = read_const(state, ins, &RHS(ins, 1));
5899                 mkconst(state, ins, left <= right);
5900         }
5901         else if (is_zero(RHS(ins, 0))) {
5902                 mkconst(state, ins, 1);
5903         }
5904         else if (RHS(ins, 0) == RHS(ins, 1)) {
5905                 mkconst(state, ins, 1);
5906         }
5907 }
5908
5909 static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
5910 {
5911         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 0))) {
5912                 long_t left, right;
5913                 left  = read_sconst(ins, &RHS(ins, 0));
5914                 right = read_sconst(ins, &RHS(ins, 1));
5915                 mkconst(state, ins, left >= right);
5916         }
5917         else if (RHS(ins, 0) == RHS(ins, 1)) {
5918                 mkconst(state, ins, 1);
5919         }
5920 }
5921
5922 static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
5923 {
5924         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5925                 ulong_t left, right;
5926                 left  = read_const(state, ins, &RHS(ins, 0));
5927                 right = read_const(state, ins, &RHS(ins, 1));
5928                 mkconst(state, ins, left >= right);
5929         }
5930         else if (is_zero(RHS(ins, 1))) {
5931                 mkconst(state, ins, 1);
5932         }
5933         else if (RHS(ins, 0) == RHS(ins, 1)) {
5934                 mkconst(state, ins, 1);
5935         }
5936 }
5937
5938 static void simplify_lfalse(struct compile_state *state, struct triple *ins)
5939 {
5940         if (is_const(RHS(ins, 0))) {
5941                 ulong_t left;
5942                 left = read_const(state, ins, &RHS(ins, 0));
5943                 mkconst(state, ins, left == 0);
5944         }
5945         /* Otherwise if I am the only user... */
5946         else if ((RHS(ins, 0)->use->member == ins) && (RHS(ins, 0)->use->next == 0)) {
5947                 int need_copy = 1;
5948                 /* Invert a boolean operation */
5949                 switch(RHS(ins, 0)->op) {
5950                 case OP_LTRUE:   RHS(ins, 0)->op = OP_LFALSE;  break;
5951                 case OP_LFALSE:  RHS(ins, 0)->op = OP_LTRUE;   break;
5952                 case OP_EQ:      RHS(ins, 0)->op = OP_NOTEQ;   break;
5953                 case OP_NOTEQ:   RHS(ins, 0)->op = OP_EQ;      break;
5954                 case OP_SLESS:   RHS(ins, 0)->op = OP_SMOREEQ; break;
5955                 case OP_ULESS:   RHS(ins, 0)->op = OP_UMOREEQ; break;
5956                 case OP_SMORE:   RHS(ins, 0)->op = OP_SLESSEQ; break;
5957                 case OP_UMORE:   RHS(ins, 0)->op = OP_ULESSEQ; break;
5958                 case OP_SLESSEQ: RHS(ins, 0)->op = OP_SMORE;   break;
5959                 case OP_ULESSEQ: RHS(ins, 0)->op = OP_UMORE;   break;
5960                 case OP_SMOREEQ: RHS(ins, 0)->op = OP_SLESS;   break;
5961                 case OP_UMOREEQ: RHS(ins, 0)->op = OP_ULESS;   break;
5962                 default:
5963                         need_copy = 0;
5964                         break;
5965                 }
5966                 if (need_copy) {
5967                         mkcopy(state, ins, RHS(ins, 0));
5968                 }
5969         }
5970 }
5971
5972 static void simplify_ltrue (struct compile_state *state, struct triple *ins)
5973 {
5974         if (is_const(RHS(ins, 0))) {
5975                 ulong_t left;
5976                 left = read_const(state, ins, &RHS(ins, 0));
5977                 mkconst(state, ins, left != 0);
5978         }
5979         else switch(RHS(ins, 0)->op) {
5980         case OP_LTRUE:   case OP_LFALSE:  case OP_EQ:      case OP_NOTEQ:
5981         case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
5982         case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
5983                 mkcopy(state, ins, RHS(ins, 0));
5984         }
5985
5986 }
5987
5988 static void simplify_copy(struct compile_state *state, struct triple *ins)
5989 {
5990         if (is_const(RHS(ins, 0))) {
5991                 switch(RHS(ins, 0)->op) {
5992                 case OP_INTCONST:
5993                 {
5994                         ulong_t left;
5995                         left = read_const(state, ins, &RHS(ins, 0));
5996                         mkconst(state, ins, left);
5997                         break;
5998                 }
5999                 case OP_ADDRCONST:
6000                 {
6001                         struct triple *sdecl;
6002                         ulong_t offset;
6003                         sdecl  = MISC(RHS(ins, 0), 0);
6004                         offset = RHS(ins, 0)->u.cval;
6005                         mkaddr_const(state, ins, sdecl, offset);
6006                         break;
6007                 }
6008                 default:
6009                         internal_error(state, ins, "uknown constant");
6010                         break;
6011                 }
6012         }
6013 }
6014
6015 static void simplify_branch(struct compile_state *state, struct triple *ins)
6016 {
6017         struct block *block;
6018         if (ins->op != OP_BRANCH) {
6019                 internal_error(state, ins, "not branch");
6020         }
6021         if (ins->use != 0) {
6022                 internal_error(state, ins, "branch use");
6023         }
6024 #warning "FIXME implement simplify branch."
6025         /* The challenge here with simplify branch is that I need to 
6026          * make modifications to the control flow graph as well
6027          * as to the branch instruction itself.
6028          */
6029         block = ins->u.block;
6030         
6031         if (TRIPLE_RHS(ins->sizes) && is_const(RHS(ins, 0))) {
6032                 struct triple *targ;
6033                 ulong_t value;
6034                 value = read_const(state, ins, &RHS(ins, 0));
6035                 unuse_triple(RHS(ins, 0), ins);
6036                 targ = TARG(ins, 0);
6037                 ins->sizes = TRIPLE_SIZES(0, 0, 0, 1);
6038                 if (value) {
6039                         unuse_triple(ins->next, ins);
6040                         TARG(ins, 0) = targ;
6041                 }
6042                 else {
6043                         unuse_triple(targ, ins);
6044                         TARG(ins, 0) = ins->next;
6045                 }
6046 #warning "FIXME handle the case of making a branch unconditional"
6047         }
6048         if (TARG(ins, 0) == ins->next) {
6049                 unuse_triple(ins->next, ins);
6050                 if (TRIPLE_RHS(ins->sizes)) {
6051                         unuse_triple(RHS(ins, 0), ins);
6052                         unuse_triple(ins->next, ins);
6053                 }
6054                 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
6055                 ins->op = OP_NOOP;
6056                 if (ins->use) {
6057                         internal_error(state, ins, "noop use != 0");
6058                 }
6059 #warning "FIXME handle the case of killing a branch"
6060         }
6061 }
6062
6063 static void simplify_phi(struct compile_state *state, struct triple *ins)
6064 {
6065         struct triple **expr;
6066         ulong_t value;
6067         expr = triple_rhs(state, ins, 0);
6068         if (!*expr || !is_const(*expr)) {
6069                 return;
6070         }
6071         value = read_const(state, ins, expr);
6072         for(;expr;expr = triple_rhs(state, ins, expr)) {
6073                 if (!*expr || !is_const(*expr)) {
6074                         return;
6075                 }
6076                 if (value != read_const(state, ins, expr)) {
6077                         return;
6078                 }
6079         }
6080         mkconst(state, ins, value);
6081 }
6082
6083
6084 static void simplify_bsf(struct compile_state *state, struct triple *ins)
6085 {
6086         if (is_const(RHS(ins, 0))) {
6087                 ulong_t left;
6088                 left = read_const(state, ins, &RHS(ins, 0));
6089                 mkconst(state, ins, bsf(left));
6090         }
6091 }
6092
6093 static void simplify_bsr(struct compile_state *state, struct triple *ins)
6094 {
6095         if (is_const(RHS(ins, 0))) {
6096                 ulong_t left;
6097                 left = read_const(state, ins, &RHS(ins, 0));
6098                 mkconst(state, ins, bsr(left));
6099         }
6100 }
6101
6102
6103 typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
6104 static const simplify_t table_simplify[] = {
6105 #if 0
6106 #define simplify_smul     simplify_noop
6107 #define simplify_umul     simplify_noop
6108 #define simplify_sdiv     simplify_noop
6109 #define simplify_udiv     simplify_noop
6110 #define simplify_smod     simplify_noop
6111 #define simplify_umod     simplify_noop
6112 #endif
6113 #if 0
6114 #define simplify_add      simplify_noop
6115 #define simplify_sub      simplify_noop
6116 #endif
6117 #if 0
6118 #define simplify_sl       simplify_noop
6119 #define simplify_usr      simplify_noop
6120 #define simplify_ssr      simplify_noop
6121 #endif
6122 #if 0
6123 #define simplify_and      simplify_noop
6124 #define simplify_xor      simplify_noop
6125 #define simplify_or       simplify_noop
6126 #endif
6127 #if 0
6128 #define simplify_pos      simplify_noop
6129 #define simplify_neg      simplify_noop
6130 #define simplify_invert   simplify_noop
6131 #endif
6132
6133 #if 0
6134 #define simplify_eq       simplify_noop
6135 #define simplify_noteq    simplify_noop
6136 #endif
6137 #if 0
6138 #define simplify_sless    simplify_noop
6139 #define simplify_uless    simplify_noop
6140 #define simplify_smore    simplify_noop
6141 #define simplify_umore    simplify_noop
6142 #endif
6143 #if 0
6144 #define simplify_slesseq  simplify_noop
6145 #define simplify_ulesseq  simplify_noop
6146 #define simplify_smoreeq  simplify_noop
6147 #define simplify_umoreeq  simplify_noop
6148 #endif
6149 #if 0
6150 #define simplify_lfalse   simplify_noop
6151 #endif
6152 #if 0
6153 #define simplify_ltrue    simplify_noop
6154 #endif
6155
6156 #if 0
6157 #define simplify_copy     simplify_noop
6158 #endif
6159
6160 #if 0
6161 #define simplify_branch   simplify_noop
6162 #endif
6163
6164 #if 0
6165 #define simplify_phi      simplify_noop
6166 #endif
6167
6168 #if 0
6169 #define simplify_bsf      simplify_noop
6170 #define simplify_bsr      simplify_noop
6171 #endif
6172
6173 [OP_SMUL       ] = simplify_smul,
6174 [OP_UMUL       ] = simplify_umul,
6175 [OP_SDIV       ] = simplify_sdiv,
6176 [OP_UDIV       ] = simplify_udiv,
6177 [OP_SMOD       ] = simplify_smod,
6178 [OP_UMOD       ] = simplify_umod,
6179 [OP_ADD        ] = simplify_add,
6180 [OP_SUB        ] = simplify_sub,
6181 [OP_SL         ] = simplify_sl,
6182 [OP_USR        ] = simplify_usr,
6183 [OP_SSR        ] = simplify_ssr,
6184 [OP_AND        ] = simplify_and,
6185 [OP_XOR        ] = simplify_xor,
6186 [OP_OR         ] = simplify_or,
6187 [OP_POS        ] = simplify_pos,
6188 [OP_NEG        ] = simplify_neg,
6189 [OP_INVERT     ] = simplify_invert,
6190
6191 [OP_EQ         ] = simplify_eq,
6192 [OP_NOTEQ      ] = simplify_noteq,
6193 [OP_SLESS      ] = simplify_sless,
6194 [OP_ULESS      ] = simplify_uless,
6195 [OP_SMORE      ] = simplify_smore,
6196 [OP_UMORE      ] = simplify_umore,
6197 [OP_SLESSEQ    ] = simplify_slesseq,
6198 [OP_ULESSEQ    ] = simplify_ulesseq,
6199 [OP_SMOREEQ    ] = simplify_smoreeq,
6200 [OP_UMOREEQ    ] = simplify_umoreeq,
6201 [OP_LFALSE     ] = simplify_lfalse,
6202 [OP_LTRUE      ] = simplify_ltrue,
6203
6204 [OP_LOAD       ] = simplify_noop,
6205 [OP_STORE      ] = simplify_noop,
6206
6207 [OP_NOOP       ] = simplify_noop,
6208
6209 [OP_INTCONST   ] = simplify_noop,
6210 [OP_BLOBCONST  ] = simplify_noop,
6211 [OP_ADDRCONST  ] = simplify_noop,
6212
6213 [OP_WRITE      ] = simplify_noop,
6214 [OP_READ       ] = simplify_noop,
6215 [OP_COPY       ] = simplify_copy,
6216 [OP_PIECE      ] = simplify_noop,
6217 [OP_ASM        ] = simplify_noop,
6218
6219 [OP_DOT        ] = simplify_noop,
6220 [OP_VAL_VEC    ] = simplify_noop,
6221
6222 [OP_LIST       ] = simplify_noop,
6223 [OP_BRANCH     ] = simplify_branch,
6224 [OP_LABEL      ] = simplify_noop,
6225 [OP_ADECL      ] = simplify_noop,
6226 [OP_SDECL      ] = simplify_noop,
6227 [OP_PHI        ] = simplify_phi,
6228
6229 [OP_INB        ] = simplify_noop,
6230 [OP_INW        ] = simplify_noop,
6231 [OP_INL        ] = simplify_noop,
6232 [OP_OUTB       ] = simplify_noop,
6233 [OP_OUTW       ] = simplify_noop,
6234 [OP_OUTL       ] = simplify_noop,
6235 [OP_BSF        ] = simplify_bsf,
6236 [OP_BSR        ] = simplify_bsr,
6237 [OP_RDMSR      ] = simplify_noop,
6238 [OP_WRMSR      ] = simplify_noop,                    
6239 [OP_HLT        ] = simplify_noop,
6240 };
6241
6242 static void simplify(struct compile_state *state, struct triple *ins)
6243 {
6244         int op;
6245         simplify_t do_simplify;
6246         do {
6247                 op = ins->op;
6248                 do_simplify = 0;
6249                 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
6250                         do_simplify = 0;
6251                 }
6252                 else {
6253                         do_simplify = table_simplify[op];
6254                 }
6255                 if (!do_simplify) {
6256                         internal_error(state, ins, "cannot simplify op: %d %s\n",
6257                                 op, tops(op));
6258                         return;
6259                 }
6260                 do_simplify(state, ins);
6261         } while(ins->op != op);
6262 }
6263
6264 static void simplify_all(struct compile_state *state)
6265 {
6266         struct triple *ins, *first;
6267         first = RHS(state->main_function, 0);
6268         ins = first;
6269         do {
6270                 simplify(state, ins);
6271                 ins = ins->next;
6272         } while(ins != first);
6273 }
6274
6275 /*
6276  * Builtins....
6277  * ============================
6278  */
6279
6280 static void register_builtin_function(struct compile_state *state,
6281         const char *name, int op, struct type *rtype, ...)
6282 {
6283         struct type *ftype, *atype, *param, **next;
6284         struct triple *def, *arg, *result, *work, *last, *first;
6285         struct hash_entry *ident;
6286         struct file_state file;
6287         int parameters;
6288         int name_len;
6289         va_list args;
6290         int i;
6291
6292         /* Dummy file state to get debug handling right */
6293         memset(&file, 0, sizeof(file));
6294         file.basename = name;
6295         file.line = 1;
6296         file.prev = state->file;
6297         state->file = &file;
6298
6299         /* Find the Parameter count */
6300         valid_op(state, op);
6301         parameters = table_ops[op].rhs;
6302         if (parameters < 0 ) {
6303                 internal_error(state, 0, "Invalid builtin parameter count");
6304         }
6305
6306         /* Find the function type */
6307         ftype = new_type(TYPE_FUNCTION, rtype, 0);
6308         next = &ftype->right;
6309         va_start(args, rtype);
6310         for(i = 0; i < parameters; i++) {
6311                 atype = va_arg(args, struct type *);
6312                 if (!*next) {
6313                         *next = atype;
6314                 } else {
6315                         *next = new_type(TYPE_PRODUCT, *next, atype);
6316                         next = &((*next)->right);
6317                 }
6318         }
6319         if (!*next) {
6320                 *next = &void_type;
6321         }
6322         va_end(args);
6323
6324         /* Generate the needed triples */
6325         def = triple(state, OP_LIST, ftype, 0, 0);
6326         first = label(state);
6327         RHS(def, 0) = first;
6328
6329         /* Now string them together */
6330         param = ftype->right;
6331         for(i = 0; i < parameters; i++) {
6332                 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6333                         atype = param->left;
6334                 } else {
6335                         atype = param;
6336                 }
6337                 arg = flatten(state, first, variable(state, atype));
6338                 param = param->right;
6339         }
6340         result = 0;
6341         if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
6342                 result = flatten(state, first, variable(state, rtype));
6343         }
6344         MISC(def, 0) = result;
6345         work = new_triple(state, op, rtype, -1, parameters);
6346         for(i = 0, arg = first->next; i < parameters; i++, arg = arg->next) {
6347                 RHS(work, i) = read_expr(state, arg);
6348         }
6349         if (result && ((rtype->type & TYPE_MASK) == TYPE_STRUCT)) {
6350                 struct triple *val;
6351                 /* Populate the LHS with the target registers */
6352                 work = flatten(state, first, work);
6353                 work->type = &void_type;
6354                 param = rtype->left;
6355                 if (rtype->elements != TRIPLE_LHS(work->sizes)) {
6356                         internal_error(state, 0, "Invalid result type");
6357                 }
6358                 val = new_triple(state, OP_VAL_VEC, rtype, -1, -1);
6359                 for(i = 0; i < rtype->elements; i++) {
6360                         struct triple *piece;
6361                         atype = param;
6362                         if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6363                                 atype = param->left;
6364                         }
6365                         if (!TYPE_ARITHMETIC(atype->type) &&
6366                                 !TYPE_PTR(atype->type)) {
6367                                 internal_error(state, 0, "Invalid lhs type");
6368                         }
6369                         piece = triple(state, OP_PIECE, atype, work, 0);
6370                         piece->u.cval = i;
6371                         LHS(work, i) = piece;
6372                         RHS(val, i) = piece;
6373                 }
6374                 work = val;
6375         }
6376         if (result) {
6377                 work = write_expr(state, result, work);
6378         }
6379         work = flatten(state, first, work);
6380         last = flatten(state, first, label(state));
6381         name_len = strlen(name);
6382         ident = lookup(state, name, name_len);
6383         symbol(state, ident, &ident->sym_ident, def, ftype);
6384         
6385         state->file = file.prev;
6386 #if 0
6387         fprintf(stdout, "\n");
6388         loc(stdout, state, 0);
6389         fprintf(stdout, "\n__________ builtin_function _________\n");
6390         print_triple(state, def);
6391         fprintf(stdout, "__________ builtin_function _________ done\n\n");
6392 #endif
6393 }
6394
6395 static struct type *partial_struct(struct compile_state *state,
6396         const char *field_name, struct type *type, struct type *rest)
6397 {
6398         struct hash_entry *field_ident;
6399         struct type *result;
6400         int field_name_len;
6401
6402         field_name_len = strlen(field_name);
6403         field_ident = lookup(state, field_name, field_name_len);
6404
6405         result = clone_type(0, type);
6406         result->field_ident = field_ident;
6407
6408         if (rest) {
6409                 result = new_type(TYPE_PRODUCT, result, rest);
6410         }
6411         return result;
6412 }
6413
6414 static struct type *register_builtin_type(struct compile_state *state,
6415         const char *name, struct type *type)
6416 {
6417         struct hash_entry *ident;
6418         int name_len;
6419
6420         name_len = strlen(name);
6421         ident = lookup(state, name, name_len);
6422         
6423         if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6424                 ulong_t elements = 0;
6425                 struct type *field;
6426                 type = new_type(TYPE_STRUCT, type, 0);
6427                 field = type->left;
6428                 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
6429                         elements++;
6430                         field = field->right;
6431                 }
6432                 elements++;
6433                 symbol(state, ident, &ident->sym_struct, 0, type);
6434                 type->type_ident = ident;
6435                 type->elements = elements;
6436         }
6437         symbol(state, ident, &ident->sym_ident, 0, type);
6438         ident->tok = TOK_TYPE_NAME;
6439         return type;
6440 }
6441
6442
6443 static void register_builtins(struct compile_state *state)
6444 {
6445         struct type *msr_type;
6446
6447         register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type, 
6448                 &ushort_type);
6449         register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
6450                 &ushort_type);
6451         register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,   
6452                 &ushort_type);
6453
6454         register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type, 
6455                 &uchar_type, &ushort_type);
6456         register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type, 
6457                 &ushort_type, &ushort_type);
6458         register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type, 
6459                 &uint_type, &ushort_type);
6460         
6461         register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type, 
6462                 &int_type);
6463         register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type, 
6464                 &int_type);
6465
6466         msr_type = register_builtin_type(state, "__builtin_msr_t",
6467                 partial_struct(state, "lo", &ulong_type,
6468                 partial_struct(state, "hi", &ulong_type, 0)));
6469
6470         register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
6471                 &ulong_type);
6472         register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
6473                 &ulong_type, &ulong_type, &ulong_type);
6474         
6475         register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type, 
6476                 &void_type);
6477 }
6478
6479 static struct type *declarator(
6480         struct compile_state *state, struct type *type, 
6481         struct hash_entry **ident, int need_ident);
6482 static void decl(struct compile_state *state, struct triple *first);
6483 static struct type *specifier_qualifier_list(struct compile_state *state);
6484 static int isdecl_specifier(int tok);
6485 static struct type *decl_specifiers(struct compile_state *state);
6486 static int istype(int tok);
6487 static struct triple *expr(struct compile_state *state);
6488 static struct triple *assignment_expr(struct compile_state *state);
6489 static struct type *type_name(struct compile_state *state);
6490 static void statement(struct compile_state *state, struct triple *fist);
6491
6492 static struct triple *call_expr(
6493         struct compile_state *state, struct triple *func)
6494 {
6495         struct triple *def;
6496         struct type *param, *type;
6497         ulong_t pvals, index;
6498
6499         if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
6500                 error(state, 0, "Called object is not a function");
6501         }
6502         if (func->op != OP_LIST) {
6503                 internal_error(state, 0, "improper function");
6504         }
6505         eat(state, TOK_LPAREN);
6506         /* Find the return type without any specifiers */
6507         type = clone_type(0, func->type->left);
6508         def = new_triple(state, OP_CALL, func->type, -1, -1);
6509         def->type = type;
6510
6511         pvals = TRIPLE_RHS(def->sizes);
6512         MISC(def, 0) = func;
6513
6514         param = func->type->right;
6515         for(index = 0; index < pvals; index++) {
6516                 struct triple *val;
6517                 struct type *arg_type;
6518                 val = read_expr(state, assignment_expr(state));
6519                 arg_type = param;
6520                 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6521                         arg_type = param->left;
6522                 }
6523                 write_compatible(state, arg_type, val->type);
6524                 RHS(def, index) = val;
6525                 if (index != (pvals - 1)) {
6526                         eat(state, TOK_COMMA);
6527                         param = param->right;
6528                 }
6529         }
6530         eat(state, TOK_RPAREN);
6531         return def;
6532 }
6533
6534
6535 static struct triple *character_constant(struct compile_state *state)
6536 {
6537         struct triple *def;
6538         struct token *tk;
6539         const signed char *str, *end;
6540         int c;
6541         int str_len;
6542         eat(state, TOK_LIT_CHAR);
6543         tk = &state->token[0];
6544         str = tk->val.str + 1;
6545         str_len = tk->str_len - 2;
6546         if (str_len <= 0) {
6547                 error(state, 0, "empty character constant");
6548         }
6549         end = str + str_len;
6550         c = char_value(state, &str, end);
6551         if (str != end) {
6552                 error(state, 0, "multibyte character constant not supported");
6553         }
6554         def = int_const(state, &char_type, (ulong_t)((long_t)c));
6555         return def;
6556 }
6557
6558 static struct triple *string_constant(struct compile_state *state)
6559 {
6560         struct triple *def;
6561         struct token *tk;
6562         struct type *type;
6563         const signed char *str, *end;
6564         signed char *buf, *ptr;
6565         int str_len;
6566
6567         buf = 0;
6568         type = new_type(TYPE_ARRAY, &char_type, 0);
6569         type->elements = 0;
6570         /* The while loop handles string concatenation */
6571         do {
6572                 eat(state, TOK_LIT_STRING);
6573                 tk = &state->token[0];
6574                 str = tk->val.str + 1;
6575                 str_len = tk->str_len - 2;
6576                 if (str_len < 0) {
6577                         error(state, 0, "negative string constant length");
6578                 }
6579                 end = str + str_len;
6580                 ptr = buf;
6581                 buf = xmalloc(type->elements + str_len + 1, "string_constant");
6582                 memcpy(buf, ptr, type->elements);
6583                 ptr = buf + type->elements;
6584                 do {
6585                         *ptr++ = char_value(state, &str, end);
6586                 } while(str < end);
6587                 type->elements = ptr - buf;
6588         } while(peek(state) == TOK_LIT_STRING);
6589         *ptr = '\0';
6590         type->elements += 1;
6591         def = triple(state, OP_BLOBCONST, type, 0, 0);
6592         def->u.blob = buf;
6593         return def;
6594 }
6595
6596
6597 static struct triple *integer_constant(struct compile_state *state)
6598 {
6599         struct triple *def;
6600         unsigned long val;
6601         struct token *tk;
6602         char *end;
6603         int u, l, decimal;
6604         struct type *type;
6605
6606         eat(state, TOK_LIT_INT);
6607         tk = &state->token[0];
6608         errno = 0;
6609         decimal = (tk->val.str[0] != '0');
6610         val = strtoul(tk->val.str, &end, 0);
6611         if ((val == ULONG_MAX) && (errno == ERANGE)) {
6612                 error(state, 0, "Integer constant to large");
6613         }
6614         u = l = 0;
6615         if ((*end == 'u') || (*end == 'U')) {
6616                 u = 1;
6617                         end++;
6618         }
6619         if ((*end == 'l') || (*end == 'L')) {
6620                 l = 1;
6621                 end++;
6622         }
6623         if ((*end == 'u') || (*end == 'U')) {
6624                 u = 1;
6625                 end++;
6626         }
6627         if (*end) {
6628                 error(state, 0, "Junk at end of integer constant");
6629         }
6630         if (u && l)  {
6631                 type = &ulong_type;
6632         }
6633         else if (l) {
6634                 type = &long_type;
6635                 if (!decimal && (val > LONG_MAX)) {
6636                         type = &ulong_type;
6637                 }
6638         }
6639         else if (u) {
6640                 type = &uint_type;
6641                 if (val > UINT_MAX) {
6642                         type = &ulong_type;
6643                 }
6644         }
6645         else {
6646                 type = &int_type;
6647                 if (!decimal && (val > INT_MAX) && (val <= UINT_MAX)) {
6648                         type = &uint_type;
6649                 }
6650                 else if (!decimal && (val > LONG_MAX)) {
6651                         type = &ulong_type;
6652                 }
6653                 else if (val > INT_MAX) {
6654                         type = &long_type;
6655                 }
6656         }
6657         def = int_const(state, type, val);
6658         return def;
6659 }
6660
6661 static struct triple *primary_expr(struct compile_state *state)
6662 {
6663         struct triple *def;
6664         int tok;
6665         tok = peek(state);
6666         switch(tok) {
6667         case TOK_IDENT:
6668         {
6669                 struct hash_entry *ident;
6670                 /* Here ident is either:
6671                  * a varable name
6672                  * a function name
6673                  * an enumeration constant.
6674                  */
6675                 eat(state, TOK_IDENT);
6676                 ident = state->token[0].ident;
6677                 if (!ident->sym_ident) {
6678                         error(state, 0, "%s undeclared", ident->name);
6679                 }
6680                 def = ident->sym_ident->def;
6681                 break;
6682         }
6683         case TOK_ENUM_CONST:
6684                 /* Here ident is an enumeration constant */
6685                 eat(state, TOK_ENUM_CONST);
6686                 def = 0;
6687                 FINISHME();
6688                 break;
6689         case TOK_LPAREN:
6690                 eat(state, TOK_LPAREN);
6691                 def = expr(state);
6692                 eat(state, TOK_RPAREN);
6693                 break;
6694         case TOK_LIT_INT:
6695                 def = integer_constant(state);
6696                 break;
6697         case TOK_LIT_FLOAT:
6698                 eat(state, TOK_LIT_FLOAT);
6699                 error(state, 0, "Floating point constants not supported");
6700                 def = 0;
6701                 FINISHME();
6702                 break;
6703         case TOK_LIT_CHAR:
6704                 def = character_constant(state);
6705                 break;
6706         case TOK_LIT_STRING:
6707                 def = string_constant(state);
6708                 break;
6709         default:
6710                 def = 0;
6711                 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
6712         }
6713         return def;
6714 }
6715
6716 static struct triple *postfix_expr(struct compile_state *state)
6717 {
6718         struct triple *def;
6719         int postfix;
6720         def = primary_expr(state);
6721         do {
6722                 struct triple *left;
6723                 int tok;
6724                 postfix = 1;
6725                 left = def;
6726                 switch((tok = peek(state))) {
6727                 case TOK_LBRACKET:
6728                         eat(state, TOK_LBRACKET);
6729                         def = mk_subscript_expr(state, left, expr(state));
6730                         eat(state, TOK_RBRACKET);
6731                         break;
6732                 case TOK_LPAREN:
6733                         def = call_expr(state, def);
6734                         break;
6735                 case TOK_DOT:
6736                 {
6737                         struct hash_entry *field;
6738                         eat(state, TOK_DOT);
6739                         eat(state, TOK_IDENT);
6740                         field = state->token[0].ident;
6741                         def = deref_field(state, def, field);
6742                         break;
6743                 }
6744                 case TOK_ARROW:
6745                 {
6746                         struct hash_entry *field;
6747                         eat(state, TOK_ARROW);
6748                         eat(state, TOK_IDENT);
6749                         field = state->token[0].ident;
6750                         def = mk_deref_expr(state, read_expr(state, def));
6751                         def = deref_field(state, def, field);
6752                         break;
6753                 }
6754                 case TOK_PLUSPLUS:
6755                         eat(state, TOK_PLUSPLUS);
6756                         def = mk_post_inc_expr(state, left);
6757                         break;
6758                 case TOK_MINUSMINUS:
6759                         eat(state, TOK_MINUSMINUS);
6760                         def = mk_post_dec_expr(state, left);
6761                         break;
6762                 default:
6763                         postfix = 0;
6764                         break;
6765                 }
6766         } while(postfix);
6767         return def;
6768 }
6769
6770 static struct triple *cast_expr(struct compile_state *state);
6771
6772 static struct triple *unary_expr(struct compile_state *state)
6773 {
6774         struct triple *def, *right;
6775         int tok;
6776         switch((tok = peek(state))) {
6777         case TOK_PLUSPLUS:
6778                 eat(state, TOK_PLUSPLUS);
6779                 def = mk_pre_inc_expr(state, unary_expr(state));
6780                 break;
6781         case TOK_MINUSMINUS:
6782                 eat(state, TOK_MINUSMINUS);
6783                 def = mk_pre_dec_expr(state, unary_expr(state));
6784                 break;
6785         case TOK_AND:
6786                 eat(state, TOK_AND);
6787                 def = mk_addr_expr(state, cast_expr(state), 0);
6788                 break;
6789         case TOK_STAR:
6790                 eat(state, TOK_STAR);
6791                 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
6792                 break;
6793         case TOK_PLUS:
6794                 eat(state, TOK_PLUS);
6795                 right = read_expr(state, cast_expr(state));
6796                 arithmetic(state, right);
6797                 def = integral_promotion(state, right);
6798                 break;
6799         case TOK_MINUS:
6800                 eat(state, TOK_MINUS);
6801                 right = read_expr(state, cast_expr(state));
6802                 arithmetic(state, right);
6803                 def = integral_promotion(state, right);
6804                 def = triple(state, OP_NEG, def->type, def, 0);
6805                 break;
6806         case TOK_TILDE:
6807                 eat(state, TOK_TILDE);
6808                 right = read_expr(state, cast_expr(state));
6809                 integral(state, right);
6810                 def = integral_promotion(state, right);
6811                 def = triple(state, OP_INVERT, def->type, def, 0);
6812                 break;
6813         case TOK_BANG:
6814                 eat(state, TOK_BANG);
6815                 right = read_expr(state, cast_expr(state));
6816                 bool(state, right);
6817                 def = lfalse_expr(state, right);
6818                 break;
6819         case TOK_SIZEOF:
6820         {
6821                 struct type *type;
6822                 int tok1, tok2;
6823                 eat(state, TOK_SIZEOF);
6824                 tok1 = peek(state);
6825                 tok2 = peek2(state);
6826                 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
6827                         eat(state, TOK_LPAREN);
6828                         type = type_name(state);
6829                         eat(state, TOK_RPAREN);
6830                 }
6831                 else {
6832                         struct triple *expr;
6833                         expr = unary_expr(state);
6834                         type = expr->type;
6835                         release_expr(state, expr);
6836                 }
6837                 def = int_const(state, &ulong_type, size_of(state, type));
6838                 break;
6839         }
6840         case TOK_ALIGNOF:
6841         {
6842                 struct type *type;
6843                 int tok1, tok2;
6844                 eat(state, TOK_ALIGNOF);
6845                 tok1 = peek(state);
6846                 tok2 = peek2(state);
6847                 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
6848                         eat(state, TOK_LPAREN);
6849                         type = type_name(state);
6850                         eat(state, TOK_RPAREN);
6851                 }
6852                 else {
6853                         struct triple *expr;
6854                         expr = unary_expr(state);
6855                         type = expr->type;
6856                         release_expr(state, expr);
6857                 }
6858                 def = int_const(state, &ulong_type, align_of(state, type));
6859                 break;
6860         }
6861         default:
6862                 def = postfix_expr(state);
6863                 break;
6864         }
6865         return def;
6866 }
6867
6868 static struct triple *cast_expr(struct compile_state *state)
6869 {
6870         struct triple *def;
6871         int tok1, tok2;
6872         tok1 = peek(state);
6873         tok2 = peek2(state);
6874         if ((tok1 == TOK_LPAREN) && istype(tok2)) {
6875                 struct type *type;
6876                 eat(state, TOK_LPAREN);
6877                 type = type_name(state);
6878                 eat(state, TOK_RPAREN);
6879                 def = read_expr(state, cast_expr(state));
6880                 def = triple(state, OP_COPY, type, def, 0);
6881         }
6882         else {
6883                 def = unary_expr(state);
6884         }
6885         return def;
6886 }
6887
6888 static struct triple *mult_expr(struct compile_state *state)
6889 {
6890         struct triple *def;
6891         int done;
6892         def = cast_expr(state);
6893         do {
6894                 struct triple *left, *right;
6895                 struct type *result_type;
6896                 int tok, op, sign;
6897                 done = 0;
6898                 switch(tok = (peek(state))) {
6899                 case TOK_STAR:
6900                 case TOK_DIV:
6901                 case TOK_MOD:
6902                         left = read_expr(state, def);
6903                         arithmetic(state, left);
6904
6905                         eat(state, tok);
6906
6907                         right = read_expr(state, cast_expr(state));
6908                         arithmetic(state, right);
6909
6910                         result_type = arithmetic_result(state, left, right);
6911                         sign = is_signed(result_type);
6912                         op = -1;
6913                         switch(tok) {
6914                         case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
6915                         case TOK_DIV:  op = sign? OP_SDIV : OP_UDIV; break;
6916                         case TOK_MOD:  op = sign? OP_SMOD : OP_UMOD; break;
6917                         }
6918                         def = triple(state, op, result_type, left, right);
6919                         break;
6920                 default:
6921                         done = 1;
6922                         break;
6923                 }
6924         } while(!done);
6925         return def;
6926 }
6927
6928 static struct triple *add_expr(struct compile_state *state)
6929 {
6930         struct triple *def;
6931         int done;
6932         def = mult_expr(state);
6933         do {
6934                 done = 0;
6935                 switch( peek(state)) {
6936                 case TOK_PLUS:
6937                         eat(state, TOK_PLUS);
6938                         def = mk_add_expr(state, def, mult_expr(state));
6939                         break;
6940                 case TOK_MINUS:
6941                         eat(state, TOK_MINUS);
6942                         def = mk_sub_expr(state, def, mult_expr(state));
6943                         break;
6944                 default:
6945                         done = 1;
6946                         break;
6947                 }
6948         } while(!done);
6949         return def;
6950 }
6951
6952 static struct triple *shift_expr(struct compile_state *state)
6953 {
6954         struct triple *def;
6955         int done;
6956         def = add_expr(state);
6957         do {
6958                 struct triple *left, *right;
6959                 int tok, op;
6960                 done = 0;
6961                 switch((tok = peek(state))) {
6962                 case TOK_SL:
6963                 case TOK_SR:
6964                         left = read_expr(state, def);
6965                         integral(state, left);
6966                         left = integral_promotion(state, left);
6967
6968                         eat(state, tok);
6969
6970                         right = read_expr(state, add_expr(state));
6971                         integral(state, right);
6972                         right = integral_promotion(state, right);
6973                         
6974                         op = (tok == TOK_SL)? OP_SL : 
6975                                 is_signed(left->type)? OP_SSR: OP_USR;
6976
6977                         def = triple(state, op, left->type, left, right);
6978                         break;
6979                 default:
6980                         done = 1;
6981                         break;
6982                 }
6983         } while(!done);
6984         return def;
6985 }
6986
6987 static struct triple *relational_expr(struct compile_state *state)
6988 {
6989 #warning "Extend relational exprs to work on more than arithmetic types"
6990         struct triple *def;
6991         int done;
6992         def = shift_expr(state);
6993         do {
6994                 struct triple *left, *right;
6995                 struct type *arg_type;
6996                 int tok, op, sign;
6997                 done = 0;
6998                 switch((tok = peek(state))) {
6999                 case TOK_LESS:
7000                 case TOK_MORE:
7001                 case TOK_LESSEQ:
7002                 case TOK_MOREEQ:
7003                         left = read_expr(state, def);
7004                         arithmetic(state, left);
7005
7006                         eat(state, tok);
7007
7008                         right = read_expr(state, shift_expr(state));
7009                         arithmetic(state, right);
7010
7011                         arg_type = arithmetic_result(state, left, right);
7012                         sign = is_signed(arg_type);
7013                         op = -1;
7014                         switch(tok) {
7015                         case TOK_LESS:   op = sign? OP_SLESS : OP_ULESS; break;
7016                         case TOK_MORE:   op = sign? OP_SMORE : OP_UMORE; break;
7017                         case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
7018                         case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
7019                         }
7020                         def = triple(state, op, &int_type, left, right);
7021                         break;
7022                 default:
7023                         done = 1;
7024                         break;
7025                 }
7026         } while(!done);
7027         return def;
7028 }
7029
7030 static struct triple *equality_expr(struct compile_state *state)
7031 {
7032 #warning "Extend equality exprs to work on more than arithmetic types"
7033         struct triple *def;
7034         int done;
7035         def = relational_expr(state);
7036         do {
7037                 struct triple *left, *right;
7038                 int tok, op;
7039                 done = 0;
7040                 switch((tok = peek(state))) {
7041                 case TOK_EQEQ:
7042                 case TOK_NOTEQ:
7043                         left = read_expr(state, def);
7044                         arithmetic(state, left);
7045                         eat(state, tok);
7046                         right = read_expr(state, relational_expr(state));
7047                         arithmetic(state, right);
7048                         op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
7049                         def = triple(state, op, &int_type, left, right);
7050                         break;
7051                 default:
7052                         done = 1;
7053                         break;
7054                 }
7055         } while(!done);
7056         return def;
7057 }
7058
7059 static struct triple *and_expr(struct compile_state *state)
7060 {
7061         struct triple *def;
7062         def = equality_expr(state);
7063         while(peek(state) == TOK_AND) {
7064                 struct triple *left, *right;
7065                 struct type *result_type;
7066                 left = read_expr(state, def);
7067                 integral(state, left);
7068                 eat(state, TOK_AND);
7069                 right = read_expr(state, equality_expr(state));
7070                 integral(state, right);
7071                 result_type = arithmetic_result(state, left, right);
7072                 def = triple(state, OP_AND, result_type, left, right);
7073         }
7074         return def;
7075 }
7076
7077 static struct triple *xor_expr(struct compile_state *state)
7078 {
7079         struct triple *def;
7080         def = and_expr(state);
7081         while(peek(state) == TOK_XOR) {
7082                 struct triple *left, *right;
7083                 struct type *result_type;
7084                 left = read_expr(state, def);
7085                 integral(state, left);
7086                 eat(state, TOK_XOR);
7087                 right = read_expr(state, and_expr(state));
7088                 integral(state, right);
7089                 result_type = arithmetic_result(state, left, right);
7090                 def = triple(state, OP_XOR, result_type, left, right);
7091         }
7092         return def;
7093 }
7094
7095 static struct triple *or_expr(struct compile_state *state)
7096 {
7097         struct triple *def;
7098         def = xor_expr(state);
7099         while(peek(state) == TOK_OR) {
7100                 struct triple *left, *right;
7101                 struct type *result_type;
7102                 left = read_expr(state, def);
7103                 integral(state, left);
7104                 eat(state, TOK_OR);
7105                 right = read_expr(state, xor_expr(state));
7106                 integral(state, right);
7107                 result_type = arithmetic_result(state, left, right);
7108                 def = triple(state, OP_OR, result_type, left, right);
7109         }
7110         return def;
7111 }
7112
7113 static struct triple *land_expr(struct compile_state *state)
7114 {
7115         struct triple *def;
7116         def = or_expr(state);
7117         while(peek(state) == TOK_LOGAND) {
7118                 struct triple *left, *right;
7119                 left = read_expr(state, def);
7120                 bool(state, left);
7121                 eat(state, TOK_LOGAND);
7122                 right = read_expr(state, or_expr(state));
7123                 bool(state, right);
7124
7125                 def = triple(state, OP_LAND, &int_type,
7126                         ltrue_expr(state, left),
7127                         ltrue_expr(state, right));
7128         }
7129         return def;
7130 }
7131
7132 static struct triple *lor_expr(struct compile_state *state)
7133 {
7134         struct triple *def;
7135         def = land_expr(state);
7136         while(peek(state) == TOK_LOGOR) {
7137                 struct triple *left, *right;
7138                 left = read_expr(state, def);
7139                 bool(state, left);
7140                 eat(state, TOK_LOGOR);
7141                 right = read_expr(state, land_expr(state));
7142                 bool(state, right);
7143                 
7144                 def = triple(state, OP_LOR, &int_type,
7145                         ltrue_expr(state, left),
7146                         ltrue_expr(state, right));
7147         }
7148         return def;
7149 }
7150
7151 static struct triple *conditional_expr(struct compile_state *state)
7152 {
7153         struct triple *def;
7154         def = lor_expr(state);
7155         if (peek(state) == TOK_QUEST) {
7156                 struct triple *test, *left, *right;
7157                 bool(state, def);
7158                 test = ltrue_expr(state, read_expr(state, def));
7159                 eat(state, TOK_QUEST);
7160                 left = read_expr(state, expr(state));
7161                 eat(state, TOK_COLON);
7162                 right = read_expr(state, conditional_expr(state));
7163
7164                 def = cond_expr(state, test, left, right);
7165         }
7166         return def;
7167 }
7168
7169 static struct triple *eval_const_expr(
7170         struct compile_state *state, struct triple *expr)
7171 {
7172         struct triple *def;
7173         struct triple *head, *ptr;
7174         head = label(state); /* dummy initial triple */
7175         flatten(state, head, expr);
7176         for(ptr = head->next; ptr != head; ptr = ptr->next) {
7177                 simplify(state, ptr);
7178         }
7179         /* Remove the constant value the tail of the list */
7180         def = head->prev;
7181         def->prev->next = def->next;
7182         def->next->prev = def->prev;
7183         def->next = def->prev = def;
7184         if (!is_const(def)) {
7185                 internal_error(state, 0, "Not a constant expression");
7186         }
7187         /* Free the intermediate expressions */
7188         while(head->next != head) {
7189                 release_triple(state, head->next);
7190         }
7191         free_triple(state, head);
7192         return def;
7193 }
7194
7195 static struct triple *constant_expr(struct compile_state *state)
7196 {
7197         return eval_const_expr(state, conditional_expr(state));
7198 }
7199
7200 static struct triple *assignment_expr(struct compile_state *state)
7201 {
7202         struct triple *def, *left, *right;
7203         int tok, op, sign;
7204         /* The C grammer in K&R shows assignment expressions
7205          * only taking unary expressions as input on their
7206          * left hand side.  But specifies the precedence of
7207          * assignemnt as the lowest operator except for comma.
7208          *
7209          * Allowing conditional expressions on the left hand side
7210          * of an assignement results in a grammar that accepts
7211          * a larger set of statements than standard C.   As long
7212          * as the subset of the grammar that is standard C behaves
7213          * correctly this should cause no problems.
7214          * 
7215          * For the extra token strings accepted by the grammar
7216          * none of them should produce a valid lvalue, so they
7217          * should not produce functioning programs.
7218          *
7219          * GCC has this bug as well, so surprises should be minimal.
7220          */
7221         def = conditional_expr(state);
7222         left = def;
7223         switch((tok = peek(state))) {
7224         case TOK_EQ:
7225                 lvalue(state, left);
7226                 eat(state, TOK_EQ);
7227                 def = write_expr(state, left, 
7228                         read_expr(state, assignment_expr(state)));
7229                 break;
7230         case TOK_TIMESEQ:
7231         case TOK_DIVEQ:
7232         case TOK_MODEQ:
7233                 lvalue(state, left);
7234                 arithmetic(state, left);
7235                 eat(state, tok);
7236                 right = read_expr(state, assignment_expr(state));
7237                 arithmetic(state, right);
7238
7239                 sign = is_signed(left->type);
7240                 op = -1;
7241                 switch(tok) {
7242                 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
7243                 case TOK_DIVEQ:   op = sign? OP_SDIV : OP_UDIV; break;
7244                 case TOK_MODEQ:   op = sign? OP_SMOD : OP_UMOD; break;
7245                 }
7246                 def = write_expr(state, left,
7247                         triple(state, op, left->type, 
7248                                 read_expr(state, left), right));
7249                 break;
7250         case TOK_PLUSEQ:
7251                 lvalue(state, left);
7252                 eat(state, TOK_PLUSEQ);
7253                 def = write_expr(state, left,
7254                         mk_add_expr(state, left, assignment_expr(state)));
7255                 break;
7256         case TOK_MINUSEQ:
7257                 lvalue(state, left);
7258                 eat(state, TOK_MINUSEQ);
7259                 def = write_expr(state, left,
7260                         mk_sub_expr(state, left, assignment_expr(state)));
7261                 break;
7262         case TOK_SLEQ:
7263         case TOK_SREQ:
7264         case TOK_ANDEQ:
7265         case TOK_XOREQ:
7266         case TOK_OREQ:
7267                 lvalue(state, left);
7268                 integral(state, left);
7269                 eat(state, tok);
7270                 right = read_expr(state, assignment_expr(state));
7271                 integral(state, right);
7272                 right = integral_promotion(state, right);
7273                 sign = is_signed(left->type);
7274                 op = -1;
7275                 switch(tok) {
7276                 case TOK_SLEQ:  op = OP_SL; break;
7277                 case TOK_SREQ:  op = sign? OP_SSR: OP_USR; break;
7278                 case TOK_ANDEQ: op = OP_AND; break;
7279                 case TOK_XOREQ: op = OP_XOR; break;
7280                 case TOK_OREQ:  op = OP_OR; break;
7281                 }
7282                 def = write_expr(state, left,
7283                         triple(state, op, left->type, 
7284                                 read_expr(state, left), right));
7285                 break;
7286         }
7287         return def;
7288 }
7289
7290 static struct triple *expr(struct compile_state *state)
7291 {
7292         struct triple *def;
7293         def = assignment_expr(state);
7294         while(peek(state) == TOK_COMMA) {
7295                 struct triple *left, *right;
7296                 left = def;
7297                 eat(state, TOK_COMMA);
7298                 right = assignment_expr(state);
7299                 def = triple(state, OP_COMMA, right->type, left, right);
7300         }
7301         return def;
7302 }
7303
7304 static void expr_statement(struct compile_state *state, struct triple *first)
7305 {
7306         if (peek(state) != TOK_SEMI) {
7307                 flatten(state, first, expr(state));
7308         }
7309         eat(state, TOK_SEMI);
7310 }
7311
7312 static void if_statement(struct compile_state *state, struct triple *first)
7313 {
7314         struct triple *test, *jmp1, *jmp2, *middle, *end;
7315
7316         jmp1 = jmp2 = middle = 0;
7317         eat(state, TOK_IF);
7318         eat(state, TOK_LPAREN);
7319         test = expr(state);
7320         bool(state, test);
7321         /* Cleanup and invert the test */
7322         test = lfalse_expr(state, read_expr(state, test));
7323         eat(state, TOK_RPAREN);
7324         /* Generate the needed pieces */
7325         middle = label(state);
7326         jmp1 = branch(state, middle, test);
7327         /* Thread the pieces together */
7328         flatten(state, first, test);
7329         flatten(state, first, jmp1);
7330         flatten(state, first, label(state));
7331         statement(state, first);
7332         if (peek(state) == TOK_ELSE) {
7333                 eat(state, TOK_ELSE);
7334                 /* Generate the rest of the pieces */
7335                 end = label(state);
7336                 jmp2 = branch(state, end, 0);
7337                 /* Thread them together */
7338                 flatten(state, first, jmp2);
7339                 flatten(state, first, middle);
7340                 statement(state, first);
7341                 flatten(state, first, end);
7342         }
7343         else {
7344                 flatten(state, first, middle);
7345         }
7346 }
7347
7348 static void for_statement(struct compile_state *state, struct triple *first)
7349 {
7350         struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
7351         struct triple *label1, *label2, *label3;
7352         struct hash_entry *ident;
7353
7354         eat(state, TOK_FOR);
7355         eat(state, TOK_LPAREN);
7356         head = test = tail = jmp1 = jmp2 = 0;
7357         if (peek(state) != TOK_SEMI) {
7358                 head = expr(state);
7359         } 
7360         eat(state, TOK_SEMI);
7361         if (peek(state) != TOK_SEMI) {
7362                 test = expr(state);
7363                 bool(state, test);
7364                 test = ltrue_expr(state, read_expr(state, test));
7365         }
7366         eat(state, TOK_SEMI);
7367         if (peek(state) != TOK_RPAREN) {
7368                 tail = expr(state);
7369         }
7370         eat(state, TOK_RPAREN);
7371         /* Generate the needed pieces */
7372         label1 = label(state);
7373         label2 = label(state);
7374         label3 = label(state);
7375         if (test) {
7376                 jmp1 = branch(state, label3, 0);
7377                 jmp2 = branch(state, label1, test);
7378         }
7379         else {
7380                 jmp2 = branch(state, label1, 0);
7381         }
7382         end = label(state);
7383         /* Remember where break and continue go */
7384         start_scope(state);
7385         ident = state->i_break;
7386         symbol(state, ident, &ident->sym_ident, end, end->type);
7387         ident = state->i_continue;
7388         symbol(state, ident, &ident->sym_ident, label2, label2->type);
7389         /* Now include the body */
7390         flatten(state, first, head);
7391         flatten(state, first, jmp1);
7392         flatten(state, first, label1);
7393         statement(state, first);
7394         flatten(state, first, label2);
7395         flatten(state, first, tail);
7396         flatten(state, first, label3);
7397         flatten(state, first, test);
7398         flatten(state, first, jmp2);
7399         flatten(state, first, end);
7400         /* Cleanup the break/continue scope */
7401         end_scope(state);
7402 }
7403
7404 static void while_statement(struct compile_state *state, struct triple *first)
7405 {
7406         struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
7407         struct hash_entry *ident;
7408         eat(state, TOK_WHILE);
7409         eat(state, TOK_LPAREN);
7410         test = expr(state);
7411         bool(state, test);
7412         test = ltrue_expr(state, read_expr(state, test));
7413         eat(state, TOK_RPAREN);
7414         /* Generate the needed pieces */
7415         label1 = label(state);
7416         label2 = label(state);
7417         jmp1 = branch(state, label2, 0);
7418         jmp2 = branch(state, label1, test);
7419         end = label(state);
7420         /* Remember where break and continue go */
7421         start_scope(state);
7422         ident = state->i_break;
7423         symbol(state, ident, &ident->sym_ident, end, end->type);
7424         ident = state->i_continue;
7425         symbol(state, ident, &ident->sym_ident, label2, label2->type);
7426         /* Thread them together */
7427         flatten(state, first, jmp1);
7428         flatten(state, first, label1);
7429         statement(state, first);
7430         flatten(state, first, label2);
7431         flatten(state, first, test);
7432         flatten(state, first, jmp2);
7433         flatten(state, first, end);
7434         /* Cleanup the break/continue scope */
7435         end_scope(state);
7436 }
7437
7438 static void do_statement(struct compile_state *state, struct triple *first)
7439 {
7440         struct triple *label1, *label2, *test, *end;
7441         struct hash_entry *ident;
7442         eat(state, TOK_DO);
7443         /* Generate the needed pieces */
7444         label1 = label(state);
7445         label2 = label(state);
7446         end = label(state);
7447         /* Remember where break and continue go */
7448         start_scope(state);
7449         ident = state->i_break;
7450         symbol(state, ident, &ident->sym_ident, end, end->type);
7451         ident = state->i_continue;
7452         symbol(state, ident, &ident->sym_ident, label2, label2->type);
7453         /* Now include the body */
7454         flatten(state, first, label1);
7455         statement(state, first);
7456         /* Cleanup the break/continue scope */
7457         end_scope(state);
7458         /* Eat the rest of the loop */
7459         eat(state, TOK_WHILE);
7460         eat(state, TOK_LPAREN);
7461         test = read_expr(state, expr(state));
7462         bool(state, test);
7463         eat(state, TOK_RPAREN);
7464         eat(state, TOK_SEMI);
7465         /* Thread the pieces together */
7466         test = ltrue_expr(state, test);
7467         flatten(state, first, label2);
7468         flatten(state, first, test);
7469         flatten(state, first, branch(state, label1, test));
7470         flatten(state, first, end);
7471 }
7472
7473
7474 static void return_statement(struct compile_state *state, struct triple *first)
7475 {
7476         struct triple *jmp, *mv, *dest, *var, *val;
7477         int last;
7478         eat(state, TOK_RETURN);
7479
7480 #warning "FIXME implement a more general excess branch elimination"
7481         val = 0;
7482         /* If we have a return value do some more work */
7483         if (peek(state) != TOK_SEMI) {
7484                 val = read_expr(state, expr(state));
7485         }
7486         eat(state, TOK_SEMI);
7487
7488         /* See if this last statement in a function */
7489         last = ((peek(state) == TOK_RBRACE) && 
7490                 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
7491
7492         /* Find the return variable */
7493         var = MISC(state->main_function, 0);
7494         /* Find the return destination */
7495         dest = RHS(state->main_function, 0)->prev;
7496         mv = jmp = 0;
7497         /* If needed generate a jump instruction */
7498         if (!last) {
7499                 jmp = branch(state, dest, 0);
7500         }
7501         /* If needed generate an assignment instruction */
7502         if (val) {
7503                 mv = write_expr(state, var, val);
7504         }
7505         /* Now put the code together */
7506         if (mv) {
7507                 flatten(state, first, mv);
7508                 flatten(state, first, jmp);
7509         }
7510         else if (jmp) {
7511                 flatten(state, first, jmp);
7512         }
7513 }
7514
7515 static void break_statement(struct compile_state *state, struct triple *first)
7516 {
7517         struct triple *dest;
7518         eat(state, TOK_BREAK);
7519         eat(state, TOK_SEMI);
7520         if (!state->i_break->sym_ident) {
7521                 error(state, 0, "break statement not within loop or switch");
7522         }
7523         dest = state->i_break->sym_ident->def;
7524         flatten(state, first, branch(state, dest, 0));
7525 }
7526
7527 static void continue_statement(struct compile_state *state, struct triple *first)
7528 {
7529         struct triple *dest;
7530         eat(state, TOK_CONTINUE);
7531         eat(state, TOK_SEMI);
7532         if (!state->i_continue->sym_ident) {
7533                 error(state, 0, "continue statement outside of a loop");
7534         }
7535         dest = state->i_continue->sym_ident->def;
7536         flatten(state, first, branch(state, dest, 0));
7537 }
7538
7539 static void goto_statement(struct compile_state *state, struct triple *first)
7540 {
7541         FINISHME();
7542         eat(state, TOK_GOTO);
7543         eat(state, TOK_IDENT);
7544         eat(state, TOK_SEMI);
7545         error(state, 0, "goto is not implemeted");
7546         FINISHME();
7547 }
7548
7549 static void labeled_statement(struct compile_state *state, struct triple *first)
7550 {
7551         FINISHME();
7552         eat(state, TOK_IDENT);
7553         eat(state, TOK_COLON);
7554         statement(state, first);
7555         error(state, 0, "labeled statements are not implemented");
7556         FINISHME();
7557 }
7558
7559 static void switch_statement(struct compile_state *state, struct triple *first)
7560 {
7561         FINISHME();
7562         eat(state, TOK_SWITCH);
7563         eat(state, TOK_LPAREN);
7564         expr(state);
7565         eat(state, TOK_RPAREN);
7566         statement(state, first);
7567         error(state, 0, "switch statements are not implemented");
7568         FINISHME();
7569 }
7570
7571 static void case_statement(struct compile_state *state, struct triple *first)
7572 {
7573         FINISHME();
7574         eat(state, TOK_CASE);
7575         constant_expr(state);
7576         eat(state, TOK_COLON);
7577         statement(state, first);
7578         error(state, 0, "case statements are not implemented");
7579         FINISHME();
7580 }
7581
7582 static void default_statement(struct compile_state *state, struct triple *first)
7583 {
7584         FINISHME();
7585         eat(state, TOK_DEFAULT);
7586         eat(state, TOK_COLON);
7587         statement(state, first);
7588         error(state, 0, "default statements are not implemented");
7589         FINISHME();
7590 }
7591
7592 static void asm_statement(struct compile_state *state, struct triple *first)
7593 {
7594         struct asm_info *info;
7595         struct {
7596                 struct triple *constraint;
7597                 struct triple *expr;
7598         } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
7599         struct triple *def, *asm_str;
7600         int out, in, clobbers, more, colons, i;
7601
7602         eat(state, TOK_ASM);
7603         /* For now ignore the qualifiers */
7604         switch(peek(state)) {
7605         case TOK_CONST:
7606                 eat(state, TOK_CONST);
7607                 break;
7608         case TOK_VOLATILE:
7609                 eat(state, TOK_VOLATILE);
7610                 break;
7611         }
7612         eat(state, TOK_LPAREN);
7613         asm_str = string_constant(state);
7614
7615         colons = 0;
7616         out = in = clobbers = 0;
7617         /* Outputs */
7618         if ((colons == 0) && (peek(state) == TOK_COLON)) {
7619                 eat(state, TOK_COLON);
7620                 colons++;
7621                 more = (peek(state) == TOK_LIT_STRING);
7622                 while(more) {
7623                         struct triple *var;
7624                         struct triple *constraint;
7625                         more = 0;
7626                         if (out > MAX_LHS) {
7627                                 error(state, 0, "Maximum output count exceeded.");
7628                         }
7629                         constraint = string_constant(state);
7630                         eat(state, TOK_LPAREN);
7631                         var = conditional_expr(state);
7632                         eat(state, TOK_RPAREN);
7633
7634                         lvalue(state, var);
7635                         out_param[out].constraint = constraint;
7636                         out_param[out].expr       = var;
7637                         if (peek(state) == TOK_COMMA) {
7638                                 eat(state, TOK_COMMA);
7639                                 more = 1;
7640                         }
7641                         out++;
7642                 }
7643         }
7644         /* Inputs */
7645         if ((colons == 1) && (peek(state) == TOK_COLON)) {
7646                 eat(state, TOK_COLON);
7647                 colons++;
7648                 more = (peek(state) == TOK_LIT_STRING);
7649                 while(more) {
7650                         struct triple *val;
7651                         struct triple *constraint;
7652                         more = 0;
7653                         if (in > MAX_RHS) {
7654                                 error(state, 0, "Maximum input count exceeded.");
7655                         }
7656                         constraint = string_constant(state);
7657                         eat(state, TOK_LPAREN);
7658                         val = conditional_expr(state);
7659                         eat(state, TOK_RPAREN);
7660
7661                         in_param[in].constraint = constraint;
7662                         in_param[in].expr       = val;
7663                         if (peek(state) == TOK_COMMA) {
7664                                 eat(state, TOK_COMMA);
7665                                 more = 1;
7666                         }
7667                         in++;
7668                 }
7669         }
7670
7671         /* Clobber */
7672         if ((colons == 2) && (peek(state) == TOK_COLON)) {
7673                 eat(state, TOK_COLON);
7674                 colons++;
7675                 more = (peek(state) == TOK_LIT_STRING);
7676                 while(more) {
7677                         struct triple *clobber;
7678                         more = 0;
7679                         if ((clobbers + out) > MAX_LHS) {
7680                                 error(state, 0, "Maximum clobber limit exceeded.");
7681                         }
7682                         clobber = string_constant(state);
7683                         eat(state, TOK_RPAREN);
7684
7685                         clob_param[clobbers].constraint = clobber;
7686                         if (peek(state) == TOK_COMMA) {
7687                                 eat(state, TOK_COMMA);
7688                                 more = 1;
7689                         }
7690                         clobbers++;
7691                 }
7692         }
7693         eat(state, TOK_RPAREN);
7694         eat(state, TOK_SEMI);
7695
7696
7697         info = xcmalloc(sizeof(*info), "asm_info");
7698         info->str = asm_str->u.blob;
7699         free_triple(state, asm_str);
7700
7701         def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
7702         def->u.ainfo = info;
7703         for(i = 0; i < in; i++) {
7704                 struct triple *constraint;
7705                 constraint = in_param[i].constraint;
7706                 info->tmpl.rhs[i] = arch_reg_constraint(state, 
7707                         in_param[i].expr->type, constraint->u.blob);
7708
7709                 RHS(def, i) = read_expr(state,in_param[i].expr);
7710                 free_triple(state, constraint);
7711         }
7712         flatten(state, first, def);
7713         for(i = 0; i < out; i++) {
7714                 struct triple *piece;
7715                 struct triple *constraint;
7716                 constraint = out_param[i].constraint;
7717                 info->tmpl.lhs[i] = arch_reg_constraint(state,
7718                         out_param[i].expr->type, constraint->u.blob);
7719
7720                 piece = triple(state, OP_PIECE, out_param[i].expr->type, def, 0);
7721                 piece->u.cval = i;
7722                 LHS(def, i) = piece;
7723                 flatten(state, first,
7724                         write_expr(state, out_param[i].expr, piece));
7725                 free_triple(state, constraint);
7726         }
7727         for(; i - out < clobbers; i++) {
7728                 struct triple *piece;
7729                 struct triple *constraint;
7730                 constraint = clob_param[i - out].constraint;
7731                 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
7732
7733                 piece = triple(state, OP_PIECE, &void_type, def, 0);
7734                 piece->u.cval = i;
7735                 LHS(def, i) = piece;
7736                 flatten(state, first, piece);
7737                 free_triple(state, constraint);
7738         }
7739 }
7740
7741
7742 static int isdecl(int tok)
7743 {
7744         switch(tok) {
7745         case TOK_AUTO:
7746         case TOK_REGISTER:
7747         case TOK_STATIC:
7748         case TOK_EXTERN:
7749         case TOK_TYPEDEF:
7750         case TOK_CONST:
7751         case TOK_RESTRICT:
7752         case TOK_VOLATILE:
7753         case TOK_VOID:
7754         case TOK_CHAR:
7755         case TOK_SHORT:
7756         case TOK_INT:
7757         case TOK_LONG:
7758         case TOK_FLOAT:
7759         case TOK_DOUBLE:
7760         case TOK_SIGNED:
7761         case TOK_UNSIGNED:
7762         case TOK_STRUCT:
7763         case TOK_UNION:
7764         case TOK_ENUM:
7765         case TOK_TYPE_NAME: /* typedef name */
7766                 return 1;
7767         default:
7768                 return 0;
7769         }
7770 }
7771
7772 static void compound_statement(struct compile_state *state, struct triple *first)
7773 {
7774         eat(state, TOK_LBRACE);
7775         start_scope(state);
7776
7777         /* statement-list opt */
7778         while (peek(state) != TOK_RBRACE) {
7779                 statement(state, first);
7780         }
7781         end_scope(state);
7782         eat(state, TOK_RBRACE);
7783 }
7784
7785 static void statement(struct compile_state *state, struct triple *first)
7786 {
7787         int tok;
7788         tok = peek(state);
7789         if (tok == TOK_LBRACE) {
7790                 compound_statement(state, first);
7791         }
7792         else if (tok == TOK_IF) {
7793                 if_statement(state, first); 
7794         }
7795         else if (tok == TOK_FOR) {
7796                 for_statement(state, first);
7797         }
7798         else if (tok == TOK_WHILE) {
7799                 while_statement(state, first);
7800         }
7801         else if (tok == TOK_DO) {
7802                 do_statement(state, first);
7803         }
7804         else if (tok == TOK_RETURN) {
7805                 return_statement(state, first);
7806         }
7807         else if (tok == TOK_BREAK) {
7808                 break_statement(state, first);
7809         }
7810         else if (tok == TOK_CONTINUE) {
7811                 continue_statement(state, first);
7812         }
7813         else if (tok == TOK_GOTO) {
7814                 goto_statement(state, first);
7815         }
7816         else if (tok == TOK_SWITCH) {
7817                 switch_statement(state, first);
7818         }
7819         else if (tok == TOK_ASM) {
7820                 asm_statement(state, first);
7821         }
7822         else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
7823                 labeled_statement(state, first); 
7824         }
7825         else if (tok == TOK_CASE) {
7826                 case_statement(state, first);
7827         }
7828         else if (tok == TOK_DEFAULT) {
7829                 default_statement(state, first);
7830         }
7831         else if (isdecl(tok)) {
7832                 /* This handles C99 intermixing of statements and decls */
7833                 decl(state, first);
7834         }
7835         else {
7836                 expr_statement(state, first);
7837         }
7838 }
7839
7840 static struct type *param_decl(struct compile_state *state)
7841 {
7842         struct type *type;
7843         struct hash_entry *ident;
7844         /* Cheat so the declarator will know we are not global */
7845         start_scope(state); 
7846         ident = 0;
7847         type = decl_specifiers(state);
7848         type = declarator(state, type, &ident, 0);
7849         type->field_ident = ident;
7850         end_scope(state);
7851         return type;
7852 }
7853
7854 static struct type *param_type_list(struct compile_state *state, struct type *type)
7855 {
7856         struct type *ftype, **next;
7857         ftype = new_type(TYPE_FUNCTION, type, param_decl(state));
7858         next = &ftype->right;
7859         while(peek(state) == TOK_COMMA) {
7860                 eat(state, TOK_COMMA);
7861                 if (peek(state) == TOK_DOTS) {
7862                         eat(state, TOK_DOTS);
7863                         error(state, 0, "variadic functions not supported");
7864                 }
7865                 else {
7866                         *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
7867                         next = &((*next)->right);
7868                 }
7869         }
7870         return ftype;
7871 }
7872
7873
7874 static struct type *type_name(struct compile_state *state)
7875 {
7876         struct type *type;
7877         type = specifier_qualifier_list(state);
7878         /* abstract-declarator (may consume no tokens) */
7879         type = declarator(state, type, 0, 0);
7880         return type;
7881 }
7882
7883 static struct type *direct_declarator(
7884         struct compile_state *state, struct type *type, 
7885         struct hash_entry **ident, int need_ident)
7886 {
7887         struct type *outer;
7888         int op;
7889         outer = 0;
7890         arrays_complete(state, type);
7891         switch(peek(state)) {
7892         case TOK_IDENT:
7893                 eat(state, TOK_IDENT);
7894                 if (!ident) {
7895                         error(state, 0, "Unexpected identifier found");
7896                 }
7897                 /* The name of what we are declaring */
7898                 *ident = state->token[0].ident;
7899                 break;
7900         case TOK_LPAREN:
7901                 eat(state, TOK_LPAREN);
7902                 outer = declarator(state, type, ident, need_ident);
7903                 eat(state, TOK_RPAREN);
7904                 break;
7905         default:
7906                 if (need_ident) {
7907                         error(state, 0, "Identifier expected");
7908                 }
7909                 break;
7910         }
7911         do {
7912                 op = 1;
7913                 arrays_complete(state, type);
7914                 switch(peek(state)) {
7915                 case TOK_LPAREN:
7916                         eat(state, TOK_LPAREN);
7917                         type = param_type_list(state, type);
7918                         eat(state, TOK_RPAREN);
7919                         break;
7920                 case TOK_LBRACKET:
7921                 {
7922                         unsigned int qualifiers;
7923                         struct triple *value;
7924                         value = 0;
7925                         eat(state, TOK_LBRACKET);
7926                         if (peek(state) != TOK_RBRACKET) {
7927                                 value = constant_expr(state);
7928                                 integral(state, value);
7929                         }
7930                         eat(state, TOK_RBRACKET);
7931
7932                         qualifiers = type->type & (QUAL_MASK | STOR_MASK);
7933                         type = new_type(TYPE_ARRAY | qualifiers, type, 0);
7934                         if (value) {
7935                                 type->elements = value->u.cval;
7936                                 free_triple(state, value);
7937                         } else {
7938                                 type->elements = ELEMENT_COUNT_UNSPECIFIED;
7939                                 op = 0;
7940                         }
7941                 }
7942                         break;
7943                 default:
7944                         op = 0;
7945                         break;
7946                 }
7947         } while(op);
7948         if (outer) {
7949                 struct type *inner;
7950                 arrays_complete(state, type);
7951                 FINISHME();
7952                 for(inner = outer; inner->left; inner = inner->left)
7953                         ;
7954                 inner->left = type;
7955                 type = outer;
7956         }
7957         return type;
7958 }
7959
7960 static struct type *declarator(
7961         struct compile_state *state, struct type *type, 
7962         struct hash_entry **ident, int need_ident)
7963 {
7964         while(peek(state) == TOK_STAR) {
7965                 eat(state, TOK_STAR);
7966                 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
7967         }
7968         type = direct_declarator(state, type, ident, need_ident);
7969         return type;
7970 }
7971
7972
7973 static struct type *typedef_name(
7974         struct compile_state *state, unsigned int specifiers)
7975 {
7976         struct hash_entry *ident;
7977         struct type *type;
7978         eat(state, TOK_TYPE_NAME);
7979         ident = state->token[0].ident;
7980         type = ident->sym_ident->type;
7981         specifiers |= type->type & QUAL_MASK;
7982         if ((specifiers & (STOR_MASK | QUAL_MASK)) != 
7983                 (type->type & (STOR_MASK | QUAL_MASK))) {
7984                 type = clone_type(specifiers, type);
7985         }
7986         return type;
7987 }
7988
7989 static struct type *enum_specifier(
7990         struct compile_state *state, unsigned int specifiers)
7991 {
7992         int tok;
7993         struct type *type;
7994         type = 0;
7995         FINISHME();
7996         eat(state, TOK_ENUM);
7997         tok = peek(state);
7998         if (tok == TOK_IDENT) {
7999                 eat(state, TOK_IDENT);
8000         }
8001         if ((tok != TOK_IDENT) || (peek(state) == TOK_LBRACE)) {
8002                 eat(state, TOK_LBRACE);
8003                 do {
8004                         eat(state, TOK_IDENT);
8005                         if (peek(state) == TOK_EQ) {
8006                                 eat(state, TOK_EQ);
8007                                 constant_expr(state);
8008                         }
8009                         if (peek(state) == TOK_COMMA) {
8010                                 eat(state, TOK_COMMA);
8011                         }
8012                 } while(peek(state) != TOK_RBRACE);
8013                 eat(state, TOK_RBRACE);
8014         }
8015         FINISHME();
8016         return type;
8017 }
8018
8019 #if 0
8020 static struct type *struct_declarator(
8021         struct compile_state *state, struct type *type, struct hash_entry **ident)
8022 {
8023         int tok;
8024 #warning "struct_declarator is complicated because of bitfields, kill them?"
8025         tok = peek(state);
8026         if (tok != TOK_COLON) {
8027                 type = declarator(state, type, ident, 1);
8028         }
8029         if ((tok == TOK_COLON) || (peek(state) == TOK_COLON)) {
8030                 eat(state, TOK_COLON);
8031                 constant_expr(state);
8032         }
8033         FINISHME();
8034         return type;
8035 }
8036 #endif
8037
8038 static struct type *struct_or_union_specifier(
8039         struct compile_state *state, unsigned int specifiers)
8040 {
8041         struct type *struct_type;
8042         struct hash_entry *ident;
8043         unsigned int type_join;
8044         int tok;
8045         struct_type = 0;
8046         ident = 0;
8047         switch(peek(state)) {
8048         case TOK_STRUCT:
8049                 eat(state, TOK_STRUCT);
8050                 type_join = TYPE_PRODUCT;
8051                 break;
8052         case TOK_UNION:
8053                 eat(state, TOK_UNION);
8054                 type_join = TYPE_OVERLAP;
8055                 error(state, 0, "unions not yet supported\n");
8056                 break;
8057         default:
8058                 eat(state, TOK_STRUCT);
8059                 type_join = TYPE_PRODUCT;
8060                 break;
8061         }
8062         tok = peek(state);
8063         if ((tok == TOK_IDENT) || (tok == TOK_TYPE_NAME)) {
8064                 eat(state, tok);
8065                 ident = state->token[0].ident;
8066         }
8067         if (!ident || (peek(state) == TOK_LBRACE)) {
8068                 ulong_t elements;
8069                 elements = 0;
8070                 eat(state, TOK_LBRACE);
8071                 do {
8072                         struct type *base_type;
8073                         struct type **next;
8074                         int done;
8075                         base_type = specifier_qualifier_list(state);
8076                         next = &struct_type;
8077                         do {
8078                                 struct type *type;
8079                                 struct hash_entry *fident;
8080                                 done = 1;
8081                                 type = declarator(state, base_type, &fident, 1);
8082                                 elements++;
8083                                 if (peek(state) == TOK_COMMA) {
8084                                         done = 0;
8085                                         eat(state, TOK_COMMA);
8086                                 }
8087                                 type = clone_type(0, type);
8088                                 type->field_ident = fident;
8089                                 if (*next) {
8090                                         *next = new_type(type_join, *next, type);
8091                                         next = &((*next)->right);
8092                                 } else {
8093                                         *next = type;
8094                                 }
8095                         } while(!done);
8096                         eat(state, TOK_SEMI);
8097                 } while(peek(state) != TOK_RBRACE);
8098                 eat(state, TOK_RBRACE);
8099                 struct_type = new_type(TYPE_STRUCT, struct_type, 0);
8100                 struct_type->type_ident = ident;
8101                 struct_type->elements = elements;
8102                 symbol(state, ident, &ident->sym_struct, 0, struct_type);
8103         }
8104         if (ident && ident->sym_struct) {
8105                 struct_type = ident->sym_struct->type;
8106         }
8107         else if (ident && !ident->sym_struct) {
8108                 error(state, 0, "struct %s undeclared", ident->name);
8109         }
8110         return struct_type;
8111 }
8112
8113 static unsigned int storage_class_specifier_opt(struct compile_state *state)
8114 {
8115         unsigned int specifiers;
8116         switch(peek(state)) {
8117         case TOK_AUTO:
8118                 eat(state, TOK_AUTO);
8119                 specifiers = STOR_AUTO;
8120                 break;
8121         case TOK_REGISTER:
8122                 eat(state, TOK_REGISTER);
8123                 specifiers = STOR_REGISTER;
8124                 break;
8125         case TOK_STATIC:
8126                 eat(state, TOK_STATIC);
8127                 specifiers = STOR_STATIC;
8128                 break;
8129         case TOK_EXTERN:
8130                 eat(state, TOK_EXTERN);
8131                 specifiers = STOR_EXTERN;
8132                 break;
8133         case TOK_TYPEDEF:
8134                 eat(state, TOK_TYPEDEF);
8135                 specifiers = STOR_TYPEDEF;
8136                 break;
8137         default:
8138                 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
8139                         specifiers = STOR_STATIC;
8140                 }
8141                 else {
8142                         specifiers = STOR_AUTO;
8143                 }
8144         }
8145         return specifiers;
8146 }
8147
8148 static unsigned int function_specifier_opt(struct compile_state *state)
8149 {
8150         /* Ignore the inline keyword */
8151         unsigned int specifiers;
8152         specifiers = 0;
8153         switch(peek(state)) {
8154         case TOK_INLINE:
8155                 eat(state, TOK_INLINE);
8156                 specifiers = STOR_INLINE;
8157         }
8158         return specifiers;
8159 }
8160
8161 static unsigned int type_qualifiers(struct compile_state *state)
8162 {
8163         unsigned int specifiers;
8164         int done;
8165         done = 0;
8166         specifiers = QUAL_NONE;
8167         do {
8168                 switch(peek(state)) {
8169                 case TOK_CONST:
8170                         eat(state, TOK_CONST);
8171                         specifiers = QUAL_CONST;
8172                         break;
8173                 case TOK_VOLATILE:
8174                         eat(state, TOK_VOLATILE);
8175                         specifiers = QUAL_VOLATILE;
8176                         break;
8177                 case TOK_RESTRICT:
8178                         eat(state, TOK_RESTRICT);
8179                         specifiers = QUAL_RESTRICT;
8180                         break;
8181                 default:
8182                         done = 1;
8183                         break;
8184                 }
8185         } while(!done);
8186         return specifiers;
8187 }
8188
8189 static struct type *type_specifier(
8190         struct compile_state *state, unsigned int spec)
8191 {
8192         struct type *type;
8193         type = 0;
8194         switch(peek(state)) {
8195         case TOK_VOID:
8196                 eat(state, TOK_VOID);
8197                 type = new_type(TYPE_VOID | spec, 0, 0);
8198                 break;
8199         case TOK_CHAR:
8200                 eat(state, TOK_CHAR);
8201                 type = new_type(TYPE_CHAR | spec, 0, 0);
8202                 break;
8203         case TOK_SHORT:
8204                 eat(state, TOK_SHORT);
8205                 if (peek(state) == TOK_INT) {
8206                         eat(state, TOK_INT);
8207                 }
8208                 type = new_type(TYPE_SHORT | spec, 0, 0);
8209                 break;
8210         case TOK_INT:
8211                 eat(state, TOK_INT);
8212                 type = new_type(TYPE_INT | spec, 0, 0);
8213                 break;
8214         case TOK_LONG:
8215                 eat(state, TOK_LONG);
8216                 switch(peek(state)) {
8217                 case TOK_LONG:
8218                         eat(state, TOK_LONG);
8219                         error(state, 0, "long long not supported");
8220                         break;
8221                 case TOK_DOUBLE:
8222                         eat(state, TOK_DOUBLE);
8223                         error(state, 0, "long double not supported");
8224                         break;
8225                 case TOK_INT:
8226                         eat(state, TOK_INT);
8227                         type = new_type(TYPE_LONG | spec, 0, 0);
8228                         break;
8229                 default:
8230                         type = new_type(TYPE_LONG | spec, 0, 0);
8231                         break;
8232                 }
8233                 break;
8234         case TOK_FLOAT:
8235                 eat(state, TOK_FLOAT);
8236                 error(state, 0, "type float not supported");
8237                 break;
8238         case TOK_DOUBLE:
8239                 eat(state, TOK_DOUBLE);
8240                 error(state, 0, "type double not supported");
8241                 break;
8242         case TOK_SIGNED:
8243                 eat(state, TOK_SIGNED);
8244                 switch(peek(state)) {
8245                 case TOK_LONG:
8246                         eat(state, TOK_LONG);
8247                         switch(peek(state)) {
8248                         case TOK_LONG:
8249                                 eat(state, TOK_LONG);
8250                                 error(state, 0, "type long long not supported");
8251                                 break;
8252                         case TOK_INT:
8253                                 eat(state, TOK_INT);
8254                                 type = new_type(TYPE_LONG | spec, 0, 0);
8255                                 break;
8256                         default:
8257                                 type = new_type(TYPE_LONG | spec, 0, 0);
8258                                 break;
8259                         }
8260                         break;
8261                 case TOK_INT:
8262                         eat(state, TOK_INT);
8263                         type = new_type(TYPE_INT | spec, 0, 0);
8264                         break;
8265                 case TOK_SHORT:
8266                         eat(state, TOK_SHORT);
8267                         type = new_type(TYPE_SHORT | spec, 0, 0);
8268                         break;
8269                 case TOK_CHAR:
8270                         eat(state, TOK_CHAR);
8271                         type = new_type(TYPE_CHAR | spec, 0, 0);
8272                         break;
8273                 default:
8274                         type = new_type(TYPE_INT | spec, 0, 0);
8275                         break;
8276                 }
8277                 break;
8278         case TOK_UNSIGNED:
8279                 eat(state, TOK_UNSIGNED);
8280                 switch(peek(state)) {
8281                 case TOK_LONG:
8282                         eat(state, TOK_LONG);
8283                         switch(peek(state)) {
8284                         case TOK_LONG:
8285                                 eat(state, TOK_LONG);
8286                                 error(state, 0, "unsigned long long not supported");
8287                                 break;
8288                         case TOK_INT:
8289                                 eat(state, TOK_INT);
8290                                 type = new_type(TYPE_ULONG | spec, 0, 0);
8291                                 break;
8292                         default:
8293                                 type = new_type(TYPE_ULONG | spec, 0, 0);
8294                                 break;
8295                         }
8296                         break;
8297                 case TOK_INT:
8298                         eat(state, TOK_INT);
8299                         type = new_type(TYPE_UINT | spec, 0, 0);
8300                         break;
8301                 case TOK_SHORT:
8302                         eat(state, TOK_SHORT);
8303                         type = new_type(TYPE_USHORT | spec, 0, 0);
8304                         break;
8305                 case TOK_CHAR:
8306                         eat(state, TOK_CHAR);
8307                         type = new_type(TYPE_UCHAR | spec, 0, 0);
8308                         break;
8309                 default:
8310                         type = new_type(TYPE_UINT | spec, 0, 0);
8311                         break;
8312                 }
8313                 break;
8314                 /* struct or union specifier */
8315         case TOK_STRUCT:
8316         case TOK_UNION:
8317                 type = struct_or_union_specifier(state, spec);
8318                 break;
8319                 /* enum-spefifier */
8320         case TOK_ENUM:
8321                 type = enum_specifier(state, spec);
8322                 break;
8323                 /* typedef name */
8324         case TOK_TYPE_NAME:
8325                 type = typedef_name(state, spec);
8326                 break;
8327         default:
8328                 error(state, 0, "bad type specifier %s", 
8329                         tokens[peek(state)]);
8330                 break;
8331         }
8332         return type;
8333 }
8334
8335 static int istype(int tok)
8336 {
8337         switch(tok) {
8338         case TOK_CONST:
8339         case TOK_RESTRICT:
8340         case TOK_VOLATILE:
8341         case TOK_VOID:
8342         case TOK_CHAR:
8343         case TOK_SHORT:
8344         case TOK_INT:
8345         case TOK_LONG:
8346         case TOK_FLOAT:
8347         case TOK_DOUBLE:
8348         case TOK_SIGNED:
8349         case TOK_UNSIGNED:
8350         case TOK_STRUCT:
8351         case TOK_UNION:
8352         case TOK_ENUM:
8353         case TOK_TYPE_NAME:
8354                 return 1;
8355         default:
8356                 return 0;
8357         }
8358 }
8359
8360
8361 static struct type *specifier_qualifier_list(struct compile_state *state)
8362 {
8363         struct type *type;
8364         unsigned int specifiers = 0;
8365
8366         /* type qualifiers */
8367         specifiers |= type_qualifiers(state);
8368
8369         /* type specifier */
8370         type = type_specifier(state, specifiers);
8371
8372         return type;
8373 }
8374
8375 static int isdecl_specifier(int tok)
8376 {
8377         switch(tok) {
8378                 /* storage class specifier */
8379         case TOK_AUTO:
8380         case TOK_REGISTER:
8381         case TOK_STATIC:
8382         case TOK_EXTERN:
8383         case TOK_TYPEDEF:
8384                 /* type qualifier */
8385         case TOK_CONST:
8386         case TOK_RESTRICT:
8387         case TOK_VOLATILE:
8388                 /* type specifiers */
8389         case TOK_VOID:
8390         case TOK_CHAR:
8391         case TOK_SHORT:
8392         case TOK_INT:
8393         case TOK_LONG:
8394         case TOK_FLOAT:
8395         case TOK_DOUBLE:
8396         case TOK_SIGNED:
8397         case TOK_UNSIGNED:
8398                 /* struct or union specifier */
8399         case TOK_STRUCT:
8400         case TOK_UNION:
8401                 /* enum-spefifier */
8402         case TOK_ENUM:
8403                 /* typedef name */
8404         case TOK_TYPE_NAME:
8405                 /* function specifiers */
8406         case TOK_INLINE:
8407                 return 1;
8408         default:
8409                 return 0;
8410         }
8411 }
8412
8413 static struct type *decl_specifiers(struct compile_state *state)
8414 {
8415         struct type *type;
8416         unsigned int specifiers;
8417         /* I am overly restrictive in the arragement of specifiers supported.
8418          * C is overly flexible in this department it makes interpreting
8419          * the parse tree difficult.
8420          */
8421         specifiers = 0;
8422
8423         /* storage class specifier */
8424         specifiers |= storage_class_specifier_opt(state);
8425
8426         /* function-specifier */
8427         specifiers |= function_specifier_opt(state);
8428
8429         /* type qualifier */
8430         specifiers |= type_qualifiers(state);
8431
8432         /* type specifier */
8433         type = type_specifier(state, specifiers);
8434         return type;
8435 }
8436
8437 static unsigned designator(struct compile_state *state)
8438 {
8439         int tok;
8440         unsigned index;
8441         index = -1U;
8442         do {
8443                 switch(peek(state)) {
8444                 case TOK_LBRACKET:
8445                 {
8446                         struct triple *value;
8447                         eat(state, TOK_LBRACKET);
8448                         value = constant_expr(state);
8449                         eat(state, TOK_RBRACKET);
8450                         index = value->u.cval;
8451                         break;
8452                 }
8453                 case TOK_DOT:
8454                         eat(state, TOK_DOT);
8455                         eat(state, TOK_IDENT);
8456                         error(state, 0, "Struct Designators not currently supported");
8457                         break;
8458                 default:
8459                         error(state, 0, "Invalid designator");
8460                 }
8461                 tok = peek(state);
8462         } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
8463         eat(state, TOK_EQ);
8464         return index;
8465 }
8466
8467 static struct triple *initializer(
8468         struct compile_state *state, struct type *type)
8469 {
8470         struct triple *result;
8471         if (peek(state) != TOK_LBRACE) {
8472                 result = assignment_expr(state);
8473         }
8474         else {
8475                 int comma;
8476                 unsigned index, max_index;
8477                 void *buf;
8478                 max_index = index = 0;
8479                 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
8480                         max_index = type->elements;
8481                         if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
8482                                 type->elements = 0;
8483                         }
8484                 } else {
8485                         error(state, 0, "Struct initializers not currently supported");
8486                 }
8487                 buf = xcmalloc(size_of(state, type), "initializer");
8488                 eat(state, TOK_LBRACE);
8489                 do {
8490                         struct triple *value;
8491                         struct type *value_type;
8492                         size_t value_size;
8493                         int tok;
8494                         comma = 0;
8495                         tok = peek(state);
8496                         if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
8497                                 index = designator(state);
8498                         }
8499                         if ((max_index != ELEMENT_COUNT_UNSPECIFIED) &&
8500                                 (index > max_index)) {
8501                                 error(state, 0, "element beyond bounds");
8502                         }
8503                         value_type = 0;
8504                         if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
8505                                 value_type = type->left;
8506                         }
8507                         value = eval_const_expr(state, initializer(state, value_type));
8508                         value_size = size_of(state, value_type);
8509                         if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
8510                                 (max_index == ELEMENT_COUNT_UNSPECIFIED) &&
8511                                 (type->elements <= index)) {
8512                                 void *old_buf;
8513                                 size_t old_size;
8514                                 old_buf = buf;
8515                                 old_size = size_of(state, type);
8516                                 type->elements = index + 1;
8517                                 buf = xmalloc(size_of(state, type), "initializer");
8518                                 memcpy(buf, old_buf, old_size);
8519                                 xfree(old_buf);
8520                         }
8521                         if (value->op == OP_BLOBCONST) {
8522                                 memcpy((char *)buf + index * value_size, value->u.blob, value_size);
8523                         }
8524                         else if ((value->op == OP_INTCONST) && (value_size == 1)) {
8525                                 *(((uint8_t *)buf) + index) = value->u.cval & 0xff;
8526                         }
8527                         else if ((value->op == OP_INTCONST) && (value_size == 2)) {
8528                                 *(((uint16_t *)buf) + index) = value->u.cval & 0xffff;
8529                         }
8530                         else if ((value->op == OP_INTCONST) && (value_size == 4)) {
8531                                 *(((uint32_t *)buf) + index) = value->u.cval & 0xffffffff;
8532                         }
8533                         else {
8534                                 fprintf(stderr, "%d %d\n",
8535                                         value->op, value_size);
8536                                 internal_error(state, 0, "unhandled constant initializer");
8537                         }
8538                         if (peek(state) == TOK_COMMA) {
8539                                 eat(state, TOK_COMMA);
8540                                 comma = 1;
8541                         }
8542                         index += 1;
8543                 } while(comma && (peek(state) != TOK_RBRACE));
8544                 eat(state, TOK_RBRACE);
8545                 result = triple(state, OP_BLOBCONST, type, 0, 0);
8546                 result->u.blob = buf;
8547         }
8548         return result;
8549 }
8550
8551 static struct triple *function_definition(
8552         struct compile_state *state, struct type *type)
8553 {
8554         struct triple *def, *tmp, *first, *end;
8555         struct hash_entry *ident;
8556         struct type *param;
8557         int i;
8558         if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
8559                 error(state, 0, "Invalid function header");
8560         }
8561
8562         /* Verify the function type */
8563         if (((type->right->type & TYPE_MASK) != TYPE_VOID)  &&
8564                 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
8565                 (type->right->field_ident == 0)) {
8566                 error(state, 0, "Invalid function parameters");
8567         }
8568         param = type->right;
8569         i = 0;
8570         while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
8571                 i++;
8572                 if (!param->left->field_ident) {
8573                         error(state, 0, "No identifier for parameter %d\n", i);
8574                 }
8575                 param = param->right;
8576         }
8577         i++;
8578         if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
8579                 error(state, 0, "No identifier for paramter %d\n", i);
8580         }
8581         
8582         /* Get a list of statements for this function. */
8583         def = triple(state, OP_LIST, type, 0, 0);
8584
8585         /* Start a new scope for the passed parameters */
8586         start_scope(state);
8587
8588         /* Put a label at the very start of a function */
8589         first = label(state);
8590         RHS(def, 0) = first;
8591
8592         /* Put a label at the very end of a function */
8593         end = label(state);
8594         flatten(state, first, end);
8595
8596         /* Walk through the parameters and create symbol table entries
8597          * for them.
8598          */
8599         param = type->right;
8600         while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
8601                 ident = param->left->field_ident;
8602                 tmp = variable(state, param->left);
8603                 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
8604                 flatten(state, end, tmp);
8605                 param = param->right;
8606         }
8607         if ((param->type & TYPE_MASK) != TYPE_VOID) {
8608                 /* And don't forget the last parameter */
8609                 ident = param->field_ident;
8610                 tmp = variable(state, param);
8611                 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
8612                 flatten(state, end, tmp);
8613         }
8614         /* Add a variable for the return value */
8615         MISC(def, 0) = 0;
8616         if ((type->left->type & TYPE_MASK) != TYPE_VOID) {
8617                 /* Remove all type qualifiers from the return type */
8618                 tmp = variable(state, clone_type(0, type->left));
8619                 flatten(state, end, tmp);
8620                 /* Remember where the return value is */
8621                 MISC(def, 0) = tmp;
8622         }
8623
8624         /* Remember which function I am compiling.
8625          * Also assume the last defined function is the main function.
8626          */
8627         state->main_function = def;
8628
8629         /* Now get the actual function definition */
8630         compound_statement(state, end);
8631
8632         /* Remove the parameter scope */
8633         end_scope(state);
8634 #if 0
8635         fprintf(stdout, "\n");
8636         loc(stdout, state, 0);
8637         fprintf(stdout, "\n__________ function_definition _________\n");
8638         print_triple(state, def);
8639         fprintf(stdout, "__________ function_definition _________ done\n\n");
8640 #endif
8641
8642         return def;
8643 }
8644
8645 static struct triple *do_decl(struct compile_state *state, 
8646         struct type *type, struct hash_entry *ident)
8647 {
8648         struct triple *def;
8649         def = 0;
8650         /* Clean up the storage types used */
8651         switch (type->type & STOR_MASK) {
8652         case STOR_AUTO:
8653         case STOR_STATIC:
8654                 /* These are the good types I am aiming for */
8655                 break;
8656         case STOR_REGISTER:
8657                 type->type &= ~STOR_MASK;
8658                 type->type |= STOR_AUTO;
8659                 break;
8660         case STOR_EXTERN:
8661                 type->type &= ~STOR_MASK;
8662                 type->type |= STOR_STATIC;
8663                 break;
8664         case STOR_TYPEDEF:
8665                 if (!ident) {
8666                         error(state, 0, "typedef without name");
8667                 }
8668                 symbol(state, ident, &ident->sym_ident, 0, type);
8669                 ident->tok = TOK_TYPE_NAME;
8670                 return 0;
8671                 break;
8672         default:
8673                 internal_error(state, 0, "Undefined storage class");
8674         }
8675         if (((type->type & STOR_MASK) == STOR_STATIC) &&
8676                 ((type->type & QUAL_CONST) == 0)) {
8677                 error(state, 0, "non const static variables not supported");
8678         }
8679         if (ident) {
8680                 def = variable(state, type);
8681                 symbol(state, ident, &ident->sym_ident, def, type);
8682         }
8683         return def;
8684 }
8685
8686 static void decl(struct compile_state *state, struct triple *first)
8687 {
8688         struct type *base_type, *type;
8689         struct hash_entry *ident;
8690         struct triple *def;
8691         int global;
8692         global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
8693         base_type = decl_specifiers(state);
8694         ident = 0;
8695         type = declarator(state, base_type, &ident, 0);
8696         if (global && ident && (peek(state) == TOK_LBRACE)) {
8697                 /* function */
8698                 def = function_definition(state, type);
8699                 symbol(state, ident, &ident->sym_ident, def, type);
8700         }
8701         else {
8702                 int done;
8703                 flatten(state, first, do_decl(state, type, ident));
8704                 /* type or variable definition */
8705                 do {
8706                         done = 1;
8707                         if (peek(state) == TOK_EQ) {
8708                                 if (!ident) {
8709                                         error(state, 0, "cannot assign to a type");
8710                                 }
8711                                 eat(state, TOK_EQ);
8712                                 flatten(state, first,
8713                                         init_expr(state, 
8714                                                 ident->sym_ident->def, 
8715                                                 initializer(state, type)));
8716                         }
8717                         arrays_complete(state, type);
8718                         if (peek(state) == TOK_COMMA) {
8719                                 eat(state, TOK_COMMA);
8720                                 ident = 0;
8721                                 type = declarator(state, base_type, &ident, 0);
8722                                 flatten(state, first, do_decl(state, type, ident));
8723                                 done = 0;
8724                         }
8725                 } while(!done);
8726                 eat(state, TOK_SEMI);
8727         }
8728 }
8729
8730 static void decls(struct compile_state *state)
8731 {
8732         struct triple *list;
8733         int tok;
8734         list = label(state);
8735         while(1) {
8736                 tok = peek(state);
8737                 if (tok == TOK_EOF) {
8738                         return;
8739                 }
8740                 if (tok == TOK_SPACE) {
8741                         eat(state, TOK_SPACE);
8742                 }
8743                 decl(state, list);
8744                 if (list->next != list) {
8745                         error(state, 0, "global variables not supported");
8746                 }
8747         }
8748 }
8749
8750 /*
8751  * Data structurs for optimation.
8752  */
8753
8754 static void do_use_block(
8755         struct block *used, struct block_set **head, struct block *user, 
8756         int front)
8757 {
8758         struct block_set **ptr, *new;
8759         if (!used)
8760                 return;
8761         if (!user)
8762                 return;
8763         ptr = head;
8764         while(*ptr) {
8765                 if ((*ptr)->member == user) {
8766                         return;
8767                 }
8768                 ptr = &(*ptr)->next;
8769         }
8770         new = xcmalloc(sizeof(*new), "block_set");
8771         new->member = user;
8772         if (front) {
8773                 new->next = *head;
8774                 *head = new;
8775         }
8776         else {
8777                 new->next = 0;
8778                 *ptr = new;
8779         }
8780 }
8781 static void do_unuse_block(
8782         struct block *used, struct block_set **head, struct block *unuser)
8783 {
8784         struct block_set *use, **ptr;
8785         ptr = head;
8786         while(*ptr) {
8787                 use = *ptr;
8788                 if (use->member == unuser) {
8789                         *ptr = use->next;
8790                         memset(use, -1, sizeof(*use));
8791                         xfree(use);
8792                 }
8793                 else {
8794                         ptr = &use->next;
8795                 }
8796         }
8797 }
8798
8799 static void use_block(struct block *used, struct block *user)
8800 {
8801         /* Append new to the head of the list, print_block
8802          * depends on this.
8803          */
8804         do_use_block(used, &used->use, user, 1); 
8805         used->users++;
8806 }
8807 static void unuse_block(struct block *used, struct block *unuser)
8808 {
8809         do_unuse_block(used, &used->use, unuser); 
8810         used->users--;
8811 }
8812
8813 static void idom_block(struct block *idom, struct block *user)
8814 {
8815         do_use_block(idom, &idom->idominates, user, 0);
8816 }
8817
8818 static void unidom_block(struct block *idom, struct block *unuser)
8819 {
8820         do_unuse_block(idom, &idom->idominates, unuser);
8821 }
8822
8823 static void domf_block(struct block *block, struct block *domf)
8824 {
8825         do_use_block(block, &block->domfrontier, domf, 0);
8826 }
8827
8828 static void undomf_block(struct block *block, struct block *undomf)
8829 {
8830         do_unuse_block(block, &block->domfrontier, undomf);
8831 }
8832
8833 static void ipdom_block(struct block *ipdom, struct block *user)
8834 {
8835         do_use_block(ipdom, &ipdom->ipdominates, user, 0);
8836 }
8837
8838 static void unipdom_block(struct block *ipdom, struct block *unuser)
8839 {
8840         do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
8841 }
8842
8843 static void ipdomf_block(struct block *block, struct block *ipdomf)
8844 {
8845         do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
8846 }
8847
8848 static void unipdomf_block(struct block *block, struct block *unipdomf)
8849 {
8850         do_unuse_block(block, &block->ipdomfrontier, unipdomf);
8851 }
8852
8853
8854
8855 static int do_walk_triple(struct compile_state *state,
8856         struct triple *ptr, int depth,
8857         int (*cb)(struct compile_state *state, struct triple *ptr, int depth)) 
8858 {
8859         int result;
8860         result = cb(state, ptr, depth);
8861         if ((result == 0) && (ptr->op == OP_LIST)) {
8862                 struct triple *list;
8863                 list = ptr;
8864                 ptr = RHS(list, 0);
8865                 do {
8866                         result = do_walk_triple(state, ptr, depth + 1, cb);
8867                         if (ptr->next->prev != ptr) {
8868                                 internal_error(state, ptr->next, "bad prev");
8869                         }
8870                         ptr = ptr->next;
8871                         
8872                 } while((result == 0) && (ptr != RHS(list, 0)));
8873         }
8874         return result;
8875 }
8876
8877 static int walk_triple(
8878         struct compile_state *state, 
8879         struct triple *ptr, 
8880         int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
8881 {
8882         return do_walk_triple(state, ptr, 0, cb);
8883 }
8884
8885 static void do_print_prefix(int depth)
8886 {
8887         int i;
8888         for(i = 0; i < depth; i++) {
8889                 printf("  ");
8890         }
8891 }
8892
8893 #define PRINT_LIST 1
8894 static int do_print_triple(struct compile_state *state, struct triple *ins, int depth)
8895 {
8896         int op;
8897         op = ins->op;
8898         if (op == OP_LIST) {
8899 #if !PRINT_LIST
8900                 return 0;
8901 #endif
8902         }
8903         if ((op == OP_LABEL) && (ins->use)) {
8904                 printf("\n%p:\n", ins);
8905         }
8906         do_print_prefix(depth);
8907         display_triple(stdout, ins);
8908
8909         if ((ins->op == OP_BRANCH) && ins->use) {
8910                 internal_error(state, ins, "branch used?");
8911         }
8912 #if 0
8913         {
8914                 struct triple_set *user;
8915                 for(user = ins->use; user; user = user->next) {
8916                         printf("use: %p\n", user->member);
8917                 }
8918         }
8919 #endif
8920         if (triple_is_branch(state, ins)) {
8921                 printf("\n");
8922         }
8923         return 0;
8924 }
8925
8926 static void print_triple(struct compile_state *state, struct triple *ins)
8927 {
8928         walk_triple(state, ins, do_print_triple);
8929 }
8930
8931 static void print_triples(struct compile_state *state)
8932 {
8933         print_triple(state, state->main_function);
8934 }
8935
8936 struct cf_block {
8937         struct block *block;
8938 };
8939 static void find_cf_blocks(struct cf_block *cf, struct block *block)
8940 {
8941         if (!block || (cf[block->vertex].block == block)) {
8942                 return;
8943         }
8944         cf[block->vertex].block = block;
8945         find_cf_blocks(cf, block->left);
8946         find_cf_blocks(cf, block->right);
8947 }
8948
8949 static void print_control_flow(struct compile_state *state)
8950 {
8951         struct cf_block *cf;
8952         int i;
8953         printf("\ncontrol flow\n");
8954         cf = xcmalloc(sizeof(*cf) * (state->last_vertex + 1), "cf_block");
8955         find_cf_blocks(cf, state->first_block);
8956
8957         for(i = 1; i <= state->last_vertex; i++) {
8958                 struct block *block;
8959                 block = cf[i].block;
8960                 if (!block)
8961                         continue;
8962                 printf("(%p) %d:", block, block->vertex);
8963                 if (block->left) {
8964                         printf(" %d", block->left->vertex);
8965                 }
8966                 if (block->right && (block->right != block->left)) {
8967                         printf(" %d", block->right->vertex);
8968                 }
8969                 printf("\n");
8970         }
8971
8972         xfree(cf);
8973 }
8974
8975
8976 static struct block *basic_block(struct compile_state *state,
8977         struct triple *first)
8978 {
8979         struct block *block;
8980         struct triple *ptr;
8981         int op;
8982         if (first->op != OP_LABEL) {
8983                 internal_error(state, 0, "block does not start with a label");
8984         }
8985         /* See if this basic block has already been setup */
8986         if (first->u.block != 0) {
8987                 return first->u.block;
8988         }
8989         /* Allocate another basic block structure */
8990         state->last_vertex += 1;
8991         block = xcmalloc(sizeof(*block), "block");
8992         block->first = block->last = first;
8993         block->vertex = state->last_vertex;
8994         ptr = first;
8995         do {
8996                 if ((ptr != first) && (ptr->op == OP_LABEL) && ptr->use) {
8997                         break;
8998                 }
8999                 block->last = ptr;
9000                 /* If ptr->u is not used remember where the baic block is */
9001                 if (triple_stores_block(state, ptr)) {
9002                         ptr->u.block = block;
9003                 }
9004                 if (ptr->op == OP_BRANCH) {
9005                         break;
9006                 }
9007                 ptr = ptr->next;
9008         } while (ptr != RHS(state->main_function, 0));
9009         if (ptr == RHS(state->main_function, 0))
9010                 return block;
9011         op = ptr->op;
9012         if (op == OP_LABEL) {
9013                 block->left = basic_block(state, ptr);
9014                 block->right = 0;
9015                 use_block(block->left, block);
9016         }
9017         else if (op == OP_BRANCH) {
9018                 block->left = 0;
9019                 /* Trace the branch target */
9020                 block->right = basic_block(state, TARG(ptr, 0));
9021                 use_block(block->right, block);
9022                 /* If there is a test trace the branch as well */
9023                 if (TRIPLE_RHS(ptr->sizes)) {
9024                         block->left = basic_block(state, ptr->next);
9025                         use_block(block->left, block);
9026                 }
9027         }
9028         else {
9029                 internal_error(state, 0, "Bad basic block split");
9030         }
9031         return block;
9032 }
9033
9034
9035 static void walk_blocks(struct compile_state *state,
9036         void (*cb)(struct compile_state *state, struct block *block, void *arg),
9037         void *arg)
9038 {
9039         struct triple *ptr, *first;
9040         struct block *last_block;
9041         last_block = 0;
9042         first = RHS(state->main_function, 0);
9043         ptr = first;
9044         do {
9045                 struct block *block;
9046                 if (ptr->op == OP_LABEL) {
9047                         block = ptr->u.block;
9048                         if (block && (block != last_block)) {
9049                                 cb(state, block, arg);
9050                         }
9051                         last_block = block;
9052                 }
9053                 ptr = ptr->next;
9054         } while(ptr != first);
9055 }
9056
9057 static void print_block(
9058         struct compile_state *state, struct block *block, void *arg)
9059 {
9060         struct triple *ptr;
9061         FILE *fp = arg;
9062
9063         fprintf(fp, "\nblock: %p (%d), %p<-%p %p<-%p\n", 
9064                 block, 
9065                 block->vertex,
9066                 block->left, 
9067                 block->left && block->left->use?block->left->use->member : 0,
9068                 block->right, 
9069                 block->right && block->right->use?block->right->use->member : 0);
9070         if (block->first->op == OP_LABEL) {
9071                 fprintf(fp, "%p:\n", block->first);
9072         }
9073         for(ptr = block->first; ; ptr = ptr->next) {
9074                 struct triple_set *user;
9075                 int op = ptr->op;
9076                 
9077                 if (triple_stores_block(state, ptr)) {
9078                         if (ptr->u.block != block) {
9079                                 internal_error(state, ptr, 
9080                                         "Wrong block pointer: %p\n",
9081                                         ptr->u.block);
9082                         }
9083                 }
9084                 if (op == OP_ADECL) {
9085                         for(user = ptr->use; user; user = user->next) {
9086                                 if (!user->member->u.block) {
9087                                         internal_error(state, user->member, 
9088                                                 "Use %p not in a block?\n",
9089                                                 user->member);
9090                                 }
9091                         }
9092                 }
9093                 display_triple(fp, ptr);
9094
9095 #if 0
9096                 for(user = ptr->use; user; user = user->next) {
9097                         fprintf(fp, "use: %p\n", user->member);
9098                 }
9099 #endif
9100
9101                 /* Sanity checks... */
9102                 valid_ins(state, ptr);
9103                 for(user = ptr->use; user; user = user->next) {
9104                         struct triple *use;
9105                         use = user->member;
9106                         valid_ins(state, use);
9107                         if (triple_stores_block(state, user->member) &&
9108                                 !user->member->u.block) {
9109                                 internal_error(state, user->member,
9110                                         "Use %p not in a block?",
9111                                         user->member);
9112                         }
9113                 }
9114
9115                 if (ptr == block->last)
9116                         break;
9117         }
9118         fprintf(fp,"\n");
9119 }
9120
9121
9122 static void print_blocks(struct compile_state *state, FILE *fp)
9123 {
9124         fprintf(fp, "--------------- blocks ---------------\n");
9125         walk_blocks(state, print_block, fp);
9126 }
9127
9128 static void prune_nonblock_triples(struct compile_state *state)
9129 {
9130         struct block *block;
9131         struct triple *first, *ins, *next;
9132         /* Delete the triples not in a basic block */
9133         first = RHS(state->main_function, 0);
9134         block = 0;
9135         ins = first;
9136         do {
9137                 next = ins->next;
9138                 if (ins->op == OP_LABEL) {
9139                         block = ins->u.block;
9140                 }
9141                 if (!block) {
9142                         release_triple(state, ins);
9143                 }
9144                 ins = next;
9145         } while(ins != first);
9146 }
9147
9148 static void setup_basic_blocks(struct compile_state *state)
9149 {
9150         if (!triple_stores_block(state, RHS(state->main_function, 0)) ||
9151                 !triple_stores_block(state, RHS(state->main_function,0)->prev)) {
9152                 internal_error(state, 0, "ins will not store block?");
9153         }
9154         /* Find the basic blocks */
9155         state->last_vertex = 0;
9156         state->first_block = basic_block(state, RHS(state->main_function,0));
9157         /* Delete the triples not in a basic block */
9158         prune_nonblock_triples(state);
9159         /* Find the last basic block */
9160         state->last_block = RHS(state->main_function, 0)->prev->u.block;
9161         if (!state->last_block) {
9162                 internal_error(state, 0, "end not used?");
9163         }
9164         /* Insert an extra unused edge from start to the end 
9165          * This helps with reverse control flow calculations.
9166          */
9167         use_block(state->first_block, state->last_block);
9168         /* If we are debugging print what I have just done */
9169         if (state->debug & DEBUG_BASIC_BLOCKS) {
9170                 print_blocks(state, stdout);
9171                 print_control_flow(state);
9172         }
9173 }
9174
9175 static void free_basic_block(struct compile_state *state, struct block *block)
9176 {
9177         struct block_set *entry, *next;
9178         struct block *child;
9179         if (!block) {
9180                 return;
9181         }
9182         if (block->vertex == -1) {
9183                 return;
9184         }
9185         block->vertex = -1;
9186         if (block->left) {
9187                 unuse_block(block->left, block);
9188         }
9189         if (block->right) {
9190                 unuse_block(block->right, block);
9191         }
9192         if (block->idom) {
9193                 unidom_block(block->idom, block);
9194         }
9195         block->idom = 0;
9196         if (block->ipdom) {
9197                 unipdom_block(block->ipdom, block);
9198         }
9199         block->ipdom = 0;
9200         for(entry = block->use; entry; entry = next) {
9201                 next = entry->next;
9202                 child = entry->member;
9203                 unuse_block(block, child);
9204                 if (child->left == block) {
9205                         child->left = 0;
9206                 }
9207                 if (child->right == block) {
9208                         child->right = 0;
9209                 }
9210         }
9211         for(entry = block->idominates; entry; entry = next) {
9212                 next = entry->next;
9213                 child = entry->member;
9214                 unidom_block(block, child);
9215                 child->idom = 0;
9216         }
9217         for(entry = block->domfrontier; entry; entry = next) {
9218                 next = entry->next;
9219                 child = entry->member;
9220                 undomf_block(block, child);
9221         }
9222         for(entry = block->ipdominates; entry; entry = next) {
9223                 next = entry->next;
9224                 child = entry->member;
9225                 unipdom_block(block, child);
9226                 child->ipdom = 0;
9227         }
9228         for(entry = block->ipdomfrontier; entry; entry = next) {
9229                 next = entry->next;
9230                 child = entry->member;
9231                 unipdomf_block(block, child);
9232         }
9233         if (block->users != 0) {
9234                 internal_error(state, 0, "block still has users");
9235         }
9236         free_basic_block(state, block->left);
9237         block->left = 0;
9238         free_basic_block(state, block->right);
9239         block->right = 0;
9240         memset(block, -1, sizeof(*block));
9241         xfree(block);
9242 }
9243
9244 static void free_basic_blocks(struct compile_state *state)
9245 {
9246         struct triple *first, *ins;
9247         free_basic_block(state, state->first_block);
9248         state->last_vertex = 0;
9249         state->first_block = state->last_block = 0;
9250         first = RHS(state->main_function, 0);
9251         ins = first;
9252         do {
9253                 if (triple_stores_block(state, ins)) {
9254                         ins->u.block = 0;
9255                 }
9256                 ins = ins->next;
9257         } while(ins != first);
9258         
9259 }
9260
9261 struct sdom_block {
9262         struct block *block;
9263         struct sdom_block *sdominates;
9264         struct sdom_block *sdom_next;
9265         struct sdom_block *sdom;
9266         struct sdom_block *label;
9267         struct sdom_block *parent;
9268         struct sdom_block *ancestor;
9269         int vertex;
9270 };
9271
9272
9273 static void unsdom_block(struct sdom_block *block)
9274 {
9275         struct sdom_block **ptr;
9276         if (!block->sdom_next) {
9277                 return;
9278         }
9279         ptr = &block->sdom->sdominates;
9280         while(*ptr) {
9281                 if ((*ptr) == block) {
9282                         *ptr = block->sdom_next;
9283                         return;
9284                 }
9285                 ptr = &(*ptr)->sdom_next;
9286         }
9287 }
9288
9289 static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
9290 {
9291         unsdom_block(block);
9292         block->sdom = sdom;
9293         block->sdom_next = sdom->sdominates;
9294         sdom->sdominates = block;
9295 }
9296
9297
9298
9299 static int initialize_sdblock(struct sdom_block *sd,
9300         struct block *parent, struct block *block, int vertex)
9301 {
9302         if (!block || (sd[block->vertex].block == block)) {
9303                 return vertex;
9304         }
9305         vertex += 1;
9306         /* Renumber the blocks in a convinient fashion */
9307         block->vertex = vertex;
9308         sd[vertex].block    = block;
9309         sd[vertex].sdom     = &sd[vertex];
9310         sd[vertex].label    = &sd[vertex];
9311         sd[vertex].parent   = parent? &sd[parent->vertex] : 0;
9312         sd[vertex].ancestor = 0;
9313         sd[vertex].vertex   = vertex;
9314         vertex = initialize_sdblock(sd, block, block->left, vertex);
9315         vertex = initialize_sdblock(sd, block, block->right, vertex);
9316         return vertex;
9317 }
9318
9319 static int initialize_sdpblock(struct sdom_block *sd,
9320         struct block *parent, struct block *block, int vertex)
9321 {
9322         struct block_set *user;
9323         if (!block || (sd[block->vertex].block == block)) {
9324                 return vertex;
9325         }
9326         vertex += 1;
9327         /* Renumber the blocks in a convinient fashion */
9328         block->vertex = vertex;
9329         sd[vertex].block    = block;
9330         sd[vertex].sdom     = &sd[vertex];
9331         sd[vertex].label    = &sd[vertex];
9332         sd[vertex].parent   = parent? &sd[parent->vertex] : 0;
9333         sd[vertex].ancestor = 0;
9334         sd[vertex].vertex   = vertex;
9335         for(user = block->use; user; user = user->next) {
9336                 vertex = initialize_sdpblock(sd, block, user->member, vertex);
9337         }
9338         return vertex;
9339 }
9340
9341 static void compress_ancestors(struct sdom_block *v)
9342 {
9343         /* This procedure assumes ancestor(v) != 0 */
9344         /* if (ancestor(ancestor(v)) != 0) {
9345          *      compress(ancestor(ancestor(v)));
9346          *      if (semi(label(ancestor(v))) < semi(label(v))) {
9347          *              label(v) = label(ancestor(v));
9348          *      }
9349          *      ancestor(v) = ancestor(ancestor(v));
9350          * }
9351          */
9352         if (!v->ancestor) {
9353                 return;
9354         }
9355         if (v->ancestor->ancestor) {
9356                 compress_ancestors(v->ancestor->ancestor);
9357                 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
9358                         v->label = v->ancestor->label;
9359                 }
9360                 v->ancestor = v->ancestor->ancestor;
9361         }
9362 }
9363
9364 static void compute_sdom(struct compile_state *state, struct sdom_block *sd)
9365 {
9366         int i;
9367         /* // step 2 
9368          *  for each v <= pred(w) {
9369          *      u = EVAL(v);
9370          *      if (semi[u] < semi[w] { 
9371          *              semi[w] = semi[u]; 
9372          *      } 
9373          * }
9374          * add w to bucket(vertex(semi[w]));
9375          * LINK(parent(w), w);
9376          *
9377          * // step 3
9378          * for each v <= bucket(parent(w)) {
9379          *      delete v from bucket(parent(w));
9380          *      u = EVAL(v);
9381          *      dom(v) = (semi[u] < semi[v]) ? u : parent(w);
9382          * }
9383          */
9384         for(i = state->last_vertex; i >= 2; i--) {
9385                 struct sdom_block *v, *parent, *next;
9386                 struct block_set *user;
9387                 struct block *block;
9388                 block = sd[i].block;
9389                 parent = sd[i].parent;
9390                 /* Step 2 */
9391                 for(user = block->use; user; user = user->next) {
9392                         struct sdom_block *v, *u;
9393                         v = &sd[user->member->vertex];
9394                         u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9395                         if (u->sdom->vertex < sd[i].sdom->vertex) {
9396                                 sd[i].sdom = u->sdom;
9397                         }
9398                 }
9399                 sdom_block(sd[i].sdom, &sd[i]);
9400                 sd[i].ancestor = parent;
9401                 /* Step 3 */
9402                 for(v = parent->sdominates; v; v = next) {
9403                         struct sdom_block *u;
9404                         next = v->sdom_next;
9405                         unsdom_block(v);
9406                         u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
9407                         v->block->idom = (u->sdom->vertex < v->sdom->vertex)? 
9408                                 u->block : parent->block;
9409                 }
9410         }
9411 }
9412
9413 static void compute_spdom(struct compile_state *state, struct sdom_block *sd)
9414 {
9415         int i;
9416         /* // step 2 
9417          *  for each v <= pred(w) {
9418          *      u = EVAL(v);
9419          *      if (semi[u] < semi[w] { 
9420          *              semi[w] = semi[u]; 
9421          *      } 
9422          * }
9423          * add w to bucket(vertex(semi[w]));
9424          * LINK(parent(w), w);
9425          *
9426          * // step 3
9427          * for each v <= bucket(parent(w)) {
9428          *      delete v from bucket(parent(w));
9429          *      u = EVAL(v);
9430          *      dom(v) = (semi[u] < semi[v]) ? u : parent(w);
9431          * }
9432          */
9433         for(i = state->last_vertex; i >= 2; i--) {
9434                 struct sdom_block *u, *v, *parent, *next;
9435                 struct block *block;
9436                 block = sd[i].block;
9437                 parent = sd[i].parent;
9438                 /* Step 2 */
9439                 if (block->left) {
9440                         v = &sd[block->left->vertex];
9441                         u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9442                         if (u->sdom->vertex < sd[i].sdom->vertex) {
9443                                 sd[i].sdom = u->sdom;
9444                         }
9445                 }
9446                 if (block->right && (block->right != block->left)) {
9447                         v = &sd[block->right->vertex];
9448                         u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9449                         if (u->sdom->vertex < sd[i].sdom->vertex) {
9450                                 sd[i].sdom = u->sdom;
9451                         }
9452                 }
9453                 sdom_block(sd[i].sdom, &sd[i]);
9454                 sd[i].ancestor = parent;
9455                 /* Step 3 */
9456                 for(v = parent->sdominates; v; v = next) {
9457                         struct sdom_block *u;
9458                         next = v->sdom_next;
9459                         unsdom_block(v);
9460                         u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
9461                         v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)? 
9462                                 u->block : parent->block;
9463                 }
9464         }
9465 }
9466
9467 static void compute_idom(struct compile_state *state, struct sdom_block *sd)
9468 {
9469         int i;
9470         for(i = 2; i <= state->last_vertex; i++) {
9471                 struct block *block;
9472                 block = sd[i].block;
9473                 if (block->idom->vertex != sd[i].sdom->vertex) {
9474                         block->idom = block->idom->idom;
9475                 }
9476                 idom_block(block->idom, block);
9477         }
9478         sd[1].block->idom = 0;
9479 }
9480
9481 static void compute_ipdom(struct compile_state *state, struct sdom_block *sd)
9482 {
9483         int i;
9484         for(i = 2; i <= state->last_vertex; i++) {
9485                 struct block *block;
9486                 block = sd[i].block;
9487                 if (block->ipdom->vertex != sd[i].sdom->vertex) {
9488                         block->ipdom = block->ipdom->ipdom;
9489                 }
9490                 ipdom_block(block->ipdom, block);
9491         }
9492         sd[1].block->ipdom = 0;
9493 }
9494
9495         /* Theorem 1:
9496          *   Every vertex of a flowgraph G = (V, E, r) except r has
9497          *   a unique immediate dominator.  
9498          *   The edges {(idom(w), w) |w <= V - {r}} form a directed tree
9499          *   rooted at r, called the dominator tree of G, such that 
9500          *   v dominates w if and only if v is a proper ancestor of w in
9501          *   the dominator tree.
9502          */
9503         /* Lemma 1:  
9504          *   If v and w are vertices of G such that v <= w,
9505          *   than any path from v to w must contain a common ancestor
9506          *   of v and w in T.
9507          */
9508         /* Lemma 2:  For any vertex w != r, idom(w) -> w */
9509         /* Lemma 3:  For any vertex w != r, sdom(w) -> w */
9510         /* Lemma 4:  For any vertex w != r, idom(w) -> sdom(w) */
9511         /* Theorem 2:
9512          *   Let w != r.  Suppose every u for which sdom(w) -> u -> w satisfies
9513          *   sdom(u) >= sdom(w).  Then idom(w) = sdom(w).
9514          */
9515         /* Theorem 3:
9516          *   Let w != r and let u be a vertex for which sdom(u) is 
9517          *   minimum amoung vertices u satisfying sdom(w) -> u -> w.
9518          *   Then sdom(u) <= sdom(w) and idom(u) = idom(w).
9519          */
9520         /* Lemma 5:  Let vertices v,w satisfy v -> w.
9521          *           Then v -> idom(w) or idom(w) -> idom(v)
9522          */
9523
9524 static void find_immediate_dominators(struct compile_state *state)
9525 {
9526         struct sdom_block *sd;
9527         /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
9528          *           vi > w for (1 <= i <= k - 1}
9529          */
9530         /* Theorem 4:
9531          *   For any vertex w != r.
9532          *   sdom(w) = min(
9533          *                 {v|(v,w) <= E  and v < w } U 
9534          *                 {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
9535          */
9536         /* Corollary 1:
9537          *   Let w != r and let u be a vertex for which sdom(u) is 
9538          *   minimum amoung vertices u satisfying sdom(w) -> u -> w.
9539          *   Then:
9540          *                   { sdom(w) if sdom(w) = sdom(u),
9541          *        idom(w) = {
9542          *                   { idom(u) otherwise
9543          */
9544         /* The algorithm consists of the following 4 steps.
9545          * Step 1.  Carry out a depth-first search of the problem graph.  
9546          *    Number the vertices from 1 to N as they are reached during
9547          *    the search.  Initialize the variables used in succeeding steps.
9548          * Step 2.  Compute the semidominators of all vertices by applying
9549          *    theorem 4.   Carry out the computation vertex by vertex in
9550          *    decreasing order by number.
9551          * Step 3.  Implicitly define the immediate dominator of each vertex
9552          *    by applying Corollary 1.
9553          * Step 4.  Explicitly define the immediate dominator of each vertex,
9554          *    carrying out the computation vertex by vertex in increasing order
9555          *    by number.
9556          */
9557         /* Step 1 initialize the basic block information */
9558         sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
9559         initialize_sdblock(sd, 0, state->first_block, 0);
9560 #if 0
9561         sd[1].size  = 0;
9562         sd[1].label = 0;
9563         sd[1].sdom  = 0;
9564 #endif
9565         /* Step 2 compute the semidominators */
9566         /* Step 3 implicitly define the immediate dominator of each vertex */
9567         compute_sdom(state, sd);
9568         /* Step 4 explicitly define the immediate dominator of each vertex */
9569         compute_idom(state, sd);
9570         xfree(sd);
9571 }
9572
9573 static void find_post_dominators(struct compile_state *state)
9574 {
9575         struct sdom_block *sd;
9576         /* Step 1 initialize the basic block information */
9577         sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
9578
9579         initialize_sdpblock(sd, 0, state->last_block, 0);
9580
9581         /* Step 2 compute the semidominators */
9582         /* Step 3 implicitly define the immediate dominator of each vertex */
9583         compute_spdom(state, sd);
9584         /* Step 4 explicitly define the immediate dominator of each vertex */
9585         compute_ipdom(state, sd);
9586         xfree(sd);
9587 }
9588
9589
9590
9591 static void find_block_domf(struct compile_state *state, struct block *block)
9592 {
9593         struct block *child;
9594         struct block_set *user;
9595         if (block->domfrontier != 0) {
9596                 internal_error(state, block->first, "domfrontier present?");
9597         }
9598         for(user = block->idominates; user; user = user->next) {
9599                 child = user->member;
9600                 if (child->idom != block) {
9601                         internal_error(state, block->first, "bad idom");
9602                 }
9603                 find_block_domf(state, child);
9604         }
9605         if (block->left && block->left->idom != block) {
9606                 domf_block(block, block->left);
9607         }
9608         if (block->right && block->right->idom != block) {
9609                 domf_block(block, block->right);
9610         }
9611         for(user = block->idominates; user; user = user->next) {
9612                 struct block_set *frontier;
9613                 child = user->member;
9614                 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
9615                         if (frontier->member->idom != block) {
9616                                 domf_block(block, frontier->member);
9617                         }
9618                 }
9619         }
9620 }
9621
9622 static void find_block_ipdomf(struct compile_state *state, struct block *block)
9623 {
9624         struct block *child;
9625         struct block_set *user;
9626         if (block->ipdomfrontier != 0) {
9627                 internal_error(state, block->first, "ipdomfrontier present?");
9628         }
9629         for(user = block->ipdominates; user; user = user->next) {
9630                 child = user->member;
9631                 if (child->ipdom != block) {
9632                         internal_error(state, block->first, "bad ipdom");
9633                 }
9634                 find_block_ipdomf(state, child);
9635         }
9636         if (block->left && block->left->ipdom != block) {
9637                 ipdomf_block(block, block->left);
9638         }
9639         if (block->right && block->right->ipdom != block) {
9640                 ipdomf_block(block, block->right);
9641         }
9642         for(user = block->idominates; user; user = user->next) {
9643                 struct block_set *frontier;
9644                 child = user->member;
9645                 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
9646                         if (frontier->member->ipdom != block) {
9647                                 ipdomf_block(block, frontier->member);
9648                         }
9649                 }
9650         }
9651 }
9652
9653 static void print_dominated(
9654         struct compile_state *state, struct block *block, void *arg)
9655 {
9656         struct block_set *user;
9657         FILE *fp = arg;
9658
9659         fprintf(fp, "%d:", block->vertex);
9660         for(user = block->idominates; user; user = user->next) {
9661                 fprintf(fp, " %d", user->member->vertex);
9662                 if (user->member->idom != block) {
9663                         internal_error(state, user->member->first, "bad idom");
9664                 }
9665         }
9666         fprintf(fp,"\n");
9667 }
9668
9669 static void print_dominators(struct compile_state *state, FILE *fp)
9670 {
9671         fprintf(fp, "\ndominates\n");
9672         walk_blocks(state, print_dominated, fp);
9673 }
9674
9675
9676 static int print_frontiers(
9677         struct compile_state *state, struct block *block, int vertex)
9678 {
9679         struct block_set *user;
9680
9681         if (!block || (block->vertex != vertex + 1)) {
9682                 return vertex;
9683         }
9684         vertex += 1;
9685
9686         printf("%d:", block->vertex);
9687         for(user = block->domfrontier; user; user = user->next) {
9688                 printf(" %d", user->member->vertex);
9689         }
9690         printf("\n");
9691
9692         vertex = print_frontiers(state, block->left, vertex);
9693         vertex = print_frontiers(state, block->right, vertex);
9694         return vertex;
9695 }
9696 static void print_dominance_frontiers(struct compile_state *state)
9697 {
9698         printf("\ndominance frontiers\n");
9699         print_frontiers(state, state->first_block, 0);
9700         
9701 }
9702
9703 static void analyze_idominators(struct compile_state *state)
9704 {
9705         /* Find the immediate dominators */
9706         find_immediate_dominators(state);
9707         /* Find the dominance frontiers */
9708         find_block_domf(state, state->first_block);
9709         /* If debuging print the print what I have just found */
9710         if (state->debug & DEBUG_FDOMINATORS) {
9711                 print_dominators(state, stdout);
9712                 print_dominance_frontiers(state);
9713                 print_control_flow(state);
9714         }
9715 }
9716
9717
9718
9719 static void print_ipdominated(
9720         struct compile_state *state, struct block *block, void *arg)
9721 {
9722         struct block_set *user;
9723         FILE *fp = arg;
9724
9725         fprintf(fp, "%d:", block->vertex);
9726         for(user = block->ipdominates; user; user = user->next) {
9727                 fprintf(fp, " %d", user->member->vertex);
9728                 if (user->member->ipdom != block) {
9729                         internal_error(state, user->member->first, "bad ipdom");
9730                 }
9731         }
9732         fprintf(fp, "\n");
9733 }
9734
9735 static void print_ipdominators(struct compile_state *state, FILE *fp)
9736 {
9737         fprintf(fp, "\nipdominates\n");
9738         walk_blocks(state, print_ipdominated, fp);
9739 }
9740
9741 static int print_pfrontiers(
9742         struct compile_state *state, struct block *block, int vertex)
9743 {
9744         struct block_set *user;
9745
9746         if (!block || (block->vertex != vertex + 1)) {
9747                 return vertex;
9748         }
9749         vertex += 1;
9750
9751         printf("%d:", block->vertex);
9752         for(user = block->ipdomfrontier; user; user = user->next) {
9753                 printf(" %d", user->member->vertex);
9754         }
9755         printf("\n");
9756         for(user = block->use; user; user = user->next) {
9757                 vertex = print_pfrontiers(state, user->member, vertex);
9758         }
9759         return vertex;
9760 }
9761 static void print_ipdominance_frontiers(struct compile_state *state)
9762 {
9763         printf("\nipdominance frontiers\n");
9764         print_pfrontiers(state, state->last_block, 0);
9765         
9766 }
9767
9768 static void analyze_ipdominators(struct compile_state *state)
9769 {
9770         /* Find the post dominators */
9771         find_post_dominators(state);
9772         /* Find the control dependencies (post dominance frontiers) */
9773         find_block_ipdomf(state, state->last_block);
9774         /* If debuging print the print what I have just found */
9775         if (state->debug & DEBUG_RDOMINATORS) {
9776                 print_ipdominators(state, stdout);
9777                 print_ipdominance_frontiers(state);
9778                 print_control_flow(state);
9779         }
9780 }
9781
9782 static int bdominates(struct compile_state *state,
9783         struct block *dom, struct block *sub)
9784 {
9785         while(sub && (sub != dom)) {
9786                 sub = sub->idom;
9787         }
9788         return sub == dom;
9789 }
9790
9791 static int tdominates(struct compile_state *state,
9792         struct triple *dom, struct triple *sub)
9793 {
9794         struct block *bdom, *bsub;
9795         int result;
9796         bdom = block_of_triple(state, dom);
9797         bsub = block_of_triple(state, sub);
9798         if (bdom != bsub) {
9799                 result = bdominates(state, bdom, bsub);
9800         } 
9801         else {
9802                 struct triple *ins;
9803                 ins = sub;
9804                 while((ins != bsub->first) && (ins != dom)) {
9805                         ins = ins->prev;
9806                 }
9807                 result = (ins == dom);
9808         }
9809         return result;
9810 }
9811
9812 static void insert_phi_operations(struct compile_state *state)
9813 {
9814         size_t size;
9815         struct triple *first;
9816         int *has_already, *work;
9817         struct block *work_list, **work_list_tail;
9818         int iter;
9819         struct triple *var;
9820
9821         size = sizeof(int) * (state->last_vertex + 1);
9822         has_already = xcmalloc(size, "has_already");
9823         work =        xcmalloc(size, "work");
9824         iter = 0;
9825
9826         first = RHS(state->main_function, 0);
9827         for(var = first->next; var != first ; var = var->next) {
9828                 struct block *block;
9829                 struct triple_set *user;
9830                 if ((var->op != OP_ADECL) || !var->use) {
9831                         continue;
9832                 }
9833                 iter += 1;
9834                 work_list = 0;
9835                 work_list_tail = &work_list;
9836                 for(user = var->use; user; user = user->next) {
9837                         if (user->member->op == OP_READ) {
9838                                 continue;
9839                         }
9840                         if (user->member->op != OP_WRITE) {
9841                                 internal_error(state, user->member, 
9842                                         "bad variable access");
9843                         }
9844                         block = user->member->u.block;
9845                         if (!block) {
9846                                 warning(state, user->member, "dead code");
9847                         }
9848                         if (work[block->vertex] >= iter) {
9849                                 continue;
9850                         }
9851                         work[block->vertex] = iter;
9852                         *work_list_tail = block;
9853                         block->work_next = 0;
9854                         work_list_tail = &block->work_next;
9855                 }
9856                 for(block = work_list; block; block = block->work_next) {
9857                         struct block_set *df;
9858                         for(df = block->domfrontier; df; df = df->next) {
9859                                 struct triple *phi;
9860                                 struct block *front;
9861                                 int in_edges;
9862                                 front = df->member;
9863
9864                                 if (has_already[front->vertex] >= iter) {
9865                                         continue;
9866                                 }
9867                                 /* Count how many edges flow into this block */
9868                                 in_edges = front->users;
9869                                 /* Insert a phi function for this variable */
9870                                 phi = alloc_triple(
9871                                         state, OP_PHI, var->type, -1, in_edges, 
9872                                         front->first->filename, 
9873                                         front->first->line,
9874                                         front->first->col);
9875                                 phi->u.block = front;
9876                                 MISC(phi, 0) = var;
9877                                 use_triple(var, phi);
9878                                 /* Insert the phi functions immediately after the label */
9879                                 insert_triple(state, front->first->next, phi);
9880                                 if (front->first == front->last) {
9881                                         front->last = front->first->next;
9882                                 }
9883                                 has_already[front->vertex] = iter;
9884
9885                                 /* If necessary plan to visit the basic block */
9886                                 if (work[front->vertex] >= iter) {
9887                                         continue;
9888                                 }
9889                                 work[front->vertex] = iter;
9890                                 *work_list_tail = front;
9891                                 front->work_next = 0;
9892                                 work_list_tail = &front->work_next;
9893                         }
9894                 }
9895         }
9896         xfree(has_already);
9897         xfree(work);
9898 }
9899
9900 /*
9901  * C(V)
9902  * S(V)
9903  */
9904 static void fixup_block_phi_variables(
9905         struct compile_state *state, struct block *parent, struct block *block)
9906 {
9907         struct block_set *set;
9908         struct triple *ptr;
9909         int edge;
9910         if (!parent || !block)
9911                 return;
9912         /* Find the edge I am coming in on */
9913         edge = 0;
9914         for(set = block->use; set; set = set->next, edge++) {
9915                 if (set->member == parent) {
9916                         break;
9917                 }
9918         }
9919         if (!set) {
9920                 internal_error(state, 0, "phi input is not on a control predecessor");
9921         }
9922         for(ptr = block->first; ; ptr = ptr->next) {
9923                 if (ptr->op == OP_PHI) {
9924                         struct triple *var, *val, **slot;
9925                         var = MISC(ptr, 0);
9926                         if (!var) {
9927                                 internal_error(state, ptr, "no var???");
9928                         }
9929                         /* Find the current value of the variable */
9930                         val = var->use->member;
9931                         if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
9932                                 internal_error(state, val, "bad value in phi");
9933                         }
9934                         if (edge >= TRIPLE_RHS(ptr->sizes)) {
9935                                 internal_error(state, ptr, "edges > phi rhs");
9936                         }
9937                         slot = &RHS(ptr, edge);
9938                         if ((*slot != 0) && (*slot != val)) {
9939                                 internal_error(state, ptr, "phi already bound on this edge");
9940                         }
9941                         *slot = val;
9942                         use_triple(val, ptr);
9943                 }
9944                 if (ptr == block->last) {
9945                         break;
9946                 }
9947         }
9948 }
9949
9950
9951 static void rename_block_variables(
9952         struct compile_state *state, struct block *block)
9953 {
9954         struct block_set *user;
9955         struct triple *ptr, *next, *last;
9956         int done;
9957         if (!block)
9958                 return;
9959         last = block->first;
9960         done = 0;
9961         for(ptr = block->first; !done; ptr = next) {
9962                 next = ptr->next;
9963                 if (ptr == block->last) {
9964                         done = 1;
9965                 }
9966                 /* RHS(A) */
9967                 if (ptr->op == OP_READ) {
9968                         struct triple *var, *val;
9969                         var = RHS(ptr, 0);
9970                         unuse_triple(var, ptr);
9971                         if (!var->use) {
9972                                 error(state, ptr, "variable used without being set");
9973                         }
9974                         /* Find the current value of the variable */
9975                         val = var->use->member;
9976                         if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
9977                                 internal_error(state, val, "bad value in read");
9978                         }
9979                         propogate_use(state, ptr, val);
9980                         release_triple(state, ptr);
9981                         continue;
9982                 }
9983                 /* LHS(A) */
9984                 if (ptr->op == OP_WRITE) {
9985                         struct triple *var, *val;
9986                         var = LHS(ptr, 0);
9987                         val = RHS(ptr, 0);
9988                         if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
9989                                 internal_error(state, val, "bad value in write");
9990                         }
9991                         propogate_use(state, ptr, val);
9992                         unuse_triple(var, ptr);
9993                         /* Push OP_WRITE ptr->right onto a stack of variable uses */
9994                         push_triple(var, val);
9995                 }
9996                 if (ptr->op == OP_PHI) {
9997                         struct triple *var;
9998                         var = MISC(ptr, 0);
9999                         /* Push OP_PHI onto a stack of variable uses */
10000                         push_triple(var, ptr);
10001                 }
10002                 last = ptr;
10003         }
10004         block->last = last;
10005
10006         /* Fixup PHI functions in the cf successors */
10007         fixup_block_phi_variables(state, block, block->left);
10008         fixup_block_phi_variables(state, block, block->right);
10009         /* rename variables in the dominated nodes */
10010         for(user = block->idominates; user; user = user->next) {
10011                 rename_block_variables(state, user->member);
10012         }
10013         /* pop the renamed variable stack */
10014         last = block->first;
10015         done = 0;
10016         for(ptr = block->first; !done ; ptr = next) {
10017                 next = ptr->next;
10018                 if (ptr == block->last) {
10019                         done = 1;
10020                 }
10021                 if (ptr->op == OP_WRITE) {
10022                         struct triple *var;
10023                         var = LHS(ptr, 0);
10024                         /* Pop OP_WRITE ptr->right from the stack of variable uses */
10025                         pop_triple(var, RHS(ptr, 0));
10026                         release_triple(state, ptr);
10027                         continue;
10028                 }
10029                 if (ptr->op == OP_PHI) {
10030                         struct triple *var;
10031                         var = MISC(ptr, 0);
10032                         /* Pop OP_WRITE ptr->right from the stack of variable uses */
10033                         pop_triple(var, ptr);
10034                 }
10035                 last = ptr;
10036         }
10037         block->last = last;
10038 }
10039
10040 static void prune_block_variables(struct compile_state *state,
10041         struct block *block)
10042 {
10043         struct block_set *user;
10044         struct triple *next, *last, *ptr;
10045         int done;
10046         last = block->first;
10047         done = 0;
10048         for(ptr = block->first; !done; ptr = next) {
10049                 next = ptr->next;
10050                 if (ptr == block->last) {
10051                         done = 1;
10052                 }
10053                 if (ptr->op == OP_ADECL) {
10054                         struct triple_set *user, *next;
10055                         for(user = ptr->use; user; user = next) {
10056                                 struct triple *use;
10057                                 next = user->next;
10058                                 use = user->member;
10059                                 if (use->op != OP_PHI) {
10060                                         internal_error(state, use, "decl still used");
10061                                 }
10062                                 if (MISC(use, 0) != ptr) {
10063                                         internal_error(state, use, "bad phi use of decl");
10064                                 }
10065                                 unuse_triple(ptr, use);
10066                                 MISC(use, 0) = 0;
10067                         }
10068                         release_triple(state, ptr);
10069                         continue;
10070                 }
10071                 last = ptr;
10072         }
10073         block->last = last;
10074         for(user = block->idominates; user; user = user->next) {
10075                 prune_block_variables(state, user->member);
10076         }
10077 }
10078
10079 static void transform_to_ssa_form(struct compile_state *state)
10080 {
10081         insert_phi_operations(state);
10082 #if 0
10083         printf("@%s:%d\n", __FILE__, __LINE__);
10084         print_blocks(state, stdout);
10085 #endif
10086         rename_block_variables(state, state->first_block);
10087         prune_block_variables(state, state->first_block);
10088 }
10089
10090
10091 static void clear_vertex(
10092         struct compile_state *state, struct block *block, void *arg)
10093 {
10094         block->vertex = 0;
10095 }
10096
10097 static void mark_live_block(
10098         struct compile_state *state, struct block *block, int *next_vertex)
10099 {
10100         /* See if this is a block that has not been marked */
10101         if (block->vertex != 0) {
10102                 return;
10103         }
10104         block->vertex = *next_vertex;
10105         *next_vertex += 1;
10106         if (triple_is_branch(state, block->last)) {
10107                 struct triple **targ;
10108                 targ = triple_targ(state, block->last, 0);
10109                 for(; targ; targ = triple_targ(state, block->last, targ)) {
10110                         if (!*targ) {
10111                                 continue;
10112                         }
10113                         if (!triple_stores_block(state, *targ)) {
10114                                 internal_error(state, 0, "bad targ");
10115                         }
10116                         mark_live_block(state, (*targ)->u.block, next_vertex);
10117                 }
10118         }
10119         else if (block->last->next != RHS(state->main_function, 0)) {
10120                 struct triple *ins;
10121                 ins = block->last->next;
10122                 if (!triple_stores_block(state, ins)) {
10123                         internal_error(state, 0, "bad block start");
10124                 }
10125                 mark_live_block(state, ins->u.block, next_vertex);
10126         }
10127 }
10128
10129 static void transform_from_ssa_form(struct compile_state *state)
10130 {
10131         /* To get out of ssa form we insert moves on the incoming
10132          * edges to blocks containting phi functions.
10133          */
10134         struct triple *first;
10135         struct triple *phi, *next;
10136         int next_vertex;
10137
10138         /* Walk the control flow to see which blocks remain alive */
10139         walk_blocks(state, clear_vertex, 0);
10140         next_vertex = 1;
10141         mark_live_block(state, state->first_block, &next_vertex);
10142
10143         /* Walk all of the operations to find the phi functions */
10144         first = RHS(state->main_function, 0);
10145         for(phi = first->next; phi != first ; phi = next) {
10146                 struct block_set *set;
10147                 struct block *block;
10148                 struct triple **slot;
10149                 struct triple *var, *read;
10150                 struct triple_set *use, *use_next;
10151                 int edge, used;
10152                 next = phi->next;
10153                 if (phi->op != OP_PHI) {
10154                         continue;
10155                 }
10156                 block = phi->u.block;
10157                 slot  = &RHS(phi, 0);
10158
10159                 /* Forget uses from code in dead blocks */
10160                 for(use = phi->use; use; use = use_next) {
10161                         struct block *ublock;
10162                         struct triple **expr;
10163                         use_next = use->next;
10164                         ublock = block_of_triple(state, use->member);
10165                         if ((use->member == phi) || (ublock->vertex != 0)) {
10166                                 continue;
10167                         }
10168                         expr = triple_rhs(state, use->member, 0);
10169                         for(; expr; expr = triple_rhs(state, use->member, expr)) {
10170                                 if (*expr == phi) {
10171                                         *expr = 0;
10172                                 }
10173                         }
10174                         unuse_triple(phi, use->member);
10175                 }
10176
10177                 /* A variable to replace the phi function */
10178                 var = post_triple(state, phi, OP_ADECL, phi->type, 0,0);
10179                 /* A read of the single value that is set into the variable */
10180                 read = post_triple(state, var, OP_READ, phi->type, var, 0);
10181                 use_triple(var, read);
10182
10183                 /* Replaces uses of the phi with variable reads */
10184                 propogate_use(state, phi, read);
10185
10186                 /* Walk all of the incoming edges/blocks and insert moves.
10187                  */
10188                 for(edge = 0, set = block->use; set; set = set->next, edge++) {
10189                         struct block *eblock;
10190                         struct triple *move;
10191                         struct triple *val;
10192                         eblock = set->member;
10193                         val = slot[edge];
10194                         slot[edge] = 0;
10195                         unuse_triple(val, phi);
10196
10197                         if (!val || (val == &zero_triple) ||
10198                                 (block->vertex == 0) || (eblock->vertex == 0) ||
10199                                 (val == phi) || (val == read)) {
10200                                 continue;
10201                         }
10202                         
10203                         move = post_triple(state, 
10204                                 val, OP_WRITE, phi->type, var, val);
10205                         use_triple(val, move);
10206                         use_triple(var, move);
10207                 }               
10208                 /* See if there are any writers of var */
10209                 used = 0;
10210                 for(use = var->use; use; use = use->next) {
10211                         struct triple **expr;
10212                         expr = triple_lhs(state, use->member, 0);
10213                         for(; expr; expr = triple_lhs(state, use->member, expr)) {
10214                                 if (*expr == var) {
10215                                         used = 1;
10216                                 }
10217                         }
10218                 }
10219                 /* If var is not used free it */
10220                 if (!used) {
10221                         unuse_triple(var, read);
10222                         free_triple(state, read);
10223                         free_triple(state, var);
10224                 }
10225
10226                 /* Release the phi function */
10227                 release_triple(state, phi);
10228         }
10229         
10230 }
10231
10232
10233 /* 
10234  * Register conflict resolution
10235  * =========================================================
10236  */
10237
10238 static struct reg_info find_def_color(
10239         struct compile_state *state, struct triple *def)
10240 {
10241         struct triple_set *set;
10242         struct reg_info info;
10243         info.reg = REG_UNSET;
10244         info.regcm = 0;
10245         if (!triple_is_def(state, def)) {
10246                 return info;
10247         }
10248         info = arch_reg_lhs(state, def, 0);
10249         if (info.reg >= MAX_REGISTERS) {
10250                 info.reg = REG_UNSET;
10251         }
10252         for(set = def->use; set; set = set->next) {
10253                 struct reg_info tinfo;
10254                 int i;
10255                 i = find_rhs_use(state, set->member, def);
10256                 if (i < 0) {
10257                         continue;
10258                 }
10259                 tinfo = arch_reg_rhs(state, set->member, i);
10260                 if (tinfo.reg >= MAX_REGISTERS) {
10261                         tinfo.reg = REG_UNSET;
10262                 }
10263                 if ((tinfo.reg != REG_UNSET) && 
10264                         (info.reg != REG_UNSET) &&
10265                         (tinfo.reg != info.reg)) {
10266                         internal_error(state, def, "register conflict");
10267                 }
10268                 if ((info.regcm & tinfo.regcm) == 0) {
10269                         internal_error(state, def, "regcm conflict %x & %x == 0",
10270                                 info.regcm, tinfo.regcm);
10271                 }
10272                 if (info.reg == REG_UNSET) {
10273                         info.reg = tinfo.reg;
10274                 }
10275                 info.regcm &= tinfo.regcm;
10276         }
10277         if (info.reg >= MAX_REGISTERS) {
10278                 internal_error(state, def, "register out of range");
10279         }
10280         return info;
10281 }
10282
10283 static struct reg_info find_lhs_pre_color(
10284         struct compile_state *state, struct triple *ins, int index)
10285 {
10286         struct reg_info info;
10287         int zlhs, zrhs, i;
10288         zrhs = TRIPLE_RHS(ins->sizes);
10289         zlhs = TRIPLE_LHS(ins->sizes);
10290         if (!zlhs && triple_is_def(state, ins)) {
10291                 zlhs = 1;
10292         }
10293         if (index >= zlhs) {
10294                 internal_error(state, ins, "Bad lhs %d", index);
10295         }
10296         info = arch_reg_lhs(state, ins, index);
10297         for(i = 0; i < zrhs; i++) {
10298                 struct reg_info rinfo;
10299                 rinfo = arch_reg_rhs(state, ins, i);
10300                 if ((info.reg == rinfo.reg) &&
10301                         (rinfo.reg >= MAX_REGISTERS)) {
10302                         struct reg_info tinfo;
10303                         tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
10304                         info.reg = tinfo.reg;
10305                         info.regcm &= tinfo.regcm;
10306                         break;
10307                 }
10308         }
10309         if (info.reg >= MAX_REGISTERS) {
10310                 info.reg = REG_UNSET;
10311         }
10312         return info;
10313 }
10314
10315 static struct reg_info find_rhs_post_color(
10316         struct compile_state *state, struct triple *ins, int index);
10317
10318 static struct reg_info find_lhs_post_color(
10319         struct compile_state *state, struct triple *ins, int index)
10320 {
10321         struct triple_set *set;
10322         struct reg_info info;
10323         struct triple *lhs;
10324 #if 0
10325         fprintf(stderr, "find_lhs_post_color(%p, %d)\n",
10326                 ins, index);
10327 #endif
10328         if ((index == 0) && triple_is_def(state, ins)) {
10329                 lhs = ins;
10330         }
10331         else if (index < TRIPLE_LHS(ins->sizes)) {
10332                 lhs = LHS(ins, index);
10333         }
10334         else {
10335                 internal_error(state, ins, "Bad lhs %d", index);
10336                 lhs = 0;
10337         }
10338         info = arch_reg_lhs(state, ins, index);
10339         if (info.reg >= MAX_REGISTERS) {
10340                 info.reg = REG_UNSET;
10341         }
10342         for(set = lhs->use; set; set = set->next) {
10343                 struct reg_info rinfo;
10344                 struct triple *user;
10345                 int zrhs, i;
10346                 user = set->member;
10347                 zrhs = TRIPLE_RHS(user->sizes);
10348                 for(i = 0; i < zrhs; i++) {
10349                         if (RHS(user, i) != lhs) {
10350                                 continue;
10351                         }
10352                         rinfo = find_rhs_post_color(state, user, i);
10353                         if ((info.reg != REG_UNSET) &&
10354                                 (rinfo.reg != REG_UNSET) &&
10355                                 (info.reg != rinfo.reg)) {
10356                                 internal_error(state, ins, "register conflict");
10357                         }
10358                         if ((info.regcm & rinfo.regcm) == 0) {
10359                                 internal_error(state, ins, "regcm conflict %x & %x == 0",
10360                                         info.regcm, rinfo.regcm);
10361                         }
10362                         if (info.reg == REG_UNSET) {
10363                                 info.reg = rinfo.reg;
10364                         }
10365                         info.regcm &= rinfo.regcm;
10366                 }
10367         }
10368 #if 0
10369         fprintf(stderr, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
10370                 ins, index, info.reg, info.regcm);
10371 #endif
10372         return info;
10373 }
10374
10375 static struct reg_info find_rhs_post_color(
10376         struct compile_state *state, struct triple *ins, int index)
10377 {
10378         struct reg_info info, rinfo;
10379         int zlhs, i;
10380 #if 0
10381         fprintf(stderr, "find_rhs_post_color(%p, %d)\n",
10382                 ins, index);
10383 #endif
10384         rinfo = arch_reg_rhs(state, ins, index);
10385         zlhs = TRIPLE_LHS(ins->sizes);
10386         if (!zlhs && triple_is_def(state, ins)) {
10387                 zlhs = 1;
10388         }
10389         info = rinfo;
10390         if (info.reg >= MAX_REGISTERS) {
10391                 info.reg = REG_UNSET;
10392         }
10393         for(i = 0; i < zlhs; i++) {
10394                 struct reg_info linfo;
10395                 linfo = arch_reg_lhs(state, ins, i);
10396                 if ((linfo.reg == rinfo.reg) &&
10397                         (linfo.reg >= MAX_REGISTERS)) {
10398                         struct reg_info tinfo;
10399                         tinfo = find_lhs_post_color(state, ins, i);
10400                         if (tinfo.reg >= MAX_REGISTERS) {
10401                                 tinfo.reg = REG_UNSET;
10402                         }
10403                         info.regcm &= linfo.reg;
10404                         info.regcm &= tinfo.regcm;
10405                         if (info.reg != REG_UNSET) {
10406                                 internal_error(state, ins, "register conflict");
10407                         }
10408                         if (info.regcm == 0) {
10409                                 internal_error(state, ins, "regcm conflict");
10410                         }
10411                         info.reg = tinfo.reg;
10412                 }
10413         }
10414 #if 0
10415         fprintf(stderr, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
10416                 ins, index, info.reg, info.regcm);
10417 #endif
10418         return info;
10419 }
10420
10421 static struct reg_info find_lhs_color(
10422         struct compile_state *state, struct triple *ins, int index)
10423 {
10424         struct reg_info pre, post, info;
10425 #if 0
10426         fprintf(stderr, "find_lhs_color(%p, %d)\n",
10427                 ins, index);
10428 #endif
10429         pre = find_lhs_pre_color(state, ins, index);
10430         post = find_lhs_post_color(state, ins, index);
10431         if ((pre.reg != post.reg) &&
10432                 (pre.reg != REG_UNSET) &&
10433                 (post.reg != REG_UNSET)) {
10434                 internal_error(state, ins, "register conflict");
10435         }
10436         info.regcm = pre.regcm & post.regcm;
10437         info.reg = pre.reg;
10438         if (info.reg == REG_UNSET) {
10439                 info.reg = post.reg;
10440         }
10441 #if 0
10442         fprintf(stderr, "find_lhs_color(%p, %d) -> ( %d, %x)\n",
10443                 ins, index, info.reg, info.regcm);
10444 #endif
10445         return info;
10446 }
10447
10448 static struct triple *post_copy(struct compile_state *state, struct triple *ins)
10449 {
10450         struct triple_set *entry, *next;
10451         struct triple *out;
10452         struct reg_info info, rinfo;
10453
10454         info = arch_reg_lhs(state, ins, 0);
10455         out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
10456         use_triple(RHS(out, 0), out);
10457         /* Get the users of ins to use out instead */
10458         for(entry = ins->use; entry; entry = next) {
10459                 int i;
10460                 next = entry->next;
10461                 if (entry->member == out) {
10462                         continue;
10463                 }
10464                 i = find_rhs_use(state, entry->member, ins);
10465                 if (i < 0) {
10466                         continue;
10467                 }
10468                 rinfo = arch_reg_rhs(state, entry->member, i);
10469                 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
10470                         continue;
10471                 }
10472                 replace_rhs_use(state, ins, out, entry->member);
10473         }
10474         transform_to_arch_instruction(state, out);
10475         return out;
10476 }
10477
10478 static struct triple *pre_copy(
10479         struct compile_state *state, struct triple *ins, int index)
10480 {
10481         /* Carefully insert enough operations so that I can
10482          * enter any operation with a GPR32.
10483          */
10484         struct triple *in;
10485         struct triple **expr;
10486         expr = &RHS(ins, index);
10487         in = pre_triple(state, ins, OP_COPY, (*expr)->type, *expr, 0);
10488         unuse_triple(*expr, ins);
10489         *expr = in;
10490         use_triple(RHS(in, 0), in);
10491         use_triple(in, ins);
10492         transform_to_arch_instruction(state, in);
10493         return in;
10494 }
10495
10496
10497 static void insert_copies_to_phi(struct compile_state *state)
10498 {
10499         /* To get out of ssa form we insert moves on the incoming
10500          * edges to blocks containting phi functions.
10501          */
10502         struct triple *first;
10503         struct triple *phi;
10504
10505         /* Walk all of the operations to find the phi functions */
10506         first = RHS(state->main_function, 0);
10507         for(phi = first->next; phi != first ; phi = phi->next) {
10508                 struct block_set *set;
10509                 struct block *block;
10510                 struct triple **slot;
10511                 int edge;
10512                 if (phi->op != OP_PHI) {
10513                         continue;
10514                 }
10515                 phi->id |= TRIPLE_FLAG_POST_SPLIT;
10516                 block = phi->u.block;
10517                 slot  = &RHS(phi, 0);
10518                 /* Walk all of the incoming edges/blocks and insert moves.
10519                  */
10520                 for(edge = 0, set = block->use; set; set = set->next, edge++) {
10521                         struct block *eblock;
10522                         struct triple *move;
10523                         struct triple *val;
10524                         struct triple *ptr;
10525                         eblock = set->member;
10526                         val = slot[edge];
10527
10528                         if (val == phi) {
10529                                 continue;
10530                         }
10531
10532                         move = build_triple(state, OP_COPY, phi->type, val, 0,
10533                                 val->filename, val->line, val->col);
10534                         move->u.block = eblock;
10535                         move->id |= TRIPLE_FLAG_PRE_SPLIT;
10536                         use_triple(val, move);
10537                         
10538                         slot[edge] = move;
10539                         unuse_triple(val, phi);
10540                         use_triple(move, phi);
10541
10542                         /* Walk through the block backwards to find
10543                          * an appropriate location for the OP_COPY.
10544                          */
10545                         for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
10546                                 struct triple **expr;
10547                                 if ((ptr == phi) || (ptr == val)) {
10548                                         goto out;
10549                                 }
10550                                 expr = triple_rhs(state, ptr, 0);
10551                                 for(;expr; expr = triple_rhs(state, ptr, expr)) {
10552                                         if ((*expr) == phi) {
10553                                                 goto out;
10554                                         }
10555                                 }
10556                         }
10557                 out:
10558                         if (triple_is_branch(state, ptr)) {
10559                                 internal_error(state, ptr,
10560                                         "Could not insert write to phi");
10561                         }
10562                         insert_triple(state, ptr->next, move);
10563                         if (eblock->last == ptr) {
10564                                 eblock->last = move;
10565                         }
10566                         transform_to_arch_instruction(state, move);
10567                 }
10568         }
10569 }
10570
10571 struct triple_reg_set {
10572         struct triple_reg_set *next;
10573         struct triple *member;
10574         struct triple *new;
10575 };
10576
10577 struct reg_block {
10578         struct block *block;
10579         struct triple_reg_set *in;
10580         struct triple_reg_set *out;
10581         int vertex;
10582 };
10583
10584 static int do_triple_set(struct triple_reg_set **head, 
10585         struct triple *member, struct triple *new_member)
10586 {
10587         struct triple_reg_set **ptr, *new;
10588         if (!member)
10589                 return 0;
10590         ptr = head;
10591         while(*ptr) {
10592                 if ((*ptr)->member == member) {
10593                         return 0;
10594                 }
10595                 ptr = &(*ptr)->next;
10596         }
10597         new = xcmalloc(sizeof(*new), "triple_set");
10598         new->member = member;
10599         new->new    = new_member;
10600         new->next   = *head;
10601         *head       = new;
10602         return 1;
10603 }
10604
10605 static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
10606 {
10607         struct triple_reg_set *entry, **ptr;
10608         ptr = head;
10609         while(*ptr) {
10610                 entry = *ptr;
10611                 if (entry->member == member) {
10612                         *ptr = entry->next;
10613                         xfree(entry);
10614                         return;
10615                 }
10616                 else {
10617                         ptr = &entry->next;
10618                 }
10619         }
10620 }
10621
10622 static int in_triple(struct reg_block *rb, struct triple *in)
10623 {
10624         return do_triple_set(&rb->in, in, 0);
10625 }
10626 static void unin_triple(struct reg_block *rb, struct triple *unin)
10627 {
10628         do_triple_unset(&rb->in, unin);
10629 }
10630
10631 static int out_triple(struct reg_block *rb, struct triple *out)
10632 {
10633         return do_triple_set(&rb->out, out, 0);
10634 }
10635 static void unout_triple(struct reg_block *rb, struct triple *unout)
10636 {
10637         do_triple_unset(&rb->out, unout);
10638 }
10639
10640 static int initialize_regblock(struct reg_block *blocks,
10641         struct block *block, int vertex)
10642 {
10643         struct block_set *user;
10644         if (!block || (blocks[block->vertex].block == block)) {
10645                 return vertex;
10646         }
10647         vertex += 1;
10648         /* Renumber the blocks in a convinient fashion */
10649         block->vertex = vertex;
10650         blocks[vertex].block    = block;
10651         blocks[vertex].vertex   = vertex;
10652         for(user = block->use; user; user = user->next) {
10653                 vertex = initialize_regblock(blocks, user->member, vertex);
10654         }
10655         return vertex;
10656 }
10657
10658 static int phi_in(struct compile_state *state, struct reg_block *blocks,
10659         struct reg_block *rb, struct block *suc)
10660 {
10661         /* Read the conditional input set of a successor block
10662          * (i.e. the input to the phi nodes) and place it in the
10663          * current blocks output set.
10664          */
10665         struct block_set *set;
10666         struct triple *ptr;
10667         int edge;
10668         int done, change;
10669         change = 0;
10670         /* Find the edge I am coming in on */
10671         for(edge = 0, set = suc->use; set; set = set->next, edge++) {
10672                 if (set->member == rb->block) {
10673                         break;
10674                 }
10675         }
10676         if (!set) {
10677                 internal_error(state, 0, "Not coming on a control edge?");
10678         }
10679         for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
10680                 struct triple **slot, *expr, *ptr2;
10681                 int out_change, done2;
10682                 done = (ptr == suc->last);
10683                 if (ptr->op != OP_PHI) {
10684                         continue;
10685                 }
10686                 slot = &RHS(ptr, 0);
10687                 expr = slot[edge];
10688                 out_change = out_triple(rb, expr);
10689                 if (!out_change) {
10690                         continue;
10691                 }
10692                 /* If we don't define the variable also plast it
10693                  * in the current blocks input set.
10694                  */
10695                 ptr2 = rb->block->first;
10696                 for(done2 = 0; !done2; ptr2 = ptr2->next) {
10697                         if (ptr2 == expr) {
10698                                 break;
10699                         }
10700                         done2 = (ptr2 == rb->block->last);
10701                 }
10702                 if (!done2) {
10703                         continue;
10704                 }
10705                 change |= in_triple(rb, expr);
10706         }
10707         return change;
10708 }
10709
10710 static int reg_in(struct compile_state *state, struct reg_block *blocks,
10711         struct reg_block *rb, struct block *suc)
10712 {
10713         struct triple_reg_set *in_set;
10714         int change;
10715         change = 0;
10716         /* Read the input set of a successor block
10717          * and place it in the current blocks output set.
10718          */
10719         in_set = blocks[suc->vertex].in;
10720         for(; in_set; in_set = in_set->next) {
10721                 int out_change, done;
10722                 struct triple *first, *last, *ptr;
10723                 out_change = out_triple(rb, in_set->member);
10724                 if (!out_change) {
10725                         continue;
10726                 }
10727                 /* If we don't define the variable also place it
10728                  * in the current blocks input set.
10729                  */
10730                 first = rb->block->first;
10731                 last = rb->block->last;
10732                 done = 0;
10733                 for(ptr = first; !done; ptr = ptr->next) {
10734                         if (ptr == in_set->member) {
10735                                 break;
10736                         }
10737                         done = (ptr == last);
10738                 }
10739                 if (!done) {
10740                         continue;
10741                 }
10742                 change |= in_triple(rb, in_set->member);
10743         }
10744         change |= phi_in(state, blocks, rb, suc);
10745         return change;
10746 }
10747
10748
10749 static int use_in(struct compile_state *state, struct reg_block *rb)
10750 {
10751         /* Find the variables we use but don't define and add
10752          * it to the current blocks input set.
10753          */
10754 #warning "FIXME is this O(N^2) algorithm bad?"
10755         struct block *block;
10756         struct triple *ptr;
10757         int done;
10758         int change;
10759         block = rb->block;
10760         change = 0;
10761         for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
10762                 struct triple **expr;
10763                 done = (ptr == block->first);
10764                 /* The variable a phi function uses depends on the
10765                  * control flow, and is handled in phi_in, not
10766                  * here.
10767                  */
10768                 if (ptr->op == OP_PHI) {
10769                         continue;
10770                 }
10771                 expr = triple_rhs(state, ptr, 0);
10772                 for(;expr; expr = triple_rhs(state, ptr, expr)) {
10773                         struct triple *rhs, *test;
10774                         int tdone;
10775                         rhs = *expr;
10776                         if (!rhs) {
10777                                 continue;
10778                         }
10779                         /* See if rhs is defined in this block */
10780                         for(tdone = 0, test = ptr; !tdone; test = test->prev) {
10781                                 tdone = (test == block->first);
10782                                 if (test == rhs) {
10783                                         rhs = 0;
10784                                         break;
10785                                 }
10786                         }
10787                         /* If I still have a valid rhs add it to in */
10788                         change |= in_triple(rb, rhs);
10789                 }
10790         }
10791         return change;
10792 }
10793
10794 static struct reg_block *compute_variable_lifetimes(
10795         struct compile_state *state)
10796 {
10797         struct reg_block *blocks;
10798         int change;
10799         blocks = xcmalloc(
10800                 sizeof(*blocks)*(state->last_vertex + 1), "reg_block");
10801         initialize_regblock(blocks, state->last_block, 0);
10802         do {
10803                 int i;
10804                 change = 0;
10805                 for(i = 1; i <= state->last_vertex; i++) {
10806                         struct reg_block *rb;
10807                         rb = &blocks[i];
10808                         /* Add the left successor's input set to in */
10809                         if (rb->block->left) {
10810                                 change |= reg_in(state, blocks, rb, rb->block->left);
10811                         }
10812                         /* Add the right successor's input set to in */
10813                         if ((rb->block->right) && 
10814                                 (rb->block->right != rb->block->left)) {
10815                                 change |= reg_in(state, blocks, rb, rb->block->right);
10816                         }
10817                         /* Add use to in... */
10818                         change |= use_in(state, rb);
10819                 }
10820         } while(change);
10821         return blocks;
10822 }
10823
10824 static void free_variable_lifetimes(
10825         struct compile_state *state, struct reg_block *blocks)
10826 {
10827         int i;
10828         /* free in_set && out_set on each block */
10829         for(i = 1; i <= state->last_vertex; i++) {
10830                 struct triple_reg_set *entry, *next;
10831                 struct reg_block *rb;
10832                 rb = &blocks[i];
10833                 for(entry = rb->in; entry ; entry = next) {
10834                         next = entry->next;
10835                         do_triple_unset(&rb->in, entry->member);
10836                 }
10837                 for(entry = rb->out; entry; entry = next) {
10838                         next = entry->next;
10839                         do_triple_unset(&rb->out, entry->member);
10840                 }
10841         }
10842         xfree(blocks);
10843
10844 }
10845
10846 typedef struct triple *(*wvl_cb_t)(
10847         struct compile_state *state, 
10848         struct reg_block *blocks, struct triple_reg_set *live, 
10849         struct reg_block *rb, struct triple *ins, void *arg);
10850
10851 static void walk_variable_lifetimes(struct compile_state *state,
10852         struct reg_block *blocks, wvl_cb_t cb, void *arg)
10853 {
10854         int i;
10855         
10856         for(i = 1; i <= state->last_vertex; i++) {
10857                 struct triple_reg_set *live;
10858                 struct triple_reg_set *entry, *next;
10859                 struct triple *ptr, *prev;
10860                 struct reg_block *rb;
10861                 struct block *block;
10862                 int done;
10863
10864                 /* Get the blocks */
10865                 rb = &blocks[i];
10866                 block = rb->block;
10867
10868                 /* Copy out into live */
10869                 live = 0;
10870                 for(entry = rb->out; entry; entry = next) {
10871                         next = entry->next;
10872                         do_triple_set(&live, entry->member, entry->new);
10873                 }
10874                 /* Walk through the basic block calculating live */
10875                 for(done = 0, ptr = block->last; !done; ptr = prev) {
10876                         struct triple **expr, *result;
10877
10878                         prev = ptr->prev;
10879                         done = (ptr == block->first);
10880
10881                         /* Ensure the current definition is in live */
10882                         if (triple_is_def(state, ptr)) {
10883                                 do_triple_set(&live, ptr, 0);
10884                         }
10885
10886                         /* Inform the callback function of what is
10887                          * going on.
10888                          */
10889                         result = cb(state, blocks, live, rb, ptr, arg);
10890                         
10891                         /* Remove the current definition from live */
10892                         do_triple_unset(&live, ptr);
10893
10894                         /* If the current instruction was deleted continue */
10895                         if (!result) {
10896                                 if (block->last == ptr) {
10897                                         block->last = prev;
10898                                 }
10899                                 continue;
10900                         }
10901                         
10902
10903                         /* Add the current uses to live.
10904                          *
10905                          * It is safe to skip phi functions because they do
10906                          * not have any block local uses, and the block
10907                          * output sets already properly account for what
10908                          * control flow depedent uses phi functions do have.
10909                          */
10910                         if (ptr->op == OP_PHI) {
10911                                 continue;
10912                         }
10913                         expr = triple_rhs(state, ptr, 0);
10914                         for(;expr; expr = triple_rhs(state, ptr, expr)) {
10915                                 /* If the triple is not a definition skip it. */
10916                                 if (!*expr || !triple_is_def(state, *expr)) {
10917                                         continue;
10918                                 }
10919                                 do_triple_set(&live, *expr, 0);
10920                         }
10921                 }
10922                 /* Free live */
10923                 for(entry = live; entry; entry = next) {
10924                         next = entry->next;
10925                         do_triple_unset(&live, entry->member);
10926                 }
10927         }
10928 }
10929
10930 static int count_triples(struct compile_state *state)
10931 {
10932         struct triple *first, *ins;
10933         int triples = 0;
10934         first = RHS(state->main_function, 0);
10935         ins = first;
10936         do {
10937                 triples++;
10938                 ins = ins->next;
10939         } while (ins != first);
10940         return triples;
10941 }
10942 struct dead_triple {
10943         struct triple *triple;
10944         struct dead_triple *work_next;
10945         struct block *block;
10946         int color;
10947         int flags;
10948 #define TRIPLE_FLAG_ALIVE 1
10949 };
10950
10951
10952 static void awaken(
10953         struct compile_state *state,
10954         struct dead_triple *dtriple, struct triple **expr,
10955         struct dead_triple ***work_list_tail)
10956 {
10957         struct triple *triple;
10958         struct dead_triple *dt;
10959         if (!expr) {
10960                 return;
10961         }
10962         triple = *expr;
10963         if (!triple) {
10964                 return;
10965         }
10966         if (triple->id <= 0)  {
10967                 internal_error(state, triple, "bad triple id: %d",
10968                         triple->id);
10969         }
10970         if (triple->op == OP_NOOP) {
10971                 internal_warning(state, triple, "awakening noop?");
10972                 return;
10973         }
10974         dt = &dtriple[triple->id];
10975         if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
10976                 dt->flags |= TRIPLE_FLAG_ALIVE;
10977                 if (!dt->work_next) {
10978                         **work_list_tail = dt;
10979                         *work_list_tail = &dt->work_next;
10980                 }
10981         }
10982 }
10983
10984 static void eliminate_inefectual_code(struct compile_state *state)
10985 {
10986         struct block *block;
10987         struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
10988         int triples, i;
10989         struct triple *first, *ins;
10990
10991         /* Setup the work list */
10992         work_list = 0;
10993         work_list_tail = &work_list;
10994
10995         first = RHS(state->main_function, 0);
10996
10997         /* Count how many triples I have */
10998         triples = count_triples(state);
10999
11000         /* Now put then in an array and mark all of the triples dead */
11001         dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
11002         
11003         ins = first;
11004         i = 1;
11005         block = 0;
11006         do {
11007                 if (ins->op == OP_LABEL) {
11008                         block = ins->u.block;
11009                 }
11010                 dtriple[i].triple = ins;
11011                 dtriple[i].block  = block;
11012                 dtriple[i].flags  = 0;
11013                 dtriple[i].color  = ins->id;
11014                 ins->id = i;
11015                 /* See if it is an operation we always keep */
11016 #warning "FIXME handle the case of killing a branch instruction"
11017                 if (!triple_is_pure(state, ins) || triple_is_branch(state, ins)) {
11018                         awaken(state, dtriple, &ins, &work_list_tail);
11019                 }
11020                 i++;
11021                 ins = ins->next;
11022         } while(ins != first);
11023         while(work_list) {
11024                 struct dead_triple *dt;
11025                 struct block_set *user;
11026                 struct triple **expr;
11027                 dt = work_list;
11028                 work_list = dt->work_next;
11029                 if (!work_list) {
11030                         work_list_tail = &work_list;
11031                 }
11032                 /* Wake up the data depencencies of this triple */
11033                 expr = 0;
11034                 do {
11035                         expr = triple_rhs(state, dt->triple, expr);
11036                         awaken(state, dtriple, expr, &work_list_tail);
11037                 } while(expr);
11038                 do {
11039                         expr = triple_lhs(state, dt->triple, expr);
11040                         awaken(state, dtriple, expr, &work_list_tail);
11041                 } while(expr);
11042                 do {
11043                         expr = triple_misc(state, dt->triple, expr);
11044                         awaken(state, dtriple, expr, &work_list_tail);
11045                 } while(expr);
11046                 /* Wake up the forward control dependencies */
11047                 do {
11048                         expr = triple_targ(state, dt->triple, expr);
11049                         awaken(state, dtriple, expr, &work_list_tail);
11050                 } while(expr);
11051                 /* Wake up the reverse control dependencies of this triple */
11052                 for(user = dt->block->ipdomfrontier; user; user = user->next) {
11053                         awaken(state, dtriple, &user->member->last, &work_list_tail);
11054                 }
11055         }
11056         for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
11057                 if ((dt->triple->op == OP_NOOP) && 
11058                         (dt->flags & TRIPLE_FLAG_ALIVE)) {
11059                         internal_error(state, dt->triple, "noop effective?");
11060                 }
11061                 dt->triple->id = dt->color;     /* Restore the color */
11062                 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11063 #warning "FIXME handle the case of killing a basic block"
11064                         if (dt->block->first == dt->triple) {
11065                                 continue;
11066                         }
11067                         if (dt->block->last == dt->triple) {
11068                                 dt->block->last = dt->triple->prev;
11069                         }
11070                         release_triple(state, dt->triple);
11071                 }
11072         }
11073         xfree(dtriple);
11074 }
11075
11076
11077 static void insert_mandatory_copies(struct compile_state *state)
11078 {
11079         struct triple *ins, *first;
11080
11081         /* The object is with a minimum of inserted copies,
11082          * to resolve in fundamental register conflicts between
11083          * register value producers and consumers.
11084          * Theoretically we may be greater than minimal when we
11085          * are inserting copies before instructions but that
11086          * case should be rare.
11087          */
11088         first = RHS(state->main_function, 0);
11089         ins = first;
11090         do {
11091                 struct triple_set *entry, *next;
11092                 struct triple *tmp;
11093                 struct reg_info info;
11094                 unsigned reg, regcm;
11095                 int do_post_copy, do_pre_copy;
11096                 tmp = 0;
11097                 if (!triple_is_def(state, ins)) {
11098                         goto next;
11099                 }
11100                 /* Find the architecture specific color information */
11101                 info = arch_reg_lhs(state, ins, 0);
11102                 if (info.reg >= MAX_REGISTERS) {
11103                         info.reg = REG_UNSET;
11104                 }
11105                 
11106                 reg = REG_UNSET;
11107                 regcm = arch_type_to_regcm(state, ins->type);
11108                 do_post_copy = do_pre_copy = 0;
11109
11110                 /* Walk through the uses of ins and check for conflicts */
11111                 for(entry = ins->use; entry; entry = next) {
11112                         struct reg_info rinfo;
11113                         int i;
11114                         next = entry->next;
11115                         i = find_rhs_use(state, entry->member, ins);
11116                         if (i < 0) {
11117                                 continue;
11118                         }
11119                         
11120                         /* Find the users color requirements */
11121                         rinfo = arch_reg_rhs(state, entry->member, i);
11122                         if (rinfo.reg >= MAX_REGISTERS) {
11123                                 rinfo.reg = REG_UNSET;
11124                         }
11125                         
11126                         /* See if I need a pre_copy */
11127                         if (rinfo.reg != REG_UNSET) {
11128                                 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
11129                                         do_pre_copy = 1;
11130                                 }
11131                                 reg = rinfo.reg;
11132                         }
11133                         regcm &= rinfo.regcm;
11134                         regcm = arch_regcm_normalize(state, regcm);
11135                         if (regcm == 0) {
11136                                 do_pre_copy = 1;
11137                         }
11138                 }
11139                 do_post_copy =
11140                         !do_pre_copy &&
11141                         (((info.reg != REG_UNSET) && 
11142                                 (reg != REG_UNSET) &&
11143                                 (info.reg != reg)) ||
11144                         ((info.regcm & regcm) == 0));
11145
11146                 reg = info.reg;
11147                 regcm = info.regcm;
11148                 /* Walk through the uses of insert and do a pre_copy or see if a post_copy is warranted */
11149                 for(entry = ins->use; entry; entry = next) {
11150                         struct reg_info rinfo;
11151                         int i;
11152                         next = entry->next;
11153                         i = find_rhs_use(state, entry->member, ins);
11154                         if (i < 0) {
11155                                 continue;
11156                         }
11157                         
11158                         /* Find the users color requirements */
11159                         rinfo = arch_reg_rhs(state, entry->member, i);
11160                         if (rinfo.reg >= MAX_REGISTERS) {
11161                                 rinfo.reg = REG_UNSET;
11162                         }
11163
11164                         /* Now see if it is time to do the pre_copy */
11165                         if (rinfo.reg != REG_UNSET) {
11166                                 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
11167                                         ((regcm & rinfo.regcm) == 0) ||
11168                                         /* Don't let a mandatory coalesce sneak
11169                                          * into a operation that is marked to prevent
11170                                          * coalescing.
11171                                          */
11172                                         ((reg != REG_UNNEEDED) &&
11173                                         ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
11174                                         (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
11175                                         ) {
11176                                         if (do_pre_copy) {
11177                                                 struct triple *user;
11178                                                 user = entry->member;
11179                                                 if (RHS(user, i) != ins) {
11180                                                         internal_error(state, user, "bad rhs");
11181                                                 }
11182                                                 tmp = pre_copy(state, user, i);
11183                                                 continue;
11184                                         } else {
11185                                                 do_post_copy = 1;
11186                                         }
11187                                 }
11188                                 reg = rinfo.reg;
11189                         }
11190                         if ((regcm & rinfo.regcm) == 0) {
11191                                 if (do_pre_copy) {
11192                                         struct triple *user;
11193                                         user = entry->member;
11194                                         if (RHS(user, i) != ins) {
11195                                                 internal_error(state, user, "bad rhs");
11196                                         }
11197                                         tmp = pre_copy(state, user, i);
11198                                         continue;
11199                                 } else {
11200                                         do_post_copy = 1;
11201                                 }
11202                         }
11203                         regcm &= rinfo.regcm;
11204                         
11205                 }
11206                 if (do_post_copy) {
11207                         struct reg_info pre, post;
11208                         tmp = post_copy(state, ins);
11209                         pre = arch_reg_lhs(state, ins, 0);
11210                         post = arch_reg_lhs(state, tmp, 0);
11211                         if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
11212                                 internal_error(state, tmp, "useless copy");
11213                         }
11214                 }
11215         next:
11216                 ins = ins->next;
11217         } while(ins != first);
11218 }
11219
11220
11221 struct live_range_edge;
11222 struct live_range_def;
11223 struct live_range {
11224         struct live_range_edge *edges;
11225         struct live_range_def *defs;
11226 /* Note. The list pointed to by defs is kept in order.
11227  * That is baring splits in the flow control
11228  * defs dominates defs->next wich dominates defs->next->next
11229  * etc.
11230  */
11231         unsigned color;
11232         unsigned classes;
11233         unsigned degree;
11234         unsigned length;
11235         struct live_range *group_next, **group_prev;
11236 };
11237
11238 struct live_range_edge {
11239         struct live_range_edge *next;
11240         struct live_range *node;
11241 };
11242
11243 struct live_range_def {
11244         struct live_range_def *next;
11245         struct live_range_def *prev;
11246         struct live_range *lr;
11247         struct triple *def;
11248         unsigned orig_id;
11249 };
11250
11251 #define LRE_HASH_SIZE 2048
11252 struct lre_hash {
11253         struct lre_hash *next;
11254         struct live_range *left;
11255         struct live_range *right;
11256 };
11257
11258
11259 struct reg_state {
11260         struct lre_hash *hash[LRE_HASH_SIZE];
11261         struct reg_block *blocks;
11262         struct live_range_def *lrd;
11263         struct live_range *lr;
11264         struct live_range *low, **low_tail;
11265         struct live_range *high, **high_tail;
11266         unsigned defs;
11267         unsigned ranges;
11268         int passes, max_passes;
11269 #define MAX_ALLOCATION_PASSES 100
11270 };
11271
11272
11273 static unsigned regc_max_size(struct compile_state *state, int classes)
11274 {
11275         unsigned max_size;
11276         int i;
11277         max_size = 0;
11278         for(i = 0; i < MAX_REGC; i++) {
11279                 if (classes & (1 << i)) {
11280                         unsigned size;
11281                         size = arch_regc_size(state, i);
11282                         if (size > max_size) {
11283                                 max_size = size;
11284                         }
11285                 }
11286         }
11287         return max_size;
11288 }
11289
11290 static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
11291 {
11292         unsigned equivs[MAX_REG_EQUIVS];
11293         int i;
11294         if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
11295                 internal_error(state, 0, "invalid register");
11296         }
11297         if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
11298                 internal_error(state, 0, "invalid register");
11299         }
11300         arch_reg_equivs(state, equivs, reg1);
11301         for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11302                 if (equivs[i] == reg2) {
11303                         return 1;
11304                 }
11305         }
11306         return 0;
11307 }
11308
11309 static void reg_fill_used(struct compile_state *state, char *used, int reg)
11310 {
11311         unsigned equivs[MAX_REG_EQUIVS];
11312         int i;
11313         if (reg == REG_UNNEEDED) {
11314                 return;
11315         }
11316         arch_reg_equivs(state, equivs, reg);
11317         for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11318                 used[equivs[i]] = 1;
11319         }
11320         return;
11321 }
11322
11323 static void reg_inc_used(struct compile_state *state, char *used, int reg)
11324 {
11325         unsigned equivs[MAX_REG_EQUIVS];
11326         int i;
11327         if (reg == REG_UNNEEDED) {
11328                 return;
11329         }
11330         arch_reg_equivs(state, equivs, reg);
11331         for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11332                 used[equivs[i]] += 1;
11333         }
11334         return;
11335 }
11336
11337 static unsigned int hash_live_edge(
11338         struct live_range *left, struct live_range *right)
11339 {
11340         unsigned int hash, val;
11341         unsigned long lval, rval;
11342         lval = ((unsigned long)left)/sizeof(struct live_range);
11343         rval = ((unsigned long)right)/sizeof(struct live_range);
11344         hash = 0;
11345         while(lval) {
11346                 val = lval & 0xff;
11347                 lval >>= 8;
11348                 hash = (hash *263) + val;
11349         }
11350         while(rval) {
11351                 val = rval & 0xff;
11352                 rval >>= 8;
11353                 hash = (hash *263) + val;
11354         }
11355         hash = hash & (LRE_HASH_SIZE - 1);
11356         return hash;
11357 }
11358
11359 static struct lre_hash **lre_probe(struct reg_state *rstate,
11360         struct live_range *left, struct live_range *right)
11361 {
11362         struct lre_hash **ptr;
11363         unsigned int index;
11364         /* Ensure left <= right */
11365         if (left > right) {
11366                 struct live_range *tmp;
11367                 tmp = left;
11368                 left = right;
11369                 right = tmp;
11370         }
11371         index = hash_live_edge(left, right);
11372         
11373         ptr = &rstate->hash[index];
11374         while((*ptr) && ((*ptr)->left != left) && ((*ptr)->right != right)) {
11375                 ptr = &(*ptr)->next;
11376         }
11377         return ptr;
11378 }
11379
11380 static int interfere(struct reg_state *rstate,
11381         struct live_range *left, struct live_range *right)
11382 {
11383         struct lre_hash **ptr;
11384         ptr = lre_probe(rstate, left, right);
11385         return ptr && *ptr;
11386 }
11387
11388 static void add_live_edge(struct reg_state *rstate, 
11389         struct live_range *left, struct live_range *right)
11390 {
11391         /* FIXME the memory allocation overhead is noticeable here... */
11392         struct lre_hash **ptr, *new_hash;
11393         struct live_range_edge *edge;
11394
11395         if (left == right) {
11396                 return;
11397         }
11398         if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
11399                 return;
11400         }
11401         /* Ensure left <= right */
11402         if (left > right) {
11403                 struct live_range *tmp;
11404                 tmp = left;
11405                 left = right;
11406                 right = tmp;
11407         }
11408         ptr = lre_probe(rstate, left, right);
11409         if (*ptr) {
11410                 return;
11411         }
11412         new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
11413         new_hash->next  = *ptr;
11414         new_hash->left  = left;
11415         new_hash->right = right;
11416         *ptr = new_hash;
11417
11418         edge = xmalloc(sizeof(*edge), "live_range_edge");
11419         edge->next   = left->edges;
11420         edge->node   = right;
11421         left->edges  = edge;
11422         left->degree += 1;
11423         
11424         edge = xmalloc(sizeof(*edge), "live_range_edge");
11425         edge->next    = right->edges;
11426         edge->node    = left;
11427         right->edges  = edge;
11428         right->degree += 1;
11429 }
11430
11431 static void remove_live_edge(struct reg_state *rstate,
11432         struct live_range *left, struct live_range *right)
11433 {
11434         struct live_range_edge *edge, **ptr;
11435         struct lre_hash **hptr, *entry;
11436         hptr = lre_probe(rstate, left, right);
11437         if (!hptr || !*hptr) {
11438                 return;
11439         }
11440         entry = *hptr;
11441         *hptr = entry->next;
11442         xfree(entry);
11443
11444         for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
11445                 edge = *ptr;
11446                 if (edge->node == right) {
11447                         *ptr = edge->next;
11448                         memset(edge, 0, sizeof(*edge));
11449                         xfree(edge);
11450                         break;
11451                 }
11452         }
11453         for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
11454                 edge = *ptr;
11455                 if (edge->node == left) {
11456                         *ptr = edge->next;
11457                         memset(edge, 0, sizeof(*edge));
11458                         xfree(edge);
11459                         break;
11460                 }
11461         }
11462 }
11463
11464 static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
11465 {
11466         struct live_range_edge *edge, *next;
11467         for(edge = range->edges; edge; edge = next) {
11468                 next = edge->next;
11469                 remove_live_edge(rstate, range, edge->node);
11470         }
11471 }
11472
11473
11474 /* Interference graph...
11475  * 
11476  * new(n) --- Return a graph with n nodes but no edges.
11477  * add(g,x,y) --- Return a graph including g with an between x and y
11478  * interfere(g, x, y) --- Return true if there exists an edge between the nodes
11479  *                x and y in the graph g
11480  * degree(g, x) --- Return the degree of the node x in the graph g
11481  * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
11482  *
11483  * Implement with a hash table && a set of adjcency vectors.
11484  * The hash table supports constant time implementations of add and interfere.
11485  * The adjacency vectors support an efficient implementation of neighbors.
11486  */
11487
11488 /* 
11489  *     +---------------------------------------------------+
11490  *     |         +--------------+                          |
11491  *     v         v              |                          |
11492  * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select 
11493  *
11494  * -- In simplify implment optimistic coloring... (No backtracking)
11495  * -- Implement Rematerialization it is the only form of spilling we can perform
11496  *    Essentially this means dropping a constant from a register because
11497  *    we can regenerate it later.
11498  *
11499  * --- Very conservative colalescing (don't colalesce just mark the opportunities)
11500  *     coalesce at phi points...
11501  * --- Bias coloring if at all possible do the coalesing a compile time.
11502  *
11503  *
11504  */
11505
11506 static void different_colored(
11507         struct compile_state *state, struct reg_state *rstate, 
11508         struct triple *parent, struct triple *ins)
11509 {
11510         struct live_range *lr;
11511         struct triple **expr;
11512         lr = rstate->lrd[ins->id].lr;
11513         expr = triple_rhs(state, ins, 0);
11514         for(;expr; expr = triple_rhs(state, ins, expr)) {
11515                 struct live_range *lr2;
11516                 if (!*expr || (*expr == parent) || (*expr == ins)) {
11517                         continue;
11518                 }
11519                 lr2 = rstate->lrd[(*expr)->id].lr;
11520                 if (lr->color == lr2->color) {
11521                         internal_error(state, ins, "live range too big");
11522                 }
11523         }
11524 }
11525
11526
11527 static struct live_range *coalesce_ranges(
11528         struct compile_state *state, struct reg_state *rstate,
11529         struct live_range *lr1, struct live_range *lr2)
11530 {
11531         struct live_range_def *head, *mid1, *mid2, *end, *lrd;
11532         unsigned color;
11533         unsigned classes;
11534         if (lr1 == lr2) {
11535                 return lr1;
11536         }
11537         if (!lr1->defs || !lr2->defs) {
11538                 internal_error(state, 0,
11539                         "cannot coalese dead live ranges");
11540         }
11541         if ((lr1->color == REG_UNNEEDED) ||
11542                 (lr2->color == REG_UNNEEDED)) {
11543                 internal_error(state, 0, 
11544                         "cannot coalesce live ranges without a possible color");
11545         }
11546         if ((lr1->color != lr2->color) &&
11547                 (lr1->color != REG_UNSET) &&
11548                 (lr2->color != REG_UNSET)) {
11549                 internal_error(state, lr1->defs->def, 
11550                         "cannot coalesce live ranges of different colors");
11551         }
11552         color = lr1->color;
11553         if (color == REG_UNSET) {
11554                 color = lr2->color;
11555         }
11556         classes = lr1->classes & lr2->classes;
11557         if (!classes) {
11558                 internal_error(state, lr1->defs->def,
11559                         "cannot coalesce live ranges with dissimilar register classes");
11560         }
11561         /* If there is a clear dominate live range put it in lr1,
11562          * For purposes of this test phi functions are
11563          * considered dominated by the definitions that feed into
11564          * them. 
11565          */
11566         if ((lr1->defs->prev->def->op == OP_PHI) ||
11567                 ((lr2->defs->prev->def->op != OP_PHI) &&
11568                 tdominates(state, lr2->defs->def, lr1->defs->def))) {
11569                 struct live_range *tmp;
11570                 tmp = lr1;
11571                 lr1 = lr2;
11572                 lr2 = tmp;
11573         }
11574 #if 0
11575         if (lr1->defs->orig_id  & TRIPLE_FLAG_POST_SPLIT) {
11576                 fprintf(stderr, "lr1 post\n");
11577         }
11578         if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
11579                 fprintf(stderr, "lr1 pre\n");
11580         }
11581         if (lr2->defs->orig_id  & TRIPLE_FLAG_POST_SPLIT) {
11582                 fprintf(stderr, "lr2 post\n");
11583         }
11584         if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
11585                 fprintf(stderr, "lr2 pre\n");
11586         }
11587 #endif
11588 #if 0
11589         fprintf(stderr, "coalesce color1(%p): %3d color2(%p) %3d\n",
11590                 lr1->defs->def,
11591                 lr1->color,
11592                 lr2->defs->def,
11593                 lr2->color);
11594 #endif
11595         
11596         lr1->classes = classes;
11597         /* Append lr2 onto lr1 */
11598 #warning "FIXME should this be a merge instead of a splice?"
11599         head = lr1->defs;
11600         mid1 = lr1->defs->prev;
11601         mid2 = lr2->defs;
11602         end  = lr2->defs->prev;
11603         
11604         head->prev = end;
11605         end->next  = head;
11606
11607         mid1->next = mid2;
11608         mid2->prev = mid1;
11609
11610         /* Fixup the live range in the added live range defs */
11611         lrd = head;
11612         do {
11613                 lrd->lr = lr1;
11614                 lrd = lrd->next;
11615         } while(lrd != head);
11616
11617         /* Mark lr2 as free. */
11618         lr2->defs = 0;
11619         lr2->color = REG_UNNEEDED;
11620         lr2->classes = 0;
11621
11622         if (!lr1->defs) {
11623                 internal_error(state, 0, "lr1->defs == 0 ?");
11624         }
11625
11626         lr1->color   = color;
11627         lr1->classes = classes;
11628
11629         return lr1;
11630 }
11631
11632 static struct live_range_def *live_range_head(
11633         struct compile_state *state, struct live_range *lr,
11634         struct live_range_def *last)
11635 {
11636         struct live_range_def *result;
11637         result = 0;
11638         if (last == 0) {
11639                 result = lr->defs;
11640         }
11641         else if (!tdominates(state, lr->defs->def, last->next->def)) {
11642                 result = last->next;
11643         }
11644         return result;
11645 }
11646
11647 static struct live_range_def *live_range_end(
11648         struct compile_state *state, struct live_range *lr,
11649         struct live_range_def *last)
11650 {
11651         struct live_range_def *result;
11652         result = 0;
11653         if (last == 0) {
11654                 result = lr->defs->prev;
11655         }
11656         else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
11657                 result = last->prev;
11658         }
11659         return result;
11660 }
11661
11662
11663 static void initialize_live_ranges(
11664         struct compile_state *state, struct reg_state *rstate)
11665 {
11666         struct triple *ins, *first;
11667         size_t count, size;
11668         int i, j;
11669
11670         first = RHS(state->main_function, 0);
11671         /* First count how many instructions I have.
11672          */
11673         count = count_triples(state);
11674         /* Potentially I need one live range definitions for each
11675          * instruction, plus an extra for the split routines.
11676          */
11677         rstate->defs = count + 1;
11678         /* Potentially I need one live range for each instruction
11679          * plus an extra for the dummy live range.
11680          */
11681         rstate->ranges = count + 1;
11682         size = sizeof(rstate->lrd[0]) * rstate->defs;
11683         rstate->lrd = xcmalloc(size, "live_range_def");
11684         size = sizeof(rstate->lr[0]) * rstate->ranges;
11685         rstate->lr  = xcmalloc(size, "live_range");
11686
11687         /* Setup the dummy live range */
11688         rstate->lr[0].classes = 0;
11689         rstate->lr[0].color = REG_UNSET;
11690         rstate->lr[0].defs = 0;
11691         i = j = 0;
11692         ins = first;
11693         do {
11694                 /* If the triple is a variable give it a live range */
11695                 if (triple_is_def(state, ins)) {
11696                         struct reg_info info;
11697                         /* Find the architecture specific color information */
11698                         info = find_def_color(state, ins);
11699
11700                         i++;
11701                         rstate->lr[i].defs    = &rstate->lrd[j];
11702                         rstate->lr[i].color   = info.reg;
11703                         rstate->lr[i].classes = info.regcm;
11704                         rstate->lr[i].degree  = 0;
11705                         rstate->lrd[j].lr = &rstate->lr[i];
11706                 } 
11707                 /* Otherwise give the triple the dummy live range. */
11708                 else {
11709                         rstate->lrd[j].lr = &rstate->lr[0];
11710                 }
11711
11712                 /* Initalize the live_range_def */
11713                 rstate->lrd[j].next    = &rstate->lrd[j];
11714                 rstate->lrd[j].prev    = &rstate->lrd[j];
11715                 rstate->lrd[j].def     = ins;
11716                 rstate->lrd[j].orig_id = ins->id;
11717                 ins->id = j;
11718
11719                 j++;
11720                 ins = ins->next;
11721         } while(ins != first);
11722         rstate->ranges = i;
11723         rstate->defs -= 1;
11724
11725         /* Make a second pass to handle achitecture specific register
11726          * constraints.
11727          */
11728         ins = first;
11729         do {
11730                 int zlhs, zrhs, i, j;
11731                 if (ins->id > rstate->defs) {
11732                         internal_error(state, ins, "bad id");
11733                 }
11734                 
11735                 /* Walk through the template of ins and coalesce live ranges */
11736                 zlhs = TRIPLE_LHS(ins->sizes);
11737                 if ((zlhs == 0) && triple_is_def(state, ins)) {
11738                         zlhs = 1;
11739                 }
11740                 zrhs = TRIPLE_RHS(ins->sizes);
11741                 
11742                 for(i = 0; i < zlhs; i++) {
11743                         struct reg_info linfo;
11744                         struct live_range_def *lhs;
11745                         linfo = arch_reg_lhs(state, ins, i);
11746                         if (linfo.reg < MAX_REGISTERS) {
11747                                 continue;
11748                         }
11749                         if (triple_is_def(state, ins)) {
11750                                 lhs = &rstate->lrd[ins->id];
11751                         } else {
11752                                 lhs = &rstate->lrd[LHS(ins, i)->id];
11753                         }
11754                         for(j = 0; j < zrhs; j++) {
11755                                 struct reg_info rinfo;
11756                                 struct live_range_def *rhs;
11757                                 rinfo = arch_reg_rhs(state, ins, j);
11758                                 if (rinfo.reg < MAX_REGISTERS) {
11759                                         continue;
11760                                 }
11761                                 rhs = &rstate->lrd[RHS(ins, i)->id];
11762                                 if (rinfo.reg == linfo.reg) {
11763                                         coalesce_ranges(state, rstate, 
11764                                                 lhs->lr, rhs->lr);
11765                                 }
11766                         }
11767                 }
11768                 ins = ins->next;
11769         } while(ins != first);
11770 }
11771
11772 static struct triple *graph_ins(
11773         struct compile_state *state, 
11774         struct reg_block *blocks, struct triple_reg_set *live, 
11775         struct reg_block *rb, struct triple *ins, void *arg)
11776 {
11777         struct reg_state *rstate = arg;
11778         struct live_range *def;
11779         struct triple_reg_set *entry;
11780
11781         /* If the triple is not a definition
11782          * we do not have a definition to add to
11783          * the interference graph.
11784          */
11785         if (!triple_is_def(state, ins)) {
11786                 return ins;
11787         }
11788         def = rstate->lrd[ins->id].lr;
11789         
11790         /* Create an edge between ins and everything that is
11791          * alive, unless the live_range cannot share
11792          * a physical register with ins.
11793          */
11794         for(entry = live; entry; entry = entry->next) {
11795                 struct live_range *lr;
11796                 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
11797                         internal_error(state, 0, "bad entry?");
11798                 }
11799                 lr = rstate->lrd[entry->member->id].lr;
11800                 if (def == lr) {
11801                         continue;
11802                 }
11803                 if (!arch_regcm_intersect(def->classes, lr->classes)) {
11804                         continue;
11805                 }
11806                 add_live_edge(rstate, def, lr);
11807         }
11808         return ins;
11809 }
11810
11811
11812 static struct triple *print_interference_ins(
11813         struct compile_state *state, 
11814         struct reg_block *blocks, struct triple_reg_set *live, 
11815         struct reg_block *rb, struct triple *ins, void *arg)
11816 {
11817         struct reg_state *rstate = arg;
11818         struct live_range *lr;
11819
11820         lr = rstate->lrd[ins->id].lr;
11821         display_triple(stdout, ins);
11822
11823         if (lr->defs) {
11824                 struct live_range_def *lrd;
11825                 printf("       range:");
11826                 lrd = lr->defs;
11827                 do {
11828                         printf(" %-10p", lrd->def);
11829                         lrd = lrd->next;
11830                 } while(lrd != lr->defs);
11831                 printf("\n");
11832         }
11833         if (live) {
11834                 struct triple_reg_set *entry;
11835                 printf("        live:");
11836                 for(entry = live; entry; entry = entry->next) {
11837                         printf(" %-10p", entry->member);
11838                 }
11839                 printf("\n");
11840         }
11841         if (lr->edges) {
11842                 struct live_range_edge *entry;
11843                 printf("       edges:");
11844                 for(entry = lr->edges; entry; entry = entry->next) {
11845                         struct live_range_def *lrd;
11846                         lrd = entry->node->defs;
11847                         do {
11848                                 printf(" %-10p", lrd->def);
11849                                 lrd = lrd->next;
11850                         } while(lrd != entry->node->defs);
11851                         printf("|");
11852                 }
11853                 printf("\n");
11854         }
11855         if (triple_is_branch(state, ins)) {
11856                 printf("\n");
11857         }
11858         return ins;
11859 }
11860
11861 static int coalesce_live_ranges(
11862         struct compile_state *state, struct reg_state *rstate)
11863 {
11864         /* At the point where a value is moved from one
11865          * register to another that value requires two
11866          * registers, thus increasing register pressure.
11867          * Live range coaleescing reduces the register
11868          * pressure by keeping a value in one register
11869          * longer.
11870          *
11871          * In the case of a phi function all paths leading
11872          * into it must be allocated to the same register
11873          * otherwise the phi function may not be removed.
11874          *
11875          * Forcing a value to stay in a single register
11876          * for an extended period of time does have
11877          * limitations when applied to non homogenous
11878          * register pool.  
11879          *
11880          * The two cases I have identified are:
11881          * 1) Two forced register assignments may
11882          *    collide.
11883          * 2) Registers may go unused because they
11884          *    are only good for storing the value
11885          *    and not manipulating it.
11886          *
11887          * Because of this I need to split live ranges,
11888          * even outside of the context of coalesced live
11889          * ranges.  The need to split live ranges does
11890          * impose some constraints on live range coalescing.
11891          *
11892          * - Live ranges may not be coalesced across phi
11893          *   functions.  This creates a 2 headed live
11894          *   range that cannot be sanely split.
11895          *
11896          * - phi functions (coalesced in initialize_live_ranges) 
11897          *   are handled as pre split live ranges so we will
11898          *   never attempt to split them.
11899          */
11900         int coalesced;
11901         int i;
11902
11903         coalesced = 0;
11904         for(i = 0; i <= rstate->ranges; i++) {
11905                 struct live_range *lr1;
11906                 struct live_range_def *lrd1;
11907                 lr1 = &rstate->lr[i];
11908                 if (!lr1->defs) {
11909                         continue;
11910                 }
11911                 lrd1 = live_range_end(state, lr1, 0);
11912                 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
11913                         struct triple_set *set;
11914                         if (lrd1->def->op != OP_COPY) {
11915                                 continue;
11916                         }
11917                         /* Skip copies that are the result of a live range split. */
11918                         if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
11919                                 continue;
11920                         }
11921                         for(set = lrd1->def->use; set; set = set->next) {
11922                                 struct live_range_def *lrd2;
11923                                 struct live_range *lr2, *res;
11924
11925                                 lrd2 = &rstate->lrd[set->member->id];
11926
11927                                 /* Don't coalesce with instructions
11928                                  * that are the result of a live range
11929                                  * split.
11930                                  */
11931                                 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
11932                                         continue;
11933                                 }
11934                                 lr2 = rstate->lrd[set->member->id].lr;
11935                                 if (lr1 == lr2) {
11936                                         continue;
11937                                 }
11938                                 if ((lr1->color != lr2->color) &&
11939                                         (lr1->color != REG_UNSET) &&
11940                                         (lr2->color != REG_UNSET)) {
11941                                         continue;
11942                                 }
11943                                 if ((lr1->classes & lr2->classes) == 0) {
11944                                         continue;
11945                                 }
11946                                 
11947                                 if (interfere(rstate, lr1, lr2)) {
11948                                         continue;
11949                                 }
11950                                 
11951                                 res = coalesce_ranges(state, rstate, lr1, lr2);
11952                                 coalesced += 1;
11953                                 if (res != lr1) {
11954                                         goto next;
11955                                 }
11956                         }
11957                 }
11958         next:
11959                 ;
11960         }
11961         return coalesced;
11962 }
11963
11964
11965 struct coalesce_conflict {
11966         struct triple *ins;
11967         int index;
11968 };
11969 static struct triple *spot_coalesce_conflict(struct compile_state *state,
11970         struct reg_block *blocks, struct triple_reg_set *live,
11971         struct reg_block *rb, struct triple *ins, void *arg)
11972 {
11973         struct coalesce_conflict *conflict = arg;
11974         int zlhs, zrhs, i, j;
11975         int found;
11976
11977         /* See if we have a mandatory coalesce operation between
11978          * a lhs and a rhs value.  If so and the rhs value is also
11979          * alive then this triple needs to be pre copied.  Otherwise
11980          * we would have two definitions in the same live range simultaneously
11981          * alive.
11982          */
11983         found = -1;
11984         zlhs = TRIPLE_LHS(ins->sizes);
11985         if ((zlhs == 0) && triple_is_def(state, ins)) {
11986                 zlhs = 1;
11987         }
11988         zrhs = TRIPLE_RHS(ins->sizes);
11989         for(i = 0; (i < zlhs) && (found == -1); i++) {
11990                 struct reg_info linfo;
11991                 linfo = arch_reg_lhs(state, ins, i);
11992                 if (linfo.reg < MAX_REGISTERS) {
11993                         continue;
11994                 }
11995                 for(j = 0; (j < zrhs) && (found == -1); j++) {
11996                         struct reg_info rinfo;
11997                         struct triple *rhs;
11998                         struct triple_reg_set *set;
11999                         rinfo = arch_reg_rhs(state, ins, j);
12000                         if (rinfo.reg != linfo.reg) {
12001                                 continue;
12002                         }
12003                         rhs = RHS(ins, j);
12004                         for(set = live; set && (found == -1); set = set->next) {
12005                                 if (set->member == rhs) {
12006                                         found = j;
12007                                 }
12008                         }
12009                 }
12010         }
12011         /* Only update conflict if we are the least dominated conflict */
12012         if ((found != -1) &&
12013                 (!conflict->ins || tdominates(state, ins, conflict->ins))) {
12014                 conflict->ins = ins;
12015                 conflict->index = found;
12016         }
12017         return ins;
12018 }
12019
12020 static void resolve_coalesce_conflict(
12021         struct compile_state *state, struct coalesce_conflict *conflict)
12022 {
12023         struct triple *copy;
12024         copy = pre_copy(state, conflict->ins, conflict->index);
12025         copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12026 }
12027         
12028
12029 static struct triple *spot_tangle(struct compile_state *state,
12030         struct reg_block *blocks, struct triple_reg_set *live,
12031         struct reg_block *rb, struct triple *ins, void *arg)
12032 {
12033         struct triple **tangle = arg;
12034         char used[MAX_REGISTERS];
12035         struct triple_reg_set *set;
12036
12037         /* Find out which registers have multiple uses at this point */
12038         memset(used, 0, sizeof(used));
12039         for(set = live; set; set = set->next) {
12040                 struct reg_info info;
12041                 info = find_lhs_color(state, set->member, 0);
12042                 if (info.reg == REG_UNSET) {
12043                         continue;
12044                 }
12045                 reg_inc_used(state, used, info.reg);
12046         }
12047
12048         /* Now find the least dominated definition of a register in
12049          * conflict I have seen so far.
12050          */
12051         for(set = live; set; set = set->next) {
12052                 struct reg_info info;
12053                 info = find_lhs_color(state, set->member, 0);
12054                 if (used[info.reg] < 2) {
12055                         continue;
12056                 }
12057                 if (!*tangle || tdominates(state, set->member, *tangle)) {
12058                         *tangle = set->member;
12059                 }
12060         }
12061         return ins;
12062 }
12063
12064 static void resolve_tangle(struct compile_state *state, struct triple *tangle)
12065 {
12066         struct reg_info info, uinfo;
12067         struct triple_set *set, *next;
12068         struct triple *copy;
12069
12070 #if 0
12071         fprintf(stderr, "Resolving tangle: %p\n", tangle);
12072         print_blocks(state, stderr);
12073 #endif
12074         info = find_lhs_color(state, tangle, 0);
12075 #if 0
12076         fprintf(stderr, "color: %d\n", info.reg);
12077 #endif
12078         for(set = tangle->use; set; set = next) {
12079                 struct triple *user;
12080                 int i, zrhs;
12081                 next = set->next;
12082                 user = set->member;
12083                 zrhs = TRIPLE_RHS(user->sizes);
12084                 for(i = 0; i < zrhs; i++) {
12085                         if (RHS(user, i) != tangle) {
12086                                 continue;
12087                         }
12088                         uinfo = find_rhs_post_color(state, user, i);
12089 #if 0
12090                         fprintf(stderr, "%p rhs %d color: %d\n", 
12091                                 user, i, uinfo.reg);
12092 #endif
12093                         if (uinfo.reg == info.reg) {
12094                                 copy = pre_copy(state, user, i);
12095                                 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12096                         }
12097                 }
12098         }
12099         uinfo = find_lhs_pre_color(state, tangle, 0);
12100 #if 0
12101         fprintf(stderr, "pre color: %d\n", uinfo.reg);
12102 #endif
12103         if (uinfo.reg == info.reg) {
12104                 copy = post_copy(state, tangle);
12105                 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12106         }
12107 }
12108
12109
12110 struct least_conflict {
12111         struct reg_state *rstate;
12112         struct live_range *ref_range;
12113         struct triple *ins;
12114         struct triple_reg_set *live;
12115         size_t count;
12116 };
12117 static struct triple *least_conflict(struct compile_state *state,
12118         struct reg_block *blocks, struct triple_reg_set *live,
12119         struct reg_block *rb, struct triple *ins, void *arg)
12120 {
12121         struct least_conflict *conflict = arg;
12122         struct live_range_edge *edge;
12123         struct triple_reg_set *set;
12124         size_t count;
12125
12126 #warning "FIXME handle instructions with left hand sides..."
12127         /* Only instructions that introduce a new definition
12128          * can be the conflict instruction.
12129          */
12130         if (!triple_is_def(state, ins)) {
12131                 return ins;
12132         }
12133
12134         /* See if live ranges at this instruction are a
12135          * strict subset of the live ranges that are in conflict.
12136          */
12137         count = 0;
12138         for(set = live; set; set = set->next) {
12139                 struct live_range *lr;
12140                 lr = conflict->rstate->lrd[set->member->id].lr;
12141                 for(edge = conflict->ref_range->edges; edge; edge = edge->next) {
12142                         if (edge->node == lr) {
12143                                 break;
12144                         }
12145                 }
12146                 if (!edge && (lr != conflict->ref_range)) {
12147                         return ins;
12148                 }
12149                 count++;
12150         }
12151         if (count <= 1) {
12152                 return ins;
12153         }
12154
12155         /* See if there is an uncolored member in this subset. 
12156          */
12157          for(set = live; set; set = set->next) {
12158                 struct live_range *lr;
12159                 lr = conflict->rstate->lrd[set->member->id].lr;
12160                 if (lr->color == REG_UNSET) {
12161                         break;
12162                 }
12163         }
12164         if (!set && (conflict->ref_range != REG_UNSET)) {
12165                 return ins;
12166         }
12167
12168
12169         /* Find the instruction with the largest possible subset of
12170          * conflict ranges and that dominates any other instruction
12171          * with an equal sized set of conflicting ranges.
12172          */
12173         if ((count > conflict->count) ||
12174                 ((count == conflict->count) &&
12175                         tdominates(state, ins, conflict->ins))) {
12176                 struct triple_reg_set *next;
12177                 /* Remember the canidate instruction */
12178                 conflict->ins = ins;
12179                 conflict->count = count;
12180                 /* Free the old collection of live registers */
12181                 for(set = conflict->live; set; set = next) {
12182                         next = set->next;
12183                         do_triple_unset(&conflict->live, set->member);
12184                 }
12185                 conflict->live = 0;
12186                 /* Rember the registers that are alive but do not feed
12187                  * into or out of conflict->ins.
12188                  */
12189                 for(set = live; set; set = set->next) {
12190                         struct triple **expr;
12191                         if (set->member == ins) {
12192                                 goto next;
12193                         }
12194                         expr = triple_rhs(state, ins, 0);
12195                         for(;expr; expr = triple_rhs(state, ins, expr)) {
12196                                 if (*expr == set->member) {
12197                                         goto next;
12198                                 }
12199                         }
12200                         expr = triple_lhs(state, ins, 0);
12201                         for(; expr; expr = triple_lhs(state, ins, expr)) {
12202                                 if (*expr == set->member) {
12203                                         goto next;
12204                                 }
12205                         }
12206                         do_triple_set(&conflict->live, set->member, set->new);
12207                 next:
12208                         ;
12209                 }
12210         }
12211         return ins;
12212 }
12213
12214 static void find_range_conflict(struct compile_state *state,
12215         struct reg_state *rstate, char *used, struct live_range *ref_range,
12216         struct least_conflict *conflict)
12217 {
12218         /* there are 3 kinds ways conflicts can occure.
12219          * 1) the life time of 2 values simply overlap.
12220          * 2) the 2 values feed into the same instruction.
12221          * 3) the 2 values feed into a phi function.
12222          */
12223
12224         /* find the instruction where the problematic conflict comes
12225          * into existance.  that the instruction where all of
12226          * the values are alive, and among such instructions it is
12227          * the least dominated one.
12228          *
12229          * a value is alive an an instruction if either;
12230          * 1) the value defintion dominates the instruction and there
12231          *    is a use at or after that instrction
12232          * 2) the value definition feeds into a phi function in the
12233          *    same block as the instruction.  and the phi function
12234          *    is at or after the instruction.
12235          */
12236         memset(conflict, 0, sizeof(*conflict));
12237         conflict->rstate    = rstate;
12238         conflict->ref_range = ref_range;
12239         conflict->ins       = 0;
12240         conflict->count     = 0;
12241         conflict->live      = 0;
12242         walk_variable_lifetimes(state, rstate->blocks, least_conflict, conflict);
12243
12244         if (!conflict->ins) {
12245                 internal_error(state, 0, "No conflict ins?");
12246         }
12247         if (!conflict->live) {
12248                 internal_error(state, 0, "No conflict live?");
12249         }
12250         return;
12251 }
12252
12253 static struct triple *split_constrained_range(struct compile_state *state, 
12254         struct reg_state *rstate, char *used, struct least_conflict *conflict)
12255 {
12256         unsigned constrained_size;
12257         struct triple *new, *constrained;
12258         struct triple_reg_set *cset;
12259         /* Find a range that is having problems because it is
12260          * artificially constrained.
12261          */
12262         constrained_size = ~0;
12263         constrained = 0;
12264         new = 0;
12265         for(cset = conflict->live; cset; cset = cset->next) {
12266                 struct triple_set *set;
12267                 struct reg_info info;
12268                 unsigned classes;
12269                 unsigned cur_size, size;
12270                 /* Skip the live range that starts with conflict->ins */
12271                 if (cset->member == conflict->ins) {
12272                         continue;
12273                 }
12274                 /* Find how many registers this value can potentially
12275                  * be assigned to.
12276                  */
12277                 classes = arch_type_to_regcm(state, cset->member->type);
12278                 size = regc_max_size(state, classes);
12279
12280                 /* Find how many registers we allow this value to
12281                  * be assigned to.
12282                  */
12283                 info = arch_reg_lhs(state, cset->member, 0);
12284 #warning "FIXME do I need a call to arch_reg_rhs around here somewhere?"
12285                 if ((info.reg == REG_UNSET) || (info.reg >= MAX_REGISTERS)) {
12286                         cur_size = regc_max_size(state, info.regcm);
12287                 } else {
12288                         cur_size = 1;
12289                 }
12290                 /* If this live_range feeds into conflict->ins
12291                  * splitting it is unlikely to help.
12292                  */
12293                 for(set = cset->member->use; set; set = set->next) {
12294                         if (set->member == conflict->ins) {
12295                                 goto next;
12296                         }
12297                 }
12298
12299                 /* If there is no difference between potential and
12300                  * actual register count there is nothing to do.
12301                  */
12302                 if (cur_size >= size) {
12303                         continue;
12304                 }
12305                 /* Of the constrained registers deal with the
12306                  * most constrained one first.
12307                  */
12308                 if (!constrained ||
12309                         (size < constrained_size)) {
12310                         constrained = cset->member;
12311                         constrained_size = size;
12312                 }
12313         next:
12314                 ;
12315         }
12316         if (constrained) {
12317                 new = post_copy(state, constrained);
12318                 new->id |= TRIPLE_FLAG_POST_SPLIT;
12319         }
12320         return new;
12321 }
12322
12323 static int split_ranges(
12324         struct compile_state *state, struct reg_state *rstate, 
12325         char *used, struct live_range *range)
12326 {
12327         struct triple *new;
12328
12329         if ((range->color == REG_UNNEEDED) ||
12330                 (rstate->passes >= rstate->max_passes)) {
12331                 return 0;
12332         }
12333         new = 0;
12334         /* If I can't allocate a register something needs to be split */
12335         if (arch_select_free_register(state, used, range->classes) == REG_UNSET) {
12336                 struct least_conflict conflict;
12337
12338                 /* Find where in the set of registers the conflict
12339                  * actually occurs.
12340                  */
12341                 find_range_conflict(state, rstate, used, range, &conflict);
12342
12343                 /* If a range has been artifically constrained split it */
12344                 new = split_constrained_range(state, rstate, used, &conflict);
12345                 
12346                 if (!new) {
12347                 /* Ideally I would split the live range that will not be used
12348                  * for the longest period of time in hopes that this will 
12349                  * (a) allow me to spill a register or
12350                  * (b) allow me to place a value in another register.
12351                  *
12352                  * So far I don't have a test case for this, the resolving
12353                  * of mandatory constraints has solved all of my
12354                  * know issues.  So I have choosen not to write any
12355                  * code until I cat get a better feel for cases where
12356                  * it would be useful to have.
12357                  *
12358                  */
12359 #warning "WISHLIST implement live range splitting..."
12360                         return 0;
12361                 }
12362         }
12363         if (new) {
12364                 rstate->lrd[rstate->defs].orig_id = new->id;
12365                 new->id = rstate->defs;
12366                 rstate->defs++;
12367 #if 0
12368                 fprintf(stderr, "new: %p\n", new);
12369 #endif
12370                 return 1;
12371         }
12372         return 0;
12373 }
12374
12375 #if DEBUG_COLOR_GRAPH > 1
12376 #define cgdebug_printf(...) fprintf(stdout, __VA_ARGS__)
12377 #define cgdebug_flush() fflush(stdout)
12378 #elif DEBUG_COLOR_GRAPH == 1
12379 #define cgdebug_printf(...) fprintf(stderr, __VA_ARGS__)
12380 #define cgdebug_flush() fflush(stderr)
12381 #else
12382 #define cgdebug_printf(...)
12383 #define cgdebug_flush()
12384 #endif
12385
12386         
12387 static int select_free_color(struct compile_state *state, 
12388         struct reg_state *rstate, struct live_range *range)
12389 {
12390         struct triple_set *entry;
12391         struct live_range_def *lrd;
12392         struct live_range_def *phi;
12393         struct live_range_edge *edge;
12394         char used[MAX_REGISTERS];
12395         struct triple **expr;
12396
12397         /* Instead of doing just the trivial color select here I try
12398          * a few extra things because a good color selection will help reduce
12399          * copies.
12400          */
12401
12402         /* Find the registers currently in use */
12403         memset(used, 0, sizeof(used));
12404         for(edge = range->edges; edge; edge = edge->next) {
12405                 if (edge->node->color == REG_UNSET) {
12406                         continue;
12407                 }
12408                 reg_fill_used(state, used, edge->node->color);
12409         }
12410 #if DEBUG_COLOR_GRAPH > 1
12411         {
12412                 int i;
12413                 i = 0;
12414                 for(edge = range->edges; edge; edge = edge->next) {
12415                         i++;
12416                 }
12417                 cgdebug_printf("\n%s edges: %d @%s:%d.%d\n", 
12418                         tops(range->def->op), i, 
12419                         range->def->filename, range->def->line, range->def->col);
12420                 for(i = 0; i < MAX_REGISTERS; i++) {
12421                         if (used[i]) {
12422                                 cgdebug_printf("used: %s\n",
12423                                         arch_reg_str(i));
12424                         }
12425                 }
12426         }       
12427 #endif
12428
12429 #warning "FIXME detect conflicts caused by the source and destination being the same register"
12430
12431         /* If a color is already assigned see if it will work */
12432         if (range->color != REG_UNSET) {
12433                 struct live_range_def *lrd;
12434                 if (!used[range->color]) {
12435                         return 1;
12436                 }
12437                 for(edge = range->edges; edge; edge = edge->next) {
12438                         if (edge->node->color != range->color) {
12439                                 continue;
12440                         }
12441                         warning(state, edge->node->defs->def, "edge: ");
12442                         lrd = edge->node->defs;
12443                         do {
12444                                 warning(state, lrd->def, " %p %s",
12445                                         lrd->def, tops(lrd->def->op));
12446                                 lrd = lrd->next;
12447                         } while(lrd != edge->node->defs);
12448                 }
12449                 lrd = range->defs;
12450                 warning(state, range->defs->def, "def: ");
12451                 do {
12452                         warning(state, lrd->def, " %p %s",
12453                                 lrd->def, tops(lrd->def->op));
12454                         lrd = lrd->next;
12455                 } while(lrd != range->defs);
12456                 internal_error(state, range->defs->def,
12457                         "live range with already used color %s",
12458                         arch_reg_str(range->color));
12459         }
12460
12461         /* If I feed into an expression reuse it's color.
12462          * This should help remove copies in the case of 2 register instructions
12463          * and phi functions.
12464          */
12465         phi = 0;
12466         lrd = live_range_end(state, range, 0);
12467         for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
12468                 entry = lrd->def->use;
12469                 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
12470                         struct live_range_def *insd;
12471                         insd = &rstate->lrd[entry->member->id];
12472                         if (insd->lr->defs == 0) {
12473                                 continue;
12474                         }
12475                         if (!phi && (insd->def->op == OP_PHI) &&
12476                                 !interfere(rstate, range, insd->lr)) {
12477                                 phi = insd;
12478                         }
12479                         if ((insd->lr->color == REG_UNSET) ||
12480                                 ((insd->lr->classes & range->classes) == 0) ||
12481                                 (used[insd->lr->color])) {
12482                                 continue;
12483                         }
12484                         if (interfere(rstate, range, insd->lr)) {
12485                                 continue;
12486                         }
12487                         range->color = insd->lr->color;
12488                 }
12489         }
12490         /* If I feed into a phi function reuse it's color or the color
12491          * of something else that feeds into the phi function.
12492          */
12493         if (phi) {
12494                 if (phi->lr->color != REG_UNSET) {
12495                         if (used[phi->lr->color]) {
12496                                 range->color = phi->lr->color;
12497                         }
12498                 }
12499                 else {
12500                         expr = triple_rhs(state, phi->def, 0);
12501                         for(; expr; expr = triple_rhs(state, phi->def, expr)) {
12502                                 struct live_range *lr;
12503                                 if (!*expr) {
12504                                         continue;
12505                                 }
12506                                 lr = rstate->lrd[(*expr)->id].lr;
12507                                 if ((lr->color == REG_UNSET) || 
12508                                         ((lr->classes & range->classes) == 0) ||
12509                                         (used[lr->color])) {
12510                                         continue;
12511                                 }
12512                                 if (interfere(rstate, range, lr)) {
12513                                         continue;
12514                                 }
12515                                 range->color = lr->color;
12516                         }
12517                 }
12518         }
12519         /* If I don't interfere with a rhs node reuse it's color */
12520         lrd = live_range_head(state, range, 0);
12521         for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
12522                 expr = triple_rhs(state, lrd->def, 0);
12523                 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
12524                         struct live_range *lr;
12525                         if (!*expr) {
12526                                 continue;
12527                         }
12528                         lr = rstate->lrd[(*expr)->id].lr;
12529                         if ((lr->color == -1) || 
12530                                 ((lr->classes & range->classes) == 0) ||
12531                                 (used[lr->color])) {
12532                                 continue;
12533                         }
12534                         if (interfere(rstate, range, lr)) {
12535                                 continue;
12536                         }
12537                         range->color = lr->color;
12538                         break;
12539                 }
12540         }
12541         /* If I have not opportunitically picked a useful color
12542          * pick the first color that is free.
12543          */
12544         if (range->color == REG_UNSET) {
12545                 range->color = 
12546                         arch_select_free_register(state, used, range->classes);
12547         }
12548         if (range->color == REG_UNSET) {
12549                 int i;
12550                 if (split_ranges(state, rstate, used, range)) {
12551                         return 0;
12552                 }
12553                 for(edge = range->edges; edge; edge = edge->next) {
12554                         if (edge->node->color == REG_UNSET) {
12555                                 continue;
12556                         }
12557                         warning(state, edge->node->defs->def, "reg %s", 
12558                                 arch_reg_str(edge->node->color));
12559                 }
12560                 warning(state, range->defs->def, "classes: %x",
12561                         range->classes);
12562                 for(i = 0; i < MAX_REGISTERS; i++) {
12563                         if (used[i]) {
12564                                 warning(state, range->defs->def, "used: %s",
12565                                         arch_reg_str(i));
12566                         }
12567                 }
12568 #if DEBUG_COLOR_GRAPH < 2
12569                 error(state, range->defs->def, "too few registers");
12570 #else
12571                 internal_error(state, range->defs->def, "too few registers");
12572 #endif
12573         }
12574         range->classes = arch_reg_regcm(state, range->color);
12575         if (range->color == -1) {
12576                 internal_error(state, range->defs->def, "select_free_color did not?");
12577         }
12578         return 1;
12579 }
12580
12581 static int color_graph(struct compile_state *state, struct reg_state *rstate)
12582 {
12583         int colored;
12584         struct live_range_edge *edge;
12585         struct live_range *range;
12586         if (rstate->low) {
12587                 cgdebug_printf("Lo: ");
12588                 range = rstate->low;
12589                 if (*range->group_prev != range) {
12590                         internal_error(state, 0, "lo: *prev != range?");
12591                 }
12592                 *range->group_prev = range->group_next;
12593                 if (range->group_next) {
12594                         range->group_next->group_prev = range->group_prev;
12595                 }
12596                 if (&range->group_next == rstate->low_tail) {
12597                         rstate->low_tail = range->group_prev;
12598                 }
12599                 if (rstate->low == range) {
12600                         internal_error(state, 0, "low: next != prev?");
12601                 }
12602         }
12603         else if (rstate->high) {
12604                 cgdebug_printf("Hi: ");
12605                 range = rstate->high;
12606                 if (*range->group_prev != range) {
12607                         internal_error(state, 0, "hi: *prev != range?");
12608                 }
12609                 *range->group_prev = range->group_next;
12610                 if (range->group_next) {
12611                         range->group_next->group_prev = range->group_prev;
12612                 }
12613                 if (&range->group_next == rstate->high_tail) {
12614                         rstate->high_tail = range->group_prev;
12615                 }
12616                 if (rstate->high == range) {
12617                         internal_error(state, 0, "high: next != prev?");
12618                 }
12619         }
12620         else {
12621                 return 1;
12622         }
12623         cgdebug_printf(" %d\n", range - rstate->lr);
12624         range->group_prev = 0;
12625         for(edge = range->edges; edge; edge = edge->next) {
12626                 struct live_range *node;
12627                 node = edge->node;
12628                 /* Move nodes from the high to the low list */
12629                 if (node->group_prev && (node->color == REG_UNSET) &&
12630                         (node->degree == regc_max_size(state, node->classes))) {
12631                         if (*node->group_prev != node) {
12632                                 internal_error(state, 0, "move: *prev != node?");
12633                         }
12634                         *node->group_prev = node->group_next;
12635                         if (node->group_next) {
12636                                 node->group_next->group_prev = node->group_prev;
12637                         }
12638                         if (&node->group_next == rstate->high_tail) {
12639                                 rstate->high_tail = node->group_prev;
12640                         }
12641                         cgdebug_printf("Moving...%d to low\n", node - rstate->lr);
12642                         node->group_prev  = rstate->low_tail;
12643                         node->group_next  = 0;
12644                         *rstate->low_tail = node;
12645                         rstate->low_tail  = &node->group_next;
12646                         if (*node->group_prev != node) {
12647                                 internal_error(state, 0, "move2: *prev != node?");
12648                         }
12649                 }
12650                 node->degree -= 1;
12651         }
12652         colored = color_graph(state, rstate);
12653         if (colored) {
12654                 cgdebug_printf("Coloring %d @%s:%d.%d:", 
12655                         range - rstate->lr,
12656                         range->def->filename, range->def->line, range->def->col);
12657                 cgdebug_flush();
12658                 colored = select_free_color(state, rstate, range);
12659                 cgdebug_printf(" %s\n", arch_reg_str(range->color));
12660         }
12661         return colored;
12662 }
12663
12664 static void verify_colors(struct compile_state *state, struct reg_state *rstate)
12665 {
12666         struct live_range *lr;
12667         struct live_range_edge *edge;
12668         struct triple *ins, *first;
12669         char used[MAX_REGISTERS];
12670         first = RHS(state->main_function, 0);
12671         ins = first;
12672         do {
12673                 if (triple_is_def(state, ins)) {
12674                         if ((ins->id < 0) || (ins->id > rstate->defs)) {
12675                                 internal_error(state, ins, 
12676                                         "triple without a live range def");
12677                         }
12678                         lr = rstate->lrd[ins->id].lr;
12679                         if (lr->color == REG_UNSET) {
12680                                 internal_error(state, ins,
12681                                         "triple without a color");
12682                         }
12683                         /* Find the registers used by the edges */
12684                         memset(used, 0, sizeof(used));
12685                         for(edge = lr->edges; edge; edge = edge->next) {
12686                                 if (edge->node->color == REG_UNSET) {
12687                                         internal_error(state, 0,
12688                                                 "live range without a color");
12689                         }
12690                                 reg_fill_used(state, used, edge->node->color);
12691                         }
12692                         if (used[lr->color]) {
12693                                 internal_error(state, ins,
12694                                         "triple with already used color");
12695                         }
12696                 }
12697                 ins = ins->next;
12698         } while(ins != first);
12699 }
12700
12701 static void color_triples(struct compile_state *state, struct reg_state *rstate)
12702 {
12703         struct live_range *lr;
12704         struct triple *first, *ins;
12705         first = RHS(state->main_function, 0);
12706         ins = first;
12707         do {
12708                 if ((ins->id < 0) || (ins->id > rstate->defs)) {
12709                         internal_error(state, ins, 
12710                                 "triple without a live range");
12711                 }
12712                 lr = rstate->lrd[ins->id].lr;
12713                 SET_REG(ins->id, lr->color);
12714                 ins = ins->next;
12715         } while (ins != first);
12716 }
12717
12718 static void print_interference_block(
12719         struct compile_state *state, struct block *block, void *arg)
12720
12721 {
12722         struct reg_state *rstate = arg;
12723         struct reg_block *rb;
12724         struct triple *ptr;
12725         int phi_present;
12726         int done;
12727         rb = &rstate->blocks[block->vertex];
12728
12729         printf("\nblock: %p (%d), %p<-%p %p<-%p\n", 
12730                 block, 
12731                 block->vertex,
12732                 block->left, 
12733                 block->left && block->left->use?block->left->use->member : 0,
12734                 block->right, 
12735                 block->right && block->right->use?block->right->use->member : 0);
12736         if (rb->in) {
12737                 struct triple_reg_set *in_set;
12738                 printf("        in:");
12739                 for(in_set = rb->in; in_set; in_set = in_set->next) {
12740                         printf(" %-10p", in_set->member);
12741                 }
12742                 printf("\n");
12743         }
12744         phi_present = 0;
12745         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
12746                 done = (ptr == block->last);
12747                 if (ptr->op == OP_PHI) {
12748                         phi_present = 1;
12749                         break;
12750                 }
12751         }
12752         if (phi_present) {
12753                 int edge;
12754                 for(edge = 0; edge < block->users; edge++) {
12755                         printf("     in(%d):", edge);
12756                         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
12757                                 struct triple **slot;
12758                                 done = (ptr == block->last);
12759                                 if (ptr->op != OP_PHI) {
12760                                         continue;
12761                                 }
12762                                 slot = &RHS(ptr, 0);
12763                                 printf(" %-10p", slot[edge]);
12764                         }
12765                         printf("\n");
12766                 }
12767         }
12768         if (block->first->op == OP_LABEL) {
12769                 printf("%p:\n", block->first);
12770         }
12771         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
12772                 struct triple_set *user;
12773                 struct live_range *lr;
12774                 unsigned id;
12775                 int op;
12776                 op = ptr->op;
12777                 done = (ptr == block->last);
12778                 lr = rstate->lrd[ptr->id].lr;
12779                 
12780                 if (triple_stores_block(state, ptr)) {
12781                         if (ptr->u.block != block) {
12782                                 internal_error(state, ptr, 
12783                                         "Wrong block pointer: %p",
12784                                         ptr->u.block);
12785                         }
12786                 }
12787                 if (op == OP_ADECL) {
12788                         for(user = ptr->use; user; user = user->next) {
12789                                 if (!user->member->u.block) {
12790                                         internal_error(state, user->member, 
12791                                                 "Use %p not in a block?",
12792                                                 user->member);
12793                                 }
12794                                 
12795                         }
12796                 }
12797                 id = ptr->id;
12798                 SET_REG(ptr->id, lr->color);
12799                 display_triple(stdout, ptr);
12800                 ptr->id = id;
12801
12802                 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
12803                         internal_error(state, ptr, "lr has no defs!");
12804                 }
12805
12806                 if (lr->defs) {
12807                         struct live_range_def *lrd;
12808                         printf("       range:");
12809                         lrd = lr->defs;
12810                         do {
12811                                 printf(" %-10p", lrd->def);
12812                                 lrd = lrd->next;
12813                         } while(lrd != lr->defs);
12814                         printf("\n");
12815                 }
12816                 if (lr->edges > 0) {
12817                         struct live_range_edge *edge;
12818                         printf("       edges:");
12819                         for(edge = lr->edges; edge; edge = edge->next) {
12820                                 struct live_range_def *lrd;
12821                                 lrd = edge->node->defs;
12822                                 do {
12823                                         printf(" %-10p", lrd->def);
12824                                         lrd = lrd->next;
12825                                 } while(lrd != edge->node->defs);
12826                                 printf("|");
12827                         }
12828                         printf("\n");
12829                 }
12830                 /* Do a bunch of sanity checks */
12831                 valid_ins(state, ptr);
12832                 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
12833                         internal_error(state, ptr, "Invalid triple id: %d",
12834                                 ptr->id);
12835                 }
12836                 for(user = ptr->use; user; user = user->next) {
12837                         struct triple *use;
12838                         struct live_range *ulr;
12839                         use = user->member;
12840                         valid_ins(state, use);
12841                         if ((use->id < 0) || (use->id > rstate->defs)) {
12842                                 internal_error(state, use, "Invalid triple id: %d",
12843                                         use->id);
12844                         }
12845                         ulr = rstate->lrd[user->member->id].lr;
12846                         if (triple_stores_block(state, user->member) &&
12847                                 !user->member->u.block) {
12848                                 internal_error(state, user->member,
12849                                         "Use %p not in a block?",
12850                                         user->member);
12851                         }
12852                 }
12853         }
12854         if (rb->out) {
12855                 struct triple_reg_set *out_set;
12856                 printf("       out:");
12857                 for(out_set = rb->out; out_set; out_set = out_set->next) {
12858                         printf(" %-10p", out_set->member);
12859                 }
12860                 printf("\n");
12861         }
12862         printf("\n");
12863 }
12864
12865 static struct live_range *merge_sort_lr(
12866         struct live_range *first, struct live_range *last)
12867 {
12868         struct live_range *mid, *join, **join_tail, *pick;
12869         size_t size;
12870         size = (last - first) + 1;
12871         if (size >= 2) {
12872                 mid = first + size/2;
12873                 first = merge_sort_lr(first, mid -1);
12874                 mid   = merge_sort_lr(mid, last);
12875                 
12876                 join = 0;
12877                 join_tail = &join;
12878                 /* merge the two lists */
12879                 while(first && mid) {
12880                         if ((first->degree < mid->degree) ||
12881                                 ((first->degree == mid->degree) &&
12882                                         (first->length < mid->length))) {
12883                                 pick = first;
12884                                 first = first->group_next;
12885                                 if (first) {
12886                                         first->group_prev = 0;
12887                                 }
12888                         }
12889                         else {
12890                                 pick = mid;
12891                                 mid = mid->group_next;
12892                                 if (mid) {
12893                                         mid->group_prev = 0;
12894                                 }
12895                         }
12896                         pick->group_next = 0;
12897                         pick->group_prev = join_tail;
12898                         *join_tail = pick;
12899                         join_tail = &pick->group_next;
12900                 }
12901                 /* Splice the remaining list */
12902                 pick = (first)? first : mid;
12903                 *join_tail = pick;
12904                 if (pick) { 
12905                         pick->group_prev = join_tail;
12906                 }
12907         }
12908         else {
12909                 if (!first->defs) {
12910                         first = 0;
12911                 }
12912                 join = first;
12913         }
12914         return join;
12915 }
12916
12917 static void ids_from_rstate(struct compile_state *state, 
12918         struct reg_state *rstate)
12919 {
12920         struct triple *ins, *first;
12921         if (!rstate->defs) {
12922                 return;
12923         }
12924         /* Display the graph if desired */
12925         if (state->debug & DEBUG_INTERFERENCE) {
12926                 print_blocks(state, stdout);
12927                 print_control_flow(state);
12928         }
12929         first = RHS(state->main_function, 0);
12930         ins = first;
12931         do {
12932                 if (ins->id) {
12933                         struct live_range_def *lrd;
12934                         lrd = &rstate->lrd[ins->id];
12935                         ins->id = lrd->orig_id;
12936                 }
12937                 ins = ins->next;
12938         } while(ins != first);
12939 }
12940
12941 static void cleanup_live_edges(struct reg_state *rstate)
12942 {
12943         int i;
12944         /* Free the edges on each node */
12945         for(i = 1; i <= rstate->ranges; i++) {
12946                 remove_live_edges(rstate, &rstate->lr[i]);
12947         }
12948 }
12949
12950 static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
12951 {
12952         cleanup_live_edges(rstate);
12953         xfree(rstate->lrd);
12954         xfree(rstate->lr);
12955
12956         /* Free the variable lifetime information */
12957         if (rstate->blocks) {
12958                 free_variable_lifetimes(state, rstate->blocks);
12959         }
12960         rstate->defs = 0;
12961         rstate->ranges = 0;
12962         rstate->lrd = 0;
12963         rstate->lr = 0;
12964         rstate->blocks = 0;
12965 }
12966
12967 static void allocate_registers(struct compile_state *state)
12968 {
12969         struct reg_state rstate;
12970         int colored;
12971
12972         /* Clear out the reg_state */
12973         memset(&rstate, 0, sizeof(rstate));
12974         rstate.max_passes = MAX_ALLOCATION_PASSES;
12975
12976         do {
12977                 struct live_range **point, **next;
12978                 struct triple *tangle;
12979                 struct coalesce_conflict conflict;
12980                 int coalesced;
12981
12982                 /* Restore ids */
12983                 ids_from_rstate(state, &rstate);
12984
12985                 do {
12986                         /* Cleanup the temporary data structures */
12987                         cleanup_rstate(state, &rstate);
12988
12989                         /* Compute the variable lifetimes */
12990                         rstate.blocks = compute_variable_lifetimes(state);
12991
12992                         /* Find an invalid mandatory live range coalesce */
12993                         conflict.ins = 0;
12994                         conflict.index = -1;
12995                         walk_variable_lifetimes(
12996                                 state, rstate.blocks, spot_coalesce_conflict, &conflict);
12997                         
12998                         /* If a tangle was found resolve it */
12999                         if (conflict.ins) {
13000                                 resolve_coalesce_conflict(state, &conflict);
13001                         }
13002                 } while(conflict.ins);
13003
13004                 do {
13005                         /* Cleanup the temporary data structures */
13006                         cleanup_rstate(state, &rstate);
13007
13008                         /* Compute the variable lifetimes */
13009                         rstate.blocks = compute_variable_lifetimes(state);
13010
13011                         /* Find two simultaneous uses of the same register */
13012                         tangle = 0;
13013                         walk_variable_lifetimes(
13014                                 state, rstate.blocks, spot_tangle, &tangle);
13015                         
13016                         /* If a tangle was found resolve it */
13017                         if (tangle) {
13018                                 resolve_tangle(state, tangle);
13019                         }
13020                 } while(tangle);
13021
13022                 if (state->debug & DEBUG_INSERTED_COPIES) {
13023                         printf("After resolve_tangles\n");
13024                         print_blocks(state, stdout);
13025                         print_control_flow(state);
13026                 }
13027
13028                 
13029                 /* Allocate and initialize the live ranges */
13030                 initialize_live_ranges(state, &rstate);
13031                 
13032                 do {
13033                         /* Forget previous live range edge calculations */
13034                         cleanup_live_edges(&rstate);
13035
13036                         /* Compute the interference graph */
13037                         walk_variable_lifetimes(
13038                                 state, rstate.blocks, graph_ins, &rstate);
13039                 
13040                         /* Display the interference graph if desired */
13041                         if (state->debug & DEBUG_INTERFERENCE) {
13042                                 printf("\nlive variables by block\n");
13043                                 walk_blocks(state, print_interference_block, &rstate);
13044                                 printf("\nlive variables by instruction\n");
13045                                 walk_variable_lifetimes(
13046                                         state, rstate.blocks, 
13047                                         print_interference_ins, &rstate);
13048                         }
13049                         
13050                         coalesced = coalesce_live_ranges(state, &rstate);
13051                 } while(coalesced);
13052                         
13053                 /* Build the groups low and high.  But with the nodes
13054                  * first sorted by degree order.
13055                  */
13056                 rstate.low_tail  = &rstate.low;
13057                 rstate.high_tail = &rstate.high;
13058                 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
13059                 if (rstate.high) {
13060                         rstate.high->group_prev = &rstate.high;
13061                 }
13062                 for(point = &rstate.high; *point; point = &(*point)->group_next)
13063                         ;
13064                 rstate.high_tail = point;
13065                 /* Walk through the high list and move everything that needs
13066                  * to be onto low.
13067                  */
13068                 for(point = &rstate.high; *point; point = next) {
13069                         struct live_range *range;
13070                         next = &(*point)->group_next;
13071                         range = *point;
13072                         
13073                         /* If it has a low degree or it already has a color
13074                          * place the node in low.
13075                          */
13076                         if ((range->degree < regc_max_size(state, range->classes)) ||
13077                                 (range->color != REG_UNSET)) {
13078                                 cgdebug_printf("Lo: %5d degree %5d%s\n", 
13079                                         range - rstate.lr, range->degree,
13080                                         (range->color != REG_UNSET) ? " (colored)": "");
13081                                 *range->group_prev = range->group_next;
13082                                 if (range->group_next) {
13083                                         range->group_next->group_prev = range->group_prev;
13084                                 }
13085                                 if (&range->group_next == rstate.high_tail) {
13086                                         rstate.high_tail = range->group_prev;
13087                                 }
13088                                 range->group_prev  = rstate.low_tail;
13089                                 range->group_next  = 0;
13090                                 *rstate.low_tail   = range;
13091                                 rstate.low_tail    = &range->group_next;
13092                                 next = point;
13093                         }
13094                         else {
13095                                 cgdebug_printf("hi: %5d degree %5d%s\n", 
13096                                         range - rstate.lr, range->degree,
13097                                         (range->color != REG_UNSET) ? " (colored)": "");
13098                         }
13099                 }
13100                 /* Color the live_ranges */
13101                 colored = color_graph(state, &rstate);
13102                 rstate.passes++;
13103         } while (!colored);
13104
13105         /* Verify the graph was properly colored */
13106         verify_colors(state, &rstate);
13107
13108         /* Move the colors from the graph to the triples */
13109         color_triples(state, &rstate);
13110
13111         /* Cleanup the temporary data structures */
13112         cleanup_rstate(state, &rstate);
13113 }
13114
13115 /* Sparce Conditional Constant Propogation
13116  * =========================================
13117  */
13118 struct ssa_edge;
13119 struct flow_block;
13120 struct lattice_node {
13121         unsigned old_id;
13122         struct triple *def;
13123         struct ssa_edge *out;
13124         struct flow_block *fblock;
13125         struct triple *val;
13126         /* lattice high   val && !is_const(val) 
13127          * lattice const  is_const(val)
13128          * lattice low    val == 0
13129          */
13130 };
13131 struct ssa_edge {
13132         struct lattice_node *src;
13133         struct lattice_node *dst;
13134         struct ssa_edge *work_next;
13135         struct ssa_edge *work_prev;
13136         struct ssa_edge *out_next;
13137 };
13138 struct flow_edge {
13139         struct flow_block *src;
13140         struct flow_block *dst;
13141         struct flow_edge *work_next;
13142         struct flow_edge *work_prev;
13143         struct flow_edge *in_next;
13144         struct flow_edge *out_next;
13145         int executable;
13146 };
13147 struct flow_block {
13148         struct block *block;
13149         struct flow_edge *in;
13150         struct flow_edge *out;
13151         struct flow_edge left, right;
13152 };
13153
13154 struct scc_state {
13155         int ins_count;
13156         struct lattice_node *lattice;
13157         struct ssa_edge     *ssa_edges;
13158         struct flow_block   *flow_blocks;
13159         struct flow_edge    *flow_work_list;
13160         struct ssa_edge     *ssa_work_list;
13161 };
13162
13163
13164 static void scc_add_fedge(struct compile_state *state, struct scc_state *scc, 
13165         struct flow_edge *fedge)
13166 {
13167         if (!scc->flow_work_list) {
13168                 scc->flow_work_list = fedge;
13169                 fedge->work_next = fedge->work_prev = fedge;
13170         }
13171         else {
13172                 struct flow_edge *ftail;
13173                 ftail = scc->flow_work_list->work_prev;
13174                 fedge->work_next = ftail->work_next;
13175                 fedge->work_prev = ftail;
13176                 fedge->work_next->work_prev = fedge;
13177                 fedge->work_prev->work_next = fedge;
13178         }
13179 }
13180
13181 static struct flow_edge *scc_next_fedge(
13182         struct compile_state *state, struct scc_state *scc)
13183 {
13184         struct flow_edge *fedge;
13185         fedge = scc->flow_work_list;
13186         if (fedge) {
13187                 fedge->work_next->work_prev = fedge->work_prev;
13188                 fedge->work_prev->work_next = fedge->work_next;
13189                 if (fedge->work_next != fedge) {
13190                         scc->flow_work_list = fedge->work_next;
13191                 } else {
13192                         scc->flow_work_list = 0;
13193                 }
13194         }
13195         return fedge;
13196 }
13197
13198 static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
13199         struct ssa_edge *sedge)
13200 {
13201         if (!scc->ssa_work_list) {
13202                 scc->ssa_work_list = sedge;
13203                 sedge->work_next = sedge->work_prev = sedge;
13204         }
13205         else {
13206                 struct ssa_edge *stail;
13207                 stail = scc->ssa_work_list->work_prev;
13208                 sedge->work_next = stail->work_next;
13209                 sedge->work_prev = stail;
13210                 sedge->work_next->work_prev = sedge;
13211                 sedge->work_prev->work_next = sedge;
13212         }
13213 }
13214
13215 static struct ssa_edge *scc_next_sedge(
13216         struct compile_state *state, struct scc_state *scc)
13217 {
13218         struct ssa_edge *sedge;
13219         sedge = scc->ssa_work_list;
13220         if (sedge) {
13221                 sedge->work_next->work_prev = sedge->work_prev;
13222                 sedge->work_prev->work_next = sedge->work_next;
13223                 if (sedge->work_next != sedge) {
13224                         scc->ssa_work_list = sedge->work_next;
13225                 } else {
13226                         scc->ssa_work_list = 0;
13227                 }
13228         }
13229         return sedge;
13230 }
13231
13232 static void initialize_scc_state(
13233         struct compile_state *state, struct scc_state *scc)
13234 {
13235         int ins_count, ssa_edge_count;
13236         int ins_index, ssa_edge_index, fblock_index;
13237         struct triple *first, *ins;
13238         struct block *block;
13239         struct flow_block *fblock;
13240
13241         memset(scc, 0, sizeof(*scc));
13242
13243         /* Inialize pass zero find out how much memory we need */
13244         first = RHS(state->main_function, 0);
13245         ins = first;
13246         ins_count = ssa_edge_count = 0;
13247         do {
13248                 struct triple_set *edge;
13249                 ins_count += 1;
13250                 for(edge = ins->use; edge; edge = edge->next) {
13251                         ssa_edge_count++;
13252                 }
13253                 ins = ins->next;
13254         } while(ins != first);
13255 #if DEBUG_SCC
13256         fprintf(stderr, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
13257                 ins_count, ssa_edge_count, state->last_vertex);
13258 #endif
13259         scc->ins_count   = ins_count;
13260         scc->lattice     = 
13261                 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
13262         scc->ssa_edges   = 
13263                 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
13264         scc->flow_blocks = 
13265                 xcmalloc(sizeof(*scc->flow_blocks)*(state->last_vertex + 1), 
13266                         "flow_blocks");
13267
13268         /* Initialize pass one collect up the nodes */
13269         fblock = 0;
13270         block = 0;
13271         ins_index = ssa_edge_index = fblock_index = 0;
13272         ins = first;
13273         do {
13274                 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
13275                         block = ins->u.block;
13276                         if (!block) {
13277                                 internal_error(state, ins, "label without block");
13278                         }
13279                         fblock_index += 1;
13280                         block->vertex = fblock_index;
13281                         fblock = &scc->flow_blocks[fblock_index];
13282                         fblock->block = block;
13283                 }
13284                 {
13285                         struct lattice_node *lnode;
13286                         ins_index += 1;
13287                         lnode = &scc->lattice[ins_index];
13288                         lnode->def = ins;
13289                         lnode->out = 0;
13290                         lnode->fblock = fblock;
13291                         lnode->val = ins; /* LATTICE HIGH */
13292                         lnode->old_id = ins->id;
13293                         ins->id = ins_index;
13294                 }
13295                 ins = ins->next;
13296         } while(ins != first);
13297         /* Initialize pass two collect up the edges */
13298         block = 0;
13299         fblock = 0;
13300         ins = first;
13301         do {
13302                 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
13303                         struct flow_edge *fedge, **ftail;
13304                         struct block_set *bedge;
13305                         block = ins->u.block;
13306                         fblock = &scc->flow_blocks[block->vertex];
13307                         fblock->in = 0;
13308                         fblock->out = 0;
13309                         ftail = &fblock->out;
13310                         if (block->left) {
13311                                 fblock->left.dst = &scc->flow_blocks[block->left->vertex];
13312                                 if (fblock->left.dst->block != block->left) {
13313                                         internal_error(state, 0, "block mismatch");
13314                                 }
13315                                 fblock->left.out_next = 0;
13316                                 *ftail = &fblock->left;
13317                                 ftail = &fblock->left.out_next;
13318                         }
13319                         if (block->right) {
13320                                 fblock->right.dst = &scc->flow_blocks[block->right->vertex];
13321                                 if (fblock->right.dst->block != block->right) {
13322                                         internal_error(state, 0, "block mismatch");
13323                                 }
13324                                 fblock->right.out_next = 0;
13325                                 *ftail = &fblock->right;
13326                                 ftail = &fblock->right.out_next;
13327                         }
13328                         for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
13329                                 fedge->src = fblock;
13330                                 fedge->work_next = fedge->work_prev = fedge;
13331                                 fedge->executable = 0;
13332                         }
13333                         ftail = &fblock->in;
13334                         for(bedge = block->use; bedge; bedge = bedge->next) {
13335                                 struct block *src_block;
13336                                 struct flow_block *sfblock;
13337                                 struct flow_edge *sfedge;
13338                                 src_block = bedge->member;
13339                                 sfblock = &scc->flow_blocks[src_block->vertex];
13340                                 sfedge = 0;
13341                                 if (src_block->left == block) {
13342                                         sfedge = &sfblock->left;
13343                                 } else {
13344                                         sfedge = &sfblock->right;
13345                                 }
13346                                 *ftail = sfedge;
13347                                 ftail = &sfedge->in_next;
13348                                 sfedge->in_next = 0;
13349                         }
13350                 }
13351                 {
13352                         struct triple_set *edge;
13353                         struct ssa_edge **stail;
13354                         struct lattice_node *lnode;
13355                         lnode = &scc->lattice[ins->id];
13356                         lnode->out = 0;
13357                         stail = &lnode->out;
13358                         for(edge = ins->use; edge; edge = edge->next) {
13359                                 struct ssa_edge *sedge;
13360                                 ssa_edge_index += 1;
13361                                 sedge = &scc->ssa_edges[ssa_edge_index];
13362                                 *stail = sedge;
13363                                 stail = &sedge->out_next;
13364                                 sedge->src = lnode;
13365                                 sedge->dst = &scc->lattice[edge->member->id];
13366                                 sedge->work_next = sedge->work_prev = sedge;
13367                                 sedge->out_next = 0;
13368                         }
13369                 }
13370                 ins = ins->next;
13371         } while(ins != first);
13372         /* Setup a dummy block 0 as a node above the start node */
13373         {
13374                 struct flow_block *fblock, *dst;
13375                 struct flow_edge *fedge;
13376                 fblock = &scc->flow_blocks[0];
13377                 fblock->block = 0;
13378                 fblock->in = 0;
13379                 fblock->out = &fblock->left;
13380                 dst = &scc->flow_blocks[state->first_block->vertex];
13381                 fedge = &fblock->left;
13382                 fedge->src        = fblock;
13383                 fedge->dst        = dst;
13384                 fedge->work_next  = fedge;
13385                 fedge->work_prev  = fedge;
13386                 fedge->in_next    = fedge->dst->in;
13387                 fedge->out_next   = 0;
13388                 fedge->executable = 0;
13389                 fedge->dst->in = fedge;
13390                 
13391                 /* Initialize the work lists */
13392                 scc->flow_work_list = 0;
13393                 scc->ssa_work_list  = 0;
13394                 scc_add_fedge(state, scc, fedge);
13395         }
13396 #if DEBUG_SCC
13397         fprintf(stderr, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
13398                 ins_index, ssa_edge_index, fblock_index);
13399 #endif
13400 }
13401
13402         
13403 static void free_scc_state(
13404         struct compile_state *state, struct scc_state *scc)
13405 {
13406         xfree(scc->flow_blocks);
13407         xfree(scc->ssa_edges);
13408         xfree(scc->lattice);
13409         
13410 }
13411
13412 static struct lattice_node *triple_to_lattice(
13413         struct compile_state *state, struct scc_state *scc, struct triple *ins)
13414 {
13415         if (ins->id <= 0) {
13416                 internal_error(state, ins, "bad id");
13417         }
13418         return &scc->lattice[ins->id];
13419 }
13420
13421 static struct triple *preserve_lval(
13422         struct compile_state *state, struct lattice_node *lnode)
13423 {
13424         struct triple *old;
13425         /* Preserve the original value */
13426         if (lnode->val) {
13427                 old = dup_triple(state, lnode->val);
13428                 if (lnode->val != lnode->def) {
13429                         xfree(lnode->val);
13430                 }
13431                 lnode->val = 0;
13432         } else {
13433                 old = 0;
13434         }
13435         return old;
13436 }
13437
13438 static int lval_changed(struct compile_state *state, 
13439         struct triple *old, struct lattice_node *lnode)
13440 {
13441         int changed;
13442         /* See if the lattice value has changed */
13443         changed = 1;
13444         if (!old && !lnode->val) {
13445                 changed = 0;
13446         }
13447         if (changed && lnode->val && !is_const(lnode->val)) {
13448                 changed = 0;
13449         }
13450         if (changed &&
13451                 lnode->val && old &&
13452                 (memcmp(lnode->val->param, old->param,
13453                         TRIPLE_SIZE(lnode->val->sizes) * sizeof(lnode->val->param[0])) == 0) &&
13454                 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
13455                 changed = 0;
13456         }
13457         if (old) {
13458                 xfree(old);
13459         }
13460         return changed;
13461
13462 }
13463
13464 static void scc_visit_phi(struct compile_state *state, struct scc_state *scc, 
13465         struct lattice_node *lnode)
13466 {
13467         struct lattice_node *tmp;
13468         struct triple **slot, *old;
13469         struct flow_edge *fedge;
13470         int index;
13471         if (lnode->def->op != OP_PHI) {
13472                 internal_error(state, lnode->def, "not phi");
13473         }
13474         /* Store the original value */
13475         old = preserve_lval(state, lnode);
13476
13477         /* default to lattice high */
13478         lnode->val = lnode->def;
13479         slot = &RHS(lnode->def, 0);
13480         index = 0;
13481         for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
13482                 if (!fedge->executable) {
13483                         continue;
13484                 }
13485                 if (!slot[index]) {
13486                         internal_error(state, lnode->def, "no phi value");
13487                 }
13488                 tmp = triple_to_lattice(state, scc, slot[index]);
13489                 /* meet(X, lattice low) = lattice low */
13490                 if (!tmp->val) {
13491                         lnode->val = 0;
13492                 }
13493                 /* meet(X, lattice high) = X */
13494                 else if (!tmp->val) {
13495                         lnode->val = lnode->val;
13496                 }
13497                 /* meet(lattice high, X) = X */
13498                 else if (!is_const(lnode->val)) {
13499                         lnode->val = dup_triple(state, tmp->val);
13500                         lnode->val->type = lnode->def->type;
13501                 }
13502                 /* meet(const, const) = const or lattice low */
13503                 else if (!constants_equal(state, lnode->val, tmp->val)) {
13504                         lnode->val = 0;
13505                 }
13506                 if (!lnode->val) {
13507                         break;
13508                 }
13509         }
13510 #if DEBUG_SCC
13511         fprintf(stderr, "phi: %d -> %s\n",
13512                 lnode->def->id,
13513                 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
13514 #endif
13515         /* If the lattice value has changed update the work lists. */
13516         if (lval_changed(state, old, lnode)) {
13517                 struct ssa_edge *sedge;
13518                 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
13519                         scc_add_sedge(state, scc, sedge);
13520                 }
13521         }
13522 }
13523
13524 static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
13525         struct lattice_node *lnode)
13526 {
13527         int changed;
13528         struct triple *old, *scratch;
13529         struct triple **dexpr, **vexpr;
13530         int count, i;
13531         
13532         /* Store the original value */
13533         old = preserve_lval(state, lnode);
13534
13535         /* Reinitialize the value */
13536         lnode->val = scratch = dup_triple(state, lnode->def);
13537         scratch->id = lnode->old_id;
13538         scratch->next     = scratch;
13539         scratch->prev     = scratch;
13540         scratch->use      = 0;
13541
13542         count = TRIPLE_SIZE(scratch->sizes);
13543         for(i = 0; i < count; i++) {
13544                 dexpr = &lnode->def->param[i];
13545                 vexpr = &scratch->param[i];
13546                 *vexpr = *dexpr;
13547                 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
13548                         (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
13549                         *dexpr) {
13550                         struct lattice_node *tmp;
13551                         tmp = triple_to_lattice(state, scc, *dexpr);
13552                         *vexpr = (tmp->val)? tmp->val : tmp->def;
13553                 }
13554         }
13555         if (scratch->op == OP_BRANCH) {
13556                 scratch->next = lnode->def->next;
13557         }
13558         /* Recompute the value */
13559 #warning "FIXME see if simplify does anything bad"
13560         /* So far it looks like only the strength reduction
13561          * optimization are things I need to worry about.
13562          */
13563         simplify(state, scratch);
13564         /* Cleanup my value */
13565         if (scratch->use) {
13566                 internal_error(state, lnode->def, "scratch used?");
13567         }
13568         if ((scratch->prev != scratch) ||
13569                 ((scratch->next != scratch) &&
13570                         ((lnode->def->op != OP_BRANCH) ||
13571                                 (scratch->next != lnode->def->next)))) {
13572                 internal_error(state, lnode->def, "scratch in list?");
13573         }
13574         /* undo any uses... */
13575         count = TRIPLE_SIZE(scratch->sizes);
13576         for(i = 0; i < count; i++) {
13577                 vexpr = &scratch->param[i];
13578                 if (*vexpr) {
13579                         unuse_triple(*vexpr, scratch);
13580                 }
13581         }
13582         if (!is_const(scratch)) {
13583                 for(i = 0; i < count; i++) {
13584                         dexpr = &lnode->def->param[i];
13585                         if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
13586                                 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
13587                                 *dexpr) {
13588                                 struct lattice_node *tmp;
13589                                 tmp = triple_to_lattice(state, scc, *dexpr);
13590                                 if (!tmp->val) {
13591                                         lnode->val = 0;
13592                                 }
13593                         }
13594                 }
13595         }
13596         if (lnode->val && 
13597                 (lnode->val->op == lnode->def->op) &&
13598                 (memcmp(lnode->val->param, lnode->def->param, 
13599                         count * sizeof(lnode->val->param[0])) == 0) &&
13600                 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
13601                 lnode->val = lnode->def;
13602         }
13603         /* Find the cases that are always lattice lo */
13604         if (lnode->val && 
13605                 triple_is_def(state, lnode->val) &&
13606                 !triple_is_pure(state, lnode->val)) {
13607                 lnode->val = 0;
13608         }
13609         if (lnode->val && 
13610                 (lnode->val->op == OP_SDECL) && 
13611                 (lnode->val != lnode->def)) {
13612                 internal_error(state, lnode->def, "bad sdecl");
13613         }
13614         /* See if the lattice value has changed */
13615         changed = lval_changed(state, old, lnode);
13616         if (lnode->val != scratch) {
13617                 xfree(scratch);
13618         }
13619         return changed;
13620 }
13621
13622 static void scc_visit_branch(struct compile_state *state, struct scc_state *scc,
13623         struct lattice_node *lnode)
13624 {
13625         struct lattice_node *cond;
13626 #if DEBUG_SCC
13627         {
13628                 struct flow_edge *fedge;
13629                 fprintf(stderr, "branch: %d (",
13630                         lnode->def->id);
13631                 
13632                 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
13633                         fprintf(stderr, " %d", fedge->dst->block->vertex);
13634                 }
13635                 fprintf(stderr, " )");
13636                 if (TRIPLE_RHS(lnode->def->sizes) > 0) {
13637                         fprintf(stderr, " <- %d",
13638                                 RHS(lnode->def, 0)->id);
13639                 }
13640                 fprintf(stderr, "\n");
13641         }
13642 #endif
13643         if (lnode->def->op != OP_BRANCH) {
13644                 internal_error(state, lnode->def, "not branch");
13645         }
13646         /* This only applies to conditional branches */
13647         if (TRIPLE_RHS(lnode->def->sizes) == 0) {
13648                 return;
13649         }
13650         cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
13651         if (cond->val && !is_const(cond->val)) {
13652 #warning "FIXME do I need to do something here?"
13653                 warning(state, cond->def, "condition not constant?");
13654                 return;
13655         }
13656         if (cond->val == 0) {
13657                 scc_add_fedge(state, scc, cond->fblock->out);
13658                 scc_add_fedge(state, scc, cond->fblock->out->out_next);
13659         }
13660         else if (cond->val->u.cval) {
13661                 scc_add_fedge(state, scc, cond->fblock->out->out_next);
13662                 
13663         } else {
13664                 scc_add_fedge(state, scc, cond->fblock->out);
13665         }
13666
13667 }
13668
13669 static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
13670         struct lattice_node *lnode)
13671 {
13672         int changed;
13673
13674         changed = compute_lnode_val(state, scc, lnode);
13675 #if DEBUG_SCC
13676         {
13677                 struct triple **expr;
13678                 fprintf(stderr, "expr: %3d %10s (",
13679                         lnode->def->id, tops(lnode->def->op));
13680                 expr = triple_rhs(state, lnode->def, 0);
13681                 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
13682                         if (*expr) {
13683                                 fprintf(stderr, " %d", (*expr)->id);
13684                         }
13685                 }
13686                 fprintf(stderr, " ) -> %s\n",
13687                         (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
13688         }
13689 #endif
13690         if (lnode->def->op == OP_BRANCH) {
13691                 scc_visit_branch(state, scc, lnode);
13692
13693         }
13694         else if (changed) {
13695                 struct ssa_edge *sedge;
13696                 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
13697                         scc_add_sedge(state, scc, sedge);
13698                 }
13699         }
13700 }
13701
13702 static void scc_writeback_values(
13703         struct compile_state *state, struct scc_state *scc)
13704 {
13705         struct triple *first, *ins;
13706         first = RHS(state->main_function, 0);
13707         ins = first;
13708         do {
13709                 struct lattice_node *lnode;
13710                 lnode = triple_to_lattice(state, scc, ins);
13711                 /* Restore id */
13712                 ins->id = lnode->old_id;
13713 #if DEBUG_SCC
13714                 if (lnode->val && !is_const(lnode->val)) {
13715                         warning(state, lnode->def, 
13716                                 "lattice node still high?");
13717                 }
13718 #endif
13719                 if (lnode->val && (lnode->val != ins)) {
13720                         /* See if it something I know how to write back */
13721                         switch(lnode->val->op) {
13722                         case OP_INTCONST:
13723                                 mkconst(state, ins, lnode->val->u.cval);
13724                                 break;
13725                         case OP_ADDRCONST:
13726                                 mkaddr_const(state, ins, 
13727                                         MISC(lnode->val, 0), lnode->val->u.cval);
13728                                 break;
13729                         default:
13730                                 /* By default don't copy the changes,
13731                                  * recompute them in place instead.
13732                                  */
13733                                 simplify(state, ins);
13734                                 break;
13735                         }
13736                         if (is_const(lnode->val) &&
13737                                 !constants_equal(state, lnode->val, ins)) {
13738                                 internal_error(state, 0, "constants not equal");
13739                         }
13740                         /* Free the lattice nodes */
13741                         xfree(lnode->val);
13742                         lnode->val = 0;
13743                 }
13744                 ins = ins->next;
13745         } while(ins != first);
13746 }
13747
13748 static void scc_transform(struct compile_state *state)
13749 {
13750         struct scc_state scc;
13751
13752         initialize_scc_state(state, &scc);
13753
13754         while(scc.flow_work_list || scc.ssa_work_list) {
13755                 struct flow_edge *fedge;
13756                 struct ssa_edge *sedge;
13757                 struct flow_edge *fptr;
13758                 while((fedge = scc_next_fedge(state, &scc))) {
13759                         struct block *block;
13760                         struct triple *ptr;
13761                         struct flow_block *fblock;
13762                         int time;
13763                         int done;
13764                         if (fedge->executable) {
13765                                 continue;
13766                         }
13767                         if (!fedge->dst) {
13768                                 internal_error(state, 0, "fedge without dst");
13769                         }
13770                         if (!fedge->src) {
13771                                 internal_error(state, 0, "fedge without src");
13772                         }
13773                         fedge->executable = 1;
13774                         fblock = fedge->dst;
13775                         block = fblock->block;
13776                         time = 0;
13777                         for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
13778                                 if (fptr->executable) {
13779                                         time++;
13780                                 }
13781                         }
13782 #if DEBUG_SCC
13783                         fprintf(stderr, "vertex: %d time: %d\n", 
13784                                 block->vertex, time);
13785                         
13786 #endif
13787                         done = 0;
13788                         for(ptr = block->first; !done; ptr = ptr->next) {
13789                                 struct lattice_node *lnode;
13790                                 done = (ptr == block->last);
13791                                 lnode = &scc.lattice[ptr->id];
13792                                 if (ptr->op == OP_PHI) {
13793                                         scc_visit_phi(state, &scc, lnode);
13794                                 }
13795                                 else if (time == 1) {
13796                                         scc_visit_expr(state, &scc, lnode);
13797                                 }
13798                         }
13799                         if (fblock->out && !fblock->out->out_next) {
13800                                 scc_add_fedge(state, &scc, fblock->out);
13801                         }
13802                 }
13803                 while((sedge = scc_next_sedge(state, &scc))) {
13804                         struct lattice_node *lnode;
13805                         struct flow_block *fblock;
13806                         lnode = sedge->dst;
13807                         fblock = lnode->fblock;
13808 #if DEBUG_SCC
13809                         fprintf(stderr, "sedge: %5d (%5d -> %5d)\n",
13810                                 sedge - scc.ssa_edges,
13811                                 sedge->src->def->id,
13812                                 sedge->dst->def->id);
13813 #endif
13814                         if (lnode->def->op == OP_PHI) {
13815                                 scc_visit_phi(state, &scc, lnode);
13816                         }
13817                         else {
13818                                 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
13819                                         if (fptr->executable) {
13820                                                 break;
13821                                         }
13822                                 }
13823                                 if (fptr) {
13824                                         scc_visit_expr(state, &scc, lnode);
13825                                 }
13826                         }
13827                 }
13828         }
13829         
13830         scc_writeback_values(state, &scc);
13831         free_scc_state(state, &scc);
13832 }
13833
13834
13835 static void transform_to_arch_instructions(struct compile_state *state)
13836 {
13837         struct triple *ins, *first;
13838         first = RHS(state->main_function, 0);
13839         ins = first;
13840         do {
13841                 ins = transform_to_arch_instruction(state, ins);
13842         } while(ins != first);
13843 }
13844
13845 #if DEBUG_CONSISTENCY
13846 static void verify_uses(struct compile_state *state)
13847 {
13848         struct triple *first, *ins;
13849         struct triple_set *set;
13850         first = RHS(state->main_function, 0);
13851         ins = first;
13852         do {
13853                 struct triple **expr;
13854                 expr = triple_rhs(state, ins, 0);
13855                 for(; expr; expr = triple_rhs(state, ins, expr)) {
13856                         for(set = *expr?(*expr)->use:0; set; set = set->next) {
13857                                 if (set->member == ins) {
13858                                         break;
13859                                 }
13860                         }
13861                         if (!set) {
13862                                 internal_error(state, ins, "rhs not used");
13863                         }
13864                 }
13865                 expr = triple_lhs(state, ins, 0);
13866                 for(; expr; expr = triple_lhs(state, ins, expr)) {
13867                         for(set =  *expr?(*expr)->use:0; set; set = set->next) {
13868                                 if (set->member == ins) {
13869                                         break;
13870                                 }
13871                         }
13872                         if (!set) {
13873                                 internal_error(state, ins, "lhs not used");
13874                         }
13875                 }
13876                 ins = ins->next;
13877         } while(ins != first);
13878         
13879 }
13880 static void verify_blocks(struct compile_state *state)
13881 {
13882         struct triple *ins;
13883         struct block *block;
13884         block = state->first_block;
13885         if (!block) {
13886                 return;
13887         }
13888         do {
13889                 for(ins = block->first; ins != block->last->next; ins = ins->next) {
13890                         if (!triple_stores_block(state, ins)) {
13891                                 continue;
13892                         }
13893                         if (ins->u.block != block) {
13894                                 internal_error(state, ins, "inconsitent block specified");
13895                         }
13896                 }
13897                 if (!triple_stores_block(state, block->last->next)) {
13898                         internal_error(state, block->last->next, 
13899                                 "cannot find next block");
13900                 }
13901                 block = block->last->next->u.block;
13902                 if (!block) {
13903                         internal_error(state, block->last->next,
13904                                 "bad next block");
13905                 }
13906         } while(block != state->first_block);
13907 }
13908
13909 static void verify_domination(struct compile_state *state)
13910 {
13911         struct triple *first, *ins;
13912         struct triple_set *set;
13913         if (!state->first_block) {
13914                 return;
13915         }
13916         
13917         first = RHS(state->main_function, 0);
13918         ins = first;
13919         do {
13920                 for(set = ins->use; set; set = set->next) {
13921                         struct triple **expr;
13922                         if (set->member->op == OP_PHI) {
13923                                 continue;
13924                         }
13925                         /* See if the use is on the righ hand side */
13926                         expr = triple_rhs(state, set->member, 0);
13927                         for(; expr ; expr = triple_rhs(state, set->member, expr)) {
13928                                 if (*expr == ins) {
13929                                         break;
13930                                 }
13931                         }
13932                         if (expr &&
13933                                 !tdominates(state, ins, set->member)) {
13934                                 internal_error(state, set->member, 
13935                                         "non dominated rhs use?");
13936                         }
13937                 }
13938                 ins = ins->next;
13939         } while(ins != first);
13940 }
13941
13942 static void verify_piece(struct compile_state *state)
13943 {
13944         struct triple *first, *ins;
13945         first = RHS(state->main_function, 0);
13946         ins = first;
13947         do {
13948                 struct triple *ptr;
13949                 int lhs, i;
13950                 lhs = TRIPLE_LHS(ins->sizes);
13951                 if ((ins->op == OP_WRITE) || (ins->op == OP_STORE)) {
13952                         lhs = 0;
13953                 }
13954                 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
13955                         if (ptr != LHS(ins, i)) {
13956                                 internal_error(state, ins, "malformed lhs on %s",
13957                                         tops(ins->op));
13958                         }
13959                         if (ptr->op != OP_PIECE) {
13960                                 internal_error(state, ins, "bad lhs op %s at %d on %s",
13961                                         tops(ptr->op), i, tops(ins->op));
13962                         }
13963                         if (ptr->u.cval != i) {
13964                                 internal_error(state, ins, "bad u.cval of %d %d expected",
13965                                         ptr->u.cval, i);
13966                         }
13967                 }
13968                 ins = ins->next;
13969         } while(ins != first);
13970 }
13971 static void verify_ins_colors(struct compile_state *state)
13972 {
13973         struct triple *first, *ins;
13974         
13975         first = RHS(state->main_function, 0);
13976         ins = first;
13977         do {
13978                 ins = ins->next;
13979         } while(ins != first);
13980 }
13981 static void verify_consistency(struct compile_state *state)
13982 {
13983         verify_uses(state);
13984         verify_blocks(state);
13985         verify_domination(state);
13986         verify_piece(state);
13987         verify_ins_colors(state);
13988 }
13989 #else 
13990 #define verify_consistency(state) do {} while(0)
13991 #endif /* DEBUG_USES */
13992
13993 static void optimize(struct compile_state *state)
13994 {
13995         if (state->debug & DEBUG_TRIPLES) {
13996                 print_triples(state);
13997         }
13998         /* Replace structures with simpler data types */
13999         flatten_structures(state);
14000         if (state->debug & DEBUG_TRIPLES) {
14001                 print_triples(state);
14002         }
14003         verify_consistency(state);
14004         /* Analize the intermediate code */
14005         setup_basic_blocks(state);
14006         analyze_idominators(state);
14007         analyze_ipdominators(state);
14008         /* Transform the code to ssa form */
14009         transform_to_ssa_form(state);
14010         verify_consistency(state);
14011         if (state->debug & DEBUG_CODE_ELIMINATION) {
14012                 fprintf(stdout, "After transform_to_ssa_form\n");
14013                 print_blocks(state, stdout);
14014         }
14015         /* Do strength reduction and simple constant optimizations */
14016         if (state->optimize >= 1) {
14017                 simplify_all(state);
14018         }
14019         verify_consistency(state);
14020         /* Propogate constants throughout the code */
14021         if (state->optimize >= 2) {
14022 #warning "FIXME fix scc_transform"
14023                 scc_transform(state);
14024                 transform_from_ssa_form(state);
14025                 free_basic_blocks(state);
14026                 setup_basic_blocks(state);
14027                 analyze_idominators(state);
14028                 analyze_ipdominators(state);
14029                 transform_to_ssa_form(state);
14030         }
14031         verify_consistency(state);
14032 #warning "WISHLIST implement single use constants (least possible register pressure)"
14033 #warning "WISHLIST implement induction variable elimination"
14034         /* Select architecture instructions and an initial partial
14035          * coloring based on architecture constraints.
14036          */
14037         transform_to_arch_instructions(state);
14038         verify_consistency(state);
14039         if (state->debug & DEBUG_ARCH_CODE) {
14040                 printf("After transform_to_arch_instructions\n");
14041                 print_blocks(state, stdout);
14042                 print_control_flow(state);
14043         }
14044         eliminate_inefectual_code(state);
14045         verify_consistency(state);
14046         if (state->debug & DEBUG_CODE_ELIMINATION) {
14047                 printf("After eliminate_inefectual_code\n");
14048                 print_blocks(state, stdout);
14049                 print_control_flow(state);
14050         }
14051         verify_consistency(state);
14052         /* Color all of the variables to see if they will fit in registers */
14053         insert_copies_to_phi(state);
14054         if (state->debug & DEBUG_INSERTED_COPIES) {
14055                 printf("After insert_copies_to_phi\n");
14056                 print_blocks(state, stdout);
14057                 print_control_flow(state);
14058         }
14059         verify_consistency(state);
14060         insert_mandatory_copies(state);
14061         if (state->debug & DEBUG_INSERTED_COPIES) {
14062                 printf("After insert_mandatory_copies\n");
14063                 print_blocks(state, stdout);
14064                 print_control_flow(state);
14065         }
14066         verify_consistency(state);
14067         allocate_registers(state);
14068         verify_consistency(state);
14069         if (state->debug & DEBUG_INTERMEDIATE_CODE) {
14070                 print_blocks(state, stdout);
14071         }
14072         if (state->debug & DEBUG_CONTROL_FLOW) {
14073                 print_control_flow(state);
14074         }
14075         /* Remove the optimization information.
14076          * This is more to check for memory consistency than to free memory.
14077          */
14078         free_basic_blocks(state);
14079 }
14080
14081 static void print_op_asm(struct compile_state *state,
14082         struct triple *ins, FILE *fp)
14083 {
14084         struct asm_info *info;
14085         const char *ptr;
14086         unsigned lhs, rhs, i;
14087         info = ins->u.ainfo;
14088         lhs = TRIPLE_LHS(ins->sizes);
14089         rhs = TRIPLE_RHS(ins->sizes);
14090         /* Don't count the clobbers in lhs */
14091         for(i = 0; i < lhs; i++) {
14092                 if (LHS(ins, i)->type == &void_type) {
14093                         break;
14094                 }
14095         }
14096         lhs = i;
14097         fputc('\t', fp);
14098         for(ptr = info->str; *ptr; ptr++) {
14099                 char *next;
14100                 unsigned long param;
14101                 struct triple *piece;
14102                 if (*ptr != '%') {
14103                         fputc(*ptr, fp);
14104                         continue;
14105                 }
14106                 ptr++;
14107                 if (*ptr == '%') {
14108                         fputc('%', fp);
14109                         continue;
14110                 }
14111                 param = strtoul(ptr, &next, 10);
14112                 if (ptr == next) {
14113                         error(state, ins, "Invalid asm template");
14114                 }
14115                 if (param >= (lhs + rhs)) {
14116                         error(state, ins, "Invalid param %%%u in asm template",
14117                                 param);
14118                 }
14119                 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
14120                 fprintf(fp, "%s", 
14121                         arch_reg_str(ID_REG(piece->id)));
14122                 ptr = next;
14123         }
14124         fputc('\n', fp);
14125 }
14126
14127
14128 /* Only use the low x86 byte registers.  This allows me
14129  * allocate the entire register when a byte register is used.
14130  */
14131 #define X86_4_8BIT_GPRS 1
14132
14133 /* Recognized x86 cpu variants */
14134 #define BAD_CPU      0
14135 #define CPU_I386     1
14136 #define CPU_P3       2
14137 #define CPU_P4       3
14138 #define CPU_K7       4
14139 #define CPU_K8       5
14140
14141 #define CPU_DEFAULT  CPU_I386
14142
14143 /* The x86 register classes */
14144 #define REGC_FLAGS    0
14145 #define REGC_GPR8     1
14146 #define REGC_GPR16    2
14147 #define REGC_GPR32    3
14148 #define REGC_GPR64    4
14149 #define REGC_MMX      5
14150 #define REGC_XMM      6
14151 #define REGC_GPR32_8  7
14152 #define REGC_GPR16_8  8
14153 #define REGC_IMM32    9
14154 #define REGC_IMM16   10
14155 #define REGC_IMM8    11
14156 #define LAST_REGC  REGC_IMM8
14157 #if LAST_REGC >= MAX_REGC
14158 #error "MAX_REGC is to low"
14159 #endif
14160
14161 /* Register class masks */
14162 #define REGCM_FLAGS   (1 << REGC_FLAGS)
14163 #define REGCM_GPR8    (1 << REGC_GPR8)
14164 #define REGCM_GPR16   (1 << REGC_GPR16)
14165 #define REGCM_GPR32   (1 << REGC_GPR32)
14166 #define REGCM_GPR64   (1 << REGC_GPR64)
14167 #define REGCM_MMX     (1 << REGC_MMX)
14168 #define REGCM_XMM     (1 << REGC_XMM)
14169 #define REGCM_GPR32_8 (1 << REGC_GPR32_8)
14170 #define REGCM_GPR16_8 (1 << REGC_GPR16_8)
14171 #define REGCM_IMM32   (1 << REGC_IMM32)
14172 #define REGCM_IMM16   (1 << REGC_IMM16)
14173 #define REGCM_IMM8    (1 << REGC_IMM8)
14174 #define REGCM_ALL     ((1 << (LAST_REGC + 1)) - 1)
14175
14176 /* The x86 registers */
14177 #define REG_EFLAGS  2
14178 #define REGC_FLAGS_FIRST REG_EFLAGS
14179 #define REGC_FLAGS_LAST  REG_EFLAGS
14180 #define REG_AL      3
14181 #define REG_BL      4
14182 #define REG_CL      5
14183 #define REG_DL      6
14184 #define REG_AH      7
14185 #define REG_BH      8
14186 #define REG_CH      9
14187 #define REG_DH      10
14188 #define REGC_GPR8_FIRST  REG_AL
14189 #if X86_4_8BIT_GPRS
14190 #define REGC_GPR8_LAST   REG_DL
14191 #else 
14192 #define REGC_GPR8_LAST   REG_DH
14193 #endif
14194 #define REG_AX     11
14195 #define REG_BX     12
14196 #define REG_CX     13
14197 #define REG_DX     14
14198 #define REG_SI     15
14199 #define REG_DI     16
14200 #define REG_BP     17
14201 #define REG_SP     18
14202 #define REGC_GPR16_FIRST REG_AX
14203 #define REGC_GPR16_LAST  REG_SP
14204 #define REG_EAX    19
14205 #define REG_EBX    20
14206 #define REG_ECX    21
14207 #define REG_EDX    22
14208 #define REG_ESI    23
14209 #define REG_EDI    24
14210 #define REG_EBP    25
14211 #define REG_ESP    26
14212 #define REGC_GPR32_FIRST REG_EAX
14213 #define REGC_GPR32_LAST  REG_ESP
14214 #define REG_EDXEAX 27
14215 #define REGC_GPR64_FIRST REG_EDXEAX
14216 #define REGC_GPR64_LAST  REG_EDXEAX
14217 #define REG_MMX0   28
14218 #define REG_MMX1   29
14219 #define REG_MMX2   30
14220 #define REG_MMX3   31
14221 #define REG_MMX4   32
14222 #define REG_MMX5   33
14223 #define REG_MMX6   34
14224 #define REG_MMX7   35
14225 #define REGC_MMX_FIRST REG_MMX0
14226 #define REGC_MMX_LAST  REG_MMX7
14227 #define REG_XMM0   36
14228 #define REG_XMM1   37
14229 #define REG_XMM2   38
14230 #define REG_XMM3   39
14231 #define REG_XMM4   40
14232 #define REG_XMM5   41
14233 #define REG_XMM6   42
14234 #define REG_XMM7   43
14235 #define REGC_XMM_FIRST REG_XMM0
14236 #define REGC_XMM_LAST  REG_XMM7
14237 #warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
14238 #define LAST_REG   REG_XMM7
14239
14240 #define REGC_GPR32_8_FIRST REG_EAX
14241 #define REGC_GPR32_8_LAST  REG_EDX
14242 #define REGC_GPR16_8_FIRST REG_AX
14243 #define REGC_GPR16_8_LAST  REG_DX
14244
14245 #define REGC_IMM8_FIRST    -1
14246 #define REGC_IMM8_LAST     -1
14247 #define REGC_IMM16_FIRST   -2
14248 #define REGC_IMM16_LAST    -1
14249 #define REGC_IMM32_FIRST   -4
14250 #define REGC_IMM32_LAST    -1
14251
14252 #if LAST_REG >= MAX_REGISTERS
14253 #error "MAX_REGISTERS to low"
14254 #endif
14255
14256
14257 static unsigned regc_size[LAST_REGC +1] = {
14258         [REGC_FLAGS]   = REGC_FLAGS_LAST   - REGC_FLAGS_FIRST + 1,
14259         [REGC_GPR8]    = REGC_GPR8_LAST    - REGC_GPR8_FIRST + 1,
14260         [REGC_GPR16]   = REGC_GPR16_LAST   - REGC_GPR16_FIRST + 1,
14261         [REGC_GPR32]   = REGC_GPR32_LAST   - REGC_GPR32_FIRST + 1,
14262         [REGC_GPR64]   = REGC_GPR64_LAST   - REGC_GPR64_FIRST + 1,
14263         [REGC_MMX]     = REGC_MMX_LAST     - REGC_MMX_FIRST + 1,
14264         [REGC_XMM]     = REGC_XMM_LAST     - REGC_XMM_FIRST + 1,
14265         [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
14266         [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
14267         [REGC_IMM32]   = 0,
14268         [REGC_IMM16]   = 0,
14269         [REGC_IMM8]    = 0,
14270 };
14271
14272 static const struct {
14273         int first, last;
14274 } regcm_bound[LAST_REGC + 1] = {
14275         [REGC_FLAGS]   = { REGC_FLAGS_FIRST,   REGC_FLAGS_LAST },
14276         [REGC_GPR8]    = { REGC_GPR8_FIRST,    REGC_GPR8_LAST },
14277         [REGC_GPR16]   = { REGC_GPR16_FIRST,   REGC_GPR16_LAST },
14278         [REGC_GPR32]   = { REGC_GPR32_FIRST,   REGC_GPR32_LAST },
14279         [REGC_GPR64]   = { REGC_GPR64_FIRST,   REGC_GPR64_LAST },
14280         [REGC_MMX]     = { REGC_MMX_FIRST,     REGC_MMX_LAST },
14281         [REGC_XMM]     = { REGC_XMM_FIRST,     REGC_XMM_LAST },
14282         [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
14283         [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
14284         [REGC_IMM32]   = { REGC_IMM32_FIRST,   REGC_IMM32_LAST },
14285         [REGC_IMM16]   = { REGC_IMM16_FIRST,   REGC_IMM16_LAST },
14286         [REGC_IMM8]    = { REGC_IMM8_FIRST,    REGC_IMM8_LAST },
14287 };
14288
14289 static int arch_encode_cpu(const char *cpu)
14290 {
14291         struct cpu {
14292                 const char *name;
14293                 int cpu;
14294         } cpus[] = {
14295                 { "i386", CPU_I386 },
14296                 { "p3",   CPU_P3 },
14297                 { "p4",   CPU_P4 },
14298                 { "k7",   CPU_K7 },
14299                 { "k8",   CPU_K8 },
14300                 {  0,     BAD_CPU }
14301         };
14302         struct cpu *ptr;
14303         for(ptr = cpus; ptr->name; ptr++) {
14304                 if (strcmp(ptr->name, cpu) == 0) {
14305                         break;
14306                 }
14307         }
14308         return ptr->cpu;
14309 }
14310
14311 static unsigned arch_regc_size(struct compile_state *state, int class)
14312 {
14313         if ((class < 0) || (class > LAST_REGC)) {
14314                 return 0;
14315         }
14316         return regc_size[class];
14317 }
14318 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
14319 {
14320         /* See if two register classes may have overlapping registers */
14321         unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 |
14322                 REGCM_GPR32_8 | REGCM_GPR32 | REGCM_GPR64;
14323
14324         /* Special case for the immediates */
14325         if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
14326                 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
14327                 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
14328                 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) { 
14329                 return 0;
14330         }
14331         return (regcm1 & regcm2) ||
14332                 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
14333 }
14334
14335 static void arch_reg_equivs(
14336         struct compile_state *state, unsigned *equiv, int reg)
14337 {
14338         if ((reg < 0) || (reg > LAST_REG)) {
14339                 internal_error(state, 0, "invalid register");
14340         }
14341         *equiv++ = reg;
14342         switch(reg) {
14343         case REG_AL:
14344 #if X86_4_8BIT_GPRS
14345                 *equiv++ = REG_AH;
14346 #endif
14347                 *equiv++ = REG_AX;
14348                 *equiv++ = REG_EAX;
14349                 *equiv++ = REG_EDXEAX;
14350                 break;
14351         case REG_AH:
14352 #if X86_4_8BIT_GPRS
14353                 *equiv++ = REG_AL;
14354 #endif
14355                 *equiv++ = REG_AX;
14356                 *equiv++ = REG_EAX;
14357                 *equiv++ = REG_EDXEAX;
14358                 break;
14359         case REG_BL:  
14360 #if X86_4_8BIT_GPRS
14361                 *equiv++ = REG_BH;
14362 #endif
14363                 *equiv++ = REG_BX;
14364                 *equiv++ = REG_EBX;
14365                 break;
14366
14367         case REG_BH:
14368 #if X86_4_8BIT_GPRS
14369                 *equiv++ = REG_BL;
14370 #endif
14371                 *equiv++ = REG_BX;
14372                 *equiv++ = REG_EBX;
14373                 break;
14374         case REG_CL:
14375 #if X86_4_8BIT_GPRS
14376                 *equiv++ = REG_CH;
14377 #endif
14378                 *equiv++ = REG_CX;
14379                 *equiv++ = REG_ECX;
14380                 break;
14381
14382         case REG_CH:
14383 #if X86_4_8BIT_GPRS
14384                 *equiv++ = REG_CL;
14385 #endif
14386                 *equiv++ = REG_CX;
14387                 *equiv++ = REG_ECX;
14388                 break;
14389         case REG_DL:
14390 #if X86_4_8BIT_GPRS
14391                 *equiv++ = REG_DH;
14392 #endif
14393                 *equiv++ = REG_DX;
14394                 *equiv++ = REG_EDX;
14395                 *equiv++ = REG_EDXEAX;
14396                 break;
14397         case REG_DH:
14398 #if X86_4_8BIT_GPRS
14399                 *equiv++ = REG_DL;
14400 #endif
14401                 *equiv++ = REG_DX;
14402                 *equiv++ = REG_EDX;
14403                 *equiv++ = REG_EDXEAX;
14404                 break;
14405         case REG_AX:
14406                 *equiv++ = REG_AL;
14407                 *equiv++ = REG_AH;
14408                 *equiv++ = REG_EAX;
14409                 *equiv++ = REG_EDXEAX;
14410                 break;
14411         case REG_BX:
14412                 *equiv++ = REG_BL;
14413                 *equiv++ = REG_BH;
14414                 *equiv++ = REG_EBX;
14415                 break;
14416         case REG_CX:  
14417                 *equiv++ = REG_CL;
14418                 *equiv++ = REG_CH;
14419                 *equiv++ = REG_ECX;
14420                 break;
14421         case REG_DX:  
14422                 *equiv++ = REG_DL;
14423                 *equiv++ = REG_DH;
14424                 *equiv++ = REG_EDX;
14425                 *equiv++ = REG_EDXEAX;
14426                 break;
14427         case REG_SI:  
14428                 *equiv++ = REG_ESI;
14429                 break;
14430         case REG_DI:
14431                 *equiv++ = REG_EDI;
14432                 break;
14433         case REG_BP:
14434                 *equiv++ = REG_EBP;
14435                 break;
14436         case REG_SP:
14437                 *equiv++ = REG_ESP;
14438                 break;
14439         case REG_EAX:
14440                 *equiv++ = REG_AL;
14441                 *equiv++ = REG_AH;
14442                 *equiv++ = REG_AX;
14443                 *equiv++ = REG_EDXEAX;
14444                 break;
14445         case REG_EBX:
14446                 *equiv++ = REG_BL;
14447                 *equiv++ = REG_BH;
14448                 *equiv++ = REG_BX;
14449                 break;
14450         case REG_ECX:
14451                 *equiv++ = REG_CL;
14452                 *equiv++ = REG_CH;
14453                 *equiv++ = REG_CX;
14454                 break;
14455         case REG_EDX:
14456                 *equiv++ = REG_DL;
14457                 *equiv++ = REG_DH;
14458                 *equiv++ = REG_DX;
14459                 *equiv++ = REG_EDXEAX;
14460                 break;
14461         case REG_ESI: 
14462                 *equiv++ = REG_SI;
14463                 break;
14464         case REG_EDI: 
14465                 *equiv++ = REG_DI;
14466                 break;
14467         case REG_EBP: 
14468                 *equiv++ = REG_BP;
14469                 break;
14470         case REG_ESP: 
14471                 *equiv++ = REG_SP;
14472                 break;
14473         case REG_EDXEAX: 
14474                 *equiv++ = REG_AL;
14475                 *equiv++ = REG_AH;
14476                 *equiv++ = REG_DL;
14477                 *equiv++ = REG_DH;
14478                 *equiv++ = REG_AX;
14479                 *equiv++ = REG_DX;
14480                 *equiv++ = REG_EAX;
14481                 *equiv++ = REG_EDX;
14482                 break;
14483         }
14484         *equiv++ = REG_UNSET; 
14485 }
14486
14487 static unsigned arch_avail_mask(struct compile_state *state)
14488 {
14489         unsigned avail_mask;
14490         avail_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 | 
14491                 REGCM_GPR32 | REGCM_GPR32_8 | REGCM_GPR64 |
14492                 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
14493         switch(state->cpu) {
14494         case CPU_P3:
14495         case CPU_K7:
14496                 avail_mask |= REGCM_MMX;
14497                 break;
14498         case CPU_P4:
14499         case CPU_K8:
14500                 avail_mask |= REGCM_MMX | REGCM_XMM;
14501                 break;
14502         }
14503 #if 0
14504         /* Don't enable 8 bit values until I can force both operands
14505          * to be 8bits simultaneously.
14506          */
14507         avail_mask &= ~(REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16);
14508 #endif
14509         return avail_mask;
14510 }
14511
14512 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
14513 {
14514         unsigned mask, result;
14515         int class, class2;
14516         result = regcm;
14517         result &= arch_avail_mask(state);
14518
14519         for(class = 0, mask = 1; mask; mask <<= 1, class++) {
14520                 if ((result & mask) == 0) {
14521                         continue;
14522                 }
14523                 if (class > LAST_REGC) {
14524                         result &= ~mask;
14525                 }
14526                 for(class2 = 0; class2 <= LAST_REGC; class2++) {
14527                         if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
14528                                 (regcm_bound[class2].last <= regcm_bound[class].last)) {
14529                                 result |= (1 << class2);
14530                         }
14531                 }
14532         }
14533         return result;
14534 }
14535
14536 static unsigned arch_reg_regcm(struct compile_state *state, int reg)
14537 {
14538         unsigned mask;
14539         int class;
14540         mask = 0;
14541         for(class = 0; class <= LAST_REGC; class++) {
14542                 if ((reg >= regcm_bound[class].first) &&
14543                         (reg <= regcm_bound[class].last)) {
14544                         mask |= (1 << class);
14545                 }
14546         }
14547         if (!mask) {
14548                 internal_error(state, 0, "reg %d not in any class", reg);
14549         }
14550         return mask;
14551 }
14552
14553 static struct reg_info arch_reg_constraint(
14554         struct compile_state *state, struct type *type, const char *constraint)
14555 {
14556         static const struct {
14557                 char class;
14558                 unsigned int mask;
14559                 unsigned int reg;
14560         } constraints[] = {
14561                 { 'r', REGCM_GPR32, REG_UNSET },
14562                 { 'g', REGCM_GPR32, REG_UNSET },
14563                 { 'p', REGCM_GPR32, REG_UNSET },
14564                 { 'q', REGCM_GPR8,  REG_UNSET },
14565                 { 'Q', REGCM_GPR32_8, REG_UNSET },
14566                 { 'x', REGCM_XMM,   REG_UNSET },
14567                 { 'y', REGCM_MMX,   REG_UNSET },
14568                 { 'a', REGCM_GPR32, REG_EAX },
14569                 { 'b', REGCM_GPR32, REG_EBX },
14570                 { 'c', REGCM_GPR32, REG_ECX },
14571                 { 'd', REGCM_GPR32, REG_EDX },
14572                 { 'D', REGCM_GPR32, REG_EDI },
14573                 { 'S', REGCM_GPR32, REG_ESI },
14574                 { '\0', 0, REG_UNSET },
14575         };
14576         unsigned int regcm;
14577         unsigned int mask, reg;
14578         struct reg_info result;
14579         const char *ptr;
14580         regcm = arch_type_to_regcm(state, type);
14581         reg = REG_UNSET;
14582         mask = 0;
14583         for(ptr = constraint; *ptr; ptr++) {
14584                 int i;
14585                 if (*ptr ==  ' ') {
14586                         continue;
14587                 }
14588                 for(i = 0; constraints[i].class != '\0'; i++) {
14589                         if (constraints[i].class == *ptr) {
14590                                 break;
14591                         }
14592                 }
14593                 if (constraints[i].class == '\0') {
14594                         error(state, 0, "invalid register constraint ``%c''", *ptr);
14595                         break;
14596                 }
14597                 if ((constraints[i].mask & regcm) == 0) {
14598                         error(state, 0, "invalid register class %c specified",
14599                                 *ptr);
14600                 }
14601                 mask |= constraints[i].mask;
14602                 if (constraints[i].reg != REG_UNSET) {
14603                         if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
14604                                 error(state, 0, "Only one register may be specified");
14605                         }
14606                         reg = constraints[i].reg;
14607                 }
14608         }
14609         result.reg = reg;
14610         result.regcm = mask;
14611         return result;
14612 }
14613
14614 static struct reg_info arch_reg_clobber(
14615         struct compile_state *state, const char *clobber)
14616 {
14617         struct reg_info result;
14618         if (strcmp(clobber, "memory") == 0) {
14619                 result.reg = REG_UNSET;
14620                 result.regcm = 0;
14621         }
14622         else if (strcmp(clobber, "%eax") == 0) {
14623                 result.reg = REG_EAX;
14624                 result.regcm = REGCM_GPR32;
14625         }
14626         else if (strcmp(clobber, "%ebx") == 0) {
14627                 result.reg = REG_EBX;
14628                 result.regcm = REGCM_GPR32;
14629         }
14630         else if (strcmp(clobber, "%ecx") == 0) {
14631                 result.reg = REG_ECX;
14632                 result.regcm = REGCM_GPR32;
14633         }
14634         else if (strcmp(clobber, "%edx") == 0) {
14635                 result.reg = REG_EDX;
14636                 result.regcm = REGCM_GPR32;
14637         }
14638         else if (strcmp(clobber, "%esi") == 0) {
14639                 result.reg = REG_ESI;
14640                 result.regcm = REGCM_GPR32;
14641         }
14642         else if (strcmp(clobber, "%edi") == 0) {
14643                 result.reg = REG_EDI;
14644                 result.regcm = REGCM_GPR32;
14645         }
14646         else if (strcmp(clobber, "%ebp") == 0) {
14647                 result.reg = REG_EBP;
14648                 result.regcm = REGCM_GPR32;
14649         }
14650         else if (strcmp(clobber, "%esp") == 0) {
14651                 result.reg = REG_ESP;
14652                 result.regcm = REGCM_GPR32;
14653         }
14654         else if (strcmp(clobber, "cc") == 0) {
14655                 result.reg = REG_EFLAGS;
14656                 result.regcm = REGCM_FLAGS;
14657         }
14658         else if ((strncmp(clobber, "xmm", 3) == 0)  &&
14659                 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
14660                 result.reg = REG_XMM0 + octdigval(clobber[3]);
14661                 result.regcm = REGCM_XMM;
14662         }
14663         else if ((strncmp(clobber, "mmx", 3) == 0) &&
14664                 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
14665                 result.reg = REG_MMX0 + octdigval(clobber[3]);
14666                 result.regcm = REGCM_MMX;
14667         }
14668         else {
14669                 error(state, 0, "Invalid register clobber");
14670                 result.reg = REG_UNSET;
14671                 result.regcm = 0;
14672         }
14673         return result;
14674 }
14675
14676 static int do_select_reg(struct compile_state *state, 
14677         char *used, int reg, unsigned classes)
14678 {
14679         unsigned mask;
14680         if (used[reg]) {
14681                 return REG_UNSET;
14682         }
14683         mask = arch_reg_regcm(state, reg);
14684         return (classes & mask) ? reg : REG_UNSET;
14685 }
14686
14687 static int arch_select_free_register(
14688         struct compile_state *state, char *used, int classes)
14689 {
14690         /* Preference: flags, 8bit gprs, 32bit gprs, other 32bit reg
14691          * other types of registers.
14692          */
14693         int i, reg;
14694         reg = REG_UNSET;
14695         for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
14696                 reg = do_select_reg(state, used, i, classes);
14697         }
14698         for(i = REGC_GPR32_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR32_LAST); i++) {
14699                 reg = do_select_reg(state, used, i, classes);
14700         }
14701         for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
14702                 reg = do_select_reg(state, used, i, classes);
14703         }
14704         for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
14705                 reg = do_select_reg(state, used, i, classes);
14706         }
14707         for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
14708                 reg = do_select_reg(state, used, i, classes);
14709         }
14710         for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
14711                 reg = do_select_reg(state, used, i, classes);
14712         }
14713         for(i = REGC_GPR64_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR64_LAST); i++) {
14714                 reg = do_select_reg(state, used, i, classes);
14715         }
14716         return reg;
14717 }
14718
14719
14720 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type) 
14721 {
14722 #warning "FIXME force types smaller (if legal) before I get here"
14723         unsigned avail_mask;
14724         unsigned mask;
14725         mask = 0;
14726         avail_mask = arch_avail_mask(state);
14727         switch(type->type & TYPE_MASK) {
14728         case TYPE_ARRAY:
14729         case TYPE_VOID: 
14730                 mask = 0; 
14731                 break;
14732         case TYPE_CHAR:
14733         case TYPE_UCHAR:
14734                 mask = REGCM_GPR8 | 
14735                         REGCM_GPR16 | REGCM_GPR16_8 | 
14736                         REGCM_GPR32 | REGCM_GPR32_8 |
14737                         REGCM_GPR64 |
14738                         REGCM_MMX | REGCM_XMM |
14739                         REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
14740                 break;
14741         case TYPE_SHORT:
14742         case TYPE_USHORT:
14743                 mask =  REGCM_GPR16 | REGCM_GPR16_8 |
14744                         REGCM_GPR32 | REGCM_GPR32_8 |
14745                         REGCM_GPR64 |
14746                         REGCM_MMX | REGCM_XMM |
14747                         REGCM_IMM32 | REGCM_IMM16;
14748                 break;
14749         case TYPE_INT:
14750         case TYPE_UINT:
14751         case TYPE_LONG:
14752         case TYPE_ULONG:
14753         case TYPE_POINTER:
14754                 mask =  REGCM_GPR32 | REGCM_GPR32_8 |
14755                         REGCM_GPR64 | REGCM_MMX | REGCM_XMM |
14756                         REGCM_IMM32;
14757                 break;
14758         default:
14759                 internal_error(state, 0, "no register class for type");
14760                 break;
14761         }
14762         mask &= avail_mask;
14763         return mask;
14764 }
14765
14766 static int is_imm32(struct triple *imm)
14767 {
14768         return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
14769                 (imm->op == OP_ADDRCONST);
14770         
14771 }
14772 static int is_imm16(struct triple *imm)
14773 {
14774         return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
14775 }
14776 static int is_imm8(struct triple *imm)
14777 {
14778         return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
14779 }
14780
14781 static int get_imm32(struct triple *ins, struct triple **expr)
14782 {
14783         struct triple *imm;
14784         imm = *expr;
14785         while(imm->op == OP_COPY) {
14786                 imm = RHS(imm, 0);
14787         }
14788         if (!is_imm32(imm)) {
14789                 return 0;
14790         }
14791         unuse_triple(*expr, ins);
14792         use_triple(imm, ins);
14793         *expr = imm;
14794         return 1;
14795 }
14796
14797 static int get_imm8(struct triple *ins, struct triple **expr)
14798 {
14799         struct triple *imm;
14800         imm = *expr;
14801         while(imm->op == OP_COPY) {
14802                 imm = RHS(imm, 0);
14803         }
14804         if (!is_imm8(imm)) {
14805                 return 0;
14806         }
14807         unuse_triple(*expr, ins);
14808         use_triple(imm, ins);
14809         *expr = imm;
14810         return 1;
14811 }
14812
14813 #define TEMPLATE_NOP         0
14814 #define TEMPLATE_INTCONST8   1
14815 #define TEMPLATE_INTCONST32  2
14816 #define TEMPLATE_COPY_REG    3
14817 #define TEMPLATE_COPY_IMM32  4
14818 #define TEMPLATE_COPY_IMM16  5
14819 #define TEMPLATE_COPY_IMM8   6
14820 #define TEMPLATE_PHI         7
14821 #define TEMPLATE_STORE8      8
14822 #define TEMPLATE_STORE16     9
14823 #define TEMPLATE_STORE32    10
14824 #define TEMPLATE_LOAD8      11
14825 #define TEMPLATE_LOAD16     12
14826 #define TEMPLATE_LOAD32     13
14827 #define TEMPLATE_BINARY_REG 14
14828 #define TEMPLATE_BINARY_IMM 15
14829 #define TEMPLATE_SL_CL      16
14830 #define TEMPLATE_SL_IMM     17
14831 #define TEMPLATE_UNARY      18
14832 #define TEMPLATE_CMP_REG    19
14833 #define TEMPLATE_CMP_IMM    20
14834 #define TEMPLATE_TEST       21
14835 #define TEMPLATE_SET        22
14836 #define TEMPLATE_JMP        23
14837 #define TEMPLATE_INB_DX     24
14838 #define TEMPLATE_INB_IMM    25
14839 #define TEMPLATE_INW_DX     26
14840 #define TEMPLATE_INW_IMM    27
14841 #define TEMPLATE_INL_DX     28
14842 #define TEMPLATE_INL_IMM    29
14843 #define TEMPLATE_OUTB_DX    30
14844 #define TEMPLATE_OUTB_IMM   31
14845 #define TEMPLATE_OUTW_DX    32
14846 #define TEMPLATE_OUTW_IMM   33
14847 #define TEMPLATE_OUTL_DX    34
14848 #define TEMPLATE_OUTL_IMM   35
14849 #define TEMPLATE_BSF        36
14850 #define TEMPLATE_RDMSR      37
14851 #define TEMPLATE_WRMSR      38
14852 #define LAST_TEMPLATE       TEMPLATE_WRMSR
14853 #if LAST_TEMPLATE >= MAX_TEMPLATES
14854 #error "MAX_TEMPLATES to low"
14855 #endif
14856
14857 #define COPY_REGCM (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8 | REGCM_MMX | REGCM_XMM)
14858 #define COPY32_REGCM (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
14859
14860 static struct ins_template templates[] = {
14861         [TEMPLATE_NOP]      = {},
14862         [TEMPLATE_INTCONST8] = { 
14863                 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
14864         },
14865         [TEMPLATE_INTCONST32] = { 
14866                 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
14867         },
14868         [TEMPLATE_COPY_REG] = {
14869                 .lhs = { [0] = { REG_UNSET, COPY_REGCM } },
14870                 .rhs = { [0] = { REG_UNSET, COPY_REGCM }  },
14871         },
14872         [TEMPLATE_COPY_IMM32] = {
14873                 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
14874                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
14875         },
14876         [TEMPLATE_COPY_IMM16] = {
14877                 .lhs = { [0] = { REG_UNSET, COPY32_REGCM | REGCM_GPR16 } },
14878                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 } },
14879         },
14880         [TEMPLATE_COPY_IMM8] = {
14881                 .lhs = { [0] = { REG_UNSET, COPY_REGCM } },
14882                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
14883         },
14884         [TEMPLATE_PHI] = { 
14885                 .lhs = { [0] = { REG_VIRT0, COPY_REGCM } },
14886                 .rhs = { 
14887                         [ 0] = { REG_VIRT0, COPY_REGCM },
14888                         [ 1] = { REG_VIRT0, COPY_REGCM },
14889                         [ 2] = { REG_VIRT0, COPY_REGCM },
14890                         [ 3] = { REG_VIRT0, COPY_REGCM },
14891                         [ 4] = { REG_VIRT0, COPY_REGCM },
14892                         [ 5] = { REG_VIRT0, COPY_REGCM },
14893                         [ 6] = { REG_VIRT0, COPY_REGCM },
14894                         [ 7] = { REG_VIRT0, COPY_REGCM },
14895                         [ 8] = { REG_VIRT0, COPY_REGCM },
14896                         [ 9] = { REG_VIRT0, COPY_REGCM },
14897                         [10] = { REG_VIRT0, COPY_REGCM },
14898                         [11] = { REG_VIRT0, COPY_REGCM },
14899                         [12] = { REG_VIRT0, COPY_REGCM },
14900                         [13] = { REG_VIRT0, COPY_REGCM },
14901                         [14] = { REG_VIRT0, COPY_REGCM },
14902                         [15] = { REG_VIRT0, COPY_REGCM },
14903                 }, },
14904         [TEMPLATE_STORE8] = {
14905                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14906                 .rhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
14907         },
14908         [TEMPLATE_STORE16] = {
14909                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14910                 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
14911         },
14912         [TEMPLATE_STORE32] = {
14913                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14914                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14915         },
14916         [TEMPLATE_LOAD8] = {
14917                 .lhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
14918                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14919         },
14920         [TEMPLATE_LOAD16] = {
14921                 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
14922                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14923         },
14924         [TEMPLATE_LOAD32] = {
14925                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14926                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14927         },
14928         [TEMPLATE_BINARY_REG] = {
14929                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
14930                 .rhs = { 
14931                         [0] = { REG_VIRT0, REGCM_GPR32 },
14932                         [1] = { REG_UNSET, REGCM_GPR32 },
14933                 },
14934         },
14935         [TEMPLATE_BINARY_IMM] = {
14936                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
14937                 .rhs = { 
14938                         [0] = { REG_VIRT0,    REGCM_GPR32 },
14939                         [1] = { REG_UNNEEDED, REGCM_IMM32 },
14940                 },
14941         },
14942         [TEMPLATE_SL_CL] = {
14943                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
14944                 .rhs = { 
14945                         [0] = { REG_VIRT0, REGCM_GPR32 },
14946                         [1] = { REG_CL, REGCM_GPR8 },
14947                 },
14948         },
14949         [TEMPLATE_SL_IMM] = {
14950                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
14951                 .rhs = { 
14952                         [0] = { REG_VIRT0,    REGCM_GPR32 },
14953                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
14954                 },
14955         },
14956         [TEMPLATE_UNARY] = {
14957                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
14958                 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
14959         },
14960         [TEMPLATE_CMP_REG] = {
14961                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
14962                 .rhs = {
14963                         [0] = { REG_UNSET, REGCM_GPR32 },
14964                         [1] = { REG_UNSET, REGCM_GPR32 },
14965                 },
14966         },
14967         [TEMPLATE_CMP_IMM] = {
14968                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
14969                 .rhs = {
14970                         [0] = { REG_UNSET, REGCM_GPR32 },
14971                         [1] = { REG_UNNEEDED, REGCM_IMM32 },
14972                 },
14973         },
14974         [TEMPLATE_TEST] = {
14975                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
14976                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
14977         },
14978         [TEMPLATE_SET] = {
14979                 .lhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
14980                 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
14981         },
14982         [TEMPLATE_JMP] = {
14983                 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
14984         },
14985         [TEMPLATE_INB_DX] = {
14986                 .lhs = { [0] = { REG_AL,  REGCM_GPR8 } },  
14987                 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
14988         },
14989         [TEMPLATE_INB_IMM] = {
14990                 .lhs = { [0] = { REG_AL,  REGCM_GPR8 } },  
14991                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
14992         },
14993         [TEMPLATE_INW_DX]  = { 
14994                 .lhs = { [0] = { REG_AX,  REGCM_GPR16 } }, 
14995                 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
14996         },
14997         [TEMPLATE_INW_IMM] = { 
14998                 .lhs = { [0] = { REG_AX,  REGCM_GPR16 } }, 
14999                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15000         },
15001         [TEMPLATE_INL_DX]  = {
15002                 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
15003                 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15004         },
15005         [TEMPLATE_INL_IMM] = {
15006                 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
15007                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15008         },
15009         [TEMPLATE_OUTB_DX] = { 
15010                 .rhs = {
15011                         [0] = { REG_AL,  REGCM_GPR8 },
15012                         [1] = { REG_DX, REGCM_GPR16 },
15013                 },
15014         },
15015         [TEMPLATE_OUTB_IMM] = { 
15016                 .rhs = {
15017                         [0] = { REG_AL,  REGCM_GPR8 },  
15018                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
15019                 },
15020         },
15021         [TEMPLATE_OUTW_DX] = { 
15022                 .rhs = {
15023                         [0] = { REG_AX,  REGCM_GPR16 },
15024                         [1] = { REG_DX, REGCM_GPR16 },
15025                 },
15026         },
15027         [TEMPLATE_OUTW_IMM] = {
15028                 .rhs = {
15029                         [0] = { REG_AX,  REGCM_GPR16 }, 
15030                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
15031                 },
15032         },
15033         [TEMPLATE_OUTL_DX] = { 
15034                 .rhs = {
15035                         [0] = { REG_EAX, REGCM_GPR32 },
15036                         [1] = { REG_DX, REGCM_GPR16 },
15037                 },
15038         },
15039         [TEMPLATE_OUTL_IMM] = { 
15040                 .rhs = {
15041                         [0] = { REG_EAX, REGCM_GPR32 }, 
15042                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
15043                 },
15044         },
15045         [TEMPLATE_BSF] = {
15046                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15047                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15048         },
15049         [TEMPLATE_RDMSR] = {
15050                 .lhs = { 
15051                         [0] = { REG_EAX, REGCM_GPR32 },
15052                         [1] = { REG_EDX, REGCM_GPR32 },
15053                 },
15054                 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
15055         },
15056         [TEMPLATE_WRMSR] = {
15057                 .rhs = {
15058                         [0] = { REG_ECX, REGCM_GPR32 },
15059                         [1] = { REG_EAX, REGCM_GPR32 },
15060                         [2] = { REG_EDX, REGCM_GPR32 },
15061                 },
15062         },
15063 };
15064
15065 static void fixup_branches(struct compile_state *state,
15066         struct triple *cmp, struct triple *use, int jmp_op)
15067 {
15068         struct triple_set *entry, *next;
15069         for(entry = use->use; entry; entry = next) {
15070                 next = entry->next;
15071                 if (entry->member->op == OP_COPY) {
15072                         fixup_branches(state, cmp, entry->member, jmp_op);
15073                 }
15074                 else if (entry->member->op == OP_BRANCH) {
15075                         struct triple *branch, *test;
15076                         struct triple *left, *right;
15077                         left = right = 0;
15078                         left = RHS(cmp, 0);
15079                         if (TRIPLE_RHS(cmp->sizes) > 1) {
15080                                 right = RHS(cmp, 1);
15081                         }
15082                         branch = entry->member;
15083                         test = pre_triple(state, branch,
15084                                 cmp->op, cmp->type, left, right);
15085                         test->template_id = TEMPLATE_TEST; 
15086                         if (cmp->op == OP_CMP) {
15087                                 test->template_id = TEMPLATE_CMP_REG;
15088                                 if (get_imm32(test, &RHS(test, 1))) {
15089                                         test->template_id = TEMPLATE_CMP_IMM;
15090                                 }
15091                         }
15092                         use_triple(RHS(test, 0), test);
15093                         use_triple(RHS(test, 1), test);
15094                         unuse_triple(RHS(branch, 0), branch);
15095                         RHS(branch, 0) = test;
15096                         branch->op = jmp_op;
15097                         branch->template_id = TEMPLATE_JMP;
15098                         use_triple(RHS(branch, 0), branch);
15099                 }
15100         }
15101 }
15102
15103 static void bool_cmp(struct compile_state *state, 
15104         struct triple *ins, int cmp_op, int jmp_op, int set_op)
15105 {
15106         struct triple_set *entry, *next;
15107         struct triple *set;
15108
15109         /* Put a barrier up before the cmp which preceeds the
15110          * copy instruction.  If a set actually occurs this gives
15111          * us a chance to move variables in registers out of the way.
15112          */
15113
15114         /* Modify the comparison operator */
15115         ins->op = cmp_op;
15116         ins->template_id = TEMPLATE_TEST;
15117         if (cmp_op == OP_CMP) {
15118                 ins->template_id = TEMPLATE_CMP_REG;
15119                 if (get_imm32(ins, &RHS(ins, 1))) {
15120                         ins->template_id =  TEMPLATE_CMP_IMM;
15121                 }
15122         }
15123         /* Generate the instruction sequence that will transform the
15124          * result of the comparison into a logical value.
15125          */
15126         set = post_triple(state, ins, set_op, ins->type, ins, 0);
15127         use_triple(ins, set);
15128         set->template_id = TEMPLATE_SET;
15129
15130         for(entry = ins->use; entry; entry = next) {
15131                 next = entry->next;
15132                 if (entry->member == set) {
15133                         continue;
15134                 }
15135                 replace_rhs_use(state, ins, set, entry->member);
15136         }
15137         fixup_branches(state, ins, set, jmp_op);
15138 }
15139
15140 static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
15141 {
15142         struct triple *next;
15143         int lhs, i;
15144         lhs = TRIPLE_LHS(ins->sizes);
15145         for(next = ins->next, i = 0; i < lhs; i++, next = next->next) {
15146                 if (next != LHS(ins, i)) {
15147                         internal_error(state, ins, "malformed lhs on %s",
15148                                 tops(ins->op));
15149                 }
15150                 if (next->op != OP_PIECE) {
15151                         internal_error(state, ins, "bad lhs op %s at %d on %s",
15152                                 tops(next->op), i, tops(ins->op));
15153                 }
15154                 if (next->u.cval != i) {
15155                         internal_error(state, ins, "bad u.cval of %d %d expected",
15156                                 next->u.cval, i);
15157                 }
15158         }
15159         return next;
15160 }
15161
15162 struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
15163 {
15164         struct ins_template *template;
15165         struct reg_info result;
15166         int zlhs;
15167         if (ins->op == OP_PIECE) {
15168                 index = ins->u.cval;
15169                 ins = MISC(ins, 0);
15170         }
15171         zlhs = TRIPLE_LHS(ins->sizes);
15172         if (triple_is_def(state, ins)) {
15173                 zlhs = 1;
15174         }
15175         if (index >= zlhs) {
15176                 internal_error(state, ins, "index %d out of range for %s\n",
15177                         index, tops(ins->op));
15178         }
15179         switch(ins->op) {
15180         case OP_ASM:
15181                 template = &ins->u.ainfo->tmpl;
15182                 break;
15183         default:
15184                 if (ins->template_id > LAST_TEMPLATE) {
15185                         internal_error(state, ins, "bad template number %d", 
15186                                 ins->template_id);
15187                 }
15188                 template = &templates[ins->template_id];
15189                 break;
15190         }
15191         result = template->lhs[index];
15192         result.regcm = arch_regcm_normalize(state, result.regcm);
15193         if (result.reg != REG_UNNEEDED) {
15194                 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
15195         }
15196         if (result.regcm == 0) {
15197                 internal_error(state, ins, "lhs %d regcm == 0", index);
15198         }
15199         return result;
15200 }
15201
15202 struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
15203 {
15204         struct reg_info result;
15205         struct ins_template *template;
15206         if ((index > TRIPLE_RHS(ins->sizes)) ||
15207                 (ins->op == OP_PIECE)) {
15208                 internal_error(state, ins, "index %d out of range for %s\n",
15209                         index, tops(ins->op));
15210         }
15211         switch(ins->op) {
15212         case OP_ASM:
15213                 template = &ins->u.ainfo->tmpl;
15214                 break;
15215         default:
15216                 if (ins->template_id > LAST_TEMPLATE) {
15217                         internal_error(state, ins, "bad template number %d", 
15218                                 ins->template_id);
15219                 }
15220                 template = &templates[ins->template_id];
15221                 break;
15222         }
15223         result = template->rhs[index];
15224         result.regcm = arch_regcm_normalize(state, result.regcm);
15225         if (result.regcm == 0) {
15226                 internal_error(state, ins, "rhs %d regcm == 0", index);
15227         }
15228         return result;
15229 }
15230
15231 static struct triple *transform_to_arch_instruction(
15232         struct compile_state *state, struct triple *ins)
15233 {
15234         /* Transform from generic 3 address instructions
15235          * to archtecture specific instructions.
15236          * And apply architecture specific constrains to instructions.
15237          * Copies are inserted to preserve the register flexibility
15238          * of 3 address instructions.
15239          */
15240         struct triple *next;
15241         next = ins->next;
15242         switch(ins->op) {
15243         case OP_INTCONST:
15244                 ins->template_id = TEMPLATE_INTCONST32;
15245                 if (ins->u.cval < 256) {
15246                         ins->template_id = TEMPLATE_INTCONST8;
15247                 }
15248                 break;
15249         case OP_ADDRCONST:
15250                 ins->template_id = TEMPLATE_INTCONST32;
15251                 break;
15252         case OP_NOOP:
15253         case OP_SDECL:
15254         case OP_BLOBCONST:
15255         case OP_LABEL:
15256                 ins->template_id = TEMPLATE_NOP;
15257                 break;
15258         case OP_COPY:
15259                 ins->template_id = TEMPLATE_COPY_REG;
15260                 if (is_imm8(RHS(ins, 0))) {
15261                         ins->template_id = TEMPLATE_COPY_IMM8;
15262                 }
15263                 else if (is_imm16(RHS(ins, 0))) {
15264                         ins->template_id = TEMPLATE_COPY_IMM16;
15265                 }
15266                 else if (is_imm32(RHS(ins, 0))) {
15267                         ins->template_id = TEMPLATE_COPY_IMM32;
15268                 }
15269                 else if (is_const(RHS(ins, 0))) {
15270                         internal_error(state, ins, "bad constant passed to copy");
15271                 }
15272                 break;
15273         case OP_PHI:
15274                 ins->template_id = TEMPLATE_PHI;
15275                 break;
15276         case OP_STORE:
15277                 switch(ins->type->type & TYPE_MASK) {
15278                 case TYPE_CHAR:    case TYPE_UCHAR:
15279                         ins->template_id = TEMPLATE_STORE8;
15280                         break;
15281                 case TYPE_SHORT:   case TYPE_USHORT:
15282                         ins->template_id = TEMPLATE_STORE16;
15283                         break;
15284                 case TYPE_INT:     case TYPE_UINT:
15285                 case TYPE_LONG:    case TYPE_ULONG:
15286                 case TYPE_POINTER:
15287                         ins->template_id = TEMPLATE_STORE32;
15288                         break;
15289                 default:
15290                         internal_error(state, ins, "unknown type in store");
15291                         break;
15292                 }
15293                 break;
15294         case OP_LOAD:
15295                 switch(ins->type->type & TYPE_MASK) {
15296                 case TYPE_CHAR:   case TYPE_UCHAR:
15297                         ins->template_id = TEMPLATE_LOAD8;
15298                         break;
15299                 case TYPE_SHORT:
15300                 case TYPE_USHORT:
15301                         ins->template_id = TEMPLATE_LOAD16;
15302                         break;
15303                 case TYPE_INT:
15304                 case TYPE_UINT:
15305                 case TYPE_LONG:
15306                 case TYPE_ULONG:
15307                 case TYPE_POINTER:
15308                         ins->template_id = TEMPLATE_LOAD32;
15309                         break;
15310                 default:
15311                         internal_error(state, ins, "unknown type in load");
15312                         break;
15313                 }
15314                 break;
15315         case OP_ADD:
15316         case OP_SUB:
15317         case OP_AND:
15318         case OP_XOR:
15319         case OP_OR:
15320         case OP_SMUL:
15321                 ins->template_id = TEMPLATE_BINARY_REG;
15322                 if (get_imm32(ins, &RHS(ins, 1))) {
15323                         ins->template_id = TEMPLATE_BINARY_IMM;
15324                 }
15325                 break;
15326         case OP_SL:
15327         case OP_SSR:
15328         case OP_USR:
15329                 ins->template_id = TEMPLATE_SL_CL;
15330                 if (get_imm8(ins, &RHS(ins, 1))) {
15331                         ins->template_id = TEMPLATE_SL_IMM;
15332                 }
15333                 break;
15334         case OP_INVERT:
15335         case OP_NEG:
15336                 ins->template_id = TEMPLATE_UNARY;
15337                 break;
15338         case OP_EQ: 
15339                 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ); 
15340                 break;
15341         case OP_NOTEQ:
15342                 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
15343                 break;
15344         case OP_SLESS:
15345                 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
15346                 break;
15347         case OP_ULESS:
15348                 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
15349                 break;
15350         case OP_SMORE:
15351                 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
15352                 break;
15353         case OP_UMORE:
15354                 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
15355                 break;
15356         case OP_SLESSEQ:
15357                 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
15358                 break;
15359         case OP_ULESSEQ:
15360                 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
15361                 break;
15362         case OP_SMOREEQ:
15363                 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
15364                 break;
15365         case OP_UMOREEQ:
15366                 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
15367                 break;
15368         case OP_LTRUE:
15369                 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
15370                 break;
15371         case OP_LFALSE:
15372                 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
15373                 break;
15374         case OP_BRANCH:
15375                 if (TRIPLE_RHS(ins->sizes) > 0) {
15376                         internal_error(state, ins, "bad branch test");
15377                 }
15378                 ins->op = OP_JMP;
15379                 ins->template_id = TEMPLATE_NOP;
15380                 break;
15381         case OP_INB:
15382         case OP_INW:
15383         case OP_INL:
15384                 switch(ins->op) {
15385                 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
15386                 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
15387                 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
15388                 }
15389                 if (get_imm8(ins, &RHS(ins, 0))) {
15390                         ins->template_id += 1;
15391                 }
15392                 break;
15393         case OP_OUTB:
15394         case OP_OUTW:
15395         case OP_OUTL:
15396                 switch(ins->op) {
15397                 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
15398                 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
15399                 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
15400                 }
15401                 if (get_imm8(ins, &RHS(ins, 1))) {
15402                         ins->template_id += 1;
15403                 }
15404                 break;
15405         case OP_BSF:
15406         case OP_BSR:
15407                 ins->template_id = TEMPLATE_BSF;
15408                 break;
15409         case OP_RDMSR:
15410                 ins->template_id = TEMPLATE_RDMSR;
15411                 next = after_lhs(state, ins);
15412                 break;
15413         case OP_WRMSR:
15414                 ins->template_id = TEMPLATE_WRMSR;
15415                 break;
15416         case OP_HLT:
15417                 ins->template_id = TEMPLATE_NOP;
15418                 break;
15419         case OP_ASM:
15420                 ins->template_id = TEMPLATE_NOP;
15421                 next = after_lhs(state, ins);
15422                 break;
15423                 /* Already transformed instructions */
15424         case OP_TEST:
15425                 ins->template_id = TEMPLATE_TEST;
15426                 break;
15427         case OP_CMP:
15428                 ins->template_id = TEMPLATE_CMP_REG;
15429                 if (get_imm32(ins, &RHS(ins, 1))) {
15430                         ins->template_id = TEMPLATE_CMP_IMM;
15431                 }
15432                 break;
15433         case OP_JMP_EQ:      case OP_JMP_NOTEQ:
15434         case OP_JMP_SLESS:   case OP_JMP_ULESS:
15435         case OP_JMP_SMORE:   case OP_JMP_UMORE:
15436         case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
15437         case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
15438                 ins->template_id = TEMPLATE_JMP;
15439                 break;
15440         case OP_SET_EQ:      case OP_SET_NOTEQ:
15441         case OP_SET_SLESS:   case OP_SET_ULESS:
15442         case OP_SET_SMORE:   case OP_SET_UMORE:
15443         case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
15444         case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
15445                 ins->template_id = TEMPLATE_SET;
15446                 break;
15447                 /* Unhandled instructions */
15448         case OP_PIECE:
15449         default:
15450                 internal_error(state, ins, "unhandled ins: %d %s\n",
15451                         ins->op, tops(ins->op));
15452                 break;
15453         }
15454         return next;
15455 }
15456
15457 static void generate_local_labels(struct compile_state *state)
15458 {
15459         struct triple *first, *label;
15460         int label_counter;
15461         label_counter = 0;
15462         first = RHS(state->main_function, 0);
15463         label = first;
15464         do {
15465                 if ((label->op == OP_LABEL) || 
15466                         (label->op == OP_SDECL)) {
15467                         if (label->use) {
15468                                 label->u.cval = ++label_counter;
15469                         } else {
15470                                 label->u.cval = 0;
15471                         }
15472                         
15473                 }
15474                 label = label->next;
15475         } while(label != first);
15476 }
15477
15478 static int check_reg(struct compile_state *state, 
15479         struct triple *triple, int classes)
15480 {
15481         unsigned mask;
15482         int reg;
15483         reg = ID_REG(triple->id);
15484         if (reg == REG_UNSET) {
15485                 internal_error(state, triple, "register not set");
15486         }
15487         mask = arch_reg_regcm(state, reg);
15488         if (!(classes & mask)) {
15489                 internal_error(state, triple, "reg %d in wrong class",
15490                         reg);
15491         }
15492         return reg;
15493 }
15494
15495 static const char *arch_reg_str(int reg)
15496 {
15497         static const char *regs[] = {
15498                 "%bad_register",
15499                 "%bad_register2",
15500                 "%eflags",
15501                 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
15502                 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
15503                 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
15504                 "%edx:%eax",
15505                 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
15506                 "%xmm0", "%xmm1", "%xmm2", "%xmm3", 
15507                 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
15508         };
15509         if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
15510                 reg = 0;
15511         }
15512         return regs[reg];
15513 }
15514
15515
15516 static const char *reg(struct compile_state *state, struct triple *triple,
15517         int classes)
15518 {
15519         int reg;
15520         reg = check_reg(state, triple, classes);
15521         return arch_reg_str(reg);
15522 }
15523
15524 const char *type_suffix(struct compile_state *state, struct type *type)
15525 {
15526         const char *suffix;
15527         switch(size_of(state, type)) {
15528         case 1: suffix = "b"; break;
15529         case 2: suffix = "w"; break;
15530         case 4: suffix = "l"; break;
15531         default:
15532                 internal_error(state, 0, "unknown suffix");
15533                 suffix = 0;
15534                 break;
15535         }
15536         return suffix;
15537 }
15538
15539 static void print_const_val(
15540         struct compile_state *state, struct triple *ins, FILE *fp)
15541 {
15542         switch(ins->op) {
15543         case OP_INTCONST:
15544                 fprintf(fp, " $%ld ", 
15545                         (long_t)(ins->u.cval));
15546                 break;
15547         case OP_ADDRCONST:
15548                 fprintf(fp, " $L%s%lu+%lu ",
15549                         state->label_prefix, 
15550                         MISC(ins, 0)->u.cval,
15551                         ins->u.cval);
15552                 break;
15553         default:
15554                 internal_error(state, ins, "unknown constant type");
15555                 break;
15556         }
15557 }
15558
15559 static void print_binary_op(struct compile_state *state,
15560         const char *op, struct triple *ins, FILE *fp) 
15561 {
15562         unsigned mask;
15563         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
15564         if (RHS(ins, 0)->id != ins->id) {
15565                 internal_error(state, ins, "invalid register assignment");
15566         }
15567         if (is_const(RHS(ins, 1))) {
15568                 fprintf(fp, "\t%s ", op);
15569                 print_const_val(state, RHS(ins, 1), fp);
15570                 fprintf(fp, ", %s\n",
15571                         reg(state, RHS(ins, 0), mask));
15572         }
15573         else {
15574                 unsigned lmask, rmask;
15575                 int lreg, rreg;
15576                 lreg = check_reg(state, RHS(ins, 0), mask);
15577                 rreg = check_reg(state, RHS(ins, 1), mask);
15578                 lmask = arch_reg_regcm(state, lreg);
15579                 rmask = arch_reg_regcm(state, rreg);
15580                 mask = lmask & rmask;
15581                 fprintf(fp, "\t%s %s, %s\n",
15582                         op,
15583                         reg(state, RHS(ins, 1), mask),
15584                         reg(state, RHS(ins, 0), mask));
15585         }
15586 }
15587 static void print_unary_op(struct compile_state *state, 
15588         const char *op, struct triple *ins, FILE *fp)
15589 {
15590         unsigned mask;
15591         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
15592         fprintf(fp, "\t%s %s\n",
15593                 op,
15594                 reg(state, RHS(ins, 0), mask));
15595 }
15596
15597 static void print_op_shift(struct compile_state *state,
15598         const char *op, struct triple *ins, FILE *fp)
15599 {
15600         unsigned mask;
15601         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
15602         if (RHS(ins, 0)->id != ins->id) {
15603                 internal_error(state, ins, "invalid register assignment");
15604         }
15605         if (is_const(RHS(ins, 1))) {
15606                 fprintf(fp, "\t%s ", op);
15607                 print_const_val(state, RHS(ins, 1), fp);
15608                 fprintf(fp, ", %s\n",
15609                         reg(state, RHS(ins, 0), mask));
15610         }
15611         else {
15612                 fprintf(fp, "\t%s %s, %s\n",
15613                         op,
15614                         reg(state, RHS(ins, 1), REGCM_GPR8),
15615                         reg(state, RHS(ins, 0), mask));
15616         }
15617 }
15618
15619 static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
15620 {
15621         const char *op;
15622         int mask;
15623         int dreg;
15624         mask = 0;
15625         switch(ins->op) {
15626         case OP_INB: op = "inb", mask = REGCM_GPR8; break;
15627         case OP_INW: op = "inw", mask = REGCM_GPR16; break;
15628         case OP_INL: op = "inl", mask = REGCM_GPR32; break;
15629         default:
15630                 internal_error(state, ins, "not an in operation");
15631                 op = 0;
15632                 break;
15633         }
15634         dreg = check_reg(state, ins, mask);
15635         if (!reg_is_reg(state, dreg, REG_EAX)) {
15636                 internal_error(state, ins, "dst != %%eax");
15637         }
15638         if (is_const(RHS(ins, 0))) {
15639                 fprintf(fp, "\t%s ", op);
15640                 print_const_val(state, RHS(ins, 0), fp);
15641                 fprintf(fp, ", %s\n",
15642                         reg(state, ins, mask));
15643         }
15644         else {
15645                 int addr_reg;
15646                 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
15647                 if (!reg_is_reg(state, addr_reg, REG_DX)) {
15648                         internal_error(state, ins, "src != %%dx");
15649                 }
15650                 fprintf(fp, "\t%s %s, %s\n",
15651                         op, 
15652                         reg(state, RHS(ins, 0), REGCM_GPR16),
15653                         reg(state, ins, mask));
15654         }
15655 }
15656
15657 static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
15658 {
15659         const char *op;
15660         int mask;
15661         int lreg;
15662         mask = 0;
15663         switch(ins->op) {
15664         case OP_OUTB: op = "outb", mask = REGCM_GPR8; break;
15665         case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
15666         case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
15667         default:
15668                 internal_error(state, ins, "not an out operation");
15669                 op = 0;
15670                 break;
15671         }
15672         lreg = check_reg(state, RHS(ins, 0), mask);
15673         if (!reg_is_reg(state, lreg, REG_EAX)) {
15674                 internal_error(state, ins, "src != %%eax");
15675         }
15676         if (is_const(RHS(ins, 1))) {
15677                 fprintf(fp, "\t%s %s,", 
15678                         op, reg(state, RHS(ins, 0), mask));
15679                 print_const_val(state, RHS(ins, 1), fp);
15680                 fprintf(fp, "\n");
15681         }
15682         else {
15683                 int addr_reg;
15684                 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
15685                 if (!reg_is_reg(state, addr_reg, REG_DX)) {
15686                         internal_error(state, ins, "dst != %%dx");
15687                 }
15688                 fprintf(fp, "\t%s %s, %s\n",
15689                         op, 
15690                         reg(state, RHS(ins, 0), mask),
15691                         reg(state, RHS(ins, 1), REGCM_GPR16));
15692         }
15693 }
15694
15695 static void print_op_move(struct compile_state *state,
15696         struct triple *ins, FILE *fp)
15697 {
15698         /* op_move is complex because there are many types
15699          * of registers we can move between.
15700          * Because OP_COPY will be introduced in arbitrary locations
15701          * OP_COPY must not affect flags.
15702          */
15703         int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
15704         struct triple *dst, *src;
15705         if (ins->op == OP_COPY) {
15706                 src = RHS(ins, 0);
15707                 dst = ins;
15708         }
15709         else if (ins->op == OP_WRITE) {
15710                 dst = LHS(ins, 0);
15711                 src = RHS(ins, 0);
15712         }
15713         else {
15714                 internal_error(state, ins, "unknown move operation");
15715                 src = dst = 0;
15716         }
15717         if (!is_const(src)) {
15718                 int src_reg, dst_reg;
15719                 int src_regcm, dst_regcm;
15720                 src_reg = ID_REG(src->id);
15721                 dst_reg   = ID_REG(dst->id);
15722                 src_regcm = arch_reg_regcm(state, src_reg);
15723                 dst_regcm   = arch_reg_regcm(state, dst_reg);
15724                 /* If the class is the same just move the register */
15725                 if (src_regcm & dst_regcm & 
15726                         (REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32)) {
15727                         if ((src_reg != dst_reg) || !omit_copy) {
15728                                 fprintf(fp, "\tmov %s, %s\n",
15729                                         reg(state, src, src_regcm),
15730                                         reg(state, dst, dst_regcm));
15731                         }
15732                 }
15733                 /* Move 32bit to 16bit */
15734                 else if ((src_regcm & REGCM_GPR32) &&
15735                         (dst_regcm & REGCM_GPR16)) {
15736                         src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
15737                         if ((src_reg != dst_reg) || !omit_copy) {
15738                                 fprintf(fp, "\tmovw %s, %s\n",
15739                                         arch_reg_str(src_reg), 
15740                                         arch_reg_str(dst_reg));
15741                         }
15742                 }
15743                 /* Move 32bit to 8bit */
15744                 else if ((src_regcm & REGCM_GPR32_8) &&
15745                         (dst_regcm & REGCM_GPR8))
15746                 {
15747                         src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
15748                         if ((src_reg != dst_reg) || !omit_copy) {
15749                                 fprintf(fp, "\tmovb %s, %s\n",
15750                                         arch_reg_str(src_reg),
15751                                         arch_reg_str(dst_reg));
15752                         }
15753                 }
15754                 /* Move 16bit to 8bit */
15755                 else if ((src_regcm & REGCM_GPR16_8) &&
15756                         (dst_regcm & REGCM_GPR8))
15757                 {
15758                         src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
15759                         if ((src_reg != dst_reg) || !omit_copy) {
15760                                 fprintf(fp, "\tmovb %s, %s\n",
15761                                         arch_reg_str(src_reg),
15762                                         arch_reg_str(dst_reg));
15763                         }
15764                 }
15765                 /* Move 8/16bit to 16/32bit */
15766                 else if ((src_regcm & (REGCM_GPR8 | REGCM_GPR16)) && 
15767                         (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
15768                         const char *op;
15769                         op = is_signed(src->type)? "movsx": "movzx";
15770                         fprintf(fp, "\t%s %s, %s\n",
15771                                 op,
15772                                 reg(state, src, src_regcm),
15773                                 reg(state, dst, dst_regcm));
15774                 }
15775                 /* Move between sse registers */
15776                 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
15777                         if ((src_reg != dst_reg) || !omit_copy) {
15778                                 fprintf(fp, "\tmovdqa %s, %s\n",
15779                                         reg(state, src, src_regcm),
15780                                         reg(state, dst, dst_regcm));
15781                         }
15782                 }
15783                 /* Move between mmx registers or mmx & sse  registers */
15784                 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
15785                         (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
15786                         if ((src_reg != dst_reg) || !omit_copy) {
15787                                 fprintf(fp, "\tmovq %s, %s\n",
15788                                         reg(state, src, src_regcm),
15789                                         reg(state, dst, dst_regcm));
15790                         }
15791                 }
15792                 /* Move between 32bit gprs & mmx/sse registers */
15793                 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
15794                         (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
15795                         fprintf(fp, "\tmovd %s, %s\n",
15796                                 reg(state, src, src_regcm),
15797                                 reg(state, dst, dst_regcm));
15798                 }
15799 #if X86_4_8BIT_GPRS
15800                 /* Move from 8bit gprs to  mmx/sse registers */
15801                 else if ((src_regcm & REGCM_GPR8) && (src_reg <= REG_DL) &&
15802                         (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
15803                         const char *op;
15804                         int mid_reg;
15805                         op = is_signed(src->type)? "movsx":"movzx";
15806                         mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
15807                         fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
15808                                 op,
15809                                 reg(state, src, src_regcm),
15810                                 arch_reg_str(mid_reg),
15811                                 arch_reg_str(mid_reg),
15812                                 reg(state, dst, dst_regcm));
15813                 }
15814                 /* Move from mmx/sse registers and 8bit gprs */
15815                 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
15816                         (dst_regcm & REGCM_GPR8) && (dst_reg <= REG_DL)) {
15817                         int mid_reg;
15818                         mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
15819                         fprintf(fp, "\tmovd %s, %s\n",
15820                                 reg(state, src, src_regcm),
15821                                 arch_reg_str(mid_reg));
15822                 }
15823                 /* Move from 32bit gprs to 16bit gprs */
15824                 else if ((src_regcm & REGCM_GPR32) &&
15825                         (dst_regcm & REGCM_GPR16)) {
15826                         dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
15827                         if ((src_reg != dst_reg) || !omit_copy) {
15828                                 fprintf(fp, "\tmov %s, %s\n",
15829                                         arch_reg_str(src_reg),
15830                                         arch_reg_str(dst_reg));
15831                         }
15832                 }
15833                 /* Move from 32bit gprs to 8bit gprs */
15834                 else if ((src_regcm & REGCM_GPR32) &&
15835                         (dst_regcm & REGCM_GPR8)) {
15836                         dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
15837                         if ((src_reg != dst_reg) || !omit_copy) {
15838                                 fprintf(fp, "\tmov %s, %s\n",
15839                                         arch_reg_str(src_reg),
15840                                         arch_reg_str(dst_reg));
15841                         }
15842                 }
15843                 /* Move from 16bit gprs to 8bit gprs */
15844                 else if ((src_regcm & REGCM_GPR16) &&
15845                         (dst_regcm & REGCM_GPR8)) {
15846                         dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
15847                         if ((src_reg != dst_reg) || !omit_copy) {
15848                                 fprintf(fp, "\tmov %s, %s\n",
15849                                         arch_reg_str(src_reg),
15850                                         arch_reg_str(dst_reg));
15851                         }
15852                 }
15853 #endif /* X86_4_8BIT_GPRS */
15854                 else {
15855                         internal_error(state, ins, "unknown copy type");
15856                 }
15857         }
15858         else {
15859                 fprintf(fp, "\tmov ");
15860                 print_const_val(state, src, fp);
15861                 fprintf(fp, ", %s\n",
15862                         reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8));
15863         }
15864 }
15865
15866 static void print_op_load(struct compile_state *state,
15867         struct triple *ins, FILE *fp)
15868 {
15869         struct triple *dst, *src;
15870         dst = ins;
15871         src = RHS(ins, 0);
15872         if (is_const(src) || is_const(dst)) {
15873                 internal_error(state, ins, "unknown load operation");
15874         }
15875         fprintf(fp, "\tmov (%s), %s\n",
15876                 reg(state, src, REGCM_GPR32),
15877                 reg(state, dst, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32));
15878 }
15879
15880
15881 static void print_op_store(struct compile_state *state,
15882         struct triple *ins, FILE *fp)
15883 {
15884         struct triple *dst, *src;
15885         dst = LHS(ins, 0);
15886         src = RHS(ins, 0);
15887         if (is_const(src) && (src->op == OP_INTCONST)) {
15888                 long_t value;
15889                 value = (long_t)(src->u.cval);
15890                 fprintf(fp, "\tmov%s $%ld, (%s)\n",
15891                         type_suffix(state, src->type),
15892                         value,
15893                         reg(state, dst, REGCM_GPR32));
15894         }
15895         else if (is_const(dst) && (dst->op == OP_INTCONST)) {
15896                 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
15897                         type_suffix(state, src->type),
15898                         reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
15899                         dst->u.cval);
15900         }
15901         else {
15902                 if (is_const(src) || is_const(dst)) {
15903                         internal_error(state, ins, "unknown store operation");
15904                 }
15905                 fprintf(fp, "\tmov%s %s, (%s)\n",
15906                         type_suffix(state, src->type),
15907                         reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
15908                         reg(state, dst, REGCM_GPR32));
15909         }
15910         
15911         
15912 }
15913
15914 static void print_op_smul(struct compile_state *state,
15915         struct triple *ins, FILE *fp)
15916 {
15917         if (!is_const(RHS(ins, 1))) {
15918                 fprintf(fp, "\timul %s, %s\n",
15919                         reg(state, RHS(ins, 1), REGCM_GPR32),
15920                         reg(state, RHS(ins, 0), REGCM_GPR32));
15921         }
15922         else {
15923                 fprintf(fp, "\timul ");
15924                 print_const_val(state, RHS(ins, 1), fp);
15925                 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
15926         }
15927 }
15928
15929 static void print_op_cmp(struct compile_state *state,
15930         struct triple *ins, FILE *fp)
15931 {
15932         unsigned mask;
15933         int dreg;
15934         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
15935         dreg = check_reg(state, ins, REGCM_FLAGS);
15936         if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
15937                 internal_error(state, ins, "bad dest register for cmp");
15938         }
15939         if (is_const(RHS(ins, 1))) {
15940                 fprintf(fp, "\tcmp ");
15941                 print_const_val(state, RHS(ins, 1), fp);
15942                 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
15943         }
15944         else {
15945                 unsigned lmask, rmask;
15946                 int lreg, rreg;
15947                 lreg = check_reg(state, RHS(ins, 0), mask);
15948                 rreg = check_reg(state, RHS(ins, 1), mask);
15949                 lmask = arch_reg_regcm(state, lreg);
15950                 rmask = arch_reg_regcm(state, rreg);
15951                 mask = lmask & rmask;
15952                 fprintf(fp, "\tcmp %s, %s\n",
15953                         reg(state, RHS(ins, 1), mask),
15954                         reg(state, RHS(ins, 0), mask));
15955         }
15956 }
15957
15958 static void print_op_test(struct compile_state *state,
15959         struct triple *ins, FILE *fp)
15960 {
15961         unsigned mask;
15962         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
15963         fprintf(fp, "\ttest %s, %s\n",
15964                 reg(state, RHS(ins, 0), mask),
15965                 reg(state, RHS(ins, 0), mask));
15966 }
15967
15968 static void print_op_branch(struct compile_state *state,
15969         struct triple *branch, FILE *fp)
15970 {
15971         const char *bop = "j";
15972         if (branch->op == OP_JMP) {
15973                 if (TRIPLE_RHS(branch->sizes) != 0) {
15974                         internal_error(state, branch, "jmp with condition?");
15975                 }
15976                 bop = "jmp";
15977         }
15978         else {
15979                 struct triple *ptr;
15980                 if (TRIPLE_RHS(branch->sizes) != 1) {
15981                         internal_error(state, branch, "jmpcc without condition?");
15982                 }
15983                 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
15984                 if ((RHS(branch, 0)->op != OP_CMP) &&
15985                         (RHS(branch, 0)->op != OP_TEST)) {
15986                         internal_error(state, branch, "bad branch test");
15987                 }
15988 #warning "FIXME I have observed instructions between the test and branch instructions"
15989                 ptr = RHS(branch, 0);
15990                 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
15991                         if (ptr->op != OP_COPY) {
15992                                 internal_error(state, branch, "branch does not follow test");
15993                         }
15994                 }
15995                 switch(branch->op) {
15996                 case OP_JMP_EQ:       bop = "jz";  break;
15997                 case OP_JMP_NOTEQ:    bop = "jnz"; break;
15998                 case OP_JMP_SLESS:    bop = "jl";  break;
15999                 case OP_JMP_ULESS:    bop = "jb";  break;
16000                 case OP_JMP_SMORE:    bop = "jg";  break;
16001                 case OP_JMP_UMORE:    bop = "ja";  break;
16002                 case OP_JMP_SLESSEQ:  bop = "jle"; break;
16003                 case OP_JMP_ULESSEQ:  bop = "jbe"; break;
16004                 case OP_JMP_SMOREEQ:  bop = "jge"; break;
16005                 case OP_JMP_UMOREEQ:  bop = "jae"; break;
16006                 default:
16007                         internal_error(state, branch, "Invalid branch op");
16008                         break;
16009                 }
16010                 
16011         }
16012         fprintf(fp, "\t%s L%s%lu\n",
16013                 bop, 
16014                 state->label_prefix,
16015                 TARG(branch, 0)->u.cval);
16016 }
16017
16018 static void print_op_set(struct compile_state *state,
16019         struct triple *set, FILE *fp)
16020 {
16021         const char *sop = "set";
16022         if (TRIPLE_RHS(set->sizes) != 1) {
16023                 internal_error(state, set, "setcc without condition?");
16024         }
16025         check_reg(state, RHS(set, 0), REGCM_FLAGS);
16026         if ((RHS(set, 0)->op != OP_CMP) &&
16027                 (RHS(set, 0)->op != OP_TEST)) {
16028                 internal_error(state, set, "bad set test");
16029         }
16030         if (RHS(set, 0)->next != set) {
16031                 internal_error(state, set, "set does not follow test");
16032         }
16033         switch(set->op) {
16034         case OP_SET_EQ:       sop = "setz";  break;
16035         case OP_SET_NOTEQ:    sop = "setnz"; break;
16036         case OP_SET_SLESS:    sop = "setl";  break;
16037         case OP_SET_ULESS:    sop = "setb";  break;
16038         case OP_SET_SMORE:    sop = "setg";  break;
16039         case OP_SET_UMORE:    sop = "seta";  break;
16040         case OP_SET_SLESSEQ:  sop = "setle"; break;
16041         case OP_SET_ULESSEQ:  sop = "setbe"; break;
16042         case OP_SET_SMOREEQ:  sop = "setge"; break;
16043         case OP_SET_UMOREEQ:  sop = "setae"; break;
16044         default:
16045                 internal_error(state, set, "Invalid set op");
16046                 break;
16047         }
16048         fprintf(fp, "\t%s %s\n",
16049                 sop, reg(state, set, REGCM_GPR8));
16050 }
16051
16052 static void print_op_bit_scan(struct compile_state *state, 
16053         struct triple *ins, FILE *fp) 
16054 {
16055         const char *op;
16056         switch(ins->op) {
16057         case OP_BSF: op = "bsf"; break;
16058         case OP_BSR: op = "bsr"; break;
16059         default: 
16060                 internal_error(state, ins, "unknown bit scan");
16061                 op = 0;
16062                 break;
16063         }
16064         fprintf(fp, 
16065                 "\t%s %s, %s\n"
16066                 "\tjnz 1f\n"
16067                 "\tmovl $-1, %s\n"
16068                 "1:\n",
16069                 op,
16070                 reg(state, RHS(ins, 0), REGCM_GPR32),
16071                 reg(state, ins, REGCM_GPR32),
16072                 reg(state, ins, REGCM_GPR32));
16073 }
16074
16075 static void print_const(struct compile_state *state,
16076         struct triple *ins, FILE *fp)
16077 {
16078         switch(ins->op) {
16079         case OP_INTCONST:
16080                 switch(ins->type->type & TYPE_MASK) {
16081                 case TYPE_CHAR:
16082                 case TYPE_UCHAR:
16083                         fprintf(fp, ".byte 0x%02lx\n", ins->u.cval);
16084                         break;
16085                 case TYPE_SHORT:
16086                 case TYPE_USHORT:
16087                         fprintf(fp, ".short 0x%04lx\n", ins->u.cval);
16088                         break;
16089                 case TYPE_INT:
16090                 case TYPE_UINT:
16091                 case TYPE_LONG:
16092                 case TYPE_ULONG:
16093                         fprintf(fp, ".int %lu\n", ins->u.cval);
16094                         break;
16095                 default:
16096                         internal_error(state, ins, "Unknown constant type");
16097                 }
16098                 break;
16099         case OP_BLOBCONST:
16100         {
16101                 unsigned char *blob;
16102                 size_t size, i;
16103                 size = size_of(state, ins->type);
16104                 blob = ins->u.blob;
16105                 for(i = 0; i < size; i++) {
16106                         fprintf(fp, ".byte 0x%02x\n",
16107                                 blob[i]);
16108                 }
16109                 break;
16110         }
16111         default:
16112                 internal_error(state, ins, "Unknown constant type");
16113                 break;
16114         }
16115 }
16116
16117 #define TEXT_SECTION ".rom.text"
16118 #define DATA_SECTION ".rom.data"
16119
16120 static void print_sdecl(struct compile_state *state,
16121         struct triple *ins, FILE *fp)
16122 {
16123         fprintf(fp, ".section \"" DATA_SECTION "\"\n");
16124         fprintf(fp, ".balign %d\n", align_of(state, ins->type));
16125         fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
16126         print_const(state, MISC(ins, 0), fp);
16127         fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
16128                 
16129 }
16130
16131 static void print_instruction(struct compile_state *state,
16132         struct triple *ins, FILE *fp)
16133 {
16134         /* Assumption: after I have exted the register allocator
16135          * everything is in a valid register. 
16136          */
16137         switch(ins->op) {
16138         case OP_ASM:
16139                 print_op_asm(state, ins, fp);
16140                 break;
16141         case OP_ADD:    print_binary_op(state, "add", ins, fp); break;
16142         case OP_SUB:    print_binary_op(state, "sub", ins, fp); break;
16143         case OP_AND:    print_binary_op(state, "and", ins, fp); break;
16144         case OP_XOR:    print_binary_op(state, "xor", ins, fp); break;
16145         case OP_OR:     print_binary_op(state, "or",  ins, fp); break;
16146         case OP_SL:     print_op_shift(state, "shl", ins, fp); break;
16147         case OP_USR:    print_op_shift(state, "shr", ins, fp); break;
16148         case OP_SSR:    print_op_shift(state, "sar", ins, fp); break;
16149         case OP_POS:    break;
16150         case OP_NEG:    print_unary_op(state, "neg", ins, fp); break;
16151         case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
16152         case OP_INTCONST:
16153         case OP_ADDRCONST:
16154         case OP_BLOBCONST:
16155                 /* Don't generate anything here for constants */
16156         case OP_PHI:
16157                 /* Don't generate anything for variable declarations. */
16158                 break;
16159         case OP_SDECL:
16160                 print_sdecl(state, ins, fp);
16161                 break;
16162         case OP_WRITE: 
16163         case OP_COPY:   
16164                 print_op_move(state, ins, fp);
16165                 break;
16166         case OP_LOAD:
16167                 print_op_load(state, ins, fp);
16168                 break;
16169         case OP_STORE:
16170                 print_op_store(state, ins, fp);
16171                 break;
16172         case OP_SMUL:
16173                 print_op_smul(state, ins, fp);
16174                 break;
16175         case OP_CMP:    print_op_cmp(state, ins, fp); break;
16176         case OP_TEST:   print_op_test(state, ins, fp); break;
16177         case OP_JMP:
16178         case OP_JMP_EQ:      case OP_JMP_NOTEQ:
16179         case OP_JMP_SLESS:   case OP_JMP_ULESS:
16180         case OP_JMP_SMORE:   case OP_JMP_UMORE:
16181         case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
16182         case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
16183                 print_op_branch(state, ins, fp);
16184                 break;
16185         case OP_SET_EQ:      case OP_SET_NOTEQ:
16186         case OP_SET_SLESS:   case OP_SET_ULESS:
16187         case OP_SET_SMORE:   case OP_SET_UMORE:
16188         case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
16189         case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
16190                 print_op_set(state, ins, fp);
16191                 break;
16192         case OP_INB:  case OP_INW:  case OP_INL:
16193                 print_op_in(state, ins, fp); 
16194                 break;
16195         case OP_OUTB: case OP_OUTW: case OP_OUTL:
16196                 print_op_out(state, ins, fp); 
16197                 break;
16198         case OP_BSF:
16199         case OP_BSR:
16200                 print_op_bit_scan(state, ins, fp);
16201                 break;
16202         case OP_RDMSR:
16203                 after_lhs(state, ins);
16204                 fprintf(fp, "\trdmsr\n");
16205                 break;
16206         case OP_WRMSR:
16207                 fprintf(fp, "\twrmsr\n");
16208                 break;
16209         case OP_HLT:
16210                 fprintf(fp, "\thlt\n");
16211                 break;
16212         case OP_LABEL:
16213                 if (!ins->use) {
16214                         return;
16215                 }
16216                 fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
16217                 break;
16218                 /* Ignore OP_PIECE */
16219         case OP_PIECE:
16220                 break;
16221                 /* Operations I am not yet certain how to handle */
16222         case OP_UMUL:
16223         case OP_SDIV: case OP_UDIV:
16224         case OP_SMOD: case OP_UMOD:
16225                 /* Operations that should never get here */
16226         case OP_LTRUE:   case OP_LFALSE:  case OP_EQ:      case OP_NOTEQ:
16227         case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
16228         case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
16229         default:
16230                 internal_error(state, ins, "unknown op: %d %s",
16231                         ins->op, tops(ins->op));
16232                 break;
16233         }
16234 }
16235
16236 static void print_instructions(struct compile_state *state)
16237 {
16238         struct triple *first, *ins;
16239         int print_location;
16240         int last_line;
16241         int last_col;
16242         const char *last_filename;
16243         FILE *fp;
16244         print_location = 1;
16245         last_line = -1;
16246         last_col  = -1;
16247         last_filename = 0;
16248         fp = state->output;
16249         fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
16250         first = RHS(state->main_function, 0);
16251         ins = first;
16252         do {
16253                 if (print_location &&
16254                         ((last_filename != ins->filename) ||
16255                                 (last_line != ins->line) ||
16256                                 (last_col  != ins->col))) {
16257                         fprintf(fp, "\t/* %s:%d */\n",
16258                                 ins->filename, ins->line);
16259                         last_filename = ins->filename;
16260                         last_line = ins->line;
16261                         last_col  = ins->col;
16262                 }
16263
16264                 print_instruction(state, ins, fp);
16265                 ins = ins->next;
16266         } while(ins != first);
16267         
16268 }
16269 static void generate_code(struct compile_state *state)
16270 {
16271         generate_local_labels(state);
16272         print_instructions(state);
16273         
16274 }
16275
16276 static void print_tokens(struct compile_state *state)
16277 {
16278         struct token *tk;
16279         tk = &state->token[0];
16280         do {
16281 #if 1
16282                 token(state, 0);
16283 #else
16284                 next_token(state, 0);
16285 #endif
16286                 loc(stdout, state, 0);
16287                 printf("%s <- `%s'\n",
16288                         tokens[tk->tok],
16289                         tk->ident ? tk->ident->name :
16290                         tk->str_len ? tk->val.str : "");
16291                 
16292         } while(tk->tok != TOK_EOF);
16293 }
16294
16295 static void compile(const char *filename, const char *ofilename, 
16296         int cpu, int debug, int opt, const char *label_prefix)
16297 {
16298         int i;
16299         struct compile_state state;
16300         memset(&state, 0, sizeof(state));
16301         state.file = 0;
16302         for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
16303                 memset(&state.token[i], 0, sizeof(state.token[i]));
16304                 state.token[i].tok = -1;
16305         }
16306         /* Remember the debug settings */
16307         state.cpu      = cpu;
16308         state.debug    = debug;
16309         state.optimize = opt;
16310         /* Remember the output filename */
16311         state.ofilename = ofilename;
16312         state.output    = fopen(state.ofilename, "w");
16313         if (!state.output) {
16314                 error(&state, 0, "Cannot open output file %s\n",
16315                         ofilename);
16316         }
16317         /* Remember the label prefix */
16318         state.label_prefix = label_prefix;
16319         /* Prep the preprocessor */
16320         state.if_depth = 0;
16321         state.if_value = 0;
16322         /* register the C keywords */
16323         register_keywords(&state);
16324         /* register the keywords the macro preprocessor knows */
16325         register_macro_keywords(&state);
16326         /* Memorize where some special keywords are. */
16327         state.i_continue = lookup(&state, "continue", 8);
16328         state.i_break    = lookup(&state, "break", 5);
16329         /* Enter the globl definition scope */
16330         start_scope(&state);
16331         register_builtins(&state);
16332         compile_file(&state, filename, 1);
16333 #if 0
16334         print_tokens(&state);
16335 #endif  
16336         decls(&state);
16337         /* Exit the global definition scope */
16338         end_scope(&state);
16339
16340         /* Now that basic compilation has happened 
16341          * optimize the intermediate code 
16342          */
16343         optimize(&state);
16344
16345         generate_code(&state);
16346         if (state.debug) {
16347                 fprintf(stderr, "done\n");
16348         }
16349 }
16350
16351 static void version(void)
16352 {
16353         printf("romcc " VERSION " released " RELEASE_DATE "\n");
16354 }
16355
16356 static void usage(void)
16357 {
16358         version();
16359         printf(
16360                 "Usage: romcc <source>.c\n"
16361                 "Compile a C source file without using ram\n"
16362         );
16363 }
16364
16365 static void arg_error(char *fmt, ...)
16366 {
16367         va_list args;
16368         va_start(args, fmt);
16369         vfprintf(stderr, fmt, args);
16370         va_end(args);
16371         usage();
16372         exit(1);
16373 }
16374
16375 int main(int argc, char **argv)
16376 {
16377         const char *filename;
16378         const char *ofilename;
16379         const char *label_prefix;
16380         int cpu;
16381         int last_argc;
16382         int debug;
16383         int optimize;
16384         cpu = CPU_DEFAULT;
16385         label_prefix = "";
16386         ofilename = "auto.inc";
16387         optimize = 0;
16388         debug = 0;
16389         last_argc = -1;
16390         while((argc > 1) && (argc != last_argc)) {
16391                 last_argc = argc;
16392                 if (strncmp(argv[1], "--debug=", 8) == 0) {
16393                         debug = atoi(argv[1] + 8);
16394                         argv++;
16395                         argc--;
16396                 }
16397                 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
16398                         label_prefix= argv[1] + 15;
16399                         argv++;
16400                         argc--;
16401                 }
16402                 else if ((strcmp(argv[1],"-O") == 0) ||
16403                         (strcmp(argv[1], "-O1") == 0)) {
16404                         optimize = 1;
16405                         argv++;
16406                         argc--;
16407                 }
16408                 else if (strcmp(argv[1],"-O2") == 0) {
16409                         optimize = 2;
16410                         argv++;
16411                         argc--;
16412                 }
16413                 else if ((strcmp(argv[1], "-o") == 0) && (argc > 2)) {
16414                         ofilename = argv[2];
16415                         argv += 2;
16416                         argc -= 2;
16417                 }
16418                 else if (strncmp(argv[1], "-mcpu=", 6) == 0) {
16419                         cpu = arch_encode_cpu(argv[1] + 6);
16420                         if (cpu == BAD_CPU) {
16421                                 arg_error("Invalid cpu specified: %s\n",
16422                                         argv[1] + 6);
16423                         }
16424                         argv++;
16425                         argc--;
16426                 }
16427         }
16428         if (argc != 2) {
16429                 arg_error("Wrong argument count %d\n", argc);
16430         }
16431         filename = argv[1];
16432         compile(filename, ofilename, cpu, debug, optimize, label_prefix);
16433
16434         return 0;
16435 }