- Implement goto support
[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 <limits.h>
13
14 #define DEBUG_ERROR_MESSAGES 0
15 #define DEBUG_COLOR_GRAPH 0
16 #define DEBUG_SCC 0
17 #define DEBUG_CONSISTENCY 2
18
19 #warning "FIXME boundary cases with small types in larger registers"
20 #warning "FIXME give clear error messages about unused variables"
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         int report_line;
219         const char *report_name;
220         const char *report_dir;
221 };
222 struct hash_entry;
223 struct token {
224         int tok;
225         struct hash_entry *ident;
226         int str_len;
227         union {
228                 ulong_t integer;
229                 const char *str;
230         } val;
231 };
232
233 /* I have two classes of types:
234  * Operational types.
235  * Logical types.  (The type the C standard says the operation is of)
236  *
237  * The operational types are:
238  * chars
239  * shorts
240  * ints
241  * longs
242  *
243  * floats
244  * doubles
245  * long doubles
246  *
247  * pointer
248  */
249
250
251 /* Machine model.
252  * No memory is useable by the compiler.
253  * There is no floating point support.
254  * All operations take place in general purpose registers.
255  * There is one type of general purpose register.
256  * Unsigned longs are stored in that general purpose register.
257  */
258
259 /* Operations on general purpose registers.
260  */
261
262 #define OP_SMUL       0
263 #define OP_UMUL       1
264 #define OP_SDIV       2
265 #define OP_UDIV       3
266 #define OP_SMOD       4
267 #define OP_UMOD       5
268 #define OP_ADD        6
269 #define OP_SUB        7
270 #define OP_SL         8
271 #define OP_USR        9
272 #define OP_SSR       10 
273 #define OP_AND       11 
274 #define OP_XOR       12
275 #define OP_OR        13
276 #define OP_POS       14 /* Dummy positive operator don't use it */
277 #define OP_NEG       15
278 #define OP_INVERT    16
279                      
280 #define OP_EQ        20
281 #define OP_NOTEQ     21
282 #define OP_SLESS     22
283 #define OP_ULESS     23
284 #define OP_SMORE     24
285 #define OP_UMORE     25
286 #define OP_SLESSEQ   26
287 #define OP_ULESSEQ   27
288 #define OP_SMOREEQ   28
289 #define OP_UMOREEQ   29
290                      
291 #define OP_LFALSE    30  /* Test if the expression is logically false */
292 #define OP_LTRUE     31  /* Test if the expression is logcially true */
293
294 #define OP_LOAD      32
295 #define OP_STORE     33
296
297 #define OP_NOOP      34
298
299 #define OP_MIN_CONST 50
300 #define OP_MAX_CONST 59
301 #define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
302 #define OP_INTCONST  50
303 #define OP_BLOBCONST 51
304 /* For OP_BLOBCONST ->type holds the layout and size
305  * information.  u.blob holds a pointer to the raw binary
306  * data for the constant initializer.
307  */
308 #define OP_ADDRCONST 52
309 /* For OP_ADDRCONST ->type holds the type.
310  * MISC(0) holds the reference to the static variable.
311  * ->u.cval holds an offset from that value.
312  */
313
314 #define OP_WRITE     60 
315 /* OP_WRITE moves one pseudo register to another.
316  * LHS(0) holds the destination pseudo register, which must be an OP_DECL.
317  * RHS(0) holds the psuedo to move.
318  */
319
320 #define OP_READ      61
321 /* OP_READ reads the value of a variable and makes
322  * it available for the pseudo operation.
323  * Useful for things like def-use chains.
324  * RHS(0) holds points to the triple to read from.
325  */
326 #define OP_COPY      62
327 /* OP_COPY makes a copy of the psedo register or constant in RHS(0).
328  */
329 #define OP_PIECE     63
330 /* OP_PIECE returns one piece of a instruction that returns a structure.
331  * MISC(0) is the instruction
332  * u.cval is the LHS piece of the instruction to return.
333  */
334 #define OP_ASM       64
335 /* OP_ASM holds a sequence of assembly instructions, the result
336  * of a C asm directive.
337  * RHS(x) holds input value x to the assembly sequence.
338  * LHS(x) holds the output value x from the assembly sequence.
339  * u.blob holds the string of assembly instructions.
340  */
341
342 #define OP_DEREF     65
343 /* OP_DEREF generates an lvalue from a pointer.
344  * RHS(0) holds the pointer value.
345  * OP_DEREF serves as a place holder to indicate all necessary
346  * checks have been done to indicate a value is an lvalue.
347  */
348 #define OP_DOT       66
349 /* OP_DOT references a submember of a structure lvalue.
350  * RHS(0) holds the lvalue.
351  * ->u.field holds the name of the field we want.
352  *
353  * Not seen outside of expressions.
354  */
355 #define OP_VAL       67
356 /* OP_VAL returns the value of a subexpression of the current expression.
357  * Useful for operators that have side effects.
358  * RHS(0) holds the expression.
359  * MISC(0) holds the subexpression of RHS(0) that is the
360  * value of the expression.
361  *
362  * Not seen outside of expressions.
363  */
364 #define OP_LAND      68
365 /* OP_LAND performs a C logical and between RHS(0) and RHS(1).
366  * Not seen outside of expressions.
367  */
368 #define OP_LOR       69
369 /* OP_LOR performs a C logical or between RHS(0) and RHS(1).
370  * Not seen outside of expressions.
371  */
372 #define OP_COND      70
373 /* OP_CODE performas a C ? : operation. 
374  * RHS(0) holds the test.
375  * RHS(1) holds the expression to evaluate if the test returns true.
376  * RHS(2) holds the expression to evaluate if the test returns false.
377  * Not seen outside of expressions.
378  */
379 #define OP_COMMA     71
380 /* OP_COMMA performacs a C comma operation.
381  * That is RHS(0) is evaluated, then RHS(1)
382  * and the value of RHS(1) is returned.
383  * Not seen outside of expressions.
384  */
385
386 #define OP_CALL      72
387 /* OP_CALL performs a procedure call. 
388  * MISC(0) holds a pointer to the OP_LIST of a function
389  * RHS(x) holds argument x of a function
390  * 
391  * Currently not seen outside of expressions.
392  */
393 #define OP_VAL_VEC   74
394 /* OP_VAL_VEC is an array of triples that are either variable
395  * or values for a structure or an array.
396  * RHS(x) holds element x of the vector.
397  * triple->type->elements holds the size of the vector.
398  */
399
400 /* statements */
401 #define OP_LIST      80
402 /* OP_LIST Holds a list of statements, and a result value.
403  * RHS(0) holds the list of statements.
404  * MISC(0) holds the value of the statements.
405  */
406
407 #define OP_BRANCH    81 /* branch */
408 /* For branch instructions
409  * TARG(0) holds the branch target.
410  * RHS(0) if present holds the branch condition.
411  * ->next holds where to branch to if the branch is not taken.
412  * The branch target can only be a decl...
413  */
414
415 #define OP_LABEL     83
416 /* OP_LABEL is a triple that establishes an target for branches.
417  * ->use is the list of all branches that use this label.
418  */
419
420 #define OP_ADECL     84 
421 /* OP_DECL is a triple that establishes an lvalue for assignments.
422  * ->use is a list of statements that use the variable.
423  */
424
425 #define OP_SDECL     85
426 /* OP_SDECL is a triple that establishes a variable of static
427  * storage duration.
428  * ->use is a list of statements that use the variable.
429  * MISC(0) holds the initializer expression.
430  */
431
432
433 #define OP_PHI       86
434 /* OP_PHI is a triple used in SSA form code.  
435  * It is used when multiple code paths merge and a variable needs
436  * a single assignment from any of those code paths.
437  * The operation is a cross between OP_DECL and OP_WRITE, which
438  * is what OP_PHI is geneared from.
439  * 
440  * RHS(x) points to the value from code path x
441  * The number of RHS entries is the number of control paths into the block
442  * in which OP_PHI resides.  The elements of the array point to point
443  * to the variables OP_PHI is derived from.
444  *
445  * MISC(0) holds a pointer to the orginal OP_DECL node.
446  */
447
448 /* Architecture specific instructions */
449 #define OP_CMP         100
450 #define OP_TEST        101
451 #define OP_SET_EQ      102
452 #define OP_SET_NOTEQ   103
453 #define OP_SET_SLESS   104
454 #define OP_SET_ULESS   105
455 #define OP_SET_SMORE   106
456 #define OP_SET_UMORE   107
457 #define OP_SET_SLESSEQ 108
458 #define OP_SET_ULESSEQ 109
459 #define OP_SET_SMOREEQ 110
460 #define OP_SET_UMOREEQ 111
461
462 #define OP_JMP         112
463 #define OP_JMP_EQ      113
464 #define OP_JMP_NOTEQ   114
465 #define OP_JMP_SLESS   115
466 #define OP_JMP_ULESS   116
467 #define OP_JMP_SMORE   117
468 #define OP_JMP_UMORE   118
469 #define OP_JMP_SLESSEQ 119
470 #define OP_JMP_ULESSEQ 120
471 #define OP_JMP_SMOREEQ 121
472 #define OP_JMP_UMOREEQ 122
473
474 /* Builtin operators that it is just simpler to use the compiler for */
475 #define OP_INB         130
476 #define OP_INW         131
477 #define OP_INL         132
478 #define OP_OUTB        133
479 #define OP_OUTW        134
480 #define OP_OUTL        135
481 #define OP_BSF         136
482 #define OP_BSR         137
483 #define OP_RDMSR       138
484 #define OP_WRMSR       139
485 #define OP_HLT         140
486
487 struct op_info {
488         const char *name;
489         unsigned flags;
490 #define PURE   1
491 #define IMPURE 2
492 #define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
493 #define DEF    4
494 #define BLOCK  8 /* Triple stores the current block */
495         unsigned char lhs, rhs, misc, targ;
496 };
497
498 #define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \
499         .name = (NAME), \
500         .flags = (FLAGS), \
501         .lhs = (LHS), \
502         .rhs = (RHS), \
503         .misc = (MISC), \
504         .targ = (TARG), \
505          }
506 static const struct op_info table_ops[] = {
507 [OP_SMUL       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smul"),
508 [OP_UMUL       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umul"),
509 [OP_SDIV       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sdiv"),
510 [OP_UDIV       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "udiv"),
511 [OP_SMOD       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smod"),
512 [OP_UMOD       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umod"),
513 [OP_ADD        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "add"),
514 [OP_SUB        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sub"),
515 [OP_SL         ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sl"),
516 [OP_USR        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "usr"),
517 [OP_SSR        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "ssr"),
518 [OP_AND        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "and"),
519 [OP_XOR        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "xor"),
520 [OP_OR         ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "or"),
521 [OP_POS        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "pos"),
522 [OP_NEG        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "neg"),
523 [OP_INVERT     ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "invert"),
524
525 [OP_EQ         ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "eq"),
526 [OP_NOTEQ      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "noteq"),
527 [OP_SLESS      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sless"),
528 [OP_ULESS      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "uless"),
529 [OP_SMORE      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smore"),
530 [OP_UMORE      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umore"),
531 [OP_SLESSEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "slesseq"),
532 [OP_ULESSEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "ulesseq"),
533 [OP_SMOREEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smoreeq"),
534 [OP_UMOREEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umoreeq"),
535 [OP_LFALSE     ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "lfalse"),
536 [OP_LTRUE      ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "ltrue"),
537
538 [OP_LOAD       ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "load"),
539 [OP_STORE      ] = OP( 1,  1, 0, 0, IMPURE | BLOCK , "store"),
540
541 [OP_NOOP       ] = OP( 0,  0, 0, 0, PURE | BLOCK, "noop"),
542
543 [OP_INTCONST   ] = OP( 0,  0, 0, 0, PURE | DEF, "intconst"),
544 [OP_BLOBCONST  ] = OP( 0,  0, 0, 0, PURE, "blobconst"),
545 [OP_ADDRCONST  ] = OP( 0,  0, 1, 0, PURE | DEF, "addrconst"),
546
547 [OP_WRITE      ] = OP( 1,  1, 0, 0, PURE | BLOCK, "write"),
548 [OP_READ       ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "read"),
549 [OP_COPY       ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "copy"),
550 [OP_PIECE      ] = OP( 0,  0, 1, 0, PURE | DEF, "piece"),
551 [OP_ASM        ] = OP(-1, -1, 0, 0, IMPURE, "asm"),
552 [OP_DEREF      ] = OP( 0,  1, 0, 0, 0 | DEF | BLOCK, "deref"), 
553 [OP_DOT        ] = OP( 0,  1, 0, 0, 0 | DEF | BLOCK, "dot"),
554
555 [OP_VAL        ] = OP( 0,  1, 1, 0, 0 | DEF | BLOCK, "val"),
556 [OP_LAND       ] = OP( 0,  2, 0, 0, 0 | DEF | BLOCK, "land"),
557 [OP_LOR        ] = OP( 0,  2, 0, 0, 0 | DEF | BLOCK, "lor"),
558 [OP_COND       ] = OP( 0,  3, 0, 0, 0 | DEF | BLOCK, "cond"),
559 [OP_COMMA      ] = OP( 0,  2, 0, 0, 0 | DEF | BLOCK, "comma"),
560 /* Call is special most it can stand in for anything so it depends on context */
561 [OP_CALL       ] = OP(-1, -1, 1, 0, 0 | BLOCK, "call"),
562 /* The sizes of OP_CALL and OP_VAL_VEC depend upon context */
563 [OP_VAL_VEC    ] = OP( 0, -1, 0, 0, 0 | BLOCK, "valvec"),
564
565 [OP_LIST       ] = OP( 0,  1, 1, 0, 0 | DEF, "list"),
566 /* The number of targets for OP_BRANCH depends on context */
567 [OP_BRANCH     ] = OP( 0, -1, 0, 1, PURE | BLOCK, "branch"),
568 [OP_LABEL      ] = OP( 0,  0, 0, 0, PURE | BLOCK, "label"),
569 [OP_ADECL      ] = OP( 0,  0, 0, 0, PURE | BLOCK, "adecl"),
570 [OP_SDECL      ] = OP( 0,  0, 1, 0, PURE | BLOCK, "sdecl"),
571 /* The number of RHS elements of OP_PHI depend upon context */
572 [OP_PHI        ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
573
574 [OP_CMP        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK, "cmp"),
575 [OP_TEST       ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "test"),
576 [OP_SET_EQ     ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_eq"),
577 [OP_SET_NOTEQ  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_noteq"),
578 [OP_SET_SLESS  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_sless"),
579 [OP_SET_ULESS  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_uless"),
580 [OP_SET_SMORE  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_smore"),
581 [OP_SET_UMORE  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_umore"),
582 [OP_SET_SLESSEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_slesseq"),
583 [OP_SET_ULESSEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_ulesseq"),
584 [OP_SET_SMOREEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_smoreq"),
585 [OP_SET_UMOREEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_umoreq"),
586 [OP_JMP        ] = OP( 0,  0, 0, 1, PURE | BLOCK, "jmp"),
587 [OP_JMP_EQ     ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_eq"),
588 [OP_JMP_NOTEQ  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_noteq"),
589 [OP_JMP_SLESS  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_sless"),
590 [OP_JMP_ULESS  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_uless"),
591 [OP_JMP_SMORE  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_smore"),
592 [OP_JMP_UMORE  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_umore"),
593 [OP_JMP_SLESSEQ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_slesseq"),
594 [OP_JMP_ULESSEQ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_ulesseq"),
595 [OP_JMP_SMOREEQ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_smoreq"),
596 [OP_JMP_UMOREEQ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_umoreq"),
597
598 [OP_INB        ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "__inb"),
599 [OP_INW        ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "__inw"),
600 [OP_INL        ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "__inl"),
601 [OP_OUTB       ] = OP( 0,  2, 0, 0, IMPURE| BLOCK, "__outb"),
602 [OP_OUTW       ] = OP( 0,  2, 0, 0, IMPURE| BLOCK, "__outw"),
603 [OP_OUTL       ] = OP( 0,  2, 0, 0, IMPURE| BLOCK, "__outl"),
604 [OP_BSF        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "__bsf"),
605 [OP_BSR        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "__bsr"),
606 [OP_RDMSR      ] = OP( 2,  1, 0, 0, IMPURE | BLOCK, "__rdmsr"),
607 [OP_WRMSR      ] = OP( 0,  3, 0, 0, IMPURE | BLOCK, "__wrmsr"),
608 [OP_HLT        ] = OP( 0,  0, 0, 0, IMPURE | BLOCK, "__hlt"),
609 };
610 #undef OP
611 #define OP_MAX      (sizeof(table_ops)/sizeof(table_ops[0]))
612
613 static const char *tops(int index) 
614 {
615         static const char unknown[] = "unknown op";
616         if (index < 0) {
617                 return unknown;
618         }
619         if (index > OP_MAX) {
620                 return unknown;
621         }
622         return table_ops[index].name;
623 }
624
625 struct asm_info;
626 struct triple;
627 struct block;
628 struct triple_set {
629         struct triple_set *next;
630         struct triple *member;
631 };
632
633 #define MAX_LHS  15
634 #define MAX_RHS  15
635 #define MAX_MISC 15
636 #define MAX_TARG 15
637
638 struct occurance {
639         int count;
640         const char *filename;
641         const char *function;
642         int line;
643         int col;
644         struct occurance *parent;
645 };
646 struct triple {
647         struct triple *next, *prev;
648         struct triple_set *use;
649         struct type *type;
650         unsigned char op;
651         unsigned char template_id;
652         unsigned short sizes;
653 #define TRIPLE_LHS(SIZES)  (((SIZES) >>  0) & 0x0f)
654 #define TRIPLE_RHS(SIZES)  (((SIZES) >>  4) & 0x0f)
655 #define TRIPLE_MISC(SIZES) (((SIZES) >>  8) & 0x0f)
656 #define TRIPLE_TARG(SIZES) (((SIZES) >> 12) & 0x0f)
657 #define TRIPLE_SIZE(SIZES) \
658         ((((SIZES) >> 0) & 0x0f) + \
659         (((SIZES) >>  4) & 0x0f) + \
660         (((SIZES) >>  8) & 0x0f) + \
661         (((SIZES) >> 12) & 0x0f))
662 #define TRIPLE_SIZES(LHS, RHS, MISC, TARG) \
663         ((((LHS) & 0x0f) <<  0) | \
664         (((RHS) & 0x0f)  <<  4) | \
665         (((MISC) & 0x0f) <<  8) | \
666         (((TARG) & 0x0f) << 12))
667 #define TRIPLE_LHS_OFF(SIZES)  (0)
668 #define TRIPLE_RHS_OFF(SIZES)  (TRIPLE_LHS_OFF(SIZES) + TRIPLE_LHS(SIZES))
669 #define TRIPLE_MISC_OFF(SIZES) (TRIPLE_RHS_OFF(SIZES) + TRIPLE_RHS(SIZES))
670 #define TRIPLE_TARG_OFF(SIZES) (TRIPLE_MISC_OFF(SIZES) + TRIPLE_MISC(SIZES))
671 #define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF((PTR)->sizes) + (INDEX)])
672 #define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF((PTR)->sizes) + (INDEX)])
673 #define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF((PTR)->sizes) + (INDEX)])
674 #define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF((PTR)->sizes) + (INDEX)])
675         unsigned id; /* A scratch value and finally the register */
676 #define TRIPLE_FLAG_FLATTENED   (1 << 31)
677 #define TRIPLE_FLAG_PRE_SPLIT   (1 << 30)
678 #define TRIPLE_FLAG_POST_SPLIT  (1 << 29)
679         struct occurance *occurance;
680         union {
681                 ulong_t cval;
682                 struct block  *block;
683                 void *blob;
684                 struct hash_entry *field;
685                 struct asm_info *ainfo;
686         } u;
687         struct triple *param[2];
688 };
689
690 struct reg_info {
691         unsigned reg;
692         unsigned regcm;
693 };
694 struct ins_template {
695         struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
696 };
697
698 struct asm_info {
699         struct ins_template tmpl;
700         char *str;
701 };
702
703 struct block_set {
704         struct block_set *next;
705         struct block *member;
706 };
707 struct block {
708         struct block *work_next;
709         struct block *left, *right;
710         struct triple *first, *last;
711         int users;
712         struct block_set *use;
713         struct block_set *idominates;
714         struct block_set *domfrontier;
715         struct block *idom;
716         struct block_set *ipdominates;
717         struct block_set *ipdomfrontier;
718         struct block *ipdom;
719         int vertex;
720         
721 };
722
723 struct symbol {
724         struct symbol *next;
725         struct hash_entry *ident;
726         struct triple *def;
727         struct type *type;
728         int scope_depth;
729 };
730
731 struct macro {
732         struct hash_entry *ident;
733         char *buf;
734         int buf_len;
735 };
736
737 struct hash_entry {
738         struct hash_entry *next;
739         const char *name;
740         int name_len;
741         int tok;
742         struct macro *sym_define;
743         struct symbol *sym_label;
744         struct symbol *sym_struct;
745         struct symbol *sym_ident;
746 };
747
748 #define HASH_TABLE_SIZE 2048
749
750 struct compile_state {
751         const char *label_prefix;
752         const char *ofilename;
753         FILE *output;
754         struct triple *vars;
755         struct file_state *file;
756         struct occurance *last_occurance;
757         const char *function;
758         struct token token[4];
759         struct hash_entry *hash_table[HASH_TABLE_SIZE];
760         struct hash_entry *i_continue;
761         struct hash_entry *i_break;
762         int scope_depth;
763         int if_depth, if_value;
764         int macro_line;
765         struct file_state *macro_file;
766         struct triple *main_function;
767         struct block *first_block, *last_block;
768         int last_vertex;
769         int cpu;
770         int debug;
771         int optimize;
772 };
773
774 /* visibility global/local */
775 /* static/auto duration */
776 /* typedef, register, inline */
777 #define STOR_SHIFT         0
778 #define STOR_MASK     0x000f
779 /* Visibility */
780 #define STOR_GLOBAL   0x0001
781 /* Duration */
782 #define STOR_PERM     0x0002
783 /* Storage specifiers */
784 #define STOR_AUTO     0x0000
785 #define STOR_STATIC   0x0002
786 #define STOR_EXTERN   0x0003
787 #define STOR_REGISTER 0x0004
788 #define STOR_TYPEDEF  0x0008
789 #define STOR_INLINE   0x000c
790
791 #define QUAL_SHIFT         4
792 #define QUAL_MASK     0x0070
793 #define QUAL_NONE     0x0000
794 #define QUAL_CONST    0x0010
795 #define QUAL_VOLATILE 0x0020
796 #define QUAL_RESTRICT 0x0040
797
798 #define TYPE_SHIFT         8
799 #define TYPE_MASK     0x1f00
800 #define TYPE_INTEGER(TYPE)    (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG))
801 #define TYPE_ARITHMETIC(TYPE) (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE))
802 #define TYPE_UNSIGNED(TYPE)   ((TYPE) & 0x0100)
803 #define TYPE_SIGNED(TYPE)     (!TYPE_UNSIGNED(TYPE))
804 #define TYPE_MKUNSIGNED(TYPE) ((TYPE) | 0x0100)
805 #define TYPE_RANK(TYPE)       ((TYPE) & ~0x0100)
806 #define TYPE_PTR(TYPE)        (((TYPE) & TYPE_MASK) == TYPE_POINTER)
807 #define TYPE_DEFAULT  0x0000
808 #define TYPE_VOID     0x0100
809 #define TYPE_CHAR     0x0200
810 #define TYPE_UCHAR    0x0300
811 #define TYPE_SHORT    0x0400
812 #define TYPE_USHORT   0x0500
813 #define TYPE_INT      0x0600
814 #define TYPE_UINT     0x0700
815 #define TYPE_LONG     0x0800
816 #define TYPE_ULONG    0x0900
817 #define TYPE_LLONG    0x0a00 /* long long */
818 #define TYPE_ULLONG   0x0b00
819 #define TYPE_FLOAT    0x0c00
820 #define TYPE_DOUBLE   0x0d00
821 #define TYPE_LDOUBLE  0x0e00 /* long double */
822 #define TYPE_STRUCT   0x1000
823 #define TYPE_ENUM     0x1100
824 #define TYPE_POINTER  0x1200 
825 /* For TYPE_POINTER:
826  * type->left holds the type pointed to.
827  */
828 #define TYPE_FUNCTION 0x1300 
829 /* For TYPE_FUNCTION:
830  * type->left holds the return type.
831  * type->right holds the...
832  */
833 #define TYPE_PRODUCT  0x1400
834 /* TYPE_PRODUCT is a basic building block when defining structures
835  * type->left holds the type that appears first in memory.
836  * type->right holds the type that appears next in memory.
837  */
838 #define TYPE_OVERLAP  0x1500
839 /* TYPE_OVERLAP is a basic building block when defining unions
840  * type->left and type->right holds to types that overlap
841  * each other in memory.
842  */
843 #define TYPE_ARRAY    0x1600
844 /* TYPE_ARRAY is a basic building block when definitng arrays.
845  * type->left holds the type we are an array of.
846  * type-> holds the number of elements.
847  */
848
849 #define ELEMENT_COUNT_UNSPECIFIED (~0UL)
850
851 struct type {
852         unsigned int type;
853         struct type *left, *right;
854         ulong_t elements;
855         struct hash_entry *field_ident;
856         struct hash_entry *type_ident;
857 };
858
859 #define MAX_REGISTERS      75
860 #define MAX_REG_EQUIVS     16
861 #define REGISTER_BITS      16
862 #define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
863 #define TEMPLATE_BITS      6
864 #define MAX_TEMPLATES      (1<<TEMPLATE_BITS)
865 #define MAX_REGC           12
866 #define REG_UNSET          0
867 #define REG_UNNEEDED       1
868 #define REG_VIRT0          (MAX_REGISTERS + 0)
869 #define REG_VIRT1          (MAX_REGISTERS + 1)
870 #define REG_VIRT2          (MAX_REGISTERS + 2)
871 #define REG_VIRT3          (MAX_REGISTERS + 3)
872 #define REG_VIRT4          (MAX_REGISTERS + 4)
873 #define REG_VIRT5          (MAX_REGISTERS + 5)
874 #define REG_VIRT6          (MAX_REGISTERS + 5)
875 #define REG_VIRT7          (MAX_REGISTERS + 5)
876 #define REG_VIRT8          (MAX_REGISTERS + 5)
877 #define REG_VIRT9          (MAX_REGISTERS + 5)
878
879 /* Provision for 8 register classes */
880 #define REG_SHIFT  0
881 #define REGC_SHIFT REGISTER_BITS
882 #define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
883 #define REG_MASK (MAX_VIRT_REGISTERS -1)
884 #define ID_REG(ID)              ((ID) & REG_MASK)
885 #define SET_REG(ID, REG)        ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
886 #define ID_REGCM(ID)            (((ID) & REGC_MASK) >> REGC_SHIFT)
887 #define SET_REGCM(ID, REGCM)    ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
888 #define SET_INFO(ID, INFO)      ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
889                 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
890
891 static unsigned arch_reg_regcm(struct compile_state *state, int reg);
892 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
893 static void arch_reg_equivs(
894         struct compile_state *state, unsigned *equiv, int reg);
895 static int arch_select_free_register(
896         struct compile_state *state, char *used, int classes);
897 static unsigned arch_regc_size(struct compile_state *state, int class);
898 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
899 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
900 static const char *arch_reg_str(int reg);
901 static struct reg_info arch_reg_constraint(
902         struct compile_state *state, struct type *type, const char *constraint);
903 static struct reg_info arch_reg_clobber(
904         struct compile_state *state, const char *clobber);
905 static struct reg_info arch_reg_lhs(struct compile_state *state, 
906         struct triple *ins, int index);
907 static struct reg_info arch_reg_rhs(struct compile_state *state, 
908         struct triple *ins, int index);
909 static struct triple *transform_to_arch_instruction(
910         struct compile_state *state, struct triple *ins);
911
912
913
914 #define DEBUG_ABORT_ON_ERROR    0x0001
915 #define DEBUG_INTERMEDIATE_CODE 0x0002
916 #define DEBUG_CONTROL_FLOW      0x0004
917 #define DEBUG_BASIC_BLOCKS      0x0008
918 #define DEBUG_FDOMINATORS       0x0010
919 #define DEBUG_RDOMINATORS       0x0020
920 #define DEBUG_TRIPLES           0x0040
921 #define DEBUG_INTERFERENCE      0x0080
922 #define DEBUG_ARCH_CODE         0x0100
923 #define DEBUG_CODE_ELIMINATION  0x0200
924 #define DEBUG_INSERTED_COPIES   0x0400
925
926 #define GLOBAL_SCOPE_DEPTH   1
927 #define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
928
929 static void compile_file(struct compile_state *old_state, const char *filename, int local);
930
931 static void do_cleanup(struct compile_state *state)
932 {
933         if (state->output) {
934                 fclose(state->output);
935                 unlink(state->ofilename);
936         }
937 }
938
939 static int get_col(struct file_state *file)
940 {
941         int col;
942         char *ptr, *end;
943         ptr = file->line_start;
944         end = file->pos;
945         for(col = 0; ptr < end; ptr++) {
946                 if (*ptr != '\t') {
947                         col++;
948                 } 
949                 else {
950                         col = (col & ~7) + 8;
951                 }
952         }
953         return col;
954 }
955
956 static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
957 {
958         int col;
959         if (triple) {
960                 fprintf(fp, "%s:%d.%d: ", 
961                         triple->occurance->filename, 
962                         triple->occurance->line, 
963                         triple->occurance->col);
964                 return;
965         }
966         if (!state->file) {
967                 return;
968         }
969         col = get_col(state->file);
970         fprintf(fp, "%s:%d.%d: ", 
971                 state->file->report_name, state->file->report_line, col);
972 }
973
974 static void __internal_error(struct compile_state *state, struct triple *ptr, 
975         char *fmt, ...)
976 {
977         va_list args;
978         va_start(args, fmt);
979         loc(stderr, state, ptr);
980         if (ptr) {
981                 fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
982         }
983         fprintf(stderr, "Internal compiler error: ");
984         vfprintf(stderr, fmt, args);
985         fprintf(stderr, "\n");
986         va_end(args);
987         do_cleanup(state);
988         abort();
989 }
990
991
992 static void __internal_warning(struct compile_state *state, struct triple *ptr, 
993         char *fmt, ...)
994 {
995         va_list args;
996         va_start(args, fmt);
997         loc(stderr, state, ptr);
998         fprintf(stderr, "Internal compiler warning: ");
999         vfprintf(stderr, fmt, args);
1000         fprintf(stderr, "\n");
1001         va_end(args);
1002 }
1003
1004
1005
1006 static void __error(struct compile_state *state, struct triple *ptr, 
1007         char *fmt, ...)
1008 {
1009         va_list args;
1010         va_start(args, fmt);
1011         loc(stderr, state, ptr);
1012         vfprintf(stderr, fmt, args);
1013         va_end(args);
1014         fprintf(stderr, "\n");
1015         do_cleanup(state);
1016         if (state->debug & DEBUG_ABORT_ON_ERROR) {
1017                 abort();
1018         }
1019         exit(1);
1020 }
1021
1022 static void __warning(struct compile_state *state, struct triple *ptr, 
1023         char *fmt, ...)
1024 {
1025         va_list args;
1026         va_start(args, fmt);
1027         loc(stderr, state, ptr);
1028         fprintf(stderr, "warning: "); 
1029         vfprintf(stderr, fmt, args);
1030         fprintf(stderr, "\n");
1031         va_end(args);
1032 }
1033
1034 #if DEBUG_ERROR_MESSAGES 
1035 #  define internal_error fprintf(stderr,  "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_error
1036 #  define internal_warning fprintf(stderr,  "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_warning
1037 #  define error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__error
1038 #  define warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__warning
1039 #else
1040 #  define internal_error __internal_error
1041 #  define internal_warning __internal_warning
1042 #  define error __error
1043 #  define warning __warning
1044 #endif
1045 #define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1046
1047 static void valid_op(struct compile_state *state, int op)
1048 {
1049         char *fmt = "invalid op: %d";
1050         if (op >= OP_MAX) {
1051                 internal_error(state, 0, fmt, op);
1052         }
1053         if (op < 0) {
1054                 internal_error(state, 0, fmt, op);
1055         }
1056 }
1057
1058 static void valid_ins(struct compile_state *state, struct triple *ptr)
1059 {
1060         valid_op(state, ptr->op);
1061 }
1062
1063 static void process_trigraphs(struct compile_state *state)
1064 {
1065         char *src, *dest, *end;
1066         struct file_state *file;
1067         file = state->file;
1068         src = dest = file->buf;
1069         end = file->buf + file->size;
1070         while((end - src) >= 3) {
1071                 if ((src[0] == '?') && (src[1] == '?')) {
1072                         int c = -1;
1073                         switch(src[2]) {
1074                         case '=': c = '#'; break;
1075                         case '/': c = '\\'; break;
1076                         case '\'': c = '^'; break;
1077                         case '(': c = '['; break;
1078                         case ')': c = ']'; break;
1079                         case '!': c = '!'; break;
1080                         case '<': c = '{'; break;
1081                         case '>': c = '}'; break;
1082                         case '-': c = '~'; break;
1083                         }
1084                         if (c != -1) {
1085                                 *dest++ = c;
1086                                 src += 3;
1087                         }
1088                         else {
1089                                 *dest++ = *src++;
1090                         }
1091                 }
1092                 else {
1093                         *dest++ = *src++;
1094                 }
1095         }
1096         while(src != end) {
1097                 *dest++ = *src++;
1098         }
1099         file->size = dest - file->buf;
1100 }
1101
1102 static void splice_lines(struct compile_state *state)
1103 {
1104         char *src, *dest, *end;
1105         struct file_state *file;
1106         file = state->file;
1107         src = dest = file->buf;
1108         end = file->buf + file->size;
1109         while((end - src) >= 2) {
1110                 if ((src[0] == '\\') && (src[1] == '\n')) {
1111                         src += 2;
1112                 }
1113                 else {
1114                         *dest++ = *src++;
1115                 }
1116         }
1117         while(src != end) {
1118                 *dest++ = *src++;
1119         }
1120         file->size = dest - file->buf;
1121 }
1122
1123 static struct type void_type;
1124 static void use_triple(struct triple *used, struct triple *user)
1125 {
1126         struct triple_set **ptr, *new;
1127         if (!used)
1128                 return;
1129         if (!user)
1130                 return;
1131         ptr = &used->use;
1132         while(*ptr) {
1133                 if ((*ptr)->member == user) {
1134                         return;
1135                 }
1136                 ptr = &(*ptr)->next;
1137         }
1138         /* Append new to the head of the list, 
1139          * copy_func and rename_block_variables
1140          * depends on this.
1141          */
1142         new = xcmalloc(sizeof(*new), "triple_set");
1143         new->member = user;
1144         new->next   = used->use;
1145         used->use   = new;
1146 }
1147
1148 static void unuse_triple(struct triple *used, struct triple *unuser)
1149 {
1150         struct triple_set *use, **ptr;
1151         if (!used) {
1152                 return;
1153         }
1154         ptr = &used->use;
1155         while(*ptr) {
1156                 use = *ptr;
1157                 if (use->member == unuser) {
1158                         *ptr = use->next;
1159                         xfree(use);
1160                 }
1161                 else {
1162                         ptr = &use->next;
1163                 }
1164         }
1165 }
1166
1167 static void push_triple(struct triple *used, struct triple *user)
1168 {
1169         struct triple_set *new;
1170         if (!used)
1171                 return;
1172         if (!user)
1173                 return;
1174         /* Append new to the head of the list,
1175          * it's the only sensible behavoir for a stack.
1176          */
1177         new = xcmalloc(sizeof(*new), "triple_set");
1178         new->member = user;
1179         new->next   = used->use;
1180         used->use   = new;
1181 }
1182
1183 static void pop_triple(struct triple *used, struct triple *unuser)
1184 {
1185         struct triple_set *use, **ptr;
1186         ptr = &used->use;
1187         while(*ptr) {
1188                 use = *ptr;
1189                 if (use->member == unuser) {
1190                         *ptr = use->next;
1191                         xfree(use);
1192                         /* Only free one occurance from the stack */
1193                         return;
1194                 }
1195                 else {
1196                         ptr = &use->next;
1197                 }
1198         }
1199 }
1200
1201 static void put_occurance(struct occurance *occurance)
1202 {
1203         occurance->count -= 1;
1204         if (occurance->count <= 0) {
1205                 if (occurance->parent) {
1206                         put_occurance(occurance->parent);
1207                 }
1208                 xfree(occurance);
1209         }
1210 }
1211
1212 static void get_occurance(struct occurance *occurance)
1213 {
1214         occurance->count += 1;
1215 }
1216
1217
1218 static struct occurance *new_occurance(struct compile_state *state)
1219 {
1220         struct occurance *result, *last;
1221         const char *filename;
1222         const char *function;
1223         int line, col;
1224
1225         function = "";
1226         filename = 0;
1227         line = 0;
1228         col  = 0;
1229         if (state->file) {
1230                 filename = state->file->report_name;
1231                 line     = state->file->report_line;
1232                 col      = get_col(state->file);
1233         }
1234         if (state->function) {
1235                 function = state->function;
1236         }
1237         last = state->last_occurance;
1238         if (last &&
1239                 (last->col == col) &&
1240                 (last->line == line) &&
1241                 (last->function == function) &&
1242                 (strcmp(last->filename, filename) == 0)) {
1243                 get_occurance(last);
1244                 return last;
1245         }
1246         if (last) {
1247                 state->last_occurance = 0;
1248                 put_occurance(last);
1249         }
1250         result = xmalloc(sizeof(*result), "occurance");
1251         result->count    = 2;
1252         result->filename = filename;
1253         result->function = function;
1254         result->line     = line;
1255         result->col      = col;
1256         result->parent   = 0;
1257         state->last_occurance = result;
1258         return result;
1259 }
1260
1261 static struct occurance *inline_occurance(struct compile_state *state,
1262         struct occurance *new, struct occurance *orig)
1263 {
1264         struct occurance *result, *last;
1265         last = state->last_occurance;
1266         if (last &&
1267                 (last->parent   == orig) &&
1268                 (last->col      == new->col) &&
1269                 (last->line     == new->line) &&
1270                 (last->function == new->function) &&
1271                 (last->filename == new->filename)) {
1272                 get_occurance(last);
1273                 return last;
1274         }
1275         if (last) {
1276                 state->last_occurance = 0;
1277                 put_occurance(last);
1278         }
1279         get_occurance(orig);
1280         result = xmalloc(sizeof(*result), "occurance");
1281         result->count    = 2;
1282         result->filename = new->filename;
1283         result->function = new->function;
1284         result->line     = new->line;
1285         result->col      = new->col;
1286         result->parent   = orig;
1287         state->last_occurance = result;
1288         return result;
1289 }
1290         
1291
1292 static struct occurance dummy_occurance = {
1293         .count    = 2,
1294         .filename = __FILE__,
1295         .function = "",
1296         .line     = __LINE__,
1297         .col      = 0,
1298         .parent   = 0,
1299 };
1300
1301 /* The zero triple is used as a place holder when we are removing pointers
1302  * from a triple.  Having allows certain sanity checks to pass even
1303  * when the original triple that was pointed to is gone.
1304  */
1305 static struct triple zero_triple = {
1306         .next      = &zero_triple,
1307         .prev      = &zero_triple,
1308         .use       = 0,
1309         .op        = OP_INTCONST,
1310         .sizes     = TRIPLE_SIZES(0, 0, 0, 0),
1311         .id        = -1, /* An invalid id */
1312         .u = { .cval   = 0, },
1313         .occurance = &dummy_occurance,
1314         .param { [0] = 0, [1] = 0, },
1315 };
1316
1317
1318 static unsigned short triple_sizes(struct compile_state *state,
1319         int op, struct type *type, int lhs_wanted, int rhs_wanted)
1320 {
1321         int lhs, rhs, misc, targ;
1322         valid_op(state, op);
1323         lhs = table_ops[op].lhs;
1324         rhs = table_ops[op].rhs;
1325         misc = table_ops[op].misc;
1326         targ = table_ops[op].targ;
1327         
1328         
1329         if (op == OP_CALL) {
1330                 struct type *param;
1331                 rhs = 0;
1332                 param = type->right;
1333                 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
1334                         rhs++;
1335                         param = param->right;
1336                 }
1337                 if ((param->type & TYPE_MASK) != TYPE_VOID) {
1338                         rhs++;
1339                 }
1340                 lhs = 0;
1341                 if ((type->left->type & TYPE_MASK) == TYPE_STRUCT) {
1342                         lhs = type->left->elements;
1343                 }
1344         }
1345         else if (op == OP_VAL_VEC) {
1346                 rhs = type->elements;
1347         }
1348         else if ((op == OP_BRANCH) || (op == OP_PHI)) {
1349                 rhs = rhs_wanted;
1350         }
1351         else if (op == OP_ASM) {
1352                 rhs = rhs_wanted;
1353                 lhs = lhs_wanted;
1354         }
1355         if ((rhs < 0) || (rhs > MAX_RHS)) {
1356                 internal_error(state, 0, "bad rhs");
1357         }
1358         if ((lhs < 0) || (lhs > MAX_LHS)) {
1359                 internal_error(state, 0, "bad lhs");
1360         }
1361         if ((misc < 0) || (misc > MAX_MISC)) {
1362                 internal_error(state, 0, "bad misc");
1363         }
1364         if ((targ < 0) || (targ > MAX_TARG)) {
1365                 internal_error(state, 0, "bad targs");
1366         }
1367         return TRIPLE_SIZES(lhs, rhs, misc, targ);
1368 }
1369
1370 static struct triple *alloc_triple(struct compile_state *state, 
1371         int op, struct type *type, int lhs, int rhs,
1372         struct occurance *occurance)
1373 {
1374         size_t size, sizes, extra_count, min_count;
1375         struct triple *ret;
1376         sizes = triple_sizes(state, op, type, lhs, rhs);
1377
1378         min_count = sizeof(ret->param)/sizeof(ret->param[0]);
1379         extra_count = TRIPLE_SIZE(sizes);
1380         extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
1381
1382         size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
1383         ret = xcmalloc(size, "tripple");
1384         ret->op        = op;
1385         ret->sizes     = sizes;
1386         ret->type      = type;
1387         ret->next      = ret;
1388         ret->prev      = ret;
1389         ret->occurance = occurance;
1390         return ret;
1391 }
1392
1393 struct triple *dup_triple(struct compile_state *state, struct triple *src)
1394 {
1395         struct triple *dup;
1396         int src_lhs, src_rhs, src_size;
1397         src_lhs = TRIPLE_LHS(src->sizes);
1398         src_rhs = TRIPLE_RHS(src->sizes);
1399         src_size = TRIPLE_SIZE(src->sizes);
1400         get_occurance(src->occurance);
1401         dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
1402                 src->occurance);
1403         memcpy(dup, src, sizeof(*src));
1404         memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
1405         return dup;
1406 }
1407
1408 static struct triple *new_triple(struct compile_state *state, 
1409         int op, struct type *type, int lhs, int rhs)
1410 {
1411         struct triple *ret;
1412         struct occurance *occurance;
1413         occurance = new_occurance(state);
1414         ret = alloc_triple(state, op, type, lhs, rhs, occurance);
1415         return ret;
1416 }
1417
1418 static struct triple *build_triple(struct compile_state *state, 
1419         int op, struct type *type, struct triple *left, struct triple *right,
1420         struct occurance *occurance)
1421 {
1422         struct triple *ret;
1423         size_t count;
1424         ret = alloc_triple(state, op, type, -1, -1, occurance);
1425         count = TRIPLE_SIZE(ret->sizes);
1426         if (count > 0) {
1427                 ret->param[0] = left;
1428         }
1429         if (count > 1) {
1430                 ret->param[1] = right;
1431         }
1432         return ret;
1433 }
1434
1435 static struct triple *triple(struct compile_state *state, 
1436         int op, struct type *type, struct triple *left, struct triple *right)
1437 {
1438         struct triple *ret;
1439         size_t count;
1440         ret = new_triple(state, op, type, -1, -1);
1441         count = TRIPLE_SIZE(ret->sizes);
1442         if (count >= 1) {
1443                 ret->param[0] = left;
1444         }
1445         if (count >= 2) {
1446                 ret->param[1] = right;
1447         }
1448         return ret;
1449 }
1450
1451 static struct triple *branch(struct compile_state *state, 
1452         struct triple *targ, struct triple *test)
1453 {
1454         struct triple *ret;
1455         ret = new_triple(state, OP_BRANCH, &void_type, -1, test?1:0);
1456         if (test) {
1457                 RHS(ret, 0) = test;
1458         }
1459         TARG(ret, 0) = targ;
1460         /* record the branch target was used */
1461         if (!targ || (targ->op != OP_LABEL)) {
1462                 internal_error(state, 0, "branch not to label");
1463                 use_triple(targ, ret);
1464         }
1465         return ret;
1466 }
1467
1468
1469 static void insert_triple(struct compile_state *state,
1470         struct triple *first, struct triple *ptr)
1471 {
1472         if (ptr) {
1473                 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
1474                         internal_error(state, ptr, "expression already used");
1475                 }
1476                 ptr->next       = first;
1477                 ptr->prev       = first->prev;
1478                 ptr->prev->next = ptr;
1479                 ptr->next->prev = ptr;
1480                 if ((ptr->prev->op == OP_BRANCH) && 
1481                         TRIPLE_RHS(ptr->prev->sizes)) {
1482                         unuse_triple(first, ptr->prev);
1483                         use_triple(ptr, ptr->prev);
1484                 }
1485         }
1486 }
1487
1488 static int triple_stores_block(struct compile_state *state, struct triple *ins)
1489 {
1490         /* This function is used to determine if u.block 
1491          * is utilized to store the current block number.
1492          */
1493         int stores_block;
1494         valid_ins(state, ins);
1495         stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
1496         return stores_block;
1497 }
1498
1499 static struct block *block_of_triple(struct compile_state *state, 
1500         struct triple *ins)
1501 {
1502         struct triple *first;
1503         first = RHS(state->main_function, 0);
1504         while(ins != first && !triple_stores_block(state, ins)) {
1505                 if (ins == ins->prev) {
1506                         internal_error(state, 0, "ins == ins->prev?");
1507                 }
1508                 ins = ins->prev;
1509         }
1510         if (!triple_stores_block(state, ins)) {
1511                 internal_error(state, ins, "Cannot find block");
1512         }
1513         return ins->u.block;
1514 }
1515
1516 static struct triple *pre_triple(struct compile_state *state,
1517         struct triple *base,
1518         int op, struct type *type, struct triple *left, struct triple *right)
1519 {
1520         struct block *block;
1521         struct triple *ret;
1522         /* If I am an OP_PIECE jump to the real instruction */
1523         if (base->op == OP_PIECE) {
1524                 base = MISC(base, 0);
1525         }
1526         block = block_of_triple(state, base);
1527         get_occurance(base->occurance);
1528         ret = build_triple(state, op, type, left, right, base->occurance);
1529         if (triple_stores_block(state, ret)) {
1530                 ret->u.block = block;
1531         }
1532         insert_triple(state, base, ret);
1533         if (block->first == base) {
1534                 block->first = ret;
1535         }
1536         return ret;
1537 }
1538
1539 static struct triple *post_triple(struct compile_state *state,
1540         struct triple *base,
1541         int op, struct type *type, struct triple *left, struct triple *right)
1542 {
1543         struct block *block;
1544         struct triple *ret;
1545         int zlhs;
1546         /* If I am an OP_PIECE jump to the real instruction */
1547         if (base->op == OP_PIECE) {
1548                 base = MISC(base, 0);
1549         }
1550         /* If I have a left hand side skip over it */
1551         zlhs = TRIPLE_LHS(base->sizes);
1552         if (zlhs && (base->op != OP_WRITE) && (base->op != OP_STORE)) {
1553                 base = LHS(base, zlhs - 1);
1554         }
1555
1556         block = block_of_triple(state, base);
1557         get_occurance(base->occurance);
1558         ret = build_triple(state, op, type, left, right, base->occurance);
1559         if (triple_stores_block(state, ret)) {
1560                 ret->u.block = block;
1561         }
1562         insert_triple(state, base->next, ret);
1563         if (block->last == base) {
1564                 block->last = ret;
1565         }
1566         return ret;
1567 }
1568
1569 static struct triple *label(struct compile_state *state)
1570 {
1571         /* Labels don't get a type */
1572         struct triple *result;
1573         result = triple(state, OP_LABEL, &void_type, 0, 0);
1574         return result;
1575 }
1576
1577 static void display_triple(FILE *fp, struct triple *ins)
1578 {
1579         struct occurance *ptr;
1580         const char *reg;
1581         char pre, post;
1582         pre = post = ' ';
1583         if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
1584                 pre = '^';
1585         }
1586         if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
1587                 post = 'v';
1588         }
1589         reg = arch_reg_str(ID_REG(ins->id));
1590         if (ins->op == OP_INTCONST) {
1591                 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s <0x%08lx>         ",
1592                         ins, pre, post, reg, ins->template_id, tops(ins->op), 
1593                         ins->u.cval);
1594         }
1595         else if (ins->op == OP_ADDRCONST) {
1596                 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s %-10p <0x%08lx>",
1597                         ins, pre, post, reg, ins->template_id, tops(ins->op), 
1598                         MISC(ins, 0), ins->u.cval);
1599         }
1600         else {
1601                 int i, count;
1602                 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s", 
1603                         ins, pre, post, reg, ins->template_id, tops(ins->op));
1604                 count = TRIPLE_SIZE(ins->sizes);
1605                 for(i = 0; i < count; i++) {
1606                         fprintf(fp, " %-10p", ins->param[i]);
1607                 }
1608                 for(; i < 2; i++) {
1609                         fprintf(fp, "           ");
1610                 }
1611         }
1612         fprintf(fp, " @");
1613         for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
1614                 fprintf(fp, " %s,%s:%d.%d",
1615                         ptr->function, 
1616                         ptr->filename,
1617                         ptr->line, 
1618                         ptr->col);
1619         }
1620         fprintf(fp, "\n");
1621         fflush(fp);
1622 }
1623
1624 static int triple_is_pure(struct compile_state *state, struct triple *ins)
1625 {
1626         /* Does the triple have no side effects.
1627          * I.e. Rexecuting the triple with the same arguments 
1628          * gives the same value.
1629          */
1630         unsigned pure;
1631         valid_ins(state, ins);
1632         pure = PURE_BITS(table_ops[ins->op].flags);
1633         if ((pure != PURE) && (pure != IMPURE)) {
1634                 internal_error(state, 0, "Purity of %s not known\n",
1635                         tops(ins->op));
1636         }
1637         return pure == PURE;
1638 }
1639
1640 static int triple_is_branch(struct compile_state *state, struct triple *ins)
1641 {
1642         /* This function is used to determine which triples need
1643          * a register.
1644          */
1645         int is_branch;
1646         valid_ins(state, ins);
1647         is_branch = (table_ops[ins->op].targ != 0);
1648         return is_branch;
1649 }
1650
1651 static int triple_is_def(struct compile_state *state, struct triple *ins)
1652 {
1653         /* This function is used to determine which triples need
1654          * a register.
1655          */
1656         int is_def;
1657         valid_ins(state, ins);
1658         is_def = (table_ops[ins->op].flags & DEF) == DEF;
1659         return is_def;
1660 }
1661
1662 static struct triple **triple_iter(struct compile_state *state,
1663         size_t count, struct triple **vector,
1664         struct triple *ins, struct triple **last)
1665 {
1666         struct triple **ret;
1667         ret = 0;
1668         if (count) {
1669                 if (!last) {
1670                         ret = vector;
1671                 }
1672                 else if ((last >= vector) && (last < (vector + count - 1))) {
1673                         ret = last + 1;
1674                 }
1675         }
1676         return ret;
1677         
1678 }
1679
1680 static struct triple **triple_lhs(struct compile_state *state,
1681         struct triple *ins, struct triple **last)
1682 {
1683         return triple_iter(state, TRIPLE_LHS(ins->sizes), &LHS(ins,0), 
1684                 ins, last);
1685 }
1686
1687 static struct triple **triple_rhs(struct compile_state *state,
1688         struct triple *ins, struct triple **last)
1689 {
1690         return triple_iter(state, TRIPLE_RHS(ins->sizes), &RHS(ins,0), 
1691                 ins, last);
1692 }
1693
1694 static struct triple **triple_misc(struct compile_state *state,
1695         struct triple *ins, struct triple **last)
1696 {
1697         return triple_iter(state, TRIPLE_MISC(ins->sizes), &MISC(ins,0), 
1698                 ins, last);
1699 }
1700
1701 static struct triple **triple_targ(struct compile_state *state,
1702         struct triple *ins, struct triple **last)
1703 {
1704         size_t count;
1705         struct triple **ret, **vector;
1706         ret = 0;
1707         count = TRIPLE_TARG(ins->sizes);
1708         vector = &TARG(ins, 0);
1709         if (count) {
1710                 if (!last) {
1711                         ret = vector;
1712                 }
1713                 else if ((last >= vector) && (last < (vector + count - 1))) {
1714                         ret = last + 1;
1715                 }
1716                 else if ((last == (vector + count - 1)) && 
1717                         TRIPLE_RHS(ins->sizes)) {
1718                         ret = &ins->next;
1719                 }
1720         }
1721         return ret;
1722 }
1723
1724
1725 static void verify_use(struct compile_state *state,
1726         struct triple *user, struct triple *used)
1727 {
1728         int size, i;
1729         size = TRIPLE_SIZE(user->sizes);
1730         for(i = 0; i < size; i++) {
1731                 if (user->param[i] == used) {
1732                         break;
1733                 }
1734         }
1735         if (triple_is_branch(state, user)) {
1736                 if (user->next == used) {
1737                         i = -1;
1738                 }
1739         }
1740         if (i == size) {
1741                 internal_error(state, user, "%s(%p) does not use %s(%p)",
1742                         tops(user->op), user, tops(used->op), used);
1743         }
1744 }
1745
1746 static int find_rhs_use(struct compile_state *state, 
1747         struct triple *user, struct triple *used)
1748 {
1749         struct triple **param;
1750         int size, i;
1751         verify_use(state, user, used);
1752         size = TRIPLE_RHS(user->sizes);
1753         param = &RHS(user, 0);
1754         for(i = 0; i < size; i++) {
1755                 if (param[i] == used) {
1756                         return i;
1757                 }
1758         }
1759         return -1;
1760 }
1761
1762 static void free_triple(struct compile_state *state, struct triple *ptr)
1763 {
1764         size_t size;
1765         size = sizeof(*ptr) - sizeof(ptr->param) +
1766                 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr->sizes));
1767         ptr->prev->next = ptr->next;
1768         ptr->next->prev = ptr->prev;
1769         if (ptr->use) {
1770                 internal_error(state, ptr, "ptr->use != 0");
1771         }
1772         put_occurance(ptr->occurance);
1773         memset(ptr, -1, size);
1774         xfree(ptr);
1775 }
1776
1777 static void release_triple(struct compile_state *state, struct triple *ptr)
1778 {
1779         struct triple_set *set, *next;
1780         struct triple **expr;
1781         /* Remove ptr from use chains where it is the user */
1782         expr = triple_rhs(state, ptr, 0);
1783         for(; expr; expr = triple_rhs(state, ptr, expr)) {
1784                 if (*expr) {
1785                         unuse_triple(*expr, ptr);
1786                 }
1787         }
1788         expr = triple_lhs(state, ptr, 0);
1789         for(; expr; expr = triple_lhs(state, ptr, expr)) {
1790                 if (*expr) {
1791                         unuse_triple(*expr, ptr);
1792                 }
1793         }
1794         expr = triple_misc(state, ptr, 0);
1795         for(; expr; expr = triple_misc(state, ptr, expr)) {
1796                 if (*expr) {
1797                         unuse_triple(*expr, ptr);
1798                 }
1799         }
1800         expr = triple_targ(state, ptr, 0);
1801         for(; expr; expr = triple_targ(state, ptr, expr)) {
1802                 if (*expr) {
1803                         unuse_triple(*expr, ptr);
1804                 }
1805         }
1806         /* Reomve ptr from use chains where it is used */
1807         for(set = ptr->use; set; set = next) {
1808                 next = set->next;
1809                 expr = triple_rhs(state, set->member, 0);
1810                 for(; expr; expr = triple_rhs(state, set->member, expr)) {
1811                         if (*expr == ptr) {
1812                                 *expr = &zero_triple;
1813                         }
1814                 }
1815                 expr = triple_lhs(state, set->member, 0);
1816                 for(; expr; expr = triple_lhs(state, set->member, expr)) {
1817                         if (*expr == ptr) {
1818                                 *expr = &zero_triple;
1819                         }
1820                 }
1821                 expr = triple_misc(state, set->member, 0);
1822                 for(; expr; expr = triple_misc(state, set->member, expr)) {
1823                         if (*expr == ptr) {
1824                                 *expr = &zero_triple;
1825                         }
1826                 }
1827                 expr = triple_targ(state, set->member, 0);
1828                 for(; expr; expr = triple_targ(state, set->member, expr)) {
1829                         if (*expr == ptr) {
1830                                 *expr = &zero_triple;
1831                         }
1832                 }
1833                 unuse_triple(ptr, set->member);
1834         }
1835         free_triple(state, ptr);
1836 }
1837
1838 static void print_triple(struct compile_state *state, struct triple *ptr);
1839
1840 #define TOK_UNKNOWN     0
1841 #define TOK_SPACE       1
1842 #define TOK_SEMI        2
1843 #define TOK_LBRACE      3
1844 #define TOK_RBRACE      4
1845 #define TOK_COMMA       5
1846 #define TOK_EQ          6
1847 #define TOK_COLON       7
1848 #define TOK_LBRACKET    8
1849 #define TOK_RBRACKET    9
1850 #define TOK_LPAREN      10
1851 #define TOK_RPAREN      11
1852 #define TOK_STAR        12
1853 #define TOK_DOTS        13
1854 #define TOK_MORE        14
1855 #define TOK_LESS        15
1856 #define TOK_TIMESEQ     16
1857 #define TOK_DIVEQ       17
1858 #define TOK_MODEQ       18
1859 #define TOK_PLUSEQ      19
1860 #define TOK_MINUSEQ     20
1861 #define TOK_SLEQ        21
1862 #define TOK_SREQ        22
1863 #define TOK_ANDEQ       23
1864 #define TOK_XOREQ       24
1865 #define TOK_OREQ        25
1866 #define TOK_EQEQ        26
1867 #define TOK_NOTEQ       27
1868 #define TOK_QUEST       28
1869 #define TOK_LOGOR       29
1870 #define TOK_LOGAND      30
1871 #define TOK_OR          31
1872 #define TOK_AND         32
1873 #define TOK_XOR         33
1874 #define TOK_LESSEQ      34
1875 #define TOK_MOREEQ      35
1876 #define TOK_SL          36
1877 #define TOK_SR          37
1878 #define TOK_PLUS        38
1879 #define TOK_MINUS       39
1880 #define TOK_DIV         40
1881 #define TOK_MOD         41
1882 #define TOK_PLUSPLUS    42
1883 #define TOK_MINUSMINUS  43
1884 #define TOK_BANG        44
1885 #define TOK_ARROW       45
1886 #define TOK_DOT         46
1887 #define TOK_TILDE       47
1888 #define TOK_LIT_STRING  48
1889 #define TOK_LIT_CHAR    49
1890 #define TOK_LIT_INT     50
1891 #define TOK_LIT_FLOAT   51
1892 #define TOK_MACRO       52
1893 #define TOK_CONCATENATE 53
1894
1895 #define TOK_IDENT       54
1896 #define TOK_STRUCT_NAME 55
1897 #define TOK_ENUM_CONST  56
1898 #define TOK_TYPE_NAME   57
1899
1900 #define TOK_AUTO        58
1901 #define TOK_BREAK       59
1902 #define TOK_CASE        60
1903 #define TOK_CHAR        61
1904 #define TOK_CONST       62
1905 #define TOK_CONTINUE    63
1906 #define TOK_DEFAULT     64
1907 #define TOK_DO          65
1908 #define TOK_DOUBLE      66
1909 #define TOK_ELSE        67
1910 #define TOK_ENUM        68
1911 #define TOK_EXTERN      69
1912 #define TOK_FLOAT       70
1913 #define TOK_FOR         71
1914 #define TOK_GOTO        72
1915 #define TOK_IF          73
1916 #define TOK_INLINE      74
1917 #define TOK_INT         75
1918 #define TOK_LONG        76
1919 #define TOK_REGISTER    77
1920 #define TOK_RESTRICT    78
1921 #define TOK_RETURN      79
1922 #define TOK_SHORT       80
1923 #define TOK_SIGNED      81
1924 #define TOK_SIZEOF      82
1925 #define TOK_STATIC      83
1926 #define TOK_STRUCT      84
1927 #define TOK_SWITCH      85
1928 #define TOK_TYPEDEF     86
1929 #define TOK_UNION       87
1930 #define TOK_UNSIGNED    88
1931 #define TOK_VOID        89
1932 #define TOK_VOLATILE    90
1933 #define TOK_WHILE       91
1934 #define TOK_ASM         92
1935 #define TOK_ATTRIBUTE   93
1936 #define TOK_ALIGNOF     94
1937 #define TOK_FIRST_KEYWORD TOK_AUTO
1938 #define TOK_LAST_KEYWORD  TOK_ALIGNOF
1939
1940 #define TOK_DEFINE      100
1941 #define TOK_UNDEF       101
1942 #define TOK_INCLUDE     102
1943 #define TOK_LINE        103
1944 #define TOK_ERROR       104
1945 #define TOK_WARNING     105
1946 #define TOK_PRAGMA      106
1947 #define TOK_IFDEF       107
1948 #define TOK_IFNDEF      108
1949 #define TOK_ELIF        109
1950 #define TOK_ENDIF       110
1951
1952 #define TOK_FIRST_MACRO TOK_DEFINE
1953 #define TOK_LAST_MACRO  TOK_ENDIF
1954          
1955 #define TOK_EOF         111
1956
1957 static const char *tokens[] = {
1958 [TOK_UNKNOWN     ] = "unknown",
1959 [TOK_SPACE       ] = ":space:",
1960 [TOK_SEMI        ] = ";",
1961 [TOK_LBRACE      ] = "{",
1962 [TOK_RBRACE      ] = "}",
1963 [TOK_COMMA       ] = ",",
1964 [TOK_EQ          ] = "=",
1965 [TOK_COLON       ] = ":",
1966 [TOK_LBRACKET    ] = "[",
1967 [TOK_RBRACKET    ] = "]",
1968 [TOK_LPAREN      ] = "(",
1969 [TOK_RPAREN      ] = ")",
1970 [TOK_STAR        ] = "*",
1971 [TOK_DOTS        ] = "...",
1972 [TOK_MORE        ] = ">",
1973 [TOK_LESS        ] = "<",
1974 [TOK_TIMESEQ     ] = "*=",
1975 [TOK_DIVEQ       ] = "/=",
1976 [TOK_MODEQ       ] = "%=",
1977 [TOK_PLUSEQ      ] = "+=",
1978 [TOK_MINUSEQ     ] = "-=",
1979 [TOK_SLEQ        ] = "<<=",
1980 [TOK_SREQ        ] = ">>=",
1981 [TOK_ANDEQ       ] = "&=",
1982 [TOK_XOREQ       ] = "^=",
1983 [TOK_OREQ        ] = "|=",
1984 [TOK_EQEQ        ] = "==",
1985 [TOK_NOTEQ       ] = "!=",
1986 [TOK_QUEST       ] = "?",
1987 [TOK_LOGOR       ] = "||",
1988 [TOK_LOGAND      ] = "&&",
1989 [TOK_OR          ] = "|",
1990 [TOK_AND         ] = "&",
1991 [TOK_XOR         ] = "^",
1992 [TOK_LESSEQ      ] = "<=",
1993 [TOK_MOREEQ      ] = ">=",
1994 [TOK_SL          ] = "<<",
1995 [TOK_SR          ] = ">>",
1996 [TOK_PLUS        ] = "+",
1997 [TOK_MINUS       ] = "-",
1998 [TOK_DIV         ] = "/",
1999 [TOK_MOD         ] = "%",
2000 [TOK_PLUSPLUS    ] = "++",
2001 [TOK_MINUSMINUS  ] = "--",
2002 [TOK_BANG        ] = "!",
2003 [TOK_ARROW       ] = "->",
2004 [TOK_DOT         ] = ".",
2005 [TOK_TILDE       ] = "~",
2006 [TOK_LIT_STRING  ] = ":string:",
2007 [TOK_IDENT       ] = ":ident:",
2008 [TOK_TYPE_NAME   ] = ":typename:",
2009 [TOK_LIT_CHAR    ] = ":char:",
2010 [TOK_LIT_INT     ] = ":integer:",
2011 [TOK_LIT_FLOAT   ] = ":float:",
2012 [TOK_MACRO       ] = "#",
2013 [TOK_CONCATENATE ] = "##",
2014
2015 [TOK_AUTO        ] = "auto",
2016 [TOK_BREAK       ] = "break",
2017 [TOK_CASE        ] = "case",
2018 [TOK_CHAR        ] = "char",
2019 [TOK_CONST       ] = "const",
2020 [TOK_CONTINUE    ] = "continue",
2021 [TOK_DEFAULT     ] = "default",
2022 [TOK_DO          ] = "do",
2023 [TOK_DOUBLE      ] = "double",
2024 [TOK_ELSE        ] = "else",
2025 [TOK_ENUM        ] = "enum",
2026 [TOK_EXTERN      ] = "extern",
2027 [TOK_FLOAT       ] = "float",
2028 [TOK_FOR         ] = "for",
2029 [TOK_GOTO        ] = "goto",
2030 [TOK_IF          ] = "if",
2031 [TOK_INLINE      ] = "inline",
2032 [TOK_INT         ] = "int",
2033 [TOK_LONG        ] = "long",
2034 [TOK_REGISTER    ] = "register",
2035 [TOK_RESTRICT    ] = "restrict",
2036 [TOK_RETURN      ] = "return",
2037 [TOK_SHORT       ] = "short",
2038 [TOK_SIGNED      ] = "signed",
2039 [TOK_SIZEOF      ] = "sizeof",
2040 [TOK_STATIC      ] = "static",
2041 [TOK_STRUCT      ] = "struct",
2042 [TOK_SWITCH      ] = "switch",
2043 [TOK_TYPEDEF     ] = "typedef",
2044 [TOK_UNION       ] = "union",
2045 [TOK_UNSIGNED    ] = "unsigned",
2046 [TOK_VOID        ] = "void",
2047 [TOK_VOLATILE    ] = "volatile",
2048 [TOK_WHILE       ] = "while",
2049 [TOK_ASM         ] = "asm",
2050 [TOK_ATTRIBUTE   ] = "__attribute__",
2051 [TOK_ALIGNOF     ] = "__alignof__",
2052
2053 [TOK_DEFINE      ] = "define",
2054 [TOK_UNDEF       ] = "undef",
2055 [TOK_INCLUDE     ] = "include",
2056 [TOK_LINE        ] = "line",
2057 [TOK_ERROR       ] = "error",
2058 [TOK_WARNING     ] = "warning",
2059 [TOK_PRAGMA      ] = "pragma",
2060 [TOK_IFDEF       ] = "ifdef",
2061 [TOK_IFNDEF      ] = "ifndef",
2062 [TOK_ELIF        ] = "elif",
2063 [TOK_ENDIF       ] = "endif",
2064
2065 [TOK_EOF         ] = "EOF",
2066 };
2067
2068 static unsigned int hash(const char *str, int str_len)
2069 {
2070         unsigned int hash;
2071         const char *end;
2072         end = str + str_len;
2073         hash = 0;
2074         for(; str < end; str++) {
2075                 hash = (hash *263) + *str;
2076         }
2077         hash = hash & (HASH_TABLE_SIZE -1);
2078         return hash;
2079 }
2080
2081 static struct hash_entry *lookup(
2082         struct compile_state *state, const char *name, int name_len)
2083 {
2084         struct hash_entry *entry;
2085         unsigned int index;
2086         index = hash(name, name_len);
2087         entry = state->hash_table[index];
2088         while(entry && 
2089                 ((entry->name_len != name_len) ||
2090                         (memcmp(entry->name, name, name_len) != 0))) {
2091                 entry = entry->next;
2092         }
2093         if (!entry) {
2094                 char *new_name;
2095                 /* Get a private copy of the name */
2096                 new_name = xmalloc(name_len + 1, "hash_name");
2097                 memcpy(new_name, name, name_len);
2098                 new_name[name_len] = '\0';
2099
2100                 /* Create a new hash entry */
2101                 entry = xcmalloc(sizeof(*entry), "hash_entry");
2102                 entry->next = state->hash_table[index];
2103                 entry->name = new_name;
2104                 entry->name_len = name_len;
2105
2106                 /* Place the new entry in the hash table */
2107                 state->hash_table[index] = entry;
2108         }
2109         return entry;
2110 }
2111
2112 static void ident_to_keyword(struct compile_state *state, struct token *tk)
2113 {
2114         struct hash_entry *entry;
2115         entry = tk->ident;
2116         if (entry && ((entry->tok == TOK_TYPE_NAME) ||
2117                 (entry->tok == TOK_ENUM_CONST) ||
2118                 ((entry->tok >= TOK_FIRST_KEYWORD) && 
2119                         (entry->tok <= TOK_LAST_KEYWORD)))) {
2120                 tk->tok = entry->tok;
2121         }
2122 }
2123
2124 static void ident_to_macro(struct compile_state *state, struct token *tk)
2125 {
2126         struct hash_entry *entry;
2127         entry = tk->ident;
2128         if (entry && 
2129                 (entry->tok >= TOK_FIRST_MACRO) &&
2130                 (entry->tok <= TOK_LAST_MACRO)) {
2131                 tk->tok = entry->tok;
2132         }
2133 }
2134
2135 static void hash_keyword(
2136         struct compile_state *state, const char *keyword, int tok)
2137 {
2138         struct hash_entry *entry;
2139         entry = lookup(state, keyword, strlen(keyword));
2140         if (entry && entry->tok != TOK_UNKNOWN) {
2141                 die("keyword %s already hashed", keyword);
2142         }
2143         entry->tok  = tok;
2144 }
2145
2146 static void symbol(
2147         struct compile_state *state, struct hash_entry *ident,
2148         struct symbol **chain, struct triple *def, struct type *type)
2149 {
2150         struct symbol *sym;
2151         if (*chain && ((*chain)->scope_depth == state->scope_depth)) {
2152                 error(state, 0, "%s already defined", ident->name);
2153         }
2154         sym = xcmalloc(sizeof(*sym), "symbol");
2155         sym->ident = ident;
2156         sym->def   = def;
2157         sym->type  = type;
2158         sym->scope_depth = state->scope_depth;
2159         sym->next = *chain;
2160         *chain    = sym;
2161 }
2162
2163 static void label_symbol(struct compile_state *state, 
2164         struct hash_entry *ident, struct triple *label)
2165 {
2166         struct symbol *sym;
2167         if (ident->sym_label) {
2168                 error(state, 0, "label %s already defined", ident->name);
2169         }
2170         sym = xcmalloc(sizeof(*sym), "label");
2171         sym->ident = ident;
2172         sym->def   = label;
2173         sym->type  = &void_type;
2174         sym->scope_depth = FUNCTION_SCOPE_DEPTH;
2175         sym->next  = 0;
2176         ident->sym_label = sym;
2177 }
2178
2179 static void start_scope(struct compile_state *state)
2180 {
2181         state->scope_depth++;
2182 }
2183
2184 static void end_scope_syms(struct symbol **chain, int depth)
2185 {
2186         struct symbol *sym, *next;
2187         sym = *chain;
2188         while(sym && (sym->scope_depth == depth)) {
2189                 next = sym->next;
2190                 xfree(sym);
2191                 sym = next;
2192         }
2193         *chain = sym;
2194 }
2195
2196 static void end_scope(struct compile_state *state)
2197 {
2198         int i;
2199         int depth;
2200         /* Walk through the hash table and remove all symbols
2201          * in the current scope. 
2202          */
2203         depth = state->scope_depth;
2204         for(i = 0; i < HASH_TABLE_SIZE; i++) {
2205                 struct hash_entry *entry;
2206                 entry = state->hash_table[i];
2207                 while(entry) {
2208                         end_scope_syms(&entry->sym_label,  depth);
2209                         end_scope_syms(&entry->sym_struct, depth);
2210                         end_scope_syms(&entry->sym_ident,  depth);
2211                         entry = entry->next;
2212                 }
2213         }
2214         state->scope_depth = depth - 1;
2215 }
2216
2217 static void register_keywords(struct compile_state *state)
2218 {
2219         hash_keyword(state, "auto",          TOK_AUTO);
2220         hash_keyword(state, "break",         TOK_BREAK);
2221         hash_keyword(state, "case",          TOK_CASE);
2222         hash_keyword(state, "char",          TOK_CHAR);
2223         hash_keyword(state, "const",         TOK_CONST);
2224         hash_keyword(state, "continue",      TOK_CONTINUE);
2225         hash_keyword(state, "default",       TOK_DEFAULT);
2226         hash_keyword(state, "do",            TOK_DO);
2227         hash_keyword(state, "double",        TOK_DOUBLE);
2228         hash_keyword(state, "else",          TOK_ELSE);
2229         hash_keyword(state, "enum",          TOK_ENUM);
2230         hash_keyword(state, "extern",        TOK_EXTERN);
2231         hash_keyword(state, "float",         TOK_FLOAT);
2232         hash_keyword(state, "for",           TOK_FOR);
2233         hash_keyword(state, "goto",          TOK_GOTO);
2234         hash_keyword(state, "if",            TOK_IF);
2235         hash_keyword(state, "inline",        TOK_INLINE);
2236         hash_keyword(state, "int",           TOK_INT);
2237         hash_keyword(state, "long",          TOK_LONG);
2238         hash_keyword(state, "register",      TOK_REGISTER);
2239         hash_keyword(state, "restrict",      TOK_RESTRICT);
2240         hash_keyword(state, "return",        TOK_RETURN);
2241         hash_keyword(state, "short",         TOK_SHORT);
2242         hash_keyword(state, "signed",        TOK_SIGNED);
2243         hash_keyword(state, "sizeof",        TOK_SIZEOF);
2244         hash_keyword(state, "static",        TOK_STATIC);
2245         hash_keyword(state, "struct",        TOK_STRUCT);
2246         hash_keyword(state, "switch",        TOK_SWITCH);
2247         hash_keyword(state, "typedef",       TOK_TYPEDEF);
2248         hash_keyword(state, "union",         TOK_UNION);
2249         hash_keyword(state, "unsigned",      TOK_UNSIGNED);
2250         hash_keyword(state, "void",          TOK_VOID);
2251         hash_keyword(state, "volatile",      TOK_VOLATILE);
2252         hash_keyword(state, "__volatile__",  TOK_VOLATILE);
2253         hash_keyword(state, "while",         TOK_WHILE);
2254         hash_keyword(state, "asm",           TOK_ASM);
2255         hash_keyword(state, "__asm__",       TOK_ASM);
2256         hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
2257         hash_keyword(state, "__alignof__",   TOK_ALIGNOF);
2258 }
2259
2260 static void register_macro_keywords(struct compile_state *state)
2261 {
2262         hash_keyword(state, "define",        TOK_DEFINE);
2263         hash_keyword(state, "undef",         TOK_UNDEF);
2264         hash_keyword(state, "include",       TOK_INCLUDE);
2265         hash_keyword(state, "line",          TOK_LINE);
2266         hash_keyword(state, "error",         TOK_ERROR);
2267         hash_keyword(state, "warning",       TOK_WARNING);
2268         hash_keyword(state, "pragma",        TOK_PRAGMA);
2269         hash_keyword(state, "ifdef",         TOK_IFDEF);
2270         hash_keyword(state, "ifndef",        TOK_IFNDEF);
2271         hash_keyword(state, "elif",          TOK_ELIF);
2272         hash_keyword(state, "endif",         TOK_ENDIF);
2273 }
2274
2275 static int spacep(int c)
2276 {
2277         int ret = 0;
2278         switch(c) {
2279         case ' ':
2280         case '\t':
2281         case '\f':
2282         case '\v':
2283         case '\r':
2284         case '\n':
2285                 ret = 1;
2286                 break;
2287         }
2288         return ret;
2289 }
2290
2291 static int digitp(int c)
2292 {
2293         int ret = 0;
2294         switch(c) {
2295         case '0': case '1': case '2': case '3': case '4': 
2296         case '5': case '6': case '7': case '8': case '9':
2297                 ret = 1;
2298                 break;
2299         }
2300         return ret;
2301 }
2302 static int digval(int c)
2303 {
2304         int val = -1;
2305         if ((c >= '0') && (c <= '9')) {
2306                 val = c - '0';
2307         }
2308         return val;
2309 }
2310
2311 static int hexdigitp(int c)
2312 {
2313         int ret = 0;
2314         switch(c) {
2315         case '0': case '1': case '2': case '3': case '4': 
2316         case '5': case '6': case '7': case '8': case '9':
2317         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2318         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2319                 ret = 1;
2320                 break;
2321         }
2322         return ret;
2323 }
2324 static int hexdigval(int c) 
2325 {
2326         int val = -1;
2327         if ((c >= '0') && (c <= '9')) {
2328                 val = c - '0';
2329         }
2330         else if ((c >= 'A') && (c <= 'F')) {
2331                 val = 10 + (c - 'A');
2332         }
2333         else if ((c >= 'a') && (c <= 'f')) {
2334                 val = 10 + (c - 'a');
2335         }
2336         return val;
2337 }
2338
2339 static int octdigitp(int c)
2340 {
2341         int ret = 0;
2342         switch(c) {
2343         case '0': case '1': case '2': case '3': 
2344         case '4': case '5': case '6': case '7':
2345                 ret = 1;
2346                 break;
2347         }
2348         return ret;
2349 }
2350 static int octdigval(int c)
2351 {
2352         int val = -1;
2353         if ((c >= '0') && (c <= '7')) {
2354                 val = c - '0';
2355         }
2356         return val;
2357 }
2358
2359 static int letterp(int c)
2360 {
2361         int ret = 0;
2362         switch(c) {
2363         case 'a': case 'b': case 'c': case 'd': case 'e':
2364         case 'f': case 'g': case 'h': case 'i': case 'j':
2365         case 'k': case 'l': case 'm': case 'n': case 'o':
2366         case 'p': case 'q': case 'r': case 's': case 't':
2367         case 'u': case 'v': case 'w': case 'x': case 'y':
2368         case 'z':
2369         case 'A': case 'B': case 'C': case 'D': case 'E':
2370         case 'F': case 'G': case 'H': case 'I': case 'J':
2371         case 'K': case 'L': case 'M': case 'N': case 'O':
2372         case 'P': case 'Q': case 'R': case 'S': case 'T':
2373         case 'U': case 'V': case 'W': case 'X': case 'Y':
2374         case 'Z':
2375         case '_':
2376                 ret = 1;
2377                 break;
2378         }
2379         return ret;
2380 }
2381
2382 static int char_value(struct compile_state *state,
2383         const signed char **strp, const signed char *end)
2384 {
2385         const signed char *str;
2386         int c;
2387         str = *strp;
2388         c = *str++;
2389         if ((c == '\\') && (str < end)) {
2390                 switch(*str) {
2391                 case 'n':  c = '\n'; str++; break;
2392                 case 't':  c = '\t'; str++; break;
2393                 case 'v':  c = '\v'; str++; break;
2394                 case 'b':  c = '\b'; str++; break;
2395                 case 'r':  c = '\r'; str++; break;
2396                 case 'f':  c = '\f'; str++; break;
2397                 case 'a':  c = '\a'; str++; break;
2398                 case '\\': c = '\\'; str++; break;
2399                 case '?':  c = '?';  str++; break;
2400                 case '\'': c = '\''; str++; break;
2401                 case '"':  c = '"';  break;
2402                 case 'x': 
2403                         c = 0;
2404                         str++;
2405                         while((str < end) && hexdigitp(*str)) {
2406                                 c <<= 4;
2407                                 c += hexdigval(*str);
2408                                 str++;
2409                         }
2410                         break;
2411                 case '0': case '1': case '2': case '3': 
2412                 case '4': case '5': case '6': case '7':
2413                         c = 0;
2414                         while((str < end) && octdigitp(*str)) {
2415                                 c <<= 3;
2416                                 c += octdigval(*str);
2417                                 str++;
2418                         }
2419                         break;
2420                 default:
2421                         error(state, 0, "Invalid character constant");
2422                         break;
2423                 }
2424         }
2425         *strp = str;
2426         return c;
2427 }
2428
2429 static char *after_digits(char *ptr, char *end)
2430 {
2431         while((ptr < end) && digitp(*ptr)) {
2432                 ptr++;
2433         }
2434         return ptr;
2435 }
2436
2437 static char *after_octdigits(char *ptr, char *end)
2438 {
2439         while((ptr < end) && octdigitp(*ptr)) {
2440                 ptr++;
2441         }
2442         return ptr;
2443 }
2444
2445 static char *after_hexdigits(char *ptr, char *end)
2446 {
2447         while((ptr < end) && hexdigitp(*ptr)) {
2448                 ptr++;
2449         }
2450         return ptr;
2451 }
2452
2453 static void save_string(struct compile_state *state, 
2454         struct token *tk, char *start, char *end, const char *id)
2455 {
2456         char *str;
2457         int str_len;
2458         /* Create a private copy of the string */
2459         str_len = end - start + 1;
2460         str = xmalloc(str_len + 1, id);
2461         memcpy(str, start, str_len);
2462         str[str_len] = '\0';
2463
2464         /* Store the copy in the token */
2465         tk->val.str = str;
2466         tk->str_len = str_len;
2467 }
2468 static void next_token(struct compile_state *state, int index)
2469 {
2470         struct file_state *file;
2471         struct token *tk;
2472         char *token;
2473         int c, c1, c2, c3;
2474         char *tokp, *end;
2475         int tok;
2476 next_token:
2477         file = state->file;
2478         tk = &state->token[index];
2479         tk->str_len = 0;
2480         tk->ident = 0;
2481         token = tokp = file->pos;
2482         end = file->buf + file->size;
2483         tok = TOK_UNKNOWN;
2484         c = -1;
2485         if (tokp < end) {
2486                 c = *tokp;
2487         }
2488         c1 = -1;
2489         if ((tokp + 1) < end) {
2490                 c1 = tokp[1];
2491         }
2492         c2 = -1;
2493         if ((tokp + 2) < end) {
2494                 c2 = tokp[2];
2495         }
2496         c3 = -1;
2497         if ((tokp + 3) < end) {
2498                 c3 = tokp[3];
2499         }
2500         if (tokp >= end) {
2501                 tok = TOK_EOF;
2502                 tokp = end;
2503         }
2504         /* Whitespace */
2505         else if (spacep(c)) {
2506                 tok = TOK_SPACE;
2507                 while ((tokp < end) && spacep(c)) {
2508                         if (c == '\n') {
2509                                 file->line++;
2510                                 file->report_line++;
2511                                 file->line_start = tokp + 1;
2512                         }
2513                         c = *(++tokp);
2514                 }
2515                 if (!spacep(c)) {
2516                         tokp--;
2517                 }
2518         }
2519         /* EOL Comments */
2520         else if ((c == '/') && (c1 == '/')) {
2521                 tok = TOK_SPACE;
2522                 for(tokp += 2; tokp < end; tokp++) {
2523                         c = *tokp;
2524                         if (c == '\n') {
2525                                 file->line++;
2526                                 file->report_line++;
2527                                 file->line_start = tokp +1;
2528                                 break;
2529                         }
2530                 }
2531         }
2532         /* Comments */
2533         else if ((c == '/') && (c1 == '*')) {
2534                 int line;
2535                 char *line_start;
2536                 line = file->line;
2537                 line_start = file->line_start;
2538                 for(tokp += 2; (end - tokp) >= 2; tokp++) {
2539                         c = *tokp;
2540                         if (c == '\n') {
2541                                 line++;
2542                                 line_start = tokp +1;
2543                         }
2544                         else if ((c == '*') && (tokp[1] == '/')) {
2545                                 tok = TOK_SPACE;
2546                                 tokp += 1;
2547                                 break;
2548                         }
2549                 }
2550                 if (tok == TOK_UNKNOWN) {
2551                         error(state, 0, "unterminated comment");
2552                 }
2553                 file->report_line += line - file->line;
2554                 file->line = line;
2555                 file->line_start = line_start;
2556         }
2557         /* string constants */
2558         else if ((c == '"') ||
2559                 ((c == 'L') && (c1 == '"'))) {
2560                 int line;
2561                 char *line_start;
2562                 int wchar;
2563                 line = file->line;
2564                 line_start = file->line_start;
2565                 wchar = 0;
2566                 if (c == 'L') {
2567                         wchar = 1;
2568                         tokp++;
2569                 }
2570                 for(tokp += 1; tokp < end; tokp++) {
2571                         c = *tokp;
2572                         if (c == '\n') {
2573                                 line++;
2574                                 line_start = tokp + 1;
2575                         }
2576                         else if ((c == '\\') && (tokp +1 < end)) {
2577                                 tokp++;
2578                         }
2579                         else if (c == '"') {
2580                                 tok = TOK_LIT_STRING;
2581                                 break;
2582                         }
2583                 }
2584                 if (tok == TOK_UNKNOWN) {
2585                         error(state, 0, "unterminated string constant");
2586                 }
2587                 if (line != file->line) {
2588                         warning(state, 0, "multiline string constant");
2589                 }
2590                 file->report_line += line - file->line;
2591                 file->line = line;
2592                 file->line_start = line_start;
2593
2594                 /* Save the string value */
2595                 save_string(state, tk, token, tokp, "literal string");
2596         }
2597         /* character constants */
2598         else if ((c == '\'') ||
2599                 ((c == 'L') && (c1 == '\''))) {
2600                 int line;
2601                 char *line_start;
2602                 int wchar;
2603                 line = file->line;
2604                 line_start = file->line_start;
2605                 wchar = 0;
2606                 if (c == 'L') {
2607                         wchar = 1;
2608                         tokp++;
2609                 }
2610                 for(tokp += 1; tokp < end; tokp++) {
2611                         c = *tokp;
2612                         if (c == '\n') {
2613                                 line++;
2614                                 line_start = tokp + 1;
2615                         }
2616                         else if ((c == '\\') && (tokp +1 < end)) {
2617                                 tokp++;
2618                         }
2619                         else if (c == '\'') {
2620                                 tok = TOK_LIT_CHAR;
2621                                 break;
2622                         }
2623                 }
2624                 if (tok == TOK_UNKNOWN) {
2625                         error(state, 0, "unterminated character constant");
2626                 }
2627                 if (line != file->line) {
2628                         warning(state, 0, "multiline character constant");
2629                 }
2630                 file->report_line += line - file->line;
2631                 file->line = line;
2632                 file->line_start = line_start;
2633
2634                 /* Save the character value */
2635                 save_string(state, tk, token, tokp, "literal character");
2636         }
2637         /* integer and floating constants 
2638          * Integer Constants
2639          * {digits}
2640          * 0[Xx]{hexdigits}
2641          * 0{octdigit}+
2642          * 
2643          * Floating constants
2644          * {digits}.{digits}[Ee][+-]?{digits}
2645          * {digits}.{digits}
2646          * {digits}[Ee][+-]?{digits}
2647          * .{digits}[Ee][+-]?{digits}
2648          * .{digits}
2649          */
2650         
2651         else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
2652                 char *next, *new;
2653                 int is_float;
2654                 is_float = 0;
2655                 if (c != '.') {
2656                         next = after_digits(tokp, end);
2657                 }
2658                 else {
2659                         next = tokp;
2660                 }
2661                 if (next[0] == '.') {
2662                         new = after_digits(next, end);
2663                         is_float = (new != next);
2664                         next = new;
2665                 }
2666                 if ((next[0] == 'e') || (next[0] == 'E')) {
2667                         if (((next + 1) < end) && 
2668                                 ((next[1] == '+') || (next[1] == '-'))) {
2669                                 next++;
2670                         }
2671                         new = after_digits(next, end);
2672                         is_float = (new != next);
2673                         next = new;
2674                 }
2675                 if (is_float) {
2676                         tok = TOK_LIT_FLOAT;
2677                         if ((next < end) && (
2678                                 (next[0] == 'f') ||
2679                                 (next[0] == 'F') ||
2680                                 (next[0] == 'l') ||
2681                                 (next[0] == 'L'))
2682                                 ) {
2683                                 next++;
2684                         }
2685                 }
2686                 if (!is_float && digitp(c)) {
2687                         tok = TOK_LIT_INT;
2688                         if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
2689                                 next = after_hexdigits(tokp + 2, end);
2690                         }
2691                         else if (c == '0') {
2692                                 next = after_octdigits(tokp, end);
2693                         }
2694                         else {
2695                                 next = after_digits(tokp, end);
2696                         }
2697                         /* crazy integer suffixes */
2698                         if ((next < end) && 
2699                                 ((next[0] == 'u') || (next[0] == 'U'))) { 
2700                                 next++;
2701                                 if ((next < end) &&
2702                                         ((next[0] == 'l') || (next[0] == 'L'))) {
2703                                         next++;
2704                                 }
2705                         }
2706                         else if ((next < end) &&
2707                                 ((next[0] == 'l') || (next[0] == 'L'))) {
2708                                 next++;
2709                                 if ((next < end) && 
2710                                         ((next[0] == 'u') || (next[0] == 'U'))) { 
2711                                         next++;
2712                                 }
2713                         }
2714                 }
2715                 tokp = next - 1;
2716
2717                 /* Save the integer/floating point value */
2718                 save_string(state, tk, token, tokp, "literal number");
2719         }
2720         /* identifiers */
2721         else if (letterp(c)) {
2722                 tok = TOK_IDENT;
2723                 for(tokp += 1; tokp < end; tokp++) {
2724                         c = *tokp;
2725                         if (!letterp(c) && !digitp(c)) {
2726                                 break;
2727                         }
2728                 }
2729                 tokp -= 1;
2730                 tk->ident = lookup(state, token, tokp +1 - token);
2731         }
2732         /* C99 alternate macro characters */
2733         else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) { 
2734                 tokp += 3; 
2735                 tok = TOK_CONCATENATE; 
2736         }
2737         else if ((c == '.') && (c1 == '.') && (c2 == '.')) { tokp += 2; tok = TOK_DOTS; }
2738         else if ((c == '<') && (c1 == '<') && (c2 == '=')) { tokp += 2; tok = TOK_SLEQ; }
2739         else if ((c == '>') && (c1 == '>') && (c2 == '=')) { tokp += 2; tok = TOK_SREQ; }
2740         else if ((c == '*') && (c1 == '=')) { tokp += 1; tok = TOK_TIMESEQ; }
2741         else if ((c == '/') && (c1 == '=')) { tokp += 1; tok = TOK_DIVEQ; }
2742         else if ((c == '%') && (c1 == '=')) { tokp += 1; tok = TOK_MODEQ; }
2743         else if ((c == '+') && (c1 == '=')) { tokp += 1; tok = TOK_PLUSEQ; }
2744         else if ((c == '-') && (c1 == '=')) { tokp += 1; tok = TOK_MINUSEQ; }
2745         else if ((c == '&') && (c1 == '=')) { tokp += 1; tok = TOK_ANDEQ; }
2746         else if ((c == '^') && (c1 == '=')) { tokp += 1; tok = TOK_XOREQ; }
2747         else if ((c == '|') && (c1 == '=')) { tokp += 1; tok = TOK_OREQ; }
2748         else if ((c == '=') && (c1 == '=')) { tokp += 1; tok = TOK_EQEQ; }
2749         else if ((c == '!') && (c1 == '=')) { tokp += 1; tok = TOK_NOTEQ; }
2750         else if ((c == '|') && (c1 == '|')) { tokp += 1; tok = TOK_LOGOR; }
2751         else if ((c == '&') && (c1 == '&')) { tokp += 1; tok = TOK_LOGAND; }
2752         else if ((c == '<') && (c1 == '=')) { tokp += 1; tok = TOK_LESSEQ; }
2753         else if ((c == '>') && (c1 == '=')) { tokp += 1; tok = TOK_MOREEQ; }
2754         else if ((c == '<') && (c1 == '<')) { tokp += 1; tok = TOK_SL; }
2755         else if ((c == '>') && (c1 == '>')) { tokp += 1; tok = TOK_SR; }
2756         else if ((c == '+') && (c1 == '+')) { tokp += 1; tok = TOK_PLUSPLUS; }
2757         else if ((c == '-') && (c1 == '-')) { tokp += 1; tok = TOK_MINUSMINUS; }
2758         else if ((c == '-') && (c1 == '>')) { tokp += 1; tok = TOK_ARROW; }
2759         else if ((c == '<') && (c1 == ':')) { tokp += 1; tok = TOK_LBRACKET; }
2760         else if ((c == ':') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACKET; }
2761         else if ((c == '<') && (c1 == '%')) { tokp += 1; tok = TOK_LBRACE; }
2762         else if ((c == '%') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACE; }
2763         else if ((c == '%') && (c1 == ':')) { tokp += 1; tok = TOK_MACRO; }
2764         else if ((c == '#') && (c1 == '#')) { tokp += 1; tok = TOK_CONCATENATE; }
2765         else if (c == ';') { tok = TOK_SEMI; }
2766         else if (c == '{') { tok = TOK_LBRACE; }
2767         else if (c == '}') { tok = TOK_RBRACE; }
2768         else if (c == ',') { tok = TOK_COMMA; }
2769         else if (c == '=') { tok = TOK_EQ; }
2770         else if (c == ':') { tok = TOK_COLON; }
2771         else if (c == '[') { tok = TOK_LBRACKET; }
2772         else if (c == ']') { tok = TOK_RBRACKET; }
2773         else if (c == '(') { tok = TOK_LPAREN; }
2774         else if (c == ')') { tok = TOK_RPAREN; }
2775         else if (c == '*') { tok = TOK_STAR; }
2776         else if (c == '>') { tok = TOK_MORE; }
2777         else if (c == '<') { tok = TOK_LESS; }
2778         else if (c == '?') { tok = TOK_QUEST; }
2779         else if (c == '|') { tok = TOK_OR; }
2780         else if (c == '&') { tok = TOK_AND; }
2781         else if (c == '^') { tok = TOK_XOR; }
2782         else if (c == '+') { tok = TOK_PLUS; }
2783         else if (c == '-') { tok = TOK_MINUS; }
2784         else if (c == '/') { tok = TOK_DIV; }
2785         else if (c == '%') { tok = TOK_MOD; }
2786         else if (c == '!') { tok = TOK_BANG; }
2787         else if (c == '.') { tok = TOK_DOT; }
2788         else if (c == '~') { tok = TOK_TILDE; }
2789         else if (c == '#') { tok = TOK_MACRO; }
2790         if (tok == TOK_MACRO) {
2791                 /* Only match preprocessor directives at the start of a line */
2792                 char *ptr;
2793                 for(ptr = file->line_start; spacep(*ptr); ptr++)
2794                         ;
2795                 if (ptr != tokp) {
2796                         tok = TOK_UNKNOWN;
2797                 }
2798         }
2799         if (tok == TOK_UNKNOWN) {
2800                 error(state, 0, "unknown token");
2801         }
2802
2803         file->pos = tokp + 1;
2804         tk->tok = tok;
2805         if (tok == TOK_IDENT) {
2806                 ident_to_keyword(state, tk);
2807         }
2808         /* Don't return space tokens. */
2809         if (tok == TOK_SPACE) {
2810                 goto next_token;
2811         }
2812 }
2813
2814 static void compile_macro(struct compile_state *state, struct token *tk)
2815 {
2816         struct file_state *file;
2817         struct hash_entry *ident;
2818         ident = tk->ident;
2819         file = xmalloc(sizeof(*file), "file_state");
2820         file->basename = xstrdup(tk->ident->name);
2821         file->dirname = xstrdup("");
2822         file->size = ident->sym_define->buf_len;
2823         file->buf = xmalloc(file->size +2,  file->basename);
2824         memcpy(file->buf, ident->sym_define->buf, file->size);
2825         file->buf[file->size] = '\n';
2826         file->buf[file->size + 1] = '\0';
2827         file->pos = file->buf;
2828         file->line_start = file->pos;
2829         file->line = 1;
2830         file->report_line = 1;
2831         file->report_name = file->basename;
2832         file->report_dir  = file->dirname;
2833         file->prev = state->file;
2834         state->file = file;
2835 }
2836
2837
2838 static int mpeek(struct compile_state *state, int index)
2839 {
2840         struct token *tk;
2841         int rescan;
2842         tk = &state->token[index + 1];
2843         if (tk->tok == -1) {
2844                 next_token(state, index + 1);
2845         }
2846         do {
2847                 rescan = 0;
2848                 if ((tk->tok == TOK_EOF) && 
2849                         (state->file != state->macro_file) &&
2850                         (state->file->prev)) {
2851                         struct file_state *file = state->file;
2852                         state->file = file->prev;
2853                         /* file->basename is used keep it */
2854                         if (file->report_dir != file->dirname) {
2855                                 xfree(file->report_dir);
2856                         }
2857                         xfree(file->dirname);
2858                         xfree(file->buf);
2859                         xfree(file);
2860                         next_token(state, index + 1);
2861                         rescan = 1;
2862                 }
2863                 else if (tk->ident && tk->ident->sym_define) {
2864                         compile_macro(state, tk);
2865                         next_token(state, index + 1);
2866                         rescan = 1;
2867                 }
2868         } while(rescan);
2869         /* Don't show the token on the next line */
2870         if (state->macro_line < state->macro_file->line) {
2871                 return TOK_EOF;
2872         }
2873         return state->token[index +1].tok;
2874 }
2875
2876 static void meat(struct compile_state *state, int index, int tok)
2877 {
2878         int next_tok;
2879         int i;
2880         next_tok = mpeek(state, index);
2881         if (next_tok != tok) {
2882                 const char *name1, *name2;
2883                 name1 = tokens[next_tok];
2884                 name2 = "";
2885                 if (next_tok == TOK_IDENT) {
2886                         name2 = state->token[index + 1].ident->name;
2887                 }
2888                 error(state, 0, "found %s %s expected %s", 
2889                         name1, name2, tokens[tok]);
2890         }
2891         /* Free the old token value */
2892         if (state->token[index].str_len) {
2893                 memset((void *)(state->token[index].val.str), -1, 
2894                         state->token[index].str_len);
2895                 xfree(state->token[index].val.str);
2896         }
2897         for(i = index; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
2898                 state->token[i] = state->token[i + 1];
2899         }
2900         memset(&state->token[i], 0, sizeof(state->token[i]));
2901         state->token[i].tok = -1;
2902 }
2903
2904 static long_t mcexpr(struct compile_state *state, int index);
2905
2906 static long_t mprimary_expr(struct compile_state *state, int index)
2907 {
2908         long_t val;
2909         int tok;
2910         tok = mpeek(state, index);
2911         while(state->token[index + 1].ident && 
2912                 state->token[index + 1].ident->sym_define) {
2913                 meat(state, index, tok);
2914                 compile_macro(state, &state->token[index]);
2915                 tok = mpeek(state, index);
2916         }
2917         switch(tok) {
2918         case TOK_LPAREN:
2919                 meat(state, index, TOK_LPAREN);
2920                 val = mcexpr(state, index);
2921                 meat(state, index, TOK_RPAREN);
2922                 break;
2923         case TOK_LIT_INT:
2924         {
2925                 char *end;
2926                 meat(state, index, TOK_LIT_INT);
2927                 errno = 0;
2928                 val = strtol(state->token[index].val.str, &end, 0);
2929                 if (((val == LONG_MIN) || (val == LONG_MAX)) &&
2930                         (errno == ERANGE)) {
2931                         error(state, 0, "Integer constant to large");
2932                 }
2933                 break;
2934         }
2935         default:
2936                 meat(state, index, TOK_LIT_INT);
2937                 val = 0;
2938         }
2939         return val;
2940 }
2941 static long_t munary_expr(struct compile_state *state, int index)
2942 {
2943         long_t val;
2944         switch(mpeek(state, index)) {
2945         case TOK_PLUS:
2946                 meat(state, index, TOK_PLUS);
2947                 val = munary_expr(state, index);
2948                 val = + val;
2949                 break;
2950         case TOK_MINUS:
2951                 meat(state, index, TOK_MINUS);
2952                 val = munary_expr(state, index);
2953                 val = - val;
2954                 break;
2955         case TOK_TILDE:
2956                 meat(state, index, TOK_BANG);
2957                 val = munary_expr(state, index);
2958                 val = ~ val;
2959                 break;
2960         case TOK_BANG:
2961                 meat(state, index, TOK_BANG);
2962                 val = munary_expr(state, index);
2963                 val = ! val;
2964                 break;
2965         default:
2966                 val = mprimary_expr(state, index);
2967                 break;
2968         }
2969         return val;
2970         
2971 }
2972 static long_t mmul_expr(struct compile_state *state, int index)
2973 {
2974         long_t val;
2975         int done;
2976         val = munary_expr(state, index);
2977         do {
2978                 long_t right;
2979                 done = 0;
2980                 switch(mpeek(state, index)) {
2981                 case TOK_STAR:
2982                         meat(state, index, TOK_STAR);
2983                         right = munary_expr(state, index);
2984                         val = val * right;
2985                         break;
2986                 case TOK_DIV:
2987                         meat(state, index, TOK_DIV);
2988                         right = munary_expr(state, index);
2989                         val = val / right;
2990                         break;
2991                 case TOK_MOD:
2992                         meat(state, index, TOK_MOD);
2993                         right = munary_expr(state, index);
2994                         val = val % right;
2995                         break;
2996                 default:
2997                         done = 1;
2998                         break;
2999                 }
3000         } while(!done);
3001
3002         return val;
3003 }
3004
3005 static long_t madd_expr(struct compile_state *state, int index)
3006 {
3007         long_t val;
3008         int done;
3009         val = mmul_expr(state, index);
3010         do {
3011                 long_t right;
3012                 done = 0;
3013                 switch(mpeek(state, index)) {
3014                 case TOK_PLUS:
3015                         meat(state, index, TOK_PLUS);
3016                         right = mmul_expr(state, index);
3017                         val = val + right;
3018                         break;
3019                 case TOK_MINUS:
3020                         meat(state, index, TOK_MINUS);
3021                         right = mmul_expr(state, index);
3022                         val = val - right;
3023                         break;
3024                 default:
3025                         done = 1;
3026                         break;
3027                 }
3028         } while(!done);
3029
3030         return val;
3031 }
3032
3033 static long_t mshift_expr(struct compile_state *state, int index)
3034 {
3035         long_t val;
3036         int done;
3037         val = madd_expr(state, index);
3038         do {
3039                 long_t right;
3040                 done = 0;
3041                 switch(mpeek(state, index)) {
3042                 case TOK_SL:
3043                         meat(state, index, TOK_SL);
3044                         right = madd_expr(state, index);
3045                         val = val << right;
3046                         break;
3047                 case TOK_SR:
3048                         meat(state, index, TOK_SR);
3049                         right = madd_expr(state, index);
3050                         val = val >> right;
3051                         break;
3052                 default:
3053                         done = 1;
3054                         break;
3055                 }
3056         } while(!done);
3057
3058         return val;
3059 }
3060
3061 static long_t mrel_expr(struct compile_state *state, int index)
3062 {
3063         long_t val;
3064         int done;
3065         val = mshift_expr(state, index);
3066         do {
3067                 long_t right;
3068                 done = 0;
3069                 switch(mpeek(state, index)) {
3070                 case TOK_LESS:
3071                         meat(state, index, TOK_LESS);
3072                         right = mshift_expr(state, index);
3073                         val = val < right;
3074                         break;
3075                 case TOK_MORE:
3076                         meat(state, index, TOK_MORE);
3077                         right = mshift_expr(state, index);
3078                         val = val > right;
3079                         break;
3080                 case TOK_LESSEQ:
3081                         meat(state, index, TOK_LESSEQ);
3082                         right = mshift_expr(state, index);
3083                         val = val <= right;
3084                         break;
3085                 case TOK_MOREEQ:
3086                         meat(state, index, TOK_MOREEQ);
3087                         right = mshift_expr(state, index);
3088                         val = val >= right;
3089                         break;
3090                 default:
3091                         done = 1;
3092                         break;
3093                 }
3094         } while(!done);
3095         return val;
3096 }
3097
3098 static long_t meq_expr(struct compile_state *state, int index)
3099 {
3100         long_t val;
3101         int done;
3102         val = mrel_expr(state, index);
3103         do {
3104                 long_t right;
3105                 done = 0;
3106                 switch(mpeek(state, index)) {
3107                 case TOK_EQEQ:
3108                         meat(state, index, TOK_EQEQ);
3109                         right = mrel_expr(state, index);
3110                         val = val == right;
3111                         break;
3112                 case TOK_NOTEQ:
3113                         meat(state, index, TOK_NOTEQ);
3114                         right = mrel_expr(state, index);
3115                         val = val != right;
3116                         break;
3117                 default:
3118                         done = 1;
3119                         break;
3120                 }
3121         } while(!done);
3122         return val;
3123 }
3124
3125 static long_t mand_expr(struct compile_state *state, int index)
3126 {
3127         long_t val;
3128         val = meq_expr(state, index);
3129         if (mpeek(state, index) == TOK_AND) {
3130                 long_t right;
3131                 meat(state, index, TOK_AND);
3132                 right = meq_expr(state, index);
3133                 val = val & right;
3134         }
3135         return val;
3136 }
3137
3138 static long_t mxor_expr(struct compile_state *state, int index)
3139 {
3140         long_t val;
3141         val = mand_expr(state, index);
3142         if (mpeek(state, index) == TOK_XOR) {
3143                 long_t right;
3144                 meat(state, index, TOK_XOR);
3145                 right = mand_expr(state, index);
3146                 val = val ^ right;
3147         }
3148         return val;
3149 }
3150
3151 static long_t mor_expr(struct compile_state *state, int index)
3152 {
3153         long_t val;
3154         val = mxor_expr(state, index);
3155         if (mpeek(state, index) == TOK_OR) {
3156                 long_t right;
3157                 meat(state, index, TOK_OR);
3158                 right = mxor_expr(state, index);
3159                 val = val | right;
3160         }
3161         return val;
3162 }
3163
3164 static long_t mland_expr(struct compile_state *state, int index)
3165 {
3166         long_t val;
3167         val = mor_expr(state, index);
3168         if (mpeek(state, index) == TOK_LOGAND) {
3169                 long_t right;
3170                 meat(state, index, TOK_LOGAND);
3171                 right = mor_expr(state, index);
3172                 val = val && right;
3173         }
3174         return val;
3175 }
3176 static long_t mlor_expr(struct compile_state *state, int index)
3177 {
3178         long_t val;
3179         val = mland_expr(state, index);
3180         if (mpeek(state, index) == TOK_LOGOR) {
3181                 long_t right;
3182                 meat(state, index, TOK_LOGOR);
3183                 right = mland_expr(state, index);
3184                 val = val || right;
3185         }
3186         return val;
3187 }
3188
3189 static long_t mcexpr(struct compile_state *state, int index)
3190 {
3191         return mlor_expr(state, index);
3192 }
3193 static void preprocess(struct compile_state *state, int index)
3194 {
3195         /* Doing much more with the preprocessor would require
3196          * a parser and a major restructuring.
3197          * Postpone that for later.
3198          */
3199         struct file_state *file;
3200         struct token *tk;
3201         int line;
3202         int tok;
3203         
3204         file = state->file;
3205         tk = &state->token[index];
3206         state->macro_line = line = file->line;
3207         state->macro_file = file;
3208
3209         next_token(state, index);
3210         ident_to_macro(state, tk);
3211         if (tk->tok == TOK_IDENT) {
3212                 error(state, 0, "undefined preprocessing directive `%s'",
3213                         tk->ident->name);
3214         }
3215         switch(tk->tok) {
3216         case TOK_LIT_INT:
3217         {
3218                 int override_line;
3219                 override_line = strtoul(tk->val.str, 0, 10);
3220                 next_token(state, index);
3221                 /* I have a cpp line marker parse it */
3222                 if (tk->tok == TOK_LIT_STRING) {
3223                         const char *token, *base;
3224                         char *name, *dir;
3225                         int name_len, dir_len;
3226                         name = xmalloc(tk->str_len, "report_name");
3227                         token = tk->val.str + 1;
3228                         base = strrchr(token, '/');
3229                         name_len = tk->str_len -2;
3230                         if (base != 0) {
3231                                 dir_len = base - token;
3232                                 base++;
3233                                 name_len -= base - token;
3234                         } else {
3235                                 dir_len = 0;
3236                                 base = token;
3237                         }
3238                         memcpy(name, base, name_len);
3239                         name[name_len] = '\0';
3240                         dir = xmalloc(dir_len + 1, "report_dir");
3241                         memcpy(dir, token, dir_len);
3242                         dir[dir_len] = '\0';
3243                         file->report_line = override_line - 1;
3244                         file->report_name = name;
3245                         file->report_dir = dir;
3246                 }
3247         }
3248                 break;
3249         case TOK_LINE:
3250                 meat(state, index, TOK_LINE);
3251                 meat(state, index, TOK_LIT_INT);
3252                 file->report_line = strtoul(tk->val.str, 0, 10) -1;
3253                 if (mpeek(state, index) == TOK_LIT_STRING) {
3254                         const char *token, *base;
3255                         char *name, *dir;
3256                         int name_len, dir_len;
3257                         meat(state, index, TOK_LIT_STRING);
3258                         name = xmalloc(tk->str_len, "report_name");
3259                         token = tk->val.str + 1;
3260                         name_len = tk->str_len - 2;
3261                         if (base != 0) {
3262                                 dir_len = base - token;
3263                                 base++;
3264                                 name_len -= base - token;
3265                         } else {
3266                                 dir_len = 0;
3267                                 base = token;
3268                         }
3269                         memcpy(name, base, name_len);
3270                         name[name_len] = '\0';
3271                         dir = xmalloc(dir_len + 1, "report_dir");
3272                         memcpy(dir, token, dir_len);
3273                         dir[dir_len] = '\0';
3274                         file->report_name = name;
3275                         file->report_dir = dir;
3276                 }
3277                 break;
3278         case TOK_UNDEF:
3279         case TOK_PRAGMA:
3280                 if (state->if_value < 0) {
3281                         break;
3282                 }
3283                 warning(state, 0, "Ignoring preprocessor directive: %s", 
3284                         tk->ident->name);
3285                 break;
3286         case TOK_ELIF:
3287                 error(state, 0, "#elif not supported");
3288 #warning "FIXME multiple #elif and #else in an #if do not work properly"
3289                 if (state->if_depth == 0) {
3290                         error(state, 0, "#elif without #if");
3291                 }
3292                 /* If the #if was taken the #elif just disables the following code */
3293                 if (state->if_value >= 0) {
3294                         state->if_value = - state->if_value;
3295                 }
3296                 /* If the previous #if was not taken see if the #elif enables the 
3297                  * trailing code.
3298                  */
3299                 else if ((state->if_value < 0) && 
3300                         (state->if_depth == - state->if_value))
3301                 {
3302                         if (mcexpr(state, index) != 0) {
3303                                 state->if_value = state->if_depth;
3304                         }
3305                         else {
3306                                 state->if_value = - state->if_depth;
3307                         }
3308                 }
3309                 break;
3310         case TOK_IF:
3311                 state->if_depth++;
3312                 if (state->if_value < 0) {
3313                         break;
3314                 }
3315                 if (mcexpr(state, index) != 0) {
3316                         state->if_value = state->if_depth;
3317                 }
3318                 else {
3319                         state->if_value = - state->if_depth;
3320                 }
3321                 break;
3322         case TOK_IFNDEF:
3323                 state->if_depth++;
3324                 if (state->if_value < 0) {
3325                         break;
3326                 }
3327                 next_token(state, index);
3328                 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3329                         error(state, 0, "Invalid macro name");
3330                 }
3331                 if (tk->ident->sym_define == 0) {
3332                         state->if_value = state->if_depth;
3333                 } 
3334                 else {
3335                         state->if_value = - state->if_depth;
3336                 }
3337                 break;
3338         case TOK_IFDEF:
3339                 state->if_depth++;
3340                 if (state->if_value < 0) {
3341                         break;
3342                 }
3343                 next_token(state, index);
3344                 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3345                         error(state, 0, "Invalid macro name");
3346                 }
3347                 if (tk->ident->sym_define != 0) {
3348                         state->if_value = state->if_depth;
3349                 }
3350                 else {
3351                         state->if_value = - state->if_depth;
3352                 }
3353                 break;
3354         case TOK_ELSE:
3355                 if (state->if_depth == 0) {
3356                         error(state, 0, "#else without #if");
3357                 }
3358                 if ((state->if_value >= 0) ||
3359                         ((state->if_value < 0) && 
3360                                 (state->if_depth == -state->if_value)))
3361                 {
3362                         state->if_value = - state->if_value;
3363                 }
3364                 break;
3365         case TOK_ENDIF:
3366                 if (state->if_depth == 0) {
3367                         error(state, 0, "#endif without #if");
3368                 }
3369                 if ((state->if_value >= 0) ||
3370                         ((state->if_value < 0) &&
3371                                 (state->if_depth == -state->if_value))) 
3372                 {
3373                         state->if_value = state->if_depth - 1;
3374                 }
3375                 state->if_depth--;
3376                 break;
3377         case TOK_DEFINE:
3378         {
3379                 struct hash_entry *ident;
3380                 struct macro *macro;
3381                 char *ptr;
3382                 
3383                 if (state->if_value < 0) /* quit early when #if'd out */
3384                         break;
3385
3386                 meat(state, index, TOK_IDENT);
3387                 ident = tk->ident;
3388                 
3389
3390                 if (*file->pos == '(') {
3391 #warning "FIXME macros with arguments not supported"
3392                         error(state, 0, "Macros with arguments not supported");
3393                 }
3394
3395                 /* Find the end of the line to get an estimate of
3396                  * the macro's length.
3397                  */
3398                 for(ptr = file->pos; *ptr != '\n'; ptr++)  
3399                         ;
3400
3401                 if (ident->sym_define != 0) {
3402                         error(state, 0, "macro %s already defined\n", ident->name);
3403                 }
3404                 macro = xmalloc(sizeof(*macro), "macro");
3405                 macro->ident = ident;
3406                 macro->buf_len = ptr - file->pos +1;
3407                 macro->buf = xmalloc(macro->buf_len +2, "macro buf");
3408
3409                 memcpy(macro->buf, file->pos, macro->buf_len);
3410                 macro->buf[macro->buf_len] = '\n';
3411                 macro->buf[macro->buf_len +1] = '\0';
3412
3413                 ident->sym_define = macro;
3414                 break;
3415         }
3416         case TOK_ERROR:
3417         {
3418                 char *end;
3419                 int len;
3420                 /* Find the end of the line */
3421                 for(end = file->pos; *end != '\n'; end++)
3422                         ;
3423                 len = (end - file->pos);
3424                 if (state->if_value >= 0) {
3425                         error(state, 0, "%*.*s", len, len, file->pos);
3426                 }
3427                 file->pos = end;
3428                 break;
3429         }
3430         case TOK_WARNING:
3431         {
3432                 char *end;
3433                 int len;
3434                 /* Find the end of the line */
3435                 for(end = file->pos; *end != '\n'; end++)
3436                         ;
3437                 len = (end - file->pos);
3438                 if (state->if_value >= 0) {
3439                         warning(state, 0, "%*.*s", len, len, file->pos);
3440                 }
3441                 file->pos = end;
3442                 break;
3443         }
3444         case TOK_INCLUDE:
3445         {
3446                 char *name;
3447                 char *ptr;
3448                 int local;
3449                 local = 0;
3450                 name = 0;
3451                 next_token(state, index);
3452                 if (tk->tok == TOK_LIT_STRING) {
3453                         const char *token;
3454                         int name_len;
3455                         name = xmalloc(tk->str_len, "include");
3456                         token = tk->val.str +1;
3457                         name_len = tk->str_len -2;
3458                         if (*token == '"') {
3459                                 token++;
3460                                 name_len--;
3461                         }
3462                         memcpy(name, token, name_len);
3463                         name[name_len] = '\0';
3464                         local = 1;
3465                 }
3466                 else if (tk->tok == TOK_LESS) {
3467                         char *start, *end;
3468                         start = file->pos;
3469                         for(end = start; *end != '\n'; end++) {
3470                                 if (*end == '>') {
3471                                         break;
3472                                 }
3473                         }
3474                         if (*end == '\n') {
3475                                 error(state, 0, "Unterminated included directive");
3476                         }
3477                         name = xmalloc(end - start + 1, "include");
3478                         memcpy(name, start, end - start);
3479                         name[end - start] = '\0';
3480                         file->pos = end +1;
3481                         local = 0;
3482                 }
3483                 else {
3484                         error(state, 0, "Invalid include directive");
3485                 }
3486                 /* Error if there are any characters after the include */
3487                 for(ptr = file->pos; *ptr != '\n'; ptr++) {
3488                         switch(*ptr) {
3489                         case ' ':
3490                         case '\t':
3491                         case '\v':
3492                                 break;
3493                         default:
3494                                 error(state, 0, "garbage after include directive");
3495                         }
3496                 }
3497                 if (state->if_value >= 0) {
3498                         compile_file(state, name, local);
3499                 }
3500                 xfree(name);
3501                 next_token(state, index);
3502                 return;
3503         }
3504         default:
3505                 /* Ignore # without a following ident */
3506                 if (tk->tok == TOK_IDENT) {
3507                         error(state, 0, "Invalid preprocessor directive: %s", 
3508                                 tk->ident->name);
3509                 }
3510                 break;
3511         }
3512         /* Consume the rest of the macro line */
3513         do {
3514                 tok = mpeek(state, index);
3515                 meat(state, index, tok);
3516         } while(tok != TOK_EOF);
3517         return;
3518 }
3519
3520 static void token(struct compile_state *state, int index)
3521 {
3522         struct file_state *file;
3523         struct token *tk;
3524         int rescan;
3525
3526         tk = &state->token[index];
3527         next_token(state, index);
3528         do {
3529                 rescan = 0;
3530                 file = state->file;
3531                 if (tk->tok == TOK_EOF && file->prev) {
3532                         state->file = file->prev;
3533                         /* file->basename is used keep it */
3534                         xfree(file->dirname);
3535                         xfree(file->buf);
3536                         xfree(file);
3537                         next_token(state, index);
3538                         rescan = 1;
3539                 }
3540                 else if (tk->tok == TOK_MACRO) {
3541                         preprocess(state, index);
3542                         rescan = 1;
3543                 }
3544                 else if (tk->ident && tk->ident->sym_define) {
3545                         compile_macro(state, tk);
3546                         next_token(state, index);
3547                         rescan = 1;
3548                 }
3549                 else if (state->if_value < 0) {
3550                         next_token(state, index);
3551                         rescan = 1;
3552                 }
3553         } while(rescan);
3554 }
3555
3556 static int peek(struct compile_state *state)
3557 {
3558         if (state->token[1].tok == -1) {
3559                 token(state, 1);
3560         }
3561         return state->token[1].tok;
3562 }
3563
3564 static int peek2(struct compile_state *state)
3565 {
3566         if (state->token[1].tok == -1) {
3567                 token(state, 1);
3568         }
3569         if (state->token[2].tok == -1) {
3570                 token(state, 2);
3571         }
3572         return state->token[2].tok;
3573 }
3574
3575 static void eat(struct compile_state *state, int tok)
3576 {
3577         int next_tok;
3578         int i;
3579         next_tok = peek(state);
3580         if (next_tok != tok) {
3581                 const char *name1, *name2;
3582                 name1 = tokens[next_tok];
3583                 name2 = "";
3584                 if (next_tok == TOK_IDENT) {
3585                         name2 = state->token[1].ident->name;
3586                 }
3587                 error(state, 0, "\tfound %s %s expected %s",
3588                         name1, name2 ,tokens[tok]);
3589         }
3590         /* Free the old token value */
3591         if (state->token[0].str_len) {
3592                 xfree((void *)(state->token[0].val.str));
3593         }
3594         for(i = 0; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
3595                 state->token[i] = state->token[i + 1];
3596         }
3597         memset(&state->token[i], 0, sizeof(state->token[i]));
3598         state->token[i].tok = -1;
3599 }
3600
3601 #warning "FIXME do not hardcode the include paths"
3602 static char *include_paths[] = {
3603         "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/include",
3604         "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/arch/i386/include",
3605         "/home/eric/projects/linuxbios/checkin/solo/freebios2/src",
3606         0
3607 };
3608
3609 static void compile_file(struct compile_state *state, const char *filename, int local)
3610 {
3611         char cwd[4096];
3612         const char *subdir, *base;
3613         int subdir_len;
3614         struct file_state *file;
3615         char *basename;
3616         file = xmalloc(sizeof(*file), "file_state");
3617
3618         base = strrchr(filename, '/');
3619         subdir = filename;
3620         if (base != 0) {
3621                 subdir_len = base - filename;
3622                 base++;
3623         }
3624         else {
3625                 base = filename;
3626                 subdir_len = 0;
3627         }
3628         basename = xmalloc(strlen(base) +1, "basename");
3629         strcpy(basename, base);
3630         file->basename = basename;
3631
3632         if (getcwd(cwd, sizeof(cwd)) == 0) {
3633                 die("cwd buffer to small");
3634         }
3635         
3636         if (subdir[0] == '/') {
3637                 file->dirname = xmalloc(subdir_len + 1, "dirname");
3638                 memcpy(file->dirname, subdir, subdir_len);
3639                 file->dirname[subdir_len] = '\0';
3640         }
3641         else {
3642                 char *dir;
3643                 int dirlen;
3644                 char **path;
3645                 /* Find the appropriate directory... */
3646                 dir = 0;
3647                 if (!state->file && exists(cwd, filename)) {
3648                         dir = cwd;
3649                 }
3650                 if (local && state->file && exists(state->file->dirname, filename)) {
3651                         dir = state->file->dirname;
3652                 }
3653                 for(path = include_paths; !dir && *path; path++) {
3654                         if (exists(*path, filename)) {
3655                                 dir = *path;
3656                         }
3657                 }
3658                 if (!dir) {
3659                         error(state, 0, "Cannot find `%s'\n", filename);
3660                 }
3661                 dirlen = strlen(dir);
3662                 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
3663                 memcpy(file->dirname, dir, dirlen);
3664                 file->dirname[dirlen] = '/';
3665                 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
3666                 file->dirname[dirlen + 1 + subdir_len] = '\0';
3667         }
3668         file->buf = slurp_file(file->dirname, file->basename, &file->size);
3669         xchdir(cwd);
3670
3671         file->pos = file->buf;
3672         file->line_start = file->pos;
3673         file->line = 1;
3674
3675         file->report_line = 1;
3676         file->report_name = file->basename;
3677         file->report_dir  = file->dirname;
3678
3679         file->prev = state->file;
3680         state->file = file;
3681         
3682         process_trigraphs(state);
3683         splice_lines(state);
3684 }
3685
3686 /* Type helper functions */
3687
3688 static struct type *new_type(
3689         unsigned int type, struct type *left, struct type *right)
3690 {
3691         struct type *result;
3692         result = xmalloc(sizeof(*result), "type");
3693         result->type = type;
3694         result->left = left;
3695         result->right = right;
3696         result->field_ident = 0;
3697         result->type_ident = 0;
3698         return result;
3699 }
3700
3701 static struct type *clone_type(unsigned int specifiers, struct type *old)
3702 {
3703         struct type *result;
3704         result = xmalloc(sizeof(*result), "type");
3705         memcpy(result, old, sizeof(*result));
3706         result->type &= TYPE_MASK;
3707         result->type |= specifiers;
3708         return result;
3709 }
3710
3711 #define SIZEOF_SHORT 2
3712 #define SIZEOF_INT   4
3713 #define SIZEOF_LONG  (sizeof(long_t))
3714
3715 #define ALIGNOF_SHORT 2
3716 #define ALIGNOF_INT   4
3717 #define ALIGNOF_LONG  (sizeof(long_t))
3718
3719 #define MASK_UCHAR(X)    ((X) & ((ulong_t)0xff))
3720 #define MASK_USHORT(X)   ((X) & (((ulong_t)1 << (SIZEOF_SHORT*8)) - 1))
3721 static inline ulong_t mask_uint(ulong_t x)
3722 {
3723         if (SIZEOF_INT < SIZEOF_LONG) {
3724                 ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT*8))) -1;
3725                 x &= mask;
3726         }
3727         return x;
3728 }
3729 #define MASK_UINT(X)      (mask_uint(X))
3730 #define MASK_ULONG(X)    (X)
3731
3732 static struct type void_type   = { .type  = TYPE_VOID };
3733 static struct type char_type   = { .type  = TYPE_CHAR };
3734 static struct type uchar_type  = { .type  = TYPE_UCHAR };
3735 static struct type short_type  = { .type  = TYPE_SHORT };
3736 static struct type ushort_type = { .type  = TYPE_USHORT };
3737 static struct type int_type    = { .type  = TYPE_INT };
3738 static struct type uint_type   = { .type  = TYPE_UINT };
3739 static struct type long_type   = { .type  = TYPE_LONG };
3740 static struct type ulong_type  = { .type  = TYPE_ULONG };
3741
3742 static struct triple *variable(struct compile_state *state, struct type *type)
3743 {
3744         struct triple *result;
3745         if ((type->type & STOR_MASK) != STOR_PERM) {
3746                 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3747                         result = triple(state, OP_ADECL, type, 0, 0);
3748                 } else {
3749                         struct type *field;
3750                         struct triple **vector;
3751                         ulong_t index;
3752                         result = new_triple(state, OP_VAL_VEC, type, -1, -1);
3753                         vector = &result->param[0];
3754
3755                         field = type->left;
3756                         index = 0;
3757                         while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
3758                                 vector[index] = variable(state, field->left);
3759                                 field = field->right;
3760                                 index++;
3761                         }
3762                         vector[index] = variable(state, field);
3763                 }
3764         }
3765         else {
3766                 result = triple(state, OP_SDECL, type, 0, 0);
3767         }
3768         return result;
3769 }
3770
3771 static void stor_of(FILE *fp, struct type *type)
3772 {
3773         switch(type->type & STOR_MASK) {
3774         case STOR_AUTO:
3775                 fprintf(fp, "auto ");
3776                 break;
3777         case STOR_STATIC:
3778                 fprintf(fp, "static ");
3779                 break;
3780         case STOR_EXTERN:
3781                 fprintf(fp, "extern ");
3782                 break;
3783         case STOR_REGISTER:
3784                 fprintf(fp, "register ");
3785                 break;
3786         case STOR_TYPEDEF:
3787                 fprintf(fp, "typedef ");
3788                 break;
3789         case STOR_INLINE:
3790                 fprintf(fp, "inline ");
3791                 break;
3792         }
3793 }
3794 static void qual_of(FILE *fp, struct type *type)
3795 {
3796         if (type->type & QUAL_CONST) {
3797                 fprintf(fp, " const");
3798         }
3799         if (type->type & QUAL_VOLATILE) {
3800                 fprintf(fp, " volatile");
3801         }
3802         if (type->type & QUAL_RESTRICT) {
3803                 fprintf(fp, " restrict");
3804         }
3805 }
3806
3807 static void name_of(FILE *fp, struct type *type)
3808 {
3809         stor_of(fp, type);
3810         switch(type->type & TYPE_MASK) {
3811         case TYPE_VOID:
3812                 fprintf(fp, "void");
3813                 qual_of(fp, type);
3814                 break;
3815         case TYPE_CHAR:
3816                 fprintf(fp, "signed char");
3817                 qual_of(fp, type);
3818                 break;
3819         case TYPE_UCHAR:
3820                 fprintf(fp, "unsigned char");
3821                 qual_of(fp, type);
3822                 break;
3823         case TYPE_SHORT:
3824                 fprintf(fp, "signed short");
3825                 qual_of(fp, type);
3826                 break;
3827         case TYPE_USHORT:
3828                 fprintf(fp, "unsigned short");
3829                 qual_of(fp, type);
3830                 break;
3831         case TYPE_INT:
3832                 fprintf(fp, "signed int");
3833                 qual_of(fp, type);
3834                 break;
3835         case TYPE_UINT:
3836                 fprintf(fp, "unsigned int");
3837                 qual_of(fp, type);
3838                 break;
3839         case TYPE_LONG:
3840                 fprintf(fp, "signed long");
3841                 qual_of(fp, type);
3842                 break;
3843         case TYPE_ULONG:
3844                 fprintf(fp, "unsigned long");
3845                 qual_of(fp, type);
3846                 break;
3847         case TYPE_POINTER:
3848                 name_of(fp, type->left);
3849                 fprintf(fp, " * ");
3850                 qual_of(fp, type);
3851                 break;
3852         case TYPE_PRODUCT:
3853         case TYPE_OVERLAP:
3854                 name_of(fp, type->left);
3855                 fprintf(fp, ", ");
3856                 name_of(fp, type->right);
3857                 break;
3858         case TYPE_ENUM:
3859                 fprintf(fp, "enum %s", type->type_ident->name);
3860                 qual_of(fp, type);
3861                 break;
3862         case TYPE_STRUCT:
3863                 fprintf(fp, "struct %s", type->type_ident->name);
3864                 qual_of(fp, type);
3865                 break;
3866         case TYPE_FUNCTION:
3867         {
3868                 name_of(fp, type->left);
3869                 fprintf(fp, " (*)(");
3870                 name_of(fp, type->right);
3871                 fprintf(fp, ")");
3872                 break;
3873         }
3874         case TYPE_ARRAY:
3875                 name_of(fp, type->left);
3876                 fprintf(fp, " [%ld]", type->elements);
3877                 break;
3878         default:
3879                 fprintf(fp, "????: %x", type->type & TYPE_MASK);
3880                 break;
3881         }
3882 }
3883
3884 static size_t align_of(struct compile_state *state, struct type *type)
3885 {
3886         size_t align;
3887         align = 0;
3888         switch(type->type & TYPE_MASK) {
3889         case TYPE_VOID:
3890                 align = 1;
3891                 break;
3892         case TYPE_CHAR:
3893         case TYPE_UCHAR:
3894                 align = 1;
3895                 break;
3896         case TYPE_SHORT:
3897         case TYPE_USHORT:
3898                 align = ALIGNOF_SHORT;
3899                 break;
3900         case TYPE_INT:
3901         case TYPE_UINT:
3902         case TYPE_ENUM:
3903                 align = ALIGNOF_INT;
3904                 break;
3905         case TYPE_LONG:
3906         case TYPE_ULONG:
3907         case TYPE_POINTER:
3908                 align = ALIGNOF_LONG;
3909                 break;
3910         case TYPE_PRODUCT:
3911         case TYPE_OVERLAP:
3912         {
3913                 size_t left_align, right_align;
3914                 left_align  = align_of(state, type->left);
3915                 right_align = align_of(state, type->right);
3916                 align = (left_align >= right_align) ? left_align : right_align;
3917                 break;
3918         }
3919         case TYPE_ARRAY:
3920                 align = align_of(state, type->left);
3921                 break;
3922         case TYPE_STRUCT:
3923                 align = align_of(state, type->left);
3924                 break;
3925         default:
3926                 error(state, 0, "alignof not yet defined for type\n");
3927                 break;
3928         }
3929         return align;
3930 }
3931
3932 static size_t size_of(struct compile_state *state, struct type *type)
3933 {
3934         size_t size;
3935         size = 0;
3936         switch(type->type & TYPE_MASK) {
3937         case TYPE_VOID:
3938                 size = 0;
3939                 break;
3940         case TYPE_CHAR:
3941         case TYPE_UCHAR:
3942                 size = 1;
3943                 break;
3944         case TYPE_SHORT:
3945         case TYPE_USHORT:
3946                 size = SIZEOF_SHORT;
3947                 break;
3948         case TYPE_INT:
3949         case TYPE_UINT:
3950         case TYPE_ENUM:
3951                 size = SIZEOF_INT;
3952                 break;
3953         case TYPE_LONG:
3954         case TYPE_ULONG:
3955         case TYPE_POINTER:
3956                 size = SIZEOF_LONG;
3957                 break;
3958         case TYPE_PRODUCT:
3959         {
3960                 size_t align, pad;
3961                 size = size_of(state, type->left);
3962                 while((type->right->type & TYPE_MASK) == TYPE_PRODUCT) {
3963                         type = type->right;
3964                         align = align_of(state, type->left);
3965                         pad = align - (size % align);
3966                         size = size + pad + size_of(state, type->left);
3967                 }
3968                 align = align_of(state, type->right);
3969                 pad = align - (size % align);
3970                 size = size + pad + sizeof(type->right);
3971                 break;
3972         }
3973         case TYPE_OVERLAP:
3974         {
3975                 size_t size_left, size_right;
3976                 size_left = size_of(state, type->left);
3977                 size_right = size_of(state, type->right);
3978                 size = (size_left >= size_right)? size_left : size_right;
3979                 break;
3980         }
3981         case TYPE_ARRAY:
3982                 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
3983                         internal_error(state, 0, "Invalid array type");
3984                 } else {
3985                         size = size_of(state, type->left) * type->elements;
3986                 }
3987                 break;
3988         case TYPE_STRUCT:
3989                 size = size_of(state, type->left);
3990                 break;
3991         default:
3992                 error(state, 0, "sizeof not yet defined for type\n");
3993                 break;
3994         }
3995         return size;
3996 }
3997
3998 static size_t field_offset(struct compile_state *state, 
3999         struct type *type, struct hash_entry *field)
4000 {
4001         size_t size, align, pad;
4002         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4003                 internal_error(state, 0, "field_offset only works on structures");
4004         }
4005         size = 0;
4006         type = type->left;
4007         while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4008                 if (type->left->field_ident == field) {
4009                         type = type->left;
4010                 }
4011                 size += size_of(state, type->left);
4012                 type = type->right;
4013                 align = align_of(state, type->left);
4014                 pad = align - (size % align);
4015                 size += pad;
4016         }
4017         if (type->field_ident != field) {
4018                 internal_error(state, 0, "field_offset: member %s not present",
4019                         field->name);
4020         }
4021         return size;
4022 }
4023
4024 static struct type *field_type(struct compile_state *state, 
4025         struct type *type, struct hash_entry *field)
4026 {
4027         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4028                 internal_error(state, 0, "field_type only works on structures");
4029         }
4030         type = type->left;
4031         while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4032                 if (type->left->field_ident == field) {
4033                         type = type->left;
4034                         break;
4035                 }
4036                 type = type->right;
4037         }
4038         if (type->field_ident != field) {
4039                 internal_error(state, 0, "field_type: member %s not present", 
4040                         field->name);
4041         }
4042         return type;
4043 }
4044
4045 static struct triple *struct_field(struct compile_state *state,
4046         struct triple *decl, struct hash_entry *field)
4047 {
4048         struct triple **vector;
4049         struct type *type;
4050         ulong_t index;
4051         type = decl->type;
4052         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4053                 return decl;
4054         }
4055         if (decl->op != OP_VAL_VEC) {
4056                 internal_error(state, 0, "Invalid struct variable");
4057         }
4058         if (!field) {
4059                 internal_error(state, 0, "Missing structure field");
4060         }
4061         type = type->left;
4062         vector = &RHS(decl, 0);
4063         index = 0;
4064         while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4065                 if (type->left->field_ident == field) {
4066                         type = type->left;
4067                         break;
4068                 }
4069                 index += 1;
4070                 type = type->right;
4071         }
4072         if (type->field_ident != field) {
4073                 internal_error(state, 0, "field %s not found?", field->name);
4074         }
4075         return vector[index];
4076 }
4077
4078 static void arrays_complete(struct compile_state *state, struct type *type)
4079 {
4080         if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
4081                 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4082                         error(state, 0, "array size not specified");
4083                 }
4084                 arrays_complete(state, type->left);
4085         }
4086 }
4087
4088 static unsigned int do_integral_promotion(unsigned int type)
4089 {
4090         type &= TYPE_MASK;
4091         if (TYPE_INTEGER(type) && 
4092                 TYPE_RANK(type) < TYPE_RANK(TYPE_INT)) {
4093                 type = TYPE_INT;
4094         }
4095         return type;
4096 }
4097
4098 static unsigned int do_arithmetic_conversion(
4099         unsigned int left, unsigned int right)
4100 {
4101         left &= TYPE_MASK;
4102         right &= TYPE_MASK;
4103         if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
4104                 return TYPE_LDOUBLE;
4105         }
4106         else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
4107                 return TYPE_DOUBLE;
4108         }
4109         else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
4110                 return TYPE_FLOAT;
4111         }
4112         left = do_integral_promotion(left);
4113         right = do_integral_promotion(right);
4114         /* If both operands have the same size done */
4115         if (left == right) {
4116                 return left;
4117         }
4118         /* If both operands have the same signedness pick the larger */
4119         else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
4120                 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
4121         }
4122         /* If the signed type can hold everything use it */
4123         else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
4124                 return left;
4125         }
4126         else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
4127                 return right;
4128         }
4129         /* Convert to the unsigned type with the same rank as the signed type */
4130         else if (TYPE_SIGNED(left)) {
4131                 return TYPE_MKUNSIGNED(left);
4132         }
4133         else {
4134                 return TYPE_MKUNSIGNED(right);
4135         }
4136 }
4137
4138 /* see if two types are the same except for qualifiers */
4139 static int equiv_types(struct type *left, struct type *right)
4140 {
4141         unsigned int type;
4142         /* Error if the basic types do not match */
4143         if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4144                 return 0;
4145         }
4146         type = left->type & TYPE_MASK;
4147         /* if the basic types match and it is an arithmetic type we are done */
4148         if (TYPE_ARITHMETIC(type)) {
4149                 return 1;
4150         }
4151         /* If it is a pointer type recurse and keep testing */
4152         if (type == TYPE_POINTER) {
4153                 return equiv_types(left->left, right->left);
4154         }
4155         else if (type == TYPE_ARRAY) {
4156                 return (left->elements == right->elements) &&
4157                         equiv_types(left->left, right->left);
4158         }
4159         /* test for struct/union equality */
4160         else if (type == TYPE_STRUCT) {
4161                 return left->type_ident == right->type_ident;
4162         }
4163         /* Test for equivalent functions */
4164         else if (type == TYPE_FUNCTION) {
4165                 return equiv_types(left->left, right->left) &&
4166                         equiv_types(left->right, right->right);
4167         }
4168         /* We only see TYPE_PRODUCT as part of function equivalence matching */
4169         else if (type == TYPE_PRODUCT) {
4170                 return equiv_types(left->left, right->left) &&
4171                         equiv_types(left->right, right->right);
4172         }
4173         /* We should see TYPE_OVERLAP */
4174         else {
4175                 return 0;
4176         }
4177 }
4178
4179 static int equiv_ptrs(struct type *left, struct type *right)
4180 {
4181         if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4182                 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4183                 return 0;
4184         }
4185         return equiv_types(left->left, right->left);
4186 }
4187
4188 static struct type *compatible_types(struct type *left, struct type *right)
4189 {
4190         struct type *result;
4191         unsigned int type, qual_type;
4192         /* Error if the basic types do not match */
4193         if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4194                 return 0;
4195         }
4196         type = left->type & TYPE_MASK;
4197         qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4198         result = 0;
4199         /* if the basic types match and it is an arithmetic type we are done */
4200         if (TYPE_ARITHMETIC(type)) {
4201                 result = new_type(qual_type, 0, 0);
4202         }
4203         /* If it is a pointer type recurse and keep testing */
4204         else if (type == TYPE_POINTER) {
4205                 result = compatible_types(left->left, right->left);
4206                 if (result) {
4207                         result = new_type(qual_type, result, 0);
4208                 }
4209         }
4210         /* test for struct/union equality */
4211         else if (type == TYPE_STRUCT) {
4212                 if (left->type_ident == right->type_ident) {
4213                         result = left;
4214                 }
4215         }
4216         /* Test for equivalent functions */
4217         else if (type == TYPE_FUNCTION) {
4218                 struct type *lf, *rf;
4219                 lf = compatible_types(left->left, right->left);
4220                 rf = compatible_types(left->right, right->right);
4221                 if (lf && rf) {
4222                         result = new_type(qual_type, lf, rf);
4223                 }
4224         }
4225         /* We only see TYPE_PRODUCT as part of function equivalence matching */
4226         else if (type == TYPE_PRODUCT) {
4227                 struct type *lf, *rf;
4228                 lf = compatible_types(left->left, right->left);
4229                 rf = compatible_types(left->right, right->right);
4230                 if (lf && rf) {
4231                         result = new_type(qual_type, lf, rf);
4232                 }
4233         }
4234         else {
4235                 /* Nothing else is compatible */
4236         }
4237         return result;
4238 }
4239
4240 static struct type *compatible_ptrs(struct type *left, struct type *right)
4241 {
4242         struct type *result;
4243         if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4244                 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4245                 return 0;
4246         }
4247         result = compatible_types(left->left, right->left);
4248         if (result) {
4249                 unsigned int qual_type;
4250                 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4251                 result = new_type(qual_type, result, 0);
4252         }
4253         return result;
4254         
4255 }
4256 static struct triple *integral_promotion(
4257         struct compile_state *state, struct triple *def)
4258 {
4259         struct type *type;
4260         type = def->type;
4261         /* As all operations are carried out in registers
4262          * the values are converted on load I just convert
4263          * logical type of the operand.
4264          */
4265         if (TYPE_INTEGER(type->type)) {
4266                 unsigned int int_type;
4267                 int_type = type->type & ~TYPE_MASK;
4268                 int_type |= do_integral_promotion(type->type);
4269                 if (int_type != type->type) {
4270                         def->type = new_type(int_type, 0, 0);
4271                 }
4272         }
4273         return def;
4274 }
4275
4276
4277 static void arithmetic(struct compile_state *state, struct triple *def)
4278 {
4279         if (!TYPE_ARITHMETIC(def->type->type)) {
4280                 error(state, 0, "arithmetic type expexted");
4281         }
4282 }
4283
4284 static void ptr_arithmetic(struct compile_state *state, struct triple *def)
4285 {
4286         if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
4287                 error(state, def, "pointer or arithmetic type expected");
4288         }
4289 }
4290
4291 static int is_integral(struct triple *ins)
4292 {
4293         return TYPE_INTEGER(ins->type->type);
4294 }
4295
4296 static void integral(struct compile_state *state, struct triple *def)
4297 {
4298         if (!is_integral(def)) {
4299                 error(state, 0, "integral type expected");
4300         }
4301 }
4302
4303
4304 static void bool(struct compile_state *state, struct triple *def)
4305 {
4306         if (!TYPE_ARITHMETIC(def->type->type) &&
4307                 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
4308                 error(state, 0, "arithmetic or pointer type expected");
4309         }
4310 }
4311
4312 static int is_signed(struct type *type)
4313 {
4314         return !!TYPE_SIGNED(type->type);
4315 }
4316
4317 /* Is this value located in a register otherwise it must be in memory */
4318 static int is_in_reg(struct compile_state *state, struct triple *def)
4319 {
4320         int in_reg;
4321         if (def->op == OP_ADECL) {
4322                 in_reg = 1;
4323         }
4324         else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
4325                 in_reg = 0;
4326         }
4327         else if (def->op == OP_VAL_VEC) {
4328                 in_reg = is_in_reg(state, RHS(def, 0));
4329         }
4330         else if (def->op == OP_DOT) {
4331                 in_reg = is_in_reg(state, RHS(def, 0));
4332         }
4333         else {
4334                 internal_error(state, 0, "unknown expr storage location");
4335                 in_reg = -1;
4336         }
4337         return in_reg;
4338 }
4339
4340 /* Is this a stable variable location otherwise it must be a temporary */
4341 static int is_stable(struct compile_state *state, struct triple *def)
4342 {
4343         int ret;
4344         ret = 0;
4345         if (!def) {
4346                 return 0;
4347         }
4348         if ((def->op == OP_ADECL) || 
4349                 (def->op == OP_SDECL) || 
4350                 (def->op == OP_DEREF) ||
4351                 (def->op == OP_BLOBCONST)) {
4352                 ret = 1;
4353         }
4354         else if (def->op == OP_DOT) {
4355                 ret = is_stable(state, RHS(def, 0));
4356         }
4357         else if (def->op == OP_VAL_VEC) {
4358                 struct triple **vector;
4359                 ulong_t i;
4360                 ret = 1;
4361                 vector = &RHS(def, 0);
4362                 for(i = 0; i < def->type->elements; i++) {
4363                         if (!is_stable(state, vector[i])) {
4364                                 ret = 0;
4365                                 break;
4366                         }
4367                 }
4368         }
4369         return ret;
4370 }
4371
4372 static int is_lvalue(struct compile_state *state, struct triple *def)
4373 {
4374         int ret;
4375         ret = 1;
4376         if (!def) {
4377                 return 0;
4378         }
4379         if (!is_stable(state, def)) {
4380                 return 0;
4381         }
4382         if (def->type->type & QUAL_CONST) {
4383                 ret = 0;
4384         }
4385         else if (def->op == OP_DOT) {
4386                 ret = is_lvalue(state, RHS(def, 0));
4387         }
4388         return ret;
4389 }
4390
4391 static void lvalue(struct compile_state *state, struct triple *def)
4392 {
4393         if (!def) {
4394                 internal_error(state, def, "nothing where lvalue expected?");
4395         }
4396         if (!is_lvalue(state, def)) { 
4397                 error(state, def, "lvalue expected");
4398         }
4399 }
4400
4401 static int is_pointer(struct triple *def)
4402 {
4403         return (def->type->type & TYPE_MASK) == TYPE_POINTER;
4404 }
4405
4406 static void pointer(struct compile_state *state, struct triple *def)
4407 {
4408         if (!is_pointer(def)) {
4409                 error(state, def, "pointer expected");
4410         }
4411 }
4412
4413 static struct triple *int_const(
4414         struct compile_state *state, struct type *type, ulong_t value)
4415 {
4416         struct triple *result;
4417         switch(type->type & TYPE_MASK) {
4418         case TYPE_CHAR:
4419         case TYPE_INT:   case TYPE_UINT:
4420         case TYPE_LONG:  case TYPE_ULONG:
4421                 break;
4422         default:
4423                 internal_error(state, 0, "constant for unkown type");
4424         }
4425         result = triple(state, OP_INTCONST, type, 0, 0);
4426         result->u.cval = value;
4427         return result;
4428 }
4429
4430
4431 static struct triple *do_mk_addr_expr(struct compile_state *state, 
4432         struct triple *expr, struct type *type, ulong_t offset)
4433 {
4434         struct triple *result;
4435         lvalue(state, expr);
4436
4437         result = 0;
4438         if (expr->op == OP_ADECL) {
4439                 error(state, expr, "address of auto variables not supported");
4440         }
4441         else if (expr->op == OP_SDECL) {
4442                 result = triple(state, OP_ADDRCONST, type, 0, 0);
4443                 MISC(result, 0) = expr;
4444                 result->u.cval = offset;
4445         }
4446         else if (expr->op == OP_DEREF) {
4447                 result = triple(state, OP_ADD, type,
4448                         RHS(expr, 0),
4449                         int_const(state, &ulong_type, offset));
4450         }
4451         return result;
4452 }
4453
4454 static struct triple *mk_addr_expr(
4455         struct compile_state *state, struct triple *expr, ulong_t offset)
4456 {
4457         struct type *type;
4458         
4459         type = new_type(
4460                 TYPE_POINTER | (expr->type->type & QUAL_MASK),
4461                 expr->type, 0);
4462
4463         return do_mk_addr_expr(state, expr, type, offset);
4464 }
4465
4466 static struct triple *mk_deref_expr(
4467         struct compile_state *state, struct triple *expr)
4468 {
4469         struct type *base_type;
4470         pointer(state, expr);
4471         base_type = expr->type->left;
4472         if (!TYPE_PTR(base_type->type) && !TYPE_ARITHMETIC(base_type->type)) {
4473                 error(state, 0, 
4474                         "Only pointer and arithmetic values can be dereferenced");
4475         }
4476         return triple(state, OP_DEREF, base_type, expr, 0);
4477 }
4478
4479 static struct triple *deref_field(
4480         struct compile_state *state, struct triple *expr, struct hash_entry *field)
4481 {
4482         struct triple *result;
4483         struct type *type, *member;
4484         if (!field) {
4485                 internal_error(state, 0, "No field passed to deref_field");
4486         }
4487         result = 0;
4488         type = expr->type;
4489         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4490                 error(state, 0, "request for member %s in something not a struct or union",
4491                         field->name);
4492         }
4493         member = type->left;
4494         while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4495                 if (member->left->field_ident == field) {
4496                         member = member->left;
4497                         break;
4498                 }
4499                 member = member->right;
4500         }
4501         if (member->field_ident != field) {
4502                 error(state, 0, "%s is not a member", field->name);
4503         }
4504         if ((type->type & STOR_MASK) == STOR_PERM) {
4505                 /* Do the pointer arithmetic to get a deref the field */
4506                 ulong_t offset;
4507                 offset = field_offset(state, type, field);
4508                 result = do_mk_addr_expr(state, expr, member, offset);
4509                 result = mk_deref_expr(state, result);
4510         }
4511         else {
4512                 /* Find the variable for the field I want. */
4513                 result = triple(state, OP_DOT, 
4514                         field_type(state, type, field), expr, 0);
4515                 result->u.field = field;
4516         }
4517         return result;
4518 }
4519
4520 static struct triple *read_expr(struct compile_state *state, struct triple *def)
4521 {
4522         int op;
4523         if  (!def) {
4524                 return 0;
4525         }
4526         if (!is_stable(state, def)) {
4527                 return def;
4528         }
4529         /* Tranform an array to a pointer to the first element */
4530 #warning "CHECK_ME is this the right place to transform arrays to pointers?"
4531         if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
4532                 struct type *type;
4533                 struct triple *result;
4534                 type = new_type(
4535                         TYPE_POINTER | (def->type->type & QUAL_MASK),
4536                         def->type->left, 0);
4537                 result = triple(state, OP_ADDRCONST, type, 0, 0);
4538                 MISC(result, 0) = def;
4539                 return result;
4540         }
4541         if (is_in_reg(state, def)) {
4542                 op = OP_READ;
4543         } else {
4544                 op = OP_LOAD;
4545         }
4546         return triple(state, op, def->type, def, 0);
4547 }
4548
4549 static void write_compatible(struct compile_state *state,
4550         struct type *dest, struct type *rval)
4551 {
4552         int compatible = 0;
4553         /* Both operands have arithmetic type */
4554         if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
4555                 compatible = 1;
4556         }
4557         /* One operand is a pointer and the other is a pointer to void */
4558         else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
4559                 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
4560                 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
4561                         ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
4562                 compatible = 1;
4563         }
4564         /* If both types are the same without qualifiers we are good */
4565         else if (equiv_ptrs(dest, rval)) {
4566                 compatible = 1;
4567         }
4568         /* test for struct/union equality  */
4569         else if (((dest->type & TYPE_MASK) == TYPE_STRUCT) &&
4570                 ((rval->type & TYPE_MASK) == TYPE_STRUCT) &&
4571                 (dest->type_ident == rval->type_ident)) {
4572                 compatible = 1;
4573         }
4574         if (!compatible) {
4575                 error(state, 0, "Incompatible types in assignment");
4576         }
4577 }
4578
4579 static struct triple *write_expr(
4580         struct compile_state *state, struct triple *dest, struct triple *rval)
4581 {
4582         struct triple *def;
4583         int op;
4584
4585         def = 0;
4586         if (!rval) {
4587                 internal_error(state, 0, "missing rval");
4588         }
4589
4590         if (rval->op == OP_LIST) {
4591                 internal_error(state, 0, "expression of type OP_LIST?");
4592         }
4593         if (!is_lvalue(state, dest)) {
4594                 internal_error(state, 0, "writing to a non lvalue?");
4595         }
4596
4597         write_compatible(state, dest->type, rval->type);
4598
4599         /* Now figure out which assignment operator to use */
4600         op = -1;
4601         if (is_in_reg(state, dest)) {
4602                 op = OP_WRITE;
4603         } else {
4604                 op = OP_STORE;
4605         }
4606         def = triple(state, op, dest->type, dest, rval);
4607         return def;
4608 }
4609
4610 static struct triple *init_expr(
4611         struct compile_state *state, struct triple *dest, struct triple *rval)
4612 {
4613         struct triple *def;
4614
4615         def = 0;
4616         if (!rval) {
4617                 internal_error(state, 0, "missing rval");
4618         }
4619         if ((dest->type->type & STOR_MASK) != STOR_PERM) {
4620                 rval = read_expr(state, rval);
4621                 def = write_expr(state, dest, rval);
4622         }
4623         else {
4624                 /* Fill in the array size if necessary */
4625                 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
4626                         ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
4627                         if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4628                                 dest->type->elements = rval->type->elements;
4629                         }
4630                 }
4631                 if (!equiv_types(dest->type, rval->type)) {
4632                         error(state, 0, "Incompatible types in inializer");
4633                 }
4634                 MISC(dest, 0) = rval;
4635                 insert_triple(state, dest, rval);
4636                 rval->id |= TRIPLE_FLAG_FLATTENED;
4637                 use_triple(MISC(dest, 0), dest);
4638         }
4639         return def;
4640 }
4641
4642 struct type *arithmetic_result(
4643         struct compile_state *state, struct triple *left, struct triple *right)
4644 {
4645         struct type *type;
4646         /* Sanity checks to ensure I am working with arithmetic types */
4647         arithmetic(state, left);
4648         arithmetic(state, right);
4649         type = new_type(
4650                 do_arithmetic_conversion(
4651                         left->type->type, 
4652                         right->type->type), 0, 0);
4653         return type;
4654 }
4655
4656 struct type *ptr_arithmetic_result(
4657         struct compile_state *state, struct triple *left, struct triple *right)
4658 {
4659         struct type *type;
4660         /* Sanity checks to ensure I am working with the proper types */
4661         ptr_arithmetic(state, left);
4662         arithmetic(state, right);
4663         if (TYPE_ARITHMETIC(left->type->type) && 
4664                 TYPE_ARITHMETIC(right->type->type)) {
4665                 type = arithmetic_result(state, left, right);
4666         }
4667         else if (TYPE_PTR(left->type->type)) {
4668                 type = left->type;
4669         }
4670         else {
4671                 internal_error(state, 0, "huh?");
4672                 type = 0;
4673         }
4674         return type;
4675 }
4676
4677
4678 /* boolean helper function */
4679
4680 static struct triple *ltrue_expr(struct compile_state *state, 
4681         struct triple *expr)
4682 {
4683         switch(expr->op) {
4684         case OP_LTRUE:   case OP_LFALSE:  case OP_EQ:      case OP_NOTEQ:
4685         case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
4686         case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
4687                 /* If the expression is already boolean do nothing */
4688                 break;
4689         default:
4690                 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
4691                 break;
4692         }
4693         return expr;
4694 }
4695
4696 static struct triple *lfalse_expr(struct compile_state *state, 
4697         struct triple *expr)
4698 {
4699         return triple(state, OP_LFALSE, &int_type, expr, 0);
4700 }
4701
4702 static struct triple *cond_expr(
4703         struct compile_state *state, 
4704         struct triple *test, struct triple *left, struct triple *right)
4705 {
4706         struct triple *def;
4707         struct type *result_type;
4708         unsigned int left_type, right_type;
4709         bool(state, test);
4710         left_type = left->type->type;
4711         right_type = right->type->type;
4712         result_type = 0;
4713         /* Both operands have arithmetic type */
4714         if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
4715                 result_type = arithmetic_result(state, left, right);
4716         }
4717         /* Both operands have void type */
4718         else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
4719                 ((right_type & TYPE_MASK) == TYPE_VOID)) {
4720                 result_type = &void_type;
4721         }
4722         /* pointers to the same type... */
4723         else if ((result_type = compatible_ptrs(left->type, right->type))) {
4724                 ;
4725         }
4726         /* Both operands are pointers and left is a pointer to void */
4727         else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4728                 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4729                 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4730                 result_type = right->type;
4731         }
4732         /* Both operands are pointers and right is a pointer to void */
4733         else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4734                 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4735                 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4736                 result_type = left->type;
4737         }
4738         if (!result_type) {
4739                 error(state, 0, "Incompatible types in conditional expression");
4740         }
4741         /* Cleanup and invert the test */
4742         test = lfalse_expr(state, read_expr(state, test));
4743         def = new_triple(state, OP_COND, result_type, 0, 3);
4744         def->param[0] = test;
4745         def->param[1] = left;
4746         def->param[2] = right;
4747         return def;
4748 }
4749
4750
4751 static int expr_depth(struct compile_state *state, struct triple *ins)
4752 {
4753         int count;
4754         count = 0;
4755         if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
4756                 count = 0;
4757         }
4758         else if (ins->op == OP_DEREF) {
4759                 count = expr_depth(state, RHS(ins, 0)) - 1;
4760         }
4761         else if (ins->op == OP_VAL) {
4762                 count = expr_depth(state, RHS(ins, 0)) - 1;
4763         }
4764         else if (ins->op == OP_COMMA) {
4765                 int ldepth, rdepth;
4766                 ldepth = expr_depth(state, RHS(ins, 0));
4767                 rdepth = expr_depth(state, RHS(ins, 1));
4768                 count = (ldepth >= rdepth)? ldepth : rdepth;
4769         }
4770         else if (ins->op == OP_CALL) {
4771                 /* Don't figure the depth of a call just guess it is huge */
4772                 count = 1000;
4773         }
4774         else {
4775                 struct triple **expr;
4776                 expr = triple_rhs(state, ins, 0);
4777                 for(;expr; expr = triple_rhs(state, ins, expr)) {
4778                         if (*expr) {
4779                                 int depth;
4780                                 depth = expr_depth(state, *expr);
4781                                 if (depth > count) {
4782                                         count = depth;
4783                                 }
4784                         }
4785                 }
4786         }
4787         return count + 1;
4788 }
4789
4790 static struct triple *flatten(
4791         struct compile_state *state, struct triple *first, struct triple *ptr);
4792
4793 static struct triple *flatten_generic(
4794         struct compile_state *state, struct triple *first, struct triple *ptr)
4795 {
4796         struct rhs_vector {
4797                 int depth;
4798                 struct triple **ins;
4799         } vector[MAX_RHS];
4800         int i, rhs, lhs;
4801         /* Only operations with just a rhs should come here */
4802         rhs = TRIPLE_RHS(ptr->sizes);
4803         lhs = TRIPLE_LHS(ptr->sizes);
4804         if (TRIPLE_SIZE(ptr->sizes) != lhs + rhs) {
4805                 internal_error(state, ptr, "unexpected args for: %d %s",
4806                         ptr->op, tops(ptr->op));
4807         }
4808         /* Find the depth of the rhs elements */
4809         for(i = 0; i < rhs; i++) {
4810                 vector[i].ins = &RHS(ptr, i);
4811                 vector[i].depth = expr_depth(state, *vector[i].ins);
4812         }
4813         /* Selection sort the rhs */
4814         for(i = 0; i < rhs; i++) {
4815                 int j, max = i;
4816                 for(j = i + 1; j < rhs; j++ ) {
4817                         if (vector[j].depth > vector[max].depth) {
4818                                 max = j;
4819                         }
4820                 }
4821                 if (max != i) {
4822                         struct rhs_vector tmp;
4823                         tmp = vector[i];
4824                         vector[i] = vector[max];
4825                         vector[max] = tmp;
4826                 }
4827         }
4828         /* Now flatten the rhs elements */
4829         for(i = 0; i < rhs; i++) {
4830                 *vector[i].ins = flatten(state, first, *vector[i].ins);
4831                 use_triple(*vector[i].ins, ptr);
4832         }
4833         
4834         /* Now flatten the lhs elements */
4835         for(i = 0; i < lhs; i++) {
4836                 struct triple **ins = &LHS(ptr, i);
4837                 *ins = flatten(state, first, *ins);
4838                 use_triple(*ins, ptr);
4839         }
4840         return ptr;
4841 }
4842
4843 static struct triple *flatten_land(
4844         struct compile_state *state, struct triple *first, struct triple *ptr)
4845 {
4846         struct triple *left, *right;
4847         struct triple *val, *test, *jmp, *label1, *end;
4848
4849         /* Find the triples */
4850         left = RHS(ptr, 0);
4851         right = RHS(ptr, 1);
4852
4853         /* Generate the needed triples */
4854         end = label(state);
4855
4856         /* Thread the triples together */
4857         val          = flatten(state, first, variable(state, ptr->type));
4858         left         = flatten(state, first, write_expr(state, val, left));
4859         test         = flatten(state, first, 
4860                 lfalse_expr(state, read_expr(state, val)));
4861         jmp          = flatten(state, first, branch(state, end, test));
4862         label1       = flatten(state, first, label(state));
4863         right        = flatten(state, first, write_expr(state, val, right));
4864         TARG(jmp, 0) = flatten(state, first, end); 
4865         
4866         /* Now give the caller something to chew on */
4867         return read_expr(state, val);
4868 }
4869
4870 static struct triple *flatten_lor(
4871         struct compile_state *state, struct triple *first, struct triple *ptr)
4872 {
4873         struct triple *left, *right;
4874         struct triple *val, *jmp, *label1, *end;
4875
4876         /* Find the triples */
4877         left = RHS(ptr, 0);
4878         right = RHS(ptr, 1);
4879
4880         /* Generate the needed triples */
4881         end = label(state);
4882
4883         /* Thread the triples together */
4884         val          = flatten(state, first, variable(state, ptr->type));
4885         left         = flatten(state, first, write_expr(state, val, left));
4886         jmp          = flatten(state, first, branch(state, end, left));
4887         label1       = flatten(state, first, label(state));
4888         right        = flatten(state, first, write_expr(state, val, right));
4889         TARG(jmp, 0) = flatten(state, first, end);
4890        
4891         
4892         /* Now give the caller something to chew on */
4893         return read_expr(state, val);
4894 }
4895
4896 static struct triple *flatten_cond(
4897         struct compile_state *state, struct triple *first, struct triple *ptr)
4898 {
4899         struct triple *test, *left, *right;
4900         struct triple *val, *mv1, *jmp1, *label1, *mv2, *middle, *jmp2, *end;
4901
4902         /* Find the triples */
4903         test = RHS(ptr, 0);
4904         left = RHS(ptr, 1);
4905         right = RHS(ptr, 2);
4906
4907         /* Generate the needed triples */
4908         end = label(state);
4909         middle = label(state);
4910
4911         /* Thread the triples together */
4912         val           = flatten(state, first, variable(state, ptr->type));
4913         test          = flatten(state, first, test);
4914         jmp1          = flatten(state, first, branch(state, middle, test));
4915         label1        = flatten(state, first, label(state));
4916         left          = flatten(state, first, left);
4917         mv1           = flatten(state, first, write_expr(state, val, left));
4918         jmp2          = flatten(state, first, branch(state, end, 0));
4919         TARG(jmp1, 0) = flatten(state, first, middle);
4920         right         = flatten(state, first, right);
4921         mv2           = flatten(state, first, write_expr(state, val, right));
4922         TARG(jmp2, 0) = flatten(state, first, end);
4923         
4924         /* Now give the caller something to chew on */
4925         return read_expr(state, val);
4926 }
4927
4928 struct triple *copy_func(struct compile_state *state, struct triple *ofunc, 
4929         struct occurance *base_occurance)
4930 {
4931         struct triple *nfunc;
4932         struct triple *nfirst, *ofirst;
4933         struct triple *new, *old;
4934
4935 #if 0
4936         fprintf(stdout, "\n");
4937         loc(stdout, state, 0);
4938         fprintf(stdout, "\n__________ copy_func _________\n");
4939         print_triple(state, ofunc);
4940         fprintf(stdout, "__________ copy_func _________ done\n\n");
4941 #endif
4942
4943         /* Make a new copy of the old function */
4944         nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
4945         nfirst = 0;
4946         ofirst = old = RHS(ofunc, 0);
4947         do {
4948                 struct triple *new;
4949                 struct occurance *occurance;
4950                 int old_lhs, old_rhs;
4951                 old_lhs = TRIPLE_LHS(old->sizes);
4952                 old_rhs = TRIPLE_RHS(old->sizes);
4953                 occurance = inline_occurance(state, base_occurance, old->occurance);
4954                 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
4955                         occurance);
4956                 if (!triple_stores_block(state, new)) {
4957                         memcpy(&new->u, &old->u, sizeof(new->u));
4958                 }
4959                 if (!nfirst) {
4960                         RHS(nfunc, 0) = nfirst = new;
4961                 }
4962                 else {
4963                         insert_triple(state, nfirst, new);
4964                 }
4965                 new->id |= TRIPLE_FLAG_FLATTENED;
4966                 
4967                 /* During the copy remember new as user of old */
4968                 use_triple(old, new);
4969
4970                 /* Populate the return type if present */
4971                 if (old == MISC(ofunc, 0)) {
4972                         MISC(nfunc, 0) = new;
4973                 }
4974                 old = old->next;
4975         } while(old != ofirst);
4976
4977         /* Make a second pass to fix up any unresolved references */
4978         old = ofirst;
4979         new = nfirst;
4980         do {
4981                 struct triple **oexpr, **nexpr;
4982                 int count, i;
4983                 /* Lookup where the copy is, to join pointers */
4984                 count = TRIPLE_SIZE(old->sizes);
4985                 for(i = 0; i < count; i++) {
4986                         oexpr = &old->param[i];
4987                         nexpr = &new->param[i];
4988                         if (!*nexpr && *oexpr && (*oexpr)->use) {
4989                                 *nexpr = (*oexpr)->use->member;
4990                                 if (*nexpr == old) {
4991                                         internal_error(state, 0, "new == old?");
4992                                 }
4993                                 use_triple(*nexpr, new);
4994                         }
4995                         if (!*nexpr && *oexpr) {
4996                                 internal_error(state, 0, "Could not copy %d\n", i);
4997                         }
4998                 }
4999                 old = old->next;
5000                 new = new->next;
5001         } while((old != ofirst) && (new != nfirst));
5002         
5003         /* Make a third pass to cleanup the extra useses */
5004         old = ofirst;
5005         new = nfirst;
5006         do {
5007                 unuse_triple(old, new);
5008                 old = old->next;
5009                 new = new->next;
5010         } while ((old != ofirst) && (new != nfirst));
5011         return nfunc;
5012 }
5013
5014 static struct triple *flatten_call(
5015         struct compile_state *state, struct triple *first, struct triple *ptr)
5016 {
5017         /* Inline the function call */
5018         struct type *ptype;
5019         struct triple *ofunc, *nfunc, *nfirst, *param, *result;
5020         struct triple *end, *nend;
5021         int pvals, i;
5022
5023         /* Find the triples */
5024         ofunc = MISC(ptr, 0);
5025         if (ofunc->op != OP_LIST) {
5026                 internal_error(state, 0, "improper function");
5027         }
5028         nfunc = copy_func(state, ofunc, ptr->occurance);
5029         nfirst = RHS(nfunc, 0)->next;
5030         /* Prepend the parameter reading into the new function list */
5031         ptype = nfunc->type->right;
5032         param = RHS(nfunc, 0)->next;
5033         pvals = TRIPLE_RHS(ptr->sizes);
5034         for(i = 0; i < pvals; i++) {
5035                 struct type *atype;
5036                 struct triple *arg;
5037                 atype = ptype;
5038                 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
5039                         atype = ptype->left;
5040                 }
5041                 while((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
5042                         param = param->next;
5043                 }
5044                 arg = RHS(ptr, i);
5045                 flatten(state, nfirst, write_expr(state, param, arg));
5046                 ptype = ptype->right;
5047                 param = param->next;
5048         }
5049         result = 0;
5050         if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
5051                 result = read_expr(state, MISC(nfunc,0));
5052         }
5053 #if 0
5054         fprintf(stdout, "\n");
5055         loc(stdout, state, 0);
5056         fprintf(stdout, "\n__________ flatten_call _________\n");
5057         print_triple(state, nfunc);
5058         fprintf(stdout, "__________ flatten_call _________ done\n\n");
5059 #endif
5060
5061         /* Get rid of the extra triples */
5062         nfirst = RHS(nfunc, 0)->next;
5063         free_triple(state, RHS(nfunc, 0));
5064         RHS(nfunc, 0) = 0;
5065         free_triple(state, nfunc);
5066
5067         /* Append the new function list onto the return list */
5068         end = first->prev;
5069         nend = nfirst->prev;
5070         end->next    = nfirst;
5071         nfirst->prev = end;
5072         nend->next   = first;
5073         first->prev  = nend;
5074
5075         return result;
5076 }
5077
5078 static struct triple *flatten(
5079         struct compile_state *state, struct triple *first, struct triple *ptr)
5080 {
5081         struct triple *orig_ptr;
5082         if (!ptr)
5083                 return 0;
5084         do {
5085                 orig_ptr = ptr;
5086                 /* Only flatten triples once */
5087                 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
5088                         return ptr;
5089                 }
5090                 switch(ptr->op) {
5091                 case OP_WRITE:
5092                 case OP_STORE:
5093                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5094                         LHS(ptr, 0) = flatten(state, first, LHS(ptr, 0));
5095                         use_triple(LHS(ptr, 0), ptr);
5096                         use_triple(RHS(ptr, 0), ptr);
5097                         break;
5098                 case OP_COMMA:
5099                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5100                         ptr = RHS(ptr, 1);
5101                         break;
5102                 case OP_VAL:
5103                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5104                         return MISC(ptr, 0);
5105                         break;
5106                 case OP_LAND:
5107                         ptr = flatten_land(state, first, ptr);
5108                         break;
5109                 case OP_LOR:
5110                         ptr = flatten_lor(state, first, ptr);
5111                         break;
5112                 case OP_COND:
5113                         ptr = flatten_cond(state, first, ptr);
5114                         break;
5115                 case OP_CALL:
5116                         ptr = flatten_call(state, first, ptr);
5117                         break;
5118                 case OP_READ:
5119                 case OP_LOAD:
5120                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5121                         use_triple(RHS(ptr, 0), ptr);
5122                         break;
5123                 case OP_BRANCH:
5124                         use_triple(TARG(ptr, 0), ptr);
5125                         if (TRIPLE_RHS(ptr->sizes)) {
5126                                 use_triple(RHS(ptr, 0), ptr);
5127                                 if (ptr->next != ptr) {
5128                                         use_triple(ptr->next, ptr);
5129                                 }
5130                         }
5131                         break;
5132                 case OP_BLOBCONST:
5133                         insert_triple(state, first, ptr);
5134                         ptr->id |= TRIPLE_FLAG_FLATTENED;
5135                         ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
5136                         use_triple(MISC(ptr, 0), ptr);
5137                         break;
5138                 case OP_DEREF:
5139                         /* Since OP_DEREF is just a marker delete it when I flatten it */
5140                         ptr = RHS(ptr, 0);
5141                         RHS(orig_ptr, 0) = 0;
5142                         free_triple(state, orig_ptr);
5143                         break;
5144                 case OP_DOT:
5145                 {
5146                         struct triple *base;
5147                         base = RHS(ptr, 0);
5148                         base = flatten(state, first, base);
5149                         if (base->op == OP_VAL_VEC) {
5150                                 ptr = struct_field(state, base, ptr->u.field);
5151                         }
5152                         break;
5153                 }
5154                 case OP_PIECE:
5155                         MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5156                         use_triple(MISC(ptr, 0), ptr);
5157                         use_triple(ptr, MISC(ptr, 0));
5158                         break;
5159                 case OP_ADDRCONST:
5160                 case OP_SDECL:
5161                         MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5162                         use_triple(MISC(ptr, 0), ptr);
5163                         break;
5164                 case OP_ADECL:
5165                         break;
5166                 default:
5167                         /* Flatten the easy cases we don't override */
5168                         ptr = flatten_generic(state, first, ptr);
5169                         break;
5170                 }
5171         } while(ptr && (ptr != orig_ptr));
5172         if (ptr) {
5173                 insert_triple(state, first, ptr);
5174                 ptr->id |= TRIPLE_FLAG_FLATTENED;
5175         }
5176         return ptr;
5177 }
5178
5179 static void release_expr(struct compile_state *state, struct triple *expr)
5180 {
5181         struct triple *head;
5182         head = label(state);
5183         flatten(state, head, expr);
5184         while(head->next != head) {
5185                 release_triple(state, head->next);
5186         }
5187         free_triple(state, head);
5188 }
5189
5190 static int replace_rhs_use(struct compile_state *state,
5191         struct triple *orig, struct triple *new, struct triple *use)
5192 {
5193         struct triple **expr;
5194         int found;
5195         found = 0;
5196         expr = triple_rhs(state, use, 0);
5197         for(;expr; expr = triple_rhs(state, use, expr)) {
5198                 if (*expr == orig) {
5199                         *expr = new;
5200                         found = 1;
5201                 }
5202         }
5203         if (found) {
5204                 unuse_triple(orig, use);
5205                 use_triple(new, use);
5206         }
5207         return found;
5208 }
5209
5210 static int replace_lhs_use(struct compile_state *state,
5211         struct triple *orig, struct triple *new, struct triple *use)
5212 {
5213         struct triple **expr;
5214         int found;
5215         found = 0;
5216         expr = triple_lhs(state, use, 0);
5217         for(;expr; expr = triple_lhs(state, use, expr)) {
5218                 if (*expr == orig) {
5219                         *expr = new;
5220                         found = 1;
5221                 }
5222         }
5223         if (found) {
5224                 unuse_triple(orig, use);
5225                 use_triple(new, use);
5226         }
5227         return found;
5228 }
5229
5230 static void propogate_use(struct compile_state *state,
5231         struct triple *orig, struct triple *new)
5232 {
5233         struct triple_set *user, *next;
5234         for(user = orig->use; user; user = next) {
5235                 struct triple *use;
5236                 int found;
5237                 next = user->next;
5238                 use = user->member;
5239                 found = 0;
5240                 found |= replace_rhs_use(state, orig, new, use);
5241                 found |= replace_lhs_use(state, orig, new, use);
5242                 if (!found) {
5243                         internal_error(state, use, "use without use");
5244                 }
5245         }
5246         if (orig->use) {
5247                 internal_error(state, orig, "used after propogate_use");
5248         }
5249 }
5250
5251 /*
5252  * Code generators
5253  * ===========================
5254  */
5255
5256 static struct triple *mk_add_expr(
5257         struct compile_state *state, struct triple *left, struct triple *right)
5258 {
5259         struct type *result_type;
5260         /* Put pointer operands on the left */
5261         if (is_pointer(right)) {
5262                 struct triple *tmp;
5263                 tmp = left;
5264                 left = right;
5265                 right = tmp;
5266         }
5267         left  = read_expr(state, left);
5268         right = read_expr(state, right);
5269         result_type = ptr_arithmetic_result(state, left, right);
5270         if (is_pointer(left)) {
5271                 right = triple(state, 
5272                         is_signed(right->type)? OP_SMUL : OP_UMUL, 
5273                         &ulong_type, 
5274                         right, 
5275                         int_const(state, &ulong_type, 
5276                                 size_of(state, left->type->left)));
5277         }
5278         return triple(state, OP_ADD, result_type, left, right);
5279 }
5280
5281 static struct triple *mk_sub_expr(
5282         struct compile_state *state, struct triple *left, struct triple *right)
5283 {
5284         struct type *result_type;
5285         result_type = ptr_arithmetic_result(state, left, right);
5286         left  = read_expr(state, left);
5287         right = read_expr(state, right);
5288         if (is_pointer(left)) {
5289                 right = triple(state, 
5290                         is_signed(right->type)? OP_SMUL : OP_UMUL, 
5291                         &ulong_type, 
5292                         right, 
5293                         int_const(state, &ulong_type, 
5294                                 size_of(state, left->type->left)));
5295         }
5296         return triple(state, OP_SUB, result_type, left, right);
5297 }
5298
5299 static struct triple *mk_pre_inc_expr(
5300         struct compile_state *state, struct triple *def)
5301 {
5302         struct triple *val;
5303         lvalue(state, def);
5304         val = mk_add_expr(state, def, int_const(state, &int_type, 1));
5305         return triple(state, OP_VAL, def->type,
5306                 write_expr(state, def, val),
5307                 val);
5308 }
5309
5310 static struct triple *mk_pre_dec_expr(
5311         struct compile_state *state, struct triple *def)
5312 {
5313         struct triple *val;
5314         lvalue(state, def);
5315         val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
5316         return triple(state, OP_VAL, def->type,
5317                 write_expr(state, def, val),
5318                 val);
5319 }
5320
5321 static struct triple *mk_post_inc_expr(
5322         struct compile_state *state, struct triple *def)
5323 {
5324         struct triple *val;
5325         lvalue(state, def);
5326         val = read_expr(state, def);
5327         return triple(state, OP_VAL, def->type,
5328                 write_expr(state, def,
5329                         mk_add_expr(state, val, int_const(state, &int_type, 1)))
5330                 , val);
5331 }
5332
5333 static struct triple *mk_post_dec_expr(
5334         struct compile_state *state, struct triple *def)
5335 {
5336         struct triple *val;
5337         lvalue(state, def);
5338         val = read_expr(state, def);
5339         return triple(state, OP_VAL, def->type, 
5340                 write_expr(state, def,
5341                         mk_sub_expr(state, val, int_const(state, &int_type, 1)))
5342                 , val);
5343 }
5344
5345 static struct triple *mk_subscript_expr(
5346         struct compile_state *state, struct triple *left, struct triple *right)
5347 {
5348         left  = read_expr(state, left);
5349         right = read_expr(state, right);
5350         if (!is_pointer(left) && !is_pointer(right)) {
5351                 error(state, left, "subscripted value is not a pointer");
5352         }
5353         return mk_deref_expr(state, mk_add_expr(state, left, right));
5354 }
5355
5356 /*
5357  * Compile time evaluation
5358  * ===========================
5359  */
5360 static int is_const(struct triple *ins)
5361 {
5362         return IS_CONST_OP(ins->op);
5363 }
5364
5365 static int constants_equal(struct compile_state *state, 
5366         struct triple *left, struct triple *right)
5367 {
5368         int equal;
5369         if (!is_const(left) || !is_const(right)) {
5370                 equal = 0;
5371         }
5372         else if (left->op != right->op) {
5373                 equal = 0;
5374         }
5375         else if (!equiv_types(left->type, right->type)) {
5376                 equal = 0;
5377         }
5378         else {
5379                 equal = 0;
5380                 switch(left->op) {
5381                 case OP_INTCONST:
5382                         if (left->u.cval == right->u.cval) {
5383                                 equal = 1;
5384                         }
5385                         break;
5386                 case OP_BLOBCONST:
5387                 {
5388                         size_t lsize, rsize;
5389                         lsize = size_of(state, left->type);
5390                         rsize = size_of(state, right->type);
5391                         if (lsize != rsize) {
5392                                 break;
5393                         }
5394                         if (memcmp(left->u.blob, right->u.blob, lsize) == 0) {
5395                                 equal = 1;
5396                         }
5397                         break;
5398                 }
5399                 case OP_ADDRCONST:
5400                         if ((MISC(left, 0) == MISC(right, 0)) &&
5401                                 (left->u.cval == right->u.cval)) {
5402                                 equal = 1;
5403                         }
5404                         break;
5405                 default:
5406                         internal_error(state, left, "uknown constant type");
5407                         break;
5408                 }
5409         }
5410         return equal;
5411 }
5412
5413 static int is_zero(struct triple *ins)
5414 {
5415         return is_const(ins) && (ins->u.cval == 0);
5416 }
5417
5418 static int is_one(struct triple *ins)
5419 {
5420         return is_const(ins) && (ins->u.cval == 1);
5421 }
5422
5423 static long_t bsr(ulong_t value)
5424 {
5425         int i;
5426         for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
5427                 ulong_t mask;
5428                 mask = 1;
5429                 mask <<= i;
5430                 if (value & mask) {
5431                         return i;
5432                 }
5433         }
5434         return -1;
5435 }
5436
5437 static long_t bsf(ulong_t value)
5438 {
5439         int i;
5440         for(i = 0; i < (sizeof(ulong_t)*8); i++) {
5441                 ulong_t mask;
5442                 mask = 1;
5443                 mask <<= 1;
5444                 if (value & mask) {
5445                         return i;
5446                 }
5447         }
5448         return -1;
5449 }
5450
5451 static long_t log2(ulong_t value)
5452 {
5453         return bsr(value);
5454 }
5455
5456 static long_t tlog2(struct triple *ins)
5457 {
5458         return log2(ins->u.cval);
5459 }
5460
5461 static int is_pow2(struct triple *ins)
5462 {
5463         ulong_t value, mask;
5464         long_t log;
5465         if (!is_const(ins)) {
5466                 return 0;
5467         }
5468         value = ins->u.cval;
5469         log = log2(value);
5470         if (log == -1) {
5471                 return 0;
5472         }
5473         mask = 1;
5474         mask <<= log;
5475         return  ((value & mask) == value);
5476 }
5477
5478 static ulong_t read_const(struct compile_state *state,
5479         struct triple *ins, struct triple **expr)
5480 {
5481         struct triple *rhs;
5482         rhs = *expr;
5483         switch(rhs->type->type &TYPE_MASK) {
5484         case TYPE_CHAR:   
5485         case TYPE_SHORT:
5486         case TYPE_INT:
5487         case TYPE_LONG:
5488         case TYPE_UCHAR:   
5489         case TYPE_USHORT:  
5490         case TYPE_UINT:
5491         case TYPE_ULONG:
5492         case TYPE_POINTER:
5493                 break;
5494         default:
5495                 internal_error(state, rhs, "bad type to read_const\n");
5496                 break;
5497         }
5498         return rhs->u.cval;
5499 }
5500
5501 static long_t read_sconst(struct triple *ins, struct triple **expr)
5502 {
5503         struct triple *rhs;
5504         rhs = *expr;
5505         return (long_t)(rhs->u.cval);
5506 }
5507
5508 static void unuse_rhs(struct compile_state *state, struct triple *ins)
5509 {
5510         struct triple **expr;
5511         expr = triple_rhs(state, ins, 0);
5512         for(;expr;expr = triple_rhs(state, ins, expr)) {
5513                 if (*expr) {
5514                         unuse_triple(*expr, ins);
5515                         *expr = 0;
5516                 }
5517         }
5518 }
5519
5520 static void unuse_lhs(struct compile_state *state, struct triple *ins)
5521 {
5522         struct triple **expr;
5523         expr = triple_lhs(state, ins, 0);
5524         for(;expr;expr = triple_lhs(state, ins, expr)) {
5525                 unuse_triple(*expr, ins);
5526                 *expr = 0;
5527         }
5528 }
5529
5530 static void check_lhs(struct compile_state *state, struct triple *ins)
5531 {
5532         struct triple **expr;
5533         expr = triple_lhs(state, ins, 0);
5534         for(;expr;expr = triple_lhs(state, ins, expr)) {
5535                 internal_error(state, ins, "unexpected lhs");
5536         }
5537         
5538 }
5539 static void check_targ(struct compile_state *state, struct triple *ins)
5540 {
5541         struct triple **expr;
5542         expr = triple_targ(state, ins, 0);
5543         for(;expr;expr = triple_targ(state, ins, expr)) {
5544                 internal_error(state, ins, "unexpected targ");
5545         }
5546 }
5547
5548 static void wipe_ins(struct compile_state *state, struct triple *ins)
5549 {
5550         /* Becareful which instructions you replace the wiped
5551          * instruction with, as there are not enough slots
5552          * in all instructions to hold all others.
5553          */
5554         check_targ(state, ins);
5555         unuse_rhs(state, ins);
5556         unuse_lhs(state, ins);
5557 }
5558
5559 static void mkcopy(struct compile_state *state, 
5560         struct triple *ins, struct triple *rhs)
5561 {
5562         wipe_ins(state, ins);
5563         ins->op = OP_COPY;
5564         ins->sizes = TRIPLE_SIZES(0, 1, 0, 0);
5565         RHS(ins, 0) = rhs;
5566         use_triple(RHS(ins, 0), ins);
5567 }
5568
5569 static void mkconst(struct compile_state *state, 
5570         struct triple *ins, ulong_t value)
5571 {
5572         if (!is_integral(ins) && !is_pointer(ins)) {
5573                 internal_error(state, ins, "unknown type to make constant\n");
5574         }
5575         wipe_ins(state, ins);
5576         ins->op = OP_INTCONST;
5577         ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
5578         ins->u.cval = value;
5579 }
5580
5581 static void mkaddr_const(struct compile_state *state,
5582         struct triple *ins, struct triple *sdecl, ulong_t value)
5583 {
5584         wipe_ins(state, ins);
5585         ins->op = OP_ADDRCONST;
5586         ins->sizes = TRIPLE_SIZES(0, 0, 1, 0);
5587         MISC(ins, 0) = sdecl;
5588         ins->u.cval = value;
5589         use_triple(sdecl, ins);
5590 }
5591
5592 /* Transform multicomponent variables into simple register variables */
5593 static void flatten_structures(struct compile_state *state)
5594 {
5595         struct triple *ins, *first;
5596         first = RHS(state->main_function, 0);
5597         ins = first;
5598         /* Pass one expand structure values into valvecs.
5599          */
5600         ins = first;
5601         do {
5602                 struct triple *next;
5603                 next = ins->next;
5604                 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5605                         if (ins->op == OP_VAL_VEC) {
5606                                 /* Do nothing */
5607                         }
5608                         else if ((ins->op == OP_LOAD) || (ins->op == OP_READ)) {
5609                                 struct triple *def, **vector;
5610                                 struct type *tptr;
5611                                 int op;
5612                                 ulong_t i;
5613
5614                                 op = ins->op;
5615                                 def = RHS(ins, 0);
5616                                 get_occurance(ins->occurance);
5617                                 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
5618                                         ins->occurance);
5619
5620                                 vector = &RHS(next, 0);
5621                                 tptr = next->type->left;
5622                                 for(i = 0; i < next->type->elements; i++) {
5623                                         struct triple *sfield;
5624                                         struct type *mtype;
5625                                         mtype = tptr;
5626                                         if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5627                                                 mtype = mtype->left;
5628                                         }
5629                                         sfield = deref_field(state, def, mtype->field_ident);
5630                                         
5631                                         vector[i] = triple(
5632                                                 state, op, mtype, sfield, 0);
5633                                         put_occurance(vector[i]->occurance);
5634                                         get_occurance(next->occurance);
5635                                         vector[i]->occurance = next->occurance;
5636                                         tptr = tptr->right;
5637                                 }
5638                                 propogate_use(state, ins, next);
5639                                 flatten(state, ins, next);
5640                                 free_triple(state, ins);
5641                         }
5642                         else if ((ins->op == OP_STORE) || (ins->op == OP_WRITE)) {
5643                                 struct triple *src, *dst, **vector;
5644                                 struct type *tptr;
5645                                 int op;
5646                                 ulong_t i;
5647
5648                                 op = ins->op;
5649                                 src = RHS(ins, 0);
5650                                 dst = LHS(ins, 0);
5651                                 get_occurance(ins->occurance);
5652                                 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
5653                                         ins->occurance);
5654                                 
5655                                 vector = &RHS(next, 0);
5656                                 tptr = next->type->left;
5657                                 for(i = 0; i < ins->type->elements; i++) {
5658                                         struct triple *dfield, *sfield;
5659                                         struct type *mtype;
5660                                         mtype = tptr;
5661                                         if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5662                                                 mtype = mtype->left;
5663                                         }
5664                                         sfield = deref_field(state, src, mtype->field_ident);
5665                                         dfield = deref_field(state, dst, mtype->field_ident);
5666                                         vector[i] = triple(
5667                                                 state, op, mtype, dfield, sfield);
5668                                         put_occurance(vector[i]->occurance);
5669                                         get_occurance(next->occurance);
5670                                         vector[i]->occurance = next->occurance;
5671                                         tptr = tptr->right;
5672                                 }
5673                                 propogate_use(state, ins, next);
5674                                 flatten(state, ins, next);
5675                                 free_triple(state, ins);
5676                         }
5677                 }
5678                 ins = next;
5679         } while(ins != first);
5680         /* Pass two flatten the valvecs.
5681          */
5682         ins = first;
5683         do {
5684                 struct triple *next;
5685                 next = ins->next;
5686                 if (ins->op == OP_VAL_VEC) {
5687                         release_triple(state, ins);
5688                 } 
5689                 ins = next;
5690         } while(ins != first);
5691         /* Pass three verify the state and set ->id to 0.
5692          */
5693         ins = first;
5694         do {
5695                 ins->id &= ~TRIPLE_FLAG_FLATTENED;
5696                 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5697                         internal_error(state, 0, "STRUCT_TYPE remains?");
5698                 }
5699                 if (ins->op == OP_DOT) {
5700                         internal_error(state, 0, "OP_DOT remains?");
5701                 }
5702                 if (ins->op == OP_VAL_VEC) {
5703                         internal_error(state, 0, "OP_VAL_VEC remains?");
5704                 }
5705                 ins = ins->next;
5706         } while(ins != first);
5707 }
5708
5709 /* For those operations that cannot be simplified */
5710 static void simplify_noop(struct compile_state *state, struct triple *ins)
5711 {
5712         return;
5713 }
5714
5715 static void simplify_smul(struct compile_state *state, struct triple *ins)
5716 {
5717         if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5718                 struct triple *tmp;
5719                 tmp = RHS(ins, 0);
5720                 RHS(ins, 0) = RHS(ins, 1);
5721                 RHS(ins, 1) = tmp;
5722         }
5723         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5724                 long_t left, right;
5725                 left  = read_sconst(ins, &RHS(ins, 0));
5726                 right = read_sconst(ins, &RHS(ins, 1));
5727                 mkconst(state, ins, left * right);
5728         }
5729         else if (is_zero(RHS(ins, 1))) {
5730                 mkconst(state, ins, 0);
5731         }
5732         else if (is_one(RHS(ins, 1))) {
5733                 mkcopy(state, ins, RHS(ins, 0));
5734         }
5735         else if (is_pow2(RHS(ins, 1))) {
5736                 struct triple *val;
5737                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5738                 ins->op = OP_SL;
5739                 insert_triple(state, ins, val);
5740                 unuse_triple(RHS(ins, 1), ins);
5741                 use_triple(val, ins);
5742                 RHS(ins, 1) = val;
5743         }
5744 }
5745
5746 static void simplify_umul(struct compile_state *state, struct triple *ins)
5747 {
5748         if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5749                 struct triple *tmp;
5750                 tmp = RHS(ins, 0);
5751                 RHS(ins, 0) = RHS(ins, 1);
5752                 RHS(ins, 1) = tmp;
5753         }
5754         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5755                 ulong_t left, right;
5756                 left  = read_const(state, ins, &RHS(ins, 0));
5757                 right = read_const(state, ins, &RHS(ins, 1));
5758                 mkconst(state, ins, left * right);
5759         }
5760         else if (is_zero(RHS(ins, 1))) {
5761                 mkconst(state, ins, 0);
5762         }
5763         else if (is_one(RHS(ins, 1))) {
5764                 mkcopy(state, ins, RHS(ins, 0));
5765         }
5766         else if (is_pow2(RHS(ins, 1))) {
5767                 struct triple *val;
5768                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5769                 ins->op = OP_SL;
5770                 insert_triple(state, ins, val);
5771                 unuse_triple(RHS(ins, 1), ins);
5772                 use_triple(val, ins);
5773                 RHS(ins, 1) = val;
5774         }
5775 }
5776
5777 static void simplify_sdiv(struct compile_state *state, struct triple *ins)
5778 {
5779         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5780                 long_t left, right;
5781                 left  = read_sconst(ins, &RHS(ins, 0));
5782                 right = read_sconst(ins, &RHS(ins, 1));
5783                 mkconst(state, ins, left / right);
5784         }
5785         else if (is_zero(RHS(ins, 0))) {
5786                 mkconst(state, ins, 0);
5787         }
5788         else if (is_zero(RHS(ins, 1))) {
5789                 error(state, ins, "division by zero");
5790         }
5791         else if (is_one(RHS(ins, 1))) {
5792                 mkcopy(state, ins, RHS(ins, 0));
5793         }
5794         else if (is_pow2(RHS(ins, 1))) {
5795                 struct triple *val;
5796                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5797                 ins->op = OP_SSR;
5798                 insert_triple(state, ins, val);
5799                 unuse_triple(RHS(ins, 1), ins);
5800                 use_triple(val, ins);
5801                 RHS(ins, 1) = val;
5802         }
5803 }
5804
5805 static void simplify_udiv(struct compile_state *state, struct triple *ins)
5806 {
5807         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5808                 ulong_t left, right;
5809                 left  = read_const(state, ins, &RHS(ins, 0));
5810                 right = read_const(state, ins, &RHS(ins, 1));
5811                 mkconst(state, ins, left / right);
5812         }
5813         else if (is_zero(RHS(ins, 0))) {
5814                 mkconst(state, ins, 0);
5815         }
5816         else if (is_zero(RHS(ins, 1))) {
5817                 error(state, ins, "division by zero");
5818         }
5819         else if (is_one(RHS(ins, 1))) {
5820                 mkcopy(state, ins, RHS(ins, 0));
5821         }
5822         else if (is_pow2(RHS(ins, 1))) {
5823                 struct triple *val;
5824                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5825                 ins->op = OP_USR;
5826                 insert_triple(state, ins, val);
5827                 unuse_triple(RHS(ins, 1), ins);
5828                 use_triple(val, ins);
5829                 RHS(ins, 1) = val;
5830         }
5831 }
5832
5833 static void simplify_smod(struct compile_state *state, struct triple *ins)
5834 {
5835         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5836                 long_t left, right;
5837                 left  = read_const(state, ins, &RHS(ins, 0));
5838                 right = read_const(state, ins, &RHS(ins, 1));
5839                 mkconst(state, ins, left % right);
5840         }
5841         else if (is_zero(RHS(ins, 0))) {
5842                 mkconst(state, ins, 0);
5843         }
5844         else if (is_zero(RHS(ins, 1))) {
5845                 error(state, ins, "division by zero");
5846         }
5847         else if (is_one(RHS(ins, 1))) {
5848                 mkconst(state, ins, 0);
5849         }
5850         else if (is_pow2(RHS(ins, 1))) {
5851                 struct triple *val;
5852                 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
5853                 ins->op = OP_AND;
5854                 insert_triple(state, ins, val);
5855                 unuse_triple(RHS(ins, 1), ins);
5856                 use_triple(val, ins);
5857                 RHS(ins, 1) = val;
5858         }
5859 }
5860 static void simplify_umod(struct compile_state *state, struct triple *ins)
5861 {
5862         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5863                 ulong_t left, right;
5864                 left  = read_const(state, ins, &RHS(ins, 0));
5865                 right = read_const(state, ins, &RHS(ins, 1));
5866                 mkconst(state, ins, left % right);
5867         }
5868         else if (is_zero(RHS(ins, 0))) {
5869                 mkconst(state, ins, 0);
5870         }
5871         else if (is_zero(RHS(ins, 1))) {
5872                 error(state, ins, "division by zero");
5873         }
5874         else if (is_one(RHS(ins, 1))) {
5875                 mkconst(state, ins, 0);
5876         }
5877         else if (is_pow2(RHS(ins, 1))) {
5878                 struct triple *val;
5879                 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
5880                 ins->op = OP_AND;
5881                 insert_triple(state, ins, val);
5882                 unuse_triple(RHS(ins, 1), ins);
5883                 use_triple(val, ins);
5884                 RHS(ins, 1) = val;
5885         }
5886 }
5887
5888 static void simplify_add(struct compile_state *state, struct triple *ins)
5889 {
5890         /* start with the pointer on the left */
5891         if (is_pointer(RHS(ins, 1))) {
5892                 struct triple *tmp;
5893                 tmp = RHS(ins, 0);
5894                 RHS(ins, 0) = RHS(ins, 1);
5895                 RHS(ins, 1) = tmp;
5896         }
5897         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5898                 if (!is_pointer(RHS(ins, 0))) {
5899                         ulong_t left, right;
5900                         left  = read_const(state, ins, &RHS(ins, 0));
5901                         right = read_const(state, ins, &RHS(ins, 1));
5902                         mkconst(state, ins, left + right);
5903                 }
5904                 else /* op == OP_ADDRCONST */ {
5905                         struct triple *sdecl;
5906                         ulong_t left, right;
5907                         sdecl = MISC(RHS(ins, 0), 0);
5908                         left  = RHS(ins, 0)->u.cval;
5909                         right = RHS(ins, 1)->u.cval;
5910                         mkaddr_const(state, ins, sdecl, left + right);
5911                 }
5912         }
5913         else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5914                 struct triple *tmp;
5915                 tmp = RHS(ins, 1);
5916                 RHS(ins, 1) = RHS(ins, 0);
5917                 RHS(ins, 0) = tmp;
5918         }
5919 }
5920
5921 static void simplify_sub(struct compile_state *state, struct triple *ins)
5922 {
5923         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5924                 if (!is_pointer(RHS(ins, 0))) {
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 /* op == OP_ADDRCONST */ {
5931                         struct triple *sdecl;
5932                         ulong_t left, right;
5933                         sdecl = MISC(RHS(ins, 0), 0);
5934                         left  = RHS(ins, 0)->u.cval;
5935                         right = RHS(ins, 1)->u.cval;
5936                         mkaddr_const(state, ins, sdecl, left - right);
5937                 }
5938         }
5939 }
5940
5941 static void simplify_sl(struct compile_state *state, struct triple *ins)
5942 {
5943         if (is_const(RHS(ins, 1))) {
5944                 ulong_t right;
5945                 right = read_const(state, ins, &RHS(ins, 1));
5946                 if (right >= (size_of(state, ins->type)*8)) {
5947                         warning(state, ins, "left shift count >= width of type");
5948                 }
5949         }
5950         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5951                 ulong_t left, right;
5952                 left  = read_const(state, ins, &RHS(ins, 0));
5953                 right = read_const(state, ins, &RHS(ins, 1));
5954                 mkconst(state, ins,  left << right);
5955         }
5956 }
5957
5958 static void simplify_usr(struct compile_state *state, struct triple *ins)
5959 {
5960         if (is_const(RHS(ins, 1))) {
5961                 ulong_t right;
5962                 right = read_const(state, ins, &RHS(ins, 1));
5963                 if (right >= (size_of(state, ins->type)*8)) {
5964                         warning(state, ins, "right shift count >= width of type");
5965                 }
5966         }
5967         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5968                 ulong_t left, right;
5969                 left  = read_const(state, ins, &RHS(ins, 0));
5970                 right = read_const(state, ins, &RHS(ins, 1));
5971                 mkconst(state, ins, left >> right);
5972         }
5973 }
5974
5975 static void simplify_ssr(struct compile_state *state, struct triple *ins)
5976 {
5977         if (is_const(RHS(ins, 1))) {
5978                 ulong_t right;
5979                 right = read_const(state, ins, &RHS(ins, 1));
5980                 if (right >= (size_of(state, ins->type)*8)) {
5981                         warning(state, ins, "right shift count >= width of type");
5982                 }
5983         }
5984         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5985                 long_t left, right;
5986                 left  = read_sconst(ins, &RHS(ins, 0));
5987                 right = read_sconst(ins, &RHS(ins, 1));
5988                 mkconst(state, ins, left >> right);
5989         }
5990 }
5991
5992 static void simplify_and(struct compile_state *state, struct triple *ins)
5993 {
5994         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5995                 ulong_t left, right;
5996                 left  = read_const(state, ins, &RHS(ins, 0));
5997                 right = read_const(state, ins, &RHS(ins, 1));
5998                 mkconst(state, ins, left & right);
5999         }
6000 }
6001
6002 static void simplify_or(struct compile_state *state, struct triple *ins)
6003 {
6004         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6005                 ulong_t left, right;
6006                 left  = read_const(state, ins, &RHS(ins, 0));
6007                 right = read_const(state, ins, &RHS(ins, 1));
6008                 mkconst(state, ins, left | right);
6009         }
6010 }
6011
6012 static void simplify_xor(struct compile_state *state, struct triple *ins)
6013 {
6014         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6015                 ulong_t left, right;
6016                 left  = read_const(state, ins, &RHS(ins, 0));
6017                 right = read_const(state, ins, &RHS(ins, 1));
6018                 mkconst(state, ins, left ^ right);
6019         }
6020 }
6021
6022 static void simplify_pos(struct compile_state *state, struct triple *ins)
6023 {
6024         if (is_const(RHS(ins, 0))) {
6025                 mkconst(state, ins, RHS(ins, 0)->u.cval);
6026         }
6027         else {
6028                 mkcopy(state, ins, RHS(ins, 0));
6029         }
6030 }
6031
6032 static void simplify_neg(struct compile_state *state, struct triple *ins)
6033 {
6034         if (is_const(RHS(ins, 0))) {
6035                 ulong_t left;
6036                 left = read_const(state, ins, &RHS(ins, 0));
6037                 mkconst(state, ins, -left);
6038         }
6039         else if (RHS(ins, 0)->op == OP_NEG) {
6040                 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
6041         }
6042 }
6043
6044 static void simplify_invert(struct compile_state *state, struct triple *ins)
6045 {
6046         if (is_const(RHS(ins, 0))) {
6047                 ulong_t left;
6048                 left = read_const(state, ins, &RHS(ins, 0));
6049                 mkconst(state, ins, ~left);
6050         }
6051 }
6052
6053 static void simplify_eq(struct compile_state *state, struct triple *ins)
6054 {
6055         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6056                 ulong_t left, right;
6057                 left  = read_const(state, ins, &RHS(ins, 0));
6058                 right = read_const(state, ins, &RHS(ins, 1));
6059                 mkconst(state, ins, left == right);
6060         }
6061         else if (RHS(ins, 0) == RHS(ins, 1)) {
6062                 mkconst(state, ins, 1);
6063         }
6064 }
6065
6066 static void simplify_noteq(struct compile_state *state, struct triple *ins)
6067 {
6068         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6069                 ulong_t left, right;
6070                 left  = read_const(state, ins, &RHS(ins, 0));
6071                 right = read_const(state, ins, &RHS(ins, 1));
6072                 mkconst(state, ins, left != right);
6073         }
6074         else if (RHS(ins, 0) == RHS(ins, 1)) {
6075                 mkconst(state, ins, 0);
6076         }
6077 }
6078
6079 static void simplify_sless(struct compile_state *state, struct triple *ins)
6080 {
6081         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6082                 long_t left, right;
6083                 left  = read_sconst(ins, &RHS(ins, 0));
6084                 right = read_sconst(ins, &RHS(ins, 1));
6085                 mkconst(state, ins, left < right);
6086         }
6087         else if (RHS(ins, 0) == RHS(ins, 1)) {
6088                 mkconst(state, ins, 0);
6089         }
6090 }
6091
6092 static void simplify_uless(struct compile_state *state, struct triple *ins)
6093 {
6094         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6095                 ulong_t left, right;
6096                 left  = read_const(state, ins, &RHS(ins, 0));
6097                 right = read_const(state, ins, &RHS(ins, 1));
6098                 mkconst(state, ins, left < right);
6099         }
6100         else if (is_zero(RHS(ins, 0))) {
6101                 mkconst(state, ins, 1);
6102         }
6103         else if (RHS(ins, 0) == RHS(ins, 1)) {
6104                 mkconst(state, ins, 0);
6105         }
6106 }
6107
6108 static void simplify_smore(struct compile_state *state, struct triple *ins)
6109 {
6110         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6111                 long_t left, right;
6112                 left  = read_sconst(ins, &RHS(ins, 0));
6113                 right = read_sconst(ins, &RHS(ins, 1));
6114                 mkconst(state, ins, left > right);
6115         }
6116         else if (RHS(ins, 0) == RHS(ins, 1)) {
6117                 mkconst(state, ins, 0);
6118         }
6119 }
6120
6121 static void simplify_umore(struct compile_state *state, struct triple *ins)
6122 {
6123         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6124                 ulong_t left, right;
6125                 left  = read_const(state, ins, &RHS(ins, 0));
6126                 right = read_const(state, ins, &RHS(ins, 1));
6127                 mkconst(state, ins, left > right);
6128         }
6129         else if (is_zero(RHS(ins, 1))) {
6130                 mkconst(state, ins, 1);
6131         }
6132         else if (RHS(ins, 0) == RHS(ins, 1)) {
6133                 mkconst(state, ins, 0);
6134         }
6135 }
6136
6137
6138 static void simplify_slesseq(struct compile_state *state, struct triple *ins)
6139 {
6140         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6141                 long_t left, right;
6142                 left  = read_sconst(ins, &RHS(ins, 0));
6143                 right = read_sconst(ins, &RHS(ins, 1));
6144                 mkconst(state, ins, left <= right);
6145         }
6146         else if (RHS(ins, 0) == RHS(ins, 1)) {
6147                 mkconst(state, ins, 1);
6148         }
6149 }
6150
6151 static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
6152 {
6153         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6154                 ulong_t left, right;
6155                 left  = read_const(state, ins, &RHS(ins, 0));
6156                 right = read_const(state, ins, &RHS(ins, 1));
6157                 mkconst(state, ins, left <= right);
6158         }
6159         else if (is_zero(RHS(ins, 0))) {
6160                 mkconst(state, ins, 1);
6161         }
6162         else if (RHS(ins, 0) == RHS(ins, 1)) {
6163                 mkconst(state, ins, 1);
6164         }
6165 }
6166
6167 static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
6168 {
6169         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 0))) {
6170                 long_t left, right;
6171                 left  = read_sconst(ins, &RHS(ins, 0));
6172                 right = read_sconst(ins, &RHS(ins, 1));
6173                 mkconst(state, ins, left >= right);
6174         }
6175         else if (RHS(ins, 0) == RHS(ins, 1)) {
6176                 mkconst(state, ins, 1);
6177         }
6178 }
6179
6180 static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
6181 {
6182         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6183                 ulong_t left, right;
6184                 left  = read_const(state, ins, &RHS(ins, 0));
6185                 right = read_const(state, ins, &RHS(ins, 1));
6186                 mkconst(state, ins, left >= right);
6187         }
6188         else if (is_zero(RHS(ins, 1))) {
6189                 mkconst(state, ins, 1);
6190         }
6191         else if (RHS(ins, 0) == RHS(ins, 1)) {
6192                 mkconst(state, ins, 1);
6193         }
6194 }
6195
6196 static void simplify_lfalse(struct compile_state *state, struct triple *ins)
6197 {
6198         if (is_const(RHS(ins, 0))) {
6199                 ulong_t left;
6200                 left = read_const(state, ins, &RHS(ins, 0));
6201                 mkconst(state, ins, left == 0);
6202         }
6203         /* Otherwise if I am the only user... */
6204         else if ((RHS(ins, 0)->use->member == ins) && (RHS(ins, 0)->use->next == 0)) {
6205                 int need_copy = 1;
6206                 /* Invert a boolean operation */
6207                 switch(RHS(ins, 0)->op) {
6208                 case OP_LTRUE:   RHS(ins, 0)->op = OP_LFALSE;  break;
6209                 case OP_LFALSE:  RHS(ins, 0)->op = OP_LTRUE;   break;
6210                 case OP_EQ:      RHS(ins, 0)->op = OP_NOTEQ;   break;
6211                 case OP_NOTEQ:   RHS(ins, 0)->op = OP_EQ;      break;
6212                 case OP_SLESS:   RHS(ins, 0)->op = OP_SMOREEQ; break;
6213                 case OP_ULESS:   RHS(ins, 0)->op = OP_UMOREEQ; break;
6214                 case OP_SMORE:   RHS(ins, 0)->op = OP_SLESSEQ; break;
6215                 case OP_UMORE:   RHS(ins, 0)->op = OP_ULESSEQ; break;
6216                 case OP_SLESSEQ: RHS(ins, 0)->op = OP_SMORE;   break;
6217                 case OP_ULESSEQ: RHS(ins, 0)->op = OP_UMORE;   break;
6218                 case OP_SMOREEQ: RHS(ins, 0)->op = OP_SLESS;   break;
6219                 case OP_UMOREEQ: RHS(ins, 0)->op = OP_ULESS;   break;
6220                 default:
6221                         need_copy = 0;
6222                         break;
6223                 }
6224                 if (need_copy) {
6225                         mkcopy(state, ins, RHS(ins, 0));
6226                 }
6227         }
6228 }
6229
6230 static void simplify_ltrue (struct compile_state *state, struct triple *ins)
6231 {
6232         if (is_const(RHS(ins, 0))) {
6233                 ulong_t left;
6234                 left = read_const(state, ins, &RHS(ins, 0));
6235                 mkconst(state, ins, left != 0);
6236         }
6237         else switch(RHS(ins, 0)->op) {
6238         case OP_LTRUE:   case OP_LFALSE:  case OP_EQ:      case OP_NOTEQ:
6239         case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
6240         case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
6241                 mkcopy(state, ins, RHS(ins, 0));
6242         }
6243
6244 }
6245
6246 static void simplify_copy(struct compile_state *state, struct triple *ins)
6247 {
6248         if (is_const(RHS(ins, 0))) {
6249                 switch(RHS(ins, 0)->op) {
6250                 case OP_INTCONST:
6251                 {
6252                         ulong_t left;
6253                         left = read_const(state, ins, &RHS(ins, 0));
6254                         mkconst(state, ins, left);
6255                         break;
6256                 }
6257                 case OP_ADDRCONST:
6258                 {
6259                         struct triple *sdecl;
6260                         ulong_t offset;
6261                         sdecl  = MISC(RHS(ins, 0), 0);
6262                         offset = RHS(ins, 0)->u.cval;
6263                         mkaddr_const(state, ins, sdecl, offset);
6264                         break;
6265                 }
6266                 default:
6267                         internal_error(state, ins, "uknown constant");
6268                         break;
6269                 }
6270         }
6271 }
6272
6273 static void simplify_branch(struct compile_state *state, struct triple *ins)
6274 {
6275         struct block *block;
6276         if (ins->op != OP_BRANCH) {
6277                 internal_error(state, ins, "not branch");
6278         }
6279         if (ins->use != 0) {
6280                 internal_error(state, ins, "branch use");
6281         }
6282 #warning "FIXME implement simplify branch."
6283         /* The challenge here with simplify branch is that I need to 
6284          * make modifications to the control flow graph as well
6285          * as to the branch instruction itself.
6286          */
6287         block = ins->u.block;
6288         
6289         if (TRIPLE_RHS(ins->sizes) && is_const(RHS(ins, 0))) {
6290                 struct triple *targ;
6291                 ulong_t value;
6292                 value = read_const(state, ins, &RHS(ins, 0));
6293                 unuse_triple(RHS(ins, 0), ins);
6294                 targ = TARG(ins, 0);
6295                 ins->sizes = TRIPLE_SIZES(0, 0, 0, 1);
6296                 if (value) {
6297                         unuse_triple(ins->next, ins);
6298                         TARG(ins, 0) = targ;
6299                 }
6300                 else {
6301                         unuse_triple(targ, ins);
6302                         TARG(ins, 0) = ins->next;
6303                 }
6304 #warning "FIXME handle the case of making a branch unconditional"
6305         }
6306         if (TARG(ins, 0) == ins->next) {
6307                 unuse_triple(ins->next, ins);
6308                 if (TRIPLE_RHS(ins->sizes)) {
6309                         unuse_triple(RHS(ins, 0), ins);
6310                         unuse_triple(ins->next, ins);
6311                 }
6312                 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
6313                 ins->op = OP_NOOP;
6314                 if (ins->use) {
6315                         internal_error(state, ins, "noop use != 0");
6316                 }
6317 #warning "FIXME handle the case of killing a branch"
6318         }
6319 }
6320
6321 static void simplify_phi(struct compile_state *state, struct triple *ins)
6322 {
6323         struct triple **expr;
6324         ulong_t value;
6325         expr = triple_rhs(state, ins, 0);
6326         if (!*expr || !is_const(*expr)) {
6327                 return;
6328         }
6329         value = read_const(state, ins, expr);
6330         for(;expr;expr = triple_rhs(state, ins, expr)) {
6331                 if (!*expr || !is_const(*expr)) {
6332                         return;
6333                 }
6334                 if (value != read_const(state, ins, expr)) {
6335                         return;
6336                 }
6337         }
6338         mkconst(state, ins, value);
6339 }
6340
6341
6342 static void simplify_bsf(struct compile_state *state, struct triple *ins)
6343 {
6344         if (is_const(RHS(ins, 0))) {
6345                 ulong_t left;
6346                 left = read_const(state, ins, &RHS(ins, 0));
6347                 mkconst(state, ins, bsf(left));
6348         }
6349 }
6350
6351 static void simplify_bsr(struct compile_state *state, struct triple *ins)
6352 {
6353         if (is_const(RHS(ins, 0))) {
6354                 ulong_t left;
6355                 left = read_const(state, ins, &RHS(ins, 0));
6356                 mkconst(state, ins, bsr(left));
6357         }
6358 }
6359
6360
6361 typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
6362 static const simplify_t table_simplify[] = {
6363 #if 0
6364 #define simplify_smul     simplify_noop
6365 #define simplify_umul     simplify_noop
6366 #define simplify_sdiv     simplify_noop
6367 #define simplify_udiv     simplify_noop
6368 #define simplify_smod     simplify_noop
6369 #define simplify_umod     simplify_noop
6370 #endif
6371 #if 0
6372 #define simplify_add      simplify_noop
6373 #define simplify_sub      simplify_noop
6374 #endif
6375 #if 0
6376 #define simplify_sl       simplify_noop
6377 #define simplify_usr      simplify_noop
6378 #define simplify_ssr      simplify_noop
6379 #endif
6380 #if 0
6381 #define simplify_and      simplify_noop
6382 #define simplify_xor      simplify_noop
6383 #define simplify_or       simplify_noop
6384 #endif
6385 #if 0
6386 #define simplify_pos      simplify_noop
6387 #define simplify_neg      simplify_noop
6388 #define simplify_invert   simplify_noop
6389 #endif
6390
6391 #if 0
6392 #define simplify_eq       simplify_noop
6393 #define simplify_noteq    simplify_noop
6394 #endif
6395 #if 0
6396 #define simplify_sless    simplify_noop
6397 #define simplify_uless    simplify_noop
6398 #define simplify_smore    simplify_noop
6399 #define simplify_umore    simplify_noop
6400 #endif
6401 #if 0
6402 #define simplify_slesseq  simplify_noop
6403 #define simplify_ulesseq  simplify_noop
6404 #define simplify_smoreeq  simplify_noop
6405 #define simplify_umoreeq  simplify_noop
6406 #endif
6407 #if 0
6408 #define simplify_lfalse   simplify_noop
6409 #endif
6410 #if 0
6411 #define simplify_ltrue    simplify_noop
6412 #endif
6413
6414 #if 0
6415 #define simplify_copy     simplify_noop
6416 #endif
6417
6418 #if 0
6419 #define simplify_branch   simplify_noop
6420 #endif
6421
6422 #if 0
6423 #define simplify_phi      simplify_noop
6424 #endif
6425
6426 #if 0
6427 #define simplify_bsf      simplify_noop
6428 #define simplify_bsr      simplify_noop
6429 #endif
6430
6431 [OP_SMUL       ] = simplify_smul,
6432 [OP_UMUL       ] = simplify_umul,
6433 [OP_SDIV       ] = simplify_sdiv,
6434 [OP_UDIV       ] = simplify_udiv,
6435 [OP_SMOD       ] = simplify_smod,
6436 [OP_UMOD       ] = simplify_umod,
6437 [OP_ADD        ] = simplify_add,
6438 [OP_SUB        ] = simplify_sub,
6439 [OP_SL         ] = simplify_sl,
6440 [OP_USR        ] = simplify_usr,
6441 [OP_SSR        ] = simplify_ssr,
6442 [OP_AND        ] = simplify_and,
6443 [OP_XOR        ] = simplify_xor,
6444 [OP_OR         ] = simplify_or,
6445 [OP_POS        ] = simplify_pos,
6446 [OP_NEG        ] = simplify_neg,
6447 [OP_INVERT     ] = simplify_invert,
6448
6449 [OP_EQ         ] = simplify_eq,
6450 [OP_NOTEQ      ] = simplify_noteq,
6451 [OP_SLESS      ] = simplify_sless,
6452 [OP_ULESS      ] = simplify_uless,
6453 [OP_SMORE      ] = simplify_smore,
6454 [OP_UMORE      ] = simplify_umore,
6455 [OP_SLESSEQ    ] = simplify_slesseq,
6456 [OP_ULESSEQ    ] = simplify_ulesseq,
6457 [OP_SMOREEQ    ] = simplify_smoreeq,
6458 [OP_UMOREEQ    ] = simplify_umoreeq,
6459 [OP_LFALSE     ] = simplify_lfalse,
6460 [OP_LTRUE      ] = simplify_ltrue,
6461
6462 [OP_LOAD       ] = simplify_noop,
6463 [OP_STORE      ] = simplify_noop,
6464
6465 [OP_NOOP       ] = simplify_noop,
6466
6467 [OP_INTCONST   ] = simplify_noop,
6468 [OP_BLOBCONST  ] = simplify_noop,
6469 [OP_ADDRCONST  ] = simplify_noop,
6470
6471 [OP_WRITE      ] = simplify_noop,
6472 [OP_READ       ] = simplify_noop,
6473 [OP_COPY       ] = simplify_copy,
6474 [OP_PIECE      ] = simplify_noop,
6475 [OP_ASM        ] = simplify_noop,
6476
6477 [OP_DOT        ] = simplify_noop,
6478 [OP_VAL_VEC    ] = simplify_noop,
6479
6480 [OP_LIST       ] = simplify_noop,
6481 [OP_BRANCH     ] = simplify_branch,
6482 [OP_LABEL      ] = simplify_noop,
6483 [OP_ADECL      ] = simplify_noop,
6484 [OP_SDECL      ] = simplify_noop,
6485 [OP_PHI        ] = simplify_phi,
6486
6487 [OP_INB        ] = simplify_noop,
6488 [OP_INW        ] = simplify_noop,
6489 [OP_INL        ] = simplify_noop,
6490 [OP_OUTB       ] = simplify_noop,
6491 [OP_OUTW       ] = simplify_noop,
6492 [OP_OUTL       ] = simplify_noop,
6493 [OP_BSF        ] = simplify_bsf,
6494 [OP_BSR        ] = simplify_bsr,
6495 [OP_RDMSR      ] = simplify_noop,
6496 [OP_WRMSR      ] = simplify_noop,                    
6497 [OP_HLT        ] = simplify_noop,
6498 };
6499
6500 static void simplify(struct compile_state *state, struct triple *ins)
6501 {
6502         int op;
6503         simplify_t do_simplify;
6504         do {
6505                 op = ins->op;
6506                 do_simplify = 0;
6507                 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
6508                         do_simplify = 0;
6509                 }
6510                 else {
6511                         do_simplify = table_simplify[op];
6512                 }
6513                 if (!do_simplify) {
6514                         internal_error(state, ins, "cannot simplify op: %d %s\n",
6515                                 op, tops(op));
6516                         return;
6517                 }
6518                 do_simplify(state, ins);
6519         } while(ins->op != op);
6520 }
6521
6522 static void simplify_all(struct compile_state *state)
6523 {
6524         struct triple *ins, *first;
6525         first = RHS(state->main_function, 0);
6526         ins = first;
6527         do {
6528                 simplify(state, ins);
6529                 ins = ins->next;
6530         } while(ins != first);
6531 }
6532
6533 /*
6534  * Builtins....
6535  * ============================
6536  */
6537
6538 static void register_builtin_function(struct compile_state *state,
6539         const char *name, int op, struct type *rtype, ...)
6540 {
6541         struct type *ftype, *atype, *param, **next;
6542         struct triple *def, *arg, *result, *work, *last, *first;
6543         struct hash_entry *ident;
6544         struct file_state file;
6545         int parameters;
6546         int name_len;
6547         va_list args;
6548         int i;
6549
6550         /* Dummy file state to get debug handling right */
6551         memset(&file, 0, sizeof(file));
6552         file.basename = "<built-in>";
6553         file.line = 1;
6554         file.report_line = 1;
6555         file.report_name = file.basename;
6556         file.prev = state->file;
6557         state->file = &file;
6558         state->function = name;
6559
6560         /* Find the Parameter count */
6561         valid_op(state, op);
6562         parameters = table_ops[op].rhs;
6563         if (parameters < 0 ) {
6564                 internal_error(state, 0, "Invalid builtin parameter count");
6565         }
6566
6567         /* Find the function type */
6568         ftype = new_type(TYPE_FUNCTION, rtype, 0);
6569         next = &ftype->right;
6570         va_start(args, rtype);
6571         for(i = 0; i < parameters; i++) {
6572                 atype = va_arg(args, struct type *);
6573                 if (!*next) {
6574                         *next = atype;
6575                 } else {
6576                         *next = new_type(TYPE_PRODUCT, *next, atype);
6577                         next = &((*next)->right);
6578                 }
6579         }
6580         if (!*next) {
6581                 *next = &void_type;
6582         }
6583         va_end(args);
6584
6585         /* Generate the needed triples */
6586         def = triple(state, OP_LIST, ftype, 0, 0);
6587         first = label(state);
6588         RHS(def, 0) = first;
6589
6590         /* Now string them together */
6591         param = ftype->right;
6592         for(i = 0; i < parameters; i++) {
6593                 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6594                         atype = param->left;
6595                 } else {
6596                         atype = param;
6597                 }
6598                 arg = flatten(state, first, variable(state, atype));
6599                 param = param->right;
6600         }
6601         result = 0;
6602         if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
6603                 result = flatten(state, first, variable(state, rtype));
6604         }
6605         MISC(def, 0) = result;
6606         work = new_triple(state, op, rtype, -1, parameters);
6607         for(i = 0, arg = first->next; i < parameters; i++, arg = arg->next) {
6608                 RHS(work, i) = read_expr(state, arg);
6609         }
6610         if (result && ((rtype->type & TYPE_MASK) == TYPE_STRUCT)) {
6611                 struct triple *val;
6612                 /* Populate the LHS with the target registers */
6613                 work = flatten(state, first, work);
6614                 work->type = &void_type;
6615                 param = rtype->left;
6616                 if (rtype->elements != TRIPLE_LHS(work->sizes)) {
6617                         internal_error(state, 0, "Invalid result type");
6618                 }
6619                 val = new_triple(state, OP_VAL_VEC, rtype, -1, -1);
6620                 for(i = 0; i < rtype->elements; i++) {
6621                         struct triple *piece;
6622                         atype = param;
6623                         if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6624                                 atype = param->left;
6625                         }
6626                         if (!TYPE_ARITHMETIC(atype->type) &&
6627                                 !TYPE_PTR(atype->type)) {
6628                                 internal_error(state, 0, "Invalid lhs type");
6629                         }
6630                         piece = triple(state, OP_PIECE, atype, work, 0);
6631                         piece->u.cval = i;
6632                         LHS(work, i) = piece;
6633                         RHS(val, i) = piece;
6634                 }
6635                 work = val;
6636         }
6637         if (result) {
6638                 work = write_expr(state, result, work);
6639         }
6640         work = flatten(state, first, work);
6641         last = flatten(state, first, label(state));
6642         name_len = strlen(name);
6643         ident = lookup(state, name, name_len);
6644         symbol(state, ident, &ident->sym_ident, def, ftype);
6645         
6646         state->file = file.prev;
6647         state->function = 0;
6648 #if 0
6649         fprintf(stdout, "\n");
6650         loc(stdout, state, 0);
6651         fprintf(stdout, "\n__________ builtin_function _________\n");
6652         print_triple(state, def);
6653         fprintf(stdout, "__________ builtin_function _________ done\n\n");
6654 #endif
6655 }
6656
6657 static struct type *partial_struct(struct compile_state *state,
6658         const char *field_name, struct type *type, struct type *rest)
6659 {
6660         struct hash_entry *field_ident;
6661         struct type *result;
6662         int field_name_len;
6663
6664         field_name_len = strlen(field_name);
6665         field_ident = lookup(state, field_name, field_name_len);
6666
6667         result = clone_type(0, type);
6668         result->field_ident = field_ident;
6669
6670         if (rest) {
6671                 result = new_type(TYPE_PRODUCT, result, rest);
6672         }
6673         return result;
6674 }
6675
6676 static struct type *register_builtin_type(struct compile_state *state,
6677         const char *name, struct type *type)
6678 {
6679         struct hash_entry *ident;
6680         int name_len;
6681
6682         name_len = strlen(name);
6683         ident = lookup(state, name, name_len);
6684         
6685         if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6686                 ulong_t elements = 0;
6687                 struct type *field;
6688                 type = new_type(TYPE_STRUCT, type, 0);
6689                 field = type->left;
6690                 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
6691                         elements++;
6692                         field = field->right;
6693                 }
6694                 elements++;
6695                 symbol(state, ident, &ident->sym_struct, 0, type);
6696                 type->type_ident = ident;
6697                 type->elements = elements;
6698         }
6699         symbol(state, ident, &ident->sym_ident, 0, type);
6700         ident->tok = TOK_TYPE_NAME;
6701         return type;
6702 }
6703
6704
6705 static void register_builtins(struct compile_state *state)
6706 {
6707         struct type *msr_type;
6708
6709         register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type, 
6710                 &ushort_type);
6711         register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
6712                 &ushort_type);
6713         register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,   
6714                 &ushort_type);
6715
6716         register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type, 
6717                 &uchar_type, &ushort_type);
6718         register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type, 
6719                 &ushort_type, &ushort_type);
6720         register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type, 
6721                 &uint_type, &ushort_type);
6722         
6723         register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type, 
6724                 &int_type);
6725         register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type, 
6726                 &int_type);
6727
6728         msr_type = register_builtin_type(state, "__builtin_msr_t",
6729                 partial_struct(state, "lo", &ulong_type,
6730                 partial_struct(state, "hi", &ulong_type, 0)));
6731
6732         register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
6733                 &ulong_type);
6734         register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
6735                 &ulong_type, &ulong_type, &ulong_type);
6736         
6737         register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type, 
6738                 &void_type);
6739 }
6740
6741 static struct type *declarator(
6742         struct compile_state *state, struct type *type, 
6743         struct hash_entry **ident, int need_ident);
6744 static void decl(struct compile_state *state, struct triple *first);
6745 static struct type *specifier_qualifier_list(struct compile_state *state);
6746 static int isdecl_specifier(int tok);
6747 static struct type *decl_specifiers(struct compile_state *state);
6748 static int istype(int tok);
6749 static struct triple *expr(struct compile_state *state);
6750 static struct triple *assignment_expr(struct compile_state *state);
6751 static struct type *type_name(struct compile_state *state);
6752 static void statement(struct compile_state *state, struct triple *fist);
6753
6754 static struct triple *call_expr(
6755         struct compile_state *state, struct triple *func)
6756 {
6757         struct triple *def;
6758         struct type *param, *type;
6759         ulong_t pvals, index;
6760
6761         if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
6762                 error(state, 0, "Called object is not a function");
6763         }
6764         if (func->op != OP_LIST) {
6765                 internal_error(state, 0, "improper function");
6766         }
6767         eat(state, TOK_LPAREN);
6768         /* Find the return type without any specifiers */
6769         type = clone_type(0, func->type->left);
6770         def = new_triple(state, OP_CALL, func->type, -1, -1);
6771         def->type = type;
6772
6773         pvals = TRIPLE_RHS(def->sizes);
6774         MISC(def, 0) = func;
6775
6776         param = func->type->right;
6777         for(index = 0; index < pvals; index++) {
6778                 struct triple *val;
6779                 struct type *arg_type;
6780                 val = read_expr(state, assignment_expr(state));
6781                 arg_type = param;
6782                 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6783                         arg_type = param->left;
6784                 }
6785                 write_compatible(state, arg_type, val->type);
6786                 RHS(def, index) = val;
6787                 if (index != (pvals - 1)) {
6788                         eat(state, TOK_COMMA);
6789                         param = param->right;
6790                 }
6791         }
6792         eat(state, TOK_RPAREN);
6793         return def;
6794 }
6795
6796
6797 static struct triple *character_constant(struct compile_state *state)
6798 {
6799         struct triple *def;
6800         struct token *tk;
6801         const signed char *str, *end;
6802         int c;
6803         int str_len;
6804         eat(state, TOK_LIT_CHAR);
6805         tk = &state->token[0];
6806         str = tk->val.str + 1;
6807         str_len = tk->str_len - 2;
6808         if (str_len <= 0) {
6809                 error(state, 0, "empty character constant");
6810         }
6811         end = str + str_len;
6812         c = char_value(state, &str, end);
6813         if (str != end) {
6814                 error(state, 0, "multibyte character constant not supported");
6815         }
6816         def = int_const(state, &char_type, (ulong_t)((long_t)c));
6817         return def;
6818 }
6819
6820 static struct triple *string_constant(struct compile_state *state)
6821 {
6822         struct triple *def;
6823         struct token *tk;
6824         struct type *type;
6825         const signed char *str, *end;
6826         signed char *buf, *ptr;
6827         int str_len;
6828
6829         buf = 0;
6830         type = new_type(TYPE_ARRAY, &char_type, 0);
6831         type->elements = 0;
6832         /* The while loop handles string concatenation */
6833         do {
6834                 eat(state, TOK_LIT_STRING);
6835                 tk = &state->token[0];
6836                 str = tk->val.str + 1;
6837                 str_len = tk->str_len - 2;
6838                 if (str_len < 0) {
6839                         error(state, 0, "negative string constant length");
6840                 }
6841                 end = str + str_len;
6842                 ptr = buf;
6843                 buf = xmalloc(type->elements + str_len + 1, "string_constant");
6844                 memcpy(buf, ptr, type->elements);
6845                 ptr = buf + type->elements;
6846                 do {
6847                         *ptr++ = char_value(state, &str, end);
6848                 } while(str < end);
6849                 type->elements = ptr - buf;
6850         } while(peek(state) == TOK_LIT_STRING);
6851         *ptr = '\0';
6852         type->elements += 1;
6853         def = triple(state, OP_BLOBCONST, type, 0, 0);
6854         def->u.blob = buf;
6855         return def;
6856 }
6857
6858
6859 static struct triple *integer_constant(struct compile_state *state)
6860 {
6861         struct triple *def;
6862         unsigned long val;
6863         struct token *tk;
6864         char *end;
6865         int u, l, decimal;
6866         struct type *type;
6867
6868         eat(state, TOK_LIT_INT);
6869         tk = &state->token[0];
6870         errno = 0;
6871         decimal = (tk->val.str[0] != '0');
6872         val = strtoul(tk->val.str, &end, 0);
6873         if ((val == ULONG_MAX) && (errno == ERANGE)) {
6874                 error(state, 0, "Integer constant to large");
6875         }
6876         u = l = 0;
6877         if ((*end == 'u') || (*end == 'U')) {
6878                 u = 1;
6879                         end++;
6880         }
6881         if ((*end == 'l') || (*end == 'L')) {
6882                 l = 1;
6883                 end++;
6884         }
6885         if ((*end == 'u') || (*end == 'U')) {
6886                 u = 1;
6887                 end++;
6888         }
6889         if (*end) {
6890                 error(state, 0, "Junk at end of integer constant");
6891         }
6892         if (u && l)  {
6893                 type = &ulong_type;
6894         }
6895         else if (l) {
6896                 type = &long_type;
6897                 if (!decimal && (val > LONG_MAX)) {
6898                         type = &ulong_type;
6899                 }
6900         }
6901         else if (u) {
6902                 type = &uint_type;
6903                 if (val > UINT_MAX) {
6904                         type = &ulong_type;
6905                 }
6906         }
6907         else {
6908                 type = &int_type;
6909                 if (!decimal && (val > INT_MAX) && (val <= UINT_MAX)) {
6910                         type = &uint_type;
6911                 }
6912                 else if (!decimal && (val > LONG_MAX)) {
6913                         type = &ulong_type;
6914                 }
6915                 else if (val > INT_MAX) {
6916                         type = &long_type;
6917                 }
6918         }
6919         def = int_const(state, type, val);
6920         return def;
6921 }
6922
6923 static struct triple *primary_expr(struct compile_state *state)
6924 {
6925         struct triple *def;
6926         int tok;
6927         tok = peek(state);
6928         switch(tok) {
6929         case TOK_IDENT:
6930         {
6931                 struct hash_entry *ident;
6932                 /* Here ident is either:
6933                  * a varable name
6934                  * a function name
6935                  * an enumeration constant.
6936                  */
6937                 eat(state, TOK_IDENT);
6938                 ident = state->token[0].ident;
6939                 if (!ident->sym_ident) {
6940                         error(state, 0, "%s undeclared", ident->name);
6941                 }
6942                 def = ident->sym_ident->def;
6943                 break;
6944         }
6945         case TOK_ENUM_CONST:
6946                 /* Here ident is an enumeration constant */
6947                 eat(state, TOK_ENUM_CONST);
6948                 def = 0;
6949                 FINISHME();
6950                 break;
6951         case TOK_LPAREN:
6952                 eat(state, TOK_LPAREN);
6953                 def = expr(state);
6954                 eat(state, TOK_RPAREN);
6955                 break;
6956         case TOK_LIT_INT:
6957                 def = integer_constant(state);
6958                 break;
6959         case TOK_LIT_FLOAT:
6960                 eat(state, TOK_LIT_FLOAT);
6961                 error(state, 0, "Floating point constants not supported");
6962                 def = 0;
6963                 FINISHME();
6964                 break;
6965         case TOK_LIT_CHAR:
6966                 def = character_constant(state);
6967                 break;
6968         case TOK_LIT_STRING:
6969                 def = string_constant(state);
6970                 break;
6971         default:
6972                 def = 0;
6973                 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
6974         }
6975         return def;
6976 }
6977
6978 static struct triple *postfix_expr(struct compile_state *state)
6979 {
6980         struct triple *def;
6981         int postfix;
6982         def = primary_expr(state);
6983         do {
6984                 struct triple *left;
6985                 int tok;
6986                 postfix = 1;
6987                 left = def;
6988                 switch((tok = peek(state))) {
6989                 case TOK_LBRACKET:
6990                         eat(state, TOK_LBRACKET);
6991                         def = mk_subscript_expr(state, left, expr(state));
6992                         eat(state, TOK_RBRACKET);
6993                         break;
6994                 case TOK_LPAREN:
6995                         def = call_expr(state, def);
6996                         break;
6997                 case TOK_DOT:
6998                 {
6999                         struct hash_entry *field;
7000                         eat(state, TOK_DOT);
7001                         eat(state, TOK_IDENT);
7002                         field = state->token[0].ident;
7003                         def = deref_field(state, def, field);
7004                         break;
7005                 }
7006                 case TOK_ARROW:
7007                 {
7008                         struct hash_entry *field;
7009                         eat(state, TOK_ARROW);
7010                         eat(state, TOK_IDENT);
7011                         field = state->token[0].ident;
7012                         def = mk_deref_expr(state, read_expr(state, def));
7013                         def = deref_field(state, def, field);
7014                         break;
7015                 }
7016                 case TOK_PLUSPLUS:
7017                         eat(state, TOK_PLUSPLUS);
7018                         def = mk_post_inc_expr(state, left);
7019                         break;
7020                 case TOK_MINUSMINUS:
7021                         eat(state, TOK_MINUSMINUS);
7022                         def = mk_post_dec_expr(state, left);
7023                         break;
7024                 default:
7025                         postfix = 0;
7026                         break;
7027                 }
7028         } while(postfix);
7029         return def;
7030 }
7031
7032 static struct triple *cast_expr(struct compile_state *state);
7033
7034 static struct triple *unary_expr(struct compile_state *state)
7035 {
7036         struct triple *def, *right;
7037         int tok;
7038         switch((tok = peek(state))) {
7039         case TOK_PLUSPLUS:
7040                 eat(state, TOK_PLUSPLUS);
7041                 def = mk_pre_inc_expr(state, unary_expr(state));
7042                 break;
7043         case TOK_MINUSMINUS:
7044                 eat(state, TOK_MINUSMINUS);
7045                 def = mk_pre_dec_expr(state, unary_expr(state));
7046                 break;
7047         case TOK_AND:
7048                 eat(state, TOK_AND);
7049                 def = mk_addr_expr(state, cast_expr(state), 0);
7050                 break;
7051         case TOK_STAR:
7052                 eat(state, TOK_STAR);
7053                 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
7054                 break;
7055         case TOK_PLUS:
7056                 eat(state, TOK_PLUS);
7057                 right = read_expr(state, cast_expr(state));
7058                 arithmetic(state, right);
7059                 def = integral_promotion(state, right);
7060                 break;
7061         case TOK_MINUS:
7062                 eat(state, TOK_MINUS);
7063                 right = read_expr(state, cast_expr(state));
7064                 arithmetic(state, right);
7065                 def = integral_promotion(state, right);
7066                 def = triple(state, OP_NEG, def->type, def, 0);
7067                 break;
7068         case TOK_TILDE:
7069                 eat(state, TOK_TILDE);
7070                 right = read_expr(state, cast_expr(state));
7071                 integral(state, right);
7072                 def = integral_promotion(state, right);
7073                 def = triple(state, OP_INVERT, def->type, def, 0);
7074                 break;
7075         case TOK_BANG:
7076                 eat(state, TOK_BANG);
7077                 right = read_expr(state, cast_expr(state));
7078                 bool(state, right);
7079                 def = lfalse_expr(state, right);
7080                 break;
7081         case TOK_SIZEOF:
7082         {
7083                 struct type *type;
7084                 int tok1, tok2;
7085                 eat(state, TOK_SIZEOF);
7086                 tok1 = peek(state);
7087                 tok2 = peek2(state);
7088                 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7089                         eat(state, TOK_LPAREN);
7090                         type = type_name(state);
7091                         eat(state, TOK_RPAREN);
7092                 }
7093                 else {
7094                         struct triple *expr;
7095                         expr = unary_expr(state);
7096                         type = expr->type;
7097                         release_expr(state, expr);
7098                 }
7099                 def = int_const(state, &ulong_type, size_of(state, type));
7100                 break;
7101         }
7102         case TOK_ALIGNOF:
7103         {
7104                 struct type *type;
7105                 int tok1, tok2;
7106                 eat(state, TOK_ALIGNOF);
7107                 tok1 = peek(state);
7108                 tok2 = peek2(state);
7109                 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7110                         eat(state, TOK_LPAREN);
7111                         type = type_name(state);
7112                         eat(state, TOK_RPAREN);
7113                 }
7114                 else {
7115                         struct triple *expr;
7116                         expr = unary_expr(state);
7117                         type = expr->type;
7118                         release_expr(state, expr);
7119                 }
7120                 def = int_const(state, &ulong_type, align_of(state, type));
7121                 break;
7122         }
7123         default:
7124                 def = postfix_expr(state);
7125                 break;
7126         }
7127         return def;
7128 }
7129
7130 static struct triple *cast_expr(struct compile_state *state)
7131 {
7132         struct triple *def;
7133         int tok1, tok2;
7134         tok1 = peek(state);
7135         tok2 = peek2(state);
7136         if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7137                 struct type *type;
7138                 eat(state, TOK_LPAREN);
7139                 type = type_name(state);
7140                 eat(state, TOK_RPAREN);
7141                 def = read_expr(state, cast_expr(state));
7142                 def = triple(state, OP_COPY, type, def, 0);
7143         }
7144         else {
7145                 def = unary_expr(state);
7146         }
7147         return def;
7148 }
7149
7150 static struct triple *mult_expr(struct compile_state *state)
7151 {
7152         struct triple *def;
7153         int done;
7154         def = cast_expr(state);
7155         do {
7156                 struct triple *left, *right;
7157                 struct type *result_type;
7158                 int tok, op, sign;
7159                 done = 0;
7160                 switch(tok = (peek(state))) {
7161                 case TOK_STAR:
7162                 case TOK_DIV:
7163                 case TOK_MOD:
7164                         left = read_expr(state, def);
7165                         arithmetic(state, left);
7166
7167                         eat(state, tok);
7168
7169                         right = read_expr(state, cast_expr(state));
7170                         arithmetic(state, right);
7171
7172                         result_type = arithmetic_result(state, left, right);
7173                         sign = is_signed(result_type);
7174                         op = -1;
7175                         switch(tok) {
7176                         case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
7177                         case TOK_DIV:  op = sign? OP_SDIV : OP_UDIV; break;
7178                         case TOK_MOD:  op = sign? OP_SMOD : OP_UMOD; break;
7179                         }
7180                         def = triple(state, op, result_type, left, right);
7181                         break;
7182                 default:
7183                         done = 1;
7184                         break;
7185                 }
7186         } while(!done);
7187         return def;
7188 }
7189
7190 static struct triple *add_expr(struct compile_state *state)
7191 {
7192         struct triple *def;
7193         int done;
7194         def = mult_expr(state);
7195         do {
7196                 done = 0;
7197                 switch( peek(state)) {
7198                 case TOK_PLUS:
7199                         eat(state, TOK_PLUS);
7200                         def = mk_add_expr(state, def, mult_expr(state));
7201                         break;
7202                 case TOK_MINUS:
7203                         eat(state, TOK_MINUS);
7204                         def = mk_sub_expr(state, def, mult_expr(state));
7205                         break;
7206                 default:
7207                         done = 1;
7208                         break;
7209                 }
7210         } while(!done);
7211         return def;
7212 }
7213
7214 static struct triple *shift_expr(struct compile_state *state)
7215 {
7216         struct triple *def;
7217         int done;
7218         def = add_expr(state);
7219         do {
7220                 struct triple *left, *right;
7221                 int tok, op;
7222                 done = 0;
7223                 switch((tok = peek(state))) {
7224                 case TOK_SL:
7225                 case TOK_SR:
7226                         left = read_expr(state, def);
7227                         integral(state, left);
7228                         left = integral_promotion(state, left);
7229
7230                         eat(state, tok);
7231
7232                         right = read_expr(state, add_expr(state));
7233                         integral(state, right);
7234                         right = integral_promotion(state, right);
7235                         
7236                         op = (tok == TOK_SL)? OP_SL : 
7237                                 is_signed(left->type)? OP_SSR: OP_USR;
7238
7239                         def = triple(state, op, left->type, left, right);
7240                         break;
7241                 default:
7242                         done = 1;
7243                         break;
7244                 }
7245         } while(!done);
7246         return def;
7247 }
7248
7249 static struct triple *relational_expr(struct compile_state *state)
7250 {
7251 #warning "Extend relational exprs to work on more than arithmetic types"
7252         struct triple *def;
7253         int done;
7254         def = shift_expr(state);
7255         do {
7256                 struct triple *left, *right;
7257                 struct type *arg_type;
7258                 int tok, op, sign;
7259                 done = 0;
7260                 switch((tok = peek(state))) {
7261                 case TOK_LESS:
7262                 case TOK_MORE:
7263                 case TOK_LESSEQ:
7264                 case TOK_MOREEQ:
7265                         left = read_expr(state, def);
7266                         arithmetic(state, left);
7267
7268                         eat(state, tok);
7269
7270                         right = read_expr(state, shift_expr(state));
7271                         arithmetic(state, right);
7272
7273                         arg_type = arithmetic_result(state, left, right);
7274                         sign = is_signed(arg_type);
7275                         op = -1;
7276                         switch(tok) {
7277                         case TOK_LESS:   op = sign? OP_SLESS : OP_ULESS; break;
7278                         case TOK_MORE:   op = sign? OP_SMORE : OP_UMORE; break;
7279                         case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
7280                         case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
7281                         }
7282                         def = triple(state, op, &int_type, left, right);
7283                         break;
7284                 default:
7285                         done = 1;
7286                         break;
7287                 }
7288         } while(!done);
7289         return def;
7290 }
7291
7292 static struct triple *equality_expr(struct compile_state *state)
7293 {
7294 #warning "Extend equality exprs to work on more than arithmetic types"
7295         struct triple *def;
7296         int done;
7297         def = relational_expr(state);
7298         do {
7299                 struct triple *left, *right;
7300                 int tok, op;
7301                 done = 0;
7302                 switch((tok = peek(state))) {
7303                 case TOK_EQEQ:
7304                 case TOK_NOTEQ:
7305                         left = read_expr(state, def);
7306                         arithmetic(state, left);
7307                         eat(state, tok);
7308                         right = read_expr(state, relational_expr(state));
7309                         arithmetic(state, right);
7310                         op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
7311                         def = triple(state, op, &int_type, left, right);
7312                         break;
7313                 default:
7314                         done = 1;
7315                         break;
7316                 }
7317         } while(!done);
7318         return def;
7319 }
7320
7321 static struct triple *and_expr(struct compile_state *state)
7322 {
7323         struct triple *def;
7324         def = equality_expr(state);
7325         while(peek(state) == TOK_AND) {
7326                 struct triple *left, *right;
7327                 struct type *result_type;
7328                 left = read_expr(state, def);
7329                 integral(state, left);
7330                 eat(state, TOK_AND);
7331                 right = read_expr(state, equality_expr(state));
7332                 integral(state, right);
7333                 result_type = arithmetic_result(state, left, right);
7334                 def = triple(state, OP_AND, result_type, left, right);
7335         }
7336         return def;
7337 }
7338
7339 static struct triple *xor_expr(struct compile_state *state)
7340 {
7341         struct triple *def;
7342         def = and_expr(state);
7343         while(peek(state) == TOK_XOR) {
7344                 struct triple *left, *right;
7345                 struct type *result_type;
7346                 left = read_expr(state, def);
7347                 integral(state, left);
7348                 eat(state, TOK_XOR);
7349                 right = read_expr(state, and_expr(state));
7350                 integral(state, right);
7351                 result_type = arithmetic_result(state, left, right);
7352                 def = triple(state, OP_XOR, result_type, left, right);
7353         }
7354         return def;
7355 }
7356
7357 static struct triple *or_expr(struct compile_state *state)
7358 {
7359         struct triple *def;
7360         def = xor_expr(state);
7361         while(peek(state) == TOK_OR) {
7362                 struct triple *left, *right;
7363                 struct type *result_type;
7364                 left = read_expr(state, def);
7365                 integral(state, left);
7366                 eat(state, TOK_OR);
7367                 right = read_expr(state, xor_expr(state));
7368                 integral(state, right);
7369                 result_type = arithmetic_result(state, left, right);
7370                 def = triple(state, OP_OR, result_type, left, right);
7371         }
7372         return def;
7373 }
7374
7375 static struct triple *land_expr(struct compile_state *state)
7376 {
7377         struct triple *def;
7378         def = or_expr(state);
7379         while(peek(state) == TOK_LOGAND) {
7380                 struct triple *left, *right;
7381                 left = read_expr(state, def);
7382                 bool(state, left);
7383                 eat(state, TOK_LOGAND);
7384                 right = read_expr(state, or_expr(state));
7385                 bool(state, right);
7386
7387                 def = triple(state, OP_LAND, &int_type,
7388                         ltrue_expr(state, left),
7389                         ltrue_expr(state, right));
7390         }
7391         return def;
7392 }
7393
7394 static struct triple *lor_expr(struct compile_state *state)
7395 {
7396         struct triple *def;
7397         def = land_expr(state);
7398         while(peek(state) == TOK_LOGOR) {
7399                 struct triple *left, *right;
7400                 left = read_expr(state, def);
7401                 bool(state, left);
7402                 eat(state, TOK_LOGOR);
7403                 right = read_expr(state, land_expr(state));
7404                 bool(state, right);
7405                 
7406                 def = triple(state, OP_LOR, &int_type,
7407                         ltrue_expr(state, left),
7408                         ltrue_expr(state, right));
7409         }
7410         return def;
7411 }
7412
7413 static struct triple *conditional_expr(struct compile_state *state)
7414 {
7415         struct triple *def;
7416         def = lor_expr(state);
7417         if (peek(state) == TOK_QUEST) {
7418                 struct triple *test, *left, *right;
7419                 bool(state, def);
7420                 test = ltrue_expr(state, read_expr(state, def));
7421                 eat(state, TOK_QUEST);
7422                 left = read_expr(state, expr(state));
7423                 eat(state, TOK_COLON);
7424                 right = read_expr(state, conditional_expr(state));
7425
7426                 def = cond_expr(state, test, left, right);
7427         }
7428         return def;
7429 }
7430
7431 static struct triple *eval_const_expr(
7432         struct compile_state *state, struct triple *expr)
7433 {
7434         struct triple *def;
7435         struct triple *head, *ptr;
7436         head = label(state); /* dummy initial triple */
7437         flatten(state, head, expr);
7438         for(ptr = head->next; ptr != head; ptr = ptr->next) {
7439                 simplify(state, ptr);
7440         }
7441         /* Remove the constant value the tail of the list */
7442         def = head->prev;
7443         def->prev->next = def->next;
7444         def->next->prev = def->prev;
7445         def->next = def->prev = def;
7446         if (!is_const(def)) {
7447                 internal_error(state, 0, "Not a constant expression");
7448         }
7449         /* Free the intermediate expressions */
7450         while(head->next != head) {
7451                 release_triple(state, head->next);
7452         }
7453         free_triple(state, head);
7454         return def;
7455 }
7456
7457 static struct triple *constant_expr(struct compile_state *state)
7458 {
7459         return eval_const_expr(state, conditional_expr(state));
7460 }
7461
7462 static struct triple *assignment_expr(struct compile_state *state)
7463 {
7464         struct triple *def, *left, *right;
7465         int tok, op, sign;
7466         /* The C grammer in K&R shows assignment expressions
7467          * only taking unary expressions as input on their
7468          * left hand side.  But specifies the precedence of
7469          * assignemnt as the lowest operator except for comma.
7470          *
7471          * Allowing conditional expressions on the left hand side
7472          * of an assignement results in a grammar that accepts
7473          * a larger set of statements than standard C.   As long
7474          * as the subset of the grammar that is standard C behaves
7475          * correctly this should cause no problems.
7476          * 
7477          * For the extra token strings accepted by the grammar
7478          * none of them should produce a valid lvalue, so they
7479          * should not produce functioning programs.
7480          *
7481          * GCC has this bug as well, so surprises should be minimal.
7482          */
7483         def = conditional_expr(state);
7484         left = def;
7485         switch((tok = peek(state))) {
7486         case TOK_EQ:
7487                 lvalue(state, left);
7488                 eat(state, TOK_EQ);
7489                 def = write_expr(state, left, 
7490                         read_expr(state, assignment_expr(state)));
7491                 break;
7492         case TOK_TIMESEQ:
7493         case TOK_DIVEQ:
7494         case TOK_MODEQ:
7495                 lvalue(state, left);
7496                 arithmetic(state, left);
7497                 eat(state, tok);
7498                 right = read_expr(state, assignment_expr(state));
7499                 arithmetic(state, right);
7500
7501                 sign = is_signed(left->type);
7502                 op = -1;
7503                 switch(tok) {
7504                 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
7505                 case TOK_DIVEQ:   op = sign? OP_SDIV : OP_UDIV; break;
7506                 case TOK_MODEQ:   op = sign? OP_SMOD : OP_UMOD; break;
7507                 }
7508                 def = write_expr(state, left,
7509                         triple(state, op, left->type, 
7510                                 read_expr(state, left), right));
7511                 break;
7512         case TOK_PLUSEQ:
7513                 lvalue(state, left);
7514                 eat(state, TOK_PLUSEQ);
7515                 def = write_expr(state, left,
7516                         mk_add_expr(state, left, assignment_expr(state)));
7517                 break;
7518         case TOK_MINUSEQ:
7519                 lvalue(state, left);
7520                 eat(state, TOK_MINUSEQ);
7521                 def = write_expr(state, left,
7522                         mk_sub_expr(state, left, assignment_expr(state)));
7523                 break;
7524         case TOK_SLEQ:
7525         case TOK_SREQ:
7526         case TOK_ANDEQ:
7527         case TOK_XOREQ:
7528         case TOK_OREQ:
7529                 lvalue(state, left);
7530                 integral(state, left);
7531                 eat(state, tok);
7532                 right = read_expr(state, assignment_expr(state));
7533                 integral(state, right);
7534                 right = integral_promotion(state, right);
7535                 sign = is_signed(left->type);
7536                 op = -1;
7537                 switch(tok) {
7538                 case TOK_SLEQ:  op = OP_SL; break;
7539                 case TOK_SREQ:  op = sign? OP_SSR: OP_USR; break;
7540                 case TOK_ANDEQ: op = OP_AND; break;
7541                 case TOK_XOREQ: op = OP_XOR; break;
7542                 case TOK_OREQ:  op = OP_OR; break;
7543                 }
7544                 def = write_expr(state, left,
7545                         triple(state, op, left->type, 
7546                                 read_expr(state, left), right));
7547                 break;
7548         }
7549         return def;
7550 }
7551
7552 static struct triple *expr(struct compile_state *state)
7553 {
7554         struct triple *def;
7555         def = assignment_expr(state);
7556         while(peek(state) == TOK_COMMA) {
7557                 struct triple *left, *right;
7558                 left = def;
7559                 eat(state, TOK_COMMA);
7560                 right = assignment_expr(state);
7561                 def = triple(state, OP_COMMA, right->type, left, right);
7562         }
7563         return def;
7564 }
7565
7566 static void expr_statement(struct compile_state *state, struct triple *first)
7567 {
7568         if (peek(state) != TOK_SEMI) {
7569                 flatten(state, first, expr(state));
7570         }
7571         eat(state, TOK_SEMI);
7572 }
7573
7574 static void if_statement(struct compile_state *state, struct triple *first)
7575 {
7576         struct triple *test, *jmp1, *jmp2, *middle, *end;
7577
7578         jmp1 = jmp2 = middle = 0;
7579         eat(state, TOK_IF);
7580         eat(state, TOK_LPAREN);
7581         test = expr(state);
7582         bool(state, test);
7583         /* Cleanup and invert the test */
7584         test = lfalse_expr(state, read_expr(state, test));
7585         eat(state, TOK_RPAREN);
7586         /* Generate the needed pieces */
7587         middle = label(state);
7588         jmp1 = branch(state, middle, test);
7589         /* Thread the pieces together */
7590         flatten(state, first, test);
7591         flatten(state, first, jmp1);
7592         flatten(state, first, label(state));
7593         statement(state, first);
7594         if (peek(state) == TOK_ELSE) {
7595                 eat(state, TOK_ELSE);
7596                 /* Generate the rest of the pieces */
7597                 end = label(state);
7598                 jmp2 = branch(state, end, 0);
7599                 /* Thread them together */
7600                 flatten(state, first, jmp2);
7601                 flatten(state, first, middle);
7602                 statement(state, first);
7603                 flatten(state, first, end);
7604         }
7605         else {
7606                 flatten(state, first, middle);
7607         }
7608 }
7609
7610 static void for_statement(struct compile_state *state, struct triple *first)
7611 {
7612         struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
7613         struct triple *label1, *label2, *label3;
7614         struct hash_entry *ident;
7615
7616         eat(state, TOK_FOR);
7617         eat(state, TOK_LPAREN);
7618         head = test = tail = jmp1 = jmp2 = 0;
7619         if (peek(state) != TOK_SEMI) {
7620                 head = expr(state);
7621         } 
7622         eat(state, TOK_SEMI);
7623         if (peek(state) != TOK_SEMI) {
7624                 test = expr(state);
7625                 bool(state, test);
7626                 test = ltrue_expr(state, read_expr(state, test));
7627         }
7628         eat(state, TOK_SEMI);
7629         if (peek(state) != TOK_RPAREN) {
7630                 tail = expr(state);
7631         }
7632         eat(state, TOK_RPAREN);
7633         /* Generate the needed pieces */
7634         label1 = label(state);
7635         label2 = label(state);
7636         label3 = label(state);
7637         if (test) {
7638                 jmp1 = branch(state, label3, 0);
7639                 jmp2 = branch(state, label1, test);
7640         }
7641         else {
7642                 jmp2 = branch(state, label1, 0);
7643         }
7644         end = label(state);
7645         /* Remember where break and continue go */
7646         start_scope(state);
7647         ident = state->i_break;
7648         symbol(state, ident, &ident->sym_ident, end, end->type);
7649         ident = state->i_continue;
7650         symbol(state, ident, &ident->sym_ident, label2, label2->type);
7651         /* Now include the body */
7652         flatten(state, first, head);
7653         flatten(state, first, jmp1);
7654         flatten(state, first, label1);
7655         statement(state, first);
7656         flatten(state, first, label2);
7657         flatten(state, first, tail);
7658         flatten(state, first, label3);
7659         flatten(state, first, test);
7660         flatten(state, first, jmp2);
7661         flatten(state, first, end);
7662         /* Cleanup the break/continue scope */
7663         end_scope(state);
7664 }
7665
7666 static void while_statement(struct compile_state *state, struct triple *first)
7667 {
7668         struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
7669         struct hash_entry *ident;
7670         eat(state, TOK_WHILE);
7671         eat(state, TOK_LPAREN);
7672         test = expr(state);
7673         bool(state, test);
7674         test = ltrue_expr(state, read_expr(state, test));
7675         eat(state, TOK_RPAREN);
7676         /* Generate the needed pieces */
7677         label1 = label(state);
7678         label2 = label(state);
7679         jmp1 = branch(state, label2, 0);
7680         jmp2 = branch(state, label1, test);
7681         end = label(state);
7682         /* Remember where break and continue go */
7683         start_scope(state);
7684         ident = state->i_break;
7685         symbol(state, ident, &ident->sym_ident, end, end->type);
7686         ident = state->i_continue;
7687         symbol(state, ident, &ident->sym_ident, label2, label2->type);
7688         /* Thread them together */
7689         flatten(state, first, jmp1);
7690         flatten(state, first, label1);
7691         statement(state, first);
7692         flatten(state, first, label2);
7693         flatten(state, first, test);
7694         flatten(state, first, jmp2);
7695         flatten(state, first, end);
7696         /* Cleanup the break/continue scope */
7697         end_scope(state);
7698 }
7699
7700 static void do_statement(struct compile_state *state, struct triple *first)
7701 {
7702         struct triple *label1, *label2, *test, *end;
7703         struct hash_entry *ident;
7704         eat(state, TOK_DO);
7705         /* Generate the needed pieces */
7706         label1 = label(state);
7707         label2 = label(state);
7708         end = label(state);
7709         /* Remember where break and continue go */
7710         start_scope(state);
7711         ident = state->i_break;
7712         symbol(state, ident, &ident->sym_ident, end, end->type);
7713         ident = state->i_continue;
7714         symbol(state, ident, &ident->sym_ident, label2, label2->type);
7715         /* Now include the body */
7716         flatten(state, first, label1);
7717         statement(state, first);
7718         /* Cleanup the break/continue scope */
7719         end_scope(state);
7720         /* Eat the rest of the loop */
7721         eat(state, TOK_WHILE);
7722         eat(state, TOK_LPAREN);
7723         test = read_expr(state, expr(state));
7724         bool(state, test);
7725         eat(state, TOK_RPAREN);
7726         eat(state, TOK_SEMI);
7727         /* Thread the pieces together */
7728         test = ltrue_expr(state, test);
7729         flatten(state, first, label2);
7730         flatten(state, first, test);
7731         flatten(state, first, branch(state, label1, test));
7732         flatten(state, first, end);
7733 }
7734
7735
7736 static void return_statement(struct compile_state *state, struct triple *first)
7737 {
7738         struct triple *jmp, *mv, *dest, *var, *val;
7739         int last;
7740         eat(state, TOK_RETURN);
7741
7742 #warning "FIXME implement a more general excess branch elimination"
7743         val = 0;
7744         /* If we have a return value do some more work */
7745         if (peek(state) != TOK_SEMI) {
7746                 val = read_expr(state, expr(state));
7747         }
7748         eat(state, TOK_SEMI);
7749
7750         /* See if this last statement in a function */
7751         last = ((peek(state) == TOK_RBRACE) && 
7752                 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
7753
7754         /* Find the return variable */
7755         var = MISC(state->main_function, 0);
7756         /* Find the return destination */
7757         dest = RHS(state->main_function, 0)->prev;
7758         mv = jmp = 0;
7759         /* If needed generate a jump instruction */
7760         if (!last) {
7761                 jmp = branch(state, dest, 0);
7762         }
7763         /* If needed generate an assignment instruction */
7764         if (val) {
7765                 mv = write_expr(state, var, val);
7766         }
7767         /* Now put the code together */
7768         if (mv) {
7769                 flatten(state, first, mv);
7770                 flatten(state, first, jmp);
7771         }
7772         else if (jmp) {
7773                 flatten(state, first, jmp);
7774         }
7775 }
7776
7777 static void break_statement(struct compile_state *state, struct triple *first)
7778 {
7779         struct triple *dest;
7780         eat(state, TOK_BREAK);
7781         eat(state, TOK_SEMI);
7782         if (!state->i_break->sym_ident) {
7783                 error(state, 0, "break statement not within loop or switch");
7784         }
7785         dest = state->i_break->sym_ident->def;
7786         flatten(state, first, branch(state, dest, 0));
7787 }
7788
7789 static void continue_statement(struct compile_state *state, struct triple *first)
7790 {
7791         struct triple *dest;
7792         eat(state, TOK_CONTINUE);
7793         eat(state, TOK_SEMI);
7794         if (!state->i_continue->sym_ident) {
7795                 error(state, 0, "continue statement outside of a loop");
7796         }
7797         dest = state->i_continue->sym_ident->def;
7798         flatten(state, first, branch(state, dest, 0));
7799 }
7800
7801 static void goto_statement(struct compile_state *state, struct triple *first)
7802 {
7803         struct hash_entry *ident;
7804         eat(state, TOK_GOTO);
7805         eat(state, TOK_IDENT);
7806         ident = state->token[0].ident;
7807         if (!ident->sym_label) {
7808                 /* If this is a forward branch allocate the label now,
7809                  * it will be flattend in the appropriate location later.
7810                  */
7811                 struct triple *ins;
7812                 ins = label(state);
7813                 label_symbol(state, ident, ins);
7814         }
7815         eat(state, TOK_SEMI);
7816
7817         flatten(state, first, branch(state, ident->sym_label->def, 0));
7818 }
7819
7820 static void labeled_statement(struct compile_state *state, struct triple *first)
7821 {
7822         struct triple *ins;
7823         struct hash_entry *ident;
7824         eat(state, TOK_IDENT);
7825
7826         ident = state->token[0].ident;
7827         if (ident->sym_label && ident->sym_label->def) {
7828                 ins = ident->sym_label->def;
7829                 put_occurance(ins->occurance);
7830                 ins->occurance = new_occurance(state);
7831         }
7832         else {
7833                 ins = label(state);
7834                 label_symbol(state, ident, ins);
7835         }
7836         if (ins->id & TRIPLE_FLAG_FLATTENED) {
7837                 error(state, 0, "label %s already defined", ident->name);
7838         }
7839         flatten(state, first, ins);
7840
7841         eat(state, TOK_COLON);
7842         statement(state, first);
7843 }
7844
7845 static void switch_statement(struct compile_state *state, struct triple *first)
7846 {
7847         FINISHME();
7848         eat(state, TOK_SWITCH);
7849         eat(state, TOK_LPAREN);
7850         expr(state);
7851         eat(state, TOK_RPAREN);
7852         statement(state, first);
7853         error(state, 0, "switch statements are not implemented");
7854         FINISHME();
7855 }
7856
7857 static void case_statement(struct compile_state *state, struct triple *first)
7858 {
7859         FINISHME();
7860         eat(state, TOK_CASE);
7861         constant_expr(state);
7862         eat(state, TOK_COLON);
7863         statement(state, first);
7864         error(state, 0, "case statements are not implemented");
7865         FINISHME();
7866 }
7867
7868 static void default_statement(struct compile_state *state, struct triple *first)
7869 {
7870         FINISHME();
7871         eat(state, TOK_DEFAULT);
7872         eat(state, TOK_COLON);
7873         statement(state, first);
7874         error(state, 0, "default statements are not implemented");
7875         FINISHME();
7876 }
7877
7878 static void asm_statement(struct compile_state *state, struct triple *first)
7879 {
7880         struct asm_info *info;
7881         struct {
7882                 struct triple *constraint;
7883                 struct triple *expr;
7884         } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
7885         struct triple *def, *asm_str;
7886         int out, in, clobbers, more, colons, i;
7887
7888         eat(state, TOK_ASM);
7889         /* For now ignore the qualifiers */
7890         switch(peek(state)) {
7891         case TOK_CONST:
7892                 eat(state, TOK_CONST);
7893                 break;
7894         case TOK_VOLATILE:
7895                 eat(state, TOK_VOLATILE);
7896                 break;
7897         }
7898         eat(state, TOK_LPAREN);
7899         asm_str = string_constant(state);
7900
7901         colons = 0;
7902         out = in = clobbers = 0;
7903         /* Outputs */
7904         if ((colons == 0) && (peek(state) == TOK_COLON)) {
7905                 eat(state, TOK_COLON);
7906                 colons++;
7907                 more = (peek(state) == TOK_LIT_STRING);
7908                 while(more) {
7909                         struct triple *var;
7910                         struct triple *constraint;
7911                         char *str;
7912                         more = 0;
7913                         if (out > MAX_LHS) {
7914                                 error(state, 0, "Maximum output count exceeded.");
7915                         }
7916                         constraint = string_constant(state);
7917                         str = constraint->u.blob;
7918                         if (str[0] != '=') {
7919                                 error(state, 0, "Output constraint does not start with =");
7920                         }
7921                         constraint->u.blob = str + 1;
7922                         eat(state, TOK_LPAREN);
7923                         var = conditional_expr(state);
7924                         eat(state, TOK_RPAREN);
7925
7926                         lvalue(state, var);
7927                         out_param[out].constraint = constraint;
7928                         out_param[out].expr       = var;
7929                         if (peek(state) == TOK_COMMA) {
7930                                 eat(state, TOK_COMMA);
7931                                 more = 1;
7932                         }
7933                         out++;
7934                 }
7935         }
7936         /* Inputs */
7937         if ((colons == 1) && (peek(state) == TOK_COLON)) {
7938                 eat(state, TOK_COLON);
7939                 colons++;
7940                 more = (peek(state) == TOK_LIT_STRING);
7941                 while(more) {
7942                         struct triple *val;
7943                         struct triple *constraint;
7944                         char *str;
7945                         more = 0;
7946                         if (in > MAX_RHS) {
7947                                 error(state, 0, "Maximum input count exceeded.");
7948                         }
7949                         constraint = string_constant(state);
7950                         str = constraint->u.blob;
7951                         if (digitp(str[0] && str[1] == '\0')) {
7952                                 int val;
7953                                 val = digval(str[0]);
7954                                 if ((val < 0) || (val >= out)) {
7955                                         error(state, 0, "Invalid input constraint %d", val);
7956                                 }
7957                         }
7958                         eat(state, TOK_LPAREN);
7959                         val = conditional_expr(state);
7960                         eat(state, TOK_RPAREN);
7961
7962                         in_param[in].constraint = constraint;
7963                         in_param[in].expr       = val;
7964                         if (peek(state) == TOK_COMMA) {
7965                                 eat(state, TOK_COMMA);
7966                                 more = 1;
7967                         }
7968                         in++;
7969                 }
7970         }
7971
7972         /* Clobber */
7973         if ((colons == 2) && (peek(state) == TOK_COLON)) {
7974                 eat(state, TOK_COLON);
7975                 colons++;
7976                 more = (peek(state) == TOK_LIT_STRING);
7977                 while(more) {
7978                         struct triple *clobber;
7979                         more = 0;
7980                         if ((clobbers + out) > MAX_LHS) {
7981                                 error(state, 0, "Maximum clobber limit exceeded.");
7982                         }
7983                         clobber = string_constant(state);
7984                         eat(state, TOK_RPAREN);
7985
7986                         clob_param[clobbers].constraint = clobber;
7987                         if (peek(state) == TOK_COMMA) {
7988                                 eat(state, TOK_COMMA);
7989                                 more = 1;
7990                         }
7991                         clobbers++;
7992                 }
7993         }
7994         eat(state, TOK_RPAREN);
7995         eat(state, TOK_SEMI);
7996
7997
7998         info = xcmalloc(sizeof(*info), "asm_info");
7999         info->str = asm_str->u.blob;
8000         free_triple(state, asm_str);
8001
8002         def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
8003         def->u.ainfo = info;
8004
8005         /* Find the register constraints */
8006         for(i = 0; i < out; i++) {
8007                 struct triple *constraint;
8008                 constraint = out_param[i].constraint;
8009                 info->tmpl.lhs[i] = arch_reg_constraint(state, 
8010                         out_param[i].expr->type, constraint->u.blob);
8011                 free_triple(state, constraint);
8012         }
8013         for(; i - out < clobbers; i++) {
8014                 struct triple *constraint;
8015                 constraint = clob_param[i - out].constraint;
8016                 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
8017                 free_triple(state, constraint);
8018         }
8019         for(i = 0; i < in; i++) {
8020                 struct triple *constraint;
8021                 const char *str;
8022                 constraint = in_param[i].constraint;
8023                 str = constraint->u.blob;
8024                 if (digitp(str[0]) && str[1] == '\0') {
8025                         struct reg_info cinfo;
8026                         int val;
8027                         val = digval(str[0]);
8028                         cinfo.reg = info->tmpl.lhs[val].reg;
8029                         cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
8030                         cinfo.regcm &= info->tmpl.lhs[val].regcm;
8031                         if (cinfo.reg == REG_UNSET) {
8032                                 cinfo.reg = REG_VIRT0 + val;
8033                         }
8034                         if (cinfo.regcm == 0) {
8035                                 error(state, 0, "No registers for %d", val);
8036                         }
8037                         info->tmpl.lhs[val] = cinfo;
8038                         info->tmpl.rhs[i]   = cinfo;
8039                                 
8040                 } else {
8041                         info->tmpl.rhs[i] = arch_reg_constraint(state, 
8042                                 in_param[i].expr->type, str);
8043                 }
8044                 free_triple(state, constraint);
8045         }
8046
8047         /* Now build the helper expressions */
8048         for(i = 0; i < in; i++) {
8049                 RHS(def, i) = read_expr(state,in_param[i].expr);
8050         }
8051         flatten(state, first, def);
8052         for(i = 0; i < out; i++) {
8053                 struct triple *piece;
8054                 piece = triple(state, OP_PIECE, out_param[i].expr->type, def, 0);
8055                 piece->u.cval = i;
8056                 LHS(def, i) = piece;
8057                 flatten(state, first,
8058                         write_expr(state, out_param[i].expr, piece));
8059         }
8060         for(; i - out < clobbers; i++) {
8061                 struct triple *piece;
8062                 piece = triple(state, OP_PIECE, &void_type, def, 0);
8063                 piece->u.cval = i;
8064                 LHS(def, i) = piece;
8065                 flatten(state, first, piece);
8066         }
8067 }
8068
8069
8070 static int isdecl(int tok)
8071 {
8072         switch(tok) {
8073         case TOK_AUTO:
8074         case TOK_REGISTER:
8075         case TOK_STATIC:
8076         case TOK_EXTERN:
8077         case TOK_TYPEDEF:
8078         case TOK_CONST:
8079         case TOK_RESTRICT:
8080         case TOK_VOLATILE:
8081         case TOK_VOID:
8082         case TOK_CHAR:
8083         case TOK_SHORT:
8084         case TOK_INT:
8085         case TOK_LONG:
8086         case TOK_FLOAT:
8087         case TOK_DOUBLE:
8088         case TOK_SIGNED:
8089         case TOK_UNSIGNED:
8090         case TOK_STRUCT:
8091         case TOK_UNION:
8092         case TOK_ENUM:
8093         case TOK_TYPE_NAME: /* typedef name */
8094                 return 1;
8095         default:
8096                 return 0;
8097         }
8098 }
8099
8100 static void compound_statement(struct compile_state *state, struct triple *first)
8101 {
8102         eat(state, TOK_LBRACE);
8103         start_scope(state);
8104
8105         /* statement-list opt */
8106         while (peek(state) != TOK_RBRACE) {
8107                 statement(state, first);
8108         }
8109         end_scope(state);
8110         eat(state, TOK_RBRACE);
8111 }
8112
8113 static void statement(struct compile_state *state, struct triple *first)
8114 {
8115         int tok;
8116         tok = peek(state);
8117         if (tok == TOK_LBRACE) {
8118                 compound_statement(state, first);
8119         }
8120         else if (tok == TOK_IF) {
8121                 if_statement(state, first); 
8122         }
8123         else if (tok == TOK_FOR) {
8124                 for_statement(state, first);
8125         }
8126         else if (tok == TOK_WHILE) {
8127                 while_statement(state, first);
8128         }
8129         else if (tok == TOK_DO) {
8130                 do_statement(state, first);
8131         }
8132         else if (tok == TOK_RETURN) {
8133                 return_statement(state, first);
8134         }
8135         else if (tok == TOK_BREAK) {
8136                 break_statement(state, first);
8137         }
8138         else if (tok == TOK_CONTINUE) {
8139                 continue_statement(state, first);
8140         }
8141         else if (tok == TOK_GOTO) {
8142                 goto_statement(state, first);
8143         }
8144         else if (tok == TOK_SWITCH) {
8145                 switch_statement(state, first);
8146         }
8147         else if (tok == TOK_ASM) {
8148                 asm_statement(state, first);
8149         }
8150         else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
8151                 labeled_statement(state, first); 
8152         }
8153         else if (tok == TOK_CASE) {
8154                 case_statement(state, first);
8155         }
8156         else if (tok == TOK_DEFAULT) {
8157                 default_statement(state, first);
8158         }
8159         else if (isdecl(tok)) {
8160                 /* This handles C99 intermixing of statements and decls */
8161                 decl(state, first);
8162         }
8163         else {
8164                 expr_statement(state, first);
8165         }
8166 }
8167
8168 static struct type *param_decl(struct compile_state *state)
8169 {
8170         struct type *type;
8171         struct hash_entry *ident;
8172         /* Cheat so the declarator will know we are not global */
8173         start_scope(state); 
8174         ident = 0;
8175         type = decl_specifiers(state);
8176         type = declarator(state, type, &ident, 0);
8177         type->field_ident = ident;
8178         end_scope(state);
8179         return type;
8180 }
8181
8182 static struct type *param_type_list(struct compile_state *state, struct type *type)
8183 {
8184         struct type *ftype, **next;
8185         ftype = new_type(TYPE_FUNCTION, type, param_decl(state));
8186         next = &ftype->right;
8187         while(peek(state) == TOK_COMMA) {
8188                 eat(state, TOK_COMMA);
8189                 if (peek(state) == TOK_DOTS) {
8190                         eat(state, TOK_DOTS);
8191                         error(state, 0, "variadic functions not supported");
8192                 }
8193                 else {
8194                         *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
8195                         next = &((*next)->right);
8196                 }
8197         }
8198         return ftype;
8199 }
8200
8201
8202 static struct type *type_name(struct compile_state *state)
8203 {
8204         struct type *type;
8205         type = specifier_qualifier_list(state);
8206         /* abstract-declarator (may consume no tokens) */
8207         type = declarator(state, type, 0, 0);
8208         return type;
8209 }
8210
8211 static struct type *direct_declarator(
8212         struct compile_state *state, struct type *type, 
8213         struct hash_entry **ident, int need_ident)
8214 {
8215         struct type *outer;
8216         int op;
8217         outer = 0;
8218         arrays_complete(state, type);
8219         switch(peek(state)) {
8220         case TOK_IDENT:
8221                 eat(state, TOK_IDENT);
8222                 if (!ident) {
8223                         error(state, 0, "Unexpected identifier found");
8224                 }
8225                 /* The name of what we are declaring */
8226                 *ident = state->token[0].ident;
8227                 break;
8228         case TOK_LPAREN:
8229                 eat(state, TOK_LPAREN);
8230                 outer = declarator(state, type, ident, need_ident);
8231                 eat(state, TOK_RPAREN);
8232                 break;
8233         default:
8234                 if (need_ident) {
8235                         error(state, 0, "Identifier expected");
8236                 }
8237                 break;
8238         }
8239         do {
8240                 op = 1;
8241                 arrays_complete(state, type);
8242                 switch(peek(state)) {
8243                 case TOK_LPAREN:
8244                         eat(state, TOK_LPAREN);
8245                         type = param_type_list(state, type);
8246                         eat(state, TOK_RPAREN);
8247                         break;
8248                 case TOK_LBRACKET:
8249                 {
8250                         unsigned int qualifiers;
8251                         struct triple *value;
8252                         value = 0;
8253                         eat(state, TOK_LBRACKET);
8254                         if (peek(state) != TOK_RBRACKET) {
8255                                 value = constant_expr(state);
8256                                 integral(state, value);
8257                         }
8258                         eat(state, TOK_RBRACKET);
8259
8260                         qualifiers = type->type & (QUAL_MASK | STOR_MASK);
8261                         type = new_type(TYPE_ARRAY | qualifiers, type, 0);
8262                         if (value) {
8263                                 type->elements = value->u.cval;
8264                                 free_triple(state, value);
8265                         } else {
8266                                 type->elements = ELEMENT_COUNT_UNSPECIFIED;
8267                                 op = 0;
8268                         }
8269                 }
8270                         break;
8271                 default:
8272                         op = 0;
8273                         break;
8274                 }
8275         } while(op);
8276         if (outer) {
8277                 struct type *inner;
8278                 arrays_complete(state, type);
8279                 FINISHME();
8280                 for(inner = outer; inner->left; inner = inner->left)
8281                         ;
8282                 inner->left = type;
8283                 type = outer;
8284         }
8285         return type;
8286 }
8287
8288 static struct type *declarator(
8289         struct compile_state *state, struct type *type, 
8290         struct hash_entry **ident, int need_ident)
8291 {
8292         while(peek(state) == TOK_STAR) {
8293                 eat(state, TOK_STAR);
8294                 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
8295         }
8296         type = direct_declarator(state, type, ident, need_ident);
8297         return type;
8298 }
8299
8300
8301 static struct type *typedef_name(
8302         struct compile_state *state, unsigned int specifiers)
8303 {
8304         struct hash_entry *ident;
8305         struct type *type;
8306         eat(state, TOK_TYPE_NAME);
8307         ident = state->token[0].ident;
8308         type = ident->sym_ident->type;
8309         specifiers |= type->type & QUAL_MASK;
8310         if ((specifiers & (STOR_MASK | QUAL_MASK)) != 
8311                 (type->type & (STOR_MASK | QUAL_MASK))) {
8312                 type = clone_type(specifiers, type);
8313         }
8314         return type;
8315 }
8316
8317 static struct type *enum_specifier(
8318         struct compile_state *state, unsigned int specifiers)
8319 {
8320         int tok;
8321         struct type *type;
8322         type = 0;
8323         FINISHME();
8324         eat(state, TOK_ENUM);
8325         tok = peek(state);
8326         if (tok == TOK_IDENT) {
8327                 eat(state, TOK_IDENT);
8328         }
8329         if ((tok != TOK_IDENT) || (peek(state) == TOK_LBRACE)) {
8330                 eat(state, TOK_LBRACE);
8331                 do {
8332                         eat(state, TOK_IDENT);
8333                         if (peek(state) == TOK_EQ) {
8334                                 eat(state, TOK_EQ);
8335                                 constant_expr(state);
8336                         }
8337                         if (peek(state) == TOK_COMMA) {
8338                                 eat(state, TOK_COMMA);
8339                         }
8340                 } while(peek(state) != TOK_RBRACE);
8341                 eat(state, TOK_RBRACE);
8342         }
8343         FINISHME();
8344         return type;
8345 }
8346
8347 #if 0
8348 static struct type *struct_declarator(
8349         struct compile_state *state, struct type *type, struct hash_entry **ident)
8350 {
8351         int tok;
8352 #warning "struct_declarator is complicated because of bitfields, kill them?"
8353         tok = peek(state);
8354         if (tok != TOK_COLON) {
8355                 type = declarator(state, type, ident, 1);
8356         }
8357         if ((tok == TOK_COLON) || (peek(state) == TOK_COLON)) {
8358                 eat(state, TOK_COLON);
8359                 constant_expr(state);
8360         }
8361         FINISHME();
8362         return type;
8363 }
8364 #endif
8365
8366 static struct type *struct_or_union_specifier(
8367         struct compile_state *state, unsigned int specifiers)
8368 {
8369         struct type *struct_type;
8370         struct hash_entry *ident;
8371         unsigned int type_join;
8372         int tok;
8373         struct_type = 0;
8374         ident = 0;
8375         switch(peek(state)) {
8376         case TOK_STRUCT:
8377                 eat(state, TOK_STRUCT);
8378                 type_join = TYPE_PRODUCT;
8379                 break;
8380         case TOK_UNION:
8381                 eat(state, TOK_UNION);
8382                 type_join = TYPE_OVERLAP;
8383                 error(state, 0, "unions not yet supported\n");
8384                 break;
8385         default:
8386                 eat(state, TOK_STRUCT);
8387                 type_join = TYPE_PRODUCT;
8388                 break;
8389         }
8390         tok = peek(state);
8391         if ((tok == TOK_IDENT) || (tok == TOK_TYPE_NAME)) {
8392                 eat(state, tok);
8393                 ident = state->token[0].ident;
8394         }
8395         if (!ident || (peek(state) == TOK_LBRACE)) {
8396                 ulong_t elements;
8397                 elements = 0;
8398                 eat(state, TOK_LBRACE);
8399                 do {
8400                         struct type *base_type;
8401                         struct type **next;
8402                         int done;
8403                         base_type = specifier_qualifier_list(state);
8404                         next = &struct_type;
8405                         do {
8406                                 struct type *type;
8407                                 struct hash_entry *fident;
8408                                 done = 1;
8409                                 type = declarator(state, base_type, &fident, 1);
8410                                 elements++;
8411                                 if (peek(state) == TOK_COMMA) {
8412                                         done = 0;
8413                                         eat(state, TOK_COMMA);
8414                                 }
8415                                 type = clone_type(0, type);
8416                                 type->field_ident = fident;
8417                                 if (*next) {
8418                                         *next = new_type(type_join, *next, type);
8419                                         next = &((*next)->right);
8420                                 } else {
8421                                         *next = type;
8422                                 }
8423                         } while(!done);
8424                         eat(state, TOK_SEMI);
8425                 } while(peek(state) != TOK_RBRACE);
8426                 eat(state, TOK_RBRACE);
8427                 struct_type = new_type(TYPE_STRUCT, struct_type, 0);
8428                 struct_type->type_ident = ident;
8429                 struct_type->elements = elements;
8430                 symbol(state, ident, &ident->sym_struct, 0, struct_type);
8431         }
8432         if (ident && ident->sym_struct) {
8433                 struct_type = ident->sym_struct->type;
8434         }
8435         else if (ident && !ident->sym_struct) {
8436                 error(state, 0, "struct %s undeclared", ident->name);
8437         }
8438         return struct_type;
8439 }
8440
8441 static unsigned int storage_class_specifier_opt(struct compile_state *state)
8442 {
8443         unsigned int specifiers;
8444         switch(peek(state)) {
8445         case TOK_AUTO:
8446                 eat(state, TOK_AUTO);
8447                 specifiers = STOR_AUTO;
8448                 break;
8449         case TOK_REGISTER:
8450                 eat(state, TOK_REGISTER);
8451                 specifiers = STOR_REGISTER;
8452                 break;
8453         case TOK_STATIC:
8454                 eat(state, TOK_STATIC);
8455                 specifiers = STOR_STATIC;
8456                 break;
8457         case TOK_EXTERN:
8458                 eat(state, TOK_EXTERN);
8459                 specifiers = STOR_EXTERN;
8460                 break;
8461         case TOK_TYPEDEF:
8462                 eat(state, TOK_TYPEDEF);
8463                 specifiers = STOR_TYPEDEF;
8464                 break;
8465         default:
8466                 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
8467                         specifiers = STOR_STATIC;
8468                 }
8469                 else {
8470                         specifiers = STOR_AUTO;
8471                 }
8472         }
8473         return specifiers;
8474 }
8475
8476 static unsigned int function_specifier_opt(struct compile_state *state)
8477 {
8478         /* Ignore the inline keyword */
8479         unsigned int specifiers;
8480         specifiers = 0;
8481         switch(peek(state)) {
8482         case TOK_INLINE:
8483                 eat(state, TOK_INLINE);
8484                 specifiers = STOR_INLINE;
8485         }
8486         return specifiers;
8487 }
8488
8489 static unsigned int type_qualifiers(struct compile_state *state)
8490 {
8491         unsigned int specifiers;
8492         int done;
8493         done = 0;
8494         specifiers = QUAL_NONE;
8495         do {
8496                 switch(peek(state)) {
8497                 case TOK_CONST:
8498                         eat(state, TOK_CONST);
8499                         specifiers = QUAL_CONST;
8500                         break;
8501                 case TOK_VOLATILE:
8502                         eat(state, TOK_VOLATILE);
8503                         specifiers = QUAL_VOLATILE;
8504                         break;
8505                 case TOK_RESTRICT:
8506                         eat(state, TOK_RESTRICT);
8507                         specifiers = QUAL_RESTRICT;
8508                         break;
8509                 default:
8510                         done = 1;
8511                         break;
8512                 }
8513         } while(!done);
8514         return specifiers;
8515 }
8516
8517 static struct type *type_specifier(
8518         struct compile_state *state, unsigned int spec)
8519 {
8520         struct type *type;
8521         type = 0;
8522         switch(peek(state)) {
8523         case TOK_VOID:
8524                 eat(state, TOK_VOID);
8525                 type = new_type(TYPE_VOID | spec, 0, 0);
8526                 break;
8527         case TOK_CHAR:
8528                 eat(state, TOK_CHAR);
8529                 type = new_type(TYPE_CHAR | spec, 0, 0);
8530                 break;
8531         case TOK_SHORT:
8532                 eat(state, TOK_SHORT);
8533                 if (peek(state) == TOK_INT) {
8534                         eat(state, TOK_INT);
8535                 }
8536                 type = new_type(TYPE_SHORT | spec, 0, 0);
8537                 break;
8538         case TOK_INT:
8539                 eat(state, TOK_INT);
8540                 type = new_type(TYPE_INT | spec, 0, 0);
8541                 break;
8542         case TOK_LONG:
8543                 eat(state, TOK_LONG);
8544                 switch(peek(state)) {
8545                 case TOK_LONG:
8546                         eat(state, TOK_LONG);
8547                         error(state, 0, "long long not supported");
8548                         break;
8549                 case TOK_DOUBLE:
8550                         eat(state, TOK_DOUBLE);
8551                         error(state, 0, "long double not supported");
8552                         break;
8553                 case TOK_INT:
8554                         eat(state, TOK_INT);
8555                         type = new_type(TYPE_LONG | spec, 0, 0);
8556                         break;
8557                 default:
8558                         type = new_type(TYPE_LONG | spec, 0, 0);
8559                         break;
8560                 }
8561                 break;
8562         case TOK_FLOAT:
8563                 eat(state, TOK_FLOAT);
8564                 error(state, 0, "type float not supported");
8565                 break;
8566         case TOK_DOUBLE:
8567                 eat(state, TOK_DOUBLE);
8568                 error(state, 0, "type double not supported");
8569                 break;
8570         case TOK_SIGNED:
8571                 eat(state, TOK_SIGNED);
8572                 switch(peek(state)) {
8573                 case TOK_LONG:
8574                         eat(state, TOK_LONG);
8575                         switch(peek(state)) {
8576                         case TOK_LONG:
8577                                 eat(state, TOK_LONG);
8578                                 error(state, 0, "type long long not supported");
8579                                 break;
8580                         case TOK_INT:
8581                                 eat(state, TOK_INT);
8582                                 type = new_type(TYPE_LONG | spec, 0, 0);
8583                                 break;
8584                         default:
8585                                 type = new_type(TYPE_LONG | spec, 0, 0);
8586                                 break;
8587                         }
8588                         break;
8589                 case TOK_INT:
8590                         eat(state, TOK_INT);
8591                         type = new_type(TYPE_INT | spec, 0, 0);
8592                         break;
8593                 case TOK_SHORT:
8594                         eat(state, TOK_SHORT);
8595                         type = new_type(TYPE_SHORT | spec, 0, 0);
8596                         break;
8597                 case TOK_CHAR:
8598                         eat(state, TOK_CHAR);
8599                         type = new_type(TYPE_CHAR | spec, 0, 0);
8600                         break;
8601                 default:
8602                         type = new_type(TYPE_INT | spec, 0, 0);
8603                         break;
8604                 }
8605                 break;
8606         case TOK_UNSIGNED:
8607                 eat(state, TOK_UNSIGNED);
8608                 switch(peek(state)) {
8609                 case TOK_LONG:
8610                         eat(state, TOK_LONG);
8611                         switch(peek(state)) {
8612                         case TOK_LONG:
8613                                 eat(state, TOK_LONG);
8614                                 error(state, 0, "unsigned long long not supported");
8615                                 break;
8616                         case TOK_INT:
8617                                 eat(state, TOK_INT);
8618                                 type = new_type(TYPE_ULONG | spec, 0, 0);
8619                                 break;
8620                         default:
8621                                 type = new_type(TYPE_ULONG | spec, 0, 0);
8622                                 break;
8623                         }
8624                         break;
8625                 case TOK_INT:
8626                         eat(state, TOK_INT);
8627                         type = new_type(TYPE_UINT | spec, 0, 0);
8628                         break;
8629                 case TOK_SHORT:
8630                         eat(state, TOK_SHORT);
8631                         type = new_type(TYPE_USHORT | spec, 0, 0);
8632                         break;
8633                 case TOK_CHAR:
8634                         eat(state, TOK_CHAR);
8635                         type = new_type(TYPE_UCHAR | spec, 0, 0);
8636                         break;
8637                 default:
8638                         type = new_type(TYPE_UINT | spec, 0, 0);
8639                         break;
8640                 }
8641                 break;
8642                 /* struct or union specifier */
8643         case TOK_STRUCT:
8644         case TOK_UNION:
8645                 type = struct_or_union_specifier(state, spec);
8646                 break;
8647                 /* enum-spefifier */
8648         case TOK_ENUM:
8649                 type = enum_specifier(state, spec);
8650                 break;
8651                 /* typedef name */
8652         case TOK_TYPE_NAME:
8653                 type = typedef_name(state, spec);
8654                 break;
8655         default:
8656                 error(state, 0, "bad type specifier %s", 
8657                         tokens[peek(state)]);
8658                 break;
8659         }
8660         return type;
8661 }
8662
8663 static int istype(int tok)
8664 {
8665         switch(tok) {
8666         case TOK_CONST:
8667         case TOK_RESTRICT:
8668         case TOK_VOLATILE:
8669         case TOK_VOID:
8670         case TOK_CHAR:
8671         case TOK_SHORT:
8672         case TOK_INT:
8673         case TOK_LONG:
8674         case TOK_FLOAT:
8675         case TOK_DOUBLE:
8676         case TOK_SIGNED:
8677         case TOK_UNSIGNED:
8678         case TOK_STRUCT:
8679         case TOK_UNION:
8680         case TOK_ENUM:
8681         case TOK_TYPE_NAME:
8682                 return 1;
8683         default:
8684                 return 0;
8685         }
8686 }
8687
8688
8689 static struct type *specifier_qualifier_list(struct compile_state *state)
8690 {
8691         struct type *type;
8692         unsigned int specifiers = 0;
8693
8694         /* type qualifiers */
8695         specifiers |= type_qualifiers(state);
8696
8697         /* type specifier */
8698         type = type_specifier(state, specifiers);
8699
8700         return type;
8701 }
8702
8703 static int isdecl_specifier(int tok)
8704 {
8705         switch(tok) {
8706                 /* storage class specifier */
8707         case TOK_AUTO:
8708         case TOK_REGISTER:
8709         case TOK_STATIC:
8710         case TOK_EXTERN:
8711         case TOK_TYPEDEF:
8712                 /* type qualifier */
8713         case TOK_CONST:
8714         case TOK_RESTRICT:
8715         case TOK_VOLATILE:
8716                 /* type specifiers */
8717         case TOK_VOID:
8718         case TOK_CHAR:
8719         case TOK_SHORT:
8720         case TOK_INT:
8721         case TOK_LONG:
8722         case TOK_FLOAT:
8723         case TOK_DOUBLE:
8724         case TOK_SIGNED:
8725         case TOK_UNSIGNED:
8726                 /* struct or union specifier */
8727         case TOK_STRUCT:
8728         case TOK_UNION:
8729                 /* enum-spefifier */
8730         case TOK_ENUM:
8731                 /* typedef name */
8732         case TOK_TYPE_NAME:
8733                 /* function specifiers */
8734         case TOK_INLINE:
8735                 return 1;
8736         default:
8737                 return 0;
8738         }
8739 }
8740
8741 static struct type *decl_specifiers(struct compile_state *state)
8742 {
8743         struct type *type;
8744         unsigned int specifiers;
8745         /* I am overly restrictive in the arragement of specifiers supported.
8746          * C is overly flexible in this department it makes interpreting
8747          * the parse tree difficult.
8748          */
8749         specifiers = 0;
8750
8751         /* storage class specifier */
8752         specifiers |= storage_class_specifier_opt(state);
8753
8754         /* function-specifier */
8755         specifiers |= function_specifier_opt(state);
8756
8757         /* type qualifier */
8758         specifiers |= type_qualifiers(state);
8759
8760         /* type specifier */
8761         type = type_specifier(state, specifiers);
8762         return type;
8763 }
8764
8765 static unsigned designator(struct compile_state *state)
8766 {
8767         int tok;
8768         unsigned index;
8769         index = -1U;
8770         do {
8771                 switch(peek(state)) {
8772                 case TOK_LBRACKET:
8773                 {
8774                         struct triple *value;
8775                         eat(state, TOK_LBRACKET);
8776                         value = constant_expr(state);
8777                         eat(state, TOK_RBRACKET);
8778                         index = value->u.cval;
8779                         break;
8780                 }
8781                 case TOK_DOT:
8782                         eat(state, TOK_DOT);
8783                         eat(state, TOK_IDENT);
8784                         error(state, 0, "Struct Designators not currently supported");
8785                         break;
8786                 default:
8787                         error(state, 0, "Invalid designator");
8788                 }
8789                 tok = peek(state);
8790         } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
8791         eat(state, TOK_EQ);
8792         return index;
8793 }
8794
8795 static struct triple *initializer(
8796         struct compile_state *state, struct type *type)
8797 {
8798         struct triple *result;
8799         if (peek(state) != TOK_LBRACE) {
8800                 result = assignment_expr(state);
8801         }
8802         else {
8803                 int comma;
8804                 unsigned index, max_index;
8805                 void *buf;
8806                 max_index = index = 0;
8807                 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
8808                         max_index = type->elements;
8809                         if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
8810                                 type->elements = 0;
8811                         }
8812                 } else {
8813                         error(state, 0, "Struct initializers not currently supported");
8814                 }
8815                 buf = xcmalloc(size_of(state, type), "initializer");
8816                 eat(state, TOK_LBRACE);
8817                 do {
8818                         struct triple *value;
8819                         struct type *value_type;
8820                         size_t value_size;
8821                         int tok;
8822                         comma = 0;
8823                         tok = peek(state);
8824                         if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
8825                                 index = designator(state);
8826                         }
8827                         if ((max_index != ELEMENT_COUNT_UNSPECIFIED) &&
8828                                 (index > max_index)) {
8829                                 error(state, 0, "element beyond bounds");
8830                         }
8831                         value_type = 0;
8832                         if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
8833                                 value_type = type->left;
8834                         }
8835                         value = eval_const_expr(state, initializer(state, value_type));
8836                         value_size = size_of(state, value_type);
8837                         if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
8838                                 (max_index == ELEMENT_COUNT_UNSPECIFIED) &&
8839                                 (type->elements <= index)) {
8840                                 void *old_buf;
8841                                 size_t old_size;
8842                                 old_buf = buf;
8843                                 old_size = size_of(state, type);
8844                                 type->elements = index + 1;
8845                                 buf = xmalloc(size_of(state, type), "initializer");
8846                                 memcpy(buf, old_buf, old_size);
8847                                 xfree(old_buf);
8848                         }
8849                         if (value->op == OP_BLOBCONST) {
8850                                 memcpy((char *)buf + index * value_size, value->u.blob, value_size);
8851                         }
8852                         else if ((value->op == OP_INTCONST) && (value_size == 1)) {
8853                                 *(((uint8_t *)buf) + index) = value->u.cval & 0xff;
8854                         }
8855                         else if ((value->op == OP_INTCONST) && (value_size == 2)) {
8856                                 *(((uint16_t *)buf) + index) = value->u.cval & 0xffff;
8857                         }
8858                         else if ((value->op == OP_INTCONST) && (value_size == 4)) {
8859                                 *(((uint32_t *)buf) + index) = value->u.cval & 0xffffffff;
8860                         }
8861                         else {
8862                                 fprintf(stderr, "%d %d\n",
8863                                         value->op, value_size);
8864                                 internal_error(state, 0, "unhandled constant initializer");
8865                         }
8866                         if (peek(state) == TOK_COMMA) {
8867                                 eat(state, TOK_COMMA);
8868                                 comma = 1;
8869                         }
8870                         index += 1;
8871                 } while(comma && (peek(state) != TOK_RBRACE));
8872                 eat(state, TOK_RBRACE);
8873                 result = triple(state, OP_BLOBCONST, type, 0, 0);
8874                 result->u.blob = buf;
8875         }
8876         return result;
8877 }
8878
8879 static void resolve_branches(struct compile_state *state)
8880 {
8881         /* Make a second pass and finish anything outstanding
8882          * with respect to branches.  The only outstanding item
8883          * is to see if there are goto to labels that have not
8884          * been defined and to error about them.
8885          */
8886         int i;
8887         for(i = 0; i < HASH_TABLE_SIZE; i++) {
8888                 struct hash_entry *entry;
8889                 for(entry = state->hash_table[i]; entry; entry = entry->next) {
8890                         struct triple *ins;
8891                         if (!entry->sym_label) {
8892                                 continue;
8893                         }
8894                         ins = entry->sym_label->def;
8895                         if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
8896                                 error(state, ins, "label `%s' used but not defined",
8897                                         entry->name);
8898                         }
8899                 }
8900         }
8901 }
8902
8903 static struct triple *function_definition(
8904         struct compile_state *state, struct type *type)
8905 {
8906         struct triple *def, *tmp, *first, *end;
8907         struct hash_entry *ident;
8908         struct type *param;
8909         int i;
8910         if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
8911                 error(state, 0, "Invalid function header");
8912         }
8913
8914         /* Verify the function type */
8915         if (((type->right->type & TYPE_MASK) != TYPE_VOID)  &&
8916                 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
8917                 (type->right->field_ident == 0)) {
8918                 error(state, 0, "Invalid function parameters");
8919         }
8920         param = type->right;
8921         i = 0;
8922         while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
8923                 i++;
8924                 if (!param->left->field_ident) {
8925                         error(state, 0, "No identifier for parameter %d\n", i);
8926                 }
8927                 param = param->right;
8928         }
8929         i++;
8930         if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
8931                 error(state, 0, "No identifier for paramter %d\n", i);
8932         }
8933         
8934         /* Get a list of statements for this function. */
8935         def = triple(state, OP_LIST, type, 0, 0);
8936
8937         /* Start a new scope for the passed parameters */
8938         start_scope(state);
8939
8940         /* Put a label at the very start of a function */
8941         first = label(state);
8942         RHS(def, 0) = first;
8943
8944         /* Put a label at the very end of a function */
8945         end = label(state);
8946         flatten(state, first, end);
8947
8948         /* Walk through the parameters and create symbol table entries
8949          * for them.
8950          */
8951         param = type->right;
8952         while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
8953                 ident = param->left->field_ident;
8954                 tmp = variable(state, param->left);
8955                 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
8956                 flatten(state, end, tmp);
8957                 param = param->right;
8958         }
8959         if ((param->type & TYPE_MASK) != TYPE_VOID) {
8960                 /* And don't forget the last parameter */
8961                 ident = param->field_ident;
8962                 tmp = variable(state, param);
8963                 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
8964                 flatten(state, end, tmp);
8965         }
8966         /* Add a variable for the return value */
8967         MISC(def, 0) = 0;
8968         if ((type->left->type & TYPE_MASK) != TYPE_VOID) {
8969                 /* Remove all type qualifiers from the return type */
8970                 tmp = variable(state, clone_type(0, type->left));
8971                 flatten(state, end, tmp);
8972                 /* Remember where the return value is */
8973                 MISC(def, 0) = tmp;
8974         }
8975
8976         /* Remember which function I am compiling.
8977          * Also assume the last defined function is the main function.
8978          */
8979         state->main_function = def;
8980
8981         /* Now get the actual function definition */
8982         compound_statement(state, end);
8983
8984         /* Finish anything unfinished with branches */
8985         resolve_branches(state);
8986
8987         /* Remove the parameter scope */
8988         end_scope(state);
8989
8990 #if 0
8991         fprintf(stdout, "\n");
8992         loc(stdout, state, 0);
8993         fprintf(stdout, "\n__________ function_definition _________\n");
8994         print_triple(state, def);
8995         fprintf(stdout, "__________ function_definition _________ done\n\n");
8996 #endif
8997
8998         return def;
8999 }
9000
9001 static struct triple *do_decl(struct compile_state *state, 
9002         struct type *type, struct hash_entry *ident)
9003 {
9004         struct triple *def;
9005         def = 0;
9006         /* Clean up the storage types used */
9007         switch (type->type & STOR_MASK) {
9008         case STOR_AUTO:
9009         case STOR_STATIC:
9010                 /* These are the good types I am aiming for */
9011                 break;
9012         case STOR_REGISTER:
9013                 type->type &= ~STOR_MASK;
9014                 type->type |= STOR_AUTO;
9015                 break;
9016         case STOR_EXTERN:
9017                 type->type &= ~STOR_MASK;
9018                 type->type |= STOR_STATIC;
9019                 break;
9020         case STOR_TYPEDEF:
9021                 if (!ident) {
9022                         error(state, 0, "typedef without name");
9023                 }
9024                 symbol(state, ident, &ident->sym_ident, 0, type);
9025                 ident->tok = TOK_TYPE_NAME;
9026                 return 0;
9027                 break;
9028         default:
9029                 internal_error(state, 0, "Undefined storage class");
9030         }
9031         if (((type->type & STOR_MASK) == STOR_STATIC) &&
9032                 ((type->type & QUAL_CONST) == 0)) {
9033                 error(state, 0, "non const static variables not supported");
9034         }
9035         if (ident) {
9036                 def = variable(state, type);
9037                 symbol(state, ident, &ident->sym_ident, def, type);
9038         }
9039         return def;
9040 }
9041
9042 static void decl(struct compile_state *state, struct triple *first)
9043 {
9044         struct type *base_type, *type;
9045         struct hash_entry *ident;
9046         struct triple *def;
9047         int global;
9048         global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
9049         base_type = decl_specifiers(state);
9050         ident = 0;
9051         type = declarator(state, base_type, &ident, 0);
9052         if (global && ident && (peek(state) == TOK_LBRACE)) {
9053                 /* function */
9054                 state->function = ident->name;
9055                 def = function_definition(state, type);
9056                 symbol(state, ident, &ident->sym_ident, def, type);
9057                 state->function = 0;
9058         }
9059         else {
9060                 int done;
9061                 flatten(state, first, do_decl(state, type, ident));
9062                 /* type or variable definition */
9063                 do {
9064                         done = 1;
9065                         if (peek(state) == TOK_EQ) {
9066                                 if (!ident) {
9067                                         error(state, 0, "cannot assign to a type");
9068                                 }
9069                                 eat(state, TOK_EQ);
9070                                 flatten(state, first,
9071                                         init_expr(state, 
9072                                                 ident->sym_ident->def, 
9073                                                 initializer(state, type)));
9074                         }
9075                         arrays_complete(state, type);
9076                         if (peek(state) == TOK_COMMA) {
9077                                 eat(state, TOK_COMMA);
9078                                 ident = 0;
9079                                 type = declarator(state, base_type, &ident, 0);
9080                                 flatten(state, first, do_decl(state, type, ident));
9081                                 done = 0;
9082                         }
9083                 } while(!done);
9084                 eat(state, TOK_SEMI);
9085         }
9086 }
9087
9088 static void decls(struct compile_state *state)
9089 {
9090         struct triple *list;
9091         int tok;
9092         list = label(state);
9093         while(1) {
9094                 tok = peek(state);
9095                 if (tok == TOK_EOF) {
9096                         return;
9097                 }
9098                 if (tok == TOK_SPACE) {
9099                         eat(state, TOK_SPACE);
9100                 }
9101                 decl(state, list);
9102                 if (list->next != list) {
9103                         error(state, 0, "global variables not supported");
9104                 }
9105         }
9106 }
9107
9108 /*
9109  * Data structurs for optimation.
9110  */
9111
9112 static void do_use_block(
9113         struct block *used, struct block_set **head, struct block *user, 
9114         int front)
9115 {
9116         struct block_set **ptr, *new;
9117         if (!used)
9118                 return;
9119         if (!user)
9120                 return;
9121         ptr = head;
9122         while(*ptr) {
9123                 if ((*ptr)->member == user) {
9124                         return;
9125                 }
9126                 ptr = &(*ptr)->next;
9127         }
9128         new = xcmalloc(sizeof(*new), "block_set");
9129         new->member = user;
9130         if (front) {
9131                 new->next = *head;
9132                 *head = new;
9133         }
9134         else {
9135                 new->next = 0;
9136                 *ptr = new;
9137         }
9138 }
9139 static void do_unuse_block(
9140         struct block *used, struct block_set **head, struct block *unuser)
9141 {
9142         struct block_set *use, **ptr;
9143         ptr = head;
9144         while(*ptr) {
9145                 use = *ptr;
9146                 if (use->member == unuser) {
9147                         *ptr = use->next;
9148                         memset(use, -1, sizeof(*use));
9149                         xfree(use);
9150                 }
9151                 else {
9152                         ptr = &use->next;
9153                 }
9154         }
9155 }
9156
9157 static void use_block(struct block *used, struct block *user)
9158 {
9159         /* Append new to the head of the list, print_block
9160          * depends on this.
9161          */
9162         do_use_block(used, &used->use, user, 1); 
9163         used->users++;
9164 }
9165 static void unuse_block(struct block *used, struct block *unuser)
9166 {
9167         do_unuse_block(used, &used->use, unuser); 
9168         used->users--;
9169 }
9170
9171 static void idom_block(struct block *idom, struct block *user)
9172 {
9173         do_use_block(idom, &idom->idominates, user, 0);
9174 }
9175
9176 static void unidom_block(struct block *idom, struct block *unuser)
9177 {
9178         do_unuse_block(idom, &idom->idominates, unuser);
9179 }
9180
9181 static void domf_block(struct block *block, struct block *domf)
9182 {
9183         do_use_block(block, &block->domfrontier, domf, 0);
9184 }
9185
9186 static void undomf_block(struct block *block, struct block *undomf)
9187 {
9188         do_unuse_block(block, &block->domfrontier, undomf);
9189 }
9190
9191 static void ipdom_block(struct block *ipdom, struct block *user)
9192 {
9193         do_use_block(ipdom, &ipdom->ipdominates, user, 0);
9194 }
9195
9196 static void unipdom_block(struct block *ipdom, struct block *unuser)
9197 {
9198         do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
9199 }
9200
9201 static void ipdomf_block(struct block *block, struct block *ipdomf)
9202 {
9203         do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
9204 }
9205
9206 static void unipdomf_block(struct block *block, struct block *unipdomf)
9207 {
9208         do_unuse_block(block, &block->ipdomfrontier, unipdomf);
9209 }
9210
9211
9212
9213 static int do_walk_triple(struct compile_state *state,
9214         struct triple *ptr, int depth,
9215         int (*cb)(struct compile_state *state, struct triple *ptr, int depth)) 
9216 {
9217         int result;
9218         result = cb(state, ptr, depth);
9219         if ((result == 0) && (ptr->op == OP_LIST)) {
9220                 struct triple *list;
9221                 list = ptr;
9222                 ptr = RHS(list, 0);
9223                 do {
9224                         result = do_walk_triple(state, ptr, depth + 1, cb);
9225                         if (ptr->next->prev != ptr) {
9226                                 internal_error(state, ptr->next, "bad prev");
9227                         }
9228                         ptr = ptr->next;
9229                         
9230                 } while((result == 0) && (ptr != RHS(list, 0)));
9231         }
9232         return result;
9233 }
9234
9235 static int walk_triple(
9236         struct compile_state *state, 
9237         struct triple *ptr, 
9238         int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
9239 {
9240         return do_walk_triple(state, ptr, 0, cb);
9241 }
9242
9243 static void do_print_prefix(int depth)
9244 {
9245         int i;
9246         for(i = 0; i < depth; i++) {
9247                 printf("  ");
9248         }
9249 }
9250
9251 #define PRINT_LIST 1
9252 static int do_print_triple(struct compile_state *state, struct triple *ins, int depth)
9253 {
9254         int op;
9255         op = ins->op;
9256         if (op == OP_LIST) {
9257 #if !PRINT_LIST
9258                 return 0;
9259 #endif
9260         }
9261         if ((op == OP_LABEL) && (ins->use)) {
9262                 printf("\n%p:\n", ins);
9263         }
9264         do_print_prefix(depth);
9265         display_triple(stdout, ins);
9266
9267         if ((ins->op == OP_BRANCH) && ins->use) {
9268                 internal_error(state, ins, "branch used?");
9269         }
9270 #if 0
9271         {
9272                 struct triple_set *user;
9273                 for(user = ins->use; user; user = user->next) {
9274                         printf("use: %p\n", user->member);
9275                 }
9276         }
9277 #endif
9278         if (triple_is_branch(state, ins)) {
9279                 printf("\n");
9280         }
9281         return 0;
9282 }
9283
9284 static void print_triple(struct compile_state *state, struct triple *ins)
9285 {
9286         walk_triple(state, ins, do_print_triple);
9287 }
9288
9289 static void print_triples(struct compile_state *state)
9290 {
9291         print_triple(state, state->main_function);
9292 }
9293
9294 struct cf_block {
9295         struct block *block;
9296 };
9297 static void find_cf_blocks(struct cf_block *cf, struct block *block)
9298 {
9299         if (!block || (cf[block->vertex].block == block)) {
9300                 return;
9301         }
9302         cf[block->vertex].block = block;
9303         find_cf_blocks(cf, block->left);
9304         find_cf_blocks(cf, block->right);
9305 }
9306
9307 static void print_control_flow(struct compile_state *state)
9308 {
9309         struct cf_block *cf;
9310         int i;
9311         printf("\ncontrol flow\n");
9312         cf = xcmalloc(sizeof(*cf) * (state->last_vertex + 1), "cf_block");
9313         find_cf_blocks(cf, state->first_block);
9314
9315         for(i = 1; i <= state->last_vertex; i++) {
9316                 struct block *block;
9317                 block = cf[i].block;
9318                 if (!block)
9319                         continue;
9320                 printf("(%p) %d:", block, block->vertex);
9321                 if (block->left) {
9322                         printf(" %d", block->left->vertex);
9323                 }
9324                 if (block->right && (block->right != block->left)) {
9325                         printf(" %d", block->right->vertex);
9326                 }
9327                 printf("\n");
9328         }
9329
9330         xfree(cf);
9331 }
9332
9333
9334 static struct block *basic_block(struct compile_state *state,
9335         struct triple *first)
9336 {
9337         struct block *block;
9338         struct triple *ptr;
9339         int op;
9340         if (first->op != OP_LABEL) {
9341                 internal_error(state, 0, "block does not start with a label");
9342         }
9343         /* See if this basic block has already been setup */
9344         if (first->u.block != 0) {
9345                 return first->u.block;
9346         }
9347         /* Allocate another basic block structure */
9348         state->last_vertex += 1;
9349         block = xcmalloc(sizeof(*block), "block");
9350         block->first = block->last = first;
9351         block->vertex = state->last_vertex;
9352         ptr = first;
9353         do {
9354                 if ((ptr != first) && (ptr->op == OP_LABEL) && ptr->use) {
9355                         break;
9356                 }
9357                 block->last = ptr;
9358                 /* If ptr->u is not used remember where the baic block is */
9359                 if (triple_stores_block(state, ptr)) {
9360                         ptr->u.block = block;
9361                 }
9362                 if (ptr->op == OP_BRANCH) {
9363                         break;
9364                 }
9365                 ptr = ptr->next;
9366         } while (ptr != RHS(state->main_function, 0));
9367         if (ptr == RHS(state->main_function, 0))
9368                 return block;
9369         op = ptr->op;
9370         if (op == OP_LABEL) {
9371                 block->left = basic_block(state, ptr);
9372                 block->right = 0;
9373                 use_block(block->left, block);
9374         }
9375         else if (op == OP_BRANCH) {
9376                 block->left = 0;
9377                 /* Trace the branch target */
9378                 block->right = basic_block(state, TARG(ptr, 0));
9379                 use_block(block->right, block);
9380                 /* If there is a test trace the branch as well */
9381                 if (TRIPLE_RHS(ptr->sizes)) {
9382                         block->left = basic_block(state, ptr->next);
9383                         use_block(block->left, block);
9384                 }
9385         }
9386         else {
9387                 internal_error(state, 0, "Bad basic block split");
9388         }
9389         return block;
9390 }
9391
9392
9393 static void walk_blocks(struct compile_state *state,
9394         void (*cb)(struct compile_state *state, struct block *block, void *arg),
9395         void *arg)
9396 {
9397         struct triple *ptr, *first;
9398         struct block *last_block;
9399         last_block = 0;
9400         first = RHS(state->main_function, 0);
9401         ptr = first;
9402         do {
9403                 struct block *block;
9404                 if (ptr->op == OP_LABEL) {
9405                         block = ptr->u.block;
9406                         if (block && (block != last_block)) {
9407                                 cb(state, block, arg);
9408                         }
9409                         last_block = block;
9410                 }
9411                 ptr = ptr->next;
9412         } while(ptr != first);
9413 }
9414
9415 static void print_block(
9416         struct compile_state *state, struct block *block, void *arg)
9417 {
9418         struct triple *ptr;
9419         FILE *fp = arg;
9420
9421         fprintf(fp, "\nblock: %p (%d), %p<-%p %p<-%p\n", 
9422                 block, 
9423                 block->vertex,
9424                 block->left, 
9425                 block->left && block->left->use?block->left->use->member : 0,
9426                 block->right, 
9427                 block->right && block->right->use?block->right->use->member : 0);
9428         if (block->first->op == OP_LABEL) {
9429                 fprintf(fp, "%p:\n", block->first);
9430         }
9431         for(ptr = block->first; ; ptr = ptr->next) {
9432                 struct triple_set *user;
9433                 int op = ptr->op;
9434                 
9435                 if (triple_stores_block(state, ptr)) {
9436                         if (ptr->u.block != block) {
9437                                 internal_error(state, ptr, 
9438                                         "Wrong block pointer: %p\n",
9439                                         ptr->u.block);
9440                         }
9441                 }
9442                 if (op == OP_ADECL) {
9443                         for(user = ptr->use; user; user = user->next) {
9444                                 if (!user->member->u.block) {
9445                                         internal_error(state, user->member, 
9446                                                 "Use %p not in a block?\n",
9447                                                 user->member);
9448                                 }
9449                         }
9450                 }
9451                 display_triple(fp, ptr);
9452
9453 #if 0
9454                 for(user = ptr->use; user; user = user->next) {
9455                         fprintf(fp, "use: %p\n", user->member);
9456                 }
9457 #endif
9458
9459                 /* Sanity checks... */
9460                 valid_ins(state, ptr);
9461                 for(user = ptr->use; user; user = user->next) {
9462                         struct triple *use;
9463                         use = user->member;
9464                         valid_ins(state, use);
9465                         if (triple_stores_block(state, user->member) &&
9466                                 !user->member->u.block) {
9467                                 internal_error(state, user->member,
9468                                         "Use %p not in a block?",
9469                                         user->member);
9470                         }
9471                 }
9472
9473                 if (ptr == block->last)
9474                         break;
9475         }
9476         fprintf(fp,"\n");
9477 }
9478
9479
9480 static void print_blocks(struct compile_state *state, FILE *fp)
9481 {
9482         fprintf(fp, "--------------- blocks ---------------\n");
9483         walk_blocks(state, print_block, fp);
9484 }
9485
9486 static void prune_nonblock_triples(struct compile_state *state)
9487 {
9488         struct block *block;
9489         struct triple *first, *ins, *next;
9490         /* Delete the triples not in a basic block */
9491         first = RHS(state->main_function, 0);
9492         block = 0;
9493         ins = first;
9494         do {
9495                 next = ins->next;
9496                 if (ins->op == OP_LABEL) {
9497                         block = ins->u.block;
9498                 }
9499                 if (!block) {
9500                         release_triple(state, ins);
9501                 }
9502                 ins = next;
9503         } while(ins != first);
9504 }
9505
9506 static void setup_basic_blocks(struct compile_state *state)
9507 {
9508         if (!triple_stores_block(state, RHS(state->main_function, 0)) ||
9509                 !triple_stores_block(state, RHS(state->main_function,0)->prev)) {
9510                 internal_error(state, 0, "ins will not store block?");
9511         }
9512         /* Find the basic blocks */
9513         state->last_vertex = 0;
9514         state->first_block = basic_block(state, RHS(state->main_function,0));
9515         /* Delete the triples not in a basic block */
9516         prune_nonblock_triples(state);
9517         /* Find the last basic block */
9518         state->last_block = RHS(state->main_function, 0)->prev->u.block;
9519         if (!state->last_block) {
9520                 internal_error(state, 0, "end not used?");
9521         }
9522         /* Insert an extra unused edge from start to the end 
9523          * This helps with reverse control flow calculations.
9524          */
9525         use_block(state->first_block, state->last_block);
9526         /* If we are debugging print what I have just done */
9527         if (state->debug & DEBUG_BASIC_BLOCKS) {
9528                 print_blocks(state, stdout);
9529                 print_control_flow(state);
9530         }
9531 }
9532
9533 static void free_basic_block(struct compile_state *state, struct block *block)
9534 {
9535         struct block_set *entry, *next;
9536         struct block *child;
9537         if (!block) {
9538                 return;
9539         }
9540         if (block->vertex == -1) {
9541                 return;
9542         }
9543         block->vertex = -1;
9544         if (block->left) {
9545                 unuse_block(block->left, block);
9546         }
9547         if (block->right) {
9548                 unuse_block(block->right, block);
9549         }
9550         if (block->idom) {
9551                 unidom_block(block->idom, block);
9552         }
9553         block->idom = 0;
9554         if (block->ipdom) {
9555                 unipdom_block(block->ipdom, block);
9556         }
9557         block->ipdom = 0;
9558         for(entry = block->use; entry; entry = next) {
9559                 next = entry->next;
9560                 child = entry->member;
9561                 unuse_block(block, child);
9562                 if (child->left == block) {
9563                         child->left = 0;
9564                 }
9565                 if (child->right == block) {
9566                         child->right = 0;
9567                 }
9568         }
9569         for(entry = block->idominates; entry; entry = next) {
9570                 next = entry->next;
9571                 child = entry->member;
9572                 unidom_block(block, child);
9573                 child->idom = 0;
9574         }
9575         for(entry = block->domfrontier; entry; entry = next) {
9576                 next = entry->next;
9577                 child = entry->member;
9578                 undomf_block(block, child);
9579         }
9580         for(entry = block->ipdominates; entry; entry = next) {
9581                 next = entry->next;
9582                 child = entry->member;
9583                 unipdom_block(block, child);
9584                 child->ipdom = 0;
9585         }
9586         for(entry = block->ipdomfrontier; entry; entry = next) {
9587                 next = entry->next;
9588                 child = entry->member;
9589                 unipdomf_block(block, child);
9590         }
9591         if (block->users != 0) {
9592                 internal_error(state, 0, "block still has users");
9593         }
9594         free_basic_block(state, block->left);
9595         block->left = 0;
9596         free_basic_block(state, block->right);
9597         block->right = 0;
9598         memset(block, -1, sizeof(*block));
9599         xfree(block);
9600 }
9601
9602 static void free_basic_blocks(struct compile_state *state)
9603 {
9604         struct triple *first, *ins;
9605         free_basic_block(state, state->first_block);
9606         state->last_vertex = 0;
9607         state->first_block = state->last_block = 0;
9608         first = RHS(state->main_function, 0);
9609         ins = first;
9610         do {
9611                 if (triple_stores_block(state, ins)) {
9612                         ins->u.block = 0;
9613                 }
9614                 ins = ins->next;
9615         } while(ins != first);
9616         
9617 }
9618
9619 struct sdom_block {
9620         struct block *block;
9621         struct sdom_block *sdominates;
9622         struct sdom_block *sdom_next;
9623         struct sdom_block *sdom;
9624         struct sdom_block *label;
9625         struct sdom_block *parent;
9626         struct sdom_block *ancestor;
9627         int vertex;
9628 };
9629
9630
9631 static void unsdom_block(struct sdom_block *block)
9632 {
9633         struct sdom_block **ptr;
9634         if (!block->sdom_next) {
9635                 return;
9636         }
9637         ptr = &block->sdom->sdominates;
9638         while(*ptr) {
9639                 if ((*ptr) == block) {
9640                         *ptr = block->sdom_next;
9641                         return;
9642                 }
9643                 ptr = &(*ptr)->sdom_next;
9644         }
9645 }
9646
9647 static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
9648 {
9649         unsdom_block(block);
9650         block->sdom = sdom;
9651         block->sdom_next = sdom->sdominates;
9652         sdom->sdominates = block;
9653 }
9654
9655
9656
9657 static int initialize_sdblock(struct sdom_block *sd,
9658         struct block *parent, struct block *block, int vertex)
9659 {
9660         if (!block || (sd[block->vertex].block == block)) {
9661                 return vertex;
9662         }
9663         vertex += 1;
9664         /* Renumber the blocks in a convinient fashion */
9665         block->vertex = vertex;
9666         sd[vertex].block    = block;
9667         sd[vertex].sdom     = &sd[vertex];
9668         sd[vertex].label    = &sd[vertex];
9669         sd[vertex].parent   = parent? &sd[parent->vertex] : 0;
9670         sd[vertex].ancestor = 0;
9671         sd[vertex].vertex   = vertex;
9672         vertex = initialize_sdblock(sd, block, block->left, vertex);
9673         vertex = initialize_sdblock(sd, block, block->right, vertex);
9674         return vertex;
9675 }
9676
9677 static int initialize_sdpblock(struct sdom_block *sd,
9678         struct block *parent, struct block *block, int vertex)
9679 {
9680         struct block_set *user;
9681         if (!block || (sd[block->vertex].block == block)) {
9682                 return vertex;
9683         }
9684         vertex += 1;
9685         /* Renumber the blocks in a convinient fashion */
9686         block->vertex = vertex;
9687         sd[vertex].block    = block;
9688         sd[vertex].sdom     = &sd[vertex];
9689         sd[vertex].label    = &sd[vertex];
9690         sd[vertex].parent   = parent? &sd[parent->vertex] : 0;
9691         sd[vertex].ancestor = 0;
9692         sd[vertex].vertex   = vertex;
9693         for(user = block->use; user; user = user->next) {
9694                 vertex = initialize_sdpblock(sd, block, user->member, vertex);
9695         }
9696         return vertex;
9697 }
9698
9699 static void compress_ancestors(struct sdom_block *v)
9700 {
9701         /* This procedure assumes ancestor(v) != 0 */
9702         /* if (ancestor(ancestor(v)) != 0) {
9703          *      compress(ancestor(ancestor(v)));
9704          *      if (semi(label(ancestor(v))) < semi(label(v))) {
9705          *              label(v) = label(ancestor(v));
9706          *      }
9707          *      ancestor(v) = ancestor(ancestor(v));
9708          * }
9709          */
9710         if (!v->ancestor) {
9711                 return;
9712         }
9713         if (v->ancestor->ancestor) {
9714                 compress_ancestors(v->ancestor->ancestor);
9715                 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
9716                         v->label = v->ancestor->label;
9717                 }
9718                 v->ancestor = v->ancestor->ancestor;
9719         }
9720 }
9721
9722 static void compute_sdom(struct compile_state *state, struct sdom_block *sd)
9723 {
9724         int i;
9725         /* // step 2 
9726          *  for each v <= pred(w) {
9727          *      u = EVAL(v);
9728          *      if (semi[u] < semi[w] { 
9729          *              semi[w] = semi[u]; 
9730          *      } 
9731          * }
9732          * add w to bucket(vertex(semi[w]));
9733          * LINK(parent(w), w);
9734          *
9735          * // step 3
9736          * for each v <= bucket(parent(w)) {
9737          *      delete v from bucket(parent(w));
9738          *      u = EVAL(v);
9739          *      dom(v) = (semi[u] < semi[v]) ? u : parent(w);
9740          * }
9741          */
9742         for(i = state->last_vertex; i >= 2; i--) {
9743                 struct sdom_block *v, *parent, *next;
9744                 struct block_set *user;
9745                 struct block *block;
9746                 block = sd[i].block;
9747                 parent = sd[i].parent;
9748                 /* Step 2 */
9749                 for(user = block->use; user; user = user->next) {
9750                         struct sdom_block *v, *u;
9751                         v = &sd[user->member->vertex];
9752                         u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9753                         if (u->sdom->vertex < sd[i].sdom->vertex) {
9754                                 sd[i].sdom = u->sdom;
9755                         }
9756                 }
9757                 sdom_block(sd[i].sdom, &sd[i]);
9758                 sd[i].ancestor = parent;
9759                 /* Step 3 */
9760                 for(v = parent->sdominates; v; v = next) {
9761                         struct sdom_block *u;
9762                         next = v->sdom_next;
9763                         unsdom_block(v);
9764                         u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
9765                         v->block->idom = (u->sdom->vertex < v->sdom->vertex)? 
9766                                 u->block : parent->block;
9767                 }
9768         }
9769 }
9770
9771 static void compute_spdom(struct compile_state *state, struct sdom_block *sd)
9772 {
9773         int i;
9774         /* // step 2 
9775          *  for each v <= pred(w) {
9776          *      u = EVAL(v);
9777          *      if (semi[u] < semi[w] { 
9778          *              semi[w] = semi[u]; 
9779          *      } 
9780          * }
9781          * add w to bucket(vertex(semi[w]));
9782          * LINK(parent(w), w);
9783          *
9784          * // step 3
9785          * for each v <= bucket(parent(w)) {
9786          *      delete v from bucket(parent(w));
9787          *      u = EVAL(v);
9788          *      dom(v) = (semi[u] < semi[v]) ? u : parent(w);
9789          * }
9790          */
9791         for(i = state->last_vertex; i >= 2; i--) {
9792                 struct sdom_block *u, *v, *parent, *next;
9793                 struct block *block;
9794                 block = sd[i].block;
9795                 parent = sd[i].parent;
9796                 /* Step 2 */
9797                 if (block->left) {
9798                         v = &sd[block->left->vertex];
9799                         u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9800                         if (u->sdom->vertex < sd[i].sdom->vertex) {
9801                                 sd[i].sdom = u->sdom;
9802                         }
9803                 }
9804                 if (block->right && (block->right != block->left)) {
9805                         v = &sd[block->right->vertex];
9806                         u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9807                         if (u->sdom->vertex < sd[i].sdom->vertex) {
9808                                 sd[i].sdom = u->sdom;
9809                         }
9810                 }
9811                 sdom_block(sd[i].sdom, &sd[i]);
9812                 sd[i].ancestor = parent;
9813                 /* Step 3 */
9814                 for(v = parent->sdominates; v; v = next) {
9815                         struct sdom_block *u;
9816                         next = v->sdom_next;
9817                         unsdom_block(v);
9818                         u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
9819                         v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)? 
9820                                 u->block : parent->block;
9821                 }
9822         }
9823 }
9824
9825 static void compute_idom(struct compile_state *state, struct sdom_block *sd)
9826 {
9827         int i;
9828         for(i = 2; i <= state->last_vertex; i++) {
9829                 struct block *block;
9830                 block = sd[i].block;
9831                 if (block->idom->vertex != sd[i].sdom->vertex) {
9832                         block->idom = block->idom->idom;
9833                 }
9834                 idom_block(block->idom, block);
9835         }
9836         sd[1].block->idom = 0;
9837 }
9838
9839 static void compute_ipdom(struct compile_state *state, struct sdom_block *sd)
9840 {
9841         int i;
9842         for(i = 2; i <= state->last_vertex; i++) {
9843                 struct block *block;
9844                 block = sd[i].block;
9845                 if (block->ipdom->vertex != sd[i].sdom->vertex) {
9846                         block->ipdom = block->ipdom->ipdom;
9847                 }
9848                 ipdom_block(block->ipdom, block);
9849         }
9850         sd[1].block->ipdom = 0;
9851 }
9852
9853         /* Theorem 1:
9854          *   Every vertex of a flowgraph G = (V, E, r) except r has
9855          *   a unique immediate dominator.  
9856          *   The edges {(idom(w), w) |w <= V - {r}} form a directed tree
9857          *   rooted at r, called the dominator tree of G, such that 
9858          *   v dominates w if and only if v is a proper ancestor of w in
9859          *   the dominator tree.
9860          */
9861         /* Lemma 1:  
9862          *   If v and w are vertices of G such that v <= w,
9863          *   than any path from v to w must contain a common ancestor
9864          *   of v and w in T.
9865          */
9866         /* Lemma 2:  For any vertex w != r, idom(w) -> w */
9867         /* Lemma 3:  For any vertex w != r, sdom(w) -> w */
9868         /* Lemma 4:  For any vertex w != r, idom(w) -> sdom(w) */
9869         /* Theorem 2:
9870          *   Let w != r.  Suppose every u for which sdom(w) -> u -> w satisfies
9871          *   sdom(u) >= sdom(w).  Then idom(w) = sdom(w).
9872          */
9873         /* Theorem 3:
9874          *   Let w != r and let u be a vertex for which sdom(u) is 
9875          *   minimum amoung vertices u satisfying sdom(w) -> u -> w.
9876          *   Then sdom(u) <= sdom(w) and idom(u) = idom(w).
9877          */
9878         /* Lemma 5:  Let vertices v,w satisfy v -> w.
9879          *           Then v -> idom(w) or idom(w) -> idom(v)
9880          */
9881
9882 static void find_immediate_dominators(struct compile_state *state)
9883 {
9884         struct sdom_block *sd;
9885         /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
9886          *           vi > w for (1 <= i <= k - 1}
9887          */
9888         /* Theorem 4:
9889          *   For any vertex w != r.
9890          *   sdom(w) = min(
9891          *                 {v|(v,w) <= E  and v < w } U 
9892          *                 {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
9893          */
9894         /* Corollary 1:
9895          *   Let w != r and let u be a vertex for which sdom(u) is 
9896          *   minimum amoung vertices u satisfying sdom(w) -> u -> w.
9897          *   Then:
9898          *                   { sdom(w) if sdom(w) = sdom(u),
9899          *        idom(w) = {
9900          *                   { idom(u) otherwise
9901          */
9902         /* The algorithm consists of the following 4 steps.
9903          * Step 1.  Carry out a depth-first search of the problem graph.  
9904          *    Number the vertices from 1 to N as they are reached during
9905          *    the search.  Initialize the variables used in succeeding steps.
9906          * Step 2.  Compute the semidominators of all vertices by applying
9907          *    theorem 4.   Carry out the computation vertex by vertex in
9908          *    decreasing order by number.
9909          * Step 3.  Implicitly define the immediate dominator of each vertex
9910          *    by applying Corollary 1.
9911          * Step 4.  Explicitly define the immediate dominator of each vertex,
9912          *    carrying out the computation vertex by vertex in increasing order
9913          *    by number.
9914          */
9915         /* Step 1 initialize the basic block information */
9916         sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
9917         initialize_sdblock(sd, 0, state->first_block, 0);
9918 #if 0
9919         sd[1].size  = 0;
9920         sd[1].label = 0;
9921         sd[1].sdom  = 0;
9922 #endif
9923         /* Step 2 compute the semidominators */
9924         /* Step 3 implicitly define the immediate dominator of each vertex */
9925         compute_sdom(state, sd);
9926         /* Step 4 explicitly define the immediate dominator of each vertex */
9927         compute_idom(state, sd);
9928         xfree(sd);
9929 }
9930
9931 static void find_post_dominators(struct compile_state *state)
9932 {
9933         struct sdom_block *sd;
9934         /* Step 1 initialize the basic block information */
9935         sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
9936
9937         initialize_sdpblock(sd, 0, state->last_block, 0);
9938
9939         /* Step 2 compute the semidominators */
9940         /* Step 3 implicitly define the immediate dominator of each vertex */
9941         compute_spdom(state, sd);
9942         /* Step 4 explicitly define the immediate dominator of each vertex */
9943         compute_ipdom(state, sd);
9944         xfree(sd);
9945 }
9946
9947
9948
9949 static void find_block_domf(struct compile_state *state, struct block *block)
9950 {
9951         struct block *child;
9952         struct block_set *user;
9953         if (block->domfrontier != 0) {
9954                 internal_error(state, block->first, "domfrontier present?");
9955         }
9956         for(user = block->idominates; user; user = user->next) {
9957                 child = user->member;
9958                 if (child->idom != block) {
9959                         internal_error(state, block->first, "bad idom");
9960                 }
9961                 find_block_domf(state, child);
9962         }
9963         if (block->left && block->left->idom != block) {
9964                 domf_block(block, block->left);
9965         }
9966         if (block->right && block->right->idom != block) {
9967                 domf_block(block, block->right);
9968         }
9969         for(user = block->idominates; user; user = user->next) {
9970                 struct block_set *frontier;
9971                 child = user->member;
9972                 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
9973                         if (frontier->member->idom != block) {
9974                                 domf_block(block, frontier->member);
9975                         }
9976                 }
9977         }
9978 }
9979
9980 static void find_block_ipdomf(struct compile_state *state, struct block *block)
9981 {
9982         struct block *child;
9983         struct block_set *user;
9984         if (block->ipdomfrontier != 0) {
9985                 internal_error(state, block->first, "ipdomfrontier present?");
9986         }
9987         for(user = block->ipdominates; user; user = user->next) {
9988                 child = user->member;
9989                 if (child->ipdom != block) {
9990                         internal_error(state, block->first, "bad ipdom");
9991                 }
9992                 find_block_ipdomf(state, child);
9993         }
9994         if (block->left && block->left->ipdom != block) {
9995                 ipdomf_block(block, block->left);
9996         }
9997         if (block->right && block->right->ipdom != block) {
9998                 ipdomf_block(block, block->right);
9999         }
10000         for(user = block->idominates; user; user = user->next) {
10001                 struct block_set *frontier;
10002                 child = user->member;
10003                 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
10004                         if (frontier->member->ipdom != block) {
10005                                 ipdomf_block(block, frontier->member);
10006                         }
10007                 }
10008         }
10009 }
10010
10011 static void print_dominated(
10012         struct compile_state *state, struct block *block, void *arg)
10013 {
10014         struct block_set *user;
10015         FILE *fp = arg;
10016
10017         fprintf(fp, "%d:", block->vertex);
10018         for(user = block->idominates; user; user = user->next) {
10019                 fprintf(fp, " %d", user->member->vertex);
10020                 if (user->member->idom != block) {
10021                         internal_error(state, user->member->first, "bad idom");
10022                 }
10023         }
10024         fprintf(fp,"\n");
10025 }
10026
10027 static void print_dominators(struct compile_state *state, FILE *fp)
10028 {
10029         fprintf(fp, "\ndominates\n");
10030         walk_blocks(state, print_dominated, fp);
10031 }
10032
10033
10034 static int print_frontiers(
10035         struct compile_state *state, struct block *block, int vertex)
10036 {
10037         struct block_set *user;
10038
10039         if (!block || (block->vertex != vertex + 1)) {
10040                 return vertex;
10041         }
10042         vertex += 1;
10043
10044         printf("%d:", block->vertex);
10045         for(user = block->domfrontier; user; user = user->next) {
10046                 printf(" %d", user->member->vertex);
10047         }
10048         printf("\n");
10049
10050         vertex = print_frontiers(state, block->left, vertex);
10051         vertex = print_frontiers(state, block->right, vertex);
10052         return vertex;
10053 }
10054 static void print_dominance_frontiers(struct compile_state *state)
10055 {
10056         printf("\ndominance frontiers\n");
10057         print_frontiers(state, state->first_block, 0);
10058         
10059 }
10060
10061 static void analyze_idominators(struct compile_state *state)
10062 {
10063         /* Find the immediate dominators */
10064         find_immediate_dominators(state);
10065         /* Find the dominance frontiers */
10066         find_block_domf(state, state->first_block);
10067         /* If debuging print the print what I have just found */
10068         if (state->debug & DEBUG_FDOMINATORS) {
10069                 print_dominators(state, stdout);
10070                 print_dominance_frontiers(state);
10071                 print_control_flow(state);
10072         }
10073 }
10074
10075
10076
10077 static void print_ipdominated(
10078         struct compile_state *state, struct block *block, void *arg)
10079 {
10080         struct block_set *user;
10081         FILE *fp = arg;
10082
10083         fprintf(fp, "%d:", block->vertex);
10084         for(user = block->ipdominates; user; user = user->next) {
10085                 fprintf(fp, " %d", user->member->vertex);
10086                 if (user->member->ipdom != block) {
10087                         internal_error(state, user->member->first, "bad ipdom");
10088                 }
10089         }
10090         fprintf(fp, "\n");
10091 }
10092
10093 static void print_ipdominators(struct compile_state *state, FILE *fp)
10094 {
10095         fprintf(fp, "\nipdominates\n");
10096         walk_blocks(state, print_ipdominated, fp);
10097 }
10098
10099 static int print_pfrontiers(
10100         struct compile_state *state, struct block *block, int vertex)
10101 {
10102         struct block_set *user;
10103
10104         if (!block || (block->vertex != vertex + 1)) {
10105                 return vertex;
10106         }
10107         vertex += 1;
10108
10109         printf("%d:", block->vertex);
10110         for(user = block->ipdomfrontier; user; user = user->next) {
10111                 printf(" %d", user->member->vertex);
10112         }
10113         printf("\n");
10114         for(user = block->use; user; user = user->next) {
10115                 vertex = print_pfrontiers(state, user->member, vertex);
10116         }
10117         return vertex;
10118 }
10119 static void print_ipdominance_frontiers(struct compile_state *state)
10120 {
10121         printf("\nipdominance frontiers\n");
10122         print_pfrontiers(state, state->last_block, 0);
10123         
10124 }
10125
10126 static void analyze_ipdominators(struct compile_state *state)
10127 {
10128         /* Find the post dominators */
10129         find_post_dominators(state);
10130         /* Find the control dependencies (post dominance frontiers) */
10131         find_block_ipdomf(state, state->last_block);
10132         /* If debuging print the print what I have just found */
10133         if (state->debug & DEBUG_RDOMINATORS) {
10134                 print_ipdominators(state, stdout);
10135                 print_ipdominance_frontiers(state);
10136                 print_control_flow(state);
10137         }
10138 }
10139
10140 static int bdominates(struct compile_state *state,
10141         struct block *dom, struct block *sub)
10142 {
10143         while(sub && (sub != dom)) {
10144                 sub = sub->idom;
10145         }
10146         return sub == dom;
10147 }
10148
10149 static int tdominates(struct compile_state *state,
10150         struct triple *dom, struct triple *sub)
10151 {
10152         struct block *bdom, *bsub;
10153         int result;
10154         bdom = block_of_triple(state, dom);
10155         bsub = block_of_triple(state, sub);
10156         if (bdom != bsub) {
10157                 result = bdominates(state, bdom, bsub);
10158         } 
10159         else {
10160                 struct triple *ins;
10161                 ins = sub;
10162                 while((ins != bsub->first) && (ins != dom)) {
10163                         ins = ins->prev;
10164                 }
10165                 result = (ins == dom);
10166         }
10167         return result;
10168 }
10169
10170 static void insert_phi_operations(struct compile_state *state)
10171 {
10172         size_t size;
10173         struct triple *first;
10174         int *has_already, *work;
10175         struct block *work_list, **work_list_tail;
10176         int iter;
10177         struct triple *var;
10178
10179         size = sizeof(int) * (state->last_vertex + 1);
10180         has_already = xcmalloc(size, "has_already");
10181         work =        xcmalloc(size, "work");
10182         iter = 0;
10183
10184         first = RHS(state->main_function, 0);
10185         for(var = first->next; var != first ; var = var->next) {
10186                 struct block *block;
10187                 struct triple_set *user;
10188                 if ((var->op != OP_ADECL) || !var->use) {
10189                         continue;
10190                 }
10191                 iter += 1;
10192                 work_list = 0;
10193                 work_list_tail = &work_list;
10194                 for(user = var->use; user; user = user->next) {
10195                         if (user->member->op == OP_READ) {
10196                                 continue;
10197                         }
10198                         if (user->member->op != OP_WRITE) {
10199                                 internal_error(state, user->member, 
10200                                         "bad variable access");
10201                         }
10202                         block = user->member->u.block;
10203                         if (!block) {
10204                                 warning(state, user->member, "dead code");
10205                         }
10206                         if (work[block->vertex] >= iter) {
10207                                 continue;
10208                         }
10209                         work[block->vertex] = iter;
10210                         *work_list_tail = block;
10211                         block->work_next = 0;
10212                         work_list_tail = &block->work_next;
10213                 }
10214                 for(block = work_list; block; block = block->work_next) {
10215                         struct block_set *df;
10216                         for(df = block->domfrontier; df; df = df->next) {
10217                                 struct triple *phi;
10218                                 struct block *front;
10219                                 int in_edges;
10220                                 front = df->member;
10221
10222                                 if (has_already[front->vertex] >= iter) {
10223                                         continue;
10224                                 }
10225                                 /* Count how many edges flow into this block */
10226                                 in_edges = front->users;
10227                                 /* Insert a phi function for this variable */
10228                                 get_occurance(front->first->occurance);
10229                                 phi = alloc_triple(
10230                                         state, OP_PHI, var->type, -1, in_edges, 
10231                                         front->first->occurance);
10232                                 phi->u.block = front;
10233                                 MISC(phi, 0) = var;
10234                                 use_triple(var, phi);
10235                                 /* Insert the phi functions immediately after the label */
10236                                 insert_triple(state, front->first->next, phi);
10237                                 if (front->first == front->last) {
10238                                         front->last = front->first->next;
10239                                 }
10240                                 has_already[front->vertex] = iter;
10241
10242                                 /* If necessary plan to visit the basic block */
10243                                 if (work[front->vertex] >= iter) {
10244                                         continue;
10245                                 }
10246                                 work[front->vertex] = iter;
10247                                 *work_list_tail = front;
10248                                 front->work_next = 0;
10249                                 work_list_tail = &front->work_next;
10250                         }
10251                 }
10252         }
10253         xfree(has_already);
10254         xfree(work);
10255 }
10256
10257 /*
10258  * C(V)
10259  * S(V)
10260  */
10261 static void fixup_block_phi_variables(
10262         struct compile_state *state, struct block *parent, struct block *block)
10263 {
10264         struct block_set *set;
10265         struct triple *ptr;
10266         int edge;
10267         if (!parent || !block)
10268                 return;
10269         /* Find the edge I am coming in on */
10270         edge = 0;
10271         for(set = block->use; set; set = set->next, edge++) {
10272                 if (set->member == parent) {
10273                         break;
10274                 }
10275         }
10276         if (!set) {
10277                 internal_error(state, 0, "phi input is not on a control predecessor");
10278         }
10279         for(ptr = block->first; ; ptr = ptr->next) {
10280                 if (ptr->op == OP_PHI) {
10281                         struct triple *var, *val, **slot;
10282                         var = MISC(ptr, 0);
10283                         if (!var) {
10284                                 internal_error(state, ptr, "no var???");
10285                         }
10286                         /* Find the current value of the variable */
10287                         val = var->use->member;
10288                         if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10289                                 internal_error(state, val, "bad value in phi");
10290                         }
10291                         if (edge >= TRIPLE_RHS(ptr->sizes)) {
10292                                 internal_error(state, ptr, "edges > phi rhs");
10293                         }
10294                         slot = &RHS(ptr, edge);
10295                         if ((*slot != 0) && (*slot != val)) {
10296                                 internal_error(state, ptr, "phi already bound on this edge");
10297                         }
10298                         *slot = val;
10299                         use_triple(val, ptr);
10300                 }
10301                 if (ptr == block->last) {
10302                         break;
10303                 }
10304         }
10305 }
10306
10307
10308 static void rename_block_variables(
10309         struct compile_state *state, struct block *block)
10310 {
10311         struct block_set *user;
10312         struct triple *ptr, *next, *last;
10313         int done;
10314         if (!block)
10315                 return;
10316         last = block->first;
10317         done = 0;
10318         for(ptr = block->first; !done; ptr = next) {
10319                 next = ptr->next;
10320                 if (ptr == block->last) {
10321                         done = 1;
10322                 }
10323                 /* RHS(A) */
10324                 if (ptr->op == OP_READ) {
10325                         struct triple *var, *val;
10326                         var = RHS(ptr, 0);
10327                         unuse_triple(var, ptr);
10328                         if (!var->use) {
10329                                 error(state, ptr, "variable used without being set");
10330                         }
10331                         /* Find the current value of the variable */
10332                         val = var->use->member;
10333                         if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10334                                 internal_error(state, val, "bad value in read");
10335                         }
10336                         propogate_use(state, ptr, val);
10337                         release_triple(state, ptr);
10338                         continue;
10339                 }
10340                 /* LHS(A) */
10341                 if (ptr->op == OP_WRITE) {
10342                         struct triple *var, *val;
10343                         var = LHS(ptr, 0);
10344                         val = RHS(ptr, 0);
10345                         if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10346                                 internal_error(state, val, "bad value in write");
10347                         }
10348                         propogate_use(state, ptr, val);
10349                         unuse_triple(var, ptr);
10350                         /* Push OP_WRITE ptr->right onto a stack of variable uses */
10351                         push_triple(var, val);
10352                 }
10353                 if (ptr->op == OP_PHI) {
10354                         struct triple *var;
10355                         var = MISC(ptr, 0);
10356                         /* Push OP_PHI onto a stack of variable uses */
10357                         push_triple(var, ptr);
10358                 }
10359                 last = ptr;
10360         }
10361         block->last = last;
10362
10363         /* Fixup PHI functions in the cf successors */
10364         fixup_block_phi_variables(state, block, block->left);
10365         fixup_block_phi_variables(state, block, block->right);
10366         /* rename variables in the dominated nodes */
10367         for(user = block->idominates; user; user = user->next) {
10368                 rename_block_variables(state, user->member);
10369         }
10370         /* pop the renamed variable stack */
10371         last = block->first;
10372         done = 0;
10373         for(ptr = block->first; !done ; ptr = next) {
10374                 next = ptr->next;
10375                 if (ptr == block->last) {
10376                         done = 1;
10377                 }
10378                 if (ptr->op == OP_WRITE) {
10379                         struct triple *var;
10380                         var = LHS(ptr, 0);
10381                         /* Pop OP_WRITE ptr->right from the stack of variable uses */
10382                         pop_triple(var, RHS(ptr, 0));
10383                         release_triple(state, ptr);
10384                         continue;
10385                 }
10386                 if (ptr->op == OP_PHI) {
10387                         struct triple *var;
10388                         var = MISC(ptr, 0);
10389                         /* Pop OP_WRITE ptr->right from the stack of variable uses */
10390                         pop_triple(var, ptr);
10391                 }
10392                 last = ptr;
10393         }
10394         block->last = last;
10395 }
10396
10397 static void prune_block_variables(struct compile_state *state,
10398         struct block *block)
10399 {
10400         struct block_set *user;
10401         struct triple *next, *last, *ptr;
10402         int done;
10403         last = block->first;
10404         done = 0;
10405         for(ptr = block->first; !done; ptr = next) {
10406                 next = ptr->next;
10407                 if (ptr == block->last) {
10408                         done = 1;
10409                 }
10410                 if (ptr->op == OP_ADECL) {
10411                         struct triple_set *user, *next;
10412                         for(user = ptr->use; user; user = next) {
10413                                 struct triple *use;
10414                                 next = user->next;
10415                                 use = user->member;
10416                                 if (use->op != OP_PHI) {
10417                                         internal_error(state, use, "decl still used");
10418                                 }
10419                                 if (MISC(use, 0) != ptr) {
10420                                         internal_error(state, use, "bad phi use of decl");
10421                                 }
10422                                 unuse_triple(ptr, use);
10423                                 MISC(use, 0) = 0;
10424                         }
10425                         release_triple(state, ptr);
10426                         continue;
10427                 }
10428                 last = ptr;
10429         }
10430         block->last = last;
10431         for(user = block->idominates; user; user = user->next) {
10432                 prune_block_variables(state, user->member);
10433         }
10434 }
10435
10436 static void transform_to_ssa_form(struct compile_state *state)
10437 {
10438         insert_phi_operations(state);
10439 #if 0
10440         printf("@%s:%d\n", __FILE__, __LINE__);
10441         print_blocks(state, stdout);
10442 #endif
10443         rename_block_variables(state, state->first_block);
10444         prune_block_variables(state, state->first_block);
10445 }
10446
10447
10448 static void clear_vertex(
10449         struct compile_state *state, struct block *block, void *arg)
10450 {
10451         block->vertex = 0;
10452 }
10453
10454 static void mark_live_block(
10455         struct compile_state *state, struct block *block, int *next_vertex)
10456 {
10457         /* See if this is a block that has not been marked */
10458         if (block->vertex != 0) {
10459                 return;
10460         }
10461         block->vertex = *next_vertex;
10462         *next_vertex += 1;
10463         if (triple_is_branch(state, block->last)) {
10464                 struct triple **targ;
10465                 targ = triple_targ(state, block->last, 0);
10466                 for(; targ; targ = triple_targ(state, block->last, targ)) {
10467                         if (!*targ) {
10468                                 continue;
10469                         }
10470                         if (!triple_stores_block(state, *targ)) {
10471                                 internal_error(state, 0, "bad targ");
10472                         }
10473                         mark_live_block(state, (*targ)->u.block, next_vertex);
10474                 }
10475         }
10476         else if (block->last->next != RHS(state->main_function, 0)) {
10477                 struct triple *ins;
10478                 ins = block->last->next;
10479                 if (!triple_stores_block(state, ins)) {
10480                         internal_error(state, 0, "bad block start");
10481                 }
10482                 mark_live_block(state, ins->u.block, next_vertex);
10483         }
10484 }
10485
10486 static void transform_from_ssa_form(struct compile_state *state)
10487 {
10488         /* To get out of ssa form we insert moves on the incoming
10489          * edges to blocks containting phi functions.
10490          */
10491         struct triple *first;
10492         struct triple *phi, *next;
10493         int next_vertex;
10494
10495         /* Walk the control flow to see which blocks remain alive */
10496         walk_blocks(state, clear_vertex, 0);
10497         next_vertex = 1;
10498         mark_live_block(state, state->first_block, &next_vertex);
10499
10500         /* Walk all of the operations to find the phi functions */
10501         first = RHS(state->main_function, 0);
10502         for(phi = first->next; phi != first ; phi = next) {
10503                 struct block_set *set;
10504                 struct block *block;
10505                 struct triple **slot;
10506                 struct triple *var, *read;
10507                 struct triple_set *use, *use_next;
10508                 int edge, used;
10509                 next = phi->next;
10510                 if (phi->op != OP_PHI) {
10511                         continue;
10512                 }
10513                 block = phi->u.block;
10514                 slot  = &RHS(phi, 0);
10515
10516                 /* Forget uses from code in dead blocks */
10517                 for(use = phi->use; use; use = use_next) {
10518                         struct block *ublock;
10519                         struct triple **expr;
10520                         use_next = use->next;
10521                         ublock = block_of_triple(state, use->member);
10522                         if ((use->member == phi) || (ublock->vertex != 0)) {
10523                                 continue;
10524                         }
10525                         expr = triple_rhs(state, use->member, 0);
10526                         for(; expr; expr = triple_rhs(state, use->member, expr)) {
10527                                 if (*expr == phi) {
10528                                         *expr = 0;
10529                                 }
10530                         }
10531                         unuse_triple(phi, use->member);
10532                 }
10533
10534                 /* A variable to replace the phi function */
10535                 var = post_triple(state, phi, OP_ADECL, phi->type, 0,0);
10536                 /* A read of the single value that is set into the variable */
10537                 read = post_triple(state, var, OP_READ, phi->type, var, 0);
10538                 use_triple(var, read);
10539
10540                 /* Replaces uses of the phi with variable reads */
10541                 propogate_use(state, phi, read);
10542
10543                 /* Walk all of the incoming edges/blocks and insert moves.
10544                  */
10545                 for(edge = 0, set = block->use; set; set = set->next, edge++) {
10546                         struct block *eblock;
10547                         struct triple *move;
10548                         struct triple *val;
10549                         eblock = set->member;
10550                         val = slot[edge];
10551                         slot[edge] = 0;
10552                         unuse_triple(val, phi);
10553
10554                         if (!val || (val == &zero_triple) ||
10555                                 (block->vertex == 0) || (eblock->vertex == 0) ||
10556                                 (val == phi) || (val == read)) {
10557                                 continue;
10558                         }
10559                         
10560                         move = post_triple(state, 
10561                                 val, OP_WRITE, phi->type, var, val);
10562                         use_triple(val, move);
10563                         use_triple(var, move);
10564                 }               
10565                 /* See if there are any writers of var */
10566                 used = 0;
10567                 for(use = var->use; use; use = use->next) {
10568                         struct triple **expr;
10569                         expr = triple_lhs(state, use->member, 0);
10570                         for(; expr; expr = triple_lhs(state, use->member, expr)) {
10571                                 if (*expr == var) {
10572                                         used = 1;
10573                                 }
10574                         }
10575                 }
10576                 /* If var is not used free it */
10577                 if (!used) {
10578                         unuse_triple(var, read);
10579                         free_triple(state, read);
10580                         free_triple(state, var);
10581                 }
10582
10583                 /* Release the phi function */
10584                 release_triple(state, phi);
10585         }
10586         
10587 }
10588
10589
10590 /* 
10591  * Register conflict resolution
10592  * =========================================================
10593  */
10594
10595 static struct reg_info find_def_color(
10596         struct compile_state *state, struct triple *def)
10597 {
10598         struct triple_set *set;
10599         struct reg_info info;
10600         info.reg = REG_UNSET;
10601         info.regcm = 0;
10602         if (!triple_is_def(state, def)) {
10603                 return info;
10604         }
10605         info = arch_reg_lhs(state, def, 0);
10606         if (info.reg >= MAX_REGISTERS) {
10607                 info.reg = REG_UNSET;
10608         }
10609         for(set = def->use; set; set = set->next) {
10610                 struct reg_info tinfo;
10611                 int i;
10612                 i = find_rhs_use(state, set->member, def);
10613                 if (i < 0) {
10614                         continue;
10615                 }
10616                 tinfo = arch_reg_rhs(state, set->member, i);
10617                 if (tinfo.reg >= MAX_REGISTERS) {
10618                         tinfo.reg = REG_UNSET;
10619                 }
10620                 if ((tinfo.reg != REG_UNSET) && 
10621                         (info.reg != REG_UNSET) &&
10622                         (tinfo.reg != info.reg)) {
10623                         internal_error(state, def, "register conflict");
10624                 }
10625                 if ((info.regcm & tinfo.regcm) == 0) {
10626                         internal_error(state, def, "regcm conflict %x & %x == 0",
10627                                 info.regcm, tinfo.regcm);
10628                 }
10629                 if (info.reg == REG_UNSET) {
10630                         info.reg = tinfo.reg;
10631                 }
10632                 info.regcm &= tinfo.regcm;
10633         }
10634         if (info.reg >= MAX_REGISTERS) {
10635                 internal_error(state, def, "register out of range");
10636         }
10637         return info;
10638 }
10639
10640 static struct reg_info find_lhs_pre_color(
10641         struct compile_state *state, struct triple *ins, int index)
10642 {
10643         struct reg_info info;
10644         int zlhs, zrhs, i;
10645         zrhs = TRIPLE_RHS(ins->sizes);
10646         zlhs = TRIPLE_LHS(ins->sizes);
10647         if (!zlhs && triple_is_def(state, ins)) {
10648                 zlhs = 1;
10649         }
10650         if (index >= zlhs) {
10651                 internal_error(state, ins, "Bad lhs %d", index);
10652         }
10653         info = arch_reg_lhs(state, ins, index);
10654         for(i = 0; i < zrhs; i++) {
10655                 struct reg_info rinfo;
10656                 rinfo = arch_reg_rhs(state, ins, i);
10657                 if ((info.reg == rinfo.reg) &&
10658                         (rinfo.reg >= MAX_REGISTERS)) {
10659                         struct reg_info tinfo;
10660                         tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
10661                         info.reg = tinfo.reg;
10662                         info.regcm &= tinfo.regcm;
10663                         break;
10664                 }
10665         }
10666         if (info.reg >= MAX_REGISTERS) {
10667                 info.reg = REG_UNSET;
10668         }
10669         return info;
10670 }
10671
10672 static struct reg_info find_rhs_post_color(
10673         struct compile_state *state, struct triple *ins, int index);
10674
10675 static struct reg_info find_lhs_post_color(
10676         struct compile_state *state, struct triple *ins, int index)
10677 {
10678         struct triple_set *set;
10679         struct reg_info info;
10680         struct triple *lhs;
10681 #if 0
10682         fprintf(stderr, "find_lhs_post_color(%p, %d)\n",
10683                 ins, index);
10684 #endif
10685         if ((index == 0) && triple_is_def(state, ins)) {
10686                 lhs = ins;
10687         }
10688         else if (index < TRIPLE_LHS(ins->sizes)) {
10689                 lhs = LHS(ins, index);
10690         }
10691         else {
10692                 internal_error(state, ins, "Bad lhs %d", index);
10693                 lhs = 0;
10694         }
10695         info = arch_reg_lhs(state, ins, index);
10696         if (info.reg >= MAX_REGISTERS) {
10697                 info.reg = REG_UNSET;
10698         }
10699         for(set = lhs->use; set; set = set->next) {
10700                 struct reg_info rinfo;
10701                 struct triple *user;
10702                 int zrhs, i;
10703                 user = set->member;
10704                 zrhs = TRIPLE_RHS(user->sizes);
10705                 for(i = 0; i < zrhs; i++) {
10706                         if (RHS(user, i) != lhs) {
10707                                 continue;
10708                         }
10709                         rinfo = find_rhs_post_color(state, user, i);
10710                         if ((info.reg != REG_UNSET) &&
10711                                 (rinfo.reg != REG_UNSET) &&
10712                                 (info.reg != rinfo.reg)) {
10713                                 internal_error(state, ins, "register conflict");
10714                         }
10715                         if ((info.regcm & rinfo.regcm) == 0) {
10716                                 internal_error(state, ins, "regcm conflict %x & %x == 0",
10717                                         info.regcm, rinfo.regcm);
10718                         }
10719                         if (info.reg == REG_UNSET) {
10720                                 info.reg = rinfo.reg;
10721                         }
10722                         info.regcm &= rinfo.regcm;
10723                 }
10724         }
10725 #if 0
10726         fprintf(stderr, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
10727                 ins, index, info.reg, info.regcm);
10728 #endif
10729         return info;
10730 }
10731
10732 static struct reg_info find_rhs_post_color(
10733         struct compile_state *state, struct triple *ins, int index)
10734 {
10735         struct reg_info info, rinfo;
10736         int zlhs, i;
10737 #if 0
10738         fprintf(stderr, "find_rhs_post_color(%p, %d)\n",
10739                 ins, index);
10740 #endif
10741         rinfo = arch_reg_rhs(state, ins, index);
10742         zlhs = TRIPLE_LHS(ins->sizes);
10743         if (!zlhs && triple_is_def(state, ins)) {
10744                 zlhs = 1;
10745         }
10746         info = rinfo;
10747         if (info.reg >= MAX_REGISTERS) {
10748                 info.reg = REG_UNSET;
10749         }
10750         for(i = 0; i < zlhs; i++) {
10751                 struct reg_info linfo;
10752                 linfo = arch_reg_lhs(state, ins, i);
10753                 if ((linfo.reg == rinfo.reg) &&
10754                         (linfo.reg >= MAX_REGISTERS)) {
10755                         struct reg_info tinfo;
10756                         tinfo = find_lhs_post_color(state, ins, i);
10757                         if (tinfo.reg >= MAX_REGISTERS) {
10758                                 tinfo.reg = REG_UNSET;
10759                         }
10760                         info.regcm &= linfo.reg;
10761                         info.regcm &= tinfo.regcm;
10762                         if (info.reg != REG_UNSET) {
10763                                 internal_error(state, ins, "register conflict");
10764                         }
10765                         if (info.regcm == 0) {
10766                                 internal_error(state, ins, "regcm conflict");
10767                         }
10768                         info.reg = tinfo.reg;
10769                 }
10770         }
10771 #if 0
10772         fprintf(stderr, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
10773                 ins, index, info.reg, info.regcm);
10774 #endif
10775         return info;
10776 }
10777
10778 static struct reg_info find_lhs_color(
10779         struct compile_state *state, struct triple *ins, int index)
10780 {
10781         struct reg_info pre, post, info;
10782 #if 0
10783         fprintf(stderr, "find_lhs_color(%p, %d)\n",
10784                 ins, index);
10785 #endif
10786         pre = find_lhs_pre_color(state, ins, index);
10787         post = find_lhs_post_color(state, ins, index);
10788         if ((pre.reg != post.reg) &&
10789                 (pre.reg != REG_UNSET) &&
10790                 (post.reg != REG_UNSET)) {
10791                 internal_error(state, ins, "register conflict");
10792         }
10793         info.regcm = pre.regcm & post.regcm;
10794         info.reg = pre.reg;
10795         if (info.reg == REG_UNSET) {
10796                 info.reg = post.reg;
10797         }
10798 #if 0
10799         fprintf(stderr, "find_lhs_color(%p, %d) -> ( %d, %x)\n",
10800                 ins, index, info.reg, info.regcm);
10801 #endif
10802         return info;
10803 }
10804
10805 static struct triple *post_copy(struct compile_state *state, struct triple *ins)
10806 {
10807         struct triple_set *entry, *next;
10808         struct triple *out;
10809         struct reg_info info, rinfo;
10810
10811         info = arch_reg_lhs(state, ins, 0);
10812         out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
10813         use_triple(RHS(out, 0), out);
10814         /* Get the users of ins to use out instead */
10815         for(entry = ins->use; entry; entry = next) {
10816                 int i;
10817                 next = entry->next;
10818                 if (entry->member == out) {
10819                         continue;
10820                 }
10821                 i = find_rhs_use(state, entry->member, ins);
10822                 if (i < 0) {
10823                         continue;
10824                 }
10825                 rinfo = arch_reg_rhs(state, entry->member, i);
10826                 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
10827                         continue;
10828                 }
10829                 replace_rhs_use(state, ins, out, entry->member);
10830         }
10831         transform_to_arch_instruction(state, out);
10832         return out;
10833 }
10834
10835 static struct triple *pre_copy(
10836         struct compile_state *state, struct triple *ins, int index)
10837 {
10838         /* Carefully insert enough operations so that I can
10839          * enter any operation with a GPR32.
10840          */
10841         struct triple *in;
10842         struct triple **expr;
10843         if (ins->op == OP_PHI) {
10844                 internal_error(state, ins, "pre_copy on a phi?");
10845         }
10846         expr = &RHS(ins, index);
10847         in = pre_triple(state, ins, OP_COPY, (*expr)->type, *expr, 0);
10848         unuse_triple(*expr, ins);
10849         *expr = in;
10850         use_triple(RHS(in, 0), in);
10851         use_triple(in, ins);
10852         transform_to_arch_instruction(state, in);
10853         return in;
10854 }
10855
10856
10857 static void insert_copies_to_phi(struct compile_state *state)
10858 {
10859         /* To get out of ssa form we insert moves on the incoming
10860          * edges to blocks containting phi functions.
10861          */
10862         struct triple *first;
10863         struct triple *phi;
10864
10865         /* Walk all of the operations to find the phi functions */
10866         first = RHS(state->main_function, 0);
10867         for(phi = first->next; phi != first ; phi = phi->next) {
10868                 struct block_set *set;
10869                 struct block *block;
10870                 struct triple **slot;
10871                 int edge;
10872                 if (phi->op != OP_PHI) {
10873                         continue;
10874                 }
10875                 phi->id |= TRIPLE_FLAG_POST_SPLIT;
10876                 block = phi->u.block;
10877                 slot  = &RHS(phi, 0);
10878                 /* Walk all of the incoming edges/blocks and insert moves.
10879                  */
10880                 for(edge = 0, set = block->use; set; set = set->next, edge++) {
10881                         struct block *eblock;
10882                         struct triple *move;
10883                         struct triple *val;
10884                         struct triple *ptr;
10885                         eblock = set->member;
10886                         val = slot[edge];
10887
10888                         if (val == phi) {
10889                                 continue;
10890                         }
10891
10892                         get_occurance(val->occurance);
10893                         move = build_triple(state, OP_COPY, phi->type, val, 0,
10894                                 val->occurance);
10895                         move->u.block = eblock;
10896                         move->id |= TRIPLE_FLAG_PRE_SPLIT;
10897                         use_triple(val, move);
10898                         
10899                         slot[edge] = move;
10900                         unuse_triple(val, phi);
10901                         use_triple(move, phi);
10902
10903                         /* Walk through the block backwards to find
10904                          * an appropriate location for the OP_COPY.
10905                          */
10906                         for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
10907                                 struct triple **expr;
10908                                 if ((ptr == phi) || (ptr == val)) {
10909                                         goto out;
10910                                 }
10911                                 expr = triple_rhs(state, ptr, 0);
10912                                 for(;expr; expr = triple_rhs(state, ptr, expr)) {
10913                                         if ((*expr) == phi) {
10914                                                 goto out;
10915                                         }
10916                                 }
10917                         }
10918                 out:
10919                         if (triple_is_branch(state, ptr)) {
10920                                 internal_error(state, ptr,
10921                                         "Could not insert write to phi");
10922                         }
10923                         insert_triple(state, ptr->next, move);
10924                         if (eblock->last == ptr) {
10925                                 eblock->last = move;
10926                         }
10927                         transform_to_arch_instruction(state, move);
10928                 }
10929         }
10930 }
10931
10932 struct triple_reg_set {
10933         struct triple_reg_set *next;
10934         struct triple *member;
10935         struct triple *new;
10936 };
10937
10938 struct reg_block {
10939         struct block *block;
10940         struct triple_reg_set *in;
10941         struct triple_reg_set *out;
10942         int vertex;
10943 };
10944
10945 static int do_triple_set(struct triple_reg_set **head, 
10946         struct triple *member, struct triple *new_member)
10947 {
10948         struct triple_reg_set **ptr, *new;
10949         if (!member)
10950                 return 0;
10951         ptr = head;
10952         while(*ptr) {
10953                 if ((*ptr)->member == member) {
10954                         return 0;
10955                 }
10956                 ptr = &(*ptr)->next;
10957         }
10958         new = xcmalloc(sizeof(*new), "triple_set");
10959         new->member = member;
10960         new->new    = new_member;
10961         new->next   = *head;
10962         *head       = new;
10963         return 1;
10964 }
10965
10966 static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
10967 {
10968         struct triple_reg_set *entry, **ptr;
10969         ptr = head;
10970         while(*ptr) {
10971                 entry = *ptr;
10972                 if (entry->member == member) {
10973                         *ptr = entry->next;
10974                         xfree(entry);
10975                         return;
10976                 }
10977                 else {
10978                         ptr = &entry->next;
10979                 }
10980         }
10981 }
10982
10983 static int in_triple(struct reg_block *rb, struct triple *in)
10984 {
10985         return do_triple_set(&rb->in, in, 0);
10986 }
10987 static void unin_triple(struct reg_block *rb, struct triple *unin)
10988 {
10989         do_triple_unset(&rb->in, unin);
10990 }
10991
10992 static int out_triple(struct reg_block *rb, struct triple *out)
10993 {
10994         return do_triple_set(&rb->out, out, 0);
10995 }
10996 static void unout_triple(struct reg_block *rb, struct triple *unout)
10997 {
10998         do_triple_unset(&rb->out, unout);
10999 }
11000
11001 static int initialize_regblock(struct reg_block *blocks,
11002         struct block *block, int vertex)
11003 {
11004         struct block_set *user;
11005         if (!block || (blocks[block->vertex].block == block)) {
11006                 return vertex;
11007         }
11008         vertex += 1;
11009         /* Renumber the blocks in a convinient fashion */
11010         block->vertex = vertex;
11011         blocks[vertex].block    = block;
11012         blocks[vertex].vertex   = vertex;
11013         for(user = block->use; user; user = user->next) {
11014                 vertex = initialize_regblock(blocks, user->member, vertex);
11015         }
11016         return vertex;
11017 }
11018
11019 static int phi_in(struct compile_state *state, struct reg_block *blocks,
11020         struct reg_block *rb, struct block *suc)
11021 {
11022         /* Read the conditional input set of a successor block
11023          * (i.e. the input to the phi nodes) and place it in the
11024          * current blocks output set.
11025          */
11026         struct block_set *set;
11027         struct triple *ptr;
11028         int edge;
11029         int done, change;
11030         change = 0;
11031         /* Find the edge I am coming in on */
11032         for(edge = 0, set = suc->use; set; set = set->next, edge++) {
11033                 if (set->member == rb->block) {
11034                         break;
11035                 }
11036         }
11037         if (!set) {
11038                 internal_error(state, 0, "Not coming on a control edge?");
11039         }
11040         for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
11041                 struct triple **slot, *expr, *ptr2;
11042                 int out_change, done2;
11043                 done = (ptr == suc->last);
11044                 if (ptr->op != OP_PHI) {
11045                         continue;
11046                 }
11047                 slot = &RHS(ptr, 0);
11048                 expr = slot[edge];
11049                 out_change = out_triple(rb, expr);
11050                 if (!out_change) {
11051                         continue;
11052                 }
11053                 /* If we don't define the variable also plast it
11054                  * in the current blocks input set.
11055                  */
11056                 ptr2 = rb->block->first;
11057                 for(done2 = 0; !done2; ptr2 = ptr2->next) {
11058                         if (ptr2 == expr) {
11059                                 break;
11060                         }
11061                         done2 = (ptr2 == rb->block->last);
11062                 }
11063                 if (!done2) {
11064                         continue;
11065                 }
11066                 change |= in_triple(rb, expr);
11067         }
11068         return change;
11069 }
11070
11071 static int reg_in(struct compile_state *state, struct reg_block *blocks,
11072         struct reg_block *rb, struct block *suc)
11073 {
11074         struct triple_reg_set *in_set;
11075         int change;
11076         change = 0;
11077         /* Read the input set of a successor block
11078          * and place it in the current blocks output set.
11079          */
11080         in_set = blocks[suc->vertex].in;
11081         for(; in_set; in_set = in_set->next) {
11082                 int out_change, done;
11083                 struct triple *first, *last, *ptr;
11084                 out_change = out_triple(rb, in_set->member);
11085                 if (!out_change) {
11086                         continue;
11087                 }
11088                 /* If we don't define the variable also place it
11089                  * in the current blocks input set.
11090                  */
11091                 first = rb->block->first;
11092                 last = rb->block->last;
11093                 done = 0;
11094                 for(ptr = first; !done; ptr = ptr->next) {
11095                         if (ptr == in_set->member) {
11096                                 break;
11097                         }
11098                         done = (ptr == last);
11099                 }
11100                 if (!done) {
11101                         continue;
11102                 }
11103                 change |= in_triple(rb, in_set->member);
11104         }
11105         change |= phi_in(state, blocks, rb, suc);
11106         return change;
11107 }
11108
11109
11110 static int use_in(struct compile_state *state, struct reg_block *rb)
11111 {
11112         /* Find the variables we use but don't define and add
11113          * it to the current blocks input set.
11114          */
11115 #warning "FIXME is this O(N^2) algorithm bad?"
11116         struct block *block;
11117         struct triple *ptr;
11118         int done;
11119         int change;
11120         block = rb->block;
11121         change = 0;
11122         for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
11123                 struct triple **expr;
11124                 done = (ptr == block->first);
11125                 /* The variable a phi function uses depends on the
11126                  * control flow, and is handled in phi_in, not
11127                  * here.
11128                  */
11129                 if (ptr->op == OP_PHI) {
11130                         continue;
11131                 }
11132                 expr = triple_rhs(state, ptr, 0);
11133                 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11134                         struct triple *rhs, *test;
11135                         int tdone;
11136                         rhs = *expr;
11137                         if (!rhs) {
11138                                 continue;
11139                         }
11140                         /* See if rhs is defined in this block */
11141                         for(tdone = 0, test = ptr; !tdone; test = test->prev) {
11142                                 tdone = (test == block->first);
11143                                 if (test == rhs) {
11144                                         rhs = 0;
11145                                         break;
11146                                 }
11147                         }
11148                         /* If I still have a valid rhs add it to in */
11149                         change |= in_triple(rb, rhs);
11150                 }
11151         }
11152         return change;
11153 }
11154
11155 static struct reg_block *compute_variable_lifetimes(
11156         struct compile_state *state)
11157 {
11158         struct reg_block *blocks;
11159         int change;
11160         blocks = xcmalloc(
11161                 sizeof(*blocks)*(state->last_vertex + 1), "reg_block");
11162         initialize_regblock(blocks, state->last_block, 0);
11163         do {
11164                 int i;
11165                 change = 0;
11166                 for(i = 1; i <= state->last_vertex; i++) {
11167                         struct reg_block *rb;
11168                         rb = &blocks[i];
11169                         /* Add the left successor's input set to in */
11170                         if (rb->block->left) {
11171                                 change |= reg_in(state, blocks, rb, rb->block->left);
11172                         }
11173                         /* Add the right successor's input set to in */
11174                         if ((rb->block->right) && 
11175                                 (rb->block->right != rb->block->left)) {
11176                                 change |= reg_in(state, blocks, rb, rb->block->right);
11177                         }
11178                         /* Add use to in... */
11179                         change |= use_in(state, rb);
11180                 }
11181         } while(change);
11182         return blocks;
11183 }
11184
11185 static void free_variable_lifetimes(
11186         struct compile_state *state, struct reg_block *blocks)
11187 {
11188         int i;
11189         /* free in_set && out_set on each block */
11190         for(i = 1; i <= state->last_vertex; i++) {
11191                 struct triple_reg_set *entry, *next;
11192                 struct reg_block *rb;
11193                 rb = &blocks[i];
11194                 for(entry = rb->in; entry ; entry = next) {
11195                         next = entry->next;
11196                         do_triple_unset(&rb->in, entry->member);
11197                 }
11198                 for(entry = rb->out; entry; entry = next) {
11199                         next = entry->next;
11200                         do_triple_unset(&rb->out, entry->member);
11201                 }
11202         }
11203         xfree(blocks);
11204
11205 }
11206
11207 typedef void (*wvl_cb_t)(
11208         struct compile_state *state, 
11209         struct reg_block *blocks, struct triple_reg_set *live, 
11210         struct reg_block *rb, struct triple *ins, void *arg);
11211
11212 static void walk_variable_lifetimes(struct compile_state *state,
11213         struct reg_block *blocks, wvl_cb_t cb, void *arg)
11214 {
11215         int i;
11216         
11217         for(i = 1; i <= state->last_vertex; i++) {
11218                 struct triple_reg_set *live;
11219                 struct triple_reg_set *entry, *next;
11220                 struct triple *ptr, *prev;
11221                 struct reg_block *rb;
11222                 struct block *block;
11223                 int done;
11224
11225                 /* Get the blocks */
11226                 rb = &blocks[i];
11227                 block = rb->block;
11228
11229                 /* Copy out into live */
11230                 live = 0;
11231                 for(entry = rb->out; entry; entry = next) {
11232                         next = entry->next;
11233                         do_triple_set(&live, entry->member, entry->new);
11234                 }
11235                 /* Walk through the basic block calculating live */
11236                 for(done = 0, ptr = block->last; !done; ptr = prev) {
11237                         struct triple **expr;
11238
11239                         prev = ptr->prev;
11240                         done = (ptr == block->first);
11241
11242                         /* Ensure the current definition is in live */
11243                         if (triple_is_def(state, ptr)) {
11244                                 do_triple_set(&live, ptr, 0);
11245                         }
11246
11247                         /* Inform the callback function of what is
11248                          * going on.
11249                          */
11250                          cb(state, blocks, live, rb, ptr, arg);
11251                         
11252                         /* Remove the current definition from live */
11253                         do_triple_unset(&live, ptr);
11254
11255                         /* Add the current uses to live.
11256                          *
11257                          * It is safe to skip phi functions because they do
11258                          * not have any block local uses, and the block
11259                          * output sets already properly account for what
11260                          * control flow depedent uses phi functions do have.
11261                          */
11262                         if (ptr->op == OP_PHI) {
11263                                 continue;
11264                         }
11265                         expr = triple_rhs(state, ptr, 0);
11266                         for(;expr; expr = triple_rhs(state, ptr, expr)) {
11267                                 /* If the triple is not a definition skip it. */
11268                                 if (!*expr || !triple_is_def(state, *expr)) {
11269                                         continue;
11270                                 }
11271                                 do_triple_set(&live, *expr, 0);
11272                         }
11273                 }
11274                 /* Free live */
11275                 for(entry = live; entry; entry = next) {
11276                         next = entry->next;
11277                         do_triple_unset(&live, entry->member);
11278                 }
11279         }
11280 }
11281
11282 static int count_triples(struct compile_state *state)
11283 {
11284         struct triple *first, *ins;
11285         int triples = 0;
11286         first = RHS(state->main_function, 0);
11287         ins = first;
11288         do {
11289                 triples++;
11290                 ins = ins->next;
11291         } while (ins != first);
11292         return triples;
11293 }
11294 struct dead_triple {
11295         struct triple *triple;
11296         struct dead_triple *work_next;
11297         struct block *block;
11298         int color;
11299         int flags;
11300 #define TRIPLE_FLAG_ALIVE 1
11301 };
11302
11303
11304 static void awaken(
11305         struct compile_state *state,
11306         struct dead_triple *dtriple, struct triple **expr,
11307         struct dead_triple ***work_list_tail)
11308 {
11309         struct triple *triple;
11310         struct dead_triple *dt;
11311         if (!expr) {
11312                 return;
11313         }
11314         triple = *expr;
11315         if (!triple) {
11316                 return;
11317         }
11318         if (triple->id <= 0)  {
11319                 internal_error(state, triple, "bad triple id: %d",
11320                         triple->id);
11321         }
11322         if (triple->op == OP_NOOP) {
11323                 internal_warning(state, triple, "awakening noop?");
11324                 return;
11325         }
11326         dt = &dtriple[triple->id];
11327         if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11328                 dt->flags |= TRIPLE_FLAG_ALIVE;
11329                 if (!dt->work_next) {
11330                         **work_list_tail = dt;
11331                         *work_list_tail = &dt->work_next;
11332                 }
11333         }
11334 }
11335
11336 static void eliminate_inefectual_code(struct compile_state *state)
11337 {
11338         struct block *block;
11339         struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
11340         int triples, i;
11341         struct triple *first, *ins;
11342
11343         /* Setup the work list */
11344         work_list = 0;
11345         work_list_tail = &work_list;
11346
11347         first = RHS(state->main_function, 0);
11348
11349         /* Count how many triples I have */
11350         triples = count_triples(state);
11351
11352         /* Now put then in an array and mark all of the triples dead */
11353         dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
11354         
11355         ins = first;
11356         i = 1;
11357         block = 0;
11358         do {
11359                 if (ins->op == OP_LABEL) {
11360                         block = ins->u.block;
11361                 }
11362                 dtriple[i].triple = ins;
11363                 dtriple[i].block  = block;
11364                 dtriple[i].flags  = 0;
11365                 dtriple[i].color  = ins->id;
11366                 ins->id = i;
11367                 /* See if it is an operation we always keep */
11368 #warning "FIXME handle the case of killing a branch instruction"
11369                 if (!triple_is_pure(state, ins) || triple_is_branch(state, ins)) {
11370                         awaken(state, dtriple, &ins, &work_list_tail);
11371                 }
11372                 i++;
11373                 ins = ins->next;
11374         } while(ins != first);
11375         while(work_list) {
11376                 struct dead_triple *dt;
11377                 struct block_set *user;
11378                 struct triple **expr;
11379                 dt = work_list;
11380                 work_list = dt->work_next;
11381                 if (!work_list) {
11382                         work_list_tail = &work_list;
11383                 }
11384                 /* Wake up the data depencencies of this triple */
11385                 expr = 0;
11386                 do {
11387                         expr = triple_rhs(state, dt->triple, expr);
11388                         awaken(state, dtriple, expr, &work_list_tail);
11389                 } while(expr);
11390                 do {
11391                         expr = triple_lhs(state, dt->triple, expr);
11392                         awaken(state, dtriple, expr, &work_list_tail);
11393                 } while(expr);
11394                 do {
11395                         expr = triple_misc(state, dt->triple, expr);
11396                         awaken(state, dtriple, expr, &work_list_tail);
11397                 } while(expr);
11398                 /* Wake up the forward control dependencies */
11399                 do {
11400                         expr = triple_targ(state, dt->triple, expr);
11401                         awaken(state, dtriple, expr, &work_list_tail);
11402                 } while(expr);
11403                 /* Wake up the reverse control dependencies of this triple */
11404                 for(user = dt->block->ipdomfrontier; user; user = user->next) {
11405                         awaken(state, dtriple, &user->member->last, &work_list_tail);
11406                 }
11407         }
11408         for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
11409                 if ((dt->triple->op == OP_NOOP) && 
11410                         (dt->flags & TRIPLE_FLAG_ALIVE)) {
11411                         internal_error(state, dt->triple, "noop effective?");
11412                 }
11413                 dt->triple->id = dt->color;     /* Restore the color */
11414                 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11415 #warning "FIXME handle the case of killing a basic block"
11416                         if (dt->block->first == dt->triple) {
11417                                 continue;
11418                         }
11419                         if (dt->block->last == dt->triple) {
11420                                 dt->block->last = dt->triple->prev;
11421                         }
11422                         release_triple(state, dt->triple);
11423                 }
11424         }
11425         xfree(dtriple);
11426 }
11427
11428
11429 static void insert_mandatory_copies(struct compile_state *state)
11430 {
11431         struct triple *ins, *first;
11432
11433         /* The object is with a minimum of inserted copies,
11434          * to resolve in fundamental register conflicts between
11435          * register value producers and consumers.
11436          * Theoretically we may be greater than minimal when we
11437          * are inserting copies before instructions but that
11438          * case should be rare.
11439          */
11440         first = RHS(state->main_function, 0);
11441         ins = first;
11442         do {
11443                 struct triple_set *entry, *next;
11444                 struct triple *tmp;
11445                 struct reg_info info;
11446                 unsigned reg, regcm;
11447                 int do_post_copy, do_pre_copy;
11448                 tmp = 0;
11449                 if (!triple_is_def(state, ins)) {
11450                         goto next;
11451                 }
11452                 /* Find the architecture specific color information */
11453                 info = arch_reg_lhs(state, ins, 0);
11454                 if (info.reg >= MAX_REGISTERS) {
11455                         info.reg = REG_UNSET;
11456                 }
11457                 
11458                 reg = REG_UNSET;
11459                 regcm = arch_type_to_regcm(state, ins->type);
11460                 do_post_copy = do_pre_copy = 0;
11461
11462                 /* Walk through the uses of ins and check for conflicts */
11463                 for(entry = ins->use; entry; entry = next) {
11464                         struct reg_info rinfo;
11465                         int i;
11466                         next = entry->next;
11467                         i = find_rhs_use(state, entry->member, ins);
11468                         if (i < 0) {
11469                                 continue;
11470                         }
11471                         
11472                         /* Find the users color requirements */
11473                         rinfo = arch_reg_rhs(state, entry->member, i);
11474                         if (rinfo.reg >= MAX_REGISTERS) {
11475                                 rinfo.reg = REG_UNSET;
11476                         }
11477                         
11478                         /* See if I need a pre_copy */
11479                         if (rinfo.reg != REG_UNSET) {
11480                                 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
11481                                         do_pre_copy = 1;
11482                                 }
11483                                 reg = rinfo.reg;
11484                         }
11485                         regcm &= rinfo.regcm;
11486                         regcm = arch_regcm_normalize(state, regcm);
11487                         if (regcm == 0) {
11488                                 do_pre_copy = 1;
11489                         }
11490                 }
11491                 do_post_copy =
11492                         !do_pre_copy &&
11493                         (((info.reg != REG_UNSET) && 
11494                                 (reg != REG_UNSET) &&
11495                                 (info.reg != reg)) ||
11496                         ((info.regcm & regcm) == 0));
11497
11498                 reg = info.reg;
11499                 regcm = info.regcm;
11500                 /* Walk through the uses of insert and do a pre_copy or see if a post_copy is warranted */
11501                 for(entry = ins->use; entry; entry = next) {
11502                         struct reg_info rinfo;
11503                         int i;
11504                         next = entry->next;
11505                         i = find_rhs_use(state, entry->member, ins);
11506                         if (i < 0) {
11507                                 continue;
11508                         }
11509                         
11510                         /* Find the users color requirements */
11511                         rinfo = arch_reg_rhs(state, entry->member, i);
11512                         if (rinfo.reg >= MAX_REGISTERS) {
11513                                 rinfo.reg = REG_UNSET;
11514                         }
11515
11516                         /* Now see if it is time to do the pre_copy */
11517                         if (rinfo.reg != REG_UNSET) {
11518                                 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
11519                                         ((regcm & rinfo.regcm) == 0) ||
11520                                         /* Don't let a mandatory coalesce sneak
11521                                          * into a operation that is marked to prevent
11522                                          * coalescing.
11523                                          */
11524                                         ((reg != REG_UNNEEDED) &&
11525                                         ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
11526                                         (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
11527                                         ) {
11528                                         if (do_pre_copy) {
11529                                                 struct triple *user;
11530                                                 user = entry->member;
11531                                                 if (RHS(user, i) != ins) {
11532                                                         internal_error(state, user, "bad rhs");
11533                                                 }
11534                                                 tmp = pre_copy(state, user, i);
11535                                                 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
11536                                                 continue;
11537                                         } else {
11538                                                 do_post_copy = 1;
11539                                         }
11540                                 }
11541                                 reg = rinfo.reg;
11542                         }
11543                         if ((regcm & rinfo.regcm) == 0) {
11544                                 if (do_pre_copy) {
11545                                         struct triple *user;
11546                                         user = entry->member;
11547                                         if (RHS(user, i) != ins) {
11548                                                 internal_error(state, user, "bad rhs");
11549                                         }
11550                                         tmp = pre_copy(state, user, i);
11551                                         tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
11552                                         continue;
11553                                 } else {
11554                                         do_post_copy = 1;
11555                                 }
11556                         }
11557                         regcm &= rinfo.regcm;
11558                         
11559                 }
11560                 if (do_post_copy) {
11561                         struct reg_info pre, post;
11562                         tmp = post_copy(state, ins);
11563                         tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
11564                         pre = arch_reg_lhs(state, ins, 0);
11565                         post = arch_reg_lhs(state, tmp, 0);
11566                         if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
11567                                 internal_error(state, tmp, "useless copy");
11568                         }
11569                 }
11570         next:
11571                 ins = ins->next;
11572         } while(ins != first);
11573 }
11574
11575
11576 struct live_range_edge;
11577 struct live_range_def;
11578 struct live_range {
11579         struct live_range_edge *edges;
11580         struct live_range_def *defs;
11581 /* Note. The list pointed to by defs is kept in order.
11582  * That is baring splits in the flow control
11583  * defs dominates defs->next wich dominates defs->next->next
11584  * etc.
11585  */
11586         unsigned color;
11587         unsigned classes;
11588         unsigned degree;
11589         unsigned length;
11590         struct live_range *group_next, **group_prev;
11591 };
11592
11593 struct live_range_edge {
11594         struct live_range_edge *next;
11595         struct live_range *node;
11596 };
11597
11598 struct live_range_def {
11599         struct live_range_def *next;
11600         struct live_range_def *prev;
11601         struct live_range *lr;
11602         struct triple *def;
11603         unsigned orig_id;
11604 };
11605
11606 #define LRE_HASH_SIZE 2048
11607 struct lre_hash {
11608         struct lre_hash *next;
11609         struct live_range *left;
11610         struct live_range *right;
11611 };
11612
11613
11614 struct reg_state {
11615         struct lre_hash *hash[LRE_HASH_SIZE];
11616         struct reg_block *blocks;
11617         struct live_range_def *lrd;
11618         struct live_range *lr;
11619         struct live_range *low, **low_tail;
11620         struct live_range *high, **high_tail;
11621         unsigned defs;
11622         unsigned ranges;
11623         int passes, max_passes;
11624 #define MAX_ALLOCATION_PASSES 100
11625 };
11626
11627
11628 static unsigned regc_max_size(struct compile_state *state, int classes)
11629 {
11630         unsigned max_size;
11631         int i;
11632         max_size = 0;
11633         for(i = 0; i < MAX_REGC; i++) {
11634                 if (classes & (1 << i)) {
11635                         unsigned size;
11636                         size = arch_regc_size(state, i);
11637                         if (size > max_size) {
11638                                 max_size = size;
11639                         }
11640                 }
11641         }
11642         return max_size;
11643 }
11644
11645 static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
11646 {
11647         unsigned equivs[MAX_REG_EQUIVS];
11648         int i;
11649         if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
11650                 internal_error(state, 0, "invalid register");
11651         }
11652         if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
11653                 internal_error(state, 0, "invalid register");
11654         }
11655         arch_reg_equivs(state, equivs, reg1);
11656         for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11657                 if (equivs[i] == reg2) {
11658                         return 1;
11659                 }
11660         }
11661         return 0;
11662 }
11663
11664 static void reg_fill_used(struct compile_state *state, char *used, int reg)
11665 {
11666         unsigned equivs[MAX_REG_EQUIVS];
11667         int i;
11668         if (reg == REG_UNNEEDED) {
11669                 return;
11670         }
11671         arch_reg_equivs(state, equivs, reg);
11672         for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11673                 used[equivs[i]] = 1;
11674         }
11675         return;
11676 }
11677
11678 static void reg_inc_used(struct compile_state *state, char *used, int reg)
11679 {
11680         unsigned equivs[MAX_REG_EQUIVS];
11681         int i;
11682         if (reg == REG_UNNEEDED) {
11683                 return;
11684         }
11685         arch_reg_equivs(state, equivs, reg);
11686         for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11687                 used[equivs[i]] += 1;
11688         }
11689         return;
11690 }
11691
11692 static unsigned int hash_live_edge(
11693         struct live_range *left, struct live_range *right)
11694 {
11695         unsigned int hash, val;
11696         unsigned long lval, rval;
11697         lval = ((unsigned long)left)/sizeof(struct live_range);
11698         rval = ((unsigned long)right)/sizeof(struct live_range);
11699         hash = 0;
11700         while(lval) {
11701                 val = lval & 0xff;
11702                 lval >>= 8;
11703                 hash = (hash *263) + val;
11704         }
11705         while(rval) {
11706                 val = rval & 0xff;
11707                 rval >>= 8;
11708                 hash = (hash *263) + val;
11709         }
11710         hash = hash & (LRE_HASH_SIZE - 1);
11711         return hash;
11712 }
11713
11714 static struct lre_hash **lre_probe(struct reg_state *rstate,
11715         struct live_range *left, struct live_range *right)
11716 {
11717         struct lre_hash **ptr;
11718         unsigned int index;
11719         /* Ensure left <= right */
11720         if (left > right) {
11721                 struct live_range *tmp;
11722                 tmp = left;
11723                 left = right;
11724                 right = tmp;
11725         }
11726         index = hash_live_edge(left, right);
11727         
11728         ptr = &rstate->hash[index];
11729         while(*ptr) {
11730                 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
11731                         break;
11732                 }
11733                 ptr = &(*ptr)->next;
11734         }
11735         return ptr;
11736 }
11737
11738 static int interfere(struct reg_state *rstate,
11739         struct live_range *left, struct live_range *right)
11740 {
11741         struct lre_hash **ptr;
11742         ptr = lre_probe(rstate, left, right);
11743         return ptr && *ptr;
11744 }
11745
11746 static void add_live_edge(struct reg_state *rstate, 
11747         struct live_range *left, struct live_range *right)
11748 {
11749         /* FIXME the memory allocation overhead is noticeable here... */
11750         struct lre_hash **ptr, *new_hash;
11751         struct live_range_edge *edge;
11752
11753         if (left == right) {
11754                 return;
11755         }
11756         if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
11757                 return;
11758         }
11759         /* Ensure left <= right */
11760         if (left > right) {
11761                 struct live_range *tmp;
11762                 tmp = left;
11763                 left = right;
11764                 right = tmp;
11765         }
11766         ptr = lre_probe(rstate, left, right);
11767         if (*ptr) {
11768                 return;
11769         }
11770 #if 0
11771         fprintf(stderr, "new_live_edge(%p, %p)\n",
11772                 left, right);
11773 #endif
11774         new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
11775         new_hash->next  = *ptr;
11776         new_hash->left  = left;
11777         new_hash->right = right;
11778         *ptr = new_hash;
11779
11780         edge = xmalloc(sizeof(*edge), "live_range_edge");
11781         edge->next   = left->edges;
11782         edge->node   = right;
11783         left->edges  = edge;
11784         left->degree += 1;
11785         
11786         edge = xmalloc(sizeof(*edge), "live_range_edge");
11787         edge->next    = right->edges;
11788         edge->node    = left;
11789         right->edges  = edge;
11790         right->degree += 1;
11791 }
11792
11793 static void remove_live_edge(struct reg_state *rstate,
11794         struct live_range *left, struct live_range *right)
11795 {
11796         struct live_range_edge *edge, **ptr;
11797         struct lre_hash **hptr, *entry;
11798         hptr = lre_probe(rstate, left, right);
11799         if (!hptr || !*hptr) {
11800                 return;
11801         }
11802         entry = *hptr;
11803         *hptr = entry->next;
11804         xfree(entry);
11805
11806         for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
11807                 edge = *ptr;
11808                 if (edge->node == right) {
11809                         *ptr = edge->next;
11810                         memset(edge, 0, sizeof(*edge));
11811                         xfree(edge);
11812                         right->degree--;
11813                         break;
11814                 }
11815         }
11816         for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
11817                 edge = *ptr;
11818                 if (edge->node == left) {
11819                         *ptr = edge->next;
11820                         memset(edge, 0, sizeof(*edge));
11821                         xfree(edge);
11822                         left->degree--;
11823                         break;
11824                 }
11825         }
11826 }
11827
11828 static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
11829 {
11830         struct live_range_edge *edge, *next;
11831         for(edge = range->edges; edge; edge = next) {
11832                 next = edge->next;
11833                 remove_live_edge(rstate, range, edge->node);
11834         }
11835 }
11836
11837 static void transfer_live_edges(struct reg_state *rstate, 
11838         struct live_range *dest, struct live_range *src)
11839 {
11840         struct live_range_edge *edge, *next;
11841         for(edge = src->edges; edge; edge = next) {
11842                 struct live_range *other;
11843                 next = edge->next;
11844                 other = edge->node;
11845                 remove_live_edge(rstate, src, other);
11846                 add_live_edge(rstate, dest, other);
11847         }
11848 }
11849
11850
11851 /* Interference graph...
11852  * 
11853  * new(n) --- Return a graph with n nodes but no edges.
11854  * add(g,x,y) --- Return a graph including g with an between x and y
11855  * interfere(g, x, y) --- Return true if there exists an edge between the nodes
11856  *                x and y in the graph g
11857  * degree(g, x) --- Return the degree of the node x in the graph g
11858  * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
11859  *
11860  * Implement with a hash table && a set of adjcency vectors.
11861  * The hash table supports constant time implementations of add and interfere.
11862  * The adjacency vectors support an efficient implementation of neighbors.
11863  */
11864
11865 /* 
11866  *     +---------------------------------------------------+
11867  *     |         +--------------+                          |
11868  *     v         v              |                          |
11869  * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select 
11870  *
11871  * -- In simplify implment optimistic coloring... (No backtracking)
11872  * -- Implement Rematerialization it is the only form of spilling we can perform
11873  *    Essentially this means dropping a constant from a register because
11874  *    we can regenerate it later.
11875  *
11876  * --- Very conservative colalescing (don't colalesce just mark the opportunities)
11877  *     coalesce at phi points...
11878  * --- Bias coloring if at all possible do the coalesing a compile time.
11879  *
11880  *
11881  */
11882
11883 static void different_colored(
11884         struct compile_state *state, struct reg_state *rstate, 
11885         struct triple *parent, struct triple *ins)
11886 {
11887         struct live_range *lr;
11888         struct triple **expr;
11889         lr = rstate->lrd[ins->id].lr;
11890         expr = triple_rhs(state, ins, 0);
11891         for(;expr; expr = triple_rhs(state, ins, expr)) {
11892                 struct live_range *lr2;
11893                 if (!*expr || (*expr == parent) || (*expr == ins)) {
11894                         continue;
11895                 }
11896                 lr2 = rstate->lrd[(*expr)->id].lr;
11897                 if (lr->color == lr2->color) {
11898                         internal_error(state, ins, "live range too big");
11899                 }
11900         }
11901 }
11902
11903
11904 static struct live_range *coalesce_ranges(
11905         struct compile_state *state, struct reg_state *rstate,
11906         struct live_range *lr1, struct live_range *lr2)
11907 {
11908         struct live_range_def *head, *mid1, *mid2, *end, *lrd;
11909         unsigned color;
11910         unsigned classes;
11911         if (lr1 == lr2) {
11912                 return lr1;
11913         }
11914         if (!lr1->defs || !lr2->defs) {
11915                 internal_error(state, 0,
11916                         "cannot coalese dead live ranges");
11917         }
11918         if ((lr1->color == REG_UNNEEDED) ||
11919                 (lr2->color == REG_UNNEEDED)) {
11920                 internal_error(state, 0, 
11921                         "cannot coalesce live ranges without a possible color");
11922         }
11923         if ((lr1->color != lr2->color) &&
11924                 (lr1->color != REG_UNSET) &&
11925                 (lr2->color != REG_UNSET)) {
11926                 internal_error(state, lr1->defs->def, 
11927                         "cannot coalesce live ranges of different colors");
11928         }
11929         color = lr1->color;
11930         if (color == REG_UNSET) {
11931                 color = lr2->color;
11932         }
11933         classes = lr1->classes & lr2->classes;
11934         if (!classes) {
11935                 internal_error(state, lr1->defs->def,
11936                         "cannot coalesce live ranges with dissimilar register classes");
11937         }
11938         /* If there is a clear dominate live range put it in lr1,
11939          * For purposes of this test phi functions are
11940          * considered dominated by the definitions that feed into
11941          * them. 
11942          */
11943         if ((lr1->defs->prev->def->op == OP_PHI) ||
11944                 ((lr2->defs->prev->def->op != OP_PHI) &&
11945                 tdominates(state, lr2->defs->def, lr1->defs->def))) {
11946                 struct live_range *tmp;
11947                 tmp = lr1;
11948                 lr1 = lr2;
11949                 lr2 = tmp;
11950         }
11951 #if 0
11952         if (lr1->defs->orig_id  & TRIPLE_FLAG_POST_SPLIT) {
11953                 fprintf(stderr, "lr1 post\n");
11954         }
11955         if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
11956                 fprintf(stderr, "lr1 pre\n");
11957         }
11958         if (lr2->defs->orig_id  & TRIPLE_FLAG_POST_SPLIT) {
11959                 fprintf(stderr, "lr2 post\n");
11960         }
11961         if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
11962                 fprintf(stderr, "lr2 pre\n");
11963         }
11964 #endif
11965 #if 0
11966         fprintf(stderr, "coalesce color1(%p): %3d color2(%p) %3d\n",
11967                 lr1->defs->def,
11968                 lr1->color,
11969                 lr2->defs->def,
11970                 lr2->color);
11971 #endif
11972         
11973         lr1->classes = classes;
11974         /* Append lr2 onto lr1 */
11975 #warning "FIXME should this be a merge instead of a splice?"
11976         /* This FIXME item applies to the correctness of live_range_end 
11977          * and to the necessity of making multiple passes of coalesce_live_ranges.
11978          * A failure to find some coalesce opportunities in coaleace_live_ranges
11979          * does not impact the correct of the compiler just the efficiency with
11980          * which registers are allocated.
11981          */
11982         head = lr1->defs;
11983         mid1 = lr1->defs->prev;
11984         mid2 = lr2->defs;
11985         end  = lr2->defs->prev;
11986         
11987         head->prev = end;
11988         end->next  = head;
11989
11990         mid1->next = mid2;
11991         mid2->prev = mid1;
11992
11993         /* Fixup the live range in the added live range defs */
11994         lrd = head;
11995         do {
11996                 lrd->lr = lr1;
11997                 lrd = lrd->next;
11998         } while(lrd != head);
11999
12000         /* Mark lr2 as free. */
12001         lr2->defs = 0;
12002         lr2->color = REG_UNNEEDED;
12003         lr2->classes = 0;
12004
12005         if (!lr1->defs) {
12006                 internal_error(state, 0, "lr1->defs == 0 ?");
12007         }
12008
12009         lr1->color   = color;
12010         lr1->classes = classes;
12011
12012         /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
12013         transfer_live_edges(rstate, lr1, lr2);
12014
12015         return lr1;
12016 }
12017
12018 static struct live_range_def *live_range_head(
12019         struct compile_state *state, struct live_range *lr,
12020         struct live_range_def *last)
12021 {
12022         struct live_range_def *result;
12023         result = 0;
12024         if (last == 0) {
12025                 result = lr->defs;
12026         }
12027         else if (!tdominates(state, lr->defs->def, last->next->def)) {
12028                 result = last->next;
12029         }
12030         return result;
12031 }
12032
12033 static struct live_range_def *live_range_end(
12034         struct compile_state *state, struct live_range *lr,
12035         struct live_range_def *last)
12036 {
12037         struct live_range_def *result;
12038         result = 0;
12039         if (last == 0) {
12040                 result = lr->defs->prev;
12041         }
12042         else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
12043                 result = last->prev;
12044         }
12045         return result;
12046 }
12047
12048
12049 static void initialize_live_ranges(
12050         struct compile_state *state, struct reg_state *rstate)
12051 {
12052         struct triple *ins, *first;
12053         size_t count, size;
12054         int i, j;
12055
12056         first = RHS(state->main_function, 0);
12057         /* First count how many instructions I have.
12058          */
12059         count = count_triples(state);
12060         /* Potentially I need one live range definitions for each
12061          * instruction, plus an extra for the split routines.
12062          */
12063         rstate->defs = count + 1;
12064         /* Potentially I need one live range for each instruction
12065          * plus an extra for the dummy live range.
12066          */
12067         rstate->ranges = count + 1;
12068         size = sizeof(rstate->lrd[0]) * rstate->defs;
12069         rstate->lrd = xcmalloc(size, "live_range_def");
12070         size = sizeof(rstate->lr[0]) * rstate->ranges;
12071         rstate->lr  = xcmalloc(size, "live_range");
12072
12073         /* Setup the dummy live range */
12074         rstate->lr[0].classes = 0;
12075         rstate->lr[0].color = REG_UNSET;
12076         rstate->lr[0].defs = 0;
12077         i = j = 0;
12078         ins = first;
12079         do {
12080                 /* If the triple is a variable give it a live range */
12081                 if (triple_is_def(state, ins)) {
12082                         struct reg_info info;
12083                         /* Find the architecture specific color information */
12084                         info = find_def_color(state, ins);
12085
12086                         i++;
12087                         rstate->lr[i].defs    = &rstate->lrd[j];
12088                         rstate->lr[i].color   = info.reg;
12089                         rstate->lr[i].classes = info.regcm;
12090                         rstate->lr[i].degree  = 0;
12091                         rstate->lrd[j].lr = &rstate->lr[i];
12092                 } 
12093                 /* Otherwise give the triple the dummy live range. */
12094                 else {
12095                         rstate->lrd[j].lr = &rstate->lr[0];
12096                 }
12097
12098                 /* Initalize the live_range_def */
12099                 rstate->lrd[j].next    = &rstate->lrd[j];
12100                 rstate->lrd[j].prev    = &rstate->lrd[j];
12101                 rstate->lrd[j].def     = ins;
12102                 rstate->lrd[j].orig_id = ins->id;
12103                 ins->id = j;
12104
12105                 j++;
12106                 ins = ins->next;
12107         } while(ins != first);
12108         rstate->ranges = i;
12109         rstate->defs -= 1;
12110
12111         /* Make a second pass to handle achitecture specific register
12112          * constraints.
12113          */
12114         ins = first;
12115         do {
12116                 int zlhs, zrhs, i, j;
12117                 if (ins->id > rstate->defs) {
12118                         internal_error(state, ins, "bad id");
12119                 }
12120                 
12121                 /* Walk through the template of ins and coalesce live ranges */
12122                 zlhs = TRIPLE_LHS(ins->sizes);
12123                 if ((zlhs == 0) && triple_is_def(state, ins)) {
12124                         zlhs = 1;
12125                 }
12126                 zrhs = TRIPLE_RHS(ins->sizes);
12127                 
12128                 for(i = 0; i < zlhs; i++) {
12129                         struct reg_info linfo;
12130                         struct live_range_def *lhs;
12131                         linfo = arch_reg_lhs(state, ins, i);
12132                         if (linfo.reg < MAX_REGISTERS) {
12133                                 continue;
12134                         }
12135                         if (triple_is_def(state, ins)) {
12136                                 lhs = &rstate->lrd[ins->id];
12137                         } else {
12138                                 lhs = &rstate->lrd[LHS(ins, i)->id];
12139                         }
12140                         for(j = 0; j < zrhs; j++) {
12141                                 struct reg_info rinfo;
12142                                 struct live_range_def *rhs;
12143                                 rinfo = arch_reg_rhs(state, ins, j);
12144                                 if (rinfo.reg < MAX_REGISTERS) {
12145                                         continue;
12146                                 }
12147                                 rhs = &rstate->lrd[RHS(ins, i)->id];
12148                                 if (rinfo.reg == linfo.reg) {
12149                                         coalesce_ranges(state, rstate, 
12150                                                 lhs->lr, rhs->lr);
12151                                 }
12152                         }
12153                 }
12154                 ins = ins->next;
12155         } while(ins != first);
12156 }
12157
12158 static void graph_ins(
12159         struct compile_state *state, 
12160         struct reg_block *blocks, struct triple_reg_set *live, 
12161         struct reg_block *rb, struct triple *ins, void *arg)
12162 {
12163         struct reg_state *rstate = arg;
12164         struct live_range *def;
12165         struct triple_reg_set *entry;
12166
12167         /* If the triple is not a definition
12168          * we do not have a definition to add to
12169          * the interference graph.
12170          */
12171         if (!triple_is_def(state, ins)) {
12172                 return;
12173         }
12174         def = rstate->lrd[ins->id].lr;
12175         
12176         /* Create an edge between ins and everything that is
12177          * alive, unless the live_range cannot share
12178          * a physical register with ins.
12179          */
12180         for(entry = live; entry; entry = entry->next) {
12181                 struct live_range *lr;
12182                 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
12183                         internal_error(state, 0, "bad entry?");
12184                 }
12185                 lr = rstate->lrd[entry->member->id].lr;
12186                 if (def == lr) {
12187                         continue;
12188                 }
12189                 if (!arch_regcm_intersect(def->classes, lr->classes)) {
12190                         continue;
12191                 }
12192                 add_live_edge(rstate, def, lr);
12193         }
12194         return;
12195 }
12196
12197 static struct live_range *get_verify_live_range(
12198         struct compile_state *state, struct reg_state *rstate, struct triple *ins)
12199 {
12200         struct live_range *lr;
12201         struct live_range_def *lrd;
12202         int ins_found;
12203         if ((ins->id < 0) || (ins->id > rstate->defs)) {
12204                 internal_error(state, ins, "bad ins?");
12205         }
12206         lr = rstate->lrd[ins->id].lr;
12207         ins_found = 0;
12208         lrd = lr->defs;
12209         do {
12210                 if (lrd->def == ins) {
12211                         ins_found = 1;
12212                 }
12213                 lrd = lrd->next;
12214         } while(lrd != lr->defs);
12215         if (!ins_found) {
12216                 internal_error(state, ins, "ins not in live range");
12217         }
12218         return lr;
12219 }
12220
12221 static void verify_graph_ins(
12222         struct compile_state *state, 
12223         struct reg_block *blocks, struct triple_reg_set *live, 
12224         struct reg_block *rb, struct triple *ins, void *arg)
12225 {
12226         struct reg_state *rstate = arg;
12227         struct triple_reg_set *entry1, *entry2;
12228
12229
12230         /* Compare live against edges and make certain the code is working */
12231         for(entry1 = live; entry1; entry1 = entry1->next) {
12232                 struct live_range *lr1;
12233                 lr1 = get_verify_live_range(state, rstate, entry1->member);
12234                 for(entry2 = live; entry2; entry2 = entry2->next) {
12235                         struct live_range *lr2;
12236                         struct live_range_edge *edge2;
12237                         int lr1_found;
12238                         int lr2_degree;
12239                         if (entry2 == entry1) {
12240                                 continue;
12241                         }
12242                         lr2 = get_verify_live_range(state, rstate, entry2->member);
12243                         if (lr1 == lr2) {
12244                                 internal_error(state, entry2->member, 
12245                                         "live range with 2 values simultaneously alive");
12246                         }
12247                         if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
12248                                 continue;
12249                         }
12250                         if (!interfere(rstate, lr1, lr2)) {
12251                                 internal_error(state, entry2->member, 
12252                                         "edges don't interfere?");
12253                         }
12254                                 
12255                         lr1_found = 0;
12256                         lr2_degree = 0;
12257                         for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
12258                                 lr2_degree++;
12259                                 if (edge2->node == lr1) {
12260                                         lr1_found = 1;
12261                                 }
12262                         }
12263                         if (lr2_degree != lr2->degree) {
12264                                 internal_error(state, entry2->member,
12265                                         "computed degree: %d does not match reported degree: %d\n",
12266                                         lr2_degree, lr2->degree);
12267                         }
12268                         if (!lr1_found) {
12269                                 internal_error(state, entry2->member, "missing edge");
12270                         }
12271                 }
12272         }
12273         return;
12274 }
12275
12276
12277 static void print_interference_ins(
12278         struct compile_state *state, 
12279         struct reg_block *blocks, struct triple_reg_set *live, 
12280         struct reg_block *rb, struct triple *ins, void *arg)
12281 {
12282         struct reg_state *rstate = arg;
12283         struct live_range *lr;
12284         unsigned id;
12285
12286         lr = rstate->lrd[ins->id].lr;
12287         id = ins->id;
12288         ins->id = rstate->lrd[id].orig_id;
12289         SET_REG(ins->id, lr->color);
12290         display_triple(stdout, ins);
12291         ins->id = id;
12292
12293         if (lr->defs) {
12294                 struct live_range_def *lrd;
12295                 printf("       range:");
12296                 lrd = lr->defs;
12297                 do {
12298                         printf(" %-10p", lrd->def);
12299                         lrd = lrd->next;
12300                 } while(lrd != lr->defs);
12301                 printf("\n");
12302         }
12303         if (live) {
12304                 struct triple_reg_set *entry;
12305                 printf("        live:");
12306                 for(entry = live; entry; entry = entry->next) {
12307                         printf(" %-10p", entry->member);
12308                 }
12309                 printf("\n");
12310         }
12311         if (lr->edges) {
12312                 struct live_range_edge *entry;
12313                 printf("       edges:");
12314                 for(entry = lr->edges; entry; entry = entry->next) {
12315                         struct live_range_def *lrd;
12316                         lrd = entry->node->defs;
12317                         do {
12318                                 printf(" %-10p", lrd->def);
12319                                 lrd = lrd->next;
12320                         } while(lrd != entry->node->defs);
12321                         printf("|");
12322                 }
12323                 printf("\n");
12324         }
12325         if (triple_is_branch(state, ins)) {
12326                 printf("\n");
12327         }
12328         return;
12329 }
12330
12331 static int coalesce_live_ranges(
12332         struct compile_state *state, struct reg_state *rstate)
12333 {
12334         /* At the point where a value is moved from one
12335          * register to another that value requires two
12336          * registers, thus increasing register pressure.
12337          * Live range coaleescing reduces the register
12338          * pressure by keeping a value in one register
12339          * longer.
12340          *
12341          * In the case of a phi function all paths leading
12342          * into it must be allocated to the same register
12343          * otherwise the phi function may not be removed.
12344          *
12345          * Forcing a value to stay in a single register
12346          * for an extended period of time does have
12347          * limitations when applied to non homogenous
12348          * register pool.  
12349          *
12350          * The two cases I have identified are:
12351          * 1) Two forced register assignments may
12352          *    collide.
12353          * 2) Registers may go unused because they
12354          *    are only good for storing the value
12355          *    and not manipulating it.
12356          *
12357          * Because of this I need to split live ranges,
12358          * even outside of the context of coalesced live
12359          * ranges.  The need to split live ranges does
12360          * impose some constraints on live range coalescing.
12361          *
12362          * - Live ranges may not be coalesced across phi
12363          *   functions.  This creates a 2 headed live
12364          *   range that cannot be sanely split.
12365          *
12366          * - phi functions (coalesced in initialize_live_ranges) 
12367          *   are handled as pre split live ranges so we will
12368          *   never attempt to split them.
12369          */
12370         int coalesced;
12371         int i;
12372
12373         coalesced = 0;
12374         for(i = 0; i <= rstate->ranges; i++) {
12375                 struct live_range *lr1;
12376                 struct live_range_def *lrd1;
12377                 lr1 = &rstate->lr[i];
12378                 if (!lr1->defs) {
12379                         continue;
12380                 }
12381                 lrd1 = live_range_end(state, lr1, 0);
12382                 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
12383                         struct triple_set *set;
12384                         if (lrd1->def->op != OP_COPY) {
12385                                 continue;
12386                         }
12387                         /* Skip copies that are the result of a live range split. */
12388                         if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
12389                                 continue;
12390                         }
12391                         for(set = lrd1->def->use; set; set = set->next) {
12392                                 struct live_range_def *lrd2;
12393                                 struct live_range *lr2, *res;
12394
12395                                 lrd2 = &rstate->lrd[set->member->id];
12396
12397                                 /* Don't coalesce with instructions
12398                                  * that are the result of a live range
12399                                  * split.
12400                                  */
12401                                 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
12402                                         continue;
12403                                 }
12404                                 lr2 = rstate->lrd[set->member->id].lr;
12405                                 if (lr1 == lr2) {
12406                                         continue;
12407                                 }
12408                                 if ((lr1->color != lr2->color) &&
12409                                         (lr1->color != REG_UNSET) &&
12410                                         (lr2->color != REG_UNSET)) {
12411                                         continue;
12412                                 }
12413                                 if ((lr1->classes & lr2->classes) == 0) {
12414                                         continue;
12415                                 }
12416                                 
12417                                 if (interfere(rstate, lr1, lr2)) {
12418                                         continue;
12419                                 }
12420
12421                                 res = coalesce_ranges(state, rstate, lr1, lr2);
12422                                 coalesced += 1;
12423                                 if (res != lr1) {
12424                                         goto next;
12425                                 }
12426                         }
12427                 }
12428         next:
12429                 ;
12430         }
12431         return coalesced;
12432 }
12433
12434
12435 static void fix_coalesce_conflicts(struct compile_state *state,
12436         struct reg_block *blocks, struct triple_reg_set *live,
12437         struct reg_block *rb, struct triple *ins, void *arg)
12438 {
12439         int zlhs, zrhs, i, j;
12440
12441         /* See if we have a mandatory coalesce operation between
12442          * a lhs and a rhs value.  If so and the rhs value is also
12443          * alive then this triple needs to be pre copied.  Otherwise
12444          * we would have two definitions in the same live range simultaneously
12445          * alive.
12446          */
12447         zlhs = TRIPLE_LHS(ins->sizes);
12448         if ((zlhs == 0) && triple_is_def(state, ins)) {
12449                 zlhs = 1;
12450         }
12451         zrhs = TRIPLE_RHS(ins->sizes);
12452         for(i = 0; i < zlhs; i++) {
12453                 struct reg_info linfo;
12454                 linfo = arch_reg_lhs(state, ins, i);
12455                 if (linfo.reg < MAX_REGISTERS) {
12456                         continue;
12457                 }
12458                 for(j = 0; j < zrhs; j++) {
12459                         struct reg_info rinfo;
12460                         struct triple *rhs;
12461                         struct triple_reg_set *set;
12462                         int found;
12463                         found = 0;
12464                         rinfo = arch_reg_rhs(state, ins, j);
12465                         if (rinfo.reg != linfo.reg) {
12466                                 continue;
12467                         }
12468                         rhs = RHS(ins, j);
12469                         for(set = live; set && !found; set = set->next) {
12470                                 if (set->member == rhs) {
12471                                         found = 1;
12472                                 }
12473                         }
12474                         if (found) {
12475                                 struct triple *copy;
12476                                 copy = pre_copy(state, ins, j);
12477                                 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12478                         }
12479                 }
12480         }
12481         return;
12482 }
12483
12484 static void replace_set_use(struct compile_state *state,
12485         struct triple_reg_set *head, struct triple *orig, struct triple *new)
12486 {
12487         struct triple_reg_set *set;
12488         for(set = head; set; set = set->next) {
12489                 if (set->member == orig) {
12490                         set->member = new;
12491                 }
12492         }
12493 }
12494
12495 static void replace_block_use(struct compile_state *state, 
12496         struct reg_block *blocks, struct triple *orig, struct triple *new)
12497 {
12498         int i;
12499 #warning "WISHLIST visit just those blocks that need it *"
12500         for(i = 1; i <= state->last_vertex; i++) {
12501                 struct reg_block *rb;
12502                 rb = &blocks[i];
12503                 replace_set_use(state, rb->in, orig, new);
12504                 replace_set_use(state, rb->out, orig, new);
12505         }
12506 }
12507
12508 static void color_instructions(struct compile_state *state)
12509 {
12510         struct triple *ins, *first;
12511         first = RHS(state->main_function, 0);
12512         ins = first;
12513         do {
12514                 if (triple_is_def(state, ins)) {
12515                         struct reg_info info;
12516                         info = find_lhs_color(state, ins, 0);
12517                         if (info.reg >= MAX_REGISTERS) {
12518                                 info.reg = REG_UNSET;
12519                         }
12520                         SET_INFO(ins->id, info);
12521                 }
12522                 ins = ins->next;
12523         } while(ins != first);
12524 }
12525
12526 static struct reg_info read_lhs_color(
12527         struct compile_state *state, struct triple *ins, int index)
12528 {
12529         struct reg_info info;
12530         if ((index == 0) && triple_is_def(state, ins)) {
12531                 info.reg   = ID_REG(ins->id);
12532                 info.regcm = ID_REGCM(ins->id);
12533         }
12534         else if (index < TRIPLE_LHS(ins->sizes)) {
12535                 info = read_lhs_color(state, LHS(ins, index), 0);
12536         }
12537         else {
12538                 internal_error(state, ins, "Bad lhs %d", index);
12539                 info.reg = REG_UNSET;
12540                 info.regcm = 0;
12541         }
12542         return info;
12543 }
12544
12545 static struct triple *resolve_tangle(
12546         struct compile_state *state, struct triple *tangle)
12547 {
12548         struct reg_info info, uinfo;
12549         struct triple_set *set, *next;
12550         struct triple *copy;
12551
12552 #warning "WISHLIST recalculate all affected instructions colors"
12553         info = find_lhs_color(state, tangle, 0);
12554         for(set = tangle->use; set; set = next) {
12555                 struct triple *user;
12556                 int i, zrhs;
12557                 next = set->next;
12558                 user = set->member;
12559                 zrhs = TRIPLE_RHS(user->sizes);
12560                 for(i = 0; i < zrhs; i++) {
12561                         if (RHS(user, i) != tangle) {
12562                                 continue;
12563                         }
12564                         uinfo = find_rhs_post_color(state, user, i);
12565                         if (uinfo.reg == info.reg) {
12566                                 copy = pre_copy(state, user, i);
12567                                 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12568                                 SET_INFO(copy->id, uinfo);
12569                         }
12570                 }
12571         }
12572         copy = 0;
12573         uinfo = find_lhs_pre_color(state, tangle, 0);
12574         if (uinfo.reg == info.reg) {
12575                 struct reg_info linfo;
12576                 copy = post_copy(state, tangle);
12577                 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12578                 linfo = find_lhs_color(state, copy, 0);
12579                 SET_INFO(copy->id, linfo);
12580         }
12581         info = find_lhs_color(state, tangle, 0);
12582         SET_INFO(tangle->id, info);
12583         
12584         return copy;
12585 }
12586
12587
12588 static void fix_tangles(struct compile_state *state,
12589         struct reg_block *blocks, struct triple_reg_set *live,
12590         struct reg_block *rb, struct triple *ins, void *arg)
12591 {
12592         int *tangles = arg;
12593         struct triple *tangle;
12594         do {
12595                 char used[MAX_REGISTERS];
12596                 struct triple_reg_set *set;
12597                 tangle = 0;
12598
12599                 /* Find out which registers have multiple uses at this point */
12600                 memset(used, 0, sizeof(used));
12601                 for(set = live; set; set = set->next) {
12602                         struct reg_info info;
12603                         info = read_lhs_color(state, set->member, 0);
12604                         if (info.reg == REG_UNSET) {
12605                                 continue;
12606                         }
12607                         reg_inc_used(state, used, info.reg);
12608                 }
12609                 
12610                 /* Now find the least dominated definition of a register in
12611                  * conflict I have seen so far.
12612                  */
12613                 for(set = live; set; set = set->next) {
12614                         struct reg_info info;
12615                         info = read_lhs_color(state, set->member, 0);
12616                         if (used[info.reg] < 2) {
12617                                 continue;
12618                         }
12619                         /* Changing copies that feed into phi functions
12620                          * is incorrect.
12621                          */
12622                         if (set->member->use && 
12623                                 (set->member->use->member->op == OP_PHI)) {
12624                                 continue;
12625                         }
12626                         if (!tangle || tdominates(state, set->member, tangle)) {
12627                                 tangle = set->member;
12628                         }
12629                 }
12630                 /* If I have found a tangle resolve it */
12631                 if (tangle) {
12632                         struct triple *post_copy;
12633                         (*tangles)++;
12634                         post_copy = resolve_tangle(state, tangle);
12635                         if (post_copy) {
12636                                 replace_block_use(state, blocks, tangle, post_copy);
12637                         }
12638                         if (post_copy && (tangle != ins)) {
12639                                 replace_set_use(state, live, tangle, post_copy);
12640                         }
12641                 }
12642         } while(tangle);
12643         return;
12644 }
12645
12646 static int correct_tangles(
12647         struct compile_state *state, struct reg_block *blocks)
12648 {
12649         int tangles;
12650         tangles = 0;
12651         color_instructions(state);
12652         walk_variable_lifetimes(state, blocks, fix_tangles, &tangles);
12653         return tangles;
12654 }
12655
12656 struct least_conflict {
12657         struct reg_state *rstate;
12658         struct live_range *ref_range;
12659         struct triple *ins;
12660         struct triple_reg_set *live;
12661         size_t count;
12662         int constraints;
12663 };
12664 static void least_conflict(struct compile_state *state,
12665         struct reg_block *blocks, struct triple_reg_set *live,
12666         struct reg_block *rb, struct triple *ins, void *arg)
12667 {
12668         struct least_conflict *conflict = arg;
12669         struct live_range_edge *edge;
12670         struct triple_reg_set *set;
12671         size_t count;
12672         int constraints;
12673
12674 #warning "FIXME handle instructions with left hand sides..."
12675         /* Only instructions that introduce a new definition
12676          * can be the conflict instruction.
12677          */
12678         if (!triple_is_def(state, ins)) {
12679                 return;
12680         }
12681
12682         /* See if live ranges at this instruction are a
12683          * strict subset of the live ranges that are in conflict.
12684          */
12685         count = 0;
12686         for(set = live; set; set = set->next) {
12687                 struct live_range *lr;
12688                 lr = conflict->rstate->lrd[set->member->id].lr;
12689                 /* Ignore it if there cannot be an edge between these two nodes */
12690                 if (!arch_regcm_intersect(conflict->ref_range->classes, lr->classes)) {
12691                         continue;
12692                 }
12693                 for(edge = conflict->ref_range->edges; edge; edge = edge->next) {
12694                         if (edge->node == lr) {
12695                                 break;
12696                         }
12697                 }
12698                 if (!edge && (lr != conflict->ref_range)) {
12699                         return;
12700                 }
12701                 count++;
12702         }
12703         if (count <= 1) {
12704                 return;
12705         }
12706
12707 #if 0
12708         /* See if there is an uncolored member in this subset. 
12709          */
12710          for(set = live; set; set = set->next) {
12711                 struct live_range *lr;
12712                 lr = conflict->rstate->lrd[set->member->id].lr;
12713                 if (lr->color == REG_UNSET) {
12714                         break;
12715                 }
12716         }
12717         if (!set && (conflict->ref_range != REG_UNSET)) {
12718                 return;
12719         }
12720 #endif
12721
12722         /* See if any of the live registers are constrained,
12723          * if not it won't be productive to pick this as
12724          * a conflict instruction.
12725          */
12726         constraints = 0;
12727         for(set = live; set; set = set->next) {
12728                 struct triple_set *uset;
12729                 struct reg_info info;
12730                 unsigned classes;
12731                 unsigned cur_size, size;
12732                 /* Skip this instruction */
12733                 if (set->member == ins) {
12734                         continue;
12735                 }
12736                 /* Find how many registers this value can potentially 
12737                  * be assigned to.
12738                  */
12739                 classes = arch_type_to_regcm(state, set->member->type);
12740                 size = regc_max_size(state, classes);
12741                 
12742                 /* Find how many registers we allow this value to
12743                  * be assigned to.
12744                  */
12745                 info = arch_reg_lhs(state, set->member, 0);
12746                 
12747                 /* If the value does not live in a register it
12748                  * isn't constrained.
12749                  */
12750                 if (info.reg == REG_UNNEEDED) {
12751                         continue;
12752                 }
12753                 
12754                 if ((info.reg == REG_UNSET) || (info.reg >= MAX_REGISTERS)) {
12755                         cur_size = regc_max_size(state, info.regcm);
12756                 } else {
12757                         cur_size = 1;
12758                 }
12759
12760                 /* If there is no difference between potential and
12761                  * actual register count there is not a constraint
12762                  */
12763                 if (cur_size >= size) {
12764                         continue;
12765                 }
12766                 
12767                 /* If this live_range feeds into conflict->inds
12768                  * it isn't a constraint we can relieve.
12769                  */
12770                 for(uset = set->member->use; uset; uset = uset->next) {
12771                         if (uset->member == ins) {
12772                                 break;
12773                         }
12774                 }
12775                 if (uset) {
12776                         continue;
12777                 }
12778                 constraints = 1;
12779                 break;
12780         }
12781         /* Don't drop canidates with constraints */
12782         if (conflict->constraints && !constraints) {
12783                 return;
12784         }
12785
12786
12787 #if 0
12788         fprintf(stderr, "conflict ins? %p %s count: %d constraints: %d\n",
12789                 ins, tops(ins->op), count, constraints);
12790 #endif
12791         /* Find the instruction with the largest possible subset of
12792          * conflict ranges and that dominates any other instruction
12793          * with an equal sized set of conflicting ranges.
12794          */
12795         if ((count > conflict->count) ||
12796                 ((count == conflict->count) &&
12797                         tdominates(state, ins, conflict->ins))) {
12798                 struct triple_reg_set *next;
12799                 /* Remember the canidate instruction */
12800                 conflict->ins = ins;
12801                 conflict->count = count;
12802                 conflict->constraints = constraints;
12803                 /* Free the old collection of live registers */
12804                 for(set = conflict->live; set; set = next) {
12805                         next = set->next;
12806                         do_triple_unset(&conflict->live, set->member);
12807                 }
12808                 conflict->live = 0;
12809                 /* Rember the registers that are alive but do not feed
12810                  * into or out of conflict->ins.
12811                  */
12812                 for(set = live; set; set = set->next) {
12813                         struct triple **expr;
12814                         if (set->member == ins) {
12815                                 goto next;
12816                         }
12817                         expr = triple_rhs(state, ins, 0);
12818                         for(;expr; expr = triple_rhs(state, ins, expr)) {
12819                                 if (*expr == set->member) {
12820                                         goto next;
12821                                 }
12822                         }
12823                         expr = triple_lhs(state, ins, 0);
12824                         for(; expr; expr = triple_lhs(state, ins, expr)) {
12825                                 if (*expr == set->member) {
12826                                         goto next;
12827                                 }
12828                         }
12829                         do_triple_set(&conflict->live, set->member, set->new);
12830                 next:
12831                         ;
12832                 }
12833         }
12834         return;
12835 }
12836
12837 static void find_range_conflict(struct compile_state *state,
12838         struct reg_state *rstate, char *used, struct live_range *ref_range,
12839         struct least_conflict *conflict)
12840 {
12841
12842         /* there are 3 kinds ways conflicts can occure.
12843          * 1) the life time of 2 values simply overlap.
12844          * 2) the 2 values feed into the same instruction.
12845          * 3) the 2 values feed into a phi function.
12846          */
12847
12848         /* find the instruction where the problematic conflict comes
12849          * into existance.  that the instruction where all of
12850          * the values are alive, and among such instructions it is
12851          * the least dominated one.
12852          *
12853          * a value is alive an an instruction if either;
12854          * 1) the value defintion dominates the instruction and there
12855          *    is a use at or after that instrction
12856          * 2) the value definition feeds into a phi function in the
12857          *    same block as the instruction.  and the phi function
12858          *    is at or after the instruction.
12859          */
12860         memset(conflict, 0, sizeof(*conflict));
12861         conflict->rstate      = rstate;
12862         conflict->ref_range   = ref_range;
12863         conflict->ins         = 0;
12864         conflict->live        = 0;
12865         conflict->count       = 0;
12866         conflict->constraints = 0;
12867         walk_variable_lifetimes(state, rstate->blocks, least_conflict, conflict);
12868
12869         if (!conflict->ins) {
12870                 internal_error(state, ref_range->defs->def, "No conflict ins?");
12871         }
12872         if (!conflict->live) {
12873                 internal_error(state, ref_range->defs->def, "No conflict live?");
12874         }
12875 #if 0
12876         fprintf(stderr, "conflict ins: %p %s count: %d constraints: %d\n", 
12877                 conflict->ins, tops(conflict->ins->op),
12878                 conflict->count, conflict->constraints);
12879 #endif
12880         return;
12881 }
12882
12883 static struct triple *split_constrained_range(struct compile_state *state, 
12884         struct reg_state *rstate, char *used, struct least_conflict *conflict)
12885 {
12886         unsigned constrained_size;
12887         struct triple *new, *constrained;
12888         struct triple_reg_set *cset;
12889         /* Find a range that is having problems because it is
12890          * artificially constrained.
12891          */
12892         constrained_size = ~0;
12893         constrained = 0;
12894         new = 0;
12895         for(cset = conflict->live; cset; cset = cset->next) {
12896                 struct triple_set *set;
12897                 struct reg_info info;
12898                 unsigned classes;
12899                 unsigned cur_size, size;
12900                 /* Skip the live range that starts with conflict->ins */
12901                 if (cset->member == conflict->ins) {
12902                         continue;
12903                 }
12904                 /* Find how many registers this value can potentially
12905                  * be assigned to.
12906                  */
12907                 classes = arch_type_to_regcm(state, cset->member->type);
12908                 size = regc_max_size(state, classes);
12909
12910                 /* Find how many registers we allow this value to
12911                  * be assigned to.
12912                  */
12913                 info = arch_reg_lhs(state, cset->member, 0);
12914
12915                 /* If the register doesn't need a register 
12916                  * splitting it can't help.
12917                  */
12918                 if (info.reg == REG_UNNEEDED) {
12919                         continue;
12920                 }
12921 #warning "FIXME do I need a call to arch_reg_rhs around here somewhere?"
12922                 if ((info.reg == REG_UNSET) || (info.reg >= MAX_REGISTERS)) {
12923                         cur_size = regc_max_size(state, info.regcm);
12924                 } else {
12925                         cur_size = 1;
12926                 }
12927                 /* If this live_range feeds into conflict->ins
12928                  * splitting it is unlikely to help.
12929                  */
12930                 for(set = cset->member->use; set; set = set->next) {
12931                         if (set->member == conflict->ins) {
12932                                 goto next;
12933                         }
12934                 }
12935
12936                 /* If there is no difference between potential and
12937                  * actual register count there is nothing to do.
12938                  */
12939                 if (cur_size >= size) {
12940                         continue;
12941                 }
12942                 /* Of the constrained registers deal with the
12943                  * most constrained one first.
12944                  */
12945                 if (!constrained ||
12946                         (size < constrained_size)) {
12947                         constrained = cset->member;
12948                         constrained_size = size;
12949                 }
12950         next:
12951                 ;
12952         }
12953         if (constrained) {
12954                 new = post_copy(state, constrained);
12955                 new->id |= TRIPLE_FLAG_POST_SPLIT;
12956         }
12957         return new;
12958 }
12959
12960 static int split_ranges(
12961         struct compile_state *state, struct reg_state *rstate, 
12962         char *used, struct live_range *range)
12963 {
12964         struct triple *new;
12965
12966 #if 0
12967         fprintf(stderr, "split_ranges %d %s %p\n", 
12968                 rstate->passes, tops(range->defs->def->op), range->defs->def);
12969 #endif
12970         if ((range->color == REG_UNNEEDED) ||
12971                 (rstate->passes >= rstate->max_passes)) {
12972                 return 0;
12973         }
12974         new = 0;
12975         /* If I can't allocate a register something needs to be split */
12976         if (arch_select_free_register(state, used, range->classes) == REG_UNSET) {
12977                 struct least_conflict conflict;
12978
12979 #if 0
12980         fprintf(stderr, "find_range_conflict\n");
12981 #endif
12982                 /* Find where in the set of registers the conflict
12983                  * actually occurs.
12984                  */
12985                 find_range_conflict(state, rstate, used, range, &conflict);
12986
12987                 /* If a range has been artifically constrained split it */
12988                 new = split_constrained_range(state, rstate, used, &conflict);
12989                 
12990                 if (!new) {
12991                 /* Ideally I would split the live range that will not be used
12992                  * for the longest period of time in hopes that this will 
12993                  * (a) allow me to spill a register or
12994                  * (b) allow me to place a value in another register.
12995                  *
12996                  * So far I don't have a test case for this, the resolving
12997                  * of mandatory constraints has solved all of my
12998                  * know issues.  So I have choosen not to write any
12999                  * code until I cat get a better feel for cases where
13000                  * it would be useful to have.
13001                  *
13002                  */
13003 #warning "WISHLIST implement live range splitting..."
13004 #if 0
13005                         print_blocks(state, stderr);
13006                         print_dominators(state, stderr);
13007
13008 #endif
13009                         return 0;
13010                 }
13011         }
13012         if (new) {
13013                 rstate->lrd[rstate->defs].orig_id = new->id;
13014                 new->id = rstate->defs;
13015                 rstate->defs++;
13016 #if 0
13017                 fprintf(stderr, "new: %p old: %s %p\n", 
13018                         new, tops(RHS(new, 0)->op), RHS(new, 0));
13019 #endif
13020 #if 0
13021                 print_blocks(state, stderr);
13022                 print_dominators(state, stderr);
13023
13024 #endif
13025                 return 1;
13026         }
13027         return 0;
13028 }
13029
13030 #if DEBUG_COLOR_GRAPH > 1
13031 #define cgdebug_printf(...) fprintf(stdout, __VA_ARGS__)
13032 #define cgdebug_flush() fflush(stdout)
13033 #elif DEBUG_COLOR_GRAPH == 1
13034 #define cgdebug_printf(...) fprintf(stderr, __VA_ARGS__)
13035 #define cgdebug_flush() fflush(stderr)
13036 #else
13037 #define cgdebug_printf(...)
13038 #define cgdebug_flush()
13039 #endif
13040
13041         
13042 static int select_free_color(struct compile_state *state, 
13043         struct reg_state *rstate, struct live_range *range)
13044 {
13045         struct triple_set *entry;
13046         struct live_range_def *lrd;
13047         struct live_range_def *phi;
13048         struct live_range_edge *edge;
13049         char used[MAX_REGISTERS];
13050         struct triple **expr;
13051
13052         /* Instead of doing just the trivial color select here I try
13053          * a few extra things because a good color selection will help reduce
13054          * copies.
13055          */
13056
13057         /* Find the registers currently in use */
13058         memset(used, 0, sizeof(used));
13059         for(edge = range->edges; edge; edge = edge->next) {
13060                 if (edge->node->color == REG_UNSET) {
13061                         continue;
13062                 }
13063                 reg_fill_used(state, used, edge->node->color);
13064         }
13065 #if DEBUG_COLOR_GRAPH > 1
13066         {
13067                 int i;
13068                 i = 0;
13069                 for(edge = range->edges; edge; edge = edge->next) {
13070                         i++;
13071                 }
13072                 cgdebug_printf("\n%s edges: %d @%s:%d.%d\n", 
13073                         tops(range->def->op), i, 
13074                         range->def->filename, range->def->line, range->def->col);
13075                 for(i = 0; i < MAX_REGISTERS; i++) {
13076                         if (used[i]) {
13077                                 cgdebug_printf("used: %s\n",
13078                                         arch_reg_str(i));
13079                         }
13080                 }
13081         }       
13082 #endif
13083
13084 #warning "FIXME detect conflicts caused by the source and destination being the same register"
13085
13086         /* If a color is already assigned see if it will work */
13087         if (range->color != REG_UNSET) {
13088                 struct live_range_def *lrd;
13089                 if (!used[range->color]) {
13090                         return 1;
13091                 }
13092                 for(edge = range->edges; edge; edge = edge->next) {
13093                         if (edge->node->color != range->color) {
13094                                 continue;
13095                         }
13096                         warning(state, edge->node->defs->def, "edge: ");
13097                         lrd = edge->node->defs;
13098                         do {
13099                                 warning(state, lrd->def, " %p %s",
13100                                         lrd->def, tops(lrd->def->op));
13101                                 lrd = lrd->next;
13102                         } while(lrd != edge->node->defs);
13103                 }
13104                 lrd = range->defs;
13105                 warning(state, range->defs->def, "def: ");
13106                 do {
13107                         warning(state, lrd->def, " %p %s",
13108                                 lrd->def, tops(lrd->def->op));
13109                         lrd = lrd->next;
13110                 } while(lrd != range->defs);
13111                 internal_error(state, range->defs->def,
13112                         "live range with already used color %s",
13113                         arch_reg_str(range->color));
13114         }
13115
13116         /* If I feed into an expression reuse it's color.
13117          * This should help remove copies in the case of 2 register instructions
13118          * and phi functions.
13119          */
13120         phi = 0;
13121         lrd = live_range_end(state, range, 0);
13122         for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
13123                 entry = lrd->def->use;
13124                 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
13125                         struct live_range_def *insd;
13126                         insd = &rstate->lrd[entry->member->id];
13127                         if (insd->lr->defs == 0) {
13128                                 continue;
13129                         }
13130                         if (!phi && (insd->def->op == OP_PHI) &&
13131                                 !interfere(rstate, range, insd->lr)) {
13132                                 phi = insd;
13133                         }
13134                         if ((insd->lr->color == REG_UNSET) ||
13135                                 ((insd->lr->classes & range->classes) == 0) ||
13136                                 (used[insd->lr->color])) {
13137                                 continue;
13138                         }
13139                         if (interfere(rstate, range, insd->lr)) {
13140                                 continue;
13141                         }
13142                         range->color = insd->lr->color;
13143                 }
13144         }
13145         /* If I feed into a phi function reuse it's color or the color
13146          * of something else that feeds into the phi function.
13147          */
13148         if (phi) {
13149                 if (phi->lr->color != REG_UNSET) {
13150                         if (used[phi->lr->color]) {
13151                                 range->color = phi->lr->color;
13152                         }
13153                 }
13154                 else {
13155                         expr = triple_rhs(state, phi->def, 0);
13156                         for(; expr; expr = triple_rhs(state, phi->def, expr)) {
13157                                 struct live_range *lr;
13158                                 if (!*expr) {
13159                                         continue;
13160                                 }
13161                                 lr = rstate->lrd[(*expr)->id].lr;
13162                                 if ((lr->color == REG_UNSET) || 
13163                                         ((lr->classes & range->classes) == 0) ||
13164                                         (used[lr->color])) {
13165                                         continue;
13166                                 }
13167                                 if (interfere(rstate, range, lr)) {
13168                                         continue;
13169                                 }
13170                                 range->color = lr->color;
13171                         }
13172                 }
13173         }
13174         /* If I don't interfere with a rhs node reuse it's color */
13175         lrd = live_range_head(state, range, 0);
13176         for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
13177                 expr = triple_rhs(state, lrd->def, 0);
13178                 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
13179                         struct live_range *lr;
13180                         if (!*expr) {
13181                                 continue;
13182                         }
13183                         lr = rstate->lrd[(*expr)->id].lr;
13184                         if ((lr->color == -1) || 
13185                                 ((lr->classes & range->classes) == 0) ||
13186                                 (used[lr->color])) {
13187                                 continue;
13188                         }
13189                         if (interfere(rstate, range, lr)) {
13190                                 continue;
13191                         }
13192                         range->color = lr->color;
13193                         break;
13194                 }
13195         }
13196         /* If I have not opportunitically picked a useful color
13197          * pick the first color that is free.
13198          */
13199         if (range->color == REG_UNSET) {
13200                 range->color = 
13201                         arch_select_free_register(state, used, range->classes);
13202         }
13203         if (range->color == REG_UNSET) {
13204                 struct live_range_def *lrd;
13205                 int i;
13206                 if (split_ranges(state, rstate, used, range)) {
13207                         return 0;
13208                 }
13209                 for(edge = range->edges; edge; edge = edge->next) {
13210                         warning(state, edge->node->defs->def, "edge reg %s",
13211                                 arch_reg_str(edge->node->color));
13212                         lrd = edge->node->defs;
13213                         do {
13214                                 warning(state, lrd->def, " %s",
13215                                         tops(lrd->def->op));
13216                                 lrd = lrd->next;
13217                         } while(lrd != edge->node->defs);
13218                 }
13219                 warning(state, range->defs->def, "range: ");
13220                 lrd = range->defs;
13221                 do {
13222                         warning(state, lrd->def, " %s",
13223                                 tops(lrd->def->op));
13224                         lrd = lrd->next;
13225                 } while(lrd != range->defs);
13226                         
13227                 warning(state, range->defs->def, "classes: %x",
13228                         range->classes);
13229                 for(i = 0; i < MAX_REGISTERS; i++) {
13230                         if (used[i]) {
13231                                 warning(state, range->defs->def, "used: %s",
13232                                         arch_reg_str(i));
13233                         }
13234                 }
13235 #if DEBUG_COLOR_GRAPH < 2
13236                 error(state, range->defs->def, "too few registers");
13237 #else
13238                 internal_error(state, range->defs->def, "too few registers");
13239 #endif
13240         }
13241         range->classes = arch_reg_regcm(state, range->color);
13242         if (range->color == -1) {
13243                 internal_error(state, range->defs->def, "select_free_color did not?");
13244         }
13245         return 1;
13246 }
13247
13248 static int color_graph(struct compile_state *state, struct reg_state *rstate)
13249 {
13250         int colored;
13251         struct live_range_edge *edge;
13252         struct live_range *range;
13253         if (rstate->low) {
13254                 cgdebug_printf("Lo: ");
13255                 range = rstate->low;
13256                 if (*range->group_prev != range) {
13257                         internal_error(state, 0, "lo: *prev != range?");
13258                 }
13259                 *range->group_prev = range->group_next;
13260                 if (range->group_next) {
13261                         range->group_next->group_prev = range->group_prev;
13262                 }
13263                 if (&range->group_next == rstate->low_tail) {
13264                         rstate->low_tail = range->group_prev;
13265                 }
13266                 if (rstate->low == range) {
13267                         internal_error(state, 0, "low: next != prev?");
13268                 }
13269         }
13270         else if (rstate->high) {
13271                 cgdebug_printf("Hi: ");
13272                 range = rstate->high;
13273                 if (*range->group_prev != range) {
13274                         internal_error(state, 0, "hi: *prev != range?");
13275                 }
13276                 *range->group_prev = range->group_next;
13277                 if (range->group_next) {
13278                         range->group_next->group_prev = range->group_prev;
13279                 }
13280                 if (&range->group_next == rstate->high_tail) {
13281                         rstate->high_tail = range->group_prev;
13282                 }
13283                 if (rstate->high == range) {
13284                         internal_error(state, 0, "high: next != prev?");
13285                 }
13286         }
13287         else {
13288                 return 1;
13289         }
13290         cgdebug_printf(" %d\n", range - rstate->lr);
13291         range->group_prev = 0;
13292         for(edge = range->edges; edge; edge = edge->next) {
13293                 struct live_range *node;
13294                 node = edge->node;
13295                 /* Move nodes from the high to the low list */
13296                 if (node->group_prev && (node->color == REG_UNSET) &&
13297                         (node->degree == regc_max_size(state, node->classes))) {
13298                         if (*node->group_prev != node) {
13299                                 internal_error(state, 0, "move: *prev != node?");
13300                         }
13301                         *node->group_prev = node->group_next;
13302                         if (node->group_next) {
13303                                 node->group_next->group_prev = node->group_prev;
13304                         }
13305                         if (&node->group_next == rstate->high_tail) {
13306                                 rstate->high_tail = node->group_prev;
13307                         }
13308                         cgdebug_printf("Moving...%d to low\n", node - rstate->lr);
13309                         node->group_prev  = rstate->low_tail;
13310                         node->group_next  = 0;
13311                         *rstate->low_tail = node;
13312                         rstate->low_tail  = &node->group_next;
13313                         if (*node->group_prev != node) {
13314                                 internal_error(state, 0, "move2: *prev != node?");
13315                         }
13316                 }
13317                 node->degree -= 1;
13318         }
13319         colored = color_graph(state, rstate);
13320         if (colored) {
13321                 cgdebug_printf("Coloring %d @%s:%d.%d:", 
13322                         range - rstate->lr,
13323                         range->def->filename, range->def->line, range->def->col);
13324                 cgdebug_flush();
13325                 colored = select_free_color(state, rstate, range);
13326                 cgdebug_printf(" %s\n", arch_reg_str(range->color));
13327         }
13328         return colored;
13329 }
13330
13331 static void verify_colors(struct compile_state *state, struct reg_state *rstate)
13332 {
13333         struct live_range *lr;
13334         struct live_range_edge *edge;
13335         struct triple *ins, *first;
13336         char used[MAX_REGISTERS];
13337         first = RHS(state->main_function, 0);
13338         ins = first;
13339         do {
13340                 if (triple_is_def(state, ins)) {
13341                         if ((ins->id < 0) || (ins->id > rstate->defs)) {
13342                                 internal_error(state, ins, 
13343                                         "triple without a live range def");
13344                         }
13345                         lr = rstate->lrd[ins->id].lr;
13346                         if (lr->color == REG_UNSET) {
13347                                 internal_error(state, ins,
13348                                         "triple without a color");
13349                         }
13350                         /* Find the registers used by the edges */
13351                         memset(used, 0, sizeof(used));
13352                         for(edge = lr->edges; edge; edge = edge->next) {
13353                                 if (edge->node->color == REG_UNSET) {
13354                                         internal_error(state, 0,
13355                                                 "live range without a color");
13356                         }
13357                                 reg_fill_used(state, used, edge->node->color);
13358                         }
13359                         if (used[lr->color]) {
13360                                 internal_error(state, ins,
13361                                         "triple with already used color");
13362                         }
13363                 }
13364                 ins = ins->next;
13365         } while(ins != first);
13366 }
13367
13368 static void color_triples(struct compile_state *state, struct reg_state *rstate)
13369 {
13370         struct live_range *lr;
13371         struct triple *first, *ins;
13372         first = RHS(state->main_function, 0);
13373         ins = first;
13374         do {
13375                 if ((ins->id < 0) || (ins->id > rstate->defs)) {
13376                         internal_error(state, ins, 
13377                                 "triple without a live range");
13378                 }
13379                 lr = rstate->lrd[ins->id].lr;
13380                 SET_REG(ins->id, lr->color);
13381                 ins = ins->next;
13382         } while (ins != first);
13383 }
13384
13385 static void print_interference_block(
13386         struct compile_state *state, struct block *block, void *arg)
13387
13388 {
13389         struct reg_state *rstate = arg;
13390         struct reg_block *rb;
13391         struct triple *ptr;
13392         int phi_present;
13393         int done;
13394         rb = &rstate->blocks[block->vertex];
13395
13396         printf("\nblock: %p (%d), %p<-%p %p<-%p\n", 
13397                 block, 
13398                 block->vertex,
13399                 block->left, 
13400                 block->left && block->left->use?block->left->use->member : 0,
13401                 block->right, 
13402                 block->right && block->right->use?block->right->use->member : 0);
13403         if (rb->in) {
13404                 struct triple_reg_set *in_set;
13405                 printf("        in:");
13406                 for(in_set = rb->in; in_set; in_set = in_set->next) {
13407                         printf(" %-10p", in_set->member);
13408                 }
13409                 printf("\n");
13410         }
13411         phi_present = 0;
13412         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13413                 done = (ptr == block->last);
13414                 if (ptr->op == OP_PHI) {
13415                         phi_present = 1;
13416                         break;
13417                 }
13418         }
13419         if (phi_present) {
13420                 int edge;
13421                 for(edge = 0; edge < block->users; edge++) {
13422                         printf("     in(%d):", edge);
13423                         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13424                                 struct triple **slot;
13425                                 done = (ptr == block->last);
13426                                 if (ptr->op != OP_PHI) {
13427                                         continue;
13428                                 }
13429                                 slot = &RHS(ptr, 0);
13430                                 printf(" %-10p", slot[edge]);
13431                         }
13432                         printf("\n");
13433                 }
13434         }
13435         if (block->first->op == OP_LABEL) {
13436                 printf("%p:\n", block->first);
13437         }
13438         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13439                 struct triple_set *user;
13440                 struct live_range *lr;
13441                 unsigned id;
13442                 int op;
13443                 op = ptr->op;
13444                 done = (ptr == block->last);
13445                 lr = rstate->lrd[ptr->id].lr;
13446                 
13447                 if (triple_stores_block(state, ptr)) {
13448                         if (ptr->u.block != block) {
13449                                 internal_error(state, ptr, 
13450                                         "Wrong block pointer: %p",
13451                                         ptr->u.block);
13452                         }
13453                 }
13454                 if (op == OP_ADECL) {
13455                         for(user = ptr->use; user; user = user->next) {
13456                                 if (!user->member->u.block) {
13457                                         internal_error(state, user->member, 
13458                                                 "Use %p not in a block?",
13459                                                 user->member);
13460                                 }
13461                                 
13462                         }
13463                 }
13464                 id = ptr->id;
13465                 ptr->id = rstate->lrd[id].orig_id;
13466                 SET_REG(ptr->id, lr->color);
13467                 display_triple(stdout, ptr);
13468                 ptr->id = id;
13469
13470                 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
13471                         internal_error(state, ptr, "lr has no defs!");
13472                 }
13473
13474                 if (lr->defs) {
13475                         struct live_range_def *lrd;
13476                         printf("       range:");
13477                         lrd = lr->defs;
13478                         do {
13479                                 printf(" %-10p", lrd->def);
13480                                 lrd = lrd->next;
13481                         } while(lrd != lr->defs);
13482                         printf("\n");
13483                 }
13484                 if (lr->edges > 0) {
13485                         struct live_range_edge *edge;
13486                         printf("       edges:");
13487                         for(edge = lr->edges; edge; edge = edge->next) {
13488                                 struct live_range_def *lrd;
13489                                 lrd = edge->node->defs;
13490                                 do {
13491                                         printf(" %-10p", lrd->def);
13492                                         lrd = lrd->next;
13493                                 } while(lrd != edge->node->defs);
13494                                 printf("|");
13495                         }
13496                         printf("\n");
13497                 }
13498                 /* Do a bunch of sanity checks */
13499                 valid_ins(state, ptr);
13500                 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
13501                         internal_error(state, ptr, "Invalid triple id: %d",
13502                                 ptr->id);
13503                 }
13504                 for(user = ptr->use; user; user = user->next) {
13505                         struct triple *use;
13506                         struct live_range *ulr;
13507                         use = user->member;
13508                         valid_ins(state, use);
13509                         if ((use->id < 0) || (use->id > rstate->defs)) {
13510                                 internal_error(state, use, "Invalid triple id: %d",
13511                                         use->id);
13512                         }
13513                         ulr = rstate->lrd[user->member->id].lr;
13514                         if (triple_stores_block(state, user->member) &&
13515                                 !user->member->u.block) {
13516                                 internal_error(state, user->member,
13517                                         "Use %p not in a block?",
13518                                         user->member);
13519                         }
13520                 }
13521         }
13522         if (rb->out) {
13523                 struct triple_reg_set *out_set;
13524                 printf("       out:");
13525                 for(out_set = rb->out; out_set; out_set = out_set->next) {
13526                         printf(" %-10p", out_set->member);
13527                 }
13528                 printf("\n");
13529         }
13530         printf("\n");
13531 }
13532
13533 static struct live_range *merge_sort_lr(
13534         struct live_range *first, struct live_range *last)
13535 {
13536         struct live_range *mid, *join, **join_tail, *pick;
13537         size_t size;
13538         size = (last - first) + 1;
13539         if (size >= 2) {
13540                 mid = first + size/2;
13541                 first = merge_sort_lr(first, mid -1);
13542                 mid   = merge_sort_lr(mid, last);
13543                 
13544                 join = 0;
13545                 join_tail = &join;
13546                 /* merge the two lists */
13547                 while(first && mid) {
13548                         if ((first->degree < mid->degree) ||
13549                                 ((first->degree == mid->degree) &&
13550                                         (first->length < mid->length))) {
13551                                 pick = first;
13552                                 first = first->group_next;
13553                                 if (first) {
13554                                         first->group_prev = 0;
13555                                 }
13556                         }
13557                         else {
13558                                 pick = mid;
13559                                 mid = mid->group_next;
13560                                 if (mid) {
13561                                         mid->group_prev = 0;
13562                                 }
13563                         }
13564                         pick->group_next = 0;
13565                         pick->group_prev = join_tail;
13566                         *join_tail = pick;
13567                         join_tail = &pick->group_next;
13568                 }
13569                 /* Splice the remaining list */
13570                 pick = (first)? first : mid;
13571                 *join_tail = pick;
13572                 if (pick) { 
13573                         pick->group_prev = join_tail;
13574                 }
13575         }
13576         else {
13577                 if (!first->defs) {
13578                         first = 0;
13579                 }
13580                 join = first;
13581         }
13582         return join;
13583 }
13584
13585 static void ids_from_rstate(struct compile_state *state, 
13586         struct reg_state *rstate)
13587 {
13588         struct triple *ins, *first;
13589         if (!rstate->defs) {
13590                 return;
13591         }
13592         /* Display the graph if desired */
13593         if (state->debug & DEBUG_INTERFERENCE) {
13594                 print_blocks(state, stdout);
13595                 print_control_flow(state);
13596         }
13597         first = RHS(state->main_function, 0);
13598         ins = first;
13599         do {
13600                 if (ins->id) {
13601                         struct live_range_def *lrd;
13602                         lrd = &rstate->lrd[ins->id];
13603                         ins->id = lrd->orig_id;
13604                 }
13605                 ins = ins->next;
13606         } while(ins != first);
13607 }
13608
13609 static void cleanup_live_edges(struct reg_state *rstate)
13610 {
13611         int i;
13612         /* Free the edges on each node */
13613         for(i = 1; i <= rstate->ranges; i++) {
13614                 remove_live_edges(rstate, &rstate->lr[i]);
13615         }
13616 }
13617
13618 static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
13619 {
13620         cleanup_live_edges(rstate);
13621         xfree(rstate->lrd);
13622         xfree(rstate->lr);
13623
13624         /* Free the variable lifetime information */
13625         if (rstate->blocks) {
13626                 free_variable_lifetimes(state, rstate->blocks);
13627         }
13628         rstate->defs = 0;
13629         rstate->ranges = 0;
13630         rstate->lrd = 0;
13631         rstate->lr = 0;
13632         rstate->blocks = 0;
13633 }
13634
13635 static void verify_consistency(struct compile_state *state);
13636 static void allocate_registers(struct compile_state *state)
13637 {
13638         struct reg_state rstate;
13639         int colored;
13640
13641         /* Clear out the reg_state */
13642         memset(&rstate, 0, sizeof(rstate));
13643         rstate.max_passes = MAX_ALLOCATION_PASSES;
13644
13645         do {
13646                 struct live_range **point, **next;
13647                 int tangles;
13648                 int coalesced;
13649
13650 #if 0
13651                 fprintf(stderr, "pass: %d\n", rstate.passes);
13652 #endif
13653
13654                 /* Restore ids */
13655                 ids_from_rstate(state, &rstate);
13656
13657                 /* Cleanup the temporary data structures */
13658                 cleanup_rstate(state, &rstate);
13659
13660                 /* Compute the variable lifetimes */
13661                 rstate.blocks = compute_variable_lifetimes(state);
13662
13663                 /* Fix invalid mandatory live range coalesce conflicts */
13664                 walk_variable_lifetimes(
13665                         state, rstate.blocks, fix_coalesce_conflicts, 0);
13666
13667                 /* Fix two simultaneous uses of the same register.
13668                  * In a few pathlogical cases a partial untangle moves
13669                  * the tangle to a part of the graph we won't revisit.
13670                  * So we keep looping until we have no more tangle fixes
13671                  * to apply.
13672                  */
13673                 do {
13674                         tangles = correct_tangles(state, rstate.blocks);
13675                 } while(tangles);
13676
13677                 if (state->debug & DEBUG_INSERTED_COPIES) {
13678                         printf("After resolve_tangles\n");
13679                         print_blocks(state, stdout);
13680                         print_control_flow(state);
13681                 }
13682                 verify_consistency(state);
13683                 
13684                 /* Allocate and initialize the live ranges */
13685                 initialize_live_ranges(state, &rstate);
13686
13687                 /* Note current doing coalescing in a loop appears to 
13688                  * buys me nothing.  The code is left this way in case
13689                  * there is some value in it.  Or if a future bugfix
13690                  *  yields some benefit.
13691                  */
13692                 do {
13693 #if 0
13694                         fprintf(stderr, "coalescing\n");
13695 #endif                  
13696                         /* Remove any previous live edge calculations */
13697                         cleanup_live_edges(&rstate);
13698
13699                         /* Compute the interference graph */
13700                         walk_variable_lifetimes(
13701                                 state, rstate.blocks, graph_ins, &rstate);
13702                         
13703                         /* Display the interference graph if desired */
13704                         if (state->debug & DEBUG_INTERFERENCE) {
13705                                 printf("\nlive variables by block\n");
13706                                 walk_blocks(state, print_interference_block, &rstate);
13707                                 printf("\nlive variables by instruction\n");
13708                                 walk_variable_lifetimes(
13709                                         state, rstate.blocks, 
13710                                         print_interference_ins, &rstate);
13711                         }
13712                         
13713                         coalesced = coalesce_live_ranges(state, &rstate);
13714
13715 #if 0
13716                         fprintf(stderr, "coalesced: %d\n", coalesced);
13717 #endif
13718                 } while(coalesced);
13719
13720 #if DEBUG_CONSISTENCY > 1
13721 # if 0
13722                 fprintf(stderr, "verify_graph_ins...\n");
13723 # endif
13724                 /* Verify the interference graph */
13725                 walk_variable_lifetimes(
13726                         state, rstate.blocks, verify_graph_ins, &rstate);
13727 # if 0
13728                 fprintf(stderr, "verify_graph_ins done\n");
13729 #endif
13730 #endif
13731                         
13732                 /* Build the groups low and high.  But with the nodes
13733                  * first sorted by degree order.
13734                  */
13735                 rstate.low_tail  = &rstate.low;
13736                 rstate.high_tail = &rstate.high;
13737                 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
13738                 if (rstate.high) {
13739                         rstate.high->group_prev = &rstate.high;
13740                 }
13741                 for(point = &rstate.high; *point; point = &(*point)->group_next)
13742                         ;
13743                 rstate.high_tail = point;
13744                 /* Walk through the high list and move everything that needs
13745                  * to be onto low.
13746                  */
13747                 for(point = &rstate.high; *point; point = next) {
13748                         struct live_range *range;
13749                         next = &(*point)->group_next;
13750                         range = *point;
13751                         
13752                         /* If it has a low degree or it already has a color
13753                          * place the node in low.
13754                          */
13755                         if ((range->degree < regc_max_size(state, range->classes)) ||
13756                                 (range->color != REG_UNSET)) {
13757                                 cgdebug_printf("Lo: %5d degree %5d%s\n", 
13758                                         range - rstate.lr, range->degree,
13759                                         (range->color != REG_UNSET) ? " (colored)": "");
13760                                 *range->group_prev = range->group_next;
13761                                 if (range->group_next) {
13762                                         range->group_next->group_prev = range->group_prev;
13763                                 }
13764                                 if (&range->group_next == rstate.high_tail) {
13765                                         rstate.high_tail = range->group_prev;
13766                                 }
13767                                 range->group_prev  = rstate.low_tail;
13768                                 range->group_next  = 0;
13769                                 *rstate.low_tail   = range;
13770                                 rstate.low_tail    = &range->group_next;
13771                                 next = point;
13772                         }
13773                         else {
13774                                 cgdebug_printf("hi: %5d degree %5d%s\n", 
13775                                         range - rstate.lr, range->degree,
13776                                         (range->color != REG_UNSET) ? " (colored)": "");
13777                         }
13778                 }
13779                 /* Color the live_ranges */
13780                 colored = color_graph(state, &rstate);
13781                 rstate.passes++;
13782         } while (!colored);
13783
13784         /* Verify the graph was properly colored */
13785         verify_colors(state, &rstate);
13786
13787         /* Move the colors from the graph to the triples */
13788         color_triples(state, &rstate);
13789
13790         /* Cleanup the temporary data structures */
13791         cleanup_rstate(state, &rstate);
13792 }
13793
13794 /* Sparce Conditional Constant Propogation
13795  * =========================================
13796  */
13797 struct ssa_edge;
13798 struct flow_block;
13799 struct lattice_node {
13800         unsigned old_id;
13801         struct triple *def;
13802         struct ssa_edge *out;
13803         struct flow_block *fblock;
13804         struct triple *val;
13805         /* lattice high   val && !is_const(val) 
13806          * lattice const  is_const(val)
13807          * lattice low    val == 0
13808          */
13809 };
13810 struct ssa_edge {
13811         struct lattice_node *src;
13812         struct lattice_node *dst;
13813         struct ssa_edge *work_next;
13814         struct ssa_edge *work_prev;
13815         struct ssa_edge *out_next;
13816 };
13817 struct flow_edge {
13818         struct flow_block *src;
13819         struct flow_block *dst;
13820         struct flow_edge *work_next;
13821         struct flow_edge *work_prev;
13822         struct flow_edge *in_next;
13823         struct flow_edge *out_next;
13824         int executable;
13825 };
13826 struct flow_block {
13827         struct block *block;
13828         struct flow_edge *in;
13829         struct flow_edge *out;
13830         struct flow_edge left, right;
13831 };
13832
13833 struct scc_state {
13834         int ins_count;
13835         struct lattice_node *lattice;
13836         struct ssa_edge     *ssa_edges;
13837         struct flow_block   *flow_blocks;
13838         struct flow_edge    *flow_work_list;
13839         struct ssa_edge     *ssa_work_list;
13840 };
13841
13842
13843 static void scc_add_fedge(struct compile_state *state, struct scc_state *scc, 
13844         struct flow_edge *fedge)
13845 {
13846         if (!scc->flow_work_list) {
13847                 scc->flow_work_list = fedge;
13848                 fedge->work_next = fedge->work_prev = fedge;
13849         }
13850         else {
13851                 struct flow_edge *ftail;
13852                 ftail = scc->flow_work_list->work_prev;
13853                 fedge->work_next = ftail->work_next;
13854                 fedge->work_prev = ftail;
13855                 fedge->work_next->work_prev = fedge;
13856                 fedge->work_prev->work_next = fedge;
13857         }
13858 }
13859
13860 static struct flow_edge *scc_next_fedge(
13861         struct compile_state *state, struct scc_state *scc)
13862 {
13863         struct flow_edge *fedge;
13864         fedge = scc->flow_work_list;
13865         if (fedge) {
13866                 fedge->work_next->work_prev = fedge->work_prev;
13867                 fedge->work_prev->work_next = fedge->work_next;
13868                 if (fedge->work_next != fedge) {
13869                         scc->flow_work_list = fedge->work_next;
13870                 } else {
13871                         scc->flow_work_list = 0;
13872                 }
13873         }
13874         return fedge;
13875 }
13876
13877 static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
13878         struct ssa_edge *sedge)
13879 {
13880         if (!scc->ssa_work_list) {
13881                 scc->ssa_work_list = sedge;
13882                 sedge->work_next = sedge->work_prev = sedge;
13883         }
13884         else {
13885                 struct ssa_edge *stail;
13886                 stail = scc->ssa_work_list->work_prev;
13887                 sedge->work_next = stail->work_next;
13888                 sedge->work_prev = stail;
13889                 sedge->work_next->work_prev = sedge;
13890                 sedge->work_prev->work_next = sedge;
13891         }
13892 }
13893
13894 static struct ssa_edge *scc_next_sedge(
13895         struct compile_state *state, struct scc_state *scc)
13896 {
13897         struct ssa_edge *sedge;
13898         sedge = scc->ssa_work_list;
13899         if (sedge) {
13900                 sedge->work_next->work_prev = sedge->work_prev;
13901                 sedge->work_prev->work_next = sedge->work_next;
13902                 if (sedge->work_next != sedge) {
13903                         scc->ssa_work_list = sedge->work_next;
13904                 } else {
13905                         scc->ssa_work_list = 0;
13906                 }
13907         }
13908         return sedge;
13909 }
13910
13911 static void initialize_scc_state(
13912         struct compile_state *state, struct scc_state *scc)
13913 {
13914         int ins_count, ssa_edge_count;
13915         int ins_index, ssa_edge_index, fblock_index;
13916         struct triple *first, *ins;
13917         struct block *block;
13918         struct flow_block *fblock;
13919
13920         memset(scc, 0, sizeof(*scc));
13921
13922         /* Inialize pass zero find out how much memory we need */
13923         first = RHS(state->main_function, 0);
13924         ins = first;
13925         ins_count = ssa_edge_count = 0;
13926         do {
13927                 struct triple_set *edge;
13928                 ins_count += 1;
13929                 for(edge = ins->use; edge; edge = edge->next) {
13930                         ssa_edge_count++;
13931                 }
13932                 ins = ins->next;
13933         } while(ins != first);
13934 #if DEBUG_SCC
13935         fprintf(stderr, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
13936                 ins_count, ssa_edge_count, state->last_vertex);
13937 #endif
13938         scc->ins_count   = ins_count;
13939         scc->lattice     = 
13940                 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
13941         scc->ssa_edges   = 
13942                 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
13943         scc->flow_blocks = 
13944                 xcmalloc(sizeof(*scc->flow_blocks)*(state->last_vertex + 1), 
13945                         "flow_blocks");
13946
13947         /* Initialize pass one collect up the nodes */
13948         fblock = 0;
13949         block = 0;
13950         ins_index = ssa_edge_index = fblock_index = 0;
13951         ins = first;
13952         do {
13953                 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
13954                         block = ins->u.block;
13955                         if (!block) {
13956                                 internal_error(state, ins, "label without block");
13957                         }
13958                         fblock_index += 1;
13959                         block->vertex = fblock_index;
13960                         fblock = &scc->flow_blocks[fblock_index];
13961                         fblock->block = block;
13962                 }
13963                 {
13964                         struct lattice_node *lnode;
13965                         ins_index += 1;
13966                         lnode = &scc->lattice[ins_index];
13967                         lnode->def = ins;
13968                         lnode->out = 0;
13969                         lnode->fblock = fblock;
13970                         lnode->val = ins; /* LATTICE HIGH */
13971                         lnode->old_id = ins->id;
13972                         ins->id = ins_index;
13973                 }
13974                 ins = ins->next;
13975         } while(ins != first);
13976         /* Initialize pass two collect up the edges */
13977         block = 0;
13978         fblock = 0;
13979         ins = first;
13980         do {
13981                 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
13982                         struct flow_edge *fedge, **ftail;
13983                         struct block_set *bedge;
13984                         block = ins->u.block;
13985                         fblock = &scc->flow_blocks[block->vertex];
13986                         fblock->in = 0;
13987                         fblock->out = 0;
13988                         ftail = &fblock->out;
13989                         if (block->left) {
13990                                 fblock->left.dst = &scc->flow_blocks[block->left->vertex];
13991                                 if (fblock->left.dst->block != block->left) {
13992                                         internal_error(state, 0, "block mismatch");
13993                                 }
13994                                 fblock->left.out_next = 0;
13995                                 *ftail = &fblock->left;
13996                                 ftail = &fblock->left.out_next;
13997                         }
13998                         if (block->right) {
13999                                 fblock->right.dst = &scc->flow_blocks[block->right->vertex];
14000                                 if (fblock->right.dst->block != block->right) {
14001                                         internal_error(state, 0, "block mismatch");
14002                                 }
14003                                 fblock->right.out_next = 0;
14004                                 *ftail = &fblock->right;
14005                                 ftail = &fblock->right.out_next;
14006                         }
14007                         for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
14008                                 fedge->src = fblock;
14009                                 fedge->work_next = fedge->work_prev = fedge;
14010                                 fedge->executable = 0;
14011                         }
14012                         ftail = &fblock->in;
14013                         for(bedge = block->use; bedge; bedge = bedge->next) {
14014                                 struct block *src_block;
14015                                 struct flow_block *sfblock;
14016                                 struct flow_edge *sfedge;
14017                                 src_block = bedge->member;
14018                                 sfblock = &scc->flow_blocks[src_block->vertex];
14019                                 sfedge = 0;
14020                                 if (src_block->left == block) {
14021                                         sfedge = &sfblock->left;
14022                                 } else {
14023                                         sfedge = &sfblock->right;
14024                                 }
14025                                 *ftail = sfedge;
14026                                 ftail = &sfedge->in_next;
14027                                 sfedge->in_next = 0;
14028                         }
14029                 }
14030                 {
14031                         struct triple_set *edge;
14032                         struct ssa_edge **stail;
14033                         struct lattice_node *lnode;
14034                         lnode = &scc->lattice[ins->id];
14035                         lnode->out = 0;
14036                         stail = &lnode->out;
14037                         for(edge = ins->use; edge; edge = edge->next) {
14038                                 struct ssa_edge *sedge;
14039                                 ssa_edge_index += 1;
14040                                 sedge = &scc->ssa_edges[ssa_edge_index];
14041                                 *stail = sedge;
14042                                 stail = &sedge->out_next;
14043                                 sedge->src = lnode;
14044                                 sedge->dst = &scc->lattice[edge->member->id];
14045                                 sedge->work_next = sedge->work_prev = sedge;
14046                                 sedge->out_next = 0;
14047                         }
14048                 }
14049                 ins = ins->next;
14050         } while(ins != first);
14051         /* Setup a dummy block 0 as a node above the start node */
14052         {
14053                 struct flow_block *fblock, *dst;
14054                 struct flow_edge *fedge;
14055                 fblock = &scc->flow_blocks[0];
14056                 fblock->block = 0;
14057                 fblock->in = 0;
14058                 fblock->out = &fblock->left;
14059                 dst = &scc->flow_blocks[state->first_block->vertex];
14060                 fedge = &fblock->left;
14061                 fedge->src        = fblock;
14062                 fedge->dst        = dst;
14063                 fedge->work_next  = fedge;
14064                 fedge->work_prev  = fedge;
14065                 fedge->in_next    = fedge->dst->in;
14066                 fedge->out_next   = 0;
14067                 fedge->executable = 0;
14068                 fedge->dst->in = fedge;
14069                 
14070                 /* Initialize the work lists */
14071                 scc->flow_work_list = 0;
14072                 scc->ssa_work_list  = 0;
14073                 scc_add_fedge(state, scc, fedge);
14074         }
14075 #if DEBUG_SCC
14076         fprintf(stderr, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
14077                 ins_index, ssa_edge_index, fblock_index);
14078 #endif
14079 }
14080
14081         
14082 static void free_scc_state(
14083         struct compile_state *state, struct scc_state *scc)
14084 {
14085         xfree(scc->flow_blocks);
14086         xfree(scc->ssa_edges);
14087         xfree(scc->lattice);
14088         
14089 }
14090
14091 static struct lattice_node *triple_to_lattice(
14092         struct compile_state *state, struct scc_state *scc, struct triple *ins)
14093 {
14094         if (ins->id <= 0) {
14095                 internal_error(state, ins, "bad id");
14096         }
14097         return &scc->lattice[ins->id];
14098 }
14099
14100 static struct triple *preserve_lval(
14101         struct compile_state *state, struct lattice_node *lnode)
14102 {
14103         struct triple *old;
14104         /* Preserve the original value */
14105         if (lnode->val) {
14106                 old = dup_triple(state, lnode->val);
14107                 if (lnode->val != lnode->def) {
14108                         xfree(lnode->val);
14109                 }
14110                 lnode->val = 0;
14111         } else {
14112                 old = 0;
14113         }
14114         return old;
14115 }
14116
14117 static int lval_changed(struct compile_state *state, 
14118         struct triple *old, struct lattice_node *lnode)
14119 {
14120         int changed;
14121         /* See if the lattice value has changed */
14122         changed = 1;
14123         if (!old && !lnode->val) {
14124                 changed = 0;
14125         }
14126         if (changed && lnode->val && !is_const(lnode->val)) {
14127                 changed = 0;
14128         }
14129         if (changed &&
14130                 lnode->val && old &&
14131                 (memcmp(lnode->val->param, old->param,
14132                         TRIPLE_SIZE(lnode->val->sizes) * sizeof(lnode->val->param[0])) == 0) &&
14133                 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
14134                 changed = 0;
14135         }
14136         if (old) {
14137                 xfree(old);
14138         }
14139         return changed;
14140
14141 }
14142
14143 static void scc_visit_phi(struct compile_state *state, struct scc_state *scc, 
14144         struct lattice_node *lnode)
14145 {
14146         struct lattice_node *tmp;
14147         struct triple **slot, *old;
14148         struct flow_edge *fedge;
14149         int index;
14150         if (lnode->def->op != OP_PHI) {
14151                 internal_error(state, lnode->def, "not phi");
14152         }
14153         /* Store the original value */
14154         old = preserve_lval(state, lnode);
14155
14156         /* default to lattice high */
14157         lnode->val = lnode->def;
14158         slot = &RHS(lnode->def, 0);
14159         index = 0;
14160         for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
14161                 if (!fedge->executable) {
14162                         continue;
14163                 }
14164                 if (!slot[index]) {
14165                         internal_error(state, lnode->def, "no phi value");
14166                 }
14167                 tmp = triple_to_lattice(state, scc, slot[index]);
14168                 /* meet(X, lattice low) = lattice low */
14169                 if (!tmp->val) {
14170                         lnode->val = 0;
14171                 }
14172                 /* meet(X, lattice high) = X */
14173                 else if (!tmp->val) {
14174                         lnode->val = lnode->val;
14175                 }
14176                 /* meet(lattice high, X) = X */
14177                 else if (!is_const(lnode->val)) {
14178                         lnode->val = dup_triple(state, tmp->val);
14179                         lnode->val->type = lnode->def->type;
14180                 }
14181                 /* meet(const, const) = const or lattice low */
14182                 else if (!constants_equal(state, lnode->val, tmp->val)) {
14183                         lnode->val = 0;
14184                 }
14185                 if (!lnode->val) {
14186                         break;
14187                 }
14188         }
14189 #if DEBUG_SCC
14190         fprintf(stderr, "phi: %d -> %s\n",
14191                 lnode->def->id,
14192                 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14193 #endif
14194         /* If the lattice value has changed update the work lists. */
14195         if (lval_changed(state, old, lnode)) {
14196                 struct ssa_edge *sedge;
14197                 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14198                         scc_add_sedge(state, scc, sedge);
14199                 }
14200         }
14201 }
14202
14203 static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
14204         struct lattice_node *lnode)
14205 {
14206         int changed;
14207         struct triple *old, *scratch;
14208         struct triple **dexpr, **vexpr;
14209         int count, i;
14210         
14211         /* Store the original value */
14212         old = preserve_lval(state, lnode);
14213
14214         /* Reinitialize the value */
14215         lnode->val = scratch = dup_triple(state, lnode->def);
14216         scratch->id = lnode->old_id;
14217         scratch->next     = scratch;
14218         scratch->prev     = scratch;
14219         scratch->use      = 0;
14220
14221         count = TRIPLE_SIZE(scratch->sizes);
14222         for(i = 0; i < count; i++) {
14223                 dexpr = &lnode->def->param[i];
14224                 vexpr = &scratch->param[i];
14225                 *vexpr = *dexpr;
14226                 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14227                         (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14228                         *dexpr) {
14229                         struct lattice_node *tmp;
14230                         tmp = triple_to_lattice(state, scc, *dexpr);
14231                         *vexpr = (tmp->val)? tmp->val : tmp->def;
14232                 }
14233         }
14234         if (scratch->op == OP_BRANCH) {
14235                 scratch->next = lnode->def->next;
14236         }
14237         /* Recompute the value */
14238 #warning "FIXME see if simplify does anything bad"
14239         /* So far it looks like only the strength reduction
14240          * optimization are things I need to worry about.
14241          */
14242         simplify(state, scratch);
14243         /* Cleanup my value */
14244         if (scratch->use) {
14245                 internal_error(state, lnode->def, "scratch used?");
14246         }
14247         if ((scratch->prev != scratch) ||
14248                 ((scratch->next != scratch) &&
14249                         ((lnode->def->op != OP_BRANCH) ||
14250                                 (scratch->next != lnode->def->next)))) {
14251                 internal_error(state, lnode->def, "scratch in list?");
14252         }
14253         /* undo any uses... */
14254         count = TRIPLE_SIZE(scratch->sizes);
14255         for(i = 0; i < count; i++) {
14256                 vexpr = &scratch->param[i];
14257                 if (*vexpr) {
14258                         unuse_triple(*vexpr, scratch);
14259                 }
14260         }
14261         if (!is_const(scratch)) {
14262                 for(i = 0; i < count; i++) {
14263                         dexpr = &lnode->def->param[i];
14264                         if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14265                                 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14266                                 *dexpr) {
14267                                 struct lattice_node *tmp;
14268                                 tmp = triple_to_lattice(state, scc, *dexpr);
14269                                 if (!tmp->val) {
14270                                         lnode->val = 0;
14271                                 }
14272                         }
14273                 }
14274         }
14275         if (lnode->val && 
14276                 (lnode->val->op == lnode->def->op) &&
14277                 (memcmp(lnode->val->param, lnode->def->param, 
14278                         count * sizeof(lnode->val->param[0])) == 0) &&
14279                 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
14280                 lnode->val = lnode->def;
14281         }
14282         /* Find the cases that are always lattice lo */
14283         if (lnode->val && 
14284                 triple_is_def(state, lnode->val) &&
14285                 !triple_is_pure(state, lnode->val)) {
14286                 lnode->val = 0;
14287         }
14288         if (lnode->val && 
14289                 (lnode->val->op == OP_SDECL) && 
14290                 (lnode->val != lnode->def)) {
14291                 internal_error(state, lnode->def, "bad sdecl");
14292         }
14293         /* See if the lattice value has changed */
14294         changed = lval_changed(state, old, lnode);
14295         if (lnode->val != scratch) {
14296                 xfree(scratch);
14297         }
14298         return changed;
14299 }
14300
14301 static void scc_visit_branch(struct compile_state *state, struct scc_state *scc,
14302         struct lattice_node *lnode)
14303 {
14304         struct lattice_node *cond;
14305 #if DEBUG_SCC
14306         {
14307                 struct flow_edge *fedge;
14308                 fprintf(stderr, "branch: %d (",
14309                         lnode->def->id);
14310                 
14311                 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
14312                         fprintf(stderr, " %d", fedge->dst->block->vertex);
14313                 }
14314                 fprintf(stderr, " )");
14315                 if (TRIPLE_RHS(lnode->def->sizes) > 0) {
14316                         fprintf(stderr, " <- %d",
14317                                 RHS(lnode->def, 0)->id);
14318                 }
14319                 fprintf(stderr, "\n");
14320         }
14321 #endif
14322         if (lnode->def->op != OP_BRANCH) {
14323                 internal_error(state, lnode->def, "not branch");
14324         }
14325         /* This only applies to conditional branches */
14326         if (TRIPLE_RHS(lnode->def->sizes) == 0) {
14327                 return;
14328         }
14329         cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
14330         if (cond->val && !is_const(cond->val)) {
14331 #warning "FIXME do I need to do something here?"
14332                 warning(state, cond->def, "condition not constant?");
14333                 return;
14334         }
14335         if (cond->val == 0) {
14336                 scc_add_fedge(state, scc, cond->fblock->out);
14337                 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14338         }
14339         else if (cond->val->u.cval) {
14340                 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14341                 
14342         } else {
14343                 scc_add_fedge(state, scc, cond->fblock->out);
14344         }
14345
14346 }
14347
14348 static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
14349         struct lattice_node *lnode)
14350 {
14351         int changed;
14352
14353         changed = compute_lnode_val(state, scc, lnode);
14354 #if DEBUG_SCC
14355         {
14356                 struct triple **expr;
14357                 fprintf(stderr, "expr: %3d %10s (",
14358                         lnode->def->id, tops(lnode->def->op));
14359                 expr = triple_rhs(state, lnode->def, 0);
14360                 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
14361                         if (*expr) {
14362                                 fprintf(stderr, " %d", (*expr)->id);
14363                         }
14364                 }
14365                 fprintf(stderr, " ) -> %s\n",
14366                         (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14367         }
14368 #endif
14369         if (lnode->def->op == OP_BRANCH) {
14370                 scc_visit_branch(state, scc, lnode);
14371
14372         }
14373         else if (changed) {
14374                 struct ssa_edge *sedge;
14375                 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14376                         scc_add_sedge(state, scc, sedge);
14377                 }
14378         }
14379 }
14380
14381 static void scc_writeback_values(
14382         struct compile_state *state, struct scc_state *scc)
14383 {
14384         struct triple *first, *ins;
14385         first = RHS(state->main_function, 0);
14386         ins = first;
14387         do {
14388                 struct lattice_node *lnode;
14389                 lnode = triple_to_lattice(state, scc, ins);
14390                 /* Restore id */
14391                 ins->id = lnode->old_id;
14392 #if DEBUG_SCC
14393                 if (lnode->val && !is_const(lnode->val)) {
14394                         warning(state, lnode->def, 
14395                                 "lattice node still high?");
14396                 }
14397 #endif
14398                 if (lnode->val && (lnode->val != ins)) {
14399                         /* See if it something I know how to write back */
14400                         switch(lnode->val->op) {
14401                         case OP_INTCONST:
14402                                 mkconst(state, ins, lnode->val->u.cval);
14403                                 break;
14404                         case OP_ADDRCONST:
14405                                 mkaddr_const(state, ins, 
14406                                         MISC(lnode->val, 0), lnode->val->u.cval);
14407                                 break;
14408                         default:
14409                                 /* By default don't copy the changes,
14410                                  * recompute them in place instead.
14411                                  */
14412                                 simplify(state, ins);
14413                                 break;
14414                         }
14415                         if (is_const(lnode->val) &&
14416                                 !constants_equal(state, lnode->val, ins)) {
14417                                 internal_error(state, 0, "constants not equal");
14418                         }
14419                         /* Free the lattice nodes */
14420                         xfree(lnode->val);
14421                         lnode->val = 0;
14422                 }
14423                 ins = ins->next;
14424         } while(ins != first);
14425 }
14426
14427 static void scc_transform(struct compile_state *state)
14428 {
14429         struct scc_state scc;
14430
14431         initialize_scc_state(state, &scc);
14432
14433         while(scc.flow_work_list || scc.ssa_work_list) {
14434                 struct flow_edge *fedge;
14435                 struct ssa_edge *sedge;
14436                 struct flow_edge *fptr;
14437                 while((fedge = scc_next_fedge(state, &scc))) {
14438                         struct block *block;
14439                         struct triple *ptr;
14440                         struct flow_block *fblock;
14441                         int time;
14442                         int done;
14443                         if (fedge->executable) {
14444                                 continue;
14445                         }
14446                         if (!fedge->dst) {
14447                                 internal_error(state, 0, "fedge without dst");
14448                         }
14449                         if (!fedge->src) {
14450                                 internal_error(state, 0, "fedge without src");
14451                         }
14452                         fedge->executable = 1;
14453                         fblock = fedge->dst;
14454                         block = fblock->block;
14455                         time = 0;
14456                         for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14457                                 if (fptr->executable) {
14458                                         time++;
14459                                 }
14460                         }
14461 #if DEBUG_SCC
14462                         fprintf(stderr, "vertex: %d time: %d\n", 
14463                                 block->vertex, time);
14464                         
14465 #endif
14466                         done = 0;
14467                         for(ptr = block->first; !done; ptr = ptr->next) {
14468                                 struct lattice_node *lnode;
14469                                 done = (ptr == block->last);
14470                                 lnode = &scc.lattice[ptr->id];
14471                                 if (ptr->op == OP_PHI) {
14472                                         scc_visit_phi(state, &scc, lnode);
14473                                 }
14474                                 else if (time == 1) {
14475                                         scc_visit_expr(state, &scc, lnode);
14476                                 }
14477                         }
14478                         if (fblock->out && !fblock->out->out_next) {
14479                                 scc_add_fedge(state, &scc, fblock->out);
14480                         }
14481                 }
14482                 while((sedge = scc_next_sedge(state, &scc))) {
14483                         struct lattice_node *lnode;
14484                         struct flow_block *fblock;
14485                         lnode = sedge->dst;
14486                         fblock = lnode->fblock;
14487 #if DEBUG_SCC
14488                         fprintf(stderr, "sedge: %5d (%5d -> %5d)\n",
14489                                 sedge - scc.ssa_edges,
14490                                 sedge->src->def->id,
14491                                 sedge->dst->def->id);
14492 #endif
14493                         if (lnode->def->op == OP_PHI) {
14494                                 scc_visit_phi(state, &scc, lnode);
14495                         }
14496                         else {
14497                                 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14498                                         if (fptr->executable) {
14499                                                 break;
14500                                         }
14501                                 }
14502                                 if (fptr) {
14503                                         scc_visit_expr(state, &scc, lnode);
14504                                 }
14505                         }
14506                 }
14507         }
14508         
14509         scc_writeback_values(state, &scc);
14510         free_scc_state(state, &scc);
14511 }
14512
14513
14514 static void transform_to_arch_instructions(struct compile_state *state)
14515 {
14516         struct triple *ins, *first;
14517         first = RHS(state->main_function, 0);
14518         ins = first;
14519         do {
14520                 ins = transform_to_arch_instruction(state, ins);
14521         } while(ins != first);
14522 }
14523
14524 #if DEBUG_CONSISTENCY
14525 static void verify_uses(struct compile_state *state)
14526 {
14527         struct triple *first, *ins;
14528         struct triple_set *set;
14529         first = RHS(state->main_function, 0);
14530         ins = first;
14531         do {
14532                 struct triple **expr;
14533                 expr = triple_rhs(state, ins, 0);
14534                 for(; expr; expr = triple_rhs(state, ins, expr)) {
14535                         struct triple *rhs;
14536                         rhs = *expr;
14537                         for(set = rhs?rhs->use:0; set; set = set->next) {
14538                                 if (set->member == ins) {
14539                                         break;
14540                                 }
14541                         }
14542                         if (!set) {
14543                                 internal_error(state, ins, "rhs not used");
14544                         }
14545                 }
14546                 expr = triple_lhs(state, ins, 0);
14547                 for(; expr; expr = triple_lhs(state, ins, expr)) {
14548                         struct triple *lhs;
14549                         lhs = *expr;
14550                         for(set =  lhs?lhs->use:0; set; set = set->next) {
14551                                 if (set->member == ins) {
14552                                         break;
14553                                 }
14554                         }
14555                         if (!set) {
14556                                 internal_error(state, ins, "lhs not used");
14557                         }
14558                 }
14559                 ins = ins->next;
14560         } while(ins != first);
14561         
14562 }
14563 static void verify_blocks(struct compile_state *state)
14564 {
14565         struct triple *ins;
14566         struct block *block;
14567         block = state->first_block;
14568         if (!block) {
14569                 return;
14570         }
14571         do {
14572                 for(ins = block->first; ins != block->last->next; ins = ins->next) {
14573                         if (!triple_stores_block(state, ins)) {
14574                                 continue;
14575                         }
14576                         if (ins->u.block != block) {
14577                                 internal_error(state, ins, "inconsitent block specified");
14578                         }
14579                 }
14580                 if (!triple_stores_block(state, block->last->next)) {
14581                         internal_error(state, block->last->next, 
14582                                 "cannot find next block");
14583                 }
14584                 block = block->last->next->u.block;
14585                 if (!block) {
14586                         internal_error(state, block->last->next,
14587                                 "bad next block");
14588                 }
14589         } while(block != state->first_block);
14590 }
14591
14592 static void verify_domination(struct compile_state *state)
14593 {
14594         struct triple *first, *ins;
14595         struct triple_set *set;
14596         if (!state->first_block) {
14597                 return;
14598         }
14599         
14600         first = RHS(state->main_function, 0);
14601         ins = first;
14602         do {
14603                 for(set = ins->use; set; set = set->next) {
14604                         struct triple **expr;
14605                         if (set->member->op == OP_PHI) {
14606                                 continue;
14607                         }
14608                         /* See if the use is on the righ hand side */
14609                         expr = triple_rhs(state, set->member, 0);
14610                         for(; expr ; expr = triple_rhs(state, set->member, expr)) {
14611                                 if (*expr == ins) {
14612                                         break;
14613                                 }
14614                         }
14615                         if (expr &&
14616                                 !tdominates(state, ins, set->member)) {
14617                                 internal_error(state, set->member, 
14618                                         "non dominated rhs use?");
14619                         }
14620                 }
14621                 ins = ins->next;
14622         } while(ins != first);
14623 }
14624
14625 static void verify_piece(struct compile_state *state)
14626 {
14627         struct triple *first, *ins;
14628         first = RHS(state->main_function, 0);
14629         ins = first;
14630         do {
14631                 struct triple *ptr;
14632                 int lhs, i;
14633                 lhs = TRIPLE_LHS(ins->sizes);
14634                 if ((ins->op == OP_WRITE) || (ins->op == OP_STORE)) {
14635                         lhs = 0;
14636                 }
14637                 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
14638                         if (ptr != LHS(ins, i)) {
14639                                 internal_error(state, ins, "malformed lhs on %s",
14640                                         tops(ins->op));
14641                         }
14642                         if (ptr->op != OP_PIECE) {
14643                                 internal_error(state, ins, "bad lhs op %s at %d on %s",
14644                                         tops(ptr->op), i, tops(ins->op));
14645                         }
14646                         if (ptr->u.cval != i) {
14647                                 internal_error(state, ins, "bad u.cval of %d %d expected",
14648                                         ptr->u.cval, i);
14649                         }
14650                 }
14651                 ins = ins->next;
14652         } while(ins != first);
14653 }
14654 static void verify_ins_colors(struct compile_state *state)
14655 {
14656         struct triple *first, *ins;
14657         
14658         first = RHS(state->main_function, 0);
14659         ins = first;
14660         do {
14661                 ins = ins->next;
14662         } while(ins != first);
14663 }
14664 static void verify_consistency(struct compile_state *state)
14665 {
14666         verify_uses(state);
14667         verify_blocks(state);
14668         verify_domination(state);
14669         verify_piece(state);
14670         verify_ins_colors(state);
14671 }
14672 #else 
14673 static void verify_consistency(struct compile_state *state) {}
14674 #endif /* DEBUG_USES */
14675
14676 static void optimize(struct compile_state *state)
14677 {
14678         if (state->debug & DEBUG_TRIPLES) {
14679                 print_triples(state);
14680         }
14681         /* Replace structures with simpler data types */
14682         flatten_structures(state);
14683         if (state->debug & DEBUG_TRIPLES) {
14684                 print_triples(state);
14685         }
14686         verify_consistency(state);
14687         /* Analize the intermediate code */
14688         setup_basic_blocks(state);
14689         analyze_idominators(state);
14690         analyze_ipdominators(state);
14691         /* Transform the code to ssa form */
14692         transform_to_ssa_form(state);
14693         verify_consistency(state);
14694         if (state->debug & DEBUG_CODE_ELIMINATION) {
14695                 fprintf(stdout, "After transform_to_ssa_form\n");
14696                 print_blocks(state, stdout);
14697         }
14698         /* Do strength reduction and simple constant optimizations */
14699         if (state->optimize >= 1) {
14700                 simplify_all(state);
14701         }
14702         verify_consistency(state);
14703         /* Propogate constants throughout the code */
14704         if (state->optimize >= 2) {
14705 #warning "FIXME fix scc_transform"
14706                 scc_transform(state);
14707                 transform_from_ssa_form(state);
14708                 free_basic_blocks(state);
14709                 setup_basic_blocks(state);
14710                 analyze_idominators(state);
14711                 analyze_ipdominators(state);
14712                 transform_to_ssa_form(state);
14713         }
14714         verify_consistency(state);
14715 #warning "WISHLIST implement single use constants (least possible register pressure)"
14716 #warning "WISHLIST implement induction variable elimination"
14717         /* Select architecture instructions and an initial partial
14718          * coloring based on architecture constraints.
14719          */
14720         transform_to_arch_instructions(state);
14721         verify_consistency(state);
14722         if (state->debug & DEBUG_ARCH_CODE) {
14723                 printf("After transform_to_arch_instructions\n");
14724                 print_blocks(state, stdout);
14725                 print_control_flow(state);
14726         }
14727         eliminate_inefectual_code(state);
14728         verify_consistency(state);
14729         if (state->debug & DEBUG_CODE_ELIMINATION) {
14730                 printf("After eliminate_inefectual_code\n");
14731                 print_blocks(state, stdout);
14732                 print_control_flow(state);
14733         }
14734         verify_consistency(state);
14735         /* Color all of the variables to see if they will fit in registers */
14736         insert_copies_to_phi(state);
14737         if (state->debug & DEBUG_INSERTED_COPIES) {
14738                 printf("After insert_copies_to_phi\n");
14739                 print_blocks(state, stdout);
14740                 print_control_flow(state);
14741         }
14742         verify_consistency(state);
14743         insert_mandatory_copies(state);
14744         if (state->debug & DEBUG_INSERTED_COPIES) {
14745                 printf("After insert_mandatory_copies\n");
14746                 print_blocks(state, stdout);
14747                 print_control_flow(state);
14748         }
14749         verify_consistency(state);
14750         allocate_registers(state);
14751         verify_consistency(state);
14752         if (state->debug & DEBUG_INTERMEDIATE_CODE) {
14753                 print_blocks(state, stdout);
14754         }
14755         if (state->debug & DEBUG_CONTROL_FLOW) {
14756                 print_control_flow(state);
14757         }
14758         /* Remove the optimization information.
14759          * This is more to check for memory consistency than to free memory.
14760          */
14761         free_basic_blocks(state);
14762 }
14763
14764 static void print_op_asm(struct compile_state *state,
14765         struct triple *ins, FILE *fp)
14766 {
14767         struct asm_info *info;
14768         const char *ptr;
14769         unsigned lhs, rhs, i;
14770         info = ins->u.ainfo;
14771         lhs = TRIPLE_LHS(ins->sizes);
14772         rhs = TRIPLE_RHS(ins->sizes);
14773         /* Don't count the clobbers in lhs */
14774         for(i = 0; i < lhs; i++) {
14775                 if (LHS(ins, i)->type == &void_type) {
14776                         break;
14777                 }
14778         }
14779         lhs = i;
14780         fprintf(fp, "#ASM\n");
14781         fputc('\t', fp);
14782         for(ptr = info->str; *ptr; ptr++) {
14783                 char *next;
14784                 unsigned long param;
14785                 struct triple *piece;
14786                 if (*ptr != '%') {
14787                         fputc(*ptr, fp);
14788                         continue;
14789                 }
14790                 ptr++;
14791                 if (*ptr == '%') {
14792                         fputc('%', fp);
14793                         continue;
14794                 }
14795                 param = strtoul(ptr, &next, 10);
14796                 if (ptr == next) {
14797                         error(state, ins, "Invalid asm template");
14798                 }
14799                 if (param >= (lhs + rhs)) {
14800                         error(state, ins, "Invalid param %%%u in asm template",
14801                                 param);
14802                 }
14803                 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
14804                 fprintf(fp, "%s", 
14805                         arch_reg_str(ID_REG(piece->id)));
14806                 ptr = next -1;
14807         }
14808         fprintf(fp, "\n#NOT ASM\n");
14809 }
14810
14811
14812 /* Only use the low x86 byte registers.  This allows me
14813  * allocate the entire register when a byte register is used.
14814  */
14815 #define X86_4_8BIT_GPRS 1
14816
14817 /* Recognized x86 cpu variants */
14818 #define BAD_CPU      0
14819 #define CPU_I386     1
14820 #define CPU_P3       2
14821 #define CPU_P4       3
14822 #define CPU_K7       4
14823 #define CPU_K8       5
14824
14825 #define CPU_DEFAULT  CPU_I386
14826
14827 /* The x86 register classes */
14828 #define REGC_FLAGS    0
14829 #define REGC_GPR8     1
14830 #define REGC_GPR16    2
14831 #define REGC_GPR32    3
14832 #define REGC_GPR64    4
14833 #define REGC_MMX      5
14834 #define REGC_XMM      6
14835 #define REGC_GPR32_8  7
14836 #define REGC_GPR16_8  8
14837 #define REGC_IMM32    9
14838 #define REGC_IMM16   10
14839 #define REGC_IMM8    11
14840 #define LAST_REGC  REGC_IMM8
14841 #if LAST_REGC >= MAX_REGC
14842 #error "MAX_REGC is to low"
14843 #endif
14844
14845 /* Register class masks */
14846 #define REGCM_FLAGS   (1 << REGC_FLAGS)
14847 #define REGCM_GPR8    (1 << REGC_GPR8)
14848 #define REGCM_GPR16   (1 << REGC_GPR16)
14849 #define REGCM_GPR32   (1 << REGC_GPR32)
14850 #define REGCM_GPR64   (1 << REGC_GPR64)
14851 #define REGCM_MMX     (1 << REGC_MMX)
14852 #define REGCM_XMM     (1 << REGC_XMM)
14853 #define REGCM_GPR32_8 (1 << REGC_GPR32_8)
14854 #define REGCM_GPR16_8 (1 << REGC_GPR16_8)
14855 #define REGCM_IMM32   (1 << REGC_IMM32)
14856 #define REGCM_IMM16   (1 << REGC_IMM16)
14857 #define REGCM_IMM8    (1 << REGC_IMM8)
14858 #define REGCM_ALL     ((1 << (LAST_REGC + 1)) - 1)
14859
14860 /* The x86 registers */
14861 #define REG_EFLAGS  2
14862 #define REGC_FLAGS_FIRST REG_EFLAGS
14863 #define REGC_FLAGS_LAST  REG_EFLAGS
14864 #define REG_AL      3
14865 #define REG_BL      4
14866 #define REG_CL      5
14867 #define REG_DL      6
14868 #define REG_AH      7
14869 #define REG_BH      8
14870 #define REG_CH      9
14871 #define REG_DH      10
14872 #define REGC_GPR8_FIRST  REG_AL
14873 #if X86_4_8BIT_GPRS
14874 #define REGC_GPR8_LAST   REG_DL
14875 #else 
14876 #define REGC_GPR8_LAST   REG_DH
14877 #endif
14878 #define REG_AX     11
14879 #define REG_BX     12
14880 #define REG_CX     13
14881 #define REG_DX     14
14882 #define REG_SI     15
14883 #define REG_DI     16
14884 #define REG_BP     17
14885 #define REG_SP     18
14886 #define REGC_GPR16_FIRST REG_AX
14887 #define REGC_GPR16_LAST  REG_SP
14888 #define REG_EAX    19
14889 #define REG_EBX    20
14890 #define REG_ECX    21
14891 #define REG_EDX    22
14892 #define REG_ESI    23
14893 #define REG_EDI    24
14894 #define REG_EBP    25
14895 #define REG_ESP    26
14896 #define REGC_GPR32_FIRST REG_EAX
14897 #define REGC_GPR32_LAST  REG_ESP
14898 #define REG_EDXEAX 27
14899 #define REGC_GPR64_FIRST REG_EDXEAX
14900 #define REGC_GPR64_LAST  REG_EDXEAX
14901 #define REG_MMX0   28
14902 #define REG_MMX1   29
14903 #define REG_MMX2   30
14904 #define REG_MMX3   31
14905 #define REG_MMX4   32
14906 #define REG_MMX5   33
14907 #define REG_MMX6   34
14908 #define REG_MMX7   35
14909 #define REGC_MMX_FIRST REG_MMX0
14910 #define REGC_MMX_LAST  REG_MMX7
14911 #define REG_XMM0   36
14912 #define REG_XMM1   37
14913 #define REG_XMM2   38
14914 #define REG_XMM3   39
14915 #define REG_XMM4   40
14916 #define REG_XMM5   41
14917 #define REG_XMM6   42
14918 #define REG_XMM7   43
14919 #define REGC_XMM_FIRST REG_XMM0
14920 #define REGC_XMM_LAST  REG_XMM7
14921 #warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
14922 #define LAST_REG   REG_XMM7
14923
14924 #define REGC_GPR32_8_FIRST REG_EAX
14925 #define REGC_GPR32_8_LAST  REG_EDX
14926 #define REGC_GPR16_8_FIRST REG_AX
14927 #define REGC_GPR16_8_LAST  REG_DX
14928
14929 #define REGC_IMM8_FIRST    -1
14930 #define REGC_IMM8_LAST     -1
14931 #define REGC_IMM16_FIRST   -2
14932 #define REGC_IMM16_LAST    -1
14933 #define REGC_IMM32_FIRST   -4
14934 #define REGC_IMM32_LAST    -1
14935
14936 #if LAST_REG >= MAX_REGISTERS
14937 #error "MAX_REGISTERS to low"
14938 #endif
14939
14940
14941 static unsigned regc_size[LAST_REGC +1] = {
14942         [REGC_FLAGS]   = REGC_FLAGS_LAST   - REGC_FLAGS_FIRST + 1,
14943         [REGC_GPR8]    = REGC_GPR8_LAST    - REGC_GPR8_FIRST + 1,
14944         [REGC_GPR16]   = REGC_GPR16_LAST   - REGC_GPR16_FIRST + 1,
14945         [REGC_GPR32]   = REGC_GPR32_LAST   - REGC_GPR32_FIRST + 1,
14946         [REGC_GPR64]   = REGC_GPR64_LAST   - REGC_GPR64_FIRST + 1,
14947         [REGC_MMX]     = REGC_MMX_LAST     - REGC_MMX_FIRST + 1,
14948         [REGC_XMM]     = REGC_XMM_LAST     - REGC_XMM_FIRST + 1,
14949         [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
14950         [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
14951         [REGC_IMM32]   = 0,
14952         [REGC_IMM16]   = 0,
14953         [REGC_IMM8]    = 0,
14954 };
14955
14956 static const struct {
14957         int first, last;
14958 } regcm_bound[LAST_REGC + 1] = {
14959         [REGC_FLAGS]   = { REGC_FLAGS_FIRST,   REGC_FLAGS_LAST },
14960         [REGC_GPR8]    = { REGC_GPR8_FIRST,    REGC_GPR8_LAST },
14961         [REGC_GPR16]   = { REGC_GPR16_FIRST,   REGC_GPR16_LAST },
14962         [REGC_GPR32]   = { REGC_GPR32_FIRST,   REGC_GPR32_LAST },
14963         [REGC_GPR64]   = { REGC_GPR64_FIRST,   REGC_GPR64_LAST },
14964         [REGC_MMX]     = { REGC_MMX_FIRST,     REGC_MMX_LAST },
14965         [REGC_XMM]     = { REGC_XMM_FIRST,     REGC_XMM_LAST },
14966         [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
14967         [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
14968         [REGC_IMM32]   = { REGC_IMM32_FIRST,   REGC_IMM32_LAST },
14969         [REGC_IMM16]   = { REGC_IMM16_FIRST,   REGC_IMM16_LAST },
14970         [REGC_IMM8]    = { REGC_IMM8_FIRST,    REGC_IMM8_LAST },
14971 };
14972
14973 static int arch_encode_cpu(const char *cpu)
14974 {
14975         struct cpu {
14976                 const char *name;
14977                 int cpu;
14978         } cpus[] = {
14979                 { "i386", CPU_I386 },
14980                 { "p3",   CPU_P3 },
14981                 { "p4",   CPU_P4 },
14982                 { "k7",   CPU_K7 },
14983                 { "k8",   CPU_K8 },
14984                 {  0,     BAD_CPU }
14985         };
14986         struct cpu *ptr;
14987         for(ptr = cpus; ptr->name; ptr++) {
14988                 if (strcmp(ptr->name, cpu) == 0) {
14989                         break;
14990                 }
14991         }
14992         return ptr->cpu;
14993 }
14994
14995 static unsigned arch_regc_size(struct compile_state *state, int class)
14996 {
14997         if ((class < 0) || (class > LAST_REGC)) {
14998                 return 0;
14999         }
15000         return regc_size[class];
15001 }
15002 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
15003 {
15004         /* See if two register classes may have overlapping registers */
15005         unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 |
15006                 REGCM_GPR32_8 | REGCM_GPR32 | REGCM_GPR64;
15007
15008         /* Special case for the immediates */
15009         if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
15010                 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
15011                 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
15012                 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) { 
15013                 return 0;
15014         }
15015         return (regcm1 & regcm2) ||
15016                 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
15017 }
15018
15019 static void arch_reg_equivs(
15020         struct compile_state *state, unsigned *equiv, int reg)
15021 {
15022         if ((reg < 0) || (reg > LAST_REG)) {
15023                 internal_error(state, 0, "invalid register");
15024         }
15025         *equiv++ = reg;
15026         switch(reg) {
15027         case REG_AL:
15028 #if X86_4_8BIT_GPRS
15029                 *equiv++ = REG_AH;
15030 #endif
15031                 *equiv++ = REG_AX;
15032                 *equiv++ = REG_EAX;
15033                 *equiv++ = REG_EDXEAX;
15034                 break;
15035         case REG_AH:
15036 #if X86_4_8BIT_GPRS
15037                 *equiv++ = REG_AL;
15038 #endif
15039                 *equiv++ = REG_AX;
15040                 *equiv++ = REG_EAX;
15041                 *equiv++ = REG_EDXEAX;
15042                 break;
15043         case REG_BL:  
15044 #if X86_4_8BIT_GPRS
15045                 *equiv++ = REG_BH;
15046 #endif
15047                 *equiv++ = REG_BX;
15048                 *equiv++ = REG_EBX;
15049                 break;
15050
15051         case REG_BH:
15052 #if X86_4_8BIT_GPRS
15053                 *equiv++ = REG_BL;
15054 #endif
15055                 *equiv++ = REG_BX;
15056                 *equiv++ = REG_EBX;
15057                 break;
15058         case REG_CL:
15059 #if X86_4_8BIT_GPRS
15060                 *equiv++ = REG_CH;
15061 #endif
15062                 *equiv++ = REG_CX;
15063                 *equiv++ = REG_ECX;
15064                 break;
15065
15066         case REG_CH:
15067 #if X86_4_8BIT_GPRS
15068                 *equiv++ = REG_CL;
15069 #endif
15070                 *equiv++ = REG_CX;
15071                 *equiv++ = REG_ECX;
15072                 break;
15073         case REG_DL:
15074 #if X86_4_8BIT_GPRS
15075                 *equiv++ = REG_DH;
15076 #endif
15077                 *equiv++ = REG_DX;
15078                 *equiv++ = REG_EDX;
15079                 *equiv++ = REG_EDXEAX;
15080                 break;
15081         case REG_DH:
15082 #if X86_4_8BIT_GPRS
15083                 *equiv++ = REG_DL;
15084 #endif
15085                 *equiv++ = REG_DX;
15086                 *equiv++ = REG_EDX;
15087                 *equiv++ = REG_EDXEAX;
15088                 break;
15089         case REG_AX:
15090                 *equiv++ = REG_AL;
15091                 *equiv++ = REG_AH;
15092                 *equiv++ = REG_EAX;
15093                 *equiv++ = REG_EDXEAX;
15094                 break;
15095         case REG_BX:
15096                 *equiv++ = REG_BL;
15097                 *equiv++ = REG_BH;
15098                 *equiv++ = REG_EBX;
15099                 break;
15100         case REG_CX:  
15101                 *equiv++ = REG_CL;
15102                 *equiv++ = REG_CH;
15103                 *equiv++ = REG_ECX;
15104                 break;
15105         case REG_DX:  
15106                 *equiv++ = REG_DL;
15107                 *equiv++ = REG_DH;
15108                 *equiv++ = REG_EDX;
15109                 *equiv++ = REG_EDXEAX;
15110                 break;
15111         case REG_SI:  
15112                 *equiv++ = REG_ESI;
15113                 break;
15114         case REG_DI:
15115                 *equiv++ = REG_EDI;
15116                 break;
15117         case REG_BP:
15118                 *equiv++ = REG_EBP;
15119                 break;
15120         case REG_SP:
15121                 *equiv++ = REG_ESP;
15122                 break;
15123         case REG_EAX:
15124                 *equiv++ = REG_AL;
15125                 *equiv++ = REG_AH;
15126                 *equiv++ = REG_AX;
15127                 *equiv++ = REG_EDXEAX;
15128                 break;
15129         case REG_EBX:
15130                 *equiv++ = REG_BL;
15131                 *equiv++ = REG_BH;
15132                 *equiv++ = REG_BX;
15133                 break;
15134         case REG_ECX:
15135                 *equiv++ = REG_CL;
15136                 *equiv++ = REG_CH;
15137                 *equiv++ = REG_CX;
15138                 break;
15139         case REG_EDX:
15140                 *equiv++ = REG_DL;
15141                 *equiv++ = REG_DH;
15142                 *equiv++ = REG_DX;
15143                 *equiv++ = REG_EDXEAX;
15144                 break;
15145         case REG_ESI: 
15146                 *equiv++ = REG_SI;
15147                 break;
15148         case REG_EDI: 
15149                 *equiv++ = REG_DI;
15150                 break;
15151         case REG_EBP: 
15152                 *equiv++ = REG_BP;
15153                 break;
15154         case REG_ESP: 
15155                 *equiv++ = REG_SP;
15156                 break;
15157         case REG_EDXEAX: 
15158                 *equiv++ = REG_AL;
15159                 *equiv++ = REG_AH;
15160                 *equiv++ = REG_DL;
15161                 *equiv++ = REG_DH;
15162                 *equiv++ = REG_AX;
15163                 *equiv++ = REG_DX;
15164                 *equiv++ = REG_EAX;
15165                 *equiv++ = REG_EDX;
15166                 break;
15167         }
15168         *equiv++ = REG_UNSET; 
15169 }
15170
15171 static unsigned arch_avail_mask(struct compile_state *state)
15172 {
15173         unsigned avail_mask;
15174         avail_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 | 
15175                 REGCM_GPR32 | REGCM_GPR32_8 | REGCM_GPR64 |
15176                 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
15177         switch(state->cpu) {
15178         case CPU_P3:
15179         case CPU_K7:
15180                 avail_mask |= REGCM_MMX;
15181                 break;
15182         case CPU_P4:
15183         case CPU_K8:
15184                 avail_mask |= REGCM_MMX | REGCM_XMM;
15185                 break;
15186         }
15187 #if 0
15188         /* Don't enable 8 bit values until I can force both operands
15189          * to be 8bits simultaneously.
15190          */
15191         avail_mask &= ~(REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16);
15192 #endif
15193         return avail_mask;
15194 }
15195
15196 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
15197 {
15198         unsigned mask, result;
15199         int class, class2;
15200         result = regcm;
15201         result &= arch_avail_mask(state);
15202
15203         for(class = 0, mask = 1; mask; mask <<= 1, class++) {
15204                 if ((result & mask) == 0) {
15205                         continue;
15206                 }
15207                 if (class > LAST_REGC) {
15208                         result &= ~mask;
15209                 }
15210                 for(class2 = 0; class2 <= LAST_REGC; class2++) {
15211                         if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
15212                                 (regcm_bound[class2].last <= regcm_bound[class].last)) {
15213                                 result |= (1 << class2);
15214                         }
15215                 }
15216         }
15217         return result;
15218 }
15219
15220 static unsigned arch_reg_regcm(struct compile_state *state, int reg)
15221 {
15222         unsigned mask;
15223         int class;
15224         mask = 0;
15225         for(class = 0; class <= LAST_REGC; class++) {
15226                 if ((reg >= regcm_bound[class].first) &&
15227                         (reg <= regcm_bound[class].last)) {
15228                         mask |= (1 << class);
15229                 }
15230         }
15231         if (!mask) {
15232                 internal_error(state, 0, "reg %d not in any class", reg);
15233         }
15234         return mask;
15235 }
15236
15237 static struct reg_info arch_reg_constraint(
15238         struct compile_state *state, struct type *type, const char *constraint)
15239 {
15240         static const struct {
15241                 char class;
15242                 unsigned int mask;
15243                 unsigned int reg;
15244         } constraints[] = {
15245                 { 'r', REGCM_GPR32, REG_UNSET },
15246                 { 'g', REGCM_GPR32, REG_UNSET },
15247                 { 'p', REGCM_GPR32, REG_UNSET },
15248                 { 'q', REGCM_GPR8,  REG_UNSET },
15249                 { 'Q', REGCM_GPR32_8, REG_UNSET },
15250                 { 'x', REGCM_XMM,   REG_UNSET },
15251                 { 'y', REGCM_MMX,   REG_UNSET },
15252                 { 'a', REGCM_GPR32, REG_EAX },
15253                 { 'b', REGCM_GPR32, REG_EBX },
15254                 { 'c', REGCM_GPR32, REG_ECX },
15255                 { 'd', REGCM_GPR32, REG_EDX },
15256                 { 'D', REGCM_GPR32, REG_EDI },
15257                 { 'S', REGCM_GPR32, REG_ESI },
15258                 { '\0', 0, REG_UNSET },
15259         };
15260         unsigned int regcm;
15261         unsigned int mask, reg;
15262         struct reg_info result;
15263         const char *ptr;
15264         regcm = arch_type_to_regcm(state, type);
15265         reg = REG_UNSET;
15266         mask = 0;
15267         for(ptr = constraint; *ptr; ptr++) {
15268                 int i;
15269                 if (*ptr ==  ' ') {
15270                         continue;
15271                 }
15272                 for(i = 0; constraints[i].class != '\0'; i++) {
15273                         if (constraints[i].class == *ptr) {
15274                                 break;
15275                         }
15276                 }
15277                 if (constraints[i].class == '\0') {
15278                         error(state, 0, "invalid register constraint ``%c''", *ptr);
15279                         break;
15280                 }
15281                 if ((constraints[i].mask & regcm) == 0) {
15282                         error(state, 0, "invalid register class %c specified",
15283                                 *ptr);
15284                 }
15285                 mask |= constraints[i].mask;
15286                 if (constraints[i].reg != REG_UNSET) {
15287                         if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
15288                                 error(state, 0, "Only one register may be specified");
15289                         }
15290                         reg = constraints[i].reg;
15291                 }
15292         }
15293         result.reg = reg;
15294         result.regcm = mask;
15295         return result;
15296 }
15297
15298 static struct reg_info arch_reg_clobber(
15299         struct compile_state *state, const char *clobber)
15300 {
15301         struct reg_info result;
15302         if (strcmp(clobber, "memory") == 0) {
15303                 result.reg = REG_UNSET;
15304                 result.regcm = 0;
15305         }
15306         else if (strcmp(clobber, "%eax") == 0) {
15307                 result.reg = REG_EAX;
15308                 result.regcm = REGCM_GPR32;
15309         }
15310         else if (strcmp(clobber, "%ebx") == 0) {
15311                 result.reg = REG_EBX;
15312                 result.regcm = REGCM_GPR32;
15313         }
15314         else if (strcmp(clobber, "%ecx") == 0) {
15315                 result.reg = REG_ECX;
15316                 result.regcm = REGCM_GPR32;
15317         }
15318         else if (strcmp(clobber, "%edx") == 0) {
15319                 result.reg = REG_EDX;
15320                 result.regcm = REGCM_GPR32;
15321         }
15322         else if (strcmp(clobber, "%esi") == 0) {
15323                 result.reg = REG_ESI;
15324                 result.regcm = REGCM_GPR32;
15325         }
15326         else if (strcmp(clobber, "%edi") == 0) {
15327                 result.reg = REG_EDI;
15328                 result.regcm = REGCM_GPR32;
15329         }
15330         else if (strcmp(clobber, "%ebp") == 0) {
15331                 result.reg = REG_EBP;
15332                 result.regcm = REGCM_GPR32;
15333         }
15334         else if (strcmp(clobber, "%esp") == 0) {
15335                 result.reg = REG_ESP;
15336                 result.regcm = REGCM_GPR32;
15337         }
15338         else if (strcmp(clobber, "cc") == 0) {
15339                 result.reg = REG_EFLAGS;
15340                 result.regcm = REGCM_FLAGS;
15341         }
15342         else if ((strncmp(clobber, "xmm", 3) == 0)  &&
15343                 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15344                 result.reg = REG_XMM0 + octdigval(clobber[3]);
15345                 result.regcm = REGCM_XMM;
15346         }
15347         else if ((strncmp(clobber, "mmx", 3) == 0) &&
15348                 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15349                 result.reg = REG_MMX0 + octdigval(clobber[3]);
15350                 result.regcm = REGCM_MMX;
15351         }
15352         else {
15353                 error(state, 0, "Invalid register clobber");
15354                 result.reg = REG_UNSET;
15355                 result.regcm = 0;
15356         }
15357         return result;
15358 }
15359
15360 static int do_select_reg(struct compile_state *state, 
15361         char *used, int reg, unsigned classes)
15362 {
15363         unsigned mask;
15364         if (used[reg]) {
15365                 return REG_UNSET;
15366         }
15367         mask = arch_reg_regcm(state, reg);
15368         return (classes & mask) ? reg : REG_UNSET;
15369 }
15370
15371 static int arch_select_free_register(
15372         struct compile_state *state, char *used, int classes)
15373 {
15374         /* Preference: flags, 8bit gprs, 32bit gprs, other 32bit reg
15375          * other types of registers.
15376          */
15377         int i, reg;
15378         reg = REG_UNSET;
15379         for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
15380                 reg = do_select_reg(state, used, i, classes);
15381         }
15382         for(i = REGC_GPR32_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR32_LAST); i++) {
15383                 reg = do_select_reg(state, used, i, classes);
15384         }
15385         for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
15386                 reg = do_select_reg(state, used, i, classes);
15387         }
15388         for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
15389                 reg = do_select_reg(state, used, i, classes);
15390         }
15391         for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
15392                 reg = do_select_reg(state, used, i, classes);
15393         }
15394         for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
15395                 reg = do_select_reg(state, used, i, classes);
15396         }
15397         for(i = REGC_GPR64_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR64_LAST); i++) {
15398                 reg = do_select_reg(state, used, i, classes);
15399         }
15400         return reg;
15401 }
15402
15403
15404 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type) 
15405 {
15406 #warning "FIXME force types smaller (if legal) before I get here"
15407         unsigned avail_mask;
15408         unsigned mask;
15409         mask = 0;
15410         avail_mask = arch_avail_mask(state);
15411         switch(type->type & TYPE_MASK) {
15412         case TYPE_ARRAY:
15413         case TYPE_VOID: 
15414                 mask = 0; 
15415                 break;
15416         case TYPE_CHAR:
15417         case TYPE_UCHAR:
15418                 mask = REGCM_GPR8 | 
15419                         REGCM_GPR16 | REGCM_GPR16_8 | 
15420                         REGCM_GPR32 | REGCM_GPR32_8 |
15421                         REGCM_GPR64 |
15422                         REGCM_MMX | REGCM_XMM |
15423                         REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
15424                 break;
15425         case TYPE_SHORT:
15426         case TYPE_USHORT:
15427                 mask =  REGCM_GPR16 | REGCM_GPR16_8 |
15428                         REGCM_GPR32 | REGCM_GPR32_8 |
15429                         REGCM_GPR64 |
15430                         REGCM_MMX | REGCM_XMM |
15431                         REGCM_IMM32 | REGCM_IMM16;
15432                 break;
15433         case TYPE_INT:
15434         case TYPE_UINT:
15435         case TYPE_LONG:
15436         case TYPE_ULONG:
15437         case TYPE_POINTER:
15438                 mask =  REGCM_GPR32 | REGCM_GPR32_8 |
15439                         REGCM_GPR64 | REGCM_MMX | REGCM_XMM |
15440                         REGCM_IMM32;
15441                 break;
15442         default:
15443                 internal_error(state, 0, "no register class for type");
15444                 break;
15445         }
15446         mask &= avail_mask;
15447         return mask;
15448 }
15449
15450 static int is_imm32(struct triple *imm)
15451 {
15452         return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
15453                 (imm->op == OP_ADDRCONST);
15454         
15455 }
15456 static int is_imm16(struct triple *imm)
15457 {
15458         return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
15459 }
15460 static int is_imm8(struct triple *imm)
15461 {
15462         return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
15463 }
15464
15465 static int get_imm32(struct triple *ins, struct triple **expr)
15466 {
15467         struct triple *imm;
15468         imm = *expr;
15469         while(imm->op == OP_COPY) {
15470                 imm = RHS(imm, 0);
15471         }
15472         if (!is_imm32(imm)) {
15473                 return 0;
15474         }
15475         unuse_triple(*expr, ins);
15476         use_triple(imm, ins);
15477         *expr = imm;
15478         return 1;
15479 }
15480
15481 static int get_imm8(struct triple *ins, struct triple **expr)
15482 {
15483         struct triple *imm;
15484         imm = *expr;
15485         while(imm->op == OP_COPY) {
15486                 imm = RHS(imm, 0);
15487         }
15488         if (!is_imm8(imm)) {
15489                 return 0;
15490         }
15491         unuse_triple(*expr, ins);
15492         use_triple(imm, ins);
15493         *expr = imm;
15494         return 1;
15495 }
15496
15497 #define TEMPLATE_NOP         0
15498 #define TEMPLATE_INTCONST8   1
15499 #define TEMPLATE_INTCONST32  2
15500 #define TEMPLATE_COPY_REG    3
15501 #define TEMPLATE_COPY_IMM32  4
15502 #define TEMPLATE_COPY_IMM16  5
15503 #define TEMPLATE_COPY_IMM8   6
15504 #define TEMPLATE_PHI         7
15505 #define TEMPLATE_STORE8      8
15506 #define TEMPLATE_STORE16     9
15507 #define TEMPLATE_STORE32    10
15508 #define TEMPLATE_LOAD8      11
15509 #define TEMPLATE_LOAD16     12
15510 #define TEMPLATE_LOAD32     13
15511 #define TEMPLATE_BINARY_REG 14
15512 #define TEMPLATE_BINARY_IMM 15
15513 #define TEMPLATE_SL_CL      16
15514 #define TEMPLATE_SL_IMM     17
15515 #define TEMPLATE_UNARY      18
15516 #define TEMPLATE_CMP_REG    19
15517 #define TEMPLATE_CMP_IMM    20
15518 #define TEMPLATE_TEST       21
15519 #define TEMPLATE_SET        22
15520 #define TEMPLATE_JMP        23
15521 #define TEMPLATE_INB_DX     24
15522 #define TEMPLATE_INB_IMM    25
15523 #define TEMPLATE_INW_DX     26
15524 #define TEMPLATE_INW_IMM    27
15525 #define TEMPLATE_INL_DX     28
15526 #define TEMPLATE_INL_IMM    29
15527 #define TEMPLATE_OUTB_DX    30
15528 #define TEMPLATE_OUTB_IMM   31
15529 #define TEMPLATE_OUTW_DX    32
15530 #define TEMPLATE_OUTW_IMM   33
15531 #define TEMPLATE_OUTL_DX    34
15532 #define TEMPLATE_OUTL_IMM   35
15533 #define TEMPLATE_BSF        36
15534 #define TEMPLATE_RDMSR      37
15535 #define TEMPLATE_WRMSR      38
15536 #define LAST_TEMPLATE       TEMPLATE_WRMSR
15537 #if LAST_TEMPLATE >= MAX_TEMPLATES
15538 #error "MAX_TEMPLATES to low"
15539 #endif
15540
15541 #define COPY_REGCM (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8 | REGCM_MMX | REGCM_XMM)
15542 #define COPY32_REGCM (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
15543
15544 static struct ins_template templates[] = {
15545         [TEMPLATE_NOP]      = {},
15546         [TEMPLATE_INTCONST8] = { 
15547                 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15548         },
15549         [TEMPLATE_INTCONST32] = { 
15550                 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
15551         },
15552         [TEMPLATE_COPY_REG] = {
15553                 .lhs = { [0] = { REG_UNSET, COPY_REGCM } },
15554                 .rhs = { [0] = { REG_UNSET, COPY_REGCM }  },
15555         },
15556         [TEMPLATE_COPY_IMM32] = {
15557                 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
15558                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
15559         },
15560         [TEMPLATE_COPY_IMM16] = {
15561                 .lhs = { [0] = { REG_UNSET, COPY32_REGCM | REGCM_GPR16 } },
15562                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 } },
15563         },
15564         [TEMPLATE_COPY_IMM8] = {
15565                 .lhs = { [0] = { REG_UNSET, COPY_REGCM } },
15566                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15567         },
15568         [TEMPLATE_PHI] = { 
15569                 .lhs = { [0] = { REG_VIRT0, COPY_REGCM } },
15570                 .rhs = { 
15571                         [ 0] = { REG_VIRT0, COPY_REGCM },
15572                         [ 1] = { REG_VIRT0, COPY_REGCM },
15573                         [ 2] = { REG_VIRT0, COPY_REGCM },
15574                         [ 3] = { REG_VIRT0, COPY_REGCM },
15575                         [ 4] = { REG_VIRT0, COPY_REGCM },
15576                         [ 5] = { REG_VIRT0, COPY_REGCM },
15577                         [ 6] = { REG_VIRT0, COPY_REGCM },
15578                         [ 7] = { REG_VIRT0, COPY_REGCM },
15579                         [ 8] = { REG_VIRT0, COPY_REGCM },
15580                         [ 9] = { REG_VIRT0, COPY_REGCM },
15581                         [10] = { REG_VIRT0, COPY_REGCM },
15582                         [11] = { REG_VIRT0, COPY_REGCM },
15583                         [12] = { REG_VIRT0, COPY_REGCM },
15584                         [13] = { REG_VIRT0, COPY_REGCM },
15585                         [14] = { REG_VIRT0, COPY_REGCM },
15586                         [15] = { REG_VIRT0, COPY_REGCM },
15587                 }, },
15588         [TEMPLATE_STORE8] = {
15589                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15590                 .rhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15591         },
15592         [TEMPLATE_STORE16] = {
15593                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15594                 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
15595         },
15596         [TEMPLATE_STORE32] = {
15597                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15598                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15599         },
15600         [TEMPLATE_LOAD8] = {
15601                 .lhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15602                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15603         },
15604         [TEMPLATE_LOAD16] = {
15605                 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
15606                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15607         },
15608         [TEMPLATE_LOAD32] = {
15609                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15610                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15611         },
15612         [TEMPLATE_BINARY_REG] = {
15613                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15614                 .rhs = { 
15615                         [0] = { REG_VIRT0, REGCM_GPR32 },
15616                         [1] = { REG_UNSET, REGCM_GPR32 },
15617                 },
15618         },
15619         [TEMPLATE_BINARY_IMM] = {
15620                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15621                 .rhs = { 
15622                         [0] = { REG_VIRT0,    REGCM_GPR32 },
15623                         [1] = { REG_UNNEEDED, REGCM_IMM32 },
15624                 },
15625         },
15626         [TEMPLATE_SL_CL] = {
15627                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15628                 .rhs = { 
15629                         [0] = { REG_VIRT0, REGCM_GPR32 },
15630                         [1] = { REG_CL, REGCM_GPR8 },
15631                 },
15632         },
15633         [TEMPLATE_SL_IMM] = {
15634                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15635                 .rhs = { 
15636                         [0] = { REG_VIRT0,    REGCM_GPR32 },
15637                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
15638                 },
15639         },
15640         [TEMPLATE_UNARY] = {
15641                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15642                 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15643         },
15644         [TEMPLATE_CMP_REG] = {
15645                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15646                 .rhs = {
15647                         [0] = { REG_UNSET, REGCM_GPR32 },
15648                         [1] = { REG_UNSET, REGCM_GPR32 },
15649                 },
15650         },
15651         [TEMPLATE_CMP_IMM] = {
15652                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15653                 .rhs = {
15654                         [0] = { REG_UNSET, REGCM_GPR32 },
15655                         [1] = { REG_UNNEEDED, REGCM_IMM32 },
15656                 },
15657         },
15658         [TEMPLATE_TEST] = {
15659                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15660                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15661         },
15662         [TEMPLATE_SET] = {
15663                 .lhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15664                 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15665         },
15666         [TEMPLATE_JMP] = {
15667                 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15668         },
15669         [TEMPLATE_INB_DX] = {
15670                 .lhs = { [0] = { REG_AL,  REGCM_GPR8 } },  
15671                 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15672         },
15673         [TEMPLATE_INB_IMM] = {
15674                 .lhs = { [0] = { REG_AL,  REGCM_GPR8 } },  
15675                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15676         },
15677         [TEMPLATE_INW_DX]  = { 
15678                 .lhs = { [0] = { REG_AX,  REGCM_GPR16 } }, 
15679                 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15680         },
15681         [TEMPLATE_INW_IMM] = { 
15682                 .lhs = { [0] = { REG_AX,  REGCM_GPR16 } }, 
15683                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15684         },
15685         [TEMPLATE_INL_DX]  = {
15686                 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
15687                 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15688         },
15689         [TEMPLATE_INL_IMM] = {
15690                 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
15691                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15692         },
15693         [TEMPLATE_OUTB_DX] = { 
15694                 .rhs = {
15695                         [0] = { REG_AL,  REGCM_GPR8 },
15696                         [1] = { REG_DX, REGCM_GPR16 },
15697                 },
15698         },
15699         [TEMPLATE_OUTB_IMM] = { 
15700                 .rhs = {
15701                         [0] = { REG_AL,  REGCM_GPR8 },  
15702                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
15703                 },
15704         },
15705         [TEMPLATE_OUTW_DX] = { 
15706                 .rhs = {
15707                         [0] = { REG_AX,  REGCM_GPR16 },
15708                         [1] = { REG_DX, REGCM_GPR16 },
15709                 },
15710         },
15711         [TEMPLATE_OUTW_IMM] = {
15712                 .rhs = {
15713                         [0] = { REG_AX,  REGCM_GPR16 }, 
15714                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
15715                 },
15716         },
15717         [TEMPLATE_OUTL_DX] = { 
15718                 .rhs = {
15719                         [0] = { REG_EAX, REGCM_GPR32 },
15720                         [1] = { REG_DX, REGCM_GPR16 },
15721                 },
15722         },
15723         [TEMPLATE_OUTL_IMM] = { 
15724                 .rhs = {
15725                         [0] = { REG_EAX, REGCM_GPR32 }, 
15726                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
15727                 },
15728         },
15729         [TEMPLATE_BSF] = {
15730                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15731                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15732         },
15733         [TEMPLATE_RDMSR] = {
15734                 .lhs = { 
15735                         [0] = { REG_EAX, REGCM_GPR32 },
15736                         [1] = { REG_EDX, REGCM_GPR32 },
15737                 },
15738                 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
15739         },
15740         [TEMPLATE_WRMSR] = {
15741                 .rhs = {
15742                         [0] = { REG_ECX, REGCM_GPR32 },
15743                         [1] = { REG_EAX, REGCM_GPR32 },
15744                         [2] = { REG_EDX, REGCM_GPR32 },
15745                 },
15746         },
15747 };
15748
15749 static void fixup_branches(struct compile_state *state,
15750         struct triple *cmp, struct triple *use, int jmp_op)
15751 {
15752         struct triple_set *entry, *next;
15753         for(entry = use->use; entry; entry = next) {
15754                 next = entry->next;
15755                 if (entry->member->op == OP_COPY) {
15756                         fixup_branches(state, cmp, entry->member, jmp_op);
15757                 }
15758                 else if (entry->member->op == OP_BRANCH) {
15759                         struct triple *branch, *test;
15760                         struct triple *left, *right;
15761                         left = right = 0;
15762                         left = RHS(cmp, 0);
15763                         if (TRIPLE_RHS(cmp->sizes) > 1) {
15764                                 right = RHS(cmp, 1);
15765                         }
15766                         branch = entry->member;
15767                         test = pre_triple(state, branch,
15768                                 cmp->op, cmp->type, left, right);
15769                         test->template_id = TEMPLATE_TEST; 
15770                         if (cmp->op == OP_CMP) {
15771                                 test->template_id = TEMPLATE_CMP_REG;
15772                                 if (get_imm32(test, &RHS(test, 1))) {
15773                                         test->template_id = TEMPLATE_CMP_IMM;
15774                                 }
15775                         }
15776                         use_triple(RHS(test, 0), test);
15777                         use_triple(RHS(test, 1), test);
15778                         unuse_triple(RHS(branch, 0), branch);
15779                         RHS(branch, 0) = test;
15780                         branch->op = jmp_op;
15781                         branch->template_id = TEMPLATE_JMP;
15782                         use_triple(RHS(branch, 0), branch);
15783                 }
15784         }
15785 }
15786
15787 static void bool_cmp(struct compile_state *state, 
15788         struct triple *ins, int cmp_op, int jmp_op, int set_op)
15789 {
15790         struct triple_set *entry, *next;
15791         struct triple *set;
15792
15793         /* Put a barrier up before the cmp which preceeds the
15794          * copy instruction.  If a set actually occurs this gives
15795          * us a chance to move variables in registers out of the way.
15796          */
15797
15798         /* Modify the comparison operator */
15799         ins->op = cmp_op;
15800         ins->template_id = TEMPLATE_TEST;
15801         if (cmp_op == OP_CMP) {
15802                 ins->template_id = TEMPLATE_CMP_REG;
15803                 if (get_imm32(ins, &RHS(ins, 1))) {
15804                         ins->template_id =  TEMPLATE_CMP_IMM;
15805                 }
15806         }
15807         /* Generate the instruction sequence that will transform the
15808          * result of the comparison into a logical value.
15809          */
15810         set = post_triple(state, ins, set_op, ins->type, ins, 0);
15811         use_triple(ins, set);
15812         set->template_id = TEMPLATE_SET;
15813
15814         for(entry = ins->use; entry; entry = next) {
15815                 next = entry->next;
15816                 if (entry->member == set) {
15817                         continue;
15818                 }
15819                 replace_rhs_use(state, ins, set, entry->member);
15820         }
15821         fixup_branches(state, ins, set, jmp_op);
15822 }
15823
15824 static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
15825 {
15826         struct triple *next;
15827         int lhs, i;
15828         lhs = TRIPLE_LHS(ins->sizes);
15829         for(next = ins->next, i = 0; i < lhs; i++, next = next->next) {
15830                 if (next != LHS(ins, i)) {
15831                         internal_error(state, ins, "malformed lhs on %s",
15832                                 tops(ins->op));
15833                 }
15834                 if (next->op != OP_PIECE) {
15835                         internal_error(state, ins, "bad lhs op %s at %d on %s",
15836                                 tops(next->op), i, tops(ins->op));
15837                 }
15838                 if (next->u.cval != i) {
15839                         internal_error(state, ins, "bad u.cval of %d %d expected",
15840                                 next->u.cval, i);
15841                 }
15842         }
15843         return next;
15844 }
15845
15846 struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
15847 {
15848         struct ins_template *template;
15849         struct reg_info result;
15850         int zlhs;
15851         if (ins->op == OP_PIECE) {
15852                 index = ins->u.cval;
15853                 ins = MISC(ins, 0);
15854         }
15855         zlhs = TRIPLE_LHS(ins->sizes);
15856         if (triple_is_def(state, ins)) {
15857                 zlhs = 1;
15858         }
15859         if (index >= zlhs) {
15860                 internal_error(state, ins, "index %d out of range for %s\n",
15861                         index, tops(ins->op));
15862         }
15863         switch(ins->op) {
15864         case OP_ASM:
15865                 template = &ins->u.ainfo->tmpl;
15866                 break;
15867         default:
15868                 if (ins->template_id > LAST_TEMPLATE) {
15869                         internal_error(state, ins, "bad template number %d", 
15870                                 ins->template_id);
15871                 }
15872                 template = &templates[ins->template_id];
15873                 break;
15874         }
15875         result = template->lhs[index];
15876         result.regcm = arch_regcm_normalize(state, result.regcm);
15877         if (result.reg != REG_UNNEEDED) {
15878                 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
15879         }
15880         if (result.regcm == 0) {
15881                 internal_error(state, ins, "lhs %d regcm == 0", index);
15882         }
15883         return result;
15884 }
15885
15886 struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
15887 {
15888         struct reg_info result;
15889         struct ins_template *template;
15890         if ((index > TRIPLE_RHS(ins->sizes)) ||
15891                 (ins->op == OP_PIECE)) {
15892                 internal_error(state, ins, "index %d out of range for %s\n",
15893                         index, tops(ins->op));
15894         }
15895         switch(ins->op) {
15896         case OP_ASM:
15897                 template = &ins->u.ainfo->tmpl;
15898                 break;
15899         default:
15900                 if (ins->template_id > LAST_TEMPLATE) {
15901                         internal_error(state, ins, "bad template number %d", 
15902                                 ins->template_id);
15903                 }
15904                 template = &templates[ins->template_id];
15905                 break;
15906         }
15907         result = template->rhs[index];
15908         result.regcm = arch_regcm_normalize(state, result.regcm);
15909         if (result.regcm == 0) {
15910                 internal_error(state, ins, "rhs %d regcm == 0", index);
15911         }
15912         return result;
15913 }
15914
15915 static struct triple *transform_to_arch_instruction(
15916         struct compile_state *state, struct triple *ins)
15917 {
15918         /* Transform from generic 3 address instructions
15919          * to archtecture specific instructions.
15920          * And apply architecture specific constrains to instructions.
15921          * Copies are inserted to preserve the register flexibility
15922          * of 3 address instructions.
15923          */
15924         struct triple *next;
15925         next = ins->next;
15926         switch(ins->op) {
15927         case OP_INTCONST:
15928                 ins->template_id = TEMPLATE_INTCONST32;
15929                 if (ins->u.cval < 256) {
15930                         ins->template_id = TEMPLATE_INTCONST8;
15931                 }
15932                 break;
15933         case OP_ADDRCONST:
15934                 ins->template_id = TEMPLATE_INTCONST32;
15935                 break;
15936         case OP_NOOP:
15937         case OP_SDECL:
15938         case OP_BLOBCONST:
15939         case OP_LABEL:
15940                 ins->template_id = TEMPLATE_NOP;
15941                 break;
15942         case OP_COPY:
15943                 ins->template_id = TEMPLATE_COPY_REG;
15944                 if (is_imm8(RHS(ins, 0))) {
15945                         ins->template_id = TEMPLATE_COPY_IMM8;
15946                 }
15947                 else if (is_imm16(RHS(ins, 0))) {
15948                         ins->template_id = TEMPLATE_COPY_IMM16;
15949                 }
15950                 else if (is_imm32(RHS(ins, 0))) {
15951                         ins->template_id = TEMPLATE_COPY_IMM32;
15952                 }
15953                 else if (is_const(RHS(ins, 0))) {
15954                         internal_error(state, ins, "bad constant passed to copy");
15955                 }
15956                 break;
15957         case OP_PHI:
15958                 ins->template_id = TEMPLATE_PHI;
15959                 break;
15960         case OP_STORE:
15961                 switch(ins->type->type & TYPE_MASK) {
15962                 case TYPE_CHAR:    case TYPE_UCHAR:
15963                         ins->template_id = TEMPLATE_STORE8;
15964                         break;
15965                 case TYPE_SHORT:   case TYPE_USHORT:
15966                         ins->template_id = TEMPLATE_STORE16;
15967                         break;
15968                 case TYPE_INT:     case TYPE_UINT:
15969                 case TYPE_LONG:    case TYPE_ULONG:
15970                 case TYPE_POINTER:
15971                         ins->template_id = TEMPLATE_STORE32;
15972                         break;
15973                 default:
15974                         internal_error(state, ins, "unknown type in store");
15975                         break;
15976                 }
15977                 break;
15978         case OP_LOAD:
15979                 switch(ins->type->type & TYPE_MASK) {
15980                 case TYPE_CHAR:   case TYPE_UCHAR:
15981                         ins->template_id = TEMPLATE_LOAD8;
15982                         break;
15983                 case TYPE_SHORT:
15984                 case TYPE_USHORT:
15985                         ins->template_id = TEMPLATE_LOAD16;
15986                         break;
15987                 case TYPE_INT:
15988                 case TYPE_UINT:
15989                 case TYPE_LONG:
15990                 case TYPE_ULONG:
15991                 case TYPE_POINTER:
15992                         ins->template_id = TEMPLATE_LOAD32;
15993                         break;
15994                 default:
15995                         internal_error(state, ins, "unknown type in load");
15996                         break;
15997                 }
15998                 break;
15999         case OP_ADD:
16000         case OP_SUB:
16001         case OP_AND:
16002         case OP_XOR:
16003         case OP_OR:
16004         case OP_SMUL:
16005                 ins->template_id = TEMPLATE_BINARY_REG;
16006                 if (get_imm32(ins, &RHS(ins, 1))) {
16007                         ins->template_id = TEMPLATE_BINARY_IMM;
16008                 }
16009                 break;
16010         case OP_SL:
16011         case OP_SSR:
16012         case OP_USR:
16013                 ins->template_id = TEMPLATE_SL_CL;
16014                 if (get_imm8(ins, &RHS(ins, 1))) {
16015                         ins->template_id = TEMPLATE_SL_IMM;
16016                 }
16017                 break;
16018         case OP_INVERT:
16019         case OP_NEG:
16020                 ins->template_id = TEMPLATE_UNARY;
16021                 break;
16022         case OP_EQ: 
16023                 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ); 
16024                 break;
16025         case OP_NOTEQ:
16026                 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
16027                 break;
16028         case OP_SLESS:
16029                 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
16030                 break;
16031         case OP_ULESS:
16032                 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
16033                 break;
16034         case OP_SMORE:
16035                 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
16036                 break;
16037         case OP_UMORE:
16038                 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
16039                 break;
16040         case OP_SLESSEQ:
16041                 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
16042                 break;
16043         case OP_ULESSEQ:
16044                 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
16045                 break;
16046         case OP_SMOREEQ:
16047                 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
16048                 break;
16049         case OP_UMOREEQ:
16050                 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
16051                 break;
16052         case OP_LTRUE:
16053                 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
16054                 break;
16055         case OP_LFALSE:
16056                 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
16057                 break;
16058         case OP_BRANCH:
16059                 if (TRIPLE_RHS(ins->sizes) > 0) {
16060                         internal_error(state, ins, "bad branch test");
16061                 }
16062                 ins->op = OP_JMP;
16063                 ins->template_id = TEMPLATE_NOP;
16064                 break;
16065         case OP_INB:
16066         case OP_INW:
16067         case OP_INL:
16068                 switch(ins->op) {
16069                 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
16070                 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
16071                 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
16072                 }
16073                 if (get_imm8(ins, &RHS(ins, 0))) {
16074                         ins->template_id += 1;
16075                 }
16076                 break;
16077         case OP_OUTB:
16078         case OP_OUTW:
16079         case OP_OUTL:
16080                 switch(ins->op) {
16081                 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
16082                 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
16083                 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
16084                 }
16085                 if (get_imm8(ins, &RHS(ins, 1))) {
16086                         ins->template_id += 1;
16087                 }
16088                 break;
16089         case OP_BSF:
16090         case OP_BSR:
16091                 ins->template_id = TEMPLATE_BSF;
16092                 break;
16093         case OP_RDMSR:
16094                 ins->template_id = TEMPLATE_RDMSR;
16095                 next = after_lhs(state, ins);
16096                 break;
16097         case OP_WRMSR:
16098                 ins->template_id = TEMPLATE_WRMSR;
16099                 break;
16100         case OP_HLT:
16101                 ins->template_id = TEMPLATE_NOP;
16102                 break;
16103         case OP_ASM:
16104                 ins->template_id = TEMPLATE_NOP;
16105                 next = after_lhs(state, ins);
16106                 break;
16107                 /* Already transformed instructions */
16108         case OP_TEST:
16109                 ins->template_id = TEMPLATE_TEST;
16110                 break;
16111         case OP_CMP:
16112                 ins->template_id = TEMPLATE_CMP_REG;
16113                 if (get_imm32(ins, &RHS(ins, 1))) {
16114                         ins->template_id = TEMPLATE_CMP_IMM;
16115                 }
16116                 break;
16117         case OP_JMP_EQ:      case OP_JMP_NOTEQ:
16118         case OP_JMP_SLESS:   case OP_JMP_ULESS:
16119         case OP_JMP_SMORE:   case OP_JMP_UMORE:
16120         case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
16121         case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
16122                 ins->template_id = TEMPLATE_JMP;
16123                 break;
16124         case OP_SET_EQ:      case OP_SET_NOTEQ:
16125         case OP_SET_SLESS:   case OP_SET_ULESS:
16126         case OP_SET_SMORE:   case OP_SET_UMORE:
16127         case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
16128         case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
16129                 ins->template_id = TEMPLATE_SET;
16130                 break;
16131                 /* Unhandled instructions */
16132         case OP_PIECE:
16133         default:
16134                 internal_error(state, ins, "unhandled ins: %d %s\n",
16135                         ins->op, tops(ins->op));
16136                 break;
16137         }
16138         return next;
16139 }
16140
16141 static void generate_local_labels(struct compile_state *state)
16142 {
16143         struct triple *first, *label;
16144         int label_counter;
16145         label_counter = 0;
16146         first = RHS(state->main_function, 0);
16147         label = first;
16148         do {
16149                 if ((label->op == OP_LABEL) || 
16150                         (label->op == OP_SDECL)) {
16151                         if (label->use) {
16152                                 label->u.cval = ++label_counter;
16153                         } else {
16154                                 label->u.cval = 0;
16155                         }
16156                         
16157                 }
16158                 label = label->next;
16159         } while(label != first);
16160 }
16161
16162 static int check_reg(struct compile_state *state, 
16163         struct triple *triple, int classes)
16164 {
16165         unsigned mask;
16166         int reg;
16167         reg = ID_REG(triple->id);
16168         if (reg == REG_UNSET) {
16169                 internal_error(state, triple, "register not set");
16170         }
16171         mask = arch_reg_regcm(state, reg);
16172         if (!(classes & mask)) {
16173                 internal_error(state, triple, "reg %d in wrong class",
16174                         reg);
16175         }
16176         return reg;
16177 }
16178
16179 static const char *arch_reg_str(int reg)
16180 {
16181         static const char *regs[] = {
16182                 "%unset",
16183                 "%unneeded",
16184                 "%eflags",
16185                 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
16186                 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
16187                 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
16188                 "%edx:%eax",
16189                 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
16190                 "%xmm0", "%xmm1", "%xmm2", "%xmm3", 
16191                 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
16192         };
16193         if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
16194                 reg = 0;
16195         }
16196         return regs[reg];
16197 }
16198
16199
16200 static const char *reg(struct compile_state *state, struct triple *triple,
16201         int classes)
16202 {
16203         int reg;
16204         reg = check_reg(state, triple, classes);
16205         return arch_reg_str(reg);
16206 }
16207
16208 const char *type_suffix(struct compile_state *state, struct type *type)
16209 {
16210         const char *suffix;
16211         switch(size_of(state, type)) {
16212         case 1: suffix = "b"; break;
16213         case 2: suffix = "w"; break;
16214         case 4: suffix = "l"; break;
16215         default:
16216                 internal_error(state, 0, "unknown suffix");
16217                 suffix = 0;
16218                 break;
16219         }
16220         return suffix;
16221 }
16222
16223 static void print_const_val(
16224         struct compile_state *state, struct triple *ins, FILE *fp)
16225 {
16226         switch(ins->op) {
16227         case OP_INTCONST:
16228                 fprintf(fp, " $%ld ", 
16229                         (long_t)(ins->u.cval));
16230                 break;
16231         case OP_ADDRCONST:
16232                 fprintf(fp, " $L%s%lu+%lu ",
16233                         state->label_prefix, 
16234                         MISC(ins, 0)->u.cval,
16235                         ins->u.cval);
16236                 break;
16237         default:
16238                 internal_error(state, ins, "unknown constant type");
16239                 break;
16240         }
16241 }
16242
16243 static void print_binary_op(struct compile_state *state,
16244         const char *op, struct triple *ins, FILE *fp) 
16245 {
16246         unsigned mask;
16247         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16248         if (RHS(ins, 0)->id != ins->id) {
16249                 internal_error(state, ins, "invalid register assignment");
16250         }
16251         if (is_const(RHS(ins, 1))) {
16252                 fprintf(fp, "\t%s ", op);
16253                 print_const_val(state, RHS(ins, 1), fp);
16254                 fprintf(fp, ", %s\n",
16255                         reg(state, RHS(ins, 0), mask));
16256         }
16257         else {
16258                 unsigned lmask, rmask;
16259                 int lreg, rreg;
16260                 lreg = check_reg(state, RHS(ins, 0), mask);
16261                 rreg = check_reg(state, RHS(ins, 1), mask);
16262                 lmask = arch_reg_regcm(state, lreg);
16263                 rmask = arch_reg_regcm(state, rreg);
16264                 mask = lmask & rmask;
16265                 fprintf(fp, "\t%s %s, %s\n",
16266                         op,
16267                         reg(state, RHS(ins, 1), mask),
16268                         reg(state, RHS(ins, 0), mask));
16269         }
16270 }
16271 static void print_unary_op(struct compile_state *state, 
16272         const char *op, struct triple *ins, FILE *fp)
16273 {
16274         unsigned mask;
16275         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16276         fprintf(fp, "\t%s %s\n",
16277                 op,
16278                 reg(state, RHS(ins, 0), mask));
16279 }
16280
16281 static void print_op_shift(struct compile_state *state,
16282         const char *op, struct triple *ins, FILE *fp)
16283 {
16284         unsigned mask;
16285         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16286         if (RHS(ins, 0)->id != ins->id) {
16287                 internal_error(state, ins, "invalid register assignment");
16288         }
16289         if (is_const(RHS(ins, 1))) {
16290                 fprintf(fp, "\t%s ", op);
16291                 print_const_val(state, RHS(ins, 1), fp);
16292                 fprintf(fp, ", %s\n",
16293                         reg(state, RHS(ins, 0), mask));
16294         }
16295         else {
16296                 fprintf(fp, "\t%s %s, %s\n",
16297                         op,
16298                         reg(state, RHS(ins, 1), REGCM_GPR8),
16299                         reg(state, RHS(ins, 0), mask));
16300         }
16301 }
16302
16303 static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
16304 {
16305         const char *op;
16306         int mask;
16307         int dreg;
16308         mask = 0;
16309         switch(ins->op) {
16310         case OP_INB: op = "inb", mask = REGCM_GPR8; break;
16311         case OP_INW: op = "inw", mask = REGCM_GPR16; break;
16312         case OP_INL: op = "inl", mask = REGCM_GPR32; break;
16313         default:
16314                 internal_error(state, ins, "not an in operation");
16315                 op = 0;
16316                 break;
16317         }
16318         dreg = check_reg(state, ins, mask);
16319         if (!reg_is_reg(state, dreg, REG_EAX)) {
16320                 internal_error(state, ins, "dst != %%eax");
16321         }
16322         if (is_const(RHS(ins, 0))) {
16323                 fprintf(fp, "\t%s ", op);
16324                 print_const_val(state, RHS(ins, 0), fp);
16325                 fprintf(fp, ", %s\n",
16326                         reg(state, ins, mask));
16327         }
16328         else {
16329                 int addr_reg;
16330                 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
16331                 if (!reg_is_reg(state, addr_reg, REG_DX)) {
16332                         internal_error(state, ins, "src != %%dx");
16333                 }
16334                 fprintf(fp, "\t%s %s, %s\n",
16335                         op, 
16336                         reg(state, RHS(ins, 0), REGCM_GPR16),
16337                         reg(state, ins, mask));
16338         }
16339 }
16340
16341 static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
16342 {
16343         const char *op;
16344         int mask;
16345         int lreg;
16346         mask = 0;
16347         switch(ins->op) {
16348         case OP_OUTB: op = "outb", mask = REGCM_GPR8; break;
16349         case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
16350         case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
16351         default:
16352                 internal_error(state, ins, "not an out operation");
16353                 op = 0;
16354                 break;
16355         }
16356         lreg = check_reg(state, RHS(ins, 0), mask);
16357         if (!reg_is_reg(state, lreg, REG_EAX)) {
16358                 internal_error(state, ins, "src != %%eax");
16359         }
16360         if (is_const(RHS(ins, 1))) {
16361                 fprintf(fp, "\t%s %s,", 
16362                         op, reg(state, RHS(ins, 0), mask));
16363                 print_const_val(state, RHS(ins, 1), fp);
16364                 fprintf(fp, "\n");
16365         }
16366         else {
16367                 int addr_reg;
16368                 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
16369                 if (!reg_is_reg(state, addr_reg, REG_DX)) {
16370                         internal_error(state, ins, "dst != %%dx");
16371                 }
16372                 fprintf(fp, "\t%s %s, %s\n",
16373                         op, 
16374                         reg(state, RHS(ins, 0), mask),
16375                         reg(state, RHS(ins, 1), REGCM_GPR16));
16376         }
16377 }
16378
16379 static void print_op_move(struct compile_state *state,
16380         struct triple *ins, FILE *fp)
16381 {
16382         /* op_move is complex because there are many types
16383          * of registers we can move between.
16384          * Because OP_COPY will be introduced in arbitrary locations
16385          * OP_COPY must not affect flags.
16386          */
16387         int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
16388         struct triple *dst, *src;
16389         if (ins->op == OP_COPY) {
16390                 src = RHS(ins, 0);
16391                 dst = ins;
16392         }
16393         else if (ins->op == OP_WRITE) {
16394                 dst = LHS(ins, 0);
16395                 src = RHS(ins, 0);
16396         }
16397         else {
16398                 internal_error(state, ins, "unknown move operation");
16399                 src = dst = 0;
16400         }
16401         if (!is_const(src)) {
16402                 int src_reg, dst_reg;
16403                 int src_regcm, dst_regcm;
16404                 src_reg = ID_REG(src->id);
16405                 dst_reg   = ID_REG(dst->id);
16406                 src_regcm = arch_reg_regcm(state, src_reg);
16407                 dst_regcm   = arch_reg_regcm(state, dst_reg);
16408                 /* If the class is the same just move the register */
16409                 if (src_regcm & dst_regcm & 
16410                         (REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32)) {
16411                         if ((src_reg != dst_reg) || !omit_copy) {
16412                                 fprintf(fp, "\tmov %s, %s\n",
16413                                         reg(state, src, src_regcm),
16414                                         reg(state, dst, dst_regcm));
16415                         }
16416                 }
16417                 /* Move 32bit to 16bit */
16418                 else if ((src_regcm & REGCM_GPR32) &&
16419                         (dst_regcm & REGCM_GPR16)) {
16420                         src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
16421                         if ((src_reg != dst_reg) || !omit_copy) {
16422                                 fprintf(fp, "\tmovw %s, %s\n",
16423                                         arch_reg_str(src_reg), 
16424                                         arch_reg_str(dst_reg));
16425                         }
16426                 }
16427                 /* Move 32bit to 8bit */
16428                 else if ((src_regcm & REGCM_GPR32_8) &&
16429                         (dst_regcm & REGCM_GPR8))
16430                 {
16431                         src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
16432                         if ((src_reg != dst_reg) || !omit_copy) {
16433                                 fprintf(fp, "\tmovb %s, %s\n",
16434                                         arch_reg_str(src_reg),
16435                                         arch_reg_str(dst_reg));
16436                         }
16437                 }
16438                 /* Move 16bit to 8bit */
16439                 else if ((src_regcm & REGCM_GPR16_8) &&
16440                         (dst_regcm & REGCM_GPR8))
16441                 {
16442                         src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
16443                         if ((src_reg != dst_reg) || !omit_copy) {
16444                                 fprintf(fp, "\tmovb %s, %s\n",
16445                                         arch_reg_str(src_reg),
16446                                         arch_reg_str(dst_reg));
16447                         }
16448                 }
16449                 /* Move 8/16bit to 16/32bit */
16450                 else if ((src_regcm & (REGCM_GPR8 | REGCM_GPR16)) && 
16451                         (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
16452                         const char *op;
16453                         op = is_signed(src->type)? "movsx": "movzx";
16454                         fprintf(fp, "\t%s %s, %s\n",
16455                                 op,
16456                                 reg(state, src, src_regcm),
16457                                 reg(state, dst, dst_regcm));
16458                 }
16459                 /* Move between sse registers */
16460                 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
16461                         if ((src_reg != dst_reg) || !omit_copy) {
16462                                 fprintf(fp, "\tmovdqa %s, %s\n",
16463                                         reg(state, src, src_regcm),
16464                                         reg(state, dst, dst_regcm));
16465                         }
16466                 }
16467                 /* Move between mmx registers or mmx & sse  registers */
16468                 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
16469                         (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
16470                         if ((src_reg != dst_reg) || !omit_copy) {
16471                                 fprintf(fp, "\tmovq %s, %s\n",
16472                                         reg(state, src, src_regcm),
16473                                         reg(state, dst, dst_regcm));
16474                         }
16475                 }
16476                 /* Move between 32bit gprs & mmx/sse registers */
16477                 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
16478                         (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
16479                         fprintf(fp, "\tmovd %s, %s\n",
16480                                 reg(state, src, src_regcm),
16481                                 reg(state, dst, dst_regcm));
16482                 }
16483 #if X86_4_8BIT_GPRS
16484                 /* Move from 8bit gprs to  mmx/sse registers */
16485                 else if ((src_regcm & REGCM_GPR8) && (src_reg <= REG_DL) &&
16486                         (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
16487                         const char *op;
16488                         int mid_reg;
16489                         op = is_signed(src->type)? "movsx":"movzx";
16490                         mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16491                         fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
16492                                 op,
16493                                 reg(state, src, src_regcm),
16494                                 arch_reg_str(mid_reg),
16495                                 arch_reg_str(mid_reg),
16496                                 reg(state, dst, dst_regcm));
16497                 }
16498                 /* Move from mmx/sse registers and 8bit gprs */
16499                 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
16500                         (dst_regcm & REGCM_GPR8) && (dst_reg <= REG_DL)) {
16501                         int mid_reg;
16502                         mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16503                         fprintf(fp, "\tmovd %s, %s\n",
16504                                 reg(state, src, src_regcm),
16505                                 arch_reg_str(mid_reg));
16506                 }
16507                 /* Move from 32bit gprs to 16bit gprs */
16508                 else if ((src_regcm & REGCM_GPR32) &&
16509                         (dst_regcm & REGCM_GPR16)) {
16510                         dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
16511                         if ((src_reg != dst_reg) || !omit_copy) {
16512                                 fprintf(fp, "\tmov %s, %s\n",
16513                                         arch_reg_str(src_reg),
16514                                         arch_reg_str(dst_reg));
16515                         }
16516                 }
16517                 /* Move from 32bit gprs to 8bit gprs */
16518                 else if ((src_regcm & REGCM_GPR32) &&
16519                         (dst_regcm & REGCM_GPR8)) {
16520                         dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16521                         if ((src_reg != dst_reg) || !omit_copy) {
16522                                 fprintf(fp, "\tmov %s, %s\n",
16523                                         arch_reg_str(src_reg),
16524                                         arch_reg_str(dst_reg));
16525                         }
16526                 }
16527                 /* Move from 16bit gprs to 8bit gprs */
16528                 else if ((src_regcm & REGCM_GPR16) &&
16529                         (dst_regcm & REGCM_GPR8)) {
16530                         dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
16531                         if ((src_reg != dst_reg) || !omit_copy) {
16532                                 fprintf(fp, "\tmov %s, %s\n",
16533                                         arch_reg_str(src_reg),
16534                                         arch_reg_str(dst_reg));
16535                         }
16536                 }
16537 #endif /* X86_4_8BIT_GPRS */
16538                 else {
16539                         internal_error(state, ins, "unknown copy type");
16540                 }
16541         }
16542         else {
16543                 fprintf(fp, "\tmov ");
16544                 print_const_val(state, src, fp);
16545                 fprintf(fp, ", %s\n",
16546                         reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8));
16547         }
16548 }
16549
16550 static void print_op_load(struct compile_state *state,
16551         struct triple *ins, FILE *fp)
16552 {
16553         struct triple *dst, *src;
16554         dst = ins;
16555         src = RHS(ins, 0);
16556         if (is_const(src) || is_const(dst)) {
16557                 internal_error(state, ins, "unknown load operation");
16558         }
16559         fprintf(fp, "\tmov (%s), %s\n",
16560                 reg(state, src, REGCM_GPR32),
16561                 reg(state, dst, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32));
16562 }
16563
16564
16565 static void print_op_store(struct compile_state *state,
16566         struct triple *ins, FILE *fp)
16567 {
16568         struct triple *dst, *src;
16569         dst = LHS(ins, 0);
16570         src = RHS(ins, 0);
16571         if (is_const(src) && (src->op == OP_INTCONST)) {
16572                 long_t value;
16573                 value = (long_t)(src->u.cval);
16574                 fprintf(fp, "\tmov%s $%ld, (%s)\n",
16575                         type_suffix(state, src->type),
16576                         value,
16577                         reg(state, dst, REGCM_GPR32));
16578         }
16579         else if (is_const(dst) && (dst->op == OP_INTCONST)) {
16580                 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
16581                         type_suffix(state, src->type),
16582                         reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
16583                         dst->u.cval);
16584         }
16585         else {
16586                 if (is_const(src) || is_const(dst)) {
16587                         internal_error(state, ins, "unknown store operation");
16588                 }
16589                 fprintf(fp, "\tmov%s %s, (%s)\n",
16590                         type_suffix(state, src->type),
16591                         reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
16592                         reg(state, dst, REGCM_GPR32));
16593         }
16594         
16595         
16596 }
16597
16598 static void print_op_smul(struct compile_state *state,
16599         struct triple *ins, FILE *fp)
16600 {
16601         if (!is_const(RHS(ins, 1))) {
16602                 fprintf(fp, "\timul %s, %s\n",
16603                         reg(state, RHS(ins, 1), REGCM_GPR32),
16604                         reg(state, RHS(ins, 0), REGCM_GPR32));
16605         }
16606         else {
16607                 fprintf(fp, "\timul ");
16608                 print_const_val(state, RHS(ins, 1), fp);
16609                 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
16610         }
16611 }
16612
16613 static void print_op_cmp(struct compile_state *state,
16614         struct triple *ins, FILE *fp)
16615 {
16616         unsigned mask;
16617         int dreg;
16618         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16619         dreg = check_reg(state, ins, REGCM_FLAGS);
16620         if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
16621                 internal_error(state, ins, "bad dest register for cmp");
16622         }
16623         if (is_const(RHS(ins, 1))) {
16624                 fprintf(fp, "\tcmp ");
16625                 print_const_val(state, RHS(ins, 1), fp);
16626                 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
16627         }
16628         else {
16629                 unsigned lmask, rmask;
16630                 int lreg, rreg;
16631                 lreg = check_reg(state, RHS(ins, 0), mask);
16632                 rreg = check_reg(state, RHS(ins, 1), mask);
16633                 lmask = arch_reg_regcm(state, lreg);
16634                 rmask = arch_reg_regcm(state, rreg);
16635                 mask = lmask & rmask;
16636                 fprintf(fp, "\tcmp %s, %s\n",
16637                         reg(state, RHS(ins, 1), mask),
16638                         reg(state, RHS(ins, 0), mask));
16639         }
16640 }
16641
16642 static void print_op_test(struct compile_state *state,
16643         struct triple *ins, FILE *fp)
16644 {
16645         unsigned mask;
16646         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16647         fprintf(fp, "\ttest %s, %s\n",
16648                 reg(state, RHS(ins, 0), mask),
16649                 reg(state, RHS(ins, 0), mask));
16650 }
16651
16652 static void print_op_branch(struct compile_state *state,
16653         struct triple *branch, FILE *fp)
16654 {
16655         const char *bop = "j";
16656         if (branch->op == OP_JMP) {
16657                 if (TRIPLE_RHS(branch->sizes) != 0) {
16658                         internal_error(state, branch, "jmp with condition?");
16659                 }
16660                 bop = "jmp";
16661         }
16662         else {
16663                 struct triple *ptr;
16664                 if (TRIPLE_RHS(branch->sizes) != 1) {
16665                         internal_error(state, branch, "jmpcc without condition?");
16666                 }
16667                 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
16668                 if ((RHS(branch, 0)->op != OP_CMP) &&
16669                         (RHS(branch, 0)->op != OP_TEST)) {
16670                         internal_error(state, branch, "bad branch test");
16671                 }
16672 #warning "FIXME I have observed instructions between the test and branch instructions"
16673                 ptr = RHS(branch, 0);
16674                 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
16675                         if (ptr->op != OP_COPY) {
16676                                 internal_error(state, branch, "branch does not follow test");
16677                         }
16678                 }
16679                 switch(branch->op) {
16680                 case OP_JMP_EQ:       bop = "jz";  break;
16681                 case OP_JMP_NOTEQ:    bop = "jnz"; break;
16682                 case OP_JMP_SLESS:    bop = "jl";  break;
16683                 case OP_JMP_ULESS:    bop = "jb";  break;
16684                 case OP_JMP_SMORE:    bop = "jg";  break;
16685                 case OP_JMP_UMORE:    bop = "ja";  break;
16686                 case OP_JMP_SLESSEQ:  bop = "jle"; break;
16687                 case OP_JMP_ULESSEQ:  bop = "jbe"; break;
16688                 case OP_JMP_SMOREEQ:  bop = "jge"; break;
16689                 case OP_JMP_UMOREEQ:  bop = "jae"; break;
16690                 default:
16691                         internal_error(state, branch, "Invalid branch op");
16692                         break;
16693                 }
16694                 
16695         }
16696         fprintf(fp, "\t%s L%s%lu\n",
16697                 bop, 
16698                 state->label_prefix,
16699                 TARG(branch, 0)->u.cval);
16700 }
16701
16702 static void print_op_set(struct compile_state *state,
16703         struct triple *set, FILE *fp)
16704 {
16705         const char *sop = "set";
16706         if (TRIPLE_RHS(set->sizes) != 1) {
16707                 internal_error(state, set, "setcc without condition?");
16708         }
16709         check_reg(state, RHS(set, 0), REGCM_FLAGS);
16710         if ((RHS(set, 0)->op != OP_CMP) &&
16711                 (RHS(set, 0)->op != OP_TEST)) {
16712                 internal_error(state, set, "bad set test");
16713         }
16714         if (RHS(set, 0)->next != set) {
16715                 internal_error(state, set, "set does not follow test");
16716         }
16717         switch(set->op) {
16718         case OP_SET_EQ:       sop = "setz";  break;
16719         case OP_SET_NOTEQ:    sop = "setnz"; break;
16720         case OP_SET_SLESS:    sop = "setl";  break;
16721         case OP_SET_ULESS:    sop = "setb";  break;
16722         case OP_SET_SMORE:    sop = "setg";  break;
16723         case OP_SET_UMORE:    sop = "seta";  break;
16724         case OP_SET_SLESSEQ:  sop = "setle"; break;
16725         case OP_SET_ULESSEQ:  sop = "setbe"; break;
16726         case OP_SET_SMOREEQ:  sop = "setge"; break;
16727         case OP_SET_UMOREEQ:  sop = "setae"; break;
16728         default:
16729                 internal_error(state, set, "Invalid set op");
16730                 break;
16731         }
16732         fprintf(fp, "\t%s %s\n",
16733                 sop, reg(state, set, REGCM_GPR8));
16734 }
16735
16736 static void print_op_bit_scan(struct compile_state *state, 
16737         struct triple *ins, FILE *fp) 
16738 {
16739         const char *op;
16740         switch(ins->op) {
16741         case OP_BSF: op = "bsf"; break;
16742         case OP_BSR: op = "bsr"; break;
16743         default: 
16744                 internal_error(state, ins, "unknown bit scan");
16745                 op = 0;
16746                 break;
16747         }
16748         fprintf(fp, 
16749                 "\t%s %s, %s\n"
16750                 "\tjnz 1f\n"
16751                 "\tmovl $-1, %s\n"
16752                 "1:\n",
16753                 op,
16754                 reg(state, RHS(ins, 0), REGCM_GPR32),
16755                 reg(state, ins, REGCM_GPR32),
16756                 reg(state, ins, REGCM_GPR32));
16757 }
16758
16759 static void print_const(struct compile_state *state,
16760         struct triple *ins, FILE *fp)
16761 {
16762         switch(ins->op) {
16763         case OP_INTCONST:
16764                 switch(ins->type->type & TYPE_MASK) {
16765                 case TYPE_CHAR:
16766                 case TYPE_UCHAR:
16767                         fprintf(fp, ".byte 0x%02lx\n", ins->u.cval);
16768                         break;
16769                 case TYPE_SHORT:
16770                 case TYPE_USHORT:
16771                         fprintf(fp, ".short 0x%04lx\n", ins->u.cval);
16772                         break;
16773                 case TYPE_INT:
16774                 case TYPE_UINT:
16775                 case TYPE_LONG:
16776                 case TYPE_ULONG:
16777                         fprintf(fp, ".int %lu\n", ins->u.cval);
16778                         break;
16779                 default:
16780                         internal_error(state, ins, "Unknown constant type");
16781                 }
16782                 break;
16783         case OP_BLOBCONST:
16784         {
16785                 unsigned char *blob;
16786                 size_t size, i;
16787                 size = size_of(state, ins->type);
16788                 blob = ins->u.blob;
16789                 for(i = 0; i < size; i++) {
16790                         fprintf(fp, ".byte 0x%02x\n",
16791                                 blob[i]);
16792                 }
16793                 break;
16794         }
16795         default:
16796                 internal_error(state, ins, "Unknown constant type");
16797                 break;
16798         }
16799 }
16800
16801 #define TEXT_SECTION ".rom.text"
16802 #define DATA_SECTION ".rom.data"
16803
16804 static void print_sdecl(struct compile_state *state,
16805         struct triple *ins, FILE *fp)
16806 {
16807         fprintf(fp, ".section \"" DATA_SECTION "\"\n");
16808         fprintf(fp, ".balign %d\n", align_of(state, ins->type));
16809         fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
16810         print_const(state, MISC(ins, 0), fp);
16811         fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
16812                 
16813 }
16814
16815 static void print_instruction(struct compile_state *state,
16816         struct triple *ins, FILE *fp)
16817 {
16818         /* Assumption: after I have exted the register allocator
16819          * everything is in a valid register. 
16820          */
16821         switch(ins->op) {
16822         case OP_ASM:
16823                 print_op_asm(state, ins, fp);
16824                 break;
16825         case OP_ADD:    print_binary_op(state, "add", ins, fp); break;
16826         case OP_SUB:    print_binary_op(state, "sub", ins, fp); break;
16827         case OP_AND:    print_binary_op(state, "and", ins, fp); break;
16828         case OP_XOR:    print_binary_op(state, "xor", ins, fp); break;
16829         case OP_OR:     print_binary_op(state, "or",  ins, fp); break;
16830         case OP_SL:     print_op_shift(state, "shl", ins, fp); break;
16831         case OP_USR:    print_op_shift(state, "shr", ins, fp); break;
16832         case OP_SSR:    print_op_shift(state, "sar", ins, fp); break;
16833         case OP_POS:    break;
16834         case OP_NEG:    print_unary_op(state, "neg", ins, fp); break;
16835         case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
16836         case OP_INTCONST:
16837         case OP_ADDRCONST:
16838         case OP_BLOBCONST:
16839                 /* Don't generate anything here for constants */
16840         case OP_PHI:
16841                 /* Don't generate anything for variable declarations. */
16842                 break;
16843         case OP_SDECL:
16844                 print_sdecl(state, ins, fp);
16845                 break;
16846         case OP_WRITE: 
16847         case OP_COPY:   
16848                 print_op_move(state, ins, fp);
16849                 break;
16850         case OP_LOAD:
16851                 print_op_load(state, ins, fp);
16852                 break;
16853         case OP_STORE:
16854                 print_op_store(state, ins, fp);
16855                 break;
16856         case OP_SMUL:
16857                 print_op_smul(state, ins, fp);
16858                 break;
16859         case OP_CMP:    print_op_cmp(state, ins, fp); break;
16860         case OP_TEST:   print_op_test(state, ins, fp); break;
16861         case OP_JMP:
16862         case OP_JMP_EQ:      case OP_JMP_NOTEQ:
16863         case OP_JMP_SLESS:   case OP_JMP_ULESS:
16864         case OP_JMP_SMORE:   case OP_JMP_UMORE:
16865         case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
16866         case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
16867                 print_op_branch(state, ins, fp);
16868                 break;
16869         case OP_SET_EQ:      case OP_SET_NOTEQ:
16870         case OP_SET_SLESS:   case OP_SET_ULESS:
16871         case OP_SET_SMORE:   case OP_SET_UMORE:
16872         case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
16873         case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
16874                 print_op_set(state, ins, fp);
16875                 break;
16876         case OP_INB:  case OP_INW:  case OP_INL:
16877                 print_op_in(state, ins, fp); 
16878                 break;
16879         case OP_OUTB: case OP_OUTW: case OP_OUTL:
16880                 print_op_out(state, ins, fp); 
16881                 break;
16882         case OP_BSF:
16883         case OP_BSR:
16884                 print_op_bit_scan(state, ins, fp);
16885                 break;
16886         case OP_RDMSR:
16887                 after_lhs(state, ins);
16888                 fprintf(fp, "\trdmsr\n");
16889                 break;
16890         case OP_WRMSR:
16891                 fprintf(fp, "\twrmsr\n");
16892                 break;
16893         case OP_HLT:
16894                 fprintf(fp, "\thlt\n");
16895                 break;
16896         case OP_LABEL:
16897                 if (!ins->use) {
16898                         return;
16899                 }
16900                 fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
16901                 break;
16902                 /* Ignore OP_PIECE */
16903         case OP_PIECE:
16904                 break;
16905                 /* Operations I am not yet certain how to handle */
16906         case OP_UMUL:
16907         case OP_SDIV: case OP_UDIV:
16908         case OP_SMOD: case OP_UMOD:
16909                 /* Operations that should never get here */
16910         case OP_LTRUE:   case OP_LFALSE:  case OP_EQ:      case OP_NOTEQ:
16911         case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
16912         case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
16913         default:
16914                 internal_error(state, ins, "unknown op: %d %s",
16915                         ins->op, tops(ins->op));
16916                 break;
16917         }
16918 }
16919
16920 static void print_instructions(struct compile_state *state)
16921 {
16922         struct triple *first, *ins;
16923         int print_location;
16924         struct occurance *last_occurance;
16925         FILE *fp;
16926         print_location = 1;
16927         last_occurance = 0;
16928         fp = state->output;
16929         fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
16930         first = RHS(state->main_function, 0);
16931         ins = first;
16932         do {
16933                 if (print_location && 
16934                         last_occurance != ins->occurance) {
16935                         if (!ins->occurance->parent) {
16936                                 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
16937                                         ins->occurance->function,
16938                                         ins->occurance->filename,
16939                                         ins->occurance->line,
16940                                         ins->occurance->col);
16941                         }
16942                         else {
16943                                 struct occurance *ptr;
16944                                 fprintf(fp, "\t/*\n");
16945                                 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
16946                                         fprintf(fp, "\t * %s,%s:%d.%d\n",
16947                                                 ptr->function,
16948                                                 ptr->filename,
16949                                                 ptr->line,
16950                                                 ptr->col);
16951                                 }
16952                                 fprintf(fp, "\t */\n");
16953                                 
16954                         }
16955                         if (last_occurance) {
16956                                 put_occurance(last_occurance);
16957                         }
16958                         get_occurance(ins->occurance);
16959                         last_occurance = ins->occurance;
16960                 }
16961
16962                 print_instruction(state, ins, fp);
16963                 ins = ins->next;
16964         } while(ins != first);
16965         
16966 }
16967 static void generate_code(struct compile_state *state)
16968 {
16969         generate_local_labels(state);
16970         print_instructions(state);
16971         
16972 }
16973
16974 static void print_tokens(struct compile_state *state)
16975 {
16976         struct token *tk;
16977         tk = &state->token[0];
16978         do {
16979 #if 1
16980                 token(state, 0);
16981 #else
16982                 next_token(state, 0);
16983 #endif
16984                 loc(stdout, state, 0);
16985                 printf("%s <- `%s'\n",
16986                         tokens[tk->tok],
16987                         tk->ident ? tk->ident->name :
16988                         tk->str_len ? tk->val.str : "");
16989                 
16990         } while(tk->tok != TOK_EOF);
16991 }
16992
16993 static void compile(const char *filename, const char *ofilename, 
16994         int cpu, int debug, int opt, const char *label_prefix)
16995 {
16996         int i;
16997         struct compile_state state;
16998         memset(&state, 0, sizeof(state));
16999         state.file = 0;
17000         for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
17001                 memset(&state.token[i], 0, sizeof(state.token[i]));
17002                 state.token[i].tok = -1;
17003         }
17004         /* Remember the debug settings */
17005         state.cpu      = cpu;
17006         state.debug    = debug;
17007         state.optimize = opt;
17008         /* Remember the output filename */
17009         state.ofilename = ofilename;
17010         state.output    = fopen(state.ofilename, "w");
17011         if (!state.output) {
17012                 error(&state, 0, "Cannot open output file %s\n",
17013                         ofilename);
17014         }
17015         /* Remember the label prefix */
17016         state.label_prefix = label_prefix;
17017         /* Prep the preprocessor */
17018         state.if_depth = 0;
17019         state.if_value = 0;
17020         /* register the C keywords */
17021         register_keywords(&state);
17022         /* register the keywords the macro preprocessor knows */
17023         register_macro_keywords(&state);
17024         /* Memorize where some special keywords are. */
17025         state.i_continue = lookup(&state, "continue", 8);
17026         state.i_break    = lookup(&state, "break", 5);
17027         /* Enter the globl definition scope */
17028         start_scope(&state);
17029         register_builtins(&state);
17030         compile_file(&state, filename, 1);
17031 #if 0
17032         print_tokens(&state);
17033 #endif  
17034         decls(&state);
17035         /* Exit the global definition scope */
17036         end_scope(&state);
17037
17038         /* Now that basic compilation has happened 
17039          * optimize the intermediate code 
17040          */
17041         optimize(&state);
17042
17043         generate_code(&state);
17044         if (state.debug) {
17045                 fprintf(stderr, "done\n");
17046         }
17047 }
17048
17049 static void version(void)
17050 {
17051         printf("romcc " VERSION " released " RELEASE_DATE "\n");
17052 }
17053
17054 static void usage(void)
17055 {
17056         version();
17057         printf(
17058                 "Usage: romcc <source>.c\n"
17059                 "Compile a C source file without using ram\n"
17060         );
17061 }
17062
17063 static void arg_error(char *fmt, ...)
17064 {
17065         va_list args;
17066         va_start(args, fmt);
17067         vfprintf(stderr, fmt, args);
17068         va_end(args);
17069         usage();
17070         exit(1);
17071 }
17072
17073 int main(int argc, char **argv)
17074 {
17075         const char *filename;
17076         const char *ofilename;
17077         const char *label_prefix;
17078         int cpu;
17079         int last_argc;
17080         int debug;
17081         int optimize;
17082         cpu = CPU_DEFAULT;
17083         label_prefix = "";
17084         ofilename = "auto.inc";
17085         optimize = 0;
17086         debug = 0;
17087         last_argc = -1;
17088         while((argc > 1) && (argc != last_argc)) {
17089                 last_argc = argc;
17090                 if (strncmp(argv[1], "--debug=", 8) == 0) {
17091                         debug = atoi(argv[1] + 8);
17092                         argv++;
17093                         argc--;
17094                 }
17095                 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
17096                         label_prefix= argv[1] + 15;
17097                         argv++;
17098                         argc--;
17099                 }
17100                 else if ((strcmp(argv[1],"-O") == 0) ||
17101                         (strcmp(argv[1], "-O1") == 0)) {
17102                         optimize = 1;
17103                         argv++;
17104                         argc--;
17105                 }
17106                 else if (strcmp(argv[1],"-O2") == 0) {
17107                         optimize = 2;
17108                         argv++;
17109                         argc--;
17110                 }
17111                 else if ((strcmp(argv[1], "-o") == 0) && (argc > 2)) {
17112                         ofilename = argv[2];
17113                         argv += 2;
17114                         argc -= 2;
17115                 }
17116                 else if (strncmp(argv[1], "-mcpu=", 6) == 0) {
17117                         cpu = arch_encode_cpu(argv[1] + 6);
17118                         if (cpu == BAD_CPU) {
17119                                 arg_error("Invalid cpu specified: %s\n",
17120                                         argv[1] + 6);
17121                         }
17122                         argv++;
17123                         argc--;
17124                 }
17125         }
17126         if (argc != 2) {
17127                 arg_error("Wrong argument count %d\n", argc);
17128         }
17129         filename = argv[1];
17130         compile(filename, ofilename, cpu, debug, optimize, label_prefix);
17131
17132         return 0;
17133 }